From ae61044c85f58702e100e2180c3e764008a4d58b Mon Sep 17 00:00:00 2001 From: David Snopek Date: Thu, 24 Jul 2025 12:49:53 -0500 Subject: [PATCH] Don't use `GL_DEPTH_STENCIL_ATTACHMENT` on depth buffer from WebXR --- .../storage/render_scene_buffers_gles3.cpp | 17 +++++++++-------- .../gles3/storage/render_scene_buffers_gles3.h | 2 +- drivers/gles3/storage/texture_storage.cpp | 7 +++++++ drivers/gles3/storage/texture_storage.h | 1 + 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/drivers/gles3/storage/render_scene_buffers_gles3.cpp b/drivers/gles3/storage/render_scene_buffers_gles3.cpp index 0feb650a320..1ad5dc6bcba 100644 --- a/drivers/gles3/storage/render_scene_buffers_gles3.cpp +++ b/drivers/gles3/storage/render_scene_buffers_gles3.cpp @@ -58,19 +58,19 @@ RenderSceneBuffersGLES3::~RenderSceneBuffersGLES3() { free_render_buffer_data(); } -void RenderSceneBuffersGLES3::_rt_attach_textures(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count) { +void RenderSceneBuffersGLES3::_rt_attach_textures(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count, bool p_depth_has_stencil) { if (p_view_count > 1) { if (p_samples > 1) { #if defined(ANDROID_ENABLED) || defined(WEB_ENABLED) glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, p_color, 0, p_samples, 0, p_view_count); - glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, p_depth, 0, p_samples, 0, p_view_count); + glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, p_depth_has_stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, p_depth, 0, p_samples, 0, p_view_count); #else ERR_PRINT_ONCE("Multiview MSAA isn't supported on this platform."); #endif } else { #ifndef IOS_ENABLED glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, p_color, 0, 0, p_view_count); - glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, p_depth, 0, 0, p_view_count); + glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, p_depth_has_stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, p_depth, 0, 0, p_view_count); #else ERR_PRINT_ONCE("Multiview isn't supported on this platform."); #endif @@ -79,13 +79,13 @@ void RenderSceneBuffersGLES3::_rt_attach_textures(GLuint p_color, GLuint p_depth if (p_samples > 1) { #ifdef ANDROID_ENABLED glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_color, 0, p_samples); - glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0, p_samples); + glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, p_depth_has_stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0, p_samples); #else ERR_PRINT_ONCE("MSAA via EXT_multisampled_render_to_texture isn't supported on this platform."); #endif } else { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_color, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, p_depth_has_stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0); } } } @@ -107,7 +107,7 @@ GLuint RenderSceneBuffersGLES3::_rt_get_cached_fbo(GLuint p_color, GLuint p_dept glGenFramebuffers(1, &new_fbo.fbo); glBindFramebuffer(GL_FRAMEBUFFER, new_fbo.fbo); - _rt_attach_textures(p_color, p_depth, p_samples, p_view_count); + _rt_attach_textures(p_color, p_depth, p_samples, p_view_count, true); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) { @@ -383,7 +383,7 @@ void RenderSceneBuffersGLES3::_check_render_buffers() { glGenFramebuffers(1, &msaa3d.fbo); glBindFramebuffer(GL_FRAMEBUFFER, msaa3d.fbo); - _rt_attach_textures(internal3d.color, internal3d.depth, msaa3d.samples, view_count); + _rt_attach_textures(internal3d.color, internal3d.depth, msaa3d.samples, view_count, true); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) { @@ -662,9 +662,10 @@ GLuint RenderSceneBuffersGLES3::get_render_fbo() { if (texture_storage->render_target_is_reattach_textures(render_target)) { GLuint color = texture_storage->render_target_get_color(render_target); GLuint depth = texture_storage->render_target_get_depth(render_target); + bool depth_has_stencil = texture_storage->render_target_get_depth_has_stencil(render_target); glBindFramebuffer(GL_FRAMEBUFFER, rt_fbo); - _rt_attach_textures(color, depth, msaa3d.samples, view_count); + _rt_attach_textures(color, depth, msaa3d.samples, view_count, depth_has_stencil); glBindFramebuffer(GL_FRAMEBUFFER, texture_storage->system_fbo); } diff --git a/drivers/gles3/storage/render_scene_buffers_gles3.h b/drivers/gles3/storage/render_scene_buffers_gles3.h index a20d373b49d..f98454f3504 100644 --- a/drivers/gles3/storage/render_scene_buffers_gles3.h +++ b/drivers/gles3/storage/render_scene_buffers_gles3.h @@ -93,7 +93,7 @@ private: void _clear_back_buffers(); void _clear_glow_buffers(); - void _rt_attach_textures(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count); + void _rt_attach_textures(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count, bool p_depth_has_stencil); GLuint _rt_get_cached_fbo(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count); public: diff --git a/drivers/gles3/storage/texture_storage.cpp b/drivers/gles3/storage/texture_storage.cpp index 6fa8f42b3f3..51884ac70ff 100644 --- a/drivers/gles3/storage/texture_storage.cpp +++ b/drivers/gles3/storage/texture_storage.cpp @@ -2854,6 +2854,13 @@ GLuint TextureStorage::render_target_get_depth(RID p_render_target) const { } } +bool TextureStorage::render_target_get_depth_has_stencil(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_NULL_V(rt, 0); + + return rt->depth_has_stencil; +} + void TextureStorage::render_target_set_reattach_textures(RID p_render_target, bool p_reattach_textures) const { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_NULL(rt); diff --git a/drivers/gles3/storage/texture_storage.h b/drivers/gles3/storage/texture_storage.h index 6119b8d2475..ebe94e34adf 100644 --- a/drivers/gles3/storage/texture_storage.h +++ b/drivers/gles3/storage/texture_storage.h @@ -669,6 +669,7 @@ public: GLuint render_target_get_fbo(RID p_render_target) const; GLuint render_target_get_color(RID p_render_target) const; GLuint render_target_get_depth(RID p_render_target) const; + bool render_target_get_depth_has_stencil(RID p_render_target) const; void render_target_set_reattach_textures(RID p_render_target, bool p_reattach_textures) const; bool render_target_is_reattach_textures(RID p_render_target) const;