aorecomp

Anarchy Online classic in the browser

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


commit 70f6dbe4fb0c6b6e6a45226c97051e89cffabb6b
parent f33d501acb970338011bc4276185201c7d2c7e85
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 14:10:56 +0000

Transform and light native scene vertices

Diffstat:
Mdecompiled/Makefile.linux | 8++++----
Mdecompiled/runtime/linux_graphics.c | 100++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mdecompiled/tests/devices.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/reconstruction/test_portable_devices.py | 6+++---
Mtools/reconstruction/test_portable_graphics.py | 2+-
Mtools/reconstruction/test_portable_surfaces.py | 2+-
6 files changed, 180 insertions(+), 10 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -148,11 +148,11 @@ build/linux/create-directory: tests/create_directory.c runtime/linux_platform.c 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 $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -lm -o $@ build/linux/window: tests/window.c data/Clientr_exe.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_window.h runtime/linux_graphics.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 -lX11 -o $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -lm -lX11 -o $@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stricmp build/linux/stdio-input build/linux/guest-stat build/linux/chmod build/linux/create-directory build/linux/dynamic-imports build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio build/linux/guest-exception build/linux/platform-diagnostics build/linux/new-handler ./build/linux/linecrypt @@ -213,13 +213,13 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric 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 $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -lm -o $@ test: build/linux/surfaces build/linux/devices: tests/devices.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 $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -lm -o $@ test: build/linux/devices diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -6,6 +6,7 @@ #include <SDL2/SDL_opengl.h> #include <errno.h> #include <time.h> +#include <math.h> /* Native graphics resources never appear in guest COM objects or vtables. */ static SDL_Window *window; @@ -1649,12 +1650,66 @@ static void device_set_state(void) g_esp += 16; } +static void transform_vector(const float input[4], const float matrix[16], float output[4]) +{ + for (unsigned j = 0; j < 4; ++j) + output[j] = input[0] * matrix[j] + input[1] * matrix[4 + j] + + input[2] * matrix[8 + j] + input[3] * matrix[12 + j]; +} + +static float normalize_vector(float vector[3]) +{ + float length = sqrtf(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]); + if (length) for (unsigned j = 0; j < 3; ++j) vector[j] /= length; + return length; +} + +static void light_vertex(struct device_object *device, const float position[4], const float normal[3], float color[4]) +{ + const float *material = (const float *)device->material; + for (unsigned j = 0; j < 3; ++j) + color[j] = material[12 + j] + material[4 + j] * ((device->ambient >> (16 - j * 8)) & 255) / 255.0f; + color[3] = material[3]; + for (struct device_light *light = device->lights; light; light = light->next) { + if (!light->enabled) continue; + const float *data = (const float *)light->data; + float direction[4], source[4], attenuation = 1; + if (light->data[0] == 3) { + for (unsigned j = 0; j < 3; ++j) source[j] = -data[16 + j]; + source[3] = 0; + transform_vector(source, (const float *)device->transforms[1], direction); + normalize_vector(direction); + } else { + memcpy(source, data + 13, 12); source[3] = 1; + transform_vector(source, (const float *)device->transforms[1], direction); + for (unsigned j = 0; j < 3; ++j) direction[j] -= position[j]; + float distance = normalize_vector(direction); + if (distance > data[19]) continue; + attenuation = 1 / (data[21] + distance * data[22] + distance * distance * data[23]); + if (light->data[0] == 2) { + float spot[4]; + memcpy(source, data + 16, 12); source[3] = 0; + transform_vector(source, (const float *)device->transforms[1], spot); + normalize_vector(spot); + float cosine = -(spot[0] * direction[0] + spot[1] * direction[1] + spot[2] * direction[2]); + float inner = cosf(data[24] * .5f), outer = cosf(data[25] * .5f); + if (cosine <= outer) continue; + if (cosine < inner) attenuation *= powf((cosine - outer) / (inner - outer), data[20]); + } + } + float diffuse = fmaxf(0, normal[0] * direction[0] + normal[1] * direction[1] + normal[2] * direction[2]); + for (unsigned j = 0; j < 3; ++j) + color[j] += attenuation * (material[j] * data[1 + j] * diffuse + material[4 + j] * data[9 + j]); + } + for (unsigned j = 0; j < 4; ++j) color[j] = fminf(1, fmaxf(0, color[j])); +} + 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_indices, uint32_t element_count) { if ((primitive != 4 && primitive != 5) || - ((fvf & ~0x80u) != 0x44 && (fvf & ~0x80u) != 0x144) || flags) { + ((fvf & ~0x80u) != 0x44 && (fvf & ~0x80u) != 0x144 && fvf != 0x112) || 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; @@ -1666,9 +1721,52 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin struct gl_vertex *vertices = malloc(count * sizeof(*vertices)); uint16_t *indices = malloc(index_count * sizeof(*indices)); if (!vertices || !indices) abort(); + /* Affine world/view transforms also determine the inverse-transpose normal matrix. */ + float world_view[16], normal_matrix[9]; + if (fvf == 0x112) { + const float *world = (const float *)device->transforms[0], *view = (const float *)device->transforms[1]; + for (unsigned i = 0; i < 4; ++i) transform_vector(world + i * 4, view, world_view + i * 4); + if (world_view[3] || world_view[7] || world_view[11] || world_view[15] != 1) { + fprintf(stderr, "Unsupported non-affine world/view transform\n"); exit(3); + } + const float *m = world_view; + float cofactors[] = {m[5]*m[10]-m[6]*m[9], m[6]*m[8]-m[4]*m[10], m[4]*m[9]-m[5]*m[8], + m[2]*m[9]-m[1]*m[10], m[0]*m[10]-m[2]*m[8], m[1]*m[8]-m[0]*m[9], + m[1]*m[6]-m[2]*m[5], m[2]*m[4]-m[0]*m[6], m[0]*m[5]-m[1]*m[4]}; + float determinant = m[0]*cofactors[0] + m[1]*cofactors[1] + m[2]*cofactors[2]; + if (!determinant) { fprintf(stderr, "Unsupported singular world/view transform\n"); exit(3); } + 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 (fvf == 0x112) stride = 32; for (uint32_t i = 0; i < count; ++i) { + if (fvf == 0x112) { + float input_vertex[8], position[4], camera[4], projected[4], normal[3]; + memcpy(input_vertex, (void *)ADDR(input + i * stride), 32); + memcpy(position, input_vertex, 12); position[3] = 1; + transform_vector(position, world_view, camera); + transform_vector(camera, (const float *)device->transforms[2], projected); + float w = projected[3], width = device->target->width, height = device->target->height; + vertices[i].position[0] = projected[0] * device->viewport[2] / width + + w * ((2.0f * device->viewport[0] + device->viewport[2] + 1) / width - 1); + vertices[i].position[1] = projected[1] * device->viewport[3] / height + + w * (1 - (2.0f * device->viewport[1] + device->viewport[3] + 1) / height); + vertices[i].position[2] = projected[2]; vertices[i].position[3] = w; + for (unsigned j = 0; j < 3; ++j) + normal[j] = input_vertex[3] * normal_matrix[j] + input_vertex[4] * normal_matrix[3+j] + input_vertex[5] * normal_matrix[6+j]; + if (device->normalize_normals) normalize_vector(normal); + if (device->lighting) light_vertex(device, camera, normal, vertices[i].color); + else for (unsigned j = 0; j < 4; ++j) vertices[i].color[j] = 1; + vertices[i].fog = 0; + if (device->fog_vertex_mode == 3) { + float start, end; + 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, input_vertex + 6, 8); + continue; + } float position[4]; memcpy(position, (void *)ADDR(input + i * stride), 16); float w = 1.0f / position[3]; diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -484,6 +484,77 @@ static void indexed_vertex_buffers_use_relative_indices(uint32_t draw, uint32_t invoke(d3d, 2, 1); } +static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, uint32_t target, FILE *file) +{ + for (unsigned sample = 0; sample < 17; ++sample) { + uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128; + float vertices[3][8] = {{-.75f,.75f,.5f,0,0,-1,0,0}, {.75f,.75f,.5f,0,0,-1,1,0}, {-.75f,-.75f,.5f,0,0,-1,0,1}}; + float world[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; + float view[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; + float projection[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; + float material[] = {.4f,.6f,.8f,.5f, .5f,.25f,.75f,1, 0,0,0,0, 0,0,0,0, 0}; + float light[26] = {0, .5f,.75f,1,0, 0,0,0,0, .1f,.2f,.3f,0, 0,0,-1, 0,0,1, 10,2, 1,0,0, .5f,1.5f}; + uint32_t type = sample >= 9 && sample <= 10 ? 1 : sample >= 11 && sample <= 12 ? 2 : 3; + if (sample == 1) { material[12] = .2f; material[13] = .4f; material[14] = .6f; } + if (sample == 4 || sample == 5) for (unsigned i = 0; i < 3; ++i) vertices[i][5] = -2; + if (sample == 6) { vertices[0][5] = -.25f; vertices[1][5] = -.5f; vertices[2][5] = -.75f; } + if (sample == 7) { world[0] = .5f; world[5] = .75f; world[10] = .5f; world[12] = .125f; view[12] = -.25f; } + if (sample == 8) { view[0] = 0; view[1] = 1; view[4] = -1; view[5] = 0; light[16] = 1; } + if (sample == 9 || sample == 10) { light[13] = .3f; light[14] = .1f; light[21] = .5f; light[22] = .2f; light[23] = .1f; } + if (sample == 10) light[19] = .6f; + if (sample == 12) { light[24] = .1f; light[25] = 1; light[13] = -.5f; } + if (sample == 13 || sample == 14) { vertices[0][2] = .25f; vertices[1][2] = .5f; vertices[2][2] = .75f; } + if (sample == 14) { projection[10] = .5f; projection[11] = 1; projection[15] = 0; } + if (sample == 15) { world[0] = .5f; world[1] = .5f; world[4] = -.5f; world[5] = .5f; world[8] = .25f; vertices[0][3] = .5f; } + memcpy(memory(data), world, 64); assert(!invoke(device, 11, 3, 1u, data)); + memcpy(memory(data), view, 64); assert(!invoke(device, 11, 3, 2u, data)); + memcpy(memory(data), projection, 64); assert(!invoke(device, 11, 3, 3u, data)); + memcpy(memory(data), material, 68); assert(!invoke(device, 16, 2, data)); + memcpy(memory(data), light, 104); *memory(data) = type; + assert(!invoke(device, 18, 3, 0u, data)); + assert(!invoke(device, 44, 3, 0u, sample >= 3 ? 1u : 0u)); + assert(!invoke(device, 20, 3, 137u, sample ? 1u : 0u)); + assert(!invoke(device, 20, 3, 139u, sample == 2 ? 0x00804020u : 0u)); + assert(!invoke(device, 20, 3, 143u, sample == 5 ? 1u : 0u)); + assert(!invoke(device, 20, 3, 28u, sample == 13 || sample == 14 ? 1u : 0u)); + assert(!invoke(device, 20, 3, 140u, 3u)); + assert(!invoke(device, 20, 3, 34u, 0xffc02040u)); + assert(!invoke(device, 20, 3, 36u, 0u)); + assert(!invoke(device, 20, 3, 37u, 0x3f800000u)); + assert(!invoke(device, 20, 3, 27u, sample == 16 ? 1u : 0u)); + assert(!invoke(device, 20, 3, 19u, 5u)); + assert(!invoke(device, 20, 3, 20u, 6u)); + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x112u, 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); + if (sample == 0) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0xffffff); + if (sample == 1) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0x336699); + 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)); + } + float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; + memcpy(memory(workspace() + 256), identity, 64); + for (unsigned state = 1; state <= 3; ++state) assert(!invoke(device, 11, 3, state, workspace() + 256)); + assert(!invoke(device, 44, 3, 0u, 0u)); + assert(!invoke(device, 20, 3, 28u, 0u)); + assert(!invoke(device, 20, 3, 140u, 0u)); + assert(!invoke(device, 20, 3, 27u, 0u)); +} + static void initial_device_states(uint32_t device) { const uint32_t states[][2] = { @@ -989,6 +1060,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) depth_comparisons_control_visibility(device, color); transformed_render_states_preserve_rasterization(device, color); viewport_limits_clearing_and_drawing(device, color, file); + untransformed_vertices_apply_camera_and_lighting(device, color, file); assert(!fclose(file)); assert(!invoke(device, 2, 1)); assert(!invoke(draw, 2, 1)); diff --git a/tools/reconstruction/test_portable_devices.py b/tools/reconstruction/test_portable_devices.py @@ -19,14 +19,14 @@ wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] subprocess.run(wsl + [ "gcc", "-O2", "-Wall", "-Wextra", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", "tests/devices.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_work + "/translated", + "runtime/guest_heap.c", "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-lm", "-o", linux_work + "/translated", ], check=True) actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_work + "/translated", linux_work + "/native.rgb"], text=True, timeout=30) 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) == 1618944 +assert len(windows_pixels) == len(linux_pixels) == 1671168 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 539648 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 557056 pixels agree within one 8-bit channel step; maximum error:", error) diff --git a/tools/reconstruction/test_portable_graphics.py b/tools/reconstruction/test_portable_graphics.py @@ -19,7 +19,7 @@ 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/linux_window.c", "runtime/gl_renderer.c", "runtime/guest_heap.c", - "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-o", linux_output, + "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-lm", "-o", linux_output, ], check=True) actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_output], text=True, timeout=30) assert actual == expected, (expected, actual) diff --git a/tools/reconstruction/test_portable_surfaces.py b/tools/reconstruction/test_portable_surfaces.py @@ -19,7 +19,7 @@ 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/linux_window.c", "runtime/gl_renderer.c", "runtime/guest_heap.c", - "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-o", linux_output, + "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-lm", "-o", linux_output, ], check=True) actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_output], text=True, timeout=30) assert actual == expected, (expected, actual)