aorecomp

Anarchy Online classic in the browser

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


commit e94df371874a8667e5c8ffc8fa95f9764064e420
parent 0c3b2cfaec4bbc4f5a33f3134636843fb4ceaa80
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 03:09:32 +0000

Bind native Direct3D device and verify rasterization against Windows

Diffstat:
Mdecompiled/Makefile.linux | 7+++++++
Mdecompiled/runtime/gl_renderer.c | 26++++----------------------
Mdecompiled/runtime/linux_graphics.c | 291+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Adecompiled/tests/devices.c | 210+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_devices.py | 32++++++++++++++++++++++++++++++++
5 files changed, 531 insertions(+), 35 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -177,6 +177,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/graphics ./build/linux/window ./build/linux/surfaces + ./build/linux/devices build/linux/device-pixels.rgb build/linux/surfaces: tests/surfaces.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/linux_window.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h mkdir -p $(@D) @@ -184,4 +185,10 @@ build/linux/surfaces: tests/surfaces.c runtime/linux_graphics.c runtime/linux_wi test: build/linux/surfaces +build/linux/devices: tests/devices.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/linux_window.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -o $@ + +test: build/linux/devices + -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -4,7 +4,6 @@ #include <stddef.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <assert.h> static GLuint program, vertex_array, vertex_buffer, index_buffer; @@ -35,7 +34,7 @@ int gl_renderer_initialize(void) "layout(location=0) in vec4 position;\n" "layout(location=1) in vec4 diffuse;\n" "out vec4 color;\n" - "void main() { gl_Position=vec4(position.xy,2.0*position.z-position.w,position.w); color=diffuse; }\n", + "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; }\n", "#version 330 core\n" "in vec4 color; out vec4 pixel;\n" "void main() { pixel=color; }\n" @@ -213,31 +212,14 @@ void gl_target_read(struct gl_target *target, unsigned char *rgba) glBindFramebuffer(GL_FRAMEBUFFER, 0); check_graphics("target readback"); - /* Guest images are top-down; OpenGL readback starts at the bottom row. */ - size_t pitch = target->width * 4; - unsigned char *row = malloc(pitch); - if (!row) abort(); - for (unsigned y = 0; y < target->height / 2; ++y) { - unsigned char *top = rgba + y * pitch; - unsigned char *bottom = rgba + (target->height - 1 - y) * pitch; - memcpy(row, top, pitch); - memcpy(top, bottom, pitch); - memcpy(bottom, row, pitch); - } - free(row); + /* The shader puts the logical top row at GL row zero. This also maps GL's + * bottom-left edge inclusion to Direct3D's top-left rasterization rule. */ } void gl_target_write(struct gl_target *target, const unsigned char *rgba) { - /* Upload top-down guest pixels in OpenGL's bottom-up row order. */ - size_t pitch = target->width * 4; - unsigned char *flipped = malloc(pitch * target->height); - if (!flipped) abort(); - for (unsigned y = 0; y < target->height; ++y) - memcpy(flipped + (target->height - 1 - y) * pitch, rgba + y * pitch, pitch); glBindTexture(GL_TEXTURE_2D, target->color); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, target->width, target->height, - GL_RGBA, GL_UNSIGNED_BYTE, flipped); - free(flipped); + GL_RGBA, GL_UNSIGNED_BYTE, rgba); check_graphics("target upload"); } diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -28,8 +28,15 @@ static struct surface_object { struct surface_object *attached; struct surface_object *next; } *surfaces; -static uint32_t vtables[6]; -static const unsigned interface_slots[] = {23, 30, 8, 49, 36, 9}; +static struct device_object { + uint32_t interface, references, cull; + int scene; + struct draw_object *owner; + struct surface_object *target; + struct device_object *next; +} *devices; +static uint32_t vtables[7]; +static const unsigned interface_slots[] = {23, 30, 8, 49, 36, 9, 49}; static unsigned current_interface, current_slot; static void close_graphics(void) @@ -288,16 +295,8 @@ static void enumerate_adapters(void) g_esp += 16; } -static void enumerate_devices(void) +static void describe_device(uint32_t record) { - find_object(MEM32(g_esp + 4)); - uint32_t callback = MEM32(g_esp + 8), data = MEM32(g_esp + 12); - if (!callback) { g_eax = 0x80070057u; g_esp += 16; return; } - const char *description = (const char *)glGetString(GL_RENDERER); - const char *name = "Native OpenGL"; - size_t description_size = strlen(description) + 1; - uint32_t record = guest_malloc(236 + description_size + strlen(name) + 1); - if (!record) abort(); memset((void *)ADDR(record), 0, 236); MEM32(record) = 0x80000; /* D3DDEVCAPS_HWRASTERIZATION */ MEM32(record + 4) = MEM32(record + 60) = 56; /* D3DPRIMCAPS sizes */ @@ -311,6 +310,19 @@ static void enumerate_devices(void) * Texturing, lighting, W buffering and other unimplemented features stay zero. */ static const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; memcpy((void *)ADDR(record + 196), hal, 16); +} + +static void enumerate_devices(void) +{ + find_object(MEM32(g_esp + 4)); + uint32_t callback = MEM32(g_esp + 8), data = MEM32(g_esp + 12); + if (!callback) { g_eax = 0x80070057u; g_esp += 16; return; } + const char *description = (const char *)glGetString(GL_RENDERER); + const char *name = "Native OpenGL"; + size_t description_size = strlen(description) + 1; + uint32_t record = guest_malloc(236 + description_size + strlen(name) + 1); + if (!record) abort(); + describe_device(record); memcpy((void *)ADDR(record + 236), description, description_size); strcpy((char *)ADDR(record + 236 + description_size), name); recomp_func_t function = recomp_lookup(callback); @@ -829,9 +841,244 @@ static void surface_unlock(void) g_eax = 0; } +static struct device_object *find_device(uint32_t address) +{ + for (struct device_object *device = devices; device; device = device->next) + if (device->interface == address) return device; + fprintf(stderr, "Invalid guest Direct3D device %08x\n", address); + abort(); +} + +static void create_device(void) +{ + struct draw_object *owner = find_object(MEM32(g_esp + 4)); + uint32_t iid = MEM32(g_esp + 8), target = MEM32(g_esp + 12), output = MEM32(g_esp + 16); + const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; + g_esp += 20; + g_eax = 0x80070057u; + if (!iid || !target || !output) return; + MEM32(output) = 0; + if (memcmp((void *)ADDR(iid), hal, 16)) { g_eax = 0x887602c1u; return; } + struct surface_object *surface = find_surface(target); + if (!surface->target || !(surface->caps & 0x2000) || surface->owner != owner) return; + struct device_object *device = calloc(1, sizeof(*device)); + if (!device) abort(); + device->interface = create_interface(6); + device->references = 1; + device->cull = 3; /* D3DCULL_CCW default; drawing currently requires CULL_NONE. */ + device->owner = owner; + device->target = surface; + ++owner->references[1]; + ++surface->references[0]; + gl_target_set_depth(surface->target, surface->attached ? surface->attached->depth : NULL); + device->next = devices; + devices = device; + MEM32(output) = device->interface; + g_eax = 0; +} + +static void device_query_interface(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t iid = MEM32(g_esp + 8), output = MEM32(g_esp + 12); + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + const uint32_t device7[] = {0xf5049e79, 0x11d24861, 0xa00007a4, 0xa82906c9}; + g_esp += 16; + g_eax = 0x80070057u; + if (!output) return; + MEM32(output) = 0; + g_eax = 0x80004002u; + if (memcmp((void *)ADDR(iid), unknown, 16) && memcmp((void *)ADDR(iid), device7, 16)) return; + MEM32(output) = device->interface; + ++device->references; + g_eax = 0; +} + +static void device_add_reference(void) +{ + g_eax = ++find_device(MEM32(g_esp + 4))->references; + g_esp += 8; +} + +static void device_release_reference(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + g_eax = --device->references; + if (!g_eax) { + struct device_object **link = &devices; + while (*link != device) link = &(*link)->next; + *link = device->next; + release_surface(device->target, 0); + release_draw(device->owner, 1); + guest_free(device->interface); + free(device); + } + g_esp += 8; +} + +static void device_caps(void) +{ + find_device(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { describe_device(output); g_eax = 0; } + g_esp += 12; +} + +static void device_begin_scene(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + g_eax = device->scene ? 0x887602f8u : 0; + device->scene = 1; + g_esp += 8; +} + +static void device_end_scene(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + g_eax = device->scene ? 0 : 0x887602f9u; + device->scene = 0; + g_esp += 8; +} + +static void device_parent(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { MEM32(output) = interface_pointer(device->owner, 2); g_eax = 0; } + g_esp += 12; +} + +static void device_get_target(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { + MEM32(output) = device->target->interfaces[0]; + ++device->target->references[0]; + g_eax = 0; + } + g_esp += 12; +} + +static void device_viewport(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { + const uint32_t viewport[] = {0, 0, device->target->width, device->target->height, 0, 0x3f800000}; + memcpy((void *)ADDR(output), viewport, sizeof(viewport)); + g_eax = 0; + } + g_esp += 12; +} + +static void device_set_viewport(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t input = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (input) { + const uint32_t viewport[] = {0, 0, device->target->width, device->target->height, 0, 0x3f800000}; + if (memcmp((void *)ADDR(input), viewport, sizeof(viewport))) { + fprintf(stderr, "Unsupported Direct3D viewport\n"); exit(3); + } + g_eax = 0; + } + g_esp += 12; +} + +static void device_clear(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t count = MEM32(g_esp + 8), rectangles = MEM32(g_esp + 12), flags = MEM32(g_esp + 16); + uint32_t color = MEM32(g_esp + 20), depth = MEM32(g_esp + 24), stencil = MEM32(g_esp + 28); + g_esp += 32; + if (count || rectangles || flags != 3 || stencil) { + fprintf(stderr, "Unsupported Direct3D clear count=%u flags=%08x stencil=%u\n", count, flags, stencil); exit(3); + } + float z; + memcpy(&z, &depth, 4); + gl_target_clear(device->target->target, color, z); + g_eax = 0; +} + +static uint32_t device_state(struct device_object *device, uint32_t state) +{ + switch (state) { + case 7: case 14: case 137: return 1; /* Z enable/write and lighting. */ + case 8: return 3; /* Solid fill. */ + case 9: return 2; /* Gouraud shading. */ + case 22: return device->cull; + case 23: return 4; /* LESSEQUAL depth comparison. */ + case 26: case 27: case 28: return 0; /* Dither, blending and fog. */ + default: fprintf(stderr, "Unsupported Direct3D render state %u\n", state); exit(3); + } +} + +static void device_get_state(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t state = MEM32(g_esp + 8), output = MEM32(g_esp + 12); + g_eax = 0x80070057u; + if (output) { MEM32(output) = device_state(device, state); g_eax = 0; } + g_esp += 16; +} + +static void device_set_state(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t state = MEM32(g_esp + 8), value = MEM32(g_esp + 12); + if (state == 22 && (value == 1 || value == 3)) device->cull = value; + else if (value != device_state(device, state)) { + fprintf(stderr, "Unsupported Direct3D render state %u value=%08x\n", state, value); exit(3); + } + g_eax = 0; + g_esp += 16; +} + +static void device_draw_primitive(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t primitive = MEM32(g_esp + 8), fvf = MEM32(g_esp + 12), input = MEM32(g_esp + 16); + uint32_t count = MEM32(g_esp + 20), flags = MEM32(g_esp + 24); + g_esp += 28; + if (primitive != 4 || fvf != 0x44 || flags || device->cull != 1) { + fprintf(stderr, "Unsupported Direct3D draw primitive=%u fvf=%08x flags=%08x cull=%u\n", primitive, fvf, flags, device->cull); exit(3); + } + g_eax = 0x80070057u; + if (!input || !count || count % 3 || count > 65535) return; + struct gl_vertex *vertices = malloc(count * sizeof(*vertices)); + uint16_t *indices = malloc(count * sizeof(*indices)); + if (!vertices || !indices) abort(); + for (uint32_t i = 0; i < count; ++i) { + float position[4]; + memcpy(position, (void *)ADDR(input + i * 20), 16); + float w = 1.0f / position[3]; + /* D3D transformed coordinates put integer positions at pixel centers. */ + vertices[i].position[0] = (2 * (position[0] + 0.5f) / device->target->width - 1) * w; + vertices[i].position[1] = (1 - 2 * (position[1] + 0.5f) / device->target->height) * w; + vertices[i].position[2] = position[2] * w; + vertices[i].position[3] = w; + uint32_t color = MEM32(input + i * 20 + 16); + vertices[i].color[0] = ((color >> 16) & 255) / 255.0f; + vertices[i].color[1] = ((color >> 8) & 255) / 255.0f; + vertices[i].color[2] = (color & 255) / 255.0f; + vertices[i].color[3] = (color >> 24) / 255.0f; + indices[i] = i; + } + gl_target_draw(device->target->target, vertices, count, indices, count); + free(indices); + free(vertices); + g_eax = 0; +} + static void unavailable_method(void) { - const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper"}; + const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper", "IDirect3DDevice7"}; fprintf(stderr, "Unimplemented %s method slot %u caller=%08x\n", names[current_interface], current_slot, g_cur_func); exit(3); @@ -839,10 +1086,27 @@ static void unavailable_method(void) recomp_func_t linux_graphics_virtual(uint32_t address) { - if (address < 0xf3000000u || address >= 0xf3000600u || (address & 3)) return NULL; + if (address < 0xf3000000u || address >= 0xf3000700u || (address & 3)) return NULL; current_interface = (address - 0xf3000000u) / 0x100; current_slot = (address & 0xff) / 4; if (current_slot >= interface_slots[current_interface]) return NULL; + if (current_interface == 6) { + if (current_slot == 0) return device_query_interface; + if (current_slot == 1) return device_add_reference; + if (current_slot == 2) return device_release_reference; + if (current_slot == 3) return device_caps; + if (current_slot == 5) return device_begin_scene; + if (current_slot == 6) return device_end_scene; + if (current_slot == 7) return device_parent; + if (current_slot == 9) return device_get_target; + if (current_slot == 10) return device_clear; + if (current_slot == 13) return device_set_viewport; + if (current_slot == 15) return device_viewport; + if (current_slot == 20) return device_set_state; + if (current_slot == 21) return device_get_state; + if (current_slot == 25) return device_draw_primitive; + return unavailable_method; + } if (current_interface == 5) { if (current_slot == 0) return clipper_query_interface; if (current_slot == 1) return clipper_add_reference; @@ -875,6 +1139,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 1) return add_reference; if (current_slot == 2) return release_reference; if (current_interface == 2 && current_slot == 3) return enumerate_devices; + if (current_interface == 2 && current_slot == 4) return create_device; if (current_interface == 1 && current_slot == 4) return create_clipper; if (current_interface == 1 && current_slot == 6) return create_surface; if (current_interface == 1 && current_slot == 8) return enumerate_display_modes; diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -0,0 +1,210 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#include <d3d.h> +_Static_assert(sizeof(D3DVIEWPORT7) == 24, "guest viewport size"); +static uint32_t scratch[1024]; +#else +#include "linux_graphics.h" +#include "guest_memory.h" +recomp_func_t recomp_lookup(uint32_t address) { (void)address; assert(0); return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +#endif + +static uint32_t workspace(void) +{ +#ifdef _WIN32 + return (uintptr_t)scratch; +#else + return 0x50001000u; +#endif +} + +static uint32_t *memory(uint32_t address) +{ +#ifdef _WIN32 + return (void *)(uintptr_t)address; +#else + return (void *)ADDR(address); +#endif +} + +static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...) +{ + uint32_t args[7] = {object}; + va_list list; + va_start(list, count); + for (unsigned i = 1; i < count; ++i) args[i] = va_arg(list, uint32_t); + va_end(list); + uint32_t address = memory(*memory(object))[slot]; +#ifdef _WIN32 + if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))(uintptr_t)address)(object); + if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))(uintptr_t)address)(object, args[1]); + if (count == 3) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2]); + 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]); + 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]); + 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]); + assert(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]); +#else + uint32_t stack = g_esp, caller = g_cur_func; + g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab; + for (unsigned i = count; i; --i) PUSH32(g_esp, args[i - 1]); + PUSH32(g_esp, RECOMP_RETADDR); + recomp_func_t function = linux_graphics_virtual(address); + assert(function); + function(); + assert(g_esp == stack && g_cur_func == caller); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab); + return g_eax; +#endif +} + +static uint32_t query(uint32_t object, const uint32_t *iid) +{ + memcpy(memory(workspace() + 16), iid, 16); + assert(!invoke(object, 0, 3, workspace() + 16, workspace())); + return *memory(workspace()); +} + +static void device_lifetime_and_rendering(const char *pixels_path) +{ + const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; + const uint32_t d3d_iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; + const uint32_t hal_iid[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + memcpy(memory(workspace() + 16), draw_iid, 16); +#ifdef _WIN32 + uint32_t (WINAPI *create)(uint32_t, uint32_t, uint32_t, uint32_t) = + (void *)GetProcAddress(LoadLibraryA("ddraw.dll"), "DirectDrawCreateEx"); + assert(create && !create(0, workspace(), workspace() + 16, 0)); +#else + uint32_t stack = g_esp; + PUSH32(g_esp, 0); PUSH32(g_esp, workspace() + 16); PUSH32(g_esp, workspace()); PUSH32(g_esp, 0); + PUSH32(g_esp, RECOMP_RETADDR); + linux_graphics_lookup("DirectDrawCreateEx")(); + assert(!g_eax && g_esp == stack); +#endif + uint32_t draw = *memory(workspace()); + assert(!invoke(draw, 20, 3, 0u, 0x808u)); + uint32_t d3d = query(draw, d3d_iid), desc = workspace() + 128; + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 7; + *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x6040; + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t color = *memory(workspace()); + *memory(desc + 4) = 0x1007; *memory(desc + 104) = 0x24000; + const uint32_t depth_format[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0}; + memcpy(memory(desc + 72), depth_format, 32); + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t depth = *memory(workspace()); + assert(!invoke(color, 3, 2, depth)); + memcpy(memory(workspace() + 16), hal_iid, 16); + assert(!invoke(d3d, 4, 4, workspace() + 16, color, workspace())); + uint32_t device = *memory(workspace()); + + uint32_t identity = query(device, unknown); + assert(!invoke(device, 9, 2, workspace())); + uint32_t target = *memory(workspace()); + assert(!invoke(device, 7, 2, workspace())); + uint32_t parent = *memory(workspace()); + assert(!invoke(device, 15, 2, desc)); + + const uint32_t viewport[] = {0, 0, 32, 32, 0, 0x3f800000}; + assert(identity == device && invoke(identity, 2, 1) == 1); + assert(target == color && invoke(target, 2, 1) == 2); + assert(parent == d3d && invoke(parent, 2, 1) > 0); + assert(!memcmp(memory(desc), viewport, sizeof(viewport))); + assert(!invoke(device, 13, 2, desc)); + uint32_t caps = workspace() + 768; + memset(memory(caps), 0xa5, 240); + assert(!invoke(device, 3, 2, caps)); + assert(*memory(caps + 4) == 56 && *memory(caps + 60) == 56); + assert(*memory(caps + 236) == 0xa5a5a5a5); + assert(!memcmp(memory(caps + 196), hal_iid, 16)); + assert(!invoke(device, 21, 3, 22u, workspace()) && *memory(workspace()) == 3); + assert(invoke(device, 6, 1) == 0x887602f9u); + assert(!invoke(device, 5, 1)); + assert(invoke(device, 5, 1) == 0x887602f8u); + assert(!invoke(device, 6, 1)); + /* Driver-internal DirectDraw references differ; external owners must stay alive. */ + assert(invoke(d3d, 2, 1) > 0); + assert(invoke(color, 2, 1) == 1); + assert(invoke(depth, 2, 1) == 1); + + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + assert(!invoke(device, 20, 3, 22u, 1u)); + struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = { + {4, 4, 0.25f, 1, 0xffff0000}, {28, 4, 0.25f, 1, 0xff00ff00}, {4, 28, 0.25f, 1, 0xff0000ff} + }; + uint32_t input = workspace() + 512; + memcpy(memory(input), vertices, sizeof(vertices)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + for (unsigned i = 0; i < 3; ++i) { vertices[i].z = 0.75f; vertices[i].color = 0xffffffff; } + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + *memory(desc) = 124; + assert(!invoke(color, 25, 5, 0u, desc, 0x11u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + + assert((*memory(pixels) & 0xffffff) == 0x102030); + assert((*memory(pixels + 4 * pitch + 4 * 4) & 0xffffff) == 0xff0000); + uint32_t sample = *memory(pixels + 8 * pitch + 8 * 4); + assert(((sample >> 16) & 255) >= 169 && ((sample >> 16) & 255) <= 171); + assert(((sample >> 8) & 255) >= 41 && ((sample >> 8) & 255) <= 43); + assert((sample & 255) >= 41 && (sample & 255) <= 43); + unsigned char rgb[32 * 32 * 3]; + for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) { + uint32_t pixel = *memory(pixels + y * pitch + x * 4); + rgb[(y * 32 + x) * 3] = pixel >> 16; + rgb[(y * 32 + x) * 3 + 1] = pixel >> 8; + rgb[(y * 32 + x) * 3 + 2] = pixel; + } + assert(!invoke(color, 32, 2, 0u)); + FILE *file = fopen(pixels_path, "wb"); + assert(file && fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); + /* Fractional edges and unequal reciprocal W expose interpolation mistakes. */ + struct vertex projected[] = { + {4.25f, 4.75f, 0.25f, 1, 0xffff0000}, + {28.25f, 4.75f, 0.25f, 0.5f, 0xff00ff00}, + {4.25f, 28.75f, 0.25f, 2, 0xff0000ff} + }; + memcpy(memory(input), projected, sizeof(projected)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + *memory(desc) = 124; + assert(!invoke(color, 25, 5, 0u, desc, 0x11u, 0u)); + pixels = *memory(desc + 36); pitch = *memory(desc + 16); + for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) { + uint32_t pixel = *memory(pixels + y * pitch + x * 4); + rgb[(y * 32 + x) * 3] = pixel >> 16; + rgb[(y * 32 + x) * 3 + 1] = pixel >> 8; + rgb[(y * 32 + x) * 3 + 2] = pixel; + } + assert(!invoke(color, 32, 2, 0u)); + assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); + assert(!fclose(file)); + assert(!invoke(device, 2, 1)); + assert(!invoke(draw, 2, 1)); +} + +int main(int argc, char **argv) +{ + assert(argc == 2); +#ifndef _WIN32 + assert(guest_map("device test", 0x50000000, 0x10000)); + g_esp = 0x5000f000; +#endif + device_lifetime_and_rendering(argv[1]); + puts("device: ownership, viewport, scenes, GPU clear, transformed triangle and depth rejection passed"); + return 0; +} diff --git a/tools/reconstruction/test_portable_devices.py b/tools/reconstruction/test_portable_devices.py @@ -0,0 +1,32 @@ +"""Compare Direct3D device contracts and rendered pixels with Windows x86.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-devices" +work.mkdir(parents=True, exist_ok=True) +reference = work / "reference.exe" +subprocess.run([ + str(root / "tools/rust/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), + "-O2", "tests/devices.c", "-o", str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference), str(work / "reference.rgb")], text=True, timeout=30) +linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] +linux_work = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + [ + "gcc", "-O2", "-Wall", "-Wextra", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", + "tests/devices.c", "runtime/linux_graphics.c", "runtime/linux_window.c", "runtime/gl_renderer.c", + "runtime/guest_heap.c", "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-o", linux_work + "/translated", +], check=True) +actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_work + "/translated", linux_work + "/native.rgb"], text=True, timeout=30) +assert actual == expected, (expected, actual) +windows_pixels = (work / "reference.rgb").read_bytes() +linux_pixels = (work / "native.rgb").read_bytes() +assert len(windows_pixels) == len(linux_pixels) == 6144 +error = max(abs(a - b) for a, b in zip(windows_pixels, linux_pixels)) +assert error <= 1, error +print("Windows == Linux: " + actual.strip()) +print("All 2048 pixels agree within one 8-bit channel step; maximum error:", error)