Tue, 19 Aug 2025 18:51:46 +0200
further improve names and docu of the uniform location init functions
demo/snake/snake.c | file | annotate | diff | comparison | revisions | |
src/2d.c | file | annotate | diff | comparison | revisions | |
src/ascension/shader.h | file | annotate | diff | comparison | revisions | |
src/shader.c | file | annotate | diff | comparison | revisions | |
src/sprite.c | file | annotate | diff | comparison | revisions | |
src/text.c | file | annotate | diff | comparison | revisions |
--- a/demo/snake/snake.c Tue Aug 19 18:26:20 2025 +0200 +++ b/demo/snake/snake.c Tue Aug 19 18:51:46 2025 +0200 @@ -239,9 +239,9 @@ } PlayerShader; static void player_shader_init(AscShaderProgram *p, cx_attr_unused int flags) { - asc_shader_init_uniform_by_name(p, PlayerShader, map_albedo); - asc_shader_init_uniform_by_name(p, PlayerShader, map_color); - asc_shader_init_uniform_by_name(p, PlayerShader, color); + asc_shader_set_uniform_loc_by_name(p, PlayerShader, map_albedo); + asc_shader_set_uniform_loc_by_name(p, PlayerShader, map_color); + asc_shader_set_uniform_loc_by_name(p, PlayerShader, color); } static AscShaderProgram *player_shader_create(cx_attr_unused int unused) {
--- a/src/2d.c Tue Aug 19 18:26:20 2025 +0200 +++ b/src/2d.c Tue Aug 19 18:51:46 2025 +0200 @@ -47,16 +47,16 @@ #define ASC_RECTANGLE_SHADER_FLAG_BORDER 4 static void asc_rectangle_shader_init(AscShaderProgram *shader, int flags) { - asc_shader_init_uniform_by_name(shader, AscRectangleShader, size); + asc_shader_set_uniform_loc_by_name(shader, AscRectangleShader, size); if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_FILL)) { - asc_shader_init_uniform_by_name(shader, AscRectangleShader, color); + asc_shader_set_uniform_loc_by_name(shader, AscRectangleShader, color); } if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_BORDER)) { - asc_shader_init_uniform_by_name(shader, AscRectangleShader, border_color); - asc_shader_init_uniform_by_name(shader, AscRectangleShader, thickness); + asc_shader_set_uniform_loc_by_name(shader, AscRectangleShader, border_color); + asc_shader_set_uniform_loc_by_name(shader, AscRectangleShader, thickness); } if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_ROUND)) { - asc_shader_init_uniform_by_name(shader, AscRectangleShader, radius); + asc_shader_set_uniform_loc_by_name(shader, AscRectangleShader, radius); } } @@ -207,13 +207,13 @@ #define ASC_ELLIPSIS_SHADER_FLAG_BORDER 2 static void asc_ellipsis_shader_init(AscShaderProgram *shader, int flags) { - asc_shader_init_uniform_by_name(shader, AscEllipsisShader, radii); + asc_shader_set_uniform_loc_by_name(shader, AscEllipsisShader, radii); if (asc_test_flag(flags, ASC_ELLIPSIS_SHADER_FLAG_FILL)) { - asc_shader_init_uniform_by_name(shader, AscEllipsisShader, color); + asc_shader_set_uniform_loc_by_name(shader, AscEllipsisShader, color); } if (asc_test_flag(flags, ASC_ELLIPSIS_SHADER_FLAG_BORDER)) { - asc_shader_init_uniform_by_name(shader, AscEllipsisShader, thickness); - asc_shader_init_uniform_by_name(shader, AscEllipsisShader, border_color); + asc_shader_set_uniform_loc_by_name(shader, AscEllipsisShader, thickness); + asc_shader_set_uniform_loc_by_name(shader, AscEllipsisShader, border_color); } }
--- a/src/ascension/shader.h Tue Aug 19 18:26:20 2025 +0200 +++ b/src/ascension/shader.h Tue Aug 19 18:51:46 2025 +0200 @@ -95,7 +95,7 @@ * * To be used with asc_shader_create(). * @see asc_shader_create() - * @see asc_shader_init_uniform() + * @see asc_shader_set_uniform_loc() */ typedef void(*asc_shader_init_func)(AscShaderProgram*, int); @@ -151,30 +151,40 @@ */ void asc_shader_clear_registry(void); +/** + * Gets the location of a uniform as an integer. + * + * Usually you will not need this function. Instead, it is recommended to create a custom + * shader program struct which uses AscShaderProgram as the base struct and then add the uniform locations + * as fields to that struct. + * Then you can use asc_shader_set_uniform_loc_by_name() to initialize all uniform locations. + * + * @param shader a pointer to the shader struct + * @param name the name of the uniform + * @return the uniform's location + */ asc_uniform_loc asc_shader_get_uniform_loc(const AscShaderProgram *shader, const char *name); /** + * Gets the location of a uniform as an integer and stores it directly in the struct. + * + * @param shader a pointer to the shader struct + * @param mem_offset the offset of the @c asc_uniform_loc field in the shader program struct + * @param name the name of the uniform + */ +void asc_shader_set_uniform_loc(AscShaderProgram *shader, off_t mem_offset, const char *name); + +/** * Gets the location of a uniform as an integer and stores it in the struct. * - * This is a convenient wrapper for asc_shader_get_uniform_loc(). + * Use this macro for asc_shader_set_uniform_loc() when the name in your struct is identical to the uniform's name. * - * @param shader the shader program struct - * @param mem_offset the offset of the int field in the shader program struct - * @param name the name of the uniform - */ -void asc_shader_init_uniform(AscShaderProgram *shader, off_t mem_offset, const char *name); - -/** - * Gets the location of a uniform as an integer and stores it in the struct. - * - * This is a nicer, but less flexible, wrapper for asc_shader_get_uniform_loc(). - * - * @param s a pointer to the shader struct + * @param shader a pointer to the shader struct * @param type_name the type name of the shader struct * @param field_name the field name which must match the uniform's name */ -#define asc_shader_init_uniform_by_name(s, type_name, field_name) \ - asc_shader_init_uniform(s, offsetof(type_name, field_name), #field_name) +#define asc_shader_set_uniform_loc_by_name(shader, type_name, field_name) \ + asc_shader_set_uniform_loc(shader, offsetof(type_name, field_name), #field_name) void asc_shader_upload_model_matrix(const AscShaderProgram *shader, const AscSceneNode *node);
--- a/src/shader.c Tue Aug 19 18:26:20 2025 +0200 +++ b/src/shader.c Tue Aug 19 18:51:46 2025 +0200 @@ -269,7 +269,7 @@ return glGetUniformLocation(shader->gl_id, name); } -void asc_shader_init_uniform(AscShaderProgram *shader, off_t mem_offset, const char *name) { +void asc_shader_set_uniform_loc(AscShaderProgram *shader, off_t mem_offset, const char *name) { *((asc_uniform_loc*)((char *) shader + mem_offset)) = glGetUniformLocation(shader->gl_id, name); }
--- a/src/sprite.c Tue Aug 19 18:26:20 2025 +0200 +++ b/src/sprite.c Tue Aug 19 18:51:46 2025 +0200 @@ -41,7 +41,7 @@ } AscSpriteShader; static void asc_sprite_shader_init(AscShaderProgram *p, cx_attr_unused int flags) { - asc_shader_init_uniform_by_name(p, AscSpriteShader, tex); + asc_shader_set_uniform_loc_by_name(p, AscSpriteShader, tex); } static AscShaderProgram *asc_sprite_shader_create(int rect) {
--- a/src/text.c Tue Aug 19 18:26:20 2025 +0200 +++ b/src/text.c Tue Aug 19 18:51:46 2025 +0200 @@ -42,7 +42,7 @@ } AscTextShader; static void asc_text_shader_init(AscShaderProgram *p, cx_attr_unused int flags) { - asc_shader_init_uniform_by_name(p, AscTextShader, tex); + asc_shader_set_uniform_loc_by_name(p, AscTextShader, tex); } static AscShaderProgram *asc_text_shader_create(int flags) {