Skip to content

Class RenderShader

ClassList > RenderShader

Standard render shader with vertex and fragment stages. More...

  • #include <RenderShader.h>

Inherits the following classes: Shaders

Public Types inherited from Shaders

See Shaders

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

Public Attributes inherited from Shaders

See Shaders

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 inherited from Shaders

See Shaders

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=NONE_SHADER) override
Compiles a shader stage.
void CreatShader (const std::string & verShader, const std::string & fragShader)
Creates shader program from vertex and fragment shader names.
virtual void GenerateShader (ShaderType tar=NONE_SHADER) override
Generates shader code (unused for standard shaders).
virtual std::vector< ShaderType > GetAllShaderTypes () override const
Returns list of shader stages in this program.
virtual GLuint GetShaderID (ShaderType type) override const
Returns OpenGL shader ID for a specific stage.
virtual ShaderUnit * GetShaderUnit (ShaderType tar=NONE_SHADER) override
Returns pointer to shader unit for a specific stage.
virtual void LocalDebug () override const
Prints debug information about vertex and fragment shaders.
virtual void ParseShaderCode (const std::string & _code, ShaderType _type) override
Parses raw GLSL code string.
void ParseShaderFile (std::string _name, ShaderType _type)
Parses shader code from a file.
void ParseShaderStream (std::istream & _stream, ShaderType _type)
Parses shader code from an input stream.
virtual void RelinkShader (ShaderType tar=NONE_SHADER) override
Relinks the shader program.
RenderShader (const std::string & vert, const std::string & frag="")
Constructs and loads a render shader from files.
RenderShader ()
Default constructor (creates empty shader).
virtual void ResetID (ShaderType type, GLuint id) override
Resets shader ID for a specific stage.
RenderShader & operator= (RenderShader &&) = default
Move assignment operator.
~RenderShader ()
Destructor (releases vertex and fragment shader objects).

Public Functions inherited from Shaders

See Shaders

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 inherited from Shaders

See Shaders

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 inherited from Shaders

See Shaders

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 inherited from Shaders

See Shaders

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

RenderShader is the primary shader type for rasterization rendering. It compiles vertex and fragment shader code, links them into a program, and provides runtime recompilation support for live shader editing.

Usage:

RenderShader shader("pbr_vert", "pbr_frag");  // Load from files
shader.UseShader();
shader.SetValue("u_projection", camera.GetProjection());
// ... draw calls ...
shader.UnuseShader();

Note:

Shader files are loaded from Shaders::folder_root with appropriate extensions.

Note:

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

Public Functions Documentation

function CompileShader

Compiles a shader stage.

virtual GLuint RenderShader::CompileShader (
    ShaderType tar=NONE_SHADER
) override

Parameters:

  • tar Shader stage to compile, or NONE_SHADER to compile both

Returns:

OpenGL shader object ID, or 0 on failure

Implements Shaders::CompileShader


function CreatShader

Creates shader program from vertex and fragment shader names.

void RenderShader::CreatShader (
    const std::string & verShader,
    const std::string & fragShader
) 

Parameters:

  • verShader Vertex shader file name
  • fragShader Fragment shader file name

Note:

Loads files, compiles shaders, and links program


function GenerateShader

Generates shader code (unused for standard shaders).

virtual void RenderShader::GenerateShader (
    ShaderType tar=NONE_SHADER
) override

Parameters:

  • tar Target shader stage

Implements Shaders::GenerateShader


function GetAllShaderTypes

Returns list of shader stages in this program.

inline virtual std::vector< ShaderType > RenderShader::GetAllShaderTypes () override const

Returns:

Vector containing VERTEX_SHADER and FRAGMENT_SHADER

Implements Shaders::GetAllShaderTypes


function GetShaderID

Returns OpenGL shader ID for a specific stage.

inline virtual GLuint RenderShader::GetShaderID (
    ShaderType type
) override const

Parameters:

  • type Shader stage type

Returns:

Shader object ID

Implements Shaders::GetShaderID


function GetShaderUnit

Returns pointer to shader unit for a specific stage.

virtual ShaderUnit * RenderShader::GetShaderUnit (
    ShaderType tar=NONE_SHADER
) override

Parameters:

  • tar Shader stage (vertex or fragment)

Returns:

Pointer to ShaderUnit

Implements Shaders::GetShaderUnit


function LocalDebug

Prints debug information about vertex and fragment shaders.

virtual void RenderShader::LocalDebug () override const

Implements Shaders::LocalDebug


function ParseShaderCode

Parses raw GLSL code string.

virtual void RenderShader::ParseShaderCode (
    const std::string & _code,
    ShaderType _type
) override

Parameters:

  • _code GLSL source code
  • _type Shader stage type (vertex or fragment)

Implements Shaders::ParseShaderCode


function ParseShaderFile

Parses shader code from a file.

void RenderShader::ParseShaderFile (
    std::string _name,
    ShaderType _type
) 

Parameters:

  • _name Shader file name (without path)
  • _type Shader stage type (vertex or fragment)

function ParseShaderStream

Parses shader code from an input stream.

void RenderShader::ParseShaderStream (
    std::istream & _stream,
    ShaderType _type
) 

Parameters:

  • _stream Input stream containing GLSL code
  • _type Shader stage type (vertex or fragment)

function RelinkShader

Relinks the shader program.

virtual void RenderShader::RelinkShader (
    ShaderType tar=NONE_SHADER
) override

Parameters:

  • tar Target shader to recompile before linking, or NONE_SHADER to link without recompilation

Implements Shaders::RelinkShader


function RenderShader [1/2]

Constructs and loads a render shader from files.

RenderShader::RenderShader (
    const std::string & vert,
    const std::string & frag=""
) 

Parameters:

  • vert Vertex shader file name (without extension)
  • frag Fragment shader file name (without extension, defaults to vert name)

Note:

Automatically loads and compiles shaders from disk


function RenderShader [2/2]

Default constructor (creates empty shader).

RenderShader::RenderShader () 


function ResetID

Resets shader ID for a specific stage.

virtual void RenderShader::ResetID (
    ShaderType type,
    GLuint id
) override

Parameters:

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

Implements Shaders::ResetID


function operator=

Move assignment operator.

RenderShader & RenderShader::operator= (
    RenderShader &&
) = default

Parameters:

  • other Shader to move from

Returns:

Reference to this shader


function ~RenderShader

Destructor (releases vertex and fragment shader objects).

RenderShader::~RenderShader () 



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