Skip to main content
GameDev.net gamedev.net
🔒 Locked

sin/cos in HLSL vs_2_0

Started by jaafit Mar 23, 2007 at 7:30 PM 6 replies 13k views
Original Post
jaafit
jaafit
How exactly are sin/cos supposed to work in HLSL? I'm getting two very different results from what looks like the same code:

	float theta = 0;
	windUV.x = cos(theta) * worldPos.x - sin(theta) * worldPos.y;
	windUV.y = sin(theta) * worldPos.x + cos(theta) * worldPos.y;



The above results in the same value for windUV for each vertex.

	windUV.x = cos(0) * worldPos.x - sin(0) * worldPos.y;
	windUV.y = sin(0) * worldPos.x + cos(0) * worldPos.y;



The above works as expected. ?
Asesh
Asesh
The argument to sin/cos is the angle in radians
Sc4Freak
Sc4Freak
Quote:
Original post by Asesh
The argument to sin/cos is the angle in radians


Shouldn't really make a difference.

Keep in mind, the shader compiler probably optimises out the entire second example and just sets the variables to 0 instead of going through the sin/cos.
jaafit
jaafit
So there's a bug in the optimizer?
ET3D
ET3D
I'd suggest looking at the resulting assembly code, and seeing what the difference is.
TheAdmiral
TheAdmiral
Indeed, the only difference between the two samples is that one will invoke the sin and cos functions each frame and the other compiles them out to static code. Neglecting floating-point error, they are equivalent. Are you sure that the error can't lie elsewhere?

If not, then perhaps your problems are floating-inaccuracy-based. I'm not sure if video hardware treats sin and cos of zero as special cases, but if it doesn't, and your camera happens to be zoomed in on a patch of land that is very far away from the origin, the minute imprecision may be scaled up arbitrarily.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
ET3D
ET3D
Another way to check this further would be to change the first example to assign cos(theta) and sin(theta) to variables and look at them.

Also, when you say that you get the same value for windUV, knowing what value might provide a clue.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.