src/shader.c

Mon, 01 Apr 2024 18:54:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 01 Apr 2024 18:54:19 +0200
changeset 47
44457f6cb0a2
parent 44
b3da4096c607
child 50
d8d2e4817db1
permissions
-rw-r--r--

remove unnecessary scene container

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

mercurial