Allow an effect to have multiple inputs (although the graph still supports only one...
[movit] / effect_chain.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #include <GL/gl.h>
8 #include <GL/glext.h>
9
10 #include "util.h"
11 #include "effect_chain.h"
12 #include "gamma_expansion_effect.h"
13 #include "gamma_compression_effect.h"
14 #include "lift_gamma_gain_effect.h"
15 #include "colorspace_conversion_effect.h"
16 #include "sandbox_effect.h"
17 #include "saturation_effect.h"
18 #include "mirror_effect.h"
19 #include "vignette_effect.h"
20 #include "blur_effect.h"
21
22 EffectChain::EffectChain(unsigned width, unsigned height)
23         : width(width),
24           height(height),
25           last_added_effect(NULL),
26           use_srgb_texture_format(false),
27           finalized(false) {}
28
29 void EffectChain::add_input(const ImageFormat &format)
30 {
31         input_format = format;
32         output_color_space.insert(std::make_pair(static_cast<Effect *>(NULL), format.color_space));
33         output_gamma_curve.insert(std::make_pair(static_cast<Effect *>(NULL), format.gamma_curve));
34 }
35
36 void EffectChain::add_output(const ImageFormat &format)
37 {
38         output_format = format;
39 }
40
41 void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
42 {
43         effects.push_back(effect);
44         assert(inputs.size() == effect->num_inputs());
45         for (unsigned i = 0; i < inputs.size(); ++i) {
46                 outgoing_links.insert(std::make_pair(inputs[i], effect));
47                 incoming_links.insert(std::make_pair(effect, inputs[i]));
48         }
49         last_added_effect = effect;
50 }
51
52 Effect *instantiate_effect(EffectId effect)
53 {
54         switch (effect) {
55         case EFFECT_GAMMA_EXPANSION:
56                 return new GammaExpansionEffect();
57         case EFFECT_GAMMA_COMPRESSION:
58                 return new GammaCompressionEffect();
59         case EFFECT_COLOR_SPACE_CONVERSION:
60                 return new ColorSpaceConversionEffect();
61         case EFFECT_SANDBOX:
62                 return new SandboxEffect();
63         case EFFECT_LIFT_GAMMA_GAIN:
64                 return new LiftGammaGainEffect();
65         case EFFECT_SATURATION:
66                 return new SaturationEffect();
67         case EFFECT_MIRROR:
68                 return new MirrorEffect();
69         case EFFECT_VIGNETTE:
70                 return new VignetteEffect();
71         case EFFECT_BLUR:
72                 return new BlurEffect();
73         }
74         assert(false);
75 }
76
77 Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
78 {
79         GammaCurve current_gamma_curve = output_gamma_curve[input];
80         if (current_gamma_curve == GAMMA_sRGB) {
81                 // TODO: check if the extension exists
82                 use_srgb_texture_format = true;
83                 current_gamma_curve = GAMMA_LINEAR;
84                 return input;
85         } else {
86                 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
87                 gamma_conversion->set_int("source_curve", current_gamma_curve);
88                 std::vector<Effect *> inputs;
89                 inputs.push_back(input);
90                 gamma_conversion->add_self_to_effect_chain(this, inputs);
91                 current_gamma_curve = GAMMA_LINEAR;
92                 return gamma_conversion;
93         }
94 }
95
96 Effect *EffectChain::normalize_to_srgb(Effect *input)
97 {
98         GammaCurve current_gamma_curve = output_gamma_curve[input];
99         ColorSpace current_color_space = output_color_space[input];
100         assert(current_gamma_curve == GAMMA_LINEAR);
101         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
102         colorspace_conversion->set_int("source_space", current_color_space);
103         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
104         std::vector<Effect *> inputs;
105         inputs.push_back(input);
106         colorspace_conversion->add_self_to_effect_chain(this, inputs);
107         current_color_space = COLORSPACE_sRGB;
108         return colorspace_conversion;
109 }
110
111 Effect *EffectChain::add_effect(EffectId effect_id, const std::vector<Effect *> &inputs)
112 {
113         Effect *effect = instantiate_effect(effect_id);
114
115         assert(inputs.size() == effect->num_inputs());
116
117         std::vector<Effect *> normalized_inputs = inputs;
118         for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
119                 if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
120                         normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
121                 }
122                 if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
123                         normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
124                 }
125         }
126
127         effect->add_self_to_effect_chain(this, normalized_inputs);
128         return effect;
129 }
130
131 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
132 std::string replace_prefix(const std::string &text, const std::string &prefix)
133 {
134         std::string output;
135         size_t start = 0;
136
137         while (start < text.size()) {
138                 size_t pos = text.find("PREFIX(", start);
139                 if (pos == std::string::npos) {
140                         output.append(text.substr(start, std::string::npos));
141                         break;
142                 }
143
144                 output.append(text.substr(start, pos - start));
145                 output.append(prefix);
146                 output.append("_");
147
148                 pos += strlen("PREFIX(");
149         
150                 // Output stuff until we find the matching ), which we then eat.
151                 int depth = 1;
152                 size_t end_arg_pos = pos;
153                 while (end_arg_pos < text.size()) {
154                         if (text[end_arg_pos] == '(') {
155                                 ++depth;
156                         } else if (text[end_arg_pos] == ')') {
157                                 --depth;
158                                 if (depth == 0) {
159                                         break;
160                                 }
161                         }
162                         ++end_arg_pos;
163                 }
164                 output.append(text.substr(pos, end_arg_pos - pos));
165                 ++end_arg_pos;
166                 assert(depth == 0);
167                 start = end_arg_pos;
168         }
169         return output;
170 }
171
172 EffectChain::Phase EffectChain::compile_glsl_program(unsigned start_index, unsigned end_index)
173 {
174         bool input_needs_mipmaps = false;
175         std::string frag_shader = read_file("header.frag");
176         for (unsigned i = start_index; i < end_index; ++i) {
177                 char effect_id[256];
178                 sprintf(effect_id, "eff%d", i);
179         
180                 frag_shader += "\n";
181                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
182                 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
183                 frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id);
184                 frag_shader += "#undef PREFIX\n";
185                 frag_shader += "#undef FUNCNAME\n";
186                 frag_shader += "#undef INPUT\n";
187                 frag_shader += std::string("#define INPUT ") + effect_id + "\n";
188                 frag_shader += "\n";
189
190                 input_needs_mipmaps |= effects[i]->needs_mipmaps();
191         }
192         frag_shader.append(read_file("footer.frag"));
193         printf("%s\n", frag_shader.c_str());
194         
195         GLuint glsl_program_num = glCreateProgram();
196         GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
197         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
198         glAttachShader(glsl_program_num, vs_obj);
199         check_error();
200         glAttachShader(glsl_program_num, fs_obj);
201         check_error();
202         glLinkProgram(glsl_program_num);
203         check_error();
204
205         Phase phase;
206         phase.glsl_program_num = glsl_program_num;
207         phase.input_needs_mipmaps = input_needs_mipmaps;
208         phase.start = start_index;
209         phase.end = end_index;
210
211         return phase;
212 }
213
214 void EffectChain::finalize()
215 {
216         // Add normalizers to get the output format right.
217         GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect];  // FIXME
218         ColorSpace current_color_space = output_color_space[last_added_effect];  // FIXME
219         if (current_color_space != output_format.color_space) {
220                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
221                 colorspace_conversion->set_int("source_space", current_color_space);
222                 colorspace_conversion->set_int("destination_space", output_format.color_space);
223                 effects.push_back(colorspace_conversion);
224                 current_color_space = output_format.color_space;
225         }
226         if (current_gamma_curve != output_format.gamma_curve) {
227                 if (current_gamma_curve != GAMMA_LINEAR) {
228                         normalize_to_linear_gamma(last_added_effect);  // FIXME
229                 }
230                 assert(current_gamma_curve == GAMMA_LINEAR);
231                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
232                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
233                 effects.push_back(gamma_conversion);
234                 current_gamma_curve = output_format.gamma_curve;
235         }
236
237         // Construct the GLSL programs. We end a program every time we come
238         // to an effect marked as "needs many samples" (ie. "please let me
239         // sample directly from a texture, with no arithmetic in-between"),
240         // and of course at the end.
241         unsigned start = 0;
242         for (unsigned i = 0; i < effects.size(); ++i) {
243                 if (effects[i]->needs_texture_bounce() && i != start) {
244                         phases.push_back(compile_glsl_program(start, i));
245                         start = i;
246                 }
247         }
248         phases.push_back(compile_glsl_program(start, effects.size()));
249
250         // If we have more than one phase, we need intermediate render-to-texture.
251         // Construct an FBO, and then as many textures as we need.
252         if (phases.size() > 1) {
253                 glGenFramebuffers(1, &fbo);
254
255                 unsigned num_textures = std::max<int>(phases.size() - 1, 2);
256                 glGenTextures(num_textures, temp_textures);
257
258                 for (unsigned i = 0; i < num_textures; ++i) {
259                         glBindTexture(GL_TEXTURE_2D, temp_textures[i]);
260                         check_error();
261                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
262                         check_error();
263                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
264                         check_error();
265                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
266                         check_error();
267                 }
268         }
269         
270         // Translate the input format to OpenGL's enums.
271         GLenum internal_format;
272         if (use_srgb_texture_format) {
273                 internal_format = GL_SRGB8;
274         } else {
275                 internal_format = GL_RGBA8;
276         }
277         if (input_format.pixel_format == FORMAT_RGB) {
278                 format = GL_RGB;
279                 bytes_per_pixel = 3;
280         } else if (input_format.pixel_format == FORMAT_RGBA) {
281                 format = GL_RGBA;
282                 bytes_per_pixel = 4;
283         } else if (input_format.pixel_format == FORMAT_BGR) {
284                 format = GL_BGR;
285                 bytes_per_pixel = 3;
286         } else if (input_format.pixel_format == FORMAT_BGRA) {
287                 format = GL_BGRA;
288                 bytes_per_pixel = 4;
289         } else {
290                 assert(false);
291         }
292
293         // Create PBO to hold the texture holding the input image, and then the texture itself.
294         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
295         check_error();
296         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
297         check_error();
298         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
299         check_error();
300         
301         glGenTextures(1, &source_image_num);
302         check_error();
303         glBindTexture(GL_TEXTURE_2D, source_image_num);
304         check_error();
305         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
306         check_error();
307         // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here.
308         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, phases[0].input_needs_mipmaps ? GL_TRUE : GL_FALSE);
309         check_error();
310         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
311         check_error();
312
313         finalized = true;
314 }
315
316 void EffectChain::render_to_screen(unsigned char *src)
317 {
318         assert(finalized);
319
320         // Copy the pixel data into the PBO.
321         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
322         check_error();
323         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
324         memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
325         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
326         check_error();
327
328         // Re-upload the texture from the PBO.
329         glActiveTexture(GL_TEXTURE0);
330         check_error();
331         glBindTexture(GL_TEXTURE_2D, source_image_num);
332         check_error();
333         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
334         check_error();
335         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
336         check_error();
337         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
338         check_error();
339         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
340         check_error();
341
342         // Basic state.
343         glDisable(GL_BLEND);
344         check_error();
345         glDisable(GL_DEPTH_TEST);
346         check_error();
347         glDepthMask(GL_FALSE);
348         check_error();
349
350         glMatrixMode(GL_PROJECTION);
351         glLoadIdentity();
352         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
353
354         glMatrixMode(GL_MODELVIEW);
355         glLoadIdentity();
356
357         if (phases.size() > 1) {
358                 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
359                 check_error();
360         }
361
362         for (unsigned phase = 0; phase < phases.size(); ++phase) {
363                 // Set up inputs and outputs for this phase.
364                 glActiveTexture(GL_TEXTURE0);
365                 if (phase == 0) {
366                         // First phase reads from the input texture (which is already bound).
367                 } else {
368                         glBindTexture(GL_TEXTURE_2D, temp_textures[(phase + 1) % 2]);
369                         check_error();
370                 }
371                 if (phases[phase].input_needs_mipmaps) {
372                         if (phase != 0) {
373                                 // For phase 0, it's done further up.
374                                 glGenerateMipmap(GL_TEXTURE_2D);
375                                 check_error();
376                         }
377                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
378                         check_error();
379                 } else {
380                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
381                         check_error();
382                 }
383
384                 if (phase == phases.size() - 1) {
385                         // Last phase goes directly to the screen.
386                         glBindFramebuffer(GL_FRAMEBUFFER, 0);
387                         check_error();
388                 } else {
389                         glFramebufferTexture2D(
390                                 GL_FRAMEBUFFER,
391                                 GL_COLOR_ATTACHMENT0,
392                                 GL_TEXTURE_2D,
393                                 temp_textures[phase % 2],
394                                 0);
395                         check_error();
396                 }
397
398                 // We have baked an upside-down transform into the quad coordinates,
399                 // since the typical graphics program will have the origin at the upper-left,
400                 // while OpenGL uses lower-left. In the next ones, however, the origin
401                 // is all right, and we need to reverse that.
402                 if (phase == 1) {
403                         glTranslatef(0.0f, 1.0f, 0.0f);
404                         glScalef(1.0f, -1.0f, 1.0f);
405                 }
406
407                 // Give the required parameters to all the effects.
408                 glUseProgram(phases[phase].glsl_program_num);
409                 check_error();
410
411                 glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, "input_tex"), 0);
412                 check_error();
413
414                 unsigned sampler_num = 1;
415                 for (unsigned i = phases[phase].start; i < phases[phase].end; ++i) {
416                         char effect_id[256];
417                         sprintf(effect_id, "eff%d", i);
418                         effects[i]->set_uniforms(phases[phase].glsl_program_num, effect_id, &sampler_num);
419                 }
420
421                 // Now draw!
422                 glBegin(GL_QUADS);
423
424                 glTexCoord2f(0.0f, 1.0f);
425                 glVertex2f(0.0f, 0.0f);
426
427                 glTexCoord2f(1.0f, 1.0f);
428                 glVertex2f(1.0f, 0.0f);
429
430                 glTexCoord2f(1.0f, 0.0f);
431                 glVertex2f(1.0f, 1.0f);
432
433                 glTexCoord2f(0.0f, 0.0f);
434                 glVertex2f(0.0f, 1.0f);
435
436                 glEnd();
437                 check_error();
438
439                 // HACK
440                 glActiveTexture(GL_TEXTURE0);
441                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
442                 check_error();
443                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
444                 check_error();
445         }
446 }