]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Fix the name of the .dot file for the newly created colorspace input pass.
[movit] / effect_chain.cpp
index 5b5e3675fea17a81430b21eec75e225e211228c5..1832b9ee7ce6364980eef747ae2d19d6411083ae 100644 (file)
@@ -4,6 +4,7 @@
 #include <math.h>
 #include <string.h>
 #include <assert.h>
+#include <GL/glew.h>
 
 #include <algorithm>
 #include <set>
 #include "gamma_expansion_effect.h"
 #include "gamma_compression_effect.h"
 #include "colorspace_conversion_effect.h"
+#include "dither_effect.h"
 #include "input.h"
-#include "opengl.h"
+#include "init.h"
 
 EffectChain::EffectChain(float aspect_nom, float aspect_denom)
        : aspect_nom(aspect_nom),
          aspect_denom(aspect_denom),
+         dither_effect(NULL),
          fbo(0),
+         num_dither_bits(0),
          finalized(false) {}
 
 EffectChain::~EffectChain()
@@ -47,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;
 }
 
@@ -265,17 +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"));
 
-       // 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);
        
        GLuint glsl_program_num = glCreateProgram();
        GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
@@ -424,6 +427,10 @@ void EffectChain::construct_glsl_programs(Node *output)
 
 void EffectChain::output_dot(const char *filename)
 {
+       if (movit_debug_level != MOVIT_DEBUG_ON) {
+               return;
+       }
+
        FILE *fp = fopen(filename, "w");
        if (fp == NULL) {
                perror(filename);
@@ -638,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.
@@ -933,6 +955,22 @@ void EffectChain::fix_output_gamma()
                connect_nodes(output, conversion);
        }
 }
+       
+// If the user has requested dither, add a DitherEffect right at the end
+// (after GammaCompressionEffect etc.). This needs to be done after everything else,
+// since dither is about the only effect that can _not_ be done in linear space.
+void EffectChain::add_dither_if_needed()
+{
+       if (num_dither_bits == 0) {
+               return;
+       }
+       Node *output = find_output_node();
+       Node *dither = add_node(new DitherEffect());
+       CHECK(dither->effect->set_int("num_bits", num_dither_bits));
+       connect_nodes(output, dither);
+
+       dither_effect = dither->effect;
+}
 
 // Find the output node. This is, simply, one that has no outgoing links.
 // If there are multiple ones, the graph is malformed (we do not support
@@ -966,8 +1004,11 @@ void EffectChain::finalize()
        }
        output_dot("step1-rewritten.dot");
 
+       find_color_spaces_for_inputs();
+       output_dot("step2-input-colorspace.dot");
+
        propagate_gamma_and_color_space();
-       output_dot("step2-propagated.dot");
+       output_dot("step3-propagated.dot");
 
        fix_internal_color_spaces();
        fix_output_color_space();
@@ -984,12 +1025,16 @@ void EffectChain::finalize()
        fix_internal_gamma_by_asking_inputs(8);
        fix_internal_gamma_by_inserting_nodes(9);
 
-       output_dot("step10-final.dot");
+       output_dot("step10-before-dither.dot");
+
+       add_dither_if_needed();
+
+       output_dot("step11-final.dot");
        
        // Construct all needed GLSL programs, starting at the output.
        construct_glsl_programs(find_output_node());
 
-       output_dot("step11-split-to-phases.dot");
+       output_dot("step12-split-to-phases.dot");
 
        // If we have more than one phase, we need intermediate render-to-texture.
        // Construct an FBO, and then as many textures as we need.
@@ -1126,6 +1171,10 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                        glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
                        check_error();
                        glViewport(x, y, width, height);
+                       if (dither_effect != NULL) {
+                               CHECK(dither_effect->set_int("output_width", width));
+                               CHECK(dither_effect->set_int("output_height", height));
+                       }
                } else {
                        Node *output_node = phases[phase]->effects.back();
                        glFramebufferTexture2D(