-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggt.c
422 lines (357 loc) · 10.8 KB
/
ggt.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "config.h"
#include <glib/gi18n-lib.h>
#ifdef GEGL_PROPERTIES
property_double(a_var, _("a"), 0.0)
value_range(-1.0, 1.0)
property_double(b_var, _("b"), 0.0)
value_range(-1.0, 1.0)
property_double(c_var, _("c"), 0.0)
value_range(-1.0, 1.0)
property_boolean(purge, _("Purge"), FALSE)
description("When this is switched on, shaders are \
recompiled and the base image texture is reloaded.")
property_string(vst, _("Vertex Shader"), "\
#version 460 core\n\
\n\
in vec2 icv;\n\
out vec2 icf;\n\
uniform float a;\n\
uniform float b;\n\
uniform float c;\n\
\n\
void main()\n\
{\n\
icf = icv;\n\
gl_Position.x = icv.x + a*icv.y;\n\
gl_Position.y = sin(20*b*icv.x);\n\
gl_Position.z = cos(20*b*icv.x);\n\
gl_Position.w = 1.0;\n\
}\n\
")
ui_meta("multiline", "true")
property_string(fst, _("Fragment Shader"), "\
#version 460 core\n\
\n\
in vec2 icf;\n\
out vec4 color;\n\
uniform sampler2D sam;\n\
uniform float a;\n\
uniform float b;\n\
uniform float c;\n\
\n\
void main()\n\
{\n\
vec2 uv = 0.5*(icf - 1.0);\n\
uv.x = mod(20*b*uv.x, 1.0);\n\
color = texture(sam, uv).rgba;\n\
}\n\
")
ui_meta("multiline", "true")
property_int(xVert, _("Vertices along the x-axis"), 512)
value_range(2, 1024)
property_int(yVert, _("Vertices along the y-axis"), 512)
value_range(2, 1024)
#else
#define GEGL_OP_FILTER
#define GEGL_OP_NAME ggt
#define GEGL_OP_C_SOURCE ggt.c
#include "gegl-op.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
//TODO
//draw distance
//use EGL instead of glfw, make it headless
//figure out what glew does
//write to layer of arbitrary bounds
//uniform arbitration
//cursor coordinates as uniforms
//improve UI
// shader storage/retrieval
// proper purge button
typedef struct
{
int engaged;
int purged;
int prog;
char *dst_buf;
} State;
void reportError(const char *msg)
{
puts(msg);
switch(glGetError())
{
case GL_NO_ERROR:
puts( "None");
//puts( "GL_NO_ERROR:\nNo error has been recorded. The value of this symbolic constant is guaranteed to be 0.");
break;
case GL_INVALID_ENUM:
puts( "GL_INVALID_ENUM:\nAn unacceptable value is specified for an enumerated argument. "
"The offending command is ignored and has no other side effect than to set the error flag.");
break;
case GL_INVALID_VALUE:
puts( "GL_INVALID_VALUE:\nA numeric argument is out of range. "
"The offending command is ignored and has no other side effect than to set the error flag.");
break;
case GL_INVALID_OPERATION:
puts( "GL_INVALID_OPERATION:\nThe specified operation is not allowed in the current state. "
"The offending command is ignored and has no other side effect than to set the error flag.");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
puts( "GL_INVALID_FRAMEBUFFER_OPERATION:\nThe framebuffer object is not complete. "
"The offending command is ignored and has no other side effect than to set the error flag.");
break;
case GL_OUT_OF_MEMORY:
puts( "GL_OUT_OF_MEMORY:\nThere is not enough memory left to execute the command. "
"The state of the GL is undefined, except for the state of the error flags, after this error is recorded.");
break;
case GL_STACK_UNDERFLOW:
puts( "GL_STACK_UNDERFLOW:\nAn attempt has been made to perform an operation that would cause an internal stack to underflow.");
break;
case GL_STACK_OVERFLOW:
puts( "GL_STACK_OVERFLOW:\nAn attempt has been made to perform an operation that would cause an internal stack to overflow. ");
break;
default:
}
puts("");
return;
}
void reloadVertices(int w, int h)
{
float *img_coo;
size_t img_coo_siz = 4*sizeof(float)*(w + 1)*h;
img_coo = malloc(img_coo_siz);
{
size_t index = 0;
for(int b = 0; b < h; b += 1)
{
for(int a = 0; a < w; a += 1)
{
img_coo[index++] = ((float) (2*a + 1 - w))/(w - 1);
img_coo[index++] = ((float) (2*b + 1 - h))/(h - 1);
img_coo[index++] = ((float) (2*a + 1 - w))/(w - 1);
img_coo[index++] = ((float) (2*b + 3 - h))/(h - 1);
}
img_coo[index++] = 1.0;
img_coo[index++] = ((float) (2*b + 3 - h))/(h - 1);
img_coo[index++] = -1.0;
img_coo[index++] = ((float) (2*b + 3 - h))/(h - 1);
}
}
static uint32_t VertexArrayID = 0;
glDeleteVertexArrays(1, &VertexArrayID);
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static uint32_t vertexbuffer = 0;
glDeleteBuffers(1, &vertexbuffer);
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, img_coo_siz, img_coo, GL_STATIC_DRAW);
free(img_coo);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
}
void reloadTexture(GeglRectangle *bound, GeglBuffer *input)
{
char *src_buf;
int w = bound->width;
int h = bound->height;
src_buf = malloc(w * h * 4);
gegl_buffer_get
(input, NULL, 1.0, babl_format("RGBA u8"), src_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
static uint32_t texture = 0;
glDeleteTextures(1, &texture);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, src_buf);
free(src_buf);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
void shaderTextAttach(int prog, const char *sourceText, int sort)
{
int s = glCreateShader(sort);
glShaderSource(s, 1, &sourceText, NULL);
glCompileShader(s);
int shaderCompileSuccess = GL_FALSE;
glGetShaderiv(s, GL_COMPILE_STATUS, &shaderCompileSuccess);
if(shaderCompileSuccess == GL_FALSE)
{
puts("Shader compilation error.");
if(sourceText == NULL) puts("Shader source string is a NULL pointer");
else
{
printf("Offending shader:\n\n%s\n", sourceText);
char log[4096];
glGetShaderInfoLog(s, 4096, NULL, log);
int error = glGetError();
if(error == GL_INVALID_OPERATION) puts("Shader object does not exist.");
else if(error == GL_INVALID_VALUE) printf("Impossible shader value: %d.\n", prog);
else printf("Info log:\n%s", log);
}
//raise(SIGTRAP);
}
glAttachShader(prog, s);
return;
}
void reloadShaders(GeglOperation *operation, int prog)
{
GeglProperties *o = GEGL_PROPERTIES(operation);
static int vs = 0;
static int fs = 0;
glDetachShader(prog, vs);
glDetachShader(prog, fs);
glDeleteShader(vs);
glDeleteShader(fs);
shaderTextAttach(prog, o->vst, GL_VERTEX_SHADER);
shaderTextAttach(prog, o->fst, GL_FRAGMENT_SHADER);
}
void redraw(GeglOperation *operation)
{
GeglProperties *o = GEGL_PROPERTIES(operation);
GeglRectangle bound =
*gegl_operation_source_get_bounding_box(operation, "input");
State *state = o->user_data;
int prog = state->prog;
int a_loc = glGetUniformLocation(prog, "a");
glUniform1f(a_loc, o->a_var);
int b_loc = glGetUniformLocation(prog, "b");
glUniform1f(b_loc, o->b_var);
int c_loc = glGetUniformLocation(prog, "c");
glUniform1f(c_loc, o->c_var);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*(o->xVert + 1)*o->yVert);
char *dst_buf = state->dst_buf;
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glReadPixels(0, 0, bound.width, bound.height, GL_RGBA, GL_UNSIGNED_BYTE, 0);
dst_buf = (char *) glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
//glFlush();
state->dst_buf = dst_buf;
return;
}
static void
prepare(GeglOperation *operation)
{
gegl_operation_set_format(operation, "input", babl_format("RGBA u8"));
gegl_operation_set_format(operation, "output", babl_format("RGBA u8"));
GeglProperties *o = GEGL_PROPERTIES(operation);
if(o->user_data == NULL) o->user_data = calloc(1, sizeof(State));
State *state = o->user_data;
if(state->engaged) redraw(operation);
}
void purge(GeglOperation *operation)
{
GeglProperties *o = GEGL_PROPERTIES(operation);
GeglRectangle bound =
*gegl_operation_source_get_bounding_box(operation, "input");
State *state = o->user_data;
if(!glfwInit())
{
puts("GLFW Initialization failed.");
raise(SIGTRAP);
}
static GLFWwindow* window = NULL;
glfwDestroyWindow(window);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window =
glfwCreateWindow
(bound.width, bound.height, "IRIS", NULL, NULL);
if(window == NULL)
{
printf("%d %d\n", bound.width, bound.height);
puts("Failed to create GLFW window.");
raise(SIGTRAP);
}
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)
{
puts("GLEW Initialization failed.");
raise(SIGTRAP);
}
int prog = state->prog;
glDeleteProgram(prog);
prog = glCreateProgram();
if(prog == 0)
{
puts("Program creation error.");
raise(SIGTRAP);
}
reloadShaders(operation, prog);
reloadVertices(o->xVert, o->yVert);
static uint32_t pbo = 0;
glDeleteBuffers(1, &pbo);
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
glBufferData(GL_PIXEL_PACK_BUFFER, bound.width*bound.height*4, NULL, GL_STATIC_READ);
glLinkProgram(prog);
int linkSuccess = GL_FALSE;
glGetProgramiv(prog, GL_LINK_STATUS, &linkSuccess);
if(linkSuccess != GL_TRUE)
{
puts("Program link error.");
char log[4096];
glGetProgramInfoLog(prog, 4096, NULL, log);
int error = glGetError();
if(error == GL_INVALID_OPERATION) puts("Program object does not exist.");
else if(error == GL_INVALID_VALUE) printf("Impossible program value: %d.\n", prog);
else printf("Info log:\n%s", log);
raise(SIGTRAP);
}
glUseProgram(prog);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
state->prog = prog;
}
static gboolean
process(GeglOperation *operation,
GeglBuffer *input,
GeglBuffer *output,
const GeglRectangle *result,
gint level)
{
GeglProperties *o = GEGL_PROPERTIES(operation);
GeglRectangle bound =
*gegl_operation_source_get_bounding_box(operation, "input");
State *state = o->user_data;
if((!state->purged && o->purge) || !state->engaged)
{
state->engaged = 1;
purge(operation);
reloadTexture(&bound, input);
redraw(operation);
}
state->purged = o->purge;
if(state->dst_buf == NULL)
{
puts("Received NULL pointer as destination buffer.");
raise(SIGTRAP);
}
gegl_buffer_set(output, result, 0, babl_format("RGBA u8"),
state->dst_buf + 4*(result->y*bound.width + result->x), 4*bound.width);
return TRUE;
}
static void
gegl_op_class_init(GeglOpClass *klass)
{
GeglOperationClass *operation_class;
GeglOperationFilterClass *filter_class;
operation_class = GEGL_OPERATION_CLASS(klass);
filter_class = GEGL_OPERATION_FILTER_CLASS(klass);
filter_class->process = process;
operation_class->prepare = prepare;
//operation_class->get_bounding_box = get_bounding_box;
//operation_class->get_required_for_output = get_required_for_output;
operation_class->threaded = FALSE;
gegl_operation_class_set_keys(operation_class,
"name" , "gegl:ggt",
"categories" , "map",
"description",
_("General geometric transformation."),
NULL);
}
#endif