2 #include <msp/strings/format.h>
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"))
21 blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
22 blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
24 for(unsigned i=0; i<2; ++i)
25 target[i] = new RenderTarget(w, h, (COLOR_ATTACHMENT,RGBA16F));
33 for(unsigned i=0; i<2; ++i)
37 void Bloom::set_radius(float r)
40 throw invalid_argument("Bloom::set_radius");
42 int size = min(static_cast<int>(r*3.0f), 9);
43 common_shdata.uniform("size", size);
45 vector<float> factors(size*2+1);
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)
53 common_shdata.uniform1_array("factors", size*2+1, &factors.front());
56 void Bloom::set_strength(float s)
59 throw invalid_argument("Bloom::set_strength");
60 common_shdata.uniform("strength", s);
63 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
65 Renderer::Push push(renderer);
66 renderer.set_shader_program(&blur_shader, &common_shdata);
67 for(unsigned i=0; i<2; ++i)
69 Renderer::Push push2(renderer);
70 renderer.set_framebuffer(&target[i]->get_framebuffer());
72 renderer.set_texture("source", (i ? &target[0]->get_target_texture(COLOR_ATTACHMENT) : &src), &nearest_sampler);
73 renderer.add_shader_data(blur_shdata[i]);
78 renderer.set_texture("source", &src, &nearest_sampler);
79 renderer.set_texture("blurred", &target[1]->get_target_texture(COLOR_ATTACHMENT), &linear_sampler);
80 renderer.set_shader_program(&combine_shader);
84 void Bloom::set_debug_name(const string &name)
87 for(unsigned i=0; i<2; ++i)
88 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
89 common_shdata.set_debug_name(name+" [UBO:common]");
90 blur_shdata[0].set_debug_name(name+" [UBO:blur_x]");
91 blur_shdata[1].set_debug_name(name+" [UBO:blur_y]");
98 Bloom *Bloom::Template::create(unsigned width, unsigned height) const
100 RefPtr<Bloom> bloom = new Bloom(width/size_divisor, height/size_divisor);
101 bloom->set_radius(radius);
102 bloom->set_strength(strength);
103 return bloom.release();
107 Bloom::Template::Loader::Loader(Template &t):
108 DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
110 add("strength", &Template::strength);
111 add("radius", &Template::radius);