Add buffer extension trait

This commit is contained in:
Jarrod Doyle 2024-03-23 10:55:54 +00:00
parent 132bc99540
commit f5c503a3aa
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 27 additions and 1 deletions

View File

@ -1,3 +1,5 @@
use std::ops::RangeBounds;
use bytemuck::NoUninit; use bytemuck::NoUninit;
use wgpu::util::DeviceExt; use wgpu::util::DeviceExt;
@ -71,3 +73,27 @@ impl<'a> BulkBufferBuilder<'a> {
buffers buffers
} }
} }
pub trait BufferExt {
fn get_mapped_range<S: RangeBounds<wgpu::BufferAddress>, T: bytemuck::Pod>(
&self,
context: &Context,
bounds: S,
) -> Vec<T>;
}
impl BufferExt for wgpu::Buffer {
fn get_mapped_range<S: RangeBounds<wgpu::BufferAddress>, T: bytemuck::Pod>(
&self,
context: &Context,
bounds: S,
) -> Vec<T> {
let slice = self.slice(bounds);
slice.map_async(wgpu::MapMode::Read, |_| {});
context.device.poll(wgpu::Maintain::Wait);
let data: Vec<T> = bytemuck::cast_slice(slice.get_mapped_range().as_ref()).to_vec();
self.unmap();
data
}
}

View File

@ -6,7 +6,7 @@ mod texture;
pub use self::{ pub use self::{
bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, bind_group::{BindGroupBuilder, BindGroupLayoutBuilder},
buffer::BulkBufferBuilder, buffer::{BufferExt, BulkBufferBuilder},
context::Context, context::Context,
renderer::Renderer, renderer::Renderer,
texture::{Texture, TextureBuilder}, texture::{Texture, TextureBuilder},