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

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

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;

float4 position;
position.x = (vid == 2) ? 3.0 : -1.0;
position.y = (vid == 0) ? -3.0 : 1.0;
position.zw = 1.0;

out.position = position;

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 ?

// Draw command after setting pipeline state    
renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)

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

/// A vertex function that generates full screen quad pass.
vertex ColorInOut quadPassVertex(uint vid[[vertex_id]])
{
    ColorInOut out;
    
    float4 position;
    position.x = (vid == 2) ? 3.0 : -1.0;
    position.y = (vid == 0) ? -3.0 : 1.0;
    position.zw = 1.0;
    
    out.position = position;
    
    return out;
}

答案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:

确定