6
\$\begingroup\$

What are the uses for SV_POSITION in the pixel shader? Previously this was the POSITION semantic and it wasn't readable in the pixel shader, but now that it is, what can it be used for?

In an SM2 deferred renderer, you'd typically take the texture coordinates, transform them to the [-1, 1] range and then use an inverse view/position matrix to get the world position of a pixel. Would SV_POSITION be usable here to reduce any of the calculations? And if so, how would you do that?

\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

SV_Position in the pixel shader gives you the the center point of the pixel being shaded, in a [0, bufferWidth] x [0, bufferHeight] range. (It can also be used in centroid mode with MSAA to get the centroid of the covered samples.) This value can be passed directly to Texture2D.Load to retrieve the corresponding pixel from a texture the same size as the screen. So it may not be necessary to pass screen-space UVs in an interpolator, for things like the G-buffer in deferred shading.

For reconstructing the world space position you would still need to load from the depth buffer, convert it to linear depth and combine that with the screen-space position. SV_Position wouldn't really help you with that part. (FYI, you don't need to do a full matrix multiply in the pixel shader, though; you can do most of the math in the vertex shader and reduce the pixel shader part to linear depth * an interpolated vector + camera position.)

\$\endgroup\$
4
  • \$\begingroup\$ Alright so, not so useful in this case. I am curious about that optimization though, could you explain more about it or do you have a link showing how to do it? (Im not very good at matrix math) \$\endgroup\$
    – Telanor
    Jun 14, 2012 at 2:45
  • \$\begingroup\$ @Telanor The idea is to do the inverse view matrix multiply in the vertex shader, assuming a depth of 1.0 in world units, and with no translation. That gives you a vector which you then interpolate and multiply by the actual linear depth in the pixel shader, and add to the camera position. \$\endgroup\$ Jun 14, 2012 at 3:44
  • \$\begingroup\$ Thanks! Is there an authoritative source on this? Looking at MSDN it isn't as detailed. \$\endgroup\$ Feb 26, 2018 at 0:13
  • 1
    \$\begingroup\$ @AlexBudovski I don't know of one unfortunately. I believe MS has a more detailed D3D spec for their partners but it is not available publicly. \$\endgroup\$ Feb 26, 2018 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .