aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/terrain_occlusion.c (8053B)


      1 #include "recomp_types.h"
      2 #include <assert.h>
      3 
      4 void sub_250FFBB0(void);
      5 enum { occluder=0x50001000, camera=0x50002000, viewport=0x50003000,
      6     world=0x50004000, rows=0x50005000, horizons=0x50006000, heights=0x50010000 };
      7 
      8 /* Camera and vector boundaries supply a fixed view over a flat heightmap. */
      9 static void stack_probe(void) { g_esp += 4 - g_eax; }
     10 static void camera_matrix(void) { g_eax=MEM32(g_esp+4); g_esp+=8; }
     11 static void position(void) { g_eax=world; g_esp+=4; }
     12 static void height(void) { fp_push_impl(g_st,&g_fp_top,0); g_esp+=8; }
     13 static void quaternion(void)
     14 {
     15     float v[3], q[4];
     16     memcpy(v,(void *)ADDR(g_ecx),12);
     17     memcpy(q,(void *)ADDR(MEM32(g_esp+4)),16);
     18     float cross[]={q[1]*v[2]-q[2]*v[1], q[2]*v[0]-q[0]*v[2], q[0]*v[1]-q[1]*v[0]};
     19     float twice[]={q[1]*cross[2]-q[2]*cross[1], q[2]*cross[0]-q[0]*cross[2], q[0]*cross[1]-q[1]*cross[0]};
     20     for(unsigned i=0;i<3;++i) *(float *)ADDR(g_ecx+i*4)=v[i]+2*q[3]*cross[i]+2*twice[i];
     21     g_eax=g_ecx; g_esp+=8;
     22 }
     23 static void matrix_product(void)
     24 {
     25     float matrix[16]={1,0,0,0, 0,1,0,0, 0,0,-1,0, -64,-4,64,1};
     26     g_eax=MEM32(g_esp+4);
     27     memcpy((void *)ADDR(g_eax),matrix,sizeof(matrix));
     28     g_esp+=12;
     29 }
     30 static void cross(void)
     31 {
     32     float lhs[3],rhs[3];
     33     memcpy(lhs,(void *)ADDR(g_ecx),12);
     34     memcpy(rhs,(void *)ADDR(MEM32(g_esp+8)),12);
     35     float result[]={lhs[1]*rhs[2]-lhs[2]*rhs[1], lhs[2]*rhs[0]-lhs[0]*rhs[2], lhs[0]*rhs[1]-lhs[1]*rhs[0]};
     36     g_eax=MEM32(g_esp+4);
     37     memcpy((void *)ADDR(g_eax),result,12);
     38     g_esp+=12;
     39 }
     40 static void scale(void)
     41 {
     42     float factor=*(float *)ADDR(g_esp+4);
     43     for(unsigned i=0;i<12;i+=4) *(float *)ADDR(g_ecx+i)*=factor;
     44     g_eax=g_ecx; g_esp+=8;
     45 }
     46 static void add(void)
     47 {
     48     uint32_t rhs=MEM32(g_esp+4);
     49     for(unsigned i=0;i<12;i+=4) *(float *)ADDR(g_ecx+i)+=*(float *)ADDR(rhs+i);
     50     g_eax=g_ecx; g_esp+=8;
     51 }
     52 static void floor_value(void) { fp_push_impl(g_st, &g_fp_top, floor(*(double *)ADDR(g_esp+4))); g_esp+=4; }
     53 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
     54 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     55 recomp_func_t recomp_lookup(uint32_t address)
     56 {
     57     switch(address) {
     58     case 0x70000014: return floor_value;
     59     case 0x25138170: return stack_probe;
     60     case 0x250f0f70: case 0x250f0fa0: return camera_matrix;
     61     case 0x250e2ec0: return position;
     62     case 0x25101360: return height;
     63     case 0x70000000: return quaternion;
     64     case 0x70000004: return matrix_product;
     65     case 0x70000008: return cross;
     66     case 0x7000000c: return scale;
     67     case 0x70000010: return add;
     68     default: fprintf(stderr,"Unexpected occlusion dependency: %08x\n",address); abort();
     69     }
     70 }
     71 
     72 static unsigned yield_count;
     73 static uint64_t yield_hash;
     74 static void yield(void)
     75 {
     76     ++yield_count;
     77     const unsigned char *bytes = (const unsigned char *)g_st;
     78     for (unsigned i = 0; i < sizeof(g_st); ++i) yield_hash = yield_hash * 33 + bytes[i];
     79     bytes = (const unsigned char *)g_st_i64;
     80     for (unsigned i = 0; i < sizeof(g_st_i64); ++i) yield_hash = yield_hash * 33 + bytes[i];
     81 }
     82 static void arrange(unsigned index)
     83 {
     84     memset((void *)ADDR(0x50000000), 0, 0x30000);
     85     memset((void *)ADDR(0x25150000), 0, 0x70000);
     86     MEM32(occluder)=heights;
     87     MEM32(occluder+8)=MEM32(occluder+12)=64;
     88     *(float *)ADDR(occluder+16)=.25f;
     89     *(float *)ADDR(occluder+20)=2;
     90     MEM32(occluder+0x28)=index % 4 == 3 ? 127 : 128;
     91     MEM32(occluder+0x2c)=49;
     92     MEM32(occluder+0x30)=rows;
     93     MEM32(occluder+0x8c)=1;
     94     MEM32(viewport+12)=camera;
     95     MEM32(viewport+0x24)=1024; MEM32(viewport+0x28)=768;
     96     *(float *)ADDR(camera+0x90)=1;
     97     *(float *)ADDR(camera+0x138)=-1;
     98     *(float *)ADDR(camera+0x140)=1;
     99     *(float *)ADDR(world+0x30)=*(float *)ADDR(world+0x38)=64;
    100     *(float *)ADDR(world+0x34)=4;
    101     for(unsigned i=0;i<49;++i) {
    102         *(float *)ADDR(rows+i*20+4)=(i+1)*4;
    103         MEM32(rows+i*20+16)=horizons+i*0x200;
    104         memset((void *)ADDR(horizons+i*0x200),0xa5,512);
    105     }
    106     MEM32(0x25151260)=0x70000014;
    107     MEM32(0x2515157c)=0x70000000;
    108     MEM32(0x25151524)=0x70000004;
    109     MEM32(0x25151570)=0x70000008;
    110     MEM32(0x25151510)=0x7000000c;
    111     MEM32(0x251514f8)=0x70000010;
    112     *(float *)ADDR(0x25151788)=.5f;
    113     *(float *)ADDR(0x251517a4)=1;
    114     *(float *)ADDR(0x2515176c)=-1;
    115     *(float *)ADDR(0x25153f30)=10000;
    116     *(float *)ADDR(0x25151da0)=.0001f;
    117     *(double *)ADDR(0x25151790)=6.283185307179586;
    118     MEM32(0x25151780)=0x40490fdb;
    119     MEM32(0x25151784)=0x3e22f983;
    120     MEM32(0x25152c00)=0xbf000000;
    121     MEM32(0x2515412c)=0x40000000;
    122     MEM32(0x2515822c)=0x33d6bf95;
    123     g_esp=0x5002f000; g_ecx=occluder; g_fs_base=0x50000100;
    124     g_ebx=123; g_esi=234; g_edi=345; g_ebp=456;
    125     MEM32(g_fs_base)=0x12345678;
    126     MEM32(g_esp+4)=viewport;
    127 
    128     for (unsigned i = 0; i < 4096; ++i)
    129         MEM16(heights+i*2) = (i*17 + index*31) % 240;
    130     *(float *)ADDR(camera+0x88) = sin(index*.05);
    131     *(float *)ADDR(camera+0x90) = cos(index*.05);
    132     *(float *)ADDR(world+0x34) = 4 + index%12;
    133     for (unsigned i = 0; i < 49; ++i)
    134         *(float *)ADDR(rows+i*20+4) = (i+1)*(2+index%8);
    135     for (unsigned i = 0; i < 8; ++i) {
    136         g_st[i] = (int)(i*23+index%15)-42.5;
    137         g_st_i64[i] = INT64_MAX-i-index;
    138     }
    139     g_fp_top = index%5;
    140     g_fpu_cw = 0x37f | ((index % 4) << 10);
    141     g_flag_k = g_flag_a = g_flag_b = g_flag_cf = 0;
    142     g_eax = g_edx = 0;
    143     g_backedges = g_icall_count = 0;
    144     yield_count = 0; yield_hash = 0;
    145 }
    146 
    147 static uint64_t horizon_hash(void)
    148 {
    149     uint64_t hash = UINT64_C(14695981039346656037);
    150     const unsigned char *bytes = (const unsigned char *)ADDR(horizons);
    151     for (unsigned i = 0; i < 49 * 512; ++i)
    152         hash = (hash ^ bytes[i]) * UINT64_C(1099511628211);
    153     return hash;
    154 }
    155 
    156 
    157 static void test_horizon_rounding_and_guest_boundaries(void)
    158 {
    159     /* Exact horizon bytes and FPU observations from
    160        client/game/bbr.dll/lifted-0064.c:sub_250FFBB0. Each case uses a different
    161        x87 rounding mode and a populated 49-row horizon, including padding. */
    162     const uint64_t expected_horizons[] = {
    163         UINT64_C(0xc8cde735760c600e), UINT64_C(0x01928dd57c222063),
    164         UINT64_C(0x0cb44d6b70f9277d), UINT64_C(0x010febe08ef77022)
    165     };
    166     const unsigned expected_yields[] = {584, 736, 852, 1007};
    167     const uint64_t expected_yield_hashes[] = {
    168         UINT64_C(0x456718a9740f9328), UINT64_C(0xdb069c23a804ff60),
    169         UINT64_C(0x16edf1c5ec0d6d30), UINT64_C(0xd4695bad58a89224)
    170     };
    171     recomp_yield_hook = yield;
    172     for (unsigned mode = 0; mode < 4; ++mode) {
    173         arrange(16 + mode);
    174 
    175         sub_250FFBB0();
    176 
    177         assert(horizon_hash() == expected_horizons[mode]);
    178         assert(yield_count == expected_yields[mode]);
    179         assert(yield_hash == expected_yield_hashes[mode]);
    180         assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
    181         assert(g_esp == 0x5002f008 && MEM32(g_fs_base) == 0x12345678);
    182         assert(g_fp_top == 1 + mode);
    183         assert(g_st[0] == -41.5 + mode && g_st[3] == 27.5 + mode);
    184         assert(g_st_i64[0] == INT64_MAX - 16 - mode);
    185         assert(g_st[4] == 0 && g_st_i64[4] == 0);
    186         assert(g_st[7] == 0 && g_st_i64[7] == 0);
    187     }
    188 }
    189 
    190 static void test_disabled_occlusion_preserves_fpu_state(void)
    191 {
    192     arrange(16);
    193     MEM32(occluder) = 0;
    194     double values[8];
    195     int64_t integers[8];
    196     memcpy(values, g_st, sizeof(values));
    197     memcpy(integers, g_st_i64, sizeof(integers));
    198 
    199     sub_250FFBB0();
    200 
    201     assert(!memcmp(values, g_st, sizeof(values)));
    202     assert(!memcmp(integers, g_st_i64, sizeof(integers)));
    203     assert(g_fp_top == 1 && yield_count == 0);
    204     assert(g_esp == 0x5002f008 && MEM32(g_fs_base) == 0x12345678);
    205     assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
    206 }
    207 
    208 int main(void)
    209 {
    210     assert(guest_map("occlusion fixture", 0x50000000, 0x30000));
    211     assert(guest_map("occlusion constants", 0x25150000, 0x70000));
    212     test_horizon_rounding_and_guest_boundaries();
    213     test_disabled_occlusion_preserves_fpu_state();
    214     puts("terrain occlusion: rounding, horizon, guest calls and yields passed");
    215 }