Skip to content

Commit e746fb8

Browse files
committed
Use Mesh class in Border effect
Reduced draw calls from 4/8 to 1/2 by specifying 8 vertices accessed via indices to draw each border in a single call.
1 parent 87e2698 commit e746fb8

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

src/libprojectM/MilkdropPreset/Border.cpp

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ namespace libprojectM {
44
namespace MilkdropPreset {
55

66
Border::Border(PresetState& presetState)
7-
: RenderItem()
8-
, m_presetState(presetState)
7+
: m_presetState(presetState)
8+
, m_borderMesh(Renderer::VertexBufferUsage::StreamDraw)
99
{
10-
RenderItem::Init();
11-
}
12-
13-
void Border::InitVertexAttrib()
14-
{
15-
glEnableVertexAttribArray(0);
16-
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
17-
glDisableVertexAttribArray(1);
10+
m_borderMesh.SetRenderPrimitiveType(Renderer::Mesh::PrimitiveType::Triangles);
11+
m_borderMesh.SetVertexCount(8);
1812

19-
std::array<Point, 4> vertices{};
20-
glBufferData(GL_ARRAY_BUFFER, sizeof(Point) * 4, vertices.data(), GL_STREAM_DRAW);
13+
m_borderMesh.Indices().Set({
14+
{
15+
0, 1, 4,
16+
1, 4, 5,
17+
2, 3, 6,
18+
3, 7, 6,
19+
2, 0, 6,
20+
0, 4, 6,
21+
3, 7, 5,
22+
1, 3, 5
23+
}});
2124
}
2225

2326
void Border::Draw(const PerFrameContext& presetPerFrameContext)
@@ -26,9 +29,6 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
2629
float const outerBorderSize = static_cast<float>(*presetPerFrameContext.ob_size);
2730
float const innerBorderSize = static_cast<float>(*presetPerFrameContext.ib_size);
2831

29-
glBindVertexArray(m_vaoID);
30-
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
31-
3232
// No additive drawing for borders
3333
glEnable(GL_BLEND);
3434
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -37,7 +37,8 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
3737
shader->Bind();
3838
shader->SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
3939

40-
std::array<Point, 4> vertices{};
40+
m_borderMesh.Bind();
41+
4142
for (int border = 0; border < 2; border++)
4243
{
4344
float r = (border == 0) ? static_cast<float>(*presetPerFrameContext.ob_r) : static_cast<float>(*presetPerFrameContext.ib_r);
@@ -52,39 +53,24 @@ void Border::Draw(const PerFrameContext& presetPerFrameContext)
5253
float innerRadius = (border == 0) ? 1.0f - outerBorderSize : 1.0f - outerBorderSize - innerBorderSize;
5354
float outerRadius = (border == 0) ? 1.0f : 1.0f - outerBorderSize;
5455

55-
vertices[0].x = innerRadius;
56-
vertices[1].x = outerRadius;
57-
vertices[2].x = outerRadius;
58-
vertices[3].x = innerRadius;
59-
vertices[0].y = innerRadius;
60-
vertices[1].y = outerRadius;
61-
vertices[2].y = -outerRadius;
62-
vertices[3].y = -innerRadius;
63-
64-
for (int rot = 0; rot < 4; rot++)
65-
{
66-
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Point) * 4, vertices.data());
67-
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
68-
69-
// Rotate 90 degrees
70-
// Milkdrop code calculates cos(PI/2) and sin(PI/2), which is 0 and 1 respectively.
71-
// Our code here simplifies the expressions accordingly.
72-
for (int vertex = 0; vertex < 4; vertex++)
73-
{
74-
float const x = vertices[vertex].x;
75-
float const y = vertices[vertex].y;
76-
vertices[vertex].x = -y; // x * cos(PI/2) - y * sin(PI/2) == x * 0 - y * 1
77-
vertices[vertex].y = x; // x * sin(PI/2) + y * cos(PI/2) == x * 1 + y * 0
78-
}
79-
}
56+
m_borderMesh.Vertices().Set({{outerRadius, outerRadius},
57+
{outerRadius, -outerRadius},
58+
{-outerRadius, outerRadius},
59+
{-outerRadius, -outerRadius},
60+
{innerRadius, innerRadius},
61+
{innerRadius, -innerRadius},
62+
{-innerRadius, innerRadius},
63+
{-innerRadius, -innerRadius}});
64+
65+
m_borderMesh.Update();
66+
m_borderMesh.Draw();
8067
}
8168
}
8269

70+
Renderer::Mesh::Unbind();
8371
Renderer::Shader::Unbind();
8472

8573
glDisable(GL_BLEND);
86-
glBindBuffer(GL_ARRAY_BUFFER, 0);
87-
glBindVertexArray(0);
8874
}
8975

9076
} // namespace MilkdropPreset

src/libprojectM/MilkdropPreset/Border.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "PerFrameContext.hpp"
44
#include "PresetState.hpp"
55

6-
#include "Renderer/RenderItem.hpp"
6+
#include <Renderer/Mesh.hpp>
77

88
namespace libprojectM {
99
namespace MilkdropPreset {
@@ -12,15 +12,13 @@ namespace MilkdropPreset {
1212
/**
1313
* @brief Renders a border around the screen.
1414
*/
15-
class Border : public Renderer::RenderItem
15+
class Border
1616
{
1717
public:
1818
Border() = delete;
1919

2020
explicit Border(PresetState& presetState);
2121

22-
void InitVertexAttrib() override;
23-
2422
/**
2523
* Draws the border.
2624
* @param presetPerFrameContext The per-frame context variables.
@@ -29,6 +27,7 @@ class Border : public Renderer::RenderItem
2927

3028
private:
3129
PresetState& m_presetState; //!< The global preset state.
30+
Renderer::Mesh m_borderMesh; //!< The border geometry.
3231
};
3332

3433
} // namespace MilkdropPreset

0 commit comments

Comments
 (0)