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 ()
 
virtual void BeginDrawing () override
 > More...
 
virtual void DrawIndices (unsigned int aOffset, unsigned int aSize) override
 > More...
 
virtual void DrawAll () override
 > More...
 
virtual void EndDrawing () override
 > More...
 
void Map ()
 

Maps vertex buffer stored in GPU memory.

More...
 
void Unmap ()
 

Unmaps vertex buffer.

More...
 
virtual void SetShader (SHADER &aShader)
 Function SetShader() Allows using shaders with the stored data. 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
 

Buffers initialization flag

More...
 
boost::scoped_array< GLuint > m_indices
 

Pointer to the current indices buffer

More...
 
GLuint * m_indicesPtr
 

Pointer to the first free cell in the indices buffer

More...
 
GLuint m_indicesBuffer
 

Handle to indices buffer

More...
 
unsigned int m_indicesSize
 

Number of indices stored in the indices buffer

More...
 
unsigned int m_indicesCapacity
 

Current indices buffer size

More...
 
bool m_isDrawing
 

Drawing status flag.

More...
 
VERTEX_CONTAINERm_container
 

Container that stores vertices data.

More...
 
SHADERm_shader
 

Shader handling

More...
 
int m_shaderAttrib
 

Location of shader attributes (for glVertexAttribPointer)

More...
 

Detailed Description

Definition at line 100 of file gpu_manager.h.

Constructor & Destructor Documentation

GPU_CACHED_MANAGER::GPU_CACHED_MANAGER ( VERTEX_CONTAINER aContainer)

Definition at line 82 of file gpu_manager.cpp.

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

82  :
83  GPU_MANAGER( aContainer ), m_buffersInitialized( false ), m_indicesPtr( NULL ),
85 {
86  // Allocate the biggest possible buffer for indices
87  resizeIndices( aContainer->GetSize() );
88 }
unsigned int m_indicesCapacity
Current indices buffer size
Definition: gpu_manager.h:144
bool m_buffersInitialized
Buffers initialization flag
Definition: gpu_manager.h:129
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:141
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:138
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:135
virtual unsigned int GetSize() const
Function GetSize() returns amount of vertices currently stored in the container.
void resizeIndices(unsigned int aNewSize)
Resizes the indices buffer to aNewSize if necessary
GPU_MANAGER(VERTEX_CONTAINER *aContainer)
Definition: gpu_manager.cpp:58
GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER ( )

Definition at line 91 of file gpu_manager.cpp.

References m_buffersInitialized, and m_indicesBuffer.

92 {
94  {
95  glBindBuffer( GL_ARRAY_BUFFER, 0 );
96  glDeleteBuffers( 1, &m_indicesBuffer );
97  }
98 }
bool m_buffersInitialized
Buffers initialization flag
Definition: gpu_manager.h:129
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:138

Member Function Documentation

void GPU_CACHED_MANAGER::BeginDrawing ( )
overridevirtual

>

Function BeginDrawing() Prepares the stored data to be drawn.

Implements KIGFX::GPU_MANAGER.

Definition at line 101 of file gpu_manager.cpp.

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().

102 {
103  wxASSERT( !m_isDrawing );
104 
105  if( !m_buffersInitialized )
106  {
107  glGenBuffers( 1, &m_indicesBuffer );
108  checkGlError( "generating vertices buffer" );
109  m_buffersInitialized = true;
110  }
111 
112  if( m_container->IsDirty() )
114 
115  // Number of vertices to be drawn in the EndDrawing()
116  m_indicesSize = 0;
117  // Set the indices pointer to the beginning of the indices-to-draw buffer
118  m_indicesPtr = m_indices.get();
119 
120  m_isDrawing = true;
121 }
int checkGlError(const std::string &aInfo, bool aThrow)
Checks if one of recent OpenGL operations has failed.
Definition: utils.cpp:30
bool m_buffersInitialized
Buffers initialization flag
Definition: gpu_manager.h:129
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:90
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:141
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:138
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:135
virtual unsigned int GetSize() const
Function GetSize() returns amount of vertices currently stored in the container.
bool IsDirty()
Function IsDirty() returns information about container cache state.
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:87
void resizeIndices(unsigned int aNewSize)
Resizes the indices buffer to aNewSize if necessary
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:132
void GPU_CACHED_MANAGER::DrawAll ( )
overridevirtual

>

Function DrawIndices() Makes the GPU draw all the vertices stored in the container.

Implements KIGFX::GPU_MANAGER.

Definition at line 135 of file gpu_manager.cpp.

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

136 {
137  wxASSERT( m_isDrawing );
138 
139  for( unsigned int i = 0; i < m_indicesSize; *m_indicesPtr++ = i++ );
140 
141  m_indicesSize = m_container->GetSize();
142 }
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:90
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:141
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:135
virtual unsigned int GetSize() const
Function GetSize() returns amount of vertices currently stored in the container.
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:87
void GPU_CACHED_MANAGER::DrawIndices ( unsigned int  aOffset,
unsigned int  aSize 
)
overridevirtual

>

Function DrawIndices() Makes 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 124 of file gpu_manager.cpp.

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

125 {
126  wxASSERT( m_isDrawing );
127 
128  // Copy indices of items that should be drawn to GPU memory
129  for( unsigned int i = aOffset; i < aOffset + aSize; *m_indicesPtr++ = i++ );
130 
131  m_indicesSize += aSize;
132 }
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:141
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:135
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:87
void GPU_CACHED_MANAGER::EndDrawing ( )
overridevirtual

>

Function EndDrawing() Clears the container after drawing routines.

Implements KIGFX::GPU_MANAGER.

Definition at line 145 of file gpu_manager.cpp.

References KIGFX::ColorOffset, KIGFX::ColorStride, KIGFX::CoordStride, KIGFX::SHADER::Deactivate(), KIGFX::CACHED_CONTAINER::GetBufferHandle(), KIGFX::CACHED_CONTAINER::IsMapped(), KIGFX::GPU_MANAGER::m_container, 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::ShaderOffset, KIGFX::ShaderStride, PROF_COUNTER::Stop(), KIGFX::CACHED_CONTAINER::Unmap(), KIGFX::SHADER::Use(), and KIGFX::VertexSize.

146 {
147 #ifdef __WXDEBUG__
148  PROF_COUNTER totalRealTime;
149 #endif /* __WXDEBUG__ */
150 
151  wxASSERT( m_isDrawing );
152 
153  CACHED_CONTAINER* cached = static_cast<CACHED_CONTAINER*>( m_container );
154 
155  if( cached->IsMapped() )
156  cached->Unmap();
157 
158  if( m_indicesSize == 0 )
159  {
160  m_isDrawing = false;
161  return;
162  }
163 
164  // Prepare buffers
165  glEnableClientState( GL_VERTEX_ARRAY );
166  glEnableClientState( GL_COLOR_ARRAY );
167 
168  // Bind vertices data buffers
169  glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
170  glVertexPointer( CoordStride, GL_FLOAT, VertexSize, 0 );
171  glColorPointer( ColorStride, GL_UNSIGNED_BYTE, VertexSize, (GLvoid*) ColorOffset );
172 
173  if( m_shader != NULL ) // Use shader if applicable
174  {
175  m_shader->Use();
176  glEnableVertexAttribArray( m_shaderAttrib );
177  glVertexAttribPointer( m_shaderAttrib, ShaderStride, GL_FLOAT, GL_FALSE,
178  VertexSize, (GLvoid*) ShaderOffset );
179  }
180 
181  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_indicesBuffer );
182  glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_indicesSize * sizeof(int),
183  (GLvoid*) m_indices.get(), GL_DYNAMIC_DRAW );
184 
185  glDrawElements( GL_TRIANGLES, m_indicesSize, GL_UNSIGNED_INT, 0 );
186 
187 #ifdef __WXDEBUG__
188  wxLogTrace( "GAL_PROFILE", wxT( "Cached manager size: %d" ), m_indicesSize );
189 #endif /* __WXDEBUG__ */
190 
191  glBindBuffer( GL_ARRAY_BUFFER, 0 );
192  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
193 
194  // Deactivate vertex array
195  glDisableClientState( GL_COLOR_ARRAY );
196  glDisableClientState( GL_VERTEX_ARRAY );
197 
198  if( m_shader != NULL )
199  {
200  glDisableVertexAttribArray( m_shaderAttrib );
201  m_shader->Deactivate();
202  }
203 
204  m_isDrawing = false;
205 
206 #ifdef __WXDEBUG__
207  totalRealTime.Stop();
208  wxLogTrace( "GAL_PROFILE",
209  wxT( "GPU_CACHED_MANAGER::EndDrawing(): %.1f ms" ), totalRealTime.msecs() );
210 #endif /* __WXDEBUG__ */
211 }
const size_t VertexSize
Definition: vertex_common.h:57
void Stop()
save the time when this function was called, and set the counter stane to stop
Definition: profile.h:82
const size_t CoordStride
Definition: vertex_common.h:61
int m_shaderAttrib
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:96
void Use()
Use the shader.
Definition: shader.h:130
unsigned int GetBufferHandle() const
Function GetBufferHandle() returns handle to the vertex buffer.
const size_t ShaderOffset
Definition: vertex_common.h:70
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:45
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:90
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:141
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:138
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:139
const size_t ShaderStride
Definition: vertex_common.h:72
double msecs() const
Definition: profile.h:121
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:87
const size_t ColorStride
Definition: vertex_common.h:67
SHADER * m_shader
Shader handling
Definition: gpu_manager.h:93
bool IsMapped() const
Function IsMapped() returns true if vertex buffer is currently mapped.
const size_t ColorOffset
Definition: vertex_common.h:64
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:132
GPU_MANAGER * GPU_MANAGER::MakeManager ( VERTEX_CONTAINER aContainer)
staticinherited

Definition at line 46 of file gpu_manager.cpp.

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

47 {
48  if( typeid( *aContainer ) == typeid( CACHED_CONTAINER ) )
49  return new GPU_CACHED_MANAGER( aContainer );
50  else if( typeid( *aContainer ) == typeid( NONCACHED_CONTAINER ) )
51  return new GPU_NONCACHED_MANAGER( aContainer );
52 
53  wxASSERT_MSG( false, wxT( "Not handled container type" ) );
54  return NULL;
55 }
void KIGFX::GPU_CACHED_MANAGER::Map ( )

Maps vertex buffer stored in GPU memory.

void GPU_CACHED_MANAGER::resizeIndices ( unsigned int  aNewSize)
protected

Resizes the indices buffer to aNewSize if necessary

Definition at line 214 of file gpu_manager.cpp.

References m_indices, and m_indicesCapacity.

Referenced by BeginDrawing(), and GPU_CACHED_MANAGER().

215 {
216  if( aNewSize > m_indicesCapacity )
217  {
218  m_indicesCapacity = aNewSize;
219  m_indices.reset( new GLuint[m_indicesCapacity] );
220  }
221 }
unsigned int m_indicesCapacity
Current indices buffer size
Definition: gpu_manager.h:144
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:132
void GPU_MANAGER::SetShader ( SHADER aShader)
virtualinherited

Function SetShader() Allows using shaders with the stored data.

Parameters
aShaderis the object that allows using shaders.

Definition at line 69 of file gpu_manager.cpp.

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

70 {
71  m_shader = &aShader;
72  m_shaderAttrib = m_shader->GetAttribute( "attrShaderParams" );
73 
74  if( m_shaderAttrib == -1 )
75  {
76  DisplayError( NULL, wxT( "Could not get the shader attribute location" ) );
77  }
78 }
int m_shaderAttrib
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:96
int GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:160
SHADER * m_shader
Shader handling
Definition: gpu_manager.h:93
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
void KIGFX::GPU_CACHED_MANAGER::Unmap ( )

Unmaps vertex buffer.

Member Data Documentation

bool KIGFX::GPU_CACHED_MANAGER::m_buffersInitialized
protected

Buffers initialization flag

Definition at line 129 of file gpu_manager.h.

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

VERTEX_CONTAINER* KIGFX::GPU_MANAGER::m_container
protectedinherited

Container that stores vertices data.

Definition at line 90 of file gpu_manager.h.

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

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

Pointer to the current indices buffer

Definition at line 132 of file gpu_manager.h.

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

GLuint KIGFX::GPU_CACHED_MANAGER::m_indicesBuffer
protected

Handle to indices buffer

Definition at line 138 of file gpu_manager.h.

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

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesCapacity
protected

Current indices buffer size

Definition at line 144 of file gpu_manager.h.

Referenced by resizeIndices().

GLuint* KIGFX::GPU_CACHED_MANAGER::m_indicesPtr
protected

Pointer to the first free cell in the indices buffer

Definition at line 135 of file gpu_manager.h.

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

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesSize
protected

Number of indices stored in the indices buffer

Definition at line 141 of file gpu_manager.h.

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

bool KIGFX::GPU_MANAGER::m_isDrawing
protectedinherited

Drawing status flag.

Definition at line 87 of file gpu_manager.h.

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

SHADER* KIGFX::GPU_MANAGER::m_shader
protectedinherited

Shader handling

Definition at line 93 of file gpu_manager.h.

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

int KIGFX::GPU_MANAGER::m_shaderAttrib
protectedinherited

Location of shader attributes (for glVertexAttribPointer)

Definition at line 96 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: