src/shader.c

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 50
d8d2e4817db1
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

universe@15 1 /*
universe@15 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@15 3 * Copyright 2023 Mike Becker. All rights reserved.
universe@15 4 *
universe@15 5 * Redistribution and use in source and binary forms, with or without
universe@15 6 * modification, are permitted provided that the following conditions are met:
universe@15 7 *
universe@15 8 * 1. Redistributions of source code must retain the above copyright
universe@15 9 * notice, this list of conditions and the following disclaimer.
universe@15 10 *
universe@15 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@15 12 * notice, this list of conditions and the following disclaimer in the
universe@15 13 * documentation and/or other materials provided with the distribution.
universe@15 14 *
universe@15 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@15 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@15 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@15 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@15 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@15 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@15 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@15 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@15 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@15 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@15 25 * POSSIBILITY OF SUCH DAMAGE.
universe@15 26 */
universe@15 27
universe@15 28 #include "ascension/shader.h"
universe@15 29 #include "ascension/files.h"
universe@15 30 #include "ascension/error.h"
universe@15 31
universe@15 32 #include <GL/glew.h>
universe@16 33 #include <string.h>
universe@15 34
universe@15 35 AscShader asc_shader_compile(unsigned int type,
universe@15 36 char const *code,
universe@15 37 int length) {
universe@15 38 GLuint id = glCreateShader(type);
universe@15 39 if (id == 0) {
universe@15 40 asc_error("glCreateShader failed");
universe@15 41 return (AscShader) {0};
universe@15 42 }
universe@15 43
universe@15 44 GLint success;
universe@15 45 glShaderSource(id, 1, &code, &length);
universe@15 46 glCompileShader(id);
universe@15 47 glGetShaderiv(id, GL_COMPILE_STATUS, &success);
universe@15 48 if (success) {
universe@15 49 asc_dprintf("Shader %u compiled", id);
universe@15 50 return (AscShader) {id};
universe@15 51 } else {
universe@15 52 char *log = malloc(1024);
universe@15 53 glGetShaderInfoLog(id, 1024, NULL, log);
universe@15 54 glDeleteShader(id);
universe@15 55 asc_error(log);
universe@15 56 free(log);
universe@15 57 return (AscShader) {0};
universe@15 58 }
universe@15 59 }
universe@15 60
universe@15 61 AscShader asc_shader_compilef(unsigned int type,
universe@15 62 char const *filename) {
universe@15 63 asc_file code = asc_file_mmap_rdonly(filename);
universe@15 64 if (code.ptr == NULL) {
universe@15 65 asc_error("Mapping shader file into memory failed");
universe@15 66 return (AscShader) {0};
universe@15 67 }
universe@15 68 AscShader ret = asc_shader_compile(type, code.ptr, code.length);
universe@15 69 asc_file_unmap(code);
universe@15 70 return ret;
universe@15 71 }
universe@15 72
universe@15 73 AscShaderProgram asc_shader_link(AscShader vertex,
universe@15 74 AscShader fragment) {
universe@15 75 if (vertex.id == 0 || fragment.id == 0) {
universe@15 76 asc_dprintf("Skip linking shader program - shaders have not been loaded correctly.");
universe@15 77 return (AscShaderProgram) {0};
universe@15 78 }
universe@15 79
universe@15 80 GLint success;
universe@15 81 GLint id = glCreateProgram();
universe@15 82 if (id <= 0) {
universe@15 83 asc_error("glCreateProgram failed");
universe@15 84 return (AscShaderProgram) {0};
universe@15 85 }
universe@15 86 glAttachShader(id, vertex.id);
universe@15 87 glAttachShader(id, fragment.id);
universe@15 88 glLinkProgram(id);
universe@15 89 glGetProgramiv(id, GL_LINK_STATUS, &success);
universe@15 90 glDetachShader(id, vertex.id);
universe@15 91 glDetachShader(id, fragment.id);
universe@15 92 if (success) {
universe@15 93 asc_dprintf("Shader Program %u linked (vtf: %u, frag: %u)", id, vertex.id, fragment.id);
universe@16 94 AscShaderProgram prog;
universe@16 95 prog.id = id;
universe@16 96 prog.model = glGetUniformLocation(id, "model");
universe@16 97 prog.view = glGetUniformLocation(id, "view");
universe@16 98 prog.projection = glGetUniformLocation(id, "projection");
universe@16 99 return prog;
universe@15 100 } else {
universe@15 101 char *log = malloc(1024);
universe@15 102 glGetProgramInfoLog(id, 1024, NULL, log);
universe@15 103 glDeleteShader(id);
universe@15 104 asc_error(log);
universe@15 105 free(log);
universe@15 106 return (AscShaderProgram) {0};
universe@15 107 }
universe@15 108 }
universe@15 109
universe@15 110 void asc_shader_destroy(AscShader shader) {
universe@15 111 if (shader.id > 0) {
universe@15 112 asc_dprintf("Delete Shader %u", shader.id);
universe@15 113 glDeleteShader(shader.id);
universe@15 114 }
universe@15 115 shader.id = 0;
universe@15 116 }
universe@15 117
universe@15 118 void asc_shader_program_destroy(AscShaderProgram program) {
universe@15 119 if (program.id > 0) {
universe@15 120 asc_dprintf("Delete Shader Program %u", program.id);
universe@15 121 glDeleteProgram(program.id);
universe@15 122 }
universe@15 123 program.id = 0;
universe@15 124 }
universe@15 125
universe@44 126 AscShaderProgram asc_shader_easy_compile_and_link(
universe@15 127 char const *vtxName, char const *fragName) {
universe@15 128 AscShader font_vtx = asc_shader_compilef(GL_VERTEX_SHADER, vtxName);
universe@15 129 AscShader font_frag = asc_shader_compilef(GL_FRAGMENT_SHADER, fragName);
universe@15 130 AscShaderProgram prog = asc_shader_link(font_vtx, font_frag);
universe@15 131 asc_shader_destroy(font_vtx);
universe@15 132 asc_shader_destroy(font_frag);
universe@15 133 return prog;
universe@15 134 }

mercurial