| author | |
| committer | |
| log | c0680fd15d2bb1083422d70356d3d8a28cbeb609 |
| tree | e259c0f88d0b7ca2610b87e1ee3c046db9fe59bf |
| parent | bdc13296e484339dfda2fee378da66be86175a75 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
3 files changed, 805 insertions(+), 114 deletions(-)
crates/engine/src/eval/masks.rs+363-21| ... | @@ -253,24 +253,81 @@ impl Engine { | ... | @@ -253,24 +253,81 @@ impl Engine { |
| 253 | 253 | ||
| 254 | let soft_px = soft * size[0] as f32; | 254 | let soft_px = soft * size[0] as f32; |
| 255 | let need_post = soft_px >= 0.5 || invert || (level - 1.0).abs() > 1e-4 || prev_mask.is_some(); | 255 | let need_post = soft_px >= 0.5 || invert || (level - 1.0).abs() > 1e-4 || prev_mask.is_some(); |
| 256 | let pixels = rasterize_polyline_mask( | 256 | let raw_tex = self.alloc_tex(size, BitDepth::Int8, if need_post { 0 } else { key }); |
| 257 | size, | 257 | |
| 258 | &points, | 258 | let has_coverage = match polyline_tight_commands(size, &points, [center[0] as f32, center[1] as f32], angle) { |
| 259 | [center[0] as f32, center[1] as f32], | 259 | Some(([bx0, by0, bx1, by1], cmds)) if raw_tex.size == size => { |
| 260 | angle, | 260 | let bw = (bx1 - bx0) as u32; |
| 261 | if need_post { false } else { invert }, | 261 | let bh = (by1 - by0) as u32; |
| 262 | if need_post { 1.0 } else { level }, | 262 | let mut cov_buf = vec![0u8; (bw as usize) * (bh as usize)]; |
| 263 | ); | 263 | let mut scratch = zeno::Scratch::new(); |
| 264 | let post = need_post.then_some(MaskPost { | 264 | let _ = zeno::Mask::with_scratch(&cmds, &mut scratch) |
| 265 | soft_px, | 265 | .size(bw, bh) |
| 266 | is_box, | 266 | .origin(zeno::Origin::TopLeft) |
| 267 | clamp_edges, | 267 | .render_into(&mut cov_buf, None); |
| 268 | paint_mode, | 268 | |
| 269 | invert, | 269 | let eff_level = if need_post { 1.0 } else { level }; |
| 270 | level, | 270 | let mut rgba = vec![0u8; cov_buf.len() * 4]; |
| 271 | prev: prev_mask.as_ref(), | 271 | if (eff_level - 1.0).abs() <= 1e-6 { |
| 272 | }); | 272 | for (dst, &cov) in rgba.chunks_exact_mut(4).zip(cov_buf.iter()) { |
| 273 | let tex = self.upload_cpu_mask(enc, size, key, &pixels, post); | 273 | dst.fill(cov); |
| 274 | } | ||
| 275 | } else { | ||
| 276 | for (dst, &cov) in rgba.chunks_exact_mut(4).zip(cov_buf.iter()) { | ||
| 277 | let b = (cov as f32 * eff_level).clamp(0.0, 255.0) as u8; | ||
| 278 | dst.fill(b); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | if bw == size[0] && bh == size[1] { | ||
| 283 | self.queue.write_texture( | ||
| 284 | raw_tex.texture.as_image_copy(), | ||
| 285 | &rgba, | ||
| 286 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(bw * 4), rows_per_image: Some(bh) }, | ||
| 287 | wgpu::Extent3d { width: bw, height: bh, depth_or_array_layers: 1 }, | ||
| 288 | ); | ||
| 289 | } else { | ||
| 290 | let small_tex = self.alloc_tex([bw, bh], BitDepth::Int8, 0); | ||
| 291 | self.queue.write_texture( | ||
| 292 | small_tex.texture.as_image_copy(), | ||
| 293 | &rgba, | ||
| 294 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(bw * 4), rows_per_image: Some(bh) }, | ||
| 295 | wgpu::Extent3d { width: bw, height: bh, depth_or_array_layers: 1 }, | ||
| 296 | ); | ||
| 297 | clear_texture_transparent(enc, &raw_tex.view); | ||
| 298 | enc.copy_texture_to_texture( | ||
| 299 | small_tex.texture.as_image_copy(), | ||
| 300 | wgpu::TexelCopyTextureInfo { | ||
| 301 | texture: &raw_tex.texture, | ||
| 302 | mip_level: 0, | ||
| 303 | origin: wgpu::Origin3d { x: bx0 as u32, y: by0 as u32, z: 0 }, | ||
| 304 | aspect: wgpu::TextureAspect::All, | ||
| 305 | }, | ||
| 306 | wgpu::Extent3d { width: bw, height: bh, depth_or_array_layers: 1 }, | ||
| 307 | ); | ||
| 308 | } | ||
| 309 | true | ||
| 310 | } | ||
| 311 | _ => { | ||
| 312 | clear_texture_transparent(enc, &raw_tex.view); | ||
| 313 | false | ||
| 314 | } | ||
| 315 | }; | ||
| 316 | |||
| 317 | let tex = if need_post { | ||
| 318 | let post = MaskPost { | ||
| 319 | soft_px: if has_coverage { soft_px } else { 0.0 }, | ||
| 320 | is_box, | ||
| 321 | clamp_edges, | ||
| 322 | paint_mode, | ||
| 323 | invert, | ||
| 324 | level, | ||
| 325 | prev: prev_mask.as_ref(), | ||
| 326 | }; | ||
| 327 | self.apply_mask_post(enc, size, key, raw_tex, post) | ||
| 328 | } else { | ||
| 329 | raw_tex | ||
| 330 | }; | ||
| 274 | self.cache.insert(key, tex.clone(), node.id, frame_i); | 331 | self.cache.insert(key, tex.clone(), node.id, frame_i); |
| 275 | tex | 332 | tex |
| 276 | } | 333 | } |
| ... | @@ -314,20 +371,33 @@ impl Engine { | ... | @@ -314,20 +371,33 @@ impl Engine { |
| 314 | pixels: &[u8], | 371 | pixels: &[u8], |
| 315 | post: Option<MaskPost>, | 372 | post: Option<MaskPost>, |
| 316 | ) -> CachedTexture { | 373 | ) -> CachedTexture { |
| 317 | let raw_tex = create_texture(&self.device, size, if post.is_some() { 0 } else { key }); | 374 | let raw_tex = self.alloc_tex(size, BitDepth::Int8, if post.is_some() { 0 } else { key }); |
| 318 | self.queue.write_texture( | 375 | self.queue.write_texture( |
| 319 | raw_tex.texture.as_image_copy(), | 376 | raw_tex.texture.as_image_copy(), |
| 320 | pixels, | 377 | pixels, |
| 321 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(size[0] * 4), rows_per_image: Some(size[1]) }, | 378 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(size[0] * 4), rows_per_image: Some(size[1]) }, |
| 322 | wgpu::Extent3d { width: size[0], height: size[1], depth_or_array_layers: 1 }, | 379 | wgpu::Extent3d { width: size[0], height: size[1], depth_or_array_layers: 1 }, |
| 323 | ); | 380 | ); |
| 324 | let Some(p) = post else { return raw_tex }; | 381 | match post { |
| 382 | Some(p) => self.apply_mask_post(enc, size, key, raw_tex, p), | ||
| 383 | None => raw_tex, | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | fn apply_mask_post( | ||
| 388 | &self, | ||
| 389 | enc: &mut wgpu::CommandEncoder, | ||
| 390 | size: [u32; 2], | ||
| 391 | key: u64, | ||
| 392 | raw_tex: CachedTexture, | ||
| 393 | p: MaskPost, | ||
| 394 | ) -> CachedTexture { | ||
| 325 | let blurred = if p.soft_px >= 0.5 { | 395 | let blurred = if p.soft_px >= 0.5 { |
| 326 | self.blur_mask_texture(enc, &raw_tex, p.soft_px, p.is_box, p.clamp_edges) | 396 | self.blur_mask_texture(enc, &raw_tex, p.soft_px, p.is_box, p.clamp_edges) |
| 327 | } else { | 397 | } else { |
| 328 | raw_tex | 398 | raw_tex |
| 329 | }; | 399 | }; |
| 330 | let combined = create_texture(&self.device, size, key); | 400 | let combined = self.alloc_tex(size, BitDepth::Int8, key); |
| 331 | let mut u = UniformParams::default(); | 401 | let mut u = UniformParams::default(); |
| 332 | u.p[0] = [p.paint_mode, if p.invert { 1.0 } else { 0.0 }, p.level, if p.prev.is_some() { 1.0 } else { 0.0 }]; | 402 | u.p[0] = [p.paint_mode, if p.invert { 1.0 } else { 0.0 }, p.level, if p.prev.is_some() { 1.0 } else { 0.0 }]; |
| 333 | let mv = p.prev.map_or(&self.dummy.view, |m| &m.view); | 403 | let mv = p.prev.map_or(&self.dummy.view, |m| &m.view); |
| ... | @@ -740,6 +810,122 @@ fn wand_flood_fill( | ... | @@ -740,6 +810,122 @@ fn wand_flood_fill( |
| 740 | out | 810 | out |
| 741 | } | 811 | } |
| 742 | 812 | ||
| 813 | fn clear_texture_transparent(enc: &mut wgpu::CommandEncoder, view: &wgpu::TextureView) { | ||
| 814 | let _pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { | ||
| 815 | label: Some("mask-clear"), | ||
| 816 | color_attachments: &[Some(wgpu::RenderPassColorAttachment { | ||
| 817 | view, | ||
| 818 | depth_slice: None, | ||
| 819 | resolve_target: None, | ||
| 820 | ops: wgpu::Operations { | ||
| 821 | load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), | ||
| 822 | store: wgpu::StoreOp::Store, | ||
| 823 | }, | ||
| 824 | })], | ||
| 825 | depth_stencil_attachment: None, | ||
| 826 | timestamp_writes: None, | ||
| 827 | occlusion_query_set: None, | ||
| 828 | multiview_mask: None, | ||
| 829 | }); | ||
| 830 | } | ||
| 831 | |||
| 832 | /// Builds `zeno` commands for a `PolylineMask` shifted into its tight pixel bounding box | ||
| 833 | /// `[bx0, by0, bx1, by1]` clamped to `[0..w, 0..h]`, returning `None` if empty or off-canvas. | ||
| 834 | fn polyline_tight_commands( | ||
| 835 | size: [u32; 2], | ||
| 836 | points: &[model::PolyPoint], | ||
| 837 | center: [f32; 2], | ||
| 838 | angle_deg: f32, | ||
| 839 | ) -> Option<([i32; 4], Vec<zeno::Command>)> { | ||
| 840 | if points.len() < 2 || size[0] == 0 || size[1] == 0 { | ||
| 841 | return None; | ||
| 842 | } | ||
| 843 | let [w, h] = size; | ||
| 844 | let aspect = w as f32 / (h as f32).max(1.0); | ||
| 845 | let (s, c) = angle_deg.to_radians().sin_cos(); | ||
| 846 | let to_px = |x: f64, y: f64| -> [f32; 2] { | ||
| 847 | let (x, y) = (x as f32, y as f32 / aspect); | ||
| 848 | let (rx, ry) = (x * c - y * s, (x * s + y * c) * aspect); | ||
| 849 | [(center[0] + rx) * w as f32, (1.0 - (center[1] + ry)) * h as f32] | ||
| 850 | }; | ||
| 851 | |||
| 852 | let mut min_x = f32::INFINITY; | ||
| 853 | let mut min_y = f32::INFINITY; | ||
| 854 | let mut max_x = f32::NEG_INFINITY; | ||
| 855 | let mut max_y = f32::NEG_INFINITY; | ||
| 856 | let mut grow = |p: [f32; 2]| { | ||
| 857 | if p[0].is_finite() && p[1].is_finite() { | ||
| 858 | min_x = min_x.min(p[0]); | ||
| 859 | min_y = min_y.min(p[1]); | ||
| 860 | max_x = max_x.max(p[0]); | ||
| 861 | max_y = max_y.max(p[1]); | ||
| 862 | } | ||
| 863 | }; | ||
| 864 | |||
| 865 | let mut cmds = Vec::with_capacity(points.len() + 2); | ||
| 866 | let p0 = to_px(points[0].x, points[0].y); | ||
| 867 | grow(p0); | ||
| 868 | cmds.push(zeno::Command::MoveTo(zeno::Vector::new(p0[0], p0[1]))); | ||
| 869 | for i in 0..points.len() { | ||
| 870 | let a = &points[i]; | ||
| 871 | let b = &points[(i + 1) % points.len()]; | ||
| 872 | let p = to_px(b.x, b.y); | ||
| 873 | grow(p); | ||
| 874 | if a.linear && b.linear { | ||
| 875 | cmds.push(zeno::Command::LineTo(zeno::Vector::new(p[0], p[1]))); | ||
| 876 | } else { | ||
| 877 | let c1 = to_px(a.x + a.rx, a.y + a.ry); | ||
| 878 | let c2 = to_px(b.x + b.lx, b.y + b.ly); | ||
| 879 | grow(c1); | ||
| 880 | grow(c2); | ||
| 881 | cmds.push(zeno::Command::CurveTo( | ||
| 882 | zeno::Vector::new(c1[0], c1[1]), | ||
| 883 | zeno::Vector::new(c2[0], c2[1]), | ||
| 884 | zeno::Vector::new(p[0], p[1]), | ||
| 885 | )); | ||
| 886 | } | ||
| 887 | } | ||
| 888 | cmds.push(zeno::Command::Close); | ||
| 889 | |||
| 890 | if min_x > max_x || min_y > max_y { | ||
| 891 | return None; | ||
| 892 | } | ||
| 893 | let wi = w as i32; | ||
| 894 | let hi = h as i32; | ||
| 895 | let bx0 = (min_x.floor() as i32).saturating_sub(2).clamp(0, wi); | ||
| 896 | let by0 = (min_y.floor() as i32).saturating_sub(2).clamp(0, hi); | ||
| 897 | let bx1 = (max_x.ceil() as i32).saturating_add(2).clamp(0, wi); | ||
| 898 | let by1 = (max_y.ceil() as i32).saturating_add(2).clamp(0, hi); | ||
| 899 | if bx1 <= bx0 || by1 <= by0 { | ||
| 900 | return None; | ||
| 901 | } | ||
| 902 | |||
| 903 | if bx0 != 0 || by0 != 0 { | ||
| 904 | let ox = bx0 as f32; | ||
| 905 | let oy = by0 as f32; | ||
| 906 | let shift = |v: &mut zeno::Vector| { | ||
| 907 | v.x -= ox; | ||
| 908 | v.y -= oy; | ||
| 909 | }; | ||
| 910 | for cmd in &mut cmds { | ||
| 911 | match cmd { | ||
| 912 | zeno::Command::MoveTo(p) | zeno::Command::LineTo(p) => shift(p), | ||
| 913 | zeno::Command::QuadTo(c, p) => { | ||
| 914 | shift(c); | ||
| 915 | shift(p); | ||
| 916 | } | ||
| 917 | zeno::Command::CurveTo(c1, c2, p) => { | ||
| 918 | shift(c1); | ||
| 919 | shift(c2); | ||
| 920 | shift(p); | ||
| 921 | } | ||
| 922 | zeno::Command::Close => {} | ||
| 923 | } | ||
| 924 | } | ||
| 925 | } | ||
| 926 | Some(([bx0, by0, bx1, by1], cmds)) | ||
| 927 | } | ||
| 928 | |||
| 743 | #[cfg(test)] | 929 | #[cfg(test)] |
| 744 | mod tests { | 930 | mod tests { |
| 745 | use super::*; | 931 | use super::*; |
| ... | @@ -974,4 +1160,160 @@ mod tests { | ... | @@ -974,4 +1160,160 @@ mod tests { |
| 974 | let wand_chain = eng.readback_rgba(&t_wc).unwrap(); | 1160 | let wand_chain = eng.readback_rgba(&t_wc).unwrap(); |
| 975 | assert_eq!(wand_chain[3], 255); | 1161 | assert_eq!(wand_chain[3], 255); |
| 976 | } | 1162 | } |
| 1163 | |||
| 1164 | #[test] | ||
| 1165 | fn polyline_mask_tight_bbox_matches_full_canvas_across_invert_soft_edge_and_oob() { | ||
| 1166 | let comp_src = r#"{ | ||
| 1167 | Tools = ordered() { | ||
| 1168 | PolySmall = PolylineMask { | ||
| 1169 | Inputs = { | ||
| 1170 | MaskWidth = Input { Value = 96, }, MaskHeight = Input { Value = 64, }, | ||
| 1171 | Center = Input { Value = { 0.65, 0.35 }, }, | ||
| 1172 | Angle = Input { Value = 22.0, }, | ||
| 1173 | Polyline = Input { | ||
| 1174 | Value = Polyline { | ||
| 1175 | Closed = true, | ||
| 1176 | Points = { | ||
| 1177 | { Linear = true, X = -0.1, Y = -0.1 }, | ||
| 1178 | { Linear = true, X = 0.12, Y = -0.05 }, | ||
| 1179 | { Linear = true, X = 0.08, Y = 0.12 }, | ||
| 1180 | { Linear = true, X = -0.08, Y = 0.1 }, | ||
| 1181 | } | ||
| 1182 | }, | ||
| 1183 | }, | ||
| 1184 | }, | ||
| 1185 | }, | ||
| 1186 | PolyInvertLevel = PolylineMask { | ||
| 1187 | Inputs = { | ||
| 1188 | MaskWidth = Input { Value = 96, }, MaskHeight = Input { Value = 64, }, | ||
| 1189 | Center = Input { Value = { 0.5, 0.5 }, }, | ||
| 1190 | Invert = Input { Value = 1, }, | ||
| 1191 | Level = Input { Value = 0.75, }, | ||
| 1192 | Polyline = Input { | ||
| 1193 | Value = Polyline { | ||
| 1194 | Closed = true, | ||
| 1195 | Points = { | ||
| 1196 | { Linear = true, X = -0.15, Y = -0.15 }, | ||
| 1197 | { Linear = true, X = 0.15, Y = -0.15 }, | ||
| 1198 | { Linear = true, X = 0.15, Y = 0.15 }, | ||
| 1199 | { Linear = true, X = -0.15, Y = 0.15 }, | ||
| 1200 | } | ||
| 1201 | }, | ||
| 1202 | }, | ||
| 1203 | }, | ||
| 1204 | }, | ||
| 1205 | PolySoft = PolylineMask { | ||
| 1206 | Inputs = { | ||
| 1207 | MaskWidth = Input { Value = 96, }, MaskHeight = Input { Value = 64, }, | ||
| 1208 | SoftEdge = Input { Value = 0.06, }, | ||
| 1209 | ClippingMode = Input { Value = FuID { "None" }, }, | ||
| 1210 | Polyline = Input { | ||
| 1211 | Value = Polyline { | ||
| 1212 | Closed = true, | ||
| 1213 | Points = { | ||
| 1214 | { Linear = true, X = -0.1, Y = -0.1 }, | ||
| 1215 | { Linear = true, X = 0.1, Y = -0.1 }, | ||
| 1216 | { Linear = true, X = 0.1, Y = 0.1 }, | ||
| 1217 | { Linear = true, X = -0.1, Y = 0.1 }, | ||
| 1218 | } | ||
| 1219 | }, | ||
| 1220 | }, | ||
| 1221 | }, | ||
| 1222 | }, | ||
| 1223 | PolyPartialOob = PolylineMask { | ||
| 1224 | Inputs = { | ||
| 1225 | MaskWidth = Input { Value = 96, }, MaskHeight = Input { Value = 64, }, | ||
| 1226 | Polyline = Input { | ||
| 1227 | Value = Polyline { | ||
| 1228 | Closed = true, | ||
| 1229 | Points = { | ||
| 1230 | { Linear = true, X = -0.8, Y = -0.2 }, | ||
| 1231 | { Linear = true, X = -0.2, Y = -0.2 }, | ||
| 1232 | { Linear = true, X = -0.2, Y = 0.2 }, | ||
| 1233 | { Linear = true, X = -0.8, Y = 0.2 }, | ||
| 1234 | } | ||
| 1235 | }, | ||
| 1236 | }, | ||
| 1237 | }, | ||
| 1238 | }, | ||
| 1239 | PolyAllOob = PolylineMask { | ||
| 1240 | Inputs = { | ||
| 1241 | MaskWidth = Input { Value = 96, }, MaskHeight = Input { Value = 64, }, | ||
| 1242 | Polyline = Input { | ||
| 1243 | Value = Polyline { | ||
| 1244 | Closed = true, | ||
| 1245 | Points = { | ||
| 1246 | { Linear = true, X = 1.2, Y = 0.0 }, | ||
| 1247 | { Linear = true, X = 1.5, Y = 0.0 }, | ||
| 1248 | { Linear = true, X = 1.5, Y = 0.3 }, | ||
| 1249 | { Linear = true, X = 1.2, Y = 0.3 }, | ||
| 1250 | } | ||
| 1251 | }, | ||
| 1252 | }, | ||
| 1253 | }, | ||
| 1254 | }, | ||
| 1255 | PolyCoverAll = PolylineMask { | ||
| 1256 | Inputs = { | ||
| 1257 | MaskWidth = Input { Value = 96, }, MaskHeight = Input { Value = 64, }, | ||
| 1258 | Polyline = Input { | ||
| 1259 | Value = Polyline { | ||
| 1260 | Closed = true, | ||
| 1261 | Points = { | ||
| 1262 | { Linear = true, X = -1.0, Y = -1.0 }, | ||
| 1263 | { Linear = true, X = 1.0, Y = -1.0 }, | ||
| 1264 | { Linear = true, X = 1.0, Y = 1.0 }, | ||
| 1265 | { Linear = true, X = -1.0, Y = 1.0 }, | ||
| 1266 | } | ||
| 1267 | }, | ||
| 1268 | }, | ||
| 1269 | }, | ||
| 1270 | }, | ||
| 1271 | } | ||
| 1272 | }"#; | ||
| 1273 | let comp = Comp::from_fusion_str(comp_src).expect("parse polyline comp"); | ||
| 1274 | let Ok(mut eng) = Engine::new_headless() else { return }; | ||
| 1275 | |||
| 1276 | let check_hard = |eng: &mut Engine, name: &str, center: [f32; 2], angle: f32, invert: bool, level: f32| { | ||
| 1277 | let nid = comp.find_node(name).unwrap(); | ||
| 1278 | let pts = Engine::polyline_points(&comp, comp.node(nid), 0.0); | ||
| 1279 | let expected = rasterize_polyline_mask([96, 64], &pts, center, angle, invert, level); | ||
| 1280 | let (tex, _) = eng.evaluate(&comp, nid, 0.0); | ||
| 1281 | let got = eng.readback_rgba(&tex).unwrap(); | ||
| 1282 | for (i, (&g, &e)) in got.iter().zip(expected.iter()).enumerate() { | ||
| 1283 | assert!(g.abs_diff(e) <= 1, "{name} byte {i}: got {g}, expected {e}"); | ||
| 1284 | } | ||
| 1285 | }; | ||
| 1286 | |||
| 1287 | check_hard(&mut eng, "PolySmall", [0.65, 0.35], 22.0, false, 1.0); | ||
| 1288 | check_hard(&mut eng, "PolyInvertLevel", [0.5, 0.5], 0.0, true, 0.75); | ||
| 1289 | check_hard(&mut eng, "PolyPartialOob", [0.5, 0.5], 0.0, false, 1.0); | ||
| 1290 | check_hard(&mut eng, "PolyAllOob", [0.5, 0.5], 0.0, false, 1.0); | ||
| 1291 | check_hard(&mut eng, "PolyCoverAll", [0.5, 0.5], 0.0, false, 1.0); | ||
| 1292 | |||
| 1293 | // SoftEdge > 0: compare GPU-blurred tight upload against full-canvas upload_cpu_mask. | ||
| 1294 | let soft_id = comp.find_node("PolySoft").unwrap(); | ||
| 1295 | let soft_pts = Engine::polyline_points(&comp, comp.node(soft_id), 0.0); | ||
| 1296 | let full_raw = rasterize_polyline_mask([96, 64], &soft_pts, [0.5, 0.5], 0.0, false, 1.0); | ||
| 1297 | let mut enc = eng.device.create_command_encoder(&Default::default()); | ||
| 1298 | let ref_tex = eng.upload_cpu_mask( | ||
| 1299 | &mut enc, | ||
| 1300 | [96, 64], | ||
| 1301 | 0, | ||
| 1302 | &full_raw, | ||
| 1303 | Some(MaskPost { | ||
| 1304 | soft_px: 0.06 * 96.0, | ||
| 1305 | is_box: false, | ||
| 1306 | clamp_edges: 0.0, | ||
| 1307 | paint_mode: 0.0, | ||
| 1308 | invert: false, | ||
| 1309 | level: 1.0, | ||
| 1310 | prev: None, | ||
| 1311 | }), | ||
| 1312 | ); | ||
| 1313 | eng.queue.submit(Some(enc.finish())); | ||
| 1314 | let ref_px = eng.readback_rgba(&ref_tex).unwrap(); | ||
| 1315 | let (soft_tex, _) = eng.evaluate(&comp, soft_id, 0.0); | ||
| 1316 | let soft_px = eng.readback_rgba(&soft_tex).unwrap(); | ||
| 1317 | assert_eq!(soft_px, ref_px, "SoftEdge PolylineMask must match full-canvas pixel-for-pixel"); | ||
| 1318 | } | ||
| 977 | } | 1319 | } |
crates/engine/src/eval/text_3d.rs+127-34| ... | @@ -301,6 +301,61 @@ fn scale_text_chars_x( | ... | @@ -301,6 +301,61 @@ fn scale_text_chars_x( |
| 301 | ([out_w as u32, tex_size[1]], [out_x0, origin[1]], out) | 301 | ([out_w as u32, tex_size[1]], [out_x0, origin[1]], out) |
| 302 | } | 302 | } |
| 303 | 303 | ||
| 304 | // Keep `eval::mod`'s re-exported `media` symbols referenced across non-test targets without hitting | ||
| 305 | // the legacy full-canvas CPU path. | ||
| 306 | const _: fn([u32; 2], &TextPlusParams<'_>, i32) -> ([u32; 2], [i32; 2], Vec<u8>) = rasterize_text_plus; | ||
| 307 | |||
| 308 | fn needs_canvas_aligned_text_output(comp: &Comp, node: &Node) -> bool { | ||
| 309 | let mut target_names: Vec<&str> = vec![node.name.as_str()]; | ||
| 310 | let mut seen = HashSet::new(); | ||
| 311 | seen.insert(node.id); | ||
| 312 | for _ in 0..4 { | ||
| 313 | let mut added = false; | ||
| 314 | for other in &comp.nodes { | ||
| 315 | if seen.contains(&other.id) { | ||
| 316 | continue; | ||
| 317 | } | ||
| 318 | let reads_target = other | ||
| 319 | .inputs | ||
| 320 | .values() | ||
| 321 | .any(|b| b.source_op.as_deref().is_some_and(|s| target_names.contains(&s))); | ||
| 322 | if !reads_target { | ||
| 323 | continue; | ||
| 324 | } | ||
| 325 | if other.bypassed || matches!(other.kind.as_str(), "PipeRouter" | "Fuse.Wireless") { | ||
| 326 | seen.insert(other.id); | ||
| 327 | target_names.push(other.name.as_str()); | ||
| 328 | added = true; | ||
| 329 | continue; | ||
| 330 | } | ||
| 331 | if matches!( | ||
| 332 | other.kind.as_str(), | ||
| 333 | "Fuse.Outline" | ||
| 334 | | "ErodeDilate" | ||
| 335 | | "Blur" | ||
| 336 | | "Glow" | ||
| 337 | | "SoftGlow" | ||
| 338 | | "InvertColor" | ||
| 339 | | "BrightnessContrast" | ||
| 340 | | "ColorCorrector" | ||
| 341 | | "Fuse.Colorize" | ||
| 342 | | "VariBlur" | ||
| 343 | | "Filter" | ||
| 344 | | "ColorCurves" | ||
| 345 | | "HueCurves" | ||
| 346 | | "ColorGain" | ||
| 347 | | "Custom" | ||
| 348 | ) { | ||
| 349 | return true; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | if !added { | ||
| 353 | break; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | false | ||
| 357 | } | ||
| 358 | |||
| 304 | impl Engine { | 359 | impl Engine { |
| 305 | #[allow(clippy::too_many_arguments)] | 360 | #[allow(clippy::too_many_arguments)] |
| 306 | pub(super) fn eval_p_render( | 361 | pub(super) fn eval_p_render( |
| ... | @@ -443,7 +498,7 @@ impl Engine { | ... | @@ -443,7 +498,7 @@ impl Engine { |
| 443 | time: f64, | 498 | time: f64, |
| 444 | frame_i: i64, | 499 | frame_i: i64, |
| 445 | comp_dims: [u32; 2], | 500 | comp_dims: [u32; 2], |
| 446 | _enc: &mut wgpu::CommandEncoder, | 501 | enc: &mut wgpu::CommandEncoder, |
| 447 | stats: &mut EvalStats, | 502 | stats: &mut EvalStats, |
| 448 | is_text3d: bool, | 503 | is_text3d: bool, |
| 449 | ) -> CachedTexture { | 504 | ) -> CachedTexture { |
| ... | @@ -455,12 +510,15 @@ impl Engine { | ... | @@ -455,12 +510,15 @@ impl Engine { |
| 455 | 510 | ||
| 456 | let ti = resolve_text_inputs(comp, node, time, is_text3d, &self.expr); | 511 | let ti = resolve_text_inputs(comp, node, time, is_text3d, &self.expr); |
| 457 | let is_border_fill = ti.outline_enabled && ti.elem2_shape == 2; | 512 | let is_border_fill = ti.outline_enabled && ti.elem2_shape == 2; |
| 513 | let has_mask = Self::resolve_effect_mask_id(comp, node).is_some(); | ||
| 514 | let need_canvas = is_text3d || has_mask || needs_canvas_aligned_text_output(comp, node); | ||
| 458 | 515 | ||
| 459 | let mut kh = DefaultHasher::new(); | 516 | let mut kh = DefaultHasher::new(); |
| 460 | self.cache.gpu_salt.hash(&mut kh); | 517 | self.cache.gpu_salt.hash(&mut kh); |
| 461 | self.cache.node_salt(node.id).hash(&mut kh); | 518 | self.cache.node_salt(node.id).hash(&mut kh); |
| 462 | "TextPlus".hash(&mut kh); | 519 | "TextPlus".hash(&mut kh); |
| 463 | is_text3d.hash(&mut kh); | 520 | is_text3d.hash(&mut kh); |
| 521 | need_canvas.hash(&mut kh); | ||
| 464 | ti.baseline_at_center.hash(&mut kh); | 522 | ti.baseline_at_center.hash(&mut kh); |
| 465 | size.hash(&mut kh); | 523 | size.hash(&mut kh); |
| 466 | ti.text_str.hash(&mut kh); | 524 | ti.text_str.hash(&mut kh); |
| ... | @@ -508,39 +566,7 @@ impl Engine { | ... | @@ -508,39 +566,7 @@ impl Engine { |
| 508 | 0 | 566 | 0 |
| 509 | }; | 567 | }; |
| 510 | let rich = ti.as_rich_params(); | 568 | let rich = ti.as_rich_params(); |
| 511 | let has_soft = ti.soft_fill != [0.0; 3] || ti.soft_outline != [0.0; 3]; | 569 | let (tex_size, origin, pixels) = crate::text_engine::rasterize_text_plus_rich(size, &rich, border_pad_px); |
| 512 | let (tex_size, origin, pixels) = if !is_text3d && ti.cls_rules.is_empty() && !has_soft { | ||
| 513 | let is_text_outline = ti.outline_enabled && ti.elem2_shape != 2; | ||
| 514 | let outline_width_px = if is_text_outline && ti.outline_thickness > 1e-4 { | ||
| 515 | (ti.outline_thickness * size_px).clamp(2.0, 56.0) | ||
| 516 | } else { | ||
| 517 | 0.0 | ||
| 518 | }; | ||
| 519 | rasterize_text_plus( | ||
| 520 | size, | ||
| 521 | &TextPlusParams { | ||
| 522 | text: &ti.text_str, | ||
| 523 | font_family: &ti.font_family, | ||
| 524 | font_style: &ti.font_style, | ||
| 525 | size_norm: ti.size_norm, | ||
| 526 | tracking: ti.tracking, | ||
| 527 | line_spacing: ti.line_spacing, | ||
| 528 | center: ti.center, | ||
| 529 | h_lcr: ti.h_lcr, | ||
| 530 | v_tcb: ti.v_tcb, | ||
| 531 | fill_rgba: ti.fill_rgba, | ||
| 532 | write_on_start: ti.write_start, | ||
| 533 | write_on_end: ti.write_end, | ||
| 534 | shear_x: ti.shear_x, | ||
| 535 | cls: &[], | ||
| 536 | outline_rgba: rich.outline_rgba, | ||
| 537 | outline_width_px, | ||
| 538 | }, | ||
| 539 | border_pad_px, | ||
| 540 | ) | ||
| 541 | } else { | ||
| 542 | crate::text_engine::rasterize_text_plus_rich(size, &rich, border_pad_px) | ||
| 543 | }; | ||
| 544 | let (tex_size, origin, mut pixels) = if is_text3d && (ti.char_size_x - 1.0).abs() > 1e-4 { | 570 | let (tex_size, origin, mut pixels) = if is_text3d && (ti.char_size_x - 1.0).abs() > 1e-4 { |
| 545 | scale_text_chars_x(tex_size, origin, size, pixels, ti.char_size_x) | 571 | scale_text_chars_x(tex_size, origin, size, pixels, ti.char_size_x) |
| 546 | } else { | 572 | } else { |
| ... | @@ -794,6 +820,11 @@ impl Engine { | ... | @@ -794,6 +820,11 @@ impl Engine { |
| 794 | }, | 820 | }, |
| 795 | ); | 821 | ); |
| 796 | 822 | ||
| 823 | let final_tex = if need_canvas && final_tex.has_dod() { | ||
| 824 | self.crop_to_canvas(enc, final_tex) | ||
| 825 | } else { | ||
| 826 | final_tex | ||
| 827 | }; | ||
| 797 | self.cache.insert(key, final_tex.clone(), node.id, frame_i); | 828 | self.cache.insert(key, final_tex.clone(), node.id, frame_i); |
| 798 | final_tex | 829 | final_tex |
| 799 | } | 830 | } |
| ... | @@ -1401,4 +1432,66 @@ Composition { | ... | @@ -1401,4 +1432,66 @@ Composition { |
| 1401 | let cov = |px: &[u8]| px.chunks_exact(4).filter(|c| c[3] > 0).count(); | 1432 | let cov = |px: &[u8]| px.chunks_exact(4).filter(|c| c[3] > 0).count(); |
| 1402 | assert!(cov(&wide) > cov(&base) + 10, "CharacterSizeX=2.5 should increase coverage: {} vs {}", cov(&wide), cov(&base)); | 1433 | assert!(cov(&wide) > cov(&base) + 10, "CharacterSizeX=2.5 should increase coverage: {} vs {}", cov(&wide), cov(&base)); |
| 1403 | } | 1434 | } |
| 1435 | |||
| 1436 | #[test] | ||
| 1437 | fn text_plus_tight_dod_merges_and_handles_outline_and_effect_mask() { | ||
| 1438 | let src = r#"Composition { CurrentTime = 0, RenderRange = { 0, 1 }, GlobalRange = { 0, 1 }, Tools = ordered() { | ||
| 1439 | Bg = Background { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, }, | ||
| 1440 | Rect = RectangleMask { Inputs = { MaskWidth = Input { Value = 128, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, }, }, | ||
| 1441 | Txt = TextPlus { | ||
| 1442 | Inputs = { | ||
| 1443 | Width = Input { Value = 128, }, | ||
| 1444 | Height = Input { Value = 64, }, | ||
| 1445 | StyledText = Input { Value = "HI", }, | ||
| 1446 | Size = Input { Value = 0.25, }, | ||
| 1447 | }, | ||
| 1448 | }, | ||
| 1449 | TxtEmpty = TextPlus { | ||
| 1450 | Inputs = { | ||
| 1451 | Width = Input { Value = 128, }, | ||
| 1452 | Height = Input { Value = 64, }, | ||
| 1453 | StyledText = Input { Value = "", }, | ||
| 1454 | Size = Input { Value = 0.25, }, | ||
| 1455 | }, | ||
| 1456 | }, | ||
| 1457 | TxtMasked = TextPlus { | ||
| 1458 | Inputs = { | ||
| 1459 | Width = Input { Value = 128, }, | ||
| 1460 | Height = Input { Value = 64, }, | ||
| 1461 | StyledText = Input { Value = "HI", }, | ||
| 1462 | Size = Input { Value = 0.25, }, | ||
| 1463 | EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, | ||
| 1464 | }, | ||
| 1465 | }, | ||
| 1466 | M = Merge { | ||
| 1467 | Inputs = { | ||
| 1468 | Background = Input { SourceOp = "Bg", Source = "Output", }, | ||
| 1469 | Foreground = Input { SourceOp = "Txt", Source = "Output", }, | ||
| 1470 | }, | ||
| 1471 | }, | ||
| 1472 | ED = ErodeDilate { | ||
| 1473 | Inputs = { | ||
| 1474 | XAmount = Input { Value = 0.05, }, | ||
| 1475 | Input = Input { SourceOp = "TxtMasked", Source = "Output", }, | ||
| 1476 | }, | ||
| 1477 | }, | ||
| 1478 | } }"#; | ||
| 1479 | let comp = Comp::from_fusion_str(src).unwrap(); | ||
| 1480 | let Ok(mut eng) = Engine::new_headless() else { return }; | ||
| 1481 | let (t_m, _) = eng.evaluate(&comp, comp.find_node("M").unwrap(), 0.0); | ||
| 1482 | let px_m = eng.readback_rgba(&t_m).unwrap(); | ||
| 1483 | assert!(px_m.chunks_exact(4).any(|c| c[0] > 200 && c[3] == 255), "merged tight TextPlus must render white text"); | ||
| 1484 | let (t_e, _) = eng.evaluate(&comp, comp.find_node("TxtEmpty").unwrap(), 0.0); | ||
| 1485 | assert_eq!(t_e.size, [128, 64]); | ||
| 1486 | let px_e = eng.readback_rgba(&t_e).unwrap(); | ||
| 1487 | assert!(px_e.iter().all(|&b| b == 0), "empty TextPlus must be transparent"); | ||
| 1488 | let (t_ed, _) = eng.evaluate(&comp, comp.find_node("ED").unwrap(), 0.0); | ||
| 1489 | assert_eq!(t_ed.size, [128, 64]); | ||
| 1490 | let px_ed = eng.readback_rgba(&t_ed).unwrap(); | ||
| 1491 | let cov_ed = px_ed.chunks_exact(4).filter(|c| c[3] > 200).count(); | ||
| 1492 | let (t_tm, _) = eng.evaluate(&comp, comp.find_node("TxtMasked").unwrap(), 0.0); | ||
| 1493 | let px_tm = eng.readback_rgba(&t_tm).unwrap(); | ||
| 1494 | let cov_tm = px_tm.chunks_exact(4).filter(|c| c[3] > 200).count(); | ||
| 1495 | assert!(cov_ed > cov_tm + 50, "ErodeDilate downstream of TextPlus must dilate using canvas width: {cov_ed} vs {cov_tm}"); | ||
| 1496 | } | ||
| 1404 | } | 1497 | } |
crates/engine/src/text_engine.rs+315-59| ... | @@ -657,71 +657,118 @@ fn gaussian_kernel_1d(sigma: f32) -> (isize, Vec<f32>) { | ... | @@ -657,71 +657,118 @@ fn gaussian_kernel_1d(sigma: f32) -> (isize, Vec<f32>) { |
| 657 | 657 | ||
| 658 | fn apply_separable_gaussian(plane: &mut [f32], w: usize, h: usize, sigma_x: f32, sigma_y: f32) { | 658 | fn apply_separable_gaussian(plane: &mut [f32], w: usize, h: usize, sigma_x: f32, sigma_y: f32) { |
| 659 | let (rx, kx) = gaussian_kernel_1d(sigma_x); | 659 | let (rx, kx) = gaussian_kernel_1d(sigma_x); |
| 660 | if rx > 0 { | 660 | let (ry, ky) = gaussian_kernel_1d(sigma_y); |
| 661 | let mut tmp = vec![0.0_f32; w * h]; | 661 | if rx <= 0 && ry <= 0 { |
| 662 | return; | ||
| 663 | } | ||
| 664 | let mut tmp = vec![0.0_f32; w * h]; | ||
| 665 | let src_is_tmp = if rx > 0 { | ||
| 666 | let r = rx as usize; | ||
| 667 | let klen = kx.len(); | ||
| 668 | let mid_end = w.saturating_sub(r); | ||
| 669 | let left_end = r.min(w); | ||
| 670 | let right_start = mid_end.max(left_end); | ||
| 662 | for y in 0..h { | 671 | for y in 0..h { |
| 663 | let row = &plane[y * w..(y + 1) * w]; | 672 | let row = &plane[y * w..(y + 1) * w]; |
| 664 | let dst = &mut tmp[y * w..(y + 1) * w]; | 673 | let dst = &mut tmp[y * w..(y + 1) * w]; |
| 665 | for x in 0..w { | 674 | for x in 0..left_end { |
| 675 | let k_start = r - x; | ||
| 676 | let k_end = (w + r - x).min(klen); | ||
| 677 | let s_start = x + k_start - r; | ||
| 666 | let mut acc = 0.0_f32; | 678 | let mut acc = 0.0_f32; |
| 667 | let xi = x as isize; | 679 | for (&s, &kw) in row[s_start..s_start + (k_end - k_start)].iter().zip(&kx[k_start..k_end]) { |
| 668 | for (ki, &kw) in kx.iter().enumerate() { | 680 | acc += s * kw; |
| 669 | let sx = xi + (ki as isize - rx); | 681 | } |
| 670 | if sx >= 0 && sx < w as isize { | 682 | dst[x] = acc; |
| 671 | acc += row[sx as usize] * kw; | 683 | } |
| 672 | } | 684 | for x in left_end..mid_end { |
| 685 | let win = &row[x - r..x + r + 1]; | ||
| 686 | let mut acc = 0.0_f32; | ||
| 687 | for (&s, &kw) in win.iter().zip(kx.iter()) { | ||
| 688 | acc += s * kw; | ||
| 689 | } | ||
| 690 | dst[x] = acc; | ||
| 691 | } | ||
| 692 | for x in right_start..w { | ||
| 693 | let k_end = w + r - x; | ||
| 694 | let mut acc = 0.0_f32; | ||
| 695 | for (&s, &kw) in row[x - r..w].iter().zip(&kx[..k_end]) { | ||
| 696 | acc += s * kw; | ||
| 673 | } | 697 | } |
| 674 | dst[x] = acc; | 698 | dst[x] = acc; |
| 675 | } | 699 | } |
| 676 | } | 700 | } |
| 677 | plane.copy_from_slice(&tmp); | 701 | true |
| 678 | } | 702 | } else { |
| 679 | let (ry, ky) = gaussian_kernel_1d(sigma_y); | 703 | false |
| 704 | }; | ||
| 705 | |||
| 680 | if ry > 0 { | 706 | if ry > 0 { |
| 681 | let mut tmp = vec![0.0_f32; w * h]; | 707 | let (src_buf, dst_buf): (&[f32], &mut [f32]) = if src_is_tmp { |
| 708 | (&tmp, plane) | ||
| 709 | } else { | ||
| 710 | tmp.copy_from_slice(plane); | ||
| 711 | (&tmp, plane) | ||
| 712 | }; | ||
| 713 | dst_buf.fill(0.0); | ||
| 714 | let klen = ky.len() as isize; | ||
| 682 | for y in 0..h { | 715 | for y in 0..h { |
| 683 | let yi = y as isize; | 716 | let yi = y as isize; |
| 684 | for x in 0..w { | 717 | let k_start = (ry - yi).max(0) as usize; |
| 685 | let mut acc = 0.0_f32; | 718 | let k_end = ((h as isize + ry - yi).min(klen)).max(0) as usize; |
| 686 | for (ki, &kw) in ky.iter().enumerate() { | 719 | let dst_row = &mut dst_buf[y * w..(y + 1) * w]; |
| 687 | let sy = yi + (ki as isize - ry); | 720 | for (ki, &kw) in ky[k_start..k_end].iter().enumerate() { |
| 688 | if sy >= 0 && sy < h as isize { | 721 | let sy = (yi + (k_start + ki) as isize - ry) as usize; |
| 689 | acc += plane[sy as usize * w + x] * kw; | 722 | let src_row = &src_buf[sy * w..(sy + 1) * w]; |
| 690 | } | 723 | for (d, &s) in dst_row.iter_mut().zip(src_row.iter()) { |
| 724 | *d += s * kw; | ||
| 691 | } | 725 | } |
| 692 | tmp[y * w + x] = acc; | ||
| 693 | } | 726 | } |
| 694 | } | 727 | } |
| 728 | } else if src_is_tmp { | ||
| 695 | plane.copy_from_slice(&tmp); | 729 | plane.copy_from_slice(&tmp); |
| 696 | } | 730 | } |
| 697 | } | 731 | } |
| 698 | 732 | ||
| 699 | fn blit_plane_max(c: &Coverage, origin: [i32; 2], tw: usize, th: usize, plane: &mut [f32]) { | 733 | fn blit_plane_max(c: &Coverage, origin: [i32; 2], tw: usize, th: usize, plane: &mut [f32]) { |
| 700 | for y in 0..c.height as i32 { | 734 | let cw = c.width as i32; |
| 701 | let py = c.top + y - origin[1]; | 735 | let ch = c.height as i32; |
| 702 | if py < 0 || py >= th as i32 { | 736 | let x0 = (origin[0] - c.left).max(0); |
| 703 | continue; | 737 | let y0 = (origin[1] - c.top).max(0); |
| 704 | } | 738 | let x1 = (origin[0] + tw as i32 - c.left).min(cw); |
| 705 | for x in 0..c.width as i32 { | 739 | let y1 = (origin[1] + th as i32 - c.top).min(ch); |
| 706 | let px = c.left + x - origin[0]; | 740 | if x0 >= x1 || y0 >= y1 { |
| 707 | let a = c.data[(y as u32 * c.width + x as u32) as usize]; | 741 | return; |
| 708 | if a > 0 && px >= 0 && px < tw as i32 { | 742 | } |
| 709 | let idx = py as usize * tw + px as usize; | 743 | let span = (x1 - x0) as usize; |
| 710 | plane[idx] = plane[idx].max(a as f32 / 255.0); | 744 | let dst_x0 = (c.left + x0 - origin[0]) as usize; |
| 745 | for y in y0..y1 { | ||
| 746 | let py = (c.top + y - origin[1]) as usize; | ||
| 747 | let src_start = y as usize * c.width as usize + x0 as usize; | ||
| 748 | let src_row = &c.data[src_start..src_start + span]; | ||
| 749 | let dst_start = py * tw + dst_x0; | ||
| 750 | let dst_row = &mut plane[dst_start..dst_start + span]; | ||
| 751 | for (d, &a) in dst_row.iter_mut().zip(src_row.iter()) { | ||
| 752 | if a > 0 { | ||
| 753 | *d = d.max(a as f32 / 255.0); | ||
| 711 | } | 754 | } |
| 712 | } | 755 | } |
| 713 | } | 756 | } |
| 714 | } | 757 | } |
| 715 | 758 | ||
| 716 | /// Extended `TextPlus` / `Text3D` rasterizer with `StyledTextCLS` per-character styling and | 759 | /// Extended `TextPlus` / `Text3D` rasterizer with `StyledTextCLS` per-character styling and |
| 717 | /// Element 1/2 Softness blur + glow. | 760 | /// Element 1/2 Softness blur + glow. Returns a tight-DoD `(tex_size, origin, rgba)` buffer. |
| 718 | pub fn rasterize_text_plus_rich( | 761 | pub fn rasterize_text_plus_rich( |
| 719 | dims: [u32; 2], | 762 | dims: [u32; 2], |
| 720 | params: &RichTextPlusParams<'_>, | 763 | params: &RichTextPlusParams<'_>, |
| 721 | dod_pad: i32, | 764 | dod_pad: i32, |
| 722 | ) -> ([u32; 2], [i32; 2], Vec<u8>) { | 765 | ) -> ([u32; 2], [i32; 2], Vec<u8>) { |
| 723 | let [w, h] = dims; | 766 | let [w, h] = dims; |
| 724 | let empty = || (dims, [0, 0], vec![0u8; (w as usize) * (h as usize) * 4]); | 767 | let empty = || ([1u32, 1u32], [0i32, 0i32], vec![0u8; 4]); |
| 768 | let has_outline = params.outline_enabled && params.outline_rgba[3] > 0.0; | ||
| 769 | if params.fill_rgba[3] <= 0.0 && !has_outline && dod_pad <= 0 { | ||
| 770 | return empty(); | ||
| 771 | } | ||
| 725 | let Some((glyphs, _)) = layout_glyphs(dims, params) else { | 772 | let Some((glyphs, _)) = layout_glyphs(dims, params) else { |
| 726 | return empty(); | 773 | return empty(); |
| 727 | }; | 774 | }; |
| ... | @@ -737,24 +784,25 @@ pub fn rasterize_text_plus_rich( | ... | @@ -737,24 +784,25 @@ pub fn rasterize_text_plus_rich( |
| 737 | let sx2 = clamp_sigma(params.soft_outline[0]); | 784 | let sx2 = clamp_sigma(params.soft_outline[0]); |
| 738 | let sy2 = clamp_sigma(params.soft_outline[1]); | 785 | let sy2 = clamp_sigma(params.soft_outline[1]); |
| 739 | let blur_pad = (sx1.max(sy1).max(sx2).max(sy2) * 3.0).ceil().clamp(0.0, 64.0) as i32; | 786 | let blur_pad = (sx1.max(sy1).max(sx2).max(sy2) * 3.0).ceil().clamp(0.0, 64.0) as i32; |
| 740 | let eff_pad = dod_pad.clamp(0, 4096).max(blur_pad); | 787 | // Keep at least a 1px transparent apron around glyphs so GPU bilinear filtering never clamps non-zero edge alpha. |
| 788 | let eff_pad = dod_pad.clamp(0, 4096).max(blur_pad).max(1); | ||
| 741 | 789 | ||
| 742 | let (mut min_x, mut min_y, mut max_x, mut max_y) = (0i32, 0i32, w as i32, h as i32); | 790 | let (mut min_x, mut min_y, mut max_x, mut max_y) = (i32::MAX, i32::MAX, i32::MIN, i32::MIN); |
| 743 | let mut grow = |c: &Coverage, pad: i32| { | 791 | let mut grow = |c: &Coverage, pad: i32| { |
| 744 | min_x = min_x.min(c.left - pad); | 792 | min_x = min_x.min(c.left.saturating_sub(pad)); |
| 745 | min_y = min_y.min(c.top - pad); | 793 | min_y = min_y.min(c.top.saturating_sub(pad)); |
| 746 | max_x = max_x.max(c.left + c.width as i32 + pad); | 794 | max_x = max_x.max((c.left + c.width as i32).saturating_add(pad)); |
| 747 | max_y = max_y.max(c.top + c.height as i32 + pad); | 795 | max_y = max_y.max((c.top + c.height as i32).saturating_add(pad)); |
| 748 | }; | 796 | }; |
| 749 | 797 | ||
| 750 | let has_outline = params.outline_enabled && params.outline_rgba[3] > 0.0; | ||
| 751 | let mut placed = Vec::with_capacity(glyphs.len()); | 798 | let mut placed = Vec::with_capacity(glyphs.len()); |
| 752 | let mut any_outline = false; | 799 | let mut any_outline = false; |
| 753 | for g in &glyphs { | 800 | for g in &glyphs { |
| 754 | let f = fill(&g.commands); | 801 | let f = fill(&g.commands); |
| 755 | let l = if has_outline && g.outline_w > 1e-3 { | 802 | let l = if has_outline && g.outline_w > 1e-3 { |
| 756 | any_outline = true; | 803 | let s = stroke(&g.commands, g.outline_w); |
| 757 | stroke(&g.commands, g.outline_w) | 804 | any_outline |= s.is_some(); |
| 805 | s | ||
| 758 | } else { | 806 | } else { |
| 759 | None | 807 | None |
| 760 | }; | 808 | }; |
| ... | @@ -766,13 +814,19 @@ pub fn rasterize_text_plus_rich( | ... | @@ -766,13 +814,19 @@ pub fn rasterize_text_plus_rich( |
| 766 | } | 814 | } |
| 767 | placed.push((f, l)); | 815 | placed.push((f, l)); |
| 768 | } | 816 | } |
| 817 | if min_x > max_x || min_y > max_y { | ||
| 818 | return empty(); | ||
| 819 | } | ||
| 769 | 820 | ||
| 770 | let origin_x = min_x.max(-(w as i32)); | 821 | let origin_x = min_x.max(-(w as i32)); |
| 771 | let origin_y = min_y.max(-(h as i32)); | 822 | let origin_y = min_y.max(-(h as i32)); |
| 772 | let end_x = max_x.min((w as i32) * 2); | 823 | let end_x = max_x.min((w as i32) * 2); |
| 773 | let end_y = max_y.min((h as i32) * 2); | 824 | let end_y = max_y.min((h as i32) * 2); |
| 774 | let tw = (end_x - origin_x).max(1) as usize; | 825 | if end_x <= origin_x || end_y <= origin_y { |
| 775 | let th = (end_y - origin_y).max(1) as usize; | 826 | return empty(); |
| 827 | } | ||
| 828 | let tw = (end_x - origin_x) as usize; | ||
| 829 | let th = (end_y - origin_y) as usize; | ||
| 776 | 830 | ||
| 777 | let mut fill_plane = vec![0.0_f32; tw * th]; | 831 | let mut fill_plane = vec![0.0_f32; tw * th]; |
| 778 | let mut line_plane = vec![0.0_f32; if any_outline { tw * th } else { 0 }]; | 832 | let mut line_plane = vec![0.0_f32; if any_outline { tw * th } else { 0 }]; |
| ... | @@ -796,22 +850,33 @@ pub fn rasterize_text_plus_rich( | ... | @@ -796,22 +850,33 @@ pub fn rasterize_text_plus_rich( |
| 796 | let glow2 = 1.0 / (1.0 - params.soft_outline[2].clamp(0.0, 0.999)); | 850 | let glow2 = 1.0 / (1.0 - params.soft_outline[2].clamp(0.0, 0.999)); |
| 797 | let (fc, oc) = (params.fill_rgba, params.outline_rgba); | 851 | let (fc, oc) = (params.fill_rgba, params.outline_rgba); |
| 798 | let mut out = vec![0u8; tw * th * 4]; | 852 | let mut out = vec![0u8; tw * th * 4]; |
| 799 | for (i, px) in out.chunks_exact_mut(4).enumerate() { | 853 | if any_outline { |
| 800 | let fa = (fill_plane[i] * fc[3] * glow1).clamp(0.0, 1.0); | 854 | let f_scale = fc[3] * glow1; |
| 801 | let oa = if any_outline { | 855 | let o_scale = oc[3] * glow2; |
| 802 | (line_plane[i] * oc[3] * glow2).clamp(0.0, 1.0) | 856 | for (i, px) in out.chunks_exact_mut(4).enumerate() { |
| 803 | } else { | 857 | let fa = (fill_plane[i] * f_scale).clamp(0.0, 1.0); |
| 804 | 0.0 | 858 | let oa = (line_plane[i] * o_scale).clamp(0.0, 1.0); |
| 805 | }; | 859 | if fa <= 0.0 && oa <= 0.0 { |
| 806 | if fa <= 0.0 && oa <= 0.0 { | 860 | continue; |
| 807 | continue; | 861 | } |
| 862 | let under = 1.0 - fa; | ||
| 863 | px[0] = ((fc[0] * fa + oc[0] * oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 864 | px[1] = ((fc[1] * fa + oc[1] * oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 865 | px[2] = ((fc[2] * fa + oc[2] * oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 866 | px[3] = ((fa + oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 808 | } | 867 | } |
| 809 | let under = 1.0 - fa; | 868 | } else { |
| 810 | for k in 0..3 { | 869 | let f_scale = fc[3] * glow1; |
| 811 | let v = fc[k] * fa + oc[k] * oa * under; | 870 | for (i, px) in out.chunks_exact_mut(4).enumerate() { |
| 812 | px[k] = (v * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | 871 | let fa = (fill_plane[i] * f_scale).clamp(0.0, 1.0); |
| 872 | if fa <= 0.0 { | ||
| 873 | continue; | ||
| 874 | } | ||
| 875 | px[0] = (fc[0] * fa * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 876 | px[1] = (fc[1] * fa * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 877 | px[2] = (fc[2] * fa * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 878 | px[3] = (fa * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 813 | } | 879 | } |
| 814 | px[3] = ((fa + oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | ||
| 815 | } | 880 | } |
| 816 | ([tw as u32, th as u32], [origin_x, origin_y], out) | 881 | ([tw as u32, th as u32], [origin_x, origin_y], out) |
| 817 | } | 882 | } |
| ... | @@ -1700,4 +1765,195 @@ mod tests { | ... | @@ -1700,4 +1765,195 @@ mod tests { |
| 1700 | let resolved = Font::find("FissionRuntimeAliasFont", "Regular").expect("registered alias font"); | 1765 | let resolved = Font::find("FissionRuntimeAliasFont", "Regular").expect("registered alias font"); |
| 1701 | assert!(resolved.face().glyph_index('A').is_some()); | 1766 | assert!(resolved.face().glyph_index('A').is_some()); |
| 1702 | } | 1767 | } |
| 1768 | |||
| 1769 | #[test] | ||
| 1770 | fn tight_dod_empty_whitespace_and_zero_alpha_return_1x1_transparent() { | ||
| 1771 | let mut p = RichTextPlusParams { | ||
| 1772 | text: "", | ||
| 1773 | font_family: "Liberation Sans", | ||
| 1774 | font_style: "Regular", | ||
| 1775 | size_norm: 0.08, | ||
| 1776 | tracking: 1.0, | ||
| 1777 | line_spacing: 1.0, | ||
| 1778 | center: [0.5, 0.5], | ||
| 1779 | h_lcr: 0.0, | ||
| 1780 | v_tcb: 0.0, | ||
| 1781 | fill_rgba: [1.0; 4], | ||
| 1782 | write_on_start: 0.0, | ||
| 1783 | write_on_end: 1.0, | ||
| 1784 | shear_x: 0.0, | ||
| 1785 | cls: &[], | ||
| 1786 | outline_rgba: [0.0; 4], | ||
| 1787 | outline_thickness_norm: 0.0, | ||
| 1788 | outline_enabled: false, | ||
| 1789 | soft_fill: [0.0; 3], | ||
| 1790 | soft_outline: [0.0; 3], | ||
| 1791 | baseline_at_center: false, | ||
| 1792 | }; | ||
| 1793 | assert_eq!(rasterize_text_plus_rich([640, 360], &p, 0), ([1, 1], [0, 0], vec![0, 0, 0, 0])); | ||
| 1794 | p.text = " "; | ||
| 1795 | assert_eq!(rasterize_text_plus_rich([640, 360], &p, 0), ([1, 1], [0, 0], vec![0, 0, 0, 0])); | ||
| 1796 | p.text = "AB"; | ||
| 1797 | p.write_on_end = 0.0; | ||
| 1798 | assert_eq!(rasterize_text_plus_rich([640, 360], &p, 0), ([1, 1], [0, 0], vec![0, 0, 0, 0])); | ||
| 1799 | p.write_on_end = 1.0; | ||
| 1800 | p.fill_rgba[3] = 0.0; | ||
| 1801 | assert_eq!(rasterize_text_plus_rich([640, 360], &p, 0), ([1, 1], [0, 0], vec![0, 0, 0, 0])); | ||
| 1802 | } | ||
| 1803 | |||
| 1804 | #[test] | ||
| 1805 | fn tight_dod_text_plus_matches_full_canvas_across_cls_outline_and_softness() { | ||
| 1806 | let dims = [640u32, 360u32]; | ||
| 1807 | let composite_onto_canvas = |sz: [u32; 2], orig: [i32; 2], rgba: &[u8]| -> Vec<u8> { | ||
| 1808 | let mut canvas = vec![0u8; (dims[0] * dims[1] * 4) as usize]; | ||
| 1809 | for y in 0..sz[1] as i32 { | ||
| 1810 | let cy = orig[1] + y; | ||
| 1811 | if cy < 0 || cy >= dims[1] as i32 { | ||
| 1812 | continue; | ||
| 1813 | } | ||
| 1814 | for x in 0..sz[0] as i32 { | ||
| 1815 | let cx = orig[0] + x; | ||
| 1816 | if cx < 0 || cx >= dims[0] as i32 { | ||
| 1817 | continue; | ||
| 1818 | } | ||
| 1819 | let si = ((y as u32 * sz[0] + x as u32) * 4) as usize; | ||
| 1820 | let di = ((cy as u32 * dims[0] + cx as u32) * 4) as usize; | ||
| 1821 | canvas[di..di + 4].copy_from_slice(&rgba[si..si + 4]); | ||
| 1822 | } | ||
| 1823 | } | ||
| 1824 | canvas | ||
| 1825 | }; | ||
| 1826 | |||
| 1827 | // 1. Plain + outline vs legacy full-canvas `rasterize_text_plus`. | ||
| 1828 | let p_legacy = TextPlusParams { | ||
| 1829 | text: "Hello", | ||
| 1830 | font_family: "Liberation Sans", | ||
| 1831 | font_style: "Regular", | ||
| 1832 | size_norm: 0.08, | ||
| 1833 | tracking: 1.0, | ||
| 1834 | line_spacing: 1.0, | ||
| 1835 | center: [0.5, 0.5], | ||
| 1836 | h_lcr: 0.0, | ||
| 1837 | v_tcb: 0.0, | ||
| 1838 | fill_rgba: [1.0, 0.9, 0.8, 1.0], | ||
| 1839 | write_on_start: 0.0, | ||
| 1840 | write_on_end: 1.0, | ||
| 1841 | shear_x: 0.0, | ||
| 1842 | cls: &[], | ||
| 1843 | outline_rgba: [0.2, 0.4, 1.0, 0.8], | ||
| 1844 | outline_width_px: (0.04_f32 * 0.08 * 640.0).clamp(1.0, 128.0), | ||
| 1845 | }; | ||
| 1846 | let (lsz, lorig, lrgba) = rasterize_text_plus(dims, &p_legacy, 0); | ||
| 1847 | let p_rich = RichTextPlusParams { | ||
| 1848 | text: "Hello", | ||
| 1849 | font_family: "Liberation Sans", | ||
| 1850 | font_style: "Regular", | ||
| 1851 | size_norm: 0.08, | ||
| 1852 | tracking: 1.0, | ||
| 1853 | line_spacing: 1.0, | ||
| 1854 | center: [0.5, 0.5], | ||
| 1855 | h_lcr: 0.0, | ||
| 1856 | v_tcb: 0.0, | ||
| 1857 | fill_rgba: [1.0, 0.9, 0.8, 1.0], | ||
| 1858 | write_on_start: 0.0, | ||
| 1859 | write_on_end: 1.0, | ||
| 1860 | shear_x: 0.0, | ||
| 1861 | cls: &[], | ||
| 1862 | outline_rgba: [0.2, 0.4, 1.0, 0.8], | ||
| 1863 | outline_thickness_norm: 0.04, | ||
| 1864 | outline_enabled: true, | ||
| 1865 | soft_fill: [0.0; 3], | ||
| 1866 | soft_outline: [0.0; 3], | ||
| 1867 | baseline_at_center: false, | ||
| 1868 | }; | ||
| 1869 | let (tsz, torig, trgba) = rasterize_text_plus_rich(dims, &p_rich, 0); | ||
| 1870 | assert!(tsz[0] < dims[0] && tsz[1] < dims[1], "expected tight DoD, got {tsz:?}"); | ||
| 1871 | assert!(torig[0] > 0 && torig[1] > 0, "expected non-zero tight origin, got {torig:?}"); | ||
| 1872 | let tight_canvas = composite_onto_canvas(tsz, torig, &trgba); | ||
| 1873 | let legacy_canvas = composite_onto_canvas(lsz, lorig, &lrgba); | ||
| 1874 | for (i, (&a, &b)) in tight_canvas.iter().zip(legacy_canvas.iter()).enumerate() { | ||
| 1875 | assert!(a.abs_diff(b) <= 4, "legacy parity diff at byte {i}: {a} vs {b}"); | ||
| 1876 | } | ||
| 1877 | let (psz_plain, porig_plain, prgba_plain) = rasterize_text_plus_rich(dims, &p_rich, 80); | ||
| 1878 | assert_eq!(tight_canvas, composite_onto_canvas(psz_plain, porig_plain, &prgba_plain)); | ||
| 1879 | |||
| 1880 | // 2. CLS offsets + Softness blur: tight bbox matches full-plane blur pixel-for-pixel. | ||
| 1881 | let cls = [ | ||
| 1882 | StyledClsRule { prop_id: 1350, start: 1, end: 1, value: 0.04, text: String::new() }, | ||
| 1883 | StyledClsRule { prop_id: 1351, start: 2, end: 2, value: -0.03, text: String::new() }, | ||
| 1884 | StyledClsRule { prop_id: 102, start: 3, end: 3, value: 0.12, text: String::new() }, | ||
| 1885 | ]; | ||
| 1886 | let p_soft = RichTextPlusParams { | ||
| 1887 | cls: &cls, | ||
| 1888 | soft_fill: [3.0, 2.5, 0.25], | ||
| 1889 | soft_outline: [4.0, 3.0, 0.4], | ||
| 1890 | ..p_rich | ||
| 1891 | }; | ||
| 1892 | let (ssz, sorig, srgba) = rasterize_text_plus_rich(dims, &p_soft, 0); | ||
| 1893 | assert!(ssz[0] < dims[0] && ssz[1] < dims[1], "expected tight soft DoD, got {ssz:?}"); | ||
| 1894 | // Padded extra 40px must yield identical canvas pixels because blur tail already decays to 0 inside 3*sigma. | ||
| 1895 | let (psz, porig, prgba) = rasterize_text_plus_rich(dims, &p_soft, 40); | ||
| 1896 | assert_eq!( | ||
| 1897 | composite_onto_canvas(ssz, sorig, &srgba), | ||
| 1898 | composite_onto_canvas(psz, porig, &prgba) | ||
| 1899 | ); | ||
| 1900 | } | ||
| 1901 | |||
| 1902 | #[test] | ||
| 1903 | fn separable_gaussian_fast_path_matches_naive_reference() { | ||
| 1904 | let w = 29usize; | ||
| 1905 | let h = 23usize; | ||
| 1906 | let mut init = vec![0.0_f32; w * h]; | ||
| 1907 | for y in 0..h { | ||
| 1908 | for x in 0..w { | ||
| 1909 | init[y * w + x] = (((x * 7 + y * 13) % 19) as f32) / 18.0; | ||
| 1910 | } | ||
| 1911 | } | ||
| 1912 | let naive = |src: &[f32], sx: f32, sy: f32| -> Vec<f32> { | ||
| 1913 | let mut p = src.to_vec(); | ||
| 1914 | let (rx, kx) = gaussian_kernel_1d(sx); | ||
| 1915 | if rx > 0 { | ||
| 1916 | let mut tmp = vec![0.0_f32; w * h]; | ||
| 1917 | for y in 0..h { | ||
| 1918 | for x in 0..w { | ||
| 1919 | let mut acc = 0.0_f32; | ||
| 1920 | for (ki, &kw) in kx.iter().enumerate() { | ||
| 1921 | let ix = x as isize + ki as isize - rx; | ||
| 1922 | if ix >= 0 && ix < w as isize { | ||
| 1923 | acc += p[y * w + ix as usize] * kw; | ||
| 1924 | } | ||
| 1925 | } | ||
| 1926 | tmp[y * w + x] = acc; | ||
| 1927 | } | ||
| 1928 | } | ||
| 1929 | p = tmp; | ||
| 1930 | } | ||
| 1931 | let (ry, ky) = gaussian_kernel_1d(sy); | ||
| 1932 | if ry > 0 { | ||
| 1933 | let mut tmp = vec![0.0_f32; w * h]; | ||
| 1934 | for y in 0..h { | ||
| 1935 | for x in 0..w { | ||
| 1936 | let mut acc = 0.0_f32; | ||
| 1937 | for (ki, &kw) in ky.iter().enumerate() { | ||
| 1938 | let iy = y as isize + ki as isize - ry; | ||
| 1939 | if iy >= 0 && iy < h as isize { | ||
| 1940 | acc += p[iy as usize * w + x] * kw; | ||
| 1941 | } | ||
| 1942 | } | ||
| 1943 | tmp[y * w + x] = acc; | ||
| 1944 | } | ||
| 1945 | } | ||
| 1946 | p = tmp; | ||
| 1947 | } | ||
| 1948 | p | ||
| 1949 | }; | ||
| 1950 | for (sx, sy) in [(2.3, 1.7), (3.1, 0.0), (0.0, 2.8)] { | ||
| 1951 | let mut fast = init.clone(); | ||
| 1952 | apply_separable_gaussian(&mut fast, w, h, sx, sy); | ||
| 1953 | let ref_buf = naive(&init, sx, sy); | ||
| 1954 | for (a, b) in fast.iter().zip(ref_buf.iter()) { | ||
| 1955 | assert!((a - b).abs() < 1e-6, "mismatch for ({sx}, {sy}): {a} vs {b}"); | ||
| 1956 | } | ||
| 1957 | } | ||
| 1958 | } | ||
| 1703 | } | 1959 | } |