]> git.sesse.net Git - movit/blob - effect_chain.cpp
Unbreak the final normalizers.
[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 <algorithm>
11 #include <set>
12
13 #include "util.h"
14 #include "effect_chain.h"
15 #include "gamma_expansion_effect.h"
16 #include "gamma_compression_effect.h"
17 #include "lift_gamma_gain_effect.h"
18 #include "colorspace_conversion_effect.h"
19 #include "sandbox_effect.h"
20 #include "saturation_effect.h"
21 #include "mirror_effect.h"
22 #include "vignette_effect.h"
23 #include "blur_effect.h"
24 #include "diffusion_effect.h"
25
26 EffectChain::EffectChain(unsigned width, unsigned height)
27         : width(width),
28           height(height),
29           use_srgb_texture_format(false),
30           finalized(false) {}
31
32 void EffectChain::add_input(const ImageFormat &format)
33 {
34         input_format = format;
35         output_color_space.insert(std::make_pair(static_cast<Effect *>(NULL), format.color_space));
36         output_gamma_curve.insert(std::make_pair(static_cast<Effect *>(NULL), format.gamma_curve));
37         effect_ids.insert(std::make_pair(static_cast<Effect *>(NULL), "src_image"));
38 }
39
40 void EffectChain::add_output(const ImageFormat &format)
41 {
42         output_format = format;
43 }
44
45 void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
46 {
47         char effect_id[256];
48         sprintf(effect_id, "eff%u", (unsigned)effects.size());
49
50         effects.push_back(effect);
51         effect_ids.insert(std::make_pair(effect, effect_id));
52         assert(inputs.size() == effect->num_inputs());
53         for (unsigned i = 0; i < inputs.size(); ++i) {
54                 if (inputs[i] != NULL) {
55                         assert(std::find(effects.begin(), effects.end(), inputs[i]) != effects.end());
56                 }
57                 outgoing_links[inputs[i]].push_back(effect);
58         }
59         incoming_links.insert(std::make_pair(effect, inputs));
60         output_gamma_curve[effect] = output_gamma_curve[last_added_effect()];
61         output_color_space[effect] = output_color_space[last_added_effect()];
62 }
63
64 Effect *instantiate_effect(EffectId effect)
65 {
66         switch (effect) {
67         case EFFECT_GAMMA_EXPANSION:
68                 return new GammaExpansionEffect();
69         case EFFECT_GAMMA_COMPRESSION:
70                 return new GammaCompressionEffect();
71         case EFFECT_COLOR_SPACE_CONVERSION:
72                 return new ColorSpaceConversionEffect();
73         case EFFECT_SANDBOX:
74                 return new SandboxEffect();
75         case EFFECT_LIFT_GAMMA_GAIN:
76                 return new LiftGammaGainEffect();
77         case EFFECT_SATURATION:
78                 return new SaturationEffect();
79         case EFFECT_MIRROR:
80                 return new MirrorEffect();
81         case EFFECT_VIGNETTE:
82                 return new VignetteEffect();
83         case EFFECT_BLUR:
84                 return new BlurEffect();
85         case EFFECT_DIFFUSION:
86                 return new DiffusionEffect();
87         }
88         assert(false);
89 }
90
91 Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
92 {
93         assert(output_gamma_curve.count(input) != 0);
94         if (output_gamma_curve[input] == GAMMA_sRGB) {
95                 // TODO: check if the extension exists
96                 use_srgb_texture_format = true;
97                 output_gamma_curve[input] = GAMMA_LINEAR;
98                 return input;
99         } else {
100                 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
101                 gamma_conversion->set_int("source_curve", output_gamma_curve[input]);
102                 std::vector<Effect *> inputs;
103                 inputs.push_back(input);
104                 gamma_conversion->add_self_to_effect_chain(this, inputs);
105                 output_gamma_curve[gamma_conversion] = GAMMA_LINEAR;
106                 return gamma_conversion;
107         }
108 }
109
110 Effect *EffectChain::normalize_to_srgb(Effect *input)
111 {
112         assert(output_gamma_curve.count(input) != 0);
113         assert(output_color_space.count(input) != 0);
114         assert(output_gamma_curve[input] == GAMMA_LINEAR);
115         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
116         colorspace_conversion->set_int("source_space", output_color_space[input]);
117         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
118         std::vector<Effect *> inputs;
119         inputs.push_back(input);
120         colorspace_conversion->add_self_to_effect_chain(this, inputs);
121         output_color_space[colorspace_conversion] = COLORSPACE_sRGB;
122         return colorspace_conversion;
123 }
124
125 Effect *EffectChain::add_effect(EffectId effect_id, const std::vector<Effect *> &inputs)
126 {
127         Effect *effect = instantiate_effect(effect_id);
128
129         assert(inputs.size() == effect->num_inputs());
130
131         std::vector<Effect *> normalized_inputs = inputs;
132         for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
133                 assert(output_gamma_curve.count(normalized_inputs[i]) != 0);
134                 if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
135                         normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
136                 }
137                 assert(output_color_space.count(normalized_inputs[i]) != 0);
138                 if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
139                         normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
140                 }
141         }
142
143         effect->add_self_to_effect_chain(this, normalized_inputs);
144         return effect;
145 }
146
147 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
148 std::string replace_prefix(const std::string &text, const std::string &prefix)
149 {
150         std::string output;
151         size_t start = 0;
152
153         while (start < text.size()) {
154                 size_t pos = text.find("PREFIX(", start);
155                 if (pos == std::string::npos) {
156                         output.append(text.substr(start, std::string::npos));
157                         break;
158                 }
159
160                 output.append(text.substr(start, pos - start));
161                 output.append(prefix);
162                 output.append("_");
163
164                 pos += strlen("PREFIX(");
165         
166                 // Output stuff until we find the matching ), which we then eat.
167                 int depth = 1;
168                 size_t end_arg_pos = pos;
169                 while (end_arg_pos < text.size()) {
170                         if (text[end_arg_pos] == '(') {
171                                 ++depth;
172                         } else if (text[end_arg_pos] == ')') {
173                                 --depth;
174                                 if (depth == 0) {
175                                         break;
176                                 }
177                         }
178                         ++end_arg_pos;
179                 }
180                 output.append(text.substr(pos, end_arg_pos - pos));
181                 ++end_arg_pos;
182                 assert(depth == 0);
183                 start = end_arg_pos;
184         }
185         return output;
186 }
187
188 EffectChain::Phase EffectChain::compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects)
189 {
190         assert(!inputs.empty());
191         assert(!effects.empty());
192
193         // Figure out the true set of inputs to this phase. These are the ones
194         // that we need somehow but don't calculate ourselves.
195         std::set<Effect *> effect_set(effects.begin(), effects.end());
196         std::set<Effect *> input_set(inputs.begin(), inputs.end());
197         std::vector<Effect *> true_inputs;
198         std::set_difference(input_set.begin(), input_set.end(),
199                 effect_set.begin(), effect_set.end(),
200                 std::back_inserter(true_inputs));
201
202         bool input_needs_mipmaps = false;
203         std::string frag_shader = read_file("header.frag");
204
205         // Create functions for all the texture inputs that we need.
206         for (unsigned i = 0; i < true_inputs.size(); ++i) {
207                 Effect *effect = true_inputs[i];
208                 assert(effect_ids.count(effect) != 0);
209                 std::string effect_id = effect_ids[effect];
210         
211                 frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";       
212                 frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
213                 if (effect == NULL) {
214                         // OpenGL's origin is bottom-left, but most graphics software assumes
215                         // a top-left origin. Thus, for inputs that come from the user,
216                         // we flip the y coordinate. However, for FBOs, the origin
217                         // is all correct, so don't do anything.
218                         frag_shader += "\ttc.y = 1.0f - tc.y;\n";
219                 }
220                 frag_shader += "\treturn texture2D(tex_" + effect_id + ", tc);\n";
221                 frag_shader += "}\n";
222                 frag_shader += "\n";
223         }
224
225         std::string last_effect_id;
226         for (unsigned i = 0; i < effects.size(); ++i) {
227                 Effect *effect = effects[i];
228                 assert(effect != NULL);
229                 assert(effect_ids.count(effect) != 0);
230                 std::string effect_id = effect_ids[effect];
231                 last_effect_id = effect_id;
232
233                 if (incoming_links[effect].size() == 1) {
234                         frag_shader += std::string("#define INPUT ") + effect_ids[incoming_links[effect][0]] + "\n";
235                 } else {
236                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
237                                 char buf[256];
238                                 sprintf(buf, "#define INPUT%d %s\n", j + 1, effect_ids[incoming_links[effect][j]].c_str());
239                                 frag_shader += buf;
240                         }
241                 }
242         
243                 frag_shader += "\n";
244                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
245                 frag_shader += replace_prefix(effect->output_convenience_uniforms(), effect_id);
246                 frag_shader += replace_prefix(effect->output_fragment_shader(), effect_id);
247                 frag_shader += "#undef PREFIX\n";
248                 frag_shader += "#undef FUNCNAME\n";
249                 if (incoming_links[effect].size() == 1) {
250                         frag_shader += "#undef INPUT\n";
251                 } else {
252                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
253                                 char buf[256];
254                                 sprintf(buf, "#undef INPUT%d\n", j + 1);
255                                 frag_shader += buf;
256                         }
257                 }
258                 frag_shader += "\n";
259
260                 input_needs_mipmaps |= effect->needs_mipmaps();
261         }
262         assert(!last_effect_id.empty());
263         frag_shader += std::string("#define INPUT ") + last_effect_id + "\n";
264         frag_shader.append(read_file("footer.frag"));
265         printf("%s\n", frag_shader.c_str());
266         
267         GLuint glsl_program_num = glCreateProgram();
268         GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
269         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
270         glAttachShader(glsl_program_num, vs_obj);
271         check_error();
272         glAttachShader(glsl_program_num, fs_obj);
273         check_error();
274         glLinkProgram(glsl_program_num);
275         check_error();
276
277         Phase phase;
278         phase.glsl_program_num = glsl_program_num;
279         phase.input_needs_mipmaps = input_needs_mipmaps;
280         phase.inputs = true_inputs;
281         phase.effects = effects;
282
283         return phase;
284 }
285
286 // Construct GLSL programs, starting at the given effect and following
287 // the chain from there. We end a program every time we come to an effect
288 // marked as "needs texture bounce", one that is used by multiple other
289 // effects, and of course at the end.
290 void EffectChain::construct_glsl_programs(Effect *start, std::set<Effect *> *completed_effects)
291 {
292         if (completed_effects->count(start) != 0) {
293                 // This has already been done for us.
294                 return;
295         }
296
297         std::vector<Effect *> this_phase_inputs;  // Also includes all intermediates; these will be filtered away later.
298         std::vector<Effect *> this_phase_effects;
299         Effect *node = start;
300         for ( ;; ) {  // Termination condition within loop.
301                 if (node == NULL) {
302                         this_phase_inputs.push_back(node);
303                 } else {
304                         // Check that we have all the inputs we need for this effect.
305                         // If not, we end the phase here right away; the other side
306                         // of the input chain will eventually come and pick the effect up.
307                         assert(incoming_links.count(node) != 0);
308                         std::vector<Effect *> deps = incoming_links[node];
309                         assert(!deps.empty());
310                         bool have_all_deps = true;
311                         for (unsigned i = 0; i < deps.size(); ++i) {
312                                 if (completed_effects->count(deps[i]) == 0) {
313                                         have_all_deps = false;
314                                         break;
315                                 }
316                         }
317                 
318                         if (!have_all_deps) {
319                                 if (!this_phase_effects.empty()) {
320                                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
321                                 }
322                                 return;
323                         }
324                         this_phase_inputs.insert(this_phase_inputs.end(), deps.begin(), deps.end());    
325                         this_phase_effects.push_back(node);
326                 }
327                 completed_effects->insert(node);        
328
329                 // Find all the effects that use this one as a direct input.
330                 if (outgoing_links.count(node) == 0) {
331                         // End of the line; output.
332                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
333                         return;
334                 }
335
336                 std::vector<Effect *> next = outgoing_links[node];
337                 assert(!next.empty());
338                 if (next.size() > 1) {
339                         // More than one effect uses this as the input.
340                         // The easiest thing to do (and probably also the safest
341                         // performance-wise in most cases) is to bounce it to a texture
342                         // and then let the next passes read from that.
343                         if (node != NULL) {
344                                 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
345                         }
346
347                         // Start phases for all the effects that need us (in arbitrary order).
348                         for (unsigned i = 0; i < next.size(); ++i) {
349                                 construct_glsl_programs(next[i], completed_effects);
350                         }
351                         return;
352                 }
353         
354                 // OK, only one effect uses this as the input. Keep iterating,
355                 // but first see if it requires a texture bounce; if so, give it
356                 // one by starting a new phase.
357                 node = next[0];
358                 if (node->needs_texture_bounce()) {
359                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
360                         this_phase_inputs.clear();
361                         this_phase_effects.clear();
362                 }
363         }
364 }
365
366 void EffectChain::finalize()
367 {
368         // Add normalizers to get the output format right.
369         assert(output_gamma_curve.count(last_added_effect()) != 0);
370         assert(output_color_space.count(last_added_effect()) != 0);
371         ColorSpace current_color_space = output_color_space[last_added_effect()];  // FIXME
372         if (current_color_space != output_format.color_space) {
373                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
374                 colorspace_conversion->set_int("source_space", current_color_space);
375                 colorspace_conversion->set_int("destination_space", output_format.color_space);
376                 std::vector<Effect *> inputs;
377                 inputs.push_back(last_added_effect());
378                 colorspace_conversion->add_self_to_effect_chain(this, inputs);
379                 output_color_space[colorspace_conversion] = output_format.color_space;
380         }
381         GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect()];  // FIXME
382         if (current_gamma_curve != output_format.gamma_curve) {
383                 if (current_gamma_curve != GAMMA_LINEAR) {
384                         normalize_to_linear_gamma(last_added_effect());  // FIXME
385                 }
386                 assert(current_gamma_curve == GAMMA_LINEAR);
387                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
388                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
389                 std::vector<Effect *> inputs;
390                 inputs.push_back(last_added_effect());
391                 gamma_conversion->add_self_to_effect_chain(this, inputs);
392                 output_gamma_curve[gamma_conversion] = output_format.gamma_curve;
393         }
394
395         // Construct all needed GLSL programs, starting at the input.
396         std::set<Effect *> completed_effects;
397         construct_glsl_programs(NULL, &completed_effects);
398
399         // If we have more than one phase, we need intermediate render-to-texture.
400         // Construct an FBO, and then as many textures as we need.
401         // We choose the simplest option of having one texture per output,
402         // since otherwise this turns into an (albeit simple)
403         // register allocation problem.
404         if (phases.size() > 1) {
405                 glGenFramebuffers(1, &fbo);
406
407                 for (unsigned i = 0; i < phases.size() - 1; ++i) {
408                         Effect *output_effect = phases[i].effects.back();
409                         GLuint temp_texture;
410                         glGenTextures(1, &temp_texture);
411                         check_error();
412                         glBindTexture(GL_TEXTURE_2D, temp_texture);
413                         check_error();
414                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
415                         check_error();
416                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
417                         check_error();
418                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
419                         check_error();
420                         effect_output_textures.insert(std::make_pair(output_effect, temp_texture));
421                 }
422         }
423         
424         // Translate the input format to OpenGL's enums.
425         GLenum internal_format;
426         if (use_srgb_texture_format) {
427                 internal_format = GL_SRGB8;
428         } else {
429                 internal_format = GL_RGBA8;
430         }
431         if (input_format.pixel_format == FORMAT_RGB) {
432                 format = GL_RGB;
433                 bytes_per_pixel = 3;
434         } else if (input_format.pixel_format == FORMAT_RGBA) {
435                 format = GL_RGBA;
436                 bytes_per_pixel = 4;
437         } else if (input_format.pixel_format == FORMAT_BGR) {
438                 format = GL_BGR;
439                 bytes_per_pixel = 3;
440         } else if (input_format.pixel_format == FORMAT_BGRA) {
441                 format = GL_BGRA;
442                 bytes_per_pixel = 4;
443         } else {
444                 assert(false);
445         }
446
447         // Create PBO to hold the texture holding the input image, and then the texture itself.
448         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
449         check_error();
450         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
451         check_error();
452         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
453         check_error();
454         
455         glGenTextures(1, &source_image_num);
456         check_error();
457         glBindTexture(GL_TEXTURE_2D, source_image_num);
458         check_error();
459         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
460         check_error();
461         // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here.
462         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, phases[0].input_needs_mipmaps ? GL_TRUE : GL_FALSE);
463         check_error();
464         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
465         check_error();
466
467         finalized = true;
468 }
469
470 void EffectChain::render_to_screen(unsigned char *src)
471 {
472         assert(finalized);
473
474         // Copy the pixel data into the PBO.
475         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
476         check_error();
477         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
478         memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
479         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
480         check_error();
481
482         // Re-upload the texture from the PBO.
483         glActiveTexture(GL_TEXTURE0);
484         check_error();
485         glBindTexture(GL_TEXTURE_2D, source_image_num);
486         check_error();
487         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
488         check_error();
489         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
490         check_error();
491         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
492         check_error();
493         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
494         check_error();
495
496         // Basic state.
497         glDisable(GL_BLEND);
498         check_error();
499         glDisable(GL_DEPTH_TEST);
500         check_error();
501         glDepthMask(GL_FALSE);
502         check_error();
503
504         glMatrixMode(GL_PROJECTION);
505         glLoadIdentity();
506         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
507
508         glMatrixMode(GL_MODELVIEW);
509         glLoadIdentity();
510
511         if (phases.size() > 1) {
512                 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
513                 check_error();
514         }
515
516         std::set<Effect *> generated_mipmaps;
517         generated_mipmaps.insert(NULL);  // Already done further up.
518
519         for (unsigned phase = 0; phase < phases.size(); ++phase) {
520                 glUseProgram(phases[phase].glsl_program_num);
521                 check_error();
522
523                 // Set up inputs for this phase.
524                 assert(!phases[phase].inputs.empty());
525                 for (unsigned sampler = 0; sampler < phases[phase].inputs.size(); ++sampler) {
526                         glActiveTexture(GL_TEXTURE0 + sampler);
527                         Effect *input = phases[phase].inputs[sampler];
528                         if (input == NULL) {
529                                 glBindTexture(GL_TEXTURE_2D, source_image_num);
530                                 check_error();
531                         } else {
532                                 assert(effect_output_textures.count(input) != 0);
533                                 glBindTexture(GL_TEXTURE_2D, effect_output_textures[input]);
534                                 check_error();
535                         }
536                         if (phases[phase].input_needs_mipmaps) {
537                                 if (generated_mipmaps.count(input) == 0) {
538                                         glGenerateMipmap(GL_TEXTURE_2D);
539                                         check_error();
540                                         generated_mipmaps.insert(input);
541                                 }
542                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
543                                 check_error();
544                         } else {
545                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
546                                 check_error();
547                         }
548
549                         assert(effect_ids.count(input));
550                         std::string texture_name = std::string("tex_") + effect_ids[input];
551                         glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, texture_name.c_str()), sampler);
552                         check_error();
553                 }
554
555                 // And now the output.
556                 if (phase == phases.size() - 1) {
557                         // Last phase goes directly to the screen.
558                         glBindFramebuffer(GL_FRAMEBUFFER, 0);
559                         check_error();
560                 } else {
561                         Effect *last_effect = phases[phase].effects.back();
562                         assert(effect_output_textures.count(last_effect) != 0);
563                         glFramebufferTexture2D(
564                                 GL_FRAMEBUFFER,
565                                 GL_COLOR_ATTACHMENT0,
566                                 GL_TEXTURE_2D,
567                                 effect_output_textures[last_effect],
568                                 0);
569                         check_error();
570                 }
571
572                 // Give the required parameters to all the effects.
573                 unsigned sampler_num = phases[phase].inputs.size();
574                 for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
575                         Effect *effect = phases[phase].effects[i];
576                         effect->set_uniforms(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
577                 }
578
579                 // Now draw!
580                 glBegin(GL_QUADS);
581
582                 glTexCoord2f(0.0f, 0.0f);
583                 glVertex2f(0.0f, 0.0f);
584
585                 glTexCoord2f(1.0f, 0.0f);
586                 glVertex2f(1.0f, 0.0f);
587
588                 glTexCoord2f(1.0f, 1.0f);
589                 glVertex2f(1.0f, 1.0f);
590
591                 glTexCoord2f(0.0f, 1.0f);
592                 glVertex2f(0.0f, 1.0f);
593
594                 glEnd();
595                 check_error();
596
597                 // HACK
598                 glActiveTexture(GL_TEXTURE0);
599                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
600                 check_error();
601                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
602                 check_error();
603         }
604 }