From b3d621a8471e6e2147530a22adebd60ea8db370d Mon Sep 17 00:00:00 2001 From: tobid7 Date: Fri, 19 Dec 2025 21:08:32 +0100 Subject: [PATCH] Add LayerOptimisation / QoL change --- include/pd/lithium/drawlist.hpp | 12 ++++++++++-- pd/core/source/color.cpp | 2 +- pd/lithium/source/drawlist.cpp | 26 +++++++++++++++++++++----- pd/ui7/source/container/button.cpp | 4 ++-- pd/ui7/source/container/dragdata.cpp | 4 ++-- pd/ui7/source/container/image.cpp | 4 ++-- pd/ui7/source/menu.cpp | 10 +++++----- 7 files changed, 43 insertions(+), 19 deletions(-) diff --git a/include/pd/lithium/drawlist.hpp b/include/pd/lithium/drawlist.hpp index ca33936..6367c7d 100755 --- a/include/pd/lithium/drawlist.hpp +++ b/include/pd/lithium/drawlist.hpp @@ -64,14 +64,22 @@ class PD_LITHIUM_API DrawList { /** * Append an input drawlist on top of this one * This Function will clear the Input list to make sure - * THat the moved memory blocks don't get used + * That the moved memory blocks don't get used * @param list DrawList to move into current */ void Merge(DrawList::Ref list); + /** + * Optimize a Drawlist to a more or less perfect order + * to reduce drawcall overhead... This function also uses + * the Layersystem to keep specific stuff in the correct order + */ + void Optimize(); Command::Ref PreGenerateCmd(); void AddCommand(Command::Ref v); void Clear(); + void Layer(int l) { this->pLayer = l; } + int Layer() { return this->pLayer; } void SetFont(Font::Ref font) { pCurrentFont = font; } void SetFontScale(float scale) { pFontScale = scale; } @@ -196,7 +204,7 @@ class PD_LITHIUM_API DrawList { /** Data Section */ std::stack pClipRects; - int Layer; + int pLayer; float pFontScale = 0.7f; Font::Ref pCurrentFont; Texture::Ref CurrentTex; diff --git a/pd/core/source/color.cpp b/pd/core/source/color.cpp index d7b28b9..c89ec20 100755 --- a/pd/core/source/color.cpp +++ b/pd/core/source/color.cpp @@ -32,7 +32,7 @@ PD_CORE_API std::string Color::Hex(bool rgba) const { s << std::hex << std::setw(2) << std::setfill('0') << (int)r; s << std::hex << std::setw(2) << std::setfill('0') << (int)g; s << std::hex << std::setw(2) << std::setfill('0') << (int)b; - if (rgba) { + if (rgba || a != 255) { // QoL change btw s << std::hex << std::setw(2) << std::setfill('0') << (int)a; } return s.str(); diff --git a/pd/lithium/source/drawlist.cpp b/pd/lithium/source/drawlist.cpp index 374b876..990ef90 100755 --- a/pd/lithium/source/drawlist.cpp +++ b/pd/lithium/source/drawlist.cpp @@ -59,9 +59,25 @@ PD_LITHIUM_API void DrawList::Merge(DrawList::Ref list) { list->Clear(); } +PD_LITHIUM_API void DrawList::Optimize() { +#ifndef NDEBUG + PD::TT::Scope s("Optimize"); +#endif + 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 + return a->Index < b->Index; + } + 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(); - cmd->Layer = Layer; + cmd->Layer = pLayer; cmd->Index = pDrawList.size(); cmd->Tex = CurrentTex; pClipCmd(cmd.get()); @@ -275,8 +291,8 @@ PD_LITHIUM_API void DrawList::DrawText(const fvec2 &pos, std::vector cmds; pCurrentFont->CmdTextEx(cmds, pos, color, pFontScale, text); for (size_t i = 0; i < cmds.size(); i++) { - cmds[i]->Index = pDrawList.size(); - cmds[i]->Layer = Layer; + cmds[i]->Index = pDrawList.size() + i; + cmds[i]->Layer = pLayer; AddCommand(std::move(cmds[i])); } } @@ -290,8 +306,8 @@ PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2 &p, std::vector cmds; pCurrentFont->CmdTextEx(cmds, p, color, pFontScale, text, flags, box); for (size_t i = 0; i < cmds.size(); i++) { - cmds[i]->Index = pDrawList.size(); - cmds[i]->Layer = Layer; + cmds[i]->Index = pDrawList.size() + i; + cmds[i]->Layer = pLayer; AddCommand(std::move(cmds[i])); } } diff --git a/pd/ui7/source/container/button.cpp b/pd/ui7/source/container/button.cpp index c10964f..1896b5b 100755 --- a/pd/ui7/source/container/button.cpp +++ b/pd/ui7/source/container/button.cpp @@ -50,10 +50,10 @@ PD_UI7_API void Button::Draw() { // Assert(io.get() && list.get(), "Did you run Container::Init correctly?"); // io->Ren->OnScreen(screen); list->DrawRectFilled(FinalPos(), size, io->Theme->Get(color)); - list->Layer++; + list->pLayer++; list->DrawText(FinalPos() + size * 0.5 - tdim * 0.5, label, io->Theme->Get(UI7Color_Text)); - list->Layer--; + list->pLayer--; } PD_UI7_API void Button::Update() { diff --git a/pd/ui7/source/container/dragdata.cpp b/pd/ui7/source/container/dragdata.cpp index 8fa7b28..9fd67f2 100755 --- a/pd/ui7/source/container/dragdata.cpp +++ b/pd/ui7/source/container/dragdata.cpp @@ -83,11 +83,11 @@ PD_UI7_API void DragData::Draw() { vec2 td = io->Font->GetTextBounds(p, io->FontScale); list->DrawRectFilled(FinalPos() + fvec2(off_x, 0), td + io->FramePadding, io->Theme->Get(UI7Color_Button)); - list->Layer++; + list->pLayer++; list->DrawTextEx(FinalPos() + fvec2(off_x, 0), p, io->Theme->Get(UI7Color_Text), LiTextFlags_AlignMid, td + io->FramePadding); - list->Layer--; + list->pLayer--; off_x += td.x + io->ItemSpace.x + io->FramePadding.x; } list->DrawText(FinalPos() + fvec2(off_x, io->FramePadding.y * 0.5), label, diff --git a/pd/ui7/source/container/image.cpp b/pd/ui7/source/container/image.cpp index 01a7daa..c4dd674 100755 --- a/pd/ui7/source/container/image.cpp +++ b/pd/ui7/source/container/image.cpp @@ -29,11 +29,11 @@ PD_UI7_API void Image::Draw() { // Assert(io.get() && list.get(), "Did you run Container::Init correctly?"); // Assert(img.get(), "Image is nullptr!"); // io->Ren->OnScreen(screen); - list->Layer++; + list->pLayer++; list->DrawTexture(img); list->DrawRectFilled(FinalPos(), newsize, 0xffffffff); list->DrawSolid(); - list->Layer--; + list->pLayer--; } } // namespace UI7 } // namespace PD \ No newline at end of file diff --git a/pd/ui7/source/menu.cpp b/pd/ui7/source/menu.cpp index b88b327..8133966 100755 --- a/pd/ui7/source/menu.cpp +++ b/pd/ui7/source/menu.cpp @@ -239,7 +239,7 @@ PD_UI7_API void Menu::DrawBaseLayout() { if (!(Flags & UI7MenuFlags_NoResize)) { Container::Ref r = DynObj::New( [](IO::Ref io, Li::DrawList::Ref l, UI7::Container* self) { - l->Layer = 1; + l->pLayer = 1; l->PathAdd(self->FinalPos() + self->GetSize() - fvec2(0, 20)); l->PathAdd(self->FinalPos() + self->GetSize()); l->PathAdd(self->FinalPos() + self->GetSize() - fvec2(20, 0)); @@ -255,7 +255,7 @@ PD_UI7_API void Menu::DrawBaseLayout() { /** Background */ Container::Ref r = DynObj::New([](IO::Ref io, Li::DrawList::Ref l, UI7::Container* self) { - l->Layer = 0; + l->pLayer = 0; l->PathRectEx(self->FinalPos(), self->FinalPos() + self->GetSize(), 10.f, LiPathRectFlags_KeepTop | LiPathRectFlags_KeepBot); l->PathFill(io->Theme->Get(UI7Color_Background)); @@ -272,11 +272,11 @@ PD_UI7_API void Menu::DrawBaseLayout() { if (!(Flags & UI7MenuFlags_NoTitlebar)) { Container::Ref r = DynObj::New( [=, this](UI7::IO::Ref io, Li::DrawList::Ref l, UI7::Container* self) { - l->Layer = 20; + l->pLayer = 20; /** Header Bar */ l->DrawRectFilled(self->FinalPos(), self->GetSize(), io->Theme->Get(UI7Color_Header)); - l->Layer = 21; + l->pLayer = 21; /** Inline if statement to shift the Text if collapse sym is shown */ /** What the hell is this code btw (didn't found a better way) */ l->DrawText(self->FinalPos() + fvec2(Flags & UI7MenuFlags_NoClose @@ -297,7 +297,7 @@ PD_UI7_API void Menu::DrawBaseLayout() { r = DynObj::New([=, this](UI7::IO::Ref io, Li::DrawList::Ref l, UI7::Container* self) { /** This sym actually requires layer 21 (i dont know why) */ - l->Layer = 21; + l->pLayer = 21; /** * Symbol (Position Swapping set by pIsOpen ? openpos : closepos;) */