Skip to content

Commit 6458806

Browse files
Start workin' on RHI
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 17595c0 commit 6458806

21 files changed

Lines changed: 536 additions & 14 deletions

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "engine/native/thirdparty/bgfx"]
88
path = engine/native/thirdparty/bgfx
99
url = https://github.com/bkaradzic/bgfx
10+
[submodule "engine/native/thirdparty/sdl"]
11+
path = engine/native/thirdparty/sdl
12+
url = https://github.com/libsdl-org/SDL

CMakeLists.txt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,53 @@ endif()
2525

2626
project(DraconicEngine LANGUAGES C CXX)
2727

28+
# Ensure that everyone is on the same page hardware wise or else we get errors
29+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
30+
add_compile_options(-mavx2 -mfma)
31+
endif()
32+
2833
# Only have safe global rules here
2934
set(CMAKE_CXX_STANDARD 23)
3035
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3136
set(CMAKE_CXX_MODULE_STD ON)
37+
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # bgfx & deps give error if this isn't set, this is a temp workaround
38+
# Force all to use Clang or else a mismatch occurs
39+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
40+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
3241

3342
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
3443

3544
include(CTest)
3645

37-
add_subdirectory(engine/native)
46+
add_subdirectory(engine/native)
47+
48+
find_package(PkgConfig REQUIRED)
49+
pkg_check_modules(X11_LIBS REQUIRED IMPORTED_TARGET x11 xext xcursor xrandr xrender xi xfixes)
50+
51+
add_executable(draconic engine/native/main/main.cpp)
52+
enable_modules(draconic)
53+
target_link_libraries(draconic
54+
PRIVATE
55+
rhi
56+
core
57+
bgfx
58+
bx
59+
bimg
60+
-Wl,--whole-archive SDL3::SDL3-static -Wl,--no-whole-archive
61+
c++
62+
c++abi
63+
PkgConfig::X11_LIBS
64+
dl
65+
pthread
66+
m
67+
)
68+
69+
# Shader copying
70+
set(ENGINE_SHADER_DIR "${CMAKE_SOURCE_DIR}/engine/native/rendering/shaders")
71+
add_custom_command(
72+
TARGET draconic POST_BUILD
73+
COMMAND ${CMAKE_COMMAND} -E copy_directory
74+
"${ENGINE_SHADER_DIR}"
75+
"$<TARGET_FILE_DIR:draconic>"
76+
COMMENT "Copying shaders to build directory..."
77+
)

CMakePresets.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"CMAKE_CXX_STANDARD": "23",
2222
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
2323
"CMAKE_CXX_EXTENSIONS": "OFF",
24-
"CMAKE_CXX_MODULE_STD": "1",
2524
"CMAKE_CXX_FLAGS_INIT": "-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
2625
}
2726
},

cmake/Compiler.cmake

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ else()
1515
add_compile_definitions(DEBUG)
1616
endif()
1717

18-
# Force Clang to use libc++
19-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
20-
add_compile_options(-stdlib=libc++)
21-
add_link_options(-stdlib=libc++)
22-
endif()
23-
2418
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
2519
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
2620
# TODO: Make SIMD level configurable or detect at runtime

engine/native/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ include(Modules)
44
add_subdirectory(thirdparty)
55

66
add_modules_library(core SHARED)
7-
target_link_libraries(core PUBLIC definitions math)
7+
target_link_libraries(core PUBLIC definitions math filesystem)
8+
9+
add_subdirectory(rendering)

engine/native/core/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
add_modules_library(definitions)
22
add_modules_library(math)
3-
target_link_libraries(math PUBLIC definitions)
3+
add_modules_library(filesystem)
4+
target_link_libraries(math PUBLIC definitions)
5+
target_link_libraries(filesystem PUBLIC definitions)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module;
2+
3+
import std;
4+
5+
module core.filesystem;
6+
7+
namespace draco::filesystem
8+
{
9+
std::vector<std::uint8_t> load_binary(const std::string& path)
10+
{
11+
// Open at the end (ate) to get size and in binary mode
12+
std::ifstream file(path, std::ios::binary | std::ios::ate);
13+
14+
if (!file.is_open()) {
15+
std::println("Error: Could not open file at: {}", path);
16+
// Return an empty vector
17+
return {};
18+
}
19+
20+
std::streamsize size = file.tellg();
21+
if (size <= 0) {
22+
std::println("Error: File is empty or unreadable: {}", path);
23+
return {};
24+
}
25+
26+
file.seekg(0, std::ios::beg);
27+
28+
std::vector<std::uint8_t> buffer(static_cast<std::size_t>(size));
29+
if (file.read(reinterpret_cast<char*>(buffer.data()), size)) {
30+
return buffer;
31+
}
32+
33+
std::println("Error: Failed to read file contents: {}", path);
34+
return {};
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export module core.filesystem;
2+
3+
import std;
4+
5+
export namespace draco::filesystem
6+
{
7+
// Returns a buffer of the file data
8+
std::vector<std::uint8_t> load_binary(const std::string& path);
9+
}

engine/native/main/main.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import std;
2+
3+
#include <SDL3/SDL.h>
4+
#include <bgfx/bgfx.h>
5+
#include <bx/math.h>
6+
7+
import core.filesystem;
8+
9+
import rendering.rhi;
10+
import rendering.rhi.vertex;
11+
12+
int main(int argc, char* argv[])
13+
{
14+
if (!SDL_Init(SDL_INIT_VIDEO)) {
15+
std::println("SDL init failed: {}", SDL_GetError());
16+
return -1;
17+
}
18+
19+
SDL_Window* window = SDL_CreateWindow(
20+
"Draconic Engine",
21+
1280, 720,
22+
SDL_WINDOW_RESIZABLE
23+
);
24+
25+
if (!window) {
26+
std::println("Failed to create window: {}", SDL_GetError());
27+
return -1;
28+
}
29+
30+
const char* driver = SDL_GetCurrentVideoDriver();
31+
std::println("Driver: {}", driver ? driver : "Unknown");
32+
33+
void* nwh = nullptr; // Native window handle
34+
void* ndt = nullptr; // Native display type
35+
36+
#if defined(__linux__)
37+
38+
SDL_PropertiesID props = SDL_GetWindowProperties(window);
39+
40+
if (driver && std::string_view(driver) == "x11")
41+
{
42+
ndt = SDL_GetPointerProperty(props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, nullptr); // Get the X11 display pointer
43+
nwh = (void*)(uintptr_t)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); // Get the X11 window number and cast it to a pointer
44+
}
45+
46+
#endif
47+
48+
if (!nwh) {
49+
std::println("Failed to get native window handle");
50+
return -1;
51+
}
52+
53+
if(!ndt) {
54+
std::println("Failed to get native display type");
55+
return -1;
56+
}
57+
58+
// Init the RHI with the native window handle and initial size
59+
if (!draco::rhi::init(ndt, nwh, 1280, 720)) {
60+
std::println("Failed to initialize RHI");
61+
SDL_DestroyWindow(window);
62+
SDL_Quit();
63+
return -1;
64+
}
65+
66+
// Geometry data for a triangle to test rendering
67+
// It includes both positions & colors
68+
draco::rhi::PosColorVertex triangle[] = {
69+
{ 0.0f, 0.5f, 0.0f, 0xff0000ff },
70+
{ 0.5f, -0.5f, 0.0f, 0xff00ff00 },
71+
{ -0.5f, -0.5f, 0.0f, 0xffff0000}
72+
};
73+
74+
auto vbh = draco::rhi::create_vertex_buffer(triangle, sizeof(triangle));
75+
76+
// Load the vertex & fragment shaders
77+
auto vs_data = draco::filesystem::load_binary("vs_triangle.bin");
78+
auto fs_data = draco::filesystem::load_binary("fs_triangle.bin");
79+
80+
// If the path is empty, return an error
81+
if (vs_data.empty() || fs_data.empty()) {
82+
std::println("Failed to load shaders");
83+
std::println("Workin' dir: {}", std::filesystem::current_path().string());
84+
return -1;
85+
}
86+
87+
auto vsh = draco::rhi::create_shader(vs_data.data(), (uint32_t)vs_data.size());
88+
auto fsh = draco::rhi::create_shader(fs_data.data(), (uint32_t)fs_data.size());
89+
90+
91+
// TODO: Expose our own macros for the state flags instead of using bgfx's directly, tis is just for testin'
92+
auto pipeline = draco::rhi::create_pipeline({vsh, fsh, (0
93+
| BGFX_STATE_WRITE_RGB
94+
| BGFX_STATE_WRITE_A
95+
| BGFX_STATE_MSAA
96+
| BGFX_STATE_PT_TRISTRIP)});
97+
98+
bool running = true;
99+
100+
while (running)
101+
{
102+
SDL_Event event;
103+
while (SDL_PollEvent(&event))
104+
{
105+
if (event.type == SDL_EVENT_QUIT)
106+
running = false;
107+
}
108+
109+
int w, h;
110+
SDL_GetWindowSize(window, &w, &h);
111+
112+
draco::rhi::resize(uint16_t(w), uint16_t(h));
113+
114+
draco::rhi::begin_frame();
115+
116+
draco::rhi::RenderPacket packet{};
117+
packet.vertex_buffer = vbh;
118+
packet.pipeline = pipeline;
119+
bx::mtxIdentity(packet.model);
120+
121+
draco::rhi::submit(packet, 0);
122+
123+
draco::rhi::end_frame();
124+
}
125+
126+
draco::rhi::shutdown();
127+
128+
SDL_DestroyWindow(window);
129+
SDL_Quit();
130+
return 0;
131+
}
132+
133+
// Fun fact: AR literally went mad & tis is the result
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_modules_library(rhi SHARED)
2+
target_sources(rhi PRIVATE rhi/rhi_bgfx.cpp rhi/vertex.cppm)
3+
target_link_libraries(rhi PUBLIC core bgfx bx)

0 commit comments

Comments
 (0)