Mercurial > games > semicongine
comparison src/zamikongine/vulkan_helpers.nim @ 499:3f1111f3b9f8
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 18 Jan 2023 09:52:03 +0700 |
parents | 73a0954beabd |
children |
comparison
equal
deleted
inserted
replaced
498:2fa8e418deae | 499:3f1111f3b9f8 |
---|---|
4 import std/logging | 4 import std/logging |
5 import std/macros | 5 import std/macros |
6 | 6 |
7 import ./vulkan | 7 import ./vulkan |
8 import ./window | 8 import ./window |
9 | |
10 | |
11 const ENABLEVULKANVALIDATIONLAYERS* = not defined(release) | |
12 | |
13 | 9 |
14 template checkVkResult*(call: untyped) = | 10 template checkVkResult*(call: untyped) = |
15 when defined(release): | 11 when defined(release): |
16 discard call | 12 discard call |
17 else: | 13 else: |
23 let value = call | 19 let value = call |
24 if value != VK_SUCCESS: | 20 if value != VK_SUCCESS: |
25 error "Vulkan error: ", astToStr(call), " returned ", $value | 21 error "Vulkan error: ", astToStr(call), " returned ", $value |
26 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value) | 22 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value) |
27 | 23 |
24 # the included code need checkVkResult, therefore having the template above | |
25 when defined(linux): | |
26 include ./platform/linux/vulkan | |
27 when defined(windows): | |
28 include ./platform/windows/vulkan | |
29 | |
30 const ENABLEVULKANVALIDATIONLAYERS* = not defined(release) | |
31 | |
28 func addrOrNil[T](obj: var openArray[T]): ptr T = | 32 func addrOrNil[T](obj: var openArray[T]): ptr T = |
29 if obj.len > 0: addr(obj[0]) else: nil | 33 if obj.len > 0: addr(obj[0]) else: nil |
30 | 34 |
31 func VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} = | 35 func VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} = |
32 (variant shl 29) or (major shl 22) or (minor shl 12) or patch | 36 (variant shl 29) or (major shl 22) or (minor shl 12) or patch |
130 return VK_PRESENT_MODE_FIFO_KHR | 134 return VK_PRESENT_MODE_FIFO_KHR |
131 | 135 |
132 | 136 |
133 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance = | 137 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance = |
134 | 138 |
135 var requiredExtensions = @["VK_KHR_surface".cstring] | 139 var requiredExtensions = @["VK_KHR_surface".cstring] & REQUIRED_PLATFORM_EXTENSIONS |
136 when defined(linux): | |
137 requiredExtensions.add("VK_KHR_xlib_surface".cstring) | |
138 when defined(windows): | |
139 requiredExtensions.add("VK_KHR_win32_surface".cstring) | |
140 when ENABLEVULKANVALIDATIONLAYERS: | 140 when ENABLEVULKANVALIDATIONLAYERS: |
141 requiredExtensions.add("VK_EXT_debug_utils".cstring) | 141 requiredExtensions.add("VK_EXT_debug_utils".cstring) |
142 | 142 |
143 let availableExtensions = getInstanceExtensions() | 143 let availableExtensions = getInstanceExtensions() |
144 for extension in requiredExtensions: | 144 for extension in requiredExtensions: |
175 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions[0])) | 175 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions[0])) |
176 ) | 176 ) |
177 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result)) | 177 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result)) |
178 | 178 |
179 loadVK_KHR_surface() | 179 loadVK_KHR_surface() |
180 when defined(linux): | 180 load_platform_extensions() |
181 loadVK_KHR_xlib_surface() | |
182 when defined(windows): | |
183 loadVK_KHR_win32_surface() | |
184 loadVK_KHR_swapchain() | 181 loadVK_KHR_swapchain() |
185 when ENABLEVULKANVALIDATIONLAYERS: | 182 when ENABLEVULKANVALIDATIONLAYERS: |
186 loadVK_EXT_debug_utils(result) | 183 loadVK_EXT_debug_utils(result) |
187 | 184 |
188 | 185 |
226 echo &"{messageSeverity}: {VkDebugUtilsMessageTypeFlagBitsEXT(messageTypes)}: {pCallbackData.pMessage}" | 223 echo &"{messageSeverity}: {VkDebugUtilsMessageTypeFlagBitsEXT(messageTypes)}: {pCallbackData.pMessage}" |
227 return VK_FALSE | 224 return VK_FALSE |
228 | 225 |
229 proc getSurfaceCapabilities*(device: VkPhysicalDevice, surface: VkSurfaceKHR): VkSurfaceCapabilitiesKHR = | 226 proc getSurfaceCapabilities*(device: VkPhysicalDevice, surface: VkSurfaceKHR): VkSurfaceCapabilitiesKHR = |
230 checkVkResult device.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(surface, addr(result)) | 227 checkVkResult device.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(surface, addr(result)) |
231 | |
232 when defined(linux): | |
233 proc createVulkanSurface*(instance: VkInstance, window: NativeWindow): VkSurfaceKHR = | |
234 var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR( | |
235 sType: VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, | |
236 dpy: window.display, | |
237 window: window.window, | |
238 ) | |
239 checkVkResult vkCreateXlibSurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result)) | |
240 when defined(windows): | |
241 proc createVulkanSurface*(instance: VkInstance, window: NativeWindow): VkSurfaceKHR = | |
242 var surfaceCreateInfo = VkWin32SurfaceCreateInfoKHR( | |
243 sType: VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR, | |
244 hinstance: window.hinstance, | |
245 hwnd: window.hwnd, | |
246 ) | |
247 checkVkResult vkCreateWin32SurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result)) |