diff options
| -rw-r--r-- | _sokol_gfx.inl | 159 | ||||
| -rw-r--r-- | _sokol_gfx_gl.inl | 47 | ||||
| -rw-r--r-- | fips.yml | 2 | ||||
| -rw-r--r-- | sokol_gfx.h | 45 |
4 files changed, 233 insertions, 20 deletions
diff --git a/_sokol_gfx.inl b/_sokol_gfx.inl index 7bd7cff9..a5824ee9 100644 --- a/_sokol_gfx.inl +++ b/_sokol_gfx.inl @@ -1,4 +1,157 @@ -#pragma once /* - FIXME: Sokol Gfx generic implementation code -*/
\ No newline at end of file + Sokol Gfx generic implementation code +*/ +typedef enum { + _SG_SLOT_STATE_FREE, + _SG_SLOT_STATE_ALLOC, + _SG_SLOT_STATE_VALID, +} _sg_slot_state; + +typedef struct { + sg_id id; + _sg_slot_state state; +} _sg_slot; + +static void _sg_init_slot(_sg_slot* slot) { + SOKOL_ASSERT(slot); + slot->id = SG_INVALID_ID; + slot->state = _SG_SLOT_STATE_FREE; +} + +#ifdef SOKOL_GFX_USE_GL +#include "_sokol_gfx_gl.inl" +#elif SOKOL_GFX_USE_D3D11 +#include "_sokol_gfx_d3d11.inl" +#elif SOKOL_GFX_USE_METAL +#include "_sokol_gfx_metal.inl" +#else +#error "Please select rendering backend by definining SOKOL_GFX_USE_GL, SOKOL_GFX_USE_D3D11 or SOKOL_GFX_USE_METAL" +#endif + +//------------------------------------------------------------------------------ +typedef struct { + int num; + int cur; + uint32_t unique_counter; + int* free_queue; +} _sg_pool; + +static void _sg_init_pool(_sg_pool* pool, int num) { + SOKOL_ASSERT(pool && (num > 0)); + pool->num = num; + pool->cur = 0; + pool->unique_counter = 0; + pool->free_queue = SOKOL_MALLOC(sizeof(int)*num); + for (int i = num-1; i >= 0; i--) { + pool->free_queue[pool->cur++] = i; + } +} + +static void _sg_destroy_pool(_sg_pool* pool) { + SOKOL_ASSERT(pool); + SOKOL_FREE(pool->free_queue); + pool->free_queue = 0; + pool->num = 0; + pool->cur = 0; + pool->unique_counter = 0; +} + +static sg_id _sg_pool_alloc_id(_sg_pool* pool) { + SOKOL_ASSERT(pool); + SOKOL_ASSERT(pool->cur > 0); + SOKOL_ASSERT(pool->free_queue); + int slot_index = pool->free_queue[--pool->cur]; + return ((pool->unique_counter++)<<SOKOL_GFX_ID_SHIFT) | (slot_index&SOKOL_GFX_ID_MASK); +} + +static void _sg_pool_free_id(_sg_pool* pool, sg_id id) { + SOKOL_ASSERT(pool); + SOKOL_ASSERT(pool->free_queue); + SOKOL_ASSERT(pool->cur < pool->num); + pool->free_queue[pool->cur++] = id; +} + +//------------------------------------------------------------------------------ +void sg_init_setup_desc(sg_setup_desc* desc) { + SOKOL_ASSERT(desc); + desc->width = 640; + desc->height = 400; + desc->sample_count = 1; + desc->buffer_pool_size = 128; + desc->image_pool_size = 128; + desc->shader_pool_size = 128; + desc->pipeline_pool_size = 128; + desc->pass_pool_size = 128; + desc->label_stack_size = 32; + //FIXME! + //desc->default_pass_action +} + +typedef struct { + _sg_pool buffer_pool; + _sg_pool image_pool; + _sg_pool shader_pool; + _sg_pool pipeline_pool; + _sg_pool pass_pool; + _sg_buffer* buffers; + _sg_image* images; + _sg_shader* shaders; + _sg_pipeline* pipelines; + _sg_pass* passes; +} _sg_state; +_sg_state* _sg = 0; + +void sg_setup(sg_setup_desc* desc) { + SOKOL_ASSERT(!_sg); + SOKOL_ASSERT(desc); + SOKOL_ASSERT((desc->width > 0) && (desc->height > 0)); + SOKOL_ASSERT(desc->sample_count >= 1); + SOKOL_ASSERT((desc->buffer_pool_size > 0) && (desc->buffer_pool_size < SOKOL_GFX_MAX_POOL_SIZE)); + SOKOL_ASSERT((desc->image_pool_size > 0) && (desc->image_pool_size < SOKOL_GFX_MAX_POOL_SIZE)); + SOKOL_ASSERT((desc->shader_pool_size > 0) && (desc->shader_pool_size < SOKOL_GFX_MAX_POOL_SIZE)); + SOKOL_ASSERT((desc->pipeline_pool_size > 0) && (desc->pipeline_pool_size < SOKOL_GFX_MAX_POOL_SIZE)); + SOKOL_ASSERT((desc->pass_pool_size > 0) && (desc->pass_pool_size < SOKOL_GFX_MAX_POOL_SIZE)); + SOKOL_ASSERT(desc->label_stack_size > 0); + + _sg = SOKOL_MALLOC(sizeof(_sg_state)); + _sg_init_pool(&_sg->buffer_pool, desc->buffer_pool_size); + _sg->buffers = SOKOL_MALLOC(sizeof(_sg_buffer) * _sg->buffer_pool.num); + for (int i = 0; i < _sg->buffer_pool.num; i++) { + _sg_init_buffer(&_sg->buffers[i]); + } + _sg_init_pool(&_sg->image_pool, desc->image_pool_size); + _sg->images = SOKOL_MALLOC(sizeof(_sg_image) * _sg->image_pool.num); + for (int i = 0; i < _sg->image_pool.num; i++) { + _sg_init_image(&_sg->images[i]); + } + _sg_init_pool(&_sg->shader_pool, desc->shader_pool_size); + _sg->shaders = SOKOL_MALLOC(sizeof(_sg_shader) * _sg->shader_pool.num); + for (int i = 0; i < _sg->shader_pool.num; i++) { + _sg_init_shader(&_sg->shaders[i]); + } + _sg_init_pool(&_sg->pipeline_pool, desc->pipeline_pool_size); + _sg->pipelines = SOKOL_MALLOC(sizeof(_sg_pipeline) * _sg->pipeline_pool.num); + for (int i = 0; i < _sg->pipeline_pool.num; i++) { + _sg_init_pipeline(&_sg->pipelines[i]); + } + _sg_init_pool(&_sg->pass_pool, desc->pass_pool_size); + _sg->passes = SOKOL_MALLOC(sizeof(_sg_pass) * _sg->pass_pool.num); + for (int i = 0; i < _sg->pass_pool.num; i++) { + _sg_init_pass(&_sg->passes[i]); + } +} + +void sg_discard() { + SOKOL_ASSERT(_sg); + + SOKOL_FREE(_sg->passes); _sg->passes = 0; + SOKOL_FREE(_sg->pipelines); _sg->pipelines = 0; + SOKOL_FREE(_sg->shaders); _sg->shaders = 0; + SOKOL_FREE(_sg->images); _sg->images = 0; + SOKOL_FREE(_sg->buffers); _sg->buffers = 0; + _sg_destroy_pool(&_sg->pass_pool); + _sg_destroy_pool(&_sg->pipeline_pool); + _sg_destroy_pool(&_sg->shader_pool); + _sg_destroy_pool(&_sg->image_pool); + _sg_destroy_pool(&_sg->buffer_pool); +} diff --git a/_sokol_gfx_gl.inl b/_sokol_gfx_gl.inl index e2676fb1..a617dff6 100644 --- a/_sokol_gfx_gl.inl +++ b/_sokol_gfx_gl.inl @@ -1,4 +1,49 @@ -#pragma once /* FIXME: Sokol Gfx GL rendering backend */ + +typedef struct { + _sg_slot slot; +} _sg_buffer; + +typedef struct { + _sg_slot slot; +} _sg_image; + +typedef struct { + _sg_slot slot; +} _sg_shader; + +typedef struct { + _sg_slot slot; +} _sg_pipeline; + +typedef struct { + _sg_slot slot; +} _sg_pass; + +static void _sg_init_buffer(_sg_buffer* buf) { + SOKOL_ASSERT(buf); + _sg_init_slot(&buf->slot); +} + +static void _sg_init_image(_sg_image* img) { + SOKOL_ASSERT(img); + _sg_init_slot(&img->slot); +} + +static void _sg_init_shader(_sg_shader* shd) { + SOKOL_ASSERT(shd); + _sg_init_slot(&shd->slot); +} + +static void _sg_init_pipeline(_sg_pipeline* pip) { + SOKOL_ASSERT(pip); + _sg_init_slot(&pip->slot); +} + +static void _sg_init_pass(_sg_pass* pass) { + SOKOL_ASSERT(pass); + _sg_init_slot(&pass->slot); +} + diff --git a/fips.yml b/fips.yml new file mode 100644 index 00000000..93f5c31f --- /dev/null +++ b/fips.yml @@ -0,0 +1,2 @@ +exports: + header-dirs: [ . ]
\ No newline at end of file diff --git a/sokol_gfx.h b/sokol_gfx.h index 5208c60a..dd866abf 100644 --- a/sokol_gfx.h +++ b/sokol_gfx.h @@ -2,26 +2,44 @@ /* FIXME */ +#include <stdlib.h> #include <stdint.h> #include <stdbool.h> +#ifndef SOKOL_ASSERT +#include <assert.h> +#define SOKOL_ASSERT(c) assert(c) +#endif +#ifndef SOKOL_MALLOC +#define SOKOL_MALLOC(s) malloc(s) +#define SOKOL_FREE(p) free(p) +#endif + typedef uint32_t sg_label; typedef uint32_t sg_id; +static const sg_id SG_INVALID_ID = 0xFFFFFFFF; -struct sg_pass_action { - /* FIXME! */ +enum { + SOKOL_GFX_ID_SHIFT = 16, + SOKOL_GFX_ID_MASK = (1<<SOKOL_GFX_ID_SHIFT)-1, + SOKOL_GFX_MAX_POOL_SIZE = (1<<SOKOL_GFX_ID_SHIFT), }; typedef struct { + /* FIXME! */ +} sg_pass_action; + +typedef struct { int width; int height; int sample_count; - sg_pass_action default_pass_action; int buffer_pool_size; int image_pool_size; int shader_pool_size; int pipeline_pool_size; int pass_pool_size; + int label_stack_size; + sg_pass_action default_pass_action; } sg_setup_desc; typedef enum { @@ -237,54 +255,49 @@ typedef struct { /* FIXME */ } sg_update_image_desc; +/* setup */ +extern void sg_init_setup_desc(sg_setup_desc* desc); extern void sg_setup(sg_setup_desc* desc); extern void sg_discard(); extern bool sg_isvalid(); extern bool sg_query_feature(sg_feature feature); +/* resource management */ extern sg_label sg_gen_label(); extern void sg_push_label(sg_label); extern sg_label sg_pop_label(); - extern void sg_init_buffer_desc(sg_buffer_desc* desc); extern void sg_init_image_desc(sg_image_desc* desc); extern void sg_init_shader_desc(sg_shader_desc* desc); extern void sg_init_pipeline_desc(sg_pipeline_desc* desc); extern void sg_init_pass_desc(sg_pass_desc* desc); -extern void sg_init_pass_action(sg_pass_action* pass_action); - extern sg_id sg_make_buffer(sg_buffer_desc* desc, void* opt_data, int opt_bytes); extern sg_id sg_make_image(sg_image_desc* desc, void* opt_data, int opt_bytes); -extern sg_id sg_make_pipeline(sg_pipeline_desc* desc); extern sg_id sg_make_shader(sg_shader_desc* desc); +extern sg_id sg_make_pipeline(sg_pipeline_desc* desc); extern sg_id sg_make_pass(sg_pass_desc* desc); extern void sg_destroy(sg_label label); +extern void sg_update_buffer(sg_id buf_id, void* data, int num_bytes); +extern void sg_update_image(sg_id img_id, void* data, sg_update_image_desc* desc); +/* rendering */ extern void sg_begin_pass(sg_id pass_id, sg_pass_action* pass_action); extern void sg_end_pass(); - extern void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left); extern void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left); extern void sg_apply_draw_state(sg_draw_state* ds); extern void sg_apply_uniform_block(sg_stage stage, int slot, void* data, int num_bytes); - -extern void sg_update_buffer(sg_id buf_id, void* data, int num_bytes); -extern void sg_update_image(sg_id img_id, void* data, sg_update_image_desc* desc); - extern void sg_draw(int base_element, int num_elements, int num_instances); - extern void sg_commit(); extern void sg_reset_state_cache(); +/* async resource setup */ extern sg_id sg_alloc_buffer(); extern sg_id sg_alloc_image(); extern void sg_init_buffer(sg_id buf_id, sg_buffer_desc* desc); extern void sg_init_image(sg_id img_id, sg_image_desc* desc); #ifdef SOKOL_IMPLEMENTATION -#ifdef SOKOL_GFX_USE_GL -#include "_sokol_gfx_gl.inl" -#endif #include "_sokol_gfx.inl" #endif |