-
Notifications
You must be signed in to change notification settings - Fork 14
/
shadertoy.c
309 lines (261 loc) · 11.9 KB
/
shadertoy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright © 2020 Antonin Stefanutti <antonin.stefanutti@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <err.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <regex.h>
#include <stdlib.h>
#include <GLES3/gl3.h>
#include "common.h"
GLint iTime, iFrame;
static const char *shadertoy_vs_tmpl_100 =
"// version (default: 1.10) \n"
"%s \n"
" \n"
"attribute vec3 position; \n"
" \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(position, 1.0); \n"
"} \n";
static const char *shadertoy_vs_tmpl_300 =
"// version \n"
"%s \n"
" \n"
"in vec3 position; \n"
" \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(position, 1.0); \n"
"} \n";
static const char *shadertoy_fs_tmpl_100 =
"// version (default: 1.10) \n"
"%s \n"
" \n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
"precision highp float; \n"
"#else \n"
"precision mediump float; \n"
"#endif \n"
" \n"
"uniform vec3 iResolution; // viewport resolution (in pixels) \n"
"uniform float iTime; // shader playback time (in seconds) \n"
"uniform int iFrame; // current frame number \n"
"uniform vec4 iMouse; // mouse pixel coords \n"
"uniform vec4 iDate; // (year, month, day, time in seconds) \n"
" \n"
"// Shader body \n"
"%s \n"
" \n"
"void main() \n"
"{ \n"
" mainImage(gl_FragColor, gl_FragCoord.xy); \n"
"} \n";
static const char *shadertoy_fs_tmpl_300 =
"// version \n"
"%s \n"
" \n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
"precision highp float; \n"
"#else \n"
"precision mediump float; \n"
"#endif \n"
" \n"
"out vec4 fragColor; \n"
" \n"
"uniform vec3 iResolution; // viewport resolution (in pixels) \n"
"uniform float iTime; // shader playback time (in seconds) \n"
"uniform int iFrame; // current frame number \n"
"uniform vec4 iMouse; // mouse pixel coords \n"
"uniform vec4 iDate; // (year, month, day, time in seconds) \n"
" \n"
"// Shader body \n"
"%s \n"
" \n"
"void main() \n"
"{ \n"
" mainImage(fragColor, gl_FragCoord.xy); \n"
"} \n";
static const GLfloat vertices[] = {
// First triangle:
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f,
// Second triangle:
-1.0f, -1.0f,
1.0f, -1.0f,
1.0f, 1.0f,
};
static const char *load_shader(const char *file) {
struct stat statbuf;
int fd, ret;
fd = open(file, 0);
if (fd < 0) {
err(fd, "could not open '%s'", file);
}
ret = fstat(fd, &statbuf);
if (ret < 0) {
err(ret, "could not stat '%s'", file);
}
return mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
}
#define GLSL_VERSION_REGEX "GLSL[[:space:]]*(ES)?[[:space:]]*([[:digit:]]+)\\.([[:digit:]]+)"
static char *extract_group(const char *str, regmatch_t group) {
char *c = calloc(group.rm_eo - group.rm_so, sizeof(char));
memcpy(c, &str[group.rm_so], group.rm_eo - group.rm_so);
return c;
}
static char *glsl_version() {
int ret;
regex_t regex;
if ((ret = regcomp(®ex, GLSL_VERSION_REGEX, REG_EXTENDED)) != 0) {
err(ret, "failed to compile GLSL version regex");
}
char *version = "";
const char *glsl_version = (char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
if (strlen(glsl_version) == 0) {
printf("Cannot detect GLSL version from %s\n", "GL_SHADING_LANGUAGE_VERSION");
return version;
}
size_t nGroups = 4;
regmatch_t groups[nGroups];
ret = regexec(®ex, glsl_version, nGroups, groups, 0);
if (ret == REG_NOMATCH) {
printf("Cannot match GLSL version '%s'\n", glsl_version);
} else if (ret != 0) {
err(ret, "failed to match GLSL version '%s'", glsl_version);
} else {
char *es = extract_group(glsl_version, groups[1]);
char *major = extract_group(glsl_version, groups[2]);
char *minor = extract_group(glsl_version, groups[3]);
if (strcmp(minor, "0") == 0) {
free(minor);
minor = malloc(sizeof(char) * 3);
strcpy(minor, "00");
}
bool is100 = strcmp(major, "1") == 0 && strcmp(minor, "00") == 0;
bool hasES = strcasecmp(es, "ES") == 0 && !is100;
asprintf(&version, "%s%s%s", major, minor, hasES ? " es" : "");
free(es);
free(major);
free(minor);
}
regfree(®ex);
return version;
}
typedef void (*onInitCallback)(uint program, uint width, uint height);
typedef void (*onRenderCallback)(uint64_t frame, float time);
typedef struct {
void (**callbacks)();
size_t length;
} Callbacks;
void addCallback(Callbacks *callbacks, void callback()) {
if (!callbacks->callbacks) {
callbacks->length = 1;
callbacks->callbacks = malloc(sizeof(callbacks->callbacks));
callbacks->callbacks[0] = callback;
} else {
callbacks->length++;
callbacks->callbacks = realloc(callbacks->callbacks, callbacks->length * sizeof(callbacks->callbacks));
callbacks->callbacks[callbacks->length - 1] = callback;
}
}
Callbacks onInitCallbacks;
void onInit(onInitCallback callback) {
addCallback(&onInitCallbacks, (void (*)) callback);
}
Callbacks onRenderCallbacks;
void onRender(onRenderCallback callback) {
addCallback(&onRenderCallbacks, (void (*)) callback);
}
static void draw_shadertoy(uint64_t start_time, unsigned frame) {
float time = ((float) (get_time_ns() - start_time)) / NSEC_PER_SEC;
glUniform1f(iTime, time);
// Replace the above to input elapsed time relative to 60 FPS
// glUniform1f(iTime, (GLfloat) frame / 60.0f);
glUniform1ui(iFrame, frame);
for (uint i = 0; i < onRenderCallbacks.length; i++) {
((onRenderCallback) onRenderCallbacks.callbacks[i])(frame, time);
}
start_perfcntrs();
glDrawArrays(GL_TRIANGLES, 0, 6);
end_perfcntrs();
}
int init_shadertoy(const struct gbm *gbm, struct egl *egl, const char *file) {
int ret;
char *shadertoy_vs, *shadertoy_fs;
GLuint program, vbo;
GLint iResolution;
const char *shader = load_shader(file);
const char *version = glsl_version();
if (strlen(version) > 0) {
char *invalid;
long v = strtol(version, &invalid, 10);
if (invalid == version) {
printf("failed to parse detected GLSL version: %s\n", invalid);
return -1;
}
char *version_directive;
asprintf(&version_directive, "#version %s", version);
printf("Using GLSL version directive: %s\n", version_directive);
bool is_glsl_3 = v >= 300;
asprintf(&shadertoy_vs, is_glsl_3 ? shadertoy_vs_tmpl_300 : shadertoy_vs_tmpl_100, version_directive);
asprintf(&shadertoy_fs, is_glsl_3 ? shadertoy_fs_tmpl_300 : shadertoy_fs_tmpl_100, version_directive, shader);
} else {
asprintf(&shadertoy_vs, shadertoy_vs_tmpl_100, version);
asprintf(&shadertoy_fs, shadertoy_fs_tmpl_100, version, shader);
}
ret = create_program(shadertoy_vs, shadertoy_fs);
if (ret < 0) {
printf("failed to create program\n");
return -1;
}
program = ret;
ret = link_program(program);
if (ret) {
printf("failed to link program\n");
return -1;
}
glViewport(0, 0, gbm->width, gbm->height);
glUseProgram(program);
iTime = glGetUniformLocation(program, "iTime");
iFrame = glGetUniformLocation(program, "iFrame");
iResolution = glGetUniformLocation(program, "iResolution");
glUniform3f(iResolution, gbm->width, gbm->height, 0);
for (uint i = 0; i < onInitCallbacks.length; i++) {
((onInitCallback) onInitCallbacks.callbacks[i])(program, gbm->width, gbm->height);
}
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), &vertices[0]);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (intptr_t) 0);
glEnableVertexAttribArray(0);
egl->draw = draw_shadertoy;
return 0;
}