wgpu_core/command/
draw.rs

1use alloc::boxed::Box;
2
3use thiserror::Error;
4
5use wgt::error::{ErrorType, WebGpuError};
6
7use super::bind::BinderError;
8use crate::command::pass;
9use crate::{
10    binding_model::{LateMinBufferBindingSizeMismatch, PushConstantUploadError},
11    resource::{
12        DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError,
13        ResourceErrorIdent,
14    },
15    track::ResourceUsageCompatibilityError,
16};
17
18/// Error validating a draw call.
19#[derive(Clone, Debug, Error)]
20#[non_exhaustive]
21pub enum DrawError {
22    #[error("Blend constant needs to be set")]
23    MissingBlendConstant,
24    #[error("Render pipeline must be set")]
25    MissingPipeline(#[from] pass::MissingPipeline),
26    #[error("Currently set {pipeline} requires vertex buffer {index} to be set")]
27    MissingVertexBuffer {
28        pipeline: ResourceErrorIdent,
29        index: u32,
30    },
31    #[error("Index buffer must be set")]
32    MissingIndexBuffer,
33    #[error(transparent)]
34    IncompatibleBindGroup(#[from] Box<BinderError>),
35    #[error("Vertex {last_vertex} extends beyond limit {vertex_limit} imposed by the buffer in slot {slot}. Did you bind the correct `Vertex` step-rate vertex buffer?")]
36    VertexBeyondLimit {
37        last_vertex: u64,
38        vertex_limit: u64,
39        slot: u32,
40    },
41    #[error("Instance {last_instance} extends beyond limit {instance_limit} imposed by the buffer in slot {slot}. Did you bind the correct `Instance` step-rate vertex buffer?")]
42    InstanceBeyondLimit {
43        last_instance: u64,
44        instance_limit: u64,
45        slot: u32,
46    },
47    #[error("Index {last_index} extends beyond limit {index_limit}. Did you bind the correct index buffer?")]
48    IndexBeyondLimit { last_index: u64, index_limit: u64 },
49    #[error(
50        "Index buffer format {buffer_format:?} doesn't match {pipeline}'s index format {pipeline_format:?}"
51    )]
52    UnmatchedIndexFormats {
53        pipeline: ResourceErrorIdent,
54        pipeline_format: wgt::IndexFormat,
55        buffer_format: wgt::IndexFormat,
56    },
57    #[error(transparent)]
58    BindingSizeTooSmall(#[from] LateMinBufferBindingSizeMismatch),
59}
60
61impl WebGpuError for DrawError {
62    fn webgpu_error_type(&self) -> ErrorType {
63        ErrorType::Validation
64    }
65}
66
67/// Error encountered when encoding a render command.
68/// This is the shared error set between render bundles and passes.
69#[derive(Clone, Debug, Error)]
70#[non_exhaustive]
71pub enum RenderCommandError {
72    #[error(transparent)]
73    BindGroupIndexOutOfRange(#[from] pass::BindGroupIndexOutOfRange),
74    #[error("Vertex buffer index {index} is greater than the device's requested `max_vertex_buffers` limit {max}")]
75    VertexBufferIndexOutOfRange { index: u32, max: u32 },
76    #[error("Render pipeline targets are incompatible with render pass")]
77    IncompatiblePipelineTargets(#[from] crate::device::RenderPassCompatibilityError),
78    #[error("{0} writes to depth, while the pass has read-only depth access")]
79    IncompatibleDepthAccess(ResourceErrorIdent),
80    #[error("{0} writes to stencil, while the pass has read-only stencil access")]
81    IncompatibleStencilAccess(ResourceErrorIdent),
82    #[error(transparent)]
83    ResourceUsageCompatibility(#[from] ResourceUsageCompatibilityError),
84    #[error(transparent)]
85    DestroyedResource(#[from] DestroyedResourceError),
86    #[error(transparent)]
87    MissingBufferUsage(#[from] MissingBufferUsageError),
88    #[error(transparent)]
89    MissingTextureUsage(#[from] MissingTextureUsageError),
90    #[error(transparent)]
91    PushConstants(#[from] PushConstantUploadError),
92    #[error("Viewport size {{ w: {w}, h: {h} }} greater than device's requested `max_texture_dimension_2d` limit {max}, or less than zero")]
93    InvalidViewportRectSize { w: f32, h: f32, max: u32 },
94    #[error("Viewport has invalid rect {rect:?} for device's requested `max_texture_dimension_2d` limit; Origin less than -2 * `max_texture_dimension_2d` ({min}), or rect extends past 2 * `max_texture_dimension_2d` - 1 ({max})")]
95    InvalidViewportRectPosition { rect: Rect<f32>, min: f32, max: f32 },
96    #[error("Viewport minDepth {0} and/or maxDepth {1} are not in [0, 1]")]
97    InvalidViewportDepth(f32, f32),
98    #[error("Scissor {0:?} is not contained in the render target {1:?}")]
99    InvalidScissorRect(Rect<u32>, wgt::Extent3d),
100    #[error("Support for {0} is not implemented yet")]
101    Unimplemented(&'static str),
102}
103
104impl WebGpuError for RenderCommandError {
105    fn webgpu_error_type(&self) -> ErrorType {
106        let e: &dyn WebGpuError = match self {
107            Self::IncompatiblePipelineTargets(e) => e,
108            Self::ResourceUsageCompatibility(e) => e,
109            Self::DestroyedResource(e) => e,
110            Self::MissingBufferUsage(e) => e,
111            Self::MissingTextureUsage(e) => e,
112            Self::PushConstants(e) => e,
113
114            Self::BindGroupIndexOutOfRange { .. }
115            | Self::VertexBufferIndexOutOfRange { .. }
116            | Self::IncompatibleDepthAccess(..)
117            | Self::IncompatibleStencilAccess(..)
118            | Self::InvalidViewportRectSize { .. }
119            | Self::InvalidViewportRectPosition { .. }
120            | Self::InvalidViewportDepth(..)
121            | Self::InvalidScissorRect(..)
122            | Self::Unimplemented(..) => return ErrorType::Validation,
123        };
124        e.webgpu_error_type()
125    }
126}
127
128#[derive(Clone, Copy, Debug, Default)]
129#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
130pub struct Rect<T> {
131    pub x: T,
132    pub y: T,
133    pub w: T,
134    pub h: T,
135}