+ if (phases.size() > 1) {
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+ check_error();
+ }
+
+ std::set<Effect *> generated_mipmaps;
+ generated_mipmaps.insert(effects[0]); // Already done further up.
+
+ for (unsigned phase = 0; phase < phases.size(); ++phase) {
+ glUseProgram(phases[phase].glsl_program_num);
+ check_error();
+
+ // Set up RTT inputs for this phase.
+ for (unsigned sampler = 0; sampler < phases[phase].inputs.size(); ++sampler) {
+ glActiveTexture(GL_TEXTURE0 + sampler);
+ Effect *input = phases[phase].inputs[sampler];
+ assert(effect_output_textures.count(input) != 0);
+ glBindTexture(GL_TEXTURE_2D, effect_output_textures[input]);
+ check_error();
+ if (phases[phase].input_needs_mipmaps) {
+ if (generated_mipmaps.count(input) == 0) {
+ glGenerateMipmap(GL_TEXTURE_2D);
+ check_error();
+ generated_mipmaps.insert(input);
+ }
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+ check_error();
+ } else {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ check_error();
+ }
+
+ assert(effect_ids.count(input));
+ std::string texture_name = std::string("tex_") + effect_ids[input];
+ glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, texture_name.c_str()), sampler);
+ check_error();
+ }
+
+ // And now the output.
+ if (phase == phases.size() - 1) {
+ // Last phase goes directly to the screen.
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ check_error();
+ } else {
+ Effect *last_effect = phases[phase].effects.back();
+ assert(effect_output_textures.count(last_effect) != 0);
+ glFramebufferTexture2D(
+ GL_FRAMEBUFFER,
+ GL_COLOR_ATTACHMENT0,
+ GL_TEXTURE_2D,
+ effect_output_textures[last_effect],
+ 0);
+ check_error();
+ }