SDEngine
Game Engine
Loading...
Searching...
No Matches
vulkan_utils.hpp
Go to the documentation of this file.
1#pragma once
2#include <source_location>
3
4namespace sd {
5
6// TODO(docs): Document CheckVulkanRes and CheckVulkanResVal
7// - Explain the pattern for Vulkan error handling
8// - When to use each function
9// - Example usage patterns
10inline void check_vulkan_res(vk::Result result, std::string_view message,
11 std::source_location loc = std::source_location::current()) {
12 if (result != vk::Result::eSuccess) {
13 auto p =
14 std::format("{}:{} {}: {}", loc.file_name(), loc.line(), message, vk::to_string(result));
16 }
17}
18
19template<typename T>
20auto check_vulkan_res_val(T&& result, std::string_view message,
21 std::source_location loc = std::source_location::current()) {
22 // Try to detect if it's a vk::ResultValue or something like std::expected
23 if constexpr (requires { result.result; }) {
24 check_vulkan_res(result.result, message, loc);
25 return std::move(result.value);
26 } else if constexpr (requires { result.has_value(); }) {
27 if (!result.has_value()) {
28 check_vulkan_res(result.ERROR(), message, loc);
29 }
30 return std::move(*result);
31 } else {
32 // Fallback or assume it's already a value (though this shouldn't happen with this name)
33 return std::forward<T>(result);
34 }
35}
36
37inline u32 find_memory_type(const vk::PhysicalDevice& physical_device, u32 type_filter,
38 vk::MemoryPropertyFlags properties) {
39 vk::PhysicalDeviceMemoryProperties mem_properties = physical_device.getMemoryProperties();
40 for (u32 i = 0; i < mem_properties.memoryTypeCount; i++) {
41 if ((type_filter & (1 << i)) &&
42 (mem_properties.memoryTypes[i].propertyFlags & properties) == properties)
43 return i;
44 }
45 engine_abort("Failed to find memory type");
46}
47
48} // namespace sd
Definition Application.hpp:28
auto check_vulkan_res_val(T &&result, std::string_view message, std::source_location loc=std::source_location::current())
Definition vulkan_utils.hpp:20
void engine_abort()
Definition base.hpp:46
u32 find_memory_type(const vk::PhysicalDevice &physical_device, u32 type_filter, vk::MemoryPropertyFlags properties)
Definition vulkan_utils.hpp:37
void check_vulkan_res(vk::Result result, std::string_view message, std::source_location loc=std::source_location::current())
Definition vulkan_utils.hpp:10
std::uint32_t u32
Definition types.hpp:15
constexpr T g_type_max
Definition types.hpp:21