michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "WebGLContext.h" michael@0: #include "WebGLRenderbuffer.h" michael@0: #include "WebGLTexture.h" michael@0: #include "mozilla/dom/WebGLRenderingContextBinding.h" michael@0: #include "GLContext.h" michael@0: #include "ScopedGLHelpers.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::gl; michael@0: michael@0: static GLenum michael@0: DepthStencilDepthFormat(GLContext* gl) { michael@0: // We might not be able to get 24-bit, so let's pretend! michael@0: if (gl->IsGLES() && !gl->IsExtensionSupported(gl::GLContext::OES_depth24)) michael@0: return LOCAL_GL_DEPTH_COMPONENT16; michael@0: michael@0: return LOCAL_GL_DEPTH_COMPONENT24; michael@0: } michael@0: michael@0: static bool michael@0: SupportsDepthStencil(GLContext* gl) { michael@0: return gl->IsExtensionSupported(GLContext::EXT_packed_depth_stencil) || michael@0: gl->IsExtensionSupported(GLContext::OES_packed_depth_stencil); michael@0: } michael@0: michael@0: static bool michael@0: NeedsDepthStencilEmu(GLContext* gl, GLenum internalFormat) { michael@0: MOZ_ASSERT(internalFormat != LOCAL_GL_DEPTH_STENCIL); michael@0: if (internalFormat != LOCAL_GL_DEPTH24_STENCIL8) michael@0: return false; michael@0: michael@0: return !SupportsDepthStencil(gl); michael@0: } michael@0: michael@0: JSObject* michael@0: WebGLRenderbuffer::WrapObject(JSContext *cx) { michael@0: return dom::WebGLRenderbufferBinding::Wrap(cx, this); michael@0: } michael@0: michael@0: WebGLRenderbuffer::WebGLRenderbuffer(WebGLContext *context) michael@0: : WebGLContextBoundObject(context) michael@0: , mPrimaryRB(0) michael@0: , mSecondaryRB(0) michael@0: , mInternalFormat(0) michael@0: , mInternalFormatForGL(0) michael@0: , mHasEverBeenBound(false) michael@0: , mImageDataStatus(WebGLImageDataStatus::NoImageData) michael@0: { michael@0: SetIsDOMBinding(); michael@0: mContext->MakeContextCurrent(); michael@0: michael@0: mContext->gl->fGenRenderbuffers(1, &mPrimaryRB); michael@0: if (!SupportsDepthStencil(mContext->gl)) michael@0: mContext->gl->fGenRenderbuffers(1, &mSecondaryRB); michael@0: michael@0: mContext->mRenderbuffers.insertBack(this); michael@0: } michael@0: michael@0: void michael@0: WebGLRenderbuffer::Delete() { michael@0: mContext->MakeContextCurrent(); michael@0: michael@0: mContext->gl->fDeleteRenderbuffers(1, &mPrimaryRB); michael@0: if (mSecondaryRB) michael@0: mContext->gl->fDeleteRenderbuffers(1, &mSecondaryRB); michael@0: michael@0: LinkedListElement::removeFrom(mContext->mRenderbuffers); michael@0: } michael@0: michael@0: int64_t michael@0: WebGLRenderbuffer::MemoryUsage() const { michael@0: int64_t pixels = int64_t(Width()) * int64_t(Height()); michael@0: michael@0: GLenum primaryFormat = InternalFormatForGL(); michael@0: // If there is no defined format, we're not taking up any memory michael@0: if (!primaryFormat) { michael@0: return 0; michael@0: } michael@0: michael@0: int64_t secondarySize = 0; michael@0: if (mSecondaryRB) { michael@0: if (NeedsDepthStencilEmu(mContext->gl, primaryFormat)) { michael@0: primaryFormat = DepthStencilDepthFormat(mContext->gl); michael@0: secondarySize = 1*pixels; // STENCIL_INDEX8 michael@0: } else { michael@0: secondarySize = 1*1*2; // 1x1xRGBA4 michael@0: } michael@0: } michael@0: michael@0: int64_t primarySize = 0; michael@0: switch (primaryFormat) { michael@0: case LOCAL_GL_STENCIL_INDEX8: michael@0: primarySize = 1*pixels; michael@0: break; michael@0: case LOCAL_GL_RGBA4: michael@0: case LOCAL_GL_RGB5_A1: michael@0: case LOCAL_GL_RGB565: michael@0: case LOCAL_GL_DEPTH_COMPONENT16: michael@0: primarySize = 2*pixels; michael@0: break; michael@0: case LOCAL_GL_RGB8: michael@0: case LOCAL_GL_DEPTH_COMPONENT24: michael@0: primarySize = 3*pixels; michael@0: break; michael@0: case LOCAL_GL_RGBA8: michael@0: case LOCAL_GL_SRGB8_ALPHA8_EXT: michael@0: case LOCAL_GL_DEPTH24_STENCIL8: michael@0: case LOCAL_GL_DEPTH_COMPONENT32: michael@0: primarySize = 4*pixels; michael@0: break; michael@0: case LOCAL_GL_RGB16F: michael@0: primarySize = 2*3*pixels; michael@0: break; michael@0: case LOCAL_GL_RGBA16F: michael@0: primarySize = 2*4*pixels; michael@0: break; michael@0: case LOCAL_GL_RGB32F: michael@0: primarySize = 4*3*pixels; michael@0: break; michael@0: case LOCAL_GL_RGBA32F: michael@0: primarySize = 4*4*pixels; michael@0: break; michael@0: default: michael@0: MOZ_ASSERT(false, "Unknown `primaryFormat`."); michael@0: break; michael@0: } michael@0: michael@0: return primarySize + secondarySize; michael@0: } michael@0: michael@0: void michael@0: WebGLRenderbuffer::BindRenderbuffer() const { michael@0: /* Do this explicitly here, since the meaning changes for depth-stencil emu. michael@0: * Under normal circumstances, there's only one RB: `mPrimaryRB`. michael@0: * `mSecondaryRB` is used when we have to pretend that the renderbuffer is michael@0: * DEPTH_STENCIL, when it's actually one DEPTH buffer `mPrimaryRB` and one michael@0: * STENCIL buffer `mSecondaryRB`. michael@0: * michael@0: * In the DEPTH_STENCIL emulation case, we're actually juggling two RBs, but michael@0: * we can only bind one of them at a time. We choose to unconditionally bind michael@0: * the depth RB. When we need to ask about the stencil buffer (say, how many michael@0: * stencil bits we have), we temporarily bind the stencil RB, so that it michael@0: * looks like we're just asking the question of a combined DEPTH_STENCIL michael@0: * buffer. michael@0: */ michael@0: mContext->gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB); michael@0: } michael@0: michael@0: void michael@0: WebGLRenderbuffer::RenderbufferStorage(GLenum internalFormat, GLsizei width, GLsizei height) const { michael@0: GLContext* gl = mContext->gl; michael@0: michael@0: GLenum primaryFormat = internalFormat; michael@0: GLenum secondaryFormat = 0; michael@0: michael@0: if (NeedsDepthStencilEmu(mContext->gl, primaryFormat)) { michael@0: primaryFormat = DepthStencilDepthFormat(gl); michael@0: secondaryFormat = LOCAL_GL_STENCIL_INDEX8; michael@0: } michael@0: michael@0: gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, primaryFormat, width, height); michael@0: michael@0: if (!mSecondaryRB) { michael@0: MOZ_ASSERT(!secondaryFormat); michael@0: return; michael@0: } michael@0: // We can't leave the secondary RB unspecified either, since we should michael@0: // handle the case where we attach a non-depth-stencil RB to a michael@0: // depth-stencil attachment point, or attach this depth-stencil RB to a michael@0: // non-depth-stencil attachment point. michael@0: ScopedBindRenderbuffer autoRB(gl, mSecondaryRB); michael@0: if (secondaryFormat) { michael@0: gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, secondaryFormat, width, height); michael@0: } else { michael@0: gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, LOCAL_GL_RGBA4, 1, 1); michael@0: } michael@0: } michael@0: michael@0: void michael@0: WebGLRenderbuffer::FramebufferRenderbuffer(GLenum attachment) const { michael@0: GLContext* gl = mContext->gl; michael@0: if (attachment != LOCAL_GL_DEPTH_STENCIL_ATTACHMENT) { michael@0: gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, attachment, LOCAL_GL_RENDERBUFFER, mPrimaryRB); michael@0: return; michael@0: } michael@0: michael@0: GLuint stencilRB = mPrimaryRB; michael@0: if (NeedsDepthStencilEmu(mContext->gl, InternalFormatForGL())) { michael@0: printf_stderr("DEV-ONLY: Using secondary buffer to emulate DepthStencil.\n"); michael@0: MOZ_ASSERT(mSecondaryRB); michael@0: stencilRB = mSecondaryRB; michael@0: } michael@0: gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_DEPTH_ATTACHMENT, LOCAL_GL_RENDERBUFFER, mPrimaryRB); michael@0: gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_STENCIL_ATTACHMENT, LOCAL_GL_RENDERBUFFER, stencilRB); michael@0: } michael@0: michael@0: GLint michael@0: WebGLRenderbuffer::GetRenderbufferParameter(GLenum target, GLenum pname) const { michael@0: GLContext* gl = mContext->gl; michael@0: michael@0: switch (pname) { michael@0: case LOCAL_GL_RENDERBUFFER_STENCIL_SIZE: { michael@0: if (NeedsDepthStencilEmu(mContext->gl, InternalFormatForGL())) { michael@0: if (gl->WorkAroundDriverBugs() && michael@0: gl->Renderer() == GLRenderer::Tegra) michael@0: { michael@0: return 8; michael@0: } michael@0: michael@0: ScopedBindRenderbuffer autoRB(gl, mSecondaryRB); michael@0: michael@0: GLint i = 0; michael@0: gl->fGetRenderbufferParameteriv(target, pname, &i); michael@0: return i; michael@0: } michael@0: // Fall through otherwise. michael@0: } michael@0: case LOCAL_GL_RENDERBUFFER_WIDTH: michael@0: case LOCAL_GL_RENDERBUFFER_HEIGHT: michael@0: case LOCAL_GL_RENDERBUFFER_RED_SIZE: michael@0: case LOCAL_GL_RENDERBUFFER_GREEN_SIZE: michael@0: case LOCAL_GL_RENDERBUFFER_BLUE_SIZE: michael@0: case LOCAL_GL_RENDERBUFFER_ALPHA_SIZE: michael@0: case LOCAL_GL_RENDERBUFFER_DEPTH_SIZE: { michael@0: GLint i = 0; michael@0: gl->fGetRenderbufferParameteriv(target, pname, &i); michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(false, "This function should only be called with valid `pname`."); michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLRenderbuffer) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLRenderbuffer, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLRenderbuffer, Release)