From 37fdcb8a0f435c42f737252450626f2bd38bbd0f Mon Sep 17 00:00:00 2001 From: ladevieq Date: Tue, 5 May 2026 00:50:13 +0200 Subject: Clean up around included files --- include/aabb.hpp | 3 +- include/bvh.hpp | 5 ++- include/camera.hpp | 2 -- include/gltf.hpp | 67 +++++++++++++++++------------------ include/material.hpp | 15 -------- include/mesh.hpp | 22 ++---------- include/scene.hpp | 60 +++++++++++--------------------- include/utils.hpp | 78 +++++++++++++++++++++++++++-------------- include/vec3.hpp | 2 +- include/vulkan-loader.hpp | 23 ++---------- include/window.hpp | 89 ++++++++++++++++++++++++++++++++--------------- 11 files changed, 178 insertions(+), 188 deletions(-) (limited to 'include') diff --git a/include/aabb.hpp b/include/aabb.hpp index adeb1fb..4e47a8f 100644 --- a/include/aabb.hpp +++ b/include/aabb.hpp @@ -1,8 +1,9 @@ #ifndef __AABB_HPP_ #define __AABB_HPP_ +#include + #include "vec3.hpp" -#include "triangle.hpp" class aabb { public: diff --git a/include/bvh.hpp b/include/bvh.hpp index 2b66dcb..28a8554 100644 --- a/include/bvh.hpp +++ b/include/bvh.hpp @@ -1,10 +1,9 @@ #pragma once -#include +#include -#include "aabb.hpp" -// #include "sphere.hpp" #include "triangle.hpp" +#include "aabb.hpp" struct bvh_node { aabb bounding_box; diff --git a/include/camera.hpp b/include/camera.hpp index 5c1db61..ab81896 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -1,8 +1,6 @@ #ifndef __CAMERA_HPP_ #define __CAMERA_HPP_ -#include "vec3.hpp" - class camera { public: camera( diff --git a/include/gltf.hpp b/include/gltf.hpp index 1ccaf56..72d757c 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -3,59 +3,60 @@ #include -#include - -using json = nlohmann::json; - -#include "material.hpp" +#include "utils.hpp" +#include "handle.hpp" #include "mesh.hpp" +#include "material.hpp" struct node { - std::vector children; - Mesh* mesh = nullptr; + std::vector children; + u32 mesh; }; struct raw_image { int32_t width; int32_t height; int32_t channels; - uint8_t *data; + const uint8_t *data; }; -namespace std::filesystem { - class path; -} +struct buffer { + u8* bytes; + size_t size; +}; class gltf { public: - gltf(const std::filesystem::path &filepath); - - node root_node; - -private: - - void load_node(uint32_t index, node &parent); - - void load_meshes(); - - Mesh::submesh load_primitive(const json &primitive); - - Mesh::attribute load_attribute(uint32_t accessor_index); - - void load_textures(const std::filesystem::path &path); + gltf(const char* filepath); + void deinit(); - void load_materials(); + std::vector root_nodes; - json gltf_json; + std::vector nodes; std::vector meshes; std::vector materials; -#ifdef API_TEST std::vector> textures; // handle samplers; -#else - std::vector textures; -#endif // API_TEST - std::vector> buffers; + std::vector buffers; + + + // gbl only + struct glb_chunk { + u32 length; + u32 type; + u8 data[1]; + }; + + struct glb_header { + u32 magic; + u32 version; + u32 length; + }; + std::vector chunks; + glb_header* header; + + // TODO: remove + bool is_glb; }; #endif diff --git a/include/material.hpp b/include/material.hpp index 35710d2..167e58e 100644 --- a/include/material.hpp +++ b/include/material.hpp @@ -1,24 +1,9 @@ #pragma once -#include - -#include "vec3.hpp" - -#ifdef API_TEST -#include "vk-device-types.hpp" -#else -class Texture; -#endif - struct material { color base_color; -#ifdef API_TEST handle base_color_texture; handle metallic_roughness_texture; -#else - Texture* base_color_texture; - Texture* metallic_roughness_texture; -#endif // API_TEST float metalness = 1.f; float roughness = 1.f; }; diff --git a/include/mesh.hpp b/include/mesh.hpp index b88896d..c396814 100644 --- a/include/mesh.hpp +++ b/include/mesh.hpp @@ -1,15 +1,5 @@ #pragma once -#include -#include -#include - -#ifdef API_TEST -#include "vk-device-types.hpp" -#else -#include "vk-renderer.hpp" -#endif // API_TEST - enum ATTRIBUTE_TYPE { POSITION, NORMAL, @@ -34,15 +24,15 @@ public: }; struct attribute { - uint8_t* data = nullptr; + const uint8_t* data = nullptr; size_t length = 0; }; struct submesh { attribute indices; std::array attributes; - uint32_t vertex_count = 0; - uint32_t index_count = 0; + u32 vertex_count = 0; + u32 index_count = 0; // material& material; }; @@ -93,15 +83,9 @@ private: std::vector submeshes; struct gpu_mesh { -#ifdef API_TEST std::array, ATTRIBUTE_TYPE::MAX_ATTRIBUTE> attributes; const device_buffer& indices; const device_buffer& submeshes; -#else - std::array attributes; - Buffer* indices; - Buffer* submeshes; -#endif // API_TEST }; // allocate gpu mesh upon instanciation diff --git a/include/scene.hpp b/include/scene.hpp index 091e4fc..a1c76e2 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -1,44 +1,36 @@ #ifndef __SCENE_HPP_ #define __SCENE_HPP_ -#include - -#ifdef API_TEST -#include "vk-device-types.hpp" -#include "vk-device.hpp" -#endif // API_TEST -#include "camera.hpp" - class Buffer; -class scene { - struct metadata { - camera cam; +struct metadata { + camera cam; - uint32_t max_bounce = 3; - uint32_t min_bounce = 1; + uint32_t max_bounce = 3; + uint32_t min_bounce = 1; - uint32_t width; - uint32_t height; + uint32_t width; + uint32_t height; - uint32_t sample_index = 0; + uint32_t sample_index = 1; - uint32_t enable_dof = (uint32_t)false; - uint32_t debug_bvh = (uint32_t)false; - int32_t downscale_factor = 2; + uint32_t enable_dof = (uint32_t)false; + uint32_t debug_bvh = (uint32_t)false; + int32_t downscale_factor = 1; - metadata(const camera &cam, uint32_t width, uint32_t height); - }; + metadata(const camera &cam, uint32_t width, uint32_t height); +}; +class scene { // void random_scene(); + gltf model; public: scene(const camera& cam, uint32_t width, uint32_t height); + void deinit(); metadata meta; -#ifdef API_TEST - handle scene_buffer_handle; handle indices_buffer_handle; handle positions_buffer_handle; handle normals_buffer_handle; @@ -46,22 +38,12 @@ public: handle bvh_buffer_handle; handle materials_buffer_handle; - uint64_t scene_buffer_address() { return vkdevice::get_render_device().get_buffer(scene_buffer_handle).device_address; }; - uint64_t indices_buffer_address() { return vkdevice::get_render_device().get_buffer(indices_buffer_handle).device_address; }; - uint64_t positions_buffer_address() { return vkdevice::get_render_device().get_buffer(positions_buffer_handle).device_address; }; - uint64_t normals_buffer_address() { return vkdevice::get_render_device().get_buffer(normals_buffer_handle).device_address; }; - uint64_t uvs_buffer_address() { return vkdevice::get_render_device().get_buffer(uvs_buffer_handle).device_address; }; - uint64_t bvh_buffer_address() { return vkdevice::get_render_device().get_buffer(bvh_buffer_handle).device_address; }; - uint64_t materials_buffer_address() { return vkdevice::get_render_device().get_buffer(materials_buffer_handle).device_address; }; -#else - Buffer* scene_buffer; - Buffer* indices_buffer; - Buffer* positions_buffer; - Buffer* normals_buffer; - Buffer* uvs_buffer; - Buffer* bvh_buffer; - Buffer* materials_buffer; -#endif // API_TEST + uint64_t indices_buffer_address() { return vkdevice::get().get_buffer(indices_buffer_handle).device_address; }; + uint64_t positions_buffer_address() { return vkdevice::get().get_buffer(positions_buffer_handle).device_address; }; + uint64_t normals_buffer_address() { return vkdevice::get().get_buffer(normals_buffer_handle).device_address; }; + uint64_t uvs_buffer_address() { return vkdevice::get().get_buffer(uvs_buffer_handle).device_address; }; + uint64_t bvh_buffer_address() { return vkdevice::get().get_buffer(bvh_buffer_handle).device_address; }; + uint64_t materials_buffer_address() { return vkdevice::get().get_buffer(materials_buffer_handle).device_address; }; }; #endif // !__SCENE_HPP_ diff --git a/include/utils.hpp b/include/utils.hpp index e49b218..19a5d9f 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,13 +1,56 @@ #pragma once -#include -#include - -std::vector read_file(const char* path); +static constexpr size_t KB = 1024U; +static constexpr size_t MB = 1024U * KB; +static constexpr size_t GB = 1024U * MB; +const char* DUMMY_NAME = "NO_NAME"; + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +uint8_t* read_file(const char* path, size_t size); + +const uint32_t max_markers = 1024U; +// hashmap ? +uint32_t marker_count = 0U; +const char* marker_names[max_markers] = { 0 }; +uint64_t marker_counter_start[max_markers] = { 0 }; +uint64_t marker_counter_end[max_markers] = { 0 }; + +#define BEGIN_MARKER(name) \ + static uint32_t marker_##name##_index = UINT32_MAX; \ + do { \ + if (marker_##name##_index == UINT32_MAX) { \ + marker_##name##_index = marker_count++; \ + marker_names[marker_##name##_index] = #name; \ + } \ + { \ + LARGE_INTEGER start_qpc; \ + QueryPerformanceCounter(&start_qpc); \ + marker_counter_start[marker_##name##_index] = start_qpc.QuadPart; \ + } \ + } while(0); + +#define END_MARKER(name) \ + do { \ + LARGE_INTEGER end_qpc; \ + QueryPerformanceCounter(&end_qpc); \ + marker_counter_end[marker_##name##_index] = end_qpc.QuadPart; \ + } while(0); #define PI 3.14159265359 #define EPSILON 0.000001 + +#define ALIGN(value, alignment) \ + (((value) + ((alignment) - 1)) & ~((alignment) - 1)) + inline float deg_to_rad(float deg) { return static_cast(deg * PI / 180.f); } @@ -31,31 +74,14 @@ inline float clamp(float x, float min, float max) { } -#if defined(WINDOWS) - -#include +#if _WIN32 void log_last_error(); -class watcher { -public: - typedef void(*func)(void); - - static bool watch_file(const char* filepath, func callback); - - static bool watch_dir(const char* dir_path); - - static void pull_changes(); - -private: +typedef void(*func)(void); +bool watch_file(const char* filepath, func callback); - struct watch_data { - HANDLE dir_handle; - OVERLAPPED overlapped; - DWORD buffer[512]; - }; +bool watch_dir(const char* dir_path); - inline static std::unordered_map callbacks; - inline static std::unordered_map watched_dirs; -}; +void pull_changes(); #endif diff --git a/include/vec3.hpp b/include/vec3.hpp index 42172e6..5fd143d 100644 --- a/include/vec3.hpp +++ b/include/vec3.hpp @@ -1,7 +1,6 @@ #ifndef __VEC3_HPP_ #define __VEC3_HPP_ -#include #include struct vec3u { @@ -60,6 +59,7 @@ class vec3 { vec3& normalize(); [[nodiscard]] bool near_zero() const; + [[nodiscard]] bool is_zero() const; void print() const; diff --git a/include/vulkan-loader.hpp b/include/vulkan-loader.hpp index ec1464d..7a3a68e 100644 --- a/include/vulkan-loader.hpp +++ b/include/vulkan-loader.hpp @@ -3,8 +3,7 @@ #define VK_NO_PROTOTYPES -#if defined(WINDOWS) -#include +#if _WIN32 #include typedef unsigned long DWORD; typedef const wchar_t* LPCWSTR; @@ -24,7 +23,6 @@ typedef struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES; X(vkGetInstanceProcAddr) \ X(vkGetDeviceProcAddr) - #define VULKAN_APPLICATION_FUNCTIONS \ X(vkCreateInstance) \ X(vkEnumerateInstanceVersion) \ @@ -154,32 +152,15 @@ typedef struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES; X(vkDestroyBuffer) \ X(vkDestroyRenderPass) + #define X(name) extern PFN_##name name; - VULKAN_EXPORTED_FUNCTIONS VULKAN_APPLICATION_FUNCTIONS VULKAN_INSTANCE_FUNCTIONS VULKAN_DEVICE_FUNCTIONS #undef X - -#define X(name) void Load_##name(); - VULKAN_EXPORTED_FUNCTIONS - VULKAN_APPLICATION_FUNCTIONS -#undef X - -#define X(name) void Load_##name(VkInstance _instance); - VULKAN_INSTANCE_FUNCTIONS -#undef X - -#define X(name) void Load_##name(VkDevice _device); - VULKAN_DEVICE_FUNCTIONS -#undef X - - void load_vulkan(); - void load_instance_functions(VkInstance instance); - void load_device_functions(VkDevice device); #endif // !__VULKAN_LOADER_HPP_ diff --git a/include/window.hpp b/include/window.hpp index 2484742..9c79614 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -1,23 +1,10 @@ #ifndef __WINDOW_HPP_ #define __WINDOW_HPP_ -#include - -class vkrenderer; - #if defined(LINUX) #include #include -#elif defined(WINDOWS) -// #include -typedef struct HWND__* HWND; -typedef int64_t LRESULT; -typedef int64_t LPARAM; -typedef uint64_t WPARAM; -typedef unsigned int UINT; -typedef void* LPVOID; -#define CALLBACK __stdcall #endif // enum MODIFIERS: uint32_t { @@ -37,14 +24,22 @@ typedef void* LPVOID; // }; // TODO improve this -#if defined(WINDOWS) -enum KEYS: uint32_t { +#if _WIN32 +enum KEY: uint32_t { LSHIFT = 0x10, RSHIFT = 0x10, LCTRL = 0x11, RCTRL = 0x11, ALT, SPACE = 0x20, + PAGE_UP, + PAGE_DOWN, + END, + HOME, + LEFT_ARROW, + UP_ARROW, + RIGHT_ARROW, + DOWN_ARROW, A = 0x41, B, C, @@ -71,10 +66,22 @@ enum KEYS: uint32_t { X, Y, Z, - MAX_KEYS = 255 + F1 = 0x70, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + MAX_KEY = 0xff }; #else -enum KEYS: uint32_t { +enum KEY: uint32_t { SPACE = 32, A = 0x61, B, @@ -108,25 +115,43 @@ enum KEYS: uint32_t { RCTRL, LALT = 65513, RALT, - MAX_KEYS = 0xffff + MAX_KEY = 0xffff }; #endif -enum BUTTONS: uint32_t { +enum BUTTON: uint32_t { LEFT, RIGHT, MIDDLE, - MAX_BUTTONS = 255 + MAX_BUTTON = 255 }; -enum EVENT_TYPES: int32_t { +enum EVENT_TYPE: int32_t { QUIT, RESIZE, KEY_PRESS, + KEY_DOWN, KEY_RELEASE, + KEY_UP, BUTTON_PRESS, BUTTON_RELEASE, - MOUSE_MOVE + MOUSE_MOVE, + MOUSE_WHEEL, +}; + +enum CURSOR_TYPE: int32_t { + ARROW, + TEXT_INPUT, + SIZE_ALL, + SIZE_NS, + SIZE_WE, + SIZE_NESW, + SIZE_NWSE, + HAND, + WAIT, + PROGRESS, + NOT_ALLOWED, + MAX_CURSOR, }; // struct event { @@ -148,10 +173,14 @@ struct event { int32_t x; int32_t y; } position; - KEYS key; - BUTTONS button; + struct { + int32_t x; + int32_t y; + } delta; + KEY key; + BUTTON button; } data; - EVENT_TYPES type; + EVENT_TYPE type; }; class window { @@ -161,13 +190,15 @@ class window { void poll_events(); -#if defined(WINDOWS) +#if _WIN32 void message_loop(); LRESULT message_handler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam); static LRESULT CALLBACK window_procedure(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam); + static void set_cursor(enum CURSOR_TYPE type); + HWND handle {}; const char* window_class_name; LPVOID main_fiber; @@ -190,8 +221,10 @@ class window { bool isOpen; - bool keyboard[KEYS::MAX_KEYS] { false }; - bool mouse[BUTTONS::MAX_BUTTONS] { false }; + bool keyboard[KEY::MAX_KEY] { false }; + bool mouse[BUTTON::MAX_BUTTON] { false }; + + static HCURSOR cursors[CURSOR_TYPE::MAX_CURSOR]; }; #endif // !__WINDOW_HPP_ -- cgit v1.3