| ... | @@ -16,6 +16,96 @@ pub enum CacheMode { | ... | @@ -16,6 +16,96 @@ pub enum CacheMode { |
| 16 | Bypass, | 16 | Bypass, |
| 17 | } | 17 | } |
| 18 | | 18 | |
| | 19 | /// Fusion's empty-sky Z-buffer sentinel value (eye-space Z is negative in front of the |
| | 20 | /// camera, so closer geometry has a larger/less-negative Z than `-1e30`). |
| | 21 | pub const EMPTY_Z_SENTINEL: f32 = -1.0e30; |
| | 22 | |
| | 23 | /// A single GPU auxiliary image channel texture (`Z`, `Normal`, `Vector`, `BackVector`). |
| | 24 | #[derive(Clone, Debug)] |
| | 25 | pub struct AuxChannelTex { |
| | 26 | pub texture: Arc<wgpu::Texture>, |
| | 27 | pub view: Arc<wgpu::TextureView>, |
| | 28 | pub size: [u32; 2], |
| | 29 | pub format: wgpu::TextureFormat, |
| | 30 | pub bytes: u64, |
| | 31 | } |
| | 32 | |
| | 33 | impl AuxChannelTex { |
| | 34 | pub fn new(device: &wgpu::Device, size: [u32; 2], format: wgpu::TextureFormat, label: &str) -> Self { |
| | 35 | let max_dim = device.limits().max_texture_dimension_2d.max(1); |
| | 36 | let (clamped, _) = clamp_dims_to_limit(size[0], size[1], max_dim); |
| | 37 | let texture = device.create_texture(&wgpu::TextureDescriptor { |
| | 38 | label: Some(label), |
| | 39 | size: wgpu::Extent3d { |
| | 40 | width: clamped[0], |
| | 41 | height: clamped[1], |
| | 42 | depth_or_array_layers: 1, |
| | 43 | }, |
| | 44 | mip_level_count: 1, |
| | 45 | sample_count: 1, |
| | 46 | dimension: wgpu::TextureDimension::D2, |
| | 47 | format, |
| | 48 | usage: wgpu::TextureUsages::TEXTURE_BINDING |
| | 49 | | wgpu::TextureUsages::STORAGE_BINDING |
| | 50 | | wgpu::TextureUsages::COPY_SRC |
| | 51 | | wgpu::TextureUsages::COPY_DST, |
| | 52 | view_formats: &[], |
| | 53 | }); |
| | 54 | let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); |
| | 55 | let bytes = texture_bytes(clamped, format); |
| | 56 | Self { |
| | 57 | texture: Arc::new(texture), |
| | 58 | view: Arc::new(view), |
| | 59 | size: clamped, |
| | 60 | format, |
| | 61 | bytes, |
| | 62 | } |
| | 63 | } |
| | 64 | |
| | 65 | pub fn from_texture(texture: wgpu::Texture, size: [u32; 2]) -> Self { |
| | 66 | let format = texture.format(); |
| | 67 | let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); |
| | 68 | let bytes = texture_bytes(size, format); |
| | 69 | Self { |
| | 70 | texture: Arc::new(texture), |
| | 71 | view: Arc::new(view), |
| | 72 | size, |
| | 73 | format, |
| | 74 | bytes, |
| | 75 | } |
| | 76 | } |
| | 77 | } |
| | 78 | |
| | 79 | /// Optional Fusion auxiliary channels carried alongside RGBA on a `CachedTexture`. |
| | 80 | /// Remains `None` (zero allocation, zero runtime overhead) when no aux channels exist. |
| | 81 | #[derive(Clone, Debug, Default)] |
| | 82 | pub struct AuxChannels { |
| | 83 | /// Eye-space Z depth (`R32Float`, negative in front of camera, `EMPTY_Z_SENTINEL` on sky). |
| | 84 | pub z: Option<AuxChannelTex>, |
| | 85 | /// Surface normal `(NX, NY, NZ, 0)` (`Rgba16Float`). |
| | 86 | pub normal: Option<AuxChannelTex>, |
| | 87 | /// Forward motion vector `(vx, vy, 0, 0)` in pixels (`Rgba16Float`). |
| | 88 | pub vector: Option<AuxChannelTex>, |
| | 89 | /// Backward motion vector `(bx, by, 0, 0)` in pixels (`Rgba16Float`). |
| | 90 | pub back_vector: Option<AuxChannelTex>, |
| | 91 | } |
| | 92 | |
| | 93 | impl AuxChannels { |
| | 94 | pub fn is_empty(&self) -> bool { |
| | 95 | self.z.is_none() |
| | 96 | && self.normal.is_none() |
| | 97 | && self.vector.is_none() |
| | 98 | && self.back_vector.is_none() |
| | 99 | } |
| | 100 | |
| | 101 | pub fn total_bytes(&self) -> u64 { |
| | 102 | self.z.as_ref().map_or(0, |t| t.bytes) |
| | 103 | .saturating_add(self.normal.as_ref().map_or(0, |t| t.bytes)) |
| | 104 | .saturating_add(self.vector.as_ref().map_or(0, |t| t.bytes)) |
| | 105 | .saturating_add(self.back_vector.as_ref().map_or(0, |t| t.bytes)) |
| | 106 | } |
| | 107 | } |
| | 108 | |
| 19 | #[derive(Clone, Debug)] | 109 | #[derive(Clone, Debug)] |
| 20 | pub struct CachedTexture { | 110 | pub struct CachedTexture { |
| 21 | pub texture: Arc<wgpu::Texture>, | 111 | pub texture: Arc<wgpu::Texture>, |
| ... | @@ -34,6 +124,8 @@ pub struct CachedTexture { | ... | @@ -34,6 +124,8 @@ pub struct CachedTexture { |
| 34 | pub tex_scale: f32, | 124 | pub tex_scale: f32, |
| 35 | /// Infinite RoI edge mode propagated from upstream `Merge` (`Some(1)` = Wrap, `Some(3)` = Mirror). | 125 | /// Infinite RoI edge mode propagated from upstream `Merge` (`Some(1)` = Wrap, `Some(3)` = Mirror). |
| 36 | pub edge_mode: Option<u8>, | 126 | pub edge_mode: Option<u8>, |
| | 127 | /// Optional Fusion auxiliary channels (`Z`, `Normal`, `Vector`, `BackVector`). |
| | 128 | pub aux: Option<Arc<AuxChannels>>, |
| 37 | pub content_hash: u64, | 129 | pub content_hash: u64, |
| 38 | pub bytes: u64, | 130 | pub bytes: u64, |
| 39 | } | 131 | } |
| ... | @@ -50,6 +142,7 @@ fn bytes_per_pixel(format: wgpu::TextureFormat) -> u64 { | ... | @@ -50,6 +142,7 @@ fn bytes_per_pixel(format: wgpu::TextureFormat) -> u64 { |
| 50 | match format { | 142 | match format { |
| 51 | wgpu::TextureFormat::Rgba32Float => 16, | 143 | wgpu::TextureFormat::Rgba32Float => 16, |
| 52 | wgpu::TextureFormat::Rgba16Float => 8, | 144 | wgpu::TextureFormat::Rgba16Float => 8, |
| | 145 | wgpu::TextureFormat::R32Float => 4, |
| 53 | _ => u64::from(format.block_copy_size(None).unwrap_or(4)), | 146 | _ => u64::from(format.block_copy_size(None).unwrap_or(4)), |
| 54 | } | 147 | } |
| 55 | } | 148 | } |
| ... | @@ -114,6 +207,7 @@ impl CachedTexture { | ... | @@ -114,6 +207,7 @@ impl CachedTexture { |
| 114 | format, | 207 | format, |
| 115 | tex_scale: 1.0, | 208 | tex_scale: 1.0, |
| 116 | edge_mode: None, | 209 | edge_mode: None, |
| | 210 | aux: None, |
| 117 | content_hash, | 211 | content_hash, |
| 118 | bytes, | 212 | bytes, |
| 119 | } | 213 | } |
| ... | @@ -141,6 +235,7 @@ impl CachedTexture { | ... | @@ -141,6 +235,7 @@ impl CachedTexture { |
| 141 | format, | 235 | format, |
| 142 | tex_scale: 1.0, | 236 | tex_scale: 1.0, |
| 143 | edge_mode: None, | 237 | edge_mode: None, |
| | 238 | aux: None, |
| 144 | content_hash, | 239 | content_hash, |
| 145 | bytes, | 240 | bytes, |
| 146 | } | 241 | } |
| ... | @@ -149,6 +244,20 @@ impl CachedTexture { | ... | @@ -149,6 +244,20 @@ impl CachedTexture { |
| 149 | pub fn has_dod(&self) -> bool { | 244 | pub fn has_dod(&self) -> bool { |
| 150 | self.origin != [0, 0] || self.size != self.canvas_size | 245 | self.origin != [0, 0] || self.size != self.canvas_size |
| 151 | } | 246 | } |
| | 247 | |
| | 248 | /// Attaches (or clears) auxiliary channels and updates `self.bytes` for VRAM tracking. |
| | 249 | pub fn set_aux(&mut self, aux: Option<Arc<AuxChannels>>) { |
| | 250 | let base = texture_bytes(self.size, self.format); |
| | 251 | let aux_filtered = aux.filter(|a| !a.is_empty()); |
| | 252 | let aux_bytes = aux_filtered.as_ref().map_or(0, |a| a.total_bytes()); |
| | 253 | self.aux = aux_filtered; |
| | 254 | self.bytes = base.saturating_add(aux_bytes); |
| | 255 | } |
| | 256 | |
| | 257 | /// Returns a cloned copy of existing `AuxChannels` or an empty default. |
| | 258 | pub fn aux_cloned(&self) -> AuxChannels { |
| | 259 | self.aux.as_deref().cloned().unwrap_or_default() |
| | 260 | } |
| 152 | } | 261 | } |
| 153 | | 262 | |
| 154 | #[derive(Clone, Debug, Default)] | 263 | #[derive(Clone, Debug, Default)] |