From c9c7359f5b52f751077e583513efef7cd2c19806 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Tue, 14 Apr 2026 18:10:15 +0200 Subject: [PATCH] deduplicate vertex buffer validation in `is_ready` --- wgpu-core/src/command/bundle.rs | 29 +------------- wgpu-core/src/command/render.rs | 70 +++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 58 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 308373c24..782b06c7c 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -1454,34 +1454,7 @@ impl State { self.binder.check_compatibility(pipeline.as_ref())?; self.binder.check_late_buffer_bindings()?; - // Check all needed vertex buffers have been bound - for index in pipeline - .vertex_steps - .iter() - .enumerate() - .filter_map(|(index, step)| step.map(|_| index)) - { - if self.vertex.slots[index].is_none() { - return Err(DrawError::MissingVertexBuffer { - pipeline: pipeline.error_ident(), - index, - }); - } - } - - let bind_group_space_used = self.binder.last_assigned_index().map_or(0, |i| i + 1); - let vertex_buffer_space_used = self.vertex.last_assigned_index().map_or(0, |i| i + 1); - - let bind_groups_plus_vertex_buffers = - u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap(); - if bind_groups_plus_vertex_buffers - > self.device.limits.max_bind_groups_plus_vertex_buffers - { - return Err(DrawError::TooManyBindGroupsPlusVertexBuffers { - given: bind_groups_plus_vertex_buffers, - limit: self.device.limits.max_bind_groups_plus_vertex_buffers, - }); - } + self.vertex.validate(pipeline.as_ref(), &self.binder)?; if family == DrawCommandFamily::DrawIndexed { let index_format = match &self.index { diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 3ea7b16c4..4c6633c10 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -523,7 +523,7 @@ pub(crate) struct VertexSlot { /// derived from those buffers and the current pipeline, avoiding recomputation on each draw. #[derive(Debug, Default)] pub(crate) struct VertexState { - pub(crate) slots: [Option; hal::MAX_VERTEX_BUFFERS], + slots: [Option; hal::MAX_VERTEX_BUFFERS], pub(crate) limits: VertexLimits, } @@ -557,7 +557,7 @@ impl VertexState { ); } - pub(crate) fn last_assigned_index(&self) -> Option { + fn last_assigned_index(&self) -> Option { self.slots .iter() .enumerate() @@ -565,6 +565,43 @@ impl VertexState { .next_back() } + pub(super) fn validate( + &self, + pipeline: &RenderPipeline, + binder: &Binder, + ) -> Result<(), DrawError> { + // Check all needed vertex buffers have been bound + for index in pipeline + .vertex_steps + .iter() + .enumerate() + .filter_map(|(index, step)| step.map(|_| index)) + { + if self.slots[index].is_none() { + return Err(DrawError::MissingVertexBuffer { + pipeline: pipeline.error_ident(), + index, + }); + } + } + + let bind_group_space_used = binder.last_assigned_index().map_or(0, |i| i + 1); + let vertex_buffer_space_used = self.last_assigned_index().map_or(0, |i| i + 1); + + let bind_groups_plus_vertex_buffers = + u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap(); + if bind_groups_plus_vertex_buffers + > pipeline.device.limits.max_bind_groups_plus_vertex_buffers + { + return Err(DrawError::TooManyBindGroupsPlusVertexBuffers { + given: bind_groups_plus_vertex_buffers, + limit: pipeline.device.limits.max_bind_groups_plus_vertex_buffers, + }); + } + + Ok(()) + } + /// Call `f` for each dirty slot with `(slot_index, buffer, offset, size)` and mark them clean. pub(crate) fn flush(&mut self, mut f: F) where @@ -617,34 +654,7 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { return Err(DrawError::MissingBlendConstant); } - // Check all needed vertex buffers have been bound - for index in pipeline - .vertex_steps - .iter() - .enumerate() - .filter_map(|(index, step)| step.map(|_| index)) - { - if self.vertex.slots[index].is_none() { - return Err(DrawError::MissingVertexBuffer { - pipeline: pipeline.error_ident(), - index, - }); - } - } - - let bind_group_space_used = self.pass.binder.last_assigned_index().map_or(0, |i| i + 1); - let vertex_buffer_space_used = self.vertex.last_assigned_index().map_or(0, |i| i + 1); - - let bind_groups_plus_vertex_buffers = - u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap(); - if bind_groups_plus_vertex_buffers - > pipeline.device.limits.max_bind_groups_plus_vertex_buffers - { - return Err(DrawError::TooManyBindGroupsPlusVertexBuffers { - given: bind_groups_plus_vertex_buffers, - limit: pipeline.device.limits.max_bind_groups_plus_vertex_buffers, - }); - } + self.vertex.validate(pipeline.as_ref(), &self.pass.binder)?; if family == DrawCommandFamily::DrawIndexed { // Pipeline expects an index buffer