| 1 | use super::*; |
| 2 | use crate::text_engine::{RichTextPlusParams, StyledClsRule}; |
| 3 | |
| 4 | struct ResolvedTextInputs { |
| 5 | text_str: String, |
| 6 | font_family: String, |
| 7 | font_style: String, |
| 8 | size_norm: f32, |
| 9 | tracking: f32, |
| 10 | line_spacing: f32, |
| 11 | center: [f32; 2], |
| 12 | h_lcr: f32, |
| 13 | v_tcb: f32, |
| 14 | fill_rgba: [f32; 4], |
| 15 | outline_enabled: bool, |
| 16 | elem2_shape: i32, |
| 17 | outline_rgba: [f32; 4], |
| 18 | elem2_opacity: f32, |
| 19 | outline_thickness: f32, |
| 20 | border_level: i32, |
| 21 | border_ext_h: f32, |
| 22 | border_ext_v: f32, |
| 23 | border_round: f32, |
| 24 | border_offset: [f32; 2], |
| 25 | write_start: f32, |
| 26 | write_end: f32, |
| 27 | shear_x: f32, |
| 28 | char_size_x: f32, |
| 29 | cls_rules: Vec<StyledClsRule>, |
| 30 | soft_fill: [f32; 3], |
| 31 | soft_outline: [f32; 3], |
| 32 | baseline_at_center: bool, |
| 33 | } |
| 34 | |
| 35 | fn resolve_text_inputs(comp: &Comp, node: &Node, time: f64, is_text3d: bool, expr: &ExprEngine) -> ResolvedTextInputs { |
| 36 | let text_str = comp.eval_text(node, "StyledText", time, "", expr); |
| 37 | let cls_rules: Vec<StyledClsRule> = comp |
| 38 | .text_cls_entries(node, "StyledText") |
| 39 | .iter() |
| 40 | .map(|e| StyledClsRule { |
| 41 | prop_id: e.prop_id, |
| 42 | start: e.start, |
| 43 | end: e.end, |
| 44 | value: e.value, |
| 45 | text: e.text.clone(), |
| 46 | }) |
| 47 | .collect(); |
| 48 | |
| 49 | let font_family = node |
| 50 | .static_str("Font") |
| 51 | .unwrap_or(if is_text3d { "Open Sans" } else { "Helvetica Neue" }) |
| 52 | .to_owned(); |
| 53 | let font_style = node |
| 54 | .static_str("Style") |
| 55 | .unwrap_or(if is_text3d { "Bold" } else { "Regular" }) |
| 56 | .to_owned(); |
| 57 | let raw_size = comp.eval_number(node, "Size", time, if is_text3d { 1.0 } else { 0.08 }, expr) as f32; |
| 58 | let size_norm = if is_text3d { raw_size * 0.21 } else { raw_size }; |
| 59 | let tracking = comp.eval_number(node, "CharacterSpacing", time, 1.0, expr) as f32; |
| 60 | let line_spacing = comp.eval_number(node, "LineSpacing", time, 1.0, expr) as f32; |
| 61 | let center = if is_text3d { |
| 62 | [0.5, 0.5] |
| 63 | } else { |
| 64 | let c = comp.eval_point(node, "Center", time, [0.5, 0.5], expr); |
| 65 | [c[0] as f32, c[1] as f32] |
| 66 | }; |
| 67 | let h_lcr = comp.eval_number(node, "HorizontalLeftCenterRight", time, 0.0, expr) as f32; |
| 68 | let v_tcb = comp.eval_number(node, "VerticalTopCenterBottom", time, 0.0, expr) as f32; |
| 69 | let fill_rgba = [ |
| 70 | comp.eval_number(node, "Red1", time, 1.0, expr) as f32, |
| 71 | comp.eval_number(node, "Green1", time, 1.0, expr) as f32, |
| 72 | comp.eval_number(node, "Blue1", time, 1.0, expr) as f32, |
| 73 | comp.eval_number(node, "Alpha1", time, 1.0, expr) as f32, |
| 74 | ]; |
| 75 | let outline_enabled = comp.eval_number(node, "Enabled2", time, 0.0, expr) > 0.5; |
| 76 | let elem2_shape = comp.eval_number(node, "ElementShape2", time, 1.0, expr).round() as i32; |
| 77 | let outline_rgba = [ |
| 78 | comp.eval_number(node, "Red2", time, 1.0, expr) as f32, |
| 79 | comp.eval_number(node, "Green2", time, 0.0, expr) as f32, |
| 80 | comp.eval_number(node, "Blue2", time, 0.0, expr) as f32, |
| 81 | comp.eval_number(node, "Alpha2", time, 1.0, expr) as f32, |
| 82 | ]; |
| 83 | let elem2_opacity = comp.eval_number(node, "Opacity2", time, 1.0, expr).clamp(0.0, 1.0) as f32; |
| 84 | let outline_thickness = comp.eval_number(node, "Thickness2", time, 0.02, expr) as f32; |
| 85 | let border_level = comp.eval_number(node, "Level2", time, 3.0, expr).round() as i32; |
| 86 | let border_ext_h = comp.eval_number(node, "ExtendHorizontal2", time, 0.0, expr) as f32; |
| 87 | let border_ext_v = comp.eval_number(node, "ExtendVertical2", time, 0.0, expr) as f32; |
| 88 | let border_round = comp.eval_number(node, "Round2", time, 0.0, expr).clamp(0.0, 1.0) as f32; |
| 89 | let bo = comp.eval_point(node, "Offset2", time, [0.0, 0.0], expr); |
| 90 | let border_offset = [bo[0] as f32, bo[1] as f32]; |
| 91 | let write_start = comp.eval_number(node, "Start", time, 0.0, expr) as f32; |
| 92 | let write_end = comp.eval_number(node, "End", time, 1.0, expr) as f32; |
| 93 | let shear_x = comp.eval_number(node, "CharacterShearX", time, 0.0, expr) as f32; |
| 94 | let char_size_x = if is_text3d { |
| 95 | comp.eval_number(node, "CharacterSizeX", time, 1.0, expr).max(0.0) as f32 |
| 96 | } else { |
| 97 | 1.0 |
| 98 | }; |
| 99 | |
| 100 | let soft1_on = comp.eval_number(node, "Softness1", time, 1.0, expr) > 0.5; |
| 101 | let soft2_on = comp.eval_number(node, "Softness2", time, 1.0, expr) > 0.5; |
| 102 | let soft_fill = if soft1_on { |
| 103 | [ |
| 104 | comp.eval_number(node, "SoftnessX1", time, 0.0, expr) as f32, |
| 105 | comp.eval_number(node, "SoftnessY1", time, 0.0, expr) as f32, |
| 106 | comp.eval_number(node, "SoftnessGlow1", time, 0.0, expr) as f32, |
| 107 | ] |
| 108 | } else { |
| 109 | [0.0; 3] |
| 110 | }; |
| 111 | let soft_outline = if soft2_on { |
| 112 | [ |
| 113 | comp.eval_number(node, "SoftnessX2", time, 0.0, expr) as f32, |
| 114 | comp.eval_number(node, "SoftnessY2", time, 0.0, expr) as f32, |
| 115 | comp.eval_number(node, "SoftnessGlow2", time, 0.0, expr) as f32, |
| 116 | ] |
| 117 | } else { |
| 118 | [0.0; 3] |
| 119 | }; |
| 120 | let baseline_at_center = is_text3d |
| 121 | && !node.inputs.contains_key("VerticalJustificationNew") |
| 122 | && !node.inputs.contains_key("VerticalTopCenterBottom"); |
| 123 | |
| 124 | ResolvedTextInputs { |
| 125 | text_str, |
| 126 | font_family, |
| 127 | font_style, |
| 128 | size_norm, |
| 129 | tracking, |
| 130 | line_spacing, |
| 131 | center, |
| 132 | h_lcr, |
| 133 | v_tcb, |
| 134 | fill_rgba, |
| 135 | outline_enabled, |
| 136 | elem2_shape, |
| 137 | outline_rgba, |
| 138 | elem2_opacity, |
| 139 | outline_thickness, |
| 140 | border_level, |
| 141 | border_ext_h, |
| 142 | border_ext_v, |
| 143 | border_round, |
| 144 | border_offset, |
| 145 | write_start, |
| 146 | write_end, |
| 147 | shear_x, |
| 148 | char_size_x, |
| 149 | cls_rules, |
| 150 | soft_fill, |
| 151 | soft_outline, |
| 152 | baseline_at_center, |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | impl ResolvedTextInputs { |
| 157 | fn geometry_hash(&self, ext_depth: f32, bevel_depth: f32, bevel_width: f32) -> u64 { |
| 158 | let mut h = DefaultHasher::new(); |
| 159 | "Text3DMesh".hash(&mut h); |
| 160 | self.text_str.hash(&mut h); |
| 161 | self.font_family.hash(&mut h); |
| 162 | self.font_style.hash(&mut h); |
| 163 | self.baseline_at_center.hash(&mut h); |
| 164 | for v in [ |
| 165 | self.size_norm, |
| 166 | self.tracking, |
| 167 | self.line_spacing, |
| 168 | self.center[0], |
| 169 | self.center[1], |
| 170 | self.h_lcr, |
| 171 | self.v_tcb, |
| 172 | self.write_start, |
| 173 | self.write_end, |
| 174 | self.shear_x, |
| 175 | self.char_size_x, |
| 176 | ext_depth, |
| 177 | bevel_depth, |
| 178 | bevel_width, |
| 179 | ] { |
| 180 | v.to_bits().hash(&mut h); |
| 181 | } |
| 182 | self.cls_rules.len().hash(&mut h); |
| 183 | for r in &self.cls_rules { |
| 184 | r.prop_id.hash(&mut h); |
| 185 | r.start.hash(&mut h); |
| 186 | r.end.hash(&mut h); |
| 187 | r.value.to_bits().hash(&mut h); |
| 188 | r.text.hash(&mut h); |
| 189 | } |
| 190 | h.finish() |
| 191 | } |
| 192 | |
| 193 | fn as_rich_params(&self) -> RichTextPlusParams<'_> { |
| 194 | let is_text_outline = self.outline_enabled && self.elem2_shape != 2; |
| 195 | let outline_rgba = self.outline_rgba.map(|c| c * self.elem2_opacity); |
| 196 | RichTextPlusParams { |
| 197 | text: &self.text_str, |
| 198 | font_family: &self.font_family, |
| 199 | font_style: &self.font_style, |
| 200 | size_norm: self.size_norm, |
| 201 | tracking: self.tracking, |
| 202 | line_spacing: self.line_spacing, |
| 203 | center: self.center, |
| 204 | h_lcr: self.h_lcr, |
| 205 | v_tcb: self.v_tcb, |
| 206 | fill_rgba: self.fill_rgba, |
| 207 | write_on_start: self.write_start, |
| 208 | write_on_end: self.write_end, |
| 209 | shear_x: self.shear_x, |
| 210 | cls: &self.cls_rules, |
| 211 | outline_rgba, |
| 212 | outline_thickness_norm: if is_text_outline { self.outline_thickness } else { 0.0 }, |
| 213 | outline_enabled: is_text_outline, |
| 214 | soft_fill: self.soft_fill, |
| 215 | soft_outline: self.soft_outline, |
| 216 | baseline_at_center: self.baseline_at_center, |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Scales individual character spans horizontally about each glyph's center (`Text3D.CharacterSizeX`). |
| 222 | fn scale_text_chars_x( |
| 223 | tex_size: [u32; 2], |
| 224 | origin: [i32; 2], |
| 225 | canvas: [u32; 2], |
| 226 | pixels: Vec<u8>, |
| 227 | char_sx: f32, |
| 228 | ) -> ([u32; 2], [i32; 2], Vec<u8>) { |
| 229 | let tw = tex_size[0] as usize; |
| 230 | let th = tex_size[1] as usize; |
| 231 | if tw == 0 || th == 0 || pixels.len() < tw * th * 4 || char_sx <= 1e-4 { |
| 232 | return (tex_size, origin, pixels); |
| 233 | } |
| 234 | let row_has: Vec<bool> = (0..th) |
| 235 | .map(|y| (0..tw).any(|x| pixels[(y * tw + x) * 4 + 3] > 0)) |
| 236 | .collect(); |
| 237 | let mut spans: Vec<(usize, usize, usize, usize)> = Vec::new(); |
| 238 | let mut y = 0; |
| 239 | while y < th { |
| 240 | if !row_has[y] { |
| 241 | y += 1; |
| 242 | continue; |
| 243 | } |
| 244 | let y0 = y; |
| 245 | while y < th && row_has[y] { |
| 246 | y += 1; |
| 247 | } |
| 248 | let y1 = y - 1; |
| 249 | let mut x = 0; |
| 250 | while x < tw { |
| 251 | let has_col = (y0..=y1).any(|ry| pixels[(ry * tw + x) * 4 + 3] > 0); |
| 252 | if !has_col { |
| 253 | x += 1; |
| 254 | continue; |
| 255 | } |
| 256 | let x0 = x; |
| 257 | while x < tw && (y0..=y1).any(|ry| pixels[(ry * tw + x) * 4 + 3] > 0) { |
| 258 | x += 1; |
| 259 | } |
| 260 | spans.push((x0, x - 1, y0, y1)); |
| 261 | } |
| 262 | } |
| 263 | if spans.is_empty() { |
| 264 | return (tex_size, origin, pixels); |
| 265 | } |
| 266 | let cw = canvas[0] as i32; |
| 267 | let mut out_x0 = origin[0]; |
| 268 | let mut out_x1 = origin[0] + tw as i32; |
| 269 | for &(x0, x1, _, _) in &spans { |
| 270 | let cx = origin[0] as f32 + (x0 + x1 + 1) as f32 * 0.5; |
| 271 | let hw = (x1 - x0 + 1) as f32 * 0.5 * char_sx + 1.0; |
| 272 | out_x0 = out_x0.min((cx - hw).floor() as i32); |
| 273 | out_x1 = out_x1.max((cx + hw).ceil() as i32); |
| 274 | } |
| 275 | if cw <= 0 || out_x0 >= cw || out_x1 <= 0 { |
| 276 | return ([1, 1], [0, 0], vec![0; 4]); |
| 277 | } |
| 278 | out_x0 = out_x0.clamp(0, cw - 1); |
| 279 | out_x1 = out_x1.clamp(out_x0 + 1, cw); |
| 280 | let out_w = (out_x1 - out_x0) as usize; |
| 281 | let mut out = vec![0u8; out_w * th * 4]; |
| 282 | for &(x0, x1, y0, y1) in &spans { |
| 283 | let span_cx = (x0 + x1 + 1) as f32 * 0.5; |
| 284 | let cx_canvas = origin[0] as f32 + span_cx; |
| 285 | let hw = (x1 - x0 + 1) as f32 * 0.5 * char_sx + 1.0; |
| 286 | let dx0 = ((cx_canvas - hw).floor() as i32 - out_x0).max(0) as usize; |
| 287 | let dx1 = ((cx_canvas + hw).ceil() as i32 - out_x0).min(out_w as i32) as usize; |
| 288 | for ry in y0..=y1 { |
| 289 | for dx in dx0..dx1 { |
| 290 | let gx = (out_x0 + dx as i32) as f32 + 0.5; |
| 291 | let sx = (gx - cx_canvas) / char_sx + span_cx - 0.5; |
| 292 | let i0 = sx.floor() as isize; |
| 293 | let f = sx - i0 as f32; |
| 294 | let sample = |ix: isize, c: usize| -> f32 { |
| 295 | if ix < x0 as isize || ix > x1 as isize { |
| 296 | 0.0 |
| 297 | } else { |
| 298 | pixels[(ry * tw + ix as usize) * 4 + c] as f32 |
| 299 | } |
| 300 | }; |
| 301 | let dst_off = (ry * out_w + dx) * 4; |
| 302 | for c in 0..4 { |
| 303 | let v = (sample(i0, c) * (1.0 - f) + sample(i0 + 1, c) * f).round().clamp(0.0, 255.0) as u8; |
| 304 | if v > out[dst_off + c] { |
| 305 | out[dst_off + c] = v; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | ([out_w as u32, tex_size[1]], [out_x0, origin[1]], out) |
| 312 | } |
| 313 | |
| 314 | // Keep `eval::mod`'s re-exported `media` symbols referenced across non-test targets without hitting |
| 315 | // the legacy full-canvas CPU path. |
| 316 | const _: fn([u32; 2], &TextPlusParams<'_>, i32) -> ([u32; 2], [i32; 2], Vec<u8>) = rasterize_text_plus; |
| 317 | |
| 318 | fn needs_canvas_aligned_text_output(comp: &Comp, node: &Node) -> bool { |
| 319 | let mut queue = std::collections::VecDeque::new(); |
| 320 | let mut seen = HashSet::new(); |
| 321 | queue.push_back(node.id); |
| 322 | seen.insert(node.id); |
| 323 | while let Some(cur_id) = queue.pop_front() { |
| 324 | let cur_name = comp.node(cur_id).name.as_str(); |
| 325 | for other in &comp.nodes { |
| 326 | let mut is_passthrough = matches!(other.kind.as_str(), "MacroOperator" | "GroupOperator") |
| 327 | && other.macro_output == Some(cur_id); |
| 328 | for (port, b) in &other.inputs { |
| 329 | if b.source_op.as_deref() != Some(cur_name) { |
| 330 | continue; |
| 331 | } |
| 332 | if other.bypassed |
| 333 | || matches!( |
| 334 | other.kind.as_str(), |
| 335 | "PipeRouter" |
| 336 | | "Fuse.Wireless" |
| 337 | | "Wireless" |
| 338 | | "Switch" |
| 339 | | "Fuse.Switch" |
| 340 | | "Fuse.SwitchElse" |
| 341 | | "MacroOperator" |
| 342 | | "GroupOperator" |
| 343 | ) |
| 344 | { |
| 345 | is_passthrough = true; |
| 346 | continue; |
| 347 | } |
| 348 | if (other.kind == "Merge" && port == "Background") |
| 349 | || matches!( |
| 350 | other.kind.as_str(), |
| 351 | "pEmitter" |
| 352 | | "Renderer3D" |
| 353 | | "Displace3D" |
| 354 | | "Texture2DOperator" |
| 355 | | "TextureTransformOperator" |
| 356 | | "BumpMap" |
| 357 | | "Shape3D" |
| 358 | | "ImagePlane3D" |
| 359 | | "ReplaceMaterial3D" |
| 360 | | "TimeStretcher" |
| 361 | | "TimeSpeed" |
| 362 | | "Fuse.Outline" |
| 363 | | "ErodeDilate" |
| 364 | | "Blur" |
| 365 | | "Glow" |
| 366 | | "SoftGlow" |
| 367 | | "InvertColor" |
| 368 | | "BrightnessContrast" |
| 369 | | "ColorCorrector" |
| 370 | | "Fuse.Colorize" |
| 371 | | "VariBlur" |
| 372 | | "Filter" |
| 373 | | "ColorCurves" |
| 374 | | "HueCurves" |
| 375 | | "ColorGain" |
| 376 | | "Custom" |
| 377 | ) |
| 378 | || other.kind.starts_with("Mtl") |
| 379 | { |
| 380 | return true; |
| 381 | } |
| 382 | } |
| 383 | if is_passthrough && seen.insert(other.id) { |
| 384 | queue.push_back(other.id); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | false |
| 389 | } |
| 390 | |
| 391 | impl Engine { |
| 392 | #[allow(clippy::too_many_arguments)] |
| 393 | pub(super) fn eval_p_render( |
| 394 | &mut self, |
| 395 | comp: &Comp, |
| 396 | node: &Node, |
| 397 | time: f64, |
| 398 | frame_i: i64, |
| 399 | comp_dims: [u32; 2], |
| 400 | enc: &mut wgpu::CommandEncoder, |
| 401 | visiting: &mut HashSet<(NodeId, i64)>, |
| 402 | stats: &mut EvalStats, |
| 403 | ) -> CachedTexture { |
| 404 | let w = comp.eval_number(node, "Width", time, comp.width as f64, &self.expr).round().max(1.0) as u32; |
| 405 | let h = comp.eval_number(node, "Height", time, comp.height as f64, &self.expr).round().max(1.0) as u32; |
| 406 | let raw_dims = if w > 1 && h > 1 { self.scaled_dims(w, h) } else { comp_dims }; |
| 407 | let max_dim = self.device.limits().max_texture_dimension_2d.max(1); |
| 408 | let (dims, tex_scale) = crate::cache::clamp_dims_to_limit(raw_dims[0], raw_dims[1], max_dim); |
| 409 | let dst_depth = self.node_depth(comp, node, time, None); |
| 410 | let time_key = (if time == 0.0 { 0.0 } else { time }).to_bits() as i64; |
| 411 | |
| 412 | let bitmap_sources = crate::particles::collect_bitmap_sources(comp, node.id); |
| 413 | let mut sprites: HashMap<NodeId, crate::particles::SpriteImage> = HashMap::new(); |
| 414 | let mut sprite_hashes: Vec<(NodeId, u64)> = Vec::with_capacity(bitmap_sources.len()); |
| 415 | let mut pending_sprites: Vec<(NodeId, NodeId, CachedTexture)> = Vec::new(); |
| 416 | |
| 417 | for (emitter_id, sprite_node_id) in bitmap_sources { |
| 418 | let spr_key = (sprite_node_id, comp.revision, time_key); |
| 419 | if let Some((cached, spr_hash)) = self.sprite_cache.get(&spr_key).cloned() { |
| 420 | sprite_hashes.push((emitter_id, spr_hash)); |
| 421 | sprites.insert(emitter_id, cached); |
| 422 | continue; |
| 423 | } |
| 424 | let sprite_tex = self.eval_node(comp, sprite_node_id, time, enc, visiting, stats); |
| 425 | sprite_hashes.push((emitter_id, sprite_tex.content_hash)); |
| 426 | pending_sprites.push((emitter_id, sprite_node_id, sprite_tex)); |
| 427 | } |
| 428 | |
| 429 | if !pending_sprites.is_empty() { |
| 430 | // Flush the main encoder so even already-memoized upstream sprites are submitted |
| 431 | // before `readback_rgba` maps their GPU memory. |
| 432 | let flush_enc = std::mem::replace( |
| 433 | enc, |
| 434 | self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { |
| 435 | label: Some("comp-eval-post-particle-sprite"), |
| 436 | }), |
| 437 | ); |
| 438 | self.queue.submit(Some(flush_enc.finish())); |
| 439 | self.cache.flush_pending_pool(); |
| 440 | for (emitter_id, sprite_node_id, sprite_tex) in pending_sprites { |
| 441 | let spr_key = (sprite_node_id, comp.revision, time_key); |
| 442 | if let Ok(rgba) = self.readback_rgba(&sprite_tex) { |
| 443 | let spr = crate::particles::SpriteImage { |
| 444 | size: sprite_tex.size, |
| 445 | rgba, |
| 446 | }; |
| 447 | if self.sprite_cache.len() > 64 { |
| 448 | self.sprite_cache.clear(); |
| 449 | } |
| 450 | self.sprite_cache.insert(spr_key, (spr.clone(), sprite_tex.content_hash)); |
| 451 | sprites.insert(emitter_id, spr); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | let mut h_key = DefaultHasher::new(); |
| 457 | self.cache.gpu_salt.hash(&mut h_key); |
| 458 | self.cache.node_salt(node.id).hash(&mut h_key); |
| 459 | "pRender".hash(&mut h_key); |
| 460 | comp.revision.hash(&mut h_key); |
| 461 | time_key.hash(&mut h_key); |
| 462 | dims.hash(&mut h_key); |
| 463 | dst_depth.hash(&mut h_key); |
| 464 | sprite_hashes.hash(&mut h_key); |
| 465 | let key = h_key.finish(); |
| 466 | |
| 467 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 468 | stats.cache_hits += 1; |
| 469 | return hit; |
| 470 | } |
| 471 | stats.cache_misses += 1; |
| 472 | stats.nodes_evaluated += 1; |
| 473 | |
| 474 | let rgba = crate::particles::render_particles_2d( |
| 475 | &mut self.particles, |
| 476 | comp, |
| 477 | node.id, |
| 478 | time, |
| 479 | dims, |
| 480 | &self.expr, |
| 481 | &sprites, |
| 482 | ); |
| 483 | let base_tex = create_texture(&self.device, dims, if dst_depth == BitDepth::Int8 { key } else { 0 }); |
| 484 | self.queue.write_texture( |
| 485 | base_tex.texture.as_image_copy(), |
| 486 | &rgba, |
| 487 | wgpu::TexelCopyBufferLayout { |
| 488 | offset: 0, |
| 489 | bytes_per_row: Some(dims[0] * 4), |
| 490 | rows_per_image: Some(dims[1]), |
| 491 | }, |
| 492 | wgpu::Extent3d { |
| 493 | width: dims[0], |
| 494 | height: dims[1], |
| 495 | depth_or_array_layers: 1, |
| 496 | }, |
| 497 | ); |
| 498 | |
| 499 | let mut out = if dst_depth != BitDepth::Int8 { |
| 500 | let promoted = self.alloc_tex(dims, dst_depth, key); |
| 501 | let u = UniformParams::default(); |
| 502 | self.dispatch_op(enc, ShaderOp::ChangeDepth, &base_tex.view, &self.dummy.view, &self.dummy.view, &promoted, &u); |
| 503 | promoted |
| 504 | } else { |
| 505 | base_tex |
| 506 | }; |
| 507 | out.tex_scale = tex_scale; |
| 508 | self.cache.insert(key, out.clone(), node.id, frame_i); |
| 509 | out |
| 510 | } |
| 511 | |
| 512 | pub(super) fn eval_text_plus( |
| 513 | &mut self, |
| 514 | comp: &Comp, |
| 515 | node: &Node, |
| 516 | time: f64, |
| 517 | frame_i: i64, |
| 518 | comp_dims: [u32; 2], |
| 519 | enc: &mut wgpu::CommandEncoder, |
| 520 | stats: &mut EvalStats, |
| 521 | ) -> CachedTexture { |
| 522 | self.eval_text_plus_opts(comp, node, time, frame_i, comp_dims, enc, stats, false) |
| 523 | } |
| 524 | |
| 525 | #[allow(clippy::too_many_arguments)] |
| 526 | fn eval_text_plus_opts( |
| 527 | &mut self, |
| 528 | comp: &Comp, |
| 529 | node: &Node, |
| 530 | time: f64, |
| 531 | frame_i: i64, |
| 532 | comp_dims: [u32; 2], |
| 533 | enc: &mut wgpu::CommandEncoder, |
| 534 | stats: &mut EvalStats, |
| 535 | is_text3d: bool, |
| 536 | ) -> CachedTexture { |
| 537 | let w = comp.eval_number(node, "Width", time, comp.width as f64, &self.expr) as u32; |
| 538 | let h = comp.eval_number(node, "Height", time, comp.height as f64, &self.expr) as u32; |
| 539 | let raw_size = if !is_text3d && w >= 16 && h >= 16 { self.scaled_dims(w, h) } else { comp_dims }; |
| 540 | let max_dim = self.device.limits().max_texture_dimension_2d.max(1); |
| 541 | let (size, tex_scale) = crate::cache::clamp_dims_to_limit(raw_size[0], raw_size[1], max_dim); |
| 542 | |
| 543 | let ti = resolve_text_inputs(comp, node, time, is_text3d, &self.expr); |
| 544 | let is_border_fill = ti.outline_enabled && ti.elem2_shape == 2; |
| 545 | let has_mask = Self::resolve_effect_mask_id(comp, node).is_some(); |
| 546 | let need_canvas = is_text3d || has_mask || needs_canvas_aligned_text_output(comp, node); |
| 547 | |
| 548 | let mut kh = DefaultHasher::new(); |
| 549 | self.cache.gpu_salt.hash(&mut kh); |
| 550 | self.cache.node_salt(node.id).hash(&mut kh); |
| 551 | "TextPlus".hash(&mut kh); |
| 552 | is_text3d.hash(&mut kh); |
| 553 | need_canvas.hash(&mut kh); |
| 554 | ti.baseline_at_center.hash(&mut kh); |
| 555 | size.hash(&mut kh); |
| 556 | ti.text_str.hash(&mut kh); |
| 557 | ti.font_family.hash(&mut kh); |
| 558 | ti.font_style.hash(&mut kh); |
| 559 | ti.outline_enabled.hash(&mut kh); |
| 560 | ti.elem2_shape.hash(&mut kh); |
| 561 | ti.border_level.hash(&mut kh); |
| 562 | ti.cls_rules.len().hash(&mut kh); |
| 563 | for r in &ti.cls_rules { |
| 564 | r.prop_id.hash(&mut kh); |
| 565 | r.start.hash(&mut kh); |
| 566 | r.end.hash(&mut kh); |
| 567 | r.value.to_bits().hash(&mut kh); |
| 568 | r.text.hash(&mut kh); |
| 569 | } |
| 570 | for v in [ |
| 571 | ti.size_norm, ti.tracking, ti.line_spacing, ti.center[0], ti.center[1], ti.h_lcr, ti.v_tcb, |
| 572 | ti.fill_rgba[0], ti.fill_rgba[1], ti.fill_rgba[2], ti.fill_rgba[3], |
| 573 | ti.outline_rgba[0], ti.outline_rgba[1], ti.outline_rgba[2], ti.outline_rgba[3], ti.elem2_opacity, |
| 574 | ti.outline_thickness, ti.border_ext_h, ti.border_ext_v, ti.border_round, |
| 575 | ti.border_offset[0], ti.border_offset[1], ti.write_start, ti.write_end, ti.shear_x, |
| 576 | ti.char_size_x, |
| 577 | ti.soft_fill[0], ti.soft_fill[1], ti.soft_fill[2], |
| 578 | ti.soft_outline[0], ti.soft_outline[1], ti.soft_outline[2], |
| 579 | ] { |
| 580 | v.to_bits().hash(&mut kh); |
| 581 | } |
| 582 | let key = kh.finish(); |
| 583 | |
| 584 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 585 | stats.cache_hits += 1; |
| 586 | return hit; |
| 587 | } |
| 588 | stats.cache_misses += 1; |
| 589 | stats.nodes_evaluated += 1; |
| 590 | |
| 591 | let size_px = ti.size_norm * size[0] as f32; |
| 592 | let border_pad_px = if is_border_fill { |
| 593 | let ext_max = ti.border_ext_h.abs().max(ti.border_ext_v.abs()) * size_px; |
| 594 | let off_max = (ti.border_offset[0].abs() * size[0] as f32) |
| 595 | .max(ti.border_offset[1].abs() * size[1] as f32); |
| 596 | let pad = 0.45 * size_px + ext_max + off_max; |
| 597 | if pad.is_finite() { (pad.ceil() as i32).saturating_add(4) } else { 0 } |
| 598 | } else { |
| 599 | 0 |
| 600 | }; |
| 601 | let rich = ti.as_rich_params(); |
| 602 | let (tex_size, origin, pixels) = crate::text_engine::rasterize_text_plus_rich(size, &rich, border_pad_px); |
| 603 | let (tex_size, origin, mut pixels) = if is_text3d && (ti.char_size_x - 1.0).abs() > 1e-4 { |
| 604 | scale_text_chars_x(tex_size, origin, size, pixels, ti.char_size_x) |
| 605 | } else { |
| 606 | (tex_size, origin, pixels) |
| 607 | }; |
| 608 | let outline_rgba = rich.outline_rgba; |
| 609 | let elem2_opacity = ti.elem2_opacity; |
| 610 | let border_level = ti.border_level; |
| 611 | let border_ext_h = ti.border_ext_h; |
| 612 | let border_ext_v = ti.border_ext_v; |
| 613 | let border_round = ti.border_round; |
| 614 | let border_offset = [ti.border_offset[0] as f64, ti.border_offset[1] as f64]; |
| 615 | |
| 616 | // Fusion TextPlus ElementShape = 2 (BorderFill): draw filled (optionally rounded) box(es) |
| 617 | // behind text according to Level (0=Text, 1=Line, 2=Word, 3=Character), ExtendH/V, Round, Offset. |
| 618 | if is_border_fill && elem2_opacity > 1e-4 { |
| 619 | let tw = tex_size[0] as usize; |
| 620 | let th = tex_size[1] as usize; |
| 621 | // Find non-empty rows to identify vertical line bands. |
| 622 | let mut row_has = vec![false; th]; |
| 623 | for y in 0..th { |
| 624 | let base = y * tw * 4; |
| 625 | for x in 0..tw { |
| 626 | if pixels[base + x * 4 + 3] > 8 { |
| 627 | row_has[y] = true; |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | // Merge vertical gaps smaller than 0.28 * size_px (e.g. dot on 'i'/'j'). |
| 633 | let v_merge = (0.28 * size_px).ceil() as usize; |
| 634 | let mut lines: Vec<(usize, usize)> = Vec::new(); |
| 635 | let mut y = 0; |
| 636 | while y < th { |
| 637 | if row_has[y] { |
| 638 | let y0 = y; |
| 639 | let mut y1 = y; |
| 640 | y += 1; |
| 641 | while y < th { |
| 642 | if row_has[y] { |
| 643 | y1 = y; |
| 644 | y += 1; |
| 645 | } else { |
| 646 | let mut gap = 0; |
| 647 | while y + gap < th && !row_has[y + gap] && gap <= v_merge { |
| 648 | gap += 1; |
| 649 | } |
| 650 | if gap <= v_merge && y + gap < th { |
| 651 | y += gap; |
| 652 | y1 = y; |
| 653 | y += 1; |
| 654 | } else { |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | lines.push((y0, y1)); |
| 660 | } else { |
| 661 | y += 1; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if !lines.is_empty() { |
| 666 | let mut boxes: Vec<[f32; 4]> = Vec::new(); // [x0, y0, x1, y1] |
| 667 | let base_hx = if border_level == 3 { |
| 668 | 0.03 * size_px |
| 669 | } else { |
| 670 | 0.18 * size_px |
| 671 | }; |
| 672 | let ext_scale = if border_level == 3 { 0.20 } else { 1.0 }; |
| 673 | let ext_x = border_ext_h * size_px * ext_scale; |
| 674 | let ext_y = border_ext_v * size_px * ext_scale; |
| 675 | let off_x = border_offset[0] as f32 * size[0] as f32; |
| 676 | let off_y = -(border_offset[1] as f32) * size[1] as f32; |
| 677 | |
| 678 | if border_level == 0 { |
| 679 | // Whole-text bounding box |
| 680 | let mut x0 = tw; |
| 681 | let mut x1 = 0usize; |
| 682 | let gy0 = lines.first().unwrap().0; |
| 683 | let gy1 = lines.last().unwrap().1; |
| 684 | for yy in gy0..=gy1 { |
| 685 | let base = yy * tw * 4; |
| 686 | for xx in 0..tw { |
| 687 | if pixels[base + xx * 4 + 3] > 8 { |
| 688 | x0 = x0.min(xx); |
| 689 | x1 = x1.max(xx); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | if x0 <= x1 { |
| 694 | let top_pad = 0.14 * size_px + ext_y; |
| 695 | let bot_pad = 0.16 * size_px + ext_y; |
| 696 | boxes.push([ |
| 697 | x0 as f32 - base_hx - ext_x + off_x, |
| 698 | gy0 as f32 - top_pad + off_y, |
| 699 | x1 as f32 + 1.0 + base_hx + ext_x + off_x, |
| 700 | gy1 as f32 + 1.0 + bot_pad + off_y, |
| 701 | ]); |
| 702 | } |
| 703 | } else { |
| 704 | // Per-line (1), per-word (2), or per-character (3) |
| 705 | let h_merge = match border_level { |
| 706 | 1 => tw, // single box per line |
| 707 | 2 => (0.18 * size_px).ceil().max(2.0) as usize, // merge chars within a word |
| 708 | _ => if border_round > 0.01 { 0 } else { (0.08 * size_px).ceil() as usize }, |
| 709 | }; |
| 710 | for &(ly0, ly1) in &lines { |
| 711 | let mut col_has = vec![false; tw]; |
| 712 | for yy in ly0..=ly1 { |
| 713 | let base = yy * tw * 4; |
| 714 | for xx in 0..tw { |
| 715 | if pixels[base + xx * 4 + 3] > 8 { |
| 716 | col_has[xx] = true; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | let mut runs: Vec<(usize, usize)> = Vec::new(); |
| 721 | let mut x = 0; |
| 722 | while x < tw { |
| 723 | if col_has[x] { |
| 724 | let x0 = x; |
| 725 | let mut x1 = x; |
| 726 | x += 1; |
| 727 | while x < tw { |
| 728 | if col_has[x] { |
| 729 | x1 = x; |
| 730 | x += 1; |
| 731 | } else { |
| 732 | let mut gap = 0; |
| 733 | while x + gap < tw && !col_has[x + gap] && gap <= h_merge { |
| 734 | gap += 1; |
| 735 | } |
| 736 | if gap <= h_merge && x + gap < tw { |
| 737 | x += gap; |
| 738 | x1 = x; |
| 739 | x += 1; |
| 740 | } else { |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | runs.push((x0, x1)); |
| 746 | } else { |
| 747 | x += 1; |
| 748 | } |
| 749 | } |
| 750 | let cy = 0.5 * (ly0 as f32 + ly1 as f32 + 1.0) + 0.015 * size_px; |
| 751 | let half_h = (0.40 * size_px).max(0.5 * (ly1 as f32 - ly0 as f32 + 1.0) + 0.06 * size_px) + ext_y; |
| 752 | for (rx0, rx1) in runs { |
| 753 | let cx = 0.5 * (rx0 as f32 + rx1 as f32 + 1.0) + off_x; |
| 754 | let half_w = 0.5 * (rx1 as f32 - rx0 as f32 + 1.0) + base_hx + ext_x; |
| 755 | boxes.push([ |
| 756 | cx - half_w, |
| 757 | cy - half_h + off_y, |
| 758 | cx + half_w, |
| 759 | cy + half_h + off_y, |
| 760 | ]); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // Rasterize antialiased rounded boxes and composite text OVER box! |
| 766 | let mut box_cov = vec![0.0_f32; tw * th]; |
| 767 | for b in &boxes { |
| 768 | let cx = 0.5 * (b[0] + b[2]); |
| 769 | let cy = 0.5 * (b[1] + b[3]); |
| 770 | let hw = (0.5 * (b[2] - b[0])).max(0.5); |
| 771 | let hh = (0.5 * (b[3] - b[1])).max(0.5); |
| 772 | let rad = (border_round * hh).min(hw.min(hh)); |
| 773 | let ix0 = (b[0].floor() as i32).saturating_sub(1).clamp(0, tw as i32 - 1) as usize; |
| 774 | let ix1 = (b[2].ceil() as i32).saturating_add(1).clamp(0, tw as i32 - 1) as usize; |
| 775 | let iy0 = (b[1].floor() as i32).saturating_sub(1).clamp(0, th as i32 - 1) as usize; |
| 776 | let iy1 = (b[3].ceil() as i32).saturating_add(1).clamp(0, th as i32 - 1) as usize; |
| 777 | for yy in iy0..=iy1 { |
| 778 | let py = yy as f32 + 0.5 - cy; |
| 779 | let qy = py.abs() - (hh - rad); |
| 780 | let row_off = yy * tw; |
| 781 | for xx in ix0..=ix1 { |
| 782 | let px = xx as f32 + 0.5 - cx; |
| 783 | let qx = px.abs() - (hw - rad); |
| 784 | let sdf = (qx.max(0.0).hypot(qy.max(0.0))) + qx.max(qy).min(0.0) - rad; |
| 785 | let cov = (0.5 - sdf).clamp(0.0, 1.0); |
| 786 | if cov > box_cov[row_off + xx] { |
| 787 | box_cov[row_off + xx] = cov; |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | let br = outline_rgba[0]; |
| 794 | let bg = outline_rgba[1]; |
| 795 | let bb = outline_rgba[2]; |
| 796 | let ba = outline_rgba[3]; |
| 797 | for i in 0..(tw * th) { |
| 798 | let cov = box_cov[i]; |
| 799 | if cov <= 1e-4 { |
| 800 | continue; |
| 801 | } |
| 802 | let idx = i * 4; |
| 803 | let fg_r = pixels[idx] as f32 / 255.0; |
| 804 | let fg_g = pixels[idx + 1] as f32 / 255.0; |
| 805 | let fg_b = pixels[idx + 2] as f32 / 255.0; |
| 806 | let fg_a = pixels[idx + 3] as f32 / 255.0; |
| 807 | let bg_a = cov * ba; |
| 808 | let bg_r = cov * br; |
| 809 | let bg_g = cov * bg; |
| 810 | let bg_b = cov * bb; |
| 811 | let inv_fg = 1.0 - fg_a; |
| 812 | let out_r = (fg_r + bg_r * inv_fg).clamp(0.0, 1.0); |
| 813 | let out_g = (fg_g + bg_g * inv_fg).clamp(0.0, 1.0); |
| 814 | let out_b = (fg_b + bg_b * inv_fg).clamp(0.0, 1.0); |
| 815 | let out_a = (fg_a + bg_a * inv_fg).clamp(0.0, 1.0); |
| 816 | pixels[idx] = (out_r * 255.0 + 0.5) as u8; |
| 817 | pixels[idx + 1] = (out_g * 255.0 + 0.5) as u8; |
| 818 | pixels[idx + 2] = (out_b * 255.0 + 0.5) as u8; |
| 819 | pixels[idx + 3] = (out_a * 255.0 + 0.5) as u8; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | let mut final_tex = create_texture_dod(&self.device, tex_size, origin, size, key); |
| 825 | let upload_pixels: std::borrow::Cow<'_, [u8]> = if final_tex.size != tex_size { |
| 826 | if let Some(img_buf) = image::RgbaImage::from_raw(tex_size[0], tex_size[1], pixels) { |
| 827 | let resized = image::imageops::resize( |
| 828 | &img_buf, |
| 829 | final_tex.size[0], |
| 830 | final_tex.size[1], |
| 831 | image::imageops::FilterType::Triangle, |
| 832 | ); |
| 833 | std::borrow::Cow::Owned(resized.into_raw()) |
| 834 | } else { |
| 835 | std::borrow::Cow::Owned(vec![0u8; (final_tex.size[0] * final_tex.size[1] * 4) as usize]) |
| 836 | } |
| 837 | } else { |
| 838 | std::borrow::Cow::Owned(pixels) |
| 839 | }; |
| 840 | final_tex.tex_scale *= tex_scale; |
| 841 | self.queue.write_texture( |
| 842 | final_tex.texture.as_image_copy(), |
| 843 | &upload_pixels, |
| 844 | wgpu::TexelCopyBufferLayout { |
| 845 | offset: 0, |
| 846 | bytes_per_row: Some(final_tex.size[0] * 4), |
| 847 | rows_per_image: Some(final_tex.size[1]), |
| 848 | }, |
| 849 | wgpu::Extent3d { |
| 850 | width: final_tex.size[0], |
| 851 | height: final_tex.size[1], |
| 852 | depth_or_array_layers: 1, |
| 853 | }, |
| 854 | ); |
| 855 | |
| 856 | let final_tex = if need_canvas && final_tex.has_dod() { |
| 857 | self.crop_to_canvas(enc, final_tex) |
| 858 | } else { |
| 859 | final_tex |
| 860 | }; |
| 861 | self.cache.insert(key, final_tex.clone(), node.id, frame_i); |
| 862 | final_tex |
| 863 | } |
| 864 | |
| 865 | #[allow(clippy::too_many_arguments)] |
| 866 | pub(super) fn eval_renderer3d( |
| 867 | &mut self, |
| 868 | comp: &Comp, |
| 869 | node: &Node, |
| 870 | time: f64, |
| 871 | frame_i: i64, |
| 872 | comp_dims: [u32; 2], |
| 873 | enc: &mut wgpu::CommandEncoder, |
| 874 | visiting: &mut HashSet<(NodeId, i64)>, |
| 875 | stats: &mut EvalStats, |
| 876 | ) -> CachedTexture { |
| 877 | let w = comp.eval_number(node, "Width", time, comp.width as f64, &self.expr) as u32; |
| 878 | let h = comp.eval_number(node, "Height", time, comp.height as f64, &self.expr) as u32; |
| 879 | let raw_size = if w >= 16 && h >= 16 { self.scaled_dims(w, h) } else { comp_dims }; |
| 880 | let max_dim = self.device.limits().max_texture_dimension_2d.max(1); |
| 881 | let (size, tex_scale) = crate::cache::clamp_dims_to_limit(raw_size[0], raw_size[1], max_dim); |
| 882 | let aspect = (size[0] as f32 / size[1].max(1) as f32).max(1e-4); |
| 883 | |
| 884 | let want_z = comp.eval_number(node, "RendererSoftware.Channels.Z", time, 0.0, &self.expr) >= 0.5 |
| 885 | || comp.eval_number(node, "RendererOpenGL.Channels.Z", time, 0.0, &self.expr) >= 0.5; |
| 886 | let want_normal = comp.eval_number(node, "RendererSoftware.Channels.Normal", time, 0.0, &self.expr) >= 0.5 |
| 887 | || comp.eval_number(node, "RendererOpenGL.Channels.Normal", time, 0.0, &self.expr) >= 0.5; |
| 888 | |
| 889 | let Some(scene_id) = node.connected_node("SceneInput").and_then(|s| comp.find_node(s)) else { |
| 890 | let mut empty = create_texture(&self.device, size, 0); |
| 891 | empty.tex_scale = tex_scale; |
| 892 | crate::scene3d::Scene3DRenderer::attach_empty_aux(&self.device, &self.queue, &mut empty, want_z, want_normal); |
| 893 | return empty; |
| 894 | }; |
| 895 | |
| 896 | // Only the active renderer's lighting toggle counts; Fusion's default renderer is software. |
| 897 | let renderer = node.static_str("RendererType").unwrap_or("RendererSoftware"); |
| 898 | let lighting_enabled = comp.eval_number(node, &format!("{renderer}.LightingEnabled"), time, 0.0, &self.expr) > 0.5; |
| 899 | // Only the OpenGL renderer shades shadows, and only when lighting is on. |
| 900 | let shadows_enabled = lighting_enabled |
| 901 | && renderer == "RendererOpenGL" |
| 902 | && comp.eval_number(node, "RendererOpenGL.ShadowsEnabled", time, 0.0, &self.expr) > 0.5; |
| 903 | let mut cameras = Vec::new(); |
| 904 | let mut mesh_specs = Vec::new(); |
| 905 | let mut lights = crate::scene3d::SceneLights::default(); |
| 906 | let mut visited_nodes = Vec::new(); |
| 907 | crate::scene3d::collect_scene_with_nodes( |
| 908 | comp, |
| 909 | scene_id, |
| 910 | time, |
| 911 | &self.expr, |
| 912 | crate::scene3d::mat4_identity(), |
| 913 | aspect, |
| 914 | &mut cameras, |
| 915 | &mut mesh_specs, |
| 916 | &mut lights, |
| 917 | &mut visited_nodes, |
| 918 | 0, |
| 919 | ); |
| 920 | |
| 921 | let selector = node.static_str("CameraSelector").unwrap_or_default(); |
| 922 | let selected_camera = cameras.iter().find(|c| c.name == selector).or(cameras.first()); |
| 923 | |
| 924 | let (view, mut proj, cam_pos, cam_far) = if let Some(cam) = selected_camera { |
| 925 | let bad_clip = !(cam.near.is_finite() && cam.far.is_finite() && cam.near < cam.far) |
| 926 | || (!cam.orthographic && (cam.near <= 0.0 || !cam.vfov_rad.is_finite() || cam.vfov_rad <= 0.0 || cam.vfov_rad >= std::f32::consts::PI)); |
| 927 | if bad_clip { |
| 928 | let mut empty = create_texture(&self.device, size, 0); |
| 929 | empty.tex_scale = tex_scale; |
| 930 | crate::scene3d::Scene3DRenderer::attach_empty_aux(&self.device, &self.queue, &mut empty, want_z, want_normal); |
| 931 | return empty; |
| 932 | } |
| 933 | let v = crate::scene3d::mat4_inverse(cam.world); |
| 934 | let p = if cam.orthographic { |
| 935 | let inv_w = 1.0 / aspect.max(1e-4); |
| 936 | let inv_dz = 1.0 / (cam.far - cam.near).max(1e-4); |
| 937 | [ |
| 938 | inv_w, 0.0, 0.0, 0.0, |
| 939 | 0.0, 1.0, 0.0, 0.0, |
| 940 | 0.0, 0.0, -inv_dz, 0.0, |
| 941 | 0.0, 0.0, -cam.near * inv_dz, 1.0, |
| 942 | ] |
| 943 | } else { |
| 944 | crate::scene3d::mat4_perspective_wgpu(cam.vfov_rad, aspect, cam.near, cam.far) |
| 945 | }; |
| 946 | (v, p, [cam.world[12], cam.world[13], cam.world[14]], cam.far) |
| 947 | } else { |
| 948 | let v = crate::scene3d::mat4_translate(0.0, 0.0, -5.0); |
| 949 | let p = crate::scene3d::mat4_perspective_wgpu(45.0_f32.to_radians(), aspect, 0.05, 5000.0); |
| 950 | (v, p, [0.0, 0.0, 5.0], 5000.0) |
| 951 | }; |
| 952 | let lock_os = comp.eval_number(node, "LockOverscanXY", time, 1.0, &self.expr) > 0.5; |
| 953 | let os_main = comp.eval_number(node, "Overscan", time, 1.0, &self.expr).max(1e-4) as f32; |
| 954 | let (osx, osy) = if lock_os { |
| 955 | (os_main, os_main) |
| 956 | } else { |
| 957 | ( |
| 958 | comp.eval_number(node, "OverscanX", time, 1.0, &self.expr).max(1e-4) as f32, |
| 959 | comp.eval_number(node, "OverscanY", time, 1.0, &self.expr).max(1e-4) as f32, |
| 960 | ) |
| 961 | }; |
| 962 | proj[0] /= osx; |
| 963 | proj[5] /= osy; |
| 964 | let view_proj = crate::scene3d::mat4_mul(proj, view); |
| 965 | |
| 966 | let mut draw_meshes = Vec::with_capacity(mesh_specs.len()); |
| 967 | let mut upstream_hashes = Vec::with_capacity(mesh_specs.len()); |
| 968 | let mut hash_floats = Vec::with_capacity(28 + mesh_specs.len() * 20); |
| 969 | hash_floats.extend_from_slice(&view_proj); |
| 970 | hash_floats.push(if lighting_enabled { 1.0 } else { 0.0 }); |
| 971 | hash_floats.push(if shadows_enabled { 1.0 } else { 0.0 }); |
| 972 | hash_floats.push(if want_z { 1.0 } else { 0.0 }); |
| 973 | hash_floats.push(if want_normal { 1.0 } else { 0.0 }); |
| 974 | hash_floats.extend_from_slice(&lights.ambient); |
| 975 | hash_floats.push(lights.count as f32); |
| 976 | for dl in &lights.dirs { |
| 977 | hash_floats.extend_from_slice(&dl.to_light); |
| 978 | hash_floats.extend_from_slice(&dl.color); |
| 979 | } |
| 980 | for pl in &lights.points { |
| 981 | hash_floats.extend_from_slice(&pl.pos); |
| 982 | hash_floats.extend_from_slice(&pl.color); |
| 983 | hash_floats.push(pl.decay_type); |
| 984 | hash_floats.push(pl.decay_rate); |
| 985 | hash_floats.push(pl.shadow_density.unwrap_or(-1.0)); |
| 986 | } |
| 987 | for sl in &lights.spots { |
| 988 | hash_floats.extend_from_slice(&sl.pos); |
| 989 | hash_floats.extend_from_slice(&sl.dir); |
| 990 | hash_floats.extend_from_slice(&sl.color); |
| 991 | hash_floats.push(sl.cos_inner); |
| 992 | hash_floats.push(sl.cos_outer); |
| 993 | hash_floats.push(sl.decay_type); |
| 994 | } |
| 995 | |
| 996 | let mut h_scene = DefaultHasher::new(); |
| 997 | for vid in &visited_nodes { |
| 998 | vid.hash(&mut h_scene); |
| 999 | self.cache.node_salt(*vid).hash(&mut h_scene); |
| 1000 | } |
| 1001 | |
| 1002 | // First resolve all 2D textures & Text3D plates/extrusions & Displace3D heightmaps |
| 1003 | let mut pending_displace_tex: Vec<Option<(CachedTexture, f32, f32, i32)>> = Vec::with_capacity(mesh_specs.len()); |
| 1004 | for ms in &mesh_specs { |
| 1005 | let mut mesh_shape = ms.shape.clone(); |
| 1006 | let mut custom_mesh_key = None; |
| 1007 | let mut diffuse_col = ms.diffuse_col; |
| 1008 | let tex = if let Some(t3d) = &ms.text3d { |
| 1009 | let tnode = comp.node(t3d.node_id); |
| 1010 | let ext_on = comp.eval_number(tnode, "Extrusion", time, 1.0, &self.expr) > 0.5; |
| 1011 | let ext_depth = if ext_on { |
| 1012 | let nested = comp.eval_number(tnode, "Extrusion.ExtrusionDepth", time, 0.0, &self.expr); |
| 1013 | comp.eval_number(tnode, "ExtrusionDepth", time, nested, &self.expr) as f32 |
| 1014 | } else { |
| 1015 | 0.0 |
| 1016 | }; |
| 1017 | if ext_depth.abs() > 1e-4 { |
| 1018 | let ti = resolve_text_inputs(comp, tnode, time, true, &self.expr); |
| 1019 | let bevel_depth = comp.eval_number( |
| 1020 | tnode, |
| 1021 | "Extrusion.BevelDepth", |
| 1022 | time, |
| 1023 | comp.eval_number(tnode, "BevelDepth", time, 0.0, &self.expr), |
| 1024 | &self.expr, |
| 1025 | ) as f32; |
| 1026 | let bevel_width = comp.eval_number( |
| 1027 | tnode, |
| 1028 | "Extrusion.BevelWidth", |
| 1029 | time, |
| 1030 | comp.eval_number(tnode, "BevelWidth", time, 0.0, &self.expr), |
| 1031 | &self.expr, |
| 1032 | ) as f32; |
| 1033 | let mesh_key = ti.geometry_hash(ext_depth, bevel_depth, bevel_width); |
| 1034 | mesh_key.hash(&mut h_scene); |
| 1035 | custom_mesh_key = Some(mesh_key); |
| 1036 | let verts = self.scene3d.get_or_build_text3d_mesh(mesh_key, || { |
| 1037 | crate::text_engine::build_text3d_mesh(&ti.as_rich_params(), ext_depth) |
| 1038 | }); |
| 1039 | let custom = crate::scene3d::ShapeKind::CustomMesh(verts); |
| 1040 | mesh_shape = match &ms.shape { |
| 1041 | crate::scene3d::ShapeKind::WithMaterialFx { fx, .. } => { |
| 1042 | crate::scene3d::ShapeKind::WithMaterialFx { inner: Box::new(custom), fx: *fx } |
| 1043 | } |
| 1044 | _ => custom, |
| 1045 | }; |
| 1046 | for k in 0..4 { |
| 1047 | diffuse_col[k] *= ti.fill_rgba[k]; |
| 1048 | } |
| 1049 | None |
| 1050 | } else { |
| 1051 | Some(self.eval_text_plus_opts(comp, tnode, time, frame_i, [2048, 1024], enc, stats, true)) |
| 1052 | } |
| 1053 | } else { |
| 1054 | ms.material_node.map(|mid| self.eval_node(comp, mid, time, enc, visiting, stats)) |
| 1055 | }; |
| 1056 | let bump_tex = match ms.bump_node { |
| 1057 | Some(bid) if Some(bid) == ms.material_node => tex.clone(), |
| 1058 | Some(bid) => Some(self.eval_node(comp, bid, time, enc, visiting, stats)), |
| 1059 | None => None, |
| 1060 | }; |
| 1061 | let disp_entry = ms.displace.as_ref().map(|ds| { |
| 1062 | let dtex = self.eval_node(comp, ds.input_node, time, enc, visiting, stats); |
| 1063 | (dtex, ds.scale, ds.bias, ds.channel) |
| 1064 | }); |
| 1065 | mesh_shape.hash_into(&mut h_scene); |
| 1066 | upstream_hashes.push(tex.as_ref().map_or(0, |t| t.content_hash)); |
| 1067 | upstream_hashes.push(bump_tex.as_ref().map_or(0, |t| t.content_hash)); |
| 1068 | upstream_hashes.push(disp_entry.as_ref().map_or(0, |(t, _, _, _)| t.content_hash)); |
| 1069 | hash_floats.extend_from_slice(&ms.world); |
| 1070 | hash_floats.extend_from_slice(&diffuse_col); |
| 1071 | hash_floats.extend_from_slice(&ms.specular_col); |
| 1072 | hash_floats.push(ms.specular_intensity); |
| 1073 | hash_floats.push(ms.specular_exponent); |
| 1074 | hash_floats.push(if ms.affected_by_lights { 1.0 } else { 0.0 }); |
| 1075 | hash_floats.push(if ms.casts_shadow { 1.0 } else { 0.0 }); |
| 1076 | hash_floats.push(if ms.receives_shadows { 1.0 } else { 0.0 }); |
| 1077 | hash_floats.push(if ms.flip_normals { 1.0 } else { 0.0 }); |
| 1078 | if let Some((_, sc, bi, ch)) = disp_entry { |
| 1079 | hash_floats.push(sc); |
| 1080 | hash_floats.push(bi); |
| 1081 | hash_floats.push(ch as f32); |
| 1082 | } |
| 1083 | draw_meshes.push(crate::scene3d::DrawMesh { |
| 1084 | world: ms.world, |
| 1085 | shape: mesh_shape, |
| 1086 | custom_mesh_key, |
| 1087 | tex, |
| 1088 | bump_tex, |
| 1089 | diffuse_col, |
| 1090 | specular_col: ms.specular_col, |
| 1091 | specular_intensity: ms.specular_intensity, |
| 1092 | specular_exponent: ms.specular_exponent, |
| 1093 | affected_by_lights: ms.affected_by_lights, |
| 1094 | casts_shadow: ms.casts_shadow, |
| 1095 | receives_shadows: ms.receives_shadows, |
| 1096 | flip_normals: ms.flip_normals, |
| 1097 | displace: None, |
| 1098 | }); |
| 1099 | pending_displace_tex.push(disp_entry); |
| 1100 | } |
| 1101 | upstream_hashes.push(h_scene.finish()); |
| 1102 | |
| 1103 | let key = self.make_key(node, "Renderer3D", size, &upstream_hashes, &hash_floats); |
| 1104 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 1105 | stats.cache_hits += 1; |
| 1106 | return hit; |
| 1107 | } |
| 1108 | stats.cache_misses += 1; |
| 1109 | stats.nodes_evaluated += 1; |
| 1110 | |
| 1111 | // If any Displace3D heightmaps are present, flush pending encoder commands so CPU readback sees fresh pixels. |
| 1112 | if pending_displace_tex.iter().any(|d| d.is_some()) { |
| 1113 | let flush_enc = std::mem::replace( |
| 1114 | enc, |
| 1115 | self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { |
| 1116 | label: Some("comp-eval-post-disp3d"), |
| 1117 | }), |
| 1118 | ); |
| 1119 | self.queue.submit(Some(flush_enc.finish())); |
| 1120 | self.cache.flush_pending_pool(); |
| 1121 | for (dm, entry) in draw_meshes.iter_mut().zip(pending_displace_tex.into_iter()) { |
| 1122 | if let Some((dtex, scale, bias, channel)) = entry { |
| 1123 | if let Ok(rgba) = self.readback_rgba(&dtex) { |
| 1124 | dm.displace = Some(crate::scene3d::DisplaceHeightmap { |
| 1125 | size: dtex.size, |
| 1126 | rgba, |
| 1127 | scale, |
| 1128 | bias, |
| 1129 | channel, |
| 1130 | }); |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | let mut dst = create_texture(&self.device, size, key); |
| 1137 | dst.tex_scale = tex_scale; |
| 1138 | self.scene3d.render_meshes_with_aux( |
| 1139 | &self.device, |
| 1140 | &self.queue, |
| 1141 | enc, |
| 1142 | &mut dst, |
| 1143 | &self.dummy, |
| 1144 | view_proj, |
| 1145 | view, |
| 1146 | proj, |
| 1147 | cam_pos, |
| 1148 | cam_far, |
| 1149 | lights, |
| 1150 | lighting_enabled, |
| 1151 | shadows_enabled, |
| 1152 | want_z, |
| 1153 | want_normal, |
| 1154 | &draw_meshes, |
| 1155 | ); |
| 1156 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 1157 | dst |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | #[cfg(test)] |
| 1162 | mod tests { |
| 1163 | use model::Comp; |
| 1164 | use crate::Engine; |
| 1165 | |
| 1166 | fn render_text_plus(opacity2: f32, line_spacing: f32, styled: &str) -> ([u32; 2], Vec<u8>) { |
| 1167 | let src = format!( |
| 1168 | r#"Composition {{ |
| 1169 | CurrentTime = 0, |
| 1170 | RenderRange = {{ 0, 1 }}, |
| 1171 | GlobalRange = {{ 0, 1 }}, |
| 1172 | Tools = ordered() {{ |
| 1173 | T1 = TextPlus {{ |
| 1174 | Inputs = {{ |
| 1175 | Width = Input {{ Value = 320, }}, |
| 1176 | Height = Input {{ Value = 240, }}, |
| 1177 | StyledText = Input {{ Value = "{styled}", }}, |
| 1178 | Size = Input {{ Value = 0.20, }}, |
| 1179 | LineSpacing = Input {{ Value = {line_spacing}, }}, |
| 1180 | Red1 = Input {{ Value = 1.0, }}, |
| 1181 | Green1 = Input {{ Value = 1.0, }}, |
| 1182 | Blue1 = Input {{ Value = 1.0, }}, |
| 1183 | Alpha1 = Input {{ Value = 1.0, }}, |
| 1184 | Enabled2 = Input {{ Value = 1, }}, |
| 1185 | ElementShape2 = Input {{ Value = 1, }}, |
| 1186 | Red2 = Input {{ Value = 1.0, }}, |
| 1187 | Green2 = Input {{ Value = 0.0, }}, |
| 1188 | Blue2 = Input {{ Value = 0.0, }}, |
| 1189 | Alpha2 = Input {{ Value = 1.0, }}, |
| 1190 | Opacity2 = Input {{ Value = {opacity2}, }}, |
| 1191 | Thickness2 = Input {{ Value = 0.12, }}, |
| 1192 | }}, |
| 1193 | }}, |
| 1194 | }}, |
| 1195 | }}"# |
| 1196 | ); |
| 1197 | let comp = Comp::from_fusion_str(&src).expect("parse TextPlus test comp"); |
| 1198 | let Ok(mut eng) = Engine::new_headless() else { |
| 1199 | return ([0, 0], Vec::new()); |
| 1200 | }; |
| 1201 | let id = comp.find_node("T1").expect("T1"); |
| 1202 | let (tex, _) = eng.evaluate(&comp, id, 0.0); |
| 1203 | let rgba = eng.readback_rgba(&tex).expect("readback TextPlus"); |
| 1204 | (tex.size, rgba) |
| 1205 | } |
| 1206 | |
| 1207 | #[test] |
| 1208 | fn test_text_plus_outline_opacity_and_line_spacing() { |
| 1209 | let (size_full, px_full) = render_text_plus(1.0, 1.0, "H"); |
| 1210 | if size_full == [0, 0] { |
| 1211 | return; |
| 1212 | } |
| 1213 | let (_, px_zero) = render_text_plus(0.0, 1.0, "H"); |
| 1214 | let count_red_outline = |rgba: &[u8]| -> usize { |
| 1215 | rgba.chunks_exact(4) |
| 1216 | .filter(|c| c[0] > 180 && c[1] < 40 && c[2] < 40 && c[3] > 180) |
| 1217 | .count() |
| 1218 | }; |
| 1219 | assert!( |
| 1220 | count_red_outline(&px_full) > 20, |
| 1221 | "Opacity2=1.0 should render red outline pixels" |
| 1222 | ); |
| 1223 | assert_eq!( |
| 1224 | count_red_outline(&px_zero), |
| 1225 | 0, |
| 1226 | "Opacity2=0.0 must suppress the outline completely" |
| 1227 | ); |
| 1228 | |
| 1229 | let y_span = |rgba: &[u8], w: u32, h: u32| -> i32 { |
| 1230 | let mut min_y = i32::MAX; |
| 1231 | let mut max_y = i32::MIN; |
| 1232 | for y in 0..h as usize { |
| 1233 | let row = y * w as usize * 4; |
| 1234 | if (0..w as usize).any(|x| rgba[row + x * 4 + 3] > 64) { |
| 1235 | min_y = min_y.min(y as i32); |
| 1236 | max_y = max_y.max(y as i32); |
| 1237 | } |
| 1238 | } |
| 1239 | max_y - min_y |
| 1240 | }; |
| 1241 | let (s1, p1) = render_text_plus(0.0, 1.0, "H\\nH"); |
| 1242 | let (s2, p2) = render_text_plus(0.0, 2.0, "H\\nH"); |
| 1243 | let diff = y_span(&p2, s2[0], s2[1]) - y_span(&p1, s1[0], s1[1]); |
| 1244 | // Size=0.20 * W=320 * 0.8 = 51.2px per 1.0 LineSpacing step. |
| 1245 | assert!( |
| 1246 | (46..=56).contains(&diff), |
| 1247 | "LineSpacing 2.0 vs 1.0 on 320px wide frame should expand ~51px, got {diff}" |
| 1248 | ); |
| 1249 | } |
| 1250 | |
| 1251 | #[test] |
| 1252 | fn renderer3d_invalidates_cache_when_shape3d_radius_animates() { |
| 1253 | let src = r#" |
| 1254 | Composition { |
| 1255 | CurrentTime = 0, |
| 1256 | RenderRange = { 0, 10 }, |
| 1257 | GlobalRange = { 0, 10 }, |
| 1258 | Tools = ordered() { |
| 1259 | RadAnim = BezierSpline { |
| 1260 | KeyFrames = { |
| 1261 | [0] = { 0.2, Flags = { Linear = true } }, |
| 1262 | [5] = { 1.6, Flags = { Linear = true } }, |
| 1263 | }, |
| 1264 | }, |
| 1265 | Sphere1 = Shape3D { |
| 1266 | Inputs = { |
| 1267 | Shape = Input { Value = FuID { "SurfaceSphereInputs" }, }, |
| 1268 | ["SurfaceSphereInputs.Radius"] = Input { SourceOp = "RadAnim", Source = "Value", }, |
| 1269 | ["Transform3DOp.Translate.Z"] = Input { Value = -3.0, }, |
| 1270 | }, |
| 1271 | }, |
| 1272 | R3D = Renderer3D { |
| 1273 | Inputs = { |
| 1274 | Width = Input { Value = 64, }, |
| 1275 | Height = Input { Value = 64, }, |
| 1276 | SceneInput = Input { SourceOp = "Sphere1", Source = "Output", }, |
| 1277 | }, |
| 1278 | }, |
| 1279 | }, |
| 1280 | } |
| 1281 | "#; |
| 1282 | let comp = Comp::from_fusion_str(src).expect("parse Renderer3D animated radius comp"); |
| 1283 | let Ok(mut eng) = Engine::new_headless() else { |
| 1284 | return; |
| 1285 | }; |
| 1286 | let r3d = comp.find_node("R3D").unwrap(); |
| 1287 | let (t0, _) = eng.evaluate(&comp, r3d, 0.0); |
| 1288 | let p0 = eng.readback_rgba(&t0).unwrap(); |
| 1289 | let (t5, _) = eng.evaluate(&comp, r3d, 5.0); |
| 1290 | let p5 = eng.readback_rgba(&t5).unwrap(); |
| 1291 | |
| 1292 | let cov = |px: &[u8]| px.chunks_exact(4).filter(|c| c[3] > 16).count(); |
| 1293 | let c0 = cov(&p0); |
| 1294 | let c5 = cov(&p5); |
| 1295 | assert!(c0 > 10, "frame 0 small sphere should be visible, got {c0} pixels"); |
| 1296 | assert!( |
| 1297 | c5 > c0 * 2, |
| 1298 | "frame 5 larger Radius must invalidate Renderer3D cache and cover far more pixels (f0={c0}, f5={c5})" |
| 1299 | ); |
| 1300 | } |
| 1301 | |
| 1302 | #[test] |
| 1303 | fn extruded_text3d_reuses_mesh_cache_key_arc_and_renderer3d_cache() { |
| 1304 | let src = r#" |
| 1305 | Composition { |
| 1306 | CurrentTime = 0, |
| 1307 | RenderRange = { 0, 10 }, |
| 1308 | GlobalRange = { 0, 10 }, |
| 1309 | Tools = ordered() { |
| 1310 | CamAnim = BezierSpline { |
| 1311 | KeyFrames = { |
| 1312 | [0] = { 4.0, Flags = { Linear = true } }, |
| 1313 | [1] = { 4.5, Flags = { Linear = true } }, |
| 1314 | }, |
| 1315 | }, |
| 1316 | Cam = Camera3D { |
| 1317 | Inputs = { |
| 1318 | ["Transform3DOp.Translate.Z"] = Input { SourceOp = "CamAnim", Source = "Value", }, |
| 1319 | ApertureW = Input { Value = 1.0, }, |
| 1320 | ApertureH = Input { Value = 1.0, }, |
| 1321 | }, |
| 1322 | }, |
| 1323 | Txt = Text3D { |
| 1324 | Inputs = { |
| 1325 | StyledText = Input { Value = "OB", }, |
| 1326 | Font = Input { Value = "Liberation Sans", }, |
| 1327 | Style = Input { Value = "Regular", }, |
| 1328 | Size = Input { Value = 1.0, }, |
| 1329 | Extrusion = Input { Value = 1, }, |
| 1330 | ["Extrusion.ExtrusionDepth"] = Input { Value = 0.2, }, |
| 1331 | }, |
| 1332 | }, |
| 1333 | M = Merge3D { |
| 1334 | Inputs = { |
| 1335 | SceneInput1 = Input { SourceOp = "Txt", Source = "Output", }, |
| 1336 | SceneInput2 = Input { SourceOp = "Cam", Source = "Output", }, |
| 1337 | }, |
| 1338 | }, |
| 1339 | R3D = Renderer3D { |
| 1340 | Inputs = { |
| 1341 | Width = Input { Value = 64, }, |
| 1342 | Height = Input { Value = 64, }, |
| 1343 | SceneInput = Input { SourceOp = "M", Source = "Output", }, |
| 1344 | }, |
| 1345 | }, |
| 1346 | }, |
| 1347 | } |
| 1348 | "#; |
| 1349 | let comp = Comp::from_fusion_str(src).expect("parse Text3D comp"); |
| 1350 | let Ok(mut eng) = Engine::new_headless() else { |
| 1351 | return; |
| 1352 | }; |
| 1353 | let r3d = comp.find_node("R3D").unwrap(); |
| 1354 | let txt = comp.find_node("Txt").unwrap(); |
| 1355 | |
| 1356 | let (_, s0) = eng.evaluate(&comp, r3d, 0.0); |
| 1357 | assert_eq!(s0.cache_misses, 1); |
| 1358 | assert_eq!(eng.scene3d.gpu_mesh_count(), 1); |
| 1359 | |
| 1360 | // Identical second evaluation hits Renderer3D output cache and skips CPU/GPU rebuilds. |
| 1361 | let (_, s0_repeat) = eng.evaluate(&comp, r3d, 0.0); |
| 1362 | assert_eq!(s0_repeat.cache_hits, 1); |
| 1363 | assert_eq!(s0_repeat.cache_misses, 0); |
| 1364 | |
| 1365 | // Moving the camera invalidates Renderer3D output, yet reuses the exact same Text3D mesh key, |
| 1366 | // cached Arc, and GPU mesh LRU entry. |
| 1367 | let (_, s1) = eng.evaluate(&comp, r3d, 1.0); |
| 1368 | assert_eq!(s1.cache_misses, 1); |
| 1369 | assert_eq!(eng.scene3d.gpu_mesh_count(), 1); |
| 1370 | |
| 1371 | let ti0 = super::resolve_text_inputs(&comp, comp.node(txt), 0.0, true, &eng.expr); |
| 1372 | let ti1 = super::resolve_text_inputs(&comp, comp.node(txt), 1.0, true, &eng.expr); |
| 1373 | let mk0 = ti0.geometry_hash(0.2, 0.0, 0.0); |
| 1374 | let mk1 = ti1.geometry_hash(0.2, 0.0, 0.0); |
| 1375 | assert_eq!(mk0, mk1); |
| 1376 | let a0 = eng.scene3d.get_or_build_text3d_mesh(mk0, || panic!("mesh must be cached")); |
| 1377 | let a1 = eng.scene3d.get_or_build_text3d_mesh(mk1, || panic!("mesh must be cached")); |
| 1378 | assert!(std::sync::Arc::ptr_eq(&a0, &a1)); |
| 1379 | } |
| 1380 | |
| 1381 | #[test] |
| 1382 | fn border_fill_applies_opacity2_once() { |
| 1383 | let src = r#" |
| 1384 | Composition { |
| 1385 | CurrentTime = 0, |
| 1386 | RenderRange = { 0, 1 }, |
| 1387 | GlobalRange = { 0, 1 }, |
| 1388 | Tools = ordered() { |
| 1389 | T1 = TextPlus { |
| 1390 | Inputs = { |
| 1391 | Width = Input { Value = 128, }, |
| 1392 | Height = Input { Value = 128, }, |
| 1393 | StyledText = Input { Value = "I", }, |
| 1394 | Size = Input { Value = 0.25, }, |
| 1395 | Red1 = Input { Value = 1.0, }, |
| 1396 | Green1 = Input { Value = 1.0, }, |
| 1397 | Blue1 = Input { Value = 1.0, }, |
| 1398 | Alpha1 = Input { Value = 1.0, }, |
| 1399 | Enabled2 = Input { Value = 1, }, |
| 1400 | ElementShape2 = Input { Value = 2, }, |
| 1401 | Red2 = Input { Value = 1.0, }, |
| 1402 | Green2 = Input { Value = 0.0, }, |
| 1403 | Blue2 = Input { Value = 0.0, }, |
| 1404 | Alpha2 = Input { Value = 1.0, }, |
| 1405 | Opacity2 = Input { Value = 0.5, }, |
| 1406 | ExtendHorizontal2 = Input { Value = 1.0, }, |
| 1407 | ExtendVertical2 = Input { Value = 1.0, }, |
| 1408 | }, |
| 1409 | }, |
| 1410 | }, |
| 1411 | } |
| 1412 | "#; |
| 1413 | let comp = Comp::from_fusion_str(src).expect("parse BorderFill comp"); |
| 1414 | let Ok(mut eng) = Engine::new_headless() else { |
| 1415 | return; |
| 1416 | }; |
| 1417 | let t1 = comp.find_node("T1").unwrap(); |
| 1418 | let (tex, _) = eng.evaluate(&comp, t1, 0.0); |
| 1419 | let rgba = eng.readback_rgba(&tex).unwrap(); |
| 1420 | // Pure red BorderFill margin pixels (outside white glyph 'I') have g=0, b=0, and alpha ~128 |
| 1421 | // for Opacity2=0.5 (buggy elem2_opacity^2 gave ~64). |
| 1422 | let box_alpha = rgba |
| 1423 | .chunks_exact(4) |
| 1424 | .filter(|px| px[1] == 0 && px[2] == 0 && px[3] > 0) |
| 1425 | .map(|px| px[3]) |
| 1426 | .max() |
| 1427 | .unwrap_or(0); |
| 1428 | assert!( |
| 1429 | (118..=138).contains(&box_alpha), |
| 1430 | "BorderFill Opacity2=0.5 should yield ~128 alpha, got {box_alpha}" |
| 1431 | ); |
| 1432 | } |
| 1433 | |
| 1434 | #[test] |
| 1435 | fn camera_near_ge_far_renders_empty_and_overscan_shrinks_coverage() { |
| 1436 | let make_ren = |near: f32, overscan: f32| { |
| 1437 | let src = format!( |
| 1438 | r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ |
| 1439 | Sph = Shape3D {{ Inputs = {{ Shape = Input {{ Value = FuID {{ "SurfaceSphereInputs" }}, }}, }}, }}, |
| 1440 | Cam = Camera3D {{ Inputs = {{ ["Transform3DOp.Translate.Z"] = Input {{ Value = 3.0, }}, PerspNearClip = Input {{ Value = {near}, }}, PerspFarClip = Input {{ Value = 100.0, }}, }}, }}, |
| 1441 | M = Merge3D {{ Inputs = {{ SceneInput1 = Input {{ SourceOp = "Sph", Source = "Output", }}, SceneInput2 = Input {{ SourceOp = "Cam", Source = "Output", }}, }}, }}, |
| 1442 | R = Renderer3D {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, Overscan = Input {{ Value = {overscan}, }}, SceneInput = Input {{ SourceOp = "M", Source = "Output", }}, }}, }}, |
| 1443 | }} }}"# |
| 1444 | ); |
| 1445 | let comp = Comp::from_fusion_str(&src).unwrap(); |
| 1446 | let mut eng = Engine::new_headless().ok()?; |
| 1447 | let r = comp.find_node("R").unwrap(); |
| 1448 | let (tex, _) = eng.evaluate(&comp, r, 0.0); |
| 1449 | eng.readback_rgba(&tex).ok() |
| 1450 | }; |
| 1451 | let (Some(empty), Some(normal), Some(wide)) = (make_ren(200.0, 1.0), make_ren(0.1, 1.0), make_ren(0.1, 2.5)) else { |
| 1452 | return; |
| 1453 | }; |
| 1454 | let cov = |px: &[u8]| px.chunks_exact(4).filter(|c| c[3] > 0).count(); |
| 1455 | assert_eq!(cov(&empty), 0, "near >= far must render empty"); |
| 1456 | assert!(cov(&normal) > cov(&wide), "overscan=2.5 must shrink projected sphere: {} vs {}", cov(&normal), cov(&wide)); |
| 1457 | } |
| 1458 | |
| 1459 | #[test] |
| 1460 | fn text3d_character_size_x_widens_glyphs_without_moving_baseline() { |
| 1461 | let render_txt = |csx: f32| { |
| 1462 | let src = format!( |
| 1463 | r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ |
| 1464 | T = Text3D {{ Inputs = {{ StyledText = Input {{ Value = "HI", }}, Size = Input {{ Value = 0.5, }}, CharacterSizeX = Input {{ Value = {csx}, }}, }}, }}, |
| 1465 | Cam = Camera3D {{ Inputs = {{ ["Transform3DOp.Translate.Z"] = Input {{ Value = 3.0, }}, }}, }}, |
| 1466 | M = Merge3D {{ Inputs = {{ SceneInput1 = Input {{ SourceOp = "T", Source = "Output", }}, SceneInput2 = Input {{ SourceOp = "Cam", Source = "Output", }}, }}, }}, |
| 1467 | R = Renderer3D {{ Inputs = {{ Width = Input {{ Value = 128, }}, Height = Input {{ Value = 128, }}, SceneInput = Input {{ SourceOp = "M", Source = "Output", }}, }}, }}, |
| 1468 | }} }}"# |
| 1469 | ); |
| 1470 | let comp = Comp::from_fusion_str(&src).unwrap(); |
| 1471 | let mut eng = Engine::new_headless().ok()?; |
| 1472 | let r = comp.find_node("R").unwrap(); |
| 1473 | let (tex, _) = eng.evaluate(&comp, r, 0.0); |
| 1474 | eng.readback_rgba(&tex).ok() |
| 1475 | }; |
| 1476 | let (Some(base), Some(wide)) = (render_txt(1.0), render_txt(2.5)) else { |
| 1477 | return; |
| 1478 | }; |
| 1479 | let cov = |px: &[u8]| px.chunks_exact(4).filter(|c| c[3] > 0).count(); |
| 1480 | assert!(cov(&wide) > cov(&base) + 10, "CharacterSizeX=2.5 should increase coverage: {} vs {}", cov(&wide), cov(&base)); |
| 1481 | } |
| 1482 | |
| 1483 | #[test] |
| 1484 | fn text_plus_tight_dod_merges_and_handles_outline_and_effect_mask() { |
| 1485 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 1 }, GlobalRange = { 0, 1 }, Tools = ordered() { |
| 1486 | Bg = Background { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, }, |
| 1487 | Rect = RectangleMask { Inputs = { MaskWidth = Input { Value = 128, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, }, }, |
| 1488 | Txt = TextPlus { |
| 1489 | Inputs = { |
| 1490 | Width = Input { Value = 128, }, |
| 1491 | Height = Input { Value = 64, }, |
| 1492 | StyledText = Input { Value = "HI", }, |
| 1493 | Size = Input { Value = 0.25, }, |
| 1494 | }, |
| 1495 | }, |
| 1496 | TxtEmpty = TextPlus { |
| 1497 | Inputs = { |
| 1498 | Width = Input { Value = 128, }, |
| 1499 | Height = Input { Value = 64, }, |
| 1500 | StyledText = Input { Value = "", }, |
| 1501 | Size = Input { Value = 0.25, }, |
| 1502 | }, |
| 1503 | }, |
| 1504 | TxtMasked = TextPlus { |
| 1505 | Inputs = { |
| 1506 | Width = Input { Value = 128, }, |
| 1507 | Height = Input { Value = 64, }, |
| 1508 | StyledText = Input { Value = "HI", }, |
| 1509 | Size = Input { Value = 0.25, }, |
| 1510 | EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, |
| 1511 | }, |
| 1512 | }, |
| 1513 | M = Merge { |
| 1514 | Inputs = { |
| 1515 | Background = Input { SourceOp = "Bg", Source = "Output", }, |
| 1516 | Foreground = Input { SourceOp = "Txt", Source = "Output", }, |
| 1517 | }, |
| 1518 | }, |
| 1519 | ED = ErodeDilate { |
| 1520 | Inputs = { |
| 1521 | XAmount = Input { Value = 0.05, }, |
| 1522 | Input = Input { SourceOp = "TxtMasked", Source = "Output", }, |
| 1523 | }, |
| 1524 | }, |
| 1525 | } }"#; |
| 1526 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1527 | let Ok(mut eng) = Engine::new_headless() else { return }; |
| 1528 | let (t_m, _) = eng.evaluate(&comp, comp.find_node("M").unwrap(), 0.0); |
| 1529 | let px_m = eng.readback_rgba(&t_m).unwrap(); |
| 1530 | assert!(px_m.chunks_exact(4).any(|c| c[0] > 200 && c[3] == 255), "merged tight TextPlus must render white text"); |
| 1531 | let (t_e, _) = eng.evaluate(&comp, comp.find_node("TxtEmpty").unwrap(), 0.0); |
| 1532 | assert_eq!(t_e.size, [128, 64]); |
| 1533 | let px_e = eng.readback_rgba(&t_e).unwrap(); |
| 1534 | assert!(px_e.iter().all(|&b| b == 0), "empty TextPlus must be transparent"); |
| 1535 | let (t_ed, _) = eng.evaluate(&comp, comp.find_node("ED").unwrap(), 0.0); |
| 1536 | assert_eq!(t_ed.size, [128, 64]); |
| 1537 | let px_ed = eng.readback_rgba(&t_ed).unwrap(); |
| 1538 | let cov_ed = px_ed.chunks_exact(4).filter(|c| c[3] > 200).count(); |
| 1539 | let (t_tm, _) = eng.evaluate(&comp, comp.find_node("TxtMasked").unwrap(), 0.0); |
| 1540 | let px_tm = eng.readback_rgba(&t_tm).unwrap(); |
| 1541 | let cov_tm = px_tm.chunks_exact(4).filter(|c| c[3] > 200).count(); |
| 1542 | assert!(cov_ed > cov_tm + 50, "ErodeDilate downstream of TextPlus must dilate using canvas width: {cov_ed} vs {cov_tm}"); |
| 1543 | } |
| 1544 | |
| 1545 | #[test] |
| 1546 | fn offscreen_right_text3d_with_char_size_x_does_not_panic() { |
| 1547 | let (sz, orig, px) = super::scale_text_chars_x([8, 8], [150, 10], [128, 128], vec![255; 8 * 8 * 4], 1.5); |
| 1548 | assert_eq!(sz, [1, 1]); |
| 1549 | assert_eq!(orig, [0, 0]); |
| 1550 | assert_eq!(px, vec![0; 4]); |
| 1551 | |
| 1552 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 1 }, GlobalRange = { 0, 1 }, Tools = ordered() { |
| 1553 | T = Text3D { Inputs = { |
| 1554 | StyledText = Input { Value = "OFF", }, |
| 1555 | Size = Input { Value = 0.25, }, |
| 1556 | Center = Input { Value = { 1.5, 0.5 }, }, |
| 1557 | CharacterSizeX = Input { Value = 1.5, }, |
| 1558 | }, }, |
| 1559 | Cam = Camera3D { Inputs = { ["Transform3DOp.Translate.Z"] = Input { Value = 3.0, }, }, }, |
| 1560 | M = Merge3D { Inputs = { SceneInput1 = Input { SourceOp = "T", Source = "Output", }, SceneInput2 = Input { SourceOp = "Cam", Source = "Output", }, }, }, |
| 1561 | R = Renderer3D { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, SceneInput = Input { SourceOp = "M", Source = "Output", }, }, }, |
| 1562 | } }"#; |
| 1563 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1564 | let Ok(mut eng) = Engine::new_headless() else { return }; |
| 1565 | let (tex, _) = eng.evaluate(&comp, comp.find_node("R").unwrap(), 0.0); |
| 1566 | assert_eq!(tex.size, [128, 128]); |
| 1567 | } |
| 1568 | |
| 1569 | #[test] |
| 1570 | fn text_plus_feeding_merge_background_and_time_speed_gets_canvas_aligned_output() { |
| 1571 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 5 }, GlobalRange = { 0, 5 }, Tools = ordered() { |
| 1572 | TxtBg = TextPlus { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 64, }, StyledText = Input { Value = "BG", }, Size = Input { Value = 0.2, }, }, }, |
| 1573 | TxtFg = TextPlus { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 64, }, StyledText = Input { Value = "FG", }, Size = Input { Value = 0.2, }, }, }, |
| 1574 | M = Merge { Inputs = { |
| 1575 | Background = Input { SourceOp = "TxtBg", Source = "Output", }, |
| 1576 | Foreground = Input { SourceOp = "TxtFg", Source = "Output", }, |
| 1577 | }, }, |
| 1578 | TxtTime = TextPlus { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 64, }, StyledText = Input { Value = "TM", }, Size = Input { Value = 0.2, }, }, }, |
| 1579 | P1 = PipeRouter { Inputs = { Input = Input { SourceOp = "TxtTime", Source = "Output", }, }, }, |
| 1580 | P2 = PipeRouter { Inputs = { Input = Input { SourceOp = "P1", Source = "Output", }, }, }, |
| 1581 | P3 = PipeRouter { Inputs = { Input = Input { SourceOp = "P2", Source = "Output", }, }, }, |
| 1582 | P4 = PipeRouter { Inputs = { Input = Input { SourceOp = "P3", Source = "Output", }, }, }, |
| 1583 | Sw = Fuse.Switch { Inputs = { Input1 = Input { SourceOp = "P4", Source = "Output", }, }, }, |
| 1584 | TS = TimeSpeed { Inputs = { Speed = Input { Value = 1.0, }, Input = Input { SourceOp = "Sw", Source = "Output", }, }, }, |
| 1585 | } }"#; |
| 1586 | let comp = Comp::from_fusion_str(src).unwrap(); |
| 1587 | assert!(super::needs_canvas_aligned_text_output(&comp, comp.node(comp.find_node("TxtBg").unwrap()))); |
| 1588 | assert!(!super::needs_canvas_aligned_text_output(&comp, comp.node(comp.find_node("TxtFg").unwrap()))); |
| 1589 | assert!(super::needs_canvas_aligned_text_output(&comp, comp.node(comp.find_node("TxtTime").unwrap()))); |
| 1590 | |
| 1591 | let Ok(mut eng) = Engine::new_headless() else { return }; |
| 1592 | let mut enc = eng.device.create_command_encoder(&Default::default()); |
| 1593 | let mut visiting = std::collections::HashSet::new(); |
| 1594 | let mut stats = super::EvalStats::default(); |
| 1595 | let t_bg = eng.eval_node(&comp, comp.find_node("TxtBg").unwrap(), 0.0, &mut enc, &mut visiting, &mut stats); |
| 1596 | assert_eq!(t_bg.origin, [0, 0]); |
| 1597 | assert_eq!(t_bg.size, [128, 64]); |
| 1598 | let t_fg = eng.eval_node(&comp, comp.find_node("TxtFg").unwrap(), 0.0, &mut enc, &mut visiting, &mut stats); |
| 1599 | assert!(t_fg.has_dod(), "TextPlus feeding only Merge.Foreground keeps tight DoD"); |
| 1600 | let t_time = eng.eval_node(&comp, comp.find_node("TxtTime").unwrap(), 0.0, &mut enc, &mut visiting, &mut stats); |
| 1601 | assert_eq!(t_time.origin, [0, 0]); |
| 1602 | assert_eq!(t_time.size, [128, 64]); |
| 1603 | eng.queue.submit(Some(enc.finish())); |
| 1604 | } |
| 1605 | } |