aorecomp

Anarchy Online classic in the browser

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


client/tests/surfaces.c (43512B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdarg.h>
      6 #ifdef _WIN32
      7 #include <windows.h>
      8 #include <ddraw.h>
      9 #include <stddef.h>
     10 _Static_assert(sizeof(DDSURFACEDESC2) == 124, "guest surface descriptor size");
     11 _Static_assert(offsetof(DDSURFACEDESC2, lpSurface) == 36, "guest pixels offset");
     12 static uint32_t scratch[1024];
     13 #else
     14 #include "linux_graphics.h"
     15 #include "linux_window.h"
     16 #include "guest_memory.h"
     17 #include "guest_heap.h"
     18 #include <SDL2/SDL.h>
     19 recomp_func_t recomp_lookup(uint32_t address)
     20 {
     21     assert(address == 0x11110000u);
     22     return linux_window_lookup("user32.dll", "DefWindowProcA");
     23 }
     24 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     25 #endif
     26 
     27 static uint32_t workspace(void)
     28 {
     29 #ifdef _WIN32
     30     return (uintptr_t)scratch;
     31 #else
     32     return 0x50001000u;
     33 #endif
     34 }
     35 
     36 static uint32_t *memory(uint32_t address)
     37 {
     38 #ifdef _WIN32
     39     return (void *)(uintptr_t)address;
     40 #else
     41     return (void *)ADDR(address);
     42 #endif
     43 }
     44 
     45 static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...)
     46 {
     47     uint32_t arguments[6] = {object};
     48     va_list list;
     49     va_start(list, count);
     50     for (unsigned i = 1; i < count; ++i) arguments[i] = va_arg(list, uint32_t);
     51     va_end(list);
     52     uint32_t address = memory(*memory(object))[slot];
     53 #ifdef _WIN32
     54     if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))(uintptr_t)address)(object);
     55     if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1]);
     56     if (count == 3) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2]);
     57     if (count == 4) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3]);
     58     if (count == 5) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3], arguments[4]);
     59     assert(count == 6);
     60     return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
     61 #else
     62     uint32_t stack = g_esp, caller = g_cur_func;
     63     g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab;
     64     for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]);
     65     PUSH32(g_esp, RECOMP_RETADDR);
     66     recomp_func_t function = linux_graphics_virtual(address);
     67     assert(function);
     68     function();
     69     assert(g_esp == stack && g_cur_func == caller);
     70     assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab);
     71     return g_eax;
     72 #endif
     73 }
     74 
     75 static uint32_t create_draw(void)
     76 {
     77     static const uint32_t iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
     78     uint32_t output = workspace(), guid = workspace() + 16;
     79     memcpy(memory(guid), iid, 16);
     80 #ifdef _WIN32
     81     uint32_t (WINAPI *create)(uint32_t, uint32_t, uint32_t, uint32_t) =
     82         (void *)GetProcAddress(LoadLibraryA("ddraw.dll"), "DirectDrawCreateEx");
     83     assert(create && !create(0, output, guid, 0));
     84 #else
     85     uint32_t stack = g_esp;
     86     PUSH32(g_esp, 0); PUSH32(g_esp, guid); PUSH32(g_esp, output); PUSH32(g_esp, 0);
     87     PUSH32(g_esp, RECOMP_RETADDR);
     88     linux_graphics_lookup("DirectDrawCreateEx")();
     89     assert(!g_eax && g_esp == stack);
     90 #endif
     91     uint32_t object = *memory(output);
     92     assert(!invoke(object, 20, 3, 0u, 0x808u));
     93     return object;
     94 }
     95 
     96 static uint32_t create_surface(uint32_t draw, uint32_t caps)
     97 {
     98     uint32_t desc = workspace() + 128;
     99     memset(memory(desc), 0, 124);
    100     *memory(desc) = 124;
    101     *memory(desc + 4) = caps == 0x200 ? 1 : 7;
    102     if (caps != 0x200) { *memory(desc + 8) = 16; *memory(desc + 12) = 32; }
    103     *memory(desc + 104) = caps;
    104     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    105     return *memory(workspace());
    106 }
    107 
    108 static void surface_lifetime_and_description(void)
    109 {
    110     uint32_t draw = create_draw(), primary = create_surface(draw, 0x200);
    111     uint32_t desc = workspace() + 128;
    112     uint32_t duplicate = invoke(draw, 6, 4, desc, workspace(), 0u);
    113     uint32_t offscreen = create_surface(draw, 0x2040);
    114     memset(memory(desc), 0xa5, 128);
    115     *memory(desc) = 124;
    116 
    117     uint32_t result = invoke(offscreen, 22, 2, desc);
    118     uint32_t owner_result = invoke(offscreen, 36, 2, workspace());
    119     uint32_t owner = *memory(workspace());
    120 
    121     assert(duplicate == 0x88760234u && !result && !owner_result && owner == draw);
    122     const uint32_t expected[31] = {
    123         124, 0x100f, 16, 32, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    124         32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0, 0x10006040, 0, 0, 0, 0
    125     };
    126     assert(!memcmp(memory(desc), expected, sizeof(expected)));
    127     assert(*memory(desc + 124) == 0xa5a5a5a5);
    128     uint32_t format = workspace() + 256;
    129     memset(memory(format), 0xa5, 36);
    130     *memory(format) = 32;
    131     assert(!invoke(offscreen, 21, 2, format));
    132     assert(!memcmp(memory(format), expected + 18, 32));
    133     assert(*memory(format + 32) == 0xa5a5a5a5);
    134     assert(invoke(owner, 2, 1) == 3);
    135     assert(!invoke(primary, 22, 2, desc));
    136     assert(*memory(desc + 104) == 0x1000c200);
    137 #ifdef _WIN32
    138     assert(*memory(desc + 12) == (uint32_t)GetSystemMetrics(SM_CXSCREEN));
    139     assert(*memory(desc + 8) == (uint32_t)GetSystemMetrics(SM_CYSCREEN));
    140 #else
    141     SDL_DisplayMode mode;
    142     assert(!SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(linux_graphics_window()), &mode));
    143     assert(*memory(desc + 12) == (uint32_t)mode.w && *memory(desc + 8) == (uint32_t)mode.h);
    144 #endif
    145 
    146     /* IUnknown is the older surface interface and retains the owner separately. */
    147     const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
    148     memcpy(memory(workspace() + 16), unknown, 16);
    149     assert(!invoke(offscreen, 0, 3, workspace() + 16, workspace()));
    150     uint32_t held = *memory(workspace());
    151     assert(held != offscreen);
    152     assert(!invoke(held, 0, 3, workspace() + 16, workspace()));
    153     assert(*memory(workspace()) == held && invoke(held, 2, 1) == 1);
    154     const uint32_t surface7[] = {0x06675a80, 0x11d23b9b, 0x60002fb9, 0x5bea9797};
    155     memcpy(memory(workspace() + 16), surface7, 16);
    156     assert(!invoke(held, 0, 3, workspace() + 16, workspace()));
    157     assert(*memory(workspace()) == offscreen && invoke(offscreen, 2, 1) == 1);
    158     assert(invoke(held, 1, 1) == 2 && invoke(held, 2, 1) == 1);
    159     assert(!invoke(offscreen, 2, 1));
    160     assert(invoke(draw, 1, 1) == 4 && invoke(draw, 2, 1) == 3);
    161     assert(!invoke(held, 2, 1));
    162     assert(invoke(draw, 1, 1) == 3 && invoke(draw, 2, 1) == 2);
    163     assert(!invoke(primary, 2, 1));
    164     /* Releasing the primary permits a new one. Surfaces keep a released owner alive. */
    165     primary = create_surface(draw, 0x200);
    166     assert(invoke(draw, 2, 1) == 1);
    167     assert(!invoke(primary, 36, 2, workspace()) && *memory(workspace()) == draw);
    168     assert(invoke(draw, 2, 1) == 1);
    169     assert(!invoke(primary, 2, 1));
    170 }
    171 
    172 static void surface_pixels_round_trip(uint32_t caps)
    173 {
    174     uint32_t draw = create_draw(), surface = create_surface(draw, caps);
    175     uint32_t desc = workspace() + 128, info = workspace() + 256;
    176     *memory(desc) = 124;
    177     assert(!invoke(surface, 25, 5, 0u, desc, 0x21u, 0u));
    178     uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    179     assert(pixels && pitch == 128 && *memory(desc + 4) == 0x100f);
    180     assert(*memory(desc + 104) == (caps == 0x840 ? 0x840 : 0x10006040));
    181     for (unsigned y = 0; y < 16; ++y)
    182         for (unsigned x = 0; x < 32; ++x)
    183             *memory(pixels + y * pitch + x * 4) = (y << 16) | (x << 8) | (x ^ y);
    184     *memory(info) = 124;
    185     assert(!invoke(surface, 22, 2, info) && !*memory(info + 36));
    186 
    187     assert(!invoke(surface, 32, 2, 0u));
    188     uint32_t unlocked = invoke(surface, 32, 2, 0u);
    189     assert(!invoke(surface, 25, 5, 0u, desc, 0x11u, 0u));
    190     pixels = *memory(desc + 36);
    191     pitch = *memory(desc + 16);
    192 
    193     assert(unlocked == 0x88760248u);
    194     for (unsigned y = 0; y < 16; ++y)
    195         for (unsigned x = 0; x < 32; ++x)
    196             assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == ((y << 16) | (x << 8) | (x ^ y)));
    197     assert(!invoke(surface, 32, 2, 0u));
    198     assert(!invoke(surface, 24, 1) && !invoke(surface, 27, 1));
    199     assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1));
    200 }
    201 
    202 #ifndef _WIN32
    203 static void batched_texture_writes_preserve_reads_and_blits(uint32_t caps2)
    204 {
    205     uint32_t draw = create_draw(), output = create_surface(draw, 0x2040);
    206     uint32_t desc = workspace() + 128, rect = workspace() + 64;
    207     const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0};
    208     memset(memory(desc), 0, 124);
    209     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    210     *memory(desc + 8) = 4; *memory(desc + 12) = 4;
    211     memcpy(memory(desc + 72), format, sizeof(format));
    212     *memory(desc + 104) = 0x1000; *memory(desc + 108) = caps2;
    213     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    214     uint32_t texture = *memory(workspace());
    215 
    216     assert(!linux_graphics_begin_texture_update(output));
    217     linux_graphics_end_texture_update(0);
    218     assert(linux_graphics_begin_texture_update(texture) == texture);
    219     assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
    220     uint32_t pixels = *memory(desc + 36);
    221     for (unsigned i = 0; i < 16; ++i) *memory(pixels + i * 4) = 0x00112233;
    222     assert(!invoke(texture, 32, 2, 0u));
    223     assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    224     assert(*memory(desc + 36) == pixels && *memory(pixels) == 0x00112233);
    225     assert(!invoke(texture, 32, 2, 0u));
    226 
    227     // A nested partial update must retain the surrounding pixels and guest layout.
    228     assert(linux_graphics_begin_texture_update(texture) == texture);
    229     const int32_t middle[] = {1, 1, 3, 3};
    230     memcpy(memory(rect), middle, sizeof(middle));
    231     assert(!invoke(texture, 25, 5, rect, desc, 0x21u, 0u));
    232     assert(*memory(desc + 36) == pixels + 20);
    233     for (unsigned y = 1; y < 3; ++y) for (unsigned x = 1; x < 3; ++x)
    234         *memory(pixels + y * 16 + x * 4) = 0x00445566;
    235     assert(!invoke(texture, 32, 2, rect));
    236     linux_graphics_end_texture_update(texture);
    237 
    238     // A GPU consumer inside the batch sees every completed write.
    239     const int32_t full[] = {0, 0, 4, 4};
    240     memcpy(memory(rect), full, sizeof(full));
    241     assert(!invoke(output, 5, 6, rect, texture, 0u, 0x1000000u, 0u));
    242     assert(!invoke(output, 25, 5, rect, desc, 0x11u, 0u));
    243     for (unsigned y = 0; y < 4; ++y) for (unsigned x = 0; x < 4; ++x)
    244         assert((*memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4) & 0xffffff) ==
    245             (x >= 1 && x < 3 && y >= 1 && y < 3 ? 0x00445566u : 0x00112233u));
    246     assert(!invoke(output, 32, 2, rect));
    247     assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    248     assert((*memory(*memory(desc + 36)) & 0xffffff) == 0x00112233);
    249     assert((*memory(*memory(desc + 36) + 20) & 0xffffff) == 0x00445566);
    250     assert(!invoke(texture, 32, 2, 0u));
    251 
    252     // A blit into the batch invalidates its retained CPU copy before the next lock.
    253     assert(!invoke(output, 25, 5, rect, desc, 0x21u, 0u));
    254     *memory(*memory(desc + 36)) = 0x00778899;
    255     assert(!invoke(output, 32, 2, rect));
    256     assert(!invoke(texture, 5, 6, 0u, output, rect, 0x1000000u, 0u));
    257     assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    258     assert((*memory(*memory(desc + 36)) & 0xffffff) == 0x00778899);
    259     assert(!invoke(texture, 32, 2, 0u));
    260     linux_graphics_end_texture_update(texture);
    261     assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    262     assert((*memory(*memory(desc + 36)) & 0xffffff) == 0x00778899);
    263     assert(!invoke(texture, 32, 2, 0u));
    264     assert(!invoke(texture, 2, 1) && !invoke(output, 2, 1) && !invoke(draw, 2, 1));
    265 }
    266 
    267 static void uploaded_surface_releases_temporary_pixels(uint32_t caps2)
    268 {
    269     uint32_t draw = create_draw();
    270     uint32_t desc = workspace() + 128;
    271     const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0};
    272     memset(memory(desc), 0, 124);
    273     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    274     *memory(desc + 8) = 16; *memory(desc + 12) = 32;
    275     memcpy(memory(desc + 72), format, sizeof(format));
    276     *memory(desc + 104) = 0x1000; *memory(desc + 108) = caps2;
    277     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    278     uint32_t surface = *memory(workspace());
    279     *memory(desc) = 124;
    280     assert(!invoke(surface, 25, 5, 0u, desc, 0x21u, 0u));
    281     uint32_t pixels = *memory(desc + 36);
    282     for (unsigned i = 0; i < 512; ++i) *memory(pixels + i * 4) = 0x00112233;
    283 
    284     assert(!invoke(surface, 32, 2, 0u));
    285     uint32_t reused = guest_malloc(2048);
    286     memset(memory(reused), 0x55, 2048);
    287     assert(!invoke(surface, 25, 5, 0u, desc, 0x11u, 0u));
    288 
    289     assert(reused == pixels);
    290     for (unsigned i = 0; i < 512; ++i)
    291         assert((*memory(*memory(desc + 36) + i * 4) & 0xffffff) == 0x00112233);
    292     assert(!invoke(surface, 32, 2, 0u));
    293     guest_free(reused);
    294     assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1));
    295 }
    296 
    297 static void rgb565_textures_preserve_pixels_and_rectangle_locks(uint32_t caps2)
    298 {
    299     uint32_t draw = create_draw(), desc = workspace() + 128;
    300     const uint32_t format[] = {32, 0x40, 0, 16, 0xf800, 0x7e0, 0x1f, 0};
    301     memset(memory(desc), 0, 124);
    302     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    303     *memory(desc + 8) = 2; *memory(desc + 12) = 4;
    304     memcpy(memory(desc + 72), format, sizeof(format));
    305     *memory(desc + 104) = 0x1000; *memory(desc + 108) = caps2;
    306     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    307     uint32_t texture = *memory(workspace());
    308     assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
    309     uint16_t *pixels = (void *)memory(*memory(desc + 36));
    310     const uint16_t colors[] = {0xf800, 0x7e0, 0x1f, 0xffff, 0x1234, 0, 0x8410, 0x7bef};
    311     assert(*memory(desc + 16) == 8);
    312     assert(!memcmp(memory(desc + 72), format, sizeof(format)));
    313     memcpy(pixels, colors, sizeof(colors));
    314     assert(!invoke(texture, 32, 2, 0u));
    315     uint32_t rect = workspace() + 400;
    316     const int32_t rectangle[] = {1, 1, 3, 2};
    317     memcpy(memory(rect), rectangle, sizeof(rectangle));
    318 
    319     assert(!invoke(texture, 25, 5, rect, desc, 0x21u, 0u));
    320     pixels = (void *)memory(*memory(desc + 36));
    321     pixels[0] = 0x7e0;
    322     pixels[1] = 0xf800;
    323     assert(!invoke(texture, 32, 2, rect));
    324     assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    325     pixels = (void *)memory(*memory(desc + 36));
    326 
    327     const uint16_t expected[] = {0xf800, 0x7e0, 0x1f, 0xffff, 0x1234, 0x7e0, 0xf800, 0x7bef};
    328     assert(!memcmp(pixels, expected, sizeof(expected)));
    329     assert(!invoke(texture, 32, 2, 0u));
    330     assert(!invoke(texture, 2, 1) && !invoke(draw, 2, 1));
    331 }
    332 
    333 static void rgb565_blits_preserve_the_native_skin_merge_key(void)
    334 {
    335     uint32_t draw = create_draw(), source = create_surface(draw, 0x2040);
    336     uint32_t desc = workspace() + 128, rect = workspace() + 400;
    337     *memory(desc) = 124;
    338     assert(!invoke(source, 25, 5, 0u, desc, 0x21u, 0u));
    339     const uint32_t cloth[] = {0x00ff00, 0x123456, 0x0000ff, 0x00fe00};
    340     memcpy(memory(*memory(desc + 36)), cloth, sizeof(cloth));
    341     assert(!invoke(source, 32, 2, 0u));
    342     const uint32_t format[] = {32, 0x40, 0, 16, 0xf800, 0x7e0, 0x1f, 0};
    343     memset(memory(desc), 0, 124);
    344     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    345     *memory(desc + 8) = 1; *memory(desc + 12) = 4;
    346     memcpy(memory(desc + 72), format, sizeof(format));
    347     *memory(desc + 104) = 0x1000;
    348     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    349     uint32_t texture = *memory(workspace());
    350     const int32_t rectangle[] = {0, 0, 4, 1};
    351     memcpy(memory(rect), rectangle, sizeof(rectangle));
    352 
    353     assert(!invoke(texture, 5, 6, 0u, source, rect, 0x1000000u, 0u));
    354     assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
    355     uint16_t *pixels = (void *)memory(*memory(desc + 36));
    356     const uint16_t packed[] = {0x7e0, 0x11aa, 0x1f, 0x7e0};
    357     assert(!memcmp(pixels, packed, sizeof(packed)));
    358     for (unsigned i = 0; i < 4; ++i)
    359         if (pixels[i] == 0x7e0) pixels[i] = 0x9b07;
    360     assert(!invoke(texture, 32, 2, 0u));
    361     assert(!invoke(source, 5, 6, rect, texture, 0u, 0x1000000u, 0u));
    362     assert(!invoke(source, 25, 5, 0u, desc, 0x11u, 0u));
    363 
    364     const uint32_t merged[] = {0x9c6139, 0x103452, 0x0000ff, 0x9c6139};
    365     for (unsigned i = 0; i < 4; ++i)
    366         assert((memory(*memory(desc + 36))[i] & 0xffffff) == merged[i]);
    367     assert(!invoke(source, 32, 2, 0u));
    368     assert(!invoke(texture, 2, 1) && !invoke(source, 2, 1) && !invoke(draw, 2, 1));
    369 }
    370 #endif
    371 
    372 static void rectangle_locks_preserve_surrounding_pixels(uint32_t caps)
    373 {
    374     uint32_t draw = create_draw(), surface = create_surface(draw, caps);
    375     uint32_t desc = workspace() + 128, rect = workspace() + 64;
    376     *memory(desc) = 124;
    377     assert(!invoke(surface, 25, 5, 0u, desc, 0x821u, 0u));
    378     uint32_t base = *memory(desc + 36), pitch = *memory(desc + 16);
    379     for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x)
    380         *memory(base + y * pitch + x * 4) = 0x00112233;
    381     assert(!invoke(surface, 32, 2, 0u));
    382     const int32_t rectangle[] = {3, 5, 11, 13};
    383     memcpy(memory(rect), rectangle, 16);
    384 
    385     assert(!invoke(surface, 25, 5, rect, desc, 0x801u, 0u));
    386     uint32_t pixels = *memory(desc + 36);
    387     assert(pixels == base + 5 * pitch + 12);
    388     assert(*memory(desc + 8) == 16 && *memory(desc + 12) == 32 && *memory(desc + 16) == pitch);
    389     for (unsigned y = 0; y < 8; ++y) for (unsigned x = 0; x < 8; ++x)
    390         *memory(pixels + y * pitch + x * 4) = 0x0099aabb;
    391     *memory(rect) = 4;
    392     assert(invoke(surface, 32, 2, rect) == 0x88760248u);
    393     *memory(rect) = 3;
    394     assert(!invoke(surface, 32, 2, rect));
    395     assert(!invoke(surface, 25, 5, 0u, desc, 0x811u, 0u));
    396     pixels = *memory(desc + 36);
    397 
    398     for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x) {
    399         uint32_t expected = x >= 3 && x < 11 && y >= 5 && y < 13 ? 0x99aabb : 0x112233;
    400         assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == expected);
    401     }
    402     assert(!invoke(surface, 32, 2, 0u));
    403     const int32_t invalid[][4] = {{-1, 0, 4, 4}, {0, 0, 33, 16}, {8, 8, 4, 4}};
    404     for (unsigned i = 0; i < 3; ++i) {
    405         memcpy(memory(rect), invalid[i], 16);
    406         memset(memory(desc), 0xa5, 124); *memory(desc) = 124;
    407         assert(invoke(surface, 25, 5, rect, desc, 0x801u, 0u) == 0x80070057u);
    408         for (unsigned j = 1; j < 31; ++j) assert(*memory(desc + j * 4) == (j == 9 ? 0 : 0xa5a5a5a5));
    409     }
    410     const int32_t empty[] = {4, 4, 4, 8};
    411     memcpy(memory(rect), empty, 16);
    412     assert(!invoke(surface, 25, 5, rect, desc, 0x801u, 0u));
    413     assert(!invoke(surface, 32, 2, 0u));
    414     assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1));
    415 }
    416 
    417 static void clipper_attachment_retains_objects(void)
    418 {
    419     uint32_t draw = create_draw(), surface = create_surface(draw, 0x2040);
    420     assert(!invoke(draw, 4, 4, 0u, workspace(), 0u));
    421     uint32_t first = *memory(workspace());
    422     assert(!invoke(draw, 4, 4, 0u, workspace(), 0u));
    423     uint32_t second = *memory(workspace());
    424     assert(invoke(draw, 1, 1) == 5 && invoke(draw, 2, 1) == 4);
    425     assert(invoke(surface, 15, 2, workspace()) == 0x88760238u && !*memory(workspace()));
    426     assert(invoke(surface, 28, 2, 0u) == 0x88760238u);
    427     const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
    428     memcpy(memory(workspace() + 16), unknown, 16);
    429     assert(!invoke(first, 0, 3, workspace() + 16, workspace()));
    430     assert(*memory(workspace()) == first && invoke(first, 2, 1) == 1);
    431 
    432     assert(!invoke(surface, 28, 2, first));
    433     assert(!invoke(surface, 28, 2, first));
    434     assert(!invoke(surface, 15, 2, workspace()));
    435     uint32_t attached = *memory(workspace());
    436 
    437     assert(attached == first && invoke(attached, 2, 1) == 2);
    438     assert(!invoke(surface, 28, 2, second));
    439     assert(!invoke(first, 2, 1));
    440     assert(invoke(second, 2, 1) == 1);
    441     assert(invoke(draw, 2, 1) == 2);
    442     assert(!invoke(surface, 2, 1));
    443 }
    444 
    445 static void depth_surface_attachment_and_lifetime(void)
    446 {
    447     uint32_t draw = create_draw(), color = create_surface(draw, 0x6040);
    448     uint32_t desc = workspace() + 128, caps = workspace() + 64;
    449     memset(memory(desc), 0, 124);
    450     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    451     *memory(desc + 8) = 16; *memory(desc + 12) = 32;
    452     *memory(desc + 104) = 0x24000;
    453     const uint32_t format32[] = {32, 0x400, 0, 32, 0, 0xffffffffu, 0, 0};
    454     memcpy(memory(desc + 72), format32, 32);
    455     uint32_t unsupported = invoke(draw, 6, 4, desc, workspace(), 0u);
    456     const uint32_t format24[] = {32, 0x400, 0, 24, 0, 0xffffff, 0, 0};
    457     memcpy(memory(desc + 72), format24, 32);
    458     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    459     uint32_t depth = *memory(workspace());
    460     assert(!invoke(depth, 22, 2, desc));
    461     memset(memory(caps), 0, 16);
    462     *memory(caps) = 0x20000;
    463 
    464     assert(!invoke(color, 3, 2, depth));
    465     uint32_t repeated = invoke(color, 3, 2, depth);
    466     *memory(caps) = 0x401000;
    467     assert(invoke(color, 12, 3, caps, workspace()) == 0x887600ffu && !*memory(workspace()));
    468     *memory(caps) = 0x20000;
    469     assert(!invoke(color, 12, 3, caps, workspace()));
    470     uint32_t attached = *memory(workspace());
    471 
    472     assert(unsupported == 0x80004001u && repeated == 0x8876019au);
    473     assert(*memory(desc + 4) == 0x100f && *memory(desc + 16) == 128);
    474     assert(*memory(desc + 104) == 0x10024000);
    475     assert(!memcmp(memory(desc + 72), format24, 32));
    476     assert(attached == depth && invoke(attached, 2, 1) == 2);
    477     assert(!invoke(color, 8, 3, 0u, depth));
    478     assert(invoke(depth, 1, 1) == 2 && invoke(depth, 2, 1) == 1);
    479     assert(!invoke(color, 3, 2, depth));
    480     assert(invoke(depth, 2, 1) == 1);
    481     /* AO falls back directly to 16-bit depth after the 32-bit rejection. */
    482     memset(memory(desc), 0, 124);
    483     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    484     *memory(desc + 8) = 16; *memory(desc + 12) = 32;
    485     *memory(desc + 104) = 0x24000;
    486     const uint32_t format16[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0};
    487     memcpy(memory(desc + 72), format16, 32);
    488     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    489     uint32_t fallback = *memory(workspace());
    490     assert(!invoke(fallback, 22, 2, desc));
    491     assert(!memcmp(memory(desc + 72), format16, 32));
    492     assert(!invoke(color, 8, 3, 0u, depth));
    493     assert(!invoke(color, 3, 2, fallback));
    494     assert(invoke(fallback, 2, 1) == 1);
    495     assert(invoke(draw, 2, 1) == 2);
    496     assert(!invoke(color, 2, 1));
    497 }
    498 
    499 #ifndef _WIN32
    500 static uint32_t window_call(const char *name, unsigned count, const uint32_t *arguments)
    501 {
    502     uint32_t stack = g_esp;
    503     for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]);
    504     PUSH32(g_esp, RECOMP_RETADDR);
    505     linux_window_lookup("user32.dll", name)();
    506     assert(g_esp == stack);
    507     return g_eax;
    508 }
    509 #endif
    510 
    511 static void window_clipper_regions(void)
    512 {
    513     uint32_t draw = create_draw();
    514     assert(!invoke(draw, 4, 4, 0u, workspace(), 0u));
    515     uint32_t clipper = *memory(workspace()), output = workspace() + 256, size = workspace() + 32;
    516 #ifdef _WIN32
    517     HWND native = CreateWindowExA(0, "STATIC", "AO clipper test", 0, 20, 30, 320, 200,
    518                                   NULL, NULL, GetModuleHandleA(NULL), NULL);
    519     uint32_t handle = (uintptr_t)native;
    520 #else
    521     uint32_t record = workspace() + 512, text = workspace() + 560;
    522     memset(memory(record), 0, 40);
    523     *memory(record + 4) = 0x11110000u;
    524     *memory(record + 16) = 1;
    525     *memory(record + 36) = text;
    526     strcpy((char *)memory(text), "AO clipper test");
    527     uint32_t atom = window_call("RegisterClassA", 1, &record);
    528     assert(atom);
    529     uint32_t arguments[] = {0, atom, text, 0, 20, 30, 320, 200, 0, 0, 1, 0};
    530     uint32_t handle = window_call("CreateWindowExA", 12, arguments);
    531 #endif
    532     assert(handle);
    533     assert(!invoke(clipper, 4, 2, workspace()) && !*memory(workspace()));
    534     *memory(size) = 0;
    535     assert(invoke(clipper, 3, 4, 0u, 0u, size) == 0x887600cdu && !*memory(size));
    536     assert(invoke(clipper, 8, 3, 0u, 0x123u) == 0x80070057u);
    537 
    538     assert(!invoke(clipper, 8, 3, 0u, handle));
    539     assert(!invoke(clipper, 3, 4, 0u, 0u, size));
    540     uint32_t required = *memory(size);
    541     *memory(size) = 64;
    542     memset(memory(output), 0xa5, 64);
    543     uint32_t result = invoke(clipper, 3, 4, 0u, output, size);
    544 
    545     const uint32_t empty[] = {32, 1, 0, 0, 0, 0, 0, 0};
    546     assert(!result && required == 32 && *memory(size) == 64);
    547     assert(!memcmp(memory(output), empty, sizeof(empty)) && *memory(output + 32) == 0xa5a5a5a5);
    548     assert(invoke(clipper, 8, 3, 1u, handle) == 0x80070057u);
    549     assert(!invoke(clipper, 4, 2, workspace()) && *memory(workspace()) == handle);
    550 
    551 #ifndef _WIN32
    552     /* The compositor clips the visible native client; verify moving/filtering live bounds. */
    553     SDL_Window *native = linux_window_native(handle);
    554     SDL_ShowWindow(native);
    555     SDL_SetWindowPosition(native, 100, 120);
    556     SDL_SetWindowSize(native, 320, 200);
    557     int x, y;
    558     SDL_GetWindowPosition(native, &x, &y);
    559     *memory(size) = 64;
    560     assert(!invoke(clipper, 3, 4, 0u, output, size));
    561     assert(*memory(output + 8) == 1 && *memory(output + 12) == 16);
    562     assert(*memory(output + 16) == (uint32_t)x && *memory(output + 20) == (uint32_t)y);
    563     assert(*memory(output + 24) == (uint32_t)(x + 320) && *memory(output + 28) == (uint32_t)(y + 200));
    564     assert(!memcmp(memory(output + 16), memory(output + 32), 16));
    565     uint32_t filter = workspace() + 64;
    566     *memory(filter) = x + 10; *memory(filter + 4) = y + 20;
    567     *memory(filter + 8) = x + 30; *memory(filter + 12) = y + 40;
    568     assert(!invoke(clipper, 3, 4, filter, output, size));
    569     assert(!memcmp(memory(output + 16), memory(filter), 16));
    570 
    571     uint32_t primary = create_surface(draw, 0x200), source = create_surface(draw, 0x840);
    572     uint32_t desc = workspace() + 128, rectangle = workspace() + 80;
    573     assert(!invoke(primary, 28, 2, clipper));
    574     assert(!invoke(source, 25, 5, 0u, desc, 0x21u, 0u));
    575     uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    576     for (unsigned row = 0; row < 16; ++row) for (unsigned column = 0; column < 32; ++column)
    577         *memory(pixels + row * pitch + column * 4) = (row << 16) | (column << 8) | 0x55;
    578     assert(!invoke(source, 32, 2, 0u));
    579     *memory(desc) = 124;
    580     assert(!invoke(primary, 25, 5, 0u, desc, 0x21u, 0u));
    581     memset(memory(*memory(desc + 36)), 0, *memory(desc + 16) * *memory(desc + 8));
    582     assert(!invoke(primary, 32, 2, 0u));
    583     int32_t destination[] = {x - 8, y - 4, x + 24, y + 12};
    584     memcpy(memory(rectangle), destination, 16);
    585 
    586     assert(!invoke(primary, 5, 6, rectangle, source, 0u, 0x200u, 0u));
    587     const int32_t outside[] = {INT32_MIN, 0, INT32_MIN + 32, 16};
    588     memcpy(memory(rectangle), outside, sizeof(outside));
    589     assert(!invoke(primary, 5, 6, rectangle, source, 0u, 0x200u, 0u));
    590     *memory(desc) = 124;
    591     assert(!invoke(primary, 25, 5, 0u, desc, 0x11u, 0u));
    592     pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    593 
    594     for (int row = -4; row < 16; ++row) for (int column = -8; column < 32; ++column) {
    595         uint32_t expected = 0;
    596         if (row >= 0 && row < 12 && column >= 0 && column < 24)
    597             expected = ((row + 4) << 16) | ((column + 8) << 8) | 0x55;
    598         assert((*memory(pixels + (y + row) * pitch + (x + column) * 4) & 0xffffff) == expected);
    599     }
    600     assert(!invoke(primary, 32, 2, 0u));
    601     SDL_HideWindow(native);
    602     destination[0] = x; destination[1] = y; destination[2] = x + 32; destination[3] = y + 16;
    603     memcpy(memory(rectangle), destination, 16);
    604     assert(!invoke(primary, 5, 6, rectangle, source, 0u, 0x200u, 0u));
    605     *memory(desc) = 124;
    606     assert(!invoke(primary, 25, 5, 0u, desc, 0x11u, 0u));
    607     pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    608     assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == 0x040855);
    609     assert(!invoke(primary, 32, 2, 0u));
    610     assert(!invoke(primary, 2, 1) && !invoke(source, 2, 1));
    611     assert(window_call("DestroyWindow", 1, &handle));
    612     uint32_t unregister[] = {atom, 1};
    613     assert(window_call("UnregisterClassA", 2, unregister));
    614 #else
    615     assert(DestroyWindow(native));
    616 #endif
    617     *memory(size) = 0;
    618     assert(invoke(clipper, 3, 4, 0u, 0u, size) == 0x80004005u);
    619     assert(!invoke(clipper, 8, 3, 0u, 0u));
    620     assert(invoke(clipper, 3, 4, 0u, 0u, size) == 0x887600cdu);
    621     assert(!invoke(clipper, 2, 1) && !invoke(draw, 2, 1));
    622 }
    623 
    624 static void surface_blits_copy_pixels(void)
    625 {
    626     uint32_t draw = create_draw(), desc = workspace() + 128;
    627     uint32_t source_rect = workspace() + 64, target_rect = workspace() + 80;
    628     const int32_t from[][4] = {{0, 0, 32, 16}, {2, 3, 10, 11}, {2, 3, 10, 7}};
    629     const int32_t to[][4] = {{0, 0, 32, 16}, {5, 4, 13, 12}, {24, 12, 32, 16}};
    630     for (unsigned storage = 0; storage < 4; ++storage) {
    631         uint32_t source = create_surface(draw, storage & 1 ? 0x840 : 0x2040);
    632         uint32_t target = create_surface(draw, storage & 2 ? 0x840 : 0x2040);
    633         assert(!invoke(source, 25, 5, 0u, desc, 0x21u, 0u));
    634         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    635         for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x)
    636             *memory(pixels + y * pitch + x * 4) = (y << 16) | (x << 8) | (x ^ y);
    637         assert(!invoke(source, 32, 2, 0u));
    638         for (unsigned scenario = 0; scenario < 3; ++scenario) {
    639             *memory(desc) = 124;
    640             assert(!invoke(target, 25, 5, 0u, desc, 0x21u, 0u));
    641             pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    642             for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x)
    643                 *memory(pixels + y * pitch + x * 4) = 0xaabbcc;
    644             assert(!invoke(target, 32, 2, 0u));
    645             memcpy(memory(source_rect), from[scenario], 16);
    646             memcpy(memory(target_rect), to[scenario], 16);
    647 
    648             assert(!invoke(target, 5, 6, scenario ? target_rect : 0u, source,
    649                            scenario ? source_rect : 0u, 0x200u, 0u));
    650             *memory(desc) = 124;
    651             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    652             pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    653 
    654             for (int y = 0; y < 16; ++y) for (int x = 0; x < 32; ++x) {
    655                 uint32_t expected = 0xaabbcc;
    656                 if (x >= to[scenario][0] && x < to[scenario][2] && y >= to[scenario][1] && y < to[scenario][3]) {
    657                     unsigned sx = from[scenario][0] + x - to[scenario][0];
    658                     unsigned sy = from[scenario][1] + y - to[scenario][1];
    659                     expected = (sy << 16) | (sx << 8) | (sx ^ sy);
    660                 }
    661                 if ((*memory(pixels + y * pitch + x * 4) & 0xffffff) != expected)
    662                     fprintf(stderr, "blit storage=%u scenario=%u pixel=%d,%d actual=%06x expected=%06x\n",
    663                             storage, scenario, x, y, *memory(pixels + y * pitch + x * 4) & 0xffffff, expected);
    664                 assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == expected);
    665             }
    666             assert(!invoke(target, 32, 2, 0u));
    667         }
    668         assert(!invoke(source, 2, 1) && !invoke(target, 2, 1));
    669     }
    670     assert(!invoke(draw, 2, 1));
    671 }
    672 
    673 static void private_data_is_copied_and_owned_by_each_surface(void)
    674 {
    675     uint32_t draw = create_draw(), first = create_surface(draw, 0x40), second = create_surface(draw, 0x40);
    676     uint32_t key = workspace() + 32, other_key = workspace() + 48;
    677     uint32_t input = workspace() + 256, output = workspace() + 512, size = workspace() + 80;
    678     const uint32_t guid[] = {0x684b06f3, 0x11d2ebae, 0xc000f481, 0x99f2684f};
    679     const uint32_t payload[] = {0x60001234, 2, 3, 4, 5};
    680     memcpy(memory(key), guid, 16); memcpy(memory(other_key), guid, 16);
    681     *memory(other_key + 12) = 0x12345678;
    682     memcpy(memory(input), payload, 20);
    683     memset(memory(output), 0xa5, 32);
    684     *memory(size) = 32;
    685 
    686     assert(invoke(first, 41, 4, key, output, size) == 0x887600ffu && *memory(size) == 32);
    687     assert(invoke(first, 42, 2, key) == 0x887600ffu);
    688     assert(!invoke(first, 40, 5, key, input, 20u, 0u));
    689     *memory(input) = 99;
    690     assert(!invoke(first, 40, 5, other_key, input, 3u, 0u));
    691     assert(!invoke(second, 40, 5, key, input, 8u, 0u));
    692 
    693     const uint32_t capacities[] = {0, 4, 20, 32};
    694     for (unsigned i = 0; i < 4; ++i) {
    695         *memory(size) = capacities[i];
    696         memset(memory(output), 0xa5, 32);
    697         uint32_t result = invoke(first, 41, 4, key, output, size);
    698         assert(result == (capacities[i] < 20 ? 0x887602b2u : 0));
    699         assert(*memory(size) == 20);
    700         if (capacities[i] >= 20) assert(!memcmp(memory(output), payload, 20));
    701         else for (unsigned j = 0; j < 5; ++j) assert(*memory(output + j * 4) == 0xa5a5a5a5);
    702         assert(*memory(output + 20) == 0xa5a5a5a5 && *memory(output + 28) == 0xa5a5a5a5);
    703     }
    704     *memory(size) = 0;
    705     assert(invoke(first, 41, 4, key, 0u, size) == 0x887602b2u && *memory(size) == 20);
    706     assert(!invoke(first, 40, 5, key, input, 8u, 0u));
    707     assert(invoke(first, 40, 5, key, input, 0u, 0u) == 0x80070057u);
    708     *memory(size) = 32;
    709     assert(!invoke(first, 41, 4, key, output, size));
    710     assert(*memory(size) == 8 && *memory(output) == 99 && *memory(output + 4) == 2);
    711     assert(!invoke(first, 42, 2, key));
    712     assert(invoke(first, 42, 2, key) == 0x887600ffu);
    713     assert(!invoke(second, 41, 4, key, output, size) && *memory(output) == 99);
    714     memset(memory(output), 0xa5, 32);
    715     assert(!invoke(first, 41, 4, other_key, output, size));
    716     assert(*memory(size) == 3 && *memory(output) == 0xa5000063);
    717     assert(!invoke(first, 2, 1) && !invoke(second, 2, 1) && !invoke(draw, 2, 1));
    718 }
    719 
    720 static void palette_entries_and_lifetime(void)
    721 {
    722     const uint32_t caps[] = {0x100, 0x200, 1, 4, 0x44, 0x404, 0x108, 0x102};
    723     const unsigned counts[] = {2, 4, 16, 256, 256, 256, 2, 2};
    724     uint32_t draw = create_draw(), input = workspace() + 1024, output = workspace() + 2048;
    725     uint32_t iid = workspace() + 16;
    726     const uint32_t palette_iid[] = {0x6c14db84, 0x11cea733, 0x200021a5, 0x60e50baf};
    727     memcpy(memory(iid), palette_iid, 16);
    728 
    729     for (unsigned c = 0; c < sizeof(caps) / sizeof(caps[0]); ++c) {
    730         for (unsigned i = 0; i < 256; ++i) memory(input)[i] = 0xa5030201u + i;
    731         assert(!invoke(draw, 5, 5, caps[c], input, workspace(), 0u));
    732         uint32_t palette = *memory(workspace());
    733         assert(!invoke(palette, 3, 2, workspace()));
    734         assert(*memory(workspace()) == (caps[c] & ~(0x400u | 8u)));
    735         assert(!invoke(palette, 0, 3, iid, workspace()) && *memory(workspace()) == palette);
    736         assert(invoke(palette, 2, 1) == 1);
    737         assert(invoke(draw, 1, 1) == 3 && invoke(draw, 2, 1) == 2);
    738         memset(memory(output), 0xcc, 1028);
    739         assert(!invoke(palette, 4, 5, 0u, 0u, counts[c], output));
    740         unsigned bytes = counts[c] * (caps[c] & 2 ? 1 : 4);
    741         if (counts[c] == 256 && !(caps[c] & 0x40)) {
    742             memory(input)[0] &= 0xff000000;
    743             memory(input)[255] |= 0xffffff;
    744         }
    745 
    746         assert(!memcmp(memory(output), memory(input), bytes));
    747         assert(*((unsigned char *)memory(output) + bytes) == 0xcc);
    748         *memory(input) = 0xa5191817;
    749         assert(!invoke(palette, 6, 5, 0u, 1u, 1u, input));
    750         assert(!invoke(palette, 4, 5, 0u, 1u, 1u, output));
    751         assert(!memcmp(memory(output), memory(input), caps[c] & 2 ? 1 : 4));
    752         assert(invoke(palette, 4, 5, 0u, 0u, 0u, output) == 0x80070057u);
    753         assert(invoke(palette, 6, 5, 0u, counts[c], 1u, input) == 0x80070057u);
    754         assert(invoke(palette, 4, 5, 0u, 0u, 0xffffffffu, output) == 0x80070057u);
    755         assert(invoke(palette, 4, 5, 1u, 0u, 1u, output) == 0x80070057u);
    756         assert(invoke(palette, 5, 4, draw, caps[c], input) == 0x88760005u);
    757         assert(!invoke(palette, 2, 1));
    758     }
    759     *memory(workspace()) = 0xa5a5a5a5;
    760     assert(invoke(draw, 5, 5, 0x104u, input, workspace(), 0u) == 0x80070057u);
    761     assert(!*memory(workspace()));
    762     assert(!invoke(draw, 2, 1));
    763 }
    764 
    765 static void absent_mip_attachment_returns_not_found(void)
    766 {
    767     uint32_t draw = create_draw(), surface = create_surface(draw, 0x2040);
    768     uint32_t caps = workspace() + 64, output = workspace();
    769     memset(memory(caps), 0, 16);
    770     *memory(caps) = 0x401000;
    771     *memory(output) = 0xa5a5a5a5;
    772 
    773     uint32_t result = invoke(surface, 12, 3, caps, output);
    774 
    775     assert(result == 0x887600ffu && !*memory(output));
    776     assert(invoke(surface, 1, 1) == 2 && invoke(surface, 2, 1) == 1);
    777     assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1));
    778 }
    779 
    780 static void mip_chain_levels_and_pixels(int batch)
    781 {
    782     uint32_t draw = create_draw(), desc = workspace() + 128, caps = workspace() + 64;
    783     const unsigned widths[] = {32, 16, 8, 4, 2, 1};
    784     const unsigned heights[] = {16, 8, 4, 2, 1, 1};
    785     const unsigned pitches[] = {128, 64, 32, 16, 8, 8};
    786     const uint32_t format[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
    787     memset(memory(desc), 0, 124);
    788     *memory(desc) = 124; *memory(desc + 4) = 0x101007;
    789     *memory(desc + 8) = 16; *memory(desc + 12) = 32;
    790     memcpy(memory(desc + 72), format, 32);
    791     *memory(desc + 104) = 0x401008; *memory(desc + 108) = 0x10;
    792     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    793     uint32_t levels[6] = {*memory(workspace())};
    794     memset(memory(caps), 0, 16);
    795     *memory(caps) = 0x401000;
    796 #ifndef _WIN32
    797     if (batch) assert(linux_graphics_begin_texture_update(levels[0]) == levels[0]);
    798 #endif
    799 
    800     for (unsigned i = 0; i < 6; ++i) {
    801         *memory(desc) = 124;
    802         assert(!invoke(levels[i], 25, 5, 0u, desc, 0x21u, 0u));
    803         uint32_t pixels = *memory(desc + 36);
    804         for (unsigned y = 0; y < heights[i]; ++y)
    805             for (unsigned x = 0; x < widths[i]; ++x)
    806                 *memory(pixels + y * pitches[i] + x * 4) = 0xff102030 + i + x + y;
    807         assert(!invoke(levels[i], 32, 2, 0u));
    808         if (i < 5) {
    809             assert(!invoke(levels[i], 12, 3, caps, workspace()));
    810             levels[i + 1] = *memory(workspace());
    811         }
    812     }
    813 
    814 #ifndef _WIN32
    815     if (batch) linux_graphics_end_texture_update(levels[0]);
    816 #endif
    817     assert(invoke(draw, 1, 1) == 4 && invoke(draw, 2, 1) == 3);
    818     for (unsigned i = 0; i < 6; ++i) {
    819         assert(!invoke(levels[i], 25, 5, 0u, desc, 0x11u, 0u));
    820         assert(*memory(desc + 4) == 0x12100f && *memory(desc + 24) == 6 - i);
    821         assert(*memory(desc + 8) == heights[i] && *memory(desc + 12) == widths[i]);
    822         assert(*memory(desc + 16) == pitches[i] && *memory(desc + 104) == 0x401808);
    823         assert(*memory(desc + 108) == (i ? 0x10010u : 0x10u));
    824         uint32_t pixels = *memory(desc + 36);
    825         for (unsigned y = 0; y < heights[i]; ++y)
    826             for (unsigned x = 0; x < widths[i]; ++x)
    827                 assert(*memory(pixels + y * pitches[i] + x * 4) == 0xff102030 + i + x + y);
    828         assert(!invoke(levels[i], 32, 2, 0u));
    829     }
    830     assert(invoke(levels[5], 12, 3, caps, workspace()) == 0x887600ffu && !*memory(workspace()));
    831     for (unsigned i = 1; i < 6; ++i) assert(invoke(levels[i], 2, 1) == 1);
    832     assert(!invoke(levels[0], 2, 1));
    833     assert(!invoke(draw, 2, 1));
    834 }
    835 
    836 static void partial_mip_chain_preserves_padded_rows(void)
    837 {
    838     uint32_t draw = create_draw(), desc = workspace() + 128, caps = workspace() + 64;
    839     const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0};
    840     memset(memory(desc), 0, 124);
    841     *memory(desc) = 124; *memory(desc + 4) = 0x121007;
    842     *memory(desc + 8) = 4; *memory(desc + 12) = 1; *memory(desc + 24) = 2;
    843     memcpy(memory(desc + 72), format, 32);
    844     *memory(desc + 104) = 0x401008; *memory(desc + 108) = 0x10;
    845     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    846     uint32_t top = *memory(workspace());
    847     memset(memory(caps), 0, 16); *memory(caps) = 0x401000;
    848     assert(!invoke(top, 12, 3, caps, workspace()));
    849     uint32_t child = *memory(workspace());
    850     const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
    851     memcpy(memory(workspace() + 16), unknown, 16);
    852     assert(!invoke(child, 0, 3, workspace() + 16, workspace()));
    853     /* The parent also owns the retained older-interface alias of this mip. */
    854 
    855     assert(!invoke(child, 25, 5, 0u, desc, 0x21u, 0u));
    856     uint32_t pixels = *memory(desc + 36);
    857     *memory(pixels) = 0xff123456; *memory(pixels + 8) = 0xff789abc;
    858     assert(!invoke(child, 32, 2, 0u));
    859     assert(!invoke(child, 25, 5, 0u, desc, 0x11u, 0u));
    860     pixels = *memory(desc + 36);
    861 
    862     assert(*memory(desc + 8) == 2 && *memory(desc + 12) == 1);
    863     assert(*memory(desc + 16) == 8 && *memory(desc + 24) == 1);
    864     assert(*memory(pixels) == 0xff123456 && *memory(pixels + 8) == 0xff789abc);
    865     assert(!invoke(child, 32, 2, 0u));
    866     assert(invoke(child, 12, 3, caps, workspace()) == 0x887600ffu && !*memory(workspace()));
    867     assert(invoke(child, 2, 1) == 1);
    868     assert(!invoke(top, 2, 1) && !invoke(draw, 2, 1));
    869 }
    870 
    871 static void surface_identity_collisions_and_alias_lifetimes(void)
    872 {
    873     uint32_t draw = create_draw(), surfaces[4097], aliases[4097];
    874     uint32_t iid = workspace() + 16, desc = workspace() + 128;
    875     const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
    876     const uint32_t surface7[] = {0x06675a80, 0x11d23b9b, 0x60002fb9, 0x5bea9797};
    877     memcpy(memory(iid), unknown, 16);
    878     for (unsigned i = 0; i < 4097; ++i) {
    879         surfaces[i] = create_surface(draw, 0x840);
    880         assert(!invoke(surfaces[i], 0, 3, iid, workspace()));
    881         aliases[i] = *memory(workspace());
    882     }
    883 
    884     for (unsigned i = 0; i < 4097; ++i) {
    885         if (i % 2) {
    886             assert(!invoke(aliases[i], 2, 1));
    887             aliases[i] = 0;
    888         } else {
    889             assert(!invoke(surfaces[i], 2, 1));
    890             surfaces[i] = 0;
    891         }
    892     }
    893     memcpy(memory(iid), surface7, 16);
    894     for (unsigned i = 0; i < 4097; i += 2) {
    895         assert(!invoke(aliases[i], 0, 3, iid, workspace()));
    896         surfaces[i] = *memory(workspace());
    897     }
    898 
    899     for (unsigned i = 0; i < 4097; ++i) {
    900         *memory(desc) = 124;
    901         assert(!invoke(surfaces[i], 22, 2, desc));
    902         assert(*memory(desc + 8) == 16 && *memory(desc + 12) == 32);
    903         if (aliases[i]) assert(!invoke(aliases[i], 2, 1));
    904         assert(!invoke(surfaces[i], 2, 1));
    905     }
    906     assert(!invoke(draw, 2, 1));
    907 }
    908 
    909 int main(void)
    910 {
    911 #ifndef _WIN32
    912     assert(guest_map("surface test", 0x50000000, 0x10000));
    913     g_esp = 0x5000f000;
    914     g_fs_base = 0x50000000;
    915 #endif
    916     surface_lifetime_and_description();
    917     surface_pixels_round_trip(0x2040);
    918     surface_pixels_round_trip(0x840);
    919 #ifndef _WIN32
    920     batched_texture_writes_preserve_reads_and_blits(0);
    921     batched_texture_writes_preserve_reads_and_blits(0x10);
    922     uploaded_surface_releases_temporary_pixels(0);
    923     uploaded_surface_releases_temporary_pixels(0x10);
    924     rgb565_textures_preserve_pixels_and_rectangle_locks(0);
    925     rgb565_textures_preserve_pixels_and_rectangle_locks(0x10);
    926     rgb565_blits_preserve_the_native_skin_merge_key();
    927 #endif
    928     rectangle_locks_preserve_surrounding_pixels(0x2040);
    929     rectangle_locks_preserve_surrounding_pixels(0x840);
    930     clipper_attachment_retains_objects();
    931     depth_surface_attachment_and_lifetime();
    932     window_clipper_regions();
    933     private_data_is_copied_and_owned_by_each_surface();
    934     surface_blits_copy_pixels();
    935     palette_entries_and_lifetime();
    936     absent_mip_attachment_returns_not_found();
    937     mip_chain_levels_and_pixels(0);
    938 #ifndef _WIN32
    939     mip_chain_levels_and_pixels(1);
    940 #endif
    941     partial_mip_chain_preserves_padded_rows();
    942     surface_identity_collisions_and_alias_lifetimes();
    943     puts("surfaces: descriptions, COM ownership, clipper regions, palettes, depth attachments, private data, rectangle locks, blits and pixel round trips passed");
    944     return 0;
    945 }