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) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-08-18 14:17:16 +00:00
|
|
|
*
|
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_gpu.h>
|
|
|
|
#include <gal/opengl/vertex_manager.h>
|
|
|
|
#include <gal/opengl/vertex_item.h>
|
|
|
|
#include <gal/opengl/shader.h>
|
|
|
|
#include <gal/opengl/utils.h>
|
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
#include <list>
|
|
|
|
|
2021-04-30 23:24:09 +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 GPU cached container.
|
|
|
|
*
|
|
|
|
* Use "KICAD_GAL_CACHED_CONTAINER_GPU" to enable GAL OpenGL GPU cached container tracing.
|
|
|
|
*
|
|
|
|
* @ingroup trace_env_vars
|
|
|
|
*/
|
|
|
|
static const wxChar* const traceGalCachedContainerGpu = wxT( "KICAD_GAL_CACHED_CONTAINER_GPU" );
|
|
|
|
|
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
CACHED_CONTAINER_GPU::CACHED_CONTAINER_GPU( unsigned int aSize ) :
|
2021-02-22 13:15:09 +00:00
|
|
|
CACHED_CONTAINER( aSize ),
|
|
|
|
m_isMapped( false ),
|
|
|
|
m_glBufferHandle( -1 )
|
2017-08-08 12:25:03 +00:00
|
|
|
{
|
2017-08-08 13:33:57 +00:00
|
|
|
m_useCopyBuffer = GLEW_ARB_copy_buffer;
|
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
wxString vendor( glGetString( GL_VENDOR ) );
|
2020-08-18 14:17:16 +00:00
|
|
|
|
|
|
|
// workaround for intel GPU drivers: disable glCopyBuffer, causes crashes/freezes on
|
|
|
|
// certain driver versions
|
2021-02-16 22:25:27 +00:00
|
|
|
if( vendor.Contains( "Intel " ) || vendor.Contains( "etnaviv" ) )
|
2018-08-16 12:34:25 +00:00
|
|
|
{
|
|
|
|
m_useCopyBuffer = false;
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2017-08-08 13:33:57 +00:00
|
|
|
glGenBuffers( 1, &m_glBufferHandle );
|
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, m_glBufferHandle );
|
2021-02-22 13:15:09 +00:00
|
|
|
glBufferData( GL_ARRAY_BUFFER, m_currentSize * VERTEX_SIZE, nullptr, GL_DYNAMIC_DRAW );
|
2017-08-08 13:33:57 +00:00
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "allocating video memory for cached container", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CACHED_CONTAINER_GPU::~CACHED_CONTAINER_GPU()
|
|
|
|
{
|
|
|
|
if( m_isMapped )
|
|
|
|
Unmap();
|
|
|
|
|
2020-10-30 19:35:24 +00:00
|
|
|
if( glDeleteBuffers )
|
|
|
|
glDeleteBuffers( 1, &m_glBufferHandle );
|
2017-08-08 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER_GPU::Map()
|
|
|
|
{
|
2018-03-22 10:59:38 +00:00
|
|
|
wxCHECK( !IsMapped(), /*void*/ );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
2020-10-30 19:35:24 +00:00
|
|
|
// OpenGL version might suddenly stop being available in Windows when an RDP session is started
|
|
|
|
if( !glBindBuffer )
|
|
|
|
throw std::runtime_error( "OpenGL no longer available!" );
|
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, m_glBufferHandle );
|
|
|
|
m_vertices = static_cast<VERTEX*>( glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE ) );
|
|
|
|
|
2021-03-22 19:34:33 +00:00
|
|
|
if( checkGlError( "mapping vertices buffer", __FILE__, __LINE__ ) == GL_NO_ERROR )
|
2018-03-22 10:59:38 +00:00
|
|
|
m_isMapped = true;
|
2017-08-08 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER_GPU::Unmap()
|
|
|
|
{
|
2018-03-22 10:59:38 +00:00
|
|
|
wxCHECK( IsMapped(), /*void*/ );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
// This gets called from ~CACHED_CONTAINER_GPU. To avoid throwing an exception from
|
|
|
|
// the dtor, catch it here instead.
|
2021-02-16 22:29:12 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
glUnmapBuffer( GL_ARRAY_BUFFER );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "unmapping vertices buffer", __FILE__, __LINE__ );
|
2021-02-16 22:29:12 +00:00
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
2021-02-22 13:15:09 +00:00
|
|
|
m_vertices = nullptr;
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "unbinding vertices buffer", __FILE__, __LINE__ );
|
2021-02-16 22:29:12 +00:00
|
|
|
}
|
|
|
|
catch( const std::runtime_error& err )
|
|
|
|
{
|
|
|
|
wxLogError( wxT( "OpenGL did not shut down properly.\n\n%s" ), err.what() );
|
|
|
|
}
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
m_isMapped = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CACHED_CONTAINER_GPU::defragmentResize( unsigned int aNewSize )
|
|
|
|
{
|
|
|
|
if( !m_useCopyBuffer )
|
|
|
|
return defragmentResizeMemcpy( aNewSize );
|
|
|
|
|
2018-03-22 10:59:38 +00:00
|
|
|
wxCHECK( IsMapped(), false );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
2021-03-22 19:34:33 +00:00
|
|
|
wxLogTrace( traceGalCachedContainerGpu,
|
2021-02-16 22:25:27 +00:00
|
|
|
wxT( "Resizing & defragmenting container 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
|
|
|
|
|
|
|
GLuint newBuffer;
|
|
|
|
|
|
|
|
// glCopyBufferSubData requires a buffer to be unmapped
|
|
|
|
glUnmapBuffer( GL_ARRAY_BUFFER );
|
|
|
|
|
2018-03-22 10:59:38 +00:00
|
|
|
// Create a new destination buffer
|
2017-08-08 12:25:03 +00:00
|
|
|
glGenBuffers( 1, &newBuffer );
|
|
|
|
|
|
|
|
// It would be best to use GL_COPY_WRITE_BUFFER here,
|
|
|
|
// but it is not available everywhere
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2017-08-08 12:25:03 +00:00
|
|
|
GLint eaBuffer = -1;
|
|
|
|
glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
|
2018-03-22 10:59:38 +00:00
|
|
|
wxASSERT( eaBuffer == 0 );
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2017-08-08 12:25:03 +00:00
|
|
|
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
|
2021-02-22 13:15:09 +00:00
|
|
|
glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize * VERTEX_SIZE, nullptr, GL_DYNAMIC_DRAW );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "creating buffer during defragmentation", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
ITEMS::iterator it, it_end;
|
2021-02-16 22:25:27 +00:00
|
|
|
int newOffset = 0;
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
// Defragmentation
|
|
|
|
for( it = m_items.begin(), it_end = m_items.end(); it != it_end; ++it )
|
|
|
|
{
|
|
|
|
VERTEX_ITEM* item = *it;
|
2021-02-16 22:25:27 +00:00
|
|
|
int itemOffset = item->GetOffset();
|
|
|
|
int itemSize = item->GetSize();
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
// Move an item to the new container
|
2021-02-16 22:25:27 +00:00
|
|
|
glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, itemOffset * VERTEX_SIZE,
|
|
|
|
newOffset * VERTEX_SIZE, itemSize * VERTEX_SIZE );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
// Update new offset
|
|
|
|
item->setOffset( newOffset );
|
|
|
|
|
|
|
|
// Move to the next free space
|
|
|
|
newOffset += itemSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the current item and place it at the end
|
|
|
|
if( m_item->GetSize() > 0 )
|
|
|
|
{
|
|
|
|
glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
|
2021-02-16 22:25:27 +00:00
|
|
|
m_item->GetOffset() * VERTEX_SIZE, newOffset * VERTEX_SIZE,
|
|
|
|
m_item->GetSize() * VERTEX_SIZE );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
m_item->setOffset( newOffset );
|
|
|
|
m_chunkOffset = newOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
|
|
|
|
glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
2018-03-22 10:59:38 +00:00
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
// Previously we have unmapped the array buffer, now when it is also
|
|
|
|
// unbound, it may be officially marked as unmapped
|
|
|
|
m_isMapped = false;
|
|
|
|
glDeleteBuffers( 1, &m_glBufferHandle );
|
|
|
|
|
|
|
|
// Switch to the new vertex buffer
|
|
|
|
m_glBufferHandle = newBuffer;
|
|
|
|
Map();
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "switching buffers during defragmentation", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
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( traceGalCachedContainerGpu, "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 ) );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CACHED_CONTAINER_GPU::defragmentResizeMemcpy( unsigned int aNewSize )
|
|
|
|
{
|
2018-03-22 10:59:38 +00:00
|
|
|
wxCHECK( IsMapped(), false );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
2021-03-22 19:34:33 +00:00
|
|
|
wxLogTrace( traceGalCachedContainerGpu,
|
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
|
|
|
|
2021-02-16 22:25:27 +00:00
|
|
|
GLuint newBuffer;
|
2017-08-08 12:25:03 +00:00
|
|
|
VERTEX* newBufferMem;
|
|
|
|
|
|
|
|
// Create the destination buffer
|
|
|
|
glGenBuffers( 1, &newBuffer );
|
|
|
|
|
|
|
|
// It would be best to use GL_COPY_WRITE_BUFFER here,
|
|
|
|
// but it is not available everywhere
|
2021-04-30 23:24:09 +00:00
|
|
|
#ifdef KICAD_GAL_PROFILE
|
2017-08-08 12:25:03 +00:00
|
|
|
GLint eaBuffer = -1;
|
|
|
|
glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
|
2018-03-22 10:59:38 +00:00
|
|
|
wxASSERT( eaBuffer == 0 );
|
2021-04-30 23:24:09 +00:00
|
|
|
#endif /* KICAD_GAL_PROFILE */
|
2017-08-08 12:25:03 +00:00
|
|
|
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
|
2021-02-22 13:15:09 +00:00
|
|
|
glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize * VERTEX_SIZE, nullptr, GL_DYNAMIC_DRAW );
|
2017-08-08 12:25:03 +00:00
|
|
|
newBufferMem = static_cast<VERTEX*>( glMapBuffer( GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY ) );
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "creating buffer during defragmentation", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
defragment( newBufferMem );
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
|
|
|
|
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
|
|
|
|
Unmap();
|
|
|
|
glDeleteBuffers( 1, &m_glBufferHandle );
|
|
|
|
|
|
|
|
// Switch to the new vertex buffer
|
|
|
|
m_glBufferHandle = newBuffer;
|
|
|
|
Map();
|
2021-03-22 19:34:33 +00:00
|
|
|
checkGlError( "switching buffers during defragmentation", __FILE__, __LINE__ );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
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( traceGalCachedContainerGpu, "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 ) );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|