annotate src/vulkan_helpers.nim @ 1:bb2a7d3a7003

add: more steps in setup
author Sam <sam@basx.dev>
date Fri, 16 Dec 2022 00:05:41 +0700
parents 5daf3f236d87
children 213fdf8d31dd
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
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
3
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
4 import ./glslang/glslang
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
5 import ./vulkan
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
6
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
7
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8 when defined(release):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9 const ENABLEVULKANVALIDATIONLAYERS = false
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10 else:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11 const ENABLEVULKANVALIDATIONLAYERS = true
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
12
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
13
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
14 template checkVkResult*(call: untyped) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15 let value = call
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
16 if value != VK_SUCCESS:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
17 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
18
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
19
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
20 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
21 (variant shl 29) or (major shl 22) or (minor shl 12) or patch
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
22
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
23
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
24 proc getInstanceExtensions*(): seq[string] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
25 var extensionCount: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
26 checkVkResult vkEnumerateInstanceExtensionProperties(nil, addr(extensionCount), nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
27 var extensions = newSeq[VkExtensionProperties](extensionCount)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
28 checkVkResult vkEnumerateInstanceExtensionProperties(nil, addr(extensionCount), addr(extensions[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
29
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
30 for extension in extensions:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
31 result.add(join(extension.extensionName).strip(chars={char(0)}))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
33
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
34 proc getDeviceExtensions*(device: VkPhysicalDevice): seq[string] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
35 var extensionCount: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
36 checkVkResult vkEnumerateDeviceExtensionProperties(device, nil, addr(extensionCount), nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37 var extensions = newSeq[VkExtensionProperties](extensionCount)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
38 checkVkResult vkEnumerateDeviceExtensionProperties(device, nil, addr(extensionCount), addr(extensions[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
39
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
40 for extension in extensions:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
41 result.add(join(extension.extensionName).strip(chars={char(0)}))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
42
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
43
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
44 proc getValidationLayers*(): seq[string] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
45 var n_layers: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
46 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
47 var layers = newSeq[VkLayerProperties](n_layers)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
48 checkVkResult vkEnumerateInstanceLayerProperties(addr(n_layers), addr(layers[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
49
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
50 for layer in layers:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
51 result.add(join(layer.layerName).strip(chars={char(0)}))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
52
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
53
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
54 proc getVulkanPhysicalDevices*(instance: VkInstance): seq[VkPhysicalDevice] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 var n_devices: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
56 checkVkResult vkEnumeratePhysicalDevices(instance, addr(n_devices), nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
57 result = newSeq[VkPhysicalDevice](n_devices)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
58 checkVkResult vkEnumeratePhysicalDevices(instance, addr(n_devices), addr(result[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
59
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
60
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
61 proc getQueueFamilies*(device: VkPhysicalDevice): seq[VkQueueFamilyProperties] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 var n_queuefamilies: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
63 vkGetPhysicalDeviceQueueFamilyProperties(device, addr(n_queuefamilies), nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
64 result = newSeq[VkQueueFamilyProperties](n_queuefamilies)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
65 vkGetPhysicalDeviceQueueFamilyProperties(device, addr(n_queuefamilies), addr(result[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
66
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
67
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
68 proc getDeviceSurfaceFormats*(device: VkPhysicalDevice, surface: VkSurfaceKHR): seq[VkSurfaceFormatKHR] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
69 var n_formats: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
70 checkVkResult vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, addr(n_formats), nil);
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
71 result = newSeq[VkSurfaceFormatKHR](n_formats)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
72 checkVkResult vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, addr(n_formats), addr(result[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
73
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
74
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
75 proc getDeviceSurfacePresentModes*(device: VkPhysicalDevice, surface: VkSurfaceKHR): seq[VkPresentModeKHR] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
76 var n_modes: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
77 checkVkResult vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, addr(n_modes), nil);
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
78 result = newSeq[VkPresentModeKHR](n_modes)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
79 checkVkResult vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, addr(n_modes), addr(result[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
80
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
81
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
82 proc getSwapChainImages*(device: VkDevice, swapChain: VkSwapchainKHR): seq[VkImage] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
83 var n_images: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
84 checkVkResult vkGetSwapchainImagesKHR(device, swapChain, addr(n_images), nil);
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
85 result = newSeq[VkImage](n_images)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
86 checkVkResult vkGetSwapchainImagesKHR(device, swapChain, addr(n_images), addr(result[0]));
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
87
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
88
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
89 proc getPresentMode*(modes: seq[VkPresentModeKHR]): VkPresentModeKHR =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
90 let preferredModes = [
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
91 VK_PRESENT_MODE_MAILBOX_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
92 VK_PRESENT_MODE_FIFO_RELAXED_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
93 VK_PRESENT_MODE_FIFO_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
94 VK_PRESENT_MODE_IMMEDIATE_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
95 ]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
96 for preferredMode in preferredModes:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
97 for mode in modes:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
98 if preferredMode == mode:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
99 return mode
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
100 # 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
101 return VK_PRESENT_MODE_FIFO_KHR
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
102
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
103
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
104 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
105 var requiredExtensions = [
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
106 "VK_EXT_acquire_xlib_display".cstring,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
107 "VK_EXT_direct_mode_display".cstring,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
108 "VK_KHR_display".cstring,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
109 "VK_KHR_surface".cstring,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
110 "VK_KHR_xlib_surface".cstring,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
111 ]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
112 let availableExtensions = getInstanceExtensions()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
113 for extension in requiredExtensions:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
114 assert $extension in availableExtensions
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
115
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
116 let desiredLayers = ["VK_LAYER_KHRONOS_validation".cstring, "VK_LAYER_MESA_overlay".cstring]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
117 let availableLayers = getValidationLayers()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
118 var usableLayers = newSeq[cstring]()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
119
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
120 when ENABLEVULKANVALIDATIONLAYERS:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
121 for layer in desiredLayers:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
122 if $layer in availableLayers:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
123 usableLayers.add(layer)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
124
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
125 echo "Using validation layers: ", usableLayers
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
126 echo "Using extensions: ", requiredExtensions
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
127
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
128 var appinfo = VkApplicationInfo(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
129 sType: VK_STRUCTURE_TYPE_APPLICATION_INFO,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
130 pApplicationName: "Hello Triangle",
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
131 pEngineName: "Custom engine",
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
132 apiVersion: vulkanVersion,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
133 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
134 var createinfo = VkInstanceCreateInfo(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
135 sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
136 pApplicationInfo: addr(appinfo),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
137 enabledLayerCount: usableLayers.len.uint32,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
138 ppEnabledLayerNames: cast[ptr UncheckedArray[cstring]](addr(usableLayers[0])),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
139 enabledExtensionCount: requiredExtensions.len.uint32,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
140 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
141 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
142 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
143
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
144 loadVK_KHR_surface()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
145 loadVK_KHR_xlib_surface()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
146 loadVK_KHR_swapchain()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
147
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
148
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
149 proc getVulcanDevice*(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
150 physicalDevice: var VkPhysicalDevice,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
151 features: var VkPhysicalDeviceFeatures,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
152 selectedQueueFamily: uint32,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
153 ): (VkDevice, VkQueue) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
154 # setup queue and device
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
155 var priority = 1.0'f32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
156 var queueCreateInfo = VkDeviceQueueCreateInfo(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
157 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
158 queueFamilyIndex: uint32(selectedQueueFamily),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
159 queueCount: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
160 pQueuePriorities: addr(priority),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
161 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
162
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
163 var requiredExtensions = ["VK_KHR_swapchain".cstring]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
164 var deviceCreateInfo = VkDeviceCreateInfo(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
165 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
166 pQueueCreateInfos: addr(queueCreateInfo),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
167 queueCreateInfoCount: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
168 pEnabledFeatures: addr(features),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
169 enabledExtensionCount: requiredExtensions.len.uint32,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
170 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
171 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
172 checkVkResult vkCreateDevice(physicalDevice, addr(deviceCreateInfo), nil, addr(result[0]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
173 vkGetDeviceQueue(result[0], selectedQueueFamily, 0'u32, addr(result[1]));
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
174
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
175 proc createShaderStage*(device: VkDevice, stage: VkShaderStageFlagBits, shader: string): VkPipelineShaderStageCreateInfo =
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
176 const VK_GLSL_MAP = {
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
177 VK_SHADER_STAGE_VERTEX_BIT: GLSLANG_STAGE_VERTEX,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
178 VK_SHADER_STAGE_FRAGMENT_BIT: GLSLANG_STAGE_FRAGMENT,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
179 }.toTable()
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
180 var code = compileGLSLToSPIRV(VK_GLSL_MAP[stage], shader, "<memory-shader>")
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
181 var createInfo = VkShaderModuleCreateInfo(
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
182 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
183 codeSize: code.len.uint,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
184 pCode: addr(code[0]),
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
185 )
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
186 var shaderModule: VkShaderModule
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
187 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule))
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
188
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
189 var vertShaderStageInfo = VkPipelineShaderStageCreateInfo(
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
190 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
191 stage: stage,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
192 module: shaderModule,
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
193 pName: "main", # entry point for shader
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
194 )