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