]> git.tdb.fi Git - libs/gl.git/commitdiff
Use samplers in effects and postprocessors
authorMikko Rasa <tdb@tdb.fi>
Thu, 11 Feb 2021 23:03:16 +0000 (01:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 11 Feb 2021 23:03:16 +0000 (01:03 +0200)
14 files changed:
source/ambientocclusion.cpp
source/ambientocclusion.h
source/bloom.cpp
source/bloom.h
source/colorcurve.cpp
source/colorcurve.h
source/effect.cpp
source/effect.h
source/environmentmap.cpp
source/environmentmap.h
source/postprocessor.cpp
source/postprocessor.h
source/shadowmap.cpp
source/shadowmap.h

index 17e4789d0ace5a1ef520d4169a66599aae6a3fa1..8c1dd616c4b53ba40b49aea69bfd237bd89e1b17 100644 (file)
@@ -15,14 +15,14 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
        occlude_target(w, h, (RENDER_COLOR,R8)),
        occlude_shader("ambientocclusion_occlude.glsl"),
        combine_shader("ambientocclusion_combine.glsl"),
-       quad(get_fullscreen_quad())
+       quad(get_fullscreen_quad()),
+       linear_sampler(get_linear_sampler()),
+       nearest_sampler(get_nearest_sampler())
 {
-       occlude_target.set_texture_filter(LINEAR);
-       texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR));
+       texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR), linear_sampler.get());
 
        unsigned seed = 1;
        rotate_lookup.storage(RGBA8, 4, 4, 1);
-       rotate_lookup.get_default_sampler().set_filter(NEAREST);
        unsigned char data[64];
        for(unsigned i=0; i<16; ++i)
        {
@@ -36,7 +36,7 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
        }
        rotate_lookup.image(0, data);
 
-       texturing.attach(3, rotate_lookup);
+       texturing.attach(3, rotate_lookup, nearest_sampler.get());
 
        shdata.uniform("source", 0);
        shdata.uniform("depth", 1);
@@ -91,8 +91,8 @@ void AmbientOcclusion::set_edge_depth_threshold(float edt)
 
 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
 {
-       texturing.attach(0, color);
-       texturing.attach(1, depth);
+       texturing.attach(0, color, nearest_sampler.get());
+       texturing.attach(1, depth, nearest_sampler.get());
 
        if(renderer.get_camera())
                shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
index 5a779979c8829d0c3d724a78200534e1386469c7..6c2c5d7eea938daa6be0e6e5ce2387dd2a361660 100644 (file)
@@ -47,6 +47,8 @@ private:
        Program combine_shader;
        mutable ProgramData shdata;
        RefPtr<Mesh> quad;
+       RefPtr<Sampler> linear_sampler;
+       RefPtr<Sampler> nearest_sampler;
 
 public:
        AmbientOcclusion(unsigned, unsigned, float = 1.0f);
index c7bfd467418b12f58cc3eb9c08afb46878a143c0..ee7c76c3217bef3befc48e40b794d3f485cb4389 100644 (file)
@@ -16,19 +16,20 @@ namespace GL {
 Bloom::Bloom(unsigned w, unsigned h):
        blur_shader("bloom_blur.glsl"),
        combine_shader("bloom_combine.glsl"),
-       quad(get_fullscreen_quad())
+       quad(get_fullscreen_quad()),
+       nearest_sampler(get_nearest_sampler()),
+       linear_sampler(get_linear_sampler())
 {
        blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
        blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
 
        for(unsigned i=0; i<2; ++i)
                target[i] = new RenderTarget(w, h, (RENDER_COLOR,RGB16F));
-       target[1]->set_texture_filter(LINEAR);
 
        common_shdata.uniform("source", 0);
        common_shdata.uniform("blurred", 1);
 
-       combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR));
+       combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR), linear_sampler.get());
 
        set_radius(2.0f);
        set_strength(0.2f);
@@ -74,12 +75,12 @@ void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
        {
                BindRestore bind_fbo(target[i]->get_framebuffer());
                Renderer::Push push2(renderer);
-               renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src);
+               renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src, nearest_sampler.get());
                renderer.add_shader_data(blur_shdata[i]);
                quad->draw(renderer);
        }
 
-       combine_texturing.attach(0, src);
+       combine_texturing.attach(0, src, nearest_sampler.get());
        renderer.set_texturing(&combine_texturing);
        renderer.set_shader_program(&combine_shader);
        quad->draw(renderer);
index ddf46fe96ea1f3b881936ddb7bc80c16c6e0b423..6b10195b873e684d6af7634b85d8dd31a9f05879 100644 (file)
@@ -48,6 +48,8 @@ private:
        Program combine_shader;
        Texturing combine_texturing;
        RefPtr<Mesh> quad;
+       RefPtr<Sampler> nearest_sampler;
+       RefPtr<Sampler> linear_sampler;
 
 public:
        Bloom(unsigned, unsigned);
index 0b62583fc4e810e9ea8a772a99dde7fb74db6e4c..8811b065fd3b6f0dddddd86a8e091e08d97bc224 100644 (file)
@@ -13,16 +13,15 @@ namespace GL {
 
 ColorCurve::ColorCurve():
        shprog("colorcurve.glsl"),
-       quad(get_fullscreen_quad())
+       quad(get_fullscreen_quad()),
+       linear_sampler(get_linear_sampler()),
+       nearest_sampler(get_nearest_sampler())
 {
        shdata.uniform("source", 0);
        shdata.uniform("curve", 1);
 
        curve.storage(LUMINANCE8, 256, 1);
-       Sampler &sampler = curve.get_default_sampler();
-       sampler.set_min_filter(LINEAR);
-       sampler.set_wrap(CLAMP_TO_EDGE);
-       texturing.attach(1, curve);
+       texturing.attach(1, curve, linear_sampler.get());
 
        set_exposure_adjust(0.0f);
        set_brightness_response(0.4f);
@@ -72,7 +71,7 @@ void ColorCurve::set_linear()
 
 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
 {
-       texturing.attach(0, color_buf);
+       texturing.attach(0, color_buf, nearest_sampler.get());
 
        Renderer::Push push(renderer);
        renderer.set_shader_program(&shprog, &shdata);
index 87d43353c80c1dcbadcc329c30992e82c16ce835..3f65b04916897c6d85bde7ec4f33fece160a3cd1 100644 (file)
@@ -50,6 +50,8 @@ private:
        Texture1D curve;
        Texturing texturing;
        RefPtr<Mesh> quad;
+       RefPtr<Sampler> linear_sampler;
+       RefPtr<Sampler> nearest_sampler;
 
 public:
        ColorCurve();
index 36a50bc38aceb7f95c4974e62582d4854e48e9a8..ad789af06019785deb0d1a68f16236b3c37157af 100644 (file)
@@ -1,8 +1,11 @@
 #include "effect.h"
+#include "sampler.h"
 
 namespace Msp {
 namespace GL {
 
+WeakPtr<Sampler> Effect::linear_sampler;
+
 Effect::Effect(Renderable &r):
        renderable(r)
 {
@@ -19,5 +22,18 @@ void Effect::disable_for_pass(const Tag &tag)
        enabled_passes.erase(tag);
 }
 
+RefPtr<Sampler> Effect::get_linear_sampler()
+{
+       RefPtr<Sampler> sampler = linear_sampler;
+       if(!sampler)
+       {
+               sampler = new Sampler;
+               sampler->set_filter(LINEAR);
+               sampler->set_wrap(CLAMP_TO_EDGE);
+               linear_sampler = sampler;
+       }
+       return sampler;
+}
+
 } // namespace GL
 } // namespace Msp
index c55aebf95a5c0363e459a4c010fa79c89ad018e0..bc9e19ec818f2a392889866bdf36428c7c00024b 100644 (file)
@@ -2,11 +2,14 @@
 #define MSP_GL_EFFECT_H_
 
 #include <set>
+#include <msp/core/refptr.h>
 #include "renderable.h"
 
 namespace Msp {
 namespace GL {
 
+class Sampler;
+
 /**
 Effects are used to wrap other renderables and give them additional visual
 properties.  An Effect's render method should set up the necessary state, call
@@ -18,6 +21,10 @@ protected:
        Renderable &renderable;
        std::set<Tag> enabled_passes;
 
+private:
+       static WeakPtr<Sampler> linear_sampler;
+
+protected:
        Effect(Renderable &);
 public:
        virtual ~Effect() { }
@@ -30,6 +37,9 @@ public:
 
        virtual void setup_frame(Renderer &r) { renderable.setup_frame(r); }
        virtual void finish_frame() { renderable.finish_frame(); }
+
+protected:
+       static RefPtr<Sampler> get_linear_sampler();
 };
 
 } // namespace GL
index f4000c768cd2f3c77e8462e37655d4f10b3ed066..3902eb8c95b240434036bdd0915e9433cd373149 100644 (file)
@@ -13,14 +13,12 @@ EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
        Effect(r),
        size(s),
        environment(e),
+       sampler(get_linear_sampler()),
        rendered(false),
        update_interval(1),
        update_delay(0)
 {
        env_tex.storage(RGB8, size, 1);
-       Sampler &env_samp = env_tex.get_default_sampler();
-       env_samp.set_wrap(CLAMP_TO_EDGE);
-       env_samp.set_min_filter(LINEAR);
        depth_buf.storage(DEPTH_COMPONENT32F, size, size);
        for(unsigned i=0; i<6; ++i)
        {
@@ -109,6 +107,7 @@ void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const
 
        unsigned unit = renderer.allocate_effect_texunit();
        shdata.uniform("environment", static_cast<int>(unit));
+       Bind _bind_sampler(*sampler, unit);
        Bind _bind_env(env_tex, unit);
 
        const Matrix &camera_matrix = renderer.get_camera()->get_object_matrix();
index 6fdd3749a9e316e6cad18cf6caeccc37613ad86c..c54968de35d5cfc7bf11be0ee14559c34cf766fc 100644 (file)
@@ -32,6 +32,7 @@ private:
        TextureCube env_tex;
        Renderbuffer depth_buf;
        Framebuffer fbo[6];
+       RefPtr<Sampler> sampler;
        Camera camera;
        mutable ProgramData shdata;
        bool rendered;
index ffc753e4b11ed658280b647e05af688ccec0039b..6af069e97df613c76fda5a4ed8399b88c4e69b87 100644 (file)
@@ -1,12 +1,15 @@
 #include "mesh.h"
 #include "meshbuilder.h"
 #include "postprocessor.h"
+#include "sampler.h"
 #include "shader.h"
 
 namespace Msp {
 namespace GL {
 
 WeakPtr<Mesh> PostProcessor::fullscreen_quad;
+WeakPtr<Sampler> PostProcessor::nearest_sampler;
+WeakPtr<Sampler> PostProcessor::linear_sampler;
 
 void PostProcessor::render(Renderer &, const Texture2D &color, const Texture2D &depth)
 {
@@ -15,8 +18,7 @@ void PostProcessor::render(Renderer &, const Texture2D &color, const Texture2D &
 
 RefPtr<Mesh> PostProcessor::get_fullscreen_quad()
 {
-       RefPtr<Mesh> mesh;
-       mesh = fullscreen_quad;
+       RefPtr<Mesh> mesh = fullscreen_quad;
        if(!mesh)
        {
                mesh = new Mesh(VERTEX2);
@@ -32,6 +34,32 @@ RefPtr<Mesh> PostProcessor::get_fullscreen_quad()
        return mesh;
 }
 
+RefPtr<Sampler> PostProcessor::get_nearest_sampler()
+{
+       RefPtr<Sampler> sampler = nearest_sampler;
+       if(!sampler)
+       {
+               sampler = new Sampler;
+               sampler->set_filter(NEAREST);
+               sampler->set_wrap(CLAMP_TO_EDGE);
+               nearest_sampler = sampler;
+       }
+       return sampler;
+}
+
+RefPtr<Sampler> PostProcessor::get_linear_sampler()
+{
+       RefPtr<Sampler> sampler = linear_sampler;
+       if(!sampler)
+       {
+               sampler = new Sampler;
+               sampler->set_filter(LINEAR);
+               sampler->set_wrap(CLAMP_TO_EDGE);
+               linear_sampler = sampler;
+       }
+       return sampler;
+}
+
 
 PostProcessor::Template::Template():
        size_divisor(1)
index daaf434efe06028c7891cfa3f1685139099a152a..c107d885f658e028522fdb0fe463bbd755d60d1d 100644 (file)
@@ -8,6 +8,7 @@ namespace GL {
 
 class Mesh;
 class Renderer;
+class Sampler;
 class Shader;
 class Texture2D;
 
@@ -37,6 +38,8 @@ public:
 
 private:
        static WeakPtr<Mesh> fullscreen_quad;
+       static WeakPtr<Sampler> nearest_sampler;
+       static WeakPtr<Sampler> linear_sampler;
 
 protected:
        PostProcessor() { }
@@ -52,6 +55,9 @@ protected:
        /** Returns a mesh consisting of a single quad, covering the entire screen.
        The vertices are in normalized device coordinates. */
        static RefPtr<Mesh> get_fullscreen_quad();
+
+       static RefPtr<Sampler> get_nearest_sampler();
+       static RefPtr<Sampler> get_linear_sampler();
 };
 
 } // namespace GL
index 6250f899005b23684f0397b324609a174a858631..eaf6e1373cb126a387d535e9e2b1352199eed2e7 100644 (file)
@@ -12,6 +12,8 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+WeakPtr<Sampler> ShadowMap::shadow_sampler;
+
 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
        Effect(r),
        size(s),
@@ -20,10 +22,15 @@ ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
        depth_bias(4),
        rendered(false)
 {
-       Sampler &depth_samp = depth_buf.get_default_sampler();
-       depth_samp.set_min_filter(LINEAR);
-       depth_samp.set_compare(LEQUAL);
-       depth_samp.set_wrap(CLAMP_TO_EDGE);
+       sampler = shadow_sampler;
+       if(!sampler)
+       {
+               sampler = new Sampler;
+               sampler->set_filter(LINEAR);
+               sampler->set_compare(LEQUAL);
+               sampler->set_wrap(CLAMP_TO_EDGE);
+               shadow_sampler = sampler;
+       }
        depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
        fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
        fbo.require_complete();
@@ -101,6 +108,7 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const
        int iunit = unit;
        shdata.uniform("shadow_map", iunit);
 
+       Bind _bind_sampler(*sampler, unit);
        Bind _bind_depth(depth_buf, unit);
 
        if(const Camera *camera = renderer.get_camera())
index fe36fca0e5bac9e6e7eff35f3a33065d9fb3d85e..e142fc8cafdd56023459035f53d48e73205c997b 100644 (file)
@@ -27,12 +27,15 @@ private:
        Framebuffer fbo;
        Matrix shadow_matrix;
        Texture2D depth_buf;
+       RefPtr<Sampler> sampler;
        Vector3 target;
        float radius;
        float depth_bias;
        mutable ProgramData shdata;
        bool rendered;
 
+       static WeakPtr<Sampler> shadow_sampler;
+
 public:
        ShadowMap(unsigned, Renderable &, const Light &);