diff --git a/CHANGELOG.md b/CHANGELOG.md index f91d440ad5..ccf5808009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ the same every time it is rendered, we now warn if it is missing. +fn vert_main(v_in: VertexInput) -> @builtin(position) @invariant vec4 {...} ``` +### Added/New Features + +- Add `Buffer::size()` and `Buffer::usage()`; by @kpreid in [#2923](https://github.com/gfx-rs/wgpu/pull/2923) + ### Bug Fixes #### General diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 1e56f1572f..d5fb4e0679 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -659,6 +659,7 @@ pub struct Buffer { context: Arc, id: ::BufferId, map_context: Mutex, + size: wgt::BufferAddress, usage: BufferUsages, } @@ -2129,6 +2130,7 @@ impl Device { context: Arc::clone(&self.context), id: Context::device_create_buffer(&*self.context, &self.id, desc), map_context: Mutex::new(map_context), + size: desc.size, usage: desc.usage, } } @@ -2426,6 +2428,20 @@ impl Buffer { pub fn destroy(&self) { Context::buffer_destroy(&*self.context, &self.id); } + + /// Returns the length of the buffer allocation in bytes. + /// + /// This is always equal to the `size` that was specified when creating the buffer. + pub fn size(&self) -> wgt::BufferAddress { + self.size + } + + /// Returns the allowed usages for this `Buffer`. + /// + /// This is always equal to the `usage` that was specified when creating the buffer. + pub fn usage(&self) -> BufferUsages { + self.usage + } } impl<'a> BufferSlice<'a> { diff --git a/wgpu/tests/resource_descriptor_accessor.rs b/wgpu/tests/resource_descriptor_accessor.rs new file mode 100644 index 0000000000..f43a996a40 --- /dev/null +++ b/wgpu/tests/resource_descriptor_accessor.rs @@ -0,0 +1,20 @@ +use crate::common::{initialize_test, TestParameters}; + +/// Buffer's size and usage can be read back. +#[test] +fn buffer_size_and_usage() { + initialize_test(TestParameters::default(), |ctx| { + let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: 1234, + usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + + assert_eq!(buffer.size(), 1234); + assert_eq!( + buffer.usage(), + wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST + ); + }) +} diff --git a/wgpu/tests/root.rs b/wgpu/tests/root.rs index 0119a8fea5..d034418dc3 100644 --- a/wgpu/tests/root.rs +++ b/wgpu/tests/root.rs @@ -6,6 +6,7 @@ mod device; mod example_wgsl; mod instance; mod poll; +mod resource_descriptor_accessor; mod shader_primitive_index; mod vertex_indices; mod zero_init_texture_after_discard;