1 #define GL_GLEXT_PROTOTYPES 1
17 #include "alpha_division_effect.h"
18 #include "alpha_multiplication_effect.h"
19 #include "colorspace_conversion_effect.h"
20 #include "dither_effect.h"
22 #include "effect_chain.h"
23 #include "gamma_compression_effect.h"
24 #include "gamma_expansion_effect.h"
27 #include "resource_pool.h"
34 EffectChain::EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool)
35 : aspect_nom(aspect_nom),
36 aspect_denom(aspect_denom),
40 resource_pool(resource_pool) {
41 if (resource_pool == NULL) {
42 this->resource_pool = new ResourcePool();
43 owns_resource_pool = true;
45 owns_resource_pool = false;
49 EffectChain::~EffectChain()
51 for (unsigned i = 0; i < nodes.size(); ++i) {
52 delete nodes[i]->effect;
55 for (unsigned i = 0; i < phases.size(); ++i) {
56 resource_pool->release_glsl_program(phases[i]->glsl_program_num);
59 if (owns_resource_pool) {
64 Input *EffectChain::add_input(Input *input)
67 inputs.push_back(input);
72 void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
75 output_format = format;
76 output_alpha_format = alpha_format;
79 Node *EffectChain::add_node(Effect *effect)
81 for (unsigned i = 0; i < nodes.size(); ++i) {
82 assert(nodes[i]->effect != effect);
85 Node *node = new Node;
86 node->effect = effect;
87 node->disabled = false;
88 node->output_color_space = COLORSPACE_INVALID;
89 node->output_gamma_curve = GAMMA_INVALID;
90 node->output_alpha_type = ALPHA_INVALID;
92 nodes.push_back(node);
93 node_map[effect] = node;
94 effect->inform_added(this);
98 void EffectChain::connect_nodes(Node *sender, Node *receiver)
100 sender->outgoing_links.push_back(receiver);
101 receiver->incoming_links.push_back(sender);
104 void EffectChain::replace_receiver(Node *old_receiver, Node *new_receiver)
106 new_receiver->incoming_links = old_receiver->incoming_links;
107 old_receiver->incoming_links.clear();
109 for (unsigned i = 0; i < new_receiver->incoming_links.size(); ++i) {
110 Node *sender = new_receiver->incoming_links[i];
111 for (unsigned j = 0; j < sender->outgoing_links.size(); ++j) {
112 if (sender->outgoing_links[j] == old_receiver) {
113 sender->outgoing_links[j] = new_receiver;
119 void EffectChain::replace_sender(Node *old_sender, Node *new_sender)
121 new_sender->outgoing_links = old_sender->outgoing_links;
122 old_sender->outgoing_links.clear();
124 for (unsigned i = 0; i < new_sender->outgoing_links.size(); ++i) {
125 Node *receiver = new_sender->outgoing_links[i];
126 for (unsigned j = 0; j < receiver->incoming_links.size(); ++j) {
127 if (receiver->incoming_links[j] == old_sender) {
128 receiver->incoming_links[j] = new_sender;
134 void EffectChain::insert_node_between(Node *sender, Node *middle, Node *receiver)
136 for (unsigned i = 0; i < sender->outgoing_links.size(); ++i) {
137 if (sender->outgoing_links[i] == receiver) {
138 sender->outgoing_links[i] = middle;
139 middle->incoming_links.push_back(sender);
142 for (unsigned i = 0; i < receiver->incoming_links.size(); ++i) {
143 if (receiver->incoming_links[i] == sender) {
144 receiver->incoming_links[i] = middle;
145 middle->outgoing_links.push_back(receiver);
149 assert(middle->incoming_links.size() == middle->effect->num_inputs());
152 void EffectChain::find_all_nonlinear_inputs(Node *node, vector<Node *> *nonlinear_inputs)
154 if (node->output_gamma_curve == GAMMA_LINEAR &&
155 node->effect->effect_type_id() != "GammaCompressionEffect") {
158 if (node->effect->num_inputs() == 0) {
159 nonlinear_inputs->push_back(node);
161 assert(node->effect->num_inputs() == node->incoming_links.size());
162 for (unsigned i = 0; i < node->incoming_links.size(); ++i) {
163 find_all_nonlinear_inputs(node->incoming_links[i], nonlinear_inputs);
168 Effect *EffectChain::add_effect(Effect *effect, const vector<Effect *> &inputs)
171 assert(inputs.size() == effect->num_inputs());
172 Node *node = add_node(effect);
173 for (unsigned i = 0; i < inputs.size(); ++i) {
174 assert(node_map.count(inputs[i]) != 0);
175 connect_nodes(node_map[inputs[i]], node);
180 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
181 string replace_prefix(const string &text, const string &prefix)
186 while (start < text.size()) {
187 size_t pos = text.find("PREFIX(", start);
188 if (pos == string::npos) {
189 output.append(text.substr(start, string::npos));
193 output.append(text.substr(start, pos - start));
194 output.append(prefix);
197 pos += strlen("PREFIX(");
199 // Output stuff until we find the matching ), which we then eat.
201 size_t end_arg_pos = pos;
202 while (end_arg_pos < text.size()) {
203 if (text[end_arg_pos] == '(') {
205 } else if (text[end_arg_pos] == ')') {
213 output.append(text.substr(pos, end_arg_pos - pos));
221 Phase *EffectChain::compile_glsl_program(
222 const vector<Node *> &inputs,
223 const vector<Node *> &effects)
225 Phase *phase = new Phase;
226 assert(!effects.empty());
228 // Deduplicate the inputs.
229 vector<Node *> true_inputs = inputs;
230 sort(true_inputs.begin(), true_inputs.end());
231 true_inputs.erase(unique(true_inputs.begin(), true_inputs.end()), true_inputs.end());
233 bool input_needs_mipmaps = false;
234 string frag_shader = read_file("header.frag");
236 // Create functions for all the texture inputs that we need.
237 for (unsigned i = 0; i < true_inputs.size(); ++i) {
238 Node *input = true_inputs[i];
240 sprintf(effect_id, "in%u", i);
241 phase->effect_ids.insert(make_pair(input, effect_id));
243 frag_shader += string("uniform sampler2D tex_") + effect_id + ";\n";
244 frag_shader += string("vec4 ") + effect_id + "(vec2 tc) {\n";
245 frag_shader += "\treturn texture2D(tex_" + string(effect_id) + ", tc);\n";
246 frag_shader += "}\n";
250 vector<Node *> sorted_effects = topological_sort(effects);
252 for (unsigned i = 0; i < sorted_effects.size(); ++i) {
253 Node *node = sorted_effects[i];
255 sprintf(effect_id, "eff%u", i);
256 phase->effect_ids.insert(make_pair(node, effect_id));
258 if (node->incoming_links.size() == 1) {
259 frag_shader += string("#define INPUT ") + phase->effect_ids[node->incoming_links[0]] + "\n";
261 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
263 sprintf(buf, "#define INPUT%d %s\n", j + 1, phase->effect_ids[node->incoming_links[j]].c_str());
269 frag_shader += string("#define FUNCNAME ") + effect_id + "\n";
270 frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), effect_id);
271 frag_shader += replace_prefix(node->effect->output_fragment_shader(), effect_id);
272 frag_shader += "#undef PREFIX\n";
273 frag_shader += "#undef FUNCNAME\n";
274 if (node->incoming_links.size() == 1) {
275 frag_shader += "#undef INPUT\n";
277 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
279 sprintf(buf, "#undef INPUT%d\n", j + 1);
285 input_needs_mipmaps |= node->effect->needs_mipmaps();
287 for (unsigned i = 0; i < sorted_effects.size(); ++i) {
288 Node *node = sorted_effects[i];
289 if (node->effect->num_inputs() == 0) {
290 CHECK(node->effect->set_int("needs_mipmaps", input_needs_mipmaps));
293 frag_shader += string("#define INPUT ") + phase->effect_ids[sorted_effects.back()] + "\n";
294 frag_shader.append(read_file("footer.frag"));
296 phase->glsl_program_num = resource_pool->compile_glsl_program(read_file("vs.vert"), frag_shader);
297 phase->input_needs_mipmaps = input_needs_mipmaps;
298 phase->inputs = true_inputs;
299 phase->effects = sorted_effects;
304 // Construct GLSL programs, starting at the given effect and following
305 // the chain from there. We end a program every time we come to an effect
306 // marked as "needs texture bounce", one that is used by multiple other
307 // effects, every time an effect wants to change the output size,
308 // and of course at the end.
310 // We follow a quite simple depth-first search from the output, although
311 // without any explicit recursion.
312 void EffectChain::construct_glsl_programs(Node *output)
314 // Which effects have already been completed?
315 // We need to keep track of it, as an effect with multiple outputs
316 // could otherwise be calculated multiple times.
317 set<Node *> completed_effects;
319 // Effects in the current phase, as well as inputs (outputs from other phases
320 // that we depend on). Note that since we start iterating from the end,
321 // the effect list will be in the reverse order.
322 vector<Node *> this_phase_inputs;
323 vector<Node *> this_phase_effects;
325 // Effects that we have yet to calculate, but that we know should
326 // be in the current phase.
327 stack<Node *> effects_todo_this_phase;
329 // Effects that we have yet to calculate, but that come from other phases.
330 // We delay these until we have this phase done in its entirety,
331 // at which point we pick any of them and start a new phase from that.
332 stack<Node *> effects_todo_other_phases;
334 effects_todo_this_phase.push(output);
336 for ( ;; ) { // Termination condition within loop.
337 if (!effects_todo_this_phase.empty()) {
338 // OK, we have more to do this phase.
339 Node *node = effects_todo_this_phase.top();
340 effects_todo_this_phase.pop();
342 // This should currently only happen for effects that are inputs
343 // (either true inputs or phase outputs). We special-case inputs,
344 // and then deduplicate phase outputs in compile_glsl_program().
345 if (node->effect->num_inputs() == 0) {
346 if (find(this_phase_effects.begin(), this_phase_effects.end(), node) != this_phase_effects.end()) {
350 assert(completed_effects.count(node) == 0);
353 this_phase_effects.push_back(node);
354 completed_effects.insert(node);
356 // Find all the dependencies of this effect, and add them to the stack.
357 vector<Node *> deps = node->incoming_links;
358 assert(node->effect->num_inputs() == deps.size());
359 for (unsigned i = 0; i < deps.size(); ++i) {
360 bool start_new_phase = false;
362 // FIXME: If we sample directly from a texture, we won't need this.
363 if (node->effect->needs_texture_bounce()) {
364 start_new_phase = true;
367 if (deps[i]->outgoing_links.size() > 1) {
368 if (deps[i]->effect->num_inputs() > 0) {
369 // More than one effect uses this as the input,
370 // and it is not a texture itself.
371 // The easiest thing to do (and probably also the safest
372 // performance-wise in most cases) is to bounce it to a texture
373 // and then let the next passes read from that.
374 start_new_phase = true;
376 // For textures, we try to be slightly more clever;
377 // if none of our outputs need a bounce, we don't bounce
378 // but instead simply use the effect many times.
380 // Strictly speaking, we could bounce it for some outputs
381 // and use it directly for others, but the processing becomes
382 // somewhat simpler if the effect is only used in one such way.
383 for (unsigned j = 0; j < deps[i]->outgoing_links.size(); ++j) {
384 Node *rdep = deps[i]->outgoing_links[j];
385 start_new_phase |= rdep->effect->needs_texture_bounce();
390 if (deps[i]->effect->changes_output_size()) {
391 start_new_phase = true;
394 if (start_new_phase) {
395 effects_todo_other_phases.push(deps[i]);
396 this_phase_inputs.push_back(deps[i]);
398 effects_todo_this_phase.push(deps[i]);
404 // No more effects to do this phase. Take all the ones we have,
405 // and create a GLSL program for it.
406 if (!this_phase_effects.empty()) {
407 reverse(this_phase_effects.begin(), this_phase_effects.end());
408 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
409 this_phase_effects.back()->phase = phases.back();
410 this_phase_inputs.clear();
411 this_phase_effects.clear();
413 assert(this_phase_inputs.empty());
414 assert(this_phase_effects.empty());
416 // If we have no effects left, exit.
417 if (effects_todo_other_phases.empty()) {
421 Node *node = effects_todo_other_phases.top();
422 effects_todo_other_phases.pop();
424 if (completed_effects.count(node) == 0) {
425 // Start a new phase, calculating from this effect.
426 effects_todo_this_phase.push(node);
430 // Finally, since the phases are found from the output but must be executed
431 // from the input(s), reverse them, too.
432 reverse(phases.begin(), phases.end());
435 void EffectChain::output_dot(const char *filename)
437 if (movit_debug_level != MOVIT_DEBUG_ON) {
441 FILE *fp = fopen(filename, "w");
447 fprintf(fp, "digraph G {\n");
448 fprintf(fp, " output [shape=box label=\"(output)\"];\n");
449 for (unsigned i = 0; i < nodes.size(); ++i) {
450 // Find out which phase this event belongs to.
451 vector<int> in_phases;
452 for (unsigned j = 0; j < phases.size(); ++j) {
453 const Phase* p = phases[j];
454 if (find(p->effects.begin(), p->effects.end(), nodes[i]) != p->effects.end()) {
455 in_phases.push_back(j);
459 if (in_phases.empty()) {
460 fprintf(fp, " n%ld [label=\"%s\"];\n", (long)nodes[i], nodes[i]->effect->effect_type_id().c_str());
461 } else if (in_phases.size() == 1) {
462 fprintf(fp, " n%ld [label=\"%s\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
463 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
464 (in_phases[0] % 8) + 1);
466 // If we had new enough Graphviz, style="wedged" would probably be ideal here.
468 fprintf(fp, " n%ld [label=\"%s [in multiple phases]\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
469 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
470 (in_phases[0] % 8) + 1);
473 char from_node_id[256];
474 snprintf(from_node_id, 256, "n%ld", (long)nodes[i]);
476 for (unsigned j = 0; j < nodes[i]->outgoing_links.size(); ++j) {
477 char to_node_id[256];
478 snprintf(to_node_id, 256, "n%ld", (long)nodes[i]->outgoing_links[j]);
480 vector<string> labels = get_labels_for_edge(nodes[i], nodes[i]->outgoing_links[j]);
481 output_dot_edge(fp, from_node_id, to_node_id, labels);
484 if (nodes[i]->outgoing_links.empty() && !nodes[i]->disabled) {
486 vector<string> labels = get_labels_for_edge(nodes[i], NULL);
487 output_dot_edge(fp, from_node_id, "output", labels);
495 vector<string> EffectChain::get_labels_for_edge(const Node *from, const Node *to)
497 vector<string> labels;
499 if (to != NULL && to->effect->needs_texture_bounce()) {
500 labels.push_back("needs_bounce");
502 if (from->effect->changes_output_size()) {
503 labels.push_back("resize");
506 switch (from->output_color_space) {
507 case COLORSPACE_INVALID:
508 labels.push_back("spc[invalid]");
510 case COLORSPACE_REC_601_525:
511 labels.push_back("spc[rec601-525]");
513 case COLORSPACE_REC_601_625:
514 labels.push_back("spc[rec601-625]");
520 switch (from->output_gamma_curve) {
522 labels.push_back("gamma[invalid]");
525 labels.push_back("gamma[sRGB]");
527 case GAMMA_REC_601: // and GAMMA_REC_709
528 labels.push_back("gamma[rec601/709]");
534 switch (from->output_alpha_type) {
536 labels.push_back("alpha[invalid]");
539 labels.push_back("alpha[blank]");
541 case ALPHA_POSTMULTIPLIED:
542 labels.push_back("alpha[postmult]");
551 void EffectChain::output_dot_edge(FILE *fp,
552 const string &from_node_id,
553 const string &to_node_id,
554 const vector<string> &labels)
556 if (labels.empty()) {
557 fprintf(fp, " %s -> %s;\n", from_node_id.c_str(), to_node_id.c_str());
559 string label = labels[0];
560 for (unsigned k = 1; k < labels.size(); ++k) {
561 label += ", " + labels[k];
563 fprintf(fp, " %s -> %s [label=\"%s\"];\n", from_node_id.c_str(), to_node_id.c_str(), label.c_str());
567 void EffectChain::size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height)
569 unsigned scaled_width, scaled_height;
571 if (float(width) * aspect_denom >= float(height) * aspect_nom) {
572 // Same aspect, or W/H > aspect (image is wider than the frame).
573 // In either case, keep width, and adjust height.
574 scaled_width = width;
575 scaled_height = lrintf(width * aspect_denom / aspect_nom);
577 // W/H < aspect (image is taller than the frame), so keep height,
579 scaled_width = lrintf(height * aspect_nom / aspect_denom);
580 scaled_height = height;
583 // We should be consistently larger or smaller then the existing choice,
584 // since we have the same aspect.
585 assert(!(scaled_width < *output_width && scaled_height > *output_height));
586 assert(!(scaled_height < *output_height && scaled_width > *output_width));
588 if (scaled_width >= *output_width && scaled_height >= *output_height) {
589 *output_width = scaled_width;
590 *output_height = scaled_height;
594 // Propagate input texture sizes throughout, and inform effects downstream.
595 // (Like a lot of other code, we depend on effects being in topological order.)
596 void EffectChain::inform_input_sizes(Phase *phase)
598 // All effects that have a defined size (inputs and RTT inputs)
599 // get that. Reset all others.
600 for (unsigned i = 0; i < phase->effects.size(); ++i) {
601 Node *node = phase->effects[i];
602 if (node->effect->num_inputs() == 0) {
603 Input *input = static_cast<Input *>(node->effect);
604 node->output_width = input->get_width();
605 node->output_height = input->get_height();
606 assert(node->output_width != 0);
607 assert(node->output_height != 0);
609 node->output_width = node->output_height = 0;
612 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
613 Node *input = phase->inputs[i];
614 input->output_width = input->phase->virtual_output_width;
615 input->output_height = input->phase->virtual_output_height;
616 assert(input->output_width != 0);
617 assert(input->output_height != 0);
620 // Now propagate from the inputs towards the end, and inform as we go.
621 // The rules are simple:
623 // 1. Don't touch effects that already have given sizes (ie., inputs).
624 // 2. If all of your inputs have the same size, that will be your output size.
625 // 3. Otherwise, your output size is 0x0.
626 for (unsigned i = 0; i < phase->effects.size(); ++i) {
627 Node *node = phase->effects[i];
628 if (node->effect->num_inputs() == 0) {
631 unsigned this_output_width = 0;
632 unsigned this_output_height = 0;
633 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
634 Node *input = node->incoming_links[j];
635 node->effect->inform_input_size(j, input->output_width, input->output_height);
637 this_output_width = input->output_width;
638 this_output_height = input->output_height;
639 } else if (input->output_width != this_output_width || input->output_height != this_output_height) {
641 this_output_width = 0;
642 this_output_height = 0;
645 node->output_width = this_output_width;
646 node->output_height = this_output_height;
650 // Note: You should call inform_input_sizes() before this, as the last effect's
651 // desired output size might change based on the inputs.
652 void EffectChain::find_output_size(Phase *phase)
654 Node *output_node = phase->effects.back();
656 // If the last effect explicitly sets an output size, use that.
657 if (output_node->effect->changes_output_size()) {
658 output_node->effect->get_output_size(&phase->output_width, &phase->output_height,
659 &phase->virtual_output_width, &phase->virtual_output_height);
663 // If all effects have the same size, use that.
664 unsigned output_width = 0, output_height = 0;
665 bool all_inputs_same_size = true;
667 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
668 Node *input = phase->inputs[i];
669 assert(input->phase->output_width != 0);
670 assert(input->phase->output_height != 0);
671 if (output_width == 0 && output_height == 0) {
672 output_width = input->phase->virtual_output_width;
673 output_height = input->phase->virtual_output_height;
674 } else if (output_width != input->phase->virtual_output_width ||
675 output_height != input->phase->virtual_output_height) {
676 all_inputs_same_size = false;
679 for (unsigned i = 0; i < phase->effects.size(); ++i) {
680 Effect *effect = phase->effects[i]->effect;
681 if (effect->num_inputs() != 0) {
685 Input *input = static_cast<Input *>(effect);
686 if (output_width == 0 && output_height == 0) {
687 output_width = input->get_width();
688 output_height = input->get_height();
689 } else if (output_width != input->get_width() ||
690 output_height != input->get_height()) {
691 all_inputs_same_size = false;
695 if (all_inputs_same_size) {
696 assert(output_width != 0);
697 assert(output_height != 0);
698 phase->virtual_output_width = phase->output_width = output_width;
699 phase->virtual_output_height = phase->output_height = output_height;
703 // If not, fit all the inputs into the current aspect, and select the largest one.
706 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
707 Node *input = phase->inputs[i];
708 assert(input->phase->output_width != 0);
709 assert(input->phase->output_height != 0);
710 size_rectangle_to_fit(input->phase->output_width, input->phase->output_height, &output_width, &output_height);
712 for (unsigned i = 0; i < phase->effects.size(); ++i) {
713 Effect *effect = phase->effects[i]->effect;
714 if (effect->num_inputs() != 0) {
718 Input *input = static_cast<Input *>(effect);
719 size_rectangle_to_fit(input->get_width(), input->get_height(), &output_width, &output_height);
721 assert(output_width != 0);
722 assert(output_height != 0);
723 phase->virtual_output_width = phase->output_width = output_width;
724 phase->virtual_output_height = phase->output_height = output_height;
727 void EffectChain::sort_all_nodes_topologically()
729 nodes = topological_sort(nodes);
732 vector<Node *> EffectChain::topological_sort(const vector<Node *> &nodes)
734 set<Node *> nodes_left_to_visit(nodes.begin(), nodes.end());
735 vector<Node *> sorted_list;
736 for (unsigned i = 0; i < nodes.size(); ++i) {
737 topological_sort_visit_node(nodes[i], &nodes_left_to_visit, &sorted_list);
739 reverse(sorted_list.begin(), sorted_list.end());
743 void EffectChain::topological_sort_visit_node(Node *node, set<Node *> *nodes_left_to_visit, vector<Node *> *sorted_list)
745 if (nodes_left_to_visit->count(node) == 0) {
748 nodes_left_to_visit->erase(node);
749 for (unsigned i = 0; i < node->outgoing_links.size(); ++i) {
750 topological_sort_visit_node(node->outgoing_links[i], nodes_left_to_visit, sorted_list);
752 sorted_list->push_back(node);
755 void EffectChain::find_color_spaces_for_inputs()
757 for (unsigned i = 0; i < nodes.size(); ++i) {
758 Node *node = nodes[i];
759 if (node->disabled) {
762 if (node->incoming_links.size() == 0) {
763 Input *input = static_cast<Input *>(node->effect);
764 node->output_color_space = input->get_color_space();
765 node->output_gamma_curve = input->get_gamma_curve();
767 Effect::AlphaHandling alpha_handling = input->alpha_handling();
768 switch (alpha_handling) {
769 case Effect::OUTPUT_BLANK_ALPHA:
770 node->output_alpha_type = ALPHA_BLANK;
772 case Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA:
773 node->output_alpha_type = ALPHA_PREMULTIPLIED;
775 case Effect::OUTPUT_POSTMULTIPLIED_ALPHA:
776 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
778 case Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK:
779 case Effect::DONT_CARE_ALPHA_TYPE:
784 if (node->output_alpha_type == ALPHA_PREMULTIPLIED) {
785 assert(node->output_gamma_curve == GAMMA_LINEAR);
791 // Propagate gamma and color space information as far as we can in the graph.
792 // The rules are simple: Anything where all the inputs agree, get that as
793 // output as well. Anything else keeps having *_INVALID.
794 void EffectChain::propagate_gamma_and_color_space()
796 // We depend on going through the nodes in order.
797 sort_all_nodes_topologically();
799 for (unsigned i = 0; i < nodes.size(); ++i) {
800 Node *node = nodes[i];
801 if (node->disabled) {
804 assert(node->incoming_links.size() == node->effect->num_inputs());
805 if (node->incoming_links.size() == 0) {
806 assert(node->output_color_space != COLORSPACE_INVALID);
807 assert(node->output_gamma_curve != GAMMA_INVALID);
811 Colorspace color_space = node->incoming_links[0]->output_color_space;
812 GammaCurve gamma_curve = node->incoming_links[0]->output_gamma_curve;
813 for (unsigned j = 1; j < node->incoming_links.size(); ++j) {
814 if (node->incoming_links[j]->output_color_space != color_space) {
815 color_space = COLORSPACE_INVALID;
817 if (node->incoming_links[j]->output_gamma_curve != gamma_curve) {
818 gamma_curve = GAMMA_INVALID;
822 // The conversion effects already have their outputs set correctly,
823 // so leave them alone.
824 if (node->effect->effect_type_id() != "ColorspaceConversionEffect") {
825 node->output_color_space = color_space;
827 if (node->effect->effect_type_id() != "GammaCompressionEffect" &&
828 node->effect->effect_type_id() != "GammaExpansionEffect") {
829 node->output_gamma_curve = gamma_curve;
834 // Propagate alpha information as far as we can in the graph.
835 // Similar to propagate_gamma_and_color_space().
836 void EffectChain::propagate_alpha()
838 // We depend on going through the nodes in order.
839 sort_all_nodes_topologically();
841 for (unsigned i = 0; i < nodes.size(); ++i) {
842 Node *node = nodes[i];
843 if (node->disabled) {
846 assert(node->incoming_links.size() == node->effect->num_inputs());
847 if (node->incoming_links.size() == 0) {
848 assert(node->output_alpha_type != ALPHA_INVALID);
852 // The alpha multiplication/division effects are special cases.
853 if (node->effect->effect_type_id() == "AlphaMultiplicationEffect") {
854 assert(node->incoming_links.size() == 1);
855 assert(node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED);
856 node->output_alpha_type = ALPHA_PREMULTIPLIED;
859 if (node->effect->effect_type_id() == "AlphaDivisionEffect") {
860 assert(node->incoming_links.size() == 1);
861 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
862 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
866 // GammaCompressionEffect and GammaExpansionEffect are also a special case,
867 // because they are the only one that _need_ postmultiplied alpha.
868 if (node->effect->effect_type_id() == "GammaCompressionEffect" ||
869 node->effect->effect_type_id() == "GammaExpansionEffect") {
870 assert(node->incoming_links.size() == 1);
871 if (node->incoming_links[0]->output_alpha_type == ALPHA_BLANK) {
872 node->output_alpha_type = ALPHA_BLANK;
873 } else if (node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED) {
874 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
876 node->output_alpha_type = ALPHA_INVALID;
881 // Only inputs can have unconditional alpha output (OUTPUT_BLANK_ALPHA
882 // or OUTPUT_POSTMULTIPLIED_ALPHA), and they have already been
883 // taken care of above. Rationale: Even if you could imagine
884 // e.g. an effect that took in an image and set alpha=1.0
885 // unconditionally, it wouldn't make any sense to have it as
886 // e.g. OUTPUT_BLANK_ALPHA, since it wouldn't know whether it
887 // got its input pre- or postmultiplied, so it wouldn't know
888 // whether to divide away the old alpha or not.
889 Effect::AlphaHandling alpha_handling = node->effect->alpha_handling();
890 assert(alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
891 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK ||
892 alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
894 // If the node has multiple inputs, check that they are all valid and
896 bool any_invalid = false;
897 bool any_premultiplied = false;
898 bool any_postmultiplied = false;
900 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
901 switch (node->incoming_links[j]->output_alpha_type) {
906 // Blank is good as both pre- and postmultiplied alpha,
907 // so just ignore it.
909 case ALPHA_PREMULTIPLIED:
910 any_premultiplied = true;
912 case ALPHA_POSTMULTIPLIED:
913 any_postmultiplied = true;
921 node->output_alpha_type = ALPHA_INVALID;
925 // Inputs must be of the same type.
926 if (any_premultiplied && any_postmultiplied) {
927 node->output_alpha_type = ALPHA_INVALID;
931 if (alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
932 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
933 // If the effect has asked for premultiplied alpha, check that it has got it.
934 if (any_postmultiplied) {
935 node->output_alpha_type = ALPHA_INVALID;
936 } else if (!any_premultiplied &&
937 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
938 // Blank input alpha, and the effect preserves blank alpha.
939 node->output_alpha_type = ALPHA_BLANK;
941 node->output_alpha_type = ALPHA_PREMULTIPLIED;
944 // OK, all inputs are the same, and this effect is not going
946 assert(alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
947 if (any_premultiplied) {
948 node->output_alpha_type = ALPHA_PREMULTIPLIED;
949 } else if (any_postmultiplied) {
950 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
952 node->output_alpha_type = ALPHA_BLANK;
958 bool EffectChain::node_needs_colorspace_fix(Node *node)
960 if (node->disabled) {
963 if (node->effect->num_inputs() == 0) {
967 // propagate_gamma_and_color_space() has already set our output
968 // to COLORSPACE_INVALID if the inputs differ, so we can rely on that.
969 if (node->output_color_space == COLORSPACE_INVALID) {
972 return (node->effect->needs_srgb_primaries() && node->output_color_space != COLORSPACE_sRGB);
975 // Fix up color spaces so that there are no COLORSPACE_INVALID nodes left in
976 // the graph. Our strategy is not always optimal, but quite simple:
977 // Find an effect that's as early as possible where the inputs are of
978 // unacceptable colorspaces (that is, either different, or, if the effect only
979 // wants sRGB, not sRGB.) Add appropriate conversions on all its inputs,
980 // propagate the information anew, and repeat until there are no more such
982 void EffectChain::fix_internal_color_spaces()
984 unsigned colorspace_propagation_pass = 0;
988 for (unsigned i = 0; i < nodes.size(); ++i) {
989 Node *node = nodes[i];
990 if (!node_needs_colorspace_fix(node)) {
994 // Go through each input that is not sRGB, and insert
995 // a colorspace conversion after it.
996 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
997 Node *input = node->incoming_links[j];
998 assert(input->output_color_space != COLORSPACE_INVALID);
999 if (input->output_color_space == COLORSPACE_sRGB) {
1002 Node *conversion = add_node(new ColorspaceConversionEffect());
1003 CHECK(conversion->effect->set_int("source_space", input->output_color_space));
1004 CHECK(conversion->effect->set_int("destination_space", COLORSPACE_sRGB));
1005 conversion->output_color_space = COLORSPACE_sRGB;
1006 replace_sender(input, conversion);
1007 connect_nodes(input, conversion);
1010 // Re-sort topologically, and propagate the new information.
1011 propagate_gamma_and_color_space();
1018 sprintf(filename, "step5-colorspacefix-iter%u.dot", ++colorspace_propagation_pass);
1019 output_dot(filename);
1020 assert(colorspace_propagation_pass < 100);
1021 } while (found_any);
1023 for (unsigned i = 0; i < nodes.size(); ++i) {
1024 Node *node = nodes[i];
1025 if (node->disabled) {
1028 assert(node->output_color_space != COLORSPACE_INVALID);
1032 bool EffectChain::node_needs_alpha_fix(Node *node)
1034 if (node->disabled) {
1038 // propagate_alpha() has already set our output to ALPHA_INVALID if the
1039 // inputs differ or we are otherwise in mismatch, so we can rely on that.
1040 return (node->output_alpha_type == ALPHA_INVALID);
1043 // Fix up alpha so that there are no ALPHA_INVALID nodes left in
1044 // the graph. Similar to fix_internal_color_spaces().
1045 void EffectChain::fix_internal_alpha(unsigned step)
1047 unsigned alpha_propagation_pass = 0;
1051 for (unsigned i = 0; i < nodes.size(); ++i) {
1052 Node *node = nodes[i];
1053 if (!node_needs_alpha_fix(node)) {
1057 // If we need to fix up GammaExpansionEffect, then clearly something
1058 // is wrong, since the combination of premultiplied alpha and nonlinear inputs
1060 assert(node->effect->effect_type_id() != "GammaExpansionEffect");
1062 AlphaType desired_type = ALPHA_PREMULTIPLIED;
1064 // GammaCompressionEffect is special; it needs postmultiplied alpha.
1065 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1066 assert(node->incoming_links.size() == 1);
1067 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
1068 desired_type = ALPHA_POSTMULTIPLIED;
1071 // Go through each input that is not premultiplied alpha, and insert
1072 // a conversion before it.
1073 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1074 Node *input = node->incoming_links[j];
1075 assert(input->output_alpha_type != ALPHA_INVALID);
1076 if (input->output_alpha_type == desired_type ||
1077 input->output_alpha_type == ALPHA_BLANK) {
1081 if (desired_type == ALPHA_PREMULTIPLIED) {
1082 conversion = add_node(new AlphaMultiplicationEffect());
1084 conversion = add_node(new AlphaDivisionEffect());
1086 conversion->output_alpha_type = desired_type;
1087 replace_sender(input, conversion);
1088 connect_nodes(input, conversion);
1091 // Re-sort topologically, and propagate the new information.
1092 propagate_gamma_and_color_space();
1100 sprintf(filename, "step%u-alphafix-iter%u.dot", step, ++alpha_propagation_pass);
1101 output_dot(filename);
1102 assert(alpha_propagation_pass < 100);
1103 } while (found_any);
1105 for (unsigned i = 0; i < nodes.size(); ++i) {
1106 Node *node = nodes[i];
1107 if (node->disabled) {
1110 assert(node->output_alpha_type != ALPHA_INVALID);
1114 // Make so that the output is in the desired color space.
1115 void EffectChain::fix_output_color_space()
1117 Node *output = find_output_node();
1118 if (output->output_color_space != output_format.color_space) {
1119 Node *conversion = add_node(new ColorspaceConversionEffect());
1120 CHECK(conversion->effect->set_int("source_space", output->output_color_space));
1121 CHECK(conversion->effect->set_int("destination_space", output_format.color_space));
1122 conversion->output_color_space = output_format.color_space;
1123 connect_nodes(output, conversion);
1125 propagate_gamma_and_color_space();
1129 // Make so that the output is in the desired pre-/postmultiplication alpha state.
1130 void EffectChain::fix_output_alpha()
1132 Node *output = find_output_node();
1133 assert(output->output_alpha_type != ALPHA_INVALID);
1134 if (output->output_alpha_type == ALPHA_BLANK) {
1135 // No alpha output, so we don't care.
1138 if (output->output_alpha_type == ALPHA_PREMULTIPLIED &&
1139 output_alpha_format == OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED) {
1140 Node *conversion = add_node(new AlphaDivisionEffect());
1141 connect_nodes(output, conversion);
1143 propagate_gamma_and_color_space();
1145 if (output->output_alpha_type == ALPHA_POSTMULTIPLIED &&
1146 output_alpha_format == OUTPUT_ALPHA_FORMAT_PREMULTIPLIED) {
1147 Node *conversion = add_node(new AlphaMultiplicationEffect());
1148 connect_nodes(output, conversion);
1150 propagate_gamma_and_color_space();
1154 bool EffectChain::node_needs_gamma_fix(Node *node)
1156 if (node->disabled) {
1160 // Small hack since the output is not an explicit node:
1161 // If we are the last node and our output is in the wrong
1162 // space compared to EffectChain's output, we need to fix it.
1163 // This will only take us to linear, but fix_output_gamma()
1164 // will come and take us to the desired output gamma
1167 // This needs to be before everything else, since it could
1168 // even apply to inputs (if they are the only effect).
1169 if (node->outgoing_links.empty() &&
1170 node->output_gamma_curve != output_format.gamma_curve &&
1171 node->output_gamma_curve != GAMMA_LINEAR) {
1175 if (node->effect->num_inputs() == 0) {
1179 // propagate_gamma_and_color_space() has already set our output
1180 // to GAMMA_INVALID if the inputs differ, so we can rely on that,
1181 // except for GammaCompressionEffect.
1182 if (node->output_gamma_curve == GAMMA_INVALID) {
1185 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1186 assert(node->incoming_links.size() == 1);
1187 return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
1190 return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
1193 // Very similar to fix_internal_color_spaces(), but for gamma.
1194 // There is one difference, though; before we start adding conversion nodes,
1195 // we see if we can get anything out of asking the sources to deliver
1196 // linear gamma directly. fix_internal_gamma_by_asking_inputs()
1197 // does that part, while fix_internal_gamma_by_inserting_nodes()
1198 // inserts nodes as needed afterwards.
1199 void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
1201 unsigned gamma_propagation_pass = 0;
1205 for (unsigned i = 0; i < nodes.size(); ++i) {
1206 Node *node = nodes[i];
1207 if (!node_needs_gamma_fix(node)) {
1211 // See if all inputs can give us linear gamma. If not, leave it.
1212 vector<Node *> nonlinear_inputs;
1213 find_all_nonlinear_inputs(node, &nonlinear_inputs);
1214 assert(!nonlinear_inputs.empty());
1217 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1218 Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
1219 all_ok &= input->can_output_linear_gamma();
1226 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1227 CHECK(nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1));
1228 nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
1231 // Re-sort topologically, and propagate the new information.
1232 propagate_gamma_and_color_space();
1239 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1240 output_dot(filename);
1241 assert(gamma_propagation_pass < 100);
1242 } while (found_any);
1245 void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
1247 unsigned gamma_propagation_pass = 0;
1251 for (unsigned i = 0; i < nodes.size(); ++i) {
1252 Node *node = nodes[i];
1253 if (!node_needs_gamma_fix(node)) {
1257 // Special case: We could be an input and still be asked to
1258 // fix our gamma; if so, we should be the only node
1259 // (as node_needs_gamma_fix() would only return true in
1260 // for an input in that case). That means we should insert
1261 // a conversion node _after_ ourselves.
1262 if (node->incoming_links.empty()) {
1263 assert(node->outgoing_links.empty());
1264 Node *conversion = add_node(new GammaExpansionEffect());
1265 CHECK(conversion->effect->set_int("source_curve", node->output_gamma_curve));
1266 conversion->output_gamma_curve = GAMMA_LINEAR;
1267 connect_nodes(node, conversion);
1270 // If not, go through each input that is not linear gamma,
1271 // and insert a gamma conversion after it.
1272 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1273 Node *input = node->incoming_links[j];
1274 assert(input->output_gamma_curve != GAMMA_INVALID);
1275 if (input->output_gamma_curve == GAMMA_LINEAR) {
1278 Node *conversion = add_node(new GammaExpansionEffect());
1279 CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
1280 conversion->output_gamma_curve = GAMMA_LINEAR;
1281 replace_sender(input, conversion);
1282 connect_nodes(input, conversion);
1285 // Re-sort topologically, and propagate the new information.
1287 propagate_gamma_and_color_space();
1294 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1295 output_dot(filename);
1296 assert(gamma_propagation_pass < 100);
1297 } while (found_any);
1299 for (unsigned i = 0; i < nodes.size(); ++i) {
1300 Node *node = nodes[i];
1301 if (node->disabled) {
1304 assert(node->output_gamma_curve != GAMMA_INVALID);
1308 // Make so that the output is in the desired gamma.
1309 // Note that this assumes linear input gamma, so it might create the need
1310 // for another pass of fix_internal_gamma().
1311 void EffectChain::fix_output_gamma()
1313 Node *output = find_output_node();
1314 if (output->output_gamma_curve != output_format.gamma_curve) {
1315 Node *conversion = add_node(new GammaCompressionEffect());
1316 CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
1317 conversion->output_gamma_curve = output_format.gamma_curve;
1318 connect_nodes(output, conversion);
1322 // If the user has requested dither, add a DitherEffect right at the end
1323 // (after GammaCompressionEffect etc.). This needs to be done after everything else,
1324 // since dither is about the only effect that can _not_ be done in linear space.
1325 void EffectChain::add_dither_if_needed()
1327 if (num_dither_bits == 0) {
1330 Node *output = find_output_node();
1331 Node *dither = add_node(new DitherEffect());
1332 CHECK(dither->effect->set_int("num_bits", num_dither_bits));
1333 connect_nodes(output, dither);
1335 dither_effect = dither->effect;
1338 // Find the output node. This is, simply, one that has no outgoing links.
1339 // If there are multiple ones, the graph is malformed (we do not support
1340 // multiple outputs right now).
1341 Node *EffectChain::find_output_node()
1343 vector<Node *> output_nodes;
1344 for (unsigned i = 0; i < nodes.size(); ++i) {
1345 Node *node = nodes[i];
1346 if (node->disabled) {
1349 if (node->outgoing_links.empty()) {
1350 output_nodes.push_back(node);
1353 assert(output_nodes.size() == 1);
1354 return output_nodes[0];
1357 void EffectChain::finalize()
1359 // Save the current locale, and set it to C, so that we can output decimal
1360 // numbers with printf and be sure to get them in the format mandated by GLSL.
1361 char *saved_locale = setlocale(LC_NUMERIC, "C");
1363 // Output the graph as it is before we do any conversions on it.
1364 output_dot("step0-start.dot");
1366 // Give each effect in turn a chance to rewrite its own part of the graph.
1367 // Note that if more effects are added as part of this, they will be
1368 // picked up as part of the same for loop, since they are added at the end.
1369 for (unsigned i = 0; i < nodes.size(); ++i) {
1370 nodes[i]->effect->rewrite_graph(this, nodes[i]);
1372 output_dot("step1-rewritten.dot");
1374 find_color_spaces_for_inputs();
1375 output_dot("step2-input-colorspace.dot");
1378 output_dot("step3-propagated-alpha.dot");
1380 propagate_gamma_and_color_space();
1381 output_dot("step4-propagated-all.dot");
1383 fix_internal_color_spaces();
1384 fix_internal_alpha(6);
1385 fix_output_color_space();
1386 output_dot("step7-output-colorspacefix.dot");
1388 output_dot("step8-output-alphafix.dot");
1390 // Note that we need to fix gamma after colorspace conversion,
1391 // because colorspace conversions might create needs for gamma conversions.
1392 // Also, we need to run an extra pass of fix_internal_gamma() after
1393 // fixing the output gamma, as we only have conversions to/from linear,
1394 // and fix_internal_alpha() since GammaCompressionEffect needs
1395 // postmultiplied input.
1396 fix_internal_gamma_by_asking_inputs(9);
1397 fix_internal_gamma_by_inserting_nodes(10);
1399 output_dot("step11-output-gammafix.dot");
1401 output_dot("step12-output-alpha-propagated.dot");
1402 fix_internal_alpha(13);
1403 output_dot("step14-output-alpha-fixed.dot");
1404 fix_internal_gamma_by_asking_inputs(15);
1405 fix_internal_gamma_by_inserting_nodes(16);
1407 output_dot("step17-before-dither.dot");
1409 add_dither_if_needed();
1411 output_dot("step18-final.dot");
1413 // Construct all needed GLSL programs, starting at the output.
1414 construct_glsl_programs(find_output_node());
1416 output_dot("step19-split-to-phases.dot");
1418 assert(phases[0]->inputs.empty());
1421 setlocale(LC_NUMERIC, saved_locale);
1424 void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height)
1428 // Save original viewport.
1429 GLuint x = 0, y = 0;
1432 if (width == 0 && height == 0) {
1434 glGetIntegerv(GL_VIEWPORT, viewport);
1437 width = viewport[2];
1438 height = viewport[3];
1442 glDisable(GL_BLEND);
1444 glDisable(GL_DEPTH_TEST);
1446 glDepthMask(GL_FALSE);
1449 if (phases.size() > 1) {
1450 glGenFramebuffers(1, &fbo);
1452 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1456 set<Node *> generated_mipmaps;
1458 // We choose the simplest option of having one texture per output,
1459 // since otherwise this turns into an (albeit simple) register allocation problem.
1460 map<Phase *, GLuint> output_textures;
1462 for (unsigned phase = 0; phase < phases.size(); ++phase) {
1463 // Find a texture for this phase.
1464 inform_input_sizes(phases[phase]);
1465 if (phase != phases.size() - 1) {
1466 find_output_size(phases[phase]);
1468 GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height);
1469 output_textures.insert(make_pair(phases[phase], tex_num));
1472 const GLuint glsl_program_num = phases[phase]->glsl_program_num;
1473 glUseProgram(glsl_program_num);
1476 // Set up RTT inputs for this phase.
1477 for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
1478 glActiveTexture(GL_TEXTURE0 + sampler);
1479 Node *input = phases[phase]->inputs[sampler];
1480 glBindTexture(GL_TEXTURE_2D, output_textures[input->phase]);
1482 if (phases[phase]->input_needs_mipmaps) {
1483 if (generated_mipmaps.count(input) == 0) {
1484 glGenerateMipmap(GL_TEXTURE_2D);
1486 generated_mipmaps.insert(input);
1488 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1491 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1494 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1496 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1499 string texture_name = string("tex_") + phases[phase]->effect_ids[input];
1500 glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler);
1504 // And now the output.
1505 if (phase == phases.size() - 1) {
1506 // Last phase goes to the output the user specified.
1507 glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
1509 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
1510 assert(status == GL_FRAMEBUFFER_COMPLETE);
1511 glViewport(x, y, width, height);
1512 if (dither_effect != NULL) {
1513 CHECK(dither_effect->set_int("output_width", width));
1514 CHECK(dither_effect->set_int("output_height", height));
1517 glFramebufferTexture2D(
1519 GL_COLOR_ATTACHMENT0,
1521 output_textures[phases[phase]],
1524 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
1525 assert(status == GL_FRAMEBUFFER_COMPLETE);
1526 glViewport(0, 0, phases[phase]->output_width, phases[phase]->output_height);
1529 // Give the required parameters to all the effects.
1530 unsigned sampler_num = phases[phase]->inputs.size();
1531 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1532 Node *node = phases[phase]->effects[i];
1533 node->effect->set_gl_state(glsl_program_num, phases[phase]->effect_ids[node], &sampler_num);
1538 float vertices[] = {
1545 int position_attrib = glGetAttribLocation(glsl_program_num, "position");
1546 assert(position_attrib != -1);
1547 glEnableVertexAttribArray(position_attrib);
1549 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
1552 int texcoord_attrib = glGetAttribLocation(glsl_program_num, "texcoord");
1553 if (texcoord_attrib != -1) {
1554 glEnableVertexAttribArray(texcoord_attrib);
1556 glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices); // Same as texcoords.
1560 glDrawArrays(GL_QUADS, 0, 4);
1565 glDisableVertexAttribArray(position_attrib);
1567 if (texcoord_attrib != -1) {
1568 glDisableVertexAttribArray(texcoord_attrib);
1572 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1573 Node *node = phases[phase]->effects[i];
1574 node->effect->clear_gl_state();
1578 for (map<Phase *, GLuint>::const_iterator texture_it = output_textures.begin();
1579 texture_it != output_textures.end();
1581 resource_pool->release_2d_texture(texture_it->second);
1584 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1588 glDeleteFramebuffers(1, &fbo);
1593 } // namespace movit