Skip to content

Commit

Permalink
[Compat GL] Use DEPTH24_STENCIL8 for stencil8 when STENCIL_INDEX8 uns…
Browse files Browse the repository at this point in the history
…upported

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 <[email protected]>
Auto-Submit: Shrek Shao <[email protected]>
Reviewed-by: Stephen White <[email protected]>
  • Loading branch information
shrekshao authored and Dawn LUCI CQ committed Dec 19, 2024
1 parent 0cb4107 commit 6d70efb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/dawn/native/opengl/DeviceGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ MaybeError Device::Initialize(const UnpackedPtr<DeviceDescriptor>& 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
Expand Down Expand Up @@ -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<Ref<BindGroupBase>> Device::CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) {
return BindGroup::Create(this, descriptor);
Expand Down
1 change: 1 addition & 0 deletions src/dawn/native/opengl/DeviceGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/dawn/native/opengl/GLFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace dawn::native::opengl {

GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA) {
GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA, GLenum internalFormatForStencil8) {
GLFormatTable table;

using Type = GLFormat::ComponentType;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/dawn/native/opengl/GLFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct GLFormat {
};

using GLFormatTable = ityp::array<FormatIndex, GLFormat, kKnownFormatCount>;
GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA);
GLFormatTable BuildGLFormatTable(GLenum internalFormatForBGRA, GLenum internalFormatForStencil8);

} // namespace dawn::native::opengl

Expand Down
5 changes: 3 additions & 2 deletions src/dawn/tests/end2end/DepthStencilCopyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<wgpu::TextureFormat>(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<wgpu::TextureFormat>(utils::kStencilFormats.begin(),
utils::kStencilFormats.end()));

Expand Down

0 comments on commit 6d70efb

Please sign in to comment.