Kohi Game Engine
shader_system.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include "containers/hashtable.h"
17 #include "defines.h"
19 
21 typedef struct shader_system_config {
31 
35 typedef enum shader_state {
43 
47 typedef struct shader_uniform {
66 
70 typedef struct shader_attribute {
72  char* name;
78 
79 typedef enum shader_flags {
85 
87 
91 typedef struct shader {
94 
95  char* name;
96 
98 
101 
111 
121 
124 
127 
132 
135 
138 
140 
145 
150 
153 
156 
159 
166 
171 
175 
186 b8 shader_system_initialize(u64* memory_requirement, void* memory, void* config);
187 
193 void shader_system_shutdown(void* state);
194 
203 
210 KAPI u32 shader_system_get_id(const char* shader_name);
211 
219 
226 KAPI shader* shader_system_get(const char* shader_name);
227 
234 KAPI b8 shader_system_use(const char* shader_name);
235 
243 
251 KAPI u16 shader_system_uniform_index(shader* s, const char* uniform_name);
252 
261 KAPI b8 shader_system_uniform_set(const char* uniform_name, const void* value);
262 
271 KAPI b8 shader_system_sampler_set(const char* sampler_name, const texture* t);
272 
281 KAPI b8 shader_system_uniform_set_by_index(u16 index, const void* value);
282 
292 
301 
311 
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:177
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
_Bool b8
8-bit boolean type
Definition: defines.h:58
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:22
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:19
This file contains the hashtable implementation.
shader_scope
Defines shader scope, which indicates how often it gets updated.
Definition: resource_types.h:411
shader_attribute_type
Available attribute types.
Definition: resource_types.h:376
shader_uniform_type
Available uniform types.
Definition: resource_types.h:391
KAPI b8 shader_system_apply_instance(b8 needs_update)
Applies instance-scoped uniforms. NOTE: Operates against the currently-used shader.
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.
void shader_system_shutdown(void *state)
Shuts down the shader system.
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 cur...
struct shader_system_config shader_system_config
Configuration for the shader system.
shader_flags
Definition: shader_system.h:79
@ SHADER_FLAG_NONE
Definition: shader_system.h:80
@ SHADER_FLAG_WIREFRAME
Definition: shader_system.h:83
@ SHADER_FLAG_DEPTH_WRITE
Definition: shader_system.h:82
@ SHADER_FLAG_DEPTH_TEST
Definition: shader_system.h:81
struct shader_uniform shader_uniform
Represents a single entry in the internal uniform array.
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....
KAPI b8 shader_system_create(renderpass *pass, const shader_config *config)
Creates a new shader with the given config.
shader_state
Represents the current state of a given shader.
Definition: shader_system.h:35
@ SHADER_STATE_NOT_CREATED
The shader has not yet gone through the creation process, and is unusable.
Definition: shader_system.h:37
@ SHADER_STATE_INITIALIZED
The shader is created and initialized, and is ready for use.
Definition: shader_system.h:41
@ SHADER_STATE_UNINITIALIZED
The shader has gone through the creation process, but not initialization. It is unusable.
Definition: shader_system.h:39
KAPI shader * shader_system_get_by_id(u32 shader_id)
Returns a pointer to a shader with the given identifier.
b8 shader_system_initialize(u64 *memory_requirement, void *memory, void *config)
Initializes the shader system using the supplied configuration. NOTE: Call this twice,...
KAPI b8 shader_system_use_by_id(u32 shader_id)
Uses the shader with the given identifier.
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.
KAPI b8 shader_system_apply_global(b8 needs_update)
Applies global-scoped uniforms. NOTE: Operates against the currently-used shader.
struct shader_attribute shader_attribute
Represents a single shader vertex attribute.
KAPI shader * shader_system_get(const char *shader_name)
Returns a pointer to a shader with the given name.
KAPI u32 shader_system_get_id(const char *shader_name)
Gets the identifier of a shader by name.
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...
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.
KAPI b8 shader_system_use(const char *shader_name)
Uses the shader with the given name.
u32 shader_flag_bits
Definition: shader_system.h:86
struct shader shader
Represents a shader on the frontend.
Represents a simple hashtable. Members of this structure should not be modified outside the functions...
Definition: hashtable.h:25
A range, typically of memory.
Definition: defines.h:61
Represents a generic renderpass.
Definition: renderer_types.h:127
Represents a single shader vertex attribute.
Definition: shader_system.h:70
char * name
The attribute name.
Definition: shader_system.h:72
shader_attribute_type type
The attribute type.
Definition: shader_system.h:74
u32 size
The attribute size in bytes.
Definition: shader_system.h:76
Configuration for a shader. Typically created and destroyed by the shader resource loader,...
Definition: resource_types.h:454
Configuration for the shader system.
Definition: shader_system.h:21
u8 max_global_textures
The maximum number of global-scope textures allowed in a single shader.
Definition: shader_system.h:27
u8 max_uniform_count
The maximum number of uniforms allowed in a single shader.
Definition: shader_system.h:25
u16 max_shader_count
The maximum number of shaders held in the system. NOTE: Should be at least 512.
Definition: shader_system.h:23
u8 max_instance_textures
The maximum number of instance-scope textures allowed in a single shader.
Definition: shader_system.h:29
Represents a single entry in the internal uniform array.
Definition: shader_system.h:47
u16 size
The size of the uniform, or 0 for samplers.
Definition: shader_system.h:58
u16 location
The location to be used as a lookup. Typically the same as the index except for samplers,...
Definition: shader_system.h:54
u64 offset
The offset in bytes from the beginning of the uniform set (global/instance/local)
Definition: shader_system.h:49
shader_scope scope
The scope of the uniform.
Definition: shader_system.h:62
shader_uniform_type type
The type of uniform.
Definition: shader_system.h:64
u8 set_index
The index of the descriptor set the uniform belongs to (0=global, 1=instance, INVALID_ID=local).
Definition: shader_system.h:60
u16 index
Index into the internal uniform array.
Definition: shader_system.h:56
Represents a shader on the frontend.
Definition: shader_system.h:91
u32 topology_types
The types of topologies used by the shader and its pipeline. See primitive_topology_type.
Definition: shader_system.h:100
u32 bound_instance_id
The identifier of the currently bound instance.
Definition: shader_system.h:142
shader_scope bound_scope
Definition: shader_system.h:139
hashtable uniform_lookup
A hashtable to store uniform index/locations by name.
Definition: shader_system.h:149
shader_uniform * uniforms
An array of uniforms in this shader. Darray.
Definition: shader_system.h:152
shader_flag_bits flags
Definition: shader_system.h:97
texture_map ** global_texture_maps
An array of global texture map pointers. Darray.
Definition: shader_system.h:134
u64 ubo_size
The actual size of the instance uniform buffer object.
Definition: shader_system.h:123
u8 instance_texture_count
The number of instance textures.
Definition: shader_system.h:137
u64 required_ubo_alignment
The amount of bytes that are required for UBO alignment.
Definition: shader_system.h:110
char * name
Definition: shader_system.h:95
u32 id
The shader identifier.
Definition: shader_system.h:93
u8 push_constant_range_count
The number of push constant ranges.
Definition: shader_system.h:161
u64 push_constant_size
The total size of all push constant ranges combined.
Definition: shader_system.h:129
u32 bound_ubo_offset
The currently bound instance's ubo offset.
Definition: shader_system.h:144
u64 global_ubo_size
The actual size of the global uniform buffer object.
Definition: shader_system.h:113
u64 ubo_stride
The stride of the instance uniform buffer object.
Definition: shader_system.h:126
u64 push_constant_stride
The push constant stride, aligned to 4 bytes as required by Vulkan.
Definition: shader_system.h:131
void * hashtable_block
The block of memory used by the uniform hashtable.
Definition: shader_system.h:147
u64 render_frame_number
Used to ensure the shader's globals are only updated once per frame.
Definition: shader_system.h:168
range push_constant_ranges[32]
An array of push constant ranges.
Definition: shader_system.h:163
void * internal_data
An opaque pointer to hold renderer API specific data. Renderer is responsible for creation and destru...
Definition: shader_system.h:173
u64 global_ubo_offset
The offset in bytes for the global UBO from the beginning of the uniform buffer.
Definition: shader_system.h:120
shader_attribute * attributes
An array of attributes. Darray.
Definition: shader_system.h:155
u16 attribute_stride
The size of all attributes combined, a.k.a. the size of a vertex.
Definition: shader_system.h:165
u64 global_ubo_stride
The stride of the global uniform buffer object.
Definition: shader_system.h:115
u8 draw_index
Used to ensure the shader's globals are only updated once per draw.
Definition: shader_system.h:170
shader_state state
The internal state of the shader.
Definition: shader_system.h:158
A structure which maps a texture, use and other properties.
Definition: resource_types.h:208
Represents a texture.
Definition: resource_types.h:165