From 431ad9c5732b84220d4c10dfbb18b7a20dbaca11 Mon Sep 17 00:00:00 2001 From: Thraix Date: Tue, 4 Apr 2023 21:14:01 +0200 Subject: [PATCH] Make Vulkan class a global instance --- CopiumEngine/CopiumEngine.vcxproj | 2 + CopiumEngine/src/copium/buffer/Buffer.cpp | 49 +++++++------- CopiumEngine/src/copium/buffer/Buffer.h | 7 +- .../src/copium/buffer/CommandBuffer.cpp | 28 ++++---- .../src/copium/buffer/CommandBuffer.h | 5 +- .../src/copium/buffer/CommandBufferScoped.cpp | 4 +- .../src/copium/buffer/CommandBufferScoped.h | 3 +- .../src/copium/buffer/Framebuffer.cpp | 29 ++++---- CopiumEngine/src/copium/buffer/Framebuffer.h | 5 +- .../src/copium/buffer/IndexBuffer.cpp | 4 +- CopiumEngine/src/copium/buffer/IndexBuffer.h | 3 +- .../copium/buffer/RendererVertexBuffer.cpp | 8 +-- .../src/copium/buffer/RendererVertexBuffer.h | 3 +- .../src/copium/buffer/UniformBuffer.cpp | 8 +-- .../src/copium/buffer/UniformBuffer.h | 5 +- .../src/copium/buffer/VertexBuffer.cpp | 4 +- CopiumEngine/src/copium/buffer/VertexBuffer.h | 3 +- CopiumEngine/src/copium/core/Application.cpp | 56 ++++++---------- CopiumEngine/src/copium/core/Application.h | 11 +-- CopiumEngine/src/copium/core/DebugMessenger.h | 1 + CopiumEngine/src/copium/core/Device.cpp | 16 ++--- CopiumEngine/src/copium/core/Device.h | 5 +- CopiumEngine/src/copium/core/Instance.cpp | 9 --- CopiumEngine/src/copium/core/Instance.h | 2 - CopiumEngine/src/copium/core/SwapChain.cpp | 67 +++++++++---------- CopiumEngine/src/copium/core/SwapChain.h | 5 +- CopiumEngine/src/copium/core/Vulkan.cpp | 42 ++++++------ CopiumEngine/src/copium/core/Vulkan.h | 36 +++++----- CopiumEngine/src/copium/core/Window.cpp | 13 ++-- CopiumEngine/src/copium/core/Window.h | 5 +- CopiumEngine/src/copium/main.cpp | 4 ++ CopiumEngine/src/copium/mesh/Mesh.h | 9 ++- .../src/copium/pipeline/DescriptorPool.cpp | 14 ++-- .../src/copium/pipeline/DescriptorPool.h | 5 +- .../src/copium/pipeline/DescriptorSet.cpp | 60 ++++++++--------- .../src/copium/pipeline/DescriptorSet.h | 6 +- CopiumEngine/src/copium/pipeline/Pipeline.cpp | 22 +++--- CopiumEngine/src/copium/pipeline/Pipeline.h | 5 +- .../src/copium/pipeline/PipelineCreator.h | 5 +- CopiumEngine/src/copium/pipeline/Shader.cpp | 11 ++- CopiumEngine/src/copium/pipeline/Shader.h | 5 +- .../src/copium/pipeline/ShaderReflector.h | 2 +- CopiumEngine/src/copium/renderer/Batch.cpp | 7 +- CopiumEngine/src/copium/renderer/Batch.h | 3 +- CopiumEngine/src/copium/renderer/Renderer.cpp | 20 +++--- CopiumEngine/src/copium/renderer/Renderer.h | 5 +- .../src/copium/sampler/ColorAttachment.cpp | 17 +++-- .../src/copium/sampler/ColorAttachment.h | 3 +- .../src/copium/sampler/DepthAttachment.cpp | 18 ++--- .../src/copium/sampler/DepthAttachment.h | 3 +- CopiumEngine/src/copium/sampler/Image.cpp | 34 +++++----- CopiumEngine/src/copium/sampler/Image.h | 13 ++-- CopiumEngine/src/copium/sampler/Sampler.cpp | 11 ++- CopiumEngine/src/copium/sampler/Sampler.h | 4 +- CopiumEngine/src/copium/sampler/Texture2D.cpp | 28 ++++---- CopiumEngine/src/copium/sampler/Texture2D.h | 5 +- 56 files changed, 333 insertions(+), 424 deletions(-) diff --git a/CopiumEngine/CopiumEngine.vcxproj b/CopiumEngine/CopiumEngine.vcxproj index 4490e3e..1aa92ae 100644 --- a/CopiumEngine/CopiumEngine.vcxproj +++ b/CopiumEngine/CopiumEngine.vcxproj @@ -119,6 +119,7 @@ true $(ProjectDir)ext/include/;C:/VulkanSDK/1.3.236.0/Include;%(AdditionalIncludeDirectories)$(ProjectDir)src/ stdcpp17 + true Console @@ -145,6 +146,7 @@ true $(ProjectDir)ext/include/;C:/VulkanSDK/1.3.236.0/Include;%(AdditionalIncludeDirectories)$(ProjectDir)src/ stdcpp17 + true Console diff --git a/CopiumEngine/src/copium/buffer/Buffer.cpp b/CopiumEngine/src/copium/buffer/Buffer.cpp index 1c2256d..53845d1 100644 --- a/CopiumEngine/src/copium/buffer/Buffer.cpp +++ b/CopiumEngine/src/copium/buffer/Buffer.cpp @@ -1,12 +1,11 @@ #include "copium/buffer/Buffer.h" -#include "copium/core/Device.h" -#include "copium/core/Instance.h" +#include "copium/core/Vulkan.h" namespace Copium { - Buffer::Buffer(Vulkan& vulkan, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count) - : vulkan{vulkan}, size{size}, count{count} + Buffer::Buffer(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count) + : size{size}, count{count} { VkBufferCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; @@ -14,25 +13,25 @@ namespace Copium createInfo.usage = usage; createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - CP_VK_ASSERT(vkCreateBuffer(vulkan.GetDevice(), &createInfo, nullptr, &handle), "Buffer : Failed to initialize buffer"); + CP_VK_ASSERT(vkCreateBuffer(Vulkan::GetDevice(), &createInfo, nullptr, &handle), "Buffer : Failed to initialize buffer"); VkMemoryRequirements memoryRequirements; - vkGetBufferMemoryRequirements(vulkan.GetDevice(), handle, &memoryRequirements); + vkGetBufferMemoryRequirements(Vulkan::GetDevice(), handle, &memoryRequirements); VkMemoryAllocateInfo allocateInfo{}; allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocateInfo.allocationSize = memoryRequirements.size; - allocateInfo.memoryTypeIndex = vulkan.GetDevice().FindMemoryType(memoryRequirements.memoryTypeBits, properties); + allocateInfo.memoryTypeIndex = Vulkan::GetDevice().FindMemoryType(memoryRequirements.memoryTypeBits, properties); - CP_VK_ASSERT(vkAllocateMemory(vulkan.GetDevice(), &allocateInfo, nullptr, &memory), "Buffer : Failed to allocate buffer memory"); + CP_VK_ASSERT(vkAllocateMemory(Vulkan::GetDevice(), &allocateInfo, nullptr, &memory), "Buffer : Failed to allocate buffer memory"); - vkBindBufferMemory(vulkan.GetDevice(), handle, memory, 0); + vkBindBufferMemory(Vulkan::GetDevice(), handle, memory, 0); } Buffer::~Buffer() { - vkFreeMemory(vulkan.GetDevice(), memory, nullptr); - vkDestroyBuffer(vulkan.GetDevice(), handle, nullptr); + vkFreeMemory(Vulkan::GetDevice(), memory, nullptr); + vkDestroyBuffer(Vulkan::GetDevice(), handle, nullptr); } void Buffer::Update(void* indexData, int index) @@ -42,9 +41,9 @@ namespace Copium if (mappedData == nullptr) { void* data; - vkMapMemory(vulkan.GetDevice(), memory, index * size, size, 0, &data); + vkMapMemory(Vulkan::GetDevice(), memory, index * size, size, 0, &data); memcpy(data, indexData, size); - vkUnmapMemory(vulkan.GetDevice(), memory); + vkUnmapMemory(Vulkan::GetDevice(), memory); } else { @@ -55,26 +54,26 @@ namespace Copium void Buffer::UpdateStaging(void* data) { VkDeviceSize bufferSize = size * count; - Buffer stagingBuffer{vulkan, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1}; + Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1}; stagingBuffer.Update(data, 0); - CopyBuffer(vulkan, stagingBuffer, *this, 0, bufferSize); + CopyBuffer(stagingBuffer, *this, 0, bufferSize); } void Buffer::UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size) { - Buffer stagingBuffer{vulkan, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, 1}; + Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, 1}; stagingBuffer.Update(data, 0); - CopyBuffer(vulkan, stagingBuffer, *this, offset, size); + CopyBuffer(stagingBuffer, *this, offset, size); } void* Buffer::Map() { CP_ASSERT(mappedData == nullptr, "Map : Mapping an already mapped buffer"); - vkMapMemory(vulkan.GetDevice(), memory, 0, size * count, 0, &mappedData); + vkMapMemory(Vulkan::GetDevice(), memory, 0, size * count, 0, &mappedData); return mappedData; } @@ -82,7 +81,7 @@ namespace Copium { CP_ASSERT(mappedData != nullptr, "Unmap : Unmapping an already unmapped buffer"); - vkUnmapMemory(vulkan.GetDevice(), memory); + vkUnmapMemory(Vulkan::GetDevice(), memory); mappedData = nullptr; } @@ -102,17 +101,17 @@ namespace Copium return size * (VkDeviceSize)index; } - void Buffer::CopyBuffer(Vulkan& vulkan, const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size) + void Buffer::CopyBuffer(const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size) { VkCommandBufferAllocateInfo allocateInfo{}; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocateInfo.commandPool = vulkan.GetDevice().GetCommandPool(); + allocateInfo.commandPool = Vulkan::GetDevice().GetCommandPool(); allocateInfo.commandBufferCount = 1; VkCommandBuffer commandBuffer; - CP_VK_ASSERT(vkAllocateCommandBuffers(vulkan.GetDevice(), &allocateInfo, &commandBuffer), "CopyBuffer : Failed to initialize command buffer"); + CP_VK_ASSERT(vkAllocateCommandBuffers(Vulkan::GetDevice(), &allocateInfo, &commandBuffer), "CopyBuffer : Failed to initialize command buffer"); VkCommandBufferBeginInfo beginInfo{}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -134,9 +133,9 @@ namespace Copium submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; - vkQueueSubmit(vulkan.GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE); - vkQueueWaitIdle(vulkan.GetDevice().GetGraphicsQueue()); + vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE); + vkQueueWaitIdle(Vulkan::GetDevice().GetGraphicsQueue()); - vkFreeCommandBuffers(vulkan.GetDevice(), vulkan.GetDevice().GetCommandPool(), 1, &commandBuffer); + vkFreeCommandBuffers(Vulkan::GetDevice(), Vulkan::GetDevice().GetCommandPool(), 1, &commandBuffer); } } diff --git a/CopiumEngine/src/copium/buffer/Buffer.h b/CopiumEngine/src/copium/buffer/Buffer.h index a3e6070..0722cfe 100644 --- a/CopiumEngine/src/copium/buffer/Buffer.h +++ b/CopiumEngine/src/copium/buffer/Buffer.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -11,8 +10,6 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Buffer); protected: - Vulkan& vulkan; - VkDeviceMemory memory; VkBuffer handle; VkDeviceSize size; @@ -21,7 +18,7 @@ namespace Copium void* mappedData = nullptr; public: - Buffer(Vulkan& vulkan, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count); + Buffer(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count); virtual ~Buffer(); void Update(void* indexData, int index); @@ -35,6 +32,6 @@ namespace Copium VkDeviceSize GetSize() const; VkDeviceSize GetPosition(int index) const; - static void CopyBuffer(Vulkan& vulkan, const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size); + static void CopyBuffer(const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size); }; } diff --git a/CopiumEngine/src/copium/buffer/CommandBuffer.cpp b/CopiumEngine/src/copium/buffer/CommandBuffer.cpp index 4eae0a7..f6d518d 100644 --- a/CopiumEngine/src/copium/buffer/CommandBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/CommandBuffer.cpp @@ -1,13 +1,11 @@ #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Device.h" -#include "copium/core/Instance.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" namespace Copium { - CommandBuffer::CommandBuffer(Vulkan& vulkan, Type type) - : vulkan{vulkan}, type{type} + CommandBuffer::CommandBuffer(Type type) + : type{type} { switch (type) { @@ -24,14 +22,14 @@ namespace Copium VkCommandBufferAllocateInfo allocateInfo{}; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocateInfo.commandPool = vulkan.GetDevice().GetCommandPool(); + allocateInfo.commandPool = Vulkan::GetDevice().GetCommandPool(); allocateInfo.commandBufferCount = commandBuffers.size(); - CP_VK_ASSERT(vkAllocateCommandBuffers(vulkan.GetDevice(), &allocateInfo, commandBuffers.data()), "CommandBuffer : Failed to allocate CommandBuffer"); + CP_VK_ASSERT(vkAllocateCommandBuffers(Vulkan::GetDevice(), &allocateInfo, commandBuffers.data()), "CommandBuffer : Failed to allocate CommandBuffer"); } CommandBuffer::~CommandBuffer() { - vkFreeCommandBuffers(vulkan.GetDevice(), vulkan.GetDevice().GetCommandPool(), commandBuffers.size(), commandBuffers.data()); + vkFreeCommandBuffers(Vulkan::GetDevice(), Vulkan::GetDevice().GetCommandPool(), commandBuffers.size(), commandBuffers.data()); } // TODO: Test as constexpr function to see if it avoids the switch case @@ -53,13 +51,13 @@ namespace Copium CP_ABORT("Begin : Unreachable switch case"); } - vkResetCommandBuffer(commandBuffers[vulkan.GetSwapChain().GetFlightIndex()], 0); - CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[vulkan.GetSwapChain().GetFlightIndex()], &beginInfo), "Begin : Failed to begin command buffer"); + vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0); + CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], &beginInfo), "Begin : Failed to begin command buffer"); } void CommandBuffer::End() { - vkEndCommandBuffer(commandBuffers[vulkan.GetSwapChain().GetFlightIndex()]); + vkEndCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]); } void CommandBuffer::Submit() @@ -67,15 +65,15 @@ namespace Copium VkSubmitInfo submitInfo{}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.commandBufferCount = 1; - submitInfo.pCommandBuffers = &commandBuffers[vulkan.GetSwapChain().GetFlightIndex()]; + submitInfo.pCommandBuffers = &commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]; - vkQueueSubmit(vulkan.GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE); + vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE); // TODO: if singleUse? - vkQueueWaitIdle(vulkan.GetDevice().GetGraphicsQueue()); + vkQueueWaitIdle(Vulkan::GetDevice().GetGraphicsQueue()); } CommandBuffer::operator VkCommandBuffer() const { - return commandBuffers[vulkan.GetSwapChain().GetFlightIndex()]; + return commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]; } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/buffer/CommandBuffer.h b/CopiumEngine/src/copium/buffer/CommandBuffer.h index 67ce2cb..907481c 100644 --- a/CopiumEngine/src/copium/buffer/CommandBuffer.h +++ b/CopiumEngine/src/copium/buffer/CommandBuffer.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -16,13 +15,11 @@ namespace Copium SingleUse, Dynamic }; private: - Vulkan& vulkan; - std::vector commandBuffers; const Type type; public: - CommandBuffer(Vulkan& vulkan, Type type); + CommandBuffer(Type type); virtual ~CommandBuffer(); void Begin(); diff --git a/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp b/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp index dcf9512..023a749 100644 --- a/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp +++ b/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp @@ -2,8 +2,8 @@ namespace Copium { - CommandBufferScoped::CommandBufferScoped(Vulkan& vulkan) - : CommandBuffer{vulkan, Type::SingleUse} + CommandBufferScoped::CommandBufferScoped() + : CommandBuffer{Type::SingleUse} { CommandBuffer::Begin(); } diff --git a/CopiumEngine/src/copium/buffer/CommandBufferScoped.h b/CopiumEngine/src/copium/buffer/CommandBufferScoped.h index e17b5c6..442ba16 100644 --- a/CopiumEngine/src/copium/buffer/CommandBufferScoped.h +++ b/CopiumEngine/src/copium/buffer/CommandBufferScoped.h @@ -1,7 +1,6 @@ #pragma once #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" namespace Copium @@ -10,7 +9,7 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped); public: - CommandBufferScoped(Vulkan& vulkan); + CommandBufferScoped(); ~CommandBufferScoped() override; }; diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.cpp b/CopiumEngine/src/copium/buffer/Framebuffer.cpp index 811f8c6..6a2d2ba 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.cpp +++ b/CopiumEngine/src/copium/buffer/Framebuffer.cpp @@ -1,14 +1,13 @@ #include "copium/buffer/Framebuffer.h" #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Device.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" #include "copium/sampler/Image.h" namespace Copium { - Framebuffer::Framebuffer(Vulkan& vulkan, uint32_t width, uint32_t height) - : vulkan{vulkan}, width{width}, height{height} + Framebuffer::Framebuffer(uint32_t width, uint32_t height) + : width{width}, height{height} { InitializeImage(); InitializeDepthBuffer(); @@ -19,19 +18,19 @@ namespace Copium Framebuffer::~Framebuffer() { for (auto& framebuffer : framebuffers) - vkDestroyFramebuffer(vulkan.GetDevice(), framebuffer, nullptr); - vkDestroyRenderPass(vulkan.GetDevice(), renderPass, nullptr); + vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr); + vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr); } void Framebuffer::Resize(uint32_t width, uint32_t height) { - vkDeviceWaitIdle(vulkan.GetDevice()); + vkDeviceWaitIdle(Vulkan::GetDevice()); this->width = width; this->height = height; colorAttachment.reset(); depthAttachment.reset(); for (auto&& framebuffer : framebuffers) - vkDestroyFramebuffer(vulkan.GetDevice(), framebuffer, nullptr); + vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr); InitializeImage(); InitializeDepthBuffer(); InitializeFramebuffers(); @@ -46,7 +45,7 @@ namespace Copium VkRenderPassBeginInfo renderPassBeginInfo{}; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.renderPass = renderPass; - renderPassBeginInfo.framebuffer = framebuffers[vulkan.GetSwapChain().GetFlightIndex()]; + renderPassBeginInfo.framebuffer = framebuffers[Vulkan::GetSwapChain().GetFlightIndex()]; ; renderPassBeginInfo.renderArea.offset = {0, 0}; renderPassBeginInfo.renderArea.extent = {width, height}; @@ -80,7 +79,7 @@ namespace Copium VkFramebuffer Framebuffer::GetFramebuffer() const { - return framebuffers[vulkan.GetSwapChain().GetFlightIndex()]; + return framebuffers[Vulkan::GetSwapChain().GetFlightIndex()]; } const ColorAttachment& Framebuffer::GetColorAttachment() const @@ -100,12 +99,12 @@ namespace Copium void Framebuffer::InitializeImage() { - colorAttachment = std::make_unique(vulkan, width, height); + colorAttachment = std::make_unique(width, height); } void Framebuffer::InitializeDepthBuffer() { - depthAttachment = std::make_unique(vulkan, width, height); + depthAttachment = std::make_unique(width, height); } void Framebuffer::InitializeRenderPass() @@ -121,7 +120,7 @@ namespace Copium colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; 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; @@ -169,7 +168,7 @@ namespace Copium renderPassCreateInfo.dependencyCount = dependencies.size(); renderPassCreateInfo.pDependencies = dependencies.data(); - 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 Framebuffer::InitializeFramebuffers() @@ -188,7 +187,7 @@ namespace Copium createInfo.height = height; createInfo.layers = 1; - CP_VK_ASSERT(vkCreateFramebuffer(vulkan.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize framebuffer"); + CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize framebuffer"); } } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.h b/CopiumEngine/src/copium/buffer/Framebuffer.h index 74a79b3..f0a57e1 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.h +++ b/CopiumEngine/src/copium/buffer/Framebuffer.h @@ -1,7 +1,6 @@ #pragma once #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/sampler/ColorAttachment.h" #include "copium/sampler/DepthAttachment.h" #include "copium/util/Common.h" @@ -14,8 +13,6 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer); private: - Vulkan& vulkan; - std::unique_ptr colorAttachment; std::unique_ptr depthAttachment; std::vector framebuffers; @@ -24,7 +21,7 @@ namespace Copium uint32_t width; uint32_t height; public: - Framebuffer(Vulkan& vulkan, uint32_t width, uint32_t height); + Framebuffer(uint32_t width, uint32_t height); ~Framebuffer(); void Resize(uint32_t width, uint32_t height); diff --git a/CopiumEngine/src/copium/buffer/IndexBuffer.cpp b/CopiumEngine/src/copium/buffer/IndexBuffer.cpp index 9f1cd96..37ab256 100644 --- a/CopiumEngine/src/copium/buffer/IndexBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/IndexBuffer.cpp @@ -4,8 +4,8 @@ namespace Copium { - IndexBuffer::IndexBuffer(Vulkan& vulkan, int indexCount) - : Buffer{vulkan, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexCount * sizeof(uint16_t), 1}, + IndexBuffer::IndexBuffer(int indexCount) + : Buffer{VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexCount * sizeof(uint16_t), 1}, indexCount{indexCount} {} diff --git a/CopiumEngine/src/copium/buffer/IndexBuffer.h b/CopiumEngine/src/copium/buffer/IndexBuffer.h index ef6454b..3c45871 100644 --- a/CopiumEngine/src/copium/buffer/IndexBuffer.h +++ b/CopiumEngine/src/copium/buffer/IndexBuffer.h @@ -2,7 +2,6 @@ #include "copium/buffer/Buffer.h" #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" namespace Copium @@ -13,7 +12,7 @@ namespace Copium private: int indexCount; public: - IndexBuffer(Vulkan& vulkan, int indexCount); + IndexBuffer(int indexCount); void Bind(const CommandBuffer& commandBuffer); void Draw(const CommandBuffer& commandBuffer); diff --git a/CopiumEngine/src/copium/buffer/RendererVertexBuffer.cpp b/CopiumEngine/src/copium/buffer/RendererVertexBuffer.cpp index ca7a39b..628720b 100644 --- a/CopiumEngine/src/copium/buffer/RendererVertexBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/RendererVertexBuffer.cpp @@ -1,17 +1,17 @@ #include "copium/buffer/RendererVertexBuffer.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" namespace Copium { - RendererVertexBuffer::RendererVertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount) - : Buffer{vulkan, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, descriptor.GetVertexSize() * vertexCount, SwapChain::MAX_FRAMES_IN_FLIGHT} + RendererVertexBuffer::RendererVertexBuffer(const VertexDescriptor& descriptor, int vertexCount) + : Buffer{VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, descriptor.GetVertexSize() * vertexCount, SwapChain::MAX_FRAMES_IN_FLIGHT} {} void RendererVertexBuffer::Bind(const CommandBuffer& commandBuffer) { - VkDeviceSize offset = GetPosition(vulkan.GetSwapChain().GetFlightIndex()); + VkDeviceSize offset = GetPosition(Vulkan::GetSwapChain().GetFlightIndex()); vkCmdBindVertexBuffers(commandBuffer, 0, 1, &handle, &offset); } } diff --git a/CopiumEngine/src/copium/buffer/RendererVertexBuffer.h b/CopiumEngine/src/copium/buffer/RendererVertexBuffer.h index 5781ca7..6f29244 100644 --- a/CopiumEngine/src/copium/buffer/RendererVertexBuffer.h +++ b/CopiumEngine/src/copium/buffer/RendererVertexBuffer.h @@ -2,7 +2,6 @@ #include "copium/buffer/Buffer.h" #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/pipeline/VertexDescriptor.h" namespace Copium @@ -11,7 +10,7 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(RendererVertexBuffer); public: - RendererVertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount); + RendererVertexBuffer(const VertexDescriptor& descriptor, int vertexCount); void Bind(const CommandBuffer& commandBuffer); }; diff --git a/CopiumEngine/src/copium/buffer/UniformBuffer.cpp b/CopiumEngine/src/copium/buffer/UniformBuffer.cpp index ef362c9..7108b03 100644 --- a/CopiumEngine/src/copium/buffer/UniformBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/UniformBuffer.cpp @@ -1,11 +1,11 @@ #include "copium/buffer/UniformBuffer.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" namespace Copium { - UniformBuffer::UniformBuffer(Vulkan& vulkan, ShaderBinding binding) - : Buffer{vulkan, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, binding.GetUniformBufferSize(), SwapChain::MAX_FRAMES_IN_FLIGHT}, binding{binding} + UniformBuffer::UniformBuffer(ShaderBinding binding) + : Buffer{VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, binding.GetUniformBufferSize(), SwapChain::MAX_FRAMES_IN_FLIGHT}, binding{binding} { buffer.resize(Buffer::GetSize()); } @@ -70,6 +70,6 @@ namespace Copium void UniformBuffer::Update() { - Buffer::Update(buffer.data(), vulkan.GetSwapChain().GetFlightIndex()); + Buffer::Update(buffer.data(), Vulkan::GetSwapChain().GetFlightIndex()); } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/buffer/UniformBuffer.h b/CopiumEngine/src/copium/buffer/UniformBuffer.h index 1e0104c..9453b31 100644 --- a/CopiumEngine/src/copium/buffer/UniformBuffer.h +++ b/CopiumEngine/src/copium/buffer/UniformBuffer.h @@ -1,7 +1,6 @@ #pragma once #include "copium/buffer/Buffer.h" -#include "copium/core/Vulkan.h" #include "copium/pipeline/ShaderBinding.h" #include "copium/util/Common.h" @@ -13,12 +12,12 @@ namespace Copium class UniformBuffer final : public Buffer { CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer); - + private: ShaderBinding binding; std::vector buffer; public: - UniformBuffer(Vulkan& vulkan, ShaderBinding binding); + UniformBuffer(ShaderBinding binding); VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const; diff --git a/CopiumEngine/src/copium/buffer/VertexBuffer.cpp b/CopiumEngine/src/copium/buffer/VertexBuffer.cpp index 52d01b4..4734c37 100644 --- a/CopiumEngine/src/copium/buffer/VertexBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/VertexBuffer.cpp @@ -2,8 +2,8 @@ namespace Copium { - VertexBuffer::VertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount) - : Buffer{vulkan, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, descriptor.GetVertexSize() * vertexCount, 1} + VertexBuffer::VertexBuffer(const VertexDescriptor& descriptor, int vertexCount) + : Buffer{VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, descriptor.GetVertexSize() * vertexCount, 1} { VkDeviceSize offset = 0; for (auto&& binding : descriptor.GetBindings()) diff --git a/CopiumEngine/src/copium/buffer/VertexBuffer.h b/CopiumEngine/src/copium/buffer/VertexBuffer.h index 27a041b..05547f5 100644 --- a/CopiumEngine/src/copium/buffer/VertexBuffer.h +++ b/CopiumEngine/src/copium/buffer/VertexBuffer.h @@ -2,7 +2,6 @@ #include "copium/buffer/Buffer.h" #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/pipeline/VertexDescriptor.h" #include "copium/util/Common.h" @@ -18,7 +17,7 @@ namespace Copium std::vector bindingOffsets; std::vector bindingSizes; public: - VertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount); + VertexBuffer(const VertexDescriptor& descriptor, int vertexCount); void Bind(const CommandBuffer& commandBuffer); void UpdateStaging(uint32_t binding, void* data); diff --git a/CopiumEngine/src/copium/core/Application.cpp b/CopiumEngine/src/copium/core/Application.cpp index e9da293..59f6af4 100644 --- a/CopiumEngine/src/copium/core/Application.cpp +++ b/CopiumEngine/src/copium/core/Application.cpp @@ -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 @@ -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->SetInstance(std::make_unique("Copium Engine")); - vulkan->SetWindow(std::make_unique(*vulkan, "Copium Engine", 1920, 1080, Window::Mode::Windowed)); - vulkan->SetDevice(std::make_unique(*vulkan)); - vulkan->SetSwapChain(std::make_unique(*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(*vulkan, vulkan->GetSwapChain().GetExtent().width, vulkan->GetSwapChain().GetExtent().height); + framebuffer = std::make_unique(Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height); } void Application::InitializeRenderer() { - renderer = std::make_unique(*vulkan, framebuffer->GetRenderPass(), *descriptorPool); + renderer = std::make_unique(framebuffer->GetRenderPass()); } void Application::InitializeTextureSampler() { - texture2D = std::make_unique(*vulkan, "res/textures/texture.png"); - texture2D2 = std::make_unique(*vulkan, "res/textures/texture2.png"); + texture2D = std::make_unique("res/textures/texture.png"); + texture2D2 = std::make_unique("res/textures/texture2.png"); } void Application::InitializeDescriptorSets() { - descriptorPool = std::make_unique(*vulkan); + descriptorPool = std::make_unique(); 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(*vulkan, creator); + graphicsPipeline = std::make_unique(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(*vulkan, creatorPassthrough); + graphicsPipelinePassthrough = std::make_unique(creatorPassthrough); } void Application::InitializeMesh() { - mesh = std::make_unique(*vulkan, vertices, indices); - meshPassthrough = std::make_unique(*vulkan, verticesPassthrough, indicesPassthrough); + mesh = std::make_unique(vertices, indices); + meshPassthrough = std::make_unique(verticesPassthrough, indicesPassthrough); } void Application::InitializeCommandBuffer() { - commandBuffer = std::make_unique(*vulkan, CommandBuffer::Type::Dynamic); + commandBuffer = std::make_unique(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(); } diff --git a/CopiumEngine/src/copium/core/Application.h b/CopiumEngine/src/copium/core/Application.h index be2f11c..2ac290d 100644 --- a/CopiumEngine/src/copium/core/Application.h +++ b/CopiumEngine/src/copium/core/Application.h @@ -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; - std::unique_ptr instance; std::unique_ptr renderer; std::unique_ptr framebuffer; std::unique_ptr texture2D; @@ -42,7 +34,6 @@ namespace Copium bool Update(); private: - void InitializeVulkan(); void InitializeFrameBuffer(); void InitializeRenderer(); void InitializeTextureSampler(); diff --git a/CopiumEngine/src/copium/core/DebugMessenger.h b/CopiumEngine/src/copium/core/DebugMessenger.h index eacf47b..1c9623d 100644 --- a/CopiumEngine/src/copium/core/DebugMessenger.h +++ b/CopiumEngine/src/copium/core/DebugMessenger.h @@ -13,6 +13,7 @@ namespace Copium CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger); private: Instance& instance; + VkDebugUtilsMessengerEXT debugMessenger; public: DebugMessenger(Instance& instance); diff --git a/CopiumEngine/src/copium/core/Device.cpp b/CopiumEngine/src/copium/core/Device.cpp index 584d036..0c5780a 100644 --- a/CopiumEngine/src/copium/core/Device.cpp +++ b/CopiumEngine/src/copium/core/Device.cpp @@ -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 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; diff --git a/CopiumEngine/src/copium/core/Device.h b/CopiumEngine/src/copium/core/Device.h index 0920c2b..24b6687 100644 --- a/CopiumEngine/src/copium/core/Device.h +++ b/CopiumEngine/src/copium/core/Device.h @@ -1,7 +1,6 @@ #pragma once #include "copium/core/QueueFamilies.h" -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -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; diff --git a/CopiumEngine/src/copium/core/Instance.cpp b/CopiumEngine/src/copium/core/Instance.cpp index 76768d5..31fd4cd 100644 --- a/CopiumEngine/src/copium/core/Instance.cpp +++ b/CopiumEngine/src/copium/core/Instance.cpp @@ -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(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"); - } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/core/Instance.h b/CopiumEngine/src/copium/core/Instance.h index 66e10eb..ba8bf5e 100644 --- a/CopiumEngine/src/copium/core/Instance.h +++ b/CopiumEngine/src/copium/core/Instance.h @@ -3,7 +3,6 @@ #include "copium/core/DebugMessenger.h" #include "copium/util/Timer.h" -#include #include #include @@ -36,6 +35,5 @@ namespace Copium void InitializeDebugMessenger(); std::vector GetRequiredExtensions(); bool CheckLayerSupport(const std::vector& layers); - static void FramebufferResizeCallback(GLFWwindow* window, int width, int height); }; } diff --git a/CopiumEngine/src/copium/core/SwapChain.cpp b/CopiumEngine/src/copium/core/SwapChain.cpp index 8590af9..9af1dc3 100644 --- a/CopiumEngine/src/copium/core/SwapChain.cpp +++ b/CopiumEngine/src/copium/core/SwapChain.cpp @@ -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 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(vulkan, extent.width, extent.height); + depthAttachment = std::make_unique(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& availableFormats) diff --git a/CopiumEngine/src/copium/core/SwapChain.h b/CopiumEngine/src/copium/core/SwapChain.h index 6c02c09..b033577 100644 --- a/CopiumEngine/src/copium/core/SwapChain.h +++ b/CopiumEngine/src/copium/core/SwapChain.h @@ -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 inFlightFences; public: - SwapChain(Vulkan& vulkan); + SwapChain(); ~SwapChain(); void BeginFrameBuffer(const CommandBuffer& commandBuffer) const; diff --git a/CopiumEngine/src/copium/core/Vulkan.cpp b/CopiumEngine/src/copium/core/Vulkan.cpp index 552e100..16fb8aa 100644 --- a/CopiumEngine/src/copium/core/Vulkan.cpp +++ b/CopiumEngine/src/copium/core/Vulkan.cpp @@ -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) + std::unique_ptr Vulkan::instance; + std::unique_ptr Vulkan::window; + std::unique_ptr Vulkan::device; + std::unique_ptr Vulkan::swapChain; + + void Vulkan::Initialize() { - this->instance = std::move(instance); + instance = std::make_unique("Copium Engine"); + window = std::make_unique( "Copium Engine", 1920, 1080, Window::Mode::Windowed); + device = std::make_unique(); + swapChain = std::make_unique(); } - void Vulkan::SetWindow(std::unique_ptr&& window) + void Vulkan::Destroy() { - this->window = std::move(window); + swapChain.reset(); + device.reset(); + window.reset(); + instance.reset(); } - void Vulkan::SetDevice(std::unique_ptr&& device) - { - this->device = std::move(device); - } - - void Vulkan::SetSwapChain(std::unique_ptr&& 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; } diff --git a/CopiumEngine/src/copium/core/Vulkan.h b/CopiumEngine/src/copium/core/Vulkan.h index 6e4aed9..85327f1 100644 --- a/CopiumEngine/src/copium/core/Vulkan.h +++ b/CopiumEngine/src/copium/core/Vulkan.h @@ -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 namespace Copium { - class Instance; - class Window; - class Device; - class SwapChain; - class Vulkan { + CP_STATIC_CLASS(Vulkan); private: - std::unique_ptr instance; - std::unique_ptr window; - std::unique_ptr device; - std::unique_ptr swapChain; + static std::unique_ptr instance; + static std::unique_ptr window; + static std::unique_ptr device; + static std::unique_ptr swapChain; public: - void SetInstance(std::unique_ptr&& instance); - void SetWindow(std::unique_ptr&& window); - void SetDevice(std::unique_ptr&& device); - void SetSwapChain(std::unique_ptr&& 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(); }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/core/Window.cpp b/CopiumEngine/src/copium/core/Window.cpp index 24c7858..b1d183d 100644 --- a/CopiumEngine/src/copium/core/Window.cpp +++ b/CopiumEngine/src/copium/core/Window.cpp @@ -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(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? } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/core/Window.h b/CopiumEngine/src/copium/core/Window.h index 6e60df9..d02846a 100644 --- a/CopiumEngine/src/copium/core/Window.h +++ b/CopiumEngine/src/copium/core/Window.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -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; diff --git a/CopiumEngine/src/copium/main.cpp b/CopiumEngine/src/copium/main.cpp index 8401cee..f33daf9 100644 --- a/CopiumEngine/src/copium/main.cpp +++ b/CopiumEngine/src/copium/main.cpp @@ -1,4 +1,5 @@ #include "copium/core/Application.h" +#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include "copium/util/Timer.h" @@ -7,6 +8,8 @@ int main() { CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context"); + + Copium::Vulkan::Initialize(); { Copium::Application application; Copium::Timer timer; @@ -23,6 +26,7 @@ int main() frames++; } } + Copium::Vulkan::Destroy(); glfwTerminate(); return 0; diff --git a/CopiumEngine/src/copium/mesh/Mesh.h b/CopiumEngine/src/copium/mesh/Mesh.h index d32788f..d530347 100644 --- a/CopiumEngine/src/copium/mesh/Mesh.h +++ b/CopiumEngine/src/copium/mesh/Mesh.h @@ -3,7 +3,6 @@ #include "copium/buffer/IndexBuffer.h" #include "copium/buffer/VertexBuffer.h" #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -19,18 +18,18 @@ namespace Copium std::unique_ptr vertexBuffer; public: template - Mesh(Vulkan& vulkan, const std::vector& vertices, const std::vector& indices); + Mesh(const std::vector& vertices, const std::vector& indices); void Bind(const CommandBuffer& commandBuffer); void Render(const CommandBuffer& commandBuffer); }; template - Mesh::Mesh(Vulkan& vulkan, const std::vector& vertices, const std::vector& indices) + Mesh::Mesh(const std::vector& vertices, const std::vector& indices) { - indexBuffer = std::make_unique(vulkan, indices.size()); + indexBuffer = std::make_unique(indices.size()); indexBuffer->UpdateStaging((void*)indices.data()); - vertexBuffer = std::make_unique(vulkan, T::GetDescriptor(), vertices.size()); + vertexBuffer = std::make_unique(T::GetDescriptor(), vertices.size()); vertexBuffer ->UpdateStaging(0, (void*)vertices.data()); } } diff --git a/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp b/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp index d5cb0ec..c5325b5 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp +++ b/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp @@ -1,12 +1,10 @@ #include "copium/pipeline/DescriptorPool.h" -#include "copium/core/Device.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" namespace Copium { - DescriptorPool::DescriptorPool(Vulkan& vulkan) - : vulkan{vulkan} + DescriptorPool::DescriptorPool() { std::vector poolSizes{2}; poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; @@ -22,12 +20,12 @@ namespace Copium createInfo.maxSets = DESCRIPTOR_SET_COUNT * SwapChain::MAX_FRAMES_IN_FLIGHT; createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; - CP_VK_ASSERT(vkCreateDescriptorPool(vulkan.GetDevice(), &createInfo, nullptr, &descriptorPool), "DescriptorPool : Failed to initialize descriptor pool"); + CP_VK_ASSERT(vkCreateDescriptorPool(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorPool), "DescriptorPool : Failed to initialize descriptor pool"); } DescriptorPool::~DescriptorPool() { - vkDestroyDescriptorPool(vulkan.GetDevice(), descriptorPool, nullptr); + vkDestroyDescriptorPool(Vulkan::GetDevice(), descriptorPool, nullptr); } std::vector DescriptorPool::AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout) @@ -40,13 +38,13 @@ namespace Copium allocateInfo.descriptorSetCount = descriptorSets.size(); allocateInfo.pSetLayouts = layouts.data(); - CP_VK_ASSERT(vkAllocateDescriptorSets(vulkan.GetDevice(), &allocateInfo, descriptorSets.data()), "AllocateDescriptorSets : Failed to allocate descriptor sets"); + CP_VK_ASSERT(vkAllocateDescriptorSets(Vulkan::GetDevice(), &allocateInfo, descriptorSets.data()), "AllocateDescriptorSets : Failed to allocate descriptor sets"); return descriptorSets; } void DescriptorPool::FreeDescriptorSets(const std::vector& descriptorSets) { - vkFreeDescriptorSets(vulkan.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data()); + vkFreeDescriptorSets(Vulkan::GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data()); } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/pipeline/DescriptorPool.h b/CopiumEngine/src/copium/pipeline/DescriptorPool.h index 1d4c1e9..ed50131 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorPool.h +++ b/CopiumEngine/src/copium/pipeline/DescriptorPool.h @@ -1,7 +1,6 @@ #pragma once #include "copium/util/Common.h" -#include "copium/core/Vulkan.h" #include @@ -11,12 +10,10 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool); private: - Vulkan& vulkan; - VkDescriptorPool descriptorPool; static const int DESCRIPTOR_SET_COUNT = 100; public: - DescriptorPool(Vulkan& vulkan); + DescriptorPool(); ~DescriptorPool(); std::vector AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout); diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp index 221166d..a79c115 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp @@ -1,12 +1,11 @@ #include "copium/pipeline/DescriptorSet.h" -#include "copium/core/Device.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" namespace Copium { - DescriptorSet::DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set& bindings) - : vulkan{vulkan}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings} + DescriptorSet::DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set& bindings) + : descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings} { CP_ASSERT(!bindings.empty(), "DescriptorSet : cannot initialize DescriptorSet with empty ShaderBindings"); @@ -15,7 +14,7 @@ namespace Copium { if (binding.bindingType == BindingType::UniformBuffer) { - std::unique_ptr uniformBuffer = std::make_unique(vulkan, binding); + std::unique_ptr uniformBuffer = std::make_unique(binding); SetUniformBuffer(*uniformBuffer, binding.binding); uniformBuffers.emplace(binding.name, std::move(uniformBuffer)); } @@ -28,25 +27,6 @@ namespace Copium descriptorPool.FreeDescriptorSets(descriptorSets); } - void DescriptorSet::SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding) - { - for (size_t i = 0; i < descriptorSets.size(); ++i) { - VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i); - - VkWriteDescriptorSet descriptorWrite{}; - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrite.dstSet = descriptorSets[i]; - descriptorWrite.dstBinding = binding; - descriptorWrite.dstArrayElement = 0; - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptorWrite.descriptorCount = 1; - descriptorWrite.pBufferInfo = &bufferInfo; - descriptorWrite.pImageInfo = nullptr; - descriptorWrite.pTexelBufferView = nullptr; - vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr); - } - } - void DescriptorSet::SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex) { for (size_t i = 0; i < descriptorSets.size(); ++i) @@ -62,16 +42,16 @@ namespace Copium descriptorWrite.pBufferInfo = nullptr; descriptorWrite.pImageInfo = &imageInfo; descriptorWrite.pTexelBufferView = nullptr; - vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr); + vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr); } } void DescriptorSet::SetSamplerDynamic(const Sampler& sampler, uint32_t binding, int arrayIndex) { - VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(vulkan.GetSwapChain().GetFlightIndex()); + VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(Vulkan::GetSwapChain().GetFlightIndex()); VkWriteDescriptorSet descriptorWrite{}; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrite.dstSet = descriptorSets[vulkan.GetSwapChain().GetFlightIndex()]; + descriptorWrite.dstSet = descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()]; descriptorWrite.dstBinding = binding; descriptorWrite.dstArrayElement = arrayIndex; descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; @@ -79,7 +59,7 @@ namespace Copium descriptorWrite.pBufferInfo = nullptr; descriptorWrite.pImageInfo = &imageInfo; descriptorWrite.pTexelBufferView = nullptr; - vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr); + vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr); } void DescriptorSet::SetSamplers(const std::vector& samplers, uint32_t binding) @@ -99,7 +79,7 @@ namespace Copium descriptorWrites[j].pImageInfo = &imageInfo; descriptorWrites[j].pTexelBufferView = nullptr; } - vkUpdateDescriptorSets(vulkan.GetDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr); + vkUpdateDescriptorSets(Vulkan::GetDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr); } } @@ -117,6 +97,26 @@ namespace Copium DescriptorSet::operator VkDescriptorSet() const { - return descriptorSets[vulkan.GetSwapChain().GetFlightIndex()]; + return descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()]; } + + void DescriptorSet::SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding) + { + for (size_t i = 0; i < descriptorSets.size(); ++i) { + VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i); + + VkWriteDescriptorSet descriptorWrite{}; + descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrite.dstSet = descriptorSets[i]; + descriptorWrite.dstBinding = binding; + descriptorWrite.dstArrayElement = 0; + descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrite.descriptorCount = 1; + descriptorWrite.pBufferInfo = &bufferInfo; + descriptorWrite.pImageInfo = nullptr; + descriptorWrite.pTexelBufferView = nullptr; + vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr); + } + } + } \ No newline at end of file diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.h b/CopiumEngine/src/copium/pipeline/DescriptorSet.h index ad0158d..d19d787 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.h +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.h @@ -20,7 +20,6 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet); private: - Vulkan& vulkan; DescriptorPool& descriptorPool; VkDescriptorSetLayout descriptorSetLayout; @@ -29,10 +28,9 @@ namespace Copium std::map> uniformBuffers; public: - DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set& bindings); + DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set& bindings); ~DescriptorSet(); - void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding); void SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0); void SetSamplerDynamic(const Sampler& sampler, uint32_t binding, int arrayIndex = 0); void SetSamplers(const std::vector& sampler, uint32_t binding); @@ -40,5 +38,7 @@ namespace Copium uint32_t GetSetIndex() const; operator VkDescriptorSet() const; + private: + void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding); }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.cpp b/CopiumEngine/src/copium/pipeline/Pipeline.cpp index 5a08d0b..b36532b 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.cpp +++ b/CopiumEngine/src/copium/pipeline/Pipeline.cpp @@ -1,13 +1,13 @@ #include "copium/pipeline/Pipeline.h" -#include "copium/core/Device.h" +#include "copium/core/Vulkan.h" #include "copium/pipeline/Shader.h" #include "copium/util/FileSystem.h" namespace Copium { - Pipeline::Pipeline(Vulkan& vulkan, PipelineCreator creator) - : vulkan{vulkan}, shaderReflector{creator.shaderReflector} + Pipeline::Pipeline(PipelineCreator creator) + : shaderReflector{creator.shaderReflector} { InitializeDescriptorSetLayout(creator); InitializePipeline(creator); @@ -15,11 +15,11 @@ namespace Copium Pipeline::~Pipeline() { - vkDestroyPipeline(vulkan.GetDevice(), graphicsPipeline, nullptr); - vkDestroyPipelineLayout(vulkan.GetDevice(), pipelineLayout, nullptr); + vkDestroyPipeline(Vulkan::GetDevice(), graphicsPipeline, nullptr); + vkDestroyPipelineLayout(Vulkan::GetDevice(), pipelineLayout, nullptr); for (auto&& descriptorSetLayout : descriptorSetLayouts) { - vkDestroyDescriptorSetLayout(vulkan.GetDevice(), descriptorSetLayout, nullptr); + vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr); } } @@ -49,7 +49,7 @@ namespace Copium bindings.emplace(binding); } - return std::make_unique(vulkan, descriptorPool, descriptorSetLayouts[setIndex], bindings); + return std::make_unique(descriptorPool, descriptorSetLayouts[setIndex], bindings); } void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator) @@ -76,13 +76,13 @@ namespace Copium createInfo.bindingCount = layoutBindings.size(); createInfo.pBindings = layoutBindings.data(); - CP_VK_ASSERT(vkCreateDescriptorSetLayout(vulkan.GetDevice(), &createInfo, nullptr, &descriptorSetLayouts[i++]), "InitializeDescriptorSetLayout : Failed to initialize descriptor set layout"); + CP_VK_ASSERT(vkCreateDescriptorSetLayout(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorSetLayouts[i++]), "InitializeDescriptorSetLayout : Failed to initialize descriptor set layout"); } } void Pipeline::InitializePipeline(const PipelineCreator& creator) { - Shader shader{vulkan, Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader}; + Shader shader{Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader}; VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{}; vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; @@ -192,7 +192,7 @@ namespace Copium pipelineLayoutCreateInfo.pushConstantRangeCount = 0; pipelineLayoutCreateInfo.pPushConstantRanges = nullptr; - CP_VK_ASSERT(vkCreatePipelineLayout(vulkan.GetDevice(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout), "InitializePipeline : Failed to initialize pipeline layout"); + CP_VK_ASSERT(vkCreatePipelineLayout(Vulkan::GetDevice(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout), "InitializePipeline : Failed to initialize pipeline layout"); const std::vector& shaderStages = shader.GetShaderStages(); VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{}; @@ -213,6 +213,6 @@ namespace Copium graphicsPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; graphicsPipelineCreateInfo.basePipelineIndex = -1; - CP_VK_ASSERT(vkCreateGraphicsPipelines(vulkan.GetDevice(), VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline), "InitializePipeline : Failed to initialize graphics pipeline"); + CP_VK_ASSERT(vkCreateGraphicsPipelines(Vulkan::GetDevice(), VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline), "InitializePipeline : Failed to initialize graphics pipeline"); } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.h b/CopiumEngine/src/copium/pipeline/Pipeline.h index d41dd30..d0be52a 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.h +++ b/CopiumEngine/src/copium/pipeline/Pipeline.h @@ -1,7 +1,6 @@ #pragma once #include "copium/buffer/CommandBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/PipelineCreator.h" #include "copium/util/Common.h" @@ -15,8 +14,6 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline); private: - Vulkan& vulkan; - ShaderReflector shaderReflector; std::vector descriptorSetLayouts{}; std::vector boundDescriptorSets; @@ -24,7 +21,7 @@ namespace Copium VkPipeline graphicsPipeline; public: - Pipeline(Vulkan& vulkan, PipelineCreator creator); + Pipeline(PipelineCreator creator); ~Pipeline(); void Bind(const CommandBuffer& commandBuffer); void SetDescriptorSet(const DescriptorSet& descriptorSet); diff --git a/CopiumEngine/src/copium/pipeline/PipelineCreator.h b/CopiumEngine/src/copium/pipeline/PipelineCreator.h index 1b2be0a..85eba16 100644 --- a/CopiumEngine/src/copium/pipeline/PipelineCreator.h +++ b/CopiumEngine/src/copium/pipeline/PipelineCreator.h @@ -1,11 +1,12 @@ #pragma once -#include "copium/pipeline/VertexDescriptor.h" #include "copium/pipeline/ShaderReflector.h" +#include "copium/pipeline/VertexDescriptor.h" + +#include #include #include -#include namespace Copium { diff --git a/CopiumEngine/src/copium/pipeline/Shader.cpp b/CopiumEngine/src/copium/pipeline/Shader.cpp index a078b51..de07694 100644 --- a/CopiumEngine/src/copium/pipeline/Shader.cpp +++ b/CopiumEngine/src/copium/pipeline/Shader.cpp @@ -1,12 +1,11 @@ #include "Shader.h" #include "copium/util/FileSystem.h" -#include "copium/core/Device.h" +#include "copium/core/Vulkan.h" namespace Copium { - Shader::Shader(Vulkan& vulkan, Type type, const std::string& vertexInput, const std::string& fragmentInput) - : vulkan{vulkan} + Shader::Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput) { switch (type) { @@ -46,8 +45,8 @@ namespace Copium Shader::~Shader() { - vkDestroyShaderModule(vulkan.GetDevice(), vertShaderModule, nullptr); - vkDestroyShaderModule(vulkan.GetDevice(), fragShaderModule, nullptr); + vkDestroyShaderModule(Vulkan::GetDevice(), vertShaderModule, nullptr); + vkDestroyShaderModule(Vulkan::GetDevice(), fragShaderModule, nullptr); } const std::vector Shader::GetShaderStages() const @@ -126,7 +125,7 @@ namespace Copium createInfo.pCode = data; VkShaderModule shaderModule; - CP_VK_ASSERT(vkCreateShaderModule(vulkan.GetDevice(), &createInfo, nullptr, &shaderModule), "InitializeShaderModule : Failed to initialize shader module"); + CP_VK_ASSERT(vkCreateShaderModule(Vulkan::GetDevice(), &createInfo, nullptr, &shaderModule), "InitializeShaderModule : Failed to initialize shader module"); return shaderModule; } diff --git a/CopiumEngine/src/copium/pipeline/Shader.h b/CopiumEngine/src/copium/pipeline/Shader.h index d646686..361356e 100644 --- a/CopiumEngine/src/copium/pipeline/Shader.h +++ b/CopiumEngine/src/copium/pipeline/Shader.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -18,13 +17,11 @@ namespace Copium }; private: - Vulkan& vulkan; - VkShaderModule vertShaderModule; VkShaderModule fragShaderModule; std::vector shaderStages; public: - Shader(Vulkan& vulkan, Type type, const std::string& vertexInput, const std::string& fragmentInput); + Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput); ~Shader(); const std::vector GetShaderStages() const; diff --git a/CopiumEngine/src/copium/pipeline/ShaderReflector.h b/CopiumEngine/src/copium/pipeline/ShaderReflector.h index fd87727..efa1f2e 100644 --- a/CopiumEngine/src/copium/pipeline/ShaderReflector.h +++ b/CopiumEngine/src/copium/pipeline/ShaderReflector.h @@ -2,8 +2,8 @@ #include "copium/pipeline/ShaderBinding.h" -#include #include +#include namespace Copium { diff --git a/CopiumEngine/src/copium/renderer/Batch.cpp b/CopiumEngine/src/copium/renderer/Batch.cpp index b92ef2a..f70bc01 100644 --- a/CopiumEngine/src/copium/renderer/Batch.cpp +++ b/CopiumEngine/src/copium/renderer/Batch.cpp @@ -4,10 +4,9 @@ namespace Copium { - Batch::Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers) - : vulkan{vulkan}, - pipeline{pipeline}, - vertexBuffer{vulkan, RendererVertex::GetDescriptor(), vertexCount}, + Batch::Batch(Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers) + : pipeline{pipeline}, + vertexBuffer{RendererVertex::GetDescriptor(), vertexCount}, descriptorSet{pipeline.CreateDescriptorSet(descriptorPool, 0)} { descriptorSet->SetSamplers(samplers, 0); diff --git a/CopiumEngine/src/copium/renderer/Batch.h b/CopiumEngine/src/copium/renderer/Batch.h index 2891dfc..45975de 100644 --- a/CopiumEngine/src/copium/renderer/Batch.h +++ b/CopiumEngine/src/copium/renderer/Batch.h @@ -11,13 +11,12 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Batch); private: - Vulkan& vulkan; Pipeline& pipeline; RendererVertexBuffer vertexBuffer; std::unique_ptr descriptorSet; public: - Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers); + Batch(Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers); RendererVertexBuffer& GetVertexBuffer(); DescriptorSet& GetDescriptorSet(); }; diff --git a/CopiumEngine/src/copium/renderer/Renderer.cpp b/CopiumEngine/src/copium/renderer/Renderer.cpp index 85a387a..3e54677 100644 --- a/CopiumEngine/src/copium/renderer/Renderer.cpp +++ b/CopiumEngine/src/copium/renderer/Renderer.cpp @@ -1,6 +1,6 @@ #include "copium/renderer/Renderer.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" #include "copium/pipeline/PipelineCreator.h" #include "copium/renderer/RendererVertex.h" @@ -11,11 +11,10 @@ namespace Copium static constexpr int MAX_NUM_INDICES = 6 * MAX_NUM_QUADS; static constexpr int MAX_NUM_TEXTURES = 32; - Renderer::Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool) - : vulkan{vulkan}, - descriptorPool{vulkan}, - ibo{vulkan, MAX_NUM_INDICES}, - emptyTexture{vulkan, {1, 0, 0, 0}, 1, 1}, + Renderer::Renderer(VkRenderPass renderPass) + : descriptorPool{}, + ibo{MAX_NUM_INDICES}, + emptyTexture{{0, 0, 0, 255}, 1, 1}, samplers{MAX_NUM_TEXTURES, &emptyTexture} { InitializeIndexBuffer(); @@ -99,7 +98,7 @@ namespace Copium PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"}; creator.SetVertexDescriptor(RendererVertex::GetDescriptor()); creator.SetDepthTest(false); - graphicsPipeline = std::make_unique(vulkan, creator); + graphicsPipeline = std::make_unique(creator); } int Renderer::AllocateSampler(const Sampler& sampler) @@ -117,6 +116,7 @@ namespace Copium NextBatch(); } batches[batchIndex]->GetDescriptorSet().SetSamplerDynamic(sampler, 0, textureCount); + samplers[textureCount] = &sampler; textureCount++; return textureCount - 1; @@ -144,13 +144,13 @@ namespace Copium void Renderer::NextBatch() { batchIndex++; + std::fill(samplers.begin(), samplers.end(), &emptyTexture); if (batchIndex >= batches.size()) { - batches.emplace_back(std::make_unique(vulkan, *graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers)); + batches.emplace_back(std::make_unique(*graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers)); } - mappedVertexBuffer = (char*)batches[batchIndex]->GetVertexBuffer().Map() + batches[batchIndex]->GetVertexBuffer().GetPosition(vulkan.GetSwapChain().GetFlightIndex()); + mappedVertexBuffer = (char*)batches[batchIndex]->GetVertexBuffer().Map() + batches[batchIndex]->GetVertexBuffer().GetPosition(Vulkan::GetSwapChain().GetFlightIndex()); quadCount = 0; textureCount = 0; - std::fill(samplers.begin(), samplers.end(), &emptyTexture); } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/renderer/Renderer.h b/CopiumEngine/src/copium/renderer/Renderer.h index 572ae7d..5565c3c 100644 --- a/CopiumEngine/src/copium/renderer/Renderer.h +++ b/CopiumEngine/src/copium/renderer/Renderer.h @@ -3,7 +3,6 @@ #include "copium/buffer/CommandBuffer.h" #include "copium/buffer/IndexBuffer.h" #include "copium/buffer/RendererVertexBuffer.h" -#include "copium/core/Vulkan.h" #include "copium/pipeline/Pipeline.h" #include "copium/renderer/Batch.h" #include "copium/sampler/Texture2D.h" @@ -18,8 +17,6 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Renderer); private: - Vulkan& vulkan; - DescriptorPool descriptorPool; IndexBuffer ibo; Texture2D emptyTexture; @@ -34,7 +31,7 @@ namespace Copium int textureCount; void* mappedVertexBuffer; public: - Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool); + Renderer(VkRenderPass renderPass); void Quad(const glm::vec2& from, const glm::vec2& to, const glm::vec3& color = glm::vec3{1, 1, 1}); void Quad(const glm::vec2& from, const glm::vec2& to, const Sampler& sampler, const glm::vec2& texCoord1 = glm::vec2{0, 0}, const glm::vec2& texCoord2 = glm::vec2{1, 1}); diff --git a/CopiumEngine/src/copium/sampler/ColorAttachment.cpp b/CopiumEngine/src/copium/sampler/ColorAttachment.cpp index 88c669e..2f8d890 100644 --- a/CopiumEngine/src/copium/sampler/ColorAttachment.cpp +++ b/CopiumEngine/src/copium/sampler/ColorAttachment.cpp @@ -1,13 +1,12 @@ #include "copium/sampler/ColorAttachment.h" -#include "copium/core/Device.h" -#include "copium/core/SwapChain.h" +#include "copium/core/Vulkan.h" #include "copium/sampler/Image.h" namespace Copium { - ColorAttachment::ColorAttachment(Vulkan& vulkan, int width, int height) - : Sampler{vulkan} + ColorAttachment::ColorAttachment(int width, int height) + : Sampler{} { InitializeColorAttachment(width, height); } @@ -15,11 +14,11 @@ namespace Copium ColorAttachment::~ColorAttachment() { for (auto&& image : images) - vkDestroyImage(vulkan.GetDevice(), image, nullptr); + vkDestroyImage(Vulkan::GetDevice(), image, nullptr); for (auto&& imageMemory : imageMemories) - vkFreeMemory(vulkan.GetDevice(), imageMemory, nullptr); + vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr); for (auto&& imageView : imageViews) - vkDestroyImageView(vulkan.GetDevice(), imageView, nullptr); + vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr); } VkDescriptorImageInfo ColorAttachment::GetDescriptorImageInfo(int index) const @@ -47,8 +46,8 @@ namespace Copium imageMemories.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); for (size_t i = 0; i < images.size(); i++) { - Image::InitializeImage(vulkan, width, height, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &images[i], &imageMemories[i]); - imageViews[i] = Image::InitializeImageView(vulkan, images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT); + Image::InitializeImage(width, height, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &images[i], &imageMemories[i]); + imageViews[i] = Image::InitializeImageView(images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT); } } } diff --git a/CopiumEngine/src/copium/sampler/ColorAttachment.h b/CopiumEngine/src/copium/sampler/ColorAttachment.h index f887695..33035e8 100644 --- a/CopiumEngine/src/copium/sampler/ColorAttachment.h +++ b/CopiumEngine/src/copium/sampler/ColorAttachment.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/sampler/Sampler.h" #include "copium/util/Common.h" @@ -16,7 +15,7 @@ namespace Copium std::vector imageMemories; std::vector imageViews; public: - ColorAttachment(Vulkan& vulkan, int width, int height); + ColorAttachment(int width, int height); ~ColorAttachment() override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; diff --git a/CopiumEngine/src/copium/sampler/DepthAttachment.cpp b/CopiumEngine/src/copium/sampler/DepthAttachment.cpp index fcedc74..225882f 100644 --- a/CopiumEngine/src/copium/sampler/DepthAttachment.cpp +++ b/CopiumEngine/src/copium/sampler/DepthAttachment.cpp @@ -1,21 +1,21 @@ #include "copium/sampler/DepthAttachment.h" -#include "copium/core/Device.h" +#include "copium/core/Vulkan.h" #include "copium/sampler/Image.h" namespace Copium { - DepthAttachment::DepthAttachment(Vulkan& vulkan, int width, int height) - : Sampler{vulkan} + DepthAttachment::DepthAttachment(int width, int height) + : Sampler{} { InitializeDepthAttachment(width, height); } DepthAttachment::~DepthAttachment() { - vkDestroyImage(vulkan.GetDevice(), image, nullptr); - vkFreeMemory(vulkan.GetDevice(), imageMemory, nullptr); - vkDestroyImageView(vulkan.GetDevice(), imageView, nullptr); + vkDestroyImage(Vulkan::GetDevice(), image, nullptr); + vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr); + vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr); } VkDescriptorImageInfo DepthAttachment::GetDescriptorImageInfo(int index) const @@ -34,8 +34,8 @@ namespace Copium void DepthAttachment::InitializeDepthAttachment(int width, int height) { - VkFormat depthFormat = Image::SelectDepthFormat(vulkan); - Image::InitializeImage(vulkan, width, height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &image, &imageMemory); - imageView = Image::InitializeImageView(vulkan, image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT); + VkFormat depthFormat = Image::SelectDepthFormat(); + Image::InitializeImage(width, height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &image, &imageMemory); + imageView = Image::InitializeImageView(image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT); } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/sampler/DepthAttachment.h b/CopiumEngine/src/copium/sampler/DepthAttachment.h index 0f31635..a1bf302 100644 --- a/CopiumEngine/src/copium/sampler/DepthAttachment.h +++ b/CopiumEngine/src/copium/sampler/DepthAttachment.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/sampler/Sampler.h" #include "copium/util/Common.h" @@ -16,7 +15,7 @@ namespace Copium VkDeviceMemory imageMemory; VkImageView imageView; public: - DepthAttachment(Vulkan& vulkan, int width, int height); + DepthAttachment(int width, int height); ~DepthAttachment() override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; diff --git a/CopiumEngine/src/copium/sampler/Image.cpp b/CopiumEngine/src/copium/sampler/Image.cpp index f3d8c56..e122357 100644 --- a/CopiumEngine/src/copium/sampler/Image.cpp +++ b/CopiumEngine/src/copium/sampler/Image.cpp @@ -1,11 +1,11 @@ #include "copium/sampler/Image.h" #include "copium/buffer/CommandBufferScoped.h" -#include "copium/core/Device.h" +#include "copium/core/Vulkan.h" namespace Copium { - void Image::InitializeImage(Vulkan& vulkan, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage* image, VkDeviceMemory* imageMemory) + void Image::InitializeImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage* image, VkDeviceMemory* imageMemory) { VkImageCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -23,22 +23,22 @@ namespace Copium createInfo.samples = VK_SAMPLE_COUNT_1_BIT; createInfo.flags = 0; - CP_VK_ASSERT(vkCreateImage(vulkan.GetDevice(), &createInfo, nullptr, image), "InitializeImage : Failed to initialize image"); + CP_VK_ASSERT(vkCreateImage(Vulkan::GetDevice(), &createInfo, nullptr, image), "InitializeImage : Failed to initialize image"); VkMemoryRequirements memoryRequirements; - vkGetImageMemoryRequirements(vulkan.GetDevice(), *image, &memoryRequirements); + vkGetImageMemoryRequirements(Vulkan::GetDevice(), *image, &memoryRequirements); VkMemoryAllocateInfo allocateInfo{}; allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocateInfo.allocationSize = memoryRequirements.size; - allocateInfo.memoryTypeIndex = vulkan.GetDevice().FindMemoryType(memoryRequirements.memoryTypeBits, properties); + allocateInfo.memoryTypeIndex = Vulkan::GetDevice().FindMemoryType(memoryRequirements.memoryTypeBits, properties); - CP_VK_ASSERT(vkAllocateMemory(vulkan.GetDevice(), &allocateInfo, nullptr, imageMemory), "InitializeImage : Failed to initiallizse image memory"); + CP_VK_ASSERT(vkAllocateMemory(Vulkan::GetDevice(), &allocateInfo, nullptr, imageMemory), "InitializeImage : Failed to initiallizse image memory"); - vkBindImageMemory(vulkan.GetDevice(), *image, *imageMemory, 0); + vkBindImageMemory(Vulkan::GetDevice(), *image, *imageMemory, 0); } - VkImageView Image::InitializeImageView(Vulkan& vulkan, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) + VkImageView Image::InitializeImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) { VkImageView imageView; VkImageViewCreateInfo createInfo{}; @@ -55,13 +55,13 @@ namespace Copium createInfo.subresourceRange.levelCount = 1; createInfo.subresourceRange.baseArrayLayer = 0; createInfo.subresourceRange.layerCount = 1; - CP_VK_ASSERT(vkCreateImageView(vulkan.GetDevice(), &createInfo, nullptr, &imageView), "InitializeImageView : Failed to initialize image view"); + CP_VK_ASSERT(vkCreateImageView(Vulkan::GetDevice(), &createInfo, nullptr, &imageView), "InitializeImageView : Failed to initialize image view"); return imageView; } - void Image::TransitionImageLayout(Vulkan& vulkan, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) + void Image::TransitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) { - CommandBufferScoped commandBuffer{vulkan}; + CommandBufferScoped commandBuffer{}; VkImageMemoryBarrier barrier{}; barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -127,9 +127,9 @@ namespace Copium vkCmdPipelineBarrier(commandBuffer, srcStage, dstStage, 0, 0, nullptr, 0, nullptr, 1, &barrier); } - void Image::CopyBufferToImage(Vulkan& vulkan, const Buffer& buffer, VkImage image, uint32_t width, uint32_t height) + void Image::CopyBufferToImage(const Buffer& buffer, VkImage image, uint32_t width, uint32_t height) { - CommandBufferScoped commandBuffer{vulkan}; + CommandBufferScoped commandBuffer{}; VkBufferImageCopy region{}; region.bufferOffset = 0; @@ -148,9 +148,9 @@ namespace Copium } - VkFormat Image::SelectDepthFormat(Vulkan& vulkan) + VkFormat Image::SelectDepthFormat() { - return SelectSupportedFormat(vulkan, {VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT}, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); + return SelectSupportedFormat({VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT}, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); } bool Image::HasStencilComponent(VkFormat format) @@ -158,12 +158,12 @@ namespace Copium return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT; } - VkFormat Image::SelectSupportedFormat(Vulkan& vulkan, const std::vector& candidates, VkImageTiling tiling, VkFormatFeatureFlags features) + VkFormat Image::SelectSupportedFormat(const std::vector& candidates, VkImageTiling tiling, VkFormatFeatureFlags features) { for (VkFormat format : candidates) { VkFormatProperties properties; - vkGetPhysicalDeviceFormatProperties(vulkan.GetDevice().GetPhysicalDevice(), format, &properties); + vkGetPhysicalDeviceFormatProperties(Vulkan::GetDevice().GetPhysicalDevice(), format, &properties); if (tiling == VK_IMAGE_TILING_LINEAR && (properties.linearTilingFeatures & features) == features) { return format; diff --git a/CopiumEngine/src/copium/sampler/Image.h b/CopiumEngine/src/copium/sampler/Image.h index 39bf0cf..9eda313 100644 --- a/CopiumEngine/src/copium/sampler/Image.h +++ b/CopiumEngine/src/copium/sampler/Image.h @@ -1,7 +1,6 @@ #pragma once #include "copium/buffer/Buffer.h" -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -12,14 +11,14 @@ namespace Copium { CP_STATIC_CLASS(Image); public: - static void InitializeImage(Vulkan& vulkan, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage* image, VkDeviceMemory* imageMemory); - static VkImageView InitializeImageView(Vulkan& vulkan, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags); - static void TransitionImageLayout(Vulkan& vulkan, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout); - static void CopyBufferToImage(Vulkan& vulkan, const Buffer& buffer, VkImage image, uint32_t width, uint32_t height); - static VkFormat SelectDepthFormat(Vulkan& vulkan); + static void InitializeImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage* image, VkDeviceMemory* imageMemory); + static VkImageView InitializeImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags); + static void TransitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout); + static void CopyBufferToImage(const Buffer& buffer, VkImage image, uint32_t width, uint32_t height); + static VkFormat SelectDepthFormat(); private: static bool HasStencilComponent(VkFormat format); - static VkFormat SelectSupportedFormat(Vulkan& vulkan, const std::vector& candidates, VkImageTiling tiling, VkFormatFeatureFlags features); + static VkFormat SelectSupportedFormat(const std::vector& candidates, VkImageTiling tiling, VkFormatFeatureFlags features); }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/sampler/Sampler.cpp b/CopiumEngine/src/copium/sampler/Sampler.cpp index a9b7390..9ea69be 100644 --- a/CopiumEngine/src/copium/sampler/Sampler.cpp +++ b/CopiumEngine/src/copium/sampler/Sampler.cpp @@ -1,24 +1,23 @@ #include "copium/sampler/Sampler.h" -#include "copium/core/Device.h" +#include "copium/core/Vulkan.h" namespace Copium { - Sampler::Sampler(Vulkan& vulkan) - : vulkan{vulkan} + Sampler::Sampler() { InitializeSampler(); } Sampler::~Sampler() { - vkDestroySampler(vulkan.GetDevice(), sampler, nullptr); + vkDestroySampler(Vulkan::GetDevice(), sampler, nullptr); } void Sampler::InitializeSampler() { VkPhysicalDeviceProperties properties{}; - vkGetPhysicalDeviceProperties(vulkan.GetDevice().GetPhysicalDevice(), &properties); + vkGetPhysicalDeviceProperties(Vulkan::GetDevice().GetPhysicalDevice(), &properties); VkSamplerCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; @@ -37,7 +36,7 @@ namespace Copium createInfo.minLod = 0.0f; createInfo.maxLod = 0.0f; - CP_VK_ASSERT(vkCreateSampler(vulkan.GetDevice(), &createInfo, nullptr, &sampler), "InitializeSampler : Failed to initialize texture sampler"); + CP_VK_ASSERT(vkCreateSampler(Vulkan::GetDevice(), &createInfo, nullptr, &sampler), "InitializeSampler : Failed to initialize texture sampler"); } Sampler::operator VkSampler() const diff --git a/CopiumEngine/src/copium/sampler/Sampler.h b/CopiumEngine/src/copium/sampler/Sampler.h index 34b08f8..fcda33f 100644 --- a/CopiumEngine/src/copium/sampler/Sampler.h +++ b/CopiumEngine/src/copium/sampler/Sampler.h @@ -1,6 +1,5 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/util/Common.h" #include @@ -11,10 +10,9 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Sampler); protected: - Vulkan& vulkan; VkSampler sampler; public: - Sampler(Vulkan& vulkan); + Sampler(); virtual ~Sampler(); virtual VkDescriptorImageInfo GetDescriptorImageInfo(int index) const = 0; diff --git a/CopiumEngine/src/copium/sampler/Texture2D.cpp b/CopiumEngine/src/copium/sampler/Texture2D.cpp index a19f1e2..0893dfa 100644 --- a/CopiumEngine/src/copium/sampler/Texture2D.cpp +++ b/CopiumEngine/src/copium/sampler/Texture2D.cpp @@ -1,21 +1,21 @@ #include "copium/sampler/Texture2D.h" -#include "copium/core/Device.h" +#include "copium/core/Vulkan.h" #define STB_IMAGE_IMPLEMENTATION #include namespace Copium { - Texture2D::Texture2D(Vulkan& vulkan, const std::string& filename) - : Sampler{vulkan} + Texture2D::Texture2D(const std::string& filename) + : Sampler{} { CP_DEBUG("Texture2D : Loading texture file: %s", filename.c_str()); InitializeTextureImageFromFile(filename); } - Texture2D::Texture2D(Vulkan& vulkan, const std::vector& rgbaData, int width, int height) - : Sampler{vulkan} + Texture2D::Texture2D(const std::vector& rgbaData, int width, int height) + : Sampler{} { CP_ASSERT(rgbaData.size() == width * height * 4, "rgbaData has invalid size, should be equal to width * height * 4 (%d) actually is %d", width * height * 4, rgbaData.size()); InitializeTextureImageFromData((void*)rgbaData.data(), width, height); @@ -23,9 +23,9 @@ namespace Copium Texture2D::~Texture2D() { - vkDestroyImage(vulkan.GetDevice(), image, nullptr); - vkFreeMemory(vulkan.GetDevice(), imageMemory, nullptr); - vkDestroyImageView(vulkan.GetDevice(), imageView, nullptr); + vkDestroyImage(Vulkan::GetDevice(), image, nullptr); + vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr); + vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr); } VkDescriptorImageInfo Texture2D::GetDescriptorImageInfo(int index) const @@ -55,15 +55,15 @@ namespace Copium void Texture2D::InitializeTextureImageFromData(void* rgbaData, int width, int height) { VkDeviceSize bufferSize = width * height * 4; - Buffer stagingBuffer{vulkan, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1}; + Buffer stagingBuffer{VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1}; void* data = stagingBuffer.Map(); memcpy(data, rgbaData, bufferSize); stagingBuffer.Unmap(); - Image::InitializeImage(vulkan, width, height, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &image, &imageMemory); - Image::TransitionImageLayout(vulkan, image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); - Image::CopyBufferToImage(vulkan, stagingBuffer, image, width, height); - Image::TransitionImageLayout(vulkan, image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - imageView = Image::InitializeImageView(vulkan, image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT); + Image::InitializeImage(width, height, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &image, &imageMemory); + Image::TransitionImageLayout(image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + Image::CopyBufferToImage(stagingBuffer, image, width, height); + Image::TransitionImageLayout(image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + imageView = Image::InitializeImageView(image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT); } } diff --git a/CopiumEngine/src/copium/sampler/Texture2D.h b/CopiumEngine/src/copium/sampler/Texture2D.h index 11775ea..5d00aca 100644 --- a/CopiumEngine/src/copium/sampler/Texture2D.h +++ b/CopiumEngine/src/copium/sampler/Texture2D.h @@ -1,7 +1,6 @@ #pragma once #include "copium/buffer/CommandBufferScoped.h" -#include "copium/core/Vulkan.h" #include "copium/sampler/Image.h" #include "copium/sampler/Sampler.h" #include "copium/util/Common.h" @@ -18,8 +17,8 @@ namespace Copium VkDeviceMemory imageMemory; VkImageView imageView; public: - Texture2D(Vulkan& vulkan, const std::string& filename); - Texture2D(Vulkan& vulkan, const std::vector& rgbaData, int width, int height); + Texture2D(const std::string& filename); + Texture2D(const std::vector& rgbaData, int width, int height); ~Texture2D() override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;