Add Buffer::size() and Buffer::usage(). (#2923)

This commit is contained in:
Kevin Reid 2022-08-02 14:10:49 -07:00 committed by GitHub
parent 29f5f8f60e
commit 780209dfb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 0 deletions

View File

@ -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<f32> {...} +fn vert_main(v_in: VertexInput) -> @builtin(position) @invariant vec4<f32> {...}
``` ```
### Added/New Features
- Add `Buffer::size()` and `Buffer::usage()`; by @kpreid in [#2923](https://github.com/gfx-rs/wgpu/pull/2923)
### Bug Fixes ### Bug Fixes
#### General #### General

View File

@ -659,6 +659,7 @@ pub struct Buffer {
context: Arc<C>, context: Arc<C>,
id: <C as Context>::BufferId, id: <C as Context>::BufferId,
map_context: Mutex<MapContext>, map_context: Mutex<MapContext>,
size: wgt::BufferAddress,
usage: BufferUsages, usage: BufferUsages,
} }
@ -2129,6 +2130,7 @@ impl Device {
context: Arc::clone(&self.context), context: Arc::clone(&self.context),
id: Context::device_create_buffer(&*self.context, &self.id, desc), id: Context::device_create_buffer(&*self.context, &self.id, desc),
map_context: Mutex::new(map_context), map_context: Mutex::new(map_context),
size: desc.size,
usage: desc.usage, usage: desc.usage,
} }
} }
@ -2426,6 +2428,20 @@ impl Buffer {
pub fn destroy(&self) { pub fn destroy(&self) {
Context::buffer_destroy(&*self.context, &self.id); 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> { impl<'a> BufferSlice<'a> {

View File

@ -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
);
})
}

View File

@ -6,6 +6,7 @@ mod device;
mod example_wgsl; mod example_wgsl;
mod instance; mod instance;
mod poll; mod poll;
mod resource_descriptor_accessor;
mod shader_primitive_index; mod shader_primitive_index;
mod vertex_indices; mod vertex_indices;
mod zero_init_texture_after_discard; mod zero_init_texture_after_discard;