sgl-3d 0.9.2

Caller-driven 3D rendering for SGL games, native and in the browser (WebGPU)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! The geometry pipelines: every pipeline that draws scene geometry from a
//! draw list, in one cache keyed by pass, by what the material and instance
//! require (face culling, alpha mode and deformed vertices: `variant`), by
//! the diagnostics layer constants and by whether the scene holds rectangle
//! lights and decals (`LitConstants`), and by the material's shader, whose
//! programs (`programs`) a pipeline is created from; the volume layers'
//! passes exist for the shaders that read their volume path, on the
//! Extended binding tier alone. The lighting pass while ray-traced
//! shadows run composes the shadow mask's provider (`shading::SHADOW_MASK`)
//! and binds the mask at its group 3; every other pass composes the
//! provider that holds no slot (`shading::SHADOW_MASK_NONE`). The lighting
//! pass while contact shadows run composes the contact provider
//! (`shading::CONTACT_SHADOW`) and binds the contact mask there instead;
//! every other pass composes the one that leaves the maps alone
//! (`shading::CONTACT_SHADOW_NONE`). The fused and
//! lighting passes while screen-space indirect light has a kept result
//! compose its provider (`shading::CAMERA_SCREEN_INDIRECT`) and bind it at
//! their group 3 too; every other pass composes the one that gives none. Every program
//! composes the lit and material-map providers of the device's binding tier
//! (`shading::lit_provider`, `shading::material_provider`), and every
//! pipeline layout takes group 0's and group 2's layouts of that tier, fixed
//! for the device, so no key holds it.
use crate::Scene;
use crate::content::identity::ShaderId;
use crate::shading::bind::{BindingTier, LitLayout};
use crate::shading::programs::*;
use crate::shading::{self, gbuffer};
use crate::view::targets::{composition_targets, mask_targets};
use std::collections::HashMap;

mod key;
mod pass;
mod programs;
mod retention;
mod variant;
pub(crate) use crate::shading::programs::CameraShadows;
pub(crate) use key::{LayerConstants, LitConstants};
use key::{PipelineKey, Specialisation};
pub(crate) use pass::{GeometryPass, depth};
use pass::{attachments_fit, targets};
use programs::{Program, ProgramSet};
pub(crate) use retention::PipelineSpecialisations;
pub(crate) use variant::{Alpha, Cull, Variant};

pub(crate) struct GeometryPipelines {
    device: wgpu::Device,
    /// Group 0 lit, then scene and material.
    lit: wgpu::PipelineLayout,
    /// `lit`'s, then the blended group 3.
    blended: wgpu::PipelineLayout,
    /// `lit`'s, then the shadow mask's group 3.
    shadow_masked: wgpu::PipelineLayout,
    /// `lit`'s, then screen-space indirect light's group 3, without and
    /// with the shadow mask's bindings.
    lit_screen_indirect: wgpu::PipelineLayout,
    shadow_masked_screen_indirect: wgpu::PipelineLayout,
    /// `lit`'s, then the contact mask's group 3, without and with
    /// screen-space indirect light's bindings.
    contact_shadowed: wgpu::PipelineLayout,
    contact_shadowed_screen_indirect: wgpu::PipelineLayout,
    /// Group 0 shadow, then scene and material.
    shadow: wgpu::PipelineLayout,
    /// `shadow`'s, then the caster positions group 3, for the GPU-built
    /// cascades' casters.
    pulled_shadow: wgpu::PipelineLayout,
    /// The programs of materials without a shader.
    programs: ProgramSet,
    /// The programs of each shader a material names, dropped with their
    /// pipelines when none does.
    shader_programs: HashMap<ShaderId, ProgramSet>,
    cache: HashMap<PipelineKey, wgpu::RenderPipeline>,
    current: Specialisation,
    /// A light or material toggled back can reuse its previous pipelines.
    previous: Option<Specialisation>,
    /// Whether `GBuffer` writes anisotropy itself; otherwise
    /// `GBufferAnisotropy` does.
    pub anisotropy_inline: bool,
    /// Whether the device has the attachments `Fused` writes.
    pub fused_supported: bool,
    /// Whether the device draws `DirectionalShadow` with unclipped depth
    /// (`DEPTH_CLIP_CONTROL`); otherwise its casters emulate it.
    unclipped_depth: bool,
    /// The device's binding tier, which every program's material maps and
    /// group 2's layout follow.
    pub tier: BindingTier,
}

impl GeometryPipelines {
    /// Create the shared layouts for this device and leave the cache empty
    /// until a frame or probe capture binds a geometry pipeline.
    pub fn new(
        device: &wgpu::Device,
        lit: &LitLayout,
        [
            shadow,
            scene,
            blended,
            shadow_mask,
            screen_indirect,
            shadow_mask_screen_indirect,
            contact_shadow,
            contact_shadow_screen_indirect,
            positions,
        ]: [&wgpu::BindGroupLayout; 9],
    ) -> Self {
        let tier = lit.tier;
        let lit = &lit.layout;
        let layout = |label, groups: &[&wgpu::BindGroupLayout]| {
            device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some(label),
                bind_group_layouts: &groups.iter().map(|&group| Some(group)).collect::<Vec<_>>(),
                immediate_size: 0,
            })
        };
        let limits = device.limits();
        let material = &shading::bind::material(device, tier);
        Self {
            device: device.clone(),
            lit: layout("lit scene geometry", &[lit, scene, material]),
            blended: layout("blended scene geometry", &[lit, scene, material, blended]),
            shadow_masked: layout(
                "shadow-masked scene lighting",
                &[lit, scene, material, shadow_mask],
            ),
            lit_screen_indirect: layout(
                "scene lighting with screen-space indirect light",
                &[lit, scene, material, screen_indirect],
            ),
            shadow_masked_screen_indirect: layout(
                "shadow-masked scene lighting with screen-space indirect light",
                &[lit, scene, material, shadow_mask_screen_indirect],
            ),
            contact_shadowed: layout(
                "contact-shadowed scene lighting",
                &[lit, scene, material, contact_shadow],
            ),
            contact_shadowed_screen_indirect: layout(
                "contact-shadowed scene lighting with screen-space indirect light",
                &[lit, scene, material, contact_shadow_screen_indirect],
            ),
            shadow: layout("shadow casters", &[shadow, scene, material]),
            pulled_shadow: layout(
                "GPU-built cascade casters",
                &[shadow, scene, material, positions],
            ),
            programs: ProgramSet::default_set(),
            shader_programs: HashMap::new(),
            cache: HashMap::new(),
            current: (LayerConstants::ALL, LitConstants::default(), false),
            previous: None,
            anisotropy_inline: attachments_fit(&limits, &targets(GeometryPass::GBuffer, true)),
            fused_supported: attachments_fit(
                &limits,
                &targets(
                    GeometryPass::Fused {
                        screen_indirect: false,
                    },
                    true,
                ),
            ),
            unclipped_depth: device
                .features()
                .contains(wgpu::Features::DEPTH_CLIP_CONTROL),
            tier,
        }
    }

    /// Select the constants for this frame or capture. Keep the current and
    /// previous specialisations, and only shaders still named by a material
    /// of this scene. Shader generations are unique across scenes.
    pub fn specialise(&mut self, layers: LayerConstants, scene: &Scene) {
        let next = (
            layers,
            LitConstants::of(scene),
            scene.materials.holds_transmissive(),
        );
        let changed = next != self.current;
        if changed {
            self.previous = Some(std::mem::replace(&mut self.current, next));
        }
        let shaders = self.shader_programs.len();
        self.shader_programs
            .retain(|&id, _| scene.shaders.get(id).is_ok_and(|shader| shader.users > 0));
        if changed || self.shader_programs.len() != shaders {
            self.cache.retain(|key, _| {
                (key.specialised(next) || self.previous.is_some_and(|s| key.specialised(s)))
                    && key
                        .shader
                        .is_none_or(|id| self.shader_programs.contains_key(&id))
            });
        }
    }

    /// Called when an executor binds a draw. GPU-built lists need the pipeline
    /// for the issued indirect command even when culling leaves zero instances.
    pub fn with_pipeline<T>(
        &mut self,
        scene: &Scene,
        pass: GeometryPass,
        variant: Variant,
        shader: Option<ShaderId>,
        use_pipeline: impl FnOnce(&wgpu::RenderPipeline) -> T,
    ) -> T {
        let (layers, lit, transmission) = self.current;
        let key = PipelineKey::new(pass, variant, shader, (layers, lit), transmission);
        if let Some(pipeline) = self.cache.get(&key) {
            return use_pipeline(pipeline);
        }
        let pipeline = self.create(scene, key);
        let result = use_pipeline(&pipeline);
        self.cache.insert(key, pipeline);
        result
    }

    /// The shader modules and pipelines it holds for game shaders.
    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub fn shader_programs(&self) -> (usize, usize) {
        (
            self.shader_programs.values().map(ProgramSet::modules).sum(),
            self.cache.keys().filter(|key| key.shader.is_some()).count(),
        )
    }

    fn create(&mut self, scene: &Scene, key: PipelineKey) -> wgpu::RenderPipeline {
        use GeometryPass::*;
        let device = &self.device;
        let masked = key.variant.alpha == Alpha::Mask;
        let shaded = key.shader.is_some();
        let vertex_buffers = &shading::vertex::GEOMETRY_BUFFERS;
        let (program, layout, label) = match key.pass {
            DirectionalShadow | PairedShadow => {
                (Program::Caster, &self.pulled_shadow, "shadow caster")
            }
            CaptureShadow | LocalShadow => (Program::Caster, &self.shadow, "shadow caster"),
            Blended { .. } => (
                Program::Geometry(GeometryForm::Blended),
                &self.blended,
                "blended scene geometry",
            ),
            Receivers => (
                Program::Geometry(GeometryForm::Plain),
                &self.lit,
                "blended receivers",
            ),
            TemporalComposition { .. } => (
                Program::Geometry(GeometryForm::Plain),
                &self.lit,
                "temporal composition of moving surfaces",
            ),
            VolumeEntry | VolumeExit => (
                Program::Geometry(GeometryForm::Plain),
                &self.lit,
                "volume layer",
            ),
            // The second exit reads the exit layer at its blended binding,
            // which the plain program declares on the Extended tier.
            VolumeSecondExit => (
                Program::Geometry(GeometryForm::Plain),
                &self.blended,
                "second exit volume layer",
            ),
            Lighting {
                shadows,
                screen_indirect,
            } => (
                Program::Geometry(GeometryForm::Camera {
                    shadows,
                    screen_indirect,
                }),
                match (shadows, screen_indirect) {
                    (CameraShadows::Maps, false) => &self.lit,
                    (CameraShadows::ShadowMask, false) => &self.shadow_masked,
                    (CameraShadows::ContactMask, false) => &self.contact_shadowed,
                    (CameraShadows::Maps, true) => &self.lit_screen_indirect,
                    (CameraShadows::ShadowMask, true) => &self.shadow_masked_screen_indirect,
                    (CameraShadows::ContactMask, true) => &self.contact_shadowed_screen_indirect,
                },
                "scene lighting",
            ),
            Fused { screen_indirect } => (
                Program::Geometry(GeometryForm::Camera {
                    shadows: CameraShadows::Maps,
                    screen_indirect,
                }),
                if screen_indirect {
                    &self.lit_screen_indirect
                } else {
                    &self.lit
                },
                "fused scene geometry and lighting",
            ),
            _ => (
                Program::Geometry(GeometryForm::Plain),
                &self.lit,
                "lit scene geometry",
            ),
        };
        let tier = self.tier;
        let programs = match key.shader {
            Some(id) => self
                .shader_programs
                .entry(id)
                .or_insert_with(|| ProgramSet::of(scene, id)),
            None => &mut self.programs,
        };
        let module = programs.module(device, tier, program);
        // Casters are depth-only: their cull selects the side that casts. A
        // directional cascade's depth is unclipped, so casters between the
        // light and the cascade are drawn at its near plane, as Bevy's
        // `UNCLIPPED_DEPTH_ORTHO` shadow views and Wicked's directional
        // shadow cameras draw them; a device without `DEPTH_CLIP_CONTROL`
        // emulates it in the shader, as Bevy does. A masked material's
        // casters discard what it cuts out, as Bevy's do (MAY_DISCARD).
        let (vertex, fragment) = match key.pass {
            // A shader's masked casters take their coverage from its surface
            // function, whose context their vertices carry.
            DirectionalShadow if masked && shaded && !self.unclipped_depth => (
                SHADOW_PULLED_SHADED_MASKED_UNCLIPPED_VS_ENTRY,
                Some(SHADOW_SHADED_MASKED_UNCLIPPED_FS_ENTRY),
            ),
            DirectionalShadow if masked && shaded => (
                SHADOW_PULLED_SHADED_MASKED_VS_ENTRY,
                Some(SHADOW_SHADED_MASKED_FS_ENTRY),
            ),
            CaptureShadow if masked && shaded && !self.unclipped_depth => (
                SHADOW_SHADED_MASKED_UNCLIPPED_VS_ENTRY,
                Some(SHADOW_SHADED_MASKED_UNCLIPPED_FS_ENTRY),
            ),
            CaptureShadow | LocalShadow if masked && shaded => (
                SHADOW_SHADED_MASKED_VS_ENTRY,
                Some(SHADOW_SHADED_MASKED_FS_ENTRY),
            ),
            DirectionalShadow if masked && !self.unclipped_depth => (
                SHADOW_PULLED_MASKED_UNCLIPPED_VS_ENTRY,
                Some(SHADOW_MASKED_UNCLIPPED_FS_ENTRY),
            ),
            DirectionalShadow if masked => {
                (SHADOW_PULLED_MASKED_VS_ENTRY, Some(SHADOW_MASKED_FS_ENTRY))
            }
            DirectionalShadow if !self.unclipped_depth => (
                SHADOW_PULLED_UNCLIPPED_VS_ENTRY,
                Some(SHADOW_UNCLIPPED_FS_ENTRY),
            ),
            DirectionalShadow => (SHADOW_PULLED_VS_ENTRY, None),
            PairedShadow if !self.unclipped_depth => (
                SHADOW_PAIRED_UNCLIPPED_VS_ENTRY,
                Some(SHADOW_UNCLIPPED_FS_ENTRY),
            ),
            PairedShadow => (SHADOW_PAIRED_VS_ENTRY, None),
            CaptureShadow if masked && !self.unclipped_depth => (
                SHADOW_MASKED_UNCLIPPED_VS_ENTRY,
                Some(SHADOW_MASKED_UNCLIPPED_FS_ENTRY),
            ),
            CaptureShadow | LocalShadow if masked => {
                (SHADOW_MASKED_VS_ENTRY, Some(SHADOW_MASKED_FS_ENTRY))
            }
            Forward => (SOURCE_VS_ENTRY, Some(FS_ENTRY)),
            GBuffer if self.anisotropy_inline => (SOURCE_VS_ENTRY, Some(STABLE_FS_ENTRY)),
            GBuffer => (SOURCE_VS_ENTRY, Some(STABLE_LEGACY_FS_ENTRY)),
            GBufferAnisotropy => (SOURCE_VS_ENTRY, Some(ANISOTROPY_FS_ENTRY)),
            Lighting { .. } => (SOURCE_VS_ENTRY, Some(SOURCE_FS_ENTRY)),
            Fused { .. } => (SOURCE_VS_ENTRY, Some(FUSED_OPAQUE_FS_ENTRY)),
            CaptureShadow if !self.unclipped_depth => {
                (SHADOW_UNCLIPPED_VS_ENTRY, Some(SHADOW_UNCLIPPED_FS_ENTRY))
            }
            CaptureShadow | LocalShadow => (SHADOW_VS_ENTRY, None),
            Blended {
                temporal_masks: false,
            } => (SOURCE_VS_ENTRY, Some(BLENDED_FS_ENTRY)),
            Blended {
                temporal_masks: true,
            } => (SOURCE_VS_ENTRY, Some(BLENDED_MASKED_FS_ENTRY)),
            Receivers => (SOURCE_VS_ENTRY, Some(RECEIVER_FS_ENTRY)),
            TemporalComposition { .. } => (SOURCE_VS_ENTRY, Some(TEMPORAL_COMPOSITION_FS_ENTRY)),
            VolumeEntry => (SOURCE_VS_ENTRY, Some(VOLUME_ENTRY_FS_ENTRY)),
            VolumeExit => (SOURCE_VS_ENTRY, Some(VOLUME_EXIT_FS_ENTRY)),
            VolumeSecondExit => (SOURCE_VS_ENTRY, Some(VOLUME_SECOND_EXIT_FS_ENTRY)),
        };
        let unclipped_depth = matches!(key.pass, DirectionalShadow | PairedShadow | CaptureShadow)
            && self.unclipped_depth;
        // Blended surfaces blend over the beauty premultiplied, their colour
        // carrying its alpha (blended_color in geometry.wgsl), as Filament
        // ef1a133 blends both its transparent and fade modes (One,
        // OneMinusSrcAlpha; docs_src/src_markdeep/Filament.md.html,
        // Transparency) and Bevy 9d12036 its `Premultiplied` materials
        // (crates/bevy_pbr/src/render/mesh.rs 3534–3541).
        let blend = matches!(key.pass, Blended { .. })
            .then_some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING);
        let mut targets: Vec<_> = targets(key.pass, self.anisotropy_inline)
            .into_iter()
            .map(|format| {
                Some(wgpu::ColorTargetState {
                    format,
                    blend,
                    write_mask: wgpu::ColorWrites::ALL,
                })
            })
            .collect();
        if key.pass
            == (Blended {
                temporal_masks: true,
            })
        {
            targets.extend(mask_targets());
        }
        if matches!(key.pass, TemporalComposition { .. }) {
            targets.extend(composition_targets());
        }
        let constants = key.constants();
        let (depth_write, depth_compare) = depth(key.pass);
        device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some(label),
            layout: Some(layout),
            vertex: wgpu::VertexState {
                module,
                entry_point: Some(vertex),
                compilation_options: wgpu::PipelineCompilationOptions {
                    constants: &constants,
                    ..Default::default()
                },
                buffers: if key.pass.pulled() {
                    &vertex_buffers[..1]
                } else {
                    vertex_buffers
                },
            },
            fragment: fragment.map(|fragment| wgpu::FragmentState {
                module,
                entry_point: Some(fragment),
                compilation_options: wgpu::PipelineCompilationOptions {
                    constants: &constants,
                    ..Default::default()
                },
                targets: &targets,
            }),
            primitive: wgpu::PrimitiveState {
                cull_mode: key.variant.cull.face(),
                unclipped_depth,
                ..Default::default()
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: gbuffer::DEPTH,
                depth_write_enabled: Some(depth_write),
                depth_compare: Some(depth_compare),
                stencil: Default::default(),
                // Casters store their depth unbiased: Bevy offsets the
                // receiver instead, by its normal and depth bias
                // (shadow_receiver_offset).
                bias: Default::default(),
            }),
            multisample: Default::default(),
            multiview_mask: None,
            cache: None,
        })
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod primary_depth_precision_tests;
#[cfg(all(test, not(target_arch = "wasm32")))]
mod specialisation_tests;