本文介绍了DirectX 11 Pixel Shader什么是SV_POSITION?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习DirectX 11的HLSL,我想知道SV_POSITION是什么,它是Vertex Shader的输出,还是Pixel Shader的输入.

I am learning HLSL for DirectX 11, and I was wondering what exactly is the SV_POSITION that is the output for a Vertex Shader, and the input for a Pixel Shader.

1:是屏幕上每个像素或对象的x,y,z?

1: Is this x,y,z of every pixel on your screen, or of the object?

2:为什么会有4个32位浮点数?

2: Why is it 4 32bit floats?

3:顶点输出是否需要此系统变量?

3: Do you need this System Variable for the vertex output?

谢谢!

推荐答案

顶点着色器阶段只有一个必需的输出:顶点的位置.然后,固定功能栅格化器使用此值来计算要绘制的像素,并为每个像素调用像素着色器.这就是系统值语义SV_Position在顶点着色器的输出上指示的内容.像素着色器实际上不需要将像素位置作为输入,但是如果有用的话就可以.输入布局还必须具有使用SV_Position语义的顶点着色器的位置.

The vertex shader stage only has one required output: the position of the vertex. This value is then used by the fixed-function rasterizer to compute which pixels are being drawn and invoke the pixel shader for each one. That's what the system value semantic SV_Position indicates on the output of a vertex shader. A pixel shader doesn't actually need to take the pixel position as input, but it can if that is useful. The input layout must also have a position for the vertex shader which uses the SV_Position semantic as well.

通常设置为:

  • 顶点布局描述了顶点在世界空间中的位置,表示为SV_Position.输入布局描述了数据的格式.
  • Tnput layout describes the positions of the vertices in world space indicated as SV_Position. The input layout describes the format of the data.

例如:

D3D11_INPUT_ELEMENT_DESC layout[] =
{
    { "SV_Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

  • 顶点着色器输入从输入汇编器接收该信息,该信息由输入布局从顶点缓冲区解码,并且还可以选择使用索引缓冲区.无论位置采用哪种格式,通常都将其转换为float以供着色器使用.

    • Vertex shader input receives that information from the input assembler as decoded by the input layout from the vertex buffer, optionally using an index buffer as well. Whatever format the position is in, typically it's converted to float for use by the shader.

      顶点着色器必须产生输出顶点位置,再次由SV_Position指示.这是像素在(-1, -1)(1, 1)范围内的归一化坐标中的(x, y)位置. z是归一化范围01的深度位置(用于深度缓冲区).

      The vertex shader has to produce the output vertex position, again indicated by SV_Position. This is the (x, y) position of the pixel in normalized coordinates in the range (-1, -1) to (1, 1). The z is the depth position (used for the depth buffer) in the normalized range 0 to 1.

      例如:

      float4 VS( float4 Pos : SV_Position ) : SV_Position
      {
          return Pos;
      }
      

      • 像素着色器可以有选择地占据位置,但不是必须的. (x,y)以像素坐标表示.通过使用D3D11_VIEWPORT中提供的视口状态,将顶点输出(剪辑空间")转换为像素.

        • The pixel shader can optionally take the position, but it doesn't have to. The (x,y) are in pixel coordinates. The vertex output ('clip space') is converted to pixels by using the viewport state that was provided in D3D11_VIEWPORT.

          像素着色器必须产生标记为SV_Targetfloat4结果.

          The pixel shader must produce a float4 result marked as SV_Target.

          例如:

          // We can omit the position
          float4 PS() : SV_Target
          {
              return float4( 1.0f, 1.0f, 0.0f, 1.0f );
          }
          
          // Or you can take it as input if that's helpful
          float4 PS( float4 Pos : SV_Position ) : SV_Target
          {
              return float4( 1.0f, 1.0f, 0.0f, 1.0f );
          }
          

          从技术上讲,顶点着色器不必将位置作为输入.使用Shader Model 4.0或更高版本的硬件,您可以使用SV_VertexId在具有以下代码的代码中自生成顶点着色器中四边形的角,如下所示,该代码不使用任何输入布局,顶点缓冲区或索引缓冲区完全没有.当然,它仍然必须为像素着色器生成输出位置.

          Technically the vertex shader doesn't have to take a position as input. With Shader Model 4.0 or better hardware, you can use the SV_VertexId to self-generate the corners of a quad in a vertex shader with code like the following which doesn't make use of any input layout, vertex buffer, or index buffer at all. It still has to produce an output position for the pixel shader of course.

          VSInputTx VSQuad(uint vI : SV_VertexId)
          {
              VSInputTx vout;
          
              float2 texcoord = float2(vI & 1, vI >> 1);
              vout.TexCoord = texcoord;
          
              vout.Position = float4((texcoord.x - 0.5f) * 2, -(texcoord.y - 0.5f) * 2, 0, 1);
              return vout;
          }
          

          当您不熟悉DirectX时,建议您查看 DirectX工具工具包,尤其是内置着色器源

          As you are new to DirectX, I recommend you take a look at the DirectX Tool Kit and in particular the built-in shader source.

          这篇关于DirectX 11 Pixel Shader什么是SV_POSITION?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:01