1 /*
2 * Copyright (C) 2016 Google, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef HELPERS_H
18 #define HELPERS_H
19
20 #include <vector>
21 #include <sstream>
22 #include <stdexcept>
23 #include <vulkan/vulkan.h>
24
25 #include "HelpersDispatchTable.h"
26
27 namespace vk {
28
assert_success(VkResult res)29 inline VkResult assert_success(VkResult res) {
30 if (res != VK_SUCCESS) {
31 std::stringstream ss;
32 ss << "VkResult " << res << " returned";
33 throw std::runtime_error(ss.str());
34 }
35
36 return res;
37 }
38
enumerate(const char * layer,std::vector<VkExtensionProperties> & exts)39 inline VkResult enumerate(const char *layer, std::vector<VkExtensionProperties> &exts) {
40 uint32_t count = 0;
41 vk::EnumerateInstanceExtensionProperties(layer, &count, nullptr);
42
43 exts.resize(count);
44 return vk::EnumerateInstanceExtensionProperties(layer, &count, exts.data());
45 }
46
enumerate(VkPhysicalDevice phy,const char * layer,std::vector<VkExtensionProperties> & exts)47 inline VkResult enumerate(VkPhysicalDevice phy, const char *layer, std::vector<VkExtensionProperties> &exts) {
48 uint32_t count = 0;
49 vk::EnumerateDeviceExtensionProperties(phy, layer, &count, nullptr);
50
51 exts.resize(count);
52 return vk::EnumerateDeviceExtensionProperties(phy, layer, &count, exts.data());
53 }
54
enumerate(VkInstance instance,std::vector<VkPhysicalDevice> & phys)55 inline VkResult enumerate(VkInstance instance, std::vector<VkPhysicalDevice> &phys) {
56 uint32_t count = 0;
57 vk::EnumeratePhysicalDevices(instance, &count, nullptr);
58
59 phys.resize(count);
60 return vk::EnumeratePhysicalDevices(instance, &count, phys.data());
61 }
62
enumerate(std::vector<VkLayerProperties> & layer_props)63 inline VkResult enumerate(std::vector<VkLayerProperties> &layer_props) {
64 uint32_t count = 0;
65 vk::EnumerateInstanceLayerProperties(&count, nullptr);
66
67 layer_props.resize(count);
68 return vk::EnumerateInstanceLayerProperties(&count, layer_props.data());
69 }
70
get(VkPhysicalDevice phy,std::vector<VkQueueFamilyProperties> & queues)71 inline VkResult get(VkPhysicalDevice phy, std::vector<VkQueueFamilyProperties> &queues) {
72 uint32_t count = 0;
73 vk::GetPhysicalDeviceQueueFamilyProperties(phy, &count, nullptr);
74
75 queues.resize(count);
76 vk::GetPhysicalDeviceQueueFamilyProperties(phy, &count, queues.data());
77
78 return VK_SUCCESS;
79 }
80
get(VkPhysicalDevice phy,VkSurfaceKHR surface,std::vector<VkSurfaceFormatKHR> & formats)81 inline VkResult get(VkPhysicalDevice phy, VkSurfaceKHR surface, std::vector<VkSurfaceFormatKHR> &formats) {
82 uint32_t count = 0;
83 vk::GetPhysicalDeviceSurfaceFormatsKHR(phy, surface, &count, nullptr);
84
85 formats.resize(count);
86 return vk::GetPhysicalDeviceSurfaceFormatsKHR(phy, surface, &count, formats.data());
87 }
88
get(VkPhysicalDevice phy,VkSurfaceKHR surface,std::vector<VkPresentModeKHR> & modes)89 inline VkResult get(VkPhysicalDevice phy, VkSurfaceKHR surface, std::vector<VkPresentModeKHR> &modes) {
90 uint32_t count = 0;
91 vk::GetPhysicalDeviceSurfacePresentModesKHR(phy, surface, &count, nullptr);
92
93 modes.resize(count);
94 return vk::GetPhysicalDeviceSurfacePresentModesKHR(phy, surface, &count, modes.data());
95 }
96
get(VkDevice dev,VkSwapchainKHR swapchain,std::vector<VkImage> & images)97 inline VkResult get(VkDevice dev, VkSwapchainKHR swapchain, std::vector<VkImage> &images) {
98 uint32_t count = 0;
99 vk::GetSwapchainImagesKHR(dev, swapchain, &count, nullptr);
100
101 images.resize(count);
102 return vk::GetSwapchainImagesKHR(dev, swapchain, &count, images.data());
103 }
104
105 } // namespace vk
106
107 #endif // HELPERS_H
108