1 #define GL_GLEXT_PROTOTYPES 1
16 #include "alpha_division_effect.h"
17 #include "alpha_multiplication_effect.h"
18 #include "colorspace_conversion_effect.h"
19 #include "dither_effect.h"
21 #include "effect_chain.h"
22 #include "gamma_compression_effect.h"
23 #include "gamma_expansion_effect.h"
28 EffectChain::EffectChain(float aspect_nom, float aspect_denom)
29 : aspect_nom(aspect_nom),
30 aspect_denom(aspect_denom),
36 EffectChain::~EffectChain()
38 for (unsigned i = 0; i < nodes.size(); ++i) {
39 if (nodes[i]->output_texture != 0) {
40 glDeleteTextures(1, &nodes[i]->output_texture);
42 delete nodes[i]->effect;
45 for (unsigned i = 0; i < phases.size(); ++i) {
46 glDeleteProgram(phases[i]->glsl_program_num);
47 glDeleteShader(phases[i]->vertex_shader);
48 glDeleteShader(phases[i]->fragment_shader);
52 glDeleteFramebuffers(1, &fbo);
56 Input *EffectChain::add_input(Input *input)
58 inputs.push_back(input);
63 void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
65 output_format = format;
66 output_alpha_format = alpha_format;
69 Node *EffectChain::add_node(Effect *effect)
72 sprintf(effect_id, "eff%u", (unsigned)nodes.size());
74 Node *node = new Node;
75 node->effect = effect;
76 node->disabled = false;
77 node->effect_id = effect_id;
78 node->output_color_space = COLORSPACE_INVALID;
79 node->output_gamma_curve = GAMMA_INVALID;
80 node->output_alpha_type = ALPHA_INVALID;
81 node->output_texture = 0;
83 nodes.push_back(node);
84 node_map[effect] = node;
88 void EffectChain::connect_nodes(Node *sender, Node *receiver)
90 sender->outgoing_links.push_back(receiver);
91 receiver->incoming_links.push_back(sender);
94 void EffectChain::replace_receiver(Node *old_receiver, Node *new_receiver)
96 new_receiver->incoming_links = old_receiver->incoming_links;
97 old_receiver->incoming_links.clear();
99 for (unsigned i = 0; i < new_receiver->incoming_links.size(); ++i) {
100 Node *sender = new_receiver->incoming_links[i];
101 for (unsigned j = 0; j < sender->outgoing_links.size(); ++j) {
102 if (sender->outgoing_links[j] == old_receiver) {
103 sender->outgoing_links[j] = new_receiver;
109 void EffectChain::replace_sender(Node *old_sender, Node *new_sender)
111 new_sender->outgoing_links = old_sender->outgoing_links;
112 old_sender->outgoing_links.clear();
114 for (unsigned i = 0; i < new_sender->outgoing_links.size(); ++i) {
115 Node *receiver = new_sender->outgoing_links[i];
116 for (unsigned j = 0; j < receiver->incoming_links.size(); ++j) {
117 if (receiver->incoming_links[j] == old_sender) {
118 receiver->incoming_links[j] = new_sender;
124 void EffectChain::insert_node_between(Node *sender, Node *middle, Node *receiver)
126 for (unsigned i = 0; i < sender->outgoing_links.size(); ++i) {
127 if (sender->outgoing_links[i] == receiver) {
128 sender->outgoing_links[i] = middle;
129 middle->incoming_links.push_back(sender);
132 for (unsigned i = 0; i < receiver->incoming_links.size(); ++i) {
133 if (receiver->incoming_links[i] == sender) {
134 receiver->incoming_links[i] = middle;
135 middle->outgoing_links.push_back(receiver);
139 assert(middle->incoming_links.size() == middle->effect->num_inputs());
142 void EffectChain::find_all_nonlinear_inputs(Node *node, std::vector<Node *> *nonlinear_inputs)
144 if (node->output_gamma_curve == GAMMA_LINEAR &&
145 node->effect->effect_type_id() != "GammaCompressionEffect") {
148 if (node->effect->num_inputs() == 0) {
149 nonlinear_inputs->push_back(node);
151 assert(node->effect->num_inputs() == node->incoming_links.size());
152 for (unsigned i = 0; i < node->incoming_links.size(); ++i) {
153 find_all_nonlinear_inputs(node->incoming_links[i], nonlinear_inputs);
158 Effect *EffectChain::add_effect(Effect *effect, const std::vector<Effect *> &inputs)
160 assert(inputs.size() == effect->num_inputs());
161 Node *node = add_node(effect);
162 for (unsigned i = 0; i < inputs.size(); ++i) {
163 assert(node_map.count(inputs[i]) != 0);
164 connect_nodes(node_map[inputs[i]], node);
169 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
170 std::string replace_prefix(const std::string &text, const std::string &prefix)
175 while (start < text.size()) {
176 size_t pos = text.find("PREFIX(", start);
177 if (pos == std::string::npos) {
178 output.append(text.substr(start, std::string::npos));
182 output.append(text.substr(start, pos - start));
183 output.append(prefix);
186 pos += strlen("PREFIX(");
188 // Output stuff until we find the matching ), which we then eat.
190 size_t end_arg_pos = pos;
191 while (end_arg_pos < text.size()) {
192 if (text[end_arg_pos] == '(') {
194 } else if (text[end_arg_pos] == ')') {
202 output.append(text.substr(pos, end_arg_pos - pos));
210 Phase *EffectChain::compile_glsl_program(
211 const std::vector<Node *> &inputs,
212 const std::vector<Node *> &effects)
214 assert(!effects.empty());
216 // Deduplicate the inputs.
217 std::vector<Node *> true_inputs = inputs;
218 std::sort(true_inputs.begin(), true_inputs.end());
219 true_inputs.erase(std::unique(true_inputs.begin(), true_inputs.end()), true_inputs.end());
221 bool input_needs_mipmaps = false;
222 std::string frag_shader = read_file("header.frag");
224 // Create functions for all the texture inputs that we need.
225 for (unsigned i = 0; i < true_inputs.size(); ++i) {
226 Node *input = true_inputs[i];
228 frag_shader += std::string("uniform sampler2D tex_") + input->effect_id + ";\n";
229 frag_shader += std::string("vec4 ") + input->effect_id + "(vec2 tc) {\n";
230 frag_shader += "\treturn texture2D(tex_" + input->effect_id + ", tc);\n";
231 frag_shader += "}\n";
235 std::vector<Node *> sorted_effects = topological_sort(effects);
237 for (unsigned i = 0; i < sorted_effects.size(); ++i) {
238 Node *node = sorted_effects[i];
240 if (node->incoming_links.size() == 1) {
241 frag_shader += std::string("#define INPUT ") + node->incoming_links[0]->effect_id + "\n";
243 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
245 sprintf(buf, "#define INPUT%d %s\n", j + 1, node->incoming_links[j]->effect_id.c_str());
251 frag_shader += std::string("#define FUNCNAME ") + node->effect_id + "\n";
252 frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), node->effect_id);
253 frag_shader += replace_prefix(node->effect->output_fragment_shader(), node->effect_id);
254 frag_shader += "#undef PREFIX\n";
255 frag_shader += "#undef FUNCNAME\n";
256 if (node->incoming_links.size() == 1) {
257 frag_shader += "#undef INPUT\n";
259 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
261 sprintf(buf, "#undef INPUT%d\n", j + 1);
267 input_needs_mipmaps |= node->effect->needs_mipmaps();
269 for (unsigned i = 0; i < sorted_effects.size(); ++i) {
270 Node *node = sorted_effects[i];
271 if (node->effect->num_inputs() == 0) {
272 CHECK(node->effect->set_int("needs_mipmaps", input_needs_mipmaps));
275 frag_shader += std::string("#define INPUT ") + sorted_effects.back()->effect_id + "\n";
276 frag_shader.append(read_file("footer.frag"));
278 if (movit_debug_level == MOVIT_DEBUG_ON) {
279 // Output shader to a temporary file, for easier debugging.
280 static int compiled_shader_num = 0;
282 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
283 FILE *fp = fopen(filename, "w");
288 fprintf(fp, "%s\n", frag_shader.c_str());
292 GLuint glsl_program_num = glCreateProgram();
293 GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
294 GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
295 glAttachShader(glsl_program_num, vs_obj);
297 glAttachShader(glsl_program_num, fs_obj);
299 glLinkProgram(glsl_program_num);
302 Phase *phase = new Phase;
303 phase->glsl_program_num = glsl_program_num;
304 phase->vertex_shader = vs_obj;
305 phase->fragment_shader = fs_obj;
306 phase->input_needs_mipmaps = input_needs_mipmaps;
307 phase->inputs = true_inputs;
308 phase->effects = sorted_effects;
313 // Construct GLSL programs, starting at the given effect and following
314 // the chain from there. We end a program every time we come to an effect
315 // marked as "needs texture bounce", one that is used by multiple other
316 // effects, every time an effect wants to change the output size,
317 // and of course at the end.
319 // We follow a quite simple depth-first search from the output, although
320 // without any explicit recursion.
321 void EffectChain::construct_glsl_programs(Node *output)
323 // Which effects have already been completed?
324 // We need to keep track of it, as an effect with multiple outputs
325 // could otherwise be calculated multiple times.
326 std::set<Node *> completed_effects;
328 // Effects in the current phase, as well as inputs (outputs from other phases
329 // that we depend on). Note that since we start iterating from the end,
330 // the effect list will be in the reverse order.
331 std::vector<Node *> this_phase_inputs;
332 std::vector<Node *> this_phase_effects;
334 // Effects that we have yet to calculate, but that we know should
335 // be in the current phase.
336 std::stack<Node *> effects_todo_this_phase;
338 // Effects that we have yet to calculate, but that come from other phases.
339 // We delay these until we have this phase done in its entirety,
340 // at which point we pick any of them and start a new phase from that.
341 std::stack<Node *> effects_todo_other_phases;
343 effects_todo_this_phase.push(output);
345 for ( ;; ) { // Termination condition within loop.
346 if (!effects_todo_this_phase.empty()) {
347 // OK, we have more to do this phase.
348 Node *node = effects_todo_this_phase.top();
349 effects_todo_this_phase.pop();
351 // This should currently only happen for effects that are inputs
352 // (either true inputs or phase outputs). We special-case inputs,
353 // and then deduplicate phase outputs in compile_glsl_program().
354 if (node->effect->num_inputs() == 0) {
355 if (find(this_phase_effects.begin(), this_phase_effects.end(), node) != this_phase_effects.end()) {
359 assert(completed_effects.count(node) == 0);
362 this_phase_effects.push_back(node);
363 completed_effects.insert(node);
365 // Find all the dependencies of this effect, and add them to the stack.
366 std::vector<Node *> deps = node->incoming_links;
367 assert(node->effect->num_inputs() == deps.size());
368 for (unsigned i = 0; i < deps.size(); ++i) {
369 bool start_new_phase = false;
371 // FIXME: If we sample directly from a texture, we won't need this.
372 if (node->effect->needs_texture_bounce()) {
373 start_new_phase = true;
376 if (deps[i]->outgoing_links.size() > 1) {
377 if (deps[i]->effect->num_inputs() > 0) {
378 // More than one effect uses this as the input,
379 // and it is not a texture itself.
380 // The easiest thing to do (and probably also the safest
381 // performance-wise in most cases) is to bounce it to a texture
382 // and then let the next passes read from that.
383 start_new_phase = true;
385 // For textures, we try to be slightly more clever;
386 // if none of our outputs need a bounce, we don't bounce
387 // but instead simply use the effect many times.
389 // Strictly speaking, we could bounce it for some outputs
390 // and use it directly for others, but the processing becomes
391 // somewhat simpler if the effect is only used in one such way.
392 for (unsigned j = 0; j < deps[i]->outgoing_links.size(); ++j) {
393 Node *rdep = deps[i]->outgoing_links[j];
394 start_new_phase |= rdep->effect->needs_texture_bounce();
399 if (deps[i]->effect->changes_output_size()) {
400 start_new_phase = true;
403 if (start_new_phase) {
404 effects_todo_other_phases.push(deps[i]);
405 this_phase_inputs.push_back(deps[i]);
407 effects_todo_this_phase.push(deps[i]);
413 // No more effects to do this phase. Take all the ones we have,
414 // and create a GLSL program for it.
415 if (!this_phase_effects.empty()) {
416 reverse(this_phase_effects.begin(), this_phase_effects.end());
417 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
418 this_phase_effects.back()->phase = phases.back();
419 this_phase_inputs.clear();
420 this_phase_effects.clear();
422 assert(this_phase_inputs.empty());
423 assert(this_phase_effects.empty());
425 // If we have no effects left, exit.
426 if (effects_todo_other_phases.empty()) {
430 Node *node = effects_todo_other_phases.top();
431 effects_todo_other_phases.pop();
433 if (completed_effects.count(node) == 0) {
434 // Start a new phase, calculating from this effect.
435 effects_todo_this_phase.push(node);
439 // Finally, since the phases are found from the output but must be executed
440 // from the input(s), reverse them, too.
441 std::reverse(phases.begin(), phases.end());
444 void EffectChain::output_dot(const char *filename)
446 if (movit_debug_level != MOVIT_DEBUG_ON) {
450 FILE *fp = fopen(filename, "w");
456 fprintf(fp, "digraph G {\n");
457 fprintf(fp, " output [shape=box label=\"(output)\"];\n");
458 for (unsigned i = 0; i < nodes.size(); ++i) {
459 // Find out which phase this event belongs to.
460 std::vector<int> in_phases;
461 for (unsigned j = 0; j < phases.size(); ++j) {
462 const Phase* p = phases[j];
463 if (std::find(p->effects.begin(), p->effects.end(), nodes[i]) != p->effects.end()) {
464 in_phases.push_back(j);
468 if (in_phases.empty()) {
469 fprintf(fp, " n%ld [label=\"%s\"];\n", (long)nodes[i], nodes[i]->effect->effect_type_id().c_str());
470 } else if (in_phases.size() == 1) {
471 fprintf(fp, " n%ld [label=\"%s\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
472 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
473 (in_phases[0] % 8) + 1);
475 // If we had new enough Graphviz, style="wedged" would probably be ideal here.
477 fprintf(fp, " n%ld [label=\"%s [in multiple phases]\" style=\"filled\" fillcolor=\"/accent8/%d\"];\n",
478 (long)nodes[i], nodes[i]->effect->effect_type_id().c_str(),
479 (in_phases[0] % 8) + 1);
482 char from_node_id[256];
483 snprintf(from_node_id, 256, "n%ld", (long)nodes[i]);
485 for (unsigned j = 0; j < nodes[i]->outgoing_links.size(); ++j) {
486 char to_node_id[256];
487 snprintf(to_node_id, 256, "n%ld", (long)nodes[i]->outgoing_links[j]);
489 std::vector<std::string> labels = get_labels_for_edge(nodes[i], nodes[i]->outgoing_links[j]);
490 output_dot_edge(fp, from_node_id, to_node_id, labels);
493 if (nodes[i]->outgoing_links.empty() && !nodes[i]->disabled) {
495 std::vector<std::string> labels = get_labels_for_edge(nodes[i], NULL);
496 output_dot_edge(fp, from_node_id, "output", labels);
504 std::vector<std::string> EffectChain::get_labels_for_edge(const Node *from, const Node *to)
506 std::vector<std::string> labels;
508 if (to != NULL && to->effect->needs_texture_bounce()) {
509 labels.push_back("needs_bounce");
511 if (from->effect->changes_output_size()) {
512 labels.push_back("resize");
515 switch (from->output_color_space) {
516 case COLORSPACE_INVALID:
517 labels.push_back("spc[invalid]");
519 case COLORSPACE_REC_601_525:
520 labels.push_back("spc[rec601-525]");
522 case COLORSPACE_REC_601_625:
523 labels.push_back("spc[rec601-625]");
529 switch (from->output_gamma_curve) {
531 labels.push_back("gamma[invalid]");
534 labels.push_back("gamma[sRGB]");
536 case GAMMA_REC_601: // and GAMMA_REC_709
537 labels.push_back("gamma[rec601/709]");
543 switch (from->output_alpha_type) {
545 labels.push_back("alpha[invalid]");
548 labels.push_back("alpha[blank]");
550 case ALPHA_POSTMULTIPLIED:
551 labels.push_back("alpha[postmult]");
560 void EffectChain::output_dot_edge(FILE *fp,
561 const std::string &from_node_id,
562 const std::string &to_node_id,
563 const std::vector<std::string> &labels)
565 if (labels.empty()) {
566 fprintf(fp, " %s -> %s;\n", from_node_id.c_str(), to_node_id.c_str());
568 std::string label = labels[0];
569 for (unsigned k = 1; k < labels.size(); ++k) {
570 label += ", " + labels[k];
572 fprintf(fp, " %s -> %s [label=\"%s\"];\n", from_node_id.c_str(), to_node_id.c_str(), label.c_str());
576 void EffectChain::size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height)
578 unsigned scaled_width, scaled_height;
580 if (float(width) * aspect_denom >= float(height) * aspect_nom) {
581 // Same aspect, or W/H > aspect (image is wider than the frame).
582 // In either case, keep width, and adjust height.
583 scaled_width = width;
584 scaled_height = lrintf(width * aspect_denom / aspect_nom);
586 // W/H < aspect (image is taller than the frame), so keep height,
588 scaled_width = lrintf(height * aspect_nom / aspect_denom);
589 scaled_height = height;
592 // We should be consistently larger or smaller then the existing choice,
593 // since we have the same aspect.
594 assert(!(scaled_width < *output_width && scaled_height > *output_height));
595 assert(!(scaled_height < *output_height && scaled_width > *output_width));
597 if (scaled_width >= *output_width && scaled_height >= *output_height) {
598 *output_width = scaled_width;
599 *output_height = scaled_height;
603 // Propagate input texture sizes throughout, and inform effects downstream.
604 // (Like a lot of other code, we depend on effects being in topological order.)
605 void EffectChain::inform_input_sizes(Phase *phase)
607 // All effects that have a defined size (inputs and RTT inputs)
608 // get that. Reset all others.
609 for (unsigned i = 0; i < phase->effects.size(); ++i) {
610 Node *node = phase->effects[i];
611 if (node->effect->num_inputs() == 0) {
612 Input *input = static_cast<Input *>(node->effect);
613 node->output_width = input->get_width();
614 node->output_height = input->get_height();
615 assert(node->output_width != 0);
616 assert(node->output_height != 0);
618 node->output_width = node->output_height = 0;
621 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
622 Node *input = phase->inputs[i];
623 input->output_width = input->phase->virtual_output_width;
624 input->output_height = input->phase->virtual_output_height;
625 assert(input->output_width != 0);
626 assert(input->output_height != 0);
629 // Now propagate from the inputs towards the end, and inform as we go.
630 // The rules are simple:
632 // 1. Don't touch effects that already have given sizes (ie., inputs).
633 // 2. If all of your inputs have the same size, that will be your output size.
634 // 3. Otherwise, your output size is 0x0.
635 for (unsigned i = 0; i < phase->effects.size(); ++i) {
636 Node *node = phase->effects[i];
637 if (node->effect->num_inputs() == 0) {
640 unsigned this_output_width = 0;
641 unsigned this_output_height = 0;
642 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
643 Node *input = node->incoming_links[j];
644 node->effect->inform_input_size(j, input->output_width, input->output_height);
646 this_output_width = input->output_width;
647 this_output_height = input->output_height;
648 } else if (input->output_width != this_output_width || input->output_height != this_output_height) {
650 this_output_width = 0;
651 this_output_height = 0;
654 node->output_width = this_output_width;
655 node->output_height = this_output_height;
659 // Note: You should call inform_input_sizes() before this, as the last effect's
660 // desired output size might change based on the inputs.
661 void EffectChain::find_output_size(Phase *phase)
663 Node *output_node = phase->effects.back();
665 // If the last effect explicitly sets an output size, use that.
666 if (output_node->effect->changes_output_size()) {
667 output_node->effect->get_output_size(&phase->output_width, &phase->output_height,
668 &phase->virtual_output_width, &phase->virtual_output_height);
672 // If all effects have the same size, use that.
673 unsigned output_width = 0, output_height = 0;
674 bool all_inputs_same_size = true;
676 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
677 Node *input = phase->inputs[i];
678 assert(input->phase->output_width != 0);
679 assert(input->phase->output_height != 0);
680 if (output_width == 0 && output_height == 0) {
681 output_width = input->phase->virtual_output_width;
682 output_height = input->phase->virtual_output_height;
683 } else if (output_width != input->phase->virtual_output_width ||
684 output_height != input->phase->virtual_output_height) {
685 all_inputs_same_size = false;
688 for (unsigned i = 0; i < phase->effects.size(); ++i) {
689 Effect *effect = phase->effects[i]->effect;
690 if (effect->num_inputs() != 0) {
694 Input *input = static_cast<Input *>(effect);
695 if (output_width == 0 && output_height == 0) {
696 output_width = input->get_width();
697 output_height = input->get_height();
698 } else if (output_width != input->get_width() ||
699 output_height != input->get_height()) {
700 all_inputs_same_size = false;
704 if (all_inputs_same_size) {
705 assert(output_width != 0);
706 assert(output_height != 0);
707 phase->virtual_output_width = phase->output_width = output_width;
708 phase->virtual_output_height = phase->output_height = output_height;
712 // If not, fit all the inputs into the current aspect, and select the largest one.
715 for (unsigned i = 0; i < phase->inputs.size(); ++i) {
716 Node *input = phase->inputs[i];
717 assert(input->phase->output_width != 0);
718 assert(input->phase->output_height != 0);
719 size_rectangle_to_fit(input->phase->output_width, input->phase->output_height, &output_width, &output_height);
721 for (unsigned i = 0; i < phase->effects.size(); ++i) {
722 Effect *effect = phase->effects[i]->effect;
723 if (effect->num_inputs() != 0) {
727 Input *input = static_cast<Input *>(effect);
728 size_rectangle_to_fit(input->get_width(), input->get_height(), &output_width, &output_height);
730 assert(output_width != 0);
731 assert(output_height != 0);
732 phase->virtual_output_width = phase->output_width = output_width;
733 phase->virtual_output_height = phase->output_height = output_height;
736 void EffectChain::sort_all_nodes_topologically()
738 nodes = topological_sort(nodes);
741 std::vector<Node *> EffectChain::topological_sort(const std::vector<Node *> &nodes)
743 std::set<Node *> nodes_left_to_visit(nodes.begin(), nodes.end());
744 std::vector<Node *> sorted_list;
745 for (unsigned i = 0; i < nodes.size(); ++i) {
746 topological_sort_visit_node(nodes[i], &nodes_left_to_visit, &sorted_list);
748 reverse(sorted_list.begin(), sorted_list.end());
752 void EffectChain::topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list)
754 if (nodes_left_to_visit->count(node) == 0) {
757 nodes_left_to_visit->erase(node);
758 for (unsigned i = 0; i < node->outgoing_links.size(); ++i) {
759 topological_sort_visit_node(node->outgoing_links[i], nodes_left_to_visit, sorted_list);
761 sorted_list->push_back(node);
764 void EffectChain::find_color_spaces_for_inputs()
766 for (unsigned i = 0; i < nodes.size(); ++i) {
767 Node *node = nodes[i];
768 if (node->disabled) {
771 if (node->incoming_links.size() == 0) {
772 Input *input = static_cast<Input *>(node->effect);
773 node->output_color_space = input->get_color_space();
774 node->output_gamma_curve = input->get_gamma_curve();
776 Effect::AlphaHandling alpha_handling = input->alpha_handling();
777 switch (alpha_handling) {
778 case Effect::OUTPUT_BLANK_ALPHA:
779 node->output_alpha_type = ALPHA_BLANK;
781 case Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA:
782 node->output_alpha_type = ALPHA_PREMULTIPLIED;
784 case Effect::OUTPUT_POSTMULTIPLIED_ALPHA:
785 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
787 case Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK:
788 case Effect::DONT_CARE_ALPHA_TYPE:
793 if (node->output_alpha_type == ALPHA_PREMULTIPLIED) {
794 assert(node->output_gamma_curve == GAMMA_LINEAR);
800 // Propagate gamma and color space information as far as we can in the graph.
801 // The rules are simple: Anything where all the inputs agree, get that as
802 // output as well. Anything else keeps having *_INVALID.
803 void EffectChain::propagate_gamma_and_color_space()
805 // We depend on going through the nodes in order.
806 sort_all_nodes_topologically();
808 for (unsigned i = 0; i < nodes.size(); ++i) {
809 Node *node = nodes[i];
810 if (node->disabled) {
813 assert(node->incoming_links.size() == node->effect->num_inputs());
814 if (node->incoming_links.size() == 0) {
815 assert(node->output_color_space != COLORSPACE_INVALID);
816 assert(node->output_gamma_curve != GAMMA_INVALID);
820 Colorspace color_space = node->incoming_links[0]->output_color_space;
821 GammaCurve gamma_curve = node->incoming_links[0]->output_gamma_curve;
822 for (unsigned j = 1; j < node->incoming_links.size(); ++j) {
823 if (node->incoming_links[j]->output_color_space != color_space) {
824 color_space = COLORSPACE_INVALID;
826 if (node->incoming_links[j]->output_gamma_curve != gamma_curve) {
827 gamma_curve = GAMMA_INVALID;
831 // The conversion effects already have their outputs set correctly,
832 // so leave them alone.
833 if (node->effect->effect_type_id() != "ColorspaceConversionEffect") {
834 node->output_color_space = color_space;
836 if (node->effect->effect_type_id() != "GammaCompressionEffect" &&
837 node->effect->effect_type_id() != "GammaExpansionEffect") {
838 node->output_gamma_curve = gamma_curve;
843 // Propagate alpha information as far as we can in the graph.
844 // Similar to propagate_gamma_and_color_space().
845 void EffectChain::propagate_alpha()
847 // We depend on going through the nodes in order.
848 sort_all_nodes_topologically();
850 for (unsigned i = 0; i < nodes.size(); ++i) {
851 Node *node = nodes[i];
852 if (node->disabled) {
855 assert(node->incoming_links.size() == node->effect->num_inputs());
856 if (node->incoming_links.size() == 0) {
857 assert(node->output_alpha_type != ALPHA_INVALID);
861 // The alpha multiplication/division effects are special cases.
862 if (node->effect->effect_type_id() == "AlphaMultiplicationEffect") {
863 assert(node->incoming_links.size() == 1);
864 assert(node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED);
865 node->output_alpha_type = ALPHA_PREMULTIPLIED;
868 if (node->effect->effect_type_id() == "AlphaDivisionEffect") {
869 assert(node->incoming_links.size() == 1);
870 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
871 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
875 // GammaCompressionEffect and GammaExpansionEffect are also a special case,
876 // because they are the only one that _need_ postmultiplied alpha.
877 if (node->effect->effect_type_id() == "GammaCompressionEffect" ||
878 node->effect->effect_type_id() == "GammaExpansionEffect") {
879 assert(node->incoming_links.size() == 1);
880 if (node->incoming_links[0]->output_alpha_type == ALPHA_BLANK) {
881 node->output_alpha_type = ALPHA_BLANK;
882 } else if (node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED) {
883 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
885 node->output_alpha_type = ALPHA_INVALID;
890 // Only inputs can have unconditional alpha output (OUTPUT_BLANK_ALPHA
891 // or OUTPUT_POSTMULTIPLIED_ALPHA), and they have already been
892 // taken care of above. Rationale: Even if you could imagine
893 // e.g. an effect that took in an image and set alpha=1.0
894 // unconditionally, it wouldn't make any sense to have it as
895 // e.g. OUTPUT_BLANK_ALPHA, since it wouldn't know whether it
896 // got its input pre- or postmultiplied, so it wouldn't know
897 // whether to divide away the old alpha or not.
898 Effect::AlphaHandling alpha_handling = node->effect->alpha_handling();
899 assert(alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
900 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK ||
901 alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
903 // If the node has multiple inputs, check that they are all valid and
905 bool any_invalid = false;
906 bool any_premultiplied = false;
907 bool any_postmultiplied = false;
909 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
910 switch (node->incoming_links[j]->output_alpha_type) {
915 // Blank is good as both pre- and postmultiplied alpha,
916 // so just ignore it.
918 case ALPHA_PREMULTIPLIED:
919 any_premultiplied = true;
921 case ALPHA_POSTMULTIPLIED:
922 any_postmultiplied = true;
930 node->output_alpha_type = ALPHA_INVALID;
934 // Inputs must be of the same type.
935 if (any_premultiplied && any_postmultiplied) {
936 node->output_alpha_type = ALPHA_INVALID;
940 if (alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
941 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
942 // If the effect has asked for premultiplied alpha, check that it has got it.
943 if (any_postmultiplied) {
944 node->output_alpha_type = ALPHA_INVALID;
945 } else if (!any_premultiplied &&
946 alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
947 // Blank input alpha, and the effect preserves blank alpha.
948 node->output_alpha_type = ALPHA_BLANK;
950 node->output_alpha_type = ALPHA_PREMULTIPLIED;
953 // OK, all inputs are the same, and this effect is not going
955 assert(alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
956 if (any_premultiplied) {
957 node->output_alpha_type = ALPHA_PREMULTIPLIED;
958 } else if (any_postmultiplied) {
959 node->output_alpha_type = ALPHA_POSTMULTIPLIED;
961 node->output_alpha_type = ALPHA_BLANK;
967 bool EffectChain::node_needs_colorspace_fix(Node *node)
969 if (node->disabled) {
972 if (node->effect->num_inputs() == 0) {
976 // propagate_gamma_and_color_space() has already set our output
977 // to COLORSPACE_INVALID if the inputs differ, so we can rely on that.
978 if (node->output_color_space == COLORSPACE_INVALID) {
981 return (node->effect->needs_srgb_primaries() && node->output_color_space != COLORSPACE_sRGB);
984 // Fix up color spaces so that there are no COLORSPACE_INVALID nodes left in
985 // the graph. Our strategy is not always optimal, but quite simple:
986 // Find an effect that's as early as possible where the inputs are of
987 // unacceptable colorspaces (that is, either different, or, if the effect only
988 // wants sRGB, not sRGB.) Add appropriate conversions on all its inputs,
989 // propagate the information anew, and repeat until there are no more such
991 void EffectChain::fix_internal_color_spaces()
993 unsigned colorspace_propagation_pass = 0;
997 for (unsigned i = 0; i < nodes.size(); ++i) {
998 Node *node = nodes[i];
999 if (!node_needs_colorspace_fix(node)) {
1003 // Go through each input that is not sRGB, and insert
1004 // a colorspace conversion after it.
1005 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1006 Node *input = node->incoming_links[j];
1007 assert(input->output_color_space != COLORSPACE_INVALID);
1008 if (input->output_color_space == COLORSPACE_sRGB) {
1011 Node *conversion = add_node(new ColorspaceConversionEffect());
1012 CHECK(conversion->effect->set_int("source_space", input->output_color_space));
1013 CHECK(conversion->effect->set_int("destination_space", COLORSPACE_sRGB));
1014 conversion->output_color_space = COLORSPACE_sRGB;
1015 replace_sender(input, conversion);
1016 connect_nodes(input, conversion);
1019 // Re-sort topologically, and propagate the new information.
1020 propagate_gamma_and_color_space();
1027 sprintf(filename, "step5-colorspacefix-iter%u.dot", ++colorspace_propagation_pass);
1028 output_dot(filename);
1029 assert(colorspace_propagation_pass < 100);
1030 } while (found_any);
1032 for (unsigned i = 0; i < nodes.size(); ++i) {
1033 Node *node = nodes[i];
1034 if (node->disabled) {
1037 assert(node->output_color_space != COLORSPACE_INVALID);
1041 bool EffectChain::node_needs_alpha_fix(Node *node)
1043 if (node->disabled) {
1047 // propagate_alpha() has already set our output to ALPHA_INVALID if the
1048 // inputs differ or we are otherwise in mismatch, so we can rely on that.
1049 return (node->output_alpha_type == ALPHA_INVALID);
1052 // Fix up alpha so that there are no ALPHA_INVALID nodes left in
1053 // the graph. Similar to fix_internal_color_spaces().
1054 void EffectChain::fix_internal_alpha(unsigned step)
1056 unsigned alpha_propagation_pass = 0;
1060 for (unsigned i = 0; i < nodes.size(); ++i) {
1061 Node *node = nodes[i];
1062 if (!node_needs_alpha_fix(node)) {
1066 // If we need to fix up GammaExpansionEffect, then clearly something
1067 // is wrong, since the combination of premultiplied alpha and nonlinear inputs
1069 assert(node->effect->effect_type_id() != "GammaExpansionEffect");
1071 AlphaType desired_type = ALPHA_PREMULTIPLIED;
1073 // GammaCompressionEffect is special; it needs postmultiplied alpha.
1074 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1075 assert(node->incoming_links.size() == 1);
1076 assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
1077 desired_type = ALPHA_POSTMULTIPLIED;
1080 // Go through each input that is not premultiplied alpha, and insert
1081 // a conversion before it.
1082 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1083 Node *input = node->incoming_links[j];
1084 assert(input->output_alpha_type != ALPHA_INVALID);
1085 if (input->output_alpha_type == desired_type ||
1086 input->output_alpha_type == ALPHA_BLANK) {
1090 if (desired_type == ALPHA_PREMULTIPLIED) {
1091 conversion = add_node(new AlphaMultiplicationEffect());
1093 conversion = add_node(new AlphaDivisionEffect());
1095 conversion->output_alpha_type = desired_type;
1096 replace_sender(input, conversion);
1097 connect_nodes(input, conversion);
1100 // Re-sort topologically, and propagate the new information.
1101 propagate_gamma_and_color_space();
1109 sprintf(filename, "step%u-alphafix-iter%u.dot", step, ++alpha_propagation_pass);
1110 output_dot(filename);
1111 assert(alpha_propagation_pass < 100);
1112 } while (found_any);
1114 for (unsigned i = 0; i < nodes.size(); ++i) {
1115 Node *node = nodes[i];
1116 if (node->disabled) {
1119 assert(node->output_alpha_type != ALPHA_INVALID);
1123 // Make so that the output is in the desired color space.
1124 void EffectChain::fix_output_color_space()
1126 Node *output = find_output_node();
1127 if (output->output_color_space != output_format.color_space) {
1128 Node *conversion = add_node(new ColorspaceConversionEffect());
1129 CHECK(conversion->effect->set_int("source_space", output->output_color_space));
1130 CHECK(conversion->effect->set_int("destination_space", output_format.color_space));
1131 conversion->output_color_space = output_format.color_space;
1132 connect_nodes(output, conversion);
1134 propagate_gamma_and_color_space();
1138 // Make so that the output is in the desired pre-/postmultiplication alpha state.
1139 void EffectChain::fix_output_alpha()
1141 Node *output = find_output_node();
1142 assert(output->output_alpha_type != ALPHA_INVALID);
1143 if (output->output_alpha_type == ALPHA_BLANK) {
1144 // No alpha output, so we don't care.
1147 if (output->output_alpha_type == ALPHA_PREMULTIPLIED &&
1148 output_alpha_format == OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED) {
1149 Node *conversion = add_node(new AlphaDivisionEffect());
1150 connect_nodes(output, conversion);
1152 propagate_gamma_and_color_space();
1154 if (output->output_alpha_type == ALPHA_POSTMULTIPLIED &&
1155 output_alpha_format == OUTPUT_ALPHA_FORMAT_PREMULTIPLIED) {
1156 Node *conversion = add_node(new AlphaMultiplicationEffect());
1157 connect_nodes(output, conversion);
1159 propagate_gamma_and_color_space();
1163 bool EffectChain::node_needs_gamma_fix(Node *node)
1165 if (node->disabled) {
1169 // Small hack since the output is not an explicit node:
1170 // If we are the last node and our output is in the wrong
1171 // space compared to EffectChain's output, we need to fix it.
1172 // This will only take us to linear, but fix_output_gamma()
1173 // will come and take us to the desired output gamma
1176 // This needs to be before everything else, since it could
1177 // even apply to inputs (if they are the only effect).
1178 if (node->outgoing_links.empty() &&
1179 node->output_gamma_curve != output_format.gamma_curve &&
1180 node->output_gamma_curve != GAMMA_LINEAR) {
1184 if (node->effect->num_inputs() == 0) {
1188 // propagate_gamma_and_color_space() has already set our output
1189 // to GAMMA_INVALID if the inputs differ, so we can rely on that,
1190 // except for GammaCompressionEffect.
1191 if (node->output_gamma_curve == GAMMA_INVALID) {
1194 if (node->effect->effect_type_id() == "GammaCompressionEffect") {
1195 assert(node->incoming_links.size() == 1);
1196 return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
1199 return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
1202 // Very similar to fix_internal_color_spaces(), but for gamma.
1203 // There is one difference, though; before we start adding conversion nodes,
1204 // we see if we can get anything out of asking the sources to deliver
1205 // linear gamma directly. fix_internal_gamma_by_asking_inputs()
1206 // does that part, while fix_internal_gamma_by_inserting_nodes()
1207 // inserts nodes as needed afterwards.
1208 void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
1210 unsigned gamma_propagation_pass = 0;
1214 for (unsigned i = 0; i < nodes.size(); ++i) {
1215 Node *node = nodes[i];
1216 if (!node_needs_gamma_fix(node)) {
1220 // See if all inputs can give us linear gamma. If not, leave it.
1221 std::vector<Node *> nonlinear_inputs;
1222 find_all_nonlinear_inputs(node, &nonlinear_inputs);
1223 assert(!nonlinear_inputs.empty());
1226 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1227 Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
1228 all_ok &= input->can_output_linear_gamma();
1235 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
1236 CHECK(nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1));
1237 nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
1240 // Re-sort topologically, and propagate the new information.
1241 propagate_gamma_and_color_space();
1248 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1249 output_dot(filename);
1250 assert(gamma_propagation_pass < 100);
1251 } while (found_any);
1254 void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
1256 unsigned gamma_propagation_pass = 0;
1260 for (unsigned i = 0; i < nodes.size(); ++i) {
1261 Node *node = nodes[i];
1262 if (!node_needs_gamma_fix(node)) {
1266 // Special case: We could be an input and still be asked to
1267 // fix our gamma; if so, we should be the only node
1268 // (as node_needs_gamma_fix() would only return true in
1269 // for an input in that case). That means we should insert
1270 // a conversion node _after_ ourselves.
1271 if (node->incoming_links.empty()) {
1272 assert(node->outgoing_links.empty());
1273 Node *conversion = add_node(new GammaExpansionEffect());
1274 CHECK(conversion->effect->set_int("source_curve", node->output_gamma_curve));
1275 conversion->output_gamma_curve = GAMMA_LINEAR;
1276 connect_nodes(node, conversion);
1279 // If not, go through each input that is not linear gamma,
1280 // and insert a gamma conversion after it.
1281 for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
1282 Node *input = node->incoming_links[j];
1283 assert(input->output_gamma_curve != GAMMA_INVALID);
1284 if (input->output_gamma_curve == GAMMA_LINEAR) {
1287 Node *conversion = add_node(new GammaExpansionEffect());
1288 CHECK(conversion->effect->set_int("source_curve", input->output_gamma_curve));
1289 conversion->output_gamma_curve = GAMMA_LINEAR;
1290 replace_sender(input, conversion);
1291 connect_nodes(input, conversion);
1294 // Re-sort topologically, and propagate the new information.
1296 propagate_gamma_and_color_space();
1303 sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
1304 output_dot(filename);
1305 assert(gamma_propagation_pass < 100);
1306 } while (found_any);
1308 for (unsigned i = 0; i < nodes.size(); ++i) {
1309 Node *node = nodes[i];
1310 if (node->disabled) {
1313 assert(node->output_gamma_curve != GAMMA_INVALID);
1317 // Make so that the output is in the desired gamma.
1318 // Note that this assumes linear input gamma, so it might create the need
1319 // for another pass of fix_internal_gamma().
1320 void EffectChain::fix_output_gamma()
1322 Node *output = find_output_node();
1323 if (output->output_gamma_curve != output_format.gamma_curve) {
1324 Node *conversion = add_node(new GammaCompressionEffect());
1325 CHECK(conversion->effect->set_int("destination_curve", output_format.gamma_curve));
1326 conversion->output_gamma_curve = output_format.gamma_curve;
1327 connect_nodes(output, conversion);
1331 // If the user has requested dither, add a DitherEffect right at the end
1332 // (after GammaCompressionEffect etc.). This needs to be done after everything else,
1333 // since dither is about the only effect that can _not_ be done in linear space.
1334 void EffectChain::add_dither_if_needed()
1336 if (num_dither_bits == 0) {
1339 Node *output = find_output_node();
1340 Node *dither = add_node(new DitherEffect());
1341 CHECK(dither->effect->set_int("num_bits", num_dither_bits));
1342 connect_nodes(output, dither);
1344 dither_effect = dither->effect;
1347 // Find the output node. This is, simply, one that has no outgoing links.
1348 // If there are multiple ones, the graph is malformed (we do not support
1349 // multiple outputs right now).
1350 Node *EffectChain::find_output_node()
1352 std::vector<Node *> output_nodes;
1353 for (unsigned i = 0; i < nodes.size(); ++i) {
1354 Node *node = nodes[i];
1355 if (node->disabled) {
1358 if (node->outgoing_links.empty()) {
1359 output_nodes.push_back(node);
1362 assert(output_nodes.size() == 1);
1363 return output_nodes[0];
1366 void EffectChain::finalize()
1368 // Save the current locale, and set it to C, so that we can output decimal
1369 // numbers with printf and be sure to get them in the format mandated by GLSL.
1370 char *saved_locale = setlocale(LC_NUMERIC, "C");
1372 // Output the graph as it is before we do any conversions on it.
1373 output_dot("step0-start.dot");
1375 // Give each effect in turn a chance to rewrite its own part of the graph.
1376 // Note that if more effects are added as part of this, they will be
1377 // picked up as part of the same for loop, since they are added at the end.
1378 for (unsigned i = 0; i < nodes.size(); ++i) {
1379 nodes[i]->effect->rewrite_graph(this, nodes[i]);
1381 output_dot("step1-rewritten.dot");
1383 find_color_spaces_for_inputs();
1384 output_dot("step2-input-colorspace.dot");
1387 output_dot("step3-propagated-alpha.dot");
1389 propagate_gamma_and_color_space();
1390 output_dot("step4-propagated-all.dot");
1392 fix_internal_color_spaces();
1393 fix_internal_alpha(6);
1394 fix_output_color_space();
1395 output_dot("step7-output-colorspacefix.dot");
1397 output_dot("step8-output-alphafix.dot");
1399 // Note that we need to fix gamma after colorspace conversion,
1400 // because colorspace conversions might create needs for gamma conversions.
1401 // Also, we need to run an extra pass of fix_internal_gamma() after
1402 // fixing the output gamma, as we only have conversions to/from linear,
1403 // and fix_internal_alpha() since GammaCompressionEffect needs
1404 // postmultiplied input.
1405 fix_internal_gamma_by_asking_inputs(9);
1406 fix_internal_gamma_by_inserting_nodes(10);
1408 output_dot("step11-output-gammafix.dot");
1410 output_dot("step12-output-alpha-propagated.dot");
1411 fix_internal_alpha(13);
1412 output_dot("step14-output-alpha-fixed.dot");
1413 fix_internal_gamma_by_asking_inputs(15);
1414 fix_internal_gamma_by_inserting_nodes(16);
1416 output_dot("step17-before-dither.dot");
1418 add_dither_if_needed();
1420 output_dot("step18-final.dot");
1422 // Construct all needed GLSL programs, starting at the output.
1423 construct_glsl_programs(find_output_node());
1425 output_dot("step19-split-to-phases.dot");
1427 // If we have more than one phase, we need intermediate render-to-texture.
1428 // Construct an FBO, and then as many textures as we need.
1429 // We choose the simplest option of having one texture per output,
1430 // since otherwise this turns into an (albeit simple)
1431 // register allocation problem.
1432 if (phases.size() > 1) {
1433 glGenFramebuffers(1, &fbo);
1435 for (unsigned i = 0; i < phases.size() - 1; ++i) {
1436 inform_input_sizes(phases[i]);
1437 find_output_size(phases[i]);
1439 Node *output_node = phases[i]->effects.back();
1440 glGenTextures(1, &output_node->output_texture);
1442 glBindTexture(GL_TEXTURE_2D, output_node->output_texture);
1444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1448 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[i]->output_width, phases[i]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1451 output_node->output_texture_width = phases[i]->output_width;
1452 output_node->output_texture_height = phases[i]->output_height;
1454 inform_input_sizes(phases.back());
1457 for (unsigned i = 0; i < inputs.size(); ++i) {
1458 inputs[i]->finalize();
1461 assert(phases[0]->inputs.empty());
1464 setlocale(LC_NUMERIC, saved_locale);
1467 void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height)
1471 // Save original viewport.
1472 GLuint x = 0, y = 0;
1474 if (width == 0 && height == 0) {
1476 glGetIntegerv(GL_VIEWPORT, viewport);
1479 width = viewport[2];
1480 height = viewport[3];
1484 glDisable(GL_BLEND);
1486 glDisable(GL_DEPTH_TEST);
1488 glDepthMask(GL_FALSE);
1491 glMatrixMode(GL_PROJECTION);
1493 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
1495 glMatrixMode(GL_MODELVIEW);
1498 if (phases.size() > 1) {
1499 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1503 std::set<Node *> generated_mipmaps;
1505 for (unsigned phase = 0; phase < phases.size(); ++phase) {
1506 // See if the requested output size has changed. If so, we need to recreate
1507 // the texture (and before we start setting up inputs).
1508 inform_input_sizes(phases[phase]);
1509 if (phase != phases.size() - 1) {
1510 find_output_size(phases[phase]);
1512 Node *output_node = phases[phase]->effects.back();
1514 if (output_node->output_texture_width != phases[phase]->output_width ||
1515 output_node->output_texture_height != phases[phase]->output_height) {
1516 glActiveTexture(GL_TEXTURE0);
1518 glBindTexture(GL_TEXTURE_2D, output_node->output_texture);
1520 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1522 glBindTexture(GL_TEXTURE_2D, 0);
1525 output_node->output_texture_width = phases[phase]->output_width;
1526 output_node->output_texture_height = phases[phase]->output_height;
1530 glUseProgram(phases[phase]->glsl_program_num);
1533 // Set up RTT inputs for this phase.
1534 for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
1535 glActiveTexture(GL_TEXTURE0 + sampler);
1536 Node *input = phases[phase]->inputs[sampler];
1537 glBindTexture(GL_TEXTURE_2D, input->output_texture);
1539 if (phases[phase]->input_needs_mipmaps) {
1540 if (generated_mipmaps.count(input) == 0) {
1541 glGenerateMipmap(GL_TEXTURE_2D);
1543 generated_mipmaps.insert(input);
1545 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1548 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1552 std::string texture_name = std::string("tex_") + input->effect_id;
1553 glUniform1i(glGetUniformLocation(phases[phase]->glsl_program_num, texture_name.c_str()), sampler);
1557 // And now the output.
1558 if (phase == phases.size() - 1) {
1559 // Last phase goes to the output the user specified.
1560 glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
1562 glViewport(x, y, width, height);
1563 if (dither_effect != NULL) {
1564 CHECK(dither_effect->set_int("output_width", width));
1565 CHECK(dither_effect->set_int("output_height", height));
1568 Node *output_node = phases[phase]->effects.back();
1569 glFramebufferTexture2D(
1571 GL_COLOR_ATTACHMENT0,
1573 output_node->output_texture,
1576 glViewport(0, 0, phases[phase]->output_width, phases[phase]->output_height);
1579 // Give the required parameters to all the effects.
1580 unsigned sampler_num = phases[phase]->inputs.size();
1581 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1582 Node *node = phases[phase]->effects[i];
1583 node->effect->set_gl_state(phases[phase]->glsl_program_num, node->effect_id, &sampler_num);
1590 glTexCoord2f(0.0f, 0.0f);
1591 glVertex2f(0.0f, 0.0f);
1593 glTexCoord2f(1.0f, 0.0f);
1594 glVertex2f(1.0f, 0.0f);
1596 glTexCoord2f(1.0f, 1.0f);
1597 glVertex2f(1.0f, 1.0f);
1599 glTexCoord2f(0.0f, 1.0f);
1600 glVertex2f(0.0f, 1.0f);
1605 for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
1606 Node *node = phases[phase]->effects[i];
1607 node->effect->clear_gl_state();