diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2022-07-09 23:45:26 +0200 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2022-07-10 00:04:17 +0200 |
| commit | cfcb5c3212f0fce667885ff0de6fa0b9fc4b3ca8 (patch) | |
| tree | d076b243e0203537d5816c9a15010723581d7f97 | |
| parent | 928b1b0503d2ecab09cc275e9775593a41e417cb (diff) | |
Improve compilation time
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | include/aabb.hpp | 5 | ||||
| -rw-r--r-- | include/bvh-node.hpp | 3 | ||||
| -rw-r--r-- | include/color.hpp | 2 | ||||
| -rw-r--r-- | include/compute-renderpass.hpp | 12 | ||||
| -rw-r--r-- | include/gltf.hpp | 8 | ||||
| -rw-r--r-- | include/primitive-renderpass.hpp | 12 | ||||
| -rw-r--r-- | include/renderpass.hpp | 7 | ||||
| -rw-r--r-- | include/scene.hpp | 9 | ||||
| -rw-r--r-- | include/utils.hpp | 7 | ||||
| -rw-r--r-- | include/vec3.hpp | 2 | ||||
| -rw-r--r-- | include/vk-api.hpp | 45 | ||||
| -rw-r--r-- | include/vk-renderer.hpp | 22 | ||||
| -rw-r--r-- | include/vulkan-loader.hpp | 7 | ||||
| -rw-r--r-- | include/window.hpp | 1 | ||||
| -rw-r--r-- | src/color.cpp | 2 | ||||
| -rw-r--r-- | src/compute-renderpass.cpp | 18 | ||||
| -rw-r--r-- | src/gltf.cpp | 32 | ||||
| -rw-r--r-- | src/main.cpp | 14 | ||||
| -rw-r--r-- | src/primitive-renderpass.cpp | 14 | ||||
| -rw-r--r-- | src/scene.cpp | 2 | ||||
| -rw-r--r-- | src/utils.cpp | 8 | ||||
| -rw-r--r-- | src/vk-api.cpp | 62 | ||||
| -rw-r--r-- | src/vk-context.cpp | 5 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 82 | ||||
| -rw-r--r-- | src/vulkan-loader.cpp | 54 |
26 files changed, 214 insertions, 223 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d884e9d..ccc798f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file") +set(CMAKE_BUILD_TYPE Debug) project(path-tracer VERSION 1.0 LANGUAGES CXX) @@ -20,6 +21,7 @@ add_compile_options( -Wnon-virtual-dtor -Wpedantic -Wunused + # -ftime-trace ) endif() diff --git a/include/aabb.hpp b/include/aabb.hpp index 85b3c21..dcab8c9 100644 --- a/include/aabb.hpp +++ b/include/aabb.hpp @@ -1,11 +1,12 @@ #ifndef __AABB_HPP_
#define __AABB_HPP_
-#include "vec3.hpp"
-#include "triangle.hpp"
#include <cmath>
#include <climits>
+#include "vec3.hpp"
+#include "triangle.hpp"
+
class aabb {
public:
aabb() = default;
diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp index 99b6d62..525e1c8 100644 --- a/include/bvh-node.hpp +++ b/include/bvh-node.hpp @@ -3,9 +3,10 @@ #include <vector>
#include <algorithm>
+#include <iostream>
#include "aabb.hpp"
-#include "sphere.hpp"
+// #include "sphere.hpp"
#include "triangle.hpp"
struct bvh_node {
diff --git a/include/color.hpp b/include/color.hpp index f300e76..312f70c 100644 --- a/include/color.hpp +++ b/include/color.hpp @@ -3,7 +3,7 @@ #include <iostream> -#include "vec3.hpp" +using color = class vec3; void write_color(std::ostream& out, const color& pixel_color); diff --git a/include/compute-renderpass.hpp b/include/compute-renderpass.hpp index 641d4b1..14c3147 100644 --- a/include/compute-renderpass.hpp +++ b/include/compute-renderpass.hpp @@ -2,7 +2,6 @@ #include "renderpass.hpp" #include "vk-api.hpp" -#include <array> class Texture; class Buffer; @@ -14,12 +13,9 @@ class ComputeRenderpass : public Renderpass { : Renderpass(api) {} - ~ComputeRenderpass() { - api.destroy_pipeline(pipeline); - } + ~ComputeRenderpass() override; - - void set_pipeline(std::string& shader_name); + void set_pipeline(const char* shader_name); void set_dispatch_size(size_t count_x, size_t count_y, size_t count_z); @@ -30,7 +26,7 @@ class ComputeRenderpass : public Renderpass { void set_constant(off_t offset, Buffer* buffer); // TODO: Do not use vulkan api type - void execute(vkrenderer& renderer, VkCommandBuffer command_buffer) override final; + void execute(vkrenderer& renderer, VkCommandBuffer command_buffer) final; Texture* output_texture; @@ -38,7 +34,7 @@ class ComputeRenderpass : public Renderpass { std::vector<Texture*> input_textures; - std::array<uint8_t, 64> constants; + uint8_t constants[64]; size_t group_count_x; size_t group_count_y; diff --git a/include/gltf.hpp b/include/gltf.hpp index 0364ca8..66fb6ec 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -1,7 +1,6 @@ #ifndef __GLTF_HPP_
#define __GLTF_HPP_
-#include <filesystem>
#include <vector>
#include <optional>
@@ -11,8 +10,6 @@ using json = nlohmann::json; #include "material.hpp"
-#include "vk-renderer.hpp"
-
struct mesh_part {
std::vector<uint16_t> indices;
std::vector<float> positions;
@@ -34,6 +31,11 @@ struct node { std::optional<mesh> mesh;
};
+namespace std::filesystem {
+ class path;
+}
+
+class vkrenderer;
class gltf {
public:
static node load(std::filesystem::path &path, std::string &filename, vkrenderer &renderer);
diff --git a/include/primitive-renderpass.hpp b/include/primitive-renderpass.hpp index 45b43e9..2e0a77b 100644 --- a/include/primitive-renderpass.hpp +++ b/include/primitive-renderpass.hpp @@ -1,11 +1,9 @@ #pragma once -#include <stdint.h> #include <vector> -#include <array> -#include "vk-renderer.hpp" #include "renderpass.hpp" +#include "vk-renderer.hpp" class Buffer; class Texture; @@ -29,7 +27,7 @@ class PrimitiveRenderpass : public Renderpass { void finalize_render_pass(); - void execute(vkrenderer& renderer, VkCommandBuffer command_buffer) override final; + void execute(vkrenderer& renderer, VkCommandBuffer command_buffer) final; VkRenderPass render_pass = VK_NULL_HANDLE; @@ -45,7 +43,7 @@ class PrimitiveRenderpass : public Renderpass { std::vector<VkImageView> color_attachments_view; std::vector<VkFormat> color_attachments_format; - std::array<framebuffer, vkrenderer::virtual_frames_count> framebuffers; + framebuffer framebuffers[vkrenderer::virtual_frames_count]; std::vector<Primitive*> primitives; }; @@ -58,7 +56,7 @@ class Primitive { : api(api), primitive_render_pass(render_pass) {}; - void set_pipeline(std::string& shader_name); + void set_pipeline(const char* shader_name); void set_constant_offset(off_t offset); void set_constant(off_t offset, uint64_t* constant); @@ -93,5 +91,5 @@ private: VkRect2D scissor = {}; - std::array<uint8_t, 64> constants; + uint8_t constants[64]; }; diff --git a/include/renderpass.hpp b/include/renderpass.hpp index a444c64..da116dd 100644 --- a/include/renderpass.hpp +++ b/include/renderpass.hpp @@ -1,8 +1,9 @@ #pragma once -#include <string> +using VkCommandBuffer = struct VkCommandBuffer_T*; -#include "vk-renderer.hpp" +class vkrenderer; +class vkapi; class Renderpass { public: @@ -11,7 +12,7 @@ class Renderpass { :api(api) {}; - virtual ~Renderpass() {}; + virtual ~Renderpass() = default; // TODO: Do not use vulkan api type virtual void execute(vkrenderer& renderer, VkCommandBuffer command_buffer) = 0; diff --git a/include/scene.hpp b/include/scene.hpp index 8e47ab0..cabd10e 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -3,9 +3,10 @@ #include "camera.hpp"
#include "bvh-node.hpp"
+#include "material.hpp"
-#include "vk-renderer.hpp"
-
+class vkrenderer;
+class Buffer;
class scene {
struct metadata {
camera cam;
@@ -18,8 +19,8 @@ class scene { uint32_t sample_index = 0;
- uint32_t enable_dof = false;
- uint32_t debug_bvh = false;
+ uint32_t enable_dof = (uint32_t)false;
+ uint32_t debug_bvh = (uint32_t)false;
int32_t downscale_factor = 1;
metadata(camera cam, uint32_t width, uint32_t height)
diff --git a/include/utils.hpp b/include/utils.hpp index 201976f..6e39098 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,8 +1,7 @@ #ifndef __UTILS_HPP_ #define __UTILS_HPP_ -#include <string> -#include <filesystem> +#include <thread> #include <vector> std::vector<uint8_t> read_file(const char* path); @@ -16,7 +15,7 @@ inline float deg_to_rad(float deg) { } inline float randd() { - return rand() / (RAND_MAX + 1.f); + return (float)rand() / (RAND_MAX + 1.f); } inline float randd(float min, float max) { @@ -24,7 +23,7 @@ inline float randd(float min, float max) { } inline int32_t randi(int32_t min, int32_t max) { - return (int32_t)(randd(min, max + 1.f)); + return (int32_t)(randd((float)min, (float)max + 1.f)); } inline float clamp(float x, float min, float max) { diff --git a/include/vec3.hpp b/include/vec3.hpp index 0250345..673bd83 100644 --- a/include/vec3.hpp +++ b/include/vec3.hpp @@ -1,7 +1,7 @@ #ifndef __VEC3_HPP_ #define __VEC3_HPP_ -#include <iostream> +#include <ostream> #include <xmmintrin.h> class vec3 { diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 0f1431a..c2ed9bd 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -1,10 +1,8 @@ #ifndef __VK_API_HPP_ #define __VK_API_HPP_ -#include <stdint.h> #include <vector> -#include <map> -#include <iostream> +#include <cassert> #include "vk-context.hpp" @@ -18,7 +16,7 @@ struct buffer { struct pipeline { std::vector<VkShaderModule> shader_modules; - VkPipeline handle; + VkPipeline handle = VK_NULL_HANDLE; VkPipelineBindPoint bind_point; }; @@ -34,8 +32,8 @@ struct image { VkFormat format; VkExtent3D size; VkImageUsageFlags usages; - bindless_index bindless_storage_index; - bindless_index bindless_sampled_index; + bindless_index bindless_storage_index = 0; + bindless_index bindless_sampled_index = 0; VkImageLayout previous_layout = VK_IMAGE_LAYOUT_UNDEFINED; VkPipelineStageFlags previous_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; @@ -68,7 +66,7 @@ struct global_descriptor { std::vector<uint32_t> index_pool[3]; global_descriptor() { - for (int32_t index = 1024; index >= 0; index--) { + for (int32_t index = 1024; index > 0; index--) { index_pool[0].push_back((uint32_t)index); index_pool[1].push_back((uint32_t)index); index_pool[2].push_back((uint32_t)index); @@ -84,10 +82,11 @@ struct global_descriptor { } void free(uint32_t index, VkDescriptorType type) { + assert(index != 0); index_pool[descriptor_type_binding(type)].push_back(index); } - int32_t descriptor_type_binding(VkDescriptorType type) { + static int32_t descriptor_type_binding(VkDescriptorType type) { switch(type) { case VK_DESCRIPTOR_TYPE_SAMPLER: return 0; @@ -96,7 +95,7 @@ struct global_descriptor { case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return 2; default: - std::cerr << "descriptor type not supported" << std::endl; + assert(true && "descriptor type not supported"); return -1; } } @@ -106,7 +105,7 @@ class window; class vkapi { public: - vkapi(); + vkapi(vkcontext& context); ~vkapi(); handle create_buffer(size_t data_size, VkBufferUsageFlags buffer_usage, VmaMemoryUsage mem_usage); @@ -121,16 +120,16 @@ class vkapi { void destroy_images(std::vector<handle>& images); - VkFence create_fence(); - void destroy_fence(VkFence fence); - std::vector<VkFence> create_fences(size_t fences_count); - void destroy_fences(std::vector<VkFence> &fences); + [[nodiscard]] VkFence create_fence() const; + void destroy_fence(VkFence fence) const; + [[nodiscard]] std::vector<VkFence> create_fences(size_t fences_count) const; + void destroy_fences(VkFence fences[], size_t fences_count) const; - VkSemaphore create_semaphore(); - void destroy_semaphore(VkSemaphore semaphore); - std::vector<VkSemaphore> create_semaphores(size_t semaphores_count); - void destroy_semaphores(std::vector<VkSemaphore> &semaphores); + [[nodiscard]] VkSemaphore create_semaphore() const; + void destroy_semaphore(VkSemaphore semaphore) const; + [[nodiscard]] std::vector<VkSemaphore> create_semaphores(size_t semaphores_count) const; + void destroy_semaphores(VkSemaphore semaphores[], size_t semaphores_count) const; handle create_sampler(VkFilter filter, VkSamplerAddressMode address_mode); void destroy_sampler(handle sampler); @@ -152,7 +151,7 @@ class vkapi { pipeline create_compute_pipeline(const char* shader_name); - pipeline create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState> dynamic_states); + pipeline create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState>& dynamic_states); void destroy_pipeline(pipeline &pipeline); @@ -191,21 +190,21 @@ class vkapi { void end_record(VkCommandBuffer command_buffer); - VkResult submit(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence); + VkResult submit(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence) const; - VkResult present(swapchain& swapchain, uint32_t image_index, VkSemaphore wait_semaphore); + VkResult present(swapchain& swapchain, uint32_t image_index, VkSemaphore wait_semaphore) const; const buffer& get_buffer(handle buffer_handle) { return *buffers[buffer_handle]; }; const image& get_image(handle image_handle) { return *images[image_handle]; }; const sampler& get_sampler(handle sampler_handle) { return *samplers[sampler_handle]; }; - vkcontext context; + vkcontext& context; private: global_descriptor bindless_descriptor; - const char* shader_stage_extension(VkShaderStageFlags shader_stage); + static const char* shader_stage_extension(VkShaderStageFlags shader_stage); // TODO: Use freelists instead std::vector<buffer*> buffers; diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 864bc48..a301a5b 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -1,8 +1,6 @@ #ifndef __VK_RENDERER_HPP_ #define __VK_RENDERER_HPP_ -#include <stdint.h> -#include <sys/types.h> #include <vector> #include "vk-api.hpp" @@ -26,8 +24,6 @@ class vkrenderer { void begin_frame(); - void compute(uint32_t width, uint32_t height); - void finish_frame(); void recreate_swapchain(); @@ -52,19 +48,23 @@ class vkrenderer { void render(); - uint32_t frame_index() { return this->virtual_frame_index; } + [[nodiscard]]uint32_t frame_index() const { return this->virtual_frame_index; } - Texture* back_buffer() { return swapchain_textures[swapchain_image_index]; } + [[nodiscard]]Texture* back_buffer() const { return swapchain_textures[swapchain_image_index]; } - VkFormat back_buffer_format() { return swapchain.surface_format.format; } + [[nodiscard]]VkFormat back_buffer_format() const { return swapchain.surface_format.format; } static constexpr uint32_t virtual_frames_count = 2; + vkcontext context; + + vkapi api; + private: void handle_swapchain_result(VkResult function_result); - vkapi api; + // vkapi api; std::vector<Renderpass*> renderpasses; @@ -78,9 +78,9 @@ class vkrenderer { pipeline tonemapping_pipeline; - std::vector<VkFence> submission_fences; - std::vector<VkSemaphore> execution_semaphores; - std::vector<VkSemaphore> acquire_semaphores; + VkFence submission_fences[virtual_frames_count]; + VkSemaphore execution_semaphores[virtual_frames_count]; + VkSemaphore acquire_semaphores[virtual_frames_count]; VkSurfaceKHR platform_surface; swapchain swapchain; diff --git a/include/vulkan-loader.hpp b/include/vulkan-loader.hpp index 16df7a6..d8c1082 100644 --- a/include/vulkan-loader.hpp +++ b/include/vulkan-loader.hpp @@ -164,10 +164,11 @@ VULKAN_DEVICE_FUNCTIONS #undef X - void load_vulkan(); - void load_instance_functions(VkInstance instance); +void load_vulkan(); - void load_device_functions(VkDevice device); +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 07753df..cab215f 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -1,7 +1,6 @@ #ifndef __WINDOW_HPP_ #define __WINDOW_HPP_ -#include <cstdint> #include <vector> #include "defines.hpp" diff --git a/src/color.cpp b/src/color.cpp index 555ff8d..6ecbd5f 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -1,5 +1,7 @@ #include "color.hpp" + #include "utils.hpp" +#include "vec3.hpp" void write_color(std::ostream& out, const color& pixel_color) { auto r = pixel_color.v[0]; diff --git a/src/compute-renderpass.cpp b/src/compute-renderpass.cpp index 7399ae3..99ee4a9 100644 --- a/src/compute-renderpass.cpp +++ b/src/compute-renderpass.cpp @@ -4,8 +4,14 @@ #include "vk-renderer.hpp" -void ComputeRenderpass::set_pipeline(std::string& shader_name) { - this->pipeline = api.create_compute_pipeline(shader_name.c_str()); +ComputeRenderpass::~ComputeRenderpass() { + api.destroy_pipeline(pipeline); +} + +void ComputeRenderpass::set_pipeline(const char* shader_name) { + if (this->pipeline.handle != VK_NULL_HANDLE) + api.destroy_pipeline(this->pipeline); + this->pipeline = api.create_compute_pipeline(shader_name); } void ComputeRenderpass::set_ouput_texture(Texture* output_texture) { @@ -14,7 +20,7 @@ void ComputeRenderpass::set_ouput_texture(Texture* output_texture) { auto image = api.get_image(output_texture->device_image); // TODO: Remove hardcoded offset - memcpy(constants.data() + 56, &image.bindless_storage_index, sizeof(bindless_index)); + memcpy(constants + 56, &image.bindless_storage_index, sizeof(bindless_index)); } void ComputeRenderpass::set_dispatch_size(size_t count_x, size_t count_y, size_t count_z) { @@ -24,20 +30,20 @@ void ComputeRenderpass::set_dispatch_size(size_t count_x, size_t count_y, size_t } void ComputeRenderpass::set_constant(off_t offset, uint64_t* constant) { - memcpy(constants.data() + offset, constant, sizeof(uint64_t)); + memcpy(constants + offset, constant, sizeof(uint64_t)); } void ComputeRenderpass::set_constant(off_t offset, Texture* texture) { auto image = api.get_image(texture->device_image); - memcpy(constants.data() + offset, (void*)&image.bindless_storage_index, sizeof(bindless_index)); + memcpy(constants + offset, (void*)&image.bindless_storage_index, sizeof(bindless_index)); input_textures.push_back(texture); } void ComputeRenderpass::set_constant(off_t offset, Buffer* buffer) { auto device_buffer = api.get_buffer(buffer->device_buffer); - memcpy(constants.data() + offset, (void*)&device_buffer.device_address, sizeof(VkDeviceAddress)); + memcpy(constants + offset, (void*)&device_buffer.device_address, sizeof(VkDeviceAddress)); } void ComputeRenderpass::execute(vkrenderer& renderer, VkCommandBuffer command_buffer) { diff --git a/src/gltf.cpp b/src/gltf.cpp index 982e76d..c45ba99 100644 --- a/src/gltf.cpp +++ b/src/gltf.cpp @@ -1,4 +1,3 @@ - #include "gltf.hpp" #include <fstream> @@ -7,23 +6,23 @@ #define STB_IMAGE_IMPLEMENTATION #include "thirdparty/stb_image.h" +#include "vk-renderer.hpp" #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; - auto &scene = gltf_json["scenes"][0]; + auto& scene = gltf_json["scenes"][0]; auto &buffers = gltf_json["buffers"]; - auto buffer_index = 0; auto buffers_content = std::vector<std::vector<uint8_t>>(buffers.size()); - for (auto &buffer : buffers) { + for (size_t buffer_index{0}; buffer_index < buffers.size(); buffer_index++) { + auto& buffer = buffers[buffer_index]; auto buffer_path = (path / buffer["uri"].get<std::string>()).string(); buffers_content[buffer_index] = read_file(buffer_path); - buffer_index++; } auto textures = load_textures(gltf_json, path, renderer); @@ -32,7 +31,7 @@ node gltf::load(std::filesystem::path &path, std::string &filename, vkrenderer & auto meshes = load_meshes(gltf_json, buffers_content, materials); node root_node; - auto &root_children = scene["nodes"]; + auto& root_children = scene["nodes"]; uint32_t root_children_count = scene["nodes"].size(); root_node.children.resize(root_children_count); @@ -91,8 +90,7 @@ std::vector<texture> gltf::load_textures(json &gltf_json, std::filesystem::path int x, y, n; auto filepath = (path / gltf_image["uri"].get<std::string>()).string(); unsigned char *data = stbi_load(filepath.c_str(), &x, &y, &n, 4); - auto *img = - renderer.create_2d_texture(static_cast<uint32_t>(x), static_cast<uint32_t>(y), VK_FORMAT_R8G8B8A8_UNORM); + auto *img = renderer.create_2d_texture(static_cast<uint32_t>(x), static_cast<uint32_t>(y), VK_FORMAT_R8G8B8A8_UNORM); renderer.update_image(img, data); @@ -109,6 +107,7 @@ std::vector<texture> gltf::load_textures(json &gltf_json, std::filesystem::path samplers[sampler_index] = renderer.create_sampler(VK_FILTER_LINEAR, VK_SAMPLER_ADDRESS_MODE_REPEAT); } + auto& api = renderer.api; auto &gltf_textures = gltf_json["textures"]; auto textures_count = gltf_textures.size(); std::vector<texture> textures{textures_count}; @@ -119,15 +118,16 @@ std::vector<texture> gltf::load_textures(json &gltf_json, std::filesystem::path auto image_index = gltf_texture["source"].get<uint32_t>(); auto &image = images[image_index]; + uint32_t image_id = api.get_image(image->device_image).bindless_sampled_index; uint32_t sampler_id = 0; - // if (gltf_texture.contains("sampler")) { - // auto sampler_index = gltf_texture["sampler"].get<uint32_t>(); - // auto &sampler = samplers[sampler_index]; - // sampler_id = sampler->device_sampler.bindless_index; - // } + if (gltf_texture.contains("sampler")) { + auto sampler_index = gltf_texture["sampler"].get<uint32_t>(); + auto &sampler = samplers[sampler_index]; + sampler_id = api.get_sampler(sampler->device_sampler).bindless_index; + } - // textures[texture_index] = - // texture{.image_id = image->device_image.bindless_sampled_index, .sampler_id = sampler_id}; + textures[texture_index] = + texture{.image_id = image_id, .sampler_id = sampler_id}; } return textures; diff --git a/src/main.cpp b/src/main.cpp index 2213d70..f155a92 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,15 @@ -#include <cassert> #include <chrono> -#include <cstdio> -#include <cstring> -#include <iostream> +#include <filesystem> #include "imgui.h" + +#ifdef ENABLE_RENDERDOC #include "thirdparty/renderdoc.h" +#endif +#include "defines.hpp" #include "compute-renderpass.hpp" #include "primitive-renderpass.hpp" -#include "defines.hpp" #include "scene.hpp" #include "vk-renderer.hpp" #include "window.hpp" @@ -29,6 +29,7 @@ struct VkVertex { }; int main() { +#ifdef ENABLE_RENDERDOC RENDERDOC_API_1_1_2 *rdoc_api = nullptr; #ifdef WINDOWS @@ -49,6 +50,7 @@ int main() { assert(ret == 1); } #endif +#endif // ENABLE_RENDERDOC // Image dimensions const float aspect_ratio = 16.0 / 9.0; @@ -427,7 +429,7 @@ int main() { // write_color(std::cout, output_image[index]); // } - std::cerr << "Done !" << std::endl; + // std::cerr << "Done !" << std::endl; return 0; } diff --git a/src/primitive-renderpass.cpp b/src/primitive-renderpass.cpp index f8521e3..0e43598 100644 --- a/src/primitive-renderpass.cpp +++ b/src/primitive-renderpass.cpp @@ -1,17 +1,15 @@ #include "primitive-renderpass.hpp" #include "vk-renderer.hpp" -#include "vulkan/vulkan_core.h" -#include <stdint.h> -void Primitive::set_pipeline(std::string &shader_name) { +void Primitive::set_pipeline(const char* shader_name) { std::vector<VkDynamicState> dynamic_states { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; - pipeline = api.create_graphics_pipeline(shader_name.c_str(), (VkShaderStageFlagBits)(VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT), primitive_render_pass.render_pass, dynamic_states); + pipeline = api.create_graphics_pipeline(shader_name, (VkShaderStageFlagBits)(VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT), primitive_render_pass.render_pass, dynamic_states); } void Primitive::set_constant_offset(off_t offset) { @@ -19,23 +17,23 @@ void Primitive::set_constant_offset(off_t offset) { } void Primitive::set_constant(off_t offset, uint64_t* constant) { - memcpy(constants.data() + offset, constant, sizeof(uint64_t)); + memcpy(constants + offset, constant, sizeof(uint64_t)); } void Primitive::set_constant(off_t offset, Texture* texture) { auto image = api.get_image(texture->device_image); - memcpy(constants.data() + offset, (void*)&image.bindless_storage_index, sizeof(bindless_index)); + memcpy(constants + offset, (void*)&image.bindless_storage_index, sizeof(bindless_index)); } void Primitive::set_constant(off_t offset, Buffer* buffer) { auto device_buffer = api.get_buffer(buffer->device_buffer); - memcpy(constants.data() + offset, (void*)&device_buffer.device_address, sizeof(VkDeviceAddress)); + memcpy(constants + offset, (void*)&device_buffer.device_address, sizeof(VkDeviceAddress)); } void Primitive::set_constant(off_t offset, Sampler* sampler) { auto device_sampler = api.get_sampler(sampler->device_sampler); - memcpy(constants.data() + offset, (void*)&device_sampler.bindless_index, sizeof(bindless_index)); + memcpy(constants + offset, (void*)&device_sampler.bindless_index, sizeof(bindless_index)); } void Primitive::set_index_buffer(Buffer* index_buffer) { diff --git a/src/scene.cpp b/src/scene.cpp index acac051..d32a696 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -3,7 +3,7 @@ #include <filesystem>
#include <queue>
-#include "utils.hpp"
+#include "vk-renderer.hpp"
#include "gltf.hpp"
// void scene::random_scene() {
diff --git a/src/utils.cpp b/src/utils.cpp index 6557ebc..ff0619b 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,9 +1,13 @@ #include "utils.hpp" -#include <iostream> - #include "defines.hpp" +#ifdef WINDOWS +#include <windows.h> +#endif + +#include <iostream> + std::vector<uint8_t> read_file(const char* path) { #if defined(WINDOWS) FILE* f = nullptr; diff --git a/src/vk-api.cpp b/src/vk-api.cpp index 1000079..5a24eb9 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -13,7 +13,8 @@ #endif -vkapi::vkapi() { +vkapi::vkapi(vkcontext& context) + : context(context) { VkCommandPoolCreateInfo cmd_pool_create_info = {}; cmd_pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; cmd_pool_create_info.pNext = nullptr; @@ -316,7 +317,7 @@ std::vector<handle> vkapi::create_images(VkExtent3D size, VkFormat format, VkIma images_handles[image_index] = create_image(size, format, usages); } - return std::move(images_handles); + return images_handles; } void vkapi::destroy_images(std::vector<handle> &images) { @@ -326,7 +327,7 @@ void vkapi::destroy_images(std::vector<handle> &images) { } -VkFence vkapi::create_fence() { +VkFence vkapi::create_fence() const { VkFence fence; VkFenceCreateInfo create_info = {}; create_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; @@ -338,28 +339,28 @@ VkFence vkapi::create_fence() { return fence; } -void vkapi::destroy_fence(VkFence fence) { +void vkapi::destroy_fence(VkFence fence) const { vkDestroyFence(context.device, fence, nullptr); } -std::vector<VkFence> vkapi::create_fences(size_t fences_count) { +std::vector<VkFence> vkapi::create_fences(size_t fences_count) const { std::vector<VkFence> fences { fences_count }; for(size_t fence_index = 0; fence_index < fences_count; fence_index++) { fences[fence_index] = create_fence(); } - return std::move(fences); + return fences; } -void vkapi::destroy_fences(std::vector<VkFence> &fences) { - for(auto &fence: fences) { - vkDestroyFence(context.device, fence, nullptr); +void vkapi::destroy_fences(VkFence fences[], size_t fences_count) const { + for(size_t index { 0 }; index < fences_count; index++) { + vkDestroyFence(context.device, fences[index], nullptr); } } -VkSemaphore vkapi::create_semaphore() { +VkSemaphore vkapi::create_semaphore() const { VkSemaphore semaphore; VkSemaphoreCreateInfo create_info = {}; create_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; @@ -371,23 +372,23 @@ VkSemaphore vkapi::create_semaphore() { return semaphore; } -void vkapi::destroy_semaphore(VkSemaphore semaphore) { +void vkapi::destroy_semaphore(VkSemaphore semaphore) const { vkDestroySemaphore(context.device, semaphore, nullptr); } -std::vector<VkSemaphore> vkapi::create_semaphores(size_t semaphores_count) { +std::vector<VkSemaphore> vkapi::create_semaphores(size_t semaphores_count) const { std::vector<VkSemaphore> semaphores { semaphores_count }; - for (size_t semaphore_index = 0; semaphore_index < semaphores_count; semaphore_index++) { + for (size_t semaphore_index { 0 }; semaphore_index < semaphores_count; semaphore_index++) { semaphores[semaphore_index] = create_semaphore(); } - return std::move(semaphores); + return semaphores; } -void vkapi::destroy_semaphores(std::vector<VkSemaphore> &semaphores) { - for (auto &semaphore: semaphores) { - vkDestroySemaphore(context.device, semaphore, nullptr); +void vkapi::destroy_semaphores(VkSemaphore semaphores[], size_t semaphores_count) const { + for(auto index { 0 }; index < semaphores_count; index++) { + vkDestroySemaphore(context.device, semaphores[index], nullptr); } } @@ -446,7 +447,7 @@ std::vector<VkCommandBuffer> vkapi::create_command_buffers(size_t command_buffer std::vector<VkCommandBuffer> command_buffers { command_buffers_count }; vkAllocateCommandBuffers(context.device, &cmd_buf_allocate_info, command_buffers.data()); - return std::move(command_buffers); + return command_buffers; } void vkapi::destroy_command_buffers(std::vector<VkCommandBuffer> &command_buffers) { @@ -585,7 +586,7 @@ std::vector<framebuffer> vkapi::create_framebuffers(VkRenderPass render_pass, st framebuffer = create_framebuffer(render_pass, formats, size); } - return std::move(framebuffers); + return framebuffers; } void vkapi::destroy_framebuffer(framebuffer framebuffer) { @@ -611,9 +612,7 @@ pipeline vkapi::create_compute_pipeline(const char* shader_name) { char shader_path[256] = {}; sprintf(shader_path, "%s%s%s", "./shaders/", shader_name, ".comp.spv"); std::vector<uint8_t> shader_code = read_file(shader_path); - if (shader_code.size() == 0) { - exit(1); - } + assert(!shader_code.empty()); pipeline.shader_modules.resize(1); @@ -652,10 +651,10 @@ pipeline vkapi::create_compute_pipeline(const char* shader_name) { &pipeline.handle )) - return std::move(pipeline); + return pipeline; } -pipeline vkapi::create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState> dynamic_states) { +pipeline vkapi::create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState>& dynamic_states) { pipeline pipeline { .bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS }; @@ -805,7 +804,7 @@ std::vector<VkDescriptorSet> vkapi::create_descriptor_sets(VkDescriptorSetLayout std::vector<VkDescriptorSet> descriptor_sets { descriptor_sets_count }; vkAllocateDescriptorSets(context.device, &descriptor_set_allocate_info, descriptor_sets.data()); - return std::move(descriptor_sets); + return descriptor_sets; } void vkapi::destroy_descriptor_sets(std::vector<VkDescriptorSet> &descriptor_sets) { @@ -986,7 +985,10 @@ void vkapi::destroy_swapchain(swapchain& swapchain) { VKRESULT(vkDeviceWaitIdle(context.device)) for(auto &image: swapchain.images) { - bindless_descriptor.free(images[image]->bindless_storage_index, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); + // TODO: Change this + if (images[image]->bindless_storage_index != 0) + bindless_descriptor.free(images[image]->bindless_storage_index, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); + if (images[image]->bindless_sampled_index != 0) bindless_descriptor.free(images[image]->bindless_sampled_index, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); vkDestroyImageView(context.device, images[image]->view, nullptr); @@ -999,7 +1001,7 @@ void vkapi::update_descriptor_image(handle img, VkDescriptorType type) { VkDescriptorImageInfo descriptor_image_info; VkWriteDescriptorSet write_descriptor; - auto current_image = images[img]; + auto* current_image = images[img]; descriptor_image_info.sampler = VK_NULL_HANDLE; descriptor_image_info.imageView = current_image->view; @@ -1232,7 +1234,7 @@ void vkapi::end_record(VkCommandBuffer command_buffer) { VKRESULT(vkEndCommandBuffer(command_buffer)) } -VkResult vkapi::submit(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence) { +VkResult vkapi::submit(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence) const { VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; VkSubmitInfo submit_info = {}; @@ -1249,7 +1251,7 @@ VkResult vkapi::submit(VkCommandBuffer command_buffer, VkSemaphore wait_semaphor return vkQueueSubmit(context.queue, 1, &submit_info, submission_fence); } -VkResult vkapi::present(swapchain& swapchain, uint32_t image_index, VkSemaphore wait_semaphore) { +VkResult vkapi::present(swapchain& swapchain, uint32_t image_index, VkSemaphore wait_semaphore) const { VkPresentInfoKHR present_info = {}; present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; present_info.pNext = nullptr; @@ -1276,7 +1278,7 @@ const char* vkapi::shader_stage_extension(VkShaderStageFlags shader_stage) { return ".comp"; } default: - std::cerr << "Stage not supported" << std::endl; + assert(true && "Stage not supported"); return ""; } } diff --git a/src/vk-context.cpp b/src/vk-context.cpp index 53f3e59..7876d86 100644 --- a/src/vk-context.cpp +++ b/src/vk-context.cpp @@ -1,7 +1,8 @@ +#include "vk-context.hpp" + #include <iostream> #include <cassert> -#include "vk-context.hpp" #include "vulkan-loader.hpp" #define VMA_IMPLEMENTATION @@ -163,7 +164,7 @@ void vkcontext::create_memory_allocator() { allocator_info.device = device; allocator_info.instance = instance; allocator_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT; - + vmaCreateAllocator(&allocator_info, &allocator); } diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index 2fcd39d..daae775 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -1,12 +1,11 @@ -#include <iostream> +#include "vk-renderer.hpp" + #include <cstring> #include <cassert> #include "compute-renderpass.hpp" #include "primitive-renderpass.hpp" -#include "vk-renderer.hpp" -#include "defines.hpp" #include "window.hpp" #ifdef _DEBUG @@ -15,14 +14,15 @@ #define VKRESULT(result) result; #endif -vkrenderer::vkrenderer(window& wnd) { +vkrenderer::vkrenderer(window& wnd) + : api(context) { platform_surface = api.create_surface(wnd); swapchain = api.create_swapchain( platform_surface, min_swapchain_image_count, - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // | - // VK_IMAGE_USAGE_TRANSFER_DST_BIT | - // VK_IMAGE_USAGE_STORAGE_BIT, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT | + VK_IMAGE_USAGE_STORAGE_BIT, VK_NULL_HANDLE ); @@ -36,23 +36,25 @@ vkrenderer::vkrenderer(window& wnd) { command_buffers = api.create_command_buffers(virtual_frames_count); - submission_fences = api.create_fences(virtual_frames_count); - execution_semaphores = api.create_semaphores(virtual_frames_count); - acquire_semaphores = api.create_semaphores(virtual_frames_count); + for(size_t index { 0 }; index < virtual_frames_count; index++) { + submission_fences[index] = api.create_fence(); + execution_semaphores[index] = api.create_semaphore(); + acquire_semaphores[index] = api.create_semaphore(); + } staging_buffer = api.create_buffer(4096 * 4096 * 4 * sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY); } vkrenderer::~vkrenderer() { - VKRESULT(vkWaitForFences(api.context.device, submission_fences.size(), submission_fences.data(), VK_TRUE, UINT64_MAX)) + VKRESULT(vkWaitForFences(context.device, virtual_frames_count, submission_fences, VK_TRUE, UINT64_MAX)) for (auto& renderpass : renderpasses) { delete renderpass; } - api.destroy_fences(submission_fences); - api.destroy_semaphores(execution_semaphores); - api.destroy_semaphores(acquire_semaphores); + api.destroy_fences(submission_fences, virtual_frames_count); + api.destroy_semaphores(execution_semaphores, virtual_frames_count); + api.destroy_semaphores(acquire_semaphores, virtual_frames_count); api.destroy_command_buffers(command_buffers); @@ -63,26 +65,18 @@ vkrenderer::~vkrenderer() { } void vkrenderer::begin_frame() { - VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) - VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[virtual_frame_index])) + VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) + VKRESULT(vkResetFences(context.device, 1, &submission_fences[virtual_frame_index])) swapchain_image_index = 0; - auto acquire_result = vkAcquireNextImageKHR(api.context.device, swapchain.handle, UINT64_MAX, acquire_semaphores[virtual_frame_index], VK_NULL_HANDLE, &swapchain_image_index); + auto acquire_result = vkAcquireNextImageKHR(context.device, swapchain.handle, UINT64_MAX, acquire_semaphores[virtual_frame_index], VK_NULL_HANDLE, &swapchain_image_index); handle_swapchain_result(acquire_result); api.start_record(command_buffers[virtual_frame_index]); } -void vkrenderer::compute(uint32_t width, uint32_t height) { - // vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, (void*)&scene_buffer_addr); - // vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 56, 4, (void*)&swapchain.images[swapchain_image_index].bindless_storage_index); - // vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 60, 4, (void*)&accumulation_images[0].bindless_storage_index); - - // api.run_compute_pipeline(cmd_buf, tonemapping_pipeline, (width / 8) + 1, (height / 8) + 1, 1); -} - void vkrenderer::finish_frame() { - auto cmd_buf = command_buffers[virtual_frame_index]; + auto* cmd_buf = command_buffers[virtual_frame_index]; // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, swapchain.images[swapchain_image_index]); @@ -97,7 +91,7 @@ void vkrenderer::finish_frame() { } void vkrenderer::recreate_swapchain() { - // VKRESULT(vkWaitForFences(api.context.device, submission_fences.size(), submission_fences.data(), VK_TRUE, UINT64_MAX)) + // VKRESULT(vkWaitForFences(context.device, submission_fences.size(), submission_fences.data(), VK_TRUE, UINT64_MAX)) auto swapchain_image_usages = api.get_image(swapchain.images[0]).usages; struct swapchain old_swapchain = swapchain; @@ -122,7 +116,7 @@ void vkrenderer::handle_swapchain_result(VkResult function_result) { case VK_ERROR_OUT_OF_DATE_KHR: return recreate_swapchain(); case VK_ERROR_SURFACE_LOST_KHR: - std::cerr << "surface lost" << std::endl; + assert(true && "surface lost"); default: return; } @@ -140,10 +134,10 @@ void vkrenderer::update_image(Texture* texture, void* data) { std::memcpy(buffer.alloc_info.pMappedData, data, max_texture_size); - VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) - VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[virtual_frame_index])) + VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) + VKRESULT(vkResetFences(context.device, 1, &submission_fences[virtual_frame_index])) - auto cmd_buf = command_buffers[virtual_frame_index]; + auto* cmd_buf = command_buffers[virtual_frame_index]; api.start_record(cmd_buf); api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, texture->device_image); @@ -157,7 +151,7 @@ void vkrenderer::update_image(Texture* texture, void* data) { api.submit(cmd_buf, VK_NULL_HANDLE, VK_NULL_HANDLE, submission_fences[virtual_frame_index]); - VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) + VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) } void vkrenderer::update_buffer(Buffer* buffer, void* data, off_t offset, size_t size) { @@ -170,7 +164,7 @@ void vkrenderer::update_buffer(Buffer* buffer, void* data, off_t offset, size_t Buffer* vkrenderer::create_buffer(size_t size, bool isStatic) { - auto buffer = new Buffer(this, size, isStatic); + auto* buffer = new Buffer(this, size, isStatic); buffer->device_buffer = api.create_buffer(isStatic ? size : size * virtual_frames_count, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); @@ -178,7 +172,7 @@ Buffer* vkrenderer::create_buffer(size_t size, bool isStatic) { } Buffer* vkrenderer::create_index_buffer(size_t size, bool isStatic) { - auto buffer = new Buffer(this, size, isStatic); + auto* buffer = new Buffer(this, size, isStatic); buffer->device_buffer = api.create_buffer(isStatic ? size : size * virtual_frames_count, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); @@ -186,7 +180,7 @@ Buffer* vkrenderer::create_index_buffer(size_t size, bool isStatic) { } Texture* vkrenderer::create_2d_texture(size_t width, size_t height, VkFormat format) { - auto texture = new Texture(); + auto* texture = new Texture(); texture->device_image = api.create_image( { .width = (uint32_t)width, .height = (uint32_t)height, .depth = 1 }, format, @@ -198,7 +192,7 @@ Texture* vkrenderer::create_2d_texture(size_t width, size_t height, VkFormat for // TODO: Check if a similar sampler has been allocated Sampler* vkrenderer::create_sampler(VkFilter filter, VkSamplerAddressMode address_mode) { - auto sampler = new Sampler(); + auto* sampler = new Sampler(); sampler->device_sampler = api.create_sampler(filter, address_mode); @@ -224,22 +218,22 @@ Primitive* vkrenderer::create_primitive(PrimitiveRenderpass& primitive_render_pa void vkrenderer::render() { // begin_frame(); - auto cmd_buf = command_buffers[virtual_frame_index]; + auto* cmd_buf = command_buffers[virtual_frame_index]; - // for (auto& renderpass: renderpasses) { - // renderpass->execute(*this, cmd_buf); + for (auto& renderpass: renderpasses) { + renderpass->execute(*this, cmd_buf); - // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, ((ComputeRenderpass*)renderpass)->output_texture->device_image); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, ((ComputeRenderpass*)renderpass)->output_texture->device_image); - // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, swapchain.images[swapchain_image_index]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, swapchain.images[swapchain_image_index]); - // api.blit_full(cmd_buf, ((ComputeRenderpass*)renderpass)->output_texture->device_image, swapchain.images[swapchain_image_index]); - // } + api.blit_full(cmd_buf, ((ComputeRenderpass*)renderpass)->output_texture->device_image, swapchain.images[swapchain_image_index]); + } // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, swapchain.images[swapchain_image_index]); - renderpasses[1]->execute(*this, cmd_buf); + // renderpasses[1]->execute(*this, cmd_buf); api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, swapchain.images[swapchain_image_index]); diff --git a/src/vulkan-loader.cpp b/src/vulkan-loader.cpp index 42c018e..0977fe8 100644 --- a/src/vulkan-loader.cpp +++ b/src/vulkan-loader.cpp @@ -1,12 +1,10 @@ -#include <iostream> - #include "vulkan-loader.hpp" + +#include <cassert> + #include "defines.hpp" #if defined(WINDOWS) -#include <Windows.h> -#include <windowsx.h> - HMODULE vulkanLibrary = LoadLibrary("vulkan-1.dll"); #define LoadProcAddress GetProcAddress @@ -33,46 +31,30 @@ void *vulkanLibrary = dlopen("libvulkan.1.dylib", RTLD_NOW); VULKAN_DEVICE_FUNCTIONS #undef X -#define X(name) \ -void Load_##name() \ -{ \ - name = reinterpret_cast<PFN_##name>(LoadProcAddress(vulkanLibrary, #name)); \ - if (!name) { \ - std::cerr << "Cannot load vulkan function " << #name << std::endl; \ - } \ +#define X(name) \ +void Load_##name() \ +{ \ + (name) = reinterpret_cast<PFN_##name>(LoadProcAddress(vulkanLibrary, #name)); \ + assert((name) && "Cannot load vulkan function " #name); \ } VULKAN_EXPORTED_FUNCTIONS -#undef X - -#define X(name) \ -void Load_##name() \ -{ \ - name = reinterpret_cast<PFN_##name>(vkGetInstanceProcAddr(nullptr, #name)); \ - if (!name) { \ - std::cerr << "Cannot load vulkan function " << #name << std::endl; \ - } \ -} VULKAN_APPLICATION_FUNCTIONS #undef X -#define X(name) \ -void Load_##name(VkInstance instance) \ -{ \ - name = reinterpret_cast<PFN_##name>(vkGetInstanceProcAddr(instance, #name)); \ - if (!name) { \ - std::cerr << "Cannot load vulkan function " << #name << std::endl; \ - } \ +#define X(name) \ +void Load_##name(VkInstance instance) \ +{ \ + (name) = reinterpret_cast<PFN_##name>(vkGetInstanceProcAddr(instance, #name)); \ + assert((name) && "Cannot load vulkan function " #name); \ } VULKAN_INSTANCE_FUNCTIONS #undef X -#define X(name) \ -void Load_##name(VkDevice device) \ -{ \ - name = reinterpret_cast<PFN_##name>(vkGetDeviceProcAddr(device, #name)); \ - if (!name) { \ - std::cerr << "Cannot load vulkan function " << #name << std::endl; \ - } \ +#define X(name) \ +void Load_##name(VkDevice device) \ +{ \ + (name) = reinterpret_cast<PFN_##name>(vkGetDeviceProcAddr(device, #name)); \ + assert((name) && "Cannot load vulkan function " #name); \ } VULKAN_DEVICE_FUNCTIONS #undef X |
