MonoGame/MonoGame · error · ArgumentException

Invalid blend function!

Error message

Invalid blend function!

What it means

Thrown by TargetBlendState.GetBlendOperation in its switch default arm when translating a BlendFunction enum value to the Direct3D11 BlendOperation. Every defined BlendFunction member (Add, Subtract, ReverseSubtract, Min, Max) has a case, so this fires only when the enum holds an out-of-range/undefined value (e.g. an invalid cast or uninitialized field).

Source

Thrown at MonoGame.Framework/Graphics/States/TargetBlendState.cs:242

            switch (blend)
            {
                case BlendFunction.Add:
                    return SharpDX.Direct3D11.BlendOperation.Add;

                case BlendFunction.Max:
                    return SharpDX.Direct3D11.BlendOperation.Maximum;

                case BlendFunction.Min:
                    return SharpDX.Direct3D11.BlendOperation.Minimum;

                case BlendFunction.ReverseSubtract:
                    return SharpDX.Direct3D11.BlendOperation.ReverseSubtract;

                case BlendFunction.Subtract:
                    return SharpDX.Direct3D11.BlendOperation.Subtract;

                default:
                    throw new ArgumentException("Invalid blend function!");
            }
        }

        static private SharpDX.Direct3D11.BlendOption GetBlendOption(Blend blend, bool alpha)
        {
            switch (blend)
            {
                case Blend.BlendFactor:
                    return SharpDX.Direct3D11.BlendOption.BlendFactor;

                case Blend.DestinationAlpha:
                    return SharpDX.Direct3D11.BlendOption.DestinationAlpha;

                case Blend.DestinationColor:
                    return alpha ? SharpDX.Direct3D11.BlendOption.DestinationAlpha : SharpDX.Direct3D11.BlendOption.DestinationColor;

                case Blend.InverseBlendFactor:
                    return SharpDX.Direct3D11.BlendOption.InverseBlendFactor;

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Validate the BlendFunction value with Enum.IsDefined before assigning it to the blend state.
  2. Fix the source of the bad integer (deserializer config, native interop marshalling) so only defined enum members are used.
  3. Default any uninitialized BlendFunction fields to BlendFunction.Add explicitly.

Example fix

// before
blend.AlphaBlendFunction = (BlendFunction)someInt; // someInt == 42 -> throws 443

// after
if (!Enum.IsDefined(typeof(BlendFunction), someInt))
    throw new ArgumentOutOfRangeException(nameof(someInt));
blend.AlphaBlendFunction = (BlendFunction)someInt;
Defensive patterns

Strategy: validation

Validate before calling

static BlendFunction SafeBlendFunction(int raw)
{
    if (!Enum.IsDefined(typeof(BlendFunction), raw))
        throw new ArgumentOutOfRangeException(nameof(raw), $"Invalid BlendFunction: {raw}");
    return (BlendFunction)raw;
}

Type guard

static bool IsValid(BlendFunction f) => Enum.IsDefined(typeof(BlendFunction), f);

Prevention

When it happens

Trigger: Assigning BlendState.AlphaBlendFunction (or a per-target blend function) a value produced by an unchecked cast from an int that is not a defined BlendFunction; leaving a BlendFunction field at its raw zeroed memory in a struct; deserializing a corrupt value. The throw occurs when the device resolves the blend state for the DirectX backend.

Common situations: Loading a serialized/custom blend configuration with a bad enum integer; interop with native code that hands back an int cast to BlendFunction; edge testing of the blend pipeline on the WindowsDX platform.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/08df851fbe756e15. Report an issue: GitHub.