1#[cfg(not(feature = "qualia"))]
4use super::vegetation_effects;
5use super::WgpuRenderer;
6
7#[repr(C)]
9#[derive(bytemuck::Pod, bytemuck::Zeroable, Clone, Copy)]
10pub(super) struct ScreenVertex {
11 pub(super) position: [f32; 2],
12 pub(super) color: [f32; 4],
13}
14
15#[repr(C)]
17#[derive(bytemuck::Pod, bytemuck::Zeroable, Clone, Copy)]
18pub(super) struct ProjectorVertex {
19 pub(super) position: [f32; 3],
20 pub(super) color: [f32; 4],
21}
22
23#[derive(Debug, Clone, Copy)]
25pub struct AmbientConfig {
26 pub enabled: bool,
28 pub particle_count: usize,
30 pub intensity: f32,
32}
33
34impl Default for AmbientConfig {
35 fn default() -> Self {
36 Self {
37 enabled: true,
38 particle_count: 50000,
39 intensity: 1.0,
40 }
41 }
42}
43
44#[cfg(not(feature = "qualia"))]
45type ParticleInstance = vegetation_effects::GpuParticle;
46
47#[repr(C)]
49#[derive(bytemuck::Pod, bytemuck::Zeroable, Clone, Copy)]
50pub(super) struct AmbientUniforms {
51 pub(super) time: f32,
52 pub(super) view_width: f32,
53 pub(super) view_height: f32,
54 pub(super) world_frame: u32,
55}
56
57impl<'a> WgpuRenderer<'a> {
58 #[cfg(not(feature = "qualia"))]
59 pub(super) fn ambient_fields(
60 device: &etalon::Device,
61 width: u32,
62 height: u32,
63 ) -> (
64 AmbientConfig,
65 Option<etalon::RenderPipeline>,
66 Option<etalon::Buffer>,
67 Option<etalon::Buffer>,
68 Option<etalon::Buffer>,
69 usize,
70 vegetation_effects::EffectPool,
71 ) {
72 let ambient_config = AmbientConfig::default();
73 let (
74 ambient_pipeline,
75 particle_buffer,
76 ambient_uniform_buffer,
77 telemetry_buffer,
78 particle_count,
79 ambient_effects,
80 ) = Self::init_ambient_visualization(device, &ambient_config, width, height);
81 (
82 ambient_config,
83 ambient_pipeline,
84 particle_buffer,
85 ambient_uniform_buffer,
86 telemetry_buffer,
87 particle_count,
88 ambient_effects,
89 )
90 }
91
92 pub(super) fn create_vertex_buffer(
93 device: &etalon::Device,
94 ) -> (etalon::Buffer, etalon::Buffer, usize) {
95 let max_vertices = 10000;
96 let vertex_buffer = device.create_buffer(&etalon::BufferDescriptor {
97 label: Some("webizen-vertex-buffer"),
98 size: (max_vertices * std::mem::size_of::<ScreenVertex>()) as u64,
99 usage: etalon::BufferUsages::VERTEX | etalon::BufferUsages::COPY_DST,
100 mapped_at_creation: false,
101 });
102 let projector_vertex_buffer = device.create_buffer(&etalon::BufferDescriptor {
103 label: Some("webizen-projector-vertex-buffer"),
104 size: (max_vertices * std::mem::size_of::<ProjectorVertex>()) as u64,
105 usage: etalon::BufferUsages::VERTEX | etalon::BufferUsages::COPY_DST,
106 mapped_at_creation: false,
107 });
108 (vertex_buffer, projector_vertex_buffer, max_vertices)
109 }
110
111 pub(super) fn create_projector_pipeline(
112 device: &etalon::Device,
113 target_format: etalon::TextureFormat,
114 ) -> etalon::RenderPipeline {
115 let shader = device.create_shader_module(etalon::ShaderModuleDescriptor {
116 label: Some("webizen-projector-shader"),
117 source: etalon::ShaderSource::Wgsl(crate::shaders::PROJECTOR_WGSL.into()),
118 });
119
120 let bind_group_layout =
121 device.create_bind_group_layout(&etalon::BindGroupLayoutDescriptor {
122 label: Some("webizen-projector-bgl"),
123 entries: &[
124 etalon::BindGroupLayoutEntry {
125 binding: 0,
126 visibility: etalon::ShaderStages::VERTEX,
127 ty: etalon::BindingType::Buffer {
128 ty: etalon::BufferBindingType::Uniform,
129 has_dynamic_offset: false,
130 min_binding_size: None,
131 },
132 count: None,
133 },
134 etalon::BindGroupLayoutEntry {
135 binding: 1,
136 visibility: etalon::ShaderStages::VERTEX,
137 ty: etalon::BindingType::Buffer {
138 ty: etalon::BufferBindingType::Storage { read_only: true },
139 has_dynamic_offset: false,
140 min_binding_size: None,
141 },
142 count: None,
143 },
144 etalon::BindGroupLayoutEntry {
145 binding: 2,
146 visibility: etalon::ShaderStages::VERTEX,
147 ty: etalon::BindingType::Buffer {
148 ty: etalon::BufferBindingType::Uniform,
149 has_dynamic_offset: false,
150 min_binding_size: None,
151 },
152 count: None,
153 },
154 ],
155 });
156
157 let pipeline_layout = device.create_pipeline_layout(&etalon::PipelineLayoutDescriptor {
158 label: Some("webizen-projector-pipeline-layout"),
159 bind_group_layouts: &[Some(&bind_group_layout)],
160 immediate_size: 0,
161 });
162
163 device.create_render_pipeline(&etalon::RenderPipelineDescriptor {
164 label: Some("webizen-projector-pipeline"),
165 layout: Some(&pipeline_layout),
166 vertex: etalon::VertexState {
167 module: &shader,
168 entry_point: Some("vertex_main"),
169 compilation_options: Default::default(),
170 buffers: &[Some(etalon::VertexBufferLayout {
171 array_stride: (std::mem::size_of::<[f32; 3]>()
172 + std::mem::size_of::<[f32; 4]>())
173 as etalon::BufferAddress,
174 step_mode: etalon::VertexStepMode::Vertex,
175 attributes: &[
176 etalon::VertexAttribute {
177 offset: 0,
178 shader_location: 0,
179 format: etalon::VertexFormat::Float32x3,
180 },
181 etalon::VertexAttribute {
182 offset: std::mem::size_of::<[f32; 3]>() as etalon::BufferAddress,
183 shader_location: 1,
184 format: etalon::VertexFormat::Float32x4,
185 },
186 ],
187 })],
188 },
189 fragment: Some(etalon::FragmentState {
190 module: &shader,
191 entry_point: Some("fragment_main"),
192 compilation_options: Default::default(),
193 targets: &[Some(etalon::ColorTargetState {
194 format: target_format,
195 blend: Some(etalon::BlendState::ALPHA_BLENDING),
196 write_mask: etalon::ColorWrites::ALL,
197 })],
198 }),
199 primitive: etalon::PrimitiveState {
200 topology: etalon::PrimitiveTopology::TriangleList,
201 strip_index_format: None,
202 front_face: etalon::FrontFace::Ccw,
203 cull_mode: None,
204 unclipped_depth: false,
205 polygon_mode: etalon::PolygonMode::Fill,
206 conservative: false,
207 },
208 depth_stencil: None,
209 multisample: etalon::MultisampleState::default(),
210 multiview_mask: None,
211 cache: None,
212 })
213 }
214
215 pub(super) fn create_render_pipeline(
216 device: &etalon::Device,
217 target_format: etalon::TextureFormat,
218 ) -> etalon::RenderPipeline {
219 let shader = device.create_shader_module(etalon::ShaderModuleDescriptor {
220 label: Some("webizen-screen-shader"),
221 source: etalon::ShaderSource::Wgsl(crate::shaders::SCREEN_WGSL.into()),
222 });
223
224 let pipeline_layout = device.create_pipeline_layout(&etalon::PipelineLayoutDescriptor {
225 label: Some("webizen-screen-pipeline-layout"),
226 bind_group_layouts: &[],
227 immediate_size: 0,
228 });
229
230 device.create_render_pipeline(&etalon::RenderPipelineDescriptor {
231 label: Some("webizen-screen-pipeline"),
232 layout: Some(&pipeline_layout),
233 vertex: etalon::VertexState {
234 module: &shader,
235 entry_point: Some("vertex_main"),
236 compilation_options: Default::default(),
237 buffers: &[Some(etalon::VertexBufferLayout {
238 array_stride: std::mem::size_of::<ScreenVertex>() as etalon::BufferAddress,
239 step_mode: etalon::VertexStepMode::Vertex,
240 attributes: &[
241 etalon::VertexAttribute {
242 offset: 0,
243 shader_location: 0,
244 format: etalon::VertexFormat::Float32x2,
245 },
246 etalon::VertexAttribute {
247 offset: std::mem::size_of::<[f32; 2]>() as etalon::BufferAddress,
248 shader_location: 1,
249 format: etalon::VertexFormat::Float32x4,
250 },
251 ],
252 })],
253 },
254 fragment: Some(etalon::FragmentState {
255 module: &shader,
256 entry_point: Some("fragment_main"),
257 compilation_options: Default::default(),
258 targets: &[Some(etalon::ColorTargetState {
259 format: target_format,
260 blend: Some(etalon::BlendState::ALPHA_BLENDING),
261 write_mask: etalon::ColorWrites::ALL,
262 })],
263 }),
264 primitive: etalon::PrimitiveState {
265 topology: etalon::PrimitiveTopology::TriangleList,
266 strip_index_format: None,
267 front_face: etalon::FrontFace::Ccw,
268 cull_mode: None,
269 polygon_mode: etalon::PolygonMode::Fill,
270 unclipped_depth: false,
271 conservative: false,
272 },
273 depth_stencil: None,
274 multisample: etalon::MultisampleState {
275 count: 1,
276 mask: !0,
277 alpha_to_coverage_enabled: false,
278 },
279 multiview_mask: None,
280 cache: None,
281 })
282 }
283
284 #[cfg(not(feature = "qualia"))]
285 fn init_ambient_visualization(
286 device: &etalon::Device,
287 config: &AmbientConfig,
288 width: u32,
289 height: u32,
290 ) -> (
291 Option<etalon::RenderPipeline>,
292 Option<etalon::Buffer>,
293 Option<etalon::Buffer>,
294 Option<etalon::Buffer>,
295 usize,
296 vegetation_effects::EffectPool,
297 ) {
298 if !config.enabled {
299 return (
300 None,
301 None,
302 None,
303 None,
304 0,
305 vegetation_effects::EffectPool::new(0, vegetation_effects::DEFAULT_WORLD_SEED),
306 );
307 }
308
309 let effects = vegetation_effects::EffectPool::new(
310 config.particle_count,
311 vegetation_effects::DEFAULT_WORLD_SEED,
312 );
313 let particle_count = effects.capacity();
314 let particles = effects.particles();
315
316 let particle_buffer = device.create_buffer(&etalon::BufferDescriptor {
317 label: Some("ambient-particle-buffer"),
318 size: (particle_count * std::mem::size_of::<ParticleInstance>()) as u64,
319 usage: etalon::BufferUsages::STORAGE | etalon::BufferUsages::COPY_DST,
320 mapped_at_creation: true,
321 });
322
323 particle_buffer
324 .slice(..)
325 .get_mapped_range_mut()
326 .expect("ambient particle buffer was mapped at creation")
327 .copy_from_slice(bytemuck::cast_slice(&particles));
328 particle_buffer.unmap();
329
330 let ambient_uniform_buffer = device.create_buffer(&etalon::BufferDescriptor {
331 label: Some("ambient-uniform-buffer"),
332 size: std::mem::size_of::<AmbientUniforms>() as u64,
333 usage: etalon::BufferUsages::UNIFORM | etalon::BufferUsages::COPY_DST,
334 mapped_at_creation: false,
335 });
336
337 let telemetry_buffer = device.create_buffer(&etalon::BufferDescriptor {
338 label: Some("ambient-telemetry-buffer"),
339 size: std::mem::size_of::<crate::scene_contract::SystemTelemetry>() as u64,
340 usage: etalon::BufferUsages::UNIFORM | etalon::BufferUsages::COPY_DST,
341 mapped_at_creation: false,
342 });
343
344 let ambient_pipeline = Self::create_ambient_pipeline(device, width, height);
345
346 (
347 Some(ambient_pipeline),
348 Some(particle_buffer),
349 Some(ambient_uniform_buffer),
350 Some(telemetry_buffer),
351 particle_count,
352 effects,
353 )
354 }
355
356 #[cfg(not(feature = "qualia"))]
357 fn create_ambient_pipeline(
358 device: &etalon::Device,
359 _width: u32,
360 _height: u32,
361 ) -> etalon::RenderPipeline {
362 let shader = device.create_shader_module(etalon::ShaderModuleDescriptor {
363 label: Some("ambient-shader"),
364 source: etalon::ShaderSource::Wgsl(crate::shaders::AMBIENT_WGSL.into()),
365 });
366
367 let bind_group_layout =
368 device.create_bind_group_layout(&etalon::BindGroupLayoutDescriptor {
369 label: Some("ambient-bind-group-layout"),
370 entries: &[
371 etalon::BindGroupLayoutEntry {
372 binding: 0,
373 visibility: etalon::ShaderStages::VERTEX,
374 ty: etalon::BindingType::Buffer {
375 ty: etalon::BufferBindingType::Uniform,
376 has_dynamic_offset: false,
377 min_binding_size: None,
378 },
379 count: None,
380 },
381 etalon::BindGroupLayoutEntry {
382 binding: 1,
383 visibility: etalon::ShaderStages::VERTEX,
384 ty: etalon::BindingType::Buffer {
385 ty: etalon::BufferBindingType::Uniform,
386 has_dynamic_offset: false,
387 min_binding_size: None,
388 },
389 count: None,
390 },
391 etalon::BindGroupLayoutEntry {
392 binding: 2,
393 visibility: etalon::ShaderStages::VERTEX,
394 ty: etalon::BindingType::Buffer {
395 ty: etalon::BufferBindingType::Storage { read_only: true },
396 has_dynamic_offset: false,
397 min_binding_size: None,
398 },
399 count: None,
400 },
401 ],
402 });
403
404 let pipeline_layout = device.create_pipeline_layout(&etalon::PipelineLayoutDescriptor {
405 label: Some("ambient-pipeline-layout"),
406 bind_group_layouts: &[Some(&bind_group_layout)],
407 immediate_size: 0,
408 });
409
410 device.create_render_pipeline(&etalon::RenderPipelineDescriptor {
411 label: Some("ambient-pipeline"),
412 layout: Some(&pipeline_layout),
413 vertex: etalon::VertexState {
414 module: &shader,
415 entry_point: Some("vertex_main"),
416 compilation_options: Default::default(),
417 buffers: &[],
418 },
419 fragment: Some(etalon::FragmentState {
420 module: &shader,
421 entry_point: Some("fragment_main"),
422 compilation_options: Default::default(),
423 targets: &[Some(etalon::ColorTargetState {
424 format: etalon::TextureFormat::Rgba8Unorm,
425 blend: Some(etalon::BlendState {
426 color: etalon::BlendComponent {
427 src_factor: etalon::BlendFactor::SrcAlpha,
428 dst_factor: etalon::BlendFactor::One,
429 operation: etalon::BlendOperation::Add,
430 },
431 alpha: etalon::BlendComponent {
432 src_factor: etalon::BlendFactor::One,
433 dst_factor: etalon::BlendFactor::One,
434 operation: etalon::BlendOperation::Add,
435 },
436 }),
437 write_mask: etalon::ColorWrites::ALL,
438 })],
439 }),
440 primitive: etalon::PrimitiveState {
441 topology: etalon::PrimitiveTopology::TriangleList,
442 strip_index_format: None,
443 front_face: etalon::FrontFace::Ccw,
444 cull_mode: None,
445 polygon_mode: etalon::PolygonMode::Fill,
446 unclipped_depth: false,
447 conservative: false,
448 },
449 depth_stencil: None,
450 multisample: etalon::MultisampleState {
451 count: 1,
452 mask: !0,
453 alpha_to_coverage_enabled: false,
454 },
455 multiview_mask: None,
456 cache: None,
457 })
458 }
459}