aorecomp

Anarchy Online classic in the browser

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


commit 9e2bf0b0f996dd057ee86dc3c9089b2b86571958
parent 39bdd0c38cff0366eb7195a7ae44c81e7117b42a
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 23:17:40 +0000

Create texture framebuffers only when rendering requires them

Diffstat:
Msrc/runtime/gl_renderer.c | 47+++++++++++++++++++++++++++--------------------
1 file changed, 27 insertions(+), 20 deletions(-)

diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c @@ -38,6 +38,7 @@ struct gl_target { int mip_dirty; }; static void make_resident(struct gl_target *target); +static void make_renderable(struct gl_target *target); static void invalidate_mips(struct gl_target *target) { @@ -209,7 +210,7 @@ void gl_depth_destroy(struct gl_depth *depth) void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth) { - make_resident(target); + make_renderable(target); assert(!depth || (depth->width == target->width && depth->height == target->height)); if (depth) ++depth->references; if (target->depth) gl_depth_destroy(target->depth); @@ -221,9 +222,8 @@ void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth) check_graphics("depth attachment"); } -static int allocate_target(struct gl_target *target) +static void allocate_texture(struct gl_target *target) { - /* Color and depth belong to the target, so switching targets preserves both. */ invalidate_mips(target); glGenTextures(1, &target->color); glBindTexture(GL_TEXTURE_2D, target->color); @@ -232,6 +232,20 @@ static int allocate_target(struct gl_target *target) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); + free(target->saved_pixels); + target->saved_pixels = NULL; +} + +static void make_resident(struct gl_target *target) +{ + if (target->saved_pixels) allocate_texture(target); +} + +static void make_renderable(struct gl_target *target) +{ + make_resident(target); + if (target->framebuffer) return; + /* 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); @@ -241,16 +255,8 @@ static int allocate_target(struct gl_target *target) glBindFramebuffer(GL_FRAMEBUFFER, 0); if (status != GL_FRAMEBUFFER_COMPLETE) { fprintf(stderr, "Native framebuffer creation failed: %04x\n", status); - return 0; + abort(); } - free(target->saved_pixels); - target->saved_pixels = NULL; - return 1; -} - -static void make_resident(struct gl_target *target) -{ - if (target->saved_pixels && !allocate_target(target)) abort(); } struct gl_target *gl_target_create(unsigned width, unsigned height, int with_depth) @@ -260,7 +266,8 @@ struct gl_target *gl_target_create(unsigned width, unsigned height, int with_dep target->width = width; target->height = height; if (with_depth) target->depth = gl_depth_create(width, height, 24); - if ((with_depth && !target->depth) || !allocate_target(target)) { gl_target_destroy(target); return NULL; } + if (with_depth && !target->depth) { gl_target_destroy(target); return NULL; } + allocate_texture(target); return target; } @@ -301,7 +308,7 @@ void gl_target_clear(struct gl_target *target, uint32_t argb, float depth) void gl_target_clear_rect(struct gl_target *target, uint32_t flags, uint32_t argb, float depth, const int32_t rectangle[4]) { invalidate_mips(target); - make_resident(target); + make_renderable(target); int32_t left = rectangle[0] < 0 ? 0 : rectangle[0], top = rectangle[1] < 0 ? 0 : rectangle[1]; int32_t right = rectangle[2] > (int32_t)target->width ? (int32_t)target->width : rectangle[2]; int32_t bottom = rectangle[3] > (int32_t)target->height ? (int32_t)target->height : rectangle[3]; @@ -327,7 +334,7 @@ void gl_target_draw(struct gl_target *target, unsigned primitive, const struct g unsigned vertex_count, const uint16_t *indices, unsigned index_count, const int32_t clip[4]) { invalidate_mips(target); - make_resident(target); + make_renderable(target); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glViewport(0, 0, target->width, target->height); glEnable(GL_SCISSOR_TEST); @@ -359,7 +366,7 @@ void gl_target_draw(struct gl_target *target, unsigned primitive, const struct g void gl_target_read(struct gl_target *target, unsigned char *rgba) { - make_resident(target); + make_renderable(target); glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glReadPixels(0, 0, target->width, target->height, GL_RGBA, GL_UNSIGNED_BYTE, rgba); glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -381,8 +388,8 @@ 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]) { invalidate_mips(target); - make_resident(source); - make_resident(target); + make_renderable(source); + make_renderable(target); glBindFramebuffer(GL_READ_FRAMEBUFFER, source->framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target->framebuffer); glDisable(GL_SCISSOR_TEST); @@ -393,7 +400,7 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height) { - make_resident(target); + make_renderable(target); glBindFramebuffer(GL_READ_FRAMEBUFFER, target->framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDisable(GL_SCISSOR_TEST); @@ -415,7 +422,7 @@ void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *stat if (state->image->mip_dirty) { unsigned level = 0; for (struct gl_target *mip = state->image->mip; mip; mip = mip->mip) { - make_resident(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);