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
//! Bounded camera-path heat shimmer, following Sousa, GPU Gems 2 chapter 19.
//! The caller supplies the vector field (the scene's transient heat
//! geometry); this is not physical refractive transport.
use crate::content::transient::HeatDistortion;
use crate::scene::transient::Transient;
use crate::shading::gbuffer::COLOR as HDR;
use crate::shading::vertex::{VertexLayout, vertex_layout};
use crate::view::targets::{attachment, target};

pub(crate) static HEAT: crate::shading::Module = crate::shading::Module {
    name: "heat_distortion",
    source: include_str!("heat.wgsl"),
    deps: &[],
};
/// The entry points the shimmer's pipeline is created with.
pub(crate) const VS_ENTRY: &str = "vs";
pub(crate) const FS_ENTRY: &str = "fs";
/// The heat shimmer's vertex buffer, read by `vs`.
pub(crate) const HEAT_LAYOUT: VertexLayout =
    vertex_layout!(HeatDistortion, [position, displacement, weight]);

pub(crate) struct Heat {
    pipeline: wgpu::RenderPipeline,
    layout: wgpu::BindGroupLayout,
    matrix: wgpu::Buffer,
    source: Option<wgpu::TextureView>,
}
impl Heat {
    pub fn new(device: &wgpu::Device) -> Self {
        let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("heat snapshot layout"),
            entries: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: false },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Depth,
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
            ],
        });
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("bounded heat shimmer"),
            source: wgpu::ShaderSource::Wgsl(crate::shading::compose(&[&HEAT]).into()),
        });
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: None,
            bind_group_layouts: &[Some(&layout)],
            immediate_size: 0,
        });
        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("bounded heat shimmer"),
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some(VS_ENTRY),
                compilation_options: Default::default(),
                buffers: &[Some(HEAT_LAYOUT.buffer)],
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some(FS_ENTRY),
                compilation_options: Default::default(),
                targets: &[Some(wgpu::ColorTargetState {
                    format: HDR,
                    blend: None,
                    write_mask: wgpu::ColorWrites::ALL,
                })],
            }),
            primitive: Default::default(),
            depth_stencil: None,
            multisample: Default::default(),
            multiview_mask: None,
            cache: None,
        });
        Self {
            pipeline,
            layout,
            matrix: crate::counters::buffer_init(
                device,
                &wgpu::util::BufferInitDescriptor {
                    label: Some("heat stable camera"),
                    contents: bytemuck::cast_slice(&glam::Mat4::IDENTITY.to_cols_array()),
                    usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                },
            ),
            source: None,
        }
    }
    #[allow(clippy::too_many_arguments)]
    pub fn encode(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        encoder: &mut wgpu::CommandEncoder,
        transient: &Transient,
        matrix: &[[f32; 4]; 4],
        color: &wgpu::TextureView,
        depth: &wgpu::TextureView,
        timing: Option<&crate::timing::GpuTiming>,
    ) {
        if transient.heat_count == 0 {
            return;
        }
        let size = [color.texture().width(), color.texture().height()];
        if self
            .source
            .as_ref()
            .is_none_or(|s| s.texture().size() != color.texture().size())
        {
            self.source = Some(target(device, "immutable heat source", size, HDR));
        }
        let source = self.source.as_ref().unwrap();
        crate::counters::write_buffer(queue, &self.matrix, 0, bytemuck::bytes_of(matrix));
        encoder.copy_texture_to_texture(
            color.texture().as_image_copy(),
            source.texture().as_image_copy(),
            color.texture().size(),
        );
        let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("heat current source and depth"),
            layout: &self.layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: self.matrix.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(source),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::TextureView(depth),
                },
            ],
        });
        let mut attachment = attachment(color).unwrap();
        attachment.ops.load = wgpu::LoadOp::Load;
        let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("camera heat before bloom and HUD"),
            color_attachments: &[Some(attachment)],
            timestamp_writes: timing.and_then(|t| t.render_pass("heat distortion")),
            ..Default::default()
        });
        pass.set_pipeline(&self.pipeline);
        pass.set_bind_group(0, &group, &[]);
        pass.set_vertex_buffer(0, transient.heat.slice(..));
        pass.draw(0..transient.heat_count, 0..1);
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use crate::content::transient::MAX_VERTICES;
    // Defects: wrong displacement scale/orientation, feedback, foreground bleed,
    // viewport clamping, stale resize resources and changes outside coverage.
    // Oracle: authored linear ramp and independently rasterized foreground stripe.
    #[test]
    fn bounded_sampling() {
        let Some((device, queue)) = crate::test_support::device() else {
            return;
        };
        pollster::block_on(async {
            let mut heat = Heat::new(&device);
            let mut transient = Transient::new(&device);
            let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, source: wgpu::ShaderSource::Wgsl("@vertex fn vs(@builtin(vertex_index) i:u32)->@builtin(position) vec4<f32> {let p=vec2(f32((i<<1u)&2u),f32(i&2u));return vec4(p*2.-1.,.75,1.);}".into()) });
            let depth_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                label: None,
                layout: None,
                vertex: wgpu::VertexState {
                    module: &shader,
                    entry_point: Some("vs"),
                    compilation_options: Default::default(),
                    buffers: &[],
                },
                fragment: None,
                primitive: Default::default(),
                depth_stencil: Some(wgpu::DepthStencilState {
                    format: wgpu::TextureFormat::Depth32Float,
                    depth_write_enabled: Some(true),
                    depth_compare: Some(wgpu::CompareFunction::Always),
                    stencil: Default::default(),
                    bias: Default::default(),
                }),
                multisample: Default::default(),
                multiview_mask: None,
                cache: None,
            });
            for size in [[32, 16], [48, 24]] {
                let color = target(&device, "ramp", size, HDR);
                let depth = target(
                    &device,
                    "foreground stripe",
                    size,
                    wgpu::TextureFormat::Depth32Float,
                );
                let pixels: Vec<u16> = (0..size[1])
                    .flat_map(|y| {
                        (0..size[0]).flat_map(move |x| {
                            [0x3c00 + x as u16 * 16, 0x3c00 + y as u16 * 16, 0, 0x3c00]
                        })
                    })
                    .collect();
                for (offset, weight, clear) in [
                    ([2.5_f32, 1.5_f32], 1., false),
                    ([-2.5, -1.5], 1., false),
                    ([0., 0.], 1., false),
                    ([2.5, 1.5], 0., false),
                    ([32., 32.], 1., false),
                    ([2.5, 1.5], 1., true),
                ] {
                    queue.write_texture(
                        color.texture().as_image_copy(),
                        bytemuck::cast_slice(&pixels),
                        wgpu::TexelCopyBufferLayout {
                            offset: 0,
                            bytes_per_row: Some(size[0] * 8),
                            rows_per_image: Some(size[1]),
                        },
                        color.texture().size(),
                    );
                    // Left three quarters, middle half vertically, at device depth .5.
                    let vertices = [
                        [-1., -0.5, 0.5],
                        [0.5, -0.5, 0.5],
                        [-1., 0.5, 0.5],
                        [-1., 0.5, 0.5],
                        [0.5, -0.5, 0.5],
                        [0.5, 0.5, 0.5],
                    ]
                    .map(|position| HeatDistortion {
                        position,
                        displacement: offset,
                        weight,
                    });
                    transient.update_heat(&queue, &vertices).unwrap();
                    // Failed replacement must not clear or partially overwrite a live list.
                    assert!(
                        transient
                            .update_heat(&queue, &vec![vertices[0]; MAX_VERTICES + 3])
                            .is_err()
                    );
                    if clear {
                        transient.update_heat(&queue, &[]).unwrap();
                    }
                    let mut encoder = device.create_command_encoder(&Default::default());
                    {
                        let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                            color_attachments: &[],
                            depth_stencil_attachment: Some(
                                wgpu::RenderPassDepthStencilAttachment {
                                    view: &depth,
                                    depth_ops: Some(wgpu::Operations {
                                        load: wgpu::LoadOp::Clear(0.2),
                                        store: wgpu::StoreOp::Store,
                                    }),
                                    stencil_ops: None,
                                },
                            ),
                            ..Default::default()
                        });
                        pass.set_pipeline(&depth_pipeline);
                        pass.set_scissor_rect(10, 0, 1, size[1]);
                        pass.draw(0..3, 0..1);
                    }
                    heat.encode(
                        &device,
                        &queue,
                        &mut encoder,
                        &transient,
                        &glam::Mat4::IDENTITY.to_cols_array_2d(),
                        &color,
                        &depth,
                        None,
                    );
                    queue.submit([encoder.finish()]);
                    let actual = crate::test_support::read(&device, &queue, color.texture(), 8);
                    for y in 0..size[1] {
                        for x in 0..size[0] {
                            let i = ((y * size[0] + x) * 8) as usize;
                            let changed = weight > 0.
                                && !clear
                                && offset[0].abs() == 2.5
                                && x < size[0] * 3 / 4
                                && y >= size[1] / 4
                                && y < size[1] * 3 / 4
                                && x != 10
                                && if offset[0] > 0. {
                                    x != 7 && x != 8
                                } else {
                                    x >= 3 && x != 12 && x != 13
                                };
                            if changed {
                                let red = crate::test_support::half(&actual[i..]);
                                let green = crate::test_support::half(&actual[i + 2..]);
                                assert!(
                                    (red - (1. + (x as f32 + offset[0]) / 64.)).abs() < 0.001,
                                    "red ({x},{y}): {red}"
                                );
                                assert!(
                                    (green - (1. + (y as f32 + offset[1]) / 64.)).abs() < 0.001,
                                    "green ({x},{y}): {green}"
                                );
                            } else {
                                assert_eq!(
                                    &actual[i..i + 8],
                                    &bytemuck::cast_slice::<_, u8>(&pixels)[i..i + 8],
                                    "preserve ({x},{y}) offset {offset:?} weight {weight}"
                                );
                            }
                        }
                    }
                }
                transient.update_heat(&queue, &[]).unwrap();
            }
        });
    }

    // Defect: heat distorting anything but the complete composed frame: run
    // before a transparent draw (glow or mist would land on the composite
    // unwarped) or on another target than the composite that exposure and
    // antialiasing read (the frame would not warp). Oracle: the same frame
    // without heat, its composite shifted by the authored two pixels wherever
    // the heat covers it and its footprint stays on screen.
    #[test]
    fn heat_warps_the_composite_after_its_transparent_draws() {
        use crate::effects::Glow;
        use crate::settings::{Antialiasing, Bloom, SceneResolution, Settings};
        use crate::{Camera, FrameInput, Mist, Renderer, Scene};
        use glam::{Mat4, Vec3};
        let Some((device, queue)) = crate::test_support::device() else {
            return;
        };
        let size = [32, 24];
        let mut scene = Scene::new(&device, &queue);
        // Glow over the whole frame, its red growing to the right.
        let glow = [
            ([-20., -20., -2.], 0.),
            ([60., -20., -2.], 4.),
            ([-20., 60., -2.], 0.),
        ]
        .map(|(position, red)| Glow {
            position,
            color: [red, 0., 0., 1.],
            ..Glow::default()
        });
        scene.update_effects(&device, &queue, &glow);
        // One mist billboard across the middle of the frame.
        scene.update_mist(&device, &queue, &[[0., 0., -2.]]);
        // Heat over the whole frame, shifting it two pixels.
        let heat = [[-10., -10., -1.], [30., -10., -1.], [-10., 30., -1.]].map(|position| {
            HeatDistortion {
                position,
                displacement: [2., 0.],
                weight: 1.,
            }
        });
        scene.update_heat_distortion(&queue, &heat).unwrap();
        let mut input = FrameInput::new(Camera {
            view: Mat4::IDENTITY,
            projection: crate::perspective(
                std::f32::consts::FRAC_PI_2,
                size[0] as f32 / size[1] as f32,
                0.1,
            ),
            eye: Vec3::ZERO,
        });
        input.camera_cut = true;
        input.atmosphere = true;
        input.mist = Mist {
            thin_color: [0., 0., 1.],
            dense_color: [0., 4., 0.],
            opacity: 1.,
            width: 3.,
            height: 3.,
            ..Mist::default()
        };
        let settings = Settings {
            scene_resolution: SceneResolution::Full,
            antialiasing: Antialiasing::Off,
            bloom: Bloom::Off,
            ..Settings::default()
        };
        let mut renderer = Renderer::for_test(&device, &queue, size, &settings);
        let output = crate::view::targets::target(
            &device,
            "caller output",
            size,
            crate::shading::gbuffer::COLOR,
        );
        let mut composite = |heat_distortion| {
            let settings = Settings {
                heat_distortion,
                ..settings
            };
            let mut encoder = device.create_command_encoder(&Default::default());
            renderer.render(
                &device,
                &queue,
                &mut encoder,
                &mut scene,
                &input,
                &settings,
                &output,
                None,
            );
            queue.submit([encoder.finish()]);
            renderer.finish_frame(&mut scene);
            crate::test_support::read(&device, &queue, renderer.targets().composite.texture(), 8)
        };
        let unwarped = composite(false);
        let warped = composite(true);
        let texel = |frame: &[u8], x: u32, y: u32| {
            let i = ((y * size[0] + x) * 8) as usize;
            frame[i..i + 8].to_vec()
        };
        // A misplaced heat shows only where the frame varies across it.
        let channel = |x, channel: usize| {
            crate::test_support::half(&texel(&unwarped, x, size[1] / 2)[channel * 2..])
        };
        assert!(
            channel(4, 0) < channel(28, 0),
            "the glow must brighten to the right"
        );
        assert!(channel(16, 1) > 0., "the mist must show");
        for y in 0..size[1] {
            for x in 0..size[0] {
                let source = if x + 2 < size[0] { x + 2 } else { x };
                assert_eq!(
                    texel(&warped, x, y),
                    texel(&unwarped, source, y),
                    "({x},{y}) is not the unwarped frame's ({source},{y})"
                );
            }
        }
    }
}