diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 17 | ||||
| -rw-r--r-- | src/camera.cpp | 27 | ||||
| -rw-r--r-- | src/compute-renderpass.cpp | 6 | ||||
| -rw-r--r-- | src/gltf.cpp | 3 | ||||
| -rw-r--r-- | src/main.cpp | 1 | ||||
| -rw-r--r-- | src/primitive-renderpass.cpp | 2 | ||||
| -rw-r--r-- | src/utils.cpp | 5 | ||||
| -rw-r--r-- | src/vec3.cpp | 8 | ||||
| -rw-r--r-- | src/vk-api.cpp | 23 | ||||
| -rw-r--r-- | src/vk-context.cpp | 15 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 8 | ||||
| -rw-r--r-- | src/vulkan-loader.cpp | 4 |
12 files changed, 65 insertions, 54 deletions
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 <fstream> #include <cstring> +#include <filesystem> #define STB_IMAGE_IMPLEMENTATION #include <stb_image.h> @@ -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 <renderdoc.h> #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 <windows.h> #endif +#include <filesystem> #include <iostream> std::vector<uint8_t> 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 <cassert> -#include <memory> -#include "vk-api.hpp" -#include "vulkan-loader.hpp" +#include <vk_mem_alloc.h> + #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<VkSemaphore> 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 <iostream> #include <cassert> -#include "vulkan-loader.hpp" - #define VMA_IMPLEMENTATION #include <vk_mem_alloc.h> +#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<const char*>& needed uint32_t available_needed_layers_count = 0; - std::vector<const char*> 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<const char*>& av vkEnumerateInstanceExtensionProperties(VK_NULL_HANDLE, &vulkan_extensions_count, vulkan_extensions_properties.data()); uint32_t available_needed_extensions_count = 0; - - std::vector<const char*> 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 <cstring> #include <cassert> +#include <vk_mem_alloc.h> + +#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 <cassert> -#include "defines.hpp" - #if defined(WINDOWS) +#include <windows.h> + HMODULE vulkanLibrary = LoadLibrary("vulkan-1.dll"); #define LoadProcAddress GetProcAddress |
