aorecomp

Anarchy Online classic in the browser

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


commit 13a89ce4a3f0e3921bd66b4cd537c22a7d0558df
parent a5cf3312b9c9879c9336c2a9a8826a87a0efa340
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 02:43:59 +0000

Add GPU-backed DirectDraw surfaces to native Linux runtime

Diffstat:
Mdecompiled/Makefile.linux | 7+++++++
Mdecompiled/runtime/gl_renderer.c | 15+++++++++++++++
Mdecompiled/runtime/gl_renderer.h | 1+
Mdecompiled/runtime/linux_graphics.c | 270++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mdecompiled/tests/graphics.c | 24++++++++++++++++++++++++
Adecompiled/tests/surfaces.c | 203+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_surfaces.py | 26++++++++++++++++++++++++++
7 files changed, 530 insertions(+), 16 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -176,5 +176,12 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/atoi ./build/linux/graphics ./build/linux/window + ./build/linux/surfaces + +build/linux/surfaces: tests/surfaces.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.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/surfaces -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -185,3 +185,18 @@ void gl_target_read(struct gl_target *target, unsigned char *rgba) } free(row); } + +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); + check_graphics("target upload"); +} diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -17,5 +17,6 @@ void gl_target_clear(struct gl_target *target, uint32_t argb, float depth); void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, unsigned vertex_count, const uint16_t *indices, unsigned index_count); void gl_target_read(struct gl_target *target, unsigned char *rgba); +void gl_target_write(struct gl_target *target, const unsigned char *rgba); #endif diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -11,8 +11,16 @@ static struct draw_object { uint32_t interfaces[3], references[2]; struct draw_object *next; } *objects; -static uint32_t vtables[3]; -static const unsigned interface_slots[] = {23, 30, 8}; +static struct surface_object { + uint32_t interfaces[2], references[2]; + uint32_t width, height, caps, pixels, lock_flags; + int locked; + struct draw_object *owner; + struct gl_target *target; + struct surface_object *next; +} *surfaces; +static uint32_t vtables[5]; +static const unsigned interface_slots[] = {23, 30, 8, 49, 36}; static unsigned current_interface, current_slot; static void close_graphics(void) @@ -62,7 +70,7 @@ static struct draw_object *find_object(uint32_t address) abort(); } -static uint32_t interface_pointer(struct draw_object *object, unsigned index) +static uint32_t create_interface(unsigned index) { if (!vtables[index]) { unsigned slots = interface_slots[index]; @@ -71,11 +79,15 @@ static uint32_t interface_pointer(struct draw_object *object, unsigned index) for (unsigned slot = 0; slot < slots; ++slot) MEM32(vtables[index] + slot * 4) = 0xf3000000u + index * 0x100 + slot * 4; } - if (!object->interfaces[index]) { - object->interfaces[index] = guest_malloc(4); - if (!object->interfaces[index]) abort(); - MEM32(object->interfaces[index]) = vtables[index]; - } + uint32_t address = guest_malloc(4); + if (!address) abort(); + MEM32(address) = vtables[index]; + return address; +} + +static uint32_t interface_pointer(struct draw_object *object, unsigned index) +{ + if (!object->interfaces[index]) object->interfaces[index] = create_interface(index); /* Direct3D7 and DirectDraw7 share a count; DirectDraw has its own. */ ++object->references[index ? 1 : 0]; return object->interfaces[index]; @@ -338,13 +350,10 @@ static void add_reference(void) g_esp += 8; } -static void release_reference(void) +static uint32_t release_draw(struct draw_object *object, unsigned index) { - uint32_t address = MEM32(g_esp + 4); - struct draw_object *object = find_object(address); - unsigned index = address == object->interfaces[0] ? 0 : 1; - g_eax = --object->references[index]; - if (!g_eax) { + uint32_t remaining = --object->references[index]; + if (!remaining) { for (unsigned i = 0; i < 3; ++i) if ((i ? 1 : 0) == index) { guest_free(object->interfaces[i]); object->interfaces[i] = 0; @@ -356,12 +365,226 @@ static void release_reference(void) *link = object->next; free(object); } + return remaining; +} + +static void release_reference(void) +{ + uint32_t address = MEM32(g_esp + 4); + struct draw_object *object = find_object(address); + g_eax = release_draw(object, address == object->interfaces[0] ? 0 : 1); + g_esp += 8; +} + +static struct surface_object *find_surface(uint32_t address) +{ + for (struct surface_object *surface = surfaces; surface; surface = surface->next) + for (unsigned i = 0; i < 2; ++i) + if (surface->interfaces[i] == address && surface->references[i]) return surface; + fprintf(stderr, "Invalid guest surface interface %08x\n", address); + abort(); +} + +static void create_surface(void) +{ + struct draw_object *owner = find_object(MEM32(g_esp + 4)); + uint32_t desc = MEM32(g_esp + 8), output = MEM32(g_esp + 12), outer = MEM32(g_esp + 16); + g_eax = 0x80070057u; + g_esp += 20; + if (!output || !desc || MEM32(desc) != 124) return; + MEM32(output) = 0; + uint32_t flags = MEM32(desc + 4), caps = MEM32(desc + 104); + uint32_t width = MEM32(desc + 12), height = MEM32(desc + 8); + if (outer || !((flags == 1 && caps == 0x200) || + (flags == 7 && (caps == 0x40 || caps == 0x2040 || caps == 0x6040)))) { + fprintf(stderr, "Unsupported DirectDraw surface flags=%08x caps=%08x outer=%08x\n", flags, caps, outer); + exit(3); + } + if (caps == 0x200) { + for (struct surface_object *surface = surfaces; surface; surface = surface->next) + if (surface->owner == owner && (surface->caps & 0x200)) { g_eax = 0x88760234u; return; } + SDL_DisplayMode mode; + if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(window), &mode)) abort(); + width = mode.w; + height = mode.h; + caps |= 0x8000; /* DDSCAPS_VISIBLE */ + } + GLint maximum; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maximum); + if (!width || !height || width > (uint32_t)maximum || height > (uint32_t)maximum || + (uint64_t)width * height * 4 > UINT32_MAX) return; + struct surface_object *surface = calloc(1, sizeof(*surface)); + if (!surface) abort(); + surface->target = gl_target_create(width, height); + if (!surface->target) { free(surface); g_eax = 0x8007000eu; return; } + surface->width = width; + surface->height = height; + surface->caps = caps | 0x10004000u; /* Local video memory. */ + surface->owner = owner; + ++owner->references[1]; + surface->interfaces[0] = create_interface(3); + surface->references[0] = 1; + surface->next = surfaces; + surfaces = surface; + MEM32(output) = surface->interfaces[0]; + g_eax = 0; +} + +static void surface_query_interface(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t iid = MEM32(g_esp + 8), output = MEM32(g_esp + 12); + static const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + static const uint32_t surface1[] = {0x6c14db81, 0x11cea733, 0x200021a5, 0x60e50baf}; + static const uint32_t surface7[] = {0x06675a80, 0x11d23b9b, 0x60002fb9, 0x5bea9797}; + g_esp += 16; + if (!output) { g_eax = 0x80070057u; return; } + unsigned index; + if (!memcmp((void *)ADDR(iid), surface7, 16)) index = 0; + else if (!memcmp((void *)ADDR(iid), unknown, 16) || !memcmp((void *)ADDR(iid), surface1, 16)) index = 1; + else { MEM32(output) = 0; g_eax = 0x80004002u; return; } + if (!surface->interfaces[index]) surface->interfaces[index] = create_interface(index + 3); + ++surface->references[index]; + MEM32(output) = surface->interfaces[index]; + g_eax = 0; +} + +static void surface_add_reference(void) +{ + uint32_t address = MEM32(g_esp + 4); + struct surface_object *surface = find_surface(address); + g_eax = ++surface->references[address == surface->interfaces[0] ? 0 : 1]; + g_esp += 8; +} + +static void surface_release_reference(void) +{ + uint32_t address = MEM32(g_esp + 4); + struct surface_object *surface = find_surface(address); + unsigned index = address == surface->interfaces[0] ? 0 : 1; + g_eax = --surface->references[index]; + if (!g_eax) { + guest_free(surface->interfaces[index]); + surface->interfaces[index] = 0; + } + if (!surface->references[0] && !surface->references[1]) { + struct surface_object **link = &surfaces; + while (*link != surface) link = &(*link)->next; + *link = surface->next; + gl_target_destroy(surface->target); + guest_free(surface->pixels); + release_draw(surface->owner, 1); + free(surface); + } + g_esp += 8; +} + +static void surface_pixel_format(uint32_t output) +{ + const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0}; + memcpy((void *)ADDR(output), format, sizeof(format)); +} + +static void describe_surface(struct surface_object *surface, uint32_t output) +{ + memset((void *)ADDR(output), 0, 124); + MEM32(output) = 124; + MEM32(output + 4) = 0x100f; + MEM32(output + 8) = surface->height; + MEM32(output + 12) = surface->width; + MEM32(output + 16) = surface->width * 4; + surface_pixel_format(output + 72); + MEM32(output + 104) = surface->caps; +} + +static void surface_description(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output && MEM32(output) == 124) { describe_surface(surface, output); g_eax = 0; } + g_esp += 12; +} + +static void surface_get_pixel_format(void) +{ + find_surface(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output && MEM32(output) == 32) { surface_pixel_format(output); g_eax = 0; } + g_esp += 12; +} + +static void surface_owner(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { MEM32(output) = interface_pointer(surface->owner, 1); g_eax = 0; } + g_esp += 12; +} + +static void surface_not_lost(void) +{ + find_surface(MEM32(g_esp + 4)); + /* Windowed FBOs survive focus and size changes in this persistent context. */ + g_eax = 0; g_esp += 8; } +static void swap_surface_channels(struct surface_object *surface) +{ + unsigned char *pixels = (void *)ADDR(surface->pixels); + for (size_t i = 0; i < (size_t)surface->width * surface->height * 4; i += 4) { + unsigned char red = pixels[i]; + pixels[i] = pixels[i + 2]; + pixels[i + 2] = red; + } +} + +static void surface_lock(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t rect = MEM32(g_esp + 8), output = MEM32(g_esp + 12); + uint32_t flags = MEM32(g_esp + 16), event = MEM32(g_esp + 20); + g_esp += 24; + g_eax = 0x80070057u; + if (!output || MEM32(output) != 124) return; + if (rect || event || (flags & ~0x31u)) { + fprintf(stderr, "Unsupported surface lock rect=%08x flags=%08x event=%08x\n", rect, flags, event); + exit(3); + } + if (surface->locked) { g_eax = 0x887601aeu; return; } + if (!surface->pixels) surface->pixels = guest_malloc(surface->width * surface->height * 4); + if (!surface->pixels) { g_eax = 0x8007000eu; return; } + gl_target_read(surface->target, (void *)ADDR(surface->pixels)); + swap_surface_channels(surface); + surface->locked = 1; + surface->lock_flags = flags; + describe_surface(surface, output); + MEM32(output + 36) = surface->pixels; + g_eax = 0; +} + +static void surface_unlock(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t rect = MEM32(g_esp + 8); + g_esp += 12; + if (rect) { fprintf(stderr, "Partial surface unlock is not implemented\n"); exit(3); } + if (!surface->locked) { g_eax = 0x88760248u; return; } + if (!(surface->lock_flags & 0x10)) { + swap_surface_channels(surface); + gl_target_write(surface->target, (void *)ADDR(surface->pixels)); + swap_surface_channels(surface); + } + surface->locked = 0; + g_eax = 0; +} + static void unavailable_method(void) { - const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7"}; + const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface"}; fprintf(stderr, "Unimplemented %s method slot %u caller=%08x\n", names[current_interface], current_slot, g_cur_func); exit(3); @@ -369,14 +592,29 @@ static void unavailable_method(void) recomp_func_t linux_graphics_virtual(uint32_t address) { - if (address < 0xf3000000u || address >= 0xf3000300u || (address & 3)) return NULL; + if (address < 0xf3000000u || address >= 0xf3000500u || (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 >= 3) { + if (current_slot == 0) return surface_query_interface; + if (current_slot == 1) return surface_add_reference; + if (current_slot == 2) return surface_release_reference; + if (current_interface == 3) { + if (current_slot == 21) return surface_get_pixel_format; + if (current_slot == 22) return surface_description; + if (current_slot == 24 || current_slot == 27) return surface_not_lost; + if (current_slot == 25) return surface_lock; + if (current_slot == 32) return surface_unlock; + if (current_slot == 36) return surface_owner; + } + return unavailable_method; + } if (current_slot == 0) return query_interface; 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 == 1 && current_slot == 6) return create_surface; if (current_interface == 1 && current_slot == 8) return enumerate_display_modes; if (current_slot == 11) return get_caps; if (current_slot == 20) return set_cooperative_level; diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -513,6 +513,29 @@ static void direct3d_enumerates_the_native_renderer(void) } #ifndef _WIN32 +static void uploads_preserve_depth_and_top_down_pixels(void) +{ + struct gl_target *target = gl_target_create(2, 2); + assert(target); + gl_target_clear(target, 0, 0.0f); + const unsigned char input[] = {10, 20, 30, 40, 50, 60, 70, 80, + 90, 100, 110, 120, 130, 140, 150, 160}; + struct gl_vertex vertices[] = { + {{-1, -1, 0.5f, 1}, {1, 1, 1, 1}}, + {{3, -1, 0.5f, 1}, {1, 1, 1, 1}}, + {{-1, 3, 0.5f, 1}, {1, 1, 1, 1}} + }; + const uint16_t indices[] = {0, 1, 2}; + unsigned char output[16]; + + gl_target_write(target, input); + gl_target_draw(target, vertices, 3, indices, 3); + gl_target_read(target, output); + + assert(!memcmp(input, output, sizeof(input))); + gl_target_destroy(target); +} + static void indexed_triangles_preserve_depth_and_target_contents(void) { struct gl_target *first = gl_target_create(32, 32), *second = gl_target_create(32, 32); @@ -614,6 +637,7 @@ int main(void) direct3d_enumerates_the_native_renderer(); #ifndef _WIN32 indexed_triangles_preserve_depth_and_target_contents(); + uploads_preserve_depth_and_top_down_pixels(); native_context_renders_and_reads_pixels(); #endif puts("DirectDraw: adapter/mode callbacks, reentrancy, capabilities, DirectDrawCreateEx, windowed mode, device information, DirectDraw/Direct3D interface lifetime and ABI passed"); diff --git a/decompiled/tests/surfaces.c b/decompiled/tests/surfaces.c @@ -0,0 +1,203 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#ifdef _WIN32 +#include <windows.h> +#include <ddraw.h> +#include <stddef.h> +_Static_assert(sizeof(DDSURFACEDESC2) == 124, "guest surface descriptor size"); +_Static_assert(offsetof(DDSURFACEDESC2, lpSurface) == 36, "guest pixels offset"); +static uint32_t scratch[256]; +#else +#include "linux_graphics.h" +#include "guest_memory.h" +#include <SDL2/SDL.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 arguments[5] = {object}; + va_list list; + va_start(list, count); + for (unsigned i = 1; i < count; ++i) arguments[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, arguments[1]); + if (count == 3) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2]); + if (count == 4) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3]); + return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3], arguments[4]); +#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, arguments[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 create_draw(void) +{ + static const uint32_t iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; + uint32_t output = workspace(), guid = workspace() + 16; + memcpy(memory(guid), 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, output, guid, 0)); +#else + uint32_t stack = g_esp; + PUSH32(g_esp, 0); PUSH32(g_esp, guid); PUSH32(g_esp, output); PUSH32(g_esp, 0); + PUSH32(g_esp, RECOMP_RETADDR); + linux_graphics_lookup("DirectDrawCreateEx")(); + assert(!g_eax && g_esp == stack); +#endif + uint32_t object = *memory(output); + assert(!invoke(object, 20, 3, 0u, 0x808u)); + return object; +} + +static uint32_t create_surface(uint32_t draw, uint32_t caps) +{ + uint32_t desc = workspace() + 128; + memset(memory(desc), 0, 124); + *memory(desc) = 124; + *memory(desc + 4) = caps == 0x200 ? 1 : 7; + if (caps != 0x200) { *memory(desc + 8) = 16; *memory(desc + 12) = 32; } + *memory(desc + 104) = caps; + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + return *memory(workspace()); +} + +static void surface_lifetime_and_description(void) +{ + uint32_t draw = create_draw(), primary = create_surface(draw, 0x200); + uint32_t desc = workspace() + 128; + uint32_t duplicate = invoke(draw, 6, 4, desc, workspace(), 0u); + uint32_t offscreen = create_surface(draw, 0x2040); + memset(memory(desc), 0xa5, 128); + *memory(desc) = 124; + + uint32_t result = invoke(offscreen, 22, 2, desc); + uint32_t owner_result = invoke(offscreen, 36, 2, workspace()); + uint32_t owner = *memory(workspace()); + + assert(duplicate == 0x88760234u && !result && !owner_result && owner == draw); + const uint32_t expected[31] = { + 124, 0x100f, 16, 32, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0, 0x10006040, 0, 0, 0, 0 + }; + assert(!memcmp(memory(desc), expected, sizeof(expected))); + assert(*memory(desc + 124) == 0xa5a5a5a5); + uint32_t format = workspace() + 256; + memset(memory(format), 0xa5, 36); + *memory(format) = 32; + assert(!invoke(offscreen, 21, 2, format)); + assert(!memcmp(memory(format), expected + 18, 32)); + assert(*memory(format + 32) == 0xa5a5a5a5); + assert(invoke(owner, 2, 1) == 3); + assert(!invoke(primary, 22, 2, desc)); + assert(*memory(desc + 104) == 0x1000c200); +#ifdef _WIN32 + assert(*memory(desc + 12) == (uint32_t)GetSystemMetrics(SM_CXSCREEN)); + assert(*memory(desc + 8) == (uint32_t)GetSystemMetrics(SM_CYSCREEN)); +#else + SDL_DisplayMode mode; + assert(!SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(linux_graphics_window()), &mode)); + assert(*memory(desc + 12) == (uint32_t)mode.w && *memory(desc + 8) == (uint32_t)mode.h); +#endif + + /* IUnknown is the older surface interface and retains the owner separately. */ + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + memcpy(memory(workspace() + 16), unknown, 16); + assert(!invoke(offscreen, 0, 3, workspace() + 16, workspace())); + uint32_t held = *memory(workspace()); + assert(held != offscreen); + assert(!invoke(held, 0, 3, workspace() + 16, workspace())); + assert(*memory(workspace()) == held && invoke(held, 2, 1) == 1); + const uint32_t surface7[] = {0x06675a80, 0x11d23b9b, 0x60002fb9, 0x5bea9797}; + memcpy(memory(workspace() + 16), surface7, 16); + assert(!invoke(held, 0, 3, workspace() + 16, workspace())); + assert(*memory(workspace()) == offscreen && invoke(offscreen, 2, 1) == 1); + assert(invoke(held, 1, 1) == 2 && invoke(held, 2, 1) == 1); + assert(!invoke(offscreen, 2, 1)); + assert(invoke(draw, 1, 1) == 4 && invoke(draw, 2, 1) == 3); + assert(!invoke(held, 2, 1)); + assert(invoke(draw, 1, 1) == 3 && invoke(draw, 2, 1) == 2); + assert(!invoke(primary, 2, 1)); + /* Releasing the primary permits a new one. Surfaces keep a released owner alive. */ + primary = create_surface(draw, 0x200); + assert(invoke(draw, 2, 1) == 1); + assert(!invoke(primary, 36, 2, workspace()) && *memory(workspace()) == draw); + assert(invoke(draw, 2, 1) == 1); + assert(!invoke(primary, 2, 1)); +} + +static void surface_pixels_round_trip(void) +{ + uint32_t draw = create_draw(), surface = create_surface(draw, 0x2040); + uint32_t desc = workspace() + 128, info = workspace() + 256; + *memory(desc) = 124; + assert(!invoke(surface, 25, 5, 0u, desc, 0x21u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert(pixels && pitch == 128 && *memory(desc + 4) == 0x100f); + for (unsigned y = 0; y < 16; ++y) + for (unsigned x = 0; x < 32; ++x) + *memory(pixels + y * pitch + x * 4) = (y << 16) | (x << 8) | (x ^ y); + *memory(info) = 124; + assert(!invoke(surface, 22, 2, info) && !*memory(info + 36)); + + assert(!invoke(surface, 32, 2, 0u)); + uint32_t unlocked = invoke(surface, 32, 2, 0u); + assert(!invoke(surface, 25, 5, 0u, desc, 0x11u, 0u)); + pixels = *memory(desc + 36); + pitch = *memory(desc + 16); + + assert(unlocked == 0x88760248u); + for (unsigned y = 0; y < 16; ++y) + for (unsigned x = 0; x < 32; ++x) + assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == ((y << 16) | (x << 8) | (x ^ y))); + assert(!invoke(surface, 32, 2, 0u)); + assert(!invoke(surface, 24, 1) && !invoke(surface, 27, 1)); + assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1)); +} + +int main(void) +{ +#ifndef _WIN32 + assert(guest_map("surface test", 0x50000000, 0x10000)); + g_esp = 0x5000f000; +#endif + surface_lifetime_and_description(); + surface_pixels_round_trip(); + puts("surfaces: primary/offscreen descriptions, COM ownership and 512 pixel round trips passed"); + return 0; +} diff --git a/tools/reconstruction/test_portable_surfaces.py b/tools/reconstruction/test_portable_surfaces.py @@ -0,0 +1,26 @@ +"""Compare native guest surfaces with the Windows x86 DirectDraw implementation.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-surfaces" +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/surfaces.c", "-o", str(reference), "-luser32", +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference)], text=True, timeout=30) +linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] +linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + [ + "gcc", "-O2", "-Wall", "-Wextra", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", + "tests/surfaces.c", "runtime/linux_graphics.c", "runtime/gl_renderer.c", "runtime/guest_heap.c", + "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-o", linux_output, +], check=True) +actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_output], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip())