$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
shader.h File Reference

High-level OpenGL shader management with metadata and uniform caching. More...

#include "gl_common.h"
#include <stdbool.h>
Include dependency graph for shader.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  UniformEntry
 Cached uniform metadata for fast lookup. More...
 
struct  Shader
 Wrapper for an OpenGL program with uniform caching and automatic cleanup. More...
 

Macros

#define SHADER_SAFE_DESTROY(s)
 Idempotent shader destruction macro. Sets the pointer to NULL after calling shader_destroy. More...
 

Enumerations

enum  { SHADER_WARNING_THROTTLE_LIMIT = 10 }
 

Functions

GLuint shader_compile (const char *path, GLenum type)
 Compiles a single shader stage from a file. More...
 
char * shader_read_file (const char *path)
 Reads a shader file into RAM, processing all includes. More...
 
char * shader_read_file_with_defines (const char *path, const char **defines, int count)
 Reads a shader file and injects defines. More...
 
GLuint shader_load_program (const char *vertex_path, const char *fragment_path)
 Helper to load a classic Vertex+Fragment program from disk. More...
 
GLuint shader_load_compute (const char *compute_path)
 Helper to load a compute program from disk. More...
 
Shadershader_load (const char *vertex_path, const char *fragment_path)
 Loads a linked program and caches all its active uniforms. More...
 
Shadershader_load_compute_program (const char *compute_path)
 Loads a compute program and caches its uniforms. More...
 
Shadershader_load_with_defines (const char *vertex_path, const char *fragment_path, const char **defines, int count)
 Special loader that injects #define directives before compilation. More...
 
void shader_destroy (Shader *shader)
 Destroys the shader wrapper and deletes the GL program. More...
 
void shader_use (Shader *shader)
 Activates the program for subsequent draw calls (glUseProgram). More...
 
GLint shader_get_uniform_location (Shader *shader, const char *name)
 Retrieves a uniform location via binary search on the cache. More...
 
void shader_set_int (Shader *shader, const char *name, int val)
 
void shader_set_float (Shader *shader, const char *name, float val)
 
void shader_set_vec2 (Shader *shader, const char *name, const float *val)
 
void shader_set_vec3 (Shader *shader, const char *name, const float *val)
 
void shader_set_vec4 (Shader *shader, const char *name, const float *val)
 
void shader_set_mat4 (Shader *shader, const char *name, const float *val)
 
void shader_set_int_loc (GLint loc, int val)
 
void shader_set_float_loc (GLint loc, float val)
 
void shader_set_vec2_loc (GLint loc, const float *val)
 
void shader_set_vec3_loc (GLint loc, const float *val)
 
void shader_set_vec4_loc (GLint loc, const float *val)
 
void shader_set_mat4_loc (GLint loc, const float *val)
 

Detailed Description

High-level OpenGL shader management with metadata and uniform caching.

Macro Definition Documentation

◆ SHADER_SAFE_DESTROY

#define SHADER_SAFE_DESTROY (   s)
Value:
do { \
if ((s) != NULL) { \
shader_destroy(s); \
(s) = NULL; \
} \
} while (0)

Idempotent shader destruction macro. Sets the pointer to NULL after calling shader_destroy.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
SHADER_WARNING_THROTTLE_LIMIT 

Function Documentation

◆ shader_compile()

GLuint shader_compile ( const char *  path,
GLenum  type 
)

Compiles a single shader stage from a file.

Supports recursive @header inclusion syntax.

Parameters
pathPath to the shader source file.
typeGL_VERTEX_SHADER, GL_FRAGMENT_SHADER, or GL_COMPUTE_SHADER.
Returns
GLuint handle of the compiled shader stage.
Here is the call graph for this function:

◆ shader_destroy()

void shader_destroy ( Shader shader)

Destroys the shader wrapper and deletes the GL program.

Parameters
shaderPointer to the object to free.

◆ shader_get_uniform_location()

GLint shader_get_uniform_location ( Shader shader,
const char *  name 
)

Retrieves a uniform location via binary search on the cache.

Parameters
shaderPointer to the wrapper.
nameUniform identifier.
Returns
GL location, or -1 if not found.
Here is the call graph for this function:

◆ shader_load()

Shader* shader_load ( const char *  vertex_path,
const char *  fragment_path 
)

Loads a linked program and caches all its active uniforms.

Parameters
vertex_pathPath to vertex source.
fragment_pathPath to fragment source.
Returns
Pointer to the Shader object.
Here is the call graph for this function:

◆ shader_load_compute()

GLuint shader_load_compute ( const char *  compute_path)

Helper to load a compute program from disk.

Returns
GLuint program handle.
Here is the call graph for this function:

◆ shader_load_compute_program()

Shader* shader_load_compute_program ( const char *  compute_path)

Loads a compute program and caches its uniforms.

Parameters
compute_pathPath to compute source.
Returns
Pointer to the Shader object.
Here is the call graph for this function:

◆ shader_load_program()

GLuint shader_load_program ( const char *  vertex_path,
const char *  fragment_path 
)

Helper to load a classic Vertex+Fragment program from disk.

Returns
GLuint program handle.
Here is the call graph for this function:

◆ shader_load_with_defines()

Shader* shader_load_with_defines ( const char *  vertex_path,
const char *  fragment_path,
const char **  defines,
int  count 
)

Special loader that injects #define directives before compilation.

Parameters
vertex_pathVertex source.
fragment_pathFragment source.
definesArray of macro strings (e.g. "BLOOM_ENABLED").
countNumber of macros.
Returns
Specialized Shader object.
Here is the call graph for this function:

◆ shader_read_file()

char* shader_read_file ( const char *  path)

Reads a shader file into RAM, processing all includes.

Parameters
pathAbsolute or relative path.
Returns
Heap-allocated null-terminated string. Result must be freed.
Here is the call graph for this function:

◆ shader_read_file_with_defines()

char* shader_read_file_with_defines ( const char *  path,
const char **  defines,
int  count 
)

Reads a shader file and injects defines.

Parameters
pathPath to source.
definesArray of macro strings.
countNumber of macros.
Returns
Heap-allocated result.
Here is the call graph for this function:

◆ shader_set_float()

void shader_set_float ( Shader shader,
const char *  name,
float  val 
)
Here is the call graph for this function:

◆ shader_set_float_loc()

void shader_set_float_loc ( GLint  loc,
float  val 
)

◆ shader_set_int()

void shader_set_int ( Shader shader,
const char *  name,
int  val 
)
Here is the call graph for this function:

◆ shader_set_int_loc()

void shader_set_int_loc ( GLint  loc,
int  val 
)

◆ shader_set_mat4()

void shader_set_mat4 ( Shader shader,
const char *  name,
const float *  val 
)
Here is the call graph for this function:

◆ shader_set_mat4_loc()

void shader_set_mat4_loc ( GLint  loc,
const float *  val 
)

◆ shader_set_vec2()

void shader_set_vec2 ( Shader shader,
const char *  name,
const float *  val 
)
Here is the call graph for this function:

◆ shader_set_vec2_loc()

void shader_set_vec2_loc ( GLint  loc,
const float *  val 
)

◆ shader_set_vec3()

void shader_set_vec3 ( Shader shader,
const char *  name,
const float *  val 
)
Here is the call graph for this function:

◆ shader_set_vec3_loc()

void shader_set_vec3_loc ( GLint  loc,
const float *  val 
)

◆ shader_set_vec4()

void shader_set_vec4 ( Shader shader,
const char *  name,
const float *  val 
)
Here is the call graph for this function:

◆ shader_set_vec4_loc()

void shader_set_vec4_loc ( GLint  loc,
const float *  val 
)

◆ shader_use()

void shader_use ( Shader shader)

Activates the program for subsequent draw calls (glUseProgram).

Parameters
shaderPointer to the wrapper.