KiCad PCB EDA Suite
KIGFX::GPU_CACHED_MANAGER Class Reference

#include <gpu_manager.h>

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

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 (unsigned int aOffset, unsigned int aSize) override
 Make the GPU draw all the vertices stored in the container. More...
 
virtual void DrawAll () 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
 Pointer to the first free cell in the indices buffer. More...
 
GLuint * m_indicesPtr
 Handle to indices buffer. More...
 
GLuint m_indicesBuffer
 Number of indices stored in the indices buffer. More...
 
unsigned int m_indicesSize
 Current indices buffer size. More...
 
unsigned int m_indicesCapacity
 
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 105 of file gpu_manager.h.

Constructor & Destructor Documentation

◆ GPU_CACHED_MANAGER()

GPU_CACHED_MANAGER::GPU_CACHED_MANAGER ( VERTEX_CONTAINER aContainer)

Definition at line 82 of file gpu_manager.cpp.

82  :
83  GPU_MANAGER( aContainer ),
84  m_buffersInitialized( false ),
85  m_indicesPtr( nullptr ),
86  m_indicesBuffer( 0 ),
87  m_indicesSize( 0 ),
89 {
90  // Allocate the biggest possible buffer for indices
91  resizeIndices( aContainer->GetSize() );
92 }
unsigned int m_indicesCapacity
Definition: gpu_manager.h:149
bool m_buffersInitialized
Pointer to the current indices buffer.
Definition: gpu_manager.h:134
unsigned int m_indicesSize
Current indices buffer size.
Definition: gpu_manager.h:146
GLuint m_indicesBuffer
Number of indices stored in the indices buffer.
Definition: gpu_manager.h:143
GLuint * m_indicesPtr
Handle to indices buffer.
Definition: gpu_manager.h:140
void resizeIndices(unsigned int aNewSize)
< Resizes the indices buffer to aNewSize if necessary
GPU_MANAGER(VERTEX_CONTAINER *aContainer)
Drawing status flag.
Definition: gpu_manager.cpp:54
virtual unsigned int GetSize() const
Return amount of vertices currently stored in the container.

References KIGFX::VERTEX_CONTAINER::GetSize(), and resizeIndices().

◆ ~GPU_CACHED_MANAGER()

GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER ( )

Prepare the stored data to be drawn.

Definition at line 95 of file gpu_manager.cpp.

96 {
98  {
99  if( glBindBuffer )
100  glBindBuffer( GL_ARRAY_BUFFER, 0 );
101 
102  if( glDeleteBuffers )
103  glDeleteBuffers( 1, &m_indicesBuffer );
104  }
105 }
bool m_buffersInitialized
Pointer to the current indices buffer.
Definition: gpu_manager.h:134
GLuint m_indicesBuffer
Number of indices stored in the indices buffer.
Definition: gpu_manager.h:143

References m_buffersInitialized, and m_indicesBuffer.

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 108 of file gpu_manager.cpp.

109 {
110  wxASSERT( !m_isDrawing );
111 
112  if( !m_buffersInitialized )
113  {
114  glGenBuffers( 1, &m_indicesBuffer );
115  checkGlError( "generating vertices buffer", __FILE__, __LINE__ );
116  m_buffersInitialized = true;
117  }
118 
119  if( m_container->IsDirty() )
121 
122  // Number of vertices to be drawn in the EndDrawing()
123  m_indicesSize = 0;
124  // Set the indices pointer to the beginning of the indices-to-draw buffer
125  m_indicesPtr = m_indices.get();
126 
127  m_isDrawing = true;
128 }
bool IsDirty() const
Return information about the container cache state.
int checkGlError(const std::string &aInfo, const char *aFile, int aLine, bool aThrow)
Check if a recent OpenGL operation has failed.
Definition: utils.cpp:45
bool m_buffersInitialized
Pointer to the current indices buffer.
Definition: gpu_manager.h:134
VERTEX_CONTAINER * m_container
Shader handling.
Definition: gpu_manager.h:92
unsigned int m_indicesSize
Current indices buffer size.
Definition: gpu_manager.h:146
GLuint m_indicesBuffer
Number of indices stored in the indices buffer.
Definition: gpu_manager.h:143
GLuint * m_indicesPtr
Handle to indices buffer.
Definition: gpu_manager.h:140
bool m_isDrawing
Container that stores vertices data.
Definition: gpu_manager.h:89
void resizeIndices(unsigned int aNewSize)
< Resizes the indices buffer to aNewSize if necessary
virtual unsigned int GetSize() const
Return amount of vertices currently stored in the container.
boost::scoped_array< GLuint > m_indices
Pointer to the first free cell in the indices buffer.
Definition: gpu_manager.h:137

References checkGlError(), KIGFX::VERTEX_CONTAINER::GetSize(), KIGFX::VERTEX_CONTAINER::IsDirty(), m_buffersInitialized, KIGFX::GPU_MANAGER::m_container, m_indices, m_indicesBuffer, m_indicesPtr, m_indicesSize, KIGFX::GPU_MANAGER::m_isDrawing, and resizeIndices().

◆ DrawAll()

void GPU_CACHED_MANAGER::DrawAll ( )
overridevirtual

Clear the container after drawing routines.

Implements KIGFX::GPU_MANAGER.

Definition at line 143 of file gpu_manager.cpp.

144 {
145  wxASSERT( m_isDrawing );
146 
147  for( unsigned int i = 0; i < m_indicesSize; *m_indicesPtr++ = i++ )
148  ;
149 
151 }
VERTEX_CONTAINER * m_container
Shader handling.
Definition: gpu_manager.h:92
unsigned int m_indicesSize
Current indices buffer size.
Definition: gpu_manager.h:146
GLuint * m_indicesPtr
Handle to indices buffer.
Definition: gpu_manager.h:140
bool m_isDrawing
Container that stores vertices data.
Definition: gpu_manager.h:89
virtual unsigned int GetSize() const
Return amount of vertices currently stored in the container.

References KIGFX::VERTEX_CONTAINER::GetSize(), KIGFX::GPU_MANAGER::m_container, m_indicesPtr, m_indicesSize, and KIGFX::GPU_MANAGER::m_isDrawing.

◆ DrawIndices()

void GPU_CACHED_MANAGER::DrawIndices ( unsigned int  aOffset,
unsigned int  aSize 
)
overridevirtual

Make the GPU draw all the vertices stored in the container.

Implements KIGFX::GPU_MANAGER.

Definition at line 131 of file gpu_manager.cpp.

132 {
133  wxASSERT( m_isDrawing );
134 
135  // Copy indices of items that should be drawn to GPU memory
136  for( unsigned int i = aOffset; i < aOffset + aSize; *m_indicesPtr++ = i++ )
137  ;
138 
139  m_indicesSize += aSize;
140 }
unsigned int m_indicesSize
Current indices buffer size.
Definition: gpu_manager.h:146
GLuint * m_indicesPtr
Handle to indices buffer.
Definition: gpu_manager.h:140
bool m_isDrawing
Container that stores vertices data.
Definition: gpu_manager.h:89

References m_indicesPtr, m_indicesSize, and KIGFX::GPU_MANAGER::m_isDrawing.

◆ EnableDepthTest()

void GPU_MANAGER::EnableDepthTest ( bool  aEnabled)
inherited

Enable/disable Z buffer depth test.

Definition at line 325 of file gpu_manager.cpp.

326 {
327  m_enableDepthTest = aEnabled;
328 }

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 154 of file gpu_manager.cpp.

155 {
156 #ifdef KICAD_GAL_PROFILE
157  PROF_COUNTER totalRealTime;
158 #endif /* KICAD_GAL_PROFILE */
159 
160  wxASSERT( m_isDrawing );
161 
162  CACHED_CONTAINER* cached = static_cast<CACHED_CONTAINER*>( m_container );
163 
164  if( cached->IsMapped() )
165  cached->Unmap();
166 
167  if( m_indicesSize == 0 )
168  {
169  m_isDrawing = false;
170  return;
171  }
172 
173  if( m_enableDepthTest )
174  glEnable( GL_DEPTH_TEST );
175  else
176  glDisable( GL_DEPTH_TEST );
177 
178  // Prepare buffers
179  glEnableClientState( GL_VERTEX_ARRAY );
180  glEnableClientState( GL_COLOR_ARRAY );
181 
182  // Bind vertices data buffers
183  glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
184  glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, (GLvoid*) COORD_OFFSET );
185  glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, (GLvoid*) COLOR_OFFSET );
186 
187  if( m_shader != nullptr ) // Use shader if applicable
188  {
189  m_shader->Use();
190  glEnableVertexAttribArray( m_shaderAttrib );
191  glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE, VERTEX_SIZE,
192  (GLvoid*) SHADER_OFFSET );
193  }
194 
195  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_indicesBuffer );
196  glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_indicesSize * sizeof( int ), (GLvoid*) m_indices.get(),
197  GL_DYNAMIC_DRAW );
198 
199  glDrawElements( GL_TRIANGLES, m_indicesSize, GL_UNSIGNED_INT, nullptr );
200 
201 #ifdef KICAD_GAL_PROFILE
202  wxLogTrace( traceGalProfile, wxT( "Cached manager size: %d" ), m_indicesSize );
203 #endif /* KICAD_GAL_PROFILE */
204 
205  glBindBuffer( GL_ARRAY_BUFFER, 0 );
206  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
207  cached->ClearDirty();
208 
209  // Deactivate vertex array
210  glDisableClientState( GL_COLOR_ARRAY );
211  glDisableClientState( GL_VERTEX_ARRAY );
212 
213  if( m_shader != nullptr )
214  {
215  glDisableVertexAttribArray( m_shaderAttrib );
216  m_shader->Deactivate();
217  }
218 
219  m_isDrawing = false;
220 
221 #ifdef KICAD_GAL_PROFILE
222  totalRealTime.Stop();
223  wxLogTrace( traceGalProfile, wxT( "GPU_CACHED_MANAGER::EndDrawing(): %.1f ms" ),
224  totalRealTime.msecs() );
225 #endif /* KICAD_GAL_PROFILE */
226 }
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition: profile.h:85
static constexpr size_t COORD_STRIDE
Definition: vertex_common.h:72
double msecs(bool aSinceLast=false)
Definition: profile.h:146
int m_shaderAttrib
true: enable Z test when drawing
Definition: gpu_manager.h:98
void Use()
Use the shader.
Definition: shader.h:126
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
A small class to help profiling.
Definition: profile.h:45
VERTEX_CONTAINER * m_container
Shader handling.
Definition: gpu_manager.h:92
virtual unsigned int GetBufferHandle() const =0
Return handle to the vertex buffer.
unsigned int m_indicesSize
Current indices buffer size.
Definition: gpu_manager.h:146
Class to store VERTEX instances with caching.
GLuint m_indicesBuffer
Number of indices stored in the indices buffer.
Definition: gpu_manager.h:143
static constexpr size_t SHADER_OFFSET
Definition: vertex_common.h:79
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:135
bool m_isDrawing
Container that stores vertices data.
Definition: gpu_manager.h:89
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:67
static constexpr size_t COLOR_OFFSET
Definition: vertex_common.h:74
SHADER * m_shader
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:95
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:81
virtual bool IsMapped() const =0
Return true if vertex buffer is currently mapped.
boost::scoped_array< GLuint > m_indices
Pointer to the first free cell in the indices buffer.
Definition: gpu_manager.h:137
void ClearDirty()
Clear the dirty flag to prevent reuploading vertices to the GPU memory.
virtual void Unmap() override=0
Finish the vertices updates stage.
static constexpr size_t COORD_OFFSET
Definition: vertex_common.h:70
static constexpr size_t COLOR_STRIDE
Definition: vertex_common.h:76

References 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(), KIGFX::GPU_MANAGER::m_container, KIGFX::GPU_MANAGER::m_enableDepthTest, m_indices, m_indicesBuffer, m_indicesSize, KIGFX::GPU_MANAGER::m_isDrawing, KIGFX::GPU_MANAGER::m_shader, KIGFX::GPU_MANAGER::m_shaderAttrib, PROF_COUNTER::msecs(), KIGFX::SHADER_OFFSET, KIGFX::SHADER_STRIDE, PROF_COUNTER::Stop(), 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 45 of file gpu_manager.cpp.

46 {
47  if( aContainer->IsCached() )
48  return new GPU_CACHED_MANAGER( aContainer );
49  else
50  return new GPU_NONCACHED_MANAGER( aContainer );
51 }
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 229 of file gpu_manager.cpp.

230 {
231  if( aNewSize > m_indicesCapacity )
232  {
233  m_indicesCapacity = aNewSize;
234  m_indices.reset( new GLuint[m_indicesCapacity] );
235  }
236 }
unsigned int m_indicesCapacity
Definition: gpu_manager.h:149
boost::scoped_array< GLuint > m_indices
Pointer to the first free cell in the indices buffer.
Definition: gpu_manager.h:137

References m_indices, and m_indicesCapacity.

Referenced by BeginDrawing(), and GPU_CACHED_MANAGER().

◆ 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 69 of file gpu_manager.cpp.

70 {
71  m_shader = &aShader;
72  m_shaderAttrib = m_shader->GetAttribute( "attrShaderParams" );
73 
74  if( m_shaderAttrib == -1 )
75  {
76  DisplayError( nullptr, wxT( "Could not get the shader attribute location" ) );
77  }
78 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:271
int GetAttribute(const std::string &aAttributeName) const
Get an attribute location.
Definition: shader.cpp:172
int m_shaderAttrib
true: enable Z test when drawing
Definition: gpu_manager.h:98
SHADER * m_shader
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:95

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 134 of file gpu_manager.h.

Referenced by BeginDrawing(), and ~GPU_CACHED_MANAGER().

◆ m_container

VERTEX_CONTAINER* KIGFX::GPU_MANAGER::m_container
protectedinherited

Shader handling.

Definition at line 92 of file gpu_manager.h.

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

◆ m_enableDepthTest

bool KIGFX::GPU_MANAGER::m_enableDepthTest
protectedinherited

◆ m_indices

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

Pointer to the first free cell in the indices buffer.

Definition at line 137 of file gpu_manager.h.

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

◆ m_indicesBuffer

GLuint KIGFX::GPU_CACHED_MANAGER::m_indicesBuffer
protected

Number of indices stored in the indices buffer.

Definition at line 143 of file gpu_manager.h.

Referenced by BeginDrawing(), EndDrawing(), and ~GPU_CACHED_MANAGER().

◆ m_indicesCapacity

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesCapacity
protected

Definition at line 149 of file gpu_manager.h.

Referenced by resizeIndices().

◆ m_indicesPtr

GLuint* KIGFX::GPU_CACHED_MANAGER::m_indicesPtr
protected

Handle to indices buffer.

Definition at line 140 of file gpu_manager.h.

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

◆ m_indicesSize

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesSize
protected

Current indices buffer size.

Definition at line 146 of file gpu_manager.h.

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

◆ m_isDrawing

bool KIGFX::GPU_MANAGER::m_isDrawing
protectedinherited

Container that stores vertices data.

Definition at line 89 of file gpu_manager.h.

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

◆ m_shader

SHADER* KIGFX::GPU_MANAGER::m_shader
protectedinherited

Location of shader attributes (for glVertexAttribPointer)

Definition at line 95 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 98 of file gpu_manager.h.

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


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