1 /*
2 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
19 * Author: Cody Northrop <cody@lunarg.com>
20 * Author: John Zulauf <jzulauf@lunarg.com>
21 */
22
23 #ifndef VKTESTBINDING_H
24 #define VKTESTBINDING_H
25
26 #include <algorithm>
27 #include <assert.h>
28 #include <iterator>
29 #include <memory>
30 #include <vector>
31
32 #include "vulkan/vulkan.h"
33
34 namespace vk_testing {
35
36 template <class Dst, class Src>
MakeVkHandles(const std::vector<Src> & v)37 std::vector<Dst> MakeVkHandles(const std::vector<Src> &v) {
38 std::vector<Dst> handles;
39 handles.reserve(v.size());
40 std::transform(v.begin(), v.end(), std::back_inserter(handles), [](const Src &o) { return o.handle(); });
41 return handles;
42 }
43
44 template <class Dst, class Src>
MakeVkHandles(const std::vector<Src * > & v)45 std::vector<Dst> MakeVkHandles(const std::vector<Src *> &v) {
46 std::vector<Dst> handles;
47 handles.reserve(v.size());
48 std::transform(v.begin(), v.end(), std::back_inserter(handles), [](const Src *o) { return o->handle(); });
49 return handles;
50 }
51
52 typedef void (*ErrorCallback)(const char *expr, const char *file, unsigned int line, const char *function);
53 void set_error_callback(ErrorCallback callback);
54
55 class PhysicalDevice;
56 class Device;
57 class Queue;
58 class DeviceMemory;
59 class Fence;
60 class Semaphore;
61 class Event;
62 class QueryPool;
63 class Buffer;
64 class BufferView;
65 class Image;
66 class ImageView;
67 class DepthStencilView;
68 class Shader;
69 class Pipeline;
70 class PipelineDelta;
71 class Sampler;
72 class DescriptorSetLayout;
73 class PipelineLayout;
74 class DescriptorSetPool;
75 class DescriptorSet;
76 class CommandBuffer;
77 class CommandPool;
78
79 std::vector<VkLayerProperties> GetGlobalLayers();
80 std::vector<VkExtensionProperties> GetGlobalExtensions();
81 std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName);
82
83 namespace internal {
84
85 template <typename T>
86 class Handle {
87 public:
handle()88 const T &handle() const { return handle_; }
initialized()89 bool initialized() const { return (handle_ != T{}); }
90
91 protected:
92 typedef T handle_type;
93
Handle()94 explicit Handle() : handle_{} {}
Handle(T handle)95 explicit Handle(T handle) : handle_(handle) {}
96
97 // handles are non-copyable
98 Handle(const Handle &) = delete;
99 Handle &operator=(const Handle &) = delete;
100
101 // handles can be moved out
Handle(Handle && src)102 Handle(Handle &&src) NOEXCEPT : handle_{src.handle_} { src.handle_ = {}; }
103 Handle &operator=(Handle &&src) NOEXCEPT {
104 handle_ = src.handle_;
105 src.handle_ = {};
106 return *this;
107 }
108
init(T handle)109 void init(T handle) {
110 assert(!initialized());
111 handle_ = handle;
112 }
113
114 private:
115 T handle_;
116 };
117
118 template <typename T>
119 class NonDispHandle : public Handle<T> {
120 protected:
NonDispHandle()121 explicit NonDispHandle() : Handle<T>(), dev_handle_(VK_NULL_HANDLE) {}
NonDispHandle(VkDevice dev,T handle)122 explicit NonDispHandle(VkDevice dev, T handle) : Handle<T>(handle), dev_handle_(dev) {}
123
NonDispHandle(NonDispHandle && src)124 NonDispHandle(NonDispHandle &&src) : Handle<T>(std::move(src)) {
125 dev_handle_ = src.dev_handle_;
126 src.dev_handle_ = VK_NULL_HANDLE;
127 }
128 NonDispHandle &operator=(NonDispHandle &&src) {
129 Handle<T>::operator=(std::move(src));
130 dev_handle_ = src.dev_handle_;
131 src.dev_handle_ = VK_NULL_HANDLE;
132 return *this;
133 }
134
device()135 const VkDevice &device() const { return dev_handle_; }
136
init(VkDevice dev,T handle)137 void init(VkDevice dev, T handle) {
138 assert(!Handle<T>::initialized() && dev_handle_ == VK_NULL_HANDLE);
139 Handle<T>::init(handle);
140 dev_handle_ = dev;
141 }
142
143 private:
144 VkDevice dev_handle_;
145 };
146
147 } // namespace internal
148
149 class PhysicalDevice : public internal::Handle<VkPhysicalDevice> {
150 public:
PhysicalDevice(VkPhysicalDevice phy)151 explicit PhysicalDevice(VkPhysicalDevice phy) : Handle(phy) {
152 memory_properties_ = memory_properties();
153 device_properties_ = properties();
154 }
155
156 VkPhysicalDeviceProperties properties() const;
157 VkPhysicalDeviceMemoryProperties memory_properties() const;
158 std::vector<VkQueueFamilyProperties> queue_properties() const;
159 VkPhysicalDeviceFeatures features() const;
160
161 bool set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkMemoryPropertyFlags properties,
162 const VkMemoryPropertyFlags forbid = 0) const;
163
164 // vkEnumerateDeviceExtensionProperties()
165 std::vector<VkExtensionProperties> extensions() const;
166 std::vector<VkExtensionProperties> extensions(const char *pLayerName) const;
167
168 // vkEnumerateLayers()
169 std::vector<VkLayerProperties> layers() const;
170
171 private:
172 void add_extension_dependencies(uint32_t dependency_count, VkExtensionProperties *depencency_props,
173 std::vector<VkExtensionProperties> &ext_list);
174
175 VkPhysicalDeviceMemoryProperties memory_properties_;
176
177 VkPhysicalDeviceProperties device_properties_;
178 };
179
180 class QueueCreateInfoArray {
181 private:
182 std::vector<VkDeviceQueueCreateInfo> queue_info_;
183 std::vector<std::vector<float>> queue_priorities_;
184
185 public:
186 QueueCreateInfoArray(const std::vector<VkQueueFamilyProperties> &queue_props);
size()187 size_t size() const { return queue_info_.size(); }
data()188 const VkDeviceQueueCreateInfo *data() const { return queue_info_.data(); }
189 };
190
191 class Device : public internal::Handle<VkDevice> {
192 public:
Device(VkPhysicalDevice phy)193 explicit Device(VkPhysicalDevice phy) : phy_(phy) {}
194 ~Device();
195
196 // vkCreateDevice()
197 void init(const VkDeviceCreateInfo &info);
198 void init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features = nullptr,
199 void *create_device_pnext = nullptr); // all queues, all extensions, etc
init()200 void init() {
201 std::vector<const char *> extensions;
202 init(extensions);
203 };
204
phy()205 const PhysicalDevice &phy() const { return phy_; }
206
GetEnabledExtensions()207 std::vector<const char *> GetEnabledExtensions() { return enabled_extensions_; }
208 bool IsEnabledExtension(const char *extension);
209
210 // vkGetDeviceProcAddr()
get_proc(const char * name)211 PFN_vkVoidFunction get_proc(const char *name) const { return vkGetDeviceProcAddr(handle(), name); }
212
213 // vkGetDeviceQueue()
graphics_queues()214 const std::vector<Queue *> &graphics_queues() const { return queues_[GRAPHICS]; }
compute_queues()215 const std::vector<Queue *> &compute_queues() { return queues_[COMPUTE]; }
dma_queues()216 const std::vector<Queue *> &dma_queues() { return queues_[DMA]; }
217
218 typedef std::vector<std::unique_ptr<Queue>> QueueFamilyQueues;
219 typedef std::vector<QueueFamilyQueues> QueueFamilies;
220 const QueueFamilyQueues &queue_family_queues(uint32_t queue_family) const;
221
222 uint32_t graphics_queue_node_index_;
223
224 struct Format {
225 VkFormat format;
226 VkImageTiling tiling;
227 VkFlags features;
228 };
229 // vkGetFormatInfo()
230 VkFormatProperties format_properties(VkFormat format);
formats()231 const std::vector<Format> &formats() const { return formats_; }
232
233 // vkDeviceWaitIdle()
234 void wait();
235
236 // vkWaitForFences()
237 VkResult wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout);
wait(const Fence & fence)238 VkResult wait(const Fence &fence) { return wait(std::vector<const Fence *>(1, &fence), true, (uint64_t)-1); }
239
240 // vkUpdateDescriptorSets()
241 void update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes, const std::vector<VkCopyDescriptorSet> &copies);
update_descriptor_sets(const std::vector<VkWriteDescriptorSet> & writes)242 void update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes) {
243 return update_descriptor_sets(writes, std::vector<VkCopyDescriptorSet>());
244 }
245
246 static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
247 VkDescriptorType type, uint32_t count,
248 const VkDescriptorImageInfo *image_info);
249 static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
250 VkDescriptorType type, uint32_t count,
251 const VkDescriptorBufferInfo *buffer_info);
252 static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
253 VkDescriptorType type, uint32_t count, const VkBufferView *buffer_views);
254 static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
255 VkDescriptorType type, const std::vector<VkDescriptorImageInfo> &image_info);
256 static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
257 VkDescriptorType type, const std::vector<VkDescriptorBufferInfo> &buffer_info);
258 static VkWriteDescriptorSet write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
259 VkDescriptorType type, const std::vector<VkBufferView> &buffer_views);
260
261 static VkCopyDescriptorSet copy_descriptor_set(const DescriptorSet &src_set, uint32_t src_binding, uint32_t src_array_element,
262 const DescriptorSet &dst_set, uint32_t dst_binding, uint32_t dst_array_element,
263 uint32_t count);
264
265 private:
266 enum QueueIndex {
267 GRAPHICS,
268 COMPUTE,
269 DMA,
270 QUEUE_COUNT,
271 };
272
273 void init_queues();
274 void init_formats();
275
276 PhysicalDevice phy_;
277
278 std::vector<const char *> enabled_extensions_;
279
280 QueueFamilies queue_families_;
281 std::vector<Queue *> queues_[QUEUE_COUNT];
282 std::vector<Format> formats_;
283 };
284
285 class Queue : public internal::Handle<VkQueue> {
286 public:
Queue(VkQueue queue,int index)287 explicit Queue(VkQueue queue, int index) : Handle(queue) { family_index_ = index; }
288
289 // vkQueueSubmit()
290 VkResult submit(const std::vector<const CommandBuffer *> &cmds, const Fence &fence, bool expect_success = true);
291 VkResult submit(const CommandBuffer &cmd, const Fence &fence, bool expect_success = true);
292 VkResult submit(const CommandBuffer &cmd, bool expect_success = true);
293
294 // vkQueueWaitIdle()
295 VkResult wait();
296
get_family_index()297 int get_family_index() { return family_index_; }
298
299 private:
300 int family_index_;
301 };
302
303 class DeviceMemory : public internal::NonDispHandle<VkDeviceMemory> {
304 public:
305 ~DeviceMemory();
306
307 // vkAllocateMemory()
308 void init(const Device &dev, const VkMemoryAllocateInfo &info);
309
310 // vkMapMemory()
311 const void *map(VkFlags flags) const;
312 void *map(VkFlags flags);
map()313 const void *map() const { return map(0); }
map()314 void *map() { return map(0); }
315
316 // vkUnmapMemory()
317 void unmap() const;
318
319 static VkMemoryAllocateInfo alloc_info(VkDeviceSize size, uint32_t memory_type_index);
320 static VkMemoryAllocateInfo get_resource_alloc_info(const vk_testing::Device &dev, const VkMemoryRequirements &reqs,
321 VkMemoryPropertyFlags mem_props);
322 };
323
324 class Fence : public internal::NonDispHandle<VkFence> {
325 public:
326 ~Fence();
327
328 // vkCreateFence()
329 void init(const Device &dev, const VkFenceCreateInfo &info);
330
331 // vkGetFenceStatus()
status()332 VkResult status() const { return vkGetFenceStatus(device(), handle()); }
333 VkResult wait(VkBool32 wait_all, uint64_t timeout) const;
334
335 static VkFenceCreateInfo create_info(VkFenceCreateFlags flags);
336 static VkFenceCreateInfo create_info();
337 };
338
339 class Semaphore : public internal::NonDispHandle<VkSemaphore> {
340 public:
341 ~Semaphore();
342
343 // vkCreateSemaphore()
344 void init(const Device &dev, const VkSemaphoreCreateInfo &info);
345
346 static VkSemaphoreCreateInfo create_info(VkFlags flags);
347 };
348
349 class Event : public internal::NonDispHandle<VkEvent> {
350 public:
351 ~Event();
352
353 // vkCreateEvent()
354 void init(const Device &dev, const VkEventCreateInfo &info);
355
356 // vkGetEventStatus()
357 // vkSetEvent()
358 // vkResetEvent()
status()359 VkResult status() const { return vkGetEventStatus(device(), handle()); }
360 void set();
361 void reset();
362
363 static VkEventCreateInfo create_info(VkFlags flags);
364 };
365
366 class QueryPool : public internal::NonDispHandle<VkQueryPool> {
367 public:
368 ~QueryPool();
369
370 // vkCreateQueryPool()
371 void init(const Device &dev, const VkQueryPoolCreateInfo &info);
372
373 // vkGetQueryPoolResults()
374 VkResult results(uint32_t first, uint32_t count, size_t size, void *data, size_t stride);
375
376 static VkQueryPoolCreateInfo create_info(VkQueryType type, uint32_t slot_count);
377 };
378
379 class Buffer : public internal::NonDispHandle<VkBuffer> {
380 public:
Buffer()381 explicit Buffer() : NonDispHandle() {}
Buffer(const Device & dev,const VkBufferCreateInfo & info)382 explicit Buffer(const Device &dev, const VkBufferCreateInfo &info) { init(dev, info); }
Buffer(const Device & dev,VkDeviceSize size)383 explicit Buffer(const Device &dev, VkDeviceSize size) { init(dev, size); }
384
385 ~Buffer();
386
387 // vkCreateBuffer()
388 void init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props);
init(const Device & dev,const VkBufferCreateInfo & info)389 void init(const Device &dev, const VkBufferCreateInfo &info) { init(dev, info, 0); }
init(const Device & dev,VkDeviceSize size,VkMemoryPropertyFlags mem_props)390 void init(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags mem_props) { init(dev, create_info(size, 0), mem_props); }
init(const Device & dev,VkDeviceSize size)391 void init(const Device &dev, VkDeviceSize size) { init(dev, size, 0); }
392 void init_as_src(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
393 const std::vector<uint32_t> *queue_families = nullptr) {
394 init(dev, create_info(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, queue_families), reqs);
395 }
396 void init_as_dst(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
397 const std::vector<uint32_t> *queue_families = nullptr) {
398 init(dev, create_info(size, VK_BUFFER_USAGE_TRANSFER_DST_BIT, queue_families), reqs);
399 }
400 void init_as_src_and_dst(const Device &dev, VkDeviceSize size, VkMemoryPropertyFlags &reqs,
401 const std::vector<uint32_t> *queue_families = nullptr) {
402 init(dev, create_info(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, queue_families), reqs);
403 }
404 void init_no_mem(const Device &dev, const VkBufferCreateInfo &info);
405
406 // get the internal memory
memory()407 const DeviceMemory &memory() const { return internal_mem_; }
memory()408 DeviceMemory &memory() { return internal_mem_; }
409
410 // vkGetObjectMemoryRequirements()
411 VkMemoryRequirements memory_requirements() const;
412
413 // vkBindObjectMemory()
414 void bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset);
415
create_info()416 const VkBufferCreateInfo &create_info() const { return create_info_; }
417 static VkBufferCreateInfo create_info(VkDeviceSize size, VkFlags usage, const std::vector<uint32_t> *queue_families = nullptr);
418
buffer_memory_barrier(VkFlags output_mask,VkFlags input_mask,VkDeviceSize offset,VkDeviceSize size)419 VkBufferMemoryBarrier buffer_memory_barrier(VkFlags output_mask, VkFlags input_mask, VkDeviceSize offset,
420 VkDeviceSize size) const {
421 VkBufferMemoryBarrier barrier = {};
422 barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
423 barrier.buffer = handle();
424 barrier.srcAccessMask = output_mask;
425 barrier.dstAccessMask = input_mask;
426 barrier.offset = offset;
427 barrier.size = size;
428 if (create_info_.sharingMode == VK_SHARING_MODE_CONCURRENT) {
429 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
430 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
431 }
432 return barrier;
433 }
434
435 private:
436 VkBufferCreateInfo create_info_;
437
438 DeviceMemory internal_mem_;
439 };
440
441 class BufferView : public internal::NonDispHandle<VkBufferView> {
442 public:
443 ~BufferView();
444
445 // vkCreateBufferView()
446 void init(const Device &dev, const VkBufferViewCreateInfo &info);
447 static VkBufferViewCreateInfo createInfo(VkBuffer buffer, VkFormat format, VkDeviceSize offset = 0,
448 VkDeviceSize range = VK_WHOLE_SIZE);
449 };
450
createInfo(VkBuffer buffer,VkFormat format,VkDeviceSize offset,VkDeviceSize range)451 inline VkBufferViewCreateInfo BufferView::createInfo(VkBuffer buffer, VkFormat format, VkDeviceSize offset, VkDeviceSize range) {
452 VkBufferViewCreateInfo info = {};
453 info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
454 info.pNext = nullptr;
455 info.flags = VkFlags(0);
456 info.buffer = buffer;
457 info.format = format;
458 info.offset = offset;
459 info.range = range;
460 return info;
461 }
462
463 class Image : public internal::NonDispHandle<VkImage> {
464 public:
Image()465 explicit Image() : NonDispHandle(), format_features_(0) {}
Image(const Device & dev,const VkImageCreateInfo & info)466 explicit Image(const Device &dev, const VkImageCreateInfo &info) : format_features_(0) { init(dev, info); }
467
468 ~Image();
469
470 // vkCreateImage()
471 void init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props);
init(const Device & dev,const VkImageCreateInfo & info)472 void init(const Device &dev, const VkImageCreateInfo &info) { init(dev, info, 0); }
473 void init_no_mem(const Device &dev, const VkImageCreateInfo &info);
474
475 // get the internal memory
memory()476 const DeviceMemory &memory() const { return internal_mem_; }
memory()477 DeviceMemory &memory() { return internal_mem_; }
478
479 // vkGetObjectMemoryRequirements()
480 VkMemoryRequirements memory_requirements() const;
481
482 // vkBindObjectMemory()
483 void bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset);
484
485 // vkGetImageSubresourceLayout()
486 VkSubresourceLayout subresource_layout(const VkImageSubresource &subres) const;
487 VkSubresourceLayout subresource_layout(const VkImageSubresourceLayers &subres) const;
488
489 bool transparent() const;
copyable()490 bool copyable() const { return (format_features_ & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT); }
491
subresource_range(VkImageAspectFlagBits aspect)492 VkImageSubresourceRange subresource_range(VkImageAspectFlagBits aspect) const {
493 return subresource_range(create_info_, aspect);
494 }
extent()495 VkExtent3D extent() const { return create_info_.extent; }
extent(uint32_t mip_level)496 VkExtent3D extent(uint32_t mip_level) const { return extent(create_info_.extent, mip_level); }
format()497 VkFormat format() const { return create_info_.format; }
usage()498 VkImageUsageFlags usage() const { return create_info_.usage; }
sharing_mode()499 VkSharingMode sharing_mode() const { return create_info_.sharingMode; }
image_memory_barrier(VkFlags output_mask,VkFlags input_mask,VkImageLayout old_layout,VkImageLayout new_layout,const VkImageSubresourceRange & range)500 VkImageMemoryBarrier image_memory_barrier(VkFlags output_mask, VkFlags input_mask, VkImageLayout old_layout,
501 VkImageLayout new_layout, const VkImageSubresourceRange &range) const {
502 VkImageMemoryBarrier barrier = {};
503 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
504 barrier.srcAccessMask = output_mask;
505 barrier.dstAccessMask = input_mask;
506 barrier.oldLayout = old_layout;
507 barrier.newLayout = new_layout;
508 barrier.image = handle();
509 barrier.subresourceRange = range;
510
511 if (sharing_mode() == VK_SHARING_MODE_CONCURRENT) {
512 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
513 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
514 }
515 return barrier;
516 }
517
518 static VkImageCreateInfo create_info();
519 static VkImageSubresource subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer);
520 static VkImageSubresource subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer);
521 static VkImageSubresourceLayers subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer,
522 uint32_t array_size);
523 static VkImageSubresourceLayers subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer,
524 uint32_t array_size);
525 static VkImageSubresourceRange subresource_range(VkImageAspectFlags aspect_mask, uint32_t base_mip_level, uint32_t mip_levels,
526 uint32_t base_array_layer, uint32_t num_layers);
527 static VkImageSubresourceRange subresource_range(const VkImageCreateInfo &info, VkImageAspectFlags aspect_mask);
528 static VkImageSubresourceRange subresource_range(const VkImageSubresource &subres);
529
530 static VkExtent2D extent(int32_t width, int32_t height);
531 static VkExtent2D extent(const VkExtent2D &extent, uint32_t mip_level);
532 static VkExtent2D extent(const VkExtent3D &extent);
533
534 static VkExtent3D extent(int32_t width, int32_t height, int32_t depth);
535 static VkExtent3D extent(const VkExtent3D &extent, uint32_t mip_level);
536
537 private:
538 void init_info(const Device &dev, const VkImageCreateInfo &info);
539
540 VkImageCreateInfo create_info_;
541 VkFlags format_features_;
542
543 DeviceMemory internal_mem_;
544 };
545
546 class ImageView : public internal::NonDispHandle<VkImageView> {
547 public:
548 ~ImageView();
549
550 // vkCreateImageView()
551 void init(const Device &dev, const VkImageViewCreateInfo &info);
552 };
553
554 class ShaderModule : public internal::NonDispHandle<VkShaderModule> {
555 public:
556 ~ShaderModule();
557
558 // vkCreateShaderModule()
559 void init(const Device &dev, const VkShaderModuleCreateInfo &info);
560 VkResult init_try(const Device &dev, const VkShaderModuleCreateInfo &info);
561
562 static VkShaderModuleCreateInfo create_info(size_t code_size, const uint32_t *code, VkFlags flags);
563 };
564
565 class Pipeline : public internal::NonDispHandle<VkPipeline> {
566 public:
567 ~Pipeline();
568
569 // vkCreateGraphicsPipeline()
570 void init(const Device &dev, const VkGraphicsPipelineCreateInfo &info);
571 // vkCreateGraphicsPipelineDerivative()
572 void init(const Device &dev, const VkGraphicsPipelineCreateInfo &info, const VkPipeline basePipeline);
573 // vkCreateComputePipeline()
574 void init(const Device &dev, const VkComputePipelineCreateInfo &info);
575 // vkLoadPipeline()
576 void init(const Device &dev, size_t size, const void *data);
577 // vkLoadPipelineDerivative()
578 void init(const Device &dev, size_t size, const void *data, VkPipeline basePipeline);
579
580 // vkCreateGraphicsPipeline with error return
581 VkResult init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info);
582
583 // vkStorePipeline()
584 size_t store(size_t size, void *data);
585 };
586
587 class PipelineLayout : public internal::NonDispHandle<VkPipelineLayout> {
588 public:
PipelineLayout()589 PipelineLayout() NOEXCEPT : NonDispHandle(){};
590 ~PipelineLayout();
591
592 // Move constructor for Visual Studio 2013
PipelineLayout(PipelineLayout && src)593 PipelineLayout(PipelineLayout &&src) : NonDispHandle(std::move(src)){};
594
595 PipelineLayout &operator=(PipelineLayout &&src) {
596 this->~PipelineLayout();
597 this->NonDispHandle::operator=(std::move(src));
598 return *this;
599 };
600
601 // vCreatePipelineLayout()
602 void init(const Device &dev, VkPipelineLayoutCreateInfo &info, const std::vector<const DescriptorSetLayout *> &layouts);
603 };
604
605 class Sampler : public internal::NonDispHandle<VkSampler> {
606 public:
607 ~Sampler();
608
609 // vkCreateSampler()
610 void init(const Device &dev, const VkSamplerCreateInfo &info);
611 };
612
613 class DescriptorSetLayout : public internal::NonDispHandle<VkDescriptorSetLayout> {
614 public:
DescriptorSetLayout()615 DescriptorSetLayout() NOEXCEPT : NonDispHandle(){};
616 ~DescriptorSetLayout();
617
618 // Move constructor for Visual Studio 2013
DescriptorSetLayout(DescriptorSetLayout && src)619 DescriptorSetLayout(DescriptorSetLayout &&src) : NonDispHandle(std::move(src)){};
620
621 DescriptorSetLayout &operator=(DescriptorSetLayout &&src) NOEXCEPT {
622 this->~DescriptorSetLayout();
623 this->NonDispHandle::operator=(std::move(src));
624 return *this;
625 }
626
627 // vkCreateDescriptorSetLayout()
628 void init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info);
629 };
630
631 class DescriptorPool : public internal::NonDispHandle<VkDescriptorPool> {
632 public:
633 ~DescriptorPool();
634
635 // Descriptor sets allocated from this pool will need access to the original
636 // object
GetObj()637 VkDescriptorPool GetObj() { return pool_; }
638
639 // vkCreateDescriptorPool()
640 void init(const Device &dev, const VkDescriptorPoolCreateInfo &info);
641
642 // vkResetDescriptorPool()
643 void reset();
644
645 // vkFreeDescriptorSet()
setDynamicUsage(bool isDynamic)646 void setDynamicUsage(bool isDynamic) { dynamic_usage_ = isDynamic; }
getDynamicUsage()647 bool getDynamicUsage() { return dynamic_usage_; }
648
649 // vkAllocateDescriptorSets()
650 std::vector<DescriptorSet *> alloc_sets(const Device &dev, const std::vector<const DescriptorSetLayout *> &layouts);
651 std::vector<DescriptorSet *> alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count);
652 DescriptorSet *alloc_sets(const Device &dev, const DescriptorSetLayout &layout);
653
654 template <typename PoolSizes>
655 static VkDescriptorPoolCreateInfo create_info(VkDescriptorPoolCreateFlags flags, uint32_t max_sets,
656 const PoolSizes &pool_sizes);
657
658 private:
659 VkDescriptorPool pool_;
660
661 // Track whether this pool's usage is VK_DESCRIPTOR_POOL_USAGE_DYNAMIC
662 bool dynamic_usage_;
663 };
664
665 template <typename PoolSizes>
create_info(VkDescriptorPoolCreateFlags flags,uint32_t max_sets,const PoolSizes & pool_sizes)666 inline VkDescriptorPoolCreateInfo DescriptorPool::create_info(VkDescriptorPoolCreateFlags flags, uint32_t max_sets,
667 const PoolSizes &pool_sizes) {
668 VkDescriptorPoolCreateInfo info{};
669 info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
670 info.pNext = nullptr;
671 info.flags = flags;
672 info.maxSets = max_sets;
673 info.poolSizeCount = pool_sizes.size();
674 info.pPoolSizes = (info.poolSizeCount) ? pool_sizes.data() : nullptr;
675 return info;
676 }
677
678 class DescriptorSet : public internal::NonDispHandle<VkDescriptorSet> {
679 public:
680 ~DescriptorSet();
681
DescriptorSet()682 explicit DescriptorSet() : NonDispHandle() {}
DescriptorSet(const Device & dev,DescriptorPool * pool,VkDescriptorSet set)683 explicit DescriptorSet(const Device &dev, DescriptorPool *pool, VkDescriptorSet set) : NonDispHandle(dev.handle(), set) {
684 containing_pool_ = pool;
685 }
686
687 private:
688 DescriptorPool *containing_pool_;
689 };
690
691 class CommandPool : public internal::NonDispHandle<VkCommandPool> {
692 public:
693 ~CommandPool();
694
CommandPool()695 explicit CommandPool() : NonDispHandle() {}
CommandPool(const Device & dev,const VkCommandPoolCreateInfo & info)696 explicit CommandPool(const Device &dev, const VkCommandPoolCreateInfo &info) { init(dev, info); }
697
698 void init(const Device &dev, const VkCommandPoolCreateInfo &info);
699
700 static VkCommandPoolCreateInfo create_info(uint32_t queue_family_index, VkCommandPoolCreateFlags flags);
701 };
702
create_info(uint32_t queue_family_index,VkCommandPoolCreateFlags flags)703 inline VkCommandPoolCreateInfo CommandPool::create_info(uint32_t queue_family_index, VkCommandPoolCreateFlags flags) {
704 VkCommandPoolCreateInfo info = {};
705 info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
706 info.queueFamilyIndex = queue_family_index;
707 info.flags = flags;
708 return info;
709 }
710
711 class CommandBuffer : public internal::Handle<VkCommandBuffer> {
712 public:
713 ~CommandBuffer();
714
CommandBuffer()715 explicit CommandBuffer() : Handle() {}
CommandBuffer(const Device & dev,const VkCommandBufferAllocateInfo & info)716 explicit CommandBuffer(const Device &dev, const VkCommandBufferAllocateInfo &info) { init(dev, info); }
717
718 // vkAllocateCommandBuffers()
719 void init(const Device &dev, const VkCommandBufferAllocateInfo &info);
720
721 // vkBeginCommandBuffer()
722 void begin(const VkCommandBufferBeginInfo *info);
723 void begin();
724
725 // vkEndCommandBuffer()
726 // vkResetCommandBuffer()
727 void end();
728 void reset(VkCommandBufferResetFlags flags);
reset()729 void reset() { reset(VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); }
730
731 static VkCommandBufferAllocateInfo create_info(VkCommandPool const &pool);
732
733 private:
734 VkDevice dev_handle_;
735 VkCommandPool cmd_pool_;
736 };
737
alloc_info(VkDeviceSize size,uint32_t memory_type_index)738 inline VkMemoryAllocateInfo DeviceMemory::alloc_info(VkDeviceSize size, uint32_t memory_type_index) {
739 VkMemoryAllocateInfo info = {};
740 info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
741 info.allocationSize = size;
742 info.memoryTypeIndex = memory_type_index;
743 return info;
744 }
745
create_info(VkDeviceSize size,VkFlags usage,const std::vector<uint32_t> * queue_families)746 inline VkBufferCreateInfo Buffer::create_info(VkDeviceSize size, VkFlags usage, const std::vector<uint32_t> *queue_families) {
747 VkBufferCreateInfo info = {};
748 info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
749 info.size = size;
750 info.usage = usage;
751
752 if (queue_families && queue_families->size() > 1) {
753 info.sharingMode = VK_SHARING_MODE_CONCURRENT;
754 info.queueFamilyIndexCount = static_cast<uint32_t>(queue_families->size());
755 info.pQueueFamilyIndices = queue_families->data();
756 }
757
758 return info;
759 }
760
create_info(VkFenceCreateFlags flags)761 inline VkFenceCreateInfo Fence::create_info(VkFenceCreateFlags flags) {
762 VkFenceCreateInfo info = {};
763 info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
764 info.flags = flags;
765 return info;
766 }
767
create_info()768 inline VkFenceCreateInfo Fence::create_info() {
769 VkFenceCreateInfo info = {};
770 info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
771 return info;
772 }
773
create_info(VkFlags flags)774 inline VkSemaphoreCreateInfo Semaphore::create_info(VkFlags flags) {
775 VkSemaphoreCreateInfo info = {};
776 info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
777 info.flags = flags;
778 return info;
779 }
780
create_info(VkFlags flags)781 inline VkEventCreateInfo Event::create_info(VkFlags flags) {
782 VkEventCreateInfo info = {};
783 info.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
784 info.flags = flags;
785 return info;
786 }
787
create_info(VkQueryType type,uint32_t slot_count)788 inline VkQueryPoolCreateInfo QueryPool::create_info(VkQueryType type, uint32_t slot_count) {
789 VkQueryPoolCreateInfo info = {};
790 info.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
791 info.queryType = type;
792 info.queryCount = slot_count;
793 return info;
794 }
795
create_info()796 inline VkImageCreateInfo Image::create_info() {
797 VkImageCreateInfo info = {};
798 info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
799 info.extent.width = 1;
800 info.extent.height = 1;
801 info.extent.depth = 1;
802 info.mipLevels = 1;
803 info.arrayLayers = 1;
804 info.samples = VK_SAMPLE_COUNT_1_BIT;
805 return info;
806 }
807
subresource(VkImageAspectFlags aspect,uint32_t mip_level,uint32_t array_layer)808 inline VkImageSubresource Image::subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer) {
809 VkImageSubresource subres = {};
810 if (aspect == 0) {
811 assert(!"Invalid VkImageAspectFlags");
812 }
813 subres.aspectMask = aspect;
814 subres.mipLevel = mip_level;
815 subres.arrayLayer = array_layer;
816 return subres;
817 }
818
subresource(const VkImageSubresourceRange & range,uint32_t mip_level,uint32_t array_layer)819 inline VkImageSubresource Image::subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer) {
820 return subresource(range.aspectMask, range.baseMipLevel + mip_level, range.baseArrayLayer + array_layer);
821 }
822
subresource(VkImageAspectFlags aspect,uint32_t mip_level,uint32_t array_layer,uint32_t array_size)823 inline VkImageSubresourceLayers Image::subresource(VkImageAspectFlags aspect, uint32_t mip_level, uint32_t array_layer,
824 uint32_t array_size) {
825 VkImageSubresourceLayers subres = {};
826 switch (aspect) {
827 case VK_IMAGE_ASPECT_COLOR_BIT:
828 case VK_IMAGE_ASPECT_DEPTH_BIT:
829 case VK_IMAGE_ASPECT_STENCIL_BIT:
830 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
831 /* valid */
832 break;
833 default:
834 assert(!"Invalid VkImageAspectFlags");
835 }
836 subres.aspectMask = aspect;
837 subres.mipLevel = mip_level;
838 subres.baseArrayLayer = array_layer;
839 subres.layerCount = array_size;
840 return subres;
841 }
842
subresource(const VkImageSubresourceRange & range,uint32_t mip_level,uint32_t array_layer,uint32_t array_size)843 inline VkImageSubresourceLayers Image::subresource(const VkImageSubresourceRange &range, uint32_t mip_level, uint32_t array_layer,
844 uint32_t array_size) {
845 return subresource(range.aspectMask, range.baseMipLevel + mip_level, range.baseArrayLayer + array_layer, array_size);
846 }
847
subresource_range(VkImageAspectFlags aspect_mask,uint32_t base_mip_level,uint32_t mip_levels,uint32_t base_array_layer,uint32_t num_layers)848 inline VkImageSubresourceRange Image::subresource_range(VkImageAspectFlags aspect_mask, uint32_t base_mip_level,
849 uint32_t mip_levels, uint32_t base_array_layer, uint32_t num_layers) {
850 VkImageSubresourceRange range = {};
851 if (aspect_mask == 0) {
852 assert(!"Invalid VkImageAspectFlags");
853 }
854 range.aspectMask = aspect_mask;
855 range.baseMipLevel = base_mip_level;
856 range.levelCount = mip_levels;
857 range.baseArrayLayer = base_array_layer;
858 range.layerCount = num_layers;
859 return range;
860 }
861
subresource_range(const VkImageCreateInfo & info,VkImageAspectFlags aspect_mask)862 inline VkImageSubresourceRange Image::subresource_range(const VkImageCreateInfo &info, VkImageAspectFlags aspect_mask) {
863 return subresource_range(aspect_mask, 0, info.mipLevels, 0, info.arrayLayers);
864 }
865
subresource_range(const VkImageSubresource & subres)866 inline VkImageSubresourceRange Image::subresource_range(const VkImageSubresource &subres) {
867 return subresource_range(subres.aspectMask, subres.mipLevel, 1, subres.arrayLayer, 1);
868 }
869
extent(int32_t width,int32_t height)870 inline VkExtent2D Image::extent(int32_t width, int32_t height) {
871 VkExtent2D extent = {};
872 extent.width = width;
873 extent.height = height;
874 return extent;
875 }
876
extent(const VkExtent2D & extent,uint32_t mip_level)877 inline VkExtent2D Image::extent(const VkExtent2D &extent, uint32_t mip_level) {
878 const int32_t width = (extent.width >> mip_level) ? extent.width >> mip_level : 1;
879 const int32_t height = (extent.height >> mip_level) ? extent.height >> mip_level : 1;
880 return Image::extent(width, height);
881 }
882
extent(const VkExtent3D & extent)883 inline VkExtent2D Image::extent(const VkExtent3D &extent) { return Image::extent(extent.width, extent.height); }
884
extent(int32_t width,int32_t height,int32_t depth)885 inline VkExtent3D Image::extent(int32_t width, int32_t height, int32_t depth) {
886 VkExtent3D extent = {};
887 extent.width = width;
888 extent.height = height;
889 extent.depth = depth;
890 return extent;
891 }
892
extent(const VkExtent3D & extent,uint32_t mip_level)893 inline VkExtent3D Image::extent(const VkExtent3D &extent, uint32_t mip_level) {
894 const int32_t width = (extent.width >> mip_level) ? extent.width >> mip_level : 1;
895 const int32_t height = (extent.height >> mip_level) ? extent.height >> mip_level : 1;
896 const int32_t depth = (extent.depth >> mip_level) ? extent.depth >> mip_level : 1;
897 return Image::extent(width, height, depth);
898 }
899
create_info(size_t code_size,const uint32_t * code,VkFlags flags)900 inline VkShaderModuleCreateInfo ShaderModule::create_info(size_t code_size, const uint32_t *code, VkFlags flags) {
901 VkShaderModuleCreateInfo info = {};
902 info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
903 info.codeSize = code_size;
904 info.pCode = code;
905 info.flags = flags;
906 return info;
907 }
908
write_descriptor_set(const DescriptorSet & set,uint32_t binding,uint32_t array_element,VkDescriptorType type,uint32_t count,const VkDescriptorImageInfo * image_info)909 inline VkWriteDescriptorSet Device::write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
910 VkDescriptorType type, uint32_t count,
911 const VkDescriptorImageInfo *image_info) {
912 VkWriteDescriptorSet write = {};
913 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
914 write.dstSet = set.handle();
915 write.dstBinding = binding;
916 write.dstArrayElement = array_element;
917 write.descriptorCount = count;
918 write.descriptorType = type;
919 write.pImageInfo = image_info;
920 return write;
921 }
922
write_descriptor_set(const DescriptorSet & set,uint32_t binding,uint32_t array_element,VkDescriptorType type,uint32_t count,const VkDescriptorBufferInfo * buffer_info)923 inline VkWriteDescriptorSet Device::write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
924 VkDescriptorType type, uint32_t count,
925 const VkDescriptorBufferInfo *buffer_info) {
926 VkWriteDescriptorSet write = {};
927 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
928 write.dstSet = set.handle();
929 write.dstBinding = binding;
930 write.dstArrayElement = array_element;
931 write.descriptorCount = count;
932 write.descriptorType = type;
933 write.pBufferInfo = buffer_info;
934 return write;
935 }
936
write_descriptor_set(const DescriptorSet & set,uint32_t binding,uint32_t array_element,VkDescriptorType type,uint32_t count,const VkBufferView * buffer_views)937 inline VkWriteDescriptorSet Device::write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
938 VkDescriptorType type, uint32_t count, const VkBufferView *buffer_views) {
939 VkWriteDescriptorSet write = {};
940 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
941 write.dstSet = set.handle();
942 write.dstBinding = binding;
943 write.dstArrayElement = array_element;
944 write.descriptorCount = count;
945 write.descriptorType = type;
946 write.pTexelBufferView = buffer_views;
947 return write;
948 }
949
write_descriptor_set(const DescriptorSet & set,uint32_t binding,uint32_t array_element,VkDescriptorType type,const std::vector<VkDescriptorImageInfo> & image_info)950 inline VkWriteDescriptorSet Device::write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
951 VkDescriptorType type,
952 const std::vector<VkDescriptorImageInfo> &image_info) {
953 return write_descriptor_set(set, binding, array_element, type, image_info.size(), &image_info[0]);
954 }
955
write_descriptor_set(const DescriptorSet & set,uint32_t binding,uint32_t array_element,VkDescriptorType type,const std::vector<VkDescriptorBufferInfo> & buffer_info)956 inline VkWriteDescriptorSet Device::write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
957 VkDescriptorType type,
958 const std::vector<VkDescriptorBufferInfo> &buffer_info) {
959 return write_descriptor_set(set, binding, array_element, type, buffer_info.size(), &buffer_info[0]);
960 }
961
write_descriptor_set(const DescriptorSet & set,uint32_t binding,uint32_t array_element,VkDescriptorType type,const std::vector<VkBufferView> & buffer_views)962 inline VkWriteDescriptorSet Device::write_descriptor_set(const DescriptorSet &set, uint32_t binding, uint32_t array_element,
963 VkDescriptorType type, const std::vector<VkBufferView> &buffer_views) {
964 return write_descriptor_set(set, binding, array_element, type, buffer_views.size(), &buffer_views[0]);
965 }
966
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)967 inline VkCopyDescriptorSet Device::copy_descriptor_set(const DescriptorSet &src_set, uint32_t src_binding,
968 uint32_t src_array_element, const DescriptorSet &dst_set,
969 uint32_t dst_binding, uint32_t dst_array_element, uint32_t count) {
970 VkCopyDescriptorSet copy = {};
971 copy.sType = VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET;
972 copy.srcSet = src_set.handle();
973 copy.srcBinding = src_binding;
974 copy.srcArrayElement = src_array_element;
975 copy.dstSet = dst_set.handle();
976 copy.dstBinding = dst_binding;
977 copy.dstArrayElement = dst_array_element;
978 copy.descriptorCount = count;
979
980 return copy;
981 }
982
create_info(VkCommandPool const & pool)983 inline VkCommandBufferAllocateInfo CommandBuffer::create_info(VkCommandPool const &pool) {
984 VkCommandBufferAllocateInfo info = {};
985 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
986 info.commandPool = pool;
987 info.commandBufferCount = 1;
988 return info;
989 }
990
991 } // namespace vk_testing
992
993 #endif // VKTESTBINDING_H
994