Home | History | Annotate | Download | only in smoke
      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 
     29 inline VkResult assert_success(VkResult res)
     30 {
     31     if (res != VK_SUCCESS) {
     32         std::stringstream ss;
     33         ss << "VkResult " << res << " returned";
     34         throw std::runtime_error(ss.str());
     35     }
     36 
     37     return res;
     38 }
     39 
     40 inline VkResult enumerate(const char *layer, std::vector<VkExtensionProperties> &exts)
     41 {
     42     uint32_t count = 0;
     43     vk::EnumerateInstanceExtensionProperties(layer, &count, nullptr);
     44 
     45     exts.resize(count);
     46     return vk::EnumerateInstanceExtensionProperties(layer, &count, exts.data());
     47 }
     48 
     49 inline VkResult enumerate(VkPhysicalDevice phy, const char *layer, std::vector<VkExtensionProperties> &exts)
     50 {
     51     uint32_t count = 0;
     52     vk::EnumerateDeviceExtensionProperties(phy, layer, &count, nullptr);
     53 
     54     exts.resize(count);
     55     return vk::EnumerateDeviceExtensionProperties(phy, layer, &count, exts.data());
     56 }
     57 
     58 inline VkResult enumerate(VkInstance instance, std::vector<VkPhysicalDevice> &phys)
     59 {
     60     uint32_t count = 0;
     61     vk::EnumeratePhysicalDevices(instance, &count, nullptr);
     62 
     63     phys.resize(count);
     64     return vk::EnumeratePhysicalDevices(instance, &count, phys.data());
     65 }
     66 
     67 inline VkResult enumerate(std::vector<VkLayerProperties> &layer_props)
     68 {
     69     uint32_t count = 0;
     70     vk::EnumerateInstanceLayerProperties(&count, nullptr);
     71 
     72     layer_props.resize(count);
     73     return vk::EnumerateInstanceLayerProperties(&count, layer_props.data());
     74 }
     75 
     76 inline VkResult get(VkPhysicalDevice phy, std::vector<VkQueueFamilyProperties> &queues)
     77 {
     78     uint32_t count = 0;
     79     vk::GetPhysicalDeviceQueueFamilyProperties(phy, &count, nullptr);
     80 
     81     queues.resize(count);
     82     vk::GetPhysicalDeviceQueueFamilyProperties(phy, &count, queues.data());
     83 
     84     return VK_SUCCESS;
     85 }
     86 
     87 inline VkResult get(VkPhysicalDevice phy, VkSurfaceKHR surface, std::vector<VkSurfaceFormatKHR> &formats)
     88 {
     89     uint32_t count = 0;
     90     vk::GetPhysicalDeviceSurfaceFormatsKHR(phy, surface, &count, nullptr);
     91 
     92     formats.resize(count);
     93     return vk::GetPhysicalDeviceSurfaceFormatsKHR(phy, surface, &count, formats.data());
     94 }
     95 
     96 inline VkResult get(VkPhysicalDevice phy, VkSurfaceKHR surface, std::vector<VkPresentModeKHR> &modes)
     97 {
     98     uint32_t count = 0;
     99     vk::GetPhysicalDeviceSurfacePresentModesKHR(phy, surface, &count, nullptr);
    100 
    101     modes.resize(count);
    102     return vk::GetPhysicalDeviceSurfacePresentModesKHR(phy, surface, &count, modes.data());
    103 }
    104 
    105 inline VkResult get(VkDevice dev, VkSwapchainKHR swapchain, std::vector<VkImage> &images)
    106 {
    107     uint32_t count = 0;
    108     vk::GetSwapchainImagesKHR(dev, swapchain, &count, nullptr);
    109 
    110     images.resize(count);
    111     return vk::GetSwapchainImagesKHR(dev, swapchain, &count, images.data());
    112 }
    113 
    114 } // namespace vk
    115 
    116 #endif // HELPERS_H
    117