+ if (output->output_alpha_type == ALPHA_PREMULTIPLIED &&
+ output_alpha_format == OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED) {
+ Node *conversion = add_node(new AlphaDivisionEffect());
+ connect_nodes(output, conversion);
+ propagate_alpha();
+ propagate_gamma_and_color_space();
+ }
+ if (output->output_alpha_type == ALPHA_POSTMULTIPLIED &&
+ output_alpha_format == OUTPUT_ALPHA_FORMAT_PREMULTIPLIED) {
+ Node *conversion = add_node(new AlphaMultiplicationEffect());
+ connect_nodes(output, conversion);
+ propagate_alpha();
+ 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.
+ 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) {
+ CHECK(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());
+ CHECK(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 after 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());
+ CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
+ conversion->output_gamma_curve = GAMMA_LINEAR;
+ replace_sender(input, conversion);
+ connect_nodes(input, conversion);
+ }
+
+ // Re-sort topologically, and propagate the new information.
+ propagate_alpha();
+ 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());
+ CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
+ conversion->output_gamma_curve = output_format.gamma_curve;
+ connect_nodes(output, conversion);
+ }
+}
+
+// If the user has requested dither, add a DitherEffect right at the end
+// (after GammaCompressionEffect etc.). This needs to be done after everything else,
+// since dither is about the only effect that can _not_ be done in linear space.
+void EffectChain::add_dither_if_needed()
+{
+ if (num_dither_bits == 0) {
+ return;
+ }
+ Node *output = find_output_node();
+ Node *dither = add_node(new DitherEffect());
+ CHECK(dither->effect->set_int("num_bits", num_dither_bits));
+ connect_nodes(output, dither);
+
+ dither_effect = dither->effect;
+}
+
+// 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()
+{
+ 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()
+{
+ // Save the current locale, and set it to C, so that we can output decimal
+ // numbers with printf and be sure to get them in the format mandated by GLSL.
+ char *saved_locale = setlocale(LC_NUMERIC, "C");
+
+ // 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");
+
+ find_color_spaces_for_inputs();
+ output_dot("step2-input-colorspace.dot");
+
+ propagate_alpha();
+ output_dot("step3-propagated-alpha.dot");
+
+ propagate_gamma_and_color_space();
+ output_dot("step4-propagated-all.dot");
+
+ fix_internal_color_spaces();
+ fix_internal_alpha(6);
+ fix_output_color_space();
+ output_dot("step7-output-colorspacefix.dot");
+ fix_output_alpha();
+ output_dot("step8-output-alphafix.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,
+ // and fix_internal_alpha() since GammaCompressionEffect needs
+ // postmultiplied input.
+ fix_internal_gamma_by_asking_inputs(9);
+ fix_internal_gamma_by_inserting_nodes(10);
+ fix_output_gamma();
+ output_dot("step11-output-gammafix.dot");
+ propagate_alpha();
+ output_dot("step12-output-alpha-propagated.dot");
+ fix_internal_alpha(13);
+ output_dot("step14-output-alpha-fixed.dot");
+ fix_internal_gamma_by_asking_inputs(15);
+ fix_internal_gamma_by_inserting_nodes(16);
+
+ output_dot("step17-before-dither.dot");
+
+ add_dither_if_needed();
+
+ output_dot("step18-final.dot");
+
+ // Construct all needed GLSL programs, starting at the output.
+ construct_glsl_programs(find_output_node());
+
+ output_dot("step19-split-to-phases.dot");