nngn
Loading...
Searching...
No Matches
instance.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_VULKAN_INSTANCE_H
2#define NNGN_GRAPHICS_VULKAN_INSTANCE_H
3
4#include <cstdint>
5#include <ranges>
6#include <span>
7#include <string_view>
8#include <type_traits>
9#include <vector>
10
11#include "graphics/graphics.h"
12
13#include "types.h"
14#include "vulkan.h"
15
16namespace nngn {
17
19class Instance {
20public:
21 using ErrorFn = void(void*);
23 Instance(void) = default;
25 ~Instance(void);
27 bool init(
28 Graphics::Version version,
29 std::span<const char *const> extensions);
37 bool init_debug(
38 Graphics::Version version,
39 std::span<const char *const> extensions,
40 std::span<const char *const> layers,
41 ErrorFn error_fn, void *error_data, Graphics::LogLevel log_level);
43 VkInstance id() const { return this->h; }
45 void set_error() { if(this->error_fn) this->error_fn(this->error_data); }
47 template<typename R> R get_proc_addr(const char *name) const;
49 template<typename T>
50 bool set_obj_name(VkDevice dev, T obj, std::string_view name) const;
52 template<typename T>
53 bool set_obj_name(
54 VkDevice d, T obj, std::string_view name, std::size_t n,
55 std::string *buf) const;
57 template<typename T>
58 bool set_obj_name(
59 VkDevice d, std::span<T> v, std::string_view name) const;
61 template<typename T>
62 bool set_obj_name(
63 VkDevice d, T obj, std::string_view name, std::size_t n) const;
64private:
65 template<typename T> static auto obj_handle(T o);
66 bool set_obj_name(
67 VkDevice d, VkObjectType t,
68 std::uint64_t obj, std::string_view name) const;
69 bool set_obj_name(
70 VkDevice d, VkObjectType t,
71 std::uint64_t obj, std::string_view name, std::size_t n,
72 std::string *buf) const;
73 VkInstance h = {};
74 VkDebugUtilsMessengerEXT messenger = {};
75 PFN_vkSetDebugUtilsObjectNameEXT set_obj_name_f = {};
77 void *error_data = {};
78};
79
82public:
83 std::uint32_t version = {};
84 std::vector<Graphics::Extension> extensions = {};
85 std::vector<Graphics::Layer> layers = {};
86 std::vector<VkPhysicalDevice> physical_devs = {};
88 bool init();
90 void init_devices(VkInstance i);
93private:
94 void init_extensions();
95 void init_layers();
96};
97
98template<typename T> auto Instance::obj_handle(T obj) {
99 if constexpr(std::is_base_of_v<Handle, T>)
100 return obj.id;
101 else
102 return reinterpret_cast<std::uintptr_t>(obj);
103}
104
105template<typename T>
107 VkDevice d, T obj, std::string_view name
108) const {
109 return this->set_obj_name(
111}
112
113template<typename T>
115 VkDevice d, T obj, std::string_view name, std::size_t n
116) const {
117 return this->set_obj_name(
119}
120
121template<typename T>
123 VkDevice d, T obj, std::string_view name, std::size_t n,
124 std::string *buf
125) const {
126 return this->set_obj_name(
127 d, vk_obj_type<T>, Instance::obj_handle(obj), name, n, buf);
128}
129
130template<typename T>
132 VkDevice d, std::span<T> v, std::string_view name
133) const {
134 std::string buf = {};
135 constexpr auto t = vk_obj_type<std::decay_t<T>>;
136 for(std::size_t i = 0, n = v.size(); i < n; ++i)
137 if(!this->set_obj_name(d, t, Instance::obj_handle(v[i]), name, i, &buf))
138 return false;
139 return true;
140}
141
142}
143
144#endif
local class const
Definition animation.lua:7
name
Definition cathedral.lua:11
Owning wrapper for a Vulkan instance.
Definition instance.h:19
bool init_debug(Graphics::Version version, std::span< const char *const > extensions, std::span< const char *const > layers, ErrorFn error_fn, void *error_data, Graphics::LogLevel log_level)
Initializes an instance in debug mode.
Definition instance.cpp:116
void(void *) ErrorFn
Definition instance.h:21
void set_error()
Called by the debug callback when an error is received.
Definition instance.h:45
bool set_obj_name(VkDevice dev, T obj, std::string_view name) const
Sets the debug name of an object.
Definition instance.h:106
void * error_data
Definition instance.h:77
VkInstance h
Definition instance.h:73
VkDebugUtilsMessengerEXT messenger
Definition instance.h:74
static auto obj_handle(T o)
Definition instance.h:98
ErrorFn * error_fn
Definition instance.h:76
PFN_vkSetDebugUtilsObjectNameEXT set_obj_name_f
Definition instance.h:75
R get_proc_addr(const char *name) const
Retrieves a function pointer by name, cast to R.
Definition instance.cpp:155
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
#define T(f0, f1, f2)
init
Definition img_common.lua:34
Definition audio.cpp:7
constexpr empty vk_obj_type
Maps types to VkObjectType values.
Definition types.h:54
Definition debug.h:13
v[1]
Definition math.lua:22
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
Definition graphics.h:155
Definition graphics.h:138
Aggregate type for information about an instance.
Definition instance.h:81
void init_devices(VkInstance i)
Initializes device data.
Definition instance.cpp:217
void init_layers()
Definition instance.cpp:199
void init_extensions()
Definition instance.cpp:192
std::uint32_t version
Definition instance.h:83
std::vector< Graphics::Layer > layers
Definition instance.h:85
std::vector< Graphics::Extension > extensions
Definition instance.h:84
bool init()
Initializes instance-independent data.
Definition instance.cpp:185
std::vector< VkPhysicalDevice > physical_devs
Definition instance.h:86
bool check_version(Graphics::Version v) const
Verifies that the given version is supported.
Definition instance.cpp:221