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) {
62 for (map<void *, GLuint>::const_iterator fbo_it = fbos.begin();
63 fbo_it != fbos.end(); ++fbo_it) {
64 glDeleteFramebuffers(1, &fbo_it->second);
69 Input *EffectChain::add_input(Input *input)
72 inputs.push_back(input);
77 void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
80 output_format = format;
81 output_alpha_format = alpha_format;
84 Node *EffectChain::add_node(Effect *effect)
86 for (unsigned i = 0; i < nodes.size(); ++i) {
87 assert(nodes[i]->effect != effect);
90 Node *node = new Node;
91 node->effect = effect;
92 node->disabled = false;
93 node->output_color_space = COLORSPACE_INVALID;
94 node->output_gamma_curve = GAMMA_INVALID;
95 node->output_alpha_type = ALPHA_INVALID;
97 nodes.push_back(node);
98 node_map[effect] = node;
99 effect->inform_added(this);
103 void EffectChain::connect_nodes(Node *sender, Node *receiver)
105 sender->outgoing_links.push_back(receiver);
106 receiver->incoming_links.push_back(sender);
109 void EffectChain::replace_receiver(Node *old_receiver, Node *new_receiver)
111 new_receiver->incoming_links = old_receiver->incoming_links;
112 old_receiver->incoming_links.clear();
114 for (unsigned i = 0; i < new_receiver->incoming_links.size(); ++i) {
115 Node *sender = new_receiver->incoming_links[i];
116 for (unsigned j = 0; j < sender->outgoing_links.size(); ++j) {
117 if (sender->outgoing_links[j] == old_receiver) {
118 sender->outgoing_links[j] = new_receiver;
124 void EffectChain::replace_sender(Node *old_sender, Node *new_sender)
126 new_sender->outgoing_links = old_sender->outgoing_links;
127 old_sender->outgoing_links.clear();
129 for (unsigned i = 0; i < new_sender->outgoing_links.size(); ++i) {
130 Node *receiver = new_sender->outgoing_links[i];
131 for (unsigned j = 0; j < receiver->incoming_links.size(); ++j) {
132 if (receiver->incoming_links[j] == old_sender) {
133 receiver->incoming_links[j] = new_sender;
139 void EffectChain::insert_node_between(Node *sender, Node *middle, Node *receiver)
141 for (unsigned i = 0; i < sender->outgoing_links.size(); ++i) {
142 if (sender->outgoing_links[i] == receiver) {
143 sender->outgoing_links[i] = middle;
144 middle->incoming_links.push_back(sender);
147 for (unsigned i = 0; i < receiver->incoming_links.size(); ++i) {
148 if (receiver->incoming_links[i] == sender) {
149 receiver->incoming_links[i] = middle;
150 middle->outgoing_links.push_back(receiver);
154 assert(middle->incoming_links.size() == middle->effect->num_inputs());
157 GLenum EffectChain::get_input_sampler(Node *node, unsigned input_num) const
159 assert(node->effect->needs_texture_bounce());
160 assert(input_num < node->incoming_links.size());
161 assert(node->incoming_links[input_num]->bound_sampler_num >= 0);
162 assert(node->incoming_links[input_num]->bound_sampler_num < 8);
163 return GL_TEXTURE0 + node->incoming_links[input_num]->bound_sampler_num;
166 void EffectChain::find_all_nonlinear_inputs(Node *node, vector<Node *> *nonlinear_inputs)
168 if (node->output_gamma_curve == GAMMA_LINEAR &&
169 node->effect->effect_type_id() != "GammaCompressionEffect") {
172 if (node->effect->num_inputs() == 0) {
173 nonlinear_inputs->push_back(node);
175 assert(node->effect->num_inputs() == node->incoming_links.size());
176 for (unsigned i = 0; i < node->incoming_links.size(); ++i) {
177 find_all_nonlinear_inputs(node->incoming_links[i], nonlinear_inputs);
182 Effect *EffectChain::add_effect(Effect *effect, const vector<Effect *> &inputs)
185 assert(inputs.size() == effect->num_inputs());
186 Node *node = add_node(effect);
187 for (unsigned i = 0; i < inputs.size(); ++i) {
188 assert(node_map.count(inputs[i]) != 0);
189 connect_nodes(node_map[inputs[i]], node);
194 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
195 string replace_prefix(const string &text, const string &prefix)
200 while (start < text.size()) {
201 size_t pos = text.find("PREFIX(", start);
202 if (pos == string::npos) {
203 output.append(text.substr(start, string::npos));
207 output.append(text.substr(start, pos - start));
208 output.append(prefix);
211 pos += strlen("PREFIX(");
213 // Output stuff until we find the matching ), which we then eat.
215 size_t end_arg_pos = pos;
216 while (end_arg_pos < text.size()) {
217 if (text[end_arg_pos] == '(') {
219 } else if (text[end_arg_pos] == ')') {
227 output.append(text.substr(pos, end_arg_pos - pos));
235 void EffectChain::compile_glsl_program(Phase *phase)
237 string frag_shader = read_file("header.frag");
239 // Create functions for all the texture inputs that we need.
240 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
241 Node *input = phase->inputs[i]->output_node;
243 sprintf(effect_id, "in%u", i);
244 phase->effect_ids.insert(make_pair(input, effect_id));
246 frag_shader += string("uniform sampler2D tex_") + effect_id + ";\n";
247 frag_shader += string("vec4 ") + effect_id + "(vec2 tc) {\n";
248 frag_shader += "\treturn texture2D(tex_" + string(effect_id) + ", tc);\n";
249 frag_shader += "}\n";
253 for (unsigned i = 0; i < phase->effects.size(); ++i) {
254 Node *node = phase->effects[i];
256 sprintf(effect_id, "eff%u", i);
257 phase->effect_ids.insert(make_pair(node, effect_id));
259 if (node->incoming_links.size() == 1) {
260 frag_shader += string("#define INPUT ") + phase->effect_ids[node->incoming_links[0]] + "\n";
262 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
264 sprintf(buf, "#define INPUT%d %s\n", j + 1, phase->effect_ids[node->incoming_links[j]].c_str());
270 frag_shader += string("#define FUNCNAME ") + effect_id + "\n";
271 frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), effect_id);
272 frag_shader += replace_prefix(node->effect->output_fragment_shader(), effect_id);
273 frag_shader += "#undef PREFIX\n";
274 frag_shader += "#undef FUNCNAME\n";
275 if (node->incoming_links.size() == 1) {
276 frag_shader += "#undef INPUT\n";
278 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
280 sprintf(buf, "#undef INPUT%d\n", j + 1);
286 frag_shader += string("#define INPUT ") + phase->effect_ids[phase->effects.back()] + "\n";
287 frag_shader.append(read_file("footer.frag"));
289 phase->glsl_program_num = resource_pool->compile_glsl_program(read_file("vs.vert"), frag_shader);
292 // Construct GLSL programs, starting at the given effect and following
293 // the chain from there. We end a program every time we come to an effect
294 // marked as "needs texture bounce", one that is used by multiple other
295 // effects, every time an effect wants to change the output size,
296 // and of course at the end.
298 // We follow a quite simple depth-first search from the output, although
299 // without recursing explicitly within each phase.
300 Phase *EffectChain::construct_phase(Node *output, map<Node *, Phase *> *completed_effects)
302 if (completed_effects->count(output)) {
303 return (*completed_effects)[output];
306 Phase *phase = new Phase;
307 phase->output_node = output;
309 // Effects that we have yet to calculate, but that we know should
310 // be in the current phase.
311 stack<Node *> effects_todo_this_phase;
312 effects_todo_this_phase.push(output);
314 while (!effects_todo_this_phase.empty()) {
315 Node *node = effects_todo_this_phase.top();
316 effects_todo_this_phase.pop();
318 // This should currently only happen for effects that are inputs
319 // (either true inputs or phase outputs). We special-case inputs,
320 // and then deduplicate phase outputs below.
321 if (node->effect->num_inputs() == 0) {
322 if (find(phase->effects.begin(), phase->effects.end(), node) != phase->effects.end()) {
326 assert(completed_effects->count(node) == 0);
329 phase->effects.push_back(node);
331 // Find all the dependencies of this effect, and add them to the stack.
332 vector<Node *> deps = node->incoming_links;
333 assert(node->effect->num_inputs() == deps.size());
334 for (unsigned i = 0; i < deps.size(); ++i) {
335 bool start_new_phase = false;
337 if (node->effect->needs_texture_bounce() &&
338 !deps[i]->effect->is_single_texture()) {
339 start_new_phase = true;
342 if (deps[i]->outgoing_links.size() > 1) {
343 if (!deps[i]->effect->is_single_texture()) {
344 // More than one effect uses this as the input,
345 // and it is not a texture itself.
346 // The easiest thing to do (and probably also the safest
347 // performance-wise in most cases) is to bounce it to a texture
348 // and then let the next passes read from that.
349 start_new_phase = true;
351 assert(deps[i]->effect->num_inputs() == 0);
353 // For textures, we try to be slightly more clever;
354 // if none of our outputs need a bounce, we don't bounce
355 // but instead simply use the effect many times.
357 // Strictly speaking, we could bounce it for some outputs
358 // and use it directly for others, but the processing becomes
359 // somewhat simpler if the effect is only used in one such way.
360 for (unsigned j = 0; j < deps[i]->outgoing_links.size(); ++j) {
361 Node *rdep = deps[i]->outgoing_links[j];
362 start_new_phase |= rdep->effect->needs_texture_bounce();
367 if (deps[i]->effect->changes_output_size()) {
368 start_new_phase = true;
371 if (start_new_phase) {
372 phase->inputs.push_back(construct_phase(deps[i], completed_effects));
374 effects_todo_this_phase.push(deps[i]);
379 // No more effects to do this phase. Take all the ones we have,
380 // and create a GLSL program for it.
381 assert(!phase->effects.empty());
383 // Deduplicate the inputs.
384 sort(phase->inputs.begin(), phase->inputs.end());
385 phase->inputs.erase(unique(phase->inputs.begin(), phase->inputs.end()), phase->inputs.end());
387 // We added the effects from the output and back, but we need to output
388 // them in topological sort order in the shader.
389 phase->effects = topological_sort(phase->effects);
391 // Figure out if we need mipmaps or not, and if so, tell the inputs that.
392 phase->input_needs_mipmaps = false;
393 for (unsigned i = 0; i < phase->effects.size(); ++i) {
394 Node *node = phase->effects[i];
395 phase->input_needs_mipmaps |= node->effect->needs_mipmaps();
397 for (unsigned i = 0; i < phase->effects.size(); ++i) {
398 Node *node = phase->effects[i];
399 if (node->effect->num_inputs() == 0) {
400 CHECK(node->effect->set_int("needs_mipmaps", phase->input_needs_mipmaps));
404 // Actually make the shader for this phase.
405 compile_glsl_program(phase);
407 assert(completed_effects->count(output) == 0);
408 completed_effects->insert(make_pair(output, phase));
409 phases.push_back(phase);
413 void EffectChain::output_dot(const char *filename)
415 if (movit_debug_level != MOVIT_DEBUG_ON) {
419 FILE *fp = fopen(filename, "w");
425 fprintf(fp, "digraph G {\n");
426 fprintf(fp, " output [shape=box label=\"(output)\"];\n");
427 for (unsigned i = 0; i < nodes.size(); ++i) {
428 // Find out which phase this event belongs to.
429 vector<int> in_phases;
430 for (unsigned j = 0; j < phases.size(); ++j) {
431 const Phase* p = phases[j];
432 if (find(p->effects.begin(), p->effects.end(), nodes[i]) != p->effects.end()) {
433 in_phases.push_back(j);
437 if (in_phases.empty()) {
438 fprintf(fp, " n%ld [label=\"%s\"];\n", (long)nodes[i], nodes[i]->effect->effect_type_id().c_str());
439 } else if (in_phases.size() == 1) {
440 fprintf(fp, " n%ld [label=\"%s\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
441 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
442 (in_phases[0] % 8) + 1);
444 // If we had new enough Graphviz, style="wedged" would probably be ideal here.
446 fprintf(fp, " n%ld [label=\"%s [in multiple phases]\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
447 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
448 (in_phases[0] % 8) + 1);
451 char from_node_id[256];
452 snprintf(from_node_id, 256, "n%ld", (long)nodes[i]);
454 for (unsigned j = 0; j < nodes[i]->outgoing_links.size(); ++j) {
455 char to_node_id[256];
456 snprintf(to_node_id, 256, "n%ld", (long)nodes[i]->outgoing_links[j]);
458 vector<string> labels = get_labels_for_edge(nodes[i], nodes[i]->outgoing_links[j]);
459 output_dot_edge(fp, from_node_id, to_node_id, labels);
462 if (nodes[i]->outgoing_links.empty() && !nodes[i]->disabled) {
464 vector<string> labels = get_labels_for_edge(nodes[i], NULL);
465 output_dot_edge(fp, from_node_id, "output", labels);
473 vector<string> EffectChain::get_labels_for_edge(const Node *from, const Node *to)
475 vector<string> labels;
477 if (to != NULL && to->effect->needs_texture_bounce()) {
478 labels.push_back("needs_bounce");
480 if (from->effect->changes_output_size()) {
481 labels.push_back("resize");
484 switch (from->output_color_space) {
485 case COLORSPACE_INVALID:
486 labels.push_back("spc[invalid]");
488 case COLORSPACE_REC_601_525:
489 labels.push_back("spc[rec601-525]");
491 case COLORSPACE_REC_601_625:
492 labels.push_back("spc[rec601-625]");
498 switch (from->output_gamma_curve) {
500 labels.push_back("gamma[invalid]");
503 labels.push_back("gamma[sRGB]");
505 case GAMMA_REC_601: // and GAMMA_REC_709
506 labels.push_back("gamma[rec601/709]");
512 switch (from->output_alpha_type) {
514 labels.push_back("alpha[invalid]");
517 labels.push_back("alpha[blank]");
519 case ALPHA_POSTMULTIPLIED:
520 labels.push_back("alpha[postmult]");
529 void EffectChain::output_dot_edge(FILE *fp,
530 const string &from_node_id,
531 const string &to_node_id,
532 const vector<string> &labels)
534 if (labels.empty()) {
535 fprintf(fp, " %s -> %s;\n", from_node_id.c_str(), to_node_id.c_str());
537 string label = labels[0];
538 for (unsigned k = 1; k < labels.size(); ++k) {
539 label += ", " + labels[k];
541 fprintf(fp, " %s -> %s [label=\"%s\"];\n", from_node_id.c_str(), to_node_id.c_str(), label.c_str());
545 void EffectChain::size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height)
547 unsigned scaled_width, scaled_height;
549 if (float(width) * aspect_denom >= float(height) * aspect_nom) {
550 // Same aspect, or W/H > aspect (image is wider than the frame).
551 // In either case, keep width, and adjust height.
552 scaled_width = width;
553 scaled_height = lrintf(width * aspect_denom / aspect_nom);
555 // W/H < aspect (image is taller than the frame), so keep height,
557 scaled_width = lrintf(height * aspect_nom / aspect_denom);
558 scaled_height = height;
561 // We should be consistently larger or smaller then the existing choice,
562 // since we have the same aspect.
563 assert(!(scaled_width < *output_width && scaled_height > *output_height));
564 assert(!(scaled_height < *output_height && scaled_width > *output_width));
566 if (scaled_width >= *output_width && scaled_height >= *output_height) {
567 *output_width = scaled_width;
568 *output_height = scaled_height;
572 // Propagate input texture sizes throughout, and inform effects downstream.
573 // (Like a lot of other code, we depend on effects being in topological order.)
574 void EffectChain::inform_input_sizes(Phase *phase)
576 // All effects that have a defined size (inputs and RTT inputs)
577 // get that. Reset all others.
578 for (unsigned i = 0; i < phase->effects.size(); ++i) {
579 Node *node = phase->effects[i];
580 if (node->effect->num_inputs() == 0) {
581 Input *input = static_cast<Input *>(node->effect);
582 node->output_width = input->get_width();
583 node->output_height = input->get_height();
584 assert(node->output_width != 0);
585 assert(node->output_height != 0);
587 node->output_width = node->output_height = 0;
590 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
591 Phase *input = phase->inputs[i];
592 input->output_node->output_width = input->virtual_output_width;
593 input->output_node->output_height = input->virtual_output_height;
594 assert(input->output_node->output_width != 0);
595 assert(input->output_node->output_height != 0);
598 // Now propagate from the inputs towards the end, and inform as we go.
599 // The rules are simple:
601 // 1. Don't touch effects that already have given sizes (ie., inputs).
602 // 2. If all of your inputs have the same size, that will be your output size.
603 // 3. Otherwise, your output size is 0x0.
604 for (unsigned i = 0; i < phase->effects.size(); ++i) {
605 Node *node = phase->effects[i];
606 if (node->effect->num_inputs() == 0) {
609 unsigned this_output_width = 0;
610 unsigned this_output_height = 0;
611 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
612 Node *input = node->incoming_links[j];
613 node->effect->inform_input_size(j, input->output_width, input->output_height);
615 this_output_width = input->output_width;
616 this_output_height = input->output_height;
617 } else if (input->output_width != this_output_width || input->output_height != this_output_height) {
619 this_output_width = 0;
620 this_output_height = 0;
623 node->output_width = this_output_width;
624 node->output_height = this_output_height;
628 // Note: You should call inform_input_sizes() before this, as the last effect's
629 // desired output size might change based on the inputs.
630 void EffectChain::find_output_size(Phase *phase)
632 Node *output_node = phase->effects.back();
634 // If the last effect explicitly sets an output size, use that.
635 if (output_node->effect->changes_output_size()) {
636 output_node->effect->get_output_size(&phase->output_width, &phase->output_height,
637 &phase->virtual_output_width, &phase->virtual_output_height);
641 // If all effects have the same size, use that.
642 unsigned output_width = 0, output_height = 0;
643 bool all_inputs_same_size = true;
645 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
646 Phase *input = phase->inputs[i];
647 assert(input->output_width != 0);
648 assert(input->output_height != 0);
649 if (output_width == 0 && output_height == 0) {
650 output_width = input->virtual_output_width;
651 output_height = input->virtual_output_height;
652 } else if (output_width != input->virtual_output_width ||
653 output_height != input->virtual_output_height) {
654 all_inputs_same_size = false;
657 for (unsigned i = 0; i < phase->effects.size(); ++i) {
658 Effect *effect = phase->effects[i]->effect;
659 if (effect->num_inputs() != 0) {
663 Input *input = static_cast<Input *>(effect);
664 if (output_width == 0 && output_height == 0) {
665 output_width = input->get_width();
666 output_height = input->get_height();
667 } else if (output_width != input->get_width() ||
668 output_height != input->get_height()) {
669 all_inputs_same_size = false;
673 if (all_inputs_same_size) {
674 assert(output_width != 0);
675 assert(output_height != 0);
676 phase->virtual_output_width = phase->output_width = output_width;
677 phase->virtual_output_height = phase->output_height = output_height;
681 // If not, fit all the inputs into the current aspect, and select the largest one.
684 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
685 Phase *input = phase->inputs[i];
686 assert(input->output_width != 0);
687 assert(input->output_height != 0);
688 size_rectangle_to_fit(input->output_width, input->output_height, &output_width, &output_height);
690 for (unsigned i = 0; i < phase->effects.size(); ++i) {
691 Effect *effect = phase->effects[i]->effect;
692 if (effect->num_inputs() != 0) {
696 Input *input = static_cast<Input *>(effect);
697 size_rectangle_to_fit(input->get_width(), input->get_height(), &output_width, &output_height);
699 assert(output_width != 0);
700 assert(output_height != 0);
701 phase->virtual_output_width = phase->output_width = output_width;
702 phase->virtual_output_height = phase->output_height = output_height;
705 void EffectChain::sort_all_nodes_topologically()
707 nodes = topological_sort(nodes);
710 vector<Node *> EffectChain::topological_sort(const vector<Node *> &nodes)
712 set<Node *> nodes_left_to_visit(nodes.begin(), nodes.end());
713 vector<Node *> sorted_list;
714 for (unsigned i = 0; i < nodes.size(); ++i) {
715 topological_sort_visit_node(nodes[i], &nodes_left_to_visit, &sorted_list);
717 reverse(sorted_list.begin(), sorted_list.end());
721 void EffectChain::topological_sort_visit_node(Node *node, set<Node *> *nodes_left_to_visit, vector<Node *> *sorted_list)
723 if (nodes_left_to_visit->count(node) == 0) {
726 nodes_left_to_visit->erase(node);
727 for (unsigned i = 0; i < node->outgoing_links.size(); ++i) {
728 topological_sort_visit_node(node->outgoing_links[i], nodes_left_to_visit, sorted_list);
730 sorted_list->push_back(node);
733 void EffectChain::find_color_spaces_for_inputs()
735 for (unsigned i = 0; i < nodes.size(); ++i) {
736 Node *node = nodes[i];
737 if (node->disabled) {
740 if (node->incoming_links.size() == 0) {
741 Input *input = static_cast<Input *>(node->effect);
742 node->output_color_space = input->get_color_space();
743 node->output_gamma_curve = input->get_gamma_curve();
745 Effect::AlphaHandling alpha_handling = input->alpha_handling();
746 switch (alpha_handling) {
747 case Effect::OUTPUT_BLANK_ALPHA:
748 node->output_alpha_type = ALPHA_BLANK;
750 case Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA:
751 node->output_alpha_type = ALPHA_PREMULTIPLIED;
753 case Effect::OUTPUT_POSTMULTIPLIED_ALPHA:
754 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
756 case Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK:
757 case Effect::DONT_CARE_ALPHA_TYPE:
762 if (node->output_alpha_type == ALPHA_PREMULTIPLIED) {
763 assert(node->output_gamma_curve == GAMMA_LINEAR);
769 // Propagate gamma and color space information as far as we can in the graph.
770 // The rules are simple: Anything where all the inputs agree, get that as
771 // output as well. Anything else keeps having *_INVALID.
772 void EffectChain::propagate_gamma_and_color_space()
774 // We depend on going through the nodes in order.
775 sort_all_nodes_topologically();
777 for (unsigned i = 0; i < nodes.size(); ++i) {
778 Node *node = nodes[i];
779 if (node->disabled) {
782 assert(node->incoming_links.size() == node->effect->num_inputs());
783 if (node->incoming_links.size() == 0) {
784 assert(node->output_color_space != COLORSPACE_INVALID);
785 assert(node->output_gamma_curve != GAMMA_INVALID);
789 Colorspace color_space = node->incoming_links[0]->output_color_space;
790 GammaCurve gamma_curve = node->incoming_links[0]->output_gamma_curve;
791 for (unsigned j = 1; j < node->incoming_links.size(); ++j) {
792 if (node->incoming_links[j]->output_color_space != color_space) {
793 color_space = COLORSPACE_INVALID;
795 if (node->incoming_links[j]->output_gamma_curve != gamma_curve) {
796 gamma_curve = GAMMA_INVALID;
800 // The conversion effects already have their outputs set correctly,
801 // so leave them alone.
802 if (node->effect->effect_type_id() != "ColorspaceConversionEffect") {
803 node->output_color_space = color_space;
805 if (node->effect->effect_type_id() != "GammaCompressionEffect" &&
806 node->effect->effect_type_id() != "GammaExpansionEffect") {
807 node->output_gamma_curve = gamma_curve;
812 // Propagate alpha information as far as we can in the graph.
813 // Similar to propagate_gamma_and_color_space().
814 void EffectChain::propagate_alpha()
816 // We depend on going through the nodes in order.
817 sort_all_nodes_topologically();
819 for (unsigned i = 0; i < nodes.size(); ++i) {
820 Node *node = nodes[i];
821 if (node->disabled) {
824 assert(node->incoming_links.size() == node->effect->num_inputs());
825 if (node->incoming_links.size() == 0) {
826 assert(node->output_alpha_type != ALPHA_INVALID);
830 // The alpha multiplication/division effects are special cases.
831 if (node->effect->effect_type_id() == "AlphaMultiplicationEffect") {
832 assert(node->incoming_links.size() == 1);
833 assert(node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED);
834 node->output_alpha_type = ALPHA_PREMULTIPLIED;
837 if (node->effect->effect_type_id() == "AlphaDivisionEffect") {
838 assert(node->incoming_links.size() == 1);
839 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
840 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
844 // GammaCompressionEffect and GammaExpansionEffect are also a special case,
845 // because they are the only one that _need_ postmultiplied alpha.
846 if (node->effect->effect_type_id() == "GammaCompressionEffect" ||
847 node->effect->effect_type_id() == "GammaExpansionEffect") {
848 assert(node->incoming_links.size() == 1);
849 if (node->incoming_links[0]->output_alpha_type == ALPHA_BLANK) {
850 node->output_alpha_type = ALPHA_BLANK;
851 } else if (node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED) {
852 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
854 node->output_alpha_type = ALPHA_INVALID;
859 // Only inputs can have unconditional alpha output (OUTPUT_BLANK_ALPHA
860 // or OUTPUT_POSTMULTIPLIED_ALPHA), and they have already been
861 // taken care of above. Rationale: Even if you could imagine
862 // e.g. an effect that took in an image and set alpha=1.0
863 // unconditionally, it wouldn't make any sense to have it as
864 // e.g. OUTPUT_BLANK_ALPHA, since it wouldn't know whether it
865 // got its input pre- or postmultiplied, so it wouldn't know
866 // whether to divide away the old alpha or not.
867 Effect::AlphaHandling alpha_handling = node->effect->alpha_handling();
868 assert(alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
869 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK ||
870 alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
872 // If the node has multiple inputs, check that they are all valid and
874 bool any_invalid = false;
875 bool any_premultiplied = false;
876 bool any_postmultiplied = false;
878 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
879 switch (node->incoming_links[j]->output_alpha_type) {
884 // Blank is good as both pre- and postmultiplied alpha,
885 // so just ignore it.
887 case ALPHA_PREMULTIPLIED:
888 any_premultiplied = true;
890 case ALPHA_POSTMULTIPLIED:
891 any_postmultiplied = true;
899 node->output_alpha_type = ALPHA_INVALID;
903 // Inputs must be of the same type.
904 if (any_premultiplied && any_postmultiplied) {
905 node->output_alpha_type = ALPHA_INVALID;
909 if (alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
910 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
911 // If the effect has asked for premultiplied alpha, check that it has got it.
912 if (any_postmultiplied) {
913 node->output_alpha_type = ALPHA_INVALID;
914 } else if (!any_premultiplied &&
915 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
916 // Blank input alpha, and the effect preserves blank alpha.
917 node->output_alpha_type = ALPHA_BLANK;
919 node->output_alpha_type = ALPHA_PREMULTIPLIED;
922 // OK, all inputs are the same, and this effect is not going
924 assert(alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
925 if (any_premultiplied) {
926 node->output_alpha_type = ALPHA_PREMULTIPLIED;
927 } else if (any_postmultiplied) {
928 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
930 node->output_alpha_type = ALPHA_BLANK;
936 bool EffectChain::node_needs_colorspace_fix(Node *node)
938 if (node->disabled) {
941 if (node->effect->num_inputs() == 0) {
945 // propagate_gamma_and_color_space() has already set our output
946 // to COLORSPACE_INVALID if the inputs differ, so we can rely on that.
947 if (node->output_color_space == COLORSPACE_INVALID) {
950 return (node->effect->needs_srgb_primaries() && node->output_color_space != COLORSPACE_sRGB);
953 // Fix up color spaces so that there are no COLORSPACE_INVALID nodes left in
954 // the graph. Our strategy is not always optimal, but quite simple:
955 // Find an effect that's as early as possible where the inputs are of
956 // unacceptable colorspaces (that is, either different, or, if the effect only
957 // wants sRGB, not sRGB.) Add appropriate conversions on all its inputs,
958 // propagate the information anew, and repeat until there are no more such
960 void EffectChain::fix_internal_color_spaces()
962 unsigned colorspace_propagation_pass = 0;
966 for (unsigned i = 0; i < nodes.size(); ++i) {
967 Node *node = nodes[i];
968 if (!node_needs_colorspace_fix(node)) {
972 // Go through each input that is not sRGB, and insert
973 // a colorspace conversion after it.
974 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
975 Node *input = node->incoming_links[j];
976 assert(input->output_color_space != COLORSPACE_INVALID);
977 if (input->output_color_space == COLORSPACE_sRGB) {
980 Node *conversion = add_node(new ColorspaceConversionEffect());
981 CHECK(conversion->effect->set_int("source_space", input->output_color_space));
982 CHECK(conversion->effect->set_int("destination_space", COLORSPACE_sRGB));
983 conversion->output_color_space = COLORSPACE_sRGB;
984 replace_sender(input, conversion);
985 connect_nodes(input, conversion);
988 // Re-sort topologically, and propagate the new information.
989 propagate_gamma_and_color_space();
996 sprintf(filename, "step5-colorspacefix-iter%u.dot", ++colorspace_propagation_pass);
997 output_dot(filename);
998 assert(colorspace_propagation_pass < 100);
1001 for (unsigned i = 0; i < nodes.size(); ++i) {
1002 Node *node = nodes[i];
1003 if (node->disabled) {
1006 assert(node->output_color_space != COLORSPACE_INVALID);
1010 bool EffectChain::node_needs_alpha_fix(Node *node)
1012 if (node->disabled) {
1016 // propagate_alpha() has already set our output to ALPHA_INVALID if the
1017 // inputs differ or we are otherwise in mismatch, so we can rely on that.
1018 return (node->output_alpha_type == ALPHA_INVALID);
1021 // Fix up alpha so that there are no ALPHA_INVALID nodes left in
1022 // the graph. Similar to fix_internal_color_spaces().
1023 void EffectChain::fix_internal_alpha(unsigned step)
1025 unsigned alpha_propagation_pass = 0;
1029 for (unsigned i = 0; i < nodes.size(); ++i) {
1030 Node *node = nodes[i];
1031 if (!node_needs_alpha_fix(node)) {
1035 // If we need to fix up GammaExpansionEffect, then clearly something
1036 // is wrong, since the combination of premultiplied alpha and nonlinear inputs
1038 assert(node->effect->effect_type_id() != "GammaExpansionEffect");
1040 AlphaType desired_type = ALPHA_PREMULTIPLIED;
1042 // GammaCompressionEffect is special; it needs postmultiplied alpha.
1043 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1044 assert(node->incoming_links.size() == 1);
1045 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
1046 desired_type = ALPHA_POSTMULTIPLIED;
1049 // Go through each input that is not premultiplied alpha, and insert
1050 // a conversion before it.
1051 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1052 Node *input = node->incoming_links[j];
1053 assert(input->output_alpha_type != ALPHA_INVALID);
1054 if (input->output_alpha_type == desired_type ||
1055 input->output_alpha_type == ALPHA_BLANK) {
1059 if (desired_type == ALPHA_PREMULTIPLIED) {
1060 conversion = add_node(new AlphaMultiplicationEffect());
1062 conversion = add_node(new AlphaDivisionEffect());
1064 conversion->output_alpha_type = desired_type;
1065 replace_sender(input, conversion);
1066 connect_nodes(input, conversion);
1069 // Re-sort topologically, and propagate the new information.
1070 propagate_gamma_and_color_space();
1078 sprintf(filename, "step%u-alphafix-iter%u.dot", step, ++alpha_propagation_pass);
1079 output_dot(filename);
1080 assert(alpha_propagation_pass < 100);
1081 } while (found_any);
1083 for (unsigned i = 0; i < nodes.size(); ++i) {
1084 Node *node = nodes[i];
1085 if (node->disabled) {
1088 assert(node->output_alpha_type != ALPHA_INVALID);
1092 // Make so that the output is in the desired color space.
1093 void EffectChain::fix_output_color_space()
1095 Node *output = find_output_node();
1096 if (output->output_color_space != output_format.color_space) {
1097 Node *conversion = add_node(new ColorspaceConversionEffect());
1098 CHECK(conversion->effect->set_int("source_space", output->output_color_space));
1099 CHECK(conversion->effect->set_int("destination_space", output_format.color_space));
1100 conversion->output_color_space = output_format.color_space;
1101 connect_nodes(output, conversion);
1103 propagate_gamma_and_color_space();
1107 // Make so that the output is in the desired pre-/postmultiplication alpha state.
1108 void EffectChain::fix_output_alpha()
1110 Node *output = find_output_node();
1111 assert(output->output_alpha_type != ALPHA_INVALID);
1112 if (output->output_alpha_type == ALPHA_BLANK) {
1113 // No alpha output, so we don't care.
1116 if (output->output_alpha_type == ALPHA_PREMULTIPLIED &&
1117 output_alpha_format == OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED) {
1118 Node *conversion = add_node(new AlphaDivisionEffect());
1119 connect_nodes(output, conversion);
1121 propagate_gamma_and_color_space();
1123 if (output->output_alpha_type == ALPHA_POSTMULTIPLIED &&
1124 output_alpha_format == OUTPUT_ALPHA_FORMAT_PREMULTIPLIED) {
1125 Node *conversion = add_node(new AlphaMultiplicationEffect());
1126 connect_nodes(output, conversion);
1128 propagate_gamma_and_color_space();
1132 bool EffectChain::node_needs_gamma_fix(Node *node)
1134 if (node->disabled) {
1138 // Small hack since the output is not an explicit node:
1139 // If we are the last node and our output is in the wrong
1140 // space compared to EffectChain's output, we need to fix it.
1141 // This will only take us to linear, but fix_output_gamma()
1142 // will come and take us to the desired output gamma
1145 // This needs to be before everything else, since it could
1146 // even apply to inputs (if they are the only effect).
1147 if (node->outgoing_links.empty() &&
1148 node->output_gamma_curve != output_format.gamma_curve &&
1149 node->output_gamma_curve != GAMMA_LINEAR) {
1153 if (node->effect->num_inputs() == 0) {
1157 // propagate_gamma_and_color_space() has already set our output
1158 // to GAMMA_INVALID if the inputs differ, so we can rely on that,
1159 // except for GammaCompressionEffect.
1160 if (node->output_gamma_curve == GAMMA_INVALID) {
1163 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1164 assert(node->incoming_links.size() == 1);
1165 return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
1168 return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
1171 // Very similar to fix_internal_color_spaces(), but for gamma.
1172 // There is one difference, though; before we start adding conversion nodes,
1173 // we see if we can get anything out of asking the sources to deliver
1174 // linear gamma directly. fix_internal_gamma_by_asking_inputs()
1175 // does that part, while fix_internal_gamma_by_inserting_nodes()
1176 // inserts nodes as needed afterwards.
1177 void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
1179 unsigned gamma_propagation_pass = 0;
1183 for (unsigned i = 0; i < nodes.size(); ++i) {
1184 Node *node = nodes[i];
1185 if (!node_needs_gamma_fix(node)) {
1189 // See if all inputs can give us linear gamma. If not, leave it.
1190 vector<Node *> nonlinear_inputs;
1191 find_all_nonlinear_inputs(node, &nonlinear_inputs);
1192 assert(!nonlinear_inputs.empty());
1195 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1196 Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
1197 all_ok &= input->can_output_linear_gamma();
1204 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1205 CHECK(nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1));
1206 nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
1209 // Re-sort topologically, and propagate the new information.
1210 propagate_gamma_and_color_space();
1217 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1218 output_dot(filename);
1219 assert(gamma_propagation_pass < 100);
1220 } while (found_any);
1223 void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
1225 unsigned gamma_propagation_pass = 0;
1229 for (unsigned i = 0; i < nodes.size(); ++i) {
1230 Node *node = nodes[i];
1231 if (!node_needs_gamma_fix(node)) {
1235 // Special case: We could be an input and still be asked to
1236 // fix our gamma; if so, we should be the only node
1237 // (as node_needs_gamma_fix() would only return true in
1238 // for an input in that case). That means we should insert
1239 // a conversion node _after_ ourselves.
1240 if (node->incoming_links.empty()) {
1241 assert(node->outgoing_links.empty());
1242 Node *conversion = add_node(new GammaExpansionEffect());
1243 CHECK(conversion->effect->set_int("source_curve", node->output_gamma_curve));
1244 conversion->output_gamma_curve = GAMMA_LINEAR;
1245 connect_nodes(node, conversion);
1248 // If not, go through each input that is not linear gamma,
1249 // and insert a gamma conversion after it.
1250 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1251 Node *input = node->incoming_links[j];
1252 assert(input->output_gamma_curve != GAMMA_INVALID);
1253 if (input->output_gamma_curve == GAMMA_LINEAR) {
1256 Node *conversion = add_node(new GammaExpansionEffect());
1257 CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
1258 conversion->output_gamma_curve = GAMMA_LINEAR;
1259 replace_sender(input, conversion);
1260 connect_nodes(input, conversion);
1263 // Re-sort topologically, and propagate the new information.
1265 propagate_gamma_and_color_space();
1272 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1273 output_dot(filename);
1274 assert(gamma_propagation_pass < 100);
1275 } while (found_any);
1277 for (unsigned i = 0; i < nodes.size(); ++i) {
1278 Node *node = nodes[i];
1279 if (node->disabled) {
1282 assert(node->output_gamma_curve != GAMMA_INVALID);
1286 // Make so that the output is in the desired gamma.
1287 // Note that this assumes linear input gamma, so it might create the need
1288 // for another pass of fix_internal_gamma().
1289 void EffectChain::fix_output_gamma()
1291 Node *output = find_output_node();
1292 if (output->output_gamma_curve != output_format.gamma_curve) {
1293 Node *conversion = add_node(new GammaCompressionEffect());
1294 CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
1295 conversion->output_gamma_curve = output_format.gamma_curve;
1296 connect_nodes(output, conversion);
1300 // If the user has requested dither, add a DitherEffect right at the end
1301 // (after GammaCompressionEffect etc.). This needs to be done after everything else,
1302 // since dither is about the only effect that can _not_ be done in linear space.
1303 void EffectChain::add_dither_if_needed()
1305 if (num_dither_bits == 0) {
1308 Node *output = find_output_node();
1309 Node *dither = add_node(new DitherEffect());
1310 CHECK(dither->effect->set_int("num_bits", num_dither_bits));
1311 connect_nodes(output, dither);
1313 dither_effect = dither->effect;
1316 // Find the output node. This is, simply, one that has no outgoing links.
1317 // If there are multiple ones, the graph is malformed (we do not support
1318 // multiple outputs right now).
1319 Node *EffectChain::find_output_node()
1321 vector<Node *> output_nodes;
1322 for (unsigned i = 0; i < nodes.size(); ++i) {
1323 Node *node = nodes[i];
1324 if (node->disabled) {
1327 if (node->outgoing_links.empty()) {
1328 output_nodes.push_back(node);
1331 assert(output_nodes.size() == 1);
1332 return output_nodes[0];
1335 void EffectChain::finalize()
1337 // Save the current locale, and set it to C, so that we can output decimal
1338 // numbers with printf and be sure to get them in the format mandated by GLSL.
1339 char *saved_locale = setlocale(LC_NUMERIC, "C");
1341 // Output the graph as it is before we do any conversions on it.
1342 output_dot("step0-start.dot");
1344 // Give each effect in turn a chance to rewrite its own part of the graph.
1345 // Note that if more effects are added as part of this, they will be
1346 // picked up as part of the same for loop, since they are added at the end.
1347 for (unsigned i = 0; i < nodes.size(); ++i) {
1348 nodes[i]->effect->rewrite_graph(this, nodes[i]);
1350 output_dot("step1-rewritten.dot");
1352 find_color_spaces_for_inputs();
1353 output_dot("step2-input-colorspace.dot");
1356 output_dot("step3-propagated-alpha.dot");
1358 propagate_gamma_and_color_space();
1359 output_dot("step4-propagated-all.dot");
1361 fix_internal_color_spaces();
1362 fix_internal_alpha(6);
1363 fix_output_color_space();
1364 output_dot("step7-output-colorspacefix.dot");
1366 output_dot("step8-output-alphafix.dot");
1368 // Note that we need to fix gamma after colorspace conversion,
1369 // because colorspace conversions might create needs for gamma conversions.
1370 // Also, we need to run an extra pass of fix_internal_gamma() after
1371 // fixing the output gamma, as we only have conversions to/from linear,
1372 // and fix_internal_alpha() since GammaCompressionEffect needs
1373 // postmultiplied input.
1374 fix_internal_gamma_by_asking_inputs(9);
1375 fix_internal_gamma_by_inserting_nodes(10);
1377 output_dot("step11-output-gammafix.dot");
1379 output_dot("step12-output-alpha-propagated.dot");
1380 fix_internal_alpha(13);
1381 output_dot("step14-output-alpha-fixed.dot");
1382 fix_internal_gamma_by_asking_inputs(15);
1383 fix_internal_gamma_by_inserting_nodes(16);
1385 output_dot("step17-before-dither.dot");
1387 add_dither_if_needed();
1389 output_dot("step18-final.dot");
1391 // Construct all needed GLSL programs, starting at the output.
1392 // We need to keep track of which effects have already been computed,
1393 // as an effect with multiple users could otherwise be calculated
1395 map<Node *, Phase *> completed_effects;
1396 construct_phase(find_output_node(), &completed_effects);
1398 output_dot("step19-split-to-phases.dot");
1400 assert(phases[0]->inputs.empty());
1403 setlocale(LC_NUMERIC, saved_locale);
1406 void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height)
1410 // Save original viewport.
1411 GLuint x = 0, y = 0;
1413 void *context = get_gl_context_identifier();
1415 if (width == 0 && height == 0) {
1417 glGetIntegerv(GL_VIEWPORT, viewport);
1420 width = viewport[2];
1421 height = viewport[3];
1425 glDisable(GL_BLEND);
1427 glDisable(GL_DEPTH_TEST);
1429 glDepthMask(GL_FALSE);
1432 if (phases.size() > 1) {
1433 if (fbos.count(context) == 0) {
1434 glGenFramebuffers(1, &fbo);
1436 fbos.insert(make_pair(context, fbo));
1438 fbo = fbos[context];
1440 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1444 set<Phase *> generated_mipmaps;
1446 // We choose the simplest option of having one texture per output,
1447 // since otherwise this turns into an (albeit simple) register allocation problem.
1448 map<Phase *, GLuint> output_textures;
1450 for (unsigned phase = 0; phase < phases.size(); ++phase) {
1451 // Find a texture for this phase.
1452 inform_input_sizes(phases[phase]);
1453 if (phase != phases.size() - 1) {
1454 find_output_size(phases[phase]);
1456 GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height);
1457 output_textures.insert(make_pair(phases[phase], tex_num));
1460 const GLuint glsl_program_num = phases[phase]->glsl_program_num;
1462 glUseProgram(glsl_program_num);
1465 // Set up RTT inputs for this phase.
1466 for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
1467 glActiveTexture(GL_TEXTURE0 + sampler);
1468 Phase *input = phases[phase]->inputs[sampler];
1469 input->output_node->bound_sampler_num = sampler;
1470 glBindTexture(GL_TEXTURE_2D, output_textures[input]);
1472 if (phases[phase]->input_needs_mipmaps) {
1473 if (generated_mipmaps.count(input) == 0) {
1474 glGenerateMipmap(GL_TEXTURE_2D);
1476 generated_mipmaps.insert(input);
1478 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1484 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1486 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1489 string texture_name = string("tex_") + phases[phase]->effect_ids[input->output_node];
1490 glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler);
1494 // And now the output.
1495 if (phase == phases.size() - 1) {
1496 // Last phase goes to the output the user specified.
1497 glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
1499 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
1500 assert(status == GL_FRAMEBUFFER_COMPLETE);
1501 glViewport(x, y, width, height);
1502 if (dither_effect != NULL) {
1503 CHECK(dither_effect->set_int("output_width", width));
1504 CHECK(dither_effect->set_int("output_height", height));
1507 glFramebufferTexture2D(
1509 GL_COLOR_ATTACHMENT0,
1511 output_textures[phases[phase]],
1514 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
1515 assert(status == GL_FRAMEBUFFER_COMPLETE);
1516 glViewport(0, 0, phases[phase]->output_width, phases[phase]->output_height);
1519 // Give the required parameters to all the effects.
1520 unsigned sampler_num = phases[phase]->inputs.size();
1521 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1522 Node *node = phases[phase]->effects[i];
1523 unsigned old_sampler_num = sampler_num;
1524 node->effect->set_gl_state(glsl_program_num, phases[phase]->effect_ids[node], &sampler_num);
1527 if (node->effect->is_single_texture()) {
1528 assert(sampler_num - old_sampler_num == 1);
1529 node->bound_sampler_num = old_sampler_num;
1531 node->bound_sampler_num = -1;
1536 float vertices[] = {
1544 glGenVertexArrays(1, &vao);
1546 glBindVertexArray(vao);
1549 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
1550 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same as vertices.
1552 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1555 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
1556 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
1561 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1562 Node *node = phases[phase]->effects[i];
1563 node->effect->clear_gl_state();
1566 glDeleteVertexArrays(1, &vao);
1570 for (map<Phase *, GLuint>::const_iterator texture_it = output_textures.begin();
1571 texture_it != output_textures.end();
1573 resource_pool->release_2d_texture(texture_it->second);
1576 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1580 } // namespace movit