{"record":{"id":"67e20d6ea044511b","repo":"stride3d/stride","slug":"this-texture-is-not-a-valid-depth-stencil-texture","errorCode":null,"errorMessage":"This Texture is not a valid Depth-Stencil Texture","messagePattern":"This Texture is not a valid Depth-Stencil Texture","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Texture.Extensions.cs","lineNumber":45,"sourceCode":"    /// <returns>A new <see cref=\"Texture\"/> representing the Texture View bound to <paramref name=\"texture\"/>.</returns>\n    public static Texture ToTextureView(this Texture texture, ViewType viewType, int arraySlice, int mipLevel)\n    {\n        var viewDescription = texture.ViewDescription;\n        viewDescription.Type = viewType;\n        viewDescription.ArraySlice = arraySlice;\n        viewDescription.MipLevel = mipLevel;\n        return texture.ToTextureView(viewDescription);\n    }\n\n    /// <summary>\n    ///   Creates a Shader Resource View that is read-only on a Depth-Stencil Texture.\n    /// </summary>\n    /// <param name=\"texture\">The Texture to create a read-only Depth-Stencil Texture View for.</param>\n    /// <returns>A new <see cref=\"Texture\"/> representing the Texture View bound to <paramref name=\"texture\"/>.</returns>\n    public static Texture ToDepthStencilReadOnlyTexture(this Texture texture)\n    {\n        if (!texture.IsDepthStencil)\n            throw new NotSupportedException(\"This Texture is not a valid Depth-Stencil Texture\");\n\n        var viewDescription = texture.ViewDescription;\n        viewDescription.Flags = TextureFlags.DepthStencilReadOnly;\n        return texture.ToTextureView(viewDescription);\n    }\n\n    /// <summary>\n    ///   Creates a Shader Resource View on a Depth-Stencil Texture.\n    /// </summary>\n    /// <param name=\"texture\">The Texture to create a Depth-Stencil Texture View for.</param>\n    /// <returns>A new <see cref=\"Texture\"/> representing the Texture View bound to <paramref name=\"texture\"/>.</returns>\n    public static Texture CreateDepthTextureCompatible(this Texture texture)\n    {\n        if (!texture.IsDepthStencil)\n            throw new NotSupportedException(\"This Texture is not a valid Depth-Stencil Texture\");\n\n        var description = texture.Description;\n        description.Format = Texture.ComputeShaderResourceFormatFromDepthFormat(description.Format); // TODO: review this","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Texture.Extensions.cs#L27-L63","documentation":"The Texture.ToDepthStencilReadOnlyTexture extension throws NotSupportedException when the source texture does not have the DepthStencil flag (texture.IsDepthStencil is false). A read-only depth-stencil view can only be created on a texture that was created as a depth-stencil resource. The library refuses rather than producing an invalid GPU view.","triggerScenarios":"Calling ToDepthStencilReadOnlyTexture() on a color/render-target texture, or on a plain shader-resource texture that was never created with TextureFlags.DepthStencil.","commonSituations":"Wiring a shadow-map or SSAO pass and accidentally passing the scene color target instead of the depth buffer; recreating the depth texture without DepthStencil flags after a resize; refactoring where the wrong Texture property is bound.","solutions":["Create the source texture with TextureFlags.DepthStencil (e.g. depth buffer = Texture.New2D(..., TextureFlags.DepthStencil))","Verify texture.IsDepthStencil before calling the extension and route color textures to a different path","Pass the correct texture (the depth buffer of the render target, e.g. depthBuffer or RenderTarget.DepthStencilBuffer)","Check texture creation code after resolution changes to ensure flags are preserved"],"exampleFix":"// before\nTexture readonlyDepth = colorTarget.ToDepthStencilReadOnlyTexture(); // throws\n// after\nvar depth = Texture.New2D(device, width, height, PixelFormat.R32_Typeless, TextureFlags.DepthStencil | TextureFlags.ShaderResource);\nTexture readonlyDepth = depth.ToDepthStencilReadOnlyTexture();","handlingStrategy":"validation","validationCode":"if (texture.IsDepthStencil)\n    var view = texture.ToDepthStencilReadOnlyTexture();\nelse\n    Log.Error(\"ToDepthStencilReadOnlyTexture requires a TextureFlags.DepthStencil texture\");","typeGuard":"static bool IsDepthTexture(Texture t) => t?.IsDepthStencil == true;","tryCatchPattern":"try { view = texture.ToDepthStencilReadOnlyTexture(); }\ncatch (NotSupportedException) { Log.Error(\"Texture lacks DepthStencil flags\"); }","preventionTips":["Create depth textures with TextureFlags.DepthStencil explicitly","Bind RenderTarget.DepthStencilBuffer, never the color target, for depth passes","Add debug asserts on IsDepthStencil at pass setup"],"tags":["csharp","texture","depth-stencil","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}