particles_storage.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. /*************************************************************************/
  2. /* particles_storage.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "particles_storage.h"
  31. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/rendering_server_globals.h"
  33. #include "texture_storage.h"
  34. using namespace RendererRD;
  35. ParticlesStorage *ParticlesStorage::singleton = nullptr;
  36. ParticlesStorage *ParticlesStorage::get_singleton() {
  37. return singleton;
  38. }
  39. ParticlesStorage::ParticlesStorage() {
  40. singleton = this;
  41. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  42. /* Particles */
  43. {
  44. // Initialize particles
  45. Vector<String> particles_modes;
  46. particles_modes.push_back("");
  47. particles_shader.shader.initialize(particles_modes, String());
  48. }
  49. MaterialStorage::get_singleton()->shader_set_data_request_function(SHADER_TYPE_PARTICLES, _create_particles_shader_funcs);
  50. MaterialStorage::get_singleton()->material_set_data_request_function(SHADER_TYPE_PARTICLES, _create_particles_material_funcs);
  51. {
  52. ShaderCompiler::DefaultIdentifierActions actions;
  53. actions.renames["COLOR"] = "PARTICLE.color";
  54. actions.renames["VELOCITY"] = "PARTICLE.velocity";
  55. //actions.renames["MASS"] = "mass"; ?
  56. actions.renames["ACTIVE"] = "particle_active";
  57. actions.renames["RESTART"] = "restart";
  58. actions.renames["CUSTOM"] = "PARTICLE.custom";
  59. for (int i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  60. String udname = "USERDATA" + itos(i + 1);
  61. actions.renames[udname] = "PARTICLE.userdata" + itos(i + 1);
  62. actions.usage_defines[udname] = "#define USERDATA" + itos(i + 1) + "_USED\n";
  63. }
  64. actions.renames["TRANSFORM"] = "PARTICLE.xform";
  65. actions.renames["TIME"] = "frame_history.data[0].time";
  66. actions.renames["PI"] = _MKSTR(Math_PI);
  67. actions.renames["TAU"] = _MKSTR(Math_TAU);
  68. actions.renames["E"] = _MKSTR(Math_E);
  69. actions.renames["LIFETIME"] = "params.lifetime";
  70. actions.renames["DELTA"] = "local_delta";
  71. actions.renames["NUMBER"] = "particle_number";
  72. actions.renames["INDEX"] = "index";
  73. //actions.renames["GRAVITY"] = "current_gravity";
  74. actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform";
  75. actions.renames["RANDOM_SEED"] = "FRAME.random_seed";
  76. actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION";
  77. actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE";
  78. actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY";
  79. actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR";
  80. actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM";
  81. actions.renames["RESTART_POSITION"] = "restart_position";
  82. actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale";
  83. actions.renames["RESTART_VELOCITY"] = "restart_velocity";
  84. actions.renames["RESTART_COLOR"] = "restart_color";
  85. actions.renames["RESTART_CUSTOM"] = "restart_custom";
  86. actions.renames["emit_subparticle"] = "emit_subparticle";
  87. actions.renames["COLLIDED"] = "collided";
  88. actions.renames["COLLISION_NORMAL"] = "collision_normal";
  89. actions.renames["COLLISION_DEPTH"] = "collision_depth";
  90. actions.renames["ATTRACTOR_FORCE"] = "attractor_force";
  91. actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  92. actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  93. actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  94. actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISON_SCALE\n";
  95. actions.sampler_array_name = "material_samplers";
  96. actions.base_texture_binding_index = 1;
  97. actions.texture_layout_set = 3;
  98. actions.base_uniform_string = "material.";
  99. actions.base_varying_index = 10;
  100. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  101. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  102. actions.global_buffer_array_variable = "global_shader_uniforms.data";
  103. particles_shader.compiler.initialize(actions);
  104. }
  105. {
  106. // default material and shader for particles shader
  107. particles_shader.default_shader = material_storage->shader_allocate();
  108. material_storage->shader_initialize(particles_shader.default_shader);
  109. material_storage->shader_set_code(particles_shader.default_shader, R"(
  110. // Default particles shader.
  111. shader_type particles;
  112. void process() {
  113. COLOR = vec4(1.0);
  114. }
  115. )");
  116. particles_shader.default_material = material_storage->material_allocate();
  117. material_storage->material_initialize(particles_shader.default_material);
  118. material_storage->material_set_shader(particles_shader.default_material, particles_shader.default_shader);
  119. ParticlesMaterialData *md = static_cast<ParticlesMaterialData *>(material_storage->material_get_data(particles_shader.default_material, SHADER_TYPE_PARTICLES));
  120. particles_shader.default_shader_rd = particles_shader.shader.version_get_shader(md->shader_data->version, 0);
  121. Vector<RD::Uniform> uniforms;
  122. {
  123. Vector<RID> ids;
  124. ids.resize(12);
  125. RID *ids_ptr = ids.ptrw();
  126. ids_ptr[0] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  127. ids_ptr[1] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  128. ids_ptr[2] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  129. ids_ptr[3] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  130. ids_ptr[4] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  131. ids_ptr[5] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  132. ids_ptr[6] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  133. ids_ptr[7] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  134. ids_ptr[8] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  135. ids_ptr[9] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  136. ids_ptr[10] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  137. ids_ptr[11] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  138. RD::Uniform u(RD::UNIFORM_TYPE_SAMPLER, 1, ids);
  139. uniforms.push_back(u);
  140. }
  141. {
  142. RD::Uniform u;
  143. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  144. u.binding = 2;
  145. u.append_id(material_storage->global_shader_uniforms_get_storage_buffer());
  146. uniforms.push_back(u);
  147. }
  148. particles_shader.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 0);
  149. }
  150. {
  151. Vector<String> copy_modes;
  152. for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) {
  153. if (i == 0) {
  154. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n");
  155. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n");
  156. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n");
  157. } else {
  158. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USERDATA_COUNT " + itos(i) + "\n");
  159. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n#define USERDATA_COUNT " + itos(i) + "\n");
  160. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n#define USERDATA_COUNT " + itos(i) + "\n");
  161. }
  162. }
  163. particles_shader.copy_shader.initialize(copy_modes);
  164. particles_shader.copy_shader_version = particles_shader.copy_shader.version_create();
  165. for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) {
  166. for (int j = 0; j < ParticlesShader::COPY_MODE_MAX; j++) {
  167. particles_shader.copy_pipelines[i * ParticlesShader::COPY_MODE_MAX + j] = RD::get_singleton()->compute_pipeline_create(particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, i * ParticlesShader::COPY_MODE_MAX + j));
  168. }
  169. }
  170. }
  171. }
  172. ParticlesStorage::~ParticlesStorage() {
  173. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  174. particles_shader.copy_shader.version_free(particles_shader.copy_shader_version);
  175. material_storage->material_free(particles_shader.default_material);
  176. material_storage->shader_free(particles_shader.default_shader);
  177. singleton = nullptr;
  178. }
  179. /* PARTICLES */
  180. RID ParticlesStorage::particles_allocate() {
  181. return particles_owner.allocate_rid();
  182. }
  183. void ParticlesStorage::particles_initialize(RID p_rid) {
  184. particles_owner.initialize_rid(p_rid, Particles());
  185. }
  186. void ParticlesStorage::particles_free(RID p_rid) {
  187. update_particles();
  188. Particles *particles = particles_owner.get_or_null(p_rid);
  189. particles->dependency.deleted_notify(p_rid);
  190. _particles_free_data(particles);
  191. particles_owner.free(p_rid);
  192. }
  193. void ParticlesStorage::particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) {
  194. Particles *particles = particles_owner.get_or_null(p_particles);
  195. ERR_FAIL_COND(!particles);
  196. if (particles->mode == p_mode) {
  197. return;
  198. }
  199. _particles_free_data(particles);
  200. particles->mode = p_mode;
  201. }
  202. void ParticlesStorage::particles_set_emitting(RID p_particles, bool p_emitting) {
  203. Particles *particles = particles_owner.get_or_null(p_particles);
  204. ERR_FAIL_COND(!particles);
  205. particles->emitting = p_emitting;
  206. }
  207. bool ParticlesStorage::particles_get_emitting(RID p_particles) {
  208. ERR_FAIL_COND_V_MSG(RSG::threaded, false, "This function should never be used with threaded rendering, as it stalls the renderer.");
  209. Particles *particles = particles_owner.get_or_null(p_particles);
  210. ERR_FAIL_COND_V(!particles, false);
  211. return particles->emitting;
  212. }
  213. void ParticlesStorage::_particles_free_data(Particles *particles) {
  214. if (particles->particle_buffer.is_valid()) {
  215. RD::get_singleton()->free(particles->particle_buffer);
  216. particles->particle_buffer = RID();
  217. RD::get_singleton()->free(particles->particle_instance_buffer);
  218. particles->particle_instance_buffer = RID();
  219. }
  220. particles->userdata_count = 0;
  221. if (particles->frame_params_buffer.is_valid()) {
  222. RD::get_singleton()->free(particles->frame_params_buffer);
  223. particles->frame_params_buffer = RID();
  224. }
  225. particles->particles_transforms_buffer_uniform_set = RID();
  226. if (RD::get_singleton()->uniform_set_is_valid(particles->trail_bind_pose_uniform_set)) {
  227. RD::get_singleton()->free(particles->trail_bind_pose_uniform_set);
  228. }
  229. particles->trail_bind_pose_uniform_set = RID();
  230. if (particles->trail_bind_pose_buffer.is_valid()) {
  231. RD::get_singleton()->free(particles->trail_bind_pose_buffer);
  232. particles->trail_bind_pose_buffer = RID();
  233. }
  234. if (RD::get_singleton()->uniform_set_is_valid(particles->collision_textures_uniform_set)) {
  235. RD::get_singleton()->free(particles->collision_textures_uniform_set);
  236. }
  237. particles->collision_textures_uniform_set = RID();
  238. if (particles->particles_sort_buffer.is_valid()) {
  239. RD::get_singleton()->free(particles->particles_sort_buffer);
  240. particles->particles_sort_buffer = RID();
  241. particles->particles_sort_uniform_set = RID();
  242. }
  243. if (particles->emission_buffer != nullptr) {
  244. particles->emission_buffer = nullptr;
  245. particles->emission_buffer_data.clear();
  246. RD::get_singleton()->free(particles->emission_storage_buffer);
  247. particles->emission_storage_buffer = RID();
  248. }
  249. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  250. //will need to be re-created
  251. RD::get_singleton()->free(particles->particles_material_uniform_set);
  252. }
  253. particles->particles_material_uniform_set = RID();
  254. }
  255. void ParticlesStorage::particles_set_amount(RID p_particles, int p_amount) {
  256. Particles *particles = particles_owner.get_or_null(p_particles);
  257. ERR_FAIL_COND(!particles);
  258. if (particles->amount == p_amount) {
  259. return;
  260. }
  261. _particles_free_data(particles);
  262. particles->amount = p_amount;
  263. particles->prev_ticks = 0;
  264. particles->phase = 0;
  265. particles->prev_phase = 0;
  266. particles->clear = true;
  267. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  268. }
  269. void ParticlesStorage::particles_set_lifetime(RID p_particles, double p_lifetime) {
  270. Particles *particles = particles_owner.get_or_null(p_particles);
  271. ERR_FAIL_COND(!particles);
  272. particles->lifetime = p_lifetime;
  273. }
  274. void ParticlesStorage::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  275. Particles *particles = particles_owner.get_or_null(p_particles);
  276. ERR_FAIL_COND(!particles);
  277. particles->one_shot = p_one_shot;
  278. }
  279. void ParticlesStorage::particles_set_pre_process_time(RID p_particles, double p_time) {
  280. Particles *particles = particles_owner.get_or_null(p_particles);
  281. ERR_FAIL_COND(!particles);
  282. particles->pre_process_time = p_time;
  283. }
  284. void ParticlesStorage::particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) {
  285. Particles *particles = particles_owner.get_or_null(p_particles);
  286. ERR_FAIL_COND(!particles);
  287. particles->explosiveness = p_ratio;
  288. }
  289. void ParticlesStorage::particles_set_randomness_ratio(RID p_particles, real_t p_ratio) {
  290. Particles *particles = particles_owner.get_or_null(p_particles);
  291. ERR_FAIL_COND(!particles);
  292. particles->randomness = p_ratio;
  293. }
  294. void ParticlesStorage::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  295. Particles *particles = particles_owner.get_or_null(p_particles);
  296. ERR_FAIL_COND(!particles);
  297. particles->custom_aabb = p_aabb;
  298. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  299. }
  300. void ParticlesStorage::particles_set_speed_scale(RID p_particles, double p_scale) {
  301. Particles *particles = particles_owner.get_or_null(p_particles);
  302. ERR_FAIL_COND(!particles);
  303. particles->speed_scale = p_scale;
  304. }
  305. void ParticlesStorage::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  306. Particles *particles = particles_owner.get_or_null(p_particles);
  307. ERR_FAIL_COND(!particles);
  308. particles->use_local_coords = p_enable;
  309. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  310. }
  311. void ParticlesStorage::particles_set_fixed_fps(RID p_particles, int p_fps) {
  312. Particles *particles = particles_owner.get_or_null(p_particles);
  313. ERR_FAIL_COND(!particles);
  314. particles->fixed_fps = p_fps;
  315. _particles_free_data(particles);
  316. particles->prev_ticks = 0;
  317. particles->phase = 0;
  318. particles->prev_phase = 0;
  319. particles->clear = true;
  320. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  321. }
  322. void ParticlesStorage::particles_set_interpolate(RID p_particles, bool p_enable) {
  323. Particles *particles = particles_owner.get_or_null(p_particles);
  324. ERR_FAIL_COND(!particles);
  325. particles->interpolate = p_enable;
  326. }
  327. void ParticlesStorage::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  328. Particles *particles = particles_owner.get_or_null(p_particles);
  329. ERR_FAIL_COND(!particles);
  330. particles->fractional_delta = p_enable;
  331. }
  332. void ParticlesStorage::particles_set_trails(RID p_particles, bool p_enable, double p_length) {
  333. Particles *particles = particles_owner.get_or_null(p_particles);
  334. ERR_FAIL_COND(!particles);
  335. ERR_FAIL_COND(p_length < 0.1);
  336. p_length = MIN(10.0, p_length);
  337. particles->trails_enabled = p_enable;
  338. particles->trail_length = p_length;
  339. _particles_free_data(particles);
  340. particles->prev_ticks = 0;
  341. particles->phase = 0;
  342. particles->prev_phase = 0;
  343. particles->clear = true;
  344. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  345. }
  346. void ParticlesStorage::particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) {
  347. Particles *particles = particles_owner.get_or_null(p_particles);
  348. ERR_FAIL_COND(!particles);
  349. if (particles->trail_bind_pose_buffer.is_valid() && particles->trail_bind_poses.size() != p_bind_poses.size()) {
  350. _particles_free_data(particles);
  351. particles->prev_ticks = 0;
  352. particles->phase = 0;
  353. particles->prev_phase = 0;
  354. particles->clear = true;
  355. }
  356. particles->trail_bind_poses = p_bind_poses;
  357. particles->trail_bind_poses_dirty = true;
  358. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  359. }
  360. void ParticlesStorage::particles_set_collision_base_size(RID p_particles, real_t p_size) {
  361. Particles *particles = particles_owner.get_or_null(p_particles);
  362. ERR_FAIL_COND(!particles);
  363. particles->collision_base_size = p_size;
  364. }
  365. void ParticlesStorage::particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) {
  366. Particles *particles = particles_owner.get_or_null(p_particles);
  367. ERR_FAIL_COND(!particles);
  368. particles->transform_align = p_transform_align;
  369. }
  370. void ParticlesStorage::particles_set_process_material(RID p_particles, RID p_material) {
  371. Particles *particles = particles_owner.get_or_null(p_particles);
  372. ERR_FAIL_COND(!particles);
  373. particles->process_material = p_material;
  374. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); //the instance buffer may have changed
  375. }
  376. RID ParticlesStorage::particles_get_process_material(RID p_particles) const {
  377. Particles *particles = particles_owner.get_or_null(p_particles);
  378. ERR_FAIL_COND_V(!particles, RID());
  379. return particles->process_material;
  380. }
  381. void ParticlesStorage::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {
  382. Particles *particles = particles_owner.get_or_null(p_particles);
  383. ERR_FAIL_COND(!particles);
  384. particles->draw_order = p_order;
  385. }
  386. void ParticlesStorage::particles_set_draw_passes(RID p_particles, int p_passes) {
  387. Particles *particles = particles_owner.get_or_null(p_particles);
  388. ERR_FAIL_COND(!particles);
  389. particles->draw_passes.resize(p_passes);
  390. }
  391. void ParticlesStorage::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  392. Particles *particles = particles_owner.get_or_null(p_particles);
  393. ERR_FAIL_COND(!particles);
  394. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  395. particles->draw_passes.write[p_pass] = p_mesh;
  396. }
  397. void ParticlesStorage::particles_restart(RID p_particles) {
  398. Particles *particles = particles_owner.get_or_null(p_particles);
  399. ERR_FAIL_COND(!particles);
  400. particles->restart_request = true;
  401. }
  402. void ParticlesStorage::_particles_allocate_emission_buffer(Particles *particles) {
  403. ERR_FAIL_COND(particles->emission_buffer != nullptr);
  404. particles->emission_buffer_data.resize(sizeof(ParticleEmissionBuffer::Data) * particles->amount + sizeof(uint32_t) * 4);
  405. memset(particles->emission_buffer_data.ptrw(), 0, particles->emission_buffer_data.size());
  406. particles->emission_buffer = reinterpret_cast<ParticleEmissionBuffer *>(particles->emission_buffer_data.ptrw());
  407. particles->emission_buffer->particle_max = particles->amount;
  408. particles->emission_storage_buffer = RD::get_singleton()->storage_buffer_create(particles->emission_buffer_data.size(), particles->emission_buffer_data);
  409. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  410. //will need to be re-created
  411. RD::get_singleton()->free(particles->particles_material_uniform_set);
  412. particles->particles_material_uniform_set = RID();
  413. }
  414. }
  415. void ParticlesStorage::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) {
  416. Particles *particles = particles_owner.get_or_null(p_particles);
  417. ERR_FAIL_COND(!particles);
  418. ERR_FAIL_COND(p_particles == p_subemitter_particles);
  419. particles->sub_emitter = p_subemitter_particles;
  420. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  421. RD::get_singleton()->free(particles->particles_material_uniform_set);
  422. particles->particles_material_uniform_set = RID(); //clear and force to re create sub emitting
  423. }
  424. }
  425. void ParticlesStorage::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) {
  426. Particles *particles = particles_owner.get_or_null(p_particles);
  427. ERR_FAIL_COND(!particles);
  428. ERR_FAIL_COND(particles->amount == 0);
  429. if (particles->emitting) {
  430. particles->clear = true;
  431. particles->emitting = false;
  432. }
  433. if (particles->emission_buffer == nullptr) {
  434. _particles_allocate_emission_buffer(particles);
  435. }
  436. particles->inactive = false;
  437. particles->inactive_time = 0;
  438. int32_t idx = particles->emission_buffer->particle_count;
  439. if (idx < particles->emission_buffer->particle_max) {
  440. RendererRD::MaterialStorage::store_transform(p_transform, particles->emission_buffer->data[idx].xform);
  441. particles->emission_buffer->data[idx].velocity[0] = p_velocity.x;
  442. particles->emission_buffer->data[idx].velocity[1] = p_velocity.y;
  443. particles->emission_buffer->data[idx].velocity[2] = p_velocity.z;
  444. particles->emission_buffer->data[idx].custom[0] = p_custom.r;
  445. particles->emission_buffer->data[idx].custom[1] = p_custom.g;
  446. particles->emission_buffer->data[idx].custom[2] = p_custom.b;
  447. particles->emission_buffer->data[idx].custom[3] = p_custom.a;
  448. particles->emission_buffer->data[idx].color[0] = p_color.r;
  449. particles->emission_buffer->data[idx].color[1] = p_color.g;
  450. particles->emission_buffer->data[idx].color[2] = p_color.b;
  451. particles->emission_buffer->data[idx].color[3] = p_color.a;
  452. particles->emission_buffer->data[idx].flags = p_emit_flags;
  453. particles->emission_buffer->particle_count++;
  454. }
  455. }
  456. void ParticlesStorage::particles_request_process(RID p_particles) {
  457. Particles *particles = particles_owner.get_or_null(p_particles);
  458. ERR_FAIL_COND(!particles);
  459. if (!particles->dirty) {
  460. particles->dirty = true;
  461. particles->update_list = particle_update_list;
  462. particle_update_list = particles;
  463. }
  464. }
  465. AABB ParticlesStorage::particles_get_current_aabb(RID p_particles) {
  466. if (RSG::threaded) {
  467. WARN_PRINT_ONCE("Calling this function with threaded rendering enabled stalls the renderer, use with care.");
  468. }
  469. const Particles *particles = particles_owner.get_or_null(p_particles);
  470. ERR_FAIL_COND_V(!particles, AABB());
  471. int total_amount = particles->amount;
  472. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  473. total_amount *= particles->trail_bind_poses.size();
  474. }
  475. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(particles->particle_buffer);
  476. ERR_FAIL_COND_V(buffer.size() != (int)(total_amount * sizeof(ParticleData)), AABB());
  477. Transform3D inv = particles->emission_transform.affine_inverse();
  478. AABB aabb;
  479. if (buffer.size()) {
  480. bool first = true;
  481. const uint8_t *data_ptr = (const uint8_t *)buffer.ptr();
  482. uint32_t particle_data_size = sizeof(ParticleData) + sizeof(float) * particles->userdata_count;
  483. for (int i = 0; i < total_amount; i++) {
  484. const ParticleData &particle_data = *(const ParticleData *)&data_ptr[particle_data_size * i];
  485. if (particle_data.active) {
  486. Vector3 pos = Vector3(particle_data.xform[12], particle_data.xform[13], particle_data.xform[14]);
  487. if (!particles->use_local_coords) {
  488. pos = inv.xform(pos);
  489. }
  490. if (first) {
  491. aabb.position = pos;
  492. first = false;
  493. } else {
  494. aabb.expand_to(pos);
  495. }
  496. }
  497. }
  498. }
  499. float longest_axis_size = 0;
  500. for (int i = 0; i < particles->draw_passes.size(); i++) {
  501. if (particles->draw_passes[i].is_valid()) {
  502. AABB maabb = MeshStorage::get_singleton()->mesh_get_aabb(particles->draw_passes[i], RID());
  503. longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size);
  504. }
  505. }
  506. aabb.grow_by(longest_axis_size);
  507. return aabb;
  508. }
  509. AABB ParticlesStorage::particles_get_aabb(RID p_particles) const {
  510. const Particles *particles = particles_owner.get_or_null(p_particles);
  511. ERR_FAIL_COND_V(!particles, AABB());
  512. return particles->custom_aabb;
  513. }
  514. void ParticlesStorage::particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) {
  515. Particles *particles = particles_owner.get_or_null(p_particles);
  516. ERR_FAIL_COND(!particles);
  517. particles->emission_transform = p_transform;
  518. }
  519. int ParticlesStorage::particles_get_draw_passes(RID p_particles) const {
  520. const Particles *particles = particles_owner.get_or_null(p_particles);
  521. ERR_FAIL_COND_V(!particles, 0);
  522. return particles->draw_passes.size();
  523. }
  524. RID ParticlesStorage::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  525. const Particles *particles = particles_owner.get_or_null(p_particles);
  526. ERR_FAIL_COND_V(!particles, RID());
  527. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  528. return particles->draw_passes[p_pass];
  529. }
  530. void ParticlesStorage::particles_add_collision(RID p_particles, RID p_particles_collision_instance) {
  531. Particles *particles = particles_owner.get_or_null(p_particles);
  532. ERR_FAIL_COND(!particles);
  533. particles->collisions.insert(p_particles_collision_instance);
  534. }
  535. void ParticlesStorage::particles_remove_collision(RID p_particles, RID p_particles_collision_instance) {
  536. Particles *particles = particles_owner.get_or_null(p_particles);
  537. ERR_FAIL_COND(!particles);
  538. particles->collisions.erase(p_particles_collision_instance);
  539. }
  540. void ParticlesStorage::particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, RID p_texture) {
  541. Particles *particles = particles_owner.get_or_null(p_particles);
  542. ERR_FAIL_COND(!particles);
  543. particles->has_sdf_collision = p_enable;
  544. particles->sdf_collision_transform = p_xform;
  545. particles->sdf_collision_to_screen = p_to_screen;
  546. particles->sdf_collision_texture = p_texture;
  547. }
  548. void ParticlesStorage::_particles_process(Particles *p_particles, double p_delta) {
  549. TextureStorage *texture_storage = TextureStorage::get_singleton();
  550. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  551. if (p_particles->particles_material_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(p_particles->particles_material_uniform_set)) {
  552. Vector<RD::Uniform> uniforms;
  553. {
  554. RD::Uniform u;
  555. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  556. u.binding = 0;
  557. u.append_id(p_particles->frame_params_buffer);
  558. uniforms.push_back(u);
  559. }
  560. {
  561. RD::Uniform u;
  562. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  563. u.binding = 1;
  564. u.append_id(p_particles->particle_buffer);
  565. uniforms.push_back(u);
  566. }
  567. {
  568. RD::Uniform u;
  569. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  570. u.binding = 2;
  571. if (p_particles->emission_storage_buffer.is_valid()) {
  572. u.append_id(p_particles->emission_storage_buffer);
  573. } else {
  574. u.append_id(MeshStorage::get_singleton()->get_default_rd_storage_buffer());
  575. }
  576. uniforms.push_back(u);
  577. }
  578. {
  579. RD::Uniform u;
  580. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  581. u.binding = 3;
  582. Particles *sub_emitter = particles_owner.get_or_null(p_particles->sub_emitter);
  583. if (sub_emitter) {
  584. if (sub_emitter->emission_buffer == nullptr) { //no emission buffer, allocate emission buffer
  585. _particles_allocate_emission_buffer(sub_emitter);
  586. }
  587. u.append_id(sub_emitter->emission_storage_buffer);
  588. } else {
  589. u.append_id(MeshStorage::get_singleton()->get_default_rd_storage_buffer());
  590. }
  591. uniforms.push_back(u);
  592. }
  593. p_particles->particles_material_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 1);
  594. }
  595. double new_phase = Math::fmod((double)p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, 1.0);
  596. //move back history (if there is any)
  597. for (uint32_t i = p_particles->frame_history.size() - 1; i > 0; i--) {
  598. p_particles->frame_history[i] = p_particles->frame_history[i - 1];
  599. }
  600. //update current frame
  601. ParticlesFrameParams &frame_params = p_particles->frame_history[0];
  602. if (p_particles->clear) {
  603. p_particles->cycle_number = 0;
  604. p_particles->random_seed = Math::rand();
  605. } else if (new_phase < p_particles->phase) {
  606. if (p_particles->one_shot) {
  607. p_particles->emitting = false;
  608. }
  609. p_particles->cycle_number++;
  610. }
  611. frame_params.emitting = p_particles->emitting;
  612. frame_params.system_phase = new_phase;
  613. frame_params.prev_system_phase = p_particles->phase;
  614. p_particles->phase = new_phase;
  615. frame_params.time = RendererCompositorRD::singleton->get_total_time();
  616. frame_params.delta = p_delta * p_particles->speed_scale;
  617. frame_params.random_seed = p_particles->random_seed;
  618. frame_params.explosiveness = p_particles->explosiveness;
  619. frame_params.randomness = p_particles->randomness;
  620. if (p_particles->use_local_coords) {
  621. RendererRD::MaterialStorage::store_transform(Transform3D(), frame_params.emission_transform);
  622. } else {
  623. RendererRD::MaterialStorage::store_transform(p_particles->emission_transform, frame_params.emission_transform);
  624. }
  625. frame_params.cycle = p_particles->cycle_number;
  626. frame_params.frame = p_particles->frame_counter++;
  627. frame_params.pad0 = 0;
  628. frame_params.pad1 = 0;
  629. frame_params.pad2 = 0;
  630. { //collision and attractors
  631. frame_params.collider_count = 0;
  632. frame_params.attractor_count = 0;
  633. frame_params.particle_size = p_particles->collision_base_size;
  634. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  635. RID collision_heightmap_texture;
  636. Transform3D to_particles;
  637. if (p_particles->use_local_coords) {
  638. to_particles = p_particles->emission_transform.affine_inverse();
  639. }
  640. if (p_particles->has_sdf_collision && RD::get_singleton()->texture_is_valid(p_particles->sdf_collision_texture)) {
  641. //2D collision
  642. Transform2D xform = p_particles->sdf_collision_transform; //will use dotproduct manually so invert beforehand
  643. Transform2D revert = xform.affine_inverse();
  644. frame_params.collider_count = 1;
  645. frame_params.colliders[0].transform[0] = xform.columns[0][0];
  646. frame_params.colliders[0].transform[1] = xform.columns[0][1];
  647. frame_params.colliders[0].transform[2] = 0;
  648. frame_params.colliders[0].transform[3] = xform.columns[2][0];
  649. frame_params.colliders[0].transform[4] = xform.columns[1][0];
  650. frame_params.colliders[0].transform[5] = xform.columns[1][1];
  651. frame_params.colliders[0].transform[6] = 0;
  652. frame_params.colliders[0].transform[7] = xform.columns[2][1];
  653. frame_params.colliders[0].transform[8] = revert.columns[0][0];
  654. frame_params.colliders[0].transform[9] = revert.columns[0][1];
  655. frame_params.colliders[0].transform[10] = 0;
  656. frame_params.colliders[0].transform[11] = revert.columns[2][0];
  657. frame_params.colliders[0].transform[12] = revert.columns[1][0];
  658. frame_params.colliders[0].transform[13] = revert.columns[1][1];
  659. frame_params.colliders[0].transform[14] = 0;
  660. frame_params.colliders[0].transform[15] = revert.columns[2][1];
  661. frame_params.colliders[0].extents[0] = p_particles->sdf_collision_to_screen.size.x;
  662. frame_params.colliders[0].extents[1] = p_particles->sdf_collision_to_screen.size.y;
  663. frame_params.colliders[0].extents[2] = p_particles->sdf_collision_to_screen.position.x;
  664. frame_params.colliders[0].scale = p_particles->sdf_collision_to_screen.position.y;
  665. frame_params.colliders[0].texture_index = 0;
  666. frame_params.colliders[0].type = ParticlesFrameParams::COLLISION_TYPE_2D_SDF;
  667. collision_heightmap_texture = p_particles->sdf_collision_texture;
  668. //replace in all other history frames where used because parameters are no longer valid if screen moves
  669. for (uint32_t i = 1; i < p_particles->frame_history.size(); i++) {
  670. if (p_particles->frame_history[i].collider_count > 0 && p_particles->frame_history[i].colliders[0].type == ParticlesFrameParams::COLLISION_TYPE_2D_SDF) {
  671. p_particles->frame_history[i].colliders[0] = frame_params.colliders[0];
  672. }
  673. }
  674. }
  675. uint32_t collision_3d_textures_used = 0;
  676. for (const RID &E : p_particles->collisions) {
  677. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(E);
  678. if (!pci || !pci->active) {
  679. continue;
  680. }
  681. ParticlesCollision *pc = particles_collision_owner.get_or_null(pci->collision);
  682. ERR_CONTINUE(!pc);
  683. Transform3D to_collider = pci->transform;
  684. if (p_particles->use_local_coords) {
  685. to_collider = to_particles * to_collider;
  686. }
  687. Vector3 scale = to_collider.basis.get_scale();
  688. to_collider.basis.orthonormalize();
  689. if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  690. //attractor
  691. if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) {
  692. continue;
  693. }
  694. ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count];
  695. RendererRD::MaterialStorage::store_transform(to_collider, attr.transform);
  696. attr.strength = pc->attractor_strength;
  697. attr.attenuation = pc->attractor_attenuation;
  698. attr.directionality = pc->attractor_directionality;
  699. switch (pc->type) {
  700. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: {
  701. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE;
  702. float radius = pc->radius;
  703. radius *= (scale.x + scale.y + scale.z) / 3.0;
  704. attr.extents[0] = radius;
  705. attr.extents[1] = radius;
  706. attr.extents[2] = radius;
  707. } break;
  708. case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: {
  709. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX;
  710. Vector3 extents = pc->extents * scale;
  711. attr.extents[0] = extents.x;
  712. attr.extents[1] = extents.y;
  713. attr.extents[2] = extents.z;
  714. } break;
  715. case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: {
  716. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  717. continue;
  718. }
  719. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_VECTOR_FIELD;
  720. Vector3 extents = pc->extents * scale;
  721. attr.extents[0] = extents.x;
  722. attr.extents[1] = extents.y;
  723. attr.extents[2] = extents.z;
  724. attr.texture_index = collision_3d_textures_used;
  725. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  726. collision_3d_textures_used++;
  727. } break;
  728. default: {
  729. }
  730. }
  731. frame_params.attractor_count++;
  732. } else {
  733. //collider
  734. if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) {
  735. continue;
  736. }
  737. ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count];
  738. RendererRD::MaterialStorage::store_transform(to_collider, col.transform);
  739. switch (pc->type) {
  740. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  741. col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE;
  742. float radius = pc->radius;
  743. radius *= (scale.x + scale.y + scale.z) / 3.0;
  744. col.extents[0] = radius;
  745. col.extents[1] = radius;
  746. col.extents[2] = radius;
  747. } break;
  748. case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: {
  749. col.type = ParticlesFrameParams::COLLISION_TYPE_BOX;
  750. Vector3 extents = pc->extents * scale;
  751. col.extents[0] = extents.x;
  752. col.extents[1] = extents.y;
  753. col.extents[2] = extents.z;
  754. } break;
  755. case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: {
  756. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  757. continue;
  758. }
  759. col.type = ParticlesFrameParams::COLLISION_TYPE_SDF;
  760. Vector3 extents = pc->extents * scale;
  761. col.extents[0] = extents.x;
  762. col.extents[1] = extents.y;
  763. col.extents[2] = extents.z;
  764. col.texture_index = collision_3d_textures_used;
  765. col.scale = (scale.x + scale.y + scale.z) * 0.333333333333; //non uniform scale non supported
  766. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  767. collision_3d_textures_used++;
  768. } break;
  769. case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: {
  770. if (collision_heightmap_texture != RID()) { //already taken
  771. continue;
  772. }
  773. col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD;
  774. Vector3 extents = pc->extents * scale;
  775. col.extents[0] = extents.x;
  776. col.extents[1] = extents.y;
  777. col.extents[2] = extents.z;
  778. collision_heightmap_texture = pc->heightfield_texture;
  779. } break;
  780. default: {
  781. }
  782. }
  783. frame_params.collider_count++;
  784. }
  785. }
  786. bool different = false;
  787. if (collision_3d_textures_used == p_particles->collision_3d_textures_used) {
  788. for (int i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  789. if (p_particles->collision_3d_textures[i] != collision_3d_textures[i]) {
  790. different = true;
  791. break;
  792. }
  793. }
  794. }
  795. if (collision_heightmap_texture != p_particles->collision_heightmap_texture) {
  796. different = true;
  797. }
  798. bool uniform_set_valid = RD::get_singleton()->uniform_set_is_valid(p_particles->collision_textures_uniform_set);
  799. if (different || !uniform_set_valid) {
  800. if (uniform_set_valid) {
  801. RD::get_singleton()->free(p_particles->collision_textures_uniform_set);
  802. }
  803. Vector<RD::Uniform> uniforms;
  804. {
  805. RD::Uniform u;
  806. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  807. u.binding = 0;
  808. for (uint32_t i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  809. RID rd_tex;
  810. if (i < collision_3d_textures_used) {
  811. Texture *t = TextureStorage::get_singleton()->get_texture(collision_3d_textures[i]);
  812. if (t && t->type == Texture::TYPE_3D) {
  813. rd_tex = t->rd_texture;
  814. }
  815. }
  816. if (rd_tex == RID()) {
  817. rd_tex = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_3D_WHITE);
  818. }
  819. u.append_id(rd_tex);
  820. }
  821. uniforms.push_back(u);
  822. }
  823. {
  824. RD::Uniform u;
  825. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  826. u.binding = 1;
  827. if (collision_heightmap_texture.is_valid()) {
  828. u.append_id(collision_heightmap_texture);
  829. } else {
  830. u.append_id(texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_BLACK));
  831. }
  832. uniforms.push_back(u);
  833. }
  834. p_particles->collision_textures_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 2);
  835. }
  836. }
  837. ParticlesShader::PushConstant push_constant;
  838. int process_amount = p_particles->amount;
  839. if (p_particles->trails_enabled && p_particles->trail_bind_poses.size() > 1) {
  840. process_amount *= p_particles->trail_bind_poses.size();
  841. }
  842. push_constant.clear = p_particles->clear;
  843. push_constant.total_particles = p_particles->amount;
  844. push_constant.lifetime = p_particles->lifetime;
  845. push_constant.trail_size = p_particles->trail_params.size();
  846. push_constant.use_fractional_delta = p_particles->fractional_delta;
  847. push_constant.sub_emitter_mode = !p_particles->emitting && p_particles->emission_buffer && (p_particles->emission_buffer->particle_count > 0 || p_particles->force_sub_emit);
  848. push_constant.trail_pass = false;
  849. p_particles->force_sub_emit = false; //reset
  850. Particles *sub_emitter = particles_owner.get_or_null(p_particles->sub_emitter);
  851. if (sub_emitter && sub_emitter->emission_storage_buffer.is_valid()) {
  852. // print_line("updating subemitter buffer");
  853. int32_t zero[4] = { 0, sub_emitter->amount, 0, 0 };
  854. RD::get_singleton()->buffer_update(sub_emitter->emission_storage_buffer, 0, sizeof(uint32_t) * 4, zero);
  855. push_constant.can_emit = true;
  856. if (sub_emitter->emitting) {
  857. sub_emitter->emitting = false;
  858. sub_emitter->clear = true; //will need to clear if it was emitting, sorry
  859. }
  860. //make sure the sub emitter processes particles too
  861. sub_emitter->inactive = false;
  862. sub_emitter->inactive_time = 0;
  863. sub_emitter->force_sub_emit = true;
  864. } else {
  865. push_constant.can_emit = false;
  866. }
  867. if (p_particles->emission_buffer && p_particles->emission_buffer->particle_count) {
  868. RD::get_singleton()->buffer_update(p_particles->emission_storage_buffer, 0, sizeof(uint32_t) * 4 + sizeof(ParticleEmissionBuffer::Data) * p_particles->emission_buffer->particle_count, p_particles->emission_buffer);
  869. p_particles->emission_buffer->particle_count = 0;
  870. }
  871. p_particles->clear = false;
  872. if (p_particles->trail_params.size() > 1) {
  873. //fill the trail params
  874. for (uint32_t i = 0; i < p_particles->trail_params.size(); i++) {
  875. uint32_t src_idx = i * p_particles->frame_history.size() / p_particles->trail_params.size();
  876. p_particles->trail_params[i] = p_particles->frame_history[src_idx];
  877. }
  878. } else {
  879. p_particles->trail_params[0] = p_particles->frame_history[0];
  880. }
  881. RD::get_singleton()->buffer_update(p_particles->frame_params_buffer, 0, sizeof(ParticlesFrameParams) * p_particles->trail_params.size(), p_particles->trail_params.ptr());
  882. ParticlesMaterialData *m = static_cast<ParticlesMaterialData *>(material_storage->material_get_data(p_particles->process_material, SHADER_TYPE_PARTICLES));
  883. if (!m) {
  884. m = static_cast<ParticlesMaterialData *>(material_storage->material_get_data(particles_shader.default_material, SHADER_TYPE_PARTICLES));
  885. }
  886. ERR_FAIL_COND(!m);
  887. p_particles->has_collision_cache = m->shader_data->uses_collision;
  888. //todo should maybe compute all particle systems together?
  889. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  890. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, m->shader_data->pipeline);
  891. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles_shader.base_uniform_set, 0);
  892. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->particles_material_uniform_set, 1);
  893. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->collision_textures_uniform_set, 2);
  894. if (m->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(m->uniform_set)) {
  895. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, m->uniform_set, 3);
  896. }
  897. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  898. if (p_particles->trails_enabled && p_particles->trail_bind_poses.size() > 1) {
  899. //trails requires two passes in order to catch particle starts
  900. RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount / p_particles->trail_bind_poses.size(), 1, 1);
  901. RD::get_singleton()->compute_list_add_barrier(compute_list);
  902. push_constant.trail_pass = true;
  903. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  904. RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount - p_particles->amount, 1, 1);
  905. } else {
  906. RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount, 1, 1);
  907. }
  908. RD::get_singleton()->compute_list_end();
  909. }
  910. void ParticlesStorage::particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) {
  911. Particles *particles = particles_owner.get_or_null(p_particles);
  912. ERR_FAIL_COND(!particles);
  913. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) {
  914. return;
  915. }
  916. if (particles->particle_buffer.is_null()) {
  917. return; //particles have not processed yet
  918. }
  919. bool do_sort = particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH;
  920. //copy to sort buffer
  921. if (do_sort && particles->particles_sort_buffer == RID()) {
  922. uint32_t size = particles->amount;
  923. if (size & 1) {
  924. size++; //make multiple of 16
  925. }
  926. size *= sizeof(float) * 2;
  927. particles->particles_sort_buffer = RD::get_singleton()->storage_buffer_create(size);
  928. {
  929. Vector<RD::Uniform> uniforms;
  930. {
  931. RD::Uniform u;
  932. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  933. u.binding = 0;
  934. u.append_id(particles->particles_sort_buffer);
  935. uniforms.push_back(u);
  936. }
  937. particles->particles_sort_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, ParticlesShader::COPY_MODE_FILL_SORT_BUFFER), 1);
  938. }
  939. }
  940. ParticlesShader::CopyPushConstant copy_push_constant;
  941. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  942. int fixed_fps = 60.0;
  943. if (particles->fixed_fps > 0) {
  944. fixed_fps = particles->fixed_fps;
  945. }
  946. copy_push_constant.trail_size = particles->trail_bind_poses.size();
  947. copy_push_constant.trail_total = particles->frame_history.size();
  948. copy_push_constant.frame_delta = 1.0 / fixed_fps;
  949. } else {
  950. copy_push_constant.trail_size = 1;
  951. copy_push_constant.trail_total = 1;
  952. copy_push_constant.frame_delta = 0.0;
  953. }
  954. copy_push_constant.order_by_lifetime = (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME);
  955. copy_push_constant.lifetime_split = MIN(particles->amount * particles->phase, particles->amount - 1);
  956. copy_push_constant.lifetime_reverse = particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME;
  957. copy_push_constant.frame_remainder = particles->interpolate ? particles->frame_remainder : 0.0;
  958. copy_push_constant.total_particles = particles->amount;
  959. copy_push_constant.copy_mode_2d = false;
  960. Vector3 axis = -p_axis; // cameras look to z negative
  961. if (particles->use_local_coords) {
  962. axis = particles->emission_transform.basis.xform_inv(axis).normalized();
  963. }
  964. copy_push_constant.sort_direction[0] = axis.x;
  965. copy_push_constant.sort_direction[1] = axis.y;
  966. copy_push_constant.sort_direction[2] = axis.z;
  967. copy_push_constant.align_up[0] = p_up_axis.x;
  968. copy_push_constant.align_up[1] = p_up_axis.y;
  969. copy_push_constant.align_up[2] = p_up_axis.z;
  970. copy_push_constant.align_mode = particles->transform_align;
  971. if (do_sort) {
  972. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  973. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_SORT_BUFFER + particles->userdata_count * ParticlesShader::COPY_MODE_MAX]);
  974. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  975. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  976. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2);
  977. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  978. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1);
  979. RD::get_singleton()->compute_list_end();
  980. RendererCompositorRD::singleton->get_effects()->sort_buffer(particles->particles_sort_uniform_set, particles->amount);
  981. }
  982. copy_push_constant.total_particles *= copy_push_constant.total_particles;
  983. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  984. uint32_t copy_pipeline = do_sort ? ParticlesShader::COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER : ParticlesShader::COPY_MODE_FILL_INSTANCES;
  985. copy_pipeline += particles->userdata_count * ParticlesShader::COPY_MODE_MAX;
  986. copy_push_constant.copy_mode_2d = particles->mode == RS::PARTICLES_MODE_2D ? 1 : 0;
  987. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[copy_pipeline]);
  988. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  989. if (do_sort) {
  990. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  991. }
  992. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2);
  993. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  994. RD::get_singleton()->compute_list_dispatch_threads(compute_list, copy_push_constant.total_particles, 1, 1);
  995. RD::get_singleton()->compute_list_end();
  996. }
  997. void ParticlesStorage::_particles_update_buffers(Particles *particles) {
  998. uint32_t userdata_count = 0;
  999. const Material *material = MaterialStorage::get_singleton()->get_material(particles->process_material);
  1000. if (material && material->shader && material->shader->data) {
  1001. const ParticlesShaderData *shader_data = static_cast<const ParticlesShaderData *>(material->shader->data);
  1002. userdata_count = shader_data->userdata_count;
  1003. }
  1004. if (userdata_count != particles->userdata_count) {
  1005. // Mismatch userdata, re-create buffers.
  1006. _particles_free_data(particles);
  1007. }
  1008. if (particles->amount > 0 && particles->particle_buffer.is_null()) {
  1009. int total_amount = particles->amount;
  1010. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1011. total_amount *= particles->trail_bind_poses.size();
  1012. }
  1013. uint32_t xform_size = particles->mode == RS::PARTICLES_MODE_2D ? 2 : 3;
  1014. particles->particle_buffer = RD::get_singleton()->storage_buffer_create((sizeof(ParticleData) + userdata_count * sizeof(float) * 4) * total_amount);
  1015. particles->userdata_count = userdata_count;
  1016. particles->particle_instance_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 4 * (xform_size + 1 + 1) * total_amount);
  1017. //needs to clear it
  1018. {
  1019. Vector<RD::Uniform> uniforms;
  1020. {
  1021. RD::Uniform u;
  1022. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1023. u.binding = 1;
  1024. u.append_id(particles->particle_buffer);
  1025. uniforms.push_back(u);
  1026. }
  1027. {
  1028. RD::Uniform u;
  1029. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1030. u.binding = 2;
  1031. u.append_id(particles->particle_instance_buffer);
  1032. uniforms.push_back(u);
  1033. }
  1034. particles->particles_copy_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, 0), 0);
  1035. }
  1036. }
  1037. }
  1038. void ParticlesStorage::update_particles() {
  1039. while (particle_update_list) {
  1040. //use transform feedback to process particles
  1041. Particles *particles = particle_update_list;
  1042. //take and remove
  1043. particle_update_list = particles->update_list;
  1044. particles->update_list = nullptr;
  1045. particles->dirty = false;
  1046. _particles_update_buffers(particles);
  1047. if (particles->restart_request) {
  1048. particles->prev_ticks = 0;
  1049. particles->phase = 0;
  1050. particles->prev_phase = 0;
  1051. particles->clear = true;
  1052. particles->restart_request = false;
  1053. }
  1054. if (particles->inactive && !particles->emitting) {
  1055. //go next
  1056. continue;
  1057. }
  1058. if (particles->emitting) {
  1059. if (particles->inactive) {
  1060. //restart system from scratch
  1061. particles->prev_ticks = 0;
  1062. particles->phase = 0;
  1063. particles->prev_phase = 0;
  1064. particles->clear = true;
  1065. }
  1066. particles->inactive = false;
  1067. particles->inactive_time = 0;
  1068. } else {
  1069. particles->inactive_time += particles->speed_scale * RendererCompositorRD::singleton->get_frame_delta_time();
  1070. if (particles->inactive_time > particles->lifetime * 1.2) {
  1071. particles->inactive = true;
  1072. continue;
  1073. }
  1074. }
  1075. #ifndef _MSC_VER
  1076. #warning Should use display refresh rate for all this
  1077. #endif
  1078. float screen_hz = 60;
  1079. int fixed_fps = 0;
  1080. if (particles->fixed_fps > 0) {
  1081. fixed_fps = particles->fixed_fps;
  1082. } else if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1083. fixed_fps = screen_hz;
  1084. }
  1085. {
  1086. //update trails
  1087. int history_size = 1;
  1088. int trail_steps = 1;
  1089. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1090. history_size = MAX(1, int(particles->trail_length * fixed_fps));
  1091. trail_steps = particles->trail_bind_poses.size();
  1092. }
  1093. if (uint32_t(history_size) != particles->frame_history.size()) {
  1094. particles->frame_history.resize(history_size);
  1095. memset(particles->frame_history.ptr(), 0, sizeof(ParticlesFrameParams) * history_size);
  1096. }
  1097. if (uint32_t(trail_steps) != particles->trail_params.size() || particles->frame_params_buffer.is_null()) {
  1098. particles->trail_params.resize(trail_steps);
  1099. if (particles->frame_params_buffer.is_valid()) {
  1100. RD::get_singleton()->free(particles->frame_params_buffer);
  1101. }
  1102. particles->frame_params_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticlesFrameParams) * trail_steps);
  1103. }
  1104. if (particles->trail_bind_poses.size() > 1 && particles->trail_bind_pose_buffer.is_null()) {
  1105. particles->trail_bind_pose_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 16 * particles->trail_bind_poses.size());
  1106. particles->trail_bind_poses_dirty = true;
  1107. }
  1108. if (particles->trail_bind_pose_uniform_set.is_null()) {
  1109. Vector<RD::Uniform> uniforms;
  1110. {
  1111. RD::Uniform u;
  1112. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1113. u.binding = 0;
  1114. if (particles->trail_bind_pose_buffer.is_valid()) {
  1115. u.append_id(particles->trail_bind_pose_buffer);
  1116. } else {
  1117. u.append_id(MeshStorage::get_singleton()->get_default_rd_storage_buffer());
  1118. }
  1119. uniforms.push_back(u);
  1120. }
  1121. particles->trail_bind_pose_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, 0), 2);
  1122. }
  1123. if (particles->trail_bind_pose_buffer.is_valid() && particles->trail_bind_poses_dirty) {
  1124. if (particles_shader.pose_update_buffer.size() < uint32_t(particles->trail_bind_poses.size()) * 16) {
  1125. particles_shader.pose_update_buffer.resize(particles->trail_bind_poses.size() * 16);
  1126. }
  1127. for (int i = 0; i < particles->trail_bind_poses.size(); i++) {
  1128. RendererRD::MaterialStorage::store_transform(particles->trail_bind_poses[i], &particles_shader.pose_update_buffer[i * 16]);
  1129. }
  1130. RD::get_singleton()->buffer_update(particles->trail_bind_pose_buffer, 0, particles->trail_bind_poses.size() * 16 * sizeof(float), particles_shader.pose_update_buffer.ptr());
  1131. }
  1132. }
  1133. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  1134. if (particles->clear && particles->pre_process_time > 0.0) {
  1135. double frame_time;
  1136. if (fixed_fps > 0) {
  1137. frame_time = 1.0 / fixed_fps;
  1138. } else {
  1139. frame_time = 1.0 / 30.0;
  1140. }
  1141. double todo = particles->pre_process_time;
  1142. while (todo >= 0) {
  1143. _particles_process(particles, frame_time);
  1144. todo -= frame_time;
  1145. }
  1146. }
  1147. if (fixed_fps > 0) {
  1148. double frame_time;
  1149. double decr;
  1150. if (zero_time_scale) {
  1151. frame_time = 0.0;
  1152. decr = 1.0 / fixed_fps;
  1153. } else {
  1154. frame_time = 1.0 / fixed_fps;
  1155. decr = frame_time;
  1156. }
  1157. double delta = RendererCompositorRD::singleton->get_frame_delta_time();
  1158. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  1159. delta = 0.1;
  1160. } else if (delta <= 0.0) { //unlikely but..
  1161. delta = 0.001;
  1162. }
  1163. double todo = particles->frame_remainder + delta;
  1164. while (todo >= frame_time) {
  1165. _particles_process(particles, frame_time);
  1166. todo -= decr;
  1167. }
  1168. particles->frame_remainder = todo;
  1169. } else {
  1170. if (zero_time_scale) {
  1171. _particles_process(particles, 0.0);
  1172. } else {
  1173. _particles_process(particles, RendererCompositorRD::singleton->get_frame_delta_time());
  1174. }
  1175. }
  1176. //copy particles to instance buffer
  1177. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) {
  1178. //does not need view dependent operation, do copy here
  1179. ParticlesShader::CopyPushConstant copy_push_constant;
  1180. int total_amount = particles->amount;
  1181. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1182. total_amount *= particles->trail_bind_poses.size();
  1183. }
  1184. // Affect 2D only.
  1185. if (particles->use_local_coords) {
  1186. // In local mode, particle positions are calculated locally (relative to the node position)
  1187. // and they're also drawn locally.
  1188. // It works as expected, so we just pass an identity transform.
  1189. RendererRD::MaterialStorage::store_transform(Transform3D(), copy_push_constant.inv_emission_transform);
  1190. } else {
  1191. // In global mode, particle positions are calculated globally (relative to the canvas origin)
  1192. // but they're drawn locally.
  1193. // So, we need to pass the inverse of the emission transform to bring the
  1194. // particles to local coordinates before drawing.
  1195. Transform3D inv = particles->emission_transform.affine_inverse();
  1196. RendererRD::MaterialStorage::store_transform(inv, copy_push_constant.inv_emission_transform);
  1197. }
  1198. copy_push_constant.total_particles = total_amount;
  1199. copy_push_constant.frame_remainder = particles->interpolate ? particles->frame_remainder : 0.0;
  1200. copy_push_constant.align_mode = particles->transform_align;
  1201. copy_push_constant.align_up[0] = 0;
  1202. copy_push_constant.align_up[1] = 0;
  1203. copy_push_constant.align_up[2] = 0;
  1204. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1205. copy_push_constant.trail_size = particles->trail_bind_poses.size();
  1206. copy_push_constant.trail_total = particles->frame_history.size();
  1207. copy_push_constant.frame_delta = 1.0 / fixed_fps;
  1208. } else {
  1209. copy_push_constant.trail_size = 1;
  1210. copy_push_constant.trail_total = 1;
  1211. copy_push_constant.frame_delta = 0.0;
  1212. }
  1213. copy_push_constant.order_by_lifetime = (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME);
  1214. copy_push_constant.lifetime_split = MIN(particles->amount * particles->phase, particles->amount - 1);
  1215. copy_push_constant.lifetime_reverse = particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME;
  1216. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1217. copy_push_constant.copy_mode_2d = particles->mode == RS::PARTICLES_MODE_2D ? 1 : 0;
  1218. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES + particles->userdata_count * ParticlesShader::COPY_MODE_MAX]);
  1219. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  1220. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2);
  1221. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  1222. RD::get_singleton()->compute_list_dispatch_threads(compute_list, total_amount, 1, 1);
  1223. RD::get_singleton()->compute_list_end();
  1224. }
  1225. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1226. }
  1227. }
  1228. bool ParticlesStorage::particles_is_inactive(RID p_particles) const {
  1229. ERR_FAIL_COND_V_MSG(RSG::threaded, false, "This function should never be used with threaded rendering, as it stalls the renderer.");
  1230. const Particles *particles = particles_owner.get_or_null(p_particles);
  1231. ERR_FAIL_COND_V(!particles, false);
  1232. return !particles->emitting && particles->inactive;
  1233. }
  1234. /* Particles SHADER */
  1235. void ParticlesStorage::ParticlesShaderData::set_path_hint(const String &p_path) {
  1236. path = p_path;
  1237. }
  1238. void ParticlesStorage::ParticlesShaderData::set_code(const String &p_code) {
  1239. ParticlesStorage *particles_storage = ParticlesStorage::get_singleton();
  1240. //compile
  1241. code = p_code;
  1242. valid = false;
  1243. ubo_size = 0;
  1244. uniforms.clear();
  1245. uses_collision = false;
  1246. if (code.is_empty()) {
  1247. return; //just invalid, but no error
  1248. }
  1249. ShaderCompiler::GeneratedCode gen_code;
  1250. ShaderCompiler::IdentifierActions actions;
  1251. actions.entry_point_stages["start"] = ShaderCompiler::STAGE_COMPUTE;
  1252. actions.entry_point_stages["process"] = ShaderCompiler::STAGE_COMPUTE;
  1253. /*
  1254. uses_time = false;
  1255. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  1256. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  1257. actions.usage_flag_pointers["TIME"] = &uses_time;
  1258. */
  1259. actions.usage_flag_pointers["COLLIDED"] = &uses_collision;
  1260. userdata_count = 0;
  1261. for (uint32_t i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  1262. userdatas_used[i] = false;
  1263. actions.usage_flag_pointers["USERDATA" + itos(i + 1)] = &userdatas_used[i];
  1264. }
  1265. actions.uniforms = &uniforms;
  1266. Error err = particles_storage->particles_shader.compiler.compile(RS::SHADER_PARTICLES, code, &actions, path, gen_code);
  1267. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  1268. if (version.is_null()) {
  1269. version = particles_storage->particles_shader.shader.version_create();
  1270. }
  1271. for (uint32_t i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  1272. if (userdatas_used[i]) {
  1273. userdata_count++;
  1274. }
  1275. }
  1276. particles_storage->particles_shader.shader.version_set_compute_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_COMPUTE], gen_code.defines);
  1277. ERR_FAIL_COND(!particles_storage->particles_shader.shader.version_is_valid(version));
  1278. ubo_size = gen_code.uniform_total_size;
  1279. ubo_offsets = gen_code.uniform_offsets;
  1280. texture_uniforms = gen_code.texture_uniforms;
  1281. //update pipelines
  1282. pipeline = RD::get_singleton()->compute_pipeline_create(particles_storage->particles_shader.shader.version_get_shader(version, 0));
  1283. valid = true;
  1284. }
  1285. void ParticlesStorage::ParticlesShaderData::set_default_texture_param(const StringName &p_name, RID p_texture, int p_index) {
  1286. if (!p_texture.is_valid()) {
  1287. if (default_texture_params.has(p_name) && default_texture_params[p_name].has(p_index)) {
  1288. default_texture_params[p_name].erase(p_index);
  1289. if (default_texture_params[p_name].is_empty()) {
  1290. default_texture_params.erase(p_name);
  1291. }
  1292. }
  1293. } else {
  1294. if (!default_texture_params.has(p_name)) {
  1295. default_texture_params[p_name] = HashMap<int, RID>();
  1296. }
  1297. default_texture_params[p_name][p_index] = p_texture;
  1298. }
  1299. }
  1300. void ParticlesStorage::ParticlesShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  1301. HashMap<int, StringName> order;
  1302. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : uniforms) {
  1303. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  1304. continue;
  1305. }
  1306. if (E.value.texture_order >= 0) {
  1307. order[E.value.texture_order + 100000] = E.key;
  1308. } else {
  1309. order[E.value.order] = E.key;
  1310. }
  1311. }
  1312. String last_group;
  1313. for (const KeyValue<int, StringName> &E : order) {
  1314. String group = uniforms[E.value].group;
  1315. if (!uniforms[E.value].subgroup.is_empty()) {
  1316. group += "::" + uniforms[E.value].subgroup;
  1317. }
  1318. if (group != last_group) {
  1319. PropertyInfo pi;
  1320. pi.usage = PROPERTY_USAGE_GROUP;
  1321. pi.name = group;
  1322. p_param_list->push_back(pi);
  1323. last_group = group;
  1324. }
  1325. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E.value]);
  1326. pi.name = E.value;
  1327. p_param_list->push_back(pi);
  1328. }
  1329. }
  1330. void ParticlesStorage::ParticlesShaderData::get_instance_param_list(List<RendererMaterialStorage::InstanceShaderParam> *p_param_list) const {
  1331. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : uniforms) {
  1332. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  1333. continue;
  1334. }
  1335. RendererMaterialStorage::InstanceShaderParam p;
  1336. p.info = ShaderLanguage::uniform_to_property_info(E.value);
  1337. p.info.name = E.key; //supply name
  1338. p.index = E.value.instance_index;
  1339. p.default_value = ShaderLanguage::constant_value_to_variant(E.value.default_value, E.value.type, E.value.array_size, E.value.hint);
  1340. p_param_list->push_back(p);
  1341. }
  1342. }
  1343. bool ParticlesStorage::ParticlesShaderData::is_param_texture(const StringName &p_param) const {
  1344. if (!uniforms.has(p_param)) {
  1345. return false;
  1346. }
  1347. return uniforms[p_param].texture_order >= 0;
  1348. }
  1349. bool ParticlesStorage::ParticlesShaderData::is_animated() const {
  1350. return false;
  1351. }
  1352. bool ParticlesStorage::ParticlesShaderData::casts_shadows() const {
  1353. return false;
  1354. }
  1355. Variant ParticlesStorage::ParticlesShaderData::get_default_parameter(const StringName &p_parameter) const {
  1356. if (uniforms.has(p_parameter)) {
  1357. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  1358. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  1359. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.array_size, uniform.hint);
  1360. }
  1361. return Variant();
  1362. }
  1363. RS::ShaderNativeSourceCode ParticlesStorage::ParticlesShaderData::get_native_source_code() const {
  1364. return ParticlesStorage::get_singleton()->particles_shader.shader.version_get_native_source_code(version);
  1365. }
  1366. ParticlesStorage::ParticlesShaderData::~ParticlesShaderData() {
  1367. //pipeline variants will clear themselves if shader is gone
  1368. if (version.is_valid()) {
  1369. ParticlesStorage::get_singleton()->particles_shader.shader.version_free(version);
  1370. }
  1371. }
  1372. ShaderData *ParticlesStorage::_create_particles_shader_func() {
  1373. ParticlesShaderData *shader_data = memnew(ParticlesShaderData);
  1374. return shader_data;
  1375. }
  1376. bool ParticlesStorage::ParticlesMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  1377. return update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set, ParticlesStorage::get_singleton()->particles_shader.shader.version_get_shader(shader_data->version, 0), 3);
  1378. }
  1379. ParticlesStorage::ParticlesMaterialData::~ParticlesMaterialData() {
  1380. free_parameters_uniform_set(uniform_set);
  1381. }
  1382. MaterialData *ParticlesStorage::_create_particles_material_func(ParticlesShaderData *p_shader) {
  1383. ParticlesMaterialData *material_data = memnew(ParticlesMaterialData);
  1384. material_data->shader_data = p_shader;
  1385. //update will happen later anyway so do nothing.
  1386. return material_data;
  1387. }
  1388. ////////
  1389. /* PARTICLES COLLISION API */
  1390. RID ParticlesStorage::particles_collision_allocate() {
  1391. return particles_collision_owner.allocate_rid();
  1392. }
  1393. void ParticlesStorage::particles_collision_initialize(RID p_rid) {
  1394. particles_collision_owner.initialize_rid(p_rid, ParticlesCollision());
  1395. }
  1396. void ParticlesStorage::particles_collision_free(RID p_rid) {
  1397. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_rid);
  1398. if (particles_collision->heightfield_texture.is_valid()) {
  1399. RD::get_singleton()->free(particles_collision->heightfield_texture);
  1400. }
  1401. particles_collision->dependency.deleted_notify(p_rid);
  1402. particles_collision_owner.free(p_rid);
  1403. }
  1404. RID ParticlesStorage::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const {
  1405. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1406. ERR_FAIL_COND_V(!particles_collision, RID());
  1407. ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, RID());
  1408. if (particles_collision->heightfield_texture == RID()) {
  1409. //create
  1410. const int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 };
  1411. Size2i size;
  1412. if (particles_collision->extents.x > particles_collision->extents.z) {
  1413. size.x = resolutions[particles_collision->heightfield_resolution];
  1414. size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x);
  1415. } else {
  1416. size.y = resolutions[particles_collision->heightfield_resolution];
  1417. size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y);
  1418. }
  1419. RD::TextureFormat tf;
  1420. tf.format = RD::DATA_FORMAT_D32_SFLOAT;
  1421. tf.width = size.x;
  1422. tf.height = size.y;
  1423. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1424. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1425. particles_collision->heightfield_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1426. Vector<RID> fb_tex;
  1427. fb_tex.push_back(particles_collision->heightfield_texture);
  1428. particles_collision->heightfield_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  1429. particles_collision->heightfield_fb_size = size;
  1430. }
  1431. return particles_collision->heightfield_fb;
  1432. }
  1433. void ParticlesStorage::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) {
  1434. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1435. ERR_FAIL_COND(!particles_collision);
  1436. if (p_type == particles_collision->type) {
  1437. return;
  1438. }
  1439. if (particles_collision->heightfield_texture.is_valid()) {
  1440. RD::get_singleton()->free(particles_collision->heightfield_texture);
  1441. particles_collision->heightfield_texture = RID();
  1442. }
  1443. particles_collision->type = p_type;
  1444. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1445. }
  1446. void ParticlesStorage::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) {
  1447. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1448. ERR_FAIL_COND(!particles_collision);
  1449. particles_collision->cull_mask = p_cull_mask;
  1450. }
  1451. void ParticlesStorage::particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) {
  1452. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1453. ERR_FAIL_COND(!particles_collision);
  1454. particles_collision->radius = p_radius;
  1455. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1456. }
  1457. void ParticlesStorage::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) {
  1458. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1459. ERR_FAIL_COND(!particles_collision);
  1460. particles_collision->extents = p_extents;
  1461. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1462. }
  1463. void ParticlesStorage::particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) {
  1464. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1465. ERR_FAIL_COND(!particles_collision);
  1466. particles_collision->attractor_strength = p_strength;
  1467. }
  1468. void ParticlesStorage::particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) {
  1469. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1470. ERR_FAIL_COND(!particles_collision);
  1471. particles_collision->attractor_directionality = p_directionality;
  1472. }
  1473. void ParticlesStorage::particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) {
  1474. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1475. ERR_FAIL_COND(!particles_collision);
  1476. particles_collision->attractor_attenuation = p_curve;
  1477. }
  1478. void ParticlesStorage::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) {
  1479. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1480. ERR_FAIL_COND(!particles_collision);
  1481. particles_collision->field_texture = p_texture;
  1482. }
  1483. void ParticlesStorage::particles_collision_height_field_update(RID p_particles_collision) {
  1484. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1485. ERR_FAIL_COND(!particles_collision);
  1486. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1487. }
  1488. void ParticlesStorage::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) {
  1489. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1490. ERR_FAIL_COND(!particles_collision);
  1491. ERR_FAIL_INDEX(p_resolution, RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX);
  1492. if (particles_collision->heightfield_resolution == p_resolution) {
  1493. return;
  1494. }
  1495. particles_collision->heightfield_resolution = p_resolution;
  1496. if (particles_collision->heightfield_texture.is_valid()) {
  1497. RD::get_singleton()->free(particles_collision->heightfield_texture);
  1498. particles_collision->heightfield_texture = RID();
  1499. }
  1500. }
  1501. AABB ParticlesStorage::particles_collision_get_aabb(RID p_particles_collision) const {
  1502. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1503. ERR_FAIL_COND_V(!particles_collision, AABB());
  1504. switch (particles_collision->type) {
  1505. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT:
  1506. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  1507. AABB aabb;
  1508. aabb.position = -Vector3(1, 1, 1) * particles_collision->radius;
  1509. aabb.size = Vector3(2, 2, 2) * particles_collision->radius;
  1510. return aabb;
  1511. }
  1512. default: {
  1513. AABB aabb;
  1514. aabb.position = -particles_collision->extents;
  1515. aabb.size = particles_collision->extents * 2;
  1516. return aabb;
  1517. }
  1518. }
  1519. return AABB();
  1520. }
  1521. Vector3 ParticlesStorage::particles_collision_get_extents(RID p_particles_collision) const {
  1522. const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1523. ERR_FAIL_COND_V(!particles_collision, Vector3());
  1524. return particles_collision->extents;
  1525. }
  1526. bool ParticlesStorage::particles_collision_is_heightfield(RID p_particles_collision) const {
  1527. const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1528. ERR_FAIL_COND_V(!particles_collision, false);
  1529. return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE;
  1530. }
  1531. RID ParticlesStorage::particles_collision_instance_create(RID p_collision) {
  1532. ParticlesCollisionInstance pci;
  1533. pci.collision = p_collision;
  1534. return particles_collision_instance_owner.make_rid(pci);
  1535. }
  1536. void ParticlesStorage::particles_collision_instance_free(RID p_rid) {
  1537. particles_collision_instance_owner.free(p_rid);
  1538. }
  1539. void ParticlesStorage::particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) {
  1540. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance);
  1541. ERR_FAIL_COND(!pci);
  1542. pci->transform = p_transform;
  1543. }
  1544. void ParticlesStorage::particles_collision_instance_set_active(RID p_collision_instance, bool p_active) {
  1545. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance);
  1546. ERR_FAIL_COND(!pci);
  1547. pci->active = p_active;
  1548. }