client/tests/texture_resampling.c (7876B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_25140DFB(void); 5 void sub_25142392(void); 6 void sub_251418C6(void); 7 void sub_25141467(void); 8 void sub_25141276(void); 9 void sub_2514132C(void); 10 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 11 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 12 recomp_func_t recomp_lookup(uint32_t address) 13 { 14 if (address == 0x251418c6) return sub_251418C6; 15 if (address == 0x25141467) return sub_25141467; 16 if (address == 0x25141276) return sub_25141276; 17 if (address == 0x2514132c) return sub_2514132C; 18 assert(address == 0x25142392); 19 return sub_25142392; 20 } 21 22 static unsigned yield_count; 23 static void observe_yield(void) { ++yield_count; } 24 25 int main(void) 26 { 27 assert(guest_map("texture resampling", 0x50000000, 0x20000)); 28 assert(guest_map("resampling stack", 0x51000000, 0x20000)); 29 /* BGR source pixels and the scratch stack cross guest page boundaries. */ 30 const unsigned char pixels[] = {0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255}; 31 /* Integer, fractional, clipped, reversed and fully outside rows. */ 32 const int positions[][3] = {{0, 0, 32768}, {0, 32768, 32768}, {-32768, 32768, 32768}, {98304, 32768, -32768}, {0, -65536, 32768}, {0, 131072, 32768}}; 33 const uint32_t expected[][4] = { 34 {0xffff0000, 0xff7f7f00, 0xff00ff00, 0xff00ff00}, 35 {0xff7f007f, 0xff7f7f7f, 0xff7fff7f, 0xff7fff7f}, 36 {0xfe7f007e, 0xff7f007f, 0xff7f7f7f, 0xff7fff7f}, 37 {0xff7fff7f, 0xff7fff7f, 0xff7f7f7f, 0xff7f007f}, 38 {0xfefe0000, 0xfe7f7e00, 0xfe00fe00, 0xfe00fe00}, 39 {0, 0, 0, 0} 40 }; 41 const struct { uint32_t bits, reader; } formats[] = { 42 {24, 0x25142392}, {8, 0x251418c6}, {16, 0x25141467}, 43 {32, 0x25141276}, {32, 0x2514132c} 44 }; 45 for (unsigned format = 0; format < 5; ++format) { 46 for (unsigned i = 0; i < 6; ++i) { 47 memset((void *)ADDR(0x50000000), 0xa5, 0x20000); 48 if (formats[format].bits == 8) { 49 const unsigned char indices[] = {255, 3, 77, 9}; 50 memcpy((void *)ADDR(0x5000fffe), indices, sizeof(indices)); 51 MEM32(0x50008000 + 255 * 4) = 0x120000ff; 52 MEM32(0x50008000 + 3 * 4) = 0x3400ff00; 53 MEM32(0x50008000 + 77 * 4) = 0x56ff0000; 54 MEM32(0x50008000 + 9 * 4) = 0x78ffffff; 55 MEM32(0x5000102c) = 0x50008000; 56 } else if (formats[format].bits == 16) { 57 const uint16_t colors[] = {0xf800, 0x07e0, 0x001f, 0xffff}; 58 memcpy((void *)ADDR(0x5000fffe), colors, sizeof(colors)); 59 } else if (formats[format].bits == 32) { 60 const uint32_t colors[] = {0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffffff}; 61 memcpy((void *)ADDR(0x5000fffe), colors, sizeof(colors)); 62 } else { 63 memcpy((void *)ADDR(0x5000fffe), pixels, sizeof(pixels)); 64 } 65 MEM32(0x50001020) = formats[format].bits; 66 MEM32(0x50001024) = formats[format].bits / 4; 67 MEM32(0x50001028) = 0x5000fffe; 68 MEM32(0x50001054) = 0; MEM32(0x50001058) = 0; 69 MEM32(0x5000105c) = 2; MEM32(0x50001060) = 2; 70 MEM32(0x50001090) = positions[i][2]; 71 MEM32(0x500010a8) = positions[i][0] + 0x7fff; 72 MEM32(0x500010ac) = positions[i][1] + 0x7fff; 73 MEM32(0x500010b8) = formats[format].reader; 74 g_esp = 0x51010020; g_ebp = 123; 75 g_ebx = 234; g_esi = 345; g_edi = 456; 76 MEM32(g_esp + 4) = 4; 77 MEM32(g_esp + 8) = 0x50006000; 78 MEM32(g_esp + 12) = 0x50001000; 79 80 sub_25140DFB(); 81 82 for (unsigned j = 0; j < 4; ++j) 83 assert(MEM32(0x50006000 + j * 4) == expected[i][j]); 84 assert(g_esp == 0x51010024 && g_ebp == 123); 85 assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); 86 assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x50006010) == 0xa5a5a5a5); 87 } 88 } 89 90 const uint32_t colors[] = { 91 0xff0100ff, 0, 0x8001ff02, 0xffffffff, 92 0x01ff0100, 0x010001ff, 0, 0x01020304 93 }; 94 const uint32_t opaque_expected[][4] = { 95 {0xfe0000fe, 0xfe000000, 0xfe00fe01, 0xfefefefe}, 96 {0xfefe0000, 0xfe0000fe, 0xfe000000, 0xff020304} 97 }; 98 const uint32_t alpha_expected[][4] = { 99 {0xfe0000fe, 0, 0x7f00fe01, 0xfefefefe}, 100 {0x00fe0000, 0x000000fe, 0, 0x01020304} 101 }; 102 for (unsigned format = 0; format < 3; ++format) { 103 for (unsigned row = 0; row < 2; ++row) { 104 memset((void *)ADDR(0x50000000), 0xa5, 0x20000); 105 unsigned bytes = format == 0 ? 3 : 4; 106 for (unsigned i = 0; i < 8; ++i) 107 for (unsigned channel = 0; channel < bytes; ++channel) 108 MEM8(0x5000fffe + i * bytes + channel) = colors[i] >> (channel * 8); 109 MEM32(0x50001020) = bytes * 8; 110 MEM32(0x50001024) = bytes * 4; 111 MEM32(0x50001028) = 0x5000fffe; 112 MEM32(0x50001054) = 0; MEM32(0x50001058) = 0; 113 MEM32(0x5000105c) = 4; MEM32(0x50001060) = 2; 114 MEM32(0x50001090) = 65536; 115 MEM32(0x500010a8) = 0x8000; 116 MEM32(0x500010ac) = row * 65536 + 0x8000; 117 MEM32(0x500010b8) = format == 0 ? 0x25142392 : format == 1 ? 0x25141276 : 0x2514132c; 118 g_esp = 0x51010020; g_ebp = 123; 119 g_ebx = 234; g_esi = 345; g_edi = 456; 120 MEM32(g_esp + 4) = 4; 121 MEM32(g_esp + 8) = 0x50006000; 122 MEM32(g_esp + 12) = 0x50001000; 123 recomp_yield_hook = observe_yield; 124 g_backedges = RECOMP_YIELD_EVERY - 1; 125 yield_count = 0; 126 127 sub_25140DFB(); 128 129 for (unsigned x = 0; x < 4; ++x) 130 assert(MEM32(0x50006000 + x * 4) == 131 (format == 1 ? alpha_expected[row][x] : opaque_expected[row][x])); 132 assert(MEM32(0x500010a8) == 0x40001); 133 assert(MEM32(0x500010ac) == row * 65536 + 1); 134 assert(MEM32(0x500010b4) == (row ? 14 : 10)); 135 assert(g_eax == 0x50006000 && g_esp == 0x51010024 && g_ebp == 123); 136 assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); 137 assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x50006010) == 0xa5a5a5a5); 138 assert(yield_count > 0); 139 } 140 } 141 142 const uint32_t mip_pixels[] = { 143 0xff0000ff, 0x8000ff00, 0x01020304, 0, 0, 0, 144 0x40ff0000, 0xffffffff, 0, 0, 0, 0 145 }; 146 const uint32_t mip_expected[][3] = { 147 {0xae7f7f7e, 0, 0}, {0xfe7f7f7e, 0xfe000000, 0xfe000000} 148 }; 149 for (unsigned opaque = 0; opaque < 2; ++opaque) { 150 memset((void *)ADDR(0x50000000), 0xa5, 0x20000); 151 memcpy((void *)ADDR(0x5000fffe), mip_pixels, sizeof(mip_pixels)); 152 MEM32(0x50001020) = 32; MEM32(0x50001024) = 24; 153 MEM32(0x50001028) = 0x5000fffe; 154 MEM32(0x50001054) = 0; MEM32(0x50001058) = 0; 155 MEM32(0x5000105c) = 6; MEM32(0x50001060) = 2; 156 MEM32(0x50001090) = 131072; 157 MEM32(0x500010a8) = 65536; MEM32(0x500010ac) = 65536; 158 MEM32(0x500010b8) = opaque ? 0x2514132c : 0x25141276; 159 g_esp = 0x51010020; 160 MEM32(g_esp + 4) = 3; MEM32(g_esp + 8) = 0x50006000; MEM32(g_esp + 12) = 0x50001000; 161 recomp_yield_hook = NULL; 162 g_backedges = g_icall_count = 0; 163 164 sub_25140DFB(); 165 166 for (unsigned x = 0; x < 3; ++x) 167 assert(MEM32(0x50006000 + x * 4) == mip_expected[opaque][x]); 168 assert(MEM32(0x500010b4) == (opaque ? 0 : 15)); 169 assert(MEM32(0x500010a8) == 0x68001 && MEM32(0x500010ac) == 0x8001); 170 assert(g_backedges == (opaque ? 27 : 23) && g_icall_count == 3); 171 assert(g_eax == 0x50006000 && g_esp == 0x51010024); 172 assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x5000600c) == 0xa5a5a5a5); 173 } 174 }