-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.h
238 lines (183 loc) · 4.1 KB
/
globals.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifdef _MSC_VER
# pragma once
#endif
#ifndef GLOBALS_H
#define GLOBALS_H
#include <vulkan/vulkan.hpp>
#include "vk_mem_alloc.h"
struct vkRenderCtx_t
{
vk::Instance instance;
vk::PhysicalDevice physicalDevice;
vk::PhysicalDeviceProperties physicalDeviceProperties;
vk::Device device;
VmaAllocator allocator;
uint32_t queueFamily;
vk::Queue queue;
vk::Format swapchainFormat;
vk::Extent2D swapchainExtent;
vk::RenderPass renderPass;
vk::CommandPool commandPool;
};
extern vkRenderCtx_t vkRenderCtx;
template<typename T>
struct VmaAlloc
{
VmaAllocation allocation;
T value;
VmaAlloc& operator=(std::nullptr_t)
{
allocation = nullptr;
value = nullptr;
return *this;
}
operator bool() const
{
return value;
}
bool operator==(const VmaAlloc& other) const
{
return value == other.value;
}
bool operator!=(const VmaAlloc& other) const
{
return value != other.value;
}
};
template<typename T>
using UniqueVmaAlloc = vk::UniqueHandle<VmaAlloc<T>,vk::DispatchLoaderStatic>;
using UniqueVmaAllocator = vk::UniqueHandle<VmaAllocator,vk::DispatchLoaderStatic>;
template<typename T, typename Dispatch = vk::DispatchLoaderStatic>
class UniqueVector :
public vk::UniqueHandleTraits<T,Dispatch>::deleter
{
using Vector = std::vector<T>;
using Deleter = typename vk::UniqueHandleTraits<T,Dispatch>::deleter;
using size_type = typename Vector::size_type;
private:
Vector m_value;
public:
UniqueVector(Vector data = Vector(), const Deleter& deleter = Deleter()) :
Deleter(deleter),
m_value(std::move(data))
{}
UniqueVector(const UniqueVector&) = delete;
UniqueVector(UniqueVector&& other) :
Deleter(std::move(static_cast<Deleter&>(other))),
m_value(other.release())
{}
~UniqueVector()
{
std::for_each(m_value.begin(), m_value.end(), [this](auto x) { Deleter::destroy(x); });
}
UniqueVector& operator=(const UniqueVector&) = delete;
UniqueVector& operator=(UniqueVector&& other)
{
reset(other.release());
*static_cast<Deleter*>(this) = std::move(static_cast<Deleter&>(other));
return *this;
}
T& operator[](size_type i)
{
return m_value[i];
}
const T& operator[](size_type i) const
{
return m_value[i];
}
explicit operator bool() const
{
return m_value.operator bool();
}
const Vector* operator->() const
{
return &m_value;
}
Vector* operator->()
{
return &m_value;
}
const Vector& operator*() const
{
return m_value;
}
Vector& operator*()
{
return m_value;
}
const Vector& get() const
{
return m_value;
}
Vector& get()
{
return m_value;
}
void reset(Vector value = Vector())
{
std::for_each(m_value.begin(), m_value.end(), [this](auto x) { Deleter::destroy(x); });
m_value = std::move(value);
}
Vector release()
{
return std::exchange(m_value, Vector());
}
void swap(UniqueVector& rhs)
{
std::swap(m_value, rhs.m_value);
std::swap(static_cast<Deleter&>(*this), static_cast<Deleter&>(rhs));
}
};
namespace std
{
template<typename T>
void swap(UniqueVector<T>& lhs, UniqueVector<T>& rhs)
{
lhs.swap(rhs);
}
}
namespace vk
{
template<typename T>
class VmaDestroy {};
template<>
class VmaDestroy<NoParent>
{
public:
VmaDestroy(Optional<const AllocationCallbacks> = nullptr) {}
protected:
void destroy(VmaAllocator allocator)
{
vmaDestroyAllocator(allocator);
}
};
template<>
class VmaDestroy<VmaAllocator>
{
public:
VmaDestroy(VmaAllocator allocator = nullptr, Optional<const AllocationCallbacks> = nullptr) : m_allocator(allocator) {}
protected:
void destroy(const VmaAlloc<vk::Buffer>& alloc)
{
vmaDestroyBuffer(m_allocator, static_cast<VkBuffer>(alloc.value), alloc.allocation);
}
void destroy(const VmaAlloc<vk::Image>& alloc)
{
vmaDestroyImage(m_allocator, static_cast<VkImage>(alloc.value), alloc.allocation);
}
private:
VmaAllocator m_allocator;
};
template<typename Dispatch>
struct UniqueHandleTraits<VmaAllocator,Dispatch>
{
using deleter = VmaDestroy<NoParent>;
};
template<typename T,typename Dispatch>
struct UniqueHandleTraits<VmaAlloc<T>,Dispatch>
{
using deleter = VmaDestroy<VmaAllocator>;
};
}
size_t align_offset(size_t offset, size_t alignment);
#endif