]> git.sesse.net Git - movit/blob - demo.cpp
Make timer query objects polled asynchronously, so that the CPU blocks less on the...
[movit] / demo.cpp
1 #define NO_SDL_GLEXT 1
2
3 #define WIDTH 1280
4 #define HEIGHT 720
5
6 #include <epoxy/gl.h>
7
8 #ifdef HAVE_SDL2
9 #include <SDL2/SDL.h>
10 #include <SDL2/SDL_error.h>
11 #include <SDL2/SDL_events.h>
12 #include <SDL2/SDL_image.h>
13 #include <SDL2/SDL_keyboard.h>
14 #include <SDL2/SDL_mouse.h>
15 #include <SDL2/SDL_video.h>
16 #else
17 #include <SDL/SDL.h>
18 #include <SDL/SDL_error.h>
19 #include <SDL/SDL_events.h>
20 #include <SDL/SDL_image.h>
21 #include <SDL/SDL_keyboard.h>
22 #include <SDL/SDL_keysym.h>
23 #include <SDL/SDL_mouse.h>
24 #include <SDL/SDL_video.h>
25 #endif
26
27 #include <assert.h>
28 #include <features.h>
29 #include <math.h>
30 #include <png.h>
31 #include <pngconf.h>
32 #include <setjmp.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <time.h>
37
38 #include "diffusion_effect.h"
39 #include "effect.h"
40 #include "effect_chain.h"
41 #include "flat_input.h"
42 #include "image_format.h"
43 #include "init.h"
44 #include "lift_gamma_gain_effect.h"
45 #include "saturation_effect.h"
46 #include "util.h"
47 #include "widgets.h"
48
49 using namespace movit;
50
51 unsigned char result[WIDTH * HEIGHT * 4];
52
53 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
54 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
55 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
56 float saturation = 1.0f;
57
58 //float radius = 0.3f;
59 // float inner_radius = 0.3f;
60 float blur_radius = 20.0f;
61 float blurred_mix_amount = 0.5f;
62         
63 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
64 {
65         RGBTriplet lift(0.0f, 0.0f, 0.0f);
66         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
67         RGBTriplet gain(1.0f, 1.0f, 1.0f);
68
69         hsv2rgb_normalized(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
70         hsv2rgb_normalized(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
71         hsv2rgb_normalized(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
72
73         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
74         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
75         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
76         assert(ok);
77
78         if (saturation < 0.0) {
79                 saturation = 0.0;
80         }
81         ok = saturation_effect->set_float("saturation", saturation);
82         assert(ok);
83 }
84
85 void mouse(int x, int y)
86 {
87         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
88         float yf = (HEIGHT - y) / (float)HEIGHT;
89
90         if (yf < 0.2f) {
91                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
92         } else if (yf >= 0.2f && yf < 0.4f) {
93                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
94         } else if (yf >= 0.4f && yf < 0.6f) {
95                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
96         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
97                 saturation = (xf / 0.2f) * 4.0f;
98 #if 0
99         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
100                 radius = (xf / 0.2f);
101         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
102                 inner_radius = (xf / 0.2f);
103 #endif
104         } else if (yf >= 0.75f && yf < 0.77f && xf < 0.2f) {
105                 blur_radius = (xf / 0.2f) * 100.0f;
106         } else if (yf >= 0.80f && yf < 0.82f && xf < 0.2f) {
107                 blurred_mix_amount = (xf / 0.2f);
108         }
109 }
110
111 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
112 {
113         SDL_Surface *img = IMG_Load(filename);
114         if (img == NULL) {
115                 fprintf(stderr, "Load of '%s' failed\n", filename);
116                 exit(1);
117         }
118
119         SDL_PixelFormat rgba_fmt;
120         rgba_fmt.palette = NULL;
121         rgba_fmt.BitsPerPixel = 32;
122         rgba_fmt.BytesPerPixel = 8;
123         rgba_fmt.Rloss = rgba_fmt.Gloss = rgba_fmt.Bloss = rgba_fmt.Aloss = 0;
124
125         // NOTE: Assumes little endian.
126         rgba_fmt.Rmask = 0x00ff0000;
127         rgba_fmt.Gmask = 0x0000ff00;
128         rgba_fmt.Bmask = 0x000000ff;
129         rgba_fmt.Amask = 0xff000000;
130
131         rgba_fmt.Rshift = 16;
132         rgba_fmt.Gshift = 8;
133         rgba_fmt.Bshift = 0;
134         rgba_fmt.Ashift = 24;
135
136 #ifndef HAVE_SDL2
137         rgba_fmt.colorkey = 0;
138         rgba_fmt.alpha = 255;
139 #endif
140
141         SDL_Surface *converted = SDL_ConvertSurface(img, &rgba_fmt, SDL_SWSURFACE);
142
143         *w = img->w;
144         *h = img->h;
145
146         SDL_FreeSurface(img);
147
148         return (unsigned char *)converted->pixels;
149 }
150
151 void write_png(const char *filename, unsigned char *screenbuf)
152 {
153         FILE *fp = fopen(filename, "wb");
154         png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
155         png_infop info_ptr = png_create_info_struct(png_ptr);
156         
157         if (setjmp(png_jmpbuf(png_ptr))) {
158                 fclose(fp);
159                 fprintf(stderr, "Write to %s failed; exiting.\n", filename);
160                 exit(1);
161         }
162
163         png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
164
165         png_bytep *row_pointers = new png_bytep[HEIGHT];
166         for (unsigned y = 0; y < HEIGHT; ++y) {
167                 row_pointers[y] = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
168         }
169
170         png_init_io(png_ptr, fp);
171         png_set_rows(png_ptr, info_ptr, row_pointers);
172         png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, NULL);
173         png_destroy_write_struct(&png_ptr, &info_ptr);
174         fclose(fp);
175
176         delete[] row_pointers;
177 }
178
179 int main(int argc, char **argv)
180 {
181         bool quit = false;
182
183         if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
184                 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
185                 exit(1);
186         }
187         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
188         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
189         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
190         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
191
192 #ifdef HAVE_SDL2
193         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
194         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
195         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
196         // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
197         SDL_Window *window = SDL_CreateWindow("OpenGL window",
198                 SDL_WINDOWPOS_UNDEFINED,
199                 SDL_WINDOWPOS_UNDEFINED,
200                 WIDTH, HEIGHT,
201                 SDL_WINDOW_OPENGL);
202         SDL_GLContext context = SDL_GL_CreateContext(window);
203         assert(context != NULL);
204 #else
205         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
206         SDL_WM_SetCaption("OpenGL window", NULL);
207 #endif
208
209         CHECK(init_movit(".", MOVIT_DEBUG_ON));
210         printf("GPU texture subpixel precision: about %.1f bits\n",
211                 log2(1.0f / movit_texel_subpixel_precision));
212         printf("Wrongly rounded x+0.48 or x+0.52 values: %d/510\n",
213                 movit_num_wrongly_rounded);
214         if (movit_num_wrongly_rounded > 0) {
215                 printf("Rounding off in the shader to compensate.\n");
216         }
217         
218         unsigned img_w, img_h;
219         unsigned char *src_img = load_image(argc > 1 ? argv[1] : "blg_wheels_woman_1.jpg", &img_w, &img_h);
220
221         EffectChain chain(WIDTH, HEIGHT);
222         glViewport(0, 0, WIDTH, HEIGHT);
223
224         ImageFormat inout_format;
225         inout_format.color_space = COLORSPACE_sRGB;
226         inout_format.gamma_curve = GAMMA_sRGB;
227
228         FlatInput *input = new FlatInput(inout_format, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, img_w, img_h);
229         chain.add_input(input);
230         Effect *lift_gamma_gain_effect = chain.add_effect(new LiftGammaGainEffect());
231         Effect *saturation_effect = chain.add_effect(new SaturationEffect());
232         Effect *diffusion_effect = chain.add_effect(new DiffusionEffect());
233         //Effect *vignette_effect = chain.add_effect(new VignetteEffect());
234         //Effect *sandbox_effect = chain.add_effect(new SandboxEffect());
235         //sandbox_effect->set_float("parm", 42.0f);
236         //chain.add_effect(new MirrorEffect());
237         chain.add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
238         chain.set_dither_bits(8);
239         chain.finalize();
240
241         // generate a PBO to hold the data we read back with glReadPixels()
242         // (Intel/DRI goes into a slow path if we don't read to PBO)
243         GLuint pbo;
244         glGenBuffers(1, &pbo);
245         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
246         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
247
248         init_hsv_resources();
249         check_error();
250
251         int frame = 0;
252         bool screenshot = false;
253 #if _POSIX_C_SOURCE >= 199309L
254         struct timespec start, now;
255         clock_gettime(CLOCK_MONOTONIC, &start);
256 #else
257         struct timeval start, now;
258         gettimeofday(&start, NULL);
259 #endif
260
261         while (!quit) {
262                 SDL_Event event;
263                 while (SDL_PollEvent(&event)) {
264                         if (event.type == SDL_QUIT) {
265                                 quit = true;
266                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
267                                 quit = true;
268                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
269                                 screenshot = true;
270                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
271                                 mouse(event.button.x, event.button.y);
272                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
273                                 mouse(event.motion.x, event.motion.y);
274                         }
275                 }
276
277                 ++frame;
278
279                 update_hsv(lift_gamma_gain_effect, saturation_effect);
280                 //vignette_effect->set_float("radius", radius);
281                 //vignette_effect->set_float("inner_radius", inner_radius);
282                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
283
284                 CHECK(diffusion_effect->set_float("radius", blur_radius));
285                 CHECK(diffusion_effect->set_float("blurred_mix_amount", blurred_mix_amount));
286
287                 input->set_pixel_data(src_img);
288                 chain.render_to_screen();
289                 
290                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
291                 check_error();
292                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
293                 check_error();
294                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
295                 check_error();
296
297                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
298                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
299                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
300                 draw_saturation_bar(0.6f, saturation / 4.0f);
301 #if 0
302                 draw_saturation_bar(0.65f, radius);
303                 draw_saturation_bar(0.70f, inner_radius);
304 #endif
305                 draw_saturation_bar(0.75f, blur_radius / 100.0f);
306                 draw_saturation_bar(0.80f, blurred_mix_amount);
307
308 #ifdef HAVE_SDL2
309                 SDL_GL_SwapWindow(window);
310 #else
311                 SDL_GL_SwapBuffers();
312 #endif
313                 check_error();
314
315                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
316                 check_error();
317                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
318                 check_error();
319                 if (screenshot) {
320                         char filename[256];
321                         sprintf(filename, "frame%05d.png", frame);
322                         write_png(filename, screenbuf);
323                         printf("Screenshot: %s\n", filename);
324                         screenshot = false;
325                 }
326                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
327                 check_error();
328                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
329                 check_error();
330
331 #if 1
332 #if _POSIX_C_SOURCE >= 199309L
333                 clock_gettime(CLOCK_MONOTONIC, &now);
334                 double elapsed = now.tv_sec - start.tv_sec +
335                         1e-9 * (now.tv_nsec - start.tv_nsec);
336 #else
337                 gettimeofday(&now, NULL);
338                 double elapsed = now.tv_sec - start.tv_sec +
339                         1e-6 * (now.tv_usec - start.tv_usec);
340 #endif
341                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
342                         frame, elapsed, frame / elapsed,
343                         1e3 * elapsed / frame);
344
345                 // Reset every 100 frames, so that local variations in frame times
346                 // (especially for the first few frames, when the shaders are
347                 // compiled etc.) don't make it hard to measure for the entire
348                 // remaining duration of the program.
349                 if (frame == 100) {
350                         frame = 0;
351                         start = now;
352                 }
353 #endif
354         }
355         cleanup_hsv_resources();
356         return 0; 
357 }