I was trying to find something similiar to glReadPixel() and stumbled upon this post: https://www.gamedev.net/topic/594722-reading-one-pixel-from-texture-to-cpu-in-dx11/
Seemed reasonable, tried it but lo and behold didnt work.
Sample code;
{
DX11DepthBufferReadback::DX11DepthBufferReadback(ID3D11DevicePtr device, ID3D11DeviceContextPtr context, ID3D11Texture2DPtr depthBuffer) :
mContext(context),
mDepthbuffer(depthBuffer),
mStaging1x1(nullptr)
{
D3D11_TEXTURE2D_DESC depthTextureDesc;
ZeroMemory(&depthTextureDesc, sizeof(depthTextureDesc));
depthBuffer->GetDesc(&depthTextureDesc);
depthTextureDesc.Usage = D3D11_USAGE_STAGING;
depthTextureDesc.BindFlags = 0;
depthTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
device->CreateTexture2D(&depthTextureDesc, NULL, &mStaging1x1);
}
float DX11DepthBufferReadback::GetDepthValue(const WindowPosition& position) const
{
D3D11_BOX box;
ZeroMemory(&box, sizeof(box));
box.left = position.x;
box.right = box.left + 1;
box.top = position.y;
box.bottom = box.top + 1;
box.front = 0;
box.back = 1;
mContext->CopySubresourceRegion(mStaging1x1, 0, 0, 0, 0, mDepthbuffer, 0, &box);
D3D11_MAPPED_SUBRESOURCE mappedRes;
mContext->Map(mStaging1x1, 0, D3D11_MAP_READ, 0, &mappedRes);
float depth = *static_cast<float*>(mappedRes.pData);
mContext->Unmap(mStaging1x1, 0);
return depth;
}
}
This is the debug ouput I get:
D3D11 ERROR: ID3D11DeviceContext::CopySubresourceRegion: If CopySubresourceRegion is used with Multisampled or D3D11_BIND_DEPTH_STENCIL Resources, then the whole Subresource must be copied. DstX, DstY, and DstZ must all be 0, while pSrcBox must be NULL. [ RESOURCE_MANIPULATION ERROR #280: COPYSUBRESOURCEREGION_INVALIDSOURCEBOX]
So this dosn't seem to work. Is there any similiarly easy/straight forward solution that works?