aorecomp

Anarchy Online classic in the browser

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


commit eae512da46778c84e7aca1f2a07c7f69f6247363
parent 2ec3e42b17fb15881a3508befa8ed52f602eee99
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 03:42:19 +0000

Implement native surface metadata and advance graphics setup

Diffstat:
Mdecompiled/runtime/gl_renderer.c | 14+++++++++++++-
Mdecompiled/runtime/gl_renderer.h | 1+
Mdecompiled/runtime/linux_graphics.c | 128++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Mdecompiled/tests/devices.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/surfaces.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mtools/reconstruction/test_portable_devices.py | 4++--
6 files changed, 242 insertions(+), 21 deletions(-)

diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -184,14 +184,26 @@ void gl_target_destroy(struct gl_target *target) void gl_target_clear(struct gl_target *target, uint32_t argb, float depth) { + const int32_t rectangle[] = {0, 0, target->width, target->height}; + gl_target_clear_rect(target, argb, depth, rectangle); +} + +void gl_target_clear_rect(struct gl_target *target, uint32_t argb, float depth, const int32_t rectangle[4]) +{ + int32_t left = rectangle[0] < 0 ? 0 : rectangle[0], top = rectangle[1] < 0 ? 0 : rectangle[1]; + int32_t right = rectangle[2] > (int32_t)target->width ? (int32_t)target->width : rectangle[2]; + int32_t bottom = rectangle[3] > (int32_t)target->height ? (int32_t)target->height : rectangle[3]; + if (left >= right || top >= bottom) return; glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); - glDisable(GL_SCISSOR_TEST); + glEnable(GL_SCISSOR_TEST); + glScissor(left, top, right - left, bottom - top); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); glClearColor(((argb >> 16) & 255) / 255.0f, ((argb >> 8) & 255) / 255.0f, (argb & 255) / 255.0f, (argb >> 24) / 255.0f); glClearDepth(depth); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_SCISSOR_TEST); glBindFramebuffer(GL_FRAMEBUFFER, 0); check_graphics("target clear"); } diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -23,6 +23,7 @@ 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_clear_rect(struct gl_target *target, uint32_t argb, float depth, const int32_t rectangle[4]); 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); diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -17,6 +17,11 @@ static struct clipper_object { struct draw_object *owner; struct clipper_object *next; } *clippers; +struct surface_data { + uint32_t guid[4], size; + struct surface_data *next; + unsigned char bytes[]; +}; static struct surface_object { uint32_t interfaces[2], references[2]; uint32_t width, height, caps, pixels, lock_flags, depth_bits, alpha; @@ -27,6 +32,7 @@ static struct surface_object { struct gl_target *target; struct gl_depth *depth; struct surface_object *attached; + struct surface_data *data; struct surface_object *next; } *surfaces; static struct device_object { @@ -34,6 +40,7 @@ static struct device_object { uint32_t min_filter, mag_filter, mip_filter, address_u, address_v, color_op, alpha_op; uint32_t alpha_arg2; uint32_t blend, source_blend, destination_blend; + uint32_t material[17]; int scene; struct draw_object *owner; struct surface_object *target; @@ -598,7 +605,7 @@ static void create_surface(void) int depth = flags == 0x1007 && caps == 0x24000; int texture = (flags == 0x1007 || flags == 0x101007) && (caps == 0x1000 || caps == 0x5000); if (outer || !((flags == 1 && caps == 0x200) || depth || texture || - (flags == 7 && (caps == 0x40 || caps == 0x2040 || caps == 0x6040)))) { + (flags == 7 && (caps == 0x40 || caps == 0x840 || caps == 0x2040 || caps == 0x6040)))) { fprintf(stderr, "Unsupported DirectDraw surface flags=%08x caps=%08x outer=%08x\n", flags, caps, outer); exit(3); } @@ -648,19 +655,19 @@ static void create_surface(void) surface->alpha = MEM32(desc + 76) & 1; surface->caps2 = MEM32(desc + 108); gl_target_set_depth(surface->target, NULL); - if (surface->caps2 == 0x10) { - /* Managed textures retain CPU storage and upload writes on unlock. */ - surface->pixels = guest_malloc(width * height * 4); - if (!surface->pixels) { - gl_target_destroy(surface->target); free(surface); g_eax = 0x8007000eu; return; - } - memset((void *)ADDR(surface->pixels), 0, width * height * 4); - gl_target_write(surface->target, (void *)ADDR(surface->pixels)); - } } surface->width = width; surface->height = height; - surface->caps = caps | (surface->caps2 ? 0x800u : 0x10004000u); /* Managed system or local video memory. */ + surface->caps = caps | ((surface->caps2 || (caps & 0x800)) ? 0x800u : 0x10004000u); + if (surface->caps & 0x800) { + /* System-memory surfaces retain CPU storage and upload writes on unlock. */ + surface->pixels = guest_malloc(width * height * 4); + if (!surface->pixels) { + gl_target_destroy(surface->target); free(surface); g_eax = 0x8007000eu; return; + } + memset((void *)ADDR(surface->pixels), 0, width * height * 4); + gl_target_write(surface->target, (void *)ADDR(surface->pixels)); + } surface->description_flags = 0x100f | (flags & 0x100000); surface->owner = owner; ++owner->references[1]; @@ -712,6 +719,11 @@ static uint32_t release_surface(struct surface_object *surface, unsigned index) *link = surface->next; if (surface->target) gl_target_destroy(surface->target); if (surface->depth) gl_depth_destroy(surface->depth); + while (surface->data) { + struct surface_data *next = surface->data->next; + free(surface->data); + surface->data = next; + } if (surface->attached) release_surface(surface->attached, 0); if (surface->clipper) release_clipper(surface->clipper); guest_free(surface->pixels); @@ -729,6 +741,69 @@ static void surface_release_reference(void) g_esp += 8; } +static struct surface_data **surface_data_link(struct surface_object *surface, uint32_t guid) +{ + struct surface_data **link = &surface->data; + while (*link && memcmp((*link)->guid, (void *)ADDR(guid), 16)) link = &(*link)->next; + return link; +} + +static void surface_set_private_data(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t guid = MEM32(g_esp + 8), input = MEM32(g_esp + 12); + uint32_t size = MEM32(g_esp + 16), flags = MEM32(g_esp + 20); + g_esp += 24; + if (flags) { fprintf(stderr, "Unsupported surface private-data flags=%08x\n", flags); exit(3); } + g_eax = 0x80070057u; + if (!guid || !input || !size) return; + struct surface_data *data = malloc(sizeof(*data) + (size_t)size); + if (!data) { g_eax = 0x8007000eu; return; } + memcpy(data->guid, (void *)ADDR(guid), 16); + memcpy(data->bytes, (void *)ADDR(input), size); + data->size = size; + struct surface_data **link = surface_data_link(surface, guid); + data->next = *link ? (*link)->next : NULL; + free(*link); + *link = data; + g_eax = 0; +} + +static void surface_get_private_data(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t guid = MEM32(g_esp + 8), output = MEM32(g_esp + 12), size = MEM32(g_esp + 16); + g_esp += 20; + g_eax = 0x80070057u; + if (!guid || !size) return; + struct surface_data *data = *surface_data_link(surface, guid); + g_eax = 0x887600ffu; /* DDERR_NOTFOUND leaves the size unchanged. */ + if (!data) return; + uint32_t capacity = MEM32(size); + MEM32(size) = data->size; + if (capacity < data->size) { g_eax = 0x887602b2u; return; } + g_eax = 0x80070057u; + if (!output) return; + memcpy((void *)ADDR(output), data->bytes, data->size); + g_eax = 0; +} + +static void surface_free_private_data(void) +{ + struct surface_object *surface = find_surface(MEM32(g_esp + 4)); + uint32_t guid = MEM32(g_esp + 8); + g_esp += 12; + g_eax = 0x80070057u; + if (!guid) return; + struct surface_data **link = surface_data_link(surface, guid); + g_eax = 0x887600ffu; + if (!*link) return; + struct surface_data *data = *link; + *link = data->next; + free(data); + g_eax = 0; +} + static void surface_pixel_format(struct surface_object *surface, uint32_t output) { const uint32_t format[] = {32, 0x40 | surface->alpha, 0, 32, 0xff0000, 0xff00, 0xff, @@ -891,7 +966,7 @@ static void surface_lock(void) 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; } - if (!surface->caps2) { + if (!(surface->caps & 0x800)) { gl_target_read(surface->target, (void *)ADDR(surface->pixels)); swap_surface_channels(surface); } @@ -1076,18 +1151,39 @@ static void device_set_viewport(void) g_esp += 12; } +static void device_material(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t address = MEM32(g_esp + 8); + g_esp += 12; + g_eax = 0x80070057u; + if (!address) return; + /* XYZRHW vertices are already lit. XYZ drawing remains unsupported. */ + if (current_slot == 16) memcpy(device->material, (void *)ADDR(address), sizeof(device->material)); + else memcpy((void *)ADDR(address), device->material, sizeof(device->material)); + g_eax = 0; +} + 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) { + 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); + if (!count) gl_target_clear(device->target->target, color, z); + for (uint32_t i = 0; i < count; ++i) { + int32_t rectangle[4]; + memcpy(rectangle, (void *)ADDR(rectangles + i * 16), 16); + if (rectangle[2] < rectangle[0] || rectangle[3] < rectangle[1]) { + g_eax = 0x80070057u; return; + } + gl_target_clear_rect(device->target->target, color, z, rectangle); + } g_eax = 0; } @@ -1339,6 +1435,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) 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 == 16 || current_slot == 17) return device_material; if (current_slot == 20) return device_set_state; if (current_slot == 21) return device_get_state; if (current_slot == 25) return device_draw_primitive; @@ -1373,6 +1470,9 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 28) return surface_set_clipper; if (current_slot == 32) return surface_unlock; if (current_slot == 36) return surface_owner; + if (current_slot == 40) return surface_set_private_data; + if (current_slot == 41) return surface_get_private_data; + if (current_slot == 42) return surface_free_private_data; } return unavailable_method; } diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -237,6 +237,62 @@ static void initial_device_states(uint32_t device) } } +static void rectangle_clears_preserve_outside_depth(uint32_t device, uint32_t target, FILE *file) +{ + uint32_t rectangles = workspace() + 64, input = workspace() + 512, desc = workspace() + 128; + const int32_t regions[][4] = {{3, 5, 27, 23}, {1, 1, 15, 13}, {13, 9, 31, 31}}; + memcpy(memory(rectangles), regions, sizeof(regions)); + struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = { + {0, 0, .5f, 1, 0xffffffff}, {32, 0, .5f, 1, 0xffffffff}, {0, 32, .5f, 1, 0xffffffff} + }; + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 20, 3, 27u, 0u)); + for (unsigned count = 1; count <= 2; ++count) { + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 10, 7, count, rectangles + (count == 2 ? 16 : 0), 3u, 0xff335577u, 0u, 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(target, 25, 5, 0u, desc, 0x11u, 0u)); + + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert((*memory(pixels) & 0xffffff) == 0xffffff); + assert((*memory(pixels + 6 * pitch + 6 * 4) & 0xffffff) == 0x335577); + assert((*memory(pixels + 31 * pitch + 31 * 4) & 0xffffff) == 0x102030); + 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(target, 32, 2, 0u)); + assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); + } +} + +static void material_state_is_copied(uint32_t device) +{ + uint32_t input = workspace() + 128, output = workspace() + 256; + memset(memory(output), 0xa5, 72); + assert(!invoke(device, 17, 2, output)); + for (unsigned i = 0; i < 17; ++i) assert(!*memory(output + i * 4)); + const float material[] = {.2f, .3f, .4f, .5f, .6f, .7f, .8f, .9f, + 1, .1f, .2f, .3f, .4f, .5f, .6f, .7f, 16}; + memcpy(memory(input), material, sizeof(material)); + + assert(!invoke(device, 16, 2, input)); + memset(memory(input), 0, sizeof(material)); + assert(!invoke(device, 17, 2, output)); + + assert(!memcmp(memory(output), material, sizeof(material))); + assert(*memory(output + 68) == 0xa5a5a5a5); + assert(invoke(device, 16, 2, 0u) == 0x80070057u); + assert(invoke(device, 17, 2, 0u) == 0x80070057u); +} + static void device_lifetime_and_rendering(const char *pixels_path) { const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; @@ -273,6 +329,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) assert(!invoke(d3d, 4, 4, workspace() + 16, color, workspace())); uint32_t device = *memory(workspace()); initial_device_states(device); + material_state_is_copied(device); uint32_t identity = query(device, unknown); assert(!invoke(device, 9, 2, workspace())); @@ -360,6 +417,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) assert(!invoke(color, 32, 2, 0u)); assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); textured_triangles(draw, device, color, file); + rectangle_clears_preserve_outside_depth(device, color, file); assert(!fclose(file)); assert(!invoke(device, 2, 1)); assert(!invoke(draw, 2, 1)); diff --git a/decompiled/tests/surfaces.c b/decompiled/tests/surfaces.c @@ -166,14 +166,15 @@ static void surface_lifetime_and_description(void) assert(!invoke(primary, 2, 1)); } -static void surface_pixels_round_trip(void) +static void surface_pixels_round_trip(uint32_t caps) { - uint32_t draw = create_draw(), surface = create_surface(draw, 0x2040); + uint32_t draw = create_draw(), surface = create_surface(draw, caps); 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); + assert(*memory(desc + 104) == (caps == 0x840 ? 0x840 : 0x10006040)); 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); @@ -359,6 +360,53 @@ static void window_clipper_regions(void) assert(!invoke(clipper, 2, 1) && !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); + uint32_t key = workspace() + 32, other_key = workspace() + 48; + uint32_t input = workspace() + 256, output = workspace() + 512, size = workspace() + 80; + const uint32_t guid[] = {0x684b06f3, 0x11d2ebae, 0xc000f481, 0x99f2684f}; + const uint32_t payload[] = {0x60001234, 2, 3, 4, 5}; + memcpy(memory(key), guid, 16); memcpy(memory(other_key), guid, 16); + *memory(other_key + 12) = 0x12345678; + memcpy(memory(input), payload, 20); + memset(memory(output), 0xa5, 32); + *memory(size) = 32; + + assert(invoke(first, 41, 4, key, output, size) == 0x887600ffu && *memory(size) == 32); + assert(invoke(first, 42, 2, key) == 0x887600ffu); + assert(!invoke(first, 40, 5, key, input, 20u, 0u)); + *memory(input) = 99; + assert(!invoke(first, 40, 5, other_key, input, 3u, 0u)); + assert(!invoke(second, 40, 5, key, input, 8u, 0u)); + + const uint32_t capacities[] = {0, 4, 20, 32}; + for (unsigned i = 0; i < 4; ++i) { + *memory(size) = capacities[i]; + memset(memory(output), 0xa5, 32); + uint32_t result = invoke(first, 41, 4, key, output, size); + assert(result == (capacities[i] < 20 ? 0x887602b2u : 0)); + assert(*memory(size) == 20); + if (capacities[i] >= 20) assert(!memcmp(memory(output), payload, 20)); + else for (unsigned j = 0; j < 5; ++j) assert(*memory(output + j * 4) == 0xa5a5a5a5); + assert(*memory(output + 20) == 0xa5a5a5a5 && *memory(output + 28) == 0xa5a5a5a5); + } + *memory(size) = 0; + assert(invoke(first, 41, 4, key, 0u, size) == 0x887602b2u && *memory(size) == 20); + assert(!invoke(first, 40, 5, key, input, 8u, 0u)); + assert(invoke(first, 40, 5, key, input, 0u, 0u) == 0x80070057u); + *memory(size) = 32; + assert(!invoke(first, 41, 4, key, output, size)); + assert(*memory(size) == 8 && *memory(output) == 99 && *memory(output + 4) == 2); + assert(!invoke(first, 42, 2, key)); + assert(invoke(first, 42, 2, key) == 0x887600ffu); + assert(!invoke(second, 41, 4, key, output, size) && *memory(output) == 99); + memset(memory(output), 0xa5, 32); + assert(!invoke(first, 41, 4, other_key, output, size)); + assert(*memory(size) == 3 && *memory(output) == 0xa5000063); + assert(!invoke(first, 2, 1) && !invoke(second, 2, 1) && !invoke(draw, 2, 1)); +} + int main(void) { #ifndef _WIN32 @@ -367,10 +415,12 @@ int main(void) g_fs_base = 0x50000000; #endif surface_lifetime_and_description(); - surface_pixels_round_trip(); + surface_pixels_round_trip(0x2040); + surface_pixels_round_trip(0x840); 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"); + 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"); return 0; } diff --git a/tools/reconstruction/test_portable_devices.py b/tools/reconstruction/test_portable_devices.py @@ -25,8 +25,8 @@ actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_wor 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) == 497664 +assert len(windows_pixels) == len(linux_pixels) == 503808 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 165888 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 167936 pixels agree within one 8-bit channel step; maximum error:", error)