# hlsl float comparisation

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

## Recommended Posts

Hi all, i'm currently working on shadow mapping but i stumbled upon some weird behavior in hlsl while using float comparisation. I need to calculate the distance to the light but i didnt find a general way to do this for both ortho and projection matrices so i decided to make an if statement in the shader to see which calculation is needed.

I tried providing the maximum distance of the light and for directional lights provide 0 to indicate orthographical projection is used and just use the vertex position's z as depth.

here's what i tried using:
if( maxLightDistance <= 0.2 ){	Out.fDepth = Out.position.z;}else{	Out.fDepth = length( Out.position ) / maxLightDistance;}

the comparisation uses <= because i thought maybe a float 0 isn't actually 0. However when i provide maxLightDistance 0 still the second calculation is used. 0 isn't smaller than 0.2?

To make it more funny, providing 0.1 does actually work but yeah.

Now i swapped to comparisation to:
if( maxLightDistance > 0.0 ){	Out.fDepth = length( Out.position ) / maxLightDistance;}else{	Out.fDepth = Out.position.z;}

and with this source providing 0 works.

Now what i want to know is if there is a general approach to handle both ortho and perspective depth calculations in the same manner and how comes the first comparisation failes.

##### Share on other sites
that code i posted is part of it, here's the rest of the shadow generating part with only the last chunk from my preveous post:

float4x4 World;float4x4 LightProj;float maxLightDistance;struct VSOUTPUT_SHADOW{	float4 position : POSITION;	float fDepth : TEXCOORD0;};//Shadow generation vertex shaderVSOUTPUT_SHADOW ShadowVs( float4 inPosition : POSITION ){	//Output struct	VSOUTPUT_SHADOW Out;	//Output the transformed position	Out.position = mul( mul( inPosition, World ), LightProj );	//Output the scene depth	Out.fDepth = 0.0f;	if( maxLightDistance > 0.0 )	{		Out.fDepth = length( Out.position ) / maxLightDistance;	}	else	{		Out.fDepth = Out.position.z;	}		//Out.fDepth = maxLightDistance;	return Out;}// Shadow generation fragment shaderfloat4 ShadowFs( VSOUTPUT_SHADOW In ) : COLOR0{	float depth = In.fDepth;	//Output the scene depth	return float4( depth, depth, depth, 1.0f );}technique GenerateShadowMap{	pass p0	{		CullMode = NONE;		VertexShader = compile vs_2_0 ShadowVs();		PixelShader = compile ps_2_0 ShadowFs();	}}

there is more code in the shader but those shouldn't affect this part so left that out for clarity. This is the terrain shader, models is just a direct clone without the CullMode = NONE part.

*edit: small error in the code, not using world position vertex to calculate depth, this doesn't contribute to the real problem though.

[Edited by - Murdocki on October 19, 2010 9:48:50 AM]

##### Share on other sites
If you need to use a float as a boolean value, use -1 as false and 1 as true with 0 as the border.

##### Share on other sites
Well, I see you use LightProj... Your light matrix should be a View * Projection matrix

The view matrix is a matrix that holds the position of your light, and where it is pointing (basically). The projection is like the lens of your screen.

After you make the change, take out the if statements and just pass the z component of the transformed position to the pixel shader.

##### Share on other sites
howdy,

thanks for the suggestions but please stick with my questions, they were:
-Is there a general approach to handle both ortho and perspective depth calculations in the same manner?
-How comes the first comparisation (using the <= 0.2) failes. When i'm providing a maxLightDistance of 0.0f?

also passing trough the z component of the transformed vertex doesn't work for perspective matrices for me, should it?

##### Share on other sites
Sigh... helping people can be difficult...

The reason i didnt answer your question is because your code is most likely incorrect; that is why I asked about your float4x4 LightProj variable. If it does contain a View matrix, then it should be named LightViewProj, or something similar so that others(like me, and who ever else decides to help you) can understand the code, and thus render assistance. If that variable does not contain the view matrix then your light is using the identity matrix for its view, which means your code will not work correctly.

##### Share on other sites
what i ment was that my question is not about code working incorrectly but about the wrong code being executed. I do not see how a "wrong" set up matrix can lead to the wrong codepath being followed so if you could explain that that would be great.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 40
• ### Forum Statistics

• Total Topics
634130
• Total Posts
3015705
×