Search code examples
openglglslopenframeworks

A few questions regarding GLSL pipeline


I used to code HLSL when I was working with DirectX but have forgotten quite a lot and I am now trying to figure out GLSL and how it works. I am using a fixed pipeline of OpenGL in OpenFramework which is sort of a new concept to me but a good starting point.

My first question: For the Vertex Shader all vertices will be computed before entering the Fragment Shader, correct? So I can preccompute information here like finding the Min/Max values of the Z pos of all vertices feeded into the frame buffer?

Then when entering the Fragment Shader I can access these precomputed values by the varying variables? So if I set a value from the Vertex Shader in a varying variable (like Min/Max from a z position of a vertex) I can later access these set values from the Fragment Shader?

Also the build-in variable gl_FragCoord when accessing the Z element always seem to yield a 1.0 for every vertex for me. Is this related to how I transform the vertices' position in the Vertex Shader?

And finally I am trying to find the Min/Max Z position of all the vertices and pass them along to the Fragment Shader, is the follow code correct?

varying float minZ = 9999.9;
varying float maxZ = -9999.9;

void main()
{
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_TexCoord[1] = gl_MultiTexCoord1;
    gl_FrontColor  = gl_Color;
    gl_Position    = ftransform();

    if (gl_Position.z < minZ) minZ = gl_Position.z;
    if (gl_Position.z > maxZ) maxZ = gl_Position.z;
}

Sorry if this is an inappropriate way of asking questions on StackOverflow, perhaps I should've split it up into several questions.


Solution

  • For the Vertex Shader all vertices will be computed before entering the Fragment Shader, correct?

    No.

    So I can preccompute information here like finding the Min/Max values of the Z pos of all vertices feeded into the frame buffer?

    Not directly, because the vertex shader sees one vertex at a time and GPUs operate in parallel. Determining min/max would require something like map-reduce to be efficient, which requires some way to pass information between thread. It could be done, but not very efficiently.

    In your code snippet minZ and maxZ are not globals shared between all vertices. They are local varyings tied to the vertex currently processed.

    Then when entering the Fragment Shader I can access these precomputed values by the varying variables?

    Those are not really "precomputed", they're just processed in the immediate step before. It's not that first the vertex shader is executed for all vertices, then the fragment shader. Each primitive (i.e. a single triangle) passes right through the stages without being stored inbetween.