2017-01-12 08:20:04 +00:00
|
|
|
/*
|
2013-07-23 16:39:07 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-01-13 15:48:23 +00:00
|
|
|
* Copyright (C) 2013-2017 CERN
|
2021-02-16 22:25:27 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-07-23 16:39:07 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2014-02-24 10:17:49 +00:00
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
2013-07-23 16:39:07 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file opengl_compositor.cpp
|
2015-02-15 01:18:35 +00:00
|
|
|
* @brief Class that handles multitarget rendering (i.e. to different textures/surfaces) and
|
2013-07-23 16:39:07 +00:00
|
|
|
* later compositing into a single image (OpenGL flavour).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/opengl/opengl_compositor.h>
|
2016-05-02 13:56:16 +00:00
|
|
|
#include <gal/opengl/utils.h>
|
2015-02-15 01:18:35 +00:00
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
#include <gal/color4d.h>
|
|
|
|
|
2015-02-15 01:18:35 +00:00
|
|
|
#include <cassert>
|
2019-12-05 13:43:55 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
2021-06-01 05:17:57 +00:00
|
|
|
#include <wx/log.h>
|
2021-10-03 12:58:56 +00:00
|
|
|
#include <wx/debug.h>
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
OPENGL_COMPOSITOR::OPENGL_COMPOSITOR() :
|
2021-02-16 22:25:27 +00:00
|
|
|
m_initialized( false ),
|
|
|
|
m_curBuffer( 0 ),
|
|
|
|
m_mainFbo( 0 ),
|
|
|
|
m_depthBuffer( 0 ),
|
|
|
|
m_curFbo( DIRECT_RENDERING ),
|
|
|
|
m_currentAntialiasingMode( OPENGL_ANTIALIASING_MODE::NONE )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2019-12-05 13:43:55 +00:00
|
|
|
m_antialiasing = std::make_unique<ANTIALIASING_NONE>( this );
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OPENGL_COMPOSITOR::~OPENGL_COMPOSITOR()
|
|
|
|
{
|
|
|
|
if( m_initialized )
|
2021-02-24 00:09:41 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
clean();
|
|
|
|
}
|
|
|
|
catch( const std::runtime_error& exc )
|
|
|
|
{
|
|
|
|
wxLogError( wxT( "Run time exception `%s` occurred in OPENGL_COMPOSITOR destructor." ),
|
|
|
|
exc.what() );
|
|
|
|
}
|
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-23 15:21:00 +00:00
|
|
|
void OPENGL_COMPOSITOR::SetAntialiasingMode( OPENGL_ANTIALIASING_MODE aMode )
|
|
|
|
{
|
|
|
|
m_currentAntialiasingMode = aMode;
|
2017-01-13 09:36:59 +00:00
|
|
|
|
|
|
|
if( m_initialized )
|
2016-12-23 15:21:00 +00:00
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-23 15:21:00 +00:00
|
|
|
OPENGL_ANTIALIASING_MODE OPENGL_COMPOSITOR::GetAntialiasingMode() const
|
|
|
|
{
|
|
|
|
return m_currentAntialiasingMode;
|
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
void OPENGL_COMPOSITOR::Initialize()
|
|
|
|
{
|
|
|
|
if( m_initialized )
|
|
|
|
return;
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
switch( m_currentAntialiasingMode )
|
|
|
|
{
|
2021-06-09 00:20:41 +00:00
|
|
|
case OPENGL_ANTIALIASING_MODE::SMAA:
|
|
|
|
m_antialiasing = std::make_unique<ANTIALIASING_SMAA>( this );
|
2021-02-16 22:25:27 +00:00
|
|
|
break;
|
2021-06-09 00:20:41 +00:00
|
|
|
case OPENGL_ANTIALIASING_MODE::SUPERSAMPLING:
|
|
|
|
m_antialiasing = std::make_unique<ANTIALIASING_SUPERSAMPLING>( this );
|
2021-02-16 22:25:27 +00:00
|
|
|
break;
|
2021-06-09 00:20:41 +00:00
|
|
|
default:
|
|
|
|
m_antialiasing = std::make_unique<ANTIALIASING_NONE>( this );
|
2021-02-16 22:25:27 +00:00
|
|
|
break;
|
2016-12-23 15:21:00 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 20:48:07 +00:00
|
|
|
VECTOR2I dims = m_antialiasing->GetInternalBufferSize();
|
2017-01-13 15:48:23 +00:00
|
|
|
assert( dims.x != 0 && dims.y != 0 );
|
|
|
|
|
|
|
|
GLint maxBufSize;
|
|
|
|
glGetIntegerv( GL_MAX_RENDERBUFFER_SIZE_EXT, &maxBufSize );
|
|
|
|
|
2023-06-28 20:48:07 +00:00
|
|
|
if( dims.x < 0 || dims.y < 0 || dims.x > maxBufSize || dims.y >= maxBufSize )
|
2017-01-13 15:59:31 +00:00
|
|
|
throw std::runtime_error( "Requested render buffer size is not supported" );
|
2016-12-22 17:58:29 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
// We need framebuffer objects for drawing the screen contents
|
|
|
|
// Generate framebuffer and a depth buffer
|
2016-05-02 13:56:16 +00:00
|
|
|
glGenFramebuffersEXT( 1, &m_mainFbo );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "generating framebuffer", __FILE__, __LINE__ );
|
2016-05-02 13:56:16 +00:00
|
|
|
bindFb( m_mainFbo );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Allocate memory for the depth buffer
|
|
|
|
// Attach the depth buffer to the framebuffer
|
2013-10-29 16:28:29 +00:00
|
|
|
glGenRenderbuffersEXT( 1, &m_depthBuffer );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "generating renderbuffer", __FILE__, __LINE__ );
|
2013-10-29 16:28:29 +00:00
|
|
|
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_depthBuffer );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "binding renderbuffer", __FILE__, __LINE__ );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, dims.x, dims.y );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "creating renderbuffer storage", __FILE__, __LINE__ );
|
2016-05-02 14:15:24 +00:00
|
|
|
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT,
|
2014-07-09 09:22:42 +00:00
|
|
|
GL_RENDERBUFFER_EXT, m_depthBuffer );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "attaching renderbuffer", __FILE__, __LINE__ );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Unbind the framebuffer, so by default all the rendering goes directly to the display
|
2016-05-02 13:56:16 +00:00
|
|
|
bindFb( DIRECT_RENDERING );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
m_initialized = true;
|
2016-12-22 19:02:34 +00:00
|
|
|
|
|
|
|
m_antialiasing->Init();
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::Resize( unsigned int aWidth, unsigned int aHeight )
|
|
|
|
{
|
|
|
|
if( m_initialized )
|
|
|
|
clean();
|
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
m_antialiasing->OnLostBuffers();
|
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
m_width = aWidth;
|
2013-07-23 16:39:07 +00:00
|
|
|
m_height = aHeight;
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
unsigned int OPENGL_COMPOSITOR::CreateBuffer()
|
2016-12-22 17:58:29 +00:00
|
|
|
{
|
|
|
|
return m_antialiasing->CreateBuffer();
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2023-06-28 20:48:07 +00:00
|
|
|
unsigned int OPENGL_COMPOSITOR::CreateBuffer( VECTOR2I aDimensions )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2015-02-15 01:18:35 +00:00
|
|
|
assert( m_initialized );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2016-05-02 13:56:16 +00:00
|
|
|
int maxBuffers, maxTextureSize;
|
2014-07-09 09:22:42 +00:00
|
|
|
|
2014-11-13 15:37:15 +00:00
|
|
|
// Get the maximum number of buffers
|
2014-07-09 09:22:42 +00:00
|
|
|
glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS, (GLint*) &maxBuffers );
|
|
|
|
|
2016-05-02 13:56:16 +00:00
|
|
|
if( (int) usedBuffers() >= maxBuffers )
|
2013-10-14 09:39:21 +00:00
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
throw std::runtime_error( "Cannot create more framebuffers. OpenGL rendering backend "
|
|
|
|
"requires at least 3 framebuffers. You may try to update/change "
|
|
|
|
"your graphic drivers." );
|
2016-05-02 13:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*) &maxTextureSize );
|
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
if( maxTextureSize < (int) aDimensions.x || maxTextureSize < (int) aDimensions.y )
|
2016-05-02 13:56:16 +00:00
|
|
|
{
|
|
|
|
throw std::runtime_error( "Requested texture size is not supported. "
|
2021-02-16 22:25:27 +00:00
|
|
|
"Could not create a buffer." );
|
2013-10-14 09:39:21 +00:00
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// GL_COLOR_ATTACHMENTn are consecutive integers
|
|
|
|
GLuint attachmentPoint = GL_COLOR_ATTACHMENT0 + usedBuffers();
|
|
|
|
GLuint textureTarget;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Generate the texture for the pixel storage
|
2016-12-22 17:58:29 +00:00
|
|
|
glActiveTexture( GL_TEXTURE0 );
|
2013-07-24 13:06:59 +00:00
|
|
|
glGenTextures( 1, &textureTarget );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "generating framebuffer texture target", __FILE__, __LINE__ );
|
2013-07-24 13:06:59 +00:00
|
|
|
glBindTexture( GL_TEXTURE_2D, textureTarget );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "binding framebuffer texture target", __FILE__, __LINE__ );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Set texture parameters
|
|
|
|
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
|
2016-12-22 17:58:29 +00:00
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, aDimensions.x, aDimensions.y, 0, GL_RGBA,
|
2021-07-15 19:26:35 +00:00
|
|
|
GL_UNSIGNED_BYTE, nullptr );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "creating framebuffer texture", __FILE__, __LINE__ );
|
2013-07-24 13:06:59 +00:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Bind the texture to the specific attachment point, clear and rebind the screen
|
2016-05-02 13:56:16 +00:00
|
|
|
bindFb( m_mainFbo );
|
2021-02-16 22:25:27 +00:00
|
|
|
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachmentPoint, GL_TEXTURE_2D, textureTarget,
|
|
|
|
0 );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
|
|
|
// Check the status, exit if the framebuffer can't be created
|
2013-10-29 16:28:29 +00:00
|
|
|
GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
|
2013-08-02 08:34:23 +00:00
|
|
|
{
|
|
|
|
switch( status )
|
|
|
|
{
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
2016-12-23 15:29:14 +00:00
|
|
|
throw std::runtime_error( "The framebuffer attachment points are incomplete." );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
|
2016-12-23 15:29:14 +00:00
|
|
|
throw std::runtime_error( "No images attached to the framebuffer." );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
2016-05-02 13:56:16 +00:00
|
|
|
throw std::runtime_error( "The framebuffer does not have at least one "
|
2021-02-16 22:25:27 +00:00
|
|
|
"image attached to it." );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
2016-05-02 13:56:16 +00:00
|
|
|
throw std::runtime_error( "The framebuffer read buffer is incomplete." );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
2021-10-03 12:58:56 +00:00
|
|
|
throw std::runtime_error( "The combination of internal formats of the attached "
|
|
|
|
"images violates an implementation-dependent set of "
|
|
|
|
"restrictions." );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
|
2016-05-02 13:56:16 +00:00
|
|
|
throw std::runtime_error( "GL_RENDERBUFFER_SAMPLES is not the same for "
|
2021-02-16 22:25:27 +00:00
|
|
|
"all attached renderbuffers" );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT:
|
2016-05-02 13:56:16 +00:00
|
|
|
throw std::runtime_error( "Framebuffer incomplete layer targets errors." );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2016-12-23 15:29:14 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
|
|
|
|
throw std::runtime_error( "Framebuffer attachments have different dimensions" );
|
|
|
|
|
2013-08-02 08:34:23 +00:00
|
|
|
default:
|
2016-12-23 15:29:14 +00:00
|
|
|
throw std::runtime_error( "Unknown error occurred when creating the framebuffer." );
|
2013-08-02 08:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
ClearBuffer( COLOR4D::BLACK );
|
2013-09-04 14:25:57 +00:00
|
|
|
|
|
|
|
// Return to direct rendering (we were asked only to create a buffer, not switch to one)
|
2016-05-02 13:56:16 +00:00
|
|
|
bindFb( DIRECT_RENDERING );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Store the new buffer
|
2016-12-22 17:58:29 +00:00
|
|
|
OPENGL_BUFFER buffer = { aDimensions, textureTarget, attachmentPoint };
|
2013-07-24 13:06:59 +00:00
|
|
|
m_buffers.push_back( buffer );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
return usedBuffers();
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
GLenum OPENGL_COMPOSITOR::GetBufferTexture( unsigned int aBufferHandle )
|
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
wxASSERT( aBufferHandle > 0 && aBufferHandle <= usedBuffers() );
|
2016-12-22 17:58:29 +00:00
|
|
|
return m_buffers[aBufferHandle - 1].textureTarget;
|
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
void OPENGL_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
|
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
wxASSERT( m_initialized );
|
|
|
|
wxASSERT( aBufferHandle <= usedBuffers() );
|
2013-07-24 13:06:59 +00:00
|
|
|
|
2016-05-02 13:56:16 +00:00
|
|
|
// Either unbind the FBO for direct rendering, or bind the one with target textures
|
|
|
|
bindFb( aBufferHandle == DIRECT_RENDERING ? DIRECT_RENDERING : m_mainFbo );
|
2013-07-24 13:06:59 +00:00
|
|
|
|
2016-05-02 13:56:16 +00:00
|
|
|
// Switch the target texture
|
|
|
|
if( m_curFbo != DIRECT_RENDERING )
|
2013-07-24 13:06:59 +00:00
|
|
|
{
|
2016-05-02 13:56:16 +00:00
|
|
|
m_curBuffer = aBufferHandle - 1;
|
|
|
|
glDrawBuffer( m_buffers[m_curBuffer].attachmentPoint );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "setting draw buffer", __FILE__, __LINE__ );
|
2016-12-22 17:58:29 +00:00
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
glViewport( 0, 0, m_buffers[m_curBuffer].dimensions.x,
|
|
|
|
m_buffers[m_curBuffer].dimensions.y );
|
2017-01-13 09:36:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-22 17:58:29 +00:00
|
|
|
glViewport( 0, 0, GetScreenSize().x, GetScreenSize().y );
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
void OPENGL_COMPOSITOR::ClearBuffer( const COLOR4D& aColor )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
wxASSERT( m_initialized );
|
2017-03-04 07:51:35 +00:00
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
glClearColor( aColor.r, aColor.g, aColor.b, 0.0f );
|
2016-05-02 14:15:24 +00:00
|
|
|
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2023-06-28 20:48:07 +00:00
|
|
|
VECTOR2I OPENGL_COMPOSITOR::GetScreenSize() const
|
2016-12-22 17:58:29 +00:00
|
|
|
{
|
2023-06-28 20:48:07 +00:00
|
|
|
typedef VECTOR2I::coord_type coord_t;
|
|
|
|
wxASSERT( m_width <= static_cast<unsigned int>( std::numeric_limits<coord_t>::max() ) );
|
|
|
|
wxASSERT( m_height <= static_cast<unsigned int>( std::numeric_limits<coord_t>::max() ) );
|
|
|
|
|
|
|
|
return { static_cast<coord_t>( m_width ), static_cast<coord_t>( m_height ) };
|
2016-12-22 17:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
void OPENGL_COMPOSITOR::Begin()
|
|
|
|
{
|
|
|
|
m_antialiasing->Begin();
|
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-22 19:02:34 +00:00
|
|
|
void OPENGL_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
|
2016-12-22 17:58:29 +00:00
|
|
|
{
|
|
|
|
m_antialiasing->DrawBuffer( aBufferHandle );
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-22 19:02:34 +00:00
|
|
|
void OPENGL_COMPOSITOR::DrawBuffer( unsigned int aSourceHandle, unsigned int aDestHandle )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
wxASSERT( m_initialized );
|
|
|
|
wxASSERT( aSourceHandle != 0 && aSourceHandle <= usedBuffers() );
|
|
|
|
wxASSERT( aDestHandle <= usedBuffers() );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2016-12-22 19:02:34 +00:00
|
|
|
// Switch to the destination buffer and blit the scene
|
2021-02-16 22:25:27 +00:00
|
|
|
SetBuffer( aDestHandle );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Depth test has to be disabled to make transparency working
|
|
|
|
glDisable( GL_DEPTH_TEST );
|
|
|
|
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
|
|
|
|
|
|
|
|
// Enable texturing and bind the main texture
|
|
|
|
glEnable( GL_TEXTURE_2D );
|
2016-12-22 17:58:29 +00:00
|
|
|
glBindTexture( GL_TEXTURE_2D, m_buffers[aSourceHandle - 1].textureTarget );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Draw a full screen quad with the texture
|
|
|
|
glMatrixMode( GL_MODELVIEW );
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
glMatrixMode( GL_PROJECTION );
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glBegin( GL_TRIANGLES );
|
2021-02-16 22:25:27 +00:00
|
|
|
glTexCoord2f( 0.0f, 1.0f );
|
|
|
|
glVertex2f( -1.0f, 1.0f );
|
|
|
|
glTexCoord2f( 0.0f, 0.0f );
|
|
|
|
glVertex2f( -1.0f, -1.0f );
|
|
|
|
glTexCoord2f( 1.0f, 1.0f );
|
|
|
|
glVertex2f( 1.0f, 1.0f );
|
|
|
|
|
|
|
|
glTexCoord2f( 1.0f, 1.0f );
|
|
|
|
glVertex2f( 1.0f, 1.0f );
|
|
|
|
glTexCoord2f( 0.0f, 0.0f );
|
|
|
|
glVertex2f( -1.0f, -1.0f );
|
|
|
|
glTexCoord2f( 1.0f, 0.0f );
|
|
|
|
glVertex2f( 1.0f, -1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode( GL_MODELVIEW );
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
void OPENGL_COMPOSITOR::Present()
|
|
|
|
{
|
|
|
|
m_antialiasing->Present();
|
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2017-01-13 09:36:59 +00:00
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::bindFb( unsigned int aFb )
|
|
|
|
{
|
2016-05-02 13:56:16 +00:00
|
|
|
// Currently there are only 2 valid FBOs
|
2021-10-03 12:58:56 +00:00
|
|
|
wxASSERT( aFb == DIRECT_RENDERING || aFb == m_mainFbo );
|
2016-05-02 13:56:16 +00:00
|
|
|
|
|
|
|
if( m_curFbo != aFb )
|
|
|
|
{
|
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER, aFb );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "switching framebuffer", __FILE__, __LINE__ );
|
2016-05-02 13:56:16 +00:00
|
|
|
m_curFbo = aFb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
void OPENGL_COMPOSITOR::clean()
|
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
wxASSERT( m_initialized );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2016-05-02 13:56:16 +00:00
|
|
|
bindFb( DIRECT_RENDERING );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2021-10-03 12:58:56 +00:00
|
|
|
for( const OPENGL_BUFFER& buffer : m_buffers )
|
|
|
|
glDeleteTextures( 1, &buffer.textureTarget );
|
2013-07-30 16:29:54 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
m_buffers.clear();
|
|
|
|
|
2020-10-30 19:35:24 +00:00
|
|
|
if( glDeleteFramebuffersEXT )
|
|
|
|
glDeleteFramebuffersEXT( 1, &m_mainFbo );
|
|
|
|
|
|
|
|
if( glDeleteRenderbuffersEXT )
|
|
|
|
glDeleteRenderbuffersEXT( 1, &m_depthBuffer );
|
2014-07-09 09:22:42 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
m_initialized = false;
|
|
|
|
}
|
2019-01-21 18:42:06 +00:00
|
|
|
|
2019-02-16 19:25:10 +00:00
|
|
|
|
2019-01-21 18:42:06 +00:00
|
|
|
int OPENGL_COMPOSITOR::GetAntialiasSupersamplingFactor() const
|
|
|
|
{
|
2021-06-09 00:20:41 +00:00
|
|
|
switch ( m_currentAntialiasingMode )
|
2019-01-21 18:42:06 +00:00
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
case OPENGL_ANTIALIASING_MODE::SUPERSAMPLING: return 2;
|
|
|
|
default: return 1;
|
2019-01-21 18:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-02 13:04:23 +00:00
|
|
|
|
|
|
|
VECTOR2D OPENGL_COMPOSITOR::GetAntialiasRenderingOffset() const
|
|
|
|
{
|
|
|
|
switch( m_currentAntialiasingMode )
|
|
|
|
{
|
2021-06-09 00:20:41 +00:00
|
|
|
case OPENGL_ANTIALIASING_MODE::SUPERSAMPLING: return VECTOR2D( 0.5, -0.5 );
|
2021-10-03 12:58:56 +00:00
|
|
|
default: return VECTOR2D( 0, 0 );
|
2021-06-02 13:04:23 +00:00
|
|
|
}
|
|
|
|
}
|