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:
2026-01-16 12:13:48 +01:00
parent eb5d5f9974
commit 0ef6d34435
14 changed files with 161 additions and 86 deletions

View File

@@ -31,12 +31,22 @@ SOFTWARE.
namespace PD {
namespace Li {
PD_LITHIUM_API DrawList::DrawList(int initial_size) {
DrawSolid();
pPool.Init(initial_size);
}
PD_LITHIUM_API DrawList::~DrawList() {
Clear();
pPool.Deinit();
}
PD_LITHIUM_API void DrawList::DrawSolid() { CurrentTex = Gfx::GetSolidTex(); }
PD_LITHIUM_API void DrawList::Clear() {
pNumIndices = 0;
pNumVertices = 0;
pDrawList.clear();
pPool.Reset();
pPath.clear();
if (pCurrentFont) {
pCurrentFont->CleanupTMS();
@@ -46,18 +56,14 @@ PD_LITHIUM_API void DrawList::Clear() {
}
}
PD_LITHIUM_API void DrawList::AddCommand(Command::Ref v) {
pNumIndices += v->IndexBuffer.size();
pNumVertices += v->VertexBuffer.size();
pDrawList.push_back(std::move(v));
}
PD_LITHIUM_API void DrawList::Merge(DrawList::Ref list) {
for (size_t i = 0; i < list->pDrawList.size(); i++) {
pPool.Merge(list->pPool);
/*for (size_t i = 0; i < list->pDrawList.size(); i++) {
pNumIndices += list->pDrawList[i]->IndexBuffer.size();
pNumVertices += list->pDrawList[i]->VertexBuffer.size();
pDrawList.push_back(std::move(list->pDrawList[i]));
}
auto cmd = pPool.NewCmd();
pDrawList.push_back(list->pDrawList[i]);
}*/
/** Make sure The list gets cleared */
list->Clear();
}
@@ -66,7 +72,7 @@ PD_LITHIUM_API void DrawList::Optimize() {
#ifndef NDEBUG
PD::TT::Scope s("Optimize");
#endif
std::sort(pDrawList.begin(), pDrawList.end(),
/*std::sort(pDrawList.begin(), pDrawList.end(),
[](const PD::Li::Command::Ref &a, const PD::Li::Command::Ref &b) {
if (a->Layer == b->Layer) { // Same layer
if (a->Tex == b->Tex) { // same tex
@@ -75,19 +81,19 @@ PD_LITHIUM_API void DrawList::Optimize() {
return a->Tex < b->Tex; // order by address
}
return a->Layer < b->Layer; // Order by layer
});
});*/
}
PD_LITHIUM_API Command::Ref DrawList::PreGenerateCmd() {
Command::Ref cmd = Command::New();
PD_LITHIUM_API Command::Ref DrawList::GetNewCmd() {
Command::Ref cmd = pPool.NewCmd();
cmd->Layer = pLayer;
cmd->Index = pDrawList.size();
cmd->Index = pPool.Size() - 1;
cmd->Tex = CurrentTex;
pClipCmd(cmd.get());
pClipCmd(cmd);
return cmd;
}
PD_LITHIUM_API void DrawList::pClipCmd(Command *cmd) {
PD_LITHIUM_API void DrawList::pClipCmd(Command::Ref cmd) {
if (!pClipRects.empty()) {
cmd->ScissorOn = true;
cmd->ScissorRect = ivec4(pClipRects.top());
@@ -260,7 +266,7 @@ PD_LITHIUM_API void DrawList::DrawPolyLine(const std::vector<fvec2> &points,
return;
}
DrawSolid();
auto cmd = PreGenerateCmd();
auto cmd = GetNewCmd();
bool close = (flags & (1 << 0));
int num_points = close ? (int)points.size() : (int)points.size() - 1;
if (flags & (1 << 1)) {
@@ -270,10 +276,9 @@ PD_LITHIUM_API void DrawList::DrawPolyLine(const std::vector<fvec2> &points,
for (int i = 0; i < num_points; i++) {
int j = (i + 1) == (int)points.size() ? 0 : (i + 1);
auto line = Renderer::PrimLine(points[i], points[j], thickness);
Renderer::CmdQuad(cmd.get(), line, vec4(0.f, 1.f, 1.f, 0.f), clr);
Renderer::CmdQuad(cmd, line, vec4(0.f, 1.f, 1.f, 0.f), clr);
}
}
AddCommand(std::move(cmd));
}
PD_LITHIUM_API void DrawList::DrawConvexPolyFilled(
@@ -281,9 +286,8 @@ PD_LITHIUM_API void DrawList::DrawConvexPolyFilled(
if (points.size() < 3) {
return; // Need at least three points
}
auto cmd = PreGenerateCmd();
Renderer::CmdConvexPolyFilled(cmd.get(), points, clr, CurrentTex);
AddCommand(std::move(cmd));
auto cmd = GetNewCmd();
Renderer::CmdConvexPolyFilled(cmd, points, clr, CurrentTex);
}
PD_LITHIUM_API void DrawList::DrawText(const fvec2 &pos,
@@ -291,13 +295,7 @@ PD_LITHIUM_API void DrawList::DrawText(const fvec2 &pos,
if (!pCurrentFont) {
return;
}
std::vector<Command::Ref> cmds;
pCurrentFont->CmdTextEx(cmds, pos, color, pFontScale, text);
for (size_t i = 0; i < cmds.size(); i++) {
cmds[i]->Index = pDrawList.size() + i;
cmds[i]->Layer = pLayer;
AddCommand(std::move(cmds[i]));
}
pCurrentFont->CmdTextEx(pPool, pos, color, pFontScale, text);
}
PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2 &p,
@@ -306,13 +304,7 @@ PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2 &p,
if (!pCurrentFont) {
return;
}
std::vector<Command::Ref> cmds;
pCurrentFont->CmdTextEx(cmds, p, color, pFontScale, text, flags, box);
for (size_t i = 0; i < cmds.size(); i++) {
cmds[i]->Index = pDrawList.size() + i;
cmds[i]->Layer = pLayer;
AddCommand(std::move(cmds[i]));
}
pCurrentFont->CmdTextEx(pPool, p, color, pFontScale, text, flags, box);
}
PD_LITHIUM_API void DrawList::DrawLine(const fvec2 &a, const fvec2 &b,

View File

@@ -248,10 +248,9 @@ PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string &text, float scale) {
return res;
}
PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
const fvec2 &pos, u32 color, float scale,
const std::string &text, LiTextFlags flags,
const fvec2 &box) {
PD_LITHIUM_API void Font::CmdTextEx(CmdPool &cmds, const fvec2 &pos, u32 color,
float scale, const std::string &text,
LiTextFlags flags, const fvec2 &box) {
fvec2 off;
float cfs = (DefaultPixelHeight * scale) / (float)PixelHeight;
float lh = (float)PixelHeight * cfs;
@@ -294,7 +293,7 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
it = pShortText(it, scale, box - pos, tmp_dim);
}
auto wline = Strings::MakeWstring(it);
auto cmd = Command::New();
auto cmd = cmds.NewCmd();
auto Tex = GetCodepoint(wline[0]).Tex;
cmd->Tex = Tex;
for (auto &jt : wline) {
@@ -304,8 +303,7 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
continue;
}
if (Tex != cp.Tex) {
cmds.push_back(std::move(cmd));
cmd = Command::New();
cmd = cmds.NewCmd();
Tex = cp.Tex;
cmd->Tex = Tex;
}
@@ -318,19 +316,18 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
Rect rec = Renderer::PrimRect(
rpos + vec2(off.x + 1, off.y + (cp.Offset * cfs)) + 1,
cp.Size * cfs, 0.f);
Renderer::CmdQuad(cmd.get(), rec, cp.SimpleUV, 0xff111111);
Renderer::CmdQuad(cmd, rec, cp.SimpleUV, 0xff111111);
}
// Draw
Rect rec = Renderer::PrimRect(
rpos + off + fvec2(0, (cp.Offset * cfs)), cp.Size * cfs, 0.f);
Renderer::CmdQuad(cmd.get(), rec, cp.SimpleUV, color);
Renderer::CmdQuad(cmd, rec, cp.SimpleUV, color);
off.x += cp.Size.x * cfs + 2 * cfs;
} else {
off.x += 4 * cfs;
}
}
}
cmds.push_back(std::move(cmd));
off.y += lh;
off.x = 0;
}

View File

@@ -88,7 +88,7 @@ PD_LITHIUM_API Rect Renderer::PrimLine(const fvec2& a, const fvec2& b,
return Rect(a + off, b + off, a - off, b - off);
}
PD_LITHIUM_API void Renderer::CmdQuad(Command* cmd, const Rect& quad,
PD_LITHIUM_API void Renderer::CmdQuad(Command::Ref cmd, const Rect& quad,
const Rect& uv, u32 color) {
cmd->AddIdx(0).AddIdx(1).AddIdx(2);
cmd->AddIdx(0).AddIdx(2).AddIdx(3);
@@ -98,7 +98,7 @@ PD_LITHIUM_API void Renderer::CmdQuad(Command* cmd, const Rect& quad,
cmd->AddVtx(Vertex(quad.BotLeft(), uv.BotLeft(), color));
}
PD_LITHIUM_API void Renderer::CmdTriangle(Command* cmd, const fvec2 a,
PD_LITHIUM_API void Renderer::CmdTriangle(Command::Ref cmd, const fvec2 a,
const fvec2 b, const fvec2 c,
u32 clr) {
cmd->AddIdx(2).AddIdx(1).AddIdx(0);
@@ -111,7 +111,8 @@ PD_LITHIUM_API void Renderer::CmdTriangle(Command* cmd, const fvec2 a,
// would probably be faster to render out of screen than checking if
// it could be skipped)
PD_LITHIUM_API void Renderer::CmdConvexPolyFilled(
Command* cmd, const std::vector<fvec2>& points, u32 clr, Texture::Ref tex) {
Command::Ref cmd, const std::vector<fvec2>& points, u32 clr,
Texture::Ref tex) {
if (points.size() < 3 || tex == nullptr) {
return; // Need at least three points
}