move framebuffer management from the renderer to the app

This commit is contained in:
Sven Balzer
2026-07-03 14:42:20 +02:00
parent 18c1b3639c
commit f3dcd26ce7
4 changed files with 115 additions and 104 deletions
+17 -7
View File
@@ -33,6 +33,7 @@ using namespace glm;
#define TILE_ATLAS_SIZE (512)
static SDL_Window *window;
static R_Texture framebuffer;
static R_Texture player_texture;
static R_Texture tile_textures_atlas;
@@ -458,7 +459,7 @@ static R_Texture create_texture(const char *path) {
return NULL;
}
R_Texture result = renderer_texture_create(channels == 4 ? R_TEXTURE_FORMAT_RGBA8_UNORM_SRGB : R_TEXTURE_FORMAT_R8_UNORM, R_TEXTURE_USAGE_SAMPLED, 1, width, height, data, path);
R_Texture result = renderer_texture_create(channels == 4 ? R_TEXTURE_FORMAT_RGBA8_UNORM_SRGB : R_TEXTURE_FORMAT_R8_UNORM, R_TEXTURE_USAGE_SAMPLED, false, width, height, data, path);
if (!result) {
log_error("Failed to load texture (\"%s\").", path_to_load);
stbi_image_free(data);
@@ -577,7 +578,7 @@ static bool load_map(const char *name, Map *result) {
char buffer_name[256] = "Map ";
SDL_strlcat(buffer_name, result->name, SDL_arraysize(buffer_name));
result->texture = renderer_texture_create(R_TEXTURE_FORMAT_R16_UINT, R_TEXTURE_USAGE_STORAGE, 1, result->size.x, result->size.y, result->tiles, "map_texture");
result->texture = renderer_texture_create(R_TEXTURE_FORMAT_R16_UINT, R_TEXTURE_USAGE_STORAGE, false, result->size.x, result->size.y, result->tiles, "map_texture");
SDL_Log("Loaded map file.");
return true;
@@ -658,7 +659,7 @@ static void change_map_size(Map *map, char direction, int amount) {
player.position = clamp(player.position, i32vec2(0, 0), map->size - 2);
map->texture = renderer_texture_create(R_TEXTURE_FORMAT_R16_UINT, R_TEXTURE_USAGE_SAMPLED, 1, map->size.x, map->size.y, map->tiles, "map_texture");
map->texture = renderer_texture_create(R_TEXTURE_FORMAT_R16_UINT, R_TEXTURE_USAGE_SAMPLED, false, map->size.x, map->size.y, map->tiles, "map_texture");
}
static void blit(char *dst, Sint32 dst_pitch, Sint32 dst_x, Sint32 dst_y, char *src, Sint32 src_pitch, Sint32 width, Sint32 height, int components = 4) {
@@ -755,13 +756,13 @@ static void setup_memory_functions() {}
#endif
static bool recreate_tile_textures() {
tile_textures_atlas = renderer_texture_create(R_TEXTURE_FORMAT_RGBA8_UNORM_SRGB, R_TEXTURE_USAGE_SAMPLED, 1, TILE_ATLAS_SIZE, TILE_ATLAS_SIZE, NULL, "tile_atlas_texture");
tile_textures_atlas = renderer_texture_create(R_TEXTURE_FORMAT_RGBA8_UNORM_SRGB, R_TEXTURE_USAGE_SAMPLED, false, TILE_ATLAS_SIZE, TILE_ATLAS_SIZE, NULL, "tile_atlas_texture");
if (!tile_textures_atlas) {
log_error("Failed to create texture.");
return false;
}
tile_textures_atlas_imgui = renderer_texture_create(R_TEXTURE_FORMAT_RGBA8_UNORM, R_TEXTURE_USAGE_SAMPLED, 1, TILE_ATLAS_SIZE, TILE_ATLAS_SIZE, NULL, "tile_atlas_texture imgui");
tile_textures_atlas_imgui = renderer_texture_create(R_TEXTURE_FORMAT_RGBA8_UNORM, R_TEXTURE_USAGE_SAMPLED, false, TILE_ATLAS_SIZE, TILE_ATLAS_SIZE, NULL, "tile_atlas_texture imgui");
if (!tile_textures_atlas) {
log_error("Failed to create texture.");
return false;
@@ -1368,7 +1369,7 @@ static void render_editor() {
}
}
renderer_frame_set_target(R_framebuffer, NULL, R_LOAD_OP_CLEAR, R_STORE_OP_STORE, { 0.01f, 0.01f, 0.01f, 0.01f });
renderer_frame_set_target(framebuffer, R_surface, R_LOAD_OP_CLEAR, R_STORE_OP_STORE, { 0.01f, 0.01f, 0.01f, 0.01f });
{
ZoneScopedN("Draw Map");
@@ -1524,7 +1525,7 @@ static void render_game() {
}
}
renderer_frame_set_target(R_framebuffer, NULL, R_LOAD_OP_CLEAR, R_STORE_OP_STORE, { 0.01f, 0.01f, 0.01f, 0.01f });
renderer_frame_set_target(framebuffer, R_surface, R_LOAD_OP_CLEAR, R_STORE_OP_STORE, { 0.01f, 0.01f, 0.01f, 0.01f });
{
ZoneScopedN("Draw Map");
@@ -1614,6 +1615,15 @@ static void render() {
renderer_frame_begin();
if (!framebuffer || renderer_texture_get_width(framebuffer) != renderer_texture_get_width(R_surface) || renderer_texture_get_height(framebuffer) != renderer_texture_get_height(R_surface)) {
if (framebuffer) {
renderer_texture_destroy(framebuffer);
framebuffer = NULL;
}
framebuffer = renderer_texture_create(renderer_texture_get_format(R_surface), R_TEXTURE_USAGE_TARGET, true, renderer_texture_get_width(R_surface), renderer_texture_get_height(R_surface), NULL, "framebuffer");
}
if (in_editor) {
render_editor();
} else {