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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package vk_backend
import bk ".."
import gpu "../../core"
import "core:log"
import vk "vendor:vulkan"
// --- Lifecycle ---
shutdown_vk :: proc() {
if g_vk == nil {return}
vk.DeviceWaitIdle(g_vk.device.device)
// Destroy all active samplers
for &entry in g_vk.samplers {
if entry.active {
vk.DestroySampler(g_vk.device.device, entry.handle, nil)
entry.active = false
}
}
// Destroy all active framebuffers
for &entry in g_vk.framebuffers {
if entry.active {
vk.DestroyFramebuffer(g_vk.device.device, entry.handle, nil)
entry.active = false
}
}
// Destroy all active render passes (except swapchain's)
for &entry in g_vk.render_passes {
if entry.active && entry.handle != g_vk.swapchain.render_pass {
vk.DestroyRenderPass(g_vk.device.device, entry.handle, nil)
}
entry.active = false
}
// Destroy all active descriptors
for &entry in g_vk.descriptors {
if entry.active {
switch entry.kind {
case .Set_Layout:
vk.DestroyDescriptorSetLayout(g_vk.device.device, entry.set_layout, nil)
case .Pool:
vk.DestroyDescriptorPool(g_vk.device.device, entry.pool, nil)
case .Set:
// Sets freed when pool is destroyed
}
entry.active = false
}
}
// Destroy all active shaders
for &entry in g_vk.shaders {
if entry.active {
vk.DestroyShaderModule(g_vk.device.device, entry.module.handle, nil)
entry.active = false
}
}
// Destroy all active pipelines
for &entry in g_vk.pipelines {
if entry.active {
if entry.is_compute {
if entry.compute.pipeline != 0 {
vk.DestroyPipeline(g_vk.device.device, entry.compute.pipeline, nil)
}
if entry.compute.layout != 0 {
vk.DestroyPipelineLayout(g_vk.device.device, entry.compute.layout, nil)
}
if entry.compute.desc_pool != 0 {
vk.DestroyDescriptorPool(g_vk.device.device, entry.compute.desc_pool, nil)
}
if entry.compute.desc_set_layout != 0 {
vk.DestroyDescriptorSetLayout(
g_vk.device.device,
entry.compute.desc_set_layout,
nil,
)
}
} else {
if entry.graphics.pipeline != 0 {
vk.DestroyPipeline(g_vk.device.device, entry.graphics.pipeline, nil)
}
if entry.graphics.layout != 0 {
vk.DestroyPipelineLayout(g_vk.device.device, entry.graphics.layout, nil)
}
}
entry.active = false
}
}
// Destroy swapchain framebuffers before textures -- they reference the depth view
for fb in g_vk.swapchain.framebuffers {
if fb != 0 {
vk.DestroyFramebuffer(g_vk.device.device, fb, nil)
}
}
delete(g_vk.swapchain.framebuffers)
g_vk.swapchain.framebuffers = nil
// Destroy all active textures
for &entry in g_vk.textures {
if entry.active {
gpu.destroy_image(&g_vk.device, &entry.image)
entry.active = false
}
}
// Clear swapchain depth_view since the texture pool already destroyed it
g_vk.swapchain.depth_view = 0
// Destroy all active buffers
for &entry in g_vk.buffers {
if entry.active {
gpu.destroy_buffer(&g_vk.device, &entry.buffer)
entry.active = false
}
}
// Destroy core Vulkan objects
if g_vk.cmd_pool != 0 {
vk.DestroyCommandPool(g_vk.device.device, g_vk.cmd_pool, nil)
}
gpu.destroy_sync_objects(&g_vk.device, &g_vk.frame_sync)
gpu.destroy_swapchain(&g_vk.swapchain, &g_vk.device)
if g_vk.surface != 0 {
vk.DestroySurfaceKHR(g_vk.instance.instance, g_vk.surface, nil)
}
gpu.destroy_device(&g_vk.device)
gpu.destroy_instance(&g_vk.instance)
free(g_vk)
g_vk = nil
bk.backend_initialized = false
}
wait_idle_vk :: proc() {
if g_vk != nil {
vk.DeviceWaitIdle(g_vk.device.device)
}
}
// --- Frame lifecycle ---
begin_frame_vk :: proc(clear_color: [4]f32) -> (bk.Frame_Context, bool) {
if g_vk == nil {return {}, false}
frame := g_vk.current_frame
sync := &g_vk.frame_sync[frame]
cmd := g_vk.cmd_buffers[frame]
// Wait for previous frame's fence
vk.WaitForFences(g_vk.device.device, 1, &sync.in_flight, true, max(u64))
if g_vk.headless {
vk.ResetFences(g_vk.device.device, 1, &sync.in_flight)
vk.ResetCommandBuffer(cmd, {})
begin_info := vk.CommandBufferBeginInfo {
sType = .COMMAND_BUFFER_BEGIN_INFO,
}
result := vk.BeginCommandBuffer(cmd, &begin_info)
if result != .SUCCESS {
log.errorf("gpu/vk: headless vkBeginCommandBuffer failed: %v", result)
return {}, false
}
g_vk.frame_active = true
return bk.Frame_Context{frame_index = frame}, true
}
// Acquire next swapchain image
result := vk.AcquireNextImageKHR(
g_vk.device.device,
g_vk.swapchain.handle,
max(u64),
sync.image_available,
0,
&g_vk.image_index,
)
if result == .ERROR_OUT_OF_DATE_KHR {
// Swapchain is stale — recreate immediately so the next frame can acquire.
extent := g_vk.swapchain.extent
gpu.recreate_swapchain(
&g_vk.swapchain,
&g_vk.device,
g_vk.surface,
extent.width,
extent.height,
)
recreate_swapchain_depth_and_framebuffers()
if g_vk.default_render_pass != bk.NULL_RENDER_PASS {
g_vk.render_passes[g_vk.default_render_pass].handle = g_vk.swapchain.render_pass
}
return {}, false
}
// Reset fence after successful acquire
vk.ResetFences(g_vk.device.device, 1, &sync.in_flight)
// Reset and begin command buffer
vk.ResetCommandBuffer(cmd, {})
begin_info := vk.CommandBufferBeginInfo {
sType = .COMMAND_BUFFER_BEGIN_INFO,
}
vk.BeginCommandBuffer(cmd, &begin_info)
g_vk.frame_active = true
return bk.Frame_Context{frame_index = frame}, true
}
end_frame_vk :: proc(ctx: bk.Frame_Context) -> bool {
if g_vk == nil || !g_vk.frame_active {return false}
frame := ctx.frame_index
sync := &g_vk.frame_sync[frame]
cmd := g_vk.cmd_buffers[frame]
// End command buffer
result := vk.EndCommandBuffer(cmd)
if result != .SUCCESS {
log.errorf("gpu/vk: vkEndCommandBuffer failed: %v", result)
g_vk.frame_active = false
return false
}
if g_vk.headless {
submit_info := vk.SubmitInfo {
sType = .SUBMIT_INFO,
commandBufferCount = 1,
pCommandBuffers = &cmd,
}
result = vk.QueueSubmit(g_vk.device.graphics_queue, 1, &submit_info, sync.in_flight)
if result != .SUCCESS {
log.errorf("gpu/vk: headless vkQueueSubmit failed: %v", result)
}
g_vk.frame_active = false
g_vk.current_frame = (frame + 1) % bk.MAX_FRAMES_IN_FLIGHT
return result == .SUCCESS
}
// Submit
wait_stage := vk.PipelineStageFlags{.COLOR_ATTACHMENT_OUTPUT}
submit_info := vk.SubmitInfo {
sType = .SUBMIT_INFO,
waitSemaphoreCount = 1,
pWaitSemaphores = &sync.image_available,
pWaitDstStageMask = &wait_stage,
commandBufferCount = 1,
pCommandBuffers = &cmd,
signalSemaphoreCount = 1,
pSignalSemaphores = &g_vk.swapchain.render_finished_semaphores[g_vk.image_index],
}
result = vk.QueueSubmit(g_vk.device.graphics_queue, 1, &submit_info, sync.in_flight)
if result != .SUCCESS {
log.errorf("gpu/vk: vkQueueSubmit failed: %v", result)
}
// Present
sc_handle := g_vk.swapchain.handle
present_info := vk.PresentInfoKHR {
sType = .PRESENT_INFO_KHR,
waitSemaphoreCount = 1,
pWaitSemaphores = &g_vk.swapchain.render_finished_semaphores[g_vk.image_index],
swapchainCount = 1,
pSwapchains = &sc_handle,
pImageIndices = &g_vk.image_index,
}
result = vk.QueuePresentKHR(g_vk.device.present_queue, &present_info)
g_vk.frame_active = false
g_vk.current_frame = (frame + 1) % bk.MAX_FRAMES_IN_FLIGHT
if result == .ERROR_OUT_OF_DATE_KHR || result == .SUBOPTIMAL_KHR {
// Wait for the just-submitted GPU work to complete before
// destroying swapchain resources. This ensures semaphores are
// fully consumed and Wayland explicit sync state is clean.
vk.WaitForFences(g_vk.device.device, 1, &sync.in_flight, true, max(u64))
extent := g_vk.swapchain.extent
gpu.recreate_swapchain(
&g_vk.swapchain,
&g_vk.device,
g_vk.surface,
extent.width,
extent.height,
)
recreate_swapchain_depth_and_framebuffers()
// Update default render pass handle
if g_vk.default_render_pass != bk.NULL_RENDER_PASS {
g_vk.render_passes[g_vk.default_render_pass].handle = g_vk.swapchain.render_pass
}
return true
}
return false
}
on_resize_vk :: proc(width, height: u32) {
if g_vk == nil {return}
if width == 0 || height == 0 {return}
if g_vk.headless {
g_vk.headless_extent = {width, height}
return
}
vk.DeviceWaitIdle(g_vk.device.device)
gpu.recreate_swapchain(&g_vk.swapchain, &g_vk.device, g_vk.surface, width, height)
recreate_swapchain_depth_and_framebuffers()
// Update default render pass handle
if g_vk.default_render_pass != bk.NULL_RENDER_PASS {
g_vk.render_passes[g_vk.default_render_pass].handle = g_vk.swapchain.render_pass
}
}
get_extent_vk :: proc() -> bk.Extent {
if g_vk == nil {return {}}
if g_vk.headless do return g_vk.headless_extent
return bk.Extent{width = g_vk.swapchain.extent.width, height = g_vk.swapchain.extent.height}
}
// --- Sync queries ---
get_default_render_pass_vk :: proc() -> bk.Render_Pass_Handle {
if g_vk == nil {return bk.NULL_RENDER_PASS}
return g_vk.default_render_pass
}
get_depth_format_vk :: proc() -> bk.Format {
if g_vk == nil {return .Undefined}
if g_vk.headless {return .Undefined}
return from_vk_format(g_vk.swapchain.depth_format)
}
// Recreate depth buffer and framebuffers after swapchain recreation.
@(private)
recreate_swapchain_depth_and_framebuffers :: proc() {
sc := &g_vk.swapchain
// Destroy old depth texture pool entry (image + view + memory)
if sc.depth_view != 0 {
for &entry in g_vk.textures {
if entry.active && entry.image.view == sc.depth_view {
gpu.destroy_image(&g_vk.device, &entry.image)
entry.active = false
break
}
}
sc.depth_view = 0
}
// Recreate depth buffer if we have a depth format
if sc.depth_format != .UNDEFINED {
depth_img, depth_ok := gpu.create_image(
&g_vk.device,
sc.extent.width,
sc.extent.height,
sc.depth_format,
.OPTIMAL,
{.DEPTH_STENCIL_ATTACHMENT},
{.DEVICE_LOCAL},
)
if depth_ok {
depth_view, dv_ok := gpu.create_image_view(
&g_vk.device,
depth_img.image,
sc.depth_format,
{.DEPTH},
)
if dv_ok {
depth_img.view = depth_view
sc.depth_view = depth_view
// Store in texture pool
dh, dh_ok := alloc_texture_handle()
if dh_ok {
g_vk.textures[dh].image = depth_img
g_vk.textures[dh].active = true
}
} else {
gpu.destroy_image(&g_vk.device, &depth_img)
}
}
}
gpu.create_framebuffers(sc, &g_vk.device)
}