2 #include <msp/strings/format.h>
17 Bloom::Bloom(Resources &resources, unsigned w, unsigned h):
18 blur_shader(resources.get<Program>("_bloom_blur.glsl.shader")),
19 combine_shader(resources.get<Program>("_bloom_combine.glsl.shader")),
20 quad(resources.get<Mesh>("_fullscreen_quad.mesh")),
21 nearest_sampler(resources.get<Sampler>("_nearest_clamp.samp")),
22 linear_sampler(resources.get<Sampler>("_linear_clamp.samp"))
24 blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
25 blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
27 for(unsigned i=0; i<2; ++i)
28 target[i] = new RenderTarget(w, h, (RENDER_COLOR,RGB16F));
36 for(unsigned i=0; i<2; ++i)
40 void Bloom::set_radius(float r)
43 throw invalid_argument("Bloom::set_radius");
45 int size = min(static_cast<int>(r*3.0f), 9);
46 common_shdata.uniform("size", size);
48 vector<float> factors(size*2+1);
51 for(int i=-size; i<=size; ++i)
52 sum += (factors[size+i] = exp(-i*i/r));
53 for(int i=0; i<=size*2; ++i)
56 common_shdata.uniform1_array("factors", size*2+1, &factors.front());
59 void Bloom::set_strength(float s)
62 throw invalid_argument("Bloom::set_strength");
63 common_shdata.uniform("strength", s);
66 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
68 Renderer::Push push(renderer);
69 renderer.set_shader_program(&blur_shader, &common_shdata);
70 for(unsigned i=0; i<2; ++i)
72 BindRestore bind_fbo(target[i]->get_framebuffer());
73 Renderer::Push push2(renderer);
74 renderer.set_texture("source", (i ? &target[0]->get_target_texture(RENDER_COLOR) : &src), &nearest_sampler);
75 renderer.add_shader_data(blur_shdata[i]);
79 renderer.set_texture("source", &src, &nearest_sampler);
80 renderer.set_texture("blurred", &target[1]->get_target_texture(RENDER_COLOR), &linear_sampler);
81 renderer.set_shader_program(&combine_shader);
86 Bloom::Template::Template():
91 Bloom *Bloom::Template::create(Resources &res, unsigned width, unsigned height) const
93 RefPtr<Bloom> bloom = new Bloom(res, width/size_divisor, height/size_divisor);
94 bloom->set_radius(radius);
95 bloom->set_strength(strength);
96 return bloom.release();
100 Bloom::Template::Loader::Loader(Template &t):
101 DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
103 add("strength", &Template::strength);
104 add("radius", &Template::radius);