2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-11-26 07:25:25 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2014-07-11 04:43:01 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2013-12-01 02:20:57 +04:00
|
|
|
#include "GLContext.h"
|
2013-11-26 07:25:25 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
/* ScopedGLState - Wraps glEnable/glDisable. **********************************/
|
|
|
|
|
|
|
|
// Use |newState = true| to enable, |false| to disable.
|
|
|
|
ScopedGLState::ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL), mCapability(aCapability) {
|
2013-11-26 07:25:25 +04:00
|
|
|
mOldState = mGL->fIsEnabled(mCapability);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
// Early out if we're already in the right state.
|
|
|
|
if (aNewState == mOldState) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
if (aNewState) {
|
|
|
|
mGL->fEnable(mCapability);
|
|
|
|
} else {
|
|
|
|
mGL->fDisable(mCapability);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 00:02:18 +04:00
|
|
|
ScopedGLState::ScopedGLState(GLContext* aGL, GLenum aCapability)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL), mCapability(aCapability) {
|
2014-01-08 00:02:18 +04:00
|
|
|
mOldState = mGL->fIsEnabled(mCapability);
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedGLState::~ScopedGLState() {
|
2013-11-26 07:25:25 +04:00
|
|
|
if (mOldState) {
|
|
|
|
mGL->fEnable(mCapability);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fDisable(mCapability);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
/* ScopedBindFramebuffer - Saves and restores with GetUserBoundFB and
|
|
|
|
* BindUserFB. */
|
|
|
|
|
|
|
|
void ScopedBindFramebuffer::Init() {
|
2015-09-25 17:29:53 +03:00
|
|
|
if (mGL->IsSupported(GLFeature::split_framebuffer)) {
|
2014-05-14 04:14:53 +04:00
|
|
|
mOldReadFB = mGL->GetReadFB();
|
|
|
|
mOldDrawFB = mGL->GetDrawFB();
|
|
|
|
} else {
|
|
|
|
mOldReadFB = mOldDrawFB = mGL->GetFB();
|
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindFramebuffer::ScopedBindFramebuffer(GLContext* aGL) : mGL(aGL) {
|
2013-11-26 07:25:25 +04:00
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedBindFramebuffer::ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL) {
|
2013-11-26 07:25:25 +04:00
|
|
|
Init();
|
|
|
|
mGL->BindFB(aNewFB);
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindFramebuffer::~ScopedBindFramebuffer() {
|
2014-05-14 04:14:53 +04:00
|
|
|
if (mOldReadFB == mOldDrawFB) {
|
|
|
|
mGL->BindFB(mOldDrawFB);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2014-05-14 04:14:53 +04:00
|
|
|
mGL->BindDrawFB(mOldDrawFB);
|
|
|
|
mGL->BindReadFB(mOldReadFB);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
/* ScopedBindTextureUnit ******************************************************/
|
|
|
|
|
|
|
|
ScopedBindTextureUnit::ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL), mOldTexUnit(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
MOZ_ASSERT(aTexUnit >= LOCAL_GL_TEXTURE0);
|
|
|
|
mGL->GetUIntegerv(LOCAL_GL_ACTIVE_TEXTURE, &mOldTexUnit);
|
|
|
|
mGL->fActiveTexture(aTexUnit);
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindTextureUnit::~ScopedBindTextureUnit() {
|
|
|
|
mGL->fActiveTexture(mOldTexUnit);
|
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
/* ScopedTexture **************************************************************/
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedTexture::ScopedTexture(GLContext* aGL) : mGL(aGL), mTexture(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
mGL->fGenTextures(1, &mTexture);
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedTexture::~ScopedTexture() { mGL->fDeleteTextures(1, &mTexture); }
|
2013-11-26 07:25:25 +04:00
|
|
|
|
2014-05-01 01:30:21 +04:00
|
|
|
/* ScopedFramebuffer
|
|
|
|
* **************************************************************/
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedFramebuffer::ScopedFramebuffer(GLContext* aGL) : mGL(aGL), mFB(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
mGL->fGenFramebuffers(1, &mFB);
|
2014-05-01 01:30:21 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedFramebuffer::~ScopedFramebuffer() { mGL->fDeleteFramebuffers(1, &mFB); }
|
2014-05-01 01:30:21 +04:00
|
|
|
|
|
|
|
/* ScopedRenderbuffer
|
|
|
|
* **************************************************************/
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedRenderbuffer::ScopedRenderbuffer(GLContext* aGL) : mGL(aGL), mRB(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
mGL->fGenRenderbuffers(1, &mRB);
|
2014-05-01 01:30:21 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedRenderbuffer::~ScopedRenderbuffer() {
|
|
|
|
mGL->fDeleteRenderbuffers(1, &mRB);
|
|
|
|
}
|
2014-05-01 01:30:21 +04:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
/* ScopedBindTexture **********************************************************/
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
static GLuint GetBoundTexture(GLContext* gl, GLenum texTarget) {
|
|
|
|
GLenum bindingTarget;
|
|
|
|
switch (texTarget) {
|
|
|
|
case LOCAL_GL_TEXTURE_2D:
|
|
|
|
bindingTarget = LOCAL_GL_TEXTURE_BINDING_2D;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_TEXTURE_CUBE_MAP:
|
|
|
|
bindingTarget = LOCAL_GL_TEXTURE_BINDING_CUBE_MAP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_TEXTURE_3D:
|
|
|
|
bindingTarget = LOCAL_GL_TEXTURE_BINDING_3D;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_TEXTURE_2D_ARRAY:
|
|
|
|
bindingTarget = LOCAL_GL_TEXTURE_BINDING_2D_ARRAY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_TEXTURE_RECTANGLE_ARB:
|
|
|
|
bindingTarget = LOCAL_GL_TEXTURE_BINDING_RECTANGLE_ARB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_TEXTURE_EXTERNAL:
|
|
|
|
bindingTarget = LOCAL_GL_TEXTURE_BINDING_EXTERNAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("bad texTarget");
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint ret = 0;
|
|
|
|
gl->GetUIntegerv(bindingTarget, &ret);
|
2015-11-25 07:15:29 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
ScopedBindTexture::ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
|
|
|
|
GLenum aTarget)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL), mTarget(aTarget), mOldTex(GetBoundTexture(aGL, aTarget)) {
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fBindTexture(mTarget, aNewTex);
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindTexture::~ScopedBindTexture() { mGL->fBindTexture(mTarget, mOldTex); }
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
/* ScopedBindRenderbuffer *****************************************************/
|
|
|
|
|
|
|
|
void ScopedBindRenderbuffer::Init() {
|
|
|
|
mOldRB = 0;
|
|
|
|
mGL->GetUIntegerv(LOCAL_GL_RENDERBUFFER_BINDING, &mOldRB);
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindRenderbuffer::ScopedBindRenderbuffer(GLContext* aGL) : mGL(aGL) {
|
2013-11-26 07:25:25 +04:00
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedBindRenderbuffer::ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL) {
|
2013-11-26 07:25:25 +04:00
|
|
|
Init();
|
|
|
|
mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, aNewRB);
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindRenderbuffer::~ScopedBindRenderbuffer() {
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mOldRB);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ScopedFramebufferForTexture ************************************************/
|
|
|
|
ScopedFramebufferForTexture::ScopedFramebufferForTexture(GLContext* aGL,
|
|
|
|
GLuint aTexture,
|
|
|
|
GLenum aTarget)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL), mComplete(false), mFB(0) {
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fGenFramebuffers(1, &mFB);
|
|
|
|
ScopedBindFramebuffer autoFB(aGL, mFB);
|
|
|
|
mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
aTarget, aTexture, 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
if (status == LOCAL_GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
mComplete = true;
|
|
|
|
} else {
|
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
mFB = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedFramebufferForTexture::~ScopedFramebufferForTexture() {
|
2013-11-26 07:25:25 +04:00
|
|
|
if (!mFB) return;
|
|
|
|
|
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
mFB = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ScopedFramebufferForRenderbuffer *******************************************/
|
|
|
|
|
|
|
|
ScopedFramebufferForRenderbuffer::ScopedFramebufferForRenderbuffer(
|
|
|
|
GLContext* aGL, GLuint aRB)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL), mComplete(false), mFB(0) {
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fGenFramebuffers(1, &mFB);
|
|
|
|
ScopedBindFramebuffer autoFB(aGL, mFB);
|
|
|
|
mGL->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
|
|
|
|
LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
LOCAL_GL_RENDERBUFFER, aRB);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
if (status == LOCAL_GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
mComplete = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
mFB = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedFramebufferForRenderbuffer::~ScopedFramebufferForRenderbuffer() {
|
2013-11-26 07:25:25 +04:00
|
|
|
if (!mFB) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
2018-11-30 13:46:48 +03:00
|
|
|
mFB = 0;
|
2013-11-26 07:25:25 +04:00
|
|
|
}
|
|
|
|
|
2014-01-08 00:02:18 +04:00
|
|
|
/* ScopedViewportRect *********************************************************/
|
|
|
|
|
|
|
|
ScopedViewportRect::ScopedViewportRect(GLContext* aGL, GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL) {
|
2015-11-25 07:15:29 +03:00
|
|
|
mGL->fGetIntegerv(LOCAL_GL_VIEWPORT, mSavedViewportRect);
|
|
|
|
mGL->fViewport(x, y, width, height);
|
2014-01-08 00:02:18 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedViewportRect::~ScopedViewportRect() {
|
2015-11-25 07:15:29 +03:00
|
|
|
mGL->fViewport(mSavedViewportRect[0], mSavedViewportRect[1],
|
|
|
|
mSavedViewportRect[2], mSavedViewportRect[3]);
|
2014-01-08 00:02:18 +04:00
|
|
|
}
|
|
|
|
|
2014-01-08 00:02:18 +04:00
|
|
|
/* ScopedScissorRect **********************************************************/
|
|
|
|
|
|
|
|
ScopedScissorRect::ScopedScissorRect(GLContext* aGL, GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL) {
|
2015-11-25 07:15:29 +03:00
|
|
|
mGL->fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mSavedScissorRect);
|
|
|
|
mGL->fScissor(x, y, width, height);
|
2014-01-08 00:02:18 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedScissorRect::ScopedScissorRect(GLContext* aGL) : mGL(aGL) {
|
2015-11-25 07:15:29 +03:00
|
|
|
mGL->fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mSavedScissorRect);
|
2014-01-08 00:02:18 +04:00
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedScissorRect::~ScopedScissorRect() {
|
2015-11-25 07:15:29 +03:00
|
|
|
mGL->fScissor(mSavedScissorRect[0], mSavedScissorRect[1],
|
|
|
|
mSavedScissorRect[2], mSavedScissorRect[3]);
|
2014-01-08 00:02:18 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
/* ScopedVertexAttribPointer **************************************************/
|
|
|
|
|
|
|
|
ScopedVertexAttribPointer::ScopedVertexAttribPointer(
|
|
|
|
GLContext* aGL, GLuint index, GLint size, GLenum type,
|
|
|
|
realGLboolean normalized, GLsizei stride, GLuint buffer,
|
|
|
|
const GLvoid* pointer)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL),
|
2018-07-06 10:56:24 +03:00
|
|
|
mAttribEnabled(0),
|
|
|
|
mAttribSize(0),
|
|
|
|
mAttribStride(0),
|
|
|
|
mAttribType(0),
|
|
|
|
mAttribNormalized(0),
|
|
|
|
mAttribBufferBinding(0),
|
|
|
|
mAttribPointer(nullptr),
|
|
|
|
mBoundBuffer(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
WrapImpl(index);
|
|
|
|
mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, buffer);
|
|
|
|
mGL->fVertexAttribPointer(index, size, type, normalized, stride, pointer);
|
|
|
|
mGL->fEnableVertexAttribArray(index);
|
2014-05-29 18:19:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedVertexAttribPointer::ScopedVertexAttribPointer(GLContext* aGL,
|
|
|
|
GLuint index)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(aGL),
|
2018-07-06 10:56:24 +03:00
|
|
|
mAttribEnabled(0),
|
|
|
|
mAttribSize(0),
|
|
|
|
mAttribStride(0),
|
|
|
|
mAttribType(0),
|
|
|
|
mAttribNormalized(0),
|
|
|
|
mAttribBufferBinding(0),
|
|
|
|
mAttribPointer(nullptr),
|
|
|
|
mBoundBuffer(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
WrapImpl(index);
|
2014-05-29 18:19:48 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
void ScopedVertexAttribPointer::WrapImpl(GLuint index) {
|
|
|
|
mAttribIndex = index;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
/*
|
|
|
|
* mGL->fGetVertexAttribiv takes:
|
|
|
|
* VERTEX_ATTRIB_ARRAY_ENABLED
|
|
|
|
* VERTEX_ATTRIB_ARRAY_SIZE,
|
|
|
|
* VERTEX_ATTRIB_ARRAY_STRIDE,
|
|
|
|
* VERTEX_ATTRIB_ARRAY_TYPE,
|
|
|
|
* VERTEX_ATTRIB_ARRAY_NORMALIZED,
|
|
|
|
* VERTEX_ATTRIB_ARRAY_BUFFER_BINDING,
|
|
|
|
* CURRENT_VERTEX_ATTRIB
|
|
|
|
*
|
|
|
|
* CURRENT_VERTEX_ATTRIB is vertex shader state. \o/
|
|
|
|
* Others appear to be vertex array state,
|
|
|
|
* or alternatively in the internal vertex array state
|
|
|
|
* for a buffer object.
|
|
|
|
*/
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
mGL->fGetVertexAttribiv(mAttribIndex, LOCAL_GL_VERTEX_ATTRIB_ARRAY_ENABLED,
|
|
|
|
&mAttribEnabled);
|
|
|
|
mGL->fGetVertexAttribiv(mAttribIndex, LOCAL_GL_VERTEX_ATTRIB_ARRAY_SIZE,
|
|
|
|
&mAttribSize);
|
|
|
|
mGL->fGetVertexAttribiv(mAttribIndex, LOCAL_GL_VERTEX_ATTRIB_ARRAY_STRIDE,
|
|
|
|
&mAttribStride);
|
|
|
|
mGL->fGetVertexAttribiv(mAttribIndex, LOCAL_GL_VERTEX_ATTRIB_ARRAY_TYPE,
|
|
|
|
&mAttribType);
|
|
|
|
mGL->fGetVertexAttribiv(mAttribIndex, LOCAL_GL_VERTEX_ATTRIB_ARRAY_NORMALIZED,
|
|
|
|
&mAttribNormalized);
|
|
|
|
mGL->fGetVertexAttribiv(mAttribIndex,
|
|
|
|
LOCAL_GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING,
|
|
|
|
&mAttribBufferBinding);
|
|
|
|
mGL->fGetVertexAttribPointerv(
|
|
|
|
mAttribIndex, LOCAL_GL_VERTEX_ATTRIB_ARRAY_POINTER, &mAttribPointer);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
// Note that uniform values are program state, so we don't need to rebind
|
|
|
|
// those.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
mGL->GetUIntegerv(LOCAL_GL_ARRAY_BUFFER_BINDING, &mBoundBuffer);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedVertexAttribPointer::~ScopedVertexAttribPointer() {
|
2014-05-29 18:19:48 +04:00
|
|
|
mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mAttribBufferBinding);
|
|
|
|
mGL->fVertexAttribPointer(mAttribIndex, mAttribSize, mAttribType,
|
|
|
|
mAttribNormalized, mAttribStride, mAttribPointer);
|
|
|
|
if (mAttribEnabled)
|
|
|
|
mGL->fEnableVertexAttribArray(mAttribIndex);
|
|
|
|
else
|
|
|
|
mGL->fDisableVertexAttribArray(mAttribIndex);
|
|
|
|
mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mBoundBuffer);
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:01:51 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2016-06-14 20:07:55 +03:00
|
|
|
// ScopedPackState
|
2014-10-08 08:01:51 +04:00
|
|
|
|
2016-06-14 20:07:55 +03:00
|
|
|
ScopedPackState::ScopedPackState(GLContext* gl)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(gl),
|
2018-07-06 10:56:24 +03:00
|
|
|
mAlignment(0),
|
|
|
|
mPixelBuffer(0),
|
|
|
|
mRowLength(0),
|
|
|
|
mSkipPixels(0),
|
|
|
|
mSkipRows(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
mGL->fGetIntegerv(LOCAL_GL_PACK_ALIGNMENT, &mAlignment);
|
2014-10-08 08:01:51 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
if (mAlignment != 4) mGL->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, 4);
|
2016-06-14 20:07:55 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
if (!mGL->HasPBOState()) return;
|
2016-06-14 20:07:55 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
mGL->fGetIntegerv(LOCAL_GL_PIXEL_PACK_BUFFER_BINDING, (GLint*)&mPixelBuffer);
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_PACK_ROW_LENGTH, &mRowLength);
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_PACK_SKIP_PIXELS, &mSkipPixels);
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_PACK_SKIP_ROWS, &mSkipRows);
|
2016-06-14 20:07:55 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
if (mPixelBuffer != 0) mGL->fBindBuffer(LOCAL_GL_PIXEL_PACK_BUFFER, 0);
|
|
|
|
if (mRowLength != 0) mGL->fPixelStorei(LOCAL_GL_PACK_ROW_LENGTH, 0);
|
|
|
|
if (mSkipPixels != 0) mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_PIXELS, 0);
|
|
|
|
if (mSkipRows != 0) mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_ROWS, 0);
|
2014-10-08 08:01:51 +04:00
|
|
|
}
|
|
|
|
|
2019-03-29 23:18:53 +03:00
|
|
|
bool ScopedPackState::SetForWidthAndStrideRGBA(GLsizei aWidth,
|
|
|
|
GLsizei aStride) {
|
|
|
|
MOZ_ASSERT(aStride % 4 == 0, "RGBA data should always be 4-byte aligned");
|
|
|
|
MOZ_ASSERT(aStride / 4 >= aWidth, "Stride too small");
|
|
|
|
if (aStride / 4 == aWidth) {
|
|
|
|
// No special handling needed.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mGL->HasPBOState()) {
|
|
|
|
// HasPBOState implies support for GL_PACK_ROW_LENGTH.
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_PACK_ROW_LENGTH, aStride / 4);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedPackState::~ScopedPackState() {
|
2016-06-14 20:07:55 +03:00
|
|
|
mGL->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, mAlignment);
|
|
|
|
|
2016-12-30 02:17:23 +03:00
|
|
|
if (!mGL->HasPBOState()) return;
|
2016-06-14 20:07:55 +03:00
|
|
|
|
|
|
|
mGL->fBindBuffer(LOCAL_GL_PIXEL_PACK_BUFFER, mPixelBuffer);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_PACK_ROW_LENGTH, mRowLength);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_PIXELS, mSkipPixels);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_ROWS, mSkipRows);
|
2014-10-08 08:01:51 +04:00
|
|
|
}
|
|
|
|
|
2017-04-12 19:22:28 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// ResetUnpackState
|
|
|
|
|
|
|
|
ResetUnpackState::ResetUnpackState(GLContext* gl)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(gl),
|
2018-07-06 10:56:24 +03:00
|
|
|
mAlignment(0),
|
|
|
|
mPBO(0),
|
|
|
|
mRowLength(0),
|
|
|
|
mImageHeight(0),
|
|
|
|
mSkipPixels(0),
|
|
|
|
mSkipRows(0),
|
|
|
|
mSkipImages(0) {
|
2018-04-13 16:01:28 +03:00
|
|
|
const auto fnReset = [&](GLenum pname, GLuint val, GLuint* const out_old) {
|
|
|
|
mGL->GetUIntegerv(pname, out_old);
|
|
|
|
if (*out_old != val) {
|
|
|
|
mGL->fPixelStorei(pname, val);
|
|
|
|
}
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
// Default is 4, but 1 is more useful.
|
|
|
|
fnReset(LOCAL_GL_UNPACK_ALIGNMENT, 1, &mAlignment);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
if (!mGL->HasPBOState()) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
mGL->GetUIntegerv(LOCAL_GL_PIXEL_UNPACK_BUFFER_BINDING, &mPBO);
|
|
|
|
if (mPBO != 0) mGL->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
fnReset(LOCAL_GL_UNPACK_ROW_LENGTH, 0, &mRowLength);
|
|
|
|
fnReset(LOCAL_GL_UNPACK_IMAGE_HEIGHT, 0, &mImageHeight);
|
|
|
|
fnReset(LOCAL_GL_UNPACK_SKIP_PIXELS, 0, &mSkipPixels);
|
|
|
|
fnReset(LOCAL_GL_UNPACK_SKIP_ROWS, 0, &mSkipRows);
|
|
|
|
fnReset(LOCAL_GL_UNPACK_SKIP_IMAGES, 0, &mSkipImages);
|
2017-04-12 19:22:28 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ResetUnpackState::~ResetUnpackState() {
|
2017-04-12 19:22:28 +03:00
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, mAlignment);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 19:22:28 +03:00
|
|
|
if (!mGL->HasPBOState()) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 19:22:28 +03:00
|
|
|
mGL->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, mPBO);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 19:22:28 +03:00
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, mRowLength);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT, mImageHeight);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_PIXELS, mSkipPixels);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS, mSkipRows);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES, mSkipImages);
|
|
|
|
}
|
|
|
|
|
2016-12-30 02:17:23 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// ScopedBindPBO
|
|
|
|
|
|
|
|
static GLuint GetPBOBinding(GLContext* gl, GLenum target) {
|
|
|
|
if (!gl->HasPBOState()) return 0;
|
|
|
|
|
|
|
|
GLenum targetBinding;
|
|
|
|
switch (target) {
|
|
|
|
case LOCAL_GL_PIXEL_PACK_BUFFER:
|
|
|
|
targetBinding = LOCAL_GL_PIXEL_PACK_BUFFER_BINDING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_PIXEL_UNPACK_BUFFER:
|
|
|
|
targetBinding = LOCAL_GL_PIXEL_UNPACK_BUFFER_BINDING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
return gl->GetIntAs<GLuint>(targetBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedBindPBO::ScopedBindPBO(GLContext* gl, GLenum target)
|
2019-06-17 09:40:27 +03:00
|
|
|
: mGL(gl), mTarget(target), mPBO(GetPBOBinding(mGL, mTarget)) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
ScopedBindPBO::~ScopedBindPBO() {
|
2016-12-30 02:17:23 +03:00
|
|
|
if (!mGL->HasPBOState()) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-30 02:17:23 +03:00
|
|
|
mGL->fBindBuffer(mTarget, mPBO);
|
|
|
|
}
|
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|