Skip to content

Commit

Permalink
Merge pull request #77326 from BastiaanOlij/fix_material_dependencies
Browse files Browse the repository at this point in the history
Properly clear material slots on mesh instance when material is freed
  • Loading branch information
akien-mga committed May 22, 2023
2 parents 061c6f2 + 1a128fc commit c3c1909
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 0 additions & 1 deletion servers/rendering/renderer_scene_cull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "core/object/worker_thread_pool.h"
#include "core/os/os.h"
#include "rendering_server_default.h"
#include "rendering_server_globals.h"

#include <new>

Expand Down
24 changes: 24 additions & 0 deletions servers/rendering/renderer_scene_cull.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "servers/rendering/renderer_scene_occlusion_cull.h"
#include "servers/rendering/renderer_scene_render.h"
#include "servers/rendering/rendering_method.h"
#include "servers/rendering/rendering_server_globals.h"
#include "servers/rendering/storage/utilities.h"
#include "servers/xr/xr_interface.h"

Expand Down Expand Up @@ -516,6 +517,29 @@ class RendererSceneCull : public RenderingMethod {
} else if (p_dependency == instance->skeleton) {
singleton->instance_attach_skeleton(instance->self, RID());
} else {
// It's possible the same material is used in multiple slots,
// so we check whether we need to clear them all.
if (p_dependency == instance->material_override) {
singleton->instance_geometry_set_material_override(instance->self, RID());
}
if (p_dependency == instance->material_overlay) {
singleton->instance_geometry_set_material_overlay(instance->self, RID());
}
for (int i = 0; i < instance->materials.size(); i++) {
if (p_dependency == instance->materials[i]) {
singleton->instance_set_surface_override_material(instance->self, i, RID());
}
}
if (instance->base_type == RS::INSTANCE_PARTICLES) {
RID particle_material = RSG::particles_storage->particles_get_process_material(instance->base);
if (p_dependency == particle_material) {
RSG::particles_storage->particles_set_process_material(instance->base, RID());
}
}

// Even if no change is made we still need to call `_instance_queue_update`.
// This dependency could also be a result of the freed material being used
// by the mesh this mesh instance uses.
singleton->_instance_queue_update(instance, false, true);
}
}
Expand Down

0 comments on commit c3c1909

Please sign in to comment.