Add a bulk buffer building helper

This commit is contained in:
Jarrod Doyle 2023-09-08 15:32:14 +01:00
parent 1098c418f1
commit 017883e205
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 49 additions and 0 deletions

View File

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

47
src/gfx/buffer.rs Normal file
View File

@ -0,0 +1,47 @@
use bytemuck::NoUninit;
use wgpu::util::DeviceExt;
use super::Context;
#[derive(Debug)]
pub struct BulkBufferBuilder<'a> {
descriptors: Vec<wgpu::util::BufferInitDescriptor<'a>>,
current_usage: wgpu::BufferUsages,
}
impl<'a> BulkBufferBuilder<'a> {
pub fn new() -> Self {
Self {
descriptors: vec![],
current_usage: wgpu::BufferUsages::UNIFORM,
}
}
pub fn with_usage(mut self, usage: wgpu::BufferUsages) -> Self {
self.current_usage = usage;
self
}
pub fn with_buffer(mut self, label: &'a str, contents: &'a [u8]) -> Self {
let descriptor = wgpu::util::BufferInitDescriptor {
label: Some(label),
contents,
usage: self.current_usage,
};
self.descriptors.push(descriptor);
self
}
pub fn with_bytemuck_buffer<A: NoUninit>(mut self, label: &'a str, contents: &'a [A]) -> Self {
self.with_buffer(label, bytemuck::cast_slice(contents))
}
pub fn build(self, context: &Context) -> Vec<wgpu::Buffer> {
let mut buffers = vec![];
for descriptor in self.descriptors {
buffers.push(context.device.create_buffer_init(&descriptor));
}
buffers
}
}