rendering_device_vulkan.h 47 KB

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