Search code examples
openglglslshader

What shader to compute something about a primitive?


I want to take all the vertices of my primitive (in particular, 2 vertices of a GL_LINE), and compute some stuff with them to be used by the fragment shader (in particular, the coefficients of the line equation ax + by + c = 0).

Since I don't see the other vertices of the primitive in the vertex shader, is there a shader stage best suited to compute such information?

I don't want to do in CPU because I want the information after transformation and projection. Can I do it in one of the tesselation or geometry shaders, and pass the output as a flat varying to fragment shader. In this case, the same primitive would be output, unchanged.

Is it possible? Is it a good idea?


Solution

  • Most shader stages have a name that describes what it actually does. A "vertex shader" takes a vertex as input and produces a vertex as output. A "fragment shader" takes a fragment as input and produces a fragment as output. A "tessellation evaluation shader" is a bit more oblique, but it does evaluate the tessellation operation performed by the fixed-function tessellation unit.

    "Geometry shader" is the odd one out. It takes a primitive as input and produces zero or more primitives as output. By all rights, it ought to have been called a "primitive shader", but blame Microsoft for the name.

    If you're looking to compute the window-space distance of a fragment's sample position from the line that generated that fragment, then you can use a geometry shader.