c ++,directx 12:颜色选择问题

时间:2016-01-07 20:02:31

标签: c++ directx-12 direct3d12

我想在DirectX 12中实现颜色选择。所以基本上我要做的是同时渲染到两个渲染目标。第一个渲染目标应包含正常渲染,而第二个渲染目标应包含objectID。

要渲染到两个渲染目标,我认为您需要做的就是使用OMSetRenderTargets设置它们。

问题1 :如何指定哪个着色器或管道状态对象应该用于特定的渲染目标?就像你怎么说应该用shader_0渲染render_target_0一样,render_target_1应该用shader_1渲染?

问题2 :如何在帧缓冲区渲染后读取像素?是否在DirectX 11中使用CopySubresourceRegion然后Map?你需要使用回读堆吗?您是否需要使用资源屏障或围栏或某种排序同步原语来避免CPU和GPU同时使用帧缓冲资源?

我尝试使用谷歌搜索答案,但没有走得太远,因为DirectX 12很新,而且还没有很多针对DirectX 12的示例,教程或开源项目。

提前感谢您的帮助。

代码示例的额外特殊奖励点。

1 个答案:

答案 0 :(得分:3)

由于我没有在Stack Overflow上得到任何回复,我在gamedev.net上交叉发布并在那里得到了很好的回复: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->关闭()"代码。

EDIT2:read_back_texture的状态转换是不必要的。回读堆中的资源必须始终具有状态COPY_DEST。