comparison 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
comparison
equal deleted inserted replaced
9:227e53d1a853 11:3415afff1d1a
1 import std/tables 1 import std/tables
2 import std/strutils 2 import std/strutils
3 import std/strformat 3 import std/strformat
4 import std/logging 4 import std/logging
5 import std/macros
5 6
6 import ./glslang/glslang 7 import ./glslang/glslang
7 import ./vulkan 8 import ./vulkan
8 import ./window 9 import ./window
9 10
10 when defined(release): 11
11 const ENABLEVULKANVALIDATIONLAYERS* = false 12 const ENABLEVULKANVALIDATIONLAYERS* = not defined(release)
12 else:
13 const ENABLEVULKANVALIDATIONLAYERS* = true
14 13
15 14
16 template checkVkResult*(call: untyped) = 15 template checkVkResult*(call: untyped) =
17 when defined(release): 16 when defined(release):
18 discard call 17 discard call
19 else: 18 else:
20 debug(&"CALLING vulkan: {astToStr(call)}") 19 debug "CALLING vulkan: ", astToStr(call)
21 let value = call 20 let value = call
22 if value != VK_SUCCESS: 21 if value != VK_SUCCESS:
22 error "Vulkan error: ", astToStr(call), " returned ", $value
23 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value) 23 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value)
24 24
25 func addrOrNil[T](obj: var openArray[T]): ptr T = 25 func addrOrNil[T](obj: var openArray[T]): ptr T =
26 if obj.len > 0: addr(obj[0]) else: nil 26 if obj.len > 0: addr(obj[0]) else: nil
27 27
126 # should never be reached, but seems to be garuanteed by vulkan specs to always be available 126 # should never be reached, but seems to be garuanteed by vulkan specs to always be available
127 return VK_PRESENT_MODE_FIFO_KHR 127 return VK_PRESENT_MODE_FIFO_KHR
128 128
129 129
130 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance = 130 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance =
131 var requiredExtensions = [ 131
132 "VK_EXT_acquire_xlib_display".cstring, 132 when defined(linux):
133 "VK_EXT_direct_mode_display".cstring, 133 var requiredExtensions = [
134 "VK_KHR_display".cstring, 134 # "VK_EXT_acquire_xlib_display".cstring,
135 "VK_KHR_surface".cstring, 135 # "VK_EXT_direct_mode_display".cstring,
136 "VK_KHR_xlib_surface".cstring, 136 # "VK_KHR_display".cstring,
137 "VK_EXT_debug_utils".cstring, 137 "VK_KHR_surface".cstring,
138 ] 138 "VK_KHR_xlib_surface".cstring,
139 "VK_EXT_debug_utils".cstring,
140 ]
141 when defined(windows):
142 var requiredExtensions = [
143 "VK_KHR_win32_surface".cstring,
144 #"VK_KHR_display".cstring,
145 "VK_KHR_surface".cstring,
146 "VK_EXT_debug_utils".cstring,
147 ]
148
139 let availableExtensions = getInstanceExtensions() 149 let availableExtensions = getInstanceExtensions()
140 for extension in requiredExtensions: 150 for extension in requiredExtensions:
141 assert $extension in availableExtensions 151 assert $extension in availableExtensions, $extension
142 152
143 let desiredLayers = ["VK_LAYER_KHRONOS_validation".cstring, "VK_LAYER_MESA_overlay".cstring] 153 let desiredLayers = ["VK_LAYER_KHRONOS_validation".cstring, "VK_LAYER_MESA_overlay".cstring]
144 let availableLayers = getValidationLayers() 154 let availableLayers = getValidationLayers()
145 var usableLayers = newSeq[cstring]() 155 var usableLayers = newSeq[cstring]()
146 156
169 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions)) 179 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions))
170 ) 180 )
171 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result)) 181 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result))
172 182
173 loadVK_KHR_surface() 183 loadVK_KHR_surface()
174 loadVK_KHR_xlib_surface() 184 when defined(linux):
185 loadVK_KHR_xlib_surface()
186 when defined(windows):
187 loadVK_KHR_win32_surface()
175 loadVK_KHR_swapchain() 188 loadVK_KHR_swapchain()
176 when ENABLEVULKANVALIDATIONLAYERS: 189 when ENABLEVULKANVALIDATIONLAYERS:
177 loadVK_EXT_debug_utils(result) 190 loadVK_EXT_debug_utils(result)
178 191
179 192