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