move global pools into gfx driver

prevents double copy problems
saves memory
(needs to be optimized...)
This commit is contained in:
2026-09-02 08:04:50 +02:00
parent daaf473af7
commit 54584728c3
10 changed files with 161 additions and 162 deletions
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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);
+2 -2
View File
@@ -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,
+92 -16
View File
@@ -3,7 +3,6 @@
#include <pd/core/mat.hpp>
#include <pd/drivers/interface.hpp>
#include <pd/lithium/command.hpp>
#include <pd/lithium/pools.hpp>
#include <pd/lithium/texture.hpp>
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<GfxDriver> driver;
};
+5 -48
View File
@@ -2,7 +2,6 @@
#include <cstddef>
#include <pd/core/pool.hpp>
#include <pd/lithium/pools.hpp>
#include <pd/lithium/vertex.hpp>
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;
-1
View File
@@ -4,5 +4,4 @@
#include <pd/lithium/drawlist.hpp>
#include <pd/lithium/font.hpp>
#include <pd/lithium/math.hpp>
#include <pd/lithium/pools.hpp>
#include <pd/lithium/vertex.hpp>
-26
View File
@@ -1,26 +0,0 @@
#pragma once
#include <cstddef>
#include <pd/lithium/vertex.hpp>
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
+58
View File
@@ -0,0 +1,58 @@
#include <pd/drivers/gfx.hpp>
#include <pd/lithium/command.hpp>
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
-64
View File
@@ -1,64 +0,0 @@
#include <pd/core/core.hpp>
#include <pd/lithium/pools.hpp>
namespace PD {
namespace Li {
PD::Pool<Vertex> pVtxPool;
PD::Pool<u16> 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
+1 -2
View File
@@ -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();