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...
 
void EnableDepthTest (bool aEnabled)
 Function EnableDepthTest() Enables/disables 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
 

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...
 
bool m_enableDepthTest
 

true: enable Z test when drawing

More...
 

Detailed Description

Definition at line 108 of file gpu_manager.h.

Constructor & Destructor Documentation

◆ GPU_CACHED_MANAGER()

GPU_CACHED_MANAGER::GPU_CACHED_MANAGER ( VERTEX_CONTAINER aContainer)

Definition at line 75 of file gpu_manager.cpp.

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:152
bool m_buffersInitialized
Buffers initialization flag
Definition: gpu_manager.h:137
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:149
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:146
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:143
void resizeIndices(unsigned int aNewSize)
Resizes the indices buffer to aNewSize if necessary
GPU_MANAGER(VERTEX_CONTAINER *aContainer)
Definition: gpu_manager.cpp:51
virtual unsigned int GetSize() const
Function GetSize() returns amount of vertices currently stored in the container.

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

◆ ~GPU_CACHED_MANAGER()

GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER ( )

Definition at line 84 of file gpu_manager.cpp.

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:137
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:146

References m_buffersInitialized, and m_indicesBuffer.

Member Function Documentation

◆ BeginDrawing()

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.

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 }
bool IsDirty() const
Returns information about the container cache state.
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:137
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:95
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:149
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:146
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:143
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:92
void resizeIndices(unsigned int aNewSize)
Resizes the indices buffer to aNewSize if necessary
virtual unsigned int GetSize() const
Function GetSize() returns amount of vertices currently stored in the container.
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:140

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

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.

129 {
130  wxASSERT( m_isDrawing );
131 
132  for( unsigned int i = 0; i < m_indicesSize; *m_indicesPtr++ = i++ );
133 
135 }
VERTEX_CONTAINER * m_container
Container that stores vertices data.
Definition: gpu_manager.h:95
unsigned int m_indicesSize
Number of indices stored in the indices buffer
Definition: gpu_manager.h:149
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:143
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:92
size_t i
Definition: json11.cpp:597
virtual unsigned int GetSize() const
Function GetSize() returns amount of vertices currently stored in the container.

References KIGFX::VERTEX_CONTAINER::GetSize(), i, 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

Implements KIGFX::GPU_MANAGER.

Definition at line 117 of file gpu_manager.cpp.

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:149
GLuint * m_indicesPtr
Pointer to the first free cell in the indices buffer
Definition: gpu_manager.h:143
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:92
size_t i
Definition: json11.cpp:597

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

◆ EnableDepthTest()

void GPU_MANAGER::EnableDepthTest ( bool  aEnabled)
inherited

Function EnableDepthTest() Enables/disables Z buffer depth test.

Definition at line 309 of file gpu_manager.cpp.

310 {
311  m_enableDepthTest = aEnabled;
312 }
bool m_enableDepthTest
true: enable Z test when drawing
Definition: gpu_manager.h:104

References KIGFX::GPU_MANAGER::m_enableDepthTest.

◆ EndDrawing()

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.

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  if( m_enableDepthTest )
158  glEnable( GL_DEPTH_TEST );
159  else
160  glDisable( GL_DEPTH_TEST );
161 
162  // Prepare buffers
163  glEnableClientState( GL_VERTEX_ARRAY );
164  glEnableClientState( GL_COLOR_ARRAY );
165 
166  // Bind vertices data buffers
167  glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
168  glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, (GLvoid*) COORD_OFFSET );
169  glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, (GLvoid*) COLOR_OFFSET );
170 
171  if( m_shader != NULL ) // Use shader if applicable
172  {
173  m_shader->Use();
174  glEnableVertexAttribArray( m_shaderAttrib );
175  glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE,
176  VERTEX_SIZE, (GLvoid*) SHADER_OFFSET );
177  }
178 
179  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_indicesBuffer );
180  glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_indicesSize * sizeof(int),
181  (GLvoid*) m_indices.get(), GL_DYNAMIC_DRAW );
182 
183  glDrawElements( GL_TRIANGLES, m_indicesSize, GL_UNSIGNED_INT, 0 );
184 
185 #ifdef __WXDEBUG__
186  wxLogTrace( "GAL_PROFILE", wxT( "Cached manager size: %d" ), m_indicesSize );
187 #endif /* __WXDEBUG__ */
188 
189  glBindBuffer( GL_ARRAY_BUFFER, 0 );
190  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
191  cached->ClearDirty();
192 
193  // Deactivate vertex array
194  glDisableClientState( GL_COLOR_ARRAY );
195  glDisableClientState( GL_VERTEX_ARRAY );
196 
197  if( m_shader != NULL )
198  {
199  glDisableVertexAttribArray( m_shaderAttrib );
200  m_shader->Deactivate();
201  }
202 
203  m_isDrawing = false;
204 
205 #ifdef __WXDEBUG__
206  totalRealTime.Stop();
207  wxLogTrace( "GAL_PROFILE",
208  wxT( "GPU_CACHED_MANAGER::EndDrawing(): %.1f ms" ), totalRealTime.msecs() );
209 #endif /* __WXDEBUG__ */
210 }
void Stop()
save the time when this function was called, and set the counter stane to stop
Definition: profile.h:83
static constexpr size_t COORD_STRIDE
Definition: vertex_common.h:68
int m_shaderAttrib
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:101
void Use()
Use the shader.
Definition: shader.h:132
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:95
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:149
Class to store VERTEX instances with caching.
GLuint m_indicesBuffer
Handle to indices buffer
Definition: gpu_manager.h:146
static constexpr size_t SHADER_OFFSET
Definition: vertex_common.h:75
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:141
bool m_isDrawing
Drawing status flag.
Definition: gpu_manager.h:92
static constexpr size_t VERTEX_SIZE
Definition: vertex_common.h:63
static constexpr size_t COLOR_OFFSET
Definition: vertex_common.h:70
SHADER * m_shader
Shader handling
Definition: gpu_manager.h:98
static constexpr size_t SHADER_STRIDE
Definition: vertex_common.h:77
double msecs() const
Definition: profile.h:144
bool m_enableDepthTest
true: enable Z test when drawing
Definition: gpu_manager.h:104
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:140
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:66
static constexpr size_t COLOR_STRIDE
Definition: vertex_common.h:72

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

◆ MakeManager()

GPU_MANAGER * GPU_MANAGER::MakeManager ( VERTEX_CONTAINER aContainer)
staticinherited

Definition at line 42 of file gpu_manager.cpp.

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.

References KIGFX::VERTEX_CONTAINER::IsCached().

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

◆ Map()

void KIGFX::GPU_CACHED_MANAGER::Map ( )

Maps vertex buffer stored in GPU memory.

◆ resizeIndices()

void GPU_CACHED_MANAGER::resizeIndices ( unsigned int  aNewSize)
protected

Resizes the indices buffer to aNewSize if necessary

Definition at line 213 of file gpu_manager.cpp.

214 {
215  if( aNewSize > m_indicesCapacity )
216  {
217  m_indicesCapacity = aNewSize;
218  m_indices.reset( new GLuint[m_indicesCapacity] );
219  }
220 }
unsigned int m_indicesCapacity
Current indices buffer size
Definition: gpu_manager.h:152
boost::scoped_array< GLuint > m_indices
Pointer to the current indices buffer
Definition: gpu_manager.h:140

References m_indices, and m_indicesCapacity.

Referenced by BeginDrawing(), and GPU_CACHED_MANAGER().

◆ SetShader()

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.

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 GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:167
int m_shaderAttrib
Location of shader attributes (for glVertexAttribPointer)
Definition: gpu_manager.h:101
SHADER * m_shader
Shader handling
Definition: gpu_manager.h:98
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:243

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

◆ Unmap()

void KIGFX::GPU_CACHED_MANAGER::Unmap ( )

Unmaps vertex buffer.

Member Data Documentation

◆ m_buffersInitialized

bool KIGFX::GPU_CACHED_MANAGER::m_buffersInitialized
protected

Buffers initialization flag

Definition at line 137 of file gpu_manager.h.

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

◆ m_container

VERTEX_CONTAINER* KIGFX::GPU_MANAGER::m_container
protectedinherited

Container that stores vertices data.

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

true: enable Z test when drawing

Definition at line 104 of file gpu_manager.h.

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

◆ m_indices

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

Pointer to the current indices buffer

Definition at line 140 of file gpu_manager.h.

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

◆ m_indicesBuffer

GLuint KIGFX::GPU_CACHED_MANAGER::m_indicesBuffer
protected

Handle to indices buffer

Definition at line 146 of file gpu_manager.h.

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

◆ m_indicesCapacity

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesCapacity
protected

Current indices buffer size

Definition at line 152 of file gpu_manager.h.

Referenced by resizeIndices().

◆ m_indicesPtr

GLuint* KIGFX::GPU_CACHED_MANAGER::m_indicesPtr
protected

Pointer to the first free cell in the indices buffer

Definition at line 143 of file gpu_manager.h.

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

◆ m_indicesSize

unsigned int KIGFX::GPU_CACHED_MANAGER::m_indicesSize
protected

Number of indices stored in the indices buffer

Definition at line 149 of file gpu_manager.h.

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

◆ m_isDrawing

bool KIGFX::GPU_MANAGER::m_isDrawing
protectedinherited

Drawing status flag.

Definition at line 92 of file gpu_manager.h.

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

◆ m_shader

SHADER* KIGFX::GPU_MANAGER::m_shader
protectedinherited

Shader handling

Definition at line 98 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

Location of shader attributes (for glVertexAttribPointer)

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