-
Notifications
You must be signed in to change notification settings - Fork 4
/
sstoy.c
328 lines (273 loc) · 8.27 KB
/
sstoy.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <X11/Xlib.h>
#include "config.h"
static const char common_shader_header[] =
"#version 100\n"
"precision highp float;";
static const char vertex_shader_body[] =
"attribute vec4 iPosition;"
"void main(){gl_Position=iPosition;}";
static const char fragment_shader_header[] =
"uniform vec3 iResolution;"
"uniform float iGlobalTime;"
"uniform float iChannelTime[4];"
"uniform vec4 iMouse;"
"uniform vec4 iDate;"
"uniform float iSampleRate;"
"uniform vec3 iChannelResolution[4];"
"uniform sampler2D iChannel0;"
"uniform sampler2D iChannel1;"
"uniform sampler2D iChannel2;"
"uniform sampler2D iChannel3;\n";
static const char fragment_shader_footer[] =
"\nvoid main(){mainImage(gl_FragColor,gl_FragCoord.xy);}";
static Display *x_display;
static Window x_root;
static Window x_window;
static EGLDisplay egl_display;
static EGLContext egl_context;
static EGLSurface egl_surface;
static GLsizei viewport_width = -1;
static GLsizei viewport_height = -1;
static GLuint shader_program;
static GLint attrib_position;
static GLint sampler_channel[4];
static GLint uniform_cres;
static GLint uniform_date;
static GLint uniform_gtime;
static GLint uniform_mouse;
static GLint uniform_res;
static
void die(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
static
double timespec_diff(const struct timespec *start, const struct timespec *stop)
{
struct timespec d;
if ((stop->tv_nsec - start->tv_nsec) < 0) {
d.tv_sec = stop->tv_sec - start->tv_sec - 1;
d.tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000l;
} else {
d.tv_sec = stop->tv_sec - start->tv_sec;
d.tv_nsec = stop->tv_nsec - start->tv_nsec;
}
return (double)d.tv_sec + (double)d.tv_nsec / 1000000000.0;
}
static
void monotonic_time(struct timespec *tp)
{
if (clock_gettime(CLOCK_MONOTONIC, tp) == -1)
die("clock_gettime on CLOCK_MONOTIC failed.\n");
}
static
GLuint compile_shader(GLenum type, GLsizei nsources, const char **sources)
{
GLuint shader;
GLint success, len;
GLsizei i, srclens[nsources];
char *log;
for (i = 0; i < nsources; ++i)
srclens[i] = (GLsizei)strlen(sources[i]);
shader = glCreateShader(type);
glShaderSource(shader, nsources, sources, srclens);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
log = malloc(len);
glGetShaderInfoLog(shader, len, NULL, log);
fprintf(stderr, "%s\n\n", log);
free(log);
}
die("Error compiling shader.\n");
}
return shader;
}
static
void resize(GLsizei w, GLsizei h)
{
if (viewport_width != w || viewport_height != h) {
glUniform3f(uniform_res, (float)w, (float)h, 0.0f);
glViewport(0, 0, w, h);
viewport_width = w;
viewport_height = h;
}
}
static
void startup(void)
{
static const EGLint cv[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
int screen, nvi;
XWindowAttributes gwa;
XSetWindowAttributes swa;
XVisualInfo *vi, vit;
EGLint vid, ncfg, len, success;
EGLConfig cfg;
GLuint vtx, frag;
const char *sources[4];
char* log;
if (!(x_display = XOpenDisplay(NULL)))
die("Unable to open X display.\n");
if ((egl_display = eglGetDisplay(x_display)) == EGL_NO_DISPLAY)
die("Unable to get EGL display.\n");
if (!eglBindAPI(EGL_OPENGL_ES_API))
die("Unable to bind OpenGL ES API to EGL.\n");
if (!eglInitialize(egl_display, NULL, NULL))
die("Unable to initialize EGL.\n");
if (!eglChooseConfig(egl_display, egl_config, &cfg, 1, &ncfg))
die("Unable to find EGL framebuffer configuration.\n");
egl_context = eglCreateContext(egl_display, cfg, EGL_NO_CONTEXT, cv);
if (egl_context == EGL_NO_CONTEXT)
die("Unable to create EGL context.\n");
if (!eglGetConfigAttrib(egl_display, cfg, EGL_NATIVE_VISUAL_ID, &vid))
die("Unable to get X VisualID.\n");
screen = DefaultScreen(x_display);
x_root = RootWindow(x_display, screen);
vit.visualid = vid;
if (!(vi = XGetVisualInfo(x_display, VisualIDMask, &vit, &nvi)))
die("Unable to find matching XVisualInfo for framebuffer.\n");
swa.background_pixel = 0;
swa.colormap =
XCreateColormap(x_display, x_root, vi->visual, AllocNone);
swa.event_mask =
ExposureMask | StructureNotifyMask |
KeyPressMask | PointerMotionMask;
swa.override_redirect = False;
x_window = XCreateWindow(x_display, x_root, 0, 0,
DisplayWidth(x_display, screen),
DisplayHeight(x_display, screen),
0, vi->depth, InputOutput, vi->visual,
CWBackPixel | CWColormap | CWEventMask |
CWOverrideRedirect, &swa);
XStoreName(x_display, x_window, "sstoy");
XMapWindow(x_display, x_window);
XFlush(x_display);
egl_surface = eglCreateWindowSurface(egl_display, cfg, x_window, NULL);
if (egl_surface == EGL_NO_SURFACE)
die("Unable to create EGL window surface.\n");
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
sources[0] = common_shader_header;
sources[1] = vertex_shader_body;
vtx = compile_shader(GL_VERTEX_SHADER, 2, sources);
sources[0] = common_shader_header;
sources[1] = fragment_shader_header;
sources[2] = default_fragment_shader;
sources[3] = fragment_shader_footer;
frag = compile_shader(GL_FRAGMENT_SHADER, 4, sources);
shader_program = glCreateProgram();
glAttachShader(shader_program, vtx);
glAttachShader(shader_program, frag);
glLinkProgram(shader_program);
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
log = malloc(len);
glGetProgramInfoLog(shader_program, len, &len, log);
fprintf(stderr, "%s\n\n", log);
free(log);
}
die("Error linking shader program.\n");
}
glDeleteShader(vtx);
glDeleteShader(frag);
glReleaseShaderCompiler();
glClearColor(0.0f, 0.0f, 0.0f, 1.0);
glUseProgram(shader_program);
glValidateProgram(shader_program);
if (glGetError() != GL_NO_ERROR)
die("Shader program is not valid.\n");
attrib_position = glGetAttribLocation(shader_program, "iPosition");
sampler_channel[0] = glGetUniformLocation(shader_program, "iChannel0");
sampler_channel[1] = glGetUniformLocation(shader_program, "iChannel1");
sampler_channel[2] = glGetUniformLocation(shader_program, "iChannel2");
sampler_channel[3] = glGetUniformLocation(shader_program, "iChannel3");
uniform_cres = glGetUniformLocation(shader_program, "iChannelResolution");
uniform_date = glGetUniformLocation(shader_program, "iDate");
uniform_gtime = glGetUniformLocation(shader_program, "iGlobalTime");
uniform_mouse = glGetUniformLocation(shader_program, "iMouse");
uniform_res = glGetUniformLocation(shader_program, "iResolution");
if (!XGetWindowAttributes(x_display, x_window, &gwa))
die("Unable to get window size.\n");
resize(gwa.width, gwa.height);
}
static
void shutdown(void)
{
glDeleteProgram(shader_program);
eglDestroyContext(egl_display, egl_context);
eglDestroySurface(egl_display, egl_surface);
eglTerminate(egl_display);
XDestroyWindow(x_display, x_window);
XCloseDisplay(x_display);
}
static
bool process_events(void)
{
XEvent ev;
KeySym key;
bool done = false;
while (XPending(x_display) > 0) {
XNextEvent(x_display, &ev);
switch (ev.type) {
case ConfigureNotify:
resize(ev.xconfigure.width, ev.xconfigure.height);
break;
case KeyPress:
key = XLookupKeysym(&ev.xkey, 0);
if (key == XK_Escape || key == XK_q)
done = true;
break;
default:
break;
}
}
return !done;
}
static
void render(double abstime)
{
static const GLfloat vertices[] = {
-1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f, 1.0f,
};
if (uniform_gtime >= 0)
glUniform1f(uniform_gtime, (float)abstime);
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(attrib_position);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
eglSwapBuffers(egl_display, egl_surface);
}
int main(int argc, char **argv)
{
struct timespec start, cur;
startup();
monotonic_time(&start);
for (;;) {
if (!process_events())
break;
render(timespec_diff(&start, &cur));
monotonic_time(&cur);
}
shutdown();
return 0;
}