在C#WPF或Windows Forms应用程序中使用DirectX c ++ DLL [英] Using a DirectX c++ DLL in a C# WPF or Windows Forms Application

查看:201
本文介绍了在C#WPF或Windows Forms应用程序中使用DirectX c ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用c ++编写了DX11渲染器。我现在正在寻找一种实现其编辑器/ GUI的方法。

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

由于我非常习惯Windows窗体和WPF C#应用程序,因此我正在考虑将渲染器放入dll中,然后从ac#应用程序中加载该dll并使用它绘制成一个表格的定义部分。这甚至可以与托管c#应用程序传递的窗口句柄一起使用,如果可以的话,性能如何?

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?

据我了解,由于与DX11 API的实际通信仍将直接在c ++中进行,而c#部分只会告诉dll该怎么做,性能损失应该是几乎没有。还是这通常不是一个好主意,而使用库或D2D在c ++中直接实现GUI是更好的方法。

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.

推荐答案

在使用WPF,您可以使用 D3DImage ,就像您的xaml中的简单图片一样。一般原则如下:

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


  • 分配Direct3D 9纹理,这将是您的渲染目标。

  • 将其移交给D3DImage.SetBackBuffer。

  • 将渲染代码放入CompositionTarget.Rendering事件处理程序中。

  • 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();


由于使用的是Direct3D 11,使用 DXGI表面共享与D3D11共享D3D9文本。我知道有两个例子可以说明如何做到这一点。 Microsoft D3D11Image 和D3DImageEx(我个人的喜好,可以在网上的多个地方找到)。

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).

关于性能,一旦使用WPF,您就不再需要自己做演示了。您实际上是在基于WPF D3D9的渲染器中编写了一个小的纹理渲染逻辑。因此,包括演讲时间在内的所有内容都无法控制。根据我的个人经验,您可以以不错的帧率清晰地渲染简单的场景。对于真正的图形密集型应用程序和严格的FPS要求,我将使用更轻便(且侵入性较小)的UI解决方案。

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.

请注意,您还可以使用WinformHost控件来获取HWND并将其渲染。如果您想将UI控件叠加在渲染的框架上-AFAIK则根本无法做到这一点。

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.

这篇关于在C#WPF或Windows Forms应用程序中使用DirectX c ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆