Refactor the Command / DrawList System
Use Command Pool instead of always allocating. This gives us e big performance diffrence on the 3ds Fixed IDS of ui7 for now
This commit is contained in:
@@ -34,7 +34,7 @@ class Command {
|
||||
Command() = default;
|
||||
~Command() = default;
|
||||
|
||||
PD_UNIQUE(Command);
|
||||
PD_RAW(Command);
|
||||
|
||||
Command& AddIdx(const u16& idx) {
|
||||
IndexBuffer.push_back(VertexBuffer.size() + idx);
|
||||
@@ -52,6 +52,16 @@ class Command {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
VertexBuffer.clear();
|
||||
IndexBuffer.clear();
|
||||
Index = 0;
|
||||
Layer = 0;
|
||||
Tex = 0;
|
||||
ScissorOn = false;
|
||||
ScissorRect = ivec4();
|
||||
}
|
||||
|
||||
std::vector<Vertex> VertexBuffer;
|
||||
std::vector<u16> IndexBuffer;
|
||||
ivec4 ScissorRect;
|
||||
@@ -60,5 +70,71 @@ class Command {
|
||||
int Index;
|
||||
Texture::Ref Tex;
|
||||
};
|
||||
|
||||
class CmdPool {
|
||||
public:
|
||||
CmdPool() {}
|
||||
~CmdPool() {}
|
||||
|
||||
Command::Ref NewCmd() {
|
||||
if (pPoolIdx >= pPool.size()) {
|
||||
Resize(pPool.size() + 128);
|
||||
}
|
||||
Command::Ref nu = pPool[pPoolIdx++];
|
||||
nu->Layer = Layer;
|
||||
nu->Index = pPoolIdx - 1;
|
||||
return nu;
|
||||
}
|
||||
|
||||
void Init(size_t initial_size) { Resize(initial_size); }
|
||||
|
||||
void Deinit() {
|
||||
for (auto it : pPool) {
|
||||
Command::Delete(it);
|
||||
}
|
||||
pPool.clear();
|
||||
}
|
||||
|
||||
void Resize(size_t nulen) {
|
||||
if (nulen <= pPool.size()) {
|
||||
return; // no idea yet
|
||||
}
|
||||
size_t oldlen = pPool.size();
|
||||
pPool.resize(nulen);
|
||||
for (size_t i = oldlen; i < pPool.size(); i++) {
|
||||
pPool[i] = Command::New();
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
for (u32 i = 0; i < pPoolIdx; i++) {
|
||||
pPool[i]->Clear();
|
||||
}
|
||||
pPoolIdx = 0;
|
||||
}
|
||||
|
||||
Command::Ref GetCmd(size_t idx) const { return pPool[idx]; }
|
||||
Command::Ref GetCmd(size_t idx) { return pPool[idx]; }
|
||||
|
||||
size_t Size() const { return pPoolIdx; }
|
||||
size_t Cap() const { return pPool.size(); }
|
||||
|
||||
void Merge(CmdPool& p) {
|
||||
if (pPoolIdx + p.Size() > pPool.size()) {
|
||||
Resize(pPoolIdx + p.Size());
|
||||
}
|
||||
for (size_t i = 0; i < p.Size(); i++) {
|
||||
size_t idx = pPoolIdx++;
|
||||
*pPool[idx] = *p.GetCmd(i);
|
||||
pPool[idx]->Index = idx;
|
||||
}
|
||||
p.Reset();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Command::Ref> pPool;
|
||||
u32 pPoolIdx = 0;
|
||||
int Layer = 0;
|
||||
};
|
||||
} // namespace Li
|
||||
} // namespace PD
|
||||
@@ -48,8 +48,8 @@ namespace PD {
|
||||
namespace Li {
|
||||
class PD_LITHIUM_API DrawList {
|
||||
public:
|
||||
DrawList() { DrawSolid(); }
|
||||
~DrawList() { pDrawList.clear(); }
|
||||
DrawList(int initial_size = 64);
|
||||
~DrawList();
|
||||
|
||||
/** Require Copy and Move Constructors */
|
||||
|
||||
@@ -81,8 +81,7 @@ class PD_LITHIUM_API DrawList {
|
||||
*/
|
||||
void Optimize();
|
||||
|
||||
Command::Ref PreGenerateCmd();
|
||||
void AddCommand(Command::Ref v);
|
||||
Command::Ref GetNewCmd();
|
||||
void Clear();
|
||||
void Layer(int l) { this->pLayer = l; }
|
||||
int Layer() { return this->pLayer; }
|
||||
@@ -206,7 +205,7 @@ class PD_LITHIUM_API DrawList {
|
||||
}
|
||||
pClipRects.pop();
|
||||
}
|
||||
const std::vector<Command::Ref>& Data() const { return pDrawList; }
|
||||
const CmdPool& Data() const { return pPool; }
|
||||
/** One linear Clip rect Setup */
|
||||
void pClipCmd(Command* cmd);
|
||||
|
||||
@@ -217,7 +216,7 @@ class PD_LITHIUM_API DrawList {
|
||||
float pFontScale = 0.7f;
|
||||
Font::Ref pCurrentFont;
|
||||
Texture::Ref CurrentTex;
|
||||
std::vector<Command::Ref> pDrawList;
|
||||
CmdPool pPool;
|
||||
std::vector<fvec2> pPath;
|
||||
u32 pNumIndices = 0;
|
||||
u32 pNumVertices = 0;
|
||||
|
||||
@@ -93,8 +93,8 @@ class PD_LITHIUM_API Font {
|
||||
/**
|
||||
* Extended Draw Text Function that vreates a Command List
|
||||
*/
|
||||
void CmdTextEx(std::vector<Command::Ref>& cmds, const fvec2& pos, u32 color,
|
||||
float scale, const std::string& text, LiTextFlags flags = 0,
|
||||
void CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color, float scale,
|
||||
const std::string& text, LiTextFlags flags = 0,
|
||||
const fvec2& box = 0);
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user