55 wxString vendor( glGetString( GL_VENDOR ) );
61 if( vendor.StartsWith(
"Intel" ) || vendor.Contains(
"etnaviv" ) )
66#ifdef KICAD_GAL_PROFILE
73 glBindBuffer( GL_ARRAY_BUFFER, 0 );
74 checkGlError(
"allocating video memory for cached container", __FILE__, __LINE__ );
94 throw std::runtime_error(
"OpenGL no longer available!" );
97 m_vertices =
static_cast<VERTEX*
>( glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE ) );
99 if(
checkGlError(
"mapping vertices buffer", __FILE__, __LINE__ ) == GL_NO_ERROR
107 glBindBuffer( GL_ARRAY_BUFFER, 0 );
108 throw std::runtime_error(
"Could not map vertex buffer: glMapBuffer returned null" );
121 glUnmapBuffer( GL_ARRAY_BUFFER );
122 checkGlError(
"unmapping vertices buffer", __FILE__, __LINE__ );
123 glBindBuffer( GL_ARRAY_BUFFER, 0 );
125 checkGlError(
"unbinding vertices buffer", __FILE__, __LINE__ );
127 catch(
const std::runtime_error& err )
129 wxLogError( wxT(
"OpenGL did not shut down properly.\n\n%s" ), err.what() );
144 wxT(
"Resizing & defragmenting container from %d to %d" ),
m_currentSize,
151#ifdef KICAD_GAL_PROFILE
158 glUnmapBuffer( GL_ARRAY_BUFFER );
161 glGenBuffers( 1, &newBuffer );
165#ifdef KICAD_GAL_PROFILE
167 glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
168 wxASSERT( eaBuffer == 0 );
170 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
171 glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize *
VERTEX_SIZE,
nullptr, GL_DYNAMIC_DRAW );
172 checkGlError(
"creating buffer during defragmentation", __FILE__, __LINE__ );
174 ITEMS::iterator it, it_end;
178 for( it =
m_items.begin(), it_end =
m_items.end(); it != it_end; ++it )
182 int itemSize = item->
GetSize();
185 glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, itemOffset *
VERTEX_SIZE,
192 newOffset += itemSize;
196 if(
m_item->GetSize() > 0 )
198 glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
202 m_item->setOffset( newOffset );
207 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
208 glBindBuffer( GL_ARRAY_BUFFER, 0 );
222 catch(
const std::runtime_error& )
229 checkGlError(
"switching buffers during defragmentation", __FILE__, __LINE__ );
231#ifdef KICAD_GAL_PROFILE
256 wxT(
"Resizing & defragmenting container (memcpy) from %d to %d" ),
m_currentSize,
263#ifdef KICAD_GAL_PROFILE
271 glGenBuffers( 1, &newBuffer );
275#ifdef KICAD_GAL_PROFILE
277 glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
278 wxASSERT( eaBuffer == 0 );
281 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
282 glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize *
VERTEX_SIZE,
nullptr, GL_DYNAMIC_DRAW );
283 newBufferMem =
static_cast<VERTEX*
>( glMapBuffer( GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY ) );
284 checkGlError(
"creating buffer during defragmentation", __FILE__, __LINE__ );
286 if( newBufferMem ==
nullptr )
288 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
289 glDeleteBuffers( 1, &newBuffer );
296 glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
297 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
308 catch(
const std::runtime_error& )
315 checkGlError(
"switching buffers during defragmentation", __FILE__, __LINE__ );
317#ifdef KICAD_GAL_PROFILE
339 unsigned int size = 0;
341 for(
const auto& item :
m_items )
343 size += item->GetSize();
void Map() override
Finish the vertices updates stage.
bool m_isMapped
Vertex buffer handle.
virtual unsigned int AllItemsSize() const override
bool IsMapped() const override
Prepare the container for vertices updates.
bool defragmentResizeMemcpy(unsigned int aNewSize)
Flag saying if vertex buffer is currently mapped.
void Unmap() override
Finish the vertices updates stage.
CACHED_CONTAINER_GPU(unsigned int aSize=DEFAULT_SIZE)
bool defragmentResize(unsigned int aNewSize) override
Remove empty spaces between chunks and optionally resizes the container.
unsigned int m_glBufferHandle
Flag saying whether it is safe to use glCopyBufferSubData.
unsigned int m_chunkOffset
Maximal vertex index number stored in the container.
VERTEX_ITEM * m_item
Properties of currently modified chunk & item.
FREE_CHUNK_MAP m_freeChunks
Stored VERTEX_ITEMs.
void defragment(VERTEX *aTarget)
Transfer all stored data to a new buffer, removing empty spaces between the data chunks in the contai...
CACHED_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
ITEMS m_items
Currently modified item.
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
unsigned int m_freeSpace
Current container size, expressed in vertices.
unsigned int usedSpace() const
Return size of the used memory space.
void setOffset(unsigned int aOffset)
Set data offset in the container.
unsigned int GetOffset() const
Return data offset in the container.
unsigned int GetSize() const
Return information about number of vertices stored.
A small class to help profiling.
void Stop()
Save the time when this function was called, and set the counter stane to stop.
double msecs(bool aSinceLast=false)
static const wxChar *const traceGalCachedContainerGpu
Flag to enable debug output of the GAL OpenGL GPU cached container.
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
The Cairo implementation of the graphics abstraction layer.
static constexpr size_t VERTEX_SIZE
wxLogTrace helper definitions.
int checkGlError(const std::string &aInfo, const char *aFile, int aLine, bool aThrow)
Check if a recent OpenGL operation has failed.
Class to handle an item held in a container.