diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bf2f30..1554c42 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,10 +48,10 @@ set(PD_SOURCES source/drivers/hid.cpp # Lithium + source/lithium/command.cpp source/lithium/drawlist.cpp source/lithium/font.cpp source/lithium/math.cpp - source/lithium/pools.cpp # Ultra source/ultra/canvas.cpp diff --git a/backends/source/gfx_opengl2.cpp b/backends/source/gfx_opengl2.cpp index 9b60f6d..3c9b528 100644 --- a/backends/source/gfx_opengl2.cpp +++ b/backends/source/gfx_opengl2.cpp @@ -95,11 +95,11 @@ void GfxOpenGL2::Submit(size_t count, size_t start) { glUseProgram(pShader); glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data()); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex), + glBufferData(GL_ARRAY_BUFFER, GetVertexPoolSize() * sizeof(PD::Li::Vertex), GetVertexBufPtr(0), GL_DYNAMIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16), + glBufferData(GL_ELEMENT_ARRAY_BUFFER, GetIndexPoolSize() * sizeof(u16), GetIndexBufPtr(0), GL_DYNAMIC_DRAW); pSetupShaderAttribs(pShader); diff --git a/backends/source/gfx_opengl3.cpp b/backends/source/gfx_opengl3.cpp index 1ede14e..46ebc7c 100644 --- a/backends/source/gfx_opengl3.cpp +++ b/backends/source/gfx_opengl3.cpp @@ -67,11 +67,11 @@ void GfxOpenGL3::Submit(size_t count, size_t start) { glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data()); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex), + glBufferData(GL_ARRAY_BUFFER, GetVertexPoolSize() * sizeof(PD::Li::Vertex), GetVertexBufPtr(0), GL_DYNAMIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(u16), + glBufferData(GL_ELEMENT_ARRAY_BUFFER, GetIndexPoolSize() * sizeof(u16), GetIndexBufPtr(0), GL_DYNAMIC_DRAW); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, diff --git a/include/pd/drivers/gfx.hpp b/include/pd/drivers/gfx.hpp index 920862e..c7a2082 100755 --- a/include/pd/drivers/gfx.hpp +++ b/include/pd/drivers/gfx.hpp @@ -3,7 +3,6 @@ #include #include #include -#include #include using PDGfxBackendFlags = PD::u32; @@ -47,6 +46,17 @@ class PD_API GfxDriver : public DriverInterface { size_t GetNumDrawcalls() const { return CountDrawcalls; } size_t GetNumCommands() const { return CountCommands; } + // Global Pool Interface + virtual size_t AllocateVertices(size_t count, PD::ptr accessor) = 0; + virtual size_t AllocateIndices(size_t count, PD::ptr accessor) = 0; + virtual bool ExpandVertices(size_t count, PD::ptr accessor) = 0; + virtual bool ExpandIndices(size_t count, PD::ptr accessor) = 0; + virtual void PutVertex(size_t loc, const Li::Vertex& vtx, + PD::ptr accessor) = 0; + virtual void PutIndex(size_t loc, u16 idx, PD::ptr accessor) = 0; + virtual const Li::Vertex& GetVertex(size_t loc) const = 0; + virtual const u16& GetIndex(size_t loc) const = 0; + protected: virtual void SysDeinit() {} virtual void SysInit() {} @@ -105,41 +115,76 @@ class GfxDriverBase : public GfxDriver { if (!CurrentTex) { CurrentTex = pWhite.GetID(); } - size_t startidx = CurrentIndex; + size_t startidx = commands[index].FirstIndex; + size_t num_indices = 0; while (index < commands.size() && (CurrentTex == commands[index].Tex || (CurrentTex == pWhite.GetID() && commands[index].Tex == 0))) { - const auto& c = commands[index]; - auto pIdx = pIdxPool.Allocate(c.IndexCount); - auto pVtx = pVtxPool.Allocate(c.VertexCount); - for (size_t i = 0; i < c.IndexCount; i++) { - pIdx[i] = CurrentVertex + Li::GetIndex(c.FirstIndex + i); - } - CurrentIndex += c.IndexCount; - CurrentVertex += c.VertexCount; - for (size_t i = 0; i < c.VertexCount; i++) { - pVtx[i] = Li::GetVertex(c.FirstVertex + i); - } + num_indices += commands[index].IndexCount; index++; } - Submit(CurrentIndex - startidx, startidx); + Submit(num_indices, startidx); pCountDrawcalls++; } } + size_t AllocateVertices(size_t count, PD::ptr accessor) override { + pVertexAccessor = accessor; + size_t loc = pVtxPool.size(); + pVtxPool.Allocate(count); + return loc; + } + + size_t AllocateIndices(size_t count, PD::ptr accessor) override { + pIndexAccessor = accessor; + size_t loc = pIdxPool.size(); + pIdxPool.Allocate(count); + return loc; + } + + bool ExpandVertices(size_t count, PD::ptr accessor) override { + if (pVertexAccessor != accessor) return false; + pVtxPool.Allocate(count); + return true; + } + + bool ExpandIndices(size_t count, PD::ptr accessor) override { + if (pIndexAccessor != accessor) return false; + pIdxPool.Allocate(count); + return true; + } + + void PutVertex(size_t loc, const Li::Vertex& vtx, PD::ptr accessor) override { + if (pVertexAccessor != accessor) return; + pVtxPool.Put(loc, vtx); + } + + void PutIndex(size_t loc, u16 idx, PD::ptr accessor) override { + if (pIndexAccessor != accessor) return; + pIdxPool.Put(loc, idx); + } + + const Li::Vertex& GetVertex(size_t loc) const override { + return pVtxPool[loc]; + } + + const u16& GetIndex(size_t loc) const override { return pIdxPool[loc]; } + protected: u16* GetIndexBufPtr(size_t start) { return &pIdxPool[start]; } Li::Vertex* GetVertexBufPtr(size_t start) { return &pVtxPool[start]; } size_t GetVertexPoolSize() const { return pVtxPool.size(); } size_t GetIndexPoolSize() const { return pIdxPool.size(); } void ResetPools() override { - pVtxPool.ResetFast(); - pIdxPool.ResetFast(); + pVtxPool.NoReset(); + pIdxPool.NoReset(); } private: VtxPool pVtxPool; IdxPool pIdxPool; + PD::ptr pVertexAccessor = 0; + PD::ptr pIndexAccessor = 0; }; class PD_API Gfx { @@ -183,6 +228,37 @@ class PD_API Gfx { static size_t GetNumDrawcalls() { return driver->GetNumDrawcalls(); } static size_t GetNumCommands() { return driver->GetNumCommands(); } + // Gloabal Pool Interface + static size_t AllocateVertices(size_t count, PD::ptr accessor) { + return driver->AllocateVertices(count, accessor); + } + + static size_t AllocateIndices(size_t count, PD::ptr accessor) { + return driver->AllocateIndices(count, accessor); + } + + static bool ExpandVertices(size_t count, PD::ptr accessor) { + return driver->ExpandVertices(count, accessor); + } + + static bool ExpandIndices(size_t count, PD::ptr accessor) { + return driver->ExpandIndices(count, accessor); + } + + static void PutVertex(size_t loc, const Li::Vertex& vtx, PD::ptr accessor) { + driver->PutVertex(loc, vtx, accessor); + } + + static void PutIndex(size_t loc, u16 idx, PD::ptr accessor) { + driver->PutIndex(loc, idx, accessor); + } + + static const Li::Vertex& GetVertex(size_t loc) { + return driver->GetVertex(loc); + } + + static const u16& GetIndex(size_t loc) { return driver->GetIndex(loc); } + private: static std::unique_ptr driver; }; diff --git a/include/pd/lithium/command.hpp b/include/pd/lithium/command.hpp index 06936ad..5a731b5 100644 --- a/include/pd/lithium/command.hpp +++ b/include/pd/lithium/command.hpp @@ -2,7 +2,6 @@ #include #include -#include #include namespace PD { @@ -12,54 +11,12 @@ class Command { Command() { Reset(); } ~Command() {} - void Reserve(size_t vtx, size_t idx) { - if (!FirstVertex) { - FirstVertex = AllocateVertices(vtx, (PD::ptr)this); - VertexCountMax = vtx; - } else { - ExpandVertices(vtx, (PD::ptr)this); - VertexCountMax += vtx; - } - if (!FirstIndex) { - FirstIndex = AllocateIndices(idx, (PD::ptr)this); - IndexCountMax = idx; - } else { - ExpandIndices(idx, (PD::ptr)this); - IndexCountMax += idx; - } - } + void Reserve(size_t vtx, size_t idx); + void Reset(); - void Reset() { - Layer = 0; - Tex = 0; - FirstIndex = 0; - FirstVertex = 0; - IndexCount = 0; - VertexCount = 0; - VertexCountMax = 0; - IndexCountMax = 0; - } - - Command& Add(const Vertex& vtx) { - if (VertexCount <= VertexCountMax) - PutVertex(FirstVertex + VertexCount++, vtx, (PD::ptr)this); - return *this; - } - Command& Add(u16 idx) { - if (IndexCount <= IndexCountMax) - PutIndex(FirstIndex + IndexCount++, VertexCount + idx, (PD::ptr)this); - return *this; - } - Command& Add(u16 a, u16 b, u16 c) { - if (IndexCount + 3 <= IndexCountMax) { - size_t idx = FirstIndex + IndexCount; - PutIndex(idx + 0, VertexCount + a, (PD::ptr)this); - PutIndex(idx + 1, VertexCount + b, (PD::ptr)this); - PutIndex(idx + 2, VertexCount + c, (PD::ptr)this); - IndexCount += 3; - } - return *this; - } + Command& Add(const Vertex& vtx); + Command& Add(u16 idx); + Command& Add(u16 a, u16 b, u16 c); int Layer = 0; ptr Tex = 0; diff --git a/include/pd/lithium/lithium.hpp b/include/pd/lithium/lithium.hpp index 9cde277..e1165ce 100644 --- a/include/pd/lithium/lithium.hpp +++ b/include/pd/lithium/lithium.hpp @@ -4,5 +4,4 @@ #include #include #include -#include #include diff --git a/include/pd/lithium/pools.hpp b/include/pd/lithium/pools.hpp deleted file mode 100644 index fbdce30..0000000 --- a/include/pd/lithium/pools.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include -#include - -namespace PD { -namespace Li { -/** - * Allocate an amount of vertices - * returns the index of the first vertex - */ -PD_API size_t AllocateVertices(size_t count, PD::ptr accessor); -/** - * Allocate an amount of indices - * returns the index of the first elem - */ -PD_API size_t AllocateIndices(size_t count, PD::ptr accessor); -PD_API bool ExpandVertices(size_t count, PD::ptr accessor); -PD_API bool ExpandIndices(size_t count, PD::ptr accessor); -PD_API void PutVertex(size_t loc, const Vertex& vtx, PD::ptr accessor); -PD_API void PutIndex(size_t loc, u16 idx, PD::ptr accessor); -PD_API const Vertex& GetVertex(size_t loc); -PD_API const u16& GetIndex(size_t loc); -PD_API void ResetPools(); -} // namespace Li -} // namespace PD \ No newline at end of file diff --git a/source/lithium/command.cpp b/source/lithium/command.cpp new file mode 100644 index 0000000..64c71a6 --- /dev/null +++ b/source/lithium/command.cpp @@ -0,0 +1,58 @@ +#include +#include + +namespace PD { +namespace Li { + +void Command::Reserve(size_t vtx, size_t idx) { + if (VertexCountMax == 0) { + FirstVertex = Gfx::AllocateVertices(vtx, (PD::ptr)this); + VertexCountMax = vtx; + } else { + Gfx::ExpandVertices(vtx, (PD::ptr)this); + VertexCountMax += vtx; + } + if (IndexCountMax == 0) { + FirstIndex = Gfx::AllocateIndices(idx, (PD::ptr)this); + IndexCountMax = idx; + } else { + Gfx::ExpandIndices(idx, (PD::ptr)this); + IndexCountMax += idx; + } +} + +void Command::Reset() { + Layer = 0; + Tex = 0; + FirstIndex = 0; + FirstVertex = 0; + IndexCount = 0; + VertexCount = 0; + VertexCountMax = 0; + IndexCountMax = 0; +} + +Command& Command::Add(const Vertex& vtx) { + if (VertexCount <= VertexCountMax) + Gfx::PutVertex(FirstVertex + VertexCount++, vtx, (PD::ptr)this); + return *this; +} +Command& Command::Add(u16 idx) { + if (IndexCount <= IndexCountMax) + Gfx::PutIndex(FirstIndex + IndexCount++, FirstVertex + VertexCount + idx, + (PD::ptr)this); + return *this; +} +Command& Command::Add(u16 a, u16 b, u16 c) { + if (IndexCount + 3 <= IndexCountMax) { + size_t idx = FirstIndex + IndexCount; + Gfx::PutIndex(idx + 0, FirstVertex + VertexCount + a, (PD::ptr)this); + Gfx::PutIndex(idx + 1, FirstVertex + VertexCount + b, (PD::ptr)this); + Gfx::PutIndex(idx + 2, FirstVertex + VertexCount + c, (PD::ptr)this); + IndexCount += 3; + } + return *this; +} + +} // namespace Li +} // namespace PD \ No newline at end of file diff --git a/source/lithium/pools.cpp b/source/lithium/pools.cpp deleted file mode 100644 index 62acd7a..0000000 --- a/source/lithium/pools.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include - -namespace PD { -namespace Li { -PD::Pool pVtxPool; -PD::Pool pIdxPool; -PD::ptr pVertexAccessor = 0; -PD::ptr pIndexAccessor = 0; - -PD_API size_t AllocateVertices(size_t count, PD::ptr accessor) { - pVertexAccessor = accessor; - int loc = pVtxPool.size(); - pVtxPool.Allocate(count); - return loc; -} - -PD_API size_t AllocateIndices(size_t count, PD::ptr accessor) { - pIndexAccessor = accessor; - int loc = pIdxPool.size(); - pIdxPool.Allocate(count); - return loc; -} - -PD_API bool ExpandVertices(size_t count, PD::ptr accessor) { - if (pVertexAccessor != accessor) return false; - pVtxPool.Allocate(count); - return true; -} - -PD_API bool ExpandIndices(size_t count, PD::ptr accessor) { - if (pIndexAccessor != accessor) return false; - pIdxPool.Allocate(count); - return true; -} - -PD_API void PutVertex(size_t loc, const Vertex& vtx, PD::ptr accessor) { - if (pVertexAccessor != accessor) return; - pVtxPool.Put(loc, vtx); -} - -PD_API void PutIndex(size_t loc, u16 idx, PD::ptr accessor) { - if (pIndexAccessor != accessor) return; - pIdxPool.Put(loc, idx); -} - -PD_API const Vertex& GetVertex(size_t loc) { - if (loc < pVtxPool.size()) return pVtxPool[loc]; - static Vertex pErrVtx; - return pErrVtx; -} - -PD_API const u16& GetIndex(size_t loc) { - if (loc < pIdxPool.size()) return pIdxPool[loc]; - static u16 pErrIdx = 0; - return pErrIdx; -} - -PD_API void ResetPools() { - pVtxPool.NoReset(); - pIdxPool.NoReset(); -} -} // namespace Li -} // namespace PD \ No newline at end of file diff --git a/tests/gfx/source/main.cpp b/tests/gfx/source/main.cpp index 2a04381..980f9ab 100644 --- a/tests/gfx/source/main.cpp +++ b/tests/gfx/source/main.cpp @@ -199,8 +199,8 @@ int main(int argc, char** argv) { RightStick.pColor = "#00ffff"; while (pOs->Mainloop()) { PD::Hid::Update(); + PD::Gfx::Reset(); // needs to be on top now pOs->ClearViewPort(); - PD::Li::ResetPools(); // Move to other place (or refactor this) app.Update(pOs->GetViewport(), pList); pList.SetFontscale(0.7); if (PD::Hid::IsEvent(PD::Hid::Event::Down, PD::Hid::Gamepad::CPLeft | @@ -263,7 +263,6 @@ int main(int argc, char** argv) { pList.PathAdd(PD::fvec2(1000, 360)); pList.PathStroke("#ff00ff", 10, LiDrawFlags_AA); - PD::Gfx::Reset(); PD::Gfx::Draw(pList); pList.Clear(); pOs->SwapBuffers();