]> git.tdb.fi Git - libs/gl.git/blob - source/bloom.cpp
Remove an XXX because I no longer remember how to reproduce the issue
[libs/gl.git] / source / 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 "shader.h"
8 #include "tests.h"
9 #include "texunit.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Bloom::Bloom(unsigned w, unsigned h):
17         blur_shader("bloom_blur.glsl"),
18         combine_shader("bloom_combine.glsl"),
19         quad(get_fullscreen_quad())
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, (RENDER_COLOR,RGB16F));
26
27         common_shdata.uniform("source", 0);
28         common_shdata.uniform("blurred", 1);
29
30         combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR));
31
32         set_radius(2.0f);
33         set_strength(0.2f);
34 }
35
36 Bloom::~Bloom()
37 {
38         for(unsigned i=0; i<2; ++i)
39                 delete target[i];
40 }
41
42 void Bloom::set_radius(float r)
43 {
44         if(r<=0.0f)
45                 throw invalid_argument("Bloom::set_radius");
46
47         int size = min(static_cast<int>(r*3.0f), 9);
48         common_shdata.uniform("size", size);
49
50         vector<float> factors(size*2+1);
51         float sum = 0.0f;
52         r = 2*r*r;
53         for(int i=-size; i<=size; ++i)
54                 sum += (factors[size+i] = exp(-i*i/r));
55         for(int i=0; i<=size*2; ++i)
56                 factors[i] /= sum;
57
58         common_shdata.uniform1_array("factors", size*2+1, &factors.front());
59 }
60
61 void Bloom::set_strength(float s)
62 {
63         if(s<0.0f || s>1.0f)
64                 throw invalid_argument("Bloom::set_strength");
65         common_shdata.uniform("strength", s);
66 }
67
68 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
69 {
70         Renderer::Push push(renderer);
71         renderer.set_shader_program(&blur_shader, &common_shdata);
72         for(unsigned i=0; i<2; ++i)
73         {
74                 BindRestore bind_fbo(target[i]->get_framebuffer());
75                 Renderer::Push push2(renderer);
76                 renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src);
77                 renderer.add_shader_data(blur_shdata[i]);
78                 quad.draw(renderer);
79         }
80
81         combine_texturing.attach(0, src);
82         renderer.set_texturing(&combine_texturing);
83         renderer.set_shader_program(&combine_shader);
84         quad.draw(renderer);
85 }
86
87
88 Bloom::Template::Template():
89         radius(2.0f),
90         strength(0.2f)
91 { }
92
93 Bloom *Bloom::Template::create(unsigned width, unsigned height) const
94 {
95         RefPtr<Bloom> bloom = new Bloom(width/size_divisor, height/size_divisor);
96         bloom->set_radius(radius);
97         bloom->set_strength(strength);
98         return bloom.release();
99 }
100
101
102 Bloom::Template::Loader::Loader(Template &t):
103         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
104 {
105         add("strength", &Template::strength);
106         add("radius", &Template::radius);
107 }
108
109 } // namespace GL
110 } // namespace Msp