Thursday, September 5, 2019

directx - SV_POSITION in pixel shader


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?



Answer



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


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...