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,
11 std::string_view message,
12 std::source_location loc = std::source_location::current()) {
13 if (result != vk::Result::eSuccess) {
14 log::engine::critical("{}:{} {}: {}",
15 loc.file_name(),
16 loc.line(),
17 message,
18 vk::to_string(result));
19 // TODO: why doesnt the vk::to_string(result) work correctly, it shjould return the correct
20 // string
21 }
22}
23
24template<typename T>
26 std::string_view message,
27 std::source_location loc = std::source_location::current()) {
28 // Try to detect if it's a vk::ResultValue or something like std::expected
29 if constexpr (requires { result.result; }) {
30 check_vulkan_res(result.result, message, loc);
31 return std::move(result.value);
32 } else if constexpr (requires { result.has_value(); }) {
33 if (!result.has_value()) {
34 check_vulkan_res(result.ERROR(), message, loc);
35 }
36 return std::move(*result);
37 } else {
38 // Fallback or assume it's already a value (though this shouldn't happen with this name)
39 return std::forward<T>(result);
40 }
41}
42
43inline U32 find_memory_type(const vk::PhysicalDevice& physical_device,
45 vk::MemoryPropertyFlags properties) {
46 vk::PhysicalDeviceMemoryProperties mem_properties = physical_device.getMemoryProperties();
47 for (U32 i = 0; i < mem_properties.memoryTypeCount; i++) {
48 if ((type_filter & (1 << i)) &&
49 (mem_properties.memoryTypes[i].propertyFlags & properties) == properties)
50 return i;
51 }
52 NOT_IMPLEMENTED; // fallback??
53 return 0;
54}
55
56} // namespace sd
#define NOT_IMPLEMENTED
Definition base.hpp:27
Definition Application.hpp:22
auto check_vulkan_res_val(T &&result, std::string_view message, std::source_location loc=std::source_location::current())
Definition vulkan_utils.hpp:25
void check_vulkan_res(vk::Result result, std::string_view message, std::source_location loc=std::source_location::current())
Definition vulkan_utils.hpp:10
U32 find_memory_type(const vk::PhysicalDevice &physical_device, U32 type_filter, vk::MemoryPropertyFlags properties)
Definition vulkan_utils.hpp:43
consteval U64 type_id_of()
Definition type_id.hpp:6
std::uint32_t U32
Definition types.hpp:15