Sat, 02 Aug 2025 13:07:28 +0200
remove the possibility of non-lazy-initializable shader programs
it is still possible to pre-initialize them, but whenever they are used,
they should be re-created when they don't exist anymore
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/src/2d.c Fri Aug 01 18:19:33 2025 +0200 +++ b/src/2d.c Sat Aug 02 13:07:28 2025 +0200 @@ -97,7 +97,7 @@ asc_set_flag_if(flags, ASC_RECTANGLE_SHADER_FLAG_BORDER, border); // Look up and activate the shader - const AscShaderProgram *shader = asc_shader_lookup_or_create( + const AscShaderProgram *shader = asc_shader_lookup( ASC_SHADER_RECTANGLE(flags), asc_rectangle_shader_create, flags ); @@ -230,7 +230,7 @@ asc_set_flag_if(flags, ASC_ELLIPSIS_SHADER_FLAG_BORDER, border); // Look up and activate the shader - const AscShaderProgram *shader = asc_shader_lookup_or_create( + const AscShaderProgram *shader = asc_shader_lookup( ASC_SHADER_ELLIPSIS(flags), asc_ellipsis_shader_create, flags );
--- a/src/ascension/shader.h Fri Aug 01 18:19:33 2025 +0200 +++ b/src/ascension/shader.h Sat Aug 02 13:07:28 2025 +0200 @@ -131,54 +131,23 @@ */ int asc_shader_use(const AscShaderProgram *shader, const AscCamera *camera); - -/** - * Registers a shader under a certain ID. - * - * @param id the custom ID of the shader - * @param create_func the function that creates the shader - * @param create_flags flags passed to create_func - * @return the shader created by the @c create_func or @c NULL when shader compilation failed - * @see asc_shader_lookup() - */ -const AscShaderProgram *asc_shader_register(unsigned int id, asc_shader_create_func create_func, int create_flags); - -/** - * Looks up a shader by ID. - * - * @param id the ID of the shader to look up - * @return the shader or @c NULL if no shader with such ID exists - * @see asc_shader_register() - */ -const AscShaderProgram *asc_shader_lookup(unsigned int id); - /** * Looks up a shader by ID or registers a new one if no shader exists with the specified ID. * - * This function can be used for lazy initialization of shaders. - * * @param id the custom ID of the shader * @param create_func the function to create the shader * @param create_flags flags passed to create_func * @return the found shader or the newly created shader or @c NULL when shader compilation failed - * @see asc_shader_lookup() - * @see asc_shader_register() */ -const AscShaderProgram *asc_shader_lookup_or_create(unsigned int id, asc_shader_create_func create_func, int create_flags); +const AscShaderProgram *asc_shader_lookup(unsigned int id, asc_shader_create_func create_func, int create_flags); /** * Frees all registered shaders. * - * Completely wipes all shaders. - * Be careful with this function when shaders are needed that have been registered with @c asc_shader_register(). - * Those need to be registered again, or @a asc_shader_lookup() will fail. - * Shaders that are used with @a asc_shader_lookup_or_create() will be re-created automatically. + * The still required shaders are re-created when they are used next. * - * TODO: maybe we should add another function that only clears lazy-initialized shaders - * another alternative would be to keep the initialization info and lazy-initialize cleared shaders on lookup - * - * @see asc_shader_lookup_or_create() + * @see asc_shader_lookup() */ void asc_shader_clear_registry(void);
--- a/src/shader.c Fri Aug 01 18:19:33 2025 +0200 +++ b/src/shader.c Sat Aug 02 13:07:28 2025 +0200 @@ -237,40 +237,24 @@ return asc_error_catch_gl("Activating shader"); } -const AscShaderProgram *asc_shader_register(unsigned int id, asc_shader_create_func create_func, int create_flags) { - AscGLContext *glctx = asc_active_glctx; -#ifndef NDEBUG - { - const AscShaderProgram *prog = asc_shader_lookup(id); - if (prog != NULL) { - asc_error("Shader program %u already exists. This is a bug!", id); - // still return it, so that the caller does not die immediately - return prog; +const AscShaderProgram *asc_shader_lookup(unsigned int id, asc_shader_create_func create_func, int create_flags) { + AscShaderProgram *prog = NULL; + CxIterator iter = cxListIterator(asc_active_glctx->shaders); + cx_foreach(AscShaderProgram *, p, iter) { + if (p->id == id) { + prog = p; + break; } } -#endif - AscShaderProgram *prog = create_func(create_flags); if (prog == NULL) { - // create an empty program to prevent future loading attempts - prog = cxZallocDefault(sizeof(AscShaderProgram)); - } - prog->id = id; - cxListAdd(glctx->shaders, prog); - return prog; -} - -const AscShaderProgram *asc_shader_lookup(unsigned int id) { - CxIterator iter = cxListIterator(asc_active_glctx->shaders); - cx_foreach(const AscShaderProgram *, prog, iter) { - if (prog->id == id) return prog; - } - return NULL; -} - -const AscShaderProgram *asc_shader_lookup_or_create(unsigned int id, asc_shader_create_func create_func, int create_flags) { - const AscShaderProgram *prog = asc_shader_lookup(id); - if (prog == NULL) { - return asc_shader_register(id, create_func, create_flags); + AscGLContext *glctx = asc_active_glctx; + prog = create_func(create_flags); + if (prog == NULL) { + // create an empty program to prevent future loading attempts + prog = cxZallocDefault(sizeof(AscShaderProgram)); + } + prog->id = id; + cxListAdd(glctx->shaders, prog); } return prog; }
--- a/src/sprite.c Fri Aug 01 18:19:33 2025 +0200 +++ b/src/sprite.c Sat Aug 02 13:07:28 2025 +0200 @@ -87,7 +87,7 @@ // Activate shader // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes - const AscShaderProgram *shader = asc_shader_lookup_or_create( + const AscShaderProgram *shader = asc_shader_lookup( ASC_SHADER_SPRITE(flags), asc_sprite_shader_create, flags );
--- a/src/text.c Fri Aug 01 18:19:33 2025 +0200 +++ b/src/text.c Sat Aug 02 13:07:28 2025 +0200 @@ -111,7 +111,7 @@ // Activate shader // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes - const AscShaderProgram *shader = asc_shader_lookup_or_create( + const AscShaderProgram *shader = asc_shader_lookup( ASC_SHADER_TEXT(0), asc_text_shader_create, 0 );