nngn
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_VULKAN_UTILS_H
2#define NNGN_GRAPHICS_VULKAN_UTILS_H
3
4#include <algorithm>
5#include <cstdint>
6#include <ranges>
7#include <span>
8#include <string>
9#include <vector>
10
11#include "graphics/graphics.h"
12#include "utils/concepts.h"
13#include "utils/fn.h"
14#include "utils/log.h"
15
16#include "types.h"
17
18#define LOG_RESULT(f, ...) nngn::vk_check_result(#f, f(__VA_ARGS__))
19#define CHECK_RESULT(f, ...) \
20 do { if(!LOG_RESULT(f, __VA_ARGS__)) [[unlikely]] return false; } while(0);
21
22namespace nngn {
23
24const char *vk_strerror(VkResult result);
25const char *vk_enum_str(VkDebugUtilsMessageSeverityFlagBitsEXT f);
26const char *vk_enum_str(VkDebugUtilsMessageTypeFlagsEXT f);
27
29std::string vk_version_str(std::uint32_t v);
30std::string vk_version_str(Graphics::Version v);
31
33Graphics::Version vk_parse_version(std::uint32_t v);
34
35template<typename T>
37
38inline nngn::uvec2 vk_extent_to_vec(VkExtent2D e)
39 { return {e.width, e.height}; }
40inline VkExtent2D vk_vec_to_extent(nngn::uvec2 v)
41 { return {.width = v.x, .height = v.y}; }
42
47bool vk_check_result(const char *func, VkResult r);
48
54template<c_function_pointer auto f>
55auto vk_enumerate(const auto &...args);
56
58std::vector<Graphics::Extension> vk_parse_extensions(
59 std::span<const VkExtensionProperties> s);
60
63
64template<typename T, auto ...Ps>
65constexpr auto vk_vertex_input(
66 std::span<const VkVertexInputBindingDescription> bindings);
67
68template<typename T>
69concept VkNamedRange = requires {
70 requires std::ranges::sized_range<T>;
71 {std::declval<std::ranges::range_value_t<T>>().name.data()}
72 -> std::same_as<char*>;
73};
74
76std::vector<const char*> vk_names(const VkNamedRange auto &r);
77
78template<typename T>
84
85inline bool vk_check_result(const char *func, VkResult r) {
86 if(r == VK_SUCCESS) [[likely]]
87 return true;
88 Log::l() << func << ": " << vk_strerror(r) << '\n';
89 return false;
90}
91
92template<c_function_pointer auto f>
93auto vk_enumerate(const auto &...args) {
94 u32 n = 0;
95 f(args..., &n, nullptr);
96 std::vector<std::remove_pointer_t<types_last_t<fn_args<f>>>> ret(n);
97 f(args..., &n, ret.data());
98 return ret;
99}
100
101std::vector<const char*> vk_names(const VkNamedRange auto &r) {
102 std::vector<const char*> ret = {};
103 ret.reserve(r.size());
104 for(const auto &x : r)
105 ret.push_back(x.name.data());
106 return ret;
107}
108
109template<standard_layout T, auto ...Ps>
111 constexpr auto n = sizeof...(Ps);
112 const std::array off = {static_cast<u32>(offsetof_ptr(Ps))...};
113 std::array<VkVertexInputAttributeDescription, n> ret = {};
114 for(std::size_t i = 0; i < n; ++i)
115 ret[i] = {
116 .location = static_cast<u32>(i),
117 .binding = 0,
118 .format = VK_FORMAT_R32G32B32_SFLOAT,
119 .offset = off[i],
120 };
121 return ret;
122}
123
124constexpr VkPipelineVertexInputStateCreateInfo vk_vertex_input(
125 std::span<const VkVertexInputBindingDescription> bindings,
126 std::span<const VkVertexInputAttributeDescription> attrs
127) {
128 return {
129 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
130 .vertexBindingDescriptionCount =
131 static_cast<std::uint32_t>(bindings.size()),
132 .pVertexBindingDescriptions = bindings.data(),
133 .vertexAttributeDescriptionCount =
134 static_cast<std::uint32_t>(attrs.size()),
135 .pVertexAttributeDescriptions = attrs.data(),
136 };
137}
138
139}
140
141#endif
name
Definition cathedral.lua:11
static std::ostream & l()
Definition log.cpp:56
Definition utils.h:69
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
#define T(f0, f1, f2)
local r
Definition gamma.lua:7
Definition audio.cpp:7
const char * vk_enum_str(VkDebugUtilsMessageSeverityFlagBitsEXT f)
Definition utils.cpp:108
std::string vk_version_str(Graphics::Version v)
Definition utils.cpp:119
std::vector< Graphics::Extension > vk_parse_extensions(std::span< const VkExtensionProperties > s)
Populates Graphics::Extension objects from their Vulkan equivalents.
Definition utils.cpp:158
Graphics::Version vk_parse_version(std::uint32_t v)
Unpacks a Vulkan version number.
Definition utils.cpp:144
std::uint32_t u32
Definition def.h:14
Graphics::PresentMode vk_present_mode(VkPresentModeKHR m)
Populates a Graphics::PresentMode from its Vulkan equivalent.
Definition utils.cpp:175
vk_create_info_type< T > vk_create_info(vk_create_info_type< T > info)
Definition utils.h:79
auto vk_enumerate(const auto &...args)
Creates a vector from the result of calling f with args.
Definition utils.h:93
nngn::uvec2 vk_extent_to_vec(VkExtent2D e)
Definition utils.h:38
bool vk_check_result(const char *func, VkResult r)
Checks that r is VK_SUCCESS or emits a log message.
Definition utils.h:85
typename detail::vk_create_info_type_impl< T >::type vk_create_info_type
Maps types to *CretaeInfo structures.
Definition types.h:47
VkExtent2D vk_vec_to_extent(nngn::uvec2 v)
Definition utils.h:40
std::vector< const char * > vk_names(const VkNamedRange auto &r)
Extracts the name character array from common objects.
Definition utils.h:101
auto vk_vertex_attrs()
Definition utils.h:110
const char * vk_strerror(VkResult result)
Definition utils.cpp:13
std::size_t offsetof_ptr(auto T::*p)
Similar to the stdlib's offsetof, but using member data pointers.
Definition utils.h:153
constexpr empty vk_struct_type
Maps types to VkStructureType values.
Definition types.h:51
constexpr auto vk_vertex_input(std::span< const VkVertexInputBindingDescription > bindings)
func
Definition plot.lua:8
local m
Definition input.lua:28
v[1]
Definition math.lua:22
local function f()) end
PresentMode
Definition graphics.h:148
T x
Definition vec2.h:12
local e
Definition math.lua:4
std::chrono::seconds s
Definition timing.cpp:6