{"record":{"id":"5bfefab009fe4c2b","repo":"stride3d/stride","slug":"nostencilbufferfordepthformat-formatted-with-viewformat","errorCode":null,"errorMessage":"NoStencilBufferForDepthFormat (formatted with ViewFormat)","messagePattern":"NoStencilBufferForDepthFormat \\(formatted with ViewFormat\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Direct3D12/CommandList.Direct3D12.cs","lineNumber":1172,"sourceCode":"        /// <param name=\"options\">\n        ///   A combination of <see cref=\"DepthStencilClearOptions\"/> flags identifying what parts of the Depth-Stencil Buffer to clear.\n        /// </param>\n        /// <param name=\"depth\">The depth value to use for clearing the Depth Buffer.</param>\n        /// <param name=\"stencil\">The stencil value to use for clearing the Stencil Buffer.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"depthStencilBuffer\"/> is <see langword=\"null\"/>.</exception>\n        /// <exception cref=\"InvalidOperationException\">Cannot clear a Stencil Buffer without a Stencil Buffer format.</exception>\n        public void Clear(Texture depthStencilBuffer, DepthStencilClearOptions options, float depth = 1, byte stencil = 0)\n        {\n            ArgumentNullException.ThrowIfNull(depthStencilBuffer);\n\n            ResourceBarrierTransition(depthStencilBuffer, BarrierLayout.DepthStencilWrite);\n            FlushResourceBarriers();\n\n            // Check that the Depth-Stencil Buffer has a Stencil if Clear Stencil is requested\n            if (options.HasFlag(DepthStencilClearOptions.Stencil))\n            {\n                if (!depthStencilBuffer.HasStencil)\n                    throw new InvalidOperationException(string.Format(FrameworkResources.NoStencilBufferForDepthFormat, depthStencilBuffer.ViewFormat));\n            }\n\n            scoped ref SilkBox2I nullRect = ref NullRef<SilkBox2I>();\n\n            currentCommandList.NativeCommandList.ClearDepthStencilView(depthStencilBuffer.NativeDepthStencilView,\n                                                                       (ClearFlags) options, depth, stencil,\n                                                                       NumRects: 0, in nullRect);\n            RecordDebugCounter(DebugCounterKind.Clear);\n        }\n\n        /// <summary>\n        ///   Clears the specified Render Target.\n        /// </summary>\n        /// <param name=\"renderTarget\">The Render Target to clear.</param>\n        /// <param name=\"color\">The color to use to clear the Render Target.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"renderTarget\"/> is <see langword=\"null\"/>.</exception>\n        public void Clear(Texture renderTarget, Color4 color)\n        {","sourceCodeStart":1154,"sourceCodeEnd":1190,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Direct3D12/CommandList.Direct3D12.cs#L1154-L1190","documentation":"Thrown in CommandList.Clear on D3D12 when DepthStencilClearOptions.Stencil is requested but the bound depth-stencil buffer has no stencil component (HasStencil == false). The library throws InvalidOperationException, formatting FrameworkResources.NoStencilBufferForDepthFormat with the buffer's ViewFormat, because there is no stencil plane to clear.","triggerScenarios":"Calling commandList.Clear(depthStencilBuffer, DepthStencilClearOptions.Depth | DepthStencilClearOptions.Stencil, ...) (or just Stencil) where depthStencilBuffer.ViewFormat is stencil-less, e.g. D16_UNorm or D32_Float instead of D24_UNorm_S8_UInt / D32_Float_S8X24_UInt.","commonSituations":"A shared clear helper always passing Stencil, a camera/graphics-compositor configured with a depth-only format while stencil clears remain enabled, or switching the depth format to D32_Float for precision without updating clear options.","solutions":["Remove DepthStencilClearOptions.Stencil from the clear options, clearing depth only.","Recreate the depth-stencil buffer with a packed depth-stencil format (D24_UNorm_S8_UInt or D32_Float_S8X24_UInt).","Guard the call with a check of depthStencilBuffer.HasStencil before requesting a stencil clear."],"exampleFix":"// before\ncommandList.Clear(depthBuffer, DepthStencilClearOptions.Depth | DepthStencilClearOptions.Stencil);\n// after\nvar options = depthBuffer.HasStencil\n    ? DepthStencilClearOptions.Depth | DepthStencilClearOptions.Stencil\n    : DepthStencilClearOptions.Depth;\ncommandList.Clear(depthBuffer, options);","handlingStrategy":"type-guard","validationCode":"if (options.HasFlag(DepthStencilClearOptions.Stencil) && !depthStencilBuffer.HasStencil)\n    options &= ~DepthStencilClearOptions.Stencil;","typeGuard":"static bool SupportsStencilClear(Texture depthBuffer, DepthStencilClearOptions options) => !options.HasFlag(DepthStencilClearOptions.Stencil) || depthBuffer.HasStencil;","tryCatchPattern":"try { commandList.Clear(depthBuffer, options); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"stencil\")) { commandList.Clear(depthBuffer, options & ~DepthStencilClearOptions.Stencil); }","preventionTips":["Always check Texture.HasStencil before requesting a stencil clear.","Use packed depth-stencil formats (D24_UNorm_S8_UInt, D32_Float_S8X24_UInt) if stencil is part of your rendering pipeline.","Derive clear options from the depth buffer's format instead of hard-coding them."],"tags":["graphics","direct3d12","depth-stencil","stencil","clear"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}