rendering_device.h 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /**************************************************************************/
  2. /* rendering_device.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef RENDERING_DEVICE_H
  31. #define RENDERING_DEVICE_H
  32. #include "core/object/class_db.h"
  33. #include "core/object/worker_thread_pool.h"
  34. #include "core/os/condition_variable.h"
  35. #include "core/os/thread_safe.h"
  36. #include "core/templates/local_vector.h"
  37. #include "core/templates/oa_hash_map.h"
  38. #include "core/templates/rid_owner.h"
  39. #include "core/variant/typed_array.h"
  40. #include "servers/display_server.h"
  41. #include "servers/rendering/rendering_device_commons.h"
  42. #include "servers/rendering/rendering_device_driver.h"
  43. #include "servers/rendering/rendering_device_graph.h"
  44. class RDTextureFormat;
  45. class RDTextureView;
  46. class RDAttachmentFormat;
  47. class RDSamplerState;
  48. class RDVertexAttribute;
  49. class RDShaderSource;
  50. class RDShaderSPIRV;
  51. class RDUniform;
  52. class RDPipelineRasterizationState;
  53. class RDPipelineMultisampleState;
  54. class RDPipelineDepthStencilState;
  55. class RDPipelineColorBlendState;
  56. class RDFramebufferPass;
  57. class RDPipelineSpecializationConstant;
  58. class RenderingDevice : public RenderingDeviceCommons {
  59. GDCLASS(RenderingDevice, Object)
  60. _THREAD_SAFE_CLASS_
  61. private:
  62. Thread::ID render_thread_id;
  63. public:
  64. enum ShaderLanguage {
  65. SHADER_LANGUAGE_GLSL,
  66. SHADER_LANGUAGE_HLSL
  67. };
  68. typedef int64_t DrawListID;
  69. typedef int64_t ComputeListID;
  70. typedef String (*ShaderSPIRVGetCacheKeyFunction)(const RenderingDevice *p_render_device);
  71. typedef Vector<uint8_t> (*ShaderCompileToSPIRVFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, const RenderingDevice *p_render_device);
  72. typedef Vector<uint8_t> (*ShaderCacheFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language);
  73. typedef void (*InvalidationCallback)(void *);
  74. private:
  75. static ShaderCompileToSPIRVFunction compile_to_spirv_function;
  76. static ShaderCacheFunction cache_function;
  77. static ShaderSPIRVGetCacheKeyFunction get_spirv_cache_key_function;
  78. static RenderingDevice *singleton;
  79. RenderingContextDriver *context = nullptr;
  80. RenderingDeviceDriver *driver = nullptr;
  81. RenderingContextDriver::Device device;
  82. bool local_device_processing = false;
  83. bool is_main_instance = false;
  84. protected:
  85. static void _bind_methods();
  86. #ifndef DISABLE_DEPRECATED
  87. RID _shader_create_from_bytecode_bind_compat_79606(const Vector<uint8_t> &p_shader_binary);
  88. static void _bind_compatibility_methods();
  89. #endif
  90. /***************************/
  91. /**** ID INFRASTRUCTURE ****/
  92. /***************************/
  93. public:
  94. //base numeric ID for all types
  95. enum {
  96. INVALID_FORMAT_ID = -1
  97. };
  98. enum IDType {
  99. ID_TYPE_FRAMEBUFFER_FORMAT,
  100. ID_TYPE_VERTEX_FORMAT,
  101. ID_TYPE_DRAW_LIST,
  102. ID_TYPE_COMPUTE_LIST = 4,
  103. ID_TYPE_MAX,
  104. ID_BASE_SHIFT = 58, // 5 bits for ID types.
  105. ID_MASK = (ID_BASE_SHIFT - 1),
  106. };
  107. private:
  108. HashMap<RID, HashSet<RID>> dependency_map; // IDs to IDs that depend on it.
  109. HashMap<RID, HashSet<RID>> reverse_dependency_map; // Same as above, but in reverse.
  110. void _add_dependency(RID p_id, RID p_depends_on);
  111. void _free_dependencies(RID p_id);
  112. private:
  113. /***************************/
  114. /**** BUFFER MANAGEMENT ****/
  115. /***************************/
  116. // These are temporary buffers on CPU memory that hold
  117. // the information until the CPU fetches it and places it
  118. // either on GPU buffers, or images (textures). It ensures
  119. // updates are properly synchronized with whatever the
  120. // GPU is doing.
  121. //
  122. // The logic here is as follows, only 3 of these
  123. // blocks are created at the beginning (one per frame)
  124. // they can each belong to a frame (assigned to current when
  125. // used) and they can only be reused after the same frame is
  126. // recycled.
  127. //
  128. // When CPU requires to allocate more than what is available,
  129. // more of these buffers are created. If a limit is reached,
  130. // then a fence will ensure will wait for blocks allocated
  131. // in previous frames are processed. If that fails, then
  132. // another fence will ensure everything pending for the current
  133. // frame is processed (effectively stalling).
  134. //
  135. // See the comments in the code to understand better how it works.
  136. struct StagingBufferBlock {
  137. RDD::BufferID driver_id;
  138. uint64_t frame_used = 0;
  139. uint32_t fill_amount = 0;
  140. };
  141. Vector<StagingBufferBlock> staging_buffer_blocks;
  142. int staging_buffer_current = 0;
  143. uint32_t staging_buffer_block_size = 0;
  144. uint64_t staging_buffer_max_size = 0;
  145. bool staging_buffer_used = false;
  146. enum StagingRequiredAction {
  147. STAGING_REQUIRED_ACTION_NONE,
  148. STAGING_REQUIRED_ACTION_FLUSH_AND_STALL_ALL,
  149. STAGING_REQUIRED_ACTION_STALL_PREVIOUS
  150. };
  151. Error _staging_buffer_allocate(uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, StagingRequiredAction &r_required_action, bool p_can_segment = true);
  152. void _staging_buffer_execute_required_action(StagingRequiredAction p_required_action);
  153. Error _insert_staging_block();
  154. struct Buffer {
  155. RDD::BufferID driver_id;
  156. uint32_t size = 0;
  157. BitField<RDD::BufferUsageBits> usage;
  158. RDG::ResourceTracker *draw_tracker = nullptr;
  159. int32_t transfer_worker_index = -1;
  160. uint64_t transfer_worker_operation = 0;
  161. };
  162. Buffer *_get_buffer_from_owner(RID p_buffer);
  163. Error _buffer_initialize(Buffer *p_buffer, const uint8_t *p_data, size_t p_data_size, uint32_t p_required_align = 32);
  164. void update_perf_report();
  165. uint32_t gpu_copy_count = 0;
  166. uint32_t copy_bytes_count = 0;
  167. String perf_report_text;
  168. RID_Owner<Buffer, true> uniform_buffer_owner;
  169. RID_Owner<Buffer, true> storage_buffer_owner;
  170. RID_Owner<Buffer, true> texture_buffer_owner;
  171. public:
  172. Error buffer_copy(RID p_src_buffer, RID p_dst_buffer, uint32_t p_src_offset, uint32_t p_dst_offset, uint32_t p_size);
  173. Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data);
  174. Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size);
  175. Vector<uint8_t> buffer_get_data(RID p_buffer, uint32_t p_offset = 0, uint32_t p_size = 0); // This causes stall, only use to retrieve large buffers for saving.
  176. /*****************/
  177. /**** TEXTURE ****/
  178. /*****************/
  179. // In modern APIs, the concept of textures may not exist;
  180. // instead there is the image (the memory pretty much,
  181. // the view (how the memory is interpreted) and the
  182. // sampler (how it's sampled from the shader).
  183. //
  184. // Texture here includes the first two stages, but
  185. // It's possible to create textures sharing the image
  186. // but with different views. The main use case for this
  187. // is textures that can be read as both SRGB/Linear,
  188. // or slices of a texture (a mipmap, a layer, a 3D slice)
  189. // for a framebuffer to render into it.
  190. struct Texture {
  191. struct SharedFallback {
  192. uint32_t revision = 1;
  193. RDD::TextureID texture;
  194. RDG::ResourceTracker *texture_tracker = nullptr;
  195. RDD::BufferID buffer;
  196. RDG::ResourceTracker *buffer_tracker = nullptr;
  197. bool raw_reinterpretation = false;
  198. };
  199. RDD::TextureID driver_id;
  200. TextureType type = TEXTURE_TYPE_MAX;
  201. DataFormat format = DATA_FORMAT_MAX;
  202. TextureSamples samples = TEXTURE_SAMPLES_MAX;
  203. TextureSliceType slice_type = TEXTURE_SLICE_MAX;
  204. Rect2i slice_rect;
  205. uint32_t width = 0;
  206. uint32_t height = 0;
  207. uint32_t depth = 0;
  208. uint32_t layers = 0;
  209. uint32_t mipmaps = 0;
  210. uint32_t usage_flags = 0;
  211. uint32_t base_mipmap = 0;
  212. uint32_t base_layer = 0;
  213. Vector<DataFormat> allowed_shared_formats;
  214. bool is_resolve_buffer = false;
  215. bool is_discardable = false;
  216. bool has_initial_data = false;
  217. BitField<RDD::TextureAspectBits> read_aspect_flags;
  218. BitField<RDD::TextureAspectBits> barrier_aspect_flags;
  219. bool bound = false; // Bound to framebuffer.
  220. RID owner;
  221. RDG::ResourceTracker *draw_tracker = nullptr;
  222. HashMap<Rect2i, RDG::ResourceTracker *> slice_trackers;
  223. SharedFallback *shared_fallback = nullptr;
  224. int32_t transfer_worker_index = -1;
  225. uint64_t transfer_worker_operation = 0;
  226. RDD::TextureSubresourceRange barrier_range() const {
  227. RDD::TextureSubresourceRange r;
  228. r.aspect = barrier_aspect_flags;
  229. r.base_mipmap = base_mipmap;
  230. r.mipmap_count = mipmaps;
  231. r.base_layer = base_layer;
  232. r.layer_count = layers;
  233. return r;
  234. }
  235. TextureFormat texture_format() const {
  236. TextureFormat tf;
  237. tf.format = format;
  238. tf.width = width;
  239. tf.height = height;
  240. tf.depth = depth;
  241. tf.array_layers = layers;
  242. tf.mipmaps = mipmaps;
  243. tf.texture_type = type;
  244. tf.samples = samples;
  245. tf.usage_bits = usage_flags;
  246. tf.shareable_formats = allowed_shared_formats;
  247. tf.is_resolve_buffer = is_resolve_buffer;
  248. tf.is_discardable = is_discardable;
  249. return tf;
  250. }
  251. };
  252. RID_Owner<Texture, true> texture_owner;
  253. uint32_t texture_upload_region_size_px = 0;
  254. Vector<uint8_t> _texture_get_data(Texture *tex, uint32_t p_layer, bool p_2d = false);
  255. uint32_t _texture_layer_count(Texture *p_texture) const;
  256. uint32_t _texture_alignment(Texture *p_texture) const;
  257. Error _texture_initialize(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data);
  258. void _texture_check_shared_fallback(Texture *p_texture);
  259. void _texture_update_shared_fallback(RID p_texture_rid, Texture *p_texture, bool p_for_writing);
  260. void _texture_free_shared_fallback(Texture *p_texture);
  261. void _texture_copy_shared(RID p_src_texture_rid, Texture *p_src_texture, RID p_dst_texture_rid, Texture *p_dst_texture);
  262. void _texture_create_reinterpret_buffer(Texture *p_texture);
  263. public:
  264. struct TextureView {
  265. DataFormat format_override = DATA_FORMAT_MAX; // // Means, use same as format.
  266. TextureSwizzle swizzle_r = TEXTURE_SWIZZLE_R;
  267. TextureSwizzle swizzle_g = TEXTURE_SWIZZLE_G;
  268. TextureSwizzle swizzle_b = TEXTURE_SWIZZLE_B;
  269. TextureSwizzle swizzle_a = TEXTURE_SWIZZLE_A;
  270. bool operator==(const TextureView &p_other) const {
  271. if (format_override != p_other.format_override) {
  272. return false;
  273. } else if (swizzle_r != p_other.swizzle_r) {
  274. return false;
  275. } else if (swizzle_g != p_other.swizzle_g) {
  276. return false;
  277. } else if (swizzle_b != p_other.swizzle_b) {
  278. return false;
  279. } else if (swizzle_a != p_other.swizzle_a) {
  280. return false;
  281. } else {
  282. return true;
  283. }
  284. }
  285. };
  286. RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>());
  287. RID texture_create_shared(const TextureView &p_view, RID p_with_texture);
  288. RID texture_create_from_extension(TextureType p_type, DataFormat p_format, TextureSamples p_samples, BitField<RenderingDevice::TextureUsageBits> p_usage, uint64_t p_image, uint64_t p_width, uint64_t p_height, uint64_t p_depth, uint64_t p_layers);
  289. RID texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps = 1, TextureSliceType p_slice_type = TEXTURE_SLICE_2D, uint32_t p_layers = 0);
  290. Error texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data);
  291. Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer); // CPU textures will return immediately, while GPU textures will most likely force a flush
  292. bool texture_is_format_supported_for_usage(DataFormat p_format, BitField<TextureUsageBits> p_usage) const;
  293. bool texture_is_shared(RID p_texture);
  294. bool texture_is_valid(RID p_texture);
  295. TextureFormat texture_get_format(RID p_texture);
  296. Size2i texture_size(RID p_texture);
  297. #ifndef DISABLE_DEPRECATED
  298. uint64_t texture_get_native_handle(RID p_texture);
  299. #endif
  300. 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);
  301. 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);
  302. Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture);
  303. void texture_set_discardable(RID p_texture, bool p_discardable);
  304. bool texture_is_discardable(RID p_texture);
  305. /*********************/
  306. /**** FRAMEBUFFER ****/
  307. /*********************/
  308. // In modern APIs, generally, framebuffers work similar to how they
  309. // do in OpenGL, with the exception that
  310. // the "format" (RDD::RenderPassID) is not dynamic
  311. // and must be more or less the same as the one
  312. // used for the render pipelines.
  313. struct AttachmentFormat {
  314. enum : uint32_t {
  315. UNUSED_ATTACHMENT = 0xFFFFFFFF
  316. };
  317. DataFormat format;
  318. TextureSamples samples;
  319. uint32_t usage_flags;
  320. AttachmentFormat() {
  321. format = DATA_FORMAT_R8G8B8A8_UNORM;
  322. samples = TEXTURE_SAMPLES_1;
  323. usage_flags = 0;
  324. }
  325. };
  326. struct FramebufferPass {
  327. Vector<int32_t> color_attachments;
  328. Vector<int32_t> input_attachments;
  329. Vector<int32_t> resolve_attachments;
  330. Vector<int32_t> preserve_attachments;
  331. int32_t depth_attachment = ATTACHMENT_UNUSED;
  332. int32_t vrs_attachment = ATTACHMENT_UNUSED; // density map for VRS, only used if supported
  333. };
  334. typedef int64_t FramebufferFormatID;
  335. private:
  336. struct FramebufferFormatKey {
  337. Vector<AttachmentFormat> attachments;
  338. Vector<FramebufferPass> passes;
  339. uint32_t view_count = 1;
  340. bool operator<(const FramebufferFormatKey &p_key) const {
  341. if (view_count != p_key.view_count) {
  342. return view_count < p_key.view_count;
  343. }
  344. uint32_t pass_size = passes.size();
  345. uint32_t key_pass_size = p_key.passes.size();
  346. if (pass_size != key_pass_size) {
  347. return pass_size < key_pass_size;
  348. }
  349. const FramebufferPass *pass_ptr = passes.ptr();
  350. const FramebufferPass *key_pass_ptr = p_key.passes.ptr();
  351. for (uint32_t i = 0; i < pass_size; i++) {
  352. { // Compare color attachments.
  353. uint32_t attachment_size = pass_ptr[i].color_attachments.size();
  354. uint32_t key_attachment_size = key_pass_ptr[i].color_attachments.size();
  355. if (attachment_size != key_attachment_size) {
  356. return attachment_size < key_attachment_size;
  357. }
  358. const int32_t *pass_attachment_ptr = pass_ptr[i].color_attachments.ptr();
  359. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].color_attachments.ptr();
  360. for (uint32_t j = 0; j < attachment_size; j++) {
  361. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  362. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  363. }
  364. }
  365. }
  366. { // Compare input attachments.
  367. uint32_t attachment_size = pass_ptr[i].input_attachments.size();
  368. uint32_t key_attachment_size = key_pass_ptr[i].input_attachments.size();
  369. if (attachment_size != key_attachment_size) {
  370. return attachment_size < key_attachment_size;
  371. }
  372. const int32_t *pass_attachment_ptr = pass_ptr[i].input_attachments.ptr();
  373. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].input_attachments.ptr();
  374. for (uint32_t j = 0; j < attachment_size; j++) {
  375. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  376. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  377. }
  378. }
  379. }
  380. { // Compare resolve attachments.
  381. uint32_t attachment_size = pass_ptr[i].resolve_attachments.size();
  382. uint32_t key_attachment_size = key_pass_ptr[i].resolve_attachments.size();
  383. if (attachment_size != key_attachment_size) {
  384. return attachment_size < key_attachment_size;
  385. }
  386. const int32_t *pass_attachment_ptr = pass_ptr[i].resolve_attachments.ptr();
  387. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].resolve_attachments.ptr();
  388. for (uint32_t j = 0; j < attachment_size; j++) {
  389. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  390. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  391. }
  392. }
  393. }
  394. { // Compare preserve attachments.
  395. uint32_t attachment_size = pass_ptr[i].preserve_attachments.size();
  396. uint32_t key_attachment_size = key_pass_ptr[i].preserve_attachments.size();
  397. if (attachment_size != key_attachment_size) {
  398. return attachment_size < key_attachment_size;
  399. }
  400. const int32_t *pass_attachment_ptr = pass_ptr[i].preserve_attachments.ptr();
  401. const int32_t *key_pass_attachment_ptr = key_pass_ptr[i].preserve_attachments.ptr();
  402. for (uint32_t j = 0; j < attachment_size; j++) {
  403. if (pass_attachment_ptr[j] != key_pass_attachment_ptr[j]) {
  404. return pass_attachment_ptr[j] < key_pass_attachment_ptr[j];
  405. }
  406. }
  407. }
  408. if (pass_ptr[i].depth_attachment != key_pass_ptr[i].depth_attachment) {
  409. return pass_ptr[i].depth_attachment < key_pass_ptr[i].depth_attachment;
  410. }
  411. }
  412. int as = attachments.size();
  413. int bs = p_key.attachments.size();
  414. if (as != bs) {
  415. return as < bs;
  416. }
  417. const AttachmentFormat *af_a = attachments.ptr();
  418. const AttachmentFormat *af_b = p_key.attachments.ptr();
  419. for (int i = 0; i < as; i++) {
  420. const AttachmentFormat &a = af_a[i];
  421. const AttachmentFormat &b = af_b[i];
  422. if (a.format != b.format) {
  423. return a.format < b.format;
  424. }
  425. if (a.samples != b.samples) {
  426. return a.samples < b.samples;
  427. }
  428. if (a.usage_flags != b.usage_flags) {
  429. return a.usage_flags < b.usage_flags;
  430. }
  431. }
  432. return false; // Equal.
  433. }
  434. };
  435. static RDD::RenderPassID _render_pass_create(RenderingDeviceDriver *p_driver, const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, VectorView<RDD::AttachmentLoadOp> p_load_ops, VectorView<RDD::AttachmentStoreOp> p_store_ops, uint32_t p_view_count = 1, Vector<TextureSamples> *r_samples = nullptr);
  436. static RDD::RenderPassID _render_pass_create_from_graph(RenderingDeviceDriver *p_driver, VectorView<RDD::AttachmentLoadOp> p_load_ops, VectorView<RDD::AttachmentStoreOp> p_store_ops, void *p_user_data);
  437. // This is a cache and it's never freed, it ensures
  438. // IDs for a given format are always unique.
  439. RBMap<FramebufferFormatKey, FramebufferFormatID> framebuffer_format_cache;
  440. struct FramebufferFormat {
  441. const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E;
  442. RDD::RenderPassID render_pass; // Here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec).
  443. Vector<TextureSamples> pass_samples;
  444. uint32_t view_count = 1; // Number of views.
  445. };
  446. HashMap<FramebufferFormatID, FramebufferFormat> framebuffer_formats;
  447. struct Framebuffer {
  448. RenderingDevice *rendering_device = nullptr;
  449. FramebufferFormatID format_id;
  450. uint32_t storage_mask = 0;
  451. Vector<RID> texture_ids;
  452. InvalidationCallback invalidated_callback = nullptr;
  453. void *invalidated_callback_userdata = nullptr;
  454. RDG::FramebufferCache *framebuffer_cache = nullptr;
  455. Size2 size;
  456. uint32_t view_count;
  457. };
  458. RID_Owner<Framebuffer, true> framebuffer_owner;
  459. public:
  460. // This ID is warranted to be unique for the same formats, does not need to be freed
  461. FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count = 1);
  462. FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1);
  463. FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1);
  464. TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0);
  465. RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  466. RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, const Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  467. RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID);
  468. bool framebuffer_is_valid(RID p_framebuffer) const;
  469. void framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata);
  470. FramebufferFormatID framebuffer_get_format(RID p_framebuffer);
  471. /*****************/
  472. /**** SAMPLER ****/
  473. /*****************/
  474. private:
  475. RID_Owner<RDD::SamplerID, true> sampler_owner;
  476. public:
  477. RID sampler_create(const SamplerState &p_state);
  478. bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_sampler_filter) const;
  479. /**********************/
  480. /**** VERTEX ARRAY ****/
  481. /**********************/
  482. typedef int64_t VertexFormatID;
  483. private:
  484. // Vertex buffers in Vulkan are similar to how
  485. // they work in OpenGL, except that instead of
  486. // an attribute index, there is a buffer binding
  487. // index (for binding the buffers in real-time)
  488. // and a location index (what is used in the shader).
  489. //
  490. // This mapping is done here internally, and it's not
  491. // exposed.
  492. RID_Owner<Buffer, true> vertex_buffer_owner;
  493. struct VertexDescriptionKey {
  494. Vector<VertexAttribute> vertex_formats;
  495. bool operator==(const VertexDescriptionKey &p_key) const {
  496. int vdc = vertex_formats.size();
  497. int vdck = p_key.vertex_formats.size();
  498. if (vdc != vdck) {
  499. return false;
  500. } else {
  501. const VertexAttribute *a_ptr = vertex_formats.ptr();
  502. const VertexAttribute *b_ptr = p_key.vertex_formats.ptr();
  503. for (int i = 0; i < vdc; i++) {
  504. const VertexAttribute &a = a_ptr[i];
  505. const VertexAttribute &b = b_ptr[i];
  506. if (a.location != b.location) {
  507. return false;
  508. }
  509. if (a.offset != b.offset) {
  510. return false;
  511. }
  512. if (a.format != b.format) {
  513. return false;
  514. }
  515. if (a.stride != b.stride) {
  516. return false;
  517. }
  518. if (a.frequency != b.frequency) {
  519. return false;
  520. }
  521. }
  522. return true; // They are equal.
  523. }
  524. }
  525. uint32_t hash() const {
  526. int vdc = vertex_formats.size();
  527. uint32_t h = hash_murmur3_one_32(vdc);
  528. const VertexAttribute *ptr = vertex_formats.ptr();
  529. for (int i = 0; i < vdc; i++) {
  530. const VertexAttribute &vd = ptr[i];
  531. h = hash_murmur3_one_32(vd.location, h);
  532. h = hash_murmur3_one_32(vd.offset, h);
  533. h = hash_murmur3_one_32(vd.format, h);
  534. h = hash_murmur3_one_32(vd.stride, h);
  535. h = hash_murmur3_one_32(vd.frequency, h);
  536. }
  537. return hash_fmix32(h);
  538. }
  539. };
  540. struct VertexDescriptionHash {
  541. static _FORCE_INLINE_ uint32_t hash(const VertexDescriptionKey &p_key) {
  542. return p_key.hash();
  543. }
  544. };
  545. // This is a cache and it's never freed, it ensures that
  546. // ID used for a specific format always remain the same.
  547. HashMap<VertexDescriptionKey, VertexFormatID, VertexDescriptionHash> vertex_format_cache;
  548. struct VertexDescriptionCache {
  549. Vector<VertexAttribute> vertex_formats;
  550. RDD::VertexFormatID driver_id;
  551. };
  552. HashMap<VertexFormatID, VertexDescriptionCache> vertex_formats;
  553. struct VertexArray {
  554. RID buffer;
  555. VertexFormatID description;
  556. int vertex_count = 0;
  557. uint32_t max_instances_allowed = 0;
  558. Vector<RDD::BufferID> buffers; // Not owned, just referenced.
  559. Vector<RDG::ResourceTracker *> draw_trackers; // Not owned, just referenced.
  560. Vector<uint64_t> offsets;
  561. Vector<int32_t> transfer_worker_indices;
  562. Vector<uint64_t> transfer_worker_operations;
  563. HashSet<RID> untracked_buffers;
  564. };
  565. RID_Owner<VertexArray, true> vertex_array_owner;
  566. struct IndexBuffer : public Buffer {
  567. uint32_t max_index = 0; // Used for validation.
  568. uint32_t index_count = 0;
  569. IndexBufferFormat format = INDEX_BUFFER_FORMAT_UINT16;
  570. bool supports_restart_indices = false;
  571. };
  572. RID_Owner<IndexBuffer, true> index_buffer_owner;
  573. struct IndexArray {
  574. uint32_t max_index = 0; // Remember the maximum index here too, for validation.
  575. RDD::BufferID driver_id; // Not owned, inherited from index buffer.
  576. RDG::ResourceTracker *draw_tracker = nullptr; // Not owned, inherited from index buffer.
  577. uint32_t offset = 0;
  578. uint32_t indices = 0;
  579. IndexBufferFormat format = INDEX_BUFFER_FORMAT_UINT16;
  580. bool supports_restart_indices = false;
  581. int32_t transfer_worker_index = -1;
  582. uint64_t transfer_worker_operation = 0;
  583. };
  584. RID_Owner<IndexArray, true> index_array_owner;
  585. public:
  586. RID vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_as_storage = false);
  587. // This ID is warranted to be unique for the same formats, does not need to be freed
  588. VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_descriptions);
  589. RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers, const Vector<uint64_t> &p_offsets = Vector<uint64_t>());
  590. 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);
  591. RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count);
  592. /****************/
  593. /**** SHADER ****/
  594. /****************/
  595. // Some APIs (e.g., Vulkan) specifies a really complex behavior for the application
  596. // in order to tell when descriptor sets need to be re-bound (or not).
  597. // "When binding a descriptor set (see Descriptor Set Binding) to set
  598. // number N, if the previously bound descriptor sets for sets zero
  599. // through N-1 were all bound using compatible pipeline layouts,
  600. // then performing this binding does not disturb any of the lower numbered sets.
  601. // If, additionally, the previous bound descriptor set for set N was
  602. // bound using a pipeline layout compatible for set N, then the bindings
  603. // in sets numbered greater than N are also not disturbed."
  604. // As a result, we need to figure out quickly when something is no longer "compatible".
  605. // in order to avoid costly rebinds.
  606. private:
  607. struct UniformSetFormat {
  608. Vector<ShaderUniform> uniforms;
  609. _FORCE_INLINE_ bool operator<(const UniformSetFormat &p_other) const {
  610. if (uniforms.size() != p_other.uniforms.size()) {
  611. return uniforms.size() < p_other.uniforms.size();
  612. }
  613. for (int i = 0; i < uniforms.size(); i++) {
  614. if (uniforms[i] < p_other.uniforms[i]) {
  615. return true;
  616. } else if (p_other.uniforms[i] < uniforms[i]) {
  617. return false;
  618. }
  619. }
  620. return false;
  621. }
  622. };
  623. // Always grows, never shrinks, ensuring unique IDs, but we assume
  624. // the amount of formats will never be a problem, as the amount of shaders
  625. // in a game is limited.
  626. RBMap<UniformSetFormat, uint32_t> uniform_set_format_cache;
  627. // Shaders in Vulkan are just pretty much
  628. // precompiled blocks of SPIR-V bytecode. They
  629. // are most likely not really compiled to host
  630. // assembly until a pipeline is created.
  631. //
  632. // When supplying the shaders, this implementation
  633. // will use the reflection abilities of glslang to
  634. // understand and cache everything required to
  635. // create and use the descriptor sets (Vulkan's
  636. // biggest pain).
  637. //
  638. // Additionally, hashes are created for every set
  639. // to do quick validation and ensuring the user
  640. // does not submit something invalid.
  641. struct Shader : public ShaderDescription {
  642. String name; // Used for debug.
  643. RDD::ShaderID driver_id;
  644. uint32_t layout_hash = 0;
  645. BitField<RDD::PipelineStageBits> stage_bits;
  646. Vector<uint32_t> set_formats;
  647. };
  648. String _shader_uniform_debug(RID p_shader, int p_set = -1);
  649. RID_Owner<Shader, true> shader_owner;
  650. #ifndef DISABLE_DEPRECATED
  651. public:
  652. enum BarrierMask {
  653. BARRIER_MASK_VERTEX = 1,
  654. BARRIER_MASK_FRAGMENT = 8,
  655. BARRIER_MASK_COMPUTE = 2,
  656. BARRIER_MASK_TRANSFER = 4,
  657. BARRIER_MASK_RASTER = BARRIER_MASK_VERTEX | BARRIER_MASK_FRAGMENT, // 9,
  658. BARRIER_MASK_ALL_BARRIERS = 0x7FFF, // all flags set
  659. BARRIER_MASK_NO_BARRIER = 0x8000,
  660. };
  661. enum InitialAction {
  662. INITIAL_ACTION_LOAD,
  663. INITIAL_ACTION_CLEAR,
  664. INITIAL_ACTION_DISCARD,
  665. INITIAL_ACTION_MAX,
  666. INITIAL_ACTION_CLEAR_REGION = INITIAL_ACTION_CLEAR,
  667. INITIAL_ACTION_CLEAR_REGION_CONTINUE = INITIAL_ACTION_CLEAR,
  668. INITIAL_ACTION_KEEP = INITIAL_ACTION_LOAD,
  669. INITIAL_ACTION_DROP = INITIAL_ACTION_DISCARD,
  670. INITIAL_ACTION_CONTINUE = INITIAL_ACTION_LOAD,
  671. };
  672. enum FinalAction {
  673. FINAL_ACTION_STORE,
  674. FINAL_ACTION_DISCARD,
  675. FINAL_ACTION_MAX,
  676. FINAL_ACTION_READ = FINAL_ACTION_STORE,
  677. FINAL_ACTION_CONTINUE = FINAL_ACTION_STORE,
  678. };
  679. void barrier(BitField<BarrierMask> p_from = BARRIER_MASK_ALL_BARRIERS, BitField<BarrierMask> p_to = BARRIER_MASK_ALL_BARRIERS);
  680. void full_barrier();
  681. void draw_command_insert_label(String p_label_name, const Color &p_color = Color(1, 1, 1, 1));
  682. 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>());
  683. Error draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids);
  684. Vector<int64_t> _draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, 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 TypedArray<RID> &p_storage_textures = TypedArray<RID>());
  685. Vector<int64_t> _draw_list_switch_to_next_pass_split(uint32_t p_splits);
  686. private:
  687. void _draw_list_end_bind_compat_81356(BitField<BarrierMask> p_post_barrier);
  688. void _compute_list_end_bind_compat_81356(BitField<BarrierMask> p_post_barrier);
  689. void _barrier_bind_compat_81356(BitField<BarrierMask> p_from, BitField<BarrierMask> p_to);
  690. void _draw_list_end_bind_compat_84976(BitField<BarrierMask> p_post_barrier);
  691. void _compute_list_end_bind_compat_84976(BitField<BarrierMask> p_post_barrier);
  692. InitialAction _convert_initial_action_84976(InitialAction p_old_initial_action);
  693. FinalAction _convert_final_action_84976(FinalAction p_old_final_action);
  694. DrawListID _draw_list_begin_bind_compat_84976(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, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const TypedArray<RID> &p_storage_textures);
  695. ComputeListID _compute_list_begin_bind_compat_84976(bool p_allow_draw_overlap);
  696. Error _buffer_update_bind_compat_84976(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier);
  697. Error _buffer_clear_bind_compat_84976(RID p_buffer, uint32_t p_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier);
  698. Error _texture_update_bind_compat_84976(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier);
  699. Error _texture_copy_bind_compat_84976(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, BitField<BarrierMask> p_post_barrier);
  700. Error _texture_clear_bind_compat_84976(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, BitField<BarrierMask> p_post_barrier);
  701. Error _texture_resolve_multisample_bind_compat_84976(RID p_from_texture, RID p_to_texture, BitField<BarrierMask> p_post_barrier);
  702. FramebufferFormatID _screen_get_framebuffer_format_bind_compat_87340() const;
  703. DrawListID _draw_list_begin_bind_compat_90993(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, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region);
  704. DrawListID _draw_list_begin_bind_compat_98670(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, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, uint32_t p_breadcrumb);
  705. #endif
  706. public:
  707. RenderingContextDriver *get_context_driver() const { return context; }
  708. const RDD::Capabilities &get_device_capabilities() const { return driver->get_capabilities(); }
  709. bool has_feature(const Features p_feature) const;
  710. Vector<uint8_t> shader_compile_spirv_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language = SHADER_LANGUAGE_GLSL, String *r_error = nullptr, bool p_allow_cache = true);
  711. String shader_get_spirv_cache_key() const;
  712. static void shader_set_compile_to_spirv_function(ShaderCompileToSPIRVFunction p_function);
  713. static void shader_set_spirv_cache_function(ShaderCacheFunction p_function);
  714. static void shader_set_get_cache_key_function(ShaderSPIRVGetCacheKeyFunction p_function);
  715. String shader_get_binary_cache_key() const;
  716. Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
  717. RID shader_create_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name = "");
  718. RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, RID p_placeholder = RID());
  719. RID shader_create_placeholder();
  720. uint64_t shader_get_vertex_input_attribute_mask(RID p_shader);
  721. /******************/
  722. /**** UNIFORMS ****/
  723. /******************/
  724. String get_perf_report() const;
  725. enum StorageBufferUsage {
  726. STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT = 1,
  727. };
  728. RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  729. RID storage_buffer_create(uint32_t p_size, const Vector<uint8_t> &p_data = Vector<uint8_t>(), BitField<StorageBufferUsage> p_usage = 0);
  730. RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>());
  731. struct Uniform {
  732. UniformType uniform_type = UNIFORM_TYPE_IMAGE;
  733. uint32_t binding = 0; // Binding index as specified in shader.
  734. private:
  735. // In most cases only one ID is provided per binding, so avoid allocating memory unnecessarily for performance.
  736. RID id; // If only one is provided, this is used.
  737. Vector<RID> ids; // If multiple ones are provided, this is used instead.
  738. public:
  739. _FORCE_INLINE_ uint32_t get_id_count() const {
  740. return (id.is_valid() ? 1 : ids.size());
  741. }
  742. _FORCE_INLINE_ RID get_id(uint32_t p_idx) const {
  743. if (id.is_valid()) {
  744. ERR_FAIL_COND_V(p_idx != 0, RID());
  745. return id;
  746. } else {
  747. return ids[p_idx];
  748. }
  749. }
  750. _FORCE_INLINE_ void set_id(uint32_t p_idx, RID p_id) {
  751. if (id.is_valid()) {
  752. ERR_FAIL_COND(p_idx != 0);
  753. id = p_id;
  754. } else {
  755. ids.write[p_idx] = p_id;
  756. }
  757. }
  758. _FORCE_INLINE_ void append_id(RID p_id) {
  759. if (ids.is_empty()) {
  760. if (id == RID()) {
  761. id = p_id;
  762. } else {
  763. ids.push_back(id);
  764. ids.push_back(p_id);
  765. id = RID();
  766. }
  767. } else {
  768. ids.push_back(p_id);
  769. }
  770. }
  771. _FORCE_INLINE_ void clear_ids() {
  772. id = RID();
  773. ids.clear();
  774. }
  775. _FORCE_INLINE_ Uniform(UniformType p_type, int p_binding, RID p_id) {
  776. uniform_type = p_type;
  777. binding = p_binding;
  778. id = p_id;
  779. }
  780. _FORCE_INLINE_ Uniform(UniformType p_type, int p_binding, const Vector<RID> &p_ids) {
  781. uniform_type = p_type;
  782. binding = p_binding;
  783. ids = p_ids;
  784. }
  785. _FORCE_INLINE_ Uniform() = default;
  786. };
  787. private:
  788. static const uint32_t MAX_UNIFORM_SETS = 16;
  789. static const uint32_t MAX_PUSH_CONSTANT_SIZE = 128;
  790. // This structure contains the descriptor set. They _need_ to be allocated
  791. // for a shader (and will be erased when this shader is erased), but should
  792. // work for other shaders as long as the hash matches. This covers using
  793. // them in shader variants.
  794. //
  795. // Keep also in mind that you can share buffers between descriptor sets, so
  796. // the above restriction is not too serious.
  797. struct UniformSet {
  798. uint32_t format = 0;
  799. RID shader_id;
  800. uint32_t shader_set = 0;
  801. RDD::UniformSetID driver_id;
  802. struct AttachableTexture {
  803. uint32_t bind = 0;
  804. RID texture;
  805. };
  806. struct SharedTexture {
  807. uint32_t writing = 0;
  808. RID texture;
  809. };
  810. LocalVector<AttachableTexture> attachable_textures; // Used for validation.
  811. Vector<RDG::ResourceTracker *> draw_trackers;
  812. Vector<RDG::ResourceUsage> draw_trackers_usage;
  813. HashMap<RID, RDG::ResourceUsage> untracked_usage;
  814. LocalVector<SharedTexture> shared_textures_to_update;
  815. InvalidationCallback invalidated_callback = nullptr;
  816. void *invalidated_callback_userdata = nullptr;
  817. };
  818. RID_Owner<UniformSet, true> uniform_set_owner;
  819. void _uniform_set_update_shared(UniformSet *p_uniform_set);
  820. public:
  821. template <typename Collection>
  822. RID uniform_set_create(const Collection &p_uniforms, RID p_shader, uint32_t p_shader_set);
  823. bool uniform_set_is_valid(RID p_uniform_set);
  824. void uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata);
  825. /*******************/
  826. /**** PIPELINES ****/
  827. /*******************/
  828. // Render pipeline contains ALL the
  829. // information required for drawing.
  830. // This includes all the rasterizer state
  831. // as well as shader used, framebuffer format,
  832. // etc.
  833. // While the pipeline is just a single object
  834. // (VkPipeline) a lot of values are also saved
  835. // here to do validation (vulkan does none by
  836. // default) and warn the user if something
  837. // was not supplied as intended.
  838. private:
  839. struct RenderPipeline {
  840. // Cached values for validation.
  841. #ifdef DEBUG_ENABLED
  842. struct Validation {
  843. FramebufferFormatID framebuffer_format;
  844. uint32_t render_pass = 0;
  845. uint32_t dynamic_state = 0;
  846. VertexFormatID vertex_format;
  847. bool uses_restart_indices = false;
  848. uint32_t primitive_minimum = 0;
  849. uint32_t primitive_divisor = 0;
  850. } validation;
  851. #endif
  852. // Actual pipeline.
  853. RID shader;
  854. RDD::ShaderID shader_driver_id;
  855. uint32_t shader_layout_hash = 0;
  856. Vector<uint32_t> set_formats;
  857. RDD::PipelineID driver_id;
  858. BitField<RDD::PipelineStageBits> stage_bits;
  859. uint32_t push_constant_size = 0;
  860. };
  861. RID_Owner<RenderPipeline, true> render_pipeline_owner;
  862. bool pipeline_cache_enabled = false;
  863. size_t pipeline_cache_size = 0;
  864. String pipeline_cache_file_path;
  865. WorkerThreadPool::TaskID pipeline_cache_save_task = WorkerThreadPool::INVALID_TASK_ID;
  866. Vector<uint8_t> _load_pipeline_cache();
  867. void _update_pipeline_cache(bool p_closing = false);
  868. static void _save_pipeline_cache(void *p_data);
  869. struct ComputePipeline {
  870. RID shader;
  871. RDD::ShaderID shader_driver_id;
  872. uint32_t shader_layout_hash = 0;
  873. Vector<uint32_t> set_formats;
  874. RDD::PipelineID driver_id;
  875. uint32_t push_constant_size = 0;
  876. uint32_t local_group_size[3] = { 0, 0, 0 };
  877. };
  878. RID_Owner<ComputePipeline, true> compute_pipeline_owner;
  879. public:
  880. RID render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags = 0, uint32_t p_for_render_pass = 0, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  881. bool render_pipeline_is_valid(RID p_pipeline);
  882. RID compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants = Vector<PipelineSpecializationConstant>());
  883. bool compute_pipeline_is_valid(RID p_pipeline);
  884. private:
  885. /****************/
  886. /**** SCREEN ****/
  887. /****************/
  888. HashMap<DisplayServer::WindowID, RDD::SwapChainID> screen_swap_chains;
  889. HashMap<DisplayServer::WindowID, RDD::FramebufferID> screen_framebuffers;
  890. uint32_t _get_swap_chain_desired_count() const;
  891. public:
  892. Error screen_create(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID);
  893. Error screen_prepare_for_drawing(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID);
  894. int screen_get_width(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID) const;
  895. int screen_get_height(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID) const;
  896. int screen_get_pre_rotation_degrees(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID) const;
  897. FramebufferFormatID screen_get_framebuffer_format(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID) const;
  898. Error screen_free(DisplayServer::WindowID p_screen = DisplayServer::MAIN_WINDOW_ID);
  899. /*************************/
  900. /**** DRAW LISTS (II) ****/
  901. /*************************/
  902. private:
  903. // Draw list contains both the command buffer
  904. // used for drawing as well as a LOT of
  905. // information used for validation. This
  906. // validation is cheap so most of it can
  907. // also run in release builds.
  908. struct DrawList {
  909. Rect2i viewport;
  910. bool viewport_set = false;
  911. struct SetState {
  912. uint32_t pipeline_expected_format = 0;
  913. uint32_t uniform_set_format = 0;
  914. RDD::UniformSetID uniform_set_driver_id;
  915. RID uniform_set;
  916. bool bound = false;
  917. };
  918. struct State {
  919. SetState sets[MAX_UNIFORM_SETS];
  920. uint32_t set_count = 0;
  921. RID pipeline;
  922. RID pipeline_shader;
  923. RDD::ShaderID pipeline_shader_driver_id;
  924. uint32_t pipeline_shader_layout_hash = 0;
  925. uint32_t pipeline_push_constant_size = 0;
  926. RID vertex_array;
  927. RID index_array;
  928. uint32_t draw_count = 0;
  929. } state;
  930. #ifdef DEBUG_ENABLED
  931. struct Validation {
  932. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  933. // Actual render pass values.
  934. uint32_t dynamic_state = 0;
  935. VertexFormatID vertex_format = INVALID_ID;
  936. uint32_t vertex_array_size = 0;
  937. uint32_t vertex_max_instances_allowed = 0xFFFFFFFF;
  938. bool index_buffer_uses_restart_indices = false;
  939. uint32_t index_array_count = 0;
  940. uint32_t index_array_max_index = 0;
  941. Vector<uint32_t> set_formats;
  942. Vector<bool> set_bound;
  943. Vector<RID> set_rids;
  944. // Last pipeline set values.
  945. bool pipeline_active = false;
  946. uint32_t pipeline_dynamic_state = 0;
  947. VertexFormatID pipeline_vertex_format = INVALID_ID;
  948. RID pipeline_shader;
  949. bool pipeline_uses_restart_indices = false;
  950. uint32_t pipeline_primitive_divisor = 0;
  951. uint32_t pipeline_primitive_minimum = 0;
  952. uint32_t pipeline_push_constant_size = 0;
  953. bool pipeline_push_constant_supplied = false;
  954. } validation;
  955. #else
  956. struct Validation {
  957. uint32_t vertex_array_size = 0;
  958. uint32_t index_array_count = 0;
  959. } validation;
  960. #endif
  961. };
  962. DrawList *draw_list = nullptr;
  963. uint32_t draw_list_subpass_count = 0;
  964. #ifdef DEBUG_ENABLED
  965. FramebufferFormatID draw_list_framebuffer_format = INVALID_ID;
  966. #endif
  967. uint32_t draw_list_current_subpass = 0;
  968. Vector<RID> draw_list_bound_textures;
  969. _FORCE_INLINE_ DrawList *_get_draw_list_ptr(DrawListID p_id);
  970. Error _draw_list_allocate(const Rect2i &p_viewport, uint32_t p_subpass);
  971. void _draw_list_free(Rect2i *r_last_viewport = nullptr);
  972. public:
  973. enum DrawFlags {
  974. DRAW_DEFAULT_ALL = 0,
  975. DRAW_CLEAR_COLOR_0 = (1 << 0),
  976. DRAW_CLEAR_COLOR_1 = (1 << 1),
  977. DRAW_CLEAR_COLOR_2 = (1 << 2),
  978. DRAW_CLEAR_COLOR_3 = (1 << 3),
  979. DRAW_CLEAR_COLOR_4 = (1 << 4),
  980. DRAW_CLEAR_COLOR_5 = (1 << 5),
  981. DRAW_CLEAR_COLOR_6 = (1 << 6),
  982. DRAW_CLEAR_COLOR_7 = (1 << 7),
  983. DRAW_CLEAR_COLOR_MASK = 0xFF,
  984. DRAW_CLEAR_COLOR_ALL = DRAW_CLEAR_COLOR_MASK,
  985. DRAW_IGNORE_COLOR_0 = (1 << 8),
  986. DRAW_IGNORE_COLOR_1 = (1 << 9),
  987. DRAW_IGNORE_COLOR_2 = (1 << 10),
  988. DRAW_IGNORE_COLOR_3 = (1 << 11),
  989. DRAW_IGNORE_COLOR_4 = (1 << 12),
  990. DRAW_IGNORE_COLOR_5 = (1 << 13),
  991. DRAW_IGNORE_COLOR_6 = (1 << 14),
  992. DRAW_IGNORE_COLOR_7 = (1 << 15),
  993. DRAW_IGNORE_COLOR_MASK = 0xFF00,
  994. DRAW_IGNORE_COLOR_ALL = DRAW_IGNORE_COLOR_MASK,
  995. DRAW_CLEAR_DEPTH = (1 << 16),
  996. DRAW_IGNORE_DEPTH = (1 << 17),
  997. DRAW_CLEAR_STENCIL = (1 << 18),
  998. DRAW_IGNORE_STENCIL = (1 << 19),
  999. DRAW_CLEAR_ALL = DRAW_CLEAR_COLOR_ALL | DRAW_CLEAR_DEPTH | DRAW_CLEAR_STENCIL,
  1000. DRAW_IGNORE_ALL = DRAW_IGNORE_COLOR_ALL | DRAW_IGNORE_DEPTH | DRAW_IGNORE_STENCIL
  1001. };
  1002. DrawListID draw_list_begin_for_screen(DisplayServer::WindowID p_screen = 0, const Color &p_clear_color = Color());
  1003. DrawListID draw_list_begin(RID p_framebuffer, BitField<DrawFlags> p_draw_flags = DRAW_DEFAULT_ALL, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth_value = 1.0f, uint32_t p_clear_stencil_value = 0, const Rect2 &p_region = Rect2(), uint32_t p_breadcrumb = 0);
  1004. void draw_list_set_blend_constants(DrawListID p_list, const Color &p_color);
  1005. void draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline);
  1006. void draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index);
  1007. void draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array);
  1008. void draw_list_bind_index_array(DrawListID p_list, RID p_index_array);
  1009. void draw_list_set_line_width(DrawListID p_list, float p_width);
  1010. void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size);
  1011. void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0);
  1012. void draw_list_draw_indirect(DrawListID p_list, bool p_use_indices, RID p_buffer, uint32_t p_offset = 0, uint32_t p_draw_count = 1, uint32_t p_stride = 0);
  1013. void draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect);
  1014. void draw_list_disable_scissor(DrawListID p_list);
  1015. uint32_t draw_list_get_current_pass();
  1016. DrawListID draw_list_switch_to_next_pass();
  1017. void draw_list_end();
  1018. private:
  1019. /***********************/
  1020. /**** COMPUTE LISTS ****/
  1021. /***********************/
  1022. struct ComputeList {
  1023. struct SetState {
  1024. uint32_t pipeline_expected_format = 0;
  1025. uint32_t uniform_set_format = 0;
  1026. RDD::UniformSetID uniform_set_driver_id;
  1027. RID uniform_set;
  1028. bool bound = false;
  1029. };
  1030. struct State {
  1031. SetState sets[MAX_UNIFORM_SETS];
  1032. uint32_t set_count = 0;
  1033. RID pipeline;
  1034. RID pipeline_shader;
  1035. RDD::ShaderID pipeline_shader_driver_id;
  1036. uint32_t pipeline_shader_layout_hash = 0;
  1037. uint32_t local_group_size[3] = { 0, 0, 0 };
  1038. uint8_t push_constant_data[MAX_PUSH_CONSTANT_SIZE] = {};
  1039. uint32_t push_constant_size = 0;
  1040. uint32_t dispatch_count = 0;
  1041. } state;
  1042. #ifdef DEBUG_ENABLED
  1043. struct Validation {
  1044. bool active = true; // Means command buffer was not closed, so you can keep adding things.
  1045. Vector<uint32_t> set_formats;
  1046. Vector<bool> set_bound;
  1047. Vector<RID> set_rids;
  1048. // Last pipeline set values.
  1049. bool pipeline_active = false;
  1050. RID pipeline_shader;
  1051. uint32_t invalid_set_from = 0;
  1052. uint32_t pipeline_push_constant_size = 0;
  1053. bool pipeline_push_constant_supplied = false;
  1054. } validation;
  1055. #endif
  1056. };
  1057. ComputeList *compute_list = nullptr;
  1058. ComputeList::State compute_list_barrier_state;
  1059. public:
  1060. ComputeListID compute_list_begin();
  1061. void compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline);
  1062. void compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index);
  1063. void compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size);
  1064. void compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
  1065. void compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads);
  1066. void compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset);
  1067. void compute_list_add_barrier(ComputeListID p_list);
  1068. void compute_list_end();
  1069. private:
  1070. /*************************/
  1071. /**** TRANSFER WORKER ****/
  1072. /*************************/
  1073. struct TransferWorker {
  1074. uint32_t index = 0;
  1075. RDD::BufferID staging_buffer;
  1076. uint32_t max_transfer_size = 0;
  1077. uint32_t staging_buffer_size_in_use = 0;
  1078. uint32_t staging_buffer_size_allocated = 0;
  1079. RDD::CommandBufferID command_buffer;
  1080. RDD::CommandPoolID command_pool;
  1081. RDD::FenceID command_fence;
  1082. LocalVector<RDD::TextureBarrier> texture_barriers;
  1083. bool recording = false;
  1084. bool submitted = false;
  1085. BinaryMutex thread_mutex;
  1086. uint64_t operations_processed = 0;
  1087. uint64_t operations_submitted = 0;
  1088. uint64_t operations_counter = 0;
  1089. BinaryMutex operations_mutex;
  1090. };
  1091. LocalVector<TransferWorker *> transfer_worker_pool;
  1092. uint32_t transfer_worker_pool_max_size = 1;
  1093. LocalVector<uint64_t> transfer_worker_operation_used_by_draw;
  1094. LocalVector<uint32_t> transfer_worker_pool_available_list;
  1095. LocalVector<RDD::TextureBarrier> transfer_worker_pool_texture_barriers;
  1096. BinaryMutex transfer_worker_pool_mutex;
  1097. BinaryMutex transfer_worker_pool_texture_barriers_mutex;
  1098. ConditionVariable transfer_worker_pool_condition;
  1099. TransferWorker *_acquire_transfer_worker(uint32_t p_transfer_size, uint32_t p_required_align, uint32_t &r_staging_offset);
  1100. void _release_transfer_worker(TransferWorker *p_transfer_worker);
  1101. void _end_transfer_worker(TransferWorker *p_transfer_worker);
  1102. void _submit_transfer_worker(TransferWorker *p_transfer_worker, VectorView<RDD::SemaphoreID> p_signal_semaphores = VectorView<RDD::SemaphoreID>());
  1103. void _wait_for_transfer_worker(TransferWorker *p_transfer_worker);
  1104. void _flush_barriers_for_transfer_worker(TransferWorker *p_transfer_worker);
  1105. void _check_transfer_worker_operation(uint32_t p_transfer_worker_index, uint64_t p_transfer_worker_operation);
  1106. void _check_transfer_worker_buffer(Buffer *p_buffer);
  1107. void _check_transfer_worker_texture(Texture *p_texture);
  1108. void _check_transfer_worker_vertex_array(VertexArray *p_vertex_array);
  1109. void _check_transfer_worker_index_array(IndexArray *p_index_array);
  1110. void _submit_transfer_workers(RDD::CommandBufferID p_draw_command_buffer = RDD::CommandBufferID());
  1111. void _submit_transfer_barriers(RDD::CommandBufferID p_draw_command_buffer);
  1112. void _wait_for_transfer_workers();
  1113. void _free_transfer_workers();
  1114. /***********************/
  1115. /**** COMMAND GRAPH ****/
  1116. /***********************/
  1117. bool _texture_make_mutable(Texture *p_texture, RID p_texture_id);
  1118. bool _buffer_make_mutable(Buffer *p_buffer, RID p_buffer_id);
  1119. bool _vertex_array_make_mutable(VertexArray *p_vertex_array, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker);
  1120. bool _index_array_make_mutable(IndexArray *p_index_array, RDG::ResourceTracker *p_resource_tracker);
  1121. bool _uniform_set_make_mutable(UniformSet *p_uniform_set, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker);
  1122. bool _dependency_make_mutable(RID p_id, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker);
  1123. bool _dependencies_make_mutable_recursive(RID p_id, RDG::ResourceTracker *p_resource_tracker);
  1124. bool _dependencies_make_mutable(RID p_id, RDG::ResourceTracker *p_resource_tracker);
  1125. RenderingDeviceGraph draw_graph;
  1126. /**************************/
  1127. /**** QUEUE MANAGEMENT ****/
  1128. /**************************/
  1129. RDD::CommandQueueFamilyID main_queue_family;
  1130. RDD::CommandQueueFamilyID transfer_queue_family;
  1131. RDD::CommandQueueFamilyID present_queue_family;
  1132. RDD::CommandQueueID main_queue;
  1133. RDD::CommandQueueID transfer_queue;
  1134. RDD::CommandQueueID present_queue;
  1135. /**************************/
  1136. /**** FRAME MANAGEMENT ****/
  1137. /**************************/
  1138. // This is the frame structure. There are normally
  1139. // 3 of these (used for triple buffering), or 2
  1140. // (double buffering). They are cycled constantly.
  1141. //
  1142. // It contains two command buffers, one that is
  1143. // used internally for setting up (creating stuff)
  1144. // and another used mostly for drawing.
  1145. //
  1146. // They also contains a list of things that need
  1147. // to be disposed of when deleted, which can't
  1148. // happen immediately due to the asynchronous
  1149. // nature of the GPU. They will get deleted
  1150. // when the frame is cycled.
  1151. struct Frame {
  1152. // List in usage order, from last to free to first to free.
  1153. List<Buffer> buffers_to_dispose_of;
  1154. List<Texture> textures_to_dispose_of;
  1155. List<Framebuffer> framebuffers_to_dispose_of;
  1156. List<RDD::SamplerID> samplers_to_dispose_of;
  1157. List<Shader> shaders_to_dispose_of;
  1158. List<UniformSet> uniform_sets_to_dispose_of;
  1159. List<RenderPipeline> render_pipelines_to_dispose_of;
  1160. List<ComputePipeline> compute_pipelines_to_dispose_of;
  1161. // The command pool used by the command buffer.
  1162. RDD::CommandPoolID command_pool;
  1163. // The command buffer used by the main thread when recording the frame.
  1164. RDD::CommandBufferID command_buffer;
  1165. // Signaled by the command buffer submission. Present must wait on this semaphore.
  1166. RDD::SemaphoreID semaphore;
  1167. // Signaled by the command buffer submission. Must wait on this fence before beginning command recording for the frame.
  1168. RDD::FenceID fence;
  1169. bool fence_signaled = false;
  1170. // Semaphores the frame must wait on before executing the command buffer.
  1171. LocalVector<RDD::SemaphoreID> semaphores_to_wait_on;
  1172. // Swap chains prepared for drawing during the frame that must be presented.
  1173. LocalVector<RDD::SwapChainID> swap_chains_to_present;
  1174. // Semaphores the transfer workers can use to wait before rendering the frame.
  1175. // This must have the same size of the transfer worker pool.
  1176. TightLocalVector<RDD::SemaphoreID> transfer_worker_semaphores;
  1177. // Extra command buffer pool used for driver workarounds.
  1178. RDG::CommandBufferPool command_buffer_pool;
  1179. struct Timestamp {
  1180. String description;
  1181. uint64_t value = 0;
  1182. };
  1183. RDD::QueryPoolID timestamp_pool;
  1184. TightLocalVector<String> timestamp_names;
  1185. TightLocalVector<uint64_t> timestamp_cpu_values;
  1186. uint32_t timestamp_count = 0;
  1187. TightLocalVector<String> timestamp_result_names;
  1188. TightLocalVector<uint64_t> timestamp_cpu_result_values;
  1189. TightLocalVector<uint64_t> timestamp_result_values;
  1190. uint32_t timestamp_result_count = 0;
  1191. uint64_t index = 0;
  1192. };
  1193. uint32_t max_timestamp_query_elements = 0;
  1194. int frame = 0;
  1195. TightLocalVector<Frame> frames;
  1196. uint64_t frames_drawn = 0;
  1197. void _free_pending_resources(int p_frame);
  1198. uint64_t texture_memory = 0;
  1199. uint64_t buffer_memory = 0;
  1200. void _free_internal(RID p_id);
  1201. void _begin_frame();
  1202. void _end_frame();
  1203. void _execute_frame(bool p_present);
  1204. void _stall_for_previous_frames();
  1205. void _flush_and_stall_for_all_frames();
  1206. template <typename T>
  1207. void _free_rids(T &p_owner, const char *p_type);
  1208. #ifdef DEV_ENABLED
  1209. HashMap<RID, String> resource_names;
  1210. #endif
  1211. public:
  1212. Error initialize(RenderingContextDriver *p_context, DisplayServer::WindowID p_main_window = DisplayServer::INVALID_WINDOW_ID);
  1213. void finalize();
  1214. void _set_max_fps(int p_max_fps);
  1215. void free(RID p_id);
  1216. /****************/
  1217. /**** Timing ****/
  1218. /****************/
  1219. void capture_timestamp(const String &p_name);
  1220. uint32_t get_captured_timestamps_count() const;
  1221. uint64_t get_captured_timestamps_frame() const;
  1222. uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
  1223. uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
  1224. String get_captured_timestamp_name(uint32_t p_index) const;
  1225. /****************/
  1226. /**** LIMITS ****/
  1227. /****************/
  1228. uint64_t limit_get(Limit p_limit) const;
  1229. void swap_buffers();
  1230. uint32_t get_frame_delay() const;
  1231. void submit();
  1232. void sync();
  1233. enum MemoryType {
  1234. MEMORY_TEXTURES,
  1235. MEMORY_BUFFERS,
  1236. MEMORY_TOTAL
  1237. };
  1238. uint64_t get_memory_usage(MemoryType p_type) const;
  1239. RenderingDevice *create_local_device();
  1240. void set_resource_name(RID p_id, const String &p_name);
  1241. void draw_command_begin_label(String p_label_name, const Color &p_color = Color(1, 1, 1, 1));
  1242. void draw_command_end_label();
  1243. String get_device_vendor_name() const;
  1244. String get_device_name() const;
  1245. DeviceType get_device_type() const;
  1246. String get_device_api_name() const;
  1247. String get_device_api_version() const;
  1248. String get_device_pipeline_cache_uuid() const;
  1249. bool is_composite_alpha_supported() const;
  1250. uint64_t get_driver_resource(DriverResource p_resource, RID p_rid = RID(), uint64_t p_index = 0);
  1251. String get_driver_and_device_memory_report() const;
  1252. String get_tracked_object_name(uint32_t p_type_index) const;
  1253. uint64_t get_tracked_object_type_count() const;
  1254. uint64_t get_driver_total_memory() const;
  1255. uint64_t get_driver_allocation_count() const;
  1256. uint64_t get_driver_memory_by_object_type(uint32_t p_type) const;
  1257. uint64_t get_driver_allocs_by_object_type(uint32_t p_type) const;
  1258. uint64_t get_device_total_memory() const;
  1259. uint64_t get_device_allocation_count() const;
  1260. uint64_t get_device_memory_by_object_type(uint32_t p_type) const;
  1261. uint64_t get_device_allocs_by_object_type(uint32_t p_type) const;
  1262. static RenderingDevice *get_singleton();
  1263. void make_current();
  1264. RenderingDevice();
  1265. ~RenderingDevice();
  1266. private:
  1267. /*****************/
  1268. /**** BINDERS ****/
  1269. /*****************/
  1270. RID _texture_create(const Ref<RDTextureFormat> &p_format, const Ref<RDTextureView> &p_view, const TypedArray<PackedByteArray> &p_data = Array());
  1271. RID _texture_create_shared(const Ref<RDTextureView> &p_view, RID p_with_texture);
  1272. RID _texture_create_shared_from_slice(const Ref<RDTextureView> &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);
  1273. Ref<RDTextureFormat> _texture_get_format(RID p_rd_texture);
  1274. FramebufferFormatID _framebuffer_format_create(const TypedArray<RDAttachmentFormat> &p_attachments, uint32_t p_view_count);
  1275. FramebufferFormatID _framebuffer_format_create_multipass(const TypedArray<RDAttachmentFormat> &p_attachments, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_view_count);
  1276. RID _framebuffer_create(const TypedArray<RID> &p_textures, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  1277. RID _framebuffer_create_multipass(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1);
  1278. RID _sampler_create(const Ref<RDSamplerState> &p_state);
  1279. VertexFormatID _vertex_format_create(const TypedArray<RDVertexAttribute> &p_vertex_formats);
  1280. RID _vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const TypedArray<RID> &p_src_buffers, const Vector<int64_t> &p_offsets = Vector<int64_t>());
  1281. Ref<RDShaderSPIRV> _shader_compile_spirv_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache = true);
  1282. Vector<uint8_t> _shader_compile_binary_from_spirv(const Ref<RDShaderSPIRV> &p_bytecode, const String &p_shader_name = "");
  1283. RID _shader_create_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name = "");
  1284. RID _uniform_set_create(const TypedArray<RDUniform> &p_uniforms, RID p_shader, uint32_t p_shader_set);
  1285. Error _buffer_update_bind(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data);
  1286. RID _render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const Ref<RDPipelineRasterizationState> &p_rasterization_state, const Ref<RDPipelineMultisampleState> &p_multisample_state, const Ref<RDPipelineDepthStencilState> &p_depth_stencil_state, const Ref<RDPipelineColorBlendState> &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags, uint32_t p_for_render_pass, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants);
  1287. RID _compute_pipeline_create(RID p_shader, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants);
  1288. void _draw_list_set_push_constant(DrawListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size);
  1289. void _compute_list_set_push_constant(ComputeListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size);
  1290. };
  1291. VARIANT_ENUM_CAST(RenderingDevice::DeviceType)
  1292. VARIANT_ENUM_CAST(RenderingDevice::DriverResource)
  1293. VARIANT_ENUM_CAST(RenderingDevice::ShaderStage)
  1294. VARIANT_ENUM_CAST(RenderingDevice::ShaderLanguage)
  1295. VARIANT_ENUM_CAST(RenderingDevice::CompareOperator)
  1296. VARIANT_ENUM_CAST(RenderingDevice::DataFormat)
  1297. VARIANT_ENUM_CAST(RenderingDevice::TextureType)
  1298. VARIANT_ENUM_CAST(RenderingDevice::TextureSamples)
  1299. VARIANT_BITFIELD_CAST(RenderingDevice::TextureUsageBits)
  1300. VARIANT_ENUM_CAST(RenderingDevice::TextureSwizzle)
  1301. VARIANT_ENUM_CAST(RenderingDevice::TextureSliceType)
  1302. VARIANT_ENUM_CAST(RenderingDevice::SamplerFilter)
  1303. VARIANT_ENUM_CAST(RenderingDevice::SamplerRepeatMode)
  1304. VARIANT_ENUM_CAST(RenderingDevice::SamplerBorderColor)
  1305. VARIANT_ENUM_CAST(RenderingDevice::VertexFrequency)
  1306. VARIANT_ENUM_CAST(RenderingDevice::IndexBufferFormat)
  1307. VARIANT_BITFIELD_CAST(RenderingDevice::StorageBufferUsage)
  1308. VARIANT_ENUM_CAST(RenderingDevice::UniformType)
  1309. VARIANT_ENUM_CAST(RenderingDevice::RenderPrimitive)
  1310. VARIANT_ENUM_CAST(RenderingDevice::PolygonCullMode)
  1311. VARIANT_ENUM_CAST(RenderingDevice::PolygonFrontFace)
  1312. VARIANT_ENUM_CAST(RenderingDevice::StencilOperation)
  1313. VARIANT_ENUM_CAST(RenderingDevice::LogicOperation)
  1314. VARIANT_ENUM_CAST(RenderingDevice::BlendFactor)
  1315. VARIANT_ENUM_CAST(RenderingDevice::BlendOperation)
  1316. VARIANT_BITFIELD_CAST(RenderingDevice::PipelineDynamicStateFlags)
  1317. VARIANT_ENUM_CAST(RenderingDevice::PipelineSpecializationConstantType)
  1318. VARIANT_ENUM_CAST(RenderingDevice::Limit)
  1319. VARIANT_ENUM_CAST(RenderingDevice::MemoryType)
  1320. VARIANT_ENUM_CAST(RenderingDevice::Features)
  1321. VARIANT_ENUM_CAST(RenderingDevice::BreadcrumbMarker)
  1322. VARIANT_BITFIELD_CAST(RenderingDevice::DrawFlags);
  1323. #ifndef DISABLE_DEPRECATED
  1324. VARIANT_BITFIELD_CAST(RenderingDevice::BarrierMask);
  1325. VARIANT_ENUM_CAST(RenderingDevice::InitialAction)
  1326. VARIANT_ENUM_CAST(RenderingDevice::FinalAction)
  1327. #endif
  1328. typedef RenderingDevice RD;
  1329. #endif // RENDERING_DEVICE_H