More work to drivers

- Add gfx_test
- add texture loading to GfxOpenGL
- add full submit code
- add debug logging
- add construct and destroy functionality to Pool
- add command functionality
- add vertex and index pools to lithium (static and not threadsafe yet)
- Update GfxDriver Matrix with SetViewPort
- Add glfw (only dependency of gfx_test) maybe later required for input driver
This commit is contained in:
2026-03-16 17:33:46 +01:00
parent 4924d86bc0
commit fe9194b907
20 changed files with 371 additions and 67 deletions

View File

@@ -25,6 +25,7 @@ SOFTWARE.
#include <chrono>
#include <cmath>
#include <cstddef>
#include <exception>
#include <format>
#include <fstream>
@@ -42,4 +43,17 @@ using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
using ptr = uintptr_t;
} // namespace PD
void Log(const std::string& txt);
template <typename... Args>
void Log(std::format_string<Args...> fmt, Args&&... args) {
std::string msg = std::format(fmt, std::forward<Args>(args)...);
Log(msg);
}
} // namespace PD
#ifdef PD_DEBUG
#define PDLOG(fmt, ...) \
PD::Log("[{}:{}]: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define PDLOG(fmt, ...)
#endif

View File

@@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <memory>
#include <pd/common.hpp>
namespace PD {
template <typename T, typename Alloc = std::allocator<T>>
@@ -24,6 +25,10 @@ class Pool {
pPos = 0;
pCap = size;
pData = pAlloc.allocate(size);
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::construct(pAlloc, &pData[i]);
}
PDLOG("Pool::Init({})", size);
}
Pool(const Pool&) = delete;
@@ -49,7 +54,15 @@ class Pool {
return false;
}
void Reset() { pPos = 0; }
void Reset() {
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::destroy(pAlloc, &pData[i]);
}
pPos = 0;
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::construct(pAlloc, &pData[i]);
}
}
size_t size() const { return pPos; }
size_t capacity() const { return pCap; }

View File

@@ -29,6 +29,7 @@ class PD_API GfxDriver : public DriverInterface {
virtual ~GfxDriver() = default;
virtual void Init() {}
virtual void Deinit() { SysDeinit(); }
void SetViewPort(const ivec2& size);
void SetViewPort(int x, int y);
@@ -93,7 +94,6 @@ class PD_API GfxDriverBase : public GfxDriver {
void Draw(const Pool<Li::Command>& commands) override {
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;
@@ -144,6 +144,22 @@ class PD_API Gfx {
driver = std::make_unique<T>(std::forward<Args>(args)...);
}
static void Init() { driver->Init(); }
static void Deinit() { driver->Deinit(); }
static void SetViewPort(const ivec2& vp) { driver->SetViewPort(vp); }
static void SetViewPort(int w, int h) { driver->SetViewPort(w, h); }
static void Reset() { driver->Reset(); }
static void Draw(const Pool<Li::Command>& commands) {
driver->Draw(commands);
}
static TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) {
return driver->LoadTexture(pixels, w, h, type, filter);
}
static const char* GetDriverName() { return driver->GetName(); }
private:
static std::unique_ptr<GfxDriver> driver;
};

View File

@@ -1,24 +1,61 @@
#pragma once
#include <cstddef>
#include <pd/core/pool.hpp>
#include <pd/lithium/pools.hpp>
#include <pd/lithium/vertex.hpp>
namespace PD {
namespace Li {
class Command {
public:
Command() {}
Command() { Reset(); }
~Command() {}
void Reserve(size_t vtx, size_t idx) {
if (!FirstVertex)
FirstVertex = AllocateVertices(vtx);
else
AllocateVertices(vtx);
if (!FirstIndex)
FirstIndex = AllocateIndices(idx);
else
AllocateIndices(idx);
}
void Reset() {
Layer = 0;
Tex = 0;
FirstIndex = nullptr;
FirstVertex = nullptr;
IndexCount = 0;
VertexCount = 0;
}
Command& Add(const Vertex& vtx) {
FirstVertex[VertexCount++] = vtx;
return *this;
}
Command& Add(u16 idx) {
FirstIndex[IndexCount++] = VertexCount + idx;
return *this;
}
Command& Add(u16 a, u16 b, u16 c) {
FirstIndex[IndexCount++] = VertexCount + a;
FirstIndex[IndexCount++] = VertexCount + b;
FirstIndex[IndexCount++] = VertexCount + c;
return *this;
}
int Layer = 0;
ptr Tex = 0;
Vertex* FirstVertex = nullptr;
u16* FirstIndex = nullptr;
size_t VertexCount;
size_t IndexCount;
PD::Pool<Vertex>* pVpool;
PD::Pool<u16>* pIpool;
size_t VertexCount = 0;
size_t IndexCount = 0;
// Todo: implement
size_t VertexCountMax = 0;
size_t IndexCountMax = 0;
};
} // namespace Li
} // namespace PD

View File

@@ -0,0 +1,5 @@
#pragma once
#include <pd/lithium/command.hpp>
#include <pd/lithium/pools.hpp>
#include <pd/lithium/vertex.hpp>

View File

@@ -0,0 +1,12 @@
#pragma once
#include <cstddef>
#include <pd/lithium/vertex.hpp>
namespace PD {
namespace Li {
void InitPools(size_t max_vertices = 32768);
Vertex* AllocateVertices(size_t count);
u16* AllocateIndices(size_t count);
} // namespace Li
} // namespace PD