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::{BindingError, 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    #[error(
61        "Wrong pipeline type for this draw command. Attempted to call {} draw command on {} pipeline",
62        if *wanted_mesh_pipeline {"mesh shader"} else {"standard"},
63        if *wanted_mesh_pipeline {"standard"} else {"mesh shader"},
64    )]
65    WrongPipelineType { wanted_mesh_pipeline: bool },
66    #[error(
67        "Each current draw group size dimension ({current:?}) must be less or equal to {limit}, and the product must be less or equal to {max_total}"
68    )]
69    InvalidGroupSize {
70        current: [u32; 3],
71        limit: u32,
72        max_total: u32,
73    },
74}
75
76impl WebGpuError for DrawError {
77    fn webgpu_error_type(&self) -> ErrorType {
78        ErrorType::Validation
79    }
80}
81
82/// Error encountered when encoding a render command.
83/// This is the shared error set between render bundles and passes.
84#[derive(Clone, Debug, Error)]
85#[non_exhaustive]
86pub enum RenderCommandError {
87    #[error(transparent)]
88    BindGroupIndexOutOfRange(#[from] pass::BindGroupIndexOutOfRange),
89    #[error("Vertex buffer index {index} is greater than the device's requested `max_vertex_buffers` limit {max}")]
90    VertexBufferIndexOutOfRange { index: u32, max: u32 },
91    #[error(
92        "Offset {offset} for vertex buffer in slot {slot} is not a multiple of `VERTEX_ALIGNMENT`"
93    )]
94    UnalignedVertexBuffer { slot: u32, offset: u64 },
95    #[error("Offset {offset} for index buffer is not a multiple of {alignment}")]
96    UnalignedIndexBuffer { offset: u64, alignment: usize },
97    #[error("Render pipeline targets are incompatible with render pass")]
98    IncompatiblePipelineTargets(#[from] crate::device::RenderPassCompatibilityError),
99    #[error("{0} writes to depth, while the pass has read-only depth access")]
100    IncompatibleDepthAccess(ResourceErrorIdent),
101    #[error("{0} writes to stencil, while the pass has read-only stencil access")]
102    IncompatibleStencilAccess(ResourceErrorIdent),
103    #[error(transparent)]
104    ResourceUsageCompatibility(#[from] ResourceUsageCompatibilityError),
105    #[error(transparent)]
106    DestroyedResource(#[from] DestroyedResourceError),
107    #[error(transparent)]
108    MissingBufferUsage(#[from] MissingBufferUsageError),
109    #[error(transparent)]
110    MissingTextureUsage(#[from] MissingTextureUsageError),
111    #[error(transparent)]
112    PushConstants(#[from] PushConstantUploadError),
113    #[error(transparent)]
114    BindingError(#[from] BindingError),
115    #[error("Viewport size {{ w: {w}, h: {h} }} greater than device's requested `max_texture_dimension_2d` limit {max}, or less than zero")]
116    InvalidViewportRectSize { w: f32, h: f32, max: u32 },
117    #[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})")]
118    InvalidViewportRectPosition { rect: Rect<f32>, min: f32, max: f32 },
119    #[error("Viewport minDepth {0} and/or maxDepth {1} are not in [0, 1]")]
120    InvalidViewportDepth(f32, f32),
121    #[error("Scissor {0:?} is not contained in the render target {1:?}")]
122    InvalidScissorRect(Rect<u32>, wgt::Extent3d),
123    #[error("Support for {0} is not implemented yet")]
124    Unimplemented(&'static str),
125}
126
127impl WebGpuError for RenderCommandError {
128    fn webgpu_error_type(&self) -> ErrorType {
129        let e: &dyn WebGpuError = match self {
130            Self::IncompatiblePipelineTargets(e) => e,
131            Self::ResourceUsageCompatibility(e) => e,
132            Self::DestroyedResource(e) => e,
133            Self::MissingBufferUsage(e) => e,
134            Self::MissingTextureUsage(e) => e,
135            Self::PushConstants(e) => e,
136            Self::BindingError(e) => e,
137
138            Self::BindGroupIndexOutOfRange { .. }
139            | Self::VertexBufferIndexOutOfRange { .. }
140            | Self::UnalignedIndexBuffer { .. }
141            | Self::UnalignedVertexBuffer { .. }
142            | Self::IncompatibleDepthAccess(..)
143            | Self::IncompatibleStencilAccess(..)
144            | Self::InvalidViewportRectSize { .. }
145            | Self::InvalidViewportRectPosition { .. }
146            | Self::InvalidViewportDepth(..)
147            | Self::InvalidScissorRect(..)
148            | Self::Unimplemented(..) => return ErrorType::Validation,
149        };
150        e.webgpu_error_type()
151    }
152}
153
154#[derive(Clone, Copy, Debug, Default)]
155#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
156pub struct Rect<T> {
157    pub x: T,
158    pub y: T,
159    pub w: T,
160    pub h: T,
161}