oxihuman-viewer 0.2.1

wgpu/WebGPU rendering adapter for OxiHuman
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Pipeline caching and creation for the OxiHuman wgpu render pipeline.
//!
//! [`PipelineCache`] lazily creates and caches [`wgpu::RenderPipeline`] objects
//! keyed by [`PipelineKey`].  All pipelines share the same vertex buffer layout
//! (position + normal + UV + tangent, interleaved).

use std::collections::HashMap;
use std::sync::Arc;

use anyhow::{Context, Result};

use crate::gpu::wgsl_shaders::{
    FRAGMENT_SHADER_PBR, FRAGMENT_SHADER_SHADOW, FRAGMENT_SHADER_TONEMAP,
    FRAGMENT_SHADER_WIREFRAME, VERTEX_SHADER_FULLSCREEN, VERTEX_SHADER_PBR, VERTEX_SHADER_SHADOW,
    VERTEX_SHADER_WIREFRAME,
};

// ── Vertex stride & attribute offsets ─────────────────────────────────────────

/// Byte size of one interleaved vertex:
/// `position (12) + normal (12) + uv (8) + tangent (16) = 48 bytes`.
pub const VERTEX_STRIDE: u64 = 48;

/// Attribute: position at offset 0 (Float32x3).
const ATTR_POSITION: wgpu::VertexAttribute = wgpu::VertexAttribute {
    format: wgpu::VertexFormat::Float32x3,
    offset: 0,
    shader_location: 0,
};
/// Attribute: normal at offset 12 (Float32x3).
const ATTR_NORMAL: wgpu::VertexAttribute = wgpu::VertexAttribute {
    format: wgpu::VertexFormat::Float32x3,
    offset: 12,
    shader_location: 1,
};
/// Attribute: UV at offset 24 (Float32x2).
const ATTR_UV: wgpu::VertexAttribute = wgpu::VertexAttribute {
    format: wgpu::VertexFormat::Float32x2,
    offset: 24,
    shader_location: 2,
};
/// Attribute: tangent at offset 32 (Float32x4).
const ATTR_TANGENT: wgpu::VertexAttribute = wgpu::VertexAttribute {
    format: wgpu::VertexFormat::Float32x4,
    offset: 32,
    shader_location: 3,
};

/// Returns the interleaved vertex buffer layout used by all mesh pipelines.
pub fn mesh_vertex_buffer_layout() -> wgpu::VertexBufferLayout<'static> {
    wgpu::VertexBufferLayout {
        array_stride: VERTEX_STRIDE,
        step_mode: wgpu::VertexStepMode::Vertex,
        attributes: &[ATTR_POSITION, ATTR_NORMAL, ATTR_UV, ATTR_TANGENT],
    }
}

// ── PipelineKey ───────────────────────────────────────────────────────────────

/// Discriminant used to look up a cached pipeline.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PipelineKey {
    /// Opaque PBR geometry pass.
    Pbr,
    /// Alpha-blended PBR geometry pass.
    PbrAlpha,
    /// Wireframe debug overlay.
    Wireframe,
    /// Shadow map depth pass.
    Shadow,
    /// Fullscreen post-processing (tonemapping).
    Fullscreen,
}

// ── PipelineCache ─────────────────────────────────────────────────────────────

/// Lazily creates and caches [`wgpu::RenderPipeline`]s by [`PipelineKey`].
///
/// All pipelines within a cache share the same:
/// * colour attachment format (`surface_format`)
/// * depth format (`depth_format`)
/// * bind group layouts supplied at construction time
pub struct PipelineCache {
    pipelines: HashMap<PipelineKey, Arc<wgpu::RenderPipeline>>,
    /// Bind group layout for group 0 (camera).
    camera_bgl: Arc<wgpu::BindGroupLayout>,
    /// Bind group layout for group 1 (model transform).
    model_bgl: Arc<wgpu::BindGroupLayout>,
    /// Bind group layout for group 2 (material + textures).
    material_bgl: Arc<wgpu::BindGroupLayout>,
    /// Bind group layout for group 3 (lights).
    lights_bgl: Arc<wgpu::BindGroupLayout>,
}

impl PipelineCache {
    /// Create an empty cache with the provided bind group layouts.
    pub fn new(
        camera_bgl: Arc<wgpu::BindGroupLayout>,
        model_bgl: Arc<wgpu::BindGroupLayout>,
        material_bgl: Arc<wgpu::BindGroupLayout>,
        lights_bgl: Arc<wgpu::BindGroupLayout>,
    ) -> Self {
        Self {
            pipelines: HashMap::new(),
            camera_bgl,
            model_bgl,
            material_bgl,
            lights_bgl,
        }
    }

    /// Return a reference to the cached pipeline for `key`, creating it first
    /// if not yet present.
    pub fn get_or_create(
        &mut self,
        key: PipelineKey,
        device: &wgpu::Device,
        surface_format: wgpu::TextureFormat,
        depth_format: wgpu::TextureFormat,
    ) -> Result<&wgpu::RenderPipeline> {
        if !self.pipelines.contains_key(&key) {
            let pipeline = self
                .create_pipeline(key, device, surface_format, depth_format)
                .with_context(|| format!("creating pipeline {:?}", key))?;
            self.pipelines.insert(key, Arc::new(pipeline));
        }
        Ok(self.pipelines[&key].as_ref())
    }

    // ── Private helpers ───────────────────────────────────────────────────────

    fn create_pipeline(
        &self,
        key: PipelineKey,
        device: &wgpu::Device,
        surface_format: wgpu::TextureFormat,
        depth_format: wgpu::TextureFormat,
    ) -> Result<wgpu::RenderPipeline> {
        match key {
            PipelineKey::Pbr => self.build_pbr(device, surface_format, depth_format, false),
            PipelineKey::PbrAlpha => self.build_pbr(device, surface_format, depth_format, true),
            PipelineKey::Wireframe => self.build_wireframe(device, surface_format, depth_format),
            PipelineKey::Shadow => self.build_shadow(device, depth_format),
            PipelineKey::Fullscreen => self.build_fullscreen(device, surface_format),
        }
    }

    /// Compile a shader module from a WGSL source string.
    fn make_shader(device: &wgpu::Device, label: &str, src: &str) -> wgpu::ShaderModule {
        device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some(label),
            source: wgpu::ShaderSource::Wgsl(src.into()),
        })
    }

    /// PBR pipeline layout shared between opaque and alpha passes.
    fn pbr_pipeline_layout(&self, device: &wgpu::Device) -> wgpu::PipelineLayout {
        device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("pbr_pipeline_layout"),
            bind_group_layouts: &[
                Some(&*self.camera_bgl),
                Some(&*self.model_bgl),
                Some(&*self.material_bgl),
                Some(&*self.lights_bgl),
            ],
            immediate_size: 0,
        })
    }

    fn build_pbr(
        &self,
        device: &wgpu::Device,
        surface_format: wgpu::TextureFormat,
        depth_format: wgpu::TextureFormat,
        alpha_blend: bool,
    ) -> Result<wgpu::RenderPipeline> {
        let vs = Self::make_shader(device, "pbr_vs", VERTEX_SHADER_PBR);
        let fs = Self::make_shader(device, "pbr_fs", FRAGMENT_SHADER_PBR);
        let layout = self.pbr_pipeline_layout(device);

        let blend = if alpha_blend {
            Some(wgpu::BlendState {
                color: wgpu::BlendComponent {
                    src_factor: wgpu::BlendFactor::SrcAlpha,
                    dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                    operation: wgpu::BlendOperation::Add,
                },
                alpha: wgpu::BlendComponent {
                    src_factor: wgpu::BlendFactor::One,
                    dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                    operation: wgpu::BlendOperation::Add,
                },
            })
        } else {
            Some(wgpu::BlendState::REPLACE)
        };

        let depth_write = !alpha_blend;

        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some(if alpha_blend {
                "pbr_alpha_pipeline"
            } else {
                "pbr_pipeline"
            }),
            layout: Some(&layout),
            vertex: wgpu::VertexState {
                module: &vs,
                entry_point: Some("vs_main"),
                buffers: &[mesh_vertex_buffer_layout()],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &fs,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: surface_format,
                    blend,
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: Some(wgpu::Face::Back),
                polygon_mode: wgpu::PolygonMode::Fill,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: depth_format,
                depth_write_enabled: Some(depth_write),
                depth_compare: Some(wgpu::CompareFunction::Less),
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState {
                count: 4, // 4x MSAA
                mask: !0,
                alpha_to_coverage_enabled: false,
            },
            multiview_mask: None,
            cache: None,
        });

        Ok(pipeline)
    }

    fn build_wireframe(
        &self,
        device: &wgpu::Device,
        surface_format: wgpu::TextureFormat,
        depth_format: wgpu::TextureFormat,
    ) -> Result<wgpu::RenderPipeline> {
        let vs = Self::make_shader(device, "wireframe_vs", VERTEX_SHADER_WIREFRAME);
        let fs = Self::make_shader(device, "wireframe_fs", FRAGMENT_SHADER_WIREFRAME);

        // Wireframe uses only camera + model + a small color uniform (group 2).
        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("wireframe_pipeline_layout"),
            bind_group_layouts: &[
                Some(&*self.camera_bgl),
                Some(&*self.model_bgl),
                Some(&*self.material_bgl),
            ],
            immediate_size: 0,
        });

        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("wireframe_pipeline"),
            layout: Some(&layout),
            vertex: wgpu::VertexState {
                module: &vs,
                entry_point: Some("vs_wireframe"),
                buffers: &[mesh_vertex_buffer_layout()],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &fs,
                entry_point: Some("fs_wireframe"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: surface_format,
                    blend: Some(wgpu::BlendState::REPLACE),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: None,
                polygon_mode: wgpu::PolygonMode::Line,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: depth_format,
                depth_write_enabled: Some(false),
                depth_compare: Some(wgpu::CompareFunction::LessEqual),
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState::default(),
            multiview_mask: None,
            cache: None,
        });

        Ok(pipeline)
    }

    fn build_shadow(
        &self,
        device: &wgpu::Device,
        depth_format: wgpu::TextureFormat,
    ) -> Result<wgpu::RenderPipeline> {
        let vs = Self::make_shader(device, "shadow_vs", VERTEX_SHADER_SHADOW);
        let fs = Self::make_shader(device, "shadow_fs", FRAGMENT_SHADER_SHADOW);

        // Shadow pass: shadow uniform (group 0) + model (group 1).
        let shadow_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("shadow_bgl"),
            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,
            }],
        });

        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("shadow_pipeline_layout"),
            bind_group_layouts: &[Some(&shadow_bgl), Some(&*self.model_bgl)],
            immediate_size: 0,
        });

        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("shadow_pipeline"),
            layout: Some(&layout),
            vertex: wgpu::VertexState {
                module: &vs,
                entry_point: Some("vs_shadow"),
                buffers: &[mesh_vertex_buffer_layout()],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &fs,
                entry_point: Some("fs_shadow"),
                targets: &[],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: Some(wgpu::Face::Front), // front-face culling reduces shadow acne
                polygon_mode: wgpu::PolygonMode::Fill,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: depth_format,
                depth_write_enabled: Some(true),
                depth_compare: Some(wgpu::CompareFunction::Less),
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState {
                    constant: 2,
                    slope_scale: 2.0,
                    clamp: 0.0,
                },
            }),
            multisample: wgpu::MultisampleState::default(),
            multiview_mask: None,
            cache: None,
        });

        Ok(pipeline)
    }

    fn build_fullscreen(
        &self,
        device: &wgpu::Device,
        surface_format: wgpu::TextureFormat,
    ) -> Result<wgpu::RenderPipeline> {
        let vs = Self::make_shader(device, "fullscreen_vs", VERTEX_SHADER_FULLSCREEN);
        let fs = Self::make_shader(device, "tonemap_fs", FRAGMENT_SHADER_TONEMAP);

        // Fullscreen pass: HDR texture + sampler + tonemap params (group 0).
        let fs_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("fullscreen_bgl"),
            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::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
            ],
        });

        let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("fullscreen_pipeline_layout"),
            bind_group_layouts: &[Some(&fs_bgl)],
            immediate_size: 0,
        });

        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("fullscreen_pipeline"),
            layout: Some(&layout),
            vertex: wgpu::VertexState {
                module: &vs,
                entry_point: Some("vs_fullscreen"),
                buffers: &[],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &fs,
                entry_point: Some("fs_tonemap"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: surface_format,
                    blend: Some(wgpu::BlendState::REPLACE),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                cull_mode: None,
                ..Default::default()
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            multiview_mask: None,
            cache: None,
        });

        Ok(pipeline)
    }
}