KiCad PCB EDA Suite
KIGFX::SHADER Class Reference

Provide the access to the OpenGL shaders. More...

#include <shader.h>

Public Member Functions

 SHADER ()
 
virtual ~SHADER ()
 
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)
 Load one of the built-in shaders and compiles it. More...
 
bool Link ()
 Link the shaders. More...
 
bool IsLinked () const
 Return 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
 Return 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, const VECTOR2D &aValue) const
 
void SetParameter (int aParameterNumber, float f0, float f1, float f2, float f3) const
 
int GetAttribute (const std::string &aAttributeName) const
 Get 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
 Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]. More...
 
GLuint geomOutputType
 
std::deque< GLint > parameterLocation
 Location of the parameter. More...
 

Detailed Description

Provide 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 76 of file shader.h.

Constructor & Destructor Documentation

◆ SHADER()

SHADER::SHADER ( )

Definition at line 41 of file shader.cpp.

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

References programNumber.

◆ ~SHADER()

SHADER::~SHADER ( )
virtual

Definition at line 55 of file shader.cpp.

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

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

Member Function Documentation

◆ AddParameter()

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 130 of file shader.cpp.

131 {
132  GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() );
133 
134  if( location >= 0 )
135  parameterLocation.push_back( location );
136  else
137  throw std::runtime_error( "Could not find shader uniform: " + aParameterName );
138 
139  return parameterLocation.size() - 1;
140 }
GLuint programNumber
Shader program number.
Definition: shader.h:221
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:232

References parameterLocation, and programNumber.

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

◆ ConfigureGeometryShader()

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 92 of file shader.cpp.

94 {
95  maximumVertices = maxVertices;
96  geomInputType = geometryInputType;
97  geomOutputType = geometryOutputType;
98 }
GLuint geomInputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:228
GLuint geomOutputType
Definition: shader.h:231
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:225

References geomInputType, geomOutputType, and maximumVertices.

◆ Deactivate()

void KIGFX::SHADER::Deactivate ( )
inline

Deactivate the shader and use the default OpenGL program.

Definition at line 135 of file shader.h.

136  {
137  glUseProgram( 0 );
138  active = false;
139  }
bool active
Is any of shaders used?
Definition: shader.h:224

References active.

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

◆ GetAttribute()

int SHADER::GetAttribute ( const std::string &  aAttributeName) const

Get an attribute location.

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

Definition at line 172 of file shader.cpp.

173 {
174  return glGetAttribLocation( programNumber, aAttributeName.c_str() );
175 }
GLuint programNumber
Shader program number.
Definition: shader.h:221

References programNumber.

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

◆ IsActive()

bool KIGFX::SHADER::IsActive ( ) const
inline

Return the current state of the shader.

Returns
True if any of shaders is enabled.

Definition at line 146 of file shader.h.

147  {
148  return active;
149  }
bool active
Is any of shaders used?
Definition: shader.h:224

References active.

◆ IsLinked()

bool KIGFX::SHADER::IsLinked ( ) const
inline

Return true if shaders are linked correctly.

Definition at line 118 of file shader.h.

119  {
120  return isShaderLinked;
121  }
bool isShaderLinked
Is the shader linked?
Definition: shader.h:223

References isShaderLinked.

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

◆ Link()

bool SHADER::Link ( )

Link the shaders.

Returns
true in case of success, false otherwise.

Definition at line 101 of file shader.cpp.

102 {
103  // Shader linking
104  glLinkProgram( programNumber );
106 
107  // Check the Link state
108  GLint tmp;
109  glGetObjectParameterivARB( programNumber, GL_OBJECT_LINK_STATUS_ARB, &tmp );
110  isShaderLinked = !!tmp;
111 
112 #ifdef DEBUG
113  if( !isShaderLinked )
114  {
115  int maxLength;
116  glGetProgramiv( programNumber, GL_INFO_LOG_LENGTH, &maxLength );
117  maxLength = maxLength + 1;
118  char* linkInfoLog = new char[maxLength];
119  glGetProgramInfoLog( programNumber, maxLength, &maxLength, linkInfoLog );
120  std::cerr << "Shader linking error:" << std::endl;
121  std::cerr << linkInfoLog;
122  delete[] linkInfoLog;
123  }
124 #endif /* DEBUG */
125 
126  return isShaderLinked;
127 }
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:178
bool isShaderLinked
Is the shader linked?
Definition: shader.h:223
GLuint programNumber
Shader program number.
Definition: shader.h:221

References isShaderLinked, programInfo(), and programNumber.

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

◆ LoadShaderFromFile()

bool SHADER::LoadShaderFromFile ( SHADER_TYPE  aShaderType,
const std::string &  aShaderSourceName 
)

Load 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 83 of file shader.cpp.

84 {
85  // Load shader sources
86  const std::string shaderSource = ReadSource( aShaderSourceName );
87 
88  return LoadShaderFromStrings( aShaderType, shaderSource );
89 }
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&... aArgs)
Add a shader and compile the shader sources.
Definition: shader.h:93
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:216

References LoadShaderFromStrings(), and ReadSource().

◆ loadShaderFromStringArray()

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 238 of file shader.cpp.

239 {
240  assert( !isShaderLinked );
241 
242  // Create the program
243  if( !isProgramCreated )
244  {
245  programNumber = glCreateProgram();
246  isProgramCreated = true;
247  }
248 
249  // Create a shader
250  GLuint shaderNumber = glCreateShader( aShaderType );
251  shaderNumbers.push_back( shaderNumber );
252 
253  // Get the program info
255 
256  // Attach the sources
257  glShaderSource( shaderNumber, aSize, (const GLchar**) aArray, nullptr );
259 
260  // Compile and attach shader to the program
261  glCompileShader( shaderNumber );
262  GLint status;
263  glGetShaderiv( shaderNumber, GL_COMPILE_STATUS, &status );
264 
265  if( status != GL_TRUE )
266  {
267  shaderInfo( shaderNumber );
268 
269  GLint maxLength = 0;
270  glGetShaderiv( shaderNumber, GL_INFO_LOG_LENGTH, &maxLength );
271 
272  // The maxLength includes the NULL character
273  std::vector<GLchar> errorLog( (size_t) maxLength );
274  glGetShaderInfoLog( shaderNumber, maxLength, &maxLength, &errorLog[0] );
275 
276  // Provide the infolog in whatever manor you deem best.
277  // Exit with failure.
278  glDeleteShader( shaderNumber ); // Don't leak the shader.
279 
280  throw std::runtime_error( &errorLog[0] );
281  }
282 
283  glAttachShader( programNumber, shaderNumber );
285 
286  // Special handling for the geometry shader
287  if( aShaderType == SHADER_TYPE_GEOMETRY )
288  {
289  glProgramParameteriEXT( programNumber, GL_GEOMETRY_VERTICES_OUT_EXT, maximumVertices );
290  glProgramParameteriEXT( programNumber, GL_GEOMETRY_INPUT_TYPE_EXT, geomInputType );
291  glProgramParameteriEXT( programNumber, GL_GEOMETRY_OUTPUT_TYPE_EXT, geomOutputType );
292  }
293 
294  return true;
295 }
GLuint geomInputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:228
GLuint geomOutputType
Definition: shader.h:231
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:178
bool isShaderLinked
Is the shader linked?
Definition: shader.h:223
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:225
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:220
void shaderInfo(GLuint aShader)
Get the shader information.
Definition: shader.cpp:197
Geometry shader.
Definition: shader.h:48
bool isProgramCreated
Flag for program creation.
Definition: shader.h:222
GLuint programNumber
Shader program number.
Definition: shader.h:221

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

Referenced by LoadShaderFromStrings().

◆ LoadShaderFromStrings()

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 93 of file shader.h.

94  {
95  const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
96  return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
97  }
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition: shader.cpp:238
const char * translateStringArg(const std::string &str)
Definition: shader.h:53

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

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

◆ programInfo()

void SHADER::programInfo ( GLuint  aProgram)
private

Get the shader program information.

Parameters
aProgramis the program number.

Definition at line 178 of file shader.cpp.

179 {
180  GLint glInfoLogLength = 0;
181  GLint writtenChars = 0;
182 
183  // Get the length of the info string
184  glGetProgramiv( aProgram, GL_INFO_LOG_LENGTH, &glInfoLogLength );
185 
186  // Print the information
187  if( glInfoLogLength > 2 )
188  {
189  GLchar* glInfoLog = new GLchar[glInfoLogLength];
190  glGetProgramInfoLog( aProgram, glInfoLogLength, &writtenChars, glInfoLog );
191 
192  delete[] glInfoLog;
193  }
194 }
int PGM_BASE * aProgram
Definition: cvpcb.cpp:110

References aProgram.

Referenced by Link(), and loadShaderFromStringArray().

◆ ReadSource()

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 216 of file shader.cpp.

217 {
218  // Open the shader source for reading
219  std::ifstream inputFile( aShaderSourceName.c_str(), std::ifstream::in );
220  std::string shaderSource;
221 
222  if( !inputFile )
223  throw std::runtime_error( "Can't read the shader source: " + aShaderSourceName );
224 
225  std::string shaderSourceLine;
226 
227  // Read all lines from the text file
228  while( getline( inputFile, shaderSourceLine ) )
229  {
230  shaderSource += shaderSourceLine;
231  shaderSource += "\n";
232  }
233 
234  return shaderSource;
235 }

Referenced by LoadShaderFromFile().

◆ SetParameter() [1/4]

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 143 of file shader.cpp.

144 {
145  assert( (unsigned) parameterNumber < parameterLocation.size() );
146 
147  glUniform1f( parameterLocation[parameterNumber], value );
148 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:232

References parameterLocation.

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

◆ SetParameter() [2/4]

void SHADER::SetParameter ( int  aParameterNumber,
int  aValue 
) const

Definition at line 151 of file shader.cpp.

152 {
153  assert( (unsigned) parameterNumber < parameterLocation.size() );
154 
155  glUniform1i( parameterLocation[parameterNumber], value );
156 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:232

References parameterLocation.

◆ SetParameter() [3/4]

void SHADER::SetParameter ( int  aParameterNumber,
const VECTOR2D aValue 
) const

Definition at line 165 of file shader.cpp.

166 {
167  assert( (unsigned) aParameterNumber < parameterLocation.size() );
168  glUniform2f( parameterLocation[aParameterNumber], aValue.x, aValue.y );
169 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:232

References parameterLocation, VECTOR2< T >::x, and VECTOR2< T >::y.

◆ SetParameter() [4/4]

void SHADER::SetParameter ( int  aParameterNumber,
float  f0,
float  f1,
float  f2,
float  f3 
) const

Definition at line 158 of file shader.cpp.

159 {
160  assert( (unsigned) parameterNumber < parameterLocation.size() );
161  float arr[4] = { f0, f1, f2, f3 };
162  glUniform4fv( parameterLocation[parameterNumber], 1, arr );
163 }
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:232

References parameterLocation.

◆ shaderInfo()

void SHADER::shaderInfo ( GLuint  aShader)
private

Get the shader information.

Parameters
aShaderis the shader number.

Definition at line 197 of file shader.cpp.

198 {
199  GLint glInfoLogLength = 0;
200  GLint writtenChars = 0;
201 
202  // Get the length of the info string
203  glGetShaderiv( aShader, GL_INFO_LOG_LENGTH, &glInfoLogLength );
204 
205  // Print the information
206  if( glInfoLogLength > 2 )
207  {
208  GLchar* glInfoLog = new GLchar[glInfoLogLength];
209  glGetShaderInfoLog( aShader, glInfoLogLength, &writtenChars, glInfoLog );
210 
211  delete[] glInfoLog;
212  }
213 }

Referenced by loadShaderFromStringArray().

◆ Use()

void KIGFX::SHADER::Use ( )
inline

Use the shader.

Definition at line 126 of file shader.h.

127  {
128  glUseProgram( programNumber );
129  active = true;
130  }
GLuint programNumber
Shader program number.
Definition: shader.h:221
bool active
Is any of shaders used?
Definition: shader.h:224

References active, and programNumber.

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

Member Data Documentation

◆ active

bool KIGFX::SHADER::active
private

Is any of shaders used?

Definition at line 224 of file shader.h.

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

◆ geomInputType

GLuint KIGFX::SHADER::geomInputType
private

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

Definition at line 228 of file shader.h.

Referenced by ConfigureGeometryShader(), and loadShaderFromStringArray().

◆ geomOutputType

GLuint KIGFX::SHADER::geomOutputType
private

Definition at line 231 of file shader.h.

Referenced by ConfigureGeometryShader(), and loadShaderFromStringArray().

◆ isProgramCreated

bool KIGFX::SHADER::isProgramCreated
private

Flag for program creation.

Definition at line 222 of file shader.h.

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

◆ isShaderLinked

bool KIGFX::SHADER::isShaderLinked
private

Is the shader linked?

Definition at line 223 of file shader.h.

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

◆ maximumVertices

GLuint KIGFX::SHADER::maximumVertices
private

The maximum of vertices to be generated.

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

Definition at line 225 of file shader.h.

Referenced by ConfigureGeometryShader(), and loadShaderFromStringArray().

◆ parameterLocation

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

Location of the parameter.

Definition at line 232 of file shader.h.

Referenced by AddParameter(), and SetParameter().

◆ programNumber

GLuint KIGFX::SHADER::programNumber
private

Shader program number.

Definition at line 221 of file shader.h.

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

◆ shaderNumbers

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

Shader number list.

Definition at line 220 of file shader.h.

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


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