Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF and DirectX 11 via D3DImage

I want to use DirectX 11 from unmanaged C++ code and use WFP for the GUI. SlimDX is not suitable for me. I have found the solution to make working WPF with DirectX 10:

WPF & DirectX 10 via D3DImage

But I couldn't manage to work it with DirectX 11. Just blank screen with two buttons. Does anybody know how to make WPF working with DirectX 11.

Also I had seen that when I'm just running this example the CPU usage is about 4-5% for Intel i5 750 (Windows 7 64 bit, NVidia Geforce 430). I think it's too much.. Is it possible to decrease CPU usage ?

You can find my code here: http://www.gamedev.net/topic/619534-wpf-directx-11-via-d3dimage/

like image 688
Alex Avatar asked Feb 01 '12 11:02

Alex


3 Answers

I used wrong format for the vertex buffer layout. I used DXGI_FORMAT_B8G8R8A8_UNORM, but have to use DXGI_FORMAT_R32G32B32_FLOAT in my case.

like image 65
Alex Avatar answered Nov 12 '22 19:11

Alex


You're missing a call to D3DImage.AddDirtyRect. After you render your scene with Direct3D11, you need to tell WPF that there is a new image ready to be shown. To do that, you perform the following, where d3dimage is your D3DImage instance:

d3dimage.Lock();

//...
//your render code
///...

var surface = renderTexture.GetSurfaceLevel(0);

d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.ComPointer);

var rect = System.Windows.Int32Rect(0, 0, width, height);

d3dimage.AddDirtyRect(rect);

d3dimage.Unlock();

This needs to be done each frame.

Additional info about AddDirtyRect

like image 2
Patrick Lafferty Avatar answered Nov 12 '22 17:11

Patrick Lafferty


I can't comment on how to make DirectX11 work with that example, but the 5% CPU you are witnessing is probably the render loop in DirectX spinning but with no work to do.

A render loop is commonly used in games to render "as fast as you can" while updating the scene/physics/AI on a different thread. In desktop visualization applications this is not usually necessary.

For scientific visualization applications where updates to the GUI only occur when the user interacts (e.g. drags an item, pans the viewport) I've redesigned the render loop to only render when there is an update. ie: after your work to pan the viewport within mousemove, you force DirectX to re-draw by setting a flag or similar. This way "drawing when you need it" will reduce your CPU to near zero while the application is idling and is more suitable for desktop applications.

like image 1
Dr. Andrew Burnett-Thompson Avatar answered Nov 12 '22 18:11

Dr. Andrew Burnett-Thompson