]> git.sesse.net Git - movit/commitdiff
Support multiple render phases (with FBOs and all), and make a sample blur effect...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Oct 2012 22:20:05 +0000 (00:20 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Oct 2012 22:20:05 +0000 (00:20 +0200)
Makefile
blur_effect.cpp [new file with mode: 0644]
blur_effect.frag [new file with mode: 0644]
blur_effect.h [new file with mode: 0644]
effect_chain.cpp
effect_chain.h
effect_id.h
main.cpp

index 917d2bd9970e0a136354810776e0087e612a9b1a..b2cb05971f1a2f0093226e786cb0c4de53cf5bb4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CXX=g++
 CXXFLAGS=-Wall -g
 LDFLAGS=-lSDL -lSDL_image -lGL
 OBJS=main.o util.o widgets.o effect.o effect_chain.o
 CXXFLAGS=-Wall -g
 LDFLAGS=-lSDL -lSDL_image -lGL
 OBJS=main.o util.o widgets.o effect.o effect_chain.o
-OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o saturation_effect.o vignette_effect.o mirror_effect.o
+OBJS += lift_gamma_gain_effect.o gamma_expansion_effect.o gamma_compression_effect.o colorspace_conversion_effect.o saturation_effect.o vignette_effect.o mirror_effect.o blur_effect.o
 
 test: $(OBJS)
        $(CXX) -o test $(OBJS) $(LDFLAGS)
 
 test: $(OBJS)
        $(CXX) -o test $(OBJS) $(LDFLAGS)
diff --git a/blur_effect.cpp b/blur_effect.cpp
new file mode 100644 (file)
index 0000000..e945295
--- /dev/null
@@ -0,0 +1,24 @@
+#define GL_GLEXT_PROTOTYPES 1
+
+#include <math.h>
+#include <GL/gl.h>
+#include <GL/glext.h>
+
+#include "blur_effect.h"
+#include "util.h"
+
+BlurEffect::BlurEffect()
+       : radius(0.3f)
+{
+       register_float("radius", (float *)&radius);
+}
+
+std::string BlurEffect::output_fragment_shader()
+{
+       return read_file("blur_effect.frag");
+}
+
+void BlurEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+{
+       Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
+}
diff --git a/blur_effect.frag b/blur_effect.frag
new file mode 100644 (file)
index 0000000..bfa1d16
--- /dev/null
@@ -0,0 +1,17 @@
+// A simple, very stupid horizontal blur. Will be fixed soonish.
+
+vec4 FUNCNAME(vec2 tc) {
+       vec4 x = LAST_INPUT(tc);
+       return
+               vec4(0.1) * LAST_INPUT(tc + vec2(-0.010f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2(-0.008f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2(-0.006f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2(-0.004f, 0.0)) +
+               vec4(0.2) * LAST_INPUT(tc + vec2(-0.002f, 0.0)) +
+               vec4(0.3) * LAST_INPUT(tc + vec2(-0.000f, 0.0)) +
+               vec4(0.2) * LAST_INPUT(tc + vec2( 0.002f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2( 0.004f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2( 0.006f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2( 0.008f, 0.0)) +
+               vec4(0.1) * LAST_INPUT(tc + vec2( 0.010f, 0.0));
+}
diff --git a/blur_effect.h b/blur_effect.h
new file mode 100644 (file)
index 0000000..0657136
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef _BLUR_EFFECT_H
+#define _BLUR_EFFECT_H 1
+
+#include "effect.h"
+
+class BlurEffect : public Effect {
+public:
+       BlurEffect();
+       std::string output_fragment_shader();
+
+       virtual bool needs_many_samples() const { return true; }
+       virtual bool needs_mipmaps() const { return true; }
+
+       void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+
+private:
+       float radius;
+};
+
+#endif // !defined(_BLUR_EFFECT_H)
index 9ff5fd812beb934ba9467a805416e7851c379a11..899c6a3f4ca9335e518cd3939a452cafe6955801 100644 (file)
@@ -16,6 +16,7 @@
 #include "saturation_effect.h"
 #include "mirror_effect.h"
 #include "vignette_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) {}
 
 EffectChain::EffectChain(unsigned width, unsigned height)
        : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
@@ -49,6 +50,8 @@ Effect *instantiate_effect(EffectId effect)
                return new MirrorEffect();
        case EFFECT_VIGNETTE:
                return new VignetteEffect();
                return new MirrorEffect();
        case EFFECT_VIGNETTE:
                return new VignetteEffect();
+       case EFFECT_BLUR:
+               return new BlurEffect();
        }
        assert(false);
 }
        }
        assert(false);
 }
@@ -88,10 +91,6 @@ Effect *EffectChain::add_effect(EffectId effect_id)
                normalize_to_srgb();
        }
 
                normalize_to_srgb();
        }
 
-       // not handled yet
-       assert(!effect->needs_many_samples());
-       assert(!effect->needs_mipmaps());
-
        effects.push_back(effect);
        return effect;
 }
        effects.push_back(effect);
        return effect;
 }
@@ -137,29 +136,11 @@ std::string replace_prefix(const std::string &text, const std::string &prefix)
        return output;
 }
 
        return output;
 }
 
-void EffectChain::finalize()
+EffectChain::Phase EffectChain::compile_glsl_program(unsigned start_index, unsigned end_index)
 {
 {
-       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;
-       }
-       
+       bool input_needs_mipmaps = false;
        std::string frag_shader = read_file("header.frag");
        std::string frag_shader = read_file("header.frag");
-       for (unsigned i = 0; i < effects.size(); ++i) {
+       for (unsigned i = start_index; i < end_index; ++i) {
                char effect_id[256];
                sprintf(effect_id, "eff%d", i);
        
                char effect_id[256];
                sprintf(effect_id, "eff%d", i);
        
@@ -172,11 +153,13 @@ void EffectChain::finalize()
                frag_shader += "#undef LAST_INPUT\n";
                frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
                frag_shader += "\n";
                frag_shader += "#undef LAST_INPUT\n";
                frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
                frag_shader += "\n";
+
+               input_needs_mipmaps |= effects[i]->needs_mipmaps();
        }
        frag_shader.append(read_file("footer.frag"));
        printf("%s\n", frag_shader.c_str());
        
        }
        frag_shader.append(read_file("footer.frag"));
        printf("%s\n", frag_shader.c_str());
        
-       glsl_program_num = glCreateProgram();
+       GLuint glsl_program_num = glCreateProgram();
        GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
        GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
        glAttachShader(glsl_program_num, vs_obj);
        GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
        GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
        glAttachShader(glsl_program_num, vs_obj);
@@ -186,7 +169,68 @@ void EffectChain::finalize()
        glLinkProgram(glsl_program_num);
        check_error();
 
        glLinkProgram(glsl_program_num);
        check_error();
 
-       // Translate the format to OpenGL's enums.
+       Phase phase;
+       phase.glsl_program_num = glsl_program_num;
+       phase.input_needs_mipmaps = input_needs_mipmaps;
+       phase.start = start_index;
+       phase.end = end_index;
+
+       return phase;
+}
+
+void EffectChain::finalize()
+{
+       // Add normalizers to get the output format right.
+       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;
+       }
+
+       // Construct the GLSL programs. We end a program every time we come
+       // to an effect marked as "needs many samples" (ie. "please let me
+       // sample directly from a texture, with no arithmetic in-between"),
+       // 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) {
+                       phases.push_back(compile_glsl_program(start, i));
+                       start = i;
+               }
+       }
+       phases.push_back(compile_glsl_program(start, effects.size()));
+
+       // If we have more than one phase, we need intermediate render-to-texture.
+       // Construct an FBO, and then as many textures as we need.
+       if (phases.size() > 1) {
+               glGenFramebuffers(1, &fbo);
+
+               unsigned num_textures = std::max<int>(phases.size() - 1, 2);
+               glGenTextures(num_textures, temp_textures);
+
+               unsigned char *empty = new unsigned char[width * height * 4];
+               for (unsigned i = 0; i < num_textures; ++i) {
+                       glBindTexture(GL_TEXTURE_2D, temp_textures[i]);
+                       check_error();
+                       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, empty);
+                       check_error();
+               }
+               delete[] empty;
+       }
+       
+       // Translate the input format to OpenGL's enums.
        GLenum internal_format;
        if (use_srgb_texture_format) {
                internal_format = GL_SRGB8;
        GLenum internal_format;
        if (use_srgb_texture_format) {
                internal_format = GL_SRGB8;
@@ -209,7 +253,7 @@ void EffectChain::finalize()
                assert(false);
        }
 
                assert(false);
        }
 
-       // Create PBO to hold the texture, and then the texture itself.
+       // Create PBO to hold the texture holding the input image, 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);
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
        check_error();
        glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
@@ -252,22 +296,14 @@ void EffectChain::render_to_screen(unsigned char *src)
        check_error();
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
        check_error();
        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);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        check_error();
        check_error();
-
-       glUseProgram(glsl_program_num);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        check_error();
        check_error();
-
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
        check_error();
        check_error();
-       glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
-
-       unsigned sampler_num = 1;
-       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, &sampler_num);
-       }
 
 
+       // Basic state.
        glDisable(GL_BLEND);
        check_error();
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_BLEND);
        check_error();
        glDisable(GL_DEPTH_TEST);
@@ -282,20 +318,81 @@ void EffectChain::render_to_screen(unsigned char *src)
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
 
-       glBegin(GL_QUADS);
+       if (phases.size() > 1) {
+               glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+               check_error();
+       }
 
 
-       glTexCoord2f(0.0f, 1.0f);
-       glVertex2f(0.0f, 0.0f);
+       for (unsigned phase = 0; phase < phases.size(); ++phase) {
+               // Set up inputs and outputs for this phase.
+               if (phase == 0) {
+                       // First phase reads from the input texture (which is already bound).
+               } else {
+                       glBindTexture(GL_TEXTURE_2D, temp_textures[(phase + 1) % 2]);
+                       check_error();
+               }
+               if (phases[phase].input_needs_mipmaps) {
+                       glGenerateMipmap(GL_TEXTURE_2D);
+                       check_error();
+                       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+                       check_error();
+               } else {
+                       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+                       check_error();
+               }
 
 
-       glTexCoord2f(1.0f, 1.0f);
-       glVertex2f(1.0f, 0.0f);
+               if (phase == phases.size() - 1) {
+                       // Last phase goes directly to the screen.
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+                       check_error();
+               } else {
+                       glFramebufferTexture2D(
+                               GL_FRAMEBUFFER,
+                               GL_COLOR_ATTACHMENT0,
+                               GL_TEXTURE_2D,
+                               temp_textures[phase % 2],
+                               0);
+               }
 
 
-       glTexCoord2f(1.0f, 0.0f);
-       glVertex2f(1.0f, 1.0f);
+               // We have baked an upside-down transform into the quad coordinates,
+               // since the typical graphics program will have the origin at the upper-left,
+               // while OpenGL uses lower-left. In the next ones, however, the origin
+               // is all right, and we need to reverse that.
+               if (phase == 1) {
+                       glTranslatef(0.0f, 1.0f, 0.0f);
+                       glScalef(1.0f, -1.0f, 1.0f);
+               }
 
 
-       glTexCoord2f(0.0f, 0.0f);
-       glVertex2f(0.0f, 1.0f);
+               // Give the required parameters to all the effects.
+               glUseProgram(phases[phase].glsl_program_num);
+               check_error();
 
 
-       glEnd();
-       check_error();
+               glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, "input_tex"), 0);
+               check_error();
+
+               unsigned sampler_num = 1;
+               for (unsigned i = phases[phase].start; i < phases[phase].end; ++i) {
+                       char effect_id[256];
+                       sprintf(effect_id, "eff%d", i);
+                       effects[i]->set_uniforms(phases[phase].glsl_program_num, effect_id, &sampler_num);
+               }
+
+               // Now draw!
+               glBegin(GL_QUADS);
+
+               glTexCoord2f(0.0f, 1.0f);
+               glVertex2f(0.0f, 0.0f);
+
+               glTexCoord2f(1.0f, 1.0f);
+               glVertex2f(1.0f, 0.0f);
+
+               glTexCoord2f(1.0f, 0.0f);
+               glVertex2f(1.0f, 1.0f);
+
+               glTexCoord2f(0.0f, 0.0f);
+               glVertex2f(0.0f, 1.0f);
+
+               glEnd();
+               check_error();
+       }
 }
 }
index 5b8ddbca45295f54a2bc4db4b132b1eeaa1efbb4..4abfb97eba1390f0ca7f16daff094bea14dabd60 100644 (file)
@@ -46,9 +46,18 @@ public:
        void render_to_screen(unsigned char *src);
 
 private:
        void render_to_screen(unsigned char *src);
 
 private:
+       struct Phase {
+               GLint glsl_program_num;
+               bool input_needs_mipmaps;
+               unsigned start, end;
+       };
+
        void normalize_to_linear_gamma();
        void normalize_to_srgb();
 
        void normalize_to_linear_gamma();
        void normalize_to_srgb();
 
+       // Create a GLSL program computing effects [start, end>.
+       Phase compile_glsl_program(unsigned start_index, unsigned end_index);
+
        unsigned width, height;
        ImageFormat input_format, output_format;
        std::vector<Effect *> effects;
        unsigned width, height;
        ImageFormat input_format, output_format;
        std::vector<Effect *> effects;
@@ -56,7 +65,11 @@ private:
        GLuint source_image_num;
        bool use_srgb_texture_format;
 
        GLuint source_image_num;
        bool use_srgb_texture_format;
 
-       GLint glsl_program_num;
+       GLuint fbo;
+       GLuint temp_textures[2];
+
+       std::vector<Phase> phases;
+
        GLenum format, bytes_per_pixel;
        bool finalized;
 
        GLenum format, bytes_per_pixel;
        bool finalized;
 
index 19923ccd6723b8c17690848a41da2b89543c4f03..291002b033e75b1215af6f72b670e7f3061f2358 100644 (file)
@@ -14,6 +14,7 @@ enum EffectId {
        // Spatial.
        EFFECT_MIRROR,
        EFFECT_VIGNETTE,
        // Spatial.
        EFFECT_MIRROR,
        EFFECT_VIGNETTE,
+       EFFECT_BLUR,
 };
 
 #endif // !defined(_EFFECT_ID_H)
 };
 
 #endif // !defined(_EFFECT_ID_H)
index 666a1edc779e1e1faf439afbff1c2f4725bc9c21..17cbeca9f6524aca66f7e2c4eb31575ca074951f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -169,6 +169,7 @@ int main(int argc, char **argv)
        chain.add_input(inout_format);
        Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
        Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
        chain.add_input(inout_format);
        Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
        Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
+       Effect *blur_effect = chain.add_effect(EFFECT_BLUR);
        Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
        //chain.add_effect(EFFECT_MIRROR);
        chain.add_output(inout_format);
        Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
        //chain.add_effect(EFFECT_MIRROR);
        chain.add_output(inout_format);
@@ -225,6 +226,7 @@ int main(int argc, char **argv)
                glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
                check_error();
 
                glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
                check_error();
 
+               glLoadIdentity();
                draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
                draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
                draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
                draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
                draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
                draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);