aorecomp

Anarchy Online classic in the browser

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


client/tests/devices.c (113944B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <stdarg.h>
      5 #include <string.h>
      6 #ifdef _WIN32
      7 #include <windows.h>
      8 #include <d3d.h>
      9 _Static_assert(sizeof(D3DVIEWPORT7) == 24, "guest viewport size");
     10 static uint32_t scratch[1024];
     11 #else
     12 #include "linux_graphics.h"
     13 #include "guest_memory.h"
     14 static void format_callback(void);
     15 recomp_func_t recomp_lookup(uint32_t address) { assert(address == 0x11110000u); return format_callback; }
     16 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     17 #endif
     18 
     19 static uint32_t workspace(void)
     20 {
     21 #ifdef _WIN32
     22     return (uintptr_t)scratch;
     23 #else
     24     return 0x50001000u;
     25 #endif
     26 }
     27 
     28 static uint32_t *memory(uint32_t address)
     29 {
     30 #ifdef _WIN32
     31     return (void *)(uintptr_t)address;
     32 #else
     33     return (void *)ADDR(address);
     34 #endif
     35 }
     36 
     37 static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...)
     38 {
     39     uint32_t args[8] = {object};
     40     va_list list;
     41     va_start(list, count);
     42     for (unsigned i = 1; i < count; ++i) args[i] = va_arg(list, uint32_t);
     43     va_end(list);
     44     uint32_t address = memory(*memory(object))[slot];
     45 #ifdef _WIN32
     46     if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))(uintptr_t)address)(object);
     47     if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))(uintptr_t)address)(object, args[1]);
     48     if (count == 3) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2]);
     49     if (count == 4) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3]);
     50     if (count == 5) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4]);
     51     if (count == 6) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5]);
     52     if (count == 7) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5], args[6]);
     53     assert(count == 8);
     54     return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
     55 #else
     56     uint32_t stack = g_esp, caller = g_cur_func;
     57     g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab;
     58     for (unsigned i = count; i; --i) PUSH32(g_esp, args[i - 1]);
     59     PUSH32(g_esp, RECOMP_RETADDR);
     60     recomp_func_t function = linux_graphics_virtual(address);
     61     assert(function);
     62     function();
     63     assert(g_esp == stack && g_cur_func == caller);
     64     assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab);
     65     return g_eax;
     66 #endif
     67 }
     68 
     69 static uint32_t query(uint32_t object, const uint32_t *iid)
     70 {
     71     memcpy(memory(workspace() + 16), iid, 16);
     72     assert(!invoke(object, 0, 3, workspace() + 16, workspace()));
     73     return *memory(workspace());
     74 }
     75 
     76 static unsigned format_count, format_mask;
     77 static uint32_t inspect_format(uint32_t record, uint32_t data)
     78 {
     79     assert(*memory(record) == 32 && (data == 0 || data == 1));
     80     if ((*memory(record + 4) & 0x40) && *memory(record + 12) == 32 &&
     81         *memory(record + 16) == 0xff0000 && *memory(record + 20) == 0xff00 && *memory(record + 24) == 0xff) {
     82         if (*memory(record + 28) == 0xff000000) format_mask |= 2;
     83         else if (!*memory(record + 28)) format_mask |= 1;
     84     }
     85     if (*memory(record + 4) == 0x40 && *memory(record + 12) == 16 &&
     86         *memory(record + 16) == 0xf800 && *memory(record + 20) == 0x7e0 &&
     87         *memory(record + 24) == 0x1f && !*memory(record + 28)) format_mask |= 4;
     88     ++format_count;
     89     return data ? 0 : 1;
     90 }
     91 
     92 #ifdef _WIN32
     93 static HRESULT WINAPI format_callback(DDPIXELFORMAT *format, void *data)
     94 {
     95     return inspect_format((uintptr_t)format, (uintptr_t)data);
     96 }
     97 #else
     98 static void format_callback(void)
     99 {
    100     g_eax = inspect_format(MEM32(g_esp + 4), MEM32(g_esp + 8));
    101     g_esp += 12;
    102 }
    103 #endif
    104 
    105 static void rgb565_managed_texture_survives_eviction(uint32_t draw)
    106 {
    107     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    108     uint32_t d3d = query(draw, iid), desc = workspace() + 128;
    109     const uint32_t format[] = {32, 0x40, 0, 16, 0xf800, 0x7e0, 0x1f, 0};
    110     memset(memory(desc), 0, 124);
    111     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
    112     *memory(desc + 8) = 2; *memory(desc + 12) = 2;
    113     memcpy(memory(desc + 72), format, sizeof(format));
    114     *memory(desc + 104) = 0x1000; *memory(desc + 108) = 0x10;
    115     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    116     uint32_t texture = *memory(workspace());
    117     assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
    118     const uint16_t colors[] = {0xf800, 0x7e0, 0x1f, 0x1234};
    119     memcpy(memory(*memory(desc + 36)), colors, sizeof(colors));
    120     assert(!invoke(texture, 32, 2, 0u));
    121 
    122     assert(!invoke(d3d, 7, 1) && !invoke(d3d, 7, 1));
    123     assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    124 
    125     assert(*memory(desc + 16) == 4);
    126     assert(!memcmp(memory(*memory(desc + 36)), colors, sizeof(colors)));
    127     assert(!invoke(texture, 32, 2, 0u));
    128     assert(!invoke(texture, 2, 1));
    129     invoke(d3d, 2, 1);
    130 }
    131 
    132 static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
    133 {
    134     uint32_t desc = workspace() + 128, input = workspace() + 512;
    135     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    136     uint32_t d3d = query(draw, iid);
    137     assert(!invoke(d3d, 7, 1));
    138 #ifdef _WIN32
    139     uint32_t callback = (uintptr_t)format_callback;
    140 #else
    141     uint32_t callback = 0x11110000u;
    142 #endif
    143     assert(!invoke(device, 4, 3, callback, 0u) && format_mask == 7);
    144     format_count = 0;
    145     assert(!invoke(device, 4, 3, callback, 1u) && format_count == 1);
    146     const uint32_t states[] = {1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 16, 17, 18, 21};
    147     const uint32_t defaults[] = {4, 2, 1, 2, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1};
    148     for (unsigned i = 0; i < sizeof(states) / sizeof(states[0]); ++i) {
    149         assert(!invoke(device, 36, 4, 0u, states[i], workspace()));
    150         assert(*memory(workspace()) == defaults[i]);
    151     }
    152     assert(!invoke(device, 34, 3, 0u, workspace()) && !*memory(workspace()));
    153     assert(!invoke(device, 21, 3, 19u, workspace()) && *memory(workspace()) == 2);
    154     assert(!invoke(device, 21, 3, 20u, workspace()) && *memory(workspace()) == 1);
    155     assert(!invoke(device, 21, 3, 27u, workspace()) && !*memory(workspace()));
    156     const uint32_t texels[] = {
    157         0xffff0000, 0x8000ff00, 0x400000ff, 0x00ffffff,
    158         0xff203040, 0x8090a0b0, 0x40ff00ff, 0x0000ffff,
    159         0xff804020, 0x80108030, 0x40204080, 0x00ffff00,
    160         0xff000000, 0x80777777, 0x40bbbbbb, 0x00336699
    161     };
    162 
    163     for (unsigned variant = 0; variant < 4; ++variant) {
    164         unsigned alpha = variant & 1, managed = variant >> 1;
    165         memset(memory(desc), 0, 124);
    166         *memory(desc) = 124; *memory(desc + 4) = managed ? 0x101007 : 0x1007;
    167         *memory(desc + 8) = 4; *memory(desc + 12) = 4; *memory(desc + 104) = 0x1000;
    168         *memory(desc + 108) = managed ? 0x10 : 0;
    169         const uint32_t format[] = {32, 0x40 | alpha, 0, 32, 0xff0000, 0xff00, 0xff, alpha ? 0xff000000 : 0};
    170         memcpy(memory(desc + 72), format, 32);
    171         assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    172         uint32_t texture = *memory(workspace());
    173         assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
    174         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    175         assert(!memcmp(memory(desc + 72), format, 32));
    176         assert(*memory(desc + 4) == (managed ? 0x10100f : 0x100f));
    177         assert(*memory(desc + 104) == (managed ? 0x1800 : 0x10005000));
    178         assert(*memory(desc + 108) == (managed ? 0x10 : 0));
    179         for (unsigned y = 0; y < 4; ++y) memcpy(memory(pixels + y * pitch), texels + y * 4, 16);
    180         assert(!invoke(texture, 32, 2, 0u));
    181         assert(!invoke(d3d, 7, 1) && !invoke(d3d, 7, 1));
    182         assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
    183         pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    184         for (unsigned y = 0; y < 4; ++y) assert(!memcmp(memory(pixels + y * pitch), texels + y * 4, 16));
    185         assert(!invoke(texture, 32, 2, 0u));
    186         assert(!invoke(device, 35, 3, 0u, texture));
    187         assert(!invoke(device, 35, 3, 0u, texture));
    188         assert(!invoke(device, 34, 3, 0u, workspace()) && *memory(workspace()) == texture);
    189         assert(invoke(texture, 2, 1) == 2);
    190         assert(invoke(texture, 2, 1) == 1);
    191 
    192         const uint32_t blends[][3] = {{0, 2, 1}, {1, 5, 6}, {1, 6, 5}, {1, 1, 2}, {1, 2, 1}, {1, 2, 3}, {1, 3, 1}};
    193         for (unsigned legacy = 0; legacy <= 5; ++legacy)
    194         for (unsigned scenario = 0; scenario < (legacy < 2 ? 56 : 4); ++scenario) {
    195             uint32_t filter = scenario & 1 ? 2 : 1, address = scenario & 2 ? 3 : 1;
    196             float span = scenario & 4 ? 16.25f : 1.75f;
    197             struct vertex { float x, y, z, rhw; uint32_t color; float u, v; } vertices[] = {
    198                 {0, 0, .25f, 1, 0x4080c0ff, -.25f, -.375f},
    199                 {32, 0, .25f, .5f, 0x8080c0ff, span, -.375f},
    200                 {0, 32, .25f, 2, 0xc080c0ff, -.25f, span}
    201             };
    202             memcpy(memory(input), vertices, sizeof(vertices));
    203             const float matrices[][16] = {
    204                 {2,0,0,0, 0,3,0,0, 0,0,1,0, .3f,.7f,0,1},
    205                 {1,0,0,0, 0,1,0,0, .125f,.375f,1,0, 3,7,0,1},
    206                 {0,1,0,0, -1,0,0,0, 1,0,1,0, 3,7,0,1},
    207                 {1,.5f,0,0, .25f,1,0,0, .125f,-.25f,1,0, 3,7,0,1},
    208             };
    209             memcpy(memory(workspace() + 1024), matrices[scenario & 3], 64);
    210             assert(!invoke(device, 11, 3, 16u, workspace() + 1024));
    211             assert(!invoke(device, 37, 4, 0u, 24u, legacy == 3 || legacy == 5 ? 2u : 0u));
    212             assert(!invoke(device, 36, 4, 0u, 24u, workspace()) && *memory(workspace()) == (legacy == 3 || legacy == 5 ? 2u : 0u));
    213             assert(!invoke(device, 20, 3, 137u, legacy < 4 ? 1u : 0u));
    214             assert(!invoke(device, 37, 4, 0u, 11u, legacy == 4 && (scenario & 1) ? 1u : 0u));
    215             if (legacy >= 4) for (unsigned i = 0; i < 3; ++i) {
    216                 float xyz[] = {vertices[i].x / 16 - 1, 1 - vertices[i].y / 16, .25f};
    217                 memcpy(memory(input + i * 24), xyz, 12);
    218                 *memory(input + i * 24 + 12) = vertices[i].color;
    219                 memcpy(memory(input + i * 24 + 16), &vertices[i].u, 8);
    220             }
    221             assert(!invoke(device, 37, 4, 0u, 16u, filter));
    222             assert(!invoke(device, 37, 4, 0u, 17u, filter));
    223             assert(!invoke(device, 37, 4, 0u, 18u, (scenario & 4) ? 2u : 1u));
    224             assert(!invoke(device, 37, 4, 0u, 1u, (scenario & 3) == 3 ? 2u : (scenario & 3) == 0 ? 7u : 4u));
    225             assert(!invoke(device, 37, 4, 0u, 3u, scenario & 4 ? 0u : 1u));
    226             assert(!invoke(device, 36, 4, 0u, 3u, workspace()) && *memory(workspace()) == (scenario & 4 ? 0u : 1u));
    227             assert(!invoke(device, 37, 4, 0u, 4u, scenario & 2 ? 4u : scenario & 1 ? 3u : 2u));
    228             assert(!invoke(device, 37, 4, 0u, 6u, scenario & 4 ? 0u : 1u));
    229             assert(!invoke(device, 36, 4, 0u, 6u, workspace()) && *memory(workspace()) == (scenario & 4 ? 0u : 1u));
    230             assert(!invoke(device, 20, 3, 27u, blends[scenario / 8][0]));
    231             assert(!invoke(device, 20, 3, 19u, blends[scenario / 8][1]));
    232             assert(!invoke(device, 20, 3, 20u, blends[scenario / 8][2]));
    233             assert(!invoke(device, 21, 3, 19u, workspace()) && *memory(workspace()) == blends[scenario / 8][1]);
    234             assert(!invoke(device, 21, 3, 20u, workspace()) && *memory(workspace()) == blends[scenario / 8][2]);
    235             if (legacy < 2) {
    236                 assert(!invoke(device, 37, 4, 0u, 12u, legacy ? address : 1u));
    237                 assert(!invoke(device, 37, 4, 0u, 13u, legacy ? 1u : address));
    238                 assert(!invoke(device, 37, 4, 0u, 14u, legacy ? 1u : address));
    239                 assert(!invoke(device, 36, 4, 0u, 12u, workspace()) && *memory(workspace()) == (legacy ? address : 1));
    240                 assert(!invoke(device, 36, 4, 0u, 13u, workspace()) && *memory(workspace()) == (legacy ? 1 : address));
    241                 assert(!invoke(device, 36, 4, 0u, 14u, workspace()) && *memory(workspace()) == (legacy ? 1 : address));
    242             } else if (legacy == 2) {
    243                 assert(!invoke(device, 37, 4, 0u, 12u, 3u));
    244                 assert(!invoke(device, 37, 4, 0u, 13u, 3u));
    245                 assert(!invoke(device, 37, 4, 0u, 14u, 3u));
    246                 assert(!invoke(device, 37, 4, 0u, 13u, 1u));
    247                 assert(!invoke(device, 37, 4, 0u, 14u, 1u));
    248                 assert(!invoke(device, 36, 4, 0u, 12u, workspace()) && *memory(workspace()) == 3);
    249             } else {
    250                 assert(!invoke(device, 37, 4, 0u, 12u, 1u));
    251                 assert(!invoke(device, 37, 4, 0u, 13u, 3u));
    252                 assert(!invoke(device, 37, 4, 0u, 14u, 3u));
    253                 assert(!invoke(device, 37, 4, 0u, 12u, 1u));
    254                 assert(!invoke(device, 36, 4, 0u, 13u, workspace()) && *memory(workspace()) == 3);
    255                 assert(!invoke(device, 36, 4, 0u, 14u, workspace()) && *memory(workspace()) == 3);
    256             }
    257             assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    258 
    259             assert(!invoke(d3d, 7, 1));
    260             assert(!invoke(device, 5, 1));
    261             assert(!invoke(device, 25, 6, 4u, legacy >= 4 ? 0x142u : 0x144u, input, 3u, 0u));
    262             assert(!invoke(device, 6, 1));
    263             *memory(desc) = 124;
    264             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    265             pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    266 
    267             unsigned char rgb[32 * 32 * 3];
    268             for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    269                 uint32_t pixel = *memory(pixels + y * pitch + x * 4);
    270                 rgb[(y * 32 + x) * 3] = pixel >> 16;
    271                 rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
    272                 rgb[(y * 32 + x) * 3 + 2] = pixel;
    273             }
    274             assert(!invoke(target, 32, 2, 0u));
    275             assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
    276         }
    277         const uint32_t formats[] = {0x244, 0x2c4, 0x212, 0x252, 0x242};
    278         for (unsigned format = 0; format < 5; ++format) for (unsigned coordinate = 0; coordinate < 2; ++coordinate)
    279         for (unsigned transform = 0; transform <= 2; transform += 2) {
    280             uint32_t fvf = formats[format];
    281             unsigned transformed = (fvf & 14) == 4;
    282             unsigned prefix = transformed ? 16 : 12;
    283             if (fvf & 16) prefix += 12;
    284             if (fvf & 64) prefix += 4;
    285             if (fvf & 128) prefix += 4;
    286             unsigned stride = prefix + 16;
    287             for (unsigned i = 0; i < 3; ++i) {
    288                 float position[] = {i == 1 ? 28 : 4, i == 2 ? 28 : 4, .25f, 1};
    289                 if (!transformed) { position[0] = position[0] / 16 - 1; position[1] = 1 - position[1] / 16; }
    290                 uint32_t vertex = input + i * stride;
    291                 memset(memory(vertex), 0, stride);
    292                 memcpy(memory(vertex), position, transformed ? 16 : 12);
    293                 if (fvf & 64) *memory(vertex + (transformed ? 16 : fvf & 16 ? 24 : 12)) = 0xff80c0ff;
    294                 const float uv[] = {i == 1 ? .875f : .125f, i == 2 ? .875f : .125f,
    295                                     i == 2 ? .375f : .625f, i == 1 ? .875f : .375f};
    296                 memcpy(memory(vertex + prefix), uv, 16);
    297             }
    298             assert(!invoke(device, 20, 3, 137u, 0u));
    299             assert(!invoke(device, 20, 3, 27u, 0u));
    300             assert(!invoke(device, 37, 4, 0u, 1u, 2u));
    301             assert(!invoke(device, 37, 4, 0u, 16u, coordinate + 1));
    302             assert(!invoke(device, 37, 4, 0u, 17u, coordinate + 1));
    303             assert(!invoke(device, 37, 4, 0u, 11u, coordinate));
    304             assert(!invoke(device, 36, 4, 0u, 11u, workspace()) && *memory(workspace()) == coordinate);
    305             assert(!invoke(device, 37, 4, 0u, 24u, transform));
    306             assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    307 
    308             assert(!invoke(device, 5, 1));
    309             assert(!invoke(device, 25, 6, 4u, fvf, input, 3u, 0u));
    310             assert(!invoke(device, 6, 1));
    311 
    312             *memory(desc) = 124;
    313             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    314             pixels = *memory(desc + 36); pitch = *memory(desc + 16);
    315             unsigned char rgb[32 * 32 * 3];
    316             for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    317                 uint32_t pixel = *memory(pixels + y * pitch + x * 4);
    318                 rgb[(y * 32 + x) * 3] = pixel >> 16;
    319                 rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
    320                 rgb[(y * 32 + x) * 3 + 2] = pixel;
    321             }
    322             assert(!invoke(target, 32, 2, 0u));
    323             assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
    324         }
    325         assert(!invoke(device, 37, 4, 0u, 11u, 0u));
    326         assert(!invoke(device, 35, 3, 0u, 0u));
    327         assert(!invoke(device, 34, 3, 0u, workspace()) && !*memory(workspace()));
    328     }
    329     assert(!invoke(device, 37, 4, 0u, 24u, 0u));
    330     invoke(d3d, 2, 1);
    331 }
    332 
    333 static void mip_levels_render_after_edits_and_eviction(uint32_t draw, uint32_t device, uint32_t target)
    334 {
    335     uint32_t desc = workspace() + 128, caps = workspace() + 64, input = workspace() + 512;
    336     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    337     uint32_t d3d = query(draw, iid);
    338     const uint32_t format[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
    339     const uint32_t colors[] = {0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff, 0xff00ffff};
    340     const unsigned sizes[] = {16, 8, 4, 2, 1};
    341     const float extents[] = {1.5f, 3, 6, 12, 24};
    342     memset(memory(desc), 0, 124);
    343     *memory(desc) = 124; *memory(desc + 4) = 0x101007;
    344     *memory(desc + 8) = 16; *memory(desc + 12) = 16;
    345     memcpy(memory(desc + 72), format, 32);
    346     *memory(desc + 104) = 0x401008; *memory(desc + 108) = 0x10;
    347     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
    348     uint32_t levels[5] = {*memory(workspace())};
    349     memset(memory(caps), 0, 16); *memory(caps) = 0x401000;
    350     for (unsigned i = 0; i < 5; ++i) {
    351         assert(!invoke(levels[i], 25, 5, 0u, desc, 0x21u, 0u));
    352         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    353         for (unsigned y = 0; y < sizes[i]; ++y) for (unsigned x = 0; x < sizes[i]; ++x)
    354             *memory(pixels + y * pitch + x * 4) = colors[i];
    355         assert(!invoke(levels[i], 32, 2, 0u));
    356         if (i < 4) {
    357             assert(!invoke(levels[i], 12, 3, caps, workspace()));
    358             levels[i + 1] = *memory(workspace());
    359         }
    360     }
    361     assert(!invoke(device, 35, 3, 0u, levels[0]));
    362     assert(!invoke(device, 20, 3, 27u, 0u));
    363     assert(!invoke(device, 20, 3, 22u, 1u));
    364     assert(!invoke(device, 37, 4, 0u, 1u, 2u));
    365     assert(!invoke(device, 37, 4, 0u, 2u, 2u));
    366     assert(!invoke(device, 37, 4, 0u, 12u, 1u));
    367     assert(!invoke(device, 37, 4, 0u, 16u, 1u));
    368     assert(!invoke(device, 37, 4, 0u, 17u, 1u));
    369     assert(!invoke(device, 37, 4, 0u, 18u, 2u));
    370 
    371     for (unsigned pass = 0; pass < 3; ++pass) {
    372         if (pass == 1) {
    373             assert(!invoke(levels[2], 25, 5, 0u, desc, 0x21u, 0u));
    374             uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    375             for (unsigned y = 0; y < 4; ++y) for (unsigned x = 0; x < 4; ++x)
    376                 *memory(pixels + y * pitch + x * 4) = 0xffffff00;
    377             assert(!invoke(levels[2], 32, 2, 0u));
    378             /* DirectDraw managed textures dirty their top level for GPU upload. */
    379             assert(!invoke(levels[0], 25, 5, 0u, desc, 0x21u, 0u));
    380             assert(!invoke(levels[0], 32, 2, 0u));
    381         }
    382         if (pass == 2) assert(!invoke(d3d, 7, 1));
    383         for (unsigned level = 0; level < 5; ++level) {
    384             struct { float xyzw[4]; uint32_t diffuse; float uv[2]; } vertices[] = {
    385                 {{4, 4, .25f, 1}, 0xffffffff, {0, 0}},
    386                 {{28, 4, .25f, 1}, 0xffffffff, {extents[level], 0}},
    387                 {{4, 28, .25f, 1}, 0xffffffff, {0, extents[level]}},
    388             };
    389             memcpy(memory(input), vertices, sizeof(vertices));
    390             assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    391             assert(!invoke(device, 5, 1));
    392             assert(!invoke(device, 25, 6, 4u, 0x144u, input, 3u, 0u));
    393             assert(!invoke(device, 6, 1));
    394 
    395             *memory(desc) = 124;
    396             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    397             uint32_t pixel = *memory(*memory(desc + 36) + 8 * *memory(desc + 16) + 8 * 4);
    398             assert((pixel & 0xffffff) == ((pass && level == 2 ? 0xffffff00 : colors[level]) & 0xffffff));
    399             assert(!invoke(target, 32, 2, 0u));
    400         }
    401     }
    402     assert(!invoke(device, 35, 3, 0u, 0u));
    403     for (unsigned i = 1; i < 5; ++i) assert(invoke(levels[i], 2, 1) == 1);
    404     assert(!invoke(levels[0], 2, 1));
    405     invoke(d3d, 2, 1);
    406 }
    407 
    408 static void transformed_vertex_fog_blends_rgb(uint32_t device, uint32_t target, FILE *file)
    409 {
    410     uint32_t desc = workspace() + 128, input = workspace() + 512;
    411     const uint32_t factors[][3] = {{0,0,0}, {128,128,128}, {255,255,255}, {0,128,255}, {0,128,255}, {0,128,255}, {0,0,0}};
    412     assert(!invoke(device, 20, 3, 27u, 0u));
    413     assert(!invoke(device, 20, 3, 34u, 0x9ac02040u));
    414     assert(!invoke(device, 21, 3, 34u, workspace()) && *memory(workspace()) == 0x9ac02040);
    415     /* Precomputed transformed-vertex fog ignores distance formula parameters. */
    416     assert(!invoke(device, 20, 3, 36u, 0x3e4ccccdu));
    417     assert(!invoke(device, 20, 3, 37u, 0x3f4ccccdu));
    418     assert(!invoke(device, 20, 3, 38u, 0x3f000000u));
    419     assert(!invoke(device, 21, 3, 36u, workspace()) && *memory(workspace()) == 0x3e4ccccd);
    420     assert(!invoke(device, 21, 3, 37u, workspace()) && *memory(workspace()) == 0x3f4ccccd);
    421     assert(!invoke(device, 21, 3, 38u, workspace()) && *memory(workspace()) == 0x3f000000);
    422     for (unsigned specular = 0; specular < 2; ++specular)
    423     for (unsigned mode = 0; mode <= 3; mode += 3) for (unsigned sample = 0; sample < 7; ++sample) {
    424         assert(!invoke(device, 20, 3, 29u, specular));
    425         assert(!invoke(device, 21, 3, 29u, workspace()) && *memory(workspace()) == specular);
    426         assert(!invoke(device, 20, 3, 140u, mode));
    427         assert(!invoke(device, 21, 3, 140u, workspace()) && *memory(workspace()) == mode);
    428         struct { float xyzw[4]; uint32_t diffuse, specular; } vertices[] = {
    429             {{4, 4, .25f, 1}, 0xff6080a0, (factors[sample][0] << 24) | 0xabcdef},
    430             {{28, 4, .25f, 1}, 0xff6080a0, (factors[sample][1] << 24) | 0xabcdef},
    431             {{4, 28, .25f, 1}, 0xff6080a0, (factors[sample][2] << 24) | 0xabcdef},
    432         };
    433         if (sample == 4) {
    434             vertices[0].specular = (vertices[0].specular & 0xff000000) | 0x102030;
    435             vertices[1].specular = (vertices[1].specular & 0xff000000) | 0x406080;
    436             vertices[2].specular = (vertices[2].specular & 0xff000000) | 0x80c0ff;
    437         }
    438         if (sample >= 4) { vertices[1].xyzw[3] = 2; vertices[2].xyzw[3] = .5f; }
    439         if (sample == 6) {
    440             for (unsigned i = 0; i < 3; ++i) memcpy(memory(input + i * 20), &vertices[i], 20);
    441         } else memcpy(memory(input), vertices, sizeof(vertices));
    442         assert(!invoke(device, 20, 3, 28u, sample == 5 ? 0u : 1u));
    443         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    444 
    445         assert(!invoke(device, 5, 1));
    446         assert(!invoke(device, 25, 6, 4u, sample == 6 ? 0x44u : 0xc4u, input, 3u, 0u));
    447         assert(!invoke(device, 6, 1));
    448         *memory(desc) = 124;
    449         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    450         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    451 
    452         if (sample == 0 || sample == 6) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0xc02040);
    453         if (sample == 2 || sample == 5) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == (specular ? 0xffffffu : 0x6080a0u));
    454         unsigned char rgb[32 * 32 * 3];
    455         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    456             uint32_t pixel = *memory(pixels + y * pitch + x * 4);
    457             rgb[(y * 32 + x) * 3] = pixel >> 16;
    458             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
    459             rgb[(y * 32 + x) * 3 + 2] = pixel;
    460         }
    461         assert(!invoke(target, 32, 2, 0u));
    462         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
    463     }
    464     assert(!invoke(device, 20, 3, 28u, 0u));
    465     assert(!invoke(device, 20, 3, 140u, 0u));
    466     assert(!invoke(device, 20, 3, 29u, 0u));
    467 }
    468 
    469 static void vertex_buffer_draws_match_memory_draws(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
    470 {
    471     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    472     const uint32_t formats[] = {0x44, 0xc4, 0x144, 0x1c4};
    473     const unsigned strides[] = {20, 24, 28, 32};
    474     const float positions[][4] = {{4,4,.25f,1}, {28,4,.25f,1}, {4,28,.25f,1}, {28,28,.25f,1}};
    475     const unsigned list[] = {0,1,2,2,1,3};
    476     const unsigned fan[] = {0,1,3,2};
    477     uint32_t d3d = query(draw, iid), desc = workspace() + 128, input = workspace() + 512;
    478     assert(!invoke(device, 20, 3, 27u, 0u));
    479     assert(!invoke(device, 20, 3, 14u, 1u));
    480     assert(!invoke(device, 20, 3, 23u, 4u));
    481     for (unsigned format = 0; format < 4; ++format)
    482     for (unsigned primitive = 4; primitive <= 6; ++primitive) for (unsigned reverse = 0; reverse <= 1; ++reverse) {
    483         unsigned stride = strides[format], count = primitive == 4 ? 6 : 4;
    484         uint32_t description[] = {16, 0x800, formats[format], 8};
    485         memcpy(memory(desc), description, 16);
    486         assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
    487         uint32_t buffer = *memory(workspace());
    488         unsigned char original[256];
    489         memset(original, 0xa5, sizeof(original));
    490         for (unsigned i = 0; i < count; ++i) {
    491             unsigned char *vertex = original + (i + 2) * stride;
    492             float position[4];
    493             memcpy(position, positions[primitive == 4 ? list[i] : primitive == 6 ? fan[i] : i], 16);
    494             if (reverse) { float x = position[0]; position[0] = position[1]; position[1] = x; }
    495             memcpy(vertex, position, 16);
    496             uint32_t color = 0xff5a96d2, specular = 0xffffffff;
    497             memcpy(vertex + 16, &color, 4);
    498             if (formats[format] & 0x80) memcpy(vertex + 20, &specular, 4);
    499             if (formats[format] & 0x100) {
    500                 const float uv[] = {.125f, .875f};
    501                 memcpy(vertex + stride - 8, uv, 8);
    502             }
    503         }
    504         memcpy(memory(input), original, stride * 8);
    505         assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u));
    506         memcpy(memory(*memory(workspace())), original, stride * 8);
    507         assert(!invoke(buffer, 4, 1));
    508         assert(!invoke(buffer, 7, 3, device, 0u));
    509         for (unsigned use_buffer = 0; use_buffer <= 1; ++use_buffer) for (unsigned cull = 1; cull <= 3; ++cull) {
    510             assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    511             assert(!invoke(device, 20, 3, 22u, cull));
    512             assert(!invoke(device, 21, 3, 22u, workspace()) && *memory(workspace()) == cull);
    513             assert(!invoke(device, 5, 1));
    514             if (use_buffer) assert(!invoke(device, 31, 6, primitive, buffer, 2u, count, 0u));
    515             else assert(!invoke(device, 25, 6, primitive, formats[format], input + 2*stride, count, 0u));
    516             assert(!invoke(device, 6, 1));
    517 
    518             *memory(desc) = 124;
    519             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    520             uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    521             uint32_t color = cull == (reverse ? 3 : 2) ? 0x102030 : 0x5a96d2;
    522             assert((*memory(pixels + 10 * pitch + 10 * 4) & 0xffffff) == color);
    523             assert((*memory(pixels + 23 * pitch + 23 * 4) & 0xffffff) == color);
    524             assert((*memory(pixels) & 0xffffff) == 0x102030);
    525             unsigned char rgb[32 * 32 * 3];
    526             for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    527                 uint32_t pixel = *memory(pixels + y * pitch + x * 4);
    528                 rgb[(y * 32 + x) * 3] = pixel >> 16;
    529                 rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
    530                 rgb[(y * 32 + x) * 3 + 2] = pixel;
    531             }
    532             assert(!invoke(target, 32, 2, 0u));
    533             assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
    534         }
    535         assert(!invoke(buffer, 3, 4, 0x11u, workspace(), 0u));
    536         assert(!memcmp(memory(*memory(workspace())), original, stride * 8));
    537         assert(!invoke(buffer, 4, 1));
    538         /* Native drivers may retain the most recently submitted vertex buffer. */
    539         invoke(buffer, 2, 1);
    540     }
    541     assert(!invoke(device, 20, 3, 22u, 1u));
    542     invoke(d3d, 2, 1);
    543 }
    544 
    545 static void indexed_vertex_buffers_use_relative_indices(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
    546 {
    547     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    548     const uint32_t formats[] = {0x44, 0xc4, 0x144, 0x1c4};
    549     const unsigned strides[] = {20, 24, 28, 32};
    550     const float positions[][4] = {{4,28,.25f,1}, {4,4,.25f,1}, {28,28,.25f,1}, {28,4,.25f,1}};
    551     const uint16_t lists[][6] = {{1,3,0,0,3,2}, {1,3,0,2}, {1,3,2,0}};
    552     uint32_t d3d = query(draw, iid), desc = workspace() + 128, elements = workspace() + 512;
    553     for (unsigned format = 0; format < 4; ++format) for (unsigned start = 0; start <= 2; start += 2) {
    554         uint32_t description[] = {16, 0x800, formats[format], 6};
    555         memcpy(memory(desc), description, 16);
    556         assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
    557         uint32_t buffer = *memory(workspace());
    558         assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u));
    559         uint32_t input = *memory(workspace());
    560         memset(memory(input), 0xa5, 6 * strides[format]);
    561         for (unsigned i = 0; i < 4; ++i) {
    562             uint32_t vertex = input + (start + i) * strides[format];
    563             memcpy(memory(vertex), positions[i], 16);
    564             *memory(vertex + 16) = 0xff5a96d2;
    565             if (formats[format] & 0x80) *memory(vertex + 20) = 0xffffffff;
    566             if (formats[format] & 0x100) memset(memory(vertex + strides[format] - 8), 0, 8);
    567         }
    568         assert(!invoke(buffer, 4, 1));
    569         for (unsigned primitive = 4; primitive <= 6; ++primitive) for (unsigned cull = 1; cull <= 3; ++cull) {
    570             memcpy(memory(elements), lists[primitive - 4], 12);
    571             assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    572             assert(!invoke(device, 20, 3, 22u, cull));
    573             assert(!invoke(device, 5, 1));
    574             assert(!invoke(device, 32, 8, primitive, buffer, start, 4u, elements, primitive == 4 ? 6u : 4u, 0u));
    575             assert(!invoke(device, 6, 1));
    576 
    577             *memory(desc) = 124;
    578             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    579             uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    580             assert((*memory(pixels + 10 * pitch + 10 * 4) & 0xffffff) == (cull == 2 ? 0x102030 : 0x5a96d2));
    581             assert((*memory(pixels + 23 * pitch + 23 * 4) & 0xffffff) == (cull == 2 ? 0x102030 : 0x5a96d2));
    582             unsigned char rgb[32 * 32 * 3];
    583             for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    584                 uint32_t pixel = *memory(pixels + y * pitch + x * 4);
    585                 rgb[(y * 32 + x) * 3] = pixel >> 16;
    586                 rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
    587                 rgb[(y * 32 + x) * 3 + 2] = pixel;
    588             }
    589             assert(!invoke(target, 32, 2, 0u));
    590             assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
    591         }
    592         invoke(buffer, 2, 1);
    593     }
    594     assert(!invoke(device, 20, 3, 22u, 1u));
    595     invoke(d3d, 2, 1);
    596 }
    597 
    598 static void vertex_buffer_writes_update_rendered_pixels(uint32_t draw, uint32_t device, uint32_t target)
    599 {
    600     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    601     uint32_t d3d = query(draw, iid), desc = workspace() + 128, elements = workspace() + 512;
    602     const uint32_t description[] = {16, 0x800, 0x142, 6};
    603     const float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    604     const float positions[][3] = {{-.75f,.75f,.5f}, {.75f,.75f,.5f}, {-.75f,-.75f,.5f}, {.75f,-.75f,.5f}};
    605     const uint16_t indices[] = {0,1,2,1,3,2};
    606     memcpy(memory(desc), description, sizeof(description));
    607     assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
    608     uint32_t buffer = *memory(workspace());
    609     assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u));
    610     uint32_t input = *memory(workspace());
    611     memset(memory(input), 0, 144);
    612     for (unsigned i = 0; i < 4; ++i) {
    613         memcpy(memory(input + (i + 2) * 24), positions[i], 12);
    614         *memory(input + (i + 2) * 24 + 12) = 0xff5a96d2;
    615     }
    616     assert(!invoke(buffer, 4, 1));
    617     memcpy(memory(elements), indices, sizeof(indices));
    618     memcpy(memory(workspace() + 256), identity, sizeof(identity));
    619     for (unsigned transform = 1; transform <= 3; ++transform)
    620         assert(!invoke(device, 11, 3, transform, workspace() + 256));
    621     assert(!invoke(device, 20, 3, 137u, 0u));
    622     assert(!invoke(device, 20, 3, 29u, 0u));
    623     assert(!invoke(device, 20, 3, 27u, 0u));
    624     assert(!invoke(device, 20, 3, 28u, 0u));
    625     assert(!invoke(device, 20, 3, 15u, 0u));
    626     assert(!invoke(device, 20, 3, 22u, 1u));
    627     assert(!invoke(device, 20, 3, 23u, 8u));
    628     assert(!invoke(device, 35, 3, 0u, 0u));
    629     assert(!invoke(device, 37, 4, 0u, 1u, 2u));
    630     assert(!invoke(device, 37, 4, 0u, 2u, 0u));
    631     assert(!invoke(device, 37, 4, 1u, 1u, 1u));
    632 
    633     const uint32_t expected[] = {0x5a96d2, 0xff0000, 0xff0000};
    634     for (unsigned step = 0; step < 3; ++step) {
    635         if (step) {
    636             assert(!invoke(buffer, 3, 4, step == 1 ? 0x1020u : 0x11u, workspace(), 0u));
    637             if (step == 1) for (unsigned i = 0; i < 4; ++i)
    638                 *memory(*memory(workspace()) + (i + 2) * 24 + 12) = 0xffff0000;
    639             assert(!invoke(buffer, 4, 1));
    640         }
    641         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff000000u, 0x3f800000u, 0u));
    642         assert(!invoke(device, 5, 1));
    643         assert(!invoke(device, 32, 8, 4u, buffer, 2u, 4u, elements, 6u, 0u));
    644         assert(!invoke(device, 6, 1));
    645 
    646         *memory(desc) = 124;
    647         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    648         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    649         assert((*memory(pixels + 16 * pitch + 16 * 4) & 0xffffff) == expected[step]);
    650         assert(!invoke(target, 32, 2, 0u));
    651     }
    652     invoke(buffer, 2, 1);
    653     invoke(d3d, 2, 1);
    654     assert(!invoke(device, 20, 3, 23u, 4u));
    655     assert(!invoke(device, 37, 4, 0u, 2u, 2u));
    656 }
    657 
    658 static void line_lists_draw_independent_segments(uint32_t draw, uint32_t device, uint32_t target)
    659 {
    660     const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
    661     uint32_t d3d = query(draw, iid), desc = workspace() + 128, input = workspace() + 512;
    662     const float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    663     memcpy(memory(workspace() + 256), identity, 64);
    664     for (unsigned transform = 1; transform <= 3; ++transform)
    665         assert(!invoke(device, 11, 3, transform, workspace() + 256));
    666     assert(!invoke(device, 20, 3, 137u, 0u));
    667     assert(!invoke(device, 20, 3, 28u, 0u));
    668     assert(!invoke(device, 20, 3, 29u, 0u));
    669     assert(!invoke(device, 20, 3, 15u, 0u));
    670     assert(!invoke(device, 20, 3, 27u, 0u));
    671     assert(!invoke(device, 20, 3, 23u, 4u));
    672     assert(!invoke(device, 35, 3, 0u, 0u));
    673     assert(!invoke(device, 37, 4, 0u, 1u, 2u));
    674     assert(!invoke(device, 37, 4, 0u, 2u, 0u));
    675     assert(!invoke(device, 37, 4, 0u, 4u, 2u));
    676     assert(!invoke(device, 37, 4, 0u, 5u, 0u));
    677     assert(!invoke(device, 37, 4, 0u, 11u, 0u));
    678     assert(!invoke(device, 37, 4, 0u, 24u, 0u));
    679     assert(!invoke(device, 37, 4, 1u, 1u, 1u));
    680 
    681     for (unsigned transformed = 0; transformed <= 1; ++transformed) {
    682         unsigned stride = transformed ? 28 : 24;
    683         uint32_t format = transformed ? 0x144 : 0x142;
    684         const float positions[][4] = {{4,8,.25f,1}, {28,8,.25f,1}, {4,24,.25f,1}, {28,24,.25f,1}};
    685         for (unsigned i = 0; i < 4; ++i) {
    686             uint32_t vertex = input + i * stride;
    687             if (transformed) memcpy(memory(vertex), positions[i], 16);
    688             else {
    689                 float xyz[] = {positions[i][0] / 16 - 1, 1 - positions[i][1] / 16, .25f};
    690                 memcpy(memory(vertex), xyz, 12);
    691             }
    692             *memory(vertex + stride - 12) = 0xff5a96d2;
    693             memset(memory(vertex + stride - 8), 0, 8);
    694         }
    695         uint32_t description[] = {16, 0x800, format, 6};
    696         memcpy(memory(desc), description, 16);
    697         assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
    698         uint32_t buffer = *memory(workspace());
    699         assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u));
    700         memcpy(memory(*memory(workspace()) + 2 * stride), memory(input), 4 * stride);
    701         assert(!invoke(buffer, 4, 1));
    702         const uint16_t indices[] = {1, 0, 3, 2};
    703         memcpy(memory(input + 128), indices, sizeof(indices));
    704 
    705         for (unsigned path = 0; path < 3; ++path)
    706         for (unsigned cull = 1; cull <= 3; ++cull) for (unsigned count = 2; count <= 4; count += 2) {
    707             assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    708             assert(!invoke(device, 20, 3, 22u, cull));
    709             assert(!invoke(device, 5, 1));
    710             if (path == 0) assert(!invoke(device, 25, 6, 2u, format, input, count, 0u));
    711             else if (path == 1) assert(!invoke(device, 31, 6, 2u, buffer, 2u, count, 0u));
    712             else assert(!invoke(device, 32, 8, 2u, buffer, 2u, count, input + 128, count, 0u));
    713             assert(!invoke(device, 6, 1));
    714 
    715             *memory(desc) = 124;
    716             assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    717             uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    718             assert((*memory(pixels + 8 * pitch + 16 * 4) & 0xffffff) == 0x5a96d2);
    719             assert((*memory(pixels + 16 * pitch + 16 * 4) & 0xffffff) == 0x102030);
    720             assert((*memory(pixels + 24 * pitch + 16 * 4) & 0xffffff) == (count == 4 ? 0x5a96d2 : 0x102030));
    721             assert(!invoke(target, 32, 2, 0u));
    722         }
    723         invoke(buffer, 2, 1);
    724     }
    725     invoke(d3d, 2, 1);
    726 }
    727 
    728 static void projective_world_and_view_matrices_render(uint32_t device, uint32_t target)
    729 {
    730     uint32_t data = workspace() + 256, input = workspace() + 512, desc = workspace() + 128;
    731     const float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    732     const float projective[] = {1,0,0,.5f, 0,1,0,0, 0,0,1,0, .5f,0,0,1};
    733     const float positions[][3] = {{-.75f,.75f,.5f}, {.75f,.75f,.5f}, {.75f,-.75f,.5f}, {-.75f,-.75f,.5f}};
    734     for (unsigned i = 0; i < 4; ++i) {
    735         const float normal[] = {1,0,0};
    736         memcpy(memory(input + i * 36), positions[i], 12);
    737         memcpy(memory(input + i * 36 + 12), normal, 12);
    738         *memory(input + i * 36 + 24) = 0xff5a96d2;
    739         memset(memory(input + i * 36 + 28), 0, 8);
    740     }
    741     const float material[] = {.5f,0,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0};
    742     memcpy(memory(data), material, sizeof(material));
    743     assert(!invoke(device, 16, 2, data));
    744     float light[26] = {0};
    745     light[1] = 1; light[16] = -1;
    746     memcpy(memory(data), light, sizeof(light));
    747     *memory(data) = 3;
    748     assert(!invoke(device, 18, 3, 0u, data));
    749     for (unsigned i = 0; i < 9; ++i) assert(!invoke(device, 44, 3, i, i == 0));
    750     assert(!invoke(device, 20, 3, 139u, 0u));
    751     assert(!invoke(device, 20, 3, 143u, 0u));
    752     assert(!invoke(device, 20, 3, 145u, 0u));
    753     assert(!invoke(device, 20, 3, 148u, 0u));
    754     assert(!invoke(device, 20, 3, 28u, 0u));
    755     assert(!invoke(device, 20, 3, 27u, 0u));
    756     assert(!invoke(device, 20, 3, 15u, 0u));
    757     assert(!invoke(device, 20, 3, 22u, 1u));
    758     assert(!invoke(device, 35, 3, 0u, 0u));
    759     assert(!invoke(device, 37, 4, 0u, 1u, 2u));
    760     assert(!invoke(device, 37, 4, 0u, 2u, 0u));
    761     assert(!invoke(device, 37, 4, 0u, 11u, 0u));
    762     assert(!invoke(device, 37, 4, 0u, 24u, 0u));
    763     assert(!invoke(device, 37, 4, 1u, 1u, 1u));
    764     /* Disabled stages can retain normal-coordinate generation without using it. */
    765     assert(!invoke(device, 37, 4, 1u, 11u, 0x10000u));
    766 
    767     for (unsigned transform = 1; transform <= 2; ++transform)
    768     for (unsigned cpu = 0; cpu <= 1; ++cpu)
    769     for (unsigned lighting = 0; lighting <= 1; ++lighting)
    770     for (unsigned singular = 0; singular <= !lighting; ++singular) {
    771         for (unsigned state = 1; state <= 3; ++state) {
    772             memcpy(memory(data), state == transform ? projective : identity, 64);
    773             if (state == transform && singular) *memory(data + 40) = 0;
    774             assert(!invoke(device, 11, 3, state, data));
    775         }
    776         assert(!invoke(device, 20, 3, 29u, cpu));
    777         assert(!invoke(device, 20, 3, 137u, lighting));
    778         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    779         assert(!invoke(device, 5, 1));
    780         assert(!invoke(device, 25, 6, 6u, 0x152u, input, 4u, 0u));
    781         assert(!invoke(device, 6, 1));
    782 
    783         *memory(desc) = 124;
    784         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    785         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    786         assert((*memory(pixels + 16 * pitch + 16 * 4) & 0xffffff) == (lighting ? 0xaa0000u : 0x5a96d2u));
    787         assert((*memory(pixels + 4 * pitch + 4 * 4) & 0xffffff) == 0x102030);
    788         assert(!invoke(target, 32, 2, 0u));
    789     }
    790 }
    791 
    792 static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, uint32_t target, FILE *file)
    793 {
    794     for (unsigned emissive = 0; emissive < 3; ++emissive)
    795     for (unsigned specular = 0; specular < 5; ++specular)
    796     for (unsigned colored = 0; colored <= 2; ++colored) for (unsigned sample = 0; sample < 23; ++sample) {
    797         uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128;
    798 #ifndef _WIN32
    799         /* Exercise vertex buffers across contiguous and independently mapped pages. */
    800         if (!colored && !specular && !emissive && sample < 2)
    801             input = sample ? 0x5200ffe0 : 0x5100ffe0;
    802 #endif
    803         float vertices[3][8] = {{-.75f,.75f,.5f,0,0,-1,0,0}, {.75f,.75f,.5f,0,0,-1,1,0}, {-.75f,-.75f,.5f,0,0,-1,0,1}};
    804         float world[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    805         float view[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    806         float projection[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    807         float material[] = {.4f,.6f,.8f,.5f, .5f,.25f,.75f,1, 0,0,0,0, 0,0,0,0, 0};
    808         float light[26] = {0, .5f,.75f,1,0, 0,0,0,0, .1f,.2f,.3f,0, 0,0,-1, 0,0,1, 10,2, 1,0,0, .5f,1.5f};
    809         material[8] = .4f; material[9] = .5f; material[10] = .6f;
    810         material[16] = specular == 2 || specular == 4 ? 0 : specular == 3 ? 2 : 8;
    811         light[5] = .5f; light[6] = .6f; light[7] = .7f;
    812         assert(!invoke(device, 20, 3, 29u, specular ? 1u : 0u));
    813         assert(!invoke(device, 20, 3, 146u, specular ? 0u : 2u));
    814         assert(!invoke(device, 21, 3, 146u, data) && *memory(data) == (specular ? 0u : 2u));
    815         assert(!invoke(device, 20, 3, 148u, emissive));
    816         assert(!invoke(device, 21, 3, 148u, data) && *memory(data) == emissive);
    817         uint32_t type = sample >= 9 && sample <= 10 ? 1 : sample >= 11 && sample <= 12 ? 2 : 3;
    818         if (sample == 1) { material[12] = .2f; material[13] = .4f; material[14] = .6f; }
    819         if (sample == 4 || sample == 5) for (unsigned i = 0; i < 3; ++i) vertices[i][5] = -2;
    820         if (sample == 6) { vertices[0][5] = -.25f; vertices[1][5] = -.5f; vertices[2][5] = -.75f; }
    821         if (sample == 7) { world[0] = .5f; world[5] = .75f; world[10] = .5f; world[12] = .125f; view[12] = -.25f; }
    822         if (sample == 8) { view[0] = 0; view[1] = 1; view[4] = -1; view[5] = 0; light[16] = 1; }
    823         if (sample == 9 || sample == 10) { light[13] = .3f; light[14] = .1f; light[21] = .5f; light[22] = .2f; light[23] = .1f; }
    824         if (sample == 10) light[19] = .6f;
    825         if (sample == 12) { light[24] = .1f; light[25] = 1; light[13] = -.5f; }
    826         if (sample == 13 || sample == 14) { vertices[0][2] = .25f; vertices[1][2] = .5f; vertices[2][2] = .75f; }
    827         if (sample == 14) { projection[10] = .5f; projection[11] = 1; projection[15] = 0; }
    828         if (sample == 15) { world[0] = .5f; world[1] = .5f; world[4] = -.5f; world[5] = .5f; world[8] = .25f; vertices[0][3] = .5f; }
    829         /* Mixed lights, shader capacity, range boundaries and coincident vertices. */
    830         if (sample >= 17) type = 1;
    831         if (sample == 20) { light[13] = -.75f; light[14] = .75f; light[15] = .5f; }
    832         if (sample == 21) { light[13] = -.75f; light[14] = .75f; light[15] = -.5f; light[19] = 1; }
    833         if (sample == 22) { view[0] = 0; view[1] = 1; view[4] = -1; view[5] = 0; view[12] = .1f; }
    834         memcpy(memory(data), world, 64); assert(!invoke(device, 11, 3, 1u, data));
    835         memcpy(memory(data), view, 64); assert(!invoke(device, 11, 3, 2u, data));
    836         memcpy(memory(data), projection, 64); assert(!invoke(device, 11, 3, 3u, data));
    837         memcpy(memory(data), material, 68); assert(!invoke(device, 16, 2, data));
    838         assert(!invoke(device, 17, 2, data));
    839         assert(!memcmp(memory(data), material, 68));
    840         memcpy(memory(data), light, 104); *memory(data) = type;
    841         assert(!invoke(device, 18, 3, 0u, data));
    842         assert(!invoke(device, 44, 3, 0u, sample >= 3 ? 1u : 0u));
    843         unsigned lights = sample == 17 ? 2 : sample == 18 ? 8 : sample == 19 ? 9 : 1;
    844         for (unsigned i = 1; i < 9; ++i) {
    845             light[1] = .03f; light[2] = .02f; light[3] = .01f;
    846             light[9] = .01f; light[10] = .02f; light[11] = .03f;
    847             light[13] = i * .1f;
    848             memcpy(memory(data), light, 104); *memory(data) = i % 2 ? 3 : 1;
    849             assert(!invoke(device, 18, 3, i, data));
    850             assert(!invoke(device, 44, 3, i, i < lights ? 1u : 0u));
    851         }
    852         assert(!invoke(device, 20, 3, 137u, sample ? 1u : 0u));
    853         assert(!invoke(device, 20, 3, 139u, sample == 2 ? 0x00804020u : 0u));
    854         assert(!invoke(device, 20, 3, 143u, sample == 5 ? 1u : 0u));
    855         assert(!invoke(device, 20, 3, 28u, sample == 13 || sample == 14 ? 1u : 0u));
    856         assert(!invoke(device, 20, 3, 140u, 3u));
    857         assert(!invoke(device, 20, 3, 34u, 0xffc02040u));
    858         assert(!invoke(device, 20, 3, 36u, 0u));
    859         assert(!invoke(device, 20, 3, 37u, 0x3f800000u));
    860         assert(!invoke(device, 20, 3, 27u, sample == 16 ? 1u : 0u));
    861         assert(!invoke(device, 20, 3, 19u, 5u));
    862         assert(!invoke(device, 20, 3, 20u, 6u));
    863         if (colored) for (unsigned i = 0; i < 3; ++i) {
    864             unsigned stride = colored == 1 ? 36 : 24, color_offset = colored == 1 ? 24 : 12;
    865             memcpy(memory(input + i * stride), vertices[i], color_offset);
    866             *memory(input + i * stride + color_offset) = sample == 6 ? (0xff204060u + i * 0x203020u) : 0x804060a0u;
    867             memcpy(memory(input + i * stride + color_offset + 4), vertices[i] + 6, 8);
    868         } else for (unsigned i = 0; i < 3; ++i)
    869             memcpy(memory(input + i * sizeof(vertices[i])), vertices[i], sizeof(vertices[i]));
    870         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    871 
    872         assert(!invoke(device, 5, 1));
    873         assert(!invoke(device, 25, 6, 4u, colored == 2 ? 0x142u : colored ? 0x152u : 0x112u, input, 3u, 0u));
    874         assert(!invoke(device, 6, 1));
    875 
    876         *memory(desc) = 124;
    877         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    878         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    879         if (!emissive && !specular && !colored && sample >= 17) {
    880             const uint32_t expected[] = {0x3b6fe8, 0x5287ff, 0x568aff, 0x0d0d39, 0x2a55aa, 0x366ae0};
    881             uint32_t pixel = *memory(pixels + 8 * pitch + 32);
    882             /* Allow one quantization step for GPU floating-point rounding. */
    883             for (unsigned channel = 0; channel < 3; ++channel) {
    884                 int difference = (int)((pixel >> (channel * 8)) & 255) -
    885                     (int)((expected[sample - 17] >> (channel * 8)) & 255);
    886                 assert(difference >= -1 && difference <= 1);
    887             }
    888         }
    889         if (sample == 0) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == (colored ? 0x4060a0 : 0xffffff));
    890         if (sample == 1) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) ==
    891             (emissive == 1 && colored ? 0x4060a0 : 0x336699));
    892         unsigned char rgb[32 * 32 * 3];
    893         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    894             uint32_t pixel = *memory(pixels + y * pitch + x * 4);
    895             rgb[(y * 32 + x) * 3] = pixel >> 16;
    896             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
    897             rgb[(y * 32 + x) * 3 + 2] = pixel;
    898         }
    899         assert(!invoke(target, 32, 2, 0u));
    900         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
    901     }
    902     float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    903     memcpy(memory(workspace() + 256), identity, 64);
    904     for (unsigned state = 1; state <= 3; ++state) assert(!invoke(device, 11, 3, state, workspace() + 256));
    905     assert(!invoke(device, 44, 3, 0u, 0u));
    906     assert(!invoke(device, 20, 3, 28u, 0u));
    907     assert(!invoke(device, 20, 3, 140u, 0u));
    908     assert(!invoke(device, 20, 3, 27u, 0u));
    909     assert(!invoke(device, 20, 3, 29u, 0u));
    910     assert(!invoke(device, 20, 3, 146u, 2u));
    911     assert(!invoke(device, 20, 3, 148u, 0u));
    912 }
    913 
    914 static void diffuse_material_source_selects_lighting_color(uint32_t device, uint32_t target)
    915 {
    916     uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128;
    917     struct vertex { float x, y, z, nx, ny, nz; uint32_t color; float u, v; } vertices[] = {
    918         {-.75f,.75f,.5f,0,0,-1,0xffff0000,0,0},
    919         {.75f,.75f,.5f,0,0,-1,0xffff0000,0,0},
    920         {-.75f,-.75f,.5f,0,0,-1,0xffff0000,0,0}
    921     };
    922     const float material[] = {0,1,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0};
    923     const float light[] = {0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 0,0,0, 0,0,1, 0,0, 0,0,0, 0,0};
    924     memcpy(memory(data), material, sizeof(material));
    925     assert(!invoke(device, 16, 2, data));
    926     memcpy(memory(data), light, sizeof(light));
    927     *memory(data) = 3;
    928     assert(!invoke(device, 18, 3, 0u, data));
    929     assert(!invoke(device, 44, 3, 0u, 1u));
    930     assert(!invoke(device, 20, 3, 139u, 0u));
    931     for (unsigned lighting = 0; lighting <= 1; ++lighting)
    932     for (unsigned colored = 0; colored <= 1; ++colored)
    933     for (unsigned source = 0; source <= 2; ++source) {
    934         for (unsigned i = 0; i < 3; ++i) {
    935             if (colored) memcpy(memory(input + i * 36), &vertices[i], 36);
    936             else {
    937                 memcpy(memory(input + i * 32), &vertices[i], 24);
    938                 memcpy(memory(input + i * 32 + 24), &vertices[i].u, 8);
    939             }
    940         }
    941         assert(!invoke(device, 20, 3, 137u, lighting));
    942         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    943 
    944         assert(!invoke(device, 20, 3, 145u, source));
    945         assert(!invoke(device, 5, 1));
    946         assert(!invoke(device, 25, 6, 4u, colored ? 0x152u : 0x112u, input, 3u, 0u));
    947         assert(!invoke(device, 6, 1));
    948 
    949         assert(!invoke(device, 21, 3, 145u, data) && *memory(data) == source);
    950         *memory(desc) = 124;
    951         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    952         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    953         uint32_t expected = lighting ? (colored && source == 1 ? 0xff0000 : 0x00ff00) :
    954                                       (colored ? 0xff0000 : 0xffffff);
    955         assert((*memory(pixels + 8 * pitch + 8 * 4) & 0xffffff) == expected);
    956         assert(!invoke(target, 32, 2, 0u));
    957     }
    958     assert(!invoke(device, 44, 3, 0u, 0u));
    959     assert(!invoke(device, 20, 3, 145u, 1u));
    960 }
    961 
    962 static void light_enable_changes_apply_between_draws(uint32_t device, uint32_t target)
    963 {
    964     uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128;
    965     const float vertices[][8] = {{-.75f,.75f,.5f,0,0,-1,0,0}, {.75f,.75f,.5f,0,0,-1,1,0}, {-.75f,-.75f,.5f,0,0,-1,0,1}};
    966     const float material[] = {1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0};
    967     memcpy(memory(input), vertices, sizeof(vertices));
    968     memcpy(memory(data), material, sizeof(material));
    969     assert(!invoke(device, 16, 2, data));
    970     assert(!invoke(device, 20, 3, 137u, 1u));
    971     assert(!invoke(device, 20, 3, 139u, 0u));
    972     for (unsigned index = 0; index < 30; ++index) {
    973         float light[26] = {0};
    974         light[1] = index == 1;
    975         light[2] = index == 15;
    976         light[3] = index == 29;
    977         light[18] = 1;
    978         memcpy(memory(data), light, sizeof(light));
    979         *memory(data) = 3;
    980         assert(!invoke(device, 18, 3, index, data));
    981         assert(!invoke(device, 44, 3, index, 0u));
    982     }
    983     const unsigned masks[] = {0, 5, 2, 0, 7, 1, 4};
    984     const uint32_t expected[] = {0, 0xff00ff, 0x00ff00, 0, 0xffffff, 0xff0000, 0x0000ff};
    985     for (unsigned sample = 0; sample < sizeof(masks) / sizeof(*masks); ++sample) {
    986         assert(!invoke(device, 44, 3, 1u, masks[sample] & 1u));
    987         assert(!invoke(device, 44, 3, 15u, (masks[sample] >> 1) & 1u));
    988         assert(!invoke(device, 44, 3, 29u, (masks[sample] >> 2) & 1u));
    989         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
    990         assert(!invoke(device, 5, 1));
    991         assert(!invoke(device, 25, 6, 4u, 0x112u, input, 3u, 0u));
    992         assert(!invoke(device, 6, 1));
    993 
    994         *memory(desc) = 124;
    995         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
    996         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
    997         assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == expected[sample]);
    998         assert(!invoke(target, 32, 2, 0u));
    999     }
   1000     assert(!invoke(device, 44, 3, 29u, 0u));
   1001 }
   1002 
   1003 static void initial_device_states(uint32_t device)
   1004 {
   1005     const uint32_t states[][2] = {
   1006         {2, 0xffffffff}, {4, 1}, {7, 1}, {8, 3}, {9, 2}, {10, 0},
   1007         {14, 1}, {15, 0}, {16, 1}, {19, 2}, {20, 1}, {22, 3}, {23, 4},
   1008         {24, 0}, {25, 8}, {26, 0}, {27, 0}, {28, 0}, {29, 0}, {30, 0},
   1009         {33, 0}, {34, 0}, {35, 0}, {36, 0}, {37, 0x3f800000}, {38, 0x3f800000},
   1010         {40, 0xffffffff}, {41, 0}, {47, 0}, {48, 0}, {52, 0}, {53, 1},
   1011         {54, 1}, {55, 1}, {56, 8}, {57, 0}, {58, 0xffffffff},
   1012         {59, 0xffffffff}, {60, 0xffffffff}, {128, 0}, {129, 0}, {130, 0},
   1013         {131, 0}, {132, 0}, {133, 0}, {134, 0}, {135, 0}, {136, 1},
   1014         {137, 1}, {138, 0}, {139, 0}, {140, 0}, {141, 1}, {142, 1},
   1015         {143, 0}, {144, 0}, {145, 1}, {146, 2}, {147, 0}, {148, 0}, {151, 0}, {152, 0}
   1016     };
   1017     const uint32_t texture_defaults[] = {4, 2, 1, 2, 2, 1, 0, 0, 0, 0, 0, 1,
   1018                                         1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0};
   1019 
   1020     for (unsigned i = 0; i < sizeof(states) / sizeof(states[0]); ++i) {
   1021         assert(!invoke(device, 21, 3, states[i][0], workspace() + 4 * i));
   1022     }
   1023     for (unsigned stage = 0; stage < 8; ++stage) for (unsigned state = 1; state <= 24; ++state) {
   1024         assert(!invoke(device, 36, 4, stage, state, workspace() + 256 + (stage * 24 + state - 1) * 4));
   1025     }
   1026 
   1027     for (unsigned i = 0; i < sizeof(states) / sizeof(states[0]); ++i) {
   1028         assert(*memory(workspace() + 4 * i) == states[i][1]);
   1029     }
   1030     for (unsigned stage = 0; stage < 8; ++stage) for (unsigned state = 1; state <= 24; ++state) {
   1031         uint32_t expected = stage && (state == 1 || state == 4) ? 1 : texture_defaults[state - 1];
   1032         assert(*memory(workspace() + 256 + (stage * 24 + state - 1) * 4) == expected);
   1033     }
   1034 }
   1035 
   1036 static void rectangle_clears_preserve_outside_depth(uint32_t device, uint32_t target, FILE *file)
   1037 {
   1038     uint32_t rectangles = workspace() + 64, input = workspace() + 512, desc = workspace() + 128;
   1039     const int32_t regions[][4] = {{3, 5, 27, 23}, {1, 1, 15, 13}, {13, 9, 31, 31}};
   1040     memcpy(memory(rectangles), regions, sizeof(regions));
   1041     struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1042         {0, 0, .5f, 1, 0xffffffff}, {32, 0, .5f, 1, 0xffffffff}, {0, 32, .5f, 1, 0xffffffff}
   1043     };
   1044     memcpy(memory(input), vertices, sizeof(vertices));
   1045     assert(!invoke(device, 20, 3, 27u, 0u));
   1046     for (unsigned flags = 1; flags <= 3; ++flags) for (unsigned count = 0; count <= 2; ++count) {
   1047         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1048 
   1049         assert(!invoke(device, 10, 7, count, count ? rectangles + (count == 2 ? 16 : 0) : 0,
   1050                        flags, 0xff335577u, 0u, 0u));
   1051         assert(!invoke(device, 5, 1));
   1052         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1053         assert(!invoke(device, 6, 1));
   1054         *memory(desc) = 124;
   1055         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1056 
   1057         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1058         uint32_t inside = flags == 1 ? 0xffffff : flags == 2 ? 0x102030 : 0x335577;
   1059         assert((*memory(pixels) & 0xffffff) == (count ? 0xffffff : inside));
   1060         assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == inside);
   1061         assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) ==
   1062                (!count && (flags & 1) ? 0x335577 : 0x102030));
   1063         unsigned char rgb[32 * 32 * 3];
   1064         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   1065             uint32_t pixel = *memory(pixels + y * pitch + x * 4);
   1066             rgb[(y * 32 + x) * 3] = pixel >> 16;
   1067             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   1068             rgb[(y * 32 + x) * 3 + 2] = pixel;
   1069         }
   1070         assert(!invoke(target, 32, 2, 0u));
   1071         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   1072     }
   1073 }
   1074 
   1075 static void depth_write_controls_occlusion(uint32_t device, uint32_t target)
   1076 {
   1077     for (unsigned enabled = 0; enabled <= 1; ++enabled) {
   1078         uint32_t input = workspace() + 512, desc = workspace() + 128;
   1079         struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1080             {0, 0, .25f, 1, 0xffff0000}, {32, 0, .25f, 1, 0xffff0000}, {0, 32, .25f, 1, 0xffff0000}
   1081         };
   1082         assert(!invoke(device, 20, 3, 14u, enabled));
   1083         assert(!invoke(device, 21, 3, 14u, workspace()) && *memory(workspace()) == enabled);
   1084         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1085         memcpy(memory(input), vertices, sizeof(vertices));
   1086 
   1087         assert(!invoke(device, 5, 1));
   1088         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1089         assert(!invoke(device, 20, 3, 14u, 1u));
   1090         for (unsigned i = 0; i < 3; ++i) { vertices[i].z = .75f; vertices[i].color = 0xff0000ff; }
   1091         memcpy(memory(input), vertices, sizeof(vertices));
   1092         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1093         assert(!invoke(device, 6, 1));
   1094 
   1095         *memory(desc) = 124;
   1096         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1097         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1098         assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == (enabled ? 0xff0000 : 0x0000ff));
   1099         assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030);
   1100         assert(!invoke(target, 32, 2, 0u));
   1101     }
   1102 }
   1103 
   1104 static void disabling_depth_preserves_occlusion(uint32_t device, uint32_t target)
   1105 {
   1106     const uint32_t first_pixels[2][2] = {{0xff0000, 0xff0000}, {0x102030, 0xff0000}};
   1107     const uint32_t final_pixels[2][2] = {{0xff0000, 0x00ff00}, {0x102030, 0xff0000}};
   1108     assert(!invoke(device, 21, 3, 7u, workspace()) && *memory(workspace()) == 1);
   1109     for (unsigned enabled = 0; enabled <= 1; ++enabled) for (unsigned closer = 0; closer <= 1; ++closer) {
   1110         uint32_t input = workspace() + 512, desc = workspace() + 128;
   1111         struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1112             {0, 0, 0, 1, 0xffff0000}, {32, 0, 0, 1, 0xffff0000}, {0, 32, 0, 1, 0xffff0000}
   1113         };
   1114         assert(!invoke(device, 20, 3, 7u, enabled));
   1115         assert(!invoke(device, 21, 3, 7u, workspace()) && *memory(workspace()) == enabled);
   1116         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f000000u, 0u));
   1117         for (unsigned i = 0; i < 3; ++i) vertices[i].z = closer ? .25f : .75f;
   1118         memcpy(memory(input), vertices, sizeof(vertices));
   1119 
   1120         assert(!invoke(device, 5, 1));
   1121         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1122         assert(!invoke(device, 6, 1));
   1123 
   1124         *memory(desc) = 124;
   1125         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1126         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1127         assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == first_pixels[enabled][closer]);
   1128         assert(!invoke(target, 32, 2, 0u));
   1129 
   1130         // Restore depth testing against the buffer retained by the first draw.
   1131         assert(!invoke(device, 20, 3, 7u, 1u));
   1132         for (unsigned i = 0; i < 3; ++i) {
   1133             vertices[i].z = closer ? .375f : .625f;
   1134             vertices[i].color = 0xff00ff00;
   1135         }
   1136         memcpy(memory(input), vertices, sizeof(vertices));
   1137         assert(!invoke(device, 5, 1));
   1138         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1139         assert(!invoke(device, 6, 1));
   1140 
   1141         *memory(desc) = 124;
   1142         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1143         pixels = *memory(desc + 36); pitch = *memory(desc + 16);
   1144         assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == final_pixels[enabled][closer]);
   1145         assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030);
   1146         assert(!invoke(target, 32, 2, 0u));
   1147     }
   1148 }
   1149 
   1150 static void transformed_fans_without_diffuse_use_white(uint32_t draw, uint32_t device, uint32_t target)
   1151 {
   1152     uint32_t desc = workspace() + 128, input = workspace() + 512;
   1153     memset(memory(desc), 0, 124);
   1154     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
   1155     *memory(desc + 8) = *memory(desc + 12) = 2; *memory(desc + 104) = 0x1000;
   1156     const uint32_t format[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
   1157     memcpy(memory(desc + 72), format, 32);
   1158     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   1159     uint32_t texture = *memory(workspace());
   1160     assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
   1161     uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1162     *memory(pixels) = 0xffff0000; *memory(pixels + 4) = 0xff00ff00;
   1163     *memory(pixels + pitch) = 0xff0000ff; *memory(pixels + pitch + 4) = 0xffffffff;
   1164     assert(!invoke(texture, 32, 2, 0u));
   1165     assert(!invoke(device, 35, 3, 0u, texture));
   1166     const float positions[][4] = {{4,4,.25f,1}, {28,4,.25f,1}, {28,28,.25f,1}, {4,28,.25f,1}};
   1167     for (unsigned textures = 0; textures <= 2; ++textures) for (unsigned specular = 0; specular <= 1; ++specular) {
   1168         unsigned stride = 16 + specular * 4 + textures * 8;
   1169         for (unsigned i = 0; i < 4; ++i) {
   1170             uint32_t vertex = input + i * stride;
   1171             memcpy(memory(vertex), positions[i], 16);
   1172             if (specular) *memory(vertex + 16) = 0xff010203;
   1173             const float uv[] = {.75f, .25f};
   1174             if (textures == 2) memset(memory(vertex + 16 + specular * 4), 0, 8);
   1175             if (textures) memcpy(memory(vertex + stride - 8), uv, 8);
   1176         }
   1177         assert(!invoke(device, 35, 3, 0u, textures ? texture : 0u));
   1178         assert(!invoke(device, 37, 4, 0u, 11u, textures ? textures - 1 : 0u));
   1179         assert(!invoke(device, 20, 3, 28u, specular));
   1180         assert(!invoke(device, 20, 3, 34u, 0xff102030u));
   1181         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1182 
   1183         assert(!invoke(device, 5, 1));
   1184         assert(!invoke(device, 25, 6, 6u, 4u | textures << 8 | specular << 7, input, 4u, 0u));
   1185         assert(!invoke(device, 6, 1));
   1186 
   1187         *memory(desc) = 124;
   1188         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1189         pixels = *memory(desc + 36); pitch = *memory(desc + 16);
   1190         assert((*memory(pixels + 10 * pitch + 10 * 4) & 0xffffff) == (textures ? 0x00ff00 : 0xffffff));
   1191         assert((*memory(pixels + 23 * pitch + 23 * 4) & 0xffffff) == (textures ? 0x00ff00 : 0xffffff));
   1192         assert((*memory(pixels) & 0xffffff) == 0x102030);
   1193         assert(!invoke(target, 32, 2, 0u));
   1194     }
   1195     assert(!invoke(device, 20, 3, 28u, 0u));
   1196     assert(!invoke(device, 37, 4, 0u, 11u, 0u));
   1197     assert(!invoke(device, 35, 3, 0u, 0u));
   1198     invoke(texture, 2, 1);
   1199 }
   1200 
   1201 static void destination_color_blending_multiplies_channels(uint32_t device, uint32_t target)
   1202 {
   1203     const uint32_t multiplied[] = {0x202018, 0x401004};
   1204     assert(!invoke(device, 3, 2, workspace()));
   1205     assert((*memory(workspace() + 76) & 0x100) && (*memory(workspace() + 80) & 0x100));
   1206     for (unsigned destination = 0; destination <= 1; ++destination) for (unsigned enabled = 0; enabled <= 1; ++enabled) {
   1207         uint32_t input = workspace() + 512, desc = workspace() + 128;
   1208         struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1209             {0, 0, .25f, 1, 0xff4080c0}, {32, 0, .25f, 1, 0xff4080c0}, {0, 32, .25f, 1, 0xff4080c0}
   1210         };
   1211         assert(!invoke(device, 20, 3, 27u, enabled));
   1212         assert(!invoke(device, 20, 3, 19u, destination ? 1u : 9u));
   1213         assert(!invoke(device, 20, 3, 20u, destination ? 9u : 1u));
   1214         assert(!invoke(device, 21, 3, destination ? 20u : 19u, workspace()) && *memory(workspace()) == 9);
   1215         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff804020u, 0x3f800000u, 0u));
   1216         memcpy(memory(input), vertices, sizeof(vertices));
   1217 
   1218         assert(!invoke(device, 5, 1));
   1219         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1220         assert(!invoke(device, 6, 1));
   1221 
   1222         *memory(desc) = 124;
   1223         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1224         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1225         assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == (enabled ? multiplied[destination] : 0x4080c0));
   1226         assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x804020);
   1227         assert(!invoke(target, 32, 2, 0u));
   1228     }
   1229     assert(!invoke(device, 20, 3, 27u, 0u));
   1230     assert(!invoke(device, 20, 3, 19u, 2u));
   1231     assert(!invoke(device, 20, 3, 20u, 1u));
   1232 }
   1233 
   1234 static void depth_comparisons_control_visibility(uint32_t device, uint32_t target)
   1235 {
   1236     const unsigned visible[8][3] = {{0,0,0}, {1,0,0}, {0,1,0}, {1,1,0},
   1237                                     {0,0,1}, {1,0,1}, {0,1,1}, {1,1,1}};
   1238     const float depths[] = {.25f, .5f, .75f};
   1239     uint32_t input = workspace() + 512, desc = workspace() + 128;
   1240     struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1241         {0, 0, 0, 1, 0xff00ff00}, {32, 0, 0, 1, 0xff00ff00}, {0, 32, 0, 1, 0xff00ff00}
   1242     };
   1243     for (unsigned function = 1; function <= 8; ++function) for (unsigned z = 0; z < 3; ++z) {
   1244         assert(!invoke(device, 20, 3, 23u, function));
   1245         assert(!invoke(device, 21, 3, 23u, workspace()) && *memory(workspace()) == function);
   1246         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f000000u, 0u));
   1247         for (unsigned i = 0; i < 3; ++i) vertices[i].z = depths[z];
   1248         memcpy(memory(input), vertices, sizeof(vertices));
   1249         assert(!invoke(device, 5, 1));
   1250         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1251         assert(!invoke(device, 6, 1));
   1252 
   1253         *memory(desc) = 124;
   1254         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1255         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1256         assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) ==
   1257                (visible[function - 1][z] ? 0x00ff00 : 0x102030));
   1258         assert(!invoke(target, 32, 2, 0u));
   1259     }
   1260     assert(!invoke(device, 20, 3, 23u, 4u));
   1261 }
   1262 
   1263 static void transformed_render_states_preserve_rasterization(uint32_t device, uint32_t target)
   1264 {
   1265     uint32_t input = workspace() + 512, desc = workspace() + 128;
   1266     struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1267         {-16, -16, 0, 1, 0xff00ff00}, {48, -16, 0, 1, 0xff00ff00}, {-16, 48, 0, 1, 0xff00ff00}
   1268     };
   1269     const float depths[] = {-.25f, .5f, 1.25f};
   1270     const uint32_t ambient[] = {0xff001428, 0x12345678, 0xffffffff};
   1271     assert(!invoke(device, 20, 3, 23u, 8u));
   1272     for (unsigned lighting = 0; lighting <= 1; ++lighting)
   1273     for (unsigned clipping = 0; clipping <= 1; ++clipping) for (unsigned z = 0; z < 3; ++z) {
   1274         assert(!invoke(device, 20, 3, 136u, clipping));
   1275         assert(!invoke(device, 21, 3, 136u, workspace()) && *memory(workspace()) == clipping);
   1276         assert(!invoke(device, 20, 3, 137u, lighting));
   1277         assert(!invoke(device, 21, 3, 137u, workspace()) && *memory(workspace()) == lighting);
   1278         assert(!invoke(device, 20, 3, 139u, ambient[z]));
   1279         assert(!invoke(device, 21, 3, 139u, workspace()) && *memory(workspace()) == ambient[z]);
   1280         assert(!invoke(device, 20, 3, 143u, lighting));
   1281         assert(!invoke(device, 21, 3, 143u, workspace()) && *memory(workspace()) == lighting);
   1282         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1283         for (unsigned i = 0; i < 3; ++i) vertices[i].z = depths[z];
   1284         memcpy(memory(input), vertices, sizeof(vertices));
   1285         assert(!invoke(device, 5, 1));
   1286         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1287         assert(!invoke(device, 6, 1));
   1288 
   1289         *memory(desc) = 124;
   1290         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1291         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1292         uint32_t pixel = *memory(pixels + 6 * pitch + 6 * 4) & 0xffffff;
   1293         assert(pixel == (z == 1 ? 0x00ff00 : 0x102030));
   1294         assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030);
   1295         assert(!invoke(target, 32, 2, 0u));
   1296     }
   1297     assert(!invoke(device, 20, 3, 136u, 1u));
   1298     assert(!invoke(device, 20, 3, 139u, 0u));
   1299     assert(!invoke(device, 20, 3, 143u, 0u));
   1300     assert(!invoke(device, 20, 3, 23u, 4u));
   1301 }
   1302 
   1303 static void viewport_limits_clearing_and_drawing(uint32_t device, uint32_t target, FILE *file)
   1304 {
   1305     uint32_t view = workspace() + 64, desc = workspace() + 128, input = workspace() + 512;
   1306     uint32_t rectangle = workspace() + 96;
   1307     const uint32_t full[] = {0, 0, 32, 32, 0, 0x3f800000};
   1308     const uint32_t regions[][6] = {{0, 0, 1, 1, 0, 0x3f800000}, {7, 9, 12, 10, 0, 0x3f800000}};
   1309     struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1310         {0, 0, .5f, 1, 0xffffffff}, {32, 0, .5f, 1, 0xffffffff}, {0, 32, .5f, 1, 0xffffffff}
   1311     };
   1312     memcpy(memory(input), vertices, sizeof(vertices));
   1313     for (unsigned scenario = 0; scenario < 6; ++scenario) {
   1314         memcpy(memory(view), full, sizeof(full));
   1315         assert(!invoke(device, 13, 2, view));
   1316         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1317         memcpy(memory(view), regions[scenario / 3], 24);
   1318         const int32_t clear[] = {0, 0, 32, 32};
   1319         memcpy(memory(rectangle), clear, sizeof(clear));
   1320 
   1321         assert(!invoke(device, 13, 2, view));
   1322         memset(memory(desc), 0xa5, 28);
   1323         assert(!invoke(device, 15, 2, desc));
   1324         assert(!memcmp(memory(desc), regions[scenario / 3], 24) && *memory(desc + 24) == 0xa5a5a5a5);
   1325         assert(!invoke(device, 10, 7, scenario % 3 == 2 ? 1u : 0u,
   1326                        scenario % 3 == 2 ? rectangle : 0u, 3u, 0xff335577u, 0x3f800000u, 0u));
   1327         if (scenario % 3 == 1) {
   1328             assert(!invoke(device, 5, 1));
   1329             assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1330             assert(!invoke(device, 6, 1));
   1331         }
   1332         *memory(desc) = 124;
   1333         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1334 
   1335         uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1336         assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030);
   1337         unsigned char rgb[32 * 32 * 3];
   1338         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   1339             uint32_t pixel = *memory(pixels + y * pitch + x * 4);
   1340             rgb[(y * 32 + x) * 3] = pixel >> 16;
   1341             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   1342             rgb[(y * 32 + x) * 3 + 2] = pixel;
   1343         }
   1344         assert(!invoke(target, 32, 2, 0u));
   1345         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   1346     }
   1347     memcpy(memory(view), full, sizeof(full));
   1348     assert(!invoke(device, 13, 2, view));
   1349     *memory(view + 8) = 33;
   1350     assert(invoke(device, 13, 2, view) == 0x80070057u);
   1351     assert(invoke(device, 13, 2, 0u) == 0x80070057u);
   1352     assert(!invoke(device, 15, 2, desc));
   1353     assert(!memcmp(memory(desc), full, sizeof(full)));
   1354 }
   1355 
   1356 static void material_state_is_copied(uint32_t device)
   1357 {
   1358     uint32_t input = workspace() + 128, output = workspace() + 256;
   1359     memset(memory(output), 0xa5, 72);
   1360     assert(!invoke(device, 17, 2, output));
   1361     for (unsigned i = 0; i < 17; ++i) assert(!*memory(output + i * 4));
   1362     const float material[] = {.2f, .3f, .4f, .5f, .6f, .7f, .8f, .9f,
   1363                               1, .1f, .2f, .3f, .4f, .5f, .6f, .7f, 16};
   1364     memcpy(memory(input), material, sizeof(material));
   1365 
   1366     assert(!invoke(device, 16, 2, input));
   1367     memset(memory(input), 0, sizeof(material));
   1368     assert(!invoke(device, 17, 2, output));
   1369 
   1370     assert(!memcmp(memory(output), material, sizeof(material)));
   1371     assert(*memory(output + 68) == 0xa5a5a5a5);
   1372     assert(invoke(device, 16, 2, 0u) == 0x80070057u);
   1373     assert(invoke(device, 17, 2, 0u) == 0x80070057u);
   1374 }
   1375 
   1376 static void vertex_buffer_identities_survive_other_releases(uint32_t d3d)
   1377 {
   1378     uint32_t buffers[8193];
   1379     uint32_t desc = workspace() + 128;
   1380     uint32_t values[] = {16, 0x800, 0x44, 1};
   1381     memcpy(memory(desc), values, 16);
   1382     for (unsigned i = 0; i < 8193; ++i) {
   1383         assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
   1384         buffers[i] = *memory(workspace());
   1385         assert(!invoke(buffers[i], 3, 4, 1u, workspace(), 0u));
   1386         *memory(*memory(workspace())) = buffers[i];
   1387         assert(!invoke(buffers[i], 4, 1));
   1388     }
   1389 
   1390     for (unsigned i = 0; i < 8193; i += 2)
   1391         assert(!invoke(buffers[i], 2, 1));
   1392 
   1393     for (unsigned i = 1; i < 8193; i += 2) {
   1394         assert(!invoke(buffers[i], 3, 4, 1u, workspace(), workspace() + 4));
   1395         assert(*memory(workspace() + 4) == 20);
   1396         assert(*memory(*memory(workspace())) == buffers[i]);
   1397         assert(!invoke(buffers[i], 4, 1));
   1398         assert(!invoke(buffers[i], 2, 1));
   1399     }
   1400 }
   1401 
   1402 static void vertex_buffers_preserve_data_and_description(uint32_t d3d)
   1403 {
   1404     const uint32_t formats[] = {0x112, 0x1c4, 0x44, 0x242, 0x00030112};
   1405     const uint32_t strides[] = {32, 32, 20, 32, 28};
   1406     const uint32_t buffer_iid[] = {0xf5049e7d, 0x11d24861, 0xa00007a4, 0xa82906c9};
   1407     const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
   1408     for (unsigned i = 0; i < 5; ++i) {
   1409         uint32_t desc = workspace() + 128;
   1410         uint32_t values[] = {16, 0x800, formats[i], 17};
   1411         memcpy(memory(desc), values, 16);
   1412         assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
   1413         uint32_t buffer = *memory(workspace());
   1414         assert(buffer);
   1415         assert(!invoke(buffer, 4, 1));
   1416 
   1417         assert(!invoke(buffer, 3, 4, 1u, workspace(), workspace() + 4));
   1418         uint32_t data = *memory(workspace()), size = *memory(workspace() + 4);
   1419         assert(size == strides[i] * 17);
   1420         assert(!invoke(buffer, 3, 4, 1u, workspace(), 0u));
   1421         assert(*memory(workspace()) == data);
   1422         for (unsigned n = 0; n < size; ++n) ((unsigned char *)memory(data))[n] = (n * 37) & 255;
   1423         assert(!invoke(buffer, 4, 1));
   1424         assert(!invoke(buffer, 3, 4, 0x11u, workspace(), 0u));
   1425         data = *memory(workspace());
   1426 
   1427         for (unsigned n = 0; n < size; ++n) assert(((unsigned char *)memory(data))[n] == ((n * 37) & 255));
   1428         assert(!invoke(buffer, 4, 1));
   1429         assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u));
   1430         data = *memory(workspace());
   1431         ((unsigned char *)memory(data))[17] = 0x5a;
   1432         assert(!invoke(buffer, 4, 1));
   1433         assert(!invoke(buffer, 3, 4, 0x11u, workspace(), 0u));
   1434         data = *memory(workspace());
   1435         for (unsigned n = 0; n < size; ++n)
   1436             assert(((unsigned char *)memory(data))[n] == (n == 17 ? 0x5a : ((n * 37) & 255)));
   1437         assert(!invoke(buffer, 4, 1));
   1438         assert(!invoke(buffer, 4, 1));
   1439         memset(memory(desc), 0xa5, 20);
   1440         *memory(desc) = 0;
   1441         assert(!invoke(buffer, 6, 2, desc));
   1442         values[0] = 0; /* The driver preserves the caller's size field. */
   1443         assert(!memcmp(memory(desc), values, 16) && *memory(desc + 16) == 0xa5a5a5a5);
   1444         assert(query(buffer, buffer_iid) == buffer);
   1445         assert(query(buffer, unknown) == buffer);
   1446         assert(invoke(buffer, 2, 1) == 2);
   1447         assert(invoke(buffer, 2, 1) == 1);
   1448         assert(invoke(buffer, 1, 1) == 2);
   1449         assert(invoke(buffer, 2, 1) == 1);
   1450         assert(!invoke(buffer, 2, 1));
   1451     }
   1452 }
   1453 
   1454 static void vertex_buffer_optimization_preserves_data(uint32_t d3d, uint32_t device)
   1455 {
   1456     uint32_t desc = workspace() + 128;
   1457     const uint32_t description[] = {16, 0x800, 0x44, 1};
   1458     const uint32_t vertex[] = {0x40800000, 0x40800000, 0x3e800000, 0x3f800000, 0xff5a96d2};
   1459     memcpy(memory(desc), description, 16);
   1460     assert(!invoke(d3d, 5, 4, desc, workspace(), 0u));
   1461     uint32_t buffer = *memory(workspace());
   1462     assert(!invoke(buffer, 3, 4, 1u, workspace(), 0u));
   1463     memcpy(memory(*memory(workspace())), vertex, sizeof(vertex));
   1464     assert(!invoke(buffer, 4, 1));
   1465 
   1466     assert(!invoke(buffer, 7, 3, device, 0u));
   1467     assert(!invoke(buffer, 7, 3, device, 0u));
   1468 
   1469     assert(!invoke(buffer, 6, 2, desc));
   1470     assert(*memory(desc) == 16);
   1471     assert(*memory(desc + 4) == 0x80000800u);
   1472     assert(*memory(desc + 8) == 0x44);
   1473     assert(*memory(desc + 12) == 1);
   1474     assert(!invoke(buffer, 3, 4, 0x11u, workspace(), workspace() + 4));
   1475     assert(*memory(workspace() + 4) == 20);
   1476     assert(!memcmp(memory(*memory(workspace())), vertex, sizeof(vertex)));
   1477     assert(!invoke(buffer, 4, 1));
   1478     assert(!invoke(buffer, 2, 1));
   1479 }
   1480 
   1481 static void light_definitions_and_enable_state_are_owned(uint32_t device)
   1482 {
   1483     uint32_t input = workspace() + 512, output = workspace() + 768;
   1484     const uint32_t default_light[] = {3, 0x3f800000, 0x3f800000, 0x3f800000,
   1485         0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0x3f800000, 0,0,0,0,0,0,0};
   1486     memset(memory(output), 0xa5, 108);
   1487     assert(invoke(device, 19, 3, 12345u, output) == 0x80070057u);
   1488     assert(invoke(device, 45, 3, 12345u, output) == 0x80070057u);
   1489     assert(*memory(output) == 0xa5a5a5a5);
   1490 
   1491     assert(!invoke(device, 44, 3, 12345u, 0u));
   1492     assert(!invoke(device, 19, 3, 12345u, output));
   1493     assert(!memcmp(memory(output), default_light, 104));
   1494     assert(!invoke(device, 45, 3, 12345u, output) && !*memory(output));
   1495     assert(!invoke(device, 44, 3, 12345u, 17u));
   1496     assert(!invoke(device, 45, 3, 12345u, output) && *memory(output) == 128);
   1497     for (unsigned type = 1; type <= 3; ++type) {
   1498         memcpy(memory(input), default_light, 104);
   1499         *memory(input) = type;
   1500         *memory(input + 64) = 0x3f800000; /* Unnormalized direction is retained. */
   1501         *memory(input + 76) = 0x42c80000; /* Range 100. */
   1502         *memory(input + 84) = 0x3f800000; /* Constant attenuation 1. */
   1503         *memory(input + 96) = 0x3f000000; /* Theta 0.5. */
   1504         *memory(input + 100) = 0x3f800000; /* Phi 1. */
   1505         assert(!invoke(device, 18, 3, type, input));
   1506         memset(memory(output), 0xcc, 108);
   1507 
   1508         assert(!invoke(device, 19, 3, type, output));
   1509         assert(!memcmp(memory(output), memory(input), 104));
   1510         assert(*memory(output + 104) == 0xcccccccc);
   1511         assert(!invoke(device, 45, 3, type, output) && !*memory(output));
   1512         assert(!invoke(device, 44, 3, type, 1u));
   1513         assert(!invoke(device, 18, 3, type, input));
   1514         memset(memory(input), 0, 104);
   1515         assert(!invoke(device, 45, 3, type, output) && *memory(output) == 128);
   1516         assert(!invoke(device, 44, 3, type, 0u));
   1517     }
   1518     assert(!invoke(device, 44, 3, 12345u, 0u));
   1519     assert(invoke(device, 18, 3, 99u, input) == 0x80070057u);
   1520     *memory(input) = 3;
   1521     assert(invoke(device, 18, 3, 99u, input) == 0x80070057u);
   1522     assert(invoke(device, 19, 3, 99u, output) == 0x80070057u);
   1523 }
   1524 
   1525 static void transforms_copy_geometry_and_texture_matrices(uint32_t device)
   1526 {
   1527     const float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
   1528     const float matrices[][16] = {
   1529         {2,0,0,0, 0,3,0,0, 0,0,4,0, 5,6,7,1},
   1530         {0,1,0,0, -1,0,0,0, 0,0,1,0, -2,3,-4,1},
   1531         {1.5f,0,0,0, 0,2,0,0, 0,0,1,1, 0,0,-.5f,0},
   1532         {2,0,0,0, 0,3,0,0, .25f,.5f,1,0, 0,0,0,1},
   1533         {0,1,0,0, -1,0,0,0, .75f,.25f,1,0, 0,0,0,1},
   1534     };
   1535     const unsigned states[] = {1, 2, 3, 16, 17};
   1536     uint32_t input = workspace() + 512, output = workspace() + 768;
   1537     for (unsigned index = 0; index < 5; ++index) {
   1538         assert(!invoke(device, 12, 3, states[index], output));
   1539         assert(!memcmp(memory(output), identity, 64));
   1540     }
   1541 
   1542     for (unsigned index = 0; index < 5; ++index) {
   1543         memcpy(memory(input), matrices[index], 64);
   1544         assert(!invoke(device, 11, 3, states[index], input));
   1545         memset(memory(input), 0xa5, 64);
   1546     }
   1547 
   1548     for (unsigned index = 0; index < 5; ++index) {
   1549         memset(memory(output), 0xcc, 68);
   1550         assert(!invoke(device, 12, 3, states[index], output));
   1551         assert(!memcmp(memory(output), matrices[index], 64));
   1552         assert(*memory(output + 64) == 0xcccccccc);
   1553         memcpy(memory(input), identity, 64);
   1554         assert(!invoke(device, 11, 3, states[index], input));
   1555     }
   1556 }
   1557 
   1558 static void depth_bias_offsets_overlapping_geometry(uint32_t device, uint32_t target, unsigned bits, FILE *file)
   1559 {
   1560     uint32_t desc = workspace() + 128, input = workspace() + 512;
   1561     assert(!invoke(device, 35, 3, 0u, 0u));
   1562     assert(!invoke(device, 20, 3, 137u, 0u));
   1563     assert(!invoke(device, 20, 3, 22u, 1u));
   1564     assert(!invoke(device, 20, 3, 14u, 1u));
   1565     assert(!invoke(device, 20, 3, 23u, 2u));
   1566     const uint32_t biases[] = {0, 1, 2, 16};
   1567     for (unsigned slope = 0; slope < 5; ++slope)
   1568     for (unsigned bias = 0; bias < 4; ++bias)
   1569     for (unsigned distance = 0; distance < 12; ++distance) {
   1570         struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1571             {0,0,.5f,1,0xffff0000}, {32,0,.5f,1,0xffff0000},
   1572             {0,32,.5f,1,0xffff0000},
   1573         };
   1574         const float depths[] = {.5f, .125f, .875f, .5f, .5f};
   1575         const float slopes[] = {0, 0, 0, .03125f, -.25f};
   1576         for (unsigned i = 0; i < 3; ++i) vertices[i].z = depths[slope] + (i == 1 ? slopes[slope] : 0);
   1577         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1578         assert(!invoke(device, 20, 3, 47u, 0u));
   1579         memcpy(memory(input), vertices, sizeof(vertices));
   1580         assert(!invoke(device, 5, 1));
   1581         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1582         const float offsets[] = {0, .5f, 1, 1.5f, 2, 8, 16, 32, 128, 256, 512, 1024};
   1583         for (unsigned i = 0; i < 3; ++i) {
   1584             vertices[i].z += offsets[distance] / (bits == 16 ? 65535 : 16777215);
   1585             vertices[i].color = 0xff0000ff;
   1586         }
   1587         memcpy(memory(input), vertices, sizeof(vertices));
   1588 
   1589         assert(!invoke(device, 20, 3, 47u, biases[bias]));
   1590         assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1591         assert(!invoke(device, 6, 1));
   1592 
   1593         assert(!invoke(device, 21, 3, 47u, workspace()) && *memory(workspace()) == biases[bias]);
   1594         *memory(desc) = 124;
   1595         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1596         unsigned char rgb[32 * 32 * 3];
   1597         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   1598             uint32_t pixel = *memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4);
   1599             rgb[(y * 32 + x) * 3] = pixel >> 16;
   1600             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   1601             rgb[(y * 32 + x) * 3 + 2] = pixel;
   1602         }
   1603         assert(!invoke(target, 32, 2, 0u));
   1604         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   1605     }
   1606     assert(!invoke(device, 20, 3, 47u, 0u));
   1607     assert(!invoke(device, 20, 3, 23u, 4u));
   1608 }
   1609 
   1610 static void fourfold_modulation_clamps_before_the_next_stage(uint32_t device, uint32_t target)
   1611 {
   1612     uint32_t desc = workspace() + 128, input = workspace() + 512;
   1613     // Keep nonsaturated channels away from eight-bit rounding boundaries.
   1614     struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   1615         {0,0,.25f,1,0xc0c02020}, {32,0,.25f,1,0xc0c02020}, {0,32,.25f,1,0xc0c02020}
   1616     };
   1617     memcpy(memory(input), vertices, sizeof(vertices));
   1618     assert(!invoke(device, 20, 3, 137u, 0u));
   1619     assert(!invoke(device, 20, 3, 29u, 0u));
   1620     assert(!invoke(device, 20, 3, 28u, 0u));
   1621     assert(!invoke(device, 20, 3, 27u, 0u));
   1622     assert(!invoke(device, 20, 3, 15u, 0u));
   1623     assert(!invoke(device, 20, 3, 22u, 1u));
   1624     assert(!invoke(device, 20, 3, 23u, 8u));
   1625     assert(!invoke(device, 20, 3, 60u, 0x80808080u));
   1626     for (unsigned stage = 0; stage < 2; ++stage) {
   1627         assert(!invoke(device, 35, 3, stage, 0u));
   1628         assert(!invoke(device, 37, 4, stage, 2u, stage ? 1u : 3u));
   1629         assert(!invoke(device, 37, 4, stage, 3u, stage ? 3u : 0u));
   1630         assert(!invoke(device, 37, 4, stage, 4u, 2u));
   1631         assert(!invoke(device, 37, 4, stage, 5u, stage ? 1u : 0u));
   1632     }
   1633 
   1634     assert(!invoke(device, 37, 4, 0u, 1u, 6u));
   1635     assert(!invoke(device, 37, 4, 1u, 1u, 4u));
   1636     assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1637     assert(!invoke(device, 5, 1));
   1638     assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1639     assert(!invoke(device, 6, 1));
   1640 
   1641     assert(!invoke(device, 36, 4, 0u, 1u, workspace()) && *memory(workspace()) == 6);
   1642     *memory(desc) = 124;
   1643     assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1644     uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1645     assert((*memory(pixels + 8 * pitch + 8 * 4) & 0xffffff) == 0x802020);
   1646     assert(!invoke(target, 32, 2, 0u));
   1647 
   1648     for (unsigned i = 0; i < 3; ++i) vertices[i].color = 0xc0c08020;
   1649     memcpy(memory(input), vertices, sizeof(vertices));
   1650     assert(!invoke(device, 37, 4, 0u, 4u, 6u));
   1651     assert(!invoke(device, 37, 4, 0u, 5u, 3u));
   1652     assert(!invoke(device, 37, 4, 0u, 6u, 0u));
   1653     assert(!invoke(device, 37, 4, 1u, 4u, 4u));
   1654     assert(!invoke(device, 37, 4, 1u, 6u, 3u));
   1655     assert(!invoke(device, 20, 3, 27u, 1u));
   1656     assert(!invoke(device, 20, 3, 19u, 5u));
   1657     assert(!invoke(device, 20, 3, 20u, 6u));
   1658 
   1659     assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff000000u, 0x3f800000u, 0u));
   1660     assert(!invoke(device, 5, 1));
   1661     assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   1662     assert(!invoke(device, 6, 1));
   1663 
   1664     assert(!invoke(device, 36, 4, 0u, 4u, workspace()) && *memory(workspace()) == 6);
   1665     *memory(desc) = 124;
   1666     assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1667     pixels = *memory(desc + 36); pitch = *memory(desc + 16);
   1668     assert((*memory(pixels + 8 * pitch + 8 * 4) & 0xffffff) == 0x404010);
   1669     assert(!invoke(target, 32, 2, 0u));
   1670     assert(!invoke(device, 37, 4, 1u, 1u, 1u));
   1671     assert(!invoke(device, 37, 4, 0u, 1u, 4u));
   1672     assert(!invoke(device, 20, 3, 23u, 4u));
   1673 }
   1674 
   1675 static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
   1676 {
   1677     uint32_t desc = workspace() + 128, input = workspace() + 512, textures[2];
   1678     const uint32_t texels[] = {0x40802040, 0x80c08020, 0xc02040c0, 0xff60a080};
   1679     for (unsigned i = 0; i < 2; ++i) {
   1680         memset(memory(desc), 0, 124);
   1681         *memory(desc) = 124; *memory(desc + 4) = 0x1007;
   1682         *memory(desc + 8) = 2; *memory(desc + 12) = 2; *memory(desc + 104) = 0x1000;
   1683         const uint32_t format[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
   1684         memcpy(memory(desc + 72), format, 32);
   1685         assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   1686         textures[i] = *memory(workspace());
   1687         assert(!invoke(textures[i], 25, 5, 0u, desc, 0x21u, 0u));
   1688         for (unsigned y = 0; y < 2; ++y) for (unsigned x = 0; x < 2; ++x)
   1689             *memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4) = texels[(y * 2 + x + i) % 4];
   1690         assert(!invoke(textures[i], 32, 2, 0u));
   1691     }
   1692     assert(!invoke(device, 20, 3, 137u, 0u));
   1693     assert(!invoke(device, 20, 3, 28u, 0u));
   1694     assert(!invoke(device, 20, 3, 22u, 1u));
   1695     assert(!invoke(device, 20, 3, 27u, 1u));
   1696     assert(!invoke(device, 20, 3, 19u, 5u));
   1697     assert(!invoke(device, 20, 3, 20u, 6u));
   1698     for (unsigned replicate = 0; replicate < 4; ++replicate)
   1699     for (unsigned sample = 0; sample < 640; ++sample) {
   1700         struct vertex { float x, y, z, rhw; uint32_t color; float u, v, u1, v1; } vertices[] = {
   1701             {0,0,.25f,1,0x4080c0a0, -.25f,.125f, .875f,-.375f},
   1702             {32,0,.25f,.5f,0x8080c0a0, 1.75f,.125f, -.125f,-.375f},
   1703             {0,32,.25f,2,0xc080c0a0, -.25f,1.875f, .875f,1.625f},
   1704         };
   1705         if (sample >= 320) {
   1706             const float world[] = {.75f,0,0,0, 0,.875f,0,0, 0,0,1,0, .125f,0,0,1};
   1707             const float view[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,-.25f,0,1};
   1708             memcpy(memory(workspace() + 1024), world, 64);
   1709             assert(!invoke(device, 11, 3, 1u, workspace() + 1024));
   1710             memcpy(memory(workspace() + 1024), view, 64);
   1711             assert(!invoke(device, 11, 3, 2u, workspace() + 1024));
   1712         }
   1713         uint32_t factor = sample & 8 ? 0x804060c0u : 0x20c08040u;
   1714         assert(!invoke(device, 20, 3, 60u, factor));
   1715         assert(!invoke(device, 21, 3, 60u, workspace()) && *memory(workspace()) == factor);
   1716         memcpy(memory(input), vertices, sizeof(vertices));
   1717         if ((sample & 32) && sample < 512) for (unsigned i = 0; i < 3; ++i) {
   1718             float xyz[] = {vertices[i].x / 16 - 1, 1 - vertices[i].y / 16, .25f};
   1719             memcpy(memory(input + i * 32), xyz, 12);
   1720             memcpy(memory(input + i * 32 + 12), &vertices[i].color, 20);
   1721         }
   1722         assert(!invoke(device, 20, 3, 29u, sample >= 512 && sample < 576 ? 1u : 0u));
   1723         if (sample >= 512) {
   1724             const uint32_t specular[] = {0x00102040, 0x80307050, 0xff602030};
   1725             for (unsigned i = 0; i < 3; ++i) {
   1726                 memcpy(memory(input + i * 40), &vertices[i], 20);
   1727                 *memory(input + i * 40 + 20) = specular[i];
   1728                 memcpy(memory(input + i * 40 + 24), &vertices[i].u, 16);
   1729             }
   1730             assert(!invoke(device, 20, 3, 28u, sample & 4 ? 1u : 0u));
   1731             assert(!invoke(device, 20, 3, 34u, 0x00406080u));
   1732         }
   1733         const uint32_t arguments[] = {0, 1, 2, 0, 3, 2, 2};
   1734         assert(!invoke(device, 20, 3, 143u, sample >= 576 && (sample & 8) ? 1u : 0u));
   1735         if (sample >= 576) for (unsigned i = 0; i < 3; ++i) {
   1736             const float normals[][3] = {{.25f,.5f,-1}, {.75f,.25f,-.5f}, {-.5f,.75f,.25f}};
   1737             float xyz[] = {vertices[i].x / 16 - 1, 1 - vertices[i].y / 16, .25f};
   1738             memcpy(memory(input + i * 40), xyz, 12);
   1739             memcpy(memory(input + i * 40 + 12), normals[i], 12);
   1740             memcpy(memory(input + i * 40 + 24), &vertices[i].u, 16);
   1741         }
   1742         unsigned first_argument = sample >= 448 ? (sample >> 2) & 3 : arguments[sample / 64];
   1743         if (replicate & 1) first_argument |= 0x20;
   1744         unsigned second_argument = sample >= 256 ? sample & 3 : sample & 16 ? 0 : 1;
   1745         if (replicate & 2) second_argument |= 0x20;
   1746         for (unsigned stage = 0; stage < 2; ++stage) {
   1747             uint32_t texture = sample >= 192 && sample < 256 ? 0 : textures[sample & 16 ? 0 : stage];
   1748             assert(!invoke(device, 35, 3, stage, texture));
   1749             assert(!invoke(device, 34, 3, stage, workspace()) && *memory(workspace()) == texture);
   1750             if (texture) assert(invoke(texture, 2, 1) > 0);
   1751             unsigned coordinate = sample >= 320 && (stage || (sample & 16)) ? (sample >= 576 ? 0x10000 : 0x20000) : stage;
   1752             assert(!invoke(device, 37, 4, stage, 11u, coordinate));
   1753             assert(!invoke(device, 36, 4, stage, 11u, workspace()) && *memory(workspace()) == coordinate);
   1754             assert(!invoke(device, 37, 4, stage, 24u, sample >= 384 ? 3u : sample >= 320 ? (sample & 4 ? 2u : 0u) : sample & 32 ? 2u : 0u));
   1755             const float matrix[] = {2,0,0,0, 0,1,0,0, .125f,.375f,1,0, 0,0,0,1};
   1756             memcpy(memory(workspace() + 1024), matrix, 64);
   1757             assert(!invoke(device, 11, 3, 16u + stage, workspace() + 1024));
   1758             assert(!invoke(device, 37, 4, stage, 18u, 1u));
   1759             assert(!invoke(device, 37, 4, stage, 16u, stage + 1));
   1760             assert(!invoke(device, 37, 4, stage, 17u, stage + 1));
   1761             assert(!invoke(device, 37, 4, stage, 12u, stage ? 3u : 1u));
   1762             assert(!invoke(device, 37, 4, stage, 13u, stage ? 3u : 1u));
   1763             assert(!invoke(device, 37, 4, stage, 14u, stage ? 3u : 1u));
   1764             assert(!invoke(device, 37, 4, stage, 2u, first_argument));
   1765             assert(!invoke(device, 37, 4, stage, 5u, sample & 16 ? (first_argument + 1) % 4 : first_argument));
   1766             assert(!invoke(device, 36, 4, stage, 2u, workspace()) && *memory(workspace()) == first_argument);
   1767             const uint32_t operations[] = {4, 7, 2, 1};
   1768             assert(!invoke(device, 37, 4, stage, 1u, sample >= 448 && (stage || (sample & 16)) ? 20u : stage ? operations[(sample >> 2) & 3] : 4u));
   1769             assert(!invoke(device, 37, 4, stage, 4u, stage ? (sample & 3) + 1 : 4u));
   1770             assert(!invoke(device, 37, 4, stage, 3u, second_argument));
   1771             assert(!invoke(device, 36, 4, stage, 3u, workspace()) && *memory(workspace()) == second_argument);
   1772             assert(!invoke(device, 37, 4, stage, 6u, (sample >= 256 ? 3u : sample & 16 ? 0u : 1u) | (replicate & 2 ? 0x20u : 0u)));
   1773         }
   1774         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1775 
   1776         assert(!invoke(device, 5, 1));
   1777         *memory(workspace()) = 0xa5a5a5a5;
   1778         *memory(workspace() + 4) = 0xcccccccc;
   1779         assert(!invoke(device, 38, 2, workspace()));
   1780         assert(*memory(workspace()) == 1 && *memory(workspace() + 4) == 0xcccccccc);
   1781         assert(!invoke(device, 25, 6, 4u, sample >= 576 ? 0x212u : sample >= 512 ? 0x2c4u : sample & 32 ? 0x242u : 0x244u, input, 3u, 0u));
   1782         assert(!invoke(device, 6, 1));
   1783 
   1784         *memory(desc) = 124;
   1785         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1786         unsigned char rgb[32 * 32 * 3];
   1787         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   1788             uint32_t pixel = *memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4);
   1789             rgb[(y * 32 + x) * 3] = pixel >> 16;
   1790             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   1791             rgb[(y * 32 + x) * 3 + 2] = pixel;
   1792         }
   1793         assert(!invoke(target, 32, 2, 0u));
   1794         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   1795     }
   1796     assert(!invoke(device, 35, 3, 0u, textures[0]));
   1797     assert(!invoke(device, 35, 3, 1u, textures[0]));
   1798     assert(!invoke(device, 35, 3, 0u, 0u));
   1799     assert(invoke(textures[0], 2, 1) == 1);
   1800     assert(!invoke(device, 35, 3, 1u, 0u));
   1801     assert(!invoke(textures[1], 2, 1));
   1802 }
   1803 
   1804 static void alpha_tests_preserve_rejected_color_and_depth(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
   1805 {
   1806     uint32_t desc = workspace() + 128, input = workspace() + 512;
   1807     memset(memory(desc), 0, 124);
   1808     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
   1809     *memory(desc + 8) = 1; *memory(desc + 12) = 4; *memory(desc + 104) = 0x1000;
   1810     const uint32_t format[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
   1811     memcpy(memory(desc + 72), format, 32);
   1812     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   1813     uint32_t texture = *memory(workspace());
   1814     assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
   1815     const uint32_t texels[] = {0x00ffffff, 0x7fffffff, 0x80ffffff, 0xffffffff};
   1816     memcpy(memory(*memory(desc + 36)), texels, 16);
   1817     assert(!invoke(texture, 32, 2, 0u));
   1818     assert(!invoke(device, 20, 3, 22u, 1u));
   1819     assert(!invoke(device, 20, 3, 14u, 1u));
   1820     assert(!invoke(device, 20, 3, 23u, 4u));
   1821     assert(!invoke(device, 20, 3, 27u, 0u));
   1822     assert(!invoke(device, 20, 3, 28u, 0u));
   1823     assert(!invoke(device, 20, 3, 29u, 0u));
   1824     assert(!invoke(device, 37, 4, 1u, 1u, 1u));
   1825     assert(!invoke(device, 37, 4, 0u, 1u, 2u));
   1826     assert(!invoke(device, 37, 4, 0u, 2u, 0u));
   1827     assert(!invoke(device, 37, 4, 0u, 4u, 2u));
   1828     assert(!invoke(device, 37, 4, 0u, 11u, 0u));
   1829     assert(!invoke(device, 37, 4, 0u, 13u, 3u));
   1830     assert(!invoke(device, 37, 4, 0u, 14u, 3u));
   1831     const uint32_t references[] = {0,1,7,8,15,16,31,32,63,64,95,96,127,128,159,160,191,192,223,224,239,240,247,248,254,255};
   1832     for (unsigned mode = 0; mode < 4; ++mode) for (unsigned enabled = 0; enabled < 2; ++enabled)
   1833     for (unsigned function = 1; function <= 8; ++function) for (unsigned ref = 0; ref < 26; ++ref) {
   1834         struct vertex { float x, y, z, rhw; uint32_t color; float u, v; } vertices[] = {
   1835             {0,0,.25f,1,0x80ff0000,0,.5f}, {32,0,.25f,1,0x80ff0000,1,.5f},
   1836             {32,32,.25f,1,0x80ff0000,1,.5f}, {0,32,.25f,1,0x80ff0000,0,.5f},
   1837         };
   1838         if (mode == 1) {
   1839             vertices[0].color = vertices[3].color = 0x00ff0000;
   1840             vertices[1].color = vertices[2].color = 0xffff0000;
   1841         }
   1842         assert(!invoke(device, 35, 3, 0u, mode >= 2 ? texture : 0u));
   1843         assert(!invoke(device, 37, 4, 0u, 5u, mode >= 2 ? 2u : 0u));
   1844         assert(!invoke(device, 37, 4, 0u, 16u, mode == 3 ? 2u : 1u));
   1845         assert(!invoke(device, 37, 4, 0u, 17u, mode == 3 ? 2u : 1u));
   1846         assert(!invoke(device, 20, 3, 15u, enabled));
   1847         assert(!invoke(device, 20, 3, 24u, references[ref]));
   1848         assert(!invoke(device, 20, 3, 25u, function));
   1849         assert(!invoke(device, 21, 3, 15u, workspace()) && *memory(workspace()) == enabled);
   1850         assert(!invoke(device, 21, 3, 24u, workspace()) && *memory(workspace()) == references[ref]);
   1851         assert(!invoke(device, 21, 3, 25u, workspace()) && *memory(workspace()) == function);
   1852         assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   1853 
   1854         memcpy(memory(input), vertices, sizeof(vertices));
   1855         assert(!invoke(device, 5, 1));
   1856         assert(!invoke(device, 25, 6, 6u, 0x144u, input, 4u, 0u));
   1857         assert(!invoke(device, 20, 3, 15u, 0u));
   1858         for (unsigned i = 0; i < 4; ++i) { vertices[i].z = .75f; vertices[i].color = 0xff0000ff; }
   1859         memcpy(memory(input), vertices, sizeof(vertices));
   1860         assert(!invoke(device, 25, 6, 6u, 0x144u, input, 4u, 0u));
   1861         assert(!invoke(device, 6, 1));
   1862 
   1863         *memory(desc) = 124;
   1864         assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
   1865         unsigned char rgb[32 * 32 * 3];
   1866         for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   1867             uint32_t pixel = *memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4);
   1868             if (!enabled || function == 8) assert((pixel & 0xffffff) == 0xff0000);
   1869             if (enabled && function == 1) assert((pixel & 0xffffff) == 0x0000ff);
   1870             /* Filter precision can put exact half-byte alpha on either side of a cutoff.
   1871              * Check either visibility result only at those 1152 boundary pixels. */
   1872             if (mode == 3 && enabled && function >= 2 && function <= 7 &&
   1873                 ((x == 8 && (references[ref] == 63 || references[ref] == 64)) ||
   1874                  (x == 16 && (references[ref] == 127 || references[ref] == 128)) ||
   1875                  (x == 24 && (references[ref] == 191 || references[ref] == 192)))) {
   1876                 assert((pixel & 0xffffff) == 0xff0000 || (pixel & 0xffffff) == 0x0000ff);
   1877                 pixel = 0;
   1878             }
   1879             rgb[(y * 32 + x) * 3] = pixel >> 16;
   1880             rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   1881             rgb[(y * 32 + x) * 3 + 2] = pixel;
   1882         }
   1883         assert(!invoke(target, 32, 2, 0u));
   1884         assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   1885     }
   1886     assert(!invoke(device, 35, 3, 0u, 0u));
   1887     assert(!invoke(texture, 2, 1));
   1888     assert(!invoke(device, 20, 3, 24u, 0u));
   1889     assert(!invoke(device, 20, 3, 25u, 8u));
   1890 }
   1891 
   1892 static void changing_render_targets_preserves_viewport_and_ownership(uint32_t draw, uint32_t device, uint32_t original)
   1893 {
   1894     uint32_t desc = workspace() + 128, viewport = workspace() + 256;
   1895     const uint32_t initial_viewport[] = {0, 0, 32, 32, 0, 0x3f800000};
   1896     memset(memory(desc), 0, 124);
   1897     *memory(desc) = 124; *memory(desc + 4) = 7;
   1898     *memory(desc + 8) = 48; *memory(desc + 12) = 64; *memory(desc + 104) = 0x6040;
   1899     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   1900     uint32_t replacement = *memory(workspace());
   1901     *memory(desc + 4) = 0x1007; *memory(desc + 104) = 0x24000;
   1902     const uint32_t format[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0};
   1903     memcpy(memory(desc + 72), format, 32);
   1904     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   1905     uint32_t depth = *memory(workspace());
   1906     assert(!invoke(replacement, 3, 2, depth));
   1907     assert(invoke(depth, 2, 1) == 1);
   1908 
   1909     assert(!invoke(device, 8, 3, replacement, 0u));
   1910     assert(!invoke(device, 8, 3, replacement, 0u));
   1911     assert(!invoke(device, 15, 2, viewport));
   1912     assert(!invoke(device, 9, 2, workspace()));
   1913 
   1914     assert(*memory(workspace()) == replacement);
   1915     assert(!memcmp(memory(viewport), initial_viewport, sizeof(initial_viewport)));
   1916     assert(invoke(replacement, 2, 1) == 2);
   1917     assert(invoke(replacement, 2, 1) == 1);
   1918     assert(invoke(original, 1, 1) == 2);
   1919     assert(invoke(original, 2, 1) == 1);
   1920 
   1921     *memory(viewport + 8) = 64; *memory(viewport + 12) = 48;
   1922     assert(!invoke(device, 13, 2, viewport));
   1923     assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff123456u, 0x3f800000u, 0u));
   1924     *memory(desc) = 124;
   1925     assert(!invoke(replacement, 25, 5, 0u, desc, 0x11u, 0u));
   1926 
   1927     uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   1928     assert((*memory(pixels + 47 * pitch + 63 * 4) & 0xffffff) == 0x123456);
   1929     assert(!invoke(replacement, 32, 2, 0u));
   1930     assert(!invoke(device, 8, 3, original, 0u));
   1931     memcpy(memory(viewport), initial_viewport, sizeof(initial_viewport));
   1932     assert(!invoke(device, 13, 2, viewport));
   1933     assert(!invoke(device, 9, 2, workspace()) && *memory(workspace()) == original);
   1934     assert(invoke(original, 2, 1) == 2);
   1935 }
   1936 
   1937 #ifdef __EMSCRIPTEN__
   1938 static void browser_resize_reallocates_the_cpu_drawing_surface(uint32_t draw, uint32_t device)
   1939 {
   1940     uint32_t desc = workspace() + 128, rectangle = workspace() + 256;
   1941     memset(memory(desc), 0, 124);
   1942     *memory(desc) = 124; *memory(desc + 4) = 7;
   1943     *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x840;
   1944     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   1945     uint32_t surface = *memory(workspace());
   1946     const uint32_t bounds[] = {0, 0, 101, 73};
   1947     memcpy(memory(rectangle), bounds, sizeof(bounds));
   1948 
   1949     assert(!invoke(device, 5, 1));
   1950     assert(!linux_graphics_resize(101, 73, surface));
   1951     assert(!invoke(device, 6, 1));
   1952     assert(linux_graphics_resize(101, 73, surface));
   1953 
   1954     assert(!invoke(surface, 25, 5, rectangle, desc, 0x801u, 0u));
   1955     assert(*memory(desc + 8) == 73 && *memory(desc + 12) == 101);
   1956     assert(*memory(desc + 16) == 404);
   1957     uint32_t pixels = *memory(desc + 36);
   1958     *memory(pixels + 72 * 404 + 100 * 4) = 0xff123456;
   1959     assert(!invoke(surface, 32, 2, rectangle));
   1960     assert(!invoke(surface, 25, 5, rectangle, desc, 0x11u, 0u));
   1961     assert(*memory(*memory(desc + 36) + 72 * 404 + 100 * 4) == 0xff123456);
   1962     assert(!invoke(surface, 32, 2, rectangle));
   1963 
   1964     assert(linux_graphics_resize(17, 19, surface));
   1965 
   1966     assert(!invoke(surface, 25, 5, 0u, desc, 0x11u, 0u));
   1967     assert(*memory(desc + 8) == 19 && *memory(desc + 12) == 17);
   1968     assert(*memory(desc + 16) == 68);
   1969     assert(!invoke(surface, 32, 2, 0u));
   1970     assert(!invoke(surface, 2, 1));
   1971 }
   1972 #endif
   1973 
   1974 static void device_lifetime_and_rendering(const char *pixels_path)
   1975 {
   1976     const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
   1977     const uint32_t d3d_iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
   1978     const uint32_t hal_iid[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0};
   1979     const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
   1980     memcpy(memory(workspace() + 16), draw_iid, 16);
   1981 #ifdef _WIN32
   1982     uint32_t (WINAPI *create)(uint32_t, uint32_t, uint32_t, uint32_t) =
   1983         (void *)GetProcAddress(LoadLibraryA("ddraw.dll"), "DirectDrawCreateEx");
   1984     assert(create && !create(0, workspace(), workspace() + 16, 0));
   1985 #else
   1986     uint32_t stack = g_esp;
   1987     PUSH32(g_esp, 0); PUSH32(g_esp, workspace() + 16); PUSH32(g_esp, workspace()); PUSH32(g_esp, 0);
   1988     PUSH32(g_esp, RECOMP_RETADDR);
   1989     linux_graphics_lookup("DirectDrawCreateEx")();
   1990     assert(!g_eax && g_esp == stack);
   1991 #endif
   1992     uint32_t draw = *memory(workspace());
   1993     assert(!invoke(draw, 20, 3, 0u, 0x808u));
   1994     uint32_t d3d = query(draw, d3d_iid), desc = workspace() + 128;
   1995     vertex_buffer_identities_survive_other_releases(d3d);
   1996     vertex_buffers_preserve_data_and_description(d3d);
   1997     memset(memory(desc), 0, 124);
   1998     *memory(desc) = 124; *memory(desc + 4) = 7;
   1999     *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x6040;
   2000     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   2001     uint32_t color = *memory(workspace());
   2002     *memory(desc + 4) = 0x1007; *memory(desc + 104) = 0x24000;
   2003     const uint32_t depth_format[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0};
   2004     memcpy(memory(desc + 72), depth_format, 32);
   2005     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   2006     uint32_t depth = *memory(workspace());
   2007     assert(!invoke(color, 3, 2, depth));
   2008     memcpy(memory(workspace() + 16), hal_iid, 16);
   2009     assert(!invoke(d3d, 4, 4, workspace() + 16, color, workspace()));
   2010     uint32_t device = *memory(workspace());
   2011     vertex_buffer_optimization_preserves_data(d3d, device);
   2012     initial_device_states(device);
   2013     material_state_is_copied(device);
   2014     transforms_copy_geometry_and_texture_matrices(device);
   2015     light_definitions_and_enable_state_are_owned(device);
   2016     changing_render_targets_preserves_viewport_and_ownership(draw, device, color);
   2017 #ifdef __EMSCRIPTEN__
   2018     browser_resize_reallocates_the_cpu_drawing_surface(draw, device);
   2019 #endif
   2020 
   2021     uint32_t identity = query(device, unknown);
   2022     assert(!invoke(device, 9, 2, workspace()));
   2023     uint32_t target = *memory(workspace());
   2024     assert(!invoke(device, 7, 2, workspace()));
   2025     uint32_t parent = *memory(workspace());
   2026     assert(!invoke(device, 15, 2, desc));
   2027 
   2028     const uint32_t viewport[] = {0, 0, 32, 32, 0, 0x3f800000};
   2029     assert(identity == device && invoke(identity, 2, 1) == 1);
   2030     assert(target == color && invoke(target, 2, 1) == 2);
   2031     assert(parent == d3d && invoke(parent, 2, 1) > 0);
   2032     assert(!memcmp(memory(desc), viewport, sizeof(viewport)));
   2033     assert(!invoke(device, 13, 2, desc));
   2034     uint32_t caps = workspace() + 768;
   2035     memset(memory(caps), 0xa5, 240);
   2036     assert(!invoke(device, 3, 2, caps));
   2037     assert(*memory(caps + 4) == 56 && *memory(caps + 60) == 56);
   2038     assert(*memory(caps + 236) == 0xa5a5a5a5);
   2039     assert(!memcmp(memory(caps + 196), hal_iid, 16));
   2040     assert(!invoke(device, 21, 3, 22u, workspace()) && *memory(workspace()) == 3);
   2041     assert(invoke(device, 6, 1) == 0x887602f9u);
   2042     assert(!invoke(device, 5, 1));
   2043     assert(invoke(device, 5, 1) == 0x887602f8u);
   2044     assert(!invoke(device, 6, 1));
   2045     /* Driver-internal DirectDraw references differ; external owners must stay alive. */
   2046     assert(invoke(d3d, 2, 1) > 0);
   2047     assert(invoke(color, 2, 1) == 1);
   2048     assert(invoke(depth, 2, 1) == 1);
   2049 
   2050     assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   2051     assert(!invoke(device, 20, 3, 22u, 1u));
   2052     struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
   2053         {4, 4, 0.25f, 1, 0xffff0000}, {28, 4, 0.25f, 1, 0xff00ff00}, {4, 28, 0.25f, 1, 0xff0000ff}
   2054     };
   2055     uint32_t input = workspace() + 512;
   2056     memcpy(memory(input), vertices, sizeof(vertices));
   2057 
   2058     assert(!invoke(device, 5, 1));
   2059     assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   2060     for (unsigned i = 0; i < 3; ++i) { vertices[i].z = 0.75f; vertices[i].color = 0xffffffff; }
   2061     memcpy(memory(input), vertices, sizeof(vertices));
   2062     assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   2063     assert(!invoke(device, 6, 1));
   2064     *memory(desc) = 124;
   2065     assert(!invoke(color, 25, 5, 0u, desc, 0x11u, 0u));
   2066     uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
   2067 
   2068     assert((*memory(pixels) & 0xffffff) == 0x102030);
   2069     assert((*memory(pixels + 4 * pitch + 4 * 4) & 0xffffff) == 0xff0000);
   2070     uint32_t sample = *memory(pixels + 8 * pitch + 8 * 4);
   2071     assert(((sample >> 16) & 255) >= 169 && ((sample >> 16) & 255) <= 171);
   2072     assert(((sample >> 8) & 255) >= 41 && ((sample >> 8) & 255) <= 43);
   2073     assert((sample & 255) >= 41 && (sample & 255) <= 43);
   2074     unsigned char rgb[32 * 32 * 3];
   2075     for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   2076         uint32_t pixel = *memory(pixels + y * pitch + x * 4);
   2077         rgb[(y * 32 + x) * 3] = pixel >> 16;
   2078         rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   2079         rgb[(y * 32 + x) * 3 + 2] = pixel;
   2080     }
   2081     assert(!invoke(color, 32, 2, 0u));
   2082     FILE *file = fopen(pixels_path, "wb");
   2083     assert(file && fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   2084     /* Fractional edges and unequal reciprocal W expose interpolation mistakes. */
   2085     struct vertex projected[] = {
   2086         {4.25f, 4.75f, 0.25f, 1, 0xffff0000},
   2087         {28.25f, 4.75f, 0.25f, 0.5f, 0xff00ff00},
   2088         {4.25f, 28.75f, 0.25f, 2, 0xff0000ff}
   2089     };
   2090     memcpy(memory(input), projected, sizeof(projected));
   2091     assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
   2092     assert(!invoke(device, 5, 1));
   2093     assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
   2094     assert(!invoke(device, 6, 1));
   2095     *memory(desc) = 124;
   2096     assert(!invoke(color, 25, 5, 0u, desc, 0x11u, 0u));
   2097     pixels = *memory(desc + 36); pitch = *memory(desc + 16);
   2098     for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
   2099         uint32_t pixel = *memory(pixels + y * pitch + x * 4);
   2100         rgb[(y * 32 + x) * 3] = pixel >> 16;
   2101         rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
   2102         rgb[(y * 32 + x) * 3 + 2] = pixel;
   2103     }
   2104     assert(!invoke(color, 32, 2, 0u));
   2105     assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
   2106     transformed_fans_without_diffuse_use_white(draw, device, color);
   2107     textured_triangles(draw, device, color, file);
   2108     rgb565_managed_texture_survives_eviction(draw);
   2109     mip_levels_render_after_edits_and_eviction(draw, device, color);
   2110     transformed_vertex_fog_blends_rgb(device, color, file);
   2111     vertex_buffer_draws_match_memory_draws(draw, device, color, file);
   2112     indexed_vertex_buffers_use_relative_indices(draw, device, color, file);
   2113     vertex_buffer_writes_update_rendered_pixels(draw, device, color);
   2114     rectangle_clears_preserve_outside_depth(device, color, file);
   2115     depth_write_controls_occlusion(device, color);
   2116     disabling_depth_preserves_occlusion(device, color);
   2117     destination_color_blending_multiplies_channels(device, color);
   2118     depth_comparisons_control_visibility(device, color);
   2119     transformed_render_states_preserve_rasterization(device, color);
   2120     viewport_limits_clearing_and_drawing(device, color, file);
   2121     untransformed_vertices_apply_camera_and_lighting(device, color, file);
   2122     diffuse_material_source_selects_lighting_color(device, color);
   2123     light_enable_changes_apply_between_draws(device, color);
   2124     depth_bias_offsets_overlapping_geometry(device, color, 16, file);
   2125     assert(!invoke(color, 8, 3, 0u, depth));
   2126     memset(memory(desc), 0, 124);
   2127     *memory(desc) = 124; *memory(desc + 4) = 0x1007;
   2128     *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x24000;
   2129     const uint32_t depth24[] = {32, 0x400, 0, 24, 0, 0xffffff, 0, 0};
   2130     memcpy(memory(desc + 72), depth24, 32);
   2131     assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
   2132     depth = *memory(workspace());
   2133     assert(!invoke(color, 3, 2, depth));
   2134     assert(invoke(depth, 2, 1) == 1);
   2135     /* A new device selects the newly attached depth buffer on both platforms. */
   2136     assert(invoke(color, 1, 1) == 2);
   2137     assert(!invoke(device, 2, 1));
   2138     d3d = query(draw, d3d_iid);
   2139     memcpy(memory(workspace() + 16), hal_iid, 16);
   2140     assert(!invoke(d3d, 4, 4, workspace() + 16, color, workspace()));
   2141     device = *memory(workspace());
   2142     assert(invoke(color, 2, 1) == 1);
   2143     assert(invoke(d3d, 2, 1) > 0);
   2144     depth_bias_offsets_overlapping_geometry(device, color, 24, file);
   2145     alpha_tests_preserve_rejected_color_and_depth(draw, device, color, file);
   2146     fourfold_modulation_clamps_before_the_next_stage(device, color);
   2147     two_texture_stages_combine_independent_samples(draw, device, color, file);
   2148     line_lists_draw_independent_segments(draw, device, color);
   2149     projective_world_and_view_matrices_render(device, color);
   2150     assert(!fclose(file));
   2151     assert(!invoke(device, 2, 1));
   2152     assert(!invoke(draw, 2, 1));
   2153 }
   2154 
   2155 int main(int argc, char **argv)
   2156 {
   2157 #ifndef __EMSCRIPTEN__
   2158     assert(argc == 2);
   2159 #endif
   2160 #ifndef _WIN32
   2161     assert(guest_map("device test", 0x50000000, 0x10000));
   2162     assert(guest_map("split vertex page", 0x51000000, 0x10000));
   2163     assert(guest_map("split vertex page", 0x51010000, 0x10000));
   2164     assert(guest_map("contiguous vertex pages", 0x52000000, 0x20000));
   2165     g_esp = 0x5000f000;
   2166 #endif
   2167 #ifdef __EMSCRIPTEN__
   2168     device_lifetime_and_rendering("/devices.rgb");
   2169     linux_graphics_shutdown();
   2170 #else
   2171     device_lifetime_and_rendering(argv[1]);
   2172 #endif
   2173     puts("device: ownership, viewport, scenes, clear, triangles, line lists, depth, textures, filtering and blending passed");
   2174     return 0;
   2175 }