Search code examples
c#.netdirectxfloating-pointslimdx

StopWatch returns Integer after DirectX Device creation


The question is pretty "simple". I have an engine that can work with both DirectX9 and DirectX10 with SlimDX in C#. I use the Stopwatch class to time the game and test speed of some routine. Strangely exactly after the creation of the DirectX9 the floating point of the StopWatch go crazy and the GetTimeStamp()/Frequency division returns only integer values (single with .000000). Why this? Please note that with DirectX10, this problem isn't present. (if I do the check just before opening the device in DirectX9 it works, just after don't.)

This is the code i use for both version (DirectX10 and DirectX9)

(Double)Stopwatch.GetTimestamp() * 1000d / (Double)Stopwatch.Frequency

Any clues?

EDIT : I tested using QueryPerformanceCounter, but the result is the same. Perfect calculation before creating the DirectX9 device, only integer after.

EDIT2: After some testing i managed to make it show some floating point values but they are pretty different from the DirectX10 one in precision.

Here's some example :

DirectX10

Form creation : 110.09241
LoadContent Full : 738.64486
LoadContent ContentManager : 593.57572

DirectX9

Form creation : 112.45000
LoadContent Full : 489.50000
LoadContent ContentManager : 355.50000

EDIT3: Tested on another machine and the result is the same...before the creating of the device all ok, after only integer...


Solution

  • By default, Direct3D 9 reduces the floating point precision to 32-bit. You can prevent this using the D3DCREATE_FPU_PRESERVE flag. In SlimDX, You would use the SlimDX.Direct3D9.CreateFlags.FpuPreserve flag when creating the device.