From f4e0bcd917fd4270100e219c5fba7af185060111 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 8 May 2021 18:41:28 +0300 Subject: [PATCH] Store the ambient occlusion rotate lookup texture in resources --- source/effects/ambientocclusion.cpp | 28 ++++++++++++++++++++++------ source/effects/ambientocclusion.h | 3 ++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/source/effects/ambientocclusion.cpp b/source/effects/ambientocclusion.cpp index bbff1f25..b4050aef 100644 --- a/source/effects/ambientocclusion.cpp +++ b/source/effects/ambientocclusion.cpp @@ -13,6 +13,7 @@ namespace Msp { namespace GL { AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float): + rotate_lookup(get_or_create_rotate_lookup()), occlude_target(w, h, (RENDER_COLOR,R8)), occlude_shader(Resources::get_global().get("_ambientocclusion_occlude.glsl.shader")), combine_shader(Resources::get_global().get("_ambientocclusion_combine.glsl.shader")), @@ -21,8 +22,26 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float): nearest_clamp_sampler(Resources::get_global().get("_nearest_clamp.samp")), nearest_sampler(Resources::get_global().get("_nearest.samp")) { + set_n_samples(16); + set_occlusion_radius(0.5f); + set_darkness(1.0f); + set_edge_depth_threshold(0.1f); +} + +const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup() +{ + Resources &resources = Resources::get_global(); + + static const string name = "_ambientocclusion_rotate.tex2d"; + Texture2D *rotate_lookup = resources.find(name); + if(rotate_lookup) + return *rotate_lookup; + + rotate_lookup = new Texture2D; + rotate_lookup->storage(RGBA8, 4, 4, 1); + resources.add(name, rotate_lookup); + unsigned seed = 1; - rotate_lookup.storage(RGBA8, 4, 4, 1); unsigned char data[64]; for(unsigned i=0; i<16; ++i) { @@ -34,12 +53,9 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float): data[i*4+2] = 255-s; data[i*4+3] = ((i+i/4)%2)*255; } - rotate_lookup.image(0, data); + rotate_lookup->image(0, data); - set_n_samples(16); - set_occlusion_radius(0.5f); - set_darkness(1.0f); - set_edge_depth_threshold(0.1f); + return *rotate_lookup; } float AmbientOcclusion::random(unsigned &seed) diff --git a/source/effects/ambientocclusion.h b/source/effects/ambientocclusion.h index 71a92703..61d4d4f3 100644 --- a/source/effects/ambientocclusion.h +++ b/source/effects/ambientocclusion.h @@ -39,7 +39,7 @@ public: }; private: - Texture2D rotate_lookup; + const Texture2D &rotate_lookup; RenderTarget occlude_target; const Program &occlude_shader; const Program &combine_shader; @@ -53,6 +53,7 @@ public: AmbientOcclusion(unsigned, unsigned, float = 1.0f); private: + static const Texture2D &get_or_create_rotate_lookup(); static float random(unsigned &); public: -- 2.43.0