diff --git a/src/dawn/native/opengl/DeviceGL.cpp b/src/dawn/native/opengl/DeviceGL.cpp index bc6df998ce..7969a90d99 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 0b75b37659..d26a7ce527 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 b1abde86b9..d94f73bbd5 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 5acaf4b0d1..a272fe316f 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 1eb060eb54..edae745f6d 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()));