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 /src | |
| parent | 928b1b0503d2ecab09cc275e9775593a41e417cb (diff) | |
Improve compilation time
Diffstat (limited to 'src')
| -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 |
11 files changed, 142 insertions, 151 deletions
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 |
