Fixed uneeded copies of TriangleVertices
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
#include "VulkanContext.h"
|
||||
#include "VulkanCommandBuffers.h"
|
||||
#include "VulkanDeviceManager.h"
|
||||
#include "VulkanFramebuffers.h"
|
||||
#include "VulkanSwapChain.h"
|
||||
#include "VulkanVertexBuffer.h"
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
#include "utilities/Logger.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
VulkanContext::VulkanContext()
|
||||
@@ -16,7 +20,7 @@ VulkanContext::~VulkanContext()
|
||||
{
|
||||
}
|
||||
|
||||
void VulkanContext::Initialize(FVulkanConfig& InConfig)
|
||||
void VulkanContext::Initialize(FVulkanConfig& InConfig, const std::vector<Vertex>& InVertices)
|
||||
{
|
||||
Config = InConfig;
|
||||
|
||||
@@ -40,15 +44,18 @@ void VulkanContext::Initialize(FVulkanConfig& InConfig)
|
||||
DeviceManager.PickPhysicalDevice();
|
||||
DeviceManager.CreateLogicalDevice();
|
||||
|
||||
auto SwapChainSupport = DeviceManager.QuerySwapChainSupport(DeviceManager.GetPhysicalDevice());
|
||||
|
||||
SwapChain.Initialize(FSwapConfig(
|
||||
DeviceManager.GetDevice(),
|
||||
Surface,
|
||||
Config.Window,
|
||||
DeviceManager.GetPhysicalQueueFamilies().GraphicsFamily,
|
||||
DeviceManager.GetPhysicalQueueFamilies().PresentFamily,
|
||||
DeviceManager.GetSwapChainSupport().Capabilities,
|
||||
DeviceManager.GetSwapChainSupport().Formats,
|
||||
DeviceManager.GetSwapChainSupport().PresentModes));
|
||||
SwapChainSupport.Capabilities,
|
||||
SwapChainSupport.Formats,
|
||||
SwapChainSupport.PresentModes));
|
||||
|
||||
SwapChain.CreateSwapChain();
|
||||
SwapChain.CreateImageViews();
|
||||
|
||||
@@ -69,15 +76,46 @@ void VulkanContext::Initialize(FVulkanConfig& InConfig)
|
||||
CommandBuffers.CreateCommandPool(DeviceManager.GetPhysicalQueueFamilies().GraphicsFamily);
|
||||
|
||||
VertexBuffer.Initialize(FVertexBufferConfig(DeviceManager.GetDevice(), DeviceManager.GetPhysicalDevice()));
|
||||
VertexBuffer.CreateVertexBuffer(Config.Vertices);
|
||||
VertexBuffer.CreateVertexBuffer(InVertices);
|
||||
|
||||
CommandBuffers.CreateCommandBuffers(MAX_FRAMES_IN_FLIGHT);
|
||||
|
||||
CreateSyncObjects();
|
||||
|
||||
VkDescriptorPoolSize PoolSizes[] = {
|
||||
{ VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
|
||||
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
|
||||
};
|
||||
VkDescriptorPoolCreateInfo PoolInfo = {};
|
||||
PoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
PoolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
||||
PoolInfo.maxSets = 1000;
|
||||
PoolInfo.poolSizeCount = (uint32_t)std::size(PoolSizes);
|
||||
PoolInfo.pPoolSizes = PoolSizes;
|
||||
vkCreateDescriptorPool(DeviceManager.GetDevice(), &PoolInfo, nullptr, &ImGuiPool);
|
||||
}
|
||||
|
||||
void VulkanContext::Cleanup()
|
||||
{
|
||||
if (bImGuiVulkanInitialized)
|
||||
{
|
||||
ImGui_ImplVulkan_Shutdown();
|
||||
}
|
||||
|
||||
if (bImGuiGlfwInitialized)
|
||||
{
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
}
|
||||
|
||||
CleanupSwapChain();
|
||||
|
||||
VertexBuffer.Cleanup();
|
||||
@@ -173,7 +211,7 @@ void VulkanContext::CreateSyncObjects()
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanContext::DrawFrame()
|
||||
void VulkanContext::DrawFrame(bool bDrawImGui, uint32_t InVerticesSize)
|
||||
{
|
||||
vkWaitForFences(DeviceManager.GetDevice(), 1, &InFlightFences[CurrentFrame], VK_TRUE, UINT64_MAX);
|
||||
|
||||
@@ -198,18 +236,22 @@ void VulkanContext::DrawFrame()
|
||||
|
||||
vkResetFences(DeviceManager.GetDevice(), 1, &InFlightFences[CurrentFrame]);
|
||||
|
||||
vkAcquireNextImageKHR(DeviceManager.GetDevice(), SwapChain.GetSwapChain(), UINT64_MAX, ImageAvailableSemaphores[CurrentFrame], VK_NULL_HANDLE, &ImageIndex);
|
||||
|
||||
vkResetCommandBuffer(CommandBuffers.GetCommandBuffer(CurrentFrame), 0);
|
||||
CommandBuffers.RecordCommandBuffer(
|
||||
|
||||
FRecordCommandBuffersParams Params{
|
||||
CommandBuffers.GetCommandBuffer(CurrentFrame),
|
||||
ImageIndex,
|
||||
VertexBuffer.GetVertexBuffer(),
|
||||
Config.Vertices,
|
||||
InVerticesSize,
|
||||
RenderPass.GetRenderPass(),
|
||||
SwapChain.GetSwapChainExtent(),
|
||||
GraphicsPipeline.GetGraphicsPipeline(),
|
||||
Framebuffers.GetSwapChainFrameBuffers());
|
||||
Framebuffers.GetSwapChainFrameBuffers(),
|
||||
bDrawImGui
|
||||
// DrawData
|
||||
};
|
||||
|
||||
CommandBuffers.RecordCommandBuffer(Params);
|
||||
|
||||
VkSubmitInfo SubmitInfo{};
|
||||
SubmitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
@@ -275,9 +317,71 @@ void VulkanContext::RecreateSwapChain()
|
||||
|
||||
CleanupSwapChain();
|
||||
|
||||
auto SwapChainSupport = DeviceManager.QuerySwapChainSupport(DeviceManager.GetPhysicalDevice());
|
||||
|
||||
SwapChain.Initialize(FSwapConfig(
|
||||
DeviceManager.GetDevice(),
|
||||
Surface,
|
||||
Config.Window,
|
||||
DeviceManager.GetPhysicalQueueFamilies().GraphicsFamily,
|
||||
DeviceManager.GetPhysicalQueueFamilies().PresentFamily,
|
||||
SwapChainSupport.Capabilities,
|
||||
SwapChainSupport.Formats,
|
||||
SwapChainSupport.PresentModes));
|
||||
SwapChain.CreateSwapChain();
|
||||
SwapChain.CreateImageViews();
|
||||
|
||||
Framebuffers.Initialize(FFramebufferConfig(
|
||||
DeviceManager.GetDevice(),
|
||||
RenderPass.GetRenderPass(),
|
||||
SwapChain.GetSwapChainImageViews(),
|
||||
SwapChain.GetSwapChainExtent()));
|
||||
Framebuffers.CreateFramebuffers();
|
||||
|
||||
InitImGui();
|
||||
}
|
||||
|
||||
void VulkanContext::InitImGui()
|
||||
{
|
||||
if (!bImGuiGlfwInitialized)
|
||||
{
|
||||
if (!ImGui_ImplGlfw_InitForVulkan(Config.Window, true))
|
||||
{
|
||||
Log::Error("Failed to initialize ImGui GLFW backend!");
|
||||
return;
|
||||
}
|
||||
bImGuiGlfwInitialized = true;
|
||||
}
|
||||
|
||||
if (bImGuiVulkanInitialized)
|
||||
{
|
||||
ImGui_ImplVulkan_Shutdown();
|
||||
bImGuiVulkanInitialized = false;
|
||||
}
|
||||
|
||||
ImGui_ImplVulkan_InitInfo ImGuiInitInfo = {};
|
||||
ImGuiInitInfo.Instance = InstanceManager.GetInstance();
|
||||
ImGuiInitInfo.PhysicalDevice = DeviceManager.GetPhysicalDevice();
|
||||
ImGuiInitInfo.Device = DeviceManager.GetDevice();
|
||||
ImGuiInitInfo.QueueFamily = DeviceManager.GetPhysicalQueueFamilies().GraphicsFamily.value_or(0);
|
||||
ImGuiInitInfo.Queue = DeviceManager.GetGraphicsQueue();
|
||||
ImGuiInitInfo.PipelineCache = VK_NULL_HANDLE;
|
||||
ImGuiInitInfo.DescriptorPool = ImGuiPool;
|
||||
ImGuiInitInfo.MinImageCount = MAX_FRAMES_IN_FLIGHT;
|
||||
ImGuiInitInfo.ImageCount = SwapChain.GetSwapChainImageViews().size();
|
||||
ImGuiInitInfo.Allocator = nullptr;
|
||||
ImGuiInitInfo.PipelineInfoMain.RenderPass = RenderPass.GetRenderPass();
|
||||
ImGuiInitInfo.PipelineInfoMain.Subpass = 0;
|
||||
ImGuiInitInfo.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
ImGuiInitInfo.CheckVkResultFn = nullptr;
|
||||
|
||||
if (!ImGui_ImplVulkan_Init(&ImGuiInitInfo))
|
||||
{
|
||||
Log::Error("Failed to reinitialize ImGui Vulkan backend!");
|
||||
return;
|
||||
}
|
||||
|
||||
bImGuiVulkanInitialized = true;
|
||||
}
|
||||
|
||||
void VulkanContext::CleanupSwapChain()
|
||||
|
||||
Reference in New Issue
Block a user