aorecomp

Anarchy Online classic in the browser

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


commit 1a1609394bac240c02504be7b7075b1f01d189ea
parent 662fa7c166d12d9a0821ac4fcdc43ac3ecf5151a
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 10:58:12 +0000

Prepare enabled device lights once per draw

Diffstat:
Msrc/runtime/linux_graphics.c | 15++++++++++-----
Msrc/tests/devices.c | 42++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -61,7 +61,7 @@ static struct surface_object *surface_buckets[2][4096]; struct device_light { uint32_t index, enabled, data[26]; float camera_vector[4], spot_direction[4], spot_inner, spot_outer; - struct device_light *next; + struct device_light *next, *next_enabled; }; struct texture_stage { uint32_t min_filter, mag_filter, mip_filter, address, address_u, address_v, color_op, alpha_op; @@ -84,7 +84,7 @@ static struct device_object { int scene; struct draw_object *owner; struct surface_object *target; - struct device_light *lights; + struct device_light *lights, *enabled_lights; struct device_object *next; } *devices; static struct vertex_buffer { @@ -1837,8 +1837,7 @@ static void light_vertex(struct device_object *device, const float position[4], for (unsigned j = 0; j < 3; ++j) color[j] = emissive_color[j] + material[4 + j] * ((device->ambient >> (16 - j * 8)) & 255) / 255.0f; color[3] = diffuse_color[3]; - for (struct device_light *light = device->lights; light; light = light->next) { - if (!light->enabled) continue; + for (struct device_light *light = device->enabled_lights; light; light = light->next_enabled) { const float *data = (const float *)light->data; float direction[4], attenuation = 1; memcpy(direction, light->camera_vector, sizeof(direction)); @@ -1908,10 +1907,16 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin 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; } - /* Light transforms and spot cone angles are constant throughout a draw. */ + /* Prepare enabled lights and their transforms in the original accumulation order. */ + device->enabled_lights = NULL; + struct device_light *last_enabled = NULL; if (untransformed && device->lighting) { for (struct device_light *light = device->lights; light; light = light->next) { if (!light->enabled) continue; + if (last_enabled) last_enabled->next_enabled = light; + else device->enabled_lights = light; + last_enabled = light; + light->next_enabled = NULL; const float *data = (const float *)light->data; float source[4]; if (light->data[0] == 3) { diff --git a/src/tests/devices.c b/src/tests/devices.c @@ -775,6 +775,47 @@ static void diffuse_material_source_selects_lighting_color(uint32_t device, uint assert(!invoke(device, 20, 3, 145u, 1u)); } +static void light_enable_changes_apply_between_draws(uint32_t device, uint32_t target) +{ + uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128; + const float vertices[][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}}; + const float material[] = {1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0}; + memcpy(memory(input), vertices, sizeof(vertices)); + memcpy(memory(data), material, sizeof(material)); + assert(!invoke(device, 16, 2, data)); + assert(!invoke(device, 20, 3, 137u, 1u)); + assert(!invoke(device, 20, 3, 139u, 0u)); + for (unsigned index = 0; index < 30; ++index) { + float light[26] = {0}; + light[1] = index == 1; + light[2] = index == 15; + light[3] = index == 29; + light[18] = 1; + memcpy(memory(data), light, sizeof(light)); + *memory(data) = 3; + assert(!invoke(device, 18, 3, index, data)); + assert(!invoke(device, 44, 3, index, 0u)); + } + const unsigned masks[] = {0, 5, 2, 0, 7, 1, 4}; + const uint32_t expected[] = {0, 0xff00ff, 0x00ff00, 0, 0xffffff, 0xff0000, 0x0000ff}; + for (unsigned sample = 0; sample < sizeof(masks) / sizeof(*masks); ++sample) { + assert(!invoke(device, 44, 3, 1u, masks[sample] & 1u)); + assert(!invoke(device, 44, 3, 15u, (masks[sample] >> 1) & 1u)); + assert(!invoke(device, 44, 3, 29u, (masks[sample] >> 2) & 1u)); + 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); + assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == expected[sample]); + assert(!invoke(target, 32, 2, 0u)); + } + assert(!invoke(device, 44, 3, 29u, 0u)); +} + static void initial_device_states(uint32_t device) { const uint32_t states[][2] = { @@ -1609,6 +1650,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) viewport_limits_clearing_and_drawing(device, color, file); untransformed_vertices_apply_camera_and_lighting(device, color, file); diffuse_material_source_selects_lighting_color(device, color); + light_enable_changes_apply_between_draws(device, color); depth_bias_offsets_overlapping_geometry(device, color, 16, file); assert(!invoke(color, 8, 3, 0u, depth)); memset(memory(desc), 0, 124);