From 6d70efb1ad29ce918d69799ecb20f784de4e1f24 Mon Sep 17 00:00:00 2001 From: Shrek Shao Date: Wed, 18 Dec 2024 16:57:53 -0800 Subject: [PATCH] [Compat GL] Use DEPTH24_STENCIL8 for stencil8 when STENCIL_INDEX8 unsupported OpenGLES 3.1 doesn't support STENCIL_INDEX8 format by default. But stencil8 format is required by compat. Create DEPTH24_STENCIL8 texture to emulate. Bug: 382084196, 42241333 Change-Id: Iaf193df429edd3bf2d12dbbc99ff3942435f80c0 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/217975 Commit-Queue: Stephen White Auto-Submit: Shrek Shao Reviewed-by: Stephen White --- src/dawn/native/opengl/DeviceGL.cpp | 10 +++++++++- src/dawn/native/opengl/DeviceGL.h | 1 + src/dawn/native/opengl/GLFormat.cpp | 8 ++++++-- src/dawn/native/opengl/GLFormat.h | 2 +- src/dawn/tests/end2end/DepthStencilCopyTests.cpp | 5 +++-- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/dawn/native/opengl/DeviceGL.cpp b/src/dawn/native/opengl/DeviceGL.cpp index bc6df998ce1..7969a90d990 100644 --- a/src/dawn/native/opengl/DeviceGL.cpp +++ b/src/dawn/native/opengl/DeviceGL.cpp @@ -160,7 +160,7 @@ MaybeError Device::Initialize(const UnpackedPtr& descriptor) { mContext->MakeCurrent(); const OpenGLFunctions& gl = mGL; - mFormatTable = BuildGLFormatTable(GetBGRAInternalFormat(gl)); + mFormatTable = BuildGLFormatTable(GetBGRAInternalFormat(gl), GetStencil8InternalFormat(gl)); // Use the debug output functionality to get notified about GL errors // TODO(crbug.com/dawn/1475): add support for the KHR_debug and ARB_debug_output @@ -229,6 +229,14 @@ GLenum Device::GetBGRAInternalFormat(const OpenGLFunctions& gl) const { } } +GLenum Device::GetStencil8InternalFormat(const OpenGLFunctions& gl) const { + if (gl.GetVersion().IsDesktop() || gl.IsAtLeastGLES(3, 2) || + gl.IsGLExtensionSupported("GL_OES_texture_stencil8")) { + return GL_STENCIL_INDEX8; + } + return GL_DEPTH24_STENCIL8; +} + ResultOrError> Device::CreateBindGroupImpl( const BindGroupDescriptor* descriptor) { return BindGroup::Create(this, descriptor); diff --git a/src/dawn/native/opengl/DeviceGL.h b/src/dawn/native/opengl/DeviceGL.h index 0b75b376594..d26a7ce5271 100644 --- a/src/dawn/native/opengl/DeviceGL.h +++ b/src/dawn/native/opengl/DeviceGL.h @@ -150,6 +150,7 @@ class Device final : public DeviceBase { const SharedFenceDescriptor* descriptor) override; GLenum GetBGRAInternalFormat(const OpenGLFunctions& gl) const; + GLenum GetStencil8InternalFormat(const OpenGLFunctions& gl) const; void DestroyImpl() override; const OpenGLFunctions mGL; diff --git a/src/dawn/native/opengl/GLFormat.cpp b/src/dawn/native/opengl/GLFormat.cpp index b1abde86b9f..d94f73bbd56 100644 --- a/src/dawn/native/opengl/GLFormat.cpp +++ b/src/dawn/native/opengl/GLFormat.cpp @@ -29,7 +29,7 @@ namespace dawn::native::opengl { -GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA) { +GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA, GLenum internalFormatForStencil8) { GLFormatTable table; using Type = GLFormat::ComponentType; @@ -114,7 +114,11 @@ GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA) { AddFormat(wgpu::TextureFormat::Depth24Plus, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, Type::DepthStencil); AddFormat(wgpu::TextureFormat::Depth24PlusStencil8, GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, Type::DepthStencil); AddFormat(wgpu::TextureFormat::Depth16Unorm, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, Type::DepthStencil); - AddFormat(wgpu::TextureFormat::Stencil8, GL_STENCIL_INDEX8, GL_STENCIL, GL_UNSIGNED_BYTE, Type::DepthStencil); + + // Internal format for stencil8 can be either GL_STENCIL_INDEX8 or GL_DEPTH24_STENCIL8 + DAWN_ASSERT(internalFormatForStencil8 == GL_STENCIL_INDEX8 || internalFormatForStencil8 == GL_DEPTH24_STENCIL8); + bool useStencilIndex8 = internalFormatForStencil8 == GL_STENCIL_INDEX8; + AddFormat(wgpu::TextureFormat::Stencil8, internalFormatForStencil8, useStencilIndex8 ? GL_STENCIL : GL_DEPTH_STENCIL, useStencilIndex8 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_INT_24_8, Type::DepthStencil); // Block compressed formats AddFormat(wgpu::TextureFormat::BC1RGBAUnorm, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_RGBA, GL_UNSIGNED_BYTE, Type::Float); diff --git a/src/dawn/native/opengl/GLFormat.h b/src/dawn/native/opengl/GLFormat.h index 5acaf4b0d16..a272fe316f5 100644 --- a/src/dawn/native/opengl/GLFormat.h +++ b/src/dawn/native/opengl/GLFormat.h @@ -46,7 +46,7 @@ struct GLFormat { }; using GLFormatTable = ityp::array; -GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA); +GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA, GLenum internalFormatForStencil8); } // namespace dawn::native::opengl diff --git a/src/dawn/tests/end2end/DepthStencilCopyTests.cpp b/src/dawn/tests/end2end/DepthStencilCopyTests.cpp index 1eb060eb544..edae745f6db 100644 --- a/src/dawn/tests/end2end/DepthStencilCopyTests.cpp +++ b/src/dawn/tests/end2end/DepthStencilCopyTests.cpp @@ -1608,13 +1608,14 @@ DAWN_INSTANTIATE_TEST_P( MetalBackend( {"metal_use_both_depth_and_stencil_attachments_for_combined_depth_stencil_formats"}), MetalBackend({"use_blit_for_buffer_to_stencil_texture_copy"}), OpenGLBackend(), - OpenGLESBackend(), + OpenGLESBackend(), OpenGLESBackend({"gl_force_es_31_and_no_extensions"}), // Test with the vulkan_use_s8 toggle forced on and off. VulkanBackend({"vulkan_use_s8"}, {}), VulkanBackend({}, {"vulkan_use_s8"})}, std::vector(utils::kStencilFormats.begin(), utils::kStencilFormats.end())); DAWN_INSTANTIATE_TEST_P(StencilCopyTests_Compat, - {OpenGLBackend(), OpenGLESBackend()}, + {OpenGLBackend(), OpenGLESBackend(), + OpenGLESBackend({"gl_force_es_31_and_no_extensions"})}, std::vector(utils::kStencilFormats.begin(), utils::kStencilFormats.end()));