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

Constructor & Destructor Documentation

GPU_CACHED_MANAGER::GPU_CACHED_MANAGER ( VERTEX_CONTAINER aContainer)

Definition at line 75 of file gpu_manager.cpp.

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

75  :
76  GPU_MANAGER( aContainer ), m_buffersInitialized( false ), m_indicesPtr( NULL ),
78 {
79  // Allocate the biggest possible buffer for indices
80  resizeIndices( aContainer->GetSize() );
81 }
unsigned int m_indicesCapacity
Current indices buffer size
Definition: gpu_manager.h:143
bool m_buffersInitialized
Buffers initialization flag
Definition: gpu_manager.h:128
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:140
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:137
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:134
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:51
GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER ( )

Definition at line 84 of file gpu_manager.cpp.

References m_buffersInitialized, and m_indicesBuffer.

85 {
87  {
88  glBindBuffer( GL_ARRAY_BUFFER, 0 );
89  glDeleteBuffers( 1, &m_indicesBuffer );
90  }
91 }
bool m_buffersInitialized
Buffers initialization flag
Definition: gpu_manager.h:128
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:137

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

95 {
96  wxASSERT( !m_isDrawing );
97 
99  {
100  glGenBuffers( 1, &m_indicesBuffer );
101  checkGlError( "generating vertices buffer" );
102  m_buffersInitialized = true;
103  }
104 
105  if( m_container->IsDirty() )
107 
108  // Number of vertices to be drawn in the EndDrawing()
109  m_indicesSize = 0;
110  // Set the indices pointer to the beginning of the indices-to-draw buffer
111  m_indicesPtr = m_indices.get();
112 
113  m_isDrawing = true;
114 }
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:128
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:89
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:140
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:137
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:134
bool IsDirty() const
Returns information about the container cache state.
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:86
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:131
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 128 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.

129 {
130  wxASSERT( m_isDrawing );
131 
132  for( unsigned int i = 0; i < m_indicesSize; *m_indicesPtr++ = i++ );
133 
134  m_indicesSize = m_container->GetSize();
135 }
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:89
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:140
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:134
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:86
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 117 of file gpu_manager.cpp.

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

118 {
119  wxASSERT( m_isDrawing );
120 
121  // Copy indices of items that should be drawn to GPU memory
122  for( unsigned int i = aOffset; i < aOffset + aSize; *m_indicesPtr++ = i++ );
123 
124  m_indicesSize += aSize;
125 }
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:140
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:134
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:86
void GPU_CACHED_MANAGER::EndDrawing ( )
overridevirtual

>

Function EndDrawing() Clears the container after drawing routines.

Implements KIGFX::GPU_MANAGER.

Definition at line 138 of file gpu_manager.cpp.

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, 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(), KIGFX::CACHED_CONTAINER::Unmap(), KIGFX::SHADER::Use(), and KIGFX::VERTEX_SIZE.

139 {
140 #ifdef __WXDEBUG__
141  PROF_COUNTER totalRealTime;
142 #endif /* __WXDEBUG__ */
143 
144  wxASSERT( m_isDrawing );
145 
146  CACHED_CONTAINER* cached = static_cast<CACHED_CONTAINER*>( m_container );
147 
148  if( cached->IsMapped() )
149  cached->Unmap();
150 
151  if( m_indicesSize == 0 )
152  {
153  m_isDrawing = false;
154  return;
155  }
156 
157  // Prepare buffers
158  glEnableClientState( GL_VERTEX_ARRAY );
159  glEnableClientState( GL_COLOR_ARRAY );
160 
161  // Bind vertices data buffers
162  glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
163  glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, (GLvoid*) COORD_OFFSET );
164  glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, (GLvoid*) COLOR_OFFSET );
165 
166  if( m_shader != NULL ) // Use shader if applicable
167  {
168  m_shader->Use();
169  glEnableVertexAttribArray( m_shaderAttrib );
170  glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE,
171  VERTEX_SIZE, (GLvoid*) SHADER_OFFSET );
172  }
173 
174  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_indicesBuffer );
175  glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_indicesSize * sizeof(int),
176  (GLvoid*) m_indices.get(), GL_DYNAMIC_DRAW );
177 
178  glDrawElements( GL_TRIANGLES, m_indicesSize, GL_UNSIGNED_INT, 0 );
179 
180 #ifdef __WXDEBUG__
181  wxLogTrace( "GAL_PROFILE", wxT( "Cached manager size: %d" ), m_indicesSize );
182 #endif /* __WXDEBUG__ */
183 
184  glBindBuffer( GL_ARRAY_BUFFER, 0 );
185  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
186  cached->ClearDirty();
187 
188  // Deactivate vertex array
189  glDisableClientState( GL_COLOR_ARRAY );
190  glDisableClientState( GL_VERTEX_ARRAY );
191 
192  if( m_shader != NULL )
193  {
194  glDisableVertexAttribArray( m_shaderAttrib );
195  m_shader->Deactivate();
196  }
197 
198  m_isDrawing = false;
199 
200 #ifdef __WXDEBUG__
201  totalRealTime.Stop();
202  wxLogTrace( "GAL_PROFILE",
203  wxT( "GPU_CACHED_MANAGER::EndDrawing(): %.1f ms" ), totalRealTime.msecs() );
204 #endif /* __WXDEBUG__ */
205 }
void Stop()
save the time when this function was called, and set the counter stane to stop
Definition: profile.h:82
static constexpr size_t COORD_STRIDE
Definition: vertex_common.h:62
int m_shaderAttrib
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:95
void Use()
Use the shader.
Definition: shader.h:130
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:89
virtual unsigned int GetBufferHandle() const =0
Returns handle to the vertex buffer.
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:140
Class to store VERTEX instances with caching.
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:137
static constexpr size_t SHADER_OFFSET
Definition: vertex_common.h:69
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:139
double msecs() const
Definition: profile.h:124
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:86
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:57
static constexpr size_t COLOR_OFFSET
Definition: vertex_common.h:64
SHADER * m_shader
Shader handling
Definition: gpu_manager.h:92
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:71
virtual bool IsMapped() const =0
Returns true if vertex buffer is currently mapped.
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:131
void ClearDirty()
Clears the dirty flag to prevent reuploading vertices to the GPU memory.
virtual void Unmap() override=0
>
static constexpr size_t COORD_OFFSET
Definition: vertex_common.h:60
static constexpr size_t COLOR_STRIDE
Definition: vertex_common.h:66
GPU_MANAGER * GPU_MANAGER::MakeManager ( VERTEX_CONTAINER aContainer)
staticinherited

Definition at line 42 of file gpu_manager.cpp.

References KIGFX::VERTEX_CONTAINER::IsCached().

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

43 {
44  if( aContainer->IsCached() )
45  return new GPU_CACHED_MANAGER( aContainer );
46  else
47  return new GPU_NONCACHED_MANAGER( aContainer );
48 }
virtual bool IsCached() const =0
Returns true if the container caches vertex data in RAM or video memory.
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 208 of file gpu_manager.cpp.

References m_indices, and m_indicesCapacity.

Referenced by BeginDrawing(), and GPU_CACHED_MANAGER().

209 {
210  if( aNewSize > m_indicesCapacity )
211  {
212  m_indicesCapacity = aNewSize;
213  m_indices.reset( new GLuint[m_indicesCapacity] );
214  }
215 }
unsigned int m_indicesCapacity
Current indices buffer size
Definition: gpu_manager.h:143
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:131
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 62 of file gpu_manager.cpp.

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

63 {
64  m_shader = &aShader;
65  m_shaderAttrib = m_shader->GetAttribute( "attrShaderParams" );
66 
67  if( m_shaderAttrib == -1 )
68  {
69  DisplayError( NULL, wxT( "Could not get the shader attribute location" ) );
70  }
71 }
int m_shaderAttrib
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:95
int GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:160
SHADER * m_shader
Shader handling
Definition: gpu_manager.h:92
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
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 128 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 89 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 131 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 137 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 143 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 134 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 140 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 86 of file gpu_manager.h.

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

SHADER* KIGFX::GPU_MANAGER::m_shader
protectedinherited

Shader handling

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