+ Effect *effect = effects[i];
+ if (outgoing_links.count(effect) == 0 || outgoing_links[effect].size() == 0) {
+ output_effects.push_back(effect);
+ }
+ }
+ assert(output_effects.size() == 1);
+ Effect *output_effect = output_effects[0];
+
+ // Add normalizers to get the output format right.
+ assert(output_gamma_curve.count(output_effect) != 0);
+ assert(output_color_space.count(output_effect) != 0);
+ ColorSpace current_color_space = output_color_space[output_effect];
+ if (current_color_space != output_format.color_space) {
+ ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
+ colorspace_conversion->set_int("source_space", current_color_space);
+ colorspace_conversion->set_int("destination_space", output_format.color_space);
+ std::vector<Effect *> inputs;
+ inputs.push_back(output_effect);
+ colorspace_conversion->add_self_to_effect_chain(this, inputs);
+ output_color_space[colorspace_conversion] = output_format.color_space;
+ output_effect = colorspace_conversion;
+ }
+ GammaCurve current_gamma_curve = output_gamma_curve[output_effect];
+ if (current_gamma_curve != output_format.gamma_curve) {
+ if (current_gamma_curve != GAMMA_LINEAR) {
+ output_effect = normalize_to_linear_gamma(output_effect);
+ current_gamma_curve = GAMMA_LINEAR;
+ }
+ GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
+ gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
+ std::vector<Effect *> inputs;
+ inputs.push_back(output_effect);
+ gamma_conversion->add_self_to_effect_chain(this, inputs);
+ output_gamma_curve[gamma_conversion] = output_format.gamma_curve;
+ output_effect = gamma_conversion;
+ }
+
+ // Construct all needed GLSL programs, starting at the output.
+ construct_glsl_programs(output_effect);
+
+ // If we have more than one phase, we need intermediate render-to-texture.
+ // Construct an FBO, and then as many textures as we need.
+ // We choose the simplest option of having one texture per output,
+ // since otherwise this turns into an (albeit simple)
+ // register allocation problem.
+ if (phases.size() > 1) {
+ glGenFramebuffers(1, &fbo);
+
+ for (unsigned i = 0; i < phases.size() - 1; ++i) {
+ Effect *output_effect = phases[i].effects.back();
+ GLuint temp_texture;
+ glGenTextures(1, &temp_texture);
+ check_error();
+ glBindTexture(GL_TEXTURE_2D, temp_texture);
+ check_error();
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ check_error();
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ check_error();
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ check_error();
+ effect_output_textures.insert(std::make_pair(output_effect, temp_texture));
+ }
+ }
+
+ for (unsigned i = 0; i < inputs.size(); ++i) {
+ inputs[i]->finalize();