+
+ 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)
+{
+ assert(start != NULL);
+ 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.
+ assert(node != NULL);
+
+ // 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) == 1);
+ std::vector<Effect *> deps = incoming_links[node];
+ assert(node->num_inputs() == deps.size());
+ if (!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) {
+ if (node->num_inputs() != 0) {
+ // More than one effect uses this as the input, and it is not a texture itself.
+ // 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.
+ 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.
+ assert(output_gamma_curve.count(last_added_effect()) != 0);
+ assert(output_color_space.count(last_added_effect()) != 0);
+ 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);
+ std::vector<Effect *> inputs;
+ inputs.push_back(last_added_effect());
+ colorspace_conversion->add_self_to_effect_chain(this, inputs);
+ output_color_space[colorspace_conversion] = output_format.color_space;
+ }
+ GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect()]; // FIXME
+ 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);
+ std::vector<Effect *> inputs;
+ inputs.push_back(last_added_effect());
+ gamma_conversion->add_self_to_effect_chain(this, inputs);
+ output_gamma_curve[gamma_conversion] = output_format.gamma_curve;
+ }
+
+ // Construct all needed GLSL programs, starting at the input.
+ std::set<Effect *> completed_effects;
+ construct_glsl_programs(effects[0], &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));
+ }
+ }
+
+ (static_cast<Input *>(effects[0]))->finalize();
+
+ finalized = true;
+}
+
+void EffectChain::render_to_screen()
+{
+ assert(finalized);
+
+ // Basic state.
+ glDisable(GL_BLEND);
+ check_error();
+ glDisable(GL_DEPTH_TEST);
+ check_error();
+ glDepthMask(GL_FALSE);
+ check_error();
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ 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();
+ }
+
+ // Give the required parameters to all the effects.
+ unsigned sampler_num = phases[phase].inputs.size();
+ for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
+ Effect *effect = phases[phase].effects[i];
+ effect->set_gl_state(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
+ }
+
+ // Now draw!
+ glBegin(GL_QUADS);
+
+ glTexCoord2f(0.0f, 0.0f);
+ glVertex2f(0.0f, 0.0f);
+
+ glTexCoord2f(1.0f, 0.0f);
+ glVertex2f(1.0f, 0.0f);
+
+ glTexCoord2f(1.0f, 1.0f);
+ glVertex2f(1.0f, 1.0f);
+
+ glTexCoord2f(0.0f, 1.0f);
+ glVertex2f(0.0f, 1.0f);
+
+ glEnd();
+ check_error();
+
+ for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
+ Effect *effect = phases[phase].effects[i];
+ effect->clear_gl_state();
+ }
+ }