From f5c503a3aaeaa24062bc15af90e9b0f002bf6912 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 23 Mar 2024 10:55:54 +0000 Subject: [PATCH] Add buffer extension trait --- src/gfx/buffer.rs | 26 ++++++++++++++++++++++++++ src/gfx/mod.rs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/gfx/buffer.rs b/src/gfx/buffer.rs index 1b4c73c..441f2e3 100644 --- a/src/gfx/buffer.rs +++ b/src/gfx/buffer.rs @@ -1,3 +1,5 @@ +use std::ops::RangeBounds; + use bytemuck::NoUninit; use wgpu::util::DeviceExt; @@ -71,3 +73,27 @@ impl<'a> BulkBufferBuilder<'a> { buffers } } + +pub trait BufferExt { + fn get_mapped_range, T: bytemuck::Pod>( + &self, + context: &Context, + bounds: S, + ) -> Vec; +} + +impl BufferExt for wgpu::Buffer { + fn get_mapped_range, T: bytemuck::Pod>( + &self, + context: &Context, + bounds: S, + ) -> Vec { + let slice = self.slice(bounds); + slice.map_async(wgpu::MapMode::Read, |_| {}); + context.device.poll(wgpu::Maintain::Wait); + let data: Vec = bytemuck::cast_slice(slice.get_mapped_range().as_ref()).to_vec(); + self.unmap(); + + data + } +} diff --git a/src/gfx/mod.rs b/src/gfx/mod.rs index 49baf37..f6507eb 100644 --- a/src/gfx/mod.rs +++ b/src/gfx/mod.rs @@ -6,7 +6,7 @@ mod texture; pub use self::{ bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, - buffer::BulkBufferBuilder, + buffer::{BufferExt, BulkBufferBuilder}, context::Context, renderer::Renderer, texture::{Texture, TextureBuilder},