authoravatarclover caruso<git@paperclover.net>2026-12-01 22:10:15-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-01 22:39:16-08:00
logd55c5cbdd1064226377fa4510c5e47cc9647091e
tree673e070c4bf446b3f92ef530cff49eecb604be13
parent11dff3d99e505db517ac8cca162d72a8506a55ef
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

feat(engine): add optional AuxChannels (Z, Normal, Vector, BackVector) on CachedTexture


1 files changed, 109 insertions(+), 0 deletions(-)

crates/engine/src/cache.rs+109
......@@ -16,6 +16,96 @@ pub enum CacheMode {
1616 Bypass,
1717}
1818
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`).
21pub const EMPTY_Z_SENTINEL: f32 = -1.0e30;
22
23/// A single GPU auxiliary image channel texture (`Z`, `Normal`, `Vector`, `BackVector`).
24#[derive(Clone, Debug)]
25pub 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
33impl 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)]
82pub 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
93impl 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
19109#[derive(Clone, Debug)]
20110pub struct CachedTexture {
21111 pub texture: Arc<wgpu::Texture>,
......@@ -34,6 +124,8 @@ pub struct CachedTexture {
34124 pub tex_scale: f32,
35125 /// Infinite RoI edge mode propagated from upstream `Merge` (`Some(1)` = Wrap, `Some(3)` = Mirror).
36126 pub edge_mode: Option<u8>,
127 /// Optional Fusion auxiliary channels (`Z`, `Normal`, `Vector`, `BackVector`).
128 pub aux: Option<Arc<AuxChannels>>,
37129 pub content_hash: u64,
38130 pub bytes: u64,
39131}
......@@ -50,6 +142,7 @@ fn bytes_per_pixel(format: wgpu::TextureFormat) -> u64 {
50142 match format {
51143 wgpu::TextureFormat::Rgba32Float => 16,
52144 wgpu::TextureFormat::Rgba16Float => 8,
145 wgpu::TextureFormat::R32Float => 4,
53146 _ => u64::from(format.block_copy_size(None).unwrap_or(4)),
54147 }
55148}
......@@ -114,6 +207,7 @@ impl CachedTexture {
114207 format,
115208 tex_scale: 1.0,
116209 edge_mode: None,
210 aux: None,
117211 content_hash,
118212 bytes,
119213 }
......@@ -141,6 +235,7 @@ impl CachedTexture {
141235 format,
142236 tex_scale: 1.0,
143237 edge_mode: None,
238 aux: None,
144239 content_hash,
145240 bytes,
146241 }
......@@ -149,6 +244,20 @@ impl CachedTexture {
149244 pub fn has_dod(&self) -> bool {
150245 self.origin != [0, 0] || self.size != self.canvas_size
151246 }
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 }
152261}
153262
154263#[derive(Clone, Debug, Default)]