From 23e6a58d59b7a11cb8bdb7aa61d3c534c02027e7 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 11 Jul 2022 00:50:27 +0200 Subject: Refactor includes & improve compile time Add platform defined to CMakeLists.txt --- CMakeLists.txt | 2 +- include/camera.hpp | 4 ++-- include/compute-renderpass.hpp | 5 +---- include/defines.hpp | 12 ------------ include/utils.hpp | 8 +++++++- include/vec3.hpp | 5 ++--- include/vk-api.hpp | 10 +++++++--- include/vk-context.hpp | 11 +++++------ include/vk-renderer.hpp | 1 + include/vulkan-loader.hpp | 11 ----------- include/window.hpp | 2 -- src/CMakeLists.txt | 17 ++++++++++++++--- src/camera.cpp | 27 +++++++++------------------ src/compute-renderpass.cpp | 6 ++++-- src/gltf.cpp | 3 ++- src/main.cpp | 1 - src/primitive-renderpass.cpp | 2 +- src/utils.cpp | 5 ++--- src/vec3.cpp | 8 ++++---- src/vk-api.cpp | 23 ++++++++++++++++------- src/vk-context.cpp | 15 +++++---------- src/vk-renderer.cpp | 8 ++++++-- src/vulkan-loader.cpp | 4 ++-- 23 files changed, 91 insertions(+), 99 deletions(-) delete mode 100644 include/defines.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c50d437..d9ddfe5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") add_compile_options( -msse4.1 -fcolor-diagnostics -fansi-escape-codes # colored output when using ninja + -ftime-trace # https://github.com/cpp-best-practices/cppbestpractices/blob/master/02-Use_the_Tools_Available.md#gcc--clang -Wall -Wextra @@ -21,7 +22,6 @@ add_compile_options( -Wnon-virtual-dtor -Wpedantic -Wunused - # -ftime-trace ) endif() diff --git a/include/camera.hpp b/include/camera.hpp index 60ca5b5..5c1db61 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -14,9 +14,9 @@ class camera { float focus_dist ); - void set_aspect_ratio(float aspect_ratio); + void set_aspect_ratio(float ratio); - void move(vec3 v); + void move(const vec3& v); void rotate_y(float theta); diff --git a/include/compute-renderpass.hpp b/include/compute-renderpass.hpp index 14c3147..60a1e05 100644 --- a/include/compute-renderpass.hpp +++ b/include/compute-renderpass.hpp @@ -9,10 +9,7 @@ class Buffer; class ComputeRenderpass : public Renderpass { public: - ComputeRenderpass(vkapi& api) - : Renderpass(api) - {} - + ComputeRenderpass(vkapi& api); ~ComputeRenderpass() override; void set_pipeline(const char* shader_name); diff --git a/include/defines.hpp b/include/defines.hpp deleted file mode 100644 index 2b56c7a..0000000 --- a/include/defines.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef __DEFINES_HPP_ -#define __DEFINES_HPP_ - -#if defined(__linux__) -#define LINUX -#elif defined(_WIN32) || defined(_WIN64) -#define WINDOWS -#elif defined(__APPLE__) -#define MACOS -#endif - -#endif // !__DEFINES_HPP_ diff --git a/include/utils.hpp b/include/utils.hpp index 6e39098..52f3297 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,8 +1,8 @@ #ifndef __UTILS_HPP_ #define __UTILS_HPP_ -#include #include +#include std::vector read_file(const char* path); std::vector read_file(std::string& path); @@ -10,6 +10,10 @@ std::vector read_file(std::string& path); #define PI 3.14159265359 #define EPSILON 0.000001 +namespace std::filesystem { + class path; +} + inline float deg_to_rad(float deg) { return deg * PI / 180.f; } @@ -32,4 +36,6 @@ inline float clamp(float x, float min, float max) { return x; } +void watch_file(std::filesystem::path&& filepath, std::function&& callback); + #endif // !__UTILS_HPP_ diff --git a/include/vec3.hpp b/include/vec3.hpp index 673bd83..e3be2a4 100644 --- a/include/vec3.hpp +++ b/include/vec3.hpp @@ -1,7 +1,6 @@ #ifndef __VEC3_HPP_ #define __VEC3_HPP_ -#include #include class vec3 { @@ -43,6 +42,8 @@ class vec3 { bool near_zero() const; + void print() const; + static vec3 random(); static vec3 random(float min, float max); @@ -68,8 +69,6 @@ vec3 reflect(const vec3& v, const vec3& n); vec3 refract(const vec3& uv, const vec3& n, float etai_over_etat); -std::ostream& operator<<(std::ostream& out, vec3 vec); - using point3 = vec3; using color = vec3; diff --git a/include/vk-api.hpp b/include/vk-api.hpp index c2ed9bd..2c00b40 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -4,11 +4,14 @@ #include #include -#include "vk-context.hpp" +#include + +using VmaAllocation = struct VmaAllocation_T*; +using VmaMemoryUsage = enum VmaMemoryUsage; struct buffer { VmaAllocation alloc; - VmaAllocationInfo alloc_info; + void* device_ptr; VkBuffer handle; size_t size; VkDeviceAddress device_address; @@ -27,7 +30,7 @@ struct image { VkImage handle; VkImageView view; VmaAllocation alloc; - VmaAllocationInfo alloc_info; + void* device_ptr; VkImageSubresourceRange subresource_range; VkFormat format; VkExtent3D size; @@ -102,6 +105,7 @@ struct global_descriptor { }; class window; +class vkcontext; class vkapi { public: diff --git a/include/vk-context.hpp b/include/vk-context.hpp index 29e17d2..7638d07 100644 --- a/include/vk-context.hpp +++ b/include/vk-context.hpp @@ -2,10 +2,9 @@ #define __VK_CONTEXT_HPP_ #include +#include -#include "vulkan-loader.hpp" - -#include +using VmaAllocator = struct VmaAllocator_T*; class vkcontext { public: @@ -36,11 +35,11 @@ class vkcontext { void select_queue(VkQueueFlags queueUsage); - void check_available_instance_layers(std::vector& needed_layers); + static void check_available_instance_layers(std::vector& needed_layers); - void check_available_instance_extensions(std::vector& available_layers, std::vector& needed_extensions); + static void check_available_instance_extensions(std::vector& available_layers, std::vector& needed_extensions); - bool support_required_features(VkPhysicalDevice physical_device); + static bool support_required_features(VkPhysicalDevice physical_device); VkDebugUtilsMessengerEXT debugMessenger; diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index b0a34f5..a6107cd 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -3,6 +3,7 @@ #include +#include "vk-context.hpp" #include "vk-api.hpp" diff --git a/include/vulkan-loader.hpp b/include/vulkan-loader.hpp index d8c1082..e15a447 100644 --- a/include/vulkan-loader.hpp +++ b/include/vulkan-loader.hpp @@ -1,17 +1,6 @@ #ifndef __VULKAN_LOADER_HPP_ #define __VULKAN_LOADER_HPP_ -#include "defines.hpp" - -#if defined(WINDOWS) -#define VK_USE_PLATFORM_WIN32_KHR -#elif defined(LINUX) -#define VK_USE_PLATFORM_XCB_KHR -#elif defined(MACOS) -#define VK_USE_PLATFORM_METAL_EXT -#endif - -#define VK_NO_PROTOTYPES #include #define VULKAN_EXPORTED_FUNCTIONS \ diff --git a/include/window.hpp b/include/window.hpp index cab215f..3f0fa01 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -3,8 +3,6 @@ #include -#include "defines.hpp" - class vkrenderer; #if defined(LINUX) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 86d91d5..c1f4843 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,16 @@ -add_definitions(-DNOMINMAX -D_USE_MATH_DEFINES) - set(LIBRARIES imgui::imgui nlohmann_json::nlohmann_json Tracy::TracyClient) +set(DEFINES -DNOMINMAX -D_USE_MATH_DEFINES -DVK_NO_PROTOTYPES) + +if(UNIX AND NOT APPLE) + set(DEFINES ${DEFINES} -DLINUX -DVK_USE_PLATFORM_XCB_KHR) +elseif(WIN32) + set(DEFINES ${DEFINES} -DWINDOWS -DVK_USE_PLATFORM_WIN32_KHR) +elseif(APPLE) + set(DEFINES ${DEFINES} -DMACOS -DVK_USE_PLATFORM_METAL_EXT) +endif() + + add_executable( path-tracer main.cpp @@ -18,9 +27,11 @@ add_executable( gltf.cpp compute-renderpass.cpp primitive-renderpass.cpp - ${Tracy_SOURCE_DIR}/TracyClient.cpp + # ${Tracy_SOURCE_DIR}/TracyClient.cpp ) +target_compile_definitions(path-tracer PUBLIC ${DEFINES}) + target_include_directories(path-tracer SYSTEM PUBLIC ${CMAKE_SOURCE_DIR}/thirdparty) target_include_directories(path-tracer PUBLIC ${CMAKE_SOURCE_DIR}/include) diff --git a/src/camera.cpp b/src/camera.cpp index 908ba09..e9e5f60 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -10,27 +10,20 @@ camera::camera( float aspect_ratio, float aperture, float focus_dist - ) : position(position), lens_radius(aperture / 2.0), fov(v_fov), focus_distance(focus_dist), aspect_ratio(aspect_ratio) { + ) : position(position), lens_radius(aperture / 2.f), fov(v_fov), focus_distance(focus_dist) { forward = (target - position).normalize(); right = forward.cross(vec3{ 0.0, 1.0, 0.0 }).normalize(); up = right.cross(forward).normalize(); - auto h = tan(deg_to_rad(fov) / 2.0); - auto viewport_height = 2.0 * h; - auto viewport_width = viewport_height * aspect_ratio; - - horizontal = focus_dist * viewport_width * right; - vertical = focus_dist * viewport_height * up; - - first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_distance * forward; + set_aspect_ratio(aspect_ratio); } -void camera::set_aspect_ratio(float aspect_ratio) { - aspect_ratio = aspect_ratio; +void camera::set_aspect_ratio(float ratio) { + aspect_ratio = ratio; - auto h = tan(deg_to_rad(fov) / 2.0); - auto viewport_height = 2.0 * h; + auto h = tan(deg_to_rad(fov) / 2.f); + auto viewport_height = 2.f * h; auto viewport_width = viewport_height * aspect_ratio; horizontal = focus_distance * viewport_width * right; @@ -39,7 +32,7 @@ void camera::set_aspect_ratio(float aspect_ratio) { first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_distance * forward; } -void camera::move(vec3 v) { +void camera::move(const vec3& v) { position += v; first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_distance * forward; } @@ -52,11 +45,9 @@ void camera::rotate_y(float theta) { right = forward.cross(vec3{ 0.0, 1.0, 0.0 }).normalize(); up = right.cross(forward).normalize(); - auto h = tan(deg_to_rad(fov) / 2.0); - auto viewport_height = 2.0 * h; - auto viewport_width = viewport_height * aspect_ratio; + auto h = tan(deg_to_rad(fov) / 2.f); + auto viewport_height = 2.f * h; - horizontal = focus_distance * viewport_width * right; vertical = focus_distance * viewport_height * up; first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_distance * forward; } diff --git a/src/compute-renderpass.cpp b/src/compute-renderpass.cpp index 99ee4a9..b7af1ba 100644 --- a/src/compute-renderpass.cpp +++ b/src/compute-renderpass.cpp @@ -1,9 +1,11 @@ #include "compute-renderpass.hpp" -#include "vk-api.hpp" - #include "vk-renderer.hpp" +ComputeRenderpass::ComputeRenderpass(vkapi& api) + : Renderpass(api) +{} + ComputeRenderpass::~ComputeRenderpass() { api.destroy_pipeline(pipeline); } diff --git a/src/gltf.cpp b/src/gltf.cpp index 282b4a4..a044842 100644 --- a/src/gltf.cpp +++ b/src/gltf.cpp @@ -2,6 +2,7 @@ #include #include +#include #define STB_IMAGE_IMPLEMENTATION #include @@ -10,7 +11,7 @@ #include "utils.hpp" node gltf::load(std::filesystem::path &path, std::string &filename, vkrenderer &renderer) { - std::fstream f{path / filename}; + std::fstream f { path / filename }; json gltf_json; f >> gltf_json; diff --git a/src/main.cpp b/src/main.cpp index f61e0f7..045d1b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ #include #endif -#include "defines.hpp" #include "compute-renderpass.hpp" #include "primitive-renderpass.hpp" #include "scene.hpp" diff --git a/src/primitive-renderpass.cpp b/src/primitive-renderpass.cpp index 0e43598..56b4771 100644 --- a/src/primitive-renderpass.cpp +++ b/src/primitive-renderpass.cpp @@ -1,6 +1,6 @@ #include "primitive-renderpass.hpp" -#include "vk-renderer.hpp" +#include "vulkan-loader.hpp" void Primitive::set_pipeline(const char* shader_name) { diff --git a/src/utils.cpp b/src/utils.cpp index ff0619b..48e9a5f 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,11 +1,10 @@ #include "utils.hpp" -#include "defines.hpp" - -#ifdef WINDOWS +#if defined(WINDOWS) #include #endif +#include #include std::vector read_file(const char* path) { diff --git a/src/vec3.cpp b/src/vec3.cpp index 5cf04f0..096dde0 100644 --- a/src/vec3.cpp +++ b/src/vec3.cpp @@ -92,6 +92,10 @@ bool vec3::near_zero() const { return (fabs(v[0]) < EPSILON) && (fabs(v[1]) < EPSILON) && (fabs(v[2]) < EPSILON); } +void vec3::print() const { + printf("%f %f %f\n", v[0], v[1], v[2]); +} + vec3 vec3::random() { return vec3{randd(), randd(), randd()}; } vec3 vec3::random(float min, float max) { return vec3{randd(min, max), randd(min, max), randd(min, max)}; } @@ -132,7 +136,3 @@ vec3 refract(const vec3 &uv, const vec3 &n, float etai_over_etat) { return r_out_perp + r_out_parallel; } - -std::ostream &operator<<(std::ostream &out, vec3 vec) { - return out << vec[0] << ' ' << vec[1] << ' ' << vec[2] << std::endl; -} diff --git a/src/vk-api.cpp b/src/vk-api.cpp index de23b65..2ae57e2 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -1,10 +1,13 @@ +#include "vk-api.hpp" + #include -#include -#include "vk-api.hpp" -#include "vulkan-loader.hpp" +#include + #include "window.hpp" #include "utils.hpp" +#include "vk-context.hpp" +#include "vulkan-loader.hpp" #ifdef _DEBUG #define VKRESULT(result) assert(result == VK_SUCCESS); @@ -171,12 +174,14 @@ handle vkapi::create_buffer(size_t data_size, VkBufferUsageFlags buffer_usage, V buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; buffer_info.size = data_size; buffer_info.usage = buffer_usage | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR; - + VmaAllocationCreateInfo alloc_create_info = {}; alloc_create_info.usage = mem_usage; alloc_create_info.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; - VKRESULT(vmaCreateBuffer(context.allocator, &buffer_info, &alloc_create_info, &buffer->handle, &buffer->alloc, &buffer->alloc_info)) + VmaAllocationInfo alloc_info {}; + + VKRESULT(vmaCreateBuffer(context.allocator, &buffer_info, &alloc_create_info, &buffer->handle, &buffer->alloc, &alloc_info)) VkBufferDeviceAddressInfo buffer_device_address_info = {}; buffer_device_address_info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO; @@ -186,6 +191,7 @@ handle vkapi::create_buffer(size_t data_size, VkBufferUsageFlags buffer_usage, V buffer->size = data_size; buffer->device_address = vkGetBufferDeviceAddress(context.device, &buffer_device_address_info); + buffer->device_ptr = alloc_info.pMappedData; buffers.push_back(buffer); @@ -260,7 +266,9 @@ handle vkapi::create_image(VkExtent3D size, VkFormat format, VkImageUsageFlags u VmaAllocationCreateInfo alloc_create_info = {}; alloc_create_info.usage = VMA_MEMORY_USAGE_GPU_ONLY; - VKRESULT(vmaCreateImage(context.allocator, &img_create_info, &alloc_create_info, &image->handle, &image->alloc, &image->alloc_info)) + VmaAllocationInfo alloc_info {}; + + VKRESULT(vmaCreateImage(context.allocator, &img_create_info, &alloc_create_info, &image->handle, &image->alloc, &alloc_info)) VkImageViewCreateInfo image_view_create_info = {}; image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; @@ -280,6 +288,7 @@ handle vkapi::create_image(VkExtent3D size, VkFormat format, VkImageUsageFlags u VKRESULT(vkCreateImageView(context.device, &image_view_create_info, VK_NULL_HANDLE, &image->view)) image->usages = usages; + image->device_ptr = alloc_info.pMappedData; images.push_back(image); @@ -387,7 +396,7 @@ std::vector vkapi::create_semaphores(size_t semaphores_count) const } void vkapi::destroy_semaphores(VkSemaphore semaphores[], size_t semaphores_count) const { - for(auto index { 0 }; index < semaphores_count; index++) { + for(size_t index { 0 }; index < semaphores_count; index++) { vkDestroySemaphore(context.device, semaphores[index], nullptr); } } diff --git a/src/vk-context.cpp b/src/vk-context.cpp index 0a5e391..3f7f6e0 100644 --- a/src/vk-context.cpp +++ b/src/vk-context.cpp @@ -3,11 +3,11 @@ #include #include -#include "vulkan-loader.hpp" - #define VMA_IMPLEMENTATION #include +#include "vulkan-loader.hpp" + #ifdef _DEBUG #define VKRESULT(result) assert(result == VK_SUCCESS); #else @@ -218,11 +218,9 @@ void vkcontext::check_available_instance_layers(std::vector& needed uint32_t available_needed_layers_count = 0; - std::vector available_layers; for (auto& layer_property: layer_properties) { for (auto& needed_layer: needed_layers) { if (strcmp(layer_property.layerName, needed_layer) == 0) { - available_layers.push_back(needed_layer); available_needed_layers_count++; } } @@ -239,12 +237,9 @@ void vkcontext::check_available_instance_extensions(std::vector& av vkEnumerateInstanceExtensionProperties(VK_NULL_HANDLE, &vulkan_extensions_count, vulkan_extensions_properties.data()); uint32_t available_needed_extensions_count = 0; - - std::vector available_extensions; for (auto& extension: vulkan_extensions_properties) { for (auto& needed_extension: needed_extensions) { if (strcmp(extension.extensionName, needed_extension) == 0) { - available_extensions.push_back(needed_extension); available_needed_extensions_count++; } } @@ -277,9 +272,9 @@ bool vkcontext::support_required_features(VkPhysicalDevice physical_device) { vulkan_12_features.imagelessFramebuffer == VK_TRUE) { std::cerr << "Using physical device " << physical_device_properties.deviceName << std::endl; return true; - } else { - std::cerr << "Physical device " << physical_device_properties.deviceName << " is missing some features" << std::endl; - return false; } + + std::cerr << "Physical device " << physical_device_properties.deviceName << " is missing some features" << std::endl; + return false; } diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index cbdd09f..e00e5f7 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -3,6 +3,10 @@ #include #include +#include + +#include "vulkan-loader.hpp" + #include "compute-renderpass.hpp" #include "primitive-renderpass.hpp" @@ -132,7 +136,7 @@ void vkrenderer::update_image(Texture* texture, void* data) { staging_buffer = api.create_buffer(max_texture_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY); } - std::memcpy(buffer.alloc_info.pMappedData, data, max_texture_size); + std::memcpy(buffer.device_ptr, data, max_texture_size); VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) VKRESULT(vkResetFences(context.device, 1, &submission_fences[virtual_frame_index])) @@ -159,7 +163,7 @@ void vkrenderer::update_buffer(Buffer* buffer, void* data, off_t offset, size_t size_t dynamic_offset = buffer->isStatic ? 0 : buffer->size * virtual_frame_index; - std::memcpy((uint8_t*)device_buffer.alloc_info.pMappedData + dynamic_offset + offset, data, size); + std::memcpy((uint8_t*)device_buffer.device_ptr + dynamic_offset + offset, data, size); } diff --git a/src/vulkan-loader.cpp b/src/vulkan-loader.cpp index 0977fe8..933b8f1 100644 --- a/src/vulkan-loader.cpp +++ b/src/vulkan-loader.cpp @@ -2,9 +2,9 @@ #include -#include "defines.hpp" - #if defined(WINDOWS) +#include + HMODULE vulkanLibrary = LoadLibrary("vulkan-1.dll"); #define LoadProcAddress GetProcAddress -- cgit v1.3