2

I have written a DX11 renderer using c++. I am now looking for a way to implement an Editor/GUI for it.

Since im pretty used to Windows Forms and WPF C# Applications im thinking about putting my renderer inside a dll, load the dll from a c# application and use it to draw into a defined section of the form. Does this even work with a window handle passed by a managed c# appication and how is the performance if it does?

From my understanding, since the actual communication with the DX11 API would still be directly in c++ and the c# part would only tell the dll what to do the performance loss should be almost nothing. Or is this generally a bad idea and implementing the GUI directly in c++ using a library or D2D is the better approach.

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
Eric
  • 1,183
  • 6
  • 17
  • You might want to look into the code of SharpDX to get some inspiration. Or just simply use SharpDX, if suitable for your needs... ;-) –  Feb 19 '19 at 13:54
  • This is exactly what i not want to do, SharpDX is a wrapper for the whole API which means every single call to the API has an extra overhead. There is a reason why no one uses c# to write games. Im just looking for a GUI without abandoning the benefits of using c++ directly. Here is a pretty interesting benchmark comparison for that: http://code4k.blogspot.com/2011/03/benchmarking-cnet-direct3d-11-apis-vs.html – Eric Feb 19 '19 at 14:03
  • Ah, okay, i misunderstood you. If your question is merely about whether you should render the UI as part of the DX surface through your renderer, or whether the UI should be built in .NET with the renderer just rendering the scene/model in a panel/surface within that UI, then this is purely a design and implementation choice depending on what functionality and what interactions between UI elements and rendered scene/model you want/need. There is no simple, general answer that one is good and the other is bad. It is a decision you have to make based on desired features and involved devl. costs. –  Feb 19 '19 at 14:23

1 Answers1

1

In WPF you can use D3DImage which is just like a simple Image in your xaml. The general principle is as follows:

  • Allocate a Direct3D 9 texture, this will be your rendering target.
  • Hand it over to the D3DImage.SetBackBuffer.
  • Put your rendering code inside CompositionTarget.Rendering event handler.

        RenderStuffToTexture();
        d3dImage.Lock();
        d3dImage.AddDirtyRect(new Int32Rect()
        {
            X = 0,
            Y = 0,
            Height = d3dImage.PixelHeight,
            Width = d3dImage.PixelWidth
        });
        d3dImage.Unlock();
    

Since you are using Direct3D 11, you have to use DXGI surface sharing to share the D3D9 textute with D3D11. There are 2 examples I know of that illustrate how to do just that. Microsoft D3D11Image and D3DImageEx (my personal preference, can be found in multiple places online).

Regarding performance, once you use WPF you no longer doing the presentation yourself. You literally write a small render-to-texture logic inside a WPF D3D9-based renderer. So not everything can be controlled including presentation time. From my personal experience, you can definitly render simple scenes at a nice framerate. For a really graphics intensive app and strict FPS requirements I would use a more lightweight (and less intrusive) UI solution.

Note that you can also use WinformHost control to get a HWND and render to it. This has a serious disadvantage if you want to overlay UI controls over the rendered frame - AFAIK you simply can't.

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37