-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsybok.hpp
2312 lines (1978 loc) · 72 KB
/
sybok.hpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 David Robillard <[email protected]>
// SPDX-License-Identifier: ISC
/**
@file sybok.hpp
@brief A minimal C++ wrapper for the Vulkan API.
This is a manually-written minimal wrapper for Vulkan. It makes working
with Vulkan a little easier in C++, but takes a different approach than
vulkan.hpp. In particular:
- Works nicely with dynamic loading. Since the API itself is an object, it
is simple to ensure the dynamically loaded API (or a consistent API in
general) is used everywhere. Passing a dispatch parameter to every
function as in vulkan.hpp makes dynamic loading extremely painful (not to
mention ugly), and mistakes tend to become link time errors. This is, in
my opinion, a glaring design flaw, and the real reason why this wrapper
reluctantly exists.
- Explicit separation of the initial API that does not require an instance
to load, from the rest of the API that does.
- Opinionated use of scoped handles everywhere.
- Remains close to the C API so that code can be easily ported. This means
that the pattern of return codes with output parameters is preserved,
except with smart handles that make leaks impossible. While less pretty,
this does not require exceptions.
- No exceptions or RTTI required.
- A safe scoped API for commands that encodes the semantics of the Vulkan
API. For example, it is statically impossible to call render scope
commands while not in a render scope.
- A reasonable amount of relatively readable code.
On the other hand, there are far fewer niceties, and the C API is used
directly as much as possible, particularly for structs (although they are
taken by const reference so they can be written inline). There is only
support for a minimal portable subset of Vulkan 1.1 with a few portable KHR
extensions.
In short, if the above sounds appealing, or you want a minimal wrapper that
can be extended if necessary to suit your application, you might find this
useful. If you want a fully-featured wrapper for Vulkan and don't care
about linker dependencies, you probably won't.
*/
#ifndef SYBOK_HPP
#define SYBOK_HPP
#ifdef VULKAN_CORE_H_
# error "sybok.hpp must be included before or instead of vulkan headers"
#endif
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wswitch-enum"
#endif
#define VK_NO_PROTOTYPES
// On 64-bit platforms, all handles are "dispatchable" pointers
#if defined(__LP64__) || defined(_WIN64) || \
(defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || \
defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || \
defined(__powerpc64__)
# define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) \
typedef struct object##_T* object; // NOLINT(bugprone-macro-parentheses)
// On 32-bit platforms, some "non-dispatchable" handles are 64 bit integers
#else
/// Trivial wrapper class for a 64-bit integer handle for type safety
template<class Tag>
struct NonDispatchableHandle {
explicit operator uint64_t() const noexcept { return handle; }
explicit operator bool() const noexcept { return handle; }
uint64_t handle;
};
# define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) \
using object = NonDispatchableHandle<struct Sk##object##Tag>;
#endif
#include <vulkan/vulkan_core.h> // IWYU pragma: export
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <utility>
#if __cplusplus >= 201703L
# define SYBOK_NODISCARD [[nodiscard]]
#elif defined(__GNUC__)
# define SYBOK_NODISCARD [[gnu::warn_unused_result]]
#else
# define SYBOK_NODISCARD
#endif
/// Helper macro to make array arguments format nicely
#define SK_COUNTED(count, ...) count, __VA_ARGS__
namespace sk {
class CommandScope;
class RenderCommandScope;
inline const char*
string(const VkResult result)
{
switch (result) {
case VK_SUCCESS:
return "Success";
case VK_NOT_READY:
return "Not Ready";
case VK_TIMEOUT:
return "Timeout";
case VK_EVENT_SET:
return "Event set";
case VK_EVENT_RESET:
return "Event reset";
case VK_INCOMPLETE:
return "Incomplete";
case VK_ERROR_OUT_OF_HOST_MEMORY:
return "Out of host memory";
case VK_ERROR_OUT_OF_DEVICE_MEMORY:
return "Out of device memory";
case VK_ERROR_INITIALIZATION_FAILED:
return "Initialization failed";
case VK_ERROR_DEVICE_LOST:
return "Device lost";
case VK_ERROR_MEMORY_MAP_FAILED:
return "Memory map failed";
case VK_ERROR_LAYER_NOT_PRESENT:
return "Layer not present";
case VK_ERROR_EXTENSION_NOT_PRESENT:
return "Extension not present";
case VK_ERROR_FEATURE_NOT_PRESENT:
return "Feature not present";
case VK_ERROR_INCOMPATIBLE_DRIVER:
return "Incompatible driver";
case VK_ERROR_TOO_MANY_OBJECTS:
return "Too many objects";
case VK_ERROR_FORMAT_NOT_SUPPORTED:
return "Format not supported";
case VK_ERROR_FRAGMENTED_POOL:
return "Fragmented pool";
case VK_ERROR_OUT_OF_POOL_MEMORY: // Vulkan 1.1
return "Out of pool memory";
case VK_ERROR_INVALID_EXTERNAL_HANDLE: // Vulkan 1.1
return "Invalid external handle";
case VK_ERROR_SURFACE_LOST_KHR: // VK_KHR_surface
return "Surface lost";
case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: // VK_KHR_surface
return "Native window in use";
case VK_SUBOPTIMAL_KHR: // VK_KHR_swapchain
return "Suboptimal";
case VK_ERROR_OUT_OF_DATE_KHR: // VK_KHR_swapchain
return "Out of date";
case VK_ERROR_VALIDATION_FAILED_EXT: // VK_EXT_debug_report
return "Validation failed";
default:
break;
}
return "Unknown error";
}
inline const char*
string(const VkPresentModeKHR presentMode)
{
switch (presentMode) {
case VK_PRESENT_MODE_IMMEDIATE_KHR:
return "Immediate";
case VK_PRESENT_MODE_MAILBOX_KHR:
return "Mailbox";
case VK_PRESENT_MODE_FIFO_KHR:
return "FIFO";
case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
return "Relaxed FIFO";
default:
break;
}
return "Unknown present mode";
}
inline const char*
string(const VkDebugReportFlagBitsEXT flag)
{
switch (flag) {
case VK_DEBUG_REPORT_INFORMATION_BIT_EXT:
return "Information";
case VK_DEBUG_REPORT_WARNING_BIT_EXT:
return "Warning";
case VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT:
return "Performance Warning";
case VK_DEBUG_REPORT_ERROR_BIT_EXT:
return "Error";
case VK_DEBUG_REPORT_DEBUG_BIT_EXT:
return "Debug";
default:
break;
}
return "Unknown report";
}
template<class T>
class GlobalDeleter
{
public:
using DestroyFunc = void (*)(T, const VkAllocationCallbacks*);
GlobalDeleter() = default;
~GlobalDeleter() = default;
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
GlobalDeleter(DestroyFunc destroyFunc) noexcept
: _destroyFunc{destroyFunc}
{}
GlobalDeleter(const GlobalDeleter&) = delete;
GlobalDeleter& operator=(const GlobalDeleter&) = delete;
GlobalDeleter(GlobalDeleter&& other) noexcept
{
std::swap(_destroyFunc, other._destroyFunc);
}
GlobalDeleter& operator=(GlobalDeleter&& other) noexcept
{
std::swap(_destroyFunc, other._destroyFunc);
return *this;
}
void operator()(T handle) noexcept
{
if (_destroyFunc && handle) {
_destroyFunc(handle, nullptr);
}
}
private:
DestroyFunc _destroyFunc{};
};
template<class T, class Parent>
class DependantDeleter
{
public:
using DestroyFunc = void (*)(Parent, T, const VkAllocationCallbacks*);
DependantDeleter() = default;
~DependantDeleter() = default;
DependantDeleter(Parent parent, DestroyFunc destroyFunc) noexcept
: _parent{parent}
, _destroyFunc{destroyFunc}
{}
DependantDeleter(const DependantDeleter&) = delete;
DependantDeleter& operator=(const DependantDeleter&) = delete;
DependantDeleter(DependantDeleter&& other) noexcept { swap(other); }
DependantDeleter& operator=(DependantDeleter&& other) noexcept
{
swap(other);
return *this;
}
void operator()(T handle) noexcept
{
if (_parent && _destroyFunc && handle) {
_destroyFunc(_parent, handle, nullptr);
}
}
private:
void swap(DependantDeleter& other) noexcept
{
std::swap(_parent, other._parent);
std::swap(_destroyFunc, other._destroyFunc);
}
Parent _parent{};
DestroyFunc _destroyFunc{};
};
template<class T, class Pool, class FreeFuncResult>
class PoolDeleter
{
public:
using FreeFunc = FreeFuncResult (*)(VkDevice, Pool, uint32_t, const T*);
PoolDeleter() noexcept = default;
~PoolDeleter() noexcept = default;
PoolDeleter(VkDevice device,
Pool pool,
uint32_t count,
FreeFunc freeFunc) noexcept
: _device{device}
, _pool{pool}
, _count{count}
, _freeFunc{freeFunc}
{}
PoolDeleter(const PoolDeleter&) = delete;
PoolDeleter& operator=(const PoolDeleter&) = delete;
PoolDeleter(PoolDeleter&& other) noexcept { swap(other); }
PoolDeleter& operator=(PoolDeleter&& other) noexcept
{
swap(other);
return *this;
}
void operator()(T* handle) noexcept
{
if (_device && _pool && handle) {
_freeFunc(_device, _pool, _count, handle);
}
}
private:
void swap(PoolDeleter& other) noexcept
{
std::swap(_device, other._device);
std::swap(_pool, other._pool);
std::swap(_count, other._count);
std::swap(_freeFunc, other._freeFunc);
}
VkDevice _device{};
Pool _pool{};
uint32_t _count{};
FreeFunc _freeFunc{};
};
template<class T, class TDeleter>
class UniqueDispatchableHandle
{
public:
using Deleter = TDeleter;
using Handle = T;
static_assert(std::is_pointer<T>::value, "");
UniqueDispatchableHandle() = default;
UniqueDispatchableHandle(Handle handle, Deleter deleter) noexcept
: _handle{handle}
, _deleter{std::move(deleter)}
{}
~UniqueDispatchableHandle() noexcept
{
if (_handle) {
_deleter(_handle);
}
}
UniqueDispatchableHandle(const UniqueDispatchableHandle&) noexcept = delete;
UniqueDispatchableHandle& operator=(
const UniqueDispatchableHandle&) noexcept = delete;
UniqueDispatchableHandle(UniqueDispatchableHandle&& other) noexcept
{
swap(other);
}
UniqueDispatchableHandle& operator=(UniqueDispatchableHandle&& other) noexcept
{
swap(other);
return *this;
}
const Handle& get() const noexcept { return _handle; }
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
operator Handle() const noexcept { return _handle; }
private:
void swap(UniqueDispatchableHandle& other) noexcept
{
std::swap(_handle, other._handle);
std::swap(_deleter, other._deleter);
}
Handle _handle{};
Deleter _deleter{};
};
#if defined(__LP64__) || defined(_WIN64) || \
(defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || \
defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || \
defined(__powerpc64__)
template<class T, class TDeleter>
using UniqueNonDispatchableHandle = UniqueDispatchableHandle<T, TDeleter>;
#else
template<class T, class TDeleter>
class UniqueNonDispatchableHandle
{
public:
using Deleter = TDeleter;
using Handle = T;
UniqueNonDispatchableHandle() = default;
UniqueNonDispatchableHandle(T handle, Deleter deleter) noexcept
: _handle{handle}
, _deleter{std::move(deleter)}
{
assert(handle);
}
~UniqueNonDispatchableHandle() noexcept
{
if (_handle) {
_deleter(_handle);
}
}
UniqueNonDispatchableHandle(const UniqueNonDispatchableHandle&) noexcept =
delete;
UniqueNonDispatchableHandle& operator=(
const UniqueNonDispatchableHandle&) noexcept = delete;
UniqueNonDispatchableHandle(UniqueNonDispatchableHandle&& other) noexcept
{
swap(other);
}
UniqueNonDispatchableHandle& operator=(
UniqueNonDispatchableHandle&& other) noexcept
{
swap(other);
return *this;
}
const Handle& get() const noexcept { return _handle; }
operator Handle() const noexcept { return _handle; }
private:
void swap(UniqueNonDispatchableHandle& other) noexcept
{
std::swap(_handle, other._handle);
std::swap(_deleter, other._deleter);
}
T _handle{};
Deleter _deleter{};
};
#endif
template<class Vector, class Deleter>
class UniqueArrayHandle
{
public:
using T = typename Vector::value_type;
UniqueArrayHandle() = default;
UniqueArrayHandle(uint32_t size, Vector&& array, Deleter deleter) noexcept
: _array{std::move(array)}
, _deleter{std::move(deleter)}
, _size{size}
{
assert(!_array.empty());
}
~UniqueArrayHandle() noexcept
{
if (!_array.empty()) {
_deleter(_array.data());
}
}
UniqueArrayHandle(const UniqueArrayHandle&) noexcept = delete;
UniqueArrayHandle& operator=(const UniqueArrayHandle&) noexcept = delete;
UniqueArrayHandle(UniqueArrayHandle&& other) noexcept { swap(other); }
UniqueArrayHandle& operator=(UniqueArrayHandle&& other) noexcept
{
swap(other);
return *this;
}
const T& operator[](const size_t index) const noexcept
{
return _array[index];
}
T& operator[](const size_t index) noexcept { return _array[index]; }
const T* get() const noexcept { return _array.data(); }
T* get() noexcept { return _array.data(); }
private:
void swap(UniqueArrayHandle& other) noexcept
{
std::swap(_array, other._array);
std::swap(_deleter, other._deleter);
std::swap(_size, other._size);
}
Vector _array{};
Deleter _deleter{};
uint32_t _size{};
};
template<typename T>
class OptionalParameter
{
public:
using Handle = typename T::Handle;
// NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
OptionalParameter(const T& value) noexcept
: _handle{value.get()}
{}
OptionalParameter() noexcept = default;
~OptionalParameter() noexcept = default;
OptionalParameter(const OptionalParameter&) = delete;
OptionalParameter& operator=(const OptionalParameter&) = delete;
OptionalParameter(OptionalParameter&&) = delete;
OptionalParameter& operator=(OptionalParameter&&) = delete;
Handle get() const noexcept { return _handle; }
private:
Handle _handle{};
};
template<typename T>
using GlobalObject = UniqueDispatchableHandle<T, GlobalDeleter<T>>;
template<typename T>
using InstanceChild =
UniqueNonDispatchableHandle<T, DependantDeleter<T, VkInstance>>;
template<typename T>
using DispatchableDeviceChild =
UniqueDispatchableHandle<T, DependantDeleter<T, VkDevice>>;
template<typename T>
using NonDispatchableDeviceChild =
UniqueNonDispatchableHandle<T, DependantDeleter<T, VkDevice>>;
template<typename Vector, typename Pool, typename FreeFuncResult>
using PoolChild = UniqueArrayHandle<
Vector,
PoolDeleter<typename Vector::value_type, Pool, FreeFuncResult>>;
using Device = GlobalObject<VkDevice>;
using Instance = GlobalObject<VkInstance>;
using PhysicalDevice = VkPhysicalDevice; // Weak handle, no destroy function
using Queue = VkQueue; // Weak handle, no destroy function
using Buffer = NonDispatchableDeviceChild<VkBuffer>;
using BufferView = NonDispatchableDeviceChild<VkBufferView>;
using CommandBuffer = DispatchableDeviceChild<VkCommandBuffer>;
using CommandPool = NonDispatchableDeviceChild<VkCommandPool>;
using DescriptorPool = NonDispatchableDeviceChild<VkDescriptorPool>;
using DescriptorSetLayout = NonDispatchableDeviceChild<VkDescriptorSetLayout>;
using DeviceMemory = NonDispatchableDeviceChild<VkDeviceMemory>;
using Event = NonDispatchableDeviceChild<VkEvent>;
using Fence = NonDispatchableDeviceChild<VkFence>;
using Framebuffer = NonDispatchableDeviceChild<VkFramebuffer>;
using Image = NonDispatchableDeviceChild<VkImage>;
using ImageView = NonDispatchableDeviceChild<VkImageView>;
using Pipeline = NonDispatchableDeviceChild<VkPipeline>;
using PipelineCache = NonDispatchableDeviceChild<VkPipelineCache>;
using PipelineLayout = NonDispatchableDeviceChild<VkPipelineLayout>;
using QueryPool = NonDispatchableDeviceChild<VkQueryPool>;
using RenderPass = NonDispatchableDeviceChild<VkRenderPass>;
using Sampler = NonDispatchableDeviceChild<VkSampler>;
using Semaphore = NonDispatchableDeviceChild<VkSemaphore>;
using ShaderModule = NonDispatchableDeviceChild<VkShaderModule>;
template<class VkCommandBufferVector>
using CommandBuffers = PoolChild<VkCommandBufferVector, VkCommandPool, void>;
template<class VkDescriptorSetVector>
using DescriptorSets =
PoolChild<VkDescriptorSetVector, VkDescriptorPool, VkResult>;
// VK_KHR_swapchain
using SwapchainKHR = NonDispatchableDeviceChild<VkSwapchainKHR>;
// VK_KHR_surface
using SurfaceKHR = InstanceChild<VkSurfaceKHR>;
// VK_EXT_debug_report
using DebugReportCallbackEXT = InstanceChild<VkDebugReportCallbackEXT>;
template<size_t...>
struct IndexSequence {};
template<size_t N, size_t... Next>
struct IndexSequenceHelper
: public IndexSequenceHelper<N - 1U, N - 1U, Next...> {};
template<size_t... Next>
struct IndexSequenceHelper<0U, Next...> {
using type = IndexSequence<Next...>;
};
template<size_t N>
using makeIndexSequence = typename IndexSequenceHelper<N>::type;
template<class T, class Parent, class DestroyFunc, size_t count, size_t... Is>
std::array<T, count>
make_handle_array_h(Parent parent,
DestroyFunc destroyFunc,
std::array<typename T::Handle, count> handles,
IndexSequence<Is...>) noexcept
{
return {T{handles[Is], {parent, destroyFunc}}...};
}
template<class T, class Parent, class DestroyFunc, size_t count>
std::array<T, count>
make_handle_array(Parent parent,
DestroyFunc destroyFunc,
std::array<typename T::Handle, count> handles) noexcept
{
return make_handle_array_h<T, Parent, DestroyFunc, count>(
parent, destroyFunc, handles, makeIndexSequence<count>());
}
namespace detail {
template<class Value, class Vector, class Func, class... Args>
inline VkResult
wrapVectorAccessor(Vector& vector, Func func, Args... args) noexcept
{
uint32_t count = 0U;
VkResult r = func(args..., &count, nullptr);
if (r > VK_INCOMPLETE) {
vector.clear();
return r;
}
vector = Vector(count);
if ((r = func(args..., &count, vector.data()))) {
vector.clear();
return r;
}
return VK_SUCCESS;
}
} // namespace detail
class VulkanApi;
struct MappedMemory {
MappedMemory() noexcept = default;
MappedMemory(const VulkanApi& api,
VkDevice device,
VkDeviceMemory memory,
void* data) noexcept
: _api{&api}
, _device{device}
, _memory{memory}
, _data{data}
{}
MappedMemory(const MappedMemory&) = delete;
MappedMemory& operator=(const MappedMemory&) = delete;
MappedMemory(MappedMemory&& mappedMemory) noexcept
: _api{mappedMemory._api}
, _device{mappedMemory._device}
, _memory{mappedMemory._memory}
, _data{mappedMemory._data}
{
mappedMemory._device = {};
mappedMemory._memory = {};
mappedMemory._data = {};
}
MappedMemory& operator=(MappedMemory&& mappedMemory) noexcept
{
std::swap(_api, mappedMemory._api);
std::swap(_device, mappedMemory._device);
std::swap(_memory, mappedMemory._memory);
std::swap(_data, mappedMemory._data);
return *this;
}
~MappedMemory() noexcept;
const void* get() const noexcept { return _data; }
void* get() noexcept { return _data; }
private:
const VulkanApi* _api{};
VkDevice _device{};
VkDeviceMemory _memory{};
void* _data{};
};
class VulkanInitApi
{
public:
template<typename NotFoundFunc>
VkResult init(PFN_vkGetInstanceProcAddr pGetInstanceProcAddr,
NotFoundFunc notFound) noexcept
{
#define SK_INIT(name) \
do { \
if (!(name = PFN_##name(getInstanceProcAddr(NULL, #name)))) { \
notFound(#name); \
} \
} while (0)
vkGetInstanceProcAddr = pGetInstanceProcAddr;
SK_INIT(vkCreateInstance);
vkDestroyInstance = {}; // Loaded after we create an instance
SK_INIT(vkEnumerateInstanceExtensionProperties);
SK_INIT(vkEnumerateInstanceLayerProperties);
if (!vkCreateInstance || !vkEnumerateInstanceExtensionProperties ||
!vkEnumerateInstanceLayerProperties) {
return VK_ERROR_INITIALIZATION_FAILED;
}
return VK_SUCCESS;
#undef SK_INIT
}
VkResult init(PFN_vkGetInstanceProcAddr pGetInstanceProcAddr) noexcept
{
return init(pGetInstanceProcAddr, [](const char*) {});
}
PFN_vkVoidFunction getInstanceProcAddr(VkInstance instance,
const char* const name) const noexcept
{
return vkGetInstanceProcAddr(instance, name);
}
VkResult createInstance(const VkInstanceCreateInfo& createInfo,
Instance& instance) noexcept
{
VkInstance h = {};
if (const VkResult r = vkCreateInstance(&createInfo, nullptr, &h)) {
return r;
}
if (!h) {
// Shouldn't actually happen, but this lets the compiler know that
return VK_ERROR_INITIALIZATION_FAILED;
}
if (!vkDestroyInstance) {
vkDestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
getInstanceProcAddr(instance, "vkDestroyInstance"));
}
instance = {h, {vkDestroyInstance}};
return VK_SUCCESS;
}
template<class Vector>
VkResult enumerateInstanceExtensionProperties(
Vector& properties) const noexcept
{
return detail::wrapVectorAccessor<VkExtensionProperties>(
properties, vkEnumerateInstanceExtensionProperties, nullptr);
}
template<class Vector>
VkResult enumerateInstanceExtensionProperties(
const char* const layerName,
Vector& properties) const noexcept
{
return detail::wrapVectorAccessor<VkExtensionProperties>(
properties, vkEnumerateInstanceExtensionProperties, layerName);
}
template<class Vector>
VkResult enumerateInstanceLayerProperties(Vector& properties) const noexcept
{
return detail::wrapVectorAccessor<VkLayerProperties>(
properties, vkEnumerateInstanceLayerProperties);
}
private:
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr{};
#define SK_FUNC(name) \
PFN_##name name {}
SK_FUNC(vkCreateInstance);
SK_FUNC(vkDestroyInstance);
SK_FUNC(vkEnumerateInstanceExtensionProperties);
SK_FUNC(vkEnumerateInstanceLayerProperties);
#undef SK_FUNC
};
class VulkanApi
{
public:
template<typename NotFoundFunc>
VkResult init(const VulkanInitApi& initApi,
const Instance& instance,
NotFoundFunc notFound) noexcept
{
VkResult r = VK_SUCCESS;
const auto notFoundWrapper = [&r, notFound](const char* name) {
r = VK_INCOMPLETE;
notFound(name);
};
#define SK_INIT(name) \
do { \
if (!(name = PFN_##name(initApi.getInstanceProcAddr(instance, #name)))) { \
notFoundWrapper(#name); \
} \
} while (0)
SK_INIT(vkAllocateCommandBuffers);
SK_INIT(vkAllocateDescriptorSets);
SK_INIT(vkAllocateMemory);
SK_INIT(vkBeginCommandBuffer);
SK_INIT(vkBindBufferMemory);
SK_INIT(vkBindImageMemory);
SK_INIT(vkCmdBeginQuery);
SK_INIT(vkCmdBeginRenderPass);
SK_INIT(vkCmdBindDescriptorSets);
SK_INIT(vkCmdBindIndexBuffer);
SK_INIT(vkCmdBindPipeline);
SK_INIT(vkCmdBindVertexBuffers);
SK_INIT(vkCmdBlitImage);
SK_INIT(vkCmdClearAttachments);
SK_INIT(vkCmdClearColorImage);
SK_INIT(vkCmdClearDepthStencilImage);
SK_INIT(vkCmdCopyBuffer);
SK_INIT(vkCmdCopyBufferToImage);
SK_INIT(vkCmdCopyImage);
SK_INIT(vkCmdCopyImageToBuffer);
SK_INIT(vkCmdCopyQueryPoolResults);
SK_INIT(vkCmdDispatch);
SK_INIT(vkCmdDispatchIndirect);
SK_INIT(vkCmdDraw);
SK_INIT(vkCmdDrawIndexed);
SK_INIT(vkCmdDrawIndexedIndirect);
SK_INIT(vkCmdDrawIndirect);
SK_INIT(vkCmdEndQuery);
SK_INIT(vkCmdEndRenderPass);
SK_INIT(vkCmdExecuteCommands);
SK_INIT(vkCmdFillBuffer);
SK_INIT(vkCmdNextSubpass);
SK_INIT(vkCmdPipelineBarrier);
SK_INIT(vkCmdPushConstants);
SK_INIT(vkCmdResetEvent);
SK_INIT(vkCmdResetQueryPool);
SK_INIT(vkCmdResolveImage);
SK_INIT(vkCmdSetBlendConstants);
SK_INIT(vkCmdSetDepthBias);
SK_INIT(vkCmdSetDepthBounds);
SK_INIT(vkCmdSetEvent);
SK_INIT(vkCmdSetLineWidth);
SK_INIT(vkCmdSetScissor);
SK_INIT(vkCmdSetStencilCompareMask);
SK_INIT(vkCmdSetStencilReference);
SK_INIT(vkCmdSetStencilWriteMask);
SK_INIT(vkCmdSetViewport);
SK_INIT(vkCmdUpdateBuffer);
SK_INIT(vkCmdWaitEvents);
SK_INIT(vkCmdWriteTimestamp);
SK_INIT(vkCreateBuffer);
SK_INIT(vkCreateBufferView);
SK_INIT(vkCreateCommandPool);
SK_INIT(vkCreateComputePipelines);
SK_INIT(vkCreateDescriptorPool);
SK_INIT(vkCreateDescriptorSetLayout);
SK_INIT(vkCreateDevice);
SK_INIT(vkCreateEvent);
SK_INIT(vkCreateFence);
SK_INIT(vkCreateFramebuffer);
SK_INIT(vkCreateGraphicsPipelines);
SK_INIT(vkCreateImage);
SK_INIT(vkCreateImageView);
SK_INIT(vkCreateInstance);
SK_INIT(vkCreatePipelineCache);
SK_INIT(vkCreatePipelineLayout);
SK_INIT(vkCreateQueryPool);
SK_INIT(vkCreateRenderPass);
SK_INIT(vkCreateSampler);
SK_INIT(vkCreateSemaphore);
SK_INIT(vkCreateShaderModule);
SK_INIT(vkDestroyBuffer);
SK_INIT(vkDestroyBufferView);
SK_INIT(vkDestroyCommandPool);
SK_INIT(vkDestroyDescriptorPool);
SK_INIT(vkDestroyDescriptorSetLayout);
SK_INIT(vkDestroyDevice);
SK_INIT(vkDestroyEvent);
SK_INIT(vkDestroyFence);
SK_INIT(vkDestroyFramebuffer);
SK_INIT(vkDestroyImage);
SK_INIT(vkDestroyImageView);
SK_INIT(vkDestroyPipeline);
SK_INIT(vkDestroyPipelineCache);
SK_INIT(vkDestroyPipelineLayout);
SK_INIT(vkDestroyQueryPool);
SK_INIT(vkDestroyRenderPass);
SK_INIT(vkDestroySampler);
SK_INIT(vkDestroySemaphore);
SK_INIT(vkDestroyShaderModule);
SK_INIT(vkDeviceWaitIdle);
SK_INIT(vkEndCommandBuffer);
SK_INIT(vkEnumerateDeviceExtensionProperties);
SK_INIT(vkEnumerateDeviceLayerProperties);
SK_INIT(vkEnumeratePhysicalDevices);
SK_INIT(vkFlushMappedMemoryRanges);
SK_INIT(vkFreeCommandBuffers);
SK_INIT(vkFreeDescriptorSets);
SK_INIT(vkFreeMemory);
SK_INIT(vkGetBufferMemoryRequirements);
SK_INIT(vkGetDeviceMemoryCommitment);
SK_INIT(vkGetDeviceProcAddr);
SK_INIT(vkGetDeviceQueue);
SK_INIT(vkGetEventStatus);
SK_INIT(vkGetFenceStatus);
SK_INIT(vkGetImageMemoryRequirements);
SK_INIT(vkGetImageSparseMemoryRequirements);
SK_INIT(vkGetImageSubresourceLayout);
SK_INIT(vkGetInstanceProcAddr);
SK_INIT(vkGetPhysicalDeviceFeatures);
SK_INIT(vkGetPhysicalDeviceFormatProperties);
SK_INIT(vkGetPhysicalDeviceImageFormatProperties);
SK_INIT(vkGetPhysicalDeviceMemoryProperties);
SK_INIT(vkGetPhysicalDeviceProperties);
SK_INIT(vkGetPhysicalDeviceQueueFamilyProperties);
SK_INIT(vkGetPhysicalDeviceSparseImageFormatProperties);
SK_INIT(vkGetPipelineCacheData);
SK_INIT(vkGetQueryPoolResults);
SK_INIT(vkGetRenderAreaGranularity);
SK_INIT(vkInvalidateMappedMemoryRanges);
SK_INIT(vkMapMemory);
SK_INIT(vkMergePipelineCaches);
SK_INIT(vkQueueBindSparse);
SK_INIT(vkQueueSubmit);
SK_INIT(vkQueueWaitIdle);
SK_INIT(vkResetCommandBuffer);
SK_INIT(vkResetCommandPool);
SK_INIT(vkResetDescriptorPool);
SK_INIT(vkResetEvent);
SK_INIT(vkResetFences);
SK_INIT(vkSetEvent);
SK_INIT(vkUnmapMemory);
SK_INIT(vkUpdateDescriptorSets);
SK_INIT(vkWaitForFences);
// VK_EXT_debug_report
SK_INIT(vkCreateDebugReportCallbackEXT);
SK_INIT(vkDebugReportMessageEXT);
SK_INIT(vkDestroyDebugReportCallbackEXT);
// VK_KHR_surface
SK_INIT(vkDestroySurfaceKHR);
SK_INIT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
SK_INIT(vkGetPhysicalDeviceSurfaceFormatsKHR);
SK_INIT(vkGetPhysicalDeviceSurfacePresentModesKHR);
SK_INIT(vkGetPhysicalDeviceSurfaceSupportKHR);
// VK_KHR_swapchain
SK_INIT(vkAcquireNextImageKHR);
SK_INIT(vkCreateSwapchainKHR);
SK_INIT(vkDestroySwapchainKHR);
SK_INIT(vkGetDeviceGroupPresentCapabilitiesKHR);
SK_INIT(vkGetDeviceGroupSurfacePresentModesKHR);