Suppose I drew a quad onto the screen with a custom value being passed from the vertex shader to the pixel shader, as shown in the attachment. (The 0s and 1s correspond to the value being assigned in the vertex shader).
In the pixel shader, what value would be read for the top left pixel? Would it be 0.25 or 0 (assuming the same z for all 4 vertices)? i.e. is the interpolated value obtained from the center of the pixel or from the corner?
Finally, is this behavior the same in OpenGL and DirectX (or between shading langues)? And does it depend on which interpolator is used (e.g. TEXCOORD0 vs NORMAL vs POSITION in HLSL)?
Thanks!
Interpolators
just take a look at the OpenGL specs.
http://www.opengl.org/registry/doc/GLSLangSpec.4.10.6.clean.pdf: 4.3.9 Interpolation
http://www.opengl.org/registry/doc/glspec41.core.20100725.pdf:3.5 Line Segments, 2.19 Flatshading
in a shader you can tell openGL excatly how every single varrying should be interpolated (perspective correct, linear or flat)
you can also decide at which position each fragment should be sampled.
the standart way is that u get perspective interpolation with random samples.
So to answer your question, there is no guarantee which value your fragments would have in your example.
The left top fragment could have any value between 0. and 0.5
http://www.opengl.org/registry/doc/GLSLangSpec.4.10.6.clean.pdf: 4.3.9 Interpolation
http://www.opengl.org/registry/doc/glspec41.core.20100725.pdf:3.5 Line Segments, 2.19 Flatshading
in a shader you can tell openGL excatly how every single varrying should be interpolated (perspective correct, linear or flat)
you can also decide at which position each fragment should be sampled.
the standart way is that u get perspective interpolation with random samples.
So to answer your question, there is no guarantee which value your fragments would have in your example.
The left top fragment could have any value between 0. and 0.5
Thanks for the replies. If it's always 0.25 that's exactly what I need but I'll also look more at the gl spec to see if there's a way to enforce that. (The reason is for computing shadow bias.)
D3D9 interpolates the same, but because it addresses pixels in a different way, you can accidentally get "0.0" instead of "0.25" by specifying slightly incorrect vertex positions.
like the glsl specs point out you can use the centroid keyword, which enforces to sample at the middle of the pixel.
eg. centroid in vec2 texcoord;
eg. centroid in vec2 texcoord;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement