]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Allow an effect to have multiple inputs (although the graph still supports only one...
[movit] / effect_chain.cpp
index 85c8be374a0702118c74ce2faa3c9c5e6d175674..fc20ae117ce4f1adf8e24b49461f13063b97581d 100644 (file)
 #include "gamma_compression_effect.h"
 #include "lift_gamma_gain_effect.h"
 #include "colorspace_conversion_effect.h"
+#include "sandbox_effect.h"
 #include "saturation_effect.h"
 #include "mirror_effect.h"
 #include "vignette_effect.h"
 #include "blur_effect.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
-       : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
+       : width(width),
+         height(height),
+         last_added_effect(NULL),
+         use_srgb_texture_format(false),
+         finalized(false) {}
 
 void EffectChain::add_input(const ImageFormat &format)
 {
        input_format = format;
-       current_color_space = format.color_space;
-       current_gamma_curve = format.gamma_curve;
+       output_color_space.insert(std::make_pair(static_cast<Effect *>(NULL), format.color_space));
+       output_gamma_curve.insert(std::make_pair(static_cast<Effect *>(NULL), format.gamma_curve));
 }
 
 void EffectChain::add_output(const ImageFormat &format)
 {
        output_format = format;
 }
-       
+
+void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
+{
+       effects.push_back(effect);
+       assert(inputs.size() == effect->num_inputs());
+       for (unsigned i = 0; i < inputs.size(); ++i) {
+               outgoing_links.insert(std::make_pair(inputs[i], effect));
+               incoming_links.insert(std::make_pair(effect, inputs[i]));
+       }
+       last_added_effect = effect;
+}
+
 Effect *instantiate_effect(EffectId effect)
 {
        switch (effect) {
@@ -42,6 +58,8 @@ Effect *instantiate_effect(EffectId effect)
                return new GammaCompressionEffect();
        case EFFECT_COLOR_SPACE_CONVERSION:
                return new ColorSpaceConversionEffect();
+       case EFFECT_SANDBOX:
+               return new SandboxEffect();
        case EFFECT_LIFT_GAMMA_GAIN:
                return new LiftGammaGainEffect();
        case EFFECT_SATURATION:
@@ -56,42 +74,57 @@ Effect *instantiate_effect(EffectId effect)
        assert(false);
 }
 
-void EffectChain::normalize_to_linear_gamma()
+Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
 {
+       GammaCurve current_gamma_curve = output_gamma_curve[input];
        if (current_gamma_curve == GAMMA_sRGB) {
                // TODO: check if the extension exists
                use_srgb_texture_format = true;
+               current_gamma_curve = GAMMA_LINEAR;
+               return input;
        } else {
                GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
                gamma_conversion->set_int("source_curve", current_gamma_curve);
-               effects.push_back(gamma_conversion);
+               std::vector<Effect *> inputs;
+               inputs.push_back(input);
+               gamma_conversion->add_self_to_effect_chain(this, inputs);
+               current_gamma_curve = GAMMA_LINEAR;
+               return gamma_conversion;
        }
-       current_gamma_curve = GAMMA_LINEAR;
 }
 
-void EffectChain::normalize_to_srgb()
+Effect *EffectChain::normalize_to_srgb(Effect *input)
 {
+       GammaCurve current_gamma_curve = output_gamma_curve[input];
+       ColorSpace current_color_space = output_color_space[input];
        assert(current_gamma_curve == GAMMA_LINEAR);
        ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
        colorspace_conversion->set_int("source_space", current_color_space);
        colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
-       effects.push_back(colorspace_conversion);
+       std::vector<Effect *> inputs;
+       inputs.push_back(input);
+       colorspace_conversion->add_self_to_effect_chain(this, inputs);
        current_color_space = COLORSPACE_sRGB;
+       return colorspace_conversion;
 }
 
-Effect *EffectChain::add_effect(EffectId effect_id)
+Effect *EffectChain::add_effect(EffectId effect_id, const std::vector<Effect *> &inputs)
 {
        Effect *effect = instantiate_effect(effect_id);
 
-       if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
-               normalize_to_linear_gamma();
-       }
+       assert(inputs.size() == effect->num_inputs());
 
-       if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
-               normalize_to_srgb();
+       std::vector<Effect *> normalized_inputs = inputs;
+       for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
+               if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
+                       normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
+               }
+               if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
+                       normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
+               }
        }
 
-       effects.push_back(effect);
+       effect->add_self_to_effect_chain(this, normalized_inputs);
        return effect;
 }
 
@@ -150,8 +183,8 @@ EffectChain::Phase EffectChain::compile_glsl_program(unsigned start_index, unsig
                frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id);
                frag_shader += "#undef PREFIX\n";
                frag_shader += "#undef FUNCNAME\n";
-               frag_shader += "#undef LAST_INPUT\n";
-               frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
+               frag_shader += "#undef INPUT\n";
+               frag_shader += std::string("#define INPUT ") + effect_id + "\n";
                frag_shader += "\n";
 
                input_needs_mipmaps |= effects[i]->needs_mipmaps();
@@ -181,6 +214,8 @@ EffectChain::Phase EffectChain::compile_glsl_program(unsigned start_index, unsig
 void EffectChain::finalize()
 {
        // Add normalizers to get the output format right.
+       GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect];  // FIXME
+       ColorSpace current_color_space = output_color_space[last_added_effect];  // FIXME
        if (current_color_space != output_format.color_space) {
                ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
                colorspace_conversion->set_int("source_space", current_color_space);
@@ -190,7 +225,7 @@ void EffectChain::finalize()
        }
        if (current_gamma_curve != output_format.gamma_curve) {
                if (current_gamma_curve != GAMMA_LINEAR) {
-                       normalize_to_linear_gamma();
+                       normalize_to_linear_gamma(last_added_effect);  // FIXME
                }
                assert(current_gamma_curve == GAMMA_LINEAR);
                GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
@@ -205,7 +240,7 @@ void EffectChain::finalize()
        // and of course at the end.
        unsigned start = 0;
        for (unsigned i = 0; i < effects.size(); ++i) {
-               if (effects[i]->needs_many_samples() && i != start) {
+               if (effects[i]->needs_texture_bounce() && i != start) {
                        phases.push_back(compile_glsl_program(start, i));
                        start = i;
                }
@@ -220,8 +255,6 @@ void EffectChain::finalize()
                unsigned num_textures = std::max<int>(phases.size() - 1, 2);
                glGenTextures(num_textures, temp_textures);
 
-               unsigned char *empty = new unsigned char[width * height * 4];
-               memset(empty, 0, width * height * 4);
                for (unsigned i = 0; i < num_textures; ++i) {
                        glBindTexture(GL_TEXTURE_2D, temp_textures[i]);
                        check_error();
@@ -229,10 +262,9 @@ void EffectChain::finalize()
                        check_error();
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
                        check_error();
-                       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, empty);
+                       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
                        check_error();
                }
-               delete[] empty;
        }
        
        // Translate the input format to OpenGL's enums.
@@ -263,10 +295,7 @@ void EffectChain::finalize()
        check_error();
        glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
        check_error();
-
-       void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
-       memset(mapped_pbo, 0, width * height * bytes_per_pixel);
-       glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
        check_error();
        
        glGenTextures(1, &source_image_num);
@@ -275,9 +304,10 @@ void EffectChain::finalize()
        check_error();
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        check_error();
-       glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
+       // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here.
+       glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, phases[0].input_needs_mipmaps ? GL_TRUE : GL_FALSE);
        check_error();
-       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
+       glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
        check_error();
 
        finalized = true;
@@ -339,8 +369,11 @@ void EffectChain::render_to_screen(unsigned char *src)
                        check_error();
                }
                if (phases[phase].input_needs_mipmaps) {
-                       glGenerateMipmap(GL_TEXTURE_2D);
-                       check_error();
+                       if (phase != 0) {
+                               // For phase 0, it's done further up.
+                               glGenerateMipmap(GL_TEXTURE_2D);
+                               check_error();
+                       }
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
                        check_error();
                } else {