aorecomp

Anarchy Online classic in the browser

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


commit 11d5ddb1c62ee6801465b8b9fff0bb9c61f59e0c
parent 2ce8d1a70ef535f219a58a6768a1965a321bbce4
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 14:41:06 +0000

Select native texture coordinates from two UV sets

Diffstat:
Mdecompiled/runtime/linux_graphics.c | 28+++++++++++++++++-----------
Mdecompiled/tests/devices.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/reconstruction/test_portable_devices.py | 4++--
3 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -52,7 +52,7 @@ struct device_light { static struct device_object { uint32_t interface, references, cull; uint32_t min_filter, mag_filter, mip_filter, address, address_u, address_v, color_op, alpha_op; - uint32_t sampler_u, sampler_v, texture_transform; + uint32_t sampler_u, sampler_v, texture_transform, texture_coordinate; uint32_t color_arg2, alpha_arg2; uint32_t fog_enabled, fog_color, fog_start, fog_end, fog_density, fog_vertex_mode; uint32_t blend, source_blend, destination_blend, depth_write, depth_function, clipping, lighting; @@ -1709,9 +1709,10 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin uint32_t input, uint32_t count, uint32_t flags, uint32_t input_indices, uint32_t element_count) { - int untransformed = (fvf & ~0x40u) == 0x112 || fvf == 0x142; - if ((primitive != 4 && primitive != 5) || - ((fvf & ~0x80u) != 0x44 && (fvf & ~0x80u) != 0x144 && !untransformed) || flags) { + unsigned texture_count = (fvf >> 8) & 15, format = fvf & ~0x300u; + int untransformed = texture_count >= 1 && (format == 0x12 || format == 0x52 || format == 0x42); + if ((primitive != 4 && primitive != 5) || texture_count > 2 || + (format != 0x44 && format != 0xc4 && !untransformed) || 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; @@ -1740,11 +1741,11 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin for (unsigned i = 0; i < 9; ++i) normal_matrix[i] = cofactors[i] / determinant; } /* Specular input is inactive while D3DRENDERSTATE_SPECULARENABLE is false. */ - uint32_t uv_offset = 20 + (fvf & 0x80 ? 4 : 0), stride = uv_offset + (fvf & 0x100 ? 8 : 0); - if (untransformed) stride = 12 + (fvf & 0x10 ? 12 : 0) + (fvf & 0x40 ? 4 : 0) + 8; + uint32_t uv_offset = 20 + (fvf & 0x80 ? 4 : 0), stride = uv_offset + texture_count * 8; + if (untransformed) stride = 12 + (fvf & 0x10 ? 12 : 0) + (fvf & 0x40 ? 4 : 0) + texture_count * 8; for (uint32_t i = 0; i < count; ++i) { if (untransformed) { - float input_vertex[9], position[4], camera[4], projected[4], normal[3], diffuse[4]; + float input_vertex[11], position[4], camera[4], projected[4], normal[3], diffuse[4]; memcpy(input_vertex, (void *)ADDR(input + i * stride), stride); memcpy(position, input_vertex, 12); position[3] = 1; transform_vector(position, world_view, camera); @@ -1774,7 +1775,9 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin memcpy(&start, &device->fog_start, 4); memcpy(&end, &device->fog_end, 4); vertices[i].fog = fminf(1, fmaxf(0, (end - fabsf(camera[2])) / (end - start))); } - memcpy(vertices[i].uv, (const unsigned char *)input_vertex + stride - 8, 8); + vertices[i].uv[0] = vertices[i].uv[1] = 0; + if (device->texture_coordinate < texture_count) + memcpy(vertices[i].uv, (const unsigned char *)input_vertex + stride - texture_count * 8 + device->texture_coordinate * 8, 8); if (device->texture_transform == 2) { float coordinate[] = {vertices[i].uv[0], vertices[i].uv[1], 1, 0}, transformed[4]; transform_vector(coordinate, (const float *)device->transforms[3], transformed); @@ -1797,7 +1800,8 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin vertices[i].color[3] = (color >> 24) / 255.0f; vertices[i].fog = fvf & 0x80 ? (MEM32(input + i * stride + 20) >> 24) / 255.0f : 0; 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 (device->texture_coordinate < texture_count) + memcpy(vertices[i].uv, (void *)ADDR(input + i * stride + uv_offset + device->texture_coordinate * 8), 8); } 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) { @@ -1876,7 +1880,8 @@ static uint32_t texture_stage_state(struct device_object *device, uint32_t state case 6: return device->alpha_arg2; case 4: return device->alpha_op; case 7: case 8: case 9: case 10: /* Inactive bump matrix. */ - case 11: case 15: case 19: case 20: case 22: case 23: return 0; + case 15: case 19: case 20: case 22: case 23: return 0; + case 11: return device->texture_coordinate; case 24: return device->texture_transform; case 12: return device->address; case 13: return device->address_u; @@ -1909,7 +1914,8 @@ static void device_set_texture_state(void) struct device_object *device = find_device(MEM32(g_esp + 4)); uint32_t stage = MEM32(g_esp + 8), state = MEM32(g_esp + 12), value = MEM32(g_esp + 16); if (stage) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } - if (state == 16 && (value == 1 || value == 2)) device->mag_filter = value; + if (state == 11 && value <= 1) device->texture_coordinate = value; + else if (state == 16 && (value == 1 || value == 2)) device->mag_filter = value; else if (state == 17 && (value == 1 || value == 2)) device->min_filter = value; else if (state == 18 && (value == 1 || value == 2)) device->mip_filter = value; else if (state == 24 && (value == 0 || value == 2)) device->texture_transform = value; diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -181,6 +181,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, assert(!invoke(device, 37, 4, 0u, 24u, legacy == 3 || legacy == 5 ? 2u : 0u)); assert(!invoke(device, 36, 4, 0u, 24u, workspace()) && *memory(workspace()) == (legacy == 3 || legacy == 5 ? 2u : 0u)); assert(!invoke(device, 20, 3, 137u, legacy < 4 ? 1u : 0u)); + assert(!invoke(device, 37, 4, 0u, 11u, legacy == 4 && (scenario & 1) ? 1u : 0u)); if (legacy >= 4) for (unsigned i = 0; i < 3; ++i) { float xyz[] = {vertices[i].x / 16 - 1, 1 - vertices[i].y / 16, .25f}; memcpy(memory(input + i * 24), xyz, 12); @@ -243,6 +244,55 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, assert(!invoke(target, 32, 2, 0u)); assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); } + const uint32_t formats[] = {0x244, 0x2c4, 0x212, 0x252, 0x242}; + for (unsigned format = 0; format < 5; ++format) for (unsigned coordinate = 0; coordinate < 2; ++coordinate) + for (unsigned transform = 0; transform <= 2; transform += 2) { + uint32_t fvf = formats[format]; + unsigned transformed = (fvf & 14) == 4; + unsigned prefix = transformed ? 16 : 12; + if (fvf & 16) prefix += 12; + if (fvf & 64) prefix += 4; + if (fvf & 128) prefix += 4; + unsigned stride = prefix + 16; + for (unsigned i = 0; i < 3; ++i) { + float position[] = {i == 1 ? 28 : 4, i == 2 ? 28 : 4, .25f, 1}; + if (!transformed) { position[0] = position[0] / 16 - 1; position[1] = 1 - position[1] / 16; } + uint32_t vertex = input + i * stride; + memset(memory(vertex), 0, stride); + memcpy(memory(vertex), position, transformed ? 16 : 12); + if (fvf & 64) *memory(vertex + (transformed ? 16 : fvf & 16 ? 24 : 12)) = 0xff80c0ff; + const float uv[] = {i == 1 ? .875f : .125f, i == 2 ? .875f : .125f, + i == 2 ? .375f : .625f, i == 1 ? .875f : .375f}; + memcpy(memory(vertex + prefix), uv, 16); + } + assert(!invoke(device, 20, 3, 137u, 0u)); + assert(!invoke(device, 20, 3, 27u, 0u)); + assert(!invoke(device, 37, 4, 0u, 1u, 2u)); + assert(!invoke(device, 37, 4, 0u, 16u, coordinate + 1)); + assert(!invoke(device, 37, 4, 0u, 17u, coordinate + 1)); + assert(!invoke(device, 37, 4, 0u, 11u, coordinate)); + assert(!invoke(device, 36, 4, 0u, 11u, workspace()) && *memory(workspace()) == coordinate); + assert(!invoke(device, 37, 4, 0u, 24u, transform)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, fvf, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + pixels = *memory(desc + 36); pitch = *memory(desc + 16); + 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)); + } + assert(!invoke(device, 37, 4, 0u, 11u, 0u)); assert(!invoke(device, 35, 3, 0u, 0u)); assert(!invoke(device, 34, 3, 0u, workspace()) && !*memory(workspace())); } 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) == 1873920 +assert len(windows_pixels) == len(linux_pixels) == 2119680 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 624640 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 706560 pixels agree within one 8-bit channel step; maximum error:", error)