aorecomp

Anarchy Online classic in the browser

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


commit be4f2c69feab82deb816d2384761e8b06008cc73
parent a49df82c54075b0ec7581def2792327af2e9c187
Author: anon <anon@example.com>
Date:   Mon, 19 Oct 2026 22:32:08 +0000

Share GPU mip storage and preserve sublevel lifetimes

Diffstat:
Msrc/Makefile.linux | 7+++++++
Msrc/Makefile.wasm | 4++++
Msrc/runtime/gl_renderer.c | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Msrc/runtime/linux_graphics.c | 5+++--
Asrc/tests/mip_storage.c | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 219 insertions(+), 15 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -227,6 +227,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/window ./build/linux/surfaces ./build/linux/devices build/linux/device-pixels.rgb + ./build/linux/mip-storage ./build/linux/data-record ./build/linux/qsort ./build/linux/finite @@ -279,6 +280,12 @@ build/linux/devices: tests/devices.c runtime/linux_graphics.c runtime/linux_wind test: build/linux/devices +build/linux/mip-storage: tests/mip_storage.c runtime/gl_renderer.c runtime/gl_renderer.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -lm -o $@ + +test: build/linux/mip-storage + build/linux/files: tests/files.c runtime/linux_file.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_file.h runtime/guest_memory.h runtime/recomp_types.h runtime/linux_path.c runtime/linux_path.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -144,6 +144,10 @@ build/wasm/web/capture-test.js: tests/browser_capture.c runtime/gl_renderer.c ru @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=1 -sOFFSCREENCANVAS_SUPPORT -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -o $@ +build/wasm/web/mip-storage.html: tests/mip_storage.c runtime/gl_renderer.c runtime/gl_renderer.h Makefile.wasm + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sUSE_SDL=2 -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sOFFSCREENCANVAS_SUPPORT -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sEXIT_RUNTIME -o $@ + build/wasm/vertex-initialization.js: tests/vertex_initialization.c game/bbr.dll/lifted-0061.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c @@ -27,6 +27,7 @@ static GLuint transform_buffer; static GLint transform_enabled, color_bgra; static unsigned input_fvf, input_coordinates[2]; static GLuint samplers[2]; +static struct gl_target *bound_images[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]; static GLint texture_color_arg1[2], texture_alpha_arg1[2]; @@ -42,7 +43,7 @@ struct gl_depth { GLuint buffer; }; struct gl_target { - unsigned width, height; + unsigned width, height, level; /* Level zero owns color; sublevels borrow storage. */ GLuint framebuffer, color; struct gl_depth *depth; unsigned char *saved_pixels; @@ -264,6 +265,7 @@ void gl_renderer_shutdown(void) glDeleteVertexArrays(1, &vertex_array); glDeleteProgram(program); transform_buffer = index_buffer = vertex_buffer = vertex_array = program = 0; + memset(bound_images, 0, sizeof(bound_images)); } struct gl_depth *gl_depth_create(unsigned width, unsigned height, unsigned bits) @@ -304,6 +306,7 @@ void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth) static void allocate_texture(struct gl_target *target) { + target->level = 0; invalidate_mips(target); glGenTextures(1, &target->color); glBindTexture(GL_TEXTURE_2D, target->color); @@ -328,7 +331,7 @@ static void make_renderable(struct gl_target *target) /* Sampled textures need no framebuffer or synchronous completeness query. */ glGenFramebuffers(1, &target->framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->color, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->color, target->level); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target->depth ? target->depth->buffer : 0); /* RGBA8 attachments have a fixed layout. Browser completeness queries would @@ -343,6 +346,49 @@ static void make_renderable(struct gl_target *target) glBindFramebuffer(GL_FRAMEBUFFER, 0); } +/* A separately sampled or rendered sublevel needs its own level-zero image. */ +static void make_independent(struct gl_target *target) +{ + if (!target->level) return; + make_renderable(target); + GLint texture; + glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture); + GLuint framebuffer = target->framebuffer; + target->framebuffer = 0; + allocate_texture(target); + glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, target->width, target->height); + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + glDeleteFramebuffers(1, &framebuffer); + glBindTexture(GL_TEXTURE_2D, texture); +} + +/* Preserve retained children before removing the texture that backs their pixels. */ +static void materialize_mips(struct gl_target *target) +{ + if (target->mip) materialize_mips(target->mip); + make_independent(target); +} + +/* Move descendants first so deleting an old owner never invalidates a child. */ +static unsigned share_mips(struct gl_target *image, struct gl_target *mip, unsigned level) +{ + unsigned last = mip->mip ? share_mips(image, mip->mip, level + 1) : level; + make_resident(mip); + if (mip->color == image->color && mip->level == level) return last; + make_renderable(mip); + glBindFramebuffer(GL_READ_FRAMEBUFFER, mip->framebuffer); + glBindTexture(GL_TEXTURE_2D, image->color); + glCopyTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, 0, 0, mip->width, mip->height, 0); + if (mip == bound_images[0] || mip == bound_images[1]) return last; + glDeleteFramebuffers(1, &mip->framebuffer); + if (!mip->level) glDeleteTextures(1, &mip->color); + mip->framebuffer = 0; + mip->color = image->color; + mip->level = level; + return last; +} + struct gl_target *gl_target_create(unsigned width, unsigned height, int with_depth) { struct gl_target *target = calloc(1, sizeof(*target)); @@ -357,14 +403,19 @@ struct gl_target *gl_target_create(unsigned width, unsigned height, int with_dep void gl_target_destroy(struct gl_target *target) { - if (target->mip) target->mip->parent = NULL; + for (unsigned stage = 0; stage < 2; ++stage) + if (bound_images[stage] == target) bound_images[stage] = NULL; + if (target->mip) { + materialize_mips(target->mip); + target->mip->parent = NULL; + } if (target->parent) { target->parent->mip = NULL; invalidate_mips(target->parent); } glDeleteFramebuffers(1, &target->framebuffer); if (target->depth) gl_depth_destroy(target->depth); - glDeleteTextures(1, &target->color); + if (!target->level) glDeleteTextures(1, &target->color); free(target->saved_pixels); free(target); } @@ -374,14 +425,17 @@ void gl_target_evict(struct gl_target *target, const unsigned char *rgba) /* Managed textures retain their contents while releasing GPU allocations. */ assert(!target->depth); if (target->saved_pixels) return; + if (!target->level && target->mip) materialize_mips(target->mip); unsigned char *pixels = malloc((size_t)target->width * target->height * 4); if (!pixels) abort(); if (rgba) memcpy(pixels, rgba, (size_t)target->width * target->height * 4); else gl_target_read(target, pixels); glDeleteFramebuffers(1, &target->framebuffer); - glDeleteTextures(1, &target->color); + if (!target->level) glDeleteTextures(1, &target->color); target->framebuffer = target->color = 0; + target->level = 0; target->saved_pixels = pixels; + invalidate_mips(target); } void gl_target_clear(struct gl_target *target, uint32_t argb, float depth) @@ -419,6 +473,7 @@ static void draw_data(struct gl_target *target, unsigned primitive, const void * unsigned vertex_bytes, const uint16_t *indices, unsigned index_count, const int32_t clip[4]) { invalidate_mips(target); + make_independent(target); make_renderable(target); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glViewport(0, 0, target->width, target->height); @@ -518,13 +573,19 @@ void gl_target_write(struct gl_target *target, const unsigned char *rgba) invalidate_mips(target); make_resident(target); glBindTexture(GL_TEXTURE_2D, target->color); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, target->width, target->height, + glTexSubImage2D(GL_TEXTURE_2D, target->level, 0, 0, target->width, target->height, GL_RGBA, GL_UNSIGNED_BYTE, 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]) { invalidate_mips(target); + make_resident(source); + make_resident(target); + if (source != target && source->color == target->color) { + if (source->level) make_independent(source); + else make_independent(target); + } make_renderable(source); make_renderable(target); glBindFramebuffer(GL_READ_FRAMEBUFFER, source->framebuffer); @@ -569,19 +630,15 @@ void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *state) { assert(stage < 2); + bound_images[stage] = state ? state->image : NULL; glUniform1i(texture_enabled[stage], state != NULL); glActiveTexture(GL_TEXTURE0 + stage); glBindSampler(stage, samplers[stage]); if (state && state->image) { + make_independent(state->image); make_resident(state->image); if (state->image->mip_dirty) { - unsigned level = 0; - for (struct gl_target *mip = state->image->mip; mip; mip = mip->mip) { - make_renderable(mip); - glBindFramebuffer(GL_READ_FRAMEBUFFER, mip->framebuffer); - glBindTexture(GL_TEXTURE_2D, state->image->color); - glCopyTexImage2D(GL_TEXTURE_2D, ++level, GL_RGBA8, 0, 0, mip->width, mip->height, 0); - } + unsigned level = state->image->mip ? share_mips(state->image, state->image->mip, 1) : 0; glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glBindTexture(GL_TEXTURE_2D, state->image->color); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level); diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -1058,8 +1058,6 @@ static uint32_t release_surface(struct surface_object *surface, unsigned index) struct surface_object **link = &surfaces; while (*link != surface) link = &(*link)->next; *link = surface->next; - if (surface->target) gl_target_destroy(surface->target); - if (surface->depth) gl_depth_destroy(surface->depth); while (surface->data) { struct surface_data *next = surface->data->next; free(surface->data); @@ -1075,6 +1073,9 @@ static uint32_t release_surface(struct surface_object *surface, unsigned index) } release_surface(surface->attached, 0); } + /* Release implicit children before the texture backing their mip levels. */ + if (surface->target) gl_target_destroy(surface->target); + if (surface->depth) gl_depth_destroy(surface->depth); if (surface->clipper) release_clipper(surface->clipper); guest_free(surface->pixels); if (!(surface->caps2 & 0x10000)) { diff --git a/src/tests/mip_storage.c b/src/tests/mip_storage.c @@ -0,0 +1,135 @@ +#include "gl_renderer.h" +#include <SDL2/SDL.h> +#include <assert.h> +#include <stdio.h> +#include <string.h> +#ifdef __EMSCRIPTEN__ +#include <emscripten/html5.h> +#endif + +static void pixel_is(struct gl_target *target, unsigned size, uint32_t color) +{ + unsigned char pixels[64]; + gl_target_read(target, pixels); + for (unsigned i = 0; i < size * size; ++i) { + assert(pixels[i * 4] == ((color >> 16) & 255)); + assert(pixels[i * 4 + 1] == ((color >> 8) & 255)); + assert(pixels[i * 4 + 2] == (color & 255)); + assert(pixels[i * 4 + 3] == (color >> 24)); + } +} + +int main(void) +{ + assert(!SDL_Init(SDL_INIT_VIDEO)); +#ifdef __EMSCRIPTEN__ + EmscriptenWebGLContextAttributes attributes; + emscripten_webgl_init_context_attributes(&attributes); + attributes.majorVersion = 2; + attributes.antialias = 0; + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#canvas", &attributes); + assert(context > 0); + assert(emscripten_webgl_make_context_current(context) == EMSCRIPTEN_RESULT_SUCCESS); +#else + assert(!SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)); + assert(!SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)); + assert(!SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)); + SDL_Window *window = SDL_CreateWindow("Mip storage test", 0, 0, 32, 32, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL); + assert(window); + SDL_GLContext context = SDL_GL_CreateContext(window); + assert(context); +#endif + assert(gl_renderer_initialize()); + + struct gl_target *root = gl_target_create(4, 4, 0); + struct gl_target *child = gl_target_create(2, 2, 0); + struct gl_target *leaf = gl_target_create(1, 1, 0); + gl_target_clear(root, 0xffff0000, 1); + gl_target_clear(child, 0xff00ff00, 1); + gl_target_clear(leaf, 0xff0000ff, 1); + gl_target_set_mip(root, child); + gl_target_set_mip(child, leaf); + struct gl_texture_state state = {.image = root, .mip_point = 1}; + + gl_renderer_set_texture(0, &state); + gl_target_clear(child, 0xffffff00, 1); + const unsigned char magenta[] = {255, 0, 255, 255}; + gl_target_write(leaf, magenta); + state.image = child; + gl_renderer_set_texture(1, &state); + state.image = root; + gl_renderer_set_texture(0, &state); + + pixel_is(root, 4, 0xffff0000); + pixel_is(child, 2, 0xffffff00); + pixel_is(leaf, 1, 0xffff00ff); + + // Blits must keep aliased source and destination images independent. + const int32_t root_rect[] = {0, 0, 4, 4}, child_rect[] = {0, 0, 2, 2}; + gl_target_blit(root, child, root_rect, child_rect); + pixel_is(child, 2, 0xffff0000); + pixel_is(leaf, 1, 0xffff00ff); + gl_target_clear(child, 0xff00ffff, 1); + gl_target_blit(child, root, child_rect, root_rect); + pixel_is(root, 4, 0xff00ffff); + + // Independently bound levels must remain valid in either texture-stage order. + struct gl_target *output = gl_target_create(2, 2, 0); + const struct gl_vertex vertices[] = { + {.position = {-1, -1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {0, 0}, .uv_second = {0, 0}}, + {.position = {1, -1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {1, 0}, .uv_second = {1, 0}}, + {.position = {-1, 1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {0, 1}, .uv_second = {0, 1}}, + {.position = {1, 1, .5f, 1}, .color = {1, 1, 1, 1}, .uv = {1, 1}, .uv_second = {1, 1}} + }; + const uint16_t indices[] = {0, 1, 2, 1, 3, 2}; + const int32_t clip[] = {0, 0, 2, 2}; + gl_target_clear(root, 0xffff8040, 1); + gl_target_clear(child, 0xff40ff80, 1); + state.color_op = state.alpha_op = 4; + state.color_arg1 = state.alpha_arg1 = 2; + state.alpha = 1; + state.mip_point = 0; + for (unsigned order = 0; order < 2; ++order) { + state.image = order ? child : root; + state.color_arg2 = state.alpha_arg2 = 0; + gl_renderer_set_texture(0, &state); + state.image = order ? root : child; + state.color_arg2 = state.alpha_arg2 = 1; + gl_renderer_set_texture(1, &state); + gl_target_draw(output, 4, vertices, 4, indices, 6, clip); + pixel_is(output, 2, 0xff408020); + } + gl_target_destroy(output); + + // Rendering into a borrowed sublevel must not feed back through its parent. + gl_renderer_set_texture(1, NULL); + gl_target_clear(root, 0xff550011, 1); + state.image = root; + state.color_arg2 = state.alpha_arg2 = 0; + gl_renderer_set_texture(0, &state); + gl_target_draw(child, 4, vertices, 4, indices, 6, clip); + pixel_is(child, 2, 0xff550011); + pixel_is(leaf, 1, 0xffff00ff); + + // Parent eviction and destruction preserve retained child levels. + gl_renderer_set_texture(1, NULL); + gl_renderer_set_texture(0, &state); + gl_target_evict(root, NULL); + pixel_is(child, 2, 0xff550011); + gl_renderer_set_texture(0, &state); + gl_target_destroy(root); + pixel_is(child, 2, 0xff550011); + pixel_is(leaf, 1, 0xffff00ff); + gl_target_destroy(child); + pixel_is(leaf, 1, 0xffff00ff); + gl_target_destroy(leaf); + gl_renderer_shutdown(); +#ifdef __EMSCRIPTEN__ + emscripten_webgl_destroy_context(context); +#else + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(window); +#endif + SDL_Quit(); + puts("mip storage: child edits, independent binding, blits, eviction and retained descendants passed"); +}