Kohi Game Engine
material_system.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include "core_render_types.h"
16 #include "identifiers/khandle.h"
18 
19 #include <defines.h>
20 #include <strings/kname.h>
21 
22 #define MATERIAL_DEFAULT_NAME_STANDARD "Material.DefaultStandard"
23 #define MATERIAL_DEFAULT_NAME_WATER "Material.DefaultWater"
24 #define MATERIAL_DEFAULT_NAME_BLENDED "Material.DefaultBlended"
25 
26 #define MATERIAL_MAX_IRRADIANCE_CUBEMAP_COUNT 4
27 #define MATERIAL_MAX_SHADOW_CASCADES 4
28 #define MATERIAL_MAX_POINT_LIGHTS 10
29 #define MATERIAL_MAX_VIEWS 4
30 
31 #define MATERIAL_DEFAULT_BASE_COLOUR_VALUE (vec4){1.0f, 1.0f, 1.0f, 1.0f}
32 #define MATERIAL_DEFAULT_NORMAL_VALUE (vec3){0.0f, 0.0f, 1.0f}
33 #define MATERIAL_DEFAULT_NORMAL_ENABLED true
34 #define MATERIAL_DEFAULT_METALLIC_VALUE 0.0f
35 #define MATERIAL_DEFAULT_ROUGHNESS_VALUE 0.5f
36 #define MATERIAL_DEFAULT_AO_VALUE 1.0f
37 #define MATERIAL_DEFAULT_AO_ENABLED true
38 #define MATERIAL_DEFAULT_MRA_VALUE (vec3){0.0f, 0.5f, 1.0f}
39 #define MATERIAL_DEFAULT_MRA_ENABLED true
40 #define MATERIAL_DEFAULT_HAS_TRANSPARENCY false
41 #define MATERIAL_DEFAULT_DOUBLE_SIDED false
42 #define MATERIAL_DEFAULT_RECIEVES_SHADOW true
43 #define MATERIAL_DEFAULT_CASTS_SHADOW true
44 #define MATERIAL_DEFAULT_USE_VERTEX_COLOUR_AS_BASE_COLOUR false
45 
46 struct material_system_state;
47 struct frame_data;
48 
50 typedef struct material_system_config {
53 
57 
58 typedef enum material_texture_input {
59  // Forms the base colour of a material. Albedo for PBR, sometimes known as a "diffuse" colour. Specifies per-pixel colour.
61  // Texture specifying per-pixel normal vector.
63  // Texture specifying per-pixel metallic value.
65  // Texture specifying per-pixel roughness value.
67  // Texture specifying per-pixel ambient occlusion value.
69  // Texture specifying per-pixel emissive value.
71  // Texture specifying the reflection (only used for water materials)
73  // Texture specifying per-pixel refraction strength.
75  // Texture specifying the reflection depth (only used for water materials)
77  // Texture specifying the refraction depth.
80  // Texture holding per-pixel metallic (r), roughness (g) and ambient occlusion (b) value.
82  // The size of the material_texture_input enumeration.
85 
96 b8 material_system_initialize(u64* memory_requirement, struct material_system_state* state, const material_system_config* config);
97 
103 void material_system_shutdown(struct material_system_state* state);
104 
105 // -------------------------------------------------
106 // ---------------- MATERIAL -----------------------
107 // -------------------------------------------------
108 
118 KAPI b8 material_system_get_handle(struct material_system_state* state, kname name, khandle* out_material_handle);
119 
120 KAPI b8 material_is_loaded_get(struct material_system_state* state, khandle material);
121 
122 KAPI ktexture material_texture_get(struct material_system_state* state, khandle material, material_texture_input tex_input);
123 KAPI void material_texture_set(struct material_system_state* state, khandle material, material_texture_input tex_input, ktexture texture);
124 
125 KAPI texture_channel material_metallic_texture_channel_get(struct material_system_state* state, khandle material);
126 KAPI void material_metallic_texture_channel_set(struct material_system_state* state, khandle material, texture_channel value);
127 
128 KAPI texture_channel material_roughness_texture_channel_get(struct material_system_state* state, khandle material);
129 KAPI void material_roughness_texture_channel_set(struct material_system_state* state, khandle material, texture_channel value);
130 
131 KAPI texture_channel material_ao_texture_channel_get(struct material_system_state* state, khandle material);
132 KAPI void material_ao_texture_channel_set(struct material_system_state* state, khandle material, texture_channel value);
133 
134 KAPI texture_filter material_texture_filter_get(struct material_system_state* state, khandle material);
135 KAPI void material_texture_filter_set(struct material_system_state* state, khandle material, texture_filter value);
136 
137 KAPI texture_repeat material_texture_mode_get(struct material_system_state* state, khandle material);
138 KAPI void material_texture_mode_set(struct material_system_state* state, khandle material, texture_repeat value);
139 
140 KAPI b8 material_has_transparency_get(struct material_system_state* state, khandle material);
141 KAPI void material_has_transparency_set(struct material_system_state* state, khandle material, b8 value);
142 
143 KAPI b8 material_double_sided_get(struct material_system_state* state, khandle material);
144 KAPI void material_double_sided_set(struct material_system_state* state, khandle material, b8 value);
145 
146 KAPI b8 material_recieves_shadow_get(struct material_system_state* state, khandle material);
147 KAPI void material_recieves_shadow_set(struct material_system_state* state, khandle material, b8 value);
148 
149 KAPI b8 material_casts_shadow_get(struct material_system_state* state, khandle material);
150 KAPI void material_casts_shadow_set(struct material_system_state* state, khandle material, b8 value);
151 
152 KAPI b8 material_normal_enabled_get(struct material_system_state* state, khandle material);
153 KAPI void material_normal_enabled_set(struct material_system_state* state, khandle material, b8 value);
154 
155 KAPI b8 material_ao_enabled_get(struct material_system_state* state, khandle material);
156 KAPI void material_ao_enabled_set(struct material_system_state* state, khandle material, b8 value);
157 
158 KAPI b8 material_emissive_enabled_get(struct material_system_state* state, khandle material);
159 KAPI void material_emissive_enabled_set(struct material_system_state* state, khandle material, b8 value);
160 
161 KAPI b8 material_refraction_enabled_get(struct material_system_state* state, khandle material);
162 KAPI void material_refraction_enabled_set(struct material_system_state* state, khandle material, b8 value);
163 
164 KAPI f32 material_refraction_scale_get(struct material_system_state* state, khandle material);
165 KAPI void material_refraction_scale_set(struct material_system_state* state, khandle material, f32 value);
166 
167 KAPI b8 material_use_vertex_colour_as_base_colour_get(struct material_system_state* state, khandle material);
168 KAPI void material_use_vertex_colour_as_base_colour_set(struct material_system_state* state, khandle material, b8 value);
169 
179 KAPI b8 material_flag_set(struct material_system_state* state, khandle material, kmaterial_flag_bits flag, b8 value);
180 
189 KAPI b8 material_flag_get(struct material_system_state* state, khandle material, kmaterial_flag_bits flag);
190 
191 // -------------------------------------------------
192 // ------------- MATERIAL INSTANCE -----------------
193 // -------------------------------------------------
194 
204 KAPI b8 material_system_acquire(struct material_system_state* state, kname name, material_instance* out_instance);
205 
212 KAPI void material_system_release(struct material_system_state* state, material_instance* instance);
213 
217 typedef struct material_frame_data {
218  // Light space for shadow mapping. Per cascade
228 
232 b8 material_system_prepare_frame(struct material_system_state* state, material_frame_data mat_frame_data, struct frame_data* p_frame_data);
233 
234 b8 material_system_apply(struct material_system_state* state, khandle material, struct frame_data* p_frame_data);
235 
242 b8 material_system_apply_instance(struct material_system_state* state, const material_instance* instance, struct material_instance_draw_data draw_data, struct frame_data* p_frame_data);
243 
253 KAPI b8 material_instance_flag_set(struct material_system_state* state, material_instance instance, kmaterial_flag_bits flag, b8 value);
254 
263 KAPI b8 material_instance_flag_get(struct material_system_state* state, material_instance instance, kmaterial_flag_bits flag);
264 
273 KAPI b8 material_instance_base_colour_get(struct material_system_state* state, material_instance instance, vec4* out_value);
274 
283 KAPI b8 material_instance_base_colour_set(struct material_system_state* state, material_instance instance, vec4 value);
284 
293 KAPI b8 material_instance_uv_offset_get(struct material_system_state* state, material_instance instance, vec3* out_value);
294 
303 KAPI b8 material_instance_uv_offset_set(struct material_system_state* state, material_instance instance, vec3 value);
304 
313 KAPI b8 material_instance_uv_scale_get(struct material_system_state* state, material_instance instance, vec3* out_value);
314 
323 KAPI b8 material_instance_uv_scale_set(struct material_system_state* state, material_instance instance, vec3 value);
324 
331 KAPI material_instance material_system_get_default_standard(struct material_system_state* state);
332 
339 KAPI material_instance material_system_get_default_water(struct material_system_state* state);
340 
347 KAPI material_instance material_system_get_default_blended(struct material_system_state* state);
348 
354 KAPI void material_system_dump(struct material_system_state* state);
texture_channel
Definition: core_render_types.h:74
texture_filter
Represents supported texture filtering modes.
Definition: core_render_types.h:59
kmaterial_flag_bits
Definition: core_render_types.h:342
texture_repeat
Definition: core_render_types.h:66
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:205
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
_Bool b8
8-bit boolean type
Definition: defines.h:58
float f32
32-bit floating point number
Definition: defines.h:47
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
A global handle system for Kohi. Handles are used to obtain various resources using a unique handle i...
This files contains an implementation of knames.
u64 kname
A kname is a string hash made for quick comparisons versus traditional string comparisons.
Definition: kname.h:36
u16 ktexture
Represents a texture to be used for rendering purposes, stored on the GPU (VRAM)
Definition: kresource_types.h:45
material_texture_input
Definition: material_system.h:58
@ MATERIAL_TEXTURE_INPUT_REFRACTION_DEPTH
Definition: material_system.h:78
@ MATERIAL_TEXTURE_INPUT_REFLECTION
Definition: material_system.h:72
@ MATERIAL_TEXTURE_INPUT_COUNT
Definition: material_system.h:83
@ MATERIAL_TEXTURE_INPUT_REFRACTION
Definition: material_system.h:74
@ MATERIAL_TEXTURE_INPUT_NORMAL
Definition: material_system.h:62
@ MATERIAL_TEXTURE_INPUT_MRA
Definition: material_system.h:81
@ MATERIAL_TEXTURE_INPUT_BASE_COLOUR
Definition: material_system.h:60
@ MATERIAL_TEXTURE_INPUT_REFLECTION_DEPTH
Definition: material_system.h:76
@ MATERIAL_TEXTURE_INPUT_METALLIC
Definition: material_system.h:64
@ MATERIAL_TEXTURE_INPUT_DUDV
Definition: material_system.h:79
@ MATERIAL_TEXTURE_INPUT_EMISSIVE
Definition: material_system.h:70
@ MATERIAL_TEXTURE_INPUT_ROUGHNESS
Definition: material_system.h:66
@ MATERIAL_TEXTURE_INPUT_AMBIENT_OCCLUSION
Definition: material_system.h:68
KAPI b8 material_system_acquire(struct material_system_state *state, kname name, material_instance *out_instance)
Attempts to acquire an instance of the material with the given handle. Increases internal reference c...
KAPI void material_roughness_texture_channel_set(struct material_system_state *state, khandle material, texture_channel value)
KAPI b8 material_has_transparency_get(struct material_system_state *state, khandle material)
KAPI void material_casts_shadow_set(struct material_system_state *state, khandle material, b8 value)
KAPI void material_texture_mode_set(struct material_system_state *state, khandle material, texture_repeat value)
KAPI b8 material_recieves_shadow_get(struct material_system_state *state, khandle material)
KAPI b8 material_flag_set(struct material_system_state *state, khandle material, kmaterial_flag_bits flag, b8 value)
Sets the given material flag's state.
KAPI b8 material_emissive_enabled_get(struct material_system_state *state, khandle material)
KAPI b8 material_instance_uv_offset_set(struct material_system_state *state, material_instance instance, vec3 value)
Sets the value of the material instance-specific UV offset. Can be used for animating the position of...
#define MATERIAL_MAX_VIEWS
Definition: material_system.h:29
b8 material_system_apply(struct material_system_state *state, khandle material, struct frame_data *p_frame_data)
KAPI b8 material_casts_shadow_get(struct material_system_state *state, khandle material)
KAPI void material_normal_enabled_set(struct material_system_state *state, khandle material, b8 value)
KAPI void material_system_dump(struct material_system_state *state)
Dumps all of the registered materials and their reference counts/handles.
KAPI void material_texture_filter_set(struct material_system_state *state, khandle material, texture_filter value)
b8 material_system_prepare_frame(struct material_system_state *state, material_frame_data mat_frame_data, struct frame_data *p_frame_data)
struct material_system_config material_system_config
The configuration for the material system.
KAPI b8 material_is_loaded_get(struct material_system_state *state, khandle material)
KAPI void material_use_vertex_colour_as_base_colour_set(struct material_system_state *state, khandle material, b8 value)
KAPI b8 material_instance_uv_scale_set(struct material_system_state *state, material_instance instance, vec3 value)
Sets the value of the material instance-specific UV scale. Can be used for animating the position of ...
KAPI void material_metallic_texture_channel_set(struct material_system_state *state, khandle material, texture_channel value)
KAPI b8 material_instance_flag_set(struct material_system_state *state, material_instance instance, kmaterial_flag_bits flag, b8 value)
Sets the given material instance flag's state.
KAPI texture_channel material_roughness_texture_channel_get(struct material_system_state *state, khandle material)
KAPI material_instance material_system_get_default_standard(struct material_system_state *state)
Gets an instance of the default standard material.
KAPI b8 material_instance_uv_scale_get(struct material_system_state *state, material_instance instance, vec3 *out_value)
Gets the value of the material instance-specific UV scale. Can be used for animating the position of ...
struct material_instance_draw_data material_instance_draw_data
b8 material_system_apply_instance(struct material_system_state *state, const material_instance *instance, struct material_instance_draw_data draw_data, struct frame_data *p_frame_data)
KAPI void material_ao_texture_channel_set(struct material_system_state *state, khandle material, texture_channel value)
KAPI b8 material_instance_base_colour_set(struct material_system_state *state, material_instance instance, vec4 value)
Sets the value of the material instance-specific base colour.
KAPI void material_emissive_enabled_set(struct material_system_state *state, khandle material, b8 value)
KAPI void material_double_sided_set(struct material_system_state *state, khandle material, b8 value)
KAPI b8 material_refraction_enabled_get(struct material_system_state *state, khandle material)
KAPI void material_refraction_enabled_set(struct material_system_state *state, khandle material, b8 value)
KAPI b8 material_normal_enabled_get(struct material_system_state *state, khandle material)
KAPI texture_filter material_texture_filter_get(struct material_system_state *state, khandle material)
KAPI void material_ao_enabled_set(struct material_system_state *state, khandle material, b8 value)
KAPI b8 material_use_vertex_colour_as_base_colour_get(struct material_system_state *state, khandle material)
b8 material_system_initialize(u64 *memory_requirement, struct material_system_state *state, const material_system_config *config)
Initializes the material system. Should be called twice; once to get the memory requirement (passing ...
KAPI void material_has_transparency_set(struct material_system_state *state, khandle material, b8 value)
KAPI b8 material_double_sided_get(struct material_system_state *state, khandle material)
KAPI texture_channel material_ao_texture_channel_get(struct material_system_state *state, khandle material)
struct material_frame_data material_frame_data
KAPI texture_channel material_metallic_texture_channel_get(struct material_system_state *state, khandle material)
KAPI b8 material_system_get_handle(struct material_system_state *state, kname name, khandle *out_material_handle)
Attempts to get the identifier of a material with the given name. If it has not yet been loaded,...
KAPI b8 material_instance_uv_offset_get(struct material_system_state *state, material_instance instance, vec3 *out_value)
Gets the value of the material instance-specific UV offset. Can be used for animating the position of...
KAPI f32 material_refraction_scale_get(struct material_system_state *state, khandle material)
KAPI b8 material_flag_get(struct material_system_state *state, khandle material, kmaterial_flag_bits flag)
Gets value of the given material flag's state.
#define MATERIAL_MAX_SHADOW_CASCADES
Definition: material_system.h:27
KAPI void material_recieves_shadow_set(struct material_system_state *state, khandle material, b8 value)
KAPI ktexture material_texture_get(struct material_system_state *state, khandle material, material_texture_input tex_input)
KAPI void material_system_release(struct material_system_state *state, material_instance *instance)
Releases the given material instance.
KAPI material_instance material_system_get_default_water(struct material_system_state *state)
Gets an instance of the default water material.
KAPI void material_refraction_scale_set(struct material_system_state *state, khandle material, f32 value)
KAPI b8 material_instance_base_colour_get(struct material_system_state *state, material_instance instance, vec4 *out_value)
Gets the value of the material instance-specific base colour.
KAPI material_instance material_system_get_default_blended(struct material_system_state *state)
Gets an instance of the default blended material.
KAPI b8 material_ao_enabled_get(struct material_system_state *state, khandle material)
KAPI void material_texture_set(struct material_system_state *state, khandle material, material_texture_input tex_input, ktexture texture)
KAPI texture_repeat material_texture_mode_get(struct material_system_state *state, khandle material)
void material_system_shutdown(struct material_system_state *state)
Shuts down the material system.
KAPI b8 material_instance_flag_get(struct material_system_state *state, material_instance instance, kmaterial_flag_bits flag)
Gets value of the given material instance flag's state.
Engine-level current frame-specific data.
Definition: frame_data.h:11
A handle is a unique identifier used a system in the engine to avoid using raw pointers where possibl...
Definition: khandle.h:25
Definition: material_system.h:217
vec4 view_positions[MATERIAL_MAX_VIEWS]
Definition: material_system.h:222
mat4 projection
Definition: material_system.h:220
mat4 directional_light_spaces[MATERIAL_MAX_SHADOW_CASCADES]
Definition: material_system.h:219
f32 delta_time
Definition: material_system.h:226
mat4 views[MATERIAL_MAX_VIEWS]
Definition: material_system.h:221
ktexture irradiance_cubemap_textures[MATERIAL_MAX_SHADOW_CASCADES]
Definition: material_system.h:230
f32 game_time
Definition: material_system.h:227
f32 cascade_splits[MATERIAL_MAX_SHADOW_CASCADES]
Definition: material_system.h:224
u32 render_mode
Definition: material_system.h:223
ktexture shadow_map_texture
Definition: material_system.h:229
f32 shadow_bias
Definition: material_system.h:225
Definition: material_system.h:236
mat4 model
Definition: material_system.h:237
vec4 clipping_plane
Definition: material_system.h:238
u32 irradiance_cubemap_index
Definition: material_system.h:239
u32 view_index
Definition: material_system.h:240
A material instance, which contains handles to both the base material as well as the instance itself....
Definition: core_render_types.h:395
The configuration for the material system.
Definition: material_system.h:50
u32 max_material_count
The maximum number of loaded materials.
Definition: material_system.h:52
u32 max_instance_count
The maximum number of material instances.
Definition: material_system.h:55
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:195
A 3-element vector.
Definition: math_types.h:49
A 4-element vector.
Definition: math_types.h:89