Call gpu_cleanup early when memory is full

This commit is contained in:
luboslenco
2026-01-11 20:49:00 +01:00
parent 0813604623
commit 8cb28a1777
3 changed files with 25 additions and 6 deletions
+19 -6
View File
@@ -1171,6 +1171,14 @@ void gpu_execute_and_wait() {
}
}
static void gpu_cleanup_internal() {
while (buffers_to_destroy_count > 0) {
buffers_to_destroy_count--;
vkFreeMemory(device, buffer_memories_to_destroy[buffers_to_destroy_count], NULL);
vkDestroyBuffer(device, buffers_to_destroy[buffers_to_destroy_count], NULL);
}
}
void gpu_present_internal() {
vkEndCommandBuffer(command_buffer);
vkResetFences(device, 1, &fence);
@@ -1208,11 +1216,7 @@ void gpu_present_internal() {
framebuffer_acquired = false;
framebuffer_index = (framebuffer_index + 1) % GPU_FRAMEBUFFER_COUNT;
while (buffers_to_destroy_count > 0) {
buffers_to_destroy_count--;
vkFreeMemory(device, buffer_memories_to_destroy[buffers_to_destroy_count], NULL);
vkDestroyBuffer(device, buffers_to_destroy[buffers_to_destroy_count], NULL);
}
gpu_cleanup_internal();
}
void gpu_draw_internal() {
@@ -1692,7 +1696,16 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
.allocationSize = mem_reqs.size,
};
mem_alloc.memoryTypeIndex = memory_type_from_properties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
vkAllocateMemory(device, &mem_alloc, NULL, &texture->impl.mem);
VkResult result = vkAllocateMemory(device, &mem_alloc, NULL, &texture->impl.mem);
if (result != VK_SUCCESS && gpu_cleanup_pending()) {
gpu_execute_and_wait();
gpu_cleanup_internal();
gpu_cleanup();
gpu_texture_init_from_bytes(texture, data, width, height, format);
return;
}
vkBindImageMemory(device, texture->impl.image, texture->impl.mem, 0);
if (gpu_in_use) {
+4
View File
@@ -115,6 +115,10 @@ void gpu_cleanup() {
}
}
bool gpu_cleanup_pending() {
return textures_to_destroy_count > 0 || buffers_to_destroy_count > 0 || pipelines_to_destroy_count > 0;
}
void gpu_present() {
gpu_present_internal();
draw_calls_last = draw_calls;
+2
View File
@@ -154,6 +154,8 @@ void gpu_begin_internal(gpu_clear_t flags, unsigned color, float depth);
void gpu_end(void);
void gpu_end_internal(void);
void gpu_execute_and_wait(void);
void gpu_cleanup(void);
bool gpu_cleanup_pending(void);
void gpu_present(void);
void gpu_present_internal(void);
void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after);