| 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 | |
| 31 | #include "particles_storage.h" |
| 32 | |
| 33 | #include "servers/rendering/renderer_rd/renderer_compositor_rd.h" |
| 34 | #include "servers/rendering/rendering_server_globals.h" |
| 35 | #include "texture_storage.h" |
| 36 | |
| 37 | using namespace RendererRD; |
| 38 | |
| 39 | ParticlesStorage *ParticlesStorage::singleton = nullptr; |
| 40 | |
| 41 | ParticlesStorage *ParticlesStorage::get_singleton() { |
| 42 | return singleton; |
| 43 | } |
| 44 | |
| 45 | ParticlesStorage::ParticlesStorage() { |
| 46 | singleton = this; |
| 47 | |
| 48 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 49 | |
| 50 | /* Effects */ |
| 51 | |
| 52 | sort_effects = memnew(SortEffects); |
| 53 | |
| 54 | /* Particles */ |
| 55 | |
| 56 | { |
| 57 | String defines = "#define SAMPLERS_BINDING_FIRST_INDEX " + itos(SAMPLERS_BINDING_FIRST_INDEX) + "\n"; |
| 58 | // Initialize particles |
| 59 | Vector<String> particles_modes; |
| 60 | particles_modes.push_back(""); |
| 61 | particles_shader.shader.initialize(particles_modes, defines); |
| 62 | } |
| 63 | MaterialStorage::get_singleton()->shader_set_data_request_function(MaterialStorage::SHADER_TYPE_PARTICLES, _create_particles_shader_funcs); |
| 64 | MaterialStorage::get_singleton()->material_set_data_request_function(MaterialStorage::SHADER_TYPE_PARTICLES, _create_particles_material_funcs); |
| 65 | |
| 66 | { |
| 67 | ShaderCompiler::DefaultIdentifierActions actions; |
| 68 | |
| 69 | actions.renames["COLOR"] = "PARTICLE.color"; |
| 70 | actions.renames["VELOCITY"] = "PARTICLE.velocity"; |
| 71 | actions.renames["MASS"] = "mass"; |
| 72 | actions.renames["ACTIVE"] = "particle_active"; |
| 73 | actions.renames["RESTART"] = "restart"; |
| 74 | actions.renames["CUSTOM"] = "PARTICLE.custom"; |
| 75 | actions.renames["AMOUNT_RATIO"] = "FRAME.amount_ratio"; |
| 76 | for (int i = 0; i < ParticlesShader::MAX_USERDATAS; i++) { |
| 77 | String udname = "USERDATA" + itos(i + 1); |
| 78 | actions.renames[udname] = "PARTICLE.userdata" + itos(i + 1); |
| 79 | actions.usage_defines[udname] = "#define USERDATA" + itos(i + 1) + "_USED\n"; |
| 80 | } |
| 81 | actions.renames["TRANSFORM"] = "PARTICLE.xform"; |
| 82 | actions.renames["TIME"] = "frame_history.data[0].time"; |
| 83 | actions.renames["PI"] = String::num(Math::PI); |
| 84 | actions.renames["TAU"] = String::num(Math::TAU); |
| 85 | actions.renames["E"] = String::num(Math::E); |
| 86 | actions.renames["LIFETIME"] = "params.lifetime"; |
| 87 | actions.renames["DELTA"] = "local_delta"; |
| 88 | actions.renames["NUMBER"] = "particle_number"; |
| 89 | actions.renames["INDEX"] = "index"; |
| 90 | //actions.renames["GRAVITY"] = "current_gravity"; |
| 91 | actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform"; |
| 92 | actions.renames["EMITTER_VELOCITY"] = "FRAME.emitter_velocity"; |
| 93 | actions.renames["INTERPOLATE_TO_END"] = "FRAME.interp_to_end"; |
| 94 | actions.renames["RANDOM_SEED"] = "FRAME.random_seed"; |
| 95 | actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION"; |
| 96 | actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE"; |
| 97 | actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY"; |
| 98 | actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR"; |
| 99 | actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM"; |
| 100 | actions.renames["RESTART_POSITION"] = "restart_position"; |
| 101 | actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale"; |
| 102 | actions.renames["RESTART_VELOCITY"] = "restart_velocity"; |
| 103 | actions.renames["RESTART_COLOR"] = "restart_color"; |
| 104 | actions.renames["RESTART_CUSTOM"] = "restart_custom"; |
| 105 | actions.renames["emit_subparticle"] = "emit_subparticle"; |
| 106 | actions.renames["COLLIDED"] = "collided"; |
| 107 | actions.renames["COLLISION_NORMAL"] = "collision_normal"; |
| 108 | actions.renames["COLLISION_DEPTH"] = "collision_depth"; |
| 109 | actions.renames["ATTRACTOR_FORCE"] = "attractor_force"; |
| 110 | |
| 111 | actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n"; |
| 112 | actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n"; |
| 113 | actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n"; |
| 114 | actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISION_SCALE\n"; |
| 115 | |
| 116 | actions.base_texture_binding_index = 1; |
| 117 | actions.texture_layout_set = 3; |
| 118 | actions.base_uniform_string = "material."; |
| 119 | actions.base_varying_index = 10; |
| 120 | |
| 121 | actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP; |
| 122 | actions.default_repeat = ShaderLanguage::REPEAT_ENABLE; |
| 123 | actions.global_buffer_array_variable = "global_shader_uniforms.data"; |
| 124 | |
| 125 | particles_shader.compiler.initialize(actions); |
| 126 | } |
| 127 | |
| 128 | { |
| 129 | // default material and shader for particles shader |
| 130 | particles_shader.default_shader = material_storage->shader_allocate(); |
| 131 | material_storage->shader_initialize(particles_shader.default_shader); |
| 132 | material_storage->shader_set_code(particles_shader.default_shader, R"( |
| 133 | // Default particles shader. |
| 134 | |
| 135 | shader_type particles; |
| 136 | |
| 137 | void process() { |
| 138 | COLOR = vec4(1.0); |
| 139 | } |
| 140 | )"); |
| 141 | particles_shader.default_material = material_storage->material_allocate(); |
| 142 | material_storage->material_initialize(particles_shader.default_material); |
| 143 | material_storage->material_set_shader(particles_shader.default_material, particles_shader.default_shader); |
| 144 | |
| 145 | ParticleProcessMaterialData *md = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(particles_shader.default_material, MaterialStorage::SHADER_TYPE_PARTICLES)); |
| 146 | particles_shader.default_shader_rd = particles_shader.shader.version_get_shader(md->shader_data->version, 0); |
| 147 | |
| 148 | Vector<RD::Uniform> uniforms; |
| 149 | |
| 150 | { |
| 151 | RD::Uniform u; |
| 152 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 153 | u.binding = 2; |
| 154 | u.append_id(material_storage->global_shader_uniforms_get_storage_buffer()); |
| 155 | uniforms.push_back(u); |
| 156 | } |
| 157 | |
| 158 | material_storage->samplers_rd_get_default().append_uniforms(uniforms, SAMPLERS_BINDING_FIRST_INDEX); |
| 159 | |
| 160 | particles_shader.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, BASE_UNIFORM_SET); |
| 161 | } |
| 162 | |
| 163 | { |
| 164 | Vector<String> copy_modes; |
| 165 | for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) { |
| 166 | if (i == 0) { |
| 167 | copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n"); |
| 168 | copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n"); |
| 169 | copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n"); |
| 170 | } else { |
| 171 | copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USERDATA_COUNT " + itos(i) + "\n"); |
| 172 | copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n#define USERDATA_COUNT " + itos(i) + "\n"); |
| 173 | copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n#define USERDATA_COUNT " + itos(i) + "\n"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | particles_shader.copy_shader.initialize(copy_modes); |
| 178 | |
| 179 | particles_shader.copy_shader_version = particles_shader.copy_shader.version_create(); |
| 180 | |
| 181 | for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) { |
| 182 | for (int j = 0; j < ParticlesShader::COPY_MODE_MAX; j++) { |
| 183 | particles_shader.copy_pipelines[i][j].create_compute_pipeline(particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, i * ParticlesShader::COPY_MODE_MAX + j)); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | ParticlesStorage::~ParticlesStorage() { |
| 190 | for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) { |
| 191 | for (int j = 0; j < ParticlesShader::COPY_MODE_MAX; j++) { |
| 192 | particles_shader.copy_pipelines[i][j].free(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | particles_shader.copy_shader.version_free(particles_shader.copy_shader_version); |
| 197 | |
| 198 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 199 | material_storage->material_free(particles_shader.default_material); |
| 200 | material_storage->shader_free(particles_shader.default_shader); |
| 201 | |
| 202 | if (sort_effects) { |
| 203 | memdelete(sort_effects); |
| 204 | sort_effects = nullptr; |
| 205 | } |
| 206 | |
| 207 | singleton = nullptr; |
| 208 | } |
| 209 | |
| 210 | bool ParticlesStorage::free(RID p_rid) { |
| 211 | if (owns_particles(p_rid)) { |
| 212 | particles_free(p_rid); |
| 213 | return true; |
| 214 | } else if (owns_particles_collision(p_rid)) { |
| 215 | particles_collision_free(p_rid); |
| 216 | return true; |
| 217 | } else if (owns_particles_collision_instance(p_rid)) { |
| 218 | particles_collision_instance_free(p_rid); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | /* PARTICLES */ |
| 226 | |
| 227 | RID ParticlesStorage::particles_allocate() { |
| 228 | return particles_owner.allocate_rid(); |
| 229 | } |
| 230 | |
| 231 | void ParticlesStorage::particles_initialize(RID p_rid) { |
| 232 | particles_owner.initialize_rid(p_rid); |
| 233 | } |
| 234 | |
| 235 | void ParticlesStorage::particles_free(RID p_rid) { |
| 236 | Particles *particles = particles_owner.get_or_null(p_rid); |
| 237 | |
| 238 | particles->dependency.deleted_notify(p_rid); |
| 239 | particles->update_list.remove_from_list(); |
| 240 | |
| 241 | _particles_free_data(particles); |
| 242 | particles_owner.free(p_rid); |
| 243 | } |
| 244 | |
| 245 | void ParticlesStorage::particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) { |
| 246 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 247 | ERR_FAIL_NULL(particles); |
| 248 | if (particles->mode == p_mode) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | _particles_free_data(particles); |
| 253 | |
| 254 | particles->mode = p_mode; |
| 255 | } |
| 256 | |
| 257 | void ParticlesStorage::particles_set_emitting(RID p_particles, bool p_emitting) { |
| 258 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 259 | ERR_FAIL_NULL(particles); |
| 260 | |
| 261 | particles->emitting = p_emitting; |
| 262 | } |
| 263 | |
| 264 | bool ParticlesStorage::particles_get_emitting(RID p_particles) { |
| 265 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 266 | ERR_FAIL_NULL_V(particles, false); |
| 267 | |
| 268 | return particles->emitting; |
| 269 | } |
| 270 | |
| 271 | void ParticlesStorage::_particles_free_data(Particles *particles) { |
| 272 | if (particles->particle_buffer.is_valid()) { |
| 273 | RD::get_singleton()->free_rid(particles->particle_buffer); |
| 274 | particles->particle_buffer = RID(); |
| 275 | RD::get_singleton()->free_rid(particles->particle_instance_buffer); |
| 276 | particles->particle_instance_buffer = RID(); |
| 277 | } |
| 278 | |
| 279 | particles->userdata_count = 0; |
| 280 | |
| 281 | if (particles->frame_params_buffer.is_valid()) { |
| 282 | RD::get_singleton()->free_rid(particles->frame_params_buffer); |
| 283 | particles->frame_params_buffer = RID(); |
| 284 | } |
| 285 | particles->particles_transforms_buffer_uniform_set = RID(); |
| 286 | |
| 287 | if (RD::get_singleton()->uniform_set_is_valid(particles->trail_bind_pose_uniform_set)) { |
| 288 | RD::get_singleton()->free_rid(particles->trail_bind_pose_uniform_set); |
| 289 | } |
| 290 | particles->trail_bind_pose_uniform_set = RID(); |
| 291 | |
| 292 | if (particles->trail_bind_pose_buffer.is_valid()) { |
| 293 | RD::get_singleton()->free_rid(particles->trail_bind_pose_buffer); |
| 294 | particles->trail_bind_pose_buffer = RID(); |
| 295 | } |
| 296 | if (RD::get_singleton()->uniform_set_is_valid(particles->collision_textures_uniform_set)) { |
| 297 | RD::get_singleton()->free_rid(particles->collision_textures_uniform_set); |
| 298 | } |
| 299 | particles->collision_textures_uniform_set = RID(); |
| 300 | |
| 301 | if (particles->particles_sort_buffer.is_valid()) { |
| 302 | RD::get_singleton()->free_rid(particles->particles_sort_buffer); |
| 303 | particles->particles_sort_buffer = RID(); |
| 304 | particles->particles_sort_uniform_set = RID(); |
| 305 | } |
| 306 | |
| 307 | if (particles->emission_buffer != nullptr) { |
| 308 | particles->emission_buffer = nullptr; |
| 309 | particles->emission_buffer_data.clear(); |
| 310 | RD::get_singleton()->free_rid(particles->emission_storage_buffer); |
| 311 | particles->emission_storage_buffer = RID(); |
| 312 | } |
| 313 | |
| 314 | if (particles->unused_emission_storage_buffer.is_valid()) { |
| 315 | RD::get_singleton()->free_rid(particles->unused_emission_storage_buffer); |
| 316 | particles->unused_emission_storage_buffer = RID(); |
| 317 | } |
| 318 | |
| 319 | if (particles->unused_trail_storage_buffer.is_valid()) { |
| 320 | RD::get_singleton()->free_rid(particles->unused_trail_storage_buffer); |
| 321 | particles->unused_trail_storage_buffer = RID(); |
| 322 | } |
| 323 | |
| 324 | if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) { |
| 325 | //will need to be re-created |
| 326 | RD::get_singleton()->free_rid(particles->particles_material_uniform_set); |
| 327 | } |
| 328 | particles->particles_material_uniform_set = RID(); |
| 329 | } |
| 330 | |
| 331 | void ParticlesStorage::particles_set_amount(RID p_particles, int p_amount) { |
| 332 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 333 | ERR_FAIL_NULL(particles); |
| 334 | |
| 335 | if (particles->amount == p_amount) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | _particles_free_data(particles); |
| 340 | |
| 341 | particles->amount = p_amount; |
| 342 | |
| 343 | particles->prev_ticks = 0; |
| 344 | particles->phase = 0; |
| 345 | particles->prev_phase = 0; |
| 346 | particles->clear = true; |
| 347 | |
| 348 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 349 | } |
| 350 | |
| 351 | void ParticlesStorage::particles_set_amount_ratio(RID p_particles, float p_amount_ratio) { |
| 352 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 353 | ERR_FAIL_NULL(particles); |
| 354 | |
| 355 | particles->amount_ratio = p_amount_ratio; |
| 356 | } |
| 357 | |
| 358 | void ParticlesStorage::particles_set_lifetime(RID p_particles, double p_lifetime) { |
| 359 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 360 | ERR_FAIL_NULL(particles); |
| 361 | particles->lifetime = p_lifetime; |
| 362 | } |
| 363 | |
| 364 | void ParticlesStorage::particles_set_one_shot(RID p_particles, bool p_one_shot) { |
| 365 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 366 | ERR_FAIL_NULL(particles); |
| 367 | particles->one_shot = p_one_shot; |
| 368 | } |
| 369 | |
| 370 | void ParticlesStorage::particles_set_pre_process_time(RID p_particles, double p_time) { |
| 371 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 372 | ERR_FAIL_NULL(particles); |
| 373 | particles->pre_process_time = p_time; |
| 374 | } |
| 375 | |
| 376 | void ParticlesStorage::particles_request_process_time(RID p_particles, real_t p_request_process_time) { |
| 377 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 378 | ERR_FAIL_NULL(particles); |
| 379 | particles->request_process_time = p_request_process_time; |
| 380 | } |
| 381 | |
| 382 | void ParticlesStorage::particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) { |
| 383 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 384 | ERR_FAIL_NULL(particles); |
| 385 | particles->explosiveness = p_ratio; |
| 386 | } |
| 387 | void ParticlesStorage::particles_set_randomness_ratio(RID p_particles, real_t p_ratio) { |
| 388 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 389 | ERR_FAIL_NULL(particles); |
| 390 | particles->randomness = p_ratio; |
| 391 | } |
| 392 | |
| 393 | void ParticlesStorage::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) { |
| 394 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 395 | ERR_FAIL_NULL(particles); |
| 396 | particles->custom_aabb = p_aabb; |
| 397 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 398 | } |
| 399 | |
| 400 | void ParticlesStorage::particles_set_speed_scale(RID p_particles, double p_scale) { |
| 401 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 402 | ERR_FAIL_NULL(particles); |
| 403 | |
| 404 | particles->speed_scale = p_scale; |
| 405 | } |
| 406 | void ParticlesStorage::particles_set_use_local_coordinates(RID p_particles, bool p_enable) { |
| 407 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 408 | ERR_FAIL_NULL(particles); |
| 409 | |
| 410 | particles->use_local_coords = p_enable; |
| 411 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 412 | } |
| 413 | |
| 414 | void ParticlesStorage::particles_set_fixed_fps(RID p_particles, int p_fps) { |
| 415 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 416 | ERR_FAIL_NULL(particles); |
| 417 | |
| 418 | particles->fixed_fps = p_fps; |
| 419 | |
| 420 | _particles_free_data(particles); |
| 421 | |
| 422 | particles->prev_ticks = 0; |
| 423 | particles->phase = 0; |
| 424 | particles->prev_phase = 0; |
| 425 | particles->clear = true; |
| 426 | |
| 427 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 428 | } |
| 429 | |
| 430 | void ParticlesStorage::particles_set_interpolate(RID p_particles, bool p_enable) { |
| 431 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 432 | ERR_FAIL_NULL(particles); |
| 433 | |
| 434 | particles->interpolate = p_enable; |
| 435 | } |
| 436 | |
| 437 | void ParticlesStorage::particles_set_fractional_delta(RID p_particles, bool p_enable) { |
| 438 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 439 | ERR_FAIL_NULL(particles); |
| 440 | |
| 441 | particles->fractional_delta = p_enable; |
| 442 | } |
| 443 | |
| 444 | void ParticlesStorage::particles_set_trails(RID p_particles, bool p_enable, double p_length) { |
| 445 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 446 | ERR_FAIL_NULL(particles); |
| 447 | ERR_FAIL_COND(p_length < 0.01 - CMP_EPSILON); |
| 448 | p_length = MIN(10.0, p_length); |
| 449 | |
| 450 | particles->trails_enabled = p_enable; |
| 451 | particles->trail_lifetime = p_length; |
| 452 | |
| 453 | _particles_free_data(particles); |
| 454 | |
| 455 | particles->prev_ticks = 0; |
| 456 | particles->phase = 0; |
| 457 | particles->prev_phase = 0; |
| 458 | particles->clear = true; |
| 459 | |
| 460 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 461 | } |
| 462 | |
| 463 | void ParticlesStorage::particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) { |
| 464 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 465 | ERR_FAIL_NULL(particles); |
| 466 | if (particles->trail_bind_pose_buffer.is_valid() && particles->trail_bind_poses.size() != p_bind_poses.size()) { |
| 467 | _particles_free_data(particles); |
| 468 | |
| 469 | particles->prev_ticks = 0; |
| 470 | particles->phase = 0; |
| 471 | particles->prev_phase = 0; |
| 472 | particles->clear = true; |
| 473 | } |
| 474 | particles->trail_bind_poses = p_bind_poses; |
| 475 | particles->trail_bind_poses_dirty = true; |
| 476 | |
| 477 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 478 | } |
| 479 | |
| 480 | void ParticlesStorage::particles_set_collision_base_size(RID p_particles, real_t p_size) { |
| 481 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 482 | ERR_FAIL_NULL(particles); |
| 483 | |
| 484 | particles->collision_base_size = p_size; |
| 485 | } |
| 486 | |
| 487 | void ParticlesStorage::particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) { |
| 488 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 489 | ERR_FAIL_NULL(particles); |
| 490 | |
| 491 | particles->transform_align = p_transform_align; |
| 492 | } |
| 493 | |
| 494 | void ParticlesStorage::particles_set_process_material(RID p_particles, RID p_material) { |
| 495 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 496 | ERR_FAIL_NULL(particles); |
| 497 | |
| 498 | particles->process_material = p_material; |
| 499 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); //the instance buffer may have changed |
| 500 | } |
| 501 | |
| 502 | RID ParticlesStorage::particles_get_process_material(RID p_particles) const { |
| 503 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 504 | ERR_FAIL_NULL_V(particles, RID()); |
| 505 | |
| 506 | return particles->process_material; |
| 507 | } |
| 508 | |
| 509 | void ParticlesStorage::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) { |
| 510 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 511 | ERR_FAIL_NULL(particles); |
| 512 | |
| 513 | particles->draw_order = p_order; |
| 514 | } |
| 515 | |
| 516 | void ParticlesStorage::particles_set_draw_passes(RID p_particles, int p_passes) { |
| 517 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 518 | ERR_FAIL_NULL(particles); |
| 519 | |
| 520 | particles->draw_passes.resize(p_passes); |
| 521 | } |
| 522 | |
| 523 | void ParticlesStorage::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) { |
| 524 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 525 | ERR_FAIL_NULL(particles); |
| 526 | ERR_FAIL_INDEX(p_pass, particles->draw_passes.size()); |
| 527 | particles->draw_passes.write[p_pass] = p_mesh; |
| 528 | } |
| 529 | |
| 530 | void ParticlesStorage::particles_restart(RID p_particles) { |
| 531 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 532 | ERR_FAIL_NULL(particles); |
| 533 | |
| 534 | particles->restart_request = true; |
| 535 | } |
| 536 | |
| 537 | void ParticlesStorage::particles_set_seed(RID p_particles, uint32_t p_seed) { |
| 538 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 539 | ERR_FAIL_NULL(particles); |
| 540 | particles->random_seed = p_seed; |
| 541 | } |
| 542 | |
| 543 | void ParticlesStorage::_particles_allocate_emission_buffer(Particles *particles) { |
| 544 | ERR_FAIL_COND(particles->emission_buffer != nullptr); |
| 545 | |
| 546 | particles->emission_buffer_data.resize(sizeof(ParticleEmissionBuffer::Data) * particles->amount + sizeof(uint32_t) * 4); |
| 547 | memset(particles->emission_buffer_data.ptrw(), 0, particles->emission_buffer_data.size()); |
| 548 | particles->emission_buffer = reinterpret_cast<ParticleEmissionBuffer *>(particles->emission_buffer_data.ptrw()); |
| 549 | particles->emission_buffer->particle_max = particles->amount; |
| 550 | |
| 551 | particles->emission_storage_buffer = RD::get_singleton()->storage_buffer_create(particles->emission_buffer_data.size(), particles->emission_buffer_data); |
| 552 | |
| 553 | if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) { |
| 554 | //will need to be re-created |
| 555 | RD::get_singleton()->free_rid(particles->particles_material_uniform_set); |
| 556 | particles->particles_material_uniform_set = RID(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void ParticlesStorage::_particles_ensure_unused_emission_buffer(Particles *particles) { |
| 561 | if (particles->unused_emission_storage_buffer.is_null()) { |
| 562 | // For rendering devices that do not support empty arrays (like C++), |
| 563 | // we need to size the buffer with at least 1 element. |
| 564 | particles->unused_emission_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticleEmissionBuffer)); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void ParticlesStorage::_particles_ensure_unused_trail_buffer(Particles *particles) { |
| 569 | if (particles->unused_trail_storage_buffer.is_null()) { |
| 570 | particles->unused_trail_storage_buffer = RD::get_singleton()->storage_buffer_create(16 * sizeof(float)); // Size of mat4. |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | void ParticlesStorage::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) { |
| 575 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 576 | ERR_FAIL_NULL(particles); |
| 577 | ERR_FAIL_COND(p_particles == p_subemitter_particles); |
| 578 | |
| 579 | particles->sub_emitter = p_subemitter_particles; |
| 580 | |
| 581 | if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) { |
| 582 | RD::get_singleton()->free_rid(particles->particles_material_uniform_set); |
| 583 | particles->particles_material_uniform_set = RID(); //clear and force to re create sub emitting |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | 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) { |
| 588 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 589 | ERR_FAIL_NULL(particles); |
| 590 | ERR_FAIL_COND(particles->amount == 0); |
| 591 | |
| 592 | if (particles->emitting) { |
| 593 | particles->clear = true; |
| 594 | particles->emitting = false; |
| 595 | } |
| 596 | |
| 597 | if (particles->emission_buffer == nullptr) { |
| 598 | _particles_allocate_emission_buffer(particles); |
| 599 | } |
| 600 | |
| 601 | particles->inactive = false; |
| 602 | particles->inactive_time = 0; |
| 603 | |
| 604 | int32_t idx = particles->emission_buffer->particle_count; |
| 605 | if (idx < particles->emission_buffer->particle_max) { |
| 606 | RendererRD::MaterialStorage::store_transform(p_transform, particles->emission_buffer->data[idx].xform); |
| 607 | |
| 608 | particles->emission_buffer->data[idx].velocity[0] = p_velocity.x; |
| 609 | particles->emission_buffer->data[idx].velocity[1] = p_velocity.y; |
| 610 | particles->emission_buffer->data[idx].velocity[2] = p_velocity.z; |
| 611 | |
| 612 | particles->emission_buffer->data[idx].custom[0] = p_custom.r; |
| 613 | particles->emission_buffer->data[idx].custom[1] = p_custom.g; |
| 614 | particles->emission_buffer->data[idx].custom[2] = p_custom.b; |
| 615 | particles->emission_buffer->data[idx].custom[3] = p_custom.a; |
| 616 | |
| 617 | particles->emission_buffer->data[idx].color[0] = p_color.r; |
| 618 | particles->emission_buffer->data[idx].color[1] = p_color.g; |
| 619 | particles->emission_buffer->data[idx].color[2] = p_color.b; |
| 620 | particles->emission_buffer->data[idx].color[3] = p_color.a; |
| 621 | |
| 622 | particles->emission_buffer->data[idx].flags = p_emit_flags; |
| 623 | particles->emission_buffer->particle_count++; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void ParticlesStorage::particles_request_process(RID p_particles) { |
| 628 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 629 | ERR_FAIL_NULL(particles); |
| 630 | |
| 631 | if (!particles->dirty) { |
| 632 | particles->dirty = true; |
| 633 | |
| 634 | if (!particles->update_list.in_list()) { |
| 635 | particle_update_list.add(&particles->update_list); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | AABB ParticlesStorage::particles_get_current_aabb(RID p_particles) { |
| 641 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 642 | ERR_FAIL_NULL_V(particles, AABB()); |
| 643 | |
| 644 | int total_amount = particles->amount; |
| 645 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 646 | total_amount *= particles->trail_bind_poses.size(); |
| 647 | } |
| 648 | |
| 649 | uint32_t particle_data_size = sizeof(ParticleData) + sizeof(float) * 4 * particles->userdata_count; |
| 650 | Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(particles->particle_buffer); |
| 651 | ERR_FAIL_COND_V(buffer.size() != (int)(total_amount * particle_data_size), AABB()); |
| 652 | |
| 653 | Transform3D inv = particles->emission_transform.affine_inverse(); |
| 654 | |
| 655 | AABB aabb; |
| 656 | if (buffer.size()) { |
| 657 | bool first = true; |
| 658 | |
| 659 | const uint8_t *data_ptr = (const uint8_t *)buffer.ptr(); |
| 660 | |
| 661 | for (int i = 0; i < total_amount; i++) { |
| 662 | const ParticleData &particle_data = *(const ParticleData *)&data_ptr[particle_data_size * i]; |
| 663 | if (particle_data.active) { |
| 664 | Vector3 pos = Vector3(particle_data.xform[12], particle_data.xform[13], particle_data.xform[14]); |
| 665 | if (!particles->use_local_coords) { |
| 666 | pos = inv.xform(pos); |
| 667 | } |
| 668 | if (first) { |
| 669 | aabb.position = pos; |
| 670 | first = false; |
| 671 | } else { |
| 672 | aabb.expand_to(pos); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | float longest_axis_size = 0; |
| 679 | for (int i = 0; i < particles->draw_passes.size(); i++) { |
| 680 | if (particles->draw_passes[i].is_valid()) { |
| 681 | AABB maabb = MeshStorage::get_singleton()->mesh_get_aabb(particles->draw_passes[i], RID()); |
| 682 | longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | aabb.grow_by(longest_axis_size); |
| 687 | |
| 688 | return aabb; |
| 689 | } |
| 690 | |
| 691 | AABB ParticlesStorage::particles_get_aabb(RID p_particles) const { |
| 692 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 693 | ERR_FAIL_NULL_V(particles, AABB()); |
| 694 | |
| 695 | return particles->custom_aabb; |
| 696 | } |
| 697 | |
| 698 | void ParticlesStorage::particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) { |
| 699 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 700 | ERR_FAIL_NULL(particles); |
| 701 | |
| 702 | particles->emission_transform = p_transform; |
| 703 | } |
| 704 | |
| 705 | void ParticlesStorage::particles_set_emitter_velocity(RID p_particles, const Vector3 &p_velocity) { |
| 706 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 707 | ERR_FAIL_NULL(particles); |
| 708 | |
| 709 | particles->emitter_velocity = p_velocity; |
| 710 | } |
| 711 | |
| 712 | void ParticlesStorage::particles_set_interp_to_end(RID p_particles, float p_interp) { |
| 713 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 714 | ERR_FAIL_NULL(particles); |
| 715 | |
| 716 | particles->interp_to_end = p_interp; |
| 717 | } |
| 718 | |
| 719 | int ParticlesStorage::particles_get_draw_passes(RID p_particles) const { |
| 720 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 721 | ERR_FAIL_NULL_V(particles, 0); |
| 722 | |
| 723 | return particles->draw_passes.size(); |
| 724 | } |
| 725 | |
| 726 | RID ParticlesStorage::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const { |
| 727 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 728 | ERR_FAIL_NULL_V(particles, RID()); |
| 729 | ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID()); |
| 730 | |
| 731 | return particles->draw_passes[p_pass]; |
| 732 | } |
| 733 | |
| 734 | void ParticlesStorage::particles_update_dependency(RID p_particles, DependencyTracker *p_instance) { |
| 735 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 736 | ERR_FAIL_NULL(particles); |
| 737 | p_instance->update_dependency(&particles->dependency); |
| 738 | } |
| 739 | |
| 740 | void ParticlesStorage::particles_get_instance_buffer_motion_vectors_offsets(RID p_particles, uint32_t &r_current_offset, uint32_t &r_prev_offset) { |
| 741 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 742 | ERR_FAIL_NULL(particles); |
| 743 | r_current_offset = particles->instance_motion_vectors_current_offset; |
| 744 | r_prev_offset = particles->instance_motion_vectors_previous_offset; |
| 745 | } |
| 746 | |
| 747 | void ParticlesStorage::particles_add_collision(RID p_particles, RID p_particles_collision_instance) { |
| 748 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 749 | ERR_FAIL_NULL(particles); |
| 750 | particles->collisions.insert(p_particles_collision_instance); |
| 751 | } |
| 752 | |
| 753 | void ParticlesStorage::particles_remove_collision(RID p_particles, RID p_particles_collision_instance) { |
| 754 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 755 | ERR_FAIL_NULL(particles); |
| 756 | particles->collisions.erase(p_particles_collision_instance); |
| 757 | } |
| 758 | |
| 759 | 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) { |
| 760 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 761 | ERR_FAIL_NULL(particles); |
| 762 | particles->has_sdf_collision = p_enable; |
| 763 | particles->sdf_collision_transform = p_xform; |
| 764 | particles->sdf_collision_to_screen = p_to_screen; |
| 765 | particles->sdf_collision_texture = p_texture; |
| 766 | } |
| 767 | |
| 768 | void ParticlesStorage::_particles_process(Particles *p_particles, double p_delta) { |
| 769 | TextureStorage *texture_storage = TextureStorage::get_singleton(); |
| 770 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 771 | |
| 772 | if (p_particles->particles_material_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(p_particles->particles_material_uniform_set)) { |
| 773 | thread_local LocalVector<RD::Uniform> uniforms; |
| 774 | uniforms.clear(); |
| 775 | |
| 776 | { |
| 777 | RD::Uniform u; |
| 778 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 779 | u.binding = 0; |
| 780 | u.append_id(p_particles->frame_params_buffer); |
| 781 | uniforms.push_back(u); |
| 782 | } |
| 783 | { |
| 784 | RD::Uniform u; |
| 785 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 786 | u.binding = 1; |
| 787 | u.append_id(p_particles->particle_buffer); |
| 788 | uniforms.push_back(u); |
| 789 | } |
| 790 | |
| 791 | { |
| 792 | RD::Uniform u; |
| 793 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 794 | u.binding = 2; |
| 795 | if (p_particles->emission_storage_buffer.is_valid()) { |
| 796 | u.append_id(p_particles->emission_storage_buffer); |
| 797 | } else { |
| 798 | _particles_ensure_unused_emission_buffer(p_particles); |
| 799 | u.append_id(p_particles->unused_emission_storage_buffer); |
| 800 | } |
| 801 | uniforms.push_back(u); |
| 802 | } |
| 803 | { |
| 804 | RD::Uniform u; |
| 805 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 806 | u.binding = 3; |
| 807 | Particles *sub_emitter = particles_owner.get_or_null(p_particles->sub_emitter); |
| 808 | if (sub_emitter) { |
| 809 | if (sub_emitter->emission_buffer == nullptr) { //no emission buffer, allocate emission buffer |
| 810 | _particles_allocate_emission_buffer(sub_emitter); |
| 811 | } |
| 812 | u.append_id(sub_emitter->emission_storage_buffer); |
| 813 | } else { |
| 814 | _particles_ensure_unused_emission_buffer(p_particles); |
| 815 | u.append_id(p_particles->unused_emission_storage_buffer); |
| 816 | } |
| 817 | uniforms.push_back(u); |
| 818 | } |
| 819 | |
| 820 | p_particles->particles_material_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 1); |
| 821 | } |
| 822 | |
| 823 | double new_phase = Math::fmod((double)p_particles->phase + (p_delta / p_particles->lifetime), 1.0); |
| 824 | |
| 825 | //move back history (if there is any) |
| 826 | for (uint32_t i = p_particles->frame_history.size() - 1; i > 0; i--) { |
| 827 | p_particles->frame_history[i] = p_particles->frame_history[i - 1]; |
| 828 | } |
| 829 | //update current frame |
| 830 | ParticlesFrameParams &frame_params = p_particles->frame_history[0]; |
| 831 | |
| 832 | if (p_particles->clear) { |
| 833 | p_particles->cycle_number = 0; |
| 834 | } else if (new_phase < p_particles->phase) { |
| 835 | if (p_particles->one_shot) { |
| 836 | p_particles->emitting = false; |
| 837 | } |
| 838 | p_particles->cycle_number++; |
| 839 | } |
| 840 | |
| 841 | frame_params.emitting = p_particles->emitting; |
| 842 | frame_params.system_phase = new_phase; |
| 843 | frame_params.prev_system_phase = p_particles->phase; |
| 844 | |
| 845 | p_particles->phase = new_phase; |
| 846 | |
| 847 | frame_params.time = RendererCompositorRD::get_singleton()->get_total_time(); |
| 848 | frame_params.delta = p_delta; |
| 849 | frame_params.random_seed = p_particles->random_seed; |
| 850 | frame_params.explosiveness = p_particles->explosiveness; |
| 851 | frame_params.randomness = p_particles->randomness; |
| 852 | |
| 853 | if (p_particles->use_local_coords) { |
| 854 | RendererRD::MaterialStorage::store_transform(Transform3D(), frame_params.emission_transform); |
| 855 | } else { |
| 856 | RendererRD::MaterialStorage::store_transform(p_particles->emission_transform, frame_params.emission_transform); |
| 857 | } |
| 858 | |
| 859 | frame_params.cycle = p_particles->cycle_number; |
| 860 | frame_params.frame = p_particles->frame_counter++; |
| 861 | frame_params.amount_ratio = p_particles->amount_ratio; |
| 862 | frame_params.pad1 = 0; |
| 863 | frame_params.pad2 = 0; |
| 864 | frame_params.emitter_velocity[0] = p_particles->emitter_velocity.x; |
| 865 | frame_params.emitter_velocity[1] = p_particles->emitter_velocity.y; |
| 866 | frame_params.emitter_velocity[2] = p_particles->emitter_velocity.z; |
| 867 | frame_params.interp_to_end = p_particles->interp_to_end; |
| 868 | |
| 869 | { //collision and attractors |
| 870 | |
| 871 | frame_params.collider_count = 0; |
| 872 | frame_params.attractor_count = 0; |
| 873 | frame_params.particle_size = p_particles->collision_base_size; |
| 874 | |
| 875 | RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES]; |
| 876 | RID collision_heightmap_texture; |
| 877 | |
| 878 | Transform3D to_particles; |
| 879 | if (p_particles->use_local_coords) { |
| 880 | to_particles = p_particles->emission_transform.affine_inverse(); |
| 881 | } |
| 882 | |
| 883 | if (p_particles->has_sdf_collision && RD::get_singleton()->texture_is_valid(p_particles->sdf_collision_texture)) { |
| 884 | //2D collision |
| 885 | |
| 886 | Transform2D xform = p_particles->sdf_collision_transform; //will use dotproduct manually so invert beforehand |
| 887 | |
| 888 | if (!p_particles->use_local_coords) { |
| 889 | Transform2D emission; |
| 890 | emission.columns[0] = Vector2(p_particles->emission_transform.basis.get_column(0).x, p_particles->emission_transform.basis.get_column(0).y); |
| 891 | emission.columns[1] = Vector2(p_particles->emission_transform.basis.get_column(1).x, p_particles->emission_transform.basis.get_column(1).y); |
| 892 | emission.set_origin(Vector2(p_particles->emission_transform.origin.x, p_particles->emission_transform.origin.y)); |
| 893 | xform = xform * emission.affine_inverse(); |
| 894 | } |
| 895 | |
| 896 | Transform2D revert = xform.affine_inverse(); |
| 897 | frame_params.collider_count = 1; |
| 898 | frame_params.colliders[0].transform[0] = xform.columns[0][0]; |
| 899 | frame_params.colliders[0].transform[1] = xform.columns[0][1]; |
| 900 | frame_params.colliders[0].transform[2] = 0; |
| 901 | frame_params.colliders[0].transform[3] = xform.columns[2][0]; |
| 902 | |
| 903 | frame_params.colliders[0].transform[4] = xform.columns[1][0]; |
| 904 | frame_params.colliders[0].transform[5] = xform.columns[1][1]; |
| 905 | frame_params.colliders[0].transform[6] = 0; |
| 906 | frame_params.colliders[0].transform[7] = xform.columns[2][1]; |
| 907 | |
| 908 | frame_params.colliders[0].transform[8] = revert.columns[0][0]; |
| 909 | frame_params.colliders[0].transform[9] = revert.columns[0][1]; |
| 910 | frame_params.colliders[0].transform[10] = 0; |
| 911 | frame_params.colliders[0].transform[11] = revert.columns[2][0]; |
| 912 | |
| 913 | frame_params.colliders[0].transform[12] = revert.columns[1][0]; |
| 914 | frame_params.colliders[0].transform[13] = revert.columns[1][1]; |
| 915 | frame_params.colliders[0].transform[14] = 0; |
| 916 | frame_params.colliders[0].transform[15] = revert.columns[2][1]; |
| 917 | |
| 918 | frame_params.colliders[0].extents[0] = p_particles->sdf_collision_to_screen.size.x; |
| 919 | frame_params.colliders[0].extents[1] = p_particles->sdf_collision_to_screen.size.y; |
| 920 | frame_params.colliders[0].extents[2] = p_particles->sdf_collision_to_screen.position.x; |
| 921 | frame_params.colliders[0].scale = p_particles->sdf_collision_to_screen.position.y; |
| 922 | frame_params.colliders[0].texture_index = 0; |
| 923 | frame_params.colliders[0].type = ParticlesFrameParams::COLLISION_TYPE_2D_SDF; |
| 924 | |
| 925 | collision_heightmap_texture = p_particles->sdf_collision_texture; |
| 926 | |
| 927 | //replace in all other history frames where used because parameters are no longer valid if screen moves |
| 928 | for (ParticlesFrameParams ¶ms : p_particles->frame_history) { |
| 929 | if (params.collider_count > 0 && params.colliders[0].type == ParticlesFrameParams::COLLISION_TYPE_2D_SDF) { |
| 930 | params.colliders[0] = frame_params.colliders[0]; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | uint32_t collision_3d_textures_used = 0; |
| 936 | for (const RID &E : p_particles->collisions) { |
| 937 | ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(E); |
| 938 | if (!pci || !pci->active) { |
| 939 | continue; |
| 940 | } |
| 941 | ParticlesCollision *pc = particles_collision_owner.get_or_null(pci->collision); |
| 942 | ERR_CONTINUE(!pc); |
| 943 | |
| 944 | Transform3D to_collider = pci->transform; |
| 945 | if (p_particles->use_local_coords) { |
| 946 | to_collider = to_particles * to_collider; |
| 947 | } |
| 948 | Vector3 scale = to_collider.basis.get_scale(); |
| 949 | to_collider.basis.orthonormalize(); |
| 950 | |
| 951 | if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) { |
| 952 | //attractor |
| 953 | if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) { |
| 954 | continue; |
| 955 | } |
| 956 | |
| 957 | ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count]; |
| 958 | |
| 959 | RendererRD::MaterialStorage::store_transform(to_collider, attr.transform); |
| 960 | attr.strength = pc->attractor_strength; |
| 961 | attr.attenuation = pc->attractor_attenuation; |
| 962 | attr.directionality = pc->attractor_directionality; |
| 963 | |
| 964 | switch (pc->type) { |
| 965 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: { |
| 966 | attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE; |
| 967 | float radius = pc->radius; |
| 968 | radius *= (scale.x + scale.y + scale.z) / 3.0; |
| 969 | attr.extents[0] = radius; |
| 970 | attr.extents[1] = radius; |
| 971 | attr.extents[2] = radius; |
| 972 | } break; |
| 973 | case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: { |
| 974 | attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX; |
| 975 | Vector3 extents = pc->extents * scale; |
| 976 | attr.extents[0] = extents.x; |
| 977 | attr.extents[1] = extents.y; |
| 978 | attr.extents[2] = extents.z; |
| 979 | } break; |
| 980 | case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: { |
| 981 | if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) { |
| 982 | continue; |
| 983 | } |
| 984 | attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_VECTOR_FIELD; |
| 985 | Vector3 extents = pc->extents * scale; |
| 986 | attr.extents[0] = extents.x; |
| 987 | attr.extents[1] = extents.y; |
| 988 | attr.extents[2] = extents.z; |
| 989 | attr.texture_index = collision_3d_textures_used; |
| 990 | |
| 991 | collision_3d_textures[collision_3d_textures_used] = pc->field_texture; |
| 992 | collision_3d_textures_used++; |
| 993 | } break; |
| 994 | default: { |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | frame_params.attractor_count++; |
| 999 | } else { |
| 1000 | //collider |
| 1001 | if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) { |
| 1002 | continue; |
| 1003 | } |
| 1004 | |
| 1005 | ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count]; |
| 1006 | |
| 1007 | RendererRD::MaterialStorage::store_transform(to_collider, col.transform); |
| 1008 | switch (pc->type) { |
| 1009 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: { |
| 1010 | col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE; |
| 1011 | float radius = pc->radius; |
| 1012 | radius *= (scale.x + scale.y + scale.z) / 3.0; |
| 1013 | col.extents[0] = radius; |
| 1014 | col.extents[1] = radius; |
| 1015 | col.extents[2] = radius; |
| 1016 | } break; |
| 1017 | case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: { |
| 1018 | col.type = ParticlesFrameParams::COLLISION_TYPE_BOX; |
| 1019 | Vector3 extents = pc->extents * scale; |
| 1020 | col.extents[0] = extents.x; |
| 1021 | col.extents[1] = extents.y; |
| 1022 | col.extents[2] = extents.z; |
| 1023 | } break; |
| 1024 | case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: { |
| 1025 | if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) { |
| 1026 | continue; |
| 1027 | } |
| 1028 | col.type = ParticlesFrameParams::COLLISION_TYPE_SDF; |
| 1029 | Vector3 extents = pc->extents * scale; |
| 1030 | col.extents[0] = extents.x; |
| 1031 | col.extents[1] = extents.y; |
| 1032 | col.extents[2] = extents.z; |
| 1033 | col.texture_index = collision_3d_textures_used; |
| 1034 | col.scale = (scale.x + scale.y + scale.z) * 0.333333333333; //non uniform scale non supported |
| 1035 | |
| 1036 | collision_3d_textures[collision_3d_textures_used] = pc->field_texture; |
| 1037 | collision_3d_textures_used++; |
| 1038 | } break; |
| 1039 | case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: { |
| 1040 | if (collision_heightmap_texture != RID()) { //already taken |
| 1041 | continue; |
| 1042 | } |
| 1043 | |
| 1044 | col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD; |
| 1045 | Vector3 extents = pc->extents * scale; |
| 1046 | col.extents[0] = extents.x; |
| 1047 | col.extents[1] = extents.y; |
| 1048 | col.extents[2] = extents.z; |
| 1049 | collision_heightmap_texture = pc->heightfield_texture; |
| 1050 | } break; |
| 1051 | default: { |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | frame_params.collider_count++; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | bool different = false; |
| 1060 | if (collision_3d_textures_used == p_particles->collision_3d_textures_used) { |
| 1061 | for (int i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) { |
| 1062 | if (p_particles->collision_3d_textures[i] != collision_3d_textures[i]) { |
| 1063 | different = true; |
| 1064 | break; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | if (collision_heightmap_texture != p_particles->collision_heightmap_texture) { |
| 1070 | different = true; |
| 1071 | } |
| 1072 | |
| 1073 | bool uniform_set_valid = RD::get_singleton()->uniform_set_is_valid(p_particles->collision_textures_uniform_set); |
| 1074 | |
| 1075 | if (different || !uniform_set_valid) { |
| 1076 | if (uniform_set_valid) { |
| 1077 | RD::get_singleton()->free_rid(p_particles->collision_textures_uniform_set); |
| 1078 | } |
| 1079 | |
| 1080 | thread_local LocalVector<RD::Uniform> uniforms; |
| 1081 | uniforms.clear(); |
| 1082 | |
| 1083 | { |
| 1084 | RD::Uniform u; |
| 1085 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1086 | u.binding = 0; |
| 1087 | for (uint32_t i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) { |
| 1088 | RID rd_tex; |
| 1089 | if (i < collision_3d_textures_used) { |
| 1090 | if (TextureStorage::get_singleton()->texture_get_type(collision_3d_textures[i]) == TextureStorage::TYPE_3D) { |
| 1091 | rd_tex = TextureStorage::get_singleton()->texture_get_rd_texture(collision_3d_textures[i]); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | if (rd_tex == RID()) { |
| 1096 | rd_tex = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE); |
| 1097 | } |
| 1098 | u.append_id(rd_tex); |
| 1099 | } |
| 1100 | uniforms.push_back(u); |
| 1101 | } |
| 1102 | { |
| 1103 | RD::Uniform u; |
| 1104 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1105 | u.binding = 1; |
| 1106 | if (collision_heightmap_texture.is_valid()) { |
| 1107 | u.append_id(collision_heightmap_texture); |
| 1108 | } else { |
| 1109 | u.append_id(texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_BLACK)); |
| 1110 | } |
| 1111 | uniforms.push_back(u); |
| 1112 | } |
| 1113 | p_particles->collision_textures_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 2); |
| 1114 | p_particles->collision_heightmap_texture = collision_heightmap_texture; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | ParticlesShader::PushConstant push_constant; |
| 1119 | |
| 1120 | int process_amount = p_particles->amount; |
| 1121 | |
| 1122 | if (p_particles->trails_enabled && p_particles->trail_bind_poses.size() > 1) { |
| 1123 | process_amount *= p_particles->trail_bind_poses.size(); |
| 1124 | } |
| 1125 | push_constant.clear = p_particles->clear; |
| 1126 | push_constant.total_particles = p_particles->amount; |
| 1127 | push_constant.lifetime = p_particles->lifetime; |
| 1128 | push_constant.trail_size = p_particles->trail_params.size(); |
| 1129 | push_constant.use_fractional_delta = p_particles->fractional_delta; |
| 1130 | push_constant.sub_emitter_mode = !p_particles->emitting && p_particles->emission_buffer && (p_particles->emission_buffer->particle_count > 0 || p_particles->force_sub_emit); |
| 1131 | push_constant.trail_pass = false; |
| 1132 | |
| 1133 | p_particles->force_sub_emit = false; //reset |
| 1134 | |
| 1135 | Particles *sub_emitter = particles_owner.get_or_null(p_particles->sub_emitter); |
| 1136 | |
| 1137 | if (sub_emitter && sub_emitter->emission_storage_buffer.is_valid()) { |
| 1138 | // print_line("updating subemitter buffer"); |
| 1139 | int32_t zero[4] = { 0, sub_emitter->amount, 0, 0 }; |
| 1140 | RD::get_singleton()->buffer_update(sub_emitter->emission_storage_buffer, 0, sizeof(uint32_t) * 4, zero); |
| 1141 | push_constant.can_emit = true; |
| 1142 | |
| 1143 | if (sub_emitter->emitting) { |
| 1144 | sub_emitter->emitting = false; |
| 1145 | sub_emitter->clear = true; //will need to clear if it was emitting, sorry |
| 1146 | } |
| 1147 | //make sure the sub emitter processes particles too |
| 1148 | sub_emitter->inactive = false; |
| 1149 | sub_emitter->inactive_time = 0; |
| 1150 | |
| 1151 | sub_emitter->force_sub_emit = true; |
| 1152 | |
| 1153 | } else { |
| 1154 | push_constant.can_emit = false; |
| 1155 | } |
| 1156 | |
| 1157 | if (p_particles->emission_buffer && p_particles->emission_buffer->particle_count) { |
| 1158 | 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); |
| 1159 | p_particles->emission_buffer->particle_count = 0; |
| 1160 | } |
| 1161 | |
| 1162 | p_particles->clear = false; |
| 1163 | |
| 1164 | if (p_particles->trail_params.size() > 1) { |
| 1165 | //fill the trail params |
| 1166 | for (uint32_t i = 0; i < p_particles->trail_params.size(); i++) { |
| 1167 | uint32_t src_idx = i * p_particles->frame_history.size() / p_particles->trail_params.size(); |
| 1168 | if (p_particles->speed_scale <= 0.0) { |
| 1169 | // Stop trails. |
| 1170 | src_idx = 0; |
| 1171 | } |
| 1172 | p_particles->trail_params[i] = p_particles->frame_history[src_idx]; |
| 1173 | } |
| 1174 | } else { |
| 1175 | p_particles->trail_params[0] = p_particles->frame_history[0]; |
| 1176 | } |
| 1177 | |
| 1178 | RD::get_singleton()->buffer_update(p_particles->frame_params_buffer, 0, sizeof(ParticlesFrameParams) * p_particles->trail_params.size(), p_particles->trail_params.ptr()); |
| 1179 | |
| 1180 | ParticleProcessMaterialData *m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(p_particles->process_material, MaterialStorage::SHADER_TYPE_PARTICLES)); |
| 1181 | if (!m) { |
| 1182 | m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(particles_shader.default_material, MaterialStorage::SHADER_TYPE_PARTICLES)); |
| 1183 | } |
| 1184 | |
| 1185 | ERR_FAIL_NULL(m); |
| 1186 | |
| 1187 | p_particles->has_collision_cache = m->shader_data->uses_collision; |
| 1188 | |
| 1189 | //todo should maybe compute all particle systems together? |
| 1190 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 1191 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, m->shader_data->pipeline.get_rid()); |
| 1192 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles_shader.base_uniform_set, BASE_UNIFORM_SET); |
| 1193 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->particles_material_uniform_set, MATERIAL_UNIFORM_SET); |
| 1194 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->collision_textures_uniform_set, COLLISION_TEXTURTES_UNIFORM_SET); |
| 1195 | |
| 1196 | if (m->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(m->uniform_set)) { |
| 1197 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, m->uniform_set, 3); |
| 1198 | m->set_as_used(); |
| 1199 | } |
| 1200 | |
| 1201 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant)); |
| 1202 | |
| 1203 | if (p_particles->trails_enabled && p_particles->trail_bind_poses.size() > 1) { |
| 1204 | //trails requires two passes in order to catch particle starts |
| 1205 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount / p_particles->trail_bind_poses.size(), 1, 1); |
| 1206 | |
| 1207 | RD::get_singleton()->compute_list_add_barrier(compute_list); |
| 1208 | |
| 1209 | push_constant.trail_pass = true; |
| 1210 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant)); |
| 1211 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount - p_particles->amount, 1, 1); |
| 1212 | } else { |
| 1213 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount, 1, 1); |
| 1214 | } |
| 1215 | |
| 1216 | RD::get_singleton()->compute_list_end(); |
| 1217 | } |
| 1218 | |
| 1219 | void ParticlesStorage::particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) { |
| 1220 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 1221 | ERR_FAIL_NULL(particles); |
| 1222 | |
| 1223 | 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) { |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | if (particles->particle_buffer.is_null() || particles->trail_bind_pose_uniform_set.is_null()) { |
| 1228 | return; //particles have not processed yet |
| 1229 | } |
| 1230 | |
| 1231 | bool do_sort = particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH; |
| 1232 | |
| 1233 | //copy to sort buffer |
| 1234 | if (do_sort && particles->particles_sort_buffer == RID()) { |
| 1235 | uint32_t size = particles->amount; |
| 1236 | if (size & 1) { |
| 1237 | size++; //make multiple of 16 |
| 1238 | } |
| 1239 | size *= sizeof(float) * 2; |
| 1240 | particles->particles_sort_buffer = RD::get_singleton()->storage_buffer_create(size); |
| 1241 | |
| 1242 | { |
| 1243 | Vector<RD::Uniform> uniforms; |
| 1244 | |
| 1245 | { |
| 1246 | RD::Uniform u; |
| 1247 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1248 | u.binding = 0; |
| 1249 | u.append_id(particles->particles_sort_buffer); |
| 1250 | uniforms.push_back(u); |
| 1251 | } |
| 1252 | |
| 1253 | 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); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | ParticlesShader::CopyPushConstant copy_push_constant; |
| 1258 | |
| 1259 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1260 | int fixed_fps = 60.0; |
| 1261 | if (particles->fixed_fps > 0) { |
| 1262 | fixed_fps = particles->fixed_fps; |
| 1263 | } |
| 1264 | |
| 1265 | copy_push_constant.trail_size = particles->trail_bind_poses.size(); |
| 1266 | copy_push_constant.trail_total = particles->frame_history.size(); |
| 1267 | copy_push_constant.frame_delta = 1.0 / fixed_fps; |
| 1268 | } else { |
| 1269 | copy_push_constant.trail_size = 1; |
| 1270 | copy_push_constant.trail_total = 1; |
| 1271 | copy_push_constant.frame_delta = 0.0; |
| 1272 | } |
| 1273 | |
| 1274 | copy_push_constant.order_by_lifetime = (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME); |
| 1275 | copy_push_constant.lifetime_split = (MIN(int(particles->amount * particles->phase), particles->amount - 1) + 1) % particles->amount; |
| 1276 | copy_push_constant.lifetime_reverse = particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME; |
| 1277 | copy_push_constant.motion_vectors_current_offset = particles->instance_motion_vectors_current_offset; |
| 1278 | |
| 1279 | copy_push_constant.frame_remainder = particles->interpolate ? particles->frame_remainder : 0.0; |
| 1280 | copy_push_constant.total_particles = particles->amount; |
| 1281 | copy_push_constant.copy_mode_2d = false; |
| 1282 | |
| 1283 | Vector3 axis = -p_axis; // cameras look to z negative |
| 1284 | |
| 1285 | if (particles->use_local_coords) { |
| 1286 | axis = particles->emission_transform.basis.xform_inv(axis).normalized(); |
| 1287 | } |
| 1288 | |
| 1289 | copy_push_constant.sort_direction[0] = axis.x; |
| 1290 | copy_push_constant.sort_direction[1] = axis.y; |
| 1291 | copy_push_constant.sort_direction[2] = axis.z; |
| 1292 | |
| 1293 | copy_push_constant.align_up[0] = p_up_axis.x; |
| 1294 | copy_push_constant.align_up[1] = p_up_axis.y; |
| 1295 | copy_push_constant.align_up[2] = p_up_axis.z; |
| 1296 | |
| 1297 | copy_push_constant.align_mode = particles->transform_align; |
| 1298 | |
| 1299 | if (do_sort) { |
| 1300 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 1301 | |
| 1302 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[particles->userdata_count][ParticlesShader::COPY_MODE_FILL_SORT_BUFFER].get_rid()); |
| 1303 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0); |
| 1304 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1); |
| 1305 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2); |
| 1306 | RD::get_singleton()->compute_list_set_push_constant(compute_list, ©_push_constant, sizeof(ParticlesShader::CopyPushConstant)); |
| 1307 | |
| 1308 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1); |
| 1309 | |
| 1310 | RD::get_singleton()->compute_list_end(); |
| 1311 | sort_effects->sort_buffer(particles->particles_sort_uniform_set, particles->amount); |
| 1312 | } |
| 1313 | |
| 1314 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1315 | copy_push_constant.total_particles *= particles->trail_bind_poses.size(); |
| 1316 | } |
| 1317 | |
| 1318 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 1319 | uint32_t copy_mode = do_sort ? ParticlesShader::COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER : ParticlesShader::COPY_MODE_FILL_INSTANCES; |
| 1320 | copy_push_constant.copy_mode_2d = particles->mode == RS::PARTICLES_MODE_2D ? 1 : 0; |
| 1321 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[particles->userdata_count][copy_mode].get_rid()); |
| 1322 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0); |
| 1323 | if (do_sort) { |
| 1324 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1); |
| 1325 | } |
| 1326 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2); |
| 1327 | |
| 1328 | RD::get_singleton()->compute_list_set_push_constant(compute_list, ©_push_constant, sizeof(ParticlesShader::CopyPushConstant)); |
| 1329 | |
| 1330 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, copy_push_constant.total_particles, 1, 1); |
| 1331 | |
| 1332 | RD::get_singleton()->compute_list_end(); |
| 1333 | } |
| 1334 | |
| 1335 | void ParticlesStorage::_particles_update_buffers(Particles *particles) { |
| 1336 | uint32_t userdata_count = 0; |
| 1337 | |
| 1338 | if (particles->process_material.is_valid()) { |
| 1339 | ParticleProcessMaterialData *material_data = static_cast<ParticleProcessMaterialData *>(MaterialStorage::get_singleton()->material_get_data(particles->process_material, MaterialStorage::SHADER_TYPE_PARTICLES)); |
| 1340 | if (material_data && material_data->shader_data->version.is_valid() && material_data->shader_data->valid) { |
| 1341 | userdata_count = material_data->shader_data->userdata_count; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | bool uses_motion_vectors = RSG::viewport->get_num_viewports_with_motion_vectors() > 0 || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0); |
| 1346 | bool index_draw_order = particles->draw_order == RS::ParticlesDrawOrder::PARTICLES_DRAW_ORDER_INDEX; |
| 1347 | bool enable_motion_vectors = uses_motion_vectors && index_draw_order && !particles->instance_motion_vectors_enabled; |
| 1348 | bool only_instances_changed = false; |
| 1349 | |
| 1350 | if (userdata_count != particles->userdata_count) { |
| 1351 | // Mismatch userdata, re-create all buffers. |
| 1352 | _particles_free_data(particles); |
| 1353 | } else if (enable_motion_vectors) { |
| 1354 | // Only motion vectors are required, release the transforms buffer and uniform set. |
| 1355 | if (particles->particle_instance_buffer.is_valid()) { |
| 1356 | RD::get_singleton()->free_rid(particles->particle_instance_buffer); |
| 1357 | particles->particle_instance_buffer = RID(); |
| 1358 | } |
| 1359 | |
| 1360 | particles->particles_transforms_buffer_uniform_set = RID(); |
| 1361 | only_instances_changed = true; |
| 1362 | } else if (!particles->particle_buffer.is_null()) { |
| 1363 | // No operation is required because a buffer already exists, return early. |
| 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | if (particles->amount > 0) { |
| 1368 | int total_amount = particles->amount; |
| 1369 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1370 | total_amount *= particles->trail_bind_poses.size(); |
| 1371 | } |
| 1372 | |
| 1373 | uint32_t xform_size = particles->mode == RS::PARTICLES_MODE_2D ? 2 : 3; |
| 1374 | if (particles->particle_buffer.is_null()) { |
| 1375 | particles->particle_buffer = RD::get_singleton()->storage_buffer_create((sizeof(ParticleData) + userdata_count * sizeof(float) * 4) * total_amount); |
| 1376 | particles->userdata_count = userdata_count; |
| 1377 | } |
| 1378 | |
| 1379 | PackedByteArray data; |
| 1380 | uint32_t particle_instance_buffer_size = total_amount * (xform_size + 1 + 1) * sizeof(float) * 4; |
| 1381 | if (uses_motion_vectors) { |
| 1382 | particle_instance_buffer_size *= 2; |
| 1383 | particles->instance_motion_vectors_enabled = true; |
| 1384 | } |
| 1385 | |
| 1386 | data.resize_initialized(particle_instance_buffer_size); |
| 1387 | |
| 1388 | particles->particle_instance_buffer = RD::get_singleton()->storage_buffer_create(particle_instance_buffer_size, data); |
| 1389 | |
| 1390 | { |
| 1391 | Vector<RD::Uniform> uniforms; |
| 1392 | |
| 1393 | { |
| 1394 | RD::Uniform u; |
| 1395 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1396 | u.binding = 1; |
| 1397 | u.append_id(particles->particle_buffer); |
| 1398 | uniforms.push_back(u); |
| 1399 | } |
| 1400 | { |
| 1401 | RD::Uniform u; |
| 1402 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1403 | u.binding = 2; |
| 1404 | u.append_id(particles->particle_instance_buffer); |
| 1405 | uniforms.push_back(u); |
| 1406 | } |
| 1407 | |
| 1408 | 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); |
| 1409 | } |
| 1410 | |
| 1411 | particles->instance_motion_vectors_current_offset = 0; |
| 1412 | particles->instance_motion_vectors_previous_offset = 0; |
| 1413 | particles->instance_motion_vectors_last_change = -1; |
| 1414 | |
| 1415 | if (only_instances_changed) { |
| 1416 | // Notify the renderer the instances uniform must be retrieved again, as it's the only element that has been changed because motion vectors were enabled. |
| 1417 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES_INSTANCES); |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | void ParticlesStorage::update_particles() { |
| 1422 | if (!particle_update_list.first()) { |
| 1423 | return; |
| 1424 | } |
| 1425 | |
| 1426 | RENDER_TIMESTAMP("Update GPUParticles"); |
| 1427 | uint32_t frame = RSG::rasterizer->get_frame_number(); |
| 1428 | bool uses_motion_vectors = RSG::viewport->get_num_viewports_with_motion_vectors() > 0 || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0); |
| 1429 | while (particle_update_list.first()) { |
| 1430 | //use transform feedback to process particles |
| 1431 | |
| 1432 | Particles *particles = particle_update_list.first()->self(); |
| 1433 | |
| 1434 | particles->update_list.remove_from_list(); |
| 1435 | particles->dirty = false; |
| 1436 | |
| 1437 | _particles_update_buffers(particles); |
| 1438 | |
| 1439 | if (particles->restart_request) { |
| 1440 | particles->prev_ticks = 0; |
| 1441 | particles->phase = 0; |
| 1442 | particles->prev_phase = 0; |
| 1443 | particles->clear = true; |
| 1444 | particles->restart_request = false; |
| 1445 | } |
| 1446 | |
| 1447 | if (particles->inactive && !particles->emitting) { |
| 1448 | //go next |
| 1449 | continue; |
| 1450 | } |
| 1451 | |
| 1452 | if (particles->emitting) { |
| 1453 | if (particles->inactive) { |
| 1454 | //restart system from scratch |
| 1455 | particles->prev_ticks = 0; |
| 1456 | particles->phase = 0; |
| 1457 | particles->prev_phase = 0; |
| 1458 | particles->clear = true; |
| 1459 | } |
| 1460 | particles->inactive = false; |
| 1461 | particles->inactive_time = 0; |
| 1462 | } else { |
| 1463 | particles->inactive_time += particles->speed_scale * RendererCompositorRD::get_singleton()->get_frame_delta_time(); |
| 1464 | if (particles->inactive_time > particles->lifetime * 1.2) { |
| 1465 | particles->inactive = true; |
| 1466 | continue; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | // TODO: Should use display refresh rate for all this. |
| 1471 | float screen_hz = 60; |
| 1472 | |
| 1473 | int fixed_fps = 0; |
| 1474 | if (particles->fixed_fps > 0) { |
| 1475 | fixed_fps = particles->fixed_fps; |
| 1476 | } else if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1477 | fixed_fps = screen_hz; |
| 1478 | } |
| 1479 | { |
| 1480 | //update trails |
| 1481 | int history_size = 1; |
| 1482 | int trail_steps = 1; |
| 1483 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1484 | history_size = MAX(1, int(particles->trail_lifetime * fixed_fps)); |
| 1485 | trail_steps = particles->trail_bind_poses.size(); |
| 1486 | } |
| 1487 | |
| 1488 | if (uint32_t(history_size) != particles->frame_history.size()) { |
| 1489 | particles->frame_history.resize(history_size); |
| 1490 | memset(particles->frame_history.ptr(), 0, sizeof(ParticlesFrameParams) * history_size); |
| 1491 | // Set the frame number so that we are able to distinguish an uninitialized |
| 1492 | // frame from the true frame number zero. See issue #88712 for details. |
| 1493 | for (int i = 0; i < history_size; i++) { |
| 1494 | particles->frame_history[i].frame = UINT32_MAX; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | if (uint32_t(trail_steps) != particles->trail_params.size() || particles->frame_params_buffer.is_null()) { |
| 1499 | particles->trail_params.resize(trail_steps); |
| 1500 | if (particles->frame_params_buffer.is_valid()) { |
| 1501 | RD::get_singleton()->free_rid(particles->frame_params_buffer); |
| 1502 | } |
| 1503 | particles->frame_params_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticlesFrameParams) * trail_steps); |
| 1504 | } |
| 1505 | |
| 1506 | if (particles->trail_bind_poses.size() > 1 && particles->trail_bind_pose_buffer.is_null()) { |
| 1507 | particles->trail_bind_pose_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 16 * particles->trail_bind_poses.size()); |
| 1508 | particles->trail_bind_poses_dirty = true; |
| 1509 | } |
| 1510 | |
| 1511 | if (particles->trail_bind_pose_uniform_set.is_null()) { |
| 1512 | Vector<RD::Uniform> uniforms; |
| 1513 | { |
| 1514 | RD::Uniform u; |
| 1515 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1516 | u.binding = 0; |
| 1517 | if (particles->trail_bind_pose_buffer.is_valid()) { |
| 1518 | u.append_id(particles->trail_bind_pose_buffer); |
| 1519 | } else { |
| 1520 | _particles_ensure_unused_trail_buffer(particles); |
| 1521 | u.append_id(particles->unused_trail_storage_buffer); |
| 1522 | } |
| 1523 | uniforms.push_back(u); |
| 1524 | } |
| 1525 | |
| 1526 | 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); |
| 1527 | } |
| 1528 | |
| 1529 | if (particles->trail_bind_pose_buffer.is_valid() && particles->trail_bind_poses_dirty) { |
| 1530 | if (particles_shader.pose_update_buffer.size() < uint32_t(particles->trail_bind_poses.size()) * 16) { |
| 1531 | particles_shader.pose_update_buffer.resize(particles->trail_bind_poses.size() * 16); |
| 1532 | } |
| 1533 | |
| 1534 | for (int i = 0; i < particles->trail_bind_poses.size(); i++) { |
| 1535 | RendererRD::MaterialStorage::store_transform(particles->trail_bind_poses[i], &particles_shader.pose_update_buffer[i * 16]); |
| 1536 | } |
| 1537 | |
| 1538 | 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()); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | double todo = particles->request_process_time; |
| 1543 | if (particles->clear) { |
| 1544 | todo += particles->pre_process_time; |
| 1545 | } |
| 1546 | |
| 1547 | if (todo > 0.0) { |
| 1548 | double frame_time; |
| 1549 | if (fixed_fps > 0) { |
| 1550 | frame_time = 1.0 / fixed_fps; |
| 1551 | } else { |
| 1552 | frame_time = 1.0 / 30.0; |
| 1553 | } |
| 1554 | |
| 1555 | float tmp_scale = particles->speed_scale; |
| 1556 | // We need this otherwise the speed scale of the particle system influences the TODO. |
| 1557 | particles->speed_scale = 1.0; |
| 1558 | while (todo >= 0) { |
| 1559 | _particles_process(particles, frame_time); |
| 1560 | todo -= frame_time; |
| 1561 | } |
| 1562 | particles->request_process_time = 0.0; |
| 1563 | particles->speed_scale = tmp_scale; |
| 1564 | } |
| 1565 | |
| 1566 | double time_scale = MAX(particles->speed_scale, 0.0); |
| 1567 | |
| 1568 | if (fixed_fps > 0) { |
| 1569 | double frame_time = 1.0 / fixed_fps; |
| 1570 | double delta = RendererCompositorRD::get_singleton()->get_frame_delta_time(); |
| 1571 | if (delta > 0.1) { //avoid recursive stalls if fps goes below 10 |
| 1572 | delta = 0.1; |
| 1573 | } else if (delta <= 0.0) { //unlikely but.. |
| 1574 | delta = 0.001; |
| 1575 | } |
| 1576 | todo = particles->frame_remainder + delta * time_scale; |
| 1577 | |
| 1578 | while (todo >= frame_time || particles->clear) { |
| 1579 | _particles_process(particles, frame_time); |
| 1580 | todo -= frame_time; |
| 1581 | } |
| 1582 | |
| 1583 | particles->frame_remainder = todo; |
| 1584 | } else { |
| 1585 | _particles_process(particles, RendererCompositorRD::get_singleton()->get_frame_delta_time() * time_scale); |
| 1586 | } |
| 1587 | |
| 1588 | // Ensure that memory is initialized (the code above should ensure that _particles_process is always called at least once upon clearing). |
| 1589 | DEV_ASSERT(!particles->clear); |
| 1590 | |
| 1591 | int total_amount = particles->amount; |
| 1592 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1593 | total_amount *= particles->trail_bind_poses.size(); |
| 1594 | } |
| 1595 | |
| 1596 | // Swap offsets for motion vectors. Motion vectors can only be used when the draw order keeps the indices consistent across frames. |
| 1597 | bool index_draw_order = particles->draw_order == RS::ParticlesDrawOrder::PARTICLES_DRAW_ORDER_INDEX; |
| 1598 | particles->instance_motion_vectors_previous_offset = particles->instance_motion_vectors_current_offset; |
| 1599 | if (uses_motion_vectors && index_draw_order && particles->instance_motion_vectors_enabled && (frame - particles->instance_motion_vectors_last_change) == 1) { |
| 1600 | particles->instance_motion_vectors_current_offset = total_amount - particles->instance_motion_vectors_current_offset; |
| 1601 | } |
| 1602 | |
| 1603 | particles->instance_motion_vectors_last_change = frame; |
| 1604 | |
| 1605 | // Copy particles to instance buffer. |
| 1606 | 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) { |
| 1607 | //does not need view dependent operation, do copy here |
| 1608 | ParticlesShader::CopyPushConstant copy_push_constant; |
| 1609 | |
| 1610 | // Affect 2D only. |
| 1611 | if (particles->use_local_coords) { |
| 1612 | // In local mode, particle positions are calculated locally (relative to the node position) |
| 1613 | // and they're also drawn locally. |
| 1614 | // It works as expected, so we just pass an identity transform. |
| 1615 | RendererRD::MaterialStorage::store_transform(Transform3D(), copy_push_constant.inv_emission_transform); |
| 1616 | } else { |
| 1617 | // In global mode, particle positions are calculated globally (relative to the canvas origin) |
| 1618 | // but they're drawn locally. |
| 1619 | // So, we need to pass the inverse of the emission transform to bring the |
| 1620 | // particles to local coordinates before drawing. |
| 1621 | Transform3D inv = particles->emission_transform.affine_inverse(); |
| 1622 | RendererRD::MaterialStorage::store_transform(inv, copy_push_constant.inv_emission_transform); |
| 1623 | } |
| 1624 | |
| 1625 | copy_push_constant.total_particles = total_amount; |
| 1626 | copy_push_constant.frame_remainder = particles->interpolate ? particles->frame_remainder : 0.0; |
| 1627 | copy_push_constant.align_mode = particles->transform_align; |
| 1628 | copy_push_constant.align_up[0] = 0; |
| 1629 | copy_push_constant.align_up[1] = 0; |
| 1630 | copy_push_constant.align_up[2] = 0; |
| 1631 | |
| 1632 | if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) { |
| 1633 | copy_push_constant.trail_size = particles->trail_bind_poses.size(); |
| 1634 | copy_push_constant.trail_total = particles->frame_history.size(); |
| 1635 | copy_push_constant.frame_delta = 1.0 / fixed_fps; |
| 1636 | } else { |
| 1637 | copy_push_constant.trail_size = 1; |
| 1638 | copy_push_constant.trail_total = 1; |
| 1639 | copy_push_constant.frame_delta = 0.0; |
| 1640 | } |
| 1641 | |
| 1642 | copy_push_constant.order_by_lifetime = (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME); |
| 1643 | copy_push_constant.lifetime_split = (MIN(int(particles->amount * particles->phase), particles->amount - 1) + 1) % particles->amount; |
| 1644 | copy_push_constant.lifetime_reverse = particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME; |
| 1645 | copy_push_constant.motion_vectors_current_offset = particles->instance_motion_vectors_current_offset; |
| 1646 | |
| 1647 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 1648 | copy_push_constant.copy_mode_2d = particles->mode == RS::PARTICLES_MODE_2D ? 1 : 0; |
| 1649 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[particles->userdata_count][ParticlesShader::COPY_MODE_FILL_INSTANCES].get_rid()); |
| 1650 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0); |
| 1651 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2); |
| 1652 | RD::get_singleton()->compute_list_set_push_constant(compute_list, ©_push_constant, sizeof(ParticlesShader::CopyPushConstant)); |
| 1653 | |
| 1654 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, total_amount, 1, 1); |
| 1655 | |
| 1656 | RD::get_singleton()->compute_list_end(); |
| 1657 | } |
| 1658 | |
| 1659 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | Dependency *ParticlesStorage::particles_get_dependency(RID p_particles) const { |
| 1664 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 1665 | ERR_FAIL_NULL_V(particles, nullptr); |
| 1666 | |
| 1667 | return &particles->dependency; |
| 1668 | } |
| 1669 | |
| 1670 | bool ParticlesStorage::particles_is_inactive(RID p_particles) const { |
| 1671 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 1672 | ERR_FAIL_NULL_V(particles, false); |
| 1673 | return !particles->emitting && particles->inactive; |
| 1674 | } |
| 1675 | |
| 1676 | /* Particles SHADER */ |
| 1677 | |
| 1678 | void ParticlesStorage::ParticlesShaderData::set_code(const String &p_code) { |
| 1679 | ParticlesStorage *particles_storage = ParticlesStorage::get_singleton(); |
| 1680 | //compile |
| 1681 | |
| 1682 | code = p_code; |
| 1683 | valid = false; |
| 1684 | ubo_size = 0; |
| 1685 | uniforms.clear(); |
| 1686 | uses_collision = false; |
| 1687 | |
| 1688 | if (code.is_empty()) { |
| 1689 | return; //just invalid, but no error |
| 1690 | } |
| 1691 | |
| 1692 | ShaderCompiler::GeneratedCode gen_code; |
| 1693 | ShaderCompiler::IdentifierActions actions; |
| 1694 | actions.entry_point_stages["start"] = ShaderCompiler::STAGE_COMPUTE; |
| 1695 | actions.entry_point_stages["process"] = ShaderCompiler::STAGE_COMPUTE; |
| 1696 | |
| 1697 | /* |
| 1698 | uses_time = false; |
| 1699 | |
| 1700 | actions.render_mode_flags["use_half_res_pass"] = &uses_half_res; |
| 1701 | actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res; |
| 1702 | |
| 1703 | actions.usage_flag_pointers["TIME"] = &uses_time; |
| 1704 | */ |
| 1705 | |
| 1706 | actions.usage_flag_pointers["COLLIDED"] = &uses_collision; |
| 1707 | |
| 1708 | userdata_count = 0; |
| 1709 | for (uint32_t i = 0; i < ParticlesShader::MAX_USERDATAS; i++) { |
| 1710 | userdatas_used[i] = false; |
| 1711 | actions.usage_flag_pointers["USERDATA" + itos(i + 1)] = &userdatas_used[i]; |
| 1712 | } |
| 1713 | |
| 1714 | actions.uniforms = &uniforms; |
| 1715 | |
| 1716 | Error err = particles_storage->particles_shader.compiler.compile(RS::SHADER_PARTICLES, code, &actions, path, gen_code); |
| 1717 | ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed."); |
| 1718 | |
| 1719 | if (version.is_null()) { |
| 1720 | version = particles_storage->particles_shader.shader.version_create(); |
| 1721 | } else { |
| 1722 | pipeline.free(); |
| 1723 | } |
| 1724 | |
| 1725 | for (uint32_t i = 0; i < ParticlesShader::MAX_USERDATAS; i++) { |
| 1726 | if (userdatas_used[i]) { |
| 1727 | userdata_count++; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | 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); |
| 1732 | ERR_FAIL_COND(!particles_storage->particles_shader.shader.version_is_valid(version)); |
| 1733 | |
| 1734 | ubo_size = gen_code.uniform_total_size; |
| 1735 | ubo_offsets = gen_code.uniform_offsets; |
| 1736 | texture_uniforms = gen_code.texture_uniforms; |
| 1737 | |
| 1738 | //update pipelines |
| 1739 | |
| 1740 | pipeline.create_compute_pipeline(particles_storage->particles_shader.shader.version_get_shader(version, 0)); |
| 1741 | |
| 1742 | valid = true; |
| 1743 | } |
| 1744 | |
| 1745 | bool ParticlesStorage::ParticlesShaderData::is_animated() const { |
| 1746 | return false; |
| 1747 | } |
| 1748 | |
| 1749 | bool ParticlesStorage::ParticlesShaderData::casts_shadows() const { |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | RS::ShaderNativeSourceCode ParticlesStorage::ParticlesShaderData::get_native_source_code() const { |
| 1754 | return ParticlesStorage::get_singleton()->particles_shader.shader.version_get_native_source_code(version); |
| 1755 | } |
| 1756 | |
| 1757 | Pair<ShaderRD *, RID> ParticlesStorage::ParticlesShaderData::get_native_shader_and_version() const { |
| 1758 | return { &ParticlesStorage::get_singleton()->particles_shader.shader, version }; |
| 1759 | } |
| 1760 | |
| 1761 | ParticlesStorage::ParticlesShaderData::~ParticlesShaderData() { |
| 1762 | pipeline.free(); |
| 1763 | |
| 1764 | if (version.is_valid()) { |
| 1765 | ParticlesStorage::get_singleton()->particles_shader.shader.version_free(version); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | MaterialStorage::ShaderData *ParticlesStorage::_create_particles_shader_func() { |
| 1770 | ParticlesShaderData *shader_data = memnew(ParticlesShaderData); |
| 1771 | return shader_data; |
| 1772 | } |
| 1773 | |
| 1774 | bool ParticlesStorage::ParticleProcessMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) { |
| 1775 | 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, false); |
| 1776 | } |
| 1777 | |
| 1778 | ParticlesStorage::ParticleProcessMaterialData::~ParticleProcessMaterialData() { |
| 1779 | free_parameters_uniform_set(uniform_set); |
| 1780 | } |
| 1781 | |
| 1782 | MaterialStorage::MaterialData *ParticlesStorage::_create_particles_material_func(ParticlesShaderData *p_shader) { |
| 1783 | ParticleProcessMaterialData *material_data = memnew(ParticleProcessMaterialData); |
| 1784 | material_data->shader_data = p_shader; |
| 1785 | //update will happen later anyway so do nothing. |
| 1786 | return material_data; |
| 1787 | } |
| 1788 | //////// |
| 1789 | |
| 1790 | /* PARTICLES COLLISION API */ |
| 1791 | |
| 1792 | RID ParticlesStorage::particles_collision_allocate() { |
| 1793 | return particles_collision_owner.allocate_rid(); |
| 1794 | } |
| 1795 | void ParticlesStorage::particles_collision_initialize(RID p_rid) { |
| 1796 | particles_collision_owner.initialize_rid(p_rid, ParticlesCollision()); |
| 1797 | } |
| 1798 | |
| 1799 | void ParticlesStorage::particles_collision_free(RID p_rid) { |
| 1800 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_rid); |
| 1801 | |
| 1802 | if (particles_collision->heightfield_texture.is_valid()) { |
| 1803 | RD::get_singleton()->free_rid(particles_collision->heightfield_texture); |
| 1804 | } |
| 1805 | particles_collision->dependency.deleted_notify(p_rid); |
| 1806 | particles_collision_owner.free(p_rid); |
| 1807 | } |
| 1808 | |
| 1809 | RID ParticlesStorage::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const { |
| 1810 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1811 | ERR_FAIL_NULL_V(particles_collision, RID()); |
| 1812 | ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, RID()); |
| 1813 | |
| 1814 | if (particles_collision->heightfield_texture == RID()) { |
| 1815 | //create |
| 1816 | const int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 }; |
| 1817 | Size2i size; |
| 1818 | if (particles_collision->extents.x > particles_collision->extents.z) { |
| 1819 | size.x = resolutions[particles_collision->heightfield_resolution]; |
| 1820 | size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x); |
| 1821 | } else { |
| 1822 | size.y = resolutions[particles_collision->heightfield_resolution]; |
| 1823 | size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y); |
| 1824 | } |
| 1825 | |
| 1826 | RD::TextureFormat tf; |
| 1827 | tf.format = RD::DATA_FORMAT_D32_SFLOAT; |
| 1828 | tf.width = size.x; |
| 1829 | tf.height = size.y; |
| 1830 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 1831 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 1832 | |
| 1833 | particles_collision->heightfield_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 1834 | |
| 1835 | Vector<RID> fb_tex; |
| 1836 | fb_tex.push_back(particles_collision->heightfield_texture); |
| 1837 | particles_collision->heightfield_fb = RD::get_singleton()->framebuffer_create(fb_tex); |
| 1838 | particles_collision->heightfield_fb_size = size; |
| 1839 | } |
| 1840 | |
| 1841 | return particles_collision->heightfield_fb; |
| 1842 | } |
| 1843 | |
| 1844 | void ParticlesStorage::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) { |
| 1845 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1846 | ERR_FAIL_NULL(particles_collision); |
| 1847 | |
| 1848 | if (p_type == particles_collision->type) { |
| 1849 | return; |
| 1850 | } |
| 1851 | |
| 1852 | if (particles_collision->heightfield_texture.is_valid()) { |
| 1853 | RD::get_singleton()->free_rid(particles_collision->heightfield_texture); |
| 1854 | particles_collision->heightfield_texture = RID(); |
| 1855 | } |
| 1856 | particles_collision->type = p_type; |
| 1857 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1858 | } |
| 1859 | |
| 1860 | void ParticlesStorage::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) { |
| 1861 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1862 | ERR_FAIL_NULL(particles_collision); |
| 1863 | particles_collision->cull_mask = p_cull_mask; |
| 1864 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_CULL_MASK); |
| 1865 | } |
| 1866 | |
| 1867 | uint32_t ParticlesStorage::particles_collision_get_cull_mask(RID p_particles_collision) const { |
| 1868 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1869 | ERR_FAIL_NULL_V(particles_collision, 0); |
| 1870 | return particles_collision->cull_mask; |
| 1871 | } |
| 1872 | |
| 1873 | uint32_t ParticlesStorage::particles_collision_get_height_field_mask(RID p_particles_collision) const { |
| 1874 | const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1875 | ERR_FAIL_NULL_V(particles_collision, false); |
| 1876 | return particles_collision->heightfield_mask; |
| 1877 | } |
| 1878 | |
| 1879 | void ParticlesStorage::particles_collision_set_height_field_mask(RID p_particles_collision, uint32_t p_heightfield_mask) { |
| 1880 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1881 | ERR_FAIL_NULL(particles_collision); |
| 1882 | particles_collision->heightfield_mask = p_heightfield_mask; |
| 1883 | } |
| 1884 | |
| 1885 | void ParticlesStorage::particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) { |
| 1886 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1887 | ERR_FAIL_NULL(particles_collision); |
| 1888 | |
| 1889 | particles_collision->radius = p_radius; |
| 1890 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1891 | } |
| 1892 | |
| 1893 | void ParticlesStorage::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) { |
| 1894 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1895 | ERR_FAIL_NULL(particles_collision); |
| 1896 | |
| 1897 | particles_collision->extents = p_extents; |
| 1898 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1899 | } |
| 1900 | |
| 1901 | void ParticlesStorage::particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) { |
| 1902 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1903 | ERR_FAIL_NULL(particles_collision); |
| 1904 | |
| 1905 | particles_collision->attractor_strength = p_strength; |
| 1906 | } |
| 1907 | |
| 1908 | void ParticlesStorage::particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) { |
| 1909 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1910 | ERR_FAIL_NULL(particles_collision); |
| 1911 | |
| 1912 | particles_collision->attractor_directionality = p_directionality; |
| 1913 | } |
| 1914 | |
| 1915 | void ParticlesStorage::particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) { |
| 1916 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1917 | ERR_FAIL_NULL(particles_collision); |
| 1918 | |
| 1919 | particles_collision->attractor_attenuation = p_curve; |
| 1920 | } |
| 1921 | |
| 1922 | void ParticlesStorage::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) { |
| 1923 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1924 | ERR_FAIL_NULL(particles_collision); |
| 1925 | |
| 1926 | particles_collision->field_texture = p_texture; |
| 1927 | } |
| 1928 | |
| 1929 | void ParticlesStorage::particles_collision_height_field_update(RID p_particles_collision) { |
| 1930 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1931 | ERR_FAIL_NULL(particles_collision); |
| 1932 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1933 | } |
| 1934 | |
| 1935 | void ParticlesStorage::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) { |
| 1936 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1937 | ERR_FAIL_NULL(particles_collision); |
| 1938 | ERR_FAIL_INDEX(p_resolution, RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX); |
| 1939 | |
| 1940 | if (particles_collision->heightfield_resolution == p_resolution) { |
| 1941 | return; |
| 1942 | } |
| 1943 | |
| 1944 | particles_collision->heightfield_resolution = p_resolution; |
| 1945 | |
| 1946 | if (particles_collision->heightfield_texture.is_valid()) { |
| 1947 | RD::get_singleton()->free_rid(particles_collision->heightfield_texture); |
| 1948 | particles_collision->heightfield_texture = RID(); |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | AABB ParticlesStorage::particles_collision_get_aabb(RID p_particles_collision) const { |
| 1953 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1954 | ERR_FAIL_NULL_V(particles_collision, AABB()); |
| 1955 | |
| 1956 | switch (particles_collision->type) { |
| 1957 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: |
| 1958 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: { |
| 1959 | AABB aabb; |
| 1960 | aabb.position = -Vector3(1, 1, 1) * particles_collision->radius; |
| 1961 | aabb.size = Vector3(2, 2, 2) * particles_collision->radius; |
| 1962 | return aabb; |
| 1963 | } |
| 1964 | default: { |
| 1965 | AABB aabb; |
| 1966 | aabb.position = -particles_collision->extents; |
| 1967 | aabb.size = particles_collision->extents * 2; |
| 1968 | return aabb; |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | Vector3 ParticlesStorage::particles_collision_get_extents(RID p_particles_collision) const { |
| 1974 | const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1975 | ERR_FAIL_NULL_V(particles_collision, Vector3()); |
| 1976 | return particles_collision->extents; |
| 1977 | } |
| 1978 | |
| 1979 | bool ParticlesStorage::particles_collision_is_heightfield(RID p_particles_collision) const { |
| 1980 | const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1981 | ERR_FAIL_NULL_V(particles_collision, false); |
| 1982 | return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE; |
| 1983 | } |
| 1984 | |
| 1985 | Dependency *ParticlesStorage::particles_collision_get_dependency(RID p_particles_collision) const { |
| 1986 | ParticlesCollision *pc = particles_collision_owner.get_or_null(p_particles_collision); |
| 1987 | ERR_FAIL_NULL_V(pc, nullptr); |
| 1988 | |
| 1989 | return &pc->dependency; |
| 1990 | } |
| 1991 | |
| 1992 | /* Particles collision instance */ |
| 1993 | |
| 1994 | RID ParticlesStorage::particles_collision_instance_create(RID p_collision) { |
| 1995 | ParticlesCollisionInstance pci; |
| 1996 | pci.collision = p_collision; |
| 1997 | return particles_collision_instance_owner.make_rid(pci); |
| 1998 | } |
| 1999 | |
| 2000 | void ParticlesStorage::particles_collision_instance_free(RID p_rid) { |
| 2001 | particles_collision_instance_owner.free(p_rid); |
| 2002 | } |
| 2003 | |
| 2004 | void ParticlesStorage::particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) { |
| 2005 | ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance); |
| 2006 | ERR_FAIL_NULL(pci); |
| 2007 | pci->transform = p_transform; |
| 2008 | } |
| 2009 | |
| 2010 | void ParticlesStorage::particles_collision_instance_set_active(RID p_collision_instance, bool p_active) { |
| 2011 | ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance); |
| 2012 | ERR_FAIL_NULL(pci); |
| 2013 | pci->active = p_active; |
| 2014 | } |