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

[SlimDX] - D3D9 - double/float issues

Started by nietakt Feb 8, 2011 at 11:54 AM 3 replies 1.4k views
Original Post
nietakt
nietakt
Hello!

Just a short preamble before I actually begin - precision and repeatability of computations is *very* important to me, that is why I even noticed the issue to be described in a sec.

Well, calling the Device constructor does *something* to the calling thread. I cannot say what it is (hence the question), but it changes the way this thread multiplies doubles. Resultantly, the thread seems to cast them to floats and back to doubles. Here comes the example:


//init.
//...

//double multiplication test
double a = 0.86602539649920685;
double b = 1.0000000000000000;
double c = a * b; // c = 0.86602539649920685

//device creation
_device = new Device(d3d, iAdapter, devType, this.Handle, flags, _presentParams);

//double multiplication test
double d = 0.86602539649920685;
double e = 1.0000000000000000;
double f = d * e; // f = 0.86602538824081421 [!!!]
double g = (double)((float)d * (float)e); // g = 0.86602538824081421 [!!!]


And as of now, the thread will do the same lousy thing whenever it stumbles upon double multiplication.
However
  • calling the very same multiplication in a separate thread (once the main thread has gone through device creation) -> okay
  • forcing the device creation to a separate thread (just for testing) -> the main thread remains okayand that is why I guess it must be a certain thread 'setting' or whatever.

    The question: what is the reason for this and can I sanely undo what device creation seems to be doing?

    Thanks
unbird
unbird
Use the CreateFlags.FpuPreserve with the device creation. Otherwise the FPU control flags "turn" your doubles into floats.

The DX docs state this is done for performance reasons, though I really haven't seen discussions in this regard or even experienced a bottle-neck myself.

It's really a pain in the ass trying not to use this flag: It can corrupt anything using doubles. E.g. I had a bad behavior from the .NET Random class once.

By the way: Congrats on your effort for nailing this down so exactly. I did not know it affects the calling thread only.
nietakt
nietakt
Yep, that's it!

unbird - thanks a lot^2 . (I tried to rate you, but couldn't find the proper switch).
unbird
unbird
With the new site you don't rate members, but posts. I thought you already did, actually: See that green (+) thingy bottom [strike]left[/strike] right of every post ? Be careful, you can't take them back (yet?).
nietakt
nietakt
Okay, did it :].
The first rating was not from me, seems somebody else has found your reply helpful as well.

See you some other time,
nietakt

Topic Locked

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

Sign in to reply to this topic.