Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does gl_PointSize work

Coming from a DX background, I am trying to comprehend exactly what/how gl_PointSize and gl_PointCoord work. I've searched online and the man pages, but haven't had a really good explanation on them. Say I have a 300x300 output buffer, and I defined a vertex shader with 90,000 points, corresponding to every location in the 300x300 buffer (with increments of 1 in each dimension). Now in the vertex shader if I define a gl_PointSize of 2, does it invoke the fragment shader 90,000 times or 360,000 times?? If it's 360,000 times, I can understand what gl_PointCoord represents. But if it's only 90,000 times, does that mean each fragment output then represents 4 pixels? And in this case, what does gl_PointCoord represent? Wouldn't it always be 0.5,0.5 and not really useful??

Thanks

like image 996
user1181950 Avatar asked Aug 24 '15 22:08

user1181950


1 Answers

Section 14.4.1 "Basic Point Rasterization" of the OpenGL 4.5 core profile specification states:

Point rasterization produces a fragment for each framebuffer pixel whose center lies inside a square centered at the point’s (xw; yw), with side length equal to the current point size.

So in case of a point size > 1, several fragments might be generated. The fragment shader is invoked at least once per fragment. If we keep multisampling out of the picture it is invoked once per fragment, so 360,000 times is the correct answer.

Note that this also ignores the early depth test which might or might not play a role in the scene you describing, as it might discard the fragments before the FS is invoked.

From the same section of the spec:

All fragments produced in rasterizing a point sprite are assigned the same associated data, which are those of the vertex corresponding to the point. However, the fragment shader built-in gl_PointCoord contains point sprite texture coordinates. The s point sprite texture coordinate varies from zero to one across the point horizontally left-to-right. If POINT_SPRITE_COORD_ORIGIN is LOWER_LEFT, the t coordinate varies from zero to one vertically bottom-to-top. Otherwise if the point sprite texture coordinate origin is UPPER_LEFT, the t coordinate varies from zero to one vertically top-to-bottom. [...]

So the point coordinate does exactly represent what you would expect it to represent. Note that this definition means that the point coordinate does not necessarily always end up as 0.5 even if the point size is 1 and no multisampling is used. In that case, the fragment shader is invoked for pixel centers, but the point might not lie exactly on a pixel center, so you will see where exactly in the 1x1 pixel big point you are sampling.

like image 159
derhass Avatar answered Oct 14 '22 07:10

derhass