| 1 | //! Wave 35C evaluators & GPU pipelines: |
| 2 | //! - `CoordSpace` (`Rectangular to Polar` / `Polar to Rectangular`) |
| 3 | //! - `LensDistort` (3DE Classic LD Model / Brown-Conrady / FusionRadial / FusionRadialDiv) |
| 4 | //! - `TriangleMask` (3-point SDF mask with `SoftEdge`, `BorderWidth`, `Solid`, `Invert`, `PaintMode`) |
| 5 | //! - `BSplineMask` (in `bspline.rs`: uniform rational cubic B-spline mask with animated `BSplinePolyline`) |
| 6 | //! - `Trails` (deterministic bounded-lookback temporal echo with per-step gain, transform, blur, apply mode) |
| 7 | |
| 8 | pub(in crate::eval) mod bspline; |
| 9 | #[cfg(test)] |
| 10 | mod tests; |
| 11 | |
| 12 | use super::*; |
| 13 | use std::num::NonZeroU64; |
| 14 | |
| 15 | const WARPS_MASKS_WGSL: &str = include_str!("../shaders/warps_masks.wgsl"); |
| 16 | const WM_UNIFORM_SLOTS_PER_CHUNK: u32 = 512; |
| 17 | |
| 18 | fn build_wm_wgsl(format: wgpu::TextureFormat, hdr_float: bool) -> String { |
| 19 | let storage_fmt = match format { |
| 20 | wgpu::TextureFormat::Rgba16Float => "rgba16float", |
| 21 | wgpu::TextureFormat::Rgba32Float => "rgba32float", |
| 22 | _ => "rgba8unorm", |
| 23 | }; |
| 24 | let mut src = WARPS_MASKS_WGSL.to_owned(); |
| 25 | if storage_fmt != "rgba8unorm" { |
| 26 | src = src.replace( |
| 27 | "texture_storage_2d<rgba8unorm, write>", |
| 28 | &format!("texture_storage_2d<{storage_fmt}, write>"), |
| 29 | ); |
| 30 | } |
| 31 | if hdr_float { |
| 32 | src = src.replace("const HDR_FLOAT: bool = false;", "const HDR_FLOAT: bool = true;"); |
| 33 | } |
| 34 | src |
| 35 | } |
| 36 | |
| 37 | fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { |
| 38 | wgpu::BindGroupLayoutEntry { |
| 39 | binding, |
| 40 | visibility: wgpu::ShaderStages::COMPUTE, |
| 41 | ty: wgpu::BindingType::Texture { |
| 42 | sample_type: wgpu::TextureSampleType::Float { filterable: true }, |
| 43 | view_dimension: wgpu::TextureViewDimension::D2, |
| 44 | multisampled: false, |
| 45 | }, |
| 46 | count: None, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn sampler_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { |
| 51 | wgpu::BindGroupLayoutEntry { |
| 52 | binding, |
| 53 | visibility: wgpu::ShaderStages::COMPUTE, |
| 54 | ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), |
| 55 | count: None, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fn storage_entry(binding: u32, format: wgpu::TextureFormat) -> wgpu::BindGroupLayoutEntry { |
| 60 | wgpu::BindGroupLayoutEntry { |
| 61 | binding, |
| 62 | visibility: wgpu::ShaderStages::COMPUTE, |
| 63 | ty: wgpu::BindingType::StorageTexture { |
| 64 | access: wgpu::StorageTextureAccess::WriteOnly, |
| 65 | format, |
| 66 | view_dimension: wgpu::TextureViewDimension::D2, |
| 67 | }, |
| 68 | count: None, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn uniform_entry(binding: u32, size: u64) -> wgpu::BindGroupLayoutEntry { |
| 73 | wgpu::BindGroupLayoutEntry { |
| 74 | binding, |
| 75 | visibility: wgpu::ShaderStages::COMPUTE, |
| 76 | ty: wgpu::BindingType::Buffer { |
| 77 | ty: wgpu::BufferBindingType::Uniform, |
| 78 | has_dynamic_offset: true, |
| 79 | min_binding_size: NonZeroU64::new(size), |
| 80 | }, |
| 81 | count: None, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #[derive(Clone, Copy)] |
| 86 | enum WmOp { |
| 87 | CoordSpace, |
| 88 | LensDistort, |
| 89 | TriangleMask, |
| 90 | TrailsStep, |
| 91 | TrailsBlend, |
| 92 | } |
| 93 | |
| 94 | struct WmFormatPipelines { |
| 95 | bgl: wgpu::BindGroupLayout, |
| 96 | coord_space: wgpu::ComputePipeline, |
| 97 | lens_distort: wgpu::ComputePipeline, |
| 98 | triangle_mask: wgpu::ComputePipeline, |
| 99 | trails_step: wgpu::ComputePipeline, |
| 100 | trails_blend: wgpu::ComputePipeline, |
| 101 | } |
| 102 | |
| 103 | impl WmFormatPipelines { |
| 104 | fn pipeline(&self, op: WmOp) -> &wgpu::ComputePipeline { |
| 105 | match op { |
| 106 | WmOp::CoordSpace => &self.coord_space, |
| 107 | WmOp::LensDistort => &self.lens_distort, |
| 108 | WmOp::TriangleMask => &self.triangle_mask, |
| 109 | WmOp::TrailsStep => &self.trails_step, |
| 110 | WmOp::TrailsBlend => &self.trails_blend, |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub(super) struct WarpsMasksPipelines { |
| 116 | sampler: wgpu::Sampler, |
| 117 | uniform_stride: u32, |
| 118 | uniform_chunks: Vec<wgpu::Buffer>, |
| 119 | uniform_cursor: u32, |
| 120 | by_format: HashMap<(wgpu::TextureFormat, bool), WmFormatPipelines>, |
| 121 | } |
| 122 | |
| 123 | impl WarpsMasksPipelines { |
| 124 | pub(super) fn new(device: &wgpu::Device) -> Self { |
| 125 | let uniform_size = std::mem::size_of::<UniformParams>() as u32; |
| 126 | let min_align = device.limits().min_uniform_buffer_offset_alignment.max(1); |
| 127 | let uniform_stride = uniform_size.div_ceil(min_align) * min_align; |
| 128 | let sampler = device.create_sampler(&wgpu::SamplerDescriptor { |
| 129 | label: Some("wm-linear"), |
| 130 | address_mode_u: wgpu::AddressMode::ClampToEdge, |
| 131 | address_mode_v: wgpu::AddressMode::ClampToEdge, |
| 132 | mag_filter: wgpu::FilterMode::Linear, |
| 133 | min_filter: wgpu::FilterMode::Linear, |
| 134 | mipmap_filter: wgpu::MipmapFilterMode::Nearest, |
| 135 | ..Default::default() |
| 136 | }); |
| 137 | Self { |
| 138 | sampler, |
| 139 | uniform_stride, |
| 140 | uniform_chunks: Vec::new(), |
| 141 | uniform_cursor: 0, |
| 142 | by_format: HashMap::new(), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | pub(super) fn reset_uniform_cursor(&mut self) { |
| 147 | self.uniform_cursor = 0; |
| 148 | } |
| 149 | |
| 150 | pub(super) fn trim_uniform_chunks(&mut self, max_chunks: usize) { |
| 151 | self.uniform_cursor = 0; |
| 152 | if self.uniform_chunks.len() > max_chunks { |
| 153 | self.uniform_chunks.truncate(max_chunks); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn alloc_uniform_slot( |
| 158 | &mut self, |
| 159 | device: &wgpu::Device, |
| 160 | queue: &wgpu::Queue, |
| 161 | params: &UniformParams, |
| 162 | ) -> (usize, u32) { |
| 163 | let slot_global = self.uniform_cursor; |
| 164 | let chunk_idx = (slot_global / WM_UNIFORM_SLOTS_PER_CHUNK) as usize; |
| 165 | let slot_in_chunk = slot_global % WM_UNIFORM_SLOTS_PER_CHUNK; |
| 166 | while chunk_idx >= self.uniform_chunks.len() { |
| 167 | let size = u64::from(WM_UNIFORM_SLOTS_PER_CHUNK) * u64::from(self.uniform_stride); |
| 168 | let buf = device.create_buffer(&wgpu::BufferDescriptor { |
| 169 | label: Some("wm-params-ring"), |
| 170 | size, |
| 171 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| 172 | mapped_at_creation: false, |
| 173 | }); |
| 174 | self.uniform_chunks.push(buf); |
| 175 | } |
| 176 | let offset = slot_in_chunk * self.uniform_stride; |
| 177 | queue.write_buffer( |
| 178 | &self.uniform_chunks[chunk_idx], |
| 179 | u64::from(offset), |
| 180 | bytemuck::bytes_of(params), |
| 181 | ); |
| 182 | self.uniform_cursor = slot_global + 1; |
| 183 | (chunk_idx, offset) |
| 184 | } |
| 185 | |
| 186 | fn ensure_format(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat, hdr_float: bool) { |
| 187 | if self.by_format.contains_key(&(format, hdr_float)) { |
| 188 | return; |
| 189 | } |
| 190 | let u_size = std::mem::size_of::<UniformParams>() as u64; |
| 191 | let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 192 | label: Some("wm-bgl"), |
| 193 | entries: &[ |
| 194 | tex_entry(0), |
| 195 | tex_entry(1), |
| 196 | tex_entry(2), |
| 197 | sampler_entry(3), |
| 198 | storage_entry(4, format), |
| 199 | uniform_entry(5, u_size), |
| 200 | ], |
| 201 | }); |
| 202 | let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { |
| 203 | label: Some("wm-pl"), |
| 204 | bind_group_layouts: &[Some(&bgl)], |
| 205 | immediate_size: 0, |
| 206 | }); |
| 207 | let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { |
| 208 | label: Some("warps-masks-wgsl"), |
| 209 | source: wgpu::ShaderSource::Wgsl(build_wm_wgsl(format, hdr_float).into()), |
| 210 | }); |
| 211 | let mk = |entry: &str| { |
| 212 | device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { |
| 213 | label: Some(entry), |
| 214 | layout: Some(&pl), |
| 215 | module: &module, |
| 216 | entry_point: Some(entry), |
| 217 | compilation_options: Default::default(), |
| 218 | cache: None, |
| 219 | }) |
| 220 | }; |
| 221 | let fp = WmFormatPipelines { |
| 222 | bgl, |
| 223 | coord_space: mk("coord_space_op"), |
| 224 | lens_distort: mk("lens_distort_op"), |
| 225 | triangle_mask: mk("triangle_mask_op"), |
| 226 | trails_step: mk("trails_step_op"), |
| 227 | trails_blend: mk("trails_blend_op"), |
| 228 | }; |
| 229 | self.by_format.insert((format, hdr_float), fp); |
| 230 | } |
| 231 | |
| 232 | #[allow(clippy::too_many_arguments)] |
| 233 | fn dispatch( |
| 234 | &mut self, |
| 235 | device: &wgpu::Device, |
| 236 | queue: &wgpu::Queue, |
| 237 | enc: &mut wgpu::CommandEncoder, |
| 238 | op: WmOp, |
| 239 | src_a: &wgpu::TextureView, |
| 240 | src_b: &wgpu::TextureView, |
| 241 | mask: &wgpu::TextureView, |
| 242 | dst: &CachedTexture, |
| 243 | params: &UniformParams, |
| 244 | ) { |
| 245 | let hdr_float = dst.depth.is_float(); |
| 246 | self.ensure_format(device, dst.format, hdr_float); |
| 247 | let (chunk_idx, offset) = self.alloc_uniform_slot(device, queue, params); |
| 248 | let fp = &self.by_format[&(dst.format, hdr_float)]; |
| 249 | let u_size = NonZeroU64::new(std::mem::size_of::<UniformParams>() as u64); |
| 250 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 251 | label: Some("wm-bg"), |
| 252 | layout: &fp.bgl, |
| 253 | entries: &[ |
| 254 | wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(src_a) }, |
| 255 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(src_b) }, |
| 256 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(mask) }, |
| 257 | wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::Sampler(&self.sampler) }, |
| 258 | wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::TextureView(&dst.view) }, |
| 259 | wgpu::BindGroupEntry { |
| 260 | binding: 5, |
| 261 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| 262 | buffer: &self.uniform_chunks[chunk_idx], |
| 263 | offset: 0, |
| 264 | size: u_size, |
| 265 | }), |
| 266 | }, |
| 267 | ], |
| 268 | }); |
| 269 | let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { |
| 270 | label: Some("warps-masks-pass"), |
| 271 | timestamp_writes: None, |
| 272 | }); |
| 273 | pass.set_pipeline(fp.pipeline(op)); |
| 274 | pass.set_bind_group(0, &bg, &[offset]); |
| 275 | pass.dispatch_workgroups(dst.size[0].div_ceil(8), dst.size[1].div_ceil(8), 1); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | pub(self) fn eval_first_num( |
| 280 | comp: &Comp, |
| 281 | node: &Node, |
| 282 | keys: &[&str], |
| 283 | time: f64, |
| 284 | default: f64, |
| 285 | expr: &ExprEngine, |
| 286 | ) -> f64 { |
| 287 | for &k in keys { |
| 288 | if node.inputs.contains_key(k) { |
| 289 | return comp.eval_number(node, k, time, default, expr); |
| 290 | } |
| 291 | } |
| 292 | default |
| 293 | } |
| 294 | |
| 295 | fn eval_first_point( |
| 296 | comp: &Comp, |
| 297 | node: &Node, |
| 298 | keys: &[&str], |
| 299 | time: f64, |
| 300 | default: [f64; 2], |
| 301 | expr: &ExprEngine, |
| 302 | ) -> [f64; 2] { |
| 303 | for &k in keys { |
| 304 | if node.inputs.contains_key(k) { |
| 305 | return comp.eval_point(node, k, time, default, expr); |
| 306 | } |
| 307 | } |
| 308 | default |
| 309 | } |
| 310 | |
| 311 | pub(self) fn wm_mask_clamp_edges(node: &Node) -> f32 { |
| 312 | if node.static_str("ClippingMode").unwrap_or("None").eq_ignore_ascii_case("Frame") { |
| 313 | 1.0 |
| 314 | } else { |
| 315 | 0.0 |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | pub(self) fn wm_is_paint_ignore(node: &Node) -> bool { |
| 320 | node.static_str("PaintMode") |
| 321 | .unwrap_or("Merge") |
| 322 | .eq_ignore_ascii_case("Ignore") |
| 323 | } |
| 324 | |
| 325 | fn trails_apply_mode_id(mode_str: &str) -> f32 { |
| 326 | match mode_str { |
| 327 | "Screen" => 1.0, |
| 328 | "Multiply" => 2.0, |
| 329 | "Darken" => 3.0, |
| 330 | "Lighten" => 4.0, |
| 331 | "Difference" => 5.0, |
| 332 | "Exclusion" => 6.0, |
| 333 | "Overlay" => 7.0, |
| 334 | "Hard Light" => 8.0, |
| 335 | "Soft Light" => 9.0, |
| 336 | "LinearLight" | "Linear Light" => 10.0, |
| 337 | "Color Dodge" => 11.0, |
| 338 | "LinearDodge" => 12.0, |
| 339 | "VividLight" | "Vivid Light" => 13.0, |
| 340 | "Hypotenuse" => 14.0, |
| 341 | "DarkerColor" | "Darker Color" => 15.0, |
| 342 | "LighterColor" | "Lighter Color" => 16.0, |
| 343 | "PinLight" | "Pin Light" => 17.0, |
| 344 | "Color Burn" => 18.0, |
| 345 | "LinearBurn" | "Linear Burn" => 19.0, |
| 346 | "Geometric" => 20.0, |
| 347 | "Hue" => 21.0, |
| 348 | "Saturation" => 22.0, |
| 349 | "Color" => 23.0, |
| 350 | "Luminosity" => 24.0, |
| 351 | _ => 0.0, |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | fn trails_operator_id(op_str: &str) -> f32 { |
| 356 | match op_str { |
| 357 | "Under" => 1.0, |
| 358 | "In" => 2.0, |
| 359 | "Held Out" | "HeldOut" => 3.0, |
| 360 | "Atop" => 4.0, |
| 361 | "XOr" => 5.0, |
| 362 | _ => 0.0, |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | fn eval_trails_step_params( |
| 367 | comp: &Comp, |
| 368 | node: &Node, |
| 369 | t: f64, |
| 370 | expr: &ExprEngine, |
| 371 | ) -> ([f32; 4], [f32; 4], [f32; 4], [f32; 2]) { |
| 372 | let lock_rgba = comp.eval_number(node, "LockRGBA", t, 1.0, expr) > 0.5; |
| 373 | let gr = eval_first_num(comp, node, &["GainRed", "RedGain"], t, 1.0, expr).max(0.0) as f32; |
| 374 | let gains = if lock_rgba { |
| 375 | [gr, gr, gr, gr] |
| 376 | } else { |
| 377 | [ |
| 378 | gr, |
| 379 | eval_first_num(comp, node, &["GainGreen", "GreenGain"], t, 1.0, expr).max(0.0) as f32, |
| 380 | eval_first_num(comp, node, &["GainBlue", "BlueGain"], t, 1.0, expr).max(0.0) as f32, |
| 381 | eval_first_num(comp, node, &["GainAlpha", "AlphaGain"], t, 1.0, expr).max(0.0) as f32, |
| 382 | ] |
| 383 | }; |
| 384 | let mut xo = comp.eval_number(node, "XOffset", t, 0.0, expr) as f32; |
| 385 | let mut yo = comp.eval_number(node, "YOffset", t, 0.0, expr) as f32; |
| 386 | if !node.inputs.contains_key("XOffset") && !node.inputs.contains_key("YOffset") && node.inputs.contains_key("Offset") { |
| 387 | let off = comp.eval_point(node, "Offset", t, [0.0, 0.0], expr); |
| 388 | xo = off[0] as f32; |
| 389 | yo = off[1] as f32; |
| 390 | } |
| 391 | let lock_scale = comp.eval_number(node, "LockScaleXY", t, 1.0, expr) > 0.5; |
| 392 | let xs = eval_first_num(comp, node, &["XScale", "Size"], t, 1.0, expr) as f32; |
| 393 | let ys = if lock_scale { xs } else { comp.eval_number(node, "YScale", t, 1.0, expr) as f32 }; |
| 394 | let ang = eval_first_num(comp, node, &["Rotate", "Angle"], t, 0.0, expr) as f32; |
| 395 | let piv = comp.eval_point(node, "Pivot", t, [0.5, 0.5], expr); |
| 396 | let lock_blur = comp.eval_number(node, "LockBlurXY", t, 1.0, expr) > 0.5; |
| 397 | let xb = eval_first_num(comp, node, &["XBlurSize", "BlurSize"], t, 0.0, expr).max(0.0) as f32; |
| 398 | let yb = if lock_blur { xb } else { comp.eval_number(node, "YBlurSize", t, 0.0, expr).max(0.0) as f32 }; |
| 399 | ([xo, yo, xs, ys], [ang.to_radians(), piv[0] as f32, piv[1] as f32, 0.0], gains, [xb, yb]) |
| 400 | } |
| 401 | |
| 402 | impl Engine { |
| 403 | #[allow(clippy::too_many_arguments)] |
| 404 | fn dispatch_wm( |
| 405 | &self, |
| 406 | enc: &mut wgpu::CommandEncoder, |
| 407 | op: WmOp, |
| 408 | src_a: &wgpu::TextureView, |
| 409 | src_b: &wgpu::TextureView, |
| 410 | mask: &wgpu::TextureView, |
| 411 | dst: &CachedTexture, |
| 412 | params: &UniformParams, |
| 413 | ) { |
| 414 | let cell = self |
| 415 | .warps_masks_pipelines |
| 416 | .get_or_init(|| RefCell::new(WarpsMasksPipelines::new(&self.device))); |
| 417 | cell.borrow_mut().dispatch( |
| 418 | &self.device, |
| 419 | &self.queue, |
| 420 | enc, |
| 421 | op, |
| 422 | src_a, |
| 423 | src_b, |
| 424 | mask, |
| 425 | dst, |
| 426 | params, |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | // ------------------------------------------------------------------------ |
| 431 | // 1. CoordSpace |
| 432 | // ------------------------------------------------------------------------ |
| 433 | #[allow(clippy::too_many_arguments)] |
| 434 | pub(super) fn eval_coord_space( |
| 435 | &mut self, |
| 436 | comp: &Comp, |
| 437 | node: &Node, |
| 438 | time: f64, |
| 439 | frame_i: i64, |
| 440 | enc: &mut wgpu::CommandEncoder, |
| 441 | visiting: &mut HashSet<(NodeId, i64)>, |
| 442 | stats: &mut EvalStats, |
| 443 | ) -> CachedTexture { |
| 444 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 445 | return self.dummy.clone(); |
| 446 | }; |
| 447 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 448 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 449 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 450 | if blend <= 1e-6 && !mbm_flag { |
| 451 | return src; |
| 452 | } |
| 453 | let src = self.crop_to_canvas(enc, src); |
| 454 | let shape = comp.eval_number(node, "Shape", time, 0.0, &self.expr).round().clamp(0.0, 1.0) as f32; |
| 455 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 456 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 457 | let mbm = mask.is_some() && mbm_flag; |
| 458 | let skip = self.process_skip(comp, node, time); |
| 459 | |
| 460 | let mut u = UniformParams::default(); |
| 461 | u.p[0] = [shape, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; |
| 462 | u.p[1] = [if mbm { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0]; |
| 463 | u.p[2] = skip; |
| 464 | |
| 465 | let key = self.make_key( |
| 466 | node, |
| 467 | "CoordSpace", |
| 468 | src.size, |
| 469 | &[src.content_hash, mask_hash], |
| 470 | bytemuck::cast_slice(&u.p[..3]), |
| 471 | ); |
| 472 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 473 | stats.cache_hits += 1; |
| 474 | return hit; |
| 475 | } |
| 476 | stats.cache_misses += 1; |
| 477 | stats.nodes_evaluated += 1; |
| 478 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 479 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 480 | self.dispatch_wm(enc, WmOp::CoordSpace, &src.view, &self.dummy.view, mv, &dst, &u); |
| 481 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 482 | dst |
| 483 | } |
| 484 | |
| 485 | // ------------------------------------------------------------------------ |
| 486 | // 2. LensDistort |
| 487 | // ------------------------------------------------------------------------ |
| 488 | #[allow(clippy::too_many_arguments)] |
| 489 | pub(super) fn eval_lens_distort( |
| 490 | &mut self, |
| 491 | comp: &Comp, |
| 492 | node: &Node, |
| 493 | time: f64, |
| 494 | frame_i: i64, |
| 495 | enc: &mut wgpu::CommandEncoder, |
| 496 | visiting: &mut HashSet<(NodeId, i64)>, |
| 497 | stats: &mut EvalStats, |
| 498 | ) -> CachedTexture { |
| 499 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 500 | return self.dummy.clone(); |
| 501 | }; |
| 502 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 503 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 504 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 505 | if blend <= 1e-6 && !mbm_flag { |
| 506 | return src; |
| 507 | } |
| 508 | let src = self.crop_to_canvas(enc, src); |
| 509 | |
| 510 | let mode = match node.static_str("Mode") { |
| 511 | Some(s) if s.eq_ignore_ascii_case("Distort") => 0.0_f32, |
| 512 | Some(s) if s.eq_ignore_ascii_case("Undistort") => 1.0_f32, |
| 513 | _ => comp.eval_number(node, "Mode", time, 1.0, &self.expr).round().clamp(0.0, 1.0) as f32, |
| 514 | }; |
| 515 | let model_str = node.static_str("Model").unwrap_or("DEClassicLDModel"); |
| 516 | let is_div = model_str.eq_ignore_ascii_case("FusionRadialDiv"); |
| 517 | |
| 518 | let k1 = eval_first_num( |
| 519 | comp, |
| 520 | node, |
| 521 | match model_str { |
| 522 | "FusionRadial" => &["FusionRadial.LowOrderDistortion", "Distortion", "k1"], |
| 523 | "FusionRadialDiv" => &["FusionRadialDiv.LowOrderDistortion", "Distortion", "k1"], |
| 524 | "PFTrack" => &["PFTrack.LowOrderDistortion", "Distortion", "k1"], |
| 525 | "DE4RadialStandardDegree4" => &["DE4RadialStandardDegree4.DistortionDegree2", "Distortion", "k1"], |
| 526 | "DE4RadialFisheyeDegree8Release2" => &["DE4RadialFisheyeDegree8Release2.DistortionDegree2", "Distortion", "k1"], |
| 527 | "DE4RadialFisheyeDegree8" => &["DE4RadialFisheyeDegree8.DistortionDegree2", "Distortion", "k1"], |
| 528 | _ => &[ |
| 529 | "DEClassicLDModel.Distortion", |
| 530 | "FusionRadial.LowOrderDistortion", |
| 531 | "FusionRadialDiv.LowOrderDistortion", |
| 532 | "PFTrack.LowOrderDistortion", |
| 533 | "Distortion", |
| 534 | "k1", |
| 535 | ], |
| 536 | }, |
| 537 | time, |
| 538 | 0.0, |
| 539 | &self.expr, |
| 540 | ) as f32; |
| 541 | let k2 = eval_first_num( |
| 542 | comp, |
| 543 | node, |
| 544 | match model_str { |
| 545 | "FusionRadial" => &["FusionRadial.HighOrderDistortion", "QuarticDistortion", "k2"], |
| 546 | "FusionRadialDiv" => &["FusionRadialDiv.HighOrderDistortion", "QuarticDistortion", "k2"], |
| 547 | "PFTrack" => &["PFTrack.HighOrderDistortion", "QuarticDistortion", "k2"], |
| 548 | "DE4RadialStandardDegree4" => &["DE4RadialStandardDegree4.QuarticDistortionDegree4", "QuarticDistortion", "k2"], |
| 549 | "DE4RadialFisheyeDegree8Release2" => &["DE4RadialFisheyeDegree8Release2.QuarticDistortionDegree4", "QuarticDistortion", "k2"], |
| 550 | "DE4RadialFisheyeDegree8" => &["DE4RadialFisheyeDegree8.QuarticDistortionDegree4", "QuarticDistortion", "k2"], |
| 551 | _ => &[ |
| 552 | "DEClassicLDModel.QuarticDistortion", |
| 553 | "FusionRadial.HighOrderDistortion", |
| 554 | "FusionRadialDiv.HighOrderDistortion", |
| 555 | "PFTrack.HighOrderDistortion", |
| 556 | "QuarticDistortion", |
| 557 | "k2", |
| 558 | ], |
| 559 | }, |
| 560 | time, |
| 561 | 0.0, |
| 562 | &self.expr, |
| 563 | ) as f32; |
| 564 | let anam = eval_first_num( |
| 565 | comp, |
| 566 | node, |
| 567 | &["DEClassicLDModel.AnamorphicSqueeze", "PFTrack.AnamorphicSqueeze", "AnamorphicSqueeze", "Anamorphic"], |
| 568 | time, |
| 569 | 1.0, |
| 570 | &self.expr, |
| 571 | ) |
| 572 | .max(0.05) as f32; |
| 573 | let curv_x = eval_first_num(comp, node, &["DEClassicLDModel.CurvatureX", "CurvatureX"], time, 0.0, &self.expr) as f32; |
| 574 | let curv_y = eval_first_num(comp, node, &["DEClassicLDModel.CurvatureY", "CurvatureY"], time, 0.0, &self.expr) as f32; |
| 575 | let p1 = eval_first_num( |
| 576 | comp, |
| 577 | node, |
| 578 | &["FusionRadial.TangentialDistortion.X", "FusionRadialDiv.TangentialDistortion.X", "p1"], |
| 579 | time, |
| 580 | 0.0, |
| 581 | &self.expr, |
| 582 | ) as f32; |
| 583 | let p2 = eval_first_num( |
| 584 | comp, |
| 585 | node, |
| 586 | &["FusionRadial.TangentialDistortion.Y", "FusionRadialDiv.TangentialDistortion.Y", "p2"], |
| 587 | time, |
| 588 | 0.0, |
| 589 | &self.expr, |
| 590 | ) as f32; |
| 591 | |
| 592 | let mut center = eval_first_point( |
| 593 | comp, |
| 594 | node, |
| 595 | &["Center", "FusionRadial.LensCenter", "FusionRadialDiv.LensCenter"], |
| 596 | time, |
| 597 | [0.5, 0.5], |
| 598 | &self.expr, |
| 599 | ); |
| 600 | if node.inputs.contains_key("PFTrack.LensCenterX") { |
| 601 | center[0] = comp.eval_number(node, "PFTrack.LensCenterX", time, center[0], &self.expr); |
| 602 | } |
| 603 | if node.inputs.contains_key("PFTrack.LensCenterY") { |
| 604 | center[1] = comp.eval_number(node, "PFTrack.LensCenterY", time, center[1], &self.expr); |
| 605 | } |
| 606 | center[0] += comp.eval_number(node, "LensShiftX", time, 0.0, &self.expr); |
| 607 | center[1] += comp.eval_number(node, "LensShiftY", time, 0.0, &self.expr); |
| 608 | |
| 609 | let pixel_aspect = if node.inputs.contains_key("Aspect") { |
| 610 | comp.eval_number(node, "Aspect", time, 1.0, &self.expr).max(1e-4) as f32 |
| 611 | } else if node.inputs.contains_key("PixelAspect") |
| 612 | && comp.eval_number(node, "UseSourcePixelAspect", time, 1.0, &self.expr) <= 0.5 |
| 613 | { |
| 614 | let pa = comp.eval_point(node, "PixelAspect", time, [1.0, 1.0], &self.expr); |
| 615 | (pa[0] / pa[1].max(1e-4)).max(1e-4) as f32 |
| 616 | } else { |
| 617 | 1.0 |
| 618 | }; |
| 619 | let scale = comp.eval_number(node, "Scale", time, 1.0, &self.expr).max(1e-4) as f32; |
| 620 | let edges = if node.inputs.contains_key("Edges") { |
| 621 | comp.eval_number(node, "Edges", time, 0.0, &self.expr).round().clamp(0.0, 3.0) as f32 |
| 622 | } else { |
| 623 | let em = comp.eval_number(node, "EdgeMode", time, 0.0, &self.expr).round() as i32; |
| 624 | if em >= 1 { 2.0 } else { 0.0 } |
| 625 | }; |
| 626 | let out_map = if comp.eval_number(node, "OutputDistortionMap", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 }; |
| 627 | |
| 628 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 629 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 630 | let mbm = mask.is_some() && mbm_flag; |
| 631 | let skip = self.process_skip(comp, node, time); |
| 632 | |
| 633 | let mut u = UniformParams::default(); |
| 634 | u.p[0] = [mode, k1, k2, anam]; |
| 635 | u.p[1] = [curv_x, curv_y, p1, p2]; |
| 636 | u.p[2] = [center[0] as f32, center[1] as f32, pixel_aspect, scale]; |
| 637 | u.p[3] = [edges, if is_div { 1.0 } else { 0.0 }, out_map, blend]; |
| 638 | u.p[4] = [if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }, 0.0]; |
| 639 | u.p[5] = skip; |
| 640 | |
| 641 | let key = self.make_key( |
| 642 | node, |
| 643 | "LensDistort", |
| 644 | src.size, |
| 645 | &[src.content_hash, mask_hash], |
| 646 | bytemuck::cast_slice(&u.p[..6]), |
| 647 | ); |
| 648 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 649 | stats.cache_hits += 1; |
| 650 | return hit; |
| 651 | } |
| 652 | stats.cache_misses += 1; |
| 653 | stats.nodes_evaluated += 1; |
| 654 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 655 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 656 | self.dispatch_wm(enc, WmOp::LensDistort, &src.view, &self.dummy.view, mv, &dst, &u); |
| 657 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 658 | dst |
| 659 | } |
| 660 | |
| 661 | // ------------------------------------------------------------------------ |
| 662 | // 3. TriangleMask |
| 663 | // ------------------------------------------------------------------------ |
| 664 | #[allow(clippy::too_many_arguments)] |
| 665 | pub(super) fn eval_triangle_mask( |
| 666 | &mut self, |
| 667 | comp: &Comp, |
| 668 | node: &Node, |
| 669 | time: f64, |
| 670 | frame_i: i64, |
| 671 | comp_dims: [u32; 2], |
| 672 | enc: &mut wgpu::CommandEncoder, |
| 673 | visiting: &mut HashSet<(NodeId, i64)>, |
| 674 | stats: &mut EvalStats, |
| 675 | ) -> CachedTexture { |
| 676 | let output_size_mode = node.static_str("OutputSize").unwrap_or("Default"); |
| 677 | let size = if output_size_mode != "Custom" { |
| 678 | if let Some(ts) = self.mask_target_size { |
| 679 | ts |
| 680 | } else { |
| 681 | self.explicit_mask_size(comp, node, time, comp_dims) |
| 682 | } |
| 683 | } else { |
| 684 | self.explicit_mask_size(comp, node, time, comp_dims) |
| 685 | }; |
| 686 | let p1 = comp.eval_point(node, "Point1", time, [0.1, 0.1], &self.expr); |
| 687 | let p2 = comp.eval_point(node, "Point2", time, [0.9, 0.1], &self.expr); |
| 688 | let p3 = comp.eval_point(node, "Point3", time, [0.5, 0.9], &self.expr); |
| 689 | let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32; |
| 690 | let border_w = comp.eval_number(node, "BorderWidth", time, 0.0, &self.expr) as f32; |
| 691 | let solid = if comp.eval_number(node, "Solid", time, 1.0, &self.expr) > 0.5 { 1.0 } else { 0.0 }; |
| 692 | let invert = if comp.eval_number(node, "Invert", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 }; |
| 693 | let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32; |
| 694 | let is_box = Self::mask_filter_is_box(node); |
| 695 | let clamp_edges = wm_mask_clamp_edges(node); |
| 696 | let (prev_mask, _) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 697 | if wm_is_paint_ignore(node) { |
| 698 | return prev_mask.unwrap_or_else(|| self.alloc_tex(size, BitDepth::Int8, 0)); |
| 699 | } |
| 700 | let prev_hash = prev_mask.as_ref().map_or(0, |m| m.content_hash); |
| 701 | let paint_mode = Self::paint_mode_id(node); |
| 702 | |
| 703 | let mut u = UniformParams::default(); |
| 704 | u.p[0] = [p1[0] as f32, p1[1] as f32, p2[0] as f32, p2[1] as f32]; |
| 705 | u.p[1] = [p3[0] as f32, p3[1] as f32, soft, border_w]; |
| 706 | u.p[2] = [solid, invert, level, if prev_mask.is_some() { 1.0 } else { 0.0 }]; |
| 707 | u.p[3] = [paint_mode, if is_box { 1.0 } else { 0.0 }, clamp_edges, 0.0]; |
| 708 | |
| 709 | let key = self.make_key(node, "TriangleMask", size, &[prev_hash], bytemuck::cast_slice(&u.p[..4])); |
| 710 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 711 | stats.cache_hits += 1; |
| 712 | return hit; |
| 713 | } |
| 714 | stats.cache_misses += 1; |
| 715 | stats.nodes_evaluated += 1; |
| 716 | |
| 717 | let mv = prev_mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 718 | let soft_px = soft * size[0] as f32; |
| 719 | let dst = if soft_px >= 0.5 { |
| 720 | let raw = self.alloc_tex(size, BitDepth::Int8, 0); |
| 721 | let mut u_raw = u; |
| 722 | u_raw.p[1][2] = 0.0; |
| 723 | u_raw.p[2] = [solid, 0.0, 1.0, 0.0]; |
| 724 | self.dispatch_wm(enc, WmOp::TriangleMask, &self.dummy.view, &self.dummy.view, &self.dummy.view, &raw, &u_raw); |
| 725 | let blurred = self.blur_mask_texture(enc, &raw, soft_px, is_box, clamp_edges); |
| 726 | let out_tex = self.alloc_tex(size, BitDepth::Int8, key); |
| 727 | let mut uc = UniformParams::default(); |
| 728 | uc.p[0] = [paint_mode, invert, level, if prev_mask.is_some() { 1.0 } else { 0.0 }]; |
| 729 | self.dispatch_op(enc, ShaderOp::MaskCombine, &blurred.view, &self.dummy.view, mv, &out_tex, &uc); |
| 730 | out_tex |
| 731 | } else { |
| 732 | let out_tex = self.alloc_tex(size, BitDepth::Int8, key); |
| 733 | self.dispatch_wm(enc, WmOp::TriangleMask, &self.dummy.view, &self.dummy.view, mv, &out_tex, &u); |
| 734 | out_tex |
| 735 | }; |
| 736 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 737 | dst |
| 738 | } |
| 739 | |
| 740 | // ------------------------------------------------------------------------ |
| 741 | // 4. Trails (deterministic bounded-lookback temporal persistence) |
| 742 | // ------------------------------------------------------------------------ |
| 743 | #[allow(clippy::too_many_arguments)] |
| 744 | pub(super) fn eval_trails( |
| 745 | &mut self, |
| 746 | comp: &Comp, |
| 747 | node: &Node, |
| 748 | time: f64, |
| 749 | frame_i: i64, |
| 750 | enc: &mut wgpu::CommandEncoder, |
| 751 | visiting: &mut HashSet<(NodeId, i64)>, |
| 752 | stats: &mut EvalStats, |
| 753 | ) -> CachedTexture { |
| 754 | let Some(src_id) = node.connected_node("Input").and_then(|s| comp.find_node(s)) else { |
| 755 | return self.dummy.clone(); |
| 756 | }; |
| 757 | |
| 758 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 759 | let mbm_flag = self.multiplies_by_mask(comp, node, time); |
| 760 | if blend <= 1e-6 && !mbm_flag { |
| 761 | return self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 762 | } |
| 763 | |
| 764 | let (_, _, cur_gains, _) = eval_trails_step_params(comp, node, time, &self.expr); |
| 765 | let mode_id = trails_apply_mode_id(node.static_str("ApplyMode").unwrap_or("Normal")); |
| 766 | let op_id = trails_operator_id(node.static_str("Operator").unwrap_or("Over")); |
| 767 | let sub_add = comp.eval_number(node, "SubtractiveAdditive", time, 1.0, &self.expr) as f32; |
| 768 | let merge_gain = comp.eval_number(node, "Gain", time, 1.0, &self.expr) as f32; |
| 769 | let burn_in = comp.eval_number(node, "BurnIn", time, 0.0, &self.expr) as f32; |
| 770 | let merge_under = comp.eval_number(node, "MergeUnder", time, 0.0, &self.expr) > 0.5; |
| 771 | |
| 772 | let preroll = eval_first_num(comp, node, &["NumberOfPrerollFrames", "Preroll"], time, 0.0, &self.expr) |
| 773 | .round() |
| 774 | .clamp(0.0, 128.0) as i64; |
| 775 | let restart = comp.eval_number(node, "Restart", time, 0.0, &self.expr) > 0.5; |
| 776 | let cur_frame = time.round() as i64; |
| 777 | let subframe = time - cur_frame as f64; |
| 778 | let range_start = if cur_frame >= comp.render_range[0] { |
| 779 | comp.render_range[0].saturating_sub(preroll) |
| 780 | } else { |
| 781 | comp.global_range[0].min(cur_frame) |
| 782 | }; |
| 783 | |
| 784 | let max_gain = cur_gains[0].max(cur_gains[1]).max(cur_gains[2]).max(cur_gains[3]); |
| 785 | let max_lookback: i64 = if restart || max_gain <= 1e-6 { |
| 786 | 0 |
| 787 | } else if max_gain < 0.9999 { |
| 788 | ((1.0_f64 / 512.0).ln() / (max_gain as f64).ln()) |
| 789 | .ceil() |
| 790 | .clamp(1.0, 64.0) as i64 |
| 791 | } else { |
| 792 | 64_i64.max(preroll).min(128) |
| 793 | }; |
| 794 | let window_start = range_start.max(cur_frame - max_lookback).min(cur_frame); |
| 795 | |
| 796 | let mut frames = Vec::with_capacity((cur_frame - window_start + 1) as usize); |
| 797 | let mut frame_times = Vec::with_capacity(frames.capacity()); |
| 798 | let mut frame_hashes = Vec::with_capacity(frames.capacity() + 1); |
| 799 | for f in window_start..=cur_frame { |
| 800 | let t = f as f64 + subframe; |
| 801 | let tex = self.eval_node(comp, src_id, t, enc, visiting, stats); |
| 802 | frame_hashes.push(tex.content_hash); |
| 803 | frame_times.push(t); |
| 804 | frames.push(tex); |
| 805 | } |
| 806 | let cur_src = frames.last().unwrap().clone(); |
| 807 | let cur_src_c = self.crop_to_canvas(enc, cur_src.clone()); |
| 808 | let size = cur_src_c.size; |
| 809 | let dst_depth = self.node_depth(comp, node, time, Some(cur_src_c.depth)); |
| 810 | |
| 811 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 812 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 813 | frame_hashes.push(mask_hash); |
| 814 | frame_hashes.push(dst_depth as u64); |
| 815 | let mbm = mask.is_some() && mbm_flag; |
| 816 | let skip = self.process_skip(comp, node, time); |
| 817 | let needs_blend = blend < 1.0 - 1e-6 || mask.is_some() || skip.iter().any(|&s| s > 0.5); |
| 818 | |
| 819 | if frames.len() == 1 && !needs_blend { |
| 820 | return cur_src; |
| 821 | } |
| 822 | |
| 823 | let mut step_params = Vec::with_capacity(frames.len().saturating_sub(1)); |
| 824 | let mut param_floats = Vec::with_capacity(14 + frames.len() * 14); |
| 825 | param_floats.extend_from_slice(&[ |
| 826 | mode_id, op_id, sub_add, merge_gain, burn_in, |
| 827 | if merge_under { 1.0 } else { 0.0 }, |
| 828 | blend, if mask_inv { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }, |
| 829 | skip[0], skip[1], skip[2], skip[3], |
| 830 | ]); |
| 831 | for &t_k in frame_times.iter().skip(1) { |
| 832 | let sp = eval_trails_step_params(comp, node, t_k, &self.expr); |
| 833 | param_floats.extend_from_slice(&sp.0); |
| 834 | param_floats.extend_from_slice(&sp.1); |
| 835 | param_floats.extend_from_slice(&sp.2); |
| 836 | param_floats.extend_from_slice(&sp.3); |
| 837 | step_params.push(sp); |
| 838 | } |
| 839 | |
| 840 | let key = self.make_key(node, "Trails", size, &frame_hashes, &param_floats); |
| 841 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 842 | stats.cache_hits += 1; |
| 843 | return hit; |
| 844 | } |
| 845 | stats.cache_misses += 1; |
| 846 | stats.nodes_evaluated += 1; |
| 847 | |
| 848 | let scale_ref = size[0] as f32 / 720.0; |
| 849 | let mut accum = self.crop_to_canvas(enc, frames[0].clone()); |
| 850 | let last_idx = frames.len() - 1; |
| 851 | for (i, frame_tex) in frames.iter().enumerate().skip(1) { |
| 852 | let (p0, p1, gains, blur_raw) = step_params[i - 1]; |
| 853 | let mut u_step = UniformParams::default(); |
| 854 | u_step.p[0] = p0; |
| 855 | u_step.p[1] = p1; |
| 856 | u_step.p[2] = gains; |
| 857 | |
| 858 | let cur_k = self.crop_to_canvas(enc, frame_tex.clone()); |
| 859 | let stepped = self.alloc_tex(size, dst_depth, 0); |
| 860 | self.dispatch_wm(enc, WmOp::TrailsStep, &accum.view, &self.dummy.view, &self.dummy.view, &stepped, &u_step); |
| 861 | |
| 862 | let rx = blur_raw[0] * scale_ref * 0.96; |
| 863 | let ry = blur_raw[1] * scale_ref * 0.96; |
| 864 | let mut trailed = stepped; |
| 865 | if rx >= 0.25 { |
| 866 | let tmp = self.alloc_tex(size, dst_depth, 0); |
| 867 | let mut ub = UniformParams::default(); |
| 868 | ub.p[0] = [1.0, 0.0, rx, 0.0]; |
| 869 | ub.p[1] = [1.0, 0.0, 0.0, 0.0]; |
| 870 | self.dispatch_op(enc, ShaderOp::Blur, &trailed.view, &self.dummy.view, &self.dummy.view, &tmp, &ub); |
| 871 | trailed = tmp; |
| 872 | } |
| 873 | if ry >= 0.25 { |
| 874 | let tmp = self.alloc_tex(size, dst_depth, 0); |
| 875 | let mut ub = UniformParams::default(); |
| 876 | ub.p[0] = [0.0, 1.0, ry, 0.0]; |
| 877 | ub.p[1] = [1.0, 0.0, 0.0, 0.0]; |
| 878 | self.dispatch_op(enc, ShaderOp::Blur, &trailed.view, &self.dummy.view, &self.dummy.view, &tmp, &ub); |
| 879 | trailed = tmp; |
| 880 | } |
| 881 | |
| 882 | let step_key = if i == last_idx && !needs_blend { key } else { 0 }; |
| 883 | let next = self.alloc_tex(size, dst_depth, step_key); |
| 884 | let (bg, fg) = if merge_under { (&cur_k, &trailed) } else { (&trailed, &cur_k) }; |
| 885 | let mut um = UniformParams::default(); |
| 886 | um.p[0] = [0.5, 0.5, 1.0, 0.0]; |
| 887 | um.p[1] = [1.0, merge_gain, mode_id, 0.0]; |
| 888 | um.p[2] = [0.0, 0.0, 0.0, op_id]; |
| 889 | um.p[3] = [0.0, 0.0, fg.canvas_size[0] as f32, fg.canvas_size[1] as f32]; |
| 890 | um.p[6] = [burn_in, sub_add, 0.0, 0.0]; |
| 891 | self.dispatch_op(enc, ShaderOp::Merge, &bg.view, &fg.view, &self.dummy.view, &next, &um); |
| 892 | accum = next; |
| 893 | } |
| 894 | |
| 895 | let mut dst = if needs_blend { |
| 896 | let out_tex = self.alloc_tex(size, dst_depth, key); |
| 897 | let mut ub = UniformParams::default(); |
| 898 | ub.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 }]; |
| 899 | ub.p[1] = skip; |
| 900 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 901 | self.dispatch_wm(enc, WmOp::TrailsBlend, &cur_src_c.view, &accum.view, mv, &out_tex, &ub); |
| 902 | out_tex |
| 903 | } else { |
| 904 | accum |
| 905 | }; |
| 906 | if cur_src_c.aux.is_some() && dst.size == cur_src_c.size && dst.origin == cur_src_c.origin { |
| 907 | dst.set_aux(cur_src_c.aux.clone()); |
| 908 | } |
| 909 | |
| 910 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 911 | dst |
| 912 | } |
| 913 | } |