2013-07-22 08:41:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-08-08 12:25:03 +00:00
|
|
|
* Copyright 2013-2017 CERN
|
2021-02-16 22:25:27 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-07-22 08:41:12 +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
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/opengl/gpu_manager.h>
|
2017-08-08 12:25:03 +00:00
|
|
|
#include <gal/opengl/cached_container_gpu.h>
|
|
|
|
#include <gal/opengl/cached_container_ram.h>
|
2013-07-22 08:41:12 +00:00
|
|
|
#include <gal/opengl/noncached_container.h>
|
|
|
|
#include <gal/opengl/shader.h>
|
2016-05-02 13:56:16 +00:00
|
|
|
#include <gal/opengl/utils.h>
|
2021-11-28 21:32:46 +00:00
|
|
|
#include <gal/opengl/vertex_item.h>
|
|
|
|
|
2023-09-07 11:22:10 +00:00
|
|
|
#include <core/profile.h>
|
2016-05-02 13:56:16 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#include <typeinfo>
|
2013-11-11 09:48:49 +00:00
|
|
|
#include <confirm.h>
|
2021-03-22 19:34:33 +00:00
|
|
|
#include <trace_helpers.h>
|
2016-05-02 13:56:17 +00:00
|
|
|
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2023-09-07 11:22:10 +00:00
|
|
|
#include <core/profile.h>
|
2013-11-11 09:48:49 +00:00
|
|
|
#include <wx/log.h>
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
GPU_MANAGER* GPU_MANAGER::MakeManager( VERTEX_CONTAINER* aContainer )
|
|
|
|
{
|
2017-08-08 12:25:03 +00:00
|
|
|
if( aContainer->IsCached() )
|
2013-07-22 08:41:12 +00:00
|
|
|
return new GPU_CACHED_MANAGER( aContainer );
|
2017-08-08 12:25:03 +00:00
|
|
|
else
|
2013-07-22 08:41:12 +00:00
|
|
|
return new GPU_NONCACHED_MANAGER( aContainer );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GPU_MANAGER::GPU_MANAGER( VERTEX_CONTAINER* aContainer ) :
|
2021-02-16 22:25:27 +00:00
|
|
|
m_isDrawing( false ),
|
|
|
|
m_container( aContainer ),
|
2021-07-15 19:26:35 +00:00
|
|
|
m_shader( nullptr ),
|
2021-02-16 22:25:27 +00:00
|
|
|
m_shaderAttrib( 0 ),
|
|
|
|
m_enableDepthTest( true )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GPU_MANAGER::~GPU_MANAGER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GPU_MANAGER::SetShader( SHADER& aShader )
|
|
|
|
{
|
|
|
|
m_shader = &aShader;
|
2022-05-18 11:55:52 +00:00
|
|
|
m_shaderAttrib = m_shader->GetAttribute( "a_shaderParams" );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
if( m_shaderAttrib == -1 )
|
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
DisplayError( nullptr, wxT( "Could not get the shader attribute location" ) );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cached manager
|
|
|
|
GPU_CACHED_MANAGER::GPU_CACHED_MANAGER( VERTEX_CONTAINER* aContainer ) :
|
2021-02-16 22:25:27 +00:00
|
|
|
GPU_MANAGER( aContainer ),
|
|
|
|
m_buffersInitialized( false ),
|
2021-12-03 16:49:23 +00:00
|
|
|
m_indicesCapacity( 0 ),
|
|
|
|
m_totalHuge( 0 ),
|
|
|
|
m_totalNormal( 0 ),
|
|
|
|
m_indexBufSize( 0 ),
|
|
|
|
m_indexBufMaxSize( 0 ),
|
|
|
|
m_curVrangeSize( 0 )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
void GPU_CACHED_MANAGER::BeginDrawing()
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2016-05-02 14:12:16 +00:00
|
|
|
wxASSERT( !m_isDrawing );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
m_curVrangeSize = 0;
|
|
|
|
m_indexBufMaxSize = 0;
|
|
|
|
m_indexBufSize = 0;
|
|
|
|
m_vranges.clear();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
m_isDrawing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
void GPU_CACHED_MANAGER::DrawIndices( const VERTEX_ITEM *aItem )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
wxASSERT( m_isDrawing );
|
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
unsigned int offset = aItem->GetOffset();
|
|
|
|
unsigned int size = aItem->GetSize();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
if( size > 1000 )
|
|
|
|
{
|
|
|
|
m_totalHuge += size;
|
|
|
|
m_vranges.emplace_back( offset, offset + size - 1, true );
|
|
|
|
m_indexBufSize = std::max( m_curVrangeSize, m_indexBufSize );
|
|
|
|
m_curVrangeSize = 0;
|
|
|
|
}
|
|
|
|
else if ( size > 0 )
|
|
|
|
{
|
|
|
|
m_totalNormal += size;
|
|
|
|
m_vranges.emplace_back( offset, offset + size - 1, false );
|
|
|
|
m_curVrangeSize += size;
|
|
|
|
}
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GPU_CACHED_MANAGER::EndDrawing()
|
|
|
|
{
|
|
|
|
wxASSERT( m_isDrawing );
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
CACHED_CONTAINER* cached = static_cast<CACHED_CONTAINER*>( m_container );
|
|
|
|
|
|
|
|
if( cached->IsMapped() )
|
|
|
|
cached->Unmap();
|
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
m_indexBufSize = std::max( m_curVrangeSize, m_indexBufSize );
|
|
|
|
m_indexBufMaxSize = std::max( 2*m_indexBufSize, m_indexBufMaxSize );
|
|
|
|
|
|
|
|
resizeIndices( m_indexBufMaxSize );
|
2016-05-02 14:08:50 +00:00
|
|
|
|
2018-10-14 22:09:59 +00:00
|
|
|
if( m_enableDepthTest )
|
|
|
|
glEnable( GL_DEPTH_TEST );
|
|
|
|
else
|
|
|
|
glDisable( GL_DEPTH_TEST );
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Prepare buffers
|
|
|
|
glEnableClientState( GL_VERTEX_ARRAY );
|
|
|
|
glEnableClientState( GL_COLOR_ARRAY );
|
|
|
|
|
|
|
|
// Bind vertices data buffers
|
2016-05-02 14:12:16 +00:00
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
|
2017-09-09 18:40:00 +00:00
|
|
|
glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, (GLvoid*) COORD_OFFSET );
|
|
|
|
glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, (GLvoid*) COLOR_OFFSET );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
if( m_shader != nullptr ) // Use shader if applicable
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
m_shader->Use();
|
|
|
|
glEnableVertexAttribArray( m_shaderAttrib );
|
2021-02-16 22:25:27 +00:00
|
|
|
glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE, VERTEX_SIZE,
|
|
|
|
(GLvoid*) SHADER_OFFSET );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 19:16:18 +00:00
|
|
|
PROF_TIMER cntDraw( "gl-draw-elements" );
|
2014-09-13 17:31:40 +00:00
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
int n_ranges = m_vranges.size();
|
|
|
|
int n = 0;
|
|
|
|
GLuint* iptr = m_indices.get();
|
|
|
|
GLuint icnt = 0;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
int drawCalls = 0;
|
|
|
|
|
|
|
|
while( n < n_ranges )
|
|
|
|
{
|
|
|
|
VRANGE* cur = &m_vranges[n];
|
|
|
|
|
|
|
|
if( cur->m_isContinuous )
|
|
|
|
{
|
|
|
|
if( icnt > 0 )
|
|
|
|
{
|
|
|
|
glDrawElements( GL_TRIANGLES, icnt, GL_UNSIGNED_INT, m_indices.get() );
|
|
|
|
drawCalls++;
|
|
|
|
}
|
|
|
|
|
|
|
|
icnt = 0;
|
|
|
|
iptr = m_indices.get();
|
|
|
|
|
|
|
|
glDrawArrays( GL_TRIANGLES, cur->m_start, cur->m_end - cur->m_start + 1 );
|
|
|
|
drawCalls++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( GLuint i = cur->m_start; i <= cur->m_end; i++ )
|
|
|
|
{
|
|
|
|
*iptr++ = i;
|
|
|
|
icnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( icnt > 0 )
|
|
|
|
{
|
|
|
|
glDrawElements( GL_TRIANGLES, icnt, GL_UNSIGNED_INT, m_indices.get() );
|
|
|
|
drawCalls++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cntDraw.Stop();
|
|
|
|
|
|
|
|
KI_TRACE( traceGalProfile,
|
2022-01-24 16:34:36 +00:00
|
|
|
"Cached manager size: VBO size %u iranges %zu max elt size %u drawcalls %u\n",
|
2021-11-28 21:32:46 +00:00
|
|
|
cached->AllItemsSize(), m_vranges.size(), m_indexBufMaxSize, drawCalls );
|
|
|
|
KI_TRACE( traceGalProfile, "Timing: %s\n", cntDraw.to_string() );
|
2016-05-02 13:56:17 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
2017-08-21 11:26:00 +00:00
|
|
|
cached->ClearDirty();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
// Deactivate vertex array
|
|
|
|
glDisableClientState( GL_COLOR_ARRAY );
|
|
|
|
glDisableClientState( GL_VERTEX_ARRAY );
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
if( m_shader != nullptr )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
glDisableVertexAttribArray( m_shaderAttrib );
|
|
|
|
m_shader->Deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_isDrawing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-03 19:12:00 +00:00
|
|
|
void GPU_CACHED_MANAGER::resizeIndices( unsigned int aNewSize )
|
|
|
|
{
|
|
|
|
if( aNewSize > m_indicesCapacity )
|
|
|
|
{
|
|
|
|
m_indicesCapacity = aNewSize;
|
|
|
|
m_indices.reset( new GLuint[m_indicesCapacity] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Noncached manager
|
|
|
|
GPU_NONCACHED_MANAGER::GPU_NONCACHED_MANAGER( VERTEX_CONTAINER* aContainer ) :
|
2021-02-16 22:25:27 +00:00
|
|
|
GPU_MANAGER( aContainer )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GPU_NONCACHED_MANAGER::BeginDrawing()
|
|
|
|
{
|
|
|
|
// Nothing has to be prepared
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-28 21:32:46 +00:00
|
|
|
void GPU_NONCACHED_MANAGER::DrawIndices( const VERTEX_ITEM* aItem )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Not implemented yet" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GPU_NONCACHED_MANAGER::EndDrawing()
|
|
|
|
{
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2022-09-17 18:43:46 +00:00
|
|
|
PROF_TIMER totalRealTime;
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2016-05-02 13:56:17 +00:00
|
|
|
|
2016-05-02 14:08:50 +00:00
|
|
|
if( m_container->GetSize() == 0 )
|
|
|
|
return;
|
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
VERTEX* vertices = m_container->GetAllVertices();
|
|
|
|
GLfloat* coordinates = (GLfloat*) ( vertices );
|
|
|
|
GLubyte* colors = (GLubyte*) ( vertices ) + COLOR_OFFSET;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2018-10-14 22:09:59 +00:00
|
|
|
if( m_enableDepthTest )
|
|
|
|
glEnable( GL_DEPTH_TEST );
|
|
|
|
else
|
|
|
|
glDisable( GL_DEPTH_TEST );
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Prepare buffers
|
|
|
|
glEnableClientState( GL_VERTEX_ARRAY );
|
|
|
|
glEnableClientState( GL_COLOR_ARRAY );
|
|
|
|
|
2017-09-09 18:40:00 +00:00
|
|
|
glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, coordinates );
|
|
|
|
glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, colors );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
if( m_shader != nullptr ) // Use shader if applicable
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2021-02-16 22:25:27 +00:00
|
|
|
GLfloat* shaders = (GLfloat*) ( vertices ) + SHADER_OFFSET / sizeof( GLfloat );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
m_shader->Use();
|
|
|
|
glEnableVertexAttribArray( m_shaderAttrib );
|
2021-02-16 22:25:27 +00:00
|
|
|
glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE, VERTEX_SIZE,
|
|
|
|
shaders );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glDrawArrays( GL_TRIANGLES, 0, m_container->GetSize() );
|
|
|
|
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2021-03-22 19:34:33 +00:00
|
|
|
wxLogTrace( traceGalProfile, wxT( "Noncached manager size: %d" ), m_container->GetSize() );
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2016-05-02 13:56:17 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Deactivate vertex array
|
|
|
|
glDisableClientState( GL_COLOR_ARRAY );
|
|
|
|
glDisableClientState( GL_VERTEX_ARRAY );
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
if( m_shader != nullptr )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
glDisableVertexAttribArray( m_shaderAttrib );
|
|
|
|
m_shader->Deactivate();
|
|
|
|
}
|
2016-05-02 13:56:17 +00:00
|
|
|
|
2018-10-12 17:08:25 +00:00
|
|
|
m_container->Clear();
|
|
|
|
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2016-12-31 12:00:24 +00:00
|
|
|
totalRealTime.Stop();
|
2021-03-22 19:34:33 +00:00
|
|
|
wxLogTrace( traceGalProfile, wxT( "GPU_NONCACHED_MANAGER::EndDrawing(): %.1f ms" ),
|
2021-02-16 22:25:27 +00:00
|
|
|
totalRealTime.msecs() );
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
2018-10-14 22:09:59 +00:00
|
|
|
|
|
|
|
void GPU_MANAGER::EnableDepthTest( bool aEnabled )
|
|
|
|
{
|
|
|
|
m_enableDepthTest = aEnabled;
|
|
|
|
}
|