Вы находитесь на странице: 1из 2

11/12/2009

Why use Shaders?
• What we’ve done until now: used a “hardwired” pipeline 
• Produces limited effects
• Effects look the same (e.g. Lighting!)
• Gamers want unique look and feel
• Multi‐texturing w/ combiners helps, but still limited
Shaders in OpenGL
p • Less interoperable, less portable
• Current graphics practice: use programmable GPU
• A shader is a small program that runs on a “GPU”
• Written in a high‐level shader language (HLSL, Cg, GLSL)
• OpenGL natively supports GLSL Language (others by extension)
• Allows greater rendering flexibility
• Achieve visual effects that would be difficult or impossible w/ fixed
• Offload highly parallel work from CPU for greater performance

Adapted from notes by Hsien-Hsin Lee, Ga Tech & Jim Whitehead, UC-Santa Cruz

Fixed versus Programmable
Demographics of a GPU
3D Apps Fixed Function Pipeline
• “Graphics Processing Unit”
API commands
• Highly Parallel SIMD Architecture
3D API: • Up to 104 concurrent threads! 
Direct3D NVidia GeForce FX
• Hardware thread scheduling
GPU cmd & data stream
• In‐order issue
Assembled
A bl d Pi l
Pixel Pi l
Pixel • NVidia 8800GTX (high‐end G80 core)
( )
Vtx index polygons location updates
• 16 Stream Multiprocessors
GPU Primitive Rasterization  Raster  Frame 
Frontend Assembly & Interpolation Operations Buffer • Each  SM contains 8 unified streaming processors – 128 in total
• NVidia GTX280 (high‐end GT200 core)
Transformed Transformed • 24 Stream Multiprocessors
vertices Fragments
• Each SM contains 8 unified streaming processors – 240 in total
Programmable  Programmable  • ~1.5 billion transistors 
Vertex Shader Fragment Shader

Adapted from Cg Tutorial, Nvidia Corp, & Hsien-Hsin Lee, Ga Tech Adapted from notes by Ashu Rege, Nvidia Corp.

Anatomy of a Shader Program Shader Component Programs
• A Shader is a program written in textual form in some high  • Vertex shaders
level Shader Language • Executed once per vertex in a scene. 
• Programs tend to have these components: • Transforms 3D position in space to 2D coordinate on screen
• Global variables • Can manipulate position, color, texture coordinates
• Built‐in variables invoked through standard calls
• Cannot add new vertices
• User‐defined data structures used within the shader programs
• Message passing architecture
M i hit t • Fragment shaders (pixel shaders)
Fragment shaders (pixel shaders)
• To pass arbitrary data from client code (C/C++) to Shader • Calculates the color of individual pixels
• To pass data between component shader programs • Used for lighting, texturing, bump mapping, etc.
• Shader component programs  • Executed once per pixel per geometric primitive
• Most commonly vertex and/or pixel shaders
• Invoked at specific locations in the pipeline on many data elements in  • Geometry shaders 
parallel (e.g. pixel, fragment, geometry) • Can add/remove vertices from a mesh
• “Techniques” (Cg, HLSL) – not covered in this lecture • Can procedurally generate geometry, or add detail to shapes
• Describe grouping of vertex and pixel shader
• Describe ordering of same
Adapted from notes by Jim Whitehead, UC-Santa Cruz Adapted from notes by Jim Whitehead, UC-Santa Cruz

1
11/12/2009

Summary from last lecture (1) Summary from last lecture (2)
Graphics Processing Unit (GPU)
Understanding GLSL Shader program structure
• Replaces traditional “fixed function” graphics pipeline
• Massively parallel, special‐purpose SIMD processor • Variable qualifiers
• Executes on thousands of threads concurrently – Determine variable scope & access capability of global variables
Shader programs (GLSL) – Essential for communicating between program components
• Vertex shaders • C code to vertex shader
• Executes once per vertex • Vertex shader to fragment shader
• Input: vertex & associated data from C code (e.g. glVertex*) – 3 Qualifier types:
• Uniform
• Minimal output: updated vertex position, color
Mi i l t t d t d t iti l
– Visible to both vertex & fragment shaders
• Fragment shaders
– Do not change within a mesh declaration , e.g. set prior to glBegin() ‐ glEnd() block
• Executes once per pixel or sub‐pixel region – Can be written only by calling program (C code), cannot be changed by shader programs
• Input: transformed data from vertex shader • Attribute
– Values are rasterized, bilinearly interpolated – Visible to vertex shaders  but not fragment shaders
– Hardware does this for us automatically – Typically set once per vertex, e.g. make a call to change attrib. before glVertex() call
• Typically fragment shader invocations >> vertex shader invocations  – Can be written only by calling program (C code), cannot be changed by shader programs
– Typically by orders of magnitude • Varying
– But not always!  (Data sensitive) – Not visible to C code program
• Output: color value to framebuffer – Typically set once per vertex shader invocation
• Each has void main() entry point – Can only be written in vertex shader and read by fragment shader
• No pointers or direct memory access (with major exception discussed shortly!) – Values interpolated automatically for each fragment during rasterization

Summary from last lecture (3) Summary from last lecture (4)
Understanding GLSL Shader program structure (cont) Understanding GLSL Shader program structure (cont)

• Data access & communication • Shader program initialization
– Shader program variables stored in a lookup table – Obtain an empty program shell from GL:  glCreateProgram()
– Can change values in client (C code) program  – Shader program encapsulates vertex shader, fragment shader
– 2 Step process: – Must add vertex, fragment shaders to it (typically 1 of each)
– Ask GL for variable by name: – For each vertex, fragment  shader...
– glGet{Uniform|Attribute}Location
glGet{Uniform|Attribute}Location  – Read in code from source file (write a function to read ASCII file)
Read in code from source file (write a function to read ASCII file)
– Parameters: shader program handle, “var name” – Get a fresh handle: glCreateShader()
– Returns numeric ID (handle) to variable in table – GL_VERTEX_SHADER
– Set the variable state:  – GL_FRAGMENT_SHADER
– glVertexAttrib, glUniform – Give the shader source code: glShaderSource()
– Function has type qualified postfix (e.g. 3,4,f,d,v etc.) – Compile the source code: glCompileShader()
– Parameters: handle to shader variable, type‐correct value – Attach to the program shell: glAttachShader()
– Applies only to Uniform, Attribute qualified – Link the program: glLinkProgram()
– Tell GL to actually use it: glUseProgram()
– At the right moment in your code…
– Can turn on/off selectively!
– One C program can use many shaders.

Вам также может понравиться