# Accurate Z in pixel shader -- possible?

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

## 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]

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002310
×