Make Vulkan class a global instance
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
#include "copium/core/Application.h"
|
||||
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/mesh/Vertex.h"
|
||||
#include "copium/mesh/VertexPassthrough.h"
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Window.h"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
@@ -40,7 +37,6 @@ namespace Copium
|
||||
|
||||
Application::Application()
|
||||
{
|
||||
InitializeVulkan();
|
||||
InitializeFrameBuffer();
|
||||
InitializeRenderer();
|
||||
InitializeGraphicsPipeline();
|
||||
@@ -52,56 +48,46 @@ namespace Copium
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
vkDeviceWaitIdle(vulkan->GetDevice());
|
||||
vkDeviceWaitIdle(Vulkan::GetDevice());
|
||||
}
|
||||
|
||||
bool Application::Update()
|
||||
{
|
||||
if (framebuffer->GetWidth() != vulkan->GetSwapChain().GetExtent().width || framebuffer->GetHeight() != vulkan->GetSwapChain().GetExtent().height)
|
||||
if (framebuffer->GetWidth() != Vulkan::GetSwapChain().GetExtent().width || framebuffer->GetHeight() != Vulkan::GetSwapChain().GetExtent().height)
|
||||
{
|
||||
framebuffer->Resize(vulkan->GetSwapChain().GetExtent().width / 8, vulkan->GetSwapChain().GetExtent().height / 8);
|
||||
framebuffer->Resize(Vulkan::GetSwapChain().GetExtent().width / 8, Vulkan::GetSwapChain().GetExtent().height / 8);
|
||||
descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
|
||||
}
|
||||
|
||||
if (!vulkan->GetSwapChain().BeginPresent())
|
||||
if (!Vulkan::GetSwapChain().BeginPresent())
|
||||
return true;
|
||||
|
||||
RecordCommandBuffer();
|
||||
vulkan->GetSwapChain().SubmitToGraphicsQueue(*commandBuffer);
|
||||
Vulkan::GetSwapChain().SubmitToGraphicsQueue(*commandBuffer);
|
||||
|
||||
vulkan->GetSwapChain().EndPresent();
|
||||
return !glfwWindowShouldClose(vulkan->GetWindow().GetWindow());
|
||||
}
|
||||
|
||||
void Application::InitializeVulkan()
|
||||
{
|
||||
vulkan = std::make_unique<Vulkan>();
|
||||
vulkan->SetInstance(std::make_unique<Instance>("Copium Engine"));
|
||||
vulkan->SetWindow(std::make_unique<Window>(*vulkan, "Copium Engine", 1920, 1080, Window::Mode::Windowed));
|
||||
vulkan->SetDevice(std::make_unique<Device>(*vulkan));
|
||||
vulkan->SetSwapChain(std::make_unique<SwapChain>(*vulkan));
|
||||
CP_ASSERT(vulkan->Valid(), "Vulkan Manager was not initialized correctly");
|
||||
Vulkan::GetSwapChain().EndPresent();
|
||||
return !glfwWindowShouldClose(Vulkan::GetWindow().GetWindow());
|
||||
}
|
||||
|
||||
void Application::InitializeFrameBuffer()
|
||||
{
|
||||
framebuffer = std::make_unique<Framebuffer>(*vulkan, vulkan->GetSwapChain().GetExtent().width, vulkan->GetSwapChain().GetExtent().height);
|
||||
framebuffer = std::make_unique<Framebuffer>(Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height);
|
||||
}
|
||||
|
||||
void Application::InitializeRenderer()
|
||||
{
|
||||
renderer = std::make_unique<Renderer>(*vulkan, framebuffer->GetRenderPass(), *descriptorPool);
|
||||
renderer = std::make_unique<Renderer>(framebuffer->GetRenderPass());
|
||||
}
|
||||
|
||||
void Application::InitializeTextureSampler()
|
||||
{
|
||||
texture2D = std::make_unique<Texture2D>(*vulkan, "res/textures/texture.png");
|
||||
texture2D2 = std::make_unique<Texture2D>(*vulkan, "res/textures/texture2.png");
|
||||
texture2D = std::make_unique<Texture2D>("res/textures/texture.png");
|
||||
texture2D2 = std::make_unique<Texture2D>("res/textures/texture2.png");
|
||||
}
|
||||
|
||||
void Application::InitializeDescriptorSets()
|
||||
{
|
||||
descriptorPool = std::make_unique<DescriptorPool>(*vulkan);
|
||||
descriptorPool = std::make_unique<DescriptorPool>();
|
||||
|
||||
descriptorSet = graphicsPipeline->CreateDescriptorSet(*descriptorPool, 0);
|
||||
descriptorSet->SetSampler(*texture2D, 1);
|
||||
@@ -116,22 +102,22 @@ namespace Copium
|
||||
{
|
||||
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
|
||||
creator.SetVertexDescriptor(Vertex::GetDescriptor());
|
||||
graphicsPipeline = std::make_unique<Pipeline>(*vulkan, creator);
|
||||
graphicsPipeline = std::make_unique<Pipeline>(creator);
|
||||
|
||||
PipelineCreator creatorPassthrough{vulkan->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
|
||||
PipelineCreator creatorPassthrough{Vulkan::GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
|
||||
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
|
||||
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*vulkan, creatorPassthrough);
|
||||
graphicsPipelinePassthrough = std::make_unique<Pipeline>(creatorPassthrough);
|
||||
}
|
||||
|
||||
void Application::InitializeMesh()
|
||||
{
|
||||
mesh = std::make_unique<Mesh>(*vulkan, vertices, indices);
|
||||
meshPassthrough = std::make_unique<Mesh>(*vulkan, verticesPassthrough, indicesPassthrough);
|
||||
mesh = std::make_unique<Mesh>(vertices, indices);
|
||||
meshPassthrough = std::make_unique<Mesh>(verticesPassthrough, indicesPassthrough);
|
||||
}
|
||||
|
||||
void Application::InitializeCommandBuffer()
|
||||
{
|
||||
commandBuffer = std::make_unique<CommandBuffer>(*vulkan, CommandBuffer::Type::Dynamic);
|
||||
commandBuffer = std::make_unique<CommandBuffer>(CommandBuffer::Type::Dynamic);
|
||||
}
|
||||
|
||||
void Application::RecordCommandBuffer()
|
||||
@@ -164,7 +150,7 @@ namespace Copium
|
||||
|
||||
framebuffer->Unbind(*commandBuffer);
|
||||
|
||||
vulkan->GetSwapChain().BeginFrameBuffer(*commandBuffer);
|
||||
Vulkan::GetSwapChain().BeginFrameBuffer(*commandBuffer);
|
||||
|
||||
graphicsPipelinePassthrough->Bind(*commandBuffer);
|
||||
graphicsPipelinePassthrough->SetDescriptorSet(*descriptorSetPassthrough);
|
||||
@@ -173,7 +159,7 @@ namespace Copium
|
||||
meshPassthrough->Bind(*commandBuffer);
|
||||
meshPassthrough->Render(*commandBuffer);
|
||||
|
||||
vulkan->GetSwapChain().EndFrameBuffer(*commandBuffer);
|
||||
Vulkan::GetSwapChain().EndFrameBuffer(*commandBuffer);
|
||||
commandBuffer->End();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/buffer/Framebuffer.h"
|
||||
#include "copium/buffer/IndexBuffer.h"
|
||||
#include "copium/buffer/UniformBuffer.h"
|
||||
#include "copium/buffer/VertexBuffer.h"
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/mesh/Mesh.h"
|
||||
#include "copium/pipeline/DescriptorPool.h"
|
||||
#include "copium/pipeline/DescriptorSet.h"
|
||||
#include "copium/pipeline/Pipeline.h"
|
||||
#include "copium/sampler/Texture2D.h"
|
||||
#include "copium/renderer/Renderer.h"
|
||||
#include "copium/sampler/Texture2D.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
@@ -20,8 +14,6 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(Application);
|
||||
private:
|
||||
std::unique_ptr<Vulkan> vulkan;
|
||||
std::unique_ptr<Instance> instance;
|
||||
std::unique_ptr<Renderer> renderer;
|
||||
std::unique_ptr<Framebuffer> framebuffer;
|
||||
std::unique_ptr<Texture2D> texture2D;
|
||||
@@ -42,7 +34,6 @@ namespace Copium
|
||||
|
||||
bool Update();
|
||||
private:
|
||||
void InitializeVulkan();
|
||||
void InitializeFrameBuffer();
|
||||
void InitializeRenderer();
|
||||
void InitializeTextureSampler();
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Copium
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger);
|
||||
private:
|
||||
Instance& instance;
|
||||
|
||||
VkDebugUtilsMessengerEXT debugMessenger;
|
||||
public:
|
||||
DebugMessenger(Instance& instance);
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/core/Window.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Device::Device(Vulkan& vulkan)
|
||||
: vulkan{vulkan}
|
||||
Device::Device()
|
||||
{
|
||||
SelectPhysicalDevice();
|
||||
InitializeLogicalDevice();
|
||||
@@ -60,11 +58,11 @@ namespace Copium
|
||||
void Device::SelectPhysicalDevice()
|
||||
{
|
||||
uint32_t deviceCount;
|
||||
vkEnumeratePhysicalDevices(vulkan.GetInstance(), &deviceCount, nullptr);
|
||||
vkEnumeratePhysicalDevices(Vulkan::GetInstance(), &deviceCount, nullptr);
|
||||
CP_ASSERT(deviceCount != 0, "SelectPhysicaDevice : No available devices support Vulkan");
|
||||
|
||||
std::vector<VkPhysicalDevice> devices(deviceCount);
|
||||
vkEnumeratePhysicalDevices(vulkan.GetInstance(), &deviceCount, devices.data());
|
||||
vkEnumeratePhysicalDevices(Vulkan::GetInstance(), &deviceCount, devices.data());
|
||||
CP_INFO("SelectPhysicaDevice : Available devices:");
|
||||
for (auto&& device : devices)
|
||||
{
|
||||
@@ -88,7 +86,7 @@ namespace Copium
|
||||
|
||||
void Device::InitializeLogicalDevice()
|
||||
{
|
||||
QueueFamiliesQuery query{vulkan.GetWindow().GetSurface(), physicalDevice};
|
||||
QueueFamiliesQuery query{Vulkan::GetWindow().GetSurface(), physicalDevice};
|
||||
|
||||
float queuePriority = 1.0f;
|
||||
|
||||
@@ -144,14 +142,14 @@ namespace Copium
|
||||
if (!deviceFeatures.fillModeNonSolid || !deviceFeatures.samplerAnisotropy)
|
||||
return false;
|
||||
|
||||
QueueFamiliesQuery query{vulkan.GetWindow().GetSurface(), device};
|
||||
QueueFamiliesQuery query{Vulkan::GetWindow().GetSurface(), device};
|
||||
if (!query.AllRequiredFamiliesSupported())
|
||||
return false;
|
||||
|
||||
if (!CheckDeviceExtensionSupport(device))
|
||||
return false;
|
||||
|
||||
SwapChainSupportDetails details{vulkan.GetWindow().GetSurface(), device};
|
||||
SwapChainSupportDetails details{Vulkan::GetWindow().GetSurface(), device};
|
||||
if (!details.Valid())
|
||||
return false;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/core/QueueFamilies.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
@@ -13,8 +12,6 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(Device);
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
VkPhysicalDevice physicalDevice;
|
||||
VkDevice device;
|
||||
VkCommandPool commandPool;
|
||||
@@ -27,7 +24,7 @@ namespace Copium
|
||||
// TODO end
|
||||
|
||||
public:
|
||||
Device(Vulkan& vulkan);
|
||||
Device();
|
||||
~Device();
|
||||
|
||||
VkQueue GetGraphicsQueue() const;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "Instance.h"
|
||||
|
||||
#include "copium/core/QueueFamilies.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
namespace Copium
|
||||
@@ -110,12 +109,4 @@ namespace Copium
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Instance::FramebufferResizeCallback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
Instance* instance = static_cast<Instance*>(glfwGetWindowUserPointer(window));
|
||||
// TODO: Fix swap chain resizing again
|
||||
// instance->swapChain->ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
|
||||
CP_UNIMPLEMENTED("Framebuffer resizing currently not supported");
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "copium/core/DebugMessenger.h"
|
||||
#include "copium/util/Timer.h"
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <set>
|
||||
|
||||
@@ -36,6 +35,5 @@ namespace Copium
|
||||
void InitializeDebugMessenger();
|
||||
std::vector<const char*> GetRequiredExtensions();
|
||||
bool CheckLayerSupport(const std::vector<const char*>& layers);
|
||||
static void FramebufferResizeCallback(GLFWwindow* window, int width, int height);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
#include "copium/core/SwapChain.h"
|
||||
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/QueueFamilies.h"
|
||||
#include "copium/core/Window.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/sampler/Image.h"
|
||||
|
||||
namespace Copium
|
||||
@@ -34,8 +32,7 @@ namespace Copium
|
||||
return !formats.empty() && !presentModes.empty();
|
||||
}
|
||||
|
||||
SwapChain::SwapChain(Vulkan& vulkan)
|
||||
: vulkan{vulkan}
|
||||
SwapChain::SwapChain()
|
||||
{
|
||||
Initialize();
|
||||
InitializeImageViews();
|
||||
@@ -49,12 +46,12 @@ namespace Copium
|
||||
{
|
||||
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
||||
{
|
||||
vkDestroyFence(vulkan.GetDevice(), inFlightFences[i], nullptr);
|
||||
vkDestroySemaphore(vulkan.GetDevice(), renderFinishedSemaphores[i], nullptr);
|
||||
vkDestroySemaphore(vulkan.GetDevice(), imageAvailableSemaphores[i], nullptr);
|
||||
vkDestroyFence(Vulkan::GetDevice(), inFlightFences[i], nullptr);
|
||||
vkDestroySemaphore(Vulkan::GetDevice(), renderFinishedSemaphores[i], nullptr);
|
||||
vkDestroySemaphore(Vulkan::GetDevice(), imageAvailableSemaphores[i], nullptr);
|
||||
}
|
||||
Destroy();
|
||||
vkDestroyRenderPass(vulkan.GetDevice(), renderPass, nullptr);
|
||||
vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr);
|
||||
}
|
||||
|
||||
void SwapChain::BeginFrameBuffer(const CommandBuffer& commandBuffer) const
|
||||
@@ -114,15 +111,15 @@ namespace Copium
|
||||
|
||||
bool SwapChain::BeginPresent()
|
||||
{
|
||||
vkWaitForFences(vulkan.GetDevice(), 1, &inFlightFences[flightIndex], VK_TRUE, UINT64_MAX);
|
||||
vkWaitForFences(Vulkan::GetDevice(), 1, &inFlightFences[flightIndex], VK_TRUE, UINT64_MAX);
|
||||
|
||||
VkResult result = vkAcquireNextImageKHR(vulkan.GetDevice(), handle, UINT64_MAX, imageAvailableSemaphores[flightIndex], VK_NULL_HANDLE, &imageIndex);
|
||||
VkResult result = vkAcquireNextImageKHR(Vulkan::GetDevice(), handle, UINT64_MAX, imageAvailableSemaphores[flightIndex], VK_NULL_HANDLE, &imageIndex);
|
||||
if (result == VK_ERROR_OUT_OF_DATE_KHR)
|
||||
{
|
||||
Recreate();
|
||||
return false;
|
||||
}
|
||||
vkResetFences(vulkan.GetDevice(), 1, &inFlightFences[flightIndex]);
|
||||
vkResetFences(Vulkan::GetDevice(), 1, &inFlightFences[flightIndex]);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -140,7 +137,7 @@ namespace Copium
|
||||
submitInfo.signalSemaphoreCount = 1;
|
||||
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[flightIndex];
|
||||
|
||||
CP_VK_ASSERT(vkQueueSubmit(vulkan.GetDevice().GetGraphicsQueue(), 1, &submitInfo, inFlightFences[flightIndex]), "SubmitGraphicsQueue : Failed to submit command buffer");
|
||||
CP_VK_ASSERT(vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, inFlightFences[flightIndex]), "SubmitGraphicsQueue : Failed to submit command buffer");
|
||||
}
|
||||
|
||||
void SwapChain::EndPresent()
|
||||
@@ -154,7 +151,7 @@ namespace Copium
|
||||
presentInfo.pImageIndices = &imageIndex;
|
||||
presentInfo.pResults = nullptr;
|
||||
|
||||
VkResult result = vkQueuePresentKHR(vulkan.GetDevice().GetPresentQueue(), &presentInfo);
|
||||
VkResult result = vkQueuePresentKHR(Vulkan::GetDevice().GetPresentQueue(), &presentInfo);
|
||||
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || resizeFramebuffer)
|
||||
{
|
||||
Recreate();
|
||||
@@ -173,14 +170,14 @@ namespace Copium
|
||||
{
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
glfwGetFramebufferSize(vulkan.GetWindow().GetWindow(), &width, &height);
|
||||
glfwGetFramebufferSize(Vulkan::GetWindow().GetWindow(), &width, &height);
|
||||
while (width == 0 || height == 0)
|
||||
{
|
||||
glfwGetFramebufferSize(vulkan.GetWindow().GetWindow(), &width, &height);
|
||||
glfwGetFramebufferSize(Vulkan::GetWindow().GetWindow(), &width, &height);
|
||||
glfwWaitEvents();
|
||||
}
|
||||
|
||||
vkDeviceWaitIdle(vulkan.GetDevice());
|
||||
vkDeviceWaitIdle(Vulkan::GetDevice());
|
||||
|
||||
Destroy();
|
||||
|
||||
@@ -197,11 +194,11 @@ namespace Copium
|
||||
|
||||
void SwapChain::Initialize()
|
||||
{
|
||||
SwapChainSupportDetails swapChainSupport{vulkan.GetWindow().GetSurface(), vulkan.GetDevice().GetPhysicalDevice()};
|
||||
SwapChainSupportDetails swapChainSupport{Vulkan::GetWindow().GetSurface(), Vulkan::GetDevice().GetPhysicalDevice()};
|
||||
|
||||
VkSurfaceFormatKHR format = SelectSwapSurfaceFormat(swapChainSupport.formats);
|
||||
VkPresentModeKHR presentMode = SelectSwapPresentMode(swapChainSupport.presentModes);
|
||||
extent = SelectSwapExtent(vulkan.GetWindow().GetWindow(), swapChainSupport.capabilities);
|
||||
extent = SelectSwapExtent(Vulkan::GetWindow().GetWindow(), swapChainSupport.capabilities);
|
||||
imageFormat = format.format;
|
||||
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
||||
if (swapChainSupport.capabilities.maxImageCount != 0)
|
||||
@@ -209,12 +206,12 @@ namespace Copium
|
||||
imageCount = std::min(imageCount, swapChainSupport.capabilities.maxImageCount);
|
||||
}
|
||||
|
||||
QueueFamiliesQuery queueFamilies{vulkan.GetWindow().GetSurface(), vulkan.GetDevice().GetPhysicalDevice()};
|
||||
QueueFamiliesQuery queueFamilies{Vulkan::GetWindow().GetSurface(), Vulkan::GetDevice().GetPhysicalDevice()};
|
||||
std::vector<uint32_t> queueFamilyIndices{queueFamilies.graphicsFamily.value(), queueFamilies.presentFamily.value()};
|
||||
|
||||
VkSwapchainCreateInfoKHR createInfo{};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
createInfo.surface = vulkan.GetWindow().GetSurface();
|
||||
createInfo.surface = Vulkan::GetWindow().GetSurface();
|
||||
createInfo.minImageCount = imageCount;
|
||||
createInfo.imageFormat = format.format;
|
||||
createInfo.imageColorSpace = format.colorSpace;
|
||||
@@ -239,11 +236,11 @@ namespace Copium
|
||||
createInfo.pQueueFamilyIndices = nullptr;
|
||||
}
|
||||
|
||||
CP_VK_ASSERT(vkCreateSwapchainKHR(vulkan.GetDevice(), &createInfo, nullptr, &handle), "Initialize : Failed to initialize the swapchain");
|
||||
CP_VK_ASSERT(vkCreateSwapchainKHR(Vulkan::GetDevice(), &createInfo, nullptr, &handle), "Initialize : Failed to initialize the swapchain");
|
||||
|
||||
vkGetSwapchainImagesKHR(vulkan.GetDevice(), handle, &imageCount, nullptr);
|
||||
vkGetSwapchainImagesKHR(Vulkan::GetDevice(), handle, &imageCount, nullptr);
|
||||
images.resize(imageCount);
|
||||
vkGetSwapchainImagesKHR(vulkan.GetDevice(), handle, &imageCount, images.data());
|
||||
vkGetSwapchainImagesKHR(Vulkan::GetDevice(), handle, &imageCount, images.data());
|
||||
}
|
||||
|
||||
void SwapChain::InitializeImageViews()
|
||||
@@ -251,13 +248,13 @@ namespace Copium
|
||||
imageViews.resize(images.size());
|
||||
for (size_t i = 0; i < images.size(); i++)
|
||||
{
|
||||
imageViews[i] = Image::InitializeImageView(vulkan, images[i], imageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
imageViews[i] = Image::InitializeImageView(images[i], imageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void SwapChain::InitializeDepthAttachment()
|
||||
{
|
||||
depthAttachment = std::make_unique<DepthAttachment>(vulkan, extent.width, extent.height);
|
||||
depthAttachment = std::make_unique<DepthAttachment>(extent.width, extent.height);
|
||||
}
|
||||
|
||||
void SwapChain::InitializeRenderPass()
|
||||
@@ -273,7 +270,7 @@ namespace Copium
|
||||
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
|
||||
VkAttachmentDescription depthAttachment{};
|
||||
depthAttachment.format = Image::SelectDepthFormat(vulkan);
|
||||
depthAttachment.format = Image::SelectDepthFormat();
|
||||
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
@@ -314,7 +311,7 @@ namespace Copium
|
||||
renderPassCreateInfo.dependencyCount = 1;
|
||||
renderPassCreateInfo.pDependencies = &dependency;
|
||||
|
||||
CP_VK_ASSERT(vkCreateRenderPass(vulkan.GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "InitializeRenderPass : Failed to initialze render pass");
|
||||
CP_VK_ASSERT(vkCreateRenderPass(Vulkan::GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "InitializeRenderPass : Failed to initialze render pass");
|
||||
}
|
||||
|
||||
void SwapChain::InitializeFramebuffers()
|
||||
@@ -334,7 +331,7 @@ namespace Copium
|
||||
createInfo.height = extent.height;
|
||||
createInfo.layers = 1;
|
||||
|
||||
CP_VK_ASSERT(vkCreateFramebuffer(vulkan.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize swap chain framebuffer");
|
||||
CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize swap chain framebuffer");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,14 +344,14 @@ namespace Copium
|
||||
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
||||
{
|
||||
CP_VK_ASSERT(vkCreateSemaphore(vulkan.GetDevice(), &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]), "InitializeSyncObjects : Failed to initialize available image semaphore");
|
||||
CP_VK_ASSERT(vkCreateSemaphore(vulkan.GetDevice(), &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]), "InitializeSyncObjects : Failed to initialize render finished semaphore");
|
||||
CP_VK_ASSERT(vkCreateSemaphore(Vulkan::GetDevice(), &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]), "InitializeSyncObjects : Failed to initialize available image semaphore");
|
||||
CP_VK_ASSERT(vkCreateSemaphore(Vulkan::GetDevice(), &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]), "InitializeSyncObjects : Failed to initialize render finished semaphore");
|
||||
|
||||
VkFenceCreateInfo fenceCreateInfo{};
|
||||
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
|
||||
CP_VK_ASSERT(vkCreateFence(vulkan.GetDevice(), &fenceCreateInfo, nullptr, &inFlightFences[i]), "InitializeSyncObjects : Failed to initialize in flight fence");
|
||||
CP_VK_ASSERT(vkCreateFence(Vulkan::GetDevice(), &fenceCreateInfo, nullptr, &inFlightFences[i]), "InitializeSyncObjects : Failed to initialize in flight fence");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,13 +359,13 @@ namespace Copium
|
||||
{
|
||||
for (auto&& framebuffer : framebuffers)
|
||||
{
|
||||
vkDestroyFramebuffer(vulkan.GetDevice(), framebuffer, nullptr);
|
||||
vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
|
||||
}
|
||||
for (auto&& swapChainImageView : imageViews)
|
||||
{
|
||||
vkDestroyImageView(vulkan.GetDevice(), swapChainImageView, nullptr);
|
||||
vkDestroyImageView(Vulkan::GetDevice(), swapChainImageView, nullptr);
|
||||
}
|
||||
vkDestroySwapchainKHR(vulkan.GetDevice(), handle, nullptr);
|
||||
vkDestroySwapchainKHR(Vulkan::GetDevice(), handle, nullptr);
|
||||
}
|
||||
|
||||
VkSurfaceFormatKHR SwapChain::SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/buffer/CommandBuffer.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/sampler/DepthAttachment.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
@@ -27,8 +26,6 @@ namespace Copium
|
||||
public:
|
||||
static const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
VkSwapchainKHR handle;
|
||||
VkRenderPass renderPass;
|
||||
VkFormat imageFormat;
|
||||
@@ -46,7 +43,7 @@ namespace Copium
|
||||
std::vector<VkFence> inFlightFences;
|
||||
|
||||
public:
|
||||
SwapChain(Vulkan& vulkan);
|
||||
SwapChain();
|
||||
~SwapChain();
|
||||
|
||||
void BeginFrameBuffer(const CommandBuffer& commandBuffer) const;
|
||||
|
||||
@@ -1,48 +1,44 @@
|
||||
#include "copium/core/Vulkan.h"
|
||||
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Window.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
void Vulkan::SetInstance(std::unique_ptr<Instance>&& instance)
|
||||
std::unique_ptr<Instance> Vulkan::instance;
|
||||
std::unique_ptr<Window> Vulkan::window;
|
||||
std::unique_ptr<Device> Vulkan::device;
|
||||
std::unique_ptr<SwapChain> Vulkan::swapChain;
|
||||
|
||||
void Vulkan::Initialize()
|
||||
{
|
||||
this->instance = std::move(instance);
|
||||
instance = std::make_unique<Instance>("Copium Engine");
|
||||
window = std::make_unique<Window>( "Copium Engine", 1920, 1080, Window::Mode::Windowed);
|
||||
device = std::make_unique<Device>();
|
||||
swapChain = std::make_unique<SwapChain>();
|
||||
}
|
||||
|
||||
void Vulkan::SetWindow(std::unique_ptr<Window>&& window)
|
||||
void Vulkan::Destroy()
|
||||
{
|
||||
this->window = std::move(window);
|
||||
swapChain.reset();
|
||||
device.reset();
|
||||
window.reset();
|
||||
instance.reset();
|
||||
}
|
||||
|
||||
void Vulkan::SetDevice(std::unique_ptr<Device>&& device)
|
||||
{
|
||||
this->device = std::move(device);
|
||||
}
|
||||
|
||||
void Vulkan::SetSwapChain(std::unique_ptr<SwapChain>&& swapChain)
|
||||
{
|
||||
this->swapChain = std::move(swapChain);
|
||||
}
|
||||
|
||||
Instance& Vulkan::GetInstance() const
|
||||
Instance& Vulkan::GetInstance()
|
||||
{
|
||||
return *instance;
|
||||
}
|
||||
|
||||
Window& Vulkan::GetWindow() const
|
||||
Window& Vulkan::GetWindow()
|
||||
{
|
||||
return *window;
|
||||
}
|
||||
|
||||
Device& Vulkan::GetDevice() const
|
||||
Device& Vulkan::GetDevice()
|
||||
{
|
||||
return *device;
|
||||
}
|
||||
|
||||
SwapChain& Vulkan::GetSwapChain() const
|
||||
SwapChain& Vulkan::GetSwapChain()
|
||||
{
|
||||
return *swapChain;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Window.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class Instance;
|
||||
class Window;
|
||||
class Device;
|
||||
class SwapChain;
|
||||
|
||||
class Vulkan
|
||||
{
|
||||
CP_STATIC_CLASS(Vulkan);
|
||||
private:
|
||||
std::unique_ptr<Instance> instance;
|
||||
std::unique_ptr<Window> window;
|
||||
std::unique_ptr<Device> device;
|
||||
std::unique_ptr<SwapChain> swapChain;
|
||||
static std::unique_ptr<Instance> instance;
|
||||
static std::unique_ptr<Window> window;
|
||||
static std::unique_ptr<Device> device;
|
||||
static std::unique_ptr<SwapChain> swapChain;
|
||||
public:
|
||||
void SetInstance(std::unique_ptr<Instance>&& instance);
|
||||
void SetWindow(std::unique_ptr<Window>&& window);
|
||||
void SetDevice(std::unique_ptr<Device>&& device);
|
||||
void SetSwapChain(std::unique_ptr<SwapChain>&& swapChain);
|
||||
Instance& GetInstance() const;
|
||||
Window& GetWindow() const;
|
||||
Device& GetDevice() const;
|
||||
SwapChain& GetSwapChain() const;
|
||||
bool Valid();
|
||||
static void Initialize();
|
||||
static void Destroy();
|
||||
static Instance& GetInstance();
|
||||
static Window& GetWindow();
|
||||
static Device& GetDevice();
|
||||
static SwapChain& GetSwapChain();
|
||||
static bool Valid();
|
||||
|
||||
};
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
#include "copium/core/Window.h"
|
||||
|
||||
#include "copium/core/Instance.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Window::Window(Vulkan& vulkan, const std::string& windowName, int width, int height, Mode mode)
|
||||
: vulkan{vulkan}
|
||||
Window::Window(const std::string& windowName, int width, int height, Mode mode)
|
||||
{
|
||||
InitializeWindow(windowName, width, height, mode);
|
||||
InitializeSurface();
|
||||
@@ -14,7 +12,7 @@ namespace Copium
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
vkDestroySurfaceKHR(vulkan.GetInstance(), surface, nullptr);
|
||||
vkDestroySurfaceKHR(Vulkan::GetInstance(), surface, nullptr);
|
||||
glfwDestroyWindow(window);
|
||||
}
|
||||
|
||||
@@ -66,13 +64,12 @@ namespace Copium
|
||||
|
||||
void Window::InitializeSurface()
|
||||
{
|
||||
CP_VK_ASSERT(glfwCreateWindowSurface(vulkan.GetInstance(), window, nullptr, &surface), "InitializeSurface : Failed to create Vulkan surface");
|
||||
CP_VK_ASSERT(glfwCreateWindowSurface(Vulkan::GetInstance(), window, nullptr, &surface), "InitializeSurface : Failed to create Vulkan surface");
|
||||
}
|
||||
|
||||
void Window::FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height)
|
||||
{
|
||||
Window* window = static_cast<Window*>(glfwGetWindowUserPointer(glfwWindow));
|
||||
window->vulkan.GetSwapChain().ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
|
||||
Vulkan::GetSwapChain().ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
@@ -16,13 +15,11 @@ namespace Copium
|
||||
Fullscreen, BorderlessWindowed, Windowed
|
||||
};
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
GLFWwindow* window;
|
||||
VkSurfaceKHR surface;
|
||||
|
||||
public:
|
||||
Window(Vulkan& vulkan, const std::string& windowName, int width, int height, Mode mode);
|
||||
Window(const std::string& windowName, int width, int height, Mode mode);
|
||||
~Window();
|
||||
|
||||
VkSurfaceKHR GetSurface() const;
|
||||
|
||||
Reference in New Issue
Block a user