rendering_device_vulkan.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /**************************************************************************/
  2. /* rendering_device_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. #ifndef RENDERING_DEVICE_VULKAN_H
  31. #define RENDERING_DEVICE_VULKAN_H
  32. #include "core/object/worker_thread_pool.h"
  33. #include "core/os/thread_safe.h"
  34. #include "core/templates/local_vector.h"
  35. #include "core/templates/oa_hash_map.h"
  36. #include "core/templates/rid_owner.h"
  37. #include "servers/rendering/rendering_device.h"
  38. #ifdef DEBUG_ENABLED
  39. #ifndef _DEBUG
  40. #define _DEBUG
  41. #endif
  42. #endif
  43. #include "thirdparty/vulkan/vk_mem_alloc.h"
  44. #ifdef USE_VOLK
  45. #include <volk.h>
  46. #else
  47. #include <vulkan/vulkan.h>
  48. #endif
  49. class VulkanContext;
  50. class RenderingDeviceVulkan : public RenderingDevice {
  51. _THREAD_SAFE_CLASS_
  52. // Miscellaneous tables that map
  53. // our enums to enums used
  54. // by vulkan.
  55. VkPhysicalDeviceLimits limits;
  56. static const VkFormat vulkan_formats[DATA_FORMAT_MAX];
  57. static const char *named_formats[DATA_FORMAT_MAX];
  58. static const VkCompareOp compare_operators[COMPARE_OP_MAX];
  59. static const VkStencilOp stencil_operations[STENCIL_OP_MAX];
  60. static const VkSampleCountFlagBits rasterization_sample_count[TEXTURE_SAMPLES_MAX];
  61. static const VkLogicOp logic_operations[RenderingDevice::LOGIC_OP_MAX];
  62. static const VkBlendFactor blend_factors[RenderingDevice::BLEND_FACTOR_MAX];
  63. static const VkBlendOp blend_operations[RenderingDevice::BLEND_OP_MAX];
  64. static const VkSamplerAddressMode address_modes[SAMPLER_REPEAT_MODE_MAX];
  65. static const VkBorderColor sampler_border_colors[SAMPLER_BORDER_COLOR_MAX];
  66. static const VkImageType vulkan_image_type[TEXTURE_TYPE_MAX];
  67. // Functions used for format
  68. // validation, and ensures the
  69. // user passes valid data.
  70. static int get_format_vertex_size(DataFormat p_format);
  71. static uint32_t get_image_format_pixel_size(DataFormat p_format);
  72. static void get_compressed_image_format_block_dimensions(DataFormat p_format, uint32_t &r_w, uint32_t &r_h);
  73. uint32_t get_compressed_image_format_block_byte_size(DataFormat p_format);
  74. static uint32_t get_compressed_image_format_pixel_rshift(DataFormat p_format);
  75. static uint32_t get_image_format_required_size(DataFormat p_format, uint32_t p_width, uint32_t p_height, uint32_t p_depth, uint32_t p_mipmaps, uint32_t *r_blockw = nullptr, uint32_t *r_blockh = nullptr, uint32_t *r_depth = nullptr);
  76. static uint32_t get_image_required_mipmaps(uint32_t p_width, uint32_t p_height, uint32_t p_depth);
  77. static bool format_has_stencil(DataFormat p_format);
  78. /***************************/
  79. /**** ID INFRASTRUCTURE ****/
  80. /***************************/
  81. enum IDType {
  82. ID_TYPE_FRAMEBUFFER_FORMAT,
  83. ID_TYPE_VERTEX_FORMAT,
  84. ID_TYPE_DRAW_LIST,
  85. ID_TYPE_SPLIT_DRAW_LIST,
  86. ID_TYPE_COMPUTE_LIST,
  87. ID_TYPE_MAX,
  88. ID_BASE_SHIFT = 58 // 5 bits for ID types.
  89. };
  90. VkDevice device = VK_NULL_HANDLE;
  91. HashMap<RID, HashSet<RID>> dependency_map; // IDs to IDs that depend on it.
  92. HashMap<RID, HashSet<RID>> reverse_dependency_map; // Same as above, but in reverse.
  93. void _add_dependency(RID p_id, RID p_depends_on);
  94. void _free_dependencies(RID p_id);
  95. /*****************/
  96. /**** TEXTURE ****/
  97. /*****************/
  98. // In Vulkan, the concept of textures does not exist,
  99. // instead there is the image (the memory pretty much,
  100. // the view (how the memory is interpreted) and the
  101. // sampler (how it's sampled from the shader).
  102. //
  103. // Texture here includes the first two stages, but
  104. // It's possible to create textures sharing the image
  105. // but with different views. The main use case for this
  106. // is textures that can be read as both SRGB/Linear,
  107. // or slices of a texture (a mipmap, a layer, a 3D slice)
  108. // for a framebuffer to render into it.
  109. struct Texture {
  110. VkImage image = VK_NULL_HANDLE;
  111. VmaAllocation allocation = nullptr;
  112. VmaAllocationInfo allocation_info;
  113. VkImageView view = VK_NULL_HANDLE;
  114. TextureType type;
  115. DataFormat format;
  116. TextureSamples samples;
  117. uint32_t width = 0;
  118. uint32_t height = 0;
  119. uint32_t depth = 0;
  120. uint32_t layers = 0;
  121. uint32_t mipmaps = 0;
  122. uint32_t usage_flags = 0;
  123. uint32_t base_mipmap = 0;
  124. uint32_t base_layer = 0;
  125. Vector<DataFormat> allowed_shared_formats;
  126. VkImageLayout layout;
  127. uint64_t used_in_frame = 0;
  128. bool used_in_transfer = false;
  129. bool used_in_raster = false;
  130. bool used_in_compute = false;
  131. bool is_resolve_buffer = false;
  132. uint32_t read_aspect_mask = 0;
  133. uint32_t barrier_aspect_mask = 0;
  134. bool bound = false; // Bound to framebffer.
  135. RID owner;
  136. };
  137. RID_Owner<Texture> texture_owner;
  138. uint32_t texture_upload_region_size_px = 0;
  139. Vector<uint8_t> _texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer, bool p_2d = false);
  140. Error _texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier, bool p_use_setup_queue);
  141. /*****************/
  142. /**** SAMPLER ****/
  143. /*****************/
  144. RID_Owner<VkSampler> sampler_owner;
  145. /***************************/
  146. /**** BUFFER MANAGEMENT ****/
  147. /***************************/
  148. // These are temporary buffers on CPU memory that hold
  149. // the information until the CPU fetches it and places it
  150. // either on GPU buffers, or images (textures). It ensures
  151. // updates are properly synchronized with whatever the
  152. // GPU is doing.
  153. //
  154. // The logic here is as follows, only 3 of these
  155. // blocks are created at the beginning (one per frame)
  156. // they can each belong to a frame (assigned to current when
  157. // used) and they can only be reused after the same frame is
  158. // recycled.
  159. //
  160. // When CPU requires to allocate more than what is available,
  161. // more of these buffers are created. If a limit is reached,
  162. // then a fence will ensure will wait for blocks allocated
  163. // in previous frames are processed. If that fails, then
  164. // another fence will ensure everything pending for the current
  165. // frame is processed (effectively stalling).
  166. //
  167. // See the comments in the code to understand better how it works.
  168. struct StagingBufferBlock {
  169. VkBuffer buffer = VK_NULL_HANDLE;
  170. VmaAllocation allocation = nullptr;
  171. uint64_t frame_used = 0;
  172. uint32_t fill_amount = 0;
  173. };
  174. Vector<StagingBufferBlock> staging_buffer_blocks;
  175. int staging_buffer_current = 0;
  176. uint32_t staging_buffer_block_size = 0;
  177. uint64_t staging_buffer_max_size = 0;
  178. bool staging_buffer_used = false;
  179. Error _staging_buffer_allocate(uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, bool p_can_segment = true);
  180. Error _insert_staging_block();
  181. struct Buffer {
  182. uint32_t size = 0;
  183. uint32_t usage = 0;
  184. VkBuffer buffer = VK_NULL_HANDLE;
  185. VmaAllocation allocation = nullptr;
  186. VkDescriptorBufferInfo buffer_info; // Used for binding.
  187. Buffer() {
  188. }
  189. };
  190. Error _buffer_allocate(Buffer *p_buffer, uint32_t p_size, uint32_t p_usage, VmaMemoryUsage p_mem_usage, VmaAllocationCreateFlags p_mem_flags);
  191. Error _buffer_free(Buffer *p_buffer);
  192. Error _buffer_update(Buffer *p_buffer, size_t p_offset, const uint8_t *p_data, size_t p_data_size, bool p_use_draw_command_buffer = false, uint32_t p_required_align = 32);
  193. void _full_barrier(bool p_sync_with_draw);
  194. void _memory_barrier(VkPipelineStageFlags p_src_stage_mask, VkPipelineStageFlags p_dst_stage_mask, VkAccessFlags p_src_access, VkAccessFlags p_dst_access, bool p_sync_with_draw);
  195. void _buffer_memory_barrier(VkBuffer buffer, uint64_t p_from, uint64_t p_size, VkPipelineStageFlags p_src_stage_mask, VkPipelineStageFlags p_dst_stage_mask, VkAccessFlags p_src_access, VkAccessFlags p_dst_access, bool p_sync_with_draw);
  196. /*********************/
  197. /**** FRAMEBUFFER ****/
  198. /*********************/
  199. // In Vulkan, framebuffers work similar to how they
  200. // do in OpenGL, with the exception that
  201. // the "format" (vkRenderPass) is not dynamic
  202. // and must be more or less the same as the one
  203. // used for the render pipelines.
  204. struct FramebufferFormatKey {
  205. Vector<AttachmentFormat> attachments;
  206. Vector<FramebufferPass> passes;
  207. uint32_t view_count = 1;
  208. bool operator<(const FramebufferFormatKey &p_key) const {
  209. if (view_count != p_key.view_count) {
  210. return view_count < p_key.view_count;
  211. }
  212. uint32_t pass_size = passes.size();
  213. uint32_t key_pass_size = p_key.passes.size();
  214. if (pass_size != key_pass_size) {
  215. return pass_size < key_pass_size;
  216. }
  217. const FramebufferPass *pass_ptr = passes.ptr();
  218. const FramebufferPass *key_pass_ptr = p_key.passes.ptr();
  219. for (uint32_t i = 0; i < pass_size; i++) {
  220. { // Compare color attachments.
  221. uint32_t attachment_size = pass_ptr[i].color_attachments.size();
  222. uint32_t key_attachment_size = key_pass_ptr[i].color_attachments.size();
  223. if (attachment_size != key_attachment_size) {
  224. return attachment_size < key_attachment_size;
  225. }
  226. const int32_t *pass_attachment_ptr = pass_ptr[i].color_attachments.ptr();
  227. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].color_attachments.ptr();
  228. for (uint32_t j = 0; j < attachment_size; j++) {
  229. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  230. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  231. }
  232. }
  233. }
  234. { // Compare input attachments.
  235. uint32_t attachment_size = pass_ptr[i].input_attachments.size();
  236. uint32_t key_attachment_size = key_pass_ptr[i].input_attachments.size();
  237. if (attachment_size != key_attachment_size) {
  238. return attachment_size < key_attachment_size;
  239. }
  240. const int32_t *pass_attachment_ptr = pass_ptr[i].input_attachments.ptr();
  241. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].input_attachments.ptr();
  242. for (uint32_t j = 0; j < attachment_size; j++) {
  243. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  244. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  245. }
  246. }
  247. }
  248. { // Compare resolve attachments.
  249. uint32_t attachment_size = pass_ptr[i].resolve_attachments.size();
  250. uint32_t key_attachment_size = key_pass_ptr[i].resolve_attachments.size();
  251. if (attachment_size != key_attachment_size) {
  252. return attachment_size < key_attachment_size;
  253. }
  254. const int32_t *pass_attachment_ptr = pass_ptr[i].resolve_attachments.ptr();
  255. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].resolve_attachments.ptr();
  256. for (uint32_t j = 0; j < attachment_size; j++) {
  257. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  258. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  259. }
  260. }
  261. }
  262. { // Compare preserve attachments.
  263. uint32_t attachment_size = pass_ptr[i].preserve_attachments.size();
  264. uint32_t key_attachment_size = key_pass_ptr[i].preserve_attachments.size();
  265. if (attachment_size != key_attachment_size) {
  266. return attachment_size < key_attachment_size;
  267. }
  268. const int32_t *pass_attachment_ptr = pass_ptr[i].preserve_attachments.ptr();
  269. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].preserve_attachments.ptr();
  270. for (uint32_t j = 0; j < attachment_size; j++) {
  271. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  272. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  273. }
  274. }
  275. }
  276. if (pass_ptr[i].depth_attachment != key_pass_ptr[i].depth_attachment) {
  277. return pass_ptr[i].depth_attachment < key_pass_ptr[i].depth_attachment;
  278. }
  279. }
  280. int as = attachments.size();
  281. int bs = p_key.attachments.size();
  282. if (as != bs) {
  283. return as < bs;
  284. }
  285. const AttachmentFormat *af_a = attachments.ptr();
  286. const AttachmentFormat *af_b = p_key.attachments.ptr();
  287. for (int i = 0; i < as; i++) {
  288. const AttachmentFormat &a = af_a[i];
  289. const AttachmentFormat &b = af_b[i];
  290. if (a.format != b.format) {
  291. return a.format < b.format;
  292. }
  293. if (a.samples != b.samples) {
  294. return a.samples < b.samples;
  295. }
  296. if (a.usage_flags != b.usage_flags) {
  297. return a.usage_flags < b.usage_flags;
  298. }
  299. }
  300. return false; // Equal.
  301. }
  302. };
  303. VkRenderPass _render_pass_create(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, uint32_t p_view_count = 1, Vector<TextureSamples> *r_samples = nullptr);
  304. // This is a cache and it's never freed, it ensures
  305. // IDs for a given format are always unique.
  306. RBMap<FramebufferFormatKey, FramebufferFormatID> framebuffer_format_cache;
  307. struct FramebufferFormat {
  308. const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E;
  309. VkRenderPass render_pass = VK_NULL_HANDLE; // Here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec).
  310. Vector<TextureSamples> pass_samples;
  311. uint32_t view_count = 1; // Number of views.
  312. };
  313. HashMap<FramebufferFormatID, FramebufferFormat> framebuffer_formats;
  314. struct Framebuffer {
  315. FramebufferFormatID format_id = 0;
  316. struct VersionKey {
  317. InitialAction initial_color_action;
  318. FinalAction final_color_action;
  319. InitialAction initial_depth_action;
  320. FinalAction final_depth_action;
  321. uint32_t view_count;
  322. bool operator<(const VersionKey &p_key) const {
  323. if (initial_color_action == p_key.initial_color_action) {
  324. if (final_color_action == p_key.final_color_action) {
  325. if (initial_depth_action == p_key.initial_depth_action) {
  326. if (final_depth_action == p_key.final_depth_action) {
  327. return view_count < p_key.view_count;
  328. } else {
  329. return final_depth_action < p_key.final_depth_action;
  330. }
  331. } else {
  332. return initial_depth_action < p_key.initial_depth_action;
  333. }
  334. } else {
  335. return final_color_action < p_key.final_color_action;
  336. }
  337. } else {
  338. return initial_color_action < p_key.initial_color_action;
  339. }
  340. }
  341. };
  342. uint32_t storage_mask = 0;
  343. Vector<RID> texture_ids;
  344. InvalidationCallback invalidated_callback = nullptr;
  345. void *invalidated_callback_userdata = nullptr;
  346. struct Version {
  347. VkFramebuffer framebuffer = VK_NULL_HANDLE;
  348. VkRenderPass render_pass = VK_NULL_HANDLE; // This one is owned.
  349. uint32_t subpass_count = 1;
  350. };
  351. RBMap<VersionKey, Version> framebuffers;
  352. Size2 size;
  353. uint32_t view_count;
  354. };
  355. RID_Owner<Framebuffer> framebuffer_owner;
  356. /***********************/
  357. /**** VERTEX BUFFER ****/
  358. /***********************/
  359. // Vertex buffers in Vulkan are similar to how
  360. // they work in OpenGL, except that instead of
  361. // an attribute index, there is a buffer binding
  362. // index (for binding the buffers in real-time)
  363. // and a location index (what is used in the shader).
  364. //
  365. // This mapping is done here internally, and it's not
  366. // exposed.
  367. RID_Owner<Buffer> vertex_buffer_owner;
  368. struct VertexDescriptionKey {
  369. Vector<VertexAttribute> vertex_formats;
  370. bool operator==(const VertexDescriptionKey &p_key) const {
  371. int vdc = vertex_formats.size();
  372. int vdck = p_key.vertex_formats.size();
  373. if (vdc != vdck) {
  374. return false;
  375. } else {
  376. const VertexAttribute *a_ptr = vertex_formats.ptr();
  377. const VertexAttribute *b_ptr = p_key.vertex_formats.ptr();
  378. for (int i = 0; i < vdc; i++) {
  379. const VertexAttribute &a = a_ptr[i];
  380. const VertexAttribute &b = b_ptr[i];
  381. if (a.location != b.location) {
  382. return false;
  383. }
  384. if (a.offset != b.offset) {
  385. return false;
  386. }
  387. if (a.format != b.format) {
  388. return false;
  389. }
  390. if (a.stride != b.stride) {
  391. return false;
  392. }
  393. if (a.frequency != b.frequency) {
  394. return false;
  395. }
  396. }
  397. return true; // They are equal.
  398. }
  399. }
  400. uint32_t hash() const {
  401. int vdc = vertex_formats.size();
  402. uint32_t h = hash_murmur3_one_32(vdc);
  403. const VertexAttribute *ptr = vertex_formats.ptr();
  404. for (int i = 0; i < vdc; i++) {
  405. const VertexAttribute &vd = ptr[i];
  406. h = hash_murmur3_one_32(vd.location, h);
  407. h = hash_murmur3_one_32(vd.offset, h);
  408. h = hash_murmur3_one_32(vd.format, h);
  409. h = hash_murmur3_one_32(vd.stride, h);
  410. h = hash_murmur3_one_32(vd.frequency, h);
  411. }
  412. return hash_fmix32(h);
  413. }
  414. };
  415. struct VertexDescriptionHash {
  416. static _FORCE_INLINE_ uint32_t hash(const VertexDescriptionKey &p_key) {
  417. return p_key.hash();
  418. }
  419. };
  420. // This is a cache and it's never freed, it ensures that
  421. // ID used for a specific format always remain the same.
  422. HashMap<VertexDescriptionKey, VertexFormatID, VertexDescriptionHash> vertex_format_cache;
  423. struct VertexDescriptionCache {
  424. Vector<VertexAttribute> vertex_formats;
  425. VkVertexInputBindingDescription *bindings = nullptr;
  426. VkVertexInputAttributeDescription *attributes = nullptr;
  427. VkPipelineVertexInputStateCreateInfo create_info;
  428. };
  429. HashMap<VertexFormatID, VertexDescriptionCache> vertex_formats;
  430. struct VertexArray {
  431. RID buffer;
  432. VertexFormatID description = 0;
  433. int vertex_count = 0;
  434. uint32_t max_instances_allowed = 0;
  435. Vector<VkBuffer> buffers; // Not owned, just referenced.
  436. Vector<VkDeviceSize> offsets;
  437. };
  438. RID_Owner<VertexArray> vertex_array_owner;
  439. struct IndexBuffer : public Buffer {
  440. uint32_t max_index = 0; // Used for validation.
  441. uint32_t index_count = 0;
  442. VkIndexType index_type = VK_INDEX_TYPE_NONE_NV;
  443. bool supports_restart_indices = false;
  444. };
  445. RID_Owner<IndexBuffer> index_buffer_owner;
  446. struct IndexArray {
  447. uint32_t max_index = 0; // Remember the maximum index here too, for validation.
  448. VkBuffer buffer; // Not owned, inherited from index buffer.
  449. uint32_t offset = 0;
  450. uint32_t indices = 0;
  451. VkIndexType index_type = VK_INDEX_TYPE_NONE_NV;
  452. bool supports_restart_indices = false;
  453. };
  454. RID_Owner<IndexArray> index_array_owner;
  455. /****************/
  456. /**** SHADER ****/
  457. /****************/
  458. // Vulkan specifies a really complex behavior for the application
  459. // in order to tell when descriptor sets need to be re-bound (or not).
  460. // "When binding a descriptor set (see Descriptor Set Binding) to set
  461. // number N, if the previously bound descriptor sets for sets zero
  462. // through N-1 were all bound using compatible pipeline layouts,
  463. // then performing this binding does not disturb any of the lower numbered sets.
  464. // If, additionally, the previous bound descriptor set for set N was
  465. // bound using a pipeline layout compatible for set N, then the bindings
  466. // in sets numbered greater than N are also not disturbed."
  467. // As a result, we need to figure out quickly when something is no longer "compatible".
  468. // in order to avoid costly rebinds.
  469. struct UniformInfo {
  470. UniformType type = UniformType::UNIFORM_TYPE_MAX;
  471. bool writable = false;
  472. int binding = 0;
  473. uint32_t stages = 0;
  474. int length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
  475. bool operator!=(const UniformInfo &p_info) const {
  476. return (binding != p_info.binding || type != p_info.type || writable != p_info.writable || stages != p_info.stages || length != p_info.length);
  477. }
  478. bool operator<(const UniformInfo &p_info) const {
  479. if (binding != p_info.binding) {
  480. return binding < p_info.binding;
  481. }
  482. if (type != p_info.type) {
  483. return type < p_info.type;
  484. }
  485. if (writable != p_info.writable) {
  486. return writable < p_info.writable;
  487. }
  488. if (stages != p_info.stages) {
  489. return stages < p_info.stages;
  490. }
  491. return length < p_info.length;
  492. }
  493. };
  494. struct UniformSetFormat {
  495. Vector<UniformInfo> uniform_info;
  496. bool operator<(const UniformSetFormat &p_format) const {
  497. uint32_t size = uniform_info.size();
  498. uint32_t psize = p_format.uniform_info.size();
  499. if (size != psize) {
  500. return size < psize;
  501. }
  502. const UniformInfo *infoptr = uniform_info.ptr();
  503. const UniformInfo *pinfoptr = p_format.uniform_info.ptr();
  504. for (uint32_t i = 0; i < size; i++) {
  505. if (infoptr[i] != pinfoptr[i]) {
  506. return infoptr[i] < pinfoptr[i];
  507. }
  508. }
  509. return false;
  510. }
  511. };
  512. // Always grows, never shrinks, ensuring unique IDs, but we assume
  513. // the amount of formats will never be a problem, as the amount of shaders
  514. // in a game is limited.
  515. RBMap<UniformSetFormat, uint32_t> uniform_set_format_cache;
  516. // Shaders in Vulkan are just pretty much
  517. // precompiled blocks of SPIR-V bytecode. They
  518. // are most likely not really compiled to host
  519. // assembly until a pipeline is created.
  520. //
  521. // When supplying the shaders, this implementation
  522. // will use the reflection abilities of glslang to
  523. // understand and cache everything required to
  524. // create and use the descriptor sets (Vulkan's
  525. // biggest pain).
  526. //
  527. // Additionally, hashes are created for every set
  528. // to do quick validation and ensuring the user
  529. // does not submit something invalid.
  530. struct Shader {
  531. struct Set {
  532. Vector<UniformInfo> uniform_info;
  533. VkDescriptorSetLayout descriptor_set_layout = VK_NULL_HANDLE;
  534. };
  535. uint64_t vertex_input_mask = 0; // Inputs used, this is mostly for validation.
  536. uint32_t fragment_output_mask = 0;
  537. struct PushConstant {
  538. uint32_t size = 0;
  539. uint32_t vk_stages_mask = 0;
  540. };
  541. PushConstant push_constant;
  542. uint32_t compute_local_size[3] = { 0, 0, 0 };
  543. struct SpecializationConstant {
  544. PipelineSpecializationConstant constant;
  545. uint32_t stage_flags = 0;
  546. };
  547. bool is_compute = false;
  548. Vector<Set> sets;
  549. Vector<uint32_t> set_formats;
  550. Vector<VkPipelineShaderStageCreateInfo> pipeline_stages;
  551. Vector<SpecializationConstant> specialization_constants;
  552. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  553. String name; // Used for debug.
  554. };
  555. String _shader_uniform_debug(RID p_shader, int p_set = -1);
  556. RID_Owner<Shader> shader_owner;
  557. /******************/
  558. /**** UNIFORMS ****/
  559. /******************/
  560. // Descriptor sets require allocation from a pool.
  561. // The documentation on how to use pools properly
  562. // is scarce, and the documentation is strange.
  563. //
  564. // Basically, you can mix and match pools as you
  565. // like, but you'll run into fragmentation issues.
  566. // Because of this, the recommended approach is to
  567. // create a pool for every descriptor set type, as
  568. // this prevents fragmentation.
  569. //
  570. // This is implemented here as a having a list of
  571. // pools (each can contain up to 64 sets) for each
  572. // set layout. The amount of sets for each type
  573. // is used as the key.
  574. enum {
  575. MAX_DESCRIPTOR_POOL_ELEMENT = 65535
  576. };
  577. struct DescriptorPoolKey {
  578. union {
  579. struct {
  580. uint16_t uniform_type[UNIFORM_TYPE_MAX]; // Using 16 bits because, for sending arrays, each element is a pool set.
  581. };
  582. struct {
  583. uint64_t key1;
  584. uint64_t key2;
  585. uint64_t key3;
  586. };
  587. };
  588. bool operator<(const DescriptorPoolKey &p_key) const {
  589. if (key1 != p_key.key1) {
  590. return key1 < p_key.key1;
  591. }
  592. if (key2 != p_key.key2) {
  593. return key2 < p_key.key2;
  594. }
  595. return key3 < p_key.key3;
  596. }
  597. DescriptorPoolKey() {
  598. key1 = 0;
  599. key2 = 0;
  600. key3 = 0;
  601. }
  602. };
  603. struct DescriptorPool {
  604. VkDescriptorPool pool;
  605. uint32_t usage;
  606. };
  607. RBMap<DescriptorPoolKey, HashSet<DescriptorPool *>> descriptor_pools;
  608. uint32_t max_descriptors_per_pool = 0;
  609. DescriptorPool *_descriptor_pool_allocate(const DescriptorPoolKey &p_key);
  610. void _descriptor_pool_free(const DescriptorPoolKey &p_key, DescriptorPool *p_pool);
  611. RID_Owner<Buffer> uniform_buffer_owner;
  612. RID_Owner<Buffer> storage_buffer_owner;
  613. // Texture buffer needs a view.
  614. struct TextureBuffer {
  615. Buffer buffer;
  616. VkBufferView view = VK_NULL_HANDLE;
  617. };
  618. RID_Owner<TextureBuffer> texture_buffer_owner;
  619. // This structure contains the descriptor set. They _need_ to be allocated
  620. // for a shader (and will be erased when this shader is erased), but should
  621. // work for other shaders as long as the hash matches. This covers using
  622. // them in shader variants.
  623. //
  624. // Keep also in mind that you can share buffers between descriptor sets, so
  625. // the above restriction is not too serious.
  626. struct UniformSet {
  627. uint32_t format = 0;
  628. RID shader_id;
  629. uint32_t shader_set = 0;
  630. DescriptorPool *pool = nullptr;
  631. DescriptorPoolKey pool_key;
  632. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  633. //VkPipelineLayout pipeline_layout; // Not owned, inherited from shader.
  634. struct AttachableTexture {
  635. uint32_t bind;
  636. RID texture;
  637. };
  638. LocalVector<AttachableTexture> attachable_textures; // Used for validation.
  639. Vector<Texture *> mutable_sampled_textures; // Used for layout change.
  640. Vector<Texture *> mutable_storage_textures; // Used for layout change.
  641. InvalidationCallback invalidated_callback = nullptr;
  642. void *invalidated_callback_userdata = nullptr;
  643. };
  644. RID_Owner<UniformSet> uniform_set_owner;
  645. /*******************/
  646. /**** PIPELINES ****/
  647. /*******************/
  648. // Render pipeline contains ALL the
  649. // information required for drawing.
  650. // This includes all the rasterizer state
  651. // as well as shader used, framebuffer format,
  652. // etc.
  653. // While the pipeline is just a single object
  654. // (VkPipeline) a lot of values are also saved
  655. // here to do validation (vulkan does none by
  656. // default) and warn the user if something
  657. // was not supplied as intended.
  658. struct RenderPipeline {
  659. // Cached values for validation.
  660. #ifdef DEBUG_ENABLED
  661. struct Validation {
  662. FramebufferFormatID framebuffer_format = 0;
  663. uint32_t render_pass = 0;
  664. uint32_t dynamic_state = 0;
  665. VertexFormatID vertex_format = 0;
  666. bool uses_restart_indices = false;
  667. uint32_t primitive_minimum = 0;
  668. uint32_t primitive_divisor = 0;
  669. } validation;
  670. #endif
  671. // Actual pipeline.
  672. RID shader;
  673. Vector<uint32_t> set_formats;
  674. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // Not owned, needed for push constants.
  675. VkPipeline pipeline = VK_NULL_HANDLE;
  676. uint32_t push_constant_size = 0;
  677. uint32_t push_constant_stages_mask = 0;
  678. };
  679. RID_Owner<RenderPipeline> render_pipeline_owner;
  680. struct PipelineCacheHeader {
  681. uint32_t magic;
  682. uint32_t data_size;
  683. uint64_t data_hash;
  684. uint32_t vendor_id;
  685. uint32_t device_id;
  686. uint32_t driver_version;
  687. uint8_t uuid[VK_UUID_SIZE];
  688. uint8_t driver_abi;
  689. };
  690. struct PipelineCache {
  691. String file_path;
  692. PipelineCacheHeader header = {};
  693. size_t current_size = 0;
  694. LocalVector<uint8_t> buffer;
  695. VkPipelineCache cache_object = VK_NULL_HANDLE;
  696. };
  697. PipelineCache pipelines_cache;
  698. WorkerThreadPool::TaskID pipelines_cache_save_task = WorkerThreadPool::INVALID_TASK_ID;
  699. void _load_pipeline_cache();
  700. void _update_pipeline_cache(bool p_closing = false);
  701. static void _save_pipeline_cache(void *p_data);
  702. struct ComputePipeline {
  703. RID shader;
  704. Vector<uint32_t> set_formats;
  705. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // Not owned, needed for push constants.
  706. VkPipeline pipeline = VK_NULL_HANDLE;
  707. uint32_t push_constant_size = 0;
  708. uint32_t push_constant_stages_mask = 0;
  709. uint32_t local_group_size[3] = { 0, 0, 0 };
  710. };
  711. RID_Owner<ComputePipeline> compute_pipeline_owner;
  712. /*******************/
  713. /**** DRAW LIST ****/
  714. /*******************/
  715. // Draw list contains both the command buffer
  716. // used for drawing as well as a LOT of
  717. // information used for validation. This
  718. // validation is cheap so most of it can
  719. // also run in release builds.
  720. // When using split command lists, this is
  721. // implemented internally using secondary command
  722. // buffers. As they can be created in threads,
  723. // each needs its own command pool.
  724. struct SplitDrawListAllocator {
  725. VkCommandPool command_pool = VK_NULL_HANDLE;
  726. Vector<VkCommandBuffer> command_buffers; // One for each frame.
  727. };
  728. Vector<SplitDrawListAllocator> split_draw_list_allocators;
  729. struct DrawList {
  730. VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
  731. Rect2i viewport;
  732. bool viewport_set = false;
  733. struct SetState {
  734. uint32_t pipeline_expected_format = 0;
  735. uint32_t uniform_set_format = 0;
  736. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  737. RID uniform_set;
  738. bool bound = false;
  739. };
  740. struct State {
  741. SetState sets[MAX_UNIFORM_SETS];
  742. uint32_t set_count = 0;
  743. RID pipeline;
  744. RID pipeline_shader;
  745. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  746. RID vertex_array;
  747. RID index_array;
  748. uint32_t pipeline_push_constant_stages = 0;
  749. } state;
  750. #ifdef DEBUG_ENABLED
  751. struct Validation {
  752. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  753. // Actual render pass values.
  754. uint32_t dynamic_state = 0;
  755. VertexFormatID vertex_format = INVALID_ID;
  756. uint32_t vertex_array_size = 0;
  757. uint32_t vertex_max_instances_allowed = 0xFFFFFFFF;
  758. bool index_buffer_uses_restart_indices = false;
  759. uint32_t index_array_size = 0;
  760. uint32_t index_array_max_index = 0;
  761. uint32_t index_array_offset = 0;
  762. Vector<uint32_t> set_formats;
  763. Vector<bool> set_bound;
  764. Vector<RID> set_rids;
  765. // Last pipeline set values.
  766. bool pipeline_active = false;
  767. uint32_t pipeline_dynamic_state = 0;
  768. VertexFormatID pipeline_vertex_format = INVALID_ID;
  769. RID pipeline_shader;
  770. bool pipeline_uses_restart_indices = false;
  771. uint32_t pipeline_primitive_divisor = 0;
  772. uint32_t pipeline_primitive_minimum = 0;
  773. uint32_t pipeline_push_constant_size = 0;
  774. bool pipeline_push_constant_supplied = false;
  775. } validation;
  776. #else
  777. struct Validation {
  778. uint32_t vertex_array_size = 0;
  779. uint32_t index_array_size = 0;
  780. uint32_t index_array_offset;
  781. } validation;
  782. #endif
  783. };
  784. DrawList *draw_list = nullptr; // One for regular draw lists, multiple for split.
  785. uint32_t draw_list_subpass_count = 0;
  786. uint32_t draw_list_count = 0;
  787. VkRenderPass draw_list_render_pass = VK_NULL_HANDLE;
  788. VkFramebuffer draw_list_vkframebuffer = VK_NULL_HANDLE;
  789. #ifdef DEBUG_ENABLED
  790. FramebufferFormatID draw_list_framebuffer_format = INVALID_ID;
  791. #endif
  792. uint32_t draw_list_current_subpass = 0;
  793. bool draw_list_split = false;
  794. Vector<RID> draw_list_bound_textures;
  795. Vector<RID> draw_list_storage_textures;
  796. bool draw_list_unbind_color_textures = false;
  797. bool draw_list_unbind_depth_textures = false;
  798. void _draw_list_insert_clear_region(DrawList *p_draw_list, Framebuffer *p_framebuffer, Point2i p_viewport_offset, Point2i p_viewport_size, bool p_clear_color, const Vector<Color> &p_clear_colors, bool p_clear_depth, float p_depth, uint32_t p_stencil);
  799. Error _draw_list_setup_framebuffer(Framebuffer *p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, VkFramebuffer *r_framebuffer, VkRenderPass *r_render_pass, uint32_t *r_subpass_count);
  800. Error _draw_list_render_pass_begin(Framebuffer *framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_colors, float p_clear_depth, uint32_t p_clear_stencil, Point2i viewport_offset, Point2i viewport_size, VkFramebuffer vkframebuffer, VkRenderPass render_pass, VkCommandBuffer command_buffer, VkSubpassContents subpass_contents, const Vector<RID> &p_storage_textures, bool p_constrained_to_region);
  801. _FORCE_INLINE_ DrawList *_get_draw_list_ptr(DrawListID p_id);
  802. Buffer *_get_buffer_from_owner(RID p_buffer, VkPipelineStageFlags &dst_stage_mask, VkAccessFlags &dst_access, BitField<BarrierMask> p_post_barrier);
  803. Error _draw_list_allocate(const Rect2i &p_viewport, uint32_t p_splits, uint32_t p_subpass);
  804. void _draw_list_free(Rect2i *r_last_viewport = nullptr);
  805. /**********************/
  806. /**** COMPUTE LIST ****/
  807. /**********************/
  808. struct ComputeList {
  809. VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
  810. struct SetState {
  811. uint32_t pipeline_expected_format = 0;
  812. uint32_t uniform_set_format = 0;
  813. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  814. RID uniform_set;
  815. bool bound = false;
  816. };
  817. struct State {
  818. HashSet<Texture *> textures_to_sampled_layout;
  819. SetState sets[MAX_UNIFORM_SETS];
  820. uint32_t set_count = 0;
  821. RID pipeline;
  822. RID pipeline_shader;
  823. uint32_t local_group_size[3] = { 0, 0, 0 };
  824. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  825. uint32_t pipeline_push_constant_stages = 0;
  826. bool allow_draw_overlap;
  827. } state;
  828. #ifdef DEBUG_ENABLED
  829. struct Validation {
  830. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  831. Vector<uint32_t> set_formats;
  832. Vector<bool> set_bound;
  833. Vector<RID> set_rids;
  834. // Last pipeline set values.
  835. bool pipeline_active = false;
  836. RID pipeline_shader;
  837. uint32_t invalid_set_from = 0;
  838. uint32_t pipeline_push_constant_size = 0;
  839. bool pipeline_push_constant_supplied = false;
  840. } validation;
  841. #endif
  842. };
  843. ComputeList *compute_list = nullptr;
  844. void _compute_list_add_barrier(BitField<BarrierMask> p_post_barrier, uint32_t p_barrier_flags, uint32_t p_access_flags);
  845. /**************************/
  846. /**** FRAME MANAGEMENT ****/
  847. /**************************/
  848. // This is the frame structure. There are normally
  849. // 3 of these (used for triple buffering), or 2
  850. // (double buffering). They are cycled constantly.
  851. //
  852. // It contains two command buffers, one that is
  853. // used internally for setting up (creating stuff)
  854. // and another used mostly for drawing.
  855. //
  856. // They also contains a list of things that need
  857. // to be disposed of when deleted, which can't
  858. // happen immediately due to the asynchronous
  859. // nature of the GPU. They will get deleted
  860. // when the frame is cycled.
  861. struct Frame {
  862. // List in usage order, from last to free to first to free.
  863. List<Buffer> buffers_to_dispose_of;
  864. List<Texture> textures_to_dispose_of;
  865. List<Framebuffer> framebuffers_to_dispose_of;
  866. List<VkSampler> samplers_to_dispose_of;
  867. List<Shader> shaders_to_dispose_of;
  868. List<VkBufferView> buffer_views_to_dispose_of;
  869. List<UniformSet> uniform_sets_to_dispose_of;
  870. List<RenderPipeline> render_pipelines_to_dispose_of;
  871. List<ComputePipeline> compute_pipelines_to_dispose_of;
  872. VkCommandPool command_pool = VK_NULL_HANDLE;
  873. // Used for filling up newly created buffers with data provided on creation.
  874. // Primarily intended to be accessed by worker threads.
  875. // Ideally this cmd buffer should use an async transfer queue.
  876. VkCommandBuffer setup_command_buffer = VK_NULL_HANDLE;
  877. // The main cmd buffer for drawing and compute.
  878. // Primarily intended to be used by the main thread to do most stuff.
  879. VkCommandBuffer draw_command_buffer = VK_NULL_HANDLE;
  880. struct Timestamp {
  881. String description;
  882. uint64_t value = 0;
  883. };
  884. VkQueryPool timestamp_pool;
  885. TightLocalVector<String> timestamp_names;
  886. TightLocalVector<uint64_t> timestamp_cpu_values;
  887. uint32_t timestamp_count = 0;
  888. TightLocalVector<String> timestamp_result_names;
  889. TightLocalVector<uint64_t> timestamp_cpu_result_values;
  890. TightLocalVector<uint64_t> timestamp_result_values;
  891. uint32_t timestamp_result_count = 0;
  892. uint64_t index = 0;
  893. };
  894. uint32_t max_timestamp_query_elements = 0;
  895. TightLocalVector<Frame> frames; // Frames available, for main device they are cycled (usually 3), for local devices only 1.
  896. int frame = 0; // Current frame.
  897. int frame_count = 0; // Total amount of frames.
  898. uint64_t frames_drawn = 0;
  899. RID local_device;
  900. bool local_device_processing = false;
  901. void _free_pending_resources(int p_frame);
  902. VmaAllocator allocator = nullptr;
  903. HashMap<uint32_t, VmaPool> small_allocs_pools;
  904. VmaPool _find_or_create_small_allocs_pool(uint32_t p_mem_type_index);
  905. VulkanContext *context = nullptr;
  906. uint64_t image_memory = 0;
  907. uint64_t buffer_memory = 0;
  908. void _free_internal(RID p_id);
  909. void _flush(bool p_current_frame);
  910. bool screen_prepared = false;
  911. template <class T>
  912. void _free_rids(T &p_owner, const char *p_type);
  913. void _finalize_command_bufers();
  914. void _begin_frame();
  915. #ifdef DEV_ENABLED
  916. HashMap<RID, String> resource_names;
  917. #endif
  918. VkSampleCountFlagBits _ensure_supported_sample_count(TextureSamples p_requested_sample_count) const;
  919. public:
  920. virtual RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>());
  921. virtual RID texture_create_shared(const TextureView &p_view, RID p_with_texture);
  922. virtual RID texture_create_from_extension(TextureType p_type, DataFormat p_format, TextureSamples p_samples, BitField<RenderingDevice::TextureUsageBits> p_flags, uint64_t p_image, uint64_t p_width, uint64_t p_height, uint64_t p_depth, uint64_t p_layers);
  923. virtual RID texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps = 1, TextureSliceType p_slice_type = TEXTURE_SLICE_2D, uint32_t p_layers = 0);
  924. virtual Error texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  925. virtual Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer);
  926. virtual bool texture_is_format_supported_for_usage(DataFormat p_format, BitField<RenderingDevice::TextureUsageBits> p_usage) const;
  927. virtual bool texture_is_shared(RID p_texture);
  928. virtual bool texture_is_valid(RID p_texture);
  929. virtual TextureFormat texture_get_format(RID p_texture);
  930. virtual Size2i texture_size(RID p_texture);
  931. virtual uint64_t texture_get_native_handle(RID p_texture);
  932. virtual Error texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  933. virtual Error texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  934. virtual Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  935. /*********************/
  936. /**** FRAMEBUFFER ****/
  937. /*********************/
  938. virtual FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count = 1);
  939. virtual FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1);
  940. virtual FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1);
  941. virtual TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0);
  942. virtual RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  943. virtual RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, const Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  944. virtual RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID);
  945. virtual bool framebuffer_is_valid(RID p_framebuffer) const;
  946. virtual void framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata);
  947. virtual FramebufferFormatID framebuffer_get_format(RID p_framebuffer);
  948. /*****************/
  949. /**** SAMPLER ****/
  950. /*****************/
  951. virtual RID sampler_create(const SamplerState &p_state);
  952. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_sampler_filter) const;
  953. /**********************/
  954. /**** VERTEX ARRAY ****/
  955. /**********************/
  956. virtual RID vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_as_storage = false);
  957. // Internally reference counted, this ID is warranted to be unique for the same description, but needs to be freed as many times as it was allocated.
  958. virtual VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats);
  959. virtual RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers, const Vector<uint64_t> &p_offsets = Vector<uint64_t>());
  960. virtual RID index_buffer_create(uint32_t p_size_indices, IndexBufferFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_restart_indices = false);
  961. virtual RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count);
  962. /****************/
  963. /**** SHADER ****/
  964. /****************/
  965. virtual String shader_get_binary_cache_key() const;
  966. virtual Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
  967. virtual RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, RID p_placeholder = RID());
  968. virtual RID shader_create_placeholder();
  969. virtual uint64_t shader_get_vertex_input_attribute_mask(RID p_shader);
  970. /*****************/
  971. /**** UNIFORM ****/
  972. /*****************/
  973. virtual RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  974. virtual RID storage_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), BitField<StorageBufferUsage> p_usage = 0);
  975. virtual RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  976. virtual RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set);
  977. virtual bool uniform_set_is_valid(RID p_uniform_set);
  978. virtual void uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata);
  979. virtual Error buffer_copy(RID p_src_buffer, RID p_dst_buffer, uint32_t p_src_offset, uint32_t p_dst_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  980. virtual Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS); // Works for any buffer.
  981. virtual Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  982. virtual Vector<uint8_t> buffer_get_data(RID p_buffer, uint32_t p_offset = 0, uint32_t p_size = 0);
  983. /*************************/
  984. /**** RENDER PIPELINE ****/
  985. /*************************/
  986. virtual RID render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags = 0, uint32_t p_for_render_pass = 0, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  987. virtual bool render_pipeline_is_valid(RID p_pipeline);
  988. /**************************/
  989. /**** COMPUTE PIPELINE ****/
  990. /**************************/
  991. virtual RID compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  992. virtual bool compute_pipeline_is_valid(RID p_pipeline);
  993. /****************/
  994. /**** SCREEN ****/
  995. /****************/
  996. virtual int screen_get_width(DisplayServer::WindowID p_screen = 0) const;
  997. virtual int screen_get_height(DisplayServer::WindowID p_screen = 0) const;
  998. virtual FramebufferFormatID screen_get_framebuffer_format() const;
  999. /********************/
  1000. /**** DRAW LISTS ****/
  1001. /********************/
  1002. virtual DrawListID draw_list_begin_for_screen(DisplayServer::WindowID p_screen = 0, const Color &p_clear_color = Color());
  1003. virtual DrawListID draw_list_begin(RID p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>());
  1004. virtual Error draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, DrawListID *r_split_ids, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>());
  1005. virtual void draw_list_set_blend_constants(DrawListID p_list, const Color &p_color);
  1006. virtual void draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline);
  1007. virtual void draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index);
  1008. virtual void draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array);
  1009. virtual void draw_list_bind_index_array(DrawListID p_list, RID p_index_array);
  1010. virtual void draw_list_set_line_width(DrawListID p_list, float p_width);
  1011. virtual void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size);
  1012. virtual void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0);
  1013. virtual void draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect);
  1014. virtual void draw_list_disable_scissor(DrawListID p_list);
  1015. virtual uint32_t draw_list_get_current_pass();
  1016. virtual DrawListID draw_list_switch_to_next_pass();
  1017. virtual Error draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids);
  1018. virtual void draw_list_end(BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  1019. /***********************/
  1020. /**** COMPUTE LISTS ****/
  1021. /***********************/
  1022. virtual ComputeListID compute_list_begin(bool p_allow_draw_overlap = false);
  1023. virtual void compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline);
  1024. virtual void compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index);
  1025. virtual void compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size);
  1026. virtual void compute_list_add_barrier(ComputeListID p_list);
  1027. virtual void compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
  1028. virtual void compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads);
  1029. virtual void compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset);
  1030. virtual void compute_list_end(BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  1031. virtual void barrier(BitField<BarrierMask> p_from = BARRIER_MASK_ALL_BARRIERS, BitField<BarrierMask> p_to = BARRIER_MASK_ALL_BARRIERS);
  1032. virtual void full_barrier();
  1033. /**************/
  1034. /**** FREE ****/
  1035. /**************/
  1036. virtual void free(RID p_id);
  1037. /****************/
  1038. /**** Timing ****/
  1039. /****************/
  1040. virtual void capture_timestamp(const String &p_name);
  1041. virtual uint32_t get_captured_timestamps_count() const;
  1042. virtual uint64_t get_captured_timestamps_frame() const;
  1043. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
  1044. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
  1045. virtual String get_captured_timestamp_name(uint32_t p_index) const;
  1046. /****************/
  1047. /**** Limits ****/
  1048. /****************/
  1049. virtual uint64_t limit_get(Limit p_limit) const;
  1050. virtual void prepare_screen_for_drawing();
  1051. void initialize(VulkanContext *p_context, bool p_local_device = false);
  1052. void finalize();
  1053. virtual void swap_buffers(); // For main device.
  1054. virtual void submit(); // For local device.
  1055. virtual void sync(); // For local device.
  1056. virtual uint32_t get_frame_delay() const;
  1057. virtual RenderingDevice *create_local_device();
  1058. virtual uint64_t get_memory_usage(MemoryType p_type) const;
  1059. virtual void set_resource_name(RID p_id, const String p_name);
  1060. virtual void draw_command_begin_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
  1061. virtual void draw_command_insert_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
  1062. virtual void draw_command_end_label();
  1063. virtual String get_device_vendor_name() const;
  1064. virtual String get_device_name() const;
  1065. virtual RenderingDevice::DeviceType get_device_type() const;
  1066. virtual String get_device_api_version() const;
  1067. virtual String get_device_pipeline_cache_uuid() const;
  1068. virtual uint64_t get_driver_resource(DriverResource p_resource, RID p_rid = RID(), uint64_t p_index = 0);
  1069. virtual bool has_feature(const Features p_feature) const;
  1070. RenderingDeviceVulkan();
  1071. ~RenderingDeviceVulkan();
  1072. };
  1073. #endif // RENDERING_DEVICE_VULKAN_H