gloss_renderer/forward_renderer/render_passes/
point_pipeline.rs1use std::collections::HashMap;
2
3use crate::{
4 components::{ColorsGPU, ModelMatrix, Name, Renderable, VertsGPU, VisPoints},
5 config::RenderConfig,
6 forward_renderer::{bind_group_collection::BindGroupCollection, locals::LocalEntData},
7 scene::Scene,
8};
9
10use easy_wgpu::{
11 bind_group::{BindGroupBuilder, BindGroupWrapper},
12 bind_group_layout::{BindGroupLayoutBuilder, BindGroupLayoutDesc},
13 buffer::Buffer,
14 gpu::Gpu,
15 pipeline::RenderPipelineDescBuilder,
16 utils::create_empty_group,
17};
18
19use gloss_hecs::Entity;
20
21use super::{pipeline_runner::PipelineRunner, upload_pass::PerFrameUniforms};
22
23use encase;
24use gloss_utils::numerical::align;
25
26#[include_wgsl_oil::include_wgsl_oil("../../../shaders/gbuffer_point_instanced.wgsl")]
28mod shader_code {}
29
30pub struct PointPipeline {
32 render_pipeline: wgpu::RenderPipeline,
33 _empty_group: wgpu::BindGroup,
34 locals_uniform: Buffer, locals_bind_groups: LocalsBindGroups,
36}
37
38impl PointPipeline {
39 pub fn new(gpu: &Gpu, params: &RenderConfig, color_target_format: wgpu::TextureFormat, depth_target_format: wgpu::TextureFormat) -> Self {
43 const_assert!(std::mem::size_of::<Locals>() % 16 == 0);
45
46 let render_pipeline = RenderPipelineDescBuilder::new()
48 .label("point_pipeline")
49 .shader_code(shader_code::SOURCE)
50 .shader_label("point_shader")
51 .add_bind_group_layout_desc(PerFrameUniforms::build_layout_desc())
52 .add_bind_group_layout_desc(LocalsBindGroups::build_layout_desc())
54 .add_vertex_buffer_layout(VertsGPU::vertex_buffer_layout_instanced::<0>())
55 .add_vertex_buffer_layout(ColorsGPU::vertex_buffer_layout_instanced::<1>())
56 .add_render_target(wgpu::ColorTargetState {
57 format: color_target_format,
58 blend: None,
59 write_mask: wgpu::ColorWrites::ALL,
60 })
61 .depth_state(Some(wgpu::DepthStencilState {
62 format: depth_target_format,
63 depth_write_enabled: true,
64 depth_compare: wgpu::CompareFunction::Greater,
65 stencil: wgpu::StencilState::default(),
66 bias: wgpu::DepthBiasState::default(),
67 }))
68 .multisample(wgpu::MultisampleState {
69 count: params.msaa_nr_samples,
70 ..Default::default()
71 })
72 .build_pipeline(gpu.device());
73
74 let empty_group = create_empty_group(gpu.device());
75
76 let size_bytes = 0x10000;
77 let usage = wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM;
78 let locals_uniform = Buffer::new_empty(gpu.device(), usage, Some("local_buffer"), size_bytes);
79
80 let locals_bind_groups = LocalsBindGroups::new(gpu);
81
82 Self {
83 render_pipeline,
84 _empty_group: empty_group,
85 locals_uniform,
86 locals_bind_groups,
87 }
88 }
89}
90
91impl PipelineRunner for PointPipeline {
92 type QueryItems<'a> = (&'a VertsGPU, &'a ColorsGPU, &'a VisPoints, &'a Name);
93 type QueryState<'a> = gloss_hecs::QueryBorrow<'a, gloss_hecs::With<Self::QueryItems<'a>, &'a Renderable>>;
94
95 fn query_state(scene: &Scene) -> Self::QueryState<'_> {
96 scene.world.query::<Self::QueryItems<'_>>().with::<&Renderable>()
97 }
98
99 fn prepare<'a>(&mut self, gpu: &Gpu, _per_frame_uniforms: &PerFrameUniforms, scene: &'a Scene) -> Self::QueryState<'a> {
100 self.begin_pass();
101 self.update_locals(gpu, scene);
102 Self::query_state(scene)
103 }
104
105 fn run<'r>(
106 &'r mut self,
107 render_pass: &mut wgpu::RenderPass<'r>,
108 per_frame_uniforms: &'r PerFrameUniforms,
109 _render_params: &RenderConfig,
110 query_state: &'r mut Self::QueryState<'_>,
111 _scene: &Scene,
112 ) {
113 if query_state.iter().count() == 0 {
115 return;
116 }
117
118 render_pass.set_pipeline(&self.render_pipeline);
119
120 render_pass.set_bind_group(0, &per_frame_uniforms.bind_group, &[]);
122 for (_id, (verts, colors, vis_points, name)) in query_state.iter() {
126 if !vis_points.show_points {
127 continue;
128 }
129
130 let (local_bg, offset) = &self.locals_bind_groups.mesh2local_bind[&name.0.clone()];
132 render_pass.set_bind_group(1, local_bg.bg(), &[*offset]);
133 render_pass.set_vertex_buffer(0, verts.buf.slice(..));
134 render_pass.set_vertex_buffer(1, colors.buf.slice(..));
135 render_pass.draw(0..6, 0..verts.nr_vertices);
136 }
137 }
138
139 fn begin_pass(&mut self) {}
140
141 fn update_locals(&mut self, gpu: &Gpu, scene: &Scene) {
142 Self::update_locals_inner::<Locals, _>(
143 gpu,
144 scene,
145 &mut self.locals_uniform,
146 &mut self.locals_bind_groups,
147 &mut Self::query_state(scene),
148 );
149 }
150}
151
152#[repr(C)]
154#[derive(Clone, Copy, encase::ShaderType)]
155struct Locals {
156 model_matrix: nalgebra::Matrix4<f32>,
157 color_type: i32,
158 point_color: nalgebra::Vector4<f32>,
159 point_size: f32,
160 is_point_size_in_world_space: u32,
161 zbuffer: u32,
162 }
167impl LocalEntData for Locals {
168 fn new(entity: Entity, scene: &Scene) -> Self {
169 let model_matrix = scene.get_comp::<&ModelMatrix>(&entity).unwrap().0.to_homogeneous();
170 let vis_points = scene.get_comp::<&VisPoints>(&entity).unwrap();
171 let color_type = vis_points.color_type as i32;
172 Locals {
173 model_matrix,
174 color_type,
175 point_color: vis_points.point_color,
176 point_size: vis_points.point_size,
177 is_point_size_in_world_space: u32::from(vis_points.is_point_size_in_world_space),
178 zbuffer: u32::from(vis_points.zbuffer),
179 }
181 }
182}
183
184struct LocalsBindGroups {
185 layout: wgpu::BindGroupLayout,
186 pub mesh2local_bind: HashMap<String, (BindGroupWrapper, u32)>,
187}
188impl BindGroupCollection for LocalsBindGroups {
189 fn new(gpu: &Gpu) -> Self {
190 Self {
191 layout: Self::build_layout_desc().into_bind_group_layout(gpu.device()),
192 mesh2local_bind: HashMap::default(),
193 }
194 }
195
196 fn build_layout_desc() -> BindGroupLayoutDesc {
197 BindGroupLayoutBuilder::new()
198 .label("gbuffer_pass_locals_layout")
199 .add_entry_uniform(
201 wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
202 true,
203 wgpu::BufferSize::new(u64::from(align(u32::try_from(std::mem::size_of::<Locals>()).unwrap(), 256))),
204 )
205 .build()
206 }
207
208 fn update_bind_group(&mut self, _entity: Entity, gpu: &Gpu, mesh_name: &str, ubo: &Buffer, offset_in_ubo: u32, _scene: &Scene) {
209 let entries = BindGroupBuilder::new().add_entry_buf_chunk::<Locals>(&ubo.buffer).build_entries();
210
211 self.update_if_stale(mesh_name, entries, offset_in_ubo, gpu);
212 }
213 fn get_layout(&self) -> &wgpu::BindGroupLayout {
214 &self.layout
215 }
216 fn get_mut_entity2binds(&mut self) -> &mut HashMap<String, (BindGroupWrapper, u32)> {
217 &mut self.mesh2local_bind
218 }
219}