Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.)

share|improve this answer
    
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) –  Telanor Jun 14 '12 at 2:45
    
@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. –  Nathan Reed Jun 14 '12 at 3:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.