Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
kpreid marked this conversation as resolved.
Show resolved Hide resolved
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;