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

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

## Recommended Posts

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

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

[color="#000000"]

Hidden

Hidden

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

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

so :
 return output.vPositionCS.z 
...should be fine with this semantic

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

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 24
• 43
• 75