KiCad PCB EDA Suite
KIGFX::GPU_CACHED_MANAGER Class Reference

#include <gpu_manager.h>

Inheritance diagram for KIGFX::GPU_CACHED_MANAGER:
KIGFX::GPU_MANAGER

Classes

struct  VRANGE
 

Public Member Functions

 GPU_CACHED_MANAGER (VERTEX_CONTAINER *aContainer)
 
 ~GPU_CACHED_MANAGER ()
 Prepare the stored data to be drawn. More...
 
virtual void BeginDrawing () override
 Make the GPU draw given range of vertices. More...
 
virtual void DrawIndices (const VERTEX_ITEM *aItem) override
 Clear the container after drawing routines. More...
 
virtual void EndDrawing () override
 Map vertex buffer stored in GPU memory. More...
 
void Map ()
 Unmap vertex buffer. More...
 
void Unmap ()
 
virtual void SetShader (SHADER &aShader)
 Allow using shaders with the stored data. More...
 
void EnableDepthTest (bool aEnabled)
 Enable/disable Z buffer depth test. More...
 

Static Public Member Functions

static GPU_MANAGERMakeManager (VERTEX_CONTAINER *aContainer)
 

Protected Member Functions

void resizeIndices (unsigned int aNewSize)
 < Resizes the indices buffer to aNewSize if necessary More...
 

Protected Attributes

bool m_buffersInitialized
 Pointer to the current indices buffer. More...
 
boost::scoped_array< GLuint > m_indices
 Current indices buffer size. More...
 
unsigned int m_indicesCapacity
 Ranges of visible vertex indices to render. More...
 
std::vector< VRANGEm_vranges
 Number of huge VRANGEs (i.e. large zones) with separate draw calls. More...
 
int m_totalHuge
 Number of regular VRANGEs (small items) pooled into single draw call. More...
 
int m_totalNormal
 Current size of index buffer. More...
 
unsigned int m_indexBufSize
 Maximum size taken by the index buffer for all frames rendered so far. More...
 
unsigned int m_indexBufMaxSize
 Size of the current VRANGE. More...
 
unsigned int m_curVrangeSize
 
bool m_isDrawing
 Container that stores vertices data. More...
 
VERTEX_CONTAINERm_container
 Shader handling. More...
 
SHADERm_shader
 Location of shader attributes (for glVertexAttribPointer) More...
 
int m_shaderAttrib
 true: enable Z test when drawing More...
 
bool m_enableDepthTest
 

Detailed Description

Definition at line 102 of file gpu_manager.h.

Constructor & Destructor Documentation

◆ GPU_CACHED_MANAGER()

GPU_CACHED_MANAGER::GPU_CACHED_MANAGER ( VERTEX_CONTAINER aContainer)

Definition at line 85 of file gpu_manager.cpp.

85 :
86 GPU_MANAGER( aContainer ),
87 m_buffersInitialized( false ),
89 m_totalHuge( 0 ),
90 m_totalNormal( 0 ),
91 m_indexBufSize( 0 ),
94{
95}
int m_totalHuge
Number of regular VRANGEs (small items) pooled into single draw call.
Definition: gpu_manager.h:153
unsigned int m_indexBufMaxSize
Size of the current VRANGE.
Definition: gpu_manager.h:162
unsigned int m_indicesCapacity
Ranges of visible vertex indices to render.
Definition: gpu_manager.h:147
unsigned int m_curVrangeSize
Definition: gpu_manager.h:165
bool m_buffersInitialized
Pointer to the current indices buffer.
Definition: gpu_manager.h:141
int m_totalNormal
Current size of index buffer.
Definition: gpu_manager.h:156
unsigned int m_indexBufSize
Maximum size taken by the index buffer for all frames rendered so far.
Definition: gpu_manager.h:159
GPU_MANAGER(VERTEX_CONTAINER *aContainer)
Drawing status flag.
Definition: gpu_manager.cpp:57

◆ ~GPU_CACHED_MANAGER()

GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER ( )

Prepare the stored data to be drawn.

Definition at line 98 of file gpu_manager.cpp.

99{
100}

Member Function Documentation

◆ BeginDrawing()

void GPU_CACHED_MANAGER::BeginDrawing ( )
overridevirtual

Make the GPU draw given range of vertices.

Parameters
aOffsetis the beginning of the range.
aSizeis the number of vertices to be drawn.

Implements KIGFX::GPU_MANAGER.

Definition at line 103 of file gpu_manager.cpp.

104{
105 wxASSERT( !m_isDrawing );
106
107 m_curVrangeSize = 0;
109 m_indexBufSize = 0;
110 m_vranges.clear();
111
112 m_isDrawing = true;
113}
std::vector< VRANGE > m_vranges
Number of huge VRANGEs (i.e. large zones) with separate draw calls.
Definition: gpu_manager.h:150
bool m_isDrawing
Container that stores vertices data.
Definition: gpu_manager.h:86

References m_curVrangeSize, m_indexBufMaxSize, m_indexBufSize, KIGFX::GPU_MANAGER::m_isDrawing, and m_vranges.

◆ DrawIndices()

void GPU_CACHED_MANAGER::DrawIndices ( const VERTEX_ITEM aItem)
overridevirtual

Clear the container after drawing routines.

Implements KIGFX::GPU_MANAGER.

Definition at line 116 of file gpu_manager.cpp.

117{
118 wxASSERT( m_isDrawing );
119
120 unsigned int offset = aItem->GetOffset();
121 unsigned int size = aItem->GetSize();
122
123 if( size > 1000 )
124 {
125 m_totalHuge += size;
126 m_vranges.emplace_back( offset, offset + size - 1, true );
128 m_curVrangeSize = 0;
129 }
130 else if ( size > 0 )
131 {
132 m_totalNormal += size;
133 m_vranges.emplace_back( offset, offset + size - 1, false );
134 m_curVrangeSize += size;
135 }
136}
unsigned int GetOffset() const
Return data offset in the container.
Definition: vertex_item.h:68
unsigned int GetSize() const
Return information about number of vertices stored.
Definition: vertex_item.h:58

References KIGFX::VERTEX_ITEM::GetOffset(), KIGFX::VERTEX_ITEM::GetSize(), m_curVrangeSize, m_indexBufSize, KIGFX::GPU_MANAGER::m_isDrawing, m_totalHuge, m_totalNormal, and m_vranges.

◆ EnableDepthTest()

void GPU_MANAGER::EnableDepthTest ( bool  aEnabled)
inherited

Enable/disable Z buffer depth test.

Definition at line 332 of file gpu_manager.cpp.

333{
334 m_enableDepthTest = aEnabled;
335}

References KIGFX::GPU_MANAGER::m_enableDepthTest.

◆ EndDrawing()

void GPU_CACHED_MANAGER::EndDrawing ( )
overridevirtual

Map vertex buffer stored in GPU memory.

Implements KIGFX::GPU_MANAGER.

Definition at line 139 of file gpu_manager.cpp.

140{
141 wxASSERT( m_isDrawing );
142
143 CACHED_CONTAINER* cached = static_cast<CACHED_CONTAINER*>( m_container );
144
145 if( cached->IsMapped() )
146 cached->Unmap();
147
150
152
154 glEnable( GL_DEPTH_TEST );
155 else
156 glDisable( GL_DEPTH_TEST );
157
158 // Prepare buffers
159 glEnableClientState( GL_VERTEX_ARRAY );
160 glEnableClientState( GL_COLOR_ARRAY );
161
162 // Bind vertices data buffers
163 glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
164 glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, (GLvoid*) COORD_OFFSET );
165 glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, (GLvoid*) COLOR_OFFSET );
166
167 if( m_shader != nullptr ) // Use shader if applicable
168 {
169 m_shader->Use();
170 glEnableVertexAttribArray( m_shaderAttrib );
171 glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE, VERTEX_SIZE,
172 (GLvoid*) SHADER_OFFSET );
173 }
174
175 PROF_TIMER cntDraw( "gl-draw-elements" );
176
177 int n_ranges = m_vranges.size();
178 int n = 0;
179 GLuint* iptr = m_indices.get();
180 GLuint icnt = 0;
181
182 int drawCalls = 0;
183
184 while( n < n_ranges )
185 {
186 VRANGE* cur = &m_vranges[n];
187
188 if( cur->m_isContinuous )
189 {
190 if( icnt > 0 )
191 {
192 glDrawElements( GL_TRIANGLES, icnt, GL_UNSIGNED_INT, m_indices.get() );
193 drawCalls++;
194 }
195
196 icnt = 0;
197 iptr = m_indices.get();
198
199 glDrawArrays( GL_TRIANGLES, cur->m_start, cur->m_end - cur->m_start + 1 );
200 drawCalls++;
201 }
202 else
203 {
204 for( GLuint i = cur->m_start; i <= cur->m_end; i++ )
205 {
206 *iptr++ = i;
207 icnt++;
208 }
209 }
210 n++;
211 }
212
213 if( icnt > 0 )
214 {
215 glDrawElements( GL_TRIANGLES, icnt, GL_UNSIGNED_INT, m_indices.get() );
216 drawCalls++;
217 }
218
219 cntDraw.Stop();
220
222 "Cached manager size: VBO size %u iranges %zu max elt size %u drawcalls %u\n",
223 cached->AllItemsSize(), m_vranges.size(), m_indexBufMaxSize, drawCalls );
224 KI_TRACE( traceGalProfile, "Timing: %s\n", cntDraw.to_string() );
225
226 glBindBuffer( GL_ARRAY_BUFFER, 0 );
227 cached->ClearDirty();
228
229 // Deactivate vertex array
230 glDisableClientState( GL_COLOR_ARRAY );
231 glDisableClientState( GL_VERTEX_ARRAY );
232
233 if( m_shader != nullptr )
234 {
235 glDisableVertexAttribArray( m_shaderAttrib );
237 }
238
239 m_isDrawing = false;
240}
Class to store VERTEX instances with caching.
virtual unsigned int GetBufferHandle() const =0
Return handle to the vertex buffer.
virtual unsigned int AllItemsSize() const
virtual bool IsMapped() const =0
Return true if vertex buffer is currently mapped.
virtual void Unmap() override=0
Finish the vertices updates stage.
boost::scoped_array< GLuint > m_indices
Current indices buffer size.
Definition: gpu_manager.h:144
void resizeIndices(unsigned int aNewSize)
< Resizes the indices buffer to aNewSize if necessary
int m_shaderAttrib
true: enable Z test when drawing
Definition: gpu_manager.h:95
VERTEX_CONTAINER * m_container
Shader handling.
Definition: gpu_manager.h:89
SHADER * m_shader
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:92
void Use()
Use the shader.
Definition: shader.h:126
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:135
void ClearDirty()
Clear the dirty flag to prevent reuploading vertices to the GPU memory.
A small class to help profiling.
Definition: profile.h:47
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
static constexpr size_t COLOR_OFFSET
Definition: vertex_common.h:74
static constexpr size_t COORD_OFFSET
Definition: vertex_common.h:70
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:67
static constexpr size_t SHADER_OFFSET
Definition: vertex_common.h:79
static constexpr size_t COLOR_STRIDE
Definition: vertex_common.h:76
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:81
static constexpr size_t COORD_STRIDE
Definition: vertex_common.h:72
#define KI_TRACE(aWhat,...)

References KIGFX::CACHED_CONTAINER::AllItemsSize(), KIGFX::VERTEX_CONTAINER::ClearDirty(), KIGFX::COLOR_OFFSET, KIGFX::COLOR_STRIDE, KIGFX::COORD_OFFSET, KIGFX::COORD_STRIDE, KIGFX::SHADER::Deactivate(), KIGFX::CACHED_CONTAINER::GetBufferHandle(), KIGFX::CACHED_CONTAINER::IsMapped(), KI_TRACE, KIGFX::GPU_MANAGER::m_container, m_curVrangeSize, KIGFX::GPU_MANAGER::m_enableDepthTest, KIGFX::GPU_CACHED_MANAGER::VRANGE::m_end, m_indexBufMaxSize, m_indexBufSize, m_indices, KIGFX::GPU_CACHED_MANAGER::VRANGE::m_isContinuous, KIGFX::GPU_MANAGER::m_isDrawing, KIGFX::GPU_MANAGER::m_shader, KIGFX::GPU_MANAGER::m_shaderAttrib, KIGFX::GPU_CACHED_MANAGER::VRANGE::m_start, m_vranges, resizeIndices(), KIGFX::SHADER_OFFSET, KIGFX::SHADER_STRIDE, PROF_TIMER::Stop(), PROF_TIMER::to_string(), traceGalProfile, KIGFX::CACHED_CONTAINER::Unmap(), KIGFX::SHADER::Use(), and KIGFX::VERTEX_SIZE.

◆ MakeManager()

GPU_MANAGER * GPU_MANAGER::MakeManager ( VERTEX_CONTAINER aContainer)
staticinherited

Definition at line 48 of file gpu_manager.cpp.

49{
50 if( aContainer->IsCached() )
51 return new GPU_CACHED_MANAGER( aContainer );
52 else
53 return new GPU_NONCACHED_MANAGER( aContainer );
54}
virtual bool IsCached() const =0
Return true if the container caches vertex data in RAM or video memory.

References KIGFX::VERTEX_CONTAINER::IsCached().

Referenced by KIGFX::VERTEX_MANAGER::VERTEX_MANAGER().

◆ Map()

void KIGFX::GPU_CACHED_MANAGER::Map ( )

Unmap vertex buffer.

◆ resizeIndices()

void GPU_CACHED_MANAGER::resizeIndices ( unsigned int  aNewSize)
protected

< Resizes the indices buffer to aNewSize if necessary

Buffers initialization flag

Definition at line 243 of file gpu_manager.cpp.

244{
245 if( aNewSize > m_indicesCapacity )
246 {
247 m_indicesCapacity = aNewSize;
248 m_indices.reset( new GLuint[m_indicesCapacity] );
249 }
250}

References m_indices, and m_indicesCapacity.

Referenced by EndDrawing().

◆ SetShader()

void GPU_MANAGER::SetShader ( SHADER aShader)
virtualinherited

Allow using shaders with the stored data.

Parameters
aShaderis the object that allows using shaders.

Definition at line 72 of file gpu_manager.cpp.

73{
74 m_shader = &aShader;
75 m_shaderAttrib = m_shader->GetAttribute( "a_shaderParams" );
76
77 if( m_shaderAttrib == -1 )
78 {
79 DisplayError( nullptr, wxT( "Could not get the shader attribute location" ) );
80 }
81}
int GetAttribute(const std::string &aAttributeName) const
Get an attribute location.
Definition: shader.cpp:172
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280

References DisplayError(), KIGFX::SHADER::GetAttribute(), KIGFX::GPU_MANAGER::m_shader, and KIGFX::GPU_MANAGER::m_shaderAttrib.

◆ Unmap()

void KIGFX::GPU_CACHED_MANAGER::Unmap ( )

Member Data Documentation

◆ m_buffersInitialized

bool KIGFX::GPU_CACHED_MANAGER::m_buffersInitialized
protected

Pointer to the current indices buffer.

Definition at line 141 of file gpu_manager.h.

◆ m_container

VERTEX_CONTAINER* KIGFX::GPU_MANAGER::m_container
protectedinherited

Shader handling.

Definition at line 89 of file gpu_manager.h.

Referenced by EndDrawing(), and KIGFX::GPU_NONCACHED_MANAGER::EndDrawing().

◆ m_curVrangeSize

unsigned int KIGFX::GPU_CACHED_MANAGER::m_curVrangeSize
protected

Definition at line 165 of file gpu_manager.h.

Referenced by BeginDrawing(), DrawIndices(), and EndDrawing().

◆ m_enableDepthTest

bool KIGFX::GPU_MANAGER::m_enableDepthTest
protectedinherited

◆ m_indexBufMaxSize

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indexBufMaxSize
protected

Size of the current VRANGE.

Definition at line 162 of file gpu_manager.h.

Referenced by BeginDrawing(), and EndDrawing().

◆ m_indexBufSize

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indexBufSize
protected

Maximum size taken by the index buffer for all frames rendered so far.

Definition at line 159 of file gpu_manager.h.

Referenced by BeginDrawing(), DrawIndices(), and EndDrawing().

◆ m_indices

boost::scoped_array<GLuint> KIGFX::GPU_CACHED_MANAGER::m_indices
protected

Current indices buffer size.

Definition at line 144 of file gpu_manager.h.

Referenced by EndDrawing(), and resizeIndices().

◆ m_indicesCapacity

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesCapacity
protected

Ranges of visible vertex indices to render.

Definition at line 147 of file gpu_manager.h.

Referenced by resizeIndices().

◆ m_isDrawing

bool KIGFX::GPU_MANAGER::m_isDrawing
protectedinherited

Container that stores vertices data.

Definition at line 86 of file gpu_manager.h.

Referenced by BeginDrawing(), DrawIndices(), and EndDrawing().

◆ m_shader

SHADER* KIGFX::GPU_MANAGER::m_shader
protectedinherited

Location of shader attributes (for glVertexAttribPointer)

Definition at line 92 of file gpu_manager.h.

Referenced by EndDrawing(), KIGFX::GPU_NONCACHED_MANAGER::EndDrawing(), and KIGFX::GPU_MANAGER::SetShader().

◆ m_shaderAttrib

int KIGFX::GPU_MANAGER::m_shaderAttrib
protectedinherited

true: enable Z test when drawing

Definition at line 95 of file gpu_manager.h.

Referenced by EndDrawing(), KIGFX::GPU_NONCACHED_MANAGER::EndDrawing(), and KIGFX::GPU_MANAGER::SetShader().

◆ m_totalHuge

int KIGFX::GPU_CACHED_MANAGER::m_totalHuge
protected

Number of regular VRANGEs (small items) pooled into single draw call.

Definition at line 153 of file gpu_manager.h.

Referenced by DrawIndices().

◆ m_totalNormal

int KIGFX::GPU_CACHED_MANAGER::m_totalNormal
protected

Current size of index buffer.

Definition at line 156 of file gpu_manager.h.

Referenced by DrawIndices().

◆ m_vranges

std::vector<VRANGE> KIGFX::GPU_CACHED_MANAGER::m_vranges
protected

Number of huge VRANGEs (i.e. large zones) with separate draw calls.

Definition at line 150 of file gpu_manager.h.

Referenced by BeginDrawing(), DrawIndices(), and EndDrawing().


The documentation for this class was generated from the following files: