add dynamic reload of all shaders

Mon, 09 Jun 2025 13:18:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 09 Jun 2025 13:18:41 +0200
changeset 140
d190fe5315bd
parent 139
5d655459db85
child 141
cd82643bb6d9

add dynamic reload of all shaders

src/ascension/shader.h file | annotate | diff | comparison | revisions
src/shader.c file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- 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
--- 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);
+}
--- 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();

mercurial