1
votes

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?

1
Not sure but I suspect a geometry shader using the lines input (and output?) primitive would do the trick. Might be a better way though.G.M.
"in particular, the coefficients of the line equation ax + by + c = 0." That sounds suspiciously like something that you can avoid having to compute per-primitive and can just interpolate if you're sufficiently clever. Can you explain what you're doing with this equation in the FS? If it's some kind of linear math, I'd bet it can be precomputed in the VS and interpolated.Nicol Bolas
I want to implement GL_LINE_SMOOTH in shader, and use the line equation to compute its distance to the fragment.lvella
Compute shaders are your best bet. A very silly (but effective) alternative would be to include the required data for your calculation as a vertex attribute and just do it in the vertex shader.3Dave

1 Answers

1
votes

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.