]> git.tdb.fi Git - libs/gl.git/blob - source/bloom.cpp
Move postprocessor shaders to the builtin shaderlib
[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 "shader.h"
7 #include "tests.h"
8 #include "texunit.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Bloom::Bloom(unsigned w, unsigned h):
16         blur_shader("bloom_blur.glsl"),
17         combine_shader("bloom_combine.glsl"),
18         quad(get_fullscreen_quad())
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         blur_shdata_common.uniform("source", 0);
24         for(unsigned i=0; i<2; ++i)
25         {
26                 tex[i].set_min_filter(NEAREST);
27                 tex[i].set_wrap(CLAMP_TO_EDGE);
28                 tex[i].storage(RGB16F, w, h);
29                 fbo[i].attach(COLOR_ATTACHMENT0, tex[i], 0);
30                 fbo[i].require_complete();
31         }
32
33         combine_shdata.uniform("source", 1);
34         combine_shdata.uniform("blurred", 0);
35
36         combine_texturing.attach(0, tex[1]);
37
38         set_radius(2.0f);
39         set_strength(0.2f);
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         blur_shdata_common.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         blur_shdata_common.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         combine_shdata.uniform("strength", s);
66 }
67
68 void Bloom::render(const Texture2D &src, const Texture2D &)
69 {
70         BindRestore unbind_dtest(static_cast<DepthTest *>(0));
71         BindRestore unbind_blend(static_cast<Blend *>(0));
72         Bind bind_mesh(quad);
73
74         {
75                 Bind bind_shader(blur_shader);
76                 blur_shdata_common.apply();
77                 for(unsigned i=0; i<2; ++i)
78                 {
79                         BindRestore bind_fbo(fbo[i]);
80                         Bind bind_tex(i ? tex[0] : src);
81                         blur_shdata[i].apply();
82                         quad.draw();
83                 }
84         }
85
86         combine_texturing.attach(1, src);
87         Bind bind_texturing(combine_texturing);
88         Bind bind_shader(combine_shader);
89         combine_shdata.apply();
90         quad.draw();
91 }
92
93 } // namespace GL
94 } // namespace Msp