diff --git a/src/gfx.rs b/src/gfx.rs index bc9fb61..49baf37 100644 --- a/src/gfx.rs +++ b/src/gfx.rs @@ -1,10 +1,12 @@ mod bind_group; +mod buffer; mod context; mod renderer; mod texture; pub use self::{ bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, + buffer::BulkBufferBuilder, context::Context, renderer::Renderer, texture::{Texture, TextureBuilder}, diff --git a/src/gfx/buffer.rs b/src/gfx/buffer.rs new file mode 100644 index 0000000..984dc05 --- /dev/null +++ b/src/gfx/buffer.rs @@ -0,0 +1,47 @@ +use bytemuck::NoUninit; +use wgpu::util::DeviceExt; + +use super::Context; + +#[derive(Debug)] +pub struct BulkBufferBuilder<'a> { + descriptors: Vec>, + 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(mut self, label: &'a str, contents: &'a [A]) -> Self { + self.with_buffer(label, bytemuck::cast_slice(contents)) + } + + pub fn build(self, context: &Context) -> Vec { + let mut buffers = vec![]; + for descriptor in self.descriptors { + buffers.push(context.device.create_buffer_init(&descriptor)); + } + buffers + } +}