annotate src/vulkan_helpers.nim @ 463:91544fc1afe5

did: hello world triangle, a bit of code organization
author Sam <sam@basx.dev>
date Mon, 19 Dec 2022 10:41:20 +0700
parents 0bc8643cfe25
children af9183acb173
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
1 import std/tables
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
3 import std/strformat
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
4 import std/logging
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
5
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
6 import ./glslang/glslang
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
7 import ./vulkan
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
8
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9 when defined(release):
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
10 const ENABLEVULKANVALIDATIONLAYERS* = false
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11 else:
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
12 const ENABLEVULKANVALIDATIONLAYERS* = true
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
13
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
14
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15 template checkVkResult*(call: untyped) =
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
16 when defined(release):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
17 discard call
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
18 else:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
19 let value = call
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
20 debug(&"CALLING vulkan: {astToStr(call)}")
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
21 if value != VK_SUCCESS:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
22 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value)
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
23
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
24
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
25 proc VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
26 (variant shl 29) or (major shl 22) or (minor shl 12) or patch
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
27
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
28
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
29 proc filterForSurfaceFormat*(formats: seq[VkSurfaceFormatKHR]): seq[VkSurfaceFormatKHR] =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
30 for format in formats:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
31 if format.format == VK_FORMAT_B8G8R8A8_SRGB and format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
32 result.add(format)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
33
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
34
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
35 proc cleanString*(str: openArray[char]): string =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
36 for i in 0 ..< len(str):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
37 if str[i] == char(0):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
38 result = join(str[0 ..< i])
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
39 break
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
40
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
41 proc getInstanceExtensions*(): seq[string] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
42 var extensionCount: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
43 checkVkResult vkEnumerateInstanceExtensionProperties(nil, addr(extensionCount), nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
44 var extensions = newSeq[VkExtensionProperties](extensionCount)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
45 checkVkResult vkEnumerateInstanceExtensionProperties(nil, addr(extensionCount), addr(extensions[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
46
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
47 for extension in extensions:
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
48 result.add(cleanString(extension.extensionName))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
49
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
50
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
51 proc getDeviceExtensions*(device: VkPhysicalDevice): seq[string] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
52 var extensionCount: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
53 checkVkResult vkEnumerateDeviceExtensionProperties(device, nil, addr(extensionCount), nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
54 var extensions = newSeq[VkExtensionProperties](extensionCount)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 checkVkResult vkEnumerateDeviceExtensionProperties(device, nil, addr(extensionCount), addr(extensions[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
56
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
57 for extension in extensions:
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
58 result.add(cleanString(extension.extensionName))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
59
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
60
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
61 proc getValidationLayers*(): seq[string] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 var n_layers: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
63 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
64 var layers = newSeq[VkLayerProperties](n_layers)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
65 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), addr(layers[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
66
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
67 for layer in layers:
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
68 result.add(cleanString(layer.layerName))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
69
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
70
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
71 proc getVulkanPhysicalDevices*(instance: VkInstance): seq[VkPhysicalDevice] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
72 var n_devices: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
73 checkVkResult vkEnumeratePhysicalDevices(instance, addr(n_devices), nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
74 result = newSeq[VkPhysicalDevice](n_devices)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
75 checkVkResult vkEnumeratePhysicalDevices(instance, addr(n_devices), addr(result[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
76
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
77
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
78 proc getQueueFamilies*(device: VkPhysicalDevice): seq[VkQueueFamilyProperties] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
79 var n_queuefamilies: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
80 vkGetPhysicalDeviceQueueFamilyProperties(device, addr(n_queuefamilies), nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
81 result = newSeq[VkQueueFamilyProperties](n_queuefamilies)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
82 vkGetPhysicalDeviceQueueFamilyProperties(device, addr(n_queuefamilies), addr(result[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
83
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
84
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
85 proc getDeviceSurfaceFormats*(device: VkPhysicalDevice, surface: VkSurfaceKHR): seq[VkSurfaceFormatKHR] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
86 var n_formats: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
87 checkVkResult vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, addr(n_formats), nil);
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
88 result = newSeq[VkSurfaceFormatKHR](n_formats)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
89 checkVkResult vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, addr(n_formats), addr(result[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
90
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
91
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
92 proc getDeviceSurfacePresentModes*(device: VkPhysicalDevice, surface: VkSurfaceKHR): seq[VkPresentModeKHR] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
93 var n_modes: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
94 checkVkResult vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, addr(n_modes), nil);
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
95 result = newSeq[VkPresentModeKHR](n_modes)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
96 checkVkResult vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, addr(n_modes), addr(result[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
97
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
98
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
99 proc getSwapChainImages*(device: VkDevice, swapChain: VkSwapchainKHR): seq[VkImage] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
100 var n_images: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
101 checkVkResult vkGetSwapchainImagesKHR(device, swapChain, addr(n_images), nil);
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
102 result = newSeq[VkImage](n_images)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
103 checkVkResult vkGetSwapchainImagesKHR(device, swapChain, addr(n_images), addr(result[0]));
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
104
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
105
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
106 proc getPresentMode*(modes: seq[VkPresentModeKHR]): VkPresentModeKHR =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
107 let preferredModes = [
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
108 VK_PRESENT_MODE_MAILBOX_KHR, # triple buffering
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
109 VK_PRESENT_MODE_FIFO_RELAXED_KHR, # double duffering
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
110 VK_PRESENT_MODE_FIFO_KHR, # double duffering
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
111 VK_PRESENT_MODE_IMMEDIATE_KHR, # single buffering
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
112 ]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
113 for preferredMode in preferredModes:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
114 for mode in modes:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
115 if preferredMode == mode:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
116 return mode
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
117 # should never be reached, but seems to be garuanteed by vulkan specs to always be available
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
118 return VK_PRESENT_MODE_FIFO_KHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
119
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
120
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
121 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
122 var requiredExtensions = [
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
123 "VK_EXT_acquire_xlib_display".cstring,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
124 "VK_EXT_direct_mode_display".cstring,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
125 "VK_KHR_display".cstring,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
126 "VK_KHR_surface".cstring,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
127 "VK_KHR_xlib_surface".cstring,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
128 "VK_EXT_debug_utils".cstring,
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
129 ]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
130 let availableExtensions = getInstanceExtensions()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
131 for extension in requiredExtensions:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
132 assert $extension in availableExtensions
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
133
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
134 let desiredLayers = ["VK_LAYER_KHRONOS_validation".cstring, "VK_LAYER_MESA_overlay".cstring]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
135 let availableLayers = getValidationLayers()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
136 var usableLayers = newSeq[cstring]()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
137
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
138 when ENABLEVULKANVALIDATIONLAYERS:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
139 for layer in desiredLayers:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
140 if $layer in availableLayers:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
141 usableLayers.add(layer)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
142
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
143 echo "Available validation layers: ", availableLayers
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
144 echo "Using validation layers: ", usableLayers
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
145 echo "Available extensions: ", availableExtensions
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
146 echo "Using extensions: ", requiredExtensions
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
147
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
148 var appinfo = VkApplicationInfo(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
149 sType: VK_STRUCTURE_TYPE_APPLICATION_INFO,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
150 pApplicationName: "Hello Triangle",
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
151 pEngineName: "Custom engine",
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
152 apiVersion: vulkanVersion,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
153 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
154 var createinfo = VkInstanceCreateInfo(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
155 sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
156 pApplicationInfo: addr(appinfo),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
157 enabledLayerCount: usableLayers.len.uint32,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
158 ppEnabledLayerNames: cast[ptr UncheckedArray[cstring]](addr(usableLayers[0])),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
159 enabledExtensionCount: requiredExtensions.len.uint32,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
160 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
161 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
162 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
163
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
164 loadVK_KHR_surface()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
165 loadVK_KHR_xlib_surface()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
166 loadVK_KHR_swapchain()
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
167 when ENABLEVULKANVALIDATIONLAYERS:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
168 loadVK_EXT_debug_utils(result)
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
169
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
170
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
171 proc getVulcanDevice*(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
172 physicalDevice: var VkPhysicalDevice,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
173 features: var VkPhysicalDeviceFeatures,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
174 graphicsQueueFamily: uint32,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
175 presentationQueueFamily: uint32,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
176 ): (VkDevice, VkQueue, VkQueue) =
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
177 # setup queue and device
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
178 # TODO: need check this, possibly wrong logic, see Vulkan tutorial
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
179 var priority = 1.0'f32
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
180 var queueCreateInfo = [
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
181 VkDeviceQueueCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
182 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
183 queueFamilyIndex: graphicsQueueFamily,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
184 queueCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
185 pQueuePriorities: addr(priority),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
186 ),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
187 ]
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
188
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
189 var requiredExtensions = ["VK_KHR_swapchain".cstring]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
190 var deviceCreateInfo = VkDeviceCreateInfo(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
191 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
192 queueCreateInfoCount: uint32(queueCreateInfo.len),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
193 pQueueCreateInfos: addr(queueCreateInfo[0]),
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
194 pEnabledFeatures: addr(features),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
195 enabledExtensionCount: requiredExtensions.len.uint32,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
196 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
197 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
198 checkVkResult vkCreateDevice(physicalDevice, addr(deviceCreateInfo), nil, addr(result[0]))
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
199 vkGetDeviceQueue(result[0], graphicsQueueFamily, 0'u32, addr(result[1]));
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
200 vkGetDeviceQueue(result[0], presentationQueueFamily, 0'u32, addr(result[2]));
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
201
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
202 proc createShaderStage*(device: VkDevice, stage: VkShaderStageFlagBits, shader: string): VkPipelineShaderStageCreateInfo =
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
203 const VK_GLSL_MAP = {
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
204 VK_SHADER_STAGE_VERTEX_BIT: GLSLANG_STAGE_VERTEX,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
205 VK_SHADER_STAGE_FRAGMENT_BIT: GLSLANG_STAGE_FRAGMENT,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
206 }.toTable()
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
207 var code = compileGLSLToSPIRV(VK_GLSL_MAP[stage], shader, "<memory-shader>")
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
208 var createInfo = VkShaderModuleCreateInfo(
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
209 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
210 codeSize: uint(code.len * sizeof(uint32)),
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
211 pCode: addr(code[0]),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
212 )
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
213 var shaderModule: VkShaderModule
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
214 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule))
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
215
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
216 return VkPipelineShaderStageCreateInfo(
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
217 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
218 stage: stage,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
219 module: shaderModule,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
220 pName: "main", # entry point for shader
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
221 )
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
222
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
223 proc debugCallback*(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
224 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
225 messageTypes: VkDebugUtilsMessageTypeFlagsEXT,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
226 pCallbackData: VkDebugUtilsMessengerCallbackDataEXT,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
227 userData: pointer
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
228 ): VkBool32 {.cdecl.} =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
229 echo &"{messageSeverity}: {VkDebugUtilsMessageTypeFlagBitsEXT(messageTypes)}: {pCallbackData.pMessage}"
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
230 return VK_FALSE