Skip to content

Commit 0b3ed66

Browse files
committed
Pass old swapchain handle during recreation for driver memory reuse
1 parent 9267c66 commit 0b3ed66

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/client/renderer/RenderSystem.zig

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ pub const RenderSystem = struct {
395395
try self.createSurface();
396396
try self.pickPhysicalDevice();
397397
try self.createLogicalDevice();
398-
try self.createSwapchain();
398+
try self.createSwapchain(null);
399399
try self.createImageViews();
400400
try self.createDepthResources();
401401
try self.createRenderPass();
@@ -1806,14 +1806,22 @@ pub const RenderSystem = struct {
18061806

18071807
_ = vkDeviceWaitIdle(self.device);
18081808

1809-
// Cleanup old swapchain resources
1809+
// Cleanup old swapchain resources (keep old swapchain handle for driver reuse)
18101810
self.destroyFramebuffers();
18111811
self.destroyDepthResources();
18121812
self.destroyImageViews();
1813-
self.destroySwapchain();
1813+
const old_swapchain = self.swapchain;
1814+
if (self.swapchain_images.len > 0) {
1815+
self.allocator.free(self.swapchain_images);
1816+
self.swapchain_images = &.{};
1817+
}
18141818

1815-
// Recreate
1816-
try self.createSwapchain();
1819+
// Recreate (passing old handle so driver can reuse memory)
1820+
try self.createSwapchain(old_swapchain);
1821+
// Old swapchain is retired now that the new one is created
1822+
if (old_swapchain) |old| {
1823+
if (vk.vkDestroySwapchainKHR) |destroy| destroy(self.device, old, null);
1824+
}
18171825
try self.createImageViews();
18181826
try self.createDepthResources();
18191827
try self.createFramebuffers();
@@ -2043,7 +2051,7 @@ pub const RenderSystem = struct {
20432051
logger.info("Logical device created", .{});
20442052
}
20452053

2046-
fn createSwapchain(self: *Self) !void {
2054+
fn createSwapchain(self: *Self, old_swapchain: vk.VkSwapchainKHR) !void {
20472055
const vkGetPhysicalDeviceSurfaceCapabilitiesKHR = vk.vkGetPhysicalDeviceSurfaceCapabilitiesKHR orelse return error.VulkanFunctionNotLoaded;
20482056
const vkGetPhysicalDeviceSurfaceFormatsKHR = vk.vkGetPhysicalDeviceSurfaceFormatsKHR orelse return error.VulkanFunctionNotLoaded;
20492057
const vkGetPhysicalDeviceSurfacePresentModesKHR = vk.vkGetPhysicalDeviceSurfacePresentModesKHR orelse return error.VulkanFunctionNotLoaded;
@@ -2119,7 +2127,7 @@ pub const RenderSystem = struct {
21192127
.compositeAlpha = vk.VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
21202128
.presentMode = chosen_mode,
21212129
.clipped = vk.VK_TRUE,
2122-
.oldSwapchain = null,
2130+
.oldSwapchain = old_swapchain,
21232131
};
21242132

21252133
const queue_family_indices = [_]u32{ self.graphics_family, self.present_family };

0 commit comments

Comments
 (0)