Sign in to follow this  

Accurate Z in pixel shader -- possible?

This topic is 3862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

(DX9, SM3) I want to get the Z value of the pixel in the pixel shader. I try to do it by transferring the transformed coordinates from the vertex shader via texture coords. Unfortunately, the Z coordinates I get this way don't match the real Z very well. In a simple test program, I use the following shader:
void DepthVS(float3 inPos : POSITION,
	     out float4 outPos: POSITION,
	     out float3 outTexPos : TEXCOORD0)
{
	outPos = float4(inPos,1);
	outTexPos.xy = (outPos.xy + 1) / 2 * gRenderArea.xy;
	outTexPos.y = gRenderArea.y - outTexPos.y; // reverse y.
	outTexPos.z = outPos.z;
}
This works if the original coords don't need high accuracy. 0, 1, 0.2 work well. However, given numbers such as 0.1111, the (x,y) interpolation starts returning values which are sometimes quite a bit (~0.005) off from the VPOS coordinate, coupled with ddx and ddy values which aren't (1,0) or (0,1) for x and y. Naturally this extends to the z coordinate (though there's nothing to compare it with). Note: this is all debugged under the ref device. Hardware writes similar results, but I haven't tried to write all the interpolation values from it. I added rounding code:
void DepthVS(float3 inPos : POSITION,
	     out float4 outPos: POSITION,
	     out float3 outTexPos : TEXCOORD0)
{
	outPos = float4(inPos,1);
	outTexPos.xy = (outPos.xy + 1) / 2 * gRenderArea.xy;
	outTexPos.y = gRenderArea.y - outTexPos.y; // reverse y.
	outTexPos.xy = round(outTexPos.xy*16)/16;
	float2 roundedPos = outTexPos.xy * 2 / gRenderArea.xy - 1;
	outPos.xy = roundedPos;
	outTexPos.z = outPos.z;
}
In this case I round to 1/16 of a pixel, and this results in good interpolation. Any more accuracy (such as 1/32), and errors start to creep in. This rounding would be fine, but I have no idea how to apply it to z. So it seems that I can get accurate x,y positions in several ways (VPOS being the easiest), but can anyone tell me a way to get an accurate z? Hopefully those of you who have worked with shadow algorithms and such can shed some light on this. Thanks! Edit: The error in x,y can actually get to around 0.05 in size, not 0.005. [Edited by - ET3D on May 21, 2007 3:24:45 PM]

Share this post


Link to post
Share on other sites

This topic is 3862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this