bind any number of textures (i'm unstoppable)

This commit is contained in:
2025-09-19 00:22:22 +02:00
parent 1cf3c7eb50
commit f3ee854de0
8 changed files with 30 additions and 28 deletions
+1
View File
@@ -22,6 +22,7 @@ uniform sampler2D tex4;
uniform sampler2D tex5;
uniform sampler2D tex6;
uniform sampler2D tex7;
uniform sampler2D tex8;
// 3. definitions
// --------------
+1 -1
View File
@@ -8,5 +8,5 @@ in vec2 vUV;
out vec4 fragColor;
void main() {
fragColor = fx_stage(vUV, tex7, tex0);
fragColor = fx_stage(vUV, tex8, tex0);
}
+3 -2
View File
@@ -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);
}
+4 -1
View File
@@ -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
-4
View File
@@ -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 */
+2
View File
@@ -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);
+16 -15
View File
@@ -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
+3 -5
View File
@@ -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;