Rename set_uniforms() to set_gl_state(), and make a corresponding clear_gl_state().
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 5 Oct 2012 19:29:34 +0000 (21:29 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 5 Oct 2012 19:29:34 +0000 (21:29 +0200)
blur_effect.cpp
blur_effect.h
effect.cpp
effect.h
effect_chain.cpp
lift_gamma_gain_effect.cpp
lift_gamma_gain_effect.h
sandbox_effect.cpp
sandbox_effect.h
vignette_effect.cpp
vignette_effect.h

index 2851698..e6a9a31 100644 (file)
@@ -47,9 +47,9 @@ std::string SingleBlurPassEffect::output_fragment_shader()
        return read_file("blur_effect.frag");
 }
 
-void SingleBlurPassEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
 {
-       Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
+       Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
        int base_texture_size, texture_size;
        if (direction == HORIZONTAL) {
@@ -202,3 +202,13 @@ void SingleBlurPassEffect::set_uniforms(GLuint glsl_program_num, const std::stri
        set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS + 1);
 #endif
 }
+
+void SingleBlurPassEffect::clear_gl_state()
+{
+       glActiveTexture(GL_TEXTURE0);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
+       check_error();
+}
index 3e516b7..a440949 100644 (file)
@@ -22,7 +22,7 @@ public:
        virtual std::string output_fragment_shader() {
                assert(false);
        }
-       virtual void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
+       virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
                assert(false);
        }
 
@@ -44,7 +44,8 @@ public:
        virtual bool needs_texture_bounce() const { return true; }
        virtual bool needs_mipmaps() const { return true; }
 
-       void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+       void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+       void clear_gl_state();
        
        enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
 
index 1aa3778..8cdf236 100644 (file)
@@ -211,7 +211,7 @@ std::string Effect::output_convenience_uniforms() const
        return output;
 }
 
-void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
+void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
 {
        for (std::map<std::string, float*>::const_iterator it = params_float.begin();
             it != params_float.end();
@@ -246,3 +246,5 @@ void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix, un
                ++*sampler_num;
        }
 }
+
+void Effect::clear_gl_state() {}
index 082c234..d14176b 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -115,17 +115,19 @@ public:
        // Returns the GLSL fragment shader string for this effect.
        virtual std::string output_fragment_shader() = 0;
 
-       // Set all uniforms the shader needs in the current GL context.
-       // The default implementation sets one uniform per registered parameter.
+       // Set all OpenGL state that this effect needs before rendering.
+       // The default implementation sets one uniform per registered parameter,
+       // but no other state.
        //
        // <sampler_num> is the first free texture sampler. If you want to use
        // textures, you can bind a texture to GL_TEXTURE0 + <sampler_num>,
        // and then increment the number (so that the next effect in the chain
        // will use a different sampler).
-       //
-       // NOTE: Currently this is also abused a bit to set other GL state
-       // the effect might need.
-       virtual void set_uniforms(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
+       virtual void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
+
+       // If you set any special OpenGL state in set_gl_state(), you can clear it
+       // after rendering here. The default implementation does nothing.
+       virtual void clear_gl_state();
 
        // Set a parameter; intended to be called from user code.
        // Neither of these take ownership of the pointer.
index bfb30bf..2144bfd 100644 (file)
@@ -573,7 +573,7 @@ void EffectChain::render_to_screen(unsigned char *src)
                unsigned sampler_num = phases[phase].inputs.size();
                for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
                        Effect *effect = phases[phase].effects[i];
-                       effect->set_uniforms(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
+                       effect->set_gl_state(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
                }
 
                // Now draw!
@@ -594,11 +594,9 @@ void EffectChain::render_to_screen(unsigned char *src)
                glEnd();
                check_error();
 
-               // HACK
-               glActiveTexture(GL_TEXTURE0);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
-               check_error();
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
-               check_error();
+               for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
+                       Effect *effect = phases[phase].effects[i];
+                       effect->clear_gl_state();
+               }
        }
 }
index 19cef18..2dc8278 100644 (file)
@@ -22,9 +22,9 @@ std::string LiftGammaGainEffect::output_fragment_shader()
        return read_file("lift_gamma_gain_effect.frag");
 }
 
-void LiftGammaGainEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+void LiftGammaGainEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
 {
-       Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
+       Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
        RGBTriplet gain_pow_inv_gamma(
                pow(gain.r, 1.0f / gamma.r),
index b57a899..533a7b2 100644 (file)
@@ -24,7 +24,7 @@ public:
        LiftGammaGainEffect();
        std::string output_fragment_shader();
 
-       void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+       void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
 
 private:
        RGBTriplet lift, gamma, gain;
index 85fa900..6fdfa51 100644 (file)
@@ -19,9 +19,9 @@ std::string SandboxEffect::output_fragment_shader()
        return read_file("sandbox_effect.frag");
 }
 
-void SandboxEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+void SandboxEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
 {
-       Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
+       Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
        // Any OpenGL state you might want to set, goes here.
 }
index 391be6d..44e8d3f 100644 (file)
@@ -15,7 +15,7 @@ public:
        SandboxEffect();
        std::string output_fragment_shader();
 
-       void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+       void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
 
 private:
        float parm;
index e6e7e7b..b3a6c04 100644 (file)
@@ -22,9 +22,9 @@ std::string VignetteEffect::output_fragment_shader()
        return read_file("vignette_effect.frag");
 }
 
-void VignetteEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+void VignetteEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
 {
-       Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
+       Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
        set_uniform_float(glsl_program_num, prefix, "inv_radius", 1.0f / radius);
 
index 2567e83..d7c66f6 100644 (file)
@@ -13,7 +13,7 @@ public:
 
        virtual bool needs_srgb_primaries() const { return false; }
 
-       void set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+       void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
 
 private:
        Point2D center;