KiCad PCB EDA Suite
KIGFX::SHADER Class Reference

Class SHADER provides the access to the OpenGL shaders. More...

#include <shader.h>

Public Member Functions

 SHADER ()
 Constructor. More...
 
virtual ~SHADER ()
 Destructor. More...
 
template<typename... Args>
bool LoadShaderFromStrings (SHADER_TYPE aShaderType, Args &&...aArgs)
 Add a shader and compile the shader sources. More...
 
bool LoadShaderFromFile (SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
 Loads one of the built-in shaders and compiles it. More...
 
bool Link ()
 Link the shaders. More...
 
bool IsLinked () const
 Returns true if shaders are linked correctly. More...
 
void Use ()
 Use the shader. More...
 
void Deactivate ()
 Deactivate the shader and use the default OpenGL program. More...
 
bool IsActive () const
 Returns the current state of the shader. More...
 
void ConfigureGeometryShader (GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
 Configure the geometry shader - has to be done before linking! More...
 
int AddParameter (const std::string &aParameterName)
 Add a parameter to the parameter queue. More...
 
void SetParameter (int aParameterNumber, float aValue) const
 Set a parameter of the shader. More...
 
void SetParameter (int aParameterNumber, int aValue) const
 
void SetParameter (int aParameterNumber, float f0, float f1, float f2, float f3) const
 
int GetAttribute (const std::string &aAttributeName) const
 Gets an attribute location. More...
 

Static Public Member Functions

static std::string ReadSource (const std::string &aShaderSourceName)
 Read the shader source file. More...
 

Private Member Functions

bool loadShaderFromStringArray (SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
 Compile vertex of fragment shader source code into the program. More...
 
void programInfo (GLuint aProgram)
 Get the shader program information. More...
 
void shaderInfo (GLuint aShader)
 Get the shader information. More...
 

Private Attributes

std::deque< GLuint > shaderNumbers
 Shader number list. More...
 
GLuint programNumber
 Shader program number. More...
 
bool isProgramCreated
 Flag for program creation. More...
 
bool isShaderLinked
 Is the shader linked? More...
 
bool active
 Is any of shaders used? More...
 
GLuint maximumVertices
 The maximum of vertices to be generated. More...
 
GLuint geomInputType
 Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]. More...
 
GLuint geomOutputType
 Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]. More...
 
std::deque< GLint > parameterLocation
 Location of the parameter. More...
 

Detailed Description

Class SHADER provides the access to the OpenGL shaders.

The purpose of this class is advanced drawing with OpenGL. One example is using the pixel shader for drawing exact circles or for anti-aliasing. This class supports vertex, geometry and fragment shaders.
Make sure that the hardware supports these features. This can be identified with the "GLEW" library.

Definition at line 74 of file shader.h.

Constructor & Destructor Documentation

SHADER::SHADER ( )

Constructor.

Definition at line 40 of file shader.cpp.

References programNumber.

40  :
41  isProgramCreated( false ),
42  isShaderLinked( false ),
43  active( false ),
44  maximumVertices( 4 ),
45  geomInputType( GL_LINES ),
46  geomOutputType( GL_LINES )
47 
48 {
49  // Do not have uninitialized members:
50  programNumber = 0;
51 }
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:231
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:232
bool isShaderLinked
Is the shader linked?
Definition: shader.h:228
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:230
bool isProgramCreated
Flag for program creation.
Definition: shader.h:227
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool active
Is any of shaders used?
Definition: shader.h:229
SHADER::~SHADER ( )
virtual

Destructor.

Definition at line 54 of file shader.cpp.

References active, Deactivate(), isProgramCreated, programNumber, and shaderNumbers.

55 {
56  if( active )
57  Deactivate();
58 
59  if( isProgramCreated )
60  {
61  // Delete the shaders and the program
62  for( std::deque<GLuint>::iterator it = shaderNumbers.begin();
63  it != shaderNumbers.end(); ++it )
64  {
65  GLuint shader = *it;
66 
67  if( glIsShader( shader ) )
68  {
69  glDetachShader( programNumber, shader );
70  glDeleteShader( shader );
71  }
72  }
73 
74  glDeleteProgram( programNumber );
75  }
76 }
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:225
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:139
bool isProgramCreated
Flag for program creation.
Definition: shader.h:227
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool active
Is any of shaders used?
Definition: shader.h:229

Member Function Documentation

int SHADER::AddParameter ( const std::string &  aParameterName)

Add a parameter to the parameter queue.

To communicate with the shader use this function to set up the names for the uniform variables. These are queued in a list and can be assigned with the SetParameter(..) method using the queue position.

Parameters
aParameterNameis the name of the parameter.
Returns
the added parameter location.

Definition at line 124 of file shader.cpp.

References parameterLocation, and programNumber.

Referenced by KIGFX::OPENGL_GAL::BeginDrawing().

125 {
126  GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() );
127 
128  if( location >= 0 )
129  parameterLocation.push_back( location );
130  else
131  throw std::runtime_error( "Could not find shader uniform: " + aParameterName );
132 
133  return parameterLocation.size() - 1;
134 }
GLuint programNumber
Shader program number.
Definition: shader.h:226
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:233
void SHADER::ConfigureGeometryShader ( GLuint  maxVertices,
GLuint  geometryInputType,
GLuint  geometryOutputType 
)

Configure the geometry shader - has to be done before linking!

Parameters
maxVerticesis the maximum of vertices to be generated.
geometryInputTypeis the input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]
geometryOutputTypeis the output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]

Definition at line 87 of file shader.cpp.

References geomInputType, geomOutputType, and maximumVertices.

89 {
90  maximumVertices = maxVertices;
91  geomInputType = geometryInputType;
92  geomOutputType = geometryOutputType;
93 }
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:231
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:232
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:230
void KIGFX::SHADER::Deactivate ( )
inline

Deactivate the shader and use the default OpenGL program.

Definition at line 139 of file shader.h.

References active.

Referenced by KIGFX::OPENGL_GAL::BeginDrawing(), KIGFX::GPU_CACHED_MANAGER::EndDrawing(), KIGFX::GPU_NONCACHED_MANAGER::EndDrawing(), and ~SHADER().

140  {
141  glUseProgram( 0 );
142  active = false;
143  }
bool active
Is any of shaders used?
Definition: shader.h:229
int SHADER::GetAttribute ( const std::string &  aAttributeName) const

Gets an attribute location.

Parameters
aAttributeNameis the name of the attribute.
Returns
the location.

Definition at line 160 of file shader.cpp.

References programNumber.

Referenced by KIGFX::GPU_MANAGER::SetShader().

161 {
162  return glGetAttribLocation( programNumber, aAttributeName.c_str() );
163 }
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool KIGFX::SHADER::IsActive ( ) const
inline

Returns the current state of the shader.

Returns
True if any of shaders is enabled.

Definition at line 150 of file shader.h.

References active.

151  {
152  return active;
153  }
bool active
Is any of shaders used?
Definition: shader.h:229
bool KIGFX::SHADER::IsLinked ( ) const
inline

Returns true if shaders are linked correctly.

Definition at line 122 of file shader.h.

References isShaderLinked.

Referenced by KIGFX::OPENGL_GAL::init().

123  {
124  return isShaderLinked;
125  }
bool isShaderLinked
Is the shader linked?
Definition: shader.h:228
bool SHADER::Link ( )

Link the shaders.

Returns
true in case of success, false otherwise.

Definition at line 96 of file shader.cpp.

References isShaderLinked, programInfo(), and programNumber.

Referenced by KIGFX::OPENGL_GAL::init().

97 {
98  // Shader linking
99  glLinkProgram( programNumber );
101 
102  // Check the Link state
103  glGetObjectParameterivARB( programNumber, GL_OBJECT_LINK_STATUS_ARB,
104  (GLint*) &isShaderLinked );
105 
106 #ifdef DEBUG
107  if( !isShaderLinked )
108  {
109  int maxLength;
110  glGetProgramiv( programNumber, GL_INFO_LOG_LENGTH, &maxLength );
111  maxLength = maxLength + 1;
112  char* linkInfoLog = new char[maxLength];
113  glGetProgramInfoLog( programNumber, maxLength, &maxLength, linkInfoLog );
114  std::cerr << "Shader linking error:" << std::endl;
115  std::cerr << linkInfoLog;
116  delete[] linkInfoLog;
117  }
118 #endif /* DEBUG */
119 
120  return isShaderLinked;
121 }
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:166
bool isShaderLinked
Is the shader linked?
Definition: shader.h:228
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool SHADER::LoadShaderFromFile ( SHADER_TYPE  aShaderType,
const std::string &  aShaderSourceName 
)

Loads one of the built-in shaders and compiles it.

Parameters
aShaderSourceNameis the shader source file name.
aShaderTypeis the type of the shader.
Returns
True in case of success, false otherwise.

Definition at line 78 of file shader.cpp.

References LoadShaderFromStrings(), and ReadSource().

79 {
80  // Load shader sources
81  const std::string shaderSource = ReadSource( aShaderSourceName );
82 
83  return LoadShaderFromStrings( aShaderType, shaderSource );
84 }
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&...aArgs)
Add a shader and compile the shader sources.
Definition: shader.h:97
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:208
bool SHADER::loadShaderFromStringArray ( SHADER_TYPE  aShaderType,
const char **  aArray,
size_t  aSize 
)
private

Compile vertex of fragment shader source code into the program.

Definition at line 230 of file shader.cpp.

References geomInputType, geomOutputType, isProgramCreated, isShaderLinked, maximumVertices, programInfo(), programNumber, KIGFX::SHADER_TYPE_GEOMETRY, shaderInfo(), and shaderNumbers.

Referenced by LoadShaderFromStrings().

232 {
233  assert( !isShaderLinked );
234 
235  // Create the program
236  if( !isProgramCreated )
237  {
238  programNumber = glCreateProgram();
239  isProgramCreated = true;
240  }
241 
242  // Create a shader
243  GLuint shaderNumber = glCreateShader( aShaderType );
244  shaderNumbers.push_back( shaderNumber );
245 
246  // Get the program info
248 
249  // Attach the sources
250  glShaderSource( shaderNumber, aSize, (const GLchar**) aArray, NULL );
252 
253  // Compile and attach shader to the program
254  glCompileShader( shaderNumber );
255  GLint status;
256  glGetShaderiv( shaderNumber, GL_COMPILE_STATUS, &status );
257 
258  if( status != GL_TRUE )
259  {
260  shaderInfo( shaderNumber );
261  throw std::runtime_error( "Shader compilation error" );
262  }
263 
264  glAttachShader( programNumber, shaderNumber );
266 
267  // Special handling for the geometry shader
268  if( aShaderType == SHADER_TYPE_GEOMETRY )
269  {
270  glProgramParameteriEXT( programNumber, GL_GEOMETRY_VERTICES_OUT_EXT, maximumVertices );
271  glProgramParameteriEXT( programNumber, GL_GEOMETRY_INPUT_TYPE_EXT, geomInputType );
272  glProgramParameteriEXT( programNumber, GL_GEOMETRY_OUTPUT_TYPE_EXT, geomOutputType );
273  }
274 
275  return true;
276 }
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:231
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:232
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:166
bool isShaderLinked
Is the shader linked?
Definition: shader.h:228
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:230
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:225
void shaderInfo(GLuint aShader)
Get the shader information.
Definition: shader.cpp:187
Geometry shader.
Definition: shader.h:46
bool isProgramCreated
Flag for program creation.
Definition: shader.h:227
GLuint programNumber
Shader program number.
Definition: shader.h:226
template<typename... Args>
bool KIGFX::SHADER::LoadShaderFromStrings ( SHADER_TYPE  aShaderType,
Args &&...  aArgs 
)
inline

Add a shader and compile the shader sources.

Parameters
aArgsis the list of strings (std::string or convertible to const char*) which are concatenated and compiled as a single shader source code.
aShaderTypeis the type of the shader.
Returns
True in case of success, false otherwise.

Definition at line 97 of file shader.h.

References loadShaderFromStringArray(), and KIGFX::DETAIL::translateStringArg().

Referenced by KIGFX::OPENGL_GAL::init(), and LoadShaderFromFile().

98  {
99  const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
100  return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
101  }
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition: shader.cpp:230
const char * translateStringArg(const std::string &str)
Definition: shader.h:51
void SHADER::programInfo ( GLuint  aProgram)
private

Get the shader program information.

Parameters
aProgramis the program number.

Definition at line 166 of file shader.cpp.

Referenced by Link(), and loadShaderFromStringArray().

167 {
168  GLint glInfoLogLength = 0;
169  GLint writtenChars = 0;
170 
171  // Get the length of the info string
172  glGetProgramiv( aProgram, GL_INFO_LOG_LENGTH, &glInfoLogLength );
173 
174  // Print the information
175  if( glInfoLogLength > 2 )
176  {
177  GLchar* glInfoLog = new GLchar[glInfoLogLength];
178  glGetProgramInfoLog( aProgram, glInfoLogLength, &writtenChars, glInfoLog );
179 
180  std::cerr << glInfoLog << std::endl;
181 
182  delete[] glInfoLog;
183  }
184 }
int PGM_BASE * aProgram
Definition: pcbnew.cpp:195
std::string SHADER::ReadSource ( const std::string &  aShaderSourceName)
static

Read the shader source file.

Parameters
aShaderSourceNameis the shader source file name.
Returns
the source as string

Definition at line 208 of file shader.cpp.

Referenced by LoadShaderFromFile().

209 {
210  // Open the shader source for reading
211  std::ifstream inputFile( aShaderSourceName.c_str(), std::ifstream::in );
212  std::string shaderSource;
213 
214  if( !inputFile )
215  throw std::runtime_error( "Can't read the shader source: " + aShaderSourceName );
216 
217  std::string shaderSourceLine;
218 
219  // Read all lines from the text file
220  while( getline( inputFile, shaderSourceLine ) )
221  {
222  shaderSource += shaderSourceLine;
223  shaderSource += "\n";
224  }
225 
226  return shaderSource;
227 }
void SHADER::SetParameter ( int  aParameterNumber,
float  aValue 
) const

Set a parameter of the shader.

Parameters
aParameterNumberis the number of the parameter.
aValueis the value of the parameter.

Definition at line 137 of file shader.cpp.

References parameterLocation.

Referenced by KIGFX::OPENGL_GAL::BeginDrawing().

138 {
139  assert( (unsigned) parameterNumber < parameterLocation.size() );
140 
141  glUniform1f( parameterLocation[parameterNumber], value );
142 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:233
void SHADER::SetParameter ( int  aParameterNumber,
int  aValue 
) const

Definition at line 145 of file shader.cpp.

References parameterLocation.

146 {
147  assert( (unsigned) parameterNumber < parameterLocation.size() );
148 
149  glUniform1i( parameterLocation[parameterNumber], value );
150 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:233
void SHADER::SetParameter ( int  aParameterNumber,
float  f0,
float  f1,
float  f2,
float  f3 
) const

Definition at line 152 of file shader.cpp.

References parameterLocation.

153 {
154  assert( (unsigned)parameterNumber < parameterLocation.size() );
155  float arr[4] = { f0, f1, f2, f3 };
156  glUniform4fv( parameterLocation[parameterNumber], 1, arr );
157 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:233
void SHADER::shaderInfo ( GLuint  aShader)
private

Get the shader information.

Parameters
aShaderis the shader number.

Definition at line 187 of file shader.cpp.

Referenced by loadShaderFromStringArray().

188 {
189  GLint glInfoLogLength = 0;
190  GLint writtenChars = 0;
191 
192  // Get the length of the info string
193  glGetShaderiv( aShader, GL_INFO_LOG_LENGTH, &glInfoLogLength );
194 
195  // Print the information
196  if( glInfoLogLength > 2 )
197  {
198  GLchar* glInfoLog = new GLchar[glInfoLogLength];
199  glGetShaderInfoLog( aShader, glInfoLogLength, &writtenChars, glInfoLog );
200 
201  std::cerr << glInfoLog << std::endl;
202 
203  delete[] glInfoLog;
204  }
205 }
void KIGFX::SHADER::Use ( )
inline

Use the shader.

Definition at line 130 of file shader.h.

References active, and programNumber.

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

131  {
132  glUseProgram( programNumber );
133  active = true;
134  }
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool active
Is any of shaders used?
Definition: shader.h:229

Member Data Documentation

bool KIGFX::SHADER::active
private

Is any of shaders used?

Definition at line 229 of file shader.h.

Referenced by Deactivate(), IsActive(), Use(), and ~SHADER().

GLuint KIGFX::SHADER::geomInputType
private

Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].

Definition at line 231 of file shader.h.

Referenced by ConfigureGeometryShader(), and loadShaderFromStringArray().

GLuint KIGFX::SHADER::geomOutputType
private

Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].

Definition at line 232 of file shader.h.

Referenced by ConfigureGeometryShader(), and loadShaderFromStringArray().

bool KIGFX::SHADER::isProgramCreated
private

Flag for program creation.

Definition at line 227 of file shader.h.

Referenced by loadShaderFromStringArray(), and ~SHADER().

bool KIGFX::SHADER::isShaderLinked
private

Is the shader linked?

Definition at line 228 of file shader.h.

Referenced by IsLinked(), Link(), and loadShaderFromStringArray().

GLuint KIGFX::SHADER::maximumVertices
private

The maximum of vertices to be generated.

Definition at line 230 of file shader.h.

Referenced by ConfigureGeometryShader(), and loadShaderFromStringArray().

std::deque<GLint> KIGFX::SHADER::parameterLocation
private

Location of the parameter.

Definition at line 233 of file shader.h.

Referenced by AddParameter(), and SetParameter().

GLuint KIGFX::SHADER::programNumber
private

Shader program number.

Definition at line 226 of file shader.h.

Referenced by AddParameter(), GetAttribute(), Link(), loadShaderFromStringArray(), SHADER(), Use(), and ~SHADER().

std::deque<GLuint> KIGFX::SHADER::shaderNumbers
private

Shader number list.

Definition at line 225 of file shader.h.

Referenced by loadShaderFromStringArray(), and ~SHADER().


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