render: add OpenGL resource caching; optimize texture handling
Mildly improves rendering performance by caching OpenGL resources. Namely: - Cache shader program, VBO/EBO, and textures per output - Automatically free image data after GPU upload - Force RGBA format for consistent texture handling - Track texture state across output changes - Add texture invalidation on image changes This reduces the memory usage by a solid 30MB, but it's still not quite enough. I expect (or rather, hope) that we can cut it by half. Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6a6a6964eebc783c5bc07b1fef7548a8d49f529c
This commit is contained in:
parent
3cbf6d5645
commit
d1116e7721
4 changed files with 221 additions and 114 deletions
49
src/image.c
49
src/image.c
|
|
@ -49,11 +49,12 @@ int chroma_image_load(chroma_image_t *image, const char *path) {
|
|||
(double)file_size / (1024.0 * 1024.0));
|
||||
}
|
||||
|
||||
// Load image data using stb_image
|
||||
stbi_set_flip_vertically_on_load(0); // Keep images right-side up
|
||||
// Load image data using stb_image, force RGBA format to avoid conversion
|
||||
stbi_set_flip_vertically_on_load(0); // keep images right-side up
|
||||
|
||||
image->data =
|
||||
stbi_load(path, &image->width, &image->height, &image->channels, 0);
|
||||
stbi_load(path, &image->width, &image->height, &image->channels, 4);
|
||||
image->channels = 4; // always RGBA after forced conversion
|
||||
if (!image->data) {
|
||||
chroma_log("ERROR", "Failed to load image %s: %s", path,
|
||||
stbi_failure_reason());
|
||||
|
|
@ -68,39 +69,14 @@ int chroma_image_load(chroma_image_t *image, const char *path) {
|
|||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Check supported formats
|
||||
if (image->channels < 3 || image->channels > 4) {
|
||||
chroma_log("ERROR",
|
||||
"Unsupported image format: %d channels (need RGB or RGBA)",
|
||||
// Validate we have RGBA data (should always be true with forced conversion)
|
||||
if (image->channels != 4) {
|
||||
chroma_log("ERROR", "Failed to load image as RGBA: got %d channels",
|
||||
image->channels);
|
||||
chroma_image_free(image);
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Convert RGB to RGBA if necessary for consistent handling
|
||||
if (image->channels == 3) {
|
||||
int pixel_count = image->width * image->height;
|
||||
unsigned char *rgba_data = malloc(pixel_count * 4);
|
||||
if (!rgba_data) {
|
||||
chroma_log("ERROR", "Failed to allocate memory for RGBA conversion");
|
||||
chroma_image_free(image);
|
||||
return CHROMA_ERROR_MEMORY;
|
||||
}
|
||||
|
||||
// Convert RGB to RGBA
|
||||
for (int i = 0; i < pixel_count; i++) {
|
||||
rgba_data[i * 4 + 0] = image->data[i * 3 + 0]; // R
|
||||
rgba_data[i * 4 + 1] = image->data[i * 3 + 1]; // G
|
||||
rgba_data[i * 4 + 2] = image->data[i * 3 + 2]; // B
|
||||
rgba_data[i * 4 + 3] = 255; // A
|
||||
}
|
||||
|
||||
// Replace original data
|
||||
stbi_image_free(image->data);
|
||||
image->data = rgba_data;
|
||||
image->channels = 4;
|
||||
}
|
||||
|
||||
image->loaded = true;
|
||||
|
||||
chroma_log("INFO", "Loaded image: %s (%dx%d, %d channels, %.2f MB)", path,
|
||||
|
|
@ -118,13 +94,8 @@ void chroma_image_free(chroma_image_t *image) {
|
|||
}
|
||||
|
||||
if (image->data) {
|
||||
if (image->channels == 4 && strlen(image->path) > 0) {
|
||||
// If we converted from RGB to RGBA, use regular free()
|
||||
free(image->data);
|
||||
} else {
|
||||
// If loaded directly by stb_image, use stbi_image_free()
|
||||
stbi_image_free(image->data);
|
||||
}
|
||||
// Always use stbi_image_free since we load directly with stbi_load
|
||||
stbi_image_free(image->data);
|
||||
image->data = NULL;
|
||||
}
|
||||
|
||||
|
|
@ -265,4 +236,4 @@ void chroma_image_init_stb(void) {
|
|||
stbi_ldr_to_hdr_scale(1.0f);
|
||||
|
||||
chroma_log("DEBUG", "Initialized stb_image library");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue