commit c814feda7979442587dd4fa148f1594482f0eb5a
parent 0267c8b401b977124e60d8edd323d60a9168d47f
Author: anon <anon@example.com>
Date: Mon, 19 Oct 2026 23:48:23 +0000
Restore independent mip storage for cross-browser rendering
Diffstat:
2 files changed, 37 insertions(+), 68 deletions(-)
diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c
@@ -28,7 +28,6 @@ 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];
@@ -44,7 +43,7 @@ struct gl_depth {
GLuint buffer;
};
struct gl_target {
- unsigned width, height, level; /* Level zero owns color; sublevels borrow storage. */
+ unsigned width, height;
GLuint framebuffer, color;
struct gl_depth *depth;
unsigned char *saved_pixels;
@@ -267,7 +266,6 @@ 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)
@@ -308,7 +306,6 @@ 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);
@@ -338,12 +335,18 @@ static void make_resident(struct gl_target *target)
static void make_renderable(struct gl_target *target)
{
- make_resident(target);
+ /* Restoring a draw destination must preserve the currently sampled image. */
+ if (!target->color) {
+ GLint texture;
+ glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
+ make_resident(target);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ }
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, target->level);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->color, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
target->depth ? target->depth->buffer : 0);
/* RGBA8 attachments have a fixed layout. Browser completeness queries would
@@ -358,49 +361,6 @@ 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));
@@ -415,19 +375,14 @@ struct gl_target *gl_target_create(unsigned width, unsigned height, int with_dep
void gl_target_destroy(struct gl_target *target)
{
- 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->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);
- if (!target->level) glDeleteTextures(1, &target->color);
+ glDeleteTextures(1, &target->color);
free(target->saved_pixels);
free(target);
}
@@ -437,15 +392,13 @@ 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);
- if (!target->level) glDeleteTextures(1, &target->color);
+ glDeleteTextures(1, &target->color);
target->framebuffer = target->color = 0;
- target->level = 0;
/* Small mip tails stay raw; compress larger inactive images losslessly. */
uLong size = (size_t)target->width * target->height * 4;
if (size > 64) {
@@ -498,7 +451,6 @@ 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);
@@ -598,7 +550,7 @@ 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, target->level, 0, 0, target->width, target->height,
+ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, target->width, target->height,
GL_RGBA, GL_UNSIGNED_BYTE, rgba);
}
@@ -607,10 +559,6 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in
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);
@@ -655,15 +603,20 @@ 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 = state->image->mip ? share_mips(state->image, state->image->mip, 1) : 0;
+ /* 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);
+ }
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/tests/mip_storage.c b/src/tests/mip_storage.c
@@ -155,6 +155,22 @@ int main(void)
assert(!memcmp(tiny, restored, sizeof(tiny)));
gl_target_destroy(inactive);
+ root = gl_target_create(1, 1, 0);
+ output = gl_target_create(2, 2, 0);
+ gl_target_write(root, magenta);
+ gl_target_clear(output, 0, 1);
+ gl_target_evict(output, NULL);
+ state.image = root;
+ state.color_op = state.alpha_op = 2;
+ gl_renderer_set_texture(0, &state);
+ gl_renderer_set_texture(1, NULL);
+
+ gl_target_draw(output, 4, vertices, 4, indices, 6, clip);
+
+ pixel_is(output, 2, 0xffff00ff);
+ gl_target_destroy(output);
+ gl_target_destroy(root);
+
gl_renderer_shutdown();
#ifdef __EMSCRIPTEN__
emscripten_webgl_destroy_context(context);