particles_storage.cpp 73 KB

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