Tuesday, 22 July 2014

Dual-source blending in DirectX 11

It's not described in the documentation, but dual-source blending in DirectX 11 D3D11_BLEND_SRC1_COLOR is achieved using SV_TARGET1. You would have a BlendState like this (all in fx format):
BlendState CompositeBlend
    BlendEnable[0]  =TRUE;
    SrcBlend        =ONE;
    DestBlend       =SRC1_COLOR;
    BlendOp         =ADD;
And an output structure that looks like it's writing to two rendertargets. But you only have one rendertarget!
struct TwoColourOutput
    float4 add      :SV_TARGET0;
    float4 multiply :SV_TARGET1;
And then the pixel shader does:
TwoColourOutput PSMain(v2f IN) : SV_TARGET
    TwoColourOutput result =...
    return result;
The result - the value in the rendertarget is multiplied by result.multiply, and then result.add is added to it. Viola!