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