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