Skip to content

Commit

Permalink
Add Buffer::size() and Buffer::usage(). (gfx-rs#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid authored Aug 2, 2022
1 parent 29f5f8f commit 780209d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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> {...}
```

### Added/New Features

- Add `Buffer::size()` and `Buffer::usage()`; by @kpreid in [#2923](https://github.com/gfx-rs/wgpu/pull/2923)

### Bug Fixes

#### General
Expand Down
16 changes: 16 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ pub struct Buffer {
context: Arc<C>,
id: <C as Context>::BufferId,
map_context: Mutex<MapContext>,
size: wgt::BufferAddress,
usage: BufferUsages,
}

Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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> {
Expand Down
20 changes: 20 additions & 0 deletions wgpu/tests/resource_descriptor_accessor.rs
Original file line number Diff line number Diff line change
@@ -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
);
})
}
1 change: 1 addition & 0 deletions wgpu/tests/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 780209d

Please sign in to comment.