@@ -789,7 +789,7 @@ void TextureStorage::texture_free(RID p_texture) {
789789
790790 texture_atlas_remove_texture (p_texture);
791791
792- for (int i = 0 ; i < t->proxies .size (); i++) {
792+ for (uint32_t i = 0 ; i < t->proxies .size (); i++) {
793793 Texture *p = texture_owner.get_or_null (t->proxies [i]);
794794 ERR_CONTINUE (!p);
795795 p->proxy_to = RID ();
@@ -1064,6 +1064,27 @@ void TextureStorage::texture_proxy_update(RID p_texture, RID p_proxy_to) {
10641064 proxy_to->proxies .push_back (p_texture);
10651065}
10661066
1067+ void TextureStorage::texture_remap_proxies (RID p_from_texture, RID p_to_texture) {
1068+ Texture *from_tex = texture_owner.get_or_null (p_from_texture);
1069+ ERR_FAIL_NULL (from_tex);
1070+ ERR_FAIL_COND (from_tex->is_proxy );
1071+ Texture *to_tex = texture_owner.get_or_null (p_to_texture);
1072+ ERR_FAIL_NULL (to_tex);
1073+ ERR_FAIL_COND (to_tex->is_proxy );
1074+
1075+ if (from_tex == to_tex) {
1076+ return ;
1077+ }
1078+
1079+ // Make a local copy, we're about to change the content of the original.
1080+ thread_local LocalVector<RID> proxies = from_tex->proxies ;
1081+
1082+ // Now change them to our new texture.
1083+ for (RID &proxy : proxies) {
1084+ texture_proxy_update (proxy, p_to_texture);
1085+ }
1086+ }
1087+
10671088void TextureStorage::texture_2d_placeholder_initialize (RID p_texture) {
10681089 texture_2d_initialize (p_texture, texture_2d_placeholder);
10691090}
@@ -2553,13 +2574,21 @@ void TextureStorage::render_target_set_override(RID p_render_target, RID p_color
25532574 ERR_FAIL_NULL (rt);
25542575 ERR_FAIL_COND (rt->direct_to_screen );
25552576
2577+ // Remember what our current color output is.
2578+ RID was_color_texture = render_target_get_texture (p_render_target);
2579+
25562580 rt->overridden .velocity = p_velocity_texture;
25572581
25582582 if (rt->overridden .color == p_color_texture && rt->overridden .depth == p_depth_texture) {
25592583 return ;
25602584 }
25612585
25622586 if (p_color_texture.is_null () && p_depth_texture.is_null ()) {
2587+ // Set this back to our default textures.
2588+ if (was_color_texture.is_valid ()) {
2589+ texture_remap_proxies (was_color_texture, rt->texture );
2590+ }
2591+
25632592 _clear_render_target (rt);
25642593 _update_render_target (rt);
25652594 return ;
@@ -2574,6 +2603,12 @@ void TextureStorage::render_target_set_override(RID p_render_target, RID p_color
25742603 rt->overridden .depth_has_stencil = p_depth_texture.is_null ();
25752604 rt->overridden .is_overridden = true ;
25762605
2606+ // Update to our new color output.
2607+ RID new_color_texture = render_target_get_texture (p_render_target);
2608+ if (was_color_texture.is_valid () && new_color_texture.is_valid ()) {
2609+ texture_remap_proxies (was_color_texture, new_color_texture);
2610+ }
2611+
25772612 uint32_t hash_key = hash_murmur3_one_64 (p_color_texture.get_id ());
25782613 hash_key = hash_murmur3_one_64 (p_depth_texture.get_id (), hash_key);
25792614 hash_key = hash_fmix32 (hash_key);
0 commit comments