rendering_device_vulkan.h 49 KB

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