Skip to content

Commit

Permalink
Merge pull request #82431 from bitsawer/fix_gpuparticles_free
Browse files Browse the repository at this point in the history
Fix errors when freeing GPUParticles
  • Loading branch information
akien-mga authored Oct 11, 2023
2 parents f2f6f99 + 898d1a2 commit b137180
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
19 changes: 11 additions & 8 deletions drivers/gles3/storage/particles_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ RID ParticlesStorage::particles_allocate() {
}

void ParticlesStorage::particles_initialize(RID p_rid) {
particles_owner.initialize_rid(p_rid, Particles());
particles_owner.initialize_rid(p_rid);
}

void ParticlesStorage::particles_free(RID p_rid) {
update_particles();
Particles *particles = particles_owner.get_or_null(p_rid);

particles->dependency.deleted_notify(p_rid);
particles->update_list.remove_from_list();

_particles_free_data(particles);
particles_owner.free(p_rid);
}
Expand Down Expand Up @@ -362,8 +364,10 @@ void ParticlesStorage::particles_request_process(RID p_particles) {

if (!particles->dirty) {
particles->dirty = true;
particles->update_list = particle_update_list;
particle_update_list = particles;

if (!particles->update_list.in_list()) {
particle_update_list.add(&particles->update_list);
}
}
}

Expand Down Expand Up @@ -1003,13 +1007,12 @@ void ParticlesStorage::update_particles() {
glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_GLOBALS_UNIFORM_LOCATION, global_buffer);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

while (particle_update_list) {
while (particle_update_list.first()) {
// Use transform feedback to process particles.

Particles *particles = particle_update_list;
Particles *particles = particle_update_list.first()->self();

particle_update_list = particles->update_list;
particles->update_list = nullptr;
particles->update_list.remove_from_list();
particles->dirty = false;

_particles_update_buffers(particles);
Expand Down
7 changes: 4 additions & 3 deletions drivers/gles3/storage/particles_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ParticlesStorage : public RendererParticlesStorage {
uint32_t userdata_count = 0;

bool dirty = false;
Particles *update_list = nullptr;
SelfList<Particles> update_list;

double phase = 0.0;
double prev_phase = 0.0;
Expand Down Expand Up @@ -242,7 +242,8 @@ class ParticlesStorage : public RendererParticlesStorage {
double trail_length = 1.0;
bool trails_enabled = false;

Particles() {
Particles() :
update_list(this) {
}
};

Expand All @@ -264,7 +265,7 @@ class ParticlesStorage : public RendererParticlesStorage {
RID copy_shader_version;
} particles_shader;

Particles *particle_update_list = nullptr;
SelfList<Particles>::List particle_update_list;

mutable RID_Owner<Particles, true> particles_owner;

Expand Down
20 changes: 11 additions & 9 deletions servers/rendering/renderer_rd/storage_rd/particles_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ RID ParticlesStorage::particles_allocate() {
}

void ParticlesStorage::particles_initialize(RID p_rid) {
particles_owner.initialize_rid(p_rid, Particles());
particles_owner.initialize_rid(p_rid);
}

void ParticlesStorage::particles_free(RID p_rid) {
update_particles();
Particles *particles = particles_owner.get_or_null(p_rid);

particles->dependency.deleted_notify(p_rid);
particles->update_list.remove_from_list();

_particles_free_data(particles);
particles_owner.free(p_rid);
}
Expand Down Expand Up @@ -587,8 +589,10 @@ void ParticlesStorage::particles_request_process(RID p_particles) {

if (!particles->dirty) {
particles->dirty = true;
particles->update_list = particle_update_list;
particle_update_list = particles;

if (!particles->update_list.in_list()) {
particle_update_list.add(&particles->update_list);
}
}
}

Expand Down Expand Up @@ -1373,14 +1377,12 @@ void ParticlesStorage::_particles_update_buffers(Particles *particles) {
void ParticlesStorage::update_particles() {
uint32_t frame = RSG::rasterizer->get_frame_number();
bool uses_motion_vectors = RSG::viewport->get_num_viewports_with_motion_vectors() > 0;
while (particle_update_list) {
while (particle_update_list.first()) {
//use transform feedback to process particles

Particles *particles = particle_update_list;
Particles *particles = particle_update_list.first()->self();

//take and remove
particle_update_list = particles->update_list;
particles->update_list = nullptr;
particles->update_list.remove_from_list();
particles->dirty = false;

_particles_update_buffers(particles);
Expand Down
7 changes: 4 additions & 3 deletions servers/rendering/renderer_rd/storage_rd/particles_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class ParticlesStorage : public RendererParticlesStorage {
RID particles_sort_uniform_set;

bool dirty = false;
Particles *update_list = nullptr;
SelfList<Particles> update_list;

RID sub_emitter;

Expand Down Expand Up @@ -256,7 +256,8 @@ class ParticlesStorage : public RendererParticlesStorage {
LocalVector<ParticlesFrameParams> frame_history;
LocalVector<ParticlesFrameParams> trail_params;

Particles() {
Particles() :
update_list(this) {
}
};

Expand Down Expand Up @@ -328,7 +329,7 @@ class ParticlesStorage : public RendererParticlesStorage {

} particles_shader;

Particles *particle_update_list = nullptr;
SelfList<Particles>::List particle_update_list;

mutable RID_Owner<Particles, true> particles_owner;

Expand Down

0 comments on commit b137180

Please sign in to comment.