From 42b5f1997cdd5e99645e24dca6cb89cc7b081a09 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 1 Nov 2025 18:23:08 -0700 Subject: Add support for alpha mode --- src/core/shader_program.c | 119 ++++++++++++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 42 deletions(-) (limited to 'src/core') diff --git a/src/core/shader_program.c b/src/core/shader_program.c index 3cbe48d..eeb46f8 100644 --- a/src/core/shader_program.c +++ b/src/core/shader_program.c @@ -72,17 +72,23 @@ void gfx_deactivate_shader_program(const ShaderProgram* prog) { ASSERT_GL; } -static void set_texture_uniform( - GLuint prog, const char* name, int texture_unit, const Texture* texture) { +static void set_int_uniform(GLuint prog, const char* name, int value) { assert(prog != 0); assert(name); - assert(texture); const GLint location = glGetUniformLocation(prog, name); if (location >= 0) { - glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(texture->target, texture->id); - glUniform1i(location, texture_unit); + glUniform1i(location, value); + } +} + +static void set_float_uniform(GLuint prog, const char* name, float value) { + assert(prog != 0); + assert(name); + + const GLint location = glGetUniformLocation(prog, name); + if (location >= 0) { + glUniform1f(location, value); } } @@ -118,13 +124,17 @@ static void set_vec4_uniform(GLuint prog, const char* name, vec4 value) { } } -static void set_float_uniform(GLuint prog, const char* name, float value) { +static void set_texture_uniform( + GLuint prog, const char* name, int texture_unit, const Texture* texture) { assert(prog != 0); assert(name); + assert(texture); const GLint location = glGetUniformLocation(prog, name); if (location >= 0) { - glUniform1f(location, value); + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(texture->target, texture->id); + glUniform1i(location, texture_unit); } } @@ -135,23 +145,30 @@ void gfx_apply_uniforms(const ShaderProgram* prog) { for (int i = 0; i < prog->num_uniforms; ++i) { const ShaderUniform* uniform = &prog->uniforms[i]; switch (uniform->type) { - case UniformTexture: - set_texture_uniform( - prog->id, uniform->name.str, next_texture_unit, - uniform->value.texture); - next_texture_unit++; + case UniformInt: + set_int_uniform(prog->id, uniform->name.str, uniform->value.uniform_int); + break; + case UniformFloat: + set_float_uniform( + prog->id, uniform->name.str, uniform->value.uniform_float); break; case UniformMat4: - set_mat4_uniform(prog->id, uniform->name.str, &uniform->value.mat4, 1); + set_mat4_uniform( + prog->id, uniform->name.str, &uniform->value.uniform_mat4, 1); break; case UniformVec3: - set_vec3_uniform(prog->id, uniform->name.str, uniform->value.vec3); + set_vec3_uniform( + prog->id, uniform->name.str, uniform->value.uniform_vec3); break; case UniformVec4: - set_vec4_uniform(prog->id, uniform->name.str, uniform->value.vec4); + set_vec4_uniform( + prog->id, uniform->name.str, uniform->value.uniform_vec4); break; - case UniformFloat: - set_float_uniform(prog->id, uniform->name.str, uniform->value.scalar); + case UniformTexture: + set_texture_uniform( + prog->id, uniform->name.str, next_texture_unit, + uniform->value.texture); + next_texture_unit++; break; case UniformMat4Array: set_mat4_uniform( @@ -179,8 +196,9 @@ static ShaderUniform* get_or_allocate_uniform( // Create the uniform if it does not exist. if (prog->num_uniforms == GFX_MAX_UNIFORMS_PER_SHADER) { - FAIL("Exceeded the maximum number of uniforms per shader. Please increase " - "this value."); + FAIL( + "Exceeded the maximum number of uniforms per shader. Please increase " + "this value."); return 0; } ShaderUniform* uniform = &prog->uniforms[prog->num_uniforms]; @@ -191,21 +209,38 @@ static ShaderUniform* get_or_allocate_uniform( // The functions below save the value of a uniform in the shader program. If the // uniform does not even exist, then there is no need to store the value. -void gfx_set_texture_uniform( - ShaderProgram* prog, const char* name, const Texture* texture) { +void gfx_set_int_uniform(ShaderProgram* prog, const char* name, int value) { assert(prog); assert(name); - assert(texture); + // No need to store the uniform on our side if it does not exist in the + // program. const GLint location = glGetUniformLocation(prog->id, name); if (location < 0) { return; } ShaderUniform* uniform = get_or_allocate_uniform(prog, name); assert(uniform); - uniform->name = sstring_make(name); - uniform->type = UniformTexture; - uniform->value.texture = texture; + uniform->name = sstring_make(name); + uniform->type = UniformInt; + uniform->value.uniform_int = value; +} + +void gfx_set_float_uniform(ShaderProgram* prog, const char* name, float value) { + assert(prog); + assert(name); + + // No need to store the uniform on our side if it does not exist in the + // program. + const GLint location = glGetUniformLocation(prog->id, name); + if (location < 0) { + return; + } + ShaderUniform* uniform = get_or_allocate_uniform(prog, name); + assert(uniform); + uniform->name = sstring_make(name); + uniform->type = UniformFloat; + uniform->value.uniform_float = value; } void gfx_set_mat4_uniform( @@ -220,9 +255,9 @@ void gfx_set_mat4_uniform( } ShaderUniform* uniform = get_or_allocate_uniform(prog, name); assert(uniform); - uniform->name = sstring_make(name); - uniform->type = UniformMat4; - uniform->value.mat4 = *mat; + uniform->name = sstring_make(name); + uniform->type = UniformMat4; + uniform->value.uniform_mat4 = *mat; } void gfx_set_vec3_uniform(ShaderProgram* prog, const char* name, vec3 value) { @@ -235,9 +270,9 @@ void gfx_set_vec3_uniform(ShaderProgram* prog, const char* name, vec3 value) { } ShaderUniform* uniform = get_or_allocate_uniform(prog, name); assert(uniform); - uniform->name = sstring_make(name); - uniform->type = UniformVec3; - uniform->value.vec3 = value; + uniform->name = sstring_make(name); + uniform->type = UniformVec3; + uniform->value.uniform_vec3 = value; } void gfx_set_vec4_uniform(ShaderProgram* prog, const char* name, vec4 value) { @@ -250,26 +285,26 @@ void gfx_set_vec4_uniform(ShaderProgram* prog, const char* name, vec4 value) { } ShaderUniform* uniform = get_or_allocate_uniform(prog, name); assert(uniform); - uniform->name = sstring_make(name); - uniform->type = UniformVec4; - uniform->value.vec4 = value; + uniform->name = sstring_make(name); + uniform->type = UniformVec4; + uniform->value.uniform_vec4 = value; } -void gfx_set_float_uniform(ShaderProgram* prog, const char* name, float value) { +void gfx_set_texture_uniform( + ShaderProgram* prog, const char* name, const Texture* texture) { assert(prog); assert(name); + assert(texture); - // No need to store the uniform on our side if it does not exist in the - // program. const GLint location = glGetUniformLocation(prog->id, name); if (location < 0) { return; } ShaderUniform* uniform = get_or_allocate_uniform(prog, name); assert(uniform); - uniform->name = sstring_make(name); - uniform->type = UniformFloat; - uniform->value.scalar = value; + uniform->name = sstring_make(name); + uniform->type = UniformTexture; + uniform->value.texture = texture; } void gfx_set_mat4_array_uniform( @@ -277,7 +312,7 @@ void gfx_set_mat4_array_uniform( assert(prog); assert(name); assert(mats); - + const GLint location = glGetUniformLocation(prog->id, name); if (location < 0) { return; -- cgit v1.2.3