1use alloc::boxed::Box;
2use core::num::NonZeroU32;
3
4use thiserror::Error;
5
6use wgt::error::{ErrorType, WebGpuError};
7
8use super::bind::BinderError;
9use crate::command::pass;
10use crate::resource::InvalidResourceError;
11use crate::validation::InvalidWorkgroupSizeError;
12use crate::{
13 binding_model::{BindingError, ImmediateUploadError},
14 device::Device,
15 resource::{
16 DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError,
17 ResourceErrorIdent,
18 },
19 track::ResourceUsageCompatibilityError,
20};
21
22#[derive(Clone, Debug, Error)]
24#[non_exhaustive]
25pub enum DrawError {
26 #[error("Blend constant needs to be set")]
27 MissingBlendConstant,
28 #[error("Render pipeline must be set")]
29 MissingPipeline(#[from] pass::MissingPipeline),
30 #[error("Currently set {pipeline} requires vertex buffer {index} to be set")]
31 MissingVertexBuffer {
32 pipeline: ResourceErrorIdent,
33 index: usize,
34 },
35 #[error("Index buffer must be set")]
36 MissingIndexBuffer,
37 #[error(transparent)]
38 IncompatibleBindGroup(#[from] Box<BinderError>),
39 #[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?")]
40 VertexBeyondLimit {
41 last_vertex: u64,
42 vertex_limit: u64,
43 slot: u32,
44 },
45 #[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?")]
46 InstanceBeyondLimit {
47 last_instance: u64,
48 instance_limit: u64,
49 slot: u32,
50 },
51 #[error("Index {last_index} extends beyond limit {index_limit}. Did you bind the correct index buffer?")]
52 IndexBeyondLimit { last_index: u64, index_limit: u64 },
53 #[error("For indexed drawing with strip topology, {pipeline}'s strip index format {strip_index_format:?} must match index buffer format {buffer_format:?}")]
54 UnmatchedStripIndexFormat {
55 pipeline: ResourceErrorIdent,
56 strip_index_format: Option<wgt::IndexFormat>,
57 buffer_format: wgt::IndexFormat,
58 },
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(transparent)]
67 InvalidGroupSize(#[from] InvalidWorkgroupSizeError),
68 #[error(
69 "Mesh shader calls in multiview render passes require enabling the `EXPERIMENTAL_MESH_SHADER_MULTIVIEW` feature, and the highest bit ({highest_view_index}) in the multiview mask must be less than `Limits::max_mesh_multiview_view_count` ({max_multiviews})"
70 )]
71 MeshPipelineMultiviewLimitsViolated {
72 highest_view_index: u32,
73 max_multiviews: u32,
74 },
75 #[error("Not all immediate data required by the pipeline has been set via set_immediates (missing byte ranges: {missing})")]
76 MissingImmediateData {
77 missing: naga::valid::ImmediateSlots,
78 },
79 #[error("The number of bind groups + vertex buffers {given} exceeds the limit {limit}")]
80 TooManyBindGroupsPlusVertexBuffers { given: u32, limit: u32 },
81}
82
83impl WebGpuError for DrawError {
84 fn webgpu_error_type(&self) -> ErrorType {
85 ErrorType::Validation
86 }
87}
88
89#[derive(Clone, Debug, Error)]
92#[non_exhaustive]
93pub enum RenderCommandError {
94 #[error(transparent)]
95 BindGroupIndexOutOfRange(#[from] pass::BindGroupIndexOutOfRange),
96 #[error("Vertex buffer index {index} is greater than the device's requested `max_vertex_buffers` limit {max}")]
97 VertexBufferIndexOutOfRange { index: u32, max: u32 },
98 #[error(
99 "Offset {offset} for vertex buffer in slot {slot} is not a multiple of `VERTEX_ALIGNMENT`"
100 )]
101 UnalignedVertexBuffer { slot: u32, offset: u64 },
102 #[error("Offset {offset} for index buffer is not a multiple of {alignment}")]
103 UnalignedIndexBuffer { offset: u64, alignment: usize },
104 #[error("Render pipeline targets are incompatible with render pass")]
105 IncompatiblePipelineTargets(#[from] crate::device::RenderPassCompatibilityError),
106 #[error("{0} writes to depth, while the pass has read-only depth access")]
107 IncompatibleDepthAccess(ResourceErrorIdent),
108 #[error("{0} writes to stencil, while the pass has read-only stencil access")]
109 IncompatibleStencilAccess(ResourceErrorIdent),
110 #[error(transparent)]
111 ResourceUsageCompatibility(#[from] ResourceUsageCompatibilityError),
112 #[error(transparent)]
113 DestroyedResource(#[from] DestroyedResourceError),
114 #[error(transparent)]
115 InvalidResource(#[from] InvalidResourceError),
116 #[error(transparent)]
117 MissingBufferUsage(#[from] MissingBufferUsageError),
118 #[error(transparent)]
119 MissingTextureUsage(#[from] MissingTextureUsageError),
120 #[error(transparent)]
121 ImmediateData(#[from] ImmediateUploadError),
122 #[error(transparent)]
123 BindingError(#[from] BindingError),
124 #[error("Viewport size {{ w: {w}, h: {h} }} greater than device's requested `max_texture_dimension_2d` limit {max}, or less than zero")]
125 InvalidViewportRectSize { w: f32, h: f32, max: u32 },
126 #[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})")]
127 InvalidViewportRectPosition { rect: Rect<f32>, min: f32, max: f32 },
128 #[error("Viewport minDepth {0} and/or maxDepth {1} are not in [0, 1]")]
129 InvalidViewportDepth(f32, f32),
130 #[error("Scissor {0:?} is not contained in the render target {1:?}")]
131 InvalidScissorRect(Rect<u32>, wgt::Extent3d),
132 #[error("Indirect buffer offset {0:?} is not a multiple of 4")]
133 UnalignedIndirectBufferOffset(wgt::BufferAddress),
134 #[error("Indirect draw arguments of {args_size} bytes (count = {count}) starting at {offset} would overrun buffer size of {buffer_size}")]
135 IndirectBufferOverrun {
136 count: u32,
137 offset: u64,
138 args_size: u64,
139 buffer_size: u64,
140 },
141 #[error("Support for {0} is not implemented yet")]
142 Unimplemented(&'static str),
143}
144
145impl WebGpuError for RenderCommandError {
146 fn webgpu_error_type(&self) -> ErrorType {
147 match self {
148 Self::IncompatiblePipelineTargets(e) => e.webgpu_error_type(),
149 Self::ResourceUsageCompatibility(e) => e.webgpu_error_type(),
150 Self::DestroyedResource(e) => e.webgpu_error_type(),
151 Self::InvalidResource(e) => e.webgpu_error_type(),
152 Self::MissingBufferUsage(e) => e.webgpu_error_type(),
153 Self::MissingTextureUsage(e) => e.webgpu_error_type(),
154 Self::ImmediateData(e) => e.webgpu_error_type(),
155 Self::BindingError(e) => e.webgpu_error_type(),
156
157 Self::BindGroupIndexOutOfRange { .. }
158 | Self::VertexBufferIndexOutOfRange { .. }
159 | Self::UnalignedIndexBuffer { .. }
160 | Self::UnalignedVertexBuffer { .. }
161 | Self::IncompatibleDepthAccess(..)
162 | Self::IncompatibleStencilAccess(..)
163 | Self::InvalidViewportRectSize { .. }
164 | Self::InvalidViewportRectPosition { .. }
165 | Self::InvalidViewportDepth(..)
166 | Self::InvalidScissorRect(..)
167 | Self::UnalignedIndirectBufferOffset(..)
168 | Self::IndirectBufferOverrun { .. }
169 | Self::Unimplemented(..) => ErrorType::Validation,
170 }
171 }
172}
173
174pub use wgt::Rect;
175
176pub(crate) fn validate_mesh_draw_multiview(
179 device: &Device,
180 multiview_mask: Option<NonZeroU32>,
181) -> Result<(), DrawError> {
182 if let Some(mv) = multiview_mask {
183 let highest_bit = 31 - mv.leading_zeros();
184
185 if !device
186 .features
187 .contains(wgt::Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW)
188 || highest_bit >= device.limits.max_mesh_multiview_view_count
189 {
190 return Err(DrawError::MeshPipelineMultiviewLimitsViolated {
191 highest_view_index: highest_bit,
192 max_multiviews: device.limits.max_mesh_multiview_view_count,
193 });
194 }
195 }
196 Ok(())
197}