aorecomp

Anarchy Online classic in the browser

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


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

Implement native window clippers and attached depth buffers

Diffstat:
Mdecompiled/Makefile.linux | 4++--
Mdecompiled/runtime/gl_renderer.c | 55++++++++++++++++++++++++++++++++++++++++++++++++-------
Mdecompiled/runtime/gl_renderer.h | 4++++
Mdecompiled/runtime/linux_graphics.c | 306+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mdecompiled/runtime/linux_window.c | 5+++++
Mdecompiled/runtime/linux_window.h | 2++
Mdecompiled/tests/graphics.c | 37++++++++++++++++++++++++++++++++++++-
Mdecompiled/tests/surfaces.c | 177++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mtools/reconstruction/test_portable_graphics.py | 2+-
Mtools/reconstruction/test_portable_surfaces.py | 2+-
10 files changed, 558 insertions(+), 36 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -136,7 +136,7 @@ build/linux/create-directory: tests/create_directory.c runtime/linux_platform.c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ -build/linux/graphics: tests/graphics.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 +build/linux/graphics: tests/graphics.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 $@ @@ -178,7 +178,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./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 +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) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -o $@ diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -5,11 +5,17 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <assert.h> static GLuint program, vertex_array, vertex_buffer, index_buffer; +struct gl_depth { + unsigned width, height, references; + GLuint buffer; +}; struct gl_target { unsigned width, height; - GLuint framebuffer, color, depth; + GLuint framebuffer, color; + struct gl_depth *depth; }; static void check_graphics(const char *operation) @@ -88,6 +94,41 @@ void gl_renderer_shutdown(void) index_buffer = vertex_buffer = vertex_array = program = 0; } +struct gl_depth *gl_depth_create(unsigned width, unsigned height, unsigned bits) +{ + assert(bits == 16 || bits == 24); + struct gl_depth *depth = calloc(1, sizeof(*depth)); + if (!depth) return NULL; + depth->width = width; + depth->height = height; + depth->references = 1; + glGenRenderbuffers(1, &depth->buffer); + glBindRenderbuffer(GL_RENDERBUFFER, depth->buffer); + glRenderbufferStorage(GL_RENDERBUFFER, bits == 16 ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24, width, height); + check_graphics("depth allocation"); + return depth; +} + +void gl_depth_destroy(struct gl_depth *depth) +{ + if (--depth->references) return; + glDeleteRenderbuffers(1, &depth->buffer); + free(depth); +} + +void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth) +{ + assert(!depth || (depth->width == target->width && depth->height == target->height)); + if (depth) ++depth->references; + if (target->depth) gl_depth_destroy(target->depth); + target->depth = depth; + glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth ? depth->buffer : 0); + assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + check_graphics("depth attachment"); +} + struct gl_target *gl_target_create(unsigned width, unsigned height) { struct gl_target *target = calloc(1, sizeof(*target)); @@ -101,13 +142,12 @@ struct gl_target *gl_target_create(unsigned width, unsigned height) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glGenRenderbuffers(1, &target->depth); - glBindRenderbuffer(GL_RENDERBUFFER, target->depth); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); + target->depth = gl_depth_create(width, height, 24); + if (!target->depth) { gl_target_destroy(target); return NULL; } glGenFramebuffers(1, &target->framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->color, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target->depth); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target->depth->buffer); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); glBindFramebuffer(GL_FRAMEBUFFER, 0); if (status != GL_FRAMEBUFFER_COMPLETE) { @@ -122,7 +162,7 @@ struct gl_target *gl_target_create(unsigned width, unsigned height) void gl_target_destroy(struct gl_target *target) { glDeleteFramebuffers(1, &target->framebuffer); - glDeleteRenderbuffers(1, &target->depth); + if (target->depth) gl_depth_destroy(target->depth); glDeleteTextures(1, &target->color); free(target); } @@ -149,7 +189,8 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, glDisable(GL_SCISSOR_TEST); glDisable(GL_BLEND); glDisable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); + if (target->depth) glEnable(GL_DEPTH_TEST); + else glDisable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -4,6 +4,7 @@ /* These are host objects. Guest addresses are translated by the API adapter. */ struct gl_target; +struct gl_depth; struct gl_vertex { float position[4]; /* D3D clip coordinates: -w <= x,y <= w and 0 <= z <= w. */ float color[4]; @@ -13,6 +14,9 @@ int gl_renderer_initialize(void); void gl_renderer_shutdown(void); struct gl_target *gl_target_create(unsigned width, unsigned height); void gl_target_destroy(struct gl_target *target); +struct gl_depth *gl_depth_create(unsigned width, unsigned height, unsigned bits); +void gl_depth_destroy(struct gl_depth *depth); +void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth); 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); diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -1,4 +1,5 @@ #include "linux_graphics.h" +#include "linux_window.h" #include "guest_heap.h" #include "gl_renderer.h" #include <SDL2/SDL.h> @@ -11,16 +12,24 @@ static struct draw_object { uint32_t interfaces[3], references[2]; struct draw_object *next; } *objects; +static struct clipper_object { + uint32_t interface, references, window; + struct draw_object *owner; + struct clipper_object *next; +} *clippers; static struct surface_object { uint32_t interfaces[2], references[2]; - uint32_t width, height, caps, pixels, lock_flags; + uint32_t width, height, caps, pixels, lock_flags, depth_bits; int locked; struct draw_object *owner; + struct clipper_object *clipper; struct gl_target *target; + struct gl_depth *depth; + struct surface_object *attached; struct surface_object *next; } *surfaces; -static uint32_t vtables[5]; -static const unsigned interface_slots[] = {23, 30, 8, 49, 36}; +static uint32_t vtables[6]; +static const unsigned interface_slots[] = {23, 30, 8, 49, 36, 9}; static unsigned current_interface, current_slot; static void close_graphics(void) @@ -297,7 +306,7 @@ static void enumerate_devices(void) MEM32(record + 72) = 8; /* Depth comparison: LESSEQUAL */ MEM32(record + 88) = 8; /* Gouraud RGB interpolation */ MEM32(record + 116) = 0x100; /* DDBD_32 color */ - MEM32(record + 120) = 0x200; /* DDBD_24 depth */ + MEM32(record + 120) = 0x600; /* DDBD_16 | DDBD_24 depth */ /* HAL denotes rasterization without hardware transform-and-lighting. * Texturing, lighting, W buffering and other unimplemented features stay zero. */ static const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; @@ -376,6 +385,138 @@ static void release_reference(void) g_esp += 8; } +static struct clipper_object *find_clipper(uint32_t address) +{ + for (struct clipper_object *clipper = clippers; clipper; clipper = clipper->next) + if (clipper->interface == address) return clipper; + fprintf(stderr, "Invalid guest clipper interface %08x\n", address); + abort(); +} + +static uint32_t release_clipper(struct clipper_object *clipper) +{ + uint32_t remaining = --clipper->references; + if (!remaining) { + struct clipper_object **link = &clippers; + while (*link != clipper) link = &(*link)->next; + *link = clipper->next; + release_draw(clipper->owner, 1); + guest_free(clipper->interface); + free(clipper); + } + return remaining; +} + +static void create_clipper(void) +{ + struct draw_object *owner = find_object(MEM32(g_esp + 4)); + uint32_t flags = MEM32(g_esp + 8), output = MEM32(g_esp + 12), outer = MEM32(g_esp + 16); + g_esp += 20; + g_eax = 0x80070057u; + if (!output || flags || outer) return; + struct clipper_object *clipper = calloc(1, sizeof(*clipper)); + if (!clipper) abort(); + clipper->interface = create_interface(5); + clipper->references = 1; + clipper->owner = owner; + ++owner->references[1]; + clipper->next = clippers; + clippers = clipper; + MEM32(output) = clipper->interface; + g_eax = 0; +} + +static void clipper_query_interface(void) +{ + struct clipper_object *clipper = find_clipper(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 clipper_iid[] = {0x6c14db85, 0x11cea733, 0x200021a5, 0x60e50baf}; + 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), clipper_iid, 16)) return; + MEM32(output) = clipper->interface; + ++clipper->references; + g_eax = 0; +} + +static void clipper_add_reference(void) +{ + g_eax = ++find_clipper(MEM32(g_esp + 4))->references; + g_esp += 8; +} + +static void clipper_release_reference(void) +{ + g_eax = release_clipper(find_clipper(MEM32(g_esp + 4))); + g_esp += 8; +} + +static void clipper_set_window(void) +{ + struct clipper_object *clipper = find_clipper(MEM32(g_esp + 4)); + uint32_t flags = MEM32(g_esp + 8), handle = MEM32(g_esp + 12); + g_eax = 0x80070057u; + if (!flags && (!handle || linux_window_native(handle))) { clipper->window = handle; g_eax = 0; } + g_esp += 16; +} + +static void clipper_get_window(void) +{ + struct clipper_object *clipper = find_clipper(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { MEM32(output) = clipper->window; g_eax = 0; } + g_esp += 12; +} + +static void clipper_get_list(void) +{ + struct clipper_object *clipper = find_clipper(MEM32(g_esp + 4)); + uint32_t filter = MEM32(g_esp + 8), output = MEM32(g_esp + 12), size = MEM32(g_esp + 16); + g_esp += 20; + g_eax = 0x80070057u; + if (!size) return; + if (output && MEM32(size) >= 12) MEM32(output + 8) = 0; + if (!clipper->window) { g_eax = 0x887600cdu; return; } + SDL_Window *native = linux_window_native(clipper->window); + if (!native) { g_eax = 0x80004005u; return; } + + /* SDL/compositor owns occlusion; the virtual primary clips to our client area. */ + SDL_Rect bounds = {0}; + if (!(SDL_GetWindowFlags(native) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED))) { + SDL_GetWindowPosition(native, &bounds.x, &bounds.y); + SDL_GetWindowSize(native, &bounds.w, &bounds.h); + } + if (filter) { + SDL_Rect subset = {(int32_t)MEM32(filter), (int32_t)MEM32(filter + 4), + (int32_t)(MEM32(filter + 8) - MEM32(filter)), (int32_t)(MEM32(filter + 12) - MEM32(filter + 4))}; + SDL_Rect intersection; + if (SDL_IntersectRect(&bounds, &subset, &intersection)) bounds = intersection; + else bounds = (SDL_Rect){0}; + } + unsigned count = bounds.w > 0 && bounds.h > 0 ? 1 : 0; + uint32_t required = 32 + count * 16; + if (!output) { MEM32(size) = required; g_eax = 0; return; } + if (MEM32(size) < required) { g_eax = 0x88760236u; return; } + memset((void *)ADDR(output), 0, required); + MEM32(output) = 32; + MEM32(output + 4) = 1; /* RDH_RECTANGLES */ + MEM32(output + 8) = count; + MEM32(output + 12) = count * 16; + if (count) { + MEM32(output + 16) = bounds.x; + MEM32(output + 20) = bounds.y; + MEM32(output + 24) = bounds.x + bounds.w; + MEM32(output + 28) = bounds.y + bounds.h; + memcpy((void *)ADDR(output + 32), (void *)ADDR(output + 16), 16); + } + g_eax = 0; +} + static struct surface_object *find_surface(uint32_t address) { for (struct surface_object *surface = surfaces; surface; surface = surface->next) @@ -395,11 +536,23 @@ static void create_surface(void) 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) || + int depth = flags == 0x1007 && caps == 0x24000; + if (outer || !((flags == 1 && caps == 0x200) || depth || (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 (depth) { + const uint32_t format16[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0}; + const uint32_t format24[] = {32, 0x400, 0, 24, 0, 0xffffff, 0, 0}; + const uint32_t format32[] = {32, 0x400, 0, 32, 0, 0xffffffffu, 0, 0}; + /* Match the unsupported format response and allow AO's depth fallback. */ + if (!memcmp((void *)ADDR(desc + 72), format32, 32)) { g_eax = 0x80004001u; return; } + if (memcmp((void *)ADDR(desc + 72), format24, 32) && memcmp((void *)ADDR(desc + 72), format16, 32)) { + fprintf(stderr, "Unsupported depth pixel format flags=%08x bits=%u\n", MEM32(desc + 76), MEM32(desc + 84)); + 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; } @@ -415,8 +568,12 @@ static void create_surface(void) (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; } + if (depth) { + surface->depth_bits = MEM32(desc + 84); + surface->depth = gl_depth_create(width, height, surface->depth_bits); + } + else surface->target = gl_target_create(width, height); + if (!surface->target && !surface->depth) { free(surface); g_eax = 0x8007000eu; return; } surface->width = width; surface->height = height; surface->caps = caps | 0x10004000u; /* Local video memory. */ @@ -457,13 +614,10 @@ static void surface_add_reference(void) g_esp += 8; } -static void surface_release_reference(void) +static uint32_t release_surface(struct surface_object *surface, unsigned index) { - 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) { + uint32_t remaining = --surface->references[index]; + if (!remaining) { guest_free(surface->interfaces[index]); surface->interfaces[index] = 0; } @@ -471,18 +625,31 @@ static void surface_release_reference(void) struct surface_object **link = &surfaces; while (*link != surface) link = &(*link)->next; *link = surface->next; - gl_target_destroy(surface->target); + if (surface->target) gl_target_destroy(surface->target); + if (surface->depth) gl_depth_destroy(surface->depth); + if (surface->attached) release_surface(surface->attached, 0); + if (surface->clipper) release_clipper(surface->clipper); guest_free(surface->pixels); release_draw(surface->owner, 1); free(surface); } + return remaining; +} + +static void surface_release_reference(void) +{ + uint32_t address = MEM32(g_esp + 4); + struct surface_object *surface = find_surface(address); + g_eax = release_surface(surface, address == surface->interfaces[0] ? 0 : 1); g_esp += 8; } -static void surface_pixel_format(uint32_t output) +static void surface_pixel_format(struct surface_object *surface, uint32_t output) { const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0}; - memcpy((void *)ADDR(output), format, sizeof(format)); + const uint32_t depth[] = {32, 0x400, 0, surface->depth_bits, 0, + surface->depth_bits == 16 ? 0xffff : 0xffffff, 0, 0}; + memcpy((void *)ADDR(output), surface->depth ? depth : format, sizeof(format)); } static void describe_surface(struct surface_object *surface, uint32_t output) @@ -493,7 +660,7 @@ static void describe_surface(struct surface_object *surface, uint32_t output) MEM32(output + 8) = surface->height; MEM32(output + 12) = surface->width; MEM32(output + 16) = surface->width * 4; - surface_pixel_format(output + 72); + surface_pixel_format(surface, output + 72); MEM32(output + 104) = surface->caps; } @@ -508,10 +675,10 @@ static void surface_description(void) static void surface_get_pixel_format(void) { - find_surface(MEM32(g_esp + 4)); + struct surface_object *surface = 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; } + if (output && MEM32(output) == 32) { surface_pixel_format(surface, output); g_eax = 0; } g_esp += 12; } @@ -524,6 +691,38 @@ static void surface_owner(void) g_esp += 12; } +static void surface_set_clipper(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t address = MEM32(g_esp + 8); + struct clipper_object *clipper = address ? find_clipper(address) : NULL; + g_eax = 0; + if (!clipper && !surface->clipper) g_eax = 0x88760238u; + if (clipper != surface->clipper) { + if (clipper) ++clipper->references; + if (surface->clipper) release_clipper(surface->clipper); + surface->clipper = clipper; + } + g_esp += 12; +} + +static void surface_get_clipper(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) = 0; + g_eax = 0x88760238u; + if (surface->clipper) { + MEM32(output) = surface->clipper->interface; + ++surface->clipper->references; + g_eax = 0; + } + } + g_esp += 12; +} + static void surface_not_lost(void) { find_surface(MEM32(g_esp + 4)); @@ -532,6 +731,54 @@ static void surface_not_lost(void) g_esp += 8; } +static void surface_attach(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + struct surface_object *depth = find_surface(MEM32(g_esp + 8)); + g_esp += 12; + if (!surface->target || !depth->depth || surface->width != depth->width || surface->height != depth->height) { + fprintf(stderr, "Unsupported surface attachment\n"); exit(3); + } + if (surface->attached) { g_eax = 0x8876019au; return; } + surface->attached = depth; + ++depth->references[0]; + gl_target_set_depth(surface->target, depth->depth); + g_eax = 0; +} + +static void surface_get_attached(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t caps = MEM32(g_esp + 8), output = MEM32(g_esp + 12); + g_esp += 16; + g_eax = 0x80070057u; + if (!caps || !output) return; + if (MEM32(caps) != 0x20000 || MEM32(caps + 4) || MEM32(caps + 8) || MEM32(caps + 12)) { + fprintf(stderr, "Unsupported attachment query\n"); exit(3); + } + MEM32(output) = 0; + g_eax = 0x887600ffu; /* DDERR_NOTFOUND */ + if (surface->attached) { + MEM32(output) = surface->attached->interfaces[0]; + ++surface->attached->references[0]; + g_eax = 0; + } +} + +static void surface_detach(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t flags = MEM32(g_esp + 8), address = MEM32(g_esp + 12); + g_esp += 16; + g_eax = 0x80070057u; + if (flags) return; + if (!surface->attached || surface->attached->interfaces[0] != address) { g_eax = 0x887601ccu; return; } + gl_target_set_depth(surface->target, NULL); + release_surface(surface->attached, 0); + surface->attached = NULL; + g_eax = 0; +} + static void swap_surface_channels(struct surface_object *surface) { unsigned char *pixels = (void *)ADDR(surface->pixels); @@ -550,7 +797,7 @@ static void surface_lock(void) g_esp += 24; g_eax = 0x80070057u; if (!output || MEM32(output) != 124) return; - if (rect || event || (flags & ~0x31u)) { + if (surface->depth || rect || event || (flags & ~0x31u)) { fprintf(stderr, "Unsupported surface lock rect=%08x flags=%08x event=%08x\n", rect, flags, event); exit(3); } @@ -584,7 +831,7 @@ static void surface_unlock(void) static void unavailable_method(void) { - const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface"}; + const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper"}; fprintf(stderr, "Unimplemented %s method slot %u caller=%08x\n", names[current_interface], current_slot, g_cur_func); exit(3); @@ -592,19 +839,33 @@ static void unavailable_method(void) recomp_func_t linux_graphics_virtual(uint32_t address) { - if (address < 0xf3000000u || address >= 0xf3000500u || (address & 3)) return NULL; + if (address < 0xf3000000u || address >= 0xf3000600u || (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 == 5) { + if (current_slot == 0) return clipper_query_interface; + if (current_slot == 1) return clipper_add_reference; + if (current_slot == 2) return clipper_release_reference; + if (current_slot == 3) return clipper_get_list; + if (current_slot == 4) return clipper_get_window; + if (current_slot == 8) return clipper_set_window; + return unavailable_method; + } 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 == 3) return surface_attach; + if (current_slot == 8) return surface_detach; + if (current_slot == 12) return surface_get_attached; + if (current_slot == 15) return surface_get_clipper; 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 == 28) return surface_set_clipper; if (current_slot == 32) return surface_unlock; if (current_slot == 36) return surface_owner; } @@ -614,6 +875,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 == 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; if (current_slot == 11) return get_caps; diff --git a/decompiled/runtime/linux_window.c b/decompiled/runtime/linux_window.c @@ -22,6 +22,11 @@ static struct display_context { } *display_contexts; static uint32_t next_context = 0xf4300000u; +struct SDL_Window *linux_window_native(uint32_t handle) +{ + return handle && handle == client.handle ? linux_graphics_window() : NULL; +} + static void unsupported(const char *operation, uint32_t value) { fprintf(stderr, "Unimplemented native window %s: %08x caller=%08x\n", diff --git a/decompiled/runtime/linux_window.h b/decompiled/runtime/linux_window.h @@ -3,5 +3,7 @@ #include "recomp_types.h" recomp_func_t linux_window_lookup(const char *dll, const char *name); +struct SDL_Window; +struct SDL_Window *linux_window_native(uint32_t handle); #endif diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -456,7 +456,7 @@ static int inspect_device(uint32_t description, uint32_t name, uint32_t caps, ui assert(*memory(caps + 4) == 56 && *memory(caps + 60) == 56); assert(*memory(caps + 116) & 0x100); /* 32-bit color */ #ifndef _WIN32 - assert(*memory(caps + 120) == 0x200); /* Native target's 24-bit depth. */ + assert(*memory(caps + 120) == 0x600); /* Native 16/24-bit depth buffers. */ assert(!strcmp((char *)memory(description), (const char *)glGetString(GL_RENDERER))); assert(!strcmp((char *)memory(name), "Native OpenGL")); assert(*memory(caps) == 0x80000u); @@ -513,6 +513,40 @@ static void direct3d_enumerates_the_native_renderer(void) } #ifndef _WIN32 +static void attached_depth_is_shared_and_retained(void) +{ + struct gl_target *first = gl_target_create(32, 32), *second = gl_target_create(32, 32); + struct gl_depth *depth = gl_depth_create(32, 32, 16); + assert(first && second && depth); + gl_target_set_depth(first, depth); + gl_target_set_depth(second, depth); + gl_depth_destroy(depth); + gl_target_clear(first, 0xff000000, 1.0f); + gl_target_clear(second, 0xff0000ff, 1.0f); + struct gl_vertex vertices[] = { + {{-0.75f, -0.75f, 0.25f, 1}, {1, 1, 1, 1}}, + {{0.75f, -0.75f, 0.25f, 1}, {1, 1, 1, 1}}, + {{0, 0.75f, 0.25f, 1}, {1, 1, 1, 1}} + }; + const uint16_t indices[] = {0, 1, 2}; + unsigned char pixels[32 * 32 * 4]; + + gl_target_draw(first, vertices, 3, indices, 3); + gl_target_set_depth(first, NULL); + gl_target_destroy(first); + for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = 0.75f; + gl_target_draw(second, vertices, 3, indices, 3); + gl_target_read(second, pixels); + + unsigned center = (16 * 32 + 16) * 4; + assert(pixels[center] == 0 && pixels[center + 1] == 0 && pixels[center + 2] == 255); + gl_target_set_depth(second, NULL); + gl_target_draw(second, vertices, 3, indices, 3); + gl_target_read(second, pixels); + assert(pixels[center] == 255 && pixels[center + 1] == 255 && pixels[center + 2] == 255); + gl_target_destroy(second); +} + static void uploads_preserve_depth_and_top_down_pixels(void) { struct gl_target *target = gl_target_create(2, 2); @@ -638,6 +672,7 @@ int main(void) #ifndef _WIN32 indexed_triangles_preserve_depth_and_target_contents(); uploads_preserve_depth_and_top_down_pixels(); + attached_depth_is_shared_and_retained(); 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 @@ -12,9 +12,14 @@ _Static_assert(offsetof(DDSURFACEDESC2, lpSurface) == 36, "guest pixels offset") static uint32_t scratch[256]; #else #include "linux_graphics.h" +#include "linux_window.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(uint32_t address) +{ + assert(address == 0x11110000u); + return linux_window_lookup("user32.dll", "DefWindowProcA"); +} recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } #endif @@ -190,14 +195,182 @@ static void surface_pixels_round_trip(void) assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1)); } +static void clipper_attachment_retains_objects(void) +{ + uint32_t draw = create_draw(), surface = create_surface(draw, 0x2040); + assert(!invoke(draw, 4, 4, 0u, workspace(), 0u)); + uint32_t first = *memory(workspace()); + assert(!invoke(draw, 4, 4, 0u, workspace(), 0u)); + uint32_t second = *memory(workspace()); + assert(invoke(draw, 1, 1) == 5 && invoke(draw, 2, 1) == 4); + assert(invoke(surface, 15, 2, workspace()) == 0x88760238u && !*memory(workspace())); + assert(invoke(surface, 28, 2, 0u) == 0x88760238u); + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + memcpy(memory(workspace() + 16), unknown, 16); + assert(!invoke(first, 0, 3, workspace() + 16, workspace())); + assert(*memory(workspace()) == first && invoke(first, 2, 1) == 1); + + assert(!invoke(surface, 28, 2, first)); + assert(!invoke(surface, 28, 2, first)); + assert(!invoke(surface, 15, 2, workspace())); + uint32_t attached = *memory(workspace()); + + assert(attached == first && invoke(attached, 2, 1) == 2); + assert(!invoke(surface, 28, 2, second)); + assert(!invoke(first, 2, 1)); + assert(invoke(second, 2, 1) == 1); + assert(invoke(draw, 2, 1) == 2); + assert(!invoke(surface, 2, 1)); +} + +static void depth_surface_attachment_and_lifetime(void) +{ + uint32_t draw = create_draw(), color = create_surface(draw, 0x6040); + uint32_t desc = workspace() + 128, caps = workspace() + 64; + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 0x1007; + *memory(desc + 8) = 16; *memory(desc + 12) = 32; + *memory(desc + 104) = 0x24000; + const uint32_t format32[] = {32, 0x400, 0, 32, 0, 0xffffffffu, 0, 0}; + memcpy(memory(desc + 72), format32, 32); + uint32_t unsupported = invoke(draw, 6, 4, desc, workspace(), 0u); + const uint32_t format24[] = {32, 0x400, 0, 24, 0, 0xffffff, 0, 0}; + memcpy(memory(desc + 72), format24, 32); + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t depth = *memory(workspace()); + assert(!invoke(depth, 22, 2, desc)); + memset(memory(caps), 0, 16); + *memory(caps) = 0x20000; + + assert(!invoke(color, 3, 2, depth)); + uint32_t repeated = invoke(color, 3, 2, depth); + assert(!invoke(color, 12, 3, caps, workspace())); + uint32_t attached = *memory(workspace()); + + assert(unsupported == 0x80004001u && repeated == 0x8876019au); + assert(*memory(desc + 4) == 0x100f && *memory(desc + 16) == 128); + assert(*memory(desc + 104) == 0x10024000); + assert(!memcmp(memory(desc + 72), format24, 32)); + assert(attached == depth && invoke(attached, 2, 1) == 2); + assert(!invoke(color, 8, 3, 0u, depth)); + assert(invoke(depth, 1, 1) == 2 && invoke(depth, 2, 1) == 1); + assert(!invoke(color, 3, 2, depth)); + assert(invoke(depth, 2, 1) == 1); + /* AO falls back directly to 16-bit depth after the 32-bit rejection. */ + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 0x1007; + *memory(desc + 8) = 16; *memory(desc + 12) = 32; + *memory(desc + 104) = 0x24000; + const uint32_t format16[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0}; + memcpy(memory(desc + 72), format16, 32); + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t fallback = *memory(workspace()); + assert(!invoke(fallback, 22, 2, desc)); + assert(!memcmp(memory(desc + 72), format16, 32)); + assert(!invoke(color, 8, 3, 0u, depth)); + assert(!invoke(color, 3, 2, fallback)); + assert(invoke(fallback, 2, 1) == 1); + assert(invoke(draw, 2, 1) == 2); + assert(!invoke(color, 2, 1)); +} + +#ifndef _WIN32 +static uint32_t window_call(const char *name, unsigned count, const uint32_t *arguments) +{ + uint32_t stack = g_esp; + for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]); + PUSH32(g_esp, RECOMP_RETADDR); + linux_window_lookup("user32.dll", name)(); + assert(g_esp == stack); + return g_eax; +} +#endif + +static void window_clipper_regions(void) +{ + uint32_t draw = create_draw(); + assert(!invoke(draw, 4, 4, 0u, workspace(), 0u)); + uint32_t clipper = *memory(workspace()), output = workspace() + 256, size = workspace() + 32; +#ifdef _WIN32 + HWND native = CreateWindowExA(0, "STATIC", "AO clipper test", 0, 20, 30, 320, 200, + NULL, NULL, GetModuleHandleA(NULL), NULL); + uint32_t handle = (uintptr_t)native; +#else + uint32_t record = workspace() + 512, text = workspace() + 560; + memset(memory(record), 0, 40); + *memory(record + 4) = 0x11110000u; + *memory(record + 16) = 1; + *memory(record + 36) = text; + strcpy((char *)memory(text), "AO clipper test"); + uint32_t atom = window_call("RegisterClassA", 1, &record); + assert(atom); + uint32_t arguments[] = {0, atom, text, 0, 20, 30, 320, 200, 0, 0, 1, 0}; + uint32_t handle = window_call("CreateWindowExA", 12, arguments); +#endif + assert(handle); + assert(!invoke(clipper, 4, 2, workspace()) && !*memory(workspace())); + *memory(size) = 0; + assert(invoke(clipper, 3, 4, 0u, 0u, size) == 0x887600cdu && !*memory(size)); + assert(invoke(clipper, 8, 3, 0u, 0x123u) == 0x80070057u); + + assert(!invoke(clipper, 8, 3, 0u, handle)); + assert(!invoke(clipper, 3, 4, 0u, 0u, size)); + uint32_t required = *memory(size); + *memory(size) = 64; + memset(memory(output), 0xa5, 64); + uint32_t result = invoke(clipper, 3, 4, 0u, output, size); + + const uint32_t empty[] = {32, 1, 0, 0, 0, 0, 0, 0}; + assert(!result && required == 32 && *memory(size) == 64); + assert(!memcmp(memory(output), empty, sizeof(empty)) && *memory(output + 32) == 0xa5a5a5a5); + assert(invoke(clipper, 8, 3, 1u, handle) == 0x80070057u); + assert(!invoke(clipper, 4, 2, workspace()) && *memory(workspace()) == handle); + +#ifndef _WIN32 + /* The compositor clips the visible native client; verify moving/filtering live bounds. */ + SDL_Window *native = linux_window_native(handle); + SDL_ShowWindow(native); + SDL_SetWindowPosition(native, 100, 120); + SDL_SetWindowSize(native, 320, 200); + int x, y; + SDL_GetWindowPosition(native, &x, &y); + *memory(size) = 64; + assert(!invoke(clipper, 3, 4, 0u, output, size)); + assert(*memory(output + 8) == 1 && *memory(output + 12) == 16); + assert(*memory(output + 16) == (uint32_t)x && *memory(output + 20) == (uint32_t)y); + assert(*memory(output + 24) == (uint32_t)(x + 320) && *memory(output + 28) == (uint32_t)(y + 200)); + assert(!memcmp(memory(output + 16), memory(output + 32), 16)); + uint32_t filter = workspace() + 64; + *memory(filter) = x + 10; *memory(filter + 4) = y + 20; + *memory(filter + 8) = x + 30; *memory(filter + 12) = y + 40; + assert(!invoke(clipper, 3, 4, filter, output, size)); + assert(!memcmp(memory(output + 16), memory(filter), 16)); + SDL_HideWindow(native); + assert(window_call("DestroyWindow", 1, &handle)); + uint32_t unregister[] = {atom, 1}; + assert(window_call("UnregisterClassA", 2, unregister)); +#else + assert(DestroyWindow(native)); +#endif + *memory(size) = 0; + assert(invoke(clipper, 3, 4, 0u, 0u, size) == 0x80004005u); + assert(!invoke(clipper, 8, 3, 0u, 0u)); + assert(invoke(clipper, 3, 4, 0u, 0u, size) == 0x887600cdu); + assert(!invoke(clipper, 2, 1) && !invoke(draw, 2, 1)); +} + int main(void) { #ifndef _WIN32 assert(guest_map("surface test", 0x50000000, 0x10000)); g_esp = 0x5000f000; + g_fs_base = 0x50000000; #endif surface_lifetime_and_description(); surface_pixels_round_trip(); - puts("surfaces: primary/offscreen descriptions, COM ownership and 512 pixel round trips passed"); + clipper_attachment_retains_objects(); + depth_surface_attachment_and_lifetime(); + window_clipper_regions(); + puts("surfaces: descriptions, COM ownership, clipper regions, depth attachments and 512 pixel round trips passed"); return 0; } diff --git a/tools/reconstruction/test_portable_graphics.py b/tools/reconstruction/test_portable_graphics.py @@ -18,7 +18,7 @@ linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/transla wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] subprocess.run(wsl + [ "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", - "tests/graphics.c", "runtime/linux_graphics.c", "runtime/gl_renderer.c", "runtime/guest_heap.c", + "tests/graphics.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_output, ], check=True) actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_output], text=True, timeout=30) diff --git a/tools/reconstruction/test_portable_surfaces.py b/tools/reconstruction/test_portable_surfaces.py @@ -18,7 +18,7 @@ linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/transla 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", + "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", "-lSDL2", "-lGL", "-o", linux_output, ], check=True) actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_output], text=True, timeout=30)