59 wxString vendor( glGetString( GL_VENDOR ) );
65 if( vendor.StartsWith(
"Intel" ) || vendor.Contains(
"etnaviv" ) )
70#ifdef KICAD_GAL_PROFILE
77 glBindBuffer( GL_ARRAY_BUFFER, 0 );
78 checkGlError(
"allocating video memory for cached container", __FILE__, __LINE__ );
98 throw std::runtime_error(
"OpenGL no longer available!" );
101 m_vertices =
static_cast<VERTEX*
>( glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE ) );
103 if(
checkGlError(
"mapping vertices buffer", __FILE__, __LINE__ ) == GL_NO_ERROR
111 glBindBuffer( GL_ARRAY_BUFFER, 0 );
112 throw std::runtime_error(
"Could not map vertex buffer: glMapBuffer returned null" );
125 glUnmapBuffer( GL_ARRAY_BUFFER );
126 checkGlError(
"unmapping vertices buffer", __FILE__, __LINE__ );
127 glBindBuffer( GL_ARRAY_BUFFER, 0 );
129 checkGlError(
"unbinding vertices buffer", __FILE__, __LINE__ );
131 catch(
const std::runtime_error& err )
133 wxLogError( wxT(
"OpenGL did not shut down properly.\n\n%s" ), err.what() );
148 wxT(
"Resizing & defragmenting container from %d to %d" ),
m_currentSize,
155#ifdef KICAD_GAL_PROFILE
162 glUnmapBuffer( GL_ARRAY_BUFFER );
165 glGenBuffers( 1, &newBuffer );
169#ifdef KICAD_GAL_PROFILE
171 glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
172 wxASSERT( eaBuffer == 0 );
174 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
175 glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize *
VERTEX_SIZE,
nullptr, GL_DYNAMIC_DRAW );
176 checkGlError(
"creating buffer during defragmentation", __FILE__, __LINE__ );
178 ITEMS::iterator it, it_end;
182 for( it =
m_items.begin(), it_end =
m_items.end(); it != it_end; ++it )
186 int itemSize = item->
GetSize();
189 glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, itemOffset *
VERTEX_SIZE,
196 newOffset += itemSize;
200 if(
m_item->GetSize() > 0 )
202 glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
206 m_item->setOffset( newOffset );
211 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
212 glBindBuffer( GL_ARRAY_BUFFER, 0 );
226 catch(
const std::runtime_error& )
233 checkGlError(
"switching buffers during defragmentation", __FILE__, __LINE__ );
235#ifdef KICAD_GAL_PROFILE
260 wxT(
"Resizing & defragmenting container (memcpy) from %d to %d" ),
m_currentSize,
267#ifdef KICAD_GAL_PROFILE
275 glGenBuffers( 1, &newBuffer );
279#ifdef KICAD_GAL_PROFILE
281 glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
282 wxASSERT( eaBuffer == 0 );
285 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
286 glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize *
VERTEX_SIZE,
nullptr, GL_DYNAMIC_DRAW );
287 newBufferMem =
static_cast<VERTEX*
>( glMapBuffer( GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY ) );
288 checkGlError(
"creating buffer during defragmentation", __FILE__, __LINE__ );
290 if( newBufferMem ==
nullptr )
292 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
293 glDeleteBuffers( 1, &newBuffer );
300 glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
301 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
312 catch(
const std::runtime_error& )
319 checkGlError(
"switching buffers during defragmentation", __FILE__, __LINE__ );
321#ifdef KICAD_GAL_PROFILE
343 unsigned int size = 0;
345 for(
const auto& item :
m_items )
347 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.