commit 7df41efa879766050f474c34ce846e547871690b
parent 20b620d9e62cbf0bca191263867c70f5da61f804
Author: anon <anon@example.com>
Date: Tue, 20 Oct 2026 03:12:28 +0000
Retain lossless texture copies and release idle GPU images
Diffstat:
2 files changed, 220 insertions(+), 39 deletions(-)
diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c
@@ -14,6 +14,7 @@ EMSCRIPTEN_KEEPALIVE void browser_capture_frames(int count)
#define SHADER_VERSION "#version 300 es\nprecision highp float;\nprecision highp int;\n"
#else
#include <SDL2/SDL_opengl.h>
+#include <SDL2/SDL_timer.h>
#define SHADER_VERSION "#version 330 core\n"
#endif
#include <stddef.h>
@@ -50,7 +51,11 @@ struct gl_target {
uLong saved_compressed_size;
struct gl_target *mip, *parent;
int mip_dirty;
+ double last_used;
+ struct gl_target *previous, *next;
};
+static struct gl_target *targets;
+static double texture_frame_time, texture_last_sweep;
static void make_resident(struct gl_target *target);
static void make_renderable(struct gl_target *target);
@@ -304,33 +309,62 @@ void gl_target_set_depth(struct gl_target *target, struct gl_depth *depth)
check_graphics("depth attachment");
}
+/* Raw saved pixels are borrowed; compressed pixels need a temporary buffer. */
+static unsigned char *decode_pixels(struct gl_target *target)
+{
+ if (!target->saved_compressed_size) return target->saved_pixels;
+ uLongf size = (size_t)target->width * target->height * 4;
+ unsigned char *pixels = malloc(size);
+ if (!pixels || uncompress(pixels, &size, target->saved_pixels,
+ target->saved_compressed_size) != Z_OK ||
+ size != (size_t)target->width * target->height * 4) abort();
+ return pixels;
+}
+
+static void save_pixels(struct gl_target *target, unsigned char *pixels)
+{
+ /* Small mip tails stay raw; compress larger inactive images losslessly. */
+ uLong size = (size_t)target->width * target->height * 4;
+ if (size > 64) {
+ uLongf compressed_size = compressBound(size);
+ unsigned char *compressed = malloc(compressed_size);
+ if (!compressed || compress2(compressed, &compressed_size, pixels, size, Z_BEST_SPEED) != Z_OK) abort();
+ if (compressed_size < size) {
+ free(pixels);
+ pixels = realloc(compressed, compressed_size);
+ if (!pixels) abort();
+ target->saved_compressed_size = compressed_size;
+ } else free(compressed);
+ }
+ target->saved_pixels = pixels;
+}
+
+static void discard_saved_pixels(struct gl_target *target)
+{
+ free(target->saved_pixels);
+ target->saved_pixels = NULL;
+ target->saved_compressed_size = 0;
+}
+
static void allocate_texture(struct gl_target *target)
{
invalidate_mips(target);
glGenTextures(1, &target->color);
glBindTexture(GL_TEXTURE_2D, target->color);
- unsigned char *pixels = target->saved_pixels;
- if (target->saved_compressed_size) {
- uLongf size = (size_t)target->width * target->height * 4;
- pixels = malloc(size);
- if (!pixels || uncompress(pixels, &size, target->saved_pixels,
- target->saved_compressed_size) != Z_OK ||
- size != (size_t)target->width * target->height * 4) abort();
- }
+ unsigned char *pixels = decode_pixels(target);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, target->width, target->height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
if (pixels != target->saved_pixels) free(pixels);
- target->saved_compressed_size = 0;
+ if (target->depth) discard_saved_pixels(target);
+ target->last_used = texture_frame_time;
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);
+ if (!target->color) allocate_texture(target);
}
static void make_renderable(struct gl_target *target)
@@ -365,16 +399,21 @@ struct gl_target *gl_target_create(unsigned width, unsigned height, int with_dep
{
struct gl_target *target = calloc(1, sizeof(*target));
if (!target) return NULL;
+ target->next = targets;
+ if (targets) targets->previous = target;
+ targets = target;
target->width = width;
target->height = height;
if (with_depth) target->depth = gl_depth_create(width, height, 24);
if (with_depth && !target->depth) { gl_target_destroy(target); return NULL; }
- allocate_texture(target);
return target;
}
void gl_target_destroy(struct gl_target *target)
{
+ if (target->previous) target->previous->next = target->next;
+ else targets = target->next;
+ if (target->next) target->next->previous = target->previous;
if (target->mip) target->mip->parent = NULL;
if (target->parent) {
target->parent->mip = NULL;
@@ -389,30 +428,19 @@ void gl_target_destroy(struct gl_target *target)
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;
- 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);
+ if (!target->color && !rgba) return;
+ if (rgba || !target->saved_pixels) {
+ 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);
+ discard_saved_pixels(target);
+ save_pixels(target, pixels);
+ }
glDeleteFramebuffers(1, &target->framebuffer);
glDeleteTextures(1, &target->color);
target->framebuffer = target->color = 0;
- /* Small mip tails stay raw; compress larger inactive images losslessly. */
- uLong size = (size_t)target->width * target->height * 4;
- if (size > 64) {
- uLongf compressed_size = compressBound(size);
- unsigned char *compressed = malloc(compressed_size);
- if (!compressed || compress2(compressed, &compressed_size, pixels, size, Z_BEST_SPEED) != Z_OK) abort();
- if (compressed_size < size) {
- free(pixels);
- pixels = realloc(compressed, compressed_size);
- if (!pixels) abort();
- target->saved_compressed_size = compressed_size;
- } else free(compressed);
- }
- target->saved_pixels = pixels;
invalidate_mips(target);
}
@@ -430,6 +458,7 @@ void gl_target_clear_rect(struct gl_target *target, uint32_t flags, uint32_t arg
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];
if (left >= right || top >= bottom) return;
+ discard_saved_pixels(target);
glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer);
glEnable(GL_SCISSOR_TEST);
glScissor(left, top, right - left, bottom - top);
@@ -452,6 +481,7 @@ static void draw_data(struct gl_target *target, unsigned primitive, const void *
{
invalidate_mips(target);
make_renderable(target);
+ discard_saved_pixels(target);
glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer);
glViewport(0, 0, target->width, target->height);
glEnable(GL_SCISSOR_TEST);
@@ -535,6 +565,12 @@ void gl_target_draw_fvf(struct gl_target *target, unsigned primitive, const void
void gl_target_read(struct gl_target *target, unsigned char *rgba)
{
+ if (target->saved_pixels) {
+ unsigned char *pixels = decode_pixels(target);
+ memcpy(rgba, pixels, (size_t)target->width * target->height * 4);
+ if (pixels != target->saved_pixels) free(pixels);
+ return;
+ }
make_renderable(target);
glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer);
glReadPixels(0, 0, target->width, target->height, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
@@ -548,7 +584,16 @@ void gl_target_read(struct gl_target *target, unsigned char *rgba)
void gl_target_write(struct gl_target *target, const unsigned char *rgba)
{
invalidate_mips(target);
- make_resident(target);
+ /* Retain compressed contents for GPU eviction without readback. */
+ if (!target->color || !target->depth) {
+ size_t size = (size_t)target->width * target->height * 4;
+ unsigned char *pixels = malloc(size);
+ if (!pixels) abort();
+ memcpy(pixels, rgba, size);
+ discard_saved_pixels(target);
+ save_pixels(target, pixels);
+ }
+ if (!target->color) return;
glBindTexture(GL_TEXTURE_2D, target->color);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, target->width, target->height,
GL_RGBA, GL_UNSIGNED_BYTE, rgba);
@@ -561,6 +606,8 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in
make_resident(target);
make_renderable(source);
make_renderable(target);
+ discard_saved_pixels(target);
+ source->last_used = target->last_used = texture_frame_time;
glBindFramebuffer(GL_READ_FRAMEBUFFER, source->framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target->framebuffer);
glDisable(GL_SCISSOR_TEST);
@@ -571,6 +618,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)
{
+ target->last_used = texture_frame_time;
make_renderable(target);
glBindFramebuffer(GL_READ_FRAMEBUFFER, target->framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
@@ -598,6 +646,18 @@ void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int
#else
check_graphics("frame rendering and presentation");
#endif
+ /* Release inactive sampled textures from their retained CPU contents. */
+ if (texture_frame_time - texture_last_sweep >= 1000) {
+ for (struct gl_target *image = targets; image; image = image->next)
+ if (!image->depth && image->color && image->saved_pixels &&
+ texture_frame_time - image->last_used > 3000) gl_target_evict(image, NULL);
+ texture_last_sweep = texture_frame_time;
+ }
+#ifdef __EMSCRIPTEN__
+ texture_frame_time = emscripten_get_now();
+#else
+ texture_frame_time = SDL_GetTicks64();
+#endif
}
void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *state)
@@ -607,15 +667,25 @@ void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *stat
glActiveTexture(GL_TEXTURE0 + stage);
glBindSampler(stage, samplers[stage]);
if (state && state->image) {
+ state->image->last_used = texture_frame_time;
make_resident(state->image);
if (state->image->mip_dirty) {
/* Sublevels keep independent storage for edits and framebuffer readback. */
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);
+ if (!mip->color) {
+ /* Upload a copy without allocating another GPU texture. */
+ unsigned char *pixels = decode_pixels(mip);
+ glBindTexture(GL_TEXTURE_2D, state->image->color);
+ glTexImage2D(GL_TEXTURE_2D, ++level, GL_RGBA8, mip->width, mip->height, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+ if (pixels != mip->saved_pixels) free(pixels);
+ } else {
+ 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);
+ }
}
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, state->image->color);
diff --git a/src/tests/mip_storage.c b/src/tests/mip_storage.c
@@ -1,5 +1,10 @@
#include "gl_renderer.h"
#include <SDL2/SDL.h>
+#ifdef __EMSCRIPTEN__
+#include <GLES3/gl3.h>
+#else
+#include <SDL2/SDL_opengl.h>
+#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
@@ -171,6 +176,112 @@ int main(void)
gl_target_destroy(output);
gl_target_destroy(root);
+ root = gl_target_create(4, 4, 0);
+ child = gl_target_create(2, 2, 0);
+ leaf = gl_target_create(1, 1, 0);
+ output = gl_target_create(2, 2, 0);
+ gl_target_clear(root, 0xffe06020, 1);
+ gl_target_clear(child, 0xff30b070, 1);
+ gl_target_clear(leaf, 0xff5090d0, 1);
+ gl_target_set_mip(root, child);
+ gl_target_set_mip(child, leaf);
+ state.image = root;
+ state.mip_point = 1;
+ gl_renderer_set_texture(0, &state);
+
+ gl_target_draw(output, 4, vertices, 4, indices, 6, clip);
+
+ pixel_is(output, 2, 0xff30b070);
+ gl_target_destroy(output);
+
+ output = gl_target_create(1, 1, 0);
+ const int32_t one_pixel_clip[] = {0, 0, 1, 1};
+ gl_renderer_set_texture(0, &state);
+
+ gl_target_draw(output, 4, vertices, 4, indices, 6, one_pixel_clip);
+
+ pixel_is(output, 1, 0xff5090d0);
+ gl_target_destroy(output);
+ gl_target_destroy(root);
+ gl_target_destroy(child);
+ gl_target_destroy(leaf);
+
+ root = gl_target_create(4, 4, 0);
+ child = gl_target_create(2, 2, 0);
+ leaf = gl_target_create(1, 1, 0);
+ output = gl_target_create(2, 2, 0);
+ unsigned char red_pixels[64], green_pixels[16];
+ const unsigned char red[] = {255, 0, 0, 255}, green[] = {0, 255, 0, 255};
+ for (unsigned i = 0; i < 64; i += 4) memcpy(red_pixels + i, red, 4);
+ for (unsigned i = 0; i < 16; i += 4) memcpy(green_pixels + i, green, 4);
+ gl_target_write(root, red_pixels);
+ gl_target_write(child, green_pixels);
+ gl_target_write(leaf, magenta);
+ gl_target_set_mip(root, child);
+ gl_target_set_mip(child, leaf);
+ gl_target_clear(output, 0, 1);
+ state.image = root;
+ state.mip_point = 1;
+ gl_renderer_set_texture(0, &state);
+ GLint texture;
+ glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
+ assert(glIsTexture(texture));
+
+ gl_target_present(output, clip, 32, 32);
+ SDL_Delay(3100);
+ gl_target_present(output, clip, 32, 32);
+ gl_target_present(output, clip, 32, 32);
+
+ assert(!glIsTexture(texture));
+ gl_renderer_set_texture(0, &state);
+ gl_target_draw(output, 4, vertices, 4, indices, 6, clip);
+ pixel_is(output, 2, 0xff00ff00);
+ gl_target_destroy(output);
+ gl_target_destroy(root);
+ pixel_is(child, 2, 0xff00ff00);
+ pixel_is(leaf, 1, 0xffff00ff);
+ gl_target_destroy(child);
+ gl_target_destroy(leaf);
+
+ // Retained CPU pixels must follow clears, draws and blits on the GPU.
+ root = gl_target_create(32, 32, 0);
+ child = gl_target_create(1, 1, 0);
+ const int32_t large_clip[] = {0, 0, 32, 32};
+ const int32_t small_clip[] = {0, 0, 1, 1};
+ for (unsigned i = 0; i < sizeof(expected); i += 4)
+ memcpy(expected + i, magenta, 4);
+ gl_target_write(root, expected);
+ gl_target_write(child, green);
+ state.image = root;
+ gl_renderer_set_texture(0, &state);
+
+ gl_target_clear(root, 0xff00ff00, 1);
+ gl_target_read(root, restored);
+
+ for (unsigned i = 0; i < sizeof(restored); i += 4)
+ assert(!memcmp(restored + i, green, 4));
+
+ gl_target_write(root, expected);
+ state.image = child;
+ state.mip_point = 0;
+ gl_renderer_set_texture(0, &state);
+
+ gl_target_draw(root, 4, vertices, 4, indices, 6, large_clip);
+ gl_target_read(root, restored);
+
+ for (unsigned i = 0; i < sizeof(restored); i += 4)
+ assert(!memcmp(restored + i, green, 4));
+
+ gl_target_write(root, expected);
+
+ gl_target_blit(child, root, small_clip, large_clip);
+ gl_target_read(root, restored);
+
+ for (unsigned i = 0; i < sizeof(restored); i += 4)
+ assert(!memcmp(restored + i, green, 4));
+ gl_target_destroy(root);
+ gl_target_destroy(child);
+
gl_renderer_shutdown();
#ifdef __EMSCRIPTEN__
emscripten_webgl_destroy_context(context);
@@ -179,5 +290,5 @@ int main(void)
SDL_DestroyWindow(window);
#endif
SDL_Quit();
- puts("mip storage: child edits, independent binding, blits, eviction and retained descendants passed");
+ puts("mip storage: independent binding, GPU edits, timed eviction and retained descendants passed");
}