从 Direct2D 渲染目标获取 Direct3D 设备

Get Direct3D device from Direct2D render target

我正在使用 Direct2D 呈现我的用户界面。

我想要的是能够更轻松地分析我的 ui 渲染(因为我使用多个面板,使用图形调试器有点麻烦)。

因为我知道 Direct2D 在后台使用 Direct3D 设备(确切地说是使用 10_0 功能级别的 d3d11 设备),所以我想知道是否可以从中检索 ID310Device 或 ID3D11Device 实例ID2D1RenderTarget 或 ID2D1Factory 对象。

在那种情况下,我可以轻松地在 BeginDraw/EndDraw 调用上附加时间戳查询。

我尝试了几次 QueryInterface 调用,但 none 到目前为止已经成功。

一个有趣的未记录的秘密是,你从 ID2D1Factory 得到的任何 ID2D1RenderTarget 也将是一个 ID2D1DeviceContext(从我收集到的信息来看,这似乎是故意的,只是 不小心 没有记录?)。只需调用 IUnknown::QueryInterface() 即可获取。从那里你可以尝试使用 GetDevice()GetTarget() 等方法。如果你能得到目标,那么你也许可以狡猾地获得支持IDXGIDeviceSubObject::GetDevice() https://msdn.microsoft.com/en-us/library/windows/desktop/bb174529(v=vs.85).aspxIDXGISurface(我没有验证这部分)

而在 Win10 中,ID2D1Device2 似乎可以准确地满足您的需求:GetDxgiDevice() https://msdn.microsoft.com/en-us/library/windows/desktop/dn917489(v=vs.85).aspx。因此,在这种情况下,您的 ID2D1RenderTarget 通过 IUnknown::QueryInterface() 转换为 ID2D1DeviceContext,然后通过 ID2D1DeviceContext::GetDevice() 转换为 ID2D1Device,然后将其转换为 ID2D1Device2 通过另一个调用 IUnknown::QueryInterface().