diff --git a/UwURenderEngine/RenderEngine/UwURenderEngine.cpp b/UwURenderEngine/RenderEngine/UwURenderEngine.cpp index a294ced..4a23135 100644 --- a/UwURenderEngine/RenderEngine/UwURenderEngine.cpp +++ b/UwURenderEngine/RenderEngine/UwURenderEngine.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include "GPU_GarbageCollector.h" #include "Core/CoreInstance.hpp" #include "Log/Log.hpp" @@ -129,19 +131,23 @@ UwURenderEngine::QueueFamilyIndices UwURenderEngine::find_queue_family_indices(V if (family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { queue_family_indices.graphics_family = i; + queue_family_indices.is_find_graphics_family = true; break; } } - if (queue_family_indices.graphics_family.has_value() == false) + if (queue_family_indices.is_find_graphics_family == false) throw std::runtime_error("Failed to find a graphics queue family"); // Find present VkBool32 present_support = false; - VK_CHECK(vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, queue_family_indices.graphics_family.value(), surface, &present_support)) + VK_CHECK(vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, queue_family_indices.graphics_family, surface, &present_support)) // Если графическая очередь поддерживет презентацию кадров, то используем её if (present_support == VK_TRUE) + { queue_family_indices.present_family = queue_family_indices.graphics_family; + queue_family_indices.is_find_present_family = true; + } // иначе ищем другую подходящую очередь else { @@ -152,26 +158,33 @@ UwURenderEngine::QueueFamilyIndices UwURenderEngine::find_queue_family_indices(V if (present_support == VK_TRUE) { queue_family_indices.present_family = i; + queue_family_indices.is_find_present_family = true; break; } } } - if (queue_family_indices.present_family.has_value() == false) + if (queue_family_indices.is_find_present_family == false) throw std::runtime_error("Failed to find a present queue family"); // С начала пытаемся найти очередь специалезированную // для копирования отличную от графической for(uint32_t i = 0; i < family_properties.size(); ++i) { - if (i == queue_family_indices.graphics_family.value()) + if (i == queue_family_indices.graphics_family) continue; if (family_properties[i].queueFlags & VK_QUEUE_TRANSFER_BIT) + { queue_family_indices.transfer_family = i; + queue_family_indices.is_find_transfer_family = true; + } } // Если не находим, то используем графическую - if (queue_family_indices.transfer_family.has_value() == false) + if (queue_family_indices.is_find_transfer_family == false) + { queue_family_indices.transfer_family = queue_family_indices.graphics_family; + queue_family_indices.is_find_transfer_family = true; + } return queue_family_indices; } @@ -275,9 +288,9 @@ void UwURenderEngine::create_logical_device() VkPhysicalDeviceFeatures device_features{}; std::vector queue_create_infos; - std::set unique_queue_families = {indices.graphics_family.value(), - indices.present_family.value(), - indices.transfer_family.value()}; + std::set unique_queue_families = {indices.graphics_family, + indices.present_family, + indices.transfer_family}; float queue_priority = 1.0f; VkDeviceQueueCreateInfo device_queue_create_info{}; @@ -310,9 +323,9 @@ void UwURenderEngine::create_logical_device() #endif VK_CHECK(vkCreateDevice(physical_device_, &device_create_info, nullptr, &device_)) - vkGetDeviceQueue(device_, indices.graphics_family.value(), 0, &graphics_queue_); - vkGetDeviceQueue(device_, indices.present_family.value(), 0, &present_queue_); - vkGetDeviceQueue(device_, indices.transfer_family.value(), 0, &transfer_queue_); + vkGetDeviceQueue(device_, indices.graphics_family, 0, &graphics_queue_); + vkGetDeviceQueue(device_, indices.present_family, 0, &present_queue_); + vkGetDeviceQueue(device_, indices.transfer_family, 0, &transfer_queue_); } void UwURenderEngine::create_swapchain() @@ -342,9 +355,9 @@ void UwURenderEngine::create_swapchain() swapchain_create_info.oldSwapchain = VK_NULL_HANDLE; QueueFamilyIndices family_indices = find_queue_family_indices(physical_device_, surface_); - const std::set queue_family_indices = { family_indices.graphics_family.value(), - family_indices.present_family.value(), - family_indices.transfer_family.value() }; + const std::set queue_family_indices = { family_indices.graphics_family, + family_indices.present_family, + family_indices.transfer_family }; std::vector arr_families; if (queue_family_indices.size() > 1) { @@ -441,7 +454,7 @@ void UwURenderEngine::create_render_pass() void UwURenderEngine::create_description_set_layout() { - VkDescriptorSetLayoutBinding ubo_layout_binding{}; + VkDescriptorSetLayoutBinding ubo_layout_binding; ubo_layout_binding.binding = 0; ubo_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; ubo_layout_binding.descriptorCount = 1; @@ -476,7 +489,7 @@ void UwURenderEngine::create_uniform_buffers() void UwURenderEngine::create_descriptor_pool() { - VkDescriptorPoolSize pool_size{}; + VkDescriptorPoolSize pool_size; pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; pool_size.descriptorCount = static_cast(swapchain_images_.size()); @@ -690,7 +703,7 @@ void UwURenderEngine::create_command_pool() VkCommandPoolCreateInfo command_pool_info{}; command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - command_pool_info.queueFamilyIndex = queue_family_indices.graphics_family.value(); + command_pool_info.queueFamilyIndex = queue_family_indices.graphics_family; command_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; VK_CHECK(vkCreateCommandPool(device_, &command_pool_info, nullptr, &command_pool_)) @@ -698,9 +711,9 @@ void UwURenderEngine::create_command_pool() std::vector UwURenderEngine::get_unique_family_indices(const QueueFamilyIndices& indices) { - std::set set_indices = {indices.graphics_family.value(), - indices.present_family.value(), - indices.transfer_family.value()}; + std::set set_indices = {indices.graphics_family, + indices.present_family, + indices.transfer_family}; std::vector arr_indices; arr_indices.reserve(set_indices.size()); @@ -715,7 +728,7 @@ void UwURenderEngine::allocate_vertex_buffer() std::vector arr_indices = get_unique_family_indices(indices); - std::vector transfer_index = {indices.transfer_family.value()}; + std::vector transfer_index = {indices.transfer_family}; VkDeviceSize size_buffer = sizeof(vertices_[0]) * vertices_.size(); VkBuffer staging_buffer = DeviceFunctions::create_buffer(device_, size_buffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, @@ -735,7 +748,7 @@ void UwURenderEngine::allocate_vertex_buffer() memcpy(data, vertices_.data(), size_buffer); vkUnmapMemory(device_, staging_buffer_memory); - DeviceFunctions::device_memcpy(device_, indices.transfer_family.value(), staging_buffer, vertex_buffer_, size_buffer); + DeviceFunctions::device_memcpy(device_, indices.transfer_family, staging_buffer, vertex_buffer_, size_buffer); vkFreeMemory(device_, staging_buffer_memory, nullptr); vkDestroyBuffer(device_, staging_buffer, nullptr); @@ -744,12 +757,12 @@ void UwURenderEngine::allocate_vertex_buffer() void UwURenderEngine::allocate_index_buffer() { QueueFamilyIndices indices = find_queue_family_indices(physical_device_, surface_); - std::set set_indices = {indices.graphics_family.value(), - indices.present_family.value(), - indices.transfer_family.value()}; + std::set set_indices = {indices.graphics_family, + indices.present_family, + indices.transfer_family}; std::vector arr_indices = get_unique_family_indices(indices); - std::vector transfer_index = {indices.transfer_family.value()}; + std::vector transfer_index = {indices.transfer_family}; VkDeviceSize size_buffer = sizeof(indices_[0]) * indices_.size(); VkBuffer staging_buffer = DeviceFunctions::create_buffer(device_, size_buffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, @@ -768,7 +781,7 @@ void UwURenderEngine::allocate_index_buffer() index_buffer_memory_ = DeviceFunctions::allocate_device_memory(physical_device_, device_, index_buffer_, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); vkBindBufferMemory(device_, index_buffer_, index_buffer_memory_, 0); - DeviceFunctions::device_memcpy(device_, indices.transfer_family.value(),staging_buffer, index_buffer_, size_buffer); + DeviceFunctions::device_memcpy(device_, indices.transfer_family,staging_buffer, index_buffer_, size_buffer); vkDestroyBuffer(device_, staging_buffer, nullptr); vkFreeMemory(device_, staging_buffer_memory, nullptr); @@ -855,7 +868,7 @@ void UwURenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint VK_CHECK(vkEndCommandBuffer(command_buffer)) } -void UwURenderEngine::update_uniform_buffer(uint32_t current_frame) +void UwURenderEngine::update_uniform_buffer(uint32_t current_frame) const { static auto start_time = std::chrono::high_resolution_clock::now(); auto current_time = std::chrono::high_resolution_clock::now(); @@ -873,6 +886,7 @@ void UwURenderEngine::draw_frame() vkWaitForFences(device_, 1, &in_flight_fences_[current_frame_], VK_TRUE, UINT64_MAX); vkResetFences(device_, 1, &in_flight_fences_[current_frame_]); + // Free video memory garbage_collector_->FreeHeap(); uint32_t image_index; @@ -907,7 +921,7 @@ void UwURenderEngine::draw_frame() vkQueuePresentKHR(present_queue_, &present_info); - current_frame_ = (current_frame_ + 1) % static_cast(swapchain_images_.size()); + current_frame_ = (current_frame_ + 1) % static_cast(swapchain_images_.size()); } void UwURenderEngine::create_surface() diff --git a/UwURenderEngine/RenderEngine/UwURenderEngine.hpp b/UwURenderEngine/RenderEngine/UwURenderEngine.hpp index 56fc015..5ece50b 100644 --- a/UwURenderEngine/RenderEngine/UwURenderEngine.hpp +++ b/UwURenderEngine/RenderEngine/UwURenderEngine.hpp @@ -3,14 +3,12 @@ #include "RenderEngineBase.hpp" #include "ModelManager.hpp" -#include #include #include #include #define GLM_FORCE_RADIANS #include -#include class GPU_GarbageCollector; @@ -18,15 +16,18 @@ class UwURenderEngine : public RenderEngineBase { struct QueueFamilyIndices { - std::optional graphics_family; - std::optional present_family; - std::optional transfer_family; + uint32_t graphics_family = 0; + uint32_t present_family = 0; + uint32_t transfer_family = 0; + bool is_find_graphics_family = false; + bool is_find_present_family = false; + bool is_find_transfer_family = false; }; struct SwapchainSupportDetails { - VkSurfaceCapabilitiesKHR capabilities; std::vector formats; std::vector present_modes; + VkSurfaceCapabilitiesKHR capabilities; }; struct UniformBufferObject { glm::mat4 model; @@ -55,8 +56,8 @@ class UwURenderEngine : public RenderEngineBase static const std::vector deviceExtensions; - int current_frame_ = 0; - + // Counting frame from the beginning + unsigned int current_frame_ = 0; CoreInstance& core_; std::shared_ptr model_manager_; @@ -130,7 +131,7 @@ class UwURenderEngine : public RenderEngineBase void allocate_index_buffer(); void record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) const; - void update_uniform_buffer(uint32_t current_frame); + void update_uniform_buffer(uint32_t current_frame) const; void draw_frame(); public: // Can throw the exception