Skip to content

Commit da76777

Browse files
committed
Refactor Buffer.h interface
1 parent 9baac78 commit da76777

7 files changed

Lines changed: 42 additions & 59 deletions

File tree

OverEditor/src/UI/Panels/ViewportPanel.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ namespace OverEditor
5959
0.5f, 0.5f, 1.0f, 1.0f,
6060
-0.5f, 0.5f, 0.0f, 1.0f
6161
};
62-
s_Data->GizmoVB->BufferData(quadVertices, sizeof(quadVertices));
62+
s_Data->GizmoVB->Allocate(sizeof(quadVertices), quadVertices);
6363
s_Data->GizmoVA->AddVertexBuffer(s_Data->GizmoVB);
6464

6565
s_Data->GizmoIB = IndexBuffer::Create();
6666
static constexpr uint32_t quadIndices[] = { 0, 1, 2, 2, 3, 0 };
67-
s_Data->GizmoIB->BufferData(quadIndices, OE_ARRAY_SIZE(quadIndices));
67+
s_Data->GizmoIB->Allocate(OE_ARRAY_SIZE(quadIndices), quadIndices);
6868
s_Data->GizmoVA->SetIndexBuffer(s_Data->GizmoIB);
6969

7070
// Grid
@@ -83,11 +83,11 @@ namespace OverEditor
8383
1.0f, 1.0f, 1.0f, 1.0f,
8484
-1.0f, 1.0f, 0.0f, 1.0f
8585
};
86-
s_Data->GridVB->BufferData(gridVertices, sizeof(gridVertices));
86+
s_Data->GridVB->Allocate(sizeof(gridVertices), gridVertices);
8787
s_Data->GridVA->AddVertexBuffer(s_Data->GridVB);
8888

8989
s_Data->GridIB = IndexBuffer::Create();
90-
s_Data->GridIB->BufferData(quadIndices, OE_ARRAY_SIZE(quadIndices));
90+
s_Data->GridIB->Allocate(OE_ARRAY_SIZE(quadIndices), quadIndices);
9191
s_Data->GridVA->SetIndexBuffer(s_Data->GridIB);
9292
}
9393
}

OverEngine/src/OverEngine/Renderer/Buffer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace OverEngine
1010
{
11-
Ref<VertexBuffer> VertexBuffer::Create(const void* vertices, uint32_t size, bool staticDraw)
11+
Ref<VertexBuffer> VertexBuffer::Create(const void* vertices, uint32_t size)
1212
{
1313
switch (RendererAPI::GetAPI())
1414
{
1515
case RendererAPI::API::None: OE_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
16-
case RendererAPI::API::OpenGL: return CreateRef<OpenGLVertexBuffer>(vertices, size, staticDraw);
16+
case RendererAPI::API::OpenGL: return CreateRef<OpenGLVertexBuffer>(vertices, size);
1717
}
1818

1919
OE_CORE_ASSERT(false, "Unknown RendererAPI!");
@@ -32,12 +32,12 @@ namespace OverEngine
3232
return nullptr;
3333
}
3434

35-
Ref<IndexBuffer> IndexBuffer::Create(const uint32_t* indices, uint32_t count, bool staticDraw)
35+
Ref<IndexBuffer> IndexBuffer::Create(const uint32_t* indices, uint32_t count)
3636
{
3737
switch (RendererAPI::GetAPI())
3838
{
3939
case RendererAPI::API::None: OE_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
40-
case RendererAPI::API::OpenGL: return CreateRef<OpenGLIndexBuffer>(indices, count, staticDraw);
40+
case RendererAPI::API::OpenGL: return CreateRef<OpenGLIndexBuffer>(indices, count);
4141
}
4242

4343
OE_CORE_ASSERT(false, "Unknown RendererAPI!");

OverEngine/src/OverEngine/Renderer/Buffer.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,21 @@ namespace OverEngine
100100
uint32_t m_Stride = 0;
101101
};
102102

103-
// TODO: Refactor: BufferData and AllocateStorage are basically the same
104-
// TODO: Use and enum instead of staticDraw
105-
103+
// TODO: Add support for specifying allocation mode (static/dynamic/stream)
104+
106105
class VertexBuffer
107106
{
108107
public:
109108
static Ref<VertexBuffer> Create();
110-
static Ref<VertexBuffer> Create(const void* vertices, uint32_t size, bool staticDraw = true);
109+
static Ref<VertexBuffer> Create(const void* vertices, uint32_t size);
111110

112111
virtual ~VertexBuffer() = default;
113112

114113
virtual void Bind() const = 0;
115114
virtual void Unbind() const = 0;
116115

117-
virtual void BufferData(const void* vertices, uint32_t size, bool staticDraw = true) const = 0;
118-
virtual void BufferSubData(const void* vertices, uint32_t size, uint32_t offset = 0) const = 0;
119-
virtual void AllocateStorage(uint32_t size) const = 0;
116+
virtual void Allocate(uint32_t size, const void* vertices = nullptr) const = 0;
117+
virtual void Upload(const void* vertices, uint32_t size, uint32_t offset = 0) const = 0;
120118

121119
virtual const BufferLayout& GetLayout() const = 0;
122120
virtual void SetLayout(const BufferLayout& layout) = 0;
@@ -126,16 +124,15 @@ namespace OverEngine
126124
{
127125
public:
128126
static Ref<IndexBuffer> Create();
129-
static Ref<IndexBuffer> Create(const uint32_t* indices, uint32_t count, bool staticDraw = true);
127+
static Ref<IndexBuffer> Create(const uint32_t* indices, uint32_t count);
130128

131129
virtual ~IndexBuffer() = default;
132130

133131
virtual void Bind() const = 0;
134132
virtual void Unbind() const = 0;
135133

136-
virtual void BufferData(const uint32_t* indices, uint32_t count, bool staticDraw = true) const = 0;
137-
virtual void BufferSubData(const uint32_t* indices, uint32_t count, uint32_t offset = 0) const = 0;
138-
virtual void AllocateStorage(uint32_t count) const = 0;
134+
virtual void Allocate(uint32_t count, const uint32_t* indices = nullptr) const = 0;
135+
virtual void Upload(const uint32_t* indices, uint32_t count, uint32_t offset = 0) const = 0;
139136

140137
virtual uint32_t GetCount() const = 0;
141138
};

OverEngine/src/OverEngine/Renderer/Renderer2D.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace OverEngine
5858
s_Data->QuadVA = VertexArray::Create();
5959

6060
s_Data->QuadVB = VertexBuffer::Create();
61-
s_Data->QuadVB->AllocateStorage(MaxQuadCount * 4 * sizeof(Vertex));
61+
s_Data->QuadVB->Allocate(MaxQuadCount * 4 * sizeof(Vertex));
6262
s_Data->QuadVB->SetLayout({
6363
{ ShaderDataType::Float3, "a_Position" },
6464

@@ -88,7 +88,7 @@ namespace OverEngine
8888
indices[6 * i + 5] = 4 * i + 0;
8989
}
9090

91-
quadIB->BufferData(indices, MaxQuadCount);
91+
quadIB->Allocate(MaxQuadCount, indices);
9292
delete[] indices;
9393

9494
s_Data->QuadVA->SetIndexBuffer(quadIB);
@@ -183,7 +183,7 @@ namespace OverEngine
183183
}
184184

185185
// Upload Data
186-
s_Data->QuadVB->BufferSubData((void*)s_Data->QuadBufferBasePtr, 4 * s_Data->QuadCount * sizeof(Vertex));
186+
s_Data->QuadVB->Upload((void*)s_Data->QuadBufferBasePtr, 4 * s_Data->QuadCount * sizeof(Vertex));
187187

188188
// Bind Textures
189189
for (uint8_t i = 0; i < s_Data->TextureCount; i++)

OverEngine/src/Platform/OpenGL/OpenGLBuffer.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace OverEngine
99
// VertexBuffer /////////////////////////////////////////////////////////////
1010
/////////////////////////////////////////////////////////////////////////////
1111

12-
OpenGLVertexBuffer::OpenGLVertexBuffer(const void* vertices, uint32_t size, bool staticDraw)
12+
OpenGLVertexBuffer::OpenGLVertexBuffer(const void* vertices, uint32_t size)
1313
{
1414
glCreateBuffers(1, &m_RendererID);
15-
BufferData(vertices, size, staticDraw);
15+
Allocate(size, vertices);
1616
}
1717

1818
OpenGLVertexBuffer::OpenGLVertexBuffer()
@@ -34,34 +34,28 @@ namespace OverEngine
3434
{
3535
glBindBuffer(GL_ARRAY_BUFFER, 0);
3636
}
37-
38-
void OpenGLVertexBuffer::BufferData(const void* vertices, uint32_t size, bool staticDraw) const
37+
38+
void OpenGLVertexBuffer::Allocate(uint32_t size, const void* vertices) const
3939
{
4040
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
41-
glBufferData(GL_ARRAY_BUFFER, size, vertices, staticDraw ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
41+
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_DYNAMIC_DRAW);
4242
}
4343

44-
void OpenGLVertexBuffer::BufferSubData(const void* vertices, uint32_t size, uint32_t offset) const
44+
void OpenGLVertexBuffer::Upload(const void* vertices, uint32_t size, uint32_t offset) const
4545
{
4646
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
4747
glBufferSubData(GL_ARRAY_BUFFER, offset, size, vertices);
4848
}
4949

50-
void OpenGLVertexBuffer::AllocateStorage(uint32_t size) const
51-
{
52-
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
53-
glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
54-
}
55-
5650
/////////////////////////////////////////////////////////////////////////////
5751
// IndexBuffer //////////////////////////////////////////////////////////////
5852
/////////////////////////////////////////////////////////////////////////////
5953

60-
OpenGLIndexBuffer::OpenGLIndexBuffer(const uint32_t* indices, uint32_t count, bool staticDraw)
54+
OpenGLIndexBuffer::OpenGLIndexBuffer(const uint32_t* indices, uint32_t count)
6155
: m_Count(count)
6256
{
6357
glCreateBuffers(1, &m_RendererID);
64-
BufferData(indices, count, staticDraw);
58+
Allocate(count, indices);
6559
}
6660

6761
OpenGLIndexBuffer::OpenGLIndexBuffer()
@@ -85,26 +79,20 @@ namespace OverEngine
8579
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
8680
}
8781

88-
void OpenGLIndexBuffer::BufferData(const uint32_t* indices, uint32_t count, bool staticDraw) const
82+
void OpenGLIndexBuffer::Allocate(uint32_t count, const uint32_t* indices) const
8983
{
84+
// TODO: Keep this?
85+
// if (count <= m_Count)
86+
// return;
87+
9088
m_Count = count;
9189
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
92-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), indices, staticDraw ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
90+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), indices, GL_DYNAMIC_DRAW);
9391
}
9492

95-
void OpenGLIndexBuffer::BufferSubData(const uint32_t* indices, uint32_t count, uint32_t offset) const
93+
void OpenGLIndexBuffer::Upload(const uint32_t* indices, uint32_t count, uint32_t offset) const
9694
{
9795
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
9896
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset * sizeof(uint32_t), count * sizeof(uint32_t), indices);
9997
}
100-
101-
void OpenGLIndexBuffer::AllocateStorage(uint32_t count) const
102-
{
103-
if (count <= m_Count)
104-
return;
105-
106-
m_Count = count;
107-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
108-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), nullptr, GL_DYNAMIC_DRAW);
109-
}
11098
}

OverEngine/src/Platform/OpenGL/OpenGLBuffer.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ namespace OverEngine
88
{
99
public:
1010
OpenGLVertexBuffer();
11-
OpenGLVertexBuffer(const void* vertices, uint32_t size, bool staticDraw);
11+
OpenGLVertexBuffer(const void* vertices, uint32_t size);
1212
virtual ~OpenGLVertexBuffer();
1313

1414
virtual void Bind() const override;
1515
virtual void Unbind() const override;
1616

17-
virtual void BufferData(const void* vertices, uint32_t size, bool staticDraw = true) const override;
18-
virtual void BufferSubData(const void* vertices, uint32_t size, uint32_t offset = 0) const override;
19-
virtual void AllocateStorage(uint32_t size) const override;
17+
virtual void Allocate(uint32_t size, const void* vertices = nullptr) const override;
18+
virtual void Upload(const void* vertices, uint32_t size, uint32_t offset = 0) const override;
2019

2120
virtual const BufferLayout& GetLayout() const override { return m_Layout; }
2221
virtual void SetLayout(const BufferLayout& layout) override { m_Layout = layout; }
@@ -29,15 +28,14 @@ namespace OverEngine
2928
{
3029
public:
3130
OpenGLIndexBuffer();
32-
OpenGLIndexBuffer(const uint32_t* indices, uint32_t count, bool staticDraw);
31+
OpenGLIndexBuffer(const uint32_t* indices, uint32_t count);
3332
virtual ~OpenGLIndexBuffer();
3433

3534
virtual void Bind() const override;
3635
virtual void Unbind() const override;
3736

38-
virtual void BufferData(const uint32_t* indices, uint32_t count, bool staticDraw = true) const override;
39-
virtual void BufferSubData(const uint32_t* indices, uint32_t count, uint32_t offset = 0) const override;
40-
virtual void AllocateStorage(uint32_t count) const override;
37+
virtual void Allocate(uint32_t count, const uint32_t* indices = nullptr) const override;
38+
virtual void Upload(const uint32_t* indices, uint32_t count, uint32_t offset = 0) const override;
4139

4240
virtual uint32_t GetCount() const override { return m_Count; }
4341
private:

Sandbox/src/LiquidGlass/BlurPass.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class BlurPass {
2727
Vertex { { 1.0, 1.0, 0.0 }, { 1.0, 1.0 } },
2828
Vertex { { -1., 1.0, 0.0 }, { 0.0, 1.0 } }
2929
};
30-
m_VB->BufferData(vertices, 4 * sizeof(Vertex), true);
30+
m_VB->Allocate(4 * sizeof(Vertex), vertices);
3131

3232

3333
uint32_t indices[6] = {0, 1, 2, 2, 3, 0 };
34-
m_IB = IndexBuffer::Create(indices, 6, false);
34+
m_IB = IndexBuffer::Create(indices, 6);
3535

3636
m_VA = VertexArray::Create();
3737
m_VA->AddVertexBuffer(m_VB);

0 commit comments

Comments
 (0)