Skip to content

Commit

Permalink
Add conversions from BufferSlice to BufferBinding and `BindingRes…
Browse files Browse the repository at this point in the history
…ource`.
  • Loading branch information
kpreid committed Feb 15, 2025
1 parent cfe6ba8 commit b3fd880
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ By @cwfitzgerald in [#7030](https://github.com/gfx-rs/wgpu/pull/7030).
- Add `Buffer` methods corresponding to `BufferSlice` methods, so you can skip creating a `BufferSlice` when it offers no benefit, and `BufferSlice::slice()` for sub-slicing a slice. By @kpreid in [#7123](https://github.com/gfx-rs/wgpu/pull/7123).
- Add `BufferSlice::buffer()`, `BufferSlice::offset()` and `BufferSlice::size()`.
- Add `impl From<BufferSlice> for BufferBinding` and `impl From<BufferSlice> for BindingResource`, allowing `BufferSlice`s to be easily used in creating bind groups.
- Add `util::StagingBelt::allocate()` so the staging belt can be used to write textures. By @kpreid in [#6900](https://github.com/gfx-rs/wgpu/pull/6900).
- Added `CommandEncoder::transition_resources()` for native API interop, and allowing users to slightly optimize barriers. By @JMS55 in [#6678](https://github.com/gfx-rs/wgpu/pull/6678).
Expand Down
18 changes: 18 additions & 0 deletions tests/validation_tests/api/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,22 @@ mod buffer_slice {
(&buffer, 10, NonZero::new(90).unwrap())
);
}

#[test]
fn into_buffer_binding() {
let (device, _queue) = crate::request_noop_device();
let buffer = device.create_buffer(ARBITRARY_DESC);

// BindingResource doesn’t implement PartialEq, so use matching
let wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: b,
offset: 50,
size: Some(size),
}) = wgpu::BindingResource::from(buffer.slice(50..80))
else {
panic!("didn't match")
};
assert_eq!(b, &buffer);
assert_eq!(size, NonZero::new(30).unwrap());
}
}
23 changes: 22 additions & 1 deletion wgpu/src/api/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ impl Buffer {
///
/// You can pass buffer slices to methods like [`RenderPass::set_vertex_buffer`]
/// and [`RenderPass::set_index_buffer`] to indicate which portion of the buffer
/// a draw call should consult.
/// a draw call should consult. You can also convert it to a [`BufferBinding`]
/// with `.into()`.
///
/// To access the slice's contents on the CPU, you must first [map] the buffer,
/// and then call [`BufferSlice::get_mapped_range`] or
Expand Down Expand Up @@ -530,6 +531,26 @@ impl<'a> BufferSlice<'a> {
}
}

impl<'a> From<BufferSlice<'a>> for crate::BufferBinding<'a> {
/// Convert a [`BufferSlice`] to an equivalent [`BufferBinding`],
/// provided that it will be used without a dynamic offset.
fn from(value: BufferSlice<'a>) -> Self {
BufferBinding {
buffer: value.buffer,
offset: value.offset,
size: value.size,
}
}
}

impl<'a> From<BufferSlice<'a>> for crate::BindingResource<'a> {
/// Convert a [`BufferSlice`] to an equivalent [`BindingResource::Buffer`],
/// provided that it will be used without a dynamic offset.
fn from(value: BufferSlice<'a>) -> Self {
crate::BindingResource::Buffer(crate::BufferBinding::from(value))
}
}

/// The mapped portion of a buffer, if any, and its outstanding views.
///
/// This ensures that views fall within the mapped range and don't overlap, and
Expand Down

0 comments on commit b3fd880

Please sign in to comment.