aorecomp

Anarchy Online classic in the browser

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


commit c157a161e7fc88f99493c4619c8bbebe47e53b1b
parent 3445005cb8946fa38883a809659afbb746b0b974
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 11:58:49 +0000

Upload GPU-transformed vertices in their original format

Diffstat:
Msrc/runtime/gl_renderer.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Msrc/runtime/gl_renderer.h | 3+++
Msrc/runtime/linux_graphics.c | 24++++++++++++++++++++----
Msrc/tests/devices.c | 11++++++++++-
4 files changed, 114 insertions(+), 25 deletions(-)

diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c @@ -24,7 +24,8 @@ EMSCRIPTEN_KEEPALIVE void browser_capture_frames(int count) static GLuint program, vertex_array, vertex_buffer, index_buffer; static GLuint transform_buffer; -static GLint transform_enabled; +static GLint transform_enabled, color_bgra; +static unsigned input_fvf, input_coordinates[2]; 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]; @@ -72,6 +73,22 @@ static void check_graphics(const char *operation) } } +static void packed_vertex_format(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)); + glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, uv_second)); + glEnableVertexAttribArray(4); + glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, specular)); + glEnableVertexAttribArray(5); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); +} + int gl_renderer_initialize(void) { if (program) return 1; @@ -84,7 +101,7 @@ 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" - "uniform bool transform_enabled;\n" + "uniform bool transform_enabled, color_bgra;\n" "struct VertexLight { vec4 vector, diffuse, ambient, attenuation; };\n" "layout(std140) uniform VertexTransform {\n" " mat4 world_view, projection; mat3 normal_matrix;\n" @@ -93,7 +110,8 @@ int gl_renderer_initialize(void) "};\n" "void main() {\n" " vec4 projected = position;\n" - " color = diffuse; uv = texcoord; uv_second = texcoord_second;\n" + " vec4 vertex_diffuse = color_bgra ? diffuse.bgra : diffuse;\n" + " color = vertex_diffuse; uv = texcoord; uv_second = texcoord_second;\n" " fog_factor = fog; highlight = specular;\n" " if (transform_enabled) {\n" " vec4 camera = world_view * position;\n" @@ -105,8 +123,8 @@ int gl_renderer_initialize(void) " 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" + " vec4 lit_diffuse = flags.z != 0.0 ? vertex_diffuse : material_diffuse;\n" + " vec3 emissive = flags.w != 0.0 ? vertex_diffuse.rgb : material_emissive.rgb;\n" " color = vec4(emissive + material_ambient.rgb * ambient.rgb, lit_diffuse.a);\n" " for (int i = 0; i < 8; ++i) {\n" " if (lights[i].vector.w == 0.0) break;\n" @@ -205,6 +223,8 @@ int gl_renderer_initialize(void) glUseProgram(program); glUniform1i(glGetUniformLocation(program, "image0"), 0); glUniform1i(glGetUniformLocation(program, "image1"), 1); + color_bgra = glGetUniformLocation(program, "color_bgra"); + input_fvf = 0; transform_enabled = glGetUniformLocation(program, "transform_enabled"); glUniformBlockBinding(program, glGetUniformBlockIndex(program, "VertexTransform"), 0); glGenBuffers(1, &transform_buffer); @@ -221,18 +241,7 @@ int gl_renderer_initialize(void) glBindVertexArray(vertex_array); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); - 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)); - glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, uv_second)); - glEnableVertexAttribArray(4); - glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, specular)); - glEnableVertexAttribArray(5); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); + packed_vertex_format(); check_graphics("renderer initialization"); return 1; } @@ -405,8 +414,8 @@ void gl_target_clear_rect(struct gl_target *target, uint32_t flags, uint32_t arg glBindFramebuffer(GL_FRAMEBUFFER, 0); } -void gl_target_draw(struct gl_target *target, unsigned primitive, const struct gl_vertex *vertices, - unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]) +static void draw_data(struct gl_target *target, unsigned primitive, const void *vertices, + unsigned vertex_bytes, const uint16_t *indices, unsigned index_count, const int32_t clip[4]) { invalidate_mips(target); make_renderable(target); @@ -431,7 +440,7 @@ void gl_target_draw(struct gl_target *target, unsigned primitive, const struct g glPolygonOffset(-(float)depth_bias * .25f, -(float)depth_bias * .5f); } else glDisable(GL_POLYGON_OFFSET_FILL); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - glBufferData(GL_ARRAY_BUFFER, vertex_count * sizeof(*vertices), vertices, GL_STREAM_DRAW); + glBufferData(GL_ARRAY_BUFFER, vertex_bytes, vertices, GL_STREAM_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count * sizeof(*indices), indices, GL_STREAM_DRAW); glDrawElements(primitive == 2 ? GL_LINES : GL_TRIANGLES, index_count, GL_UNSIGNED_SHORT, NULL); glDisable(GL_SCISSOR_TEST); @@ -439,6 +448,58 @@ void gl_target_draw(struct gl_target *target, unsigned primitive, const struct g /* Check errors at presentation/readback without synchronizing every draw. */ } +void gl_target_draw(struct gl_target *target, unsigned primitive, const struct gl_vertex *vertices, + unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]) +{ + if (input_fvf) { + packed_vertex_format(); + glUniform1i(color_bgra, 0); + input_fvf = 0; + } + draw_data(target, primitive, vertices, vertex_count * sizeof(*vertices), indices, index_count, clip); +} + +void gl_target_draw_fvf(struct gl_target *target, unsigned primitive, const void *vertices, + unsigned fvf, const unsigned coordinates[2], const float color[4], unsigned vertex_count, + const uint16_t *indices, unsigned index_count, const int32_t clip[4]) +{ + unsigned textures = (fvf >> 8) & 15; + unsigned uv_offset = 12 + (fvf & 0x10 ? 12 : 0) + (fvf & 0x40 ? 4 : 0); + unsigned stride = uv_offset + textures * 8; + if (input_fvf != fvf || input_coordinates[0] != coordinates[0] || input_coordinates[1] != coordinates[1]) { + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, NULL); + if (fvf & 0x10) { + glEnableVertexAttribArray(5); + glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, stride, (void *)(uintptr_t)12); + } else { + glDisableVertexAttribArray(5); + glVertexAttrib3f(5, 0, 0, 0); + } + if (fvf & 0x40) { + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)(uintptr_t)(uv_offset - 4)); + } else glDisableVertexAttribArray(1); + for (unsigned stage = 0; stage < 2; ++stage) { + unsigned attribute = stage ? 4 : 2; + if (coordinates[stage] < textures) { + glEnableVertexAttribArray(attribute); + glVertexAttribPointer(attribute, 2, GL_FLOAT, GL_FALSE, stride, + (void *)(uintptr_t)(uv_offset + coordinates[stage] * 8)); + } else { + glDisableVertexAttribArray(attribute); + glVertexAttrib2f(attribute, 0, 0); + } + } + glDisableVertexAttribArray(3); + glVertexAttrib1f(3, 0); + glUniform1i(color_bgra, (fvf & 0x40) != 0); + input_fvf = fvf; + memcpy(input_coordinates, coordinates, sizeof(input_coordinates)); + } + if (!(fvf & 0x40)) glVertexAttrib4fv(1, color); + draw_data(target, primitive, vertices, vertex_count * stride, indices, index_count, clip); +} + void gl_target_read(struct gl_target *target, unsigned char *rgba) { make_renderable(target); diff --git a/src/runtime/gl_renderer.h b/src/runtime/gl_renderer.h @@ -49,6 +49,9 @@ void gl_target_clear_rect(struct gl_target *target, uint32_t flags, uint32_t arg /* Primitive is D3D line list (2) or triangle list (4); clip is guest top-down x, y, width, height. */ void gl_target_draw(struct gl_target *target, unsigned primitive, const struct gl_vertex *vertices, unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]); +void gl_target_draw_fvf(struct gl_target *target, unsigned primitive, const void *vertices, + unsigned fvf, const unsigned coordinates[2], const float color[4], unsigned vertex_count, + const uint16_t *indices, unsigned index_count, const int32_t clip[4]); void gl_target_read(struct gl_target *target, unsigned char *rgba); 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]); diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -1938,9 +1938,8 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin for (uint32_t i = 0; input_indices && i < element_count; ++i) if (MEM16(input_indices + i * 2) >= count) return; uint32_t index_count = primitive == 2 || primitive == 4 ? element_count : (element_count - 2) * 3; - struct gl_vertex *vertices = malloc(count * sizeof(*vertices)); uint16_t *indices = malloc(index_count * sizeof(*indices)); - if (!vertices || !indices) abort(); + if (!indices) abort(); /* Affine world/view transforms also determine the inverse-transpose normal matrix. */ float world_view[16], normal_matrix[9]; if (untransformed) { @@ -2033,7 +2032,19 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin /* 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; - for (uint32_t i = 0; i < count; ++i) { + const void *raw_vertices = gpu_transform ? (const void *)ADDR(input) : NULL; + if (raw_vertices) { + uint64_t end = (uint64_t)input + count * stride; + for (uint64_t page = ((uint64_t)input | 0xffffu) + 1; page < end; page += 0x10000) { + if (page > UINT32_MAX || ADDR((uint32_t)page) != (uintptr_t)raw_vertices + page - input) { + raw_vertices = NULL; + break; + } + } + } + struct gl_vertex *vertices = raw_vertices ? NULL : malloc(count * sizeof(*vertices)); + if (!raw_vertices && !vertices) abort(); + for (uint32_t i = 0; !raw_vertices && i < count; ++i) { memset(vertices[i].specular, 0, sizeof(vertices[i].specular)); if (untransformed) { const unsigned char *vertex = (const unsigned char *)ADDR(input + i * stride); @@ -2166,7 +2177,12 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin gl_renderer_set_depth(device->depth_write, device->depth_function, device->depth_bias); 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, primitive == 2 ? 2 : 4, vertices, count, indices, index_count, clip); + if (raw_vertices) { + const unsigned coordinates[] = {device->stages[0].texture_coordinate, device->stages[1].texture_coordinate}; + const float white[] = {1, 1, 1, 1}; + gl_target_draw_fvf(device->target->target, primitive == 2 ? 2 : 4, raw_vertices, fvf, coordinates, + device->lighting ? (const float *)device->material : white, count, indices, index_count, clip); + } else gl_target_draw(device->target->target, primitive == 2 ? 2 : 4, vertices, count, indices, index_count, clip); free(indices); free(vertices); g_eax = 0; diff --git a/src/tests/devices.c b/src/tests/devices.c @@ -641,6 +641,11 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui for (unsigned specular = 0; specular < 5; ++specular) 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; +#ifndef _WIN32 + /* Exercise vertex buffers across contiguous and independently mapped pages. */ + if (!colored && !specular && !emissive && sample < 2) + input = sample ? 0x5200ffe0 : 0x5100ffe0; +#endif 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}; @@ -706,7 +711,8 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui memcpy(memory(input + i * stride), vertices[i], color_offset); *memory(input + i * stride + color_offset) = sample == 6 ? (0xff204060u + i * 0x203020u) : 0x804060a0u; memcpy(memory(input + i * stride + color_offset + 4), vertices[i] + 6, 8); - } else memcpy(memory(input), vertices, sizeof(vertices)); + } else for (unsigned i = 0; i < 3; ++i) + memcpy(memory(input + i * sizeof(vertices[i])), vertices[i], sizeof(vertices[i])); assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); assert(!invoke(device, 5, 1)); @@ -1797,6 +1803,9 @@ int main(int argc, char **argv) #endif #ifndef _WIN32 assert(guest_map("device test", 0x50000000, 0x10000)); + assert(guest_map("split vertex page", 0x51000000, 0x10000)); + assert(guest_map("split vertex page", 0x51010000, 0x10000)); + assert(guest_map("contiguous vertex pages", 0x52000000, 0x20000)); g_esp = 0x5000f000; #endif #ifdef __EMSCRIPTEN__