Kohi Game Engine
material_system.h File Reference

The material system is responsible for managing materials in the engine, including reference counting and auto-unloading. More...

#include "defines.h"
#include "resources/resource_types.h"

Go to the source code of this file.

Data Structures

struct  material_system_config
 The configuration for the material system. More...
 

Macros

#define DEFAULT_MATERIAL_NAME   "default"
 The name of the default material. More...
 
#define DEFAULT_UI_MATERIAL_NAME   "default_ui"
 The name of the default UI material. More...
 
#define DEFAULT_TERRAIN_MATERIAL_NAME   "default_terrain"
 The name of the default terrain material. More...
 

Typedefs

typedef struct material_system_config material_system_config
 The configuration for the material system. More...
 

Functions

b8 material_system_initialize (u64 *memory_requirement, void *state, void *config)
 Initializes the material system. Should be called twice; once to get the memory requirement (passing state=0), and a second time passing an allocated block of memory to actually initialize the system. More...
 
void material_system_shutdown (void *state)
 Shuts down the material system. More...
 
KAPI materialmaterial_system_acquire (const char *name)
 Attempts to acquire a material with the given name. If it has not yet been loaded, this triggers it to load. If the material is not found, a pointer to the default material is returned. If the material is found and loaded, its reference counter is incremented. More...
 
KAPI materialmaterial_system_acquire_terrain_material (const char *material_name, u32 material_count, const char **material_names, b8 auto_release)
 Attempts to acquire a terrain material with the given name. If it has not yet been loaded, this triggers it to be loaded from using the provided standard material names. If the material is not able to be loaded, a pointer to the default terrain material is returned. If the material is found and loaded, its reference counter is incremented. More...
 
KAPI materialmaterial_system_acquire_from_config (material_config *config)
 Attempts to acquire a material from the given configuration. If it has not yet been loaded, this triggers it to load. If the material is not found, a pointer to the default material is returned. If the material is found and loaded, its reference counter is incremented. More...
 
KAPI void material_system_release (const char *name)
 Releases a material with the given name. Ignores non-existant materials. Decreases the reference counter by 1. If the reference counter reaches 0 and auto_release was set to true, the material is unloaded, releasing internal resources. More...
 
KAPI materialmaterial_system_get_default (void)
 Gets a pointer to the default material. Does not reference count. More...
 
KAPI materialmaterial_system_get_default_ui (void)
 Gets a pointer to the default UI material. Does not reference count. More...
 
KAPI materialmaterial_system_get_default_terrain (void)
 Gets a pointer to the default terrain material. Does not reference count. More...
 
KAPI b8 material_system_apply_global (u32 shader_id, const struct frame_data *p_frame_data, const mat4 *projection, const mat4 *view, const vec4 *ambient_colour, const vec3 *view_position, u32 render_mode)
 Applies global-level data for the material shader id. More...
 
KAPI b8 material_system_apply_instance (material *m, struct frame_data *p_frame_data, b8 needs_update)
 Applies instance-level material data for the given material. More...
 
KAPI b8 material_system_apply_local (material *m, const mat4 *model)
 Applies local-level material data (typically just model matrix). More...
 
KAPI void material_system_dump (void)
 Dumps all of the registered materials and their reference counts/handles. More...
 

Detailed Description

The material system is responsible for managing materials in the engine, including reference counting and auto-unloading.

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

Macro Definition Documentation

◆ DEFAULT_MATERIAL_NAME

#define DEFAULT_MATERIAL_NAME   "default"

The name of the default material.

◆ DEFAULT_TERRAIN_MATERIAL_NAME

#define DEFAULT_TERRAIN_MATERIAL_NAME   "default_terrain"

The name of the default terrain material.

◆ DEFAULT_UI_MATERIAL_NAME

#define DEFAULT_UI_MATERIAL_NAME   "default_ui"

The name of the default UI material.

Typedef Documentation

◆ material_system_config

The configuration for the material system.

Function Documentation

◆ material_system_acquire()

KAPI material* material_system_acquire ( const char *  name)

Attempts to acquire a material with the given name. If it has not yet been loaded, this triggers it to load. If the material is not found, a pointer to the default material is returned. If the material is found and loaded, its reference counter is incremented.

Parameters
nameThe name of the material to find.
Returns
A pointer to the loaded material. Can be a pointer to the default material if not found.

◆ material_system_acquire_from_config()

KAPI material* material_system_acquire_from_config ( material_config config)

Attempts to acquire a material from the given configuration. If it has not yet been loaded, this triggers it to load. If the material is not found, a pointer to the default material is returned. If the material is found and loaded, its reference counter is incremented.

Parameters
configThe config of the material to load.
Returns
A pointer to the loaded material.

◆ material_system_acquire_terrain_material()

KAPI material* material_system_acquire_terrain_material ( const char *  material_name,
u32  material_count,
const char **  material_names,
b8  auto_release 
)

Attempts to acquire a terrain material with the given name. If it has not yet been loaded, this triggers it to be loaded from using the provided standard material names. If the material is not able to be loaded, a pointer to the default terrain material is returned. If the material is found and loaded, its reference counter is incremented.

Parameters
nameThe name of the terrain material to find.
material_countThe number of standard source material names.
material_namesThe names of the source materials to be used.
Returns
A pointer to the loaded terrain material. Can be a pointer to the defualt terrain material if not found.

◆ material_system_apply_global()

KAPI b8 material_system_apply_global ( u32  shader_id,
const struct frame_data p_frame_data,
const mat4 projection,
const mat4 view,
const vec4 ambient_colour,
const vec3 view_position,
u32  render_mode 
)

Applies global-level data for the material shader id.

Parameters
shader_idThe identifier of the shader to apply globals for.
p_frame_dataA constant pointer to the current frame's data.
projectionA constant pointer to a projection matrix.
viewA constant pointer to a view matrix.
ambient_colourThe ambient colour of the scene.
view_positionThe camera position.
render_modeThe render mode.
Returns
True on success; otherwise false.

◆ material_system_apply_instance()

KAPI b8 material_system_apply_instance ( material m,
struct frame_data p_frame_data,
b8  needs_update 
)

Applies instance-level material data for the given material.

Parameters
mA pointer to the material to be applied.
p_frame_dataA pointer to the current frame's data.
needs_updateIndicates if material internals require updating, or if they should just be bound.
Returns
True on success; otherwise false.

◆ material_system_apply_local()

KAPI b8 material_system_apply_local ( material m,
const mat4 model 
)

Applies local-level material data (typically just model matrix).

Parameters
mA pointer to the material to be applied.
modelA constant pointer to the model matrix to be applied.
Returns
True on success; otherwise false.

◆ material_system_dump()

KAPI void material_system_dump ( void  )

Dumps all of the registered materials and their reference counts/handles.

◆ material_system_get_default()

KAPI material* material_system_get_default ( void  )

Gets a pointer to the default material. Does not reference count.

◆ material_system_get_default_terrain()

KAPI material* material_system_get_default_terrain ( void  )

Gets a pointer to the default terrain material. Does not reference count.

◆ material_system_get_default_ui()

KAPI material* material_system_get_default_ui ( void  )

Gets a pointer to the default UI material. Does not reference count.

◆ material_system_initialize()

b8 material_system_initialize ( u64 memory_requirement,
void *  state,
void *  config 
)

Initializes the material system. Should be called twice; once to get the memory requirement (passing state=0), and a second time passing an allocated block of memory to actually initialize the system.

Parameters
memory_requirementA pointer to hold the memory requirement as it is calculated.
stateA block of memory to hold the state or, if gathering the memory requirement, 0.
configThe configuration (material_system_config) for this system.
Returns
True on success; otherwise false.

◆ material_system_release()

KAPI void material_system_release ( const char *  name)

Releases a material with the given name. Ignores non-existant materials. Decreases the reference counter by 1. If the reference counter reaches 0 and auto_release was set to true, the material is unloaded, releasing internal resources.

Parameters
nameThe name of the material to unload.

◆ material_system_shutdown()

void material_system_shutdown ( void *  state)

Shuts down the material system.

Parameters
stateThe state block of memory.