]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Defer fetching inputs' color spaces and gamma to finalize().
[movit] / effect_chain.cpp
index 84b5c4e74f3f3a1cf7bbfd32beb89c165c79a8c2..cb7c52caa6cbd831642c1960e8bf701aca277684 100644 (file)
@@ -18,6 +18,7 @@
 #include "colorspace_conversion_effect.h"
 #include "dither_effect.h"
 #include "input.h"
+#include "init.h"
 
 EffectChain::EffectChain(float aspect_nom, float aspect_denom)
        : aspect_nom(aspect_nom),
@@ -50,10 +51,7 @@ EffectChain::~EffectChain()
 Input *EffectChain::add_input(Input *input)
 {
        inputs.push_back(input);
-
-       Node *node = add_node(input);
-       node->output_color_space = input->get_color_space();
-       node->output_gamma_curve = input->get_gamma_curve();
+       add_node(input);
        return input;
 }
 
@@ -268,19 +266,19 @@ Phase *EffectChain::compile_glsl_program(
        frag_shader += std::string("#define INPUT ") + effects.back()->effect_id + "\n";
        frag_shader.append(read_file("footer.frag"));
 
-#ifndef NDEBUG
-       // Output shader to a temporary file, for easier debugging.
-       static int compiled_shader_num = 0;
-       char filename[256];
-       sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
-       FILE *fp = fopen(filename, "w");
-       if (fp == NULL) {
-               perror(filename);
-               exit(1);
+       if (movit_debug_level == MOVIT_DEBUG_ON) {
+               // Output shader to a temporary file, for easier debugging.
+               static int compiled_shader_num = 0;
+               char filename[256];
+               sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
+               FILE *fp = fopen(filename, "w");
+               if (fp == NULL) {
+                       perror(filename);
+                       exit(1);
+               }
+               fprintf(fp, "%s\n", frag_shader.c_str());
+               fclose(fp);
        }
-       fprintf(fp, "%s\n", frag_shader.c_str());
-       fclose(fp);
-#endif
        
        GLuint glsl_program_num = glCreateProgram();
        GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
@@ -429,9 +427,9 @@ void EffectChain::construct_glsl_programs(Node *output)
 
 void EffectChain::output_dot(const char *filename)
 {
-#ifdef NDEBUG
-       return;
-#endif
+       if (movit_debug_level != MOVIT_DEBUG_ON) {
+               return;
+       }
 
        FILE *fp = fopen(filename, "w");
        if (fp == NULL) {
@@ -647,6 +645,21 @@ void EffectChain::topological_sort_visit_node(Node *node, std::set<Node *> *visi
        sorted_list->push_back(node);
 }
 
+void EffectChain::find_color_spaces_for_inputs()
+{
+       for (unsigned i = 0; i < nodes.size(); ++i) {
+               Node *node = nodes[i];
+               if (node->disabled) {
+                       continue;
+               }
+               if (node->incoming_links.size() == 0) {
+                       Input *input = static_cast<Input *>(node->effect);
+                       node->output_color_space = input->get_color_space();
+                       node->output_gamma_curve = input->get_gamma_curve();
+               }
+       }
+}
+
 // Propagate gamma and color space information as far as we can in the graph.
 // The rules are simple: Anything where all the inputs agree, get that as
 // output as well. Anything else keeps having *_INVALID.
@@ -991,9 +1004,12 @@ void EffectChain::finalize()
        }
        output_dot("step1-rewritten.dot");
 
-       propagate_gamma_and_color_space();
+       find_color_spaces_for_inputs();
        output_dot("step2-propagated.dot");
 
+       propagate_gamma_and_color_space();
+       output_dot("step3-propagated.dot");
+
        fix_internal_color_spaces();
        fix_output_color_space();
        output_dot("step4-output-colorspacefix.dot");