Add namespace to all classes
This commit is contained in:
@@ -192,7 +192,6 @@
|
|||||||
<ClInclude Include="src\VertexBuffer.h" />
|
<ClInclude Include="src\VertexBuffer.h" />
|
||||||
<ClInclude Include="src\VertexDescriptor.h" />
|
<ClInclude Include="src\VertexDescriptor.h" />
|
||||||
<ClInclude Include="src\VulkanException.h" />
|
<ClInclude Include="src\VulkanException.h" />
|
||||||
<ClInclude Include="src\Window.h" />
|
|
||||||
<ClInclude Include="src\VertexPassthrough.h" />
|
<ClInclude Include="src\VertexPassthrough.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -50,9 +50,6 @@
|
|||||||
<ClInclude Include="src\Instance.h">
|
<ClInclude Include="src\Instance.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="src\Window.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="src\Timer.h">
|
<ClInclude Include="src\Timer.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
|||||||
@@ -0,0 +1,239 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Buffer.h"
|
||||||
|
#include "DescriptorPool.h"
|
||||||
|
#include "DescriptorSet.h"
|
||||||
|
#include "Framebuffer.h"
|
||||||
|
#include "IndexBuffer.h"
|
||||||
|
#include "Instance.h"
|
||||||
|
#include "Pipeline.h"
|
||||||
|
#include "Texture2D.h"
|
||||||
|
#include "Timer.h"
|
||||||
|
#include "UniformBuffer.h"
|
||||||
|
#include "Vertex.h"
|
||||||
|
#include "VertexBuffer.h"
|
||||||
|
#include "VertexPassthrough.h"
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <optional>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
|
{
|
||||||
|
const std::vector<Vertex> vertices = {
|
||||||
|
Vertex{{-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||||
|
Vertex{{ 0.5f, 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
||||||
|
Vertex{{ 0.5f, 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
||||||
|
Vertex{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
|
||||||
|
Vertex{{-0.5f, 0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||||
|
Vertex{{ 0.5f, 0.0f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
||||||
|
Vertex{{ 0.5f, 0.0f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
||||||
|
Vertex{{-0.5f, 0.0f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::vector<uint16_t> indices = {
|
||||||
|
0, 1, 2, 2, 3, 0,
|
||||||
|
4, 5, 6, 6, 7, 4
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::vector<VertexPassthrough> verticesPassthrough = {
|
||||||
|
VertexPassthrough{{-1.0f, -1.0f}},
|
||||||
|
VertexPassthrough{{ 1.0f, -1.0f}},
|
||||||
|
VertexPassthrough{{ 1.0f, 1.0f}},
|
||||||
|
VertexPassthrough{{-1.0f, 1.0f}},
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::vector<uint16_t> indicesPassthrough = {
|
||||||
|
0, 1, 2, 2, 3, 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct alignas(64) ShaderUniform
|
||||||
|
{
|
||||||
|
alignas(16) glm::mat4 projection;
|
||||||
|
alignas(16) glm::mat4 view;
|
||||||
|
alignas(16) glm::mat4 model;
|
||||||
|
alignas(16) glm::vec3 lightPos;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Application final
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Instance> instance;
|
||||||
|
std::unique_ptr<Pipeline> graphicsPipeline;
|
||||||
|
std::unique_ptr<Texture2D> texture2D;
|
||||||
|
std::unique_ptr<UniformBuffer> shaderUniformBuffer;
|
||||||
|
std::unique_ptr<DescriptorPool> descriptorPool;
|
||||||
|
std::unique_ptr<DescriptorSet> descriptorSet;
|
||||||
|
std::unique_ptr<VertexBuffer> vertexBuffer;
|
||||||
|
std::unique_ptr<IndexBuffer> indexBuffer;
|
||||||
|
std::unique_ptr<CommandBuffer> commandBuffer;
|
||||||
|
|
||||||
|
std::unique_ptr<Framebuffer> framebuffer;
|
||||||
|
std::unique_ptr<Pipeline> graphicsPipelinePassthrough;
|
||||||
|
std::unique_ptr<VertexBuffer> vertexBufferPassthrough;
|
||||||
|
std::unique_ptr<IndexBuffer> indexBufferPassthrough;
|
||||||
|
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Application()
|
||||||
|
{
|
||||||
|
InitializeInstance();
|
||||||
|
InitializeFrameBuffer();
|
||||||
|
InitializeGraphicsPipeline();
|
||||||
|
InitializeTextureSampler();
|
||||||
|
InitializeUniformBuffer();
|
||||||
|
InitializeDescriptorSets();
|
||||||
|
InitializeVertexBuffer();
|
||||||
|
InitializeIndexBuffer();
|
||||||
|
InitializeCommandBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
~Application()
|
||||||
|
{
|
||||||
|
vkDeviceWaitIdle(instance->GetDevice());
|
||||||
|
}
|
||||||
|
|
||||||
|
Application(Application&&) = delete;
|
||||||
|
Application(const Application&) = delete;
|
||||||
|
Application& operator=(Application&&) = delete;
|
||||||
|
Application& operator=(const Application&) = delete;
|
||||||
|
|
||||||
|
bool Update()
|
||||||
|
{
|
||||||
|
if (!instance->BeginPresent())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
RecordCommandBuffer();
|
||||||
|
commandBuffer->SubmitAsGraphicsQueue();
|
||||||
|
|
||||||
|
return instance->EndPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void InitializeInstance()
|
||||||
|
{
|
||||||
|
instance = std::make_unique<Instance>("Copium Engine");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeFrameBuffer()
|
||||||
|
{
|
||||||
|
framebuffer = std::make_unique<Framebuffer>(*instance, instance->GetSwapChain().GetExtent().width, instance->GetSwapChain().GetExtent().height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeTextureSampler()
|
||||||
|
{
|
||||||
|
texture2D = std::make_unique<Texture2D>(*instance, "res/textures/texture.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeUniformBuffer()
|
||||||
|
{
|
||||||
|
shaderUniformBuffer = std::make_unique<UniformBuffer>(*instance, sizeof(ShaderUniform));
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeDescriptorSets()
|
||||||
|
{
|
||||||
|
descriptorPool = std::make_unique<DescriptorPool>(*instance);
|
||||||
|
|
||||||
|
descriptorSet = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0));
|
||||||
|
descriptorSet->AddUniform(*shaderUniformBuffer, 0);
|
||||||
|
descriptorSet->AddTexture2D(*texture2D, 1);
|
||||||
|
|
||||||
|
descriptorSetPassthrough = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipelinePassthrough->GetDescriptorSetLayout(0));
|
||||||
|
descriptorSetPassthrough->AddTexture2D(framebuffer->GetTexture2D(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeGraphicsPipeline()
|
||||||
|
{
|
||||||
|
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
|
||||||
|
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
|
||||||
|
creator.AddDescriptorSetLayoutBinding(0, 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
|
creator.SetVertexDescriptor(Vertex::GetDescriptor());
|
||||||
|
creator.SetCullMode(VK_CULL_MODE_NONE);
|
||||||
|
graphicsPipeline = std::make_unique<Pipeline>(*instance, creator);
|
||||||
|
|
||||||
|
PipelineCreator creatorPassthrough{instance->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
|
||||||
|
creatorPassthrough.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
|
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
|
||||||
|
creatorPassthrough.SetCullMode(VK_CULL_MODE_NONE);
|
||||||
|
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*instance, creatorPassthrough);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeVertexBuffer()
|
||||||
|
{
|
||||||
|
vertexBuffer = std::make_unique<VertexBuffer>(*instance, Vertex::GetDescriptor(), vertices.size());
|
||||||
|
vertexBuffer->Update(0, (void*)vertices.data());
|
||||||
|
|
||||||
|
vertexBufferPassthrough = std::make_unique<VertexBuffer>(*instance, VertexPassthrough::GetDescriptor(), verticesPassthrough.size());
|
||||||
|
vertexBufferPassthrough->Update(0, (void*)verticesPassthrough.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeIndexBuffer()
|
||||||
|
{
|
||||||
|
indexBuffer = std::make_unique<IndexBuffer>(*instance, indices.size());
|
||||||
|
indexBuffer->UpdateStaging((void*)indices.data());
|
||||||
|
|
||||||
|
indexBufferPassthrough = std::make_unique<IndexBuffer>(*instance, indicesPassthrough.size());
|
||||||
|
indexBufferPassthrough->UpdateStaging((void*)indicesPassthrough.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeCommandBuffer()
|
||||||
|
{
|
||||||
|
commandBuffer = std::make_unique<CommandBuffer>(*instance, CommandBuffer::Type::Dynamic);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RecordCommandBuffer()
|
||||||
|
{
|
||||||
|
commandBuffer->Begin();
|
||||||
|
std::vector<VkClearValue> clearValues{2};
|
||||||
|
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
|
||||||
|
clearValues[1].depthStencil = {1.0f, 0};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
framebuffer->Bind(*commandBuffer);
|
||||||
|
graphicsPipeline->Bind(*commandBuffer);
|
||||||
|
|
||||||
|
UpdateUniformBuffer();
|
||||||
|
|
||||||
|
vertexBuffer->Bind(*commandBuffer);
|
||||||
|
indexBuffer->Bind(*commandBuffer);
|
||||||
|
|
||||||
|
graphicsPipeline->SetDescriptorSet(0, *descriptorSet);
|
||||||
|
graphicsPipeline->BindDescriptorSets(commandBuffer->GetHandle());
|
||||||
|
|
||||||
|
indexBuffer->Draw(*commandBuffer);
|
||||||
|
framebuffer->Unbind(*commandBuffer);
|
||||||
|
|
||||||
|
instance->GetSwapChain().BeginFrameBuffer(*commandBuffer);
|
||||||
|
|
||||||
|
graphicsPipelinePassthrough->Bind(*commandBuffer);
|
||||||
|
graphicsPipelinePassthrough->SetDescriptorSet(0, *descriptorSetPassthrough);
|
||||||
|
graphicsPipelinePassthrough->BindDescriptorSets(commandBuffer->GetHandle());
|
||||||
|
vertexBufferPassthrough->Bind(*commandBuffer);
|
||||||
|
indexBufferPassthrough->Bind(*commandBuffer);
|
||||||
|
indexBufferPassthrough->Draw(*commandBuffer);
|
||||||
|
|
||||||
|
instance->GetSwapChain().EndFrameBuffer(*commandBuffer);
|
||||||
|
commandBuffer->End();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateUniformBuffer()
|
||||||
|
{
|
||||||
|
static Timer startTimer;
|
||||||
|
|
||||||
|
float time = startTimer.Elapsed();
|
||||||
|
ShaderUniform shaderUniform;
|
||||||
|
shaderUniform.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
shaderUniform.projection = glm::perspective(glm::radians(45.0f), instance->GetSwapChain().GetExtent().width / (float)instance->GetSwapChain().GetExtent().height, 0.1f, 10.0f);
|
||||||
|
shaderUniform.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
shaderUniform.projection[1][1] *= -1;
|
||||||
|
shaderUniform.lightPos = glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1};
|
||||||
|
|
||||||
|
shaderUniformBuffer->Update(shaderUniform);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+142
-139
@@ -5,166 +5,169 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
class Buffer
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Buffer);
|
class Buffer
|
||||||
protected:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
VkDeviceMemory memory;
|
|
||||||
VkBuffer handle;
|
|
||||||
VkDeviceSize size;
|
|
||||||
int count;
|
|
||||||
|
|
||||||
void* mappedData = nullptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Buffer(Instance& instance, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count)
|
|
||||||
: instance{instance}, size{size}, count{count}
|
|
||||||
{
|
|
||||||
VkBufferCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
||||||
createInfo.size = size * (VkDeviceSize)count;
|
|
||||||
createInfo.usage = usage;
|
|
||||||
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateBuffer(instance.GetDevice(), &createInfo, nullptr, &handle), "Failed to initialize buffer");
|
|
||||||
|
|
||||||
VkMemoryRequirements memoryRequirements;
|
|
||||||
vkGetBufferMemoryRequirements(instance.GetDevice(), handle, &memoryRequirements);
|
|
||||||
|
|
||||||
VkMemoryAllocateInfo allocateInfo{};
|
|
||||||
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
||||||
allocateInfo.allocationSize = memoryRequirements.size;
|
|
||||||
allocateInfo.memoryTypeIndex = instance.FindMemoryType(memoryRequirements.memoryTypeBits, properties);
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkAllocateMemory(instance.GetDevice(), &allocateInfo, nullptr, &memory), "Failed to allocate buffer memory");
|
|
||||||
|
|
||||||
vkBindBufferMemory(instance.GetDevice(), handle, memory, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Buffer()
|
|
||||||
{
|
{
|
||||||
vkFreeMemory(instance.GetDevice(), memory, nullptr);
|
CP_DELETE_COPY_AND_MOVE_CTOR(Buffer);
|
||||||
vkDestroyBuffer(instance.GetDevice(), handle, nullptr);
|
protected:
|
||||||
}
|
Instance& instance;
|
||||||
|
|
||||||
void Update(void* indexData, int index)
|
VkDeviceMemory memory;
|
||||||
{
|
VkBuffer handle;
|
||||||
CP_ASSERT(index >= 0 && index < count, "index is outside of the buffer");
|
VkDeviceSize size;
|
||||||
|
int count;
|
||||||
|
|
||||||
if (mappedData == nullptr)
|
void* mappedData = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Buffer(Instance& instance, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count)
|
||||||
|
: instance{instance}, size{size}, count{count}
|
||||||
{
|
{
|
||||||
void* data;
|
VkBufferCreateInfo createInfo{};
|
||||||
vkMapMemory(instance.GetDevice(), memory, index * size, size, 0, &data);
|
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
memcpy(data, indexData, size);
|
createInfo.size = size * (VkDeviceSize)count;
|
||||||
|
createInfo.usage = usage;
|
||||||
|
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateBuffer(instance.GetDevice(), &createInfo, nullptr, &handle), "Failed to initialize buffer");
|
||||||
|
|
||||||
|
VkMemoryRequirements memoryRequirements;
|
||||||
|
vkGetBufferMemoryRequirements(instance.GetDevice(), handle, &memoryRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocateInfo{};
|
||||||
|
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocateInfo.allocationSize = memoryRequirements.size;
|
||||||
|
allocateInfo.memoryTypeIndex = instance.FindMemoryType(memoryRequirements.memoryTypeBits, properties);
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkAllocateMemory(instance.GetDevice(), &allocateInfo, nullptr, &memory), "Failed to allocate buffer memory");
|
||||||
|
|
||||||
|
vkBindBufferMemory(instance.GetDevice(), handle, memory, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Buffer()
|
||||||
|
{
|
||||||
|
vkFreeMemory(instance.GetDevice(), memory, nullptr);
|
||||||
|
vkDestroyBuffer(instance.GetDevice(), handle, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update(void* indexData, int index)
|
||||||
|
{
|
||||||
|
CP_ASSERT(index >= 0 && index < count, "index is outside of the buffer");
|
||||||
|
|
||||||
|
if (mappedData == nullptr)
|
||||||
|
{
|
||||||
|
void* data;
|
||||||
|
vkMapMemory(instance.GetDevice(), memory, index * size, size, 0, &data);
|
||||||
|
memcpy(data, indexData, size);
|
||||||
|
vkUnmapMemory(instance.GetDevice(), memory);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memcpy((char*)mappedData + index * size, indexData, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateStaging(void* data)
|
||||||
|
{
|
||||||
|
VkDeviceSize bufferSize = size * count;
|
||||||
|
Buffer stagingBuffer{instance, 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(instance, stagingBuffer, *this, 0, bufferSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size)
|
||||||
|
{
|
||||||
|
Buffer stagingBuffer{instance, 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(instance, stagingBuffer, *this, offset, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* Map()
|
||||||
|
{
|
||||||
|
CP_ASSERT(mappedData == nullptr, "Mapping an already mapped buffer");
|
||||||
|
vkMapMemory(instance.GetDevice(), memory, 0, size * count, 0, &mappedData);
|
||||||
|
return mappedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unmap()
|
||||||
|
{
|
||||||
|
CP_ASSERT(mappedData != nullptr, "Unmapping an already unmapped buffer");
|
||||||
|
|
||||||
vkUnmapMemory(instance.GetDevice(), memory);
|
vkUnmapMemory(instance.GetDevice(), memory);
|
||||||
|
mappedData = nullptr;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
virtual void Bind(const CommandBuffer& commandBuffer) { CP_UNIMPLEMENTED(); };
|
||||||
|
|
||||||
|
void BindAsVertexBuffer(VkCommandBuffer commandBuffer)
|
||||||
{
|
{
|
||||||
memcpy((char*)mappedData + index * size, indexData, size);
|
VkDeviceSize offset = 0;
|
||||||
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &handle, &offset);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void UpdateStaging(void* data)
|
void BindAsIndexBuffer(VkCommandBuffer commandBuffer)
|
||||||
{
|
{
|
||||||
VkDeviceSize bufferSize = size * count;
|
// TODO: Maybe don't assume that indices are uint16?
|
||||||
Buffer stagingBuffer{instance, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1};
|
vkCmdBindIndexBuffer(commandBuffer, handle, 0, VK_INDEX_TYPE_UINT16);
|
||||||
|
}
|
||||||
|
|
||||||
stagingBuffer.Update(data, 0);
|
VkBuffer GetHandle() const
|
||||||
|
{
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
CopyBuffer(instance, stagingBuffer, *this, 0, bufferSize);
|
VkDeviceSize GetSize() const
|
||||||
}
|
{
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
void UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size)
|
VkDeviceSize GetPosition(int index) const
|
||||||
{
|
{
|
||||||
Buffer stagingBuffer{instance, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, 1};
|
CP_ASSERT(index >= 0 && index < count, "index is outside of the buffer");
|
||||||
|
return size * (VkDeviceSize)index;
|
||||||
|
}
|
||||||
|
|
||||||
stagingBuffer.Update(data, 0);
|
static void CopyBuffer(Instance& instance, const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size)
|
||||||
|
{
|
||||||
|
VkCommandBufferAllocateInfo allocateInfo{};
|
||||||
|
|
||||||
CopyBuffer(instance, stagingBuffer, *this, offset, size);
|
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
}
|
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||||
|
allocateInfo.commandPool = instance.GetCommandPool();
|
||||||
|
allocateInfo.commandBufferCount = 1;
|
||||||
|
|
||||||
void* Map()
|
VkCommandBuffer commandBuffer;
|
||||||
{
|
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, &commandBuffer), "Failed to initialize command buffer");
|
||||||
CP_ASSERT(mappedData == nullptr, "Mapping an already mapped buffer");
|
|
||||||
vkMapMemory(instance.GetDevice(), memory, 0, size * count, 0, &mappedData);
|
|
||||||
return mappedData;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Unmap()
|
VkCommandBufferBeginInfo beginInfo{};
|
||||||
{
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||||
CP_ASSERT(mappedData != nullptr, "Unmapping an already unmapped buffer");
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||||
|
|
||||||
vkUnmapMemory(instance.GetDevice(), memory);
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
||||||
mappedData = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Bind(const CommandBuffer& commandBuffer) { CP_UNIMPLEMENTED(); };
|
VkBufferCopy bufferCopy{};
|
||||||
|
bufferCopy.dstOffset = offset;
|
||||||
|
bufferCopy.srcOffset = 0;
|
||||||
|
bufferCopy.size = size;
|
||||||
|
|
||||||
void BindAsVertexBuffer(VkCommandBuffer commandBuffer)
|
vkCmdCopyBuffer(commandBuffer, srcBuffer.GetHandle(), dstBuffer.GetHandle(), 1, &bufferCopy);
|
||||||
{
|
|
||||||
VkDeviceSize offset = 0;
|
|
||||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &handle, &offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BindAsIndexBuffer(VkCommandBuffer commandBuffer)
|
vkEndCommandBuffer(commandBuffer);
|
||||||
{
|
|
||||||
// TODO: Maybe don't assume that indices are uint16?
|
|
||||||
vkCmdBindIndexBuffer(commandBuffer, handle, 0, VK_INDEX_TYPE_UINT16);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkBuffer GetHandle() const
|
VkSubmitInfo submitInfo{};
|
||||||
{
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||||
return handle;
|
submitInfo.commandBufferCount = 1;
|
||||||
}
|
submitInfo.pCommandBuffers = &commandBuffer;
|
||||||
|
|
||||||
VkDeviceSize GetSize() const
|
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
|
||||||
{
|
vkQueueWaitIdle(instance.GetGraphicsQueue());
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDeviceSize GetPosition(int index) const
|
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), 1, &commandBuffer);
|
||||||
{
|
}
|
||||||
CP_ASSERT(index >= 0 && index < count, "index is outside of the buffer");
|
};
|
||||||
return size * (VkDeviceSize)index;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void CopyBuffer(Instance& instance, 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 = instance.GetCommandPool();
|
|
||||||
allocateInfo.commandBufferCount = 1;
|
|
||||||
|
|
||||||
VkCommandBuffer commandBuffer;
|
|
||||||
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, &commandBuffer), "Failed to initialize command buffer");
|
|
||||||
|
|
||||||
VkCommandBufferBeginInfo beginInfo{};
|
|
||||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
|
||||||
|
|
||||||
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
|
||||||
|
|
||||||
VkBufferCopy bufferCopy{};
|
|
||||||
bufferCopy.dstOffset = offset;
|
|
||||||
bufferCopy.srcOffset = 0;
|
|
||||||
bufferCopy.size = size;
|
|
||||||
|
|
||||||
vkCmdCopyBuffer(commandBuffer, srcBuffer.GetHandle(), dstBuffer.GetHandle(), 1, &bufferCopy);
|
|
||||||
|
|
||||||
vkEndCommandBuffer(commandBuffer);
|
|
||||||
|
|
||||||
VkSubmitInfo submitInfo{};
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &commandBuffer;
|
|
||||||
|
|
||||||
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
|
|
||||||
vkQueueWaitIdle(instance.GetGraphicsQueue());
|
|
||||||
|
|
||||||
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), 1, &commandBuffer);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
+86
-83
@@ -4,96 +4,99 @@
|
|||||||
#include "Instance.h"
|
#include "Instance.h"
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
enum class CommandBufferType
|
namespace Copium
|
||||||
{
|
{
|
||||||
SingleUse, Dynamic
|
class CommandBuffer
|
||||||
};
|
|
||||||
|
|
||||||
class CommandBuffer
|
|
||||||
{
|
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
|
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
std::vector<VkCommandBuffer> commandBuffers;
|
|
||||||
const CommandBufferType type;
|
|
||||||
VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE};
|
|
||||||
|
|
||||||
public:
|
|
||||||
CommandBuffer(Instance& instance, CommandBufferType type)
|
|
||||||
: instance{instance}, type{type}
|
|
||||||
{
|
{
|
||||||
if (type == CommandBufferType::Dynamic)
|
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
|
||||||
commandBuffers.resize(instance.GetMaxFramesInFlight());
|
public:
|
||||||
else
|
enum class Type
|
||||||
commandBuffers.resize(1);
|
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo allocateInfo{};
|
|
||||||
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
||||||
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
||||||
allocateInfo.commandPool = instance.GetCommandPool();
|
|
||||||
allocateInfo.commandBufferCount = commandBuffers.size();
|
|
||||||
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, commandBuffers.data()), "Failed to allocate CommandBuffer");
|
|
||||||
}
|
|
||||||
|
|
||||||
~CommandBuffer()
|
|
||||||
{
|
|
||||||
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), commandBuffers.size(), commandBuffers.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
operator VkCommandBuffer() const
|
|
||||||
{
|
|
||||||
return currentCommandBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Test as constexpr function to see if it avoids the switch case
|
|
||||||
void Begin()
|
|
||||||
{
|
|
||||||
VkCommandBufferBeginInfo beginInfo{};
|
|
||||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
beginInfo.flags = 0;
|
|
||||||
beginInfo.pInheritanceInfo = nullptr;
|
|
||||||
switch(type)
|
|
||||||
{
|
{
|
||||||
case CommandBufferType::SingleUse:
|
SingleUse, Dynamic
|
||||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
};
|
||||||
currentCommandBuffer = commandBuffers.front();
|
private:
|
||||||
break;
|
Instance& instance;
|
||||||
case CommandBufferType::Dynamic:
|
|
||||||
currentCommandBuffer = commandBuffers[instance.GetFlightIndex()];
|
std::vector<VkCommandBuffer> commandBuffers;
|
||||||
break;
|
const Type type;
|
||||||
default:
|
VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE};
|
||||||
CP_WARN("Unhandled enum case: %d", (int)type);
|
|
||||||
|
public:
|
||||||
|
CommandBuffer(Instance& instance, Type type)
|
||||||
|
: instance{instance}, type{type}
|
||||||
|
{
|
||||||
|
if (type == Type::Dynamic)
|
||||||
|
commandBuffers.resize(instance.GetMaxFramesInFlight());
|
||||||
|
else
|
||||||
|
commandBuffers.resize(1);
|
||||||
|
|
||||||
|
VkCommandBufferAllocateInfo allocateInfo{};
|
||||||
|
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
|
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||||
|
allocateInfo.commandPool = instance.GetCommandPool();
|
||||||
|
allocateInfo.commandBufferCount = commandBuffers.size();
|
||||||
|
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, commandBuffers.data()), "Failed to allocate CommandBuffer");
|
||||||
}
|
}
|
||||||
|
|
||||||
vkResetCommandBuffer(currentCommandBuffer, 0);
|
~CommandBuffer()
|
||||||
CP_VK_ASSERT(vkBeginCommandBuffer(currentCommandBuffer, &beginInfo), "Failed to begin command buffer");
|
{
|
||||||
}
|
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), commandBuffers.size(), commandBuffers.data());
|
||||||
|
}
|
||||||
|
|
||||||
void End()
|
operator VkCommandBuffer() const
|
||||||
{
|
{
|
||||||
vkEndCommandBuffer(currentCommandBuffer);
|
return currentCommandBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Submit()
|
// TODO: Test as constexpr function to see if it avoids the switch case
|
||||||
{
|
void Begin()
|
||||||
VkSubmitInfo submitInfo{};
|
{
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
VkCommandBufferBeginInfo beginInfo{};
|
||||||
submitInfo.commandBufferCount = 1;
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||||
submitInfo.pCommandBuffers = ¤tCommandBuffer;
|
beginInfo.flags = 0;
|
||||||
|
beginInfo.pInheritanceInfo = nullptr;
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case Type::SingleUse:
|
||||||
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||||
|
currentCommandBuffer = commandBuffers.front();
|
||||||
|
break;
|
||||||
|
case Type::Dynamic:
|
||||||
|
currentCommandBuffer = commandBuffers[instance.GetFlightIndex()];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
CP_WARN("Unhandled enum case: %d", (int)type);
|
||||||
|
}
|
||||||
|
|
||||||
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
|
vkResetCommandBuffer(currentCommandBuffer, 0);
|
||||||
// TODO: if singleUse?
|
CP_VK_ASSERT(vkBeginCommandBuffer(currentCommandBuffer, &beginInfo), "Failed to begin command buffer");
|
||||||
vkQueueWaitIdle(instance.GetGraphicsQueue());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SubmitAsGraphicsQueue()
|
void End()
|
||||||
{
|
{
|
||||||
instance.SubmitGraphicsQueue({currentCommandBuffer});
|
vkEndCommandBuffer(currentCommandBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
VkCommandBuffer GetHandle() const
|
void Submit()
|
||||||
{
|
{
|
||||||
return currentCommandBuffer;
|
VkSubmitInfo submitInfo{};
|
||||||
}
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||||
};
|
submitInfo.commandBufferCount = 1;
|
||||||
|
submitInfo.pCommandBuffers = ¤tCommandBuffer;
|
||||||
|
|
||||||
|
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
|
||||||
|
// TODO: if singleUse?
|
||||||
|
vkQueueWaitIdle(instance.GetGraphicsQueue());
|
||||||
|
}
|
||||||
|
|
||||||
|
void SubmitAsGraphicsQueue()
|
||||||
|
{
|
||||||
|
instance.SubmitGraphicsQueue({currentCommandBuffer});
|
||||||
|
}
|
||||||
|
|
||||||
|
VkCommandBuffer GetHandle() const
|
||||||
|
{
|
||||||
|
return currentCommandBuffer;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,19 +3,22 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "CommandBuffer.h"
|
#include "CommandBuffer.h"
|
||||||
|
|
||||||
class CommandBufferScoped : public CommandBuffer
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped);
|
class CommandBufferScoped : public CommandBuffer
|
||||||
public:
|
|
||||||
CommandBufferScoped(Instance& instance)
|
|
||||||
: CommandBuffer{instance, CommandBufferType::SingleUse}
|
|
||||||
{
|
{
|
||||||
CommandBuffer::Begin();
|
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped);
|
||||||
}
|
public:
|
||||||
|
CommandBufferScoped(Instance& instance)
|
||||||
|
: CommandBuffer{instance, Type::SingleUse}
|
||||||
|
{
|
||||||
|
CommandBuffer::Begin();
|
||||||
|
}
|
||||||
|
|
||||||
~CommandBufferScoped()
|
~CommandBufferScoped()
|
||||||
{
|
{
|
||||||
CommandBuffer::End();
|
CommandBuffer::End();
|
||||||
CommandBuffer::Submit();
|
CommandBuffer::Submit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|||||||
+21
-19
@@ -3,29 +3,29 @@
|
|||||||
#include "VulkanException.h"
|
#include "VulkanException.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define TERM_RED "\x1B[31m"
|
#define CP_TERM_RED "\x1B[31m"
|
||||||
#define TERM_GREEN "\x1B[32m"
|
#define CP_TERM_GREEN "\x1B[32m"
|
||||||
#define TERM_YELLOW "\x1B[33m"
|
#define CP_TERM_YELLOW "\x1B[33m"
|
||||||
#define TERM_GRAY "\x1B[90m"
|
#define CP_TERM_GRAY "\x1B[90m"
|
||||||
#define TERM_CLEAR "\033[0m"
|
#define CP_TERM_CLEAR "\033[0m"
|
||||||
|
|
||||||
#define CP_DEBUG(format, ...) std::cout << TERM_GRAY << "[DBG] " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl
|
#define CP_DEBUG(format, ...) std::cout << CP_TERM_GRAY << "[DBG] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||||
#define CP_INFO(format, ...) std::cout << "[INF] " << StringFormat(format, __VA_ARGS__) << std::endl
|
#define CP_INFO(format, ...) std::cout << "[INF] " << Copium::StringFormat(format, __VA_ARGS__) << std::endl
|
||||||
#define CP_WARN(format, ...) std::cout << TERM_YELLOW << "[WRN] " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl
|
#define CP_WARN(format, ...) std::cout << CP_TERM_YELLOW << "[WRN] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||||
#define CP_ERR(format, ...) std::cout << TERM_RED << "[ERR] " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl
|
#define CP_ERR(format, ...) std::cout << CP_TERM_RED << "[ERR] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||||
|
|
||||||
// Continue traces, will not print the [XXX] tag before the log
|
// Continue traces, will not print the [XXX] tag before the log
|
||||||
#define CP_DEBUG_CONT(format, ...) std::cout << TERM_GRAY << " " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl
|
#define CP_DEBUG_CONT(format, ...) std::cout << CP_TERM_GRAY << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||||
#define CP_INFO_CONT(format, ...) std::cout << " " << StringFormat(format, __VA_ARGS__) << std::endl
|
#define CP_INFO_CONT(format, ...) std::cout << " " << Copium::StringFormat(format, __VA_ARGS__) << std::endl
|
||||||
#define CP_WARN_CONT(format, ...) std::cout << TERM_YELLOW << " " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl
|
#define CP_WARN_CONT(format, ...) std::cout << CP_TERM_YELLOW << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||||
#define CP_ERR_CONT(format, ...) std::cout << TERM_RED << " " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl
|
#define CP_ERR_CONT(format, ...) std::cout << CP_TERM_RED << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||||
|
|
||||||
#define CP_UNIMPLEMENTED() CP_WARN("%s is unimplemented", __FUNCTION__)
|
#define CP_UNIMPLEMENTED() CP_WARN("%s is unimplemented", __FUNCTION__)
|
||||||
#define CP_ABORT(format, ...) \
|
#define CP_ABORT(format, ...) \
|
||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
CP_ERR(format, __VA_ARGS__); \
|
CP_ERR(format, __VA_ARGS__); \
|
||||||
throw std::runtime_error(StringFormat(format, __VA_ARGS__)); \
|
throw std::runtime_error(Copium::StringFormat(format, __VA_ARGS__)); \
|
||||||
} while(false)
|
} while(false)
|
||||||
#define CP_ASSERT(Function, format, ...) \
|
#define CP_ASSERT(Function, format, ...) \
|
||||||
do \
|
do \
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
if(!(Function)) \
|
if(!(Function)) \
|
||||||
{ \
|
{ \
|
||||||
CP_ERR(format, __VA_ARGS__); \
|
CP_ERR(format, __VA_ARGS__); \
|
||||||
throw std::runtime_error(StringFormat(format, __VA_ARGS__)); \
|
throw std::runtime_error(Copium::StringFormat(format, __VA_ARGS__)); \
|
||||||
} \
|
} \
|
||||||
} while(false)
|
} while(false)
|
||||||
#define CP_VK_ASSERT(Function, format, ...) \
|
#define CP_VK_ASSERT(Function, format, ...) \
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
if(Function != VK_SUCCESS) \
|
if(Function != VK_SUCCESS) \
|
||||||
{ \
|
{ \
|
||||||
CP_ERR(format, __VA_ARGS__); \
|
CP_ERR(format, __VA_ARGS__); \
|
||||||
throw VulkanException(StringFormat(format, __VA_ARGS__)); \
|
throw VulkanException(Copium::StringFormat(format, __VA_ARGS__)); \
|
||||||
} \
|
} \
|
||||||
} while(false)
|
} while(false)
|
||||||
#define CP_DELETE_COPY_AND_MOVE_CTOR(ClassName) \
|
#define CP_DELETE_COPY_AND_MOVE_CTOR(ClassName) \
|
||||||
@@ -51,13 +51,15 @@
|
|||||||
ClassName& operator=(ClassName&&) = delete; \
|
ClassName& operator=(ClassName&&) = delete; \
|
||||||
ClassName& operator=(const ClassName&) = delete
|
ClassName& operator=(const ClassName&) = delete
|
||||||
|
|
||||||
template<typename ... Args>
|
namespace Copium
|
||||||
std::string StringFormat(const std::string& format, Args... args)
|
|
||||||
{
|
{
|
||||||
|
template<typename ... Args>
|
||||||
|
std::string StringFormat(const std::string& format, Args... args)
|
||||||
|
{
|
||||||
int size = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
int size = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
||||||
CP_ASSERT(size > 0, "Error during formatting");
|
CP_ASSERT(size > 0, "Error during formatting");
|
||||||
std::unique_ptr<char[]> buf(new char[size]);
|
std::unique_ptr<char[]> buf(new char[size]);
|
||||||
std::snprintf(buf.get(), size, format.c_str(), args...);
|
std::snprintf(buf.get(), size, format.c_str(), args...);
|
||||||
return std::string(buf.get(), buf.get() + size - 1);
|
return std::string(buf.get(), buf.get() + size - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+78
-75
@@ -1,88 +1,91 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
|
namespace Copium
|
||||||
class DebugMessenger
|
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger);
|
|
||||||
public:
|
|
||||||
VkInstance instance;
|
|
||||||
VkDebugUtilsMessengerEXT debugMessenger;
|
|
||||||
|
|
||||||
DebugMessenger(VkInstance instance)
|
class DebugMessenger
|
||||||
: instance{instance}
|
|
||||||
{
|
{
|
||||||
#ifndef NDEBUG
|
CP_DELETE_COPY_AND_MOVE_CTOR(DebugMessenger);
|
||||||
VkDebugUtilsMessengerCreateInfoEXT createInfo{};
|
public:
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
VkInstance instance;
|
||||||
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
VkDebugUtilsMessengerEXT debugMessenger;
|
||||||
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
|
||||||
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
|
||||||
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
|
||||||
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
|
||||||
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
|
||||||
createInfo.pfnUserCallback = DebugCallback;
|
|
||||||
createInfo.pUserData = nullptr;
|
|
||||||
CP_VK_ASSERT(vkCreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger), "Failed to initialze debug messenger");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
~DebugMessenger()
|
DebugMessenger(VkInstance instance)
|
||||||
{
|
: instance{instance}
|
||||||
#ifndef NDEBUG
|
|
||||||
vkDestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void AddRequiredExtensions(std::vector<const char*>* extensions)
|
|
||||||
{
|
|
||||||
#ifndef NDEBUG
|
|
||||||
extensions->emplace_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void AddRequiredLayers(std::vector<const char*>* layers)
|
|
||||||
{
|
|
||||||
#ifndef NDEBUG
|
|
||||||
layers->emplace_back("VK_LAYER_KHRONOS_validation");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|
||||||
VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|
||||||
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|
||||||
void* pUserData)
|
|
||||||
{
|
|
||||||
if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
|
|
||||||
{
|
{
|
||||||
if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
|
#ifndef NDEBUG
|
||||||
|
VkDebugUtilsMessengerCreateInfoEXT createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||||
|
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
||||||
|
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
||||||
|
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
||||||
|
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
||||||
|
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
||||||
|
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
||||||
|
createInfo.pfnUserCallback = DebugCallback;
|
||||||
|
createInfo.pUserData = nullptr;
|
||||||
|
CP_VK_ASSERT(vkCreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger), "Failed to initialze debug messenger");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
~DebugMessenger()
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
vkDestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void AddRequiredExtensions(std::vector<const char*>* extensions)
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
extensions->emplace_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void AddRequiredLayers(std::vector<const char*>* layers)
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
layers->emplace_back("VK_LAYER_KHRONOS_validation");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
||||||
|
VkDebugUtilsMessageTypeFlagsEXT messageType,
|
||||||
|
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
||||||
|
void* pUserData)
|
||||||
|
{
|
||||||
|
if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
|
||||||
{
|
{
|
||||||
CP_ERR(pCallbackData->pMessage);
|
if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
|
||||||
throw VulkanException(pCallbackData->pMessage);
|
{
|
||||||
|
CP_ERR(pCallbackData->pMessage);
|
||||||
|
throw VulkanException(pCallbackData->pMessage);
|
||||||
|
}
|
||||||
|
CP_WARN(pCallbackData->pMessage);
|
||||||
}
|
}
|
||||||
CP_WARN(pCallbackData->pMessage);
|
return VK_FALSE;
|
||||||
}
|
}
|
||||||
return VK_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static VkResult vkCreateDebugUtilsMessengerEXT(VkInstance instance,
|
static VkResult vkCreateDebugUtilsMessengerEXT(VkInstance instance,
|
||||||
const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
||||||
const VkAllocationCallbacks* pAllocator,
|
const VkAllocationCallbacks* pAllocator,
|
||||||
VkDebugUtilsMessengerEXT* pDebugMessenger)
|
VkDebugUtilsMessengerEXT* pDebugMessenger)
|
||||||
{
|
{
|
||||||
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
||||||
if (func != nullptr)
|
if (func != nullptr)
|
||||||
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
||||||
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
||||||
}
|
|
||||||
|
|
||||||
static void vkDestroyDebugUtilsMessengerEXT(VkInstance instance,
|
|
||||||
VkDebugUtilsMessengerEXT debugMessenger,
|
|
||||||
const VkAllocationCallbacks* pAllocator) {
|
|
||||||
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
|
|
||||||
if (func != nullptr) {
|
|
||||||
func(instance, debugMessenger, pAllocator);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
static void vkDestroyDebugUtilsMessengerEXT(VkInstance instance,
|
||||||
|
VkDebugUtilsMessengerEXT debugMessenger,
|
||||||
|
const VkAllocationCallbacks* pAllocator) {
|
||||||
|
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
|
||||||
|
if (func != nullptr) {
|
||||||
|
func(instance, debugMessenger, pAllocator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
+48
-45
@@ -4,58 +4,61 @@
|
|||||||
#include "Instance.h"
|
#include "Instance.h"
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
class DescriptorPool final
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool);
|
class DescriptorPool final
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
VkDescriptorPool descriptorPool;
|
|
||||||
static const int DESCRIPTOR_SET_COUNT = 100;
|
|
||||||
public:
|
|
||||||
DescriptorPool(Instance& instance)
|
|
||||||
: instance{instance}
|
|
||||||
{
|
{
|
||||||
std::vector<VkDescriptorPoolSize> poolSizes{2};
|
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool);
|
||||||
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
private:
|
||||||
poolSizes[0].descriptorCount = DESCRIPTOR_SET_COUNT * instance.GetMaxFramesInFlight(); // TODO: how should this actually be determined?
|
Instance& instance;
|
||||||
|
|
||||||
poolSizes[1].type = VK_DESCRIPTOR_TYPE_SAMPLER;
|
VkDescriptorPool descriptorPool;
|
||||||
poolSizes[1].descriptorCount = DESCRIPTOR_SET_COUNT * instance.GetMaxFramesInFlight(); // TODO: how should this actually be determined?
|
static const int DESCRIPTOR_SET_COUNT = 100;
|
||||||
|
public:
|
||||||
|
DescriptorPool(Instance& instance)
|
||||||
|
: instance{instance}
|
||||||
|
{
|
||||||
|
std::vector<VkDescriptorPoolSize> poolSizes{2};
|
||||||
|
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||||
|
poolSizes[0].descriptorCount = DESCRIPTOR_SET_COUNT * instance.GetMaxFramesInFlight(); // TODO: how should this actually be determined?
|
||||||
|
|
||||||
VkDescriptorPoolCreateInfo createInfo{};
|
poolSizes[1].type = VK_DESCRIPTOR_TYPE_SAMPLER;
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
poolSizes[1].descriptorCount = DESCRIPTOR_SET_COUNT * instance.GetMaxFramesInFlight(); // TODO: how should this actually be determined?
|
||||||
createInfo.poolSizeCount = poolSizes.size();
|
|
||||||
createInfo.pPoolSizes = poolSizes.data();
|
|
||||||
createInfo.maxSets = DESCRIPTOR_SET_COUNT * instance.GetMaxFramesInFlight();
|
|
||||||
createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateDescriptorPool(instance.GetDevice(), &createInfo, nullptr, &descriptorPool), "Failed to initialize descriptor pool");
|
VkDescriptorPoolCreateInfo createInfo{};
|
||||||
}
|
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||||
|
createInfo.poolSizeCount = poolSizes.size();
|
||||||
|
createInfo.pPoolSizes = poolSizes.data();
|
||||||
|
createInfo.maxSets = DESCRIPTOR_SET_COUNT * instance.GetMaxFramesInFlight();
|
||||||
|
createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
||||||
|
|
||||||
~DescriptorPool()
|
CP_VK_ASSERT(vkCreateDescriptorPool(instance.GetDevice(), &createInfo, nullptr, &descriptorPool), "Failed to initialize descriptor pool");
|
||||||
{
|
}
|
||||||
vkDestroyDescriptorPool(instance.GetDevice(), descriptorPool, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<VkDescriptorSet> AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout)
|
~DescriptorPool()
|
||||||
{
|
{
|
||||||
std::vector<VkDescriptorSet> descriptorSets;
|
vkDestroyDescriptorPool(instance.GetDevice(), descriptorPool, nullptr);
|
||||||
std::vector<VkDescriptorSetLayout> layouts{static_cast<size_t>(instance.GetMaxFramesInFlight()), descriptorSetLayout};
|
}
|
||||||
VkDescriptorSetAllocateInfo allocateInfo{};
|
|
||||||
allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
|
||||||
allocateInfo.descriptorPool = descriptorPool;
|
|
||||||
allocateInfo.descriptorSetCount = instance.GetMaxFramesInFlight();
|
|
||||||
allocateInfo.pSetLayouts = layouts.data();
|
|
||||||
|
|
||||||
descriptorSets.resize(instance.GetMaxFramesInFlight());
|
std::vector<VkDescriptorSet> AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout)
|
||||||
CP_VK_ASSERT(vkAllocateDescriptorSets(instance.GetDevice(), &allocateInfo, descriptorSets.data()), "Failed to allocate descriptor sets");
|
{
|
||||||
|
std::vector<VkDescriptorSet> descriptorSets;
|
||||||
|
std::vector<VkDescriptorSetLayout> layouts{static_cast<size_t>(instance.GetMaxFramesInFlight()), descriptorSetLayout};
|
||||||
|
VkDescriptorSetAllocateInfo allocateInfo{};
|
||||||
|
allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||||
|
allocateInfo.descriptorPool = descriptorPool;
|
||||||
|
allocateInfo.descriptorSetCount = instance.GetMaxFramesInFlight();
|
||||||
|
allocateInfo.pSetLayouts = layouts.data();
|
||||||
|
|
||||||
return descriptorSets;
|
descriptorSets.resize(instance.GetMaxFramesInFlight());
|
||||||
}
|
CP_VK_ASSERT(vkAllocateDescriptorSets(instance.GetDevice(), &allocateInfo, descriptorSets.data()), "Failed to allocate descriptor sets");
|
||||||
|
|
||||||
void FreeDescriptorSets(const std::vector<VkDescriptorSet>& descriptorSets)
|
return descriptorSets;
|
||||||
{
|
}
|
||||||
vkFreeDescriptorSets(instance.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
|
|
||||||
}
|
void FreeDescriptorSets(const std::vector<VkDescriptorSet>& descriptorSets)
|
||||||
};
|
{
|
||||||
|
vkFreeDescriptorSets(instance.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
+59
-56
@@ -6,67 +6,70 @@
|
|||||||
#include "UniformBuffer.h"
|
#include "UniformBuffer.h"
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
class DescriptorSet final
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet);
|
class DescriptorSet final
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
DescriptorPool& descriptorPool;
|
|
||||||
VkDescriptorSetLayout descriptorSetLayout;
|
|
||||||
|
|
||||||
std::vector<VkDescriptorSet> descriptorSets;
|
|
||||||
|
|
||||||
public:
|
|
||||||
DescriptorSet(Instance& instance, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout)
|
|
||||||
: instance{instance}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}
|
|
||||||
{
|
{
|
||||||
descriptorSets = descriptorPool.AllocateDescriptorSets(descriptorSetLayout);
|
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet);
|
||||||
}
|
private:
|
||||||
|
Instance& instance;
|
||||||
|
DescriptorPool& descriptorPool;
|
||||||
|
VkDescriptorSetLayout descriptorSetLayout;
|
||||||
|
|
||||||
~DescriptorSet()
|
std::vector<VkDescriptorSet> descriptorSets;
|
||||||
{
|
|
||||||
descriptorPool.FreeDescriptorSets(descriptorSets);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddUniform(const UniformBuffer& uniformBuffer, uint32_t binding)
|
public:
|
||||||
{
|
DescriptorSet(Instance& instance, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout)
|
||||||
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++i) {
|
: instance{instance}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}
|
||||||
VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i);
|
{
|
||||||
|
descriptorSets = descriptorPool.AllocateDescriptorSets(descriptorSetLayout);
|
||||||
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(instance.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void AddTexture2D(const Texture2D& texture2D, uint32_t binding)
|
~DescriptorSet()
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++i) {
|
descriptorPool.FreeDescriptorSets(descriptorSets);
|
||||||
VkDescriptorImageInfo imageInfo = texture2D.GetDescriptorImageInfo(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_COMBINED_IMAGE_SAMPLER;
|
|
||||||
descriptorWrite.descriptorCount = 1;
|
|
||||||
descriptorWrite.pBufferInfo = nullptr;
|
|
||||||
descriptorWrite.pImageInfo = &imageInfo;
|
|
||||||
descriptorWrite.pTexelBufferView = nullptr;
|
|
||||||
vkUpdateDescriptorSets(instance.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
VkDescriptorSet GetHandle() const
|
void AddUniform(const UniformBuffer& uniformBuffer, uint32_t binding)
|
||||||
{
|
{
|
||||||
return descriptorSets[instance.GetFlightIndex()];
|
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++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(instance.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddTexture2D(const Texture2D& texture2D, uint32_t binding)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++i) {
|
||||||
|
VkDescriptorImageInfo imageInfo = texture2D.GetDescriptorImageInfo(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_COMBINED_IMAGE_SAMPLER;
|
||||||
|
descriptorWrite.descriptorCount = 1;
|
||||||
|
descriptorWrite.pBufferInfo = nullptr;
|
||||||
|
descriptorWrite.pImageInfo = &imageInfo;
|
||||||
|
descriptorWrite.pTexelBufferView = nullptr;
|
||||||
|
vkUpdateDescriptorSets(instance.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VkDescriptorSet GetHandle() const
|
||||||
|
{
|
||||||
|
return descriptorSets[instance.GetFlightIndex()];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+51
-46
@@ -8,65 +8,70 @@
|
|||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
namespace FileSystem
|
namespace Copium
|
||||||
{
|
{
|
||||||
static std::vector<char> ReadFile(const std::string& filename)
|
class FileSystem
|
||||||
{
|
{
|
||||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
FileSystem() = delete;
|
||||||
CP_ASSERT(file.is_open(), "Failed to open file");
|
public:
|
||||||
|
static std::vector<char> ReadFile(const std::string& filename)
|
||||||
|
{
|
||||||
|
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||||
|
CP_ASSERT(file.is_open(), "Failed to open file");
|
||||||
|
|
||||||
size_t fileSize = (size_t) file.tellg();
|
size_t fileSize = (size_t)file.tellg();
|
||||||
std::vector<char> buffer(fileSize);
|
std::vector<char> buffer(fileSize);
|
||||||
|
|
||||||
file.seekg(0);
|
file.seekg(0);
|
||||||
file.read(buffer.data(), fileSize);
|
file.read(buffer.data(), fileSize);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string ReadFileStr(const std::string& filename)
|
static std::string ReadFileStr(const std::string& filename)
|
||||||
{
|
{
|
||||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||||
CP_ASSERT(file.is_open(), "Failed to open file");
|
CP_ASSERT(file.is_open(), "Failed to open file");
|
||||||
|
|
||||||
size_t fileSize = (size_t) file.tellg();
|
size_t fileSize = (size_t)file.tellg();
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
buffer.resize(fileSize);
|
buffer.resize(fileSize);
|
||||||
|
|
||||||
file.seekg(0);
|
file.seekg(0);
|
||||||
file.read(buffer.data(), fileSize);
|
file.read(buffer.data(), fileSize);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteFile(const std::string& filename, const std::string& data)
|
static void WriteFile(const std::string& filename, const std::string& data)
|
||||||
{
|
{
|
||||||
std::ofstream file(filename, std::ios::binary);
|
std::ofstream file(filename, std::ios::binary);
|
||||||
CP_ASSERT(file.is_open(), "Failed to open file");
|
CP_ASSERT(file.is_open(), "Failed to open file");
|
||||||
|
|
||||||
file.write(data.c_str(), data.size());
|
file.write(data.c_str(), data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteFile(const std::string& filename, const char* data, size_t size)
|
static void WriteFile(const std::string& filename, const char* data, size_t size)
|
||||||
{
|
{
|
||||||
std::filesystem::path path{filename};
|
std::filesystem::path path{filename};
|
||||||
std::filesystem::create_directories(path.parent_path());
|
std::filesystem::create_directories(path.parent_path());
|
||||||
std::ofstream file(filename, std::ios::binary);
|
std::ofstream file(filename, std::ios::binary);
|
||||||
CP_ASSERT(file.is_open(), "Failed to open file");
|
CP_ASSERT(file.is_open(), "Failed to open file");
|
||||||
|
|
||||||
file.write(data, size);
|
file.write(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool FileExists(const std::string& filename)
|
static bool FileExists(const std::string& filename)
|
||||||
{
|
{
|
||||||
std::ifstream file(filename);
|
std::ifstream file(filename);
|
||||||
return file.good();
|
return file.good();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t DateModified(const std::string& filename)
|
static int64_t DateModified(const std::string& filename)
|
||||||
{
|
{
|
||||||
struct stat result;
|
struct stat result;
|
||||||
CP_ASSERT(stat(filename.c_str(), &result) == 0, "Cannot stat file %s", filename.c_str());
|
CP_ASSERT(stat(filename.c_str(), &result) == 0, "Cannot stat file %s", filename.c_str());
|
||||||
return (int64_t)result.st_mtime;
|
return (int64_t)result.st_mtime;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
+162
-159
@@ -7,168 +7,171 @@
|
|||||||
|
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
// TODO: Add resizing (recreate image, depthImage, framebuffers)
|
namespace Copium
|
||||||
class Framebuffer
|
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
|
// TODO: Add resizing (recreate image, depthImage, framebuffers)
|
||||||
private:
|
class Framebuffer
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
std::unique_ptr<Texture2D> image;
|
|
||||||
std::unique_ptr<Texture2D> depthImage;
|
|
||||||
std::vector<VkFramebuffer> framebuffers;
|
|
||||||
VkRenderPass renderPass;
|
|
||||||
|
|
||||||
uint32_t width;
|
|
||||||
uint32_t height;
|
|
||||||
public:
|
|
||||||
Framebuffer(Instance& instance, uint32_t width, uint32_t height)
|
|
||||||
: instance{instance}, width{width}, height{height}
|
|
||||||
{
|
{
|
||||||
InitializeImages();
|
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
|
||||||
InitializeDepthBuffer();
|
private:
|
||||||
InitializeRenderPass();
|
Instance& instance;
|
||||||
InitializeFramebuffers();
|
|
||||||
}
|
|
||||||
|
|
||||||
~Framebuffer()
|
std::unique_ptr<Texture2D> image;
|
||||||
{
|
std::unique_ptr<Texture2D> depthImage;
|
||||||
for (auto& framebuffer : framebuffers)
|
std::vector<VkFramebuffer> framebuffers;
|
||||||
vkDestroyFramebuffer(instance.GetDevice(), framebuffer, nullptr);
|
VkRenderPass renderPass;
|
||||||
vkDestroyRenderPass(instance.GetDevice(), renderPass, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bind(const CommandBuffer& commandBuffer)
|
uint32_t width;
|
||||||
{
|
uint32_t height;
|
||||||
std::vector<VkClearValue> clearValues{2};
|
public:
|
||||||
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
|
Framebuffer(Instance& instance, uint32_t width, uint32_t height)
|
||||||
clearValues[1].depthStencil = {1.0f, 0};
|
: instance{instance}, width{width}, height{height}
|
||||||
|
|
||||||
VkRenderPassBeginInfo renderPassBeginInfo{};
|
|
||||||
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
|
||||||
renderPassBeginInfo.renderPass = renderPass;
|
|
||||||
renderPassBeginInfo.framebuffer = framebuffers[instance.GetFlightIndex()];
|
|
||||||
renderPassBeginInfo.renderArea.offset = {0, 0};
|
|
||||||
renderPassBeginInfo.renderArea.extent = {width, height};
|
|
||||||
renderPassBeginInfo.clearValueCount = clearValues.size();
|
|
||||||
renderPassBeginInfo.pClearValues = clearValues.data();
|
|
||||||
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Unbind(const CommandBuffer& commandBuffer)
|
|
||||||
{
|
|
||||||
vkCmdEndRenderPass(commandBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkRenderPass GetRenderPass() const
|
|
||||||
{
|
|
||||||
return renderPass;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkFramebuffer GetFramebuffer() const
|
|
||||||
{
|
|
||||||
return framebuffers[instance.GetFlightIndex()];
|
|
||||||
}
|
|
||||||
|
|
||||||
const Texture2D& GetTexture2D() const
|
|
||||||
{
|
|
||||||
return *image;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
void InitializeImages()
|
|
||||||
{
|
|
||||||
image = std::make_unique<Texture2D>(instance, width, height, Texture2D::Type::Dynamic, Texture2D::Format::Color);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeDepthBuffer()
|
|
||||||
{
|
|
||||||
depthImage = std::make_unique<Texture2D>(instance, width, height, Texture2D::Type::Static, Texture2D::Format::Depth);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeRenderPass()
|
|
||||||
{
|
|
||||||
VkAttachmentDescription colorAttachment{};
|
|
||||||
colorAttachment.format = VK_FORMAT_R8G8B8A8_SRGB;
|
|
||||||
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
||||||
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
||||||
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
||||||
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
||||||
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
||||||
|
|
||||||
VkAttachmentDescription depthAttachment{};
|
|
||||||
depthAttachment.format = Image::SelectDepthFormat(instance);
|
|
||||||
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
||||||
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
||||||
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
||||||
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
||||||
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkAttachmentReference colorAttachmentRef{};
|
|
||||||
colorAttachmentRef.attachment = 0;
|
|
||||||
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkAttachmentReference depthAttachmentRef{};
|
|
||||||
depthAttachmentRef.attachment = 1;
|
|
||||||
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkSubpassDescription subpass{};
|
|
||||||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
||||||
subpass.colorAttachmentCount = 1;
|
|
||||||
subpass.pColorAttachments = &colorAttachmentRef;
|
|
||||||
subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
|
||||||
|
|
||||||
std::vector<VkSubpassDependency> dependencies{2};
|
|
||||||
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
|
|
||||||
dependencies[0].dstSubpass = 0;
|
|
||||||
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
|
||||||
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
||||||
dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
||||||
dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
||||||
|
|
||||||
dependencies[1].srcSubpass = 0;
|
|
||||||
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
|
|
||||||
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
||||||
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
|
||||||
dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
||||||
dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
||||||
|
|
||||||
std::vector<VkAttachmentDescription> attachments{colorAttachment, depthAttachment};
|
|
||||||
VkRenderPassCreateInfo renderPassCreateInfo{};
|
|
||||||
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
||||||
renderPassCreateInfo.attachmentCount = attachments.size();
|
|
||||||
renderPassCreateInfo.pAttachments = attachments.data();
|
|
||||||
renderPassCreateInfo.subpassCount = 1;
|
|
||||||
renderPassCreateInfo.pSubpasses = &subpass;
|
|
||||||
renderPassCreateInfo.dependencyCount = dependencies.size();
|
|
||||||
renderPassCreateInfo.pDependencies = dependencies.data();
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateRenderPass(instance.GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "InitializeRenderPass : Failed to initialze render pass");
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeFramebuffers()
|
|
||||||
{
|
|
||||||
framebuffers.resize(instance.GetMaxFramesInFlight());
|
|
||||||
|
|
||||||
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++i)
|
|
||||||
{
|
{
|
||||||
std::vector<VkImageView> attachments{image->GetImageView(i), depthImage->GetImageView()};
|
InitializeImages();
|
||||||
|
InitializeDepthBuffer();
|
||||||
VkFramebufferCreateInfo createInfo{};
|
InitializeRenderPass();
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
InitializeFramebuffers();
|
||||||
createInfo.renderPass = renderPass;
|
|
||||||
createInfo.attachmentCount = attachments.size();
|
|
||||||
createInfo.pAttachments = attachments.data();
|
|
||||||
createInfo.width = width;
|
|
||||||
createInfo.height = height;
|
|
||||||
createInfo.layers = 1;
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateFramebuffer(instance.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize swap chain framebuffer");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
~Framebuffer()
|
||||||
|
{
|
||||||
|
for (auto& framebuffer : framebuffers)
|
||||||
|
vkDestroyFramebuffer(instance.GetDevice(), framebuffer, nullptr);
|
||||||
|
vkDestroyRenderPass(instance.GetDevice(), renderPass, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bind(const CommandBuffer& commandBuffer)
|
||||||
|
{
|
||||||
|
std::vector<VkClearValue> clearValues{2};
|
||||||
|
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
|
||||||
|
clearValues[1].depthStencil = {1.0f, 0};
|
||||||
|
|
||||||
|
VkRenderPassBeginInfo renderPassBeginInfo{};
|
||||||
|
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||||
|
renderPassBeginInfo.renderPass = renderPass;
|
||||||
|
renderPassBeginInfo.framebuffer = framebuffers[instance.GetFlightIndex()];
|
||||||
|
renderPassBeginInfo.renderArea.offset = {0, 0};
|
||||||
|
renderPassBeginInfo.renderArea.extent = {width, height};
|
||||||
|
renderPassBeginInfo.clearValueCount = clearValues.size();
|
||||||
|
renderPassBeginInfo.pClearValues = clearValues.data();
|
||||||
|
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unbind(const CommandBuffer& commandBuffer)
|
||||||
|
{
|
||||||
|
vkCmdEndRenderPass(commandBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkRenderPass GetRenderPass() const
|
||||||
|
{
|
||||||
|
return renderPass;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkFramebuffer GetFramebuffer() const
|
||||||
|
{
|
||||||
|
return framebuffers[instance.GetFlightIndex()];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Texture2D& GetTexture2D() const
|
||||||
|
{
|
||||||
|
return *image;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void InitializeImages()
|
||||||
|
{
|
||||||
|
image = std::make_unique<Texture2D>(instance, width, height, Texture2D::Type::Dynamic, Texture2D::Format::Color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeDepthBuffer()
|
||||||
|
{
|
||||||
|
depthImage = std::make_unique<Texture2D>(instance, width, height, Texture2D::Type::Static, Texture2D::Format::Depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeRenderPass()
|
||||||
|
{
|
||||||
|
VkAttachmentDescription colorAttachment{};
|
||||||
|
colorAttachment.format = VK_FORMAT_R8G8B8A8_SRGB;
|
||||||
|
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||||
|
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||||
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||||
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||||
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
|
||||||
|
VkAttachmentDescription depthAttachment{};
|
||||||
|
depthAttachment.format = Image::SelectDepthFormat(instance);
|
||||||
|
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||||
|
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||||
|
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||||
|
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||||
|
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||||
|
|
||||||
|
VkAttachmentReference colorAttachmentRef{};
|
||||||
|
colorAttachmentRef.attachment = 0;
|
||||||
|
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||||
|
|
||||||
|
VkAttachmentReference depthAttachmentRef{};
|
||||||
|
depthAttachmentRef.attachment = 1;
|
||||||
|
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||||
|
|
||||||
|
VkSubpassDescription subpass{};
|
||||||
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||||
|
subpass.colorAttachmentCount = 1;
|
||||||
|
subpass.pColorAttachments = &colorAttachmentRef;
|
||||||
|
subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
||||||
|
|
||||||
|
std::vector<VkSubpassDependency> dependencies{2};
|
||||||
|
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
|
||||||
|
dependencies[0].dstSubpass = 0;
|
||||||
|
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
||||||
|
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
|
dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||||
|
dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||||
|
|
||||||
|
dependencies[1].srcSubpass = 0;
|
||||||
|
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
|
||||||
|
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
|
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
||||||
|
dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||||
|
dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||||
|
|
||||||
|
std::vector<VkAttachmentDescription> attachments{colorAttachment, depthAttachment};
|
||||||
|
VkRenderPassCreateInfo renderPassCreateInfo{};
|
||||||
|
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||||||
|
renderPassCreateInfo.attachmentCount = attachments.size();
|
||||||
|
renderPassCreateInfo.pAttachments = attachments.data();
|
||||||
|
renderPassCreateInfo.subpassCount = 1;
|
||||||
|
renderPassCreateInfo.pSubpasses = &subpass;
|
||||||
|
renderPassCreateInfo.dependencyCount = dependencies.size();
|
||||||
|
renderPassCreateInfo.pDependencies = dependencies.data();
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateRenderPass(instance.GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "InitializeRenderPass : Failed to initialze render pass");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeFramebuffers()
|
||||||
|
{
|
||||||
|
framebuffers.resize(instance.GetMaxFramesInFlight());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++i)
|
||||||
|
{
|
||||||
|
std::vector<VkImageView> attachments{image->GetImageView(i), depthImage->GetImageView()};
|
||||||
|
|
||||||
|
VkFramebufferCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||||||
|
createInfo.renderPass = renderPass;
|
||||||
|
createInfo.attachmentCount = attachments.size();
|
||||||
|
createInfo.pAttachments = attachments.data();
|
||||||
|
createInfo.width = width;
|
||||||
|
createInfo.height = height;
|
||||||
|
createInfo.layers = 1;
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateFramebuffer(instance.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize swap chain framebuffer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+172
-167
@@ -6,177 +6,182 @@
|
|||||||
#include "CommandBufferScoped.h"
|
#include "CommandBufferScoped.h"
|
||||||
#include "Instance.h"
|
#include "Instance.h"
|
||||||
|
|
||||||
class Image
|
namespace Copium
|
||||||
{
|
{
|
||||||
Image() = delete;
|
class Image
|
||||||
public:
|
|
||||||
static void InitializeImage(Instance& instance, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage* image, VkDeviceMemory* imageMemory)
|
|
||||||
{
|
{
|
||||||
VkImageCreateInfo createInfo{};
|
Image() = delete;
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
public:
|
||||||
createInfo.imageType = VK_IMAGE_TYPE_2D;
|
static void InitializeImage(Instance& instance, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage* image, VkDeviceMemory* imageMemory)
|
||||||
createInfo.extent.width = width;
|
|
||||||
createInfo.extent.height = height;
|
|
||||||
createInfo.extent.depth = 1;
|
|
||||||
createInfo.mipLevels = 1;
|
|
||||||
createInfo.arrayLayers = 1;
|
|
||||||
createInfo.format = format;
|
|
||||||
createInfo.tiling = tiling;
|
|
||||||
createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
createInfo.usage = usage;
|
|
||||||
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
createInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
createInfo.flags = 0;
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateImage(instance.GetDevice(), &createInfo, nullptr, image), "InitializeImage : Failed to initialize image");
|
|
||||||
|
|
||||||
VkMemoryRequirements memoryRequirements;
|
|
||||||
vkGetImageMemoryRequirements(instance.GetDevice(), *image, &memoryRequirements);
|
|
||||||
|
|
||||||
VkMemoryAllocateInfo allocateInfo{};
|
|
||||||
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
||||||
allocateInfo.allocationSize = memoryRequirements.size;
|
|
||||||
allocateInfo.memoryTypeIndex = instance.FindMemoryType(memoryRequirements.memoryTypeBits, properties);
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkAllocateMemory(instance.GetDevice(), &allocateInfo, nullptr, imageMemory), "InitializeImage : Failed to initiallizse image memory");
|
|
||||||
|
|
||||||
vkBindImageMemory(instance.GetDevice(), *image, *imageMemory, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static VkImageView InitializeImageView(Instance& instance, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags)
|
|
||||||
{
|
|
||||||
VkImageView imageView;
|
|
||||||
VkImageViewCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
||||||
createInfo.image = image;
|
|
||||||
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
||||||
createInfo.format = format;
|
|
||||||
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
||||||
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
||||||
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
||||||
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
||||||
createInfo.subresourceRange.aspectMask = aspectFlags;
|
|
||||||
createInfo.subresourceRange.baseMipLevel = 0;
|
|
||||||
createInfo.subresourceRange.levelCount = 1;
|
|
||||||
createInfo.subresourceRange.baseArrayLayer = 0;
|
|
||||||
createInfo.subresourceRange.layerCount = 1;
|
|
||||||
CP_VK_ASSERT(vkCreateImageView(instance.GetDevice(), &createInfo, nullptr, &imageView), "InitializeImageView : Failed to initialize image view");
|
|
||||||
return imageView;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void TransitionImageLayout(Instance& instance, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout)
|
|
||||||
{
|
|
||||||
CommandBufferScoped commandBuffer{instance};
|
|
||||||
|
|
||||||
VkImageMemoryBarrier barrier{};
|
|
||||||
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
||||||
barrier.oldLayout = oldLayout;
|
|
||||||
barrier.newLayout = newLayout;
|
|
||||||
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
||||||
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
||||||
barrier.image = image;
|
|
||||||
barrier.subresourceRange.baseMipLevel = 0;
|
|
||||||
barrier.subresourceRange.levelCount = 1;
|
|
||||||
barrier.subresourceRange.baseArrayLayer = 0;
|
|
||||||
barrier.subresourceRange.layerCount = 1;
|
|
||||||
barrier.srcAccessMask = 0;
|
|
||||||
barrier.dstAccessMask = 0;
|
|
||||||
|
|
||||||
VkPipelineStageFlags srcStage;
|
|
||||||
VkPipelineStageFlags dstStage;
|
|
||||||
|
|
||||||
if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
|
||||||
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
||||||
|
|
||||||
if (HasStencilComponent(format)) {
|
|
||||||
barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
|
|
||||||
barrier.srcAccessMask = 0;
|
|
||||||
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
||||||
|
|
||||||
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
||||||
dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
||||||
} else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
|
|
||||||
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
||||||
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
||||||
|
|
||||||
srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
||||||
dstStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
|
||||||
}
|
|
||||||
else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
|
||||||
barrier.srcAccessMask = 0;
|
|
||||||
barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
|
||||||
|
|
||||||
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
||||||
dstStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
|
||||||
}
|
|
||||||
else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
|
|
||||||
barrier.srcAccessMask = 0;
|
|
||||||
barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
||||||
|
|
||||||
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
||||||
dstStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
CP_ABORT("TransitioinImageLayout : Unsupported layout transition");
|
VkImageCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||||
|
createInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||||
|
createInfo.extent.width = width;
|
||||||
|
createInfo.extent.height = height;
|
||||||
|
createInfo.extent.depth = 1;
|
||||||
|
createInfo.mipLevels = 1;
|
||||||
|
createInfo.arrayLayers = 1;
|
||||||
|
createInfo.format = format;
|
||||||
|
createInfo.tiling = tiling;
|
||||||
|
createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
createInfo.usage = usage;
|
||||||
|
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
createInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
createInfo.flags = 0;
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateImage(instance.GetDevice(), &createInfo, nullptr, image), "InitializeImage : Failed to initialize image");
|
||||||
|
|
||||||
|
VkMemoryRequirements memoryRequirements;
|
||||||
|
vkGetImageMemoryRequirements(instance.GetDevice(), *image, &memoryRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocateInfo{};
|
||||||
|
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocateInfo.allocationSize = memoryRequirements.size;
|
||||||
|
allocateInfo.memoryTypeIndex = instance.FindMemoryType(memoryRequirements.memoryTypeBits, properties);
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkAllocateMemory(instance.GetDevice(), &allocateInfo, nullptr, imageMemory), "InitializeImage : Failed to initiallizse image memory");
|
||||||
|
|
||||||
|
vkBindImageMemory(instance.GetDevice(), *image, *imageMemory, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
vkCmdPipelineBarrier(commandBuffer, srcStage, dstStage, 0, 0, nullptr, 0, nullptr, 1, &barrier);
|
static VkImageView InitializeImageView(Instance& instance, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags)
|
||||||
}
|
|
||||||
|
|
||||||
static void CopyBufferToImage(Instance& instance, const Buffer& buffer, VkImage image, uint32_t width, uint32_t height)
|
|
||||||
{
|
|
||||||
CommandBufferScoped commandBuffer{instance};
|
|
||||||
|
|
||||||
VkBufferImageCopy region{};
|
|
||||||
region.bufferOffset = 0;
|
|
||||||
region.bufferRowLength = 0;
|
|
||||||
region.bufferImageHeight = 0;
|
|
||||||
|
|
||||||
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
region.imageSubresource.mipLevel = 0;
|
|
||||||
region.imageSubresource.baseArrayLayer = 0;
|
|
||||||
region.imageSubresource.layerCount = 1;
|
|
||||||
|
|
||||||
region.imageOffset = {0, 0, 0};
|
|
||||||
region.imageExtent = {width, height, 1};
|
|
||||||
|
|
||||||
vkCmdCopyBufferToImage(commandBuffer, buffer.GetHandle(), image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static VkFormat SelectDepthFormat(Instance& instance)
|
|
||||||
{
|
|
||||||
return SelectSupportedFormat(instance, {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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static bool HasStencilComponent(VkFormat format)
|
|
||||||
{
|
|
||||||
return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
|
|
||||||
}
|
|
||||||
|
|
||||||
static VkFormat SelectSupportedFormat(Instance& instance, const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features)
|
|
||||||
{
|
|
||||||
for (VkFormat format : candidates)
|
|
||||||
{
|
{
|
||||||
VkFormatProperties properties;
|
VkImageView imageView;
|
||||||
vkGetPhysicalDeviceFormatProperties(instance.GetPhysicalDevice(), format, &properties);
|
VkImageViewCreateInfo createInfo{};
|
||||||
if(tiling == VK_IMAGE_TILING_LINEAR && (properties.linearTilingFeatures & features) == features)
|
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
{
|
createInfo.image = image;
|
||||||
return format;
|
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||||
}
|
createInfo.format = format;
|
||||||
else if (tiling == VK_IMAGE_TILING_OPTIMAL && (properties.optimalTilingFeatures & features) == features)
|
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||||
{
|
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||||
return format;
|
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||||
}
|
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||||
|
createInfo.subresourceRange.aspectMask = aspectFlags;
|
||||||
|
createInfo.subresourceRange.baseMipLevel = 0;
|
||||||
|
createInfo.subresourceRange.levelCount = 1;
|
||||||
|
createInfo.subresourceRange.baseArrayLayer = 0;
|
||||||
|
createInfo.subresourceRange.layerCount = 1;
|
||||||
|
CP_VK_ASSERT(vkCreateImageView(instance.GetDevice(), &createInfo, nullptr, &imageView), "InitializeImageView : Failed to initialize image view");
|
||||||
|
return imageView;
|
||||||
}
|
}
|
||||||
CP_ABORT("SelectSupportedFormat : Failed to select supported format");
|
|
||||||
}
|
static void TransitionImageLayout(Instance& instance, VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout)
|
||||||
};
|
{
|
||||||
|
CommandBufferScoped commandBuffer{instance};
|
||||||
|
|
||||||
|
VkImageMemoryBarrier barrier{};
|
||||||
|
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||||
|
barrier.oldLayout = oldLayout;
|
||||||
|
barrier.newLayout = newLayout;
|
||||||
|
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||||
|
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||||
|
barrier.image = image;
|
||||||
|
barrier.subresourceRange.baseMipLevel = 0;
|
||||||
|
barrier.subresourceRange.levelCount = 1;
|
||||||
|
barrier.subresourceRange.baseArrayLayer = 0;
|
||||||
|
barrier.subresourceRange.layerCount = 1;
|
||||||
|
barrier.srcAccessMask = 0;
|
||||||
|
barrier.dstAccessMask = 0;
|
||||||
|
|
||||||
|
VkPipelineStageFlags srcStage;
|
||||||
|
VkPipelineStageFlags dstStage;
|
||||||
|
|
||||||
|
if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
||||||
|
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||||
|
|
||||||
|
if (HasStencilComponent(format)) {
|
||||||
|
barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
|
||||||
|
barrier.srcAccessMask = 0;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
|
||||||
|
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
||||||
|
dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||||
|
}
|
||||||
|
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
|
||||||
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||||
|
|
||||||
|
srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||||
|
dstStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
||||||
|
}
|
||||||
|
else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
||||||
|
barrier.srcAccessMask = 0;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||||
|
|
||||||
|
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
||||||
|
dstStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
||||||
|
}
|
||||||
|
else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
|
||||||
|
barrier.srcAccessMask = 0;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||||
|
|
||||||
|
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
||||||
|
dstStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CP_ABORT("TransitioinImageLayout : Unsupported layout transition");
|
||||||
|
}
|
||||||
|
|
||||||
|
vkCmdPipelineBarrier(commandBuffer, srcStage, dstStage, 0, 0, nullptr, 0, nullptr, 1, &barrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CopyBufferToImage(Instance& instance, const Buffer& buffer, VkImage image, uint32_t width, uint32_t height)
|
||||||
|
{
|
||||||
|
CommandBufferScoped commandBuffer{instance};
|
||||||
|
|
||||||
|
VkBufferImageCopy region{};
|
||||||
|
region.bufferOffset = 0;
|
||||||
|
region.bufferRowLength = 0;
|
||||||
|
region.bufferImageHeight = 0;
|
||||||
|
|
||||||
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
region.imageSubresource.mipLevel = 0;
|
||||||
|
region.imageSubresource.baseArrayLayer = 0;
|
||||||
|
region.imageSubresource.layerCount = 1;
|
||||||
|
|
||||||
|
region.imageOffset = {0, 0, 0};
|
||||||
|
region.imageExtent = {width, height, 1};
|
||||||
|
|
||||||
|
vkCmdCopyBufferToImage(commandBuffer, buffer.GetHandle(), image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static VkFormat SelectDepthFormat(Instance& instance)
|
||||||
|
{
|
||||||
|
return SelectSupportedFormat(instance, {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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static bool HasStencilComponent(VkFormat format)
|
||||||
|
{
|
||||||
|
return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static VkFormat SelectSupportedFormat(Instance& instance, const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features)
|
||||||
|
{
|
||||||
|
for (VkFormat format : candidates)
|
||||||
|
{
|
||||||
|
VkFormatProperties properties;
|
||||||
|
vkGetPhysicalDeviceFormatProperties(instance.GetPhysicalDevice(), format, &properties);
|
||||||
|
if (tiling == VK_IMAGE_TILING_LINEAR && (properties.linearTilingFeatures & features) == features)
|
||||||
|
{
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
else if (tiling == VK_IMAGE_TILING_OPTIMAL && (properties.optimalTilingFeatures & features) == features)
|
||||||
|
{
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CP_ABORT("SelectSupportedFormat : Failed to select supported format");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+20
-17
@@ -2,23 +2,26 @@
|
|||||||
|
|
||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
|
|
||||||
class IndexBuffer : public Buffer
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(IndexBuffer);
|
class IndexBuffer : public Buffer
|
||||||
private:
|
|
||||||
int indexCount;
|
|
||||||
public:
|
|
||||||
IndexBuffer(Instance& instance, int indexCount)
|
|
||||||
: Buffer{instance, 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}
|
|
||||||
{}
|
|
||||||
|
|
||||||
void Bind(const CommandBuffer& commandBuffer) override
|
|
||||||
{
|
{
|
||||||
vkCmdBindIndexBuffer(commandBuffer, handle, 0, VK_INDEX_TYPE_UINT16);
|
CP_DELETE_COPY_AND_MOVE_CTOR(IndexBuffer);
|
||||||
}
|
private:
|
||||||
|
int indexCount;
|
||||||
|
public:
|
||||||
|
IndexBuffer(Instance& instance, int indexCount)
|
||||||
|
: Buffer{instance, 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}
|
||||||
|
{}
|
||||||
|
|
||||||
void Draw(const CommandBuffer& commandBuffer)
|
void Bind(const CommandBuffer& commandBuffer) override
|
||||||
{
|
{
|
||||||
vkCmdDrawIndexed(commandBuffer, indexCount, 1, 0, 0, 0);
|
vkCmdBindIndexBuffer(commandBuffer, handle, 0, VK_INDEX_TYPE_UINT16);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
void Draw(const CommandBuffer& commandBuffer)
|
||||||
|
{
|
||||||
|
vkCmdDrawIndexed(commandBuffer, indexCount, 1, 0, 0, 0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+394
-391
@@ -9,439 +9,442 @@
|
|||||||
#include "SwapChain.h"
|
#include "SwapChain.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
|
|
||||||
class Instance final
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Instance);
|
class Instance final
|
||||||
private:
|
|
||||||
static const int MAX_FRAMES_IN_FLIGHT = 2;
|
|
||||||
static const int WINDOW_WIDTH = 1920;
|
|
||||||
static const int WINDOW_HEIGHT = 1080;
|
|
||||||
|
|
||||||
VkInstance instance;
|
|
||||||
GLFWwindow* window;
|
|
||||||
VkSurfaceKHR surface;
|
|
||||||
std::unique_ptr<DebugMessenger> debugMessenger;
|
|
||||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|
||||||
VkDevice device;
|
|
||||||
uint32_t graphicsQueueIndex;
|
|
||||||
uint32_t presentQueueIndex;
|
|
||||||
VkQueue graphicsQueue;
|
|
||||||
VkQueue presentQueue;
|
|
||||||
std::unique_ptr<SwapChain> swapChain;
|
|
||||||
int flightIndex;
|
|
||||||
std::vector<VkSemaphore> imageAvailableSemaphores;
|
|
||||||
std::vector<VkSemaphore> renderFinishedSemaphores;
|
|
||||||
std::vector<VkFence> inFlightFences;
|
|
||||||
VkCommandPool commandPool;
|
|
||||||
bool framebufferResized = false;
|
|
||||||
|
|
||||||
int frameCount = 0;
|
|
||||||
Timer timer;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Instance(const std::string& applicationName)
|
|
||||||
{
|
{
|
||||||
timer.Start();
|
CP_DELETE_COPY_AND_MOVE_CTOR(Instance);
|
||||||
InitializeWindow(applicationName);
|
private:
|
||||||
InitializeInstance(applicationName);
|
static const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||||
InitializeDebugMessenger();
|
static const int WINDOW_WIDTH = 1920;
|
||||||
InitializeSurface();
|
static const int WINDOW_HEIGHT = 1080;
|
||||||
SelectPhysicalDevice();
|
|
||||||
InitializeLogicalDevice();
|
|
||||||
InitializeCommandPool();
|
|
||||||
InitializeSwapChain();
|
|
||||||
InitializeSyncObjects();
|
|
||||||
CP_INFO("Initialized Vulkan in %f seconds", timer.Elapsed());
|
|
||||||
}
|
|
||||||
|
|
||||||
~Instance()
|
VkInstance instance;
|
||||||
{
|
GLFWwindow* window;
|
||||||
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
VkSurfaceKHR surface;
|
||||||
|
std::unique_ptr<DebugMessenger> debugMessenger;
|
||||||
|
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||||
|
VkDevice device;
|
||||||
|
uint32_t graphicsQueueIndex;
|
||||||
|
uint32_t presentQueueIndex;
|
||||||
|
VkQueue graphicsQueue;
|
||||||
|
VkQueue presentQueue;
|
||||||
|
std::unique_ptr<SwapChain> swapChain;
|
||||||
|
int flightIndex;
|
||||||
|
std::vector<VkSemaphore> imageAvailableSemaphores;
|
||||||
|
std::vector<VkSemaphore> renderFinishedSemaphores;
|
||||||
|
std::vector<VkFence> inFlightFences;
|
||||||
|
VkCommandPool commandPool;
|
||||||
|
bool framebufferResized = false;
|
||||||
|
|
||||||
|
int frameCount = 0;
|
||||||
|
Timer timer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Instance(const std::string& applicationName)
|
||||||
{
|
{
|
||||||
vkDestroyFence(device, inFlightFences[i], nullptr);
|
timer.Start();
|
||||||
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
InitializeWindow(applicationName);
|
||||||
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
InitializeInstance(applicationName);
|
||||||
|
InitializeDebugMessenger();
|
||||||
|
InitializeSurface();
|
||||||
|
SelectPhysicalDevice();
|
||||||
|
InitializeLogicalDevice();
|
||||||
|
InitializeCommandPool();
|
||||||
|
InitializeSwapChain();
|
||||||
|
InitializeSyncObjects();
|
||||||
|
CP_INFO("Initialized Vulkan in %f seconds", timer.Elapsed());
|
||||||
}
|
}
|
||||||
vkDestroyCommandPool(device, commandPool, nullptr);
|
|
||||||
swapChain.reset();
|
|
||||||
vkDestroyDevice(device, nullptr);
|
|
||||||
vkDestroySurfaceKHR(instance, surface, nullptr);
|
|
||||||
debugMessenger.reset();
|
|
||||||
vkDestroyInstance(instance, nullptr);
|
|
||||||
glfwDestroyWindow(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BeginPresent()
|
~Instance()
|
||||||
{
|
|
||||||
vkWaitForFences(device, 1, &inFlightFences[flightIndex], VK_TRUE, UINT64_MAX);
|
|
||||||
|
|
||||||
if (!swapChain->BeginPresent(imageAvailableSemaphores[flightIndex]))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
vkResetFences(device, 1, &inFlightFences[flightIndex]);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool EndPresent()
|
|
||||||
{
|
|
||||||
swapChain->EndPresent(presentQueue, &renderFinishedSemaphores[flightIndex], framebufferResized);
|
|
||||||
|
|
||||||
framebufferResized = false;
|
|
||||||
flightIndex = (flightIndex + 1) % MAX_FRAMES_IN_FLIGHT;
|
|
||||||
return !glfwWindowShouldClose(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SubmitGraphicsQueue(const std::vector<VkCommandBuffer>& commandBuffers)
|
|
||||||
{
|
|
||||||
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
|
|
||||||
VkSubmitInfo submitInfo{};
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.waitSemaphoreCount = 1;
|
|
||||||
submitInfo.pWaitSemaphores = &imageAvailableSemaphores[flightIndex];
|
|
||||||
submitInfo.pWaitDstStageMask = waitStages;
|
|
||||||
submitInfo.commandBufferCount = commandBuffers.size();
|
|
||||||
submitInfo.pCommandBuffers = commandBuffers.data();
|
|
||||||
submitInfo.signalSemaphoreCount = 1;
|
|
||||||
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[flightIndex];
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[flightIndex]), "Failed to submit command buffer");
|
|
||||||
}
|
|
||||||
|
|
||||||
VkInstance GetInstance() const
|
|
||||||
{
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLFWwindow* GetWindow() const
|
|
||||||
{
|
|
||||||
return window;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkSurfaceKHR GetSurface() const
|
|
||||||
{
|
|
||||||
return surface;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkPhysicalDevice GetPhysicalDevice() const
|
|
||||||
{
|
|
||||||
return physicalDevice;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDevice GetDevice() const
|
|
||||||
{
|
|
||||||
return device;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkCommandPool GetCommandPool() const
|
|
||||||
{
|
|
||||||
return commandPool;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkQueue GetGraphicsQueue() const
|
|
||||||
{
|
|
||||||
return graphicsQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetFlightIndex() const
|
|
||||||
{
|
|
||||||
return flightIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetMaxFramesInFlight() const
|
|
||||||
{
|
|
||||||
return MAX_FRAMES_IN_FLIGHT;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SwapChain& GetSwapChain() const
|
|
||||||
{
|
|
||||||
return *swapChain;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Create Device class and move this there
|
|
||||||
uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
|
|
||||||
{
|
|
||||||
VkPhysicalDeviceMemoryProperties memoryProperties;
|
|
||||||
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
|
|
||||||
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; ++i)
|
|
||||||
{
|
{
|
||||||
if ((typeFilter & (1 << i)) && (memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
||||||
return i;
|
{
|
||||||
|
vkDestroyFence(device, inFlightFences[i], nullptr);
|
||||||
|
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
|
||||||
|
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
|
||||||
|
}
|
||||||
|
vkDestroyCommandPool(device, commandPool, nullptr);
|
||||||
|
swapChain.reset();
|
||||||
|
vkDestroyDevice(device, nullptr);
|
||||||
|
vkDestroySurfaceKHR(instance, surface, nullptr);
|
||||||
|
debugMessenger.reset();
|
||||||
|
vkDestroyInstance(instance, nullptr);
|
||||||
|
glfwDestroyWindow(window);
|
||||||
}
|
}
|
||||||
throw std::runtime_error("Failed to find suitable memory type");
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
bool BeginPresent()
|
||||||
void InitializeWindow(const std::string& applicationName)
|
{
|
||||||
{
|
vkWaitForFences(device, 1, &inFlightFences[flightIndex], VK_TRUE, UINT64_MAX);
|
||||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
||||||
|
if (!swapChain->BeginPresent(imageAvailableSemaphores[flightIndex]))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
vkResetFences(device, 1, &inFlightFences[flightIndex]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EndPresent()
|
||||||
|
{
|
||||||
|
swapChain->EndPresent(presentQueue, &renderFinishedSemaphores[flightIndex], framebufferResized);
|
||||||
|
|
||||||
|
framebufferResized = false;
|
||||||
|
flightIndex = (flightIndex + 1) % MAX_FRAMES_IN_FLIGHT;
|
||||||
|
return !glfwWindowShouldClose(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SubmitGraphicsQueue(const std::vector<VkCommandBuffer>& commandBuffers)
|
||||||
|
{
|
||||||
|
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
|
||||||
|
VkSubmitInfo submitInfo{};
|
||||||
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||||
|
submitInfo.waitSemaphoreCount = 1;
|
||||||
|
submitInfo.pWaitSemaphores = &imageAvailableSemaphores[flightIndex];
|
||||||
|
submitInfo.pWaitDstStageMask = waitStages;
|
||||||
|
submitInfo.commandBufferCount = commandBuffers.size();
|
||||||
|
submitInfo.pCommandBuffers = commandBuffers.data();
|
||||||
|
submitInfo.signalSemaphoreCount = 1;
|
||||||
|
submitInfo.pSignalSemaphores = &renderFinishedSemaphores[flightIndex];
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[flightIndex]), "Failed to submit command buffer");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkInstance GetInstance() const
|
||||||
|
{
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLFWwindow* GetWindow() const
|
||||||
|
{
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkSurfaceKHR GetSurface() const
|
||||||
|
{
|
||||||
|
return surface;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkPhysicalDevice GetPhysicalDevice() const
|
||||||
|
{
|
||||||
|
return physicalDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkDevice GetDevice() const
|
||||||
|
{
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkCommandPool GetCommandPool() const
|
||||||
|
{
|
||||||
|
return commandPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkQueue GetGraphicsQueue() const
|
||||||
|
{
|
||||||
|
return graphicsQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetFlightIndex() const
|
||||||
|
{
|
||||||
|
return flightIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetMaxFramesInFlight() const
|
||||||
|
{
|
||||||
|
return MAX_FRAMES_IN_FLIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SwapChain& GetSwapChain() const
|
||||||
|
{
|
||||||
|
return *swapChain;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Create Device class and move this there
|
||||||
|
uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
|
||||||
|
{
|
||||||
|
VkPhysicalDeviceMemoryProperties memoryProperties;
|
||||||
|
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
|
||||||
|
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; ++i)
|
||||||
|
{
|
||||||
|
if ((typeFilter & (1 << i)) && (memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
throw std::runtime_error("Failed to find suitable memory type");
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void InitializeWindow(const std::string& applicationName)
|
||||||
|
{
|
||||||
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||||
|
|
||||||
|
|
||||||
#if defined(FULLSCREEN)
|
#if defined(FULLSCREEN)
|
||||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||||
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
||||||
window = glfwCreateWindow(mode->width, mode->height, applicationName.c_str(), glfwGetPrimaryMonitor(), nullptr);
|
window = glfwCreateWindow(mode->width, mode->height, applicationName.c_str(), glfwGetPrimaryMonitor(), nullptr);
|
||||||
#elif defined(BORDERLESS_WINDOWED)
|
#elif defined(BORDERLESS_WINDOWED)
|
||||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||||
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
||||||
window = glfwCreateWindow(mode->width, mode->height, applicationName.c_str(), nullptr, nullptr);
|
window = glfwCreateWindow(mode->width, mode->height, applicationName.c_str(), nullptr, nullptr);
|
||||||
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
|
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
|
||||||
#else
|
#else
|
||||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, applicationName.c_str(), nullptr, nullptr);
|
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, applicationName.c_str(), nullptr, nullptr);
|
||||||
#endif
|
#endif
|
||||||
CP_ASSERT(window, "Failed to initialize glfw window");
|
CP_ASSERT(window, "Failed to initialize glfw window");
|
||||||
|
|
||||||
glfwSetWindowUserPointer(window, this);
|
glfwSetWindowUserPointer(window, this);
|
||||||
glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback);
|
glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback);
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeInstance(const std::string& applicationName)
|
|
||||||
{
|
|
||||||
VkApplicationInfo appInfo{};
|
|
||||||
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
|
||||||
appInfo.pApplicationName = applicationName.c_str();
|
|
||||||
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
||||||
appInfo.pEngineName = "Copium Engine";
|
|
||||||
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
||||||
appInfo.apiVersion = VK_API_VERSION_1_1;
|
|
||||||
|
|
||||||
std::vector<const char*> requiredExtensions = GetRequiredExtensions();
|
|
||||||
|
|
||||||
uint32_t extensionCount;
|
|
||||||
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
|
||||||
std::vector<VkExtensionProperties> extensions{extensionCount};
|
|
||||||
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
|
||||||
|
|
||||||
CP_INFO("Supported Extensions:");
|
|
||||||
for (auto&& extension : extensions)
|
|
||||||
{
|
|
||||||
CP_INFO_CONT("\t%s", extension.extensionName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<const char*> layers{};
|
void InitializeInstance(const std::string& applicationName)
|
||||||
DebugMessenger::AddRequiredLayers(&layers);
|
|
||||||
CP_ASSERT(CheckLayerSupport(layers), "Some required layers are not supported");
|
|
||||||
|
|
||||||
VkInstanceCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
||||||
createInfo.pApplicationInfo = &appInfo;
|
|
||||||
createInfo.enabledExtensionCount = requiredExtensions.size();
|
|
||||||
createInfo.ppEnabledExtensionNames = requiredExtensions.data();
|
|
||||||
createInfo.enabledLayerCount = layers.size();
|
|
||||||
createInfo.ppEnabledLayerNames = layers.data();
|
|
||||||
CP_VK_ASSERT(vkCreateInstance(&createInfo, nullptr, &instance), "Failed to create instance");
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeDebugMessenger()
|
|
||||||
{
|
|
||||||
debugMessenger = std::make_unique<DebugMessenger>(instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeSurface()
|
|
||||||
{
|
|
||||||
CP_VK_ASSERT(glfwCreateWindowSurface(instance, window, nullptr, &surface), "Failed to create Vulkan surface");
|
|
||||||
}
|
|
||||||
|
|
||||||
void SelectPhysicalDevice()
|
|
||||||
{
|
|
||||||
uint32_t deviceCount;
|
|
||||||
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
|
||||||
CP_ASSERT(deviceCount != 0, "No available devices support Vulkan");
|
|
||||||
|
|
||||||
std::vector<VkPhysicalDevice> devices(deviceCount);
|
|
||||||
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
|
||||||
CP_INFO("Available devices:");
|
|
||||||
for (auto&& device : devices)
|
|
||||||
{
|
{
|
||||||
VkPhysicalDeviceProperties deviceProperties;
|
VkApplicationInfo appInfo{};
|
||||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||||
CP_INFO_CONT("\t%s", deviceProperties.deviceName);
|
appInfo.pApplicationName = applicationName.c_str();
|
||||||
|
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||||
|
appInfo.pEngineName = "Copium Engine";
|
||||||
|
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||||
|
appInfo.apiVersion = VK_API_VERSION_1_1;
|
||||||
|
|
||||||
|
std::vector<const char*> requiredExtensions = GetRequiredExtensions();
|
||||||
|
|
||||||
|
uint32_t extensionCount;
|
||||||
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
||||||
|
std::vector<VkExtensionProperties> extensions{extensionCount};
|
||||||
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
||||||
|
|
||||||
|
CP_INFO("Supported Extensions:");
|
||||||
|
for (auto&& extension : extensions)
|
||||||
|
{
|
||||||
|
CP_INFO_CONT("\t%s", extension.extensionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<const char*> layers{};
|
||||||
|
DebugMessenger::AddRequiredLayers(&layers);
|
||||||
|
CP_ASSERT(CheckLayerSupport(layers), "Some required layers are not supported");
|
||||||
|
|
||||||
|
VkInstanceCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||||
|
createInfo.pApplicationInfo = &appInfo;
|
||||||
|
createInfo.enabledExtensionCount = requiredExtensions.size();
|
||||||
|
createInfo.ppEnabledExtensionNames = requiredExtensions.data();
|
||||||
|
createInfo.enabledLayerCount = layers.size();
|
||||||
|
createInfo.ppEnabledLayerNames = layers.data();
|
||||||
|
CP_VK_ASSERT(vkCreateInstance(&createInfo, nullptr, &instance), "Failed to create instance");
|
||||||
}
|
}
|
||||||
for (auto&& device : devices)
|
|
||||||
|
void InitializeDebugMessenger()
|
||||||
{
|
{
|
||||||
if (IsPhysicalDeviceSuitable(device))
|
debugMessenger = std::make_unique<DebugMessenger>(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeSurface()
|
||||||
|
{
|
||||||
|
CP_VK_ASSERT(glfwCreateWindowSurface(instance, window, nullptr, &surface), "Failed to create Vulkan surface");
|
||||||
|
}
|
||||||
|
|
||||||
|
void SelectPhysicalDevice()
|
||||||
|
{
|
||||||
|
uint32_t deviceCount;
|
||||||
|
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
||||||
|
CP_ASSERT(deviceCount != 0, "No available devices support Vulkan");
|
||||||
|
|
||||||
|
std::vector<VkPhysicalDevice> devices(deviceCount);
|
||||||
|
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
||||||
|
CP_INFO("Available devices:");
|
||||||
|
for (auto&& device : devices)
|
||||||
{
|
{
|
||||||
VkPhysicalDeviceProperties deviceProperties;
|
VkPhysicalDeviceProperties deviceProperties;
|
||||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||||
physicalDevice = device;
|
CP_INFO_CONT("\t%s", deviceProperties.deviceName);
|
||||||
CP_INFO("Selecting device: %s", deviceProperties.deviceName);
|
}
|
||||||
break;
|
for (auto&& device : devices)
|
||||||
|
{
|
||||||
|
if (IsPhysicalDeviceSuitable(device))
|
||||||
|
{
|
||||||
|
VkPhysicalDeviceProperties deviceProperties;
|
||||||
|
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||||
|
physicalDevice = device;
|
||||||
|
CP_INFO("Selecting device: %s", deviceProperties.deviceName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CP_ASSERT(physicalDevice != VK_NULL_HANDLE, "Failed to find suitable GPU");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeLogicalDevice()
|
||||||
|
{
|
||||||
|
QueueFamiliesQuery query{surface, physicalDevice};
|
||||||
|
|
||||||
|
float queuePriority = 1.0f;
|
||||||
|
|
||||||
|
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos{};
|
||||||
|
std::set<uint32_t> uniqueQueueFamilies{query.graphicsFamily.value(), query.presentFamily.value()};
|
||||||
|
for (auto&& queueFamily : uniqueQueueFamilies)
|
||||||
|
{
|
||||||
|
VkDeviceQueueCreateInfo queueCreateInfo{};
|
||||||
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||||
|
queueCreateInfo.queueFamilyIndex = queueFamily;
|
||||||
|
queueCreateInfo.queueCount = 1;
|
||||||
|
queueCreateInfo.pQueuePriorities = &queuePriority;
|
||||||
|
queueCreateInfos.emplace_back(queueCreateInfo);
|
||||||
|
}
|
||||||
|
std::vector<const char*> deviceExtensions = GetRequiredDeviceExtensions();
|
||||||
|
VkPhysicalDeviceFeatures deviceFeatures{};
|
||||||
|
deviceFeatures.fillModeNonSolid = VK_TRUE;
|
||||||
|
deviceFeatures.samplerAnisotropy = VK_TRUE;
|
||||||
|
VkDeviceCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
||||||
|
createInfo.pQueueCreateInfos = queueCreateInfos.data();
|
||||||
|
createInfo.queueCreateInfoCount = queueCreateInfos.size();
|
||||||
|
createInfo.pEnabledFeatures = &deviceFeatures;
|
||||||
|
createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
||||||
|
createInfo.enabledExtensionCount = deviceExtensions.size();
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateDevice(physicalDevice, &createInfo, nullptr, &device), "Failed to initialize logical device");
|
||||||
|
|
||||||
|
graphicsQueueIndex = query.graphicsFamily.value();
|
||||||
|
presentQueueIndex = query.presentFamily.value();
|
||||||
|
vkGetDeviceQueue(device, graphicsQueueIndex, 0, &graphicsQueue);
|
||||||
|
vkGetDeviceQueue(device, presentQueueIndex, 0, &presentQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeSwapChain()
|
||||||
|
{
|
||||||
|
swapChain = std::make_unique<SwapChain>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeCommandPool()
|
||||||
|
{
|
||||||
|
VkCommandPoolCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||||
|
createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||||
|
createInfo.queueFamilyIndex = graphicsQueueIndex;
|
||||||
|
CP_VK_ASSERT(vkCreateCommandPool(device, &createInfo, nullptr, &commandPool), "Failed to initialize command pool");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeSyncObjects()
|
||||||
|
{
|
||||||
|
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
||||||
|
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
||||||
|
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
||||||
|
VkSemaphoreCreateInfo semaphoreCreateInfo{};
|
||||||
|
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||||
|
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
||||||
|
{
|
||||||
|
CP_VK_ASSERT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]), "Failed to initialize available image semaphore");
|
||||||
|
CP_VK_ASSERT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]), "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(device, &fenceCreateInfo, nullptr, &inFlightFences[i]), "Failed to initialize in flight fence");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CP_ASSERT(physicalDevice != VK_NULL_HANDLE, "Failed to find suitable GPU");
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeLogicalDevice()
|
std::vector<const char*> GetRequiredExtensions()
|
||||||
{
|
|
||||||
QueueFamiliesQuery query{surface, physicalDevice};
|
|
||||||
|
|
||||||
float queuePriority = 1.0f;
|
|
||||||
|
|
||||||
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos{};
|
|
||||||
std::set<uint32_t> uniqueQueueFamilies{query.graphicsFamily.value(), query.presentFamily.value()};
|
|
||||||
for(auto&& queueFamily : uniqueQueueFamilies)
|
|
||||||
{
|
{
|
||||||
VkDeviceQueueCreateInfo queueCreateInfo{};
|
uint32_t glfwExtensionCount;
|
||||||
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
const char** glfwExtensions;
|
||||||
queueCreateInfo.queueFamilyIndex = queueFamily;
|
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
||||||
queueCreateInfo.queueCount = 1;
|
|
||||||
queueCreateInfo.pQueuePriorities = &queuePriority;
|
|
||||||
queueCreateInfos.emplace_back(queueCreateInfo);
|
|
||||||
}
|
|
||||||
std::vector<const char*> deviceExtensions = GetRequiredDeviceExtensions();
|
|
||||||
VkPhysicalDeviceFeatures deviceFeatures{};
|
|
||||||
deviceFeatures.fillModeNonSolid = VK_TRUE;
|
|
||||||
deviceFeatures.samplerAnisotropy = VK_TRUE;
|
|
||||||
VkDeviceCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|
||||||
createInfo.pQueueCreateInfos = queueCreateInfos.data();
|
|
||||||
createInfo.queueCreateInfoCount = queueCreateInfos.size();
|
|
||||||
createInfo.pEnabledFeatures = &deviceFeatures;
|
|
||||||
createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
|
||||||
createInfo.enabledExtensionCount = deviceExtensions.size();
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateDevice(physicalDevice, &createInfo, nullptr, &device), "Failed to initialize logical device");
|
std::vector<const char*> extensions{glfwExtensions, glfwExtensions + glfwExtensionCount};
|
||||||
|
|
||||||
graphicsQueueIndex = query.graphicsFamily.value();
|
debugMessenger->AddRequiredExtensions(&extensions);
|
||||||
presentQueueIndex = query.presentFamily.value();
|
|
||||||
vkGetDeviceQueue(device, graphicsQueueIndex, 0, &graphicsQueue);
|
|
||||||
vkGetDeviceQueue(device, presentQueueIndex , 0, &presentQueue);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeSwapChain()
|
return extensions;
|
||||||
{
|
|
||||||
swapChain = std::make_unique<SwapChain>(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeCommandPool()
|
|
||||||
{
|
|
||||||
VkCommandPoolCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
|
||||||
createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
||||||
createInfo.queueFamilyIndex = graphicsQueueIndex;
|
|
||||||
CP_VK_ASSERT(vkCreateCommandPool(device, &createInfo, nullptr, &commandPool), "Failed to initialize command pool");
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeSyncObjects()
|
|
||||||
{
|
|
||||||
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
|
||||||
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
|
||||||
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
|
||||||
VkSemaphoreCreateInfo semaphoreCreateInfo{};
|
|
||||||
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
||||||
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
|
||||||
{
|
|
||||||
CP_VK_ASSERT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &imageAvailableSemaphores[i]), "Failed to initialize available image semaphore");
|
|
||||||
CP_VK_ASSERT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &renderFinishedSemaphores[i]), "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(device, &fenceCreateInfo, nullptr, &inFlightFences[i]), "Failed to initialize in flight fence");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<const char*> GetRequiredExtensions()
|
|
||||||
{
|
|
||||||
uint32_t glfwExtensionCount;
|
|
||||||
const char** glfwExtensions;
|
|
||||||
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
||||||
|
|
||||||
std::vector<const char*> extensions{glfwExtensions, glfwExtensions + glfwExtensionCount};
|
|
||||||
|
|
||||||
debugMessenger->AddRequiredExtensions(&extensions);
|
|
||||||
|
|
||||||
return extensions;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CheckLayerSupport(const std::vector<const char*>& layers)
|
|
||||||
{
|
|
||||||
uint32_t layerCount;
|
|
||||||
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
|
||||||
|
|
||||||
std::vector<VkLayerProperties> availableLayers(layerCount);
|
|
||||||
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
|
||||||
|
|
||||||
CP_INFO("Supported Layers:");
|
|
||||||
for (auto&& availableLayer : availableLayers)
|
|
||||||
{
|
|
||||||
CP_INFO_CONT("\t%s", availableLayer.layerName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto&& layer : layers)
|
bool CheckLayerSupport(const std::vector<const char*>& layers)
|
||||||
{
|
{
|
||||||
bool layerFound = false;
|
uint32_t layerCount;
|
||||||
|
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
||||||
|
|
||||||
|
std::vector<VkLayerProperties> availableLayers(layerCount);
|
||||||
|
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
||||||
|
|
||||||
|
CP_INFO("Supported Layers:");
|
||||||
for (auto&& availableLayer : availableLayers)
|
for (auto&& availableLayer : availableLayers)
|
||||||
{
|
{
|
||||||
if (std::strcmp(layer, availableLayer.layerName) == 0)
|
CP_INFO_CONT("\t%s", availableLayer.layerName);
|
||||||
{
|
|
||||||
layerFound = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!layerFound)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsPhysicalDeviceSuitable(VkPhysicalDevice device)
|
for (auto&& layer : layers)
|
||||||
{
|
|
||||||
VkPhysicalDeviceProperties deviceProperties;
|
|
||||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
|
||||||
if (deviceProperties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
VkPhysicalDeviceFeatures deviceFeatures;
|
|
||||||
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
|
|
||||||
if (!deviceFeatures.fillModeNonSolid || !deviceFeatures.samplerAnisotropy)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
QueueFamiliesQuery query{surface, device};
|
|
||||||
if (!query.AllRequiredFamiliesSupported())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!CheckDeviceExtensionSupport(device))
|
|
||||||
return false;
|
|
||||||
SwapChainSupportDetails details{surface, device};
|
|
||||||
if (!details.Valid())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CheckDeviceExtensionSupport(VkPhysicalDevice device)
|
|
||||||
{
|
|
||||||
uint32_t extensionCount;
|
|
||||||
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
|
|
||||||
std::vector<VkExtensionProperties> extensions{extensionCount};
|
|
||||||
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, extensions.data());
|
|
||||||
|
|
||||||
for (auto&& requiredExtension : GetRequiredDeviceExtensions())
|
|
||||||
{
|
|
||||||
bool found = false;
|
|
||||||
for (auto&& extension : extensions)
|
|
||||||
{
|
{
|
||||||
if (std::strcmp(requiredExtension, extension.extensionName) == 0)
|
bool layerFound = false;
|
||||||
|
for (auto&& availableLayer : availableLayers)
|
||||||
{
|
{
|
||||||
found = true;
|
if (std::strcmp(layer, availableLayer.layerName) == 0)
|
||||||
break;
|
{
|
||||||
|
layerFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!layerFound)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
if (!found)
|
return true;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<const char*> GetRequiredDeviceExtensions()
|
bool IsPhysicalDeviceSuitable(VkPhysicalDevice device)
|
||||||
{
|
{
|
||||||
return {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
|
VkPhysicalDeviceProperties deviceProperties;
|
||||||
}
|
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||||
|
if (deviceProperties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
||||||
|
return false;
|
||||||
|
|
||||||
static void FramebufferResizeCallback(GLFWwindow* window, int width, int height)
|
VkPhysicalDeviceFeatures deviceFeatures;
|
||||||
{
|
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
|
||||||
Instance* instance = static_cast<Instance*>(glfwGetWindowUserPointer(window));
|
if (!deviceFeatures.fillModeNonSolid || !deviceFeatures.samplerAnisotropy)
|
||||||
instance->framebufferResized = true;
|
return false;
|
||||||
}
|
|
||||||
};
|
QueueFamiliesQuery query{surface, device};
|
||||||
|
if (!query.AllRequiredFamiliesSupported())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!CheckDeviceExtensionSupport(device))
|
||||||
|
return false;
|
||||||
|
SwapChainSupportDetails details{surface, device};
|
||||||
|
if (!details.Valid())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CheckDeviceExtensionSupport(VkPhysicalDevice device)
|
||||||
|
{
|
||||||
|
uint32_t extensionCount;
|
||||||
|
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
|
||||||
|
std::vector<VkExtensionProperties> extensions{extensionCount};
|
||||||
|
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, extensions.data());
|
||||||
|
|
||||||
|
for (auto&& requiredExtension : GetRequiredDeviceExtensions())
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
for (auto&& extension : extensions)
|
||||||
|
{
|
||||||
|
if (std::strcmp(requiredExtension, extension.extensionName) == 0)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<const char*> GetRequiredDeviceExtensions()
|
||||||
|
{
|
||||||
|
return {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
|
||||||
|
}
|
||||||
|
|
||||||
|
static void FramebufferResizeCallback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
Instance* instance = static_cast<Instance*>(glfwGetWindowUserPointer(window));
|
||||||
|
instance->framebufferResized = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
+212
-210
@@ -11,246 +11,248 @@
|
|||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
class Pipeline
|
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline);
|
class Pipeline
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{};
|
|
||||||
std::vector<VkDescriptorSet> boundDescriptorSets;
|
|
||||||
VkPipelineLayout pipelineLayout;
|
|
||||||
VkPipeline graphicsPipeline;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Pipeline(Instance& instance, PipelineCreator creator)
|
|
||||||
: instance{instance}
|
|
||||||
{
|
{
|
||||||
InitializeDescriptorSetLayout(creator);
|
CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline);
|
||||||
InitializePipeline(creator);
|
private:
|
||||||
}
|
Instance& instance;
|
||||||
|
|
||||||
~Pipeline()
|
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{};
|
||||||
{
|
std::vector<VkDescriptorSet> boundDescriptorSets;
|
||||||
vkDestroyPipeline(instance.GetDevice(), graphicsPipeline, nullptr);
|
VkPipelineLayout pipelineLayout;
|
||||||
vkDestroyPipelineLayout(instance.GetDevice(), pipelineLayout, nullptr);
|
VkPipeline graphicsPipeline;
|
||||||
for (auto&& descriptorSetLayout : descriptorSetLayouts)
|
|
||||||
|
public:
|
||||||
|
Pipeline(Instance& instance, PipelineCreator creator)
|
||||||
|
: instance{instance}
|
||||||
{
|
{
|
||||||
vkDestroyDescriptorSetLayout(instance.GetDevice(), descriptorSetLayout, nullptr);
|
InitializeDescriptorSetLayout(creator);
|
||||||
|
InitializePipeline(creator);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Bind(const CommandBuffer& commandBuffer)
|
~Pipeline()
|
||||||
{
|
|
||||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
|
|
||||||
|
|
||||||
VkViewport viewport{};
|
|
||||||
viewport.x = 0.0f;
|
|
||||||
viewport.y = 0.0f;
|
|
||||||
viewport.width = instance.GetSwapChain().GetExtent().width;
|
|
||||||
viewport.height = instance.GetSwapChain().GetExtent().height;
|
|
||||||
viewport.minDepth = 0.0f;
|
|
||||||
viewport.maxDepth = 1.0f;
|
|
||||||
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
|
||||||
VkRect2D scissor{};
|
|
||||||
scissor.offset = {0, 0};
|
|
||||||
scissor.extent = instance.GetSwapChain().GetExtent();
|
|
||||||
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetDescriptorSet(uint32_t setIndex, const DescriptorSet& descriptorSet)
|
|
||||||
{
|
|
||||||
CP_ASSERT(setIndex < boundDescriptorSets.size(), "DescriptorSet index is out of bounds");
|
|
||||||
boundDescriptorSets[setIndex] = descriptorSet.GetHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BindDescriptorSets(VkCommandBuffer commandBuffer)
|
|
||||||
{
|
|
||||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, boundDescriptorSets.size(), boundDescriptorSets.data(), 0, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t setIndex) const
|
|
||||||
{
|
|
||||||
return descriptorSetLayouts[setIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void InitializeDescriptorSetLayout(const PipelineCreator& creator)
|
|
||||||
{
|
|
||||||
boundDescriptorSets.resize(creator.descriptorSetLayouts.size());
|
|
||||||
descriptorSetLayouts.resize(creator.descriptorSetLayouts.size());
|
|
||||||
int i = 0;
|
|
||||||
for (auto&& bindings : creator.descriptorSetLayouts)
|
|
||||||
{
|
{
|
||||||
std::vector<VkDescriptorSetLayoutBinding> layoutBindings{bindings.second.size()};
|
vkDestroyPipeline(instance.GetDevice(), graphicsPipeline, nullptr);
|
||||||
int j = 0;
|
vkDestroyPipelineLayout(instance.GetDevice(), pipelineLayout, nullptr);
|
||||||
for (auto&& binding : bindings.second)
|
for (auto&& descriptorSetLayout : descriptorSetLayouts)
|
||||||
{
|
{
|
||||||
layoutBindings[j].binding = binding.binding;
|
vkDestroyDescriptorSetLayout(instance.GetDevice(), descriptorSetLayout, nullptr);
|
||||||
layoutBindings[j].descriptorType = binding.type;
|
|
||||||
layoutBindings[j].descriptorCount = binding.count;
|
|
||||||
layoutBindings[j].stageFlags = binding.flags;
|
|
||||||
layoutBindings[j].pImmutableSamplers = nullptr;
|
|
||||||
j++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VkDescriptorSetLayoutCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
|
||||||
createInfo.bindingCount = layoutBindings.size();
|
|
||||||
createInfo.pBindings = layoutBindings.data();
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateDescriptorSetLayout(instance.GetDevice(), &createInfo, nullptr, &descriptorSetLayouts[i++]), "Failed to initialize descriptor set layout");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void InitializePipeline(const PipelineCreator& creator)
|
void Bind(const CommandBuffer& commandBuffer)
|
||||||
{
|
{
|
||||||
Shader shader{instance, ShaderType::GlslFile, creator.vertexShader, creator.fragmentShader};
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
|
||||||
|
|
||||||
VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{};
|
VkViewport viewport{};
|
||||||
vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
viewport.x = 0.0f;
|
||||||
vertexInputCreateInfo.vertexBindingDescriptionCount = creator.vertexDescriptor.GetBindings().size();
|
viewport.y = 0.0f;
|
||||||
vertexInputCreateInfo.pVertexBindingDescriptions = creator.vertexDescriptor.GetBindings().data();
|
viewport.width = instance.GetSwapChain().GetExtent().width;
|
||||||
vertexInputCreateInfo.vertexAttributeDescriptionCount = creator.vertexDescriptor.GetAttributes().size();
|
viewport.height = instance.GetSwapChain().GetExtent().height;
|
||||||
vertexInputCreateInfo.pVertexAttributeDescriptions = creator.vertexDescriptor.GetAttributes().data();
|
viewport.minDepth = 0.0f;
|
||||||
|
viewport.maxDepth = 1.0f;
|
||||||
|
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
||||||
|
VkRect2D scissor{};
|
||||||
|
scissor.offset = {0, 0};
|
||||||
|
scissor.extent = instance.GetSwapChain().GetExtent();
|
||||||
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||||
|
}
|
||||||
|
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo{};
|
void SetDescriptorSet(uint32_t setIndex, const DescriptorSet& descriptorSet)
|
||||||
inputAssemblyCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
{
|
||||||
inputAssemblyCreateInfo.topology = creator.topology;
|
CP_ASSERT(setIndex < boundDescriptorSets.size(), "DescriptorSet index is out of bounds");
|
||||||
inputAssemblyCreateInfo.primitiveRestartEnable = VK_FALSE;
|
boundDescriptorSets[setIndex] = descriptorSet.GetHandle();
|
||||||
|
}
|
||||||
|
|
||||||
VkViewport viewport{};
|
void BindDescriptorSets(VkCommandBuffer commandBuffer)
|
||||||
viewport.x = 0;
|
{
|
||||||
viewport.y = 0;
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, boundDescriptorSets.size(), boundDescriptorSets.data(), 0, nullptr);
|
||||||
viewport.width = instance.GetSwapChain().GetExtent().width;
|
}
|
||||||
viewport.height = instance.GetSwapChain().GetExtent().height;
|
|
||||||
viewport.minDepth = 0.0f;
|
|
||||||
viewport.maxDepth = 1.0f;
|
|
||||||
|
|
||||||
VkRect2D scissor{};
|
VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t setIndex) const
|
||||||
scissor.offset = {0, 0};
|
{
|
||||||
scissor.extent = instance.GetSwapChain().GetExtent();
|
return descriptorSetLayouts[setIndex];
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<VkDynamicState> dynamicStates = {
|
private:
|
||||||
VK_DYNAMIC_STATE_VIEWPORT,
|
void InitializeDescriptorSetLayout(const PipelineCreator& creator)
|
||||||
VK_DYNAMIC_STATE_SCISSOR
|
{
|
||||||
};
|
boundDescriptorSets.resize(creator.descriptorSetLayouts.size());
|
||||||
|
descriptorSetLayouts.resize(creator.descriptorSetLayouts.size());
|
||||||
|
int i = 0;
|
||||||
|
for (auto&& bindings : creator.descriptorSetLayouts)
|
||||||
|
{
|
||||||
|
std::vector<VkDescriptorSetLayoutBinding> layoutBindings{bindings.second.size()};
|
||||||
|
int j = 0;
|
||||||
|
for (auto&& binding : bindings.second)
|
||||||
|
{
|
||||||
|
layoutBindings[j].binding = binding.binding;
|
||||||
|
layoutBindings[j].descriptorType = binding.type;
|
||||||
|
layoutBindings[j].descriptorCount = binding.count;
|
||||||
|
layoutBindings[j].stageFlags = binding.flags;
|
||||||
|
layoutBindings[j].pImmutableSamplers = nullptr;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo{};
|
VkDescriptorSetLayoutCreateInfo createInfo{};
|
||||||
dynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||||
dynamicStateCreateInfo.dynamicStateCount = dynamicStates.size();
|
createInfo.bindingCount = layoutBindings.size();
|
||||||
dynamicStateCreateInfo.pDynamicStates = dynamicStates.data();
|
createInfo.pBindings = layoutBindings.data();
|
||||||
|
|
||||||
VkPipelineViewportStateCreateInfo viewportStateCreateInfo{};
|
CP_VK_ASSERT(vkCreateDescriptorSetLayout(instance.GetDevice(), &createInfo, nullptr, &descriptorSetLayouts[i++]), "Failed to initialize descriptor set layout");
|
||||||
viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
}
|
||||||
viewportStateCreateInfo.viewportCount = 1;
|
}
|
||||||
viewportStateCreateInfo.pViewports = &viewport;
|
|
||||||
viewportStateCreateInfo.scissorCount = 1;
|
|
||||||
viewportStateCreateInfo.pScissors = &scissor;
|
|
||||||
|
|
||||||
VkPipelineRasterizationStateCreateInfo rasterizerCreateInfo{};
|
void InitializePipeline(const PipelineCreator& creator)
|
||||||
rasterizerCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
{
|
||||||
rasterizerCreateInfo.depthClampEnable = VK_FALSE;
|
Shader shader{instance, Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader};
|
||||||
rasterizerCreateInfo.rasterizerDiscardEnable = VK_FALSE;
|
|
||||||
rasterizerCreateInfo.polygonMode = VK_POLYGON_MODE_FILL;
|
|
||||||
rasterizerCreateInfo.lineWidth = 1.0f;
|
|
||||||
rasterizerCreateInfo.cullMode = creator.cullMode;
|
|
||||||
rasterizerCreateInfo.frontFace = creator.frontFace;
|
|
||||||
|
|
||||||
rasterizerCreateInfo.depthBiasEnable = VK_FALSE;
|
VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{};
|
||||||
rasterizerCreateInfo.depthBiasConstantFactor = 0.0f;
|
vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||||
rasterizerCreateInfo.depthBiasClamp = 0.0f;
|
vertexInputCreateInfo.vertexBindingDescriptionCount = creator.vertexDescriptor.GetBindings().size();
|
||||||
rasterizerCreateInfo.depthBiasSlopeFactor = 0.0f;
|
vertexInputCreateInfo.pVertexBindingDescriptions = creator.vertexDescriptor.GetBindings().data();
|
||||||
|
vertexInputCreateInfo.vertexAttributeDescriptionCount = creator.vertexDescriptor.GetAttributes().size();
|
||||||
|
vertexInputCreateInfo.pVertexAttributeDescriptions = creator.vertexDescriptor.GetAttributes().data();
|
||||||
|
|
||||||
VkPipelineMultisampleStateCreateInfo multisampleCreateInfo{};
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo{};
|
||||||
multisampleCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
inputAssemblyCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||||||
multisampleCreateInfo.sampleShadingEnable = VK_FALSE;
|
inputAssemblyCreateInfo.topology = creator.topology;
|
||||||
multisampleCreateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
inputAssemblyCreateInfo.primitiveRestartEnable = VK_FALSE;
|
||||||
multisampleCreateInfo.minSampleShading = 1.0f;
|
|
||||||
multisampleCreateInfo.pSampleMask = nullptr;
|
|
||||||
multisampleCreateInfo.alphaToCoverageEnable = VK_FALSE;
|
|
||||||
multisampleCreateInfo.alphaToOneEnable = VK_FALSE;
|
|
||||||
|
|
||||||
VkPipelineDepthStencilStateCreateInfo depthStencilCreateInfo{};
|
VkViewport viewport{};
|
||||||
depthStencilCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
viewport.x = 0;
|
||||||
depthStencilCreateInfo.depthTestEnable = VK_TRUE;
|
viewport.y = 0;
|
||||||
depthStencilCreateInfo.depthWriteEnable = VK_TRUE;
|
viewport.width = instance.GetSwapChain().GetExtent().width;
|
||||||
depthStencilCreateInfo.depthCompareOp = VK_COMPARE_OP_LESS;
|
viewport.height = instance.GetSwapChain().GetExtent().height;
|
||||||
depthStencilCreateInfo.depthBoundsTestEnable = VK_FALSE;
|
viewport.minDepth = 0.0f;
|
||||||
depthStencilCreateInfo.minDepthBounds = 0.0f;
|
viewport.maxDepth = 1.0f;
|
||||||
depthStencilCreateInfo.maxDepthBounds = 1.0f;
|
|
||||||
depthStencilCreateInfo.stencilTestEnable = VK_FALSE;
|
VkRect2D scissor{};
|
||||||
depthStencilCreateInfo.front = {};
|
scissor.offset = {0, 0};
|
||||||
depthStencilCreateInfo.back = {};
|
scissor.extent = instance.GetSwapChain().GetExtent();
|
||||||
|
|
||||||
|
std::vector<VkDynamicState> dynamicStates = {
|
||||||
|
VK_DYNAMIC_STATE_VIEWPORT,
|
||||||
|
VK_DYNAMIC_STATE_SCISSOR
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo{};
|
||||||
|
dynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
||||||
|
dynamicStateCreateInfo.dynamicStateCount = dynamicStates.size();
|
||||||
|
dynamicStateCreateInfo.pDynamicStates = dynamicStates.data();
|
||||||
|
|
||||||
|
VkPipelineViewportStateCreateInfo viewportStateCreateInfo{};
|
||||||
|
viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
||||||
|
viewportStateCreateInfo.viewportCount = 1;
|
||||||
|
viewportStateCreateInfo.pViewports = &viewport;
|
||||||
|
viewportStateCreateInfo.scissorCount = 1;
|
||||||
|
viewportStateCreateInfo.pScissors = &scissor;
|
||||||
|
|
||||||
|
VkPipelineRasterizationStateCreateInfo rasterizerCreateInfo{};
|
||||||
|
rasterizerCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||||||
|
rasterizerCreateInfo.depthClampEnable = VK_FALSE;
|
||||||
|
rasterizerCreateInfo.rasterizerDiscardEnable = VK_FALSE;
|
||||||
|
rasterizerCreateInfo.polygonMode = VK_POLYGON_MODE_FILL;
|
||||||
|
rasterizerCreateInfo.lineWidth = 1.0f;
|
||||||
|
rasterizerCreateInfo.cullMode = creator.cullMode;
|
||||||
|
rasterizerCreateInfo.frontFace = creator.frontFace;
|
||||||
|
|
||||||
|
rasterizerCreateInfo.depthBiasEnable = VK_FALSE;
|
||||||
|
rasterizerCreateInfo.depthBiasConstantFactor = 0.0f;
|
||||||
|
rasterizerCreateInfo.depthBiasClamp = 0.0f;
|
||||||
|
rasterizerCreateInfo.depthBiasSlopeFactor = 0.0f;
|
||||||
|
|
||||||
|
VkPipelineMultisampleStateCreateInfo multisampleCreateInfo{};
|
||||||
|
multisampleCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
||||||
|
multisampleCreateInfo.sampleShadingEnable = VK_FALSE;
|
||||||
|
multisampleCreateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
multisampleCreateInfo.minSampleShading = 1.0f;
|
||||||
|
multisampleCreateInfo.pSampleMask = nullptr;
|
||||||
|
multisampleCreateInfo.alphaToCoverageEnable = VK_FALSE;
|
||||||
|
multisampleCreateInfo.alphaToOneEnable = VK_FALSE;
|
||||||
|
|
||||||
|
VkPipelineDepthStencilStateCreateInfo depthStencilCreateInfo{};
|
||||||
|
depthStencilCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
||||||
|
depthStencilCreateInfo.depthTestEnable = VK_TRUE;
|
||||||
|
depthStencilCreateInfo.depthWriteEnable = VK_TRUE;
|
||||||
|
depthStencilCreateInfo.depthCompareOp = VK_COMPARE_OP_LESS;
|
||||||
|
depthStencilCreateInfo.depthBoundsTestEnable = VK_FALSE;
|
||||||
|
depthStencilCreateInfo.minDepthBounds = 0.0f;
|
||||||
|
depthStencilCreateInfo.maxDepthBounds = 1.0f;
|
||||||
|
depthStencilCreateInfo.stencilTestEnable = VK_FALSE;
|
||||||
|
depthStencilCreateInfo.front = {};
|
||||||
|
depthStencilCreateInfo.back = {};
|
||||||
|
|
||||||
|
|
||||||
VkPipelineColorBlendAttachmentState colorBlendAttachment{}; // TODO: Add to PipelineCreator
|
VkPipelineColorBlendAttachmentState colorBlendAttachment{}; // TODO: Add to PipelineCreator
|
||||||
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
|
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
|
||||||
VK_COLOR_COMPONENT_G_BIT |
|
VK_COLOR_COMPONENT_G_BIT |
|
||||||
VK_COLOR_COMPONENT_B_BIT |
|
VK_COLOR_COMPONENT_B_BIT |
|
||||||
VK_COLOR_COMPONENT_A_BIT;
|
VK_COLOR_COMPONENT_A_BIT;
|
||||||
colorBlendAttachment.blendEnable = VK_FALSE;
|
colorBlendAttachment.blendEnable = VK_FALSE;
|
||||||
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
|
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||||
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
|
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||||
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
||||||
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||||
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||||
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||||
|
|
||||||
VkPipelineColorBlendStateCreateInfo colorBlendCreateInfo{};
|
VkPipelineColorBlendStateCreateInfo colorBlendCreateInfo{};
|
||||||
colorBlendCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
colorBlendCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||||
colorBlendCreateInfo.logicOpEnable = VK_FALSE;
|
colorBlendCreateInfo.logicOpEnable = VK_FALSE;
|
||||||
colorBlendCreateInfo.logicOp = VK_LOGIC_OP_COPY;
|
colorBlendCreateInfo.logicOp = VK_LOGIC_OP_COPY;
|
||||||
colorBlendCreateInfo.attachmentCount = 1;
|
colorBlendCreateInfo.attachmentCount = 1;
|
||||||
colorBlendCreateInfo.pAttachments = &colorBlendAttachment;
|
colorBlendCreateInfo.pAttachments = &colorBlendAttachment;
|
||||||
colorBlendCreateInfo.blendConstants[0] = 0.0f;
|
colorBlendCreateInfo.blendConstants[0] = 0.0f;
|
||||||
colorBlendCreateInfo.blendConstants[1] = 0.0f;
|
colorBlendCreateInfo.blendConstants[1] = 0.0f;
|
||||||
colorBlendCreateInfo.blendConstants[2] = 0.0f;
|
colorBlendCreateInfo.blendConstants[2] = 0.0f;
|
||||||
colorBlendCreateInfo.blendConstants[3] = 0.0f;
|
colorBlendCreateInfo.blendConstants[3] = 0.0f;
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo{};
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo{};
|
||||||
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||||
pipelineLayoutCreateInfo.setLayoutCount = descriptorSetLayouts.size();
|
pipelineLayoutCreateInfo.setLayoutCount = descriptorSetLayouts.size();
|
||||||
pipelineLayoutCreateInfo.pSetLayouts = descriptorSetLayouts.data();
|
pipelineLayoutCreateInfo.pSetLayouts = descriptorSetLayouts.data();
|
||||||
pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
||||||
pipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
pipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreatePipelineLayout(instance.GetDevice(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout), "Failed to initialize pipeline layout");
|
CP_VK_ASSERT(vkCreatePipelineLayout(instance.GetDevice(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout), "Failed to initialize pipeline layout");
|
||||||
|
|
||||||
const std::vector<VkPipelineShaderStageCreateInfo>& shaderStages = shader.GetShaderStages();
|
const std::vector<VkPipelineShaderStageCreateInfo>& shaderStages = shader.GetShaderStages();
|
||||||
VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{};
|
VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{};
|
||||||
graphicsPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
graphicsPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
||||||
graphicsPipelineCreateInfo.stageCount = shaderStages.size();
|
graphicsPipelineCreateInfo.stageCount = shaderStages.size();
|
||||||
graphicsPipelineCreateInfo.pStages = shaderStages.data();
|
graphicsPipelineCreateInfo.pStages = shaderStages.data();
|
||||||
graphicsPipelineCreateInfo.pVertexInputState = &vertexInputCreateInfo;
|
graphicsPipelineCreateInfo.pVertexInputState = &vertexInputCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pInputAssemblyState = &inputAssemblyCreateInfo;
|
graphicsPipelineCreateInfo.pInputAssemblyState = &inputAssemblyCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
|
graphicsPipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pRasterizationState = &rasterizerCreateInfo;
|
graphicsPipelineCreateInfo.pRasterizationState = &rasterizerCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pMultisampleState = &multisampleCreateInfo;
|
graphicsPipelineCreateInfo.pMultisampleState = &multisampleCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pDepthStencilState = &depthStencilCreateInfo;
|
graphicsPipelineCreateInfo.pDepthStencilState = &depthStencilCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pColorBlendState = &colorBlendCreateInfo;
|
graphicsPipelineCreateInfo.pColorBlendState = &colorBlendCreateInfo;
|
||||||
graphicsPipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo;
|
graphicsPipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo;
|
||||||
graphicsPipelineCreateInfo.layout = pipelineLayout;
|
graphicsPipelineCreateInfo.layout = pipelineLayout;
|
||||||
graphicsPipelineCreateInfo.renderPass = creator.renderPass;
|
graphicsPipelineCreateInfo.renderPass = creator.renderPass;
|
||||||
graphicsPipelineCreateInfo.subpass = 0;
|
graphicsPipelineCreateInfo.subpass = 0;
|
||||||
graphicsPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
|
graphicsPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
|
||||||
graphicsPipelineCreateInfo.basePipelineIndex = -1;
|
graphicsPipelineCreateInfo.basePipelineIndex = -1;
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateGraphicsPipelines(instance.GetDevice(), VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline), "Failed to initialize graphics pipeline");
|
CP_VK_ASSERT(vkCreateGraphicsPipelines(instance.GetDevice(), VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline), "Failed to initialize graphics pipeline");
|
||||||
}
|
}
|
||||||
|
|
||||||
VkShaderModule InitializeShaderModule(const std::vector<char>& code)
|
VkShaderModule InitializeShaderModule(const std::vector<char>& code)
|
||||||
{
|
{
|
||||||
VkShaderModuleCreateInfo createInfo{};
|
VkShaderModuleCreateInfo createInfo{};
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||||
createInfo.codeSize = code.size();
|
createInfo.codeSize = code.size();
|
||||||
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
||||||
|
|
||||||
VkShaderModule shaderModule;
|
VkShaderModule shaderModule;
|
||||||
CP_VK_ASSERT(vkCreateShaderModule(instance.GetDevice(), &createInfo, nullptr, &shaderModule), "Failed to initialize shader module");
|
CP_VK_ASSERT(vkCreateShaderModule(instance.GetDevice(), &createInfo, nullptr, &shaderModule), "Failed to initialize shader module");
|
||||||
|
|
||||||
return shaderModule;
|
return shaderModule;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
@@ -6,55 +6,58 @@
|
|||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
class PipelineCreator
|
namespace Copium
|
||||||
{
|
{
|
||||||
struct DescriptorSetBinding
|
class PipelineCreator
|
||||||
{
|
{
|
||||||
uint32_t binding;
|
struct DescriptorSetBinding
|
||||||
VkDescriptorType type;
|
{
|
||||||
uint32_t count;
|
uint32_t binding;
|
||||||
VkShaderStageFlags flags;
|
VkDescriptorType type;
|
||||||
|
uint32_t count;
|
||||||
|
VkShaderStageFlags flags;
|
||||||
|
};
|
||||||
|
friend class Pipeline;
|
||||||
|
private:
|
||||||
|
std::map<uint32_t, std::vector<DescriptorSetBinding>> descriptorSetLayouts{};
|
||||||
|
|
||||||
|
std::string vertexShader;
|
||||||
|
std::string fragmentShader;
|
||||||
|
VertexDescriptor vertexDescriptor{};
|
||||||
|
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||||
|
VkCullModeFlags cullMode = VK_CULL_MODE_BACK_BIT;
|
||||||
|
VkFrontFace frontFace = VK_FRONT_FACE_CLOCKWISE;
|
||||||
|
VkRenderPass renderPass = VK_NULL_HANDLE;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader)
|
||||||
|
: vertexShader{vertexShader}, fragmentShader{fragmentShader}, renderPass{renderPass}
|
||||||
|
{}
|
||||||
|
|
||||||
|
void SetVertexDescriptor(const VertexDescriptor& descriptor)
|
||||||
|
{
|
||||||
|
vertexDescriptor = descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddDescriptorSetLayoutBinding(uint32_t set, uint32_t binding, VkDescriptorType type, uint32_t count, VkShaderStageFlags stageFlags)
|
||||||
|
{
|
||||||
|
CP_ASSERT(set <= descriptorSetLayouts.size(), "AddDescriptorSetLayoutBinding : Cannot add descriptor set with set number greater than the current set count");
|
||||||
|
descriptorSetLayouts[set].emplace_back(DescriptorSetBinding{binding, type, count, stageFlags});
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology)
|
||||||
|
{
|
||||||
|
topology = primitiveTopology;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetCullMode(VkCullModeFlags flags)
|
||||||
|
{
|
||||||
|
cullMode = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetCullFrontFace(VkFrontFace cullFrontFace)
|
||||||
|
{
|
||||||
|
frontFace = cullFrontFace;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
friend class Pipeline;
|
}
|
||||||
private:
|
|
||||||
std::map<uint32_t, std::vector<DescriptorSetBinding>> descriptorSetLayouts{};
|
|
||||||
|
|
||||||
std::string vertexShader;
|
|
||||||
std::string fragmentShader;
|
|
||||||
VertexDescriptor vertexDescriptor{};
|
|
||||||
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
||||||
VkCullModeFlags cullMode = VK_CULL_MODE_BACK_BIT;
|
|
||||||
VkFrontFace frontFace = VK_FRONT_FACE_CLOCKWISE;
|
|
||||||
VkRenderPass renderPass = VK_NULL_HANDLE;
|
|
||||||
|
|
||||||
public:
|
|
||||||
PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader)
|
|
||||||
: vertexShader{vertexShader}, fragmentShader{fragmentShader}, renderPass{renderPass}
|
|
||||||
{}
|
|
||||||
|
|
||||||
void SetVertexDescriptor(const VertexDescriptor& descriptor)
|
|
||||||
{
|
|
||||||
vertexDescriptor = descriptor;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddDescriptorSetLayoutBinding(uint32_t set, uint32_t binding, VkDescriptorType type, uint32_t count, VkShaderStageFlags stageFlags)
|
|
||||||
{
|
|
||||||
CP_ASSERT(set <= descriptorSetLayouts.size(), "AddDescriptorSetLayoutBinding : Cannot add descriptor set with set number greater than the current set count");
|
|
||||||
descriptorSetLayouts[set].emplace_back(DescriptorSetBinding{binding, type, count, stageFlags});
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology)
|
|
||||||
{
|
|
||||||
topology = primitiveTopology;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetCullMode(VkCullModeFlags flags)
|
|
||||||
{
|
|
||||||
cullMode = flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetCullFrontFace(VkFrontFace cullFrontFace)
|
|
||||||
{
|
|
||||||
frontFace = cullFrontFace;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
+30
-27
@@ -4,38 +4,41 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct QueueFamiliesQuery
|
namespace Copium
|
||||||
{
|
{
|
||||||
std::optional<uint32_t> graphicsFamily;
|
struct QueueFamiliesQuery
|
||||||
std::optional<uint32_t> presentFamily;
|
|
||||||
|
|
||||||
QueueFamiliesQuery(VkSurfaceKHR surface, VkPhysicalDevice device)
|
|
||||||
{
|
{
|
||||||
uint32_t queueFamilyCount = 0;
|
std::optional<uint32_t> graphicsFamily;
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
std::optional<uint32_t> presentFamily;
|
||||||
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
|
||||||
|
|
||||||
int i = 0;
|
QueueFamiliesQuery(VkSurfaceKHR surface, VkPhysicalDevice device)
|
||||||
for (auto&& queueFamily : queueFamilies)
|
|
||||||
{
|
{
|
||||||
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
uint32_t queueFamilyCount = 0;
|
||||||
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
||||||
|
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
||||||
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (auto&& queueFamily : queueFamilies)
|
||||||
{
|
{
|
||||||
graphicsFamily = i;
|
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||||
|
{
|
||||||
|
graphicsFamily = i;
|
||||||
|
}
|
||||||
|
VkBool32 presentSupport = false;
|
||||||
|
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
||||||
|
if (presentSupport)
|
||||||
|
{
|
||||||
|
presentFamily = i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
VkBool32 presentSupport = false;
|
|
||||||
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
|
||||||
if (presentSupport)
|
|
||||||
{
|
|
||||||
presentFamily = i;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
bool AllRequiredFamiliesSupported()
|
||||||
|
{
|
||||||
bool AllRequiredFamiliesSupported()
|
return graphicsFamily.has_value() && presentFamily.has_value();
|
||||||
{
|
}
|
||||||
return graphicsFamily.has_value() && presentFamily.has_value();
|
};
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|||||||
+131
-127
@@ -6,146 +6,150 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "Instance.h"
|
#include "Instance.h"
|
||||||
|
|
||||||
enum class ShaderType
|
namespace Copium
|
||||||
{
|
{
|
||||||
GlslFile, GlslCode, SpvFile, SpvCode
|
class Shader
|
||||||
};
|
|
||||||
|
|
||||||
class Shader
|
|
||||||
{
|
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Shader);
|
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
VkShaderModule vertShaderModule;
|
|
||||||
VkShaderModule fragShaderModule;
|
|
||||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
|
||||||
public:
|
|
||||||
Shader(Instance& instance, ShaderType shaderType, const std::string& vertexInput, const std::string& fragmentInput)
|
|
||||||
: instance{instance}
|
|
||||||
{
|
{
|
||||||
switch (shaderType)
|
CP_DELETE_COPY_AND_MOVE_CTOR(Shader);
|
||||||
|
public:
|
||||||
|
enum class Type
|
||||||
{
|
{
|
||||||
case ShaderType::GlslCode:
|
GlslFile, GlslCode, SpvFile, SpvCode
|
||||||
vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader);
|
};
|
||||||
fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader);
|
|
||||||
break;
|
private:
|
||||||
case ShaderType::GlslFile:
|
Instance& instance;
|
||||||
vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader);
|
|
||||||
fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader);
|
VkShaderModule vertShaderModule;
|
||||||
break;
|
VkShaderModule fragShaderModule;
|
||||||
case ShaderType::SpvCode:
|
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
||||||
vertShaderModule = InitializeShaderModule(vertexInput);
|
public:
|
||||||
fragShaderModule = InitializeShaderModule(fragmentInput);
|
Shader(Instance& instance, Type type, const std::string& vertexInput, const std::string& fragmentInput)
|
||||||
break;
|
: instance{instance}
|
||||||
case ShaderType::SpvFile:
|
{
|
||||||
vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput));
|
switch (type)
|
||||||
fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput));
|
{
|
||||||
break;
|
case Type::GlslCode:
|
||||||
default:
|
vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader);
|
||||||
CP_ASSERT(false, "Unreachable switch case %d", (int)shaderType);
|
fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader);
|
||||||
|
break;
|
||||||
|
case Type::GlslFile:
|
||||||
|
vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader);
|
||||||
|
fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader);
|
||||||
|
break;
|
||||||
|
case Type::SpvCode:
|
||||||
|
vertShaderModule = InitializeShaderModule(vertexInput);
|
||||||
|
fragShaderModule = InitializeShaderModule(fragmentInput);
|
||||||
|
break;
|
||||||
|
case Type::SpvFile:
|
||||||
|
vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput));
|
||||||
|
fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
CP_ASSERT(false, "Unreachable switch case %d", (int)type);
|
||||||
|
}
|
||||||
|
|
||||||
|
shaderStages.resize(2);
|
||||||
|
shaderStages[0] = {};
|
||||||
|
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||||
|
shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||||
|
shaderStages[0].module = vertShaderModule;
|
||||||
|
shaderStages[0].pName = "main";
|
||||||
|
|
||||||
|
shaderStages[1] = {};
|
||||||
|
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||||
|
shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||||
|
shaderStages[1].module = fragShaderModule;
|
||||||
|
shaderStages[1].pName = "main";
|
||||||
}
|
}
|
||||||
|
|
||||||
shaderStages.resize(2);
|
~Shader()
|
||||||
shaderStages[0] = {};
|
|
||||||
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
||||||
shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
|
|
||||||
shaderStages[0].module = vertShaderModule;
|
|
||||||
shaderStages[0].pName = "main";
|
|
||||||
|
|
||||||
shaderStages[1] = {};
|
|
||||||
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
||||||
shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
||||||
shaderStages[1].module = fragShaderModule;
|
|
||||||
shaderStages[1].pName = "main";
|
|
||||||
}
|
|
||||||
|
|
||||||
~Shader()
|
|
||||||
{
|
|
||||||
vkDestroyShaderModule(instance.GetDevice(), vertShaderModule, nullptr);
|
|
||||||
vkDestroyShaderModule(instance.GetDevice(), fragShaderModule, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const
|
|
||||||
{
|
|
||||||
return shaderStages;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkShaderModule InitializeShaderModule(const std::vector<uint32_t>& codeSpv)
|
|
||||||
{
|
|
||||||
return InitializeShaderModule(codeSpv.data(), codeSpv.size() * sizeof(uint32_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
VkShaderModule InitializeShaderModule(const std::string& codeSpv)
|
|
||||||
{
|
|
||||||
return InitializeShaderModule(reinterpret_cast<const uint32_t*>(codeSpv.data()), codeSpv.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
VkShaderModule InitializeShaderModule(const std::vector<char>& codeSpv)
|
|
||||||
{
|
|
||||||
return InitializeShaderModule(reinterpret_cast<const uint32_t*>(codeSpv.data()), codeSpv.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
VkShaderModule InitializeShaderModuleFromGlslFile(const std::string& filename, shaderc_shader_kind shaderType)
|
|
||||||
{
|
|
||||||
std::string spvFilename = ".cache/" + filename + ".spv";
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (FileSystem::FileExists(spvFilename))
|
vkDestroyShaderModule(instance.GetDevice(), vertShaderModule, nullptr);
|
||||||
|
vkDestroyShaderModule(instance.GetDevice(), fragShaderModule, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const
|
||||||
|
{
|
||||||
|
return shaderStages;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
VkShaderModule InitializeShaderModule(const std::vector<uint32_t>& codeSpv)
|
||||||
|
{
|
||||||
|
return InitializeShaderModule(codeSpv.data(), codeSpv.size() * sizeof(uint32_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
VkShaderModule InitializeShaderModule(const std::string& codeSpv)
|
||||||
|
{
|
||||||
|
return InitializeShaderModule(reinterpret_cast<const uint32_t*>(codeSpv.data()), codeSpv.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
VkShaderModule InitializeShaderModule(const std::vector<char>& codeSpv)
|
||||||
|
{
|
||||||
|
return InitializeShaderModule(reinterpret_cast<const uint32_t*>(codeSpv.data()), codeSpv.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
VkShaderModule InitializeShaderModuleFromGlslFile(const std::string& filename, shaderc_shader_kind type)
|
||||||
|
{
|
||||||
|
std::string spvFilename = ".cache/" + filename + ".spv";
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (FileSystem::DateModified(filename) < FileSystem::DateModified(spvFilename))
|
if (FileSystem::FileExists(spvFilename))
|
||||||
{
|
{
|
||||||
CP_DEBUG("Loading cached shader file: %s", filename.c_str());
|
if (FileSystem::DateModified(filename) < FileSystem::DateModified(spvFilename))
|
||||||
std::vector<char> data = FileSystem::ReadFile(spvFilename);
|
{
|
||||||
CP_ASSERT(data.size() % 4 == 0, "Spv data size is not a factor of 4");
|
CP_DEBUG("Loading cached shader file: %s", filename.c_str());
|
||||||
return InitializeShaderModule((const uint32_t*)data.data(), data.size());
|
std::vector<char> data = FileSystem::ReadFile(spvFilename);
|
||||||
|
CP_ASSERT(data.size() % 4 == 0, "Spv data size is not a factor of 4");
|
||||||
|
return InitializeShaderModule((const uint32_t*)data.data(), data.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (const std::runtime_error& e)
|
||||||
|
{
|
||||||
|
CP_WARN("Cached shader file is invalid, recreating it");
|
||||||
|
}
|
||||||
|
CP_DEBUG("Compiling shader file: %s", filename.c_str());
|
||||||
|
shaderc::Compiler compiler;
|
||||||
|
shaderc::CompileOptions options;
|
||||||
|
|
||||||
|
options.SetOptimizationLevel(shaderc_optimization_level_size);
|
||||||
|
|
||||||
|
std::vector<char> glslCode = FileSystem::ReadFile(filename);
|
||||||
|
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(glslCode.data(), glslCode.size(), type, filename.c_str(), options);
|
||||||
|
CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile shader: %s\n%s", filename.c_str(), result.GetErrorMessage().c_str());
|
||||||
|
|
||||||
|
std::vector<uint32_t> data{result.cbegin(), result.cend()};
|
||||||
|
FileSystem::WriteFile(spvFilename, (const char*)data.data(), data.size() * sizeof(uint32_t));
|
||||||
|
return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t));
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& e)
|
|
||||||
|
VkShaderModule InitializeShaderModuleFromGlslCode(const std::string& code, shaderc_shader_kind type)
|
||||||
{
|
{
|
||||||
CP_WARN("Cached shader file is invalid, recreating it");
|
shaderc::Compiler compiler;
|
||||||
|
shaderc::CompileOptions options;
|
||||||
|
|
||||||
|
options.SetOptimizationLevel(shaderc_optimization_level_size);
|
||||||
|
|
||||||
|
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(code.data(), type, "inline_shader_code", options);
|
||||||
|
CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile inline shader code: %s", result.GetErrorMessage());
|
||||||
|
|
||||||
|
std::vector<uint32_t> data{result.cbegin(), result.cend()};
|
||||||
|
return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t));
|
||||||
}
|
}
|
||||||
CP_DEBUG("Compiling shader file: %s", filename.c_str());
|
|
||||||
shaderc::Compiler compiler;
|
|
||||||
shaderc::CompileOptions options;
|
|
||||||
|
|
||||||
options.SetOptimizationLevel(shaderc_optimization_level_size);
|
VkShaderModule InitializeShaderModule(const uint32_t* data, size_t size)
|
||||||
|
{
|
||||||
|
VkShaderModuleCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||||
|
createInfo.codeSize = size;
|
||||||
|
createInfo.pCode = data;
|
||||||
|
|
||||||
std::vector<char> glslCode = FileSystem::ReadFile(filename);
|
VkShaderModule shaderModule;
|
||||||
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(glslCode.data(), glslCode.size(), shaderType, filename.c_str(), options);
|
CP_VK_ASSERT(vkCreateShaderModule(instance.GetDevice(), &createInfo, nullptr, &shaderModule), "Failed to initialize shader module");
|
||||||
CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile shader: %s\n%s", filename.c_str(), result.GetErrorMessage().c_str());
|
|
||||||
|
|
||||||
std::vector<uint32_t> data{result.cbegin(), result.cend()};
|
return shaderModule;
|
||||||
FileSystem::WriteFile(spvFilename, (const char*)data.data(), data.size() * sizeof(uint32_t));
|
}
|
||||||
return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t));
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
VkShaderModule InitializeShaderModuleFromGlslCode(const std::string& code, shaderc_shader_kind shaderType)
|
|
||||||
{
|
|
||||||
shaderc::Compiler compiler;
|
|
||||||
shaderc::CompileOptions options;
|
|
||||||
|
|
||||||
options.SetOptimizationLevel(shaderc_optimization_level_size);
|
|
||||||
|
|
||||||
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(code.data(), shaderType, "inline_shader_code", options);
|
|
||||||
CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile inline shader code: %s", result.GetErrorMessage());
|
|
||||||
|
|
||||||
std::vector<uint32_t> data{result.cbegin(), result.cend()};
|
|
||||||
return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
VkShaderModule InitializeShaderModule(const uint32_t* data, size_t size)
|
|
||||||
{
|
|
||||||
VkShaderModuleCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
||||||
createInfo.codeSize = size;
|
|
||||||
createInfo.pCode = data;
|
|
||||||
|
|
||||||
VkShaderModule shaderModule;
|
|
||||||
CP_VK_ASSERT(vkCreateShaderModule(instance.GetDevice(), &createInfo, nullptr, &shaderModule), "Failed to initialize shader module");
|
|
||||||
|
|
||||||
return shaderModule;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
+309
-306
@@ -10,333 +10,336 @@
|
|||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
SwapChainSupportDetails::SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice)
|
namespace Copium
|
||||||
{
|
{
|
||||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
|
SwapChainSupportDetails::SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice)
|
||||||
|
|
||||||
uint32_t formatCount;
|
|
||||||
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr);
|
|
||||||
if (formatCount != 0)
|
|
||||||
{
|
{
|
||||||
formats.resize(formatCount);
|
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
|
||||||
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, formats.data());
|
|
||||||
|
uint32_t formatCount;
|
||||||
|
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr);
|
||||||
|
if (formatCount != 0)
|
||||||
|
{
|
||||||
|
formats.resize(formatCount);
|
||||||
|
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, formats.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t presentModeCount;
|
||||||
|
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr);
|
||||||
|
if (presentModeCount != 0)
|
||||||
|
{
|
||||||
|
presentModes.resize(presentModeCount);
|
||||||
|
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t presentModeCount;
|
bool SwapChainSupportDetails::Valid()
|
||||||
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr);
|
|
||||||
if (presentModeCount != 0)
|
|
||||||
{
|
{
|
||||||
presentModes.resize(presentModeCount);
|
return !formats.empty() && !presentModes.empty();
|
||||||
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool SwapChainSupportDetails::Valid()
|
SwapChain::SwapChain(Instance& instance)
|
||||||
{
|
: instance{instance}
|
||||||
return !formats.empty() && !presentModes.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
SwapChain::SwapChain(Instance& instance)
|
|
||||||
: instance{instance}
|
|
||||||
{
|
|
||||||
Initialize();
|
|
||||||
InitializeImageViews();
|
|
||||||
InitializeDepthBuffer();
|
|
||||||
InitializeRenderPass();
|
|
||||||
InitializeFramebuffers();
|
|
||||||
}
|
|
||||||
|
|
||||||
SwapChain::~SwapChain()
|
|
||||||
{
|
|
||||||
Destroy();
|
|
||||||
vkDestroyRenderPass(instance.GetDevice(), renderPass, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::BeginFrameBuffer(const CommandBuffer& commandBuffer) const
|
|
||||||
{
|
|
||||||
std::vector<VkClearValue> clearValues{2};
|
|
||||||
clearValues[0].color = {{0.02f, 0.02f, 0.02f, 1.0f}};
|
|
||||||
clearValues[1].depthStencil = {1.0f, 0};
|
|
||||||
|
|
||||||
VkRenderPassBeginInfo renderPassBeginInfo{};
|
|
||||||
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
|
||||||
renderPassBeginInfo.renderPass = renderPass;
|
|
||||||
renderPassBeginInfo.framebuffer = framebuffers[imageIndex];
|
|
||||||
renderPassBeginInfo.renderArea.offset = {0, 0};
|
|
||||||
renderPassBeginInfo.renderArea.extent = extent;
|
|
||||||
renderPassBeginInfo.clearValueCount = clearValues.size();
|
|
||||||
renderPassBeginInfo.pClearValues = clearValues.data();
|
|
||||||
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::EndFrameBuffer(const CommandBuffer& commandBuffer) const
|
|
||||||
{
|
|
||||||
vkCmdEndRenderPass(commandBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkSwapchainKHR SwapChain::GetHandle() const
|
|
||||||
{
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkRenderPass SwapChain::GetRenderPass() const
|
|
||||||
{
|
|
||||||
return renderPass;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkExtent2D SwapChain::GetExtent() const
|
|
||||||
{
|
|
||||||
return extent;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkFramebuffer SwapChain::GetFramebuffer() const
|
|
||||||
{
|
|
||||||
return framebuffers[imageIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SwapChain::BeginPresent(VkSemaphore signalSemaphore)
|
|
||||||
{
|
|
||||||
VkResult result = vkAcquireNextImageKHR(instance.GetDevice(), handle, UINT64_MAX, signalSemaphore, VK_NULL_HANDLE, &imageIndex);
|
|
||||||
if (result == VK_ERROR_OUT_OF_DATE_KHR)
|
|
||||||
{
|
{
|
||||||
Recreate();
|
Initialize();
|
||||||
return false;
|
InitializeImageViews();
|
||||||
|
InitializeDepthBuffer();
|
||||||
|
InitializeRenderPass();
|
||||||
|
InitializeFramebuffers();
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::EndPresent(VkQueue presentQueue, VkSemaphore* waitSemaphore, bool framebufferResized)
|
SwapChain::~SwapChain()
|
||||||
{
|
|
||||||
VkPresentInfoKHR presentInfo{};
|
|
||||||
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
||||||
presentInfo.waitSemaphoreCount = 1;
|
|
||||||
presentInfo.pWaitSemaphores = waitSemaphore;
|
|
||||||
presentInfo.swapchainCount = 1;
|
|
||||||
presentInfo.pSwapchains = &handle;
|
|
||||||
presentInfo.pImageIndices = &imageIndex;
|
|
||||||
presentInfo.pResults = nullptr;
|
|
||||||
|
|
||||||
VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo);
|
|
||||||
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized)
|
|
||||||
{
|
{
|
||||||
Recreate();
|
Destroy();
|
||||||
|
vkDestroyRenderPass(instance.GetDevice(), renderPass, nullptr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::Recreate()
|
void SwapChain::BeginFrameBuffer(const CommandBuffer& commandBuffer) const
|
||||||
{
|
|
||||||
int width = 0;
|
|
||||||
int height = 0;
|
|
||||||
glfwGetFramebufferSize(instance.GetWindow(), &width, &height);
|
|
||||||
while (width == 0 || height == 0)
|
|
||||||
{
|
{
|
||||||
|
std::vector<VkClearValue> clearValues{2};
|
||||||
|
clearValues[0].color = {{0.02f, 0.02f, 0.02f, 1.0f}};
|
||||||
|
clearValues[1].depthStencil = {1.0f, 0};
|
||||||
|
|
||||||
|
VkRenderPassBeginInfo renderPassBeginInfo{};
|
||||||
|
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||||
|
renderPassBeginInfo.renderPass = renderPass;
|
||||||
|
renderPassBeginInfo.framebuffer = framebuffers[imageIndex];
|
||||||
|
renderPassBeginInfo.renderArea.offset = {0, 0};
|
||||||
|
renderPassBeginInfo.renderArea.extent = extent;
|
||||||
|
renderPassBeginInfo.clearValueCount = clearValues.size();
|
||||||
|
renderPassBeginInfo.pClearValues = clearValues.data();
|
||||||
|
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::EndFrameBuffer(const CommandBuffer& commandBuffer) const
|
||||||
|
{
|
||||||
|
vkCmdEndRenderPass(commandBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkSwapchainKHR SwapChain::GetHandle() const
|
||||||
|
{
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkRenderPass SwapChain::GetRenderPass() const
|
||||||
|
{
|
||||||
|
return renderPass;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkExtent2D SwapChain::GetExtent() const
|
||||||
|
{
|
||||||
|
return extent;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkFramebuffer SwapChain::GetFramebuffer() const
|
||||||
|
{
|
||||||
|
return framebuffers[imageIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SwapChain::BeginPresent(VkSemaphore signalSemaphore)
|
||||||
|
{
|
||||||
|
VkResult result = vkAcquireNextImageKHR(instance.GetDevice(), handle, UINT64_MAX, signalSemaphore, VK_NULL_HANDLE, &imageIndex);
|
||||||
|
if (result == VK_ERROR_OUT_OF_DATE_KHR)
|
||||||
|
{
|
||||||
|
Recreate();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::EndPresent(VkQueue presentQueue, VkSemaphore* waitSemaphore, bool framebufferResized)
|
||||||
|
{
|
||||||
|
VkPresentInfoKHR presentInfo{};
|
||||||
|
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||||
|
presentInfo.waitSemaphoreCount = 1;
|
||||||
|
presentInfo.pWaitSemaphores = waitSemaphore;
|
||||||
|
presentInfo.swapchainCount = 1;
|
||||||
|
presentInfo.pSwapchains = &handle;
|
||||||
|
presentInfo.pImageIndices = &imageIndex;
|
||||||
|
presentInfo.pResults = nullptr;
|
||||||
|
|
||||||
|
VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo);
|
||||||
|
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized)
|
||||||
|
{
|
||||||
|
Recreate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::Recreate()
|
||||||
|
{
|
||||||
|
int width = 0;
|
||||||
|
int height = 0;
|
||||||
glfwGetFramebufferSize(instance.GetWindow(), &width, &height);
|
glfwGetFramebufferSize(instance.GetWindow(), &width, &height);
|
||||||
glfwWaitEvents();
|
while (width == 0 || height == 0)
|
||||||
}
|
|
||||||
|
|
||||||
vkDeviceWaitIdle(instance.GetDevice());
|
|
||||||
|
|
||||||
Destroy();
|
|
||||||
|
|
||||||
Initialize();
|
|
||||||
InitializeImageViews();
|
|
||||||
InitializeDepthBuffer();
|
|
||||||
InitializeFramebuffers();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::Initialize()
|
|
||||||
{
|
|
||||||
SwapChainSupportDetails swapChainSupport{instance.GetSurface(), instance.GetPhysicalDevice()};
|
|
||||||
|
|
||||||
VkSurfaceFormatKHR format = SelectSwapSurfaceFormat(swapChainSupport.formats);
|
|
||||||
VkPresentModeKHR presentMode = SelectSwapPresentMode(swapChainSupport.presentModes);
|
|
||||||
extent = SelectSwapExtent(instance.GetWindow(), swapChainSupport.capabilities);
|
|
||||||
imageFormat = format.format;
|
|
||||||
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
|
||||||
if (swapChainSupport.capabilities.maxImageCount != 0)
|
|
||||||
{
|
|
||||||
imageCount = std::min(imageCount, swapChainSupport.capabilities.maxImageCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
QueueFamiliesQuery queueFamilies{instance.GetSurface(), instance.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 = instance.GetSurface();
|
|
||||||
createInfo.minImageCount = imageCount;
|
|
||||||
createInfo.imageFormat = format.format;
|
|
||||||
createInfo.imageColorSpace = format.colorSpace;
|
|
||||||
createInfo.imageExtent = extent;
|
|
||||||
createInfo.imageArrayLayers = 1;
|
|
||||||
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|
||||||
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
|
||||||
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
||||||
createInfo.presentMode = presentMode;
|
|
||||||
createInfo.clipped = VK_TRUE;
|
|
||||||
createInfo.oldSwapchain = VK_NULL_HANDLE;
|
|
||||||
if (queueFamilies.graphicsFamily != queueFamilies.presentFamily)
|
|
||||||
{
|
|
||||||
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
|
||||||
createInfo.queueFamilyIndexCount = 2;
|
|
||||||
createInfo.pQueueFamilyIndices = queueFamilyIndices.data();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
createInfo.queueFamilyIndexCount = 0;
|
|
||||||
createInfo.pQueueFamilyIndices = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateSwapchainKHR(instance.GetDevice(), &createInfo, nullptr, &handle), "Failed to initialize the swapchain");
|
|
||||||
|
|
||||||
vkGetSwapchainImagesKHR(instance.GetDevice(), handle, &imageCount, nullptr);
|
|
||||||
images.resize(imageCount);
|
|
||||||
vkGetSwapchainImagesKHR(instance.GetDevice(), handle, &imageCount, images.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::InitializeImageViews()
|
|
||||||
{
|
|
||||||
imageViews.resize(images.size());
|
|
||||||
for (size_t i = 0; i < images.size(); i++)
|
|
||||||
{
|
|
||||||
imageViews[i] = Image::InitializeImageView(instance, images[i], imageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::InitializeDepthBuffer()
|
|
||||||
{
|
|
||||||
depthImage = std::make_unique<Texture2D>(instance, extent.width, extent.height, Texture2D::Type::Static, Texture2D::Format::Depth);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::InitializeRenderPass()
|
|
||||||
{
|
|
||||||
VkAttachmentDescription colorAttachment{};
|
|
||||||
colorAttachment.format = imageFormat;
|
|
||||||
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
||||||
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
||||||
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
||||||
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
||||||
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
|
||||||
|
|
||||||
VkAttachmentDescription depthAttachment{};
|
|
||||||
depthAttachment.format = Image::SelectDepthFormat(instance);
|
|
||||||
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
||||||
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
||||||
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
||||||
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
||||||
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkAttachmentReference colorAttachmentRef{};
|
|
||||||
colorAttachmentRef.attachment = 0;
|
|
||||||
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkAttachmentReference depthAttachmentRef{};
|
|
||||||
depthAttachmentRef.attachment = 1;
|
|
||||||
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkSubpassDescription subpass{};
|
|
||||||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
||||||
subpass.colorAttachmentCount = 1;
|
|
||||||
subpass.pColorAttachments = &colorAttachmentRef;
|
|
||||||
subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
|
||||||
|
|
||||||
VkSubpassDependency dependency{};
|
|
||||||
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
|
||||||
dependency.dstSubpass = 0;
|
|
||||||
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
|
||||||
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
|
||||||
dependency.srcAccessMask = 0;
|
|
||||||
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
|
||||||
|
|
||||||
std::vector<VkAttachmentDescription> attachments{colorAttachment, depthAttachment};
|
|
||||||
VkRenderPassCreateInfo renderPassCreateInfo{};
|
|
||||||
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
||||||
renderPassCreateInfo.attachmentCount = attachments.size();
|
|
||||||
renderPassCreateInfo.pAttachments = attachments.data();
|
|
||||||
renderPassCreateInfo.subpassCount = 1;
|
|
||||||
renderPassCreateInfo.pSubpasses = &subpass;
|
|
||||||
renderPassCreateInfo.dependencyCount = 1;
|
|
||||||
renderPassCreateInfo.pDependencies = &dependency;
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateRenderPass(instance.GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "Failed to initialze render pass");
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::InitializeFramebuffers()
|
|
||||||
{
|
|
||||||
framebuffers.resize(images.size());
|
|
||||||
|
|
||||||
for (size_t i = 0; i < imageViews.size(); ++i)
|
|
||||||
{
|
|
||||||
std::vector<VkImageView> attachments{imageViews[i], depthImage->GetImageView()};
|
|
||||||
|
|
||||||
VkFramebufferCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
|
||||||
createInfo.renderPass = renderPass;
|
|
||||||
createInfo.attachmentCount = attachments.size();
|
|
||||||
createInfo.pAttachments = attachments.data();
|
|
||||||
createInfo.width = extent.width;
|
|
||||||
createInfo.height = extent.height;
|
|
||||||
createInfo.layers = 1;
|
|
||||||
|
|
||||||
CP_VK_ASSERT(vkCreateFramebuffer(instance.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "Failed to initialize swap chain framebuffer");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwapChain::Destroy()
|
|
||||||
{
|
|
||||||
for (auto&& framebuffer : framebuffers)
|
|
||||||
{
|
|
||||||
vkDestroyFramebuffer(instance.GetDevice(), framebuffer, nullptr);
|
|
||||||
}
|
|
||||||
for (auto&& swapChainImageView : imageViews)
|
|
||||||
{
|
|
||||||
vkDestroyImageView(instance.GetDevice(), swapChainImageView, nullptr);
|
|
||||||
}
|
|
||||||
vkDestroySwapchainKHR(instance.GetDevice(), handle, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkSurfaceFormatKHR SwapChain::SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
|
|
||||||
{
|
|
||||||
for (auto&& availableFormat : availableFormats)
|
|
||||||
{
|
|
||||||
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
|
|
||||||
{
|
{
|
||||||
return availableFormat;
|
glfwGetFramebufferSize(instance.GetWindow(), &width, &height);
|
||||||
|
glfwWaitEvents();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return availableFormats[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
VkPresentModeKHR SwapChain::SelectSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes)
|
vkDeviceWaitIdle(instance.GetDevice());
|
||||||
{
|
|
||||||
return VK_PRESENT_MODE_FIFO_KHR;
|
Destroy();
|
||||||
for (auto&& availablePresentMode : availablePresentModes)
|
|
||||||
|
Initialize();
|
||||||
|
InitializeImageViews();
|
||||||
|
InitializeDepthBuffer();
|
||||||
|
InitializeFramebuffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::Initialize()
|
||||||
{
|
{
|
||||||
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
|
SwapChainSupportDetails swapChainSupport{instance.GetSurface(), instance.GetPhysicalDevice()};
|
||||||
|
|
||||||
|
VkSurfaceFormatKHR format = SelectSwapSurfaceFormat(swapChainSupport.formats);
|
||||||
|
VkPresentModeKHR presentMode = SelectSwapPresentMode(swapChainSupport.presentModes);
|
||||||
|
extent = SelectSwapExtent(instance.GetWindow(), swapChainSupport.capabilities);
|
||||||
|
imageFormat = format.format;
|
||||||
|
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
||||||
|
if (swapChainSupport.capabilities.maxImageCount != 0)
|
||||||
{
|
{
|
||||||
return availablePresentMode;
|
imageCount = std::min(imageCount, swapChainSupport.capabilities.maxImageCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
QueueFamiliesQuery queueFamilies{instance.GetSurface(), instance.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 = instance.GetSurface();
|
||||||
|
createInfo.minImageCount = imageCount;
|
||||||
|
createInfo.imageFormat = format.format;
|
||||||
|
createInfo.imageColorSpace = format.colorSpace;
|
||||||
|
createInfo.imageExtent = extent;
|
||||||
|
createInfo.imageArrayLayers = 1;
|
||||||
|
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||||
|
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
||||||
|
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||||
|
createInfo.presentMode = presentMode;
|
||||||
|
createInfo.clipped = VK_TRUE;
|
||||||
|
createInfo.oldSwapchain = VK_NULL_HANDLE;
|
||||||
|
if (queueFamilies.graphicsFamily != queueFamilies.presentFamily)
|
||||||
|
{
|
||||||
|
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||||
|
createInfo.queueFamilyIndexCount = 2;
|
||||||
|
createInfo.pQueueFamilyIndices = queueFamilyIndices.data();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
createInfo.queueFamilyIndexCount = 0;
|
||||||
|
createInfo.pQueueFamilyIndices = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateSwapchainKHR(instance.GetDevice(), &createInfo, nullptr, &handle), "Failed to initialize the swapchain");
|
||||||
|
|
||||||
|
vkGetSwapchainImagesKHR(instance.GetDevice(), handle, &imageCount, nullptr);
|
||||||
|
images.resize(imageCount);
|
||||||
|
vkGetSwapchainImagesKHR(instance.GetDevice(), handle, &imageCount, images.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::InitializeImageViews()
|
||||||
|
{
|
||||||
|
imageViews.resize(images.size());
|
||||||
|
for (size_t i = 0; i < images.size(); i++)
|
||||||
|
{
|
||||||
|
imageViews[i] = Image::InitializeImageView(instance, images[i], imageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VK_PRESENT_MODE_FIFO_KHR is guaranteed to be present
|
void SwapChain::InitializeDepthBuffer()
|
||||||
return VK_PRESENT_MODE_FIFO_KHR;
|
{
|
||||||
}
|
depthImage = std::make_unique<Texture2D>(instance, extent.width, extent.height, Texture2D::Type::Static, Texture2D::Format::Depth);
|
||||||
|
}
|
||||||
VkExtent2D SwapChain::SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabilitiesKHR& capabilities)
|
|
||||||
{
|
void SwapChain::InitializeRenderPass()
|
||||||
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
|
{
|
||||||
return capabilities.currentExtent;
|
VkAttachmentDescription colorAttachment{};
|
||||||
|
colorAttachment.format = imageFormat;
|
||||||
int width, height;
|
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
glfwGetFramebufferSize(window, &width, &height);
|
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||||
|
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||||
VkExtent2D extent{width, height};
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||||
extent.width = std::clamp(extent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||||
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
return extent;
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||||
|
|
||||||
|
VkAttachmentDescription depthAttachment{};
|
||||||
|
depthAttachment.format = Image::SelectDepthFormat(instance);
|
||||||
|
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||||
|
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||||
|
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||||
|
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||||
|
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||||
|
|
||||||
|
VkAttachmentReference colorAttachmentRef{};
|
||||||
|
colorAttachmentRef.attachment = 0;
|
||||||
|
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||||
|
|
||||||
|
VkAttachmentReference depthAttachmentRef{};
|
||||||
|
depthAttachmentRef.attachment = 1;
|
||||||
|
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||||
|
|
||||||
|
VkSubpassDescription subpass{};
|
||||||
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||||
|
subpass.colorAttachmentCount = 1;
|
||||||
|
subpass.pColorAttachments = &colorAttachmentRef;
|
||||||
|
subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
||||||
|
|
||||||
|
VkSubpassDependency dependency{};
|
||||||
|
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
||||||
|
dependency.dstSubpass = 0;
|
||||||
|
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
||||||
|
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
||||||
|
dependency.srcAccessMask = 0;
|
||||||
|
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||||
|
|
||||||
|
std::vector<VkAttachmentDescription> attachments{colorAttachment, depthAttachment};
|
||||||
|
VkRenderPassCreateInfo renderPassCreateInfo{};
|
||||||
|
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||||||
|
renderPassCreateInfo.attachmentCount = attachments.size();
|
||||||
|
renderPassCreateInfo.pAttachments = attachments.data();
|
||||||
|
renderPassCreateInfo.subpassCount = 1;
|
||||||
|
renderPassCreateInfo.pSubpasses = &subpass;
|
||||||
|
renderPassCreateInfo.dependencyCount = 1;
|
||||||
|
renderPassCreateInfo.pDependencies = &dependency;
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateRenderPass(instance.GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "Failed to initialze render pass");
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::InitializeFramebuffers()
|
||||||
|
{
|
||||||
|
framebuffers.resize(images.size());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < imageViews.size(); ++i)
|
||||||
|
{
|
||||||
|
std::vector<VkImageView> attachments{imageViews[i], depthImage->GetImageView()};
|
||||||
|
|
||||||
|
VkFramebufferCreateInfo createInfo{};
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||||||
|
createInfo.renderPass = renderPass;
|
||||||
|
createInfo.attachmentCount = attachments.size();
|
||||||
|
createInfo.pAttachments = attachments.data();
|
||||||
|
createInfo.width = extent.width;
|
||||||
|
createInfo.height = extent.height;
|
||||||
|
createInfo.layers = 1;
|
||||||
|
|
||||||
|
CP_VK_ASSERT(vkCreateFramebuffer(instance.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "Failed to initialize swap chain framebuffer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwapChain::Destroy()
|
||||||
|
{
|
||||||
|
for (auto&& framebuffer : framebuffers)
|
||||||
|
{
|
||||||
|
vkDestroyFramebuffer(instance.GetDevice(), framebuffer, nullptr);
|
||||||
|
}
|
||||||
|
for (auto&& swapChainImageView : imageViews)
|
||||||
|
{
|
||||||
|
vkDestroyImageView(instance.GetDevice(), swapChainImageView, nullptr);
|
||||||
|
}
|
||||||
|
vkDestroySwapchainKHR(instance.GetDevice(), handle, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkSurfaceFormatKHR SwapChain::SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
|
||||||
|
{
|
||||||
|
for (auto&& availableFormat : availableFormats)
|
||||||
|
{
|
||||||
|
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
|
||||||
|
{
|
||||||
|
return availableFormat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return availableFormats[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
VkPresentModeKHR SwapChain::SelectSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes)
|
||||||
|
{
|
||||||
|
return VK_PRESENT_MODE_FIFO_KHR;
|
||||||
|
for (auto&& availablePresentMode : availablePresentModes)
|
||||||
|
{
|
||||||
|
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
|
||||||
|
{
|
||||||
|
return availablePresentMode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// VK_PRESENT_MODE_FIFO_KHR is guaranteed to be present
|
||||||
|
return VK_PRESENT_MODE_FIFO_KHR;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkExtent2D SwapChain::SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabilitiesKHR& capabilities)
|
||||||
|
{
|
||||||
|
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
|
||||||
|
return capabilities.currentExtent;
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
|
|
||||||
|
VkExtent2D extent{width, height};
|
||||||
|
extent.width = std::clamp(extent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
|
||||||
|
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
||||||
|
return extent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+51
-48
@@ -6,60 +6,63 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
class Instance;
|
namespace Copium
|
||||||
class CommandBuffer;
|
|
||||||
class Texture2D;
|
|
||||||
|
|
||||||
struct SwapChainSupportDetails
|
|
||||||
{
|
{
|
||||||
VkSurfaceCapabilitiesKHR capabilities;
|
class Instance;
|
||||||
std::vector<VkSurfaceFormatKHR> formats;
|
class CommandBuffer;
|
||||||
std::vector<VkPresentModeKHR> presentModes;
|
class Texture2D;
|
||||||
|
|
||||||
SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice);
|
struct SwapChainSupportDetails
|
||||||
bool Valid();
|
{
|
||||||
};
|
VkSurfaceCapabilitiesKHR capabilities;
|
||||||
|
std::vector<VkSurfaceFormatKHR> formats;
|
||||||
|
std::vector<VkPresentModeKHR> presentModes;
|
||||||
|
|
||||||
class SwapChain final
|
SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice);
|
||||||
{
|
bool Valid();
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(SwapChain);
|
};
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
VkSwapchainKHR handle;
|
class SwapChain final
|
||||||
VkRenderPass renderPass;
|
{
|
||||||
VkFormat imageFormat;
|
CP_DELETE_COPY_AND_MOVE_CTOR(SwapChain);
|
||||||
VkExtent2D extent;
|
private:
|
||||||
std::unique_ptr<Texture2D> depthImage;
|
Instance& instance;
|
||||||
std::vector<VkImageView> imageViews;
|
|
||||||
std::vector<VkImage> images;
|
|
||||||
std::vector<VkFramebuffer> framebuffers;
|
|
||||||
uint32_t imageIndex;
|
|
||||||
|
|
||||||
public:
|
VkSwapchainKHR handle;
|
||||||
SwapChain(Instance& instance);
|
VkRenderPass renderPass;
|
||||||
~SwapChain();
|
VkFormat imageFormat;
|
||||||
|
VkExtent2D extent;
|
||||||
|
std::unique_ptr<Texture2D> depthImage;
|
||||||
|
std::vector<VkImageView> imageViews;
|
||||||
|
std::vector<VkImage> images;
|
||||||
|
std::vector<VkFramebuffer> framebuffers;
|
||||||
|
uint32_t imageIndex;
|
||||||
|
|
||||||
void BeginFrameBuffer(const CommandBuffer& commandBuffer) const;
|
public:
|
||||||
void EndFrameBuffer(const CommandBuffer& commandBuffer) const;
|
SwapChain(Instance& instance);
|
||||||
VkSwapchainKHR GetHandle() const;
|
~SwapChain();
|
||||||
VkRenderPass GetRenderPass() const;
|
|
||||||
VkExtent2D GetExtent() const;
|
void BeginFrameBuffer(const CommandBuffer& commandBuffer) const;
|
||||||
VkFramebuffer GetFramebuffer() const;
|
void EndFrameBuffer(const CommandBuffer& commandBuffer) const;
|
||||||
bool BeginPresent(VkSemaphore signalSemaphore);
|
VkSwapchainKHR GetHandle() const;
|
||||||
void EndPresent(VkQueue presentQueue, VkSemaphore* waitSemaphore, bool framebufferResized);
|
VkRenderPass GetRenderPass() const;
|
||||||
void Recreate();
|
VkExtent2D GetExtent() const;
|
||||||
|
VkFramebuffer GetFramebuffer() const;
|
||||||
|
bool BeginPresent(VkSemaphore signalSemaphore);
|
||||||
|
void EndPresent(VkQueue presentQueue, VkSemaphore* waitSemaphore, bool framebufferResized);
|
||||||
|
void Recreate();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Initialize();
|
void Initialize();
|
||||||
void InitializeImageViews();
|
void InitializeImageViews();
|
||||||
void InitializeDepthBuffer();
|
void InitializeDepthBuffer();
|
||||||
void InitializeRenderPass();
|
void InitializeRenderPass();
|
||||||
void InitializeFramebuffers();
|
void InitializeFramebuffers();
|
||||||
void Destroy();
|
void Destroy();
|
||||||
|
|
||||||
VkSurfaceFormatKHR SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
|
VkSurfaceFormatKHR SelectSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
|
||||||
VkPresentModeKHR SelectSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
|
VkPresentModeKHR SelectSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
|
||||||
VkExtent2D SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabilitiesKHR& capabilities);
|
VkExtent2D SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabilitiesKHR& capabilities);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|||||||
+140
-137
@@ -3,148 +3,151 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include <stb/stb_image.h>
|
#include <stb/stb_image.h>
|
||||||
|
|
||||||
Texture2D::Texture2D(Instance& instance, const std::string& filename)
|
namespace Copium
|
||||||
: instance{instance}, type{Type::Static}, format{Format::Image}
|
|
||||||
{
|
{
|
||||||
InitializeTextureImage(filename);
|
Texture2D::Texture2D(Instance& instance, const std::string& filename)
|
||||||
InitializeSampler();
|
: instance{instance}, type{Type::Static}, format{Format::Image}
|
||||||
}
|
|
||||||
|
|
||||||
Texture2D::Texture2D(Instance& instance, int width, int height, Type type, Format format)
|
|
||||||
: instance{instance}, type{type}, format{format}
|
|
||||||
{
|
|
||||||
InitializeTexture(width, height);
|
|
||||||
InitializeSampler();
|
|
||||||
}
|
|
||||||
|
|
||||||
Texture2D::~Texture2D()
|
|
||||||
{
|
|
||||||
for(auto&& image : images)
|
|
||||||
vkDestroyImage(instance.GetDevice(), image, nullptr);
|
|
||||||
for(auto&& imageMemory : imageMemories)
|
|
||||||
vkFreeMemory(instance.GetDevice(), imageMemory, nullptr);
|
|
||||||
for(auto&& imageView : imageViews)
|
|
||||||
vkDestroyImageView(instance.GetDevice(), imageView, nullptr);
|
|
||||||
vkDestroySampler(instance.GetDevice(), sampler, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDescriptorImageInfo Texture2D::GetDescriptorImageInfo(int index) const
|
|
||||||
{
|
|
||||||
VkDescriptorImageInfo imageInfo{};
|
|
||||||
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
||||||
imageInfo.sampler = sampler;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case Type::Static:
|
|
||||||
imageInfo.imageView = imageViews.front();
|
|
||||||
break;
|
|
||||||
case Type::Dynamic:
|
|
||||||
CP_ASSERT(index >= 0 && index < imageViews.size(), "GetDescriptorImageInfo : index out of bound for dynamic texture");
|
|
||||||
imageInfo.imageView = imageViews[index];
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
CP_ABORT("GetDescriptorImageInfo : Unreachable switch case");
|
|
||||||
}
|
|
||||||
|
|
||||||
return imageInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkImageView Texture2D::GetImageView() const
|
|
||||||
{
|
|
||||||
CP_ASSERT(type == Type::Static, "GetImageView : Texture2D is not static");
|
|
||||||
return imageViews.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
VkImageView Texture2D::GetImageView(int index)
|
|
||||||
{
|
|
||||||
CP_ASSERT(type == Type::Dynamic && index >= 0 && index < imageViews.size(), "GetImageView : Texture2D is not dynamic or index out of bound for SystemTexture");
|
|
||||||
return imageViews[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
void Texture2D::InitializeTextureImage(const std::string& filename)
|
|
||||||
{
|
|
||||||
int texWidth;
|
|
||||||
int texHeight;
|
|
||||||
int texChannels;
|
|
||||||
stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
|
||||||
|
|
||||||
CP_ASSERT(pixels, "InitializeTextureImage : Failed to load texture image");
|
|
||||||
|
|
||||||
VkDeviceSize bufferSize = texWidth * texHeight * 4;
|
|
||||||
Buffer stagingBuffer{instance, 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, pixels, bufferSize);
|
|
||||||
stagingBuffer.Unmap();
|
|
||||||
stbi_image_free(pixels);
|
|
||||||
|
|
||||||
images.resize(1);
|
|
||||||
imageMemories.resize(1);
|
|
||||||
imageViews.resize(1);
|
|
||||||
Image::InitializeImage(instance, texWidth, texHeight, 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, &images.front(), &imageMemories.front());
|
|
||||||
Image::TransitionImageLayout(instance, images.front(), VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
|
||||||
Image::CopyBufferToImage(instance, stagingBuffer, images.front(), texWidth, texHeight);
|
|
||||||
Image::TransitionImageLayout(instance, images.front(), VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
||||||
imageViews[0] = Image::InitializeImageView(instance, images.front(), VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Texture2D::InitializeTexture(int width, int height)
|
|
||||||
{
|
|
||||||
int count = 1;
|
|
||||||
if (type == Type::Dynamic)
|
|
||||||
count = instance.GetMaxFramesInFlight();
|
|
||||||
images.resize(count);
|
|
||||||
imageMemories.resize(count);
|
|
||||||
imageViews.resize(count);
|
|
||||||
for (size_t i = 0; i < images.size(); i++)
|
|
||||||
{
|
{
|
||||||
switch (format)
|
InitializeTextureImage(filename);
|
||||||
{
|
InitializeSampler();
|
||||||
case Format::Color:
|
}
|
||||||
{
|
|
||||||
Image::InitializeImage(instance, 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]);
|
Texture2D::Texture2D(Instance& instance, int width, int height, Type type, Format format)
|
||||||
// Image::TransitionImageLayout(instance, images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
: instance{instance}, type{type}, format{format}
|
||||||
imageViews[i] = Image::InitializeImageView(instance, images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
|
{
|
||||||
|
InitializeTexture(width, height);
|
||||||
|
InitializeSampler();
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture2D::~Texture2D()
|
||||||
|
{
|
||||||
|
for (auto&& image : images)
|
||||||
|
vkDestroyImage(instance.GetDevice(), image, nullptr);
|
||||||
|
for (auto&& imageMemory : imageMemories)
|
||||||
|
vkFreeMemory(instance.GetDevice(), imageMemory, nullptr);
|
||||||
|
for (auto&& imageView : imageViews)
|
||||||
|
vkDestroyImageView(instance.GetDevice(), imageView, nullptr);
|
||||||
|
vkDestroySampler(instance.GetDevice(), sampler, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkDescriptorImageInfo Texture2D::GetDescriptorImageInfo(int index) const
|
||||||
|
{
|
||||||
|
VkDescriptorImageInfo imageInfo{};
|
||||||
|
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
imageInfo.sampler = sampler;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case Type::Static:
|
||||||
|
imageInfo.imageView = imageViews.front();
|
||||||
break;
|
break;
|
||||||
}
|
case Type::Dynamic:
|
||||||
case Format::Depth:
|
CP_ASSERT(index >= 0 && index < imageViews.size(), "GetDescriptorImageInfo : index out of bound for dynamic texture");
|
||||||
{
|
imageInfo.imageView = imageViews[index];
|
||||||
VkFormat depthFormat = Image::SelectDepthFormat(instance);
|
|
||||||
Image::InitializeImage(instance, width, height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &images[i], &imageMemories[i]);
|
|
||||||
// Image::TransitionImageLayout(instance, images[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
|
||||||
imageViews[i] = Image::InitializeImageView(instance, images[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case Format::Image:
|
|
||||||
{
|
|
||||||
CP_ABORT("InitializeTexture : Image format currently not supported");
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
CP_ABORT("InitializeTexture : Unreachable switch case");
|
CP_ABORT("GetDescriptorImageInfo : Unreachable switch case");
|
||||||
|
}
|
||||||
|
|
||||||
|
return imageInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkImageView Texture2D::GetImageView() const
|
||||||
|
{
|
||||||
|
CP_ASSERT(type == Type::Static, "GetImageView : Texture2D is not static");
|
||||||
|
return imageViews.front();
|
||||||
|
}
|
||||||
|
|
||||||
|
VkImageView Texture2D::GetImageView(int index)
|
||||||
|
{
|
||||||
|
CP_ASSERT(type == Type::Dynamic && index >= 0 && index < imageViews.size(), "GetImageView : Texture2D is not dynamic or index out of bound for SystemTexture");
|
||||||
|
return imageViews[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
void Texture2D::InitializeTextureImage(const std::string& filename)
|
||||||
|
{
|
||||||
|
int texWidth;
|
||||||
|
int texHeight;
|
||||||
|
int texChannels;
|
||||||
|
stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
||||||
|
|
||||||
|
CP_ASSERT(pixels, "InitializeTextureImage : Failed to load texture image");
|
||||||
|
|
||||||
|
VkDeviceSize bufferSize = texWidth * texHeight * 4;
|
||||||
|
Buffer stagingBuffer{instance, 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, pixels, bufferSize);
|
||||||
|
stagingBuffer.Unmap();
|
||||||
|
stbi_image_free(pixels);
|
||||||
|
|
||||||
|
images.resize(1);
|
||||||
|
imageMemories.resize(1);
|
||||||
|
imageViews.resize(1);
|
||||||
|
Image::InitializeImage(instance, texWidth, texHeight, 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, &images.front(), &imageMemories.front());
|
||||||
|
Image::TransitionImageLayout(instance, images.front(), VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
||||||
|
Image::CopyBufferToImage(instance, stagingBuffer, images.front(), texWidth, texHeight);
|
||||||
|
Image::TransitionImageLayout(instance, images.front(), VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
imageViews[0] = Image::InitializeImageView(instance, images.front(), VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Texture2D::InitializeTexture(int width, int height)
|
||||||
|
{
|
||||||
|
int count = 1;
|
||||||
|
if (type == Type::Dynamic)
|
||||||
|
count = instance.GetMaxFramesInFlight();
|
||||||
|
images.resize(count);
|
||||||
|
imageMemories.resize(count);
|
||||||
|
imageViews.resize(count);
|
||||||
|
for (size_t i = 0; i < images.size(); i++)
|
||||||
|
{
|
||||||
|
switch (format)
|
||||||
|
{
|
||||||
|
case Format::Color:
|
||||||
|
{
|
||||||
|
Image::InitializeImage(instance, width, height, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &images[i], &imageMemories[i]);
|
||||||
|
// Image::TransitionImageLayout(instance, images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
imageViews[i] = Image::InitializeImageView(instance, images[i], VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Format::Depth:
|
||||||
|
{
|
||||||
|
VkFormat depthFormat = Image::SelectDepthFormat(instance);
|
||||||
|
Image::InitializeImage(instance, width, height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &images[i], &imageMemories[i]);
|
||||||
|
// Image::TransitionImageLayout(instance, images[i], depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
||||||
|
imageViews[i] = Image::InitializeImageView(instance, images[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Format::Image:
|
||||||
|
{
|
||||||
|
CP_ABORT("InitializeTexture : Image format currently not supported");
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
CP_ABORT("InitializeTexture : Unreachable switch case");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
void Texture2D::InitializeSampler()
|
||||||
void Texture2D::InitializeSampler()
|
{
|
||||||
{
|
VkPhysicalDeviceProperties properties{};
|
||||||
VkPhysicalDeviceProperties properties{};
|
vkGetPhysicalDeviceProperties(instance.GetPhysicalDevice(), &properties);
|
||||||
vkGetPhysicalDeviceProperties(instance.GetPhysicalDevice(), &properties);
|
|
||||||
|
VkSamplerCreateInfo createInfo{};
|
||||||
VkSamplerCreateInfo createInfo{};
|
createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
createInfo.magFilter = VK_FILTER_LINEAR;
|
||||||
createInfo.magFilter = VK_FILTER_LINEAR;
|
createInfo.minFilter = VK_FILTER_LINEAR;
|
||||||
createInfo.minFilter = VK_FILTER_LINEAR;
|
createInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||||
createInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
createInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||||
createInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||||
createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
createInfo.anisotropyEnable = VK_TRUE;
|
||||||
createInfo.anisotropyEnable = VK_TRUE;
|
createInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
|
||||||
createInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
|
createInfo.unnormalizedCoordinates = VK_FALSE;
|
||||||
createInfo.unnormalizedCoordinates = VK_FALSE;
|
createInfo.compareEnable = VK_FALSE;
|
||||||
createInfo.compareEnable = VK_FALSE;
|
createInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
||||||
createInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
createInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||||
createInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
createInfo.mipLodBias = 0.0f;
|
||||||
createInfo.mipLodBias = 0.0f;
|
createInfo.minLod = 0.0f;
|
||||||
createInfo.minLod = 0.0f;
|
createInfo.maxLod = 0.0f;
|
||||||
createInfo.maxLod = 0.0f;
|
|
||||||
|
CP_VK_ASSERT(vkCreateSampler(instance.GetDevice(), &createInfo, nullptr, &sampler), "InitializeSampler : Failed to initialize texture sampler");
|
||||||
CP_VK_ASSERT(vkCreateSampler(instance.GetDevice(), &createInfo, nullptr, &sampler), "InitializeSampler : Failed to initialize texture sampler");
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-34
@@ -6,40 +6,42 @@
|
|||||||
#include "Image.h"
|
#include "Image.h"
|
||||||
#include "Instance.h"
|
#include "Instance.h"
|
||||||
|
|
||||||
// TODO: Separate Texture2D and Framebuffer Attachments
|
namespace Copium
|
||||||
class Texture2D
|
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Texture2D);
|
// TODO: Separate Texture2D and Framebuffer Attachments
|
||||||
public:
|
class Texture2D
|
||||||
enum class Type
|
|
||||||
{
|
{
|
||||||
Static, Dynamic
|
CP_DELETE_COPY_AND_MOVE_CTOR(Texture2D);
|
||||||
|
public:
|
||||||
|
enum class Type
|
||||||
|
{
|
||||||
|
Static, Dynamic
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Format
|
||||||
|
{
|
||||||
|
Image, Color, Depth
|
||||||
|
};
|
||||||
|
private:
|
||||||
|
Instance& instance;
|
||||||
|
|
||||||
|
std::vector<VkImage> images;
|
||||||
|
std::vector<VkDeviceMemory> imageMemories;
|
||||||
|
std::vector<VkImageView> imageViews;
|
||||||
|
VkSampler sampler;
|
||||||
|
Type type;
|
||||||
|
Format format;
|
||||||
|
public:
|
||||||
|
Texture2D(Instance& instance, const std::string& filename);
|
||||||
|
Texture2D(Instance& instance, int width, int height, Type type, Format format);
|
||||||
|
~Texture2D();
|
||||||
|
|
||||||
|
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const;
|
||||||
|
VkImageView GetImageView() const;
|
||||||
|
VkImageView GetImageView(int index);
|
||||||
|
private:
|
||||||
|
void InitializeTextureImage(const std::string& filename);
|
||||||
|
void InitializeTexture(int width, int height);
|
||||||
|
void InitializeSampler();
|
||||||
};
|
};
|
||||||
|
}
|
||||||
enum class Format
|
|
||||||
{
|
|
||||||
Image, Color, Depth
|
|
||||||
};
|
|
||||||
private:
|
|
||||||
Instance& instance;
|
|
||||||
|
|
||||||
std::vector<VkImage> images;
|
|
||||||
std::vector<VkDeviceMemory> imageMemories;
|
|
||||||
std::vector<VkImageView> imageViews;
|
|
||||||
VkSampler sampler;
|
|
||||||
Type type;
|
|
||||||
Format format;
|
|
||||||
public:
|
|
||||||
Texture2D(Instance& instance, const std::string& filename);
|
|
||||||
Texture2D(Instance& instance, int width, int height, Type type, Format format);
|
|
||||||
~Texture2D();
|
|
||||||
|
|
||||||
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const;
|
|
||||||
VkImageView GetImageView() const;
|
|
||||||
VkImageView GetImageView(int index);
|
|
||||||
private:
|
|
||||||
void InitializeTextureImage(const std::string& filename);
|
|
||||||
void InitializeTexture(int width, int height);
|
|
||||||
void InitializeSampler();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|||||||
+19
-16
@@ -2,22 +2,25 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
class Timer
|
namespace Copium
|
||||||
{
|
{
|
||||||
private:
|
class Timer
|
||||||
std::chrono::time_point<std::chrono::steady_clock> startTime;
|
{
|
||||||
public:
|
private:
|
||||||
Timer()
|
std::chrono::time_point<std::chrono::steady_clock> startTime;
|
||||||
: startTime{std::chrono::steady_clock::now()}
|
public:
|
||||||
{}
|
Timer()
|
||||||
|
: startTime{std::chrono::steady_clock::now()}
|
||||||
|
{}
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
startTime = std::chrono::steady_clock::now();
|
startTime = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
double Elapsed()
|
double Elapsed()
|
||||||
{
|
{
|
||||||
return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count();
|
return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|||||||
+25
-22
@@ -4,28 +4,31 @@
|
|||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
|
||||||
class UniformBuffer : public Buffer
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
|
class UniformBuffer : public Buffer
|
||||||
|
|
||||||
public:
|
|
||||||
UniformBuffer(Instance& instance, VkDeviceSize size)
|
|
||||||
: Buffer{instance, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, instance.GetMaxFramesInFlight()}
|
|
||||||
{}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void Update(const T& t)
|
|
||||||
{
|
{
|
||||||
CP_ASSERT(sizeof(T) == Buffer::GetSize(), "Template size is not the same as buffer size %u != %u", sizeof(T), Buffer::GetSize());
|
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
|
||||||
Buffer::Update((void*)&t, instance.GetFlightIndex());
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const
|
public:
|
||||||
{
|
UniformBuffer(Instance& instance, VkDeviceSize size)
|
||||||
VkDescriptorBufferInfo bufferInfo{};
|
: Buffer{instance, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, instance.GetMaxFramesInFlight()}
|
||||||
bufferInfo.buffer = handle;
|
{}
|
||||||
bufferInfo.offset = (VkDeviceSize)index * size;
|
|
||||||
bufferInfo.range = size;
|
template <typename T>
|
||||||
return bufferInfo;
|
void Update(const T& t)
|
||||||
}
|
{
|
||||||
};
|
CP_ASSERT(sizeof(T) == Buffer::GetSize(), "Template size is not the same as buffer size %u != %u", sizeof(T), Buffer::GetSize());
|
||||||
|
Buffer::Update((void*)&t, instance.GetFlightIndex());
|
||||||
|
}
|
||||||
|
|
||||||
|
VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const
|
||||||
|
{
|
||||||
|
VkDescriptorBufferInfo bufferInfo{};
|
||||||
|
bufferInfo.buffer = handle;
|
||||||
|
bufferInfo.offset = (VkDeviceSize)index * size;
|
||||||
|
bufferInfo.range = size;
|
||||||
|
return bufferInfo;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
+16
-13
@@ -4,18 +4,21 @@
|
|||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
#include "VertexDescriptor.h"
|
#include "VertexDescriptor.h"
|
||||||
|
|
||||||
struct Vertex {
|
namespace Copium
|
||||||
glm::vec3 pos;
|
{
|
||||||
glm::vec3 color;
|
struct Vertex {
|
||||||
glm::vec2 texCoord;
|
glm::vec3 pos;
|
||||||
|
glm::vec3 color;
|
||||||
|
glm::vec2 texCoord;
|
||||||
|
|
||||||
static VertexDescriptor GetDescriptor()
|
static VertexDescriptor GetDescriptor()
|
||||||
{
|
{
|
||||||
VertexDescriptor descriptor{};
|
VertexDescriptor descriptor{};
|
||||||
descriptor.AddAttribute<Vertex>(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos));
|
descriptor.AddAttribute<Vertex>(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos));
|
||||||
descriptor.AddAttribute<Vertex>(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, color));
|
descriptor.AddAttribute<Vertex>(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, color));
|
||||||
descriptor.AddAttribute<Vertex>(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, texCoord));
|
descriptor.AddAttribute<Vertex>(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, texCoord));
|
||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
+27
-24
@@ -3,33 +3,36 @@
|
|||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
#include "VertexDescriptor.h"
|
#include "VertexDescriptor.h"
|
||||||
|
|
||||||
class VertexBuffer : public Buffer
|
namespace Copium
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(VertexBuffer);
|
class VertexBuffer : public Buffer
|
||||||
private:
|
|
||||||
std::vector<VkDeviceSize> bindingOffsets;
|
|
||||||
std::vector<VkDeviceSize> bindingSizes;
|
|
||||||
public:
|
|
||||||
VertexBuffer(Instance& instance, const VertexDescriptor& descriptor, int vertexCount)
|
|
||||||
: Buffer{instance, 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;
|
CP_DELETE_COPY_AND_MOVE_CTOR(VertexBuffer);
|
||||||
for (auto&& binding : descriptor.GetBindings())
|
private:
|
||||||
|
std::vector<VkDeviceSize> bindingOffsets;
|
||||||
|
std::vector<VkDeviceSize> bindingSizes;
|
||||||
|
public:
|
||||||
|
VertexBuffer(Instance& instance, const VertexDescriptor& descriptor, int vertexCount)
|
||||||
|
: Buffer{instance, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, descriptor.GetVertexSize() * vertexCount, 1}
|
||||||
{
|
{
|
||||||
bindingOffsets.emplace_back(offset);
|
VkDeviceSize offset = 0;
|
||||||
bindingSizes.emplace_back(binding.stride * vertexCount);
|
for (auto&& binding : descriptor.GetBindings())
|
||||||
offset += binding.stride * vertexCount;
|
{
|
||||||
|
bindingOffsets.emplace_back(offset);
|
||||||
|
bindingSizes.emplace_back(binding.stride * vertexCount);
|
||||||
|
offset += binding.stride * vertexCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Bind(const CommandBuffer& commandBuffer) override
|
void Bind(const CommandBuffer& commandBuffer) override
|
||||||
{
|
{
|
||||||
std::vector<VkBuffer> buffers{bindingOffsets.size(), handle};
|
std::vector<VkBuffer> buffers{bindingOffsets.size(), handle};
|
||||||
vkCmdBindVertexBuffers(commandBuffer, 0, bindingOffsets.size(), buffers.data(), bindingOffsets.data());
|
vkCmdBindVertexBuffers(commandBuffer, 0, bindingOffsets.size(), buffers.data(), bindingOffsets.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(uint32_t binding, void* data)
|
void Update(uint32_t binding, void* data)
|
||||||
{
|
{
|
||||||
UpdateStaging(data, bindingOffsets[binding], bindingSizes[binding]);
|
UpdateStaging(data, bindingOffsets[binding], bindingSizes[binding]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,59 +2,61 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
|
namespace Copium
|
||||||
class VertexDescriptor
|
|
||||||
{
|
{
|
||||||
private:
|
class VertexDescriptor
|
||||||
uint32_t bindingIndex = 0;
|
|
||||||
std::vector<VkVertexInputBindingDescription> bindings;
|
|
||||||
std::vector<VkVertexInputAttributeDescription> attributes;
|
|
||||||
|
|
||||||
public:
|
|
||||||
template <typename T>
|
|
||||||
void AddAttribute(uint32_t binding, uint32_t location, VkFormat format, uint32_t offset)
|
|
||||||
{
|
{
|
||||||
CP_ASSERT(binding <= bindings.size(), "Attribute binding must less than or be equal to the amount of current bindings");
|
private:
|
||||||
|
uint32_t bindingIndex = 0;
|
||||||
|
std::vector<VkVertexInputBindingDescription> bindings;
|
||||||
|
std::vector<VkVertexInputAttributeDescription> attributes;
|
||||||
|
|
||||||
if (binding == bindings.size())
|
public:
|
||||||
AddLayout(binding, sizeof(T));
|
template <typename T>
|
||||||
|
void AddAttribute(uint32_t binding, uint32_t location, VkFormat format, uint32_t offset)
|
||||||
VkVertexInputAttributeDescription description{};
|
|
||||||
description.binding = binding;
|
|
||||||
description.location = location;
|
|
||||||
description.format = format;
|
|
||||||
description.offset = offset;
|
|
||||||
attributes.emplace_back(description);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDeviceSize GetVertexSize() const
|
|
||||||
{
|
|
||||||
VkDeviceSize bufferSize = 0;
|
|
||||||
for (auto&& binding : bindings)
|
|
||||||
{
|
{
|
||||||
bufferSize += binding.stride;
|
CP_ASSERT(binding <= bindings.size(), "Attribute binding must less than or be equal to the amount of current bindings");
|
||||||
|
|
||||||
|
if (binding == bindings.size())
|
||||||
|
AddLayout(binding, sizeof(T));
|
||||||
|
|
||||||
|
VkVertexInputAttributeDescription description{};
|
||||||
|
description.binding = binding;
|
||||||
|
description.location = location;
|
||||||
|
description.format = format;
|
||||||
|
description.offset = offset;
|
||||||
|
attributes.emplace_back(description);
|
||||||
}
|
}
|
||||||
return bufferSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<VkVertexInputAttributeDescription>& GetAttributes() const
|
VkDeviceSize GetVertexSize() const
|
||||||
{
|
{
|
||||||
return attributes;
|
VkDeviceSize bufferSize = 0;
|
||||||
}
|
for (auto&& binding : bindings)
|
||||||
|
{
|
||||||
|
bufferSize += binding.stride;
|
||||||
|
}
|
||||||
|
return bufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
const std::vector<VkVertexInputBindingDescription>& GetBindings() const
|
const std::vector<VkVertexInputAttributeDescription>& GetAttributes() const
|
||||||
{
|
{
|
||||||
return bindings;
|
return attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
const std::vector<VkVertexInputBindingDescription>& GetBindings() const
|
||||||
uint32_t AddLayout(uint32_t binding, uint32_t size)
|
{
|
||||||
{
|
return bindings;
|
||||||
VkVertexInputBindingDescription description{};
|
}
|
||||||
description.binding = binding;
|
|
||||||
description.stride = size;
|
private:
|
||||||
description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
uint32_t AddLayout(uint32_t binding, uint32_t size)
|
||||||
bindings.emplace_back(description);
|
{
|
||||||
return description.binding;
|
VkVertexInputBindingDescription description{};
|
||||||
}
|
description.binding = binding;
|
||||||
};
|
description.stride = size;
|
||||||
|
description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||||
|
bindings.emplace_back(description);
|
||||||
|
return description.binding;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,13 +4,16 @@
|
|||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
#include "VertexDescriptor.h"
|
#include "VertexDescriptor.h"
|
||||||
|
|
||||||
struct VertexPassthrough {
|
namespace Copium
|
||||||
glm::vec2 texCoord;
|
{
|
||||||
|
struct VertexPassthrough {
|
||||||
|
glm::vec2 texCoord;
|
||||||
|
|
||||||
static VertexDescriptor GetDescriptor()
|
static VertexDescriptor GetDescriptor()
|
||||||
{
|
{
|
||||||
VertexDescriptor descriptor{};
|
VertexDescriptor descriptor{};
|
||||||
descriptor.AddAttribute<VertexPassthrough>(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexPassthrough , texCoord));
|
descriptor.AddAttribute<VertexPassthrough>(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexPassthrough, texCoord));
|
||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
class VulkanException : public std::runtime_error
|
namespace Copium
|
||||||
{
|
{
|
||||||
public:
|
class VulkanException : public std::runtime_error
|
||||||
VulkanException(const std::string& str)
|
{
|
||||||
: runtime_error{str.c_str()}
|
public:
|
||||||
{}
|
VulkanException(const std::string& str)
|
||||||
};
|
: runtime_error{str.c_str()}
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class Window
|
|
||||||
{
|
|
||||||
|
|
||||||
};
|
|
||||||
+4
-239
@@ -1,250 +1,15 @@
|
|||||||
#include "Buffer.h"
|
#include "Application.h"
|
||||||
#include "DescriptorPool.h"
|
#include "Common.h"
|
||||||
#include "DescriptorSet.h"
|
|
||||||
#include "Framebuffer.h"
|
|
||||||
#include "IndexBuffer.h"
|
|
||||||
#include "Instance.h"
|
|
||||||
#include "Pipeline.h"
|
|
||||||
#include "Texture2D.h"
|
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "UniformBuffer.h"
|
|
||||||
#include "Vertex.h"
|
|
||||||
#include "VertexBuffer.h"
|
|
||||||
#include "VertexPassthrough.h"
|
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <chrono>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <iostream>
|
|
||||||
#include <optional>
|
|
||||||
#include <set>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
const std::vector<Vertex> vertices = {
|
|
||||||
Vertex{{-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
|
||||||
Vertex{{ 0.5f, 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
|
||||||
Vertex{{ 0.5f, 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
|
||||||
Vertex{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
|
|
||||||
Vertex{{-0.5f, 0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
|
||||||
Vertex{{ 0.5f, 0.0f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
|
||||||
Vertex{{ 0.5f, 0.0f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
|
||||||
Vertex{{-0.5f, 0.0f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::vector<uint16_t> indices = {
|
|
||||||
0, 1, 2, 2, 3, 0,
|
|
||||||
4, 5, 6, 6, 7, 4
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::vector<VertexPassthrough> verticesPassthrough = {
|
|
||||||
VertexPassthrough{{-1.0f, -1.0f}},
|
|
||||||
VertexPassthrough{{ 1.0f, -1.0f}},
|
|
||||||
VertexPassthrough{{ 1.0f, 1.0f}},
|
|
||||||
VertexPassthrough{{-1.0f, 1.0f}},
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::vector<uint16_t> indicesPassthrough = {
|
|
||||||
0, 1, 2, 2, 3, 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct alignas(64) ShaderUniform
|
|
||||||
{
|
|
||||||
alignas(16) glm::mat4 projection;
|
|
||||||
alignas(16) glm::mat4 view;
|
|
||||||
alignas(16) glm::mat4 model;
|
|
||||||
alignas(16) glm::vec3 lightPos;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Application final
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
std::unique_ptr<Instance> instance;
|
|
||||||
std::unique_ptr<Pipeline> graphicsPipeline;
|
|
||||||
std::unique_ptr<Texture2D> texture2D;
|
|
||||||
std::unique_ptr<UniformBuffer> shaderUniformBuffer;
|
|
||||||
std::unique_ptr<DescriptorPool> descriptorPool;
|
|
||||||
std::unique_ptr<DescriptorSet> descriptorSet;
|
|
||||||
std::unique_ptr<VertexBuffer> vertexBuffer;
|
|
||||||
std::unique_ptr<IndexBuffer> indexBuffer;
|
|
||||||
std::unique_ptr<CommandBuffer> commandBuffer;
|
|
||||||
|
|
||||||
std::unique_ptr<Framebuffer> framebuffer;
|
|
||||||
std::unique_ptr<Pipeline> graphicsPipelinePassthrough;
|
|
||||||
std::unique_ptr<VertexBuffer> vertexBufferPassthrough;
|
|
||||||
std::unique_ptr<IndexBuffer> indexBufferPassthrough;
|
|
||||||
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Application()
|
|
||||||
{
|
|
||||||
InitializeInstance();
|
|
||||||
InitializeFrameBuffer();
|
|
||||||
InitializeGraphicsPipeline();
|
|
||||||
InitializeTextureSampler();
|
|
||||||
InitializeUniformBuffer();
|
|
||||||
InitializeDescriptorSets();
|
|
||||||
InitializeVertexBuffer();
|
|
||||||
InitializeIndexBuffer();
|
|
||||||
InitializeCommandBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
~Application()
|
|
||||||
{
|
|
||||||
vkDeviceWaitIdle(instance->GetDevice());
|
|
||||||
}
|
|
||||||
|
|
||||||
Application(Application&&) = delete;
|
|
||||||
Application(const Application&) = delete;
|
|
||||||
Application& operator=(Application&&) = delete;
|
|
||||||
Application& operator=(const Application&) = delete;
|
|
||||||
|
|
||||||
bool Update()
|
|
||||||
{
|
|
||||||
if (!instance->BeginPresent())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
RecordCommandBuffer();
|
|
||||||
commandBuffer->SubmitAsGraphicsQueue();
|
|
||||||
|
|
||||||
return instance->EndPresent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
void InitializeInstance()
|
|
||||||
{
|
|
||||||
instance = std::make_unique<Instance>("Copium Engine");
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeFrameBuffer()
|
|
||||||
{
|
|
||||||
framebuffer = std::make_unique<Framebuffer>(*instance, instance->GetSwapChain().GetExtent().width, instance->GetSwapChain().GetExtent().height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeTextureSampler()
|
|
||||||
{
|
|
||||||
texture2D = std::make_unique<Texture2D>(*instance, "res/textures/texture.png");
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeUniformBuffer()
|
|
||||||
{
|
|
||||||
shaderUniformBuffer = std::make_unique<UniformBuffer>(*instance, sizeof(ShaderUniform));
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeDescriptorSets()
|
|
||||||
{
|
|
||||||
descriptorPool = std::make_unique<DescriptorPool>(*instance);
|
|
||||||
|
|
||||||
descriptorSet = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0));
|
|
||||||
descriptorSet->AddUniform(*shaderUniformBuffer, 0);
|
|
||||||
descriptorSet->AddTexture2D(*texture2D, 1);
|
|
||||||
|
|
||||||
descriptorSetPassthrough = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipelinePassthrough->GetDescriptorSetLayout(0));
|
|
||||||
descriptorSetPassthrough->AddTexture2D(framebuffer->GetTexture2D(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeGraphicsPipeline()
|
|
||||||
{
|
|
||||||
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
|
|
||||||
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
|
|
||||||
creator.AddDescriptorSetLayoutBinding(0, 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
||||||
creator.SetVertexDescriptor(Vertex::GetDescriptor());
|
|
||||||
creator.SetCullMode(VK_CULL_MODE_NONE);
|
|
||||||
graphicsPipeline = std::make_unique<Pipeline>(*instance, creator);
|
|
||||||
|
|
||||||
PipelineCreator creatorPassthrough{instance->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
|
|
||||||
creatorPassthrough.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
||||||
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
|
|
||||||
creatorPassthrough.SetCullMode(VK_CULL_MODE_NONE);
|
|
||||||
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*instance, creatorPassthrough);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeVertexBuffer()
|
|
||||||
{
|
|
||||||
vertexBuffer = std::make_unique<VertexBuffer>(*instance, Vertex::GetDescriptor(), vertices.size());
|
|
||||||
vertexBuffer->Update(0, (void*)vertices.data());
|
|
||||||
|
|
||||||
vertexBufferPassthrough = std::make_unique<VertexBuffer>(*instance, VertexPassthrough::GetDescriptor(), verticesPassthrough.size());
|
|
||||||
vertexBufferPassthrough->Update(0, (void*)verticesPassthrough.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeIndexBuffer()
|
|
||||||
{
|
|
||||||
indexBuffer = std::make_unique<IndexBuffer>(*instance, indices.size());
|
|
||||||
indexBuffer->UpdateStaging((void*)indices.data());
|
|
||||||
|
|
||||||
indexBufferPassthrough = std::make_unique<IndexBuffer>(*instance, indicesPassthrough.size());
|
|
||||||
indexBufferPassthrough->UpdateStaging((void*)indicesPassthrough.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
void InitializeCommandBuffer()
|
|
||||||
{
|
|
||||||
commandBuffer = std::make_unique<CommandBuffer>(*instance, CommandBufferType::Dynamic);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RecordCommandBuffer()
|
|
||||||
{
|
|
||||||
commandBuffer->Begin();
|
|
||||||
std::vector<VkClearValue> clearValues{2};
|
|
||||||
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
|
|
||||||
clearValues[1].depthStencil = {1.0f, 0};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
framebuffer->Bind(*commandBuffer);
|
|
||||||
graphicsPipeline->Bind(*commandBuffer);
|
|
||||||
|
|
||||||
UpdateUniformBuffer();
|
|
||||||
|
|
||||||
vertexBuffer->Bind(*commandBuffer);
|
|
||||||
indexBuffer->Bind(*commandBuffer);
|
|
||||||
|
|
||||||
graphicsPipeline->SetDescriptorSet(0, *descriptorSet);
|
|
||||||
graphicsPipeline->BindDescriptorSets(commandBuffer->GetHandle());
|
|
||||||
|
|
||||||
indexBuffer->Draw(*commandBuffer);
|
|
||||||
framebuffer->Unbind(*commandBuffer);
|
|
||||||
|
|
||||||
instance->GetSwapChain().BeginFrameBuffer(*commandBuffer);
|
|
||||||
|
|
||||||
graphicsPipelinePassthrough->Bind(*commandBuffer);
|
|
||||||
graphicsPipelinePassthrough->SetDescriptorSet(0, *descriptorSetPassthrough);
|
|
||||||
graphicsPipelinePassthrough->BindDescriptorSets(commandBuffer->GetHandle());
|
|
||||||
vertexBufferPassthrough->Bind(*commandBuffer);
|
|
||||||
indexBufferPassthrough->Bind(*commandBuffer);
|
|
||||||
indexBufferPassthrough->Draw(*commandBuffer);
|
|
||||||
|
|
||||||
instance->GetSwapChain().EndFrameBuffer(*commandBuffer);
|
|
||||||
commandBuffer->End();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdateUniformBuffer()
|
|
||||||
{
|
|
||||||
static Timer startTimer;
|
|
||||||
|
|
||||||
float time = startTimer.Elapsed();
|
|
||||||
ShaderUniform shaderUniform;
|
|
||||||
shaderUniform.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
||||||
shaderUniform.projection = glm::perspective(glm::radians(45.0f), instance->GetSwapChain().GetExtent().width / (float)instance->GetSwapChain().GetExtent().height, 0.1f, 10.0f);
|
|
||||||
shaderUniform.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
||||||
shaderUniform.projection[1][1] *= -1;
|
|
||||||
shaderUniform.lightPos = glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1};
|
|
||||||
|
|
||||||
shaderUniformBuffer->Update(shaderUniform);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void func(const int* ptr) {
|
|
||||||
*const_cast<int*>(ptr) = 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context");
|
CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context");
|
||||||
{
|
{
|
||||||
Application application;
|
Copium::Application application;
|
||||||
Timer timer;
|
Copium::Timer timer;
|
||||||
int frames = 0;
|
int frames = 0;
|
||||||
while (application.Update())
|
while (application.Update())
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user