Fix an issue with textures that are immediately bounced (they need mipmaps in all...
[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 <algorithm>
8 #include <set>
9 #include <stack>
10 #include <vector>
11
12 #include "util.h"
13 #include "effect_chain.h"
14 #include "gamma_expansion_effect.h"
15 #include "gamma_compression_effect.h"
16 #include "colorspace_conversion_effect.h"
17 #include "input.h"
18 #include "opengl.h"
19
20 EffectChain::EffectChain(unsigned width, unsigned height)
21         : width(width),
22           height(height),
23           finalized(false) {}
24
25 Input *EffectChain::add_input(Input *input)
26 {
27         char eff_id[256];
28         sprintf(eff_id, "src_image%u", (unsigned)inputs.size());
29
30         effects.push_back(input);
31         inputs.push_back(input);
32         output_color_space.insert(std::make_pair(input, input->get_color_space()));
33         output_gamma_curve.insert(std::make_pair(input, input->get_gamma_curve()));
34         effect_ids.insert(std::make_pair(input, eff_id));
35         incoming_links.insert(std::make_pair(input, std::vector<Effect *>()));
36         return input;
37 }
38
39 void EffectChain::add_output(const ImageFormat &format)
40 {
41         output_format = format;
42 }
43
44 void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
45 {
46         char effect_id[256];
47         sprintf(effect_id, "eff%u", (unsigned)effects.size());
48
49         effects.push_back(effect);
50         effect_ids.insert(std::make_pair(effect, effect_id));
51         assert(inputs.size() == effect->num_inputs());
52         for (unsigned i = 0; i < inputs.size(); ++i) {
53                 assert(std::find(effects.begin(), effects.end(), inputs[i]) != effects.end());
54                 outgoing_links[inputs[i]].push_back(effect);
55         }
56         incoming_links.insert(std::make_pair(effect, inputs));
57         output_gamma_curve[effect] = output_gamma_curve[last_added_effect()];
58         output_color_space[effect] = output_color_space[last_added_effect()];
59 }
60
61 void EffectChain::find_all_nonlinear_inputs(Effect *effect,
62                                             std::vector<Input *> *nonlinear_inputs,
63                                             std::vector<Effect *> *intermediates)
64 {
65         assert(output_gamma_curve.count(effect) != 0);
66         if (output_gamma_curve[effect] == GAMMA_LINEAR) {
67                 return;
68         }
69         if (effect->num_inputs() == 0) {
70                 nonlinear_inputs->push_back(static_cast<Input *>(effect));
71         } else {
72                 intermediates->push_back(effect);
73
74                 assert(incoming_links.count(effect) == 1);
75                 std::vector<Effect *> deps = incoming_links[effect];
76                 assert(effect->num_inputs() == deps.size());
77                 for (unsigned i = 0; i < deps.size(); ++i) {
78                         find_all_nonlinear_inputs(deps[i], nonlinear_inputs, intermediates);
79                 }
80         }
81 }
82
83 Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
84 {
85         // Find out if all the inputs can be set to deliver sRGB inputs.
86         // If so, we can just ask them to do that instead of inserting a
87         // (possibly expensive) conversion operation.
88         //
89         // NOTE: We assume that effects generally don't mess with the gamma
90         // curve (except GammaCompressionEffect, which should never be
91         // inserted into a chain when this is called), so that we can just
92         // update the output gamma as we go.
93         //
94         // TODO: Setting this flag for one source might confuse a different
95         // part of the pipeline using the same source.
96         std::vector<Input *> nonlinear_inputs;
97         std::vector<Effect *> intermediates;
98         find_all_nonlinear_inputs(input, &nonlinear_inputs, &intermediates);
99
100         bool all_ok = true;
101         for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
102                 all_ok &= nonlinear_inputs[i]->can_output_linear_gamma();
103         }
104
105         if (all_ok) {
106                 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
107                         bool ok = nonlinear_inputs[i]->set_int("output_linear_gamma", 1);
108                         assert(ok);
109                         output_gamma_curve[nonlinear_inputs[i]] = GAMMA_LINEAR;
110                 }
111                 for (unsigned i = 0; i < intermediates.size(); ++i) {
112                         output_gamma_curve[intermediates[i]] = GAMMA_LINEAR;
113                 }
114                 return input;
115         }
116
117         // OK, that didn't work. Insert a conversion effect.
118         GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
119         gamma_conversion->set_int("source_curve", output_gamma_curve[input]);
120         std::vector<Effect *> inputs;
121         inputs.push_back(input);
122         gamma_conversion->add_self_to_effect_chain(this, inputs);
123         output_gamma_curve[gamma_conversion] = GAMMA_LINEAR;
124         return gamma_conversion;
125 }
126
127 Effect *EffectChain::normalize_to_srgb(Effect *input)
128 {
129         assert(output_gamma_curve.count(input) != 0);
130         assert(output_color_space.count(input) != 0);
131         assert(output_gamma_curve[input] == GAMMA_LINEAR);
132         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
133         colorspace_conversion->set_int("source_space", output_color_space[input]);
134         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
135         std::vector<Effect *> inputs;
136         inputs.push_back(input);
137         colorspace_conversion->add_self_to_effect_chain(this, inputs);
138         output_color_space[colorspace_conversion] = COLORSPACE_sRGB;
139         return colorspace_conversion;
140 }
141
142 Effect *EffectChain::add_effect(Effect *effect, const std::vector<Effect *> &inputs)
143 {
144         assert(inputs.size() == effect->num_inputs());
145
146         std::vector<Effect *> normalized_inputs = inputs;
147         for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
148                 assert(output_gamma_curve.count(normalized_inputs[i]) != 0);
149                 if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
150                         normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
151                 }
152                 assert(output_color_space.count(normalized_inputs[i]) != 0);
153                 if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
154                         normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
155                 }
156         }
157
158         effect->add_self_to_effect_chain(this, normalized_inputs);
159         return effect;
160 }
161
162 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
163 std::string replace_prefix(const std::string &text, const std::string &prefix)
164 {
165         std::string output;
166         size_t start = 0;
167
168         while (start < text.size()) {
169                 size_t pos = text.find("PREFIX(", start);
170                 if (pos == std::string::npos) {
171                         output.append(text.substr(start, std::string::npos));
172                         break;
173                 }
174
175                 output.append(text.substr(start, pos - start));
176                 output.append(prefix);
177                 output.append("_");
178
179                 pos += strlen("PREFIX(");
180         
181                 // Output stuff until we find the matching ), which we then eat.
182                 int depth = 1;
183                 size_t end_arg_pos = pos;
184                 while (end_arg_pos < text.size()) {
185                         if (text[end_arg_pos] == '(') {
186                                 ++depth;
187                         } else if (text[end_arg_pos] == ')') {
188                                 --depth;
189                                 if (depth == 0) {
190                                         break;
191                                 }
192                         }
193                         ++end_arg_pos;
194                 }
195                 output.append(text.substr(pos, end_arg_pos - pos));
196                 ++end_arg_pos;
197                 assert(depth == 0);
198                 start = end_arg_pos;
199         }
200         return output;
201 }
202
203 EffectChain::Phase *EffectChain::compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects)
204 {
205         assert(!effects.empty());
206
207         // Deduplicate the inputs.
208         std::vector<Effect *> true_inputs = inputs;
209         std::sort(true_inputs.begin(), true_inputs.end());
210         true_inputs.erase(std::unique(true_inputs.begin(), true_inputs.end()), true_inputs.end());
211
212         bool input_needs_mipmaps = false;
213         std::string frag_shader = read_file("header.frag");
214
215         // Create functions for all the texture inputs that we need.
216         for (unsigned i = 0; i < true_inputs.size(); ++i) {
217                 Effect *effect = true_inputs[i];
218                 assert(effect_ids.count(effect) != 0);
219                 std::string effect_id = effect_ids[effect];
220         
221                 frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";       
222                 frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
223                 if (effect->num_inputs() == 0) {
224                         // OpenGL's origin is bottom-left, but most graphics software assumes
225                         // a top-left origin. Thus, for inputs that come from the user,
226                         // we flip the y coordinate. However, for FBOs, the origin
227                         // is all correct, so don't do anything.
228                         frag_shader += "\ttc.y = 1.0f - tc.y;\n";
229                 }
230                 frag_shader += "\treturn texture2D(tex_" + effect_id + ", tc);\n";
231                 frag_shader += "}\n";
232                 frag_shader += "\n";
233         }
234
235         std::string last_effect_id;
236         for (unsigned i = 0; i < effects.size(); ++i) {
237                 Effect *effect = effects[i];
238                 assert(effect != NULL);
239                 assert(effect_ids.count(effect) != 0);
240                 std::string effect_id = effect_ids[effect];
241                 last_effect_id = effect_id;
242
243                 if (incoming_links[effect].size() == 1) {
244                         frag_shader += std::string("#define INPUT ") + effect_ids[incoming_links[effect][0]] + "\n";
245                 } else {
246                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
247                                 char buf[256];
248                                 sprintf(buf, "#define INPUT%d %s\n", j + 1, effect_ids[incoming_links[effect][j]].c_str());
249                                 frag_shader += buf;
250                         }
251                 }
252         
253                 frag_shader += "\n";
254                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
255                 frag_shader += replace_prefix(effect->output_convenience_uniforms(), effect_id);
256                 frag_shader += replace_prefix(effect->output_fragment_shader(), effect_id);
257                 frag_shader += "#undef PREFIX\n";
258                 frag_shader += "#undef FUNCNAME\n";
259                 if (incoming_links[effect].size() == 1) {
260                         frag_shader += "#undef INPUT\n";
261                 } else {
262                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
263                                 char buf[256];
264                                 sprintf(buf, "#undef INPUT%d\n", j + 1);
265                                 frag_shader += buf;
266                         }
267                 }
268                 frag_shader += "\n";
269
270                 input_needs_mipmaps |= effect->needs_mipmaps();
271         }
272         for (unsigned i = 0; i < effects.size(); ++i) {
273                 Effect *effect = effects[i];
274                 if (effect->num_inputs() == 0) {
275                         effect->set_int("needs_mipmaps", input_needs_mipmaps);
276                 }
277         }
278         assert(!last_effect_id.empty());
279         frag_shader += std::string("#define INPUT ") + last_effect_id + "\n";
280         frag_shader.append(read_file("footer.frag"));
281         printf("%s\n", frag_shader.c_str());
282         
283         GLuint glsl_program_num = glCreateProgram();
284         GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
285         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
286         glAttachShader(glsl_program_num, vs_obj);
287         check_error();
288         glAttachShader(glsl_program_num, fs_obj);
289         check_error();
290         glLinkProgram(glsl_program_num);
291         check_error();
292
293         Phase *phase = new Phase;
294         phase->glsl_program_num = glsl_program_num;
295         phase->input_needs_mipmaps = input_needs_mipmaps;
296         phase->inputs = true_inputs;
297         phase->effects = effects;
298
299         return phase;
300 }
301
302 // Construct GLSL programs, starting at the given effect and following
303 // the chain from there. We end a program every time we come to an effect
304 // marked as "needs texture bounce", one that is used by multiple other
305 // effects, every time an effect wants to change the output size,
306 // and of course at the end.
307 //
308 // We follow a quite simple depth-first search from the output, although
309 // without any explicit recursion.
310 void EffectChain::construct_glsl_programs(Effect *output)
311 {
312         // Which effects have already been completed in this phase?
313         // We need to keep track of it, as an effect with multiple outputs
314         // could otherwise be calculate multiple times.
315         std::set<Effect *> completed_effects;
316
317         // Effects in the current phase, as well as inputs (outputs from other phases
318         // that we depend on). Note that since we start iterating from the end,
319         // the effect list will be in the reverse order.
320         std::vector<Effect *> this_phase_inputs;
321         std::vector<Effect *> this_phase_effects;
322
323         // Effects that we have yet to calculate, but that we know should
324         // be in the current phase.
325         std::stack<Effect *> effects_todo_this_phase;
326
327         // Effects that we have yet to calculate, but that come from other phases.
328         // We delay these until we have this phase done in its entirety,
329         // at which point we pick any of them and start a new phase from that.
330         std::stack<Effect *> effects_todo_other_phases;
331
332         effects_todo_this_phase.push(output);
333
334         for ( ;; ) {  // Termination condition within loop.
335                 if (!effects_todo_this_phase.empty()) {
336                         // OK, we have more to do this phase.
337                         Effect *effect = effects_todo_this_phase.top();
338                         effects_todo_this_phase.pop();
339
340                         // This should currently only happen for effects that are phase outputs,
341                         // and we throw those out separately below.
342                         assert(completed_effects.count(effect) == 0);
343
344                         this_phase_effects.push_back(effect);
345                         completed_effects.insert(effect);
346
347                         // Find all the dependencies of this effect, and add them to the stack.
348                         assert(incoming_links.count(effect) == 1);
349                         std::vector<Effect *> deps = incoming_links[effect];
350                         assert(effect->num_inputs() == deps.size());
351                         for (unsigned i = 0; i < deps.size(); ++i) {
352                                 bool start_new_phase = false;
353
354                                 // FIXME: If we sample directly from a texture, we won't need this.
355                                 if (effect->needs_texture_bounce()) {
356                                         start_new_phase = true;
357                                 }
358
359                                 assert(outgoing_links.count(deps[i]) == 1);
360                                 if (outgoing_links[deps[i]].size() > 1 && deps[i]->num_inputs() > 0) {
361                                         // More than one effect uses this as the input,
362                                         // and it is not a texture itself.
363                                         // The easiest thing to do (and probably also the safest
364                                         // performance-wise in most cases) is to bounce it to a texture
365                                         // and then let the next passes read from that.
366                                         start_new_phase = true;
367                                 }
368
369                                 if (deps[i]->changes_output_size()) {
370                                         start_new_phase = true;
371                                 }
372
373                                 if (start_new_phase) {
374                                         effects_todo_other_phases.push(deps[i]);
375                                         this_phase_inputs.push_back(deps[i]);
376                                 } else {
377                                         effects_todo_this_phase.push(deps[i]);
378                                 }
379                         }
380                         continue;
381                 }
382
383                 // No more effects to do this phase. Take all the ones we have,
384                 // and create a GLSL program for it.
385                 if (!this_phase_effects.empty()) {
386                         reverse(this_phase_effects.begin(), this_phase_effects.end());
387                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
388                         output_effects_to_phase.insert(std::make_pair(this_phase_effects.back(), phases.back()));
389                         this_phase_inputs.clear();
390                         this_phase_effects.clear();
391                 }
392                 assert(this_phase_inputs.empty());
393                 assert(this_phase_effects.empty());
394
395                 // If we have no effects left, exit.
396                 if (effects_todo_other_phases.empty()) {
397                         break;
398                 }
399
400                 Effect *effect = effects_todo_other_phases.top();
401                 effects_todo_other_phases.pop();
402
403                 if (completed_effects.count(effect) == 0) {
404                         // Start a new phase, calculating from this effect.
405                         effects_todo_this_phase.push(effect);
406                 }
407         }
408
409         // Finally, since the phases are found from the output but must be executed
410         // from the input(s), reverse them, too.
411         std::reverse(phases.begin(), phases.end());
412 }
413
414 void EffectChain::find_output_size(EffectChain::Phase *phase)
415 {
416         Effect *output_effect = phase->effects.back();
417
418         // If the last effect explicitly sets an output size,
419         // use that.
420         if (output_effect->changes_output_size()) {
421                 output_effect->get_output_size(&phase->output_width, &phase->output_height);
422                 return;
423         }
424
425         // If not, look at the input phases, if any. We select the largest one
426         // (really assuming they all have the same aspect currently), by pixel count.
427         if (!phase->inputs.empty()) {
428                 unsigned best_width = 0, best_height = 0;
429                 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
430                         Effect *input = phase->inputs[i];
431                         assert(output_effects_to_phase.count(input) != 0);
432                         const Phase *input_phase = output_effects_to_phase[input];
433                         assert(input_phase->output_width != 0);
434                         assert(input_phase->output_height != 0);
435                         if (input_phase->output_width * input_phase->output_height > best_width * best_height) {
436                                 best_width = input_phase->output_width;
437                                 best_height = input_phase->output_height;
438                         }
439                 }
440                 assert(best_width != 0);
441                 assert(best_height != 0);
442                 phase->output_width = best_width;
443                 phase->output_height = best_height;
444                 return;
445         }
446
447         // OK, no inputs. Just use the global width/height.
448         // TODO: We probably want to use the texture's size eventually.
449         phase->output_width = width;
450         phase->output_height = height;
451 }
452
453 void EffectChain::finalize()
454 {
455         // Find the output effect. This is, simply, one that has no outgoing links.
456         // If there are multiple ones, the graph is malformed (we do not support
457         // multiple outputs right now).
458         std::vector<Effect *> output_effects;
459         for (unsigned i = 0; i < effects.size(); ++i) {
460                 Effect *effect = effects[i];
461                 if (outgoing_links.count(effect) == 0 || outgoing_links[effect].size() == 0) {
462                         output_effects.push_back(effect);
463                 }
464         }
465         assert(output_effects.size() == 1);
466         Effect *output_effect = output_effects[0];
467
468         // Add normalizers to get the output format right.
469         assert(output_gamma_curve.count(output_effect) != 0);
470         assert(output_color_space.count(output_effect) != 0);
471         ColorSpace current_color_space = output_color_space[output_effect];
472         if (current_color_space != output_format.color_space) {
473                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
474                 colorspace_conversion->set_int("source_space", current_color_space);
475                 colorspace_conversion->set_int("destination_space", output_format.color_space);
476                 std::vector<Effect *> inputs;
477                 inputs.push_back(output_effect);
478                 colorspace_conversion->add_self_to_effect_chain(this, inputs);
479                 output_color_space[colorspace_conversion] = output_format.color_space;
480                 output_effect = colorspace_conversion;
481         }
482         GammaCurve current_gamma_curve = output_gamma_curve[output_effect];
483         if (current_gamma_curve != output_format.gamma_curve) {
484                 if (current_gamma_curve != GAMMA_LINEAR) {
485                         output_effect = normalize_to_linear_gamma(output_effect);
486                         current_gamma_curve = GAMMA_LINEAR;
487                 }
488                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
489                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
490                 std::vector<Effect *> inputs;
491                 inputs.push_back(output_effect);
492                 gamma_conversion->add_self_to_effect_chain(this, inputs);
493                 output_gamma_curve[gamma_conversion] = output_format.gamma_curve;
494                 output_effect = gamma_conversion;
495         }
496
497         // Construct all needed GLSL programs, starting at the output.
498         construct_glsl_programs(output_effect);
499
500         // If we have more than one phase, we need intermediate render-to-texture.
501         // Construct an FBO, and then as many textures as we need.
502         // We choose the simplest option of having one texture per output,
503         // since otherwise this turns into an (albeit simple)
504         // register allocation problem.
505         if (phases.size() > 1) {
506                 glGenFramebuffers(1, &fbo);
507
508                 for (unsigned i = 0; i < phases.size() - 1; ++i) {
509                         find_output_size(phases[i]);
510
511                         Effect *output_effect = phases[i]->effects.back();
512                         GLuint temp_texture;
513                         glGenTextures(1, &temp_texture);
514                         check_error();
515                         glBindTexture(GL_TEXTURE_2D, temp_texture);
516                         check_error();
517                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
518                         check_error();
519                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
520                         check_error();
521                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[i]->output_width, phases[i]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
522                         check_error();
523                         effect_output_textures.insert(std::make_pair(output_effect, temp_texture));
524                         effect_output_texture_sizes.insert(std::make_pair(output_effect, std::make_pair(phases[i]->output_width, phases[i]->output_height)));
525                 }
526         }
527                 
528         for (unsigned i = 0; i < inputs.size(); ++i) {
529                 inputs[i]->finalize();
530         }
531
532         assert(phases[0]->inputs.empty());
533         
534         finalized = true;
535 }
536
537 void EffectChain::render_to_screen()
538 {
539         assert(finalized);
540
541         // Basic state.
542         glDisable(GL_BLEND);
543         check_error();
544         glDisable(GL_DEPTH_TEST);
545         check_error();
546         glDepthMask(GL_FALSE);
547         check_error();
548
549         glMatrixMode(GL_PROJECTION);
550         glLoadIdentity();
551         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
552
553         glMatrixMode(GL_MODELVIEW);
554         glLoadIdentity();
555
556         if (phases.size() > 1) {
557                 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
558                 check_error();
559         }
560
561         std::set<Effect *> generated_mipmaps;
562
563         for (unsigned phase = 0; phase < phases.size(); ++phase) {
564                 // See if the requested output size has changed. If so, we need to recreate
565                 // the texture (and before we start setting up inputs).
566                 if (phase != phases.size() - 1) {
567                         find_output_size(phases[phase]);
568
569                         Effect *output_effect = phases[phase]->effects.back();
570                         assert(effect_output_texture_sizes.count(output_effect) != 0);
571                         std::pair<GLuint, GLuint> old_size = effect_output_texture_sizes[output_effect];
572
573                         if (old_size.first != phases[phase]->output_width ||
574                             old_size.second != phases[phase]->output_height) {
575                                 glActiveTexture(GL_TEXTURE0);
576                                 check_error();
577                                 assert(effect_output_textures.count(output_effect) != 0);
578                                 glBindTexture(GL_TEXTURE_2D, effect_output_textures[output_effect]);
579                                 check_error();
580                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
581                                 check_error();
582                                 effect_output_texture_sizes[output_effect] = std::make_pair(phases[phase]->output_width, phases[phase]->output_height);
583                                 glBindTexture(GL_TEXTURE_2D, 0);
584                                 check_error();
585                         }
586                 }
587
588                 glUseProgram(phases[phase]->glsl_program_num);
589                 check_error();
590
591                 // Set up RTT inputs for this phase.
592                 for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
593                         glActiveTexture(GL_TEXTURE0 + sampler);
594                         Effect *input = phases[phase]->inputs[sampler];
595                         assert(effect_output_textures.count(input) != 0);
596                         glBindTexture(GL_TEXTURE_2D, effect_output_textures[input]);
597                         check_error();
598                         if (phases[phase]->input_needs_mipmaps) {
599                                 if (generated_mipmaps.count(input) == 0) {
600                                         glGenerateMipmap(GL_TEXTURE_2D);
601                                         check_error();
602                                         generated_mipmaps.insert(input);
603                                 }
604                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
605                                 check_error();
606                         } else {
607                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
608                                 check_error();
609                         }
610
611                         assert(effect_ids.count(input));
612                         std::string texture_name = std::string("tex_") + effect_ids[input];
613                         glUniform1i(glGetUniformLocation(phases[phase]->glsl_program_num, texture_name.c_str()), sampler);
614                         check_error();
615                 }
616
617                 // And now the output.
618                 if (phase == phases.size() - 1) {
619                         // Last phase goes directly to the screen.
620                         glBindFramebuffer(GL_FRAMEBUFFER, 0);
621                         check_error();
622                         glViewport(0, 0, width, height);
623                 } else {
624                         Effect *output_effect = phases[phase]->effects.back();
625                         assert(effect_output_textures.count(output_effect) != 0);
626                         glFramebufferTexture2D(
627                                 GL_FRAMEBUFFER,
628                                 GL_COLOR_ATTACHMENT0,
629                                 GL_TEXTURE_2D,
630                                 effect_output_textures[output_effect],
631                                 0);
632                         check_error();
633                         glViewport(0, 0, phases[phase]->output_width, phases[phase]->output_height);
634                 }
635
636                 // Give the required parameters to all the effects.
637                 unsigned sampler_num = phases[phase]->inputs.size();
638                 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
639                         Effect *effect = phases[phase]->effects[i];
640                         effect->set_gl_state(phases[phase]->glsl_program_num, effect_ids[effect], &sampler_num);
641                         check_error();
642                 }
643
644                 // Now draw!
645                 glBegin(GL_QUADS);
646
647                 glTexCoord2f(0.0f, 0.0f);
648                 glVertex2f(0.0f, 0.0f);
649
650                 glTexCoord2f(1.0f, 0.0f);
651                 glVertex2f(1.0f, 0.0f);
652
653                 glTexCoord2f(1.0f, 1.0f);
654                 glVertex2f(1.0f, 1.0f);
655
656                 glTexCoord2f(0.0f, 1.0f);
657                 glVertex2f(0.0f, 1.0f);
658
659                 glEnd();
660                 check_error();
661
662                 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
663                         Effect *effect = phases[phase]->effects[i];
664                         effect->clear_gl_state();
665                 }
666         }
667 }