Kohi Game Engine
shader_system.h File Reference

A system to manage shaders. Respondible for working with the renderer to create, destroy, bind/unbind and set shader properties such as uniforms. More...

Go to the source code of this file.

Data Structures

struct  shader_system_config
 Configuration for the shader system. More...
 
struct  shader_uniform
 Represents a single entry in the internal uniform array. More...
 
struct  shader_attribute
 Represents a single shader vertex attribute. More...
 
struct  shader
 Represents a shader on the frontend. More...
 

Typedefs

typedef struct shader_system_config shader_system_config
 Configuration for the shader system. More...
 
typedef enum shader_state shader_state
 Represents the current state of a given shader. More...
 
typedef struct shader_uniform shader_uniform
 Represents a single entry in the internal uniform array. More...
 
typedef struct shader_attribute shader_attribute
 Represents a single shader vertex attribute. More...
 
typedef enum shader_flags shader_flags
 
typedef u32 shader_flag_bits
 
typedef struct shader shader
 Represents a shader on the frontend. More...
 

Enumerations

enum  shader_state { SHADER_STATE_NOT_CREATED , SHADER_STATE_UNINITIALIZED , SHADER_STATE_INITIALIZED }
 Represents the current state of a given shader. More...
 
enum  shader_flags { SHADER_FLAG_NONE = 0x0 , SHADER_FLAG_DEPTH_TEST = 0x1 , SHADER_FLAG_DEPTH_WRITE = 0x2 , SHADER_FLAG_WIREFRAME = 0x4 }
 

Functions

b8 shader_system_initialize (u64 *memory_requirement, void *memory, void *config)
 Initializes the shader system using the supplied configuration. NOTE: Call this twice, once to obtain memory requirement (memory = 0) and a second time including allocated memory. More...
 
void shader_system_shutdown (void *state)
 Shuts down the shader system. More...
 
KAPI b8 shader_system_create (renderpass *pass, const shader_config *config)
 Creates a new shader with the given config. More...
 
KAPI u32 shader_system_get_id (const char *shader_name)
 Gets the identifier of a shader by name. More...
 
KAPI shadershader_system_get_by_id (u32 shader_id)
 Returns a pointer to a shader with the given identifier. More...
 
KAPI shadershader_system_get (const char *shader_name)
 Returns a pointer to a shader with the given name. More...
 
KAPI b8 shader_system_use (const char *shader_name)
 Uses the shader with the given name. More...
 
KAPI b8 shader_system_use_by_id (u32 shader_id)
 Uses the shader with the given identifier. More...
 
KAPI u16 shader_system_uniform_index (shader *s, const char *uniform_name)
 Returns the uniform index for a uniform with the given name, if found. More...
 
KAPI b8 shader_system_uniform_set (const char *uniform_name, const void *value)
 Sets the value of a uniform with the given name to the supplied value. NOTE: Operates against the currently-used shader. More...
 
KAPI b8 shader_system_sampler_set (const char *sampler_name, const texture *t)
 Sets the texture of a sampler with the given name to the supplied texture. NOTE: Operates against the currently-used shader. More...
 
KAPI b8 shader_system_uniform_set_by_index (u16 index, const void *value)
 Sets a uniform value by index. NOTE: Operates against the currently-used shader. More...
 
KAPI b8 shader_system_sampler_set_by_index (u16 index, const struct texture *t)
 Sets a sampler value by index. NOTE: Operates against the currently-used shader. More...
 
KAPI b8 shader_system_apply_global (b8 needs_update)
 Applies global-scoped uniforms. NOTE: Operates against the currently-used shader. More...
 
KAPI b8 shader_system_apply_instance (b8 needs_update)
 Applies instance-scoped uniforms. NOTE: Operates against the currently-used shader. More...
 
KAPI b8 shader_system_bind_instance (u32 instance_id)
 Binds the instance with the given id for use. Must be done before setting instance-scoped uniforms. NOTE: Operates against the currently-used shader. More...
 

Detailed Description

A system to manage shaders. Respondible for working with the renderer to create, destroy, bind/unbind and set shader properties such as uniforms.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Version
1.0
Date
2022-03-09

Typedef Documentation

◆ shader

typedef struct shader shader

Represents a shader on the frontend.

◆ shader_attribute

Represents a single shader vertex attribute.

◆ shader_flag_bits

◆ shader_flags

typedef enum shader_flags shader_flags

◆ shader_state

typedef enum shader_state shader_state

Represents the current state of a given shader.

◆ shader_system_config

Configuration for the shader system.

◆ shader_uniform

Represents a single entry in the internal uniform array.

Enumeration Type Documentation

◆ shader_flags

Enumerator
SHADER_FLAG_NONE 
SHADER_FLAG_DEPTH_TEST 
SHADER_FLAG_DEPTH_WRITE 
SHADER_FLAG_WIREFRAME 

◆ shader_state

Represents the current state of a given shader.

Enumerator
SHADER_STATE_NOT_CREATED 

The shader has not yet gone through the creation process, and is unusable.

SHADER_STATE_UNINITIALIZED 

The shader has gone through the creation process, but not initialization. It is unusable.

SHADER_STATE_INITIALIZED 

The shader is created and initialized, and is ready for use.

Function Documentation

◆ shader_system_apply_global()

KAPI b8 shader_system_apply_global ( b8  needs_update)

Applies global-scoped uniforms. NOTE: Operates against the currently-used shader.

Parameters
needs_updateIndicates if shader internals need to be updated, or just to be bound.
Returns
True on success; otherwise false.

◆ shader_system_apply_instance()

KAPI b8 shader_system_apply_instance ( b8  needs_update)

Applies instance-scoped uniforms. NOTE: Operates against the currently-used shader.

Parameters
needs_updateIndicates if the shader needs uniform updates or just needs to be bound.
needs_updateIndicates if shader internals need to be updated, or just to be bound.
Returns
True on success; otherwise false.

◆ shader_system_bind_instance()

KAPI b8 shader_system_bind_instance ( u32  instance_id)

Binds the instance with the given id for use. Must be done before setting instance-scoped uniforms. NOTE: Operates against the currently-used shader.

Parameters
instance_idThe identifier of the instance to bind.
Returns
True on success; otherwise false.

◆ shader_system_create()

KAPI b8 shader_system_create ( renderpass pass,
const shader_config config 
)

Creates a new shader with the given config.

Parameters
passA pointer to the renderpass to be used with this shader.
configThe configuration to be used when creating the shader.
Returns
True on success; otherwise false.

◆ shader_system_get()

KAPI shader* shader_system_get ( const char *  shader_name)

Returns a pointer to a shader with the given name.

Parameters
shader_nameThe name to search for. Case sensitive.
Returns
A pointer to a shader, if found; otherwise 0.

◆ shader_system_get_by_id()

KAPI shader* shader_system_get_by_id ( u32  shader_id)

Returns a pointer to a shader with the given identifier.

Parameters
shader_idThe shader identifier.
Returns
A pointer to a shader, if found; otherwise 0.

◆ shader_system_get_id()

KAPI u32 shader_system_get_id ( const char *  shader_name)

Gets the identifier of a shader by name.

Parameters
shader_nameThe name of the shader.
Returns
The shader id, if found; otherwise INVALID_ID.

◆ shader_system_initialize()

b8 shader_system_initialize ( u64 memory_requirement,
void *  memory,
void *  config 
)

Initializes the shader system using the supplied configuration. NOTE: Call this twice, once to obtain memory requirement (memory = 0) and a second time including allocated memory.

Parameters
memory_requirementA pointer to hold the memory requirement of this system in bytes.
memoryA memory block to be used to hold the state of this system. Pass 0 on the first call to get memory requirement.
configThe configuration (shader_system_config) to be used when initializing the system.
Returns
b8 True on success; otherwise false.

◆ shader_system_sampler_set()

KAPI b8 shader_system_sampler_set ( const char *  sampler_name,
const texture t 
)

Sets the texture of a sampler with the given name to the supplied texture. NOTE: Operates against the currently-used shader.

Parameters
uniform_nameThe name of the uniform to be set.
tA pointer to the texture to be set.
Returns
True on success; otherwise false.

◆ shader_system_sampler_set_by_index()

KAPI b8 shader_system_sampler_set_by_index ( u16  index,
const struct texture t 
)

Sets a sampler value by index. NOTE: Operates against the currently-used shader.

Parameters
indexThe index of the uniform.
valueA pointer to the texture to be set.
Returns
True on success; otherwise false.

◆ shader_system_shutdown()

void shader_system_shutdown ( void *  state)

Shuts down the shader system.

Parameters
stateA pointer to the system state.

◆ shader_system_uniform_index()

KAPI u16 shader_system_uniform_index ( shader s,
const char *  uniform_name 
)

Returns the uniform index for a uniform with the given name, if found.

Parameters
sA pointer to the shader to obtain the index from.
uniform_nameThe name of the uniform to search for.
Returns
The uniform index, if found; otherwise INVALID_ID_U16.

◆ shader_system_uniform_set()

KAPI b8 shader_system_uniform_set ( const char *  uniform_name,
const void *  value 
)

Sets the value of a uniform with the given name to the supplied value. NOTE: Operates against the currently-used shader.

Parameters
uniform_nameThe name of the uniform to be set.
valueThe value to be set.
Returns
True on success; otherwise false.

◆ shader_system_uniform_set_by_index()

KAPI b8 shader_system_uniform_set_by_index ( u16  index,
const void *  value 
)

Sets a uniform value by index. NOTE: Operates against the currently-used shader.

Parameters
indexThe index of the uniform.
valueThe value of the uniform.
Returns
True on success; otherwise false.

◆ shader_system_use()

KAPI b8 shader_system_use ( const char *  shader_name)

Uses the shader with the given name.

Parameters
shader_nameThe name of the shader to use. Case sensitive.
Returns
True on success; otherwise false.

◆ shader_system_use_by_id()

KAPI b8 shader_system_use_by_id ( u32  shader_id)

Uses the shader with the given identifier.

Parameters
shader_idThe identifier of the shader to be used.
Returns
True on success; otherwise false.