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
+40 -51
View File
@@ -17,19 +17,16 @@ typedef struct R_Texture_Impl {
Uint32 width;
Uint32 height;
Uint32 sample_count;
bool multisampled;
R_Texture_Format format;
} R_Texture_Impl;
R_Texture R_framebuffer;
R_Texture R_surface;
static SDL_Window *window;
static SDL_GPUDevice *device;
static R_Texture_Format surface_format;
static SDL_GPUTexture *surface_texture;
static R_Texture framebuffer;
static R_Texture_Impl surface;
static SDL_GPUTransferBuffer *transfer_buffer;
@@ -90,7 +87,7 @@ static SDL_GPUStoreOp store_ops[] = {
};
static_assert(SDL_arraysize(store_ops) == R_STORE_OP_COUNT);
R_Texture renderer_texture_create(R_Texture_Format format, R_Texture_Usage usage, Uint32 sample_count, Uint32 width, Uint32 height, void *data, const char *debug_name) {
R_Texture renderer_texture_create(R_Texture_Format format, R_Texture_Usage usage, bool multisampled, Uint32 width, Uint32 height, void *data, const char *debug_name) {
SDL_PropertiesID properties = SDL_CreateProperties();
SDL_SetStringProperty(properties, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, debug_name);
@@ -102,7 +99,7 @@ R_Texture renderer_texture_create(R_Texture_Format format, R_Texture_Usage usage
.height = height,
.layer_count_or_depth = 1,
.num_levels = 1,
.sample_count = sample_count == 1 ? SDL_GPU_SAMPLECOUNT_1 : SDL_GPU_SAMPLECOUNT_8,
.sample_count = multisampled ? SDL_GPU_SAMPLECOUNT_8 : SDL_GPU_SAMPLECOUNT_1,
.props = properties,
});
SDL_DestroyProperties(properties);
@@ -120,8 +117,8 @@ R_Texture renderer_texture_create(R_Texture_Format format, R_Texture_Usage usage
.width = width,
.height = height,
.sample_count = sample_count,
.format = format,
.multisampled = multisampled,
.format = format,
};
if (data) {
@@ -165,6 +162,22 @@ void renderer_texture_destroy(R_Texture texture) {
SDL_free(texture);
}
Uint32 renderer_texture_get_width(R_Texture texture) {
return texture->width;
}
Uint32 renderer_texture_get_height(R_Texture texture) {
return texture->height;
}
bool renderer_texture_get_multisampled(R_Texture texture) {
return texture->multisampled;
}
R_Texture_Format renderer_texture_get_format(R_Texture texture) {
return texture->format;
}
R_Buffer renderer_buffer_create(R_Buffer_Usage usage, Uint32 num_bytes, void *data, const char *debug_name) {
R_Buffer_Impl *result = SDL_calloc(1, sizeof(R_Buffer_Impl));
if (!result) return NULL;
@@ -246,22 +259,21 @@ void renderer_buffer_destroy(R_Buffer buffer) {
SDL_free(buffer);
}
Uint32 renderer_buffer_get_size(R_Buffer buffer) {
return buffer->num_bytes;
}
R_Buffer_Usage renderer_buffer_get_usage(R_Buffer buffer){
return buffer->usage;
}
void renderer_frame_begin() {
SDL_assert(R_framebuffer == NULL);
SDL_assert(R_surface == NULL);
Uint32 surface_width = 0, surface_height = 0;
SDL_WaitAndAcquireGPUSwapchainTexture(render_command_buffer, window, &surface_texture, &surface_width, &surface_height);
SDL_WaitAndAcquireGPUSwapchainTexture(render_command_buffer, window, &surface.texture, &surface.width, &surface.height);
if (!framebuffer || framebuffer->width != surface_width || framebuffer->height != surface_height) {
if (framebuffer) {
renderer_texture_destroy(framebuffer);
framebuffer = NULL;
}
framebuffer = renderer_texture_create(surface_format, R_TEXTURE_USAGE_TARGET, 8, surface_width, surface_height, NULL, "R_framebuffer");
}
R_framebuffer = framebuffer;
R_surface = &surface;
}
void renderer_frame_end() {
@@ -273,36 +285,13 @@ void renderer_frame_end() {
render_pass = NULL;
}
render_pass = SDL_BeginGPURenderPass(render_command_buffer,
&(SDL_GPUColorTargetInfo){
.texture = framebuffer->texture,
.mip_level = 0,
.layer_or_depth_plane = 0,
.clear_color = { 0.0f, 0.0f, 0.0f, 0.0f },
.load_op = SDL_GPU_LOADOP_LOAD,
.store_op = SDL_GPU_STOREOP_RESOLVE,
.resolve_texture = surface_texture,
.resolve_mip_level = 0,
.resolve_layer = 0,
.cycle = false,
.cycle_resolve_texture = false,
},
1,
NULL
);
SDL_EndGPURenderPass(render_pass);
render_pass = NULL;
SDL_GPUFence *frame_fence = SDL_SubmitGPUCommandBufferAndAcquireFence(render_command_buffer);
SDL_WaitForGPUFences(device, true, &frame_fence, 1);
copy_command_buffer = SDL_AcquireGPUCommandBuffer(device);
render_command_buffer = SDL_AcquireGPUCommandBuffer(device);
R_framebuffer = NULL;
R_surface = NULL;
copy_pass = SDL_BeginGPUCopyPass(copy_command_buffer);
}
@@ -397,7 +386,7 @@ bool renderer_init(SDL_Window *window_) {
SDL_SetGPUSwapchainParameters(device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR, SDL_GPU_PRESENTMODE_VSYNC);
surface_format = R_Texture_Format_from_SDL_GPUTextureFormat(SDL_GetGPUSwapchainTextureFormat(device, window));
surface.format = R_Texture_Format_from_SDL_GPUTextureFormat(SDL_GetGPUSwapchainTextureFormat(device, window));
bilinear_sampler = SDL_CreateGPUSampler(device, &(SDL_GPUSamplerCreateInfo){
.min_filter = SDL_GPU_FILTER_LINEAR,
@@ -542,7 +531,7 @@ bool renderer_init(SDL_Window *window_) {
.target_info = {
.color_target_descriptions = (SDL_GPUColorTargetDescription[]){
{
.format = texture_formats[surface_format],
.format = texture_formats[surface.format],
.blend_state = {
.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA,
.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
@@ -674,7 +663,7 @@ bool renderer_init(SDL_Window *window_) {
.target_info = {
.color_target_descriptions = (SDL_GPUColorTargetDescription[]){
{
.format = texture_formats[surface_format],
.format = texture_formats[surface.format],
.blend_state = {
.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA,
.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
@@ -806,7 +795,7 @@ bool renderer_init(SDL_Window *window_) {
.target_info = {
.color_target_descriptions = (SDL_GPUColorTargetDescription[]){
{
.format = texture_formats[surface_format],
.format = texture_formats[surface.format],
.blend_state = {
.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA,
.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
@@ -851,7 +840,7 @@ bool renderer_init(SDL_Window *window_) {
void ImGui_ImplRenderer_Init() {
cImGui_ImplSDLGPU3_Init(&(ImGui_ImplSDLGPU3_InitInfo){
.Device = device,
.ColorTargetFormat = texture_formats[surface_format],
.ColorTargetFormat = texture_formats[surface.format],
.MSAASamples = SDL_GPU_SAMPLECOUNT_8,
.SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
.PresentMode = SDL_GPU_PRESENTMODE_VSYNC,