+// Make so that the output is in the desired color space.
+void EffectChain::fix_output_color_space()
+{
+ Node *output = find_output_node();
+ if (output->output_color_space != output_format.color_space) {
+ Node *conversion = add_node(new ColorspaceConversionEffect());
+ conversion->effect->set_int("source_space", output->output_color_space);
+ conversion->effect->set_int("destination_space", output_format.color_space);
+ conversion->output_color_space = output_format.color_space;
+ connect_nodes(output, conversion);
+ propagate_gamma_and_color_space();
+ }
+}
+
+bool EffectChain::node_needs_gamma_fix(Node *node)
+{
+ if (node->disabled) {
+ return false;
+ }
+
+ // Small hack since the output is not an explicit node:
+ // If we are the last node and our output is in the wrong
+ // space compared to EffectChain's output, we need to fix it.
+ // This will only take us to linear, but fix_output_gamma()
+ // will come and take us to the desired output gamma
+ // if it is needed.
+ //
+ // This needs to be before everything else, since it could
+ // even apply to inputs (if they are the only effect).
+ if (node->outgoing_links.empty() &&
+ node->output_gamma_curve != output_format.gamma_curve &&
+ node->output_gamma_curve != GAMMA_LINEAR) {
+ return true;
+ }
+
+ if (node->effect->num_inputs() == 0) {
+ return false;
+ }
+
+ // propagate_gamma_and_color_space() has already set our output
+ // to GAMMA_INVALID if the inputs differ, so we can rely on that,
+ // except for GammaCompressionEffect.
+ if (node->output_gamma_curve == GAMMA_INVALID) {
+ return true;
+ }
+ if (node->effect->effect_type_id() == "GammaCompressionEffect") {
+ assert(node->incoming_links.size() == 1);
+ return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
+ }
+
+ return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
+}
+
+// Very similar to fix_internal_color_spaces(), but for gamma.
+// There is one difference, though; before we start adding conversion nodes,
+// we see if we can get anything out of asking the sources to deliver
+// linear gamma directly. fix_internal_gamma_by_asking_inputs()
+// does that part, while fix_internal_gamma_by_inserting_nodes()
+// inserts nodes as needed afterwards.
+void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
+{
+ unsigned gamma_propagation_pass = 0;
+ bool found_any;
+ do {
+ found_any = false;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (!node_needs_gamma_fix(node)) {
+ continue;
+ }
+
+ // See if all inputs can give us linear gamma. If not, leave it.
+ std::vector<Node *> nonlinear_inputs;
+ find_all_nonlinear_inputs(node, &nonlinear_inputs);
+ assert(!nonlinear_inputs.empty());
+
+ bool all_ok = true;
+ for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
+ Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
+ all_ok &= input->can_output_linear_gamma();
+ }
+
+ if (!all_ok) {
+ continue;
+ }
+
+ for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
+ nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1);
+ nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
+ }
+
+ // Re-sort topologically, and propagate the new information.
+ propagate_gamma_and_color_space();
+
+ found_any = true;
+ break;
+ }
+
+ char filename[256];
+ sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
+ output_dot(filename);
+ assert(gamma_propagation_pass < 100);
+ } while (found_any);
+}
+
+void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
+{
+ unsigned gamma_propagation_pass = 0;
+ bool found_any;
+ do {
+ found_any = false;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (!node_needs_gamma_fix(node)) {
+ continue;
+ }
+
+ // Special case: We could be an input and still be asked to
+ // fix our gamma; if so, we should be the only node
+ // (as node_needs_gamma_fix() would only return true in
+ // for an input in that case). That means we should insert
+ // a conversion node _after_ ourselves.
+ if (node->incoming_links.empty()) {
+ assert(node->outgoing_links.empty());
+ Node *conversion = add_node(new GammaExpansionEffect());
+ conversion->effect->set_int("source_curve", node->output_gamma_curve);
+ conversion->output_gamma_curve = GAMMA_LINEAR;
+ connect_nodes(node, conversion);
+ }
+
+ // If not, go through each input that is not linear gamma,
+ // and insert a gamma conversion before it.
+ for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
+ Node *input = node->incoming_links[j];
+ assert(input->output_gamma_curve != GAMMA_INVALID);
+ if (input->output_gamma_curve == GAMMA_LINEAR) {
+ continue;
+ }
+ Node *conversion = add_node(new GammaExpansionEffect());
+ conversion->effect->set_int("source_curve", input->output_gamma_curve);
+ conversion->output_gamma_curve = GAMMA_LINEAR;
+ insert_node_between(input, conversion, node);
+ }
+
+ // Re-sort topologically, and propagate the new information.
+ propagate_gamma_and_color_space();
+
+ found_any = true;
+ break;
+ }
+
+ char filename[256];
+ sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
+ output_dot(filename);
+ assert(gamma_propagation_pass < 100);
+ } while (found_any);
+
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ assert(node->output_gamma_curve != GAMMA_INVALID);
+ }
+}
+
+// Make so that the output is in the desired gamma.
+// Note that this assumes linear input gamma, so it might create the need
+// for another pass of fix_internal_gamma().
+void EffectChain::fix_output_gamma()
+{
+ Node *output = find_output_node();
+ if (output->output_gamma_curve != output_format.gamma_curve) {
+ Node *conversion = add_node(new GammaCompressionEffect());
+ conversion->effect->set_int("destination_curve", output_format.gamma_curve);
+ conversion->output_gamma_curve = output_format.gamma_curve;
+ connect_nodes(output, conversion);
+ }
+}
+
+// Find the output node. This is, simply, one that has no outgoing links.
+// If there are multiple ones, the graph is malformed (we do not support
+// multiple outputs right now).
+Node *EffectChain::find_output_node()
+{
+ std::vector<Node *> output_nodes;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ if (node->outgoing_links.empty()) {
+ output_nodes.push_back(node);
+ }
+ }
+ assert(output_nodes.size() == 1);
+ return output_nodes[0];
+}
+
+void EffectChain::finalize()
+{
+ // Output the graph as it is before we do any conversions on it.
+ output_dot("step0-start.dot");
+
+ // Give each effect in turn a chance to rewrite its own part of the graph.
+ // Note that if more effects are added as part of this, they will be
+ // picked up as part of the same for loop, since they are added at the end.
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ nodes[i]->effect->rewrite_graph(this, nodes[i]);
+ }
+ output_dot("step1-rewritten.dot");
+
+ propagate_gamma_and_color_space();
+ output_dot("step2-propagated.dot");
+
+ fix_internal_color_spaces();
+ fix_output_color_space();
+ output_dot("step4-output-colorspacefix.dot");
+
+ // Note that we need to fix gamma after colorspace conversion,
+ // because colorspace conversions might create needs for gamma conversions.
+ // Also, we need to run an extra pass of fix_internal_gamma() after
+ // fixing the output gamma, as we only have conversions to/from linear.
+ fix_internal_gamma_by_asking_inputs(5);
+ fix_internal_gamma_by_inserting_nodes(6);
+ fix_output_gamma();
+ output_dot("step7-output-gammafix.dot");
+ fix_internal_gamma_by_asking_inputs(8);
+ fix_internal_gamma_by_inserting_nodes(9);
+
+ output_dot("step10-final.dot");
+
+ // Construct all needed GLSL programs, starting at the output.
+ construct_glsl_programs(find_output_node());
+
+ output_dot("step11-split-to-phases.dot");
+
+ // 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) {
+ inform_input_sizes(phases[i]);
+ find_output_size(phases[i]);
+
+ Node *output_node = phases[i]->effects.back();
+ glGenTextures(1, &output_node->output_texture);
+ check_error();
+ glBindTexture(GL_TEXTURE_2D, output_node->output_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, phases[i]->output_width, phases[i]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ check_error();
+
+ output_node->output_texture_width = phases[i]->output_width;
+ output_node->output_texture_height = phases[i]->output_height;
+ }
+ inform_input_sizes(phases.back());
+ }
+
+ for (unsigned i = 0; i < inputs.size(); ++i) {
+ inputs[i]->finalize();
+ }
+
+ assert(phases[0]->inputs.empty());
+
+ finalized = true;
+}
+
+void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height)
+{
+ assert(finalized);
+
+ // Save original viewport.
+ GLuint x = 0, y = 0;
+
+ if (width == 0 && height == 0) {
+ GLint viewport[4];
+ glGetIntegerv(GL_VIEWPORT, viewport);
+ x = viewport[0];
+ y = viewport[1];
+ width = viewport[2];
+ height = viewport[3];
+ }
+
+ // Basic state.