sim-engine 0.4.1

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
509
510
511
512
513
514
515
516
517
518
519
//! Bounded mip chains for independently filtered textures.

use super::{ImageBudget, ImageError, image};

const MAX_LEVELS: usize = u32::BITS as usize;

mod regions;
pub(super) use regions::{MipRegion, MipRegionPlan};

pub(super) fn validate_layout(
    width: u32,
    height: u32,
    source_bytes: usize,
    budget: ImageBudget,
    limit: u32,
    mode: TextureMipmaps3d,
) -> Result<(), ImageError> {
    MipLayout::new(width, height, source_bytes, budget, limit, mode).map(|_| ())
}

/// Optional mip generation for a retained 3D texture.
///
/// This policy applies to a complete, independently filtered image, not to
/// individual cells of a packed atlas. Crop each tile into its own texture
/// before generating mipmaps when neighboring cells must remain isolated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TextureMipmaps3d {
    /// Keep the supplied image unchanged and sample only its original level.
    #[default]
    None,
    /// Generate every level down to 1 x 1 with an area-weighted box filter.
    ///
    /// RGB is filtered in linear light with premultiplied alpha, then converted
    /// back to straight sRGB RGBA8. Source pixels remain byte-exact. Generated
    /// levels with alpha rounded to zero have zero RGB. This does not preserve
    /// alpha-test coverage: thin masked features can disappear in distant mips.
    Generate,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(super) struct MipLevel {
    pub(super) width: u32,
    pub(super) height: u32,
    pub(super) offset: usize,
    pub(super) byte_count: usize,
}

#[derive(Debug)]
pub(in crate::renderer::mesh3d) struct CpuMipChain {
    pixels: Vec<u8>,
    layout: MipLayout,
    budget: ImageBudget,
    mode: TextureMipmaps3d,
}

#[derive(Debug, Clone, Copy)]
struct MipLayout {
    levels: [MipLevel; MAX_LEVELS],
    count: usize,
    byte_count: usize,
}

impl CpuMipChain {
    pub(super) fn new(
        width: u32,
        height: u32,
        pixels: Vec<u8>,
        budget: ImageBudget,
        maximum_texture_dimension: u32,
        mode: TextureMipmaps3d,
    ) -> Result<Self, ImageError> {
        let layout = MipLayout::new(
            width,
            height,
            pixels.len(),
            budget,
            maximum_texture_dimension,
            mode,
        )?;
        // Preflight includes every generated level before reserving storage.
        // An over-capacity caller Vec must not bypass the retained byte limit.
        let mut pixels = if pixels.capacity() > budget.max_bytes() {
            let mut compact = allocate_pixels(layout.byte_count, budget)?;
            compact.extend_from_slice(&pixels);
            compact
        } else {
            let mut pixels = pixels;
            let additional = layout.byte_count - pixels.len();
            pixels
                .try_reserve_exact(additional)
                .map_err(|_| ImageError::AllocationFailed {
                    requested_bytes: layout.byte_count,
                })?;
            validate_capacity(pixels.capacity(), budget)?;
            pixels
        };
        pixels.resize(layout.byte_count, 0);
        if layout.count > 1 {
            let linear_channels = std::array::from_fn::<_, 256, _>(|channel| {
                let encoded = channel as f64 / 255.0;
                if encoded <= 0.04045 {
                    encoded / 12.92
                } else {
                    ((encoded + 0.055) / 1.055).powf(2.4)
                }
            });
            for level in 1..layout.count {
                let source = layout.levels[level - 1];
                let destination = layout.levels[level];
                let (earlier, later) = pixels.split_at_mut(destination.offset);
                downsample(
                    &earlier[source.offset..source.offset + source.byte_count],
                    source.width,
                    source.height,
                    &mut later[..destination.byte_count],
                    destination.width,
                    destination.height,
                    &linear_channels,
                );
            }
        }
        Ok(Self {
            pixels,
            layout,
            budget,
            mode,
        })
    }

    pub(super) fn size(&self) -> (u32, u32) {
        (self.layout.levels[0].width, self.layout.levels[0].height)
    }

    pub(super) fn pixels(&self) -> &[u8] {
        &self.pixels[..self.layout.levels[0].byte_count]
    }

    pub(super) fn level_pixels(&self, level: MipLevel) -> &[u8] {
        &self.pixels[level.offset..level.offset + level.byte_count]
    }

    pub(super) fn levels(&self) -> &[MipLevel] {
        &self.layout.levels[..self.layout.count]
    }

    pub(super) fn budget(&self) -> ImageBudget {
        self.budget
    }

    pub(super) fn mode(&self) -> TextureMipmaps3d {
        self.mode
    }

    pub(super) fn recovery_memory_bytes(&self) -> usize {
        self.pixels.capacity()
    }

    pub(super) fn gpu_allocation_bytes(&self) -> usize {
        self.layout.byte_count
    }

    pub(super) fn validate_device_limit(
        &self,
        maximum_texture_dimension: u32,
    ) -> Result<(), ImageError> {
        MipLayout::new(
            self.size().0,
            self.size().1,
            self.pixels().len(),
            self.budget,
            maximum_texture_dimension,
            self.mode,
        )?;
        Ok(())
    }

    pub(super) fn try_clone(&self) -> Result<Self, ImageError> {
        let mut pixels = allocate_pixels(self.pixels.len(), self.budget)?;
        pixels.extend_from_slice(&self.pixels);
        Ok(Self {
            pixels,
            layout: self.layout,
            budget: self.budget,
            mode: self.mode,
        })
    }

    pub(super) fn allocate_gpu(&self, device: &wgpu::Device) -> wgpu::Texture {
        let (width, height) = self.size();
        device.create_texture(&wgpu::TextureDescriptor {
            label: Some("sim-engine retained 3D texture mip chain"),
            size: wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
            mip_level_count: self.layout.count as u32,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8UnormSrgb,
            usage: wgpu::TextureUsages::TEXTURE_BINDING
                | wgpu::TextureUsages::COPY_DST
                | wgpu::TextureUsages::COPY_SRC,
            view_formats: &[],
        })
    }

    pub(super) fn upload_levels(
        &self,
        queue: &wgpu::Queue,
        texture: &wgpu::Texture,
        first_level: usize,
    ) {
        for (index, level) in self.levels().iter().copied().enumerate().skip(first_level) {
            queue.write_texture(
                wgpu::TexelCopyTextureInfo {
                    texture,
                    mip_level: index as u32,
                    origin: wgpu::Origin3d::ZERO,
                    aspect: wgpu::TextureAspect::All,
                },
                self.level_pixels(level),
                wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(level.width * 4),
                    rows_per_image: Some(level.height),
                },
                wgpu::Extent3d {
                    width: level.width,
                    height: level.height,
                    depth_or_array_layers: 1,
                },
            );
        }
    }

    pub(super) fn patch_base(&mut self, region: super::ImageTexelRect, packed: &[u8]) {
        let width = self.size().0 as usize;
        let row_bytes = region.width() as usize * 4;
        for row in 0..region.height() as usize {
            let offset = ((region.y() as usize + row) * width + region.x() as usize) * 4;
            self.pixels[offset..offset + row_bytes]
                .copy_from_slice(&packed[row * row_bytes..(row + 1) * row_bytes]);
        }
    }

    pub(super) fn region_update_plan(
        &self,
        region: super::ImageTexelRect,
    ) -> Result<MipRegionPlan, ImageError> {
        MipRegionPlan::new(self.levels(), region)
    }

    /// Refilters only planned footprints in a clone of the plan's source layout.
    pub(super) fn regenerate_regions(&mut self, plan: &MipRegionPlan) {
        debug_assert_eq!(plan.size(), self.size());
        debug_assert_eq!(plan.regions().len(), self.levels().len());
        let base = plan.regions()[0];
        if base.x == 0 && base.y == 0 && (base.width, base.height) == self.size() {
            // Preserve the full-image loop specialization for whole-texture
            // updates; dynamic dirty bounds would add work without excluding
            // any texels. Both paths retain the exact same filtering kernel.
            self.regenerate();
            return;
        }
        if self.layout.count <= 1 {
            return;
        }
        let linear_channels = linear_channels();
        for level in 1..self.layout.count {
            let source = self.layout.levels[level - 1];
            let destination = self.layout.levels[level];
            let (earlier, later) = self.pixels.split_at_mut(destination.offset);
            downsample_region(
                &earlier[source.offset..source.offset + source.byte_count],
                source.width,
                source.height,
                &mut later[..destination.byte_count],
                destination.width,
                destination.height,
                &linear_channels,
                plan.regions()[level],
            );
        }
    }

    pub(super) fn regenerate(&mut self) {
        if self.layout.count <= 1 {
            return;
        }
        let linear_channels = linear_channels();
        for level in 1..self.layout.count {
            let source = self.layout.levels[level - 1];
            let destination = self.layout.levels[level];
            let (earlier, later) = self.pixels.split_at_mut(destination.offset);
            downsample(
                &earlier[source.offset..source.offset + source.byte_count],
                source.width,
                source.height,
                &mut later[..destination.byte_count],
                destination.width,
                destination.height,
                &linear_channels,
            );
        }
    }
}

impl MipLayout {
    fn new(
        width: u32,
        height: u32,
        source_byte_count: usize,
        budget: ImageBudget,
        maximum_texture_dimension: u32,
        mode: TextureMipmaps3d,
    ) -> Result<Self, ImageError> {
        image::validate_image_shape(
            width,
            height,
            source_byte_count,
            budget,
            maximum_texture_dimension,
        )?;
        let mut result = Self {
            levels: [MipLevel::default(); MAX_LEVELS],
            count: 0,
            byte_count: 0,
        };
        let mut width = width;
        let mut height = height;
        loop {
            let byte_count = (width as usize)
                .checked_mul(height as usize)
                .and_then(|texels| texels.checked_mul(4))
                .ok_or(ImageError::DimensionsTooLarge)?;
            let offset = result.byte_count;
            result.byte_count = offset
                .checked_add(byte_count)
                .ok_or(ImageError::DimensionsTooLarge)?;
            if result.byte_count > budget.max_bytes() {
                return Err(ImageError::BudgetExceeded {
                    limit: budget.max_bytes(),
                    actual: result.byte_count,
                });
            }
            result.levels[result.count] = MipLevel {
                width,
                height,
                offset,
                byte_count,
            };
            result.count += 1;
            if mode == TextureMipmaps3d::None || (width == 1 && height == 1) {
                return Ok(result);
            }
            width = (width / 2).max(1);
            height = (height / 2).max(1);
        }
    }
}

fn allocate_pixels(byte_count: usize, budget: ImageBudget) -> Result<Vec<u8>, ImageError> {
    let mut pixels = Vec::new();
    pixels
        .try_reserve_exact(byte_count)
        .map_err(|_| ImageError::AllocationFailed {
            requested_bytes: byte_count,
        })?;
    validate_capacity(pixels.capacity(), budget)?;
    Ok(pixels)
}

fn validate_capacity(capacity: usize, budget: ImageBudget) -> Result<(), ImageError> {
    if capacity > budget.max_bytes() {
        Err(ImageError::BudgetExceeded {
            limit: budget.max_bytes(),
            actual: capacity,
        })
    } else {
        Ok(())
    }
}

#[allow(clippy::too_many_arguments)]
fn downsample(
    source: &[u8],
    source_width: u32,
    source_height: u32,
    destination: &mut [u8],
    width: u32,
    height: u32,
    linear_channels: &[f64; 256],
) {
    // Integer-halving dimensions still need the entire source extent. Using a
    // fractional footprint includes the last row/column of odd-sized images.
    let horizontal_ratio = f64::from(source_width) / f64::from(width);
    let vertical_ratio = f64::from(source_height) / f64::from(height);
    for y in 0..height {
        let top = f64::from(y) * vertical_ratio;
        let bottom = (f64::from(y) + 1.0) * vertical_ratio;
        for x in 0..width {
            let left = f64::from(x) * horizontal_ratio;
            let right = (f64::from(x) + 1.0) * horizontal_ratio;
            let mut channels = [0.0; 3];
            let mut alpha_weight = 0.0;
            let mut total_weight = 0.0;
            for source_y in (top.floor() as u32)..(bottom.ceil() as u32).min(source_height) {
                let vertical_weight =
                    bottom.min(f64::from(source_y) + 1.0) - top.max(f64::from(source_y));
                for source_x in (left.floor() as u32)..(right.ceil() as u32).min(source_width) {
                    let horizontal_weight =
                        right.min(f64::from(source_x) + 1.0) - left.max(f64::from(source_x));
                    let weight = vertical_weight * horizontal_weight;
                    let index = (source_y as usize * source_width as usize + source_x as usize) * 4;
                    let alpha = f64::from(source[index + 3]) / 255.0;
                    for channel in 0..3 {
                        channels[channel] +=
                            linear_channels[source[index + channel] as usize] * alpha * weight;
                    }
                    alpha_weight += alpha * weight;
                    total_weight += weight;
                }
            }
            let index = (y as usize * width as usize + x as usize) * 4;
            let alpha = (alpha_weight / total_weight * 255.0).round() as u8;
            if alpha == 0 {
                destination[index..index + 4].fill(0);
                continue;
            }
            for channel in 0..3 {
                destination[index + channel] = linear_to_srgb8(channels[channel] / alpha_weight);
            }
            destination[index + 3] = alpha;
        }
    }
}

// Keep the full-image loop above specialized for zero-origin complete extents.
// This regional twin has identical arithmetic/rounding but dynamic loop bounds;
// frozen full-filter differential tests cover both paths, including odd sizes.
#[allow(clippy::too_many_arguments)]
fn downsample_region(
    source: &[u8],
    source_width: u32,
    source_height: u32,
    destination: &mut [u8],
    width: u32,
    height: u32,
    linear_channels: &[f64; 256],
    region: MipRegion,
) {
    // Integer-halving dimensions still need the entire source extent. Using a
    // fractional footprint includes the last row/column of odd-sized images.
    let horizontal_ratio = f64::from(source_width) / f64::from(width);
    let vertical_ratio = f64::from(source_height) / f64::from(height);
    for y in region.y..region.y + region.height {
        let top = f64::from(y) * vertical_ratio;
        let bottom = (f64::from(y) + 1.0) * vertical_ratio;
        for x in region.x..region.x + region.width {
            let left = f64::from(x) * horizontal_ratio;
            let right = (f64::from(x) + 1.0) * horizontal_ratio;
            let mut channels = [0.0; 3];
            let mut alpha_weight = 0.0;
            let mut total_weight = 0.0;
            for source_y in (top.floor() as u32)..(bottom.ceil() as u32).min(source_height) {
                let vertical_weight =
                    bottom.min(f64::from(source_y) + 1.0) - top.max(f64::from(source_y));
                for source_x in (left.floor() as u32)..(right.ceil() as u32).min(source_width) {
                    let horizontal_weight =
                        right.min(f64::from(source_x) + 1.0) - left.max(f64::from(source_x));
                    let weight = vertical_weight * horizontal_weight;
                    let index = (source_y as usize * source_width as usize + source_x as usize) * 4;
                    let alpha = f64::from(source[index + 3]) / 255.0;
                    for channel in 0..3 {
                        channels[channel] +=
                            linear_channels[source[index + channel] as usize] * alpha * weight;
                    }
                    alpha_weight += alpha * weight;
                    total_weight += weight;
                }
            }
            let index = (y as usize * width as usize + x as usize) * 4;
            let alpha = (alpha_weight / total_weight * 255.0).round() as u8;
            if alpha == 0 {
                destination[index..index + 4].fill(0);
                continue;
            }
            for channel in 0..3 {
                destination[index + channel] = linear_to_srgb8(channels[channel] / alpha_weight);
            }
            destination[index + 3] = alpha;
        }
    }
}

fn linear_to_srgb8(linear: f64) -> u8 {
    let encoded = if linear <= 0.0031308 {
        linear * 12.92
    } else {
        1.055 * linear.powf(1.0 / 2.4) - 0.055
    };
    (encoded.clamp(0.0, 1.0) * 255.0).round() as u8
}

fn linear_channels() -> [f64; 256] {
    std::array::from_fn(|channel| {
        let encoded = channel as f64 / 255.0;
        if encoded <= 0.04045 {
            encoded / 12.92
        } else {
            ((encoded + 0.055) / 1.055).powf(2.4)
        }
    })
}

#[cfg(test)]
mod tests;