rendering_device_vulkan.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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/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. bool is_resolve_buffer = false;
  131. uint32_t read_aspect_mask = 0;
  132. uint32_t barrier_aspect_mask = 0;
  133. bool bound = false; // Bound to framebffer.
  134. RID owner;
  135. };
  136. RID_Owner<Texture, true> texture_owner;
  137. uint32_t texture_upload_region_size_px = 0;
  138. Vector<uint8_t> _texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer, bool p_2d = false);
  139. 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);
  140. /*****************/
  141. /**** SAMPLER ****/
  142. /*****************/
  143. RID_Owner<VkSampler> sampler_owner;
  144. /***************************/
  145. /**** BUFFER MANAGEMENT ****/
  146. /***************************/
  147. // These are temporary buffers on CPU memory that hold
  148. // the information until the CPU fetches it and places it
  149. // either on GPU buffers, or images (textures). It ensures
  150. // updates are properly synchronized with whatever the
  151. // GPU is doing.
  152. //
  153. // The logic here is as follows, only 3 of these
  154. // blocks are created at the beginning (one per frame)
  155. // they can each belong to a frame (assigned to current when
  156. // used) and they can only be reused after the same frame is
  157. // recycled.
  158. //
  159. // When CPU requires to allocate more than what is available,
  160. // more of these buffers are created. If a limit is reached,
  161. // then a fence will ensure will wait for blocks allocated
  162. // in previous frames are processed. If that fails, then
  163. // another fence will ensure everything pending for the current
  164. // frame is processed (effectively stalling).
  165. //
  166. // See the comments in the code to understand better how it works.
  167. struct StagingBufferBlock {
  168. VkBuffer buffer = VK_NULL_HANDLE;
  169. VmaAllocation allocation = nullptr;
  170. uint64_t frame_used = 0;
  171. uint32_t fill_amount = 0;
  172. };
  173. Vector<StagingBufferBlock> staging_buffer_blocks;
  174. int staging_buffer_current = 0;
  175. uint32_t staging_buffer_block_size = 0;
  176. uint64_t staging_buffer_max_size = 0;
  177. bool staging_buffer_used = false;
  178. 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);
  179. Error _insert_staging_block();
  180. struct Buffer {
  181. uint32_t size = 0;
  182. uint32_t usage = 0;
  183. VkBuffer buffer = VK_NULL_HANDLE;
  184. VmaAllocation allocation = nullptr;
  185. VkDescriptorBufferInfo buffer_info; // Used for binding.
  186. Buffer() {
  187. }
  188. };
  189. Error _buffer_allocate(Buffer *p_buffer, uint32_t p_size, uint32_t p_usage, VmaMemoryUsage p_mem_usage, VmaAllocationCreateFlags p_mem_flags);
  190. Error _buffer_free(Buffer *p_buffer);
  191. 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);
  192. void _full_barrier(bool p_sync_with_draw);
  193. 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);
  194. 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);
  195. /*********************/
  196. /**** FRAMEBUFFER ****/
  197. /*********************/
  198. // In Vulkan, framebuffers work similar to how they
  199. // do in OpenGL, with the exception that
  200. // the "format" (vkRenderPass) is not dynamic
  201. // and must be more or less the same as the one
  202. // used for the render pipelines.
  203. struct FramebufferFormatKey {
  204. Vector<AttachmentFormat> attachments;
  205. Vector<FramebufferPass> passes;
  206. uint32_t view_count = 1;
  207. bool operator<(const FramebufferFormatKey &p_key) const {
  208. if (view_count != p_key.view_count) {
  209. return view_count < p_key.view_count;
  210. }
  211. uint32_t pass_size = passes.size();
  212. uint32_t key_pass_size = p_key.passes.size();
  213. if (pass_size != key_pass_size) {
  214. return pass_size < key_pass_size;
  215. }
  216. const FramebufferPass *pass_ptr = passes.ptr();
  217. const FramebufferPass *key_pass_ptr = p_key.passes.ptr();
  218. for (uint32_t i = 0; i < pass_size; i++) {
  219. { // Compare color attachments.
  220. uint32_t attachment_size = pass_ptr[i].color_attachments.size();
  221. uint32_t key_attachment_size = key_pass_ptr[i].color_attachments.size();
  222. if (attachment_size != key_attachment_size) {
  223. return attachment_size < key_attachment_size;
  224. }
  225. const int32_t *pass_attachment_ptr = pass_ptr[i].color_attachments.ptr();
  226. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].color_attachments.ptr();
  227. for (uint32_t j = 0; j < attachment_size; j++) {
  228. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  229. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  230. }
  231. }
  232. }
  233. { // Compare input attachments.
  234. uint32_t attachment_size = pass_ptr[i].input_attachments.size();
  235. uint32_t key_attachment_size = key_pass_ptr[i].input_attachments.size();
  236. if (attachment_size != key_attachment_size) {
  237. return attachment_size < key_attachment_size;
  238. }
  239. const int32_t *pass_attachment_ptr = pass_ptr[i].input_attachments.ptr();
  240. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].input_attachments.ptr();
  241. for (uint32_t j = 0; j < attachment_size; j++) {
  242. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  243. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  244. }
  245. }
  246. }
  247. { // Compare resolve attachments.
  248. uint32_t attachment_size = pass_ptr[i].resolve_attachments.size();
  249. uint32_t key_attachment_size = key_pass_ptr[i].resolve_attachments.size();
  250. if (attachment_size != key_attachment_size) {
  251. return attachment_size < key_attachment_size;
  252. }
  253. const int32_t *pass_attachment_ptr = pass_ptr[i].resolve_attachments.ptr();
  254. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].resolve_attachments.ptr();
  255. for (uint32_t j = 0; j < attachment_size; j++) {
  256. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  257. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  258. }
  259. }
  260. }
  261. { // Compare preserve attachments.
  262. uint32_t attachment_size = pass_ptr[i].preserve_attachments.size();
  263. uint32_t key_attachment_size = key_pass_ptr[i].preserve_attachments.size();
  264. if (attachment_size != key_attachment_size) {
  265. return attachment_size < key_attachment_size;
  266. }
  267. const int32_t *pass_attachment_ptr = pass_ptr[i].preserve_attachments.ptr();
  268. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].preserve_attachments.ptr();
  269. for (uint32_t j = 0; j < attachment_size; j++) {
  270. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  271. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  272. }
  273. }
  274. }
  275. if (pass_ptr[i].depth_attachment != key_pass_ptr[i].depth_attachment) {
  276. return pass_ptr[i].depth_attachment < key_pass_ptr[i].depth_attachment;
  277. }
  278. }
  279. int as = attachments.size();
  280. int bs = p_key.attachments.size();
  281. if (as != bs) {
  282. return as < bs;
  283. }
  284. const AttachmentFormat *af_a = attachments.ptr();
  285. const AttachmentFormat *af_b = p_key.attachments.ptr();
  286. for (int i = 0; i < as; i++) {
  287. const AttachmentFormat &a = af_a[i];
  288. const AttachmentFormat &b = af_b[i];
  289. if (a.format != b.format) {
  290. return a.format < b.format;
  291. }
  292. if (a.samples != b.samples) {
  293. return a.samples < b.samples;
  294. }
  295. if (a.usage_flags != b.usage_flags) {
  296. return a.usage_flags < b.usage_flags;
  297. }
  298. }
  299. return false; // Equal.
  300. }
  301. };
  302. 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);
  303. // This is a cache and it's never freed, it ensures
  304. // IDs for a given format are always unique.
  305. RBMap<FramebufferFormatKey, FramebufferFormatID> framebuffer_format_cache;
  306. struct FramebufferFormat {
  307. const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E;
  308. VkRenderPass render_pass = VK_NULL_HANDLE; // Here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec).
  309. Vector<TextureSamples> pass_samples;
  310. uint32_t view_count = 1; // Number of views.
  311. };
  312. HashMap<FramebufferFormatID, FramebufferFormat> framebuffer_formats;
  313. struct Framebuffer {
  314. FramebufferFormatID format_id = 0;
  315. struct VersionKey {
  316. InitialAction initial_color_action;
  317. FinalAction final_color_action;
  318. InitialAction initial_depth_action;
  319. FinalAction final_depth_action;
  320. uint32_t view_count;
  321. bool operator<(const VersionKey &p_key) const {
  322. if (initial_color_action == p_key.initial_color_action) {
  323. if (final_color_action == p_key.final_color_action) {
  324. if (initial_depth_action == p_key.initial_depth_action) {
  325. if (final_depth_action == p_key.final_depth_action) {
  326. return view_count < p_key.view_count;
  327. } else {
  328. return final_depth_action < p_key.final_depth_action;
  329. }
  330. } else {
  331. return initial_depth_action < p_key.initial_depth_action;
  332. }
  333. } else {
  334. return final_color_action < p_key.final_color_action;
  335. }
  336. } else {
  337. return initial_color_action < p_key.initial_color_action;
  338. }
  339. }
  340. };
  341. uint32_t storage_mask = 0;
  342. Vector<RID> texture_ids;
  343. InvalidationCallback invalidated_callback = nullptr;
  344. void *invalidated_callback_userdata = nullptr;
  345. struct Version {
  346. VkFramebuffer framebuffer = VK_NULL_HANDLE;
  347. VkRenderPass render_pass = VK_NULL_HANDLE; // This one is owned.
  348. uint32_t subpass_count = 1;
  349. };
  350. RBMap<VersionKey, Version> framebuffers;
  351. Size2 size;
  352. uint32_t view_count;
  353. };
  354. RID_Owner<Framebuffer, true> framebuffer_owner;
  355. /***********************/
  356. /**** VERTEX BUFFER ****/
  357. /***********************/
  358. // Vertex buffers in Vulkan are similar to how
  359. // they work in OpenGL, except that instead of
  360. // an attribute index, there is a buffer binding
  361. // index (for binding the buffers in real-time)
  362. // and a location index (what is used in the shader).
  363. //
  364. // This mapping is done here internally, and it's not
  365. // exposed.
  366. RID_Owner<Buffer, true> vertex_buffer_owner;
  367. struct VertexDescriptionKey {
  368. Vector<VertexAttribute> vertex_formats;
  369. bool operator==(const VertexDescriptionKey &p_key) const {
  370. int vdc = vertex_formats.size();
  371. int vdck = p_key.vertex_formats.size();
  372. if (vdc != vdck) {
  373. return false;
  374. } else {
  375. const VertexAttribute *a_ptr = vertex_formats.ptr();
  376. const VertexAttribute *b_ptr = p_key.vertex_formats.ptr();
  377. for (int i = 0; i < vdc; i++) {
  378. const VertexAttribute &a = a_ptr[i];
  379. const VertexAttribute &b = b_ptr[i];
  380. if (a.location != b.location) {
  381. return false;
  382. }
  383. if (a.offset != b.offset) {
  384. return false;
  385. }
  386. if (a.format != b.format) {
  387. return false;
  388. }
  389. if (a.stride != b.stride) {
  390. return false;
  391. }
  392. if (a.frequency != b.frequency) {
  393. return false;
  394. }
  395. }
  396. return true; // They are equal.
  397. }
  398. }
  399. uint32_t hash() const {
  400. int vdc = vertex_formats.size();
  401. uint32_t h = hash_murmur3_one_32(vdc);
  402. const VertexAttribute *ptr = vertex_formats.ptr();
  403. for (int i = 0; i < vdc; i++) {
  404. const VertexAttribute &vd = ptr[i];
  405. h = hash_murmur3_one_32(vd.location, h);
  406. h = hash_murmur3_one_32(vd.offset, h);
  407. h = hash_murmur3_one_32(vd.format, h);
  408. h = hash_murmur3_one_32(vd.stride, h);
  409. h = hash_murmur3_one_32(vd.frequency, h);
  410. }
  411. return hash_fmix32(h);
  412. }
  413. };
  414. struct VertexDescriptionHash {
  415. static _FORCE_INLINE_ uint32_t hash(const VertexDescriptionKey &p_key) {
  416. return p_key.hash();
  417. }
  418. };
  419. // This is a cache and it's never freed, it ensures that
  420. // ID used for a specific format always remain the same.
  421. HashMap<VertexDescriptionKey, VertexFormatID, VertexDescriptionHash> vertex_format_cache;
  422. struct VertexDescriptionCache {
  423. Vector<VertexAttribute> vertex_formats;
  424. VkVertexInputBindingDescription *bindings = nullptr;
  425. VkVertexInputAttributeDescription *attributes = nullptr;
  426. VkPipelineVertexInputStateCreateInfo create_info;
  427. };
  428. HashMap<VertexFormatID, VertexDescriptionCache> vertex_formats;
  429. struct VertexArray {
  430. RID buffer;
  431. VertexFormatID description = 0;
  432. int vertex_count = 0;
  433. uint32_t max_instances_allowed = 0;
  434. Vector<VkBuffer> buffers; // Not owned, just referenced.
  435. Vector<VkDeviceSize> offsets;
  436. };
  437. RID_Owner<VertexArray, true> vertex_array_owner;
  438. struct IndexBuffer : public Buffer {
  439. uint32_t max_index = 0; // Used for validation.
  440. uint32_t index_count = 0;
  441. VkIndexType index_type = VK_INDEX_TYPE_NONE_NV;
  442. bool supports_restart_indices = false;
  443. };
  444. RID_Owner<IndexBuffer, true> index_buffer_owner;
  445. struct IndexArray {
  446. uint32_t max_index = 0; // Remember the maximum index here too, for validation.
  447. VkBuffer buffer; // Not owned, inherited from index buffer.
  448. uint32_t offset = 0;
  449. uint32_t indices = 0;
  450. VkIndexType index_type = VK_INDEX_TYPE_NONE_NV;
  451. bool supports_restart_indices = false;
  452. };
  453. RID_Owner<IndexArray, true> index_array_owner;
  454. /****************/
  455. /**** SHADER ****/
  456. /****************/
  457. // Vulkan specifies a really complex behavior for the application
  458. // in order to tell when descriptor sets need to be re-bound (or not).
  459. // "When binding a descriptor set (see Descriptor Set Binding) to set
  460. // number N, if the previously bound descriptor sets for sets zero
  461. // through N-1 were all bound using compatible pipeline layouts,
  462. // then performing this binding does not disturb any of the lower numbered sets.
  463. // If, additionally, the previous bound descriptor set for set N was
  464. // bound using a pipeline layout compatible for set N, then the bindings
  465. // in sets numbered greater than N are also not disturbed."
  466. // As a result, we need to figure out quickly when something is no longer "compatible".
  467. // in order to avoid costly rebinds.
  468. struct UniformInfo {
  469. UniformType type = UniformType::UNIFORM_TYPE_MAX;
  470. bool writable = false;
  471. int binding = 0;
  472. uint32_t stages = 0;
  473. int length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
  474. bool operator!=(const UniformInfo &p_info) const {
  475. return (binding != p_info.binding || type != p_info.type || writable != p_info.writable || stages != p_info.stages || length != p_info.length);
  476. }
  477. bool operator<(const UniformInfo &p_info) const {
  478. if (binding != p_info.binding) {
  479. return binding < p_info.binding;
  480. }
  481. if (type != p_info.type) {
  482. return type < p_info.type;
  483. }
  484. if (writable != p_info.writable) {
  485. return writable < p_info.writable;
  486. }
  487. if (stages != p_info.stages) {
  488. return stages < p_info.stages;
  489. }
  490. return length < p_info.length;
  491. }
  492. };
  493. struct UniformSetFormat {
  494. Vector<UniformInfo> uniform_info;
  495. bool operator<(const UniformSetFormat &p_format) const {
  496. uint32_t size = uniform_info.size();
  497. uint32_t psize = p_format.uniform_info.size();
  498. if (size != psize) {
  499. return size < psize;
  500. }
  501. const UniformInfo *infoptr = uniform_info.ptr();
  502. const UniformInfo *pinfoptr = p_format.uniform_info.ptr();
  503. for (uint32_t i = 0; i < size; i++) {
  504. if (infoptr[i] != pinfoptr[i]) {
  505. return infoptr[i] < pinfoptr[i];
  506. }
  507. }
  508. return false;
  509. }
  510. };
  511. // Always grows, never shrinks, ensuring unique IDs, but we assume
  512. // the amount of formats will never be a problem, as the amount of shaders
  513. // in a game is limited.
  514. RBMap<UniformSetFormat, uint32_t> uniform_set_format_cache;
  515. // Shaders in Vulkan are just pretty much
  516. // precompiled blocks of SPIR-V bytecode. They
  517. // are most likely not really compiled to host
  518. // assembly until a pipeline is created.
  519. //
  520. // When supplying the shaders, this implementation
  521. // will use the reflection abilities of glslang to
  522. // understand and cache everything required to
  523. // create and use the descriptor sets (Vulkan's
  524. // biggest pain).
  525. //
  526. // Additionally, hashes are created for every set
  527. // to do quick validation and ensuring the user
  528. // does not submit something invalid.
  529. struct Shader {
  530. struct Set {
  531. Vector<UniformInfo> uniform_info;
  532. VkDescriptorSetLayout descriptor_set_layout = VK_NULL_HANDLE;
  533. };
  534. uint32_t vertex_input_mask = 0; // Inputs used, this is mostly for validation.
  535. uint32_t fragment_output_mask = 0;
  536. struct PushConstant {
  537. uint32_t size = 0;
  538. uint32_t vk_stages_mask = 0;
  539. };
  540. PushConstant push_constant;
  541. uint32_t compute_local_size[3] = { 0, 0, 0 };
  542. struct SpecializationConstant {
  543. PipelineSpecializationConstant constant;
  544. uint32_t stage_flags = 0;
  545. };
  546. bool is_compute = false;
  547. Vector<Set> sets;
  548. Vector<uint32_t> set_formats;
  549. Vector<VkPipelineShaderStageCreateInfo> pipeline_stages;
  550. Vector<SpecializationConstant> specialization_constants;
  551. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  552. String name; // Used for debug.
  553. };
  554. String _shader_uniform_debug(RID p_shader, int p_set = -1);
  555. RID_Owner<Shader, true> shader_owner;
  556. /******************/
  557. /**** UNIFORMS ****/
  558. /******************/
  559. // Descriptor sets require allocation from a pool.
  560. // The documentation on how to use pools properly
  561. // is scarce, and the documentation is strange.
  562. //
  563. // Basically, you can mix and match pools as you
  564. // like, but you'll run into fragmentation issues.
  565. // Because of this, the recommended approach is to
  566. // create a pool for every descriptor set type, as
  567. // this prevents fragmentation.
  568. //
  569. // This is implemented here as a having a list of
  570. // pools (each can contain up to 64 sets) for each
  571. // set layout. The amount of sets for each type
  572. // is used as the key.
  573. enum {
  574. MAX_DESCRIPTOR_POOL_ELEMENT = 65535
  575. };
  576. struct DescriptorPoolKey {
  577. union {
  578. struct {
  579. uint16_t uniform_type[UNIFORM_TYPE_MAX]; // Using 16 bits because, for sending arrays, each element is a pool set.
  580. };
  581. struct {
  582. uint64_t key1;
  583. uint64_t key2;
  584. uint64_t key3;
  585. };
  586. };
  587. bool operator<(const DescriptorPoolKey &p_key) const {
  588. if (key1 != p_key.key1) {
  589. return key1 < p_key.key1;
  590. }
  591. if (key2 != p_key.key2) {
  592. return key2 < p_key.key2;
  593. }
  594. return key3 < p_key.key3;
  595. }
  596. DescriptorPoolKey() {
  597. key1 = 0;
  598. key2 = 0;
  599. key3 = 0;
  600. }
  601. };
  602. struct DescriptorPool {
  603. VkDescriptorPool pool;
  604. uint32_t usage;
  605. };
  606. RBMap<DescriptorPoolKey, HashSet<DescriptorPool *>> descriptor_pools;
  607. uint32_t max_descriptors_per_pool = 0;
  608. DescriptorPool *_descriptor_pool_allocate(const DescriptorPoolKey &p_key);
  609. void _descriptor_pool_free(const DescriptorPoolKey &p_key, DescriptorPool *p_pool);
  610. RID_Owner<Buffer, true> uniform_buffer_owner;
  611. RID_Owner<Buffer, true> storage_buffer_owner;
  612. // Texture buffer needs a view.
  613. struct TextureBuffer {
  614. Buffer buffer;
  615. VkBufferView view = VK_NULL_HANDLE;
  616. };
  617. RID_Owner<TextureBuffer, true> texture_buffer_owner;
  618. // This structure contains the descriptor set. They _need_ to be allocated
  619. // for a shader (and will be erased when this shader is erased), but should
  620. // work for other shaders as long as the hash matches. This covers using
  621. // them in shader variants.
  622. //
  623. // Keep also in mind that you can share buffers between descriptor sets, so
  624. // the above restriction is not too serious.
  625. struct UniformSet {
  626. uint32_t format = 0;
  627. RID shader_id;
  628. uint32_t shader_set = 0;
  629. DescriptorPool *pool = nullptr;
  630. DescriptorPoolKey pool_key;
  631. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  632. //VkPipelineLayout pipeline_layout; // Not owned, inherited from shader.
  633. struct AttachableTexture {
  634. uint32_t bind;
  635. RID texture;
  636. };
  637. LocalVector<AttachableTexture> attachable_textures; // Used for validation.
  638. Vector<Texture *> mutable_sampled_textures; // Used for layout change.
  639. Vector<Texture *> mutable_storage_textures; // Used for layout change.
  640. InvalidationCallback invalidated_callback = nullptr;
  641. void *invalidated_callback_userdata = nullptr;
  642. };
  643. RID_Owner<UniformSet, true> uniform_set_owner;
  644. /*******************/
  645. /**** PIPELINES ****/
  646. /*******************/
  647. // Render pipeline contains ALL the
  648. // information required for drawing.
  649. // This includes all the rasterizer state
  650. // as well as shader used, framebuffer format,
  651. // etc.
  652. // While the pipeline is just a single object
  653. // (VkPipeline) a lot of values are also saved
  654. // here to do validation (vulkan does none by
  655. // default) and warn the user if something
  656. // was not supplied as intended.
  657. struct RenderPipeline {
  658. // Cached values for validation.
  659. #ifdef DEBUG_ENABLED
  660. struct Validation {
  661. FramebufferFormatID framebuffer_format = 0;
  662. uint32_t render_pass = 0;
  663. uint32_t dynamic_state = 0;
  664. VertexFormatID vertex_format = 0;
  665. bool uses_restart_indices = false;
  666. uint32_t primitive_minimum = 0;
  667. uint32_t primitive_divisor = 0;
  668. } validation;
  669. #endif
  670. // Actual pipeline.
  671. RID shader;
  672. Vector<uint32_t> set_formats;
  673. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // Not owned, needed for push constants.
  674. VkPipeline pipeline = VK_NULL_HANDLE;
  675. uint32_t push_constant_size = 0;
  676. uint32_t push_constant_stages_mask = 0;
  677. };
  678. RID_Owner<RenderPipeline, true> render_pipeline_owner;
  679. struct ComputePipeline {
  680. RID shader;
  681. Vector<uint32_t> set_formats;
  682. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // Not owned, needed for push constants.
  683. VkPipeline pipeline = VK_NULL_HANDLE;
  684. uint32_t push_constant_size = 0;
  685. uint32_t push_constant_stages_mask = 0;
  686. uint32_t local_group_size[3] = { 0, 0, 0 };
  687. };
  688. RID_Owner<ComputePipeline, true> compute_pipeline_owner;
  689. /*******************/
  690. /**** DRAW LIST ****/
  691. /*******************/
  692. // Draw list contains both the command buffer
  693. // used for drawing as well as a LOT of
  694. // information used for validation. This
  695. // validation is cheap so most of it can
  696. // also run in release builds.
  697. // When using split command lists, this is
  698. // implemented internally using secondary command
  699. // buffers. As they can be created in threads,
  700. // each needs its own command pool.
  701. struct SplitDrawListAllocator {
  702. VkCommandPool command_pool = VK_NULL_HANDLE;
  703. Vector<VkCommandBuffer> command_buffers; // One for each frame.
  704. };
  705. Vector<SplitDrawListAllocator> split_draw_list_allocators;
  706. struct DrawList {
  707. VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
  708. Rect2i viewport;
  709. bool viewport_set = false;
  710. struct SetState {
  711. uint32_t pipeline_expected_format = 0;
  712. uint32_t uniform_set_format = 0;
  713. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  714. RID uniform_set;
  715. bool bound = false;
  716. };
  717. struct State {
  718. SetState sets[MAX_UNIFORM_SETS];
  719. uint32_t set_count = 0;
  720. RID pipeline;
  721. RID pipeline_shader;
  722. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  723. RID vertex_array;
  724. RID index_array;
  725. uint32_t pipeline_push_constant_stages = 0;
  726. } state;
  727. #ifdef DEBUG_ENABLED
  728. struct Validation {
  729. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  730. // Actual render pass values.
  731. uint32_t dynamic_state = 0;
  732. VertexFormatID vertex_format = INVALID_ID;
  733. uint32_t vertex_array_size = 0;
  734. uint32_t vertex_max_instances_allowed = 0xFFFFFFFF;
  735. bool index_buffer_uses_restart_indices = false;
  736. uint32_t index_array_size = 0;
  737. uint32_t index_array_max_index = 0;
  738. uint32_t index_array_offset = 0;
  739. Vector<uint32_t> set_formats;
  740. Vector<bool> set_bound;
  741. Vector<RID> set_rids;
  742. // Last pipeline set values.
  743. bool pipeline_active = false;
  744. uint32_t pipeline_dynamic_state = 0;
  745. VertexFormatID pipeline_vertex_format = INVALID_ID;
  746. RID pipeline_shader;
  747. bool pipeline_uses_restart_indices = false;
  748. uint32_t pipeline_primitive_divisor = 0;
  749. uint32_t pipeline_primitive_minimum = 0;
  750. uint32_t pipeline_push_constant_size = 0;
  751. bool pipeline_push_constant_supplied = false;
  752. } validation;
  753. #else
  754. struct Validation {
  755. uint32_t vertex_array_size = 0;
  756. uint32_t index_array_size = 0;
  757. uint32_t index_array_offset;
  758. } validation;
  759. #endif
  760. };
  761. DrawList *draw_list = nullptr; // One for regular draw lists, multiple for split.
  762. uint32_t draw_list_subpass_count = 0;
  763. uint32_t draw_list_count = 0;
  764. VkRenderPass draw_list_render_pass = VK_NULL_HANDLE;
  765. VkFramebuffer draw_list_vkframebuffer = VK_NULL_HANDLE;
  766. #ifdef DEBUG_ENABLED
  767. FramebufferFormatID draw_list_framebuffer_format = INVALID_ID;
  768. #endif
  769. uint32_t draw_list_current_subpass = 0;
  770. bool draw_list_split = false;
  771. Vector<RID> draw_list_bound_textures;
  772. Vector<RID> draw_list_storage_textures;
  773. bool draw_list_unbind_color_textures = false;
  774. bool draw_list_unbind_depth_textures = false;
  775. 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);
  776. 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);
  777. 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);
  778. _FORCE_INLINE_ DrawList *_get_draw_list_ptr(DrawListID p_id);
  779. Buffer *_get_buffer_from_owner(RID p_buffer, VkPipelineStageFlags &dst_stage_mask, VkAccessFlags &dst_access, BitField<BarrierMask> p_post_barrier);
  780. Error _draw_list_allocate(const Rect2i &p_viewport, uint32_t p_splits, uint32_t p_subpass);
  781. void _draw_list_free(Rect2i *r_last_viewport = nullptr);
  782. /**********************/
  783. /**** COMPUTE LIST ****/
  784. /**********************/
  785. struct ComputeList {
  786. VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
  787. struct SetState {
  788. uint32_t pipeline_expected_format = 0;
  789. uint32_t uniform_set_format = 0;
  790. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  791. RID uniform_set;
  792. bool bound = false;
  793. };
  794. struct State {
  795. HashSet<Texture *> textures_to_sampled_layout;
  796. SetState sets[MAX_UNIFORM_SETS];
  797. uint32_t set_count = 0;
  798. RID pipeline;
  799. RID pipeline_shader;
  800. uint32_t local_group_size[3] = { 0, 0, 0 };
  801. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  802. uint32_t pipeline_push_constant_stages = 0;
  803. bool allow_draw_overlap;
  804. } state;
  805. #ifdef DEBUG_ENABLED
  806. struct Validation {
  807. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  808. Vector<uint32_t> set_formats;
  809. Vector<bool> set_bound;
  810. Vector<RID> set_rids;
  811. // Last pipeline set values.
  812. bool pipeline_active = false;
  813. RID pipeline_shader;
  814. uint32_t invalid_set_from = 0;
  815. uint32_t pipeline_push_constant_size = 0;
  816. bool pipeline_push_constant_supplied = false;
  817. } validation;
  818. #endif
  819. };
  820. ComputeList *compute_list = nullptr;
  821. /**************************/
  822. /**** FRAME MANAGEMENT ****/
  823. /**************************/
  824. // This is the frame structure. There are normally
  825. // 3 of these (used for triple buffering), or 2
  826. // (double buffering). They are cycled constantly.
  827. //
  828. // It contains two command buffers, one that is
  829. // used internally for setting up (creating stuff)
  830. // and another used mostly for drawing.
  831. //
  832. // They also contains a list of things that need
  833. // to be disposed of when deleted, which can't
  834. // happen immediately due to the asynchronous
  835. // nature of the GPU. They will get deleted
  836. // when the frame is cycled.
  837. struct Frame {
  838. // List in usage order, from last to free to first to free.
  839. List<Buffer> buffers_to_dispose_of;
  840. List<Texture> textures_to_dispose_of;
  841. List<Framebuffer> framebuffers_to_dispose_of;
  842. List<VkSampler> samplers_to_dispose_of;
  843. List<Shader> shaders_to_dispose_of;
  844. List<VkBufferView> buffer_views_to_dispose_of;
  845. List<UniformSet> uniform_sets_to_dispose_of;
  846. List<RenderPipeline> render_pipelines_to_dispose_of;
  847. List<ComputePipeline> compute_pipelines_to_dispose_of;
  848. VkCommandPool command_pool = VK_NULL_HANDLE;
  849. VkCommandBuffer setup_command_buffer = VK_NULL_HANDLE; // Used at the beginning of every frame for set-up.
  850. VkCommandBuffer draw_command_buffer = VK_NULL_HANDLE; // Used at the beginning of every frame for set-up.
  851. struct Timestamp {
  852. String description;
  853. uint64_t value = 0;
  854. };
  855. VkQueryPool timestamp_pool;
  856. TightLocalVector<String> timestamp_names;
  857. TightLocalVector<uint64_t> timestamp_cpu_values;
  858. uint32_t timestamp_count = 0;
  859. TightLocalVector<String> timestamp_result_names;
  860. TightLocalVector<uint64_t> timestamp_cpu_result_values;
  861. TightLocalVector<uint64_t> timestamp_result_values;
  862. uint32_t timestamp_result_count = 0;
  863. uint64_t index = 0;
  864. };
  865. uint32_t max_timestamp_query_elements = 0;
  866. TightLocalVector<Frame> frames; // Frames available, for main device they are cycled (usually 3), for local devices only 1.
  867. int frame = 0; // Current frame.
  868. int frame_count = 0; // Total amount of frames.
  869. uint64_t frames_drawn = 0;
  870. RID local_device;
  871. bool local_device_processing = false;
  872. void _free_pending_resources(int p_frame);
  873. VmaAllocator allocator = nullptr;
  874. HashMap<uint32_t, VmaPool> small_allocs_pools;
  875. VmaPool _find_or_create_small_allocs_pool(uint32_t p_mem_type_index);
  876. VulkanContext *context = nullptr;
  877. uint64_t image_memory = 0;
  878. uint64_t buffer_memory = 0;
  879. void _free_internal(RID p_id);
  880. void _flush(bool p_current_frame);
  881. bool screen_prepared = false;
  882. template <class T>
  883. void _free_rids(T &p_owner, const char *p_type);
  884. void _finalize_command_bufers();
  885. void _begin_frame();
  886. #ifdef DEV_ENABLED
  887. HashMap<RID, String> resource_names;
  888. #endif
  889. VkSampleCountFlagBits _ensure_supported_sample_count(TextureSamples p_requested_sample_count) const;
  890. public:
  891. virtual RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>());
  892. virtual RID texture_create_shared(const TextureView &p_view, RID p_with_texture);
  893. 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);
  894. 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);
  895. 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);
  896. virtual Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer);
  897. virtual bool texture_is_format_supported_for_usage(DataFormat p_format, BitField<RenderingDevice::TextureUsageBits> p_usage) const;
  898. virtual bool texture_is_shared(RID p_texture);
  899. virtual bool texture_is_valid(RID p_texture);
  900. virtual Size2i texture_size(RID p_texture);
  901. 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);
  902. 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);
  903. virtual Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  904. /*********************/
  905. /**** FRAMEBUFFER ****/
  906. /*********************/
  907. virtual FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count = 1);
  908. virtual FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1);
  909. virtual FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1);
  910. virtual TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0);
  911. virtual RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  912. 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);
  913. virtual RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID);
  914. virtual bool framebuffer_is_valid(RID p_framebuffer) const;
  915. virtual void framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata);
  916. virtual FramebufferFormatID framebuffer_get_format(RID p_framebuffer);
  917. /*****************/
  918. /**** SAMPLER ****/
  919. /*****************/
  920. virtual RID sampler_create(const SamplerState &p_state);
  921. /**********************/
  922. /**** VERTEX ARRAY ****/
  923. /**********************/
  924. 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);
  925. // 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.
  926. virtual VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats);
  927. 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>());
  928. 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);
  929. virtual RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count);
  930. /****************/
  931. /**** SHADER ****/
  932. /****************/
  933. virtual String shader_get_binary_cache_key() const;
  934. virtual Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
  935. virtual RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary);
  936. virtual uint32_t shader_get_vertex_input_attribute_mask(RID p_shader);
  937. /*****************/
  938. /**** UNIFORM ****/
  939. /*****************/
  940. virtual RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  941. virtual RID storage_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), BitField<StorageBufferUsage> p_usage = 0);
  942. virtual RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  943. virtual RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set);
  944. virtual bool uniform_set_is_valid(RID p_uniform_set);
  945. virtual void uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata);
  946. 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.
  947. virtual Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  948. virtual Vector<uint8_t> buffer_get_data(RID p_buffer, uint32_t p_offset = 0, uint32_t p_size = 0);
  949. /*************************/
  950. /**** RENDER PIPELINE ****/
  951. /*************************/
  952. 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>());
  953. virtual bool render_pipeline_is_valid(RID p_pipeline);
  954. /**************************/
  955. /**** COMPUTE PIPELINE ****/
  956. /**************************/
  957. virtual RID compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  958. virtual bool compute_pipeline_is_valid(RID p_pipeline);
  959. /****************/
  960. /**** SCREEN ****/
  961. /****************/
  962. virtual int screen_get_width(DisplayServer::WindowID p_screen = 0) const;
  963. virtual int screen_get_height(DisplayServer::WindowID p_screen = 0) const;
  964. virtual FramebufferFormatID screen_get_framebuffer_format() const;
  965. /********************/
  966. /**** DRAW LISTS ****/
  967. /********************/
  968. virtual DrawListID draw_list_begin_for_screen(DisplayServer::WindowID p_screen = 0, const Color &p_clear_color = Color());
  969. 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>());
  970. 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>());
  971. virtual void draw_list_set_blend_constants(DrawListID p_list, const Color &p_color);
  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(BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  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(BitField<BarrierMask> p_post_barrier = BARRIER_MASK_ALL_BARRIERS);
  997. virtual void barrier(BitField<BarrierMask> p_from = BARRIER_MASK_ALL_BARRIERS, BitField<BarrierMask> p_to = BARRIER_MASK_ALL_BARRIERS);
  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