Line data Source code
1 : /**
2 : Copyright (c) 2020-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef XENOLITH_BACKEND_VK_XLVK_H_
25 : #define XENOLITH_BACKEND_VK_XLVK_H_
26 :
27 : #include "XLCommon.h"
28 :
29 : #if LINUX
30 : #define VK_USE_PLATFORM_XCB_KHR 1
31 : #define VK_USE_PLATFORM_WAYLAND_KHR 1
32 : #endif
33 :
34 : #if MACOS
35 : #define VK_USE_PLATFORM_METAL_EXT 1
36 : #define VK_ENABLE_BETA_EXTENSIONS 1
37 : #endif
38 :
39 : #if ANDROID
40 : #define VK_USE_PLATFORM_ANDROID_KHR 1
41 : #endif
42 :
43 : #if WIN32
44 : #define VK_USE_PLATFORM_WIN32_KHR 1
45 : #endif
46 :
47 : #include <vulkan/vulkan.h>
48 :
49 : namespace STAPPLER_VERSIONIZED stappler::xenolith::vk {
50 :
51 : #if LINUX
52 : #define VK_BUFFER_USAGE_RAY_TRACING_BIT_KHR 0x00000400
53 : #define VK_BUFFER_USAGE_RAY_TRACING_BIT_NV VK_BUFFER_USAGE_RAY_TRACING_BIT_KHR
54 : #endif
55 :
56 :
57 : #define XL_VK_MIN_LOADER_MESSAGE_SEVERITY VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT
58 : #define XL_VK_MIN_MESSAGE_SEVERITY VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT
59 :
60 : #if DEBUG
61 : #define XL_VKAPI_DEBUG 0
62 : #define VK_HOOK_DEBUG 0 // enable engine hooks for Vulkan calls
63 : static constexpr bool s_enableValidationLayers = true;
64 : static constexpr bool s_printVkInfo = true;
65 : #else
66 : #define VK_HOOK_DEBUG 0 // enable engine hooks for Vulkan calls
67 : static constexpr bool s_enableValidationLayers = false;
68 : static constexpr bool s_printVkInfo = true;
69 : #endif
70 :
71 : SPUNUSED static const char * const s_validationLayers[] = {
72 : "VK_LAYER_KHRONOS_validation"
73 : };
74 :
75 : }
76 :
77 : // Debug tools
78 :
79 : // Для кадров с ошибками ожидать освобождения устройства (vkDeviceWaitIdle) при завершении
80 : #define XL_VK_FINALIZE_INVALID_FRAMES 0
81 :
82 : #include "XLVkConfig.h"
83 : #include "XLVkTable.h"
84 :
85 : namespace STAPPLER_VERSIONIZED stappler::xenolith::vk {
86 :
87 : using BufferInfo = core::BufferInfo;
88 : using ImageInfo = core::ImageInfo;
89 : using ImageInfoData = core::ImageInfoData;
90 : using ImageViewInfo = core::ImageViewInfo;
91 : using SamplerInfo = core::SamplerInfo;
92 :
93 : class Instance;
94 :
95 : static const char * const s_requiredExtension[] = {
96 : VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
97 : nullptr
98 : };
99 :
100 : static const char * const s_optionalExtension[] = {
101 : nullptr
102 : };
103 :
104 : static const char * const s_requiredDeviceExtensions[] = {
105 : VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME,
106 : nullptr
107 : };
108 :
109 : static const char * const s_optionalDeviceExtensions[] = {
110 : // Descriptor indexing
111 : VK_KHR_MAINTENANCE3_EXTENSION_NAME,
112 : VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME,
113 :
114 : // DrawInderectCount
115 : VK_KHR_DRAW_INDIRECT_COUNT_EXTENSION_NAME,
116 :
117 : // 16-bit, 8-bit shader storage
118 : VK_KHR_16BIT_STORAGE_EXTENSION_NAME,
119 : VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
120 : VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME,
121 :
122 : // BufferDeviceAddress
123 : VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME,
124 : VK_EXT_MEMORY_BUDGET_EXTENSION_NAME,
125 : VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
126 : VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
127 : #if __APPLE__
128 : VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME,
129 : #endif
130 : nullptr
131 : };
132 :
133 : enum class ExtensionFlags {
134 : None,
135 : Maintenance3 = 1 << 0,
136 : DescriptorIndexing = 1 << 1,
137 : DrawIndirectCount = 1 << 2,
138 : Storage16Bit = 1 << 3,
139 : Storage8Bit = 1 << 4,
140 : DeviceAddress = 1 << 5,
141 : ShaderFloat16 = 1 << 6,
142 : ShaderInt8 = 1 << 7,
143 : MemoryBudget = 1 << 8,
144 : GetMemoryRequirements2 = 1 << 9,
145 : DedicatedAllocation = 1 << 10,
146 : BufferDeviceAddress = 1 << 11,
147 : Portability = 1 << 12,
148 : };
149 :
150 : SP_DEFINE_ENUM_AS_MASK(ExtensionFlags);
151 :
152 : static const char * const s_promotedVk11Extensions[] = {
153 : VK_KHR_16BIT_STORAGE_EXTENSION_NAME,
154 : VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
155 : VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
156 : VK_KHR_DESCRIPTOR_UPDATE_TEMPLATE_EXTENSION_NAME,
157 : VK_KHR_DEVICE_GROUP_EXTENSION_NAME,
158 : VK_KHR_DEVICE_GROUP_CREATION_EXTENSION_NAME,
159 : VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME,
160 : VK_KHR_EXTERNAL_FENCE_CAPABILITIES_EXTENSION_NAME,
161 : VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
162 : VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
163 : VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
164 : VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME,
165 : VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
166 : VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
167 : VK_KHR_MAINTENANCE1_EXTENSION_NAME,
168 : VK_KHR_MAINTENANCE2_EXTENSION_NAME,
169 : VK_KHR_MAINTENANCE3_EXTENSION_NAME,
170 : VK_KHR_MULTIVIEW_EXTENSION_NAME,
171 : VK_KHR_RELAXED_BLOCK_LAYOUT_EXTENSION_NAME,
172 : VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
173 : VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME,
174 : VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME,
175 : VK_KHR_VARIABLE_POINTERS_EXTENSION_NAME,
176 : nullptr
177 : };
178 :
179 : static const char * const s_promotedVk12Extensions[] = {
180 : VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
181 : VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME,
182 : VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME,
183 : VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME,
184 : VK_KHR_DRAW_INDIRECT_COUNT_EXTENSION_NAME,
185 : VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME,
186 : VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
187 : VK_KHR_IMAGELESS_FRAMEBUFFER_EXTENSION_NAME,
188 : VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
189 : VK_KHR_SEPARATE_DEPTH_STENCIL_LAYOUTS_EXTENSION_NAME,
190 : VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME,
191 : VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME,
192 : VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME,
193 : VK_KHR_SHADER_SUBGROUP_EXTENDED_TYPES_EXTENSION_NAME,
194 : VK_KHR_SPIRV_1_4_EXTENSION_NAME,
195 : VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
196 : VK_KHR_UNIFORM_BUFFER_STANDARD_LAYOUT_EXTENSION_NAME,
197 : VK_KHR_VULKAN_MEMORY_MODEL_EXTENSION_NAME,
198 : VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME,
199 : VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
200 : VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME,
201 : VK_EXT_SCALAR_BLOCK_LAYOUT_EXTENSION_NAME,
202 : VK_EXT_SEPARATE_STENCIL_USAGE_EXTENSION_NAME,
203 : VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME,
204 : nullptr
205 : };
206 :
207 : static const char * const s_promotedVk13Extensions[] = {
208 : VK_KHR_COPY_COMMANDS_2_EXTENSION_NAME,
209 : VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME,
210 : VK_KHR_FORMAT_FEATURE_FLAGS_2_EXTENSION_NAME,
211 : VK_KHR_MAINTENANCE_4_EXTENSION_NAME,
212 : VK_KHR_SHADER_INTEGER_DOT_PRODUCT_EXTENSION_NAME,
213 : VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME,
214 : VK_KHR_SHADER_TERMINATE_INVOCATION_EXTENSION_NAME,
215 : VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME,
216 : VK_KHR_ZERO_INITIALIZE_WORKGROUP_MEMORY_EXTENSION_NAME,
217 : VK_EXT_4444_FORMATS_EXTENSION_NAME,
218 : VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME,
219 : VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME,
220 : VK_EXT_IMAGE_ROBUSTNESS_EXTENSION_NAME,
221 : VK_EXT_INLINE_UNIFORM_BLOCK_EXTENSION_NAME,
222 : VK_EXT_PIPELINE_CREATION_CACHE_CONTROL_EXTENSION_NAME,
223 : VK_EXT_PIPELINE_CREATION_FEEDBACK_EXTENSION_NAME,
224 : VK_EXT_PRIVATE_DATA_EXTENSION_NAME,
225 : VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME,
226 : VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME,
227 : VK_EXT_TEXEL_BUFFER_ALIGNMENT_EXTENSION_NAME,
228 : VK_EXT_TEXTURE_COMPRESSION_ASTC_HDR_EXTENSION_NAME,
229 : VK_EXT_TOOLING_INFO_EXTENSION_NAME,
230 : VK_EXT_YCBCR_2PLANE_444_FORMATS_EXTENSION_NAME,
231 : nullptr
232 : };
233 :
234 : enum class QueueOperations : uint32_t {
235 : None,
236 : Graphics = VK_QUEUE_GRAPHICS_BIT,
237 : Compute = VK_QUEUE_COMPUTE_BIT,
238 : Transfer = VK_QUEUE_TRANSFER_BIT,
239 : SparceBinding = VK_QUEUE_SPARSE_BINDING_BIT,
240 : Present = 0x8000'0000,
241 : };
242 :
243 : SP_DEFINE_ENUM_AS_MASK(QueueOperations)
244 :
245 : QueueOperations getQueueOperations(VkQueueFlags, bool present);
246 : QueueOperations getQueueOperations(core::PassType);
247 : String getQueueOperationsDesc(QueueOperations);
248 : VkShaderStageFlagBits getVkStageBits(core::ProgramStage);
249 :
250 : StringView getVkFormatName(VkFormat fmt);
251 : StringView getVkColorSpaceName(VkColorSpaceKHR fmt);
252 : StringView getVkResultName(VkResult res);
253 :
254 : String getVkMemoryPropertyFlags(VkMemoryPropertyFlags);
255 :
256 : bool checkIfExtensionAvailable(uint32_t apiVersion, const char *name, const Vector<VkExtensionProperties> &available,
257 : Vector<StringView> &optionals, Vector<StringView> &promoted, ExtensionFlags &flags);
258 :
259 : bool isPromotedExtension(uint32_t apiVersion, StringView name);
260 :
261 : size_t getFormatBlockSize(VkFormat);
262 :
263 : VkPresentModeKHR getVkPresentMode(core::PresentMode presentMode);
264 :
265 : template <typename T>
266 9054 : void sanitizeVkStruct(T &t) {
267 9054 : ::memset(&t, 0, sizeof(T));
268 9054 : }
269 :
270 : std::ostream &operator<< (std::ostream &stream, VkResult res);
271 :
272 : }
273 :
274 : #if WIN32
275 : #undef interface
276 : #endif
277 :
278 : #endif /* XENOLITH_BACKEND_VK_XLVK_H_ */
|