Skip to content

Shader ​

class · #include <rayrai/shaders/Shader.hpp>

Minimal GLSL shader wrapper.

Nested classes ​

Constructors & destructor ​

Shader ​

cpp
Shader()

Construct an empty shader program wrapper.

cpp
Shader(const Shader &) = delete
cpp
Shader(Shader &&other) noexcept

~Shader ​

cpp
~Shader()

Public member functions ​

operator= ​

cpp
Shader &operator=(const Shader &) = delete
cpp
Shader &operator=(Shader &&other) noexcept

compile ​

cpp
bool compile(const char *vertexSource, const char *fragmentSource)

Compiles the shader from vertex and fragment source code and links them.

Parameters

ParameterDescription
vertexSourceThe source code for the vertex shader.
fragmentSourceThe source code for the fragment shader.

If this Shader already holds a compiled program, the prior program is kept until the new one compiles and links successfully (two-phase replacement), so a failed compile leaves the last known-good program bound rather than a dead/zero program.

Returns true if a valid program is now active; false if compile/link failed (in which case the previous program, if any, is preserved).

compileCompute ​

cpp
bool compileCompute(const char *computeSource)

Compiles a compute shader program.

Parameters

ParameterDescription
computeSourceThe source code for the compute shader.

Returns true on success; false if compile/link failed (previous program, if any, is preserved).

use ​

cpp
void use()

Activate the shader program for rendering.

setBool ​

cpp
void setBool(std::string_view name, bool value) const

Set a boolean uniform.

Parameters

ParameterDescription
nameUniform name.
valueBoolean value.
cpp
void setBool(int location, bool value) const

setInt ​

cpp
void setInt(std::string_view name, int value) const

Set an integer uniform.

Parameters

ParameterDescription
nameUniform name.
valueInteger value.
cpp
void setInt(int location, int value) const

setFloat ​

cpp
void setFloat(std::string_view name, float value) const

Set a float uniform.

Parameters

ParameterDescription
nameUniform name.
valueFloat value.
cpp
void setFloat(int location, float value) const

setVec2 ​

cpp
void setVec2(std::string_view name, const glm::vec2 &value) const

Set a vec2 uniform.

Parameters

ParameterDescription
nameUniform name.
valueVector value.
cpp
void setVec2(int location, const glm::vec2 &value) const
cpp
void setVec2(std::string_view name, float x, float y) const

Set a vec2 uniform from components.

Parameters

ParameterDescription
nameUniform name.
xX component.
yY component.
cpp
void setVec2(int location, float x, float y) const

setVec3 ​

cpp
void setVec3(std::string_view name, const glm::vec3 &value) const

Set a vec3 uniform.

Parameters

ParameterDescription
nameUniform name.
valueVector value.
cpp
void setVec3(int location, const glm::vec3 &value) const
cpp
void setVec3(std::string_view name, float x, float y, float z) const

Set a vec3 uniform from components.

Parameters

ParameterDescription
nameUniform name.
xX component.
yY component.
zZ component.
cpp
void setVec3(int location, float x, float y, float z) const

setVec4 ​

cpp
void setVec4(std::string_view name, const glm::vec4 &value) const

Set a vec4 uniform.

Parameters

ParameterDescription
nameUniform name.
valueVector value.
cpp
void setVec4(int location, const glm::vec4 &value) const
cpp
void setVec4(
    std::string_view name,
    float x,
    float y,
    float z,
    float w) const

Set a vec4 uniform from components.

Parameters

ParameterDescription
nameUniform name.
xX component.
yY component.
zZ component.
wW component.
cpp
void setVec4(int location, float x, float y, float z, float w) const

setMat2 ​

cpp
void setMat2(std::string_view name, const glm::mat2 &mat) const

Set a mat2 uniform.

Parameters

ParameterDescription
nameUniform name.
matMatrix value.
cpp
void setMat2(int location, const glm::mat2 &mat) const

setMat3 ​

cpp
void setMat3(std::string_view name, const glm::mat3 &mat) const

Set a mat3 uniform.

Parameters

ParameterDescription
nameUniform name.
matMatrix value.
cpp
void setMat3(int location, const glm::mat3 &mat) const

setMat4 ​

cpp
void setMat4(std::string_view name, const glm::mat4 &mat) const

Set a mat4 uniform.

Parameters

ParameterDescription
nameUniform name.
matMatrix value.
cpp
void setMat4(int location, const glm::mat4 &mat) const

cachedUniformLocation ​

cpp
int cachedUniformLocation(std::string_view name) const

cacheIdentity ​

cpp
uint64_t cacheIdentity() const

Identity of the current linked program, for external uniform-location caches. Not the GL program name: drivers reuse the lowest free name, so a deleted Shader and its replacement can share both a heap address and an ID, which would let a stale location cache survive. This counter is unique for the process instead.

uniformGeneration ​

cpp
uint64_t uniformGeneration() const

uniformCache ​

cpp
template <typename T>
T &uniformCache() const

Per-program storage for a caller's resolved uniform locations.

A uniform location is a property of the linked program, not of whichever thread happens to be drawing with it, so the cache belongs here. Each distinct T gets its own slot, reached by index rather than by hashing a Shader pointer, and the storage dies with this Shader — so a later Shader at the same heap address cannot inherit stale locations.

T must be default-constructible and is expected to carry the cacheIdentity() it was resolved against, since a relink moves that.

Static member functions ​

invalidateBindingCache static ​

cpp
static void invalidateBindingCache()

Call after restoring/changing GL_CURRENT_PROGRAM through raw OpenGL.

setCompileThreadCount static ​

cpp
static void setCompileThreadCount(unsigned int threadCount)

Configure the driver-side shader compile thread budget.

Passes threadCount to glMaxShaderCompilerThreadsARB. The default (set by RayraiWindow's constructor) is 1, which keeps shader compilation single-threaded — matching rayrai's overall single-threaded default. A value greater than 1 lets the driver compile vertex/fragment stages and multiple programs concurrently on its own thread pool; useful only when the host application opts into multi-threading.

Must be called on the thread that owns the GL context, before the first shader compile; it applies the count to that context at once (other contexts get it before their first compile). Subsequent calls update the driver hint; programs already compiled are unaffected. No-op on drivers without KHR/ARB_parallel_shader_compile.

binaryCacheStats static ​

cpp
static BinaryCacheStats binaryCacheStats()

Read the current hit/miss/store counters.

Useful in tests that need to verify a second run actually picks the cached binary rather than recompiling. Counters are process-global; use resetBinaryCacheStats to scope a measurement window.

resetBinaryCacheStats static ​

cpp
static void resetBinaryCacheStats()

Reset the cache hit/miss/store counters to zero.

setBinaryCacheConfig static ​

cpp
static void setBinaryCacheConfig(
    bool enabled,
    std::string directory,
    bool logOperations)

Configure the persistent shader-binary cache.

Wired through RayraiWindow's constructor. When enabled is true and the GL driver advertises at least one program binary format, compiled programs are persisted under directory (or an auto-picked path under $XDG_CACHE_HOME/$HOME if directory is empty) keyed by GL vendor/renderer/version + GLSL source. Subsequent runs skip the GLSL compile entirely and load the cached binary.

Parameters

ParameterDescription
enabledFalse to bypass the cache entirely.
directoryExplicit cache directory. Empty for auto.
logOperationsTrue to log hits/misses/stores to stderr.

Public attributes ​

ID ​

cpp
unsigned int ID

OpenGL program id.

cacheIdentity_ ​

cpp
uint64_t cacheIdentity_

Bumped on every successful link; see cacheIdentity().

RaiSim — physics for robotics and AI.