Make Vulkan class a global instance

This commit is contained in:
Thraix
2023-04-04 21:14:01 +02:00
parent 9faec15fd6
commit 431ad9c573
56 changed files with 333 additions and 424 deletions
+2
View File
@@ -119,6 +119,7 @@
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)ext/include/;C:/VulkanSDK/1.3.236.0/Include;%(AdditionalIncludeDirectories)$(ProjectDir)src/</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)ext/include/;C:/VulkanSDK/1.3.236.0/Include;%(AdditionalIncludeDirectories)$(ProjectDir)src/</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@@ -145,6 +146,7 @@
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)ext/include/;C:/VulkanSDK/1.3.236.0/Include;%(AdditionalIncludeDirectories)$(ProjectDir)src/</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)ext/include/;C:/VulkanSDK/1.3.236.0/Include;%(AdditionalIncludeDirectories)$(ProjectDir)src/</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
+24 -25
View File
@@ -1,12 +1,11 @@
#include "copium/buffer/Buffer.h" #include "copium/buffer/Buffer.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/core/Instance.h"
namespace Copium namespace Copium
{ {
Buffer::Buffer(Vulkan& vulkan, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count) Buffer::Buffer(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count)
: vulkan{vulkan}, size{size}, count{count} : size{size}, count{count}
{ {
VkBufferCreateInfo createInfo{}; VkBufferCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
@@ -14,25 +13,25 @@ namespace Copium
createInfo.usage = usage; createInfo.usage = usage;
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; 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; VkMemoryRequirements memoryRequirements;
vkGetBufferMemoryRequirements(vulkan.GetDevice(), handle, &memoryRequirements); vkGetBufferMemoryRequirements(Vulkan::GetDevice(), handle, &memoryRequirements);
VkMemoryAllocateInfo allocateInfo{}; VkMemoryAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocateInfo.allocationSize = memoryRequirements.size; 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() Buffer::~Buffer()
{ {
vkFreeMemory(vulkan.GetDevice(), memory, nullptr); vkFreeMemory(Vulkan::GetDevice(), memory, nullptr);
vkDestroyBuffer(vulkan.GetDevice(), handle, nullptr); vkDestroyBuffer(Vulkan::GetDevice(), handle, nullptr);
} }
void Buffer::Update(void* indexData, int index) void Buffer::Update(void* indexData, int index)
@@ -42,9 +41,9 @@ namespace Copium
if (mappedData == nullptr) if (mappedData == nullptr)
{ {
void* data; void* data;
vkMapMemory(vulkan.GetDevice(), memory, index * size, size, 0, &data); vkMapMemory(Vulkan::GetDevice(), memory, index * size, size, 0, &data);
memcpy(data, indexData, size); memcpy(data, indexData, size);
vkUnmapMemory(vulkan.GetDevice(), memory); vkUnmapMemory(Vulkan::GetDevice(), memory);
} }
else else
{ {
@@ -55,26 +54,26 @@ namespace Copium
void Buffer::UpdateStaging(void* data) void Buffer::UpdateStaging(void* data)
{ {
VkDeviceSize bufferSize = size * count; 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); stagingBuffer.Update(data, 0);
CopyBuffer(vulkan, stagingBuffer, *this, 0, bufferSize); CopyBuffer(stagingBuffer, *this, 0, bufferSize);
} }
void Buffer::UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size) 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); stagingBuffer.Update(data, 0);
CopyBuffer(vulkan, stagingBuffer, *this, offset, size); CopyBuffer(stagingBuffer, *this, offset, size);
} }
void* Buffer::Map() void* Buffer::Map()
{ {
CP_ASSERT(mappedData == nullptr, "Map : Mapping an already mapped buffer"); 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; return mappedData;
} }
@@ -82,7 +81,7 @@ namespace Copium
{ {
CP_ASSERT(mappedData != nullptr, "Unmap : Unmapping an already unmapped buffer"); CP_ASSERT(mappedData != nullptr, "Unmap : Unmapping an already unmapped buffer");
vkUnmapMemory(vulkan.GetDevice(), memory); vkUnmapMemory(Vulkan::GetDevice(), memory);
mappedData = nullptr; mappedData = nullptr;
} }
@@ -102,17 +101,17 @@ namespace Copium
return size * (VkDeviceSize)index; 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{}; VkCommandBufferAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = vulkan.GetDevice().GetCommandPool(); allocateInfo.commandPool = Vulkan::GetDevice().GetCommandPool();
allocateInfo.commandBufferCount = 1; allocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer; 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{}; VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@@ -134,9 +133,9 @@ namespace Copium
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer; submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(vulkan.GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE); vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(vulkan.GetDevice().GetGraphicsQueue()); vkQueueWaitIdle(Vulkan::GetDevice().GetGraphicsQueue());
vkFreeCommandBuffers(vulkan.GetDevice(), vulkan.GetDevice().GetCommandPool(), 1, &commandBuffer); vkFreeCommandBuffers(Vulkan::GetDevice(), Vulkan::GetDevice().GetCommandPool(), 1, &commandBuffer);
} }
} }
+2 -5
View File
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -11,8 +10,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Buffer); CP_DELETE_COPY_AND_MOVE_CTOR(Buffer);
protected: protected:
Vulkan& vulkan;
VkDeviceMemory memory; VkDeviceMemory memory;
VkBuffer handle; VkBuffer handle;
VkDeviceSize size; VkDeviceSize size;
@@ -21,7 +18,7 @@ namespace Copium
void* mappedData = nullptr; void* mappedData = nullptr;
public: public:
Buffer(Vulkan& vulkan, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count); Buffer(VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count);
virtual ~Buffer(); virtual ~Buffer();
void Update(void* indexData, int index); void Update(void* indexData, int index);
@@ -35,6 +32,6 @@ namespace Copium
VkDeviceSize GetSize() const; VkDeviceSize GetSize() const;
VkDeviceSize GetPosition(int index) 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);
}; };
} }
@@ -1,13 +1,11 @@
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/core/Instance.h"
#include "copium/core/SwapChain.h"
namespace Copium namespace Copium
{ {
CommandBuffer::CommandBuffer(Vulkan& vulkan, Type type) CommandBuffer::CommandBuffer(Type type)
: vulkan{vulkan}, type{type} : type{type}
{ {
switch (type) switch (type)
{ {
@@ -24,14 +22,14 @@ namespace Copium
VkCommandBufferAllocateInfo allocateInfo{}; VkCommandBufferAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = vulkan.GetDevice().GetCommandPool(); allocateInfo.commandPool = Vulkan::GetDevice().GetCommandPool();
allocateInfo.commandBufferCount = commandBuffers.size(); 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() 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 // 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"); CP_ABORT("Begin : Unreachable switch case");
} }
vkResetCommandBuffer(commandBuffers[vulkan.GetSwapChain().GetFlightIndex()], 0); vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0);
CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[vulkan.GetSwapChain().GetFlightIndex()], &beginInfo), "Begin : Failed to begin command buffer"); CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], &beginInfo), "Begin : Failed to begin command buffer");
} }
void CommandBuffer::End() void CommandBuffer::End()
{ {
vkEndCommandBuffer(commandBuffers[vulkan.GetSwapChain().GetFlightIndex()]); vkEndCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]);
} }
void CommandBuffer::Submit() void CommandBuffer::Submit()
@@ -67,15 +65,15 @@ namespace Copium
VkSubmitInfo submitInfo{}; VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1; 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? // TODO: if singleUse?
vkQueueWaitIdle(vulkan.GetDevice().GetGraphicsQueue()); vkQueueWaitIdle(Vulkan::GetDevice().GetGraphicsQueue());
} }
CommandBuffer::operator VkCommandBuffer() const CommandBuffer::operator VkCommandBuffer() const
{ {
return commandBuffers[vulkan.GetSwapChain().GetFlightIndex()]; return commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()];
} }
} }
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -16,13 +15,11 @@ namespace Copium
SingleUse, Dynamic SingleUse, Dynamic
}; };
private: private:
Vulkan& vulkan;
std::vector<VkCommandBuffer> commandBuffers; std::vector<VkCommandBuffer> commandBuffers;
const Type type; const Type type;
public: public:
CommandBuffer(Vulkan& vulkan, Type type); CommandBuffer(Type type);
virtual ~CommandBuffer(); virtual ~CommandBuffer();
void Begin(); void Begin();
@@ -2,8 +2,8 @@
namespace Copium namespace Copium
{ {
CommandBufferScoped::CommandBufferScoped(Vulkan& vulkan) CommandBufferScoped::CommandBufferScoped()
: CommandBuffer{vulkan, Type::SingleUse} : CommandBuffer{Type::SingleUse}
{ {
CommandBuffer::Begin(); CommandBuffer::Begin();
} }
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
namespace Copium namespace Copium
@@ -10,7 +9,7 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped); CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped);
public: public:
CommandBufferScoped(Vulkan& vulkan); CommandBufferScoped();
~CommandBufferScoped() override; ~CommandBufferScoped() override;
}; };
+14 -15
View File
@@ -1,14 +1,13 @@
#include "copium/buffer/Framebuffer.h" #include "copium/buffer/Framebuffer.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/core/SwapChain.h"
#include "copium/sampler/Image.h" #include "copium/sampler/Image.h"
namespace Copium namespace Copium
{ {
Framebuffer::Framebuffer(Vulkan& vulkan, uint32_t width, uint32_t height) Framebuffer::Framebuffer(uint32_t width, uint32_t height)
: vulkan{vulkan}, width{width}, height{height} : width{width}, height{height}
{ {
InitializeImage(); InitializeImage();
InitializeDepthBuffer(); InitializeDepthBuffer();
@@ -19,19 +18,19 @@ namespace Copium
Framebuffer::~Framebuffer() Framebuffer::~Framebuffer()
{ {
for (auto& framebuffer : framebuffers) for (auto& framebuffer : framebuffers)
vkDestroyFramebuffer(vulkan.GetDevice(), framebuffer, nullptr); vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
vkDestroyRenderPass(vulkan.GetDevice(), renderPass, nullptr); vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr);
} }
void Framebuffer::Resize(uint32_t width, uint32_t height) void Framebuffer::Resize(uint32_t width, uint32_t height)
{ {
vkDeviceWaitIdle(vulkan.GetDevice()); vkDeviceWaitIdle(Vulkan::GetDevice());
this->width = width; this->width = width;
this->height = height; this->height = height;
colorAttachment.reset(); colorAttachment.reset();
depthAttachment.reset(); depthAttachment.reset();
for (auto&& framebuffer : framebuffers) for (auto&& framebuffer : framebuffers)
vkDestroyFramebuffer(vulkan.GetDevice(), framebuffer, nullptr); vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
InitializeImage(); InitializeImage();
InitializeDepthBuffer(); InitializeDepthBuffer();
InitializeFramebuffers(); InitializeFramebuffers();
@@ -46,7 +45,7 @@ namespace Copium
VkRenderPassBeginInfo renderPassBeginInfo{}; VkRenderPassBeginInfo renderPassBeginInfo{};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.framebuffer = framebuffers[vulkan.GetSwapChain().GetFlightIndex()]; renderPassBeginInfo.framebuffer = framebuffers[Vulkan::GetSwapChain().GetFlightIndex()];
; ;
renderPassBeginInfo.renderArea.offset = {0, 0}; renderPassBeginInfo.renderArea.offset = {0, 0};
renderPassBeginInfo.renderArea.extent = {width, height}; renderPassBeginInfo.renderArea.extent = {width, height};
@@ -80,7 +79,7 @@ namespace Copium
VkFramebuffer Framebuffer::GetFramebuffer() const VkFramebuffer Framebuffer::GetFramebuffer() const
{ {
return framebuffers[vulkan.GetSwapChain().GetFlightIndex()]; return framebuffers[Vulkan::GetSwapChain().GetFlightIndex()];
} }
const ColorAttachment& Framebuffer::GetColorAttachment() const const ColorAttachment& Framebuffer::GetColorAttachment() const
@@ -100,12 +99,12 @@ namespace Copium
void Framebuffer::InitializeImage() void Framebuffer::InitializeImage()
{ {
colorAttachment = std::make_unique<ColorAttachment>(vulkan, width, height); colorAttachment = std::make_unique<ColorAttachment>(width, height);
} }
void Framebuffer::InitializeDepthBuffer() void Framebuffer::InitializeDepthBuffer()
{ {
depthAttachment = std::make_unique<DepthAttachment>(vulkan, width, height); depthAttachment = std::make_unique<DepthAttachment>(width, height);
} }
void Framebuffer::InitializeRenderPass() void Framebuffer::InitializeRenderPass()
@@ -121,7 +120,7 @@ namespace Copium
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkAttachmentDescription depthAttachment{}; VkAttachmentDescription depthAttachment{};
depthAttachment.format = Image::SelectDepthFormat(vulkan); depthAttachment.format = Image::SelectDepthFormat();
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@@ -169,7 +168,7 @@ namespace Copium
renderPassCreateInfo.dependencyCount = dependencies.size(); renderPassCreateInfo.dependencyCount = dependencies.size();
renderPassCreateInfo.pDependencies = dependencies.data(); 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() void Framebuffer::InitializeFramebuffers()
@@ -188,7 +187,7 @@ namespace Copium
createInfo.height = height; createInfo.height = height;
createInfo.layers = 1; 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");
} }
} }
} }
+1 -4
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/sampler/ColorAttachment.h" #include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/DepthAttachment.h" #include "copium/sampler/DepthAttachment.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -14,8 +13,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer); CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
private: private:
Vulkan& vulkan;
std::unique_ptr<ColorAttachment> colorAttachment; std::unique_ptr<ColorAttachment> colorAttachment;
std::unique_ptr<DepthAttachment> depthAttachment; std::unique_ptr<DepthAttachment> depthAttachment;
std::vector<VkFramebuffer> framebuffers; std::vector<VkFramebuffer> framebuffers;
@@ -24,7 +21,7 @@ namespace Copium
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
public: public:
Framebuffer(Vulkan& vulkan, uint32_t width, uint32_t height); Framebuffer(uint32_t width, uint32_t height);
~Framebuffer(); ~Framebuffer();
void Resize(uint32_t width, uint32_t height); void Resize(uint32_t width, uint32_t height);
@@ -4,8 +4,8 @@
namespace Copium namespace Copium
{ {
IndexBuffer::IndexBuffer(Vulkan& vulkan, int indexCount) IndexBuffer::IndexBuffer(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}, : 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} indexCount{indexCount}
{} {}
+1 -2
View File
@@ -2,7 +2,6 @@
#include "copium/buffer/Buffer.h" #include "copium/buffer/Buffer.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
namespace Copium namespace Copium
@@ -13,7 +12,7 @@ namespace Copium
private: private:
int indexCount; int indexCount;
public: public:
IndexBuffer(Vulkan& vulkan, int indexCount); IndexBuffer(int indexCount);
void Bind(const CommandBuffer& commandBuffer); void Bind(const CommandBuffer& commandBuffer);
void Draw(const CommandBuffer& commandBuffer); void Draw(const CommandBuffer& commandBuffer);
@@ -1,17 +1,17 @@
#include "copium/buffer/RendererVertexBuffer.h" #include "copium/buffer/RendererVertexBuffer.h"
#include "copium/core/SwapChain.h" #include "copium/core/Vulkan.h"
namespace Copium namespace Copium
{ {
RendererVertexBuffer::RendererVertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount) RendererVertexBuffer::RendererVertexBuffer(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} : 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) void RendererVertexBuffer::Bind(const CommandBuffer& commandBuffer)
{ {
VkDeviceSize offset = GetPosition(vulkan.GetSwapChain().GetFlightIndex()); VkDeviceSize offset = GetPosition(Vulkan::GetSwapChain().GetFlightIndex());
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &handle, &offset); vkCmdBindVertexBuffers(commandBuffer, 0, 1, &handle, &offset);
} }
} }
@@ -2,7 +2,6 @@
#include "copium/buffer/Buffer.h" #include "copium/buffer/Buffer.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/pipeline/VertexDescriptor.h" #include "copium/pipeline/VertexDescriptor.h"
namespace Copium namespace Copium
@@ -11,7 +10,7 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(RendererVertexBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(RendererVertexBuffer);
public: public:
RendererVertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount); RendererVertexBuffer(const VertexDescriptor& descriptor, int vertexCount);
void Bind(const CommandBuffer& commandBuffer); void Bind(const CommandBuffer& commandBuffer);
}; };
@@ -1,11 +1,11 @@
#include "copium/buffer/UniformBuffer.h" #include "copium/buffer/UniformBuffer.h"
#include "copium/core/SwapChain.h" #include "copium/core/Vulkan.h"
namespace Copium namespace Copium
{ {
UniformBuffer::UniformBuffer(Vulkan& vulkan, ShaderBinding binding) UniformBuffer::UniformBuffer(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} : 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()); buffer.resize(Buffer::GetSize());
} }
@@ -70,6 +70,6 @@ namespace Copium
void UniformBuffer::Update() void UniformBuffer::Update()
{ {
Buffer::Update(buffer.data(), vulkan.GetSwapChain().GetFlightIndex()); Buffer::Update(buffer.data(), Vulkan::GetSwapChain().GetFlightIndex());
} }
} }
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/Buffer.h" #include "copium/buffer/Buffer.h"
#include "copium/core/Vulkan.h"
#include "copium/pipeline/ShaderBinding.h" #include "copium/pipeline/ShaderBinding.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -13,12 +12,12 @@ namespace Copium
class UniformBuffer final : public Buffer class UniformBuffer final : public Buffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
private:
ShaderBinding binding; ShaderBinding binding;
std::vector<uint8_t> buffer; std::vector<uint8_t> buffer;
public: public:
UniformBuffer(Vulkan& vulkan, ShaderBinding binding); UniformBuffer(ShaderBinding binding);
VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const; VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const;
@@ -2,8 +2,8 @@
namespace Copium namespace Copium
{ {
VertexBuffer::VertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount) VertexBuffer::VertexBuffer(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} : 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; VkDeviceSize offset = 0;
for (auto&& binding : descriptor.GetBindings()) for (auto&& binding : descriptor.GetBindings())
@@ -2,7 +2,6 @@
#include "copium/buffer/Buffer.h" #include "copium/buffer/Buffer.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/pipeline/VertexDescriptor.h" #include "copium/pipeline/VertexDescriptor.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -18,7 +17,7 @@ namespace Copium
std::vector<VkDeviceSize> bindingOffsets; std::vector<VkDeviceSize> bindingOffsets;
std::vector<VkDeviceSize> bindingSizes; std::vector<VkDeviceSize> bindingSizes;
public: public:
VertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount); VertexBuffer(const VertexDescriptor& descriptor, int vertexCount);
void Bind(const CommandBuffer& commandBuffer); void Bind(const CommandBuffer& commandBuffer);
void UpdateStaging(uint32_t binding, void* data); void UpdateStaging(uint32_t binding, void* data);
+21 -35
View File
@@ -1,11 +1,8 @@
#include "copium/core/Application.h" #include "copium/core/Application.h"
#include "copium/core/Vulkan.h"
#include "copium/mesh/Vertex.h" #include "copium/mesh/Vertex.h"
#include "copium/mesh/VertexPassthrough.h" #include "copium/mesh/VertexPassthrough.h"
#include "copium/core/Device.h"
#include "copium/core/Instance.h"
#include "copium/core/SwapChain.h"
#include "copium/core/Window.h"
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
@@ -40,7 +37,6 @@ namespace Copium
Application::Application() Application::Application()
{ {
InitializeVulkan();
InitializeFrameBuffer(); InitializeFrameBuffer();
InitializeRenderer(); InitializeRenderer();
InitializeGraphicsPipeline(); InitializeGraphicsPipeline();
@@ -52,56 +48,46 @@ namespace Copium
Application::~Application() Application::~Application()
{ {
vkDeviceWaitIdle(vulkan->GetDevice()); vkDeviceWaitIdle(Vulkan::GetDevice());
} }
bool Application::Update() 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); descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
} }
if (!vulkan->GetSwapChain().BeginPresent()) if (!Vulkan::GetSwapChain().BeginPresent())
return true; return true;
RecordCommandBuffer(); RecordCommandBuffer();
vulkan->GetSwapChain().SubmitToGraphicsQueue(*commandBuffer); Vulkan::GetSwapChain().SubmitToGraphicsQueue(*commandBuffer);
vulkan->GetSwapChain().EndPresent(); Vulkan::GetSwapChain().EndPresent();
return !glfwWindowShouldClose(vulkan->GetWindow().GetWindow()); return !glfwWindowShouldClose(Vulkan::GetWindow().GetWindow());
}
void Application::InitializeVulkan()
{
vulkan = std::make_unique<Vulkan>();
vulkan->SetInstance(std::make_unique<Instance>("Copium Engine"));
vulkan->SetWindow(std::make_unique<Window>(*vulkan, "Copium Engine", 1920, 1080, Window::Mode::Windowed));
vulkan->SetDevice(std::make_unique<Device>(*vulkan));
vulkan->SetSwapChain(std::make_unique<SwapChain>(*vulkan));
CP_ASSERT(vulkan->Valid(), "Vulkan Manager was not initialized correctly");
} }
void Application::InitializeFrameBuffer() void Application::InitializeFrameBuffer()
{ {
framebuffer = std::make_unique<Framebuffer>(*vulkan, vulkan->GetSwapChain().GetExtent().width, vulkan->GetSwapChain().GetExtent().height); framebuffer = std::make_unique<Framebuffer>(Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height);
} }
void Application::InitializeRenderer() void Application::InitializeRenderer()
{ {
renderer = std::make_unique<Renderer>(*vulkan, framebuffer->GetRenderPass(), *descriptorPool); renderer = std::make_unique<Renderer>(framebuffer->GetRenderPass());
} }
void Application::InitializeTextureSampler() void Application::InitializeTextureSampler()
{ {
texture2D = std::make_unique<Texture2D>(*vulkan, "res/textures/texture.png"); texture2D = std::make_unique<Texture2D>("res/textures/texture.png");
texture2D2 = std::make_unique<Texture2D>(*vulkan, "res/textures/texture2.png"); texture2D2 = std::make_unique<Texture2D>("res/textures/texture2.png");
} }
void Application::InitializeDescriptorSets() void Application::InitializeDescriptorSets()
{ {
descriptorPool = std::make_unique<DescriptorPool>(*vulkan); descriptorPool = std::make_unique<DescriptorPool>();
descriptorSet = graphicsPipeline->CreateDescriptorSet(*descriptorPool, 0); descriptorSet = graphicsPipeline->CreateDescriptorSet(*descriptorPool, 0);
descriptorSet->SetSampler(*texture2D, 1); descriptorSet->SetSampler(*texture2D, 1);
@@ -116,22 +102,22 @@ namespace Copium
{ {
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"}; PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
creator.SetVertexDescriptor(Vertex::GetDescriptor()); creator.SetVertexDescriptor(Vertex::GetDescriptor());
graphicsPipeline = std::make_unique<Pipeline>(*vulkan, creator); graphicsPipeline = std::make_unique<Pipeline>(creator);
PipelineCreator creatorPassthrough{vulkan->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"}; PipelineCreator creatorPassthrough{Vulkan::GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor()); creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*vulkan, creatorPassthrough); graphicsPipelinePassthrough = std::make_unique<Pipeline>(creatorPassthrough);
} }
void Application::InitializeMesh() void Application::InitializeMesh()
{ {
mesh = std::make_unique<Mesh>(*vulkan, vertices, indices); mesh = std::make_unique<Mesh>(vertices, indices);
meshPassthrough = std::make_unique<Mesh>(*vulkan, verticesPassthrough, indicesPassthrough); meshPassthrough = std::make_unique<Mesh>(verticesPassthrough, indicesPassthrough);
} }
void Application::InitializeCommandBuffer() void Application::InitializeCommandBuffer()
{ {
commandBuffer = std::make_unique<CommandBuffer>(*vulkan, CommandBuffer::Type::Dynamic); commandBuffer = std::make_unique<CommandBuffer>(CommandBuffer::Type::Dynamic);
} }
void Application::RecordCommandBuffer() void Application::RecordCommandBuffer()
@@ -164,7 +150,7 @@ namespace Copium
framebuffer->Unbind(*commandBuffer); framebuffer->Unbind(*commandBuffer);
vulkan->GetSwapChain().BeginFrameBuffer(*commandBuffer); Vulkan::GetSwapChain().BeginFrameBuffer(*commandBuffer);
graphicsPipelinePassthrough->Bind(*commandBuffer); graphicsPipelinePassthrough->Bind(*commandBuffer);
graphicsPipelinePassthrough->SetDescriptorSet(*descriptorSetPassthrough); graphicsPipelinePassthrough->SetDescriptorSet(*descriptorSetPassthrough);
@@ -173,7 +159,7 @@ namespace Copium
meshPassthrough->Bind(*commandBuffer); meshPassthrough->Bind(*commandBuffer);
meshPassthrough->Render(*commandBuffer); meshPassthrough->Render(*commandBuffer);
vulkan->GetSwapChain().EndFrameBuffer(*commandBuffer); Vulkan::GetSwapChain().EndFrameBuffer(*commandBuffer);
commandBuffer->End(); commandBuffer->End();
} }
+1 -10
View File
@@ -1,18 +1,12 @@
#pragma once #pragma once
#include "copium/buffer/Framebuffer.h" #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/mesh/Mesh.h"
#include "copium/pipeline/DescriptorPool.h" #include "copium/pipeline/DescriptorPool.h"
#include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/DescriptorSet.h"
#include "copium/pipeline/Pipeline.h" #include "copium/pipeline/Pipeline.h"
#include "copium/sampler/Texture2D.h"
#include "copium/renderer/Renderer.h" #include "copium/renderer/Renderer.h"
#include "copium/sampler/Texture2D.h"
namespace Copium namespace Copium
{ {
@@ -20,8 +14,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Application); CP_DELETE_COPY_AND_MOVE_CTOR(Application);
private: private:
std::unique_ptr<Vulkan> vulkan;
std::unique_ptr<Instance> instance;
std::unique_ptr<Renderer> renderer; std::unique_ptr<Renderer> renderer;
std::unique_ptr<Framebuffer> framebuffer; std::unique_ptr<Framebuffer> framebuffer;
std::unique_ptr<Texture2D> texture2D; std::unique_ptr<Texture2D> texture2D;
@@ -42,7 +34,6 @@ namespace Copium
bool Update(); bool Update();
private: private:
void InitializeVulkan();
void InitializeFrameBuffer(); void InitializeFrameBuffer();
void InitializeRenderer(); void InitializeRenderer();
void InitializeTextureSampler(); void InitializeTextureSampler();
@@ -13,6 +13,7 @@ namespace Copium
CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger); CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger);
private: private:
Instance& instance; Instance& instance;
VkDebugUtilsMessengerEXT debugMessenger; VkDebugUtilsMessengerEXT debugMessenger;
public: public:
DebugMessenger(Instance& instance); DebugMessenger(Instance& instance);
+7 -9
View File
@@ -1,13 +1,11 @@
#include "Device.h" #include "Device.h"
#include "copium/core/Instance.h" #include "copium/core/Vulkan.h"
#include "copium/core/SwapChain.h"
#include "copium/core/Window.h" #include "copium/core/Window.h"
namespace Copium namespace Copium
{ {
Device::Device(Vulkan& vulkan) Device::Device()
: vulkan{vulkan}
{ {
SelectPhysicalDevice(); SelectPhysicalDevice();
InitializeLogicalDevice(); InitializeLogicalDevice();
@@ -60,11 +58,11 @@ namespace Copium
void Device::SelectPhysicalDevice() void Device::SelectPhysicalDevice()
{ {
uint32_t deviceCount; uint32_t deviceCount;
vkEnumeratePhysicalDevices(vulkan.GetInstance(), &deviceCount, nullptr); vkEnumeratePhysicalDevices(Vulkan::GetInstance(), &deviceCount, nullptr);
CP_ASSERT(deviceCount != 0, "SelectPhysicaDevice : No available devices support Vulkan"); CP_ASSERT(deviceCount != 0, "SelectPhysicaDevice : No available devices support Vulkan");
std::vector<VkPhysicalDevice> devices(deviceCount); std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(vulkan.GetInstance(), &deviceCount, devices.data()); vkEnumeratePhysicalDevices(Vulkan::GetInstance(), &deviceCount, devices.data());
CP_INFO("SelectPhysicaDevice : Available devices:"); CP_INFO("SelectPhysicaDevice : Available devices:");
for (auto&& device : devices) for (auto&& device : devices)
{ {
@@ -88,7 +86,7 @@ namespace Copium
void Device::InitializeLogicalDevice() void Device::InitializeLogicalDevice()
{ {
QueueFamiliesQuery query{vulkan.GetWindow().GetSurface(), physicalDevice}; QueueFamiliesQuery query{Vulkan::GetWindow().GetSurface(), physicalDevice};
float queuePriority = 1.0f; float queuePriority = 1.0f;
@@ -144,14 +142,14 @@ namespace Copium
if (!deviceFeatures.fillModeNonSolid || !deviceFeatures.samplerAnisotropy) if (!deviceFeatures.fillModeNonSolid || !deviceFeatures.samplerAnisotropy)
return false; return false;
QueueFamiliesQuery query{vulkan.GetWindow().GetSurface(), device}; QueueFamiliesQuery query{Vulkan::GetWindow().GetSurface(), device};
if (!query.AllRequiredFamiliesSupported()) if (!query.AllRequiredFamiliesSupported())
return false; return false;
if (!CheckDeviceExtensionSupport(device)) if (!CheckDeviceExtensionSupport(device))
return false; return false;
SwapChainSupportDetails details{vulkan.GetWindow().GetSurface(), device}; SwapChainSupportDetails details{Vulkan::GetWindow().GetSurface(), device};
if (!details.Valid()) if (!details.Valid())
return false; return false;
+1 -4
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/core/QueueFamilies.h" #include "copium/core/QueueFamilies.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -13,8 +12,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Device); CP_DELETE_COPY_AND_MOVE_CTOR(Device);
private: private:
Vulkan& vulkan;
VkPhysicalDevice physicalDevice; VkPhysicalDevice physicalDevice;
VkDevice device; VkDevice device;
VkCommandPool commandPool; VkCommandPool commandPool;
@@ -27,7 +24,7 @@ namespace Copium
// TODO end // TODO end
public: public:
Device(Vulkan& vulkan); Device();
~Device(); ~Device();
VkQueue GetGraphicsQueue() const; VkQueue GetGraphicsQueue() const;
@@ -1,7 +1,6 @@
#include "Instance.h" #include "Instance.h"
#include "copium/core/QueueFamilies.h" #include "copium/core/QueueFamilies.h"
#include "copium/core/SwapChain.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
namespace Copium namespace Copium
@@ -110,12 +109,4 @@ namespace Copium
} }
return true; return true;
} }
void Instance::FramebufferResizeCallback(GLFWwindow* window, int width, int height)
{
Instance* instance = static_cast<Instance*>(glfwGetWindowUserPointer(window));
// TODO: Fix swap chain resizing again
// instance->swapChain->ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
CP_UNIMPLEMENTED("Framebuffer resizing currently not supported");
}
} }
-2
View File
@@ -3,7 +3,6 @@
#include "copium/core/DebugMessenger.h" #include "copium/core/DebugMessenger.h"
#include "copium/util/Timer.h" #include "copium/util/Timer.h"
#include <vulkan/vulkan.hpp>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <set> #include <set>
@@ -36,6 +35,5 @@ namespace Copium
void InitializeDebugMessenger(); void InitializeDebugMessenger();
std::vector<const char*> GetRequiredExtensions(); std::vector<const char*> GetRequiredExtensions();
bool CheckLayerSupport(const std::vector<const char*>& layers); bool CheckLayerSupport(const std::vector<const char*>& layers);
static void FramebufferResizeCallback(GLFWwindow* window, int width, int height);
}; };
} }
+32 -35
View File
@@ -1,9 +1,7 @@
#include "copium/core/SwapChain.h" #include "copium/core/SwapChain.h"
#include "copium/core/Device.h"
#include "copium/core/Instance.h"
#include "copium/core/QueueFamilies.h" #include "copium/core/QueueFamilies.h"
#include "copium/core/Window.h" #include "copium/core/Vulkan.h"
#include "copium/sampler/Image.h" #include "copium/sampler/Image.h"
namespace Copium namespace Copium
@@ -34,8 +32,7 @@ namespace Copium
return !formats.empty() && !presentModes.empty(); return !formats.empty() && !presentModes.empty();
} }
SwapChain::SwapChain(Vulkan& vulkan) SwapChain::SwapChain()
: vulkan{vulkan}
{ {
Initialize(); Initialize();
InitializeImageViews(); InitializeImageViews();
@@ -49,12 +46,12 @@ namespace Copium
{ {
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
{ {
vkDestroyFence(vulkan.GetDevice(), inFlightFences[i], nullptr); vkDestroyFence(Vulkan::GetDevice(), inFlightFences[i], nullptr);
vkDestroySemaphore(vulkan.GetDevice(), renderFinishedSemaphores[i], nullptr); vkDestroySemaphore(Vulkan::GetDevice(), renderFinishedSemaphores[i], nullptr);
vkDestroySemaphore(vulkan.GetDevice(), imageAvailableSemaphores[i], nullptr); vkDestroySemaphore(Vulkan::GetDevice(), imageAvailableSemaphores[i], nullptr);
} }
Destroy(); Destroy();
vkDestroyRenderPass(vulkan.GetDevice(), renderPass, nullptr); vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr);
} }
void SwapChain::BeginFrameBuffer(const CommandBuffer& commandBuffer) const void SwapChain::BeginFrameBuffer(const CommandBuffer& commandBuffer) const
@@ -114,15 +111,15 @@ namespace Copium
bool SwapChain::BeginPresent() 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) if (result == VK_ERROR_OUT_OF_DATE_KHR)
{ {
Recreate(); Recreate();
return false; return false;
} }
vkResetFences(vulkan.GetDevice(), 1, &inFlightFences[flightIndex]); vkResetFences(Vulkan::GetDevice(), 1, &inFlightFences[flightIndex]);
return true; return true;
} }
@@ -140,7 +137,7 @@ namespace Copium
submitInfo.signalSemaphoreCount = 1; submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[flightIndex]; 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() void SwapChain::EndPresent()
@@ -154,7 +151,7 @@ namespace Copium
presentInfo.pImageIndices = &imageIndex; presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr; 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) if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || resizeFramebuffer)
{ {
Recreate(); Recreate();
@@ -173,14 +170,14 @@ namespace Copium
{ {
int width = 0; int width = 0;
int height = 0; int height = 0;
glfwGetFramebufferSize(vulkan.GetWindow().GetWindow(), &width, &height); glfwGetFramebufferSize(Vulkan::GetWindow().GetWindow(), &width, &height);
while (width == 0 || height == 0) while (width == 0 || height == 0)
{ {
glfwGetFramebufferSize(vulkan.GetWindow().GetWindow(), &width, &height); glfwGetFramebufferSize(Vulkan::GetWindow().GetWindow(), &width, &height);
glfwWaitEvents(); glfwWaitEvents();
} }
vkDeviceWaitIdle(vulkan.GetDevice()); vkDeviceWaitIdle(Vulkan::GetDevice());
Destroy(); Destroy();
@@ -197,11 +194,11 @@ namespace Copium
void SwapChain::Initialize() void SwapChain::Initialize()
{ {
SwapChainSupportDetails swapChainSupport{vulkan.GetWindow().GetSurface(), vulkan.GetDevice().GetPhysicalDevice()}; SwapChainSupportDetails swapChainSupport{Vulkan::GetWindow().GetSurface(), Vulkan::GetDevice().GetPhysicalDevice()};
VkSurfaceFormatKHR format = SelectSwapSurfaceFormat(swapChainSupport.formats); VkSurfaceFormatKHR format = SelectSwapSurfaceFormat(swapChainSupport.formats);
VkPresentModeKHR presentMode = SelectSwapPresentMode(swapChainSupport.presentModes); VkPresentModeKHR presentMode = SelectSwapPresentMode(swapChainSupport.presentModes);
extent = SelectSwapExtent(vulkan.GetWindow().GetWindow(), swapChainSupport.capabilities); extent = SelectSwapExtent(Vulkan::GetWindow().GetWindow(), swapChainSupport.capabilities);
imageFormat = format.format; imageFormat = format.format;
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
if (swapChainSupport.capabilities.maxImageCount != 0) if (swapChainSupport.capabilities.maxImageCount != 0)
@@ -209,12 +206,12 @@ namespace Copium
imageCount = std::min(imageCount, swapChainSupport.capabilities.maxImageCount); imageCount = std::min(imageCount, swapChainSupport.capabilities.maxImageCount);
} }
QueueFamiliesQuery queueFamilies{vulkan.GetWindow().GetSurface(), vulkan.GetDevice().GetPhysicalDevice()}; QueueFamiliesQuery queueFamilies{Vulkan::GetWindow().GetSurface(), Vulkan::GetDevice().GetPhysicalDevice()};
std::vector<uint32_t> queueFamilyIndices{queueFamilies.graphicsFamily.value(), queueFamilies.presentFamily.value()}; std::vector<uint32_t> queueFamilyIndices{queueFamilies.graphicsFamily.value(), queueFamilies.presentFamily.value()};
VkSwapchainCreateInfoKHR createInfo{}; VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = vulkan.GetWindow().GetSurface(); createInfo.surface = Vulkan::GetWindow().GetSurface();
createInfo.minImageCount = imageCount; createInfo.minImageCount = imageCount;
createInfo.imageFormat = format.format; createInfo.imageFormat = format.format;
createInfo.imageColorSpace = format.colorSpace; createInfo.imageColorSpace = format.colorSpace;
@@ -239,11 +236,11 @@ namespace Copium
createInfo.pQueueFamilyIndices = nullptr; 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); images.resize(imageCount);
vkGetSwapchainImagesKHR(vulkan.GetDevice(), handle, &imageCount, images.data()); vkGetSwapchainImagesKHR(Vulkan::GetDevice(), handle, &imageCount, images.data());
} }
void SwapChain::InitializeImageViews() void SwapChain::InitializeImageViews()
@@ -251,13 +248,13 @@ namespace Copium
imageViews.resize(images.size()); imageViews.resize(images.size());
for (size_t i = 0; i < images.size(); i++) 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() void SwapChain::InitializeDepthAttachment()
{ {
depthAttachment = std::make_unique<DepthAttachment>(vulkan, extent.width, extent.height); depthAttachment = std::make_unique<DepthAttachment>(extent.width, extent.height);
} }
void SwapChain::InitializeRenderPass() void SwapChain::InitializeRenderPass()
@@ -273,7 +270,7 @@ namespace Copium
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentDescription depthAttachment{}; VkAttachmentDescription depthAttachment{};
depthAttachment.format = Image::SelectDepthFormat(vulkan); depthAttachment.format = Image::SelectDepthFormat();
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@@ -314,7 +311,7 @@ namespace Copium
renderPassCreateInfo.dependencyCount = 1; renderPassCreateInfo.dependencyCount = 1;
renderPassCreateInfo.pDependencies = &dependency; 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() void SwapChain::InitializeFramebuffers()
@@ -334,7 +331,7 @@ namespace Copium
createInfo.height = extent.height; createInfo.height = extent.height;
createInfo.layers = 1; 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; semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) 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, &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, &renderFinishedSemaphores[i]), "InitializeSyncObjects : Failed to initialize render finished semaphore");
VkFenceCreateInfo fenceCreateInfo{}; VkFenceCreateInfo fenceCreateInfo{};
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; 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) for (auto&& framebuffer : framebuffers)
{ {
vkDestroyFramebuffer(vulkan.GetDevice(), framebuffer, nullptr); vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
} }
for (auto&& swapChainImageView : imageViews) for (auto&& swapChainImageView : imageViews)
{ {
vkDestroyImageView(vulkan.GetDevice(), swapChainImageView, nullptr); vkDestroyImageView(Vulkan::GetDevice(), swapChainImageView, nullptr);
} }
vkDestroySwapchainKHR(vulkan.GetDevice(), handle, nullptr); vkDestroySwapchainKHR(Vulkan::GetDevice(), handle, nullptr);
} }
VkSurfaceFormatKHR SwapChain::SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats) VkSurfaceFormatKHR SwapChain::SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
+1 -4
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/sampler/DepthAttachment.h" #include "copium/sampler/DepthAttachment.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -27,8 +26,6 @@ namespace Copium
public: public:
static const int MAX_FRAMES_IN_FLIGHT = 2; static const int MAX_FRAMES_IN_FLIGHT = 2;
private: private:
Vulkan& vulkan;
VkSwapchainKHR handle; VkSwapchainKHR handle;
VkRenderPass renderPass; VkRenderPass renderPass;
VkFormat imageFormat; VkFormat imageFormat;
@@ -46,7 +43,7 @@ namespace Copium
std::vector<VkFence> inFlightFences; std::vector<VkFence> inFlightFences;
public: public:
SwapChain(Vulkan& vulkan); SwapChain();
~SwapChain(); ~SwapChain();
void BeginFrameBuffer(const CommandBuffer& commandBuffer) const; void BeginFrameBuffer(const CommandBuffer& commandBuffer) const;
+19 -23
View File
@@ -1,48 +1,44 @@
#include "copium/core/Vulkan.h" #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 namespace Copium
{ {
void Vulkan::SetInstance(std::unique_ptr<Instance>&& instance) std::unique_ptr<Instance> Vulkan::instance;
std::unique_ptr<Window> Vulkan::window;
std::unique_ptr<Device> Vulkan::device;
std::unique_ptr<SwapChain> Vulkan::swapChain;
void Vulkan::Initialize()
{ {
this->instance = std::move(instance); instance = std::make_unique<Instance>("Copium Engine");
window = std::make_unique<Window>( "Copium Engine", 1920, 1080, Window::Mode::Windowed);
device = std::make_unique<Device>();
swapChain = std::make_unique<SwapChain>();
} }
void Vulkan::SetWindow(std::unique_ptr<Window>&& window) void Vulkan::Destroy()
{ {
this->window = std::move(window); swapChain.reset();
device.reset();
window.reset();
instance.reset();
} }
void Vulkan::SetDevice(std::unique_ptr<Device>&& device) Instance& Vulkan::GetInstance()
{
this->device = std::move(device);
}
void Vulkan::SetSwapChain(std::unique_ptr<SwapChain>&& swapChain)
{
this->swapChain = std::move(swapChain);
}
Instance& Vulkan::GetInstance() const
{ {
return *instance; return *instance;
} }
Window& Vulkan::GetWindow() const Window& Vulkan::GetWindow()
{ {
return *window; return *window;
} }
Device& Vulkan::GetDevice() const Device& Vulkan::GetDevice()
{ {
return *device; return *device;
} }
SwapChain& Vulkan::GetSwapChain() const SwapChain& Vulkan::GetSwapChain()
{ {
return *swapChain; return *swapChain;
} }
+18 -18
View File
@@ -1,31 +1,31 @@
#pragma once #pragma once
#include "copium/core/Device.h"
#include "copium/core/Instance.h"
#include "copium/core/SwapChain.h"
#include "copium/core/Window.h"
#include "copium/util/Common.h"
#include <memory> #include <memory>
namespace Copium namespace Copium
{ {
class Instance;
class Window;
class Device;
class SwapChain;
class Vulkan class Vulkan
{ {
CP_STATIC_CLASS(Vulkan);
private: private:
std::unique_ptr<Instance> instance; static std::unique_ptr<Instance> instance;
std::unique_ptr<Window> window; static std::unique_ptr<Window> window;
std::unique_ptr<Device> device; static std::unique_ptr<Device> device;
std::unique_ptr<SwapChain> swapChain; static std::unique_ptr<SwapChain> swapChain;
public: public:
void SetInstance(std::unique_ptr<Instance>&& instance); static void Initialize();
void SetWindow(std::unique_ptr<Window>&& window); static void Destroy();
void SetDevice(std::unique_ptr<Device>&& device); static Instance& GetInstance();
void SetSwapChain(std::unique_ptr<SwapChain>&& swapChain); static Window& GetWindow();
Instance& GetInstance() const; static Device& GetDevice();
Window& GetWindow() const; static SwapChain& GetSwapChain();
Device& GetDevice() const; static bool Valid();
SwapChain& GetSwapChain() const;
bool Valid();
}; };
} }
+5 -8
View File
@@ -1,12 +1,10 @@
#include "copium/core/Window.h" #include "copium/core/Window.h"
#include "copium/core/Instance.h" #include "copium/core/Vulkan.h"
#include "copium/core/SwapChain.h"
namespace Copium namespace Copium
{ {
Window::Window(Vulkan& vulkan, const std::string& windowName, int width, int height, Mode mode) Window::Window(const std::string& windowName, int width, int height, Mode mode)
: vulkan{vulkan}
{ {
InitializeWindow(windowName, width, height, mode); InitializeWindow(windowName, width, height, mode);
InitializeSurface(); InitializeSurface();
@@ -14,7 +12,7 @@ namespace Copium
Window::~Window() Window::~Window()
{ {
vkDestroySurfaceKHR(vulkan.GetInstance(), surface, nullptr); vkDestroySurfaceKHR(Vulkan::GetInstance(), surface, nullptr);
glfwDestroyWindow(window); glfwDestroyWindow(window);
} }
@@ -66,13 +64,12 @@ namespace Copium
void Window::InitializeSurface() 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) void Window::FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height)
{ {
Window* window = static_cast<Window*>(glfwGetWindowUserPointer(glfwWindow)); Vulkan::GetSwapChain().ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
window->vulkan.GetSwapChain().ResizeFramebuffer(); // TODO: Should maybe be handled by an event system?
} }
} }
+1 -4
View File
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@@ -16,13 +15,11 @@ namespace Copium
Fullscreen, BorderlessWindowed, Windowed Fullscreen, BorderlessWindowed, Windowed
}; };
private: private:
Vulkan& vulkan;
GLFWwindow* window; GLFWwindow* window;
VkSurfaceKHR surface; VkSurfaceKHR surface;
public: 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(); ~Window();
VkSurfaceKHR GetSurface() const; VkSurfaceKHR GetSurface() const;
+4
View File
@@ -1,4 +1,5 @@
#include "copium/core/Application.h" #include "copium/core/Application.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include "copium/util/Timer.h" #include "copium/util/Timer.h"
@@ -7,6 +8,8 @@
int main() int main()
{ {
CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context"); CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context");
Copium::Vulkan::Initialize();
{ {
Copium::Application application; Copium::Application application;
Copium::Timer timer; Copium::Timer timer;
@@ -23,6 +26,7 @@ int main()
frames++; frames++;
} }
} }
Copium::Vulkan::Destroy();
glfwTerminate(); glfwTerminate();
return 0; return 0;
+4 -5
View File
@@ -3,7 +3,6 @@
#include "copium/buffer/IndexBuffer.h" #include "copium/buffer/IndexBuffer.h"
#include "copium/buffer/VertexBuffer.h" #include "copium/buffer/VertexBuffer.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <vector> #include <vector>
@@ -19,18 +18,18 @@ namespace Copium
std::unique_ptr<VertexBuffer> vertexBuffer; std::unique_ptr<VertexBuffer> vertexBuffer;
public: public:
template <typename T> template <typename T>
Mesh(Vulkan& vulkan, const std::vector<T>& vertices, const std::vector<uint16_t>& indices); Mesh(const std::vector<T>& vertices, const std::vector<uint16_t>& indices);
void Bind(const CommandBuffer& commandBuffer); void Bind(const CommandBuffer& commandBuffer);
void Render(const CommandBuffer& commandBuffer); void Render(const CommandBuffer& commandBuffer);
}; };
template <typename T> template <typename T>
Mesh::Mesh(Vulkan& vulkan, const std::vector<T>& vertices, const std::vector<uint16_t>& indices) Mesh::Mesh(const std::vector<T>& vertices, const std::vector<uint16_t>& indices)
{ {
indexBuffer = std::make_unique<IndexBuffer>(vulkan, indices.size()); indexBuffer = std::make_unique<IndexBuffer>(indices.size());
indexBuffer->UpdateStaging((void*)indices.data()); indexBuffer->UpdateStaging((void*)indices.data());
vertexBuffer = std::make_unique<VertexBuffer>(vulkan, T::GetDescriptor(), vertices.size()); vertexBuffer = std::make_unique<VertexBuffer>(T::GetDescriptor(), vertices.size());
vertexBuffer ->UpdateStaging(0, (void*)vertices.data()); vertexBuffer ->UpdateStaging(0, (void*)vertices.data());
} }
} }
@@ -1,12 +1,10 @@
#include "copium/pipeline/DescriptorPool.h" #include "copium/pipeline/DescriptorPool.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/core/SwapChain.h"
namespace Copium namespace Copium
{ {
DescriptorPool::DescriptorPool(Vulkan& vulkan) DescriptorPool::DescriptorPool()
: vulkan{vulkan}
{ {
std::vector<VkDescriptorPoolSize> poolSizes{2}; std::vector<VkDescriptorPoolSize> poolSizes{2};
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 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.maxSets = DESCRIPTOR_SET_COUNT * SwapChain::MAX_FRAMES_IN_FLIGHT;
createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; 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() DescriptorPool::~DescriptorPool()
{ {
vkDestroyDescriptorPool(vulkan.GetDevice(), descriptorPool, nullptr); vkDestroyDescriptorPool(Vulkan::GetDevice(), descriptorPool, nullptr);
} }
std::vector<VkDescriptorSet> DescriptorPool::AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout) std::vector<VkDescriptorSet> DescriptorPool::AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout)
@@ -40,13 +38,13 @@ namespace Copium
allocateInfo.descriptorSetCount = descriptorSets.size(); allocateInfo.descriptorSetCount = descriptorSets.size();
allocateInfo.pSetLayouts = layouts.data(); 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; return descriptorSets;
} }
void DescriptorPool::FreeDescriptorSets(const std::vector<VkDescriptorSet>& descriptorSets) void DescriptorPool::FreeDescriptorSets(const std::vector<VkDescriptorSet>& descriptorSets)
{ {
vkFreeDescriptorSets(vulkan.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data()); vkFreeDescriptorSets(Vulkan::GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
} }
} }
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include "copium/core/Vulkan.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -11,12 +10,10 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool); CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool);
private: private:
Vulkan& vulkan;
VkDescriptorPool descriptorPool; VkDescriptorPool descriptorPool;
static const int DESCRIPTOR_SET_COUNT = 100; static const int DESCRIPTOR_SET_COUNT = 100;
public: public:
DescriptorPool(Vulkan& vulkan); DescriptorPool();
~DescriptorPool(); ~DescriptorPool();
std::vector<VkDescriptorSet> AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout); std::vector<VkDescriptorSet> AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout);
@@ -1,12 +1,11 @@
#include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/DescriptorSet.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/core/SwapChain.h"
namespace Copium namespace Copium
{ {
DescriptorSet::DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings) DescriptorSet::DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings)
: vulkan{vulkan}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings} : descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings}
{ {
CP_ASSERT(!bindings.empty(), "DescriptorSet : cannot initialize DescriptorSet with empty ShaderBindings"); CP_ASSERT(!bindings.empty(), "DescriptorSet : cannot initialize DescriptorSet with empty ShaderBindings");
@@ -15,7 +14,7 @@ namespace Copium
{ {
if (binding.bindingType == BindingType::UniformBuffer) if (binding.bindingType == BindingType::UniformBuffer)
{ {
std::unique_ptr<UniformBuffer> uniformBuffer = std::make_unique<UniformBuffer>(vulkan, binding); std::unique_ptr<UniformBuffer> uniformBuffer = std::make_unique<UniformBuffer>(binding);
SetUniformBuffer(*uniformBuffer, binding.binding); SetUniformBuffer(*uniformBuffer, binding.binding);
uniformBuffers.emplace(binding.name, std::move(uniformBuffer)); uniformBuffers.emplace(binding.name, std::move(uniformBuffer));
} }
@@ -28,25 +27,6 @@ namespace Copium
descriptorPool.FreeDescriptorSets(descriptorSets); 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) void DescriptorSet::SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex)
{ {
for (size_t i = 0; i < descriptorSets.size(); ++i) for (size_t i = 0; i < descriptorSets.size(); ++i)
@@ -62,16 +42,16 @@ namespace Copium
descriptorWrite.pBufferInfo = nullptr; descriptorWrite.pBufferInfo = nullptr;
descriptorWrite.pImageInfo = &imageInfo; descriptorWrite.pImageInfo = &imageInfo;
descriptorWrite.pTexelBufferView = nullptr; 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) 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{}; VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSets[vulkan.GetSwapChain().GetFlightIndex()]; descriptorWrite.dstSet = descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()];
descriptorWrite.dstBinding = binding; descriptorWrite.dstBinding = binding;
descriptorWrite.dstArrayElement = arrayIndex; descriptorWrite.dstArrayElement = arrayIndex;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
@@ -79,7 +59,7 @@ namespace Copium
descriptorWrite.pBufferInfo = nullptr; descriptorWrite.pBufferInfo = nullptr;
descriptorWrite.pImageInfo = &imageInfo; descriptorWrite.pImageInfo = &imageInfo;
descriptorWrite.pTexelBufferView = nullptr; descriptorWrite.pTexelBufferView = nullptr;
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr); vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr);
} }
void DescriptorSet::SetSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding) void DescriptorSet::SetSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding)
@@ -99,7 +79,7 @@ namespace Copium
descriptorWrites[j].pImageInfo = &imageInfo; descriptorWrites[j].pImageInfo = &imageInfo;
descriptorWrites[j].pTexelBufferView = nullptr; 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 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);
}
}
} }
@@ -20,7 +20,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet); CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet);
private: private:
Vulkan& vulkan;
DescriptorPool& descriptorPool; DescriptorPool& descriptorPool;
VkDescriptorSetLayout descriptorSetLayout; VkDescriptorSetLayout descriptorSetLayout;
@@ -29,10 +28,9 @@ namespace Copium
std::map<std::string, std::unique_ptr<UniformBuffer>> uniformBuffers; std::map<std::string, std::unique_ptr<UniformBuffer>> uniformBuffers;
public: public:
DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings); DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings);
~DescriptorSet(); ~DescriptorSet();
void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding);
void SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0); void SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
void SetSamplerDynamic(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<const Sampler*>& sampler, uint32_t binding); void SetSamplers(const std::vector<const Sampler*>& sampler, uint32_t binding);
@@ -40,5 +38,7 @@ namespace Copium
uint32_t GetSetIndex() const; uint32_t GetSetIndex() const;
operator VkDescriptorSet() const; operator VkDescriptorSet() const;
private:
void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding);
}; };
} }
+11 -11
View File
@@ -1,13 +1,13 @@
#include "copium/pipeline/Pipeline.h" #include "copium/pipeline/Pipeline.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/pipeline/Shader.h" #include "copium/pipeline/Shader.h"
#include "copium/util/FileSystem.h" #include "copium/util/FileSystem.h"
namespace Copium namespace Copium
{ {
Pipeline::Pipeline(Vulkan& vulkan, PipelineCreator creator) Pipeline::Pipeline(PipelineCreator creator)
: vulkan{vulkan}, shaderReflector{creator.shaderReflector} : shaderReflector{creator.shaderReflector}
{ {
InitializeDescriptorSetLayout(creator); InitializeDescriptorSetLayout(creator);
InitializePipeline(creator); InitializePipeline(creator);
@@ -15,11 +15,11 @@ namespace Copium
Pipeline::~Pipeline() Pipeline::~Pipeline()
{ {
vkDestroyPipeline(vulkan.GetDevice(), graphicsPipeline, nullptr); vkDestroyPipeline(Vulkan::GetDevice(), graphicsPipeline, nullptr);
vkDestroyPipelineLayout(vulkan.GetDevice(), pipelineLayout, nullptr); vkDestroyPipelineLayout(Vulkan::GetDevice(), pipelineLayout, nullptr);
for (auto&& descriptorSetLayout : descriptorSetLayouts) for (auto&& descriptorSetLayout : descriptorSetLayouts)
{ {
vkDestroyDescriptorSetLayout(vulkan.GetDevice(), descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr);
} }
} }
@@ -49,7 +49,7 @@ namespace Copium
bindings.emplace(binding); bindings.emplace(binding);
} }
return std::make_unique<DescriptorSet>(vulkan, descriptorPool, descriptorSetLayouts[setIndex], bindings); return std::make_unique<DescriptorSet>(descriptorPool, descriptorSetLayouts[setIndex], bindings);
} }
void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator) void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator)
@@ -76,13 +76,13 @@ namespace Copium
createInfo.bindingCount = layoutBindings.size(); createInfo.bindingCount = layoutBindings.size();
createInfo.pBindings = layoutBindings.data(); 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) 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{}; VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{};
vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
@@ -192,7 +192,7 @@ namespace Copium
pipelineLayoutCreateInfo.pushConstantRangeCount = 0; pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pipelineLayoutCreateInfo.pPushConstantRanges = nullptr; 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<VkPipelineShaderStageCreateInfo>& shaderStages = shader.GetShaderStages(); const std::vector<VkPipelineShaderStageCreateInfo>& shaderStages = shader.GetShaderStages();
VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{}; VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{};
@@ -213,6 +213,6 @@ namespace Copium
graphicsPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; graphicsPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
graphicsPipelineCreateInfo.basePipelineIndex = -1; 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");
} }
} }
+1 -4
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/DescriptorSet.h"
#include "copium/pipeline/PipelineCreator.h" #include "copium/pipeline/PipelineCreator.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -15,8 +14,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline); CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline);
private: private:
Vulkan& vulkan;
ShaderReflector shaderReflector; ShaderReflector shaderReflector;
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{}; std::vector<VkDescriptorSetLayout> descriptorSetLayouts{};
std::vector<VkDescriptorSet> boundDescriptorSets; std::vector<VkDescriptorSet> boundDescriptorSets;
@@ -24,7 +21,7 @@ namespace Copium
VkPipeline graphicsPipeline; VkPipeline graphicsPipeline;
public: public:
Pipeline(Vulkan& vulkan, PipelineCreator creator); Pipeline(PipelineCreator creator);
~Pipeline(); ~Pipeline();
void Bind(const CommandBuffer& commandBuffer); void Bind(const CommandBuffer& commandBuffer);
void SetDescriptorSet(const DescriptorSet& descriptorSet); void SetDescriptorSet(const DescriptorSet& descriptorSet);
@@ -1,11 +1,12 @@
#pragma once #pragma once
#include "copium/pipeline/VertexDescriptor.h"
#include "copium/pipeline/ShaderReflector.h" #include "copium/pipeline/ShaderReflector.h"
#include "copium/pipeline/VertexDescriptor.h"
#include <vulkan/vulkan.hpp>
#include <map> #include <map>
#include <string> #include <string>
#include <vulkan/vulkan.hpp>
namespace Copium namespace Copium
{ {
+5 -6
View File
@@ -1,12 +1,11 @@
#include "Shader.h" #include "Shader.h"
#include "copium/util/FileSystem.h" #include "copium/util/FileSystem.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
namespace Copium namespace Copium
{ {
Shader::Shader(Vulkan& vulkan, Type type, const std::string& vertexInput, const std::string& fragmentInput) Shader::Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput)
: vulkan{vulkan}
{ {
switch (type) switch (type)
{ {
@@ -46,8 +45,8 @@ namespace Copium
Shader::~Shader() Shader::~Shader()
{ {
vkDestroyShaderModule(vulkan.GetDevice(), vertShaderModule, nullptr); vkDestroyShaderModule(Vulkan::GetDevice(), vertShaderModule, nullptr);
vkDestroyShaderModule(vulkan.GetDevice(), fragShaderModule, nullptr); vkDestroyShaderModule(Vulkan::GetDevice(), fragShaderModule, nullptr);
} }
const std::vector<VkPipelineShaderStageCreateInfo> Shader::GetShaderStages() const const std::vector<VkPipelineShaderStageCreateInfo> Shader::GetShaderStages() const
@@ -126,7 +125,7 @@ namespace Copium
createInfo.pCode = data; createInfo.pCode = data;
VkShaderModule shaderModule; 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; return shaderModule;
} }
+1 -4
View File
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <shaderc/shaderc.hpp> #include <shaderc/shaderc.hpp>
@@ -18,13 +17,11 @@ namespace Copium
}; };
private: private:
Vulkan& vulkan;
VkShaderModule vertShaderModule; VkShaderModule vertShaderModule;
VkShaderModule fragShaderModule; VkShaderModule fragShaderModule;
std::vector<VkPipelineShaderStageCreateInfo> shaderStages; std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
public: 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(); ~Shader();
const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const; const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const;
@@ -2,8 +2,8 @@
#include "copium/pipeline/ShaderBinding.h" #include "copium/pipeline/ShaderBinding.h"
#include <string>
#include <set> #include <set>
#include <string>
namespace Copium namespace Copium
{ {
+3 -4
View File
@@ -4,10 +4,9 @@
namespace Copium namespace Copium
{ {
Batch::Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers) Batch::Batch(Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers)
: vulkan{vulkan}, : pipeline{pipeline},
pipeline{pipeline}, vertexBuffer{RendererVertex::GetDescriptor(), vertexCount},
vertexBuffer{vulkan, RendererVertex::GetDescriptor(), vertexCount},
descriptorSet{pipeline.CreateDescriptorSet(descriptorPool, 0)} descriptorSet{pipeline.CreateDescriptorSet(descriptorPool, 0)}
{ {
descriptorSet->SetSamplers(samplers, 0); descriptorSet->SetSamplers(samplers, 0);
+1 -2
View File
@@ -11,13 +11,12 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Batch); CP_DELETE_COPY_AND_MOVE_CTOR(Batch);
private: private:
Vulkan& vulkan;
Pipeline& pipeline; Pipeline& pipeline;
RendererVertexBuffer vertexBuffer; RendererVertexBuffer vertexBuffer;
std::unique_ptr<DescriptorSet> descriptorSet; std::unique_ptr<DescriptorSet> descriptorSet;
public: public:
Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers); Batch(Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
RendererVertexBuffer& GetVertexBuffer(); RendererVertexBuffer& GetVertexBuffer();
DescriptorSet& GetDescriptorSet(); DescriptorSet& GetDescriptorSet();
}; };
+10 -10
View File
@@ -1,6 +1,6 @@
#include "copium/renderer/Renderer.h" #include "copium/renderer/Renderer.h"
#include "copium/core/SwapChain.h" #include "copium/core/Vulkan.h"
#include "copium/pipeline/PipelineCreator.h" #include "copium/pipeline/PipelineCreator.h"
#include "copium/renderer/RendererVertex.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_INDICES = 6 * MAX_NUM_QUADS;
static constexpr int MAX_NUM_TEXTURES = 32; static constexpr int MAX_NUM_TEXTURES = 32;
Renderer::Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool) Renderer::Renderer(VkRenderPass renderPass)
: vulkan{vulkan}, : descriptorPool{},
descriptorPool{vulkan}, ibo{MAX_NUM_INDICES},
ibo{vulkan, MAX_NUM_INDICES}, emptyTexture{{0, 0, 0, 255}, 1, 1},
emptyTexture{vulkan, {1, 0, 0, 0}, 1, 1},
samplers{MAX_NUM_TEXTURES, &emptyTexture} samplers{MAX_NUM_TEXTURES, &emptyTexture}
{ {
InitializeIndexBuffer(); InitializeIndexBuffer();
@@ -99,7 +98,7 @@ namespace Copium
PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"}; PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"};
creator.SetVertexDescriptor(RendererVertex::GetDescriptor()); creator.SetVertexDescriptor(RendererVertex::GetDescriptor());
creator.SetDepthTest(false); creator.SetDepthTest(false);
graphicsPipeline = std::make_unique<Pipeline>(vulkan, creator); graphicsPipeline = std::make_unique<Pipeline>(creator);
} }
int Renderer::AllocateSampler(const Sampler& sampler) int Renderer::AllocateSampler(const Sampler& sampler)
@@ -117,6 +116,7 @@ namespace Copium
NextBatch(); NextBatch();
} }
batches[batchIndex]->GetDescriptorSet().SetSamplerDynamic(sampler, 0, textureCount); batches[batchIndex]->GetDescriptorSet().SetSamplerDynamic(sampler, 0, textureCount);
samplers[textureCount] = &sampler; samplers[textureCount] = &sampler;
textureCount++; textureCount++;
return textureCount - 1; return textureCount - 1;
@@ -144,13 +144,13 @@ namespace Copium
void Renderer::NextBatch() void Renderer::NextBatch()
{ {
batchIndex++; batchIndex++;
std::fill(samplers.begin(), samplers.end(), &emptyTexture);
if (batchIndex >= batches.size()) if (batchIndex >= batches.size())
{ {
batches.emplace_back(std::make_unique<Batch>(vulkan, *graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers)); batches.emplace_back(std::make_unique<Batch>(*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; quadCount = 0;
textureCount = 0; textureCount = 0;
std::fill(samplers.begin(), samplers.end(), &emptyTexture);
} }
} }
+1 -4
View File
@@ -3,7 +3,6 @@
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/buffer/IndexBuffer.h" #include "copium/buffer/IndexBuffer.h"
#include "copium/buffer/RendererVertexBuffer.h" #include "copium/buffer/RendererVertexBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/pipeline/Pipeline.h" #include "copium/pipeline/Pipeline.h"
#include "copium/renderer/Batch.h" #include "copium/renderer/Batch.h"
#include "copium/sampler/Texture2D.h" #include "copium/sampler/Texture2D.h"
@@ -18,8 +17,6 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Renderer); CP_DELETE_COPY_AND_MOVE_CTOR(Renderer);
private: private:
Vulkan& vulkan;
DescriptorPool descriptorPool; DescriptorPool descriptorPool;
IndexBuffer ibo; IndexBuffer ibo;
Texture2D emptyTexture; Texture2D emptyTexture;
@@ -34,7 +31,7 @@ namespace Copium
int textureCount; int textureCount;
void* mappedVertexBuffer; void* mappedVertexBuffer;
public: 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 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}); 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});
@@ -1,13 +1,12 @@
#include "copium/sampler/ColorAttachment.h" #include "copium/sampler/ColorAttachment.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/core/SwapChain.h"
#include "copium/sampler/Image.h" #include "copium/sampler/Image.h"
namespace Copium namespace Copium
{ {
ColorAttachment::ColorAttachment(Vulkan& vulkan, int width, int height) ColorAttachment::ColorAttachment(int width, int height)
: Sampler{vulkan} : Sampler{}
{ {
InitializeColorAttachment(width, height); InitializeColorAttachment(width, height);
} }
@@ -15,11 +14,11 @@ namespace Copium
ColorAttachment::~ColorAttachment() ColorAttachment::~ColorAttachment()
{ {
for (auto&& image : images) for (auto&& image : images)
vkDestroyImage(vulkan.GetDevice(), image, nullptr); vkDestroyImage(Vulkan::GetDevice(), image, nullptr);
for (auto&& imageMemory : imageMemories) for (auto&& imageMemory : imageMemories)
vkFreeMemory(vulkan.GetDevice(), imageMemory, nullptr); vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr);
for (auto&& imageView : imageViews) for (auto&& imageView : imageViews)
vkDestroyImageView(vulkan.GetDevice(), imageView, nullptr); vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
} }
VkDescriptorImageInfo ColorAttachment::GetDescriptorImageInfo(int index) const VkDescriptorImageInfo ColorAttachment::GetDescriptorImageInfo(int index) const
@@ -47,8 +46,8 @@ namespace Copium
imageMemories.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); imageMemories.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
for (size_t i = 0; i < images.size(); i++) 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]); 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(vulkan, images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT); imageViews[i] = Image::InitializeImageView(images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
} }
} }
} }
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/sampler/Sampler.h" #include "copium/sampler/Sampler.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -16,7 +15,7 @@ namespace Copium
std::vector<VkDeviceMemory> imageMemories; std::vector<VkDeviceMemory> imageMemories;
std::vector<VkImageView> imageViews; std::vector<VkImageView> imageViews;
public: public:
ColorAttachment(Vulkan& vulkan, int width, int height); ColorAttachment(int width, int height);
~ColorAttachment() override; ~ColorAttachment() override;
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;
@@ -1,21 +1,21 @@
#include "copium/sampler/DepthAttachment.h" #include "copium/sampler/DepthAttachment.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#include "copium/sampler/Image.h" #include "copium/sampler/Image.h"
namespace Copium namespace Copium
{ {
DepthAttachment::DepthAttachment(Vulkan& vulkan, int width, int height) DepthAttachment::DepthAttachment(int width, int height)
: Sampler{vulkan} : Sampler{}
{ {
InitializeDepthAttachment(width, height); InitializeDepthAttachment(width, height);
} }
DepthAttachment::~DepthAttachment() DepthAttachment::~DepthAttachment()
{ {
vkDestroyImage(vulkan.GetDevice(), image, nullptr); vkDestroyImage(Vulkan::GetDevice(), image, nullptr);
vkFreeMemory(vulkan.GetDevice(), imageMemory, nullptr); vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr);
vkDestroyImageView(vulkan.GetDevice(), imageView, nullptr); vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
} }
VkDescriptorImageInfo DepthAttachment::GetDescriptorImageInfo(int index) const VkDescriptorImageInfo DepthAttachment::GetDescriptorImageInfo(int index) const
@@ -34,8 +34,8 @@ namespace Copium
void DepthAttachment::InitializeDepthAttachment(int width, int height) void DepthAttachment::InitializeDepthAttachment(int width, int height)
{ {
VkFormat depthFormat = Image::SelectDepthFormat(vulkan); VkFormat depthFormat = Image::SelectDepthFormat();
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); 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(vulkan, image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT); imageView = Image::InitializeImageView(image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
} }
} }
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/sampler/Sampler.h" #include "copium/sampler/Sampler.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -16,7 +15,7 @@ namespace Copium
VkDeviceMemory imageMemory; VkDeviceMemory imageMemory;
VkImageView imageView; VkImageView imageView;
public: public:
DepthAttachment(Vulkan& vulkan, int width, int height); DepthAttachment(int width, int height);
~DepthAttachment() override; ~DepthAttachment() override;
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;
+17 -17
View File
@@ -1,11 +1,11 @@
#include "copium/sampler/Image.h" #include "copium/sampler/Image.h"
#include "copium/buffer/CommandBufferScoped.h" #include "copium/buffer/CommandBufferScoped.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
namespace Copium 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{}; VkImageCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
@@ -23,22 +23,22 @@ namespace Copium
createInfo.samples = VK_SAMPLE_COUNT_1_BIT; createInfo.samples = VK_SAMPLE_COUNT_1_BIT;
createInfo.flags = 0; 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; VkMemoryRequirements memoryRequirements;
vkGetImageMemoryRequirements(vulkan.GetDevice(), *image, &memoryRequirements); vkGetImageMemoryRequirements(Vulkan::GetDevice(), *image, &memoryRequirements);
VkMemoryAllocateInfo allocateInfo{}; VkMemoryAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocateInfo.allocationSize = memoryRequirements.size; 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; VkImageView imageView;
VkImageViewCreateInfo createInfo{}; VkImageViewCreateInfo createInfo{};
@@ -55,13 +55,13 @@ namespace Copium
createInfo.subresourceRange.levelCount = 1; createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0; createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1; 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; 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{}; VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_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); 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{}; VkBufferImageCopy region{};
region.bufferOffset = 0; 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) 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; return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
} }
VkFormat Image::SelectSupportedFormat(Vulkan& vulkan, const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features) VkFormat Image::SelectSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features)
{ {
for (VkFormat format : candidates) for (VkFormat format : candidates)
{ {
VkFormatProperties properties; VkFormatProperties properties;
vkGetPhysicalDeviceFormatProperties(vulkan.GetDevice().GetPhysicalDevice(), format, &properties); vkGetPhysicalDeviceFormatProperties(Vulkan::GetDevice().GetPhysicalDevice(), format, &properties);
if (tiling == VK_IMAGE_TILING_LINEAR && (properties.linearTilingFeatures & features) == features) if (tiling == VK_IMAGE_TILING_LINEAR && (properties.linearTilingFeatures & features) == features)
{ {
return format; return format;
+6 -7
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/Buffer.h" #include "copium/buffer/Buffer.h"
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -12,14 +11,14 @@ namespace Copium
{ {
CP_STATIC_CLASS(Image); CP_STATIC_CLASS(Image);
public: 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 void InitializeImage(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 VkImageView InitializeImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags);
static void TransitionImageLayout(Vulkan& vulkan, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout); static void TransitionImageLayout(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 void CopyBufferToImage(const Buffer& buffer, VkImage image, uint32_t width, uint32_t height);
static VkFormat SelectDepthFormat(Vulkan& vulkan); static VkFormat SelectDepthFormat();
private: private:
static bool HasStencilComponent(VkFormat format); static bool HasStencilComponent(VkFormat format);
static VkFormat SelectSupportedFormat(Vulkan& vulkan, const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features); static VkFormat SelectSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
}; };
} }
+5 -6
View File
@@ -1,24 +1,23 @@
#include "copium/sampler/Sampler.h" #include "copium/sampler/Sampler.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
namespace Copium namespace Copium
{ {
Sampler::Sampler(Vulkan& vulkan) Sampler::Sampler()
: vulkan{vulkan}
{ {
InitializeSampler(); InitializeSampler();
} }
Sampler::~Sampler() Sampler::~Sampler()
{ {
vkDestroySampler(vulkan.GetDevice(), sampler, nullptr); vkDestroySampler(Vulkan::GetDevice(), sampler, nullptr);
} }
void Sampler::InitializeSampler() void Sampler::InitializeSampler()
{ {
VkPhysicalDeviceProperties properties{}; VkPhysicalDeviceProperties properties{};
vkGetPhysicalDeviceProperties(vulkan.GetDevice().GetPhysicalDevice(), &properties); vkGetPhysicalDeviceProperties(Vulkan::GetDevice().GetPhysicalDevice(), &properties);
VkSamplerCreateInfo createInfo{}; VkSamplerCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
@@ -37,7 +36,7 @@ namespace Copium
createInfo.minLod = 0.0f; createInfo.minLod = 0.0f;
createInfo.maxLod = 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 Sampler::operator VkSampler() const
+1 -3
View File
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "copium/core/Vulkan.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
@@ -11,10 +10,9 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Sampler); CP_DELETE_COPY_AND_MOVE_CTOR(Sampler);
protected: protected:
Vulkan& vulkan;
VkSampler sampler; VkSampler sampler;
public: public:
Sampler(Vulkan& vulkan); Sampler();
virtual ~Sampler(); virtual ~Sampler();
virtual VkDescriptorImageInfo GetDescriptorImageInfo(int index) const = 0; virtual VkDescriptorImageInfo GetDescriptorImageInfo(int index) const = 0;
+14 -14
View File
@@ -1,21 +1,21 @@
#include "copium/sampler/Texture2D.h" #include "copium/sampler/Texture2D.h"
#include "copium/core/Device.h" #include "copium/core/Vulkan.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h> #include <stb/stb_image.h>
namespace Copium namespace Copium
{ {
Texture2D::Texture2D(Vulkan& vulkan, const std::string& filename) Texture2D::Texture2D(const std::string& filename)
: Sampler{vulkan} : Sampler{}
{ {
CP_DEBUG("Texture2D : Loading texture file: %s", filename.c_str()); CP_DEBUG("Texture2D : Loading texture file: %s", filename.c_str());
InitializeTextureImageFromFile(filename); InitializeTextureImageFromFile(filename);
} }
Texture2D::Texture2D(Vulkan& vulkan, const std::vector<uint8_t>& rgbaData, int width, int height) Texture2D::Texture2D(const std::vector<uint8_t>& rgbaData, int width, int height)
: Sampler{vulkan} : 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()); 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); InitializeTextureImageFromData((void*)rgbaData.data(), width, height);
@@ -23,9 +23,9 @@ namespace Copium
Texture2D::~Texture2D() Texture2D::~Texture2D()
{ {
vkDestroyImage(vulkan.GetDevice(), image, nullptr); vkDestroyImage(Vulkan::GetDevice(), image, nullptr);
vkFreeMemory(vulkan.GetDevice(), imageMemory, nullptr); vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr);
vkDestroyImageView(vulkan.GetDevice(), imageView, nullptr); vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
} }
VkDescriptorImageInfo Texture2D::GetDescriptorImageInfo(int index) const VkDescriptorImageInfo Texture2D::GetDescriptorImageInfo(int index) const
@@ -55,15 +55,15 @@ namespace Copium
void Texture2D::InitializeTextureImageFromData(void* rgbaData, int width, int height) void Texture2D::InitializeTextureImageFromData(void* rgbaData, int width, int height)
{ {
VkDeviceSize bufferSize = width * height * 4; 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(); void* data = stagingBuffer.Map();
memcpy(data, rgbaData, bufferSize); memcpy(data, rgbaData, bufferSize);
stagingBuffer.Unmap(); 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::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(vulkan, image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); Image::TransitionImageLayout(image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Image::CopyBufferToImage(vulkan, stagingBuffer, image, width, height); Image::CopyBufferToImage(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); Image::TransitionImageLayout(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); imageView = Image::InitializeImageView(image, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
} }
} }
+2 -3
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "copium/buffer/CommandBufferScoped.h" #include "copium/buffer/CommandBufferScoped.h"
#include "copium/core/Vulkan.h"
#include "copium/sampler/Image.h" #include "copium/sampler/Image.h"
#include "copium/sampler/Sampler.h" #include "copium/sampler/Sampler.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -18,8 +17,8 @@ namespace Copium
VkDeviceMemory imageMemory; VkDeviceMemory imageMemory;
VkImageView imageView; VkImageView imageView;
public: public:
Texture2D(Vulkan& vulkan, const std::string& filename); Texture2D(const std::string& filename);
Texture2D(Vulkan& vulkan, const std::vector<uint8_t>& rgbaData, int width, int height); Texture2D(const std::vector<uint8_t>& rgbaData, int width, int height);
~Texture2D() override; ~Texture2D() override;
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;