From 8762abe508d7ad913bd19bd9fa4e6881e76cc8e6 Mon Sep 17 00:00:00 2001 From: klemek Date: Sun, 28 Sep 2025 22:44:12 +0200 Subject: [PATCH] parse state config --- Makefile.dev | 2 +- src/config.h | 4 +++ src/forge.c | 32 +++++++++------------ src/shaders.c | 41 +++------------------------ src/state.c | 78 +++++++++++++++++++++++++++++++++++++++++++-------- src/state.h | 2 -- src/types.h | 54 ++++++++++++++++++----------------- 7 files changed, 117 insertions(+), 96 deletions(-) diff --git a/Makefile.dev b/Makefile.dev index 4d58fc3..e4e438a 100644 --- a/Makefile.dev +++ b/Makefile.dev @@ -21,7 +21,7 @@ build: -DGLFW_NATIVE_INCLUDE_NONE \ -DLOG_USE_COLOR \ -o build/$(TARGET) \ - -g + -g -Og .PHONY: run run: build diff --git a/src/config.h b/src/config.h index 86af765..b389d83 100644 --- a/src/config.h +++ b/src/config.h @@ -21,4 +21,8 @@ #define UNSET_MIDI_CODE 300 #endif +#ifndef ARRAY_SIZE +#define ARRAY_SIZE 1024 +#endif + #endif /* CONFIG_H */ \ No newline at end of file diff --git a/src/forge.c b/src/forge.c index 38a6ede..c82b2c8 100644 --- a/src/forge.c +++ b/src/forge.c @@ -22,8 +22,8 @@ static SharedContext *context; static ShaderProgram program; static Window *window_output; static Window *window_monitor; -static VideoCapture *inputs; -static File *fragment_shaders; +static VideoCapture inputs[ARRAY_SIZE]; +static File fragment_shaders[ARRAY_SIZE]; static File common_shader_code; static Timer timer; static ConfigFile config; @@ -53,7 +53,8 @@ static void compute_fps() { } } -static void init_context(Parameters params) { +static void init_context(Parameters params, unsigned int in_count, + unsigned int frag_count) { unsigned int i; context->tempo = params.base_tempo; @@ -63,12 +64,12 @@ static void init_context(Parameters params) { memset(context->state, 0, sizeof(context->state)); if (params.demo) { - state_randomize(context, state_config, program.frag_count); + state_randomize(context, state_config, frag_count); } memset(context->seeds, 0, sizeof(context->seeds)); - for (i = 0; i < program.frag_count; i++) { + for (i = 0; i < frag_count; i++) { context->seeds[i] = rand_uint(1000); } @@ -77,7 +78,7 @@ static void init_context(Parameters params) { memset(context->input_formats, 0, sizeof(context->input_formats)); memset(context->input_fps, 0, sizeof(context->input_fps)); - for (i = 0; i < program.in_count; i++) { + for (i = 0; i < in_count; i++) { if (!inputs[i].error) { context->input_widths[i] = inputs[i].width; context->input_heights[i] = inputs[i].height; @@ -127,8 +128,6 @@ File read_fragment_shader_file(char *frag_path, unsigned int i) { static void init_files(char *frag_path, unsigned int frag_count) { unsigned int i; - fragment_shaders = malloc(frag_count * sizeof(File)); - for (i = 0; i < frag_count + 1; i++) { if (i == 0) { common_shader_code = read_fragment_shader_file(frag_path, i); @@ -154,8 +153,6 @@ static void init_inputs(char *video_in[MAX_VIDEO], unsigned int input_count, unsigned int video_size) { unsigned int i; - inputs = malloc(input_count * sizeof(VideoCapture)); - for (i = 0; i < input_count; i++) { inputs[i] = video_init(video_in[i], video_size); } @@ -181,8 +178,6 @@ static void free_video_captures(unsigned int video_count) { video_free(inputs[i]); } - - free(inputs); } static void error_callback(int error, const char *description) { @@ -242,7 +237,7 @@ static void loop(bool hr) { } void forge_run(Parameters params) { - unsigned int frag_count; + unsigned int frag_count, in_count; context = shared_init_context("/" PACKAGE "_context"); @@ -250,14 +245,17 @@ void forge_run(Parameters params) { config = config_file_read(params.config_path, false); - state_config = state_parse_config(config); + frag_count = config_file_get_int(config, "FRAG_COUNT", 1); + in_count = config_file_get_int(config, "IN_COUNT", 0); - frag_count = config_file_get_int(config, "FRAG_COUNT", 6); + state_config = state_parse_config(config); init_files(params.frag_path, frag_count); init_inputs(params.video_in, params.video_in_count, params.video_size); + init_context(params, in_count, frag_count); + if (!start_video_captures(params.video_in_count)) { return; } @@ -304,8 +302,6 @@ void forge_run(Parameters params) { window_monitor = NULL; } - init_context(params); - if (program.error) { context->stop = true; window_terminate(); @@ -345,7 +341,5 @@ void forge_run(Parameters params) { config_file_free(config); - state_free_config(state_config); - window_terminate(); } \ No newline at end of file diff --git a/src/shaders.c b/src/shaders.c index cf9f349..f559797 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -35,8 +35,6 @@ static void init_gl(ShaderProgram *program) { static void init_textures(ShaderProgram *program, SharedContext *context) { unsigned int i; - program->textures = malloc(program->tex_count * sizeof(GLuint)); - glGenTextures(program->tex_count, program->textures); for (i = 0; i < program->tex_count; i++) { @@ -116,7 +114,7 @@ static void init_input(ShaderProgram *program, ConfigFile config, VideoCapture *inputs, unsigned int input_count) { unsigned int i; unsigned tex_i; - char name[32]; + char name[256]; for (i = 0; i < program->in_count; i++) { if (i < input_count && !inputs[i].error) { @@ -132,9 +130,7 @@ static void init_input(ShaderProgram *program, ConfigFile config, static void init_framebuffers(ShaderProgram *program, ConfigFile config) { unsigned int i; unsigned tex_i; - char name[32]; - - program->frame_buffers = malloc(program->frag_count * sizeof(GLuint)); + char name[256]; glGenFramebuffers(program->frag_count, program->frame_buffers); @@ -223,8 +219,6 @@ static void init_shaders(ShaderProgram *program, File *fragment_shaders) { program->error |= !compile_shader( program->vertex_shader, "internal vertex shader", vertex_shader_text); - program->fragment_shaders = malloc(program->frag_count * sizeof(GLuint)); - // compile fragment shaders for (i = 0; i < program->frag_count; i++) { program->fragment_shaders[i] = glCreateShader(GL_FRAGMENT_SHADER); @@ -241,7 +235,7 @@ static void init_shaders(ShaderProgram *program, File *fragment_shaders) { static void init_single_program(ShaderProgram *program, unsigned int i, ConfigFile config) { unsigned int j, k; - char name[32]; + char name[256]; char *prefix; program->programs[i] = glCreateProgram(); @@ -335,30 +329,6 @@ static void init_single_program(ShaderProgram *program, unsigned int i, static void init_programs(ShaderProgram *program, ConfigFile config) { unsigned int i; - program->programs = malloc(program->frag_count * sizeof(GLuint)); - program->itime_locations = malloc(program->frag_count * sizeof(GLuint)); - program->itempo_locations = malloc(program->frag_count * sizeof(GLuint)); - program->ifps_locations = malloc(program->frag_count * sizeof(GLuint)); - program->ires_locations = malloc(program->frag_count * sizeof(GLuint)); - program->itexres_locations = malloc(program->frag_count * sizeof(GLuint)); - program->iinres_locations = - malloc(program->frag_count * program->in_count * sizeof(GLuint)); - program->iinfmt_locations = - malloc(program->frag_count * program->in_count * sizeof(GLuint)); - program->iinfps_locations = - malloc(program->frag_count * program->in_count * sizeof(GLuint)); - program->idemo_locations = malloc(program->frag_count * sizeof(GLuint)); - program->iseed_locations = - malloc(program->frag_count * program->frag_count * sizeof(GLuint)); - program->istate_locations = malloc(program->frag_count * program->frag_count * - program->sub_type_count * sizeof(GLuint)); - program->vpos_locations = malloc(program->frag_count * sizeof(GLuint)); - program->textures_locations = - malloc(program->frag_count * program->tex_count * sizeof(GLuint)); - program->sub_locations = - malloc(program->frag_count * program->sub_type_count * - program->sub_variant_count * sizeof(GLuint)); - for (i = 0; i < program->frag_count; i++) { init_single_program(program, i, config); } @@ -452,14 +422,13 @@ static void update_viewport(ShaderProgram program, SharedContext *context) { static void use_program(ShaderProgram program, int i, bool output, SharedContext *context) { unsigned int j, k; - GLuint *subroutines; + GLuint subroutines[ARRAY_SIZE]; vec2 resolution, tex_resolution, in_resolution; resolution[0] = (float)context->width; resolution[1] = (float)context->height; tex_resolution[0] = (float)context->internal_width; tex_resolution[1] = (float)context->internal_height; - subroutines = malloc(program.sub_type_count * sizeof(GLuint)); // use specific shader program glUseProgram(program.programs[i]); @@ -529,8 +498,6 @@ static void use_program(ShaderProgram program, int i, bool output, // draw output glDrawArrays(GL_TRIANGLES, 0, 6); - - free(subroutines); } void shaders_compute(ShaderProgram program, SharedContext *context, diff --git a/src/state.c b/src/state.c index f0dfa5a..8630ec1 100644 --- a/src/state.c +++ b/src/state.c @@ -5,25 +5,86 @@ #include "types.h" StateConfig state_parse_config(ConfigFile config) { + unsigned int i, j, offset, total, frag_count; StateConfig state_config; + char name[256]; state_config.select_page_count = config_file_get_int(config, "SELECT_PAGE_COUNT", 0); - // TODO parse + for (i = 0; i < state_config.select_page_count; i++) { + sprintf(name, "SELECT_PAGE_%d", i + 1); + state_config.select_page_codes[i] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + } state_config.select_item_count = config_file_get_int(config, "SELECT_ITEM_COUNT", 0); - // TODO parse + for (i = 0; i < state_config.select_item_count; i++) { + sprintf(name, "SELECT_ITEM_%d", i + 1); + state_config.select_item_codes[i] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + } + + frag_count = config_file_get_int(config, "FRAG_COUNT", 1); + + for (i = 0; i < frag_count; i++) { + sprintf(name, "SELECT_FRAG_%d", i + 1); + state_config.select_frag_codes[i] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + } state_config.src_count = config_file_get_int(config, "SRC_COUNT", 0); - // TODO parse + total = 0; + for (i = 0; i < state_config.src_count; i++) { + sprintf(name, "SRC_%d_ACTIVE_COUNT", i + 1); + state_config.src_active_counts[i] = config_file_get_int(config, name, 0); + state_config.src_active_offsets[i] = total; + total += state_config.src_active_counts[i]; + } + + for (i = 0; i < state_config.src_count; i++) { + for (j = 0; j < state_config.src_active_counts[i]; j++) { + sprintf(name, "SRC_%d_ACTIVE_%d", i + 1, j + 1); + state_config.src_active_codes[state_config.src_active_offsets[i] + j] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + } + } + + total = 0; + for (i = 0; i < state_config.src_count; i++) { + sprintf(name, "SRC_%d_COUNT", i + 1); + state_config.src_subcounts[i] = config_file_get_int(config, name, 0); + state_config.src_offsets[i] = total; + total += state_config.src_subcounts[i]; + } + + for (i = 0; i < state_config.src_count; i++) { + offset = state_config.src_offsets[i]; + for (j = 0; j < state_config.src_subcounts[i]; j++) { + sprintf(name, "SRC_%d_%d_X", i + 1, j + 1); + state_config.src_active_codes[(offset + j) * 3] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + + sprintf(name, "SRC_%d_%d_Y", i + 1, j + 1); + state_config.src_active_codes[(offset + j) * 3 + 1] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + + sprintf(name, "SRC_%d_%d_Z", i + 1, j + 1); + state_config.src_active_codes[(offset + j) * 3 + 2] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + } + } state_config.fader_count = config_file_get_int(config, "FADER_COUNT", 0); - // TODO parse + for (i = 0; i < state_config.fader_count; i++) { + sprintf(name, "FADER_%d", i + 1); + state_config.fader_codes[i] = + config_file_get_int(config, name, UNSET_MIDI_CODE); + } state_config.tap_tempo_code = config_file_get_int(config, "TAP_TEMPO", UNSET_MIDI_CODE); @@ -31,17 +92,12 @@ StateConfig state_parse_config(ConfigFile config) { return state_config; } -void state_free_config(StateConfig state_config) { - // TODO -} - void state_randomize(SharedContext *context, StateConfig state_config, unsigned int state_count) { unsigned int i; for (i = 0; i < state_count; i++) { - context->state[i] = - rand_uint(state_config.select_page_count * - state_config.select_item_count); // TODO from config + context->state[i] = rand_uint(state_config.select_page_count * + state_config.select_item_count); } } \ No newline at end of file diff --git a/src/state.h b/src/state.h index 62199de..f7f4e6d 100644 --- a/src/state.h +++ b/src/state.h @@ -8,6 +8,4 @@ StateConfig state_parse_config(ConfigFile config); void state_randomize(SharedContext *context, StateConfig state_config, unsigned int state_count); -void state_free_config(StateConfig state_config); - #endif /* STATE_H */ \ No newline at end of file diff --git a/src/types.h b/src/types.h index d329314..596b6a2 100644 --- a/src/types.h +++ b/src/types.h @@ -64,36 +64,36 @@ typedef struct ShaderProgram { GLuint vertex_array[2]; unsigned int tex_count; - GLuint *textures; + GLuint textures[ARRAY_SIZE]; unsigned int frag_count; unsigned int frag_output_index; unsigned int frag_monitor_index; - GLuint *programs; + GLuint programs[ARRAY_SIZE]; - GLuint *frame_buffers; - GLuint *fragment_shaders; + GLuint frame_buffers[ARRAY_SIZE]; + GLuint fragment_shaders[ARRAY_SIZE]; - GLuint *itime_locations; - GLuint *itempo_locations; - GLuint *ifps_locations; - GLuint *ires_locations; - GLuint *itexres_locations; - GLuint *iinres_locations; - GLuint *iinfmt_locations; - GLuint *iinfps_locations; - GLuint *idemo_locations; - GLuint *iseed_locations; - GLuint *istate_locations; + GLuint itime_locations[ARRAY_SIZE]; + GLuint itempo_locations[ARRAY_SIZE]; + GLuint ifps_locations[ARRAY_SIZE]; + GLuint ires_locations[ARRAY_SIZE]; + GLuint itexres_locations[ARRAY_SIZE]; + GLuint iinres_locations[ARRAY_SIZE]; + GLuint iinfmt_locations[ARRAY_SIZE]; + GLuint iinfps_locations[ARRAY_SIZE]; + GLuint idemo_locations[ARRAY_SIZE]; + GLuint iseed_locations[ARRAY_SIZE]; + GLuint istate_locations[ARRAY_SIZE]; - GLuint *vpos_locations; + GLuint vpos_locations[ARRAY_SIZE]; - GLuint *textures_locations; + GLuint textures_locations[ARRAY_SIZE]; unsigned int sub_type_count; unsigned int sub_variant_count; - GLuint *sub_locations; + GLuint sub_locations[ARRAY_SIZE]; unsigned int in_count; EGLDisplay egl_display; @@ -138,21 +138,23 @@ typedef struct SharedContext { typedef struct StateConfig { unsigned int select_page_count; - unsigned int *select_page_codes; + unsigned int select_page_codes[ARRAY_SIZE]; unsigned int select_item_count; - unsigned int *select_item_codes; + unsigned int select_item_codes[ARRAY_SIZE]; - unsigned int *select_frag_codes; + unsigned int select_frag_codes[ARRAY_SIZE]; unsigned int src_count; - unsigned int *src_active_counts; - unsigned int *src_active; - unsigned int *src_subcounts; - unsigned int *src_codes; + unsigned int src_active_counts[ARRAY_SIZE]; + unsigned int src_active_offsets[ARRAY_SIZE]; + unsigned int src_active_codes[ARRAY_SIZE]; + unsigned int src_subcounts[ARRAY_SIZE]; + unsigned int src_offsets[ARRAY_SIZE]; + unsigned int src_codes[ARRAY_SIZE]; unsigned int fader_count; - unsigned int *fader_codes; + unsigned int fader_codes[ARRAY_SIZE]; unsigned int tap_tempo_code; } StateConfig;