sin/cos in HLSL vs_2_0

Started by
6 comments, last by ET3D 17 years, 1 month ago
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. ?
Advertisement
The argument to sin/cos is the angle in radians
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
So there's a bug in the optimizer?
Whoops, sorry, misread the example. Didn't see the cosines in there.
NextWar: The Quest for Earth available now for Windows Phone 7.
I'd suggest looking at the resulting assembly code, and seeing what the difference is.
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.
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.

This topic is closed to new replies.

Advertisement