diff --git a/crates/engine/src/cache.rs b/crates/engine/src/cache.rs index 96352dec8114b927dec61372e99443bbba506d05..81bc8c4b6f19edf1f9adab718e2fd55c12a6b382 100644 --- a/crates/engine/src/cache.rs +++ b/crates/engine/src/cache.rs @@ -16,6 +16,96 @@ pub enum CacheMode { Bypass, } +/// Fusion's empty-sky Z-buffer sentinel value (eye-space Z is negative in front of the +/// camera, so closer geometry has a larger/less-negative Z than `-1e30`). +pub const EMPTY_Z_SENTINEL: f32 = -1.0e30; + +/// A single GPU auxiliary image channel texture (`Z`, `Normal`, `Vector`, `BackVector`). +#[derive(Clone, Debug)] +pub struct AuxChannelTex { + pub texture: Arc, + pub view: Arc, + pub size: [u32; 2], + pub format: wgpu::TextureFormat, + pub bytes: u64, +} + +impl AuxChannelTex { + pub fn new(device: &wgpu::Device, size: [u32; 2], format: wgpu::TextureFormat, label: &str) -> Self { + let max_dim = device.limits().max_texture_dimension_2d.max(1); + let (clamped, _) = clamp_dims_to_limit(size[0], size[1], max_dim); + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: clamped[0], + height: clamped[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::STORAGE_BINDING + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + let bytes = texture_bytes(clamped, format); + Self { + texture: Arc::new(texture), + view: Arc::new(view), + size: clamped, + format, + bytes, + } + } + + pub fn from_texture(texture: wgpu::Texture, size: [u32; 2]) -> Self { + let format = texture.format(); + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + let bytes = texture_bytes(size, format); + Self { + texture: Arc::new(texture), + view: Arc::new(view), + size, + format, + bytes, + } + } +} + +/// Optional Fusion auxiliary channels carried alongside RGBA on a `CachedTexture`. +/// Remains `None` (zero allocation, zero runtime overhead) when no aux channels exist. +#[derive(Clone, Debug, Default)] +pub struct AuxChannels { + /// Eye-space Z depth (`R32Float`, negative in front of camera, `EMPTY_Z_SENTINEL` on sky). + pub z: Option, + /// Surface normal `(NX, NY, NZ, 0)` (`Rgba16Float`). + pub normal: Option, + /// Forward motion vector `(vx, vy, 0, 0)` in pixels (`Rgba16Float`). + pub vector: Option, + /// Backward motion vector `(bx, by, 0, 0)` in pixels (`Rgba16Float`). + pub back_vector: Option, +} + +impl AuxChannels { + pub fn is_empty(&self) -> bool { + self.z.is_none() + && self.normal.is_none() + && self.vector.is_none() + && self.back_vector.is_none() + } + + pub fn total_bytes(&self) -> u64 { + self.z.as_ref().map_or(0, |t| t.bytes) + .saturating_add(self.normal.as_ref().map_or(0, |t| t.bytes)) + .saturating_add(self.vector.as_ref().map_or(0, |t| t.bytes)) + .saturating_add(self.back_vector.as_ref().map_or(0, |t| t.bytes)) + } +} + #[derive(Clone, Debug)] pub struct CachedTexture { pub texture: Arc, @@ -34,6 +124,8 @@ pub struct CachedTexture { pub tex_scale: f32, /// Infinite RoI edge mode propagated from upstream `Merge` (`Some(1)` = Wrap, `Some(3)` = Mirror). pub edge_mode: Option, + /// Optional Fusion auxiliary channels (`Z`, `Normal`, `Vector`, `BackVector`). + pub aux: Option>, pub content_hash: u64, pub bytes: u64, } @@ -50,6 +142,7 @@ fn bytes_per_pixel(format: wgpu::TextureFormat) -> u64 { match format { wgpu::TextureFormat::Rgba32Float => 16, wgpu::TextureFormat::Rgba16Float => 8, + wgpu::TextureFormat::R32Float => 4, _ => u64::from(format.block_copy_size(None).unwrap_or(4)), } } @@ -114,6 +207,7 @@ impl CachedTexture { format, tex_scale: 1.0, edge_mode: None, + aux: None, content_hash, bytes, } @@ -141,6 +235,7 @@ impl CachedTexture { format, tex_scale: 1.0, edge_mode: None, + aux: None, content_hash, bytes, } @@ -149,6 +244,20 @@ impl CachedTexture { pub fn has_dod(&self) -> bool { self.origin != [0, 0] || self.size != self.canvas_size } + + /// Attaches (or clears) auxiliary channels and updates `self.bytes` for VRAM tracking. + pub fn set_aux(&mut self, aux: Option>) { + let base = texture_bytes(self.size, self.format); + let aux_filtered = aux.filter(|a| !a.is_empty()); + let aux_bytes = aux_filtered.as_ref().map_or(0, |a| a.total_bytes()); + self.aux = aux_filtered; + self.bytes = base.saturating_add(aux_bytes); + } + + /// Returns a cloned copy of existing `AuxChannels` or an empty default. + pub fn aux_cloned(&self) -> AuxChannels { + self.aux.as_deref().cloned().unwrap_or_default() + } } #[derive(Clone, Debug, Default)]