gloss_renderer/forward_renderer/render_passes/
pipeline_runner.rs

1use easy_wgpu::{
2    bind_group_layout::{BindGroupLayoutBuilder, BindGroupLayoutDesc},
3    buffer::Buffer,
4    gpu::Gpu,
5};
6use gloss_hecs::Query;
7
8use crate::{
9    components::Name,
10    config::RenderConfig,
11    forward_renderer::{bind_group_collection::BindGroupCollection, locals::LocalEntData},
12    scene::Scene,
13};
14
15use super::upload_pass::PerFrameUniforms;
16
17pub trait PipelineRunner {
18    type QueryItems<'r>: Query;
19    type QueryState<'r>;
20
21    fn query_state(scene: &Scene) -> Self::QueryState<'_>;
22    fn prepare<'a>(&mut self, gpu: &Gpu, per_frame_uniforms: &PerFrameUniforms, scene: &'a Scene) -> Self::QueryState<'a>;
23    fn run<'r>(
24        &'r mut self,
25        render_pass: &mut wgpu::RenderPass<'r>,
26        per_frame_uniforms: &'r PerFrameUniforms,
27        _render_params: &RenderConfig,
28        query_state: &'r mut Self::QueryState<'_>,
29        _scene: &Scene,
30    );
31    fn begin_pass(&mut self);
32    fn input_layout_desc() -> BindGroupLayoutDesc {
33        BindGroupLayoutBuilder::new().label("empty_layout").build()
34    }
35    //is optional if there is no input to this pipeline so by default can be empty
36    fn update_input_bind_group(&mut self, _gpu: &Gpu, _scene: &Scene, _per_frame_uniforms: &PerFrameUniforms) {}
37    fn update_locals(&mut self, gpu: &Gpu, scene: &Scene);
38
39    fn update_locals_inner<Locals: LocalEntData + encase::ShaderType + encase::internal::WriteInto, Q: Query>(
40        gpu: &Gpu,
41        scene: &Scene,
42        locals_uniform: &mut Buffer,
43        locals_bind_groups: &mut impl BindGroupCollection,
44        query_state: &mut gloss_hecs::QueryBorrow<'_, Q>,
45    ) {
46        locals_uniform.reset_chunks_offset_if_necessary();
47        // Update the local binding groups for the meshes we render. We do it in two
48        // passes because the binding group cannot be created and consumed in the same
49        // loop
50        for (id, _comps) in query_state.iter() {
51            let name = scene.get_comp::<&Name>(&id).unwrap().0.clone();
52
53            // upload local stuff
54            let locals = Locals::new(id, scene);
55            let offset_in_ubo = locals_uniform.push_cpu_chunk_aligned::<Locals>(&locals);
56
57            // chekc if we need to recreate bind group (for example when any of the textures
58            // of the meshes have changed)
59            locals_bind_groups.update_bind_group(id, gpu, &name, locals_uniform, offset_in_ubo, scene);
60        }
61        locals_uniform.upload_from_cpu_chunks(gpu.queue()); //important to
62                                                            // upload everything
63                                                            // to gpu at the end
64    }
65}