Start actually piecing together the GLSL shaders from the effect chain.
[movit] / main.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3
4 #define WIDTH 1280
5 #define HEIGHT 720
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
7
8 #include <string.h>
9 #include <math.h>
10 #include <time.h>
11 #include <assert.h>
12
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 #include <SDL/SDL.h>
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
20
21 #include <GL/gl.h>
22 #include <GL/glext.h>
23
24 #include "effect.h"
25 #include "effect_chain.h"
26 #include "util.h"
27 #include "widgets.h"
28 #include "texture_enum.h"
29
30 unsigned char result[WIDTH * HEIGHT * 4];
31
32 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
33 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
34 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
35 float saturation = 1.0f;
36
37 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
38 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
39 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
40
41 void draw_picture_quad(GLint prog, int frame)
42 {
43         glUseProgramObjectARB(prog);
44         check_error();
45
46         glActiveTexture(GL_TEXTURE0);
47         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
48         glUniform1i(glGetUniformLocation(prog, "tex"), 0);
49
50         glActiveTexture(GL_TEXTURE1);
51         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
52         glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
53
54         glActiveTexture(GL_TEXTURE2);
55         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
56         glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
57
58         glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
59         //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
60         glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
61                     2.2f / gamma_r,
62                     2.2f / gamma_g,
63                     2.2f / gamma_b);
64         glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
65                     pow(gain_r, 1.0f / gamma_r),
66                     pow(gain_g, 1.0f / gamma_g),
67                     pow(gain_b, 1.0f / gamma_b));
68         glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
69
70         glDisable(GL_BLEND);
71         check_error();
72         glDisable(GL_DEPTH_TEST);
73         check_error();
74         glDepthMask(GL_FALSE);
75         check_error();
76
77         glMatrixMode(GL_PROJECTION);
78         glLoadIdentity();
79         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
80
81         glMatrixMode(GL_MODELVIEW);
82         glLoadIdentity();
83
84         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
85 //      glClear(GL_COLOR_BUFFER_BIT);
86         check_error();
87
88         glBegin(GL_QUADS);
89
90         glTexCoord2f(0.0f, 1.0f);
91         glVertex2f(0.0f, 0.0f);
92
93         glTexCoord2f(1.0f, 1.0f);
94         glVertex2f(1.0f, 0.0f);
95
96         glTexCoord2f(1.0f, 0.0f);
97         glVertex2f(1.0f, 1.0f);
98
99         glTexCoord2f(0.0f, 0.0f);
100         glVertex2f(0.0f, 1.0f);
101
102         glEnd();
103         check_error();
104 }
105
106 void update_hsv()
107 {
108         hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
109         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
110         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
111
112         if (saturation < 0.0) {
113                 saturation = 0.0;
114         }
115
116         printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
117         printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
118         printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
119         printf("saturation: %f\n", saturation);
120         printf("\n");
121 }
122
123 void mouse(int x, int y)
124 {
125         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
126         float yf = (HEIGHT - y) / (float)HEIGHT;
127
128         if (yf < 0.2f) {
129                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
130         } else if (yf >= 0.2f && yf < 0.4f) {
131                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
132         } else if (yf >= 0.4f && yf < 0.6f) {
133                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
134         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
135                 saturation = (xf / 0.2f) * 4.0f;
136         }
137
138         update_hsv();
139 }
140
141 void load_texture(const char *filename)
142 {
143         SDL_Surface *img = IMG_Load(filename);
144         if (img == NULL) {
145                 fprintf(stderr, "Load of '%s' failed\n", filename);
146                 exit(1);
147         }
148
149         // Convert to RGB.
150         SDL_PixelFormat *fmt = img->format;
151         SDL_LockSurface(img);
152         unsigned char *src_pixels = (unsigned char *)img->pixels;
153         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
154         for (unsigned i = 0; i < img->w * img->h; ++i) {
155                 unsigned char r, g, b;
156                 unsigned int temp;
157                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
158
159                 temp = pixel & fmt->Rmask;
160                 temp = temp >> fmt->Rshift;
161                 temp = temp << fmt->Rloss;
162                 r = temp;
163
164                 temp = pixel & fmt->Gmask;
165                 temp = temp >> fmt->Gshift;
166                 temp = temp << fmt->Gloss;
167                 g = temp;
168
169                 temp = pixel & fmt->Bmask;
170                 temp = temp >> fmt->Bshift;
171                 temp = temp << fmt->Bloss;
172                 b = temp;
173
174                 dst_pixels[i * 3 + 0] = r;
175                 dst_pixels[i * 3 + 1] = g;
176                 dst_pixels[i * 3 + 2] = b;
177         }
178         SDL_UnlockSurface(img);
179
180 #if 1
181         // we will convert to sRGB in the shader
182         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
183         check_error();
184 #else
185         // implicit sRGB conversion in hardware
186         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
187         check_error();
188 #endif
189         free(dst_pixels);
190         SDL_FreeSurface(img);
191 }
192
193 void write_ppm(const char *filename, unsigned char *screenbuf)
194 {
195         FILE *fp = fopen(filename, "w");
196         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
197         for (unsigned y = 0; y < HEIGHT; ++y) {
198                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
199                 for (unsigned x = 0; x < WIDTH; ++x) {
200                         fputc(srcptr[x * 4 + 2], fp);
201                         fputc(srcptr[x * 4 + 1], fp);
202                         fputc(srcptr[x * 4 + 0], fp);
203                 }
204         }
205         fclose(fp);
206 }
207
208 int main(int argc, char **argv)
209 {
210         int quit = 0;
211
212         SDL_Init(SDL_INIT_EVERYTHING);
213         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
214         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
215         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
216         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
217         SDL_WM_SetCaption("OpenGL window", NULL);
218         
219         // geez 
220         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
221         glPixelStorei(GL_PACK_ALIGNMENT, 1);
222
223         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
224         check_error();
225         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
226         check_error();
227
228         load_texture("blg_wheels_woman_1.jpg");
229
230         EffectChain chain(WIDTH, HEIGHT);
231
232         ImageFormat inout_format;
233         inout_format.pixel_format = FORMAT_RGB;
234         inout_format.color_space = COLORSPACE_sRGB;
235         inout_format.gamma_curve = GAMMA_sRGB;
236
237         chain.add_input(inout_format);
238         Effect *lift_gamma_gain_effect = chain.add_effect(LIFT_GAMMA_GAIN);
239         chain.add_output(inout_format);
240         chain.finalize();
241
242         //glGenerateMipmap(GL_TEXTURE_2D);
243         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
244         //check_error();
245
246         //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
247         //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
248         //load_texture("glacier-lake-1280-720-4087.jpg");
249
250 #if 0
251         // sRGB reverse LUT
252         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
253         check_error();
254         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
255         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
256         check_error();
257         float srgb_reverse_tex[4096];
258         for (unsigned i = 0; i < 4096; ++i) {
259                 float x = i / 4095.0;
260                 if (x < 0.0031308f) {
261                         srgb_reverse_tex[i] = 12.92f * x;
262                 } else {
263                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
264                 }
265         }
266         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
267         check_error();
268
269         // sRGB LUT
270         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
271         check_error();
272         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
273         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
274         check_error();
275         float srgb_tex[256];
276         for (unsigned i = 0; i < 256; ++i) {
277                 float x = i / 255.0;
278                 if (x < 0.04045f) {
279                         srgb_tex[i] = x * (1.0f / 12.92f);
280                 } else {
281                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
282                 }
283         }
284         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
285         check_error();
286 #endif
287
288         // generate a PDO to hold the data we read back with glReadPixels()
289         // (Intel/DRI goes into a slow path if we don't read to PDO)
290         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
291         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
292
293         make_hsv_wheel_texture();
294         update_hsv();
295
296         int prog = glCreateProgram();
297         GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
298         GLhandleARB fs_obj = compile_shader(read_file("fs.glsl"), GL_FRAGMENT_SHADER);
299         glAttachObjectARB(prog, vs_obj);
300         check_error();
301         glAttachObjectARB(prog, fs_obj);
302         check_error();
303         glLinkProgram(prog);
304         check_error();
305
306         GLchar info_log[4096];
307         GLsizei log_length = sizeof(info_log) - 1;
308         log_length = sizeof(info_log) - 1;
309         glGetProgramInfoLog(prog, log_length, &log_length, info_log);
310         info_log[log_length] = 0; 
311         printf("link: %s\n", info_log);
312
313         struct timespec start, now;
314         int frame = 0, screenshot = 0;
315         clock_gettime(CLOCK_MONOTONIC, &start);
316
317         while (!quit) {
318                 SDL_Event event;
319                 while (SDL_PollEvent(&event)) {
320                         if (event.type == SDL_QUIT) {
321                                 quit = 1;
322                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
323                                 quit = 1;
324                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
325                                 screenshot = 1;
326                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
327                                 mouse(event.button.x, event.button.y);
328                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
329                                 mouse(event.motion.x, event.motion.y);
330                         }
331                 }
332
333                 ++frame;
334
335                 
336                 draw_picture_quad(prog, frame);
337                 
338                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
339                 check_error();
340
341                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
342                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
343                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
344                 draw_saturation_bar(0.6f, saturation);
345
346                 SDL_GL_SwapBuffers();
347                 check_error();
348
349                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
350                 check_error();
351                 if (screenshot) {
352                         char filename[256];
353                         sprintf(filename, "frame%05d.ppm", frame);
354                         write_ppm(filename, screenbuf);
355                         printf("Screenshot: %s\n", filename);
356                         screenshot = 0;
357                 }
358                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
359                 check_error();
360
361 #if 1
362                 clock_gettime(CLOCK_MONOTONIC, &now);
363                 double elapsed = now.tv_sec - start.tv_sec +
364                         1e-9 * (now.tv_nsec - start.tv_nsec);
365                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
366                         frame, elapsed, frame / elapsed,
367                         1e3 * elapsed / frame);
368 #endif
369         }
370         return 0; 
371 }