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