1 /*
2 * Copyright (C) 2016 Google, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef HELPERS_H
24 #define HELPERS_H
25
26 #include <vector>
27 #include <sstream>
28 #include <stdexcept>
29 #include <vulkan/vulkan.h>
30
31 #include "HelpersDispatchTable.h"
32
33 namespace vk {
34
assert_success(VkResult res)35 inline VkResult assert_success(VkResult res)
36 {
37 if (res != VK_SUCCESS) {
38 std::stringstream ss;
39 ss << "VkResult " << res << " returned";
40 throw std::runtime_error(ss.str());
41 }
42
43 return res;
44 }
45
enumerate(const char * layer,std::vector<VkExtensionProperties> & exts)46 inline VkResult enumerate(const char *layer, std::vector<VkExtensionProperties> &exts)
47 {
48 uint32_t count = 0;
49 vk::EnumerateInstanceExtensionProperties(layer, &count, nullptr);
50
51 exts.resize(count);
52 return vk::EnumerateInstanceExtensionProperties(layer, &count, exts.data());
53 }
54
enumerate(VkPhysicalDevice phy,const char * layer,std::vector<VkExtensionProperties> & exts)55 inline VkResult enumerate(VkPhysicalDevice phy, const char *layer, std::vector<VkExtensionProperties> &exts)
56 {
57 uint32_t count = 0;
58 vk::EnumerateDeviceExtensionProperties(phy, layer, &count, nullptr);
59
60 exts.resize(count);
61 return vk::EnumerateDeviceExtensionProperties(phy, layer, &count, exts.data());
62 }
63
enumerate(VkInstance instance,std::vector<VkPhysicalDevice> & phys)64 inline VkResult enumerate(VkInstance instance, std::vector<VkPhysicalDevice> &phys)
65 {
66 uint32_t count = 0;
67 vk::EnumeratePhysicalDevices(instance, &count, nullptr);
68
69 phys.resize(count);
70 return vk::EnumeratePhysicalDevices(instance, &count, phys.data());
71 }
72
enumerate(std::vector<VkLayerProperties> & layer_props)73 inline VkResult enumerate(std::vector<VkLayerProperties> &layer_props)
74 {
75 uint32_t count = 0;
76 vk::EnumerateInstanceLayerProperties(&count, nullptr);
77
78 layer_props.resize(count);
79 return vk::EnumerateInstanceLayerProperties(&count, layer_props.data());
80 }
81
enumerate(VkPhysicalDevice phy,std::vector<VkLayerProperties> & layer_props)82 inline VkResult enumerate(VkPhysicalDevice phy, std::vector<VkLayerProperties> &layer_props)
83 {
84 uint32_t count = 0;
85 vk::EnumerateDeviceLayerProperties(phy, &count, nullptr);
86
87 layer_props.resize(count);
88 return vk::EnumerateDeviceLayerProperties(phy, &count, layer_props.data());
89 }
90
get(VkPhysicalDevice phy,std::vector<VkQueueFamilyProperties> & queues)91 inline VkResult get(VkPhysicalDevice phy, std::vector<VkQueueFamilyProperties> &queues)
92 {
93 uint32_t count = 0;
94 vk::GetPhysicalDeviceQueueFamilyProperties(phy, &count, nullptr);
95
96 queues.resize(count);
97 vk::GetPhysicalDeviceQueueFamilyProperties(phy, &count, queues.data());
98
99 return VK_SUCCESS;
100 }
101
get(VkPhysicalDevice phy,VkSurfaceKHR surface,std::vector<VkSurfaceFormatKHR> & formats)102 inline VkResult get(VkPhysicalDevice phy, VkSurfaceKHR surface, std::vector<VkSurfaceFormatKHR> &formats)
103 {
104 uint32_t count = 0;
105 vk::GetPhysicalDeviceSurfaceFormatsKHR(phy, surface, &count, nullptr);
106
107 formats.resize(count);
108 return vk::GetPhysicalDeviceSurfaceFormatsKHR(phy, surface, &count, formats.data());
109 }
110
get(VkPhysicalDevice phy,VkSurfaceKHR surface,std::vector<VkPresentModeKHR> & modes)111 inline VkResult get(VkPhysicalDevice phy, VkSurfaceKHR surface, std::vector<VkPresentModeKHR> &modes)
112 {
113 uint32_t count = 0;
114 vk::GetPhysicalDeviceSurfacePresentModesKHR(phy, surface, &count, nullptr);
115
116 modes.resize(count);
117 return vk::GetPhysicalDeviceSurfacePresentModesKHR(phy, surface, &count, modes.data());
118 }
119
get(VkDevice dev,VkSwapchainKHR swapchain,std::vector<VkImage> & images)120 inline VkResult get(VkDevice dev, VkSwapchainKHR swapchain, std::vector<VkImage> &images)
121 {
122 uint32_t count = 0;
123 vk::GetSwapchainImagesKHR(dev, swapchain, &count, nullptr);
124
125 images.resize(count);
126 return vk::GetSwapchainImagesKHR(dev, swapchain, &count, images.data());
127 }
128
129 } // namespace vk
130
131 #endif // HELPERS_H
132