55 m_glBufferHandle( -1 )
59 wxString vendor( glGetString( GL_VENDOR ) );
65 if( vendor.StartsWith(
"Intel" ) || vendor.Contains(
"etnaviv" ) )
75 glBindBuffer( GL_ARRAY_BUFFER, 0 );
76 checkGlError(
"allocating video memory for cached container", __FILE__, __LINE__ );
96 throw std::runtime_error(
"OpenGL no longer available!" );
99 m_vertices =
static_cast<VERTEX*
>( glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE ) );
101 if(
checkGlError(
"mapping vertices buffer", __FILE__, __LINE__ ) == GL_NO_ERROR )
114 glUnmapBuffer( GL_ARRAY_BUFFER );
115 checkGlError(
"unmapping vertices buffer", __FILE__, __LINE__ );
116 glBindBuffer( GL_ARRAY_BUFFER, 0 );
118 checkGlError(
"unbinding vertices buffer", __FILE__, __LINE__ );
120 catch(
const std::runtime_error& err )
122 wxLogError( wxT(
"OpenGL did not shut down properly.\n\n%s" ), err.what() );
137 wxT(
"Resizing & defragmenting container from %d to %d" ),
m_currentSize,
144#ifdef KICAD_GAL_PROFILE
151 glUnmapBuffer( GL_ARRAY_BUFFER );
154 glGenBuffers( 1, &newBuffer );
158#ifdef KICAD_GAL_PROFILE
160 glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
161 wxASSERT( eaBuffer == 0 );
163 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
164 glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize *
VERTEX_SIZE,
nullptr, GL_DYNAMIC_DRAW );
165 checkGlError(
"creating buffer during defragmentation", __FILE__, __LINE__ );
167 ITEMS::iterator it, it_end;
171 for( it =
m_items.begin(), it_end =
m_items.end(); it != it_end; ++it )
175 int itemSize = item->
GetSize();
178 glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, itemOffset *
VERTEX_SIZE,
185 newOffset += itemSize;
191 glCopyBufferSubData( GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
200 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
201 glBindBuffer( GL_ARRAY_BUFFER, 0 );
211 checkGlError(
"switching buffers during defragmentation", __FILE__, __LINE__ );
213#ifdef KICAD_GAL_PROFILE
238 wxT(
"Resizing & defragmenting container (memcpy) from %d to %d" ),
m_currentSize,
245#ifdef KICAD_GAL_PROFILE
253 glGenBuffers( 1, &newBuffer );
257#ifdef KICAD_GAL_PROFILE
259 glGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING, &eaBuffer );
260 wxASSERT( eaBuffer == 0 );
262 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, newBuffer );
263 glBufferData( GL_ELEMENT_ARRAY_BUFFER, aNewSize *
VERTEX_SIZE,
nullptr, GL_DYNAMIC_DRAW );
264 newBufferMem =
static_cast<VERTEX*
>( glMapBuffer( GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY ) );
265 checkGlError(
"creating buffer during defragmentation", __FILE__, __LINE__ );
270 glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
271 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
278 checkGlError(
"switching buffers during defragmentation", __FILE__, __LINE__ );
280#ifdef KICAD_GAL_PROFILE
302 unsigned int size = 0;
304 for(
const auto& item :
m_items )
306 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.
Class to store VERTEX instances with caching.
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...
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.
#define KI_TRACE(aWhat,...)
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.