2016-01-07 135 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。