| 1 | //! Wave 37 GPU pipelines & evaluators — High-Frequency Archive Fuses & Classic Built-in FX: |
| 2 | //! 1. `Fuse.Hexagon` — 3-color regular hexagonal lattice generator |
| 3 | //! 2. `Fuse.Lightning` — Procedural jagged fractal lightning bolt with forks & glow |
| 4 | //! 3. `Fuse.VORONOI_DIAGRAM` — Exact Euclidean Voronoi cells, edges & N-gon sites |
| 5 | //! 4. `Defocus` — Optical circular / polygonal N-gon bokeh blur with highlight bloom |
| 6 | //! 5. `AutoGain` — Pure-GPU 2-pass min/max reduction + auto-levels stretch |
| 7 | //! 6. `ColorBalance` — 3-way Shadows/Midtones/Highlights RGB balance + PreserveLuminance |
| 8 | //! 7. `SoftClip` — C1-smooth hyperbolic knee soft-clipping (HDR-safe) |
| 9 | //! 8. `Grain` — Classic Fusion RGB film grain |
| 10 | //! |
| 11 | //! WebGPU safety: all texture inputs use `TextureSampleType::Float { filterable: false }` |
| 12 | //! (read exclusively via `textureLoad`), outputs are `WriteOnly` storage textures, and |
| 13 | //! uniform slots grow in 512-slot chunks reset via `Engine::reset_uniform_cursors()`. |
| 14 | |
| 15 | #[cfg(test)] |
| 16 | mod tests; |
| 17 | |
| 18 | use super::*; |
| 19 | use std::num::NonZeroU64; |
| 20 | |
| 21 | const ARCHIVE_FX2_WGSL: &str = include_str!("../shaders/archive_fx2.wgsl"); |
| 22 | const FX2_UNIFORM_SLOTS_PER_CHUNK: u32 = 512; |
| 23 | |
| 24 | fn build_fx2_wgsl(format: wgpu::TextureFormat, hdr_float: bool) -> String { |
| 25 | let storage_fmt = match format { |
| 26 | wgpu::TextureFormat::Rgba16Float => "rgba16float", |
| 27 | wgpu::TextureFormat::Rgba32Float => "rgba32float", |
| 28 | _ => "rgba8unorm", |
| 29 | }; |
| 30 | let mut src = ARCHIVE_FX2_WGSL.to_owned(); |
| 31 | if storage_fmt != "rgba8unorm" { |
| 32 | src = src.replace( |
| 33 | "texture_storage_2d<rgba8unorm, write>", |
| 34 | &format!("texture_storage_2d<{storage_fmt}, write>"), |
| 35 | ); |
| 36 | } |
| 37 | if hdr_float { |
| 38 | src = src.replace("const HDR_FLOAT: bool = false;", "const HDR_FLOAT: bool = true;"); |
| 39 | } |
| 40 | src |
| 41 | } |
| 42 | |
| 43 | fn unfilterable_tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { |
| 44 | wgpu::BindGroupLayoutEntry { |
| 45 | binding, |
| 46 | visibility: wgpu::ShaderStages::COMPUTE, |
| 47 | ty: wgpu::BindingType::Texture { |
| 48 | sample_type: wgpu::TextureSampleType::Float { filterable: false }, |
| 49 | view_dimension: wgpu::TextureViewDimension::D2, |
| 50 | multisampled: false, |
| 51 | }, |
| 52 | count: None, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[derive(Clone, Copy)] |
| 57 | enum ArchiveFx2Op { |
| 58 | FuseHexagon, |
| 59 | FuseLightning, |
| 60 | FuseVoronoi, |
| 61 | Defocus, |
| 62 | AutoGainReduce1, |
| 63 | AutoGainReduce2, |
| 64 | AutoGainApply, |
| 65 | ColorBalance, |
| 66 | SoftClip, |
| 67 | Grain, |
| 68 | } |
| 69 | |
| 70 | struct ArchiveFx2FormatPipelines { |
| 71 | bgl: wgpu::BindGroupLayout, |
| 72 | fuse_hexagon: wgpu::ComputePipeline, |
| 73 | fuse_lightning: wgpu::ComputePipeline, |
| 74 | fuse_voronoi: wgpu::ComputePipeline, |
| 75 | defocus: wgpu::ComputePipeline, |
| 76 | autogain_reduce1: wgpu::ComputePipeline, |
| 77 | autogain_reduce2: wgpu::ComputePipeline, |
| 78 | autogain_apply: wgpu::ComputePipeline, |
| 79 | color_balance: wgpu::ComputePipeline, |
| 80 | soft_clip: wgpu::ComputePipeline, |
| 81 | grain: wgpu::ComputePipeline, |
| 82 | } |
| 83 | |
| 84 | impl ArchiveFx2FormatPipelines { |
| 85 | fn pipeline(&self, op: ArchiveFx2Op) -> &wgpu::ComputePipeline { |
| 86 | match op { |
| 87 | ArchiveFx2Op::FuseHexagon => &self.fuse_hexagon, |
| 88 | ArchiveFx2Op::FuseLightning => &self.fuse_lightning, |
| 89 | ArchiveFx2Op::FuseVoronoi => &self.fuse_voronoi, |
| 90 | ArchiveFx2Op::Defocus => &self.defocus, |
| 91 | ArchiveFx2Op::AutoGainReduce1 => &self.autogain_reduce1, |
| 92 | ArchiveFx2Op::AutoGainReduce2 => &self.autogain_reduce2, |
| 93 | ArchiveFx2Op::AutoGainApply => &self.autogain_apply, |
| 94 | ArchiveFx2Op::ColorBalance => &self.color_balance, |
| 95 | ArchiveFx2Op::SoftClip => &self.soft_clip, |
| 96 | ArchiveFx2Op::Grain => &self.grain, |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | pub(super) struct ArchiveFx2Pipelines { |
| 102 | uniform_stride: u32, |
| 103 | uniform_chunks: Vec<wgpu::Buffer>, |
| 104 | uniform_cursor: u32, |
| 105 | by_format: HashMap<(wgpu::TextureFormat, bool), ArchiveFx2FormatPipelines>, |
| 106 | } |
| 107 | |
| 108 | impl ArchiveFx2Pipelines { |
| 109 | pub(super) fn new(device: &wgpu::Device) -> Self { |
| 110 | let uniform_size = std::mem::size_of::<UniformParams>() as u32; |
| 111 | let min_align = device.limits().min_uniform_buffer_offset_alignment.max(1); |
| 112 | let uniform_stride = uniform_size.div_ceil(min_align) * min_align; |
| 113 | Self { |
| 114 | uniform_stride, |
| 115 | uniform_chunks: Vec::new(), |
| 116 | uniform_cursor: 0, |
| 117 | by_format: HashMap::new(), |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub(super) fn reset_uniform_cursor(&mut self) { |
| 122 | self.uniform_cursor = 0; |
| 123 | } |
| 124 | |
| 125 | pub(super) fn trim_uniform_chunks(&mut self, max_chunks: usize) { |
| 126 | self.uniform_cursor = 0; |
| 127 | if self.uniform_chunks.len() > max_chunks { |
| 128 | self.uniform_chunks.truncate(max_chunks); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn alloc_uniform_slot( |
| 133 | &mut self, |
| 134 | device: &wgpu::Device, |
| 135 | queue: &wgpu::Queue, |
| 136 | params: &UniformParams, |
| 137 | ) -> (usize, u32) { |
| 138 | let slot_global = self.uniform_cursor; |
| 139 | let chunk_idx = (slot_global / FX2_UNIFORM_SLOTS_PER_CHUNK) as usize; |
| 140 | let slot_in_chunk = slot_global % FX2_UNIFORM_SLOTS_PER_CHUNK; |
| 141 | while chunk_idx >= self.uniform_chunks.len() { |
| 142 | let size = u64::from(FX2_UNIFORM_SLOTS_PER_CHUNK) * u64::from(self.uniform_stride); |
| 143 | let buf = device.create_buffer(&wgpu::BufferDescriptor { |
| 144 | label: Some("archive-fx2-params-ring"), |
| 145 | size, |
| 146 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| 147 | mapped_at_creation: false, |
| 148 | }); |
| 149 | self.uniform_chunks.push(buf); |
| 150 | } |
| 151 | let offset = slot_in_chunk * self.uniform_stride; |
| 152 | queue.write_buffer(&self.uniform_chunks[chunk_idx], u64::from(offset), bytemuck::bytes_of(params)); |
| 153 | self.uniform_cursor = slot_global + 1; |
| 154 | (chunk_idx, offset) |
| 155 | } |
| 156 | |
| 157 | fn ensure_format(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat, hdr_float: bool) { |
| 158 | if self.by_format.contains_key(&(format, hdr_float)) { |
| 159 | return; |
| 160 | } |
| 161 | let u_size = std::mem::size_of::<UniformParams>() as u64; |
| 162 | let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 163 | label: Some("archive-fx2-bgl"), |
| 164 | entries: &[ |
| 165 | unfilterable_tex_entry(0), |
| 166 | unfilterable_tex_entry(1), |
| 167 | unfilterable_tex_entry(2), |
| 168 | wgpu::BindGroupLayoutEntry { |
| 169 | binding: 3, |
| 170 | visibility: wgpu::ShaderStages::COMPUTE, |
| 171 | ty: wgpu::BindingType::StorageTexture { |
| 172 | access: wgpu::StorageTextureAccess::WriteOnly, |
| 173 | format, |
| 174 | view_dimension: wgpu::TextureViewDimension::D2, |
| 175 | }, |
| 176 | count: None, |
| 177 | }, |
| 178 | wgpu::BindGroupLayoutEntry { |
| 179 | binding: 4, |
| 180 | visibility: wgpu::ShaderStages::COMPUTE, |
| 181 | ty: wgpu::BindingType::Buffer { |
| 182 | ty: wgpu::BufferBindingType::Uniform, |
| 183 | has_dynamic_offset: true, |
| 184 | min_binding_size: NonZeroU64::new(u_size), |
| 185 | }, |
| 186 | count: None, |
| 187 | }, |
| 188 | ], |
| 189 | }); |
| 190 | let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { |
| 191 | label: Some("archive-fx2-pl"), |
| 192 | bind_group_layouts: &[Some(&bgl)], |
| 193 | immediate_size: 0, |
| 194 | }); |
| 195 | let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { |
| 196 | label: Some("archive-fx2-wgsl"), |
| 197 | source: wgpu::ShaderSource::Wgsl(build_fx2_wgsl(format, hdr_float).into()), |
| 198 | }); |
| 199 | let mk = |entry: &str| { |
| 200 | device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { |
| 201 | label: Some(entry), |
| 202 | layout: Some(&pl), |
| 203 | module: &module, |
| 204 | entry_point: Some(entry), |
| 205 | compilation_options: Default::default(), |
| 206 | cache: None, |
| 207 | }) |
| 208 | }; |
| 209 | self.by_format.insert( |
| 210 | (format, hdr_float), |
| 211 | ArchiveFx2FormatPipelines { |
| 212 | bgl, |
| 213 | fuse_hexagon: mk("fuse_hexagon_op"), |
| 214 | fuse_lightning: mk("fuse_lightning_op"), |
| 215 | fuse_voronoi: mk("fuse_voronoi_op"), |
| 216 | defocus: mk("defocus_op"), |
| 217 | autogain_reduce1: mk("autogain_reduce1_op"), |
| 218 | autogain_reduce2: mk("autogain_reduce2_op"), |
| 219 | autogain_apply: mk("autogain_apply_op"), |
| 220 | color_balance: mk("color_balance_op"), |
| 221 | soft_clip: mk("soft_clip_op"), |
| 222 | grain: mk("grain_op"), |
| 223 | }, |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | #[allow(clippy::too_many_arguments)] |
| 228 | fn dispatch( |
| 229 | &mut self, |
| 230 | device: &wgpu::Device, |
| 231 | queue: &wgpu::Queue, |
| 232 | enc: &mut wgpu::CommandEncoder, |
| 233 | op: ArchiveFx2Op, |
| 234 | src_a: &wgpu::TextureView, |
| 235 | src_b: &wgpu::TextureView, |
| 236 | mask: &wgpu::TextureView, |
| 237 | dst: &CachedTexture, |
| 238 | params: &UniformParams, |
| 239 | ) { |
| 240 | let hdr_float = dst.depth.is_float(); |
| 241 | self.ensure_format(device, dst.format, hdr_float); |
| 242 | let (chunk_idx, offset) = self.alloc_uniform_slot(device, queue, params); |
| 243 | let fp = &self.by_format[&(dst.format, hdr_float)]; |
| 244 | let u_size = NonZeroU64::new(std::mem::size_of::<UniformParams>() as u64); |
| 245 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 246 | label: Some("archive-fx2-bg"), |
| 247 | layout: &fp.bgl, |
| 248 | entries: &[ |
| 249 | wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(src_a) }, |
| 250 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(src_b) }, |
| 251 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(mask) }, |
| 252 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(&dst.view) }, |
| 253 | wgpu::BindGroupEntry { |
| 254 | binding: 4, |
| 255 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| 256 | buffer: &self.uniform_chunks[chunk_idx], |
| 257 | offset: 0, |
| 258 | size: u_size, |
| 259 | }), |
| 260 | }, |
| 261 | ], |
| 262 | }); |
| 263 | let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { |
| 264 | label: Some("archive-fx2-pass"), |
| 265 | timestamp_writes: None, |
| 266 | }); |
| 267 | pass.set_pipeline(fp.pipeline(op)); |
| 268 | pass.set_bind_group(0, &bg, &[offset]); |
| 269 | pass.dispatch_workgroups(dst.size[0].div_ceil(8), dst.size[1].div_ceil(8), 1); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | fn num_any(comp: &Comp, node: &Node, keys: &[&str], time: f64, default: f64, expr: &ExprEngine) -> f64 { |
| 274 | keys.iter() |
| 275 | .find(|&&k| node.inputs.contains_key(k)) |
| 276 | .map_or(default, |&k| comp.eval_number(node, k, time, default, expr)) |
| 277 | } |
| 278 | |
| 279 | fn pt_any(comp: &Comp, node: &Node, keys: &[&str], time: f64, default: [f64; 2], expr: &ExprEngine) -> [f64; 2] { |
| 280 | keys.iter() |
| 281 | .find(|&&k| node.inputs.contains_key(k)) |
| 282 | .map_or(default, |&k| comp.eval_point(node, k, time, default, expr)) |
| 283 | } |
| 284 | |
| 285 | fn conn_any(comp: &Comp, node: &Node, ports: &[&str]) -> Option<NodeId> { |
| 286 | ports.iter().find_map(|p| node.connected_node(p).and_then(|s| comp.find_node(s))) |
| 287 | } |
| 288 | |
| 289 | fn str_any<'a>(node: &'a Node, keys: &[&str]) -> Option<&'a str> { |
| 290 | keys.iter().find_map(|k| node.static_str(k)) |
| 291 | } |
| 292 | |
| 293 | fn b2f(v: bool) -> f32 { |
| 294 | if v { 1.0 } else { 0.0 } |
| 295 | } |
| 296 | |
| 297 | impl Engine { |
| 298 | fn archive_fx2_pipelines(&self) -> &RefCell<ArchiveFx2Pipelines> { |
| 299 | self.archive_fx2_pipelines |
| 300 | .get_or_init(|| RefCell::new(ArchiveFx2Pipelines::new(&self.device))) |
| 301 | } |
| 302 | |
| 303 | #[allow(clippy::too_many_arguments)] |
| 304 | fn dispatch_fx2( |
| 305 | &self, |
| 306 | enc: &mut wgpu::CommandEncoder, |
| 307 | op: ArchiveFx2Op, |
| 308 | src_a: &wgpu::TextureView, |
| 309 | src_b: &wgpu::TextureView, |
| 310 | mask: &wgpu::TextureView, |
| 311 | dst: &CachedTexture, |
| 312 | params: &UniformParams, |
| 313 | ) { |
| 314 | self.archive_fx2_pipelines() |
| 315 | .borrow_mut() |
| 316 | .dispatch(&self.device, &self.queue, enc, op, src_a, src_b, mask, dst, params); |
| 317 | } |
| 318 | |
| 319 | fn fx2_gen_size(&self, comp: &Comp, node: &Node, time: f64, comp_dims: [u32; 2]) -> [u32; 2] { |
| 320 | let use_ff = comp.eval_number(node, "UseFrameFormatSettings", time, 0.0, &self.expr) > 0.5; |
| 321 | let w = if use_ff { |
| 322 | comp.width |
| 323 | } else { |
| 324 | comp.eval_number(node, "Width", time, comp.width as f64, &self.expr).round().max(1.0) as u32 |
| 325 | }; |
| 326 | let h = if use_ff { |
| 327 | comp.height |
| 328 | } else { |
| 329 | comp.eval_number(node, "Height", time, comp.height as f64, &self.expr).round().max(1.0) as u32 |
| 330 | }; |
| 331 | if w == comp.width && h == comp.height { comp_dims } else { self.scaled_dims(w, h) } |
| 332 | } |
| 333 | |
| 334 | /// Evaluates single-input filter pass with cache lookup, aux preservation, and `ArchiveFx2Op` dispatch. |
| 335 | #[allow(clippy::too_many_arguments)] |
| 336 | fn run_fx2_filter( |
| 337 | &mut self, |
| 338 | node: &Node, |
| 339 | tag: &str, |
| 340 | op: ArchiveFx2Op, |
| 341 | src: &CachedTexture, |
| 342 | src_b: &wgpu::TextureView, |
| 343 | mask: Option<&CachedTexture>, |
| 344 | dst_depth: BitDepth, |
| 345 | u: &UniformParams, |
| 346 | u_vecs: usize, |
| 347 | frame_i: i64, |
| 348 | enc: &mut wgpu::CommandEncoder, |
| 349 | stats: &mut EvalStats, |
| 350 | ) -> CachedTexture { |
| 351 | let mask_hash = mask.map_or(0, |m| m.content_hash); |
| 352 | let key = self.make_key( |
| 353 | node, |
| 354 | tag, |
| 355 | src.size, |
| 356 | &[src.content_hash, mask_hash, dst_depth as u64], |
| 357 | bytemuck::cast_slice(&u.p[..u_vecs]), |
| 358 | ); |
| 359 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 360 | stats.cache_hits += 1; |
| 361 | return hit; |
| 362 | } |
| 363 | stats.cache_misses += 1; |
| 364 | stats.nodes_evaluated += 1; |
| 365 | let mut dst = self.alloc_tex(src.size, dst_depth, key); |
| 366 | let mv = mask.map_or(&self.dummy.view, |m| &m.view); |
| 367 | self.dispatch_fx2(enc, op, &src.view, src_b, mv, &dst, u); |
| 368 | dst.set_aux(src.aux.clone()); |
| 369 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 370 | dst |
| 371 | } |
| 372 | |
| 373 | // ------------------------------------------------------------------------ |
| 374 | // 1. Fuse.Hexagon |
| 375 | // ------------------------------------------------------------------------ |
| 376 | #[allow(clippy::too_many_arguments)] |
| 377 | pub(super) fn eval_fuse_hexagon( |
| 378 | &mut self, |
| 379 | comp: &Comp, |
| 380 | node: &Node, |
| 381 | time: f64, |
| 382 | frame_i: i64, |
| 383 | comp_dims: [u32; 2], |
| 384 | enc: &mut wgpu::CommandEncoder, |
| 385 | visiting: &mut HashSet<(NodeId, i64)>, |
| 386 | stats: &mut EvalStats, |
| 387 | ) -> CachedTexture { |
| 388 | let size = self.fx2_gen_size(comp, node, time, comp_dims); |
| 389 | let depth = self.node_depth(comp, node, time, None); |
| 390 | let hex_size = num_any(comp, node, &["Size", "HexSize", "Scale"], time, 10.0, &self.expr).max(0.1) as f32; |
| 391 | let border = num_any(comp, node, &["Border", "BorderSize"], time, 0.0, &self.expr).max(0.0) as f32; |
| 392 | let center = pt_any(comp, node, &["Center", "Offset"], time, [0.5, 0.5], &self.expr); |
| 393 | let angle = (num_any(comp, node, &["Angle", "Rotation"], time, 0.0, &self.expr) as f32).to_radians(); |
| 394 | let col = |i: u8, def: [f64; 3]| -> [f32; 4] { |
| 395 | [ |
| 396 | comp.eval_number(node, &format!("Red{i}"), time, def[0], &self.expr) as f32, |
| 397 | comp.eval_number(node, &format!("Green{i}"), time, def[1], &self.expr) as f32, |
| 398 | comp.eval_number(node, &format!("Blue{i}"), time, def[2], &self.expr) as f32, |
| 399 | comp.eval_number(node, &format!("Alpha{i}"), time, 1.0, &self.expr) as f32, |
| 400 | ] |
| 401 | }; |
| 402 | let (c1, c2, c3) = (col(1, [1.0, 1.0, 1.0]), col(2, [0.5, 0.5, 0.5]), col(3, [0.0, 0.0, 0.0])); |
| 403 | let br = num_any(comp, node, &["BorderRed", "LineRed"], time, 0.0, &self.expr) as f32; |
| 404 | let bg = num_any(comp, node, &["BorderGreen", "LineGreen"], time, 0.0, &self.expr) as f32; |
| 405 | let bb = num_any(comp, node, &["BorderBlue", "LineBlue"], time, 0.0, &self.expr) as f32; |
| 406 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 407 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 408 | let skip = self.process_skip(comp, node, time); |
| 409 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 410 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 411 | |
| 412 | let mut u = UniformParams::default(); |
| 413 | u.p[0] = [hex_size, border, center[0] as f32, center[1] as f32]; |
| 414 | u.p[1] = c1; |
| 415 | u.p[2] = c2; |
| 416 | u.p[3] = c3; |
| 417 | u.p[4] = [angle, blend, b2f(mask.is_some()), b2f(mask_inv)]; |
| 418 | u.p[5] = [b2f(mask.is_some() && mbm_flag), br, bg, bb]; |
| 419 | u.p[6] = skip; |
| 420 | |
| 421 | let key = self.make_key(node, "FuseHexagon", size, &[mask_hash, depth as u64], bytemuck::cast_slice(&u.p[..7])); |
| 422 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 423 | stats.cache_hits += 1; |
| 424 | return hit; |
| 425 | } |
| 426 | stats.cache_misses += 1; |
| 427 | stats.nodes_evaluated += 1; |
| 428 | let dst = self.alloc_tex(size, depth, key); |
| 429 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 430 | self.dispatch_fx2(enc, ArchiveFx2Op::FuseHexagon, &self.dummy.view, &self.dummy.view, mv, &dst, &u); |
| 431 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 432 | dst |
| 433 | } |
| 434 | |
| 435 | // ------------------------------------------------------------------------ |
| 436 | // 2. Fuse.Lightning |
| 437 | // ------------------------------------------------------------------------ |
| 438 | #[allow(clippy::too_many_arguments)] |
| 439 | pub(super) fn eval_fuse_lightning( |
| 440 | &mut self, |
| 441 | comp: &Comp, |
| 442 | node: &Node, |
| 443 | time: f64, |
| 444 | frame_i: i64, |
| 445 | comp_dims: [u32; 2], |
| 446 | enc: &mut wgpu::CommandEncoder, |
| 447 | visiting: &mut HashSet<(NodeId, i64)>, |
| 448 | stats: &mut EvalStats, |
| 449 | ) -> CachedTexture { |
| 450 | let src_opt = conn_any(comp, node, &["Input", "Background", "Image"]) |
| 451 | .map(|id| self.eval_node(comp, id, time, enc, visiting, stats)); |
| 452 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 453 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 454 | if blend <= 1e-6 && !mbm_flag { |
| 455 | if let Some(s) = src_opt { |
| 456 | return s; |
| 457 | } |
| 458 | } |
| 459 | let src_c = src_opt.map(|s| self.crop_to_canvas(enc, s)); |
| 460 | let size = src_c.as_ref().map_or_else(|| self.fx2_gen_size(comp, node, time, comp_dims), |s| s.size); |
| 461 | let depth = self.node_depth(comp, node, time, src_c.as_ref().map(|s| s.depth)); |
| 462 | |
| 463 | let start = pt_any(comp, node, &["StartPosition", "Start", "Point1"], time, [0.25, 0.5], &self.expr); |
| 464 | let end = pt_any(comp, node, &["EndPosition", "End", "Point2"], time, [0.75, 0.5], &self.expr); |
| 465 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 466 | let src_hash = src_c.as_ref().map_or(0, |s| s.content_hash); |
| 467 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 468 | |
| 469 | let mut u = UniformParams::default(); |
| 470 | u.p[0] = [start[0] as f32, start[1] as f32, end[0] as f32, end[1] as f32]; |
| 471 | u.p[1] = [ |
| 472 | num_any(comp, node, &["RandomSeed", "Seed"], time, 0.0, &self.expr) as f32, |
| 473 | num_any(comp, node, &["Detail", "Subdivisions"], time, 0.2, &self.expr).clamp(0.0, 2.0) as f32, |
| 474 | num_any(comp, node, &["Jagginess", "Jitter", "Deviation"], time, 0.2, &self.expr).max(0.0) as f32, |
| 475 | num_any(comp, node, &["Thickness", "Width", "BoltWidth"], time, 0.02, &self.expr).max(0.0005) as f32, |
| 476 | ]; |
| 477 | u.p[2] = [ |
| 478 | num_any(comp, node, &["ThicknessTapering", "Taper"], time, 0.5, &self.expr).clamp(0.0, 1.0) as f32, |
| 479 | num_any(comp, node, &["Softness", "Glow", "Blur"], time, 1.5, &self.expr).max(0.0) as f32, |
| 480 | num_any(comp, node, &["Crawling", "Phase", "Crawl"], time, 0.0, &self.expr) as f32, |
| 481 | num_any(comp, node, &["BranchProb", "BranchProbability", "Forks"], time, 0.2, &self.expr).clamp(0.0, 1.0) as f32, |
| 482 | ]; |
| 483 | u.p[3] = [ |
| 484 | num_any(comp, node, &["BranchLength", "ForkLength"], time, 0.15, &self.expr).max(0.0) as f32, |
| 485 | b2f(src_c.is_some()), |
| 486 | 0.0, |
| 487 | 0.0, |
| 488 | ]; |
| 489 | u.p[4] = [ |
| 490 | num_any(comp, node, &["Red", "GlowRed"], time, 0.45, &self.expr) as f32, |
| 491 | num_any(comp, node, &["Green", "GlowGreen"], time, 0.72, &self.expr) as f32, |
| 492 | num_any(comp, node, &["Blue", "GlowBlue"], time, 1.0, &self.expr) as f32, |
| 493 | num_any(comp, node, &["Alpha"], time, 1.0, &self.expr) as f32, |
| 494 | ]; |
| 495 | u.p[5] = [blend, b2f(mask.is_some()), b2f(mask_inv), b2f(mask.is_some() && mbm_flag)]; |
| 496 | u.p[6] = self.process_skip(comp, node, time); |
| 497 | |
| 498 | let key = self.make_key(node, "FuseLightning", size, &[src_hash, mask_hash, depth as u64], bytemuck::cast_slice(&u.p[..7])); |
| 499 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 500 | stats.cache_hits += 1; |
| 501 | return hit; |
| 502 | } |
| 503 | stats.cache_misses += 1; |
| 504 | stats.nodes_evaluated += 1; |
| 505 | let dst = self.alloc_tex(size, depth, key); |
| 506 | let sv = src_c.as_ref().map_or(&self.dummy.view, |s| &s.view); |
| 507 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 508 | self.dispatch_fx2(enc, ArchiveFx2Op::FuseLightning, sv, &self.dummy.view, mv, &dst, &u); |
| 509 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 510 | dst |
| 511 | } |
| 512 | |
| 513 | // ------------------------------------------------------------------------ |
| 514 | // 3. Fuse.VORONOI_DIAGRAM |
| 515 | // ------------------------------------------------------------------------ |
| 516 | #[allow(clippy::too_many_arguments)] |
| 517 | pub(super) fn eval_fuse_voronoi_diagram( |
| 518 | &mut self, |
| 519 | comp: &Comp, |
| 520 | node: &Node, |
| 521 | time: f64, |
| 522 | frame_i: i64, |
| 523 | comp_dims: [u32; 2], |
| 524 | enc: &mut wgpu::CommandEncoder, |
| 525 | visiting: &mut HashSet<(NodeId, i64)>, |
| 526 | stats: &mut EvalStats, |
| 527 | ) -> CachedTexture { |
| 528 | let src_opt = conn_any(comp, node, &["Input", "Background", "Image"]) |
| 529 | .map(|id| self.eval_node(comp, id, time, enc, visiting, stats)); |
| 530 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 531 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 532 | if blend <= 1e-6 && !mbm_flag { |
| 533 | if let Some(s) = src_opt { |
| 534 | return s; |
| 535 | } |
| 536 | } |
| 537 | let src_c = src_opt.map(|s| self.crop_to_canvas(enc, s)); |
| 538 | let size = src_c.as_ref().map_or_else(|| self.fx2_gen_size(comp, node, time, comp_dims), |s| s.size); |
| 539 | let depth = self.node_depth(comp, node, time, src_c.as_ref().map(|s| s.depth)); |
| 540 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 541 | let src_hash = src_c.as_ref().map_or(0, |s| s.content_hash); |
| 542 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 543 | |
| 544 | let mut u = UniformParams::default(); |
| 545 | u.p[0] = [ |
| 546 | num_any(comp, node, &["RandomSeed", "Seed"], time, 0.0, &self.expr) as f32, |
| 547 | num_any(comp, node, &["Cell_Number", "CellNumber", "Cells", "Count"], time, 16.0, &self.expr).clamp(2.0, 64.0) as f32, |
| 548 | num_any(comp, node, &["Sides", "PointSides"], time, 6.0, &self.expr).clamp(3.0, 32.0) as f32, |
| 549 | num_any(comp, node, &["Size", "LineSize", "Thickness"], time, 0.006, &self.expr).max(0.0005) as f32, |
| 550 | ]; |
| 551 | u.p[1] = [ |
| 552 | num_any(comp, node, &["Scale", "Zoom"], time, 1.0, &self.expr).max(0.05) as f32, |
| 553 | num_any(comp, node, &["img_Width", "ImgWidth"], time, size[0] as f64, &self.expr).max(1.0) as f32, |
| 554 | num_any(comp, node, &["img_Height", "ImgHeight"], time, size[1] as f64, &self.expr).max(1.0) as f32, |
| 555 | num_any(comp, node, &["Line_Type", "LineType", "Style"], time, 0.0, &self.expr).round().clamp(0.0, 3.0) as f32, |
| 556 | ]; |
| 557 | u.p[2] = [ |
| 558 | b2f(src_c.is_some()), |
| 559 | num_any(comp, node, &["ShowCells", "FillCells"], time, 0.0, &self.expr) as f32, |
| 560 | num_any(comp, node, &["ShowSites", "ShowPoints"], time, 0.0, &self.expr) as f32, |
| 561 | 1.0, |
| 562 | ]; |
| 563 | u.p[3] = [ |
| 564 | num_any(comp, node, &["Red", "LineRed"], time, 1.0, &self.expr) as f32, |
| 565 | num_any(comp, node, &["Green", "LineGreen"], time, 1.0, &self.expr) as f32, |
| 566 | num_any(comp, node, &["Blue", "LineBlue"], time, 1.0, &self.expr) as f32, |
| 567 | num_any(comp, node, &["Alpha", "LineAlpha"], time, 1.0, &self.expr) as f32, |
| 568 | ]; |
| 569 | u.p[4] = [blend, b2f(mask.is_some()), b2f(mask_inv), b2f(mask.is_some() && mbm_flag)]; |
| 570 | u.p[5] = self.process_skip(comp, node, time); |
| 571 | |
| 572 | let key = self.make_key(node, "FuseVoronoiDiagram", size, &[src_hash, mask_hash, depth as u64], bytemuck::cast_slice(&u.p[..6])); |
| 573 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 574 | stats.cache_hits += 1; |
| 575 | return hit; |
| 576 | } |
| 577 | stats.cache_misses += 1; |
| 578 | stats.nodes_evaluated += 1; |
| 579 | let dst = self.alloc_tex(size, depth, key); |
| 580 | let sv = src_c.as_ref().map_or(&self.dummy.view, |s| &s.view); |
| 581 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 582 | self.dispatch_fx2(enc, ArchiveFx2Op::FuseVoronoi, sv, &self.dummy.view, mv, &dst, &u); |
| 583 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 584 | dst |
| 585 | } |
| 586 | |
| 587 | // ------------------------------------------------------------------------ |
| 588 | // 4. Defocus |
| 589 | // ------------------------------------------------------------------------ |
| 590 | #[allow(clippy::too_many_arguments)] |
| 591 | pub(super) fn eval_defocus( |
| 592 | &mut self, |
| 593 | comp: &Comp, |
| 594 | node: &Node, |
| 595 | time: f64, |
| 596 | frame_i: i64, |
| 597 | enc: &mut wgpu::CommandEncoder, |
| 598 | visiting: &mut HashSet<(NodeId, i64)>, |
| 599 | stats: &mut EvalStats, |
| 600 | ) -> CachedTexture { |
| 601 | let Some(src_id) = conn_any(comp, node, &["Input", "Background", "Image"]) else { |
| 602 | return self.dummy.clone(); |
| 603 | }; |
| 604 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 605 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 606 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 607 | let raw_size = num_any(comp, node, &["DefocusSize", "Size", "BlurSize", "XBlurSize"], time, 2.0, &self.expr).max(0.0) as f32; |
| 608 | let radius_px = (raw_size / self.proxy_scale.max(1) as f32).min(48.0); |
| 609 | if (blend <= 1e-6 || radius_px < 0.25) && !mbm_flag { |
| 610 | return src; |
| 611 | } |
| 612 | let src_c = self.crop_to_canvas(enc, src); |
| 613 | let dst_depth = self.node_depth(comp, node, time, Some(src_c.depth)); |
| 614 | let filter_mode = match str_any(node, &["Filter", "FilterType", "Shape"]) { |
| 615 | Some(s) if s.eq_ignore_ascii_case("ngon") || s.eq_ignore_ascii_case("polygon") || s.eq_ignore_ascii_case("lens") => 1.0_f32, |
| 616 | Some(s) if s.eq_ignore_ascii_case("gaussian") => 2.0_f32, |
| 617 | Some(_) => 0.0_f32, |
| 618 | None => num_any(comp, node, &["Filter", "FilterType", "Shape"], time, 0.0, &self.expr).round().clamp(0.0, 2.0) as f32, |
| 619 | }; |
| 620 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src_c.size), enc, visiting, stats); |
| 621 | |
| 622 | let mut u = UniformParams::default(); |
| 623 | u.p[0] = [ |
| 624 | radius_px, |
| 625 | filter_mode, |
| 626 | num_any(comp, node, &["Sides", "NumSides", "Blades"], time, 6.0, &self.expr).round().clamp(3.0, 16.0) as f32, |
| 627 | (num_any(comp, node, &["Rotation", "Angle", "IrisRotation"], time, 0.0, &self.expr) as f32).to_radians(), |
| 628 | ]; |
| 629 | u.p[1] = [ |
| 630 | num_any(comp, node, &["BloomLevel", "Bloom", "BloomStrength"], time, 0.0, &self.expr).max(0.0) as f32, |
| 631 | num_any(comp, node, &["BloomThreshold", "Threshold"], time, 0.9, &self.expr) as f32, |
| 632 | num_any(comp, node, &["Ratio", "Aspect"], time, 1.0, &self.expr).clamp(0.2, 5.0) as f32, |
| 633 | 0.0, |
| 634 | ]; |
| 635 | u.p[2] = [blend, b2f(mask.is_some()), b2f(mask_inv), b2f(mask.is_some() && mbm_flag)]; |
| 636 | u.p[3] = self.process_skip(comp, node, time); |
| 637 | |
| 638 | let dummy_v = self.dummy.view.clone(); |
| 639 | self.run_fx2_filter(node, "Defocus", ArchiveFx2Op::Defocus, &src_c, &dummy_v, mask.as_ref(), dst_depth, &u, 4, frame_i, enc, stats) |
| 640 | } |
| 641 | |
| 642 | // ------------------------------------------------------------------------ |
| 643 | // 5. AutoGain (pure-GPU 2-pass min/max reduction + auto-levels stretch) |
| 644 | // ------------------------------------------------------------------------ |
| 645 | #[allow(clippy::too_many_arguments)] |
| 646 | pub(super) fn eval_auto_gain( |
| 647 | &mut self, |
| 648 | comp: &Comp, |
| 649 | node: &Node, |
| 650 | time: f64, |
| 651 | frame_i: i64, |
| 652 | enc: &mut wgpu::CommandEncoder, |
| 653 | visiting: &mut HashSet<(NodeId, i64)>, |
| 654 | stats: &mut EvalStats, |
| 655 | ) -> CachedTexture { |
| 656 | let Some(src_id) = conn_any(comp, node, &["Input", "Background", "Image"]) else { |
| 657 | return self.dummy.clone(); |
| 658 | }; |
| 659 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 660 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 661 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 662 | if blend <= 1e-6 && !mbm_flag { |
| 663 | return src; |
| 664 | } |
| 665 | let src_c = self.crop_to_canvas(enc, src); |
| 666 | let dst_depth = self.node_depth(comp, node, time, Some(src_c.depth)); |
| 667 | let do_auto = num_any(comp, node, &["DoAutoGain", "AutoGain"], time, 1.0, &self.expr) > 0.5; |
| 668 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src_c.size), enc, visiting, stats); |
| 669 | |
| 670 | let mut u = UniformParams::default(); |
| 671 | u.p[0] = [ |
| 672 | b2f(do_auto), |
| 673 | num_any(comp, node, &["Lift", "Low", "Black"], time, 0.0, &self.expr) as f32, |
| 674 | num_any(comp, node, &["Gain", "High", "White"], time, 1.0, &self.expr) as f32, |
| 675 | b2f(num_any(comp, node, &["IgnoreAlpha"], time, 1.0, &self.expr) > 0.5), |
| 676 | ]; |
| 677 | u.p[1] = [ |
| 678 | b2f(num_any(comp, node, &["PerChannel", "SeparateChannels"], time, 0.0, &self.expr) > 0.5), |
| 679 | blend, |
| 680 | b2f(mask.is_some()), |
| 681 | b2f(mask_inv), |
| 682 | ]; |
| 683 | u.p[2] = [b2f(mask.is_some() && mbm_flag), 0.0, 0.0, 0.0]; |
| 684 | u.p[3] = self.process_skip(comp, node, time); |
| 685 | |
| 686 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 687 | let key = self.make_key(node, "AutoGain", src_c.size, &[src_c.content_hash, mask_hash, dst_depth as u64], bytemuck::cast_slice(&u.p[..4])); |
| 688 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 689 | stats.cache_hits += 1; |
| 690 | return hit; |
| 691 | } |
| 692 | stats.cache_misses += 1; |
| 693 | stats.nodes_evaluated += 1; |
| 694 | |
| 695 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 696 | let stats_tex = if do_auto { |
| 697 | let tile_tex = self.alloc_tex([32, 16], BitDepth::Float32, 0); |
| 698 | let mut u_red = UniformParams::default(); |
| 699 | u_red.p[0] = [b2f(mask.is_some()), b2f(mask_inv), 0.0, 0.0]; |
| 700 | self.dispatch_fx2(enc, ArchiveFx2Op::AutoGainReduce1, &src_c.view, &self.dummy.view, mv, &tile_tex, &u_red); |
| 701 | let red2 = self.alloc_tex([4, 1], BitDepth::Float32, 0); |
| 702 | self.dispatch_fx2(enc, ArchiveFx2Op::AutoGainReduce2, &tile_tex.view, &self.dummy.view, &self.dummy.view, &red2, &u_red); |
| 703 | Some(red2) |
| 704 | } else { |
| 705 | None |
| 706 | }; |
| 707 | |
| 708 | let mut dst = self.alloc_tex(src_c.size, dst_depth, key); |
| 709 | let st_view = stats_tex.as_ref().map_or(&self.dummy.view, |t| &t.view); |
| 710 | self.dispatch_fx2(enc, ArchiveFx2Op::AutoGainApply, &src_c.view, st_view, mv, &dst, &u); |
| 711 | dst.set_aux(src_c.aux.clone()); |
| 712 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 713 | dst |
| 714 | } |
| 715 | |
| 716 | // ------------------------------------------------------------------------ |
| 717 | // 6. ColorBalance |
| 718 | // ------------------------------------------------------------------------ |
| 719 | #[allow(clippy::too_many_arguments)] |
| 720 | pub(super) fn eval_color_balance( |
| 721 | &mut self, |
| 722 | comp: &Comp, |
| 723 | node: &Node, |
| 724 | time: f64, |
| 725 | frame_i: i64, |
| 726 | enc: &mut wgpu::CommandEncoder, |
| 727 | visiting: &mut HashSet<(NodeId, i64)>, |
| 728 | stats: &mut EvalStats, |
| 729 | ) -> CachedTexture { |
| 730 | let Some(src_id) = conn_any(comp, node, &["Input", "Background", "Image"]) else { |
| 731 | return self.dummy.clone(); |
| 732 | }; |
| 733 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 734 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 735 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 736 | if blend <= 1e-6 && !mbm_flag { |
| 737 | return src; |
| 738 | } |
| 739 | let src_c = self.crop_to_canvas(enc, src); |
| 740 | let dst_depth = self.node_depth(comp, node, time, Some(src_c.depth)); |
| 741 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src_c.size), enc, visiting, stats); |
| 742 | |
| 743 | let mut u = UniformParams::default(); |
| 744 | u.p[0] = [ |
| 745 | num_any(comp, node, &["ShadowRed", "ShadowsRed", "LowRed", "LiftRed"], time, 0.0, &self.expr) as f32, |
| 746 | num_any(comp, node, &["ShadowGreen", "ShadowsGreen", "LowGreen", "LiftGreen"], time, 0.0, &self.expr) as f32, |
| 747 | num_any(comp, node, &["ShadowBlue", "ShadowsBlue", "LowBlue", "LiftBlue"], time, 0.0, &self.expr) as f32, |
| 748 | b2f(num_any(comp, node, &["PreserveLuminance", "PreserveLuma"], time, 1.0, &self.expr) > 0.5), |
| 749 | ]; |
| 750 | u.p[1] = [ |
| 751 | num_any(comp, node, &["MidtoneRed", "MidtonesRed", "MidRed", "GammaRed"], time, 0.0, &self.expr) as f32, |
| 752 | num_any(comp, node, &["MidtoneGreen", "MidtonesGreen", "MidGreen", "GammaGreen"], time, 0.0, &self.expr) as f32, |
| 753 | num_any(comp, node, &["MidtoneBlue", "MidtonesBlue", "MidBlue", "GammaBlue"], time, 0.0, &self.expr) as f32, |
| 754 | 0.0, |
| 755 | ]; |
| 756 | u.p[2] = [ |
| 757 | num_any(comp, node, &["HighlightRed", "HighlightsRed", "HighRed", "GainRed"], time, 0.0, &self.expr) as f32, |
| 758 | num_any(comp, node, &["HighlightGreen", "HighlightsGreen", "HighGreen", "GainGreen"], time, 0.0, &self.expr) as f32, |
| 759 | num_any(comp, node, &["HighlightBlue", "HighlightsBlue", "HighBlue", "GainBlue"], time, 0.0, &self.expr) as f32, |
| 760 | 0.0, |
| 761 | ]; |
| 762 | u.p[3] = [blend, b2f(mask.is_some()), b2f(mask_inv), b2f(mask.is_some() && mbm_flag)]; |
| 763 | u.p[4] = self.process_skip(comp, node, time); |
| 764 | |
| 765 | let dummy_v = self.dummy.view.clone(); |
| 766 | self.run_fx2_filter(node, "ColorBalance", ArchiveFx2Op::ColorBalance, &src_c, &dummy_v, mask.as_ref(), dst_depth, &u, 5, frame_i, enc, stats) |
| 767 | } |
| 768 | |
| 769 | // ------------------------------------------------------------------------ |
| 770 | // 7. SoftClip |
| 771 | // ------------------------------------------------------------------------ |
| 772 | #[allow(clippy::too_many_arguments)] |
| 773 | pub(super) fn eval_soft_clip( |
| 774 | &mut self, |
| 775 | comp: &Comp, |
| 776 | node: &Node, |
| 777 | time: f64, |
| 778 | frame_i: i64, |
| 779 | enc: &mut wgpu::CommandEncoder, |
| 780 | visiting: &mut HashSet<(NodeId, i64)>, |
| 781 | stats: &mut EvalStats, |
| 782 | ) -> CachedTexture { |
| 783 | let Some(src_id) = conn_any(comp, node, &["Input", "Background", "Image"]) else { |
| 784 | return self.dummy.clone(); |
| 785 | }; |
| 786 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 787 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 788 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 789 | if blend <= 1e-6 && !mbm_flag { |
| 790 | return src; |
| 791 | } |
| 792 | let src_c = self.crop_to_canvas(enc, src); |
| 793 | let dst_depth = self.node_depth(comp, node, time, Some(src_c.depth)); |
| 794 | let soft = num_any(comp, node, &["Softness", "Knee"], time, 0.0, &self.expr).max(0.0); |
| 795 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src_c.size), enc, visiting, stats); |
| 796 | |
| 797 | let mut u = UniformParams::default(); |
| 798 | u.p[0] = [ |
| 799 | num_any(comp, node, &["Low", "LowClip", "BlackClip"], time, 0.0, &self.expr) as f32, |
| 800 | num_any(comp, node, &["High", "HighClip", "WhiteClip"], time, 1.0, &self.expr) as f32, |
| 801 | num_any(comp, node, &["LowKnee", "LowSoftness", "BlackKnee"], time, soft, &self.expr).max(0.0) as f32, |
| 802 | num_any(comp, node, &["HighKnee", "HighSoftness", "WhiteKnee"], time, soft, &self.expr).max(0.0) as f32, |
| 803 | ]; |
| 804 | u.p[1] = [ |
| 805 | b2f(num_any(comp, node, &["ClipAlpha"], time, 0.0, &self.expr) > 0.5), |
| 806 | blend, |
| 807 | b2f(mask.is_some()), |
| 808 | b2f(mask_inv), |
| 809 | ]; |
| 810 | u.p[2] = [b2f(mask.is_some() && mbm_flag), 0.0, 0.0, 0.0]; |
| 811 | u.p[3] = self.process_skip(comp, node, time); |
| 812 | |
| 813 | let dummy_v = self.dummy.view.clone(); |
| 814 | self.run_fx2_filter(node, "SoftClip", ArchiveFx2Op::SoftClip, &src_c, &dummy_v, mask.as_ref(), dst_depth, &u, 4, frame_i, enc, stats) |
| 815 | } |
| 816 | |
| 817 | // ------------------------------------------------------------------------ |
| 818 | // 8. Grain |
| 819 | // ------------------------------------------------------------------------ |
| 820 | #[allow(clippy::too_many_arguments)] |
| 821 | pub(super) fn eval_grain( |
| 822 | &mut self, |
| 823 | comp: &Comp, |
| 824 | node: &Node, |
| 825 | time: f64, |
| 826 | frame_i: i64, |
| 827 | enc: &mut wgpu::CommandEncoder, |
| 828 | visiting: &mut HashSet<(NodeId, i64)>, |
| 829 | stats: &mut EvalStats, |
| 830 | ) -> CachedTexture { |
| 831 | let Some(src_id) = conn_any(comp, node, &["Input", "Background", "Image"]) else { |
| 832 | return self.dummy.clone(); |
| 833 | }; |
| 834 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 835 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 836 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 837 | let power = num_any(comp, node, &["Power", "Strength", "Amount"], time, 1.0, &self.expr).max(0.0) as f32; |
| 838 | if (blend <= 1e-6 || power <= 1e-6) && !mbm_flag { |
| 839 | return src; |
| 840 | } |
| 841 | let src_c = self.crop_to_canvas(enc, src); |
| 842 | let dst_depth = self.node_depth(comp, node, time, Some(src_c.depth)); |
| 843 | let size = (num_any(comp, node, &["Size", "GrainSize", "Scale"], time, 1.0, &self.expr).max(0.25) as f32 |
| 844 | / self.proxy_scale.max(1) as f32) |
| 845 | .max(0.25); |
| 846 | let time_lock = num_any(comp, node, &["TimeLockSeed", "Static"], time, 0.0, &self.expr) > 0.5; |
| 847 | let base_seed = num_any(comp, node, &["Seed", "RandomSeed"], time, 0.0, &self.expr).round() as i32; |
| 848 | let seed = if time_lock { base_seed } else { base_seed.wrapping_add(frame_i as i32) }; |
| 849 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src_c.size), enc, visiting, stats); |
| 850 | |
| 851 | let mut u = UniformParams::default(); |
| 852 | u.p[0] = [ |
| 853 | power, |
| 854 | size, |
| 855 | num_any(comp, node, &["Softness", "Blur"], time, 0.0, &self.expr).clamp(0.0, 1.0) as f32, |
| 856 | seed as f32, |
| 857 | ]; |
| 858 | u.p[1] = [ |
| 859 | num_any(comp, node, &["RedStrength", "Red", "StrengthRed"], time, 1.0, &self.expr) as f32, |
| 860 | num_any(comp, node, &["GreenStrength", "Green", "StrengthGreen"], time, 1.0, &self.expr) as f32, |
| 861 | num_any(comp, node, &["BlueStrength", "Blue", "StrengthBlue"], time, 1.0, &self.expr) as f32, |
| 862 | b2f(num_any(comp, node, &["Monochrome"], time, 0.0, &self.expr) > 0.5), |
| 863 | ]; |
| 864 | u.p[2] = [ |
| 865 | b2f(num_any(comp, node, &["AlphaMultiply"], time, 1.0, &self.expr) > 0.5), |
| 866 | blend, |
| 867 | b2f(mask.is_some()), |
| 868 | b2f(mask_inv), |
| 869 | ]; |
| 870 | u.p[3] = [b2f(mask.is_some() && mbm_flag), 0.0, 0.0, 0.0]; |
| 871 | u.p[4] = self.process_skip(comp, node, time); |
| 872 | |
| 873 | let dummy_v = self.dummy.view.clone(); |
| 874 | self.run_fx2_filter(node, "Grain", ArchiveFx2Op::Grain, &src_c, &dummy_v, mask.as_ref(), dst_depth, &u, 5, frame_i, enc, stats) |
| 875 | } |
| 876 | } |