nightshade-renderer 0.57.0

GPU-driven wgpu renderer with a built-in frame graph.
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
//! Temporal anti-aliasing resolve pass.

use crate::wgpu::rendergraph::{PassExecutionContext, PassNode};
use wgpu::util::DeviceExt;

#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct TaaParams {
    inv_view_proj: [[f32; 4]; 4],
    prev_view_proj: [[f32; 4]; 4],
    resolution: [f32; 2],
    history_valid: f32,
    blend: f32,
    sharpness: f32,
    use_velocity: f32,
    input_resolution: [f32; 2],
}

/// One camera's temporal history: two targets it ping-pongs between, and
/// whether either holds anything worth blending against yet.
struct CameraHistory {
    /// Retained to keep [`views`](Self::views) alive.
    _textures: Vec<wgpu::Texture>,
    views: Vec<wgpu::TextureView>,
    index: usize,
    valid: bool,
    size: (u32, u32),
}

/// Render-graph pass that blends the current frame against reprojected history
/// to resolve temporal anti-aliasing, ping-ponging between two history targets.
///
/// History is kept per camera. A frame can dispatch several cameras through this
/// pass, and they see different things: two eyes of a stereo pair, or the tiles
/// of a multi-viewport editor. Sharing one history between them resolves each
/// camera against another camera's frames, which reads as everything trailing
/// whenever anything moves.
pub struct TaaPass {
    resolve_pipeline: wgpu::RenderPipeline,
    bind_group_layout: wgpu::BindGroupLayout,
    linear_sampler: wgpu::Sampler,
    point_sampler: wgpu::Sampler,
    params_buffer: wgpu::Buffer,
    format: wgpu::TextureFormat,
    /// Keyed by the camera being dispatched. `None` is the single-view path,
    /// which names no camera and needs a history of its own all the same.
    histories: std::collections::HashMap<Option<nightshade_ecs::Entity>, CameraHistory>,
    dummy_velocity_view: wgpu::TextureView,
}

impl TaaPass {
    /// Builds the resolve pipeline, samplers, and uniform buffer for output
    /// textures of `format`.
    pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
        let shader = crate::wgpu::shader_compose::compile_wgsl(
            device,
            "taa.wgsl",
            include_str!("../../shaders/taa.wgsl"),
        );

        let params = TaaParams {
            inv_view_proj: nalgebra_glm::Mat4::identity().into(),
            prev_view_proj: nalgebra_glm::Mat4::identity().into(),
            resolution: [1920.0, 1080.0],
            history_valid: 0.0,
            blend: 0.12,
            sharpness: 0.5,
            use_velocity: 0.0,
            input_resolution: [1920.0, 1080.0],
        };
        let params_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("TAA Params Buffer"),
            contents: bytemuck::cast_slice(&[params]),
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        });

        let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("TAA Bind Group Layout"),
            entries: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        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,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 3,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 4,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
                    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,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 6,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
            ],
        });

        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("TAA Pipeline Layout"),
            bind_group_layouts: &[Some(&bind_group_layout)],
            immediate_size: 0,
        });

        let target = Some(wgpu::ColorTargetState {
            format,
            blend: None,
            write_mask: wgpu::ColorWrites::ALL,
        });
        let resolve_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("TAA Resolve Pipeline"),
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vertex_main"),
                buffers: &[],
                compilation_options: Default::default(),
            },
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: None,
                unclipped_depth: false,
                polygon_mode: wgpu::PolygonMode::Fill,
                conservative: false,
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fragment_main"),
                targets: &[target.clone(), target],
                compilation_options: Default::default(),
            }),
            multiview_mask: None,
            cache: None,
        });

        let linear_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("TAA Linear Sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
            ..Default::default()
        });
        let point_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("TAA Point Sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Nearest,
            min_filter: wgpu::FilterMode::Nearest,
            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
            ..Default::default()
        });

        let dummy_velocity = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("TAA Dummy Velocity"),
            size: wgpu::Extent3d {
                width: 1,
                height: 1,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rg16Float,
            usage: wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });
        let dummy_velocity_view =
            dummy_velocity.create_view(&wgpu::TextureViewDescriptor::default());

        Self {
            resolve_pipeline,
            bind_group_layout,
            linear_sampler,
            point_sampler,
            params_buffer,
            format,
            histories: std::collections::HashMap::new(),
            dummy_velocity_view,
        }
    }

    /// Ensures `camera` has a history pair at this size, rebuilding it when the
    /// size changed and starting it invalid so the first frame after a resize
    /// blends against nothing.
    fn ensure_history(
        &mut self,
        device: &wgpu::Device,
        camera: Option<nightshade_ecs::Entity>,
        width: u32,
        height: u32,
    ) {
        if self
            .histories
            .get(&camera)
            .is_some_and(|history| history.size == (width, height))
        {
            return;
        }

        let mut textures = Vec::new();
        let mut views = Vec::new();
        for _ in 0..2 {
            let texture = device.create_texture(&wgpu::TextureDescriptor {
                label: Some("TAA History Texture"),
                size: wgpu::Extent3d {
                    width: width.max(1),
                    height: height.max(1),
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: self.format,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                    | wgpu::TextureUsages::TEXTURE_BINDING,
                view_formats: &[],
            });
            let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
            textures.push(texture);
            views.push(view);
        }

        self.histories.insert(
            camera,
            CameraHistory {
                _textures: textures,
                views,
                index: 0,
                valid: false,
                size: (width, height),
            },
        );
    }

    /// Drops history for cameras the frame no longer dispatches, so a closed
    /// viewport or a despawned eye does not hold its targets forever.
    pub fn retain_cameras(&mut self, live: &std::collections::HashSet<nightshade_ecs::Entity>) {
        self.histories
            .retain(|camera, _| camera.is_none_or(|entity| live.contains(&entity)));
    }
}

impl PassNode<crate::wgpu::render_configs::RenderInputs> for TaaPass {
    fn name(&self) -> &str {
        "taa_pass"
    }

    fn reads(&self) -> Vec<&str> {
        vec!["input", "depth"]
    }

    fn optional_reads(&self) -> Vec<&str> {
        vec!["velocity"]
    }

    fn writes(&self) -> Vec<&str> {
        vec!["output"]
    }

    fn execute<'r, 'e>(
        &mut self,
        context: PassExecutionContext<'r, 'e, crate::wgpu::render_configs::RenderInputs>,
    ) -> crate::wgpu::rendergraph::Result<Vec<crate::wgpu::rendergraph::SubGraphRunCommand<'r>>>
    {
        let (width, height) = context
            .get_texture("output")
            .map(|texture| (texture.width(), texture.height()))
            .unwrap_or((1920, 1080));

        let camera = context.configs.view.active_camera;
        self.ensure_history(context.device, camera, width, height);

        // Reproject with the unjittered view-projection the velocity producers
        // use (renderer_state tracks current and previous), so the depth
        // fallback and the motion-vector path land in the same previous space.
        let view_projection = nalgebra_glm::Mat4::from(context.configs.scene.view_projection);
        let inv_view_proj = view_projection
            .try_inverse()
            .unwrap_or_else(nalgebra_glm::Mat4::identity);
        let prev_view_proj = context.configs.scene.prev_view_projection;

        let taa_enabled = context.configs.settings.taa_enabled;
        let taa_blend = context.configs.settings.taa_blend;
        let taa_sharpness = context.configs.settings.taa_sharpness;
        let velocity_slot = context.get_texture_view("velocity").ok();
        let has_velocity = velocity_slot.is_some();
        let velocity_view = velocity_slot.unwrap_or(&self.dummy_velocity_view);
        let (read_index, write_index, history_valid) = self
            .histories
            .get(&camera)
            .map(|history| (history.index, 1 - history.index, history.valid))
            .unwrap_or((0, 1, false));
        let valid = taa_enabled && history_valid;
        let (input_width, input_height) = context
            .get_texture("input")
            .map(|texture| (texture.width(), texture.height()))
            .unwrap_or((width, height));
        let params = TaaParams {
            inv_view_proj: inv_view_proj.into(),
            prev_view_proj,
            resolution: [width.max(1) as f32, height.max(1) as f32],
            history_valid: if valid { 1.0 } else { 0.0 },
            blend: taa_blend,
            sharpness: taa_sharpness,
            use_velocity: if has_velocity && valid { 1.0 } else { 0.0 },
            input_resolution: [input_width.max(1) as f32, input_height.max(1) as f32],
        };
        context
            .queue
            .write_buffer(&self.params_buffer, 0, bytemuck::cast_slice(&[params]));

        let input_view = context.get_texture_view("input")?;
        let (depth_view, _, _) = context.get_depth_attachment("depth")?;

        let bind_group = context
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("TAA Bind Group"),
                layout: &self.bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: wgpu::BindingResource::TextureView(input_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::TextureView(
                            &self.histories[&camera].views[read_index],
                        ),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: wgpu::BindingResource::TextureView(depth_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: wgpu::BindingResource::Sampler(&self.linear_sampler),
                    },
                    wgpu::BindGroupEntry {
                        binding: 4,
                        resource: wgpu::BindingResource::Sampler(&self.point_sampler),
                    },
                    wgpu::BindGroupEntry {
                        binding: 5,
                        resource: self.params_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 6,
                        resource: wgpu::BindingResource::TextureView(velocity_view),
                    },
                ],
            });

        let (output_view, output_load_op, output_store_op) =
            context.get_color_attachment("output")?;

        {
            let mut render_pass = context
                .encoder
                .begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("TAA Resolve Pass"),
                    color_attachments: &[
                        Some(wgpu::RenderPassColorAttachment {
                            view: output_view,
                            resolve_target: None,
                            ops: wgpu::Operations {
                                load: output_load_op,
                                store: output_store_op,
                            },
                            depth_slice: None,
                        }),
                        Some(wgpu::RenderPassColorAttachment {
                            view: &self.histories[&camera].views[write_index],
                            resolve_target: None,
                            ops: wgpu::Operations {
                                load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                                store: wgpu::StoreOp::Store,
                            },
                            depth_slice: None,
                        }),
                    ],
                    depth_stencil_attachment: None,
                    timestamp_writes: None,
                    occlusion_query_set: None,
                    multiview_mask: None,
                });

            render_pass.set_pipeline(&self.resolve_pipeline);
            render_pass.set_bind_group(0, &bind_group, &[]);
            render_pass.draw(0..3, 0..1);
        }

        if let Some(history) = self.histories.get_mut(&camera) {
            history.index = write_index;
            history.valid = true;
        }

        Ok(context.into_sub_graph_commands())
    }
}