Jump to content
  • Advertisement
Sign in to follow this  
db123

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

This topic is 2486 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

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);
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Hidden

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

Share this post


Link to post
Hidden
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]

Share this post


Link to post
Hidden
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!