我在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
答案 0 :(得分:1)
您选择的格式,用法和绑定标志的组合与D3D11_RESOURCE_MISC_GDI_COMPATIBLE
不兼容。
如果启用Direct3D Debug Device,则将获得调试输出以通知您此限制。 Direct3D调试设备是弄清楚为什么获得E_INVALIDARG
的理想方法。
D3D11错误:ID3D11Device :: CreateTexture2D:D3D11_RESOURCE_MISC_GDI_COMPATIBLE要求设置D3D11_BIND_RENDER_TARGET标志。 [STATE_CREATION错误#103:CREATETEXTURE2D_INVALIDMISCFLAGS]
然后修复该问题,您将得到:
D3D11错误:ID3D11Device :: CreateTexture2D:D3D11_RESOURCE_MISC_GDI_COMPATIBLE需要D3D11_USAGE_DEFAULT。 [STATE_CREATION错误#103:CREATETEXTURE2D_INVALIDMISCFLAGS]“。
最后:
D3D11错误:ID3D11Device :: CreateTexture2D:D3D11_RESOURCE_MISC_GDI_COMPATIBLE需要B8G8R8A8格式。 [STATE_CREATION错误#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上阐明