Slight cleanup in texture upload format selection.
[movit] / effect_chain.cpp
index 56d470fb572e7764e0c78d02bf8d8a0e0a08f506..85832db88541cfb90748dd55f8aca171a6621673 100644 (file)
 #include "gamma_compression_effect.h"
 #include "lift_gamma_gain_effect.h"
 #include "colorspace_conversion_effect.h"
+#include "saturation_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)
 {
@@ -33,21 +35,32 @@ 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_VIGNETTE:
+               return new VignetteEffect();
        }
        assert(false);
 }
 
 void EffectChain::normalize_to_linear_gamma()
 {
-       GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
-       gamma_conversion->set_int("source_curve", current_gamma_curve);
-       effects.push_back(gamma_conversion);
+       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;
 }
 
@@ -124,7 +137,13 @@ std::string replace_prefix(const std::string &text, const std::string &prefix)
 
 void EffectChain::finalize()
 {
-       // TODO: If we want a non-sRGB output color space, convert.
+       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) {
@@ -157,8 +176,8 @@ void EffectChain::finalize()
        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(read_file("vs.glsl"), 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);
@@ -180,20 +199,29 @@ void EffectChain::render_to_screen(unsigned char *src)
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
 
-       // TODO: use sRGB textures if applicable
+       GLenum format, internal_format;
+       if (use_srgb_texture_format) {
+               internal_format = GL_SRGB8;
+       } else {
+               internal_format = GL_RGBA8;
+       }
        if (input_format.pixel_format == FORMAT_RGB) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
+               format = GL_RGB;
        } else if (input_format.pixel_format == FORMAT_RGBA) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
+               format = GL_RGBA;
        } else {
                assert(false);
        }
+
+       glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, src);
        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();