由于我在 Stack Overflow 上没有得到任何回应,我在 gamedev.net 上交叉发布并得到了很好的回应:http ://www.gamedev.net/topic/674529-d3d12-color-picking-questions/
对于将来发现此问题的任何人,我将在此处从 GameDev 论坛复制 red75prime 的答案。
red75prime 的回答:
问题 1 并非特定于 D3D12。使用具有多个输出的一个像素着色器。在directx 11中一次渲染到多个纹理
问题 2. 全部同意。
伪代码:
ID3D12GraphicsCommandList *gl = ...;
ID3D12CommandQueue *gqueue = ...;
ID3D12Resource *render_target, *read_back_texture;
...
// Draw scene
gl->DrawInstanced(...);
// Make ready for copy
gl->ResourceBarrier(render_target, RENDER_TARGET, COPY_SOURCE);
//gl->ResourceBarrier(read_back_texture, GENERIC_READ, COPY_DEST);
// Copy
gl->CopyTextureRegion(...);
// Make render_target ready for Present(), read_back_texture for Map()
gl->ResourceBarrier(render_target, COPY_SOURCE, PRESENT);
//gl->ResourceBarrier(read_back_texture, COPY_DEST, GENERIC_READ);
gl->Close(); // It's easy to forget
gqueue->ExecuteCommandLists(gl);
// Instruct GPU to signal when command list is done.
gqueue->Signal(fence, ...);
// Wait until GPU completes drawing
// It's inefficient. It doesn't allow GPU and CPU work in parallel.
// It's here just to make example simple.
wait_for_fence(fence, ...);
// Also, you can map texture once and store pointer to mapped data.
read_back_texture->Map(...);
// Read texture data
...
read_back_texture->Unmap();
编辑:我在代码中添加了“gl->Close()”。
EDIT2: read_back_texture 的状态转换是不必要的。回读堆中的资源必须始终具有状态 COPY_DEST。