aorecomp

Anarchy Online classic in the browser

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


commit e3c7be8cdc1b66d9eb1d36eb70dbb175c0186ece
parent 892bed9e0ee9cf1009b4d298a915315361045f1e
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 13:52:11 +0000

Implement transformed vertex fog in native renderer

Diffstat:
Mdecompiled/runtime/gl_renderer.c | 25+++++++++++++++++++++----
Mdecompiled/runtime/gl_renderer.h | 2++
Mdecompiled/runtime/linux_graphics.c | 20+++++++++++++++++---
Mdecompiled/tests/devices.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/reconstruction/test_portable_devices.py | 4++--
5 files changed, 92 insertions(+), 9 deletions(-)

diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -8,6 +8,7 @@ static GLuint program, vertex_array, vertex_buffer, index_buffer; static GLint texture_enabled, texture_alpha, texture_modulate, texture_modulate_alpha; +static GLint fog_enabled, fog_color; static int depth_write = 1; static uint32_t depth_function = 4; static uint32_t cull_mode = 1; @@ -54,15 +55,18 @@ int gl_renderer_initialize(void) "layout(location=0) in vec4 position;\n" "layout(location=1) in vec4 diffuse;\n" "layout(location=2) in vec2 texcoord;\n" - "out vec4 color; out vec2 uv;\n" - "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; uv=texcoord; }\n", + "layout(location=3) in float fog;\n" + "out vec4 color; out vec2 uv; out float fog_factor;\n" + "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; uv=texcoord; fog_factor=fog; }\n", "#version 330 core\n" - "in vec4 color; in vec2 uv; out vec4 pixel;\n" + "in vec4 color; in vec2 uv; in float fog_factor; out vec4 pixel;\n" + "uniform bool fog_enabled; uniform vec3 fog_color;\n" "uniform sampler2D image;\n" "uniform bool textured, has_alpha, modulate, modulate_alpha;\n" "void main() { pixel=color; if(textured) { vec4 t=texture(image,uv);" "pixel.rgb=modulate ? t.rgb*color.rgb : t.rgb;" - "pixel.a=has_alpha ? t.a : 1.0; if(modulate_alpha) pixel.a*=color.a; } }\n" + "pixel.a=has_alpha ? t.a : 1.0; if(modulate_alpha) pixel.a*=color.a; }" + "if(fog_enabled) pixel.rgb=mix(fog_color,pixel.rgb,clamp(fog_factor,0.0,1.0)); }\n" }; program = glCreateProgram(); for (unsigned i = 0; i < 2; ++i) { @@ -97,6 +101,8 @@ int gl_renderer_initialize(void) texture_alpha = glGetUniformLocation(program, "has_alpha"); texture_modulate = glGetUniformLocation(program, "modulate"); texture_modulate_alpha = glGetUniformLocation(program, "modulate_alpha"); + fog_enabled = glGetUniformLocation(program, "fog_enabled"); + fog_color = glGetUniformLocation(program, "fog_color"); glUseProgram(program); glUniform1i(glGetUniformLocation(program, "image"), 0); @@ -110,6 +116,8 @@ int gl_renderer_initialize(void) glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, position)); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, color)); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, uv)); + glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, fog)); + glEnableVertexAttribArray(3); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); @@ -404,6 +412,15 @@ static GLenum blend_factor(uint32_t factor) } } +void gl_renderer_set_fog(int enabled, uint32_t color) +{ + glUseProgram(program); + glUniform1i(fog_enabled, enabled); + glUniform3f(fog_color, ((color >> 16) & 255) / 255.0f, + ((color >> 8) & 255) / 255.0f, (color & 255) / 255.0f); + check_graphics("fog state"); +} + void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination) { if (enabled) { diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -9,6 +9,7 @@ struct gl_vertex { float position[4]; /* D3D clip coordinates: -w <= x,y <= w and 0 <= z <= w. */ float color[4]; float uv[2]; + float fog; }; struct gl_texture_state { struct gl_target *image; @@ -34,6 +35,7 @@ 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_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height); void gl_renderer_set_texture(const struct gl_texture_state *state); +void gl_renderer_set_fog(int enabled, uint32_t color); void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination); void gl_renderer_set_depth(int write, uint32_t function); void gl_renderer_set_cull(uint32_t mode); diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -53,6 +53,7 @@ static struct device_object { uint32_t min_filter, mag_filter, mip_filter, address, address_u, address_v, color_op, alpha_op; uint32_t sampler_u, sampler_v; uint32_t alpha_arg2; + uint32_t fog_enabled, fog_color, fog_start, fog_end, fog_density; uint32_t blend, source_blend, destination_blend, depth_write, depth_function, clipping, lighting; uint32_t material[17], viewport[6], transforms[3][16]; int scene; @@ -1341,6 +1342,7 @@ static void create_device(void) device->depth_function = 4; device->clipping = 1; device->lighting = 1; + device->fog_end = device->fog_density = 0x3f800000; for (unsigned matrix = 0; matrix < 3; ++matrix) for (unsigned i = 0; i < 16; i += 5) device->transforms[matrix][i] = 0x3f800000; device->owner = owner; @@ -1529,8 +1531,10 @@ static uint32_t device_state(struct device_object *device, uint32_t state) case 2: case 40: return UINT32_MAX; /* Retired antialias state sentinels. */ case 4: case 16: return 1; /* Perspective textures and last line pixel. */ case 10: case 15: case 24: case 29: case 30: case 33: return 0; - case 34: case 35: case 36: case 47: case 48: return 0; /* Fog/depth bias. */ - case 37: case 38: return 0x3f800000; /* Fog end and density: 1.0. */ + case 35: case 47: case 48: return 0; /* Fog/depth bias. */ + case 36: return device->fog_start; + case 37: return device->fog_end; + case 38: return device->fog_density; case 25: case 56: return 8; /* ALWAYS alpha/stencil comparison. */ case 41: case 52: case 57: return 0; /* Color key and stencil disabled. */ case 53: case 54: case 55: return 1; /* KEEP stencil operations. */ @@ -1552,7 +1556,9 @@ static uint32_t device_state(struct device_object *device, uint32_t state) case 22: return device->cull; case 23: return device->depth_function; case 27: return device->blend; - case 26: case 28: return 0; /* Dither and fog. */ + case 26: return 0; /* Dither. */ + case 28: return device->fog_enabled; + case 34: return device->fog_color; default: fprintf(stderr, "Unsupported Direct3D render state %u\n", state); exit(3); } } @@ -1619,6 +1625,11 @@ static void device_set_state(void) uint32_t state = MEM32(g_esp + 8), value = MEM32(g_esp + 12); if (state == 22 && value >= 1 && value <= 3) device->cull = value; else if (state == 27 && value <= 1) device->blend = value; + else if (state == 28 && value <= 1) device->fog_enabled = value; + else if (state == 34) device->fog_color = value; + else if (state == 36) device->fog_start = value; + else if (state == 37) device->fog_end = value; + else if (state == 38) device->fog_density = value; else if (state == 14 && value <= 1) device->depth_write = value; else if (state == 23 && value >= 1 && value <= 8) device->depth_function = value; else if (state == 136 && value <= 1) device->clipping = value; @@ -1661,6 +1672,7 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin vertices[i].color[1] = ((color >> 8) & 255) / 255.0f; vertices[i].color[2] = (color & 255) / 255.0f; 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); } @@ -1677,12 +1689,14 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin device->texture->alpha, device->color_op == 4, device->alpha_op == 4, device->mip_filter == 2}; gl_renderer_set_texture(&state); } else gl_renderer_set_texture(NULL); + gl_renderer_set_fog(device->fog_enabled, device->fog_color); gl_renderer_set_blend(device->blend, device->source_blend, device->destination_blend); gl_renderer_set_depth(device->depth_write, device->depth_function); gl_renderer_set_cull(device->cull); const int32_t clip[] = {device->viewport[0], device->viewport[1], device->viewport[2], device->viewport[3]}; gl_target_draw(device->target->target, vertices, count, indices, index_count, clip); gl_renderer_set_blend(0, 2, 1); + gl_renderer_set_fog(0, 0); gl_renderer_set_texture(NULL); free(indices); free(vertices); diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -304,6 +304,55 @@ static void mip_levels_render_after_edits_and_eviction(uint32_t draw, uint32_t d invoke(d3d, 2, 1); } +static void transformed_vertex_fog_blends_rgb(uint32_t device, uint32_t target, FILE *file) +{ + uint32_t desc = workspace() + 128, input = workspace() + 512; + const uint32_t factors[][3] = {{0,0,0}, {128,128,128}, {255,255,255}, {0,128,255}, {0,128,255}, {0,128,255}, {0,0,0}}; + assert(!invoke(device, 20, 3, 27u, 0u)); + assert(!invoke(device, 20, 3, 34u, 0x9ac02040u)); + assert(!invoke(device, 21, 3, 34u, workspace()) && *memory(workspace()) == 0x9ac02040); + /* Precomputed transformed-vertex fog ignores distance formula parameters. */ + assert(!invoke(device, 20, 3, 36u, 0x3e4ccccdu)); + assert(!invoke(device, 20, 3, 37u, 0x3f4ccccdu)); + assert(!invoke(device, 20, 3, 38u, 0x3f000000u)); + assert(!invoke(device, 21, 3, 36u, workspace()) && *memory(workspace()) == 0x3e4ccccd); + assert(!invoke(device, 21, 3, 37u, workspace()) && *memory(workspace()) == 0x3f4ccccd); + assert(!invoke(device, 21, 3, 38u, workspace()) && *memory(workspace()) == 0x3f000000); + for (unsigned sample = 0; sample < 7; ++sample) { + struct { float xyzw[4]; uint32_t diffuse, specular; } vertices[] = { + {{4, 4, .25f, 1}, 0xff6080a0, (factors[sample][0] << 24) | 0xabcdef}, + {{28, 4, .25f, 1}, 0xff6080a0, (factors[sample][1] << 24) | 0xabcdef}, + {{4, 28, .25f, 1}, 0xff6080a0, (factors[sample][2] << 24) | 0xabcdef}, + }; + if (sample >= 4) { vertices[1].xyzw[3] = 2; vertices[2].xyzw[3] = .5f; } + if (sample == 6) { + for (unsigned i = 0; i < 3; ++i) memcpy(memory(input + i * 20), &vertices[i], 20); + } else memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 20, 3, 28u, sample == 5 ? 0u : 1u)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, sample == 6 ? 0x44u : 0xc4u, 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 || sample == 6) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0xc02040); + if (sample == 2 || sample == 5) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0x6080a0); + 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, 20, 3, 28u, 0u)); +} + static void vertex_buffer_draws_match_memory_draws(uint32_t draw, uint32_t device, uint32_t target, FILE *file) { const uint32_t iid[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; @@ -868,6 +917,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); textured_triangles(draw, device, color, file); 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); rectangle_clears_preserve_outside_depth(device, color, file); depth_write_controls_occlusion(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) == 1428480 +assert len(windows_pixels) == len(linux_pixels) == 1449984 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 476160 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 483328 pixels agree within one 8-bit channel step; maximum error:", error)