renderer_storage_rd.h 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. /*************************************************************************/
  2. /* renderer_storage_rd.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_SERVER_STORAGE_RD_H
  31. #define RENDERING_SERVER_STORAGE_RD_H
  32. #include "core/templates/list.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/rid_owner.h"
  35. #include "servers/rendering/renderer_compositor.h"
  36. #include "servers/rendering/renderer_rd/effects_rd.h"
  37. #include "servers/rendering/renderer_rd/shader_compiler_rd.h"
  38. #include "servers/rendering/renderer_rd/shaders/canvas_sdf.glsl.gen.h"
  39. #include "servers/rendering/renderer_rd/shaders/particles.glsl.gen.h"
  40. #include "servers/rendering/renderer_rd/shaders/particles_copy.glsl.gen.h"
  41. #include "servers/rendering/renderer_rd/shaders/skeleton.glsl.gen.h"
  42. #include "servers/rendering/renderer_rd/shaders/voxel_gi_sdf.glsl.gen.h"
  43. #include "servers/rendering/renderer_scene_render.h"
  44. #include "servers/rendering/rendering_device.h"
  45. class RendererStorageRD : public RendererStorage {
  46. public:
  47. static _FORCE_INLINE_ void store_transform(const Transform3D &p_mtx, float *p_array) {
  48. p_array[0] = p_mtx.basis.elements[0][0];
  49. p_array[1] = p_mtx.basis.elements[1][0];
  50. p_array[2] = p_mtx.basis.elements[2][0];
  51. p_array[3] = 0;
  52. p_array[4] = p_mtx.basis.elements[0][1];
  53. p_array[5] = p_mtx.basis.elements[1][1];
  54. p_array[6] = p_mtx.basis.elements[2][1];
  55. p_array[7] = 0;
  56. p_array[8] = p_mtx.basis.elements[0][2];
  57. p_array[9] = p_mtx.basis.elements[1][2];
  58. p_array[10] = p_mtx.basis.elements[2][2];
  59. p_array[11] = 0;
  60. p_array[12] = p_mtx.origin.x;
  61. p_array[13] = p_mtx.origin.y;
  62. p_array[14] = p_mtx.origin.z;
  63. p_array[15] = 1;
  64. }
  65. static _FORCE_INLINE_ void store_basis_3x4(const Basis &p_mtx, float *p_array) {
  66. p_array[0] = p_mtx.elements[0][0];
  67. p_array[1] = p_mtx.elements[1][0];
  68. p_array[2] = p_mtx.elements[2][0];
  69. p_array[3] = 0;
  70. p_array[4] = p_mtx.elements[0][1];
  71. p_array[5] = p_mtx.elements[1][1];
  72. p_array[6] = p_mtx.elements[2][1];
  73. p_array[7] = 0;
  74. p_array[8] = p_mtx.elements[0][2];
  75. p_array[9] = p_mtx.elements[1][2];
  76. p_array[10] = p_mtx.elements[2][2];
  77. p_array[11] = 0;
  78. }
  79. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_mtx, float *p_array) {
  80. p_array[0] = p_mtx.elements[0][0];
  81. p_array[1] = p_mtx.elements[1][0];
  82. p_array[2] = p_mtx.elements[2][0];
  83. p_array[3] = 0;
  84. p_array[4] = p_mtx.elements[0][1];
  85. p_array[5] = p_mtx.elements[1][1];
  86. p_array[6] = p_mtx.elements[2][1];
  87. p_array[7] = 0;
  88. p_array[8] = p_mtx.elements[0][2];
  89. p_array[9] = p_mtx.elements[1][2];
  90. p_array[10] = p_mtx.elements[2][2];
  91. p_array[11] = 0;
  92. }
  93. static _FORCE_INLINE_ void store_transform_transposed_3x4(const Transform3D &p_mtx, float *p_array) {
  94. p_array[0] = p_mtx.basis.elements[0][0];
  95. p_array[1] = p_mtx.basis.elements[0][1];
  96. p_array[2] = p_mtx.basis.elements[0][2];
  97. p_array[3] = p_mtx.origin.x;
  98. p_array[4] = p_mtx.basis.elements[1][0];
  99. p_array[5] = p_mtx.basis.elements[1][1];
  100. p_array[6] = p_mtx.basis.elements[1][2];
  101. p_array[7] = p_mtx.origin.y;
  102. p_array[8] = p_mtx.basis.elements[2][0];
  103. p_array[9] = p_mtx.basis.elements[2][1];
  104. p_array[10] = p_mtx.basis.elements[2][2];
  105. p_array[11] = p_mtx.origin.z;
  106. }
  107. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  108. for (int i = 0; i < 4; i++) {
  109. for (int j = 0; j < 4; j++) {
  110. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  111. }
  112. }
  113. }
  114. static _FORCE_INLINE_ void store_soft_shadow_kernel(const float *p_kernel, float *p_array) {
  115. for (int i = 0; i < 128; i++) {
  116. p_array[i] = p_kernel[i];
  117. }
  118. }
  119. enum ShaderType {
  120. SHADER_TYPE_2D,
  121. SHADER_TYPE_3D,
  122. SHADER_TYPE_PARTICLES,
  123. SHADER_TYPE_SKY,
  124. SHADER_TYPE_FOG,
  125. SHADER_TYPE_MAX
  126. };
  127. struct ShaderData {
  128. virtual void set_code(const String &p_Code) = 0;
  129. virtual void set_default_texture_param(const StringName &p_name, RID p_texture) = 0;
  130. virtual void get_param_list(List<PropertyInfo> *p_param_list) const = 0;
  131. virtual void get_instance_param_list(List<InstanceShaderParam> *p_param_list) const = 0;
  132. virtual bool is_param_texture(const StringName &p_param) const = 0;
  133. virtual bool is_animated() const = 0;
  134. virtual bool casts_shadows() const = 0;
  135. virtual Variant get_default_parameter(const StringName &p_parameter) const = 0;
  136. virtual RS::ShaderNativeSourceCode get_native_source_code() const { return RS::ShaderNativeSourceCode(); }
  137. virtual ~ShaderData() {}
  138. };
  139. typedef ShaderData *(*ShaderDataRequestFunction)();
  140. struct MaterialData {
  141. void update_uniform_buffer(const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Map<StringName, Variant> &p_parameters, uint8_t *p_buffer, uint32_t p_buffer_size, bool p_use_linear_color);
  142. void update_textures(const Map<StringName, Variant> &p_parameters, const Map<StringName, RID> &p_default_textures, const Vector<ShaderCompilerRD::GeneratedCode::Texture> &p_texture_uniforms, RID *p_textures, bool p_use_linear_color);
  143. virtual void set_render_priority(int p_priority) = 0;
  144. virtual void set_next_pass(RID p_pass) = 0;
  145. virtual bool update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) = 0;
  146. virtual ~MaterialData();
  147. //to be used internally by update_parameters, in the most common configuration of material parameters
  148. bool update_parameters_uniform_set(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty, const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Vector<ShaderCompilerRD::GeneratedCode::Texture> &p_texture_uniforms, const Map<StringName, RID> &p_default_texture_params, uint32_t p_ubo_size, RID &uniform_set, RID p_shader, uint32_t p_shader_uniform_set, uint32_t p_barrier = RD::BARRIER_MASK_ALL);
  149. void free_parameters_uniform_set(RID p_uniform_set);
  150. private:
  151. friend class RendererStorageRD;
  152. RID self;
  153. List<RID>::Element *global_buffer_E = nullptr;
  154. List<RID>::Element *global_texture_E = nullptr;
  155. uint64_t global_textures_pass = 0;
  156. Map<StringName, uint64_t> used_global_textures;
  157. //internally by update_parameters_uniform_set
  158. Vector<uint8_t> ubo_data;
  159. RID uniform_buffer;
  160. Vector<RID> texture_cache;
  161. };
  162. typedef MaterialData *(*MaterialDataRequestFunction)(ShaderData *);
  163. static void _material_uniform_set_erased(const RID &p_set, void *p_material);
  164. enum DefaultRDTexture {
  165. DEFAULT_RD_TEXTURE_WHITE,
  166. DEFAULT_RD_TEXTURE_BLACK,
  167. DEFAULT_RD_TEXTURE_NORMAL,
  168. DEFAULT_RD_TEXTURE_ANISO,
  169. DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER,
  170. DEFAULT_RD_TEXTURE_CUBEMAP_BLACK,
  171. DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK,
  172. DEFAULT_RD_TEXTURE_CUBEMAP_WHITE,
  173. DEFAULT_RD_TEXTURE_3D_WHITE,
  174. DEFAULT_RD_TEXTURE_3D_BLACK,
  175. DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE,
  176. DEFAULT_RD_TEXTURE_2D_UINT,
  177. DEFAULT_RD_TEXTURE_MAX
  178. };
  179. enum DefaultRDBuffer {
  180. DEFAULT_RD_BUFFER_VERTEX,
  181. DEFAULT_RD_BUFFER_NORMAL,
  182. DEFAULT_RD_BUFFER_TANGENT,
  183. DEFAULT_RD_BUFFER_COLOR,
  184. DEFAULT_RD_BUFFER_TEX_UV,
  185. DEFAULT_RD_BUFFER_TEX_UV2,
  186. DEFAULT_RD_BUFFER_CUSTOM0,
  187. DEFAULT_RD_BUFFER_CUSTOM1,
  188. DEFAULT_RD_BUFFER_CUSTOM2,
  189. DEFAULT_RD_BUFFER_CUSTOM3,
  190. DEFAULT_RD_BUFFER_BONES,
  191. DEFAULT_RD_BUFFER_WEIGHTS,
  192. DEFAULT_RD_BUFFER_MAX,
  193. };
  194. private:
  195. /* CANVAS TEXTURE API (2D) */
  196. struct CanvasTexture {
  197. RID diffuse;
  198. RID normal_map;
  199. RID specular;
  200. Color specular_color = Color(1, 1, 1, 1);
  201. float shininess = 1.0;
  202. RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  203. RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  204. RID uniform_sets[RS::CANVAS_ITEM_TEXTURE_FILTER_MAX][RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX];
  205. Size2i size_cache = Size2i(1, 1);
  206. bool use_normal_cache = false;
  207. bool use_specular_cache = false;
  208. bool cleared_cache = true;
  209. void clear_sets();
  210. ~CanvasTexture();
  211. };
  212. RID_Owner<CanvasTexture, true> canvas_texture_owner;
  213. /* TEXTURE API */
  214. struct Texture {
  215. enum Type {
  216. TYPE_2D,
  217. TYPE_LAYERED,
  218. TYPE_3D
  219. };
  220. Type type;
  221. RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;
  222. RenderingDevice::TextureType rd_type;
  223. RID rd_texture;
  224. RID rd_texture_srgb;
  225. RenderingDevice::DataFormat rd_format;
  226. RenderingDevice::DataFormat rd_format_srgb;
  227. RD::TextureView rd_view;
  228. Image::Format format;
  229. Image::Format validated_format;
  230. int width;
  231. int height;
  232. int depth;
  233. int layers;
  234. int mipmaps;
  235. int height_2d;
  236. int width_2d;
  237. struct BufferSlice3D {
  238. Size2i size;
  239. uint32_t offset = 0;
  240. uint32_t buffer_size = 0;
  241. };
  242. Vector<BufferSlice3D> buffer_slices_3d;
  243. uint32_t buffer_size_3d = 0;
  244. bool is_render_target;
  245. bool is_proxy;
  246. Ref<Image> image_cache_2d;
  247. String path;
  248. RID proxy_to;
  249. Vector<RID> proxies;
  250. Set<RID> lightmap_users;
  251. RS::TextureDetectCallback detect_3d_callback = nullptr;
  252. void *detect_3d_callback_ud = nullptr;
  253. RS::TextureDetectCallback detect_normal_callback = nullptr;
  254. void *detect_normal_callback_ud = nullptr;
  255. RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;
  256. void *detect_roughness_callback_ud = nullptr;
  257. CanvasTexture *canvas_texture = nullptr;
  258. };
  259. struct TextureToRDFormat {
  260. RD::DataFormat format;
  261. RD::DataFormat format_srgb;
  262. RD::TextureSwizzle swizzle_r;
  263. RD::TextureSwizzle swizzle_g;
  264. RD::TextureSwizzle swizzle_b;
  265. RD::TextureSwizzle swizzle_a;
  266. TextureToRDFormat() {
  267. format = RD::DATA_FORMAT_MAX;
  268. format_srgb = RD::DATA_FORMAT_MAX;
  269. swizzle_r = RD::TEXTURE_SWIZZLE_R;
  270. swizzle_g = RD::TEXTURE_SWIZZLE_G;
  271. swizzle_b = RD::TEXTURE_SWIZZLE_B;
  272. swizzle_a = RD::TEXTURE_SWIZZLE_A;
  273. }
  274. };
  275. //textures can be created from threads, so this RID_Owner is thread safe
  276. mutable RID_Owner<Texture, true> texture_owner;
  277. Ref<Image> _validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format);
  278. RID default_rd_textures[DEFAULT_RD_TEXTURE_MAX];
  279. RID default_rd_samplers[RS::CANVAS_ITEM_TEXTURE_FILTER_MAX][RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX];
  280. RID default_rd_storage_buffer;
  281. /* DECAL ATLAS */
  282. struct DecalAtlas {
  283. struct Texture {
  284. int panorama_to_dp_users;
  285. int users;
  286. Rect2 uv_rect;
  287. };
  288. struct SortItem {
  289. RID texture;
  290. Size2i pixel_size;
  291. Size2i size;
  292. Point2i pos;
  293. bool operator<(const SortItem &p_item) const {
  294. //sort larger to smaller
  295. if (size.height == p_item.size.height) {
  296. return size.width > p_item.size.width;
  297. } else {
  298. return size.height > p_item.size.height;
  299. }
  300. }
  301. };
  302. HashMap<RID, Texture> textures;
  303. bool dirty = true;
  304. int mipmaps = 5;
  305. RID texture;
  306. RID texture_srgb;
  307. struct MipMap {
  308. RID fb;
  309. RID texture;
  310. Size2i size;
  311. };
  312. Vector<MipMap> texture_mipmaps;
  313. Size2i size;
  314. } decal_atlas;
  315. void _update_decal_atlas();
  316. /* SHADER */
  317. struct Material;
  318. struct Shader {
  319. ShaderData *data;
  320. String code;
  321. ShaderType type;
  322. Map<StringName, RID> default_texture_parameter;
  323. Set<Material *> owners;
  324. };
  325. ShaderDataRequestFunction shader_data_request_func[SHADER_TYPE_MAX];
  326. mutable RID_Owner<Shader, true> shader_owner;
  327. /* Material */
  328. struct Material {
  329. RID self;
  330. MaterialData *data = nullptr;
  331. Shader *shader = nullptr;
  332. //shortcut to shader data and type
  333. ShaderType shader_type = SHADER_TYPE_MAX;
  334. uint32_t shader_id = 0;
  335. bool uniform_dirty = false;
  336. bool texture_dirty = false;
  337. Map<StringName, Variant> params;
  338. int32_t priority = 0;
  339. RID next_pass;
  340. SelfList<Material> update_element;
  341. Dependency dependency;
  342. Material() :
  343. update_element(this) {}
  344. };
  345. MaterialDataRequestFunction material_data_request_func[SHADER_TYPE_MAX];
  346. mutable RID_Owner<Material, true> material_owner;
  347. SelfList<Material>::List material_update_list;
  348. void _material_queue_update(Material *material, bool p_uniform, bool p_texture);
  349. void _update_queued_materials();
  350. /* Mesh */
  351. struct MeshInstance;
  352. struct Mesh {
  353. struct Surface {
  354. RS::PrimitiveType primitive = RS::PRIMITIVE_POINTS;
  355. uint32_t format = 0;
  356. RID vertex_buffer;
  357. RID attribute_buffer;
  358. RID skin_buffer;
  359. uint32_t vertex_count = 0;
  360. uint32_t vertex_buffer_size = 0;
  361. uint32_t skin_buffer_size = 0;
  362. // A different pipeline needs to be allocated
  363. // depending on the inputs available in the
  364. // material.
  365. // There are never that many geometry/material
  366. // combinations, so a simple array is the most
  367. // cache-efficient structure.
  368. struct Version {
  369. uint32_t input_mask = 0;
  370. RD::VertexFormatID vertex_format = 0;
  371. RID vertex_array;
  372. };
  373. SpinLock version_lock; //needed to access versions
  374. Version *versions = nullptr; //allocated on demand
  375. uint32_t version_count = 0;
  376. RID index_buffer;
  377. RID index_array;
  378. uint32_t index_count = 0;
  379. struct LOD {
  380. float edge_length = 0.0;
  381. uint32_t index_count = 0;
  382. RID index_buffer;
  383. RID index_array;
  384. };
  385. LOD *lods = nullptr;
  386. uint32_t lod_count = 0;
  387. AABB aabb;
  388. Vector<AABB> bone_aabbs;
  389. RID blend_shape_buffer;
  390. RID material;
  391. uint32_t render_index = 0;
  392. uint64_t render_pass = 0;
  393. uint32_t multimesh_render_index = 0;
  394. uint64_t multimesh_render_pass = 0;
  395. uint32_t particles_render_index = 0;
  396. uint64_t particles_render_pass = 0;
  397. RID uniform_set;
  398. };
  399. uint32_t blend_shape_count = 0;
  400. RS::BlendShapeMode blend_shape_mode = RS::BLEND_SHAPE_MODE_NORMALIZED;
  401. Surface **surfaces = nullptr;
  402. uint32_t surface_count = 0;
  403. Vector<AABB> bone_aabbs;
  404. bool has_bone_weights = false;
  405. AABB aabb;
  406. AABB custom_aabb;
  407. Vector<RID> material_cache;
  408. List<MeshInstance *> instances;
  409. RID shadow_mesh;
  410. Set<Mesh *> shadow_owners;
  411. Dependency dependency;
  412. };
  413. mutable RID_Owner<Mesh, true> mesh_owner;
  414. struct MeshInstance {
  415. Mesh *mesh;
  416. RID skeleton;
  417. struct Surface {
  418. RID vertex_buffer;
  419. RID uniform_set;
  420. Mesh::Surface::Version *versions = nullptr; //allocated on demand
  421. uint32_t version_count = 0;
  422. };
  423. LocalVector<Surface> surfaces;
  424. LocalVector<float> blend_weights;
  425. RID blend_weights_buffer;
  426. List<MeshInstance *>::Element *I = nullptr; //used to erase itself
  427. uint64_t skeleton_version = 0;
  428. bool dirty = false;
  429. bool weights_dirty = false;
  430. SelfList<MeshInstance> weight_update_list;
  431. SelfList<MeshInstance> array_update_list;
  432. MeshInstance() :
  433. weight_update_list(this), array_update_list(this) {}
  434. };
  435. void _mesh_instance_clear(MeshInstance *mi);
  436. void _mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface);
  437. mutable RID_Owner<MeshInstance> mesh_instance_owner;
  438. SelfList<MeshInstance>::List dirty_mesh_instance_weights;
  439. SelfList<MeshInstance>::List dirty_mesh_instance_arrays;
  440. struct SkeletonShader {
  441. struct PushConstant {
  442. uint32_t has_normal;
  443. uint32_t has_tangent;
  444. uint32_t has_skeleton;
  445. uint32_t has_blend_shape;
  446. uint32_t vertex_count;
  447. uint32_t vertex_stride;
  448. uint32_t skin_stride;
  449. uint32_t skin_weight_offset;
  450. uint32_t blend_shape_count;
  451. uint32_t normalized_blend_shapes;
  452. uint32_t pad0;
  453. uint32_t pad1;
  454. };
  455. enum {
  456. UNIFORM_SET_INSTANCE = 0,
  457. UNIFORM_SET_SURFACE = 1,
  458. UNIFORM_SET_SKELETON = 2,
  459. };
  460. enum {
  461. SHADER_MODE_2D,
  462. SHADER_MODE_3D,
  463. SHADER_MODE_MAX
  464. };
  465. SkeletonShaderRD shader;
  466. RID version;
  467. RID version_shader[SHADER_MODE_MAX];
  468. RID pipeline[SHADER_MODE_MAX];
  469. RID default_skeleton_uniform_set;
  470. } skeleton_shader;
  471. void _mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint32_t p_input_mask, MeshInstance::Surface *mis = nullptr);
  472. RID mesh_default_rd_buffers[DEFAULT_RD_BUFFER_MAX];
  473. /* MultiMesh */
  474. struct MultiMesh {
  475. RID mesh;
  476. int instances = 0;
  477. RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D;
  478. bool uses_colors = false;
  479. bool uses_custom_data = false;
  480. int visible_instances = -1;
  481. AABB aabb;
  482. bool aabb_dirty = false;
  483. bool buffer_set = false;
  484. uint32_t stride_cache = 0;
  485. uint32_t color_offset_cache = 0;
  486. uint32_t custom_data_offset_cache = 0;
  487. Vector<float> data_cache; //used if individual setting is used
  488. bool *data_cache_dirty_regions = nullptr;
  489. uint32_t data_cache_used_dirty_regions = 0;
  490. RID buffer; //storage buffer
  491. RID uniform_set_3d;
  492. RID uniform_set_2d;
  493. bool dirty = false;
  494. MultiMesh *dirty_list = nullptr;
  495. Dependency dependency;
  496. };
  497. mutable RID_Owner<MultiMesh, true> multimesh_owner;
  498. MultiMesh *multimesh_dirty_list = nullptr;
  499. _FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const;
  500. _FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb);
  501. _FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb);
  502. _FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances);
  503. void _update_dirty_multimeshes();
  504. /* PARTICLES */
  505. struct ParticleData {
  506. float xform[16];
  507. float velocity[3];
  508. uint32_t active;
  509. float color[4];
  510. float custom[3];
  511. float lifetime;
  512. };
  513. struct ParticlesFrameParams {
  514. enum {
  515. MAX_ATTRACTORS = 32,
  516. MAX_COLLIDERS = 32,
  517. MAX_3D_TEXTURES = 7
  518. };
  519. enum AttractorType {
  520. ATTRACTOR_TYPE_SPHERE,
  521. ATTRACTOR_TYPE_BOX,
  522. ATTRACTOR_TYPE_VECTOR_FIELD,
  523. };
  524. struct Attractor {
  525. float transform[16];
  526. float extents[3]; //exents or radius
  527. uint32_t type;
  528. uint32_t texture_index; //texture index for vector field
  529. float strength;
  530. float attenuation;
  531. float directionality;
  532. };
  533. enum CollisionType {
  534. COLLISION_TYPE_SPHERE,
  535. COLLISION_TYPE_BOX,
  536. COLLISION_TYPE_SDF,
  537. COLLISION_TYPE_HEIGHT_FIELD,
  538. COLLISION_TYPE_2D_SDF,
  539. };
  540. struct Collider {
  541. float transform[16];
  542. float extents[3]; //exents or radius
  543. uint32_t type;
  544. uint32_t texture_index; //texture index for vector field
  545. real_t scale;
  546. uint32_t pad[2];
  547. };
  548. uint32_t emitting;
  549. float system_phase;
  550. float prev_system_phase;
  551. uint32_t cycle;
  552. real_t explosiveness;
  553. real_t randomness;
  554. float time;
  555. float delta;
  556. uint32_t frame;
  557. uint32_t pad0;
  558. uint32_t pad1;
  559. uint32_t pad2;
  560. uint32_t random_seed;
  561. uint32_t attractor_count;
  562. uint32_t collider_count;
  563. float particle_size;
  564. float emission_transform[16];
  565. Attractor attractors[MAX_ATTRACTORS];
  566. Collider colliders[MAX_COLLIDERS];
  567. };
  568. struct ParticleEmissionBufferData {
  569. };
  570. struct ParticleEmissionBuffer {
  571. struct Data {
  572. float xform[16];
  573. float velocity[3];
  574. uint32_t flags;
  575. float color[4];
  576. float custom[4];
  577. };
  578. int32_t particle_count;
  579. int32_t particle_max;
  580. uint32_t pad1;
  581. uint32_t pad2;
  582. Data data[1]; //its 2020 and empty arrays are still non standard in C++
  583. };
  584. struct Particles {
  585. RS::ParticlesMode mode = RS::PARTICLES_MODE_3D;
  586. bool inactive = true;
  587. double inactive_time = 0.0;
  588. bool emitting = false;
  589. bool one_shot = false;
  590. int amount = 0;
  591. double lifetime = 1.0;
  592. double pre_process_time = 0.0;
  593. real_t explosiveness = 0.0;
  594. real_t randomness = 0.0;
  595. bool restart_request = false;
  596. AABB custom_aabb = AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8));
  597. bool use_local_coords = true;
  598. bool has_collision_cache = false;
  599. bool has_sdf_collision = false;
  600. Transform2D sdf_collision_transform;
  601. Rect2 sdf_collision_to_screen;
  602. RID sdf_collision_texture;
  603. RID process_material;
  604. uint32_t frame_counter = 0;
  605. RS::ParticlesTransformAlign transform_align = RS::PARTICLES_TRANSFORM_ALIGN_DISABLED;
  606. RS::ParticlesDrawOrder draw_order = RS::PARTICLES_DRAW_ORDER_INDEX;
  607. Vector<RID> draw_passes;
  608. Vector<Transform3D> trail_bind_poses;
  609. bool trail_bind_poses_dirty = false;
  610. RID trail_bind_pose_buffer;
  611. RID trail_bind_pose_uniform_set;
  612. RID particle_buffer;
  613. RID particle_instance_buffer;
  614. RID frame_params_buffer;
  615. RID particles_material_uniform_set;
  616. RID particles_copy_uniform_set;
  617. RID particles_transforms_buffer_uniform_set;
  618. RID collision_textures_uniform_set;
  619. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  620. uint32_t collision_3d_textures_used = 0;
  621. RID collision_heightmap_texture;
  622. RID particles_sort_buffer;
  623. RID particles_sort_uniform_set;
  624. bool dirty = false;
  625. Particles *update_list = nullptr;
  626. RID sub_emitter;
  627. double phase = 0.0;
  628. double prev_phase = 0.0;
  629. uint64_t prev_ticks = 0;
  630. uint32_t random_seed = 0;
  631. uint32_t cycle_number = 0;
  632. double speed_scale = 1.0;
  633. int fixed_fps = 30;
  634. bool interpolate = true;
  635. bool fractional_delta = false;
  636. double frame_remainder = 0;
  637. real_t collision_base_size = 0.01;
  638. bool clear = true;
  639. bool force_sub_emit = false;
  640. Transform3D emission_transform;
  641. Vector<uint8_t> emission_buffer_data;
  642. ParticleEmissionBuffer *emission_buffer = nullptr;
  643. RID emission_storage_buffer;
  644. Set<RID> collisions;
  645. Dependency dependency;
  646. double trail_length = 1.0;
  647. bool trails_enabled = false;
  648. LocalVector<ParticlesFrameParams> frame_history;
  649. LocalVector<ParticlesFrameParams> trail_params;
  650. Particles() {
  651. }
  652. };
  653. void _particles_process(Particles *p_particles, double p_delta);
  654. void _particles_allocate_emission_buffer(Particles *particles);
  655. void _particles_free_data(Particles *particles);
  656. void _particles_update_buffers(Particles *particles);
  657. struct ParticlesShader {
  658. struct PushConstant {
  659. float lifetime;
  660. uint32_t clear;
  661. uint32_t total_particles;
  662. uint32_t trail_size;
  663. uint32_t use_fractional_delta;
  664. uint32_t sub_emitter_mode;
  665. uint32_t can_emit;
  666. uint32_t trail_pass;
  667. };
  668. ParticlesShaderRD shader;
  669. ShaderCompilerRD compiler;
  670. RID default_shader;
  671. RID default_material;
  672. RID default_shader_rd;
  673. RID base_uniform_set;
  674. struct CopyPushConstant {
  675. float sort_direction[3];
  676. uint32_t total_particles;
  677. uint32_t trail_size;
  678. uint32_t trail_total;
  679. float frame_delta;
  680. float frame_remainder;
  681. float align_up[3];
  682. uint32_t align_mode;
  683. uint32_t order_by_lifetime;
  684. uint32_t lifetime_split;
  685. uint32_t lifetime_reverse;
  686. uint32_t pad;
  687. };
  688. enum {
  689. COPY_MODE_FILL_INSTANCES,
  690. COPY_MODE_FILL_INSTANCES_2D,
  691. COPY_MODE_FILL_SORT_BUFFER,
  692. COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER,
  693. COPY_MODE_MAX,
  694. };
  695. ParticlesCopyShaderRD copy_shader;
  696. RID copy_shader_version;
  697. RID copy_pipelines[COPY_MODE_MAX];
  698. LocalVector<float> pose_update_buffer;
  699. } particles_shader;
  700. Particles *particle_update_list = nullptr;
  701. struct ParticlesShaderData : public ShaderData {
  702. bool valid;
  703. RID version;
  704. bool uses_collision = false;
  705. //PipelineCacheRD pipelines[SKY_VERSION_MAX];
  706. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  707. Vector<ShaderCompilerRD::GeneratedCode::Texture> texture_uniforms;
  708. Vector<uint32_t> ubo_offsets;
  709. uint32_t ubo_size;
  710. String path;
  711. String code;
  712. Map<StringName, RID> default_texture_params;
  713. RID pipeline;
  714. bool uses_time;
  715. virtual void set_code(const String &p_Code);
  716. virtual void set_default_texture_param(const StringName &p_name, RID p_texture);
  717. virtual void get_param_list(List<PropertyInfo> *p_param_list) const;
  718. virtual void get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const;
  719. virtual bool is_param_texture(const StringName &p_param) const;
  720. virtual bool is_animated() const;
  721. virtual bool casts_shadows() const;
  722. virtual Variant get_default_parameter(const StringName &p_parameter) const;
  723. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  724. ParticlesShaderData();
  725. virtual ~ParticlesShaderData();
  726. };
  727. ShaderData *_create_particles_shader_func();
  728. static RendererStorageRD::ShaderData *_create_particles_shader_funcs() {
  729. return base_singleton->_create_particles_shader_func();
  730. }
  731. struct ParticlesMaterialData : public MaterialData {
  732. uint64_t last_frame = 0;
  733. ParticlesShaderData *shader_data = nullptr;
  734. RID uniform_set;
  735. bool uniform_set_updated = false;
  736. virtual void set_render_priority(int p_priority) {}
  737. virtual void set_next_pass(RID p_pass) {}
  738. virtual bool update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  739. virtual ~ParticlesMaterialData();
  740. };
  741. MaterialData *_create_particles_material_func(ParticlesShaderData *p_shader);
  742. static RendererStorageRD::MaterialData *_create_particles_material_funcs(ShaderData *p_shader) {
  743. return base_singleton->_create_particles_material_func(static_cast<ParticlesShaderData *>(p_shader));
  744. }
  745. void update_particles();
  746. mutable RID_Owner<Particles, true> particles_owner;
  747. /* Particles Collision */
  748. struct ParticlesCollision {
  749. RS::ParticlesCollisionType type = RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT;
  750. uint32_t cull_mask = 0xFFFFFFFF;
  751. float radius = 1.0;
  752. Vector3 extents = Vector3(1, 1, 1);
  753. float attractor_strength = 1.0;
  754. float attractor_attenuation = 1.0;
  755. float attractor_directionality = 0.0;
  756. RID field_texture;
  757. RID heightfield_texture;
  758. RID heightfield_fb;
  759. Size2i heightfield_fb_size;
  760. RS::ParticlesCollisionHeightfieldResolution heightfield_resolution = RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_1024;
  761. Dependency dependency;
  762. };
  763. mutable RID_Owner<ParticlesCollision, true> particles_collision_owner;
  764. struct ParticlesCollisionInstance {
  765. RID collision;
  766. Transform3D transform;
  767. bool active = false;
  768. };
  769. mutable RID_Owner<ParticlesCollisionInstance> particles_collision_instance_owner;
  770. /* FOG VOLUMES */
  771. struct FogVolume {
  772. RID material;
  773. Vector3 extents = Vector3(1, 1, 1);
  774. RS::FogVolumeShape shape = RS::FOG_VOLUME_SHAPE_BOX;
  775. Dependency dependency;
  776. };
  777. mutable RID_Owner<FogVolume, true> fog_volume_owner;
  778. /* visibility_notifier */
  779. struct VisibilityNotifier {
  780. AABB aabb;
  781. Callable enter_callback;
  782. Callable exit_callback;
  783. Dependency dependency;
  784. };
  785. mutable RID_Owner<VisibilityNotifier> visibility_notifier_owner;
  786. /* Skeleton */
  787. struct Skeleton {
  788. bool use_2d = false;
  789. int size = 0;
  790. Vector<float> data;
  791. RID buffer;
  792. bool dirty = false;
  793. Skeleton *dirty_list = nullptr;
  794. Transform2D base_transform_2d;
  795. RID uniform_set_3d;
  796. RID uniform_set_mi;
  797. uint64_t version = 1;
  798. Dependency dependency;
  799. };
  800. mutable RID_Owner<Skeleton, true> skeleton_owner;
  801. _FORCE_INLINE_ void _skeleton_make_dirty(Skeleton *skeleton);
  802. Skeleton *skeleton_dirty_list = nullptr;
  803. void _update_dirty_skeletons();
  804. /* LIGHT */
  805. struct Light {
  806. RS::LightType type;
  807. float param[RS::LIGHT_PARAM_MAX];
  808. Color color = Color(1, 1, 1, 1);
  809. Color shadow_color;
  810. RID projector;
  811. bool shadow = false;
  812. bool negative = false;
  813. bool reverse_cull = false;
  814. RS::LightBakeMode bake_mode = RS::LIGHT_BAKE_DYNAMIC;
  815. uint32_t max_sdfgi_cascade = 2;
  816. uint32_t cull_mask = 0xFFFFFFFF;
  817. RS::LightOmniShadowMode omni_shadow_mode = RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  818. RS::LightDirectionalShadowMode directional_shadow_mode = RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  819. bool directional_blend_splits = false;
  820. bool directional_sky_only = false;
  821. uint64_t version = 0;
  822. Dependency dependency;
  823. };
  824. mutable RID_Owner<Light, true> light_owner;
  825. /* REFLECTION PROBE */
  826. struct ReflectionProbe {
  827. RS::ReflectionProbeUpdateMode update_mode = RS::REFLECTION_PROBE_UPDATE_ONCE;
  828. int resolution = 256;
  829. float intensity = 1.0;
  830. RS::ReflectionProbeAmbientMode ambient_mode = RS::REFLECTION_PROBE_AMBIENT_ENVIRONMENT;
  831. Color ambient_color;
  832. float ambient_color_energy = 1.0;
  833. float max_distance = 0;
  834. Vector3 extents = Vector3(1, 1, 1);
  835. Vector3 origin_offset;
  836. bool interior = false;
  837. bool box_projection = false;
  838. bool enable_shadows = false;
  839. uint32_t cull_mask = (1 << 20) - 1;
  840. float lod_threshold = 0.01;
  841. Dependency dependency;
  842. };
  843. mutable RID_Owner<ReflectionProbe, true> reflection_probe_owner;
  844. /* DECAL */
  845. struct Decal {
  846. Vector3 extents = Vector3(1, 1, 1);
  847. RID textures[RS::DECAL_TEXTURE_MAX];
  848. float emission_energy = 1.0;
  849. float albedo_mix = 1.0;
  850. Color modulate = Color(1, 1, 1, 1);
  851. uint32_t cull_mask = (1 << 20) - 1;
  852. float upper_fade = 0.3;
  853. float lower_fade = 0.3;
  854. bool distance_fade = false;
  855. float distance_fade_begin = 10;
  856. float distance_fade_length = 1;
  857. float normal_fade = 0.0;
  858. Dependency dependency;
  859. };
  860. mutable RID_Owner<Decal, true> decal_owner;
  861. /* VOXEL GI */
  862. struct VoxelGI {
  863. RID octree_buffer;
  864. RID data_buffer;
  865. RID sdf_texture;
  866. uint32_t octree_buffer_size = 0;
  867. uint32_t data_buffer_size = 0;
  868. Vector<int> level_counts;
  869. int cell_count = 0;
  870. Transform3D to_cell_xform;
  871. AABB bounds;
  872. Vector3i octree_size;
  873. float dynamic_range = 4.0;
  874. float energy = 1.0;
  875. float bias = 1.4;
  876. float normal_bias = 0.0;
  877. float propagation = 0.7;
  878. bool interior = false;
  879. bool use_two_bounces = false;
  880. float anisotropy_strength = 0.5;
  881. uint32_t version = 1;
  882. uint32_t data_version = 1;
  883. Dependency dependency;
  884. };
  885. mutable RID_Owner<VoxelGI, true> voxel_gi_owner;
  886. /* REFLECTION PROBE */
  887. struct Lightmap {
  888. RID light_texture;
  889. bool uses_spherical_harmonics = false;
  890. bool interior = false;
  891. AABB bounds = AABB(Vector3(), Vector3(1, 1, 1));
  892. int32_t array_index = -1; //unassigned
  893. PackedVector3Array points;
  894. PackedColorArray point_sh;
  895. PackedInt32Array tetrahedra;
  896. PackedInt32Array bsp_tree;
  897. struct BSP {
  898. static const int32_t EMPTY_LEAF = INT32_MIN;
  899. float plane[4];
  900. int32_t over = EMPTY_LEAF, under = EMPTY_LEAF;
  901. };
  902. Dependency dependency;
  903. };
  904. bool using_lightmap_array; //high end uses this
  905. /* for high end */
  906. Vector<RID> lightmap_textures;
  907. uint64_t lightmap_array_version = 0;
  908. mutable RID_Owner<Lightmap, true> lightmap_owner;
  909. float lightmap_probe_capture_update_speed = 4;
  910. /* RENDER TARGET */
  911. struct RenderTarget {
  912. Size2i size;
  913. uint32_t view_count;
  914. RID framebuffer;
  915. RID color;
  916. //used for retrieving from CPU
  917. RD::DataFormat color_format = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  918. RD::DataFormat color_format_srgb = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  919. Image::Format image_format = Image::FORMAT_L8;
  920. bool flags[RENDER_TARGET_FLAG_MAX];
  921. bool sdf_enabled = false;
  922. RID backbuffer; //used for effects
  923. RID backbuffer_fb;
  924. RID backbuffer_mipmap0;
  925. struct BackbufferMipmap {
  926. RID mipmap;
  927. RID mipmap_copy;
  928. };
  929. Vector<BackbufferMipmap> backbuffer_mipmaps;
  930. RID framebuffer_uniform_set;
  931. RID backbuffer_uniform_set;
  932. RID sdf_buffer_write;
  933. RID sdf_buffer_write_fb;
  934. RID sdf_buffer_process[2];
  935. RID sdf_buffer_read;
  936. RID sdf_buffer_process_uniform_sets[2];
  937. RS::ViewportSDFOversize sdf_oversize = RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT;
  938. RS::ViewportSDFScale sdf_scale = RS::VIEWPORT_SDF_SCALE_50_PERCENT;
  939. Size2i process_size;
  940. //texture generated for this owner (nor RD).
  941. RID texture;
  942. bool was_used;
  943. //clear request
  944. bool clear_requested;
  945. Color clear_color;
  946. };
  947. mutable RID_Owner<RenderTarget> render_target_owner;
  948. void _clear_render_target(RenderTarget *rt);
  949. void _update_render_target(RenderTarget *rt);
  950. void _create_render_target_backbuffer(RenderTarget *rt);
  951. void _render_target_allocate_sdf(RenderTarget *rt);
  952. void _render_target_clear_sdf(RenderTarget *rt);
  953. Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;
  954. struct RenderTargetSDF {
  955. enum {
  956. SHADER_LOAD,
  957. SHADER_LOAD_SHRINK,
  958. SHADER_PROCESS,
  959. SHADER_PROCESS_OPTIMIZED,
  960. SHADER_STORE,
  961. SHADER_STORE_SHRINK,
  962. SHADER_MAX
  963. };
  964. struct PushConstant {
  965. int32_t size[2];
  966. int32_t stride;
  967. int32_t shift;
  968. int32_t base_size[2];
  969. int32_t pad[2];
  970. };
  971. CanvasSdfShaderRD shader;
  972. RID shader_version;
  973. RID pipelines[SHADER_MAX];
  974. } rt_sdf;
  975. /* GLOBAL SHADER VARIABLES */
  976. struct GlobalVariables {
  977. enum {
  978. BUFFER_DIRTY_REGION_SIZE = 1024
  979. };
  980. struct Variable {
  981. Set<RID> texture_materials; // materials using this
  982. RS::GlobalVariableType type;
  983. Variant value;
  984. Variant override;
  985. int32_t buffer_index; //for vectors
  986. int32_t buffer_elements; //for vectors
  987. };
  988. HashMap<StringName, Variable> variables;
  989. struct Value {
  990. float x;
  991. float y;
  992. float z;
  993. float w;
  994. };
  995. struct ValueInt {
  996. int32_t x;
  997. int32_t y;
  998. int32_t z;
  999. int32_t w;
  1000. };
  1001. struct ValueUInt {
  1002. uint32_t x;
  1003. uint32_t y;
  1004. uint32_t z;
  1005. uint32_t w;
  1006. };
  1007. struct ValueUsage {
  1008. uint32_t elements = 0;
  1009. };
  1010. List<RID> materials_using_buffer;
  1011. List<RID> materials_using_texture;
  1012. RID buffer;
  1013. Value *buffer_values;
  1014. ValueUsage *buffer_usage;
  1015. bool *buffer_dirty_regions;
  1016. uint32_t buffer_dirty_region_count = 0;
  1017. uint32_t buffer_size;
  1018. bool must_update_texture_materials = false;
  1019. bool must_update_buffer_materials = false;
  1020. HashMap<RID, int32_t> instance_buffer_pos;
  1021. } global_variables;
  1022. int32_t _global_variable_allocate(uint32_t p_elements);
  1023. void _global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value);
  1024. void _global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements);
  1025. void _update_global_variables();
  1026. /* EFFECTS */
  1027. EffectsRD *effects = nullptr;
  1028. public:
  1029. virtual bool can_create_resources_async() const;
  1030. /* TEXTURE API */
  1031. virtual RID texture_allocate();
  1032. virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image);
  1033. virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type);
  1034. virtual void texture_3d_initialize(RID p_texture, Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data); //all slices, then all the mipmaps, must be coherent
  1035. virtual void texture_proxy_initialize(RID p_texture, RID p_base);
  1036. virtual void _texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate);
  1037. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  1038. virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data);
  1039. virtual void texture_proxy_update(RID p_texture, RID p_proxy_to);
  1040. //these two APIs can be used together or in combination with the others.
  1041. virtual void texture_2d_placeholder_initialize(RID p_texture);
  1042. virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type);
  1043. virtual void texture_3d_placeholder_initialize(RID p_texture);
  1044. virtual Ref<Image> texture_2d_get(RID p_texture) const;
  1045. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const;
  1046. virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const;
  1047. virtual void texture_replace(RID p_texture, RID p_by_texture);
  1048. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height);
  1049. virtual void texture_set_path(RID p_texture, const String &p_path);
  1050. virtual String texture_get_path(RID p_texture) const;
  1051. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  1052. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  1053. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata);
  1054. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info);
  1055. virtual void texture_set_proxy(RID p_proxy, RID p_base);
  1056. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable);
  1057. virtual Size2 texture_size_with_proxy(RID p_proxy);
  1058. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false);
  1059. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false);
  1060. RID decal_atlas_get_texture() const;
  1061. RID decal_atlas_get_texture_srgb() const;
  1062. _FORCE_INLINE_ Rect2 decal_atlas_get_texture_rect(RID p_texture) {
  1063. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  1064. if (!t) {
  1065. return Rect2();
  1066. }
  1067. return t->uv_rect;
  1068. }
  1069. //internal usage
  1070. _FORCE_INLINE_ RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) {
  1071. if (p_texture.is_null()) {
  1072. return RID();
  1073. }
  1074. Texture *tex = texture_owner.get_or_null(p_texture);
  1075. if (!tex) {
  1076. return RID();
  1077. }
  1078. return (p_srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1079. }
  1080. _FORCE_INLINE_ Size2i texture_2d_get_size(RID p_texture) {
  1081. if (p_texture.is_null()) {
  1082. return Size2i();
  1083. }
  1084. Texture *tex = texture_owner.get_or_null(p_texture);
  1085. if (!tex) {
  1086. return Size2i();
  1087. }
  1088. return Size2i(tex->width_2d, tex->height_2d);
  1089. }
  1090. _FORCE_INLINE_ RID texture_rd_get_default(DefaultRDTexture p_texture) {
  1091. return default_rd_textures[p_texture];
  1092. }
  1093. _FORCE_INLINE_ RID sampler_rd_get_default(RS::CanvasItemTextureFilter p_filter, RS::CanvasItemTextureRepeat p_repeat) {
  1094. return default_rd_samplers[p_filter][p_repeat];
  1095. }
  1096. /* CANVAS TEXTURE API */
  1097. RID canvas_texture_allocate();
  1098. void canvas_texture_initialize(RID p_canvas_texture);
  1099. virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture);
  1100. virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess);
  1101. virtual void canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter);
  1102. virtual void canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat);
  1103. bool canvas_texture_get_uniform_set(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID p_base_shader, int p_base_set, RID &r_uniform_set, Size2i &r_size, Color &r_specular_shininess, bool &r_use_normal, bool &r_use_specular);
  1104. /* SHADER API */
  1105. RID shader_allocate();
  1106. void shader_initialize(RID p_shader);
  1107. void shader_set_code(RID p_shader, const String &p_code);
  1108. String shader_get_code(RID p_shader) const;
  1109. void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const;
  1110. void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture);
  1111. RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const;
  1112. Variant shader_get_param_default(RID p_shader, const StringName &p_param) const;
  1113. void shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function);
  1114. virtual RS::ShaderNativeSourceCode shader_get_native_source_code(RID p_shader) const;
  1115. /* COMMON MATERIAL API */
  1116. RID material_allocate();
  1117. void material_initialize(RID p_material);
  1118. void material_set_shader(RID p_material, RID p_shader);
  1119. void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value);
  1120. Variant material_get_param(RID p_material, const StringName &p_param) const;
  1121. void material_set_next_pass(RID p_material, RID p_next_material);
  1122. void material_set_render_priority(RID p_material, int priority);
  1123. bool material_is_animated(RID p_material);
  1124. bool material_casts_shadows(RID p_material);
  1125. void material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters);
  1126. void material_update_dependency(RID p_material, DependencyTracker *p_instance);
  1127. void material_force_update_textures(RID p_material, ShaderType p_shader_type);
  1128. void material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function);
  1129. _FORCE_INLINE_ uint32_t material_get_shader_id(RID p_material) {
  1130. Material *material = material_owner.get_or_null(p_material);
  1131. return material->shader_id;
  1132. }
  1133. _FORCE_INLINE_ MaterialData *material_get_data(RID p_material, ShaderType p_shader_type) {
  1134. Material *material = material_owner.get_or_null(p_material);
  1135. if (!material || material->shader_type != p_shader_type) {
  1136. return nullptr;
  1137. } else {
  1138. return material->data;
  1139. }
  1140. }
  1141. /* MESH API */
  1142. RID mesh_allocate();
  1143. void mesh_initialize(RID p_mesh);
  1144. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count);
  1145. /// Return stride
  1146. virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface);
  1147. virtual int mesh_get_blend_shape_count(RID p_mesh) const;
  1148. virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode);
  1149. virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const;
  1150. virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  1151. virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  1152. virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  1153. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material);
  1154. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const;
  1155. virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const;
  1156. virtual int mesh_get_surface_count(RID p_mesh) const;
  1157. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb);
  1158. virtual AABB mesh_get_custom_aabb(RID p_mesh) const;
  1159. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID());
  1160. virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh);
  1161. virtual void mesh_clear(RID p_mesh);
  1162. virtual bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton);
  1163. /* MESH INSTANCE */
  1164. virtual RID mesh_instance_create(RID p_base);
  1165. virtual void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton);
  1166. virtual void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight);
  1167. virtual void mesh_instance_check_for_update(RID p_mesh_instance);
  1168. virtual void update_mesh_instances();
  1169. _FORCE_INLINE_ const RID *mesh_get_surface_count_and_materials(RID p_mesh, uint32_t &r_surface_count) {
  1170. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  1171. ERR_FAIL_COND_V(!mesh, nullptr);
  1172. r_surface_count = mesh->surface_count;
  1173. if (r_surface_count == 0) {
  1174. return nullptr;
  1175. }
  1176. if (mesh->material_cache.is_empty()) {
  1177. mesh->material_cache.resize(mesh->surface_count);
  1178. for (uint32_t i = 0; i < r_surface_count; i++) {
  1179. mesh->material_cache.write[i] = mesh->surfaces[i]->material;
  1180. }
  1181. }
  1182. return mesh->material_cache.ptr();
  1183. }
  1184. _FORCE_INLINE_ void *mesh_get_surface(RID p_mesh, uint32_t p_surface_index) {
  1185. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  1186. ERR_FAIL_COND_V(!mesh, nullptr);
  1187. ERR_FAIL_UNSIGNED_INDEX_V(p_surface_index, mesh->surface_count, nullptr);
  1188. return mesh->surfaces[p_surface_index];
  1189. }
  1190. _FORCE_INLINE_ RID mesh_get_shadow_mesh(RID p_mesh) {
  1191. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  1192. ERR_FAIL_COND_V(!mesh, RID());
  1193. return mesh->shadow_mesh;
  1194. }
  1195. _FORCE_INLINE_ RS::PrimitiveType mesh_surface_get_primitive(void *p_surface) {
  1196. Mesh::Surface *surface = reinterpret_cast<Mesh::Surface *>(p_surface);
  1197. return surface->primitive;
  1198. }
  1199. _FORCE_INLINE_ bool mesh_surface_has_lod(void *p_surface) const {
  1200. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1201. return s->lod_count > 0;
  1202. }
  1203. _FORCE_INLINE_ uint32_t mesh_surface_get_vertices_drawn_count(void *p_surface) const {
  1204. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1205. return s->index_count ? s->index_count : s->vertex_count;
  1206. }
  1207. _FORCE_INLINE_ uint32_t mesh_surface_get_lod(void *p_surface, float p_model_scale, float p_distance_threshold, float p_lod_threshold, uint32_t *r_index_count = nullptr) const {
  1208. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1209. int32_t current_lod = -1;
  1210. if (r_index_count) {
  1211. *r_index_count = s->index_count;
  1212. }
  1213. for (uint32_t i = 0; i < s->lod_count; i++) {
  1214. float screen_size = s->lods[i].edge_length * p_model_scale / p_distance_threshold;
  1215. if (screen_size > p_lod_threshold) {
  1216. break;
  1217. }
  1218. current_lod = i;
  1219. }
  1220. if (current_lod == -1) {
  1221. return 0;
  1222. } else {
  1223. if (r_index_count) {
  1224. *r_index_count = s->lods[current_lod].index_count;
  1225. }
  1226. return current_lod + 1;
  1227. }
  1228. }
  1229. _FORCE_INLINE_ RID mesh_surface_get_index_array(void *p_surface, uint32_t p_lod) const {
  1230. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1231. if (p_lod == 0) {
  1232. return s->index_array;
  1233. } else {
  1234. return s->lods[p_lod - 1].index_array;
  1235. }
  1236. }
  1237. _FORCE_INLINE_ void mesh_surface_get_vertex_arrays_and_format(void *p_surface, uint32_t p_input_mask, RID &r_vertex_array_rd, RD::VertexFormatID &r_vertex_format) {
  1238. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1239. s->version_lock.lock();
  1240. //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way
  1241. for (uint32_t i = 0; i < s->version_count; i++) {
  1242. if (s->versions[i].input_mask != p_input_mask) {
  1243. continue;
  1244. }
  1245. //we have this version, hooray
  1246. r_vertex_format = s->versions[i].vertex_format;
  1247. r_vertex_array_rd = s->versions[i].vertex_array;
  1248. s->version_lock.unlock();
  1249. return;
  1250. }
  1251. uint32_t version = s->version_count;
  1252. s->version_count++;
  1253. s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count);
  1254. _mesh_surface_generate_version_for_input_mask(s->versions[version], s, p_input_mask);
  1255. r_vertex_format = s->versions[version].vertex_format;
  1256. r_vertex_array_rd = s->versions[version].vertex_array;
  1257. s->version_lock.unlock();
  1258. }
  1259. _FORCE_INLINE_ void mesh_instance_surface_get_vertex_arrays_and_format(RID p_mesh_instance, uint32_t p_surface_index, uint32_t p_input_mask, RID &r_vertex_array_rd, RD::VertexFormatID &r_vertex_format) {
  1260. MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance);
  1261. ERR_FAIL_COND(!mi);
  1262. Mesh *mesh = mi->mesh;
  1263. ERR_FAIL_UNSIGNED_INDEX(p_surface_index, mesh->surface_count);
  1264. MeshInstance::Surface *mis = &mi->surfaces[p_surface_index];
  1265. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1266. s->version_lock.lock();
  1267. //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way
  1268. for (uint32_t i = 0; i < mis->version_count; i++) {
  1269. if (mis->versions[i].input_mask != p_input_mask) {
  1270. continue;
  1271. }
  1272. //we have this version, hooray
  1273. r_vertex_format = mis->versions[i].vertex_format;
  1274. r_vertex_array_rd = mis->versions[i].vertex_array;
  1275. s->version_lock.unlock();
  1276. return;
  1277. }
  1278. uint32_t version = mis->version_count;
  1279. mis->version_count++;
  1280. mis->versions = (Mesh::Surface::Version *)memrealloc(mis->versions, sizeof(Mesh::Surface::Version) * mis->version_count);
  1281. _mesh_surface_generate_version_for_input_mask(mis->versions[version], s, p_input_mask, mis);
  1282. r_vertex_format = mis->versions[version].vertex_format;
  1283. r_vertex_array_rd = mis->versions[version].vertex_array;
  1284. s->version_lock.unlock();
  1285. }
  1286. _FORCE_INLINE_ RID mesh_get_default_rd_buffer(DefaultRDBuffer p_buffer) {
  1287. ERR_FAIL_INDEX_V(p_buffer, DEFAULT_RD_BUFFER_MAX, RID());
  1288. return mesh_default_rd_buffers[p_buffer];
  1289. }
  1290. _FORCE_INLINE_ uint32_t mesh_surface_get_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) {
  1291. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  1292. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1293. if (s->render_pass != p_render_pass) {
  1294. (*r_index)++;
  1295. s->render_pass = p_render_pass;
  1296. s->render_index = *r_index;
  1297. }
  1298. return s->render_index;
  1299. }
  1300. _FORCE_INLINE_ uint32_t mesh_surface_get_multimesh_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) {
  1301. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  1302. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1303. if (s->multimesh_render_pass != p_render_pass) {
  1304. (*r_index)++;
  1305. s->multimesh_render_pass = p_render_pass;
  1306. s->multimesh_render_index = *r_index;
  1307. }
  1308. return s->multimesh_render_index;
  1309. }
  1310. _FORCE_INLINE_ uint32_t mesh_surface_get_particles_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) {
  1311. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  1312. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1313. if (s->particles_render_pass != p_render_pass) {
  1314. (*r_index)++;
  1315. s->particles_render_pass = p_render_pass;
  1316. s->particles_render_index = *r_index;
  1317. }
  1318. return s->particles_render_index;
  1319. }
  1320. /* MULTIMESH API */
  1321. RID multimesh_allocate();
  1322. void multimesh_initialize(RID p_multimesh);
  1323. void multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false);
  1324. int multimesh_get_instance_count(RID p_multimesh) const;
  1325. void multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  1326. void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform);
  1327. void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform);
  1328. void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  1329. void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color);
  1330. RID multimesh_get_mesh(RID p_multimesh) const;
  1331. Transform3D multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  1332. Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const;
  1333. Color multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  1334. Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const;
  1335. void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer);
  1336. Vector<float> multimesh_get_buffer(RID p_multimesh) const;
  1337. void multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  1338. int multimesh_get_visible_instances(RID p_multimesh) const;
  1339. AABB multimesh_get_aabb(RID p_multimesh) const;
  1340. _FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const {
  1341. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1342. return multimesh->xform_format;
  1343. }
  1344. _FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const {
  1345. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1346. return multimesh->uses_colors;
  1347. }
  1348. _FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const {
  1349. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1350. return multimesh->uses_custom_data;
  1351. }
  1352. _FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const {
  1353. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1354. if (multimesh->visible_instances >= 0) {
  1355. return multimesh->visible_instances;
  1356. }
  1357. return multimesh->instances;
  1358. }
  1359. _FORCE_INLINE_ RID multimesh_get_3d_uniform_set(RID p_multimesh, RID p_shader, uint32_t p_set) const {
  1360. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1361. if (!multimesh->uniform_set_3d.is_valid()) {
  1362. Vector<RD::Uniform> uniforms;
  1363. RD::Uniform u;
  1364. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1365. u.binding = 0;
  1366. u.ids.push_back(multimesh->buffer);
  1367. uniforms.push_back(u);
  1368. multimesh->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1369. }
  1370. return multimesh->uniform_set_3d;
  1371. }
  1372. _FORCE_INLINE_ RID multimesh_get_2d_uniform_set(RID p_multimesh, RID p_shader, uint32_t p_set) const {
  1373. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1374. if (!multimesh->uniform_set_2d.is_valid()) {
  1375. Vector<RD::Uniform> uniforms;
  1376. RD::Uniform u;
  1377. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1378. u.binding = 0;
  1379. u.ids.push_back(multimesh->buffer);
  1380. uniforms.push_back(u);
  1381. multimesh->uniform_set_2d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1382. }
  1383. return multimesh->uniform_set_2d;
  1384. }
  1385. /* SKELETON API */
  1386. RID skeleton_allocate();
  1387. void skeleton_initialize(RID p_skeleton);
  1388. void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false);
  1389. void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform);
  1390. void skeleton_set_world_transform(RID p_skeleton, bool p_enable, const Transform3D &p_world_transform);
  1391. int skeleton_get_bone_count(RID p_skeleton) const;
  1392. void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform);
  1393. Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const;
  1394. void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform);
  1395. Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const;
  1396. _FORCE_INLINE_ bool skeleton_is_valid(RID p_skeleton) {
  1397. return skeleton_owner.get_or_null(p_skeleton) != nullptr;
  1398. }
  1399. _FORCE_INLINE_ RID skeleton_get_3d_uniform_set(RID p_skeleton, RID p_shader, uint32_t p_set) const {
  1400. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1401. ERR_FAIL_COND_V(!skeleton, RID());
  1402. ERR_FAIL_COND_V(skeleton->size == 0, RID());
  1403. if (skeleton->use_2d) {
  1404. return RID();
  1405. }
  1406. if (!skeleton->uniform_set_3d.is_valid()) {
  1407. Vector<RD::Uniform> uniforms;
  1408. RD::Uniform u;
  1409. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1410. u.binding = 0;
  1411. u.ids.push_back(skeleton->buffer);
  1412. uniforms.push_back(u);
  1413. skeleton->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1414. }
  1415. return skeleton->uniform_set_3d;
  1416. }
  1417. /* Light API */
  1418. void _light_initialize(RID p_rid, RS::LightType p_type);
  1419. RID directional_light_allocate();
  1420. void directional_light_initialize(RID p_light);
  1421. RID omni_light_allocate();
  1422. void omni_light_initialize(RID p_light);
  1423. RID spot_light_allocate();
  1424. void spot_light_initialize(RID p_light);
  1425. void light_set_color(RID p_light, const Color &p_color);
  1426. void light_set_param(RID p_light, RS::LightParam p_param, float p_value);
  1427. void light_set_shadow(RID p_light, bool p_enabled);
  1428. void light_set_shadow_color(RID p_light, const Color &p_color);
  1429. void light_set_projector(RID p_light, RID p_texture);
  1430. void light_set_negative(RID p_light, bool p_enable);
  1431. void light_set_cull_mask(RID p_light, uint32_t p_mask);
  1432. void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled);
  1433. void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode);
  1434. void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade);
  1435. void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode);
  1436. void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode);
  1437. void light_directional_set_blend_splits(RID p_light, bool p_enable);
  1438. bool light_directional_get_blend_splits(RID p_light) const;
  1439. void light_directional_set_sky_only(RID p_light, bool p_sky_only);
  1440. bool light_directional_is_sky_only(RID p_light) const;
  1441. RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light);
  1442. RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light);
  1443. _FORCE_INLINE_ RS::LightType light_get_type(RID p_light) const {
  1444. const Light *light = light_owner.get_or_null(p_light);
  1445. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1446. return light->type;
  1447. }
  1448. AABB light_get_aabb(RID p_light) const;
  1449. _FORCE_INLINE_ float light_get_param(RID p_light, RS::LightParam p_param) {
  1450. const Light *light = light_owner.get_or_null(p_light);
  1451. ERR_FAIL_COND_V(!light, 0);
  1452. return light->param[p_param];
  1453. }
  1454. _FORCE_INLINE_ RID light_get_projector(RID p_light) {
  1455. const Light *light = light_owner.get_or_null(p_light);
  1456. ERR_FAIL_COND_V(!light, RID());
  1457. return light->projector;
  1458. }
  1459. _FORCE_INLINE_ Color light_get_color(RID p_light) {
  1460. const Light *light = light_owner.get_or_null(p_light);
  1461. ERR_FAIL_COND_V(!light, Color());
  1462. return light->color;
  1463. }
  1464. _FORCE_INLINE_ Color light_get_shadow_color(RID p_light) {
  1465. const Light *light = light_owner.get_or_null(p_light);
  1466. ERR_FAIL_COND_V(!light, Color());
  1467. return light->shadow_color;
  1468. }
  1469. _FORCE_INLINE_ uint32_t light_get_cull_mask(RID p_light) {
  1470. const Light *light = light_owner.get_or_null(p_light);
  1471. ERR_FAIL_COND_V(!light, 0);
  1472. return light->cull_mask;
  1473. }
  1474. _FORCE_INLINE_ bool light_has_shadow(RID p_light) const {
  1475. const Light *light = light_owner.get_or_null(p_light);
  1476. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1477. return light->shadow;
  1478. }
  1479. _FORCE_INLINE_ bool light_has_projector(RID p_light) const {
  1480. const Light *light = light_owner.get_or_null(p_light);
  1481. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1482. return texture_owner.owns(light->projector);
  1483. }
  1484. _FORCE_INLINE_ bool light_is_negative(RID p_light) const {
  1485. const Light *light = light_owner.get_or_null(p_light);
  1486. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1487. return light->negative;
  1488. }
  1489. _FORCE_INLINE_ float light_get_transmittance_bias(RID p_light) const {
  1490. const Light *light = light_owner.get_or_null(p_light);
  1491. ERR_FAIL_COND_V(!light, 0.0);
  1492. return light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
  1493. }
  1494. _FORCE_INLINE_ float light_get_shadow_volumetric_fog_fade(RID p_light) const {
  1495. const Light *light = light_owner.get_or_null(p_light);
  1496. ERR_FAIL_COND_V(!light, 0.0);
  1497. return light->param[RS::LIGHT_PARAM_SHADOW_VOLUMETRIC_FOG_FADE];
  1498. }
  1499. RS::LightBakeMode light_get_bake_mode(RID p_light);
  1500. uint32_t light_get_max_sdfgi_cascade(RID p_light);
  1501. uint64_t light_get_version(RID p_light) const;
  1502. /* PROBE API */
  1503. RID reflection_probe_allocate();
  1504. void reflection_probe_initialize(RID p_reflection_probe);
  1505. void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode);
  1506. void reflection_probe_set_intensity(RID p_probe, float p_intensity);
  1507. void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode);
  1508. void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color);
  1509. void reflection_probe_set_ambient_energy(RID p_probe, float p_energy);
  1510. void reflection_probe_set_max_distance(RID p_probe, float p_distance);
  1511. void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents);
  1512. void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset);
  1513. void reflection_probe_set_as_interior(RID p_probe, bool p_enable);
  1514. void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable);
  1515. void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable);
  1516. void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers);
  1517. void reflection_probe_set_resolution(RID p_probe, int p_resolution);
  1518. void reflection_probe_set_lod_threshold(RID p_probe, float p_ratio);
  1519. AABB reflection_probe_get_aabb(RID p_probe) const;
  1520. RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const;
  1521. uint32_t reflection_probe_get_cull_mask(RID p_probe) const;
  1522. Vector3 reflection_probe_get_extents(RID p_probe) const;
  1523. Vector3 reflection_probe_get_origin_offset(RID p_probe) const;
  1524. float reflection_probe_get_origin_max_distance(RID p_probe) const;
  1525. float reflection_probe_get_lod_threshold(RID p_probe) const;
  1526. int reflection_probe_get_resolution(RID p_probe) const;
  1527. bool reflection_probe_renders_shadows(RID p_probe) const;
  1528. float reflection_probe_get_intensity(RID p_probe) const;
  1529. bool reflection_probe_is_interior(RID p_probe) const;
  1530. bool reflection_probe_is_box_projection(RID p_probe) const;
  1531. RS::ReflectionProbeAmbientMode reflection_probe_get_ambient_mode(RID p_probe) const;
  1532. Color reflection_probe_get_ambient_color(RID p_probe) const;
  1533. float reflection_probe_get_ambient_color_energy(RID p_probe) const;
  1534. void base_update_dependency(RID p_base, DependencyTracker *p_instance);
  1535. void skeleton_update_dependency(RID p_skeleton, DependencyTracker *p_instance);
  1536. /* DECAL API */
  1537. RID decal_allocate();
  1538. void decal_initialize(RID p_decal);
  1539. virtual void decal_set_extents(RID p_decal, const Vector3 &p_extents);
  1540. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture);
  1541. virtual void decal_set_emission_energy(RID p_decal, float p_energy);
  1542. virtual void decal_set_albedo_mix(RID p_decal, float p_mix);
  1543. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate);
  1544. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers);
  1545. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length);
  1546. virtual void decal_set_fade(RID p_decal, float p_above, float p_below);
  1547. virtual void decal_set_normal_fade(RID p_decal, float p_fade);
  1548. _FORCE_INLINE_ Vector3 decal_get_extents(RID p_decal) {
  1549. const Decal *decal = decal_owner.get_or_null(p_decal);
  1550. return decal->extents;
  1551. }
  1552. _FORCE_INLINE_ RID decal_get_texture(RID p_decal, RS::DecalTexture p_texture) {
  1553. const Decal *decal = decal_owner.get_or_null(p_decal);
  1554. return decal->textures[p_texture];
  1555. }
  1556. _FORCE_INLINE_ Color decal_get_modulate(RID p_decal) {
  1557. const Decal *decal = decal_owner.get_or_null(p_decal);
  1558. return decal->modulate;
  1559. }
  1560. _FORCE_INLINE_ float decal_get_emission_energy(RID p_decal) {
  1561. const Decal *decal = decal_owner.get_or_null(p_decal);
  1562. return decal->emission_energy;
  1563. }
  1564. _FORCE_INLINE_ float decal_get_albedo_mix(RID p_decal) {
  1565. const Decal *decal = decal_owner.get_or_null(p_decal);
  1566. return decal->albedo_mix;
  1567. }
  1568. _FORCE_INLINE_ uint32_t decal_get_cull_mask(RID p_decal) {
  1569. const Decal *decal = decal_owner.get_or_null(p_decal);
  1570. return decal->cull_mask;
  1571. }
  1572. _FORCE_INLINE_ float decal_get_upper_fade(RID p_decal) {
  1573. const Decal *decal = decal_owner.get_or_null(p_decal);
  1574. return decal->upper_fade;
  1575. }
  1576. _FORCE_INLINE_ float decal_get_lower_fade(RID p_decal) {
  1577. const Decal *decal = decal_owner.get_or_null(p_decal);
  1578. return decal->lower_fade;
  1579. }
  1580. _FORCE_INLINE_ float decal_get_normal_fade(RID p_decal) {
  1581. const Decal *decal = decal_owner.get_or_null(p_decal);
  1582. return decal->normal_fade;
  1583. }
  1584. _FORCE_INLINE_ bool decal_is_distance_fade_enabled(RID p_decal) {
  1585. const Decal *decal = decal_owner.get_or_null(p_decal);
  1586. return decal->distance_fade;
  1587. }
  1588. _FORCE_INLINE_ float decal_get_distance_fade_begin(RID p_decal) {
  1589. const Decal *decal = decal_owner.get_or_null(p_decal);
  1590. return decal->distance_fade_begin;
  1591. }
  1592. _FORCE_INLINE_ float decal_get_distance_fade_length(RID p_decal) {
  1593. const Decal *decal = decal_owner.get_or_null(p_decal);
  1594. return decal->distance_fade_length;
  1595. }
  1596. virtual AABB decal_get_aabb(RID p_decal) const;
  1597. /* VOXEL GI API */
  1598. RID voxel_gi_allocate();
  1599. void voxel_gi_initialize(RID p_voxel_gi);
  1600. void voxel_gi_allocate_data(RID p_voxel_gi, const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts);
  1601. AABB voxel_gi_get_bounds(RID p_voxel_gi) const;
  1602. Vector3i voxel_gi_get_octree_size(RID p_voxel_gi) const;
  1603. Vector<uint8_t> voxel_gi_get_octree_cells(RID p_voxel_gi) const;
  1604. Vector<uint8_t> voxel_gi_get_data_cells(RID p_voxel_gi) const;
  1605. Vector<uint8_t> voxel_gi_get_distance_field(RID p_voxel_gi) const;
  1606. Vector<int> voxel_gi_get_level_counts(RID p_voxel_gi) const;
  1607. Transform3D voxel_gi_get_to_cell_xform(RID p_voxel_gi) const;
  1608. void voxel_gi_set_dynamic_range(RID p_voxel_gi, float p_range);
  1609. float voxel_gi_get_dynamic_range(RID p_voxel_gi) const;
  1610. void voxel_gi_set_propagation(RID p_voxel_gi, float p_range);
  1611. float voxel_gi_get_propagation(RID p_voxel_gi) const;
  1612. void voxel_gi_set_energy(RID p_voxel_gi, float p_energy);
  1613. float voxel_gi_get_energy(RID p_voxel_gi) const;
  1614. void voxel_gi_set_bias(RID p_voxel_gi, float p_bias);
  1615. float voxel_gi_get_bias(RID p_voxel_gi) const;
  1616. void voxel_gi_set_normal_bias(RID p_voxel_gi, float p_range);
  1617. float voxel_gi_get_normal_bias(RID p_voxel_gi) const;
  1618. void voxel_gi_set_interior(RID p_voxel_gi, bool p_enable);
  1619. bool voxel_gi_is_interior(RID p_voxel_gi) const;
  1620. void voxel_gi_set_use_two_bounces(RID p_voxel_gi, bool p_enable);
  1621. bool voxel_gi_is_using_two_bounces(RID p_voxel_gi) const;
  1622. void voxel_gi_set_anisotropy_strength(RID p_voxel_gi, float p_strength);
  1623. float voxel_gi_get_anisotropy_strength(RID p_voxel_gi) const;
  1624. uint32_t voxel_gi_get_version(RID p_probe);
  1625. uint32_t voxel_gi_get_data_version(RID p_probe);
  1626. RID voxel_gi_get_octree_buffer(RID p_voxel_gi) const;
  1627. RID voxel_gi_get_data_buffer(RID p_voxel_gi) const;
  1628. RID voxel_gi_get_sdf_texture(RID p_voxel_gi);
  1629. /* LIGHTMAP CAPTURE */
  1630. RID lightmap_allocate();
  1631. void lightmap_initialize(RID p_lightmap);
  1632. virtual void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics);
  1633. virtual void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds);
  1634. virtual void lightmap_set_probe_interior(RID p_lightmap, bool p_interior);
  1635. virtual void lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree);
  1636. virtual PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const;
  1637. virtual PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const;
  1638. virtual PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const;
  1639. virtual PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const;
  1640. virtual AABB lightmap_get_aabb(RID p_lightmap) const;
  1641. virtual bool lightmap_is_interior(RID p_lightmap) const;
  1642. virtual void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh);
  1643. virtual void lightmap_set_probe_capture_update_speed(float p_speed);
  1644. _FORCE_INLINE_ float lightmap_get_probe_capture_update_speed() const {
  1645. return lightmap_probe_capture_update_speed;
  1646. }
  1647. _FORCE_INLINE_ RID lightmap_get_texture(RID p_lightmap) const {
  1648. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1649. ERR_FAIL_COND_V(!lm, RID());
  1650. return lm->light_texture;
  1651. }
  1652. _FORCE_INLINE_ int32_t lightmap_get_array_index(RID p_lightmap) const {
  1653. ERR_FAIL_COND_V(!using_lightmap_array, -1); //only for arrays
  1654. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1655. return lm->array_index;
  1656. }
  1657. _FORCE_INLINE_ bool lightmap_uses_spherical_harmonics(RID p_lightmap) const {
  1658. ERR_FAIL_COND_V(!using_lightmap_array, false); //only for arrays
  1659. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  1660. return lm->uses_spherical_harmonics;
  1661. }
  1662. _FORCE_INLINE_ uint64_t lightmap_array_get_version() const {
  1663. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  1664. return lightmap_array_version;
  1665. }
  1666. _FORCE_INLINE_ int lightmap_array_get_size() const {
  1667. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  1668. return lightmap_textures.size();
  1669. }
  1670. _FORCE_INLINE_ const Vector<RID> &lightmap_array_get_textures() const {
  1671. ERR_FAIL_COND_V(!using_lightmap_array, lightmap_textures); //only for arrays
  1672. return lightmap_textures;
  1673. }
  1674. /* PARTICLES */
  1675. RID particles_allocate();
  1676. void particles_initialize(RID p_particles_collision);
  1677. void particles_set_mode(RID p_particles, RS::ParticlesMode p_mode);
  1678. void particles_set_emitting(RID p_particles, bool p_emitting);
  1679. void particles_set_amount(RID p_particles, int p_amount);
  1680. void particles_set_lifetime(RID p_particles, double p_lifetime);
  1681. void particles_set_one_shot(RID p_particles, bool p_one_shot);
  1682. void particles_set_pre_process_time(RID p_particles, double p_time);
  1683. void particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio);
  1684. void particles_set_randomness_ratio(RID p_particles, real_t p_ratio);
  1685. void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb);
  1686. void particles_set_speed_scale(RID p_particles, double p_scale);
  1687. void particles_set_use_local_coordinates(RID p_particles, bool p_enable);
  1688. void particles_set_process_material(RID p_particles, RID p_material);
  1689. void particles_set_fixed_fps(RID p_particles, int p_fps);
  1690. void particles_set_interpolate(RID p_particles, bool p_enable);
  1691. void particles_set_fractional_delta(RID p_particles, bool p_enable);
  1692. void particles_set_collision_base_size(RID p_particles, real_t p_size);
  1693. void particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align);
  1694. void particles_set_trails(RID p_particles, bool p_enable, double p_length);
  1695. void particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses);
  1696. void particles_restart(RID p_particles);
  1697. void particles_emit(RID p_particles, const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags);
  1698. void particles_set_subemitter(RID p_particles, RID p_subemitter_particles);
  1699. void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order);
  1700. void particles_set_draw_passes(RID p_particles, int p_count);
  1701. void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh);
  1702. void particles_request_process(RID p_particles);
  1703. AABB particles_get_current_aabb(RID p_particles);
  1704. AABB particles_get_aabb(RID p_particles) const;
  1705. void particles_set_emission_transform(RID p_particles, const Transform3D &p_transform);
  1706. bool particles_get_emitting(RID p_particles);
  1707. int particles_get_draw_passes(RID p_particles) const;
  1708. RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const;
  1709. void particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis);
  1710. virtual bool particles_is_inactive(RID p_particles) const;
  1711. _FORCE_INLINE_ RS::ParticlesMode particles_get_mode(RID p_particles) {
  1712. Particles *particles = particles_owner.get_or_null(p_particles);
  1713. ERR_FAIL_COND_V(!particles, RS::PARTICLES_MODE_2D);
  1714. return particles->mode;
  1715. }
  1716. _FORCE_INLINE_ uint32_t particles_get_amount(RID p_particles, uint32_t &r_trail_divisor) {
  1717. Particles *particles = particles_owner.get_or_null(p_particles);
  1718. ERR_FAIL_COND_V(!particles, 0);
  1719. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1720. r_trail_divisor = particles->trail_bind_poses.size();
  1721. } else {
  1722. r_trail_divisor = 1;
  1723. }
  1724. return particles->amount * r_trail_divisor;
  1725. }
  1726. _FORCE_INLINE_ bool particles_has_collision(RID p_particles) {
  1727. Particles *particles = particles_owner.get_or_null(p_particles);
  1728. ERR_FAIL_COND_V(!particles, 0);
  1729. return particles->has_collision_cache;
  1730. }
  1731. _FORCE_INLINE_ uint32_t particles_is_using_local_coords(RID p_particles) {
  1732. Particles *particles = particles_owner.get_or_null(p_particles);
  1733. ERR_FAIL_COND_V(!particles, false);
  1734. return particles->use_local_coords;
  1735. }
  1736. _FORCE_INLINE_ RID particles_get_instance_buffer_uniform_set(RID p_particles, RID p_shader, uint32_t p_set) {
  1737. Particles *particles = particles_owner.get_or_null(p_particles);
  1738. ERR_FAIL_COND_V(!particles, RID());
  1739. if (particles->particles_transforms_buffer_uniform_set.is_null()) {
  1740. _particles_update_buffers(particles);
  1741. Vector<RD::Uniform> uniforms;
  1742. {
  1743. RD::Uniform u;
  1744. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1745. u.binding = 0;
  1746. u.ids.push_back(particles->particle_instance_buffer);
  1747. uniforms.push_back(u);
  1748. }
  1749. particles->particles_transforms_buffer_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1750. }
  1751. return particles->particles_transforms_buffer_uniform_set;
  1752. }
  1753. virtual void particles_add_collision(RID p_particles, RID p_particles_collision_instance);
  1754. virtual void particles_remove_collision(RID p_particles, RID p_particles_collision_instance);
  1755. virtual void particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, RID p_texture);
  1756. /* PARTICLES COLLISION */
  1757. RID particles_collision_allocate();
  1758. void particles_collision_initialize(RID p_particles_collision);
  1759. virtual void particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type);
  1760. virtual void particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask);
  1761. virtual void particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius); //for spheres
  1762. virtual void particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents); //for non-spheres
  1763. virtual void particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength);
  1764. virtual void particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality);
  1765. virtual void particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve);
  1766. virtual void particles_collision_set_field_texture(RID p_particles_collision, RID p_texture); //for SDF and vector field, heightfield is dynamic
  1767. virtual void particles_collision_height_field_update(RID p_particles_collision); //for SDF and vector field
  1768. virtual void particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution); //for SDF and vector field
  1769. virtual AABB particles_collision_get_aabb(RID p_particles_collision) const;
  1770. virtual Vector3 particles_collision_get_extents(RID p_particles_collision) const;
  1771. virtual bool particles_collision_is_heightfield(RID p_particles_collision) const;
  1772. RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const;
  1773. /* FOG VOLUMES */
  1774. virtual RID fog_volume_allocate();
  1775. virtual void fog_volume_initialize(RID p_rid);
  1776. virtual void fog_volume_set_shape(RID p_fog_volume, RS::FogVolumeShape p_shape);
  1777. virtual void fog_volume_set_extents(RID p_fog_volume, const Vector3 &p_extents);
  1778. virtual void fog_volume_set_material(RID p_fog_volume, RID p_material);
  1779. virtual RS::FogVolumeShape fog_volume_get_shape(RID p_fog_volume) const;
  1780. virtual RID fog_volume_get_material(RID p_fog_volume) const;
  1781. virtual AABB fog_volume_get_aabb(RID p_fog_volume) const;
  1782. virtual Vector3 fog_volume_get_extents(RID p_fog_volume) const;
  1783. /* VISIBILITY NOTIFIER */
  1784. virtual RID visibility_notifier_allocate();
  1785. virtual void visibility_notifier_initialize(RID p_notifier);
  1786. virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb);
  1787. virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable);
  1788. virtual AABB visibility_notifier_get_aabb(RID p_notifier) const;
  1789. virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred);
  1790. //used from 2D and 3D
  1791. virtual RID particles_collision_instance_create(RID p_collision);
  1792. virtual void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform);
  1793. virtual void particles_collision_instance_set_active(RID p_collision_instance, bool p_active);
  1794. /* GLOBAL VARIABLES API */
  1795. virtual void global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value);
  1796. virtual void global_variable_remove(const StringName &p_name);
  1797. virtual Vector<StringName> global_variable_get_list() const;
  1798. virtual void global_variable_set(const StringName &p_name, const Variant &p_value);
  1799. virtual void global_variable_set_override(const StringName &p_name, const Variant &p_value);
  1800. virtual Variant global_variable_get(const StringName &p_name) const;
  1801. virtual RS::GlobalVariableType global_variable_get_type(const StringName &p_name) const;
  1802. RS::GlobalVariableType global_variable_get_type_internal(const StringName &p_name) const;
  1803. virtual void global_variables_load_settings(bool p_load_textures = true);
  1804. virtual void global_variables_clear();
  1805. virtual int32_t global_variables_instance_allocate(RID p_instance);
  1806. virtual void global_variables_instance_free(RID p_instance);
  1807. virtual void global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value);
  1808. RID global_variables_get_storage_buffer() const;
  1809. /* RENDER TARGET API */
  1810. RID render_target_create();
  1811. void render_target_set_position(RID p_render_target, int p_x, int p_y);
  1812. void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count);
  1813. RID render_target_get_texture(RID p_render_target);
  1814. void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id);
  1815. void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value);
  1816. bool render_target_was_used(RID p_render_target);
  1817. void render_target_set_as_unused(RID p_render_target);
  1818. void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);
  1819. void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);
  1820. void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);
  1821. RID render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader);
  1822. virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color);
  1823. virtual bool render_target_is_clear_requested(RID p_render_target);
  1824. virtual Color render_target_get_clear_request_color(RID p_render_target);
  1825. virtual void render_target_disable_clear_request(RID p_render_target);
  1826. virtual void render_target_do_clear_request(RID p_render_target);
  1827. virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale);
  1828. RID render_target_get_sdf_texture(RID p_render_target);
  1829. RID render_target_get_sdf_framebuffer(RID p_render_target);
  1830. void render_target_sdf_process(RID p_render_target);
  1831. virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const;
  1832. void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled);
  1833. bool render_target_is_sdf_enabled(RID p_render_target) const;
  1834. Size2 render_target_get_size(RID p_render_target);
  1835. RID render_target_get_rd_framebuffer(RID p_render_target);
  1836. RID render_target_get_rd_texture(RID p_render_target);
  1837. RID render_target_get_rd_backbuffer(RID p_render_target);
  1838. RID render_target_get_rd_backbuffer_framebuffer(RID p_render_target);
  1839. RID render_target_get_framebuffer_uniform_set(RID p_render_target);
  1840. RID render_target_get_backbuffer_uniform_set(RID p_render_target);
  1841. void render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  1842. void render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  1843. RS::InstanceType get_base_type(RID p_rid) const;
  1844. bool free(RID p_rid);
  1845. bool has_os_feature(const String &p_feature) const;
  1846. void update_dirty_resources();
  1847. void set_debug_generate_wireframes(bool p_generate) {}
  1848. //keep cached since it can be called form any thread
  1849. uint64_t texture_mem_cache = 0;
  1850. uint64_t buffer_mem_cache = 0;
  1851. uint64_t total_mem_cache = 0;
  1852. virtual void update_memory_info();
  1853. virtual uint64_t get_rendering_info(RS::RenderingInfo p_info);
  1854. String get_video_adapter_name() const;
  1855. String get_video_adapter_vendor() const;
  1856. virtual void capture_timestamps_begin();
  1857. virtual void capture_timestamp(const String &p_name);
  1858. virtual uint32_t get_captured_timestamps_count() const;
  1859. virtual uint64_t get_captured_timestamps_frame() const;
  1860. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
  1861. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
  1862. virtual String get_captured_timestamp_name(uint32_t p_index) const;
  1863. RID get_default_rd_storage_buffer() { return default_rd_storage_buffer; }
  1864. static RendererStorageRD *base_singleton;
  1865. void init_effects(bool p_prefer_raster_effects);
  1866. EffectsRD *get_effects();
  1867. RendererStorageRD();
  1868. ~RendererStorageRD();
  1869. };
  1870. #endif // RASTERIZER_STORAGE_RD_H