7 hours ago, VietNN said:
- In Dx11,we can set the constant buffer for each shader by using : VSSetConstantBuffer, PSSetConstantBuffer. But in Dx12 I am using SetGraphicsRootConstantBufferView, as I know it will set constant buffer for both VS and PS, am I right?
- For example, my project is using 5 constant buffers: A,B,C,D,E. The VS use 3 const buffer A,B,C and PS use A, D,E
- So how to port it to DX12 correctly ? SetGraphicsRootConstantBufferView(0 -> 4, A -> E) seems ok, but would PS know how to match D E in right position?
The root signature is what controls visibility and register assignment in D3D12. In your specific case for constant buffers, the D3D12_ROOT_DESCRIPTOR contains a ShaderRegister variable for you to fill out that will match what you type in your shader code as cbuffer MyCB: register(b#). For example, if you set ShaderRegister to 5, then you would declare cbuffer MyCB: register(b5).
The D3D12_ROOT_PARAMETER (of which the root descriptor is a unioned member of) has a ShaderVisibility member that controls which stages of the pipeline the parameter is visible to. You can either set the visibility to ALL, or to individual stages and use duplicate (with different stage) entries in the root signature. See here: https://msdn.microsoft.com/en-us/library/windows/desktop/dn913202(v=vs.85).aspx#visibility
In your case, you can give your shared constant buffers ALL visibility and set them once using SetGraphicsRootConstantBufferView, or you can give your shared constant buffers duplicate entries with specific visibilities (VERTEX and PIXEL) and set them each with separate calls to SetGraphicsRootConstantBufferView. The link above mentions this, but some hardware handles ALL differently and is less efficient than setting individual stages, while other hardware will have no extra cost for setting ALL.
Keep in mind general best practices for root signatures when deciding on a route - keep the root signature as small as possible, with items that change more frequently towards the beginning. Root constant descriptors take 2 DWORDS each, so can start to grow your root signature quickly if you're not mindful.
More documentation that you might find helpful:
https://msdn.microsoft.com/en-us/library/windows/desktop/dn859357(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dn986747(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dn879476(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dn879482(v=vs.85).aspx