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