Refactor
This commit is contained in:
+25
-26
@@ -1,6 +1,5 @@
|
||||
#include "RenderEngine.h"
|
||||
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
@@ -29,14 +28,14 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
||||
return VK_FALSE;
|
||||
}
|
||||
|
||||
VkResult RenderEngine::enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info)
|
||||
VkResult RenderEngine::enable_layer_validation(const VkDebugUtilsMessengerCreateInfoEXT* create_info)
|
||||
{
|
||||
PFN_vkCreateDebugUtilsMessengerEXT debug_creator = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(instance_, "vkCreateDebugUtilsMessengerEXT"));
|
||||
return debug_creator(instance_, create_info, nullptr, &debug_messenger_);
|
||||
}
|
||||
#endif
|
||||
|
||||
RenderEngine::SwapchainSupportDetails RenderEngine::query_swapchain_details()
|
||||
RenderEngine::SwapchainSupportDetails RenderEngine::query_swapchain_details() const
|
||||
{
|
||||
SwapchainSupportDetails details;
|
||||
|
||||
@@ -68,14 +67,14 @@ std::vector<const char*> RenderEngine::get_required_extensions()
|
||||
}
|
||||
|
||||
|
||||
bool RenderEngine::QueueFamilyIndices::is_complete()
|
||||
bool RenderEngine::QueueFamilyIndices::is_complete() const
|
||||
{
|
||||
return graphycs_family.has_value() && present_family.has_value();
|
||||
return graphics_family.has_value() && present_family.has_value();
|
||||
}
|
||||
|
||||
VkVertexInputBindingDescription RenderEngine::Vertex::get_binding_description()
|
||||
{
|
||||
VkVertexInputBindingDescription description{};
|
||||
VkVertexInputBindingDescription description;
|
||||
description.binding = 0;
|
||||
description.stride = sizeof(Vertex);
|
||||
description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||
@@ -139,13 +138,13 @@ RenderEngine::QueueFamilyIndices RenderEngine::find_queue_family_indices(VkPhysi
|
||||
|
||||
uint32_t queueFamilyCount = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queueFamilyCount, nullptr);
|
||||
std::vector<VkQueueFamilyProperties> family_propertieses(queueFamilyCount);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queueFamilyCount, family_propertieses.data());
|
||||
std::vector<VkQueueFamilyProperties> family_properties(queueFamilyCount);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queueFamilyCount, family_properties.data());
|
||||
|
||||
for (uint32_t i = 0; i < family_propertieses.size(); ++i)
|
||||
for (uint32_t i = 0; i < family_properties.size(); ++i)
|
||||
{
|
||||
if (family_propertieses[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||
queue_family_indices.graphycs_family = i;
|
||||
if (family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||
queue_family_indices.graphics_family = i;
|
||||
|
||||
VkBool32 present_support = false;
|
||||
VK_CHECK(vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, i, surface, &present_support));
|
||||
@@ -195,7 +194,7 @@ VkExtent2D RenderEngine::choose_extent(const VkSurfaceCapabilitiesKHR& capabilit
|
||||
}
|
||||
}
|
||||
|
||||
VkShaderModule RenderEngine::create_shader_module(const std::string code)
|
||||
VkShaderModule RenderEngine::create_shader_module(const std::string& code) const
|
||||
{
|
||||
VkShaderModuleCreateInfo shader_module_info{};
|
||||
shader_module_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||
@@ -275,7 +274,7 @@ void RenderEngine::create_logical_device()
|
||||
VkPhysicalDeviceFeatures device_features{};
|
||||
|
||||
std::vector<VkDeviceQueueCreateInfo> queue_create_infos;
|
||||
std::set<uint32_t> unique_queue_families = {indices.graphycs_family.value(), indices.present_family.value()};
|
||||
std::set<uint32_t> unique_queue_families = {indices.graphics_family.value(), indices.present_family.value()};
|
||||
|
||||
float queue_priority = 1.0f;
|
||||
VkDeviceQueueCreateInfo device_queue_create_info{};
|
||||
@@ -308,7 +307,7 @@ void RenderEngine::create_logical_device()
|
||||
|
||||
#endif
|
||||
VK_CHECK(vkCreateDevice(physical_device_, &device_create_info, nullptr, &device_));
|
||||
vkGetDeviceQueue(device_, indices.graphycs_family.value(), 0, &graphics_queue_);
|
||||
vkGetDeviceQueue(device_, indices.graphics_family.value(), 0, &graphics_queue_);
|
||||
vkGetDeviceQueue(device_, indices.present_family.value(), 0, &present_queue_);
|
||||
}
|
||||
|
||||
@@ -339,8 +338,8 @@ void RenderEngine::create_swapchain(VkPresentModeKHR desired_present_mode)
|
||||
swapchain_create_info.oldSwapchain = VK_NULL_HANDLE;
|
||||
|
||||
QueueFamilyIndices family_indices = find_queue_family_indices(physical_device_, surface_);
|
||||
uint32_t queue_family_indices[] = {family_indices.graphycs_family.value(), family_indices.present_family.value()};
|
||||
if (family_indices.graphycs_family != family_indices.present_family)
|
||||
uint32_t queue_family_indices[] = {family_indices.graphics_family.value(), family_indices.present_family.value()};
|
||||
if (family_indices.graphics_family != family_indices.present_family)
|
||||
{
|
||||
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||
swapchain_create_info.queueFamilyIndexCount = 2;
|
||||
@@ -399,7 +398,7 @@ void RenderEngine::create_render_pass()
|
||||
attachment_description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
attachment_description.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
|
||||
VkAttachmentReference attachment_reference{};
|
||||
VkAttachmentReference attachment_reference;
|
||||
attachment_reference.attachment = 0;
|
||||
attachment_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
|
||||
@@ -593,13 +592,13 @@ void RenderEngine::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.graphycs_family.value();
|
||||
command_pool_info.queueFamilyIndex = queue_family_indices.graphics_family.value();
|
||||
command_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||
|
||||
VK_CHECK(vkCreateCommandPool(device_, &command_pool_info, nullptr, &command_pool_));
|
||||
}
|
||||
|
||||
uint32_t RenderEngine::find_memory_type(uint32_t typeFilter, VkMemoryPropertyFlags properties)
|
||||
uint32_t RenderEngine::find_memory_type(uint32_t typeFilter, VkMemoryPropertyFlags properties) const
|
||||
{
|
||||
VkPhysicalDeviceMemoryProperties memory_properties{};
|
||||
vkGetPhysicalDeviceMemoryProperties(physical_device_, &memory_properties);
|
||||
@@ -636,7 +635,7 @@ void RenderEngine::allocate_vertex_buffer()
|
||||
|
||||
void* data;
|
||||
VK_CHECK(vkMapMemory(device_, vertex_buffer_memory_, 0, buffer_info.size, 0, &data));
|
||||
memcpy(data, vertices.data(), (size_t) buffer_info.size);
|
||||
memcpy(data, vertices.data(), buffer_info.size);
|
||||
vkUnmapMemory(device_, vertex_buffer_memory_);
|
||||
}
|
||||
|
||||
@@ -674,13 +673,13 @@ void RenderEngine::create_sync_objects()
|
||||
}
|
||||
}
|
||||
|
||||
void RenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index)
|
||||
void RenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) const
|
||||
{
|
||||
VkCommandBufferBeginInfo command_buffer_begin_info{};
|
||||
command_buffer_begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
VK_CHECK(vkBeginCommandBuffer(command_buffer, &command_buffer_begin_info));
|
||||
|
||||
const VkClearValue clear_color = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
|
||||
constexpr VkClearValue clear_color = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
|
||||
VkRenderPassBeginInfo render_pass_begin_info{};
|
||||
render_pass_begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
render_pass_begin_info.renderPass = render_pass_;
|
||||
@@ -697,7 +696,7 @@ void RenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint32_
|
||||
VkDeviceSize offset[] = {0};
|
||||
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offset);
|
||||
|
||||
VkViewport viewport{};
|
||||
VkViewport viewport;
|
||||
viewport.x = 0.0f;
|
||||
viewport.y = 0.0f;
|
||||
viewport.width = static_cast<float>(swapchain_extent_.width);
|
||||
@@ -706,7 +705,7 @@ void RenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint32_
|
||||
viewport.maxDepth = 1.0f;
|
||||
vkCmdSetViewport(command_buffer, 0, 1, &viewport);
|
||||
|
||||
VkRect2D scissor{};
|
||||
VkRect2D scissor;
|
||||
scissor.offset = {0, 0};
|
||||
scissor.extent = swapchain_extent_;
|
||||
vkCmdSetScissor(command_buffer, 0, 1, &scissor);
|
||||
@@ -752,7 +751,7 @@ void RenderEngine::draw_frame()
|
||||
|
||||
vkQueuePresentKHR(present_queue_, &present_info);
|
||||
|
||||
current_frame_ = (current_frame_ + 1) % swapchain_images_.size();
|
||||
current_frame_ = (current_frame_ + 1) % static_cast<int>(swapchain_images_.size());
|
||||
}
|
||||
|
||||
void RenderEngine::create_surface()
|
||||
@@ -856,7 +855,7 @@ void RenderEngine::start()
|
||||
}
|
||||
}
|
||||
|
||||
void RenderEngine::stop_render()
|
||||
void RenderEngine::stop_render() const
|
||||
{
|
||||
if (window_ != nullptr && !glfwWindowShouldClose(window_))
|
||||
glfwSetWindowShouldClose(window_, true);
|
||||
|
||||
@@ -24,9 +24,9 @@ class RenderEngine
|
||||
{
|
||||
struct QueueFamilyIndices
|
||||
{
|
||||
std::optional<uint32_t> graphycs_family;
|
||||
std::optional<uint32_t> graphics_family;
|
||||
std::optional<uint32_t> present_family;
|
||||
bool is_complete();
|
||||
bool is_complete() const;
|
||||
};
|
||||
struct SwapchainSupportDetails
|
||||
{
|
||||
@@ -43,14 +43,14 @@ class RenderEngine
|
||||
static VkVertexInputBindingDescription get_binding_description();
|
||||
static std::array<VkVertexInputAttributeDescription, 2> get_vertex_attribute_descriptions();
|
||||
};
|
||||
const std::vector<Vertex> vertices = {
|
||||
std::vector<Vertex> vertices = {
|
||||
{{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}},
|
||||
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
|
||||
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
|
||||
};
|
||||
VkBuffer vertex_buffer_ = VK_NULL_HANDLE;
|
||||
VkDeviceMemory vertex_buffer_memory_ = VK_NULL_HANDLE;
|
||||
uint32_t find_memory_type(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
||||
uint32_t find_memory_type(uint32_t typeFilter, VkMemoryPropertyFlags properties) const;
|
||||
void allocate_vertex_buffer();
|
||||
#pragma endregion
|
||||
|
||||
@@ -82,10 +82,10 @@ class RenderEngine
|
||||
std::vector<VkFence> in_flight_fences_;
|
||||
|
||||
#ifdef _DEBUG
|
||||
VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info);
|
||||
VkResult enable_layer_validation(const VkDebugUtilsMessengerCreateInfoEXT* create_info);
|
||||
VkDebugUtilsMessengerEXT debug_messenger_;
|
||||
#endif
|
||||
SwapchainSupportDetails query_swapchain_details();
|
||||
SwapchainSupportDetails query_swapchain_details() const;
|
||||
|
||||
static std::vector<const char*> get_required_extensions();
|
||||
static bool is_device_suitable(VkPhysicalDevice device, VkSurfaceKHR surface);
|
||||
@@ -94,7 +94,7 @@ class RenderEngine
|
||||
static VkSurfaceFormatKHR choose_surface_format(const std::vector<VkSurfaceFormatKHR>& surface_formats);
|
||||
static VkPresentModeKHR choose_present_mode(const std::vector<VkPresentModeKHR>& present_modes, VkPresentModeKHR desired_present_mode);
|
||||
static VkExtent2D choose_extent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window);
|
||||
VkShaderModule create_shader_module(const std::string code);
|
||||
VkShaderModule create_shader_module(const std::string& code) const;
|
||||
|
||||
void create_instance();
|
||||
void create_surface();
|
||||
@@ -109,7 +109,7 @@ class RenderEngine
|
||||
void allocate_command_buffers();
|
||||
void create_sync_objects();
|
||||
|
||||
void record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index);
|
||||
void record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) const;
|
||||
void draw_frame();
|
||||
public:
|
||||
// Can throw the exception
|
||||
@@ -117,5 +117,5 @@ public:
|
||||
~RenderEngine();
|
||||
|
||||
void start();
|
||||
void stop_render();
|
||||
void stop_render() const;
|
||||
};
|
||||
Reference in New Issue
Block a user