]> git.tdb.fi Git - libs/gl.git/blob - source/effects/bloom.cpp
Rewrite state management
[libs/gl.git] / source / effects / bloom.cpp
1 #include <cmath>
2 #include <msp/strings/format.h>
3 #include "blend.h"
4 #include "bloom.h"
5 #include "misc.h"
6 #include "renderer.h"
7 #include "resources.h"
8 #include "shader.h"
9 #include "tests.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Bloom::Bloom(unsigned w, unsigned h):
17         blur_shader(Resources::get_global().get<Program>("_bloom_blur.glsl.shader")),
18         combine_shader(Resources::get_global().get<Program>("_bloom_combine.glsl.shader")),
19         quad(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
20         nearest_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp")),
21         linear_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp"))
22 {
23         blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
24         blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
25
26         for(unsigned i=0; i<2; ++i)
27                 target[i] = new RenderTarget(w, h, (RENDER_COLOR,RGB16F));
28
29         set_radius(2.0f);
30         set_strength(0.2f);
31 }
32
33 Bloom::~Bloom()
34 {
35         for(unsigned i=0; i<2; ++i)
36                 delete target[i];
37 }
38
39 void Bloom::set_radius(float r)
40 {
41         if(r<=0.0f)
42                 throw invalid_argument("Bloom::set_radius");
43
44         int size = min(static_cast<int>(r*3.0f), 9);
45         common_shdata.uniform("size", size);
46
47         vector<float> factors(size*2+1);
48         float sum = 0.0f;
49         r = 2*r*r;
50         for(int i=-size; i<=size; ++i)
51                 sum += (factors[size+i] = exp(-i*i/r));
52         for(int i=0; i<=size*2; ++i)
53                 factors[i] /= sum;
54
55         common_shdata.uniform1_array("factors", size*2+1, &factors.front());
56 }
57
58 void Bloom::set_strength(float s)
59 {
60         if(s<0.0f || s>1.0f)
61                 throw invalid_argument("Bloom::set_strength");
62         common_shdata.uniform("strength", s);
63 }
64
65 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
66 {
67         Renderer::Push push(renderer);
68         renderer.set_shader_program(&blur_shader, &common_shdata);
69         for(unsigned i=0; i<2; ++i)
70         {
71                 BindRestore bind_fbo(target[i]->get_framebuffer());
72                 Renderer::Push push2(renderer);
73                 renderer.set_texture("source", (i ? &target[0]->get_target_texture(RENDER_COLOR) : &src), &nearest_sampler);
74                 renderer.add_shader_data(blur_shdata[i]);
75                 quad.draw(renderer);
76         }
77
78         renderer.set_texture("source", &src, &nearest_sampler);
79         renderer.set_texture("blurred", &target[1]->get_target_texture(RENDER_COLOR), &linear_sampler);
80         renderer.set_shader_program(&combine_shader);
81         quad.draw(renderer);
82 }
83
84 void Bloom::set_debug_name(const string &name)
85 {
86 #ifdef DEBUG
87         for(unsigned i=0; i<2; ++i)
88                 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
89         common_shdata.set_debug_name(name+" [UBO:common]");
90         blur_shdata[0].set_debug_name(name+" [UBO:blur_x]");
91         blur_shdata[1].set_debug_name(name+" [UBO:blur_y]");
92 #else
93         (void)name;
94 #endif
95 }
96
97
98 Bloom::Template::Template():
99         radius(2.0f),
100         strength(0.2f)
101 { }
102
103 Bloom *Bloom::Template::create(unsigned width, unsigned height) const
104 {
105         RefPtr<Bloom> bloom = new Bloom(width/size_divisor, height/size_divisor);
106         bloom->set_radius(radius);
107         bloom->set_strength(strength);
108         return bloom.release();
109 }
110
111
112 Bloom::Template::Loader::Loader(Template &t):
113         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
114 {
115         add("strength", &Template::strength);
116         add("radius", &Template::radius);
117 }
118
119 } // namespace GL
120 } // namespace Msp