annotate src/engine.nim @ 9:227e53d1a853

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