From: Mikko Rasa Date: Mon, 8 Feb 2021 11:51:44 +0000 (+0200) Subject: Better lifecycle management of the fullscreen quad in PostProcessor X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=47bfbdc8cf844aa079995fca34a3b906b49a4f66;p=libs%2Fgl.git Better lifecycle management of the fullscreen quad in PostProcessor It's now stored in a WeakPtr and the postprocessor instances hold a RefPtr to it, so it will be deleted together with the last postprocessor. --- diff --git a/source/ambientocclusion.cpp b/source/ambientocclusion.cpp index 1cfdff09..17e4789d 100644 --- a/source/ambientocclusion.cpp +++ b/source/ambientocclusion.cpp @@ -103,11 +103,11 @@ void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const { BindRestore bind_fbo(occlude_target.get_framebuffer()); - quad.draw(renderer); + quad->draw(renderer); } renderer.set_shader_program(&combine_shader); - quad.draw(renderer); + quad->draw(renderer); } diff --git a/source/ambientocclusion.h b/source/ambientocclusion.h index ff1e8244..5a779979 100644 --- a/source/ambientocclusion.h +++ b/source/ambientocclusion.h @@ -46,7 +46,7 @@ private: Program occlude_shader; Program combine_shader; mutable ProgramData shdata; - const Mesh &quad; + RefPtr quad; public: AmbientOcclusion(unsigned, unsigned, float = 1.0f); diff --git a/source/bloom.cpp b/source/bloom.cpp index 6484d052..c7bfd467 100644 --- a/source/bloom.cpp +++ b/source/bloom.cpp @@ -76,13 +76,13 @@ void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &) Renderer::Push push2(renderer); renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src); renderer.add_shader_data(blur_shdata[i]); - quad.draw(renderer); + quad->draw(renderer); } combine_texturing.attach(0, src); renderer.set_texturing(&combine_texturing); renderer.set_shader_program(&combine_shader); - quad.draw(renderer); + quad->draw(renderer); } diff --git a/source/bloom.h b/source/bloom.h index d8bb61ac..ddf46fe9 100644 --- a/source/bloom.h +++ b/source/bloom.h @@ -47,7 +47,7 @@ private: ProgramData blur_shdata[2]; Program combine_shader; Texturing combine_texturing; - const Mesh &quad; + RefPtr quad; public: Bloom(unsigned, unsigned); diff --git a/source/colorcurve.cpp b/source/colorcurve.cpp index 78a7fc16..0b62583f 100644 --- a/source/colorcurve.cpp +++ b/source/colorcurve.cpp @@ -77,7 +77,7 @@ void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Te Renderer::Push push(renderer); renderer.set_shader_program(&shprog, &shdata); renderer.set_texturing(&texturing); - quad.draw(renderer); + quad->draw(renderer); } diff --git a/source/colorcurve.h b/source/colorcurve.h index d8697d83..87d43353 100644 --- a/source/colorcurve.h +++ b/source/colorcurve.h @@ -49,7 +49,7 @@ private: ProgramData shdata; Texture1D curve; Texturing texturing; - const Mesh &quad; + RefPtr quad; public: ColorCurve(); diff --git a/source/postprocessor.cpp b/source/postprocessor.cpp index f67bf4cf..ffc753e4 100644 --- a/source/postprocessor.cpp +++ b/source/postprocessor.cpp @@ -6,27 +6,29 @@ namespace Msp { namespace GL { +WeakPtr PostProcessor::fullscreen_quad; + void PostProcessor::render(Renderer &, const Texture2D &color, const Texture2D &depth) { render(color, depth); } -const Mesh &PostProcessor::get_fullscreen_quad() -{ - static const Mesh &mesh = create_fullscreen_quad(); - return mesh; -} - -const Mesh &PostProcessor::create_fullscreen_quad() +RefPtr PostProcessor::get_fullscreen_quad() { - static Mesh mesh(VERTEX2); - MeshBuilder builder(mesh); - builder.begin(TRIANGLE_STRIP); - builder.vertex(-1, 1); - builder.vertex(-1, -1); - builder.vertex(1, 1); - builder.vertex(1, -1); - builder.end(); + RefPtr mesh; + mesh = fullscreen_quad; + if(!mesh) + { + mesh = new Mesh(VERTEX2); + MeshBuilder builder(*mesh); + builder.begin(TRIANGLE_STRIP); + builder.vertex(-1, 1); + builder.vertex(-1, -1); + builder.vertex(1, 1); + builder.vertex(1, -1); + builder.end(); + fullscreen_quad = mesh; + } return mesh; } diff --git a/source/postprocessor.h b/source/postprocessor.h index ae864c07..daaf434e 100644 --- a/source/postprocessor.h +++ b/source/postprocessor.h @@ -35,6 +35,9 @@ public: virtual PostProcessor *create(unsigned, unsigned) const = 0; }; +private: + static WeakPtr fullscreen_quad; + protected: PostProcessor() { } public: @@ -48,10 +51,7 @@ public: protected: /** Returns a mesh consisting of a single quad, covering the entire screen. The vertices are in normalized device coordinates. */ - static const Mesh &get_fullscreen_quad(); - -private: - static const Mesh &create_fullscreen_quad(); + static RefPtr get_fullscreen_quad(); }; } // namespace GL