From 20aeaa304dfb1d5ce8ea1664fd85545c8b165b29 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 21 Apr 2023 12:16:59 +0100 Subject: [PATCH] Add bind group and bind group layout builders --- src/render.rs | 2 + src/render/bind_group.rs | 96 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/render/bind_group.rs diff --git a/src/render.rs b/src/render.rs index bb348f5..c2152dc 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,7 +1,9 @@ +mod bind_group; mod context; mod texture; pub use self::{ + bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, context::Context, texture::{Texture, TextureBuilder}, }; diff --git a/src/render/bind_group.rs b/src/render/bind_group.rs new file mode 100644 index 0000000..5258419 --- /dev/null +++ b/src/render/bind_group.rs @@ -0,0 +1,96 @@ +use std::num::NonZeroU32; + +use super::Context; + +#[derive(Debug, Default)] +pub struct BindGroupLayoutBuilder<'a> { + next_binding: u32, + entries: Vec, + label: Option<&'a str>, +} + +impl<'a> BindGroupLayoutBuilder<'a> { + pub fn new() -> Self { + Self::default() + } + + #[inline] + pub fn with_label(mut self, label: &'a str) -> Self { + self.label = Some(label); + self + } + + #[inline] + pub fn with_entry( + mut self, + visibility: wgpu::ShaderStages, + ty: wgpu::BindingType, + count: Option, + ) -> Self { + self.entries.push(wgpu::BindGroupLayoutEntry { + binding: self.next_binding, + visibility, + ty, + count, + }); + self.next_binding += 1; + self + } + + #[inline] + pub fn build(self, context: &Context) -> wgpu::BindGroupLayout { + context + .device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: self.label, + entries: &self.entries, + }) + } +} + +#[derive(Debug, Default)] +pub struct BindGroupBuilder<'a> { + next_binding: u32, + label: Option<&'a str>, + entries: Vec>, + layout: Option<&'a wgpu::BindGroupLayout>, +} + +impl<'a> BindGroupBuilder<'a> { + pub fn new() -> Self { + Self::default() + } + + #[inline] + pub fn with_label(mut self, label: &'a str) -> Self { + self.label = Some(label); + self + } + + #[inline] + pub fn with_entry(mut self, resource: wgpu::BindingResource<'a>) -> Self { + self.entries.push(wgpu::BindGroupEntry { + binding: self.next_binding, + resource, + }); + self.next_binding += 1; + self + } + + #[inline] + pub fn with_layout(mut self, layout: &'a wgpu::BindGroupLayout) -> Self { + self.layout = Some(layout); + self + } + + #[inline] + pub fn build(self, context: &Context) -> wgpu::BindGroup { + context + .device + .create_bind_group(&wgpu::BindGroupDescriptor { + label: self.label, + layout: &self.layout.unwrap(), + entries: self.entries.as_slice(), + }) + } +}