why we save the oPos.zw to vDepth.xy in vertex shader and return x/y in depth pass pixel shader.

Started by
6 comments, last by db123 12 years, 1 month ago
I am reading this article:http://mynameismjp.w...ion-from-depth/

reference code:

// Depth pass vertex shader
output.vPositionCS = mul(input.vPositionOS, g_matWorldViewProj);
output.vDepthCS.xy = output.vPositionCS.zw;
// Depth pass pixel shader (output z/w)
return input.vDepthCS.x / input.vDepthVS.y;


in the first section, MJP demonstrate a traditional method to render depth, I can't understand why we must save zw to xy, and return x/y in pixel shader, why not we return [color=#494949]

vPositionCS.z directly?

i use this code to render a depth buffer for shadow map, it works well, does that have any problem?


float4 DrawShadowMapVS( HStaticMeshPositionVertexFactory Input ):SV_Position
{
float4 vWorldPos = mul( float4(Input.vPos, 1.0f ), mWorld );
float4 oPos = mul( vWorldPos, mLightViewProj );
return oPos;
}
float4 DrawShadowMapPS( float4 vPos : SV_Position ) : SV_Target0
{
float z = vPos.z;
return float4(z,z,z,1.0);
}

Advertisement
Hello

The equivalent of a 3d vertex expressed in homogeneous coordinates, let's say : (x,y,z,w) is, in cartesian coordinates : (x/w,y/w,z/w).
So in order to output the depth, we must return z/w (so in this particular case, x/y)

nb : .... and (in the case w=0) the vector expressed in homogeneous coordinates: (x,y,z,0) represents the 3d vector (x,y,z)

Hope it helps
Nico

Hello

The equivalent of a 3d vertex expressed in homogeneous coordinates, let's say : (x,y,z,w) is, in cartesian coordinates : (x/w,y/w,z/w).
So in order to output the depth, we must return z/w (so in this particular case, x/y)

nb : .... and (in the case w=0) the vector expressed in homogeneous coordinates: (x,y,z,0) represents the 3d vector (x,y,z)

Hope it helps
Nico


I can' under stand this question: why not we return [color=#000000][size=2]

vPositionCS.z/

[color=#000000][size=2]

vPositionCS.w?why we need copy zw to another register?

[color=#000000][size=2][color=#000000][size=2]

sad.png





[color="#000000"]

I return vPos.z/vPos.w in my code, it works wrong, I guess after vertex shader, it will help me divide w automaticly if the output semantic is SV_Position, so I return vPos.z, it works well.


[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

rolleyes.gif[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

[color=#000000]

blink.png

[font=courier new,courier,monospace]return [color=#000000]

output

[color=#666600]

.

[color=#000000]

vPositionCS

[color=#666600]

.

[color=#000000]

z/

[color=#000000]

output

[color=#666600]

.

[color=#000000]

vPositionCS

[color="#666600"].[color="#000000"]w

[/font]


[font=arial, helvetica, sans-serif]...should work normally[/font]

[font=arial, helvetica, sans-serif]isn't it ?[/font]

about SV_POSITION

[font=arial,helvetica,sans-serif]in this case, the z component of the associated vertex (provided to the pixel shader input) is already divided by w.
So returning z directly from the vertex shader should work.

else

return [/font]

output

[color=#666600]

.

[color=#000000]

vPositionCS

[color=#666600]

.

[color=#000000]

z/

[color=#000000]

output

[color=#666600]

.

[color=#000000]

vPositionCS

[color=#666600]

.

[color=#000000]

w





[font=arial,helvetica,sans-serif][color=#000000]should work I suppose. Isn't it ?[/font]






[font=arial,helvetica,sans-serif][color=#000000]should work I suppose. Isn't it ?[/font]

about SV_POSITION
in this case, the z component of the associated vertex (provided to the pixel shader input) is already divided by w.
So returning z directly from the vertex shader should work.

else z/w

[font=arial, helvetica, sans-serif][color=#000000]should work I suppose. Isn't it ?[/font]

a topic about SV_POSITION
so :

return output.vPositionCS.z

...should be fine with this semantic
The result from the z component SV_Position should be equivalent. I wrote it that way so that people could easily translate the code to D3D9.

The result from the z component SV_Position should be equivalent. I wrote it that way so that people could easily translate the code to D3D9.

thank you, what is the problem in d3d9 if i return z or z/w instead of return x/y?rolleyes.gif
In D3D9 the pixel shader input position only had XY.

This topic is closed to new replies.

Advertisement