2017-08-08 12:25:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright 2013-2017 CERN
|
2021-02-16 22:25:27 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2017-08-08 12:25:03 +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/cached_container_ram.h>
|
|
|
|
#include <gal/opengl/vertex_manager.h>
|
|
|
|
#include <gal/opengl/vertex_item.h>
|
|
|
|
#include <gal/opengl/shader.h>
|
|
|
|
#include <gal/opengl/utils.h>
|
|
|
|
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <list>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <wx/log.h>
|
2021-06-06 13:31:00 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2017-08-08 12:25:03 +00:00
|
|
|
#include <profile.h>
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
using namespace KIGFX;
|
|
|
|
|
2021-03-22 19:34:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to enable debug output of the GAL OpenGL cached container.
|
|
|
|
*
|
|
|
|
* Use "KICAD_GAL_CACHED_CONTAINER" to enable GAL OpenGL cached container tracing.
|
|
|
|
*
|
|
|
|
* @ingroup trace_env_vars
|
|
|
|
*/
|
|
|
|
static const wxChar* const traceGalCachedContainer = wxT( "KICAD_GAL_CACHED_CONTAINER" );
|
|
|
|
|
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
CACHED_CONTAINER_RAM::CACHED_CONTAINER_RAM( unsigned int aSize ) :
|
2021-02-16 22:25:27 +00:00
|
|
|
CACHED_CONTAINER( aSize ),
|
|
|
|
m_verticesBuffer( 0 )
|
2017-08-08 12:25:03 +00:00
|
|
|
{
|
2017-08-08 13:33:57 +00:00
|
|
|
glGenBuffers( 1, &m_verticesBuffer );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "generating vertices buffer", __FILE__, __LINE__ );
|
2017-08-08 13:33:57 +00:00
|
|
|
|
2017-09-09 18:40:00 +00:00
|
|
|
m_vertices = static_cast<VERTEX*>( malloc( aSize * VERTEX_SIZE ) );
|
2021-02-22 13:15:09 +00:00
|
|
|
|
|
|
|
if( !m_vertices )
|
|
|
|
throw std::bad_alloc();
|
2017-08-08 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CACHED_CONTAINER_RAM::~CACHED_CONTAINER_RAM()
|
|
|
|
{
|
2020-10-30 19:35:24 +00:00
|
|
|
if( glDeleteBuffers )
|
|
|
|
glDeleteBuffers( 1, &m_verticesBuffer );
|
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
free( m_vertices );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER_RAM::Unmap()
|
|
|
|
{
|
|
|
|
if( !m_dirty )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Upload vertices coordinates and shader types to GPU memory
|
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, m_verticesBuffer );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "binding vertices buffer", __FILE__, __LINE__ );
|
2017-09-09 16:00:58 +00:00
|
|
|
glBufferData( GL_ARRAY_BUFFER, m_maxIndex * VERTEX_SIZE, m_vertices, GL_STREAM_DRAW );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "transferring vertices", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "unbinding vertices buffer", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CACHED_CONTAINER_RAM::defragmentResize( unsigned int aNewSize )
|
|
|
|
{
|
2021-03-22 19:34:33 +00:00
|
|
|
wxLogTrace( traceGalCachedContainer,
|
2021-02-16 22:25:27 +00:00
|
|
|
wxT( "Resizing & defragmenting container (memcpy) from %d to %d" ), m_currentSize,
|
|
|
|
aNewSize );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
// No shrinking if we cannot fit all the data
|
|
|
|
if( usedSpace() > aNewSize )
|
|
|
|
return false;
|
|
|
|
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2017-08-08 12:25:03 +00:00
|
|
|
PROF_COUNTER totalTime;
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2017-08-08 12:25:03 +00:00
|
|
|
|
2017-09-09 18:40:00 +00:00
|
|
|
VERTEX* newBufferMem = static_cast<VERTEX*>( malloc( aNewSize * VERTEX_SIZE ) );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
if( !newBufferMem )
|
2021-02-22 13:15:09 +00:00
|
|
|
throw std::bad_alloc();
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
defragment( newBufferMem );
|
|
|
|
|
|
|
|
// Switch to the new vertex buffer
|
|
|
|
free( m_vertices );
|
|
|
|
m_vertices = newBufferMem;
|
|
|
|
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2017-08-08 12:25:03 +00:00
|
|
|
totalTime.Stop();
|
|
|
|
|
2021-03-22 19:34:33 +00:00
|
|
|
wxLogTrace( traceGalCachedContainer, "Defragmented container storing %d vertices / %.1f ms",
|
2017-08-08 12:25:03 +00:00
|
|
|
m_currentSize - m_freeSpace, totalTime.msecs() );
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
m_freeSpace += ( aNewSize - m_currentSize );
|
|
|
|
m_currentSize = aNewSize;
|
|
|
|
|
|
|
|
// Now there is only one big chunk of free memory
|
|
|
|
m_freeChunks.clear();
|
|
|
|
m_freeChunks.insert( std::make_pair( m_freeSpace, m_currentSize - m_freeSpace ) );
|
2017-09-09 16:00:58 +00:00
|
|
|
m_dirty = true;
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|