scenix-renderer 1.1.0

wgpu renderer, GPU scene upload, passes, and frame orchestration for scenix.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use std::collections::HashMap;

use scenix_core::{Color, LightId, MaterialId, MeshId, TextureId, ValidationError};
use scenix_light::{AmbientLight, DirectionalLight, PointLight, SpotLight};
use scenix_material::{
    LambertMaterial, Material, NormalMaterial, PbrMaterial, PhysicalMaterial, PipelineKey,
    ToonMaterial, UnlitMaterial, WireframeMaterial,
};
use scenix_math::{Aabb, Mat4, Vec2, Vec3, Vec4};
use scenix_mesh::Geometry;
use scenix_texture::{AddressMode, CompareFunction, FilterMode, Sampler, Texture2D, TextureFormat};
use wgpu::util::DeviceExt;

/// Interleaved GPU vertex layout used by the v0.6 renderer.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PackedVertex {
    /// Vertex position.
    pub position: [f32; 3],
    /// Vertex normal.
    pub normal: [f32; 3],
    /// Primary texture coordinate.
    pub uv: [f32; 2],
    /// Vertex color.
    pub color: [f32; 4],
    /// Tangent vector and handedness.
    pub tangent: [f32; 4],
}

impl PackedVertex {
    /// Returns the wgpu vertex-buffer layout.
    pub const fn layout() -> wgpu::VertexBufferLayout<'static> {
        const ATTRIBUTES: [wgpu::VertexAttribute; 5] = wgpu::vertex_attr_array![
            0 => Float32x3,
            1 => Float32x3,
            2 => Float32x2,
            3 => Float32x4,
            4 => Float32x4
        ];
        wgpu::VertexBufferLayout {
            array_stride: core::mem::size_of::<PackedVertex>() as u64,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &ATTRIBUTES,
        }
    }
}

/// Index type chosen for packed geometry.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum GpuIndexFormat {
    /// 16-bit index buffer.
    Uint16,
    /// 32-bit index buffer.
    Uint32,
}

impl GpuIndexFormat {
    /// Returns the matching wgpu index format.
    #[inline]
    pub const fn to_wgpu(self) -> wgpu::IndexFormat {
        match self {
            Self::Uint16 => wgpu::IndexFormat::Uint16,
            Self::Uint32 => wgpu::IndexFormat::Uint32,
        }
    }
}

/// CPU-packed geometry ready for GPU upload.
#[derive(Clone, Debug, PartialEq)]
pub struct PackedGeometry {
    /// Interleaved vertices.
    pub vertices: Vec<PackedVertex>,
    /// Raw index bytes in `index_format`.
    pub index_bytes: Vec<u8>,
    /// Number of indices.
    pub index_count: u32,
    /// Index storage format.
    pub index_format: GpuIndexFormat,
    /// Local-space geometry bounds.
    pub aabb: Aabb,
}

/// Uploaded GPU mesh buffers.
#[derive(Debug)]
pub struct GpuMesh {
    vertex_buffer: wgpu::Buffer,
    index_buffer: wgpu::Buffer,
    packed: PackedGeometry,
}

impl GpuMesh {
    /// Returns the vertex buffer.
    #[inline]
    pub const fn vertex_buffer(&self) -> &wgpu::Buffer {
        &self.vertex_buffer
    }

    /// Returns the index buffer.
    #[inline]
    pub const fn index_buffer(&self) -> &wgpu::Buffer {
        &self.index_buffer
    }

    /// Returns packed geometry metadata.
    #[inline]
    pub const fn packed(&self) -> &PackedGeometry {
        &self.packed
    }
}

/// Renderer-side material registry entry.
#[derive(Clone, Debug, PartialEq)]
pub enum RendererMaterial {
    /// Metallic-roughness material.
    Pbr(PbrMaterial),
    /// Advanced physical material.
    Physical(PhysicalMaterial),
    /// Constant-color unlit material.
    Unlit(UnlitMaterial),
    /// Diffuse Lambert material.
    Lambert(LambertMaterial),
    /// Cel-shaded material.
    Toon(ToonMaterial),
    /// Wireframe/debug preview material.
    Wireframe(WireframeMaterial),
    /// Normal visualization material.
    Normal(NormalMaterial),
}

impl RendererMaterial {
    /// Returns the material pipeline key.
    #[inline]
    pub fn pipeline_key(&self) -> PipelineKey {
        match self {
            Self::Pbr(material) => material.pipeline_key(),
            Self::Physical(material) => material.pipeline_key(),
            Self::Unlit(material) => material.pipeline_key(),
            Self::Lambert(material) => material.pipeline_key(),
            Self::Toon(material) => material.pipeline_key(),
            Self::Wireframe(material) => material.pipeline_key(),
            Self::Normal(material) => material.pipeline_key(),
        }
    }

    /// Returns whether the material should be depth-sorted with transparent draws.
    #[inline]
    pub fn is_transparent(&self) -> bool {
        match self {
            Self::Pbr(material) => material.is_transparent(),
            Self::Physical(material) => material.is_transparent(),
            Self::Unlit(material) => material.is_transparent(),
            Self::Lambert(material) => material.is_transparent(),
            Self::Toon(material) => material.is_transparent(),
            Self::Wireframe(material) => material.is_transparent(),
            Self::Normal(material) => material.is_transparent(),
        }
    }

    /// Returns the base preview color used by the stable v1 renderer path.
    #[inline]
    pub fn preview_color(&self) -> Color {
        match self {
            Self::Pbr(material) => material.albedo,
            Self::Physical(material) => material.base.albedo,
            Self::Unlit(material) => material.color,
            Self::Lambert(material) => material.color,
            Self::Toon(material) => material.color,
            Self::Wireframe(material) => Color {
                a: material.opacity,
                ..material.color
            },
            Self::Normal(_) => Color::WHITE,
        }
    }

    /// Returns a compact shader-family code for the shared v1 preview shader.
    #[inline]
    pub fn preview_shader_code(&self) -> f32 {
        match self {
            Self::Pbr(_) => 0.0,
            Self::Physical(_) => 1.0,
            Self::Unlit(_) => 2.0,
            Self::Lambert(_) => 3.0,
            Self::Toon(_) => 4.0,
            Self::Wireframe(_) => 5.0,
            Self::Normal(_) => 6.0,
        }
    }
}

/// Renderer-side texture metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GpuTexture {
    /// Texture width.
    pub width: u32,
    /// Texture height.
    pub height: u32,
    /// CPU texture format.
    pub format: TextureFormat,
    /// Matching wgpu texture format, when supported.
    pub wgpu_format: Option<wgpu::TextureFormat>,
    /// Sampler metadata.
    pub sampler: Sampler,
    /// Number of mip levels stored in the CPU texture.
    pub mip_levels: u32,
}

/// Texture metadata store used by `GpuMaterial`.
pub type TextureStore = HashMap<TextureId, GpuTexture>;

/// Renderer-side light registry entry.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RendererLight {
    /// Ambient light.
    Ambient(AmbientLight),
    /// Directional light.
    Directional(DirectionalLight),
    /// Point light.
    Point(PointLight),
    /// Spot light.
    Spot(SpotLight),
}

/// Visible draw submission generated from a scene node.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DrawSubmission {
    /// Mesh resource ID.
    pub mesh_id: MeshId,
    /// Material resource ID.
    pub material_id: MaterialId,
    /// Node world transform.
    pub world_matrix: Mat4,
    /// World-space bounds used for culling.
    pub world_aabb: Aabb,
    /// Distance from camera position to bounds center.
    pub distance_to_camera: f32,
    /// Whether this draw needs transparent sorting.
    pub transparent: bool,
    /// Stable render order.
    pub render_order: u32,
}

/// Renderer-owned GPU scene resources and CPU metadata.
#[derive(Debug, Default)]
pub struct GpuScene {
    meshes: HashMap<MeshId, GpuMesh>,
    materials: HashMap<MaterialId, RendererMaterial>,
    textures: TextureStore,
    lights: HashMap<LightId, RendererLight>,
}

impl GpuScene {
    /// Creates an empty GPU scene registry.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Packs geometry into the renderer interleaved vertex/index layout.
    pub fn pack_geometry(geometry: &Geometry) -> Result<PackedGeometry, ValidationError> {
        geometry.validate()?;
        let vertex_count = geometry.positions.len();
        let mut vertices = Vec::with_capacity(vertex_count);
        for index in 0..vertex_count {
            let position = geometry.positions[index];
            let normal = geometry.normals.get(index).copied().unwrap_or(Vec3::Y);
            let uv = geometry.uvs.get(index).copied().unwrap_or(Vec2::ZERO);
            let color = geometry.colors.get(index).copied().unwrap_or(Color::WHITE);
            let tangent = geometry
                .tangents
                .get(index)
                .copied()
                .unwrap_or(Vec4::new(1.0, 0.0, 0.0, 1.0));
            vertices.push(PackedVertex {
                position: [position.x, position.y, position.z],
                normal: [normal.x, normal.y, normal.z],
                uv: [uv.x, uv.y],
                color: color.to_array(),
                tangent: [tangent.x, tangent.y, tangent.z, tangent.w],
            });
        }

        let source_indices: Vec<u32> = if geometry.indices.is_empty() {
            (0..vertex_count as u32).collect()
        } else {
            geometry.indices.clone()
        };
        let can_use_u16 = vertex_count <= u16::MAX as usize
            && source_indices.iter().all(|index| *index <= u16::MAX as u32);
        let (index_bytes, index_format) = if can_use_u16 {
            let indices: Vec<u16> = source_indices.iter().map(|index| *index as u16).collect();
            (
                bytemuck::cast_slice(&indices).to_vec(),
                GpuIndexFormat::Uint16,
            )
        } else {
            (
                bytemuck::cast_slice(source_indices.as_slice()).to_vec(),
                GpuIndexFormat::Uint32,
            )
        };

        Ok(PackedGeometry {
            vertices,
            index_bytes,
            index_count: source_indices.len() as u32,
            index_format,
            aabb: geometry.aabb(),
        })
    }

    /// Uploads and stores a mesh.
    pub fn register_mesh(
        &mut self,
        device: &wgpu::Device,
        mesh_id: MeshId,
        geometry: &Geometry,
    ) -> Result<(), ValidationError> {
        if mesh_id.is_null() {
            return Err(ValidationError::InvalidId);
        }
        let packed = Self::pack_geometry(geometry)?;
        let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("scenix.mesh.vertices"),
            contents: bytemuck::cast_slice(packed.vertices.as_slice()),
            usage: wgpu::BufferUsages::VERTEX,
        });
        let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("scenix.mesh.indices"),
            contents: packed.index_bytes.as_slice(),
            usage: wgpu::BufferUsages::INDEX,
        });
        self.meshes.insert(
            mesh_id,
            GpuMesh {
                vertex_buffer,
                index_buffer,
                packed,
            },
        );
        Ok(())
    }

    /// Registers a PBR material.
    pub fn register_pbr_material(
        &mut self,
        material_id: MaterialId,
        material: &PbrMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Pbr(material.clone()))
    }

    /// Registers a physical material.
    pub fn register_physical_material(
        &mut self,
        material_id: MaterialId,
        material: &PhysicalMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Physical(material.clone()))
    }

    /// Registers an unlit material.
    pub fn register_unlit_material(
        &mut self,
        material_id: MaterialId,
        material: &UnlitMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Unlit(material.clone()))
    }

    /// Registers a Lambert material.
    pub fn register_lambert_material(
        &mut self,
        material_id: MaterialId,
        material: &LambertMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Lambert(material.clone()))
    }

    /// Registers a toon material.
    pub fn register_toon_material(
        &mut self,
        material_id: MaterialId,
        material: &ToonMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Toon(material.clone()))
    }

    /// Registers a wireframe preview material.
    pub fn register_wireframe_material(
        &mut self,
        material_id: MaterialId,
        material: &WireframeMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Wireframe(*material))
    }

    /// Registers a normal visualization material.
    pub fn register_normal_material(
        &mut self,
        material_id: MaterialId,
        material: &NormalMaterial,
    ) -> Result<(), ValidationError> {
        self.register_material(material_id, RendererMaterial::Normal(*material))
    }

    /// Registers a renderer material.
    pub fn register_material(
        &mut self,
        material_id: MaterialId,
        material: RendererMaterial,
    ) -> Result<(), ValidationError> {
        if material_id.is_null() {
            return Err(ValidationError::InvalidId);
        }
        self.materials.insert(material_id, material);
        Ok(())
    }

    /// Registers validated 2D texture metadata and sampler state.
    pub fn register_texture2d(
        &mut self,
        texture_id: TextureId,
        texture: &Texture2D,
        sampler: Sampler,
    ) -> Result<(), ValidationError> {
        if texture_id.is_null() {
            return Err(ValidationError::InvalidId);
        }
        texture.validate()?;
        self.textures.insert(
            texture_id,
            GpuTexture {
                width: texture.width,
                height: texture.height,
                format: texture.format,
                wgpu_format: to_wgpu_texture_format(texture.format),
                sampler,
                mip_levels: texture.mip_levels.max(1),
            },
        );
        Ok(())
    }

    /// Registers a light.
    pub fn register_light(
        &mut self,
        light_id: LightId,
        light: RendererLight,
    ) -> Result<(), ValidationError> {
        if light_id.is_null() {
            return Err(ValidationError::InvalidId);
        }
        self.lights.insert(light_id, light);
        Ok(())
    }

    /// Returns a mesh by ID.
    #[inline]
    pub fn mesh(&self, mesh_id: MeshId) -> Option<&GpuMesh> {
        self.meshes.get(&mesh_id)
    }

    /// Returns a material by ID.
    #[inline]
    pub fn material(&self, material_id: MaterialId) -> Option<&RendererMaterial> {
        self.materials.get(&material_id)
    }

    /// Returns texture metadata by ID.
    #[inline]
    pub fn texture(&self, texture_id: TextureId) -> Option<&GpuTexture> {
        self.textures.get(&texture_id)
    }

    /// Returns registered texture metadata.
    #[inline]
    pub const fn textures(&self) -> &TextureStore {
        &self.textures
    }

    /// Returns the number of registered lights.
    #[inline]
    pub fn light_count(&self) -> usize {
        self.lights.len()
    }

    /// Returns the number of registered meshes.
    #[inline]
    pub fn mesh_count(&self) -> usize {
        self.meshes.len()
    }

    /// Returns the number of registered materials.
    #[inline]
    pub fn material_count(&self) -> usize {
        self.materials.len()
    }
}

/// Converts scenix texture format metadata to a wgpu format.
pub const fn to_wgpu_texture_format(format: TextureFormat) -> Option<wgpu::TextureFormat> {
    match format {
        TextureFormat::Rgba8Unorm => Some(wgpu::TextureFormat::Rgba8Unorm),
        TextureFormat::Rgba8UnormSrgb => Some(wgpu::TextureFormat::Rgba8UnormSrgb),
        TextureFormat::Rgba16Float => Some(wgpu::TextureFormat::Rgba16Float),
        TextureFormat::Depth32Float => Some(wgpu::TextureFormat::Depth32Float),
        TextureFormat::Bc7RgbaUnorm => Some(wgpu::TextureFormat::Bc7RgbaUnorm),
        TextureFormat::Astc4x4RgbaUnorm => Some(wgpu::TextureFormat::Astc {
            block: wgpu::AstcBlock::B4x4,
            channel: wgpu::AstcChannel::Unorm,
        }),
        TextureFormat::Etc2Rgba8Unorm => Some(wgpu::TextureFormat::Etc2Rgba8Unorm),
    }
}

/// Converts sampler filter modes to wgpu filter modes.
pub const fn to_wgpu_filter_mode(filter: FilterMode) -> wgpu::FilterMode {
    match filter {
        FilterMode::Nearest => wgpu::FilterMode::Nearest,
        FilterMode::Linear => wgpu::FilterMode::Linear,
    }
}

/// Converts sampler address modes to wgpu address modes.
pub const fn to_wgpu_address_mode(address: AddressMode) -> wgpu::AddressMode {
    match address {
        AddressMode::Repeat => wgpu::AddressMode::Repeat,
        AddressMode::MirrorRepeat => wgpu::AddressMode::MirrorRepeat,
        AddressMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
    }
}

/// Converts optional compare state to wgpu compare state.
pub const fn to_wgpu_compare(compare: Option<CompareFunction>) -> Option<wgpu::CompareFunction> {
    match compare {
        Some(CompareFunction::Less) => Some(wgpu::CompareFunction::Less),
        Some(CompareFunction::LessEqual) => Some(wgpu::CompareFunction::LessEqual),
        Some(CompareFunction::Greater) => Some(wgpu::CompareFunction::Greater),
        Some(CompareFunction::GreaterEqual) => Some(wgpu::CompareFunction::GreaterEqual),
        Some(CompareFunction::Equal) => Some(wgpu::CompareFunction::Equal),
        Some(CompareFunction::NotEqual) => Some(wgpu::CompareFunction::NotEqual),
        Some(CompareFunction::Always) => Some(wgpu::CompareFunction::Always),
        Some(CompareFunction::Never) => Some(wgpu::CompareFunction::Never),
        None => None,
    }
}