[DX11] Map for staging texture with CPU read access flags returns zero / black.

Started by
6 comments, last by kavaari 2 years, 10 months ago
int resolution = 4096;
//Staging texture:
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = resolution;
desc.Height = resolution;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;

// Render target view texture
D3D11_TEXTURE2D_DESC descBack = {};
descBack.Width = resolution;
descBack.Height = resolution;
descBack.MipLevels = 1;
descBack.ArraySize = 1;
descBack.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
descBack.SampleDesc.Count = 1;
descBack.SampleDesc.Quality = 0;
descBack.Usage = D3D11_USAGE_DEFAULT;
descBack.BindFlags = D3D11_BIND_RENDER_TARGET;
descBack.CPUAccessFlags = 0;
descBack.MiscFlags = 0;

// Create textures and render target view
// Set render target view active and view rectangles
// Draw stuff 

deviceCtx->CopyResource(stagingTexture, rtvTexture);
// Graphics debugger shows that render target texture contents has been copied to staging texture.
// So staging texture is not null (at least according to debugger)

// Map command 
deviceCtx->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mappedResource);

// deviceCtx->Map returns S_OK.

u32* textureData = (u32*)mappedResource.pData;


for (int y = 0; y < resolution; ++y)
{
    for (int x = 0; x < resolution; ++x)
    {
    	// rowPitch bytes is (y * resolution * 4), so y * resolution should be correct?
    	u32 diffuse = textureData[y * resolution + x];
    	// diffuse is always 0, even though gpu debugger (renderdoc) show that the
    	// staging texture has non-zero data
    }
}

I cannot understand why it is not working? Maybe I'm missing something simple.. No D3D11 debug errors or warnings at any stage.

Advertisement

Did you check the return value of the Map call?

Is there any debug message from D3D11?

Yeah. Forgot to mention that no D3D11 debug errors or warnings at any stage. deviceCtx->Map return S_OK.

Managed to close the issue bit further. The render target view clear color gets copied to staging texture and then shows up in the mapped memory area, so cpu read from rtv should be ok. The problem is that the rendered stuff won't end up in the mapped content. Renderdoc shows that the drawn stuff is in both render target view and staging texture.

So either my for loop that tries to find content other than the clear color is wrong. Or the geometry / fragments has not been yet drawn to RTV when the CopyResource is done at GPU side.

So is deviceCtx->CopyResource() right after submitting draw calls wrong though? I though that Ctx→Map will cause pipeline stall and wait for the Draws and CopyResource to finish and then I will get to read the contents at CPU.

Managed to solve the issue. Sadly it seems that I cannot trust RenderDoc texture view, it was my source of truth..

So the active depth stencil buffer was not cleared with correct colors, so there actually was not anything drawn on the texture.

For some reason RenderDoc showed otherwise..

Thanks for everyone who gave this a look and thought.

That's too bad, but good that you tracked it down. Mismatches between RenderDoc and your actual program can occur when you accidentally wander into undefined behavior, since RenderDoc replays do not exactly match what normally happens when your app render. In this case, was it that the depth buffer was never cleared after creating it?

MJP said:

That's too bad, but good that you tracked it down. Mismatches between RenderDoc and your actual program can occur when you accidentally wander into undefined behavior, since RenderDoc replays do not exactly match what normally happens when your app render. In this case, was it that the depth buffer was never cleared after creating it?

Yes it was that depth buffer was never cleared after creating it. Not blaming renderdoc. Btw, thank you MJP for your articles, those got me started with graphics back in the XNA days!

This topic is closed to new replies.

Advertisement