gpt4 book ai didi

c++11 - 从 ID3D11Texture2D 获取 GDI DC 进行绘图

转载 作者:行者123 更新时间:2023-12-05 06:28:21 25 4
gpt4 key购买 nike

我在 directx9 中有一个实现,其中我使用 GDI DC 来渲染绘图。但是directx11中类似的代码并没有获取到GDI DC而是抛出无效调用异常。

在 directx9 中的实现:

IF_DX9ERR_THROW_HR(m_spIDevice->CreateTexture(UINT(cSizeOverlay.cx), UINT(cSizeOverlay.cy), 1, D3DUSAGE_DYNAMIC,  D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_spIOverlay, nullptr));
m_spIOverlaySurface = nullptr;
IF_DX9ERR_THROW_HR(m_spIOverlay->GetSurfaceLevel(0, &m_spIOverlaySurface));
D3DSURFACE_DESC descOverlay;
::ZeroMemory(&descOverlay, sizeof(descOverlay));
IF_DX9ERR_THROW_HR(m_spIOverlaySurface->GetDesc(&descOverlay));
// fill the texture with the color key
CRect cRect(0, 0, descOverlay.Width, descOverlay.Height);
HDC hDC = nullptr;
IF_DX9ERR_THROW_HR(m_spIOverlaySurface->GetDC(&hDC));
::SetBkColor(hDC, colKey);
::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, cRect, nullptr, 0, nullptr);
IF_DX9ERR_THROW_HR(m_spIOverlaySurface->ReleaseDC(hDC));

在 directx11 中的实现:

D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = gsl::narrow_cast<UINT>(width);
desc.Height = gsl::narrow_cast<UINT>(height);
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE;

ID3D11DevicePtr device = renderer->Device();
ID3D11Texture2DPtr texture2D;
IF_FAILED_THROW_HR(device->CreateTexture2D(&desc, nullptr, &texture2D));
// get texture surface
IDXGISurface1Ptr dxgiSurface1 = tex2D;

IF_FAILED_THROW_HR(dxgiSurface1->GetDC(FALSE, &m_overlayDC));
//Draw on the DC using GDI
if (!m_overlayDC) // we have lost the device
THROW_PE(IDS_ERR_NO_VIDEO_HARDWARE);
::SetBkColor(m_overlayDC, m_effectConstants.m_keyColor);
::ExtTextOut(m_overlayDC, 0, 0, ETO_OPAQUE, overlayRect, nullptr, 0, nullptr);
//When finish drawing release the DC
dxgiSurface1->ReleaseDC(nullptr);

m_overlayDC = nullptr;

编辑:我更改了 D3D11_TEXTURE2D_DESC,如下所示:

    CD3D11_TEXTURE2D_DESC texDesc(DXGI_FORMAT_B8G8R8X8_UNORM
, gsl::narrow_cast<UINT>(targetSize.width), gsl::narrow_cast<UINT>(targetSize.height), 1U, 1, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);

ID3D11DevicePtr device = renderer->Device();
ID3D11Texture2DPtr texture2D;
IF_FAILED_THROW_HR(device->CreateTexture2D(&texDesc, nullptr, &texture2D));

// get texture surface
IDXGISurface1Ptr dxgiSurface1 = texture2D;

IF_FAILED_THROW_HR(dxgiSurface1->GetDC(FALSE, &m_overlayDC));
//Draw on the DC using GDI
if (!m_overlayDC) // we have lost the device
THROW_PE(IDS_ERR_NO_VIDEO_HARDWARE);
::SetBkColor(m_overlayDC, m_effectConstants.m_keyColor);
::ExtTextOut(m_overlayDC, 0, 0, ETO_OPAQUE, overlayRect, nullptr, 0, nullptr);
//When finish drawing release the DC
dxgiSurface1->ReleaseDC(nullptr);

m_overlayDC = nullptr;

现在从 GetDC() 抛出异常:应用程序进行了无效调用。调用的参数或某些对象的状态不正确。启用 D3D 调试层以便通过调试消息查看详细信息。HResult:0x887A0001,设备:2170,代码:1

最佳答案

您选择的格式、用法和绑定(bind)标志的组合与 D3D11_RESOURCE_MISC_GDI_COMPATIBLE 不兼容。

如果启用 Direct3D Debug Device ,你会得到调试输出来通知你这个限制。 Direct3D Debug Device 是找出为什么您得到 E_INVALIDARG 的理想方法。

D3D11 ERROR: ID3D11Device::CreateTexture2D: D3D11_RESOURCE_MISC_GDI_COMPATIBLE requires that the D3D11_BIND_RENDER_TARGET flag be set. [ STATE_CREATION ERROR #103: CREATETEXTURE2D_INVALIDMISCFLAGS]

然后在修复之后,你会得到:

D3D11 ERROR: ID3D11Device::CreateTexture2D: D3D11_RESOURCE_MISC_GDI_COMPATIBLE requires D3D11_USAGE_DEFAULT. [ STATE_CREATION ERROR #103: CREATETEXTURE2D_INVALIDMISCFLAGS]``.

最后:

D3D11 ERROR: ID3D11Device::CreateTexture2D: D3D11_RESOURCE_MISC_GDI_COMPATIBLE requires a B8G8R8A8 format. [ STATE_CREATION ERROR #103: CREATETEXTURE2D_INVALIDMISCFLAGS]

所以把这些放在一起,这是可行的:

D3D11_TEXTURE2D_DESC desc = {};
desc.Width = ...
desc.Height = ...
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE;

Microsoft::WRL::ComPtr<ID3D11Texture2D> texture2D;
DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D(&desc, nullptr, &texture2D));

这些限制都在 Microsoft Docs 上详细说明了

关于c++11 - 从 ID3D11Texture2D 获取 GDI DC 进行绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54400679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com