From f3ee854de0cda0e9da5c06a9998195583cd5ae43 Mon Sep 17 00:00:00 2001 From: klemek Date: Fri, 19 Sep 2025 00:22:22 +0200 Subject: [PATCH] bind any number of textures (i'm unstoppable) --- shaders/frag0.glsl | 1 + shaders/frag6.glsl | 2 +- shaders/frag8.glsl | 5 +++-- shaders/shaders.cfg | 5 ++++- src/config.h | 4 ---- src/forge.c | 2 ++ src/shaders.c | 31 ++++++++++++++++--------------- src/types.h | 8 +++----- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/shaders/frag0.glsl b/shaders/frag0.glsl index f5d158d..6a8717c 100644 --- a/shaders/frag0.glsl +++ b/shaders/frag0.glsl @@ -22,6 +22,7 @@ uniform sampler2D tex4; uniform sampler2D tex5; uniform sampler2D tex6; uniform sampler2D tex7; +uniform sampler2D tex8; // 3. definitions // -------------- diff --git a/shaders/frag6.glsl b/shaders/frag6.glsl index c0f86c3..0c46af0 100644 --- a/shaders/frag6.glsl +++ b/shaders/frag6.glsl @@ -8,5 +8,5 @@ in vec2 vUV; out vec4 fragColor; void main() { - fragColor = fx_stage(vUV, tex7, tex0); + fragColor = fx_stage(vUV, tex8, tex0); } \ No newline at end of file diff --git a/shaders/frag8.glsl b/shaders/frag8.glsl index ffb132b..4c08688 100644 --- a/shaders/frag8.glsl +++ b/shaders/frag8.glsl @@ -18,6 +18,7 @@ void main() { fragColor += s(uv,0,1) * texture(tex4, uv); fragColor += s(uv,1,1) * texture(tex5, uv); fragColor += s(uv,2,1) * texture(tex6, uv); - fragColor += s(uv,0.5,0) * texture(tex7, uv-vec2(0.5,0)); - fragColor += s(uv,1.5,0) * texture(tex0, uv-vec2(0.5,0)); + fragColor += s(uv,0,0) * texture(tex7, uv); + fragColor += s(uv,1,0) * texture(tex8, uv); + fragColor += s(uv,2,0) * texture(tex0, uv); } \ No newline at end of file diff --git a/shaders/shaders.cfg b/shaders/shaders.cfg index b788cf2..deabf32 100644 --- a/shaders/shaders.cfg +++ b/shaders/shaders.cfg @@ -2,6 +2,9 @@ UNIFORM_TIME=iTime UNIFORM_TEMPO=iTempo UNIFORM_FPS=iFPS UNIFORM_RESOLUTION=iResolution +UNIFORM_TEX_PREFIX=tex + +TEX_COUNT=9 FRAG_COUNT=8 FRAG_OUTPUT=7 @@ -10,7 +13,7 @@ FRAG_1_OUT=2 FRAG_2_OUT=5 FRAG_3_OUT=3 FRAG_4_OUT=6 -FRAG_5_OUT=7 +FRAG_5_OUT=8 FRAG_6_OUT=0 SUB_TYPE_COUNT=3 diff --git a/src/config.h b/src/config.h index e31e386..96d15d3 100644 --- a/src/config.h +++ b/src/config.h @@ -9,10 +9,6 @@ #define VERSION "(dev)" #endif /* VERSION */ -#ifndef TEX_COUNT -#define TEX_COUNT 8 -#endif /* TEXT_COUNT */ - #ifndef SUB_COUNT #define SUB_COUNT 16 #endif /* SUB_COUNT */ diff --git a/src/forge.c b/src/forge.c index ebdd089..c224ffd 100644 --- a/src/forge.c +++ b/src/forge.c @@ -154,6 +154,8 @@ void forge_run(Parameters params) { timer = timer_init(30); + log_success("Initialized"); + while (!window_should_close(window)) { loop(window, program, params.hot_reload, &common_shader_code, fragment_shaders, &timer); diff --git a/src/shaders.c b/src/shaders.c index fc2d156..c547d51 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -39,9 +39,11 @@ static bool compile_shader(GLuint shader_id, char *name, char *source_code) { static void init_textures(ShaderProgram *program, Context context) { unsigned int i; - glGenTextures(TEX_COUNT, program->textures); + program->textures = malloc(program->tex_count * sizeof(GLuint)); - for (i = 0; i < TEX_COUNT; i++) { + glGenTextures(program->tex_count, program->textures); + + for (i = 0; i < program->tex_count; i++) { // selects which texture unit subsequent texture state calls will affect glActiveTexture(GL_TEXTURE0 + i); @@ -61,7 +63,7 @@ static void init_textures(ShaderProgram *program, Context context) { static void init_framebuffers(ShaderProgram *program, ConfigFile shader_config) { - unsigned int i, j; + unsigned int i; unsigned tex_i; char name[32]; @@ -135,6 +137,7 @@ static void init_single_program(ShaderProgram *program, unsigned int i, ConfigFile shader_config) { unsigned int j; char name[32]; + char *tex_prefix; program->programs[i] = glCreateProgram(); @@ -172,9 +175,10 @@ static void init_single_program(ShaderProgram *program, unsigned int i, } // create texX uniforms pointer - for (j = 0; j < TEX_COUNT; j++) { - sprintf(name, "tex%d", j); - program->textures_locations[j][i] = + tex_prefix = config_file_get_str(shader_config, "UNIFORM_TEX_PREFIX", "tex"); + for (j = 0; j < program->tex_count; j++) { + sprintf(name, "%s%d", tex_prefix, j); + program->textures_locations[i * program->frag_count + j] = glGetUniformLocation(program->programs[i], name); } @@ -200,11 +204,8 @@ static void init_programs(ShaderProgram *program, ConfigFile shader_config) { program->ifps_locations = malloc(program->frag_count * sizeof(GLuint)); program->ires_locations = malloc(program->frag_count * sizeof(GLuint)); program->vpos_locations = malloc(program->frag_count * sizeof(GLuint)); - - for (i = 0; i < TEX_COUNT; i++) { - program->textures_locations[i] = - malloc(program->frag_count * sizeof(GLuint)); - } + program->textures_locations = + malloc(program->frag_count * program->tex_count * sizeof(GLuint)); for (i = 0; i < program->frag_count; i++) { init_single_program(program, i, shader_config); @@ -218,8 +219,8 @@ ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config, program.error = false; program.last_width = context.width; program.last_height = context.height; + program.tex_count = config_file_get_int(shader_config, "TEX_COUNT", 9); program.frag_count = config_file_get_int(shader_config, "FRAG_COUNT", 6); - program.framebuffer_count = program.frag_count - 2; program.frag_output_index = config_file_get_int(shader_config, "FRAG_OUT", 0) - 1; program.frag_monitor_index = @@ -266,7 +267,7 @@ static void update_viewport(ShaderProgram program, Context context) { glViewport(0, 0, context.width, context.height); // clean and resize all textures - for (i = 0; i < TEX_COUNT; i++) { + for (i = 0; i < program.tex_count; i++) { glActiveTexture(GL_TEXTURE0 + i); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context.width, context.height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -312,8 +313,8 @@ static void use_program(ShaderProgram program, int i, bool output, glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 3, subroutines); // set GL_TEXTURE(X) to uniform sampler2D texX - for (j = 0; j < TEX_COUNT; j++) { - glUniform1i(program.textures_locations[j][i], j); + for (j = 0; j < program.tex_count; j++) { + glUniform1i(program.textures_locations[i * program.frag_count + j], j); } // draw output diff --git a/src/types.h b/src/types.h index 4d59a9d..217e9fd 100644 --- a/src/types.h +++ b/src/types.h @@ -39,7 +39,7 @@ typedef struct ShaderProgram { unsigned int frag_output_index; unsigned int frag_monitor_index; - unsigned int framebuffer_count; + unsigned int tex_count; GLuint *programs; @@ -52,7 +52,7 @@ typedef struct ShaderProgram { GLuint *ifps_locations; GLuint *ires_locations; - GLuint *textures_locations[TEX_COUNT]; + GLuint *textures_locations; GLuint sub_src_indexes[8][SUB_COUNT]; // TODO change GLuint sub_fx_indexes[8][SUB_COUNT]; @@ -64,9 +64,7 @@ typedef struct ShaderProgram { GLuint vertex_array; GLuint *frame_buffers; - GLuint textures[TEX_COUNT]; - - GLenum draw_buffers[TEX_COUNT]; + GLuint *textures; } ShaderProgram; typedef GLFWwindow Window;