| 1 | /**************************************************************************/ |
| 2 | /* particles_storage.h */ |
| 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 | #ifndef PARTICLES_STORAGE_GLES3_H |
| 32 | #define PARTICLES_STORAGE_GLES3_H |
| 33 | |
| 34 | #ifdef GLES3_ENABLED |
| 35 | |
| 36 | #include "core/templates/local_vector.h" |
| 37 | #include "core/templates/rid_owner.h" |
| 38 | #include "core/templates/self_list.h" |
| 39 | #include "drivers/gles3/shaders/particles_copy.glsl.gen.h" |
| 40 | #include "servers/rendering/storage/particles_storage.h" |
| 41 | #include "servers/rendering/storage/utilities.h" |
| 42 | |
| 43 | #include "platform_config.h" |
| 44 | #ifndef OPENGL_INCLUDE_H |
| 45 | #include <GLES3/gl3.h> |
| 46 | #else |
| 47 | #include OPENGL_INCLUDE_H |
| 48 | #endif |
| 49 | |
| 50 | namespace GLES3 { |
| 51 | |
| 52 | enum ParticlesUniformLocation { |
| 53 | PARTICLES_FRAME_UNIFORM_LOCATION, |
| 54 | PARTICLES_GLOBALS_UNIFORM_LOCATION, |
| 55 | PARTICLES_MATERIAL_UNIFORM_LOCATION, |
| 56 | }; |
| 57 | |
| 58 | class ParticlesStorage : public RendererParticlesStorage { |
| 59 | private: |
| 60 | static ParticlesStorage *singleton; |
| 61 | |
| 62 | /* PARTICLES */ |
| 63 | |
| 64 | struct ParticleInstanceData3D { |
| 65 | float xform[12]; |
| 66 | float color[2]; // Color and custom are packed together into one vec4; |
| 67 | float custom[2]; |
| 68 | }; |
| 69 | |
| 70 | struct ParticleInstanceData2D { |
| 71 | float xform[8]; |
| 72 | float color[2]; // Color and custom are packed together into one vec4; |
| 73 | float custom[2]; |
| 74 | }; |
| 75 | |
| 76 | struct ParticlesViewSort { |
| 77 | Vector3 z_dir; |
| 78 | bool operator()(const ParticleInstanceData3D &p_a, const ParticleInstanceData3D &p_b) const { |
| 79 | return z_dir.dot(Vector3(p_a.xform[3], p_a.xform[7], p_a.xform[11])) < z_dir.dot(Vector3(p_b.xform[3], p_b.xform[7], p_b.xform[11])); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | struct ParticlesFrameParams { |
| 84 | enum { |
| 85 | MAX_ATTRACTORS = 32, |
| 86 | MAX_COLLIDERS = 32, |
| 87 | MAX_3D_TEXTURES = 0 // GLES3 renderer doesn't support using 3D textures for flow field or collisions. |
| 88 | }; |
| 89 | |
| 90 | enum AttractorType { |
| 91 | ATTRACTOR_TYPE_SPHERE, |
| 92 | ATTRACTOR_TYPE_BOX, |
| 93 | ATTRACTOR_TYPE_VECTOR_FIELD, |
| 94 | }; |
| 95 | |
| 96 | struct Attractor { |
| 97 | float transform[16]; |
| 98 | float extents[4]; // Extents or radius. w-channel is padding. |
| 99 | |
| 100 | uint32_t type; |
| 101 | float strength; |
| 102 | float attenuation; |
| 103 | float directionality; |
| 104 | }; |
| 105 | |
| 106 | enum CollisionType { |
| 107 | COLLISION_TYPE_SPHERE, |
| 108 | COLLISION_TYPE_BOX, |
| 109 | COLLISION_TYPE_SDF, |
| 110 | COLLISION_TYPE_HEIGHT_FIELD, |
| 111 | COLLISION_TYPE_2D_SDF, |
| 112 | |
| 113 | }; |
| 114 | |
| 115 | struct Collider { |
| 116 | float transform[16]; |
| 117 | float extents[4]; // Extents or radius. w-channel is padding. |
| 118 | |
| 119 | uint32_t type; |
| 120 | float scale; |
| 121 | float pad0; |
| 122 | float pad1; |
| 123 | }; |
| 124 | |
| 125 | uint32_t emitting; |
| 126 | uint32_t cycle; |
| 127 | float system_phase; |
| 128 | float prev_system_phase; |
| 129 | |
| 130 | float explosiveness; |
| 131 | float randomness; |
| 132 | float time; |
| 133 | float delta; |
| 134 | |
| 135 | float particle_size; |
| 136 | float pad0; |
| 137 | float pad1; |
| 138 | float pad2; |
| 139 | |
| 140 | uint32_t random_seed; |
| 141 | uint32_t attractor_count; |
| 142 | uint32_t collider_count; |
| 143 | uint32_t frame; |
| 144 | |
| 145 | float emission_transform[16]; |
| 146 | |
| 147 | Attractor attractors[MAX_ATTRACTORS]; |
| 148 | Collider colliders[MAX_COLLIDERS]; |
| 149 | }; |
| 150 | |
| 151 | struct Particles { |
| 152 | RS::ParticlesMode mode = RS::PARTICLES_MODE_3D; |
| 153 | bool inactive = true; |
| 154 | double inactive_time = 0.0; |
| 155 | bool emitting = false; |
| 156 | bool one_shot = false; |
| 157 | int amount = 0; |
| 158 | double lifetime = 1.0; |
| 159 | double pre_process_time = 0.0; |
| 160 | real_t explosiveness = 0.0; |
| 161 | real_t randomness = 0.0; |
| 162 | bool restart_request = false; |
| 163 | AABB custom_aabb = AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)); |
| 164 | bool use_local_coords = false; |
| 165 | bool has_collision_cache = false; |
| 166 | |
| 167 | bool has_sdf_collision = false; |
| 168 | Transform2D sdf_collision_transform; |
| 169 | Rect2 sdf_collision_to_screen; |
| 170 | GLuint sdf_collision_texture = 0; |
| 171 | |
| 172 | RID process_material; |
| 173 | uint32_t frame_counter = 0; |
| 174 | RS::ParticlesTransformAlign transform_align = RS::PARTICLES_TRANSFORM_ALIGN_DISABLED; |
| 175 | |
| 176 | RS::ParticlesDrawOrder draw_order = RS::PARTICLES_DRAW_ORDER_INDEX; |
| 177 | |
| 178 | Vector<RID> draw_passes; |
| 179 | |
| 180 | GLuint frame_params_ubo = 0; |
| 181 | |
| 182 | // We may process particles multiple times each frame (if they have a fixed FPS higher than the game FPS). |
| 183 | // Unfortunately, this means we can't just use a round-robin system of 3 buffers. |
| 184 | // To ensure the sort buffer is accurate, we copy the last frame instance buffer just before processing. |
| 185 | |
| 186 | // Transform Feedback buffer and VAO for rendering. |
| 187 | // Each frame we render to this one. |
| 188 | GLuint front_vertex_array = 0; // Binds process buffer. Used for processing. |
| 189 | GLuint front_process_buffer = 0; // Transform + color + custom data + userdata + velocity + flags. Only needed for processing. |
| 190 | GLuint front_instance_buffer = 0; // Transform + color + custom data. In packed format needed for rendering. |
| 191 | |
| 192 | // VAO for transform feedback, contains last frame's data. |
| 193 | // Read from this one for particles process and then copy to last frame buffer. |
| 194 | GLuint back_vertex_array = 0; // Binds process buffer. Used for processing. |
| 195 | GLuint back_process_buffer = 0; // Transform + color + custom data + userdata + velocity + flags. Only needed for processing. |
| 196 | GLuint back_instance_buffer = 0; // Transform + color + custom data. In packed format needed for rendering. |
| 197 | |
| 198 | uint32_t instance_buffer_size_cache = 0; |
| 199 | uint32_t instance_buffer_stride_cache = 0; |
| 200 | uint32_t num_attrib_arrays_cache = 0; |
| 201 | uint32_t process_buffer_stride_cache = 0; |
| 202 | |
| 203 | // Only ever copied to, holds last frame's instance data, then swaps with sort_buffer. |
| 204 | GLuint last_frame_buffer = 0; |
| 205 | bool last_frame_buffer_filled = false; |
| 206 | float last_frame_phase = 0.0; |
| 207 | |
| 208 | // The frame-before-last's instance buffer. |
| 209 | // Use this to copy data back for sorting or computing AABB. |
| 210 | GLuint sort_buffer = 0; |
| 211 | bool sort_buffer_filled = false; |
| 212 | float sort_buffer_phase = 0.0; |
| 213 | |
| 214 | uint32_t userdata_count = 0; |
| 215 | |
| 216 | bool dirty = false; |
| 217 | Particles *update_list = nullptr; |
| 218 | |
| 219 | double phase = 0.0; |
| 220 | double prev_phase = 0.0; |
| 221 | uint64_t prev_ticks = 0; |
| 222 | uint32_t random_seed = 0; |
| 223 | |
| 224 | uint32_t cycle_number = 0; |
| 225 | |
| 226 | double speed_scale = 1.0; |
| 227 | |
| 228 | int fixed_fps = 30; |
| 229 | bool interpolate = true; |
| 230 | bool fractional_delta = false; |
| 231 | double frame_remainder = 0; |
| 232 | real_t collision_base_size = 0.01; |
| 233 | |
| 234 | bool clear = true; |
| 235 | |
| 236 | Transform3D emission_transform; |
| 237 | |
| 238 | HashSet<RID> collisions; |
| 239 | |
| 240 | Dependency dependency; |
| 241 | |
| 242 | double trail_length = 1.0; |
| 243 | bool trails_enabled = false; |
| 244 | |
| 245 | Particles() { |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | void _particles_process(Particles *p_particles, double p_delta); |
| 250 | void _particles_free_data(Particles *particles); |
| 251 | void _particles_update_buffers(Particles *particles); |
| 252 | void _particles_allocate_history_buffers(Particles *particles); |
| 253 | void _particles_update_instance_buffer(Particles *particles, const Vector3 &p_axis, const Vector3 &p_up_axis); |
| 254 | |
| 255 | template <typename T> |
| 256 | void _particles_reverse_lifetime_sort(Particles *particles); |
| 257 | |
| 258 | struct ParticlesShader { |
| 259 | RID default_shader; |
| 260 | RID default_material; |
| 261 | RID default_shader_version; |
| 262 | |
| 263 | ParticlesCopyShaderGLES3 copy_shader; |
| 264 | RID copy_shader_version; |
| 265 | } particles_shader; |
| 266 | |
| 267 | Particles *particle_update_list = nullptr; |
| 268 | |
| 269 | mutable RID_Owner<Particles, true> particles_owner; |
| 270 | |
| 271 | /* Particles Collision */ |
| 272 | |
| 273 | struct ParticlesCollision { |
| 274 | RS::ParticlesCollisionType type = RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT; |
| 275 | uint32_t cull_mask = 0xFFFFFFFF; |
| 276 | float radius = 1.0; |
| 277 | Vector3 extents = Vector3(1, 1, 1); |
| 278 | float attractor_strength = 1.0; |
| 279 | float attractor_attenuation = 1.0; |
| 280 | float attractor_directionality = 0.0; |
| 281 | GLuint field_texture = 0; |
| 282 | GLuint heightfield_texture = 0; |
| 283 | GLuint heightfield_fb = 0; |
| 284 | Size2i heightfield_fb_size; |
| 285 | |
| 286 | RS::ParticlesCollisionHeightfieldResolution heightfield_resolution = RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_1024; |
| 287 | |
| 288 | Dependency dependency; |
| 289 | }; |
| 290 | |
| 291 | struct ParticlesCollisionInstance { |
| 292 | RID collision; |
| 293 | Transform3D transform; |
| 294 | bool active = false; |
| 295 | }; |
| 296 | |
| 297 | mutable RID_Owner<ParticlesCollision, true> particles_collision_owner; |
| 298 | |
| 299 | mutable RID_Owner<ParticlesCollisionInstance> particles_collision_instance_owner; |
| 300 | |
| 301 | public: |
| 302 | static ParticlesStorage *get_singleton(); |
| 303 | |
| 304 | ParticlesStorage(); |
| 305 | virtual ~ParticlesStorage(); |
| 306 | |
| 307 | bool free(RID p_rid); |
| 308 | |
| 309 | /* PARTICLES */ |
| 310 | |
| 311 | bool owns_particles(RID p_rid) { return particles_owner.owns(p_rid); } |
| 312 | |
| 313 | virtual RID particles_allocate() override; |
| 314 | virtual void particles_initialize(RID p_rid) override; |
| 315 | virtual void particles_free(RID p_rid) override; |
| 316 | |
| 317 | virtual void particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) override; |
| 318 | virtual void particles_emit(RID p_particles, const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) override; |
| 319 | virtual void particles_set_emitting(RID p_particles, bool p_emitting) override; |
| 320 | virtual void particles_set_amount(RID p_particles, int p_amount) override; |
| 321 | virtual void particles_set_lifetime(RID p_particles, double p_lifetime) override; |
| 322 | virtual void particles_set_one_shot(RID p_particles, bool p_one_shot) override; |
| 323 | virtual void particles_set_pre_process_time(RID p_particles, double p_time) override; |
| 324 | virtual void particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) override; |
| 325 | virtual void particles_set_randomness_ratio(RID p_particles, real_t p_ratio) override; |
| 326 | virtual void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) override; |
| 327 | virtual void particles_set_speed_scale(RID p_particles, double p_scale) override; |
| 328 | virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable) override; |
| 329 | virtual void particles_set_process_material(RID p_particles, RID p_material) override; |
| 330 | virtual RID particles_get_process_material(RID p_particles) const override; |
| 331 | virtual void particles_set_fixed_fps(RID p_particles, int p_fps) override; |
| 332 | virtual void particles_set_interpolate(RID p_particles, bool p_enable) override; |
| 333 | virtual void particles_set_fractional_delta(RID p_particles, bool p_enable) override; |
| 334 | virtual void particles_set_subemitter(RID p_particles, RID p_subemitter_particles) override; |
| 335 | virtual void particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) override; |
| 336 | virtual void particles_set_collision_base_size(RID p_particles, real_t p_size) override; |
| 337 | |
| 338 | virtual void particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) override; |
| 339 | |
| 340 | virtual void particles_set_trails(RID p_particles, bool p_enable, double p_length) override; |
| 341 | virtual void particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) override; |
| 342 | |
| 343 | virtual void particles_restart(RID p_particles) override; |
| 344 | |
| 345 | virtual void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) override; |
| 346 | |
| 347 | virtual void particles_set_draw_passes(RID p_particles, int p_count) override; |
| 348 | virtual void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) override; |
| 349 | |
| 350 | virtual void particles_request_process(RID p_particles) override; |
| 351 | virtual AABB particles_get_current_aabb(RID p_particles) override; |
| 352 | virtual AABB particles_get_aabb(RID p_particles) const override; |
| 353 | |
| 354 | virtual void particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) override; |
| 355 | |
| 356 | virtual bool particles_get_emitting(RID p_particles) override; |
| 357 | virtual int particles_get_draw_passes(RID p_particles) const override; |
| 358 | virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const override; |
| 359 | |
| 360 | virtual void particles_add_collision(RID p_particles, RID p_instance) override; |
| 361 | virtual void particles_remove_collision(RID p_particles, RID p_instance) override; |
| 362 | |
| 363 | void particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, GLuint p_texture); |
| 364 | |
| 365 | virtual void update_particles() override; |
| 366 | virtual bool particles_is_inactive(RID p_particles) const override; |
| 367 | |
| 368 | _FORCE_INLINE_ RS::ParticlesMode particles_get_mode(RID p_particles) { |
| 369 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 370 | ERR_FAIL_NULL_V(particles, RS::PARTICLES_MODE_2D); |
| 371 | return particles->mode; |
| 372 | } |
| 373 | |
| 374 | _FORCE_INLINE_ uint32_t particles_get_amount(RID p_particles) { |
| 375 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 376 | ERR_FAIL_NULL_V(particles, 0); |
| 377 | |
| 378 | return particles->amount; |
| 379 | } |
| 380 | |
| 381 | _FORCE_INLINE_ GLuint particles_get_gl_buffer(RID p_particles) { |
| 382 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 383 | |
| 384 | if ((particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME) && particles->sort_buffer_filled) { |
| 385 | return particles->sort_buffer; |
| 386 | } |
| 387 | return particles->back_instance_buffer; |
| 388 | } |
| 389 | |
| 390 | _FORCE_INLINE_ bool particles_has_collision(RID p_particles) { |
| 391 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 392 | ERR_FAIL_NULL_V(particles, 0); |
| 393 | |
| 394 | return particles->has_collision_cache; |
| 395 | } |
| 396 | |
| 397 | _FORCE_INLINE_ uint32_t particles_is_using_local_coords(RID p_particles) { |
| 398 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 399 | ERR_FAIL_NULL_V(particles, false); |
| 400 | |
| 401 | return particles->use_local_coords; |
| 402 | } |
| 403 | |
| 404 | Dependency *particles_get_dependency(RID p_particles) const; |
| 405 | |
| 406 | /* PARTICLES COLLISION */ |
| 407 | bool owns_particles_collision(RID p_rid) { return particles_collision_owner.owns(p_rid); } |
| 408 | |
| 409 | virtual RID particles_collision_allocate() override; |
| 410 | virtual void particles_collision_initialize(RID p_rid) override; |
| 411 | virtual void particles_collision_free(RID p_rid) override; |
| 412 | |
| 413 | virtual void particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) override; |
| 414 | virtual void particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) override; |
| 415 | virtual void particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) override; |
| 416 | virtual void particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) override; |
| 417 | virtual void particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) override; |
| 418 | virtual void particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) override; |
| 419 | virtual void particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) override; |
| 420 | virtual void particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) override; |
| 421 | virtual void particles_collision_height_field_update(RID p_particles_collision) override; |
| 422 | virtual void particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) override; |
| 423 | virtual AABB particles_collision_get_aabb(RID p_particles_collision) const override; |
| 424 | Vector3 particles_collision_get_extents(RID p_particles_collision) const; |
| 425 | virtual bool particles_collision_is_heightfield(RID p_particles_collision) const override; |
| 426 | GLuint particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const; |
| 427 | |
| 428 | _FORCE_INLINE_ Size2i particles_collision_get_heightfield_size(RID p_particles_collision) const { |
| 429 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 430 | ERR_FAIL_NULL_V(particles_collision, Size2i()); |
| 431 | ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, Size2i()); |
| 432 | |
| 433 | return particles_collision->heightfield_fb_size; |
| 434 | } |
| 435 | |
| 436 | Dependency *particles_collision_get_dependency(RID p_particles) const; |
| 437 | |
| 438 | /* PARTICLES COLLISION INSTANCE*/ |
| 439 | bool owns_particles_collision_instance(RID p_rid) { return particles_collision_instance_owner.owns(p_rid); } |
| 440 | |
| 441 | virtual RID particles_collision_instance_create(RID p_collision) override; |
| 442 | virtual void particles_collision_instance_free(RID p_rid) override; |
| 443 | virtual void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) override; |
| 444 | virtual void particles_collision_instance_set_active(RID p_collision_instance, bool p_active) override; |
| 445 | }; |
| 446 | |
| 447 | } // namespace GLES3 |
| 448 | |
| 449 | #endif // GLES3_ENABLED |
| 450 | |
| 451 | #endif // PARTICLES_STORAGE_GLES3_H |
| 452 | |