Improve BindGroupBuilder
-use std::sync::Arc;
-use wgpu::{BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindingResource, Device};
+use crate::{cache::BindGroupCache, util::typedefs::SsoString};
+use std::{num::NonZeroU32, sync::Arc};
+use wgpu::{BindGroup, BindGroupEntry, BindGroupLayoutEntry, BindingResource, BindingType, Device, ShaderStage};
pub struct BindGroupBuilder<'a> {
- bindings: Vec<BindGroupEntry<'a>>,
- label: Option<String>,
+ label: Option<SsoString>,
+ bgl_entries: Vec<BindGroupLayoutEntry>,
+ bg_entries: Vec<BindGroupEntry<'a>>,
}
impl<'a> BindGroupBuilder<'a> {
- pub fn new(label: Option<String>) -> Self {
+ pub fn new<L>(label: Option<L>) -> Self
+ where
+ SsoString: From<L>,
+ {
Self {
- label,
- bindings: Vec::with_capacity(16),
+ label: label.map(SsoString::from),
+ bgl_entries: Vec::with_capacity(16),
+ bg_entries: Vec::with_capacity(16),
}
}
- pub fn append(&mut self, resource: BindingResource<'a>) {
- let index = self.bindings.len();
- self.bindings.push(BindGroupEntry {
+ pub fn append(
+ &mut self,
+ visibility: ShaderStage,
+ ty: BindingType,
+ count: Option<NonZeroU32>,
+ resource: BindingResource<'a>,
+ ) {
+ let index = self.bgl_entries.len();
+ self.bgl_entries.push(BindGroupLayoutEntry {
+ binding: index as u32,
+ visibility,
+ ty,
+ count,
+ });
+ self.bg_entries.push(BindGroupEntry {
binding: index as u32,
resource,
});
}
- pub fn build(self, device: &Device, layout: &BindGroupLayout) -> Arc<BindGroup> {
- let bind_group = device.create_bind_group(&BindGroupDescriptor {
- label: self.label.as_deref(),
- layout,
- entries: &self.bindings,
- });
-
- Arc::new(bind_group)
+ pub fn build(self, device: &Device, cache: BindGroupCache) -> Arc<BindGroup> {
+ cache.bind_group(device, self.label.as_deref(), &self.bgl_entries, &self.bg_entries)
}
}
