comparison src/semicongine/vulkan_helpers.nim @ 83:5e19aead2b61

add: new vulkan api wrapper, not done yet
author Sam <sam@basx.dev>
date Thu, 23 Feb 2023 00:34:38 +0700
parents e5be7a25634e
children 8412f433dc46
comparison
equal deleted inserted replaced
82:9e7937b7bcc8 83:5e19aead2b61
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 template checkVkResult*(call: untyped) =
11 when defined(release):
12 discard call
13 else:
14 # yes, a bit cheap, but this is only for nice debug output
15 var callstr = astToStr(call).replace("\n", "")
16 while callstr.find(" ") >= 0:
17 callstr = callstr.replace(" ", " ")
18 debug "CALLING vulkan: ", callstr
19 let value = call
20 if value != VK_SUCCESS:
21 error "Vulkan error: ", astToStr(call), " returned ", $value
22 raise newException(Exception, "Vulkan error: " & astToStr(call) &
23 " returned " & $value)
24 9
25 # the included code need checkVkResult, therefore having the template above 10 # the included code need checkVkResult, therefore having the template above
26 when defined(linux): 11 when defined(linux):
27 include ./platform/linux/vulkan 12 include ./platform/linux/vulkan
28 when defined(windows): 13 when defined(windows):
192 enabledExtensionCount: requiredExtensions.len.uint32, 177 enabledExtensionCount: requiredExtensions.len.uint32,
193 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr( 178 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(
194 requiredExtensions[0])) 179 requiredExtensions[0]))
195 ) 180 )
196 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result)) 181 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result))
197 182 for extension in requiredExtensions:
198 loadVK_KHR_surface() 183 result.loadExtension($extension)
199 load_platform_extensions() 184
200 loadVK_KHR_swapchain() 185 # loadVK_KHR_surface(result)
186 # loadVK_KHR_swapchain(result)
201 when ENABLEVULKANVALIDATIONLAYERS: 187 when ENABLEVULKANVALIDATIONLAYERS:
202 loadVK_EXT_debug_utils(result) 188 loadVK_EXT_debug_utils(result)
203 189
204 190
205 proc getVulcanDevice*( 191 proc getVulcanDevice*(
237 proc debugCallback*( 223 proc debugCallback*(
238 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT, 224 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT,
239 messageTypes: VkDebugUtilsMessageTypeFlagsEXT, 225 messageTypes: VkDebugUtilsMessageTypeFlagsEXT,
240 pCallbackData: VkDebugUtilsMessengerCallbackDataEXT, 226 pCallbackData: VkDebugUtilsMessengerCallbackDataEXT,
241 userData: pointer 227 userData: pointer
242 ): VkBool32 {.cdecl.} = 228 ): bool {.cdecl.} =
243 echo &"{messageSeverity}: {VkDebugUtilsMessageTypeFlagBitsEXT(messageTypes)}: {pCallbackData.pMessage}" 229 echo &"{messageSeverity}: {VkDebugUtilsMessageTypeFlagBitsEXT(messageTypes)}: {pCallbackData.pMessage}"
244 return VK_FALSE 230 return false
245 231
246 proc getSurfaceCapabilities*(device: VkPhysicalDevice, 232 proc getSurfaceCapabilities*(device: VkPhysicalDevice,
247 surface: VkSurfaceKHR): VkSurfaceCapabilitiesKHR = 233 surface: VkSurfaceKHR): VkSurfaceCapabilitiesKHR =
248 checkVkResult device.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(surface, addr(result)) 234 checkVkResult device.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(surface, addr(result))