{"record":{"id":"325113c7da5e8f52","repo":"MonoGame/MonoGame","slug":"invalid-stencil-operation","errorCode":null,"errorMessage":"Invalid stencil operation!","messagePattern":"Invalid stencil operation!","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Platform/Graphics/States/DepthStencilState.DirectX.cs","lineNumber":102,"sourceCode":"                    return SharpDX.Direct3D11.StencilOperation.Increment;\n\n                case StencilOperation.IncrementSaturation:\n                    return SharpDX.Direct3D11.StencilOperation.IncrementAndClamp;\n\n                case StencilOperation.Invert:\n                    return SharpDX.Direct3D11.StencilOperation.Invert;\n\n                case StencilOperation.Keep:\n                    return SharpDX.Direct3D11.StencilOperation.Keep;\n\n                case StencilOperation.Replace:\n                    return SharpDX.Direct3D11.StencilOperation.Replace;\n\n                case StencilOperation.Zero:\n                    return SharpDX.Direct3D11.StencilOperation.Zero;\n\n                default:\n                    throw new ArgumentException(\"Invalid stencil operation!\");\n            }\n        }\n\n        partial void PlatformDispose()\n        {\n            SharpDX.Utilities.Dispose(ref _state);\n        }\n    }\n}\n\n","sourceCodeStart":84,"sourceCodeEnd":113,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Platform/Graphics/States/DepthStencilState.DirectX.cs#L84-L113","documentation":"Thrown by the DepthStencilState DirectX backend's StencilOperation-to-SharpDX mapping switch when the input value does not match any known case. The switch converts MonoGame's StencilOperation enum to SharpDX.Direct3D11.StencilOperation; hitting the default branch means the enum held an undefined or out-of-range value, usually due to uninitialized state or a bad cast from an integer.","triggerScenarios":"Setting DepthStencilState.StencilFail / StencilDepthBufferFail / StencilPass to a value not in the StencilOperation enum (e.g. (StencilOperation)999), or default(StencilState) where a field was left as raw zero but mis-cast. Custom state objects constructed from deserialized data with an invalid enum value.","commonSituations":"Reading stencil op from a binary/JSON config that had a typo or unknown token; casting an int from an external tool without range-checking; copy-paste of a numeric literal.","solutions":["Validate stencil operation values against Enum.IsDefined(typeof(StencilOperation), value) before assigning to the state object.","Correct the configuration source to use a valid StencilOperation name (Keep, Zero, Replace, DecrementSaturation, Decrement, IncrementSaturation, Increment, Invert).","Default-construct the DepthStencilState and only override known fields."],"exampleFix":"// before\nstate.StencilEnable = true;\nstate.StencilFail = (StencilOperation)42; // invalid → throws on apply\n\n// after — use a defined enum value\nstate.StencilFail = StencilOperation.Keep;","handlingStrategy":"validation","validationCode":"// Validate stencil op before assigning\nstatic StencilOperation SafeStencilOp(int v)\n    => Enum.IsDefined(typeof(StencilOperation), v) ? (StencilOperation)v : StencilOperation.Keep;\n\nstate.StencilEnable = true;\nstate.StencilFail = SafeStencilOp(rawStencilFail);","typeGuard":"static bool IsValidStencilOp(StencilOperation op) => Enum.IsDefined(typeof(StencilOperation), op);","tryCatchPattern":"try { GraphicsDevice.DepthStencilState = state; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"stencil operation\"))\n{\n    state.StencilFail = StencilOperation.Keep;\n    state.StencilDepthBufferFail = StencilOperation.Keep;\n    state.StencilPass = StencilOperation.Keep;\n    GraphicsDevice.DepthStencilState = state;\n}","preventionTips":["Always assign named enum members, never cast raw ints.","Validate deserialized state with Enum.IsDefined.","Default-construct DepthStencilState and override only known fields.","Add unit tests for all enum members in your config format."],"tags":["directx","graphics","depth-stencil","enum-validation","monogame"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}