# HG changeset patch # User Mike Becker # Date 1749467921 -7200 # Node ID d190fe5315bd09753d65cca899ae714471aeeed9 # Parent 5d655459db85bb6632c2b9eb0978c8f372411c72 add dynamic reload of all shaders diff -r 5d655459db85 -r d190fe5315bd src/ascension/shader.h --- a/src/ascension/shader.h Sun Jun 08 14:58:19 2025 +0200 +++ b/src/ascension/shader.h Mon Jun 09 13:18:41 2025 +0200 @@ -123,11 +123,61 @@ */ void asc_shader_free(AscShaderProgram *program); +/** + * Activates a shader for use. + * + * @param shader the shader program to use + * @param camera the camera matrices (view/projection) to upload + */ void 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 + * @return the shader created by the @c create_func + * @see asc_shader_lookup() + */ AscShaderProgram *asc_shader_register(unsigned int id, asc_shader_create_func create_func); + +/** + * 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() + */ 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 + * @return the found shader or the newly created shader + * @see asc_shader_lookup() + * @see asc_shader_register() + */ AscShaderProgram *asc_shader_lookup_or_create(unsigned int id, asc_shader_create_func create_func); + +/** + * 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. + * + * 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() + */ +void asc_shader_clear_registry(void); + #endif //ASCENSION_SHADER_H diff -r 5d655459db85 -r d190fe5315bd src/shader.c --- a/src/shader.c Sun Jun 08 14:58:19 2025 +0200 +++ b/src/shader.c Mon Jun 09 13:18:41 2025 +0200 @@ -226,4 +226,8 @@ return asc_shader_register(id, create_func); } return prog; -} \ No newline at end of file +} + +void asc_shader_clear_registry(void) { + cxListClear(asc_active_glctx->shaders); +} diff -r 5d655459db85 -r d190fe5315bd test/snake/snake.c --- a/test/snake/snake.c Sun Jun 08 14:58:19 2025 +0200 +++ b/test/snake/snake.c Mon Jun 09 13:18:41 2025 +0200 @@ -206,6 +206,13 @@ asc_context_quit(); } + // debug-key for clearing the shader registry + if (asc_key_pressed(S)) { + // TODO: implement edge-triggered key press handling + asc_shader_clear_registry(); + asc_dprintf("Shader cache cleared."); + } + // quit application on ESC key press if (asc_key_pressed(ESCAPE)) { asc_context_quit();