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:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
|||||||
[submodule "vendor/stb"]
|
[submodule "vendor/stb"]
|
||||||
path = vendor/stb
|
path = vendor/stb
|
||||||
url = https://github.com/nothings/stb.git
|
url = https://github.com/nothings/stb.git
|
||||||
|
[submodule "vendor/glfw"]
|
||||||
|
path = vendor/glfw
|
||||||
|
url = https://github.com/glfw/glfw.git
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ add_subdirectory(vendor)
|
|||||||
|
|
||||||
# # Include Library Source
|
# # Include Library Source
|
||||||
set(PD_SOURCES
|
set(PD_SOURCES
|
||||||
|
# Common
|
||||||
|
source/common.cpp
|
||||||
|
|
||||||
# Core
|
# Core
|
||||||
source/core/bits.cpp
|
source/core/bits.cpp
|
||||||
@@ -28,8 +30,12 @@ set(PD_SOURCES
|
|||||||
source/core/mat.cpp
|
source/core/mat.cpp
|
||||||
source/core/strings.cpp
|
source/core/strings.cpp
|
||||||
|
|
||||||
|
# Drivers
|
||||||
source/drivers/os.cpp
|
source/drivers/os.cpp
|
||||||
source/drivers/gfx.cpp
|
source/drivers/gfx.cpp
|
||||||
|
|
||||||
|
# Lithium
|
||||||
|
source/lithium/pools.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(${PD_BUILD_SHARED})
|
if(${PD_BUILD_SHARED})
|
||||||
@@ -40,6 +46,13 @@ else()
|
|||||||
target_compile_definitions(palladium PUBLIC -DPD_BUILD_STATIC)
|
target_compile_definitions(palladium PUBLIC -DPD_BUILD_STATIC)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
target_compile_definitions(palladium PUBLIC -DPD_DEBUG=1)
|
||||||
|
target_compile_options(palladium
|
||||||
|
PUBLIC
|
||||||
|
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
|
||||||
|
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include
|
||||||
|
)
|
||||||
|
|
||||||
add_library(palladium::palladium ALIAS palladium)
|
add_library(palladium::palladium ALIAS palladium)
|
||||||
|
|
||||||
target_include_directories(palladium
|
target_include_directories(palladium
|
||||||
@@ -48,18 +61,21 @@ target_include_directories(palladium
|
|||||||
$<INSTALL_INTERFACE:include>
|
$<INSTALL_INTERFACE:include>
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_options(palladium PRIVATE
|
target_compile_options(palladium
|
||||||
|
PRIVATE
|
||||||
$<$<CONFIG:Debug>:-O0 -g>
|
$<$<CONFIG:Debug>:-O0 -g>
|
||||||
$<$<CONFIG:Release>:-O3>
|
$<$<CONFIG:Release>:-O3>
|
||||||
)
|
)
|
||||||
|
|
||||||
install(TARGETS palladium
|
install(
|
||||||
|
TARGETS palladium
|
||||||
EXPORT palladiumTargets
|
EXPORT palladiumTargets
|
||||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||||
)
|
)
|
||||||
install(DIRECTORY include/
|
install(
|
||||||
|
DIRECTORY include/
|
||||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||||
)
|
)
|
||||||
install(EXPORT palladiumTargets
|
install(EXPORT palladiumTargets
|
||||||
@@ -72,16 +88,20 @@ include(CMakePackageConfigHelpers)
|
|||||||
configure_package_config_file(
|
configure_package_config_file(
|
||||||
cmake/palladiumConfig.cmake.in
|
cmake/palladiumConfig.cmake.in
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
||||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
INSTALL_DESTINATION
|
||||||
|
${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||||
)
|
)
|
||||||
|
|
||||||
write_basic_package_version_file(
|
write_basic_package_version_file(
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
||||||
VERSION ${PROJECT_VERSION}
|
VERSION
|
||||||
COMPATIBILITY SameMajorVersion
|
${PROJECT_VERSION}
|
||||||
|
COMPATIBILITY
|
||||||
|
SameMajorVersion
|
||||||
)
|
)
|
||||||
|
|
||||||
install(FILES
|
install(
|
||||||
|
FILES
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
||||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||||
@@ -89,7 +109,8 @@ install(FILES
|
|||||||
|
|
||||||
find_program(CLANG_FORMAT clang-format)
|
find_program(CLANG_FORMAT clang-format)
|
||||||
|
|
||||||
file(GLOB_RECURSE PD_FMTFILES CONFIGURE_DEPENDS
|
file(GLOB_RECURSE PD_FMTFILES
|
||||||
|
CONFIGURE_DEPENDS
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
|
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -9,11 +9,19 @@ option(PD_ENABLE_VULKAN "Not implemented yet" OFF)
|
|||||||
add_library(pd-system STATIC
|
add_library(pd-system STATIC
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/source/gfx_opengl.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/source/gfx_opengl.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(pd-system
|
target_include_directories(pd-system
|
||||||
PUBLIC
|
PUBLIC
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||||
$<INSTALL_INTERFACE:include>
|
$<INSTALL_INTERFACE:include>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_compile_options(pd-system
|
||||||
|
PUBLIC
|
||||||
|
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
|
||||||
|
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(pd-system
|
target_link_libraries(pd-system
|
||||||
PUBLIC
|
PUBLIC
|
||||||
palladium::palladium
|
palladium::palladium
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ struct GfxOpenGLConfig {
|
|||||||
};
|
};
|
||||||
class GfxOpenGL : public GfxDriverBase<GfxOpenGLConfig> {
|
class GfxOpenGL : public GfxDriverBase<GfxOpenGLConfig> {
|
||||||
public:
|
public:
|
||||||
GfxOpenGL() {}
|
GfxOpenGL(): GfxDriverBase("OpenGL2") {}
|
||||||
~GfxOpenGL() {}
|
~GfxOpenGL() {}
|
||||||
|
|
||||||
void SysInit() override;
|
void SysInit() override;
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include <gfx_opengl.hpp>
|
#include <gfx_opengl.hpp>
|
||||||
|
|
||||||
|
#include "pd/common.hpp"
|
||||||
|
#include "pd/drivers/gfx.hpp"
|
||||||
|
|
||||||
namespace PD {
|
namespace PD {
|
||||||
GLuint compileShader(const std::string& source, GLenum type) {
|
GLuint compileShader(const std::string& source, GLenum type) {
|
||||||
GLuint shader = glCreateShader(type);
|
GLuint shader = glCreateShader(type);
|
||||||
@@ -42,6 +45,8 @@ GLuint createShaderProgram(const std::string& vertexShaderSource,
|
|||||||
glDeleteShader(vertexShader);
|
glDeleteShader(vertexShader);
|
||||||
glDeleteShader(fragmentShader);
|
glDeleteShader(fragmentShader);
|
||||||
|
|
||||||
|
if (success) PDLOG("Shader [{}] compiled sucessfully", shaderProgram);
|
||||||
|
|
||||||
return shaderProgram;
|
return shaderProgram;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,16 +123,36 @@ void GfxOpenGL::SysInit() {
|
|||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
PDLOG(
|
||||||
|
"GfxOpenGL::SysInit():\n pShader = {}\n pLocTex = {}\n pLocAlfa = "
|
||||||
|
"{}\n pLocProjection = {}\n VBO = {}\n IBO = {}",
|
||||||
|
pShader, pLocTex, pLocAlfa, pLocProjection, VBO, IBO);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxOpenGL::SysDeinit() {
|
void GfxOpenGL::SysDeinit() {
|
||||||
glDeleteBuffers(1, &VBO);
|
glDeleteBuffers(1, &VBO);
|
||||||
glDeleteBuffers(1, &IBO);
|
glDeleteBuffers(1, &IBO);
|
||||||
|
PDLOG("GfxOpenGL::SysDeinit()");
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxOpenGL::Submit(size_t count, size_t start) {
|
void GfxOpenGL::Submit(size_t count, size_t start) {
|
||||||
|
BindTexture(CurrentTex);
|
||||||
|
glUseProgram(pShader);
|
||||||
|
pSetupShaderAttribs(pShader);
|
||||||
|
glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data());
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
|
||||||
|
GetVertexBufPtr(0), GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
|
||||||
|
GetIndexBufPtr(0), GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT,
|
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT,
|
||||||
(void*)GetIndexBufPtr(start));
|
reinterpret_cast<void*>(start * sizeof(u16)));
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
BindTexture(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxOpenGL::BindTexture(TextureID id) {
|
void GfxOpenGL::BindTexture(TextureID id) {
|
||||||
@@ -139,12 +164,37 @@ void GfxOpenGL::BindTexture(TextureID id) {
|
|||||||
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
|
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxOpenGL::SysReset() {}
|
void GfxOpenGL::SysReset() {
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
TextureID GfxOpenGL::LoadTexture(const std::vector<PD::u8>& pixels, int w,
|
TextureID GfxOpenGL::LoadTexture(const std::vector<PD::u8>& pixels, int w,
|
||||||
int h, TextureFormat type,
|
int h, TextureFormat type,
|
||||||
TextureFilter filter) {
|
TextureFilter filter) {
|
||||||
return 0;
|
GLuint texID;
|
||||||
|
glGenTextures(1, &texID);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texID);
|
||||||
|
|
||||||
|
// Set base format (Always using RGBA as base)
|
||||||
|
GLenum fmt = GL_RGBA;
|
||||||
|
/*if (type == PD::Li::Texture::Type::RGB24) {
|
||||||
|
fmt = GL_RGB;
|
||||||
|
} else if (type == PD::Li::Texture::Type::A8) {
|
||||||
|
fmt = GL_ALPHA;
|
||||||
|
}*/
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
|
||||||
|
pixels.data());
|
||||||
|
if (filter == TextureFilter::Linear) {
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
} else if (filter == TextureFilter::Nearest) {
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
}
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
PDLOG("GfxOpenGL::LoadTexture -> [{}] {}", PD::ivec2(w, h), texID);
|
||||||
|
return texID;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxOpenGL::DeleteTexture(const TextureID& tex) {
|
void GfxOpenGL::DeleteTexture(const TextureID& tex) {
|
||||||
|
|||||||
@@ -25,3 +25,4 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include <pd/core/core.hpp>
|
#include <pd/core/core.hpp>
|
||||||
#include <pd/drivers/drivers.hpp>
|
#include <pd/drivers/drivers.hpp>
|
||||||
|
#include <pd/lithium/lithium.hpp>
|
||||||
@@ -25,6 +25,7 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstddef>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -42,4 +43,17 @@ using u16 = unsigned short;
|
|||||||
using u32 = unsigned int;
|
using u32 = unsigned int;
|
||||||
using u64 = unsigned long long;
|
using u64 = unsigned long long;
|
||||||
using ptr = uintptr_t;
|
using ptr = uintptr_t;
|
||||||
|
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
|
} // namespace PD
|
||||||
|
|
||||||
|
#ifdef PD_DEBUG
|
||||||
|
#define PDLOG(fmt, ...) \
|
||||||
|
PD::Log("[{}:{}]: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define PDLOG(fmt, ...)
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <pd/common.hpp>
|
#include <pd/common.hpp>
|
||||||
namespace PD {
|
namespace PD {
|
||||||
template <typename T, typename Alloc = std::allocator<T>>
|
template <typename T, typename Alloc = std::allocator<T>>
|
||||||
@@ -24,6 +25,10 @@ class Pool {
|
|||||||
pPos = 0;
|
pPos = 0;
|
||||||
pCap = size;
|
pCap = size;
|
||||||
pData = pAlloc.allocate(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;
|
Pool(const Pool&) = delete;
|
||||||
@@ -49,7 +54,15 @@ class Pool {
|
|||||||
return false;
|
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 size() const { return pPos; }
|
||||||
size_t capacity() const { return pCap; }
|
size_t capacity() const { return pCap; }
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class PD_API GfxDriver : public DriverInterface {
|
|||||||
virtual ~GfxDriver() = default;
|
virtual ~GfxDriver() = default;
|
||||||
|
|
||||||
virtual void Init() {}
|
virtual void Init() {}
|
||||||
|
virtual void Deinit() { SysDeinit(); }
|
||||||
|
|
||||||
void SetViewPort(const ivec2& size);
|
void SetViewPort(const ivec2& size);
|
||||||
void SetViewPort(int x, int y);
|
void SetViewPort(int x, int y);
|
||||||
@@ -93,7 +94,6 @@ class PD_API GfxDriverBase : public GfxDriver {
|
|||||||
|
|
||||||
void Draw(const Pool<Li::Command>& commands) override {
|
void Draw(const Pool<Li::Command>& commands) override {
|
||||||
CountCommands += commands.size();
|
CountCommands += commands.size();
|
||||||
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
|
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
while (index < commands.size()) {
|
while (index < commands.size()) {
|
||||||
CurrentTex = commands[index].Tex;
|
CurrentTex = commands[index].Tex;
|
||||||
@@ -144,6 +144,22 @@ class PD_API Gfx {
|
|||||||
driver = std::make_unique<T>(std::forward<Args>(args)...);
|
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:
|
private:
|
||||||
static std::unique_ptr<GfxDriver> driver;
|
static std::unique_ptr<GfxDriver> driver;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,24 +1,61 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
#include <pd/core/pool.hpp>
|
#include <pd/core/pool.hpp>
|
||||||
|
#include <pd/lithium/pools.hpp>
|
||||||
#include <pd/lithium/vertex.hpp>
|
#include <pd/lithium/vertex.hpp>
|
||||||
|
|
||||||
namespace PD {
|
namespace PD {
|
||||||
namespace Li {
|
namespace Li {
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
Command() {}
|
Command() { Reset(); }
|
||||||
~Command() {}
|
~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;
|
int Layer = 0;
|
||||||
ptr Tex = 0;
|
ptr Tex = 0;
|
||||||
Vertex* FirstVertex = nullptr;
|
Vertex* FirstVertex = nullptr;
|
||||||
u16* FirstIndex = nullptr;
|
u16* FirstIndex = nullptr;
|
||||||
size_t VertexCount;
|
size_t VertexCount = 0;
|
||||||
size_t IndexCount;
|
size_t IndexCount = 0;
|
||||||
|
// Todo: implement
|
||||||
PD::Pool<Vertex>* pVpool;
|
size_t VertexCountMax = 0;
|
||||||
PD::Pool<u16>* pIpool;
|
size_t IndexCountMax = 0;
|
||||||
};
|
};
|
||||||
} // namespace Li
|
} // namespace Li
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
5
include/pd/lithium/lithium.hpp
Normal file
5
include/pd/lithium/lithium.hpp
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <pd/lithium/command.hpp>
|
||||||
|
#include <pd/lithium/pools.hpp>
|
||||||
|
#include <pd/lithium/vertex.hpp>
|
||||||
12
include/pd/lithium/pools.hpp
Normal file
12
include/pd/lithium/pools.hpp
Normal 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
|
||||||
6
source/common.cpp
Normal file
6
source/common.cpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <pd/common.hpp>
|
||||||
|
|
||||||
|
void PD::Log(const std::string& txt) {
|
||||||
|
std::cout << "[PD] " << txt << std::endl;
|
||||||
|
}
|
||||||
@@ -5,15 +5,20 @@ PD_API std::unique_ptr<GfxDriver> Gfx::driver;
|
|||||||
|
|
||||||
GfxDriver::GfxDriver(std::string_view name) : DriverInterface(name) {}
|
GfxDriver::GfxDriver(std::string_view name) : DriverInterface(name) {}
|
||||||
|
|
||||||
void GfxDriver::SetViewPort(const ivec2& size) { ViewPort = size; }
|
void GfxDriver::SetViewPort(const ivec2& size) {
|
||||||
|
ViewPort = size;
|
||||||
|
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
|
||||||
|
}
|
||||||
void GfxDriver::SetViewPort(int x, int y) {
|
void GfxDriver::SetViewPort(int x, int y) {
|
||||||
ViewPort.x = x;
|
ViewPort.x = x;
|
||||||
ViewPort.y = y;
|
ViewPort.y = y;
|
||||||
|
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxDriver::Reset() {
|
void GfxDriver::Reset() {
|
||||||
CurrentVertex = 0;
|
CurrentVertex = 0;
|
||||||
CurrentIndex = 0;
|
CurrentIndex = 0;
|
||||||
|
ResetPools();
|
||||||
SysReset();
|
SysReset();
|
||||||
}
|
}
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
26
source/lithium/pools.cpp
Normal file
26
source/lithium/pools.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include <pd/lithium/pools.hpp>
|
||||||
|
|
||||||
|
#include "pd/common.hpp"
|
||||||
|
#include "pd/core/pool.hpp"
|
||||||
|
#include "pd/lithium/vertex.hpp"
|
||||||
|
|
||||||
|
namespace PD {
|
||||||
|
namespace Li {
|
||||||
|
PD::Pool<Vertex> pVtxPool;
|
||||||
|
PD::Pool<u16> pIdxPool;
|
||||||
|
|
||||||
|
void InitPools(size_t max_vertices) {
|
||||||
|
pVtxPool.Init(max_vertices);
|
||||||
|
pIdxPool.Init(max_vertices * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertex* AllocateVertices(size_t count) { return pVtxPool.Allocate(count); }
|
||||||
|
|
||||||
|
u16* AllocateIndices(size_t count) { return pIdxPool.Allocate(count); }
|
||||||
|
|
||||||
|
void ResetPools() {
|
||||||
|
pVtxPool.Reset();
|
||||||
|
pIdxPool.Reset();
|
||||||
|
}
|
||||||
|
} // namespace Li
|
||||||
|
} // namespace PD
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.22)
|
cmake_minimum_required(VERSION 3.22)
|
||||||
|
|
||||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/core)
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/core)
|
||||||
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/gfx)
|
||||||
6
tests/gfx/CMakeLists.txt
Normal file
6
tests/gfx/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.22)
|
||||||
|
|
||||||
|
project(gfx-tests)
|
||||||
|
|
||||||
|
add_executable(gfx-tests ${CMAKE_CURRENT_SOURCE_DIR}/source/main.cpp)
|
||||||
|
target_link_libraries(gfx-tests PRIVATE palladium::palladium pd-system glfw)
|
||||||
73
tests/gfx/source/main.cpp
Normal file
73
tests/gfx/source/main.cpp
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <glad/glad.h>
|
||||||
|
//////////////////////////
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include <format>
|
||||||
|
#include <gfx_opengl.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <palladium>
|
||||||
|
|
||||||
|
#include "pd/core/vec2.hpp"
|
||||||
|
#include "pd/lithium/pools.hpp"
|
||||||
|
#include "pd/lithium/vertex.hpp"
|
||||||
|
|
||||||
|
class App {
|
||||||
|
public:
|
||||||
|
App() {
|
||||||
|
PD::Os::UseDriver<PD::OsDriver>();
|
||||||
|
PD::Gfx::UseDriver<PD::GfxOpenGL>();
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
|
||||||
|
window = glfwCreateWindow(1280, 720, "gfx_tests", nullptr, nullptr);
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
|
||||||
|
glfwSwapInterval(1);
|
||||||
|
PD::Gfx::Init();
|
||||||
|
PD::Li::InitPools(8192);
|
||||||
|
std::vector<PD::u8> img(16 * 16 * 4, 0xff);
|
||||||
|
pTex = PD::Gfx::LoadTexture(img, 16, 16);
|
||||||
|
std::cout << "GfxDriver: " << PD::Gfx::GetDriverName() << std::endl;
|
||||||
|
pPool.Init(10);
|
||||||
|
auto cmd = pPool.Allocate(1);
|
||||||
|
cmd->Reserve(4, 6);
|
||||||
|
cmd->Add(PD::Li::Vertex(PD::fvec2(0, 0), PD::fvec2(0, 0), 0xffffffff));
|
||||||
|
cmd->Add(PD::Li::Vertex(PD::fvec2(50, 0), PD::fvec2(1, 0), 0xffffffff));
|
||||||
|
cmd->Add(PD::Li::Vertex(PD::fvec2(50, 50), PD::fvec2(1, 1), 0xffffffff));
|
||||||
|
cmd->Add(PD::Li::Vertex(PD::fvec2(0, 50), PD::fvec2(0, 1), 0xffffffff));
|
||||||
|
cmd->Add(0, 1, 2);
|
||||||
|
cmd->Add(0, 2, 3);
|
||||||
|
cmd->Tex = pTex;
|
||||||
|
}
|
||||||
|
~App() {
|
||||||
|
PD::Gfx::Deinit();
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Run() {
|
||||||
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
glViewport(0, 0, 1280, 720);
|
||||||
|
glClearColor(0.1, 0.1, 0.1, 0.1);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
PD::Gfx::Reset();
|
||||||
|
PD::Gfx::SetViewPort(1280, 720);
|
||||||
|
PD::Gfx::Draw(pPool);
|
||||||
|
glfwPollEvents();
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLFWwindow* window = nullptr;
|
||||||
|
PD::Pool<PD::Li::Command> pPool;
|
||||||
|
PD::ptr pTex = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
App app;
|
||||||
|
app.Run();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
6
vendor/CMakeLists.txt
vendored
6
vendor/CMakeLists.txt
vendored
@@ -6,3 +6,9 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glad)
|
|||||||
# STB
|
# STB
|
||||||
add_library(stb INTERFACE)
|
add_library(stb INTERFACE)
|
||||||
target_include_directories(stb INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/stb)
|
target_include_directories(stb INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/stb)
|
||||||
|
|
||||||
|
# GLFW
|
||||||
|
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "")
|
||||||
|
set(GLFW_BUILD_TESTS OFF CACHE BOOL "")
|
||||||
|
set(GLFW_BUILD_DOCS OFF CACHE BOOL "")
|
||||||
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glfw)
|
||||||
1
vendor/glfw
vendored
Submodule
1
vendor/glfw
vendored
Submodule
Submodule vendor/glfw added at 7b6aead9fb
Reference in New Issue
Block a user