Sun, 08 Jun 2025 14:58:19 +0200
refactor shader management - resolves #684
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2023 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "ascension/context.h" #include "ascension/error.h" #include "ascension/shader.h" #include "ascension/filesystem.h" #include <string.h> #include <stdio.h> #include <GL/glew.h> #include <cx/buffer.h> #include <cx/streams.h> /** * Compiles a shader from the given source code. * * The ID of the returned shader will be zero when something went wrong. * * @param type the shader type (use the GL enum) * @param code the source code * @param code_pp the optional preprocessor code * @return the compiled shader */ static unsigned asc_shader_compile(unsigned int type, const char *code, const char *code_pp) { GLuint id = glCreateShader(type); if (id == 0) { asc_error("glCreateShader failed: %s", glGetError()); return 0; } GLint success; const char *code_array[4]; GLint length_array[4]; #define store_str(i, s) do {cxstring cxs = cx_str(s); code_array[i] = cxs.ptr; length_array[i] = (GLint) cxs.length;} while(0) store_str(0, "#version 400 core\n"); store_str(1, code_pp); store_str(2, "\n#line 1\n"); store_str(3, code); #undef store_str // compile glShaderSource(id, cx_nmemb(length_array), code_array, length_array); glCompileShader(id); glGetShaderiv(id, GL_COMPILE_STATUS, &success); if (success) { asc_dprintf("Shader %u compiled", id); return id; } else { char *log = malloc(1024); glGetShaderInfoLog(id, 1024, NULL, log); glDeleteShader(id); asc_error("Shader %u compilation failed.\n%s", id, log); free(log); return 0; } } /** * This function links shaders into a program. * * The ID of the returned program will be zero when something went wrong. * * @param shader the shader IDs to link * @param n the number of shaders * @param prog the struct where to store the result * @retval zero success * @retval non-zero failure */ static int asc_shader_link(unsigned shader[], unsigned n, AscShaderProgram *prog) { GLint success; GLint id = glCreateProgram(); if (id <= 0) { asc_error("glCreateProgram failed: %s", glGetError()); return -1; } for (unsigned i = 0; i < n; i++) { glAttachShader(id, shader[i]); } glLinkProgram(id); glGetProgramiv(id, GL_LINK_STATUS, &success); for (unsigned i = 0; i < n; i++) { glDeleteShader(shader[i]); } if (success) { asc_dprintf("Shader Program %u linked.", id); prog->gl_id = id; // by convention every shader shall have MVP matrices prog->model = glGetUniformLocation(id, "model"); prog->view = glGetUniformLocation(id, "view"); prog->projection = glGetUniformLocation(id, "projection"); return 0; } else { char *log = malloc(1024); glGetProgramInfoLog(id, 1024, NULL, log); glDeleteProgram(id); asc_error("Linking shader program %u failed.\n%s", id, log); free(log); return -1; } } void asc_shader_free(AscShaderProgram *program) { if (program->gl_id > 0) { asc_dprintf("Delete Shader Program %u", program->gl_id); glDeleteProgram(program->gl_id); } if (program->destr_func) { program->destr_func(program); } cxFreeDefault(program); } void *asc_shader_create(AscShaderCodes codes, size_t mem_size) { AscShaderProgram *prog = cxZallocDefault(mem_size); unsigned shader[2]; unsigned n = 0; if (codes.vtx) { shader[n++] = asc_shader_compile(GL_VERTEX_SHADER, codes.vtx, codes.vtx_pp); } if (codes.frag) { shader[n++] = asc_shader_compile(GL_FRAGMENT_SHADER, codes.frag, codes.frag_pp); } if (asc_shader_link(shader, n, prog)) { cxFreeDefault(prog); prog = NULL; } for (unsigned i = 0; i < n; i++) { asc_dprintf("Delete shader: %u", shader[i]); glDeleteShader(shader[i]); } return prog; } void asc_shader_use(const AscShaderProgram *shader, const AscCamera *camera) { glUseProgram(shader->gl_id); glUniformMatrix4fv(shader->projection, 1, GL_FALSE, camera->projection); glUniformMatrix4fv(shader->view, 1, GL_FALSE, camera->view); } static int asc_shader_load_code_file(const char *filename, char **code) { if (filename == NULL) { *code = NULL; return 0; } cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.shader_path), cx_str(filename)); asc_dprintf("Load shader code from %" CX_PRIstr, CX_SFMT(fpath)); FILE *f = fopen(fpath.ptr, "r"); cx_strfree(&fpath); if (f == NULL) return -1; CxBuffer buffer; cxBufferInit(&buffer, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND); cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc); cxBufferPut(&buffer, '\0'); cxBufferShrink(&buffer, 0); *code = buffer.space; return *code == NULL ? -1 : 0; } int asc_shader_load_code_files(AscShaderCodeInfo info, AscShaderCodes *codes) { int ret = 0; ret |= asc_shader_load_code_file(info.files.vtx, &codes->vtx); codes->vtx_pp = info.defines.vtx; ret |= asc_shader_load_code_file(info.files.frag, &codes->frag); codes->frag_pp = info.defines.frag; return ret; } void asc_shader_free_codes(AscShaderCodes codes) { cxFreeDefault(codes.vtx); cxFreeDefault(codes.frag); } AscShaderProgram *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; } #endif prog = create_func(); prog->id = id; cxListAdd(glctx->shaders, prog); return prog; } AscShaderProgram *asc_shader_lookup(unsigned int id) { CxIterator iter = cxListIterator(asc_active_glctx->shaders); cx_foreach(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); if (prog == NULL) { return asc_shader_register(id, create_func); } return prog; }