Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::opengl::ShaderProgram Class Reference

Vertex and fragment shaders linked into one OpenGL program. More...

#include <aegis2d/render/opengl/shader.hpp>

Public Member Functions

 ShaderProgram (const ShaderSources &sources)
 Compiles both stages and links them.
 
 ShaderProgram (const ShaderProgram &)=delete
 
ShaderProgram & operator= (const ShaderProgram &)=delete
 
 ShaderProgram (ShaderProgram &&other) noexcept
 Takes over the program of other, which becomes empty.
 
ShaderProgram & operator= (ShaderProgram &&other) noexcept
 Deletes this program and takes over the program of other.
 
 ~ShaderProgram ()
 Deletes the program.
 
void use () const
 Makes the program current for the next draw calls.
 
std::uint32_t id () const noexcept
 OpenGL name of the program.
 
bool has_uniform (std::string_view name)
 Tells whether the linked program has an active uniform of this name.
 
void set_uniform (std::string_view name, std::int32_t value)
 Sets an int (or sampler) uniform.
 
void set_uniform (std::string_view name, float value)
 Sets a float uniform.
 
void set_uniform (std::string_view name, Vec2 value)
 Sets a vec2 uniform.
 
void set_uniform (std::string_view name, Color value)
 Sets a vec4 uniform from a color.
 
void set_uniform (std::string_view name, const Vec4 &value)
 Sets a vec4 uniform.
 
void set_uniform (std::string_view name, const Mat4 &matrix)
 Sets a mat4 uniform.
 

Static Public Member Functions

static std::string_view version_directive () noexcept
 #version line prepended to every stage.
 

Detailed Description

Vertex and fragment shaders linked into one OpenGL program.

The sources are written without #version: the program prepends the line matching the context (#version 450 core, or #version 410 core with AEGIS2D_MACOS_GL41), so the same shaders run on every platform.

Uniforms are set with glProgramUniform*, without binding the program; their locations are looked up once and cached. A uniform the driver optimized away (or a misspelt name) is ignored, as OpenGL does; has_uniform() tells which.

Needs a current Context; destroy the program before the context.

Constructor & Destructor Documentation

◆ ShaderProgram() [1/3]

aegis2d::render::opengl::ShaderProgram::ShaderProgram ( const ShaderSources &  sources)
explicit

Compiles both stages and links them.

Parameters
sourcesGLSL sources without #version line.
Exceptions
ShaderErrorwith the driver's log if a stage does not compile or the program does not link.

◆ ShaderProgram() [2/3]

aegis2d::render::opengl::ShaderProgram::ShaderProgram ( const ShaderProgram &  )
delete

◆ ShaderProgram() [3/3]

aegis2d::render::opengl::ShaderProgram::ShaderProgram ( ShaderProgram &&  other)
noexcept

Takes over the program of other, which becomes empty.

Parameters
otherProgram to move from.

◆ ~ShaderProgram()

aegis2d::render::opengl::ShaderProgram::~ShaderProgram ( )

Deletes the program.

Member Function Documentation

◆ has_uniform()

bool aegis2d::render::opengl::ShaderProgram::has_uniform ( std::string_view  name)

Tells whether the linked program has an active uniform of this name.

Parameters
nameUniform name as written in the GLSL source.
Returns
false for unknown names and uniforms the driver optimized away.

◆ id()

std::uint32_t aegis2d::render::opengl::ShaderProgram::id ( ) const
noexcept

OpenGL name of the program.

Returns
The program object, 0 once moved from.

◆ operator=() [1/2]

ShaderProgram & aegis2d::render::opengl::ShaderProgram::operator= ( const ShaderProgram &  )
delete

◆ operator=() [2/2]

ShaderProgram & aegis2d::render::opengl::ShaderProgram::operator= ( ShaderProgram &&  other)
noexcept

Deletes this program and takes over the program of other.

Parameters
otherProgram to move from.
Returns
This program.

◆ set_uniform() [1/6]

void aegis2d::render::opengl::ShaderProgram::set_uniform ( std::string_view  name,
Color  value 
)

Sets a vec4 uniform from a color.

Parameters
nameUniform name.
valueNew value, as (r, g, b, a).

◆ set_uniform() [2/6]

void aegis2d::render::opengl::ShaderProgram::set_uniform ( std::string_view  name,
const Mat4 &  matrix 
)

Sets a mat4 uniform.

Parameters
nameUniform name.
matrixNew value; GLM and GLSL both store matrices column by column.

◆ set_uniform() [3/6]

void aegis2d::render::opengl::ShaderProgram::set_uniform ( std::string_view  name,
const Vec4 &  value 
)

Sets a vec4 uniform.

Parameters
nameUniform name.
valueNew value.

◆ set_uniform() [4/6]

void aegis2d::render::opengl::ShaderProgram::set_uniform ( std::string_view  name,
float  value 
)

Sets a float uniform.

Parameters
nameUniform name.
valueNew value.

◆ set_uniform() [5/6]

void aegis2d::render::opengl::ShaderProgram::set_uniform ( std::string_view  name,
std::int32_t  value 
)

Sets an int (or sampler) uniform.

Parameters
nameUniform name.
valueNew value.

◆ set_uniform() [6/6]

void aegis2d::render::opengl::ShaderProgram::set_uniform ( std::string_view  name,
Vec2  value 
)

Sets a vec2 uniform.

Parameters
nameUniform name.
valueNew value.

◆ use()

void aegis2d::render::opengl::ShaderProgram::use ( ) const

Makes the program current for the next draw calls.

◆ version_directive()

static std::string_view aegis2d::render::opengl::ShaderProgram::version_directive ( )
staticnoexcept

#version line prepended to every stage.

Returns
"#version 450 core\n", or "#version 410 core\n" with AEGIS2D_MACOS_GL41.

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