Skip to content

Commit f6e06c8

Browse files
committed
deduplicate vertex buffer validation in is_ready
1 parent 953f1f9 commit f6e06c8

2 files changed

Lines changed: 41 additions & 58 deletions

File tree

wgpu-core/src/command/bundle.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,34 +1454,7 @@ impl State {
14541454
self.binder.check_compatibility(pipeline.as_ref())?;
14551455
self.binder.check_late_buffer_bindings()?;
14561456

1457-
// Check all needed vertex buffers have been bound
1458-
for index in pipeline
1459-
.vertex_steps
1460-
.iter()
1461-
.enumerate()
1462-
.filter_map(|(index, step)| step.map(|_| index))
1463-
{
1464-
if self.vertex.slots[index].is_none() {
1465-
return Err(DrawError::MissingVertexBuffer {
1466-
pipeline: pipeline.error_ident(),
1467-
index,
1468-
});
1469-
}
1470-
}
1471-
1472-
let bind_group_space_used = self.binder.last_assigned_index().map_or(0, |i| i + 1);
1473-
let vertex_buffer_space_used = self.vertex.last_assigned_index().map_or(0, |i| i + 1);
1474-
1475-
let bind_groups_plus_vertex_buffers =
1476-
u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap();
1477-
if bind_groups_plus_vertex_buffers
1478-
> self.device.limits.max_bind_groups_plus_vertex_buffers
1479-
{
1480-
return Err(DrawError::TooManyBindGroupsPlusVertexBuffers {
1481-
given: bind_groups_plus_vertex_buffers,
1482-
limit: self.device.limits.max_bind_groups_plus_vertex_buffers,
1483-
});
1484-
}
1457+
self.vertex.validate(&*pipeline, &self.binder)?;
14851458

14861459
if family == DrawCommandFamily::DrawIndexed {
14871460
let index_format = match &self.index {

wgpu-core/src/command/render.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ pub(crate) struct VertexSlot {
523523
/// derived from those buffers and the current pipeline, avoiding recomputation on each draw.
524524
#[derive(Debug, Default)]
525525
pub(crate) struct VertexState {
526-
pub(crate) slots: [Option<VertexSlot>; hal::MAX_VERTEX_BUFFERS],
526+
slots: [Option<VertexSlot>; hal::MAX_VERTEX_BUFFERS],
527527
pub(crate) limits: VertexLimits,
528528
}
529529

@@ -557,14 +557,51 @@ impl VertexState {
557557
);
558558
}
559559

560-
pub(crate) fn last_assigned_index(&self) -> Option<usize> {
560+
fn last_assigned_index(&self) -> Option<usize> {
561561
self.slots
562562
.iter()
563563
.enumerate()
564564
.filter_map(|(i, s)| s.as_ref().map(|_| i))
565565
.next_back()
566566
}
567567

568+
pub(super) fn validate(
569+
&self,
570+
pipeline: &RenderPipeline,
571+
binder: &Binder,
572+
) -> Result<(), DrawError> {
573+
// Check all needed vertex buffers have been bound
574+
for index in pipeline
575+
.vertex_steps
576+
.iter()
577+
.enumerate()
578+
.filter_map(|(index, step)| step.map(|_| index))
579+
{
580+
if self.slots[index].is_none() {
581+
return Err(DrawError::MissingVertexBuffer {
582+
pipeline: pipeline.error_ident(),
583+
index,
584+
});
585+
}
586+
}
587+
588+
let bind_group_space_used = binder.last_assigned_index().map_or(0, |i| i + 1);
589+
let vertex_buffer_space_used = self.last_assigned_index().map_or(0, |i| i + 1);
590+
591+
let bind_groups_plus_vertex_buffers =
592+
u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap();
593+
if bind_groups_plus_vertex_buffers
594+
> pipeline.device.limits.max_bind_groups_plus_vertex_buffers
595+
{
596+
return Err(DrawError::TooManyBindGroupsPlusVertexBuffers {
597+
given: bind_groups_plus_vertex_buffers,
598+
limit: pipeline.device.limits.max_bind_groups_plus_vertex_buffers,
599+
});
600+
}
601+
602+
Ok(())
603+
}
604+
568605
/// Call `f` for each dirty slot with `(slot_index, buffer, offset, size)` and mark them clean.
569606
pub(crate) fn flush<F>(&mut self, mut f: F)
570607
where
@@ -617,34 +654,7 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> {
617654
return Err(DrawError::MissingBlendConstant);
618655
}
619656

620-
// Check all needed vertex buffers have been bound
621-
for index in pipeline
622-
.vertex_steps
623-
.iter()
624-
.enumerate()
625-
.filter_map(|(index, step)| step.map(|_| index))
626-
{
627-
if self.vertex.slots[index].is_none() {
628-
return Err(DrawError::MissingVertexBuffer {
629-
pipeline: pipeline.error_ident(),
630-
index,
631-
});
632-
}
633-
}
634-
635-
let bind_group_space_used = self.pass.binder.last_assigned_index().map_or(0, |i| i + 1);
636-
let vertex_buffer_space_used = self.vertex.last_assigned_index().map_or(0, |i| i + 1);
637-
638-
let bind_groups_plus_vertex_buffers =
639-
u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap();
640-
if bind_groups_plus_vertex_buffers
641-
> pipeline.device.limits.max_bind_groups_plus_vertex_buffers
642-
{
643-
return Err(DrawError::TooManyBindGroupsPlusVertexBuffers {
644-
given: bind_groups_plus_vertex_buffers,
645-
limit: pipeline.device.limits.max_bind_groups_plus_vertex_buffers,
646-
});
647-
}
657+
self.vertex.validate(&*pipeline, &self.pass.binder)?;
648658

649659
if family == DrawCommandFamily::DrawIndexed {
650660
// Pipeline expects an index buffer

0 commit comments

Comments
 (0)