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"]
|
||||
path = vendor/stb
|
||||
url = https://github.com/nothings/stb.git
|
||||
[submodule "vendor/glfw"]
|
||||
path = vendor/glfw
|
||||
url = https://github.com/glfw/glfw.git
|
||||
|
||||
107
CMakeLists.txt
107
CMakeLists.txt
@@ -14,89 +14,110 @@ option(PD_BUILD_SHARED "Build Shared Library" OFF)
|
||||
option(PD_BUILD_TOOLS "Build Palladium Tools" OFF)
|
||||
|
||||
if(${PD_BUILD_TOOLS})
|
||||
add_subdirectory(tools)
|
||||
add_subdirectory(tools)
|
||||
endif()
|
||||
|
||||
add_subdirectory(vendor)
|
||||
|
||||
# # Include Library Source
|
||||
set(PD_SOURCES
|
||||
# Common
|
||||
source/common.cpp
|
||||
|
||||
# Core
|
||||
source/core/bits.cpp
|
||||
source/core/color.cpp
|
||||
source/core/mat.cpp
|
||||
source/core/strings.cpp
|
||||
# Core
|
||||
source/core/bits.cpp
|
||||
source/core/color.cpp
|
||||
source/core/mat.cpp
|
||||
source/core/strings.cpp
|
||||
|
||||
source/drivers/os.cpp
|
||||
source/drivers/gfx.cpp
|
||||
# Drivers
|
||||
source/drivers/os.cpp
|
||||
source/drivers/gfx.cpp
|
||||
|
||||
# Lithium
|
||||
source/lithium/pools.cpp
|
||||
)
|
||||
|
||||
if(${PD_BUILD_SHARED})
|
||||
add_library(palladium SHARED ${PD_SOURCES})
|
||||
target_compile_definitions(palladium PRIVATE -DPD_BUILD_SHARED)
|
||||
add_library(palladium SHARED ${PD_SOURCES})
|
||||
target_compile_definitions(palladium PRIVATE -DPD_BUILD_SHARED)
|
||||
else()
|
||||
add_library(palladium STATIC ${PD_SOURCES})
|
||||
target_compile_definitions(palladium PUBLIC -DPD_BUILD_STATIC)
|
||||
add_library(palladium STATIC ${PD_SOURCES})
|
||||
target_compile_definitions(palladium PUBLIC -DPD_BUILD_STATIC)
|
||||
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)
|
||||
|
||||
target_include_directories(palladium
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
target_compile_options(palladium PRIVATE
|
||||
$<$<CONFIG:Debug>:-O0 -g>
|
||||
$<$<CONFIG:Release>:-O3>
|
||||
target_compile_options(palladium
|
||||
PRIVATE
|
||||
$<$<CONFIG:Debug>:-O0 -g>
|
||||
$<$<CONFIG:Release>:-O3>
|
||||
)
|
||||
|
||||
install(TARGETS palladium
|
||||
EXPORT palladiumTargets
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
install(
|
||||
TARGETS palladium
|
||||
EXPORT palladiumTargets
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
install(DIRECTORY include/
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
install(
|
||||
DIRECTORY include/
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
install(EXPORT palladiumTargets
|
||||
FILE palladiumTargets.cmake
|
||||
NAMESPACE palladium::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||
FILE palladiumTargets.cmake
|
||||
NAMESPACE palladium::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||
)
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
configure_package_config_file(
|
||||
cmake/palladiumConfig.cmake.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||
cmake/palladiumConfig.cmake.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
||||
INSTALL_DESTINATION
|
||||
${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||
)
|
||||
|
||||
write_basic_package_version_file(
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
||||
VERSION ${PROJECT_VERSION}
|
||||
COMPATIBILITY SameMajorVersion
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
||||
VERSION
|
||||
${PROJECT_VERSION}
|
||||
COMPATIBILITY
|
||||
SameMajorVersion
|
||||
)
|
||||
|
||||
install(FILES
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||
install(
|
||||
FILES
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
|
||||
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
|
||||
)
|
||||
|
||||
find_program(CLANG_FORMAT clang-format)
|
||||
|
||||
file(GLOB_RECURSE PD_FMTFILES CONFIGURE_DEPENDS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
|
||||
file(GLOB_RECURSE PD_FMTFILES
|
||||
CONFIGURE_DEPENDS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
|
||||
)
|
||||
|
||||
add_custom_target(pd-clang-format
|
||||
COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES}
|
||||
COMMENT "Formatting Project Sources"
|
||||
COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES}
|
||||
COMMENT "Formatting Project Sources"
|
||||
)
|
||||
|
||||
add_subdirectory(backends)
|
||||
|
||||
@@ -7,15 +7,23 @@ option(PD_ENABLE_OPENGL3 "Enable OpenGL 3.3 (On Supported Hardware)" ON)
|
||||
option(PD_ENABLE_VULKAN "Not implemented yet" OFF)
|
||||
|
||||
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
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/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
|
||||
PUBLIC
|
||||
palladium::palladium
|
||||
glad
|
||||
PUBLIC
|
||||
palladium::palladium
|
||||
glad
|
||||
)
|
||||
@@ -17,7 +17,7 @@ struct GfxOpenGLConfig {
|
||||
};
|
||||
class GfxOpenGL : public GfxDriverBase<GfxOpenGLConfig> {
|
||||
public:
|
||||
GfxOpenGL() {}
|
||||
GfxOpenGL(): GfxDriverBase("OpenGL2") {}
|
||||
~GfxOpenGL() {}
|
||||
|
||||
void SysInit() override;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <gfx_opengl.hpp>
|
||||
|
||||
#include "pd/common.hpp"
|
||||
#include "pd/drivers/gfx.hpp"
|
||||
|
||||
namespace PD {
|
||||
GLuint compileShader(const std::string& source, GLenum type) {
|
||||
GLuint shader = glCreateShader(type);
|
||||
@@ -42,6 +45,8 @@ GLuint createShaderProgram(const std::string& vertexShaderSource,
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
if (success) PDLOG("Shader [{}] compiled sucessfully", shaderProgram);
|
||||
|
||||
return shaderProgram;
|
||||
}
|
||||
|
||||
@@ -118,16 +123,36 @@ void GfxOpenGL::SysInit() {
|
||||
|
||||
glBindBuffer(GL_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() {
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteBuffers(1, &IBO);
|
||||
PDLOG("GfxOpenGL::SysDeinit()");
|
||||
}
|
||||
|
||||
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,
|
||||
(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) {
|
||||
@@ -139,12 +164,37 @@ void GfxOpenGL::BindTexture(TextureID id) {
|
||||
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,
|
||||
int h, TextureFormat type,
|
||||
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) {
|
||||
|
||||
@@ -24,4 +24,5 @@ SOFTWARE.
|
||||
*/
|
||||
|
||||
#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 <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
|
||||
@@ -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; }
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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
|
||||
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) {}
|
||||
|
||||
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) {
|
||||
ViewPort.x = x;
|
||||
ViewPort.y = y;
|
||||
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
|
||||
}
|
||||
|
||||
void GfxDriver::Reset() {
|
||||
CurrentVertex = 0;
|
||||
CurrentIndex = 0;
|
||||
ResetPools();
|
||||
SysReset();
|
||||
}
|
||||
} // 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)
|
||||
|
||||
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;
|
||||
}
|
||||
8
vendor/CMakeLists.txt
vendored
8
vendor/CMakeLists.txt
vendored
@@ -5,4 +5,10 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glad)
|
||||
|
||||
# STB
|
||||
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