Shader
class · #include <rayrai/shaders/Shader.hpp>
Minimal GLSL shader wrapper.
Nested classes
structBinaryCacheStats — Counters reported by Shader::binaryCacheStats().
Constructors & destructor
Shader
Shader()Construct an empty shader program wrapper.
Shader(const Shader &) = deleteShader(Shader &&other) noexcept~Shader
~Shader()Public member functions
operator=
Shader &operator=(const Shader &) = deleteShader &operator=(Shader &&other) noexceptcompile
bool compile(const char *vertexSource, const char *fragmentSource)Compiles the shader from vertex and fragment source code and links them.
Parameters
| Parameter | Description |
|---|---|
vertexSource | The source code for the vertex shader. |
fragmentSource | The 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
bool compileCompute(const char *computeSource)Compiles a compute shader program.
Parameters
| Parameter | Description |
|---|---|
computeSource | The source code for the compute shader. |
Returns true on success; false if compile/link failed (previous program, if any, is preserved).
use
void use()Activate the shader program for rendering.
setBool
void setBool(std::string_view name, bool value) constSet a boolean uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
value | Boolean value. |
void setBool(int location, bool value) constsetInt
void setInt(std::string_view name, int value) constSet an integer uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
value | Integer value. |
void setInt(int location, int value) constsetFloat
void setFloat(std::string_view name, float value) constSet a float uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
value | Float value. |
void setFloat(int location, float value) constsetVec2
void setVec2(std::string_view name, const glm::vec2 &value) constSet a vec2 uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
value | Vector value. |
void setVec2(int location, const glm::vec2 &value) constvoid setVec2(std::string_view name, float x, float y) constSet a vec2 uniform from components.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
x | X component. |
y | Y component. |
void setVec2(int location, float x, float y) constsetVec3
void setVec3(std::string_view name, const glm::vec3 &value) constSet a vec3 uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
value | Vector value. |
void setVec3(int location, const glm::vec3 &value) constvoid setVec3(std::string_view name, float x, float y, float z) constSet a vec3 uniform from components.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
x | X component. |
y | Y component. |
z | Z component. |
void setVec3(int location, float x, float y, float z) constsetVec4
void setVec4(std::string_view name, const glm::vec4 &value) constSet a vec4 uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
value | Vector value. |
void setVec4(int location, const glm::vec4 &value) constvoid setVec4(
std::string_view name,
float x,
float y,
float z,
float w) constSet a vec4 uniform from components.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
x | X component. |
y | Y component. |
z | Z component. |
w | W component. |
void setVec4(int location, float x, float y, float z, float w) constsetMat2
void setMat2(std::string_view name, const glm::mat2 &mat) constSet a mat2 uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
mat | Matrix value. |
void setMat2(int location, const glm::mat2 &mat) constsetMat3
void setMat3(std::string_view name, const glm::mat3 &mat) constSet a mat3 uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
mat | Matrix value. |
void setMat3(int location, const glm::mat3 &mat) constsetMat4
void setMat4(std::string_view name, const glm::mat4 &mat) constSet a mat4 uniform.
Parameters
| Parameter | Description |
|---|---|
name | Uniform name. |
mat | Matrix value. |
void setMat4(int location, const glm::mat4 &mat) constcachedUniformLocation
int cachedUniformLocation(std::string_view name) constcacheIdentity
uint64_t cacheIdentity() constIdentity 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
uint64_t uniformGeneration() constuniformCache
template <typename T>
T &uniformCache() constPer-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
static void invalidateBindingCache()Call after restoring/changing GL_CURRENT_PROGRAM through raw OpenGL.
setCompileThreadCount static
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
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
static void resetBinaryCacheStats()Reset the cache hit/miss/store counters to zero.
setBinaryCacheConfig static
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
| Parameter | Description |
|---|---|
enabled | False to bypass the cache entirely. |
directory | Explicit cache directory. Empty for auto. |
logOperations | True to log hits/misses/stores to stderr. |
Public attributes
ID
unsigned int IDOpenGL program id.
cacheIdentity_
uint64_t cacheIdentity_Bumped on every successful link; see cacheIdentity().