From ec628eb1afab3f6a27ea32913c917d7abc72ea6f Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 2 Nov 2025 11:37:20 +0300 Subject: [PATCH] render: fix filtering; optimize VBO management Signed-off-by: NotAShelf Change-Id: I2f30f77e0f29437cac57a1064ca1f6796a6a6964 --- src/render.c | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/render.c b/src/render.c index 4a77185..f4c4a3f 100644 --- a/src/render.c +++ b/src/render.c @@ -277,7 +277,8 @@ static int init_gl_resources(chroma_output_t *output) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - output->texture_id = 0; // will be created when image is assigned + output->texture_id = 0; // will be created when image is assigned + output->vbo_dirty = true; // VBO needs initial update output->gl_resources_initialized = true; chroma_log("DEBUG", "Initialized GL resources for output %u", output->id); @@ -684,26 +685,36 @@ int chroma_render_wallpaper(chroma_state_t *state, chroma_output_t *output) { glBindTexture(GL_TEXTURE_2D, output->texture_id); glUniform1i(glGetUniformLocation(output->shader_program, "texture"), 0); - // Calculate texture coordinates based on scaling mode - float tex_coords[8]; - calculate_texture_coords(output->scale_mode, output->image->width, - output->image->height, output->width, output->height, - tex_coords); + // Update VBO only if needed. E.g, image changed, scale mode changed, or first + // render + if (output->vbo_dirty) { + // Calculate texture coordinates based on scaling mode + float tex_coords[8]; + calculate_texture_coords(output->scale_mode, output->image->width, + output->image->height, output->width, + output->height, tex_coords); - // Create dynamic vertex data with calculated texture coordinates - float dynamic_vertices[] = { - // Position Texcoord - -1.0f, -1.0f, tex_coords[0], tex_coords[1], // bottom-left - 1.0f, -1.0f, tex_coords[2], tex_coords[3], // bottom-right - 1.0f, 1.0f, tex_coords[4], tex_coords[5], // top-right - -1.0f, 1.0f, tex_coords[6], tex_coords[7] // top-left - }; + // Create dynamic vertex data with calculated texture coordinates + float dynamic_vertices[] = { + // Position Texcoord + -1.0f, -1.0f, tex_coords[0], tex_coords[1], // bottom-left + 1.0f, -1.0f, tex_coords[2], tex_coords[3], // bottom-right + 1.0f, 1.0f, tex_coords[4], tex_coords[5], // top-right + -1.0f, 1.0f, tex_coords[6], tex_coords[7] // top-left + }; - // Update VBO with dynamic texture coordinates - glBindBuffer(GL_ARRAY_BUFFER, output->vbo); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(dynamic_vertices), - dynamic_vertices); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, output->ebo); + // Update VBO with dynamic texture coordinates + glBindBuffer(GL_ARRAY_BUFFER, output->vbo); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(dynamic_vertices), + dynamic_vertices); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, output->ebo); + + output->vbo_dirty = false; // mark VBO as up to date + } else { + // Just bind the existing buffers + glBindBuffer(GL_ARRAY_BUFFER, output->vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, output->ebo); + } // Set vertex attributes GLint position_attr = glGetAttribLocation(output->shader_program, "position"); @@ -753,4 +764,7 @@ void chroma_output_invalidate_texture(chroma_output_t *output) { output->texture_uploaded = false; // reset upload flag chroma_log("DEBUG", "Invalidated texture cache for output %u", output->id); } + + // Mark VBO as dirty since texture coordinates may need recalculation + output->vbo_dirty = true; }