gloss_renderer/forward_renderer/render_platform/
bind_group_collection.rs

1use std::collections::HashMap;
2
3use easy_wgpu::{
4    bind_group::{BindGroupDesc, BindGroupWrapper},
5    bind_group_layout::BindGroupLayoutDesc,
6    buffer::Buffer,
7    gpu::Gpu,
8};
9use gloss_hecs::Entity;
10use smallvec::SmallVec;
11use wgpu::BindGroupLayout;
12
13use crate::scene::Scene;
14
15/// trait that defines a collection of uniforms each with the same layout. Each
16/// entity can be associated with a certain
17pub trait BindGroupCollection {
18    fn new(gpu: &Gpu) -> Self;
19    fn build_layout_desc() -> BindGroupLayoutDesc;
20
21    fn update_if_stale(&mut self, ent_name: &str, entries: SmallVec<[wgpu::BindGroupEntry<'_>; 16]>, offset_in_ubo: u32, gpu: &Gpu) {
22        //if there is no entry for the bind group or if the current one is stale, we
23        // recreate it
24        if !self.get_mut_entity2binds().contains_key(ent_name) || self.get_mut_entity2binds()[ent_name].0.is_stale(&entries) {
25            let bg = BindGroupDesc::new("local_bg", entries).into_bind_group_wrapper(gpu.device(), self.get_layout());
26            let bg_and_offset = (bg, offset_in_ubo);
27            self.get_mut_entity2binds().insert(ent_name.to_string(), bg_and_offset);
28        }
29
30        //sometimes just the offset of the bind group changes so we also make sure to
31        // update this.
32        self.get_mut_entity2binds()
33            .entry(ent_name.to_string())
34            .and_modify(|r| r.1 = offset_in_ubo);
35    }
36    fn update_bind_group(&mut self, _entity: Entity, gpu: &Gpu, mesh_name: &str, ubo: &Buffer, offset_in_ubo: u32, _scene: &Scene);
37
38    //getters
39    fn get_layout(&self) -> &BindGroupLayout;
40    fn get_mut_entity2binds(&mut self) -> &mut HashMap<String, (BindGroupWrapper, u32)>;
41}