1 #define GL_GLEXT_PROTOTYPES 1
16 #if defined(__APPLE__)
20 #include "alpha_division_effect.h"
21 #include "alpha_multiplication_effect.h"
22 #include "colorspace_conversion_effect.h"
23 #include "dither_effect.h"
25 #include "effect_chain.h"
26 #include "gamma_compression_effect.h"
27 #include "gamma_expansion_effect.h"
30 #include "resource_pool.h"
37 EffectChain::EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool)
38 : aspect_nom(aspect_nom),
39 aspect_denom(aspect_denom),
43 resource_pool(resource_pool) {
44 if (resource_pool == NULL) {
45 this->resource_pool = new ResourcePool();
46 owns_resource_pool = true;
48 owns_resource_pool = false;
52 EffectChain::~EffectChain()
54 for (unsigned i = 0; i < nodes.size(); ++i) {
55 delete nodes[i]->effect;
58 for (unsigned i = 0; i < phases.size(); ++i) {
59 resource_pool->release_glsl_program(phases[i]->glsl_program_num);
62 if (owns_resource_pool) {
67 Input *EffectChain::add_input(Input *input)
70 inputs.push_back(input);
75 void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
78 output_format = format;
79 output_alpha_format = alpha_format;
82 Node *EffectChain::add_node(Effect *effect)
84 for (unsigned i = 0; i < nodes.size(); ++i) {
85 assert(nodes[i]->effect != effect);
88 Node *node = new Node;
89 node->effect = effect;
90 node->disabled = false;
91 node->output_color_space = COLORSPACE_INVALID;
92 node->output_gamma_curve = GAMMA_INVALID;
93 node->output_alpha_type = ALPHA_INVALID;
95 nodes.push_back(node);
96 node_map[effect] = node;
97 effect->inform_added(this);
101 void EffectChain::connect_nodes(Node *sender, Node *receiver)
103 sender->outgoing_links.push_back(receiver);
104 receiver->incoming_links.push_back(sender);
107 void EffectChain::replace_receiver(Node *old_receiver, Node *new_receiver)
109 new_receiver->incoming_links = old_receiver->incoming_links;
110 old_receiver->incoming_links.clear();
112 for (unsigned i = 0; i < new_receiver->incoming_links.size(); ++i) {
113 Node *sender = new_receiver->incoming_links[i];
114 for (unsigned j = 0; j < sender->outgoing_links.size(); ++j) {
115 if (sender->outgoing_links[j] == old_receiver) {
116 sender->outgoing_links[j] = new_receiver;
122 void EffectChain::replace_sender(Node *old_sender, Node *new_sender)
124 new_sender->outgoing_links = old_sender->outgoing_links;
125 old_sender->outgoing_links.clear();
127 for (unsigned i = 0; i < new_sender->outgoing_links.size(); ++i) {
128 Node *receiver = new_sender->outgoing_links[i];
129 for (unsigned j = 0; j < receiver->incoming_links.size(); ++j) {
130 if (receiver->incoming_links[j] == old_sender) {
131 receiver->incoming_links[j] = new_sender;
137 void EffectChain::insert_node_between(Node *sender, Node *middle, Node *receiver)
139 for (unsigned i = 0; i < sender->outgoing_links.size(); ++i) {
140 if (sender->outgoing_links[i] == receiver) {
141 sender->outgoing_links[i] = middle;
142 middle->incoming_links.push_back(sender);
145 for (unsigned i = 0; i < receiver->incoming_links.size(); ++i) {
146 if (receiver->incoming_links[i] == sender) {
147 receiver->incoming_links[i] = middle;
148 middle->outgoing_links.push_back(receiver);
152 assert(middle->incoming_links.size() == middle->effect->num_inputs());
155 GLenum EffectChain::get_input_sampler(Node *node, unsigned input_num) const
157 assert(node->effect->needs_texture_bounce());
158 assert(input_num < node->incoming_links.size());
159 assert(node->incoming_links[input_num]->bound_sampler_num >= 0);
160 assert(node->incoming_links[input_num]->bound_sampler_num < 8);
161 return GL_TEXTURE0 + node->incoming_links[input_num]->bound_sampler_num;
164 void EffectChain::find_all_nonlinear_inputs(Node *node, vector<Node *> *nonlinear_inputs)
166 if (node->output_gamma_curve == GAMMA_LINEAR &&
167 node->effect->effect_type_id() != "GammaCompressionEffect") {
170 if (node->effect->num_inputs() == 0) {
171 nonlinear_inputs->push_back(node);
173 assert(node->effect->num_inputs() == node->incoming_links.size());
174 for (unsigned i = 0; i < node->incoming_links.size(); ++i) {
175 find_all_nonlinear_inputs(node->incoming_links[i], nonlinear_inputs);
180 Effect *EffectChain::add_effect(Effect *effect, const vector<Effect *> &inputs)
183 assert(inputs.size() == effect->num_inputs());
184 Node *node = add_node(effect);
185 for (unsigned i = 0; i < inputs.size(); ++i) {
186 assert(node_map.count(inputs[i]) != 0);
187 connect_nodes(node_map[inputs[i]], node);
192 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
193 string replace_prefix(const string &text, const string &prefix)
198 while (start < text.size()) {
199 size_t pos = text.find("PREFIX(", start);
200 if (pos == string::npos) {
201 output.append(text.substr(start, string::npos));
205 output.append(text.substr(start, pos - start));
206 output.append(prefix);
209 pos += strlen("PREFIX(");
211 // Output stuff until we find the matching ), which we then eat.
213 size_t end_arg_pos = pos;
214 while (end_arg_pos < text.size()) {
215 if (text[end_arg_pos] == '(') {
217 } else if (text[end_arg_pos] == ')') {
225 output.append(text.substr(pos, end_arg_pos - pos));
233 void EffectChain::compile_glsl_program(Phase *phase)
235 string frag_shader = read_version_dependent_file("header", "frag");
237 // Create functions for all the texture inputs that we need.
238 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
239 Node *input = phase->inputs[i]->output_node;
241 sprintf(effect_id, "in%u", i);
242 phase->effect_ids.insert(make_pair(input, effect_id));
244 frag_shader += string("uniform sampler2D tex_") + effect_id + ";\n";
245 frag_shader += string("vec4 ") + effect_id + "(vec2 tc) {\n";
246 frag_shader += "\treturn tex2D(tex_" + string(effect_id) + ", tc);\n";
247 frag_shader += "}\n";
251 for (unsigned i = 0; i < phase->effects.size(); ++i) {
252 Node *node = phase->effects[i];
254 sprintf(effect_id, "eff%u", i);
255 phase->effect_ids.insert(make_pair(node, effect_id));
257 if (node->incoming_links.size() == 1) {
258 frag_shader += string("#define INPUT ") + phase->effect_ids[node->incoming_links[0]] + "\n";
260 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
262 sprintf(buf, "#define INPUT%d %s\n", j + 1, phase->effect_ids[node->incoming_links[j]].c_str());
268 frag_shader += string("#define FUNCNAME ") + effect_id + "\n";
269 frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), effect_id);
270 frag_shader += replace_prefix(node->effect->output_fragment_shader(), effect_id);
271 frag_shader += "#undef PREFIX\n";
272 frag_shader += "#undef FUNCNAME\n";
273 if (node->incoming_links.size() == 1) {
274 frag_shader += "#undef INPUT\n";
276 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
278 sprintf(buf, "#undef INPUT%d\n", j + 1);
284 frag_shader += string("#define INPUT ") + phase->effect_ids[phase->effects.back()] + "\n";
285 frag_shader.append(read_version_dependent_file("footer", "frag"));
287 string vert_shader = read_version_dependent_file("vs", "vert");
288 phase->glsl_program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
291 // Construct GLSL programs, starting at the given effect and following
292 // the chain from there. We end a program every time we come to an effect
293 // marked as "needs texture bounce", one that is used by multiple other
294 // effects, every time an effect wants to change the output size,
295 // and of course at the end.
297 // We follow a quite simple depth-first search from the output, although
298 // without recursing explicitly within each phase.
299 Phase *EffectChain::construct_phase(Node *output, map<Node *, Phase *> *completed_effects)
301 if (completed_effects->count(output)) {
302 return (*completed_effects)[output];
305 Phase *phase = new Phase;
306 phase->output_node = output;
308 // Effects that we have yet to calculate, but that we know should
309 // be in the current phase.
310 stack<Node *> effects_todo_this_phase;
311 effects_todo_this_phase.push(output);
313 while (!effects_todo_this_phase.empty()) {
314 Node *node = effects_todo_this_phase.top();
315 effects_todo_this_phase.pop();
317 // This should currently only happen for effects that are inputs
318 // (either true inputs or phase outputs). We special-case inputs,
319 // and then deduplicate phase outputs below.
320 if (node->effect->num_inputs() == 0) {
321 if (find(phase->effects.begin(), phase->effects.end(), node) != phase->effects.end()) {
325 assert(completed_effects->count(node) == 0);
328 phase->effects.push_back(node);
330 // Find all the dependencies of this effect, and add them to the stack.
331 vector<Node *> deps = node->incoming_links;
332 assert(node->effect->num_inputs() == deps.size());
333 for (unsigned i = 0; i < deps.size(); ++i) {
334 bool start_new_phase = false;
336 if (node->effect->needs_texture_bounce() &&
337 !deps[i]->effect->is_single_texture()) {
338 start_new_phase = true;
341 if (deps[i]->outgoing_links.size() > 1) {
342 if (!deps[i]->effect->is_single_texture()) {
343 // More than one effect uses this as the input,
344 // and it is not a texture itself.
345 // The easiest thing to do (and probably also the safest
346 // performance-wise in most cases) is to bounce it to a texture
347 // and then let the next passes read from that.
348 start_new_phase = true;
350 assert(deps[i]->effect->num_inputs() == 0);
352 // For textures, we try to be slightly more clever;
353 // if none of our outputs need a bounce, we don't bounce
354 // but instead simply use the effect many times.
356 // Strictly speaking, we could bounce it for some outputs
357 // and use it directly for others, but the processing becomes
358 // somewhat simpler if the effect is only used in one such way.
359 for (unsigned j = 0; j < deps[i]->outgoing_links.size(); ++j) {
360 Node *rdep = deps[i]->outgoing_links[j];
361 start_new_phase |= rdep->effect->needs_texture_bounce();
366 if (deps[i]->effect->changes_output_size()) {
367 start_new_phase = true;
370 if (start_new_phase) {
371 phase->inputs.push_back(construct_phase(deps[i], completed_effects));
373 effects_todo_this_phase.push(deps[i]);
378 // No more effects to do this phase. Take all the ones we have,
379 // and create a GLSL program for it.
380 assert(!phase->effects.empty());
382 // Deduplicate the inputs.
383 sort(phase->inputs.begin(), phase->inputs.end());
384 phase->inputs.erase(unique(phase->inputs.begin(), phase->inputs.end()), phase->inputs.end());
386 // We added the effects from the output and back, but we need to output
387 // them in topological sort order in the shader.
388 phase->effects = topological_sort(phase->effects);
390 // Figure out if we need mipmaps or not, and if so, tell the inputs that.
391 phase->input_needs_mipmaps = false;
392 for (unsigned i = 0; i < phase->effects.size(); ++i) {
393 Node *node = phase->effects[i];
394 phase->input_needs_mipmaps |= node->effect->needs_mipmaps();
396 for (unsigned i = 0; i < phase->effects.size(); ++i) {
397 Node *node = phase->effects[i];
398 if (node->effect->num_inputs() == 0) {
399 CHECK(node->effect->set_int("needs_mipmaps", phase->input_needs_mipmaps));
403 // Actually make the shader for this phase.
404 compile_glsl_program(phase);
406 assert(completed_effects->count(output) == 0);
407 completed_effects->insert(make_pair(output, phase));
408 phases.push_back(phase);
412 void EffectChain::output_dot(const char *filename)
414 if (movit_debug_level != MOVIT_DEBUG_ON) {
418 FILE *fp = fopen(filename, "w");
424 fprintf(fp, "digraph G {\n");
425 fprintf(fp, " output [shape=box label=\"(output)\"];\n");
426 for (unsigned i = 0; i < nodes.size(); ++i) {
427 // Find out which phase this event belongs to.
428 vector<int> in_phases;
429 for (unsigned j = 0; j < phases.size(); ++j) {
430 const Phase* p = phases[j];
431 if (find(p->effects.begin(), p->effects.end(), nodes[i]) != p->effects.end()) {
432 in_phases.push_back(j);
436 if (in_phases.empty()) {
437 fprintf(fp, " n%ld [label=\"%s\"];\n", (long)nodes[i], nodes[i]->effect->effect_type_id().c_str());
438 } else if (in_phases.size() == 1) {
439 fprintf(fp, " n%ld [label=\"%s\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
440 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
441 (in_phases[0] % 8) + 1);
443 // If we had new enough Graphviz, style="wedged" would probably be ideal here.
445 fprintf(fp, " n%ld [label=\"%s [in multiple phases]\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
446 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
447 (in_phases[0] % 8) + 1);
450 char from_node_id[256];
451 snprintf(from_node_id, 256, "n%ld", (long)nodes[i]);
453 for (unsigned j = 0; j < nodes[i]->outgoing_links.size(); ++j) {
454 char to_node_id[256];
455 snprintf(to_node_id, 256, "n%ld", (long)nodes[i]->outgoing_links[j]);
457 vector<string> labels = get_labels_for_edge(nodes[i], nodes[i]->outgoing_links[j]);
458 output_dot_edge(fp, from_node_id, to_node_id, labels);
461 if (nodes[i]->outgoing_links.empty() && !nodes[i]->disabled) {
463 vector<string> labels = get_labels_for_edge(nodes[i], NULL);
464 output_dot_edge(fp, from_node_id, "output", labels);
472 vector<string> EffectChain::get_labels_for_edge(const Node *from, const Node *to)
474 vector<string> labels;
476 if (to != NULL && to->effect->needs_texture_bounce()) {
477 labels.push_back("needs_bounce");
479 if (from->effect->changes_output_size()) {
480 labels.push_back("resize");
483 switch (from->output_color_space) {
484 case COLORSPACE_INVALID:
485 labels.push_back("spc[invalid]");
487 case COLORSPACE_REC_601_525:
488 labels.push_back("spc[rec601-525]");
490 case COLORSPACE_REC_601_625:
491 labels.push_back("spc[rec601-625]");
497 switch (from->output_gamma_curve) {
499 labels.push_back("gamma[invalid]");
502 labels.push_back("gamma[sRGB]");
504 case GAMMA_REC_601: // and GAMMA_REC_709
505 labels.push_back("gamma[rec601/709]");
511 switch (from->output_alpha_type) {
513 labels.push_back("alpha[invalid]");
516 labels.push_back("alpha[blank]");
518 case ALPHA_POSTMULTIPLIED:
519 labels.push_back("alpha[postmult]");
528 void EffectChain::output_dot_edge(FILE *fp,
529 const string &from_node_id,
530 const string &to_node_id,
531 const vector<string> &labels)
533 if (labels.empty()) {
534 fprintf(fp, " %s -> %s;\n", from_node_id.c_str(), to_node_id.c_str());
536 string label = labels[0];
537 for (unsigned k = 1; k < labels.size(); ++k) {
538 label += ", " + labels[k];
540 fprintf(fp, " %s -> %s [label=\"%s\"];\n", from_node_id.c_str(), to_node_id.c_str(), label.c_str());
544 void EffectChain::size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height)
546 unsigned scaled_width, scaled_height;
548 if (float(width) * aspect_denom >= float(height) * aspect_nom) {
549 // Same aspect, or W/H > aspect (image is wider than the frame).
550 // In either case, keep width, and adjust height.
551 scaled_width = width;
552 scaled_height = lrintf(width * aspect_denom / aspect_nom);
554 // W/H < aspect (image is taller than the frame), so keep height,
556 scaled_width = lrintf(height * aspect_nom / aspect_denom);
557 scaled_height = height;
560 // We should be consistently larger or smaller then the existing choice,
561 // since we have the same aspect.
562 assert(!(scaled_width < *output_width && scaled_height > *output_height));
563 assert(!(scaled_height < *output_height && scaled_width > *output_width));
565 if (scaled_width >= *output_width && scaled_height >= *output_height) {
566 *output_width = scaled_width;
567 *output_height = scaled_height;
571 // Propagate input texture sizes throughout, and inform effects downstream.
572 // (Like a lot of other code, we depend on effects being in topological order.)
573 void EffectChain::inform_input_sizes(Phase *phase)
575 // All effects that have a defined size (inputs and RTT inputs)
576 // get that. Reset all others.
577 for (unsigned i = 0; i < phase->effects.size(); ++i) {
578 Node *node = phase->effects[i];
579 if (node->effect->num_inputs() == 0) {
580 Input *input = static_cast<Input *>(node->effect);
581 node->output_width = input->get_width();
582 node->output_height = input->get_height();
583 assert(node->output_width != 0);
584 assert(node->output_height != 0);
586 node->output_width = node->output_height = 0;
589 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
590 Phase *input = phase->inputs[i];
591 input->output_node->output_width = input->virtual_output_width;
592 input->output_node->output_height = input->virtual_output_height;
593 assert(input->output_node->output_width != 0);
594 assert(input->output_node->output_height != 0);
597 // Now propagate from the inputs towards the end, and inform as we go.
598 // The rules are simple:
600 // 1. Don't touch effects that already have given sizes (ie., inputs).
601 // 2. If all of your inputs have the same size, that will be your output size.
602 // 3. Otherwise, your output size is 0x0.
603 for (unsigned i = 0; i < phase->effects.size(); ++i) {
604 Node *node = phase->effects[i];
605 if (node->effect->num_inputs() == 0) {
608 unsigned this_output_width = 0;
609 unsigned this_output_height = 0;
610 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
611 Node *input = node->incoming_links[j];
612 node->effect->inform_input_size(j, input->output_width, input->output_height);
614 this_output_width = input->output_width;
615 this_output_height = input->output_height;
616 } else if (input->output_width != this_output_width || input->output_height != this_output_height) {
618 this_output_width = 0;
619 this_output_height = 0;
622 node->output_width = this_output_width;
623 node->output_height = this_output_height;
627 // Note: You should call inform_input_sizes() before this, as the last effect's
628 // desired output size might change based on the inputs.
629 void EffectChain::find_output_size(Phase *phase)
631 Node *output_node = phase->effects.back();
633 // If the last effect explicitly sets an output size, use that.
634 if (output_node->effect->changes_output_size()) {
635 output_node->effect->get_output_size(&phase->output_width, &phase->output_height,
636 &phase->virtual_output_width, &phase->virtual_output_height);
640 // If all effects have the same size, use that.
641 unsigned output_width = 0, output_height = 0;
642 bool all_inputs_same_size = true;
644 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
645 Phase *input = phase->inputs[i];
646 assert(input->output_width != 0);
647 assert(input->output_height != 0);
648 if (output_width == 0 && output_height == 0) {
649 output_width = input->virtual_output_width;
650 output_height = input->virtual_output_height;
651 } else if (output_width != input->virtual_output_width ||
652 output_height != input->virtual_output_height) {
653 all_inputs_same_size = false;
656 for (unsigned i = 0; i < phase->effects.size(); ++i) {
657 Effect *effect = phase->effects[i]->effect;
658 if (effect->num_inputs() != 0) {
662 Input *input = static_cast<Input *>(effect);
663 if (output_width == 0 && output_height == 0) {
664 output_width = input->get_width();
665 output_height = input->get_height();
666 } else if (output_width != input->get_width() ||
667 output_height != input->get_height()) {
668 all_inputs_same_size = false;
672 if (all_inputs_same_size) {
673 assert(output_width != 0);
674 assert(output_height != 0);
675 phase->virtual_output_width = phase->output_width = output_width;
676 phase->virtual_output_height = phase->output_height = output_height;
680 // If not, fit all the inputs into the current aspect, and select the largest one.
683 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
684 Phase *input = phase->inputs[i];
685 assert(input->output_width != 0);
686 assert(input->output_height != 0);
687 size_rectangle_to_fit(input->output_width, input->output_height, &output_width, &output_height);
689 for (unsigned i = 0; i < phase->effects.size(); ++i) {
690 Effect *effect = phase->effects[i]->effect;
691 if (effect->num_inputs() != 0) {
695 Input *input = static_cast<Input *>(effect);
696 size_rectangle_to_fit(input->get_width(), input->get_height(), &output_width, &output_height);
698 assert(output_width != 0);
699 assert(output_height != 0);
700 phase->virtual_output_width = phase->output_width = output_width;
701 phase->virtual_output_height = phase->output_height = output_height;
704 void EffectChain::sort_all_nodes_topologically()
706 nodes = topological_sort(nodes);
709 vector<Node *> EffectChain::topological_sort(const vector<Node *> &nodes)
711 set<Node *> nodes_left_to_visit(nodes.begin(), nodes.end());
712 vector<Node *> sorted_list;
713 for (unsigned i = 0; i < nodes.size(); ++i) {
714 topological_sort_visit_node(nodes[i], &nodes_left_to_visit, &sorted_list);
716 reverse(sorted_list.begin(), sorted_list.end());
720 void EffectChain::topological_sort_visit_node(Node *node, set<Node *> *nodes_left_to_visit, vector<Node *> *sorted_list)
722 if (nodes_left_to_visit->count(node) == 0) {
725 nodes_left_to_visit->erase(node);
726 for (unsigned i = 0; i < node->outgoing_links.size(); ++i) {
727 topological_sort_visit_node(node->outgoing_links[i], nodes_left_to_visit, sorted_list);
729 sorted_list->push_back(node);
732 void EffectChain::find_color_spaces_for_inputs()
734 for (unsigned i = 0; i < nodes.size(); ++i) {
735 Node *node = nodes[i];
736 if (node->disabled) {
739 if (node->incoming_links.size() == 0) {
740 Input *input = static_cast<Input *>(node->effect);
741 node->output_color_space = input->get_color_space();
742 node->output_gamma_curve = input->get_gamma_curve();
744 Effect::AlphaHandling alpha_handling = input->alpha_handling();
745 switch (alpha_handling) {
746 case Effect::OUTPUT_BLANK_ALPHA:
747 node->output_alpha_type = ALPHA_BLANK;
749 case Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA:
750 node->output_alpha_type = ALPHA_PREMULTIPLIED;
752 case Effect::OUTPUT_POSTMULTIPLIED_ALPHA:
753 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
755 case Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK:
756 case Effect::DONT_CARE_ALPHA_TYPE:
761 if (node->output_alpha_type == ALPHA_PREMULTIPLIED) {
762 assert(node->output_gamma_curve == GAMMA_LINEAR);
768 // Propagate gamma and color space information as far as we can in the graph.
769 // The rules are simple: Anything where all the inputs agree, get that as
770 // output as well. Anything else keeps having *_INVALID.
771 void EffectChain::propagate_gamma_and_color_space()
773 // We depend on going through the nodes in order.
774 sort_all_nodes_topologically();
776 for (unsigned i = 0; i < nodes.size(); ++i) {
777 Node *node = nodes[i];
778 if (node->disabled) {
781 assert(node->incoming_links.size() == node->effect->num_inputs());
782 if (node->incoming_links.size() == 0) {
783 assert(node->output_color_space != COLORSPACE_INVALID);
784 assert(node->output_gamma_curve != GAMMA_INVALID);
788 Colorspace color_space = node->incoming_links[0]->output_color_space;
789 GammaCurve gamma_curve = node->incoming_links[0]->output_gamma_curve;
790 for (unsigned j = 1; j < node->incoming_links.size(); ++j) {
791 if (node->incoming_links[j]->output_color_space != color_space) {
792 color_space = COLORSPACE_INVALID;
794 if (node->incoming_links[j]->output_gamma_curve != gamma_curve) {
795 gamma_curve = GAMMA_INVALID;
799 // The conversion effects already have their outputs set correctly,
800 // so leave them alone.
801 if (node->effect->effect_type_id() != "ColorspaceConversionEffect") {
802 node->output_color_space = color_space;
804 if (node->effect->effect_type_id() != "GammaCompressionEffect" &&
805 node->effect->effect_type_id() != "GammaExpansionEffect") {
806 node->output_gamma_curve = gamma_curve;
811 // Propagate alpha information as far as we can in the graph.
812 // Similar to propagate_gamma_and_color_space().
813 void EffectChain::propagate_alpha()
815 // We depend on going through the nodes in order.
816 sort_all_nodes_topologically();
818 for (unsigned i = 0; i < nodes.size(); ++i) {
819 Node *node = nodes[i];
820 if (node->disabled) {
823 assert(node->incoming_links.size() == node->effect->num_inputs());
824 if (node->incoming_links.size() == 0) {
825 assert(node->output_alpha_type != ALPHA_INVALID);
829 // The alpha multiplication/division effects are special cases.
830 if (node->effect->effect_type_id() == "AlphaMultiplicationEffect") {
831 assert(node->incoming_links.size() == 1);
832 assert(node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED);
833 node->output_alpha_type = ALPHA_PREMULTIPLIED;
836 if (node->effect->effect_type_id() == "AlphaDivisionEffect") {
837 assert(node->incoming_links.size() == 1);
838 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
839 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
843 // GammaCompressionEffect and GammaExpansionEffect are also a special case,
844 // because they are the only one that _need_ postmultiplied alpha.
845 if (node->effect->effect_type_id() == "GammaCompressionEffect" ||
846 node->effect->effect_type_id() == "GammaExpansionEffect") {
847 assert(node->incoming_links.size() == 1);
848 if (node->incoming_links[0]->output_alpha_type == ALPHA_BLANK) {
849 node->output_alpha_type = ALPHA_BLANK;
850 } else if (node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED) {
851 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
853 node->output_alpha_type = ALPHA_INVALID;
858 // Only inputs can have unconditional alpha output (OUTPUT_BLANK_ALPHA
859 // or OUTPUT_POSTMULTIPLIED_ALPHA), and they have already been
860 // taken care of above. Rationale: Even if you could imagine
861 // e.g. an effect that took in an image and set alpha=1.0
862 // unconditionally, it wouldn't make any sense to have it as
863 // e.g. OUTPUT_BLANK_ALPHA, since it wouldn't know whether it
864 // got its input pre- or postmultiplied, so it wouldn't know
865 // whether to divide away the old alpha or not.
866 Effect::AlphaHandling alpha_handling = node->effect->alpha_handling();
867 assert(alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
868 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK ||
869 alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
871 // If the node has multiple inputs, check that they are all valid and
873 bool any_invalid = false;
874 bool any_premultiplied = false;
875 bool any_postmultiplied = false;
877 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
878 switch (node->incoming_links[j]->output_alpha_type) {
883 // Blank is good as both pre- and postmultiplied alpha,
884 // so just ignore it.
886 case ALPHA_PREMULTIPLIED:
887 any_premultiplied = true;
889 case ALPHA_POSTMULTIPLIED:
890 any_postmultiplied = true;
898 node->output_alpha_type = ALPHA_INVALID;
902 // Inputs must be of the same type.
903 if (any_premultiplied && any_postmultiplied) {
904 node->output_alpha_type = ALPHA_INVALID;
908 if (alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
909 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
910 // If the effect has asked for premultiplied alpha, check that it has got it.
911 if (any_postmultiplied) {
912 node->output_alpha_type = ALPHA_INVALID;
913 } else if (!any_premultiplied &&
914 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
915 // Blank input alpha, and the effect preserves blank alpha.
916 node->output_alpha_type = ALPHA_BLANK;
918 node->output_alpha_type = ALPHA_PREMULTIPLIED;
921 // OK, all inputs are the same, and this effect is not going
923 assert(alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
924 if (any_premultiplied) {
925 node->output_alpha_type = ALPHA_PREMULTIPLIED;
926 } else if (any_postmultiplied) {
927 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
929 node->output_alpha_type = ALPHA_BLANK;
935 bool EffectChain::node_needs_colorspace_fix(Node *node)
937 if (node->disabled) {
940 if (node->effect->num_inputs() == 0) {
944 // propagate_gamma_and_color_space() has already set our output
945 // to COLORSPACE_INVALID if the inputs differ, so we can rely on that.
946 if (node->output_color_space == COLORSPACE_INVALID) {
949 return (node->effect->needs_srgb_primaries() && node->output_color_space != COLORSPACE_sRGB);
952 // Fix up color spaces so that there are no COLORSPACE_INVALID nodes left in
953 // the graph. Our strategy is not always optimal, but quite simple:
954 // Find an effect that's as early as possible where the inputs are of
955 // unacceptable colorspaces (that is, either different, or, if the effect only
956 // wants sRGB, not sRGB.) Add appropriate conversions on all its inputs,
957 // propagate the information anew, and repeat until there are no more such
959 void EffectChain::fix_internal_color_spaces()
961 unsigned colorspace_propagation_pass = 0;
965 for (unsigned i = 0; i < nodes.size(); ++i) {
966 Node *node = nodes[i];
967 if (!node_needs_colorspace_fix(node)) {
971 // Go through each input that is not sRGB, and insert
972 // a colorspace conversion after it.
973 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
974 Node *input = node->incoming_links[j];
975 assert(input->output_color_space != COLORSPACE_INVALID);
976 if (input->output_color_space == COLORSPACE_sRGB) {
979 Node *conversion = add_node(new ColorspaceConversionEffect());
980 CHECK(conversion->effect->set_int("source_space", input->output_color_space));
981 CHECK(conversion->effect->set_int("destination_space", COLORSPACE_sRGB));
982 conversion->output_color_space = COLORSPACE_sRGB;
983 replace_sender(input, conversion);
984 connect_nodes(input, conversion);
987 // Re-sort topologically, and propagate the new information.
988 propagate_gamma_and_color_space();
995 sprintf(filename, "step5-colorspacefix-iter%u.dot", ++colorspace_propagation_pass);
996 output_dot(filename);
997 assert(colorspace_propagation_pass < 100);
1000 for (unsigned i = 0; i < nodes.size(); ++i) {
1001 Node *node = nodes[i];
1002 if (node->disabled) {
1005 assert(node->output_color_space != COLORSPACE_INVALID);
1009 bool EffectChain::node_needs_alpha_fix(Node *node)
1011 if (node->disabled) {
1015 // propagate_alpha() has already set our output to ALPHA_INVALID if the
1016 // inputs differ or we are otherwise in mismatch, so we can rely on that.
1017 return (node->output_alpha_type == ALPHA_INVALID);
1020 // Fix up alpha so that there are no ALPHA_INVALID nodes left in
1021 // the graph. Similar to fix_internal_color_spaces().
1022 void EffectChain::fix_internal_alpha(unsigned step)
1024 unsigned alpha_propagation_pass = 0;
1028 for (unsigned i = 0; i < nodes.size(); ++i) {
1029 Node *node = nodes[i];
1030 if (!node_needs_alpha_fix(node)) {
1034 // If we need to fix up GammaExpansionEffect, then clearly something
1035 // is wrong, since the combination of premultiplied alpha and nonlinear inputs
1037 assert(node->effect->effect_type_id() != "GammaExpansionEffect");
1039 AlphaType desired_type = ALPHA_PREMULTIPLIED;
1041 // GammaCompressionEffect is special; it needs postmultiplied alpha.
1042 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1043 assert(node->incoming_links.size() == 1);
1044 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
1045 desired_type = ALPHA_POSTMULTIPLIED;
1048 // Go through each input that is not premultiplied alpha, and insert
1049 // a conversion before it.
1050 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1051 Node *input = node->incoming_links[j];
1052 assert(input->output_alpha_type != ALPHA_INVALID);
1053 if (input->output_alpha_type == desired_type ||
1054 input->output_alpha_type == ALPHA_BLANK) {
1058 if (desired_type == ALPHA_PREMULTIPLIED) {
1059 conversion = add_node(new AlphaMultiplicationEffect());
1061 conversion = add_node(new AlphaDivisionEffect());
1063 conversion->output_alpha_type = desired_type;
1064 replace_sender(input, conversion);
1065 connect_nodes(input, conversion);
1068 // Re-sort topologically, and propagate the new information.
1069 propagate_gamma_and_color_space();
1077 sprintf(filename, "step%u-alphafix-iter%u.dot", step, ++alpha_propagation_pass);
1078 output_dot(filename);
1079 assert(alpha_propagation_pass < 100);
1080 } while (found_any);
1082 for (unsigned i = 0; i < nodes.size(); ++i) {
1083 Node *node = nodes[i];
1084 if (node->disabled) {
1087 assert(node->output_alpha_type != ALPHA_INVALID);
1091 // Make so that the output is in the desired color space.
1092 void EffectChain::fix_output_color_space()
1094 Node *output = find_output_node();
1095 if (output->output_color_space != output_format.color_space) {
1096 Node *conversion = add_node(new ColorspaceConversionEffect());
1097 CHECK(conversion->effect->set_int("source_space", output->output_color_space));
1098 CHECK(conversion->effect->set_int("destination_space", output_format.color_space));
1099 conversion->output_color_space = output_format.color_space;
1100 connect_nodes(output, conversion);
1102 propagate_gamma_and_color_space();
1106 // Make so that the output is in the desired pre-/postmultiplication alpha state.
1107 void EffectChain::fix_output_alpha()
1109 Node *output = find_output_node();
1110 assert(output->output_alpha_type != ALPHA_INVALID);
1111 if (output->output_alpha_type == ALPHA_BLANK) {
1112 // No alpha output, so we don't care.
1115 if (output->output_alpha_type == ALPHA_PREMULTIPLIED &&
1116 output_alpha_format == OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED) {
1117 Node *conversion = add_node(new AlphaDivisionEffect());
1118 connect_nodes(output, conversion);
1120 propagate_gamma_and_color_space();
1122 if (output->output_alpha_type == ALPHA_POSTMULTIPLIED &&
1123 output_alpha_format == OUTPUT_ALPHA_FORMAT_PREMULTIPLIED) {
1124 Node *conversion = add_node(new AlphaMultiplicationEffect());
1125 connect_nodes(output, conversion);
1127 propagate_gamma_and_color_space();
1131 bool EffectChain::node_needs_gamma_fix(Node *node)
1133 if (node->disabled) {
1137 // Small hack since the output is not an explicit node:
1138 // If we are the last node and our output is in the wrong
1139 // space compared to EffectChain's output, we need to fix it.
1140 // This will only take us to linear, but fix_output_gamma()
1141 // will come and take us to the desired output gamma
1144 // This needs to be before everything else, since it could
1145 // even apply to inputs (if they are the only effect).
1146 if (node->outgoing_links.empty() &&
1147 node->output_gamma_curve != output_format.gamma_curve &&
1148 node->output_gamma_curve != GAMMA_LINEAR) {
1152 if (node->effect->num_inputs() == 0) {
1156 // propagate_gamma_and_color_space() has already set our output
1157 // to GAMMA_INVALID if the inputs differ, so we can rely on that,
1158 // except for GammaCompressionEffect.
1159 if (node->output_gamma_curve == GAMMA_INVALID) {
1162 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1163 assert(node->incoming_links.size() == 1);
1164 return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
1167 return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
1170 // Very similar to fix_internal_color_spaces(), but for gamma.
1171 // There is one difference, though; before we start adding conversion nodes,
1172 // we see if we can get anything out of asking the sources to deliver
1173 // linear gamma directly. fix_internal_gamma_by_asking_inputs()
1174 // does that part, while fix_internal_gamma_by_inserting_nodes()
1175 // inserts nodes as needed afterwards.
1176 void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
1178 unsigned gamma_propagation_pass = 0;
1182 for (unsigned i = 0; i < nodes.size(); ++i) {
1183 Node *node = nodes[i];
1184 if (!node_needs_gamma_fix(node)) {
1188 // See if all inputs can give us linear gamma. If not, leave it.
1189 vector<Node *> nonlinear_inputs;
1190 find_all_nonlinear_inputs(node, &nonlinear_inputs);
1191 assert(!nonlinear_inputs.empty());
1194 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1195 Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
1196 all_ok &= input->can_output_linear_gamma();
1203 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1204 CHECK(nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1));
1205 nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
1208 // Re-sort topologically, and propagate the new information.
1209 propagate_gamma_and_color_space();
1216 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1217 output_dot(filename);
1218 assert(gamma_propagation_pass < 100);
1219 } while (found_any);
1222 void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
1224 unsigned gamma_propagation_pass = 0;
1228 for (unsigned i = 0; i < nodes.size(); ++i) {
1229 Node *node = nodes[i];
1230 if (!node_needs_gamma_fix(node)) {
1234 // Special case: We could be an input and still be asked to
1235 // fix our gamma; if so, we should be the only node
1236 // (as node_needs_gamma_fix() would only return true in
1237 // for an input in that case). That means we should insert
1238 // a conversion node _after_ ourselves.
1239 if (node->incoming_links.empty()) {
1240 assert(node->outgoing_links.empty());
1241 Node *conversion = add_node(new GammaExpansionEffect());
1242 CHECK(conversion->effect->set_int("source_curve", node->output_gamma_curve));
1243 conversion->output_gamma_curve = GAMMA_LINEAR;
1244 connect_nodes(node, conversion);
1247 // If not, go through each input that is not linear gamma,
1248 // and insert a gamma conversion after it.
1249 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1250 Node *input = node->incoming_links[j];
1251 assert(input->output_gamma_curve != GAMMA_INVALID);
1252 if (input->output_gamma_curve == GAMMA_LINEAR) {
1255 Node *conversion = add_node(new GammaExpansionEffect());
1256 CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
1257 conversion->output_gamma_curve = GAMMA_LINEAR;
1258 replace_sender(input, conversion);
1259 connect_nodes(input, conversion);
1262 // Re-sort topologically, and propagate the new information.
1264 propagate_gamma_and_color_space();
1271 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1272 output_dot(filename);
1273 assert(gamma_propagation_pass < 100);
1274 } while (found_any);
1276 for (unsigned i = 0; i < nodes.size(); ++i) {
1277 Node *node = nodes[i];
1278 if (node->disabled) {
1281 assert(node->output_gamma_curve != GAMMA_INVALID);
1285 // Make so that the output is in the desired gamma.
1286 // Note that this assumes linear input gamma, so it might create the need
1287 // for another pass of fix_internal_gamma().
1288 void EffectChain::fix_output_gamma()
1290 Node *output = find_output_node();
1291 if (output->output_gamma_curve != output_format.gamma_curve) {
1292 Node *conversion = add_node(new GammaCompressionEffect());
1293 CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
1294 conversion->output_gamma_curve = output_format.gamma_curve;
1295 connect_nodes(output, conversion);
1299 // If the user has requested dither, add a DitherEffect right at the end
1300 // (after GammaCompressionEffect etc.). This needs to be done after everything else,
1301 // since dither is about the only effect that can _not_ be done in linear space.
1302 void EffectChain::add_dither_if_needed()
1304 if (num_dither_bits == 0) {
1307 Node *output = find_output_node();
1308 Node *dither = add_node(new DitherEffect());
1309 CHECK(dither->effect->set_int("num_bits", num_dither_bits));
1310 connect_nodes(output, dither);
1312 dither_effect = dither->effect;
1315 // Find the output node. This is, simply, one that has no outgoing links.
1316 // If there are multiple ones, the graph is malformed (we do not support
1317 // multiple outputs right now).
1318 Node *EffectChain::find_output_node()
1320 vector<Node *> output_nodes;
1321 for (unsigned i = 0; i < nodes.size(); ++i) {
1322 Node *node = nodes[i];
1323 if (node->disabled) {
1326 if (node->outgoing_links.empty()) {
1327 output_nodes.push_back(node);
1330 assert(output_nodes.size() == 1);
1331 return output_nodes[0];
1334 void EffectChain::finalize()
1336 // Save the current locale, and set it to C, so that we can output decimal
1337 // numbers with printf and be sure to get them in the format mandated by GLSL.
1338 #if defined(__MINGW32__)
1339 // Note that the OpenGL driver might call setlocale() behind-the-scenes,
1340 // and that might corrupt the returned pointer, so we need to take our own
1342 char *saved_locale = strdup(setlocale(LC_NUMERIC, NULL));
1343 setlocale(LC_NUMERIC, "C");
1345 locale_t c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
1346 locale_t saved_locale = uselocale(c_locale);
1349 // Output the graph as it is before we do any conversions on it.
1350 output_dot("step0-start.dot");
1352 // Give each effect in turn a chance to rewrite its own part of the graph.
1353 // Note that if more effects are added as part of this, they will be
1354 // picked up as part of the same for loop, since they are added at the end.
1355 for (unsigned i = 0; i < nodes.size(); ++i) {
1356 nodes[i]->effect->rewrite_graph(this, nodes[i]);
1358 output_dot("step1-rewritten.dot");
1360 find_color_spaces_for_inputs();
1361 output_dot("step2-input-colorspace.dot");
1364 output_dot("step3-propagated-alpha.dot");
1366 propagate_gamma_and_color_space();
1367 output_dot("step4-propagated-all.dot");
1369 fix_internal_color_spaces();
1370 fix_internal_alpha(6);
1371 fix_output_color_space();
1372 output_dot("step7-output-colorspacefix.dot");
1374 output_dot("step8-output-alphafix.dot");
1376 // Note that we need to fix gamma after colorspace conversion,
1377 // because colorspace conversions might create needs for gamma conversions.
1378 // Also, we need to run an extra pass of fix_internal_gamma() after
1379 // fixing the output gamma, as we only have conversions to/from linear,
1380 // and fix_internal_alpha() since GammaCompressionEffect needs
1381 // postmultiplied input.
1382 fix_internal_gamma_by_asking_inputs(9);
1383 fix_internal_gamma_by_inserting_nodes(10);
1385 output_dot("step11-output-gammafix.dot");
1387 output_dot("step12-output-alpha-propagated.dot");
1388 fix_internal_alpha(13);
1389 output_dot("step14-output-alpha-fixed.dot");
1390 fix_internal_gamma_by_asking_inputs(15);
1391 fix_internal_gamma_by_inserting_nodes(16);
1393 output_dot("step17-before-dither.dot");
1395 add_dither_if_needed();
1397 output_dot("step18-final.dot");
1399 // Construct all needed GLSL programs, starting at the output.
1400 // We need to keep track of which effects have already been computed,
1401 // as an effect with multiple users could otherwise be calculated
1403 map<Node *, Phase *> completed_effects;
1404 construct_phase(find_output_node(), &completed_effects);
1406 output_dot("step19-split-to-phases.dot");
1408 assert(phases[0]->inputs.empty());
1411 #if defined(__MINGW32__)
1412 setlocale(LC_NUMERIC, saved_locale);
1415 uselocale(saved_locale);
1416 freelocale(c_locale);
1420 void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height)
1424 // Save original viewport.
1425 GLuint x = 0, y = 0;
1427 if (width == 0 && height == 0) {
1429 glGetIntegerv(GL_VIEWPORT, viewport);
1432 width = viewport[2];
1433 height = viewport[3];
1437 glDisable(GL_BLEND);
1439 glDisable(GL_DEPTH_TEST);
1441 glDepthMask(GL_FALSE);
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_num = 0; phase_num < phases.size(); ++phase_num) {
1451 Phase *phase = phases[phase_num];
1453 if (phase_num == phases.size() - 1) {
1454 // Last phase goes to the output the user specified.
1455 glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
1457 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
1458 assert(status == GL_FRAMEBUFFER_COMPLETE);
1459 glViewport(x, y, width, height);
1460 if (dither_effect != NULL) {
1461 CHECK(dither_effect->set_int("output_width", width));
1462 CHECK(dither_effect->set_int("output_height", height));
1465 execute_phase(phase, phase_num == phases.size() - 1, &output_textures, &generated_mipmaps);
1468 for (map<Phase *, GLuint>::const_iterator texture_it = output_textures.begin();
1469 texture_it != output_textures.end();
1471 resource_pool->release_2d_texture(texture_it->second);
1474 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1480 void EffectChain::execute_phase(Phase *phase, bool last_phase, map<Phase *, GLuint> *output_textures, set<Phase *> *generated_mipmaps)
1484 // Find a texture for this phase.
1485 inform_input_sizes(phase);
1487 find_output_size(phase);
1489 GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F, phase->output_width, phase->output_height);
1490 output_textures->insert(make_pair(phase, tex_num));
1493 const GLuint glsl_program_num = phase->glsl_program_num;
1495 glUseProgram(glsl_program_num);
1498 // Set up RTT inputs for this phase.
1499 for (unsigned sampler = 0; sampler < phase->inputs.size(); ++sampler) {
1500 glActiveTexture(GL_TEXTURE0 + sampler);
1501 Phase *input = phase->inputs[sampler];
1502 input->output_node->bound_sampler_num = sampler;
1503 glBindTexture(GL_TEXTURE_2D, (*output_textures)[input]);
1505 if (phase->input_needs_mipmaps && generated_mipmaps->count(input) == 0) {
1506 glGenerateMipmap(GL_TEXTURE_2D);
1508 generated_mipmaps->insert(input);
1510 setup_rtt_sampler(glsl_program_num, sampler, phase->effect_ids[input->output_node], phase->input_needs_mipmaps);
1513 // And now the output. (Already set up for us if it is the last phase.)
1515 fbo = resource_pool->create_fbo((*output_textures)[phase]);
1516 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1517 glViewport(0, 0, phase->output_width, phase->output_height);
1520 // Give the required parameters to all the effects.
1521 unsigned sampler_num = phase->inputs.size();
1522 for (unsigned i = 0; i < phase->effects.size(); ++i) {
1523 Node *node = phase->effects[i];
1524 unsigned old_sampler_num = sampler_num;
1525 node->effect->set_gl_state(glsl_program_num, phase->effect_ids[node], &sampler_num);
1528 if (node->effect->is_single_texture()) {
1529 assert(sampler_num - old_sampler_num == 1);
1530 node->bound_sampler_num = old_sampler_num;
1532 node->bound_sampler_num = -1;
1537 float vertices[] = {
1545 glGenVertexArrays(1, &vao);
1547 glBindVertexArray(vao);
1550 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
1551 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same as vertices.
1553 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1556 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
1557 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
1562 for (unsigned i = 0; i < phase->effects.size(); ++i) {
1563 Node *node = phase->effects[i];
1564 node->effect->clear_gl_state();
1568 resource_pool->release_fbo(fbo);
1571 glDeleteVertexArrays(1, &vao);
1575 void EffectChain::setup_rtt_sampler(GLuint glsl_program_num, int sampler_num, const string &effect_id, bool use_mipmaps)
1577 glActiveTexture(GL_TEXTURE0 + sampler_num);
1580 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1583 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1586 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1588 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1591 string texture_name = string("tex_") + effect_id;
1592 glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler_num);
1596 } // namespace movit