c++、directx 12:颜色选择问题
c++, directx 12: Color Picking Questions
我想在 DirectX 12 中实现颜色选择。所以基本上我想做的是同时渲染到两个渲染目标。第一个渲染目标应该包含正常渲染,而第二个应该包含 objectID。
要渲染到两个渲染目标,我认为您需要做的就是使用 OMSetRenderTargets 设置它们。
问题 1:如何指定应将哪个着色器或管线状态对象用于特定渲染目标?就像你说的 render_target_0 应该用 shader_0 渲染,render_target_1 应该用 shader_1 渲染?
问题 2:渲染后如何从帧缓冲区中读取像素?是否像在 DirectX 11 中使用 CopySubresourceRegion 然后使用 Map?您需要使用回读堆吗?您是否需要使用资源屏障或栅栏或某种同步原语来避免 CPU 和 GPU 同时使用帧缓冲区资源?
我尝试在谷歌上搜索答案,但没有找到很深的答案,因为 DirectX 12 非常新,而且还没有很多针对 DirectX 12 的示例、教程或开源项目。
提前感谢您的帮助。
代码示例额外加分。
由于我在 Stack Overflow 上没有得到任何回复,我在 gamedev.net 上 cross-posted 并在那里得到了很好的回复:http://www.gamedev.net/topic/674529-d3d12-color-picking-questions/
对于以后发现此问题的任何人,我将从此处的 GameDev 论坛复制 red75prime 的回答。
red75prime 的回答:
问题 1 并非特定于 D3D12。使用一个具有多个输出的像素着色器。 Rendering to multiple textures with one pass in 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 的状态转换是不必要的。 read-back 堆中的资源必须始终具有状态 COPY_DEST。
我想在 DirectX 12 中实现颜色选择。所以基本上我想做的是同时渲染到两个渲染目标。第一个渲染目标应该包含正常渲染,而第二个应该包含 objectID。
要渲染到两个渲染目标,我认为您需要做的就是使用 OMSetRenderTargets 设置它们。
问题 1:如何指定应将哪个着色器或管线状态对象用于特定渲染目标?就像你说的 render_target_0 应该用 shader_0 渲染,render_target_1 应该用 shader_1 渲染?
问题 2:渲染后如何从帧缓冲区中读取像素?是否像在 DirectX 11 中使用 CopySubresourceRegion 然后使用 Map?您需要使用回读堆吗?您是否需要使用资源屏障或栅栏或某种同步原语来避免 CPU 和 GPU 同时使用帧缓冲区资源?
我尝试在谷歌上搜索答案,但没有找到很深的答案,因为 DirectX 12 非常新,而且还没有很多针对 DirectX 12 的示例、教程或开源项目。
提前感谢您的帮助。
代码示例额外加分。
由于我在 Stack Overflow 上没有得到任何回复,我在 gamedev.net 上 cross-posted 并在那里得到了很好的回复:http://www.gamedev.net/topic/674529-d3d12-color-picking-questions/
对于以后发现此问题的任何人,我将从此处的 GameDev 论坛复制 red75prime 的回答。
red75prime 的回答:
问题 1 并非特定于 D3D12。使用一个具有多个输出的像素着色器。 Rendering to multiple textures with one pass in 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 的状态转换是不必要的。 read-back 堆中的资源必须始终具有状态 COPY_DEST。