Wed, 10 Apr 2024 19:24:39 +0200
fix illegally skipping geometry update of text node
the reason is that the scene will not try again to update the node when it is just unhidden
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 | |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | AscShader asc_shader_compile(unsigned int type, |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | char const *code, |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | int length) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | GLuint id = glCreateShader(type); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | if (id == 0) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | asc_error("glCreateShader failed"); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | return (AscShader) {0}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | GLint success; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | glShaderSource(id, 1, &code, &length); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | glCompileShader(id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | glGetShaderiv(id, GL_COMPILE_STATUS, &success); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | if (success) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | asc_dprintf("Shader %u compiled", id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | return (AscShader) {id}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | } else { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | char *log = malloc(1024); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | glGetShaderInfoLog(id, 1024, NULL, log); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | glDeleteShader(id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | asc_error(log); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | free(log); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | return (AscShader) {0}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | AscShader asc_shader_compilef(unsigned int type, |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | char const *filename) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | asc_file code = asc_file_mmap_rdonly(filename); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | if (code.ptr == NULL) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | asc_error("Mapping shader file into memory failed"); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | return (AscShader) {0}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | 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
|
69 | asc_file_unmap(code); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | return ret; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | AscShaderProgram asc_shader_link(AscShader vertex, |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | AscShader fragment) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | if (vertex.id == 0 || fragment.id == 0) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | 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
|
77 | return (AscShaderProgram) {0}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | GLint success; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | GLint id = glCreateProgram(); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | if (id <= 0) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | asc_error("glCreateProgram failed"); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | return (AscShaderProgram) {0}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | glAttachShader(id, vertex.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | glAttachShader(id, fragment.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | glLinkProgram(id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | glGetProgramiv(id, GL_LINK_STATUS, &success); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | glDetachShader(id, vertex.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | glDetachShader(id, fragment.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | if (success) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | 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
|
94 | AscShaderProgram prog; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
95 | prog.id = id; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
96 | prog.model = glGetUniformLocation(id, "model"); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
97 | prog.view = glGetUniformLocation(id, "view"); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
98 | prog.projection = glGetUniformLocation(id, "projection"); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
99 | return prog; |
15
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | } else { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | char *log = malloc(1024); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | glGetProgramInfoLog(id, 1024, NULL, log); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | glDeleteShader(id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | asc_error(log); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | free(log); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | return (AscShaderProgram) {0}; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | void asc_shader_destroy(AscShader shader) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | if (shader.id > 0) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | asc_dprintf("Delete Shader %u", shader.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | glDeleteShader(shader.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | shader.id = 0; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | } |
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 | void asc_shader_program_destroy(AscShaderProgram program) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | if (program.id > 0) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | asc_dprintf("Delete Shader Program %u", program.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | glDeleteProgram(program.id); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | program.id = 0; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | } |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | |
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
|
126 | AscShaderProgram asc_shader_easy_compile_and_link( |
15
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | char const *vtxName, char const *fragName) { |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | 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
|
129 | 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
|
130 | AscShaderProgram prog = asc_shader_link(font_vtx, font_frag); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | asc_shader_destroy(font_vtx); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | asc_shader_destroy(font_frag); |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | return prog; |
362b7659dc76
add shader loading and unloading
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | } |