annotate src/vulkan_helpers.nim @ 11:3415afff1d1a

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