render: fix filtering; optimize VBO management

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I2f30f77e0f29437cac57a1064ca1f6796a6a6964
This commit is contained in:
raf 2025-11-02 11:37:20 +03:00
commit ec628eb1af
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -277,7 +277,8 @@ static int init_gl_resources(chroma_output_t *output) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW); 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; output->gl_resources_initialized = true;
chroma_log("DEBUG", "Initialized GL resources for output %u", output->id); 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); glBindTexture(GL_TEXTURE_2D, output->texture_id);
glUniform1i(glGetUniformLocation(output->shader_program, "texture"), 0); glUniform1i(glGetUniformLocation(output->shader_program, "texture"), 0);
// Calculate texture coordinates based on scaling mode // Update VBO only if needed. E.g, image changed, scale mode changed, or first
float tex_coords[8]; // render
calculate_texture_coords(output->scale_mode, output->image->width, if (output->vbo_dirty) {
output->image->height, output->width, output->height, // Calculate texture coordinates based on scaling mode
tex_coords); 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 // Create dynamic vertex data with calculated texture coordinates
float dynamic_vertices[] = { float dynamic_vertices[] = {
// Position Texcoord // Position Texcoord
-1.0f, -1.0f, tex_coords[0], tex_coords[1], // bottom-left -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[2], tex_coords[3], // bottom-right
1.0f, 1.0f, tex_coords[4], tex_coords[5], // top-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 -1.0f, 1.0f, tex_coords[6], tex_coords[7] // top-left
}; };
// Update VBO with dynamic texture coordinates // Update VBO with dynamic texture coordinates
glBindBuffer(GL_ARRAY_BUFFER, output->vbo); glBindBuffer(GL_ARRAY_BUFFER, output->vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(dynamic_vertices), glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(dynamic_vertices),
dynamic_vertices); dynamic_vertices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, output->ebo); 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 // Set vertex attributes
GLint position_attr = glGetAttribLocation(output->shader_program, "position"); 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 output->texture_uploaded = false; // reset upload flag
chroma_log("DEBUG", "Invalidated texture cache for output %u", output->id); 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;
} }