From 57cfccbd0300cb68d9c5d72cabdc6089882804db Mon Sep 17 00:00:00 2001 From: Ralph Kuepper Date: Tue, 17 Nov 2026 15:41:08 +0100 Subject: [PATCH] feat: cap the size of model textures at load (setMaxTextureSize) A PNG / JPEG / DDS texture in a glTF model larger than the cap is downscaled by a power of two when it is decoded (triangle filter, the same as dropping its top mip levels). 0 = no cap, the default, so nothing changes for a game that does not call it. KTX2 block-compressed textures are left alone. Why: a 4096 px RGBA texture with mips is 85 MB, and the shooter's models carry about 6.4 GB of them as authored. A 4 GB card, or an integrated GPU sharing an 8 GB machine's memory, cannot hold that; at 2048 it is a quarter. FFI bloom_set_max_texture_size (manifest, watchos stub, web mirror, validate-ffi list); TS setMaxTextureSize in bloom/core. Unit tests cover the size arithmetic. --- native/shared/src/ffi_core/visual.rs | 8 ++++ native/shared/src/models.rs | 49 +++++++++++++++++++++ native/shared/src/models_gltf_texture_io.rs | 22 +++++++++ native/watchos/src/ffi_stubs.rs | 3 ++ native/web/src/render_settings.rs | 5 +++ package.json | 7 +++ src/core/index.ts | 14 ++++++ tools/validate-ffi.js | 1 + 8 files changed, 109 insertions(+) diff --git a/native/shared/src/ffi_core/visual.rs b/native/shared/src/ffi_core/visual.rs index 039b49ae..2964313a 100644 --- a/native/shared/src/ffi_core/visual.rs +++ b/native/shared/src/ffi_core/visual.rs @@ -369,6 +369,14 @@ macro_rules! __bloom_ffi_visual { }) } + // bloom_set_max_texture_size + #[no_mangle] + pub extern "C" fn bloom_set_max_texture_size(px: f64) -> f64 { + $crate::ffi::guard_applied("bloom_set_max_texture_size", move || { + $crate::models::set_max_model_texture_size(if px > 0.0 { px as u32 } else { 0 }); + }) + } + // bloom_set_texture_streaming #[no_mangle] pub extern "C" fn bloom_set_texture_streaming(budget_mb: f64) -> f64 { diff --git a/native/shared/src/models.rs b/native/shared/src/models.rs index cd3a64d6..15312e45 100644 --- a/native/shared/src/models.rs +++ b/native/shared/src/models.rs @@ -541,6 +541,55 @@ pub struct SkeletonData { pub use crate::anim_mixer::AnimMixer; +/// The largest edge, in pixels, a model texture decoded from PNG / JPEG / DDS +/// keeps: a larger one is downscaled by a power of two at load (the same as +/// dropping its top mip levels). 0 = no cap, the default. Read at decode, so +/// it applies to models loaded after it is set. KTX2 block-compressed +/// textures are not touched (they arrive with their own mips and budget). +/// A 4096 px RGBA texture with mips is 85 MB; at 2048 it is 21 MB. +static MAX_MODEL_TEXTURE_SIZE: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + +pub fn set_max_model_texture_size(px: u32) { + MAX_MODEL_TEXTURE_SIZE.store(px, std::sync::atomic::Ordering::Relaxed); +} + +pub fn max_model_texture_size() -> u32 { + MAX_MODEL_TEXTURE_SIZE.load(std::sync::atomic::Ordering::Relaxed) +} + +/// The size an image of `w` x `h` is decoded to under a `cap` (0 = none): +/// halved until its larger edge fits, never below 1 px. +pub fn capped_texture_size(w: u32, h: u32, cap: u32) -> (u32, u32) { + let (mut w, mut h) = (w, h); + if cap == 0 { + return (w, h); + } + while w.max(h) > cap && (w > 1 || h > 1) { + w = (w / 2).max(1); + h = (h / 2).max(1); + } + (w, h) +} + +#[cfg(test)] +mod texture_cap_tests { + use super::capped_texture_size; + + #[test] + fn halves_until_the_larger_edge_fits() { + assert_eq!(capped_texture_size(4096, 4096, 2048), (2048, 2048)); + assert_eq!(capped_texture_size(4096, 2048, 1024), (1024, 512)); + assert_eq!(capped_texture_size(3000, 1000, 2048), (1500, 500)); + assert_eq!(capped_texture_size(4096, 1, 1024), (1024, 1)); + } + + #[test] + fn no_cap_or_a_small_image_is_left_alone() { + assert_eq!(capped_texture_size(4096, 4096, 0), (4096, 4096)); + assert_eq!(capped_texture_size(512, 256, 2048), (512, 256)); + } +} + // glTF/GLB parsing lives in models_gltf.rs, models3d-gated so builds // without the feature drop the gltf/image_dds deps (EN-014, re-fixed by // EN-063). Everything else in this file compiles in every configuration. diff --git a/native/shared/src/models_gltf_texture_io.rs b/native/shared/src/models_gltf_texture_io.rs index c9f6c090..50431558 100644 --- a/native/shared/src/models_gltf_texture_io.rs +++ b/native/shared/src/models_gltf_texture_io.rs @@ -60,6 +60,28 @@ pub(super) fn swap_extension(uri: &str, new_ext: &str) -> String { /// Lumberyard Bistro that ship BC-compressed textures), falling back /// to the `image` crate for PNG/JPEG/etc. Returns None on failure. pub(super) fn decode_texture_bytes(bytes: &[u8], uri: &str) -> Option<(Vec, u32, u32)> { + decode_texture_bytes_uncapped(bytes, uri).map(|(rgba, w, h)| cap_rgba(rgba, w, h)) +} + +/// Downscale decoded texels to `crate::models::max_model_texture_size()` +/// (see there). Triangle filtering widens with the ratio, so a 4:1 step +/// averages the texels it drops instead of skipping them. +fn cap_rgba(rgba: Vec, w: u32, h: u32) -> (Vec, u32, u32) { + let (tw, th) = crate::models::capped_texture_size(w, h, crate::models::max_model_texture_size()); + if (tw, th) == (w, h) { + return (rgba, w, h); + } + if rgba.len() != (w as usize) * (h as usize) * 4 { + return (rgba, w, h); + } + let Some(img) = image::RgbaImage::from_raw(w, h, rgba) else { + return (Vec::new(), 0, 0); + }; + let small = image::imageops::resize(&img, tw, th, image::imageops::FilterType::Triangle); + (small.into_raw(), tw, th) +} + +fn decode_texture_bytes_uncapped(bytes: &[u8], uri: &str) -> Option<(Vec, u32, u32)> { #[cfg(feature = "image-extras")] if crate::compressed_texture::is_ktx2(bytes) { let image = CompressedImage::from_ktx2(bytes)?; diff --git a/native/watchos/src/ffi_stubs.rs b/native/watchos/src/ffi_stubs.rs index 8a871c8a..56ab7902 100644 --- a/native/watchos/src/ffi_stubs.rs +++ b/native/watchos/src/ffi_stubs.rs @@ -380,6 +380,9 @@ #[no_mangle] pub extern "C" fn bloom_set_shadow_cascade_stagger(_p0: f64) -> f64 { 0.0 } +#[no_mangle] pub extern "C" fn bloom_set_max_texture_size(_p0: f64) -> f64 { + 0.0 +} #[no_mangle] pub extern "C" fn bloom_set_texture_streaming(_p0: f64) -> f64 { 0.0 } diff --git a/native/web/src/render_settings.rs b/native/web/src/render_settings.rs index 2b1ee890..b1664083 100644 --- a/native/web/src/render_settings.rs +++ b/native/web/src/render_settings.rs @@ -110,6 +110,11 @@ pub fn bloom_set_shadow_cascade_stagger(on: f64) -> f64 { 1.0 } #[wasm_bindgen] +pub fn bloom_set_max_texture_size(px: f64) -> f64 { + bloom_shared::models::set_max_model_texture_size(if px > 0.0 { px as u32 } else { 0 }); + 1.0 +} +#[wasm_bindgen] pub fn bloom_set_texture_streaming(_budget_mb: f64) -> f64 { // Not on the web: models arrive as bytes with no file to read levels // from later. 0 tells the game nothing was turned on. diff --git a/package.json b/package.json index d16f2ece..4fc062c2 100644 --- a/package.json +++ b/package.json @@ -2009,6 +2009,13 @@ ], "returns": "f64" }, + { + "name": "bloom_set_max_texture_size", + "params": [ + "f64" + ], + "returns": "f64" + }, { "name": "bloom_set_texture_streaming", "params": [ diff --git a/src/core/index.ts b/src/core/index.ts index e7a45315..d51a9af1 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -41,6 +41,7 @@ declare function bloom_set_sharpen_strength(strength: number): number; declare function bloom_set_ssgi_probe_spacing(pixels: number): number; declare function bloom_set_shadow_distance(metres: number): number; declare function bloom_set_shadow_cascade_stagger(on: number): number; +declare function bloom_set_max_texture_size(px: number): number; declare function bloom_set_texture_streaming(budgetMb: number): number; declare function bloom_set_shadow_filter_4tap(on: number): number; declare function bloom_set_shadow_map_size(texels: number): number; @@ -862,6 +863,19 @@ export function setShadowCascadeStagger(on: boolean): boolean { return bloom_set_shadow_cascade_stagger(on ? 1 : 0) !== 0; } +/** + * The largest edge, in pixels, a model texture keeps (0 = no cap, the + * default). A PNG / JPEG / DDS texture in a glTF model that is larger is + * downscaled by a power of two when it is decoded, which is the same as + * dropping its top mip levels: 4096 px RGBA with mips is 85 MB, 2048 is 21 MB, + * 1024 is 5 MB. Call it BEFORE loading models; it applies to models loaded + * afterwards. KTX2 block-compressed textures are left alone. For machines + * with little video memory, or integrated GPUs sharing system memory. + */ +export function setMaxTextureSize(px: number): boolean { + return bloom_set_max_texture_size(px) !== 0; +} + /** * Texture streaming: keep only the mip levels each texture needs at its * present distance, inside a budget of `budgetMb` megabytes (0 = off, the diff --git a/tools/validate-ffi.js b/tools/validate-ffi.js index 32e34a46..61c0e07b 100644 --- a/tools/validate-ffi.js +++ b/tools/validate-ffi.js @@ -72,6 +72,7 @@ const RENDERER_STATUS_FUNCTIONS = new Set([ 'bloom_set_ssao_downscale', 'bloom_set_shadow_distance', 'bloom_set_shadow_cascade_stagger', + 'bloom_set_max_texture_size', 'bloom_set_texture_streaming', 'bloom_set_shadow_filter_4tap', 'bloom_set_shadow_map_size',