aorecomp

Anarchy Online classic in the browser

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


commit f33d501acb970338011bc4276185201c7d2c7e85
parent c18104bde2e96178c0b768e70b8d74ac14d82c64
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 14:03:26 +0000

Render indexed vertex buffers through native graphics

Diffstat:
Mdecompiled/runtime/linux_graphics.c | 36++++++++++++++++++++++++------------
Mdecompiled/tests/devices.c | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mtools/reconstruction/test_portable_devices.py | 4++--
3 files changed, 84 insertions(+), 17 deletions(-)

diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -1650,15 +1650,19 @@ static void device_set_state(void) } static void draw_primitive(struct device_object *device, uint32_t primitive, uint32_t fvf, - uint32_t input, uint32_t count, uint32_t flags) + uint32_t input, uint32_t count, uint32_t flags, + uint32_t input_indices, uint32_t element_count) { if ((primitive != 4 && primitive != 5) || ((fvf & ~0x80u) != 0x44 && (fvf & ~0x80u) != 0x144) || flags) { fprintf(stderr, "Unsupported Direct3D draw primitive=%u fvf=%08x flags=%08x cull=%u\n", primitive, fvf, flags, device->cull); exit(3); } g_eax = 0x80070057u; - if (!input || count < 3 || (primitive == 4 && count % 3) || count > 65535) return; - uint32_t index_count = primitive == 4 ? count : (count - 2) * 3; + if (!input_indices) element_count = count; + if (!input || !count || element_count < 3 || (primitive == 4 && element_count % 3) || count > 65535) return; + for (uint32_t i = 0; input_indices && i < element_count; ++i) + if (MEM16(input_indices + i * 2) >= count) return; + uint32_t index_count = primitive == 4 ? element_count : (element_count - 2) * 3; struct gl_vertex *vertices = malloc(count * sizeof(*vertices)); uint16_t *indices = malloc(index_count * sizeof(*indices)); if (!vertices || !indices) abort(); @@ -1682,11 +1686,12 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin vertices[i].uv[0] = vertices[i].uv[1] = 0; if (fvf & 0x100) memcpy(vertices[i].uv, (void *)ADDR(input + i * stride + uv_offset), 8); } - if (primitive == 4) for (uint32_t i = 0; i < count; ++i) indices[i] = i; - else for (uint32_t i = 0; i < count - 2; ++i) { - indices[i * 3] = i + (i & 1); - indices[i * 3 + 1] = i + !(i & 1); - indices[i * 3 + 2] = i + 2; + if (primitive == 4) for (uint32_t i = 0; i < element_count; ++i) indices[i] = input_indices ? MEM16(input_indices + i * 2) : i; + else for (uint32_t i = 0; i < element_count - 2; ++i) { + uint32_t a = i + (i & 1), b = i + !(i & 1), c = i + 2; + indices[i * 3] = input_indices ? MEM16(input_indices + a * 2) : a; + indices[i * 3 + 1] = input_indices ? MEM16(input_indices + b * 2) : b; + indices[i * 3 + 2] = input_indices ? MEM16(input_indices + c * 2) : c; } if (device->texture && device->color_op != 1) { struct gl_texture_state state = {device->texture->target, @@ -1715,7 +1720,7 @@ static void device_draw_primitive(void) uint32_t primitive = MEM32(g_esp + 8), fvf = MEM32(g_esp + 12), input = MEM32(g_esp + 16); uint32_t count = MEM32(g_esp + 20), flags = MEM32(g_esp + 24); g_esp += 28; - draw_primitive(device, primitive, fvf, input, count, flags); + draw_primitive(device, primitive, fvf, input, count, flags, 0, 0); } static void device_set_texture(void) @@ -1899,11 +1904,18 @@ static void device_draw_vertex_buffer(void) uint32_t primitive = MEM32(g_esp + 8); struct vertex_buffer *buffer = find_vertex_buffer(MEM32(g_esp + 12)); uint32_t start = MEM32(g_esp + 16), count = MEM32(g_esp + 20), flags = MEM32(g_esp + 24); - g_esp += 28; + uint32_t indices = 0, index_count = 0; + if (current_slot == 32) { + indices = MEM32(g_esp + 24); + index_count = MEM32(g_esp + 28); + flags = MEM32(g_esp + 32); + } + g_esp += current_slot == 32 ? 36 : 28; g_eax = 0x80070057u; + if (current_slot == 32 && !indices) return; if (start > buffer->count || count > buffer->count - start) return; draw_primitive(device, primitive, buffer->fvf, - buffer->data + start * vertex_stride(buffer->fvf), count, flags); + buffer->data + start * vertex_stride(buffer->fvf), count, flags, indices, index_count); } static void vertex_buffer_query_interface(void) @@ -2031,7 +2043,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 25) return device_draw_primitive; if (current_slot == 34) return device_get_texture; if (current_slot == 35) return device_set_texture; - if (current_slot == 31) return device_draw_vertex_buffer; + if (current_slot == 31 || current_slot == 32) return device_draw_vertex_buffer; if (current_slot == 36) return device_get_texture_state; if (current_slot == 37) return device_set_texture_state; return unavailable_method; diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -36,7 +36,7 @@ static uint32_t *memory(uint32_t address) static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...) { - uint32_t args[7] = {object}; + uint32_t args[8] = {object}; va_list list; va_start(list, count); for (unsigned i = 1; i < count; ++i) args[i] = va_arg(list, uint32_t); @@ -49,8 +49,9 @@ static uint32_t invoke(uint32_t object, unsigned slot, unsigned count, ...) if (count == 4) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3]); if (count == 5) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4]); if (count == 6) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5]); - assert(count == 7); - return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5], args[6]); + if (count == 7) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5], args[6]); + assert(count == 8); + return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)address)(object, args[1], args[2], args[3], args[4], args[5], args[6], args[7]); #else uint32_t stack = g_esp, caller = g_cur_func; g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab; @@ -430,6 +431,59 @@ static void vertex_buffer_draws_match_memory_draws(uint32_t draw, uint32_t devic invoke(d3d, 2, 1); } +static void indexed_vertex_buffers_use_relative_indices(uint32_t draw, uint32_t device, uint32_t target, FILE *file) +{ + const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; + const uint32_t formats[] = {0x44, 0xc4, 0x144, 0x1c4}; + const unsigned strides[] = {20, 24, 28, 32}; + const float positions[][4] = {{4,28,.25f,1}, {4,4,.25f,1}, {28,28,.25f,1}, {28,4,.25f,1}}; + const uint16_t lists[][6] = {{1,3,0,0,3,2}, {1,3,0,2}}; + uint32_t d3d = query(draw, iid), desc = workspace() + 128, elements = workspace() + 512; + for (unsigned format = 0; format < 4; ++format) for (unsigned start = 0; start <= 2; start += 2) { + uint32_t description[] = {16, 0x800, formats[format], 6}; + memcpy(memory(desc), description, 16); + assert(!invoke(d3d, 5, 4, desc, workspace(), 0u)); + uint32_t buffer = *memory(workspace()); + assert(!invoke(buffer, 3, 4, 0x1020u, workspace(), 0u)); + uint32_t input = *memory(workspace()); + memset(memory(input), 0xa5, 6 * strides[format]); + for (unsigned i = 0; i < 4; ++i) { + uint32_t vertex = input + (start + i) * strides[format]; + memcpy(memory(vertex), positions[i], 16); + *memory(vertex + 16) = 0xff5a96d2; + if (formats[format] & 0x80) *memory(vertex + 20) = 0xffffffff; + if (formats[format] & 0x100) memset(memory(vertex + strides[format] - 8), 0, 8); + } + assert(!invoke(buffer, 4, 1)); + for (unsigned primitive = 4; primitive <= 5; ++primitive) for (unsigned cull = 1; cull <= 3; ++cull) { + memcpy(memory(elements), lists[primitive - 4], 12); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + assert(!invoke(device, 20, 3, 22u, cull)); + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 32, 8, primitive, buffer, start, 4u, elements, primitive == 4 ? 6u : 4u, 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 + 10 * pitch + 10 * 4) & 0xffffff) == (cull == 2 ? 0x102030 : 0x5a96d2)); + assert((*memory(pixels + 23 * pitch + 23 * 4) & 0xffffff) == (cull == 2 ? 0x102030 : 0x5a96d2)); + 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)); + } + invoke(buffer, 2, 1); + } + assert(!invoke(device, 20, 3, 22u, 1u)); + invoke(d3d, 2, 1); +} + static void initial_device_states(uint32_t device) { const uint32_t states[][2] = { @@ -929,6 +983,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) mip_levels_render_after_edits_and_eviction(draw, device, color); transformed_vertex_fog_blends_rgb(device, color, file); vertex_buffer_draws_match_memory_draws(draw, device, color, file); + indexed_vertex_buffers_use_relative_indices(draw, device, color, file); rectangle_clears_preserve_outside_depth(device, color, file); depth_write_controls_occlusion(device, color); depth_comparisons_control_visibility(device, color); 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) == 1471488 +assert len(windows_pixels) == len(linux_pixels) == 1618944 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 490496 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 539648 pixels agree within one 8-bit channel step; maximum error:", error)