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