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