| author | |
| committer | |
| log | b5f5ac0131829759eba196eafc3e8a56bb61effc |
| tree | 60f5fed94271e4fc35dd7554454ca5069d45a52a |
| parent | 8ca9c80ab9057ebd8247c88cca56f8cbb43baccf |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
5 files changed, 2064 insertions(+), 0 deletions(-)
crates/engine/src/eval/fx_fuses.rs created+760| ... | @@ -0,0 +1,760 @@ | ||
| 1 | //! Wave 36 GPU pipelines & evaluators: | ||
| 2 | //! - `VectorDenoise` (motion-compensated bilateral temporal denoiser) | ||
| 3 | //! - `Mandel` (escape-time Mandelbrot / Julia fractal generator with smooth coloring) | ||
| 4 | //! - `UnsharpMask` (separable Gaussian blur + thresholded high-frequency boost) | ||
| 5 | //! - `Letterbox` (aspect-preserving Letterbox & Pan-and-Scan framing) | ||
| 6 | //! - Archive Fuses in `fx_fuses/fuses.rs`: `Fuse.Wipe`, `Fuse.Null`, | ||
| 7 | //! `Fuse.SetMetaData`, `Fuse.Linger`, and `Fuse.OCLRays`. | ||
| 8 | |||
| 9 | mod fuses; | ||
| 10 | #[cfg(test)] | ||
| 11 | mod tests; | ||
| 12 | |||
| 13 | use super::*; | ||
| 14 | use crate::cache::AuxChannelTex; | ||
| 15 | use std::num::NonZeroU64; | ||
| 16 | use std::sync::Arc; | ||
| 17 | |||
| 18 | const FX_FUSES_WGSL: &str = include_str!("../shaders/fx_fuses.wgsl"); | ||
| 19 | const FX_UNIFORM_SLOTS_PER_CHUNK: u32 = 512; | ||
| 20 | |||
| 21 | fn build_fx_wgsl(format: wgpu::TextureFormat, hdr_float: bool) -> String { | ||
| 22 | let storage_fmt = match format { | ||
| 23 | wgpu::TextureFormat::Rgba16Float => "rgba16float", | ||
| 24 | wgpu::TextureFormat::Rgba32Float => "rgba32float", | ||
| 25 | _ => "rgba8unorm", | ||
| 26 | }; | ||
| 27 | let mut src = FX_FUSES_WGSL.to_owned(); | ||
| 28 | if storage_fmt != "rgba8unorm" { | ||
| 29 | src = src.replace( | ||
| 30 | "texture_storage_2d<rgba8unorm, write>", | ||
| 31 | &format!("texture_storage_2d<{storage_fmt}, write>"), | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | if hdr_float { | ||
| 35 | src = src.replace("const HDR_FLOAT: bool = false;", "const HDR_FLOAT: bool = true;"); | ||
| 36 | } | ||
| 37 | src | ||
| 38 | } | ||
| 39 | |||
| 40 | fn tex_entry(binding: u32, filterable: bool) -> wgpu::BindGroupLayoutEntry { | ||
| 41 | wgpu::BindGroupLayoutEntry { | ||
| 42 | binding, | ||
| 43 | visibility: wgpu::ShaderStages::COMPUTE, | ||
| 44 | ty: wgpu::BindingType::Texture { | ||
| 45 | sample_type: wgpu::TextureSampleType::Float { filterable }, | ||
| 46 | view_dimension: wgpu::TextureViewDimension::D2, | ||
| 47 | multisampled: false, | ||
| 48 | }, | ||
| 49 | count: None, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | #[derive(Clone, Copy)] | ||
| 54 | pub(self) enum FxOp { | ||
| 55 | VdWarp, | ||
| 56 | VdAccumColor, | ||
| 57 | VdAccumWeight, | ||
| 58 | VdFinalize, | ||
| 59 | Mandel, | ||
| 60 | UnsharpMask, | ||
| 61 | Letterbox, | ||
| 62 | Wipe, | ||
| 63 | LingerStep, | ||
| 64 | OclRays, | ||
| 65 | } | ||
| 66 | |||
| 67 | struct FxFormatPipelines { | ||
| 68 | bgl: wgpu::BindGroupLayout, | ||
| 69 | vd_warp: wgpu::ComputePipeline, | ||
| 70 | vd_accum_color: wgpu::ComputePipeline, | ||
| 71 | vd_accum_weight: wgpu::ComputePipeline, | ||
| 72 | vd_finalize: wgpu::ComputePipeline, | ||
| 73 | mandel: wgpu::ComputePipeline, | ||
| 74 | unsharp_mask: wgpu::ComputePipeline, | ||
| 75 | letterbox: wgpu::ComputePipeline, | ||
| 76 | wipe: wgpu::ComputePipeline, | ||
| 77 | linger_step: wgpu::ComputePipeline, | ||
| 78 | ocl_rays: wgpu::ComputePipeline, | ||
| 79 | } | ||
| 80 | |||
| 81 | impl FxFormatPipelines { | ||
| 82 | fn pipeline(&self, op: FxOp) -> &wgpu::ComputePipeline { | ||
| 83 | match op { | ||
| 84 | FxOp::VdWarp => &self.vd_warp, | ||
| 85 | FxOp::VdAccumColor => &self.vd_accum_color, | ||
| 86 | FxOp::VdAccumWeight => &self.vd_accum_weight, | ||
| 87 | FxOp::VdFinalize => &self.vd_finalize, | ||
| 88 | FxOp::Mandel => &self.mandel, | ||
| 89 | FxOp::UnsharpMask => &self.unsharp_mask, | ||
| 90 | FxOp::Letterbox => &self.letterbox, | ||
| 91 | FxOp::Wipe => &self.wipe, | ||
| 92 | FxOp::LingerStep => &self.linger_step, | ||
| 93 | FxOp::OclRays => &self.ocl_rays, | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | pub(super) struct FxFusesPipelines { | ||
| 99 | sampler: wgpu::Sampler, | ||
| 100 | uniform_stride: u32, | ||
| 101 | uniform_chunks: Vec<wgpu::Buffer>, | ||
| 102 | uniform_cursor: u32, | ||
| 103 | dummy_aux: AuxChannelTex, | ||
| 104 | by_format: HashMap<(wgpu::TextureFormat, bool), FxFormatPipelines>, | ||
| 105 | } | ||
| 106 | |||
| 107 | impl FxFusesPipelines { | ||
| 108 | pub(super) fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self { | ||
| 109 | let uniform_size = std::mem::size_of::<UniformParams>() as u32; | ||
| 110 | let min_align = device.limits().min_uniform_buffer_offset_alignment.max(1); | ||
| 111 | let uniform_stride = uniform_size.div_ceil(min_align) * min_align; | ||
| 112 | let sampler = device.create_sampler(&wgpu::SamplerDescriptor { | ||
| 113 | label: Some("fx-fuses-linear"), | ||
| 114 | address_mode_u: wgpu::AddressMode::ClampToEdge, | ||
| 115 | address_mode_v: wgpu::AddressMode::ClampToEdge, | ||
| 116 | mag_filter: wgpu::FilterMode::Linear, | ||
| 117 | min_filter: wgpu::FilterMode::Linear, | ||
| 118 | mipmap_filter: wgpu::MipmapFilterMode::Nearest, | ||
| 119 | ..Default::default() | ||
| 120 | }); | ||
| 121 | let dummy_aux = AuxChannelTex::new(device, [1, 1], wgpu::TextureFormat::Rgba16Float, "fx-dummy-aux"); | ||
| 122 | queue.write_texture( | ||
| 123 | dummy_aux.texture.as_image_copy(), | ||
| 124 | &[0u8; 8], | ||
| 125 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(8), rows_per_image: Some(1) }, | ||
| 126 | wgpu::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 }, | ||
| 127 | ); | ||
| 128 | Self { | ||
| 129 | sampler, | ||
| 130 | uniform_stride, | ||
| 131 | uniform_chunks: Vec::new(), | ||
| 132 | uniform_cursor: 0, | ||
| 133 | dummy_aux, | ||
| 134 | by_format: HashMap::new(), | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | pub(super) fn reset_uniform_cursor(&mut self) { | ||
| 139 | self.uniform_cursor = 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | pub(super) fn trim_uniform_chunks(&mut self, max_chunks: usize) { | ||
| 143 | self.uniform_cursor = 0; | ||
| 144 | if self.uniform_chunks.len() > max_chunks { | ||
| 145 | self.uniform_chunks.truncate(max_chunks); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | fn alloc_uniform_slot(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, params: &UniformParams) -> (usize, u32) { | ||
| 150 | let slot_global = self.uniform_cursor; | ||
| 151 | let chunk_idx = (slot_global / FX_UNIFORM_SLOTS_PER_CHUNK) as usize; | ||
| 152 | let slot_in_chunk = slot_global % FX_UNIFORM_SLOTS_PER_CHUNK; | ||
| 153 | while chunk_idx >= self.uniform_chunks.len() { | ||
| 154 | let size = u64::from(FX_UNIFORM_SLOTS_PER_CHUNK) * u64::from(self.uniform_stride); | ||
| 155 | let buf = device.create_buffer(&wgpu::BufferDescriptor { | ||
| 156 | label: Some("fx-fuses-params-ring"), | ||
| 157 | size, | ||
| 158 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, | ||
| 159 | mapped_at_creation: false, | ||
| 160 | }); | ||
| 161 | self.uniform_chunks.push(buf); | ||
| 162 | } | ||
| 163 | let offset = slot_in_chunk * self.uniform_stride; | ||
| 164 | queue.write_buffer(&self.uniform_chunks[chunk_idx], u64::from(offset), bytemuck::bytes_of(params)); | ||
| 165 | self.uniform_cursor = slot_global + 1; | ||
| 166 | (chunk_idx, offset) | ||
| 167 | } | ||
| 168 | |||
| 169 | fn ensure_format(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat, hdr_float: bool) { | ||
| 170 | if self.by_format.contains_key(&(format, hdr_float)) { | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | let u_size = std::mem::size_of::<UniformParams>() as u64; | ||
| 174 | let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { | ||
| 175 | label: Some("fx-fuses-bgl"), | ||
| 176 | entries: &[ | ||
| 177 | tex_entry(0, true), | ||
| 178 | tex_entry(1, true), | ||
| 179 | tex_entry(2, true), | ||
| 180 | tex_entry(3, false), // aux_tex: unfilterable float safe for WebGPU/wasm32 | ||
| 181 | wgpu::BindGroupLayoutEntry { | ||
| 182 | binding: 4, | ||
| 183 | visibility: wgpu::ShaderStages::COMPUTE, | ||
| 184 | ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), | ||
| 185 | count: None, | ||
| 186 | }, | ||
| 187 | wgpu::BindGroupLayoutEntry { | ||
| 188 | binding: 5, | ||
| 189 | visibility: wgpu::ShaderStages::COMPUTE, | ||
| 190 | ty: wgpu::BindingType::StorageTexture { | ||
| 191 | access: wgpu::StorageTextureAccess::WriteOnly, | ||
| 192 | format, | ||
| 193 | view_dimension: wgpu::TextureViewDimension::D2, | ||
| 194 | }, | ||
| 195 | count: None, | ||
| 196 | }, | ||
| 197 | wgpu::BindGroupLayoutEntry { | ||
| 198 | binding: 6, | ||
| 199 | visibility: wgpu::ShaderStages::COMPUTE, | ||
| 200 | ty: wgpu::BindingType::Buffer { | ||
| 201 | ty: wgpu::BufferBindingType::Uniform, | ||
| 202 | has_dynamic_offset: true, | ||
| 203 | min_binding_size: NonZeroU64::new(u_size), | ||
| 204 | }, | ||
| 205 | count: None, | ||
| 206 | }, | ||
| 207 | ], | ||
| 208 | }); | ||
| 209 | let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
| 210 | label: Some("fx-fuses-pl"), | ||
| 211 | bind_group_layouts: &[Some(&bgl)], | ||
| 212 | immediate_size: 0, | ||
| 213 | }); | ||
| 214 | let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { | ||
| 215 | label: Some("fx-fuses-wgsl"), | ||
| 216 | source: wgpu::ShaderSource::Wgsl(build_fx_wgsl(format, hdr_float).into()), | ||
| 217 | }); | ||
| 218 | let mk = |entry: &str| { | ||
| 219 | device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { | ||
| 220 | label: Some(entry), | ||
| 221 | layout: Some(&pl), | ||
| 222 | module: &module, | ||
| 223 | entry_point: Some(entry), | ||
| 224 | compilation_options: Default::default(), | ||
| 225 | cache: None, | ||
| 226 | }) | ||
| 227 | }; | ||
| 228 | let fp = FxFormatPipelines { | ||
| 229 | bgl, | ||
| 230 | vd_warp: mk("vd_warp_op"), | ||
| 231 | vd_accum_color: mk("vd_accum_color_op"), | ||
| 232 | vd_accum_weight: mk("vd_accum_weight_op"), | ||
| 233 | vd_finalize: mk("vd_finalize_op"), | ||
| 234 | mandel: mk("mandel_op"), | ||
| 235 | unsharp_mask: mk("unsharp_mask_op"), | ||
| 236 | letterbox: mk("letterbox_op"), | ||
| 237 | wipe: mk("wipe_op"), | ||
| 238 | linger_step: mk("linger_step_op"), | ||
| 239 | ocl_rays: mk("ocl_rays_op"), | ||
| 240 | }; | ||
| 241 | self.by_format.insert((format, hdr_float), fp); | ||
| 242 | } | ||
| 243 | |||
| 244 | #[allow(clippy::too_many_arguments)] | ||
| 245 | fn dispatch( | ||
| 246 | &mut self, | ||
| 247 | device: &wgpu::Device, | ||
| 248 | queue: &wgpu::Queue, | ||
| 249 | enc: &mut wgpu::CommandEncoder, | ||
| 250 | op: FxOp, | ||
| 251 | src_a: &wgpu::TextureView, | ||
| 252 | src_b: &wgpu::TextureView, | ||
| 253 | mask: &wgpu::TextureView, | ||
| 254 | aux: Option<&wgpu::TextureView>, | ||
| 255 | dst: &CachedTexture, | ||
| 256 | params: &UniformParams, | ||
| 257 | ) { | ||
| 258 | let hdr_float = dst.depth.is_float(); | ||
| 259 | self.ensure_format(device, dst.format, hdr_float); | ||
| 260 | let (chunk_idx, offset) = self.alloc_uniform_slot(device, queue, params); | ||
| 261 | let fp = &self.by_format[&(dst.format, hdr_float)]; | ||
| 262 | let aux_view = aux.unwrap_or(&self.dummy_aux.view); | ||
| 263 | let u_size = NonZeroU64::new(std::mem::size_of::<UniformParams>() as u64); | ||
| 264 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { | ||
| 265 | label: Some("fx-fuses-bg"), | ||
| 266 | layout: &fp.bgl, | ||
| 267 | entries: &[ | ||
| 268 | wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(src_a) }, | ||
| 269 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(src_b) }, | ||
| 270 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(mask) }, | ||
| 271 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(aux_view) }, | ||
| 272 | wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::Sampler(&self.sampler) }, | ||
| 273 | wgpu::BindGroupEntry { binding: 5, resource: wgpu::BindingResource::TextureView(&dst.view) }, | ||
| 274 | wgpu::BindGroupEntry { | ||
| 275 | binding: 6, | ||
| 276 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { | ||
| 277 | buffer: &self.uniform_chunks[chunk_idx], | ||
| 278 | offset: 0, | ||
| 279 | size: u_size, | ||
| 280 | }), | ||
| 281 | }, | ||
| 282 | ], | ||
| 283 | }); | ||
| 284 | let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { | ||
| 285 | label: Some("fx-fuses-pass"), | ||
| 286 | timestamp_writes: None, | ||
| 287 | }); | ||
| 288 | pass.set_pipeline(fp.pipeline(op)); | ||
| 289 | pass.set_bind_group(0, &bg, &[offset]); | ||
| 290 | pass.dispatch_workgroups(dst.size[0].div_ceil(8), dst.size[1].div_ceil(8), 1); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | pub(self) fn first_num(comp: &Comp, node: &Node, keys: &[&str], time: f64, default: f64, expr: &ExprEngine) -> f64 { | ||
| 295 | for &k in keys { | ||
| 296 | if node.inputs.contains_key(k) { | ||
| 297 | return comp.eval_number(node, k, time, default, expr); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | default | ||
| 301 | } | ||
| 302 | |||
| 303 | pub(self) fn first_point(comp: &Comp, node: &Node, keys: &[&str], time: f64, default: [f64; 2], expr: &ExprEngine) -> [f64; 2] { | ||
| 304 | for &k in keys { | ||
| 305 | if node.inputs.contains_key(k) { | ||
| 306 | return comp.eval_point(node, k, time, default, expr); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | default | ||
| 310 | } | ||
| 311 | |||
| 312 | pub(self) fn first_connected(comp: &Comp, node: &Node, ports: &[&str]) -> Option<NodeId> { | ||
| 313 | ports.iter().find_map(|p| node.connected_node(p).and_then(|s| comp.find_node(s))) | ||
| 314 | } | ||
| 315 | |||
| 316 | pub(self) fn first_str<'a>(node: &'a Node, keys: &[&str]) -> Option<&'a str> { | ||
| 317 | keys.iter().find_map(|k| node.static_str(k)) | ||
| 318 | } | ||
| 319 | |||
| 320 | impl Engine { | ||
| 321 | fn fx_fuses_pipelines(&self) -> &RefCell<FxFusesPipelines> { | ||
| 322 | self.fx_fuses_pipelines | ||
| 323 | .get_or_init(|| RefCell::new(FxFusesPipelines::new(&self.device, &self.queue))) | ||
| 324 | } | ||
| 325 | |||
| 326 | #[allow(clippy::too_many_arguments)] | ||
| 327 | pub(self) fn dispatch_fx( | ||
| 328 | &self, | ||
| 329 | enc: &mut wgpu::CommandEncoder, | ||
| 330 | op: FxOp, | ||
| 331 | src_a: &wgpu::TextureView, | ||
| 332 | src_b: &wgpu::TextureView, | ||
| 333 | mask: &wgpu::TextureView, | ||
| 334 | aux: Option<&wgpu::TextureView>, | ||
| 335 | dst: &CachedTexture, | ||
| 336 | params: &UniformParams, | ||
| 337 | ) { | ||
| 338 | self.fx_fuses_pipelines() | ||
| 339 | .borrow_mut() | ||
| 340 | .dispatch(&self.device, &self.queue, enc, op, src_a, src_b, mask, aux, dst, params); | ||
| 341 | } | ||
| 342 | |||
| 343 | // ------------------------------------------------------------------------ | ||
| 344 | // 1. VectorDenoise | ||
| 345 | // ------------------------------------------------------------------------ | ||
| 346 | #[allow(clippy::too_many_arguments)] | ||
| 347 | pub(super) fn eval_vector_denoise( | ||
| 348 | &mut self, | ||
| 349 | comp: &Comp, | ||
| 350 | node: &Node, | ||
| 351 | time: f64, | ||
| 352 | frame_i: i64, | ||
| 353 | enc: &mut wgpu::CommandEncoder, | ||
| 354 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 355 | stats: &mut EvalStats, | ||
| 356 | ) -> CachedTexture { | ||
| 357 | let Some(src_id) = first_connected(comp, node, &["Input", "Background", "Image"]) else { | ||
| 358 | return self.dummy.clone(); | ||
| 359 | }; | ||
| 360 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; | ||
| 361 | let mbm_flag = self.multiplies_by_mask(comp, node, time); | ||
| 362 | let skip = self.process_skip(comp, node, time); | ||
| 363 | let curr = self.eval_node(comp, src_id, time, enc, visiting, stats); | ||
| 364 | if blend <= 1e-6 && !mbm_flag { | ||
| 365 | return curr; | ||
| 366 | } | ||
| 367 | |||
| 368 | let radius = first_num(comp, node, &["TemporalRadius", "Radius", "Frames", "Average"], time, 1.0, &self.expr) | ||
| 369 | .round() | ||
| 370 | .clamp(0.0, 3.0) as i64; | ||
| 371 | if radius == 0 && !mbm_flag && skip == [0.0; 4] { | ||
| 372 | return curr; | ||
| 373 | } | ||
| 374 | let sigma = first_num(comp, node, &["Sigma", "Threshold", "ColorThreshold", "Tolerance"], time, 0.15, &self.expr) | ||
| 375 | .max(1e-3) as f32; | ||
| 376 | let vec_scale = first_num(comp, node, &["VectorScale", "Scale"], time, 1.0, &self.expr) as f32; | ||
| 377 | |||
| 378 | let mut neighbors: Vec<(i64, CachedTexture)> = Vec::with_capacity((radius * 2) as usize); | ||
| 379 | let mut all_same = true; | ||
| 380 | for dt in -radius..=radius { | ||
| 381 | if dt == 0 { | ||
| 382 | continue; | ||
| 383 | } | ||
| 384 | let nbr = self.eval_node(comp, src_id, time + dt as f64, enc, visiting, stats); | ||
| 385 | if nbr.content_hash != curr.content_hash || curr.content_hash == 0 { | ||
| 386 | all_same = false; | ||
| 387 | } | ||
| 388 | neighbors.push((dt, nbr)); | ||
| 389 | } | ||
| 390 | if all_same && !mbm_flag && skip == [0.0; 4] { | ||
| 391 | return curr; | ||
| 392 | } | ||
| 393 | |||
| 394 | let ext_vec = first_connected(comp, node, &["VectorInput", "Vectors"]) | ||
| 395 | .map(|vid| self.eval_node(comp, vid, time, enc, visiting, stats)); | ||
| 396 | let curr_c = self.crop_to_canvas(enc, curr.clone()); | ||
| 397 | let size = curr_c.size; | ||
| 398 | let dst_depth = self.node_depth(comp, node, time, Some(curr_c.depth)); | ||
| 399 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); | ||
| 400 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); | ||
| 401 | let mbm = mask.is_some() && mbm_flag; | ||
| 402 | |||
| 403 | let fwd_vec: Option<(Arc<wgpu::TextureView>, u64)> = if let Some(ext) = ext_vec.as_ref() { | ||
| 404 | if let Some(av) = ext.aux.as_ref().and_then(|a| a.vector.as_ref()) { | ||
| 405 | Some((av.view.clone(), ext.content_hash ^ 0xF001)) | ||
| 406 | } else { | ||
| 407 | Some((ext.view.clone(), ext.content_hash ^ 0xF002)) | ||
| 408 | } | ||
| 409 | } else { | ||
| 410 | curr.aux.as_ref().and_then(|a| a.vector.as_ref()).map(|av| (av.view.clone(), curr.content_hash ^ 0xF003)) | ||
| 411 | }; | ||
| 412 | let bwd_vec: Option<(Arc<wgpu::TextureView>, u64)> = curr | ||
| 413 | .aux | ||
| 414 | .as_ref() | ||
| 415 | .and_then(|a| a.back_vector.as_ref()) | ||
| 416 | .map(|av| (av.view.clone(), curr.content_hash ^ 0xB003)); | ||
| 417 | |||
| 418 | let mut hashes = Vec::with_capacity(neighbors.len() + 4); | ||
| 419 | hashes.push(curr_c.content_hash); | ||
| 420 | for (_, n) in &neighbors { | ||
| 421 | hashes.push(n.content_hash); | ||
| 422 | } | ||
| 423 | hashes.push(fwd_vec.as_ref().map_or(0, |v| v.1)); | ||
| 424 | hashes.push(bwd_vec.as_ref().map_or(0, |v| v.1)); | ||
| 425 | hashes.push(mask_hash); | ||
| 426 | let params_f32 = [ | ||
| 427 | radius as f32, | ||
| 428 | sigma, | ||
| 429 | vec_scale, | ||
| 430 | blend, | ||
| 431 | if mask_inv { 1.0 } else { 0.0 }, | ||
| 432 | if mbm { 1.0 } else { 0.0 }, | ||
| 433 | skip[0], | ||
| 434 | skip[1], | ||
| 435 | skip[2], | ||
| 436 | skip[3], | ||
| 437 | ]; | ||
| 438 | let key = self.make_key(node, "VectorDenoise", size, &hashes, &params_f32); | ||
| 439 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 440 | stats.cache_hits += 1; | ||
| 441 | return hit; | ||
| 442 | } | ||
| 443 | stats.cache_misses += 1; | ||
| 444 | stats.nodes_evaluated += 1; | ||
| 445 | |||
| 446 | let work_depth = BitDepth::Float16; | ||
| 447 | let search_rad = if self.hiq { 4.0_f32 } else { 2.0_f32 }; | ||
| 448 | let mut w_tex = self.alloc_tex(size, work_depth, 0); | ||
| 449 | let mut u_init = UniformParams::default(); | ||
| 450 | u_init.p[0] = [sigma, 1.0, 1.0, 0.0]; | ||
| 451 | self.dispatch_fx(enc, FxOp::VdAccumWeight, &curr_c.view, &curr_c.view, &self.dummy.view, None, &w_tex, &u_init); | ||
| 452 | |||
| 453 | let mut accum = curr_c.clone(); | ||
| 454 | for (dt, nbr) in &neighbors { | ||
| 455 | let nbr_c = self.crop_to_canvas(enc, nbr.clone()); | ||
| 456 | let warped = if nbr_c.content_hash != 0 && nbr_c.content_hash == curr_c.content_hash { | ||
| 457 | curr_c.clone() | ||
| 458 | } else { | ||
| 459 | let (vec_view, has_vec, scale_dt) = if *dt > 0 { | ||
| 460 | if let Some((v, _)) = &fwd_vec { | ||
| 461 | (Some(v.as_ref()), 1.0_f32, vec_scale * (*dt as f32)) | ||
| 462 | } else { | ||
| 463 | (None, 0.0_f32, 0.0) | ||
| 464 | } | ||
| 465 | } else if let Some((v, _)) = &bwd_vec { | ||
| 466 | (Some(v.as_ref()), 1.0_f32, vec_scale * ((-*dt) as f32)) | ||
| 467 | } else if let Some((v, _)) = &fwd_vec { | ||
| 468 | (Some(v.as_ref()), 1.0_f32, vec_scale * (*dt as f32)) | ||
| 469 | } else { | ||
| 470 | (None, 0.0_f32, 0.0) | ||
| 471 | }; | ||
| 472 | let dst_w = self.alloc_tex(size, work_depth, 0); | ||
| 473 | let mut uw = UniformParams::default(); | ||
| 474 | uw.p[0] = [has_vec, scale_dt, scale_dt, search_rad]; | ||
| 475 | self.dispatch_fx(enc, FxOp::VdWarp, &curr_c.view, &nbr_c.view, &self.dummy.view, vec_view, &dst_w, &uw); | ||
| 476 | dst_w | ||
| 477 | }; | ||
| 478 | |||
| 479 | let temporal_w = (-0.25 * (*dt * *dt) as f32).exp(); | ||
| 480 | let mut ua = UniformParams::default(); | ||
| 481 | ua.p[0] = [sigma, temporal_w, 0.0, 0.0]; | ||
| 482 | let next_acc = self.alloc_tex(size, work_depth, 0); | ||
| 483 | self.dispatch_fx(enc, FxOp::VdAccumColor, &accum.view, &warped.view, &curr_c.view, Some(&w_tex.view), &next_acc, &ua); | ||
| 484 | let next_w = self.alloc_tex(size, work_depth, 0); | ||
| 485 | self.dispatch_fx(enc, FxOp::VdAccumWeight, &curr_c.view, &warped.view, &self.dummy.view, Some(&w_tex.view), &next_w, &ua); | ||
| 486 | accum = next_acc; | ||
| 487 | w_tex = next_w; | ||
| 488 | } | ||
| 489 | |||
| 490 | let mut dst = self.alloc_tex(size, dst_depth, key); | ||
| 491 | let mut uf = UniformParams::default(); | ||
| 492 | uf.p[0] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }]; | ||
| 493 | uf.p[1] = skip; | ||
| 494 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | ||
| 495 | self.dispatch_fx(enc, FxOp::VdFinalize, &curr_c.view, &accum.view, mv, None, &dst, &uf); | ||
| 496 | if curr.aux.is_some() && curr.origin == [0, 0] && curr.size == dst.size { | ||
| 497 | dst.set_aux(curr.aux.clone()); | ||
| 498 | } | ||
| 499 | self.cache.insert(key, dst.clone(), node.id, frame_i); | ||
| 500 | dst | ||
| 501 | } | ||
| 502 | |||
| 503 | // ------------------------------------------------------------------------ | ||
| 504 | // 2. Mandel | ||
| 505 | // ------------------------------------------------------------------------ | ||
| 506 | #[allow(clippy::too_many_arguments)] | ||
| 507 | pub(super) fn eval_mandel( | ||
| 508 | &mut self, | ||
| 509 | comp: &Comp, | ||
| 510 | node: &Node, | ||
| 511 | time: f64, | ||
| 512 | frame_i: i64, | ||
| 513 | comp_dims: [u32; 2], | ||
| 514 | enc: &mut wgpu::CommandEncoder, | ||
| 515 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 516 | stats: &mut EvalStats, | ||
| 517 | ) -> CachedTexture { | ||
| 518 | let use_ff = comp.eval_number(node, "UseFrameFormatSettings", time, 0.0, &self.expr) > 0.5; | ||
| 519 | let w = if use_ff { comp.width } else { comp.eval_number(node, "Width", time, comp.width as f64, &self.expr).round().max(1.0) as u32 }; | ||
| 520 | let h = if use_ff { comp.height } else { comp.eval_number(node, "Height", time, comp.height as f64, &self.expr).round().max(1.0) as u32 }; | ||
| 521 | let size = if w == comp.width && h == comp.height { comp_dims } else { self.scaled_dims(w, h) }; | ||
| 522 | let depth = self.node_depth(comp, node, time, None); | ||
| 523 | |||
| 524 | let is_julia = match first_str(node, &["Mode", "Type", "FractalType"]) { | ||
| 525 | Some(s) => s.eq_ignore_ascii_case("julia"), | ||
| 526 | None => first_num(comp, node, &["Mode", "Type", "FractalType", "Julia"], time, 0.0, &self.expr) >= 0.5, | ||
| 527 | }; | ||
| 528 | let default_center = if is_julia { [0.0, 0.0] } else { [-0.5, 0.0] }; | ||
| 529 | let (cx, cy) = if node.inputs.contains_key("XPosition") || node.inputs.contains_key("YPosition") { | ||
| 530 | ( | ||
| 531 | comp.eval_number(node, "XPosition", time, default_center[0], &self.expr) as f32, | ||
| 532 | comp.eval_number(node, "YPosition", time, default_center[1], &self.expr) as f32, | ||
| 533 | ) | ||
| 534 | } else { | ||
| 535 | let c = first_point(comp, node, &["Center"], time, default_center, &self.expr); | ||
| 536 | (c[0] as f32, c[1] as f32) | ||
| 537 | }; | ||
| 538 | let zoom = if node.inputs.contains_key("Zoom") { | ||
| 539 | comp.eval_number(node, "Zoom", time, 1.0, &self.expr).max(1e-6) as f32 | ||
| 540 | } else if node.inputs.contains_key("Size") { | ||
| 541 | (1.0 / comp.eval_number(node, "Size", time, 1.0, &self.expr).max(1e-6)) as f32 | ||
| 542 | } else { | ||
| 543 | 1.0_f32 | ||
| 544 | }; | ||
| 545 | let rot_rad = (first_num(comp, node, &["Rotation", "Angle"], time, 0.0, &self.expr) as f32).to_radians(); | ||
| 546 | let max_iter = first_num(comp, node, &["Iterations", "MaxIterations"], time, 64.0, &self.expr).round().clamp(1.0, 1024.0) as f32; | ||
| 547 | let escape_lim = first_num(comp, node, &["EscapeLimit", "Bailout"], time, 4.0, &self.expr).max(4.0) as f32; | ||
| 548 | let escape_r2 = (escape_lim * escape_lim).max(16.0); | ||
| 549 | let grad_curve = first_num(comp, node, &["GradientCurve", "Curve"], time, 0.5, &self.expr).max(0.05) as f32; | ||
| 550 | |||
| 551 | let mut jc = first_point(comp, node, &["JuliaSeed", "C", "Seed"], time, [-0.7, 0.27015], &self.expr); | ||
| 552 | if node.inputs.contains_key("JuliaX") || node.inputs.contains_key("CReal") { | ||
| 553 | jc[0] = first_num(comp, node, &["JuliaX", "CReal"], time, jc[0], &self.expr); | ||
| 554 | } | ||
| 555 | if node.inputs.contains_key("JuliaY") || node.inputs.contains_key("CImag") { | ||
| 556 | jc[1] = first_num(comp, node, &["JuliaY", "CImag"], time, jc[1], &self.expr); | ||
| 557 | } | ||
| 558 | |||
| 559 | let any_rgb_wave = ["RedRepetitions", "GreenRepetitions", "BlueRepetitions", "RedPhase", "GreenPhase", "BluePhase"] | ||
| 560 | .iter() | ||
| 561 | .any(|k| node.inputs.contains_key(*k)); | ||
| 562 | let r_rep = comp.eval_number(node, "RedRepetitions", time, 1.0, &self.expr) as f32; | ||
| 563 | let g_rep = comp.eval_number(node, "GreenRepetitions", time, 1.0, &self.expr) as f32; | ||
| 564 | let b_rep = comp.eval_number(node, "BlueRepetitions", time, 1.0, &self.expr) as f32; | ||
| 565 | let r_ph = comp.eval_number(node, "RedPhase", time, 0.0, &self.expr) as f32; | ||
| 566 | let g_ph = comp.eval_number(node, "GreenPhase", time, if any_rgb_wave { 0.0 } else { 0.15 }, &self.expr) as f32; | ||
| 567 | let b_ph = comp.eval_number(node, "BluePhase", time, if any_rgb_wave { 0.0 } else { 0.30 }, &self.expr) as f32; | ||
| 568 | |||
| 569 | let tint = [ | ||
| 570 | first_num(comp, node, &["ColorRed", "Red"], time, 1.0, &self.expr) as f32, | ||
| 571 | first_num(comp, node, &["ColorGreen", "Green"], time, 1.0, &self.expr) as f32, | ||
| 572 | first_num(comp, node, &["ColorBlue", "Blue"], time, 1.0, &self.expr) as f32, | ||
| 573 | first_num(comp, node, &["ColorAlpha", "Alpha"], time, 1.0, &self.expr) as f32, | ||
| 574 | ]; | ||
| 575 | let inside = [ | ||
| 576 | comp.eval_number(node, "InsideRed", time, 0.0, &self.expr) as f32, | ||
| 577 | comp.eval_number(node, "InsideGreen", time, 0.0, &self.expr) as f32, | ||
| 578 | comp.eval_number(node, "InsideBlue", time, 0.0, &self.expr) as f32, | ||
| 579 | comp.eval_number(node, "InsideAlpha", time, 1.0, &self.expr) as f32, | ||
| 580 | ]; | ||
| 581 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; | ||
| 582 | let grad = comp.eval_gradient(node, "Gradient", time).or_else(|| comp.eval_gradient(node, "Palette", time)); | ||
| 583 | let lut_data = grad.map(|g| { | ||
| 584 | let mut bytes = [0u8; 256 * 4]; | ||
| 585 | for (i, texel) in bytes.chunks_exact_mut(4).enumerate() { | ||
| 586 | let c = g.sample(i as f64 / 255.0); | ||
| 587 | for (b, v) in texel.iter_mut().zip(c) { | ||
| 588 | *b = (v.clamp(0.0, 1.0) * 255.0).round() as u8; | ||
| 589 | } | ||
| 590 | } | ||
| 591 | let mut gh = DefaultHasher::new(); | ||
| 592 | bytes.hash(&mut gh); | ||
| 593 | (bytes, gh.finish()) | ||
| 594 | }); | ||
| 595 | let grad_hash = lut_data.as_ref().map_or(0, |l| l.1); | ||
| 596 | |||
| 597 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); | ||
| 598 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); | ||
| 599 | |||
| 600 | let mut u = UniformParams::default(); | ||
| 601 | u.p[0] = [cx, cy, zoom, rot_rad]; | ||
| 602 | u.p[1] = [if is_julia { 1.0 } else { 0.0 }, max_iter, escape_r2, grad_curve]; | ||
| 603 | u.p[2] = [jc[0] as f32, jc[1] as f32, r_rep, g_rep]; | ||
| 604 | u.p[3] = [b_rep, r_ph, g_ph, b_ph]; | ||
| 605 | u.p[4] = [if lut_data.is_some() { 1.0 } else { 0.0 }, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; | ||
| 606 | u.p[5] = tint; | ||
| 607 | u.p[6] = inside; | ||
| 608 | |||
| 609 | let key = self.make_key(node, "Mandel", size, &[mask_hash, grad_hash, depth as u64], bytemuck::cast_slice(&u.p[..7])); | ||
| 610 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 611 | stats.cache_hits += 1; | ||
| 612 | return hit; | ||
| 613 | } | ||
| 614 | stats.cache_misses += 1; | ||
| 615 | stats.nodes_evaluated += 1; | ||
| 616 | |||
| 617 | let lut_tex = lut_data.map(|(bytes, _)| { | ||
| 618 | let tex = create_texture(&self.device, [256, 1], 0); | ||
| 619 | self.queue.write_texture( | ||
| 620 | tex.texture.as_image_copy(), | ||
| 621 | &bytes, | ||
| 622 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(256 * 4), rows_per_image: Some(1) }, | ||
| 623 | wgpu::Extent3d { width: 256, height: 1, depth_or_array_layers: 1 }, | ||
| 624 | ); | ||
| 625 | tex | ||
| 626 | }); | ||
| 627 | let dst = self.alloc_tex(size, depth, key); | ||
| 628 | let lut_view = lut_tex.as_ref().map_or(&self.dummy.view, |t| &t.view); | ||
| 629 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | ||
| 630 | self.dispatch_fx(enc, FxOp::Mandel, &self.dummy.view, lut_view, mv, None, &dst, &u); | ||
| 631 | self.cache.insert(key, dst.clone(), node.id, frame_i); | ||
| 632 | dst | ||
| 633 | } | ||
| 634 | |||
| 635 | // ------------------------------------------------------------------------ | ||
| 636 | // 3. UnsharpMask | ||
| 637 | // ------------------------------------------------------------------------ | ||
| 638 | #[allow(clippy::too_many_arguments)] | ||
| 639 | pub(super) fn eval_unsharp_mask( | ||
| 640 | &mut self, | ||
| 641 | comp: &Comp, | ||
| 642 | node: &Node, | ||
| 643 | time: f64, | ||
| 644 | frame_i: i64, | ||
| 645 | enc: &mut wgpu::CommandEncoder, | ||
| 646 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 647 | stats: &mut EvalStats, | ||
| 648 | ) -> CachedTexture { | ||
| 649 | let Some(src_id) = first_connected(comp, node, &["Input", "Background"]) else { | ||
| 650 | return self.dummy.clone(); | ||
| 651 | }; | ||
| 652 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); | ||
| 653 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; | ||
| 654 | let lock_xy = first_num(comp, node, &["LockXY", "LockSizeXY"], time, 1.0, &self.expr) > 0.5; | ||
| 655 | let x_size = first_num(comp, node, &["XSize", "Size"], time, 2.0, &self.expr).max(0.0) as f32; | ||
| 656 | let y_size = if lock_xy { x_size } else { first_num(comp, node, &["YSize", "Size"], time, x_size as f64, &self.expr).max(0.0) as f32 }; | ||
| 657 | let gain = first_num(comp, node, &["Gain", "Amount"], time, 1.0, &self.expr) as f32; | ||
| 658 | let threshold = first_num(comp, node, &["Threshold"], time, 0.0, &self.expr).max(0.0) as f32; | ||
| 659 | let mbm_flag = self.multiplies_by_mask(comp, node, time); | ||
| 660 | let skip = self.process_skip(comp, node, time); | ||
| 661 | |||
| 662 | if (blend <= 1e-6 || gain.abs() <= 1e-6 || x_size.max(y_size) <= 1e-5) && !mbm_flag && skip == [0.0; 4] { | ||
| 663 | return src; | ||
| 664 | } | ||
| 665 | let has_mask = Self::resolve_effect_mask_id(comp, node).is_some(); | ||
| 666 | let src = if has_mask { self.crop_to_canvas(enc, src) } else { src }; | ||
| 667 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); | ||
| 668 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); | ||
| 669 | let mbm = mask.is_some() && mbm_flag; | ||
| 670 | |||
| 671 | let scale_ref = (src.canvas_size[0] as f32 / 720.0).max(0.5); | ||
| 672 | let rx = x_size * scale_ref; | ||
| 673 | let ry = y_size * scale_ref; | ||
| 674 | |||
| 675 | let mut u = UniformParams::default(); | ||
| 676 | u.p[0] = [gain, threshold, blend, if mask.is_some() { 1.0 } else { 0.0 }]; | ||
| 677 | u.p[1] = [if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }, rx, ry]; | ||
| 678 | u.p[2] = skip; | ||
| 679 | |||
| 680 | let key = self.make_key(node, "UnsharpMask", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..3])); | ||
| 681 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 682 | stats.cache_hits += 1; | ||
| 683 | return hit; | ||
| 684 | } | ||
| 685 | stats.cache_misses += 1; | ||
| 686 | stats.nodes_evaluated += 1; | ||
| 687 | |||
| 688 | let blurred = self.blur_separable(enc, &src, rx, ry, 0.0, 1.0); | ||
| 689 | let dst = self.alloc_tex_like(&src, src.depth, key); | ||
| 690 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | ||
| 691 | self.dispatch_fx(enc, FxOp::UnsharpMask, &src.view, &blurred.view, mv, None, &dst, &u); | ||
| 692 | self.cache.insert(key, dst.clone(), node.id, frame_i); | ||
| 693 | dst | ||
| 694 | } | ||
| 695 | |||
| 696 | // ------------------------------------------------------------------------ | ||
| 697 | // 4. Letterbox | ||
| 698 | // ------------------------------------------------------------------------ | ||
| 699 | #[allow(clippy::too_many_arguments)] | ||
| 700 | pub(super) fn eval_letterbox( | ||
| 701 | &mut self, | ||
| 702 | comp: &Comp, | ||
| 703 | node: &Node, | ||
| 704 | time: f64, | ||
| 705 | frame_i: i64, | ||
| 706 | comp_dims: [u32; 2], | ||
| 707 | enc: &mut wgpu::CommandEncoder, | ||
| 708 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 709 | stats: &mut EvalStats, | ||
| 710 | ) -> CachedTexture { | ||
| 711 | let Some(src_id) = first_connected(comp, node, &["Input", "Background"]) else { | ||
| 712 | return self.dummy.clone(); | ||
| 713 | }; | ||
| 714 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); | ||
| 715 | let hiq_only = comp.eval_number(node, "HiQOnly", time, 0.0, &self.expr) > 0.5; | ||
| 716 | if !self.hiq && hiq_only { | ||
| 717 | return src; | ||
| 718 | } | ||
| 719 | let src = self.crop_to_canvas(enc, src); | ||
| 720 | let max_dim = self.device.limits().max_texture_dimension_2d as f64; | ||
| 721 | let use_ff = comp.eval_number(node, "UseFrameFormatSettings", time, 0.0, &self.expr) > 0.5; | ||
| 722 | let dst_size = if use_ff { | ||
| 723 | comp_dims | ||
| 724 | } else { | ||
| 725 | let w = comp.eval_number(node, "Width", time, comp.width as f64, &self.expr).clamp(1.0, max_dim).round() as u32; | ||
| 726 | let h = comp.eval_number(node, "Height", time, comp.height as f64, &self.expr).clamp(1.0, max_dim).round() as u32; | ||
| 727 | if w == comp.width && h == comp.height { comp_dims } else { self.scaled_dims(w, h) } | ||
| 728 | }; | ||
| 729 | |||
| 730 | let mode = match node.static_str("Mode") { | ||
| 731 | Some(s) if s.to_ascii_lowercase().contains("pan") => 1.0_f32, | ||
| 732 | Some(_) => 0.0_f32, | ||
| 733 | None => if comp.eval_number(node, "Mode", time, 0.0, &self.expr) >= 0.5 { 1.0 } else { 0.0 }, | ||
| 734 | }; | ||
| 735 | let position = first_num(comp, node, &["Position", "CenterOffset"], time, 0.0, &self.expr).clamp(-1.0, 1.0) as f32; | ||
| 736 | let filter = match node.static_str("FilterMethod") { | ||
| 737 | Some("Nearest" | "Nearest Neighbor") => 0.0_f32, | ||
| 738 | Some("Box" | "Linear" | "Bilinear") => 1.0_f32, | ||
| 739 | Some("Cubic" | "Catmull-Rom" | "CatmullRom" | "Lanczos" | "Mitchell" | "Quadratic") => 3.0_f32, | ||
| 740 | Some(_) => 2.0_f32, | ||
| 741 | None => comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr) as f32, | ||
| 742 | }; | ||
| 743 | let pa_pt = comp.eval_point(node, "PixelAspect", time, [1.0, 1.0], &self.expr); | ||
| 744 | let pa = (pa_pt[0] / pa_pt[1].max(1e-4)).clamp(0.1, 10.0) as f32; | ||
| 745 | |||
| 746 | let mut u = UniformParams::default(); | ||
| 747 | u.p[0] = [mode, position, filter, pa]; | ||
| 748 | let key = self.make_key(node, "Letterbox", dst_size, &[src.content_hash], bytemuck::cast_slice(&u.p[..1])); | ||
| 749 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 750 | stats.cache_hits += 1; | ||
| 751 | return hit; | ||
| 752 | } | ||
| 753 | stats.cache_misses += 1; | ||
| 754 | stats.nodes_evaluated += 1; | ||
| 755 | let dst = self.alloc_tex(dst_size, src.depth, key); | ||
| 756 | self.dispatch_fx(enc, FxOp::Letterbox, &src.view, &self.dummy.view, &self.dummy.view, None, &dst, &u); | ||
| 757 | self.cache.insert(key, dst.clone(), node.id, frame_i); | ||
| 758 | dst | ||
| 759 | } | ||
| 760 | } | ||
crates/engine/src/eval/fx_fuses/fuses.rs created+321| ... | @@ -0,0 +1,321 @@ | ||
| 1 | //! Archive Fuse evaluators: `Fuse.Wipe`, `Fuse.Null`, `Fuse.SetMetaData`, | ||
| 2 | //! `Fuse.Linger`, and `Fuse.OCLRays`. | ||
| 3 | |||
| 4 | use super::{ | ||
| 5 | Comp, CachedTexture, Engine, EvalStats, FxOp, Node, NodeId, UniformParams, first_connected, | ||
| 6 | first_num, first_point, first_str, | ||
| 7 | }; | ||
| 8 | use model::BitDepth; | ||
| 9 | use std::collections::HashSet; | ||
| 10 | |||
| 11 | impl Engine { | ||
| 12 | // ------------------------------------------------------------------------ | ||
| 13 | // 5. Fuse.Wipe | ||
| 14 | // ------------------------------------------------------------------------ | ||
| 15 | #[allow(clippy::too_many_arguments)] | ||
| 16 | pub(in crate::eval) fn eval_fuse_wipe( | ||
| 17 | &mut self, | ||
| 18 | comp: &Comp, | ||
| 19 | node: &Node, | ||
| 20 | time: f64, | ||
| 21 | frame_i: i64, | ||
| 22 | enc: &mut wgpu::CommandEncoder, | ||
| 23 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 24 | stats: &mut EvalStats, | ||
| 25 | ) -> CachedTexture { | ||
| 26 | let bg_id = first_connected(comp, node, &["Input1", "Background", "Input"]); | ||
| 27 | let fg_id = first_connected(comp, node, &["Input2", "Foreground"]); | ||
| 28 | let Some(bg_id) = bg_id else { | ||
| 29 | return match fg_id { | ||
| 30 | Some(id) => self.eval_node(comp, id, time, enc, visiting, stats), | ||
| 31 | None => self.dummy.clone(), | ||
| 32 | }; | ||
| 33 | }; | ||
| 34 | let bg = self.eval_node(comp, bg_id, time, enc, visiting, stats); | ||
| 35 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; | ||
| 36 | let mbm_flag = self.multiplies_by_mask(comp, node, time); | ||
| 37 | if blend <= 1e-6 && !mbm_flag { | ||
| 38 | return bg; | ||
| 39 | } | ||
| 40 | let bg_c = self.crop_to_canvas(enc, bg); | ||
| 41 | let fg = fg_id.map(|id| { | ||
| 42 | let t = self.eval_node(comp, id, time, enc, visiting, stats); | ||
| 43 | self.crop_to_canvas(enc, t) | ||
| 44 | }); | ||
| 45 | |||
| 46 | let progress = first_num(comp, node, &["Amount", "Wipe", "Progress", "Mix"], time, 0.5, &self.expr) | ||
| 47 | .clamp(0.0, 1.0) as f32; | ||
| 48 | let angle_rad = (first_num(comp, node, &["Angle", "Rotation"], time, 0.0, &self.expr) as f32).to_radians(); | ||
| 49 | let softness = first_num(comp, node, &["Softness", "Border", "SoftEdge", "Feather"], time, 0.0, &self.expr) | ||
| 50 | .max(0.0) as f32; | ||
| 51 | let wipe_type = match first_str(node, &["WipeType", "Type", "Shape", "Mode"]) { | ||
| 52 | Some(s) if s.eq_ignore_ascii_case("radial") || s.eq_ignore_ascii_case("clock") || s.eq_ignore_ascii_case("angular") => 1.0_f32, | ||
| 53 | Some(s) if s.eq_ignore_ascii_case("iris") || s.eq_ignore_ascii_case("circle") || s.eq_ignore_ascii_case("circular") => 2.0_f32, | ||
| 54 | Some(s) if s.eq_ignore_ascii_case("box") || s.eq_ignore_ascii_case("rectangle") || s.eq_ignore_ascii_case("square") => 3.0_f32, | ||
| 55 | Some(_) => 0.0_f32, | ||
| 56 | None => first_num(comp, node, &["WipeType", "Type", "Shape", "Mode"], time, 0.0, &self.expr) | ||
| 57 | .round() | ||
| 58 | .clamp(0.0, 3.0) as f32, | ||
| 59 | }; | ||
| 60 | let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr); | ||
| 61 | let invert = first_num(comp, node, &["Invert", "Flip"], time, 0.0, &self.expr) > 0.5; | ||
| 62 | let skip = self.process_skip(comp, node, time); | ||
| 63 | |||
| 64 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(bg_c.size), enc, visiting, stats); | ||
| 65 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); | ||
| 66 | let fg_hash = fg.as_ref().map_or(0, |f| f.content_hash); | ||
| 67 | let mbm = mask.is_some() && mbm_flag; | ||
| 68 | let dst_depth = self.node_depth(comp, node, time, Some(fg.as_ref().map_or(bg_c.depth, |f| bg_c.depth.max(f.depth)))); | ||
| 69 | |||
| 70 | let mut u = UniformParams::default(); | ||
| 71 | u.p[0] = [progress, angle_rad, softness, wipe_type]; | ||
| 72 | u.p[1] = [center[0] as f32, center[1] as f32, if invert { 1.0 } else { 0.0 }, if fg.is_some() { 1.0 } else { 0.0 }]; | ||
| 73 | u.p[2] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }]; | ||
| 74 | u.p[3] = skip; | ||
| 75 | |||
| 76 | let key = self.make_key(node, "FuseWipe", bg_c.size, &[bg_c.content_hash, fg_hash, mask_hash], bytemuck::cast_slice(&u.p[..4])); | ||
| 77 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 78 | stats.cache_hits += 1; | ||
| 79 | return hit; | ||
| 80 | } | ||
| 81 | stats.cache_misses += 1; | ||
| 82 | stats.nodes_evaluated += 1; | ||
| 83 | let dst = self.alloc_tex(bg_c.size, dst_depth, key); | ||
| 84 | let fg_view = fg.as_ref().map_or(&self.dummy.view, |f| &f.view); | ||
| 85 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | ||
| 86 | self.dispatch_fx(enc, FxOp::Wipe, &bg_c.view, fg_view, mv, None, &dst, &u); | ||
| 87 | self.cache.insert(key, dst.clone(), node.id, frame_i); | ||
| 88 | dst | ||
| 89 | } | ||
| 90 | |||
| 91 | // ------------------------------------------------------------------------ | ||
| 92 | // 6. Fuse.Null & 7. Fuse.SetMetaData (lossless DoD + aux passthroughs) | ||
| 93 | // ------------------------------------------------------------------------ | ||
| 94 | pub(in crate::eval) fn eval_fuse_null( | ||
| 95 | &mut self, | ||
| 96 | comp: &Comp, | ||
| 97 | node: &Node, | ||
| 98 | time: f64, | ||
| 99 | enc: &mut wgpu::CommandEncoder, | ||
| 100 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 101 | stats: &mut EvalStats, | ||
| 102 | ) -> CachedTexture { | ||
| 103 | match first_connected(comp, node, &["Input", "Background", "Image", "Source", "Input1"]) { | ||
| 104 | Some(id) => self.eval_node(comp, id, time, enc, visiting, stats), | ||
| 105 | None => self.dummy.clone(), | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | pub(in crate::eval) fn eval_fuse_set_metadata( | ||
| 110 | &mut self, | ||
| 111 | comp: &Comp, | ||
| 112 | node: &Node, | ||
| 113 | time: f64, | ||
| 114 | enc: &mut wgpu::CommandEncoder, | ||
| 115 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 116 | stats: &mut EvalStats, | ||
| 117 | ) -> CachedTexture { | ||
| 118 | match first_connected(comp, node, &["Input", "Image", "Background", "Source"]) { | ||
| 119 | Some(id) => self.eval_node(comp, id, time, enc, visiting, stats), | ||
| 120 | None => self.dummy.clone(), | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | // ------------------------------------------------------------------------ | ||
| 125 | // 8. Fuse.Linger (bounded-lookback temporal persistence) | ||
| 126 | // ------------------------------------------------------------------------ | ||
| 127 | #[allow(clippy::too_many_arguments)] | ||
| 128 | pub(in crate::eval) fn eval_fuse_linger( | ||
| 129 | &mut self, | ||
| 130 | comp: &Comp, | ||
| 131 | node: &Node, | ||
| 132 | time: f64, | ||
| 133 | frame_i: i64, | ||
| 134 | enc: &mut wgpu::CommandEncoder, | ||
| 135 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 136 | stats: &mut EvalStats, | ||
| 137 | ) -> CachedTexture { | ||
| 138 | let Some(src_id) = first_connected(comp, node, &["Background", "Input", "Image"]) else { | ||
| 139 | return self.dummy.clone(); | ||
| 140 | }; | ||
| 141 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; | ||
| 142 | let mbm_flag = self.multiplies_by_mask(comp, node, time); | ||
| 143 | if blend <= 1e-6 && !mbm_flag { | ||
| 144 | return self.eval_node(comp, src_id, time, enc, visiting, stats); | ||
| 145 | } | ||
| 146 | |||
| 147 | let frames_n = first_num(comp, node, &["Frames", "MaxFrames", "Lookback"], time, 5.0, &self.expr) | ||
| 148 | .round() | ||
| 149 | .clamp(0.0, 64.0) as i64; | ||
| 150 | let decay = first_num(comp, node, &["Linger", "Decay", "Persistence", "Gain"], time, 0.7, &self.expr) | ||
| 151 | .clamp(0.0, 1.0) as f32; | ||
| 152 | let mode = match first_str(node, &["Mode", "ApplyMode"]) { | ||
| 153 | Some(s) if s.eq_ignore_ascii_case("lighten") || s.eq_ignore_ascii_case("max") => 1_i32, | ||
| 154 | Some(s) if s.eq_ignore_ascii_case("over") || s.eq_ignore_ascii_case("normal") => 2_i32, | ||
| 155 | Some(s) if s.eq_ignore_ascii_case("add") || s.eq_ignore_ascii_case("additive") => 3_i32, | ||
| 156 | Some(_) => 0_i32, | ||
| 157 | None => first_num(comp, node, &["Mode", "ApplyMode"], time, 0.0, &self.expr) | ||
| 158 | .round() | ||
| 159 | .clamp(0.0, 3.0) as i32, | ||
| 160 | }; | ||
| 161 | |||
| 162 | let cur_frame = time.round() as i64; | ||
| 163 | let subframe = time - cur_frame as f64; | ||
| 164 | let range_start = comp.global_range[0].min(cur_frame); | ||
| 165 | let lookback = if decay <= 1e-6 { 0 } else { frames_n }; | ||
| 166 | let window_start = range_start.max(cur_frame - lookback).min(cur_frame); | ||
| 167 | |||
| 168 | let mut frames = Vec::with_capacity((cur_frame - window_start + 1) as usize); | ||
| 169 | let mut hashes = Vec::with_capacity(frames.capacity() + 1); | ||
| 170 | for f in window_start..=cur_frame { | ||
| 171 | let tex = self.eval_node(comp, src_id, f as f64 + subframe, enc, visiting, stats); | ||
| 172 | hashes.push(tex.content_hash); | ||
| 173 | frames.push(tex); | ||
| 174 | } | ||
| 175 | let cur_src = frames.last().unwrap().clone(); | ||
| 176 | let cur_c = self.crop_to_canvas(enc, cur_src.clone()); | ||
| 177 | let size = cur_c.size; | ||
| 178 | let dst_depth = self.node_depth(comp, node, time, Some(cur_c.depth)); | ||
| 179 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); | ||
| 180 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); | ||
| 181 | hashes.push(mask_hash); | ||
| 182 | let mbm = mask.is_some() && mbm_flag; | ||
| 183 | let skip = self.process_skip(comp, node, time); | ||
| 184 | let needs_blend = blend < 1.0 - 1e-6 || mask.is_some() || skip.iter().any(|&s| s > 0.5); | ||
| 185 | |||
| 186 | if frames.len() == 1 && !needs_blend { | ||
| 187 | return cur_src; | ||
| 188 | } | ||
| 189 | let params_f32 = [ | ||
| 190 | decay, | ||
| 191 | mode as f32, | ||
| 192 | blend, | ||
| 193 | if mask_inv { 1.0 } else { 0.0 }, | ||
| 194 | if mbm { 1.0 } else { 0.0 }, | ||
| 195 | skip[0], | ||
| 196 | skip[1], | ||
| 197 | skip[2], | ||
| 198 | skip[3], | ||
| 199 | ]; | ||
| 200 | let key = self.make_key(node, "FuseLinger", size, &hashes, &params_f32); | ||
| 201 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 202 | stats.cache_hits += 1; | ||
| 203 | return hit; | ||
| 204 | } | ||
| 205 | stats.cache_misses += 1; | ||
| 206 | stats.nodes_evaluated += 1; | ||
| 207 | |||
| 208 | let work_depth = if dst_depth.is_float() { dst_depth } else { BitDepth::Float16 }; | ||
| 209 | let mut accum = if mode == 2 { | ||
| 210 | let mut acc = self.crop_to_canvas(enc, frames[0].clone()); | ||
| 211 | for (i, frm) in frames.iter().enumerate().skip(1) { | ||
| 212 | let frm_c = self.crop_to_canvas(enc, frm.clone()); | ||
| 213 | let is_last = i + 1 == frames.len() && !needs_blend; | ||
| 214 | let step_tex = self.alloc_tex(size, if is_last { dst_depth } else { work_depth }, if is_last { key } else { 0 }); | ||
| 215 | let mut u = UniformParams::default(); | ||
| 216 | u.p[0] = [2.0, decay, decay, 0.0]; | ||
| 217 | self.dispatch_fx(enc, FxOp::LingerStep, &acc.view, &frm_c.view, &self.dummy.view, None, &step_tex, &u); | ||
| 218 | acc = step_tex; | ||
| 219 | } | ||
| 220 | acc | ||
| 221 | } else { | ||
| 222 | let mut acc = cur_c.clone(); | ||
| 223 | let mut w_sum = 1.0_f32; | ||
| 224 | let n_past = frames.len() - 1; | ||
| 225 | for k in 1..=n_past { | ||
| 226 | let frm_c = self.crop_to_canvas(enc, frames[frames.len() - 1 - k].clone()); | ||
| 227 | let w = decay.powi(k as i32); | ||
| 228 | let factor = if mode == 0 { w / (w_sum + w) } else { w }; | ||
| 229 | w_sum += w; | ||
| 230 | let is_last = k == n_past && !needs_blend; | ||
| 231 | let step_tex = self.alloc_tex(size, if is_last { dst_depth } else { work_depth }, if is_last { key } else { 0 }); | ||
| 232 | let mut u = UniformParams::default(); | ||
| 233 | u.p[0] = [mode as f32, factor, decay, 0.0]; | ||
| 234 | self.dispatch_fx(enc, FxOp::LingerStep, &acc.view, &frm_c.view, &self.dummy.view, None, &step_tex, &u); | ||
| 235 | acc = step_tex; | ||
| 236 | } | ||
| 237 | acc | ||
| 238 | }; | ||
| 239 | |||
| 240 | if needs_blend { | ||
| 241 | let out_tex = self.alloc_tex(size, dst_depth, key); | ||
| 242 | let mut uf = UniformParams::default(); | ||
| 243 | uf.p[0] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }]; | ||
| 244 | uf.p[1] = skip; | ||
| 245 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | ||
| 246 | self.dispatch_fx(enc, FxOp::VdFinalize, &cur_c.view, &accum.view, mv, None, &out_tex, &uf); | ||
| 247 | accum = out_tex; | ||
| 248 | } | ||
| 249 | self.cache.insert(key, accum.clone(), node.id, frame_i); | ||
| 250 | accum | ||
| 251 | } | ||
| 252 | |||
| 253 | // ------------------------------------------------------------------------ | ||
| 254 | // 9. Fuse.OCLRays (volumetric radial god-rays / light shafts) | ||
| 255 | // ------------------------------------------------------------------------ | ||
| 256 | #[allow(clippy::too_many_arguments)] | ||
| 257 | pub(in crate::eval) fn eval_fuse_ocl_rays( | ||
| 258 | &mut self, | ||
| 259 | comp: &Comp, | ||
| 260 | node: &Node, | ||
| 261 | time: f64, | ||
| 262 | frame_i: i64, | ||
| 263 | enc: &mut wgpu::CommandEncoder, | ||
| 264 | visiting: &mut HashSet<(NodeId, i64)>, | ||
| 265 | stats: &mut EvalStats, | ||
| 266 | ) -> CachedTexture { | ||
| 267 | let Some(src_id) = first_connected(comp, node, &["Image", "Input", "Background"]) else { | ||
| 268 | return self.dummy.clone(); | ||
| 269 | }; | ||
| 270 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); | ||
| 271 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; | ||
| 272 | let mbm_flag = self.multiplies_by_mask(comp, node, time); | ||
| 273 | if blend <= 1e-6 && !mbm_flag { | ||
| 274 | return src; | ||
| 275 | } | ||
| 276 | let src_c = self.crop_to_canvas(enc, src); | ||
| 277 | let center = first_point(comp, node, &["Center", "LightPosition", "Position"], time, [0.5, 0.5], &self.expr); | ||
| 278 | let threshold = first_num(comp, node, &["Threshold"], time, 0.0, &self.expr).max(0.0) as f32; | ||
| 279 | let decay = first_num(comp, node, &["Decay"], time, 0.02, &self.expr).clamp(0.0, 0.9999) as f32; | ||
| 280 | let weight = first_num(comp, node, &["Weight"], time, 1.0, &self.expr).max(0.0) as f32; | ||
| 281 | let exposure = first_num(comp, node, &["Exposure", "Gain"], time, 1.0, &self.expr).max(0.0) as f32; | ||
| 282 | let default_samples = if self.hiq { 64.0 } else { 32.0 }; | ||
| 283 | let samples = first_num(comp, node, &["Samples", "Quality", "Steps"], time, default_samples, &self.expr) | ||
| 284 | .round() | ||
| 285 | .clamp(1.0, 128.0) as f32; | ||
| 286 | let density = first_num(comp, node, &["Density", "Length"], time, 1.0, &self.expr).clamp(0.0, 2.0) as f32; | ||
| 287 | let rays_only = first_num(comp, node, &["RaysOnly", "OutputRaysOnly"], time, 0.0, &self.expr) > 0.5; | ||
| 288 | let tint = [ | ||
| 289 | first_num(comp, node, &["ColorRed", "RaysRed", "Red"], time, 1.0, &self.expr) as f32, | ||
| 290 | first_num(comp, node, &["ColorGreen", "RaysGreen", "Green"], time, 1.0, &self.expr) as f32, | ||
| 291 | first_num(comp, node, &["ColorBlue", "RaysBlue", "Blue"], time, 1.0, &self.expr) as f32, | ||
| 292 | first_num(comp, node, &["ColorAlpha", "RaysAlpha", "Alpha"], time, 1.0, &self.expr) as f32, | ||
| 293 | ]; | ||
| 294 | let skip = self.process_skip(comp, node, time); | ||
| 295 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src_c.size), enc, visiting, stats); | ||
| 296 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); | ||
| 297 | let mbm = mask.is_some() && mbm_flag; | ||
| 298 | let dst_depth = self.node_depth(comp, node, time, Some(src_c.depth)); | ||
| 299 | |||
| 300 | let mut u = UniformParams::default(); | ||
| 301 | u.p[0] = [center[0] as f32, center[1] as f32, threshold, decay]; | ||
| 302 | u.p[1] = [weight, exposure, samples, density]; | ||
| 303 | u.p[2] = tint; | ||
| 304 | u.p[3] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }]; | ||
| 305 | u.p[4] = [if rays_only { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0]; | ||
| 306 | u.p[5] = skip; | ||
| 307 | |||
| 308 | let key = self.make_key(node, "FuseOCLRays", src_c.size, &[src_c.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..6])); | ||
| 309 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { | ||
| 310 | stats.cache_hits += 1; | ||
| 311 | return hit; | ||
| 312 | } | ||
| 313 | stats.cache_misses += 1; | ||
| 314 | stats.nodes_evaluated += 1; | ||
| 315 | let dst = self.alloc_tex(src_c.size, dst_depth, key); | ||
| 316 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | ||
| 317 | self.dispatch_fx(enc, FxOp::OclRays, &src_c.view, &self.dummy.view, mv, None, &dst, &u); | ||
| 318 | self.cache.insert(key, dst.clone(), node.id, frame_i); | ||
| 319 | dst | ||
| 320 | } | ||
| 321 | } | ||
crates/engine/src/eval/fx_fuses/tests.rs created+324| ... | @@ -0,0 +1,324 @@ | ||
| 1 | //! GPU unit tests for all 9 Wave 36 tools (`VectorDenoise`, `Mandel`, | ||
| 2 | //! `UnsharpMask`, `Letterbox`, `Fuse.Wipe`, `Fuse.Null`, `Fuse.SetMetaData`, | ||
| 3 | //! `Fuse.Linger`, `Fuse.OCLRays`). | ||
| 4 | |||
| 5 | use super::*; | ||
| 6 | |||
| 7 | fn comp(tools: &str) -> Comp { | ||
| 8 | let src = format!( | ||
| 9 | "Composition {{ CurrentTime = 0, RenderRange = {{ 0, 10 }}, GlobalRange = {{ 0, 10 }}, Tools = ordered() {{ {tools} }} }}" | ||
| 10 | ); | ||
| 11 | Comp::from_fusion_str(&src).expect("parse comp") | ||
| 12 | } | ||
| 13 | |||
| 14 | fn render(c: &Comp, name: &str, time: f64) -> Option<(CachedTexture, Vec<u8>)> { | ||
| 15 | let mut eng = Engine::new_headless().ok()?; | ||
| 16 | let id = c.find_node(name).expect("find node"); | ||
| 17 | let (tex, stats) = eng.evaluate(c, id, time); | ||
| 18 | assert!(stats.unsupported_nodes.is_empty(), "unexpected unsupported: {:?}", stats.unsupported_nodes); | ||
| 19 | let px = eng.readback_rgba(&tex).unwrap(); | ||
| 20 | Some((tex, px)) | ||
| 21 | } | ||
| 22 | |||
| 23 | fn at(px: &[u8], w: u32, x: u32, y: u32) -> [u8; 4] { | ||
| 24 | let i = ((y * w + x) * 4) as usize; | ||
| 25 | [px[i], px[i + 1], px[i + 2], px[i + 3]] | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn vector_denoise_reduces_temporal_flicker_and_preserves_edges() { | ||
| 30 | let c = comp(r#" | ||
| 31 | BG = Background { Inputs = { | ||
| 32 | Width = Input { Value = 32, }, Height = Input { Value = 32, }, | ||
| 33 | TopLeftRed = Input { SourceOp = "FlickerR", Source = "Value", }, | ||
| 34 | TopLeftGreen = Input { Value = 0.5, }, TopLeftBlue = Input { Value = 0.5, }, | ||
| 35 | }, }, | ||
| 36 | FlickerR = BezierSpline { KeyFrames = { | ||
| 37 | [0] = { 0.45, Flags = { Linear = true } }, | ||
| 38 | [1] = { 0.55, Flags = { Linear = true } }, | ||
| 39 | [2] = { 0.45, Flags = { Linear = true } }, | ||
| 40 | } }, | ||
| 41 | VD = VectorDenoise { Inputs = { | ||
| 42 | Input = Input { SourceOp = "BG", Source = "Output", }, | ||
| 43 | TemporalRadius = Input { Value = 1, }, | ||
| 44 | Sigma = Input { Value = 0.25, }, | ||
| 45 | }, }, | ||
| 46 | OcclBG = Background { Inputs = { | ||
| 47 | Width = Input { Value = 32, }, Height = Input { Value = 32, }, | ||
| 48 | TopLeftRed = Input { SourceOp = "JumpR", Source = "Value", }, | ||
| 49 | }, }, | ||
| 50 | JumpR = BezierSpline { KeyFrames = { | ||
| 51 | [0] = { 0.0, Flags = { Linear = true } }, | ||
| 52 | [1] = { 1.0, Flags = { Linear = true } }, | ||
| 53 | [2] = { 0.0, Flags = { Linear = true } }, | ||
| 54 | } }, | ||
| 55 | VDOccl = VectorDenoise { Inputs = { | ||
| 56 | Input = Input { SourceOp = "OcclBG", Source = "Output", }, | ||
| 57 | Average = Input { Value = 1, }, | ||
| 58 | Sigma = Input { Value = 0.05, }, | ||
| 59 | }, }, | ||
| 60 | "#); | ||
| 61 | let Some((_, px_smooth)) = render(&c, "VD", 1.0) else { return }; | ||
| 62 | let r_smooth = at(&px_smooth, 32, 16, 16)[0]; | ||
| 63 | assert!(r_smooth >= 118 && r_smooth <= 134, "expected temporal smoothing near ~124, got {r_smooth}"); | ||
| 64 | |||
| 65 | let (_, px_occl) = render(&c, "VDOccl", 1.0).unwrap(); | ||
| 66 | let r_occl = at(&px_occl, 32, 16, 16)[0]; | ||
| 67 | assert!(r_occl >= 250, "expected occlusion preservation near 255, got {r_occl}"); | ||
| 68 | } | ||
| 69 | |||
| 70 | #[test] | ||
| 71 | fn mandel_renders_mandelbrot_julia_and_gradient_lut() { | ||
| 72 | let c = comp(r#" | ||
| 73 | M = Mandel { Inputs = { | ||
| 74 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, | ||
| 75 | XPosition = Input { Value = -0.5, }, Zoom = Input { Value = 1.0, }, | ||
| 76 | Iterations = Input { Value = 80, }, EscapeLimit = Input { Value = 4, }, | ||
| 77 | GradientCurve = Input { Value = 0.45, }, | ||
| 78 | RedRepetitions = Input { Value = 1.8, }, GreenRepetitions = Input { Value = 2.0, }, | ||
| 79 | BluePhase = Input { Value = 0.25, }, | ||
| 80 | }, }, | ||
| 81 | J = Mandel { Inputs = { | ||
| 82 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, | ||
| 83 | Mode = Input { Value = FuID { "Julia" }, }, | ||
| 84 | JuliaSeed = Input { Value = { -0.7, 0.27015 }, }, | ||
| 85 | Iterations = Input { Value = 64, }, | ||
| 86 | }, }, | ||
| 87 | G = Mandel { Inputs = { | ||
| 88 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, | ||
| 89 | Gradient = Input { Value = Gradient { Colors = { | ||
| 90 | [0] = { 0, 0, 1, 1 }, | ||
| 91 | [1] = { 1, 1, 0, 1 }, | ||
| 92 | } }, }, | ||
| 93 | }, }, | ||
| 94 | "#); | ||
| 95 | let Some((tex_m, px_m)) = render(&c, "M", 0.0) else { return }; | ||
| 96 | assert_eq!(tex_m.size, [64, 64]); | ||
| 97 | assert_eq!(at(&px_m, 64, 32, 32), [0, 0, 0, 255], "cardioid center should be inside set"); | ||
| 98 | let corner = at(&px_m, 64, 2, 2); | ||
| 99 | assert!(corner[0] as u32 + corner[1] as u32 + corner[2] as u32 > 20, "exterior should be colored: {corner:?}"); | ||
| 100 | |||
| 101 | let (_, px_j) = render(&c, "J", 0.0).unwrap(); | ||
| 102 | assert_ne!(px_m, px_j, "Julia mode should differ from Mandelbrot"); | ||
| 103 | |||
| 104 | let (_, px_g) = render(&c, "G", 0.0).unwrap(); | ||
| 105 | let g_corner = at(&px_g, 64, 2, 2); | ||
| 106 | assert!(g_corner[2] > 100, "Gradient LUT exterior near t=0 should be blue: {g_corner:?}"); | ||
| 107 | } | ||
| 108 | |||
| 109 | #[test] | ||
| 110 | fn unsharp_mask_boosts_edges_and_respects_threshold_and_channels() { | ||
| 111 | let c = comp(r#" | ||
| 112 | Dark = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 0.3, }, TopLeftGreen = Input { Value = 0.3, }, TopLeftBlue = Input { Value = 0.3, }, }, }, | ||
| 113 | Bright = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 0.7, }, TopLeftGreen = Input { Value = 0.7, }, TopLeftBlue = Input { Value = 0.7, }, }, }, | ||
| 114 | Rect = RectangleMask { Inputs = { Center = Input { Value = { 0.75, 0.5 }, }, Width = Input { Value = 0.5, }, Height = Input { Value = 1.0, }, }, }, | ||
| 115 | Step = Merge { Inputs = { Background = Input { SourceOp = "Dark", Source = "Output", }, Foreground = Input { SourceOp = "Bright", Source = "Output", }, EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, }, }, | ||
| 116 | USM = UnsharpMask { Inputs = { | ||
| 117 | Input = Input { SourceOp = "Step", Source = "Output", }, | ||
| 118 | Size = Input { Value = 4.0, }, Gain = Input { Value = 2.0, }, Threshold = Input { Value = 0.0, }, | ||
| 119 | }, }, | ||
| 120 | USMHighT = UnsharpMask { Inputs = { | ||
| 121 | Input = Input { SourceOp = "Step", Source = "Output", }, | ||
| 122 | Size = Input { Value = 4.0, }, Gain = Input { Value = 2.0, }, Threshold = Input { Value = 0.9, }, | ||
| 123 | }, }, | ||
| 124 | USMNoRed = UnsharpMask { Inputs = { | ||
| 125 | Input = Input { SourceOp = "Step", Source = "Output", }, | ||
| 126 | Size = Input { Value = 4.0, }, Gain = Input { Value = 2.0, }, ProcessRed = Input { Value = 0, }, | ||
| 127 | }, }, | ||
| 128 | "#); | ||
| 129 | let Some((_, px_step)) = render(&c, "Step", 0.0) else { return }; | ||
| 130 | let (_, px_usm) = render(&c, "USM", 0.0).unwrap(); | ||
| 131 | let (_, px_hi) = render(&c, "USMHighT", 0.0).unwrap(); | ||
| 132 | let (_, px_nr) = render(&c, "USMNoRed", 0.0).unwrap(); | ||
| 133 | |||
| 134 | // Right at x=31 (dark side) and x=32 (bright side) of the step edge: | ||
| 135 | assert!(at(&px_usm, 64, 31, 32)[0] + 25 < at(&px_step, 64, 31, 32)[0], "expected dark undershoot at x=31"); | ||
| 136 | assert!(at(&px_usm, 64, 32, 32)[0] > at(&px_step, 64, 32, 32)[0] + 25, "expected bright overshoot at x=32"); | ||
| 137 | assert_eq!(at(&px_hi, 64, 32, 32)[0], at(&px_step, 64, 32, 32)[0], "high threshold must suppress boost"); | ||
| 138 | assert_eq!(at(&px_nr, 64, 32, 32)[0], at(&px_step, 64, 32, 32)[0], "ProcessRed=0 keeps red unchanged"); | ||
| 139 | assert!(at(&px_nr, 64, 32, 32)[1] > at(&px_step, 64, 32, 32)[1] + 25, "ProcessRed=0 still boosts green"); | ||
| 140 | } | ||
| 141 | |||
| 142 | #[test] | ||
| 143 | fn letterbox_frames_black_bars_position_and_panscan_crops() { | ||
| 144 | let c = comp(r#" | ||
| 145 | Wide = Background { Inputs = { | ||
| 146 | Width = Input { Value = 64, }, Height = Input { Value = 32, }, | ||
| 147 | TopLeftRed = Input { Value = 0.9, }, TopLeftGreen = Input { Value = 0.2, }, TopLeftBlue = Input { Value = 0.1, }, | ||
| 148 | }, }, | ||
| 149 | LB = Letterbox { Inputs = { | ||
| 150 | Input = Input { SourceOp = "Wide", Source = "Output", }, | ||
| 151 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, | ||
| 152 | Mode = Input { Value = 0, }, | ||
| 153 | }, }, | ||
| 154 | LBTop = Letterbox { Inputs = { | ||
| 155 | Input = Input { SourceOp = "Wide", Source = "Output", }, | ||
| 156 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, | ||
| 157 | Mode = Input { Value = 0, }, Position = Input { Value = 1.0, }, | ||
| 158 | FilterMethod = Input { Value = 0, }, | ||
| 159 | }, }, | ||
| 160 | PS = Letterbox { Inputs = { | ||
| 161 | Input = Input { SourceOp = "Wide", Source = "Output", }, | ||
| 162 | Width = Input { Value = 64, }, Height = Input { Value = 64, }, | ||
| 163 | Mode = Input { Value = 1, }, | ||
| 164 | }, }, | ||
| 165 | "#); | ||
| 166 | let Some((tex_lb, px_lb)) = render(&c, "LB", 0.0) else { return }; | ||
| 167 | assert_eq!(tex_lb.size, [64, 64]); | ||
| 168 | assert_eq!(at(&px_lb, 64, 32, 4), [0, 0, 0, 0]); | ||
| 169 | assert_eq!(at(&px_lb, 64, 32, 60), [0, 0, 0, 0]); | ||
| 170 | assert!(at(&px_lb, 64, 32, 32)[0] > 200); | ||
| 171 | |||
| 172 | // Position = +1 pushes image to top half (y=0..32) and leaves bottom half black. | ||
| 173 | let (_, px_top) = render(&c, "LBTop", 0.0).unwrap(); | ||
| 174 | assert!(at(&px_top, 64, 32, 8)[0] > 200); | ||
| 175 | assert_eq!(at(&px_top, 64, 32, 56), [0, 0, 0, 0]); | ||
| 176 | |||
| 177 | let (_, px_ps) = render(&c, "PS", 0.0).unwrap(); | ||
| 178 | assert!(at(&px_ps, 64, 32, 4)[0] > 200); | ||
| 179 | assert!(at(&px_ps, 64, 32, 60)[0] > 200); | ||
| 180 | } | ||
| 181 | |||
| 182 | #[test] | ||
| 183 | fn fuse_wipe_linear_invert_softness_radial_and_iris() { | ||
| 184 | let c = comp(r#" | ||
| 185 | Red = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, }, }, | ||
| 186 | Blue = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftBlue = Input { Value = 1, }, }, }, | ||
| 187 | Lin = Fuse.Wipe { Inputs = { | ||
| 188 | Input1 = Input { SourceOp = "Red", Source = "Output", }, | ||
| 189 | Input2 = Input { SourceOp = "Blue", Source = "Output", }, | ||
| 190 | Amount = Input { Value = 0.5, }, Angle = Input { Value = 0, }, | ||
| 191 | }, }, | ||
| 192 | Inv = Fuse.Wipe { Inputs = { | ||
| 193 | Input1 = Input { SourceOp = "Red", Source = "Output", }, | ||
| 194 | Input2 = Input { SourceOp = "Blue", Source = "Output", }, | ||
| 195 | Amount = Input { Value = 0.5, }, Invert = Input { Value = 1, }, | ||
| 196 | }, }, | ||
| 197 | Soft = Fuse.Wipe { Inputs = { | ||
| 198 | Input1 = Input { SourceOp = "Red", Source = "Output", }, | ||
| 199 | Input2 = Input { SourceOp = "Blue", Source = "Output", }, | ||
| 200 | Amount = Input { Value = 0.5, }, Softness = Input { Value = 0.4, }, | ||
| 201 | }, }, | ||
| 202 | Iris = Fuse.Wipe { Inputs = { | ||
| 203 | Input1 = Input { SourceOp = "Red", Source = "Output", }, | ||
| 204 | Input2 = Input { SourceOp = "Blue", Source = "Output", }, | ||
| 205 | Amount = Input { Value = 0.5, }, WipeType = Input { Value = FuID { "Iris" }, }, | ||
| 206 | }, }, | ||
| 207 | "#); | ||
| 208 | let Some((_, px_lin)) = render(&c, "Lin", 0.0) else { return }; | ||
| 209 | assert_eq!(at(&px_lin, 64, 8, 32), [0, 0, 255, 255]); | ||
| 210 | assert_eq!(at(&px_lin, 64, 56, 32), [255, 0, 0, 255]); | ||
| 211 | |||
| 212 | let (_, px_inv) = render(&c, "Inv", 0.0).unwrap(); | ||
| 213 | assert_eq!(at(&px_inv, 64, 8, 32), [255, 0, 0, 255]); | ||
| 214 | assert_eq!(at(&px_inv, 64, 56, 32), [0, 0, 255, 255]); | ||
| 215 | |||
| 216 | let (_, px_soft) = render(&c, "Soft", 0.0).unwrap(); | ||
| 217 | let mid = at(&px_soft, 64, 32, 32); | ||
| 218 | assert!(mid[0] > 80 && mid[2] > 80, "soft wipe center should blend both inputs: {mid:?}"); | ||
| 219 | |||
| 220 | let (_, px_iris) = render(&c, "Iris", 0.0).unwrap(); | ||
| 221 | assert_eq!(at(&px_iris, 64, 32, 32), [0, 0, 255, 255]); | ||
| 222 | assert_eq!(at(&px_iris, 64, 2, 2), [255, 0, 0, 255]); | ||
| 223 | } | ||
| 224 | |||
| 225 | #[test] | ||
| 226 | fn fuse_null_and_set_metadata_preserve_pixels_dod_and_aux() { | ||
| 227 | let c = comp(r#" | ||
| 228 | BG = Background { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, TopLeftGreen = Input { Value = 0.8, }, }, }, | ||
| 229 | Aux = Dimension.CopyAux { Inputs = { | ||
| 230 | Input = Input { SourceOp = "BG", Source = "Output", }, | ||
| 231 | Channel = Input { Value = FuID { "Z" }, }, | ||
| 232 | Mode = Input { Value = 1, }, | ||
| 233 | }, }, | ||
| 234 | NullPass = Fuse.Null { Inputs = { Input = Input { SourceOp = "Aux", Source = "Output", }, }, }, | ||
| 235 | MetaPass = Fuse.SetMetaData { Inputs = { | ||
| 236 | Input = Input { SourceOp = "NullPass", Source = "Output", }, | ||
| 237 | FieldName = Input { Value = "TEST", }, FieldValue = Input { Value = "ok", }, | ||
| 238 | }, }, | ||
| 239 | NullEmpty = Fuse.Null { }, | ||
| 240 | "#); | ||
| 241 | let Some((tex_aux, px_aux)) = render(&c, "Aux", 0.0) else { return }; | ||
| 242 | let (tex_meta, px_meta) = render(&c, "MetaPass", 0.0).unwrap(); | ||
| 243 | assert_eq!(px_aux, px_meta); | ||
| 244 | assert_eq!(tex_aux.origin, tex_meta.origin); | ||
| 245 | assert_eq!(tex_aux.canvas_size, tex_meta.canvas_size); | ||
| 246 | assert!(tex_meta.aux.as_ref().and_then(|a| a.z.as_ref()).is_some(), "aux.z must be preserved"); | ||
| 247 | |||
| 248 | let (tex_empty, _) = render(&c, "NullEmpty", 0.0).unwrap(); | ||
| 249 | assert_eq!(tex_empty.size, [1, 1]); | ||
| 250 | } | ||
| 251 | |||
| 252 | #[test] | ||
| 253 | fn fuse_linger_accumulates_decaying_past_frames_and_preserves_static() { | ||
| 254 | let c = comp(r#" | ||
| 255 | Pulse = Background { Inputs = { | ||
| 256 | Width = Input { Value = 16, }, Height = Input { Value = 16, }, | ||
| 257 | TopLeftRed = Input { SourceOp = "PulseR", Source = "Value", }, | ||
| 258 | }, }, | ||
| 259 | PulseR = BezierSpline { KeyFrames = { | ||
| 260 | [0] = { 1.0, Flags = { StepIn = true } }, | ||
| 261 | [1] = { 0.0, Flags = { StepIn = true } }, | ||
| 262 | [2] = { 0.0, Flags = { StepIn = true } }, | ||
| 263 | } }, | ||
| 264 | L = Fuse.Linger { Inputs = { | ||
| 265 | Background = Input { SourceOp = "Pulse", Source = "Output", }, | ||
| 266 | Frames = Input { Value = 4, }, Linger = Input { Value = 0.7, }, | ||
| 267 | }, }, | ||
| 268 | Static = Background { Inputs = { Width = Input { Value = 16, }, Height = Input { Value = 16, }, TopLeftGreen = Input { Value = 0.6, }, }, }, | ||
| 269 | LStatic = Fuse.Linger { Inputs = { | ||
| 270 | Background = Input { SourceOp = "Static", Source = "Output", }, | ||
| 271 | Frames = Input { Value = 4, }, Linger = Input { Value = 0.8, }, | ||
| 272 | }, }, | ||
| 273 | "#); | ||
| 274 | let Some((_, px_t1)) = render(&c, "L", 1.0) else { return }; | ||
| 275 | let (_, px_t2) = render(&c, "L", 2.0).unwrap(); | ||
| 276 | let r1 = at(&px_t1, 16, 8, 8)[0]; | ||
| 277 | let r2 = at(&px_t2, 16, 8, 8)[0]; | ||
| 278 | assert!(r1 > 40, "expected lingering persistence at frame 1, got {r1}"); | ||
| 279 | assert!(r2 > 10 && r2 < r1, "expected monotonic decay at frame 2 (r1={r1}, r2={r2})"); | ||
| 280 | |||
| 281 | let (_, px_s) = render(&c, "Static", 3.0).unwrap(); | ||
| 282 | let (_, px_ls) = render(&c, "LStatic", 3.0).unwrap(); | ||
| 283 | assert_eq!(px_s, px_ls, "static clip must be exact identity under Linger"); | ||
| 284 | } | ||
| 285 | |||
| 286 | #[test] | ||
| 287 | fn fuse_ocl_rays_casts_radial_light_shafts_with_threshold_and_tint() { | ||
| 288 | let c = comp(r#" | ||
| 289 | Black = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, }, | ||
| 290 | White = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 1, }, TopLeftBlue = Input { Value = 1, }, }, }, | ||
| 291 | Dot = EllipseMask { Inputs = { Center = Input { Value = { 0.5, 0.5 }, }, Width = Input { Value = 0.12, }, Height = Input { Value = 0.12, }, }, }, | ||
| 292 | Src = Merge { Inputs = { Background = Input { SourceOp = "Black", Source = "Output", }, Foreground = Input { SourceOp = "White", Source = "Output", }, EffectMask = Input { SourceOp = "Dot", Source = "Mask", }, }, }, | ||
| 293 | Rays = Fuse.OCLRays { Inputs = { | ||
| 294 | Image = Input { SourceOp = "Src", Source = "Output", }, | ||
| 295 | Center = Input { Value = { 0.5, 0.5 }, }, | ||
| 296 | Threshold = Input { Value = 0.2, }, Decay = Input { Value = 0.02, }, | ||
| 297 | Weight = Input { Value = 3.5, }, Exposure = Input { Value = 1.0, }, | ||
| 298 | Samples = Input { Value = 48, }, | ||
| 299 | ColorRed = Input { Value = 1.0, }, ColorGreen = Input { Value = 0.5, }, ColorBlue = Input { Value = 0.0, }, | ||
| 300 | }, }, | ||
| 301 | "#); | ||
| 302 | let Some((_, px_src)) = render(&c, "Src", 0.0) else { return }; | ||
| 303 | let (_, px_rays) = render(&c, "Rays", 0.0).unwrap(); | ||
| 304 | assert_eq!(at(&px_src, 64, 48, 32)[0], 0); | ||
| 305 | let shaft = at(&px_rays, 64, 48, 32); | ||
| 306 | assert!(shaft[0] > 15, "expected outward radial light shaft at (48, 32), got {shaft:?}"); | ||
| 307 | assert!(shaft[0] > shaft[1] && shaft[2] == 0, "expected warm orange tint: {shaft:?}"); | ||
| 308 | assert!(at(&px_rays, 64, 40, 32)[0] > at(&px_rays, 64, 58, 32)[0], "closer to source should be brighter"); | ||
| 309 | } | ||
| 310 | |||
| 311 | #[test] | ||
| 312 | fn fx_fuses_uniform_ring_grows_resets_and_trims() { | ||
| 313 | let Ok(eng) = Engine::new_headless() else { return }; | ||
| 314 | let mut pipes = FxFusesPipelines::new(&eng.device, &eng.queue); | ||
| 315 | let params = UniformParams::default(); | ||
| 316 | for _ in 0..(FX_UNIFORM_SLOTS_PER_CHUNK * 2 + 5) { | ||
| 317 | pipes.alloc_uniform_slot(&eng.device, &eng.queue, &params); | ||
| 318 | } | ||
| 319 | assert_eq!(pipes.uniform_chunks.len(), 3); | ||
| 320 | pipes.reset_uniform_cursor(); | ||
| 321 | assert_eq!(pipes.uniform_cursor, 0); | ||
| 322 | pipes.trim_uniform_chunks(2); | ||
| 323 | assert_eq!(pipes.uniform_chunks.len(), 2); | ||
| 324 | } | ||
crates/engine/src/eval/mod.rs+18| ... | @@ -29,6 +29,7 @@ mod paint; | ... | @@ -29,6 +29,7 @@ mod paint; |
| 29 | mod shapes; | 29 | mod shapes; |
| 30 | mod warps; | 30 | mod warps; |
| 31 | mod warps_masks; | 31 | mod warps_masks; |
| 32 | mod fx_fuses; | ||
| 32 | mod text_3d; | 33 | mod text_3d; |
| 33 | mod registry; | 34 | mod registry; |
| 34 | mod dispatch; | 35 | mod dispatch; |
| ... | @@ -78,6 +79,7 @@ pub struct Engine { | ... | @@ -78,6 +79,7 @@ pub struct Engine { |
| 78 | color_science_pipelines: OnceCell<RefCell<color_science::ColorSciencePipelines>>, | 79 | color_science_pipelines: OnceCell<RefCell<color_science::ColorSciencePipelines>>, |
| 79 | depth_optical_pipelines: OnceCell<RefCell<depth_optical::DepthOpticalPipelines>>, | 80 | depth_optical_pipelines: OnceCell<RefCell<depth_optical::DepthOpticalPipelines>>, |
| 80 | warps_masks_pipelines: OnceCell<RefCell<warps_masks::WarpsMasksPipelines>>, | 81 | warps_masks_pipelines: OnceCell<RefCell<warps_masks::WarpsMasksPipelines>>, |
| 82 | fx_fuses_pipelines: OnceCell<RefCell<fx_fuses::FxFusesPipelines>>, | ||
| 81 | motion_blur: crate::motion_blur::MotionBlurPipelines, | 83 | motion_blur: crate::motion_blur::MotionBlurPipelines, |
| 82 | supports_f32_filter: bool, | 84 | supports_f32_filter: bool, |
| 83 | scene3d: crate::scene3d::Scene3DRenderer, | 85 | scene3d: crate::scene3d::Scene3DRenderer, |
| ... | @@ -186,6 +188,7 @@ impl Engine { | ... | @@ -186,6 +188,7 @@ impl Engine { |
| 186 | color_science_pipelines: OnceCell::new(), | 188 | color_science_pipelines: OnceCell::new(), |
| 187 | depth_optical_pipelines: OnceCell::new(), | 189 | depth_optical_pipelines: OnceCell::new(), |
| 188 | warps_masks_pipelines: OnceCell::new(), | 190 | warps_masks_pipelines: OnceCell::new(), |
| 191 | fx_fuses_pipelines: OnceCell::new(), | ||
| 189 | motion_blur: crate::motion_blur::MotionBlurPipelines::default(), | 192 | motion_blur: crate::motion_blur::MotionBlurPipelines::default(), |
| 190 | supports_f32_filter, | 193 | supports_f32_filter, |
| 191 | scene3d, | 194 | scene3d, |
| ... | @@ -339,6 +342,9 @@ impl Engine { | ... | @@ -339,6 +342,9 @@ impl Engine { |
| 339 | if let Some(wm) = self.warps_masks_pipelines.get() { | 342 | if let Some(wm) = self.warps_masks_pipelines.get() { |
| 340 | wm.borrow_mut().reset_uniform_cursor(); | 343 | wm.borrow_mut().reset_uniform_cursor(); |
| 341 | } | 344 | } |
| 345 | if let Some(ff) = self.fx_fuses_pipelines.get() { | ||
| 346 | ff.borrow_mut().reset_uniform_cursor(); | ||
| 347 | } | ||
| 342 | if let Some(of) = self.optflow.get() { | 348 | if let Some(of) = self.optflow.get() { |
| 343 | of.reset_uniform_cursor(); | 349 | of.reset_uniform_cursor(); |
| 344 | } | 350 | } |
| ... | @@ -362,6 +368,9 @@ impl Engine { | ... | @@ -362,6 +368,9 @@ impl Engine { |
| 362 | if let Some(wm) = self.warps_masks_pipelines.get() { | 368 | if let Some(wm) = self.warps_masks_pipelines.get() { |
| 363 | wm.borrow_mut().trim_uniform_chunks(2); | 369 | wm.borrow_mut().trim_uniform_chunks(2); |
| 364 | } | 370 | } |
| 371 | if let Some(ff) = self.fx_fuses_pipelines.get() { | ||
| 372 | ff.borrow_mut().trim_uniform_chunks(2); | ||
| 373 | } | ||
| 365 | self.media.trim_nv12_uniform_chunks(2); | 374 | self.media.trim_nv12_uniform_chunks(2); |
| 366 | } | 375 | } |
| 367 | 376 | ||
| ... | @@ -836,6 +845,7 @@ impl Engine { | ... | @@ -836,6 +845,7 @@ impl Engine { |
| 836 | "DVE" => self.eval_dve(comp, node, time, frame_i, enc, visiting, stats), | 845 | "DVE" => self.eval_dve(comp, node, time, frame_i, enc, visiting, stats), |
| 837 | "Shadow" => self.eval_shadow(comp, node, time, frame_i, enc, visiting, stats), | 846 | "Shadow" => self.eval_shadow(comp, node, time, frame_i, enc, visiting, stats), |
| 838 | "Scale" | "BetterResize" => self.eval_scale(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | 847 | "Scale" | "BetterResize" => self.eval_scale(comp, node, time, frame_i, comp_dims, enc, visiting, stats), |
| 848 | "Letterbox" => self.eval_letterbox(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | ||
| 839 | "Crop" => self.eval_crop(comp, node, time, frame_i, enc, visiting, stats), | 849 | "Crop" => self.eval_crop(comp, node, time, frame_i, enc, visiting, stats), |
| 840 | "SetDomain" => self.eval_set_domain(comp, node, time, frame_i, enc, visiting, stats), | 850 | "SetDomain" => self.eval_set_domain(comp, node, time, frame_i, enc, visiting, stats), |
| 841 | "ChangeDepth" => { | 851 | "ChangeDepth" => { |
| ... | @@ -856,6 +866,7 @@ impl Engine { | ... | @@ -856,6 +866,7 @@ impl Engine { |
| 856 | "Dimension.CopyAux" => self.eval_copy_aux(comp, node, time, frame_i, enc, visiting, stats), | 866 | "Dimension.CopyAux" => self.eval_copy_aux(comp, node, time, frame_i, enc, visiting, stats), |
| 857 | "Dimension.OpticalFlow" | "OpticalFlow" => self.eval_optical_flow(comp, node, time, frame_i, enc, visiting, stats), | 867 | "Dimension.OpticalFlow" | "OpticalFlow" => self.eval_optical_flow(comp, node, time, frame_i, enc, visiting, stats), |
| 858 | "VectorMotionBlur" => self.eval_vector_motion_blur(comp, node, time, frame_i, enc, visiting, stats), | 868 | "VectorMotionBlur" => self.eval_vector_motion_blur(comp, node, time, frame_i, enc, visiting, stats), |
| 869 | "VectorDenoise" => self.eval_vector_denoise(comp, node, time, frame_i, enc, visiting, stats), | ||
| 859 | "BrightnessContrast" => self.eval_bc(comp, node, time, frame_i, enc, visiting, stats), | 870 | "BrightnessContrast" => self.eval_bc(comp, node, time, frame_i, enc, visiting, stats), |
| 860 | "ColorCorrector" => self.eval_cc(comp, node, time, frame_i, enc, visiting, stats), | 871 | "ColorCorrector" => self.eval_cc(comp, node, time, frame_i, enc, visiting, stats), |
| 861 | "ColorGain" => self.eval_color_gain(comp, node, time, frame_i, enc, visiting, stats), | 872 | "ColorGain" => self.eval_color_gain(comp, node, time, frame_i, enc, visiting, stats), |
| ... | @@ -880,6 +891,7 @@ impl Engine { | ... | @@ -880,6 +891,7 @@ impl Engine { |
| 880 | "CleanPLate" | "CleanPlate" => self.eval_clean_plate(comp, node, time, frame_i, enc, visiting, stats), | 891 | "CleanPLate" | "CleanPlate" => self.eval_clean_plate(comp, node, time, frame_i, enc, visiting, stats), |
| 881 | "Blur" => self.eval_blur(comp, node, time, frame_i, enc, visiting, stats), | 892 | "Blur" => self.eval_blur(comp, node, time, frame_i, enc, visiting, stats), |
| 882 | "Sharpen" => self.eval_sharpen(comp, node, time, frame_i, enc, visiting, stats), | 893 | "Sharpen" => self.eval_sharpen(comp, node, time, frame_i, enc, visiting, stats), |
| 894 | "UnsharpMask" => self.eval_unsharp_mask(comp, node, time, frame_i, enc, visiting, stats), | ||
| 883 | "Filter" => self.eval_filter(comp, node, time, frame_i, enc, visiting, stats), | 895 | "Filter" => self.eval_filter(comp, node, time, frame_i, enc, visiting, stats), |
| 884 | "RemoveNoise" => self.eval_remove_noise(comp, node, time, frame_i, enc, visiting, stats), | 896 | "RemoveNoise" => self.eval_remove_noise(comp, node, time, frame_i, enc, visiting, stats), |
| 885 | "VariBlur" => self.eval_vari_blur(comp, node, time, frame_i, enc, visiting, stats), | 897 | "VariBlur" => self.eval_vari_blur(comp, node, time, frame_i, enc, visiting, stats), |
| ... | @@ -937,6 +949,7 @@ impl Engine { | ... | @@ -937,6 +949,7 @@ impl Engine { |
| 937 | "FastNoise" => self.eval_fast_noise(comp, node, time, frame_i, comp_dims, enc, stats), | 949 | "FastNoise" => self.eval_fast_noise(comp, node, time, frame_i, comp_dims, enc, stats), |
| 938 | "Plasma" => self.eval_plasma(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | 950 | "Plasma" => self.eval_plasma(comp, node, time, frame_i, comp_dims, enc, visiting, stats), |
| 939 | "DaySky" => self.eval_day_sky(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | 951 | "DaySky" => self.eval_day_sky(comp, node, time, frame_i, comp_dims, enc, visiting, stats), |
| 952 | "Mandel" => self.eval_mandel(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | ||
| 940 | "FilmGrain" => self.eval_film_grain(comp, node, time, frame_i, enc, visiting, stats), | 953 | "FilmGrain" => self.eval_film_grain(comp, node, time, frame_i, enc, visiting, stats), |
| 941 | "Displace" => self.eval_displace(comp, node, time, frame_i, enc, visiting, stats), | 954 | "Displace" => self.eval_displace(comp, node, time, frame_i, enc, visiting, stats), |
| 942 | "Vortex" => self.eval_vortex(comp, node, time, frame_i, enc, visiting, stats), | 955 | "Vortex" => self.eval_vortex(comp, node, time, frame_i, enc, visiting, stats), |
| ... | @@ -953,6 +966,11 @@ impl Engine { | ... | @@ -953,6 +966,11 @@ impl Engine { |
| 953 | "Fuse.AlphaWeightedNoise" => self.eval_alpha_weighted_noise(comp, node, time, frame_i, enc, visiting, stats), | 966 | "Fuse.AlphaWeightedNoise" => self.eval_alpha_weighted_noise(comp, node, time, frame_i, enc, visiting, stats), |
| 954 | "Fuse.Crystallize" => self.eval_crystallize(comp, node, time, frame_i, enc, visiting, stats), | 967 | "Fuse.Crystallize" => self.eval_crystallize(comp, node, time, frame_i, enc, visiting, stats), |
| 955 | "Fuse.HexagonTiles" => self.eval_hexagon_tiles(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | 968 | "Fuse.HexagonTiles" => self.eval_hexagon_tiles(comp, node, time, frame_i, comp_dims, enc, visiting, stats), |
| 969 | "Fuse.Wipe" => self.eval_fuse_wipe(comp, node, time, frame_i, enc, visiting, stats), | ||
| 970 | "Fuse.Null" => self.eval_fuse_null(comp, node, time, enc, visiting, stats), | ||
| 971 | "Fuse.SetMetaData" => self.eval_fuse_set_metadata(comp, node, time, enc, visiting, stats), | ||
| 972 | "Fuse.Linger" => self.eval_fuse_linger(comp, node, time, frame_i, enc, visiting, stats), | ||
| 973 | "Fuse.OCLRays" => self.eval_fuse_ocl_rays(comp, node, time, frame_i, enc, visiting, stats), | ||
| 956 | "sRender" => self.eval_s_render(comp, node, time, frame_i, comp_dims, enc, visiting, stats), | 974 | "sRender" => self.eval_s_render(comp, node, time, frame_i, comp_dims, enc, visiting, stats), |
| 957 | "sNGon" | 975 | "sNGon" |
| 958 | | "sRectangle" | 976 | | "sRectangle" |
crates/engine/src/shaders/fx_fuses.wgsl created+641| ... | @@ -0,0 +1,641 @@ | ||
| 1 | // Wave 36 compute shaders: | ||
| 2 | // - VectorDenoise (motion-compensated bilateral temporal denoiser) | ||
| 3 | // - Mandel (escape-time Mandelbrot / Julia fractal generator with smooth coloring) | ||
| 4 | // - UnsharpMask (thresholded high-frequency sharpening boost) | ||
| 5 | // - Letterbox (aspect-preserving Letterbox & Pan-and-Scan framing) | ||
| 6 | // - Fuse.Wipe (directional / radial / iris / box transition wipe) | ||
| 7 | // - Fuse.Linger (bounded-lookback temporal persistence step & finalize) | ||
| 8 | // - Fuse.OCLRays (volumetric radial light shafts / god-rays) | ||
| 9 | // | ||
| 10 | // WebGPU-safe (`wasm32` compatible): | ||
| 11 | // - Output storage texture is WriteOnly (patched at pipeline build for rgba16float/rgba32float) | ||
| 12 | // - `aux_tex` is bound with `filterable: false` and ALWAYS accessed via `textureLoad` | ||
| 13 | // - No `read_write` storage textures; 256-byte dynamic uniform ring buffer. | ||
| 14 | |||
| 15 | @group(0) @binding(0) var src_a: texture_2d<f32>; | ||
| 16 | @group(0) @binding(1) var src_b: texture_2d<f32>; | ||
| 17 | @group(0) @binding(2) var mask_tex: texture_2d<f32>; | ||
| 18 | @group(0) @binding(3) var aux_tex: texture_2d<f32>; | ||
| 19 | @group(0) @binding(4) var samp_lin: sampler; | ||
| 20 | @group(0) @binding(5) var dst_tex: texture_storage_2d<rgba8unorm, write>; | ||
| 21 | |||
| 22 | const HDR_FLOAT: bool = false; | ||
| 23 | const PI: f32 = 3.141592653589793; | ||
| 24 | const TAU: f32 = 6.283185307179586; | ||
| 25 | |||
| 26 | struct Params { | ||
| 27 | p: array<vec4<f32>, 16>, | ||
| 28 | }; | ||
| 29 | @group(0) @binding(6) var<uniform> u: Params; | ||
| 30 | |||
| 31 | fn store_dst(p: vec2<i32>, col: vec4<f32>) { | ||
| 32 | if (HDR_FLOAT) { | ||
| 33 | textureStore(dst_tex, p, col); | ||
| 34 | } else { | ||
| 35 | textureStore(dst_tex, p, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0))); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | fn pix_to_fu(gid: vec2<u32>, dims: vec2<u32>) -> vec2<f32> { | ||
| 40 | let f = vec2<f32>(dims); | ||
| 41 | return vec2<f32>((f32(gid.x) + 0.5) / f.x, 1.0 - (f32(gid.y) + 0.5) / f.y); | ||
| 42 | } | ||
| 43 | |||
| 44 | fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, invert: f32) -> f32 { | ||
| 45 | if (has_mask < 0.5) { | ||
| 46 | return 1.0; | ||
| 47 | } | ||
| 48 | let uv = (vec2<f32>(gid) + 0.5) / vec2<f32>(dims); | ||
| 49 | var m = 0.0; | ||
| 50 | if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) { | ||
| 51 | m = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a; | ||
| 52 | } | ||
| 53 | return select(m, 1.0 - m, invert > 0.5); | ||
| 54 | } | ||
| 55 | |||
| 56 | fn apply_channel_blend( | ||
| 57 | orig: vec4<f32>, | ||
| 58 | effect: vec4<f32>, | ||
| 59 | eff: f32, | ||
| 60 | mask_v: f32, | ||
| 61 | mbm: f32, | ||
| 62 | skip: vec4<f32>, | ||
| 63 | ) -> vec4<f32> { | ||
| 64 | var out_c = mix(orig, effect, eff); | ||
| 65 | if (mbm > 0.5) { | ||
| 66 | out_c = out_c * mask_v; | ||
| 67 | } | ||
| 68 | return vec4<f32>( | ||
| 69 | select(out_c.r, orig.r, skip.x > 0.5), | ||
| 70 | select(out_c.g, orig.g, skip.y > 0.5), | ||
| 71 | select(out_c.b, orig.b, skip.z > 0.5), | ||
| 72 | select(out_c.a, orig.a, skip.w > 0.5), | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | fn safe_smoothstep(lo: f32, hi: f32, x: f32) -> f32 { | ||
| 77 | if (hi - lo <= 1e-5) { | ||
| 78 | return select(0.0, 1.0, x >= 0.5 * (lo + hi)); | ||
| 79 | } | ||
| 80 | return smoothstep(lo, hi, x); | ||
| 81 | } | ||
| 82 | |||
| 83 | fn sanitize_f32(v: f32, bound: f32) -> f32 { | ||
| 84 | if (!(v == v)) { | ||
| 85 | return 0.0; | ||
| 86 | } | ||
| 87 | return clamp(v, -bound, bound); | ||
| 88 | } | ||
| 89 | |||
| 90 | // ============================================================================ | ||
| 91 | // 1. VectorDenoise: Motion-compensated bilateral temporal denoiser | ||
| 92 | // ============================================================================ | ||
| 93 | |||
| 94 | fn vd_feat(c: vec4<f32>) -> vec4<f32> { | ||
| 95 | let luma = dot(c.rgb, vec3<f32>(0.299, 0.587, 0.114)); | ||
| 96 | let rg = (c.r - c.g) * 0.5; | ||
| 97 | let by = (c.b - 0.5 * (c.r + c.g)) * 0.5; | ||
| 98 | return vec4<f32>(luma, rg, by, c.a); | ||
| 99 | } | ||
| 100 | |||
| 101 | fn vd_patch_sad(uv: vec2<f32>, flow_px: vec2<f32>, inv_sz: vec2<f32>) -> f32 { | ||
| 102 | let uv_b = uv + flow_px * inv_sz; | ||
| 103 | var cost: f32 = 0.0; | ||
| 104 | for (var dy: i32 = -1; dy <= 1; dy = dy + 1) { | ||
| 105 | for (var dx: i32 = -1; dx <= 1; dx = dx + 1) { | ||
| 106 | let off = vec2<f32>(f32(dx), f32(dy)) * inv_sz; | ||
| 107 | let w = select(1.0, 2.0, dx == 0 && dy == 0); | ||
| 108 | let ca = textureSampleLevel(src_a, samp_lin, clamp(uv + off, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | ||
| 109 | let cb = textureSampleLevel(src_b, samp_lin, clamp(uv_b + off, vec2<f32>(0.0), vec2<f32>(1.0)), 0.0); | ||
| 110 | let d = abs(vd_feat(ca) - vd_feat(cb)); | ||
| 111 | cost = cost + w * (d.x + 0.85 * (d.y + d.z) + 0.5 * d.w); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | return cost; | ||
| 115 | } | ||
| 116 | |||
| 117 | fn vd_parabolic(c_neg: f32, c_0: f32, c_pos: f32) -> f32 { | ||
| 118 | let denom = c_neg - 2.0 * c_0 + c_pos; | ||
| 119 | if (denom <= 1e-5) { | ||
| 120 | return 0.0; | ||
| 121 | } | ||
| 122 | return clamp(0.5 * (c_neg - c_pos) / denom, -0.5, 0.5); | ||
| 123 | } | ||
| 124 | |||
| 125 | // Pass 1a: Warp neighbor frame `src_b` toward center frame `src_a`. | ||
| 126 | // p[0] = (has_vec, vec_scale_x, vec_scale_y, search_radius) | ||
| 127 | @compute @workgroup_size(8, 8) | ||
| 128 | fn vd_warp_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 129 | let dims = textureDimensions(dst_tex); | ||
| 130 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 131 | |||
| 132 | let p = vec2<i32>(gid.xy); | ||
| 133 | let fdims = vec2<f32>(dims); | ||
| 134 | let inv_sz = 1.0 / fdims; | ||
| 135 | let uv = (vec2<f32>(gid.xy) + 0.5) * inv_sz; | ||
| 136 | |||
| 137 | var flow_tex_px = vec2<f32>(0.0); | ||
| 138 | if (u.p[0].x > 0.5) { | ||
| 139 | let vdims = vec2<i32>(textureDimensions(aux_tex)); | ||
| 140 | let vx_i = clamp(i32(floor(uv.x * f32(vdims.x))), 0, max(vdims.x - 1, 0)); | ||
| 141 | let vy_i = clamp(i32(floor(uv.y * f32(vdims.y))), 0, max(vdims.y - 1, 0)); | ||
| 142 | let v_raw = textureLoad(aux_tex, vec2<i32>(vx_i, vy_i), 0).xy; | ||
| 143 | let vx = sanitize_f32(v_raw.x, 4096.0) * u.p[0].y; | ||
| 144 | let vy = sanitize_f32(v_raw.y, 4096.0) * u.p[0].z; | ||
| 145 | // Fusion motion vectors are +X right, +Y up; texture coords are +Y down. | ||
| 146 | flow_tex_px = vec2<f32>(vx, -vy); | ||
| 147 | } else { | ||
| 148 | let rad = clamp(i32(round(u.p[0].w)), 0, 4); | ||
| 149 | if (rad > 0) { | ||
| 150 | let reg = 0.0005; | ||
| 151 | var best = vec2<f32>(0.0); | ||
| 152 | var best_cost = vd_patch_sad(uv, best, inv_sz); | ||
| 153 | for (var dy: i32 = -rad; dy <= rad; dy = dy + 1) { | ||
| 154 | for (var dx: i32 = -rad; dx <= rad; dx = dx + 1) { | ||
| 155 | if (dx == 0 && dy == 0) { continue; } | ||
| 156 | let v = vec2<f32>(f32(dx), f32(dy)); | ||
| 157 | let c = vd_patch_sad(uv, v, inv_sz) + reg * dot(v, v); | ||
| 158 | if (c < best_cost) { | ||
| 159 | best_cost = c; | ||
| 160 | best = v; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | let cl = vd_patch_sad(uv, best + vec2<f32>(-1.0, 0.0), inv_sz); | ||
| 165 | let cr = vd_patch_sad(uv, best + vec2<f32>( 1.0, 0.0), inv_sz); | ||
| 166 | let cu = vd_patch_sad(uv, best + vec2<f32>( 0.0,-1.0), inv_sz); | ||
| 167 | let cd = vd_patch_sad(uv, best + vec2<f32>( 0.0, 1.0), inv_sz); | ||
| 168 | let c0 = vd_patch_sad(uv, best, inv_sz); | ||
| 169 | flow_tex_px = best + vec2<f32>(vd_parabolic(cl, c0, cr), vd_parabolic(cu, c0, cd)); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | let sample_uv = clamp(uv + flow_tex_px * inv_sz, vec2<f32>(0.0), vec2<f32>(1.0)); | ||
| 174 | let warped = textureSampleLevel(src_b, samp_lin, sample_uv, 0.0); | ||
| 175 | store_dst(p, warped); | ||
| 176 | } | ||
| 177 | |||
| 178 | fn vd_bilateral_weight(center_col: vec4<f32>, warped_col: vec4<f32>, sigma: f32, temporal_w: f32) -> f32 { | ||
| 179 | let diff = warped_col - center_col; | ||
| 180 | let dist2 = dot(diff.rgb, diff.rgb) + diff.a * diff.a; | ||
| 181 | let inv_2s2 = 0.5 / max(sigma * sigma, 1e-6); | ||
| 182 | return temporal_w * exp(-dist2 * inv_2s2); | ||
| 183 | } | ||
| 184 | |||
| 185 | // Pass 1b: Running bilateral weighted average of color. | ||
| 186 | // src_a = prev_accum, src_b = warped_nbr, mask_tex = curr_center, aux_tex = prev_weight | ||
| 187 | // p[0] = (sigma, temporal_w, 0, 0) | ||
| 188 | @compute @workgroup_size(8, 8) | ||
| 189 | fn vd_accum_color_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 190 | let dims = textureDimensions(dst_tex); | ||
| 191 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 192 | |||
| 193 | let p = vec2<i32>(gid.xy); | ||
| 194 | let prev_acc = textureLoad(src_a, p, 0); | ||
| 195 | let warped = textureLoad(src_b, p, 0); | ||
| 196 | let center = textureLoad(mask_tex, p, 0); | ||
| 197 | let prev_w = max(textureLoad(aux_tex, p, 0).x, 1e-5); | ||
| 198 | let w = vd_bilateral_weight(center, warped, u.p[0].x, u.p[0].y); | ||
| 199 | let alpha = w / (prev_w + w); | ||
| 200 | store_dst(p, mix(prev_acc, warped, alpha)); | ||
| 201 | } | ||
| 202 | |||
| 203 | // Pass 1c: Running bilateral weight sum (or fill 1.0 when p[0].z > 0.5). | ||
| 204 | // src_a = curr_center, src_b = warped_nbr, aux_tex = prev_weight | ||
| 205 | // p[0] = (sigma, temporal_w, init_one, 0) | ||
| 206 | @compute @workgroup_size(8, 8) | ||
| 207 | fn vd_accum_weight_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 208 | let dims = textureDimensions(dst_tex); | ||
| 209 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 210 | |||
| 211 | let p = vec2<i32>(gid.xy); | ||
| 212 | if (u.p[0].z > 0.5) { | ||
| 213 | store_dst(p, vec4<f32>(1.0, 0.0, 0.0, 1.0)); | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | let center = textureLoad(src_a, p, 0); | ||
| 217 | let warped = textureLoad(src_b, p, 0); | ||
| 218 | let prev_w = max(textureLoad(aux_tex, p, 0).x, 1e-5); | ||
| 219 | let w = vd_bilateral_weight(center, warped, u.p[0].x, u.p[0].y); | ||
| 220 | store_dst(p, vec4<f32>(prev_w + w, 0.0, 0.0, 1.0)); | ||
| 221 | } | ||
| 222 | |||
| 223 | // Pass 1d (also reused by Fuse.Linger): Blend/Mask/Channel-skip finalize. | ||
| 224 | // src_a = orig, src_b = effect, mask_tex = EffectMask | ||
| 225 | // p[0] = (blend, has_mask, mask_inv, mbm) | ||
| 226 | // p[1] = process_skip (r, g, b, a) | ||
| 227 | @compute @workgroup_size(8, 8) | ||
| 228 | fn vd_finalize_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 229 | let dims = textureDimensions(dst_tex); | ||
| 230 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 231 | |||
| 232 | let p = vec2<i32>(gid.xy); | ||
| 233 | let orig = textureLoad(src_a, p, 0); | ||
| 234 | let eff_col = textureLoad(src_b, p, 0); | ||
| 235 | let m = read_mask(gid.xy, dims, u.p[0].y, u.p[0].z); | ||
| 236 | let mbm = u.p[0].w; | ||
| 237 | let eff = select(clamp(u.p[0].x * m, 0.0, 1.0), clamp(u.p[0].x, 0.0, 1.0), mbm > 0.5); | ||
| 238 | let out_c = apply_channel_blend(orig, eff_col, eff, m, mbm, u.p[1]); | ||
| 239 | store_dst(p, out_c); | ||
| 240 | } | ||
| 241 | |||
| 242 | // ============================================================================ | ||
| 243 | // 2. Mandel: Mandelbrot / Julia escape-time fractal generator | ||
| 244 | // ============================================================================ | ||
| 245 | // src_b = optional 256x1 Gradient LUT when p[4].x > 0.5 | ||
| 246 | // mask_tex = EffectMask | ||
| 247 | // p[0] = (cx, cy, zoom, rot_rad) | ||
| 248 | // p[1] = (mode_julia, max_iter, escape_r2, grad_curve) | ||
| 249 | // p[2] = (julia_cr, julia_ci, r_rep, g_rep) | ||
| 250 | // p[3] = (b_rep, r_phase, g_phase, b_phase) | ||
| 251 | // p[4] = (has_lut, blend, has_mask, mask_inv) | ||
| 252 | // p[5] = tint_rgba | ||
| 253 | // p[6] = inside_rgba | ||
| 254 | @compute @workgroup_size(8, 8) | ||
| 255 | fn mandel_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 256 | let dims = textureDimensions(dst_tex); | ||
| 257 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 258 | |||
| 259 | let p = vec2<i32>(gid.xy); | ||
| 260 | let fdims = vec2<f32>(dims); | ||
| 261 | let fu = pix_to_fu(gid.xy, dims); | ||
| 262 | let aspect = fdims.x / max(fdims.y, 1.0); | ||
| 263 | |||
| 264 | let zoom = max(u.p[0].z, 1e-6); | ||
| 265 | let span_y = 2.6 / zoom; | ||
| 266 | let span_x = span_y * aspect; | ||
| 267 | let local = vec2<f32>((fu.x - 0.5) * span_x, (fu.y - 0.5) * span_y); | ||
| 268 | let ca = cos(u.p[0].w); | ||
| 269 | let sa = sin(u.p[0].w); | ||
| 270 | let rot = vec2<f32>(local.x * ca - local.y * sa, local.x * sa + local.y * ca); | ||
| 271 | let plane = vec2<f32>(u.p[0].x, u.p[0].y) + rot; | ||
| 272 | |||
| 273 | let is_julia = u.p[1].x > 0.5; | ||
| 274 | let max_iter = clamp(i32(round(u.p[1].y)), 1, 1024); | ||
| 275 | let escape_r2 = max(u.p[1].z, 16.0); | ||
| 276 | |||
| 277 | var z = select(vec2<f32>(0.0, 0.0), plane, is_julia); | ||
| 278 | let c = select(plane, vec2<f32>(u.p[2].x, u.p[2].y), is_julia); | ||
| 279 | |||
| 280 | var iter: i32 = 0; | ||
| 281 | var z2: f32 = dot(z, z); | ||
| 282 | for (var i: i32 = 0; i < 1024; i = i + 1) { | ||
| 283 | if (i >= max_iter || z2 > escape_r2) { | ||
| 284 | break; | ||
| 285 | } | ||
| 286 | let zx = z.x * z.x - z.y * z.y + c.x; | ||
| 287 | let zy = 2.0 * z.x * z.y + c.y; | ||
| 288 | z = vec2<f32>(zx, zy); | ||
| 289 | z2 = dot(z, z); | ||
| 290 | iter = iter + 1; | ||
| 291 | } | ||
| 292 | |||
| 293 | var col: vec4<f32>; | ||
| 294 | if (z2 <= escape_r2) { | ||
| 295 | col = u.p[6]; | ||
| 296 | } else { | ||
| 297 | // Smooth continuous escape-time coloring: iter + 1 - log2(log2(|z|)) | ||
| 298 | let mag = max(sqrt(z2), 2.0001); | ||
| 299 | let log_zn = log2(mag); | ||
| 300 | let nu = max(f32(iter) + 1.0 - log2(max(log_zn, 1.0001)), 0.0); | ||
| 301 | let curve = max(u.p[1].w, 0.05); | ||
| 302 | let norm_t = pow(clamp(nu / max(f32(max_iter), 1.0), 0.0, 1.0), curve); | ||
| 303 | |||
| 304 | if (u.p[4].x > 0.5) { | ||
| 305 | let lut_u = clamp(norm_t, 0.0, 1.0); | ||
| 306 | col = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u, 0.5), 0.0); | ||
| 307 | } else { | ||
| 308 | let r_rep = u.p[2].z; | ||
| 309 | let g_rep = u.p[2].w; | ||
| 310 | let b_rep = u.p[3].x; | ||
| 311 | let r_ph = u.p[3].y; | ||
| 312 | let g_ph = u.p[3].z; | ||
| 313 | let b_ph = u.p[3].w; | ||
| 314 | let r = 0.5 - 0.5 * cos(TAU * (norm_t * r_rep + r_ph)); | ||
| 315 | let g = 0.5 - 0.5 * cos(TAU * (norm_t * g_rep + g_ph)); | ||
| 316 | let b = 0.5 - 0.5 * cos(TAU * (norm_t * b_rep + b_ph)); | ||
| 317 | col = vec4<f32>(r, g, b, 1.0) * u.p[5]; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | let m = read_mask(gid.xy, dims, u.p[4].z, u.p[4].w); | ||
| 322 | let eff = clamp(u.p[4].y * m, 0.0, 1.0); | ||
| 323 | store_dst(p, col * eff); | ||
| 324 | } | ||
| 325 | |||
| 326 | // ============================================================================ | ||
| 327 | // 3. UnsharpMask: Thresholded high-frequency boost | ||
| 328 | // ============================================================================ | ||
| 329 | // src_a = original, src_b = Gaussian-blurred, mask_tex = EffectMask | ||
| 330 | // p[0] = (gain, threshold, blend, has_mask) | ||
| 331 | // p[1] = (mask_inv, mbm, 0, 0) | ||
| 332 | // p[2] = process_skip (r, g, b, a) | ||
| 333 | @compute @workgroup_size(8, 8) | ||
| 334 | fn unsharp_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 335 | let dims = textureDimensions(dst_tex); | ||
| 336 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 337 | |||
| 338 | let p = vec2<i32>(gid.xy); | ||
| 339 | let orig = textureLoad(src_a, p, 0); | ||
| 340 | let blur = textureLoad(src_b, p, 0); | ||
| 341 | let gain = u.p[0].x; | ||
| 342 | let thresh = max(u.p[0].y, 0.0); | ||
| 343 | |||
| 344 | let diff = orig - blur; | ||
| 345 | let mask_pass = vec4<f32>( | ||
| 346 | select(0.0, 1.0, abs(diff.r) > thresh), | ||
| 347 | select(0.0, 1.0, abs(diff.g) > thresh), | ||
| 348 | select(0.0, 1.0, abs(diff.b) > thresh), | ||
| 349 | select(0.0, 1.0, abs(diff.a) > thresh), | ||
| 350 | ); | ||
| 351 | let boosted = orig + gain * mask_pass * diff; | ||
| 352 | |||
| 353 | let m = read_mask(gid.xy, dims, u.p[0].w, u.p[1].x); | ||
| 354 | let mbm = u.p[1].y; | ||
| 355 | let eff = select(clamp(u.p[0].z * m, 0.0, 1.0), clamp(u.p[0].z, 0.0, 1.0), mbm > 0.5); | ||
| 356 | let out_c = apply_channel_blend(orig, boosted, eff, m, mbm, u.p[2]); | ||
| 357 | store_dst(p, out_c); | ||
| 358 | } | ||
| 359 | |||
| 360 | // ============================================================================ | ||
| 361 | // 4. Letterbox: Aspect-preserving Letterbox (0) & Pan-and-Scan (1) | ||
| 362 | // ============================================================================ | ||
| 363 | // src_a = source image | ||
| 364 | // p[0] = (mode, position, filter_method, pixel_aspect_ratio) | ||
| 365 | fn catmull_rom_w(t: f32) -> vec4<f32> { | ||
| 366 | let t2 = t * t; | ||
| 367 | let t3 = t2 * t; | ||
| 368 | return 0.5 * vec4<f32>( | ||
| 369 | -t + 2.0 * t2 - t3, | ||
| 370 | 2.0 - 5.0 * t2 + 3.0 * t3, | ||
| 371 | t + 4.0 * t2 - 3.0 * t3, | ||
| 372 | -t2 + t3, | ||
| 373 | ); | ||
| 374 | } | ||
| 375 | |||
| 376 | fn sample_bicubic_clamped(uv: vec2<f32>) -> vec4<f32> { | ||
| 377 | let sdims = textureDimensions(src_a); | ||
| 378 | let w = i32(sdims.x); | ||
| 379 | let h = i32(sdims.y); | ||
| 380 | let fc = uv * vec2<f32>(sdims) - 0.5; | ||
| 381 | let base = vec2<i32>(floor(fc)); | ||
| 382 | let f = fc - vec2<f32>(base); | ||
| 383 | let wx = catmull_rom_w(f.x); | ||
| 384 | let wy = catmull_rom_w(f.y); | ||
| 385 | var acc = vec4<f32>(0.0); | ||
| 386 | for (var j: i32 = 0; j < 4; j = j + 1) { | ||
| 387 | let sy = clamp(base.y + j - 1, 0, max(h - 1, 0)); | ||
| 388 | var row = vec4<f32>(0.0); | ||
| 389 | for (var i: i32 = 0; i < 4; i = i + 1) { | ||
| 390 | let sx = clamp(base.x + i - 1, 0, max(w - 1, 0)); | ||
| 391 | row = row + textureLoad(src_a, vec2<i32>(sx, sy), 0) * wx[i]; | ||
| 392 | } | ||
| 393 | acc = acc + row * wy[j]; | ||
| 394 | } | ||
| 395 | return acc; | ||
| 396 | } | ||
| 397 | |||
| 398 | @compute @workgroup_size(8, 8) | ||
| 399 | fn letterbox_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 400 | let dims = textureDimensions(dst_tex); | ||
| 401 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 402 | |||
| 403 | let p = vec2<i32>(gid.xy); | ||
| 404 | let sdims = textureDimensions(src_a); | ||
| 405 | let src_aspect = f32(sdims.x) / max(f32(sdims.y), 1.0); | ||
| 406 | let pa = max(u.p[0].w, 1e-4); | ||
| 407 | let dst_aspect = (f32(dims.x) * pa) / max(f32(dims.y), 1.0); | ||
| 408 | |||
| 409 | let mode = i32(round(u.p[0].x)); // 0 = Letterbox, 1 = PanScan | ||
| 410 | let pos = clamp(u.p[0].y, -1.0, 1.0); | ||
| 411 | let filt_mode = i32(round(u.p[0].z)); | ||
| 412 | let fu = pix_to_fu(gid.xy, dims); | ||
| 413 | |||
| 414 | var src_fu = fu; | ||
| 415 | if (mode == 0) { | ||
| 416 | // Letterbox: fit entire source inside target, adding crisp black bars. | ||
| 417 | if (src_aspect > dst_aspect + 1e-5) { | ||
| 418 | let h_frac = dst_aspect / src_aspect; | ||
| 419 | let y0 = (1.0 - h_frac) * 0.5 * (1.0 + pos); | ||
| 420 | src_fu.y = (fu.y - y0) / max(h_frac, 1e-5); | ||
| 421 | } else if (src_aspect < dst_aspect - 1e-5) { | ||
| 422 | let w_frac = src_aspect / dst_aspect; | ||
| 423 | let x0 = (1.0 - w_frac) * 0.5 * (1.0 + pos); | ||
| 424 | src_fu.x = (fu.x - x0) / max(w_frac, 1e-5); | ||
| 425 | } | ||
| 426 | if (src_fu.x < 0.0 || src_fu.x > 1.0 || src_fu.y < 0.0 || src_fu.y > 1.0) { | ||
| 427 | store_dst(p, vec4<f32>(0.0, 0.0, 0.0, 0.0)); | ||
| 428 | return; | ||
| 429 | } | ||
| 430 | } else { | ||
| 431 | // PanScan: fill target completely by cropping oversized source axis. | ||
| 432 | if (src_aspect > dst_aspect + 1e-5) { | ||
| 433 | let w_vis = dst_aspect / src_aspect; | ||
| 434 | let x0 = (1.0 - w_vis) * 0.5 * (1.0 + pos); | ||
| 435 | src_fu.x = x0 + fu.x * w_vis; | ||
| 436 | } else if (src_aspect < dst_aspect - 1e-5) { | ||
| 437 | let h_vis = src_aspect / dst_aspect; | ||
| 438 | let y0 = (1.0 - h_vis) * 0.5 * (1.0 + pos); | ||
| 439 | src_fu.y = y0 + fu.y * h_vis; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | let uv = clamp(vec2<f32>(src_fu.x, 1.0 - src_fu.y), vec2<f32>(0.0), vec2<f32>(1.0)); | ||
| 444 | var col: vec4<f32>; | ||
| 445 | if (filt_mode == 0) { | ||
| 446 | let sx = clamp(i32(floor(uv.x * f32(sdims.x))), 0, max(i32(sdims.x) - 1, 0)); | ||
| 447 | let sy = clamp(i32(floor(uv.y * f32(sdims.y))), 0, max(i32(sdims.y) - 1, 0)); | ||
| 448 | col = textureLoad(src_a, vec2<i32>(sx, sy), 0); | ||
| 449 | } else if (filt_mode >= 3) { | ||
| 450 | col = sample_bicubic_clamped(uv); | ||
| 451 | } else { | ||
| 452 | col = textureSampleLevel(src_a, samp_lin, uv, 0.0); | ||
| 453 | } | ||
| 454 | store_dst(p, col); | ||
| 455 | } | ||
| 456 | |||
| 457 | // ============================================================================ | ||
| 458 | // 5. Fuse.Wipe: Directional / Radial / Iris / Box transition wipe | ||
| 459 | // ============================================================================ | ||
| 460 | // src_a = Input1/Background, src_b = Input2/Foreground, mask_tex = EffectMask | ||
| 461 | // p[0] = (progress, angle_rad, softness, wipe_type) | ||
| 462 | // p[1] = (center_x, center_y, invert, has_fg) | ||
| 463 | // p[2] = (blend, has_mask, mask_inv, mbm) | ||
| 464 | // p[3] = process_skip (r, g, b, a) | ||
| 465 | @compute @workgroup_size(8, 8) | ||
| 466 | fn wipe_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 467 | let dims = textureDimensions(dst_tex); | ||
| 468 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 469 | |||
| 470 | let p = vec2<i32>(gid.xy); | ||
| 471 | let fdims = vec2<f32>(dims); | ||
| 472 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | ||
| 473 | let fu = pix_to_fu(gid.xy, dims); | ||
| 474 | |||
| 475 | let bg = textureLoad(src_a, p, 0); | ||
| 476 | let fg = select(vec4<f32>(0.0), textureSampleLevel(src_b, samp_lin, uv, 0.0), u.p[1].w > 0.5); | ||
| 477 | |||
| 478 | let progress = clamp(u.p[0].x, 0.0, 1.0); | ||
| 479 | let angle = u.p[0].y; | ||
| 480 | let softness = max(u.p[0].z, 0.0); | ||
| 481 | let wipe_type = i32(round(u.p[0].w)); | ||
| 482 | let center = vec2<f32>(u.p[1].x, u.p[1].y); | ||
| 483 | |||
| 484 | var s: f32 = 0.0; | ||
| 485 | if (wipe_type == 1) { | ||
| 486 | // Radial / Clock sweep around center | ||
| 487 | let d = fu - center; | ||
| 488 | let a = atan2(d.y, d.x) - angle; | ||
| 489 | s = fract(a / TAU); | ||
| 490 | } else if (wipe_type == 2) { | ||
| 491 | // Iris / Circular wipe (aspect-corrected) | ||
| 492 | let aspect = fdims.x / max(fdims.y, 1.0); | ||
| 493 | let d = vec2<f32>((fu.x - center.x) * aspect, fu.y - center.y); | ||
| 494 | let max_dx = max(center.x, 1.0 - center.x) * aspect; | ||
| 495 | let max_dy = max(center.y, 1.0 - center.y); | ||
| 496 | let max_r = max(sqrt(max_dx * max_dx + max_dy * max_dy), 1e-4); | ||
| 497 | s = clamp(length(d) / max_r, 0.0, 1.0); | ||
| 498 | } else if (wipe_type == 3) { | ||
| 499 | // Box / Rectangular wipe from center | ||
| 500 | let dx = abs(fu.x - center.x) / max(max(center.x, 1.0 - center.x), 1e-4); | ||
| 501 | let dy = abs(fu.y - center.y) / max(max(center.y, 1.0 - center.y), 1e-4); | ||
| 502 | s = clamp(max(dx, dy), 0.0, 1.0); | ||
| 503 | } else { | ||
| 504 | // Linear / Directional wipe along angle | ||
| 505 | let dir = vec2<f32>(cos(angle), sin(angle)); | ||
| 506 | let half_ext = max(0.5 * (abs(dir.x) + abs(dir.y)), 1e-4); | ||
| 507 | let proj = dot(fu - center, dir) / half_ext; | ||
| 508 | s = clamp(proj * 0.5 + 0.5, 0.0, 1.0); | ||
| 509 | } | ||
| 510 | |||
| 511 | let half_soft = 0.5 * softness; | ||
| 512 | let edge = progress * (1.0 + softness) - half_soft; | ||
| 513 | var w: f32; | ||
| 514 | if (progress <= 0.0) { | ||
| 515 | w = 0.0; | ||
| 516 | } else if (progress >= 1.0) { | ||
| 517 | w = 1.0; | ||
| 518 | } else { | ||
| 519 | w = 1.0 - safe_smoothstep(edge - half_soft, edge + half_soft, s); | ||
| 520 | } | ||
| 521 | if (u.p[1].z > 0.5) { | ||
| 522 | w = 1.0 - w; | ||
| 523 | } | ||
| 524 | |||
| 525 | let wiped = mix(bg, fg, w); | ||
| 526 | let m = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z); | ||
| 527 | let mbm = u.p[2].w; | ||
| 528 | let eff = select(clamp(u.p[2].x * m, 0.0, 1.0), clamp(u.p[2].x, 0.0, 1.0), mbm > 0.5); | ||
| 529 | let out_c = apply_channel_blend(bg, wiped, eff, m, mbm, u.p[3]); | ||
| 530 | store_dst(p, out_c); | ||
| 531 | } | ||
| 532 | |||
| 533 | // ============================================================================ | ||
| 534 | // 6. Fuse.Linger: Deterministic bounded-lookback temporal persistence step | ||
| 535 | // ============================================================================ | ||
| 536 | // src_a = running accum, src_b = past/next frame | ||
| 537 | // p[0] = (mode, alpha_or_decay_pow, decay, 0) | ||
| 538 | @compute @workgroup_size(8, 8) | ||
| 539 | fn linger_step_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 540 | let dims = textureDimensions(dst_tex); | ||
| 541 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 542 | |||
| 543 | let p = vec2<i32>(gid.xy); | ||
| 544 | let uv = (vec2<f32>(gid.xy) + 0.5) / vec2<f32>(dims); | ||
| 545 | let acc = textureLoad(src_a, p, 0); | ||
| 546 | let frm = textureSampleLevel(src_b, samp_lin, uv, 0.0); | ||
| 547 | let mode = i32(round(u.p[0].x)); | ||
| 548 | let factor = u.p[0].y; | ||
| 549 | let decay = u.p[0].z; | ||
| 550 | |||
| 551 | var res: vec4<f32>; | ||
| 552 | if (mode == 1) { | ||
| 553 | // Lighten / Max | ||
| 554 | res = max(acc, frm * factor); | ||
| 555 | } else if (mode == 2) { | ||
| 556 | // Premultiplied Over (current frame over decayed history) | ||
| 557 | res = frm + acc * decay * (1.0 - clamp(frm.a, 0.0, 1.0)); | ||
| 558 | } else if (mode == 3) { | ||
| 559 | // Additive | ||
| 560 | res = acc + frm * factor; | ||
| 561 | } else { | ||
| 562 | // Normalized exponential decay running average (default) | ||
| 563 | res = mix(acc, frm, factor); | ||
| 564 | } | ||
| 565 | store_dst(p, res); | ||
| 566 | } | ||
| 567 | |||
| 568 | // ============================================================================ | ||
| 569 | // 7. Fuse.OCLRays: Volumetric radial light shafts / god-rays | ||
| 570 | // ============================================================================ | ||
| 571 | // src_a = source image, mask_tex = EffectMask | ||
| 572 | // p[0] = (center_x, center_y, threshold, decay) | ||
| 573 | // p[1] = (weight, exposure, samples, density) | ||
| 574 | // p[2] = color_tint (r, g, b, a) | ||
| 575 | // p[3] = (blend, has_mask, mask_inv, mbm) | ||
| 576 | // p[4] = (rays_only, 0, 0, 0) | ||
| 577 | // p[5] = process_skip (r, g, b, a) | ||
| 578 | @compute @workgroup_size(8, 8) | ||
| 579 | fn ocl_rays_op(@builtin(global_invocation_id) gid: vec3<u32>) { | ||
| 580 | let dims = textureDimensions(dst_tex); | ||
| 581 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } | ||
| 582 | |||
| 583 | let p = vec2<i32>(gid.xy); | ||
| 584 | let fdims = vec2<f32>(dims); | ||
| 585 | let uv = (vec2<f32>(gid.xy) + 0.5) / fdims; | ||
| 586 | let orig = textureLoad(src_a, p, 0); | ||
| 587 | |||
| 588 | let center_uv = vec2<f32>(u.p[0].x, 1.0 - u.p[0].y); | ||
| 589 | let threshold = max(u.p[0].z, 0.0); | ||
| 590 | let decay = clamp(u.p[0].w, 0.0, 0.9999); | ||
| 591 | let weight = max(u.p[1].x, 0.0); | ||
| 592 | let exposure = max(u.p[1].y, 0.0); | ||
| 593 | let samples = clamp(i32(round(u.p[1].z)), 1, 128); | ||
| 594 | let density = clamp(u.p[1].w, 0.0, 2.0); | ||
| 595 | let tint = u.p[2]; | ||
| 596 | |||
| 597 | // Step attenuation normalized to 64 reference steps so ray length is consistent across sample counts. | ||
| 598 | let step_decay = pow(max(1.0 - decay, 1e-4), 64.0 / f32(samples)); | ||
| 599 | let delta_uv = (uv - center_uv) * (density / f32(samples)); | ||
| 600 | |||
| 601 | var cur_uv = uv; | ||
| 602 | var illum = 1.0; | ||
| 603 | var accum = vec4<f32>(0.0); | ||
| 604 | for (var i: i32 = 0; i < 128; i = i + 1) { | ||
| 605 | if (i >= samples) { | ||
| 606 | break; | ||
| 607 | } | ||
| 608 | cur_uv = cur_uv - delta_uv; | ||
| 609 | if (cur_uv.x >= 0.0 && cur_uv.x <= 1.0 && cur_uv.y >= 0.0 && cur_uv.y <= 1.0) { | ||
| 610 | let s = textureSampleLevel(src_a, samp_lin, cur_uv, 0.0); | ||
| 611 | let luma = dot(s.rgb, vec3<f32>(0.299, 0.587, 0.114)); | ||
| 612 | var bright = s; | ||
| 613 | if (threshold > 1e-5) { | ||
| 614 | let scale = select(0.0, (luma - threshold) / max(luma, 1e-5), luma > threshold); | ||
| 615 | bright = s * scale; | ||
| 616 | } | ||
| 617 | accum = accum + bright * illum; | ||
| 618 | } | ||
| 619 | illum = illum * step_decay; | ||
| 620 | } | ||
| 621 | |||
| 622 | let gain = (weight / f32(samples)) * exposure; | ||
| 623 | let rays_rgb = accum.rgb * gain * tint.rgb; | ||
| 624 | let rays_a = clamp(accum.a * gain * tint.a, 0.0, 1.0); | ||
| 625 | |||
| 626 | var effect: vec4<f32>; | ||
| 627 | if (u.p[4].x > 0.5) { | ||
| 628 | effect = vec4<f32>(rays_rgb, rays_a); | ||
| 629 | } else { | ||
| 630 | effect = vec4<f32>( | ||
| 631 | orig.rgb + rays_rgb, | ||
| 632 | clamp(orig.a + rays_a * (1.0 - clamp(orig.a, 0.0, 1.0)), 0.0, 1.0), | ||
| 633 | ); | ||
| 634 | } | ||
| 635 | |||
| 636 | let m = read_mask(gid.xy, dims, u.p[3].y, u.p[3].z); | ||
| 637 | let mbm = u.p[3].w; | ||
| 638 | let eff = select(clamp(u.p[3].x * m, 0.0, 1.0), clamp(u.p[3].x, 0.0, 1.0), mbm > 0.5); | ||
| 639 | let out_c = apply_channel_blend(orig, effect, eff, m, mbm, u.p[5]); | ||
| 640 | store_dst(p, out_c); | ||
| 641 | } | ||