Mercurial > games > semicongine
annotate src/semicongine/engine.nim @ 522:f2c97bdbb0b3
did: rename and update older demos to work with new APIs
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 24 Jan 2023 10:22:38 +0700 |
parents | a25325bec7f2 |
children | 311ee4e58032 |
rev | line source |
---|---|
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
1 import std/options |
501 | 2 import std/times |
3 import std/typetraits | |
4 import std/strformat | |
5 import std/enumerate | |
6 import std/logging | |
7 | |
8 | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
9 import ./math/vector |
501 | 10 import ./vulkan |
11 import ./vulkan_helpers | |
12 import ./window | |
13 import ./events | |
14 import ./shader | |
15 import ./vertex | |
16 import ./buffer | |
17 import ./thing | |
18 import ./mesh | |
19 import ./descriptor | |
20 | |
21 const MAX_FRAMES_IN_FLIGHT = 2 | |
22 const DEBUG_LOG = not defined(release) | |
23 | |
24 var logger = newConsoleLogger() | |
25 addHandler(logger) | |
26 | |
27 | |
28 const VULKAN_VERSION = VK_MAKE_API_VERSION(0'u32, 1'u32, 2'u32, 0'u32) | |
29 const ENGINE_NAME = "zamkongine" | |
30 const ENGINE_VERSION = "0.1" | |
31 const BUILD_VERSION = ENGINE_VERSION & '-' & gorge("git log -1 --format=format:'%H'") | |
32 echo "Engine: " & ENGINE_NAME & " " & BUILD_VERSION | |
33 | |
34 type | |
35 Device = object | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
36 device*: VkDevice |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
37 physicalDevice*: PhysicalDevice |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
38 graphicsQueueFamily*: uint32 |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
39 presentationQueueFamily*: uint32 |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
40 graphicsQueue*: VkQueue |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
41 presentationQueue*: VkQueue |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
42 commandPool*: VkCommandPool |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
43 commandBuffers*: array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer] |
501 | 44 Swapchain = object |
45 swapchain: VkSwapchainKHR | |
46 images: seq[VkImage] | |
47 imageviews: seq[VkImageView] | |
48 RenderPipeline*[VertexType, Uniforms] = object | |
49 device*: VkDevice | |
50 shaders*: seq[ShaderProgram[VertexType, Uniforms]] | |
51 layout*: VkPipelineLayout | |
52 pipeline*: VkPipeline | |
53 vertexBuffers*: seq[(seq[Buffer], uint32)] | |
54 indexedVertexBuffers*: seq[(seq[Buffer], Buffer, uint32, VkIndexType)] | |
55 descriptorSetLayout*: VkDescriptorSetLayout | |
56 uniformBuffers*: array[MAX_FRAMES_IN_FLIGHT, Buffer] | |
57 descriptorPool*: VkDescriptorPool | |
58 descriptors: array[MAX_FRAMES_IN_FLIGHT, VkDescriptorSet] | |
59 QueueFamily = object | |
60 properties*: VkQueueFamilyProperties | |
61 hasSurfaceSupport*: bool | |
62 PhysicalDevice = object | |
63 device*: VkPhysicalDevice | |
64 extensions*: seq[string] | |
65 properties*: VkPhysicalDeviceProperties | |
66 features*: VkPhysicalDeviceFeatures | |
67 queueFamilies*: seq[QueueFamily] | |
68 formats: seq[VkSurfaceFormatKHR] | |
69 presentModes: seq[VkPresentModeKHR] | |
70 Vulkan* = object | |
71 debugMessenger*: VkDebugUtilsMessengerEXT | |
72 instance*: VkInstance | |
73 deviceList*: seq[PhysicalDevice] | |
74 device*: Device | |
75 surface*: VkSurfaceKHR | |
76 surfaceFormat*: VkSurfaceFormatKHR | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
77 frameSize*: TVec2[uint32] |
501 | 78 swapchain*: Swapchain |
79 framebuffers*: seq[VkFramebuffer] | |
80 renderPass*: VkRenderPass | |
81 imageAvailableSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore] | |
82 renderFinishedSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore] | |
83 inFlightFences*: array[MAX_FRAMES_IN_FLIGHT, VkFence] | |
84 Input* = object | |
85 keysDown*: set[Key] | |
86 keysPressed*: set[Key] | |
87 keysReleased*: set[Key] | |
88 mouseDown*: set[MouseButton] | |
89 mousePressed*: set[MouseButton] | |
90 mouseReleased*: set[MouseButton] | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
91 mousePos*: Vec2 |
501 | 92 Engine* = object |
93 vulkan*: Vulkan | |
94 window*: NativeWindow | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
95 currentscenedata*: Thing |
501 | 96 input*: Input |
97 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
98 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
99 PhysicalDevice] = |
501 | 100 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance): |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
101 var device = PhysicalDevice(device: vulkanPhysicalDevice, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
102 extensions: getDeviceExtensions(vulkanPhysicalDevice)) |
501 | 103 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties)) |
104 vkGetPhysicalDeviceFeatures(vulkanPhysicalDevice, addr(device.features)) | |
105 device.formats = vulkanPhysicalDevice.getDeviceSurfaceFormats(surface) | |
106 device.presentModes = vulkanPhysicalDevice.getDeviceSurfacePresentModes(surface) | |
107 | |
108 debug(&"Physical device nr {int(vulkanPhysicalDevice)} {cleanString(device.properties.deviceName)}") | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
109 for i, queueFamilyProperty in enumerate(getQueueFamilies( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
110 vulkanPhysicalDevice)): |
501 | 111 var hasSurfaceSupport: VkBool32 = VK_FALSE |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
112 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
113 uint32(i), surface, addr(hasSurfaceSupport)) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
114 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
115 hasSurfaceSupport: bool(hasSurfaceSupport))) |
501 | 116 debug(&" Queue family {i} {queueFamilyProperty}") |
117 | |
118 result.add(device) | |
119 | |
120 proc filterForDevice(devices: seq[PhysicalDevice]): seq[(PhysicalDevice, uint32, uint32)] = | |
121 for device in devices: | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
122 if not (device.formats.len > 0 and device.presentModes.len > 0 and |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
123 "VK_KHR_swapchain" in device.extensions): |
501 | 124 continue |
125 var graphicsQueueFamily = high(uint32) | |
126 var presentationQueueFamily = high(uint32) | |
127 for i, queueFamily in enumerate(device.queueFamilies): | |
128 if queueFamily.hasSurfaceSupport: | |
129 presentationQueueFamily = uint32(i) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
130 if bool(uint32(queueFamily.properties.queueFlags) and ord( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
131 VK_QUEUE_GRAPHICS_BIT)): |
501 | 132 graphicsQueueFamily = uint32(i) |
133 if graphicsQueueFamily != high(uint32) and presentationQueueFamily != high(uint32): | |
134 result.add((device, graphicsQueueFamily, presentationQueueFamily)) | |
135 | |
136 for (device, graphicsQueueFamily, presentationQueueFamily) in result: | |
137 debug(&"Viable device: {cleanString(device.properties.deviceName)} (graphics queue family {graphicsQueueFamily}, presentation queue family {presentationQueueFamily})") | |
138 | |
139 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
140 proc getFrameDimension(window: NativeWindow, device: VkPhysicalDevice, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
141 surface: VkSurfaceKHR): TVec2[uint32] = |
501 | 142 let capabilities = device.getSurfaceCapabilities(surface) |
143 if capabilities.currentExtent.width != high(uint32): | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
144 return TVec2[uint32]([capabilities.currentExtent.width, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
145 capabilities.currentExtent.height]) |
501 | 146 else: |
147 let (width, height) = window.size() | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
148 return TVec2[uint32]([ |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
149 min(max(uint32(width), capabilities.minImageExtent.width), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
150 capabilities.maxImageExtent.width), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
151 min(max(uint32(height), capabilities.minImageExtent.height), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
152 capabilities.maxImageExtent.height), |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
153 ]) |
501 | 154 |
155 when DEBUG_LOG: | |
156 proc setupDebugLog(instance: VkInstance): VkDebugUtilsMessengerEXT = | |
157 var createInfo = VkDebugUtilsMessengerCreateInfoEXT( | |
158 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, | |
159 messageSeverity: VkDebugUtilsMessageSeverityFlagsEXT( | |
160 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) or | |
161 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) or | |
162 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) | |
163 ), | |
164 messageType: VkDebugUtilsMessageTypeFlagsEXT( | |
165 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) or | |
166 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) or | |
167 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) | |
168 ), | |
169 pfnUserCallback: debugCallback, | |
170 pUserData: nil, | |
171 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
172 checkVkResult instance.vkCreateDebugUtilsMessengerEXT(addr(createInfo), nil, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
173 addr(result)) |
501 | 174 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
175 proc setupVulkanDeviceAndQueues(instance: VkInstance, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
176 surface: VkSurfaceKHR): Device = |
501 | 177 let usableDevices = instance.getAllPhysicalDevices(surface).filterForDevice() |
178 if len(usableDevices) == 0: | |
179 raise newException(Exception, "No suitable graphics device found") | |
180 result.physicalDevice = usableDevices[0][0] | |
181 result.graphicsQueueFamily = usableDevices[0][1] | |
182 result.presentationQueueFamily = usableDevices[0][2] | |
183 | |
184 debug(&"Chose device {cleanString(result.physicalDevice.properties.deviceName)}") | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
185 |
501 | 186 (result.device, result.graphicsQueue, result.presentationQueue) = getVulcanDevice( |
187 result.physicalDevice.device, | |
188 result.physicalDevice.features, | |
189 result.graphicsQueueFamily, | |
190 result.presentationQueueFamily, | |
191 ) | |
192 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
193 proc setupSwapChain(device: VkDevice, physicalDevice: PhysicalDevice, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
194 surface: VkSurfaceKHR, dimension: TVec2[uint32], |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
195 surfaceFormat: VkSurfaceFormatKHR): Swapchain = |
501 | 196 |
197 let capabilities = physicalDevice.device.getSurfaceCapabilities(surface) | |
198 var selectedPresentationMode = getPresentMode(physicalDevice.presentModes) | |
513 | 199 var imageCount = capabilities.minImageCount + 1 |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
200 if capabilities.maxImageCount > 0: |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
201 imageCount = min(capabilities.maxImageCount, imageCount) |
501 | 202 # setup swapchain |
203 var swapchainCreateInfo = VkSwapchainCreateInfoKHR( | |
204 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, | |
205 surface: surface, | |
513 | 206 minImageCount: imageCount, |
501 | 207 imageFormat: surfaceFormat.format, |
208 imageColorSpace: surfaceFormat.colorSpace, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
209 imageExtent: VkExtent2D(width: dimension[0], height: dimension[1]), |
501 | 210 imageArrayLayers: 1, |
211 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), | |
212 # VK_SHARING_MODE_CONCURRENT no supported (i.e cannot use different queue families for drawing to swap surface?) | |
213 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE, | |
214 preTransform: capabilities.currentTransform, | |
215 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, | |
216 presentMode: selectedPresentationMode, | |
217 clipped: VK_TRUE, | |
218 oldSwapchain: VkSwapchainKHR(0), | |
219 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
220 checkVkResult device.vkCreateSwapchainKHR(addr(swapchainCreateInfo), nil, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
221 addr(result.swapchain)) |
501 | 222 result.images = device.getSwapChainImages(result.swapchain) |
223 | |
224 # setup swapchian image views | |
225 | |
226 result.imageviews = newSeq[VkImageView](result.images.len) | |
227 for i, image in enumerate(result.images): | |
228 var imageViewCreateInfo = VkImageViewCreateInfo( | |
229 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, | |
230 image: image, | |
231 viewType: VK_IMAGE_VIEW_TYPE_2D, | |
232 format: surfaceFormat.format, | |
233 components: VkComponentMapping( | |
234 r: VK_COMPONENT_SWIZZLE_IDENTITY, | |
235 g: VK_COMPONENT_SWIZZLE_IDENTITY, | |
236 b: VK_COMPONENT_SWIZZLE_IDENTITY, | |
237 a: VK_COMPONENT_SWIZZLE_IDENTITY, | |
238 ), | |
239 subresourceRange: VkImageSubresourceRange( | |
240 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT), | |
241 baseMipLevel: 0, | |
242 levelCount: 1, | |
243 baseArrayLayer: 0, | |
244 layerCount: 1, | |
245 ), | |
246 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
247 checkVkResult device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
248 result.imageviews[i])) |
501 | 249 |
250 proc setupRenderPass(device: VkDevice, format: VkFormat): VkRenderPass = | |
251 var | |
252 colorAttachment = VkAttachmentDescription( | |
253 format: format, | |
254 samples: VK_SAMPLE_COUNT_1_BIT, | |
255 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, | |
256 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
257 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
258 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
259 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
260 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
261 ) | |
262 colorAttachmentRef = VkAttachmentReference( | |
263 attachment: 0, | |
264 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
265 ) | |
266 subpass = VkSubpassDescription( | |
267 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
268 colorAttachmentCount: 1, | |
269 pColorAttachments: addr(colorAttachmentRef) | |
270 ) | |
271 dependency = VkSubpassDependency( | |
272 srcSubpass: VK_SUBPASS_EXTERNAL, | |
273 dstSubpass: 0, | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
274 srcStageMask: VkPipelineStageFlags( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
275 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT), |
501 | 276 srcAccessMask: VkAccessFlags(0), |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
277 dstStageMask: VkPipelineStageFlags( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
278 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT), |
501 | 279 dstAccessMask: VkAccessFlags(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT), |
280 ) | |
281 renderPassCreateInfo = VkRenderPassCreateInfo( | |
282 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | |
283 attachmentCount: 1, | |
284 pAttachments: addr(colorAttachment), | |
285 subpassCount: 1, | |
286 pSubpasses: addr(subpass), | |
287 dependencyCount: 1, | |
288 pDependencies: addr(dependency), | |
289 ) | |
290 checkVkResult device.vkCreateRenderPass(addr(renderPassCreateInfo), nil, addr(result)) | |
291 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
292 proc initRenderPipeline[VertexType, Uniforms](device: VkDevice, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
293 frameSize: TVec2[uint32], renderPass: VkRenderPass, vertexShader, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
294 fragmentShader: static string): RenderPipeline[VertexType, Uniforms] = |
501 | 295 # load shaders |
296 result.device = device | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
297 result.shaders.add(initShaderProgram[VertexType, Uniforms](device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
298 VK_SHADER_STAGE_VERTEX_BIT, vertexShader)) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
299 result.shaders.add(initShaderProgram[VertexType, Uniforms](device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
300 VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShader)) |
501 | 301 |
302 var | |
303 # define which parts can be dynamic (pipeline is fixed after setup) | |
304 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
305 dynamicState = VkPipelineDynamicStateCreateInfo( | |
306 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
307 dynamicStateCount: uint32(dynamicStates.len), | |
308 pDynamicStates: addr(dynamicStates[0]), | |
309 ) | |
310 vertexbindings = generateInputVertexBinding[VertexType]() | |
311 attributebindings = generateInputAttributeBinding[VertexType]() | |
312 | |
313 # define input data format | |
314 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
315 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
316 vertexBindingDescriptionCount: uint32(vertexbindings.len), | |
317 pVertexBindingDescriptions: addr(vertexbindings[0]), | |
318 vertexAttributeDescriptionCount: uint32(attributebindings.len), | |
319 pVertexAttributeDescriptions: addr(attributebindings[0]), | |
320 ) | |
321 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
322 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
323 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, | |
324 primitiveRestartEnable: VK_FALSE, | |
325 ) | |
326 | |
327 # setup viewport | |
328 var viewportState = VkPipelineViewportStateCreateInfo( | |
329 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
330 viewportCount: 1, | |
331 scissorCount: 1, | |
332 ) | |
333 | |
334 # rasterizerization config | |
335 var | |
336 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
337 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
338 depthClampEnable: VK_FALSE, | |
339 rasterizerDiscardEnable: VK_FALSE, | |
340 polygonMode: VK_POLYGON_MODE_FILL, | |
341 lineWidth: 1.0, | |
342 cullMode: VkCullModeFlags(VK_CULL_MODE_BACK_BIT), | |
343 frontFace: VK_FRONT_FACE_CLOCKWISE, | |
344 depthBiasEnable: VK_FALSE, | |
345 depthBiasConstantFactor: 0.0, | |
346 depthBiasClamp: 0.0, | |
347 depthBiasSlopeFactor: 0.0, | |
348 ) | |
349 multisampling = VkPipelineMultisampleStateCreateInfo( | |
350 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
351 sampleShadingEnable: VK_FALSE, | |
352 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
353 minSampleShading: 1.0, | |
354 pSampleMask: nil, | |
355 alphaToCoverageEnable: VK_FALSE, | |
356 alphaToOneEnable: VK_FALSE, | |
357 ) | |
358 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
359 colorWriteMask: VkColorComponentFlags( | |
360 ord(VK_COLOR_COMPONENT_R_BIT) or | |
361 ord(VK_COLOR_COMPONENT_G_BIT) or | |
362 ord(VK_COLOR_COMPONENT_B_BIT) or | |
363 ord(VK_COLOR_COMPONENT_A_BIT) | |
364 ), | |
365 blendEnable: VK_TRUE, | |
366 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
367 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
368 colorBlendOp: VK_BLEND_OP_ADD, | |
369 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
370 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
371 alphaBlendOp: VK_BLEND_OP_ADD, | |
372 ) | |
373 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
374 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
375 logicOpEnable: VK_TRUE, | |
376 logicOp: VK_LOGIC_OP_COPY, | |
377 attachmentCount: 1, | |
378 pAttachments: addr(colorBlendAttachment), | |
379 blendConstants: [0.0'f, 0.0'f, 0.0'f, 0.0'f], | |
380 ) | |
381 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
382 result.descriptorSetLayout = device.createUniformDescriptorLayout( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
383 VkShaderStageFlags(VK_SHADER_STAGE_VERTEX_BIT), 0) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
384 var |
501 | 385 # "globals" that go into the shader, uniforms etc. |
386 pipelineLayoutInfo = VkPipelineLayoutCreateInfo( | |
387 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
388 setLayoutCount: 1, | |
389 pSetLayouts: addr(result.descriptorSetLayout), | |
390 pushConstantRangeCount: 0, | |
391 pPushConstantRanges: nil, | |
392 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
393 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
394 addr(result.layout)) |
501 | 395 |
396 var stages: seq[VkPipelineShaderStageCreateInfo] | |
397 for shader in result.shaders: | |
398 stages.add(shader.shader) | |
399 var pipelineInfo = VkGraphicsPipelineCreateInfo( | |
400 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
401 stageCount: uint32(stages.len), | |
402 pStages: addr(stages[0]), | |
403 pVertexInputState: addr(vertexInputInfo), | |
404 pInputAssemblyState: addr(inputAssembly), | |
405 pViewportState: addr(viewportState), | |
406 pRasterizationState: addr(rasterizer), | |
407 pMultisampleState: addr(multisampling), | |
408 pDepthStencilState: nil, | |
409 pColorBlendState: addr(colorBlending), | |
410 pDynamicState: addr(dynamicState), | |
411 layout: result.layout, | |
412 renderPass: renderPass, | |
413 subpass: 0, | |
414 basePipelineHandle: VkPipeline(0), | |
415 basePipelineIndex: -1, | |
416 ) | |
417 checkVkResult vkCreateGraphicsPipelines( | |
418 device, | |
419 VkPipelineCache(0), | |
420 1, | |
421 addr(pipelineInfo), | |
422 nil, | |
423 addr(result.pipeline) | |
424 ) | |
425 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
426 proc setupFramebuffers(device: VkDevice, swapchain: var Swapchain, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
427 renderPass: VkRenderPass, dimension: TVec2[uint32]): seq[VkFramebuffer] = |
501 | 428 result = newSeq[VkFramebuffer](swapchain.images.len) |
429 for i, imageview in enumerate(swapchain.imageviews): | |
430 var framebufferInfo = VkFramebufferCreateInfo( | |
431 sType: VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, | |
432 renderPass: renderPass, | |
433 attachmentCount: 1, | |
434 pAttachments: addr(swapchain.imageviews[i]), | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
435 width: dimension[0], |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
436 height: dimension[1], |
501 | 437 layers: 1, |
438 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
439 checkVkResult device.vkCreateFramebuffer(addr(framebufferInfo), nil, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
440 result[i])) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
441 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
442 proc trash(device: VkDevice, swapchain: Swapchain, framebuffers: seq[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
443 VkFramebuffer]) = |
501 | 444 for framebuffer in framebuffers: |
445 device.vkDestroyFramebuffer(framebuffer, nil) | |
446 for imageview in swapchain.imageviews: | |
447 device.vkDestroyImageView(imageview, nil) | |
448 device.vkDestroySwapchainKHR(swapchain.swapchain, nil) | |
449 | |
450 proc recreateSwapchain(vulkan: Vulkan): (Swapchain, seq[VkFramebuffer]) = | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
451 if vulkan.frameSize.x == 0 or vulkan.frameSize.y == 0: |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
452 return (vulkan.swapchain, vulkan.framebuffers) |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
453 debug(&"Recreate swapchain with dimension {vulkan.frameSize}") |
501 | 454 checkVkResult vulkan.device.device.vkDeviceWaitIdle() |
455 | |
456 vulkan.device.device.trash(vulkan.swapchain, vulkan.framebuffers) | |
457 | |
458 result[0] = vulkan.device.device.setupSwapChain( | |
459 vulkan.device.physicalDevice, | |
460 vulkan.surface, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
461 vulkan.frameSize, |
501 | 462 vulkan.surfaceFormat |
463 ) | |
464 result[1] = vulkan.device.device.setupFramebuffers( | |
465 result[0], | |
466 vulkan.renderPass, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
467 vulkan.frameSize |
501 | 468 ) |
469 | |
470 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
471 proc setupCommandBuffers(device: VkDevice, graphicsQueueFamily: uint32): ( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
472 VkCommandPool, array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]) = |
501 | 473 # set up command buffer |
474 var poolInfo = VkCommandPoolCreateInfo( | |
475 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
476 flags: VkCommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT), | |
477 queueFamilyIndex: graphicsQueueFamily, | |
478 ) | |
479 checkVkResult device.vkCreateCommandPool(addr(poolInfo), nil, addr(result[0])) | |
480 | |
481 var allocInfo = VkCommandBufferAllocateInfo( | |
482 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
483 commandPool: result[0], | |
484 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
485 commandBufferCount: result[1].len.uint32, | |
486 ) | |
487 checkVkResult device.vkAllocateCommandBuffers(addr(allocInfo), addr(result[1][0])) | |
488 | |
489 proc setupSyncPrimitives(device: VkDevice): ( | |
490 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore], | |
491 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore], | |
492 array[MAX_FRAMES_IN_FLIGHT, VkFence], | |
493 ) = | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
494 var semaphoreInfo = VkSemaphoreCreateInfo( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
495 sType: VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO) |
501 | 496 var fenceInfo = VkFenceCreateInfo( |
497 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, | |
498 flags: VkFenceCreateFlags(VK_FENCE_CREATE_SIGNALED_BIT) | |
499 ) | |
500 for i in 0 ..< MAX_FRAMES_IN_FLIGHT: | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
501 checkVkResult device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
502 result[0][i])) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
503 checkVkResult device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
504 result[1][i])) |
501 | 505 checkVkResult device.vkCreateFence(addr(fenceInfo), nil, addr(result[2][i])) |
506 | |
507 proc igniteEngine*(windowTitle: string): Engine = | |
508 | |
509 result.window = createWindow(windowTitle) | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
510 let mousepos = result.window.getMousePosition() |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
511 if mousepos.isSome(): |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
512 result.input.mousePos = mousePos.get() |
501 | 513 |
514 # setup vulkan functions | |
515 vkLoad1_0() | |
516 vkLoad1_1() | |
517 vkLoad1_2() | |
518 | |
519 # create vulkan instance | |
520 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION) | |
521 when DEBUG_LOG: | |
522 result.vulkan.debugMessenger = result.vulkan.instance.setupDebugLog() | |
523 result.vulkan.surface = result.vulkan.instance.createVulkanSurface(result.window) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
524 result.vulkan.device = result.vulkan.instance.setupVulkanDeviceAndQueues( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
525 result.vulkan.surface) |
501 | 526 |
527 # get basic frame information | |
528 result.vulkan.surfaceFormat = result.vulkan.device.physicalDevice.formats.getSuitableSurfaceFormat() | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
529 result.vulkan.frameSize = result.window.getFrameDimension( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
530 result.vulkan.device.physicalDevice.device, result.vulkan.surface) |
501 | 531 |
532 # setup swapchain and render pipeline | |
533 result.vulkan.swapchain = result.vulkan.device.device.setupSwapChain( | |
534 result.vulkan.device.physicalDevice, | |
535 result.vulkan.surface, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
536 result.vulkan.frameSize, |
501 | 537 result.vulkan.surfaceFormat |
538 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
539 result.vulkan.renderPass = result.vulkan.device.device.setupRenderPass( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
540 result.vulkan.surfaceFormat.format) |
501 | 541 result.vulkan.framebuffers = result.vulkan.device.device.setupFramebuffers( |
542 result.vulkan.swapchain, | |
543 result.vulkan.renderPass, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
544 result.vulkan.frameSize |
501 | 545 ) |
546 ( | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
547 result.vulkan.device.commandPool, |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
548 result.vulkan.device.commandBuffers, |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
549 ) = result.vulkan.device.device.setupCommandBuffers( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
550 result.vulkan.device.graphicsQueueFamily) |
501 | 551 |
552 ( | |
553 result.vulkan.imageAvailableSemaphores, | |
554 result.vulkan.renderFinishedSemaphores, | |
555 result.vulkan.inFlightFences, | |
556 ) = result.vulkan.device.device.setupSyncPrimitives() | |
557 | |
558 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
559 proc setupPipeline*[VertexType; UniformType; IndexType: uint16|uint32]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
560 engine: var Engine, scenedata: Thing, vertexShader, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
561 fragmentShader: static string): RenderPipeline[VertexType, UniformType] = |
501 | 562 engine.currentscenedata = scenedata |
563 result = initRenderPipeline[VertexType, UniformType]( | |
564 engine.vulkan.device.device, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
565 engine.vulkan.frameSize, |
501 | 566 engine.vulkan.renderPass, |
567 vertexShader, | |
568 fragmentShader, | |
569 ) | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
570 |
501 | 571 # vertex buffers |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
572 for mesh in allPartsOfType[Mesh[VertexType]](engine.currentscenedata): |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
573 result.vertexBuffers.add createVertexBuffers(mesh, result.device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
574 engine.vulkan.device.physicalDevice.device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
575 engine.vulkan.device.commandPool, engine.vulkan.device.graphicsQueue) |
501 | 576 |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
577 # vertex buffers with indexes |
501 | 578 when not (IndexType is void): |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
579 for mesh in allPartsOfType[IndexedMesh[VertexType, IndexType]]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
580 engine.currentscenedata): |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
581 result.indexedVertexBuffers.add createIndexedVertexBuffers(mesh, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
582 result.device, engine.vulkan.device.physicalDevice.device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
583 engine.vulkan.device.commandPool, engine.vulkan.device.graphicsQueue) |
501 | 584 |
585 # uniform buffers | |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
586 when not (UniformType is void): |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
587 result.uniformBuffers = createUniformBuffers[MAX_FRAMES_IN_FLIGHT, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
588 UniformType]( |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
589 result.device, |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
590 engine.vulkan.device.physicalDevice.device |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
591 ) |
501 | 592 |
593 var | |
594 poolSize = VkDescriptorPoolSize( | |
595 `type`: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, | |
596 descriptorCount: uint32(MAX_FRAMES_IN_FLIGHT), | |
597 ) | |
598 poolInfo = VkDescriptorPoolCreateInfo( | |
599 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
600 poolSizeCount: 1, | |
601 pPoolSizes: addr(poolSize), | |
602 maxSets: uint32(MAX_FRAMES_IN_FLIGHT), | |
603 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
604 checkVkResult vkCreateDescriptorPool(result.device, addr(poolInfo), nil, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
605 result.descriptorPool)) |
501 | 606 |
607 var layouts: array[MAX_FRAMES_IN_FLIGHT, VkDescriptorSetLayout] | |
608 for i in 0 ..< MAX_FRAMES_IN_FLIGHT: | |
609 layouts[i] = result.descriptorSetLayout | |
610 var allocInfo = VkDescriptorSetAllocateInfo( | |
611 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
612 descriptorPool: result.descriptorPool, | |
613 descriptorSetCount: uint32(MAX_FRAMES_IN_FLIGHT), | |
614 pSetLayouts: addr(layouts[0]), | |
615 ) | |
616 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
617 checkVkResult vkAllocateDescriptorSets(result.device, addr(allocInfo), addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
618 result.descriptors[0])) |
501 | 619 |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
620 when not (UniformType is void): |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
621 var bufferInfos: array[MAX_FRAMES_IN_FLIGHT, array[1, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
622 VkDescriptorBufferInfo]] # because we use only one Uniform atm |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
623 for i in 0 ..< MAX_FRAMES_IN_FLIGHT: |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
624 bufferInfos[i][0] = VkDescriptorBufferInfo( |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
625 buffer: result.uniformBuffers[i].vkBuffer, |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
626 offset: VkDeviceSize(0), |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
627 range: VkDeviceSize(sizeof(UniformType)), |
501 | 628 ) |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
629 var descriptorWrite = VkWriteDescriptorSet( |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
630 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
631 dstSet: result.descriptors[i], |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
632 dstBinding: 0, |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
633 dstArrayElement: 0, |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
634 descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
635 descriptorCount: 1, |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
636 pBufferInfo: cast[ptr ptr VkDescriptorBufferInfo](addr(bufferInfos[i][ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
637 0])), |
507
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
638 ) |
15e78601b390
add: some changes to build on windows host
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
639 vkUpdateDescriptorSets(result.device, 1, addr(descriptorWrite), 0, nil) |
501 | 640 |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
641 proc updateBufferData*[T](device: Device, buffer: Buffer, data: var T) = |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
642 when stripGenericParams(T) is seq: # seq needs special treatment for automated data uploading |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
643 assert data.len > 0 |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
644 let size = data.len * sizeof(get(genericParams(typeof(data)), 0)) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
645 let dataptr = addr(data[0]) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
646 else: |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
647 let size = sizeof(data) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
648 let dataptr = addr(data) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
649 if not (HostVisible in buffer.memoryProperties): |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
650 if not (TransferDst in buffer.bufferTypes): |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
651 raise newException(Exception, "Buffer cannot be updated") |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
652 var stagingBuffer = device.device.InitBuffer(device.physicalDevice.device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
653 uint64(size), {TransferSrc}, {HostVisible, HostCoherent}) |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
654 copyMem(stagingBuffer.data, dataptr, size) |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
655 transferBuffer(device.commandPool, device.graphicsQueue, stagingBuffer, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
656 buffer, uint64(size)) |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
657 stagingBuffer.trash() |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
658 else: |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
659 copyMem(buffer.data, dataptr, size) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
660 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
661 proc updateVertexData*[T: VertexAttribute](device: Device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
662 vertexAttribute: var T) = |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
663 device.updateBufferData(vertexAttribute.buffer, vertexAttribute.data) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
664 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
665 proc updateUniformData*[VertexType, Uniforms](device: Device, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
666 pipeline: RenderPipeline[VertexType, Uniforms], data: var Uniforms) = |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
667 for buffer in pipeline.uniformBuffers: |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
668 device.updateBufferData(buffer, data) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
669 |
501 | 670 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
671 proc runPipeline[VertexType; Uniforms](commandBuffer: VkCommandBuffer, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
672 pipeline: var RenderPipeline[VertexType, Uniforms], currentFrame: int) = |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
673 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
674 pipeline.pipeline) |
501 | 675 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
676 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
677 pipeline.layout, 0, 1, addr(pipeline.descriptors[currentFrame]), 0, nil) |
501 | 678 for (vertexBufferSet, vertexCount) in pipeline.vertexBuffers: |
679 var | |
680 vertexBuffers: seq[VkBuffer] | |
681 offsets: seq[VkDeviceSize] | |
682 for buffer in vertexBufferSet: | |
683 vertexBuffers.add buffer.vkBuffer | |
684 offsets.add VkDeviceSize(0) | |
685 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
686 vkCmdBindVertexBuffers(commandBuffer, firstBinding = 0'u32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
687 bindingCount = uint32(vertexBuffers.len), pBuffers = addr(vertexBuffers[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
688 0]), pOffsets = addr(offsets[0])) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
689 vkCmdDraw(commandBuffer, vertexCount = vertexCount, instanceCount = 1'u32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
690 firstVertex = 0'u32, firstInstance = 0'u32) |
501 | 691 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
692 for (vertexBufferSet, indexBuffer, indicesCount, indexType) in |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
693 pipeline.indexedVertexBuffers: |
501 | 694 var |
695 vertexBuffers: seq[VkBuffer] | |
696 offsets: seq[VkDeviceSize] | |
697 for buffer in vertexBufferSet: | |
698 vertexBuffers.add buffer.vkBuffer | |
699 offsets.add VkDeviceSize(0) | |
700 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
701 vkCmdBindVertexBuffers(commandBuffer, firstBinding = 0'u32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
702 bindingCount = uint32(vertexBuffers.len), pBuffers = addr(vertexBuffers[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
703 0]), pOffsets = addr(offsets[0])) |
501 | 704 vkCmdBindIndexBuffer(commandBuffer, indexBuffer.vkBuffer, VkDeviceSize(0), indexType) |
705 vkCmdDrawIndexed(commandBuffer, indicesCount, 1, 0, 0, 0) | |
706 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
707 proc recordCommandBuffer(renderPass: VkRenderPass, pipeline: var RenderPipeline, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
708 commandBuffer: VkCommandBuffer, framebuffer: VkFramebuffer, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
709 frameSize: TVec2[uint32], currentFrame: int) = |
501 | 710 var |
711 beginInfo = VkCommandBufferBeginInfo( | |
712 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
713 pInheritanceInfo: nil, | |
714 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
715 clearColor = VkClearValue(color: VkClearColorValue(float32: [0.2'f, 0.2'f, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
716 0.2'f, 1.0'f])) |
501 | 717 renderPassInfo = VkRenderPassBeginInfo( |
718 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
719 renderPass: renderPass, | |
720 framebuffer: framebuffer, | |
721 renderArea: VkRect2D( | |
722 offset: VkOffset2D(x: 0, y: 0), | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
723 extent: VkExtent2D(width: frameSize.x, height: frameSize.y), |
501 | 724 ), |
725 clearValueCount: 1, | |
726 pClearValues: addr(clearColor), | |
727 ) | |
728 viewport = VkViewport( | |
729 x: 0.0, | |
730 y: 0.0, | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
731 width: (float)frameSize.x, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
732 height: (float)frameSize.y, |
501 | 733 minDepth: 0.0, |
734 maxDepth: 1.0, | |
735 ) | |
736 scissor = VkRect2D( | |
737 offset: VkOffset2D(x: 0, y: 0), | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
738 extent: VkExtent2D(width: frameSize.x, height: frameSize.y) |
501 | 739 ) |
740 checkVkResult vkBeginCommandBuffer(commandBuffer, addr(beginInfo)) | |
741 block: | |
742 vkCmdBeginRenderPass(commandBuffer, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
743 vkCmdSetViewport(commandBuffer, firstViewport = 0, viewportCount = 1, addr(viewport)) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
744 vkCmdSetScissor(commandBuffer, firstScissor = 0, scissorCount = 1, addr(scissor)) |
501 | 745 runPipeline(commandBuffer, pipeline, currentFrame) |
746 vkCmdEndRenderPass(commandBuffer) | |
747 checkVkResult vkEndCommandBuffer(commandBuffer) | |
748 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
749 proc drawFrame(window: NativeWindow, vulkan: var Vulkan, currentFrame: int, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
750 resized: bool, pipeline: var RenderPipeline) = |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
751 checkVkResult vkWaitForFences(vulkan.device.device, 1, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
752 vulkan.inFlightFences[currentFrame]), VK_TRUE, high(uint64)) |
501 | 753 var bufferImageIndex: uint32 |
754 let nextImageResult = vkAcquireNextImageKHR( | |
755 vulkan.device.device, | |
756 vulkan.swapchain.swapchain, | |
757 high(uint64), | |
758 vulkan.imageAvailableSemaphores[currentFrame], | |
759 VkFence(0), | |
760 addr(bufferImageIndex) | |
761 ) | |
762 if nextImageResult == VK_ERROR_OUT_OF_DATE_KHR: | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
763 vulkan.frameSize = window.getFrameDimension( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
764 vulkan.device.physicalDevice.device, vulkan.surface) |
501 | 765 (vulkan.swapchain, vulkan.framebuffers) = vulkan.recreateSwapchain() |
766 elif not (nextImageResult in [VK_SUCCESS, VK_SUBOPTIMAL_KHR]): | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
767 raise newException(Exception, "Vulkan error: vkAcquireNextImageKHR returned " & |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
768 $nextImageResult) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
769 checkVkResult vkResetFences(vulkan.device.device, 1, addr( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
770 vulkan.inFlightFences[currentFrame])) |
501 | 771 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
772 checkVkResult vkResetCommandBuffer(vulkan.device.commandBuffers[currentFrame], |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
773 VkCommandBufferResetFlags(0)) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
774 vulkan.renderPass.recordCommandBuffer(pipeline, vulkan.device.commandBuffers[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
775 currentFrame], vulkan.framebuffers[bufferImageIndex], vulkan.frameSize, currentFrame) |
501 | 776 var |
777 waitSemaphores = [vulkan.imageAvailableSemaphores[currentFrame]] | |
778 waitStages = [VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)] | |
779 signalSemaphores = [vulkan.renderFinishedSemaphores[currentFrame]] | |
780 submitInfo = VkSubmitInfo( | |
781 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, | |
782 waitSemaphoreCount: 1, | |
783 pWaitSemaphores: addr(waitSemaphores[0]), | |
784 pWaitDstStageMask: addr(waitStages[0]), | |
785 commandBufferCount: 1, | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
786 pCommandBuffers: addr(vulkan.device.commandBuffers[currentFrame]), |
501 | 787 signalSemaphoreCount: 1, |
788 pSignalSemaphores: addr(signalSemaphores[0]), | |
789 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
790 checkVkResult vkQueueSubmit(vulkan.device.graphicsQueue, 1, addr(submitInfo), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
791 vulkan.inFlightFences[currentFrame]) |
501 | 792 |
793 var presentInfo = VkPresentInfoKHR( | |
794 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, | |
795 waitSemaphoreCount: 1, | |
796 pWaitSemaphores: addr(signalSemaphores[0]), | |
797 swapchainCount: 1, | |
798 pSwapchains: addr(vulkan.swapchain.swapchain), | |
799 pImageIndices: addr(bufferImageIndex), | |
800 pResults: nil, | |
801 ) | |
802 let presentResult = vkQueuePresentKHR(vulkan.device.presentationQueue, addr(presentInfo)) | |
803 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
804 if presentResult == VK_ERROR_OUT_OF_DATE_KHR or presentResult == |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
805 VK_SUBOPTIMAL_KHR or resized: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
806 vulkan.frameSize = window.getFrameDimension( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
807 vulkan.device.physicalDevice.device, vulkan.surface) |
501 | 808 (vulkan.swapchain, vulkan.framebuffers) = vulkan.recreateSwapchain() |
809 | |
810 | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
811 proc run*(engine: var Engine, pipeline: var RenderPipeline, globalUpdate: proc( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
812 engine: var Engine, dt: float32)) = |
501 | 813 var |
814 currentFrame = 0 | |
815 resized = false | |
816 lastUpdate = getTime() | |
817 | |
512
9fd6a78c94a2
fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents:
507
diff
changeset
|
818 while true: |
501 | 819 # process input |
820 engine.input.keysPressed = {} | |
821 engine.input.keysReleased = {} | |
822 engine.input.mousePressed = {} | |
823 engine.input.mouseReleased = {} | |
512
9fd6a78c94a2
fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents:
507
diff
changeset
|
824 var killed = false |
501 | 825 for event in engine.window.pendingEvents(): |
826 case event.eventType: | |
827 of Quit: | |
828 killed = true | |
829 of ResizedWindow: | |
830 resized = true | |
831 of KeyPressed: | |
832 engine.input.keysPressed.incl event.key | |
833 engine.input.keysDown.incl event.key | |
834 of KeyReleased: | |
835 engine.input.keysReleased.incl event.key | |
836 engine.input.keysDown.excl event.key | |
837 of MousePressed: | |
838 engine.input.mousePressed.incl event.button | |
839 engine.input.mouseDown.incl event.button | |
840 of MouseReleased: | |
841 engine.input.mouseReleased.incl event.button | |
842 engine.input.mouseDown.excl event.button | |
843 of MouseMoved: | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
844 engine.input.mousePos = Vec2([float32(event.x), float32(event.y)]) |
512
9fd6a78c94a2
fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents:
507
diff
changeset
|
845 if killed: # at least on windows we should return immediately as swapchain recreation will fail after kill |
9fd6a78c94a2
fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents:
507
diff
changeset
|
846 break |
501 | 847 |
848 # game logic update | |
849 let | |
850 now = getTime() | |
851 dt = float32(float64((now - lastUpdate).inNanoseconds) / 1_000_000_000'f64) | |
852 lastUpdate = now | |
853 engine.globalUpdate(dt) | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
854 for thing in allThings(engine.currentscenedata): |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
855 for part in thing.parts: |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
856 part.update(dt) |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
513
diff
changeset
|
857 thing.update(dt) |
501 | 858 |
859 # submit frame for drawing | |
860 engine.window.drawFrame(engine.vulkan, currentFrame, resized, pipeline) | |
861 resized = false | |
862 currentFrame = (currentFrame + 1) mod MAX_FRAMES_IN_FLIGHT | |
863 checkVkResult vkDeviceWaitIdle(engine.vulkan.device.device) | |
864 | |
865 proc trash*(pipeline: var RenderPipeline) = | |
866 vkDestroyDescriptorPool(pipeline.device, pipeline.descriptorPool, nil) | |
867 vkDestroyDescriptorSetLayout(pipeline.device, pipeline.descriptorSetLayout, nil) | |
868 vkDestroyPipeline(pipeline.device, pipeline.pipeline, nil) | |
869 vkDestroyPipelineLayout(pipeline.device, pipeline.layout, nil) | |
870 for shader in pipeline.shaders: | |
871 vkDestroyShaderModule(pipeline.device, shader.shader.module, nil) | |
872 | |
873 for (bufferset, cnt) in pipeline.vertexBuffers.mitems: | |
874 for buffer in bufferset.mitems: | |
875 buffer.trash() | |
876 for (bufferset, indexbuffer, cnt, t) in pipeline.indexedVertexBuffers.mitems: | |
877 indexbuffer.trash() | |
878 for buffer in bufferset.mitems: | |
879 buffer.trash() | |
880 for buffer in pipeline.uniformBuffers.mitems: | |
881 buffer.trash() | |
882 | |
883 proc trash*(engine: var Engine) = | |
884 checkVkResult vkDeviceWaitIdle(engine.vulkan.device.device) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
885 engine.vulkan.device.device.trash(engine.vulkan.swapchain, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
886 engine.vulkan.framebuffers) |
501 | 887 |
888 for i in 0 ..< MAX_FRAMES_IN_FLIGHT: | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
889 engine.vulkan.device.device.vkDestroySemaphore( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
890 engine.vulkan.imageAvailableSemaphores[i], nil) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
891 engine.vulkan.device.device.vkDestroySemaphore( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
892 engine.vulkan.renderFinishedSemaphores[i], nil) |
501 | 893 engine.vulkan.device.device.vkDestroyFence(engine.vulkan.inFlightFences[i], nil) |
894 | |
895 engine.vulkan.device.device.vkDestroyRenderPass(engine.vulkan.renderPass, nil) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
896 engine.vulkan.device.device.vkDestroyCommandPool( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
897 engine.vulkan.device.commandPool, nil) |
501 | 898 |
899 engine.vulkan.instance.vkDestroySurfaceKHR(engine.vulkan.surface, nil) | |
900 engine.vulkan.device.device.vkDestroyDevice(nil) | |
901 when DEBUG_LOG: | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
902 engine.vulkan.instance.vkDestroyDebugUtilsMessengerEXT( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
903 engine.vulkan.debugMessenger, nil) |
501 | 904 engine.window.trash() |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
905 engine.vulkan.instance.vkDestroyInstance( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
521
diff
changeset
|
906 nil) # needs to happen after window is trashed as the driver might have a hook registered for the window destruction |