1use crate::math::{Mat3I16, Vec3I16, Vec3I32};
26use crate::regs::pack_xy;
27use crate::{ops, read_control, read_data, write_control, write_data};
28#[cfg(target_arch = "mips")]
29use core::arch::asm;
30
31#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
36#[repr(C)]
37pub struct Projected {
38 pub sx: i16,
40 pub sy: i16,
42 pub sz: u16,
44}
45
46#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
50#[repr(C)]
51pub struct ClassicNormalizedVector {
52 pub vector: Vec3I16,
55 pub xy_squared: i32,
57 pub squared: i32,
59}
60
61const fn isqrt_u32(n: u32) -> u32 {
63 let (mut lo, mut hi) = (0u32, 1u32 << 16);
64 while hi - lo > 1 {
65 let mid = (lo + hi) / 2;
66 if mid * mid <= n {
67 lo = mid;
68 } else {
69 hi = mid;
70 }
71 }
72 lo
73}
74
75const RSQRT_Q12_FROM_64: [i16; 192] = {
81 let mut table = [0i16; 192];
82 let mut i = 0;
83 while i < 192 {
84 table[i] = isqrt_u32((1u32 << 30) / (64 + i as u32)) as i16;
85 i += 1;
86 }
87 table
88};
89
90pub fn normalize_classic_q12_scheduled(input: Vec3I32) -> ClassicNormalizedVector {
106 let c = [
107 (input.x >> 12) as i16,
108 (input.y >> 12) as i16,
109 (input.z >> 12) as i16,
110 ];
111 let square = |v: i16| i32::from(v) * i32::from(v);
112 let xy_squared = square(c[0]).wrapping_add(square(c[1]));
113 let squared = xy_squared.wrapping_add(square(c[2]));
114 if squared <= 0 {
115 return ClassicNormalizedVector {
116 vector: Vec3I16::new(0, 0, 0),
117 xy_squared,
118 squared,
119 };
120 }
121 let even_zeros = (squared as u32).leading_zeros() & !1;
122 let in_range = if even_zeros >= 24 {
123 squared << (even_zeros - 24)
124 } else {
125 squared >> (24 - even_zeros)
126 };
127 let r = i32::from(RSQRT_Q12_FROM_64[(in_range - 64) as usize]);
128 let shift = (31 - even_zeros) / 2;
129 let scale = |v: i16| ((i32::from(v) * r) >> shift) as i16;
130 ClassicNormalizedVector {
131 vector: Vec3I16::new(scale(c[0]), scale(c[1]), scale(c[2])),
132 xy_squared,
133 squared,
134 }
135}
136
137#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
144#[repr(C)]
145pub struct AabbClipPlane {
146 pub normal: [i16; 3],
148 pub kind: u8,
150 pub signbits: u8,
152 pub distance: i32,
154}
155
156#[doc(alias = "SetRotMatrix")]
158pub fn load_rotation(m: &Mat3I16) {
159 write_control!(0, pack_xy(m.m[0][0], m.m[0][1]));
160 write_control!(1, pack_xy(m.m[0][2], m.m[1][0]));
161 write_control!(2, pack_xy(m.m[1][1], m.m[1][2]));
162 write_control!(3, pack_xy(m.m[2][0], m.m[2][1]));
163 write_control!(4, m.m[2][2] as i32 as u32);
164}
165
166#[doc(alias = "SetLightMatrix")]
168pub fn load_light_matrix(m: &Mat3I16) {
169 write_control!(8, pack_xy(m.m[0][0], m.m[0][1]));
170 write_control!(9, pack_xy(m.m[0][2], m.m[1][0]));
171 write_control!(10, pack_xy(m.m[1][1], m.m[1][2]));
172 write_control!(11, pack_xy(m.m[2][0], m.m[2][1]));
173 write_control!(12, m.m[2][2] as i32 as u32);
174}
175
176pub fn load_aabb_clip4(planes: &[AabbClipPlane; 4]) {
184 load_rotation(&Mat3I16 {
185 m: [planes[0].normal, planes[1].normal, planes[2].normal],
186 });
187 load_light_matrix(&Mat3I16 {
188 m: [planes[3].normal, [0; 3], [0; 3]],
189 });
190}
191
192#[doc(alias = "SetColorMatrix")]
194pub fn load_light_color_matrix(m: &Mat3I16) {
195 write_control!(16, pack_xy(m.m[0][0], m.m[0][1]));
196 write_control!(17, pack_xy(m.m[0][2], m.m[1][0]));
197 write_control!(18, pack_xy(m.m[1][1], m.m[1][2]));
198 write_control!(19, pack_xy(m.m[2][0], m.m[2][1]));
199 write_control!(20, m.m[2][2] as i32 as u32);
200}
201
202#[doc(alias = "SetTransMatrix")]
204pub fn load_translation(t: Vec3I32) {
205 write_control!(5, t.x as u32);
206 write_control!(6, t.y as u32);
207 write_control!(7, t.z as u32);
208}
209
210#[doc(alias = "SetBackColor")]
212pub fn load_background_color(c: Vec3I32) {
213 write_control!(13, c.x as u32);
214 write_control!(14, c.y as u32);
215 write_control!(15, c.z as u32);
216}
217
218#[doc(alias = "SetFarColor")]
221pub fn load_far_color(c: Vec3I32) {
222 write_control!(21, c.x as u32);
223 write_control!(22, c.y as u32);
224 write_control!(23, c.z as u32);
225}
226
227#[doc(alias = "SetGeomOffset")]
230pub fn set_screen_offset(ofx_15_16: i32, ofy_15_16: i32) {
231 write_control!(24, ofx_15_16 as u32);
232 write_control!(25, ofy_15_16 as u32);
233}
234
235#[doc(alias = "SetGeomScreen")]
238pub fn set_projection_plane(h: u16) {
239 write_control!(26, h as i32 as u32);
240}
241
242pub fn set_depth_cue(dqa: i16, dqb: i32) {
245 write_control!(27, dqa as i32 as u32);
246 write_control!(28, dqb as u32);
247}
248
249#[doc(alias = "AVSZ")]
252#[doc(alias = "ZSF3")]
253#[doc(alias = "ZSF4")]
254pub fn set_average_z_weights(zsf3: i16, zsf4: i16) {
255 write_control!(29, zsf3 as i32 as u32);
256 write_control!(30, zsf4 as i32 as u32);
257}
258
259#[doc(alias = "RotTransPers")]
266pub fn project_vertex(v: Vec3I16) -> Projected {
267 write_data!(0, v.xy_packed());
268 write_data!(1, v.z_packed());
269 unsafe { ops::project_single() };
272 let sxy = read_data!(14);
273 let sz = read_data!(19) as u16;
274 Projected {
275 sx: sxy as i16,
276 sy: (sxy >> 16) as i16,
277 sz,
278 }
279}
280
281#[doc(alias = "RotTransPers3")]
287pub fn project_triangle(v0: Vec3I16, v1: Vec3I16, v2: Vec3I16) -> [Projected; 3] {
288 write_data!(0, v0.xy_packed());
290 write_data!(1, v0.z_packed());
291 write_data!(2, v1.xy_packed());
292 write_data!(3, v1.z_packed());
293 write_data!(4, v2.xy_packed());
294 write_data!(5, v2.z_packed());
295 unsafe { ops::project_triple() };
298 let sxy0 = read_data!(12);
301 let sxy1 = read_data!(13);
302 let sxy2 = read_data!(14);
303 let sz1 = read_data!(17) as u16;
304 let sz2 = read_data!(18) as u16;
305 let sz3 = read_data!(19) as u16;
306 [
307 Projected {
308 sx: sxy0 as i16,
309 sy: (sxy0 >> 16) as i16,
310 sz: sz1,
311 },
312 Projected {
313 sx: sxy1 as i16,
314 sy: (sxy1 >> 16) as i16,
315 sz: sz2,
316 },
317 Projected {
318 sx: sxy2 as i16,
319 sy: (sxy2 >> 16) as i16,
320 sz: sz3,
321 },
322 ]
323}
324
325#[doc(alias = "RotTrans")]
330pub fn transform_vertex(v: Vec3I16) -> Vec3I32 {
331 write_data!(0, v.xy_packed());
332 write_data!(1, v.z_packed());
333 unsafe { ops::rotate_translate_v0() };
335 Vec3I32::new(
336 read_data!(25) as i32,
337 read_data!(26) as i32,
338 read_data!(27) as i32,
339 )
340}
341
342#[inline(always)]
347pub fn transform_vertex_scheduled(v: Vec3I16) -> Vec3I32 {
348 #[cfg(target_arch = "mips")]
349 {
350 transform_vertex_mips(v)
351 }
352 #[cfg(not(target_arch = "mips"))]
353 {
354 transform_vertex(v)
355 }
356}
357
358#[inline]
367pub fn compose_rotation_scheduled(left: &Mat3I16, right: &Mat3I16) -> Mat3I16 {
368 load_rotation(left);
369 load_translation(Vec3I32::ZERO);
370
371 let c0 = transform_vertex_scheduled(Vec3I16::new(right.m[0][0], right.m[1][0], right.m[2][0]));
372 let c1 = transform_vertex_scheduled(Vec3I16::new(right.m[0][1], right.m[1][1], right.m[2][1]));
373 let c2 = transform_vertex_scheduled(Vec3I16::new(right.m[0][2], right.m[1][2], right.m[2][2]));
374 let clamp = |value: i32| value.clamp(i16::MIN as i32, i16::MAX as i32) as i16;
375
376 Mat3I16 {
377 m: [
378 [clamp(c0.x), clamp(c1.x), clamp(c2.x)],
379 [clamp(c0.y), clamp(c1.y), clamp(c2.y)],
380 [clamp(c0.z), clamp(c1.z), clamp(c2.z)],
381 ],
382 }
383}
384
385#[inline(always)]
391pub fn project_vertex_scheduled(v: Vec3I16) -> Projected {
392 #[cfg(target_arch = "mips")]
393 {
394 project_vertex_mips(v)
395 }
396 #[cfg(not(target_arch = "mips"))]
397 {
398 project_vertex(v)
399 }
400}
401
402#[inline(always)]
408pub fn project_triangle_scheduled(v0: Vec3I16, v1: Vec3I16, v2: Vec3I16) -> [Projected; 3] {
409 #[cfg(target_arch = "mips")]
410 {
411 project_triangle_mips(v0, v1, v2)
412 }
413 #[cfg(not(target_arch = "mips"))]
414 {
415 project_triangle(v0, v1, v2)
416 }
417}
418
419#[must_use = "call read() to collect the projected triple"]
428#[doc(alias = "RTPT")]
429pub struct ProjectTripleInFlight(#[cfg(not(target_arch = "mips"))] [Projected; 3]);
430
431#[inline(always)]
438#[doc(alias = "RTPT")]
439pub fn start_project_triple(v0: Vec3I16, v1: Vec3I16, v2: Vec3I16) -> ProjectTripleInFlight {
440 #[cfg(target_arch = "mips")]
441 {
442 let v0_xy = v0.xy_packed();
443 let v0_z = v0.z_packed();
444 let v1_xy = v1.xy_packed();
445 let v1_z = v1.z_packed();
446 let v2_xy = v2.xy_packed();
447 let v2_z = v2.z_packed();
448 unsafe {
453 asm!(
454 ".word {w0}",
456 ".word {w1}",
457 ".word {w2}",
458 ".word {w3}",
459 ".word {w4}",
460 ".word {w5}",
461 ".word 0",
466 ".word 0",
467 ".word {w6}",
469 w0 = const psx_hw::gte::mtc2(8, 0),
470 w1 = const psx_hw::gte::mtc2(9, 1),
471 w2 = const psx_hw::gte::mtc2(10, 2),
472 w3 = const psx_hw::gte::mtc2(11, 3),
473 w4 = const psx_hw::gte::mtc2(12, 4),
474 w5 = const psx_hw::gte::mtc2(13, 5),
475 w6 = const psx_hw::gte::command::RTPT,
476 in("$8") v0_xy,
477 in("$9") v0_z,
478 in("$10") v1_xy,
479 in("$11") v1_z,
480 in("$12") v2_xy,
481 in("$13") v2_z,
482 options(nostack, nomem, preserves_flags),
483 );
484 }
485 ProjectTripleInFlight()
486 }
487 #[cfg(not(target_arch = "mips"))]
488 {
489 ProjectTripleInFlight(project_triangle(v0, v1, v2))
490 }
491}
492
493impl ProjectTripleInFlight {
494 #[inline(always)]
503 pub fn read(self) -> [Projected; 3] {
504 #[cfg(target_arch = "mips")]
505 {
506 let sxy0: u32;
507 let sxy1: u32;
508 let sxy2: u32;
509 let sz1: u32;
510 let sz2: u32;
511 let sz3: u32;
512 unsafe {
518 asm!(
519 ".word {w0}",
523 ".word {w1}",
524 ".word {w2}",
525 ".word {w3}",
526 ".word {w4}",
527 ".word {w5}",
528 ".word 0",
529 w0 = const psx_hw::gte::mfc2(8, 12),
530 w1 = const psx_hw::gte::mfc2(9, 13),
531 w2 = const psx_hw::gte::mfc2(10, 14),
532 w3 = const psx_hw::gte::mfc2(11, 17),
533 w4 = const psx_hw::gte::mfc2(12, 18),
534 w5 = const psx_hw::gte::mfc2(13, 19),
535 out("$8") sxy0,
536 out("$9") sxy1,
537 out("$10") sxy2,
538 out("$11") sz1,
539 out("$12") sz2,
540 out("$13") sz3,
541 options(nostack, nomem, preserves_flags),
542 );
543 }
544 [
545 Projected {
546 sx: sxy0 as i16,
547 sy: (sxy0 >> 16) as i16,
548 sz: sz1 as u16,
549 },
550 Projected {
551 sx: sxy1 as i16,
552 sy: (sxy1 >> 16) as i16,
553 sz: sz2 as u16,
554 },
555 Projected {
556 sx: sxy2 as i16,
557 sy: (sxy2 >> 16) as i16,
558 sz: sz3 as u16,
559 },
560 ]
561 }
562 #[cfg(not(target_arch = "mips"))]
563 {
564 self.0
565 }
566 }
567}
568
569#[cfg(target_arch = "mips")]
570#[inline(always)]
571fn project_vertex_mips(v: Vec3I16) -> Projected {
572 let mut sxy = v.xy_packed();
573 let mut sz = v.z_packed();
574 unsafe {
578 asm!(
579 ".word {w0}",
581 ".word {w1}",
582 ".word 0",
587 ".word 0",
588 ".word {w2}",
590 ".word {w3}",
593 ".word {w4}",
594 ".word 0",
595 w0 = const psx_hw::gte::mtc2(8, 0),
596 w1 = const psx_hw::gte::mtc2(9, 1),
597 w2 = const psx_hw::gte::command::RTPS,
598 w3 = const psx_hw::gte::mfc2(8, 14),
599 w4 = const psx_hw::gte::mfc2(9, 19),
600 inlateout("$8") sxy,
601 inlateout("$9") sz,
602 options(nostack, nomem, preserves_flags),
603 );
604 }
605 Projected {
606 sx: sxy as i16,
607 sy: (sxy >> 16) as i16,
608 sz: sz as u16,
609 }
610}
611
612#[cfg(target_arch = "mips")]
613#[inline(always)]
614fn project_triangle_mips(v0: Vec3I16, v1: Vec3I16, v2: Vec3I16) -> [Projected; 3] {
615 let v0_xy = v0.xy_packed();
616 let v0_z = v0.z_packed();
617 let v1_xy = v1.xy_packed();
618 let v1_z = v1.z_packed();
619 let v2_xy = v2.xy_packed();
620 let v2_z = v2.z_packed();
621 let sxy0: u32;
622 let sxy1: u32;
623 let sxy2: u32;
624 let sz1: u32;
625 let sz2: u32;
626 let sz3: u32;
627 unsafe {
632 asm!(
633 ".word {w0}",
635 ".word {w1}",
636 ".word {w2}",
637 ".word {w3}",
638 ".word {w4}",
639 ".word {w5}",
640 ".word 0",
645 ".word 0",
646 ".word {w6}",
648 ".word {w7}",
653 ".word {w8}",
654 ".word {w9}",
655 ".word {w10}",
656 ".word {w11}",
657 ".word {w12}",
658 ".word 0",
659 w0 = const psx_hw::gte::mtc2(8, 0),
660 w1 = const psx_hw::gte::mtc2(9, 1),
661 w2 = const psx_hw::gte::mtc2(10, 2),
662 w3 = const psx_hw::gte::mtc2(11, 3),
663 w4 = const psx_hw::gte::mtc2(12, 4),
664 w5 = const psx_hw::gte::mtc2(13, 5),
665 w6 = const psx_hw::gte::command::RTPT,
666 w7 = const psx_hw::gte::mfc2(8, 12),
667 w8 = const psx_hw::gte::mfc2(9, 13),
668 w9 = const psx_hw::gte::mfc2(10, 14),
669 w10 = const psx_hw::gte::mfc2(11, 17),
670 w11 = const psx_hw::gte::mfc2(12, 18),
671 w12 = const psx_hw::gte::mfc2(13, 19),
672 inlateout("$8") v0_xy => sxy0,
673 inlateout("$9") v0_z => sxy1,
674 inlateout("$10") v1_xy => sxy2,
675 inlateout("$11") v1_z => sz1,
676 inlateout("$12") v2_xy => sz2,
677 inlateout("$13") v2_z => sz3,
678 options(nostack, nomem, preserves_flags),
679 );
680 }
681 [
682 Projected {
683 sx: sxy0 as i16,
684 sy: (sxy0 >> 16) as i16,
685 sz: sz1 as u16,
686 },
687 Projected {
688 sx: sxy1 as i16,
689 sy: (sxy1 >> 16) as i16,
690 sz: sz2 as u16,
691 },
692 Projected {
693 sx: sxy2 as i16,
694 sy: (sxy2 >> 16) as i16,
695 sz: sz3 as u16,
696 },
697 ]
698}
699
700#[cfg(target_arch = "mips")]
701#[inline(always)]
702fn transform_vertex_mips(v: Vec3I16) -> Vec3I32 {
703 let xy = v.xy_packed();
704 let z = v.z_packed();
705 let mac1: u32;
706 let mac2: u32;
707 let mac3: u32;
708 unsafe {
713 asm!(
714 ".word {w0}",
716 ".word {w1}",
717 ".word 0",
724 ".word 0",
725 ".word {w2}",
727 ".word {w3}",
731 ".word {w4}",
732 ".word {w5}",
733 ".word 0",
734 w0 = const psx_hw::gte::mtc2(8, 0),
735 w1 = const psx_hw::gte::mtc2(9, 1),
736 w2 = const psx_hw::gte::command::ROTATE_TRANSLATE_V0,
737 w3 = const psx_hw::gte::mfc2(8, 25),
738 w4 = const psx_hw::gte::mfc2(9, 26),
739 w5 = const psx_hw::gte::mfc2(10, 27),
740 inlateout("$8") xy => mac1,
741 inlateout("$9") z => mac2,
742 lateout("$10") mac3,
743 options(nostack, nomem, preserves_flags),
744 );
745 }
746 Vec3I32::new(mac1 as i32, mac2 as i32, mac3 as i32)
747}
748
749#[derive(Clone, Copy)]
752pub struct TransformProbe {
753 pub out: Vec3I32,
758 pub x_settled: i32,
761 pub tr: [i32; 3],
766}
767
768#[inline(always)]
778pub fn transform_vertex_probed(v: Vec3I16) -> TransformProbe {
779 #[cfg(target_arch = "mips")]
780 {
781 let xy = v.xy_packed();
782 let z = v.z_packed();
783 let mac1: u32;
784 let mac2: u32;
785 let mac3: u32;
786 let mac1_settled: u32;
787 let trx: u32;
788 let try_: u32;
789 let trz: u32;
790 unsafe {
796 asm!(
797 ".word {w0}",
800 ".word {w1}",
801 ".word {w2}",
802 ".word {w3}",
803 ".word {w4}",
804 ".word {w5}",
805 ".word 0",
806 ".word 0",
811 ".word 0",
812 ".word 0",
813 ".word 0",
814 ".word {w6}",
815 ".word {w7}",
816 ".word {w8}",
817 ".word {w9}",
818 ".word 0",
819 w0 = const psx_hw::gte::mtc2(8, 0),
820 w1 = const psx_hw::gte::mtc2(9, 1),
821 w2 = const psx_hw::gte::command::ROTATE_TRANSLATE_V0,
822 w3 = const psx_hw::gte::mfc2(8, 25),
823 w4 = const psx_hw::gte::mfc2(9, 26),
824 w5 = const psx_hw::gte::mfc2(10, 27),
825 w6 = const psx_hw::gte::mfc2(11, 25),
826 w7 = const psx_hw::gte::cfc2(12, 5),
827 w8 = const psx_hw::gte::cfc2(13, 6),
828 w9 = const psx_hw::gte::cfc2(14, 7),
829 inlateout("$8") xy => mac1,
830 inlateout("$9") z => mac2,
831 lateout("$10") mac3,
832 lateout("$11") mac1_settled,
833 lateout("$12") trx,
834 lateout("$13") try_,
835 lateout("$14") trz,
836 options(nostack, nomem, preserves_flags),
837 );
838 }
839 TransformProbe {
840 out: Vec3I32::new(mac1 as i32, mac2 as i32, mac3 as i32),
841 x_settled: mac1_settled as i32,
842 tr: [trx as i32, try_ as i32, trz as i32],
843 }
844 }
845 #[cfg(not(target_arch = "mips"))]
846 {
847 let out = transform_vertex(v);
848 TransformProbe {
849 out,
850 x_settled: out.x,
851 tr: [
852 read_control!(5) as i32,
853 read_control!(6) as i32,
854 read_control!(7) as i32,
855 ],
856 }
857 }
858}
859
860pub fn average_z_triangle() -> u16 {
864 unsafe { ops::average_z3() };
867 read_data!(7) as u16
868}
869
870#[inline(always)]
877pub fn average_cached_z3(depths: [u16; 3]) -> u16 {
878 #[cfg(target_arch = "mips")]
879 {
880 let mut otz = depths[0] as u32;
881 unsafe {
886 asm!(
887 ".word {w0}",
890 ".word {w1}",
891 ".word {w2}",
892 ".word 0",
893 ".word 0",
894 ".word {w3}",
895 ".word {w4}",
897 ".word 0",
898 w0 = const psx_hw::gte::mtc2(8, 17),
899 w1 = const psx_hw::gte::mtc2(9, 18),
900 w2 = const psx_hw::gte::mtc2(10, 19),
901 w3 = const psx_hw::gte::command::AVSZ3,
902 w4 = const psx_hw::gte::mfc2(8, 7),
903 inlateout("$8") otz,
904 in("$9") depths[1] as u32,
905 in("$10") depths[2] as u32,
906 options(nostack, nomem, preserves_flags),
907 );
908 }
909 otz as u16
910 }
911 #[cfg(not(target_arch = "mips"))]
912 {
913 write_data!(17, depths[0] as u32);
914 write_data!(18, depths[1] as u32);
915 write_data!(19, depths[2] as u32);
916 unsafe { ops::average_z3() };
918 read_data!(7) as u16
919 }
920}
921
922#[inline(always)]
928#[doc(alias = "OTZ")]
929pub fn classic_ordering_depth3_from_sum(sum: u32) -> u16 {
930 #[cfg(target_arch = "mips")]
931 {
932 let mut otz = sum;
933 unsafe {
937 asm!(
938 "sll $9, $8, 2",
939 "addu $9, $9, $8",
940 "sll $10, $9, 4",
941 "addu $9, $10, $9",
942 "sll $9, $9, 2",
943 "addu $9, $9, $8",
944 "srl $8, $9, 12",
945 inlateout("$8") otz,
946 lateout("$9") _,
947 lateout("$10") _,
948 options(nostack, nomem, preserves_flags),
949 );
950 }
951 otz as u16
952 }
953 #[cfg(not(target_arch = "mips"))]
954 {
955 ((sum * 0x155) >> 12) as u16
956 }
957}
958
959#[inline(always)]
964pub fn average_cached_z4(depths: [u16; 4]) -> u16 {
965 #[cfg(target_arch = "mips")]
966 {
967 let mut otz = depths[0] as u32;
968 unsafe {
973 asm!(
974 ".word {w0}",
977 ".word {w1}",
978 ".word {w2}",
979 ".word {w3}",
980 ".word 0",
981 ".word 0",
982 ".word {w4}",
983 ".word {w5}",
985 ".word 0",
986 w0 = const psx_hw::gte::mtc2(8, 16),
987 w1 = const psx_hw::gte::mtc2(9, 17),
988 w2 = const psx_hw::gte::mtc2(10, 18),
989 w3 = const psx_hw::gte::mtc2(11, 19),
990 w4 = const psx_hw::gte::command::AVSZ4,
991 w5 = const psx_hw::gte::mfc2(8, 7),
992 inlateout("$8") otz,
993 in("$9") depths[1] as u32,
994 in("$10") depths[2] as u32,
995 in("$11") depths[3] as u32,
996 options(nostack, nomem, preserves_flags),
997 );
998 }
999 otz as u16
1000 }
1001 #[cfg(not(target_arch = "mips"))]
1002 {
1003 write_data!(16, depths[0] as u32);
1004 write_data!(17, depths[1] as u32);
1005 write_data!(18, depths[2] as u32);
1006 write_data!(19, depths[3] as u32);
1007 unsafe { ops::average_z4() };
1009 read_data!(7) as u16
1010 }
1011}
1012
1013#[inline]
1021#[doc(alias = "OTZ")]
1022#[doc(alias = "AverageZ3")]
1023pub fn average_z3_ordering_depth(depths: [u16; 3], zsf3: i16) -> u16 {
1024 let sum = depths[0] as i64 + depths[1] as i64 + depths[2] as i64;
1030 ((sum * zsf3 as i64) >> 12).clamp(0, u16::MAX as i64) as u16
1032}
1033
1034#[inline]
1040#[doc(alias = "OTZ")]
1041#[doc(alias = "AverageZ4")]
1042pub fn average_z4_ordering_depth(depths: [u16; 4], zsf4: i16) -> u16 {
1043 let sum = depths[0] as i64 + depths[1] as i64 + depths[2] as i64 + depths[3] as i64;
1045 ((sum * zsf4 as i64) >> 12).clamp(0, u16::MAX as i64) as u16
1047}
1048
1049#[inline(always)]
1050fn aabb_outer_support(mins: [i16; 3], maxs: [i16; 3], signbits: u8) -> Vec3I16 {
1051 Vec3I16::new(
1052 if signbits & 1 != 0 { mins[0] } else { maxs[0] },
1053 if signbits & 2 != 0 { mins[1] } else { maxs[1] },
1054 if signbits & 4 != 0 { mins[2] } else { maxs[2] },
1055 )
1056}
1057
1058#[cfg(target_arch = "mips")]
1060const RT_V0_NO_TRANSLATION: u32 = psx_hw::gte::mvmva(
1061 psx_hw::gte::Matrix::Rotation,
1062 psx_hw::gte::Vector::V0,
1063 psx_hw::gte::Translation::None,
1064);
1065
1066#[cfg(target_arch = "mips")]
1068const LLM_V0_NO_TRANSLATION: u32 = psx_hw::gte::mvmva(
1069 psx_hw::gte::Matrix::Light,
1070 psx_hw::gte::Vector::V0,
1071 psx_hw::gte::Translation::None,
1072);
1073
1074#[cfg(target_arch = "mips")]
1076const fn read_mac(n: u32) -> u32 {
1077 psx_hw::gte::mfc2(8, 25 + n)
1078}
1079
1080#[cfg(target_arch = "mips")]
1081#[inline(always)]
1082fn aabb_dot_mvmva<const OP: u32, const READ_MAC: u32>(v: Vec3I16) -> i32 {
1083 let mut dot = v.xy_packed();
1084 unsafe {
1091 asm!(
1092 ".word {w0}",
1093 ".word {w1}",
1094 ".word 0",
1097 ".word 0",
1098 ".word {op}",
1099 ".word {read_mac}",
1100 ".word 0",
1101 w0 = const psx_hw::gte::mtc2(8, 0),
1102 w1 = const psx_hw::gte::mtc2(9, 1),
1103 op = const OP,
1104 read_mac = const READ_MAC,
1105 inlateout("$8") dot,
1106 in("$9") v.z_packed(),
1107 options(nostack, nomem, preserves_flags),
1108 );
1109 }
1110 dot as i32
1111}
1112
1113#[inline(always)]
1114fn aabb_clip_dot(v: Vec3I16, _plane: &AabbClipPlane, index: usize) -> i32 {
1115 #[cfg(target_arch = "mips")]
1116 {
1117 match index {
1118 0 => aabb_dot_mvmva::<{ RT_V0_NO_TRANSLATION }, { read_mac(0) }>(v),
1119 1 => aabb_dot_mvmva::<{ RT_V0_NO_TRANSLATION }, { read_mac(1) }>(v),
1120 2 => aabb_dot_mvmva::<{ RT_V0_NO_TRANSLATION }, { read_mac(2) }>(v),
1121 3 => aabb_dot_mvmva::<{ LLM_V0_NO_TRANSLATION }, { read_mac(0) }>(v),
1122 _ => 0,
1123 }
1124 }
1125 #[cfg(not(target_arch = "mips"))]
1126 {
1127 let _ = index;
1128 _plane.normal[0] as i32 * v.x as i32
1129 + _plane.normal[1] as i32 * v.y as i32
1130 + _plane.normal[2] as i32 * v.z as i32
1131 }
1132}
1133
1134#[inline]
1143pub fn classify_aabb_clip4(
1144 mins: [i16; 3],
1145 maxs: [i16; 3],
1146 planes: &[AabbClipPlane; 4],
1147 mut clip_flags: u8,
1148) -> i32 {
1149 let mut index = 0usize;
1150 while index < 4 {
1151 let flag = 1u8 << index;
1152 if clip_flags & flag != 0 {
1153 let plane = &planes[index];
1154 let outer = aabb_outer_support(mins, maxs, plane.signbits);
1155 if aabb_clip_dot(outer, plane, index) < plane.distance {
1156 return -1;
1157 }
1158 let inner = aabb_outer_support(maxs, mins, plane.signbits);
1159 if aabb_clip_dot(inner, plane, index) >= plane.distance {
1160 clip_flags &= !flag;
1161 }
1162 }
1163 index += 1;
1164 }
1165 clip_flags as i32
1166}
1167
1168#[inline(always)]
1175pub fn is_aabb_outside_clip4(
1176 mins: [i16; 3],
1177 maxs: [i16; 3],
1178 planes: &[AabbClipPlane; 4],
1179 clip_flags: u8,
1180) -> bool {
1181 let mut index = 0usize;
1182 while index < 4 {
1183 let flag = 1u8 << index;
1184 if clip_flags & flag != 0 {
1185 let plane = &planes[index];
1186 let outer = aabb_outer_support(mins, maxs, plane.signbits);
1187 if aabb_clip_dot(outer, plane, index) < plane.distance {
1188 return true;
1189 }
1190 }
1191 index += 1;
1192 }
1193 false
1194}
1195
1196#[inline]
1209#[doc(alias = "NCLIP")]
1210#[doc(alias = "MAC0")]
1211pub fn screen_area(vertices: [(i16, i16); 3]) -> i32 {
1212 let (sx0, sy0) = (vertices[0].0 as i32, vertices[0].1 as i32);
1213 let (sx1, sy1) = (vertices[1].0 as i32, vertices[1].1 as i32);
1214 let (sx2, sy2) = (vertices[2].0 as i32, vertices[2].1 as i32);
1215 (sx1 - sx0) * (sy2 - sy0) - (sy1 - sy0) * (sx2 - sx0)
1219}
1220
1221#[inline(always)]
1227#[doc(alias = "NCLIP")]
1228#[doc(alias = "MAC0")]
1229pub fn screen_area_scheduled(vertices: [(i16, i16); 3]) -> i32 {
1230 #[cfg(target_arch = "mips")]
1231 {
1232 let sxy0 = pack_xy(vertices[0].0, vertices[0].1);
1233 let sxy1 = pack_xy(vertices[1].0, vertices[1].1);
1234 let mut area = pack_xy(vertices[2].0, vertices[2].1);
1235 unsafe {
1239 asm!(
1240 ".word {w0}",
1242 ".word {w1}",
1243 ".word {w2}",
1244 ".word 0",
1246 ".word 0",
1247 ".word {w3}",
1249 ".word 0",
1250 ".word 0",
1251 ".word 0",
1252 ".word 0",
1253 ".word 0",
1254 ".word 0",
1255 ".word 0",
1256 ".word 0",
1257 ".word {w4}",
1259 ".word 0",
1260 w0 = const psx_hw::gte::mtc2(8, 12),
1261 w1 = const psx_hw::gte::mtc2(9, 13),
1262 w2 = const psx_hw::gte::mtc2(10, 14),
1263 w3 = const psx_hw::gte::command::NCLIP,
1264 w4 = const psx_hw::gte::mfc2(10, 24),
1265 in("$8") sxy0,
1266 in("$9") sxy1,
1267 inlateout("$10") area,
1268 options(nostack, nomem, preserves_flags),
1269 );
1270 }
1271 area as i32
1272 }
1273 #[cfg(not(target_arch = "mips"))]
1274 {
1275 screen_area(vertices)
1276 }
1277}
1278
1279#[inline(always)]
1286pub fn screen_area_and_unpack_model_face_scheduled(
1287 vertices: [(i16, i16); 3],
1288 corner_words: [u32; 3],
1289) -> (i32, [u16; 3], u8) {
1290 #[cfg(target_arch = "mips")]
1291 {
1292 let sxy0 = pack_xy(vertices[0].0, vertices[0].1);
1293 let sxy1 = pack_xy(vertices[1].0, vertices[1].1);
1294 let mut area = pack_xy(vertices[2].0, vertices[2].1);
1295 let mut uv0 = corner_words[0];
1296 let mut uv1 = corner_words[1];
1297 let mut uv2 = corner_words[2];
1298 let palette_bank: u32;
1299 unsafe {
1303 asm!(
1304 ".word {w0}",
1306 ".word {w1}",
1307 ".word {w2}",
1308 ".word 0",
1309 ".word 0",
1310 ".word {w3}",
1311 "srl $14, $11, 14",
1314 "andi $14, $14, 3",
1315 "srl $11, $11, 16",
1316 "srl $12, $12, 16",
1317 "srl $13, $13, 16",
1318 ".word 0",
1319 ".word 0",
1320 ".word 0",
1321 ".word {w4}",
1323 ".word 0",
1324 w0 = const psx_hw::gte::mtc2(8, 12),
1325 w1 = const psx_hw::gte::mtc2(9, 13),
1326 w2 = const psx_hw::gte::mtc2(10, 14),
1327 w3 = const psx_hw::gte::command::NCLIP,
1328 w4 = const psx_hw::gte::mfc2(10, 24),
1329 in("$8") sxy0,
1330 in("$9") sxy1,
1331 inlateout("$10") area,
1332 inlateout("$11") uv0,
1333 inlateout("$12") uv1,
1334 inlateout("$13") uv2,
1335 lateout("$14") palette_bank,
1336 options(nostack, nomem, preserves_flags),
1337 );
1338 }
1339 (
1340 area as i32,
1341 [uv0 as u16, uv1 as u16, uv2 as u16],
1342 palette_bank as u8,
1343 )
1344 }
1345 #[cfg(not(target_arch = "mips"))]
1346 {
1347 (
1348 screen_area(vertices),
1349 [
1350 (corner_words[0] >> 16) as u16,
1351 (corner_words[1] >> 16) as u16,
1352 (corner_words[2] >> 16) as u16,
1353 ],
1354 ((corner_words[0] >> 14) & 3) as u8,
1355 )
1356 }
1357}
1358
1359#[inline(always)]
1368pub fn screen_area_and_average_cached_z3_scheduled(
1369 vertices: [(i16, i16); 3],
1370 depths: [u16; 3],
1371) -> (i32, u16) {
1372 #[cfg(target_arch = "mips")]
1373 {
1374 let sxy0 = pack_xy(vertices[0].0, vertices[0].1);
1375 let sxy1 = pack_xy(vertices[1].0, vertices[1].1);
1376 let mut area = pack_xy(vertices[2].0, vertices[2].1);
1377 let mut otz = depths[0] as u32;
1378 unsafe {
1383 asm!(
1384 ".word {w0}",
1386 ".word {w1}",
1387 ".word {w2}",
1388 ".word 0",
1389 ".word 0",
1390 ".word {w3}",
1391 ".word {w4}",
1394 ".word {w5}",
1395 ".word {w6}",
1396 ".word 0",
1397 ".word 0",
1398 ".word 0",
1399 ".word 0",
1400 ".word 0",
1401 ".word {w7}",
1404 ".word {w8}",
1405 ".word {w9}",
1406 ".word 0",
1407 w0 = const psx_hw::gte::mtc2(8, 12),
1408 w1 = const psx_hw::gte::mtc2(9, 13),
1409 w2 = const psx_hw::gte::mtc2(10, 14),
1410 w3 = const psx_hw::gte::command::NCLIP,
1411 w4 = const psx_hw::gte::mtc2(11, 17),
1412 w5 = const psx_hw::gte::mtc2(12, 18),
1413 w6 = const psx_hw::gte::mtc2(13, 19),
1414 w7 = const psx_hw::gte::mfc2(10, 24),
1415 w8 = const psx_hw::gte::command::AVSZ3,
1416 w9 = const psx_hw::gte::mfc2(11, 7),
1417 in("$8") sxy0,
1418 in("$9") sxy1,
1419 inlateout("$10") area,
1420 inlateout("$11") otz,
1421 in("$12") depths[1] as u32,
1422 in("$13") depths[2] as u32,
1423 options(nostack, nomem, preserves_flags),
1424 );
1425 }
1426 (area as i32, otz as u16)
1427 }
1428 #[cfg(not(target_arch = "mips"))]
1429 {
1430 (screen_area(vertices), average_cached_z3(depths))
1431 }
1432}
1433
1434#[inline(always)]
1442#[doc(alias = "OTZ")]
1443pub fn screen_area_and_classic_ordering_depth3_scheduled(
1444 vertices: [(i16, i16); 3],
1445 depths: [u16; 3],
1446) -> (i32, u16) {
1447 #[cfg(target_arch = "mips")]
1448 {
1449 let sxy0 = pack_xy(vertices[0].0, vertices[0].1);
1450 let sxy1 = pack_xy(vertices[1].0, vertices[1].1);
1451 let mut area = pack_xy(vertices[2].0, vertices[2].1);
1452 let mut otz = depths[0] as u32;
1453 let depth1 = depths[1] as u32;
1454 let depth2 = depths[2] as u32;
1455 unsafe {
1460 asm!(
1461 ".word {w0}",
1463 ".word {w1}",
1464 ".word {w2}",
1465 ".word 0",
1466 ".word 0",
1467 ".word {w3}",
1469 "addu $11, $11, $12",
1472 "addu $11, $11, $13",
1473 "sll $12, $11, 2",
1474 "addu $12, $12, $11",
1475 "sll $13, $12, 4",
1476 "addu $12, $13, $12",
1477 "sll $12, $12, 2",
1478 "addu $12, $12, $11",
1479 ".word {w4}",
1482 "srl $11, $12, 12",
1483 w0 = const psx_hw::gte::mtc2(8, 12),
1484 w1 = const psx_hw::gte::mtc2(9, 13),
1485 w2 = const psx_hw::gte::mtc2(10, 14),
1486 w3 = const psx_hw::gte::command::NCLIP,
1487 w4 = const psx_hw::gte::mfc2(10, 24),
1488 in("$8") sxy0,
1489 in("$9") sxy1,
1490 inlateout("$10") area,
1491 inlateout("$11") otz,
1492 inlateout("$12") depth1 => _,
1493 inlateout("$13") depth2 => _,
1494 options(nostack, nomem, preserves_flags),
1495 );
1496 }
1497 (area as i32, otz as u16)
1498 }
1499 #[cfg(not(target_arch = "mips"))]
1500 {
1501 let sum = depths[0] as u32 + depths[1] as u32 + depths[2] as u32;
1502 (screen_area(vertices), ((sum * 0x155) >> 12) as u16)
1503 }
1504}
1505
1506pub fn is_screen_triangle_back_facing(vertices: [(i16, i16); 3]) -> bool {
1512 screen_area(vertices) <= 0
1513}
1514
1515#[doc(alias = "FLAG")]
1519pub fn error_flags() -> u32 {
1520 read_control!(31)
1521}
1522
1523#[deprecated(note = "renamed to `start_project_triple`")]
1525#[inline(always)]
1526pub fn rtpt_kick(v0: Vec3I16, v1: Vec3I16, v2: Vec3I16) -> ProjectTripleInFlight {
1527 start_project_triple(v0, v1, v2)
1528}
1529
1530#[deprecated(note = "renamed to `classic_ordering_depth3_from_sum`")]
1532#[inline(always)]
1533pub fn classic_otz3_from_sum(sum: u32) -> u16 {
1534 classic_ordering_depth3_from_sum(sum)
1535}
1536
1537#[deprecated(note = "renamed to `is_aabb_outside_clip4`")]
1539#[inline(always)]
1540pub fn aabb_outside_clip4(
1541 mins: [i16; 3],
1542 maxs: [i16; 3],
1543 planes: &[AabbClipPlane; 4],
1544 clip_flags: u8,
1545) -> bool {
1546 is_aabb_outside_clip4(mins, maxs, planes, clip_flags)
1547}
1548
1549#[deprecated(note = "renamed to `screen_area_scheduled`")]
1551#[inline(always)]
1552pub fn screen_area_mac0_scheduled(vertices: [(i16, i16); 3]) -> i32 {
1553 screen_area_scheduled(vertices)
1554}
1555
1556#[cfg(all(test, not(target_arch = "mips")))]
1557mod host_smoke {
1558 use super::*;
1566 use crate::host;
1567
1568 fn install_identity() {
1569 load_rotation(&Mat3I16::IDENTITY);
1570 load_translation(Vec3I32::ZERO);
1571 set_screen_offset(160 << 16, 120 << 16);
1572 set_projection_plane(200);
1573 }
1574
1575 #[test]
1576 fn rtps_through_host_shim_projects_an_in_front_vertex() {
1577 host::reset();
1578 install_identity();
1579 let projected = project_vertex(Vec3I16::new(0, 0, 1024));
1583 assert_eq!(projected.sx, 160);
1584 assert_eq!(projected.sy, 120);
1585 assert!(
1586 projected.sz > 0,
1587 "near-plane vertex must yield non-zero depth"
1588 );
1589 }
1590
1591 #[test]
1592 fn rtpt_through_host_shim_matches_three_separate_rtps_calls() {
1593 host::reset();
1594 install_identity();
1595 let a = Vec3I16::new(-256, 0, 1024);
1596 let b = Vec3I16::new(256, 0, 1024);
1597 let c = Vec3I16::new(0, 256, 1024);
1598
1599 let batch = project_triangle(a, b, c);
1600
1601 host::reset();
1602 install_identity();
1603 let p_a = project_vertex(a);
1604 let p_b = project_vertex(b);
1605 let p_c = project_vertex(c);
1606
1607 assert_eq!(batch[0], p_a);
1608 assert_eq!(batch[1], p_b);
1609 assert_eq!(batch[2], p_c);
1610 }
1611
1612 #[test]
1613 fn mvmva_transform_through_host_shim_applies_rt_and_tr() {
1614 host::reset();
1615 load_rotation(&Mat3I16::IDENTITY);
1616 load_translation(Vec3I32::new(10, -20, 30));
1617
1618 let transformed = transform_vertex(Vec3I16::new(100, 200, 300));
1619
1620 assert_eq!(transformed, Vec3I32::new(110, 180, 330));
1621 }
1622
1623 #[test]
1624 fn scheduled_rotation_compose_matches_cpu_matrix_product() {
1625 host::reset();
1626 let left = Mat3I16::rotate_z(37).mul(&Mat3I16::rotate_y(91));
1627 let right = Mat3I16::rotate_y(13)
1628 .mul(&Mat3I16::rotate_x(55))
1629 .scale_columns_q12([3072, 4096, 5120]);
1630
1631 assert_eq!(compose_rotation_scheduled(&left, &right), left.mul(&right));
1632 }
1633
1634 #[test]
1635 fn classic_normalize_matches_reference_table_results() {
1636 for (input, vector, xy_squared, squared) in [
1637 ([1, 0, 0], [4096, 0, 0], 1, 1),
1638 ([3, 4, 0], [2457, 3276, 0], 25, 25),
1639 ([10, 20, 30], [1097, 2195, 3293], 500, 1400),
1640 ([-10, 20, 30], [-1098, 2195, 3293], 500, 1400),
1641 ([100, 50, -20], [3610, 1805, -723], 12_500, 12_900),
1642 ([600, 0, 200], [3898, 0, 1299], 360_000, 400_000),
1643 ] {
1644 host::reset();
1645 let result = normalize_classic_q12_scheduled(Vec3I32::new(
1646 input[0] << 12,
1647 input[1] << 12,
1648 input[2] << 12,
1649 ));
1650 assert_eq!(
1651 result,
1652 ClassicNormalizedVector {
1653 vector: Vec3I16::new(vector[0], vector[1], vector[2]),
1654 xy_squared,
1655 squared,
1656 }
1657 );
1658 }
1659
1660 host::reset();
1661 assert_eq!(
1662 normalize_classic_q12_scheduled(Vec3I32::ZERO),
1663 ClassicNormalizedVector::default()
1664 );
1665 }
1666
1667 #[test]
1668 fn scheduled_nclip_matches_software_area_on_host() {
1669 let vertices = [(-320, 112), (47, -91), (511, 230)];
1670 assert_eq!(screen_area_scheduled(vertices), screen_area(vertices));
1671 }
1672
1673 #[test]
1674 fn scheduled_nclip_face_unpack_matches_packed_record() {
1675 let vertices = [(-320, 112), (47, -91), (511, 230)];
1676 let corners = [0xabcd_c123, 0x0123_4567, 0xfedc_89ab];
1677 assert_eq!(
1678 screen_area_and_unpack_model_face_scheduled(vertices, corners),
1679 (
1680 screen_area(vertices),
1681 [0xabcd, 0x0123, 0xfedc],
1682 ((0xc123 >> 14) & 3) as u8,
1683 ),
1684 );
1685 }
1686
1687 #[test]
1688 fn cached_average_z_matches_avsz_saturation_rules() {
1689 assert_eq!(average_z3_ordering_depth([100, 200, 300], 1_365), 199);
1690 assert_eq!(average_z4_ordering_depth([100, 200, 300, 400], 1_024), 250);
1691 assert_eq!(average_z3_ordering_depth([u16::MAX; 3], i16::MAX), u16::MAX);
1692 assert_eq!(average_z4_ordering_depth([u16::MAX; 4], -1), 0);
1693
1694 set_average_z_weights(1_365, 1_024);
1695 assert_eq!(average_cached_z3([100, 200, 300]), 199);
1696 assert_eq!(average_cached_z4([100, 200, 300, 400]), 250);
1697 assert_eq!(classic_ordering_depth3_from_sum(100 + 200 + 300), 49);
1698
1699 let vertices = [(-320, 112), (47, -91), (511, 230)];
1700 assert_eq!(
1701 screen_area_and_average_cached_z3_scheduled(vertices, [100, 200, 300]),
1702 (screen_area(vertices), 199),
1703 );
1704 assert_eq!(
1705 screen_area_and_classic_ordering_depth3_scheduled(vertices, [100, 200, 300]),
1706 (screen_area(vertices), 49),
1707 );
1708 }
1709
1710 #[test]
1711 fn four_plane_aabb_clip_matches_scalar_support_points() {
1712 let plane = |normal: [i16; 3], distance| AabbClipPlane {
1713 normal,
1714 kind: 0,
1715 signbits: (normal[0] < 0) as u8
1716 | (((normal[1] < 0) as u8) << 1)
1717 | (((normal[2] < 0) as u8) << 2),
1718 distance,
1719 };
1720 let planes = [
1721 plane([4096, 1024, -512], -3000),
1722 plane([-2048, 4096, 256], 7000),
1723 plane([300, -900, 4096], -12000),
1724 plane([-700, -500, -4096], 16000),
1725 ];
1726 load_aabb_clip4(&planes);
1727
1728 let scalar = |mins: [i16; 3], maxs: [i16; 3], mut flags: u8| {
1729 for (index, plane) in planes.iter().enumerate() {
1730 let flag = 1u8 << index;
1731 if flags & flag == 0 {
1732 continue;
1733 }
1734 let outer = [
1735 if plane.normal[0] < 0 {
1736 mins[0]
1737 } else {
1738 maxs[0]
1739 },
1740 if plane.normal[1] < 0 {
1741 mins[1]
1742 } else {
1743 maxs[1]
1744 },
1745 if plane.normal[2] < 0 {
1746 mins[2]
1747 } else {
1748 maxs[2]
1749 },
1750 ];
1751 let inner = [
1752 if plane.normal[0] < 0 {
1753 maxs[0]
1754 } else {
1755 mins[0]
1756 },
1757 if plane.normal[1] < 0 {
1758 maxs[1]
1759 } else {
1760 mins[1]
1761 },
1762 if plane.normal[2] < 0 {
1763 maxs[2]
1764 } else {
1765 mins[2]
1766 },
1767 ];
1768 let dot = |point: [i16; 3]| {
1769 plane.normal[0] as i32 * point[0] as i32
1770 + plane.normal[1] as i32 * point[1] as i32
1771 + plane.normal[2] as i32 * point[2] as i32
1772 };
1773 if dot(outer) < plane.distance {
1774 return -1;
1775 }
1776 if dot(inner) >= plane.distance {
1777 flags &= !flag;
1778 }
1779 }
1780 flags as i32
1781 };
1782
1783 for (mins, maxs, flags) in [
1784 ([-10, -20, -30], [40, 50, 60], 0x0f),
1785 ([100, 200, 300], [120, 240, 360], 0x0f),
1786 ([-32000, -100, 20], [-30000, 100, 80], 0x05),
1787 ] {
1788 let expected = scalar(mins, maxs, flags);
1789 assert_eq!(classify_aabb_clip4(mins, maxs, &planes, flags), expected);
1790 assert_eq!(
1791 is_aabb_outside_clip4(mins, maxs, &planes, flags),
1792 expected < 0,
1793 );
1794 }
1795 assert_eq!(core::mem::size_of::<AabbClipPlane>(), 12);
1796 assert_eq!(core::mem::align_of::<AabbClipPlane>(), 4);
1797 }
1798
1799 #[test]
1804 fn classic_normalize_matches_previous_results_digest() {
1805 let mut seed = 0x1234_5678u32;
1806 let mut next = move || {
1807 seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
1808 seed
1809 };
1810 let mut hash = 0xcbf2_9ce4_8422_2325u64;
1811 let mut eat = |value: i32| {
1812 for byte in value.to_le_bytes() {
1813 hash ^= u64::from(byte);
1814 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
1815 }
1816 };
1817 for i in 0..40_000u32 {
1818 let mut component = || (next() as i32) >> (next() % 32);
1821 let input = Vec3I32::new(component(), component(), component());
1822 let input = if i % 7 == 0 {
1823 Vec3I32::new(input.x, 0, 0)
1824 } else {
1825 input
1826 };
1827 host::reset();
1828 let out = normalize_classic_q12_scheduled(input);
1829 eat(i32::from(out.vector.x));
1830 eat(i32::from(out.vector.y));
1831 eat(i32::from(out.vector.z));
1832 eat(out.xy_squared);
1833 eat(out.squared);
1834 }
1835 assert_eq!(hash, 0x803b_4a0d_7ea1_0cb5);
1836 }
1837
1838 const PREVIOUS_TABLE: [i16; 192] = [
1840 4096, 4064, 4033, 4003, 3973, 3944, 3916, 3888, 3861, 3835, 3809, 3783, 3758, 3734, 3710,
1841 3686, 3663, 3640, 3618, 3596, 3575, 3554, 3533, 3513, 3493, 3473, 3454, 3435, 3416, 3397,
1842 3379, 3361, 3344, 3327, 3310, 3293, 3276, 3260, 3244, 3228, 3213, 3197, 3182, 3167, 3153,
1843 3138, 3124, 3110, 3096, 3082, 3069, 3055, 3042, 3029, 3016, 3003, 2991, 2978, 2966, 2954,
1844 2942, 2930, 2919, 2907, 2896, 2885, 2873, 2862, 2852, 2841, 2830, 2820, 2809, 2799, 2789,
1845 2779, 2769, 2759, 2749, 2740, 2730, 2721, 2711, 2702, 2693, 2684, 2675, 2666, 2657, 2649,
1846 2640, 2631, 2623, 2615, 2606, 2598, 2590, 2582, 2574, 2566, 2558, 2550, 2543, 2535, 2528,
1847 2520, 2513, 2505, 2498, 2491, 2484, 2477, 2469, 2462, 2456, 2449, 2442, 2435, 2428, 2422,
1848 2415, 2409, 2402, 2396, 2389, 2383, 2377, 2371, 2364, 2358, 2352, 2346, 2340, 2334, 2328,
1849 2322, 2317, 2311, 2305, 2299, 2294, 2288, 2283, 2277, 2272, 2266, 2261, 2255, 2250, 2245,
1850 2239, 2234, 2229, 2224, 2219, 2214, 2209, 2204, 2199, 2194, 2189, 2184, 2179, 2174, 2170,
1851 2165, 2160, 2155, 2151, 2146, 2142, 2137, 2133, 2128, 2124, 2119, 2115, 2110, 2106, 2102,
1852 2097, 2093, 2089, 2084, 2080, 2076, 2072, 2068, 2064, 2060, 2056, 2052,
1853 ];
1854
1855 #[test]
1856 fn generated_rsqrt_table_matches_the_previous_table() {
1857 assert_eq!(RSQRT_Q12_FROM_64, PREVIOUS_TABLE);
1858 }
1859}