11#include " BlurTexture.hpp"
22
33#include " PerFrameContext.hpp"
4+ #include " PresetState.hpp"
45
56#include " MilkdropStaticShaders.hpp"
67
8+ #include " Renderer/ShaderCache.hpp"
9+
710#include < array>
811
912namespace libprojectM {
@@ -12,14 +15,6 @@ namespace MilkdropPreset {
1215BlurTexture::BlurTexture ()
1316 : m_blurSampler(std::make_shared<Renderer::Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR))
1417{
15- auto staticShaders = libprojectM::MilkdropPreset::MilkdropStaticShaders::Get ();
16-
17- // Compile shader sources
18- m_blur1Shader.CompileProgram (staticShaders->GetBlurVertexShader (),
19- staticShaders->GetBlur1FragmentShader ());
20- m_blur2Shader.CompileProgram (staticShaders->GetBlurVertexShader (),
21- staticShaders->GetBlur2FragmentShader ());
22-
2318 m_blurFramebuffer.CreateColorAttachment (0 , 0 );
2419
2520 // Initialize Blur VAO/VBO with a single fullscreen quad.
@@ -65,6 +60,33 @@ BlurTexture::~BlurTexture()
6560 glDeleteVertexArrays (1 , &m_vaoBlur);
6661}
6762
63+ void BlurTexture::Initialize (const Renderer::RenderContext& renderContext)
64+ {
65+ auto staticShaders = libprojectM::MilkdropPreset::MilkdropStaticShaders::Get ();
66+
67+ // Load/compile shader sources
68+ auto blur1Shader = renderContext.shaderCache ->Get (" milkdrop_blur1" );
69+ if (!blur1Shader)
70+ {
71+ blur1Shader = std::make_shared<Renderer::Shader>();
72+ blur1Shader->CompileProgram (staticShaders->GetBlurVertexShader (),
73+ staticShaders->GetBlur1FragmentShader ());
74+ renderContext.shaderCache ->Insert (" milkdrop_blur1" , blur1Shader);
75+ }
76+
77+ auto blur2Shader = renderContext.shaderCache ->Get (" milkdrop_blur2" );
78+ if (!blur2Shader)
79+ {
80+ blur2Shader = std::make_shared<Renderer::Shader>();
81+ blur2Shader->CompileProgram (staticShaders->GetBlurVertexShader (),
82+ staticShaders->GetBlur2FragmentShader ());
83+ renderContext.shaderCache ->Insert (" milkdrop_blur2" , blur2Shader);
84+ }
85+
86+ m_blur1Shader = blur1Shader;
87+ m_blur2Shader = blur2Shader;
88+ }
89+
6890void BlurTexture::SetRequiredBlurLevel (BlurTexture::BlurLevel level)
6991{
7092 m_blurLevel = std::max (level, m_blurLevel);
@@ -152,15 +174,20 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
152174 }
153175
154176 // set pixel shader
155- Renderer::Shader* blurShader;
177+ std::shared_ptr< Renderer::Shader> blurShader;
156178 if ((pass % 2 ) == 0 )
157179 {
158- blurShader = & m_blur1Shader;
180+ blurShader = m_blur1Shader. lock () ;
159181 }
160182 else
161183 {
162- blurShader = &m_blur2Shader;
184+ blurShader = m_blur2Shader.lock ();
185+ }
186+ if (!blurShader)
187+ {
188+ return ;
163189 }
190+
164191 blurShader->Bind ();
165192 blurShader->SetUniformInt (" texture_sampler" , 0 );
166193
@@ -205,10 +232,10 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
205232 // float4 _c2; // d1..d4
206233 // float4 _c3; // scale, bias, w_div, 0
207234 // -------------------------------------
208- m_blur1Shader. SetUniformFloat4 (" _c0" , {srcWidth, srcHeight, 1 .0f / srcWidth, 1 .0f / srcHeight});
209- m_blur1Shader. SetUniformFloat4 (" _c1" , {w1, w2, w3, w4});
210- m_blur1Shader. SetUniformFloat4 (" _c2" , {d1, d2, d3, d4});
211- m_blur1Shader. SetUniformFloat4 (" _c3" , {scaleNow, biasNow, w_div, 0.0 });
235+ blurShader-> SetUniformFloat4 (" _c0" , {srcWidth, srcHeight, 1 .0f / srcWidth, 1 .0f / srcHeight});
236+ blurShader-> SetUniformFloat4 (" _c1" , {w1, w2, w3, w4});
237+ blurShader-> SetUniformFloat4 (" _c2" , {d1, d2, d3, d4});
238+ blurShader-> SetUniformFloat4 (" _c3" , {scaleNow, biasNow, w_div, 0.0 });
212239 }
213240 else
214241 {
@@ -224,19 +251,19 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
224251 // float4 _c5; // w1,w2,d1,d2
225252 // float4 _c6; // w_div, edge_darken_c1, edge_darken_c2, edge_darken_c3
226253 // -------------------------------------
227- m_blur2Shader. SetUniformFloat4 (" _c0" , {srcWidth, srcHeight, 1 .0f / srcWidth, 1 .0f / srcHeight});
228- m_blur2Shader. SetUniformFloat4 (" _c5" , {w1, w2, d1, d2});
254+ blurShader-> SetUniformFloat4 (" _c0" , {srcWidth, srcHeight, 1 .0f / srcWidth, 1 .0f / srcHeight});
255+ blurShader-> SetUniformFloat4 (" _c5" , {w1, w2, d1, d2});
229256 // note: only do this first time; if you do it many times,
230257 // then the super-blurred levels will have big black lines along the top & left sides.
231258 if (pass == 1 )
232259 {
233260 // Darken edges
234- m_blur2Shader. SetUniformFloat4 (" _c6" , {w_div, (1 - blur1EdgeDarken), blur1EdgeDarken, 5 .0f });
261+ blurShader-> SetUniformFloat4 (" _c6" , {w_div, (1 - blur1EdgeDarken), blur1EdgeDarken, 5 .0f });
235262 }
236263 else
237264 {
238265 // Don't darken
239- m_blur2Shader. SetUniformFloat4 (" _c6" , {w_div, 1 .0f , 0 .0f , 5 .0f });
266+ blurShader-> SetUniformFloat4 (" _c6" , {w_div, 1 .0f , 0 .0f , 5 .0f });
240267 }
241268 }
242269
0 commit comments