annotate src/engine.nim @ 466:1dd9e2393a9e

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