Merge pull request #5 from Jiga228/main #1
@@ -6,6 +6,8 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
#include "GPU_GarbageCollector.h"
|
#include "GPU_GarbageCollector.h"
|
||||||
#include "Core/CoreInstance.hpp"
|
#include "Core/CoreInstance.hpp"
|
||||||
#include "Log/Log.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)
|
if (family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||||
{
|
{
|
||||||
queue_family_indices.graphics_family = i;
|
queue_family_indices.graphics_family = i;
|
||||||
|
queue_family_indices.is_find_graphics_family = true;
|
||||||
break;
|
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");
|
throw std::runtime_error("Failed to find a graphics queue family");
|
||||||
|
|
||||||
// Find present
|
// Find present
|
||||||
VkBool32 present_support = false;
|
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)
|
if (present_support == VK_TRUE)
|
||||||
|
{
|
||||||
queue_family_indices.present_family = queue_family_indices.graphics_family;
|
queue_family_indices.present_family = queue_family_indices.graphics_family;
|
||||||
|
queue_family_indices.is_find_present_family = true;
|
||||||
|
}
|
||||||
// иначе ищем другую подходящую очередь
|
// иначе ищем другую подходящую очередь
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -152,26 +158,33 @@ UwURenderEngine::QueueFamilyIndices UwURenderEngine::find_queue_family_indices(V
|
|||||||
if (present_support == VK_TRUE)
|
if (present_support == VK_TRUE)
|
||||||
{
|
{
|
||||||
queue_family_indices.present_family = i;
|
queue_family_indices.present_family = i;
|
||||||
|
queue_family_indices.is_find_present_family = true;
|
||||||
break;
|
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");
|
throw std::runtime_error("Failed to find a present queue family");
|
||||||
|
|
||||||
// С начала пытаемся найти очередь специалезированную
|
// С начала пытаемся найти очередь специалезированную
|
||||||
// для копирования отличную от графической
|
// для копирования отличную от графической
|
||||||
for(uint32_t i = 0; i < family_properties.size(); ++i)
|
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;
|
continue;
|
||||||
if (family_properties[i].queueFlags & VK_QUEUE_TRANSFER_BIT)
|
if (family_properties[i].queueFlags & VK_QUEUE_TRANSFER_BIT)
|
||||||
|
{
|
||||||
queue_family_indices.transfer_family = i;
|
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.transfer_family = queue_family_indices.graphics_family;
|
||||||
|
queue_family_indices.is_find_transfer_family = true;
|
||||||
|
}
|
||||||
return queue_family_indices;
|
return queue_family_indices;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,9 +288,9 @@ void UwURenderEngine::create_logical_device()
|
|||||||
VkPhysicalDeviceFeatures device_features{};
|
VkPhysicalDeviceFeatures device_features{};
|
||||||
|
|
||||||
std::vector<VkDeviceQueueCreateInfo> queue_create_infos;
|
std::vector<VkDeviceQueueCreateInfo> queue_create_infos;
|
||||||
std::set<uint32_t> unique_queue_families = {indices.graphics_family.value(),
|
std::set<uint32_t> unique_queue_families = {indices.graphics_family,
|
||||||
indices.present_family.value(),
|
indices.present_family,
|
||||||
indices.transfer_family.value()};
|
indices.transfer_family};
|
||||||
|
|
||||||
float queue_priority = 1.0f;
|
float queue_priority = 1.0f;
|
||||||
VkDeviceQueueCreateInfo device_queue_create_info{};
|
VkDeviceQueueCreateInfo device_queue_create_info{};
|
||||||
@@ -310,9 +323,9 @@ void UwURenderEngine::create_logical_device()
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
VK_CHECK(vkCreateDevice(physical_device_, &device_create_info, nullptr, &device_))
|
VK_CHECK(vkCreateDevice(physical_device_, &device_create_info, nullptr, &device_))
|
||||||
vkGetDeviceQueue(device_, indices.graphics_family.value(), 0, &graphics_queue_);
|
vkGetDeviceQueue(device_, indices.graphics_family, 0, &graphics_queue_);
|
||||||
vkGetDeviceQueue(device_, indices.present_family.value(), 0, &present_queue_);
|
vkGetDeviceQueue(device_, indices.present_family, 0, &present_queue_);
|
||||||
vkGetDeviceQueue(device_, indices.transfer_family.value(), 0, &transfer_queue_);
|
vkGetDeviceQueue(device_, indices.transfer_family, 0, &transfer_queue_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UwURenderEngine::create_swapchain()
|
void UwURenderEngine::create_swapchain()
|
||||||
@@ -342,9 +355,9 @@ void UwURenderEngine::create_swapchain()
|
|||||||
swapchain_create_info.oldSwapchain = VK_NULL_HANDLE;
|
swapchain_create_info.oldSwapchain = VK_NULL_HANDLE;
|
||||||
|
|
||||||
QueueFamilyIndices family_indices = find_queue_family_indices(physical_device_, surface_);
|
QueueFamilyIndices family_indices = find_queue_family_indices(physical_device_, surface_);
|
||||||
const std::set<uint32_t> queue_family_indices = { family_indices.graphics_family.value(),
|
const std::set<uint32_t> queue_family_indices = { family_indices.graphics_family,
|
||||||
family_indices.present_family.value(),
|
family_indices.present_family,
|
||||||
family_indices.transfer_family.value() };
|
family_indices.transfer_family };
|
||||||
std::vector<uint32_t> arr_families;
|
std::vector<uint32_t> arr_families;
|
||||||
if (queue_family_indices.size() > 1)
|
if (queue_family_indices.size() > 1)
|
||||||
{
|
{
|
||||||
@@ -441,7 +454,7 @@ void UwURenderEngine::create_render_pass()
|
|||||||
|
|
||||||
void UwURenderEngine::create_description_set_layout()
|
void UwURenderEngine::create_description_set_layout()
|
||||||
{
|
{
|
||||||
VkDescriptorSetLayoutBinding ubo_layout_binding{};
|
VkDescriptorSetLayoutBinding ubo_layout_binding;
|
||||||
ubo_layout_binding.binding = 0;
|
ubo_layout_binding.binding = 0;
|
||||||
ubo_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
ubo_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||||
ubo_layout_binding.descriptorCount = 1;
|
ubo_layout_binding.descriptorCount = 1;
|
||||||
@@ -476,7 +489,7 @@ void UwURenderEngine::create_uniform_buffers()
|
|||||||
|
|
||||||
void UwURenderEngine::create_descriptor_pool()
|
void UwURenderEngine::create_descriptor_pool()
|
||||||
{
|
{
|
||||||
VkDescriptorPoolSize pool_size{};
|
VkDescriptorPoolSize pool_size;
|
||||||
pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||||
pool_size.descriptorCount = static_cast<uint32_t>(swapchain_images_.size());
|
pool_size.descriptorCount = static_cast<uint32_t>(swapchain_images_.size());
|
||||||
|
|
||||||
@@ -690,7 +703,7 @@ void UwURenderEngine::create_command_pool()
|
|||||||
|
|
||||||
VkCommandPoolCreateInfo command_pool_info{};
|
VkCommandPoolCreateInfo command_pool_info{};
|
||||||
command_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_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;
|
command_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||||
|
|
||||||
VK_CHECK(vkCreateCommandPool(device_, &command_pool_info, nullptr, &command_pool_))
|
VK_CHECK(vkCreateCommandPool(device_, &command_pool_info, nullptr, &command_pool_))
|
||||||
@@ -698,9 +711,9 @@ void UwURenderEngine::create_command_pool()
|
|||||||
|
|
||||||
std::vector<uint32_t> UwURenderEngine::get_unique_family_indices(const QueueFamilyIndices& indices)
|
std::vector<uint32_t> UwURenderEngine::get_unique_family_indices(const QueueFamilyIndices& indices)
|
||||||
{
|
{
|
||||||
std::set<uint32_t> set_indices = {indices.graphics_family.value(),
|
std::set<uint32_t> set_indices = {indices.graphics_family,
|
||||||
indices.present_family.value(),
|
indices.present_family,
|
||||||
indices.transfer_family.value()};
|
indices.transfer_family};
|
||||||
|
|
||||||
std::vector<uint32_t> arr_indices;
|
std::vector<uint32_t> arr_indices;
|
||||||
arr_indices.reserve(set_indices.size());
|
arr_indices.reserve(set_indices.size());
|
||||||
@@ -715,7 +728,7 @@ void UwURenderEngine::allocate_vertex_buffer()
|
|||||||
|
|
||||||
|
|
||||||
std::vector<uint32_t> arr_indices = get_unique_family_indices(indices);
|
std::vector<uint32_t> arr_indices = get_unique_family_indices(indices);
|
||||||
std::vector<uint32_t> transfer_index = {indices.transfer_family.value()};
|
std::vector<uint32_t> transfer_index = {indices.transfer_family};
|
||||||
|
|
||||||
VkDeviceSize size_buffer = sizeof(vertices_[0]) * vertices_.size();
|
VkDeviceSize size_buffer = sizeof(vertices_[0]) * vertices_.size();
|
||||||
VkBuffer staging_buffer = DeviceFunctions::create_buffer(device_, size_buffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
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);
|
memcpy(data, vertices_.data(), size_buffer);
|
||||||
vkUnmapMemory(device_, staging_buffer_memory);
|
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);
|
vkFreeMemory(device_, staging_buffer_memory, nullptr);
|
||||||
vkDestroyBuffer(device_, staging_buffer, nullptr);
|
vkDestroyBuffer(device_, staging_buffer, nullptr);
|
||||||
@@ -744,12 +757,12 @@ void UwURenderEngine::allocate_vertex_buffer()
|
|||||||
void UwURenderEngine::allocate_index_buffer()
|
void UwURenderEngine::allocate_index_buffer()
|
||||||
{
|
{
|
||||||
QueueFamilyIndices indices = find_queue_family_indices(physical_device_, surface_);
|
QueueFamilyIndices indices = find_queue_family_indices(physical_device_, surface_);
|
||||||
std::set<uint32_t> set_indices = {indices.graphics_family.value(),
|
std::set<uint32_t> set_indices = {indices.graphics_family,
|
||||||
indices.present_family.value(),
|
indices.present_family,
|
||||||
indices.transfer_family.value()};
|
indices.transfer_family};
|
||||||
|
|
||||||
std::vector<uint32_t> arr_indices = get_unique_family_indices(indices);
|
std::vector<uint32_t> arr_indices = get_unique_family_indices(indices);
|
||||||
std::vector<uint32_t> transfer_index = {indices.transfer_family.value()};
|
std::vector<uint32_t> transfer_index = {indices.transfer_family};
|
||||||
|
|
||||||
VkDeviceSize size_buffer = sizeof(indices_[0]) * indices_.size();
|
VkDeviceSize size_buffer = sizeof(indices_[0]) * indices_.size();
|
||||||
VkBuffer staging_buffer = DeviceFunctions::create_buffer(device_, size_buffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
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);
|
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);
|
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);
|
vkDestroyBuffer(device_, staging_buffer, nullptr);
|
||||||
vkFreeMemory(device_, staging_buffer_memory, 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))
|
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();
|
static auto start_time = std::chrono::high_resolution_clock::now();
|
||||||
auto current_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);
|
vkWaitForFences(device_, 1, &in_flight_fences_[current_frame_], VK_TRUE, UINT64_MAX);
|
||||||
vkResetFences(device_, 1, &in_flight_fences_[current_frame_]);
|
vkResetFences(device_, 1, &in_flight_fences_[current_frame_]);
|
||||||
|
|
||||||
|
// Free video memory
|
||||||
garbage_collector_->FreeHeap();
|
garbage_collector_->FreeHeap();
|
||||||
|
|
||||||
uint32_t image_index;
|
uint32_t image_index;
|
||||||
@@ -907,7 +921,7 @@ void UwURenderEngine::draw_frame()
|
|||||||
|
|
||||||
vkQueuePresentKHR(present_queue_, &present_info);
|
vkQueuePresentKHR(present_queue_, &present_info);
|
||||||
|
|
||||||
current_frame_ = (current_frame_ + 1) % static_cast<int>(swapchain_images_.size());
|
current_frame_ = (current_frame_ + 1) % static_cast<unsigned int>(swapchain_images_.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void UwURenderEngine::create_surface()
|
void UwURenderEngine::create_surface()
|
||||||
|
|||||||
@@ -3,14 +3,12 @@
|
|||||||
#include "RenderEngineBase.hpp"
|
#include "RenderEngineBase.hpp"
|
||||||
#include "ModelManager.hpp"
|
#include "ModelManager.hpp"
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#define GLM_FORCE_RADIANS
|
#define GLM_FORCE_RADIANS
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
|
|
||||||
class GPU_GarbageCollector;
|
class GPU_GarbageCollector;
|
||||||
|
|
||||||
@@ -18,15 +16,18 @@ class UwURenderEngine : public RenderEngineBase
|
|||||||
{
|
{
|
||||||
struct QueueFamilyIndices
|
struct QueueFamilyIndices
|
||||||
{
|
{
|
||||||
std::optional<uint32_t> graphics_family;
|
uint32_t graphics_family = 0;
|
||||||
std::optional<uint32_t> present_family;
|
uint32_t present_family = 0;
|
||||||
std::optional<uint32_t> transfer_family;
|
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
|
struct SwapchainSupportDetails
|
||||||
{
|
{
|
||||||
VkSurfaceCapabilitiesKHR capabilities;
|
|
||||||
std::vector<VkSurfaceFormatKHR> formats;
|
std::vector<VkSurfaceFormatKHR> formats;
|
||||||
std::vector<VkPresentModeKHR> present_modes;
|
std::vector<VkPresentModeKHR> present_modes;
|
||||||
|
VkSurfaceCapabilitiesKHR capabilities;
|
||||||
};
|
};
|
||||||
struct UniformBufferObject {
|
struct UniformBufferObject {
|
||||||
glm::mat4 model;
|
glm::mat4 model;
|
||||||
@@ -55,8 +56,8 @@ class UwURenderEngine : public RenderEngineBase
|
|||||||
|
|
||||||
static const std::vector<const char*> deviceExtensions;
|
static const std::vector<const char*> deviceExtensions;
|
||||||
|
|
||||||
int current_frame_ = 0;
|
// Counting frame from the beginning
|
||||||
|
unsigned int current_frame_ = 0;
|
||||||
|
|
||||||
CoreInstance& core_;
|
CoreInstance& core_;
|
||||||
std::shared_ptr<ModelManager> model_manager_;
|
std::shared_ptr<ModelManager> model_manager_;
|
||||||
@@ -130,7 +131,7 @@ class UwURenderEngine : public RenderEngineBase
|
|||||||
void allocate_index_buffer();
|
void allocate_index_buffer();
|
||||||
|
|
||||||
void record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) const;
|
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();
|
void draw_frame();
|
||||||
public:
|
public:
|
||||||
// Can throw the exception
|
// Can throw the exception
|
||||||
|
|||||||
Reference in New Issue
Block a user