2016-01-07 67 views
1

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

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

问题1:你如何指定哪个着色器或流水线状态对象应该用于特定的渲染目标?像你如何说render_target_0应该与shader_0渲染,render_target_1应该渲染shader_1?

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

我试着用Google搜索的答案,但没有得到很远,因为DirectX 12是相当新的,并没有很多的例子,教程或DirectX 12的开源项目。

感谢您的帮助提前。

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

回答

3

因为我没有得到堆栈溢出任何回应,I交叉贴在gamedev.net并获得有良好的反应:http://www.gamedev.net/topic/674529-d3d12-color-picking-questions/

对于任何人在将来找到这个,我只是复制red75prime的来自GameDev论坛的答案在这里。

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。