sgl-3d 0.4.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Ray-traced shadows (the architecture's Ray-traced shadows): while
//! hardware ray tracing is in effect and `Settings::ray_traced_shadows` is
//! on, the camera's opaque surfaces take the shadows of the slots' lights
//! from rays through the scene instead of from the maps, as Wicked Engine
//! traces them (2ff1d9e `Postprocess_RTShadow`, wiRenderer.cpp 15498–15880,
//! its resources at 15440–15497): a trace at half the render size, AMD's
//! shadow denoiser over the first four slots, or the first alone at
//! `Settings::ray_traced_shadow_quality` Low (`denoise`), a temporal blend
//! of the rest and an upsample into the shadow mask, which the opaque
//! stage's lighting pass reads with the slot table at its group 3. Slot 0
//! is the directional light with the frame's cascades, slots 1 to 15 the
//! casting local lights the local-light atlas places, in its ranking
//! (`slots`).
//!
//! Reads: the G-buffer's depth, normals, F0 and motion, the camera's lit
//! group 0 (its frame's directional lights and the scene's lights), the
//! scene's group 1 and TLAS, the frame's view and the camera history's
//! last view.
//! Writes: its own tracing targets and history, the shadow mask and the
//! slot table, which it lends to the opaque stage's lighting pass.
//! Honours: the effective ray-traced shadows, on the frames whose rays
//! trace in hardware, and their quality.
//! Timing groups: `ray-traced shadow rays`, `ray-traced shadow tile
//! classification`, `ray-traced shadow filter` (three passes, or two at
//! Low),
//! `ray-traced shadow temporal`, `ray-traced shadow upsample`.
pub(crate) mod denoise;
pub(crate) mod slots;

use crate::settings::RayTracedShadowQuality;
use crate::shading::RayQueryForm;
use crate::shading::{self, shadow_mask};
use crate::view::cached_group::CachedGroup;
use crate::view::frame::{FrameContext, ShadowMask};
use crate::view::history::StageHistory;
use crate::view::trace_paths::{TracePath, TracePaths};
use std::collections::HashMap;

/// Wicked's `DOWNSAMPLE`: the trace runs at half the render size.
const DOWNSAMPLE: u32 = 2;

/// `TracedParams` in traced_common.wgsl.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Params {
    inverse_view_projection: [[f32; 4]; 4],
    view: [[f32; 4]; 4],
    previous_view: [[f32; 4]; 4],
    full: [f32; 4],
    reduced: [f32; 4],
    eye: [f32; 4],
    frame: u32,
    seed: u32,
    denoised: u32,
    padding: u32,
}

static COMMON: shading::Module = shading::Module {
    name: "traced_shadows_common",
    source: include_str!("traced/traced_common.wgsl"),
    deps: &[&shading::SHADOW_MASK_SLOTS],
};
/// The trace: the camera's lit group 0, the scene at group 1 and the
/// stage's own group 3 with the TLAS, composed with the hardware form's
/// query module (`TracePaths`).
pub(crate) static TRACE: shading::Module = shading::Module {
    name: "traced_shadows_trace",
    source: include_str!("traced/traced_trace.wgsl"),
    deps: &[
        &shading::BIND_LIT,
        &shading::GBUFFER,
        &shading::LIGHT_REACH,
        &shading::LIGHT_SURFACE,
        &shading::HASH,
        &shading::SHADOW_MASK_SLOT_KEY,
        &shading::SCENE_RAYS_PREDICATE,
        &COMMON,
    ],
};
/// The entry points the stage's pipelines are created with.
pub(crate) const TRACE_ENTRY: &str = "traced_shadow_rays";
pub(crate) const TEMPORAL_ENTRY: &str = "traced_shadow_temporal";
pub(crate) const UPSAMPLE_ENTRY: &str = "traced_shadow_upsample";
pub(crate) static TEMPORAL: shading::Module = shading::Module {
    name: "traced_shadows_temporal",
    source: include_str!("traced/traced_temporal.wgsl"),
    deps: &[&shading::SHADOW_MASK_SLOTS, &COMMON],
};
pub(crate) static UPSAMPLE: shading::Module = shading::Module {
    name: "traced_shadows_upsample",
    source: include_str!("traced/traced_upsample.wgsl"),
    deps: &[&shading::SHADOW_MASK_SLOTS, &COMMON],
};

fn texture(
    device: &wgpu::Device,
    label: &str,
    [width, height]: [u32; 2],
    layers: u32,
    format: wgpu::TextureFormat,
) -> wgpu::TextureView {
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some(label),
        size: wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: layers,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format,
        usage: wgpu::TextureUsages::TEXTURE_BINDING
            | wgpu::TextureUsages::STORAGE_BINDING
            | wgpu::TextureUsages::COPY_SRC,
        view_formats: &[],
    });
    texture.create_view(&wgpu::TextureViewDescriptor {
        dimension: (layers > 1).then_some(wgpu::TextureViewDimension::D2Array),
        ..Default::default()
    })
}

/// The stage's targets at one render size: the trace's words and linear
/// depth at the tracing resolution, the temporal blend's pair, and the
/// mask at the render size.
struct Targets {
    full: [u32; 2],
    reduced: [u32; 2],
    raw: wgpu::TextureView,
    /// Each frame's linear depth at the tracing resolution, the half the
    /// frame writes and the other, last frame's, which its reprojection
    /// reads.
    depth: [wgpu::TextureView; 2],
    temporal: [wgpu::TextureView; 2],
    mask: wgpu::TextureView,
    /// The denoiser's.
    denoise: denoise::Targets,
}

impl Targets {
    fn new(device: &wgpu::Device, full: [u32; 2]) -> Self {
        let reduced = full.map(|side| side.div_ceil(DOWNSAMPLE).max(1));
        let words = wgpu::TextureFormat::Rgba32Uint;
        let half = |label, format| texture(device, label, reduced, 1, format);
        Self {
            full,
            reduced,
            raw: half("ray-traced shadow rays", words),
            depth: [0, 1].map(|_| half("ray-traced shadow depth", wgpu::TextureFormat::R32Float)),
            temporal: [0, 1].map(|_| half("ray-traced shadow temporal", words)),
            mask: texture(
                device,
                "ray-traced shadow mask",
                full,
                shadow_mask::LAYERS,
                shadow_mask::FORMAT,
            ),
            denoise: denoise::Targets::new(device, reduced),
        }
    }
}

/// A compute pass with its group 0 for each half of the history pair.
struct Pass {
    pipeline: wgpu::ComputePipeline,
    groups: [CachedGroup; 2],
}

impl Pass {
    /// The pass `entry` of `module`, its pipeline constants `constants`.
    fn new(
        device: &wgpu::Device,
        module: &'static shading::Module,
        entry: &str,
        constants: &[(&str, f64)],
    ) -> Self {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some(entry),
            source: wgpu::ShaderSource::Wgsl(shading::compose(&[module]).into()),
        });
        let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some(entry),
            layout: None,
            module: &shader,
            entry_point: Some(entry),
            compilation_options: wgpu::PipelineCompilationOptions {
                constants,
                ..Default::default()
            },
            cache: None,
        });
        let group = || CachedGroup::new(pipeline.get_bind_group_layout(0));
        Self {
            groups: [group(), group()],
            pipeline,
        }
    }
}

/// The ray-traced shadow stage. Owns its pipelines, targets (reallocated
/// when the render size changes), slot table and history, which continues
/// across consecutive frames it runs in.
pub(crate) struct TracedShadows {
    /// The trace's programs, with its group 3.
    paths: TracePaths,
    /// The trace's pipeline for each form a frame's rays took.
    trace: HashMap<((), Option<RayQueryForm>), wgpu::ComputePipeline>,
    temporal: Pass,
    upsample: Pass,
    /// AMD's shadow denoiser over the first slots.
    denoiser: denoise::Denoiser,
    /// The quality the history was made at; another restarts it.
    quality: Option<RayTracedShadowQuality>,
    params: wgpu::Buffer,
    /// The slot table, which the lighting pass reads too.
    slot_table: wgpu::Buffer,
    slots: slots::Slots,
    /// The last frame's slot table, and whether the stage ran in it.
    table: shadow_mask::ShadowMaskSlots,
    ran: bool,
    targets: Option<Targets>,
    /// Frames since the history last restarted, and the half of each
    /// history pair the frame writes.
    history: StageHistory,
    /// Turns the rays' draws on the lights each frame the stage runs.
    seed: u32,
    /// The camera history's frame count of the last frame the stage ran.
    previous_frame: Option<u32>,
}

impl TracedShadows {
    /// `lit` and `scene` are group 0's lit layout and group 1's. Its
    /// targets are made in the first frame it runs.
    pub fn new(
        device: &wgpu::Device,
        lit: &shading::bind::LitLayout,
        scene: &wgpu::BindGroupLayout,
    ) -> Self {
        let entry = |binding, ty| wgpu::BindGroupLayoutEntry {
            binding,
            visibility: wgpu::ShaderStages::COMPUTE,
            ty,
            count: None,
        };
        let sampled = |sample_type| wgpu::BindingType::Texture {
            sample_type,
            view_dimension: wgpu::TextureViewDimension::D2,
            multisampled: false,
        };
        let uniform = wgpu::BindingType::Buffer {
            ty: wgpu::BufferBindingType::Uniform,
            has_dynamic_offset: false,
            min_binding_size: None,
        };
        let storage = |format| wgpu::BindingType::StorageTexture {
            access: wgpu::StorageTextureAccess::WriteOnly,
            format,
            view_dimension: wgpu::TextureViewDimension::D2,
        };
        let unfilterable = wgpu::TextureSampleType::Float { filterable: false };
        let entries = [
            entry(0, sampled(wgpu::TextureSampleType::Depth)),
            entry(1, sampled(unfilterable)),
            entry(2, sampled(unfilterable)),
            entry(3, uniform),
            entry(4, uniform),
            entry(5, storage(wgpu::TextureFormat::Rgba32Uint)),
            entry(6, storage(wgpu::TextureFormat::R32Float)),
            entry(7, storage(wgpu::TextureFormat::Rgba32Uint)),
            entry(8, storage(denoise::NORMAL_FORMAT)),
        ];
        let uniform_buffer = |label, size| {
            crate::counters::buffer(
                device,
                &wgpu::BufferDescriptor {
                    label: Some(label),
                    size,
                    usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                },
            )
        };
        Self {
            paths: TracePaths::new("ray-traced shadow rays", &TRACE, &entries, lit, scene),
            trace: HashMap::new(),
            temporal: Pass::new(device, &TEMPORAL, TEMPORAL_ENTRY, &[]),
            upsample: Pass::new(device, &UPSAMPLE, UPSAMPLE_ENTRY, &[]),
            denoiser: denoise::Denoiser::default(),
            quality: None,
            params: uniform_buffer(
                "ray-traced shadow parameters",
                std::mem::size_of::<Params>() as u64,
            ),
            slot_table: uniform_buffer(
                "ray-traced shadow slots",
                std::mem::size_of::<shadow_mask::ShadowMaskSlots>() as u64,
            ),
            slots: slots::Slots::default(),
            table: shadow_mask::ShadowMaskSlots::new(
                [shadow_mask::SHADOW_MASK_EMPTY; shadow_mask::RT_SHADOW_LIGHTS],
                0,
                0,
            ),
            ran: false,
            targets: None,
            history: StageHistory::default(),
            seed: 0,
            previous_frame: None,
        }
    }

    /// Traces, blends and upsamples the shadows of the frame's slots: slot
    /// 0 the directional light with the frame's cascades, the others the
    /// local lights the atlas placed, best first (`lights`). Returns the
    /// mask and slot table for the lighting pass; none where the stage does
    /// not run (`Effective::ray_traced_shadows`), a frame whose rays do not
    /// trace in hardware or whose slots hold no light.
    pub fn encode<'s>(
        &'s mut self,
        ctx: &mut FrameContext<'_>,
        lights: &slots::SlotLights,
    ) -> Option<ShadowMask<'s>> {
        self.ran = false;
        // A reset ends the history even in a frame the stage does not run,
        // so no later frame takes one from before it for its predecessor.
        if !ctx.history.valid {
            self.previous_frame = None;
        }
        let hardware = ctx
            .hardware_rays
            .filter(|_| ctx.effective.ray_traced_shadows)?;
        let size = ctx.sizes.render;
        let resized = self.targets.as_ref().is_none_or(|t| t.full != size);
        if resized {
            self.targets = Some(Targets::new(ctx.device, size));
            self.denoiser.forget_groups();
        }
        // History continues across consecutive valid frames the stage runs
        // in, as the world-space reflection denoiser's does.
        let history = ctx.history;
        let continuous = history.valid
            && self
                .previous_frame
                .is_some_and(|previous| history.frames == previous.wrapping_add(1));
        // A change of quality changes which slots the denoiser keeps a
        // history for.
        let quality = ctx.effective.ray_traced_shadow_quality;
        if resized || !continuous || self.quality != Some(quality) {
            self.history.count = 0;
        }
        self.quality = Some(quality);
        self.previous_frame = Some(history.frames);
        let table = self.slots.assign(lights.directional, &lights.local);
        self.table = table;
        self.denoiser.prepare(ctx.device, quality);
        let shape = denoise::Shape::of(quality, table.lights[0]);
        crate::counters::write_buffer(ctx.queue, &self.slot_table, 0, bytemuck::bytes_of(&table));
        let targets = self.targets.as_ref().unwrap();
        let [width, height] = targets.full.map(|side| side as f32);
        let [reduced_width, reduced_height] = targets.reduced.map(|side| side as f32);
        let view = &ctx.values.view;
        // The last submitted frame's view, this frame's after a restart.
        let previous_view = history
            .previous_camera
            .map_or(history.camera.view, |previous| previous.view);
        let eye = ctx.input.camera.eye;
        crate::counters::write_buffer(
            ctx.queue,
            &self.params,
            0,
            bytemuck::bytes_of(&Params {
                inverse_view_projection: view.inverse_view_projection,
                view: view.view,
                previous_view: previous_view.to_cols_array_2d(),
                full: [width, height, 1. / width, 1. / height],
                reduced: [
                    reduced_width,
                    reduced_height,
                    1. / reduced_width,
                    1. / reduced_height,
                ],
                eye: [eye.x, eye.y, eye.z, 1.],
                frame: self.history.count,
                seed: self.seed,
                denoised: shape.lanes.slots(),
                padding: 0,
            }),
        );
        self.seed = self.seed.wrapping_add(1);
        let current = self.history.current;
        let previous = 1 - current;
        let shared = ctx.targets;
        let resource = wgpu::BindingResource::TextureView;
        // The trace's pipeline for the device's form, made through
        // `TracePaths::pipeline`, so that a candidate program that fails
        // falls the device back to the baseline before the group is made.
        let device = ctx.device;
        let form = self.paths.pipeline(
            device,
            Some(hardware),
            (&mut self.trace, ()),
            |TracePath { shader, layout, .. }| {
                device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                    label: Some("ray-traced shadow rays"),
                    layout: Some(layout),
                    module: shader,
                    entry_point: Some(TRACE_ENTRY),
                    compilation_options: Default::default(),
                    cache: None,
                })
            },
        );
        let trace_group = self.paths.group(
            ctx.device,
            Some(hardware),
            &[
                (0, resource(&shared.depth)),
                (1, resource(&shared.normal)),
                (2, resource(&shared.f0)),
                (3, self.params.as_entire_binding()),
                (4, self.slot_table.as_entire_binding()),
                (5, resource(&targets.raw)),
                (6, resource(&targets.depth[current])),
                (7, resource(&targets.denoise.tiles)),
                (8, resource(&targets.denoise.normal)),
            ],
        );
        {
            let mut pass = ctx
                .encoder
                .begin_compute_pass(&wgpu::ComputePassDescriptor {
                    label: Some("ray-traced shadow rays"),
                    timestamp_writes: ctx
                        .timing
                        .and_then(|t| t.compute_pass("ray-traced shadow rays")),
                });
            pass.set_pipeline(&self.trace[&((), form)]);
            pass.set_bind_group(0, ctx.bindings.camera_lit(), &[]);
            pass.set_bind_group(1, &ctx.scene.scene_group, &[]);
            pass.set_bind_group(3, trace_group, &[]);
            pass.dispatch_workgroups(
                targets.reduced[0].div_ceil(8),
                targets.reduced[1].div_ceil(4),
                1,
            );
        }
        // With none of the slots it filters held, nothing reads their
        // result, and a light that takes one restarts it.
        if shape.runs(table.lights[0]) {
            self.denoiser.encode(
                ctx.device,
                ctx.encoder,
                ctx.timing,
                shape,
                &targets.denoise,
                denoise::Inputs {
                    depth: &shared.depth,
                    motion: &shared.motion,
                    half_depth: &targets.depth[current],
                    previous_depth: &targets.depth[previous],
                    params: &self.params,
                    slot_table: &self.slot_table,
                    reduced: targets.reduced,
                    current,
                },
            );
        }
        let temporal_group = self.temporal.groups[current].get(
            ctx.device,
            "ray-traced shadow temporal",
            &[
                (0, resource(&targets.raw)),
                (1, resource(&targets.temporal[previous])),
                (2, resource(&targets.depth[current])),
                (3, resource(&targets.depth[previous])),
                (4, resource(&shared.motion)),
                (5, self.params.as_entire_binding()),
                (6, self.slot_table.as_entire_binding()),
                (7, resource(&targets.temporal[current])),
                (8, targets.denoise.denoised.as_entire_binding()),
            ],
        );
        {
            let mut pass = ctx
                .encoder
                .begin_compute_pass(&wgpu::ComputePassDescriptor {
                    label: Some("ray-traced shadow temporal"),
                    timestamp_writes: ctx
                        .timing
                        .and_then(|t| t.compute_pass("ray-traced shadow temporal")),
                });
            pass.set_pipeline(&self.temporal.pipeline);
            pass.set_bind_group(0, temporal_group, &[]);
            pass.dispatch_workgroups(
                targets.reduced[0].div_ceil(8),
                targets.reduced[1].div_ceil(8),
                1,
            );
        }
        let upsample_group = self.upsample.groups[current].get(
            ctx.device,
            "ray-traced shadow upsample",
            &[
                (0, resource(&targets.temporal[current])),
                (1, resource(&targets.depth[current])),
                (2, resource(&shared.depth)),
                (3, self.params.as_entire_binding()),
                (4, resource(&targets.mask)),
                (5, self.slot_table.as_entire_binding()),
            ],
        );
        {
            let mut pass = ctx
                .encoder
                .begin_compute_pass(&wgpu::ComputePassDescriptor {
                    label: Some("ray-traced shadow upsample"),
                    timestamp_writes: ctx
                        .timing
                        .and_then(|t| t.compute_pass("ray-traced shadow upsample")),
                });
            pass.set_pipeline(&self.upsample.pipeline);
            pass.set_bind_group(0, upsample_group, &[]);
            pass.dispatch_workgroups(targets.full[0].div_ceil(8), targets.full[1].div_ceil(8), 1);
        }
        self.history.advance();
        self.ran = true;
        Some(ShadowMask {
            mask: &targets.mask,
            slots: &self.slot_table,
        })
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
impl TracedShadows {
    /// The last frame's shadow mask and slot table, where the stage ran.
    pub fn last(&self) -> Option<(&wgpu::TextureView, shadow_mask::ShadowMaskSlots)> {
        let targets = self.targets.as_ref().filter(|_| self.ran)?;
        Some((&targets.mask, self.table))
    }
}

#[cfg(test)]
pub(crate) fn mirrors() -> [crate::shading::layout_tests::Mirror; 1] {
    [crate::shading::layout_tests::mirror!(
        "traced_shadows_temporal",
        "TracedParams",
        Params,
        [
            inverse_view_projection,
            view,
            previous_view,
            full,
            reduced,
            eye,
            frame,
            seed,
            denoised
        ]
    )]
}

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