5

C# 从零开始写 SharpDx 应用 从 D3DDevice 获取 DXGIDevice 方法

 3 years ago
source link: https://lindexi.gitee.io/post/C-%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%86%99-SharpDx-%E5%BA%94%E7%94%A8-%E4%BB%8E-D3DDevice-%E8%8E%B7%E5%8F%96-DXGIDevice-%E6%96%B9%E6%B3%95.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C# 从零开始写 SharpDx 应用 从 D3DDevice 获取 DXGIDevice 方法

本文将告诉大家在拿到了 D3D11 的 Device 也就是 D3DDevice 之后,如何去获取 DXGI.Device 以及 DXGI.Factory 的方法

本文是 SharpDX 系列博客,更多博客请点击SharpDX 系列

C# 从零开始写 SharpDx 应用 初始化dx修改颜色 这篇博客完成了基础的初始化

而接下来如何根据之前的代码,也就是如何根据上一篇博客创建的 D3D11.Device 来创建对应的 DXGI.Device 以及 DXGI.Factory 对象

其实方法十分简单,我记录一下,因此我刚才在写一些逗比代码的时候,又忘记是如何创建的

其实在创建 D3D11.Device 就用到了 DxGI 的 SwapChainDescription 交换链的描述对象,而调用了 D3D11.Device.CreateWithSwapChain 可以创建对应的交换链

  D3D11.Device.CreateWithSwapChain(/*忽略代码*/, out D3D11.Device _d3DDevice, out DXGI.SwapChain _swapChain);
  // 看过前面系列博客的小伙伴就知道,其实 _d3DDevice 和 _swapChain 都是我定义好的字段,可不是在这里定义的变量哦。只是为了方便,没有修改变量名而已

有两个创建的方法,一个是根据 D3D11.Device _d3DDevice 来创建,另一个就是根据 DXGI.SwapChain _swapChain 来创建

_d3DDevice 调用 QueryInterface 可以拿到 DXGI.Device 对象

  var dxgiDevice = _d3DDevice.QueryInterface<DXGI.Device>();

而通过 DXGI.Device 获取 DXGI.Factory 对象还有一点坑,需要根据 DxGI 的显卡适配器 DXGI.Adapter 通过查询它的父级找到是哪个 DXGI.Factory 枚举出来的适配器

  DXGI.Adapter dxgiDeviceAdapter = dxgiDevice.Adapter;
  var dxgiFactory = dxgiDeviceAdapter.GetParent<DXGI.Factory>();

其实这就是坑了我的地方,也是我对 DirectX 的概念理解不够熟的原因,我开始就通过 DXGI.Device 对象尝试 GetParent 获取 DXGI.Factory 但是不出意外炸了

  var dxgiDevice = _d3DDevice.QueryInterface<DXGI.Device>();
  var dxgiFactory = dxgiDevice.GetParent<DXGI.Factory>();

提示内容如下

SharpDX.SharpDXException:“HRESULT: [0x80004002], Module: [General], ApiCode: [E_NOINTERFACE/No such interface supported], Message: 不支持此接口”

   at SharpDX.Result.CheckError()
   at SharpDX.DXGI.DXGIObject.GetParent(Guid riid, IntPtr& parentOut)
   at SharpDX.DXGI.DXGIObject.GetParent[T]()

而另一个方法获取是,在拿到 DXGI.SwapChain _swapChain 对象,就可以使用下面代码获取工厂

  var dxgiFactory2 = _swapChain.GetParent<DXGI.Factory>();
  var dxgiDevice2 = _swapChain.GetDevice<DXGI.Device>();

可以看到这两个方法获取的对象是相同的,如下面代码

            if (dxgiDevice.NativePointer == dxgiDevice2.NativePointer)
            {

            }

            if (dxgiFactory.NativePointer == dxgiFactory2.NativePointer)
            {

            }

本文代码放在 github 欢迎小伙伴访问

SharpDX Beginners Tutorial Part 3: Initializing DirectX - Johan Falk

SharpDX 系列

WPF 底层渲染

Directx11入门之D3D程序初始化 - 九野的博客 - CSDN博客

Directx11入门之第五章 渲染管线 - 九野的博客 - CSDN博客

Direct3D 11入门级知识介绍

Direct3D设备

D3D11_CREATE_DEVICE_FLAG enumeration

DirectX11 With Windows SDK–01 DirectX11初始化_X_Jun的博客-CSDN博客

Direct3D feature levels - Win32 apps


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/C-%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%86%99-SharpDx-%E5%BA%94%E7%94%A8-%E4%BB%8E-D3DDevice-%E8%8E%B7%E5%8F%96-DXGIDevice-%E6%96%B9%E6%B3%95.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK