sim-engine 0.3.0

A validated high-performance visualization engine for simulations
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
//! Immutable opaque texture materials for the retained 3D surface path.

use super::*;
use crate::mesh3d::TextureCoordinate2d;

#[cfg(test)]
#[path = "mesh3d_texture_red_tests.rs"]
mod red_tests;
#[cfg(test)]
#[path = "mesh3d_texture_tests.rs"]
mod tests;
#[cfg(test)]
pub(super) use tests::{assert_gpu_texture_contract, assert_gpu_textured_recovery};

/// Validation or allocation failure for an opaque retained 3D texture/material.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Texture3dError {
    /// Image dimensions, retained bytes, allocation or device limits failed.
    Image(ImageError),
    /// The first texel with alpha other than 255 in row-major source order.
    NonOpaquePixel {
        /// Zero-based source texel index.
        texel: usize,
    },
    /// A material tint must be normalized linear RGBA with alpha exactly one.
    InvalidTint,
    /// Attaching a texture requires a surface and one UV per mesh vertex.
    MissingTextureCoordinates,
    /// Texture and mesh must belong to the renderer's current device identity.
    RendererMismatch,
    /// An atlas region does not fit the texture.
    InvalidRegion,
}

impl fmt::Display for Texture3dError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Image(error) => write!(formatter, "3D texture: {error}"),
            Self::NonOpaquePixel { texel } => {
                write!(formatter, "3D texture texel {texel} is not opaque")
            }
            Self::InvalidTint => write!(formatter, "3D texture tint must be normalized and opaque"),
            Self::MissingTextureCoordinates => {
                write!(formatter, "textured 3D surfaces require per-vertex UVs")
            }
            Self::RendererMismatch => {
                write!(formatter, "3D texture/mesh belongs to another renderer")
            }
            Self::InvalidRegion => write!(formatter, "3D texture atlas region is out of bounds"),
        }
    }
}
impl Error for Texture3dError {}

struct TextureStorage {
    renderer_identity: Arc<()>,
    image: Image2d,
    nearest: wgpu::BindGroup,
    linear: wgpu::BindGroup,
}

/// Cheap shared handle for immutable opaque, single-level sRGB RGBA8 texels.
///
/// Clones share CPU recovery pixels and the GPU allocation. U/V has a top-left
/// origin; addressing clamps to the image boundary, with no mipmap generation.
/// Only alpha 255 is accepted. Restoring returns a new device-owned handle;
/// existing clones remain stale until explicitly rebound or scene-restored.
#[derive(Clone)]
pub struct Texture3d {
    storage: Arc<TextureStorage>,
}

impl Texture3d {
    /// Returns physical source texel dimensions.
    pub fn size(&self) -> (u32, u32) {
        self.storage.image.size()
    }
    /// Returns immutable retained row-major, top-to-bottom sRGB RGBA8 texels.
    pub fn pixels(&self) -> &[u8] {
        self.storage.image.pixels()
    }
    /// Returns creation limits retained for restoration.
    pub fn budget(&self) -> ImageBudget {
        self.storage.image.budget()
    }
    /// Returns shared CPU recovery-pixel capacity, excluding fixed handle metadata.
    pub fn recovery_memory_bytes(&self) -> usize {
        self.storage.image.recovery_memory_bytes()
    }
    /// Returns nominal single-level GPU texel bytes, excluding driver metadata.
    pub fn gpu_allocation_bytes(&self) -> usize {
        self.storage.image.gpu_allocation_bytes()
    }
    /// Maps top-left, top-right, bottom-right and bottom-left corners of an
    /// atlas region to its outer texel centers. Nearest sampling avoids adjacent
    /// cells under this mip-zero policy. For strict linear-filter isolation,
    /// provide host-owned extruded gutters around atlas cells: interpolation
    /// rounding can extend the filtering footprint slightly beyond a center.
    /// A one-texel extent maps both edges to its center.
    pub fn region_coordinates(
        &self,
        region: ImageTexelRect,
    ) -> Result<[TextureCoordinate2d; 4], Texture3dError> {
        let (width, height) = self.size();
        if !region.fits(width, height) {
            return Err(Texture3dError::InvalidRegion);
        }
        let left = (region.x() as f64 + 0.5) / f64::from(width);
        let right = (f64::from(region.x()) + f64::from(region.width()) - 0.5) / f64::from(width);
        let top = (region.y() as f64 + 0.5) / f64::from(height);
        let bottom = (f64::from(region.y()) + f64::from(region.height()) - 0.5) / f64::from(height);
        let make = |u: f64, v: f64| {
            TextureCoordinate2d::new(u as f32, v as f32).map_err(|_| Texture3dError::InvalidRegion)
        };
        Ok([
            make(left, top)?,
            make(right, top)?,
            make(right, bottom)?,
            make(left, bottom)?,
        ])
    }
    pub(super) fn identity_key(&self) -> usize {
        Arc::as_ptr(&self.storage) as usize
    }
    pub(super) fn belongs_to(&self, identity: &Arc<()>) -> bool {
        Arc::ptr_eq(identity, &self.storage.renderer_identity)
    }
    fn bind_group(&self, sampling: ImageSampling) -> &wgpu::BindGroup {
        match sampling {
            ImageSampling::Nearest => &self.storage.nearest,
            ImageSampling::Linear => &self.storage.linear,
        }
    }
}

/// Cheap immutable unlit material sharing one opaque texture and filter state.
///
/// Sampled linear RGB is multiplied by this normalized opaque tint and the
/// object's `SurfaceStyle3d` color. Depth writes/tests and no-culling winding
/// semantics are identical to ordinary opaque surfaces. Transparency, lighting
/// and alpha-cutout are intentionally separate capabilities.
#[derive(Clone)]
pub struct TextureMaterial3d {
    texture: Texture3d,
    sampling: ImageSampling,
    tint: Color,
}

impl TextureMaterial3d {
    /// Creates an immutable material; tint is normalized and fully opaque.
    pub fn new(
        texture: &Texture3d,
        sampling: ImageSampling,
        tint: Color,
    ) -> Result<Self, Texture3dError> {
        if !tint.is_normalized() || tint.alpha() != 1.0 {
            return Err(Texture3dError::InvalidTint);
        }
        Ok(Self {
            texture: texture.clone(),
            sampling,
            tint,
        })
    }
    /// Returns the shared immutable texture handle.
    pub const fn texture(&self) -> &Texture3d {
        &self.texture
    }
    /// Returns nearest or linear mip-zero sampling.
    pub const fn sampling(&self) -> ImageSampling {
        self.sampling
    }
    /// Returns multiplicative opaque linear-RGBA tint.
    pub const fn tint(&self) -> Color {
        self.tint
    }
    pub(super) fn bind_group(&self) -> &wgpu::BindGroup {
        self.texture.bind_group(self.sampling)
    }
    pub(super) fn with_restored_texture(&self, texture: &Texture3d) -> Self {
        Self {
            texture: texture.clone(),
            sampling: self.sampling,
            tint: self.tint,
        }
    }
}

#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(super) struct MeshUvGpu {
    pub(super) coordinate: [f32; 2],
}

impl MeshUvGpu {
    const ATTRIBUTES: [wgpu::VertexAttribute; 1] = wgpu::vertex_attr_array![5 => Float32x2];
    pub(super) const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
        array_stride: std::mem::size_of::<Self>() as u64,
        step_mode: wgpu::VertexStepMode::Vertex,
        attributes: &Self::ATTRIBUTES,
    };
}

pub(super) struct MeshTextureRenderer {
    pub(super) layout: wgpu::BindGroupLayout,
    pub(super) retained_pipeline: wgpu::RenderPipeline,
    pub(super) clipped_pipeline: wgpu::RenderPipeline,
}

impl MeshTextureRenderer {
    pub(super) fn new(
        device: &wgpu::Device,
        format: wgpu::TextureFormat,
        camera_layout: &wgpu::BindGroupLayout,
    ) -> Self {
        let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("sim-engine 3D texture 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::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
            ],
        });
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("sim-engine textured 3D surfaces"),
            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("mesh3d_texture.wgsl"))),
        });
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("sim-engine textured 3D pipeline layout"),
            bind_group_layouts: &[Some(camera_layout), Some(&layout)],
            immediate_size: 0,
        });
        let pipeline = |clipped: bool| {
            let buffers = if clipped {
                vec![
                    Some(SurfaceClipVertex::TEXTURED_LAYOUT),
                    Some(MeshInstanceGpu::LAYOUT),
                ]
            } else {
                vec![
                    Some(MeshVertexGpu::LAYOUT),
                    Some(MeshUvGpu::LAYOUT),
                    Some(MeshInstanceGpu::LAYOUT),
                ]
            };
            device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                label: Some("sim-engine opaque textured 3D pipeline"),
                layout: Some(&pipeline_layout),
                vertex: wgpu::VertexState {
                    module: &shader,
                    entry_point: Some(if clipped {
                        "clipped_vs_main"
                    } else {
                        "retained_vs_main"
                    }),
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                    buffers: &buffers,
                },
                primitive: wgpu::PrimitiveState {
                    topology: wgpu::PrimitiveTopology::TriangleList,
                    ..Default::default()
                },
                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::default(),
                }),
                multisample: wgpu::MultisampleState::default(),
                fragment: Some(wgpu::FragmentState {
                    module: &shader,
                    entry_point: Some("fs_main"),
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                    targets: &[Some(wgpu::ColorTargetState {
                        format,
                        blend: None,
                        write_mask: wgpu::ColorWrites::ALL,
                    })],
                }),
                multiview_mask: None,
                cache: None,
            })
        };
        Self {
            retained_pipeline: pipeline(false),
            clipped_pipeline: pipeline(true),
            layout,
        }
    }
}

impl WgpuRenderer {
    /// Creates immutable opaque sRGB RGBA8 texels with shared nearest/linear bindings.
    /// Image/device limits and every alpha byte are validated before GPU creation.
    pub fn create_texture3d_rgba8(
        &self,
        width: u32,
        height: u32,
        pixels: Vec<u8>,
        budget: ImageBudget,
    ) -> Result<Texture3d, Texture3dError> {
        create_texture(
            &self.device,
            &self.queue,
            &self.renderer_identity,
            &self.mesh3d_renderer.textures.layout,
            width,
            height,
            pixels,
            budget,
        )
    }

    /// Restores exact retained pixels on the current device. Existing handles
    /// remain unchanged; atomic `restore_scene3d` restores shared textures once.
    pub fn restore_texture3d(&self, source: &Texture3d) -> Result<Texture3d, Texture3dError> {
        image::validate_image_shape(
            source.size().0,
            source.size().1,
            source.pixels().len(),
            source.budget(),
            self.device.limits().max_texture_dimension_2d,
        )
        .map_err(Texture3dError::Image)?;
        let pixels = copy_recovery_pixels(source)?;
        self.create_texture3d_rgba8(source.size().0, source.size().1, pixels, source.budget())
    }

    /// Associates a shared material with a new retained mesh handle without
    /// allocating/uploading topology. Existing clones and scene users keep
    /// their previous material; rebind selected IDs using `Scene3d::set_mesh`.
    pub fn with_mesh3d_material(
        &self,
        mesh: &RetainedMesh3d,
        material: &TextureMaterial3d,
    ) -> Result<RetainedMesh3d, Texture3dError> {
        attach_material(&self.renderer_identity, mesh, material)
    }
}

pub(super) fn attach_material(
    identity: &Arc<()>,
    mesh: &RetainedMesh3d,
    material: &TextureMaterial3d,
) -> Result<RetainedMesh3d, Texture3dError> {
    if !Arc::ptr_eq(identity, &mesh.renderer_identity) || !material.texture.belongs_to(identity) {
        return Err(Texture3dError::RendererMismatch);
    }
    if mesh.source.texture_coordinates().len() != mesh.source.vertices().len()
        || mesh.source.triangle_count() == 0
        || mesh.texture_coordinate_buffer.is_none()
    {
        return Err(Texture3dError::MissingTextureCoordinates);
    }
    let mut result = mesh.clone();
    result.material = Some(material.clone());
    Ok(result)
}

#[allow(clippy::too_many_arguments)]
pub(super) fn create_texture(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    identity: &Arc<()>,
    layout: &wgpu::BindGroupLayout,
    width: u32,
    height: u32,
    pixels: Vec<u8>,
    budget: ImageBudget,
) -> Result<Texture3d, Texture3dError> {
    image::validate_image_shape(
        width,
        height,
        pixels.len(),
        budget,
        device.limits().max_texture_dimension_2d,
    )
    .map_err(Texture3dError::Image)?;
    if let Some(texel) = pixels.chunks_exact(4).position(|pixel| pixel[3] != 255) {
        return Err(Texture3dError::NonOpaquePixel { texel });
    }
    let image = image::create_image_resources(
        device,
        queue,
        Arc::clone(identity),
        width,
        height,
        pixels,
        budget,
    )
    .map_err(Texture3dError::Image)?;
    let binding = |filter| {
        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("sim-engine 3D texture sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            mag_filter: filter,
            min_filter: filter,
            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
            ..Default::default()
        });
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("sim-engine 3D texture binding"),
            layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&image.view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&sampler),
                },
            ],
        })
    };
    let nearest = binding(wgpu::FilterMode::Nearest);
    let linear = binding(wgpu::FilterMode::Linear);
    Ok(Texture3d {
        storage: Arc::new(TextureStorage {
            renderer_identity: Arc::clone(identity),
            image,
            nearest,
            linear,
        }),
    })
}

pub(super) fn copy_recovery_pixels(source: &Texture3d) -> Result<Vec<u8>, Texture3dError> {
    let mut pixels = Vec::new();
    pixels
        .try_reserve_exact(source.pixels().len())
        .map_err(|_| {
            Texture3dError::Image(ImageError::AllocationFailed {
                requested_bytes: source.pixels().len(),
            })
        })?;
    pixels.extend_from_slice(source.pixels());
    if pixels.capacity() > source.budget().max_bytes() {
        return Err(Texture3dError::Image(ImageError::BudgetExceeded {
            limit: source.budget().max_bytes(),
            actual: pixels.capacity(),
        }));
    }
    Ok(pixels)
}

pub(super) struct PreparedTextureRestore {
    pub(super) key: usize,
    pub(super) source: Texture3d,
    pub(super) pixels: Vec<u8>,
}

pub(super) fn prepare_scene_restoration(
    device: &wgpu::Device,
    identity: &Arc<()>,
    scene: &Scene3d,
) -> Result<Vec<PreparedTextureRestore>, Mesh3dResourceError> {
    let mut prepared = Vec::new();
    prepared
        .try_reserve_exact(scene.object_count())
        .map_err(|_| Mesh3dResourceError::HostAllocationFailed {
            requested_bytes: scene
                .object_count()
                .saturating_mul(std::mem::size_of::<PreparedTextureRestore>())
                as u64,
        })?;
    for instance in scene.instances() {
        if let Some(material) = instance.mesh.material()
            && !material.texture().belongs_to(identity)
        {
            prepared.push(PreparedTextureRestore {
                key: material.texture().identity_key(),
                source: material.texture().clone(),
                pixels: Vec::new(),
            });
        }
    }
    prepared.sort_unstable_by_key(|texture| texture.key);
    prepared.dedup_by_key(|texture| texture.key);
    for texture in &mut prepared {
        let source = &texture.source;
        image::validate_image_shape(
            source.size().0,
            source.size().1,
            source.pixels().len(),
            source.budget(),
            device.limits().max_texture_dimension_2d,
        )
        .map_err(|error| Mesh3dResourceError::Texture(Texture3dError::Image(error)))?;
        texture.pixels = copy_recovery_pixels(source).map_err(Mesh3dResourceError::Texture)?;
    }
    Ok(prepared)
}