rendering_device_vulkan.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /*************************************************************************/
  2. /* rendering_device_vulkan.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RENDERING_DEVICE_VULKAN_H
  31. #define RENDERING_DEVICE_VULKAN_H
  32. #include "core/os/thread_safe.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/oa_hash_map.h"
  35. #include "core/templates/rid_owner.h"
  36. #include "servers/rendering/rendering_device.h"
  37. #ifdef DEBUG_ENABLED
  38. #ifndef _DEBUG
  39. #define _DEBUG
  40. #endif
  41. #endif
  42. #include "vk_mem_alloc.h"
  43. #ifdef USE_VOLK
  44. #include <volk.h>
  45. #else
  46. #include <vulkan/vulkan.h>
  47. #endif
  48. class VulkanContext;
  49. class RenderingDeviceVulkan : public RenderingDevice {
  50. _THREAD_SAFE_CLASS_
  51. // Miscellaneous tables that map
  52. // our enums to enums used
  53. // by vulkan.
  54. VkPhysicalDeviceLimits limits;
  55. static const VkFormat vulkan_formats[DATA_FORMAT_MAX];
  56. static const char *named_formats[DATA_FORMAT_MAX];
  57. static const VkCompareOp compare_operators[COMPARE_OP_MAX];
  58. static const VkStencilOp stencil_operations[STENCIL_OP_MAX];
  59. static const VkSampleCountFlagBits rasterization_sample_count[TEXTURE_SAMPLES_MAX];
  60. static const VkLogicOp logic_operations[RenderingDevice::LOGIC_OP_MAX];
  61. static const VkBlendFactor blend_factors[RenderingDevice::BLEND_FACTOR_MAX];
  62. static const VkBlendOp blend_operations[RenderingDevice::BLEND_OP_MAX];
  63. static const VkSamplerAddressMode address_modes[SAMPLER_REPEAT_MODE_MAX];
  64. static const VkBorderColor sampler_border_colors[SAMPLER_BORDER_COLOR_MAX];
  65. static const VkImageType vulkan_image_type[TEXTURE_TYPE_MAX];
  66. // Functions used for format
  67. // validation, and ensures the
  68. // user passes valid data.
  69. static int get_format_vertex_size(DataFormat p_format);
  70. static uint32_t get_image_format_pixel_size(DataFormat p_format);
  71. static void get_compressed_image_format_block_dimensions(DataFormat p_format, uint32_t &r_w, uint32_t &r_h);
  72. uint32_t get_compressed_image_format_block_byte_size(DataFormat p_format);
  73. static uint32_t get_compressed_image_format_pixel_rshift(DataFormat p_format);
  74. static uint32_t get_image_format_required_size(DataFormat p_format, uint32_t p_width, uint32_t p_height, uint32_t p_depth, uint32_t p_mipmaps, uint32_t *r_blockw = nullptr, uint32_t *r_blockh = nullptr, uint32_t *r_depth = nullptr);
  75. static uint32_t get_image_required_mipmaps(uint32_t p_width, uint32_t p_height, uint32_t p_depth);
  76. static bool format_has_stencil(DataFormat p_format);
  77. /***************************/
  78. /**** ID INFRASTRUCTURE ****/
  79. /***************************/
  80. enum IDType {
  81. ID_TYPE_FRAMEBUFFER_FORMAT,
  82. ID_TYPE_VERTEX_FORMAT,
  83. ID_TYPE_DRAW_LIST,
  84. ID_TYPE_SPLIT_DRAW_LIST,
  85. ID_TYPE_COMPUTE_LIST,
  86. ID_TYPE_MAX,
  87. ID_BASE_SHIFT = 58 //5 bits for ID types
  88. };
  89. VkDevice device = VK_NULL_HANDLE;
  90. HashMap<RID, RBSet<RID>> dependency_map; //IDs to IDs that depend on it
  91. HashMap<RID, RBSet<RID>> reverse_dependency_map; //same as above, but in reverse
  92. void _add_dependency(RID p_id, RID p_depends_on);
  93. void _free_dependencies(RID p_id);
  94. /*****************/
  95. /**** TEXTURE ****/
  96. /*****************/
  97. // In Vulkan, the concept of textures does not exist,
  98. // instead there is the image (the memory pretty much,
  99. // the view (how the memory is interpreted) and the
  100. // sampler (how it's sampled from the shader).
  101. //
  102. // Texture here includes the first two stages, but
  103. // It's possible to create textures sharing the image
  104. // but with different views. The main use case for this
  105. // is textures that can be read as both SRGB/Linear,
  106. // or slices of a texture (a mipmap, a layer, a 3D slice)
  107. // for a framebuffer to render into it.
  108. struct Texture {
  109. VkImage image = VK_NULL_HANDLE;
  110. VmaAllocation allocation = nullptr;
  111. VmaAllocationInfo allocation_info;
  112. VkImageView view = VK_NULL_HANDLE;
  113. TextureType type;
  114. DataFormat format;
  115. TextureSamples samples;
  116. uint32_t width = 0;
  117. uint32_t height = 0;
  118. uint32_t depth = 0;
  119. uint32_t layers = 0;
  120. uint32_t mipmaps = 0;
  121. uint32_t usage_flags = 0;
  122. uint32_t base_mipmap = 0;
  123. uint32_t base_layer = 0;
  124. Vector<DataFormat> allowed_shared_formats;
  125. VkImageLayout layout;
  126. uint64_t used_in_frame = 0;
  127. bool used_in_transfer = false;
  128. bool used_in_raster = false;
  129. bool used_in_compute = false;
  130. uint32_t read_aspect_mask = 0;
  131. uint32_t barrier_aspect_mask = 0;
  132. bool bound = false; //bound to framebffer
  133. RID owner;
  134. };
  135. RID_Owner<Texture, true> texture_owner;
  136. uint32_t texture_upload_region_size_px = 0;
  137. Vector<uint8_t> _texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer, bool p_2d = false);
  138. Error _texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, uint32_t p_post_barrier, bool p_use_setup_queue);
  139. /*****************/
  140. /**** SAMPLER ****/
  141. /*****************/
  142. RID_Owner<VkSampler> sampler_owner;
  143. /***************************/
  144. /**** BUFFER MANAGEMENT ****/
  145. /***************************/
  146. // These are temporary buffers on CPU memory that hold
  147. // the information until the CPU fetches it and places it
  148. // either on GPU buffers, or images (textures). It ensures
  149. // updates are properly synchronized with whatever the
  150. // GPU is doing.
  151. //
  152. // The logic here is as follows, only 3 of these
  153. // blocks are created at the beginning (one per frame)
  154. // they can each belong to a frame (assigned to current when
  155. // used) and they can only be reused after the same frame is
  156. // recycled.
  157. //
  158. // When CPU requires to allocate more than what is available,
  159. // more of these buffers are created. If a limit is reached,
  160. // then a fence will ensure will wait for blocks allocated
  161. // in previous frames are processed. If that fails, then
  162. // another fence will ensure everything pending for the current
  163. // frame is processed (effectively stalling).
  164. //
  165. // See the comments in the code to understand better how it works.
  166. struct StagingBufferBlock {
  167. VkBuffer buffer = VK_NULL_HANDLE;
  168. VmaAllocation allocation = nullptr;
  169. uint64_t frame_used = 0;
  170. uint32_t fill_amount = 0;
  171. };
  172. Vector<StagingBufferBlock> staging_buffer_blocks;
  173. int staging_buffer_current = 0;
  174. uint32_t staging_buffer_block_size = 0;
  175. uint64_t staging_buffer_max_size = 0;
  176. bool staging_buffer_used = false;
  177. Error _staging_buffer_allocate(uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, bool p_can_segment = true, bool p_on_draw_command_buffer = false);
  178. Error _insert_staging_block();
  179. struct Buffer {
  180. uint32_t size = 0;
  181. uint32_t usage = 0;
  182. VkBuffer buffer = VK_NULL_HANDLE;
  183. VmaAllocation allocation = nullptr;
  184. VkDescriptorBufferInfo buffer_info; //used for binding
  185. Buffer() {
  186. }
  187. };
  188. Error _buffer_allocate(Buffer *p_buffer, uint32_t p_size, uint32_t p_usage, VmaMemoryUsage p_mem_usage, VmaAllocationCreateFlags p_mem_flags);
  189. Error _buffer_free(Buffer *p_buffer);
  190. Error _buffer_update(Buffer *p_buffer, size_t p_offset, const uint8_t *p_data, size_t p_data_size, bool p_use_draw_command_buffer = false, uint32_t p_required_align = 32);
  191. void _full_barrier(bool p_sync_with_draw);
  192. void _memory_barrier(VkPipelineStageFlags p_src_stage_mask, VkPipelineStageFlags p_dst_stage_mask, VkAccessFlags p_src_access, VkAccessFlags p_dst_sccess, bool p_sync_with_draw);
  193. void _buffer_memory_barrier(VkBuffer buffer, uint64_t p_from, uint64_t p_size, VkPipelineStageFlags p_src_stage_mask, VkPipelineStageFlags p_dst_stage_mask, VkAccessFlags p_src_access, VkAccessFlags p_dst_sccess, bool p_sync_with_draw);
  194. /*********************/
  195. /**** FRAMEBUFFER ****/
  196. /*********************/
  197. // In Vulkan, framebuffers work similar to how they
  198. // do in OpenGL, with the exception that
  199. // the "format" (vkRenderPass) is not dynamic
  200. // and must be more or less the same as the one
  201. // used for the render pipelines.
  202. struct FramebufferFormatKey {
  203. Vector<AttachmentFormat> attachments;
  204. Vector<FramebufferPass> passes;
  205. uint32_t view_count = 1;
  206. bool operator<(const FramebufferFormatKey &p_key) const {
  207. if (view_count != p_key.view_count) {
  208. return view_count < p_key.view_count;
  209. }
  210. uint32_t pass_size = passes.size();
  211. uint32_t key_pass_size = p_key.passes.size();
  212. if (pass_size != key_pass_size) {
  213. return pass_size < key_pass_size;
  214. }
  215. const FramebufferPass *pass_ptr = passes.ptr();
  216. const FramebufferPass *key_pass_ptr = p_key.passes.ptr();
  217. for (uint32_t i = 0; i < pass_size; i++) {
  218. { //compare color attachments
  219. uint32_t attachment_size = pass_ptr[i].color_attachments.size();
  220. uint32_t key_attachment_size = key_pass_ptr[i].color_attachments.size();
  221. if (attachment_size != key_attachment_size) {
  222. return attachment_size < key_attachment_size;
  223. }
  224. const int32_t *pass_attachment_ptr = pass_ptr[i].color_attachments.ptr();
  225. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].color_attachments.ptr();
  226. for (uint32_t j = 0; j < attachment_size; j++) {
  227. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  228. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  229. }
  230. }
  231. }
  232. { //compare input attachments
  233. uint32_t attachment_size = pass_ptr[i].input_attachments.size();
  234. uint32_t key_attachment_size = key_pass_ptr[i].input_attachments.size();
  235. if (attachment_size != key_attachment_size) {
  236. return attachment_size < key_attachment_size;
  237. }
  238. const int32_t *pass_attachment_ptr = pass_ptr[i].input_attachments.ptr();
  239. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].input_attachments.ptr();
  240. for (uint32_t j = 0; j < attachment_size; j++) {
  241. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  242. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  243. }
  244. }
  245. }
  246. { //compare resolve attachments
  247. uint32_t attachment_size = pass_ptr[i].resolve_attachments.size();
  248. uint32_t key_attachment_size = key_pass_ptr[i].resolve_attachments.size();
  249. if (attachment_size != key_attachment_size) {
  250. return attachment_size < key_attachment_size;
  251. }
  252. const int32_t *pass_attachment_ptr = pass_ptr[i].resolve_attachments.ptr();
  253. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].resolve_attachments.ptr();
  254. for (uint32_t j = 0; j < attachment_size; j++) {
  255. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  256. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  257. }
  258. }
  259. }
  260. { //compare preserve attachments
  261. uint32_t attachment_size = pass_ptr[i].preserve_attachments.size();
  262. uint32_t key_attachment_size = key_pass_ptr[i].preserve_attachments.size();
  263. if (attachment_size != key_attachment_size) {
  264. return attachment_size < key_attachment_size;
  265. }
  266. const int32_t *pass_attachment_ptr = pass_ptr[i].preserve_attachments.ptr();
  267. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].preserve_attachments.ptr();
  268. for (uint32_t j = 0; j < attachment_size; j++) {
  269. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  270. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  271. }
  272. }
  273. }
  274. if (pass_ptr[i].depth_attachment != key_pass_ptr[i].depth_attachment) {
  275. return pass_ptr[i].depth_attachment < key_pass_ptr[i].depth_attachment;
  276. }
  277. }
  278. int as = attachments.size();
  279. int bs = p_key.attachments.size();
  280. if (as != bs) {
  281. return as < bs;
  282. }
  283. const AttachmentFormat *af_a = attachments.ptr();
  284. const AttachmentFormat *af_b = p_key.attachments.ptr();
  285. for (int i = 0; i < as; i++) {
  286. const AttachmentFormat &a = af_a[i];
  287. const AttachmentFormat &b = af_b[i];
  288. if (a.format != b.format) {
  289. return a.format < b.format;
  290. }
  291. if (a.samples != b.samples) {
  292. return a.samples < b.samples;
  293. }
  294. if (a.usage_flags != b.usage_flags) {
  295. return a.usage_flags < b.usage_flags;
  296. }
  297. }
  298. return false; //equal
  299. }
  300. };
  301. VkRenderPass _render_pass_create(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, uint32_t p_view_count = 1, Vector<TextureSamples> *r_samples = nullptr);
  302. // This is a cache and it's never freed, it ensures
  303. // IDs for a given format are always unique.
  304. RBMap<FramebufferFormatKey, FramebufferFormatID> framebuffer_format_cache;
  305. struct FramebufferFormat {
  306. const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E;
  307. VkRenderPass render_pass = VK_NULL_HANDLE; //here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec)
  308. Vector<TextureSamples> pass_samples;
  309. uint32_t view_count = 1; // number of views
  310. };
  311. HashMap<FramebufferFormatID, FramebufferFormat> framebuffer_formats;
  312. struct Framebuffer {
  313. FramebufferFormatID format_id = 0;
  314. struct VersionKey {
  315. InitialAction initial_color_action;
  316. FinalAction final_color_action;
  317. InitialAction initial_depth_action;
  318. FinalAction final_depth_action;
  319. uint32_t view_count;
  320. bool operator<(const VersionKey &p_key) const {
  321. if (initial_color_action == p_key.initial_color_action) {
  322. if (final_color_action == p_key.final_color_action) {
  323. if (initial_depth_action == p_key.initial_depth_action) {
  324. if (final_depth_action == p_key.final_depth_action) {
  325. return view_count < p_key.view_count;
  326. } else {
  327. return final_depth_action < p_key.final_depth_action;
  328. }
  329. } else {
  330. return initial_depth_action < p_key.initial_depth_action;
  331. }
  332. } else {
  333. return final_color_action < p_key.final_color_action;
  334. }
  335. } else {
  336. return initial_color_action < p_key.initial_color_action;
  337. }
  338. }
  339. };
  340. uint32_t storage_mask = 0;
  341. Vector<RID> texture_ids;
  342. 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, true> 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, true> 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_djb2_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_djb2_one_32(vd.location, h);
  403. h = hash_djb2_one_32(vd.offset, h);
  404. h = hash_djb2_one_32(vd.format, h);
  405. h = hash_djb2_one_32(vd.stride, h);
  406. h = hash_djb2_one_32(vd.frequency, h);
  407. }
  408. return 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, true> 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, true> 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, true> 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. enum {
  466. MAX_UNIFORM_SETS = 16
  467. };
  468. struct UniformInfo {
  469. UniformType type = UniformType::UNIFORM_TYPE_MAX;
  470. int binding = 0;
  471. uint32_t stages = 0;
  472. int length = 0; //size of arrays (in total elements), or ubos (in bytes * total elements)
  473. bool operator!=(const UniformInfo &p_info) const {
  474. return (binding != p_info.binding || type != p_info.type || stages != p_info.stages || length != p_info.length);
  475. }
  476. bool operator<(const UniformInfo &p_info) const {
  477. if (binding != p_info.binding) {
  478. return binding < p_info.binding;
  479. }
  480. if (type != p_info.type) {
  481. return type < p_info.type;
  482. }
  483. if (stages != p_info.stages) {
  484. return stages < p_info.stages;
  485. }
  486. return length < p_info.length;
  487. }
  488. };
  489. struct UniformSetFormat {
  490. Vector<UniformInfo> uniform_info;
  491. bool operator<(const UniformSetFormat &p_format) const {
  492. uint32_t size = uniform_info.size();
  493. uint32_t psize = p_format.uniform_info.size();
  494. if (size != psize) {
  495. return size < psize;
  496. }
  497. const UniformInfo *infoptr = uniform_info.ptr();
  498. const UniformInfo *pinfoptr = p_format.uniform_info.ptr();
  499. for (uint32_t i = 0; i < size; i++) {
  500. if (infoptr[i] != pinfoptr[i]) {
  501. return infoptr[i] < pinfoptr[i];
  502. }
  503. }
  504. return false;
  505. }
  506. };
  507. // Always grows, never shrinks, ensuring unique IDs, but we assume
  508. // the amount of formats will never be a problem, as the amount of shaders
  509. // in a game is limited.
  510. RBMap<UniformSetFormat, uint32_t> uniform_set_format_cache;
  511. // Shaders in Vulkan are just pretty much
  512. // precompiled blocks of SPIR-V bytecode. They
  513. // are most likely not really compiled to host
  514. // assembly until a pipeline is created.
  515. //
  516. // When supplying the shaders, this implementation
  517. // will use the reflection abilities of glslang to
  518. // understand and cache everything required to
  519. // create and use the descriptor sets (Vulkan's
  520. // biggest pain).
  521. //
  522. // Additionally, hashes are created for every set
  523. // to do quick validation and ensuring the user
  524. // does not submit something invalid.
  525. struct Shader {
  526. struct Set {
  527. Vector<UniformInfo> uniform_info;
  528. VkDescriptorSetLayout descriptor_set_layout = VK_NULL_HANDLE;
  529. };
  530. uint32_t vertex_input_mask = 0; //inputs used, this is mostly for validation
  531. uint32_t fragment_output_mask = 0;
  532. struct PushConstant {
  533. uint32_t push_constant_size = 0;
  534. uint32_t push_constants_vk_stage = 0;
  535. };
  536. PushConstant push_constant;
  537. uint32_t compute_local_size[3] = { 0, 0, 0 };
  538. struct SpecializationConstant {
  539. PipelineSpecializationConstant constant;
  540. uint32_t stage_flags = 0;
  541. };
  542. bool is_compute = false;
  543. int max_output = 0;
  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, true> 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, RBSet<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, true> uniform_buffer_owner;
  608. RID_Owner<Buffer, true> 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, true> 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. UniformSetInvalidatedCallback invalidated_callback = nullptr;
  638. void *invalidated_callback_userdata = nullptr;
  639. };
  640. RID_Owner<UniformSet, true> 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 = 0;
  674. };
  675. RID_Owner<RenderPipeline, true> render_pipeline_owner;
  676. struct ComputePipeline {
  677. RID shader;
  678. Vector<uint32_t> set_formats;
  679. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; // not owned, needed for push constants
  680. VkPipeline pipeline = VK_NULL_HANDLE;
  681. uint32_t push_constant_size = 0;
  682. uint32_t push_constant_stages = 0;
  683. uint32_t local_group_size[3] = { 0, 0, 0 };
  684. };
  685. RID_Owner<ComputePipeline, true> compute_pipeline_owner;
  686. /*******************/
  687. /**** DRAW LIST ****/
  688. /*******************/
  689. // Draw list contains both the command buffer
  690. // used for drawing as well as a LOT of
  691. // information used for validation. This
  692. // validation is cheap so most of it can
  693. // also run in release builds.
  694. // When using split command lists, this is
  695. // implemented internally using secondary command
  696. // buffers. As they can be created in threads,
  697. // each needs its own command pool.
  698. struct SplitDrawListAllocator {
  699. VkCommandPool command_pool = VK_NULL_HANDLE;
  700. Vector<VkCommandBuffer> command_buffers; //one for each frame
  701. };
  702. Vector<SplitDrawListAllocator> split_draw_list_allocators;
  703. struct DrawList {
  704. VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
  705. Rect2i viewport;
  706. bool viewport_set = false;
  707. struct SetState {
  708. uint32_t pipeline_expected_format = 0;
  709. uint32_t uniform_set_format = 0;
  710. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  711. RID uniform_set;
  712. bool bound = false;
  713. };
  714. struct State {
  715. SetState sets[MAX_UNIFORM_SETS];
  716. uint32_t set_count = 0;
  717. RID pipeline;
  718. RID pipeline_shader;
  719. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  720. RID vertex_array;
  721. RID index_array;
  722. uint32_t pipeline_push_constant_stages = 0;
  723. } state;
  724. #ifdef DEBUG_ENABLED
  725. struct Validation {
  726. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  727. // Actual render pass values.
  728. uint32_t dynamic_state = 0;
  729. VertexFormatID vertex_format = INVALID_ID;
  730. uint32_t vertex_array_size = 0;
  731. uint32_t vertex_max_instances_allowed = 0xFFFFFFFF;
  732. bool index_buffer_uses_restart_indices = false;
  733. uint32_t index_array_size = 0;
  734. uint32_t index_array_max_index = 0;
  735. uint32_t index_array_offset = 0;
  736. Vector<uint32_t> set_formats;
  737. Vector<bool> set_bound;
  738. Vector<RID> set_rids;
  739. // Last pipeline set values.
  740. bool pipeline_active = false;
  741. uint32_t pipeline_dynamic_state = 0;
  742. VertexFormatID pipeline_vertex_format = INVALID_ID;
  743. RID pipeline_shader;
  744. uint32_t invalid_set_from = 0;
  745. bool pipeline_uses_restart_indices = false;
  746. uint32_t pipeline_primitive_divisor = 0;
  747. uint32_t pipeline_primitive_minimum = 0;
  748. Vector<uint32_t> pipeline_set_formats;
  749. uint32_t pipeline_push_constant_size = 0;
  750. bool pipeline_push_constant_supplied = false;
  751. } validation;
  752. #else
  753. struct Validation {
  754. uint32_t vertex_array_size = 0;
  755. uint32_t index_array_size = 0;
  756. uint32_t index_array_offset;
  757. } validation;
  758. #endif
  759. };
  760. DrawList *draw_list = nullptr; // One for regular draw lists, multiple for split.
  761. uint32_t draw_list_subpass_count = 0;
  762. uint32_t draw_list_count = 0;
  763. VkRenderPass draw_list_render_pass = VK_NULL_HANDLE;
  764. VkFramebuffer draw_list_vkframebuffer = VK_NULL_HANDLE;
  765. #ifdef DEBUG_ENABLED
  766. FramebufferFormatID draw_list_framebuffer_format = INVALID_ID;
  767. #endif
  768. uint32_t draw_list_current_subpass = 0;
  769. bool draw_list_split = false;
  770. Vector<RID> draw_list_bound_textures;
  771. Vector<RID> draw_list_storage_textures;
  772. bool draw_list_unbind_color_textures = false;
  773. bool draw_list_unbind_depth_textures = false;
  774. void _draw_list_insert_clear_region(DrawList *draw_list, Framebuffer *framebuffer, Point2i viewport_offset, Point2i viewport_size, bool p_clear_color, const Vector<Color> &p_clear_colors, bool p_clear_depth, float p_depth, uint32_t p_stencil);
  775. 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);
  776. 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);
  777. _FORCE_INLINE_ DrawList *_get_draw_list_ptr(DrawListID p_id);
  778. Buffer *_get_buffer_from_owner(RID p_buffer, VkPipelineStageFlags &dst_stage_mask, VkAccessFlags &dst_access, uint32_t p_post_barrier);
  779. Error _draw_list_allocate(const Rect2i &p_viewport, uint32_t p_splits, uint32_t p_subpass);
  780. void _draw_list_free(Rect2i *r_last_viewport = nullptr);
  781. /**********************/
  782. /**** COMPUTE LIST ****/
  783. /**********************/
  784. struct ComputeList {
  785. VkCommandBuffer command_buffer = VK_NULL_HANDLE; // If persistent, this is owned, otherwise it's shared with the ringbuffer.
  786. struct SetState {
  787. uint32_t pipeline_expected_format = 0;
  788. uint32_t uniform_set_format = 0;
  789. VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  790. RID uniform_set;
  791. bool bound = false;
  792. };
  793. struct State {
  794. RBSet<Texture *> textures_to_sampled_layout;
  795. SetState sets[MAX_UNIFORM_SETS];
  796. uint32_t set_count = 0;
  797. RID pipeline;
  798. RID pipeline_shader;
  799. uint32_t local_group_size[3] = { 0, 0, 0 };
  800. VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
  801. uint32_t pipeline_push_constant_stages = 0;
  802. bool allow_draw_overlap;
  803. } state;
  804. #ifdef DEBUG_ENABLED
  805. struct Validation {
  806. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  807. Vector<uint32_t> set_formats;
  808. Vector<bool> set_bound;
  809. Vector<RID> set_rids;
  810. // Last pipeline set values.
  811. bool pipeline_active = false;
  812. RID pipeline_shader;
  813. uint32_t invalid_set_from = 0;
  814. Vector<uint32_t> pipeline_set_formats;
  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. String *timestamp_names = nullptr;
  857. uint64_t *timestamp_cpu_values = nullptr;
  858. uint32_t timestamp_count = 0;
  859. String *timestamp_result_names = nullptr;
  860. uint64_t *timestamp_cpu_result_values = nullptr;
  861. uint64_t *timestamp_result_values = nullptr;
  862. uint32_t timestamp_result_count = 0;
  863. uint64_t index = 0;
  864. };
  865. uint32_t max_timestamp_query_elements = 0;
  866. Frame *frames = nullptr; //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. public:
  887. virtual RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>());
  888. virtual RID texture_create_shared(const TextureView &p_view, RID p_with_texture);
  889. 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);
  890. 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);
  891. virtual Error texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL);
  892. virtual Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer);
  893. virtual bool texture_is_format_supported_for_usage(DataFormat p_format, uint32_t p_usage) const;
  894. virtual bool texture_is_shared(RID p_texture);
  895. virtual bool texture_is_valid(RID p_texture);
  896. virtual Size2i texture_size(RID p_texture);
  897. virtual Error texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, uint32_t p_post_barrier = BARRIER_MASK_ALL);
  898. virtual Error texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, uint32_t p_post_barrier = BARRIER_MASK_ALL);
  899. virtual Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture, uint32_t p_post_barrier = BARRIER_MASK_ALL);
  900. /*********************/
  901. /**** FRAMEBUFFER ****/
  902. /*********************/
  903. virtual FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count = 1);
  904. virtual FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1);
  905. virtual FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1);
  906. virtual TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0);
  907. virtual RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  908. virtual RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  909. virtual RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID);
  910. virtual FramebufferFormatID framebuffer_get_format(RID p_framebuffer);
  911. /*****************/
  912. /**** SAMPLER ****/
  913. /*****************/
  914. virtual RID sampler_create(const SamplerState &p_state);
  915. /**********************/
  916. /**** VERTEX ARRAY ****/
  917. /**********************/
  918. 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);
  919. // 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
  920. virtual VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats);
  921. virtual RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers);
  922. 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);
  923. virtual RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count);
  924. /****************/
  925. /**** SHADER ****/
  926. /****************/
  927. virtual String shader_get_binary_cache_key() const;
  928. virtual Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
  929. virtual RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary);
  930. virtual uint32_t shader_get_vertex_input_attribute_mask(RID p_shader);
  931. /*****************/
  932. /**** UNIFORM ****/
  933. /*****************/
  934. virtual RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  935. virtual RID storage_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), uint32_t p_usage = 0);
  936. virtual RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  937. virtual RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set);
  938. virtual bool uniform_set_is_valid(RID p_uniform_set);
  939. virtual void uniform_set_set_invalidation_callback(RID p_uniform_set, UniformSetInvalidatedCallback p_callback, void *p_userdata);
  940. virtual Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL); //works for any buffer
  941. virtual Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, uint32_t p_post_barrier = BARRIER_MASK_ALL);
  942. virtual Vector<uint8_t> buffer_get_data(RID p_buffer);
  943. /*************************/
  944. /**** RENDER PIPELINE ****/
  945. /*************************/
  946. virtual RID render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, int p_dynamic_state_flags = 0, uint32_t p_for_render_pass = 0, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  947. virtual bool render_pipeline_is_valid(RID p_pipeline);
  948. /**************************/
  949. /**** COMPUTE PIPELINE ****/
  950. /**************************/
  951. virtual RID compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  952. virtual bool compute_pipeline_is_valid(RID p_pipeline);
  953. /****************/
  954. /**** SCREEN ****/
  955. /****************/
  956. virtual int screen_get_width(DisplayServer::WindowID p_screen = 0) const;
  957. virtual int screen_get_height(DisplayServer::WindowID p_screen = 0) const;
  958. virtual FramebufferFormatID screen_get_framebuffer_format() const;
  959. /********************/
  960. /**** DRAW LISTS ****/
  961. /********************/
  962. virtual DrawListID draw_list_begin_for_screen(DisplayServer::WindowID p_screen = 0, const Color &p_clear_color = Color());
  963. 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>());
  964. 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>());
  965. virtual void draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline);
  966. virtual void draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index);
  967. virtual void draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array);
  968. virtual void draw_list_bind_index_array(DrawListID p_list, RID p_index_array);
  969. virtual void draw_list_set_line_width(DrawListID p_list, float p_width);
  970. virtual void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size);
  971. virtual void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0);
  972. virtual void draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect);
  973. virtual void draw_list_disable_scissor(DrawListID p_list);
  974. virtual uint32_t draw_list_get_current_pass();
  975. virtual DrawListID draw_list_switch_to_next_pass();
  976. virtual Error draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids);
  977. virtual void draw_list_end(uint32_t p_post_barrier = BARRIER_MASK_ALL);
  978. /***********************/
  979. /**** COMPUTE LISTS ****/
  980. /***********************/
  981. virtual ComputeListID compute_list_begin(bool p_allow_draw_overlap = false);
  982. virtual void compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline);
  983. virtual void compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index);
  984. virtual void compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size);
  985. virtual void compute_list_add_barrier(ComputeListID p_list);
  986. virtual void compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
  987. virtual void compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads);
  988. virtual void compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset);
  989. virtual void compute_list_end(uint32_t p_post_barrier = BARRIER_MASK_ALL);
  990. virtual void barrier(uint32_t p_from = BARRIER_MASK_ALL, uint32_t p_to = BARRIER_MASK_ALL);
  991. virtual void full_barrier();
  992. /**************/
  993. /**** FREE ****/
  994. /**************/
  995. virtual void free(RID p_id);
  996. /****************/
  997. /**** Timing ****/
  998. /****************/
  999. virtual void capture_timestamp(const String &p_name);
  1000. virtual uint32_t get_captured_timestamps_count() const;
  1001. virtual uint64_t get_captured_timestamps_frame() const;
  1002. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
  1003. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
  1004. virtual String get_captured_timestamp_name(uint32_t p_index) const;
  1005. /****************/
  1006. /**** Limits ****/
  1007. /****************/
  1008. virtual uint64_t limit_get(Limit p_limit);
  1009. virtual void prepare_screen_for_drawing();
  1010. void initialize(VulkanContext *p_context, bool p_local_device = false);
  1011. void finalize();
  1012. virtual void swap_buffers(); //for main device
  1013. virtual void submit(); //for local device
  1014. virtual void sync(); //for local device
  1015. virtual uint32_t get_frame_delay() const;
  1016. virtual RenderingDevice *create_local_device();
  1017. virtual uint64_t get_memory_usage(MemoryType p_type) const;
  1018. virtual void set_resource_name(RID p_id, const String p_name);
  1019. virtual void draw_command_begin_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
  1020. virtual void draw_command_insert_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1));
  1021. virtual void draw_command_end_label();
  1022. virtual String get_device_vendor_name() const;
  1023. virtual String get_device_name() const;
  1024. virtual RenderingDevice::DeviceType get_device_type() const;
  1025. virtual String get_device_api_version() const;
  1026. virtual String get_device_pipeline_cache_uuid() const;
  1027. virtual uint64_t get_driver_resource(DriverResource p_resource, RID p_rid = RID(), uint64_t p_index = 0);
  1028. RenderingDeviceVulkan();
  1029. ~RenderingDeviceVulkan();
  1030. };
  1031. #endif // RENDERING_DEVICE_VULKAN_H