-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4287fb4
commit 92e690c
Showing
6 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "DrawBatcher.h" | ||
|
||
DrawBatcher* DrawBatcher::s_batcher = nullptr; | ||
|
||
DrawBatcher::DrawBatcher() : m_vertices() | ||
{ | ||
m_vertices.reserve(0x2000); | ||
} | ||
|
||
void DrawBatcher::DrawTriangles(DRAWVERTEX* verts, int numtris) noexcept | ||
{ | ||
auto numVerts = numtris * 3; | ||
|
||
// Prevent allocations | ||
if (m_vertices.size() + numVerts >= m_vertices.capacity()) | ||
{ | ||
Flush(); | ||
} | ||
|
||
// Add to batch | ||
for (int i = 0; i < numVerts; i++) | ||
{ | ||
m_vertices.push_back(verts[i]); | ||
} | ||
} | ||
|
||
void DrawBatcher::Flush() noexcept | ||
{ | ||
if (m_vertices.size() > 0) | ||
{ | ||
DRAW_DrawTriangles(2, 0, m_vertices.data(), m_vertices.size() / 3); | ||
|
||
m_vertices.clear(); | ||
} | ||
} | ||
|
||
DrawBatcher* DrawBatcher::GetInstance() | ||
{ | ||
// Since this is hot code which is called a lot during a frame | ||
// there's a manual singleton here instead of a C++11 standard one | ||
// which uses thread local storage | ||
if (s_batcher == nullptr) | ||
{ | ||
s_batcher = new DrawBatcher(); | ||
} | ||
|
||
return s_batcher; | ||
} | ||
|
||
#ifdef BATCH_DRAW_CALLS | ||
void DrawTriangles(DRAWVERTEX* verts, int numtris) | ||
{ | ||
DrawBatcher::GetInstance()->DrawTriangles(verts, numtris); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "Draw.h" | ||
|
||
// Batcher for DRAW_ calls to limit the amount of draw calls sent to the GPU | ||
class DrawBatcher | ||
{ | ||
private: | ||
std::vector<DRAWVERTEX> m_vertices; | ||
|
||
static DrawBatcher* s_batcher; | ||
|
||
public: | ||
DrawBatcher(); | ||
|
||
void DrawTriangles(DRAWVERTEX* verts, int numtris) noexcept; | ||
void Flush() noexcept; | ||
|
||
static DrawBatcher* GetInstance(); | ||
}; |