smk::ShaderProgram Class Reference

#include <Shader.hpp>

A shader program is a set of shader (for instance vertex shader + pixel shader) defining the rendering pipeline. More...

Description

A shader program is a set of shader (for instance vertex shader + pixel shader) defining the rendering pipeline.

This is a move-only ressource.

This class provide an interface to define the OpenGL uniforms and attributes using GLM objects.

See also
Shader

Definition at line 114 of file Shader.hpp.

Public Member Functions

 ShaderProgram ()
 The constructor. The ShaderProgram is initially invalid. You need to call AddShader and Link before being able to use it. More...
 
void AddShader (const Shader &shader)
 Add a Shader to the program list. This must called multiple time for each shader components before calling Link. More...
 
void Link () const
 Add a Shader to the program list. More...
 
bool IsReady () const
 
bool LinkStatus () const
 
void Use () const
 Bind the ShaderProgram. Future draw will use it. This unbind any previously bound ShaderProgram. More...
 
void Unuse () const
 Unbind the ShaderProgram. More...
 
GLuint id () const
 The GPU id to the ShaderProgram. More...
 
GLint Attribute (const std::string &name) const
 Return the GPU attribute id. More...
 
void SetAttribute (const std::string &name, GLint size, GLsizei stride, GLuint offset, GLboolean normalize, GLenum type) const
 Set an OpenGL attribute properties. More...
 
void SetAttribute (const std::string &name, GLint size, GLsizei stride, GLuint offset, GLboolean normalize) const
 Set an OpenGL attribute properties, assuming data are float. More...
 
void SetAttribute (const std::string &name, GLint size, GLsizei stride, GLuint offset, GLenum type) const
 Set an OpenGL attribute properties, assuming data are float. More...
 
void SetAttribute (const std::string &name, GLint size, GLsizei stride, GLuint offset) const
 Set an OpenGL attribute properties, assuming data are float. More...
 
GLint Uniform (const std::string &name)
 Return the uniform ID. More...
 
GLint operator[] (const std::string &name)
 
void SetUniform (const std::string &name, float x, float y, float z)
 Assign shader vec3 uniform. More...
 
void SetUniform (const std::string &name, const glm::vec3 &v)
 
void SetUniform (const std::string &name, const glm::vec4 &v)
 
void SetUniform (const std::string &name, const glm::mat4 &m)
 
void SetUniform (const std::string &name, const glm::mat3 &m)
 
void SetUniform (const std::string &name, float val)
 Assign shader float uniform. More...
 
void SetUniform (const std::string &name, int val)
 Assign shader int uniform. More...
 
 ShaderProgram (ShaderProgram &&) noexcept
 
 ShaderProgram (const ShaderProgram &)
 
ShaderProgramoperator= (ShaderProgram &&) noexcept
 
ShaderProgramoperator= (const ShaderProgram &)
 
bool operator== (const ShaderProgram &rhs) const
 
bool operator!= (const ShaderProgram &rhs) const
 

Constructor & Destructor Documentation

◆ ShaderProgram()

smk::ShaderProgram::ShaderProgram ( )

The constructor. The ShaderProgram is initially invalid. You need to call AddShader and Link before being able to use it.

Definition at line 219 of file Shader.cpp.

219: impl_(std::make_shared<Impl>()) {}

Member Function Documentation

◆ AddShader()

void smk::ShaderProgram::AddShader ( const Shader shader)

Add a Shader to the program list. This must called multiple time for each shader components before calling Link.

Parameters
shaderThe Shader to be added to the program list.

Definition at line 224 of file Shader.cpp.

224 {
225 if (!impl_->id) {
226 impl_->id = glCreateProgram();
227 if (!impl_->id) {
228 std::cerr << "[Error] Impossible to create a new Shader" << std::endl;
229 }
230 }
231
232 glAttachShader(id(), shader.id());
233}

◆ Attribute()

GLint smk::ShaderProgram::Attribute ( const std::string &  name) const

Return the GPU attribute id.

Parameters
nameThe attribute name in the Shader.
Returns
The GPU attribute ID. Return 0 and display an error if not found.

Definition at line 304 of file Shader.cpp.

304 {
305 GLint attrib = glGetAttribLocation(id(), name.c_str());
306 if (attrib == GL_INVALID_OPERATION || attrib < 0) {
307 std::cerr << "[Error] Attribute " << name << " doesn't exist in program"
308 << std::endl;
309 }
310
311 return attrib;
312}

◆ id()

GLuint smk::ShaderProgram::id ( ) const

The GPU id to the ShaderProgram.

Returns
The GPU id to the ShaderProgram.

Definition at line 441 of file Shader.cpp.

441 {
442 return impl_->id;
443}

◆ IsReady()

bool smk::ShaderProgram::IsReady ( ) const

Definition at line 244 of file Shader.cpp.

244 {
245 if (g_khr_parallel_shader) {
246 GLint completion_status = {};
247 std::cerr << GL_COMPLETION_STATUS_KHR << std::endl;
248 std::cerr << id() << std::endl;
249 glGetProgramiv(id(), GL_COMPLETION_STATUS_KHR, &completion_status);
250 return completion_status == GL_TRUE;
251 }
252
253 return true;
254}
GLuint id() const
The GPU id to the ShaderProgram.
Definition: Shader.cpp:441

◆ Link()

void smk::ShaderProgram::Link ( ) const

Add a Shader to the program list.

Definition at line 236 of file Shader.cpp.

236 {
237 glLinkProgram(id());
238}

◆ LinkStatus()

bool smk::ShaderProgram::LinkStatus ( ) const

Definition at line 256 of file Shader.cpp.

256 {
257 GLint result = {};
258 glGetProgramiv(id(), GL_LINK_STATUS, &result);
259 if (result == GL_TRUE) {
260 return true;
261 }
262
263 std::cout << "[Error] linkage error" << std::endl;
264
265 GLsizei logsize = 0;
266 glGetProgramiv(id(), GL_INFO_LOG_LENGTH, &logsize);
267
268 std::vector<char> log(logsize);
269 glGetProgramInfoLog(id(), logsize, &logsize, log.data());
270
271 std::cout << log.data() << std::endl;
272 return false;
273}

◆ operator!=()

bool smk::ShaderProgram::operator!= ( const ShaderProgram rhs) const

Definition at line 449 of file Shader.cpp.

449 {
450 return impl_ != rhs.impl_;
451}

◆ operator==()

bool smk::ShaderProgram::operator== ( const ShaderProgram rhs) const

Definition at line 445 of file Shader.cpp.

445 {
446 return impl_ == rhs.impl_;
447}

◆ operator[]()

GLint smk::ShaderProgram::operator[] ( const std::string &  name)

Definition at line 297 of file Shader.cpp.

297 {
298 return Uniform(name);
299}
GLint Uniform(const std::string &name)
Return the uniform ID.
Definition: Shader.cpp:278

◆ SetAttribute() [1/4]

void smk::ShaderProgram::SetAttribute ( const std::string &  name,
GLint  size,
GLsizei  stride,
GLuint  offset 
) const

Set an OpenGL attribute properties, assuming data are float.

See also
SetAttribute.

Definition at line 365 of file Shader.cpp.

368 {
369 SetAttribute(name, size, stride, offset, false, GL_FLOAT);
370}
void SetAttribute(const std::string &name, GLint size, GLsizei stride, GLuint offset, GLboolean normalize, GLenum type) const
Set an OpenGL attribute properties.
Definition: Shader.cpp:331

◆ SetAttribute() [2/4]

void smk::ShaderProgram::SetAttribute ( const std::string &  name,
GLint  size,
GLsizei  stride,
GLuint  offset,
GLboolean  normalize 
) const

Set an OpenGL attribute properties, assuming data are float.

See also
SetAttribute.

Definition at line 345 of file Shader.cpp.

349 {
350 SetAttribute(name, size, stride, offset, normalize, GL_FLOAT);
351}

◆ SetAttribute() [3/4]

void smk::ShaderProgram::SetAttribute ( const std::string &  name,
GLint  size,
GLsizei  stride,
GLuint  offset,
GLboolean  normalize,
GLenum  type 
) const

Set an OpenGL attribute properties.

Parameters
nameAttribute name in the Shader.
sizeSpecify the number of component per object. One of {1,2,3,4}.
strideSpecify the byte offset in between consecutive attribute of the same kind.
offsetOffset of the attribute in the struct.
typeThe type of data. For instance GL_FLOAT.
Returns
The GPU attribute ID. Return 0 and display an error if not found.
See also
glVertexAttribPointer

Definition at line 331 of file Shader.cpp.

336 {
337 GLint loc = Attribute(name);
338 glEnableVertexAttribArray(loc);
339 glVertexAttribPointer(loc, size, type, normalize, stride,
340 reinterpret_cast<void*>(offset)); // NOLINT
341}
GLint Attribute(const std::string &name) const
Return the GPU attribute id.
Definition: Shader.cpp:304

◆ SetAttribute() [4/4]

void smk::ShaderProgram::SetAttribute ( const std::string &  name,
GLint  size,
GLsizei  stride,
GLuint  offset,
GLenum  type 
) const

Set an OpenGL attribute properties, assuming data are float.

See also
SetAttribute.

Definition at line 355 of file Shader.cpp.

359 {
360 SetAttribute(name, size, stride, offset, false, type);
361}

◆ SetUniform() [1/3]

void smk::ShaderProgram::SetUniform ( const std::string &  name,
float  val 
)

Assign shader float uniform.

Parameters
valfloat value This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 415 of file Shader.cpp.

415 {
416 glUniform1f(Uniform(name), val);
417}

◆ SetUniform() [2/3]

void smk::ShaderProgram::SetUniform ( const std::string &  name,
float  x,
float  y,
float  z 
)

Assign shader vec3 uniform.

Parameters
xFirst vec3 component.
ySecond vec3 component.
zThird vec3 component This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 377 of file Shader.cpp.

380 {
381 glUniform3f(Uniform(name), x, y, z);
382}

◆ SetUniform() [3/3]

void smk::ShaderProgram::SetUniform ( const std::string &  name,
int  val 
)

Assign shader int uniform.

Parameters
valint value This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 422 of file Shader.cpp.

422 {
423 glUniform1i(Uniform(name), val);
424}

◆ Uniform()

GLint smk::ShaderProgram::Uniform ( const std::string &  name)

Return the uniform ID.

Parameters
nameThe uniform name in the Shader.
Returns
The GPU uniform ID. Return 0 and display an error if not found.

Definition at line 278 of file Shader.cpp.

278 {
279 auto it = impl_->uniforms.find(name);
280 if (it == impl_->uniforms.end()) {
281 // impl_->uniforms that is not referenced
282 GLint r = glGetUniformLocation(id(), name.c_str());
283
284 if (r == GL_INVALID_OPERATION || r < 0) {
285 std::cerr << "[Error] Uniform " << name << " doesn't exist in program"
286 << std::endl;
287 }
288 // add it anyways
289 impl_->uniforms[name] = r;
290
291 return r;
292 } else {
293 return it->second;
294 }
295}

◆ Unuse()

void smk::ShaderProgram::Unuse ( ) const

Unbind the ShaderProgram.

Definition at line 435 of file Shader.cpp.

435 {
436 glUseProgram(0);
437}

◆ Use()

void smk::ShaderProgram::Use ( ) const

Bind the ShaderProgram. Future draw will use it. This unbind any previously bound ShaderProgram.

Definition at line 429 of file Shader.cpp.

429 {
430 glUseProgram(id());
431}

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