D3D11 SwapChain 缓冲区为 NULL

D3D11 SwapChain Buffer is NULL

每当我 运行 这段代码时,_TextureData 结构中指向(成员 pData)的数据都是 0(比如 300 个字节只有 0)。它returns的HRESULT result总是S_OK,行深和列深都是准确的。我确信某些东西正在渲染到缓冲区,因为在我渲染到的 window 上显示了一些东西。我已经尝试在呈现之前和之后获取缓冲区的数据,无论哪种方式,数据仍然是空的。

D3D11_TEXTURE2D_DESC desc { };
ID3D11Texture2D * pCopy = nullptr;
ID3D11Texture2D * pBackBufferTexture = nullptr;
desc.Width = 800;
desc.Height = 800;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;

assert( SUCCEEDED( pSwapChain->Present( 0,
                                        0 ) ) );
pDevice->CreateTexture2D( &desc, nullptr, &pCopy );
pSwapChain->GetBuffer( 0,
                       __uuidof( ID3D11Texture2D ),
                       reinterpret_cast< void ** >( &pBackBufferTexture ) );
pContext->CopyResource( pCopy, pBackBufferTexture );
D3D11_MAPPED_SUBRESOURCE _TextureData { };
auto result = pContext->Map( pCopy, 0, D3D11_MAP_READ, 0, &_TextureData );
pContext->Unmap( pCopy, 0 );
pCopy->Release( );

交换链的​​代码给出了答案...交换链是使用 4x MSAA 创建的,但暂存纹理是单样本。

在这种情况下你不能CopyResource。相反,您必须解析 MSAA:

pContext->ResolveSubresource(pCopy, 0, pBackBufferTexture, 0, DXGI_FORMAT_R8G8B8A8_UNORM);

请参阅 DirectX 工具包 ScreenGrab 来源,它更普遍地处理这种情况。

The code also shows that you are not using the Debug device (D3D11_CREATE_DEVICE_DEBUG) which would have told you about this problem. See this blog post for details.