+ effect->add_self_to_effect_chain(this, normalized_inputs);
+ return effect;
+}
+
+// GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
+std::string replace_prefix(const std::string &text, const std::string &prefix)
+{
+ std::string output;
+ size_t start = 0;
+
+ while (start < text.size()) {
+ size_t pos = text.find("PREFIX(", start);
+ if (pos == std::string::npos) {
+ output.append(text.substr(start, std::string::npos));
+ break;
+ }
+
+ output.append(text.substr(start, pos - start));
+ output.append(prefix);
+ output.append("_");
+
+ pos += strlen("PREFIX(");
+
+ // Output stuff until we find the matching ), which we then eat.
+ int depth = 1;
+ size_t end_arg_pos = pos;
+ while (end_arg_pos < text.size()) {
+ if (text[end_arg_pos] == '(') {
+ ++depth;
+ } else if (text[end_arg_pos] == ')') {
+ --depth;
+ if (depth == 0) {
+ break;
+ }
+ }
+ ++end_arg_pos;
+ }
+ output.append(text.substr(pos, end_arg_pos - pos));
+ ++end_arg_pos;
+ assert(depth == 0);
+ start = end_arg_pos;
+ }
+ return output;
+}
+
+EffectChain::Phase EffectChain::compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects)
+{
+ assert(!inputs.empty());
+ assert(!effects.empty());
+
+ // Figure out the true set of inputs to this phase. These are the ones
+ // that we need somehow but don't calculate ourselves.
+ std::set<Effect *> effect_set(effects.begin(), effects.end());
+ std::set<Effect *> input_set(inputs.begin(), inputs.end());
+ std::vector<Effect *> true_inputs;
+ std::set_difference(input_set.begin(), input_set.end(),
+ effect_set.begin(), effect_set.end(),
+ std::back_inserter(true_inputs));
+
+ bool input_needs_mipmaps = false;
+ std::string frag_shader = read_file("header.frag");
+
+ // Create functions for all the texture inputs that we need.
+ for (unsigned i = 0; i < true_inputs.size(); ++i) {
+ Effect *effect = true_inputs[i];
+ assert(effect_ids.count(effect) != 0);
+ std::string effect_id = effect_ids[effect];
+
+ frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";
+ frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
+ if (effect->num_inputs() == 0) {
+ // OpenGL's origin is bottom-left, but most graphics software assumes
+ // a top-left origin. Thus, for inputs that come from the user,
+ // we flip the y coordinate. However, for FBOs, the origin
+ // is all correct, so don't do anything.
+ frag_shader += "\ttc.y = 1.0f - tc.y;\n";
+ }
+ frag_shader += "\treturn texture2D(tex_" + effect_id + ", tc);\n";
+ frag_shader += "}\n";
+ frag_shader += "\n";
+ }
+
+ std::string last_effect_id;
+ for (unsigned i = 0; i < effects.size(); ++i) {
+ Effect *effect = effects[i];
+ assert(effect != NULL);
+ assert(effect_ids.count(effect) != 0);
+ std::string effect_id = effect_ids[effect];
+ last_effect_id = effect_id;
+
+ if (incoming_links[effect].size() == 1) {
+ frag_shader += std::string("#define INPUT ") + effect_ids[incoming_links[effect][0]] + "\n";
+ } else {
+ for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
+ char buf[256];
+ sprintf(buf, "#define INPUT%d %s\n", j + 1, effect_ids[incoming_links[effect][j]].c_str());
+ frag_shader += buf;
+ }
+ }
+
+ frag_shader += "\n";
+ frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
+ frag_shader += replace_prefix(effect->output_convenience_uniforms(), effect_id);
+ frag_shader += replace_prefix(effect->output_fragment_shader(), effect_id);
+ frag_shader += "#undef PREFIX\n";
+ frag_shader += "#undef FUNCNAME\n";
+ if (incoming_links[effect].size() == 1) {
+ frag_shader += "#undef INPUT\n";
+ } else {
+ for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
+ char buf[256];
+ sprintf(buf, "#undef INPUT%d\n", j + 1);
+ frag_shader += buf;
+ }
+ }
+ frag_shader += "\n";
+
+ input_needs_mipmaps |= effect->needs_mipmaps();
+ }
+ for (unsigned i = 0; i < effects.size(); ++i) {
+ Effect *effect = effects[i];
+ if (effect->num_inputs() == 0) {
+ effect->set_int("needs_mipmaps", input_needs_mipmaps);
+ }
+ }
+ assert(!last_effect_id.empty());
+ frag_shader += std::string("#define INPUT ") + last_effect_id + "\n";
+ frag_shader.append(read_file("footer.frag"));
+ printf("%s\n", frag_shader.c_str());
+
+ GLuint glsl_program_num = glCreateProgram();
+ GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
+ GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
+ glAttachShader(glsl_program_num, vs_obj);
+ check_error();
+ glAttachShader(glsl_program_num, fs_obj);
+ check_error();
+ glLinkProgram(glsl_program_num);
+ check_error();
+
+ 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) {