From c4e65732befdfb675111387c3c7edb616bf2f976 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Mon, 4 Jan 2016 10:18:15 +0100 Subject: OPENGL: Introduce abstraction for framebuffer. This allows us to use various framebuffer settings easily. Now the GPU accelerated CLUT8 surface implementation does not need to query former framebuffer state anymore. --- backends/graphics/opengl/framebuffer.cpp | 251 +++++++++++++++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 backends/graphics/opengl/framebuffer.cpp (limited to 'backends/graphics/opengl/framebuffer.cpp') diff --git a/backends/graphics/opengl/framebuffer.cpp b/backends/graphics/opengl/framebuffer.cpp new file mode 100644 index 0000000000..cca00bab03 --- /dev/null +++ b/backends/graphics/opengl/framebuffer.cpp @@ -0,0 +1,251 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "backends/graphics/opengl/framebuffer.h" +#include "backends/graphics/opengl/texture.h" + +namespace OpenGL { + +Framebuffer::Framebuffer() + : _viewport(), _projectionMatrix(), _isActive(false), _clearColor(), + _blendState(false), _scissorTestState(false), _scissorBox() { +} + +void Framebuffer::activate() { + _isActive = true; + + applyViewport(); + applyClearColor(); + applyBlendState(); + applyScissorTestState(); + applyScissorBox(); +} + +void Framebuffer::deactivate() { + _isActive = false; +} + +void Framebuffer::setClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { + _clearColor[0] = r; + _clearColor[1] = g; + _clearColor[2] = b; + _clearColor[3] = a; + + // Directly apply changes when we are active. + if (isActive()) { + applyClearColor(); + } +} + +void Framebuffer::enableBlend(bool enable) { + _blendState = enable; + + // Directly apply changes when we are active. + if (isActive()) { + applyBlendState(); + } +} + +void Framebuffer::enableScissorTest(bool enable) { + _scissorTestState = enable; + + // Directly apply changes when we are active. + if (isActive()) { + applyScissorTestState(); + } +} + +void Framebuffer::setScissorBox(GLint x, GLint y, GLsizei w, GLsizei h) { + _scissorBox[0] = x; + _scissorBox[1] = y; + _scissorBox[2] = w; + _scissorBox[3] = h; + + // Directly apply changes when we are active. + if (isActive()) { + applyScissorBox(); + } +} + +void Framebuffer::applyViewport() { + GL_CALL(glViewport(_viewport[0], _viewport[1], _viewport[2], _viewport[3])); +} + +void Framebuffer::applyClearColor() { + GL_CALL(glClearColor(_clearColor[0], _clearColor[1], _clearColor[2], _clearColor[3])); +} + +void Framebuffer::applyBlendState() { + if (_blendState) { + GL_CALL(glEnable(GL_BLEND)); + } else { + GL_CALL(glDisable(GL_BLEND)); + } +} + +void Framebuffer::applyScissorTestState() { + if (_scissorTestState) { + GL_CALL(glEnable(GL_SCISSOR_TEST)); + } else { + GL_CALL(glDisable(GL_SCISSOR_TEST)); + } +} + +void Framebuffer::applyScissorBox() { + GL_CALL(glScissor(_scissorBox[0], _scissorBox[1], _scissorBox[2], _scissorBox[3])); +} + +// +// Backbuffer implementation +// + +void Backbuffer::activate() { + Framebuffer::activate(); + +#if !USE_FORCED_GLES + if (g_context.framebufferObjectSupported) { + GL_CALL(glBindFramebuffer(GL_FRAMEBUFFER, 0)); + } +#endif +} + +void Backbuffer::setDimensions(uint width, uint height) { + // Set viewport dimensions. + _viewport[0] = 0; + _viewport[1] = 0; + _viewport[2] = width; + _viewport[3] = height; + + // Setup orthogonal projection matrix. + _projectionMatrix[ 0] = 2.0f / width; + _projectionMatrix[ 1] = 0.0f; + _projectionMatrix[ 2] = 0.0f; + _projectionMatrix[ 3] = 0.0f; + + _projectionMatrix[ 4] = 0.0f; + _projectionMatrix[ 5] = -2.0f / height; + _projectionMatrix[ 6] = 0.0f; + _projectionMatrix[ 7] = 0.0f; + + _projectionMatrix[ 8] = 0.0f; + _projectionMatrix[ 9] = 0.0f; + _projectionMatrix[10] = 0.0f; + _projectionMatrix[11] = 0.0f; + + _projectionMatrix[12] = -1.0f; + _projectionMatrix[13] = 1.0f; + _projectionMatrix[14] = 0.0f; + _projectionMatrix[15] = 1.0f; + + // Directly apply changes when we are active. + if (isActive()) { + applyViewport(); + } +} + +// +// Render to texture target implementation +// + +#if !USE_FORCED_GLES +TextureTarget::TextureTarget() + : _texture(new GLTexture(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE)), _glFBO(0), _needUpdate(true) { +} + +TextureTarget::~TextureTarget() { + delete _texture; + GL_CALL_SAFE(glDeleteFramebuffers, (1, &_glFBO)); +} + +void TextureTarget::activate() { + Framebuffer::activate(); + + // Allocate framebuffer object if necessary. + if (!_glFBO) { + GL_CALL(glGenFramebuffers(1, &_glFBO)); + _needUpdate = true; + } + + // Attach destination texture to FBO. + GL_CALL(glBindFramebuffer(GL_FRAMEBUFFER, _glFBO)); + + // If required attach texture to FBO. + if (_needUpdate) { + GL_CALL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture->getGLTexture(), 0)); + _needUpdate = false; + } +} + +void TextureTarget::destroy() { + GL_CALL(glDeleteFramebuffers(1, &_glFBO)); + _glFBO = 0; + + _texture->destroy(); +} + +void TextureTarget::create() { + _texture->create(); + + _needUpdate = true; +} + +void TextureTarget::setSize(uint width, uint height) { + _texture->setSize(width, height); + + const uint texWidth = _texture->getWidth(); + const uint texHeight = _texture->getHeight(); + + // Set viewport dimensions. + _viewport[0] = 0; + _viewport[1] = 0; + _viewport[2] = texWidth; + _viewport[3] = texHeight; + + // Setup orthogonal projection matrix. + _projectionMatrix[ 0] = 2.0f / texWidth; + _projectionMatrix[ 1] = 0.0f; + _projectionMatrix[ 2] = 0.0f; + _projectionMatrix[ 3] = 0.0f; + + _projectionMatrix[ 4] = 0.0f; + _projectionMatrix[ 5] = 2.0f / texHeight; + _projectionMatrix[ 6] = 0.0f; + _projectionMatrix[ 7] = 0.0f; + + _projectionMatrix[ 8] = 0.0f; + _projectionMatrix[ 9] = 0.0f; + _projectionMatrix[10] = 0.0f; + _projectionMatrix[11] = 0.0f; + + _projectionMatrix[12] = -1.0f; + _projectionMatrix[13] = -1.0f; + _projectionMatrix[14] = 0.0f; + _projectionMatrix[15] = 1.0f; + + // Directly apply changes when we are active. + if (isActive()) { + applyViewport(); + } +} +#endif // !USE_FORCED_GLES + +} // End of namespace OpenGL -- cgit v1.2.3 From 0fe580d10c6fb73a90eccb046c8dcbf84b062b16 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Mon, 4 Jan 2016 11:38:21 +0100 Subject: OPENGL: Make shader/framebuffer part of pipeline state. --- backends/graphics/opengl/framebuffer.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'backends/graphics/opengl/framebuffer.cpp') diff --git a/backends/graphics/opengl/framebuffer.cpp b/backends/graphics/opengl/framebuffer.cpp index cca00bab03..85245b683c 100644 --- a/backends/graphics/opengl/framebuffer.cpp +++ b/backends/graphics/opengl/framebuffer.cpp @@ -22,6 +22,7 @@ #include "backends/graphics/opengl/framebuffer.h" #include "backends/graphics/opengl/texture.h" +#include "backends/graphics/opengl/pipeline.h" namespace OpenGL { @@ -34,6 +35,7 @@ void Framebuffer::activate() { _isActive = true; applyViewport(); + applyProjectionMatrix(); applyClearColor(); applyBlendState(); applyScissorTestState(); @@ -90,6 +92,10 @@ void Framebuffer::applyViewport() { GL_CALL(glViewport(_viewport[0], _viewport[1], _viewport[2], _viewport[3])); } +void Framebuffer::applyProjectionMatrix() { + g_context.activePipeline->setProjectionMatrix(_projectionMatrix); +} + void Framebuffer::applyClearColor() { GL_CALL(glClearColor(_clearColor[0], _clearColor[1], _clearColor[2], _clearColor[3])); } @@ -159,6 +165,7 @@ void Backbuffer::setDimensions(uint width, uint height) { // Directly apply changes when we are active. if (isActive()) { applyViewport(); + applyProjectionMatrix(); } } @@ -244,6 +251,7 @@ void TextureTarget::setSize(uint width, uint height) { // Directly apply changes when we are active. if (isActive()) { applyViewport(); + applyProjectionMatrix(); } } #endif // !USE_FORCED_GLES -- cgit v1.2.3 From ed6689d4fcfd30a36cb01392bbd9705732cfac4a Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 28 Feb 2016 17:02:19 +0100 Subject: OPENGL: Do not allow direct access to Context::activePipeline. --- backends/graphics/opengl/framebuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'backends/graphics/opengl/framebuffer.cpp') diff --git a/backends/graphics/opengl/framebuffer.cpp b/backends/graphics/opengl/framebuffer.cpp index 85245b683c..6775360e00 100644 --- a/backends/graphics/opengl/framebuffer.cpp +++ b/backends/graphics/opengl/framebuffer.cpp @@ -93,7 +93,7 @@ void Framebuffer::applyViewport() { } void Framebuffer::applyProjectionMatrix() { - g_context.activePipeline->setProjectionMatrix(_projectionMatrix); + g_context.getActivePipeline()->setProjectionMatrix(_projectionMatrix); } void Framebuffer::applyClearColor() { -- cgit v1.2.3 From bec2088d6c312d15e4c3377b5afbcb5a6fb0a84e Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 28 Feb 2016 17:10:02 +0100 Subject: OPENGL: Only allow Pipeline to switch active Framebuffers. --- backends/graphics/opengl/framebuffer.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'backends/graphics/opengl/framebuffer.cpp') diff --git a/backends/graphics/opengl/framebuffer.cpp b/backends/graphics/opengl/framebuffer.cpp index 6775360e00..f3332cb469 100644 --- a/backends/graphics/opengl/framebuffer.cpp +++ b/backends/graphics/opengl/framebuffer.cpp @@ -40,10 +40,14 @@ void Framebuffer::activate() { applyBlendState(); applyScissorTestState(); applyScissorBox(); + + activateInternal(); } void Framebuffer::deactivate() { _isActive = false; + + deactivateInternal(); } void Framebuffer::setClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { @@ -124,9 +128,7 @@ void Framebuffer::applyScissorBox() { // Backbuffer implementation // -void Backbuffer::activate() { - Framebuffer::activate(); - +void Backbuffer::activateInternal() { #if !USE_FORCED_GLES if (g_context.framebufferObjectSupported) { GL_CALL(glBindFramebuffer(GL_FRAMEBUFFER, 0)); @@ -183,9 +185,7 @@ TextureTarget::~TextureTarget() { GL_CALL_SAFE(glDeleteFramebuffers, (1, &_glFBO)); } -void TextureTarget::activate() { - Framebuffer::activate(); - +void TextureTarget::activateInternal() { // Allocate framebuffer object if necessary. if (!_glFBO) { GL_CALL(glGenFramebuffers(1, &_glFBO)); -- cgit v1.2.3 From 8a4938f82b05b86c8f0ed010210eb6d1847c04c9 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 28 Feb 2016 18:15:00 +0100 Subject: OPENGL: Move pipeline code to pipelines/. --- backends/graphics/opengl/framebuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'backends/graphics/opengl/framebuffer.cpp') diff --git a/backends/graphics/opengl/framebuffer.cpp b/backends/graphics/opengl/framebuffer.cpp index f3332cb469..7191aab8bc 100644 --- a/backends/graphics/opengl/framebuffer.cpp +++ b/backends/graphics/opengl/framebuffer.cpp @@ -22,7 +22,7 @@ #include "backends/graphics/opengl/framebuffer.h" #include "backends/graphics/opengl/texture.h" -#include "backends/graphics/opengl/pipeline.h" +#include "backends/graphics/opengl/pipelines/pipeline.h" namespace OpenGL { -- cgit v1.2.3