Debian Codegraph @

drivers/vulkan/rendering_device_driver_vulkan.h

raw symbols SCIP
1 /**************************************************************************/
2 /* rendering_device_driver_vulkan.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/templates/hash_map.h"
34 #include "core/templates/paged_allocator.h"
35 #include "drivers/vulkan/rendering_context_driver_vulkan.h"
36 #include "drivers/vulkan/rendering_shader_container_vulkan.h"
37 #include "servers/rendering/rendering_device_driver.h"
38
39 #ifdef DEBUG_ENABLED
40 #ifndef _MSC_VER
41 #define _DEBUG
42 #endif
43 #endif
44 #include "thirdparty/re-spirv/re-spirv.h"
45 #include "thirdparty/vulkan/vk_mem_alloc.h"
46
47 #include "drivers/vulkan/godot_vulkan.h"
48
49 // Design principles:
50 // - Vulkan structs are zero-initialized and fields not requiring a non-zero value are omitted (except in cases where expresivity reasons apply).
51 class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
52 /*****************/
53 /**** GENERIC ****/
54 /*****************/
55
56 struct CommandQueue;
57 struct SwapChain;
58 struct CommandBufferInfo;
59 struct RenderPassInfo;
60 struct Framebuffer;
61
62 struct Queue {
63 VkQueue queue = VK_NULL_HANDLE;
64 uint32_t virtual_count = 0;
65 BinaryMutex submit_mutex;
66 };
67
68 struct SubgroupCapabilities {
69 uint32_t size = 0;
70 uint32_t min_size = 0;
71 uint32_t max_size = 0;
72 VkShaderStageFlags supported_stages = 0;
73 VkSubgroupFeatureFlags supported_operations = 0;
74 VkBool32 quad_operations_in_all_stages = false;
75 bool size_control_is_supported = false;
76
77 uint32_t supported_stages_flags_rd() const;
78 String supported_stages_desc() const;
79 uint32_t supported_operations_flags_rd() const;
80 String supported_operations_desc() const;
81 };
82
83 struct ShaderCapabilities {
84 bool shader_float16_is_supported = false;
85 bool shader_int8_is_supported = false;
86 };
87
88 struct StorageBufferCapabilities {
89 bool storage_buffer_16_bit_access_is_supported = false;
90 bool uniform_and_storage_buffer_16_bit_access_is_supported = false;
91 bool storage_push_constant_16_is_supported = false;
92 bool storage_input_output_16 = false;
93 };
94
95 struct DeviceFunctions {
96 PFN_vkCreateSwapchainKHR CreateSwapchainKHR = nullptr;
97 PFN_vkDestroySwapchainKHR DestroySwapchainKHR = nullptr;
98 PFN_vkGetSwapchainImagesKHR GetSwapchainImagesKHR = nullptr;
99 PFN_vkAcquireNextImageKHR AcquireNextImageKHR = nullptr;
100 PFN_vkQueuePresentKHR QueuePresentKHR = nullptr;
101 PFN_vkCreateRenderPass2KHR CreateRenderPass2KHR = nullptr;
102 PFN_vkCmdEndRenderPass2KHR EndRenderPass2KHR = nullptr;
103
104 // Debug marker extensions.
105 PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;
106 PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;
107 PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;
108 PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;
109
110 // Debug device fault.
111 PFN_vkGetDeviceFaultInfoEXT GetDeviceFaultInfoEXT = nullptr;
112 };
113 // Debug marker extensions.
114 VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
115
116 VkDevice vk_device = VK_NULL_HANDLE;
117 RenderingContextDriverVulkan *context_driver = nullptr;
118 RenderingContextDriver::Device context_device = {};
119 uint32_t frame_count = 1;
120 VkPhysicalDevice physical_device = VK_NULL_HANDLE;
121 VkPhysicalDeviceProperties physical_device_properties = {};
122 VkPhysicalDeviceFeatures physical_device_features = {};
123 VkPhysicalDeviceFeatures requested_device_features = {};
124 HashMap<CharString, bool> requested_device_extensions;
125 HashSet<CharString> enabled_device_extension_names;
126 TightLocalVector<TightLocalVector<Queue>> queue_families;
127 TightLocalVector<VkQueueFamilyProperties> queue_family_properties;
128 RDD::Capabilities device_capabilities;
129 SubgroupCapabilities subgroup_capabilities;
130 MultiviewCapabilities multiview_capabilities;
131 FragmentShadingRateCapabilities fsr_capabilities;
132 FragmentDensityMapCapabilities fdm_capabilities;
133 ShaderCapabilities shader_capabilities;
134 StorageBufferCapabilities storage_buffer_capabilities;
135 RenderingShaderContainerFormatVulkan shader_container_format;
136 bool buffer_device_address_support = false;
137 bool vulkan_memory_model_support = false;
138 bool vulkan_memory_model_device_scope_support = false;
139 bool pipeline_cache_control_support = false;
140 bool device_fault_support = false;
141 bool framebuffer_depth_resolve = false;
142 #if defined(VK_TRACK_DEVICE_MEMORY)
143 bool device_memory_report_support = false;
144 #endif
145 #if defined(SWAPPY_FRAME_PACING_ENABLED)
146 // Swappy frame pacer for Android.
147 bool swappy_frame_pacer_enable = false;
148 uint8_t swappy_mode = 2; // See default value for display/window/frame_pacing/android/swappy_mode.
149 #endif
150 DeviceFunctions device_functions;
151
152 struct PendingFlushes {
153 LocalVector<VmaAllocation> allocations;
154 LocalVector<VkDeviceSize> offsets;
155 LocalVector<VkDeviceSize> sizes;
156 };
157
158 PendingFlushes pending_flushes;
159
160 struct PipelineStatistics {
161 Ref<FileAccess> file_access;
162 Mutex file_access_mutex;
163 };
164
165 PipelineStatistics pipeline_statistics;
166
167 void _register_requested_device_extension(const CharString &p_extension_name, bool p_required);
168 Error _initialize_device_extensions();
169 Error _check_device_features();
170 Error _check_device_capabilities();
171 void _choose_vrs_capabilities();
172 Error _add_queue_create_info(LocalVector<VkDeviceQueueCreateInfo> &r_queue_create_info);
173 Error _initialize_device(const LocalVector<VkDeviceQueueCreateInfo> &p_queue_create_info);
174 Error _initialize_allocator();
175 Error _initialize_pipeline_cache();
176 VkResult _create_render_pass(VkDevice p_device, const VkRenderPassCreateInfo2 *p_create_info, const VkAllocationCallbacks *p_allocator, VkRenderPass *p_render_pass);
177 bool _release_image_semaphore(CommandQueue *p_command_queue, uint32_t p_semaphore_index, bool p_release_on_swap_chain);
178 bool _recreate_image_semaphore(CommandQueue *p_command_queue, uint32_t p_semaphore_index, bool p_release_on_swap_chain);
179 void _set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name);
180
181 public:
182 Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override final;
183
184 private:
185 /****************/
186 /**** MEMORY ****/
187 /****************/
188
189 VmaAllocator allocator = nullptr;
190 HashMap<uint32_t, VmaPool> small_allocs_pools;
191
192 VmaPool _find_or_create_small_allocs_pool(uint32_t p_mem_type_index);
193
194 private:
195 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
196 // It's a circular buffer.
197 BufferID breadcrumb_buffer;
198 uint32_t breadcrumb_offset = 0u;
199 uint32_t breadcrumb_id = 0u;
200 #endif
201
202 public:
203 /*****************/
204 /**** BUFFERS ****/
205 /*****************/
206 struct BufferInfo {
207 VkBuffer vk_buffer = VK_NULL_HANDLE;
208 struct {
209 VmaAllocation handle = nullptr;
210 uint64_t size = UINT64_MAX;
211 } allocation;
212 uint64_t size = 0;
213 VkBufferView vk_view = VK_NULL_HANDLE; // For texel buffers.
214 // If dynamic buffer, then its range is [0; RenderingDeviceDriverVulkan::frame_count)
215 // else it's UINT32_MAX.
216 uint32_t frame_idx = UINT32_MAX;
217
218 bool is_dynamic() const { return frame_idx != UINT32_MAX; }
219 };
220
221 struct BufferDynamicInfo : BufferInfo {
222 uint8_t *persistent_ptr = nullptr;
223 #ifdef DEBUG_ENABLED
224 // For tracking that a persistent buffer isn't mapped twice in the same frame.
225 uint64_t last_frame_mapped = 0;
226 #endif
227 };
228
229 virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type, uint64_t p_frames_drawn) override final;
230 virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
231 virtual void buffer_free(BufferID p_buffer) override final;
232 virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;
233 virtual uint8_t *buffer_map(BufferID p_buffer) override final;
234 virtual void buffer_unmap(BufferID p_buffer) override final;
235 virtual uint8_t *buffer_persistent_map_advance(BufferID p_buffer, uint64_t p_frames_drawn) override final;
236 virtual uint64_t buffer_get_dynamic_offsets(Span<BufferID> p_buffers) override final;
237 virtual void buffer_flush(BufferID p_buffer) override final;
238 virtual uint64_t buffer_get_device_address(BufferID p_buffer) override final;
239
240 /*****************/
241 /**** TEXTURE ****/
242 /*****************/
243
244 struct TextureInfo {
245 VkImage vk_image = VK_NULL_HANDLE;
246 VkImageView vk_view = VK_NULL_HANDLE;
247 DataFormat rd_format = DATA_FORMAT_MAX;
248 VkImageCreateInfo vk_create_info = {};
249 VkImageViewCreateInfo vk_view_create_info = {};
250 struct {
251 VmaAllocation handle = nullptr;
252 VmaAllocationInfo info = {};
253 } allocation; // All 0/null if just a view.
254 #ifdef DEBUG_ENABLED
255 bool created_from_extension = false;
256 bool transient = false;
257 #endif
258 };
259
260 VkSampleCountFlagBits _ensure_supported_sample_count(TextureSamples p_requested_sample_count);
261
262 public:
263 virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
264 virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) override final;
265 virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
266 virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;
267 virtual void texture_free(TextureID p_texture) override final;
268 virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
269 virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
270 virtual Vector<uint8_t> texture_get_data(TextureID p_texture, uint32_t p_layer) override final;
271 virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
272 virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
273
274 /*****************/
275 /**** SAMPLER ****/
276 /*****************/
277 public:
278 virtual SamplerID sampler_create(const SamplerState &p_state) final override;
279 virtual void sampler_free(SamplerID p_sampler) final override;
280 virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
281
282 /**********************/
283 /**** VERTEX ARRAY ****/
284 /**********************/
285 private:
286 struct VertexFormatInfo {
287 TightLocalVector<VkVertexInputBindingDescription> vk_bindings;
288 TightLocalVector<VkVertexInputAttributeDescription> vk_attributes;
289 VkPipelineVertexInputStateCreateInfo vk_create_info = {};
290 };
291
292 public:
293 virtual VertexFormatID vertex_format_create(Span<VertexAttribute> p_vertex_attribs, const VertexAttributeBindingsMap &p_vertex_bindings) override final;
294 virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
295
296 /******************/
297 /**** BARRIERS ****/
298 /******************/
299
300 virtual void command_pipeline_barrier(
301 CommandBufferID p_cmd_buffer,
302 BitField<PipelineStageBits> p_src_stages,
303 BitField<PipelineStageBits> p_dst_stages,
304 VectorView<MemoryAccessBarrier> p_memory_barriers,
305 VectorView<BufferBarrier> p_buffer_barriers,
306 VectorView<TextureBarrier> p_texture_barriers) override final;
307
308 /****************/
309 /**** FENCES ****/
310 /****************/
311
312 private:
313 struct Fence {
314 VkFence vk_fence = VK_NULL_HANDLE;
315 CommandQueue *queue_signaled_from = nullptr;
316 };
317
318 public:
319 virtual FenceID fence_create() override final;
320 virtual Error fence_wait(FenceID p_fence) override final;
321 virtual void fence_free(FenceID p_fence) override final;
322
323 /********************/
324 /**** SEMAPHORES ****/
325 /********************/
326
327 virtual SemaphoreID semaphore_create() override final;
328 virtual void semaphore_free(SemaphoreID p_semaphore) override final;
329
330 /******************/
331 /**** COMMANDS ****/
332 /******************/
333
334 // ----- QUEUE FAMILY -----
335
336 virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override final;
337
338 // ----- QUEUE -----
339 private:
340 struct CommandQueue {
341 LocalVector<VkSemaphore> image_semaphores;
342 LocalVector<SwapChain *> image_semaphores_swap_chains;
343 LocalVector<uint32_t> pending_semaphores_for_execute;
344 LocalVector<uint32_t> pending_semaphores_for_fence;
345 LocalVector<uint32_t> free_image_semaphores;
346 LocalVector<Pair<Fence *, uint32_t>> image_semaphores_for_fences;
347 uint32_t queue_family = 0;
348 uint32_t queue_index = 0;
349 };
350
351 public:
352 virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override final;
353 virtual Error command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) override final;
354 virtual void command_queue_free(CommandQueueID p_cmd_queue) override final;
355
356 private:
357 // ----- POOL -----
358
359 struct CommandPool {
360 VkCommandPool vk_command_pool = VK_NULL_HANDLE;
361 CommandBufferType buffer_type = COMMAND_BUFFER_TYPE_PRIMARY;
362 LocalVector<CommandBufferInfo *> command_buffers_created;
363 };
364
365 public:
366 virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
367 virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override final;
368 virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
369
370 private:
371 // ----- BUFFER -----
372
373 struct CommandBufferInfo {
374 VkCommandBuffer vk_command_buffer = VK_NULL_HANDLE;
375 Framebuffer *active_framebuffer = nullptr;
376 RenderPassInfo *active_render_pass = nullptr;
377 };
378
379 public:
380 virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
381 virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
382 virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
383 virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
384 virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
385
386 /********************/
387 /**** SWAP CHAIN ****/
388 /********************/
389
390 private:
391 struct SwapChain {
392 VkSwapchainKHR vk_swapchain = VK_NULL_HANDLE;
393 RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
394 VkFormat format = VK_FORMAT_UNDEFINED;
395 VkColorSpaceKHR color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
396 TightLocalVector<VkImage> images;
397 TightLocalVector<VkImageView> image_views;
398 TightLocalVector<VkSemaphore> present_semaphores;
399 TightLocalVector<FramebufferID> framebuffers;
400 LocalVector<CommandQueue *> command_queues_acquired;
401 LocalVector<uint32_t> command_queues_acquired_semaphores;
402 RenderPassID render_pass;
403 int pre_transform_rotation_degrees = 0;
404 uint32_t image_index = 0;
405 #ifdef ANDROID_ENABLED
406 uint64_t refresh_duration = 0;
407 #endif
408 };
409
410 void _swap_chain_release(SwapChain *p_swap_chain);
411
412 public:
413 virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override final;
414 virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override final;
415 virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override final;
416 virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override final;
417 virtual int swap_chain_get_pre_rotation_degrees(SwapChainID p_swap_chain) override final;
418 virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override final;
419 virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) override final;
420 virtual void swap_chain_free(SwapChainID p_swap_chain) override final;
421
422 private:
423 /*********************/
424 /**** FRAMEBUFFER ****/
425 /*********************/
426
427 struct Framebuffer {
428 VkFramebuffer vk_framebuffer = VK_NULL_HANDLE;
429
430 // Only filled in if the framebuffer uses a fragment density map with offsets. Unused otherwise.
431 uint32_t fragment_density_map_offsets_layers = 0;
432
433 // Only filled in by a framebuffer created by a swap chain. Unused otherwise.
434 VkImage swap_chain_image = VK_NULL_HANDLE;
435 VkImageSubresourceRange swap_chain_image_subresource_range = {};
436 bool swap_chain_acquired = false;
437 };
438
439 public:
440 virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
441 virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
442
443 /****************/
444 /**** SHADER ****/
445 /****************/
446 private:
447 struct ShaderInfo {
448 String name;
449 VkShaderStageFlags vk_push_constant_stages = 0;
450 TightLocalVector<VkPipelineShaderStageCreateInfo> vk_stages_create_info;
451 TightLocalVector<VkDescriptorSetLayout> vk_descriptor_set_layouts;
452 TightLocalVector<respv::Shader> respv_stage_shaders;
453 TightLocalVector<Vector<uint8_t>> spirv_stage_bytes;
454 TightLocalVector<uint64_t> original_stage_size;
455 VkPipelineLayout vk_pipeline_layout = VK_NULL_HANDLE;
456 };
457
458 public:
459 virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) override final;
460 virtual void shader_free(ShaderID p_shader) override final;
461
462 virtual void shader_destroy_modules(ShaderID p_shader) override final;
463 /*********************/
464 /**** UNIFORM SET ****/
465 /*********************/
466
467 // Descriptor sets require allocation from a pool.
468 // The documentation on how to use pools properly
469 // is scarce, and the documentation is strange.
470 //
471 // Basically, you can mix and match pools as you
472 // like, but you'll run into fragmentation issues.
473 // Because of this, the recommended approach is to
474 // create a pool for every descriptor set type, as
475 // this prevents fragmentation.
476 //
477 // This is implemented here as a having a list of
478 // pools (each can contain up to 64 sets) for each
479 // set layout. The amount of sets for each type
480 // is used as the key.
481
482 private:
483 static const uint32_t MAX_UNIFORM_POOL_ELEMENT = 65535;
484
485 struct DescriptorSetPoolKey {
486 uint16_t uniform_type[UNIFORM_TYPE_MAX] = {};
487
488 bool operator<(const DescriptorSetPoolKey &p_other) const {
489 return memcmp(uniform_type, p_other.uniform_type, sizeof(uniform_type)) < 0;
490 }
491 };
492
493 using DescriptorSetPools = RBMap<DescriptorSetPoolKey, HashMap<VkDescriptorPool, uint32_t>>;
494 DescriptorSetPools descriptor_set_pools;
495 uint32_t max_descriptor_sets_per_pool = 0;
496
497 HashMap<int, DescriptorSetPools> linear_descriptor_set_pools;
498 bool linear_descriptor_pools_enabled = true;
499 VkDescriptorPool _descriptor_set_pool_create(const DescriptorSetPoolKey &p_key, bool p_linear_pool);
500 void _descriptor_set_pool_unreference(DescriptorSetPools::Iterator p_pool_sets_it, VkDescriptorPool p_vk_descriptor_pool, int p_linear_pool_index);
501
502 // Global flag to toggle usage of immutable sampler when creating pipeline layouts.
503 // It cannot change after creating the PSOs, since we need to skipping samplers when creating uniform sets.
504 bool immutable_samplers_enabled = true;
505
506 struct UniformSetInfo {
507 VkDescriptorSet vk_descriptor_set = VK_NULL_HANDLE;
508 VkDescriptorPool vk_descriptor_pool = VK_NULL_HANDLE;
509 VkDescriptorPool vk_linear_descriptor_pool = VK_NULL_HANDLE;
510 DescriptorSetPools::Iterator pool_sets_it;
511 TightLocalVector<BufferInfo const *, uint32_t> dynamic_buffers;
512 };
513
514 bool adreno_5xx_empty_descriptor_set_layout_workaround = false;
515
516 public:
517 virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;
518 virtual void linear_uniform_set_pools_reset(int p_linear_pool_index) override final;
519 virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
520 virtual bool uniform_sets_have_linear_pools() const override final;
521 virtual uint32_t uniform_sets_get_dynamic_offsets(VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) const override final;
522
523 // ----- COMMANDS -----
524
525 virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
526
527 /******************/
528 /**** TRANSFER ****/
529 /******************/
530
531 virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
532 virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
533
534 virtual void command_copy_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<TextureCopyRegion> p_regions) override final;
535 virtual void command_resolve_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) override final;
536 virtual void command_clear_color_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, const Color &p_color, const TextureSubresourceRange &p_subresources) override final;
537 virtual void command_clear_depth_stencil_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, float p_depth, uint8_t p_stencil, const TextureSubresourceRange &p_subresources) override final;
538
539 virtual void command_copy_buffer_to_texture(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<BufferTextureCopyRegion> p_regions) override final;
540 virtual void command_copy_texture_to_buffer(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, BufferID p_dst_buffer, VectorView<BufferTextureCopyRegion> p_regions) override final;
541
542 /******************/
543 /**** PIPELINE ****/
544 /******************/
545 private:
546 struct PipelineCacheHeader {
547 uint32_t magic = 0;
548 uint32_t data_size = 0;
549 uint64_t data_hash = 0;
550 uint32_t vendor_id = 0;
551 uint32_t device_id = 0;
552 uint32_t driver_version = 0;
553 uint8_t uuid[VK_UUID_SIZE] = {};
554 uint8_t driver_abi = 0;
555 };
556
557 struct PipelineCache {
558 String file_path;
559 size_t current_size = 0;
560 Vector<uint8_t> buffer; // Header then data.
561 VkPipelineCache vk_cache = VK_NULL_HANDLE;
562 };
563
564 static int caching_instance_count;
565 PipelineCache pipelines_cache;
566 String pipeline_cache_id;
567 HashMap<uint64_t, bool> has_comp_alpha;
568
569 public:
570 virtual void pipeline_free(PipelineID p_pipeline) override final;
571
572 // ----- BINDING -----
573
574 virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) override final;
575
576 // ----- CACHE -----
577
578 virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
579 virtual void pipeline_cache_free() override final;
580 virtual size_t pipeline_cache_query_size() override final;
581 virtual Vector<uint8_t> pipeline_cache_serialize() override final;
582
583 /*******************/
584 /**** RENDERING ****/
585 /*******************/
586
587 private:
588 // ----- SUBPASS -----
589
590 struct RenderPassInfo {
591 VkRenderPass vk_render_pass = VK_NULL_HANDLE;
592 bool uses_fragment_density_map = false;
593 };
594
595 public:
596 virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count, AttachmentReference p_fragment_density_map_attachment) override final;
597 virtual void render_pass_free(RenderPassID p_render_pass) override final;
598
599 // ----- COMMANDS -----
600
601 virtual void command_begin_render_pass(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, FramebufferID p_framebuffer, CommandBufferType p_cmd_buffer_type, const Rect2i &p_rect, VectorView<RenderPassClearValue> p_clear_values) override final;
602 virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
603 virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
604 virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
605 virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
606 virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
607
608 // Binding.
609 virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
610 virtual void command_bind_render_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) override final;
611
612 // Drawing.
613 virtual void command_render_draw(CommandBufferID p_cmd_buffer, uint32_t p_vertex_count, uint32_t p_instance_count, uint32_t p_base_vertex, uint32_t p_first_instance) override final;
614 virtual void command_render_draw_indexed(CommandBufferID p_cmd_buffer, uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index, int32_t p_vertex_offset, uint32_t p_first_instance) override final;
615 virtual void command_render_draw_indexed_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
616 virtual void command_render_draw_indexed_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
617 virtual void command_render_draw_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
618 virtual void command_render_draw_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
619
620 // Buffer binding.
621 virtual void command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets, uint64_t p_dynamic_offsets) override final;
622 virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
623
624 // Dynamic state.
625 virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
626 virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
627
628 // ----- PIPELINE -----
629
630 virtual PipelineID render_pipeline_create(
631 ShaderID p_shader,
632 VertexFormatID p_vertex_format,
633 RenderPrimitive p_render_primitive,
634 PipelineRasterizationState p_rasterization_state,
635 PipelineMultisampleState p_multisample_state,
636 PipelineDepthStencilState p_depth_stencil_state,
637 PipelineColorBlendState p_blend_state,
638 VectorView<int32_t> p_color_attachments,
639 BitField<PipelineDynamicStateFlags> p_dynamic_state,
640 RenderPassID p_render_pass,
641 uint32_t p_render_subpass,
642 VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
643
644 /*****************/
645 /**** COMPUTE ****/
646 /*****************/
647
648 // ----- COMMANDS -----
649
650 // Binding.
651 virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
652 virtual void command_bind_compute_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) override final;
653
654 // Dispatching.
655 virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) override final;
656 virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
657
658 // ----- PIPELINE -----
659
660 virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
661
662 /*****************/
663 /**** QUERIES ****/
664 /*****************/
665
666 // ----- TIMESTAMP -----
667
668 // Basic.
669 virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
670 virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
671 virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
672 virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
673
674 // Commands.
675 virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
676 virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
677
678 /****************/
679 /**** LABELS ****/
680 /****************/
681
682 virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
683 virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
684
685 /****************/
686 /**** DEBUG *****/
687 /****************/
688 virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
689 void print_lost_device_info();
690 void on_device_lost() const;
691 static String get_vulkan_result(VkResult err);
692
693 /********************/
694 /**** SUBMISSION ****/
695 /********************/
696
697 virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
698 virtual void end_segment() override final;
699
700 /**************/
701 /**** MISC ****/
702 /**************/
703
704 virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
705 virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
706 virtual uint64_t get_total_memory_used() override final;
707 virtual uint64_t get_lazily_memory_used() override final;
708 virtual uint64_t limit_get(Limit p_limit) override final;
709 virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
710 virtual bool has_feature(Features p_feature) override final;
711 virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
712 virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() override final;
713 virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() override final;
714 virtual String get_api_name() const override final;
715 virtual String get_api_version() const override final;
716 virtual String get_pipeline_cache_uuid() const override final;
717 virtual const Capabilities &get_capabilities() const override final;
718 virtual const RenderingShaderContainerFormat &get_shader_container_format() const override final;
719
720 virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
721
722 private:
723 /*********************/
724 /**** BOOKKEEPING ****/
725 /*********************/
726
727 using VersatileResource = VersatileResourceTemplate<
728 BufferInfo,
729 TextureInfo,
730 VertexFormatInfo,
731 ShaderInfo,
732 UniformSetInfo,
733 RenderPassInfo,
734 CommandBufferInfo>;
735 PagedAllocator<VersatileResource, true> resources_allocator;
736
737 /******************/
738
739 public:
740 RenderingDeviceDriverVulkan(RenderingContextDriverVulkan *p_context_driver);
741 virtual ~RenderingDeviceDriverVulkan();
742 };
743
744 using VKC = RenderingContextDriverVulkan;