Skip to main content

OpenGLShader Class

An OpenGL-specific implementation of a Shader program, consisting of a vertex and a fragment shader. More...

Declaration

class helios::ext::opengl::rendering::shader::OpenGLShader { ... }

Base class

classShader

Abstract representation of a Shader program (e.g. composed of vertex/fragment shader). More...

Public Constructors Index

OpenGLShader (const OpenGLShader &)=delete

Rule of three. More...

OpenGLShader (const std::string &vertexShaderPath, const std::string &fragmentShaderPath, const helios::util::io::StringFileReader &stringFileReader)

Creates and initializes this OpenGLShader. An instance of this class is guaranteed to have a progId_ != 0, hence shader-files where successfully loaded and compiled, ready to be used. More...

Public Destructor Index

~OpenGLShader () override

Deletes the program object upon destruction of this instance. More...

Public Operators Index

OpenGLShader &operator= (const OpenGLShader &)=delete

Public Member Functions Index

voiduse () const noexcept override

Activates this OpenGLShader for subsequent draw calls. This implementation calls `glUseProgram` with the `progId_` received after compilation. More...

voidsetUniformLocationMap (std::unique_ptr< const OpenGLUniformLocationMap > uniformLocationMap) noexcept

Sets the OpenGLUniformLocationMap for this OpenGLShader. Ownership is transferred to this instance. More...

intuniformLocation (helios::rendering::shader::UniformSemantics uniformSemantics) const noexcept

Returns the uniform location for the uniform represented by the specified UniformSemantics. More...

voidapplyUniformValues (const helios::rendering::shader::UniformValueMap &uniformValueMap) const noexcept override

Applies the specified UniformValueMap to the uniforms defined by the shader. This method does not change the binding state of the shader, but it passes the uniform values to the underlying rendering backend. Implementations should ensure that the shader is properly bound before this method is called. More...

Private Member Functions Index

voidload (const std::string &vertexShaderPath, const std::string &fragmentShaderPath, const helios::util::io::StringFileReader &stringFileReader)

Loads the specified vertex and fragment shader. More...

voidcompile ()

Compiles the vertex and fragment shader represented by this instance. More...

Protected Member Attributes Index

unsigned intprogId_ = 0

The program id as assigned by the underlying rendering backend. More...

std::unique_ptr< const OpenGLUniformLocationMap >uniformLocationMap_ = nullptr

A unique pointer to the OpenGLUniformLocationMap this shader uses. More...

Private Member Attributes Index

std::stringvertexShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful. More...

std::stringfragmentShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful. More...

Description

An OpenGL-specific implementation of a Shader program, consisting of a vertex and a fragment shader.

This class manages the lifecycle of the Shaders. Source files are getting loaded via a StringFileReader and immediately compiled after loading. Any occupied memory for source-files and file-paths to the shader is being cleared once compilation succeeded and are not guaranteed to persist the compilation process.

Definition at line 34 of file OpenGLShader.ixx.

Public Constructors

OpenGLShader()

helios::ext::opengl::rendering::shader::OpenGLShader::OpenGLShader (const OpenGLShader &)
delete

OpenGLShader()

helios::ext::opengl::rendering::shader::OpenGLShader::OpenGLShader (const std::string & vertexShaderPath, const std::string & fragmentShaderPath, const helios::util::io::StringFileReader & stringFileReader)
inline

Creates and initializes this OpenGLShader. An instance of this class is guaranteed to have a progId_ != 0, hence shader-files where successfully loaded and compiled, ready to be used.

Parameters
vertexShaderPath

The path to the vertex shader.

fragmentShaderPath

The path to the fragment shader.

stringFileReader

The StringFileReader used for loading the shader source files.

Exceptions
if

creating this shader failed.

Definition at line 173 of file OpenGLShader.ixx.

174 const std::string& vertexShaderPath,
175 const std::string& fragmentShaderPath,
176 const helios::util::io::StringFileReader& stringFileReader
177 ) {
178 try {
179 load(vertexShaderPath, fragmentShaderPath, stringFileReader);
180 compile();
181 } catch (std::runtime_error& e) {
182 logger_.error("Could not initialize shader");
183 throw std::runtime_error("Could not initialize shader");
184 }
185 }

Reference helios::rendering::shader::Shader::logger_.

Public Destructor

~OpenGLShader()

helios::ext::opengl::rendering::shader::OpenGLShader::~OpenGLShader ()
inline

Deletes the program object upon destruction of this instance.

See Also

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml

Definition at line 207 of file OpenGLShader.ixx.

207 ~OpenGLShader() override {
208 if (progId_ != 0) {
209 glDeleteProgram(progId_);
210 }
211 }

Reference progId_.

Public Operators

operator=()

OpenGLShader & helios::ext::opengl::rendering::shader::OpenGLShader::operator= (const OpenGLShader &)
delete

Definition at line 159 of file OpenGLShader.ixx.

Reference OpenGLShader.

Public Member Functions

applyUniformValues()

void helios::ext::opengl::rendering::shader::OpenGLShader::applyUniformValues (const helios::rendering::shader::UniformValueMap & uniformValueMap)
inline noexcept virtual

Applies the specified UniformValueMap to the uniforms defined by the shader. This method does not change the binding state of the shader, but it passes the uniform values to the underlying rendering backend. Implementations should ensure that the shader is properly bound before this method is called.

Parameters
uniformValueMap

A const ref to the `UniformValueMap` containing the values for the uniforms in this shader.

Definition at line 247 of file OpenGLShader.ixx.

248 const helios::rendering::shader::UniformValueMap& uniformValueMap) const noexcept override {
249
250 if (const auto viewMatrixUniform = uniformLocation(helios::rendering::shader::UniformSemantics::ViewMatrix); viewMatrixUniform != -1) {
251 if (const auto* mat4f_ptr = uniformValueMap.mat4f_ptr(helios::rendering::shader::UniformSemantics::ViewMatrix)) {
252 glUniformMatrix4fv(viewMatrixUniform, 1, false, mat4f_ptr);
253 }
254 }
255
256 if (const auto projectionMatrixUniform = uniformLocation(helios::rendering::shader::UniformSemantics::ProjectionMatrix); projectionMatrixUniform != -1) {
257 if (const auto* mat4f_ptr = uniformValueMap.mat4f_ptr(helios::rendering::shader::UniformSemantics::ProjectionMatrix)) {
258 glUniformMatrix4fv(projectionMatrixUniform, 1, false, mat4f_ptr);
259 }
260 }
261
262 if (const auto modelMatrixUniform = uniformLocation(helios::rendering::shader::UniformSemantics::ModelMatrix); modelMatrixUniform != -1) {
263 if (const auto* mat4f_ptr = uniformValueMap.mat4f_ptr(helios::rendering::shader::UniformSemantics::ModelMatrix)) {
264 glUniformMatrix4fv(modelMatrixUniform, 1, false, mat4f_ptr);
265 }
266 }
267 if (const auto materialBaseColorUniform = uniformLocation(helios::rendering::shader::UniformSemantics::MaterialBaseColor); materialBaseColorUniform != -1) {
268 if (const auto* vec4f_ptr = uniformValueMap.vec4f_ptr(helios::rendering::shader::UniformSemantics::MaterialBaseColor)) {
269 glUniform4fv(materialBaseColorUniform, 1, vec4f_ptr);
270 }
271 }
272
273 // texture
274 if (const auto textColorUniform = uniformLocation(helios::rendering::shader::UniformSemantics::TextColor); textColorUniform != -1) {
275 if (const auto* vec4f_ptr = uniformValueMap.vec4f_ptr(helios::rendering::shader::UniformSemantics::TextColor)) {
276 glUniform4fv(textColorUniform, 1, vec4f_ptr);
277 }
278 }
279 if (const auto textTextureUniform = uniformLocation(helios::rendering::shader::UniformSemantics::TextTexture); textTextureUniform != -1) {
280 if (const auto* int_ptr = uniformValueMap.int_ptr(helios::rendering::shader::UniformSemantics::TextTexture)) {
281 glUniform1i(textTextureUniform, *int_ptr);
282 }
283 }
284 }

References helios::rendering::shader::MaterialBaseColor, helios::rendering::shader::ModelMatrix, helios::rendering::shader::ProjectionMatrix, helios::rendering::shader::TextColor, helios::rendering::shader::TextTexture, uniformLocation and helios::rendering::shader::ViewMatrix.

setUniformLocationMap()

void helios::ext::opengl::rendering::shader::OpenGLShader::setUniformLocationMap (std::unique_ptr< const OpenGLUniformLocationMap > uniformLocationMap)
inline noexcept

Sets the OpenGLUniformLocationMap for this OpenGLShader. Ownership is transferred to this instance.

Parameters
uniformLocationMap

The OpenGLUniformMap providing the mappings for the uniforms of the underlying GLSL shader.

Definition at line 220 of file OpenGLShader.ixx.

221 std::unique_ptr<const OpenGLUniformLocationMap> uniformLocationMap) noexcept {
222 uniformLocationMap_ = std::move(uniformLocationMap);
223 }

Reference uniformLocationMap_.

uniformLocation()

int helios::ext::opengl::rendering::shader::OpenGLShader::uniformLocation (helios::rendering::shader::UniformSemantics uniformSemantics)
inline nodiscard noexcept

Returns the uniform location for the uniform represented by the specified UniformSemantics.

Parameters
uniformSemantics

The `UniformSemantics` identifier.

Returns

The integer representing the uniform variable location in the shader, or -1 if no location map was registered with this shader or if the uniform with the specified semantics was not found.

Definition at line 235 of file OpenGLShader.ixx.

235 [[nodiscard]] int uniformLocation(
236 helios::rendering::shader::UniformSemantics uniformSemantics) const noexcept {
238 return uniformLocationMap_->get(uniformSemantics);
239 }
240
241 return -1;
242 }

Reference uniformLocationMap_.

Referenced by applyUniformValues.

use()

void helios::ext::opengl::rendering::shader::OpenGLShader::use ()
inline noexcept virtual

Activates this OpenGLShader for subsequent draw calls. This implementation calls `glUseProgram` with the `progId_` received after compilation.

See Also

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glUseProgram.xhtml

Definition at line 195 of file OpenGLShader.ixx.

195 void use() const noexcept override {
196 if (!progId_) {
197 logger_.error("Cannot use shader, progId_ is invalid");
198 }
199 glUseProgram(progId_);
200 }

References helios::rendering::shader::Shader::logger_ and progId_.

Private Member Functions

compile()

void helios::ext::opengl::rendering::shader::OpenGLShader::compile ()
inline

Compiles the vertex and fragment shader represented by this instance.

Returns

true if compilation succeeded, otherwise false.

Exceptions
if

compilation failed.

Definition at line 78 of file OpenGLShader.ixx.

78 void compile() {
79 if (progId_ != 0) {
80 logger_.warn("Shader already compiled");
81 return;
82 }
83
84 const GLchar* vertexSrc = vertexShaderSource_.c_str();
85 const GLchar* fragmentSrc = fragmentShaderSource_.c_str();
86
87 const unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
88 glShaderSource(vertexShader, 1, &vertexSrc, nullptr);
89 glCompileShader(vertexShader);
90
91 const unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
92 glShaderSource(fragmentShader, 1, &fragmentSrc, nullptr);
93 glCompileShader(fragmentShader);
94
95
96 int success;
97 char infoLog[512];
98 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
99
100 if (!success) {
101 glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
102
103 logger_.error("VERTEX::COMPILATION_FAILED " + static_cast<std::string>(infoLog));
104 throw std::runtime_error("Vertex Shader Compilation failed.");
105 }
106
107 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
108 if (!success) {
109 glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
110 logger_.error("SHADER::FRAGMENT::COMPILATION_FAILED " + static_cast<std::string>(infoLog));
111 throw std::runtime_error("Fragment Shader Compilation failed.");
112 }
113
114 progId_ = glCreateProgram();
115
116 glAttachShader(progId_, vertexShader);
117 glAttachShader(progId_, fragmentShader);
118 glLinkProgram(progId_);
119
120 glGetProgramiv(progId_, GL_LINK_STATUS, &success);
121 if (!success) {
122 glGetProgramInfoLog(progId_, 512, nullptr, infoLog);
123 logger_.error("PROGRAM_LINKING_FAILED " + static_cast<std::string>(infoLog));
124 throw std::runtime_error("Program linking failed.");
125 }
126
127 glDeleteShader(vertexShader);
128 glDeleteShader(fragmentShader);
129
130 vertexShaderSource_.clear();
131 vertexShaderSource_.shrink_to_fit();
132 fragmentShaderSource_.clear();
133 fragmentShaderSource_.shrink_to_fit();
134
135 logger_.info("Shader loaded and linked");
136 }

load()

void helios::ext::opengl::rendering::shader::OpenGLShader::load (const std::string & vertexShaderPath, const std::string & fragmentShaderPath, const helios::util::io::StringFileReader & stringFileReader)
inline

Loads the specified vertex and fragment shader.

Returns

true if loading succeeded, otherwise false.

Exceptions
if

loading the specified files failed.

Definition at line 57 of file OpenGLShader.ixx.

57 void load(
58 const std::string& vertexShaderPath,
59 const std::string& fragmentShaderPath,
60 const helios::util::io::StringFileReader& stringFileReader
61 ) {
62 logger_.info(std::format("Loading shader from {0}, {1}", vertexShaderPath, fragmentShaderPath));
63 if (!stringFileReader.readInto(fragmentShaderPath, fragmentShaderSource_) ||
64 !stringFileReader.readInto(vertexShaderPath, vertexShaderSource_)) {
65 logger_.error("Could not load shader");
66 throw std::runtime_error("Could not load shader");
67 }
68 }

Protected Member Attributes

progId_

unsigned int helios::ext::opengl::rendering::shader::OpenGLShader::progId_ = 0
protected

The program id as assigned by the underlying rendering backend.

Definition at line 143 of file OpenGLShader.ixx.

143 unsigned int progId_ = 0;

Referenced by use and ~OpenGLShader.

uniformLocationMap_

std::unique_ptr<const OpenGLUniformLocationMap> helios::ext::opengl::rendering::shader::OpenGLShader::uniformLocationMap_ = nullptr
protected

A unique pointer to the OpenGLUniformLocationMap this shader uses.

Definition at line 148 of file OpenGLShader.ixx.

148 std::unique_ptr<const OpenGLUniformLocationMap> uniformLocationMap_ = nullptr;

Referenced by setUniformLocationMap and uniformLocation.

Private Member Attributes

fragmentShaderSource_

std::string helios::ext::opengl::rendering::shader::OpenGLShader::fragmentShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful.

Definition at line 48 of file OpenGLShader.ixx.

48 std::string fragmentShaderSource_;

vertexShaderSource_

std::string helios::ext::opengl::rendering::shader::OpenGLShader::vertexShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful.

Definition at line 41 of file OpenGLShader.ixx.

41 std::string vertexShaderSource_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.