Add a bulk buffer building helper
This commit is contained in:
parent
1098c418f1
commit
017883e205
|
@ -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},
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue