rendering_device_vulkan.h 45 KB

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