bevyengine/bevy · error

Can't create depth attachment. Texture format is not a depth

Error message

Can't create depth attachment. Texture format is not a depth-stencil format.

What it means

When building the DepthStencilViews for a depth attachment, the code matches the texture format's channel: DEPTH_STENCIL, DEPTH, and STENCIL map to view sets, and every other format — i.e. any color format — panics with 'not a depth-stencil format'. Depth attachments must use actual depth/stencil formats such as Depth32Float or Depth24PlusStencil8.

Source

Thrown at crates/bevy_render/src/texture/texture_attachment.rs:188

            })
        };

        match texture.texture.format().channels() {
            wgpu_types::TextureChannel::DEPTH_STENCIL => DepthStencilViews::DepthStencil {
                combined_view: texture
                    .texture
                    .create_view(&TextureViewDescriptor::default()),
                depth_view: depth_view(),
                stencil_view: stencil_view(),
            },
            wgpu_types::TextureChannel::DEPTH => DepthStencilViews::DepthOnly {
                depth_view: depth_view(),
            },
            wgpu_types::TextureChannel::STENCIL => DepthStencilViews::StencilOnly {
                stencil_view: stencil_view(),
            },
            _ => {
                panic!(
                    "Can't create depth attachment. Texture format is not a depth-stencil format."
                )
            }
        }
    }
}

/// A wrapper for a [`CachedTexture`] that is used as a depth [`RenderPassDepthStencilAttachment`].
#[derive(Clone)]
pub struct DepthStencilAttachment {
    pub texture: CachedTexture,
    pub previous_frame_texture: Option<CachedTexture>,
    depth_stencil_view_attachment: DepthStencilViewAttachment,
    pub previous_frame_depth_stencil_views: Option<DepthStencilViews>,
}

impl DepthStencilAttachment {
    pub fn depth_stencil_views(&self) -> &DepthStencilViews {

View on GitHub (pinned to 396ca72708)

Solutions

  1. Allocate the depth texture with a depth or depth-stencil format (Depth32Float, Depth16Unorm, Depth24Plus, Depth24PlusStencil8)
  2. Check format.has_depth_aspect() / has_stencil_aspect() before building the attachment and fail with a clear error instead
  3. Audit render-graph node definitions to ensure the depth target is not reusing a color format string/constant

Example fix

// before
let depth_texture = texture_cache.get(&depth_image_descriptor, /* format */ TextureFormat::Bgra8Unorm);
let attachment = DepthStencilAttachment::new(depth_texture);

// after
let depth_texture = texture_cache.get(
    &depth_image_descriptor,
    TextureFormat::Depth32Float,
);
let attachment = DepthStencilAttachment::new(depth_texture);
Defensive patterns

Strategy: type-guard

Validate before calling

// Before building the attachment:
if !format.has_depth_aspect() && !format.has_stencil_aspect() {
    return Err(format!("{format:?} is not a depth-stencil format"));
}

Type guard

fn is_depth_stencil_format(f: wgpu::TextureFormat) -> bool {
    f.has_depth_aspect() || f.has_stencil_aspect()
}

Prevention

When it happens

Trigger: Constructing a DepthStencilAttachment (or configuring a camera/extracted view's depth texture) over a CachedTexture whose format is a color format like Bgra8Unorm — for example copying a render-target format into the depth configuration or resolving the wrong texture.

Common situations: Custom render graphs where the depth target is allocated with the same format as the color target; switching a post-processing target to depth usage without changing format; refactors that mix up color and depth CachedTextures.

Related errors


AI-assisted analysis of bevyengine/bevy@396ca72708 (2026-08-20). Data as JSON: /api/errors/e356862035d0b5bc. Report an issue: GitHub.