directxdirect3d

DrawIndexed and multiple render targets


OMSetRenderTargets allows you to set multiple render targets. I want to draw the whole vertex buffer on my first render target, but only half the vertex buffer on the second.

i.e. I want to differ the VertexCountPerInstance/StartIndexLocation parameters of DrawInstanced/DrawIndexedInstanced depending on the render target.

Is this possible?


Solution

  • Strictly speaking, that’s impossible. With multiple RTs, the pixel shader is called once per pixel and produces values for all render targets.

    However, depending on your use case you might be able to workaround with blend states. Note the D3D11_BLEND_DESC structure has independent D3D11_RENDER_TARGET_BLEND_DESC for each RT.

    Therefore, you can set IndependentBlendEnable = TRUE, enable alpha-blending on the second RT, in the pixel shader compare SV_PrimitiveID with a constant, and if the primitive ID exceeds the threshold, output zero vector for the second RT. Alpha blending will then ignore output values for the second portion of the mesh. You gonna need to update a dynamic constant buffer for the pixel shader with that uint value, before calling DrawInstanced or DrawIndexedInstanced methods.

    Note however that all RTs share the same depth/stencil view. If you write depth on that pass, the pixels discarded with alpha blending will still affect the depth buffer. This may cause weird artifacts on your second RT which depend on the transformation matrices, and the order of the triangles in the mesh you’re rendering.