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    );
30    fn begin_pass(&mut self);
31    fn input_layout_desc() -> BindGroupLayoutDesc {
32        BindGroupLayoutBuilder::new().label("empty_layout").build()
33    }
34    //is optional if there is no input to this pipeline so by default can be empty
35    fn update_input_bind_group(&mut self, _gpu: &Gpu, _scene: &Scene, _per_frame_uniforms: &PerFrameUniforms) {}
36    fn update_locals(&mut self, gpu: &Gpu, scene: &Scene);
37
38    fn update_locals_inner<Locals: LocalEntData + encase::ShaderType + encase::internal::WriteInto, Q: Query>(
39        gpu: &Gpu,
40        scene: &Scene,
41        locals_uniform: &mut Buffer,
42        locals_bind_groups: &mut impl BindGroupCollection,
43        query_state: &mut gloss_hecs::QueryBorrow<'_, Q>,
44    ) {
45        locals_uniform.reset_chunks_offset_if_necessary();
46        // Update the local binding groups for the meshes we render. We do it in two
47        // passes because the binding group cannot be created and consumed in the same
48        // loop
49        for (id, _comps) in query_state.iter() {
50            let name = scene.get_comp::<&Name>(&id).unwrap().0.clone();
51
52            // upload local stuff
53            let locals = Locals::new(id, scene);
54            let offset_in_ubo = locals_uniform.push_cpu_chunk_aligned::<Locals>(&locals);
55
56            // chekc if we need to recreate bind group (for example when any of the textures
57            // of the meshes have changed)
58            locals_bind_groups.update_bind_group(id, gpu, &name, locals_uniform, offset_in_ubo, scene);
59        }
60        locals_uniform.upload_from_cpu_chunks(gpu.queue()); //important to
61                                                            // upload everything
62                                                            // to gpu at the end
63    }
64}