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:
@@ -53,6 +53,16 @@ SOFTWARE.
|
||||
return std::make_shared<x>(std::forward<Args>(args)...); \
|
||||
}
|
||||
|
||||
#define PD_RAW(x) \
|
||||
using Ref = x*; \
|
||||
template <typename... Args> \
|
||||
static Ref New(Args&&... args) { \
|
||||
x* v = new x; \
|
||||
new (v) x(std::forward<Args>(args)...); \
|
||||
return v; \
|
||||
} \
|
||||
static void Delete(Ref ref) { delete ref; }
|
||||
|
||||
#define PD_UNIQUE(x) \
|
||||
using Ref = std::unique_ptr<x>; \
|
||||
template <typename... Args> \
|
||||
|
||||
@@ -49,7 +49,7 @@ class GfxDriver {
|
||||
|
||||
virtual void BindTex(Li::TexAddress addr) {}
|
||||
|
||||
virtual void RenderDrawData(const std::vector<Li::Command::Ref>& Commands) {}
|
||||
virtual void RenderDrawData(const Li::CmdPool& Commands) {}
|
||||
|
||||
void SetViewPort(const ivec2& vp) { ViewPort = vp; }
|
||||
|
||||
@@ -96,7 +96,7 @@ class Gfx {
|
||||
static void SetViewPort(const ivec2& vp) { pGfx->SetViewPort(vp); }
|
||||
static void SetViewPort(int w, int h) { pGfx->SetViewPort(PD::ivec2(w, h)); }
|
||||
|
||||
static void RenderDrawData(const std::vector<Li::Command::Ref>& Commands) {
|
||||
static void RenderDrawData(const Li::CmdPool& Commands) {
|
||||
pGfx->RenderDrawData(Commands);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,6 +38,7 @@ class ID {
|
||||
*/
|
||||
ID(const std::string& text) {
|
||||
pID = PD::Strings::FastHash(text);
|
||||
// pStrName = text;
|
||||
pName = text;
|
||||
}
|
||||
/**
|
||||
@@ -65,8 +66,9 @@ class ID {
|
||||
/** Return the ID when casting to u32 */
|
||||
constexpr operator u32() const { return pID; }
|
||||
|
||||
u32 pID; ///< Hash of the name
|
||||
std::string_view pName; ///< Name
|
||||
u32 pID; ///< Hash of the name
|
||||
std::string pName; ///< Name
|
||||
// std::string pStrName; ///< Keep this stringview alive
|
||||
};
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
Reference in New Issue
Block a user