gl_PointSize 是如何工作的

How does gl_PointSize work

来自 DX 背景,我试图准确理解 what/how gl_PointSize 和 gl_PointCoord 的工作。我在网上和手册页上搜索过,但对它们没有很好的解释。假设我有一个 300x300 的输出缓冲区,我定义了一个包含 90,000 个点的顶点着色器,对应于 300x300 缓冲区中的每个位置(每个维度的增量为 1)。现在在顶点着色器中,如果我将 gl_PointSize 定义为 2,它会调用片段着色器 90,000 次还是 360,000 次?如果是36万次,我就能理解gl_PointCoord代表什么了。但是如果只有90,000次,那是否意味着每个片段输出然后代表4个像素?在这种情况下,gl_PointCoord 代表什么?不会一直是0.5,0.5,没啥用吧??

谢谢

OpenGL 4.5 core profile specification 的第 14.4.1 节 "Basic Point Rasterization" 指出:

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.

因此,如果点大小 > 1,可能会生成多个片段。每个片段至少调用一次片段着色器。如果我们将多重采样排除在图片之外,它会在每个片段中调用一次,因此正确答案为 360,000 次。

请注意,这也忽略了早期深度测试,它可能会或可能不会在您描述的场景中发挥作用,因为它可能会在调用 FS 之前丢弃片段。

来自规范的同一部分:

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. [...]

所以点坐标确实代表了您期望它代表的内容。请注意,此定义意味着点坐标不一定总是以 0.5 结束,即使点大小为 1 且未使用多重采样也是如此。在这种情况下,将针对 像素中心 调用片段着色器,但该点可能不完全位于像素中心,因此您将看到正在采样的 1x1 像素大点的确切位置.