aorecomp

Anarchy Online classic in the browser

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


commit 5669c87c32fb144616ab589aea2b9d78e20a56e1
parent eae512da46778c84e7aca1f2a07c7f69f6247363
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 03:53:30 +0000

Implement native rectangle locks and staging surface copies

Diffstat:
Mdecompiled/runtime/gl_renderer.c | 11+++++++++++
Mdecompiled/runtime/gl_renderer.h | 1+
Mdecompiled/runtime/linux_graphics.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mdecompiled/tests/graphics.c | 2++
Mdecompiled/tests/surfaces.c | 105++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
5 files changed, 174 insertions(+), 7 deletions(-)

diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -251,6 +251,17 @@ void gl_target_write(struct gl_target *target, const unsigned char *rgba) check_graphics("target upload"); } +void gl_target_blit(struct gl_target *source, struct gl_target *target, const int32_t source_rect[4], const int32_t target_rect[4]) +{ + glBindFramebuffer(GL_READ_FRAMEBUFFER, source->framebuffer); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target->framebuffer); + glDisable(GL_SCISSOR_TEST); + glBlitFramebuffer(source_rect[0], source_rect[1], source_rect[2], source_rect[3], + target_rect[0], target_rect[1], target_rect[2], target_rect[3], GL_COLOR_BUFFER_BIT, GL_NEAREST); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + check_graphics("surface blit"); +} + void gl_renderer_set_texture(const struct gl_texture_state *state) { glUseProgram(program); diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -28,6 +28,7 @@ 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); +void gl_target_blit(struct gl_target *source, struct gl_target *target, const int32_t source_rect[4], const int32_t target_rect[4]); void gl_renderer_set_texture(const struct gl_texture_state *state); void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination); diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -26,6 +26,7 @@ static struct surface_object { uint32_t interfaces[2], references[2]; uint32_t width, height, caps, pixels, lock_flags, depth_bits, alpha; uint32_t caps2, description_flags; + int32_t lock_rectangle[4]; int locked; struct draw_object *owner; struct clipper_object *clipper; @@ -167,6 +168,14 @@ static void set_cooperative_level(void) g_esp += 16; } +static void test_cooperative_level(void) +{ + find_object(MEM32(g_esp + 4)); + /* Windowed rendering keeps its context/resources across focus changes. */ + g_eax = 0; + g_esp += 8; +} + static void get_device_identifier(void) { find_object(MEM32(g_esp + 4)); @@ -959,11 +968,16 @@ static void surface_lock(void) g_esp += 24; g_eax = 0x80070057u; if (!output || MEM32(output) != 124) return; - if (surface->depth || rect || event || (flags & ~0x31u)) { + MEM32(output + 36) = 0; + if (surface->depth || event || (flags & ~0x831u)) { fprintf(stderr, "Unsupported surface lock rect=%08x flags=%08x event=%08x\n", rect, flags, event); exit(3); } if (surface->locked) { g_eax = 0x887601aeu; return; } + int32_t rectangle[] = {0, 0, surface->width, surface->height}; + if (rect) memcpy(rectangle, (void *)ADDR(rect), 16); + if (rectangle[0] < 0 || rectangle[1] < 0 || rectangle[2] < rectangle[0] || rectangle[3] < rectangle[1] || + rectangle[2] > (int32_t)surface->width || rectangle[3] > (int32_t)surface->height) return; if (!surface->pixels) surface->pixels = guest_malloc(surface->width * surface->height * 4); if (!surface->pixels) { g_eax = 0x8007000eu; return; } if (!(surface->caps & 0x800)) { @@ -972,8 +986,9 @@ static void surface_lock(void) } surface->locked = 1; surface->lock_flags = flags; + memcpy(surface->lock_rectangle, rectangle, 16); describe_surface(surface, output); - MEM32(output + 36) = surface->pixels; + MEM32(output + 36) = surface->pixels + (rectangle[1] * surface->width + rectangle[0]) * 4; g_eax = 0; } @@ -982,8 +997,9 @@ 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->locked || (rect && memcmp((void *)ADDR(rect), surface->lock_rectangle, 16))) { + g_eax = 0x88760248u; return; + } if (!(surface->lock_flags & 0x10)) { swap_surface_channels(surface); gl_target_write(surface->target, (void *)ADDR(surface->pixels)); @@ -993,6 +1009,42 @@ static void surface_unlock(void) g_eax = 0; } +static void surface_blit(void) +{ + struct surface_object *target = find_surface(MEM32(g_esp + 4)); + uint32_t target_rect = MEM32(g_esp + 8), source_address = MEM32(g_esp + 12); + uint32_t source_rect = MEM32(g_esp + 16), flags = MEM32(g_esp + 20), effects = MEM32(g_esp + 24); + g_esp += 28; + if (!source_address || (flags & ~0x01000200u) || effects || target->clipper || (target->caps & 0x200)) { + fprintf(stderr, "Unsupported surface blit flags=%08x effects=%08x caps=%08x\n", flags, effects, target->caps); exit(3); + } + struct surface_object *source = find_surface(source_address); + if (source == target || source->depth || target->depth) { + fprintf(stderr, "Self/depth surface blits are not implemented\n"); exit(3); + } + if (source->alpha != target->alpha) { + fprintf(stderr, "Surface blit format conversion is not implemented\n"); exit(3); + } + if (source->locked || target->locked) { g_eax = 0x887601aeu; return; } + int32_t from[] = {0, 0, source->width, source->height}, to[] = {0, 0, target->width, target->height}; + if (source_rect) memcpy(from, (void *)ADDR(source_rect), 16); + if (target_rect) memcpy(to, (void *)ADDR(target_rect), 16); + g_eax = 0x80070057u; + if (from[0] < 0 || from[1] < 0 || from[2] <= from[0] || from[3] <= from[1] || + from[2] > (int32_t)source->width || from[3] > (int32_t)source->height || + to[0] < 0 || to[1] < 0 || to[2] <= to[0] || to[3] <= to[1] || + to[2] > (int32_t)target->width || to[3] > (int32_t)target->height) return; + if (from[2] - from[0] != to[2] - to[0] || from[3] - from[1] != to[3] - to[1]) { + fprintf(stderr, "Stretched surface blits are not implemented\n"); exit(3); + } + gl_target_blit(source->target, target->target, from, to); + if (target->caps & 0x800) { + gl_target_read(target->target, (void *)ADDR(target->pixels)); + swap_surface_channels(target); + } + g_eax = 0; +} + static struct device_object *find_device(uint32_t address) { for (struct device_object *device = devices; device; device = device->next) @@ -1460,6 +1512,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 2) return surface_release_reference; if (current_interface == 3) { if (current_slot == 3) return surface_attach; + if (current_slot == 5) return surface_blit; if (current_slot == 8) return surface_detach; if (current_slot == 12) return surface_get_attached; if (current_slot == 15) return surface_get_clipper; @@ -1487,6 +1540,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_interface == 1 && current_slot == 12) return get_display_mode; if (current_slot == 11) return get_caps; if (current_slot == 20) return set_cooperative_level; + if (current_interface == 1 && current_slot == 26) return test_cooperative_level; if (current_interface == 1 && current_slot == 27) return get_device_identifier; return unavailable_method; } diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -133,12 +133,14 @@ static void direct_draw_seven_creation_and_windowed_mode(void) uint32_t result = invoke(memory(*memory(object))[20], 3, object, 0, 8); assert(result == 0); + assert(!invoke(memory(*memory(object))[26], 1, object, 0, 0)); #ifdef _WIN32 unsigned control = _control87(0, 0); #else unsigned control = g_fpu_cw; #endif assert(!invoke(memory(*memory(object))[20], 3, object, 0, 0x808)); + assert(!invoke(memory(*memory(object))[26], 1, object, 0, 0)); #ifdef _WIN32 assert(_control87(0, 0) == control); #else diff --git a/decompiled/tests/surfaces.c b/decompiled/tests/surfaces.c @@ -43,7 +43,7 @@ static uint32_t *memory(uint32_t address) static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...) { - uint32_t arguments[5] = {object}; + uint32_t arguments[6] = {object}; va_list list; va_start(list, count); for (unsigned i = 1; i < count; ++i) arguments[i] = va_arg(list, uint32_t); @@ -54,7 +54,9 @@ static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...) 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]); + if (count == 5) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3], arguments[4]); + assert(count == 6); + return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]); #else uint32_t stack = g_esp, caller = g_cur_func; g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab; @@ -196,6 +198,51 @@ static void surface_pixels_round_trip(uint32_t caps) assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1)); } +static void rectangle_locks_preserve_surrounding_pixels(uint32_t caps) +{ + uint32_t draw = create_draw(), surface = create_surface(draw, caps); + uint32_t desc = workspace() + 128, rect = workspace() + 64; + *memory(desc) = 124; + assert(!invoke(surface, 25, 5, 0u, desc, 0x821u, 0u)); + uint32_t base = *memory(desc + 36), pitch = *memory(desc + 16); + for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x) + *memory(base + y * pitch + x * 4) = 0x00112233; + assert(!invoke(surface, 32, 2, 0u)); + const int32_t rectangle[] = {3, 5, 11, 13}; + memcpy(memory(rect), rectangle, 16); + + assert(!invoke(surface, 25, 5, rect, desc, 0x801u, 0u)); + uint32_t pixels = *memory(desc + 36); + assert(pixels == base + 5 * pitch + 12); + assert(*memory(desc + 8) == 16 && *memory(desc + 12) == 32 && *memory(desc + 16) == pitch); + for (unsigned y = 0; y < 8; ++y) for (unsigned x = 0; x < 8; ++x) + *memory(pixels + y * pitch + x * 4) = 0x0099aabb; + *memory(rect) = 4; + assert(invoke(surface, 32, 2, rect) == 0x88760248u); + *memory(rect) = 3; + assert(!invoke(surface, 32, 2, rect)); + assert(!invoke(surface, 25, 5, 0u, desc, 0x811u, 0u)); + pixels = *memory(desc + 36); + + for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x) { + uint32_t expected = x >= 3 && x < 11 && y >= 5 && y < 13 ? 0x99aabb : 0x112233; + assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == expected); + } + assert(!invoke(surface, 32, 2, 0u)); + const int32_t invalid[][4] = {{-1, 0, 4, 4}, {0, 0, 33, 16}, {8, 8, 4, 4}}; + for (unsigned i = 0; i < 3; ++i) { + memcpy(memory(rect), invalid[i], 16); + memset(memory(desc), 0xa5, 124); *memory(desc) = 124; + assert(invoke(surface, 25, 5, rect, desc, 0x801u, 0u) == 0x80070057u); + for (unsigned j = 1; j < 31; ++j) assert(*memory(desc + j * 4) == (j == 9 ? 0 : 0xa5a5a5a5)); + } + const int32_t empty[] = {4, 4, 4, 8}; + memcpy(memory(rect), empty, 16); + assert(!invoke(surface, 25, 5, rect, desc, 0x801u, 0u)); + assert(!invoke(surface, 32, 2, 0u)); + 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); @@ -360,6 +407,55 @@ static void window_clipper_regions(void) assert(!invoke(clipper, 2, 1) && !invoke(draw, 2, 1)); } +static void surface_blits_copy_pixels(void) +{ + uint32_t draw = create_draw(), desc = workspace() + 128; + uint32_t source_rect = workspace() + 64, target_rect = workspace() + 80; + const int32_t from[][4] = {{0, 0, 32, 16}, {2, 3, 10, 11}, {2, 3, 10, 7}}; + const int32_t to[][4] = {{0, 0, 32, 16}, {5, 4, 13, 12}, {24, 12, 32, 16}}; + for (unsigned storage = 0; storage < 4; ++storage) { + uint32_t source = create_surface(draw, storage & 1 ? 0x840 : 0x2040); + uint32_t target = create_surface(draw, storage & 2 ? 0x840 : 0x2040); + assert(!invoke(source, 25, 5, 0u, desc, 0x21u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + 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); + assert(!invoke(source, 32, 2, 0u)); + for (unsigned scenario = 0; scenario < 3; ++scenario) { + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x21u, 0u)); + pixels = *memory(desc + 36); pitch = *memory(desc + 16); + for (unsigned y = 0; y < 16; ++y) for (unsigned x = 0; x < 32; ++x) + *memory(pixels + y * pitch + x * 4) = 0xaabbcc; + assert(!invoke(target, 32, 2, 0u)); + memcpy(memory(source_rect), from[scenario], 16); + memcpy(memory(target_rect), to[scenario], 16); + + assert(!invoke(target, 5, 6, scenario ? target_rect : 0u, source, + scenario ? source_rect : 0u, 0x200u, 0u)); + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + pixels = *memory(desc + 36); pitch = *memory(desc + 16); + + for (int y = 0; y < 16; ++y) for (int x = 0; x < 32; ++x) { + uint32_t expected = 0xaabbcc; + if (x >= to[scenario][0] && x < to[scenario][2] && y >= to[scenario][1] && y < to[scenario][3]) { + unsigned sx = from[scenario][0] + x - to[scenario][0]; + unsigned sy = from[scenario][1] + y - to[scenario][1]; + expected = (sy << 16) | (sx << 8) | (sx ^ sy); + } + if ((*memory(pixels + y * pitch + x * 4) & 0xffffff) != expected) + fprintf(stderr, "blit storage=%u scenario=%u pixel=%d,%d actual=%06x expected=%06x\n", + storage, scenario, x, y, *memory(pixels + y * pitch + x * 4) & 0xffffff, expected); + assert((*memory(pixels + y * pitch + x * 4) & 0xffffff) == expected); + } + assert(!invoke(target, 32, 2, 0u)); + } + assert(!invoke(source, 2, 1) && !invoke(target, 2, 1)); + } + assert(!invoke(draw, 2, 1)); +} + static void private_data_is_copied_and_owned_by_each_surface(void) { uint32_t draw = create_draw(), first = create_surface(draw, 0x40), second = create_surface(draw, 0x40); @@ -417,10 +513,13 @@ int main(void) surface_lifetime_and_description(); surface_pixels_round_trip(0x2040); surface_pixels_round_trip(0x840); + rectangle_locks_preserve_surrounding_pixels(0x2040); + rectangle_locks_preserve_surrounding_pixels(0x840); clipper_attachment_retains_objects(); depth_surface_attachment_and_lifetime(); window_clipper_regions(); private_data_is_copied_and_owned_by_each_surface(); - puts("surfaces: descriptions, COM ownership, clipper regions, depth attachments, private data and 1024 pixel round trips passed"); + surface_blits_copy_pixels(); + puts("surfaces: descriptions, COM ownership, clipper regions, depth attachments, private data, rectangle locks, blits and pixel round trips passed"); return 0; }