我正在尝试创建一个共享位图以与 Direct2d 渲染共享 D3D11Texture2d。创建纹理和渲染目标后,我尝试制作共享位图,但是调用因 d2d1!GetParentTexture 中的内存访问错误而崩溃。我的代码如下:
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
D3D_FEATURE_LEVEL reqFeatureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
CComPtr<ID3D11Device> device;
CComPtr<ID3D11DeviceContext> context;
HRESULT hr = D3D11CreateDevice(
NULL, // specify null to use the default adapter
D3D_DRIVER_TYPE_HARDWARE,
0,
creationFlags, // optionally set debug and Direct2D compatibility flags
reqFeatureLevels, // list of feature levels this app can support
ARRAYSIZE(reqFeatureLevels), // number of possible feature levels
D3D11_SDK_VERSION,
&device, // returns the Direct3D device created
NULL, // returns feature level of device created
&context // returns the device immediate context
);
if (FAILED(hr)) return -1;
CComPtr<ID3D11Texture2D> renderTexture;
CD3D11_TEXTURE2D_DESC textureDesc(DXGI_FORMAT_B8G8R8A8_UNORM, 100, 100, 1, 1, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
hr = device->CreateTexture2D(&textureDesc, NULL, &renderTexture);
if (FAILED(hr)) return -1;
CComPtr<IDXGISurface> dxgiSurf;
hr = renderTexture.QueryInterface<IDXGISurface>(&dxgiSurf);
OnHResult(hr, "QueryInterface<IDXGISurface>", return false);
DXGI_SURFACE_DESC desc;
dxgiSurf->GetDesc(&desc);
CComPtr<ID2D1Factory> factory;
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), NULL, (void**)&factory);
if (FAILED(hr)) return -1;
CComPtr<ID2D1RenderTarget> renderTarget;
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE));
hr = factory->CreateDxgiSurfaceRenderTarget(dxgiSurf, &props, &renderTarget);
if (FAILED(hr)) return -1;
CComPtr<ID2D1Bitmap> sharedBitmap;
D2D1_BITMAP_PROPERTIES bitmapProperties = D2D1::BitmapProperties(D2D1::PixelFormat(desc.Format, D2D1_ALPHA_MODE_IGNORE));
hr = renderTarget->CreateSharedBitmap(IID_PPV_ARGS(&dxgiSurf), &bitmapProperties, &sharedBitmap);
if (FAILED(hr)) return -1;
任何想法为什么会失败?