+ Phase phase;
+ phase.glsl_program_num = glsl_program_num;
+ phase.input_needs_mipmaps = input_needs_mipmaps;
+ phase.inputs = true_inputs;
+ phase.effects = effects;
+
+ return phase;
+}
+
+// Construct GLSL programs, starting at the given effect and following
+// the chain from there. We end a program every time we come to an effect
+// marked as "needs texture bounce", one that is used by multiple other
+// effects, and of course at the end.
+void EffectChain::construct_glsl_programs(Effect *start, std::set<Effect *> *completed_effects)
+{
+ if (completed_effects->count(start) != 0) {
+ // This has already been done for us.
+ return;
+ }
+
+ std::vector<Effect *> this_phase_inputs; // Also includes all intermediates; these will be filtered away later.
+ std::vector<Effect *> this_phase_effects;
+ Effect *node = start;
+ for ( ;; ) { // Termination condition within loop.
+ if (node == NULL) {
+ this_phase_inputs.push_back(node);
+ } else {
+ // Check that we have all the inputs we need for this effect.
+ // If not, we end the phase here right away; the other side
+ // of the input chain will eventually come and pick the effect up.
+ assert(incoming_links.count(node) != 0);
+ std::vector<Effect *> deps = incoming_links[node];
+ assert(!deps.empty());
+ bool have_all_deps = true;
+ for (unsigned i = 0; i < deps.size(); ++i) {
+ if (completed_effects->count(deps[i]) == 0) {
+ have_all_deps = false;
+ break;
+ }
+ }
+
+ if (!have_all_deps) {
+ if (!this_phase_effects.empty()) {
+ phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
+ }
+ return;
+ }
+ this_phase_inputs.insert(this_phase_inputs.end(), deps.begin(), deps.end());
+ this_phase_effects.push_back(node);
+ }
+ completed_effects->insert(node);
+
+ // Find all the effects that use this one as a direct input.
+ if (outgoing_links.count(node) == 0) {
+ // End of the line; output.
+ phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
+ return;
+ }
+
+ std::vector<Effect *> next = outgoing_links[node];
+ assert(!next.empty());
+ if (next.size() > 1) {
+ // More than one effect uses this as the input.
+ // The easiest thing to do (and probably also the safest
+ // performance-wise in most cases) is to bounce it to a texture
+ // and then let the next passes read from that.
+ if (node != NULL) {
+ phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
+ }
+
+ // Start phases for all the effects that need us (in arbitrary order).
+ for (unsigned i = 0; i < next.size(); ++i) {
+ construct_glsl_programs(next[i], completed_effects);
+ }
+ return;
+ }
+
+ // OK, only one effect uses this as the input. Keep iterating,
+ // but first see if it requires a texture bounce; if so, give it
+ // one by starting a new phase.
+ node = next[0];
+ if (node->needs_texture_bounce()) {
+ phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
+ this_phase_inputs.clear();
+ this_phase_effects.clear();
+ }
+ }
+}
+
+void EffectChain::finalize()
+{
+ // Add normalizers to get the output format right.
+ GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect]; // FIXME
+ ColorSpace current_color_space = output_color_space[last_added_effect]; // FIXME
+ 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);
+ effects.push_back(colorspace_conversion);
+ current_color_space = output_format.color_space;
+ }
+ if (current_gamma_curve != output_format.gamma_curve) {
+ if (current_gamma_curve != GAMMA_LINEAR) {
+ normalize_to_linear_gamma(last_added_effect); // FIXME
+ }
+ assert(current_gamma_curve == GAMMA_LINEAR);
+ GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
+ gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
+ effects.push_back(gamma_conversion);
+ current_gamma_curve = output_format.gamma_curve;
+ }
+
+ // Construct all needed GLSL programs, starting at the input.
+ std::set<Effect *> completed_effects;
+ construct_glsl_programs(NULL, &completed_effects);
+
+ // 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));
+ }
+ }
+
+ // Translate the input format to OpenGL's enums.