forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vktestbinding.h
1117 lines (914 loc) · 43.4 KB
/
vktestbinding.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
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 (c) 2015-2016, 2020-2021 The Khronos Group Inc.
* Copyright (c) 2015-2016, 2020-2021 Valve Corporation
* Copyright (c) 2015-2016, 2020-2021 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
* Author: Cody Northrop <cody@lunarg.com>
* Author: John Zulauf <jzulauf@lunarg.com>
*/
#ifndef VKTESTBINDING_H
#define VKTESTBINDING_H
#include <algorithm>
#include <cassert>
#include <iterator>
#include <memory>
#include <vector>
#include "lvt_function_pointers.h"
#include "test_common.h"
namespace vk_testing {
template <class Dst, class Src>
std::vector<Dst> MakeVkHandles(const std::vector<Src> &v) {
std::vector<Dst> handles;
handles.reserve(v.size());
std::transform(v.begin(), v.end(), std::back_inserter(handles), [](const Src &o) { return o.handle(); });
return handles;
}
template <class Dst, class Src>
std::vector<Dst> MakeVkHandles(const std::vector<Src *> &v) {
std::vector<Dst> handles;
handles.reserve(v.size());
std::transform(v.begin(), v.end(), std::back_inserter(handles), [](const Src *o) { return o->handle(); });
return handles;
}
typedef void (*ErrorCallback)(const char *expr, const char *file, unsigned int line, const char *function);
void set_error_callback(ErrorCallback callback);
class PhysicalDevice;
class Device;
class Queue;
class DeviceMemory;
class Fence;
class Semaphore;
class Event;
class QueryPool;
class Buffer;
class BufferView;
class Image;
class ImageView;
class DepthStencilView;
class Shader;
class Pipeline;
class PipelineDelta;
class Sampler;
class DescriptorSetLayout;
class PipelineLayout;
class DescriptorSetPool;
class DescriptorSet;
class CommandBuffer;
class CommandPool;
std::vector<VkLayerProperties> GetGlobalLayers();
std::vector<VkExtensionProperties> GetGlobalExtensions();
std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName);
namespace internal {
template <typename T>
class Handle {
public:
const T &handle() const NOEXCEPT { return handle_; }
bool initialized() const NOEXCEPT { return (handle_ != T{}); }
protected:
typedef T handle_type;
explicit Handle() NOEXCEPT : handle_{} {}
explicit Handle(T handle) NOEXCEPT : handle_(handle) {}
// handles are non-copyable
Handle(const Handle &) = delete;
Handle &operator=(const Handle &) = delete;
// handles can be moved out
Handle(Handle &&src) NOEXCEPT : handle_{src.handle_} { src.handle_ = {}; }
Handle &operator=(Handle &&src) NOEXCEPT {
handle_ = src.handle_;
src.handle_ = {};
return *this;
}
void init(T handle) NOEXCEPT {
assert(!initialized());
handle_ = handle;
}
private:
T handle_;
};
template <typename T>
class NonDispHandle : public Handle<T> {
protected:
explicit NonDispHandle() NOEXCEPT : Handle<T>(), dev_handle_(VK_NULL_HANDLE) {}
explicit NonDispHandle(VkDevice dev, T handle) NOEXCEPT : Handle<T>(handle), dev_handle_(dev) {}
NonDispHandle(NonDispHandle &&src) NOEXCEPT : Handle<T>(std::move(src)) {
dev_handle_ = src.dev_handle_;
src.dev_handle_ = VK_NULL_HANDLE;
}
NonDispHandle &operator=(NonDispHandle &&src) NOEXCEPT {
Handle<T>::operator=(std::move(src));
dev_handle_ = src.dev_handle_;
src.dev_handle_ = VK_NULL_HANDLE;
return *this;
}
const VkDevice &device() const NOEXCEPT { return dev_handle_; }
void init(VkDevice dev, T handle) NOEXCEPT {
assert(!Handle<T>::initialized() && dev_handle_ == VK_NULL_HANDLE);
Handle<T>::init(handle);
dev_handle_ = dev;
}
private:
VkDevice dev_handle_;
};
} // namespace internal
class PhysicalDevice : public internal::Handle<VkPhysicalDevice> {
public:
explicit PhysicalDevice(VkPhysicalDevice phy) : Handle(phy) {
memory_properties_ = memory_properties();
device_properties_ = properties();
}
VkPhysicalDeviceProperties properties() const;
VkPhysicalDeviceMemoryProperties memory_properties() const;
std::vector<VkQueueFamilyProperties> queue_properties() const;
VkPhysicalDeviceFeatures features() const;
bool set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkMemoryPropertyFlags properties,
const VkMemoryPropertyFlags forbid = 0) const;
// vkEnumerateDeviceExtensionProperties()
std::vector<VkExtensionProperties> extensions(const char *pLayerName = nullptr) const;
// vkEnumerateLayers()
std::vector<VkLayerProperties> layers() const;
private:
void add_extension_dependencies(uint32_t dependency_count, VkExtensionProperties *depencency_props,
std::vector<VkExtensionProperties> &ext_list);
VkPhysicalDeviceMemoryProperties memory_properties_;
VkPhysicalDeviceProperties device_properties_;
};
class QueueCreateInfoArray {
private:
std::vector<VkDeviceQueueCreateInfo> queue_info_;
std::vector<std::vector<float>> queue_priorities_;
public:
QueueCreateInfoArray(const std::vector<VkQueueFamilyProperties> &queue_props);
size_t size() const { return queue_info_.size(); }
const VkDeviceQueueCreateInfo *data() const { return queue_info_.data(); }
};
class Device : public internal::Handle<VkDevice> {
public:
explicit Device(VkPhysicalDevice phy) : phy_(phy) {}
~Device() NOEXCEPT;
// vkCreateDevice()
void init(const VkDeviceCreateInfo &info);
void init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features = nullptr,
void *create_device_pnext = nullptr); // all queues, all extensions, etc
void init() {
std::vector<const char *> extensions;
init(extensions);
};
const PhysicalDevice &phy() const { return phy_; }
std::vector<const char *> GetEnabledExtensions() { return enabled_extensions_; }
bool IsEnabledExtension(const char *extension);
// vkGetDeviceProcAddr()
PFN_vkVoidFunction get_proc(const char *name) const { return vk::GetDeviceProcAddr(handle(), name); }
// vkGetDeviceQueue()
const std::vector<Queue *> &graphics_queues() const { return queues_[GRAPHICS]; }
const std::vector<Queue *> &compute_queues() { return queues_[COMPUTE]; }
const std::vector<Queue *> &dma_queues() { return queues_[DMA]; }
typedef std::vector<std::unique_ptr<Queue>> QueueFamilyQueues;
typedef std::vector<QueueFamilyQueues> QueueFamilies;
const QueueFamilyQueues &queue_family_queues(uint32_t queue_family) const;
uint32_t graphics_queue_node_index_;
struct Format {
VkFormat format;
VkImageTiling tiling;
VkFlags features;
};
// vkGetFormatInfo()
VkFormatProperties format_properties(VkFormat format);
const std::vector<Format> &formats() const { return formats_; }
// vkDeviceWaitIdle()
void wait();
// vkWaitForFences()
VkResult wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout);
VkResult wait(const Fence &fence) { return wait(std::vector<const Fence *>(1, &fence), true, (uint64_t)-1); }
// vkUpdateDescriptorSets()
void update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes, const std::vector<VkCopyDescriptorSet> &copies);
void update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes) {
return update_descriptor_sets(writes, std::vector<VkCopyDescriptorSet>());
}
static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
VkDescriptorType type, uint32_t count,
const VkDescriptorImageInfo *image_info);
static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
VkDescriptorType type, uint32_t count,
const VkDescriptorBufferInfo *buffer_info);
static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
VkDescriptorType type, uint32_t count, const VkBufferView *buffer_views);
static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
VkDescriptorType type, const std::vector<VkDescriptorImageInfo> &image_info);
static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
VkDescriptorType type, const std::vector<VkDescriptorBufferInfo> &buffer_info);
static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
VkDescriptorType type, const std::vector<VkBufferView> &buffer_views);
static VkCopyDescriptorSet copy_descriptor_set(const DescriptorSet &src_set, uint32_t src_binding, uint32_t src_array_element,
const DescriptorSet &dst_set, uint32_t dst_binding, uint32_t dst_array_element,
uint32_t count);
private:
enum QueueIndex {
GRAPHICS,
COMPUTE,
DMA,
QUEUE_COUNT,
};
void init_queues(const VkDeviceCreateInfo &info);
void init_formats();
PhysicalDevice phy_;
std::vector<const char *> enabled_extensions_;
QueueFamilies queue_families_;
std::vector<Queue *> queues_[QUEUE_COUNT];
std::vector<Format> formats_;
};
class Queue : public internal::Handle<VkQueue> {
public:
explicit Queue(VkQueue queue, int index) : Handle(queue) { family_index_ = index; }
// vkQueueSubmit()
VkResult submit(const std::vector<const CommandBuffer *> &cmds, const Fence &fence, bool expect_success = true);
VkResult submit(const CommandBuffer &cmd, const Fence &fence, bool expect_success = true);
VkResult submit(const CommandBuffer &cmd, bool expect_success = true);
// vkQueueWaitIdle()
VkResult wait();
int get_family_index() { return family_index_; }
private:
int family_index_;
};
class DeviceMemory : public internal::NonDispHandle<VkDeviceMemory> {
public:
~DeviceMemory() NOEXCEPT;
// vkAllocateMemory()
void init(const Device &dev, const VkMemoryAllocateInfo &info);
// vkMapMemory()
const void *map(VkFlags flags) const;
void *map(VkFlags flags);
const void *map() const { return map(0); }
void *map() { return map(0); }
// vkUnmapMemory()
void unmap() const;
static VkMemoryAllocateInfo alloc_info(VkDeviceSize size, uint32_t memory_type_index);
static VkMemoryAllocateInfo get_resource_alloc_info(const vk_testing::Device &dev, const VkMemoryRequirements &reqs,
VkMemoryPropertyFlags mem_props);
};
class Fence : public internal::NonDispHandle<VkFence> {
public:
~Fence() NOEXCEPT;
// vkCreateFence()
void init(const Device &dev, const VkFenceCreateInfo &info);
// vkGetFenceStatus()
VkResult status() const { return vk::GetFenceStatus(device(), handle()); }
VkResult wait(uint64_t timeout) const;
static VkFenceCreateInfo create_info(VkFenceCreateFlags flags);
static VkFenceCreateInfo create_info();
};
class Semaphore : public internal::NonDispHandle<VkSemaphore> {
public:
~Semaphore() NOEXCEPT;
// vkCreateSemaphore()
void init(const Device &dev, const VkSemaphoreCreateInfo &info);
static VkSemaphoreCreateInfo create_info(VkFlags flags);
};
class Event : public internal::NonDispHandle<VkEvent> {
public:
~Event() NOEXCEPT;
// vkCreateEvent()
void init(const Device &dev, const VkEventCreateInfo &info);
// vkGetEventStatus()
// vkSetEvent()
// vkResetEvent()
VkResult status() const { return vk::GetEventStatus(device(), handle()); }
void set();
void cmd_set(const CommandBuffer &cmd, VkPipelineStageFlags stage_mask);
void cmd_reset(const CommandBuffer &cmd, VkPipelineStageFlags stage_mask);
void reset();
static VkEventCreateInfo create_info(VkFlags flags);
};
class QueryPool : public internal::NonDispHandle<VkQueryPool> {
public:
~QueryPool() NOEXCEPT;
// vkCreateQueryPool()
void init(const Device &dev, const VkQueryPoolCreateInfo &info);
// vkGetQueryPoolResults()
VkResult results(uint32_t first, uint32_t count, size_t size, void *data, size_t stride);
static VkQueryPoolCreateInfo create_info(VkQueryType type, uint32_t slot_count);
};
class Buffer : public internal::NonDispHandle<VkBuffer> {
public:
explicit Buffer() : NonDispHandle() {}
explicit Buffer(const Device &dev, const VkBufferCreateInfo &info) { init(dev, info); }
explicit Buffer(const Device &dev, VkDeviceSize size) { init(dev, size); }
~Buffer() NOEXCEPT;
// vkCreateBuffer()
void init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props);
void init(const Device &dev, const VkBufferCreateInfo &info) { init(dev, info, 0); }
void init(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags mem_props,
VkBufferUsageFlags usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, const std::vector<uint32_t> &queue_families = {}) {
init(dev, create_info(size, usage, &queue_families), mem_props);
}
void init(const Device &dev, VkDeviceSize size) { init(dev, size, 0); }
void init_as_src(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
const std::vector<uint32_t> *queue_families = nullptr) {
init(dev, create_info(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, queue_families), reqs);
}
void init_as_dst(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
const std::vector<uint32_t> *queue_families = nullptr) {
init(dev, create_info(size, VK_BUFFER_USAGE_TRANSFER_DST_BIT, queue_families), reqs);
}
void init_as_src_and_dst(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
const std::vector<uint32_t> *queue_families = nullptr, bool memory = true) {
if (memory)
init(dev, create_info(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, queue_families), reqs);
else
init_no_mem(dev,
create_info(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, queue_families));
}
void init_as_storage(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
const std::vector<uint32_t> *queue_families = nullptr) {
init(dev, create_info(size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, queue_families), reqs);
}
void init_no_mem(const Device &dev, const VkBufferCreateInfo &info);
// get the internal memory
const DeviceMemory &memory() const { return internal_mem_; }
DeviceMemory &memory() { return internal_mem_; }
// vkGetObjectMemoryRequirements()
VkMemoryRequirements memory_requirements() const;
// vkBindObjectMemory()
void bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset);
const VkBufferCreateInfo &create_info() const { return create_info_; }
static VkBufferCreateInfo create_info(VkDeviceSize size, VkFlags usage, const std::vector<uint32_t> *queue_families = nullptr);
VkBufferMemoryBarrier buffer_memory_barrier(VkFlags output_mask, VkFlags input_mask, VkDeviceSize offset,
VkDeviceSize size) const {
VkBufferMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
barrier.buffer = handle();
barrier.srcAccessMask = output_mask;
barrier.dstAccessMask = input_mask;
barrier.offset = offset;
barrier.size = size;
if (create_info_.sharingMode == VK_SHARING_MODE_CONCURRENT) {
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
}
return barrier;
}
VkBufferMemoryBarrier2KHR buffer_memory_barrier(VkPipelineStageFlags2KHR src_stage, VkPipelineStageFlags2KHR dst_stage,
VkAccessFlags2KHR src_access, VkAccessFlags2KHR dst_access, VkDeviceSize offset,
VkDeviceSize size) const {
VkBufferMemoryBarrier2KHR barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER_2_KHR;
barrier.buffer = handle();
barrier.srcStageMask = src_stage;
barrier.dstStageMask = dst_stage;
barrier.srcAccessMask = src_access;
barrier.dstAccessMask = dst_access;
barrier.offset = offset;
barrier.size = size;
if (create_info_.sharingMode == VK_SHARING_MODE_CONCURRENT) {
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
}
return barrier;
}
private:
VkBufferCreateInfo create_info_;
DeviceMemory internal_mem_;
};
class BufferView : public internal::NonDispHandle<VkBufferView> {
public:
~BufferView() NOEXCEPT;
// vkCreateBufferView()
void init(const Device &dev, const VkBufferViewCreateInfo &info);
static VkBufferViewCreateInfo createInfo(VkBuffer buffer, VkFormat format, VkDeviceSize offset = 0,
VkDeviceSize range = VK_WHOLE_SIZE);
};
inline VkBufferViewCreateInfo BufferView::createInfo(VkBuffer buffer, VkFormat format, VkDeviceSize offset, VkDeviceSize range) {
VkBufferViewCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
info.pNext = nullptr;
info.flags = VkFlags(0);
info.buffer = buffer;
info.format = format;
info.offset = offset;
info.range = range;
return info;
}
class Image : public internal::NonDispHandle<VkImage> {
public:
explicit Image() : NonDispHandle(), format_features_(0) {}
explicit Image(const Device &dev, const VkImageCreateInfo &info) : format_features_(0) { init(dev, info); }
~Image() NOEXCEPT;
// vkCreateImage()
void init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props);
void init(const Device &dev, const VkImageCreateInfo &info) { init(dev, info, 0); }
void init_no_mem(const Device &dev, const VkImageCreateInfo &info);
// get the internal memory
const DeviceMemory &memory() const { return internal_mem_; }
DeviceMemory &memory() { return internal_mem_; }
// vkGetObjectMemoryRequirements()
VkMemoryRequirements memory_requirements() const;
// vkBindObjectMemory()
void bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset);
// vkGetImageSubresourceLayout()
VkSubresourceLayout subresource_layout(const VkImageSubresource &subres) const;
VkSubresourceLayout subresource_layout(const VkImageSubresourceLayers &subres) const;
bool transparent() const;
bool copyable() const { return (format_features_ & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT); }
VkImageSubresourceRange subresource_range(VkImageAspectFlags aspect) const { return subresource_range(create_info_, aspect); }
VkExtent3D extent() const { return create_info_.extent; }
VkExtent3D extent(uint32_t mip_level) const { return extent(create_info_.extent, mip_level); }
VkFormat format() const { return create_info_.format; }
VkImageUsageFlags usage() const { return create_info_.usage; }
VkSharingMode sharing_mode() const { return create_info_.sharingMode; }
VkImageMemoryBarrier image_memory_barrier(VkFlags output_mask, VkFlags input_mask, VkImageLayout old_layout,
VkImageLayout new_layout, const VkImageSubresourceRange &range,
uint32_t srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
uint32_t dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED) const {
VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = output_mask;
barrier.dstAccessMask = input_mask;
barrier.oldLayout = old_layout;
barrier.newLayout = new_layout;
barrier.image = handle();
barrier.subresourceRange = range;
barrier.srcQueueFamilyIndex = srcQueueFamilyIndex;
barrier.dstQueueFamilyIndex = dstQueueFamilyIndex;
return barrier;
}
VkImageMemoryBarrier2KHR image_memory_barrier(VkPipelineStageFlags2KHR src_stage, VkPipelineStageFlags2KHR dst_stage,
VkAccessFlags2KHR src_access, VkAccessFlags2KHR dst_access,
VkImageLayout old_layout, VkImageLayout new_layout,
const VkImageSubresourceRange &range,
uint32_t srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
uint32_t dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED) const {
VkImageMemoryBarrier2KHR barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2_KHR;
barrier.srcStageMask = src_stage;
barrier.dstStageMask = dst_stage;
barrier.srcAccessMask = src_access;
barrier.dstAccessMask = dst_access;
barrier.oldLayout = old_layout;
barrier.newLayout = new_layout;
barrier.image = handle();
barrier.subresourceRange = range;
barrier.srcQueueFamilyIndex = srcQueueFamilyIndex;
barrier.dstQueueFamilyIndex = dstQueueFamilyIndex;
return barrier;
}
static VkImageCreateInfo create_info();
static VkImageSubresource subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer);
static VkImageSubresource subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer);
static VkImageSubresourceLayers subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer,
uint32_t array_size);
static VkImageSubresourceLayers subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer,
uint32_t array_size);
static VkImageSubresourceRange subresource_range(VkImageAspectFlags aspect_mask, uint32_t base_mip_level, uint32_t mip_levels,
uint32_t base_array_layer, uint32_t num_layers);
static VkImageSubresourceRange subresource_range(const VkImageCreateInfo &info, VkImageAspectFlags aspect_mask);
static VkImageSubresourceRange subresource_range(const VkImageSubresource &subres);
static VkExtent2D extent(int32_t width, int32_t height);
static VkExtent2D extent(const VkExtent2D &extent, uint32_t mip_level);
static VkExtent2D extent(const VkExtent3D &extent);
static VkExtent3D extent(int32_t width, int32_t height, int32_t depth);
static VkExtent3D extent(const VkExtent3D &extent, uint32_t mip_level);
private:
void init_info(const Device &dev, const VkImageCreateInfo &info);
VkImageCreateInfo create_info_;
VkFlags format_features_;
DeviceMemory internal_mem_;
};
class ImageView : public internal::NonDispHandle<VkImageView> {
public:
explicit ImageView() = default;
explicit ImageView(const Device &dev, const VkImageViewCreateInfo &info) { init(dev, info); }
~ImageView() NOEXCEPT;
// vkCreateImageView()
void init(const Device &dev, const VkImageViewCreateInfo &info);
};
class AccelerationStructure : public internal::NonDispHandle<VkAccelerationStructureNV> {
public:
explicit AccelerationStructure(const Device &dev, const VkAccelerationStructureCreateInfoNV &info, bool init_memory = true) {
init(dev, info, init_memory);
}
~AccelerationStructure();
// vkCreateAccelerationStructureNV
void init(const Device &dev, const VkAccelerationStructureCreateInfoNV &info, bool init_memory = true);
// vkGetAccelerationStructureMemoryRequirementsNV()
VkMemoryRequirements2 memory_requirements() const;
VkMemoryRequirements2 build_scratch_memory_requirements() const;
uint64_t opaque_handle() const { return opaque_handle_; }
const VkAccelerationStructureInfoNV &info() const { return info_; }
const VkDevice &dev() const { return device(); }
void create_scratch_buffer(const Device &dev, Buffer *buffer, VkBufferCreateInfo *pCreateInfo = NULL);
private:
VkAccelerationStructureInfoNV info_;
DeviceMemory memory_;
uint64_t opaque_handle_;
};
class AccelerationStructureKHR : public internal::NonDispHandle<VkAccelerationStructureKHR> {
public:
explicit AccelerationStructureKHR(const Device &dev, const VkAccelerationStructureCreateInfoKHR &info,
bool init_memory = true) {
init(dev, info, init_memory);
}
~AccelerationStructureKHR();
// vkCreateAccelerationStructureNV
void init(const Device &dev, const VkAccelerationStructureCreateInfoKHR &info, bool init_memory = true);
uint64_t opaque_handle() const { return opaque_handle_; }
const VkAccelerationStructureCreateInfoKHR &info() const { return info_; }
const VkDevice &dev() const { return device(); }
void create_scratch_buffer(const Device &dev, Buffer *buffer, VkBufferCreateInfo *pCreateInfo = NULL);
private:
VkAccelerationStructureCreateInfoKHR info_;
DeviceMemory memory_;
uint64_t opaque_handle_;
};
class ShaderModule : public internal::NonDispHandle<VkShaderModule> {
public:
~ShaderModule() NOEXCEPT;
// vkCreateShaderModule()
void init(const Device &dev, const VkShaderModuleCreateInfo &info);
VkResult init_try(const Device &dev, const VkShaderModuleCreateInfo &info);
static VkShaderModuleCreateInfo create_info(size_t code_size, const uint32_t *code, VkFlags flags);
};
class Pipeline : public internal::NonDispHandle<VkPipeline> {
public:
~Pipeline() NOEXCEPT;
// vkCreateGraphicsPipeline()
void init(const Device &dev, const VkGraphicsPipelineCreateInfo &info);
// vkCreateGraphicsPipelineDerivative()
void init(const Device &dev, const VkGraphicsPipelineCreateInfo &info, const VkPipeline basePipeline);
// vkCreateComputePipeline()
void init(const Device &dev, const VkComputePipelineCreateInfo &info);
// vkLoadPipeline()
void init(const Device &dev, size_t size, const void *data);
// vkLoadPipelineDerivative()
void init(const Device &dev, size_t size, const void *data, VkPipeline basePipeline);
// vkCreateGraphicsPipeline with error return
VkResult init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info);
// vkStorePipeline()
size_t store(size_t size, void *data);
};
class PipelineLayout : public internal::NonDispHandle<VkPipelineLayout> {
public:
PipelineLayout() NOEXCEPT : NonDispHandle() {}
~PipelineLayout() NOEXCEPT;
// Move constructor for Visual Studio 2013
PipelineLayout(PipelineLayout &&src) NOEXCEPT : NonDispHandle(std::move(src)){};
PipelineLayout &operator=(PipelineLayout &&src) NOEXCEPT {
this->~PipelineLayout();
this->NonDispHandle::operator=(std::move(src));
return *this;
};
// vCreatePipelineLayout()
void init(const Device &dev, VkPipelineLayoutCreateInfo &info, const std::vector<const DescriptorSetLayout *> &layouts);
};
class Sampler : public internal::NonDispHandle<VkSampler> {
public:
~Sampler() NOEXCEPT;
// vkCreateSampler()
void init(const Device &dev, const VkSamplerCreateInfo &info);
};
class DescriptorSetLayout : public internal::NonDispHandle<VkDescriptorSetLayout> {
public:
DescriptorSetLayout() NOEXCEPT : NonDispHandle(){};
~DescriptorSetLayout() NOEXCEPT;
// Move constructor for Visual Studio 2013
DescriptorSetLayout(DescriptorSetLayout &&src) NOEXCEPT : NonDispHandle(std::move(src)){};
DescriptorSetLayout &operator=(DescriptorSetLayout &&src) NOEXCEPT {
this->~DescriptorSetLayout();
this->NonDispHandle::operator=(std::move(src));
return *this;
}
// vkCreateDescriptorSetLayout()
void init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info);
};
class DescriptorPool : public internal::NonDispHandle<VkDescriptorPool> {
public:
~DescriptorPool() NOEXCEPT;
// Descriptor sets allocated from this pool will need access to the original
// object
VkDescriptorPool GetObj() { return pool_; }
// vkCreateDescriptorPool()
void init(const Device &dev, const VkDescriptorPoolCreateInfo &info);
// vkResetDescriptorPool()
void reset();
// vkFreeDescriptorSet()
void setDynamicUsage(bool isDynamic) { dynamic_usage_ = isDynamic; }
bool getDynamicUsage() { return dynamic_usage_; }
// vkAllocateDescriptorSets()
std::vector<DescriptorSet *> alloc_sets(const Device &dev, const std::vector<const DescriptorSetLayout *> &layouts);
std::vector<DescriptorSet *> alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count);
DescriptorSet *alloc_sets(const Device &dev, const DescriptorSetLayout &layout);
template <typename PoolSizes>
static VkDescriptorPoolCreateInfo create_info(VkDescriptorPoolCreateFlags flags, uint32_t max_sets,
const PoolSizes &pool_sizes);
private:
VkDescriptorPool pool_;
// Track whether this pool's usage is VK_DESCRIPTOR_POOL_USAGE_DYNAMIC
bool dynamic_usage_;
};
template <typename PoolSizes>
inline VkDescriptorPoolCreateInfo DescriptorPool::create_info(VkDescriptorPoolCreateFlags flags, uint32_t max_sets,
const PoolSizes &pool_sizes) {
VkDescriptorPoolCreateInfo info{};
info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
info.pNext = nullptr;
info.flags = flags;
info.maxSets = max_sets;
info.poolSizeCount = pool_sizes.size();
info.pPoolSizes = (info.poolSizeCount) ? pool_sizes.data() : nullptr;
return info;
}
class DescriptorSet : public internal::NonDispHandle<VkDescriptorSet> {
public:
~DescriptorSet() NOEXCEPT;
explicit DescriptorSet() : NonDispHandle() {}
explicit DescriptorSet(const Device &dev, DescriptorPool *pool, VkDescriptorSet set) : NonDispHandle(dev.handle(), set) {
containing_pool_ = pool;
}
private:
DescriptorPool *containing_pool_;
};
class CommandPool : public internal::NonDispHandle<VkCommandPool> {
public:
~CommandPool() NOEXCEPT;
explicit CommandPool() : NonDispHandle() {}
explicit CommandPool(const Device &dev, const VkCommandPoolCreateInfo &info) { init(dev, info); }
void init(const Device &dev, const VkCommandPoolCreateInfo &info);
static VkCommandPoolCreateInfo create_info(uint32_t queue_family_index, VkCommandPoolCreateFlags flags);
};
inline VkCommandPoolCreateInfo CommandPool::create_info(uint32_t queue_family_index, VkCommandPoolCreateFlags flags) {
VkCommandPoolCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
info.queueFamilyIndex = queue_family_index;
info.flags = flags;
return info;
}
class CommandBuffer : public internal::Handle<VkCommandBuffer> {
public:
~CommandBuffer() NOEXCEPT;
explicit CommandBuffer() : Handle() {}
explicit CommandBuffer(const Device &dev, const VkCommandBufferAllocateInfo &info) { init(dev, info); }
// vkAllocateCommandBuffers()
void init(const Device &dev, const VkCommandBufferAllocateInfo &info);
// vkBeginCommandBuffer()
void begin(const VkCommandBufferBeginInfo *info);
void begin();
// vkEndCommandBuffer()
// vkResetCommandBuffer()
void end();
void reset(VkCommandBufferResetFlags flags);
void reset() { reset(VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); }
static VkCommandBufferAllocateInfo create_info(VkCommandPool const &pool);
private:
VkDevice dev_handle_;
VkCommandPool cmd_pool_;
};
class RenderPass : public internal::NonDispHandle<VkRenderPass> {
public:
RenderPass() = default;
RenderPass(const Device &dev, const VkRenderPassCreateInfo &info) { init(dev, info); }
~RenderPass() NOEXCEPT;
// vkCreateRenderPass()
void init(const Device &dev, const VkRenderPassCreateInfo &info);
};
class Framebuffer : public internal::NonDispHandle<VkFramebuffer> {
public:
Framebuffer() = default;
Framebuffer(const Device &dev, const VkFramebufferCreateInfo &info) { init(dev, info); }
~Framebuffer() NOEXCEPT;
// vkCreateFramebuffer()
void init(const Device &dev, const VkFramebufferCreateInfo &info);
};
inline VkMemoryAllocateInfo DeviceMemory::alloc_info(VkDeviceSize size, uint32_t memory_type_index) {
VkMemoryAllocateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
info.allocationSize = size;
info.memoryTypeIndex = memory_type_index;
return info;
}
inline VkBufferCreateInfo Buffer::create_info(VkDeviceSize size, VkFlags usage, const std::vector<uint32_t> *queue_families) {
VkBufferCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
info.size = size;
info.usage = usage;
if (queue_families && queue_families->size() > 1) {
info.sharingMode = VK_SHARING_MODE_CONCURRENT;
info.queueFamilyIndexCount = static_cast<uint32_t>(queue_families->size());
info.pQueueFamilyIndices = queue_families->data();
}
return info;
}
inline VkFenceCreateInfo Fence::create_info(VkFenceCreateFlags flags) {
VkFenceCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
info.flags = flags;
return info;
}
inline VkFenceCreateInfo Fence::create_info() {
VkFenceCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
return info;
}
inline VkSemaphoreCreateInfo Semaphore::create_info(VkFlags flags) {
VkSemaphoreCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
info.flags = flags;
return info;
}
inline VkEventCreateInfo Event::create_info(VkFlags flags) {
VkEventCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
info.flags = flags;
return info;
}
inline VkQueryPoolCreateInfo QueryPool::create_info(VkQueryType type, uint32_t slot_count) {
VkQueryPoolCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
info.queryType = type;
info.queryCount = slot_count;
return info;
}
inline VkImageCreateInfo Image::create_info() {
VkImageCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
info.extent.width = 1;
info.extent.height = 1;
info.extent.depth = 1;
info.mipLevels = 1;
info.arrayLayers = 1;
info.samples = VK_SAMPLE_COUNT_1_BIT;
return info;
}
inline VkImageSubresource Image::subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer) {
VkImageSubresource subres = {};
if (aspect == 0) {
assert(!"Invalid VkImageAspectFlags");
}
subres.aspectMask = aspect;
subres.mipLevel = mip_level;
subres.arrayLayer = array_layer;
return subres;
}
inline VkImageSubresource Image::subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer) {
return subresource(range.aspectMask, range.baseMipLevel + mip_level, range.baseArrayLayer + array_layer);
}
inline VkImageSubresourceLayers Image::subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer,
uint32_t array_size) {
VkImageSubresourceLayers subres = {};
switch (aspect) {
case VK_IMAGE_ASPECT_COLOR_BIT:
case VK_IMAGE_ASPECT_DEPTH_BIT:
case VK_IMAGE_ASPECT_STENCIL_BIT:
case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
/* valid */
break;
default:
assert(!"Invalid VkImageAspectFlags");
}
subres.aspectMask = aspect;
subres.mipLevel = mip_level;
subres.baseArrayLayer = array_layer;
subres.layerCount = array_size;
return subres;
}
inline VkImageSubresourceLayers Image::subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer,
uint32_t array_size) {
return subresource(range.aspectMask, range.baseMipLevel + mip_level, range.baseArrayLayer + array_layer, array_size);
}
inline VkImageSubresourceRange Image::subresource_range(VkImageAspectFlags aspect_mask, uint32_t base_mip_level,
uint32_t mip_levels, uint32_t base_array_layer, uint32_t num_layers) {
VkImageSubresourceRange range = {};
if (aspect_mask == 0) {
assert(!"Invalid VkImageAspectFlags");
}
range.aspectMask = aspect_mask;
range.baseMipLevel = base_mip_level;
range.levelCount = mip_levels;
range.baseArrayLayer = base_array_layer;
range.layerCount = num_layers;
return range;
}
inline VkImageSubresourceRange Image::subresource_range(const VkImageCreateInfo &info, VkImageAspectFlags aspect_mask) {
return subresource_range(aspect_mask, 0, info.mipLevels, 0, info.arrayLayers);
}
inline VkImageSubresourceRange Image::subresource_range(const VkImageSubresource &subres) {
return subresource_range(subres.aspectMask, subres.mipLevel, 1, subres.arrayLayer, 1);
}
inline VkExtent2D Image::extent(int32_t width, int32_t height) {
VkExtent2D extent = {};
extent.width = width;
extent.height = height;
return extent;
}