Skip to content

Commit 780209d

Browse files
authored
Add Buffer::size() and Buffer::usage(). (#2923)
1 parent 29f5f8f commit 780209d

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ the same every time it is rendered, we now warn if it is missing.
5656
+fn vert_main(v_in: VertexInput) -> @builtin(position) @invariant vec4<f32> {...}
5757
```
5858

59+
### Added/New Features
60+
61+
- Add `Buffer::size()` and `Buffer::usage()`; by @kpreid in [#2923](https://github.com/gfx-rs/wgpu/pull/2923)
62+
5963
### Bug Fixes
6064

6165
#### General

wgpu/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ pub struct Buffer {
659659
context: Arc<C>,
660660
id: <C as Context>::BufferId,
661661
map_context: Mutex<MapContext>,
662+
size: wgt::BufferAddress,
662663
usage: BufferUsages,
663664
}
664665

@@ -2129,6 +2130,7 @@ impl Device {
21292130
context: Arc::clone(&self.context),
21302131
id: Context::device_create_buffer(&*self.context, &self.id, desc),
21312132
map_context: Mutex::new(map_context),
2133+
size: desc.size,
21322134
usage: desc.usage,
21332135
}
21342136
}
@@ -2426,6 +2428,20 @@ impl Buffer {
24262428
pub fn destroy(&self) {
24272429
Context::buffer_destroy(&*self.context, &self.id);
24282430
}
2431+
2432+
/// Returns the length of the buffer allocation in bytes.
2433+
///
2434+
/// This is always equal to the `size` that was specified when creating the buffer.
2435+
pub fn size(&self) -> wgt::BufferAddress {
2436+
self.size
2437+
}
2438+
2439+
/// Returns the allowed usages for this `Buffer`.
2440+
///
2441+
/// This is always equal to the `usage` that was specified when creating the buffer.
2442+
pub fn usage(&self) -> BufferUsages {
2443+
self.usage
2444+
}
24292445
}
24302446

24312447
impl<'a> BufferSlice<'a> {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::common::{initialize_test, TestParameters};
2+
3+
/// Buffer's size and usage can be read back.
4+
#[test]
5+
fn buffer_size_and_usage() {
6+
initialize_test(TestParameters::default(), |ctx| {
7+
let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
8+
label: None,
9+
size: 1234,
10+
usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
11+
mapped_at_creation: false,
12+
});
13+
14+
assert_eq!(buffer.size(), 1234);
15+
assert_eq!(
16+
buffer.usage(),
17+
wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST
18+
);
19+
})
20+
}

wgpu/tests/root.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod device;
66
mod example_wgsl;
77
mod instance;
88
mod poll;
9+
mod resource_descriptor_accessor;
910
mod shader_primitive_index;
1011
mod vertex_indices;
1112
mod zero_init_texture_after_discard;

0 commit comments

Comments
 (0)