2025-06-22 21:05:09 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2026-03-16 06:37:51 +01:00
|
|
|
#include <pd/core/mat.hpp>
|
|
|
|
|
#include <pd/drivers/interface.hpp>
|
2025-06-22 21:05:09 +02:00
|
|
|
#include <pd/lithium/command.hpp>
|
|
|
|
|
|
|
|
|
|
using LiBackendFlags = PD::u32;
|
|
|
|
|
enum LiBackendFlags_ {
|
|
|
|
|
LiBackendFlags_None = 0,
|
2026-03-16 06:37:51 +01:00
|
|
|
LiBackendFlags_FlipUV_Y = 1 << 0, // Essential for font loading
|
2025-06-22 21:05:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace PD {
|
2026-03-16 06:37:51 +01:00
|
|
|
using TextureID = ptr;
|
|
|
|
|
enum class TextureFilter {
|
|
|
|
|
Linear,
|
|
|
|
|
Nearest,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class TextureFormat {
|
|
|
|
|
RGBA32,
|
|
|
|
|
A8,
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-16 15:19:12 +01:00
|
|
|
// Pre interface class
|
2026-03-16 06:37:51 +01:00
|
|
|
class PD_API GfxDriver : public DriverInterface {
|
2026-01-25 00:04:40 +01:00
|
|
|
public:
|
2026-03-16 15:19:12 +01:00
|
|
|
GfxDriver(std::string_view name);
|
|
|
|
|
virtual ~GfxDriver() = default;
|
2026-01-25 00:04:40 +01:00
|
|
|
|
|
|
|
|
virtual void Init() {}
|
2026-03-16 15:19:12 +01:00
|
|
|
|
|
|
|
|
void SetViewPort(const ivec2& size);
|
|
|
|
|
void SetViewPort(int x, int y);
|
2026-03-16 06:37:51 +01:00
|
|
|
virtual void BindTexture(TextureID id) {}
|
2026-03-16 15:19:12 +01:00
|
|
|
void Reset();
|
2026-03-16 06:37:51 +01:00
|
|
|
virtual TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
|
|
|
|
|
TextureFormat type = TextureFormat::RGBA32,
|
2026-03-16 15:19:12 +01:00
|
|
|
TextureFilter filter = TextureFilter::Linear) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2026-03-16 06:37:51 +01:00
|
|
|
virtual void DeleteTexture(const TextureID& tex) {}
|
2026-03-16 15:19:12 +01:00
|
|
|
virtual void Draw(const Pool<Li::Command>& commands) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual void SysDeinit() {}
|
|
|
|
|
virtual void SysInit() {}
|
|
|
|
|
virtual void SysReset() {}
|
|
|
|
|
virtual void Submit(size_t count, size_t start) {}
|
|
|
|
|
virtual void ResetPools() = 0;
|
|
|
|
|
|
|
|
|
|
// Counters
|
|
|
|
|
size_t CountDrawcalls = 0;
|
|
|
|
|
size_t CountCommands = 0;
|
|
|
|
|
size_t CountVertices = 0;
|
|
|
|
|
size_t CountIndices = 0;
|
|
|
|
|
size_t CurrentIndex = 0;
|
|
|
|
|
size_t CurrentVertex = 0;
|
|
|
|
|
TextureID CurrentTex = 0;
|
|
|
|
|
Mat4 Projection;
|
|
|
|
|
ivec2 ViewPort;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct DefaultGfxConfig {
|
|
|
|
|
// Vertex Allocator
|
|
|
|
|
template <typename T>
|
|
|
|
|
using VertexAlloc = std::allocator<T>;
|
|
|
|
|
// Index Allocator
|
|
|
|
|
template <typename T>
|
|
|
|
|
using IndexAlloc = std::allocator<T>;
|
|
|
|
|
using IndexType = u16; // Index Type
|
|
|
|
|
|
|
|
|
|
static constexpr size_t NumVertices = 32768; // 8192*4
|
|
|
|
|
static constexpr size_t NumIndices = 49152; // 8192*6
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Config = DefaultGfxConfig>
|
|
|
|
|
class PD_API GfxDriverBase : public GfxDriver {
|
|
|
|
|
public:
|
|
|
|
|
using IndexType = Config::IndexType;
|
|
|
|
|
using VtxPool =
|
|
|
|
|
Pool<Li::Vertex, typename Config::template VertexAlloc<Li::Vertex>>;
|
|
|
|
|
using IdxPool =
|
|
|
|
|
Pool<IndexType, typename Config::template VertexAlloc<IndexType>>;
|
|
|
|
|
GfxDriverBase(std::string_view name = "Default") : GfxDriver(name) {}
|
|
|
|
|
virtual ~GfxDriverBase() {}
|
|
|
|
|
|
|
|
|
|
void Init() override {
|
|
|
|
|
pVtxPool.Init(Config::NumVertices);
|
|
|
|
|
pIdxPool.Init(Config::NumIndices);
|
|
|
|
|
SysInit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Draw(const Pool<Li::Command>& commands) override {
|
2026-03-16 06:37:51 +01:00
|
|
|
CountCommands += commands.size();
|
|
|
|
|
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
while (index < commands.size()) {
|
|
|
|
|
CurrentTex = commands[index].Tex;
|
|
|
|
|
if (!CurrentTex) {
|
|
|
|
|
index++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-03-16 15:19:12 +01:00
|
|
|
size_t startidx = CurrentIndex;
|
|
|
|
|
while (index < commands.size() && CurrentTex == commands[index].Tex) {
|
|
|
|
|
const auto& c = commands[index];
|
|
|
|
|
CountVertices += c.VertexCount;
|
|
|
|
|
CountIndices += c.IndexCount;
|
|
|
|
|
auto pIdx = pIdxPool.Allocate(c.IndexCount);
|
|
|
|
|
auto pVtx = pVtxPool.Allocate(c.VertexCount);
|
|
|
|
|
for (size_t i = 0; i < c.IndexCount; i++) {
|
|
|
|
|
pIdx[i] = CurrentVertex + c.FirstIndex[i];
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < c.VertexCount; i++) {
|
|
|
|
|
pVtx[i] = c.FirstVertex[i];
|
|
|
|
|
}
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
Submit(index - startidx, startidx);
|
2026-03-16 06:37:51 +01:00
|
|
|
}
|
2026-01-25 00:04:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:19:12 +01:00
|
|
|
protected:
|
|
|
|
|
IndexType* GetIndexBufPtr(size_t start) { return &pIdxPool[start]; }
|
|
|
|
|
Li::Vertex* GetVertexBufPtr(size_t start) { return &pVtxPool[start]; }
|
|
|
|
|
void ResetPools() override {
|
|
|
|
|
pVtxPool.Reset();
|
|
|
|
|
pIdxPool.Reset();
|
|
|
|
|
}
|
2025-06-22 21:05:09 +02:00
|
|
|
|
2026-03-16 15:19:12 +01:00
|
|
|
private:
|
|
|
|
|
VtxPool pVtxPool;
|
|
|
|
|
IdxPool pIdxPool;
|
2026-03-16 06:37:51 +01:00
|
|
|
};
|
2025-06-22 21:05:09 +02:00
|
|
|
|
2026-03-16 06:37:51 +01:00
|
|
|
class PD_API Gfx {
|
|
|
|
|
public:
|
|
|
|
|
Gfx() = default;
|
|
|
|
|
~Gfx() = default;
|
2025-07-23 23:21:34 +02:00
|
|
|
|
2026-03-16 06:37:51 +01:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
|
static void UseDriver(Args&&... args) {
|
|
|
|
|
// assert(driver == nullptr && "OS Driver already set");
|
|
|
|
|
driver = std::make_unique<T>(std::forward<Args>(args)...);
|
2025-06-22 21:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-16 06:37:51 +01:00
|
|
|
private:
|
|
|
|
|
static std::unique_ptr<GfxDriver> driver;
|
2025-06-22 21:05:09 +02:00
|
|
|
};
|
|
|
|
|
} // namespace PD
|