$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
shader.c File Reference
#include "shader.h"
#include "app_settings.h"
#include "glad/glad.h"
#include "io.h"
#include "log.h"
#include "utils.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for shader.c:

Data Structures

struct  LoadedBuffer
 
struct  Chunk
 
struct  IncludeContext
 

Macros

#define CLEANUP_CTX   __attribute__((cleanup(ctx_free)))
 

Enumerations

enum  { INFO_LOG_SIZE = 512 }
 
enum  { MAX_SHADER_NAME_LEN = 256 }
 
enum  { SHADER_LABEL_BUFFER_SIZE = 512 }
 
enum  { MAX_INCLUDE_DEPTH = 16 }
 
enum  { MAX_SHADER_SOURCE_SIZE = 16 * 1024 * 1024 }
 
enum  { PATH_BUFFER_SIZE = 256 }
 
enum  { RESOLVED_PATH_BUFFER_SIZE = 512 }
 
enum  { HEADER_TAG_LEN = 7 }
 
enum  { MAX_DEFINES = 32 }
 

Functions

static bool process_source (IncludeContext *ctx, const char *current_file_src, const char *current_file_path)
 
static void ctx_add_buffer (IncludeContext *ctx, char *data)
 
static bool ctx_add_chunk (IncludeContext *ctx, const char *ptr, size_t len)
 
static void ctx_free (IncludeContext *ctx)
 
static bool get_dir_from_path (const char *path, char *out_dir, size_t size)
 
static bool resolve_and_parse_include (IncludeContext *ctx, const char *path_term, const char *current_file_path)
 
static const char * parse_include_path (const char *args, char *out_path, size_t size)
 
static char * inject_defines_into_source (const char *buffer, size_t file_size, const char **defines, int count)
 
char * shader_read_file_with_defines (const char *path, const char **defines, int count)
 Reads a shader file and injects defines. More...
 
char * shader_read_file (const char *path)
 Reads a shader file into RAM, processing all includes. More...
 
static GLuint shader_compile_with_defines (const char *path, GLenum type, const char **defines, int count)
 
GLuint shader_compile (const char *path, GLenum type)
 Compiles a single shader stage from a file. More...
 
static GLuint shader_load_program_with_defines (const char *vertex_path, const char *fragment_path, const char **defines, int count)
 
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...
 
static int cmp_uniform_entry (const void *lhs, const void *rhs)
 
static void shader_cache_uniforms (Shader *shader)
 
static Shadershader_create_from_program (GLuint program, const char *name)
 
Shadershader_load (const char *vertex_path, const char *fragment_path)
 Loads a linked program and caches all its active 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...
 
Shadershader_load_compute_program (const char *compute_path)
 Loads a compute program and caches its uniforms. 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)
 

Macro Definition Documentation

◆ CLEANUP_CTX

#define CLEANUP_CTX   __attribute__((cleanup(ctx_free)))

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
INFO_LOG_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
MAX_SHADER_NAME_LEN 

◆ anonymous enum

anonymous enum
Enumerator
SHADER_LABEL_BUFFER_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
MAX_INCLUDE_DEPTH 

◆ anonymous enum

anonymous enum
Enumerator
MAX_SHADER_SOURCE_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
PATH_BUFFER_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
RESOLVED_PATH_BUFFER_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
HEADER_TAG_LEN 

◆ anonymous enum

anonymous enum
Enumerator
MAX_DEFINES 

Function Documentation

◆ cmp_uniform_entry()

static int cmp_uniform_entry ( const void *  lhs,
const void *  rhs 
)
static

◆ ctx_add_buffer()

static void ctx_add_buffer ( IncludeContext ctx,
char *  data 
)
static

◆ ctx_add_chunk()

static bool ctx_add_chunk ( IncludeContext ctx,
const char *  ptr,
size_t  len 
)
static

◆ ctx_free()

static void ctx_free ( IncludeContext ctx)
static

◆ get_dir_from_path()

static bool get_dir_from_path ( const char *  path,
char *  out_dir,
size_t  size 
)
static
Here is the call graph for this function:

◆ inject_defines_into_source()

static char* inject_defines_into_source ( const char *  buffer,
size_t  file_size,
const char **  defines,
int  count 
)
static
Here is the call graph for this function:

◆ parse_include_path()

static const char* parse_include_path ( const char *  args,
char *  out_path,
size_t  size 
)
static
Here is the call graph for this function:

◆ process_source()

static bool process_source ( IncludeContext ctx,
const char *  current_file_src,
const char *  current_file_path 
)
static
Here is the call graph for this function:

◆ resolve_and_parse_include()

static bool resolve_and_parse_include ( IncludeContext ctx,
const char *  path_term,
const char *  current_file_path 
)
static
Here is the call graph for this function:

◆ shader_cache_uniforms()

static void shader_cache_uniforms ( Shader shader)
static
Here is the call graph for this function:

◆ 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_compile_with_defines()

static GLuint shader_compile_with_defines ( const char *  path,
GLenum  type,
const char **  defines,
int  count 
)
static
Here is the call graph for this function:

◆ shader_create_from_program()

static Shader* shader_create_from_program ( GLuint  program,
const char *  name 
)
static
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_program_with_defines()

static GLuint shader_load_program_with_defines ( const char *  vertex_path,
const char *  fragment_path,
const char **  defines,
int  count 
)
static
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.