| ... | ... | @@ -389,6 +389,72 @@ fn parse_hex_literal_token(token: &str) -> Option<u32> { |
| 389 | 389 | u32::from_str_radix(hex, 16).ok() |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | #[cfg(not(target_arch = "wasm32"))] |
| 393 | const CRYPTO_PARALLEL_THRESHOLD_PIXELS: usize = 512 * 512; |
| 394 | |
| 395 | fn accumulate_cryptomatte_chunk( |
| 396 | out: &mut [f32], |
| 397 | start: usize, |
| 398 | pairs: &[(&[f32], &[f32])], |
| 399 | target_ids: &HashSet<u32>, |
| 400 | ) { |
| 401 | let len = out.len(); |
| 402 | for &(ids, covs) in pairs { |
| 403 | let ids_slice = &ids[start..start + len]; |
| 404 | let covs_slice = &covs[start..start + len]; |
| 405 | for i in 0..len { |
| 406 | if target_ids.contains(&ids_slice[i].to_bits()) { |
| 407 | out[i] += covs_slice[i]; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | for v in out { |
| 412 | *v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | fn accumulate_cryptomatte_scalar( |
| 417 | pairs: &[(&[f32], &[f32])], |
| 418 | target_ids: &HashSet<u32>, |
| 419 | pixel_count: usize, |
| 420 | ) -> Vec<f32> { |
| 421 | let mut matte = vec![0.0_f32; pixel_count]; |
| 422 | if !target_ids.is_empty() && !pairs.is_empty() { |
| 423 | accumulate_cryptomatte_chunk(&mut matte, 0, pairs, target_ids); |
| 424 | } |
| 425 | matte |
| 426 | } |
| 427 | |
| 428 | /// Accumulates Cryptomatte `(id, coverage)` rank pairs into a clamped `[0.0, 1.0]` matte, |
| 429 | /// splitting across bounded worker threads for `>= 512 * 512` images on native targets. |
| 430 | fn accumulate_cryptomatte_matte( |
| 431 | pairs: &[(&[f32], &[f32])], |
| 432 | target_ids: &HashSet<u32>, |
| 433 | pixel_count: usize, |
| 434 | ) -> Vec<f32> { |
| 435 | if target_ids.is_empty() || pairs.is_empty() { |
| 436 | return vec![0.0_f32; pixel_count]; |
| 437 | } |
| 438 | #[cfg(not(target_arch = "wasm32"))] |
| 439 | if pixel_count >= CRYPTO_PARALLEL_THRESHOLD_PIXELS { |
| 440 | let workers = std::thread::available_parallelism() |
| 441 | .map(|n| n.get().clamp(2, 8)) |
| 442 | .unwrap_or(4); |
| 443 | let chunk_len = pixel_count.div_ceil(workers).max(1); |
| 444 | let mut matte = vec![0.0_f32; pixel_count]; |
| 445 | std::thread::scope(|scope| { |
| 446 | let mut offset = 0usize; |
| 447 | for chunk in matte.chunks_mut(chunk_len) { |
| 448 | let start = offset; |
| 449 | offset += chunk.len(); |
| 450 | scope.spawn(move || accumulate_cryptomatte_chunk(chunk, start, pairs, target_ids)); |
| 451 | } |
| 452 | }); |
| 453 | return matte; |
| 454 | } |
| 455 | accumulate_cryptomatte_scalar(pairs, target_ids, pixel_count) |
| 456 | } |
| 457 | |
| 392 | 458 | /// Lists Cryptomatte layer names discovered in an OpenEXR file's header metadata |
| 393 | 459 | /// (falling back to rank-00 channel prefix discovery when metadata is absent). |
| 394 | 460 | pub fn list_cryptomatte_layers(exr_path: &Path) -> Vec<String> { |
| ... | ... | @@ -573,36 +639,24 @@ pub fn extract_cryptomatte( |
| 573 | 639 | |
| 574 | 640 | let (w, h) = dims?; |
| 575 | 641 | let pixel_count = w * h; |
| 576 | | let mut matte = vec![0.0_f32; pixel_count]; |
| 577 | | |
| 578 | 642 | if target_ids.is_empty() { |
| 579 | 643 | return Some(CryptomatteResult { |
| 580 | 644 | size: [w as u32, h as u32], |
| 581 | | matte, |
| 645 | matte: vec![0.0_f32; pixel_count], |
| 582 | 646 | }); |
| 583 | 647 | } |
| 584 | 648 | |
| 585 | 649 | // 5. Accumulate coverage across all rank (ID, coverage) pairs. |
| 650 | let mut pairs: Vec<(&[f32], &[f32])> = Vec::with_capacity(ranks.len() * 2); |
| 586 | 651 | for slots in ranks.values() { |
| 587 | 652 | if let (Some(ids), Some(covs)) = (&slots[0], &slots[1]) { |
| 588 | | for i in 0..pixel_count { |
| 589 | | if target_ids.contains(&ids[i].to_bits()) { |
| 590 | | matte[i] += covs[i]; |
| 591 | | } |
| 592 | | } |
| 653 | pairs.push((ids.as_slice(), covs.as_slice())); |
| 593 | 654 | } |
| 594 | 655 | if let (Some(ids), Some(covs)) = (&slots[2], &slots[3]) { |
| 595 | | for i in 0..pixel_count { |
| 596 | | if target_ids.contains(&ids[i].to_bits()) { |
| 597 | | matte[i] += covs[i]; |
| 598 | | } |
| 599 | | } |
| 656 | pairs.push((ids.as_slice(), covs.as_slice())); |
| 600 | 657 | } |
| 601 | 658 | } |
| 602 | | |
| 603 | | for v in &mut matte { |
| 604 | | *v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; |
| 605 | | } |
| 659 | let matte = accumulate_cryptomatte_matte(&pairs, &target_ids, pixel_count); |
| 606 | 660 | |
| 607 | 661 | Some(CryptomatteResult { |
| 608 | 662 | size: [w as u32, h as u32], |
| ... | ... | @@ -862,4 +916,75 @@ mod tests { |
| 862 | 916 | assert_eq!(parsed_valid.get("Hero_日本語"), Some(&0xa1b2_c3d4)); |
| 863 | 917 | assert_eq!(parsed_valid.get("élan_A"), Some(&0x00ff_00ff)); |
| 864 | 918 | } |
| 919 | |
| 920 | #[test] |
| 921 | fn parallel_cryptomatte_matches_scalar_bit_for_bit() { |
| 922 | let pixel_count = 520 * 520; |
| 923 | let hero_id = crypto_hash_to_float(murmur3_32(b"Hero", 0)); |
| 924 | let prop_id = crypto_hash_to_float(murmur3_32(b"Prop", 0)); |
| 925 | let other_id = crypto_hash_to_float(murmur3_32(b"Other", 0)); |
| 926 | let target_ids: HashSet<u32> = [hero_id.to_bits(), prop_id.to_bits()].into_iter().collect(); |
| 927 | |
| 928 | let mut id0 = vec![0.0_f32; pixel_count]; |
| 929 | let mut cov0 = vec![0.0_f32; pixel_count]; |
| 930 | let mut id1 = vec![0.0_f32; pixel_count]; |
| 931 | let mut cov1 = vec![0.0_f32; pixel_count]; |
| 932 | let mut id2 = vec![0.0_f32; pixel_count]; |
| 933 | let mut cov2 = vec![0.0_f32; pixel_count]; |
| 934 | |
| 935 | for i in 0..pixel_count { |
| 936 | match i % 7 { |
| 937 | 0 => { |
| 938 | id0[i] = hero_id; |
| 939 | cov0[i] = 0.65; |
| 940 | id1[i] = prop_id; |
| 941 | cov1[i] = 0.25; |
| 942 | } |
| 943 | 1 => { |
| 944 | id0[i] = other_id; |
| 945 | cov0[i] = 0.9; |
| 946 | id1[i] = hero_id; |
| 947 | cov1[i] = 0.1; |
| 948 | } |
| 949 | 2 => { |
| 950 | // Clamping > 1.0 across multiple matching ranks |
| 951 | id0[i] = hero_id; |
| 952 | cov0[i] = 0.75; |
| 953 | id1[i] = prop_id; |
| 954 | cov1[i] = 0.5; |
| 955 | } |
| 956 | 3 => { |
| 957 | // NaN coverage must sanitize to 0.0 identically |
| 958 | id0[i] = hero_id; |
| 959 | cov0[i] = f32::NAN; |
| 960 | } |
| 961 | 4 => { |
| 962 | id0[i] = other_id; |
| 963 | cov0[i] = 1.0; |
| 964 | id2[i] = prop_id; |
| 965 | cov2[i] = 0.375; |
| 966 | } |
| 967 | _ => {} |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | let pairs: Vec<(&[f32], &[f32])> = vec![ |
| 972 | (&id0, &cov0), |
| 973 | (&id1, &cov1), |
| 974 | (&id2, &cov2), |
| 975 | ]; |
| 976 | let scalar = accumulate_cryptomatte_scalar(&pairs, &target_ids, pixel_count); |
| 977 | let parallel = accumulate_cryptomatte_matte(&pairs, &target_ids, pixel_count); |
| 978 | |
| 979 | assert_eq!(parallel.len(), scalar.len()); |
| 980 | for (i, (&p, &s)) in parallel.iter().zip(scalar.iter()).enumerate() { |
| 981 | assert_eq!( |
| 982 | p.to_bits(), |
| 983 | s.to_bits(), |
| 984 | "bit mismatch at pixel {i}: parallel={p} ({:#x}) vs scalar={s} ({:#x})", |
| 985 | p.to_bits(), |
| 986 | s.to_bits() |
| 987 | ); |
| 988 | } |
| 989 | } |
| 865 | 990 | } |