1 #define GL_GLEXT_PROTOTYPES 1
16 #include "effect_chain.h"
17 #include "gamma_expansion_effect.h"
18 #include "gamma_compression_effect.h"
19 #include "colorspace_conversion_effect.h"
20 #include "alpha_multiplication_effect.h"
21 #include "alpha_division_effect.h"
22 #include "dither_effect.h"
26 EffectChain::EffectChain(float aspect_nom, float aspect_denom)
27 : aspect_nom(aspect_nom),
28 aspect_denom(aspect_denom),
34 EffectChain::~EffectChain()
36 for (unsigned i = 0; i < nodes.size(); ++i) {
37 if (nodes[i]->output_texture != 0) {
38 glDeleteTextures(1, &nodes[i]->output_texture);
40 delete nodes[i]->effect;
43 for (unsigned i = 0; i < phases.size(); ++i) {
44 glDeleteProgram(phases[i]->glsl_program_num);
45 glDeleteShader(phases[i]->vertex_shader);
46 glDeleteShader(phases[i]->fragment_shader);
50 glDeleteFramebuffers(1, &fbo);
54 Input *EffectChain::add_input(Input *input)
56 inputs.push_back(input);
61 void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
63 output_format = format;
64 output_alpha_format = alpha_format;
67 Node *EffectChain::add_node(Effect *effect)
70 sprintf(effect_id, "eff%u", (unsigned)nodes.size());
72 Node *node = new Node;
73 node->effect = effect;
74 node->disabled = false;
75 node->effect_id = effect_id;
76 node->output_color_space = COLORSPACE_INVALID;
77 node->output_gamma_curve = GAMMA_INVALID;
78 node->output_alpha_type = ALPHA_INVALID;
79 node->output_texture = 0;
81 nodes.push_back(node);
82 node_map[effect] = node;
86 void EffectChain::connect_nodes(Node *sender, Node *receiver)
88 sender->outgoing_links.push_back(receiver);
89 receiver->incoming_links.push_back(sender);
92 void EffectChain::replace_receiver(Node *old_receiver, Node *new_receiver)
94 new_receiver->incoming_links = old_receiver->incoming_links;
95 old_receiver->incoming_links.clear();
97 for (unsigned i = 0; i < new_receiver->incoming_links.size(); ++i) {
98 Node *sender = new_receiver->incoming_links[i];
99 for (unsigned j = 0; j < sender->outgoing_links.size(); ++j) {
100 if (sender->outgoing_links[j] == old_receiver) {
101 sender->outgoing_links[j] = new_receiver;
107 void EffectChain::replace_sender(Node *old_sender, Node *new_sender)
109 new_sender->outgoing_links = old_sender->outgoing_links;
110 old_sender->outgoing_links.clear();
112 for (unsigned i = 0; i < new_sender->outgoing_links.size(); ++i) {
113 Node *receiver = new_sender->outgoing_links[i];
114 for (unsigned j = 0; j < receiver->incoming_links.size(); ++j) {
115 if (receiver->incoming_links[j] == old_sender) {
116 receiver->incoming_links[j] = new_sender;
122 void EffectChain::insert_node_between(Node *sender, Node *middle, Node *receiver)
124 for (unsigned i = 0; i < sender->outgoing_links.size(); ++i) {
125 if (sender->outgoing_links[i] == receiver) {
126 sender->outgoing_links[i] = middle;
127 middle->incoming_links.push_back(sender);
130 for (unsigned i = 0; i < receiver->incoming_links.size(); ++i) {
131 if (receiver->incoming_links[i] == sender) {
132 receiver->incoming_links[i] = middle;
133 middle->outgoing_links.push_back(receiver);
137 assert(middle->incoming_links.size() == middle->effect->num_inputs());
140 void EffectChain::find_all_nonlinear_inputs(Node *node, std::vector<Node *> *nonlinear_inputs)
142 if (node->output_gamma_curve == GAMMA_LINEAR &&
143 node->effect->effect_type_id() != "GammaCompressionEffect") {
146 if (node->effect->num_inputs() == 0) {
147 nonlinear_inputs->push_back(node);
149 assert(node->effect->num_inputs() == node->incoming_links.size());
150 for (unsigned i = 0; i < node->incoming_links.size(); ++i) {
151 find_all_nonlinear_inputs(node->incoming_links[i], nonlinear_inputs);
156 Effect *EffectChain::add_effect(Effect *effect, const std::vector<Effect *> &inputs)
158 assert(inputs.size() == effect->num_inputs());
159 Node *node = add_node(effect);
160 for (unsigned i = 0; i < inputs.size(); ++i) {
161 assert(node_map.count(inputs[i]) != 0);
162 connect_nodes(node_map[inputs[i]], node);
167 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
168 std::string replace_prefix(const std::string &text, const std::string &prefix)
173 while (start < text.size()) {
174 size_t pos = text.find("PREFIX(", start);
175 if (pos == std::string::npos) {
176 output.append(text.substr(start, std::string::npos));
180 output.append(text.substr(start, pos - start));
181 output.append(prefix);
184 pos += strlen("PREFIX(");
186 // Output stuff until we find the matching ), which we then eat.
188 size_t end_arg_pos = pos;
189 while (end_arg_pos < text.size()) {
190 if (text[end_arg_pos] == '(') {
192 } else if (text[end_arg_pos] == ')') {
200 output.append(text.substr(pos, end_arg_pos - pos));
208 Phase *EffectChain::compile_glsl_program(
209 const std::vector<Node *> &inputs,
210 const std::vector<Node *> &effects)
212 assert(!effects.empty());
214 // Deduplicate the inputs.
215 std::vector<Node *> true_inputs = inputs;
216 std::sort(true_inputs.begin(), true_inputs.end());
217 true_inputs.erase(std::unique(true_inputs.begin(), true_inputs.end()), true_inputs.end());
219 bool input_needs_mipmaps = false;
220 std::string frag_shader = read_file("header.frag");
222 // Create functions for all the texture inputs that we need.
223 for (unsigned i = 0; i < true_inputs.size(); ++i) {
224 Node *input = true_inputs[i];
226 frag_shader += std::string("uniform sampler2D tex_") + input->effect_id + ";\n";
227 frag_shader += std::string("vec4 ") + input->effect_id + "(vec2 tc) {\n";
228 frag_shader += "\treturn texture2D(tex_" + input->effect_id + ", tc);\n";
229 frag_shader += "}\n";
233 std::vector<Node *> sorted_effects = topological_sort(effects);
235 for (unsigned i = 0; i < sorted_effects.size(); ++i) {
236 Node *node = sorted_effects[i];
238 if (node->incoming_links.size() == 1) {
239 frag_shader += std::string("#define INPUT ") + node->incoming_links[0]->effect_id + "\n";
241 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
243 sprintf(buf, "#define INPUT%d %s\n", j + 1, node->incoming_links[j]->effect_id.c_str());
249 frag_shader += std::string("#define FUNCNAME ") + node->effect_id + "\n";
250 frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), node->effect_id);
251 frag_shader += replace_prefix(node->effect->output_fragment_shader(), node->effect_id);
252 frag_shader += "#undef PREFIX\n";
253 frag_shader += "#undef FUNCNAME\n";
254 if (node->incoming_links.size() == 1) {
255 frag_shader += "#undef INPUT\n";
257 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
259 sprintf(buf, "#undef INPUT%d\n", j + 1);
265 input_needs_mipmaps |= node->effect->needs_mipmaps();
267 for (unsigned i = 0; i < sorted_effects.size(); ++i) {
268 Node *node = sorted_effects[i];
269 if (node->effect->num_inputs() == 0) {
270 CHECK(node->effect->set_int("needs_mipmaps", input_needs_mipmaps));
273 frag_shader += std::string("#define INPUT ") + sorted_effects.back()->effect_id + "\n";
274 frag_shader.append(read_file("footer.frag"));
276 if (movit_debug_level == MOVIT_DEBUG_ON) {
277 // Output shader to a temporary file, for easier debugging.
278 static int compiled_shader_num = 0;
280 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
281 FILE *fp = fopen(filename, "w");
286 fprintf(fp, "%s\n", frag_shader.c_str());
290 GLuint glsl_program_num = glCreateProgram();
291 GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
292 GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
293 glAttachShader(glsl_program_num, vs_obj);
295 glAttachShader(glsl_program_num, fs_obj);
297 glLinkProgram(glsl_program_num);
300 Phase *phase = new Phase;
301 phase->glsl_program_num = glsl_program_num;
302 phase->vertex_shader = vs_obj;
303 phase->fragment_shader = fs_obj;
304 phase->input_needs_mipmaps = input_needs_mipmaps;
305 phase->inputs = true_inputs;
306 phase->effects = sorted_effects;
311 // Construct GLSL programs, starting at the given effect and following
312 // the chain from there. We end a program every time we come to an effect
313 // marked as "needs texture bounce", one that is used by multiple other
314 // effects, every time an effect wants to change the output size,
315 // and of course at the end.
317 // We follow a quite simple depth-first search from the output, although
318 // without any explicit recursion.
319 void EffectChain::construct_glsl_programs(Node *output)
321 // Which effects have already been completed?
322 // We need to keep track of it, as an effect with multiple outputs
323 // could otherwise be calculated multiple times.
324 std::set<Node *> completed_effects;
326 // Effects in the current phase, as well as inputs (outputs from other phases
327 // that we depend on). Note that since we start iterating from the end,
328 // the effect list will be in the reverse order.
329 std::vector<Node *> this_phase_inputs;
330 std::vector<Node *> this_phase_effects;
332 // Effects that we have yet to calculate, but that we know should
333 // be in the current phase.
334 std::stack<Node *> effects_todo_this_phase;
336 // Effects that we have yet to calculate, but that come from other phases.
337 // We delay these until we have this phase done in its entirety,
338 // at which point we pick any of them and start a new phase from that.
339 std::stack<Node *> effects_todo_other_phases;
341 effects_todo_this_phase.push(output);
343 for ( ;; ) { // Termination condition within loop.
344 if (!effects_todo_this_phase.empty()) {
345 // OK, we have more to do this phase.
346 Node *node = effects_todo_this_phase.top();
347 effects_todo_this_phase.pop();
349 // This should currently only happen for effects that are inputs
350 // (either true inputs or phase outputs). We special-case inputs,
351 // and then deduplicate phase outputs in compile_glsl_program().
352 if (node->effect->num_inputs() == 0) {
353 if (find(this_phase_effects.begin(), this_phase_effects.end(), node) != this_phase_effects.end()) {
357 assert(completed_effects.count(node) == 0);
360 this_phase_effects.push_back(node);
361 completed_effects.insert(node);
363 // Find all the dependencies of this effect, and add them to the stack.
364 std::vector<Node *> deps = node->incoming_links;
365 assert(node->effect->num_inputs() == deps.size());
366 for (unsigned i = 0; i < deps.size(); ++i) {
367 bool start_new_phase = false;
369 // FIXME: If we sample directly from a texture, we won't need this.
370 if (node->effect->needs_texture_bounce()) {
371 start_new_phase = true;
374 if (deps[i]->outgoing_links.size() > 1) {
375 if (deps[i]->effect->num_inputs() > 0) {
376 // More than one effect uses this as the input,
377 // and it is not a texture itself.
378 // The easiest thing to do (and probably also the safest
379 // performance-wise in most cases) is to bounce it to a texture
380 // and then let the next passes read from that.
381 start_new_phase = true;
383 // For textures, we try to be slightly more clever;
384 // if none of our outputs need a bounce, we don't bounce
385 // but instead simply use the effect many times.
387 // Strictly speaking, we could bounce it for some outputs
388 // and use it directly for others, but the processing becomes
389 // somewhat simpler if the effect is only used in one such way.
390 for (unsigned j = 0; j < deps[i]->outgoing_links.size(); ++j) {
391 Node *rdep = deps[i]->outgoing_links[j];
392 start_new_phase |= rdep->effect->needs_texture_bounce();
397 if (deps[i]->effect->changes_output_size()) {
398 start_new_phase = true;
401 if (start_new_phase) {
402 effects_todo_other_phases.push(deps[i]);
403 this_phase_inputs.push_back(deps[i]);
405 effects_todo_this_phase.push(deps[i]);
411 // No more effects to do this phase. Take all the ones we have,
412 // and create a GLSL program for it.
413 if (!this_phase_effects.empty()) {
414 reverse(this_phase_effects.begin(), this_phase_effects.end());
415 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
416 this_phase_effects.back()->phase = phases.back();
417 this_phase_inputs.clear();
418 this_phase_effects.clear();
420 assert(this_phase_inputs.empty());
421 assert(this_phase_effects.empty());
423 // If we have no effects left, exit.
424 if (effects_todo_other_phases.empty()) {
428 Node *node = effects_todo_other_phases.top();
429 effects_todo_other_phases.pop();
431 if (completed_effects.count(node) == 0) {
432 // Start a new phase, calculating from this effect.
433 effects_todo_this_phase.push(node);
437 // Finally, since the phases are found from the output but must be executed
438 // from the input(s), reverse them, too.
439 std::reverse(phases.begin(), phases.end());
442 void EffectChain::output_dot(const char *filename)
444 if (movit_debug_level != MOVIT_DEBUG_ON) {
448 FILE *fp = fopen(filename, "w");
454 fprintf(fp, "digraph G {\n");
455 fprintf(fp, " output [shape=box label=\"(output)\"];\n");
456 for (unsigned i = 0; i < nodes.size(); ++i) {
457 // Find out which phase this event belongs to.
458 std::vector<int> in_phases;
459 for (unsigned j = 0; j < phases.size(); ++j) {
460 const Phase* p = phases[j];
461 if (std::find(p->effects.begin(), p->effects.end(), nodes[i]) != p->effects.end()) {
462 in_phases.push_back(j);
466 if (in_phases.empty()) {
467 fprintf(fp, " n%ld [label=\"%s\"];\n", (long)nodes[i], nodes[i]->effect->effect_type_id().c_str());
468 } else if (in_phases.size() == 1) {
469 fprintf(fp, " n%ld [label=\"%s\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
470 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
471 (in_phases[0] % 8) + 1);
473 // If we had new enough Graphviz, style="wedged" would probably be ideal here.
475 fprintf(fp, " n%ld [label=\"%s [in multiple phases]\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
476 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
477 (in_phases[0] % 8) + 1);
480 char from_node_id[256];
481 snprintf(from_node_id, 256, "n%ld", (long)nodes[i]);
483 for (unsigned j = 0; j < nodes[i]->outgoing_links.size(); ++j) {
484 char to_node_id[256];
485 snprintf(to_node_id, 256, "n%ld", (long)nodes[i]->outgoing_links[j]);
487 std::vector<std::string> labels = get_labels_for_edge(nodes[i], nodes[i]->outgoing_links[j]);
488 output_dot_edge(fp, from_node_id, to_node_id, labels);
491 if (nodes[i]->outgoing_links.empty() && !nodes[i]->disabled) {
493 std::vector<std::string> labels = get_labels_for_edge(nodes[i], NULL);
494 output_dot_edge(fp, from_node_id, "output", labels);
502 std::vector<std::string> EffectChain::get_labels_for_edge(const Node *from, const Node *to)
504 std::vector<std::string> labels;
506 if (to != NULL && to->effect->needs_texture_bounce()) {
507 labels.push_back("needs_bounce");
509 if (from->effect->changes_output_size()) {
510 labels.push_back("resize");
513 switch (from->output_color_space) {
514 case COLORSPACE_INVALID:
515 labels.push_back("spc[invalid]");
517 case COLORSPACE_REC_601_525:
518 labels.push_back("spc[rec601-525]");
520 case COLORSPACE_REC_601_625:
521 labels.push_back("spc[rec601-625]");
527 switch (from->output_gamma_curve) {
529 labels.push_back("gamma[invalid]");
532 labels.push_back("gamma[sRGB]");
534 case GAMMA_REC_601: // and GAMMA_REC_709
535 labels.push_back("gamma[rec601/709]");
541 switch (from->output_alpha_type) {
543 labels.push_back("alpha[invalid]");
546 labels.push_back("alpha[blank]");
548 case ALPHA_POSTMULTIPLIED:
549 labels.push_back("alpha[postmult]");
558 void EffectChain::output_dot_edge(FILE *fp,
559 const std::string &from_node_id,
560 const std::string &to_node_id,
561 const std::vector<std::string> &labels)
563 if (labels.empty()) {
564 fprintf(fp, " %s -> %s;\n", from_node_id.c_str(), to_node_id.c_str());
566 std::string label = labels[0];
567 for (unsigned k = 1; k < labels.size(); ++k) {
568 label += ", " + labels[k];
570 fprintf(fp, " %s -> %s [label=\"%s\"];\n", from_node_id.c_str(), to_node_id.c_str(), label.c_str());
574 void EffectChain::size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height)
576 unsigned scaled_width, scaled_height;
578 if (float(width) * aspect_denom >= float(height) * aspect_nom) {
579 // Same aspect, or W/H > aspect (image is wider than the frame).
580 // In either case, keep width, and adjust height.
581 scaled_width = width;
582 scaled_height = lrintf(width * aspect_denom / aspect_nom);
584 // W/H < aspect (image is taller than the frame), so keep height,
586 scaled_width = lrintf(height * aspect_nom / aspect_denom);
587 scaled_height = height;
590 // We should be consistently larger or smaller then the existing choice,
591 // since we have the same aspect.
592 assert(!(scaled_width < *output_width && scaled_height > *output_height));
593 assert(!(scaled_height < *output_height && scaled_width > *output_width));
595 if (scaled_width >= *output_width && scaled_height >= *output_height) {
596 *output_width = scaled_width;
597 *output_height = scaled_height;
601 // Propagate input texture sizes throughout, and inform effects downstream.
602 // (Like a lot of other code, we depend on effects being in topological order.)
603 void EffectChain::inform_input_sizes(Phase *phase)
605 // All effects that have a defined size (inputs and RTT inputs)
606 // get that. Reset all others.
607 for (unsigned i = 0; i < phase->effects.size(); ++i) {
608 Node *node = phase->effects[i];
609 if (node->effect->num_inputs() == 0) {
610 Input *input = static_cast<Input *>(node->effect);
611 node->output_width = input->get_width();
612 node->output_height = input->get_height();
613 assert(node->output_width != 0);
614 assert(node->output_height != 0);
616 node->output_width = node->output_height = 0;
619 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
620 Node *input = phase->inputs[i];
621 input->output_width = input->phase->virtual_output_width;
622 input->output_height = input->phase->virtual_output_height;
623 assert(input->output_width != 0);
624 assert(input->output_height != 0);
627 // Now propagate from the inputs towards the end, and inform as we go.
628 // The rules are simple:
630 // 1. Don't touch effects that already have given sizes (ie., inputs).
631 // 2. If all of your inputs have the same size, that will be your output size.
632 // 3. Otherwise, your output size is 0x0.
633 for (unsigned i = 0; i < phase->effects.size(); ++i) {
634 Node *node = phase->effects[i];
635 if (node->effect->num_inputs() == 0) {
638 unsigned this_output_width = 0;
639 unsigned this_output_height = 0;
640 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
641 Node *input = node->incoming_links[j];
642 node->effect->inform_input_size(j, input->output_width, input->output_height);
644 this_output_width = input->output_width;
645 this_output_height = input->output_height;
646 } else if (input->output_width != this_output_width || input->output_height != this_output_height) {
648 this_output_width = 0;
649 this_output_height = 0;
652 node->output_width = this_output_width;
653 node->output_height = this_output_height;
657 // Note: You should call inform_input_sizes() before this, as the last effect's
658 // desired output size might change based on the inputs.
659 void EffectChain::find_output_size(Phase *phase)
661 Node *output_node = phase->effects.back();
663 // If the last effect explicitly sets an output size, use that.
664 if (output_node->effect->changes_output_size()) {
665 output_node->effect->get_output_size(&phase->output_width, &phase->output_height,
666 &phase->virtual_output_width, &phase->virtual_output_height);
670 // If all effects have the same size, use that.
671 unsigned output_width = 0, output_height = 0;
672 bool all_inputs_same_size = true;
674 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
675 Node *input = phase->inputs[i];
676 assert(input->phase->output_width != 0);
677 assert(input->phase->output_height != 0);
678 if (output_width == 0 && output_height == 0) {
679 output_width = input->phase->virtual_output_width;
680 output_height = input->phase->virtual_output_height;
681 } else if (output_width != input->phase->virtual_output_width ||
682 output_height != input->phase->virtual_output_height) {
683 all_inputs_same_size = false;
686 for (unsigned i = 0; i < phase->effects.size(); ++i) {
687 Effect *effect = phase->effects[i]->effect;
688 if (effect->num_inputs() != 0) {
692 Input *input = static_cast<Input *>(effect);
693 if (output_width == 0 && output_height == 0) {
694 output_width = input->get_width();
695 output_height = input->get_height();
696 } else if (output_width != input->get_width() ||
697 output_height != input->get_height()) {
698 all_inputs_same_size = false;
702 if (all_inputs_same_size) {
703 assert(output_width != 0);
704 assert(output_height != 0);
705 phase->virtual_output_width = phase->output_width = output_width;
706 phase->virtual_output_height = phase->output_height = output_height;
710 // If not, fit all the inputs into the current aspect, and select the largest one.
713 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
714 Node *input = phase->inputs[i];
715 assert(input->phase->output_width != 0);
716 assert(input->phase->output_height != 0);
717 size_rectangle_to_fit(input->phase->output_width, input->phase->output_height, &output_width, &output_height);
719 for (unsigned i = 0; i < phase->effects.size(); ++i) {
720 Effect *effect = phase->effects[i]->effect;
721 if (effect->num_inputs() != 0) {
725 Input *input = static_cast<Input *>(effect);
726 size_rectangle_to_fit(input->get_width(), input->get_height(), &output_width, &output_height);
728 assert(output_width != 0);
729 assert(output_height != 0);
730 phase->virtual_output_width = phase->output_width = output_width;
731 phase->virtual_output_height = phase->output_height = output_height;
734 void EffectChain::sort_all_nodes_topologically()
736 nodes = topological_sort(nodes);
739 std::vector<Node *> EffectChain::topological_sort(const std::vector<Node *> &nodes)
741 std::set<Node *> nodes_left_to_visit(nodes.begin(), nodes.end());
742 std::vector<Node *> sorted_list;
743 for (unsigned i = 0; i < nodes.size(); ++i) {
744 topological_sort_visit_node(nodes[i], &nodes_left_to_visit, &sorted_list);
746 reverse(sorted_list.begin(), sorted_list.end());
750 void EffectChain::topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list)
752 if (nodes_left_to_visit->count(node) == 0) {
755 nodes_left_to_visit->erase(node);
756 for (unsigned i = 0; i < node->outgoing_links.size(); ++i) {
757 topological_sort_visit_node(node->outgoing_links[i], nodes_left_to_visit, sorted_list);
759 sorted_list->push_back(node);
762 void EffectChain::find_color_spaces_for_inputs()
764 for (unsigned i = 0; i < nodes.size(); ++i) {
765 Node *node = nodes[i];
766 if (node->disabled) {
769 if (node->incoming_links.size() == 0) {
770 Input *input = static_cast<Input *>(node->effect);
771 node->output_color_space = input->get_color_space();
772 node->output_gamma_curve = input->get_gamma_curve();
774 Effect::AlphaHandling alpha_handling = input->alpha_handling();
775 switch (alpha_handling) {
776 case Effect::OUTPUT_BLANK_ALPHA:
777 node->output_alpha_type = ALPHA_BLANK;
779 case Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA:
780 node->output_alpha_type = ALPHA_PREMULTIPLIED;
782 case Effect::OUTPUT_POSTMULTIPLIED_ALPHA:
783 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
785 case Effect::DONT_CARE_ALPHA_TYPE:
790 if (node->output_alpha_type == ALPHA_PREMULTIPLIED) {
791 assert(node->output_gamma_curve == GAMMA_LINEAR);
797 // Propagate gamma and color space information as far as we can in the graph.
798 // The rules are simple: Anything where all the inputs agree, get that as
799 // output as well. Anything else keeps having *_INVALID.
800 void EffectChain::propagate_gamma_and_color_space()
802 // We depend on going through the nodes in order.
803 sort_all_nodes_topologically();
805 for (unsigned i = 0; i < nodes.size(); ++i) {
806 Node *node = nodes[i];
807 if (node->disabled) {
810 assert(node->incoming_links.size() == node->effect->num_inputs());
811 if (node->incoming_links.size() == 0) {
812 assert(node->output_color_space != COLORSPACE_INVALID);
813 assert(node->output_gamma_curve != GAMMA_INVALID);
817 Colorspace color_space = node->incoming_links[0]->output_color_space;
818 GammaCurve gamma_curve = node->incoming_links[0]->output_gamma_curve;
819 for (unsigned j = 1; j < node->incoming_links.size(); ++j) {
820 if (node->incoming_links[j]->output_color_space != color_space) {
821 color_space = COLORSPACE_INVALID;
823 if (node->incoming_links[j]->output_gamma_curve != gamma_curve) {
824 gamma_curve = GAMMA_INVALID;
828 // The conversion effects already have their outputs set correctly,
829 // so leave them alone.
830 if (node->effect->effect_type_id() != "ColorspaceConversionEffect") {
831 node->output_color_space = color_space;
833 if (node->effect->effect_type_id() != "GammaCompressionEffect" &&
834 node->effect->effect_type_id() != "GammaExpansionEffect") {
835 node->output_gamma_curve = gamma_curve;
840 // Propagate alpha information as far as we can in the graph.
841 // Similar to propagate_gamma_and_color_space().
842 void EffectChain::propagate_alpha()
844 // We depend on going through the nodes in order.
845 sort_all_nodes_topologically();
847 for (unsigned i = 0; i < nodes.size(); ++i) {
848 Node *node = nodes[i];
849 if (node->disabled) {
852 assert(node->incoming_links.size() == node->effect->num_inputs());
853 if (node->incoming_links.size() == 0) {
854 assert(node->output_alpha_type != ALPHA_INVALID);
858 // The alpha multiplication/division effects are special cases.
859 if (node->effect->effect_type_id() == "AlphaMultiplicationEffect") {
860 assert(node->incoming_links.size() == 1);
861 assert(node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED);
862 node->output_alpha_type = ALPHA_PREMULTIPLIED;
865 if (node->effect->effect_type_id() == "AlphaDivisionEffect") {
866 assert(node->incoming_links.size() == 1);
867 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
868 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
872 // GammaCompressionEffect and GammaExpansionEffect are also a special case,
873 // because they are the only one that _need_ postmultiplied alpha.
874 if (node->effect->effect_type_id() == "GammaCompressionEffect" ||
875 node->effect->effect_type_id() == "GammaExpansionEffect") {
876 assert(node->incoming_links.size() == 1);
877 if (node->incoming_links[0]->output_alpha_type == ALPHA_BLANK) {
878 node->output_alpha_type = ALPHA_BLANK;
879 } else if (node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED) {
880 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
882 node->output_alpha_type = ALPHA_INVALID;
887 // Only inputs can have unconditional alpha output (OUTPUT_BLANK_ALPHA
888 // or OUTPUT_POSTMULTIPLIED_ALPHA), and they have already been
889 // taken care of above. Rationale: Even if you could imagine
890 // e.g. an effect that took in an image and set alpha=1.0
891 // unconditionally, it wouldn't make any sense to have it as
892 // e.g. OUTPUT_BLANK_ALPHA, since it wouldn't know whether it
893 // got its input pre- or postmultiplied, so it wouldn't know
894 // whether to divide away the old alpha or not.
895 Effect::AlphaHandling alpha_handling = node->effect->alpha_handling();
896 assert(alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
897 alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
899 // If the node has multiple inputs, check that they are all valid and
901 bool any_invalid = false;
902 bool any_premultiplied = false;
903 bool any_postmultiplied = false;
905 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
906 switch (node->incoming_links[j]->output_alpha_type) {
911 // Blank is good as both pre- and postmultiplied alpha,
912 // so just ignore it.
914 case ALPHA_PREMULTIPLIED:
915 any_premultiplied = true;
917 case ALPHA_POSTMULTIPLIED:
918 any_postmultiplied = true;
926 node->output_alpha_type = ALPHA_INVALID;
930 // Inputs must be of the same type.
931 if (any_premultiplied && any_postmultiplied) {
932 node->output_alpha_type = ALPHA_INVALID;
936 if (alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA) {
937 // If the effect has asked for premultiplied alpha, check that it has got it.
938 if (any_postmultiplied) {
939 node->output_alpha_type = ALPHA_INVALID;
941 // In some rare cases, it might be advantageous to say
942 // that blank input alpha yields blank output alpha.
943 // However, this would cause a more complex Effect interface
944 // an effect would need to guarantee that it doesn't mess with
945 // blank alpha), so this is the simplest.
946 node->output_alpha_type = ALPHA_PREMULTIPLIED;
949 // OK, all inputs are the same, and this effect is not going
951 assert(alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
952 if (any_premultiplied) {
953 node->output_alpha_type = ALPHA_PREMULTIPLIED;
954 } else if (any_postmultiplied) {
955 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
957 node->output_alpha_type = ALPHA_BLANK;
963 bool EffectChain::node_needs_colorspace_fix(Node *node)
965 if (node->disabled) {
968 if (node->effect->num_inputs() == 0) {
972 // propagate_gamma_and_color_space() has already set our output
973 // to COLORSPACE_INVALID if the inputs differ, so we can rely on that.
974 if (node->output_color_space == COLORSPACE_INVALID) {
977 return (node->effect->needs_srgb_primaries() && node->output_color_space != COLORSPACE_sRGB);
980 // Fix up color spaces so that there are no COLORSPACE_INVALID nodes left in
981 // the graph. Our strategy is not always optimal, but quite simple:
982 // Find an effect that's as early as possible where the inputs are of
983 // unacceptable colorspaces (that is, either different, or, if the effect only
984 // wants sRGB, not sRGB.) Add appropriate conversions on all its inputs,
985 // propagate the information anew, and repeat until there are no more such
987 void EffectChain::fix_internal_color_spaces()
989 unsigned colorspace_propagation_pass = 0;
993 for (unsigned i = 0; i < nodes.size(); ++i) {
994 Node *node = nodes[i];
995 if (!node_needs_colorspace_fix(node)) {
999 // Go through each input that is not sRGB, and insert
1000 // a colorspace conversion after it.
1001 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1002 Node *input = node->incoming_links[j];
1003 assert(input->output_color_space != COLORSPACE_INVALID);
1004 if (input->output_color_space == COLORSPACE_sRGB) {
1007 Node *conversion = add_node(new ColorspaceConversionEffect());
1008 CHECK(conversion->effect->set_int("source_space", input->output_color_space));
1009 CHECK(conversion->effect->set_int("destination_space", COLORSPACE_sRGB));
1010 conversion->output_color_space = COLORSPACE_sRGB;
1011 replace_sender(input, conversion);
1012 connect_nodes(input, conversion);
1015 // Re-sort topologically, and propagate the new information.
1016 propagate_gamma_and_color_space();
1023 sprintf(filename, "step5-colorspacefix-iter%u.dot", ++colorspace_propagation_pass);
1024 output_dot(filename);
1025 assert(colorspace_propagation_pass < 100);
1026 } while (found_any);
1028 for (unsigned i = 0; i < nodes.size(); ++i) {
1029 Node *node = nodes[i];
1030 if (node->disabled) {
1033 assert(node->output_color_space != COLORSPACE_INVALID);
1037 bool EffectChain::node_needs_alpha_fix(Node *node)
1039 if (node->disabled) {
1043 // propagate_alpha() has already set our output to ALPHA_INVALID if the
1044 // inputs differ or we are otherwise in mismatch, so we can rely on that.
1045 return (node->output_alpha_type == ALPHA_INVALID);
1048 // Fix up alpha so that there are no ALPHA_INVALID nodes left in
1049 // the graph. Similar to fix_internal_color_spaces().
1050 void EffectChain::fix_internal_alpha(unsigned step)
1052 unsigned alpha_propagation_pass = 0;
1056 for (unsigned i = 0; i < nodes.size(); ++i) {
1057 Node *node = nodes[i];
1058 if (!node_needs_alpha_fix(node)) {
1062 // If we need to fix up GammaExpansionEffect, then clearly something
1063 // is wrong, since the combination of premultiplied alpha and nonlinear inputs
1065 assert(node->effect->effect_type_id() != "GammaExpansionEffect");
1067 AlphaType desired_type = ALPHA_PREMULTIPLIED;
1069 // GammaCompressionEffect is special; it needs postmultiplied alpha.
1070 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1071 assert(node->incoming_links.size() == 1);
1072 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
1073 desired_type = ALPHA_POSTMULTIPLIED;
1076 // Go through each input that is not premultiplied alpha, and insert
1077 // a conversion before it.
1078 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1079 Node *input = node->incoming_links[j];
1080 assert(input->output_alpha_type != ALPHA_INVALID);
1081 if (input->output_alpha_type == desired_type ||
1082 input->output_alpha_type == ALPHA_BLANK) {
1086 if (desired_type == ALPHA_PREMULTIPLIED) {
1087 conversion = add_node(new AlphaMultiplicationEffect());
1089 conversion = add_node(new AlphaDivisionEffect());
1091 conversion->output_alpha_type = desired_type;
1092 replace_sender(input, conversion);
1093 connect_nodes(input, conversion);
1096 // Re-sort topologically, and propagate the new information.
1097 propagate_gamma_and_color_space();
1105 sprintf(filename, "step%u-alphafix-iter%u.dot", step, ++alpha_propagation_pass);
1106 output_dot(filename);
1107 assert(alpha_propagation_pass < 100);
1108 } while (found_any);
1110 for (unsigned i = 0; i < nodes.size(); ++i) {
1111 Node *node = nodes[i];
1112 if (node->disabled) {
1115 assert(node->output_alpha_type != ALPHA_INVALID);
1119 // Make so that the output is in the desired color space.
1120 void EffectChain::fix_output_color_space()
1122 Node *output = find_output_node();
1123 if (output->output_color_space != output_format.color_space) {
1124 Node *conversion = add_node(new ColorspaceConversionEffect());
1125 CHECK(conversion->effect->set_int("source_space", output->output_color_space));
1126 CHECK(conversion->effect->set_int("destination_space", output_format.color_space));
1127 conversion->output_color_space = output_format.color_space;
1128 connect_nodes(output, conversion);
1130 propagate_gamma_and_color_space();
1134 // Make so that the output is in the desired pre-/postmultiplication alpha state.
1135 void EffectChain::fix_output_alpha()
1137 Node *output = find_output_node();
1138 assert(output->output_alpha_type != ALPHA_INVALID);
1139 if (output->output_alpha_type == ALPHA_BLANK) {
1140 // No alpha output, so we don't care.
1143 if (output->output_alpha_type == ALPHA_PREMULTIPLIED &&
1144 output_alpha_format == OUTPUT_POSTMULTIPLIED_ALPHA) {
1145 Node *conversion = add_node(new AlphaDivisionEffect());
1146 connect_nodes(output, conversion);
1148 propagate_gamma_and_color_space();
1150 if (output->output_alpha_type == ALPHA_POSTMULTIPLIED &&
1151 output_alpha_format == OUTPUT_ALPHA_PREMULTIPLIED) {
1152 Node *conversion = add_node(new AlphaMultiplicationEffect());
1153 connect_nodes(output, conversion);
1155 propagate_gamma_and_color_space();
1159 bool EffectChain::node_needs_gamma_fix(Node *node)
1161 if (node->disabled) {
1165 // Small hack since the output is not an explicit node:
1166 // If we are the last node and our output is in the wrong
1167 // space compared to EffectChain's output, we need to fix it.
1168 // This will only take us to linear, but fix_output_gamma()
1169 // will come and take us to the desired output gamma
1172 // This needs to be before everything else, since it could
1173 // even apply to inputs (if they are the only effect).
1174 if (node->outgoing_links.empty() &&
1175 node->output_gamma_curve != output_format.gamma_curve &&
1176 node->output_gamma_curve != GAMMA_LINEAR) {
1180 if (node->effect->num_inputs() == 0) {
1184 // propagate_gamma_and_color_space() has already set our output
1185 // to GAMMA_INVALID if the inputs differ, so we can rely on that,
1186 // except for GammaCompressionEffect.
1187 if (node->output_gamma_curve == GAMMA_INVALID) {
1190 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1191 assert(node->incoming_links.size() == 1);
1192 return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
1195 return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
1198 // Very similar to fix_internal_color_spaces(), but for gamma.
1199 // There is one difference, though; before we start adding conversion nodes,
1200 // we see if we can get anything out of asking the sources to deliver
1201 // linear gamma directly. fix_internal_gamma_by_asking_inputs()
1202 // does that part, while fix_internal_gamma_by_inserting_nodes()
1203 // inserts nodes as needed afterwards.
1204 void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
1206 unsigned gamma_propagation_pass = 0;
1210 for (unsigned i = 0; i < nodes.size(); ++i) {
1211 Node *node = nodes[i];
1212 if (!node_needs_gamma_fix(node)) {
1216 // See if all inputs can give us linear gamma. If not, leave it.
1217 std::vector<Node *> nonlinear_inputs;
1218 find_all_nonlinear_inputs(node, &nonlinear_inputs);
1219 assert(!nonlinear_inputs.empty());
1222 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1223 Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
1224 all_ok &= input->can_output_linear_gamma();
1231 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1232 CHECK(nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1));
1233 nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
1236 // Re-sort topologically, and propagate the new information.
1237 propagate_gamma_and_color_space();
1244 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1245 output_dot(filename);
1246 assert(gamma_propagation_pass < 100);
1247 } while (found_any);
1250 void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
1252 unsigned gamma_propagation_pass = 0;
1256 for (unsigned i = 0; i < nodes.size(); ++i) {
1257 Node *node = nodes[i];
1258 if (!node_needs_gamma_fix(node)) {
1262 // Special case: We could be an input and still be asked to
1263 // fix our gamma; if so, we should be the only node
1264 // (as node_needs_gamma_fix() would only return true in
1265 // for an input in that case). That means we should insert
1266 // a conversion node _after_ ourselves.
1267 if (node->incoming_links.empty()) {
1268 assert(node->outgoing_links.empty());
1269 Node *conversion = add_node(new GammaExpansionEffect());
1270 CHECK(conversion->effect->set_int("source_curve", node->output_gamma_curve));
1271 conversion->output_gamma_curve = GAMMA_LINEAR;
1272 connect_nodes(node, conversion);
1275 // If not, go through each input that is not linear gamma,
1276 // and insert a gamma conversion after it.
1277 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1278 Node *input = node->incoming_links[j];
1279 assert(input->output_gamma_curve != GAMMA_INVALID);
1280 if (input->output_gamma_curve == GAMMA_LINEAR) {
1283 Node *conversion = add_node(new GammaExpansionEffect());
1284 CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
1285 conversion->output_gamma_curve = GAMMA_LINEAR;
1286 replace_sender(input, conversion);
1287 connect_nodes(input, conversion);
1290 // Re-sort topologically, and propagate the new information.
1292 propagate_gamma_and_color_space();
1299 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1300 output_dot(filename);
1301 assert(gamma_propagation_pass < 100);
1302 } while (found_any);
1304 for (unsigned i = 0; i < nodes.size(); ++i) {
1305 Node *node = nodes[i];
1306 if (node->disabled) {
1309 assert(node->output_gamma_curve != GAMMA_INVALID);
1313 // Make so that the output is in the desired gamma.
1314 // Note that this assumes linear input gamma, so it might create the need
1315 // for another pass of fix_internal_gamma().
1316 void EffectChain::fix_output_gamma()
1318 Node *output = find_output_node();
1319 if (output->output_gamma_curve != output_format.gamma_curve) {
1320 Node *conversion = add_node(new GammaCompressionEffect());
1321 CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
1322 conversion->output_gamma_curve = output_format.gamma_curve;
1323 connect_nodes(output, conversion);
1327 // If the user has requested dither, add a DitherEffect right at the end
1328 // (after GammaCompressionEffect etc.). This needs to be done after everything else,
1329 // since dither is about the only effect that can _not_ be done in linear space.
1330 void EffectChain::add_dither_if_needed()
1332 if (num_dither_bits == 0) {
1335 Node *output = find_output_node();
1336 Node *dither = add_node(new DitherEffect());
1337 CHECK(dither->effect->set_int("num_bits", num_dither_bits));
1338 connect_nodes(output, dither);
1340 dither_effect = dither->effect;
1343 // Find the output node. This is, simply, one that has no outgoing links.
1344 // If there are multiple ones, the graph is malformed (we do not support
1345 // multiple outputs right now).
1346 Node *EffectChain::find_output_node()
1348 std::vector<Node *> output_nodes;
1349 for (unsigned i = 0; i < nodes.size(); ++i) {
1350 Node *node = nodes[i];
1351 if (node->disabled) {
1354 if (node->outgoing_links.empty()) {
1355 output_nodes.push_back(node);
1358 assert(output_nodes.size() == 1);
1359 return output_nodes[0];
1362 void EffectChain::finalize()
1364 // Save the current locale, and set it to C, so that we can output decimal
1365 // numbers with printf and be sure to get them in the format mandated by GLSL.
1366 char *saved_locale = setlocale(LC_NUMERIC, "C");
1368 // Output the graph as it is before we do any conversions on it.
1369 output_dot("step0-start.dot");
1371 // Give each effect in turn a chance to rewrite its own part of the graph.
1372 // Note that if more effects are added as part of this, they will be
1373 // picked up as part of the same for loop, since they are added at the end.
1374 for (unsigned i = 0; i < nodes.size(); ++i) {
1375 nodes[i]->effect->rewrite_graph(this, nodes[i]);
1377 output_dot("step1-rewritten.dot");
1379 find_color_spaces_for_inputs();
1380 output_dot("step2-input-colorspace.dot");
1383 output_dot("step3-propagated-alpha.dot");
1385 propagate_gamma_and_color_space();
1386 output_dot("step4-propagated-all.dot");
1388 fix_internal_color_spaces();
1389 fix_internal_alpha(6);
1390 fix_output_color_space();
1391 output_dot("step7-output-colorspacefix.dot");
1393 output_dot("step8-output-alphafix.dot");
1395 // Note that we need to fix gamma after colorspace conversion,
1396 // because colorspace conversions might create needs for gamma conversions.
1397 // Also, we need to run an extra pass of fix_internal_gamma() after
1398 // fixing the output gamma, as we only have conversions to/from linear,
1399 // and fix_internal_alpha() since GammaCompressionEffect needs
1400 // postmultiplied input.
1401 fix_internal_gamma_by_asking_inputs(9);
1402 fix_internal_gamma_by_inserting_nodes(10);
1404 output_dot("step11-output-gammafix.dot");
1406 output_dot("step12-output-alpha-propagated.dot");
1407 fix_internal_alpha(13);
1408 output_dot("step14-output-alpha-fixed.dot");
1409 fix_internal_gamma_by_asking_inputs(15);
1410 fix_internal_gamma_by_inserting_nodes(16);
1412 output_dot("step17-before-dither.dot");
1414 add_dither_if_needed();
1416 output_dot("step18-final.dot");
1418 // Construct all needed GLSL programs, starting at the output.
1419 construct_glsl_programs(find_output_node());
1421 output_dot("step19-split-to-phases.dot");
1423 // If we have more than one phase, we need intermediate render-to-texture.
1424 // Construct an FBO, and then as many textures as we need.
1425 // We choose the simplest option of having one texture per output,
1426 // since otherwise this turns into an (albeit simple)
1427 // register allocation problem.
1428 if (phases.size() > 1) {
1429 glGenFramebuffers(1, &fbo);
1431 for (unsigned i = 0; i < phases.size() - 1; ++i) {
1432 inform_input_sizes(phases[i]);
1433 find_output_size(phases[i]);
1435 Node *output_node = phases[i]->effects.back();
1436 glGenTextures(1, &output_node->output_texture);
1438 glBindTexture(GL_TEXTURE_2D, output_node->output_texture);
1440 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1442 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1444 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[i]->output_width, phases[i]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1447 output_node->output_texture_width = phases[i]->output_width;
1448 output_node->output_texture_height = phases[i]->output_height;
1450 inform_input_sizes(phases.back());
1453 for (unsigned i = 0; i < inputs.size(); ++i) {
1454 inputs[i]->finalize();
1457 assert(phases[0]->inputs.empty());
1460 setlocale(LC_NUMERIC, saved_locale);
1463 void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height)
1467 // Save original viewport.
1468 GLuint x = 0, y = 0;
1470 if (width == 0 && height == 0) {
1472 glGetIntegerv(GL_VIEWPORT, viewport);
1475 width = viewport[2];
1476 height = viewport[3];
1480 glDisable(GL_BLEND);
1482 glDisable(GL_DEPTH_TEST);
1484 glDepthMask(GL_FALSE);
1487 glMatrixMode(GL_PROJECTION);
1489 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
1491 glMatrixMode(GL_MODELVIEW);
1494 if (phases.size() > 1) {
1495 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1499 std::set<Node *> generated_mipmaps;
1501 for (unsigned phase = 0; phase < phases.size(); ++phase) {
1502 // See if the requested output size has changed. If so, we need to recreate
1503 // the texture (and before we start setting up inputs).
1504 inform_input_sizes(phases[phase]);
1505 if (phase != phases.size() - 1) {
1506 find_output_size(phases[phase]);
1508 Node *output_node = phases[phase]->effects.back();
1510 if (output_node->output_texture_width != phases[phase]->output_width ||
1511 output_node->output_texture_height != phases[phase]->output_height) {
1512 glActiveTexture(GL_TEXTURE0);
1514 glBindTexture(GL_TEXTURE_2D, output_node->output_texture);
1516 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1518 glBindTexture(GL_TEXTURE_2D, 0);
1521 output_node->output_texture_width = phases[phase]->output_width;
1522 output_node->output_texture_height = phases[phase]->output_height;
1526 glUseProgram(phases[phase]->glsl_program_num);
1529 // Set up RTT inputs for this phase.
1530 for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
1531 glActiveTexture(GL_TEXTURE0 + sampler);
1532 Node *input = phases[phase]->inputs[sampler];
1533 glBindTexture(GL_TEXTURE_2D, input->output_texture);
1535 if (phases[phase]->input_needs_mipmaps) {
1536 if (generated_mipmaps.count(input) == 0) {
1537 glGenerateMipmap(GL_TEXTURE_2D);
1539 generated_mipmaps.insert(input);
1541 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1548 std::string texture_name = std::string("tex_") + input->effect_id;
1549 glUniform1i(glGetUniformLocation(phases[phase]->glsl_program_num, texture_name.c_str()), sampler);
1553 // And now the output.
1554 if (phase == phases.size() - 1) {
1555 // Last phase goes to the output the user specified.
1556 glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
1558 glViewport(x, y, width, height);
1559 if (dither_effect != NULL) {
1560 CHECK(dither_effect->set_int("output_width", width));
1561 CHECK(dither_effect->set_int("output_height", height));
1564 Node *output_node = phases[phase]->effects.back();
1565 glFramebufferTexture2D(
1567 GL_COLOR_ATTACHMENT0,
1569 output_node->output_texture,
1572 glViewport(0, 0, phases[phase]->output_width, phases[phase]->output_height);
1575 // Give the required parameters to all the effects.
1576 unsigned sampler_num = phases[phase]->inputs.size();
1577 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1578 Node *node = phases[phase]->effects[i];
1579 node->effect->set_gl_state(phases[phase]->glsl_program_num, node->effect_id, &sampler_num);
1586 glTexCoord2f(0.0f, 0.0f);
1587 glVertex2f(0.0f, 0.0f);
1589 glTexCoord2f(1.0f, 0.0f);
1590 glVertex2f(1.0f, 0.0f);
1592 glTexCoord2f(1.0f, 1.0f);
1593 glVertex2f(1.0f, 1.0f);
1595 glTexCoord2f(0.0f, 1.0f);
1596 glVertex2f(0.0f, 1.0f);
1601 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1602 Node *node = phases[phase]->effects[i];
1603 node->effect->clear_gl_state();