annotate semiconginev2/rendering/swapchain.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/rendering/swapchain.nim@e2901100a596
children 5dcb503ef0c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
1 const N_FRAMEBUFFERS = 3'u32
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
2
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
3 proc InitSwapchain*(
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
4 renderPass: VkRenderPass,
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
5 vSync: bool = false,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
6 samples = VK_SAMPLE_COUNT_1_BIT,
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
7 oldSwapchain: ref Swapchain = nil,
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
8 ): Option[Swapchain] =
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
9 assert vulkan.instance.Valid, "Vulkan not initialized"
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
10
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
11 var capabilities: VkSurfaceCapabilitiesKHR
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
12 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkan.physicalDevice, vulkan.surface, addr(capabilities))
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
13 let
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
14 format = DefaultSurfaceFormat()
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
15 width = capabilities.currentExtent.width
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
16 height = capabilities.currentExtent.height
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
17
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
18 if width == 0 or height == 0:
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
19 return none(Swapchain)
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
20
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
21 # following "count" is established according to vulkan specs
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
22 var minFramebufferCount = N_FRAMEBUFFERS
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
23 minFramebufferCount = max(minFramebufferCount, capabilities.minImageCount)
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
24 if capabilities.maxImageCount != 0:
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
25 minFramebufferCount = min(minFramebufferCount, capabilities.maxImageCount)
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
26
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
27 # create swapchain
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
28 let hasTripleBuffering = VK_PRESENT_MODE_MAILBOX_KHR in svkGetPhysicalDeviceSurfacePresentModesKHR()
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
29 var swapchainCreateInfo = VkSwapchainCreateInfoKHR(
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
30 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
31 surface: vulkan.surface,
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
32 minImageCount: minFramebufferCount,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
33 imageFormat: format,
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
34 imageColorSpace: VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, # only one supported without special extensions
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
35 imageExtent: capabilities.currentExtent,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
36 imageArrayLayers: 1,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
37 imageUsage: toBits [VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT],
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
38 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
39 preTransform: capabilities.currentTransform,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
40 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, # only used for blending with other windows, can be opaque
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
41 presentMode: if (vSync or not hasTripleBuffering): VK_PRESENT_MODE_FIFO_KHR else: VK_PRESENT_MODE_MAILBOX_KHR,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
42 clipped: true,
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
43 oldSwapchain: if oldSwapchain != nil: oldSwapchain.vk else: VkSwapchainKHR(0),
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
44 )
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
45 var swapchain: Swapchain
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
46 if vkCreateSwapchainKHR(vulkan.device, addr(swapchainCreateInfo), nil, addr(swapchain.vk)) != VK_SUCCESS:
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
47 return none(Swapchain)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
48
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
49 swapchain.width = width
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
50 swapchain.height = height
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
51 swapchain.renderPass = renderPass
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
52 swapchain.vSync = vSync
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
53 swapchain.samples = samples
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
54 swapchain.oldSwapchain = oldSwapchain
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
55 if swapchain.oldSwapchain != nil:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
56 swapchain.oldSwapchainCounter = INFLIGHTFRAMES.int * 2
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
57
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
58 # create msaa image+view if desired
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
59 if samples != VK_SAMPLE_COUNT_1_BIT:
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
60 swapchain.msaaImage = svkCreate2DImage(
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
61 width = width,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
62 height = height,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
63 format = format,
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
64 usage = [VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT],
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
65 samples = samples,
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
66 )
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
67 let requirements = svkGetImageMemoryRequirements(swapchain.msaaImage)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
68 swapchain.msaaMemory = svkAllocateMemory(
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
69 requirements.size,
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
70 BestMemory(mappable = false, filter = requirements.memoryTypes)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
71 )
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
72 checkVkResult vkBindImageMemory(
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
73 vulkan.device,
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
74 swapchain.msaaImage,
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
75 swapchain.msaaMemory,
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
76 0,
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
77 )
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
78 swapchain.msaaImageView = svkCreate2DImageView(swapchain.msaaImage, format)
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
79
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
80 # create framebuffers
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
81 var actualNFramebuffers: uint32
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
82 checkVkResult vkGetSwapchainImagesKHR(vulkan.device, swapchain.vk, addr(actualNFramebuffers), nil)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
83 var framebuffers = newSeq[VkImage](actualNFramebuffers)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
84 checkVkResult vkGetSwapchainImagesKHR(vulkan.device, swapchain.vk, addr(actualNFramebuffers), framebuffers.ToCPointer)
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
85
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
86 for framebuffer in framebuffers:
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
87 swapchain.framebufferViews.add svkCreate2DImageView(framebuffer, format)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
88 if samples == VK_SAMPLE_COUNT_1_BIT:
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
89 swapchain.framebuffers.add svkCreateFramebuffer(renderPass, width, height, [swapchain.framebufferViews[^1]])
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
90 else:
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
91 swapchain.framebuffers.add svkCreateFramebuffer(renderPass, width, height, [swapchain.msaaImageView, swapchain.framebufferViews[^1]])
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
92
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
93 # create sync primitives
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
94 for i in 0 ..< INFLIGHTFRAMES:
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
95 swapchain.queueFinishedFence[i] = svkCreateFence(signaled = true)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
96 swapchain.imageAvailableSemaphore[i] = svkCreateSemaphore()
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
97 swapchain.renderFinishedSemaphore[i] = svkCreateSemaphore()
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
98
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
99 # command buffers
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
100 var commandPoolCreateInfo = VkCommandPoolCreateInfo(
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
101 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
102 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
103 queueFamilyIndex: vulkan.graphicsQueueFamily,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
104 )
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
105 checkVkResult vkCreateCommandPool(vulkan.device, addr(commandPoolCreateInfo), nil, addr(swapchain.commandBufferPool))
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
106 var allocInfo = VkCommandBufferAllocateInfo(
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
107 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
108 commandPool: swapchain.commandBufferPool,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
109 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
110 commandBufferCount: INFLIGHTFRAMES,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
111 )
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
112 checkVkResult vkAllocateCommandBuffers(vulkan.device, addr(allocInfo), swapchain.commandBuffers.ToCPointer)
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
113
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
114 return some(swapchain)
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
115
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
116 proc DestroySwapchain*(swapchain: Swapchain) =
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
117
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
118 if swapchain.samples != VK_SAMPLE_COUNT_1_BIT:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
119 vkDestroyImageView(vulkan.device, swapchain.msaaImageView, nil)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
120 vkDestroyImage(vulkan.device, swapchain.msaaImage, nil)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
121 vkFreeMemory(vulkan.device, swapchain.msaaMemory, nil)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1201
diff changeset
122
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
123 for fence in swapchain.queueFinishedFence:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
124 vkDestroyFence(vulkan.device, fence, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
125
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
126 for semaphore in swapchain.imageAvailableSemaphore:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
127 vkDestroySemaphore(vulkan.device, semaphore, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
128
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
129 for semaphore in swapchain.renderFinishedSemaphore:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
130 vkDestroySemaphore(vulkan.device, semaphore, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
131
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
132 for imageView in swapchain.framebufferViews:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
133 vkDestroyImageView(vulkan.device, imageView, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
134
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
135 for framebuffer in swapchain.framebuffers:
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
136 vkDestroyFramebuffer(vulkan.device, framebuffer, nil)
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
137
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
138 vkDestroyCommandPool(vulkan.device, swapchain.commandBufferPool, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
139
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
140 vkDestroySwapchainKHR(vulkan.device, swapchain.vk, nil)
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
141
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
142 proc TryAcquireNextImage(swapchain: var Swapchain): Option[VkFramebuffer] =
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
143 if not swapchain.queueFinishedFence[swapchain.currentFiF].Await(100_000_000):
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
144 return none(VkFramebuffer)
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
145
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
146 let nextImageResult = vkAcquireNextImageKHR(
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
147 vulkan.device,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
148 swapchain.vk,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
149 high(uint64),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
150 swapchain.imageAvailableSemaphore[swapchain.currentFiF],
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
151 VkFence(0),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
152 addr(swapchain.currentFramebufferIndex),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
153 )
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
154
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
155 swapchain.queueFinishedFence[swapchain.currentFiF].svkResetFences()
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
156
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
157 if nextImageResult != VK_SUCCESS:
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
158 return none(VkFramebuffer)
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
159 return some(swapchain.framebuffers[swapchain.currentFramebufferIndex])
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
160
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
161 proc Swap(swapchain: var Swapchain, commandBuffer: VkCommandBuffer): bool =
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
162 var
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
163 waitStage = VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
164 submitInfo = VkSubmitInfo(
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
165 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
166 waitSemaphoreCount: 1,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
167 pWaitSemaphores: addr(swapchain.imageAvailableSemaphore[swapchain.currentFiF]),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
168 pWaitDstStageMask: addr(waitStage),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
169 commandBufferCount: 1,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
170 pCommandBuffers: addr(commandBuffer),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
171 signalSemaphoreCount: 1,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
172 pSignalSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentFiF]),
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
173 )
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
174 checkVkResult vkQueueSubmit(
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
175 queue = vulkan.graphicsQueue,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
176 submitCount = 1,
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
177 pSubmits = addr(submitInfo),
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
178 fence = swapchain.queueFinishedFence[swapchain.currentFiF]
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
179 )
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents:
diff changeset
180
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
181 var presentInfo = VkPresentInfoKHR(
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
182 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
183 waitSemaphoreCount: 1,
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
184 pWaitSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentFiF]),
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
185 swapchainCount: 1,
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
186 pSwapchains: addr(swapchain.vk),
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
187 pImageIndices: addr(swapchain.currentFramebufferIndex),
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
188 pResults: nil,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
189 )
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
190 let presentResult = vkQueuePresentKHR(vulkan.graphicsQueue, addr(presentInfo))
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
191
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
192 if swapchain.oldSwapchain != nil:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
193 dec swapchain.oldSwapchainCounter
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
194 if swapchain.oldSwapchainCounter <= 0:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
195 DestroySwapchain(swapchain.oldSwapchain[])
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
196 swapchain.oldSwapchain = nil
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
197
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
198 if presentResult != VK_SUCCESS:
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
199 return false
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
200
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
201 swapchain.currentFiF = (uint32(swapchain.currentFiF) + 1) mod INFLIGHTFRAMES
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
202 return true
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
203
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
204 proc Recreate(swapchain: Swapchain): Option[Swapchain] =
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
205 var oldSwapchain = new Swapchain
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
206 oldSwapchain[] = swapchain
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
207 InitSwapchain(
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
208 renderPass = swapchain.renderPass,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
209 vSync = swapchain.vSync,
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
210 samples = swapchain.samples,
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
211 oldSwapchain = oldSwapchain,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
212 )
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
213
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
214 template WithNextFrame*(swapchain: var Swapchain, framebufferName, commandBufferName, body: untyped): untyped =
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
215 var maybeFramebuffer = TryAcquireNextImage(swapchain)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
216 if maybeFramebuffer.isSome:
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
217 block:
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
218 let `framebufferName` {.inject.} = maybeFramebuffer.get
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
219 let `commandBufferName` {.inject.} = swapchain.commandBuffers[swapchain.currentFiF]
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
220 let beginInfo = VkCommandBufferBeginInfo(
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
221 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
222 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
223 )
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
224 checkVkResult vkResetCommandBuffer(`commandBufferName`, VkCommandBufferResetFlags(0))
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
225 checkVkResult vkBeginCommandBuffer(`commandBufferName`, addr(beginInfo))
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
226
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
227 body
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
228
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
229 checkVkResult vkEndCommandBuffer(`commandBufferName`)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
230 discard Swap(swapchain = swapchain, commandBuffer = `commandBufferName`)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
231 else:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
232 let maybeNewSwapchain = Recreate(swapchain)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
233 if maybeNewSwapchain.isSome:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
234 swapchain = maybeNewSwapchain.get
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
235