像素着色器中SV_POSITION的z组件

KIM CHANGJUN

嗨,我是DirectX的新手,并且在像素着色器中苦于SV_POSITION。

所以我的像素着色器照常从SV_POSITION input(float4)获取输入,我只是猜测此输入的z分量将为我提供此着色器要处理的像素的深度值。所以我只是做了一些简单的测试。

像素着色器

float4 main(PS_INPUT input) : SV_Target
{
    float Z = input.pos.z / input.pos.w;
    if (Z<0.1f)
        return RED;

    ...//draw texture
}

在此处输入图片说明 在此处输入图片说明事实证明,所有的obj都是红色的,除非我的摄像头真的很近。与我预期的结果恰好相反。

所以我只是猜测顶点着色器中SV_POSITION的输出值将不同于像素着色器中相同语义的输入值?

任何意见,将不胜感激。

亚历克斯

You're guess is correct - the input to the pixel shader is not the same as the output from the vertex shader. The SV_Position value has some special semantics associated with it that prevents this from occuring.

When doing rendering in Direct3D 11, there are several pipeline stages that take place before your data appears on the screen. These stages are a mostly programmable, but some are fixed by the system or the hardware. Those five fixed stages (in order of appearance) are the Input Assembler, the Tesselator, the Stream Output, the Rasterizer, and the Output Merger. These five stages make various changes to the input data according to specific information provided to Direct3D.

您的问题涉及第四阶段,光栅化器此阶段将获取您尝试渲染的3D几何的坐标,并生成将受渲染操作影响的像素坐标。这些像素坐标传递给像素着色器,每个生成的像素运行一次着色器。顶点着色器创建的原始坐标已被光栅化器消耗。

如果需要将深度值传递给像素着色器,则可以通过自定义语义提供该信息,或者尝试从value中访问它SV_Depth

您可以在此处找到有关管道阶段的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章