Skip to content

Commit 24982a6

Browse files
committed
Wrap setting of blend mode/functions in a class.
1 parent 9c8b4b3 commit 24982a6

File tree

14 files changed

+175
-61
lines changed

14 files changed

+175
-61
lines changed

src/libprojectM/MilkdropPreset/BlurTexture.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "MilkdropStaticShaders.hpp"
77

8+
#include <Renderer/BlendMode.hpp>
89
#include <Renderer/Point.hpp>
910
#include <Renderer/ShaderCache.hpp>
1011

@@ -150,7 +151,7 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
150151

151152
m_blurFramebuffer.Bind(0);
152153

153-
glBlendFunc(GL_ONE, GL_ZERO);
154+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::One, Renderer::BlendMode::Function::Zero);
154155

155156
for (unsigned int pass = 0; pass < passes; pass++)
156157
{
@@ -263,7 +264,7 @@ void BlurTexture::Update(const Renderer::Texture& sourceTexture, const PerFrameC
263264
}
264265

265266
Renderer::Mesh::Unbind();
266-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
267+
Renderer::BlendMode::Set(false, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);
267268

268269
// Bind previous framebuffer and reset viewport size
269270
glBindFramebuffer(GL_READ_FRAMEBUFFER, origReadFramebuffer);

src/libprojectM/MilkdropPreset/Border.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "Border.hpp"
22

3+
#include <Renderer/BlendMode.hpp>
4+
35
namespace libprojectM {
46
namespace MilkdropPreset {
57

@@ -30,8 +32,7 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
3032
float const innerBorderSize = static_cast<float>(*presetPerFrameContext.ib_size);
3133

3234
// No additive drawing for borders
33-
glEnable(GL_BLEND);
34-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
35+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);
3536

3637
auto shader = m_presetState.untexturedShader.lock();
3738
shader->Bind();
@@ -69,8 +70,7 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
6970

7071
Renderer::Mesh::Unbind();
7172
Renderer::Shader::Unbind();
72-
73-
glDisable(GL_BLEND);
73+
Renderer::BlendMode::SetBlendActive(false);
7474
}
7575

7676
} // namespace MilkdropPreset

src/libprojectM/MilkdropPreset/CustomShape.cpp

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

33
#include "PresetFileParser.hpp"
44

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

78
#include <vector>
@@ -80,7 +81,7 @@ void CustomShape::Draw()
8081
return;
8182
}
8283

83-
glEnable(GL_BLEND);
84+
Renderer::BlendMode::SetBlendActive(true);
8485

8586
for (int instance = 0; instance < m_instances; instance++)
8687
{
@@ -98,7 +99,10 @@ void CustomShape::Draw()
9899
}
99100

100101
// Additive Drawing or Overwrite
101-
glBlendFunc(GL_SRC_ALPHA, static_cast<int>(*m_perFrameContext.additive) != 0 ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA);
102+
Renderer::BlendMode::SetBlendFunction(Renderer::BlendMode::Function::SourceAlpha,
103+
static_cast<int>(*m_perFrameContext.additive) != 0
104+
? Renderer::BlendMode::Function::One
105+
: Renderer::BlendMode::Function::OneMinusSourceAlpha);
102106

103107
auto& vertexData = m_fillMesh.Vertices();
104108
auto& colorData = m_fillMesh.Colors();
@@ -281,7 +285,7 @@ void CustomShape::Draw()
281285
#ifndef USE_GLES
282286
glDisable(GL_LINE_SMOOTH);
283287
#endif
284-
glDisable(GL_BLEND);
288+
Renderer::BlendMode::SetBlendActive(false);
285289
}
286290

287291
} // namespace MilkdropPreset

src/libprojectM/MilkdropPreset/CustomWaveform.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "PerFrameContext.hpp"
44
#include "PresetFileParser.hpp"
55

6+
#include <Renderer/BlendMode.hpp>
7+
68
#include <algorithm>
79
#include <cmath>
810

@@ -161,14 +163,13 @@ void CustomWaveform::Draw(const PerFrameContext& presetPerFrameContext)
161163
glLineWidth(1);
162164

163165
// Additive wave drawing (vice overwrite)
164-
glEnable(GL_BLEND);
165166
if (m_additive)
166167
{
167-
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
168+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::One);
168169
}
169170
else
170171
{
171-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);
172173
}
173174

174175
auto shader = m_presetState.untexturedShader.lock();
@@ -223,8 +224,7 @@ void CustomWaveform::Draw(const PerFrameContext& presetPerFrameContext)
223224

224225
m_mesh.Unbind();
225226
Renderer::Shader::Unbind();
226-
227-
glDisable(GL_BLEND);
227+
Renderer::BlendMode::SetBlendActive(false);
228228
}
229229

230230
void CustomWaveform::LoadPerFrameEvaluationVariables(const PerFrameContext& presetPerFrameContext)

src/libprojectM/MilkdropPreset/DarkenCenter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "DarkenCenter.hpp"
22

3+
#include <Renderer/BlendMode.hpp>
4+
35
namespace libprojectM {
46
namespace MilkdropPreset {
57

@@ -35,16 +37,15 @@ void DarkenCenter::Draw()
3537
m_mesh.Update();
3638
}
3739

38-
glEnable(GL_BLEND);
39-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
40+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);
4041

4142
auto shader = m_presetState.untexturedShader.lock();
4243
shader->Bind();
4344
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
4445

4546
m_mesh.Draw();
4647

47-
glDisable(GL_BLEND);
48+
Renderer::BlendMode::SetBlendActive(false);
4849
Renderer::Mesh::Unbind();
4950
Renderer::Shader::Unbind();
5051
}

src/libprojectM/MilkdropPreset/Filters.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "Filters.hpp"
22

3+
#include <Renderer/BlendMode.hpp>
4+
5+
using libprojectM::Renderer::BlendMode;
6+
37
namespace libprojectM {
48
namespace MilkdropPreset {
59

@@ -20,7 +24,7 @@ void Filters::Draw()
2024
return;
2125
}
2226

23-
glEnable(GL_BLEND);
27+
BlendMode::SetBlendActive(true);
2428

2529
auto shader = m_presetState.untexturedShader.lock();
2630
shader->Bind();
@@ -48,37 +52,37 @@ void Filters::Draw()
4852
Renderer::Mesh::Unbind();
4953
Renderer::Shader::Unbind();
5054

51-
glDisable(GL_BLEND);
55+
BlendMode::SetBlendActive(false);
5256
}
5357

5458

5559
void Filters::Brighten()
5660
{
57-
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
61+
BlendMode::SetBlendFunction(BlendMode::Function::OneMinusDestinationColor, BlendMode::Function::Zero);
5862
m_filterMesh.Draw();
59-
glBlendFunc(GL_ZERO, GL_DST_COLOR);
63+
BlendMode::SetBlendFunction(BlendMode::Function::Zero, BlendMode::Function::DestinationColor);
6064
m_filterMesh.Draw();
61-
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
65+
BlendMode::SetBlendFunction(BlendMode::Function::OneMinusDestinationColor, BlendMode::Function::Zero);
6266
m_filterMesh.Draw();
6367
}
6468

6569
void Filters::Darken()
6670
{
67-
glBlendFunc(GL_ZERO, GL_DST_COLOR);
71+
BlendMode::SetBlendFunction(BlendMode::Function::Zero, BlendMode::Function::DestinationColor);
6872
m_filterMesh.Draw();
6973
}
7074

7175
void Filters::Solarize()
7276
{
73-
glBlendFunc(GL_ZERO, GL_ONE_MINUS_DST_COLOR);
77+
BlendMode::SetBlendFunction(BlendMode::Function::Zero, BlendMode::Function::OneMinusDestinationColor);
7478
m_filterMesh.Draw();
75-
glBlendFunc(GL_DST_COLOR, GL_ONE);
79+
BlendMode::SetBlendFunction(BlendMode::Function::DestinationColor, BlendMode::Function::One);
7680
m_filterMesh.Draw();
7781
}
7882

7983
void Filters::Invert()
8084
{
81-
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
85+
BlendMode::SetBlendFunction(BlendMode::Function::OneMinusDestinationColor, BlendMode::Function::Zero);
8286
m_filterMesh.Draw();
8387
}
8488

src/libprojectM/MilkdropPreset/FinalComposite.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "PresetState.hpp"
44

5+
#include <Renderer/BlendMode.hpp>
6+
57
#include <cstddef>
68

79
#ifdef MILKDROP_PRESET_DEBUG
@@ -117,7 +119,7 @@ void FinalComposite::Draw(const PresetState& presetState, const PerFrameContext&
117119
ApplyHueShaderColors(presetState);
118120

119121
// Render the grid
120-
glDisable(GL_BLEND);
122+
Renderer::BlendMode::SetBlendActive(false);
121123
m_compositeShader->LoadVariables(presetState, perFrameContext);
122124

123125
m_compositeMesh.Draw();

src/libprojectM/MilkdropPreset/MotionVectors.cpp

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

33
#include "MilkdropStaticShaders.hpp"
44

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

@@ -62,8 +63,7 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
6263

6364
m_motionVectorMesh.SetVertexCount(static_cast<std::size_t>(countX + 1) * 2); // countX + 1 lines for each grid row, 2 vertices each.
6465

65-
glEnable(GL_BLEND);
66-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
66+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::SourceAlpha, Renderer::BlendMode::Function::OneMinusSourceAlpha);
6767

6868
auto shader = GetShader();
6969
shader->Bind();
@@ -120,7 +120,7 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
120120
glDisable(GL_LINE_SMOOTH);
121121
#endif
122122

123-
glDisable(GL_BLEND);
123+
Renderer::BlendMode::SetBlendActive(false);
124124
}
125125

126126
std::shared_ptr<Renderer::Shader> MotionVectors::GetShader()

src/libprojectM/MilkdropPreset/PerPixelMesh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "PerPixelContext.hpp"
77
#include "PresetState.hpp"
88

9-
#include "Renderer/ShaderCache.hpp"
9+
#include <Renderer/BlendMode.hpp>
10+
#include <Renderer/ShaderCache.hpp>
1011

1112
#include <algorithm>
1213
#include <cmath>
@@ -320,7 +321,7 @@ void PerPixelMesh::WarpedBlit(const PresetState& presetState,
320321
}
321322

322323
// No blending between presets here, so we make sure blending is disabled.
323-
glDisable(GL_BLEND);
324+
Renderer::BlendMode::SetBlendActive(false);
324325

325326
if (!m_warpShader)
326327
{

src/libprojectM/MilkdropPreset/VideoEcho.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "VideoEcho.hpp"
22

3+
#include <Renderer/BlendMode.hpp>
4+
35
namespace libprojectM {
46
namespace MilkdropPreset {
57

@@ -76,8 +78,7 @@ void VideoEcho::Draw()
7678
DrawGammaAdjustment();
7779
}
7880

79-
glDisable(GL_BLEND);
80-
81+
Renderer::BlendMode::SetBlendActive(false);
8182
Renderer::Mesh::Unbind();
8283
Renderer::Shader::Unbind();
8384

@@ -95,8 +96,7 @@ void VideoEcho::DrawVideoEcho()
9596
auto const videoEchoOrientation = m_presetState.videoEchoOrientation % 4;
9697
auto const gammaAdj = m_presetState.gammaAdj;
9798

98-
glEnable(GL_BLEND);
99-
glBlendFunc(GL_ONE, GL_ZERO);
99+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::One, Renderer::BlendMode::Function::Zero);
100100

101101
for (int pass = 0; pass < 2; pass++)
102102
{
@@ -141,7 +141,7 @@ void VideoEcho::DrawVideoEcho()
141141

142142
if (pass == 0)
143143
{
144-
glBlendFunc(GL_ONE, GL_ONE);
144+
Renderer::BlendMode::SetBlendFunction(Renderer::BlendMode::Function::One, Renderer::BlendMode::Function::One);
145145
}
146146

147147
if (gammaAdj > 0.001f)
@@ -183,8 +183,7 @@ void VideoEcho::DrawGammaAdjustment()
183183
{0.0f, 1.0f},
184184
{1.0f, 1.0f}});
185185

186-
glDisable(GL_BLEND);
187-
glBlendFunc(GL_ONE, GL_ZERO);
186+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::One, Renderer::BlendMode::Function::Zero);
188187

189188
auto const gammaAdj = m_presetState.gammaAdj;
190189
int const redrawCount = static_cast<int>(gammaAdj - 0.0001f) + 1;
@@ -215,8 +214,7 @@ void VideoEcho::DrawGammaAdjustment()
215214

216215
if (redraw == 0)
217216
{
218-
glEnable(GL_BLEND);
219-
glBlendFunc(GL_ONE, GL_ONE);
217+
Renderer::BlendMode::Set(true, Renderer::BlendMode::Function::One, Renderer::BlendMode::Function::One);
220218
}
221219
}
222220
}

0 commit comments

Comments
 (0)