[deno] Implement nullable vertex buffer layouts

This commit is contained in:
Andy Leiserson 2025-07-09 16:28:51 -07:00 committed by Jim Blandy
parent 27e7408f16
commit 2db1d71f1b
2 changed files with 30 additions and 22 deletions

View File

@ -680,29 +680,26 @@ impl GPUDevice {
.buffers
.into_iter()
.map(|b| {
let layout = b.into_option().ok_or_else(|| {
JsErrorBox::type_error(
"Nullable GPUVertexBufferLayouts are currently not supported",
)
})?;
Ok(wgpu_core::pipeline::VertexBufferLayout {
array_stride: layout.array_stride,
step_mode: layout.step_mode.into(),
attributes: Cow::Owned(
layout
.attributes
.into_iter()
.map(|attr| wgpu_types::VertexAttribute {
format: attr.format.into(),
offset: attr.offset,
shader_location: attr.shader_location,
})
.collect(),
),
})
b.into_option().map_or_else(
wgpu_core::pipeline::VertexBufferLayout::default,
|layout| wgpu_core::pipeline::VertexBufferLayout {
array_stride: layout.array_stride,
step_mode: layout.step_mode.into(),
attributes: Cow::Owned(
layout
.attributes
.into_iter()
.map(|attr| wgpu_types::VertexAttribute {
format: attr.format.into(),
offset: attr.offset,
shader_location: attr.shader_location,
})
.collect(),
),
},
)
})
.collect::<Result<_, JsErrorBox>>()?,
.collect(),
),
};

View File

@ -371,6 +371,17 @@ pub struct VertexBufferLayout<'a> {
pub attributes: Cow<'a, [wgt::VertexAttribute]>,
}
/// A null vertex buffer layout that may be placed in unused slots.
impl Default for VertexBufferLayout<'_> {
fn default() -> Self {
Self {
array_stride: Default::default(),
step_mode: Default::default(),
attributes: Cow::Borrowed(&[]),
}
}
}
/// Describes the vertex process in a render pipeline.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]