Add bind group and bind group layout builders

This commit is contained in:
Jarrod Doyle 2023-04-21 12:16:59 +01:00
parent b73d9b8f2b
commit 20aeaa304d
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 98 additions and 0 deletions

View File

@ -1,7 +1,9 @@
mod bind_group;
mod context; mod context;
mod texture; mod texture;
pub use self::{ pub use self::{
bind_group::{BindGroupBuilder, BindGroupLayoutBuilder},
context::Context, context::Context,
texture::{Texture, TextureBuilder}, texture::{Texture, TextureBuilder},
}; };

96
src/render/bind_group.rs Normal file
View File

@ -0,0 +1,96 @@
use std::num::NonZeroU32;
use super::Context;
#[derive(Debug, Default)]
pub struct BindGroupLayoutBuilder<'a> {
next_binding: u32,
entries: Vec<wgpu::BindGroupLayoutEntry>,
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<NonZeroU32>,
) -> 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<wgpu::BindGroupEntry<'a>>,
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(),
})
}
}