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);
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>
54 VkDevice d, T obj, std::string_view name, std::size_t n,
55 std::string *buf) const;
57 template<typename T>
59 VkDevice d, std::span<T> v, std::string_view name) const;
61 template<typename T>
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);
67 VkDevice d, VkObjectType t,
68 std::uint64_t obj, std::string_view name) const;
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 = {};
76 ErrorFn *error_fn = {};
77 void *error_data = {};
78};
79
81struct InstanceInfo {
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:
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>
106inline bool Instance::set_obj_name(
107 VkDevice d, T obj, std::string_view name
108) const {
109 return this->set_obj_name(
110 d, vk_obj_type<T>, Instance::obj_handle(obj), name);
111}
112
113template<typename T>
114inline bool Instance::set_obj_name(
115 VkDevice d, T obj, std::string_view name, std::size_t n
116) const {
117 return this->set_obj_name(
118 d, vk_obj_type<T>, Instance::obj_handle(obj), name, n);
119}
120
121template<typename T>
122inline bool Instance::set_obj_name(
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>
131inline bool Instance::set_obj_name(
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
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
bool set_obj_name(VkDevice d, std::span< T > v, std::string_view name) const
Sets the debug name of a range of objects.
ErrorFn * error_fn
Definition: instance.h:76
void * error_data
Definition: instance.h:77
void set_error()
Called by the debug callback when an error is received.
Definition: instance.h:45
bool set_obj_name(VkDevice d, T obj, std::string_view name, std::size_t n, std::string *buf) const
Sets the debug name of an object.
bool set_obj_name(VkDevice dev, T obj, std::string_view name) const
Sets the debug name of an object.
VkInstance h
Definition: instance.h:73
VkDebugUtilsMessengerEXT messenger
Definition: instance.h:74
bool set_obj_name(VkDevice d, VkObjectType t, std::uint64_t obj, std::string_view name, std::size_t n, std::string *buf) const
static auto obj_handle(T o)
Definition: instance.h:98
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.
static auto obj_handle(T o)
bool set_obj_name(VkDevice d, VkObjectType t, std::uint64_t obj, std::string_view name) const
bool set_obj_name(VkDevice d, T obj, std::string_view name, std::size_t n) const
Sets the debug name of a range of objects.
init
Definition: img_common.lua:34
for i
Definition: font.lua:5
name
Definition: cathedral.lua:11
n
Definition: dump_lights.lua:5
v[1]
Definition: math.lua:19
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39
#define T(f0, f1, f2)
Definition: audio.cpp:7
Definition: debug.h:13
Definition: graphics.h:155
Definition: graphics.h:138
void init_devices(VkInstance i)
Initializes device data.
std::vector< Graphics::Layer > layers
Definition: instance.h:85
std::vector< Graphics::Extension > extensions
Definition: instance.h:84
std::uint32_t version
Definition: instance.h:83
std::vector< VkPhysicalDevice > physical_devs
Definition: instance.h:86
bool init()
Initializes instance-independent data.
bool check_version(Graphics::Version v) const
Verifies that the given version is supported.