Mercurial > games > semicongine
annotate src/engine.nim @ 17:b40466fa446a
add: vertex basics, some refactoring
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 01 Jan 2023 01:00:50 +0700 |
parents | 3415afff1d1a |
children | 90e117952f74 |
rev | line source |
---|---|
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
1 import std/typetraits |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
2 import std/strformat |
0 | 3 import std/enumerate |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
4 import std/logging |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
5 |
0 | 6 |
7 import ./vulkan | |
8 import ./vulkan_helpers | |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
9 import ./window |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
10 import ./events |
17 | 11 import ./math/vector |
12 import ./shader | |
13 import ./vertex | |
0 | 14 |
15 import ./glslang/glslang | |
16 | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
17 const MAX_FRAMES_IN_FLIGHT = 2 |
10 | 18 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
|
19 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
20 var logger = newConsoleLogger() |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
21 addHandler(logger) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
22 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
23 |
17 | 24 type |
25 MyVertex = object | |
26 position: VertexAttribute[Vec2[float32]] | |
27 color: VertexAttribute[Vec3[float32]] | |
28 | |
29 const vertices = [ | |
30 (Vec2([ 0.0'f32, -0.5'f32]), Vec3([1.0'f32, 0.0'f32, 0.0'f32])), | |
31 (Vec2([ 0.5'f32, 0.5'f32]), Vec3([0.0'f32, 1.0'f32, 0.0'f32])), | |
32 (Vec2([-0.5'f32, 0.5'f32]), Vec3([0.0'f32, 0.0'f32, 1.0'f32])) | |
33 ] | |
34 | |
35 | |
36 proc getBindingDescription(binding: int): auto = | |
37 VkVertexInputBindingDescription( | |
38 binding: uint32(binding), | |
39 stride: uint32(sizeof(vertices[0])), | |
40 inputRate: VK_VERTEX_INPUT_RATE_VERTEX, # VK_VERTEX_INPUT_RATE_INSTANCE for instances | |
41 ) | |
42 | |
43 proc getAttributeDescriptions(binding: int): auto = | |
44 [ | |
45 VkVertexInputAttributeDescription( | |
46 binding: 0'u32, | |
47 location: 0, | |
48 format: VK_FORMAT_R32G32_SFLOAT, | |
49 offset: 0, | |
50 ), | |
51 VkVertexInputAttributeDescription( | |
52 binding: 0'u32, | |
53 location: 1, | |
54 format: VK_FORMAT_R32G32B32_SFLOAT, | |
55 offset: uint32(sizeof(Vec2)), # use offsetOf? | |
56 ), | |
57 ] | |
58 | |
59 var vertexShaderCode = """ | |
60 #version 450 | |
61 | |
62 layout(location = 0) in vec2 inPosition; | |
63 layout(location = 1) in vec3 inColor; | |
64 | |
1 | 65 layout(location = 0) out vec3 fragColor; |
17 | 66 |
1 | 67 void main() { |
17 | 68 gl_Position = vec4(inPosition, 0.0, 1.0); |
69 fragColor = inColor; | |
70 } | |
71 """ | |
1 | 72 |
17 | 73 var fragmentShaderCode = """#version 450 |
1 | 74 layout(location = 0) out vec4 outColor; |
75 layout(location = 0) in vec3 fragColor; | |
76 void main() { | |
77 outColor = vec4(fragColor, 1.0); | |
78 }""" | |
0 | 79 |
80 const VULKAN_VERSION = VK_MAKE_API_VERSION(0'u32, 1'u32, 2'u32, 0'u32) | |
81 | |
82 type | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
83 Device = object |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
84 physicalDevice: PhysicalDevice |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
85 graphicsQueueFamily: uint32 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
86 presentationQueueFamily: uint32 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
87 device: VkDevice |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
88 graphicsQueue: VkQueue |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
89 presentationQueue: VkQueue |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
90 Swapchain = object |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
91 swapchain: VkSwapchainKHR |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
92 images: seq[VkImage] |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
93 imageviews: seq[VkImageView] |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
94 RenderPipeline = object |
17 | 95 shaders*: seq[ShaderProgram] |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
96 layout*: VkPipelineLayout |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
97 pipeline*: VkPipeline |
0 | 98 QueueFamily = object |
99 properties*: VkQueueFamilyProperties | |
100 hasSurfaceSupport*: bool | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
101 PhysicalDevice = object |
0 | 102 device*: VkPhysicalDevice |
103 extensions*: seq[string] | |
104 properties*: VkPhysicalDeviceProperties | |
105 features*: VkPhysicalDeviceFeatures | |
106 queueFamilies*: seq[QueueFamily] | |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
107 formats: seq[VkSurfaceFormatKHR] |
0 | 108 presentModes: seq[VkPresentModeKHR] |
109 Vulkan* = object | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
110 debugMessenger: VkDebugUtilsMessengerEXT |
0 | 111 instance*: VkInstance |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
112 deviceList*: seq[PhysicalDevice] |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
113 device*: Device |
0 | 114 surface*: VkSurfaceKHR |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
115 surfaceFormat: VkSurfaceFormatKHR |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
116 frameDimension: VkExtent2D |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
117 swapchain: Swapchain |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
118 framebuffers: seq[VkFramebuffer] |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
119 renderPass*: VkRenderPass |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
120 pipeline*: RenderPipeline |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
121 commandPool*: VkCommandPool |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
122 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
|
123 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
|
124 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
|
125 inFlightFences*: array[MAX_FRAMES_IN_FLIGHT, VkFence] |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
126 Engine* = object |
0 | 127 vulkan*: Vulkan |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
128 window: NativeWindow |
0 | 129 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
130 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhysicalDevice] = |
0 | 131 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance): |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
132 var device = PhysicalDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice)) |
0 | 133 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties)) |
134 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
|
135 device.formats = vulkanPhysicalDevice.getDeviceSurfaceFormats(surface) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
136 device.presentModes = vulkanPhysicalDevice.getDeviceSurfacePresentModes(surface) |
0 | 137 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
138 debug(&"Physical device nr {int(vulkanPhysicalDevice)} {cleanString(device.properties.deviceName)}") |
0 | 139 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)): |
1 | 140 var hasSurfaceSupport: VkBool32 = VK_FALSE |
0 | 141 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport)) |
142 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
|
143 debug(&" Queue family {i} {queueFamilyProperty}") |
0 | 144 |
145 result.add(device) | |
146 | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
147 proc filterForDevice(devices: seq[PhysicalDevice]): seq[(PhysicalDevice, uint32, uint32)] = |
0 | 148 for device in devices: |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
149 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
|
150 continue |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
151 var graphicsQueueFamily = high(uint32) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
152 var presentationQueueFamily = high(uint32) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
153 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
|
154 if queueFamily.hasSurfaceSupport: |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
155 presentationQueueFamily = uint32(i) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
156 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
|
157 graphicsQueueFamily = uint32(i) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
158 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
|
159 result.add((device, graphicsQueueFamily, presentationQueueFamily)) |
0 | 160 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
161 for (device, graphicsQueueFamily, presentationQueueFamily) in result: |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
162 debug(&"Viable device: {cleanString(device.properties.deviceName)} (graphics queue family {graphicsQueueFamily}, presentation queue family {presentationQueueFamily})") |
0 | 163 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
164 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
165 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
|
166 let capabilities = device.getSurfaceCapabilities(surface) |
0 | 167 if capabilities.currentExtent.width != high(uint32): |
168 return capabilities.currentExtent | |
169 else: | |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
170 let (width, height) = window.size() |
0 | 171 return VkExtent2D( |
172 width: min(max(uint32(width), capabilities.minImageExtent.width), capabilities.maxImageExtent.width), | |
173 height: min(max(uint32(height), capabilities.minImageExtent.height), capabilities.maxImageExtent.height), | |
174 ) | |
175 | |
10 | 176 when DEBUG_LOG: |
8 | 177 proc setupDebugLog(instance: VkInstance): VkDebugUtilsMessengerEXT = |
178 var createInfo = VkDebugUtilsMessengerCreateInfoEXT( | |
179 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, | |
180 messageSeverity: VkDebugUtilsMessageSeverityFlagsEXT( | |
181 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) or | |
182 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) or | |
183 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) | |
184 ), | |
185 messageType: VkDebugUtilsMessageTypeFlagsEXT( | |
186 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) or | |
187 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) or | |
188 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) | |
189 ), | |
190 pfnUserCallback: debugCallback, | |
191 pUserData: nil, | |
192 ) | |
193 checkVkResult instance.vkCreateDebugUtilsMessengerEXT(addr(createInfo), nil, addr(result)) | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
194 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
195 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
|
196 let usableDevices = instance.getAllPhysicalDevices(surface).filterForDevice() |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
197 if len(usableDevices) == 0: |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
198 raise newException(Exception, "No suitable graphics device found") |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
199 result.physicalDevice = usableDevices[0][0] |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
200 result.graphicsQueueFamily = usableDevices[0][1] |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
201 result.presentationQueueFamily = usableDevices[0][2] |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
202 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
203 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
|
204 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
205 (result.device, result.graphicsQueue, result.presentationQueue) = getVulcanDevice( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
206 result.physicalDevice.device, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
207 result.physicalDevice.features, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
208 result.graphicsQueueFamily, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
209 result.presentationQueueFamily, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
210 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
211 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
212 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
|
213 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
214 let capabilities = physicalDevice.device.getSurfaceCapabilities(surface) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
215 var selectedPresentationMode = getPresentMode(physicalDevice.presentModes) |
0 | 216 # setup swapchain |
217 var swapchainCreateInfo = VkSwapchainCreateInfoKHR( | |
218 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
219 surface: surface, |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
220 minImageCount: max(capabilities.minImageCount + 1, capabilities.maxImageCount), |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
221 imageFormat: surfaceFormat.format, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
222 imageColorSpace: surfaceFormat.colorSpace, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
223 imageExtent: dimension, |
0 | 224 imageArrayLayers: 1, |
225 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), | |
226 # VK_SHARING_MODE_CONCURRENT no supported (i.e cannot use different queue families for drawing to swap surface?) | |
227 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
|
228 preTransform: capabilities.currentTransform, |
0 | 229 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
230 presentMode: selectedPresentationMode, |
1 | 231 clipped: VK_TRUE, |
0 | 232 oldSwapchain: VkSwapchainKHR(0), |
233 ) | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
234 checkVkResult device.vkCreateSwapchainKHR(addr(swapchainCreateInfo), nil, addr(result.swapchain)) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
235 result.images = device.getSwapChainImages(result.swapchain) |
0 | 236 |
237 # setup swapchian image views | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
238 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
239 result.imageviews = newSeq[VkImageView](result.images.len) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
240 for i, image in enumerate(result.images): |
0 | 241 var imageViewCreateInfo = VkImageViewCreateInfo( |
242 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, | |
243 image: image, | |
244 viewType: VK_IMAGE_VIEW_TYPE_2D, | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
245 format: surfaceFormat.format, |
0 | 246 components: VkComponentMapping( |
247 r: VK_COMPONENT_SWIZZLE_IDENTITY, | |
248 g: VK_COMPONENT_SWIZZLE_IDENTITY, | |
249 b: VK_COMPONENT_SWIZZLE_IDENTITY, | |
250 a: VK_COMPONENT_SWIZZLE_IDENTITY, | |
251 ), | |
252 subresourceRange: VkImageSubresourceRange( | |
253 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT), | |
254 baseMipLevel: 0, | |
255 levelCount: 1, | |
256 baseArrayLayer: 0, | |
257 layerCount: 1, | |
258 ), | |
259 ) | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
260 checkVkResult device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr(result.imageviews[i])) |
1 | 261 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
262 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
|
263 var |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
264 colorAttachment = VkAttachmentDescription( |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
265 format: format, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
266 samples: VK_SAMPLE_COUNT_1_BIT, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
267 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
268 storeOp: VK_ATTACHMENT_STORE_OP_STORE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
269 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
|
270 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
|
271 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
272 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
|
273 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
274 colorAttachmentRef = VkAttachmentReference( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
275 attachment: 0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
276 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
|
277 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
278 subpass = VkSubpassDescription( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
279 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
280 colorAttachmentCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
281 pColorAttachments: addr(colorAttachmentRef) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
282 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
283 dependency = VkSubpassDependency( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
284 srcSubpass: VK_SUBPASS_EXTERNAL, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
285 dstSubpass: 0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
286 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
|
287 srcAccessMask: VkAccessFlags(0), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
288 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
|
289 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
|
290 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
291 renderPassCreateInfo = VkRenderPassCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
292 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
|
293 attachmentCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
294 pAttachments: addr(colorAttachment), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
295 subpassCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
296 pSubpasses: addr(subpass), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
297 dependencyCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
298 pDependencies: addr(dependency), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
299 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
300 checkVkResult device.vkCreateRenderPass(addr(renderPassCreateInfo), nil, addr(result)) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
301 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
302 proc setupRenderPipeline(device: VkDevice, frameDimension: VkExtent2D, renderPass: VkRenderPass): RenderPipeline = |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
303 # load shaders |
17 | 304 result.shaders.add(device.initShaderProgram(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderCode)) |
305 result.shaders.add(device.initShaderProgram(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderCode)) | |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
306 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
307 var |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
308 # 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
|
309 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
|
310 dynamicState = VkPipelineDynamicStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
311 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
|
312 dynamicStateCount: uint32(dynamicStates.len), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
313 pDynamicStates: addr(dynamicStates[0]), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
314 ) |
17 | 315 vertexbindings = generateInputVertexBinding[MyVertex]() |
316 attributebindings = generateInputAttributeBinding[MyVertex]() | |
317 | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
318 # define input data format |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
319 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
320 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, |
17 | 321 vertexBindingDescriptionCount: uint32(vertexbindings.len), |
322 pVertexBindingDescriptions: addr(vertexbindings[0]), | |
323 vertexAttributeDescriptionCount: uint32(attributebindings.len), | |
324 pVertexAttributeDescriptions: addr(attributebindings[0]), | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
325 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
326 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
327 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
|
328 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
329 primitiveRestartEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
330 ) |
1 | 331 |
332 # setup viewport | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
333 var viewportState = VkPipelineViewportStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
334 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
|
335 viewportCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
336 scissorCount: 1, |
1 | 337 ) |
0 | 338 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
339 # rasterizerization config |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
340 var |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
341 rasterizer = VkPipelineRasterizationStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
342 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
|
343 depthClampEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
344 rasterizerDiscardEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
345 polygonMode: VK_POLYGON_MODE_FILL, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
346 lineWidth: 1.0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
347 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
|
348 frontFace: VK_FRONT_FACE_CLOCKWISE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
349 depthBiasEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
350 depthBiasConstantFactor: 0.0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
351 depthBiasClamp: 0.0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
352 depthBiasSlopeFactor: 0.0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
353 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
354 multisampling = VkPipelineMultisampleStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
355 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
|
356 sampleShadingEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
357 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
358 minSampleShading: 1.0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
359 pSampleMask: nil, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
360 alphaToCoverageEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
361 alphaToOneEnable: VK_FALSE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
362 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
363 colorBlendAttachment = VkPipelineColorBlendAttachmentState( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
364 colorWriteMask: VkColorComponentFlags( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
365 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
|
366 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
|
367 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
|
368 ord(VK_COLOR_COMPONENT_A_BIT) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
369 ), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
370 blendEnable: VK_TRUE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
371 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
372 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
|
373 colorBlendOp: VK_BLEND_OP_ADD, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
374 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
375 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
376 alphaBlendOp: VK_BLEND_OP_ADD, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
377 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
378 colorBlending = VkPipelineColorBlendStateCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
379 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
|
380 logicOpEnable: VK_TRUE, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
381 logicOp: VK_LOGIC_OP_COPY, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
382 attachmentCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
383 pAttachments: addr(colorBlendAttachment), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
384 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
|
385 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
386 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
387 # create pipeline |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
388 pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
389 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
390 setLayoutCount: 0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
391 pSetLayouts: nil, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
392 pushConstantRangeCount: 0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
393 pPushConstantRanges: nil, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
394 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
395 checkVkResult device.vkCreatePipelineLayout(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
|
396 |
17 | 397 var stages: seq[VkPipelineShaderStageCreateInfo] |
398 for shader in result.shaders: | |
399 stages.add(shader.shader) | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
400 var pipelineInfo = VkGraphicsPipelineCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
401 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, |
17 | 402 stageCount: uint32(stages.len), |
403 pStages: addr(stages[0]), | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
404 pVertexInputState: addr(vertexInputInfo), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
405 pInputAssemblyState: addr(inputAssembly), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
406 pViewportState: addr(viewportState), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
407 pRasterizationState: addr(rasterizer), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
408 pMultisampleState: addr(multisampling), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
409 pDepthStencilState: nil, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
410 pColorBlendState: addr(colorBlending), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
411 pDynamicState: addr(dynamicState), |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
412 layout: result.layout, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
413 renderPass: renderPass, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
414 subpass: 0, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
415 basePipelineHandle: VkPipeline(0), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
416 basePipelineIndex: -1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
417 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
418 checkVkResult device.vkCreateGraphicsPipelines( |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
419 VkPipelineCache(0), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
420 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
421 addr(pipelineInfo), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
422 nil, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
423 addr(result.pipeline) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
424 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
425 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
426 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
|
427 result = newSeq[VkFramebuffer](swapchain.images.len) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
428 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
|
429 var framebufferInfo = VkFramebufferCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
430 sType: VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
431 renderPass: renderPass, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
432 attachmentCount: 1, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
433 pAttachments: addr(swapchain.imageviews[i]), |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
434 width: dimension.width, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
435 height: dimension.height, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
436 layers: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
437 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
438 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
|
439 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
440 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
|
441 for framebuffer in framebuffers: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
442 device.vkDestroyFramebuffer(framebuffer, nil) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
443 for imageview in swapchain.imageviews: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
444 device.vkDestroyImageView(imageview, nil) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
445 device.vkDestroySwapchainKHR(swapchain.swapchain, nil) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
446 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
447 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
|
448 debug(&"Recreate swapchain with dimension {vulkan.frameDimension}") |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
449 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
|
450 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
451 vulkan.device.device.trash(vulkan.swapchain, vulkan.framebuffers) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
452 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
453 result[0] = vulkan.device.device.setupSwapChain( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
454 vulkan.device.physicalDevice, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
455 vulkan.surface, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
456 vulkan.frameDimension, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
457 vulkan.surfaceFormat |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
458 ) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
459 result[1] = vulkan.device.device.setupFramebuffers( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
460 result[0], |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
461 vulkan.renderPass, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
462 vulkan.frameDimension |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
463 ) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
464 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
465 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
466 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
|
467 # set up command buffer |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
468 var poolInfo = VkCommandPoolCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
469 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
|
470 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
|
471 queueFamilyIndex: graphicsQueueFamily, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
472 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
473 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
|
474 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
475 var allocInfo = VkCommandBufferAllocateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
476 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
477 commandPool: result[0], |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
478 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
479 commandBufferCount: result[1].len.uint32, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
480 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
481 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
|
482 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
483 proc setupSyncPrimitives(device: VkDevice): ( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
484 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore], |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
485 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore], |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
486 array[MAX_FRAMES_IN_FLIGHT, VkFence], |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
487 ) = |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
488 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
|
489 var fenceInfo = VkFenceCreateInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
490 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
|
491 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
|
492 ) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
493 for i in 0 ..< MAX_FRAMES_IN_FLIGHT: |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
494 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
|
495 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
|
496 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
|
497 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
498 proc igniteEngine*(): Engine = |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
499 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
500 result.window = createWindow("Hello triangle") |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
501 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
502 # setup vulkan functions |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
503 vkLoad1_0() |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
504 vkLoad1_1() |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
505 vkLoad1_2() |
10 | 506 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
507 checkGlslangResult glslang_initialize_process() |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
508 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
509 # create vulkan instance |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
510 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION) |
10 | 511 when DEBUG_LOG: |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
512 result.vulkan.debugMessenger = result.vulkan.instance.setupDebugLog() |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
513 result.vulkan.surface = result.vulkan.instance.createVulkanSurface(result.window) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
514 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
|
515 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
516 # get basic frame information |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
517 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
|
518 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
|
519 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
520 # setup swapchain and render pipeline |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
521 result.vulkan.swapchain = result.vulkan.device.device.setupSwapChain( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
522 result.vulkan.device.physicalDevice, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
523 result.vulkan.surface, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
524 result.vulkan.frameDimension, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
525 result.vulkan.surfaceFormat |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
526 ) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
527 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
|
528 result.vulkan.pipeline = result.vulkan.device.device.setupRenderPipeline(result.vulkan.frameDimension, result.vulkan.renderPass) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
529 result.vulkan.framebuffers = result.vulkan.device.device.setupFramebuffers( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
530 result.vulkan.swapchain, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
531 result.vulkan.renderPass, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
532 result.vulkan.frameDimension |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
533 ) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
534 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
535 ( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
536 result.vulkan.commandPool, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
537 result.vulkan.commandBuffers, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
538 ) = result.vulkan.device.device.setupCommandBuffers(result.vulkan.device.graphicsQueueFamily) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
539 |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
540 ( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
541 result.vulkan.imageAvailableSemaphores, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
542 result.vulkan.renderFinishedSemaphores, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
543 result.vulkan.inFlightFences, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
544 ) = result.vulkan.device.device.setupSyncPrimitives() |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
545 |
0 | 546 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
547 proc recordCommandBuffer(renderPass: VkRenderPass, pipeline: VkPipeline, commandBuffer: VkCommandBuffer, framebuffer: VkFramebuffer, frameDimension: VkExtent2D) = |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
548 var |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
549 beginInfo = VkCommandBufferBeginInfo( |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
550 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
|
551 pInheritanceInfo: nil, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
552 ) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
553 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
|
554 renderPassInfo = VkRenderPassBeginInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
555 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
556 renderPass: renderPass, |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
557 framebuffer: framebuffer, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
558 renderArea: VkRect2D( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
559 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
|
560 extent: frameDimension, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
561 ), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
562 clearValueCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
563 pClearValues: addr(clearColor), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
564 ) |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
565 viewport = VkViewport( |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
566 x: 0.0, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
567 y: 0.0, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
568 width: (float) frameDimension.width, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
569 height: (float) frameDimension.height, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
570 minDepth: 0.0, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
571 maxDepth: 1.0, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
572 ) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
573 scissor = VkRect2D( |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
574 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
|
575 extent: frameDimension |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
576 ) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
577 checkVkResult commandBuffer.vkBeginCommandBuffer(addr(beginInfo)) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
578 commandBuffer.vkCmdBeginRenderPass(addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
579 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
580 commandBuffer.vkCmdSetViewport(firstViewport=0, viewportCount=1, addr(viewport)) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
581 commandBuffer.vkCmdSetScissor(firstScissor=0, scissorCount=1, addr(scissor)) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
582 commandBuffer.vkCmdDraw(vertexCount=3, instanceCount=1, firstVertex=0, firstInstance=0) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
583 commandBuffer.vkCmdEndRenderPass() |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
584 checkVkResult commandBuffer.vkEndCommandBuffer() |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
585 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
586 proc drawFrame(window: NativeWindow, vulkan: var Vulkan, currentFrame: int, resized: bool) = |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
587 checkVkResult vulkan.device.device.vkWaitForFences(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
|
588 var bufferImageIndex: uint32 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
589 let nextImageResult = vulkan.device.device.vkAcquireNextImageKHR( |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
590 vulkan.swapchain.swapchain, |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
591 high(uint64), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
592 vulkan.imageAvailableSemaphores[currentFrame], |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
593 VkFence(0), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
594 addr(bufferImageIndex) |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
595 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
596 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
|
597 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
|
598 (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
|
599 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
|
600 raise newException(Exception, "Vulkan error: vkAcquireNextImageKHR returned " & $nextImageResult) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
601 checkVkResult vulkan.device.device.vkResetFences(1, addr(vulkan.inFlightFences[currentFrame])) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
602 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
603 checkVkResult vulkan.commandBuffers[currentFrame].vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
604 vulkan.renderPass.recordCommandBuffer(vulkan.pipeline.pipeline, vulkan.commandBuffers[currentFrame], vulkan.framebuffers[bufferImageIndex], vulkan.frameDimension) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
605 var |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
606 waitSemaphores = [vulkan.imageAvailableSemaphores[currentFrame]] |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
607 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
|
608 signalSemaphores = [vulkan.renderFinishedSemaphores[currentFrame]] |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
609 submitInfo = VkSubmitInfo( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
610 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
611 waitSemaphoreCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
612 pWaitSemaphores: addr(waitSemaphores[0]), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
613 pWaitDstStageMask: addr(waitStages[0]), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
614 commandBufferCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
615 pCommandBuffers: addr(vulkan.commandBuffers[currentFrame]), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
616 signalSemaphoreCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
617 pSignalSemaphores: addr(signalSemaphores[0]), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
618 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
619 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
|
620 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
621 var presentInfo = VkPresentInfoKHR( |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
622 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
|
623 waitSemaphoreCount: 1, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
624 pWaitSemaphores: addr(signalSemaphores[0]), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
625 swapchainCount: 1, |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
626 pSwapchains: addr(vulkan.swapchain.swapchain), |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
627 pImageIndices: addr(bufferImageIndex), |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
628 pResults: nil, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
629 ) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
630 let presentResult = vkQueuePresentKHR(vulkan.device.presentationQueue, addr(presentInfo)) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
631 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
632 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
|
633 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
|
634 (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
|
635 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
636 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
637 proc fullThrottle*(engine: var Engine) = |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
638 var |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
639 killed = false |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
640 currentFrame = 0 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
641 resized = false |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
642 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
643 while not killed: |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
644 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
|
645 case event.eventType: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
646 of Quit: |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
647 killed = true |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
648 of ResizedWindow: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
649 resized = true |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
650 of KeyDown: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
651 echo event |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
652 if event.key == Escape: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
653 killed = true |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
654 else: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
655 discard |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
656 engine.window.drawFrame(engine.vulkan, currentFrame, resized) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
657 resized = false |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
658 currentFrame = (currentFrame + 1) mod MAX_FRAMES_IN_FLIGHT; |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
659 checkVkResult engine.vulkan.device.device.vkDeviceWaitIdle() |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
660 |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
661 proc trash*(engine: Engine) = |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
662 engine.vulkan.device.device.trash(engine.vulkan.swapchain, engine.vulkan.framebuffers) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
4
diff
changeset
|
663 checkVkResult engine.vulkan.device.device.vkDeviceWaitIdle() |
0 | 664 |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
665 for i in 0 ..< MAX_FRAMES_IN_FLIGHT: |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
666 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
|
667 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
|
668 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
|
669 |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
670 engine.vulkan.device.device.vkDestroyCommandPool(engine.vulkan.commandPool, nil) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
671 engine.vulkan.device.device.vkDestroyPipeline(engine.vulkan.pipeline.pipeline, nil) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
672 engine.vulkan.device.device.vkDestroyPipelineLayout(engine.vulkan.pipeline.layout, nil) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
673 engine.vulkan.device.device.vkDestroyRenderPass(engine.vulkan.renderPass, nil) |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
674 |
17 | 675 for shader in engine.vulkan.pipeline.shaders: |
676 engine.vulkan.device.device.vkDestroyShaderModule(shader.shader.module, nil) | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
677 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
678 engine.vulkan.instance.vkDestroySurfaceKHR(engine.vulkan.surface, nil) |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
679 engine.vulkan.device.device.vkDestroyDevice(nil) |
10 | 680 when DEBUG_LOG: |
4
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
681 engine.vulkan.instance.vkDestroyDebugUtilsMessengerEXT(engine.vulkan.debugMessenger, nil) |
af9183acb173
did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents:
2
diff
changeset
|
682 glslang_finalize_process() |
9 | 683 engine.window.trash() |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
1
diff
changeset
|
684 engine.vulkan.instance.vkDestroyInstance(nil) |