aorecomp

Anarchy Online classic in the browser

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


commit a53309dc0d32d09c2590d68f17026aedde5c6670
parent 80a1b6eacf4feb97e08c106a0f343b3787415e76
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 13:52:50 +0000

Move simple vertex transforms and lighting onto the GPU

Diffstat:
Msrc/runtime/gl_renderer.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/runtime/gl_renderer.h | 13+++++++++++--
Msrc/runtime/linux_graphics.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 120 insertions(+), 4 deletions(-)

diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c @@ -24,6 +24,8 @@ EMSCRIPTEN_KEEPALIVE void browser_capture_frames(int count) #include <assert.h> static GLuint program, vertex_array, vertex_buffer, index_buffer; +static GLuint transform_buffer; +static GLint transform_enabled; static GLuint samplers[2]; static GLint texture_enabled[2], texture_alpha[2], texture_color_op[2], texture_alpha_op[2]; static GLint texture_color_arg2[2], texture_alpha_arg2[2]; @@ -86,7 +88,40 @@ int gl_renderer_initialize(void) "layout(location=4) in vec2 texcoord_second;\n" "layout(location=5) in vec3 specular; out vec3 highlight;\n" "out vec4 color; out vec2 uv, uv_second; 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; uv_second=texcoord_second; fog_factor=fog; highlight=specular; }\n", + "uniform bool transform_enabled;\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" + "};\n" + "void main() {\n" + " vec4 projected = position;\n" + " color = diffuse; uv = texcoord; uv_second = texcoord_second;\n" + " fog_factor = fog; highlight = specular;\n" + " if (transform_enabled) {\n" + " vec4 camera = world_view * position;\n" + " projected = projection * camera;\n" + " projected.xy = projected.xy * viewport.xy + projected.w * viewport.zw;\n" + " vec3 normal = normal_matrix * specular;\n" + " if (flags.y != 0.0) {\n" + " float length = sqrt(dot(normal, normal));\n" + " if (length != 0.0) normal /= length;\n" + " }\n" + " if (flags.x != 0.0) {\n" + " 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" + " }\n" + " color = clamp(color, 0.0, 1.0);\n" + " }\n" + " highlight = vec3(0.0);\n" + " fog_factor = fog_range.z != 0.0 ? clamp((fog_range.y - abs(camera.z)) / (fog_range.y - fog_range.x), 0.0, 1.0) : 0.0;\n" + " }\n" + " gl_Position = vec4(projected.x, -projected.y, 2.0 * projected.z - projected.w, projected.w);\n" + "}\n", SHADER_VERSION "in vec4 color; in vec2 uv, uv_second; in float fog_factor; out vec4 pixel;\n" "in vec3 highlight;\n" @@ -163,6 +198,12 @@ int gl_renderer_initialize(void) glUseProgram(program); glUniform1i(glGetUniformLocation(program, "image0"), 0); glUniform1i(glGetUniformLocation(program, "image1"), 1); + transform_enabled = glGetUniformLocation(program, "transform_enabled"); + glUniformBlockBinding(program, glGetUniformBlockIndex(program, "VertexTransform"), 0); + glGenBuffers(1, &transform_buffer); + glBindBuffer(GL_UNIFORM_BUFFER, transform_buffer); + glBufferData(GL_UNIFORM_BUFFER, sizeof(struct gl_vertex_transform), NULL, GL_STREAM_DRAW); + glBindBufferBase(GL_UNIFORM_BUFFER, 0, transform_buffer); glGenSamplers(2, samplers); /* This context owns one program, vertex array and streamed buffer pair. @@ -189,14 +230,24 @@ int gl_renderer_initialize(void) return 1; } +void gl_renderer_set_vertex_transform(const struct gl_vertex_transform *state) +{ + glUniform1i(transform_enabled, state != NULL); + if (state) { + glBindBuffer(GL_UNIFORM_BUFFER, transform_buffer); + glBufferData(GL_UNIFORM_BUFFER, sizeof(*state), state, GL_STREAM_DRAW); + } +} + void gl_renderer_shutdown(void) { + glDeleteBuffers(1, &transform_buffer); glDeleteSamplers(2, samplers); glDeleteBuffers(1, &index_buffer); glDeleteBuffers(1, &vertex_buffer); glDeleteVertexArrays(1, &vertex_array); glDeleteProgram(program); - index_buffer = vertex_buffer = vertex_array = program = 0; + transform_buffer = index_buffer = vertex_buffer = vertex_array = program = 0; } struct gl_depth *gl_depth_create(unsigned width, unsigned height, unsigned bits) diff --git a/src/runtime/gl_renderer.h b/src/runtime/gl_renderer.h @@ -6,12 +6,12 @@ struct gl_target; struct gl_depth; struct gl_vertex { - float position[4]; /* D3D clip coordinates: -w <= x,y <= w and 0 <= z <= w. */ + float position[4]; /* D3D clip coordinates, or object coordinates with GPU transforms. */ float color[4]; float uv[2]; float fog; float uv_second[2]; - float specular[3]; + float specular[3]; /* Specular RGB, or the normal with GPU transforms. */ }; struct gl_texture_state { struct gl_target *image; @@ -21,6 +21,15 @@ struct gl_texture_state { uint32_t factor; }; +/* vec4-sized fields match the shader's std140 uniform block. */ +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 fog[4]; /* start, end, linear fog enabled, padding */ + float flags[4]; /* lighting, normalize normals, vertex diffuse, vertex emissive */ +}; +void gl_renderer_set_vertex_transform(const struct gl_vertex_transform *state); int gl_renderer_initialize(void); void gl_renderer_shutdown(void); struct gl_target *gl_target_create(unsigned width, unsigned height, int with_depth); diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -1938,6 +1938,41 @@ 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)); + struct gl_vertex_transform gpu = {0}; + if (gpu_transform) { + memcpy(gpu.world_view, world_view, 64); + memcpy(gpu.projection, device->transforms[2], 64); + for (unsigned i = 0; i < 3; ++i) memcpy(gpu.normal + i * 4, normal_matrix + i * 3, 12); + float width = device->target->width, height = device->target->height; + gpu.viewport[0] = device->viewport[2] / width; + gpu.viewport[1] = device->viewport[3] / height; + gpu.viewport[2] = (2.0f * device->viewport[0] + device->viewport[2] + 1) / width - 1; + gpu.viewport[3] = 1 - (2.0f * device->viewport[1] + device->viewport[3] + 1) / height; + memcpy(gpu.material_diffuse, device->material, 16); + 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; + gpu.flags[0] = device->lighting; + gpu.flags[1] = device->normalize_normals; + gpu.flags[2] = device->diffuse_source == 1 && (fvf & 0x40); + gpu.flags[3] = device->emissive_source == 1 && (fvf & 0x40); + } + gl_renderer_set_vertex_transform(gpu_transform ? &gpu : NULL); /* Pretransformed vertices carry diffuse and optional specular colors. */ 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; @@ -1947,6 +1982,27 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin 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; + if (gpu_transform) { + memcpy(vertices[i].position, position, 16); + if (fvf & 0x10) memcpy(vertices[i].specular, input_vertex + 3, 12); + if (fvf & 0x40) { + uint32_t color = MEM32(input + i * stride + (fvf & 0x10 ? 24 : 12)); + vertices[i].color[0] = ((color >> 16) & 255) / 255.0f; + 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; + } else if (device->lighting) memcpy(vertices[i].color, device->material, 16); + else for (unsigned j = 0; j < 4; ++j) vertices[i].color[j] = 1; + vertices[i].fog = 0; + for (unsigned stage = 0; stage < 2; ++stage) { + float *uv = stage ? vertices[i].uv_second : vertices[i].uv; + unsigned coordinate = device->stages[stage].texture_coordinate; + uv[0] = uv[1] = 0; + if (coordinate < texture_count) + memcpy(uv, (const unsigned char *)input_vertex + stride - texture_count * 8 + coordinate * 8, 8); + } + continue; + } 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;