Howto:Shader programming in FlightGear: Difference between revisions

m
mNo edit summary
Line 56: Line 56:


There are two types of shaders in GLSL: "vertex shaders" and "fragment shaders".
There are two types of shaders in GLSL: "vertex shaders" and "fragment shaders".
These are executed by vertex and fragment processors in the graphics hardware.


So, shaders generally go around in pairs - one shader (the "Vertex shader") is a short program that takes in one vertex from the main CPU and produces one vertex that is passed on to the GPU rasterizer which uses the vertices to create triangles - which it then chops up into individual pixel-sized fragments.  
So, shaders generally go around in pairs - one shader (the "Vertex shader") is a short program that takes in one vertex from the main CPU and produces one vertex that is passed on to the GPU rasterizer which uses the vertices to create triangles - which it then chops up into individual pixel-sized fragments.  
A vertex shader is run once per vertex, while a fragment shader is run once per pixel.
Many such executions can happen in parallel. There is no communication or ordering between
executions.
Vertex shaders are flexible and quick


== Vertex Shaders ==
== Vertex Shaders ==
Note: Loading a vertex shader turns off parts of the OpenGL pipeline
Input: Vertex attributes
 
Output: At least vertex position (in the clip space)
 
Restrictions: Cannot access any vertex other than the current one
 
Note: Loading a vertex shader turns off parts of the OpenGL pipeline (vertex shaders fully replace the "T&L Unit")


Vertex shaders operate on every vertex, the vertex shader is executed for each vertex related OpenGL call (e.g. glVertex* or glDrawArrays).
Vertex shaders operate on every vertex, the vertex shader is executed for each vertex related OpenGL call (e.g. glVertex* or glDrawArrays).
Line 66: Line 80:


Vertex Shaders take application geometry and per-vertex attributes as input and transform the input data in some meaningful way.
Vertex Shaders take application geometry and per-vertex attributes as input and transform the input data in some meaningful way.
Common tasks for a vertex shader include:
* Vertex position transformation
* Per vertex lighting
* Normal transformation
* Texture coordinates transformation or generation
* Vertex color computation
* Geometry skinning


The vertex shader runs from start to end for each and every vertex that's passed into the graphics card - the fragment process does the same thing at the pixel level. In most scenes there are a heck of a lot more pixel fragments than there are vertices - so the performance of the fragment shader is vastly more important and any work we can do in the vertex shader, we probably should.  
The vertex shader runs from start to end for each and every vertex that's passed into the graphics card - the fragment process does the same thing at the pixel level. In most scenes there are a heck of a lot more pixel fragments than there are vertices - so the performance of the fragment shader is vastly more important and any work we can do in the vertex shader, we probably should.  
Line 77: Line 99:


== Fragment Shaders ==
== Fragment Shaders ==
Note: Loading a fragment shader turns off parts of the OpenGL pipeline
Input: Interpolation of the vertex shader outputs
 
Output:Usually a fragment color.
 
Restrictions: Fragment shaders have no knowledge of neighboring pixels.
 
Note: Loading a fragment shader turns off parts of the OpenGL pipeline (pixel shaders fully replace the "Texturing Unit")
 
The other shader (the "Fragment shader" - also known (incorrectly) as the "Pixel shader") takes one pixel from the rasterizer and generates one pixel to write or blend into the frame buffer.
 
Common tasks of fragment shaders include:
* Texturing (even procedural)
* Per pixel lighting
* Fragment color computation
 


The other shader (the "Fragment shader" - also known (incorrectly) as the "Pixel shader") takes one pixel from the rasterizer and generates one pixel to write or blend into the frame buffer. A minimum fragment shader may look like this:
A minimum fragment shader may look like this:


  void main(void)
  void main(void)
2,561

edits