scena 1.1.0

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
use std::sync::Arc;

use base64::Engine;

use crate::diagnostics::AssetError;
use crate::material::{Color, TextureColorSpace};

use super::AssetPath;

#[path = "texture_ktx2.rs"]
mod texture_ktx2;

#[cfg(feature = "ktx2")]
use texture_ktx2::validate_rgba8_payload_len;
use texture_ktx2::{decode_ktx2_basisu_rgba8, ktx2_descriptor_only_error};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextureDesc {
    path: AssetPath,
    color_space: TextureColorSpace,
    sampler: TextureSamplerDesc,
    source_format: TextureSourceFormat,
    pixels: Option<Arc<TexturePixels>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TexturePixels {
    levels: Vec<TextureMipLevel>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TextureMipLevel {
    width: u32,
    height: u32,
    rgba8: Vec<u8>,
}

impl TexturePixels {
    fn single_level(width: u32, height: u32, rgba8: Vec<u8>) -> Self {
        Self {
            levels: vec![TextureMipLevel {
                width,
                height,
                rgba8,
            }],
        }
    }

    #[cfg(feature = "ktx2")]
    fn from_mip_levels(path: &AssetPath, levels: Vec<TextureMipLevel>) -> Result<Self, AssetError> {
        if levels.is_empty() {
            return Err(AssetError::Parse {
                path: path.as_str().to_string(),
                reason: "texture decode returned zero mip levels".to_string(),
            });
        }
        for (index, level) in levels.iter().enumerate() {
            validate_rgba8_payload_len(path, level.width, level.height, level.rgba8.len())
                .map_err(|error| match error {
                    AssetError::Parse { path, reason } => AssetError::Parse {
                        path,
                        reason: format!("mip level {index}: {reason}"),
                    },
                    other => other,
                })?;
        }
        Ok(Self { levels })
    }

    fn base_level(&self) -> Option<&TextureMipLevel> {
        self.levels.first()
    }

    fn mip_metadata(&self) -> Vec<(u32, u32, usize)> {
        self.levels
            .iter()
            .map(|level| (level.width, level.height, level.rgba8.len()))
            .collect()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TextureSourceFormat {
    Png,
    Jpeg,
    Webp,
    Ktx2Basisu,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TextureFilter {
    Nearest,
    Linear,
    NearestMipmapNearest,
    LinearMipmapNearest,
    NearestMipmapLinear,
    LinearMipmapLinear,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TextureWrap {
    ClampToEdge,
    MirroredRepeat,
    Repeat,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextureSamplerDesc {
    mag_filter: Option<TextureFilter>,
    min_filter: Option<TextureFilter>,
    wrap_s: TextureWrap,
    wrap_t: TextureWrap,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct TextureCacheKey {
    pub(crate) path: AssetPath,
    pub(crate) color_space: TextureColorSpace,
    pub(crate) sampler: TextureSamplerDesc,
    pub(crate) source_format: TextureSourceFormat,
}

impl TextureDesc {
    pub(crate) fn new_with_bytes(
        path: AssetPath,
        color_space: TextureColorSpace,
        sampler: TextureSamplerDesc,
        source_format: TextureSourceFormat,
        source_bytes: Option<&[u8]>,
    ) -> Result<Self, AssetError> {
        let pixels =
            decode_texture_pixels(&path, color_space, source_format, source_bytes)?.map(Arc::new);
        Ok(Self {
            path,
            color_space,
            sampler,
            source_format,
            pixels,
        })
    }

    pub fn path(&self) -> &AssetPath {
        &self.path
    }

    pub const fn color_space(&self) -> TextureColorSpace {
        self.color_space
    }

    pub const fn sampler(&self) -> TextureSamplerDesc {
        self.sampler
    }

    pub const fn source_format(&self) -> TextureSourceFormat {
        self.source_format
    }

    pub fn has_decoded_pixels(&self) -> bool {
        self.pixels.is_some()
    }

    pub fn decoded_dimensions(&self) -> Option<(u32, u32)> {
        self.pixels
            .as_ref()
            .and_then(|pixels| pixels.base_level())
            .map(|level| (level.width, level.height))
    }

    pub fn decoded_rgba8(&self) -> Option<(u32, u32, &[u8])> {
        self.pixels
            .as_ref()
            .and_then(|pixels| pixels.base_level())
            .map(|level| (level.width, level.height, level.rgba8.as_slice()))
    }

    pub fn decoded_mip_metadata(&self) -> Option<Vec<(u32, u32, usize)>> {
        self.pixels.as_ref().map(|pixels| pixels.mip_metadata())
    }

    pub(crate) fn decode_missing_pixels_from_bytes(
        &mut self,
        source_bytes: Option<&[u8]>,
    ) -> Result<(), AssetError> {
        if self.pixels.is_none() {
            self.pixels = decode_texture_pixels(
                &self.path,
                self.color_space,
                self.source_format,
                source_bytes,
            )?
            .map(Arc::new);
        }
        Ok(())
    }

    pub(crate) fn sample_bilinear(&self, uv: [f32; 2]) -> Option<Color> {
        let pixels = self.pixels.as_ref()?;
        let level = pixels.base_level()?;
        let u = wrap_texture_coordinate(uv[0], self.sampler.wrap_s);
        let v = wrap_texture_coordinate(uv[1], self.sampler.wrap_t);
        let x = u * level.width.saturating_sub(1) as f32;
        let y = v * level.height.saturating_sub(1) as f32;
        let x0 = x.floor() as u32;
        let y0 = y.floor() as u32;
        let x1 = (x0 + 1).min(level.width.saturating_sub(1));
        let y1 = (y0 + 1).min(level.height.saturating_sub(1));
        let tx = x - x0 as f32;
        let ty = y - y0 as f32;
        let c00 = self.sample_pixel_color(level, x0, y0)?;
        let c10 = self.sample_pixel_color(level, x1, y0)?;
        let c01 = self.sample_pixel_color(level, x0, y1)?;
        let c11 = self.sample_pixel_color(level, x1, y1)?;
        Some(lerp_color(
            lerp_color(c00, c10, tx),
            lerp_color(c01, c11, tx),
            ty,
        ))
    }

    fn sample_pixel_color(&self, level: &TextureMipLevel, x: u32, y: u32) -> Option<Color> {
        let offset = ((y * level.width + x) as usize) * 4;
        let rgba = level.rgba8.get(offset..offset + 4)?;
        let alpha = f32::from(rgba[3]) / 255.0;
        let mut color = match self.color_space {
            TextureColorSpace::Srgb => Color::from_srgb_u8(rgba[0], rgba[1], rgba[2]),
            TextureColorSpace::Linear => Color::from_linear_rgba(
                f32::from(rgba[0]) / 255.0,
                f32::from(rgba[1]) / 255.0,
                f32::from(rgba[2]) / 255.0,
                alpha,
            ),
        };
        color.a = alpha;
        Some(color)
    }
}

fn lerp_color(left: Color, right: Color, amount: f32) -> Color {
    Color::from_linear_rgba(
        left.r + (right.r - left.r) * amount,
        left.g + (right.g - left.g) * amount,
        left.b + (right.b - left.b) * amount,
        left.a + (right.a - left.a) * amount,
    )
}

impl TextureSamplerDesc {
    pub const fn new(
        mag_filter: Option<TextureFilter>,
        min_filter: Option<TextureFilter>,
        wrap_s: TextureWrap,
        wrap_t: TextureWrap,
    ) -> Self {
        Self {
            mag_filter,
            min_filter,
            wrap_s,
            wrap_t,
        }
    }

    pub const fn mag_filter(self) -> Option<TextureFilter> {
        self.mag_filter
    }

    pub const fn min_filter(self) -> Option<TextureFilter> {
        self.min_filter
    }

    pub const fn wrap_s(self) -> TextureWrap {
        self.wrap_s
    }

    pub const fn wrap_t(self) -> TextureWrap {
        self.wrap_t
    }
}

impl Default for TextureSamplerDesc {
    fn default() -> Self {
        Self {
            mag_filter: None,
            min_filter: None,
            wrap_s: TextureWrap::Repeat,
            wrap_t: TextureWrap::Repeat,
        }
    }
}

pub(crate) fn validate_texture_source_format(
    path: &AssetPath,
) -> Result<TextureSourceFormat, AssetError> {
    let lower = path.as_str().to_ascii_lowercase();
    if lower.ends_with(".png") || lower.starts_with("data:image/png") {
        return Ok(TextureSourceFormat::Png);
    }
    if lower.ends_with(".jpg") || lower.ends_with(".jpeg") || lower.starts_with("data:image/jpeg") {
        return Ok(TextureSourceFormat::Jpeg);
    }
    if lower.ends_with(".webp") || lower.starts_with("data:image/webp") {
        return Ok(TextureSourceFormat::Webp);
    }
    #[cfg(feature = "ktx2")]
    {
        if lower.ends_with(".ktx2") || lower.starts_with("data:image/ktx2") {
            return Ok(TextureSourceFormat::Ktx2Basisu);
        }
    }
    Err(AssetError::UnsupportedTextureFormat {
        path: path.as_str().to_string(),
        help: "supported texture format set is PNG, JPEG, and WebP; compressed texture decoders need an explicit feature/policy",
    })
}

fn wrap_texture_coordinate(value: f32, wrap: TextureWrap) -> f32 {
    if !value.is_finite() {
        return 0.0;
    }
    match wrap {
        TextureWrap::Repeat => value.rem_euclid(1.0),
        TextureWrap::ClampToEdge => value.clamp(0.0, 1.0),
        TextureWrap::MirroredRepeat => {
            let wrapped = value.rem_euclid(2.0);
            if wrapped <= 1.0 {
                wrapped
            } else {
                2.0 - wrapped
            }
        }
    }
}

fn decode_texture_pixels(
    path: &AssetPath,
    color_space: TextureColorSpace,
    source_format: TextureSourceFormat,
    source_bytes: Option<&[u8]>,
) -> Result<Option<TexturePixels>, AssetError> {
    let bytes = if let Some(bytes) = source_bytes {
        bytes.to_vec()
    } else if path.as_str().starts_with("data:") {
        decode_data_uri(path)?
    } else {
        return match source_format {
            TextureSourceFormat::Ktx2Basisu => Err(ktx2_descriptor_only_error(path)),
            TextureSourceFormat::Png | TextureSourceFormat::Jpeg | TextureSourceFormat::Webp => {
                Ok(None)
            }
        };
    };
    match source_format {
        TextureSourceFormat::Png => decode_png_rgba8(path, &bytes).map(Some),
        TextureSourceFormat::Jpeg => decode_jpeg_rgba8(path, &bytes).map(Some),
        TextureSourceFormat::Webp => Ok(None),
        TextureSourceFormat::Ktx2Basisu => {
            decode_ktx2_basisu_rgba8(path, &bytes, color_space).map(Some)
        }
    }
}

fn decode_data_uri(path: &AssetPath) -> Result<Vec<u8>, AssetError> {
    let Some((_, encoded)) = path.as_str().split_once(";base64,") else {
        return Err(AssetError::Parse {
            path: path.as_str().to_string(),
            reason: "only base64 texture data URIs are supported for embedded texture decoding"
                .to_string(),
        });
    };
    base64::engine::general_purpose::STANDARD
        .decode(encoded)
        .map_err(|error| AssetError::Parse {
            path: path.as_str().to_string(),
            reason: format!("invalid embedded texture base64: {error}"),
        })
}

fn decode_png_rgba8(path: &AssetPath, bytes: &[u8]) -> Result<TexturePixels, AssetError> {
    decode_via_image_crate(path, bytes, image::ImageFormat::Png)
}

fn decode_jpeg_rgba8(path: &AssetPath, bytes: &[u8]) -> Result<TexturePixels, AssetError> {
    decode_via_image_crate(path, bytes, image::ImageFormat::Jpeg)
}

/// Stage B2: delegate PNG/JPEG decode to the `image` crate. `image` wraps
/// the same `png` and `jpeg-decoder` crates scena previously used directly,
/// but its unified `DynamicImage::into_rgba8` handles every color-type
/// expansion (RGB→RGBA, Grayscale→RGBA, Grayscale+Alpha→RGBA, 16-bit→8-bit,
/// CMYK→RGBA) without us re-implementing each path by hand. Removes the
/// hand-rolled `cmyk_to_rgba8` + 4 color-type match arms that previously
/// lived here.
fn decode_via_image_crate(
    path: &AssetPath,
    bytes: &[u8],
    format: image::ImageFormat,
) -> Result<TexturePixels, AssetError> {
    let image =
        image::load_from_memory_with_format(bytes, format).map_err(|error| AssetError::Parse {
            path: path.as_str().to_string(),
            reason: format!("invalid texture payload: {error}"),
        })?;
    let rgba = image.into_rgba8();
    let width = rgba.width();
    let height = rgba.height();
    Ok(TexturePixels::single_level(width, height, rgba.into_raw()))
}