--- a/src/shader.c Tue Jun 10 19:29:07 2025 +0200 +++ b/src/shader.c Wed Jun 11 23:38:55 2025 +0200 @@ -157,6 +157,13 @@ } void asc_shader_use(const AscShaderProgram *shader, const AscCamera *camera) { + if (shader == NULL) { + asc_active_glctx->active_program = 0; + glUseProgram(0); + return; + } + if (asc_active_glctx->active_program == shader->gl_id) return; + asc_active_glctx->active_program = shader->gl_id; glUseProgram(shader->gl_id); glUniformMatrix4fv(shader->projection, 1, GL_FALSE, camera->projection); glUniformMatrix4fv(shader->view, 1, GL_FALSE, camera->view); @@ -195,33 +202,34 @@ cxFreeDefault(codes.frag); } -AscShaderProgram *asc_shader_register(unsigned int id, asc_shader_create_func create_func) { +const void *asc_shader_register(unsigned int id, asc_shader_create_func create_func) { AscGLContext *glctx = asc_active_glctx; - AscShaderProgram *prog = NULL; #ifndef NDEBUG - 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 *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; + } } #endif - prog = create_func(); + AscShaderProgram *prog = create_func(); prog->id = id; cxListAdd(glctx->shaders, prog); return prog; } -AscShaderProgram *asc_shader_lookup(unsigned int id) { +const void *asc_shader_lookup(unsigned int id) { CxIterator iter = cxListIterator(asc_active_glctx->shaders); - cx_foreach(AscShaderProgram *, prog, iter) { + cx_foreach(const AscShaderProgram *, prog, iter) { if (prog->id == id) return prog; } return NULL; } -AscShaderProgram *asc_shader_lookup_or_create(unsigned int id, asc_shader_create_func create_func) { - AscShaderProgram *prog = asc_shader_lookup(id); +const void *asc_shader_lookup_or_create(unsigned int id, asc_shader_create_func create_func) { + const AscShaderProgram *prog = asc_shader_lookup(id); if (prog == NULL) { return asc_shader_register(id, create_func); } @@ -230,4 +238,6 @@ void asc_shader_clear_registry(void) { cxListClear(asc_active_glctx->shaders); + // also clear the active program to avoid accidental matches with newly created shaders + asc_shader_use(NULL, NULL); }