ponko 0.0.1

A fast 2D game engine for simulating millions of things: ECS on all cores, hot reload, an in-game inspector, AI-agent control, and a C++ API.
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
//! Temporal antialiasing of the lit HDR frame (see `taa.wgsl`): the sub-pixel
//! jitter sequence, the history images, the motion vector target and the
//! resolve pass.
//!
//! The resolve reads an *input* image (the frame as the 3D passes resolved
//! it), the history and the motion vectors, and writes an *output*: the new
//! history and the frame post processing goes on with. Input and output sizes
//! are separate numbers throughout, so a lower-resolution input can later
//! be resolved up to the output's size; for now the renderer uses one size
//! for both.

use glam::Mat4;

use super::gpu3d::HDR_FORMAT;

/// How many positions the jitter visits before it repeats.
pub(super) const JITTER_SAMPLES: u32 = 16;

/// Object-only backward UV motion, added to depth-derived camera motion.
/// Zero for static surfaces; MSAA resolves coverage without a validity channel.
pub(super) const VELOCITY_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rg16Float;

/// The `index`th number of the Halton sequence in `base`, in 0..1.
pub(super) fn halton(mut index: u32, base: u32) -> f32 {
    let (mut result, mut f) = (0.0, 1.0 / base as f32);
    while index > 0 {
        result += f * (index % base) as f32;
        index /= base;
        f /= base as f32;
    }
    result
}

/// Frame `frame`'s jitter in pixels, each in -0.5..0.5: Halton (2, 3), the
/// sequence's zeroth element skipped.
pub(super) fn jitter(frame: u32) -> [f32; 2] {
    let i = frame % JITTER_SAMPLES + 1;
    [halton(i, 2) - 0.5, halton(i, 3) - 0.5]
}

/// `view_proj` with the sub-pixel `jitter` (pixels, +x right, +y down) of a
/// `size` image applied after projection.
pub(super) fn jittered(view_proj: Mat4, jitter: [f32; 2], size: (u32, u32)) -> Mat4 {
    let ndc = glam::Vec3::new(
        2.0 * jitter[0] / size.0.max(1) as f32,
        -2.0 * jitter[1] / size.1.max(1) as f32,
        0.0,
    );
    Mat4::from_translation(ndc) * view_proj
}

/// What the resolve shader reads (`Taa` in `taa.wgsl`).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct TaaUniform {
    inv_view_proj: [[f32; 4]; 4],
    view_proj: [[f32; 4]; 4],
    previous_view_proj: [[f32; 4]; 4],
    input_size: [f32; 4],
    output_size: [f32; 4],
    params: [f32; 4],
}

/// One frame's resolve settings.
pub(super) struct Resolve {
    /// The jittered camera (the one the frame was drawn with), unjittered
    /// and the unjittered one of the last frame.
    pub jittered: Mat4,
    pub current: Mat4,
    pub previous: Mat4,
    pub input_size: (u32, u32),
    pub output_size: (u32, u32),
    pub sharpness: f32,
    /// Ignore the history (first frame, camera cut, resize).
    pub reset: bool,
}

/// How much of a still frame is blended into the history each frame.
const STILL_BLEND: f32 = 0.1;

/// The resolve pass and its pipeline.
pub(super) struct Taa {
    layout: wgpu::BindGroupLayout,
    pipeline: wgpu::RenderPipeline,
    uniform: wgpu::Buffer,
    sampler: wgpu::Sampler,
}

/// The images temporal antialiasing needs, as big as the window.
pub(super) struct TaaTargets {
    /// The lit frame the 3D passes resolve into (instead of `hdr`).
    pub input: wgpu::Texture,
    pub input_view: wgpu::TextureView,
    history: [wgpu::TextureView; 2],
    /// `groups[i]` reads history `i`.
    groups: [wgpu::BindGroup; 2],
}

/// Shared by TAA and temporal AO, without allocating TAA color histories.
pub(super) struct VelocityTargets {
    /// Multisampled motion of moved models, resolved into `velocity`.
    pub velocity_color: wgpu::TextureView,
    velocity: wgpu::TextureView,
    #[cfg(test)]
    pub velocity_texture: wgpu::Texture,
}
impl VelocityTargets {
    pub fn velocity(&self) -> &wgpu::TextureView {
        &self.velocity
    }
    pub fn new(device: &wgpu::Device, size: (u32, u32), samples: u32) -> Self {
        let make = |samples, usage| {
            device.create_texture(&wgpu::TextureDescriptor {
                label: Some("object velocity"),
                size: wgpu::Extent3d {
                    width: size.0,
                    height: size.1,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: samples,
                dimension: wgpu::TextureDimension::D2,
                format: VELOCITY_FORMAT,
                usage,
                view_formats: &[],
            })
        };
        let velocity_texture = make(
            1,
            wgpu::TextureUsages::RENDER_ATTACHMENT
                | wgpu::TextureUsages::TEXTURE_BINDING
                | wgpu::TextureUsages::COPY_SRC,
        );
        let velocity = velocity_texture.create_view(&Default::default());
        let velocity_color = make(samples, wgpu::TextureUsages::RENDER_ATTACHMENT).create_view(&Default::default());
        Self {
            velocity,
            velocity_color,
            #[cfg(test)]
            velocity_texture,
        }
    }
}

impl Taa {
    pub fn new(device: &wgpu::Device) -> Self {
        let texture = |binding, depth| wgpu::BindGroupLayoutEntry {
            binding,
            visibility: wgpu::ShaderStages::FRAGMENT,
            ty: wgpu::BindingType::Texture {
                sample_type: if depth {
                    wgpu::TextureSampleType::Depth
                } else {
                    wgpu::TextureSampleType::Float { filterable: true }
                },
                view_dimension: wgpu::TextureViewDimension::D2,
                multisampled: depth,
            },
            count: None,
        };
        let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("TAA layout"),
            entries: &[
                texture(0, false),
                texture(1, false),
                texture(2, false),
                texture(3, true),
                wgpu::BindGroupLayoutEntry {
                    binding: 4,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 5,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
            ],
        });
        let shader = device.create_shader_module(wgpu::include_wgsl!("taa.wgsl"));
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("TAA pipeline layout"),
            bind_group_layouts: &[Some(&layout)],
            immediate_size: 0,
        });
        let target = Some(wgpu::ColorTargetState {
            format: HDR_FORMAT,
            blend: None,
            write_mask: wgpu::ColorWrites::ALL,
        });
        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("TAA resolve"),
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_full"),
                compilation_options: Default::default(),
                buffers: &[],
            },
            primitive: wgpu::PrimitiveState::default(),
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_resolve"),
                compilation_options: Default::default(),
                targets: &[target.clone(), target],
            }),
            multiview_mask: None,
            cache: None,
        });
        let uniform = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("TAA"),
            size: size_of::<TaaUniform>() as u64,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("TAA history"),
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });
        Self {
            layout,
            pipeline,
            uniform,
            sampler,
        }
    }

    /// Images for a `size` window, reading `depth` (the multisampled depth
    /// the frame was drawn with).
    pub fn targets(
        &self,
        device: &wgpu::Device,
        size: (u32, u32),
        velocity: &wgpu::TextureView,
        depth: &wgpu::TextureView,
    ) -> TaaTargets {
        let make = |label, format, samples, usage| {
            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: samples,
                dimension: wgpu::TextureDimension::D2,
                format,
                usage,
                view_formats: &[],
            })
        };
        let render = wgpu::TextureUsages::RENDER_ATTACHMENT;
        let bound = render | wgpu::TextureUsages::TEXTURE_BINDING;
        let input = make("TAA input", HDR_FORMAT, 1, bound);
        let input_view = input.create_view(&Default::default());
        let history = [
            make("TAA history 0", HDR_FORMAT, 1, bound).create_view(&Default::default()),
            make("TAA history 1", HDR_FORMAT, 1, bound).create_view(&Default::default()),
        ];
        let group = |history: &wgpu::TextureView| {
            device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("TAA"),
                layout: &self.layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: wgpu::BindingResource::TextureView(&input_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::TextureView(history),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: wgpu::BindingResource::TextureView(velocity),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: wgpu::BindingResource::TextureView(depth),
                    },
                    wgpu::BindGroupEntry {
                        binding: 4,
                        resource: wgpu::BindingResource::Sampler(&self.sampler),
                    },
                    wgpu::BindGroupEntry {
                        binding: 5,
                        resource: self.uniform.as_entire_binding(),
                    },
                ],
            })
        };
        let groups = [group(&history[0]), group(&history[1])];
        TaaTargets {
            input,
            input_view,
            history,
            groups,
        }
    }

    pub fn prepare(&self, queue: &wgpu::Queue, resolve: &Resolve) {
        let size = |(w, h): (u32, u32)| {
            let (w, h) = (w.max(1) as f32, h.max(1) as f32);
            [w, h, 1.0 / w, 1.0 / h]
        };
        queue.write_buffer(
            &self.uniform,
            0,
            bytemuck::bytes_of(&TaaUniform {
                inv_view_proj: resolve.jittered.inverse().to_cols_array_2d(),
                view_proj: resolve.current.to_cols_array_2d(),
                previous_view_proj: resolve.previous.to_cols_array_2d(),
                input_size: size(resolve.input_size),
                output_size: size(resolve.output_size),
                params: [
                    resolve.sharpness.clamp(0.0, 1.0),
                    STILL_BLEND,
                    f32::from(resolve.reset),
                    0.0,
                ],
            }),
        );
    }

    /// Resolves the input and `read` history into history `1 - read` and
    /// `hdr`.
    pub fn draw(
        &self,
        encoder: &mut wgpu::CommandEncoder,
        targets: &TaaTargets,
        read: usize,
        hdr: &wgpu::TextureView,
        timestamps: Option<wgpu::RenderPassTimestampWrites<'_>>,
    ) {
        let attachment = |view| {
            Some(wgpu::RenderPassColorAttachment {
                view,
                depth_slice: None,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                    store: wgpu::StoreOp::Store,
                },
            })
        };
        let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("TAA resolve"),
            color_attachments: &[attachment(&targets.history[1 - read]), attachment(hdr)],
            depth_stencil_attachment: None,
            timestamp_writes: timestamps,
            occlusion_query_set: None,
            multiview_mask: None,
        });
        pass.set_pipeline(&self.pipeline);
        pass.set_bind_group(0, &targets.groups[read], &[]);
        pass.draw(0..3, 0..1);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn halton_covers_the_pixel_without_repeating() {
        let points: Vec<[f32; 2]> = (0..JITTER_SAMPLES).map(jitter).collect();
        for p in &points {
            assert!(p[0].abs() < 0.5 && p[1].abs() < 0.5, "{p:?}");
        }
        for (i, a) in points.iter().enumerate() {
            for b in &points[i + 1..] {
                assert!(a != b);
            }
        }
        let mean = points.iter().fold([0.0, 0.0], |m, p| [m[0] + p[0], m[1] + p[1]]);
        assert!(mean[0].abs() / (JITTER_SAMPLES as f32) < 0.05, "{mean:?}");
        assert_eq!(jitter(0), jitter(JITTER_SAMPLES));
        assert_eq!(halton(1, 2), 0.5);
        assert_eq!(halton(3, 2), 0.75);
    }

    #[test]
    fn the_resolve_shader_is_valid() {
        super::super::gpu3d::check_shader("taa.wgsl", include_str!("taa.wgsl")).unwrap();
    }

    #[test]
    fn jitter_moves_the_image_by_the_requested_pixels() {
        let view_proj = glam::camera::rh::proj::directx::perspective(1.0, 1.0, 0.1, 10.0);
        let size = (100, 50);
        let p = glam::Vec3::new(0.3, -0.2, -3.0);
        let before = view_proj.project_point3(p);
        let after = jittered(view_proj, [0.5, -0.25], size).project_point3(p);
        // +x is right: half a pixel is a hundredth of the width in NDC.
        assert!((after.x - before.x - 2.0 * 0.5 / 100.0).abs() < 1e-6);
        // +y is down in pixels, up in NDC: -0.25 px is 0.25 / 50 * 2 up.
        assert!((after.y - before.y - 2.0 * 0.25 / 50.0).abs() < 1e-6);
        assert!((after.z - before.z).abs() < 1e-6);
    }
}