]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Add a mirror effect.
[movit] / effect_chain.cpp
index a8381505109affe8ade2becd09009c58b15b1233..f860cad283e7983b543beb2e4cfbb93501e5f4cb 100644 (file)
 #include "util.h"
 #include "effect_chain.h"
 #include "gamma_expansion_effect.h"
+#include "gamma_compression_effect.h"
 #include "lift_gamma_gain_effect.h"
 #include "colorspace_conversion_effect.h"
+#include "saturation_effect.h"
+#include "mirror_effect.h"
+#include "vignette_effect.h"
 #include "texture_enum.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
-       : width(width), height(height), finalized(false) {}
+       : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
 
 void EffectChain::add_input(const ImageFormat &format)
 {
@@ -32,34 +36,57 @@ void EffectChain::add_output(const ImageFormat &format)
 Effect *instantiate_effect(EffectId effect)
 {
        switch (effect) {
-       case GAMMA_CONVERSION:
+       case EFFECT_GAMMA_EXPANSION:
                return new GammaExpansionEffect();
-       case RGB_PRIMARIES_CONVERSION:
-               return new GammaExpansionEffect();
-       case LIFT_GAMMA_GAIN:
+       case EFFECT_GAMMA_COMPRESSION:
+               return new GammaCompressionEffect();
+       case EFFECT_COLOR_SPACE_CONVERSION:
+               return new ColorSpaceConversionEffect();
+       case EFFECT_LIFT_GAMMA_GAIN:
                return new LiftGammaGainEffect();
+       case EFFECT_SATURATION:
+               return new SaturationEffect();
+       case EFFECT_MIRROR:
+               return new MirrorEffect();
+       case EFFECT_VIGNETTE:
+               return new VignetteEffect();
        }
        assert(false);
 }
 
+void EffectChain::normalize_to_linear_gamma()
+{
+       if (current_gamma_curve == GAMMA_sRGB) {
+               // TODO: check if the extension exists
+               use_srgb_texture_format = true;
+       } else {
+               GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
+               gamma_conversion->set_int("source_curve", current_gamma_curve);
+               effects.push_back(gamma_conversion);
+       }
+       current_gamma_curve = GAMMA_LINEAR;
+}
+
+void EffectChain::normalize_to_srgb()
+{
+       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);
+       current_color_space = COLORSPACE_sRGB;
+}
+
 Effect *EffectChain::add_effect(EffectId effect_id)
 {
        Effect *effect = instantiate_effect(effect_id);
 
        if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
-               GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
-               gamma_conversion->set_int("source_curve", current_gamma_curve);
-               effects.push_back(gamma_conversion);
-               current_gamma_curve = GAMMA_LINEAR;
+               normalize_to_linear_gamma();
        }
 
        if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
-               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);
-               current_color_space = COLORSPACE_sRGB;
+               normalize_to_srgb();
        }
 
        // not handled yet
@@ -113,8 +140,44 @@ std::string replace_prefix(const std::string &text, const std::string &prefix)
 
 void EffectChain::finalize()
 {
-       std::string frag_shader = read_file("header.glsl");
+       if (current_color_space != output_format.color_space) {
+               ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
+               colorspace_conversion->set_int("source_space", current_color_space);
+               colorspace_conversion->set_int("destination_space", output_format.color_space);
+               effects.push_back(colorspace_conversion);
+               current_color_space = output_format.color_space;
+       }
+
+       if (current_gamma_curve != output_format.gamma_curve) {
+               if (current_gamma_curve != GAMMA_LINEAR) {
+                       normalize_to_linear_gamma();
+               }
+               assert(current_gamma_curve == GAMMA_LINEAR);
+               GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
+               gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
+               effects.push_back(gamma_conversion);
+               current_gamma_curve = output_format.gamma_curve;
+       }
+       
+       std::string vert_shader = read_file("header.vert");
+       for (unsigned i = 0; i < effects.size(); ++i) {
+               char effect_id[256];
+               sprintf(effect_id, "eff%d", i);
+       
+               vert_shader += "\n";
+               vert_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
+               vert_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
+               vert_shader += replace_prefix(effects[i]->output_vertex_shader(), effect_id);
+               vert_shader += "#undef PREFIX\n";
+               vert_shader += "#undef FUNCNAME\n";
+               vert_shader += "#undef LAST_INPUT\n";
+               vert_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
+               vert_shader += "\n";
+       }
+       vert_shader.append(read_file("footer.vert"));
+       printf("%s\n", vert_shader.c_str());
 
+       std::string frag_shader = read_file("header.frag");
        for (unsigned i = 0; i < effects.size(); ++i) {
                char effect_id[256];
                sprintf(effect_id, "eff%d", i);
@@ -122,19 +185,19 @@ void EffectChain::finalize()
                frag_shader += "\n";
                frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
                frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
-               frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
+               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 += "\n";
        }
-       frag_shader.append(read_file("footer.glsl"));
+       frag_shader.append(read_file("footer.frag"));
        printf("%s\n", frag_shader.c_str());
        
        glsl_program_num = glCreateProgram();
-       GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
-       GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
+       GLuint vs_obj = compile_shader(vert_shader, GL_VERTEX_SHADER);
+       GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
        glAttachShader(glsl_program_num, vs_obj);
        check_error();
        glAttachShader(glsl_program_num, fs_obj);
@@ -142,6 +205,46 @@ void EffectChain::finalize()
        glLinkProgram(glsl_program_num);
        check_error();
 
+       // Translate the format to OpenGL's enums.
+       GLenum internal_format;
+       if (use_srgb_texture_format) {
+               internal_format = GL_SRGB8;
+       } else {
+               internal_format = GL_RGBA8;
+       }
+       if (input_format.pixel_format == FORMAT_RGB) {
+               format = GL_RGB;
+               bytes_per_pixel = 3;
+       } else if (input_format.pixel_format == FORMAT_RGBA) {
+               format = GL_RGBA;
+               bytes_per_pixel = 4;
+       } else if (input_format.pixel_format == FORMAT_BGR) {
+               format = GL_BGR;
+               bytes_per_pixel = 3;
+       } else if (input_format.pixel_format == FORMAT_BGRA) {
+               format = GL_BGRA;
+               bytes_per_pixel = 4;
+       } else {
+               assert(false);
+       }
+
+       // Create PBO to hold the texture, and then the texture itself.
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
+       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);
+       
+       glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
+       check_error();
+       glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
+       check_error();
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
+       check_error();
+
        finalized = true;
 }
 
@@ -149,27 +252,35 @@ void EffectChain::render_to_screen(unsigned char *src)
 {
        assert(finalized);
 
+       // Copy the pixel data into the PBO.
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
        check_error();
-       glUseProgram(glsl_program_num);
+       void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
+       memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
+       glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
        check_error();
 
+       // Re-upload the texture from the PBO.
        glActiveTexture(GL_TEXTURE0);
+       check_error();
        glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
+       check_error();
+       glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
+       check_error();
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
+       check_error();
+
+       glUseProgram(glsl_program_num);
+       check_error();
 
-       // TODO: use sRGB textures if applicable
-       if (input_format.pixel_format == FORMAT_RGB) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
-       } else if (input_format.pixel_format == FORMAT_RGBA) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
-       } else {
-               assert(false);
-       }
        check_error();
        glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
 
-       //for (unsigned i = 0; i < effects.size(); ++i) {
-       //      effects[i]->set_uniforms();
-       //}
+       for (unsigned i = 0; i < effects.size(); ++i) {
+               char effect_id[256];
+               sprintf(effect_id, "eff%d", i);
+               effects[i]->set_uniforms(glsl_program_num, effect_id);
+       }
 
        glDisable(GL_BLEND);
        check_error();