client/tests/skinning.c (5319B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_250F66E0(void); 5 static unsigned events[4], event_count; 6 7 static void lock_vertices(void) 8 { 9 assert(event_count == 0); 10 events[event_count++] = 1; 11 g_eax = MEM32(g_ecx); 12 g_esp += 4; 13 } 14 15 static void unlock_vertices(void) 16 { 17 assert(event_count == 3); 18 events[event_count++] = 2; 19 g_eax = 0; 20 g_esp += 4; 21 } 22 23 static void callback(void) 24 { 25 assert(event_count == 2); 26 events[event_count++] = 3; 27 assert(MEM32(g_esp + 4) == 0x50000000); 28 assert(MEM32(g_esp + 8) == 1); 29 assert(MEM32(g_esp + 36) == 123); 30 MEM32(MEM32(g_esp + 16) + 24) = 0x3f000000; 31 MEM32(MEM32(g_esp + 16) + 28) = 0x3e800000; 32 g_esp += 4; 33 } 34 35 static void yield(void) 36 { 37 assert(event_count == 1); 38 events[event_count++] = 4; 39 } 40 41 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 42 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 43 recomp_func_t recomp_lookup(uint32_t address) 44 { 45 switch (address) { 46 case 0x250f72e0: return lock_vertices; 47 case 0x25118ef0: return unlock_vertices; 48 case 0x70000000: return callback; 49 default: abort(); 50 } 51 } 52 53 static void arrange(void) 54 { 55 /* One section, one vertex, two bones, and one post-transform callback. */ 56 memset((void *)ADDR(0x50000000), 0, 0x10000); 57 MEM32(0x25151778) = 0x3f7d70a4; 58 MEM32(0x251517a4) = 0x3f800000; 59 MEM32(0x50000008) = 1; 60 MEM32(0x5000000c) = 1; 61 MEM32(0x50000010) = 0x50000100; 62 MEM32(0x50000018) = 0x50001000; 63 MEM32(0x50000030) = 0x50000500; 64 MEM32(0x50000034) = 0x50000508; 65 MEM32(0x50000500) = 123; 66 MEM32(0x50000504) = 0x70000000; 67 MEM32(0x50000100) = 0x50000200; 68 MEM32(0x50000108) = 0x50000300; 69 MEM32(0x50000244) = 0x50000400; 70 MEM32(0x50000410) = 1; 71 MEM32(0x50000414) = 0x50000600; 72 MEM32(0x50000300) = 0x50008000; 73 MEM32(0x5000060c) = 0x50002000; 74 MEM32(0x50000614) = 0x50000700; 75 MEM32(0x5000062c) = 1; 76 MEM32(0x50000630) = 2; 77 78 /* Bind positions, copied position, normal, bone indices, and weight. */ 79 const float matrices[] = {1,0,0, 0,1,0, 0,0,1, 10,20,30, 80 1,0,0, 0,1,0, 0,0,1, -10,-20,-30}; 81 const float vertex[] = {1,2,3, 5,6,7, -7,8,9, 1,2,3}; 82 memcpy((void *)ADDR(0x50001000), matrices, sizeof(matrices)); 83 memcpy((void *)ADDR(0x50002000), vertex, sizeof(vertex)); 84 MEM32(0x50002038) = 0; 85 MEM32(0x5000203c) = 1; 86 MEM32(0x50002040) = 0x3f800000; 87 88 g_eax = 123; g_ecx = 0x50000000; g_edx = 234; g_ebx = 345; 89 g_esp = 0x5000ff00; g_ebp = 456; g_esi = 567; g_edi = 678; 90 g_flag_k = FK_CMP; g_flag_a = 111; g_flag_b = 222; g_flag_cf = 1; 91 g_fp_top = 6; 92 g_fpu_cw = 0x027f; 93 for (unsigned i = 0; i < 8; ++i) { 94 g_st[i] = i + .125; 95 g_st_i64[i] = INT64_MAX - i; 96 } 97 g_backedges = 65535; 98 recomp_yield_hook = yield; 99 event_count = 0; 100 } 101 102 static void test_vertices(unsigned kind) 103 { 104 arrange(); 105 if (kind == 1) MEM32(0x50002040) = 0x3e800000; 106 if (kind == 2) MEM8(0x50000028) = 1; 107 if (kind == 3) MEM32(0x50000300) = 0x50001000; 108 109 sub_250F66E0(); 110 111 const float expected[][8] = { 112 {11,22,33, 1,2,3, .5f,.25f}, 113 {-1,-5,-9, 1,2,3, .5f,.25f}, 114 {-7,8,9, 1,2,3, .5f,.25f}, 115 {11,22,33, 11,24,36, .5f,.25f}, 116 }; 117 uint32_t output = kind == 3 ? 0x50001000 : 0x50008000; 118 assert(!memcmp((void *)ADDR(output), expected[kind], sizeof(expected[kind]))); 119 assert(g_esp == 0x5000ff04 && g_ebp == 456 && g_ebx == 345 && g_esi == 567 && g_edi == 678); 120 assert(g_fp_top == 6 && g_fpu_cw == 0x027f); 121 assert(g_st[0] == .125 && g_st[2] == 2.125); 122 assert(g_st_i64[0] == INT64_MAX && g_st_i64[2] == INT64_MAX - 2); 123 unsigned retained = kind == 1 ? 3 : kind == 2 ? 8 : 6; 124 for (unsigned i = 0; i < 8; ++i) { 125 assert(g_st[i] == (i < retained ? i + .125 : 0)); 126 assert(g_st_i64[i] == (i < retained ? INT64_MAX - i : 0)); 127 } 128 const unsigned expected_events[] = {1,4,3,2}; 129 assert(event_count == 4 && !memcmp(events, expected_events, sizeof(events))); 130 } 131 132 static void test_double_intermediates(void) 133 { 134 arrange(); 135 const float position[] = {16777216,1,-16777216}; 136 memcpy((void *)ADDR(0x50002000), position, sizeof(position)); 137 *(float *)ADDR(0x5000100c) = 1; 138 *(float *)ADDR(0x50001018) = 1; 139 *(float *)ADDR(0x50001024) = 0; 140 141 sub_250F66E0(); 142 143 assert(*(float *)ADDR(0x50008000) == 1); 144 } 145 146 static void test_nan_weight(uint32_t weight) 147 { 148 arrange(); 149 MEM32(0x50002040) = weight; 150 151 sub_250F66E0(); 152 153 for (unsigned i = 0; i < 3; ++i) { 154 uint32_t word = MEM32(0x50008000 + i * 4); 155 assert((word & 0x7fc00000) == 0x7fc00000); 156 } 157 const float normal_and_uv[] = {1,2,3, .5f,.25f}; 158 assert(!memcmp((void *)ADDR(0x5000800c), normal_and_uv, sizeof(normal_and_uv))); 159 } 160 161 int main(void) 162 { 163 assert(guest_map("skinning fixture", 0x50000000, 0x10000)); 164 assert(guest_map("skinning constants", 0x25150000, 0x10000)); 165 for (unsigned kind = 0; kind < 4; ++kind) test_vertices(kind); 166 test_double_intermediates(); 167 test_nan_weight(0x7fc12345); 168 test_nan_weight(0xffc12345); 169 test_nan_weight(0x7f800001); 170 test_nan_weight(0xff800001); 171 puts("skinning: positions, normals, aliases, callbacks, precision, NaNs and guest ABI passed"); 172 }