Skip to main content

LegacyOpenGLShader 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::LegacyOpenGLShader { ... }

Base class

classShader

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

Public Constructors Index

LegacyOpenGLShader (const LegacyOpenGLShader &)=delete

Rule of three. More...

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

Creates and initializes this LegacyOpenGLShader. 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

~LegacyOpenGLShader () override

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

Public Operators Index

LegacyOpenGLShader &operator= (const LegacyOpenGLShader &)=delete

Public Member Functions Index

voiduse () const noexcept override

Activates this LegacyOpenGLShader 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 LegacyOpenGLShader. 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 36 of file LegacyOpenGLShader.ixx.

Public Constructors

LegacyOpenGLShader()

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

LegacyOpenGLShader()

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

Creates and initializes this LegacyOpenGLShader. 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 175 of file LegacyOpenGLShader.ixx.

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

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

Public Destructor

~LegacyOpenGLShader()

helios::ext::opengl::rendering::shader::LegacyOpenGLShader::~LegacyOpenGLShader ()
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 209 of file LegacyOpenGLShader.ixx.

210 if (progId_ != 0) {
211 glDeleteProgram(progId_);
212 }
213 }

Reference progId_.

Public Operators

operator=()

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

Definition at line 161 of file LegacyOpenGLShader.ixx.

Reference LegacyOpenGLShader.

Public Member Functions

applyUniformValues()

void helios::ext::opengl::rendering::shader::LegacyOpenGLShader::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 249 of file LegacyOpenGLShader.ixx.

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

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::LegacyOpenGLShader::setUniformLocationMap (std::unique_ptr< const OpenGLUniformLocationMap > uniformLocationMap)
inline noexcept

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

Parameters
uniformLocationMap

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

Definition at line 222 of file LegacyOpenGLShader.ixx.

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

Reference uniformLocationMap_.

uniformLocation()

int helios::ext::opengl::rendering::shader::LegacyOpenGLShader::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 237 of file LegacyOpenGLShader.ixx.

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

Reference uniformLocationMap_.

Referenced by applyUniformValues.

use()

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

Activates this LegacyOpenGLShader 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 197 of file LegacyOpenGLShader.ixx.

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

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

Private Member Functions

compile()

void helios::ext::opengl::rendering::shader::LegacyOpenGLShader::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 80 of file LegacyOpenGLShader.ixx.

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

load()

void helios::ext::opengl::rendering::shader::LegacyOpenGLShader::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 59 of file LegacyOpenGLShader.ixx.

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

Protected Member Attributes

progId_

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

The program id as assigned by the underlying rendering backend.

Definition at line 145 of file LegacyOpenGLShader.ixx.

145 unsigned int progId_ = 0;

Referenced by use and ~LegacyOpenGLShader.

uniformLocationMap_

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

A unique pointer to the OpenGLUniformLocationMap this shader uses.

Definition at line 150 of file LegacyOpenGLShader.ixx.

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

Referenced by setUniformLocationMap and uniformLocation.

Private Member Attributes

fragmentShaderSource_

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

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

Definition at line 50 of file LegacyOpenGLShader.ixx.

50 std::string fragmentShaderSource_;

vertexShaderSource_

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

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

Definition at line 43 of file LegacyOpenGLShader.ixx.

43 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.