2 * Copyright 2017 Jacob Lifshay
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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 THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #ifndef VULKAN_ICD_VULKAN_ICD_H_
24 #define VULKAN_ICD_VULKAN_ICD_H_
26 #include "vulkan/vulkan.h"
27 #include "vulkan/vk_icd.h"
28 #include "vulkan/api_objects.h"
29 #include <type_traits>
32 #include <initializer_list>
37 extern "C" VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(VkInstance instance
,
40 typedef PFN_vkVoidFunction (*VKAPI_PTR PFN_vk_icdGetInstanceProcAddr
)(VkInstance instance
,
47 class Vulkan_loader_interface
50 enum class Version
: std::uint32_t
61 static Vulkan_loader_interface
*get() noexcept
;
64 constexpr Vulkan_loader_interface() noexcept
69 constexpr Version
get_negotiated_version() const noexcept
71 return negotiated_version
;
75 enum class Procedure_address_scope
81 PFN_vkVoidFunction
get_procedure_address(const char *name
,
82 Procedure_address_scope scope
) noexcept
;
83 PFN_vkVoidFunction
get_instance_proc_addr(VkInstance instance
, const char *name
) noexcept
;
84 VkResult
create_instance(const VkInstanceCreateInfo
*create_info
,
85 const VkAllocationCallbacks
*allocator
,
86 VkInstance
*instance
) noexcept
;
87 VkResult
enumerate_instance_extension_properties(const char *layer_name
,
88 uint32_t *property_count
,
89 VkExtensionProperties
*properties
) noexcept
;
90 VkResult
enumerate_device_extension_properties(VkPhysicalDevice physical_device
,
91 const char *layer_name
,
92 uint32_t *property_count
,
93 VkExtensionProperties
*properties
) noexcept
;
96 Version negotiated_version
= Version::Version_0
;
99 static_assert(std::is_trivially_destructible
<Vulkan_loader_interface
>::value
, "");
100 static_assert(std::is_literal_type
<Vulkan_loader_interface
>::value
, "");
102 template <typename T
>
103 VkResult
vulkan_enumerate_list_helper(std::uint32_t *api_value_count
,
105 const T
*generated_values
,
106 std::size_t generated_value_count
) noexcept
108 static_assert(std::is_trivially_copyable
<T
>::value
, "");
109 assert(api_value_count
);
110 assert(static_cast<std::uint32_t>(generated_value_count
) == generated_value_count
);
113 *api_value_count
= generated_value_count
;
116 auto api_values_length
= *api_value_count
;
117 auto copy_length
= api_values_length
;
118 if(copy_length
> generated_value_count
)
119 copy_length
= generated_value_count
;
120 for(std::size_t i
= 0; i
< copy_length
; i
++)
121 api_values
[i
] = generated_values
[i
];
122 *api_value_count
= copy_length
;
123 if(copy_length
< generated_value_count
)
124 return VK_INCOMPLETE
;
128 template <typename T
>
129 VkResult
vulkan_enumerate_list_helper(std::uint32_t *api_value_count
,
131 std::initializer_list
<T
> generated_values
) noexcept
133 return vulkan_enumerate_list_helper(
134 api_value_count
, api_values
, generated_values
.begin(), generated_values
.size());
137 void print_exception(std::exception
&e
) noexcept
;
139 template <typename Fn
>
140 VkResult
catch_exceptions_and_return_result(Fn
&&fn
) noexcept
144 return std::forward
<Fn
>(fn
)();
146 catch(std::bad_alloc
&)
148 return VK_ERROR_OUT_OF_HOST_MEMORY
;
150 catch(std::exception
&e
)
153 throw; // send to std::terminate
155 // explicitly don't catch other exceptions and let std::terminate handle them
160 #endif // VULKAN_ICD_VULKAN_ICD_H_