Commit a02315b0 by Jamie Madill Committed by Commit Bot

WebGL Compat: Add DEPTH_STENCIL attachments.

This is a special WebGL 1 binding point, that does not correspond to any native functionality. Due to particularities in validation we need to represent this with additional state in the Framebuffer. WebGL 2 fixes this oddity by resolving to the GLES 3 native spec. In order to pass the WebGL framebuffer objects test, we will also need a chromium-side CL to work with the additional state tracking it does in the blink layer, and an additional patch to ANGLE to clear the depth buffer before the first use (robust resource init). BUG=angleproject:1708 Change-Id: I111f8f5a451cce7de6cf281a6bc335b92dd2daf2 Reviewed-on: https://chromium-review.googlesource.com/444095 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 89fcb8e8
......@@ -2123,7 +2123,7 @@ void Context::detachTexture(GLuint texture)
// allocation map management either here or in the resource manager at detach time.
// Zero textures are held by the Context, and we don't attempt to request them from
// the State.
mGLState.detachTexture(mZeroTextures, texture);
mGLState.detachTexture(this, mZeroTextures, texture);
}
void Context::detachBuffer(GLuint buffer)
......@@ -2162,7 +2162,7 @@ void Context::detachFramebuffer(GLuint framebuffer)
void Context::detachRenderbuffer(GLuint renderbuffer)
{
mGLState.detachRenderbuffer(renderbuffer);
mGLState.detachRenderbuffer(this, renderbuffer);
}
void Context::detachVertexArray(GLuint vertexArray)
......@@ -2738,11 +2738,11 @@ void Context::framebufferTexture2D(GLenum target,
index = ImageIndex::MakeCube(textarget, level);
}
framebuffer->setAttachment(GL_TEXTURE, attachment, index, textureObj);
framebuffer->setAttachment(this, GL_TEXTURE, attachment, index, textureObj);
}
else
{
framebuffer->resetAttachment(attachment);
framebuffer->resetAttachment(this, attachment);
}
mGLState.setObjectDirty(target);
......@@ -2759,12 +2759,13 @@ void Context::framebufferRenderbuffer(GLenum target,
if (renderbuffer != 0)
{
Renderbuffer *renderbufferObject = getRenderbuffer(renderbuffer);
framebuffer->setAttachment(GL_RENDERBUFFER, attachment, gl::ImageIndex::MakeInvalid(),
framebuffer->setAttachment(this, GL_RENDERBUFFER, attachment, gl::ImageIndex::MakeInvalid(),
renderbufferObject);
}
else
{
framebuffer->resetAttachment(attachment);
framebuffer->resetAttachment(this, attachment);
}
mGLState.setObjectDirty(target);
......@@ -2795,11 +2796,11 @@ void Context::framebufferTextureLayer(GLenum target,
index = ImageIndex::Make2DArray(level, layer);
}
framebuffer->setAttachment(GL_TEXTURE, attachment, index, textureObject);
framebuffer->setAttachment(this, GL_TEXTURE, attachment, index, textureObject);
}
else
{
framebuffer->resetAttachment(attachment);
framebuffer->resetAttachment(this, attachment);
}
mGLState.setObjectDirty(target);
......
......@@ -98,6 +98,11 @@ ContextState::~ContextState()
// Handles are released by the Context.
}
bool ContextState::isWebGL1() const
{
return (mExtensions.webglCompatibility && mClientVersion.major == 2);
}
const TextureCaps &ContextState::getTextureCap(GLenum internalFormat) const
{
return mTextureCaps.get(internalFormat);
......
......@@ -58,6 +58,8 @@ class ContextState final : public angle::NonCopyable
bool usingDisplayTextureShareGroup() const;
bool isWebGL1() const;
private:
friend class Context;
friend class ValidationContext;
......@@ -124,6 +126,8 @@ class ValidationContext : angle::NonCopyable
// Hack for the special WebGL 1 "DEPTH_STENCIL" internal format.
GLenum getConvertedRenderbufferFormat(GLenum internalformat) const;
bool isWebGL1() const { return mState.isWebGL1(); }
protected:
ContextState mState;
bool mSkipValidation;
......
......@@ -50,7 +50,8 @@ FramebufferState::FramebufferState()
mDefaultWidth(0),
mDefaultHeight(0),
mDefaultSamples(0),
mDefaultFixedSampleLocations(GL_FALSE)
mDefaultFixedSampleLocations(GL_FALSE),
mWebGLDepthStencilConsistent(true)
{
mEnabledDrawBuffers.set(0);
}
......@@ -63,7 +64,8 @@ FramebufferState::FramebufferState(const Caps &caps)
mDefaultWidth(0),
mDefaultHeight(0),
mDefaultSamples(0),
mDefaultFixedSampleLocations(GL_FALSE)
mDefaultFixedSampleLocations(GL_FALSE),
mWebGLDepthStencilConsistent(true)
{
ASSERT(mDrawBufferStates.size() > 0);
mDrawBufferStates[0] = GL_COLOR_ATTACHMENT0_EXT;
......@@ -313,16 +315,17 @@ Framebuffer::Framebuffer(egl::Surface *surface)
mDirtyColorAttachmentBindings.push_back(
ChannelBinding(this, static_cast<SignalToken>(DIRTY_BIT_COLOR_ATTACHMENT_0)));
setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(), surface);
setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_BACK, gl::ImageIndex::MakeInvalid(), surface);
if (surface->getConfig()->depthSize > 0)
{
setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(), surface);
setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_DEPTH, gl::ImageIndex::MakeInvalid(), surface);
}
if (surface->getConfig()->stencilSize > 0)
{
setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, gl::ImageIndex::MakeInvalid(), surface);
setAttachmentImpl(GL_FRAMEBUFFER_DEFAULT, GL_STENCIL, gl::ImageIndex::MakeInvalid(),
surface);
}
}
......@@ -363,17 +366,17 @@ const std::string &Framebuffer::getLabel() const
return mState.mLabel;
}
void Framebuffer::detachTexture(GLuint textureId)
void Framebuffer::detachTexture(const Context *context, GLuint textureId)
{
detachResourceById(GL_TEXTURE, textureId);
detachResourceById(context, GL_TEXTURE, textureId);
}
void Framebuffer::detachRenderbuffer(GLuint renderbufferId)
void Framebuffer::detachRenderbuffer(const Context *context, GLuint renderbufferId)
{
detachResourceById(GL_RENDERBUFFER, renderbufferId);
detachResourceById(context, GL_RENDERBUFFER, renderbufferId);
}
void Framebuffer::detachResourceById(GLenum resourceType, GLuint resourceId)
void Framebuffer::detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId)
{
for (size_t colorIndex = 0; colorIndex < mState.mColorAttachments.size(); ++colorIndex)
{
......@@ -381,10 +384,27 @@ void Framebuffer::detachResourceById(GLenum resourceType, GLuint resourceId)
DIRTY_BIT_COLOR_ATTACHMENT_0 + colorIndex);
}
if (context->isWebGL1())
{
const std::array<FramebufferAttachment *, 3> attachments = {
{&mState.mWebGLDepthStencilAttachment, &mState.mWebGLDepthAttachment,
&mState.mWebGLStencilAttachment}};
for (FramebufferAttachment *attachment : attachments)
{
if (attachment->isAttached() && attachment->type() == resourceType &&
attachment->id() == resourceId)
{
resetAttachment(context, attachment->getBinding());
}
}
}
else
{
detachMatchingAttachment(&mState.mDepthAttachment, resourceType, resourceId,
DIRTY_BIT_DEPTH_ATTACHMENT);
detachMatchingAttachment(&mState.mStencilAttachment, resourceType, resourceId,
DIRTY_BIT_STENCIL_ATTACHMENT);
}
}
void Framebuffer::detachMatchingAttachment(FramebufferAttachment *attachment,
......@@ -763,6 +783,34 @@ GLenum Framebuffer::checkStatusImpl(const ContextState &state)
}
}
// Special additional validation for WebGL 1 DEPTH/STENCIL/DEPTH_STENCIL.
if (state.isWebGL1())
{
if (!mState.mWebGLDepthStencilConsistent)
{
return GL_FRAMEBUFFER_UNSUPPORTED;
}
if (mState.mWebGLDepthStencilAttachment.isAttached())
{
if (mState.mWebGLDepthStencilAttachment.getDepthSize() == 0 ||
mState.mWebGLDepthStencilAttachment.getStencilSize() == 0)
{
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
}
}
else if (mState.mStencilAttachment.isAttached() &&
mState.mStencilAttachment.getDepthSize() > 0)
{
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
}
else if (mState.mDepthAttachment.isAttached() &&
mState.mDepthAttachment.getStencilSize() > 0)
{
return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
}
}
// we need to have at least one attachment to be complete
if (missingAttachment)
{
......@@ -951,7 +999,93 @@ bool Framebuffer::hasValidDepthStencil() const
return mState.getDepthStencilAttachment() != nullptr;
}
void Framebuffer::setAttachment(GLenum type,
void Framebuffer::setAttachment(const Context *context,
GLenum type,
GLenum binding,
const ImageIndex &textureIndex,
FramebufferAttachmentObject *resource)
{
// Context may be null in unit tests.
if (!context || !context->isWebGL1())
{
setAttachmentImpl(type, binding, textureIndex, resource);
return;
}
switch (binding)
{
case GL_DEPTH_STENCIL:
case GL_DEPTH_STENCIL_ATTACHMENT:
mState.mWebGLDepthStencilAttachment.attach(type, binding, textureIndex, resource);
break;
case GL_DEPTH:
case GL_DEPTH_ATTACHMENT:
mState.mWebGLDepthAttachment.attach(type, binding, textureIndex, resource);
break;
case GL_STENCIL:
case GL_STENCIL_ATTACHMENT:
mState.mWebGLStencilAttachment.attach(type, binding, textureIndex, resource);
break;
default:
setAttachmentImpl(type, binding, textureIndex, resource);
return;
}
commitWebGL1DepthStencilIfConsistent();
}
void Framebuffer::commitWebGL1DepthStencilIfConsistent()
{
int count = 0;
std::array<FramebufferAttachment *, 3> attachments = {{&mState.mWebGLDepthStencilAttachment,
&mState.mWebGLDepthAttachment,
&mState.mWebGLStencilAttachment}};
for (FramebufferAttachment *attachment : attachments)
{
if (attachment->isAttached())
{
count++;
}
}
mState.mWebGLDepthStencilConsistent = (count <= 1);
if (!mState.mWebGLDepthStencilConsistent)
{
// Inconsistent.
return;
}
if (mState.mWebGLDepthAttachment.isAttached())
{
const auto &depth = mState.mWebGLDepthAttachment;
setAttachmentImpl(depth.type(), GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(),
depth.getResource());
setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
}
else if (mState.mWebGLStencilAttachment.isAttached())
{
const auto &stencil = mState.mWebGLStencilAttachment;
setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
setAttachmentImpl(stencil.type(), GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(),
stencil.getResource());
}
else if (mState.mWebGLDepthStencilAttachment.isAttached())
{
const auto &depthStencil = mState.mWebGLDepthStencilAttachment;
setAttachmentImpl(depthStencil.type(), GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(),
depthStencil.getResource());
setAttachmentImpl(depthStencil.type(), GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(),
depthStencil.getResource());
}
else
{
setAttachmentImpl(GL_NONE, GL_DEPTH_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
setAttachmentImpl(GL_NONE, GL_STENCIL_ATTACHMENT, ImageIndex::MakeInvalid(), nullptr);
}
}
void Framebuffer::setAttachmentImpl(GLenum type,
GLenum binding,
const ImageIndex &textureIndex,
FramebufferAttachmentObject *resource)
......@@ -977,9 +1111,9 @@ void Framebuffer::setAttachment(GLenum type,
mDirtyBits.set(DIRTY_BIT_STENCIL_ATTACHMENT);
BindResourceChannel(&mDirtyDepthAttachmentBinding, resource);
BindResourceChannel(&mDirtyStencilAttachmentBinding, resource);
return;
}
else
{
switch (binding)
{
case GL_DEPTH:
......@@ -1012,12 +1146,11 @@ void Framebuffer::setAttachment(GLenum type,
}
break;
}
}
}
void Framebuffer::resetAttachment(GLenum binding)
void Framebuffer::resetAttachment(const Context *context, GLenum binding)
{
setAttachment(GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
setAttachment(context, GL_NONE, binding, ImageIndex::MakeInvalid(), nullptr);
}
void Framebuffer::syncState()
......
......@@ -105,6 +105,13 @@ class FramebufferState final : angle::NonCopyable
GLint mDefaultHeight;
GLint mDefaultSamples;
GLboolean mDefaultFixedSampleLocations;
// It's necessary to store all this extra state so we can restore attachments
// when DEPTH_STENCIL/DEPTH/STENCIL is unbound in WebGL 1.
FramebufferAttachment mWebGLDepthStencilAttachment;
FramebufferAttachment mWebGLDepthAttachment;
FramebufferAttachment mWebGLStencilAttachment;
bool mWebGLDepthStencilConsistent;
};
class Framebuffer final : public LabeledObject, public angle::SignalReceiver
......@@ -128,14 +135,15 @@ class Framebuffer final : public LabeledObject, public angle::SignalReceiver
GLuint id() const { return mId; }
void setAttachment(GLenum type,
void setAttachment(const Context *context,
GLenum type,
GLenum binding,
const ImageIndex &textureIndex,
FramebufferAttachmentObject *resource);
void resetAttachment(GLenum binding);
void resetAttachment(const Context *context, GLenum binding);
void detachTexture(GLuint texture);
void detachRenderbuffer(GLuint renderbuffer);
void detachTexture(const Context *context, GLuint texture);
void detachRenderbuffer(const Context *context, GLuint renderbuffer);
const FramebufferAttachment *getColorbuffer(size_t colorAttachment) const;
const FramebufferAttachment *getDepthbuffer() const;
......@@ -254,12 +262,18 @@ class Framebuffer final : public LabeledObject, public angle::SignalReceiver
GLint copyTextureLayer) const;
private:
void detachResourceById(GLenum resourceType, GLuint resourceId);
void detachResourceById(const Context *context, GLenum resourceType, GLuint resourceId);
void detachMatchingAttachment(FramebufferAttachment *attachment,
GLenum matchType,
GLuint matchId,
size_t dirtyBit);
GLenum checkStatusImpl(const ContextState &state);
void commitWebGL1DepthStencilIfConsistent();
void setAttachmentImpl(GLenum type,
GLenum binding,
const ImageIndex &textureIndex,
FramebufferAttachmentObject *resource);
FramebufferState mState;
rx::FramebufferImpl *mImpl;
......
......@@ -210,6 +210,11 @@ const egl::Surface *FramebufferAttachment::getSurface() const
return rx::GetAs<egl::Surface>(mResource);
}
FramebufferAttachmentObject *FramebufferAttachment::getResource() const
{
return mResource;
}
bool FramebufferAttachment::operator==(const FramebufferAttachment &other) const
{
if (mResource != other.mResource || mType != other.mType)
......
......@@ -126,6 +126,7 @@ class FramebufferAttachment final
Renderbuffer *getRenderbuffer() const;
Texture *getTexture() const;
const egl::Surface *getSurface() const;
FramebufferAttachmentObject *getResource() const;
// "T" must be static_castable from FramebufferAttachmentRenderTarget
template <typename T>
......@@ -177,22 +178,26 @@ class FramebufferAttachmentObject
inline Extents FramebufferAttachment::getSize() const
{
ASSERT(mResource);
return mResource->getAttachmentSize(mTarget);
}
inline const Format &FramebufferAttachment::getFormat() const
{
ASSERT(mResource);
return mResource->getAttachmentFormat(mTarget);
}
inline GLsizei FramebufferAttachment::getSamples() const
{
ASSERT(mResource);
return mResource->getAttachmentSamples(mTarget);
}
inline gl::Error FramebufferAttachment::getRenderTargetImpl(
rx::FramebufferAttachmentRenderTarget **rtOut) const
{
ASSERT(mResource);
return mResource->getAttachmentRenderTarget(mTarget, rtOut);
}
......
......@@ -779,7 +779,7 @@ GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
return it->second[sampler].id();
}
void State::detachTexture(const TextureMap &zeroTextures, GLuint texture)
void State::detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture)
{
// Textures have a detach method on State rather than a simple
// removeBinding, because the zero/null texture objects are managed
......@@ -814,12 +814,12 @@ void State::detachTexture(const TextureMap &zeroTextures, GLuint texture)
if (mReadFramebuffer)
{
mReadFramebuffer->detachTexture(texture);
mReadFramebuffer->detachTexture(context, texture);
}
if (mDrawFramebuffer)
{
mDrawFramebuffer->detachTexture(texture);
mDrawFramebuffer->detachTexture(context, texture);
}
}
......@@ -883,7 +883,7 @@ Renderbuffer *State::getCurrentRenderbuffer() const
return mRenderbuffer.get();
}
void State::detachRenderbuffer(GLuint renderbuffer)
void State::detachRenderbuffer(const Context *context, GLuint renderbuffer)
{
// [OpenGL ES 2.0.24] section 4.4 page 109:
// If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
......@@ -904,12 +904,12 @@ void State::detachRenderbuffer(GLuint renderbuffer)
if (readFramebuffer)
{
readFramebuffer->detachRenderbuffer(renderbuffer);
readFramebuffer->detachRenderbuffer(context, renderbuffer);
}
if (drawFramebuffer && drawFramebuffer != readFramebuffer)
{
drawFramebuffer->detachRenderbuffer(renderbuffer);
drawFramebuffer->detachRenderbuffer(context, renderbuffer);
}
}
......
......@@ -167,7 +167,7 @@ class State : angle::NonCopyable
Texture *getTargetTexture(GLenum target) const;
Texture *getSamplerTexture(unsigned int sampler, GLenum type) const;
GLuint getSamplerTextureId(unsigned int sampler, GLenum type) const;
void detachTexture(const TextureMap &zeroTextures, GLuint texture);
void detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture);
void initializeZeroTextures(const TextureMap &zeroTextures);
// Sampler object binding manipulation
......@@ -180,7 +180,7 @@ class State : angle::NonCopyable
void setRenderbufferBinding(Renderbuffer *renderbuffer);
GLuint getRenderbufferId() const;
Renderbuffer *getCurrentRenderbuffer() const;
void detachRenderbuffer(GLuint renderbuffer);
void detachRenderbuffer(const Context *context, GLuint renderbuffer);
// Framebuffer binding manipulation
void setReadFramebufferBinding(Framebuffer *framebuffer);
......
......@@ -628,8 +628,7 @@ void QueryRenderbufferiv(const Context *context,
break;
case GL_RENDERBUFFER_INTERNAL_FORMAT:
// Special case the WebGL 1 DEPTH_STENCIL format.
if (context->getExtensions().webglCompatibility &&
context->getClientMajorVersion() == 2 &&
if (context->isWebGL1() &&
renderbuffer->getFormat().info->internalFormat == GL_DEPTH24_STENCIL8)
{
*params = GL_DEPTH_STENCIL;
......
......@@ -91,7 +91,8 @@ TEST(ValidationESTest, DrawElementsWithMaxIndexGivesError)
VertexArray *vertexArray = new VertexArray(&mockFactory, 0, 1, 1);
Framebuffer *framebuffer = new Framebuffer(caps, &mockFactory, 1);
framebuffer->setAttachment(GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::Make2D(0), texture);
framebuffer->setAttachment(nullptr, GL_FRAMEBUFFER_DEFAULT, GL_BACK, ImageIndex::Make2D(0),
texture);
Program *program = new Program(&mockFactory, nullptr, 1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment