scena 1.7.2

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
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
use super::instancing::{INSTANCE_ATTRIBUTES, INSTANCE_BYTE_LEN, InstanceDrawBatch};
use super::material_bindings::MaterialTextureBindingMode;
use super::material_uniform::MATERIAL_UNIFORM_ENTRY_STRIDE;
use super::materials::MaterialResources;
use super::output::{
    DRAW_UNIFORM_ENTRY_STRIDE, GPU_TRIANGLE_SHADER, GPU_TRIANGLE_SHADER_TEXTURE_2D,
};
use super::vertices::{PrimitiveDrawBatch, VERTEX_ATTRIBUTES, VERTEX_BYTE_LEN};

pub(super) const BYTES_PER_PIXEL: u32 = 4;
pub(super) const GPU_COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
/// WGPU evaluates front-face winding before viewport conversion; prepared
/// triangle order that is front-facing in the CPU rasterizer maps to CCW here.
pub(super) const SCENA_FRONT_FACE: wgpu::FrontFace = wgpu::FrontFace::Ccw;

pub(super) struct UnlitPass<'a> {
    pub(super) view: &'a wgpu::TextureView,
    pub(super) resolve_target: Option<&'a wgpu::TextureView>,
    pub(super) depth_view: Option<&'a wgpu::TextureView>,
    pub(super) vertex_buffer: &'a wgpu::Buffer,
    pub(super) instance_buffer: &'a wgpu::Buffer,
    pub(super) output_bind_group: &'a wgpu::BindGroup,
    pub(super) draw_bind_group: &'a wgpu::BindGroup,
    pub(super) material_resources: &'a MaterialResources,
    pub(super) draw_batches: &'a [PrimitiveDrawBatch],
    pub(super) instance_batches: &'a [InstanceDrawBatch],
    pub(super) identity_instance: u32,
    pub(super) pipelines: UnlitPipelines<'a>,
    pub(super) color_load: ColorLoad,
    pub(super) draw_filter: DrawFilter,
    pub(super) label: &'static str,
    pub(super) draw_submissions: &'a mut u64,
}

#[derive(Debug)]
pub(super) struct MeshPipelineSet {
    single_sided: wgpu::RenderPipeline,
    double_sided: wgpu::RenderPipeline,
}

#[derive(Debug, Clone, Copy)]
pub(super) struct UnlitPipelines<'a> {
    single_sided: &'a wgpu::RenderPipeline,
    double_sided: &'a wgpu::RenderPipeline,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) enum ColorLoad {
    Clear(wgpu::Color),
    Load,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum DrawFilter {
    All,
    OpaqueOnly,
    TransparentOnly,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum DrawSideFilter {
    SingleSided,
    DoubleSided,
}

pub(super) fn encode_unlit_pass(encoder: &mut wgpu::CommandEncoder, inputs: UnlitPass<'_>) {
    let color_attachment = Some(wgpu::RenderPassColorAttachment {
        view: inputs.view,
        depth_slice: None,
        resolve_target: inputs.resolve_target,
        ops: wgpu::Operations {
            load: match inputs.color_load {
                ColorLoad::Clear(color) => wgpu::LoadOp::Clear(color),
                ColorLoad::Load => wgpu::LoadOp::Load,
            },
            store: wgpu::StoreOp::Store,
        },
    });
    let depth_stencil_attachment =
        inputs
            .depth_view
            .map(|view| wgpu::RenderPassDepthStencilAttachment {
                view,
                depth_ops: Some(wgpu::Operations {
                    load: wgpu::LoadOp::Load,
                    store: wgpu::StoreOp::Store,
                }),
                stencil_ops: None,
            });
    let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some(inputs.label),
        color_attachments: &[color_attachment],
        depth_stencil_attachment,
        timestamp_writes: None,
        occlusion_query_set: None,
        multiview_mask: None,
    });
    pass.set_bind_group(0, inputs.output_bind_group, &[]);
    pass.set_vertex_buffer(0, inputs.vertex_buffer.slice(..));
    let identity_instance_offset =
        u64::from(inputs.identity_instance).saturating_mul(INSTANCE_BYTE_LEN as u64);
    pass.set_vertex_buffer(1, inputs.instance_buffer.slice(identity_instance_offset..));
    for side_filter in [DrawSideFilter::SingleSided, DrawSideFilter::DoubleSided] {
        pass.set_pipeline(inputs.pipelines.for_side(side_filter));
        match inputs.material_resources {
            MaterialResources::PerMaterial(slots) => {
                let Some(fallback_material) = slots.first() else {
                    return;
                };
                for batch in inputs.draw_batches.iter().filter(|batch| {
                    inputs.draw_filter.includes(batch) && side_filter.includes(batch)
                }) {
                    let material = slots
                        .get(batch.material_slot as usize)
                        .unwrap_or(fallback_material);
                    // Plan line 778 commit 2: per-material bind groups always
                    // bind their own uniform buffer at offset 0; the layer
                    // index in MaterialUniform stays at 0 because each material
                    // owns a 1-layer array.
                    pass.set_bind_group(1, &material.bind_group, &[0]);
                    let draw_offset = (batch.draw_uniform_index as u64)
                        .saturating_mul(DRAW_UNIFORM_ENTRY_STRIDE)
                        as u32;
                    pass.set_bind_group(2, inputs.draw_bind_group, &[draw_offset]);
                    pass.draw(
                        batch.start_vertex..batch.start_vertex.saturating_add(batch.vertex_count),
                        0..1,
                    );
                    *inputs.draw_submissions = inputs.draw_submissions.saturating_add(1);
                }
                for batch in inputs.instance_batches.iter().filter(|batch| {
                    inputs.draw_filter.includes_instance(batch)
                        && side_filter.includes_instance(batch)
                }) {
                    let material = slots
                        .get(batch.material_slot as usize)
                        .unwrap_or(fallback_material);
                    pass.set_bind_group(1, &material.bind_group, &[0]);
                    let draw_offset = (batch.draw_uniform_index as u64)
                        .saturating_mul(DRAW_UNIFORM_ENTRY_STRIDE)
                        as u32;
                    pass.set_bind_group(2, inputs.draw_bind_group, &[draw_offset]);
                    let instance_offset =
                        u64::from(batch.start_instance).saturating_mul(INSTANCE_BYTE_LEN as u64);
                    pass.set_vertex_buffer(1, inputs.instance_buffer.slice(instance_offset..));
                    pass.draw(
                        batch.start_vertex..batch.start_vertex.saturating_add(batch.vertex_count),
                        0..batch.instance_count,
                    );
                    *inputs.draw_submissions = inputs.draw_submissions.saturating_add(1);
                }
            }
            MaterialResources::Batched(batched) => {
                // Plan line 778 commit 2: a single bind group reused for every
                // draw; per-draw dynamic offset selects the per-material uniform
                // slot, and `material_layer_index` (encoded in the uniform)
                // selects the array layer for sampling.
                for batch in inputs.draw_batches.iter().filter(|batch| {
                    inputs.draw_filter.includes(batch) && side_filter.includes(batch)
                }) {
                    let layer_index = (batch.material_slot as u64)
                        .min(u64::from(batched.layer_count.saturating_sub(1)));
                    let material_offset =
                        layer_index.saturating_mul(MATERIAL_UNIFORM_ENTRY_STRIDE) as u32;
                    pass.set_bind_group(1, &batched.bind_group, &[material_offset]);
                    let draw_offset = (batch.draw_uniform_index as u64)
                        .saturating_mul(DRAW_UNIFORM_ENTRY_STRIDE)
                        as u32;
                    pass.set_bind_group(2, inputs.draw_bind_group, &[draw_offset]);
                    pass.draw(
                        batch.start_vertex..batch.start_vertex.saturating_add(batch.vertex_count),
                        0..1,
                    );
                    *inputs.draw_submissions = inputs.draw_submissions.saturating_add(1);
                }
                for batch in inputs.instance_batches.iter().filter(|batch| {
                    inputs.draw_filter.includes_instance(batch)
                        && side_filter.includes_instance(batch)
                }) {
                    let layer_index = (batch.material_slot as u64)
                        .min(u64::from(batched.layer_count.saturating_sub(1)));
                    let material_offset =
                        layer_index.saturating_mul(MATERIAL_UNIFORM_ENTRY_STRIDE) as u32;
                    pass.set_bind_group(1, &batched.bind_group, &[material_offset]);
                    let draw_offset = (batch.draw_uniform_index as u64)
                        .saturating_mul(DRAW_UNIFORM_ENTRY_STRIDE)
                        as u32;
                    pass.set_bind_group(2, inputs.draw_bind_group, &[draw_offset]);
                    let instance_offset =
                        u64::from(batch.start_instance).saturating_mul(INSTANCE_BYTE_LEN as u64);
                    pass.set_vertex_buffer(1, inputs.instance_buffer.slice(instance_offset..));
                    pass.draw(
                        batch.start_vertex..batch.start_vertex.saturating_add(batch.vertex_count),
                        0..batch.instance_count,
                    );
                    *inputs.draw_submissions = inputs.draw_submissions.saturating_add(1);
                }
            }
        }
    }
}

impl MeshPipelineSet {
    pub(super) const fn refs(&self) -> UnlitPipelines<'_> {
        UnlitPipelines {
            single_sided: &self.single_sided,
            double_sided: &self.double_sided,
        }
    }
}

impl<'a> UnlitPipelines<'a> {
    const fn for_side(self, side_filter: DrawSideFilter) -> &'a wgpu::RenderPipeline {
        match side_filter {
            DrawSideFilter::SingleSided => self.single_sided,
            DrawSideFilter::DoubleSided => self.double_sided,
        }
    }
}

impl DrawFilter {
    fn includes(self, batch: &PrimitiveDrawBatch) -> bool {
        match self {
            DrawFilter::All => true,
            DrawFilter::OpaqueOnly => batch.depth_prepass_eligible,
            DrawFilter::TransparentOnly => !batch.depth_prepass_eligible,
        }
    }

    fn includes_instance(self, batch: &InstanceDrawBatch) -> bool {
        match self {
            DrawFilter::All => true,
            DrawFilter::OpaqueOnly => batch.depth_prepass_eligible,
            DrawFilter::TransparentOnly => !batch.depth_prepass_eligible,
        }
    }
}

impl DrawSideFilter {
    pub(super) const fn includes(self, batch: &PrimitiveDrawBatch) -> bool {
        self.includes_double_sided(batch.double_sided)
    }

    pub(super) const fn includes_instance(self, batch: &InstanceDrawBatch) -> bool {
        self.includes_double_sided(batch.double_sided)
    }

    const fn includes_double_sided(self, double_sided: bool) -> bool {
        match self {
            Self::SingleSided => !double_sided,
            Self::DoubleSided => double_sided,
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub(super) fn create_unlit_pipeline_set(
    device: &wgpu::Device,
    format: wgpu::TextureFormat,
    output_bind_group_layout: &wgpu::BindGroupLayout,
    material_bind_group_layout: &wgpu::BindGroupLayout,
    draw_bind_group_layout: &wgpu::BindGroupLayout,
    texture_binding_mode: MaterialTextureBindingMode,
    depth_compare: Option<wgpu::CompareFunction>,
    sample_count: u32,
) -> MeshPipelineSet {
    MeshPipelineSet {
        single_sided: create_unlit_pipeline(
            device,
            format,
            output_bind_group_layout,
            material_bind_group_layout,
            draw_bind_group_layout,
            texture_binding_mode,
            depth_compare,
            false,
            sample_count,
        ),
        double_sided: create_unlit_pipeline(
            device,
            format,
            output_bind_group_layout,
            material_bind_group_layout,
            draw_bind_group_layout,
            texture_binding_mode,
            depth_compare,
            true,
            sample_count,
        ),
    }
}

#[allow(clippy::too_many_arguments)]
fn create_unlit_pipeline(
    device: &wgpu::Device,
    format: wgpu::TextureFormat,
    output_bind_group_layout: &wgpu::BindGroupLayout,
    material_bind_group_layout: &wgpu::BindGroupLayout,
    draw_bind_group_layout: &wgpu::BindGroupLayout,
    texture_binding_mode: MaterialTextureBindingMode,
    depth_compare: Option<wgpu::CompareFunction>,
    double_sided: bool,
    sample_count: u32,
) -> wgpu::RenderPipeline {
    let shader_source = match texture_binding_mode {
        MaterialTextureBindingMode::Texture2d => GPU_TRIANGLE_SHADER_TEXTURE_2D,
        MaterialTextureBindingMode::Texture2dArray => GPU_TRIANGLE_SHADER,
    };
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("scena.m0.unlit_triangle"),
        source: wgpu::ShaderSource::Wgsl(shader_source.into()),
    });
    let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some("scena.m0.pipeline_layout"),
        bind_group_layouts: &[
            Some(output_bind_group_layout),
            Some(material_bind_group_layout),
            Some(draw_bind_group_layout),
        ],
        immediate_size: 0,
    });
    let vertex_buffer = wgpu::VertexBufferLayout {
        array_stride: VERTEX_BYTE_LEN as u64,
        step_mode: wgpu::VertexStepMode::Vertex,
        attributes: &VERTEX_ATTRIBUTES,
    };
    let instance_buffer = wgpu::VertexBufferLayout {
        array_stride: INSTANCE_BYTE_LEN as u64,
        step_mode: wgpu::VertexStepMode::Instance,
        attributes: &INSTANCE_ATTRIBUTES,
    };
    let label = if double_sided {
        "scena.m0.unlit_triangle_pipeline.double_sided"
    } else {
        "scena.m0.unlit_triangle_pipeline.single_sided"
    };
    device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
        label: Some(label),
        layout: Some(&pipeline_layout),
        vertex: wgpu::VertexState {
            module: &shader,
            entry_point: Some("vs_main"),
            compilation_options: wgpu::PipelineCompilationOptions::default(),
            buffers: &[vertex_buffer, instance_buffer],
        },
        primitive: wgpu::PrimitiveState {
            front_face: SCENA_FRONT_FACE,
            cull_mode: (!double_sided).then_some(wgpu::Face::Back),
            ..Default::default()
        },
        depth_stencil: depth_compare.map(|depth_compare| wgpu::DepthStencilState {
            format: wgpu::TextureFormat::Depth32Float,
            depth_write_enabled: Some(false),
            depth_compare: Some(depth_compare),
            stencil: wgpu::StencilState::default(),
            bias: wgpu::DepthBiasState::default(),
        }),
        multisample: wgpu::MultisampleState {
            count: sample_count,
            ..Default::default()
        },
        fragment: Some(wgpu::FragmentState {
            module: &shader,
            entry_point: Some("fs_main"),
            compilation_options: wgpu::PipelineCompilationOptions::default(),
            targets: &[Some(wgpu::ColorTargetState {
                format,
                blend: Some(wgpu::BlendState::ALPHA_BLENDING),
                write_mask: wgpu::ColorWrites::ALL,
            })],
        }),
        multiview_mask: None,
        cache: None,
    })
}

#[cfg(test)]
mod tests {
    #[test]
    fn unlit_pipeline_source_wires_depth_state_into_visible_color_pass() {
        let source = include_str!("pipeline.rs");
        let implementation = source
            .split("#[cfg(test)]")
            .next()
            .expect("pipeline implementation precedes tests");
        assert!(
            implementation.contains("RenderPassDepthStencilAttachment")
                && implementation.contains("depth_stencil: depth_compare.map"),
            "visible GPU color pass must use the prepared depth buffer when one exists"
        );
    }

    #[test]
    fn depth_prepass_and_color_pass_use_identical_clip_space_transform() {
        // Pi 5 V3D WebGL2 runs the fragment stage at lower-than-highp
        // precision by default. If the depth pre-pass computes clip-space
        // depth via a different matrix multiplication path than the color
        // pass, the two ULP-diverge and the LessEqual depth test rejects
        // most color-pass fragments, producing a mostly-black render on
        // V3D-class hardware while Lavapipe/desktop GL is unaffected.
        // Both shaders must use `clip_from_world * world_position`.
        let depth = include_str!("depth.rs");
        let color = include_str!("output_shader.wgsl");
        let color_tex2d = include_str!("output_shader_texture_2d.wgsl");
        for (label, source) in [
            ("depth.rs", depth),
            ("output_shader.wgsl", color),
            ("output_shader_texture_2d.wgsl", color_tex2d),
        ] {
            assert!(
                source.contains("camera.clip_from_world * world_position"),
                "{label} vs_main must use `camera.clip_from_world * world_position` so depth values match the other passes bit-for-bit",
            );
            assert!(
                !source.contains("camera.clip_from_view * camera.view_from_world * world_position")
                    && !source.contains(
                        "camera.clip_from_view * camera.view_from_world * draw.world_from_model",
                    ),
                "{label} must not reintroduce a divergent clip-space matrix path",
            );
        }
    }

    #[test]
    fn unlit_pipeline_binds_material_group_for_fragment_sampling() {
        let source = include_str!("pipeline.rs");
        let implementation = source
            .split("#[cfg(test)]")
            .next()
            .expect("pipeline implementation precedes tests");
        assert!(
            implementation.contains("material_bind_group_layout")
                && implementation.contains("material_resources")
                && implementation.contains("pass.set_bind_group(1, &material.bind_group"),
            "visible GPU color pass must bind material resources, not only camera uniforms"
        );
    }

    #[test]
    fn unlit_pipeline_can_split_opaque_and_transparent_draws_for_transmission() {
        let source = include_str!("pipeline.rs");
        let implementation = source
            .split("#[cfg(test)]")
            .next()
            .expect("pipeline implementation precedes tests");
        assert!(
            implementation.contains("enum DrawFilter")
                && implementation.contains("DrawFilter::OpaqueOnly")
                && implementation.contains("DrawFilter::TransparentOnly")
                && implementation.contains("LoadOp::Load")
                && implementation.contains("depth_prepass_eligible"),
            "physical glass needs an opaque scene-color pass followed by a transparent \
             transmission pass; one all-material alpha-blended pass can still ship fake glass"
        );
    }
}