三角形由GPU绘制时占用15像素而不是16像素的原因是什么?

huangapple go评论53阅读模式
英文:

What is the reason behind a triangle's width drawn by GPU occupying 15 pixels instead of 16 pixels?

问题

我想在一个160*160的彩色附件中绘制一个三角形。坐标是

-1.0,1.0,0.0
-0.9,0.9,0.0
-0.8,1.0,0.0

在我看来,三角形的宽度是0.2,所以它应该占据16个像素。
但结果是15。
三角形由GPU绘制时占用15像素而不是16像素的原因是什么?

我想知道GPU如何处理这种情况?我能找到一些算法或公式吗?

英文:

I want to draw a triangle in a 160*160 color attachment. the coordination is

-1.0,1.0,0.0
-0.9,0.9,0.0
-0.8,1.0,0.0

In my opinion, the width of triangle is 0.2, so it should occupy 16 pixels.
but the result is 15.
三角形由GPU绘制时占用15像素而不是16像素的原因是什么?

I want to know how GPU process this situation? Can i find some algorithm or formula?

答案1

得分: 5

你的三角形边缘恰好通过像素中心(在半整数坐标处)。因此,边缘上的像素是否属于三角形是不确定的。

一个适用的规则是,共享边的三角形必须将该边上的像素精确光栅化一次,以防止混合等问题。一个实现方法是在单独光栅化每个三角形时包括左边缘并排除右边缘(例如)。

请注意,这仅适用于当采样点恰好位于边缘上时的决策。如果你将三角形提高了四分之一像素,所有采样点将明确位于三角形内部或外部,其表面宽度将与预期一致。

为了更清晰地说明,这里有一个图示,三角形绘制在一个60x60的帧缓冲区上。钻石表示每个像素内的采样点位置。白色钻石位于三角形外部,因此不被阴影。黑色钻石被确定在三角形内部,因此被阴影。红色钻石恰好位于右边缘上,如前所述被认为在三角形外部,因此它们也不会被阴影。结果是,三角形在屏幕上的宽度仅为五个像素,而不是您所期望的六个像素:

三角形由GPU绘制时占用15像素而不是16像素的原因是什么?

英文:

The edges of your triangle pass exactly through pixel centers (at half-integer coordinates). Therefore whether those pixels on the edge belong to the triangle or not is not well determined.

One applicable rule is that triangles that share an edge must rasterize the pixels on that edge exactly once, in order to prevent artifacts with blending, among others. The way an implementation would do that is to include the left edges and exclude the right edges (for example) when rasterizing each triangle individually.

Note that this applies only to the tie-breaking when the sample is exactly on the edge. If you raised your triangle just by quarter of a pixel, all samples would be unambiguously inside or outside the triangle, and the apparent width of it would be as you'd expect.

Here's a diagram for clarity, where the triangle is drawn onto a 60x60 framebuffer. The diamonds indicate the location of the samples within each pixel. The white diamonds lay outside the triangle and thus aren't shaded. The black diamonds are determined to be within the triangles and are therefore shaded. The red diamonds happen to be exactly on the right edge, and are considered to be outside of the triangle as explained earlier, so they aren't shaded either. The result is that the triangle is only five pixels in width on the screen rather than six that you'd expect:

三角形由GPU绘制时占用15像素而不是16像素的原因是什么?

huangapple
  • 本文由 发表于 2023年6月5日 11:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定