# Work at gfx driver system
- Update pool to use template allocator directly instead of std::vector - Add a GfxDriver config Template to be able to modify settings like allocators / Types for specific Drivers - Add glad
This commit is contained in:
@@ -28,6 +28,7 @@ SOFTWARE.
|
||||
#include <exception>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <numbers>
|
||||
#include <pd/pd_p_api.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <pd/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
template <typename T, typename Alloc = std::allocator<T>>
|
||||
class Pool {
|
||||
public:
|
||||
using value_type = T;
|
||||
using iterator = T*;
|
||||
using const_iterator = const iterator*;
|
||||
|
||||
Pool() = default;
|
||||
~Pool() = default;
|
||||
~Pool() {
|
||||
if (pData) {
|
||||
pAlloc.deallocate(pData, pCap);
|
||||
pData = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static Pool* Create(size_t size) { return new Pool(size); }
|
||||
|
||||
void Init(size_t size) {
|
||||
pPos = 0;
|
||||
pCap = size;
|
||||
pPool.resize(size);
|
||||
pData = pAlloc.allocate(size);
|
||||
}
|
||||
|
||||
Pool(const Pool&) = delete;
|
||||
@@ -24,7 +33,7 @@ class Pool {
|
||||
|
||||
T* Allocate(size_t num = 1) {
|
||||
if (CheckLimits(num)) return nullptr;
|
||||
T* ret = &pPool[pPos];
|
||||
T* ret = &pData[pPos];
|
||||
pPos += num;
|
||||
return ret;
|
||||
}
|
||||
@@ -42,10 +51,19 @@ class Pool {
|
||||
|
||||
void Reset() { pPos = 0; }
|
||||
|
||||
size_t size() const { return pPos; }
|
||||
size_t capacity() const { return pCap; }
|
||||
T& at(size_t idx) { return pData[idx]; }
|
||||
const T& at(size_t idx) const { return pData[idx]; }
|
||||
|
||||
T& operator[](size_t idx) { return at(idx); }
|
||||
const T& operator[](size_t idx) const { return at(idx); }
|
||||
|
||||
private:
|
||||
Pool(size_t size) : pCap(size), pPos(0) { pPool.resize(size); }
|
||||
Pool(size_t size) : pCap(size), pPos(0) { pData = pAlloc.allocate(size); }
|
||||
size_t pCap = 0;
|
||||
size_t pPos = 0;
|
||||
std::vector<T, Alloc> pPool;
|
||||
Alloc pAlloc;
|
||||
T* pData = nullptr;
|
||||
};
|
||||
} // namespace PD
|
||||
@@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include <pd/drivers/os.hpp>
|
||||
#include <pd/drivers/os.hpp>
|
||||
#include <pd/drivers/gfx.hpp>
|
||||
@@ -22,25 +22,78 @@ enum class TextureFormat {
|
||||
A8,
|
||||
};
|
||||
|
||||
// Pre interface class
|
||||
class PD_API GfxDriver : public DriverInterface {
|
||||
public:
|
||||
GfxDriver(std::string_view name = "Default") : DriverInterface(name) {}
|
||||
virtual ~GfxDriver() {}
|
||||
GfxDriver(std::string_view name);
|
||||
virtual ~GfxDriver() = default;
|
||||
|
||||
virtual void Init() {}
|
||||
virtual void Deinit() {}
|
||||
virtual void Submit() {}
|
||||
|
||||
void SetViewPort(const ivec2& size);
|
||||
void SetViewPort(int x, int y);
|
||||
virtual void BindTexture(TextureID id) {}
|
||||
virtual void Reset() {}
|
||||
void Reset();
|
||||
virtual TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
|
||||
TextureFormat type = TextureFormat::RGBA32,
|
||||
TextureFilter filter = TextureFilter::Linear) {}
|
||||
TextureFilter filter = TextureFilter::Linear) {
|
||||
return 0;
|
||||
}
|
||||
virtual void DeleteTexture(const TextureID& tex) {}
|
||||
void Draw(const std::vector<Li::Command>& commands) {
|
||||
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 {
|
||||
CountCommands += commands.size();
|
||||
// Bind shader
|
||||
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
|
||||
// Shader Set Mtx
|
||||
size_t index = 0;
|
||||
while (index < commands.size()) {
|
||||
CurrentTex = commands[index].Tex;
|
||||
@@ -48,21 +101,36 @@ class PD_API GfxDriver : public DriverInterface {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
size_t CurrentIndex = 0;
|
||||
size_t CurrentVertex = 0;
|
||||
TextureID CurrentTex = 0;
|
||||
Mat4 Projection;
|
||||
ivec2 ViewPort;
|
||||
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();
|
||||
}
|
||||
|
||||
// Counters
|
||||
size_t CountDrawcalls = 0;
|
||||
size_t CountCommands = 0;
|
||||
size_t CountVertices = 0;
|
||||
size_t CountIndices = 0;
|
||||
private:
|
||||
VtxPool pVtxPool;
|
||||
IdxPool pIdxPool;
|
||||
};
|
||||
|
||||
class PD_API Gfx {
|
||||
|
||||
Reference in New Issue
Block a user