read pixel data from render target in d3d11
Asked Answered
M

1

8

I set a texture2d as a device render target. After draw, how can i read and write pixels from render target directly , and present it then.

Monde answered 20/11, 2012 at 17:59 Comment(1)
Do you mean read the pixels back to the CPU, or as input to another shader?Inaction
M
13

If your intent is to use it as an input to another shader: - simply ensure you created the texture2D with the D3D11_BIND_RENDER_TARGET and D3D11_BIND_SHADER_RESOURCE.

  • Bind the texture as render target and render to it.
  • Unbind it as a render target and Bind it as a shader resource and use it in the next shader.
  • Just note that a texture cannot be bound as a target and a resource at the same time.

If your intent is to access the texture on the CPU using C++ as an array of pixels, then you have to do some work. Unfortunately, due to current GPU architectures, it is not possible to directly access a texture2D's pixels since the pixels actually live in GPU memory potential in a special format(swizzled format).

  • Create a texture the GPU can render into.
  • Create a staging texture(D3D11_USAGE_STAGING) that will be used to receive the output of the GPU.
  • Render to the GPU texture.
  • Issue a ID3D11DeviceContext::CopyResource() or ID3D11DeviceContext::CopySubresource()
  • Call ID3D11DeviceContext::Map() on the staging resource to get access to the pixels.
  • Call ID3D11DeviceContext::Unmap() on the staging resource.
  • Call ID3D11DeviceContext::UpdateSubresource() to update the version of the resource the GPU has.

As you can see this is certainly not a trivial set of operations and goes against what the GPU architecture of today is optimized to do. I certainly would not recommend it.

If you do end up going down this path, be sure to also read about all the perf concerns GPU memory read back comes with: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205132(v=vs.85).aspx#Performance_Considerations

Modigliani answered 20/2, 2013 at 13:27 Comment(1)
Tip: The rows in the texture are not guaranteed to be contiguous. When copying data from a D3D11Texture2D to RAM, you need to increment the data pointer of the mapped texture (D3D11_MAPPED_SUBRESOURCE) according to its RowPitch member, and not according to the actual byte width of your row.Ahoufe

© 2022 - 2024 — McMap. All rights reserved.