Skip to content

Class Shaders

ClassList > Shaders

Base class for OpenGL shader programs. More...

  • #include <Shaders.h>

Inherited by the following classes: ChainedShader, ComputeShader, FastLoadShader, RenderShader

Classes

Type Name
struct ArrayUni
Array uniform data container.
struct ShaderPair
Pair of vertex and fragment shader names for loading.
class ShaderUnit
Individual shader compilation unit (vertex, fragment, compute, etc.).

Public Types

Type Name
typedef std::tuple< std::string, std::string, GLuint > ShaderConstInfo
Shader metadata tuple: (name, filename, GL_enum).

Public Attributes

Type Name
std::function< void(void)> InitShader = {}
Optional callback for custom shader initialization.
bool is_shader_changed = { true }
Flag indicating shader code has changed and needs recompilation.
GLuint program_id = { 0 }
OpenGL shader program ID (owned by this class)

Public Static Attributes

Type Name
std::vector< std::string > file_type = { ".vert", ".frag", ".comp", ".geom" }
File extensions for shader types.
std::string folder_root = "res/shaders/"
Root directory for shader files.
std::string const shader_type = { "Vertex Shader", "Fragment Shader", "Compute Shader", "Geometry Shader" }
Shader type name strings.

Public Functions

Type Name
virtual GLuint CompileShader (ShaderType tar) = 0
Compiles a shader stage.
virtual void GenerateShader (ShaderType tar=NONE_SHADER) = 0
Generates shader code (for procedural shaders).
virtual std::vector< ShaderType > GetAllShaderTypes () const = 0
Returns list of all shader stages in this program.
GLuint GetProgramID () const
Returns the OpenGL program ID.
virtual GLuint GetShaderID (ShaderType type) const = 0
Returns the OpenGL shader object ID for a specific stage.
virtual ShaderUnit * GetShaderUnit (ShaderType tar=NONE_SHADER) = 0
Returns pointer to shader unit for a specific stage.
virtual void LocalDebug () const = 0
Prints debug information about the shader program.
virtual void ParseShaderCode (const std::string & _code, ShaderType _type) = 0
Parses GLSL source code for a shader stage.
virtual void RelinkShader (ShaderType tar=NONE_SHADER) = 0
Relinks the shader program after shader unit changes.
void ResetCache ()
Clears the uniform location cache.
virtual void ResetID (ShaderType type, GLuint id) = 0
Resets the shader ID for a specific stage.
void SetValue (const std::string & name, const glm::mat4 & projection)
void SetValue (const std::string & name, int v0)
void SetValue (const std::string & name, int v0, int v1, int v2)
void SetValue (const std::string & name, int v0, int v1, int v2, int v3)
void SetValue (const std::string & name, float v0)
void SetValue (const std::string & name, float v0, float v1)
void SetValue (const std::string & name, float v0, float v1, float v2)
void SetValue (const std::string & name, float v0, float v1, float v2, float v3)
void SetValue (const std::string & name, const glm::vec2 & vec2)
void SetValue (const std::string & name, const glm::vec3 & vec3)
void SetValue (const std::string & name, const glm::vec4 & vec4)
void SetValue (const std::string & name, bool v0)
void SetValue (const std::string & name, const GLuint & v0)
void SetValue (const std::string & name, GLsizei count, const float * va0, ArrayType TYPE)
void SetValue (const std::string & name, Shaders::ArrayUni arr)
void SetValue (const std::string & name, GLsizei count, const int * va0, ArrayType TYPE)
void SetValue (const std::string & name, GLsizei count, const GLuint * va0, ArrayType TYPE)
void SetValue (const std::string & name, GLsizei count, const glm::mat4 * va0)
Shaders ()
Default constructor.
Shaders (const Shaders & shader)
Copy constructor.
Shaders (Shaders && shader) noexcept
Move constructor (transfers ownership of GPU resources).
void UnuseShader () const
Unbinds this shader program.
void UseShader () const
Binds this shader program for rendering.
GLuint getVarID (const char * name) const
Gets uniform location ID for a uniform variable.
Shaders & operator= (const Shaders & shader)
Copy assignment operator.
Shaders & operator= (Shaders && shader) noexcept
Move assignment operator (transfers ownership).
~Shaders ()
Destructor (releases OpenGL program object).

Public Static Functions

Type Name
GLuint CompileShaderCode (ShaderType _type, const std::string & source)
Compiles GLSL source code into an OpenGL shader object.
ShaderType ParseFileEXT (std::string path)
Infers shader type from file extension.
ShaderConstInfo ParseShaderType (ShaderType _type)
Parses shader type to metadata tuple.
std::string ReadShaderFile (ShaderType _type, const std::string & name)
Reads shader source from file.

Protected Attributes

Type Name
std::unordered_map< std::string, int > _LINK_LOC
Map of linked uniform locations.
std::unordered_map< std::string, GLuint > _uniforms_cache
Cache of uniform locations (avoids redundant glGetUniformLocation)

Protected Functions

Type Name
void _del ()
Deletes the OpenGL program object.
bool _is_link_repeat (const std::string _name)
Checks if a uniform name has already been linked.
void _resetProgramID (GLuint _ID)
Resets program ID, deleting old program if needed.

Detailed Description

Shaders provides the core functionality for shader compilation, linking, and uniform management. It is abstract and must be subclassed by specific shader types (RenderShader, ComputeShader).

Lifecycle: * Construct shader (load/parse code) * Compile shader units (vertex, fragment, etc.) * Link shader program * Use shader and set uniforms * Destroy shader (releases GPU resources)

Note:

Thread-safety: Not thread-safe. Must be used from OpenGL context thread.

Note:

Resource ownership: Owns program_id, releases via glDeleteProgram in destructor.

Public Types Documentation

typedef ShaderConstInfo

Shader metadata tuple: (name, filename, GL_enum).

using Shaders::ShaderConstInfo =  std::tuple<std::string, std::string, GLuint>;


Public Attributes Documentation

variable InitShader

Optional callback for custom shader initialization.

std::function<void(void)> Shaders::InitShader;


variable is_shader_changed

Flag indicating shader code has changed and needs recompilation.

bool Shaders::is_shader_changed;


variable program_id

OpenGL shader program ID (owned by this class)

GLuint Shaders::program_id;


Public Static Attributes Documentation

variable file_type

File extensions for shader types.

std::vector< std::string > Shaders::file_type;


variable folder_root

Root directory for shader files.

std::string Shaders::folder_root;


variable shader_type

Shader type name strings.

std::string const Shaders::shader_type;


Public Functions Documentation

function CompileShader

Compiles a shader stage.

virtual GLuint Shaders::CompileShader (
    ShaderType tar
) = 0

Parameters:

  • tar Target shader stage to compile

Returns:

OpenGL shader object ID, or 0 on failure


function GenerateShader

Generates shader code (for procedural shaders).

virtual void Shaders::GenerateShader (
    ShaderType tar=NONE_SHADER
) = 0

Parameters:

  • tar Target shader stage, or NONE_SHADER for all

function GetAllShaderTypes

Returns list of all shader stages in this program.

virtual std::vector< ShaderType > Shaders::GetAllShaderTypes () const = 0

Returns:

Vector of ShaderType enums


function GetProgramID

Returns the OpenGL program ID.

inline GLuint Shaders::GetProgramID () const

Returns:

Program ID


function GetShaderID

Returns the OpenGL shader object ID for a specific stage.

virtual GLuint Shaders::GetShaderID (
    ShaderType type
) const = 0

Parameters:

  • type Shader stage type

Returns:

Shader object ID


function GetShaderUnit

Returns pointer to shader unit for a specific stage.

virtual ShaderUnit * Shaders::GetShaderUnit (
    ShaderType tar=NONE_SHADER
) = 0

Parameters:

  • tar Target shader stage, or NONE_SHADER for default

Returns:

Pointer to ShaderUnit


function LocalDebug

Prints debug information about the shader program.

virtual void Shaders::LocalDebug () const = 0

Logs shader units, uniforms, attributes, and compilation status. Used for debugging shader issues.


function ParseShaderCode

Parses GLSL source code for a shader stage.

virtual void Shaders::ParseShaderCode (
    const std::string & _code,
    ShaderType _type
) = 0

Parameters:

  • _code GLSL source code
  • _type Shader stage type

function RelinkShader

Relinks the shader program after shader unit changes.

virtual void Shaders::RelinkShader (
    ShaderType tar=NONE_SHADER
) = 0

Parameters:

  • tar Target shader stage to relink, or NONE_SHADER to relink all

function ResetCache

Clears the uniform location cache.

inline void Shaders::ResetCache () 

Forces uniform locations to be re-queried on next SetValue call. Should be called after shader relinking.


function ResetID

Resets the shader ID for a specific stage.

virtual void Shaders::ResetID (
    ShaderType type,
    GLuint id
) = 0

Parameters:

  • type Shader stage type
  • id New OpenGL shader object ID

function SetValue [1/18]

void Shaders::SetValue (
    const std::string & name,
    const glm::mat4 & projection
) 

function SetValue [2/18]

void Shaders::SetValue (
    const std::string & name,
    int v0
) 

function SetValue [3/18]

void Shaders::SetValue (
    const std::string & name,
    int v0,
    int v1,
    int v2
) 

function SetValue [4/18]

void Shaders::SetValue (
    const std::string & name,
    int v0,
    int v1,
    int v2,
    int v3
) 

function SetValue [5/18]

void Shaders::SetValue (
    const std::string & name,
    float v0
) 

function SetValue [6/18]

void Shaders::SetValue (
    const std::string & name,
    float v0,
    float v1
) 

function SetValue [7/18]

void Shaders::SetValue (
    const std::string & name,
    float v0,
    float v1,
    float v2
) 

function SetValue [8/18]

void Shaders::SetValue (
    const std::string & name,
    float v0,
    float v1,
    float v2,
    float v3
) 

function SetValue [9/18]

void Shaders::SetValue (
    const std::string & name,
    const glm::vec2 & vec2
) 

function SetValue [10/18]

void Shaders::SetValue (
    const std::string & name,
    const glm::vec3 & vec3
) 

function SetValue [11/18]

void Shaders::SetValue (
    const std::string & name,
    const glm::vec4 & vec4
) 

function SetValue [12/18]

void Shaders::SetValue (
    const std::string & name,
    bool v0
) 

function SetValue [13/18]

void Shaders::SetValue (
    const std::string & name,
    const GLuint & v0
) 

function SetValue [14/18]

void Shaders::SetValue (
    const std::string & name,
    GLsizei count,
    const float * va0,
    ArrayType TYPE
) 

function SetValue [15/18]

void Shaders::SetValue (
    const std::string & name,
    Shaders::ArrayUni arr
) 

function SetValue [16/18]

void Shaders::SetValue (
    const std::string & name,
    GLsizei count,
    const int * va0,
    ArrayType TYPE
) 

function SetValue [17/18]

void Shaders::SetValue (
    const std::string & name,
    GLsizei count,
    const GLuint * va0,
    ArrayType TYPE
) 

function SetValue [18/18]

void Shaders::SetValue (
    const std::string & name,
    GLsizei count,
    const glm::mat4 * va0
) 

function Shaders [1/3]

Default constructor.

inline Shaders::Shaders () 


function Shaders [2/3]

Copy constructor.

Shaders::Shaders (
    const Shaders & shader
) 

Parameters:

  • shader Source shader to copy

function Shaders [3/3]

Move constructor (transfers ownership of GPU resources).

Shaders::Shaders (
    Shaders && shader
) noexcept

Parameters:

  • shader Source shader to move from

function UnuseShader

Unbinds this shader program.

void Shaders::UnuseShader () const

Calls glUseProgram(0). Should be called after rendering is complete.


function UseShader

Binds this shader program for rendering.

void Shaders::UseShader () const

Calls glUseProgram with this program's ID. Must be called before setting uniforms or issuing draw calls.


function getVarID

Gets uniform location ID for a uniform variable.

inline GLuint Shaders::getVarID (
    const char * name
) const

Parameters:

  • name Uniform variable name

Returns:

OpenGL uniform location ID

Note:

Uses cache to avoid redundant glGetUniformLocation calls


function operator=

Copy assignment operator.

Shaders & Shaders::operator= (
    const Shaders & shader
) 

Parameters:

  • shader Source shader to copy

Returns:

Reference to this shader


function operator=

Move assignment operator (transfers ownership).

Shaders & Shaders::operator= (
    Shaders && shader
) noexcept

Parameters:

  • shader Source shader to move from

Returns:

Reference to this shader


function ~Shaders

Destructor (releases OpenGL program object).

Shaders::~Shaders () 


Public Static Functions Documentation

function CompileShaderCode

Compiles GLSL source code into an OpenGL shader object.

static GLuint Shaders::CompileShaderCode (
    ShaderType _type,
    const std::string & source
) 

Parameters:

  • _type Shader stage type (vertex, fragment, etc.)
  • source GLSL source code string

Returns:

OpenGL shader object ID, or 0 on compilation failure


function ParseFileEXT

Infers shader type from file extension.

static ShaderType Shaders::ParseFileEXT (
    std::string path
) 

Parameters:

  • path Shader file path

Returns:

ShaderType inferred from extension (.vert, .frag, .comp, etc.)


function ParseShaderType

Parses shader type to metadata tuple.

static ShaderConstInfo Shaders::ParseShaderType (
    ShaderType _type
) 

Parameters:

  • _type Shader type enum

Returns:

Tuple of (name, filename extension, OpenGL enum)


function ReadShaderFile

Reads shader source from file.

static std::string Shaders::ReadShaderFile (
    ShaderType _type,
    const std::string & name
) 

Parameters:

  • _type Shader stage type
  • name Shader file name (without path or extension)

Returns:

GLSL source code as string


Protected Attributes Documentation

Map of linked uniform locations.

std::unordered_map<std::string, int> Shaders::_LINK_LOC;


variable _uniforms_cache

Cache of uniform locations (avoids redundant glGetUniformLocation)

std::unordered_map<std::string, GLuint> Shaders::_uniforms_cache;


Protected Functions Documentation

function _del

Deletes the OpenGL program object.

void Shaders::_del () 


Checks if a uniform name has already been linked.

inline bool Shaders::_is_link_repeat (
    const std::string _name
) 

Parameters:

  • _name Uniform name to check

Returns:

True if uniform already linked


function _resetProgramID

Resets program ID, deleting old program if needed.

inline void Shaders::_resetProgramID (
    GLuint _ID
) 

Parameters:

  • _ID New program ID to assign


The documentation for this class was generated from the following file src/render/Shaders.h