{"record":{"id":"08df851fbe756e15","repo":"MonoGame/MonoGame","slug":"invalid-blend-function","errorCode":null,"errorMessage":"Invalid blend function!","messagePattern":"Invalid blend function!","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/States/TargetBlendState.cs","lineNumber":242,"sourceCode":"            switch (blend)\n            {\n                case BlendFunction.Add:\n                    return SharpDX.Direct3D11.BlendOperation.Add;\n\n                case BlendFunction.Max:\n                    return SharpDX.Direct3D11.BlendOperation.Maximum;\n\n                case BlendFunction.Min:\n                    return SharpDX.Direct3D11.BlendOperation.Minimum;\n\n                case BlendFunction.ReverseSubtract:\n                    return SharpDX.Direct3D11.BlendOperation.ReverseSubtract;\n\n                case BlendFunction.Subtract:\n                    return SharpDX.Direct3D11.BlendOperation.Subtract;\n\n                default:\n                    throw new ArgumentException(\"Invalid blend function!\");\n            }\n        }\n\n        static private SharpDX.Direct3D11.BlendOption GetBlendOption(Blend blend, bool alpha)\n        {\n            switch (blend)\n            {\n                case Blend.BlendFactor:\n                    return SharpDX.Direct3D11.BlendOption.BlendFactor;\n\n                case Blend.DestinationAlpha:\n                    return SharpDX.Direct3D11.BlendOption.DestinationAlpha;\n\n                case Blend.DestinationColor:\n                    return alpha ? SharpDX.Direct3D11.BlendOption.DestinationAlpha : SharpDX.Direct3D11.BlendOption.DestinationColor;\n\n                case Blend.InverseBlendFactor:\n                    return SharpDX.Direct3D11.BlendOption.InverseBlendFactor;","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/States/TargetBlendState.cs#L224-L260","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate the BlendFunction value with Enum.IsDefined before assigning it to the blend state.","Fix the source of the bad integer (deserializer config, native interop marshalling) so only defined enum members are used.","Default any uninitialized BlendFunction fields to BlendFunction.Add explicitly."],"exampleFix":"// before\nblend.AlphaBlendFunction = (BlendFunction)someInt; // someInt == 42 -> throws 443\n\n// after\nif (!Enum.IsDefined(typeof(BlendFunction), someInt))\n    throw new ArgumentOutOfRangeException(nameof(someInt));\nblend.AlphaBlendFunction = (BlendFunction)someInt;","handlingStrategy":"validation","validationCode":"static BlendFunction SafeBlendFunction(int raw)\n{\n    if (!Enum.IsDefined(typeof(BlendFunction), raw))\n        throw new ArgumentOutOfRangeException(nameof(raw), $\"Invalid BlendFunction: {raw}\");\n    return (BlendFunction)raw;\n}","typeGuard":"static bool IsValid(BlendFunction f) => Enum.IsDefined(typeof(BlendFunction), f);","tryCatchPattern":null,"preventionTips":["Always validate ints cast to BlendFunction with Enum.IsDefined at the trust boundary.","Initialize BlendFunction fields explicitly to BlendFunction.Add.","Sanitize deserialized blend configs before applying them to a BlendState."],"tags":["monogame","graphics","blend","d3d11","enum-validation","argument-exception","internal-assert"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}