particles_storage.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. #ifdef GLES3_ENABLED
  31. #include "particles_storage.h"
  32. #include "material_storage.h"
  33. #include "mesh_storage.h"
  34. #include "texture_storage.h"
  35. #include "utilities.h"
  36. #include "servers/rendering/rendering_server_default.h"
  37. using namespace GLES3;
  38. ParticlesStorage *ParticlesStorage::singleton = nullptr;
  39. ParticlesStorage *ParticlesStorage::get_singleton() {
  40. return singleton;
  41. }
  42. ParticlesStorage::ParticlesStorage() {
  43. singleton = this;
  44. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  45. {
  46. String global_defines;
  47. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  48. material_storage->shaders.particles_process_shader.initialize(global_defines, 1);
  49. }
  50. {
  51. // default material and shader for particles shader
  52. particles_shader.default_shader = material_storage->shader_allocate();
  53. material_storage->shader_initialize(particles_shader.default_shader);
  54. material_storage->shader_set_code(particles_shader.default_shader, R"(
  55. // Default particles shader.
  56. shader_type particles;
  57. void process() {
  58. COLOR = vec4(1.0);
  59. }
  60. )");
  61. particles_shader.default_material = material_storage->material_allocate();
  62. material_storage->material_initialize(particles_shader.default_material);
  63. material_storage->material_set_shader(particles_shader.default_material, particles_shader.default_shader);
  64. }
  65. {
  66. particles_shader.copy_shader.initialize();
  67. particles_shader.copy_shader_version = particles_shader.copy_shader.version_create();
  68. }
  69. }
  70. ParticlesStorage::~ParticlesStorage() {
  71. singleton = nullptr;
  72. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  73. material_storage->material_free(particles_shader.default_material);
  74. material_storage->shader_free(particles_shader.default_shader);
  75. particles_shader.copy_shader.version_free(particles_shader.copy_shader_version);
  76. }
  77. /* PARTICLES */
  78. RID ParticlesStorage::particles_allocate() {
  79. return particles_owner.allocate_rid();
  80. }
  81. void ParticlesStorage::particles_initialize(RID p_rid) {
  82. particles_owner.initialize_rid(p_rid);
  83. }
  84. void ParticlesStorage::particles_free(RID p_rid) {
  85. Particles *particles = particles_owner.get_or_null(p_rid);
  86. particles->dependency.deleted_notify(p_rid);
  87. particles->update_list.remove_from_list();
  88. _particles_free_data(particles);
  89. particles_owner.free(p_rid);
  90. }
  91. void ParticlesStorage::particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) {
  92. Particles *particles = particles_owner.get_or_null(p_particles);
  93. ERR_FAIL_COND(!particles);
  94. if (particles->mode == p_mode) {
  95. return;
  96. }
  97. _particles_free_data(particles);
  98. particles->mode = p_mode;
  99. }
  100. void ParticlesStorage::particles_set_emitting(RID p_particles, bool p_emitting) {
  101. Particles *particles = particles_owner.get_or_null(p_particles);
  102. ERR_FAIL_COND(!particles);
  103. particles->emitting = p_emitting;
  104. }
  105. bool ParticlesStorage::particles_get_emitting(RID p_particles) {
  106. ERR_FAIL_COND_V_MSG(RSG::threaded, false, "This function should never be used with threaded rendering, as it stalls the renderer.");
  107. Particles *particles = particles_owner.get_or_null(p_particles);
  108. ERR_FAIL_COND_V(!particles, false);
  109. return particles->emitting;
  110. }
  111. void ParticlesStorage::_particles_free_data(Particles *particles) {
  112. particles->userdata_count = 0;
  113. particles->instance_buffer_size_cache = 0;
  114. particles->instance_buffer_stride_cache = 0;
  115. particles->num_attrib_arrays_cache = 0;
  116. particles->process_buffer_stride_cache = 0;
  117. if (particles->front_process_buffer != 0) {
  118. glDeleteVertexArrays(1, &particles->front_vertex_array);
  119. GLES3::Utilities::get_singleton()->buffer_free_data(particles->front_process_buffer);
  120. GLES3::Utilities::get_singleton()->buffer_free_data(particles->front_instance_buffer);
  121. particles->front_vertex_array = 0;
  122. particles->front_process_buffer = 0;
  123. particles->front_instance_buffer = 0;
  124. glDeleteVertexArrays(1, &particles->back_vertex_array);
  125. GLES3::Utilities::get_singleton()->buffer_free_data(particles->back_process_buffer);
  126. GLES3::Utilities::get_singleton()->buffer_free_data(particles->back_instance_buffer);
  127. particles->back_vertex_array = 0;
  128. particles->back_process_buffer = 0;
  129. particles->back_instance_buffer = 0;
  130. }
  131. if (particles->sort_buffer != 0) {
  132. GLES3::Utilities::get_singleton()->buffer_free_data(particles->last_frame_buffer);
  133. GLES3::Utilities::get_singleton()->buffer_free_data(particles->sort_buffer);
  134. particles->last_frame_buffer = 0;
  135. particles->sort_buffer = 0;
  136. particles->sort_buffer_filled = false;
  137. particles->last_frame_buffer_filled = false;
  138. }
  139. if (particles->frame_params_ubo != 0) {
  140. GLES3::Utilities::get_singleton()->buffer_free_data(particles->frame_params_ubo);
  141. particles->frame_params_ubo = 0;
  142. }
  143. }
  144. void ParticlesStorage::particles_set_amount(RID p_particles, int p_amount) {
  145. Particles *particles = particles_owner.get_or_null(p_particles);
  146. ERR_FAIL_COND(!particles);
  147. if (particles->amount == p_amount) {
  148. return;
  149. }
  150. _particles_free_data(particles);
  151. particles->amount = p_amount;
  152. particles->prev_ticks = 0;
  153. particles->phase = 0;
  154. particles->prev_phase = 0;
  155. particles->clear = true;
  156. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  157. }
  158. void ParticlesStorage::particles_set_lifetime(RID p_particles, double p_lifetime) {
  159. Particles *particles = particles_owner.get_or_null(p_particles);
  160. ERR_FAIL_COND(!particles);
  161. particles->lifetime = p_lifetime;
  162. }
  163. void ParticlesStorage::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  164. Particles *particles = particles_owner.get_or_null(p_particles);
  165. ERR_FAIL_COND(!particles);
  166. particles->one_shot = p_one_shot;
  167. }
  168. void ParticlesStorage::particles_set_pre_process_time(RID p_particles, double p_time) {
  169. Particles *particles = particles_owner.get_or_null(p_particles);
  170. ERR_FAIL_COND(!particles);
  171. particles->pre_process_time = p_time;
  172. }
  173. void ParticlesStorage::particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) {
  174. Particles *particles = particles_owner.get_or_null(p_particles);
  175. ERR_FAIL_COND(!particles);
  176. particles->explosiveness = p_ratio;
  177. }
  178. void ParticlesStorage::particles_set_randomness_ratio(RID p_particles, real_t p_ratio) {
  179. Particles *particles = particles_owner.get_or_null(p_particles);
  180. ERR_FAIL_COND(!particles);
  181. particles->randomness = p_ratio;
  182. }
  183. void ParticlesStorage::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  184. Particles *particles = particles_owner.get_or_null(p_particles);
  185. ERR_FAIL_COND(!particles);
  186. particles->custom_aabb = p_aabb;
  187. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  188. }
  189. void ParticlesStorage::particles_set_speed_scale(RID p_particles, double p_scale) {
  190. Particles *particles = particles_owner.get_or_null(p_particles);
  191. ERR_FAIL_COND(!particles);
  192. particles->speed_scale = p_scale;
  193. }
  194. void ParticlesStorage::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  195. Particles *particles = particles_owner.get_or_null(p_particles);
  196. ERR_FAIL_COND(!particles);
  197. particles->use_local_coords = p_enable;
  198. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  199. }
  200. void ParticlesStorage::particles_set_fixed_fps(RID p_particles, int p_fps) {
  201. Particles *particles = particles_owner.get_or_null(p_particles);
  202. ERR_FAIL_COND(!particles);
  203. particles->fixed_fps = p_fps;
  204. _particles_free_data(particles);
  205. particles->prev_ticks = 0;
  206. particles->phase = 0;
  207. particles->prev_phase = 0;
  208. particles->clear = true;
  209. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  210. }
  211. void ParticlesStorage::particles_set_interpolate(RID p_particles, bool p_enable) {
  212. Particles *particles = particles_owner.get_or_null(p_particles);
  213. ERR_FAIL_COND(!particles);
  214. particles->interpolate = p_enable;
  215. }
  216. void ParticlesStorage::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  217. Particles *particles = particles_owner.get_or_null(p_particles);
  218. ERR_FAIL_COND(!particles);
  219. particles->fractional_delta = p_enable;
  220. }
  221. void ParticlesStorage::particles_set_trails(RID p_particles, bool p_enable, double p_length) {
  222. if (p_enable) {
  223. WARN_PRINT_ONCE_ED("The GL Compatibility rendering backend does not support particle trails.");
  224. }
  225. }
  226. void ParticlesStorage::particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) {
  227. if (p_bind_poses.size() != 0) {
  228. WARN_PRINT_ONCE_ED("The GL Compatibility rendering backend does not support particle trails.");
  229. }
  230. }
  231. void ParticlesStorage::particles_set_collision_base_size(RID p_particles, real_t p_size) {
  232. Particles *particles = particles_owner.get_or_null(p_particles);
  233. ERR_FAIL_COND(!particles);
  234. particles->collision_base_size = p_size;
  235. }
  236. void ParticlesStorage::particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) {
  237. Particles *particles = particles_owner.get_or_null(p_particles);
  238. ERR_FAIL_COND(!particles);
  239. particles->transform_align = p_transform_align;
  240. }
  241. void ParticlesStorage::particles_set_process_material(RID p_particles, RID p_material) {
  242. Particles *particles = particles_owner.get_or_null(p_particles);
  243. ERR_FAIL_COND(!particles);
  244. particles->process_material = p_material;
  245. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); //the instance buffer may have changed
  246. }
  247. RID ParticlesStorage::particles_get_process_material(RID p_particles) const {
  248. Particles *particles = particles_owner.get_or_null(p_particles);
  249. ERR_FAIL_COND_V(!particles, RID());
  250. return particles->process_material;
  251. }
  252. void ParticlesStorage::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {
  253. Particles *particles = particles_owner.get_or_null(p_particles);
  254. ERR_FAIL_COND(!particles);
  255. particles->draw_order = p_order;
  256. }
  257. void ParticlesStorage::particles_set_draw_passes(RID p_particles, int p_passes) {
  258. Particles *particles = particles_owner.get_or_null(p_particles);
  259. ERR_FAIL_COND(!particles);
  260. particles->draw_passes.resize(p_passes);
  261. }
  262. void ParticlesStorage::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  263. Particles *particles = particles_owner.get_or_null(p_particles);
  264. ERR_FAIL_COND(!particles);
  265. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  266. particles->draw_passes.write[p_pass] = p_mesh;
  267. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  268. }
  269. void ParticlesStorage::particles_restart(RID p_particles) {
  270. Particles *particles = particles_owner.get_or_null(p_particles);
  271. ERR_FAIL_COND(!particles);
  272. particles->restart_request = true;
  273. }
  274. void ParticlesStorage::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) {
  275. if (p_subemitter_particles.is_valid()) {
  276. WARN_PRINT_ONCE_ED("The GL Compatibility rendering backend does not support particle sub-emitters.");
  277. }
  278. }
  279. 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) {
  280. WARN_PRINT_ONCE_ED("The GL Compatibility rendering backend does not support manually emitting particles.");
  281. }
  282. void ParticlesStorage::particles_request_process(RID p_particles) {
  283. Particles *particles = particles_owner.get_or_null(p_particles);
  284. ERR_FAIL_COND(!particles);
  285. if (!particles->dirty) {
  286. particles->dirty = true;
  287. if (!particles->update_list.in_list()) {
  288. particle_update_list.add(&particles->update_list);
  289. }
  290. }
  291. }
  292. AABB ParticlesStorage::particles_get_current_aabb(RID p_particles) {
  293. if (RSG::threaded) {
  294. WARN_PRINT_ONCE("Calling this function with threaded rendering enabled stalls the renderer, use with care.");
  295. }
  296. const Particles *particles = particles_owner.get_or_null(p_particles);
  297. ERR_FAIL_COND_V(!particles, AABB());
  298. int total_amount = particles->amount;
  299. // If available, read from the sort buffer which should be 2 frames out of date.
  300. // This will help alleviate GPU stalls.
  301. GLuint read_buffer = particles->sort_buffer_filled ? particles->sort_buffer : particles->back_instance_buffer;
  302. Vector<uint8_t> buffer = Utilities::buffer_get_data(GL_ARRAY_BUFFER, read_buffer, total_amount * sizeof(ParticleInstanceData3D));
  303. ERR_FAIL_COND_V(buffer.size() != (int)(total_amount * sizeof(ParticleInstanceData3D)), AABB());
  304. Transform3D inv = particles->emission_transform.affine_inverse();
  305. AABB aabb;
  306. if (buffer.size()) {
  307. bool first = true;
  308. const uint8_t *data_ptr = (const uint8_t *)buffer.ptr();
  309. uint32_t particle_data_size = sizeof(ParticleInstanceData3D) + sizeof(float) * particles->userdata_count;
  310. for (int i = 0; i < total_amount; i++) {
  311. const ParticleInstanceData3D &particle_data = *(const ParticleInstanceData3D *)&data_ptr[particle_data_size * i];
  312. // If scale is 0.0, we assume the particle is inactive.
  313. if (particle_data.xform[0] > 0.0) {
  314. Vector3 pos = Vector3(particle_data.xform[3], particle_data.xform[7], particle_data.xform[11]);
  315. if (!particles->use_local_coords) {
  316. pos = inv.xform(pos);
  317. }
  318. if (first) {
  319. aabb.position = pos;
  320. first = false;
  321. } else {
  322. aabb.expand_to(pos);
  323. }
  324. }
  325. }
  326. }
  327. float longest_axis_size = 0;
  328. for (int i = 0; i < particles->draw_passes.size(); i++) {
  329. if (particles->draw_passes[i].is_valid()) {
  330. AABB maabb = MeshStorage::get_singleton()->mesh_get_aabb(particles->draw_passes[i], RID());
  331. longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size);
  332. }
  333. }
  334. aabb.grow_by(longest_axis_size);
  335. return aabb;
  336. }
  337. AABB ParticlesStorage::particles_get_aabb(RID p_particles) const {
  338. const Particles *particles = particles_owner.get_or_null(p_particles);
  339. ERR_FAIL_COND_V(!particles, AABB());
  340. return particles->custom_aabb;
  341. }
  342. void ParticlesStorage::particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) {
  343. Particles *particles = particles_owner.get_or_null(p_particles);
  344. ERR_FAIL_COND(!particles);
  345. particles->emission_transform = p_transform;
  346. }
  347. int ParticlesStorage::particles_get_draw_passes(RID p_particles) const {
  348. const Particles *particles = particles_owner.get_or_null(p_particles);
  349. ERR_FAIL_COND_V(!particles, 0);
  350. return particles->draw_passes.size();
  351. }
  352. RID ParticlesStorage::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  353. const Particles *particles = particles_owner.get_or_null(p_particles);
  354. ERR_FAIL_COND_V(!particles, RID());
  355. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  356. return particles->draw_passes[p_pass];
  357. }
  358. void ParticlesStorage::particles_add_collision(RID p_particles, RID p_particles_collision_instance) {
  359. Particles *particles = particles_owner.get_or_null(p_particles);
  360. ERR_FAIL_COND(!particles);
  361. particles->collisions.insert(p_particles_collision_instance);
  362. }
  363. void ParticlesStorage::particles_remove_collision(RID p_particles, RID p_particles_collision_instance) {
  364. Particles *particles = particles_owner.get_or_null(p_particles);
  365. ERR_FAIL_COND(!particles);
  366. particles->collisions.erase(p_particles_collision_instance);
  367. }
  368. void ParticlesStorage::particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, GLuint p_texture) {
  369. Particles *particles = particles_owner.get_or_null(p_particles);
  370. ERR_FAIL_COND(!particles);
  371. particles->has_sdf_collision = p_enable;
  372. particles->sdf_collision_transform = p_xform;
  373. particles->sdf_collision_to_screen = p_to_screen;
  374. particles->sdf_collision_texture = p_texture;
  375. }
  376. // Does one step of processing particles by reading from back_process_buffer and writing to front_process_buffer.
  377. void ParticlesStorage::_particles_process(Particles *p_particles, double p_delta) {
  378. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  379. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  380. double new_phase = Math::fmod(p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, 1.0);
  381. //update current frame
  382. ParticlesFrameParams frame_params;
  383. if (p_particles->clear) {
  384. p_particles->cycle_number = 0;
  385. p_particles->random_seed = Math::rand();
  386. } else if (new_phase < p_particles->phase) {
  387. if (p_particles->one_shot) {
  388. p_particles->emitting = false;
  389. }
  390. p_particles->cycle_number++;
  391. }
  392. frame_params.emitting = p_particles->emitting;
  393. frame_params.system_phase = new_phase;
  394. frame_params.prev_system_phase = p_particles->phase;
  395. p_particles->phase = new_phase;
  396. frame_params.time = RSG::rasterizer->get_total_time();
  397. frame_params.delta = p_delta * p_particles->speed_scale;
  398. frame_params.random_seed = p_particles->random_seed;
  399. frame_params.explosiveness = p_particles->explosiveness;
  400. frame_params.randomness = p_particles->randomness;
  401. if (p_particles->use_local_coords) {
  402. GLES3::MaterialStorage::store_transform(Transform3D(), frame_params.emission_transform);
  403. } else {
  404. GLES3::MaterialStorage::store_transform(p_particles->emission_transform, frame_params.emission_transform);
  405. }
  406. frame_params.cycle = p_particles->cycle_number;
  407. frame_params.frame = p_particles->frame_counter++;
  408. frame_params.pad0 = 0;
  409. frame_params.pad1 = 0;
  410. frame_params.pad2 = 0;
  411. { //collision and attractors
  412. frame_params.collider_count = 0;
  413. frame_params.attractor_count = 0;
  414. frame_params.particle_size = p_particles->collision_base_size;
  415. GLuint collision_heightmap_texture = 0;
  416. Transform3D to_particles;
  417. if (p_particles->use_local_coords) {
  418. to_particles = p_particles->emission_transform.affine_inverse();
  419. }
  420. if (p_particles->has_sdf_collision && p_particles->sdf_collision_texture != 0) {
  421. //2D collision
  422. Transform2D xform = p_particles->sdf_collision_transform; //will use dotproduct manually so invert beforehand
  423. if (!p_particles->use_local_coords) {
  424. Transform2D emission;
  425. emission.columns[0] = Vector2(p_particles->emission_transform.basis.get_column(0).x, p_particles->emission_transform.basis.get_column(0).y);
  426. emission.columns[1] = Vector2(p_particles->emission_transform.basis.get_column(1).x, p_particles->emission_transform.basis.get_column(1).y);
  427. emission.set_origin(Vector2(p_particles->emission_transform.origin.x, p_particles->emission_transform.origin.y));
  428. xform = xform * emission.affine_inverse();
  429. }
  430. Transform2D revert = xform.affine_inverse();
  431. frame_params.collider_count = 1;
  432. frame_params.colliders[0].transform[0] = xform.columns[0][0];
  433. frame_params.colliders[0].transform[1] = xform.columns[0][1];
  434. frame_params.colliders[0].transform[2] = 0;
  435. frame_params.colliders[0].transform[3] = xform.columns[2][0];
  436. frame_params.colliders[0].transform[4] = xform.columns[1][0];
  437. frame_params.colliders[0].transform[5] = xform.columns[1][1];
  438. frame_params.colliders[0].transform[6] = 0;
  439. frame_params.colliders[0].transform[7] = xform.columns[2][1];
  440. frame_params.colliders[0].transform[8] = revert.columns[0][0];
  441. frame_params.colliders[0].transform[9] = revert.columns[0][1];
  442. frame_params.colliders[0].transform[10] = 0;
  443. frame_params.colliders[0].transform[11] = revert.columns[2][0];
  444. frame_params.colliders[0].transform[12] = revert.columns[1][0];
  445. frame_params.colliders[0].transform[13] = revert.columns[1][1];
  446. frame_params.colliders[0].transform[14] = 0;
  447. frame_params.colliders[0].transform[15] = revert.columns[2][1];
  448. frame_params.colliders[0].extents[0] = p_particles->sdf_collision_to_screen.size.x;
  449. frame_params.colliders[0].extents[1] = p_particles->sdf_collision_to_screen.size.y;
  450. frame_params.colliders[0].extents[2] = p_particles->sdf_collision_to_screen.position.x;
  451. frame_params.colliders[0].scale = p_particles->sdf_collision_to_screen.position.y;
  452. frame_params.colliders[0].type = ParticlesFrameParams::COLLISION_TYPE_2D_SDF;
  453. collision_heightmap_texture = p_particles->sdf_collision_texture;
  454. }
  455. for (const RID &E : p_particles->collisions) {
  456. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(E);
  457. if (!pci || !pci->active) {
  458. continue;
  459. }
  460. ParticlesCollision *pc = particles_collision_owner.get_or_null(pci->collision);
  461. ERR_CONTINUE(!pc);
  462. Transform3D to_collider = pci->transform;
  463. if (p_particles->use_local_coords) {
  464. to_collider = to_particles * to_collider;
  465. }
  466. Vector3 scale = to_collider.basis.get_scale();
  467. to_collider.basis.orthonormalize();
  468. if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  469. //attractor
  470. if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) {
  471. continue;
  472. }
  473. ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count];
  474. GLES3::MaterialStorage::store_transform(to_collider, attr.transform);
  475. attr.strength = pc->attractor_strength;
  476. attr.attenuation = pc->attractor_attenuation;
  477. attr.directionality = pc->attractor_directionality;
  478. switch (pc->type) {
  479. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: {
  480. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE;
  481. float radius = pc->radius;
  482. radius *= (scale.x + scale.y + scale.z) / 3.0;
  483. attr.extents[0] = radius;
  484. attr.extents[1] = radius;
  485. attr.extents[2] = radius;
  486. } break;
  487. case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: {
  488. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX;
  489. Vector3 extents = pc->extents * scale;
  490. attr.extents[0] = extents.x;
  491. attr.extents[1] = extents.y;
  492. attr.extents[2] = extents.z;
  493. } break;
  494. case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: {
  495. WARN_PRINT_ONCE_ED("Vector field particle attractors are not available in the GL Compatibility rendering backend.");
  496. } break;
  497. default: {
  498. }
  499. }
  500. frame_params.attractor_count++;
  501. } else {
  502. //collider
  503. if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) {
  504. continue;
  505. }
  506. ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count];
  507. GLES3::MaterialStorage::store_transform(to_collider, col.transform);
  508. switch (pc->type) {
  509. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  510. col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE;
  511. float radius = pc->radius;
  512. radius *= (scale.x + scale.y + scale.z) / 3.0;
  513. col.extents[0] = radius;
  514. col.extents[1] = radius;
  515. col.extents[2] = radius;
  516. } break;
  517. case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: {
  518. col.type = ParticlesFrameParams::COLLISION_TYPE_BOX;
  519. Vector3 extents = pc->extents * scale;
  520. col.extents[0] = extents.x;
  521. col.extents[1] = extents.y;
  522. col.extents[2] = extents.z;
  523. } break;
  524. case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: {
  525. WARN_PRINT_ONCE_ED("SDF Particle Colliders are not available in the GL Compatibility rendering backend.");
  526. } break;
  527. case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: {
  528. if (collision_heightmap_texture != 0) { //already taken
  529. continue;
  530. }
  531. col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD;
  532. Vector3 extents = pc->extents * scale;
  533. col.extents[0] = extents.x;
  534. col.extents[1] = extents.y;
  535. col.extents[2] = extents.z;
  536. collision_heightmap_texture = pc->heightfield_texture;
  537. } break;
  538. default: {
  539. }
  540. }
  541. frame_params.collider_count++;
  542. }
  543. }
  544. // Bind heightmap or SDF texture.
  545. GLuint heightmap = collision_heightmap_texture;
  546. if (heightmap == 0) {
  547. GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_BLACK));
  548. heightmap = tex->tex_id;
  549. }
  550. glActiveTexture(GL_TEXTURE0);
  551. glBindTexture(GL_TEXTURE_2D, heightmap);
  552. }
  553. if (p_particles->frame_params_ubo == 0) {
  554. glGenBuffers(1, &p_particles->frame_params_ubo);
  555. glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_FRAME_UNIFORM_LOCATION, p_particles->frame_params_ubo);
  556. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, p_particles->frame_params_ubo, sizeof(ParticlesFrameParams), &frame_params, GL_STREAM_DRAW, "Particle Frame UBO");
  557. } else {
  558. // Update per-frame UBO.
  559. glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_FRAME_UNIFORM_LOCATION, p_particles->frame_params_ubo);
  560. glBufferData(GL_UNIFORM_BUFFER, sizeof(ParticlesFrameParams), &frame_params, GL_STREAM_DRAW);
  561. }
  562. // Get shader and set shader uniforms;
  563. ParticleProcessMaterialData *m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(p_particles->process_material, RS::SHADER_PARTICLES));
  564. if (!m) {
  565. m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(particles_shader.default_material, RS::SHADER_PARTICLES));
  566. }
  567. ERR_FAIL_COND(!m);
  568. ParticlesShaderGLES3::ShaderVariant variant = ParticlesShaderGLES3::MODE_DEFAULT;
  569. uint32_t specialization = 0;
  570. for (uint32_t i = 0; i < p_particles->userdata_count; i++) {
  571. specialization |= (1 << i);
  572. }
  573. if (p_particles->mode == RS::ParticlesMode::PARTICLES_MODE_3D) {
  574. specialization |= ParticlesShaderGLES3::MODE_3D;
  575. }
  576. RID version = particles_shader.default_shader_version;
  577. if (m->shader_data->version.is_valid() && m->shader_data->valid) {
  578. // Bind material uniform buffer and textures.
  579. m->bind_uniforms();
  580. version = m->shader_data->version;
  581. }
  582. bool success = material_storage->shaders.particles_process_shader.version_bind_shader(version, variant, specialization);
  583. if (!success) {
  584. return;
  585. }
  586. material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::LIFETIME, p_particles->lifetime, version, variant, specialization);
  587. material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::CLEAR, p_particles->clear, version, variant, specialization);
  588. material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::TOTAL_PARTICLES, uint32_t(p_particles->amount), version, variant, specialization);
  589. material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::USE_FRACTIONAL_DELTA, p_particles->fractional_delta, version, variant, specialization);
  590. p_particles->clear = false;
  591. p_particles->has_collision_cache = m->shader_data->uses_collision;
  592. glBindVertexArray(p_particles->back_vertex_array);
  593. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, p_particles->front_process_buffer);
  594. glBeginTransformFeedback(GL_POINTS);
  595. glDrawArrays(GL_POINTS, 0, p_particles->amount);
  596. glEndTransformFeedback();
  597. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
  598. glBindVertexArray(0);
  599. SWAP(p_particles->front_process_buffer, p_particles->back_process_buffer);
  600. SWAP(p_particles->front_vertex_array, p_particles->back_vertex_array);
  601. }
  602. void ParticlesStorage::particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) {
  603. Particles *particles = particles_owner.get_or_null(p_particles);
  604. ERR_FAIL_COND(!particles);
  605. 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) {
  606. return;
  607. }
  608. if (particles->front_process_buffer == 0) {
  609. return; //particles have not processed yet
  610. }
  611. Vector3 axis = -p_axis; // cameras look to z negative
  612. if (particles->use_local_coords) {
  613. axis = particles->emission_transform.basis.xform_inv(axis).normalized();
  614. }
  615. // Sort will be done on CPU since we don't have compute shaders.
  616. // If the sort_buffer has valid data
  617. // Use a buffer that is 2 frames out of date to avoid stalls.
  618. if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->sort_buffer_filled) {
  619. glBindBuffer(GL_ARRAY_BUFFER, particles->sort_buffer);
  620. ParticleInstanceData3D *particle_array;
  621. #ifndef __EMSCRIPTEN__
  622. particle_array = static_cast<ParticleInstanceData3D *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
  623. ERR_FAIL_NULL(particle_array);
  624. #else
  625. LocalVector<ParticleInstanceData3D> particle_vector;
  626. particle_vector.resize(particles->amount);
  627. particle_array = particle_vector.ptr();
  628. glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), particle_array);
  629. #endif
  630. SortArray<ParticleInstanceData3D, ParticlesViewSort> sorter;
  631. sorter.compare.z_dir = axis;
  632. sorter.sort(particle_array, particles->amount);
  633. #ifndef __EMSCRIPTEN__
  634. glUnmapBuffer(GL_ARRAY_BUFFER);
  635. #else
  636. glBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), particle_vector.ptr());
  637. #endif
  638. }
  639. glEnable(GL_RASTERIZER_DISCARD);
  640. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  641. _particles_update_instance_buffer(particles, axis, p_up_axis);
  642. glDisable(GL_RASTERIZER_DISCARD);
  643. }
  644. void ParticlesStorage::_particles_update_buffers(Particles *particles) {
  645. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  646. uint32_t userdata_count = 0;
  647. if (particles->process_material.is_valid()) {
  648. GLES3::ParticleProcessMaterialData *material_data = static_cast<GLES3::ParticleProcessMaterialData *>(material_storage->material_get_data(particles->process_material, RS::SHADER_PARTICLES));
  649. if (material_data && material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
  650. userdata_count = material_data->shader_data->userdata_count;
  651. }
  652. }
  653. if (userdata_count != particles->userdata_count) {
  654. // Mismatch userdata, re-create buffers.
  655. _particles_free_data(particles);
  656. }
  657. if (particles->amount > 0 && particles->front_process_buffer == 0) {
  658. int total_amount = particles->amount;
  659. particles->userdata_count = userdata_count;
  660. uint32_t xform_size = particles->mode == RS::PARTICLES_MODE_2D ? 2 : 3;
  661. particles->instance_buffer_stride_cache = sizeof(float) * 4 * (xform_size + 1);
  662. particles->instance_buffer_size_cache = particles->instance_buffer_stride_cache * total_amount;
  663. particles->num_attrib_arrays_cache = 5 + userdata_count + (xform_size - 2);
  664. particles->process_buffer_stride_cache = sizeof(float) * 4 * particles->num_attrib_arrays_cache;
  665. PackedByteArray data;
  666. data.resize_zeroed(particles->process_buffer_stride_cache * total_amount);
  667. PackedByteArray instance_data;
  668. instance_data.resize_zeroed(particles->instance_buffer_size_cache);
  669. {
  670. glGenVertexArrays(1, &particles->front_vertex_array);
  671. glBindVertexArray(particles->front_vertex_array);
  672. glGenBuffers(1, &particles->front_process_buffer);
  673. glGenBuffers(1, &particles->front_instance_buffer);
  674. glBindBuffer(GL_ARRAY_BUFFER, particles->front_process_buffer);
  675. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->front_process_buffer, particles->process_buffer_stride_cache * total_amount, data.ptr(), GL_DYNAMIC_COPY, "Particles front process buffer");
  676. for (uint32_t j = 0; j < particles->num_attrib_arrays_cache; j++) {
  677. glEnableVertexAttribArray(j);
  678. glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, particles->process_buffer_stride_cache, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * j));
  679. }
  680. glBindVertexArray(0);
  681. glBindBuffer(GL_ARRAY_BUFFER, particles->front_instance_buffer);
  682. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->front_instance_buffer, particles->instance_buffer_size_cache, instance_data.ptr(), GL_DYNAMIC_COPY, "Particles front instance buffer");
  683. }
  684. {
  685. glGenVertexArrays(1, &particles->back_vertex_array);
  686. glBindVertexArray(particles->back_vertex_array);
  687. glGenBuffers(1, &particles->back_process_buffer);
  688. glGenBuffers(1, &particles->back_instance_buffer);
  689. glBindBuffer(GL_ARRAY_BUFFER, particles->back_process_buffer);
  690. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->back_process_buffer, particles->process_buffer_stride_cache * total_amount, data.ptr(), GL_DYNAMIC_COPY, "Particles back process buffer");
  691. for (uint32_t j = 0; j < particles->num_attrib_arrays_cache; j++) {
  692. glEnableVertexAttribArray(j);
  693. glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, particles->process_buffer_stride_cache, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * j));
  694. }
  695. glBindVertexArray(0);
  696. glBindBuffer(GL_ARRAY_BUFFER, particles->back_instance_buffer);
  697. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->back_instance_buffer, particles->instance_buffer_size_cache, instance_data.ptr(), GL_DYNAMIC_COPY, "Particles back instance buffer");
  698. }
  699. glBindBuffer(GL_ARRAY_BUFFER, 0);
  700. }
  701. }
  702. void ParticlesStorage::_particles_allocate_history_buffers(Particles *particles) {
  703. if (particles->sort_buffer == 0) {
  704. glGenBuffers(1, &particles->last_frame_buffer);
  705. glBindBuffer(GL_ARRAY_BUFFER, particles->last_frame_buffer);
  706. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->last_frame_buffer, particles->instance_buffer_size_cache, nullptr, GL_DYNAMIC_READ, "Particles last frame buffer");
  707. glGenBuffers(1, &particles->sort_buffer);
  708. glBindBuffer(GL_ARRAY_BUFFER, particles->sort_buffer);
  709. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->sort_buffer, particles->instance_buffer_size_cache, nullptr, GL_DYNAMIC_READ, "Particles sort buffer");
  710. particles->sort_buffer_filled = false;
  711. particles->last_frame_buffer_filled = false;
  712. glBindBuffer(GL_ARRAY_BUFFER, 0);
  713. }
  714. }
  715. void ParticlesStorage::_particles_update_instance_buffer(Particles *particles, const Vector3 &p_axis, const Vector3 &p_up_axis) {
  716. ParticlesCopyShaderGLES3::ShaderVariant variant = ParticlesCopyShaderGLES3::MODE_DEFAULT;
  717. uint64_t specialization = 0;
  718. if (particles->mode == RS::ParticlesMode::PARTICLES_MODE_3D) {
  719. specialization |= ParticlesCopyShaderGLES3::MODE_3D;
  720. }
  721. bool success = particles_shader.copy_shader.version_bind_shader(particles_shader.copy_shader_version, variant, specialization);
  722. if (!success) {
  723. return;
  724. }
  725. // Affect 2D only.
  726. if (particles->use_local_coords) {
  727. // In local mode, particle positions are calculated locally (relative to the node position)
  728. // and they're also drawn locally.
  729. // It works as expected, so we just pass an identity transform.
  730. particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::INV_EMISSION_TRANSFORM, Transform3D(), particles_shader.copy_shader_version, variant, specialization);
  731. } else {
  732. // In global mode, particle positions are calculated globally (relative to the canvas origin)
  733. // but they're drawn locally.
  734. // So, we need to pass the inverse of the emission transform to bring the
  735. // particles to local coordinates before drawing.
  736. Transform3D inv = particles->emission_transform.affine_inverse();
  737. particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::INV_EMISSION_TRANSFORM, inv, particles_shader.copy_shader_version, variant, specialization);
  738. }
  739. particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::FRAME_REMAINDER, particles->interpolate ? particles->frame_remainder : 0.0, particles_shader.copy_shader_version, variant, specialization);
  740. particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::ALIGN_MODE, uint32_t(particles->transform_align), particles_shader.copy_shader_version, variant, specialization);
  741. particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::ALIGN_UP, p_up_axis, particles_shader.copy_shader_version, variant, specialization);
  742. particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::SORT_DIRECTION, p_axis, particles_shader.copy_shader_version, variant, specialization);
  743. glBindVertexArray(particles->back_vertex_array);
  744. glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, particles->front_instance_buffer, 0, particles->instance_buffer_size_cache);
  745. glBeginTransformFeedback(GL_POINTS);
  746. if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME) {
  747. uint32_t lifetime_split = (MIN(int(particles->amount * particles->phase), particles->amount - 1) + 1) % particles->amount;
  748. uint32_t stride = particles->process_buffer_stride_cache;
  749. glBindBuffer(GL_ARRAY_BUFFER, particles->back_process_buffer);
  750. // Offset VBO so you render starting at the newest particle.
  751. if (particles->amount - lifetime_split > 0) {
  752. glEnableVertexAttribArray(0); // Color.
  753. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 0));
  754. glEnableVertexAttribArray(1); // .xyz: velocity. .z: flags.
  755. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 1));
  756. glEnableVertexAttribArray(2); // Custom.
  757. glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 2));
  758. glEnableVertexAttribArray(3); // Xform1.
  759. glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 3));
  760. glEnableVertexAttribArray(4); // Xform2.
  761. glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 4));
  762. if (particles->mode == RS::PARTICLES_MODE_3D) {
  763. glEnableVertexAttribArray(5); // Xform3.
  764. glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 5));
  765. }
  766. uint32_t to_draw = particles->amount - lifetime_split;
  767. glDrawArrays(GL_POINTS, 0, to_draw);
  768. }
  769. // Then render from index 0 up intil the newest particle.
  770. if (lifetime_split > 0) {
  771. glEndTransformFeedback();
  772. // Now output to the second portion of the instance buffer.
  773. glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, particles->front_instance_buffer, particles->instance_buffer_stride_cache * (particles->amount - lifetime_split), particles->instance_buffer_stride_cache * (lifetime_split));
  774. glBeginTransformFeedback(GL_POINTS);
  775. // Reset back to normal.
  776. for (uint32_t j = 0; j < particles->num_attrib_arrays_cache; j++) {
  777. glEnableVertexAttribArray(j);
  778. glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * j));
  779. }
  780. glDrawArrays(GL_POINTS, 0, lifetime_split);
  781. }
  782. } else {
  783. glDrawArrays(GL_POINTS, 0, particles->amount);
  784. }
  785. glEndTransformFeedback();
  786. glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0, 0, 0);
  787. glBindVertexArray(0);
  788. glBindBuffer(GL_ARRAY_BUFFER, 0);
  789. }
  790. void ParticlesStorage::update_particles() {
  791. glEnable(GL_RASTERIZER_DISCARD);
  792. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  793. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  794. glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_GLOBALS_UNIFORM_LOCATION, global_buffer);
  795. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  796. while (particle_update_list.first()) {
  797. // Use transform feedback to process particles.
  798. Particles *particles = particle_update_list.first()->self();
  799. particles->update_list.remove_from_list();
  800. particles->dirty = false;
  801. _particles_update_buffers(particles);
  802. if (particles->restart_request) {
  803. particles->prev_ticks = 0;
  804. particles->phase = 0;
  805. particles->prev_phase = 0;
  806. particles->clear = true;
  807. particles->restart_request = false;
  808. }
  809. if (particles->inactive && !particles->emitting) {
  810. //go next
  811. continue;
  812. }
  813. if (particles->emitting) {
  814. if (particles->inactive) {
  815. //restart system from scratch
  816. particles->prev_ticks = 0;
  817. particles->phase = 0;
  818. particles->prev_phase = 0;
  819. particles->clear = true;
  820. }
  821. particles->inactive = false;
  822. particles->inactive_time = 0;
  823. } else {
  824. particles->inactive_time += particles->speed_scale * RSG::rasterizer->get_frame_delta_time();
  825. if (particles->inactive_time > particles->lifetime * 1.2) {
  826. particles->inactive = true;
  827. continue;
  828. }
  829. }
  830. // Copy the instance buffer that was last used into the last_frame buffer.
  831. // sort_buffer should now be 2 frames out of date.
  832. if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME) {
  833. _particles_allocate_history_buffers(particles);
  834. SWAP(particles->last_frame_buffer, particles->sort_buffer);
  835. glBindBuffer(GL_COPY_READ_BUFFER, particles->back_instance_buffer);
  836. glBindBuffer(GL_COPY_WRITE_BUFFER, particles->last_frame_buffer);
  837. glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, particles->instance_buffer_size_cache);
  838. // Last frame's last_frame turned into this frame's sort buffer.
  839. particles->sort_buffer_filled = particles->last_frame_buffer_filled;
  840. particles->sort_buffer_phase = particles->last_frame_phase;
  841. particles->last_frame_buffer_filled = true;
  842. particles->last_frame_phase = particles->phase;
  843. glBindBuffer(GL_COPY_READ_BUFFER, 0);
  844. glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
  845. }
  846. int fixed_fps = 0;
  847. if (particles->fixed_fps > 0) {
  848. fixed_fps = particles->fixed_fps;
  849. }
  850. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  851. if (particles->clear && particles->pre_process_time > 0.0) {
  852. double frame_time;
  853. if (fixed_fps > 0) {
  854. frame_time = 1.0 / fixed_fps;
  855. } else {
  856. frame_time = 1.0 / 30.0;
  857. }
  858. double todo = particles->pre_process_time;
  859. while (todo >= 0) {
  860. _particles_process(particles, frame_time);
  861. todo -= frame_time;
  862. }
  863. }
  864. if (fixed_fps > 0) {
  865. double frame_time;
  866. double decr;
  867. if (zero_time_scale) {
  868. frame_time = 0.0;
  869. decr = 1.0 / fixed_fps;
  870. } else {
  871. frame_time = 1.0 / fixed_fps;
  872. decr = frame_time;
  873. }
  874. double delta = RSG::rasterizer->get_frame_delta_time();
  875. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  876. delta = 0.1;
  877. } else if (delta <= 0.0) { //unlikely but..
  878. delta = 0.001;
  879. }
  880. double todo = particles->frame_remainder + delta;
  881. while (todo >= frame_time) {
  882. _particles_process(particles, frame_time);
  883. todo -= decr;
  884. }
  885. particles->frame_remainder = todo;
  886. } else {
  887. if (zero_time_scale) {
  888. _particles_process(particles, 0.0);
  889. } else {
  890. _particles_process(particles, RSG::rasterizer->get_frame_delta_time());
  891. }
  892. }
  893. // Copy particles to instance buffer and pack Color/Custom.
  894. // We don't have camera information here, so don't copy here if we need camera information for view depth or align mode.
  895. 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) {
  896. _particles_update_instance_buffer(particles, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0));
  897. if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME && particles->sort_buffer_filled) {
  898. if (particles->mode == RS::ParticlesMode::PARTICLES_MODE_2D) {
  899. _particles_reverse_lifetime_sort<ParticleInstanceData2D>(particles);
  900. } else {
  901. _particles_reverse_lifetime_sort<ParticleInstanceData3D>(particles);
  902. }
  903. }
  904. }
  905. SWAP(particles->front_instance_buffer, particles->back_instance_buffer);
  906. // At the end of update, the back_buffer contains the most up-to-date-information to read from.
  907. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  908. }
  909. glDisable(GL_RASTERIZER_DISCARD);
  910. }
  911. template <typename ParticleInstanceData>
  912. void ParticlesStorage::_particles_reverse_lifetime_sort(Particles *particles) {
  913. glBindBuffer(GL_ARRAY_BUFFER, particles->sort_buffer);
  914. ParticleInstanceData *particle_array;
  915. uint32_t buffer_size = particles->amount * sizeof(ParticleInstanceData);
  916. #ifndef __EMSCRIPTEN__
  917. particle_array = static_cast<ParticleInstanceData *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, buffer_size, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
  918. ERR_FAIL_NULL(particle_array);
  919. #else
  920. LocalVector<ParticleInstanceData> particle_vector;
  921. particle_vector.resize(particles->amount);
  922. particle_array = particle_vector.ptr();
  923. glGetBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, particle_array);
  924. #endif
  925. uint32_t lifetime_split = (MIN(int(particles->amount * particles->sort_buffer_phase), particles->amount - 1) + 1) % particles->amount;
  926. for (uint32_t i = 0; i < lifetime_split / 2; i++) {
  927. SWAP(particle_array[i], particle_array[lifetime_split - i - 1]);
  928. }
  929. for (uint32_t i = 0; i < (particles->amount - lifetime_split) / 2; i++) {
  930. SWAP(particle_array[lifetime_split + i], particle_array[particles->amount - 1 - i]);
  931. }
  932. #ifndef __EMSCRIPTEN__
  933. glUnmapBuffer(GL_ARRAY_BUFFER);
  934. #else
  935. glBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, particle_vector.ptr());
  936. #endif
  937. glBindBuffer(GL_ARRAY_BUFFER, 0);
  938. }
  939. Dependency *ParticlesStorage::particles_get_dependency(RID p_particles) const {
  940. Particles *particles = particles_owner.get_or_null(p_particles);
  941. ERR_FAIL_NULL_V(particles, nullptr);
  942. return &particles->dependency;
  943. }
  944. bool ParticlesStorage::particles_is_inactive(RID p_particles) const {
  945. ERR_FAIL_COND_V_MSG(RSG::threaded, false, "This function should never be used with threaded rendering, as it stalls the renderer.");
  946. const Particles *particles = particles_owner.get_or_null(p_particles);
  947. ERR_FAIL_COND_V(!particles, false);
  948. return !particles->emitting && particles->inactive;
  949. }
  950. /* PARTICLES COLLISION API */
  951. RID ParticlesStorage::particles_collision_allocate() {
  952. return particles_collision_owner.allocate_rid();
  953. }
  954. void ParticlesStorage::particles_collision_initialize(RID p_rid) {
  955. particles_collision_owner.initialize_rid(p_rid, ParticlesCollision());
  956. }
  957. void ParticlesStorage::particles_collision_free(RID p_rid) {
  958. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_rid);
  959. if (particles_collision->heightfield_texture != 0) {
  960. GLES3::Utilities::get_singleton()->texture_free_data(particles_collision->heightfield_texture);
  961. particles_collision->heightfield_texture = 0;
  962. glDeleteFramebuffers(1, &particles_collision->heightfield_fb);
  963. particles_collision->heightfield_fb = 0;
  964. }
  965. particles_collision->dependency.deleted_notify(p_rid);
  966. particles_collision_owner.free(p_rid);
  967. }
  968. GLuint ParticlesStorage::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const {
  969. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  970. ERR_FAIL_COND_V(!particles_collision, 0);
  971. ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, 0);
  972. if (particles_collision->heightfield_texture == 0) {
  973. //create
  974. const int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 };
  975. Size2i size;
  976. if (particles_collision->extents.x > particles_collision->extents.z) {
  977. size.x = resolutions[particles_collision->heightfield_resolution];
  978. size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x);
  979. } else {
  980. size.y = resolutions[particles_collision->heightfield_resolution];
  981. size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y);
  982. }
  983. glGenTextures(1, &particles_collision->heightfield_texture);
  984. glActiveTexture(GL_TEXTURE0);
  985. glBindTexture(GL_TEXTURE_2D, particles_collision->heightfield_texture);
  986. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
  987. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  988. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  989. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  990. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  991. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  992. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  993. glGenFramebuffers(1, &particles_collision->heightfield_fb);
  994. glBindFramebuffer(GL_FRAMEBUFFER, particles_collision->heightfield_fb);
  995. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, particles_collision->heightfield_texture, 0);
  996. #ifdef DEBUG_ENABLED
  997. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  998. if (status != GL_FRAMEBUFFER_COMPLETE) {
  999. WARN_PRINT("Could create heightmap texture status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  1000. }
  1001. #endif
  1002. GLES3::Utilities::get_singleton()->texture_allocated_data(particles_collision->heightfield_texture, size.x * size.y * 4, "Particles collision heightfield texture");
  1003. particles_collision->heightfield_fb_size = size;
  1004. glBindTexture(GL_TEXTURE_2D, 0);
  1005. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1006. }
  1007. return particles_collision->heightfield_fb;
  1008. }
  1009. void ParticlesStorage::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) {
  1010. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1011. ERR_FAIL_COND(!particles_collision);
  1012. if (p_type == particles_collision->type) {
  1013. return;
  1014. }
  1015. if (particles_collision->heightfield_texture != 0) {
  1016. GLES3::Utilities::get_singleton()->texture_free_data(particles_collision->heightfield_texture);
  1017. particles_collision->heightfield_texture = 0;
  1018. glDeleteFramebuffers(1, &particles_collision->heightfield_fb);
  1019. particles_collision->heightfield_fb = 0;
  1020. }
  1021. particles_collision->type = p_type;
  1022. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1023. }
  1024. void ParticlesStorage::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) {
  1025. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1026. ERR_FAIL_COND(!particles_collision);
  1027. particles_collision->cull_mask = p_cull_mask;
  1028. }
  1029. void ParticlesStorage::particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) {
  1030. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1031. ERR_FAIL_COND(!particles_collision);
  1032. particles_collision->radius = p_radius;
  1033. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1034. }
  1035. void ParticlesStorage::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) {
  1036. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1037. ERR_FAIL_COND(!particles_collision);
  1038. particles_collision->extents = p_extents;
  1039. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1040. }
  1041. void ParticlesStorage::particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) {
  1042. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1043. ERR_FAIL_COND(!particles_collision);
  1044. particles_collision->attractor_strength = p_strength;
  1045. }
  1046. void ParticlesStorage::particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) {
  1047. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1048. ERR_FAIL_COND(!particles_collision);
  1049. particles_collision->attractor_directionality = p_directionality;
  1050. }
  1051. void ParticlesStorage::particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) {
  1052. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1053. ERR_FAIL_COND(!particles_collision);
  1054. particles_collision->attractor_attenuation = p_curve;
  1055. }
  1056. void ParticlesStorage::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) {
  1057. WARN_PRINT_ONCE_ED("The GL Compatibility rendering backend does not support SDF collisions in 3D particle shaders");
  1058. }
  1059. void ParticlesStorage::particles_collision_height_field_update(RID p_particles_collision) {
  1060. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1061. ERR_FAIL_COND(!particles_collision);
  1062. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1063. }
  1064. void ParticlesStorage::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) {
  1065. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1066. ERR_FAIL_COND(!particles_collision);
  1067. ERR_FAIL_INDEX(p_resolution, RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX);
  1068. if (particles_collision->heightfield_resolution == p_resolution) {
  1069. return;
  1070. }
  1071. particles_collision->heightfield_resolution = p_resolution;
  1072. if (particles_collision->heightfield_texture != 0) {
  1073. GLES3::Utilities::get_singleton()->texture_free_data(particles_collision->heightfield_texture);
  1074. particles_collision->heightfield_texture = 0;
  1075. glDeleteFramebuffers(1, &particles_collision->heightfield_fb);
  1076. particles_collision->heightfield_fb = 0;
  1077. }
  1078. }
  1079. AABB ParticlesStorage::particles_collision_get_aabb(RID p_particles_collision) const {
  1080. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1081. ERR_FAIL_COND_V(!particles_collision, AABB());
  1082. switch (particles_collision->type) {
  1083. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT:
  1084. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  1085. AABB aabb;
  1086. aabb.position = -Vector3(1, 1, 1) * particles_collision->radius;
  1087. aabb.size = Vector3(2, 2, 2) * particles_collision->radius;
  1088. return aabb;
  1089. }
  1090. default: {
  1091. AABB aabb;
  1092. aabb.position = -particles_collision->extents;
  1093. aabb.size = particles_collision->extents * 2;
  1094. return aabb;
  1095. }
  1096. }
  1097. }
  1098. Vector3 ParticlesStorage::particles_collision_get_extents(RID p_particles_collision) const {
  1099. const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1100. ERR_FAIL_COND_V(!particles_collision, Vector3());
  1101. return particles_collision->extents;
  1102. }
  1103. bool ParticlesStorage::particles_collision_is_heightfield(RID p_particles_collision) const {
  1104. const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1105. ERR_FAIL_COND_V(!particles_collision, false);
  1106. return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE;
  1107. }
  1108. Dependency *ParticlesStorage::particles_collision_get_dependency(RID p_particles_collision) const {
  1109. ParticlesCollision *pc = particles_collision_owner.get_or_null(p_particles_collision);
  1110. ERR_FAIL_NULL_V(pc, nullptr);
  1111. return &pc->dependency;
  1112. }
  1113. /* Particles collision instance */
  1114. RID ParticlesStorage::particles_collision_instance_create(RID p_collision) {
  1115. ParticlesCollisionInstance pci;
  1116. pci.collision = p_collision;
  1117. return particles_collision_instance_owner.make_rid(pci);
  1118. }
  1119. void ParticlesStorage::particles_collision_instance_free(RID p_rid) {
  1120. particles_collision_instance_owner.free(p_rid);
  1121. }
  1122. void ParticlesStorage::particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) {
  1123. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance);
  1124. ERR_FAIL_COND(!pci);
  1125. pci->transform = p_transform;
  1126. }
  1127. void ParticlesStorage::particles_collision_instance_set_active(RID p_collision_instance, bool p_active) {
  1128. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance);
  1129. ERR_FAIL_COND(!pci);
  1130. pci->active = p_active;
  1131. }
  1132. #endif // GLES3_ENABLED