aorecomp

Anarchy Online classic in the browser

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


commit 0910e6809ed0fc4d58fc55c7b4a3a984968b62b6
parent 11fe72be9ca6091650e3e6d67ff04584bd173f66
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 10:35:04 +0000

Move point and multiple directional lights to the GPU

Diffstat:
Msrc/runtime/gl_renderer.c | 19+++++++++++++++----
Msrc/runtime/gl_renderer.h | 7++++++-
Msrc/runtime/linux_graphics.c | 25++++++++++++++++---------
Msrc/tests/devices.c | 26+++++++++++++++++++++++++-
4 files changed, 62 insertions(+), 15 deletions(-)

diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c @@ -85,10 +85,11 @@ int gl_renderer_initialize(void) "layout(location=5) in vec3 specular; out vec3 highlight;\n" "out vec4 color; out vec2 uv, uv_second; out float fog_factor;\n" "uniform bool transform_enabled;\n" + "struct VertexLight { vec4 vector, diffuse, ambient, attenuation; };\n" "layout(std140) uniform VertexTransform {\n" " mat4 world_view, projection; mat3 normal_matrix;\n" " vec4 viewport, material_diffuse, material_ambient, material_emissive;\n" - " vec4 ambient, light_direction, light_diffuse, light_ambient, fog_range, flags;\n" + " vec4 ambient; VertexLight lights[8]; vec4 fog_range, flags;\n" "};\n" "void main() {\n" " vec4 projected = position;\n" @@ -107,9 +108,19 @@ int gl_renderer_initialize(void) " vec4 lit_diffuse = flags.z != 0.0 ? diffuse : material_diffuse;\n" " vec3 emissive = flags.w != 0.0 ? diffuse.rgb : material_emissive.rgb;\n" " color = vec4(emissive + material_ambient.rgb * ambient.rgb, lit_diffuse.a);\n" - " if (light_direction.w != 0.0) {\n" - " float intensity = max(0.0, dot(normal, light_direction.xyz));\n" - " color.rgb += lit_diffuse.rgb * light_diffuse.rgb * intensity + material_ambient.rgb * light_ambient.rgb;\n" + " for (int i = 0; i < 8; ++i) {\n" + " if (lights[i].vector.w == 0.0) break;\n" + " vec3 direction = lights[i].vector.xyz; float attenuation = 1.0;\n" + " if (lights[i].vector.w == 1.0) {\n" + " direction -= camera.xyz;\n" + " float distance = sqrt(dot(direction, direction));\n" + " if (distance > lights[i].attenuation.w) continue;\n" + " if (distance != 0.0) direction /= distance;\n" + " vec3 a = lights[i].attenuation.xyz;\n" + " attenuation = 1.0 / (a.x + distance * a.y + distance * distance * a.z);\n" + " }\n" + " float intensity = max(0.0, dot(normal, direction));\n" + " color.rgb += attenuation * (lit_diffuse.rgb * lights[i].diffuse.rgb * intensity + material_ambient.rgb * lights[i].ambient.rgb);\n" " }\n" " color = clamp(color, 0.0, 1.0);\n" " }\n" diff --git a/src/runtime/gl_renderer.h b/src/runtime/gl_renderer.h @@ -25,7 +25,12 @@ struct gl_texture_state { struct gl_vertex_transform { float world_view[16], projection[16], normal[12]; float viewport[4], material_diffuse[4], material_ambient[4], material_emissive[4]; - float ambient[4], light_direction[4], light_diffuse[4], light_ambient[4]; + float ambient[4]; + struct { + float vector[4]; /* Camera-space xyz; w is the D3D type, or zero for the end. */ + float diffuse[4], ambient[4]; + float attenuation[4]; /* constant, linear, quadratic, range */ + } lights[8]; float fog[4]; /* start, end, linear fog enabled, padding */ float flags[4]; /* lighting, normalize normals, vertex diffuse, vertex emissive */ }; diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -1991,10 +1991,23 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin /* Use GPU transforms for draws with no specular or generated texture coordinates. */ int gpu_transform = untransformed && !device->specular && !device->stages[0].texture_transform && !device->stages[1].texture_transform && - device->stages[0].texture_coordinate < 0x10000 && device->stages[1].texture_coordinate < 0x10000 && - (!device->lighting || !device->enabled_lights || - (device->enabled_lights->data[0] == 3 && !device->enabled_lights->next_enabled)); + device->stages[0].texture_coordinate < 0x10000 && device->stages[1].texture_coordinate < 0x10000; struct gl_vertex_transform gpu = {0}; + /* Keep spotlights and light sets beyond the shader capacity on the CPU. */ + unsigned light_count = 0; + for (struct device_light *light = device->enabled_lights; gpu_transform && light; light = light->next_enabled) { + if (light->data[0] == 2 || light_count == sizeof(gpu.lights) / sizeof(gpu.lights[0])) { + gpu_transform = 0; + break; + } + memcpy(gpu.lights[light_count].vector, light->camera_vector, 12); + gpu.lights[light_count].vector[3] = light->data[0]; + memcpy(gpu.lights[light_count].diffuse, light->data + 1, 16); + memcpy(gpu.lights[light_count].ambient, light->data + 9, 16); + memcpy(gpu.lights[light_count].attenuation, light->data + 21, 12); + memcpy(gpu.lights[light_count].attenuation + 3, light->data + 19, 4); + ++light_count; + } if (gpu_transform) { memcpy(gpu.world_view, world_view, 64); memcpy(gpu.projection, device->transforms[2], 64); @@ -2008,12 +2021,6 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin memcpy(gpu.material_ambient, device->material + 4, 16); memcpy(gpu.material_emissive, device->material + 12, 16); for (unsigned i = 0; i < 3; ++i) gpu.ambient[i] = ((device->ambient >> (16 - i * 8)) & 255) / 255.0f; - if (device->enabled_lights) { - memcpy(gpu.light_direction, device->enabled_lights->camera_vector, 12); - gpu.light_direction[3] = 1; - memcpy(gpu.light_diffuse, device->enabled_lights->data + 1, 16); - memcpy(gpu.light_ambient, device->enabled_lights->data + 9, 16); - } memcpy(gpu.fog, &device->fog_start, 4); memcpy(gpu.fog + 1, &device->fog_end, 4); gpu.fog[2] = device->fog_vertex_mode == 3; diff --git a/src/tests/devices.c b/src/tests/devices.c @@ -639,7 +639,7 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui { for (unsigned emissive = 0; emissive < 3; ++emissive) for (unsigned specular = 0; specular < 5; ++specular) - for (unsigned colored = 0; colored <= 2; ++colored) for (unsigned sample = 0; sample < 17; ++sample) { + for (unsigned colored = 0; colored <= 2; ++colored) for (unsigned sample = 0; sample < 23; ++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}; @@ -667,6 +667,11 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui 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; } + /* Mixed lights, shader capacity, range boundaries and coincident vertices. */ + if (sample >= 17) type = 1; + if (sample == 20) { light[13] = -.75f; light[14] = .75f; light[15] = .5f; } + if (sample == 21) { light[13] = -.75f; light[14] = .75f; light[15] = -.5f; light[19] = 1; } + if (sample == 22) { view[0] = 0; view[1] = 1; view[4] = -1; view[5] = 0; view[12] = .1f; } 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)); @@ -676,6 +681,15 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui 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)); + unsigned lights = sample == 17 ? 2 : sample == 18 ? 8 : sample == 19 ? 9 : 1; + for (unsigned i = 1; i < 9; ++i) { + light[1] = .03f; light[2] = .02f; light[3] = .01f; + light[9] = .01f; light[10] = .02f; light[11] = .03f; + light[13] = i * .1f; + memcpy(memory(data), light, 104); *memory(data) = i % 2 ? 3 : 1; + assert(!invoke(device, 18, 3, i, data)); + assert(!invoke(device, 44, 3, i, i < lights ? 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)); @@ -702,6 +716,16 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui *memory(desc) = 124; assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + if (!emissive && !specular && !colored && sample >= 17) { + const uint32_t expected[] = {0x3b6fe8, 0x5287ff, 0x568aff, 0x0d0d39, 0x2a55aa, 0x366ae0}; + uint32_t pixel = *memory(pixels + 8 * pitch + 32); + /* Allow one quantization step for GPU floating-point rounding. */ + for (unsigned channel = 0; channel < 3; ++channel) { + int difference = (int)((pixel >> (channel * 8)) & 255) - + (int)((expected[sample - 17] >> (channel * 8)) & 255); + assert(difference >= -1 && difference <= 1); + } + } if (sample == 0) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == (colored ? 0x4060a0 : 0xffffff)); if (sample == 1) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == (emissive == 1 && colored ? 0x4060a0 : 0x336699));