sgl-3d 0.2.0

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
//! Source completion: ambient occlusion of opaque receivers' ambient diffuse,
//! and their environment and probe specular from the probes that tiled
//! culling finds for each screen tile; and the composition that blends a
//! screen-space method's reflections over it.
use super::probe_culling::ProbeCulling;
use crate::shading;
use crate::view::bindings::FogVolume;
use crate::view::cached_group::CachedGroup;
use crate::view::reflection_camera;

/// Source completion (`main`) and screen-space composition
/// (`fullscreen_vs`, `compose_screen_space`).
pub(crate) static COMPLETION: shading::Module = shading::Module {
    name: "reflection_source",
    source: include_str!("source.wgsl"),
    deps: &[
        &shading::GBUFFER,
        &shading::PROBE_SAMPLING,
        &shading::SPECULAR_LOBES,
        &shading::PROBE_COLLECTION,
        &shading::FULLSCREEN_VS,
        &shading::FOG,
    ],
};
/// The entry points the program's pipelines are created with:
/// completion's `main`, and composition's beside
/// `shading::FULLSCREEN_VS_ENTRY`.
pub(crate) const MAIN_ENTRY: &str = "main";
pub(crate) const COMPOSE_SCREEN_SPACE_ENTRY: &str = "compose_screen_space";

/// Completion's and composition's reflection environment; matches
/// `ReflectionEnvironment` in source.wgsl.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct ReflectionEnvironment {
    /// The prefiltered sky's yaw.
    pub yaw: f32,
    /// The prefiltered sky's intensity.
    pub intensity: f32,
    /// The width of the screen-space method's fade below its cutoff, in
    /// perceptual roughness.
    pub fade: f32,
    /// The alpha roughness below which the screen-space method traces a
    /// lobe; 0 traces none.
    pub traced: f32,
}

/// A reflection environment of `rotation` and `strength` with no lobe traced.
pub(crate) fn environment_uniform(
    device: &wgpu::Device,
    rotation: f32,
    strength: f32,
) -> wgpu::Buffer {
    crate::counters::buffer_init(
        device,
        &wgpu::util::BufferInitDescriptor {
            label: Some("application reflection environment intensity"),
            contents: bytemuck::bytes_of(&ReflectionEnvironment {
                yaw: rotation,
                intensity: strength,
                fade: 0.,
                traced: 0.,
            }),
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        },
    )
}

pub(crate) struct Environment<'a> {
    pub sky: &'a wgpu::TextureView,
    pub sampler: &'a wgpu::Sampler,
    pub parameters: &'a wgpu::Buffer,
    pub material: &'a wgpu::TextureView,
    pub baked: &'a wgpu::TextureView,
    pub collection: &'a wgpu::Buffer,
}
pub(crate) struct Inputs<'a> {
    /// The frame's fog volume and its sampler.
    pub fog: FogVolume<'a>,
    /// How completion and composition fog, while the frame has fog.
    pub frame_fog: Option<SourceFog>,
    pub camera: reflection_camera::Camera,
    pub scene: &'a wgpu::TextureView,
    /// The ambient diffuse within `scene` before occlusion.
    pub ambient: &'a wgpu::TextureView,
    /// Complete opaque beauty.
    pub output: &'a wgpu::TextureView,
    pub normal: &'a wgpu::TextureView,
    pub anisotropy: &'a wgpu::TextureView,
    pub f0: &'a wgpu::TextureView,
    pub depth: &'a wgpu::TextureView,
    /// Lit group 0's lookup tables, for the DFG table.
    pub lookup_tables: &'a wgpu::TextureView,
    /// Ambient visibility that occludes environment and probe specular,
    /// and ambient diffuse in the `diffuse_occlusion` variant.
    pub ambient_occlusion: &'a wgpu::TextureView,
    pub environment: Environment<'a>,
}

/// Source completion's camera; matches `SourceCamera` in source.wgsl.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct SourceCamera {
    inverse: [[f32; 4]; 4],
    eye: [f32; 4],
    fog: [f32; 4],
    forward: [f32; 3],
    flags: u32,
}
/// `SourceCamera::flags`: completion and composition apply the fog.
const SOURCE_FOG: u32 = 1;

/// The frame's fog as completion and composition apply it.
#[derive(Clone, Copy, Debug)]
pub(crate) struct SourceFog {
    /// One over the fog volume's length and over its detail spread.
    pub inverse_length: f32,
    pub inverse_detail_spread: f32,
    /// The share of its fog the sky takes, 0..=1 (`Fog::sky_affect`).
    pub sky_affect: f32,
}

impl SourceCamera {
    /// `camera`, fogged by `fog`.
    fn new(camera: reflection_camera::Camera, fog: Option<SourceFog>) -> Self {
        let forward = -glam::Mat4::from_cols_array_2d(&camera.view)
            .row(2)
            .truncate();
        Self {
            inverse: camera.inverse_view_proj,
            eye: camera.camera_position,
            fog: fog.map_or([0.; 4], |fog| {
                [
                    fog.inverse_length,
                    fog.inverse_detail_spread,
                    fog.sky_affect,
                    0.,
                ]
            }),
            forward: forward.to_array(),
            flags: if fog.is_some() { SOURCE_FOG } else { 0 },
        }
    }
}

/// What completion and composition are compiled for.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct Variant {
    /// They add environment and probe specular (off only as a diagnostics
    /// layer).
    pub environment: bool,
    /// Completion writes the incident radiance, which only a screen-space
    /// method reads.
    pub incident: bool,
    /// Completion occludes receivers' ambient diffuse by the ambient
    /// visibility: while ambient occlusion runs.
    pub diffuse_occlusion: bool,
}

/// Source completion and screen-space composition compiled for one variant,
/// with their groups.
struct Completion {
    pipeline: wgpu::ComputePipeline,
    group: CachedGroup,
    /// Adds a screen-space method's traced lobes into the complete beauty.
    compose: wgpu::RenderPipeline,
    compose_group: CachedGroup,
}

/// Source completion and screen-space composition for `variant`.
fn completion(device: &wgpu::Device, variant: Variant) -> Completion {
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("reflection source completion"),
        source: wgpu::ShaderSource::Wgsl(shading::compose(&[&COMPLETION]).into()),
    });
    let constants = [
        (
            "application_environment_enabled",
            f64::from(u8::from(variant.environment)),
        ),
        (
            "incident_radiance_enabled",
            f64::from(u8::from(variant.incident)),
        ),
        (
            "diffuse_occlusion_enabled",
            f64::from(u8::from(variant.diffuse_occlusion)),
        ),
    ];
    let compilation_options = || wgpu::PipelineCompilationOptions {
        constants: &constants,
        ..Default::default()
    };
    let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: Some("reflection source completion"),
        layout: None,
        module: &shader,
        entry_point: Some(MAIN_ENTRY),
        compilation_options: compilation_options(),
        cache: None,
    });
    let compose = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
        label: Some("screen-space reflection composition"),
        layout: None,
        vertex: wgpu::VertexState {
            module: &shader,
            entry_point: Some(shading::FULLSCREEN_VS_ENTRY),
            compilation_options: compilation_options(),
            buffers: &[],
        },
        fragment: Some(wgpu::FragmentState {
            module: &shader,
            entry_point: Some(COMPOSE_SCREEN_SPACE_ENTRY),
            compilation_options: compilation_options(),
            targets: &[Some(wgpu::ColorTargetState {
                format: crate::shading::gbuffer::COLOR,
                blend: Some(wgpu::BlendState {
                    color: wgpu::BlendComponent {
                        src_factor: wgpu::BlendFactor::One,
                        dst_factor: wgpu::BlendFactor::One,
                        operation: wgpu::BlendOperation::Add,
                    },
                    alpha: wgpu::BlendComponent::REPLACE,
                }),
                write_mask: wgpu::ColorWrites::COLOR,
            })],
        }),
        primitive: Default::default(),
        depth_stencil: None,
        multisample: Default::default(),
        multiview_mask: None,
        cache: None,
    });
    Completion {
        group: CachedGroup::new(pipeline.get_bind_group_layout(0)),
        pipeline,
        compose_group: CachedGroup::new(compose.get_bind_group_layout(0)),
        compose,
    }
}

/// Completes the forward pass by adding each receiver's environment specular.
pub(crate) struct ReflectionSource {
    /// Culls the probe collection per screen tile before completion.
    culling: ProbeCulling,
    completion: Completion,
    /// What `completion` is compiled for.
    variant: Variant,
    camera: wgpu::Buffer,
    /// The render size completion covers.
    size: [u32; 2],
    /// Incident radiance for SSR, including source environment/probe
    /// specular: at the render size while completion writes it, else a 1x1
    /// stand-in that completion binds and does not write.
    pub(super) incident: wgpu::TextureView,
    /// Bound as world-space reflections while none are traced.
    no_world: wgpu::TextureView,
}
impl ReflectionSource {
    /// Culling, and completion and composition compiled for `variant`, at
    /// `size`.
    pub fn new(device: &wgpu::Device, size: [u32; 2], variant: Variant) -> Self {
        let mut source = Self {
            culling: ProbeCulling::new(device, size),
            completion: completion(device, variant),
            variant,
            camera: crate::counters::buffer(
                device,
                &wgpu::BufferDescriptor {
                    label: Some("reflection source camera"),
                    size: std::mem::size_of::<SourceCamera>() as u64,
                    usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                },
            ),
            size,
            incident: Self::target(device, [1, 1], "incident radiance"),
            no_world: Self::target(device, [1, 1], "no world-space reflections"),
        };
        source.fit_incident(device);
        source
    }
    fn target(device: &wgpu::Device, size: [u32; 2], label: &str) -> wgpu::TextureView {
        device
            .create_texture(&wgpu::TextureDescriptor {
                label: Some(label),
                size: wgpu::Extent3d {
                    width: size[0],
                    height: size[1],
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: crate::shading::gbuffer::COLOR,
                usage: wgpu::TextureUsages::TEXTURE_BINDING
                    | wgpu::TextureUsages::STORAGE_BINDING
                    | wgpu::TextureUsages::RENDER_ATTACHMENT
                    | wgpu::TextureUsages::COPY_SRC,
                view_formats: &[],
            })
            .create_view(&Default::default())
    }
    /// Rebuilds completion and composition when `variant` changes, and
    /// sizes the incident radiance for it.
    pub fn use_variant(&mut self, device: &wgpu::Device, variant: Variant) {
        if self.variant != variant {
            self.completion = completion(device, variant);
            self.variant = variant;
        }
        self.fit_incident(device);
    }
    pub fn resize(&mut self, device: &wgpu::Device, size: [u32; 2]) {
        self.size = size;
        self.culling.resize(device, size);
        self.fit_incident(device);
    }
    /// The incident radiance completion wrote; `None` when its variant
    /// writes none.
    pub fn incident(&self) -> Option<&wgpu::TextureView> {
        self.variant.incident.then_some(&self.incident)
    }
    /// Reallocates the incident radiance when its size differs from the
    /// variant's: the render size while completion writes it, else 1x1.
    fn fit_incident(&mut self, device: &wgpu::Device) {
        let size = if self.variant.incident {
            self.size
        } else {
            [1, 1]
        };
        let texture = self.incident.texture();
        if [texture.width(), texture.height()] != size {
            self.incident = Self::target(device, size, "incident radiance");
        }
    }
    pub fn encode(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        input: Inputs<'_>,
        timing: Option<&crate::timing::GpuTiming>,
    ) {
        crate::counters::write_buffer(
            queue,
            &self.camera,
            0,
            bytemuck::bytes_of(&SourceCamera::new(input.camera, input.frame_fog)),
        );
        self.culling.encode(
            encoder,
            device,
            queue,
            input.depth,
            input.camera,
            input.environment.collection,
            timing,
        );
        let texture = wgpu::BindingResource::TextureView;
        let fog = input.fog;
        let resources = [
            (0, texture(input.scene)),
            (2, texture(input.output)),
            (3, texture(input.normal)),
            (4, texture(input.f0)),
            (5, texture(input.depth)),
            (6, self.camera.as_entire_binding()),
            (7, texture(input.lookup_tables)),
            (8, texture(input.environment.sky)),
            (
                10,
                wgpu::BindingResource::Sampler(input.environment.sampler),
            ),
            (12, input.environment.parameters.as_entire_binding()),
            (13, texture(input.environment.material)),
            (14, texture(&self.incident)),
            (17, texture(input.anisotropy)),
            (18, texture(input.environment.baked)),
            (19, input.environment.collection.as_entire_binding()),
            (21, self.culling.tiles.as_entire_binding()),
            (22, texture(input.ambient_occlusion)),
            (24, texture(input.ambient)),
            (25, texture(fog.view)),
            (26, wgpu::BindingResource::Sampler(fog.sampler)),
        ];
        let group = self
            .completion
            .group
            .get(device, "reflection source inputs", &resources);
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("reflection source completion"),
            timestamp_writes: timing.and_then(|t| t.compute_pass("reflection source completion")),
        });
        pass.set_pipeline(&self.completion.pipeline);
        pass.set_bind_group(0, group, &[]);
        let [width, height] = self.size;
        pass.dispatch_workgroups(width.div_ceil(8), height.div_ceil(8), 1);
    }

    /// Adds each traced lobe's specular into `input.output`: its split-sum
    /// response times `screen_space` (rgb radiance, a confidence) blended over
    /// the receiver's environment and probe specular, fogged as completion
    /// fogs, with `world_space` filling its misses when world-space rays ran;
    /// where `surface_depth` is nearer than the opaque depth (under a blended
    /// receiver, whose `screen_space` is), the environment and probe
    /// specular alone. `encode` must have run this frame with the same
    /// inputs.
    #[allow(clippy::too_many_arguments)]
    pub fn compose(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        device: &wgpu::Device,
        input: Inputs<'_>,
        surface_depth: &wgpu::TextureView,
        screen_space: &wgpu::TextureView,
        world_space: Option<&wgpu::TextureView>,
        timing: Option<&crate::timing::GpuTiming>,
    ) {
        let world_space = world_space.unwrap_or(&self.no_world);
        let texture = wgpu::BindingResource::TextureView;
        let fog = input.fog;
        let resources = [
            (3, texture(input.normal)),
            (4, texture(input.f0)),
            (5, texture(input.depth)),
            (6, self.camera.as_entire_binding()),
            (7, texture(input.lookup_tables)),
            (8, texture(input.environment.sky)),
            (
                10,
                wgpu::BindingResource::Sampler(input.environment.sampler),
            ),
            (12, input.environment.parameters.as_entire_binding()),
            (13, texture(input.environment.material)),
            (17, texture(input.anisotropy)),
            (18, texture(input.environment.baked)),
            (19, input.environment.collection.as_entire_binding()),
            (20, texture(screen_space)),
            (21, self.culling.tiles.as_entire_binding()),
            (22, texture(input.ambient_occlusion)),
            (23, texture(world_space)),
            (24, texture(input.ambient)),
            (25, texture(fog.view)),
            (26, wgpu::BindingResource::Sampler(fog.sampler)),
            (27, texture(surface_depth)),
        ];
        let group = self.completion.compose_group.get(
            device,
            "screen-space reflection composition",
            &resources,
        );
        let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("screen-space reflection composition"),
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view: input.output,
                depth_slice: None,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Load,
                    store: wgpu::StoreOp::Store,
                },
            })],
            timestamp_writes: timing.and_then(|t| t.render_pass("reflection composition")),
            ..Default::default()
        });
        pass.set_pipeline(&self.completion.compose);
        pass.set_bind_group(0, group, &[]);
        pass.draw(0..3, 0..1);
    }
}

#[cfg(test)]
pub(crate) fn mirrors() -> [crate::shading::layout_tests::Mirror; 2] {
    use crate::shading::layout_tests::mirror;
    [
        mirror!(
            "reflection_source",
            "SourceCamera",
            SourceCamera,
            [inverse, eye, fog, forward, flags]
        ),
        mirror!(
            "reflection_source",
            "ReflectionEnvironment",
            ReflectionEnvironment,
            [yaw, intensity, fade, traced]
        ),
    ]
}

/// The constants with WGSL twins.
#[cfg(test)]
pub(crate) fn constants() -> [crate::shading::layout_tests::Constant; 1] {
    use crate::shading::layout_tests::Constant;
    [Constant::new(
        "reflection_source",
        "SOURCE_FOG",
        naga::Literal::U32(SOURCE_FOG),
    )]
}

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

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

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