+// 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, every time an effect wants to change the output size,
+// and of course at the end.
+//
+// We follow a quite simple depth-first search from the output, although
+// without any explicit recursion.
+void EffectChain::construct_glsl_programs(Effect *output)
+{
+ // Which effects have already been completed in this phase?
+ // We need to keep track of it, as an effect with multiple outputs
+ // could otherwise be calculate multiple times.
+ std::set<Effect *> completed_effects;
+
+ // Effects in the current phase, as well as inputs (outputs from other phases
+ // that we depend on). Note that since we start iterating from the end,
+ // the effect list will be in the reverse order.
+ std::vector<Effect *> this_phase_inputs;
+ std::vector<Effect *> this_phase_effects;
+
+ // Effects that we have yet to calculate, but that we know should
+ // be in the current phase.
+ std::stack<Effect *> effects_todo_this_phase;
+
+ // Effects that we have yet to calculate, but that come from other phases.
+ // We delay these until we have this phase done in its entirety,
+ // at which point we pick any of them and start a new phase from that.
+ std::stack<Effect *> effects_todo_other_phases;
+
+ effects_todo_this_phase.push(output);
+
+ for ( ;; ) { // Termination condition within loop.
+ if (!effects_todo_this_phase.empty()) {
+ // OK, we have more to do this phase.
+ Effect *effect = effects_todo_this_phase.top();
+ effects_todo_this_phase.pop();
+
+ // This should currently only happen for effects that are phase outputs,
+ // and we throw those out separately below.
+ assert(completed_effects.count(effect) == 0);
+
+ this_phase_effects.push_back(effect);
+ completed_effects.insert(effect);
+
+ // Find all the dependencies of this effect, and add them to the stack.
+ assert(incoming_links.count(effect) == 1);
+ std::vector<Effect *> deps = incoming_links[effect];
+ assert(effect->num_inputs() == deps.size());
+ for (unsigned i = 0; i < deps.size(); ++i) {
+ bool start_new_phase = false;
+
+ // FIXME: If we sample directly from a texture, we won't need this.
+ if (effect->needs_texture_bounce()) {
+ start_new_phase = true;
+ }
+
+ assert(outgoing_links.count(deps[i]) == 1);
+ if (outgoing_links[deps[i]].size() > 1 && deps[i]->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.
+ start_new_phase = true;
+ }
+
+ if (deps[i]->changes_output_size()) {
+ start_new_phase = true;
+ }
+
+ if (start_new_phase) {
+ effects_todo_other_phases.push(deps[i]);
+ this_phase_inputs.push_back(deps[i]);
+ } else {
+ effects_todo_this_phase.push(deps[i]);
+ }
+ }
+ continue;
+ }
+
+ // No more effects to do this phase. Take all the ones we have,
+ // and create a GLSL program for it.
+ if (!this_phase_effects.empty()) {
+ reverse(this_phase_effects.begin(), this_phase_effects.end());
+ phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
+ output_effects_to_phase.insert(std::make_pair(this_phase_effects.back(), phases.back()));
+ this_phase_inputs.clear();
+ this_phase_effects.clear();
+ }
+ assert(this_phase_inputs.empty());
+ assert(this_phase_effects.empty());
+
+ // If we have no effects left, exit.
+ if (effects_todo_other_phases.empty()) {
+ break;
+ }
+
+ Effect *effect = effects_todo_other_phases.top();
+ effects_todo_other_phases.pop();
+
+ if (completed_effects.count(effect) == 0) {
+ // Start a new phase, calculating from this effect.
+ effects_todo_this_phase.push(effect);
+ }
+ }
+
+ // Finally, since the phases are found from the output but must be executed
+ // from the input(s), reverse them, too.
+ std::reverse(phases.begin(), phases.end());
+}
+
+void EffectChain::find_output_size(EffectChain::Phase *phase)
+{
+ Effect *output_effect = phase->effects.back();
+
+ // If the last effect explicitly sets an output size,
+ // use that.
+ if (output_effect->changes_output_size()) {
+ output_effect->get_output_size(&phase->output_width, &phase->output_height);
+ return;
+ }
+
+ // If not, look at the input phases, if any. We select the largest one
+ // (really assuming they all have the same aspect currently), by pixel count.
+ if (!phase->inputs.empty()) {
+ unsigned best_width = 0, best_height = 0;
+ for (unsigned i = 0; i < phase->inputs.size(); ++i) {
+ Effect *input = phase->inputs[i];
+ assert(output_effects_to_phase.count(input) != 0);
+ const Phase *input_phase = output_effects_to_phase[input];
+ assert(input_phase->output_width != 0);
+ assert(input_phase->output_height != 0);
+ if (input_phase->output_width * input_phase->output_height > best_width * best_height) {
+ best_width = input_phase->output_width;
+ best_height = input_phase->output_height;
+ }
+ }
+ assert(best_width != 0);
+ assert(best_height != 0);
+ phase->output_width = best_width;
+ phase->output_height = best_height;
+ return;
+ }
+
+ // OK, no inputs. Just use the global width/height.
+ // TODO: We probably want to use the texture's size eventually.
+ phase->output_width = width;
+ phase->output_height = height;
+}
+