在顶点着色器中绘制全屏四边形。

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

Draw full screen quad in vertex shader

问题

// 从Apple Metal文档中找到了这个示例代码。这个顶点着色器如何使用3个顶点绘制一个全屏四边形?

// 在设置管道状态后绘制命令
renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)

typedef struct {
float4 position [[position]];
half3 worldNormal;
} ColorInOut;

/// 一个生成全屏四边形的顶点函数。
vertex ColorInOut quadPassVertex(uint vid[[vertex_id]])
{
ColorInOut out;

  1. float4 position;
  2. position.x = (vid == 2) ? 3.0 : -1.0;
  3. position.y = (vid == 0) ? -3.0 : 1.0;
  4. position.zw = 1.0;
  5. out.position = position;
  6. return out;

}

英文:

I came across this sample code from Apple Metal documentation. How does this vertex shader draw a full screen quad using 3 vertices ?

  1. // Draw command after setting pipeline state
  2. renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)
  3. typedef struct {
  4. float4 position [[position]];
  5. half3 worldNormal;
  6. } ColorInOut;
  7. /// A vertex function that generates full screen quad pass.
  8. vertex ColorInOut quadPassVertex(uint vid[[vertex_id]])
  9. {
  10. ColorInOut out;
  11. float4 position;
  12. position.x = (vid == 2) ? 3.0 : -1.0;
  13. position.y = (vid == 0) ? -3.0 : 1.0;
  14. position.zw = 1.0;
  15. out.position = position;
  16. return out;
  17. }

答案1

得分: 1

所绘制的三角形覆盖了归一化设备坐标的整个区间[-1, 1],无论是在x轴还是y轴方向。三角形超出[-1, 1]范围的额外部分会在管道的后续阶段进行裁剪。

英文:

The triangle that's drawn covers the whole normalized device coordinates [-1, 1] in both x and y directions. The extra area of the triangle that's outside the [-1, 1] bounds gets clipped in later stages of the pipeline.

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

发表评论

匿名网友

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

确定