-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathswapchain.zig
312 lines (262 loc) · 11.8 KB
/
swapchain.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const std = @import("std");
const vk = @import("vulkan");
const GraphicsContext = @import("graphics_context.zig").GraphicsContext;
const Allocator = std.mem.Allocator;
pub const Swapchain = struct {
pub const PresentState = enum {
optimal,
suboptimal,
};
gc: *const GraphicsContext,
allocator: Allocator,
surface_format: vk.SurfaceFormatKHR,
present_mode: vk.PresentModeKHR,
extent: vk.Extent2D,
handle: vk.SwapchainKHR,
swap_images: []SwapImage,
image_index: u32,
next_image_acquired: vk.Semaphore,
pub fn init(gc: *const GraphicsContext, allocator: Allocator, extent: vk.Extent2D) !Swapchain {
return try initRecycle(gc, allocator, extent, .null_handle);
}
pub fn initRecycle(gc: *const GraphicsContext, allocator: Allocator, extent: vk.Extent2D, old_handle: vk.SwapchainKHR) !Swapchain {
const caps = try gc.instance.getPhysicalDeviceSurfaceCapabilitiesKHR(gc.pdev, gc.surface);
const actual_extent = findActualExtent(caps, extent);
if (actual_extent.width == 0 or actual_extent.height == 0) {
return error.InvalidSurfaceDimensions;
}
const surface_format = try findSurfaceFormat(gc, allocator);
const present_mode = try findPresentMode(gc, allocator);
var image_count = caps.min_image_count + 1;
if (caps.max_image_count > 0) {
image_count = @min(image_count, caps.max_image_count);
}
const qfi = [_]u32{ gc.graphics_queue.family, gc.present_queue.family };
const sharing_mode: vk.SharingMode = if (gc.graphics_queue.family != gc.present_queue.family)
.concurrent
else
.exclusive;
const handle = try gc.dev.createSwapchainKHR(&.{
.surface = gc.surface,
.min_image_count = image_count,
.image_format = surface_format.format,
.image_color_space = surface_format.color_space,
.image_extent = actual_extent,
.image_array_layers = 1,
.image_usage = .{ .color_attachment_bit = true, .transfer_dst_bit = true },
.image_sharing_mode = sharing_mode,
.queue_family_index_count = qfi.len,
.p_queue_family_indices = &qfi,
.pre_transform = caps.current_transform,
.composite_alpha = .{ .opaque_bit_khr = true },
.present_mode = present_mode,
.clipped = vk.TRUE,
.old_swapchain = old_handle,
}, null);
errdefer gc.dev.destroySwapchainKHR(handle, null);
if (old_handle != .null_handle) {
// Apparently, the old swapchain handle still needs to be destroyed after recreating.
gc.dev.destroySwapchainKHR(old_handle, null);
}
const swap_images = try initSwapchainImages(gc, handle, surface_format.format, allocator);
errdefer {
for (swap_images) |si| si.deinit(gc);
allocator.free(swap_images);
}
var next_image_acquired = try gc.dev.createSemaphore(&.{}, null);
errdefer gc.dev.destroySemaphore(next_image_acquired, null);
const result = try gc.dev.acquireNextImageKHR(handle, std.math.maxInt(u64), next_image_acquired, .null_handle);
if (result.result != .success) {
return error.ImageAcquireFailed;
}
std.mem.swap(vk.Semaphore, &swap_images[result.image_index].image_acquired, &next_image_acquired);
return Swapchain{
.gc = gc,
.allocator = allocator,
.surface_format = surface_format,
.present_mode = present_mode,
.extent = actual_extent,
.handle = handle,
.swap_images = swap_images,
.image_index = result.image_index,
.next_image_acquired = next_image_acquired,
};
}
fn deinitExceptSwapchain(self: Swapchain) void {
for (self.swap_images) |si| si.deinit(self.gc);
self.allocator.free(self.swap_images);
self.gc.dev.destroySemaphore(self.next_image_acquired, null);
}
pub fn waitForAllFences(self: Swapchain) !void {
for (self.swap_images) |si| si.waitForFence(self.gc) catch {};
}
pub fn deinit(self: Swapchain) void {
self.deinitExceptSwapchain();
self.gc.dev.destroySwapchainKHR(self.handle, null);
}
pub fn recreate(self: *Swapchain, new_extent: vk.Extent2D) !void {
const gc = self.gc;
const allocator = self.allocator;
const old_handle = self.handle;
self.deinitExceptSwapchain();
self.* = try initRecycle(gc, allocator, new_extent, old_handle);
}
pub fn currentImage(self: Swapchain) vk.Image {
return self.swap_images[self.image_index].image;
}
pub fn currentSwapImage(self: Swapchain) *const SwapImage {
return &self.swap_images[self.image_index];
}
pub fn present(self: *Swapchain, cmdbuf: vk.CommandBuffer) !PresentState {
// Simple method:
// 1) Acquire next image
// 2) Wait for and reset fence of the acquired image
// 3) Submit command buffer with fence of acquired image,
// dependendent on the semaphore signalled by the first step.
// 4) Present current frame, dependent on semaphore signalled by previous step
// Problem: This way we can't reference the current image while rendering.
// Better method: Shuffle the steps around such that acquire next image is the last step,
// leaving the swapchain in a state with the current image.
// 1) Wait for and reset fence of current image
// 2) Submit command buffer, signalling fence of current image and dependent on
// the semaphore signalled by step 4.
// 3) Present current frame, dependent on semaphore signalled by the submit
// 4) Acquire next image, signalling its semaphore
// One problem that arises is that we can't know beforehand which semaphore to signal,
// so we keep an extra auxilery semaphore that is swapped around
// Step 1: Make sure the current frame has finished rendering
const current = self.currentSwapImage();
try current.waitForFence(self.gc);
try self.gc.dev.resetFences(1, @ptrCast(¤t.frame_fence));
// Step 2: Submit the command buffer
const wait_stage = [_]vk.PipelineStageFlags{.{ .top_of_pipe_bit = true }};
try self.gc.dev.queueSubmit(self.gc.graphics_queue.handle, 1, &[_]vk.SubmitInfo{.{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(¤t.image_acquired),
.p_wait_dst_stage_mask = &wait_stage,
.command_buffer_count = 1,
.p_command_buffers = @ptrCast(&cmdbuf),
.signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast(¤t.render_finished),
}}, current.frame_fence);
// Step 3: Present the current frame
_ = try self.gc.dev.queuePresentKHR(self.gc.present_queue.handle, &.{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(¤t.render_finished),
.swapchain_count = 1,
.p_swapchains = @ptrCast(&self.handle),
.p_image_indices = @ptrCast(&self.image_index),
});
// Step 4: Acquire next frame
const result = try self.gc.dev.acquireNextImageKHR(
self.handle,
std.math.maxInt(u64),
self.next_image_acquired,
.null_handle,
);
std.mem.swap(vk.Semaphore, &self.swap_images[result.image_index].image_acquired, &self.next_image_acquired);
self.image_index = result.image_index;
return switch (result.result) {
.success => .optimal,
.suboptimal_khr => .suboptimal,
else => unreachable,
};
}
};
const SwapImage = struct {
image: vk.Image,
view: vk.ImageView,
image_acquired: vk.Semaphore,
render_finished: vk.Semaphore,
frame_fence: vk.Fence,
fn init(gc: *const GraphicsContext, image: vk.Image, format: vk.Format) !SwapImage {
const view = try gc.dev.createImageView(&.{
.image = image,
.view_type = .@"2d",
.format = format,
.components = .{ .r = .identity, .g = .identity, .b = .identity, .a = .identity },
.subresource_range = .{
.aspect_mask = .{ .color_bit = true },
.base_mip_level = 0,
.level_count = 1,
.base_array_layer = 0,
.layer_count = 1,
},
}, null);
errdefer gc.dev.destroyImageView(view, null);
const image_acquired = try gc.dev.createSemaphore(&.{}, null);
errdefer gc.dev.destroySemaphore(image_acquired, null);
const render_finished = try gc.dev.createSemaphore(&.{}, null);
errdefer gc.dev.destroySemaphore(render_finished, null);
const frame_fence = try gc.dev.createFence(&.{ .flags = .{ .signaled_bit = true } }, null);
errdefer gc.dev.destroyFence(frame_fence, null);
return SwapImage{
.image = image,
.view = view,
.image_acquired = image_acquired,
.render_finished = render_finished,
.frame_fence = frame_fence,
};
}
fn deinit(self: SwapImage, gc: *const GraphicsContext) void {
self.waitForFence(gc) catch return;
gc.dev.destroyImageView(self.view, null);
gc.dev.destroySemaphore(self.image_acquired, null);
gc.dev.destroySemaphore(self.render_finished, null);
gc.dev.destroyFence(self.frame_fence, null);
}
fn waitForFence(self: SwapImage, gc: *const GraphicsContext) !void {
_ = try gc.dev.waitForFences(1, @ptrCast(&self.frame_fence), vk.TRUE, std.math.maxInt(u64));
}
};
fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, format: vk.Format, allocator: Allocator) ![]SwapImage {
const images = try gc.dev.getSwapchainImagesAllocKHR(swapchain, allocator);
defer allocator.free(images);
const swap_images = try allocator.alloc(SwapImage, images.len);
errdefer allocator.free(swap_images);
var i: usize = 0;
errdefer for (swap_images[0..i]) |si| si.deinit(gc);
for (images) |image| {
swap_images[i] = try SwapImage.init(gc, image, format);
i += 1;
}
return swap_images;
}
fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.SurfaceFormatKHR {
const preferred = vk.SurfaceFormatKHR{
.format = .b8g8r8a8_srgb,
.color_space = .srgb_nonlinear_khr,
};
const surface_formats = try gc.instance.getPhysicalDeviceSurfaceFormatsAllocKHR(gc.pdev, gc.surface, allocator);
defer allocator.free(surface_formats);
for (surface_formats) |sfmt| {
if (std.meta.eql(sfmt, preferred)) {
return preferred;
}
}
return surface_formats[0]; // There must always be at least one supported surface format
}
fn findPresentMode(gc: *const GraphicsContext, allocator: Allocator) !vk.PresentModeKHR {
const present_modes = try gc.instance.getPhysicalDeviceSurfacePresentModesAllocKHR(gc.pdev, gc.surface, allocator);
defer allocator.free(present_modes);
const preferred = [_]vk.PresentModeKHR{
.mailbox_khr,
.immediate_khr,
};
for (preferred) |mode| {
if (std.mem.indexOfScalar(vk.PresentModeKHR, present_modes, mode) != null) {
return mode;
}
}
return .fifo_khr;
}
fn findActualExtent(caps: vk.SurfaceCapabilitiesKHR, extent: vk.Extent2D) vk.Extent2D {
if (caps.current_extent.width != 0xFFFF_FFFF) {
return caps.current_extent;
} else {
return .{
.width = std.math.clamp(extent.width, caps.min_image_extent.width, caps.max_image_extent.width),
.height = std.math.clamp(extent.height, caps.min_image_extent.height, caps.max_image_extent.height),
};
}
}