Skip to content

Commit 1a6d3c2

Browse files
committed
Merge pull request #110002 from BastiaanOlij/fix_openxr_viewport_texture_compatibility
OpenXR: Fix ViewportTextures not displaying correct texture (OpenGL)
2 parents 94b5658 + a38256c commit 1a6d3c2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

drivers/gles3/storage/texture_storage.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
10671088
void 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);

drivers/gles3/storage/texture_storage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ struct Texture {
151151
bool is_render_target = false;
152152

153153
RID proxy_to;
154-
Vector<RID> proxies;
154+
LocalVector<RID> proxies;
155155

156156
String path;
157157
int width = 0;
@@ -525,6 +525,7 @@ class TextureStorage : public RendererTextureStorage {
525525
virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override;
526526
virtual void texture_external_update(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) override;
527527
virtual void texture_proxy_update(RID p_proxy, RID p_base) override;
528+
void texture_remap_proxies(RID p_from_texture, RID p_to_texture);
528529

529530
Ref<Image> texture_2d_placeholder;
530531
Vector<Ref<Image>> texture_2d_array_placeholder;

0 commit comments

Comments
 (0)