Skip to content

Class ComputeShader

ClassList > ComputeShader

Compute shader for GPGPU parallel computation. More...

  • #include <ComputeShader.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

Type Name
ShaderUnit comp_shader
Compute shader unit.

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 the compute shader (unused in ComputeShader ).
ComputeShader (const std::string & name)
Constructs a compute shader from file.
ComputeShader (const std::string & name, const Tuples &... args)
Constructs a compute shader with default uniform values.
ComputeShader ()
Default constructor (creates empty shader).
void CreateShader (const std::string & compShader)
Creates compute shader program from file.
virtual void GenerateShader (ShaderType tar=NONE_SHADER) override
Generates shader code (unused in ComputeShader ).
virtual std::vector< ShaderType > GetAllShaderTypes () override const
Returns list of shader stages.
virtual GLuint GetShaderID (ShaderType type) override const
Returns OpenGL shader ID for compute stage.
virtual ShaderUnit * GetShaderUnit (ShaderType tar=NONE_SHADER) override
Returns pointer to compute shader unit.
virtual void LocalDebug () override const
Prints debug information about compute shader.
virtual void ParseShaderCode (const std::string & _code, ShaderType tar) override
Parses raw GLSL code for compute shader.
virtual void RelinkShader (ShaderType tar=NONE_SHADER) override
Relinks the compute shader program.
void ResetDefult (std::string name)
Resets shader uniforms to default values from config.
virtual void ResetID (ShaderType tar, GLuint _id) override
Resets compute shader ID.
void RunComputeShader (const glm::vec2 & _size)
Dispatches compute shader with explicit size.
void RunComputeShader (GLuint workgroup_count_x=1, GLuint workgroup_count_y=1, GLuint workgroup_count_z=1) const
Dispatches compute shader with explicit workgroup counts.
void RunComputeShaderSCR (const glm::vec2 & _scr_size, GLuint _batch, bool _edge_fix=true)
Dispatches compute shader scaled to screen resolution.
ComputeShader & operator= (ComputeShader &&) = default
Move assignment operator.
ComputeShader & operator= (const ComputeShader &) = default
Copy assignment operator.
~ComputeShader ()
Destructor (releases compute shader object).

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

Type Name
std::string GetAAShaderName (RenderConfigs * config)
Returns shader name for anti-aliasing based on config.
std::string GetAOShaderName (RenderConfigs * config)
Returns shader name for ambient occlusion based on config.
std::string GetSSRShaderName (RenderConfigs * config)
Returns shader name for SSR based on config.
std::string GetShadowShaderName (char _type, char _light_type)
Returns shader name for shadow rendering.
ComputeShader & ImportShader (std::string _name)
Imports or retrieves a cached compute shader.
ComputeShader & ImportShader (std::string _name, const Tuples &... args)
Imports or retrieves a cached compute shader with default uniforms.
void ImportShaderConfigs (std::string _name, const Tuples &... args)
Stores default uniform configs for a shader.
std::shared_ptr< ComputeShader > ImportShaderSrc (std::string _name)
Imports compute shader and returns shared_ptr.
std::shared_ptr< ComputeShader > ImportShaderSrc (std::string _name, const Tuples &... args)
Imports compute shader with default uniforms and returns shared_ptr.
std::shared_ptr< ComputeShader > ImportShaderSrc (std::string _name, const Tuples &... args)
void InitComputeLib (RenderConfigs * config)
Initializes compute shader library with default shaders.
void ResetComputeLib ()
Resets compute shader library and clears cache.

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

ComputeShader provides GPU compute capabilities for tasks like image processing, post-processing effects, physics simulation, and data transformation. Supports global caching for efficient reuse and default uniform configurations.

Usage:

// Import cached shader with default uniforms
auto& shader = ComputeShader::ImportShader("ssao_comp", 
    Uni("u_radius", 0.5f), 
    Uni("u_samples", 32));

// Bind textures, set uniforms, and dispatch
shader.UseShader();
shader.SetValue("u_input", 0);
shader.RunComputeShaderSCR(viewport_size, 16);  // 16x16 workgroups
shader.UnuseShader();

Note:

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

Note:

Workgroup size: Default 16x16, configurable via local_size_x/y/z in GLSL

Public Attributes Documentation

variable comp_shader

Compute shader unit.

ShaderUnit ComputeShader::comp_shader;


Public Functions Documentation

function CompileShader

Compiles the compute shader (unused in ComputeShader ).

inline virtual GLuint ComputeShader::CompileShader (
    ShaderType tar=NONE_SHADER
) override

Returns:

Always returns 0

Implements Shaders::CompileShader


function ComputeShader [1/3]

Constructs a compute shader from file.

ComputeShader::ComputeShader (
    const std::string & name
) 

Parameters:

  • name Compute shader file name (without extension)

function ComputeShader [2/3]

Constructs a compute shader with default uniform values.

template<class... Tuples>
ComputeShader::ComputeShader (
    const std::string & name,
    const Tuples &... args
) 

Template parameters:

  • Tuples Variadic template for uniform tuples (name, value)

Parameters:

  • name Compute shader file name
  • args Uniform tuples created with Uni() macro

Note:

Automatically sets uniforms after construction


function ComputeShader [3/3]

Default constructor (creates empty shader).

ComputeShader::ComputeShader () 


function CreateShader

Creates compute shader program from file.

void ComputeShader::CreateShader (
    const std::string & compShader
) 

Parameters:

  • compShader Compute shader file name

Note:

Loads, compiles, and links the shader


function GenerateShader

Generates shader code (unused in ComputeShader ).

inline virtual void ComputeShader::GenerateShader (
    ShaderType tar=NONE_SHADER
) override

Implements Shaders::GenerateShader


function GetAllShaderTypes

Returns list of shader stages.

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

Returns:

Vector containing only COMPUTE_SHADER

Implements Shaders::GetAllShaderTypes


function GetShaderID

Returns OpenGL shader ID for compute stage.

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

Parameters:

  • type Shader type (ignored)

Returns:

Compute shader object ID

Implements Shaders::GetShaderID


function GetShaderUnit

Returns pointer to compute shader unit.

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

Parameters:

  • tar Shader type (ignored)

Returns:

Pointer to comp_shader

Implements Shaders::GetShaderUnit


function LocalDebug

Prints debug information about compute shader.

virtual void ComputeShader::LocalDebug () override const

Implements Shaders::LocalDebug


function ParseShaderCode

Parses raw GLSL code for compute shader.

virtual void ComputeShader::ParseShaderCode (
    const std::string & _code,
    ShaderType tar
) override

Parameters:

  • _code GLSL compute shader source code
  • tar Shader type (must be COMPUTE_SHADER)

Implements Shaders::ParseShaderCode


function RelinkShader

Relinks the compute shader program.

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

Parameters:

  • tar Shader type (ignored)

Implements Shaders::RelinkShader


function ResetDefult

Resets shader uniforms to default values from config.

void ComputeShader::ResetDefult (
    std::string name
) 

Parameters:

  • name Shader name (must match config_list entry)

function ResetID

Resets compute shader ID.

virtual void ComputeShader::ResetID (
    ShaderType tar,
    GLuint _id
) override

Parameters:

  • tar Shader type (must be COMPUTE_SHADER)
  • _id New OpenGL shader object ID

Implements Shaders::ResetID


function RunComputeShader [1/2]

Dispatches compute shader with explicit size.

void ComputeShader::RunComputeShader (
    const glm::vec2 & _size
) 

Parameters:

  • _size Dispatch size (workgroups will be calculated)

Note:

Assumes 16x16 workgroup size


function RunComputeShader [2/2]

Dispatches compute shader with explicit workgroup counts.

void ComputeShader::RunComputeShader (
    GLuint workgroup_count_x=1,
    GLuint workgroup_count_y=1,
    GLuint workgroup_count_z=1
) const

Direct dispatch with specified workgroup counts in each dimension.

Parameters:

  • workgroup_count_x Number of workgroups in X dimension
  • workgroup_count_y Number of workgroups in Y dimension
  • workgroup_count_z Number of workgroups in Z dimension (default 1)

Note:

Total threads = workgroup_count * local_size (from GLSL)


function RunComputeShaderSCR

Dispatches compute shader scaled to screen resolution.

void ComputeShader::RunComputeShaderSCR (
    const glm::vec2 & _scr_size,
    GLuint _batch,
    bool _edge_fix=true
) 

Automatically calculates workgroup count based on screen size and batch size. Handles edge cases with optional padding.

Parameters:

  • _scr_size Screen size (width, height)
  • _batch Workgroup size (e.g., 16 for 16x16 workgroups)
  • _edge_fix If true, adds padding for non-divisible resolutions

Note:

Common batch sizes: 8, 16, 32 (must match GLSL local_size_x/y)


function operator=

Move assignment operator.

ComputeShader & ComputeShader::operator= (
    ComputeShader &&
) = default


function operator=

Copy assignment operator.

ComputeShader & ComputeShader::operator= (
    const ComputeShader &
) = default


function ~ComputeShader

Destructor (releases compute shader object).

ComputeShader::~ComputeShader () 


Public Static Functions Documentation

function GetAAShaderName

Returns shader name for anti-aliasing based on config.

static std::string ComputeShader::GetAAShaderName (
    RenderConfigs * config
) 

Parameters:

  • config Render configuration

Returns:

Shader name string (e.g., "fxaa_comp", "smaa_comp")


function GetAOShaderName

Returns shader name for ambient occlusion based on config.

static std::string ComputeShader::GetAOShaderName (
    RenderConfigs * config
) 

Parameters:

  • config Render configuration

Returns:

Shader name string (e.g., "ssao_comp", "gtao_comp")


function GetSSRShaderName

Returns shader name for SSR based on config.

static std::string ComputeShader::GetSSRShaderName (
    RenderConfigs * config
) 

Parameters:

  • config Render configuration

Returns:

Shader name string (e.g., "ssr_comp", "ssr_hq_comp")


function GetShadowShaderName

Returns shader name for shadow rendering.

static std::string ComputeShader::GetShadowShaderName (
    char _type,
    char _light_type
) 

Parameters:

  • _type Shadow type (hard, soft, contact, etc.)
  • _light_type Light type (point, sun, spot, area)

Returns:

Shader name string


function ImportShader [1/2]

Imports or retrieves a cached compute shader.

static ComputeShader & ComputeShader::ImportShader (
    std::string _name
) 

Parameters:

  • _name Compute shader file name

Returns:

Reference to cached ComputeShader instance

Note:

If shader exists in comp_list, returns cached instance


function ImportShader [2/2]

Imports or retrieves a cached compute shader with default uniforms.

template<class... Tuples>
static ComputeShader & ComputeShader::ImportShader (
    std::string _name,
    const Tuples &... args
) 

Template parameters:

  • Tuples Variadic template for uniform tuples

Parameters:

  • _name Compute shader file name
  • args Uniform tuples created with Uni() macro

Returns:

Reference to cached ComputeShader instance


function ImportShaderConfigs

Stores default uniform configs for a shader.

template<class... Tuples>
static void ComputeShader::ImportShaderConfigs (
    std::string _name,
    const Tuples &... args
) 

Template parameters:

  • Tuples Variadic template for uniform tuples

Parameters:

  • _name Shader name
  • args Uniform tuples

function ImportShaderSrc [1/3]

Imports compute shader and returns shared_ptr.

static std::shared_ptr< ComputeShader > ComputeShader::ImportShaderSrc (
    std::string _name
) 

Parameters:

  • _name Compute shader file name

Returns:

Shared pointer to ComputeShader


function ImportShaderSrc [2/3]

Imports compute shader with default uniforms and returns shared_ptr.

template<class... Tuples>
static std::shared_ptr< ComputeShader > ComputeShader::ImportShaderSrc (
    std::string _name,
    const Tuples &... args
) 

Template parameters:

  • Tuples Variadic template for uniform tuples

Parameters:

  • _name Compute shader file name
  • args Uniform tuples

Returns:

Shared pointer to ComputeShader


function ImportShaderSrc [2/3]

template<class... Tuples>
static std::shared_ptr< ComputeShader > ComputeShader::ImportShaderSrc (
    std::string _name,
    const Tuples &... args
) 

function InitComputeLib

Initializes compute shader library with default shaders.

static void ComputeShader::InitComputeLib (
    RenderConfigs * config
) 

Parameters:

  • config Render configuration for shader variant selection

Note:

Loads all post-processing and shadow compute shaders


function ResetComputeLib

Resets compute shader library and clears cache.

static void ComputeShader::ResetComputeLib () 

Note:

Releases all cached compute shaders



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