WPF and DirectX 11 via D3DImage
Asked Answered
D

3

5

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/

Dipole answered 1/2, 2012 at 11:34 Comment(2)
Are you getting any error messages? Can you post your setup and render code?Rendering
You can find my code here: gamedev.net/topic/619534-wpf-directx-11-via-d3dimageDipole
D
3

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.

Dipole answered 2/2, 2012 at 17:9 Comment(0)
R
2

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

Rendering answered 2/2, 2012 at 16:41 Comment(1)
No, I do it in Window1.xaml.cs file. I have found my mistake. I used wrong format for layout { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }. I used DXGI_FORMAT_B8G8R8A8_UNORM, but have to: DXGI_FORMAT_R32G32B32_FLOATDipole
D
1

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.

Danyelledanyette answered 1/2, 2012 at 12:14 Comment(1)
Yes, you are right. Now WPF redraws ~60 fps. I can redraw only by notification. But if GUI is more complex than two buttons, then CPU usage increase up to 10-15 %, even if to redraw only when I need it.Dipole

© 2022 - 2024 — McMap. All rights reserved.