Fixed uneeded copies of TriangleVertices
This commit is contained in:
71
src/main.cpp
71
src/main.cpp
@@ -1,6 +1,10 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cstdint>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
|
||||
#include "utilities/Logger.h"
|
||||
#include "GlfwWindowManager.h"
|
||||
@@ -40,10 +44,42 @@ private:
|
||||
GlfwWindowManager WindowManager;
|
||||
VulkanContext VkContext;
|
||||
|
||||
bool bShowImGui = true;
|
||||
bool bShowDemo = false;
|
||||
|
||||
static void KeyCallback(GLFWwindow* Window, int Key, int Scancode, int Action, int Mods)
|
||||
{
|
||||
auto App = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(Window));
|
||||
if (Action == GLFW_PRESS || Action == GLFW_REPEAT)
|
||||
{
|
||||
App->OnKey(Key);
|
||||
}
|
||||
}
|
||||
|
||||
void OnKey(int Key)
|
||||
{
|
||||
switch (Key)
|
||||
{
|
||||
case GLFW_KEY_O:
|
||||
bShowImGui = !bShowImGui;
|
||||
Log::Info("ImGui visibility toggled: " + std::string(bShowImGui ? "ON" : "OFF"));
|
||||
break;
|
||||
case GLFW_KEY_L:
|
||||
bShowDemo = !bShowDemo;
|
||||
Log::Info("Demo window toggled: " + std::string(bShowDemo ? "ON" : "OFF"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Initialization()
|
||||
{
|
||||
InitGlfw();
|
||||
InitVulkan();
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
|
||||
VkContext.InitImGui();
|
||||
}
|
||||
|
||||
void InitVulkan()
|
||||
@@ -52,9 +88,8 @@ private:
|
||||
Settings.bValidationEnabled,
|
||||
Settings.bVerboseLogging,
|
||||
WindowManager.GetWindow(),
|
||||
TriangleVertices
|
||||
};
|
||||
VkContext.Initialize(Config);
|
||||
VkContext.Initialize(Config, TriangleVertices);
|
||||
}
|
||||
|
||||
void InitGlfw()
|
||||
@@ -70,6 +105,8 @@ private:
|
||||
|
||||
glfwSetWindowUserPointer(WindowManager.GetWindow(), this);
|
||||
glfwSetFramebufferSizeCallback(WindowManager.GetWindow(), FramebufferResizeCallback);
|
||||
|
||||
WindowManager.SetKeyCallback(KeyCallback);
|
||||
}
|
||||
|
||||
static void FramebufferResizeCallback(GLFWwindow* Window, int Width, int Height)
|
||||
@@ -78,25 +115,43 @@ private:
|
||||
App->VkContext.SetFramebufferResized(true);
|
||||
}
|
||||
|
||||
void DrawFrame()
|
||||
{
|
||||
VkContext.DrawFrame();
|
||||
}
|
||||
|
||||
void MainLoop()
|
||||
{
|
||||
VkPhysicalDeviceProperties Properties{};
|
||||
vkGetPhysicalDeviceProperties(VkContext.DeviceManager.GetPhysicalDevice(), &Properties);
|
||||
|
||||
while (!WindowManager.ShouldClose())
|
||||
{
|
||||
WindowManager.PollEvents();
|
||||
DrawFrame();
|
||||
|
||||
if (bShowImGui)
|
||||
{
|
||||
ImGui_ImplVulkan_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
if (bShowDemo)
|
||||
{
|
||||
ImGui::ShowDemoWindow();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImGui::Begin(Properties.deviceName);
|
||||
ImGui::Text("Application average %.1f FPS", ImGui::GetIO().Framerate);
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
VkContext.DrawFrame(bShowImGui, TriangleVertices.size());
|
||||
}
|
||||
}
|
||||
|
||||
void Cleanup()
|
||||
{
|
||||
Log::Info("Cleaning up...");
|
||||
|
||||
VkContext.Cleanup();
|
||||
WindowManager.Cleanup();
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "VulkanCommandBuffers.h"
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
#include "utilities/Logger.h"
|
||||
#include <cstdint>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
@@ -53,22 +55,14 @@ void VulkanCommandBuffers::CreateCommandBuffers(int FramesInFlight)
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanCommandBuffers::RecordCommandBuffer(
|
||||
VkCommandBuffer InCommandBuffer,
|
||||
uint32_t ImageIndex,
|
||||
VkBuffer InVertexBuffer,
|
||||
std::vector<Vertex> InVertices,
|
||||
VkRenderPass RenderPass,
|
||||
VkExtent2D SwapChainExtent,
|
||||
VkPipeline GraphicsPipeline,
|
||||
std::vector<VkFramebuffer> SwapChainFramebuffers)
|
||||
void VulkanCommandBuffers::RecordCommandBuffer(FRecordCommandBuffersParams& Params)
|
||||
{
|
||||
VkCommandBufferBeginInfo BeginInfo{};
|
||||
BeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
BeginInfo.flags = 0;
|
||||
BeginInfo.pInheritanceInfo = nullptr;
|
||||
|
||||
if (vkBeginCommandBuffer(InCommandBuffer, &BeginInfo) != VK_SUCCESS)
|
||||
if (vkBeginCommandBuffer(Params.InCommandBuffer, &BeginInfo) != VK_SUCCESS)
|
||||
{
|
||||
Log::Error("Failed to begin recording command buffer!");
|
||||
}
|
||||
@@ -80,41 +74,47 @@ void VulkanCommandBuffers::RecordCommandBuffer(
|
||||
VkRenderPassBeginInfo RenderPassInfo{};
|
||||
RenderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
RenderPassInfo.renderPass = RenderPass;
|
||||
RenderPassInfo.framebuffer = SwapChainFramebuffers[ImageIndex];
|
||||
RenderPassInfo.framebuffer = Params.SwapChainFramebuffers[Params.ImageIndex];
|
||||
RenderPassInfo.renderArea.offset = { 0, 0 };
|
||||
RenderPassInfo.renderArea.extent = { SwapChainExtent };
|
||||
RenderPassInfo.renderArea.extent = { Params.SwapChainExtent };
|
||||
|
||||
VkClearValue ClearColor = { { { 0.0f, 0.0f, 0.0f, 1.0f } } };
|
||||
RenderPassInfo.clearValueCount = 1;
|
||||
RenderPassInfo.pClearValues = &ClearColor;
|
||||
|
||||
vkCmdBeginRenderPass(InCommandBuffer, &RenderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
vkCmdBeginRenderPass(Params.InCommandBuffer, &RenderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
vkCmdBindPipeline(InCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, GraphicsPipeline);
|
||||
vkCmdBindPipeline(Params.InCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, Params.GraphicsPipeline);
|
||||
|
||||
VkViewport Viewport{};
|
||||
Viewport.x = 0.0f;
|
||||
Viewport.y = 0.0f;
|
||||
Viewport.width = static_cast<float>(SwapChainExtent.width);
|
||||
Viewport.height = static_cast<float>(SwapChainExtent.height);
|
||||
Viewport.width = static_cast<float>(Params.SwapChainExtent.width);
|
||||
Viewport.height = static_cast<float>(Params.SwapChainExtent.height);
|
||||
Viewport.minDepth = 0.0f;
|
||||
Viewport.maxDepth = 1.0f;
|
||||
vkCmdSetViewport(InCommandBuffer, 0, 1, &Viewport);
|
||||
vkCmdSetViewport(Params.InCommandBuffer, 0, 1, &Viewport);
|
||||
|
||||
VkRect2D Scissor{};
|
||||
Scissor.offset = { 0, 0 };
|
||||
Scissor.extent = SwapChainExtent;
|
||||
vkCmdSetScissor(InCommandBuffer, 0, 1, &Scissor);
|
||||
Scissor.extent = Params.SwapChainExtent;
|
||||
vkCmdSetScissor(Params.InCommandBuffer, 0, 1, &Scissor);
|
||||
|
||||
VkBuffer VertexBuffers[] = { InVertexBuffer };
|
||||
VkBuffer VertexBuffers[] = { Params.InVertexBuffer };
|
||||
VkDeviceSize Offsets[] = { 0 };
|
||||
vkCmdBindVertexBuffers(InCommandBuffer, 0, 1, VertexBuffers, Offsets);
|
||||
vkCmdBindVertexBuffers(Params.InCommandBuffer, 0, 1, VertexBuffers, Offsets);
|
||||
|
||||
vkCmdDraw(InCommandBuffer, static_cast<uint32_t>(InVertices.size()), 1, 0, 0);
|
||||
vkCmdDraw(Params.InCommandBuffer, static_cast<uint32_t>(Params.InVerticesSize), 1, 0, 0);
|
||||
|
||||
vkCmdEndRenderPass(InCommandBuffer);
|
||||
if (Params.bDrawImGui)
|
||||
{
|
||||
ImGui::Render();
|
||||
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), Params.InCommandBuffer);
|
||||
}
|
||||
|
||||
if (vkEndCommandBuffer(InCommandBuffer) != VK_SUCCESS)
|
||||
vkCmdEndRenderPass(Params.InCommandBuffer);
|
||||
|
||||
if (vkEndCommandBuffer(Params.InCommandBuffer) != VK_SUCCESS)
|
||||
{
|
||||
Log::Error("Failed to record command buffer!");
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -63,7 +63,6 @@ void VulkanDeviceManager::PickPhysicalDevice()
|
||||
if (Candidates.rbegin()->first > 0)
|
||||
{
|
||||
PhysicalDevice = Candidates.rbegin()->second;
|
||||
SwapChainSupport = QuerySwapChainSupport(PhysicalDevice);
|
||||
Log::Info("Suitable GPU found.");
|
||||
}
|
||||
else
|
||||
@@ -81,7 +80,7 @@ bool VulkanDeviceManager::IsDeviceSuitable(VkPhysicalDevice Device)
|
||||
bool bSwapChainAdequate = false;
|
||||
if (bExtensionsSupported)
|
||||
{
|
||||
SwapChainSupport = QuerySwapChainSupport(Device);
|
||||
SwapChainSupportDetails SwapChainSupport = QuerySwapChainSupport(Device);
|
||||
bSwapChainAdequate = !SwapChainSupport.Formats.empty() && !SwapChainSupport.PresentModes.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ void VulkanFramebuffers::CreateFramebuffers()
|
||||
}
|
||||
else
|
||||
{
|
||||
Log::Info("Successfully created framebuffers.");
|
||||
// Log::Info("Successfully created framebuffers.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <cstdint> // Necessary for uint32_t
|
||||
#include <limits> // Necessary for std::numeric_limits
|
||||
#include <algorithm> // Necessary for std::clamp
|
||||
#include <set>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
#include "utilities/Logger.h"
|
||||
@@ -23,16 +22,6 @@ void VulkanSwapChain::Initialize(FSwapConfig InSwapConfig)
|
||||
SwapConfig = InSwapConfig;
|
||||
}
|
||||
|
||||
// void VulkanSwapChain::Initialize(
|
||||
// VkDevice InDevice,
|
||||
// VkSurfaceKHR InSurface,
|
||||
// GLFWwindow* InWindow)
|
||||
// {
|
||||
// Device = InDevice;
|
||||
// Surface = InSurface;
|
||||
// Window = InWindow;
|
||||
// }
|
||||
|
||||
void VulkanSwapChain::Cleanup()
|
||||
{
|
||||
for (auto ImageView : SwapChainImageViews)
|
||||
@@ -139,7 +128,6 @@ void VulkanSwapChain::CreateSwapChain()
|
||||
CreateInfo.clipped = VK_TRUE;
|
||||
CreateInfo.oldSwapchain = VK_NULL_HANDLE;
|
||||
|
||||
Log::Info("ChooseSwapExtent 3");
|
||||
if (vkCreateSwapchainKHR(SwapConfig.Device, &CreateInfo, nullptr, &SwapChain) != VK_SUCCESS)
|
||||
{
|
||||
Log::Error("Failed to create swap chain.");
|
||||
@@ -193,11 +181,3 @@ void VulkanSwapChain::CreateImageViews()
|
||||
|
||||
Log::Info("Successfully created " + std::to_string(CreatedViews) + " image views.");
|
||||
}
|
||||
|
||||
// void VulkanSwapChain::RecreateSwapChain()
|
||||
// {
|
||||
// vkDeviceWaitIdle(SwapConfig.Device);
|
||||
//
|
||||
// CreateSwapChain();
|
||||
// CreateImageViews();
|
||||
// }
|
||||
|
||||
@@ -15,7 +15,7 @@ void VulkanVertexBuffer::Cleanup()
|
||||
vkFreeMemory(Config.Device, VertexBufferMemory, nullptr);
|
||||
}
|
||||
|
||||
void VulkanVertexBuffer::CreateVertexBuffer(std::vector<Vertex> InVertices)
|
||||
void VulkanVertexBuffer::CreateVertexBuffer(const std::vector<Vertex>& InVertices)
|
||||
{
|
||||
VkBufferCreateInfo BufferInfo{};
|
||||
BufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "VulkanVertexBuffer.h"
|
||||
#include <cstdint>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
@@ -9,6 +10,21 @@
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
struct FRecordCommandBuffersParams
|
||||
{
|
||||
VkCommandBuffer InCommandBuffer;
|
||||
uint32_t ImageIndex;
|
||||
VkBuffer InVertexBuffer;
|
||||
uint32_t InVerticesSize;
|
||||
VkRenderPass RenderPass;
|
||||
VkExtent2D SwapChainExtent;
|
||||
VkPipeline GraphicsPipeline;
|
||||
std::vector<VkFramebuffer> SwapChainFramebuffers;
|
||||
bool bDrawImGui = false;
|
||||
// std::vector<Vertex> InVertices;
|
||||
// ImDrawData* DrawData = nullptr;
|
||||
};
|
||||
|
||||
class VulkanCommandBuffers
|
||||
{
|
||||
public:
|
||||
@@ -22,15 +38,7 @@ public:
|
||||
|
||||
void CreateCommandBuffers(int FramesInFlight);
|
||||
|
||||
void RecordCommandBuffer(
|
||||
VkCommandBuffer InCommandBuffer,
|
||||
uint32_t ImageIndex,
|
||||
VkBuffer InVertexBuffer,
|
||||
std::vector<Vertex> InVertices,
|
||||
VkRenderPass RenderPass,
|
||||
VkExtent2D SwapChainExtent,
|
||||
VkPipeline GraphicsPipeline,
|
||||
std::vector<VkFramebuffer> SwapChainFramebuffers);
|
||||
void RecordCommandBuffer(FRecordCommandBuffersParams& Params);
|
||||
|
||||
std::vector<VkCommandBuffer> GetCommandBuffers() { return CommandBuffers; }
|
||||
VkCommandBuffer GetCommandBuffer(int i) { return CommandBuffers[i]; }
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "VulkanSwapChain.h"
|
||||
#include "VulkanVertexBuffer.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
@@ -22,10 +23,10 @@ static const std::vector<const char*> ValidationLayers = {
|
||||
|
||||
struct FVulkanConfig
|
||||
{
|
||||
bool bValidationEnabled = true;
|
||||
bool bVerboseLogging = false;
|
||||
GLFWwindow* Window = nullptr;
|
||||
std::vector<Vertex> Vertices;
|
||||
bool bValidationEnabled = true;
|
||||
bool bVerboseLogging = false;
|
||||
GLFWwindow* Window = nullptr;
|
||||
// std::vector<Vertex> Vertices;
|
||||
};
|
||||
|
||||
static FVulkanConfig Config = {};
|
||||
@@ -36,22 +37,6 @@ public:
|
||||
VulkanContext();
|
||||
~VulkanContext();
|
||||
|
||||
void Initialize(FVulkanConfig& InConfig);
|
||||
void Cleanup();
|
||||
|
||||
void CreateSurface(GLFWwindow* Window);
|
||||
void CreateSyncObjects();
|
||||
void DrawFrame();
|
||||
void RecreateSwapChain();
|
||||
void CleanupSwapChain();
|
||||
|
||||
VkSurfaceKHR GetSurface() { return Surface; };
|
||||
|
||||
void SetFramebufferResized(bool bResized) { bFramebufferResized = bResized; }
|
||||
|
||||
private:
|
||||
VkSurfaceKHR Surface = VK_NULL_HANDLE;
|
||||
|
||||
VulkanInstanceManager InstanceManager;
|
||||
VulkanDebugManager DebugManager;
|
||||
VulkanDeviceManager DeviceManager;
|
||||
@@ -61,16 +46,33 @@ private:
|
||||
VulkanFramebuffers Framebuffers;
|
||||
VulkanVertexBuffer VertexBuffer;
|
||||
VulkanCommandBuffers CommandBuffers;
|
||||
VkDescriptorPool ImGuiPool;
|
||||
|
||||
// VkDevice Device;
|
||||
// VkPhysicalDevice PhysicalDevice;
|
||||
// VkQueue GraphicsQueue;
|
||||
void Initialize(FVulkanConfig& InConfig, const std::vector<Vertex>& InVertices);
|
||||
void Cleanup();
|
||||
|
||||
void CreateSurface(GLFWwindow* Window);
|
||||
void CreateSyncObjects();
|
||||
void DrawFrame(bool bDrawImGui = false, uint32_t InVerticesSize = 0);
|
||||
void RecreateSwapChain();
|
||||
void InitImGui();
|
||||
void CleanupSwapChain();
|
||||
// void DrawFrame(ImDrawData* DrawData = nullptr);
|
||||
|
||||
VkSurfaceKHR GetSurface() { return Surface; };
|
||||
|
||||
void SetFramebufferResized(bool bResized) { bFramebufferResized = bResized; }
|
||||
|
||||
private:
|
||||
VkSurfaceKHR Surface = VK_NULL_HANDLE;
|
||||
|
||||
std::vector<VkSemaphore> ImageAvailableSemaphores;
|
||||
std::vector<VkSemaphore> RenderFinishedSemaphores;
|
||||
std::vector<VkFence> InFlightFences;
|
||||
|
||||
bool bFramebufferResized = false;
|
||||
bool bImGuiGlfwInitialized = false;
|
||||
bool bImGuiVulkanInitialized = false;
|
||||
|
||||
const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
uint32_t CurrentFrame = 0;
|
||||
|
||||
@@ -47,32 +47,24 @@ public:
|
||||
|
||||
void Cleanup();
|
||||
|
||||
void PickPhysicalDevice();
|
||||
void CreateLogicalDevice();
|
||||
void PickPhysicalDevice();
|
||||
void CreateLogicalDevice();
|
||||
SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice Device);
|
||||
|
||||
VkDevice GetDevice() { return Device; }
|
||||
VkPhysicalDevice GetPhysicalDevice() { return PhysicalDevice; }
|
||||
QueueFamilyIndices GetPhysicalQueueFamilies() { return PhysicalQueueFamilies; }
|
||||
VkQueue GetGraphicsQueue() { return GraphicsQueue; }
|
||||
VkQueue GetPresentQueue() { return PresentQueue; }
|
||||
SwapChainSupportDetails GetSwapChainSupport() { return SwapChainSupport; }
|
||||
VkDevice GetDevice() { return Device; }
|
||||
VkPhysicalDevice GetPhysicalDevice() { return PhysicalDevice; }
|
||||
QueueFamilyIndices GetPhysicalQueueFamilies() { return PhysicalQueueFamilies; }
|
||||
VkQueue GetGraphicsQueue() { return GraphicsQueue; }
|
||||
VkQueue GetPresentQueue() { return PresentQueue; }
|
||||
|
||||
private:
|
||||
FDeviceConfig DeviceConfig;
|
||||
|
||||
// VkInstance Instance = VK_NULL_HANDLE;
|
||||
VkDevice Device = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice PhysicalDevice = VK_NULL_HANDLE;
|
||||
VkQueue GraphicsQueue = VK_NULL_HANDLE;
|
||||
VkQueue PresentQueue = VK_NULL_HANDLE;
|
||||
QueueFamilyIndices PhysicalQueueFamilies;
|
||||
// VkSurfaceKHR Surface;
|
||||
|
||||
// GLFWwindow* Window = nullptr;
|
||||
|
||||
SwapChainSupportDetails SwapChainSupport;
|
||||
|
||||
// bool bEnableValidationLayers = false;
|
||||
|
||||
bool IsDeviceSuitable(VkPhysicalDevice Device);
|
||||
|
||||
@@ -81,6 +73,4 @@ private:
|
||||
bool CheckDeviceExtensionSupport(VkPhysicalDevice Device);
|
||||
|
||||
QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice Device);
|
||||
|
||||
SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice Device);
|
||||
};
|
||||
|
||||
@@ -12,22 +12,15 @@ public:
|
||||
|
||||
void Cleanup();
|
||||
|
||||
// void CreateRenderPass(VkFormat SwapChainImageFormat);
|
||||
|
||||
// void CreateFramebuffers(std::vector<VkImageView> SwapChainImageViews, VkExtent2D SwapChainExtent);
|
||||
|
||||
VkShaderModule CreateShaderModule(const std::vector<char>& Code);
|
||||
|
||||
void CreateGraphicsPipeline(VkExtent2D SwapChainExtent, VkRenderPass RenderPass);
|
||||
//
|
||||
// VkRenderPass GetRenderPass() { return RenderPass; }
|
||||
//
|
||||
|
||||
VkPipeline GetGraphicsPipeline() { return GraphicsPipeline; }
|
||||
|
||||
private:
|
||||
VkDevice Device;
|
||||
// VkRenderPass RenderPass;
|
||||
|
||||
VkPipelineLayout PipelineLayout;
|
||||
VkPipeline GraphicsPipeline;
|
||||
// std::vector<VkFramebuffer> SwapChainFrameBuffers;
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
|
||||
void Cleanup();
|
||||
|
||||
void CreateVertexBuffer(std::vector<Vertex> InVertices);
|
||||
void CreateVertexBuffer(const std::vector<Vertex>& InVertices);
|
||||
|
||||
uint32_t FindMemoryType(uint32_t TypeFilter, VkMemoryPropertyFlags Properties);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user