2 #include <msp/strings/format.h>
16 Bloom::Bloom(unsigned w, unsigned h):
17 blur_shader("bloom_blur.glsl"),
18 combine_shader("bloom_combine.glsl"),
19 quad(get_fullscreen_quad())
21 blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
22 blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
24 blur_shdata_common.uniform("source", 0);
25 for(unsigned i=0; i<2; ++i)
27 tex[i].set_min_filter(NEAREST);
28 tex[i].set_wrap(CLAMP_TO_EDGE);
29 tex[i].storage(RGB16F, w, h);
30 fbo[i].attach(COLOR_ATTACHMENT0, tex[i], 0);
31 fbo[i].require_complete();
34 combine_shdata.uniform("source", 1);
35 combine_shdata.uniform("blurred", 0);
37 combine_texturing.attach(0, tex[1]);
43 void Bloom::set_radius(float r)
46 throw invalid_argument("Bloom::set_radius");
48 int size = min(static_cast<int>(r*3.0f), 9);
49 blur_shdata_common.uniform("size", size);
51 vector<float> factors(size*2+1);
54 for(int i=-size; i<=size; ++i)
55 sum += (factors[size+i] = exp(-i*i/r));
56 for(int i=0; i<=size*2; ++i)
59 blur_shdata_common.uniform1_array("factors", size*2+1, &factors.front());
62 void Bloom::set_strength(float s)
65 throw invalid_argument("Bloom::set_strength");
66 combine_shdata.uniform("strength", s);
69 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
72 Renderer::Push push(renderer);
73 renderer.set_shader_program(&blur_shader, &blur_shdata_common);
74 for(unsigned i=0; i<2; ++i)
76 BindRestore bind_fbo(fbo[i]);
77 Renderer::Push push2(renderer);
78 renderer.set_texture(i ? &tex[0] : &src);
79 renderer.add_shader_data(blur_shdata[i]);
84 Renderer::Push push(renderer);
85 combine_texturing.attach(1, src);
86 renderer.set_texturing(&combine_texturing);
87 renderer.set_shader_program(&combine_shader, &combine_shdata);