Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/vulkan/rendering_device_driver_vulkan.h
9903 views
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/vulkan/vk_mem_alloc.h"
45
46
#include "drivers/vulkan/godot_vulkan.h"
47
48
// Design principles:
49
// - Vulkan structs are zero-initialized and fields not requiring a non-zero value are omitted (except in cases where expresivity reasons apply).
50
class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
51
/*****************/
52
/**** GENERIC ****/
53
/*****************/
54
55
struct CommandQueue;
56
struct SwapChain;
57
struct CommandBufferInfo;
58
struct RenderPassInfo;
59
struct Framebuffer;
60
61
struct Queue {
62
VkQueue queue = VK_NULL_HANDLE;
63
uint32_t virtual_count = 0;
64
BinaryMutex submit_mutex;
65
};
66
67
struct SubgroupCapabilities {
68
uint32_t size = 0;
69
uint32_t min_size = 0;
70
uint32_t max_size = 0;
71
VkShaderStageFlags supported_stages = 0;
72
VkSubgroupFeatureFlags supported_operations = 0;
73
VkBool32 quad_operations_in_all_stages = false;
74
bool size_control_is_supported = false;
75
76
uint32_t supported_stages_flags_rd() const;
77
String supported_stages_desc() const;
78
uint32_t supported_operations_flags_rd() const;
79
String supported_operations_desc() const;
80
};
81
82
struct ShaderCapabilities {
83
bool shader_float16_is_supported = false;
84
bool shader_int8_is_supported = false;
85
};
86
87
struct StorageBufferCapabilities {
88
bool storage_buffer_16_bit_access_is_supported = false;
89
bool uniform_and_storage_buffer_16_bit_access_is_supported = false;
90
bool storage_push_constant_16_is_supported = false;
91
bool storage_input_output_16 = false;
92
};
93
94
struct DeviceFunctions {
95
PFN_vkCreateSwapchainKHR CreateSwapchainKHR = nullptr;
96
PFN_vkDestroySwapchainKHR DestroySwapchainKHR = nullptr;
97
PFN_vkGetSwapchainImagesKHR GetSwapchainImagesKHR = nullptr;
98
PFN_vkAcquireNextImageKHR AcquireNextImageKHR = nullptr;
99
PFN_vkQueuePresentKHR QueuePresentKHR = nullptr;
100
PFN_vkCreateRenderPass2KHR CreateRenderPass2KHR = nullptr;
101
PFN_vkCmdEndRenderPass2KHR EndRenderPass2KHR = nullptr;
102
103
// Debug marker extensions.
104
PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;
105
PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;
106
PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;
107
PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;
108
109
// Debug device fault.
110
PFN_vkGetDeviceFaultInfoEXT GetDeviceFaultInfoEXT = nullptr;
111
};
112
// Debug marker extensions.
113
VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
114
115
VkDevice vk_device = VK_NULL_HANDLE;
116
RenderingContextDriverVulkan *context_driver = nullptr;
117
RenderingContextDriver::Device context_device = {};
118
uint32_t frame_count = 1;
119
VkPhysicalDevice physical_device = VK_NULL_HANDLE;
120
VkPhysicalDeviceProperties physical_device_properties = {};
121
VkPhysicalDeviceFeatures physical_device_features = {};
122
VkPhysicalDeviceFeatures requested_device_features = {};
123
HashMap<CharString, bool> requested_device_extensions;
124
HashSet<CharString> enabled_device_extension_names;
125
TightLocalVector<TightLocalVector<Queue>> queue_families;
126
TightLocalVector<VkQueueFamilyProperties> queue_family_properties;
127
RDD::Capabilities device_capabilities;
128
SubgroupCapabilities subgroup_capabilities;
129
MultiviewCapabilities multiview_capabilities;
130
FragmentShadingRateCapabilities fsr_capabilities;
131
FragmentDensityMapCapabilities fdm_capabilities;
132
ShaderCapabilities shader_capabilities;
133
StorageBufferCapabilities storage_buffer_capabilities;
134
RenderingShaderContainerFormatVulkan shader_container_format;
135
bool buffer_device_address_support = false;
136
bool vulkan_memory_model_support = false;
137
bool vulkan_memory_model_device_scope_support = false;
138
bool pipeline_cache_control_support = false;
139
bool device_fault_support = false;
140
#if defined(VK_TRACK_DEVICE_MEMORY)
141
bool device_memory_report_support = false;
142
#endif
143
#if defined(SWAPPY_FRAME_PACING_ENABLED)
144
// Swappy frame pacer for Android.
145
bool swappy_frame_pacer_enable = false;
146
uint8_t swappy_mode = 2; // See default value for display/window/frame_pacing/android/swappy_mode.
147
#endif
148
DeviceFunctions device_functions;
149
150
void _register_requested_device_extension(const CharString &p_extension_name, bool p_required);
151
Error _initialize_device_extensions();
152
Error _check_device_features();
153
Error _check_device_capabilities();
154
void _choose_vrs_capabilities();
155
Error _add_queue_create_info(LocalVector<VkDeviceQueueCreateInfo> &r_queue_create_info);
156
Error _initialize_device(const LocalVector<VkDeviceQueueCreateInfo> &p_queue_create_info);
157
Error _initialize_allocator();
158
Error _initialize_pipeline_cache();
159
VkResult _create_render_pass(VkDevice p_device, const VkRenderPassCreateInfo2 *p_create_info, const VkAllocationCallbacks *p_allocator, VkRenderPass *p_render_pass);
160
bool _release_image_semaphore(CommandQueue *p_command_queue, uint32_t p_semaphore_index, bool p_release_on_swap_chain);
161
bool _recreate_image_semaphore(CommandQueue *p_command_queue, uint32_t p_semaphore_index, bool p_release_on_swap_chain);
162
void _set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name);
163
164
public:
165
Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override final;
166
167
private:
168
/****************/
169
/**** MEMORY ****/
170
/****************/
171
172
VmaAllocator allocator = nullptr;
173
HashMap<uint32_t, VmaPool> small_allocs_pools;
174
175
VmaPool _find_or_create_small_allocs_pool(uint32_t p_mem_type_index);
176
177
private:
178
#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
179
// It's a circular buffer.
180
BufferID breadcrumb_buffer;
181
uint32_t breadcrumb_offset = 0u;
182
uint32_t breadcrumb_id = 0u;
183
#endif
184
185
public:
186
/*****************/
187
/**** BUFFERS ****/
188
/*****************/
189
struct BufferInfo {
190
VkBuffer vk_buffer = VK_NULL_HANDLE;
191
struct {
192
VmaAllocation handle = nullptr;
193
uint64_t size = UINT64_MAX;
194
} allocation;
195
uint64_t size = 0;
196
VkBufferView vk_view = VK_NULL_HANDLE; // For texel buffers.
197
};
198
199
virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) override final;
200
virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
201
virtual void buffer_free(BufferID p_buffer) override final;
202
virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;
203
virtual uint8_t *buffer_map(BufferID p_buffer) override final;
204
virtual void buffer_unmap(BufferID p_buffer) override final;
205
virtual uint64_t buffer_get_device_address(BufferID p_buffer) override final;
206
207
/*****************/
208
/**** TEXTURE ****/
209
/*****************/
210
211
struct TextureInfo {
212
VkImage vk_image = VK_NULL_HANDLE;
213
VkImageView vk_view = VK_NULL_HANDLE;
214
DataFormat rd_format = DATA_FORMAT_MAX;
215
VkImageCreateInfo vk_create_info = {};
216
VkImageViewCreateInfo vk_view_create_info = {};
217
struct {
218
VmaAllocation handle = nullptr;
219
VmaAllocationInfo info = {};
220
} allocation; // All 0/null if just a view.
221
#ifdef DEBUG_ENABLED
222
bool created_from_extension = false;
223
bool transient = false;
224
#endif
225
};
226
227
VkSampleCountFlagBits _ensure_supported_sample_count(TextureSamples p_requested_sample_count);
228
229
public:
230
virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
231
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;
232
virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
233
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;
234
virtual void texture_free(TextureID p_texture) override final;
235
virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
236
virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
237
virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) override final;
238
virtual void texture_unmap(TextureID p_texture) override final;
239
virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
240
virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
241
242
/*****************/
243
/**** SAMPLER ****/
244
/*****************/
245
public:
246
virtual SamplerID sampler_create(const SamplerState &p_state) final override;
247
virtual void sampler_free(SamplerID p_sampler) final override;
248
virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
249
250
/**********************/
251
/**** VERTEX ARRAY ****/
252
/**********************/
253
private:
254
struct VertexFormatInfo {
255
TightLocalVector<VkVertexInputBindingDescription> vk_bindings;
256
TightLocalVector<VkVertexInputAttributeDescription> vk_attributes;
257
VkPipelineVertexInputStateCreateInfo vk_create_info = {};
258
};
259
260
public:
261
virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) override final;
262
virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
263
264
/******************/
265
/**** BARRIERS ****/
266
/******************/
267
268
virtual void command_pipeline_barrier(
269
CommandBufferID p_cmd_buffer,
270
BitField<PipelineStageBits> p_src_stages,
271
BitField<PipelineStageBits> p_dst_stages,
272
VectorView<MemoryBarrier> p_memory_barriers,
273
VectorView<BufferBarrier> p_buffer_barriers,
274
VectorView<TextureBarrier> p_texture_barriers) override final;
275
276
/****************/
277
/**** FENCES ****/
278
/****************/
279
280
private:
281
struct Fence {
282
VkFence vk_fence = VK_NULL_HANDLE;
283
CommandQueue *queue_signaled_from = nullptr;
284
};
285
286
public:
287
virtual FenceID fence_create() override final;
288
virtual Error fence_wait(FenceID p_fence) override final;
289
virtual void fence_free(FenceID p_fence) override final;
290
291
/********************/
292
/**** SEMAPHORES ****/
293
/********************/
294
295
virtual SemaphoreID semaphore_create() override final;
296
virtual void semaphore_free(SemaphoreID p_semaphore) override final;
297
298
/******************/
299
/**** COMMANDS ****/
300
/******************/
301
302
// ----- QUEUE FAMILY -----
303
304
virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override final;
305
306
// ----- QUEUE -----
307
private:
308
struct CommandQueue {
309
LocalVector<VkSemaphore> image_semaphores;
310
LocalVector<SwapChain *> image_semaphores_swap_chains;
311
LocalVector<uint32_t> pending_semaphores_for_execute;
312
LocalVector<uint32_t> pending_semaphores_for_fence;
313
LocalVector<uint32_t> free_image_semaphores;
314
LocalVector<Pair<Fence *, uint32_t>> image_semaphores_for_fences;
315
uint32_t queue_family = 0;
316
uint32_t queue_index = 0;
317
};
318
319
public:
320
virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override final;
321
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;
322
virtual void command_queue_free(CommandQueueID p_cmd_queue) override final;
323
324
private:
325
// ----- POOL -----
326
327
struct CommandPool {
328
VkCommandPool vk_command_pool = VK_NULL_HANDLE;
329
CommandBufferType buffer_type = COMMAND_BUFFER_TYPE_PRIMARY;
330
LocalVector<CommandBufferInfo *> command_buffers_created;
331
};
332
333
public:
334
virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
335
virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override final;
336
virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
337
338
private:
339
// ----- BUFFER -----
340
341
struct CommandBufferInfo {
342
VkCommandBuffer vk_command_buffer = VK_NULL_HANDLE;
343
Framebuffer *active_framebuffer = nullptr;
344
RenderPassInfo *active_render_pass = nullptr;
345
};
346
347
public:
348
virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
349
virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
350
virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
351
virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
352
virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
353
354
/********************/
355
/**** SWAP CHAIN ****/
356
/********************/
357
358
private:
359
struct SwapChain {
360
VkSwapchainKHR vk_swapchain = VK_NULL_HANDLE;
361
RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
362
VkFormat format = VK_FORMAT_UNDEFINED;
363
VkColorSpaceKHR color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
364
TightLocalVector<VkImage> images;
365
TightLocalVector<VkImageView> image_views;
366
TightLocalVector<VkSemaphore> present_semaphores;
367
TightLocalVector<FramebufferID> framebuffers;
368
LocalVector<CommandQueue *> command_queues_acquired;
369
LocalVector<uint32_t> command_queues_acquired_semaphores;
370
RenderPassID render_pass;
371
int pre_transform_rotation_degrees = 0;
372
uint32_t image_index = 0;
373
#ifdef ANDROID_ENABLED
374
uint64_t refresh_duration = 0;
375
#endif
376
};
377
378
void _swap_chain_release(SwapChain *p_swap_chain);
379
380
public:
381
virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override final;
382
virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override final;
383
virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override final;
384
virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override final;
385
virtual int swap_chain_get_pre_rotation_degrees(SwapChainID p_swap_chain) override final;
386
virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override final;
387
virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) override final;
388
virtual void swap_chain_free(SwapChainID p_swap_chain) override final;
389
390
private:
391
/*********************/
392
/**** FRAMEBUFFER ****/
393
/*********************/
394
395
struct Framebuffer {
396
VkFramebuffer vk_framebuffer = VK_NULL_HANDLE;
397
398
// Only filled in if the framebuffer uses a fragment density map with offsets. Unused otherwise.
399
uint32_t fragment_density_map_offsets_layers = 0;
400
401
// Only filled in by a framebuffer created by a swap chain. Unused otherwise.
402
VkImage swap_chain_image = VK_NULL_HANDLE;
403
VkImageSubresourceRange swap_chain_image_subresource_range = {};
404
bool swap_chain_acquired = false;
405
};
406
407
public:
408
virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
409
virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
410
411
/****************/
412
/**** SHADER ****/
413
/****************/
414
private:
415
struct ShaderInfo {
416
VkShaderStageFlags vk_push_constant_stages = 0;
417
TightLocalVector<VkPipelineShaderStageCreateInfo> vk_stages_create_info;
418
TightLocalVector<VkDescriptorSetLayout> vk_descriptor_set_layouts;
419
VkPipelineLayout vk_pipeline_layout = VK_NULL_HANDLE;
420
};
421
422
public:
423
virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) override final;
424
virtual void shader_free(ShaderID p_shader) override final;
425
426
virtual void shader_destroy_modules(ShaderID p_shader) override final;
427
/*********************/
428
/**** UNIFORM SET ****/
429
/*********************/
430
431
// Descriptor sets require allocation from a pool.
432
// The documentation on how to use pools properly
433
// is scarce, and the documentation is strange.
434
//
435
// Basically, you can mix and match pools as you
436
// like, but you'll run into fragmentation issues.
437
// Because of this, the recommended approach is to
438
// create a pool for every descriptor set type, as
439
// this prevents fragmentation.
440
//
441
// This is implemented here as a having a list of
442
// pools (each can contain up to 64 sets) for each
443
// set layout. The amount of sets for each type
444
// is used as the key.
445
446
private:
447
static const uint32_t MAX_UNIFORM_POOL_ELEMENT = 65535;
448
449
struct DescriptorSetPoolKey {
450
uint16_t uniform_type[UNIFORM_TYPE_MAX] = {};
451
452
bool operator<(const DescriptorSetPoolKey &p_other) const {
453
return memcmp(uniform_type, p_other.uniform_type, sizeof(uniform_type)) < 0;
454
}
455
};
456
457
using DescriptorSetPools = RBMap<DescriptorSetPoolKey, HashMap<VkDescriptorPool, uint32_t>>;
458
DescriptorSetPools descriptor_set_pools;
459
uint32_t max_descriptor_sets_per_pool = 0;
460
461
HashMap<int, DescriptorSetPools> linear_descriptor_set_pools;
462
bool linear_descriptor_pools_enabled = true;
463
VkDescriptorPool _descriptor_set_pool_find_or_create(const DescriptorSetPoolKey &p_key, DescriptorSetPools::Iterator *r_pool_sets_it, int p_linear_pool_index);
464
void _descriptor_set_pool_unreference(DescriptorSetPools::Iterator p_pool_sets_it, VkDescriptorPool p_vk_descriptor_pool, int p_linear_pool_index);
465
466
// Global flag to toggle usage of immutable sampler when creating pipeline layouts.
467
// It cannot change after creating the PSOs, since we need to skipping samplers when creating uniform sets.
468
bool immutable_samplers_enabled = true;
469
470
struct UniformSetInfo {
471
VkDescriptorSet vk_descriptor_set = VK_NULL_HANDLE;
472
VkDescriptorPool vk_descriptor_pool = VK_NULL_HANDLE;
473
VkDescriptorPool vk_linear_descriptor_pool = VK_NULL_HANDLE;
474
DescriptorSetPools::Iterator pool_sets_it;
475
};
476
477
public:
478
virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;
479
virtual void linear_uniform_set_pools_reset(int p_linear_pool_index) override final;
480
virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
481
virtual bool uniform_sets_have_linear_pools() const override final;
482
483
// ----- COMMANDS -----
484
485
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;
486
487
/******************/
488
/**** TRANSFER ****/
489
/******************/
490
491
virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
492
virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
493
494
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;
495
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;
496
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;
497
498
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;
499
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;
500
501
/******************/
502
/**** PIPELINE ****/
503
/******************/
504
private:
505
struct PipelineCacheHeader {
506
uint32_t magic = 0;
507
uint32_t data_size = 0;
508
uint64_t data_hash = 0;
509
uint32_t vendor_id = 0;
510
uint32_t device_id = 0;
511
uint32_t driver_version = 0;
512
uint8_t uuid[VK_UUID_SIZE] = {};
513
uint8_t driver_abi = 0;
514
};
515
516
struct PipelineCache {
517
String file_path;
518
size_t current_size = 0;
519
Vector<uint8_t> buffer; // Header then data.
520
VkPipelineCache vk_cache = VK_NULL_HANDLE;
521
};
522
523
static int caching_instance_count;
524
PipelineCache pipelines_cache;
525
String pipeline_cache_id;
526
HashMap<uint64_t, bool> has_comp_alpha;
527
528
public:
529
virtual void pipeline_free(PipelineID p_pipeline) override final;
530
531
// ----- BINDING -----
532
533
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;
534
535
// ----- CACHE -----
536
537
virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
538
virtual void pipeline_cache_free() override final;
539
virtual size_t pipeline_cache_query_size() override final;
540
virtual Vector<uint8_t> pipeline_cache_serialize() override final;
541
542
/*******************/
543
/**** RENDERING ****/
544
/*******************/
545
546
private:
547
// ----- SUBPASS -----
548
549
struct RenderPassInfo {
550
VkRenderPass vk_render_pass = VK_NULL_HANDLE;
551
bool uses_fragment_density_map_offsets = false;
552
};
553
554
public:
555
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;
556
virtual void render_pass_free(RenderPassID p_render_pass) override final;
557
558
// ----- COMMANDS -----
559
560
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;
561
virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
562
virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
563
virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
564
virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
565
virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
566
567
// Binding.
568
virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
569
virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
570
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) override final;
571
572
// Drawing.
573
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;
574
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;
575
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;
576
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;
577
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;
578
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;
579
580
// Buffer binding.
581
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) override final;
582
virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
583
584
// Dynamic state.
585
virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
586
virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
587
588
// ----- PIPELINE -----
589
590
virtual PipelineID render_pipeline_create(
591
ShaderID p_shader,
592
VertexFormatID p_vertex_format,
593
RenderPrimitive p_render_primitive,
594
PipelineRasterizationState p_rasterization_state,
595
PipelineMultisampleState p_multisample_state,
596
PipelineDepthStencilState p_depth_stencil_state,
597
PipelineColorBlendState p_blend_state,
598
VectorView<int32_t> p_color_attachments,
599
BitField<PipelineDynamicStateFlags> p_dynamic_state,
600
RenderPassID p_render_pass,
601
uint32_t p_render_subpass,
602
VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
603
604
/*****************/
605
/**** COMPUTE ****/
606
/*****************/
607
608
// ----- COMMANDS -----
609
610
// Binding.
611
virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
612
virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
613
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) override final;
614
615
// Dispatching.
616
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;
617
virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
618
619
// ----- PIPELINE -----
620
621
virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
622
623
/*****************/
624
/**** QUERIES ****/
625
/*****************/
626
627
// ----- TIMESTAMP -----
628
629
// Basic.
630
virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
631
virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
632
virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
633
virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
634
635
// Commands.
636
virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
637
virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
638
639
/****************/
640
/**** LABELS ****/
641
/****************/
642
643
virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
644
virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
645
646
/****************/
647
/**** DEBUG *****/
648
/****************/
649
virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
650
void print_lost_device_info();
651
void on_device_lost() const;
652
static String get_vulkan_result(VkResult err);
653
654
/********************/
655
/**** SUBMISSION ****/
656
/********************/
657
658
virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
659
virtual void end_segment() override final;
660
661
/**************/
662
/**** MISC ****/
663
/**************/
664
665
virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
666
virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
667
virtual uint64_t get_total_memory_used() override final;
668
virtual uint64_t get_lazily_memory_used() override final;
669
virtual uint64_t limit_get(Limit p_limit) override final;
670
virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
671
virtual bool has_feature(Features p_feature) override final;
672
virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
673
virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() override final;
674
virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() override final;
675
virtual String get_api_name() const override final;
676
virtual String get_api_version() const override final;
677
virtual String get_pipeline_cache_uuid() const override final;
678
virtual const Capabilities &get_capabilities() const override final;
679
virtual const RenderingShaderContainerFormat &get_shader_container_format() const override final;
680
681
virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
682
683
private:
684
/*********************/
685
/**** BOOKKEEPING ****/
686
/*********************/
687
688
using VersatileResource = VersatileResourceTemplate<
689
BufferInfo,
690
TextureInfo,
691
VertexFormatInfo,
692
ShaderInfo,
693
UniformSetInfo,
694
RenderPassInfo,
695
CommandBufferInfo>;
696
PagedAllocator<VersatileResource, true> resources_allocator;
697
698
/******************/
699
700
public:
701
RenderingDeviceDriverVulkan(RenderingContextDriverVulkan *p_context_driver);
702
virtual ~RenderingDeviceDriverVulkan();
703
};
704
705
using VKC = RenderingContextDriverVulkan;
706
707