Skip to content

Commit

Permalink
Cherry-pick 278159@main (ed28635). https://bugs.webkit.org/show_bug.c…
Browse files Browse the repository at this point in the history
…gi?id=273177

    [GTK][WPE] Don't allow depth test and stencil clipping if packed depth stencil is not supported
    https://bugs.webkit.org/show_bug.cgi?id=273177

    Reviewed by Carlos Garcia Campos.

    Use a packed depth stencil format when available, which allows depth testing
    and stencil clipping at the same time. If packed depth stencil is not
    supported, use separate buffers for depth and stencil. In the latter case,
    if both buffers are requested at the same time, the stencil buffer won't
    be created.

    * Source/WebCore/platform/graphics/egl/GLContext.cpp:
    (WebCore::GLContext::glExtensions const):
    * Source/WebCore/platform/graphics/egl/GLContext.h:
    * Source/WebCore/platform/graphics/texmap/BitmapTexture.cpp:
    (WebCore::depthBufferFormat):
    (WebCore::BitmapTexture::initializeStencil):
    (WebCore::BitmapTexture::initializeDepthBuffer):
    (WebCore::BitmapTexture::~BitmapTexture):
    * Source/WebCore/platform/graphics/texmap/BitmapTexture.h:

    Canonical link: https://commits.webkit.org/278159@main

Canonical link: https://commits.webkit.org/274313.193@webkitglib/2.44
  • Loading branch information
magomez authored and aperezdc committed May 2, 2024
1 parent f82aff0 commit bd536df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions Source/WebCore/platform/graphics/egl/GLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ const GLContext::GLExtensions& GLContext::glExtensions() const
const char* extensionsString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
m_glExtensions.OES_texture_npot = isExtensionSupported(extensionsString, "GL_OES_texture_npot");
m_glExtensions.EXT_unpack_subimage = isExtensionSupported(extensionsString, "GL_EXT_unpack_subimage");
m_glExtensions.OES_packed_depth_stencil = isExtensionSupported(extensionsString, "GL_OES_packed_depth_stencil");
});
return m_glExtensions;
}
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/platform/graphics/egl/GLContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class GLContext {
struct GLExtensions {
bool OES_texture_npot { false };
bool EXT_unpack_subimage { false };
bool OES_packed_depth_stencil { false };
};
const GLExtensions& glExtensions() const;

Expand Down
57 changes: 39 additions & 18 deletions Source/WebCore/platform/graphics/texmap/BitmapTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,26 @@ static const GLenum s_pixelDataType = GL_UNSIGNED_INT_8_8_8_8_REV;
static const GLenum s_pixelDataType = GL_UNSIGNED_BYTE;
#endif

// On GLES3, the format we want for packed depth stencil is GL_DEPTH24_STENCIL8, but when added through
// the extension this format is called GL_DEPTH24_STENCIL8_OES. In any case they hold the same value 0x88F0
// so we can just use the first one.
// These definitions may not exist if this is a GLES1/2 context without the GL_OES_packed_depth_stencil
// extension. We need to define the one we want to use in order to build on every case.
#ifndef GL_DEPTH24_STENCIL8
#define GL_DEPTH24_STENCIL8 0x88F0
#endif

namespace WebCore {

GLenum depthBufferFormat()
{
auto* glContext = GLContext::current();
if (glContext->version() >= 300 || glContext->glExtensions().OES_packed_depth_stencil)
return GL_DEPTH24_STENCIL8;

return GL_DEPTH_COMPONENT16;
}

BitmapTexture::BitmapTexture(const IntSize& size, OptionSet<Flags> flags, GLint internalFormat)
: m_flags(flags)
, m_size(size)
Expand Down Expand Up @@ -185,34 +203,39 @@ void BitmapTexture::updateContents(GraphicsLayer* sourceLayer, const IntRect& ta

void BitmapTexture::initializeStencil()
{
#if !USE(TEXMAP_DEPTH_STENCIL_BUFFER)
if (m_rbo)
if (m_flags.contains(Flags::DepthBuffer)) {
// We have a depth buffer and we're asked to have a stencil buffer as well. This is only
// possible if packed depth stencil is available. If that's the case, just bind the depth
// buffer as the stencil one if haven't done so. If packed depth stencil is not available
// don't do anything, which will cause stencil clips on this surface to fail.
if (depthBufferFormat() == GL_DEPTH24_STENCIL8 && !m_stencilBound) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferObject);
m_stencilBound = true;
}
return;
}

glGenRenderbuffers(1, &m_rbo);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbo);
// We don't have a depth buffer. Use a stencil only buffer.
if (m_stencilBufferObject)
return;

glGenRenderbuffers(1, &m_stencilBufferObject);
glBindRenderbuffer(GL_RENDERBUFFER, m_stencilBufferObject);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, m_size.width(), m_size.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_rbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_stencilBufferObject);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
#endif
}

void BitmapTexture::initializeDepthBuffer()
{
if (m_depthBufferObject)
return;

#if USE(TEXMAP_DEPTH_STENCIL_BUFFER)
GLenum format = GL_DEPTH24_STENCIL8_OES;
#else
GLenum format = GL_DEPTH_COMPONENT16;
#endif

glGenRenderbuffers(1, &m_depthBufferObject);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthBufferObject);
glRenderbufferStorage(GL_RENDERBUFFER, format, m_size.width(), m_size.height());
glRenderbufferStorage(GL_RENDERBUFFER, depthBufferFormat(), m_size.width(), m_size.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferObject);
}
Expand Down Expand Up @@ -264,13 +287,11 @@ BitmapTexture::~BitmapTexture()
if (m_fbo)
glDeleteFramebuffers(1, &m_fbo);

#if !USE(TEXMAP_DEPTH_STENCIL_BUFFER)
if (m_rbo)
glDeleteRenderbuffers(1, &m_rbo);
#endif

if (m_depthBufferObject)
glDeleteRenderbuffers(1, &m_depthBufferObject);

if (m_stencilBufferObject)
glDeleteRenderbuffers(1, &m_stencilBufferObject);
}

void BitmapTexture::copyFromExternalTexture(GLuint sourceTextureID)
Expand Down
11 changes: 2 additions & 9 deletions Source/WebCore/platform/graphics/texmap/BitmapTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ class NativeImage;
class TextureMapper;
enum class TextureMapperFlags : uint16_t;

#if OS(WINDOWS)
#define USE_TEXMAP_DEPTH_STENCIL_BUFFER 1
#else
#define USE_TEXMAP_DEPTH_STENCIL_BUFFER 0
#endif

class BitmapTexture final : public RefCounted<BitmapTexture> {
public:
enum class Flags : uint8_t {
Expand Down Expand Up @@ -100,10 +94,9 @@ class BitmapTexture final : public RefCounted<BitmapTexture> {
IntSize m_size;
GLuint m_id { 0 };
GLuint m_fbo { 0 };
#if !USE(TEXMAP_DEPTH_STENCIL_BUFFER)
GLuint m_rbo { 0 };
#endif
GLuint m_depthBufferObject { 0 };
GLuint m_stencilBufferObject { 0 };
bool m_stencilBound { false };
bool m_shouldClear { true };
ClipStack m_clipStack;
OptionSet<TextureMapperFlags> m_colorConvertFlags;
Expand Down

0 comments on commit bd536df

Please sign in to comment.