Skip to content

Commit 458a63d

Browse files
committed
Cache compiled static preset shaders
1 parent a2e9a91 commit 458a63d

16 files changed

+209
-81
lines changed

src/libprojectM/MilkdropPreset/BlurTexture.cpp

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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

912
namespace libprojectM {
@@ -12,14 +15,6 @@ namespace MilkdropPreset {
1215
BlurTexture::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+
6890
void 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

src/libprojectM/MilkdropPreset/BlurTexture.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <Renderer/Framebuffer.hpp>
8+
#include <Renderer/RenderContext.hpp>
89
#include <Renderer/Shader.hpp>
910
#include <Renderer/TextureSamplerDescriptor.hpp>
1011

@@ -49,6 +50,12 @@ class BlurTexture
4950
*/
5051
~BlurTexture();
5152

53+
/**
54+
* @brief Initializes the blur texture.
55+
* @param renderContext
56+
*/
57+
void Initialize(const Renderer::RenderContext& renderContext);
58+
5259
/**
5360
* @brief Sets the minimum required blur level.
5461
* If the current level isn't high enough, it'll be increased.
@@ -101,8 +108,8 @@ class BlurTexture
101108
GLuint m_vboBlur; //!< Vertex buffer object for the fullscreen blur quad.
102109
GLuint m_vaoBlur; //!< Vertex array object for the fullscreen blur quad.
103110

104-
Renderer::Shader m_blur1Shader; //!< The shader used on the first blur pass.
105-
Renderer::Shader m_blur2Shader; //!< The shader used for subsequent blur passes after the initial pass.
111+
std::weak_ptr<Renderer::Shader> m_blur1Shader; //!< The shader used on the first blur pass.
112+
std::weak_ptr<Renderer::Shader> m_blur2Shader; //!< The shader used for subsequent blur passes after the initial pass.
106113

107114
int m_sourceTextureWidth{}; //!< Width of the source texture used to create the blur textures.
108115
int m_sourceTextureHeight{}; //!< Height of the source texture used to create the blur textures.

src/libprojectM/MilkdropPreset/Border.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
3333
glEnable(GL_BLEND);
3434
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3535

36-
m_presetState.untexturedShader.Bind();
37-
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
36+
auto shader = m_presetState.untexturedShader.lock();
37+
shader->Bind();
38+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
3839

3940
std::array<Point, 4> vertices{};
4041
for (int border = 0; border < 2; border++)

src/libprojectM/MilkdropPreset/CustomShape.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ void CustomShape::Draw()
192192

193193
if (static_cast<int>(*m_perFrameContext.textured) != 0)
194194
{
195-
m_presetState.texturedShader.Bind();
196-
m_presetState.texturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
197-
m_presetState.texturedShader.SetUniformInt("texture_sampler", 0);
195+
auto shader = m_presetState.texturedShader.lock();
196+
shader->Bind();
197+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
198+
shader->SetUniformInt("texture_sampler", 0);
198199

199200
// Textured shape, either main texture or texture from "image" key
200201
auto textureAspectY = m_presetState.renderContext.aspectY;
@@ -208,7 +209,7 @@ void CustomShape::Draw()
208209
auto desc = m_presetState.renderContext.textureManager->GetTexture(m_image);
209210
if (!desc.Empty())
210211
{
211-
desc.Bind(0, m_presetState.texturedShader);
212+
desc.Bind(0, *shader);
212213
textureAspectY = 1.0f;
213214
}
214215
else
@@ -251,8 +252,9 @@ void CustomShape::Draw()
251252

252253
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(TexturedPoint) * (sides + 2), vertexData.data());
253254

254-
m_presetState.untexturedShader.Bind();
255-
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
255+
auto shader = m_presetState.untexturedShader.lock();
256+
shader->Bind();
257+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
256258

257259
glBindVertexArray(m_vaoIdUntextured);
258260
glDrawArrays(GL_TRIANGLE_FAN, 0, sides + 2);
@@ -269,8 +271,9 @@ void CustomShape::Draw()
269271
points[i].y = vertexData[i + 1].y;
270272
}
271273

272-
m_presetState.untexturedShader.Bind();
273-
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
274+
auto shader = m_presetState.untexturedShader.lock();
275+
shader->Bind();
276+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
274277

275278
glVertexAttrib4f(1,
276279
static_cast<float>(*m_perFrameContext.border_r),

src/libprojectM/MilkdropPreset/CustomWaveform.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ void CustomWaveform::Draw(const PerFrameContext& presetPerFrameContext)
182182
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
183183
}
184184

185-
m_presetState.untexturedShader.Bind();
186-
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
185+
auto shader = m_presetState.untexturedShader.lock();
186+
shader->Bind();
187+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
187188

188189
auto iterations = (m_drawThick && !m_useDots) ? 4 : 1;
189190

src/libprojectM/MilkdropPreset/DarkenCenter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ void DarkenCenter::Draw()
4444
glEnable(GL_BLEND);
4545
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4646

47-
m_presetState.untexturedShader.Bind();
48-
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
47+
auto shader = m_presetState.untexturedShader.lock();
48+
shader->Bind();
49+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
4950

5051
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
5152

src/libprojectM/MilkdropPreset/Filters.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ void Filters::Draw()
2929

3030
glEnable(GL_BLEND);
3131

32-
m_presetState.untexturedShader.Bind();
33-
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
32+
auto shader = m_presetState.untexturedShader.lock();
33+
shader->Bind();
34+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
3435

3536
glBindVertexArray(m_vaoID);
3637
glVertexAttrib4f(1, 1.0, 1.0, 1.0, 1.0);

src/libprojectM/MilkdropPreset/MilkdropPreset.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void MilkdropPreset::Initialize(const Renderer::RenderContext& renderContext)
5959
{
6060
assert(renderContext.textureManager);
6161
m_state.renderContext = renderContext;
62+
m_state.blurTexture.Initialize(renderContext);
63+
m_state.LoadShaders();
6264

6365
// Initialize variables and code now we have a proper render state.
6466
CompileCodeAndRunInitExpressions();

src/libprojectM/MilkdropPreset/MotionVectors.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "MilkdropStaticShaders.hpp"
44

5+
#include <Renderer/ShaderCache.hpp>
56
#include <Renderer/TextureManager.hpp>
67

78
namespace libprojectM {
@@ -11,9 +12,6 @@ MotionVectors::MotionVectors(PresetState& presetState)
1112
: RenderItem()
1213
, m_presetState(presetState)
1314
{
14-
auto staticShaders = libprojectM::MilkdropPreset::MilkdropStaticShaders::Get();
15-
m_motionVectorShader.CompileProgram(staticShaders->GetPresetMotionVectorsVertexShader(),
16-
staticShaders->GetUntexturedDrawFragmentShader());
1715
RenderItem::Init();
1816
}
1917

@@ -89,12 +87,13 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
8987
glEnable(GL_BLEND);
9088
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9189

92-
m_motionVectorShader.Bind();
93-
m_motionVectorShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
94-
m_motionVectorShader.SetUniformFloat("length_multiplier", static_cast<float>(*presetPerFrameContext.mv_l));
95-
m_motionVectorShader.SetUniformFloat("minimum_length", minimumLength);
90+
auto shader = GetShader();
91+
shader->Bind();
92+
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
93+
shader->SetUniformFloat("length_multiplier", static_cast<float>(*presetPerFrameContext.mv_l));
94+
shader->SetUniformFloat("minimum_length", minimumLength);
9695

97-
m_motionVectorShader.SetUniformInt("warp_coordinates", 0);
96+
shader->SetUniformInt("warp_coordinates", 0);
9897

9998
motionTexture->Bind(0, m_sampler);
10099

@@ -162,5 +161,29 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
162161
glDisable(GL_BLEND);
163162
}
164163

164+
std::shared_ptr<Renderer::Shader> MotionVectors::GetShader()
165+
{
166+
auto shader = m_motionVectorShader.lock();
167+
if (!shader)
168+
{
169+
shader = m_presetState.renderContext.shaderCache->Get("milkdrop_motion_vectors");
170+
}
171+
if (!shader)
172+
{
173+
// First use, compile and cache.
174+
auto staticShaders = libprojectM::MilkdropPreset::MilkdropStaticShaders::Get();
175+
176+
shader = std::make_shared<Renderer::Shader>();
177+
shader->CompileProgram(staticShaders->GetPresetMotionVectorsVertexShader(),
178+
staticShaders->GetUntexturedDrawFragmentShader());
179+
180+
m_presetState.renderContext.shaderCache->Insert("milkdrop_motion_vectors", shader);
181+
}
182+
183+
m_motionVectorShader = shader;
184+
185+
return shader;
186+
}
187+
165188
} // namespace MilkdropPreset
166189
} // namespace libprojectM

src/libprojectM/MilkdropPreset/MotionVectors.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ class MotionVectors : public Renderer::RenderItem
3636
void Draw(const PerFrameContext& presetPerFrameContext, std::shared_ptr<Renderer::Texture> motionTexture);
3737

3838
private:
39-
struct MotionVectorVertex
40-
{
39+
struct MotionVectorVertex {
4140
float x{};
4241
float y{};
4342
int32_t index{};
4443
};
4544

45+
std::shared_ptr<Renderer::Shader> GetShader();
46+
4647
PresetState& m_presetState; //!< The global preset state.
4748

48-
Renderer::Shader m_motionVectorShader; //!< The motion vector shader, calculates the trace positions in the GPU.
49+
std::weak_ptr<Renderer::Shader> m_motionVectorShader; //!< The motion vector shader, calculates the trace positions in the GPU.
4950
std::shared_ptr<Renderer::Sampler> m_sampler{std::make_shared<Renderer::Sampler>(GL_CLAMP_TO_EDGE, GL_LINEAR)}; //!< The texture sampler.
5051

5152
int m_lastVertexCount{}; //!< Number of vertices drawn in the previous draw call.

0 commit comments

Comments
 (0)