From 780209dfb461b7d14109596a78b8eae24af51db4 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Tue, 2 Aug 2022 14:10:49 -0700 Subject: [PATCH] Add `Buffer::size()` and `Buffer::usage()`. (#2923) --- CHANGELOG.md | 4 ++++ wgpu/src/lib.rs | 16 ++++++++++++++++ wgpu/tests/resource_descriptor_accessor.rs | 20 ++++++++++++++++++++ wgpu/tests/root.rs | 1 + 4 files changed, 41 insertions(+) create mode 100644 wgpu/tests/resource_descriptor_accessor.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index b5888e5a2..bb358915d 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 1e56f1572..d5fb4e067 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 000000000..f43a996a4 --- /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 0119a8fea..d034418dc 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;