src/shader.c

Wed, 19 Mar 2025 22:43:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 19 Mar 2025 22:43:37 +0100
changeset 78
9bc544f4ce8f
parent 77
2187a732f4d7
permissions
-rw-r--r--

go back to reading shader code from files

apart from using a nice C23 feature there is
nothing really useful about embedding the code
directly

/*
 * 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/shader.h"
#include "ascension/error.h"

#include <GL/glew.h>
#include <string.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
 * @return the compiled shader
 */
static unsigned asc_shader_compile(unsigned int type, char const *code) {
    GLuint id = glCreateShader(type);
    if (id == 0) {
        asc_error("glCreateShader failed");
        return 0;
    }

    GLint success;
    int length = (int) strlen(code); // must be int because of OpenGL API
    glShaderSource(id, 1, &code, &length);
    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(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
 * @return a compiled program
 */
static AscShaderProgram asc_shader_link(unsigned shader[], unsigned n) {
    GLint success;
    GLint id = glCreateProgram();
    if (id <= 0) {
        asc_error("glCreateProgram failed");
        return (AscShaderProgram) {0};
    }
    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);
        AscShaderProgram prog;
        prog.id = id;
        // TODO: maybe not every program has the same uniforms
        prog.model = glGetUniformLocation(id, "model");
        prog.view = glGetUniformLocation(id, "view");
        prog.projection = glGetUniformLocation(id, "projection");
        return prog;
    } else {
        char *log = malloc(1024);
        glGetProgramInfoLog(id, 1024, NULL, log);
        glDeleteProgram(id);
        asc_error(log);
        free(log);
        return (AscShaderProgram) {0};
    }
}

void asc_shader_program_destroy(AscShaderProgram program) {
    if (program.id > 0) {
        asc_dprintf("Delete Shader Program %u", program.id);
        glDeleteProgram(program.id);
    }
    program.id = 0;
}

AscShaderProgram asc_shader_program_create(AscShaderCodes codes) {
    unsigned shader[4];
    unsigned n = 0;
    if (codes.vtx) {
        shader[n++] = asc_shader_compile(GL_VERTEX_SHADER, codes.vtx);
    }
    if (codes.frag) {
        shader[n++] = asc_shader_compile(GL_FRAGMENT_SHADER, codes.frag);
    }
    const AscShaderProgram prog = asc_shader_link(shader, n);
    for (unsigned i = 0; i < n; i++) {
        glDeleteShader(shader[i]);
    }
    return prog;
}

static int asc_shader_load_code_file(const char *filename, char **code) {
    if (filename == NULL) {
        *code = NULL;
        return 0;
    }
    FILE *f = fopen(filename, "r");
    if (f == NULL) return -1;
    CxBuffer buffer;
    cxBufferInit(&buffer, NULL, 1024, NULL, 0);
    cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc);
    cxBufferPut(&buffer, '\0');
    *code = realloc(buffer.space, buffer.size);
    return *code == NULL ? -1 : 0;
}

int asc_shader_load_code_files(AscShaderCodeFiles files, AscShaderCodes *codes) {
    int ret = 0;
    ret |= asc_shader_load_code_file(files.vtx, &codes->vtx);
    ret |= asc_shader_load_code_file(files.frag, &codes->frag);
    return ret;
}

void asc_shader_free_codes(AscShaderCodes codes) {
    free(codes.vtx);
    free(codes.frag);
}

mercurial