Make Vulkan class a global instance
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
#include "copium/pipeline/DescriptorPool.h"
|
||||
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
DescriptorPool::DescriptorPool(Vulkan& vulkan)
|
||||
: vulkan{vulkan}
|
||||
DescriptorPool::DescriptorPool()
|
||||
{
|
||||
std::vector<VkDescriptorPoolSize> poolSizes{2};
|
||||
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
@@ -22,12 +20,12 @@ namespace Copium
|
||||
createInfo.maxSets = DESCRIPTOR_SET_COUNT * SwapChain::MAX_FRAMES_IN_FLIGHT;
|
||||
createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
||||
|
||||
CP_VK_ASSERT(vkCreateDescriptorPool(vulkan.GetDevice(), &createInfo, nullptr, &descriptorPool), "DescriptorPool : Failed to initialize descriptor pool");
|
||||
CP_VK_ASSERT(vkCreateDescriptorPool(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorPool), "DescriptorPool : Failed to initialize descriptor pool");
|
||||
}
|
||||
|
||||
DescriptorPool::~DescriptorPool()
|
||||
{
|
||||
vkDestroyDescriptorPool(vulkan.GetDevice(), descriptorPool, nullptr);
|
||||
vkDestroyDescriptorPool(Vulkan::GetDevice(), descriptorPool, nullptr);
|
||||
}
|
||||
|
||||
std::vector<VkDescriptorSet> DescriptorPool::AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout)
|
||||
@@ -40,13 +38,13 @@ namespace Copium
|
||||
allocateInfo.descriptorSetCount = descriptorSets.size();
|
||||
allocateInfo.pSetLayouts = layouts.data();
|
||||
|
||||
CP_VK_ASSERT(vkAllocateDescriptorSets(vulkan.GetDevice(), &allocateInfo, descriptorSets.data()), "AllocateDescriptorSets : Failed to allocate descriptor sets");
|
||||
CP_VK_ASSERT(vkAllocateDescriptorSets(Vulkan::GetDevice(), &allocateInfo, descriptorSets.data()), "AllocateDescriptorSets : Failed to allocate descriptor sets");
|
||||
|
||||
return descriptorSets;
|
||||
}
|
||||
|
||||
void DescriptorPool::FreeDescriptorSets(const std::vector<VkDescriptorSet>& descriptorSets)
|
||||
{
|
||||
vkFreeDescriptorSets(vulkan.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
|
||||
vkFreeDescriptorSets(Vulkan::GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
@@ -11,12 +10,10 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool);
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
VkDescriptorPool descriptorPool;
|
||||
static const int DESCRIPTOR_SET_COUNT = 100;
|
||||
public:
|
||||
DescriptorPool(Vulkan& vulkan);
|
||||
DescriptorPool();
|
||||
~DescriptorPool();
|
||||
|
||||
std::vector<VkDescriptorSet> AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout);
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include "copium/pipeline/DescriptorSet.h"
|
||||
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/SwapChain.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
DescriptorSet::DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings)
|
||||
: vulkan{vulkan}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings}
|
||||
DescriptorSet::DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings)
|
||||
: descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings}
|
||||
{
|
||||
CP_ASSERT(!bindings.empty(), "DescriptorSet : cannot initialize DescriptorSet with empty ShaderBindings");
|
||||
|
||||
@@ -15,7 +14,7 @@ namespace Copium
|
||||
{
|
||||
if (binding.bindingType == BindingType::UniformBuffer)
|
||||
{
|
||||
std::unique_ptr<UniformBuffer> uniformBuffer = std::make_unique<UniformBuffer>(vulkan, binding);
|
||||
std::unique_ptr<UniformBuffer> uniformBuffer = std::make_unique<UniformBuffer>(binding);
|
||||
SetUniformBuffer(*uniformBuffer, binding.binding);
|
||||
uniformBuffers.emplace(binding.name, std::move(uniformBuffer));
|
||||
}
|
||||
@@ -28,25 +27,6 @@ namespace Copium
|
||||
descriptorPool.FreeDescriptorSets(descriptorSets);
|
||||
}
|
||||
|
||||
void DescriptorSet::SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding)
|
||||
{
|
||||
for (size_t i = 0; i < descriptorSets.size(); ++i) {
|
||||
VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i);
|
||||
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = descriptorSets[i];
|
||||
descriptorWrite.dstBinding = binding;
|
||||
descriptorWrite.dstArrayElement = 0;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptorWrite.descriptorCount = 1;
|
||||
descriptorWrite.pBufferInfo = &bufferInfo;
|
||||
descriptorWrite.pImageInfo = nullptr;
|
||||
descriptorWrite.pTexelBufferView = nullptr;
|
||||
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void DescriptorSet::SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex)
|
||||
{
|
||||
for (size_t i = 0; i < descriptorSets.size(); ++i)
|
||||
@@ -62,16 +42,16 @@ namespace Copium
|
||||
descriptorWrite.pBufferInfo = nullptr;
|
||||
descriptorWrite.pImageInfo = &imageInfo;
|
||||
descriptorWrite.pTexelBufferView = nullptr;
|
||||
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void DescriptorSet::SetSamplerDynamic(const Sampler& sampler, uint32_t binding, int arrayIndex)
|
||||
{
|
||||
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(vulkan.GetSwapChain().GetFlightIndex());
|
||||
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(Vulkan::GetSwapChain().GetFlightIndex());
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = descriptorSets[vulkan.GetSwapChain().GetFlightIndex()];
|
||||
descriptorWrite.dstSet = descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()];
|
||||
descriptorWrite.dstBinding = binding;
|
||||
descriptorWrite.dstArrayElement = arrayIndex;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
@@ -79,7 +59,7 @@ namespace Copium
|
||||
descriptorWrite.pBufferInfo = nullptr;
|
||||
descriptorWrite.pImageInfo = &imageInfo;
|
||||
descriptorWrite.pTexelBufferView = nullptr;
|
||||
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
|
||||
void DescriptorSet::SetSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding)
|
||||
@@ -99,7 +79,7 @@ namespace Copium
|
||||
descriptorWrites[j].pImageInfo = &imageInfo;
|
||||
descriptorWrites[j].pTexelBufferView = nullptr;
|
||||
}
|
||||
vkUpdateDescriptorSets(vulkan.GetDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
|
||||
vkUpdateDescriptorSets(Vulkan::GetDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +97,26 @@ namespace Copium
|
||||
|
||||
DescriptorSet::operator VkDescriptorSet() const
|
||||
{
|
||||
return descriptorSets[vulkan.GetSwapChain().GetFlightIndex()];
|
||||
return descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()];
|
||||
}
|
||||
|
||||
void DescriptorSet::SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding)
|
||||
{
|
||||
for (size_t i = 0; i < descriptorSets.size(); ++i) {
|
||||
VkDescriptorBufferInfo bufferInfo = uniformBuffer.GetDescriptorBufferInfo(i);
|
||||
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = descriptorSets[i];
|
||||
descriptorWrite.dstBinding = binding;
|
||||
descriptorWrite.dstArrayElement = 0;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptorWrite.descriptorCount = 1;
|
||||
descriptorWrite.pBufferInfo = &bufferInfo;
|
||||
descriptorWrite.pImageInfo = nullptr;
|
||||
descriptorWrite.pTexelBufferView = nullptr;
|
||||
vkUpdateDescriptorSets(Vulkan::GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,6 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet);
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
DescriptorPool& descriptorPool;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
|
||||
@@ -29,10 +28,9 @@ namespace Copium
|
||||
std::map<std::string, std::unique_ptr<UniformBuffer>> uniformBuffers;
|
||||
|
||||
public:
|
||||
DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings);
|
||||
DescriptorSet(DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings);
|
||||
~DescriptorSet();
|
||||
|
||||
void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding);
|
||||
void SetSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
|
||||
void SetSamplerDynamic(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
|
||||
void SetSamplers(const std::vector<const Sampler*>& sampler, uint32_t binding);
|
||||
@@ -40,5 +38,7 @@ namespace Copium
|
||||
uint32_t GetSetIndex() const;
|
||||
|
||||
operator VkDescriptorSet() const;
|
||||
private:
|
||||
void SetUniformBuffer(const UniformBuffer& uniformBuffer, uint32_t binding);
|
||||
};
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
#include "copium/pipeline/Pipeline.h"
|
||||
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/pipeline/Shader.h"
|
||||
#include "copium/util/FileSystem.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Pipeline::Pipeline(Vulkan& vulkan, PipelineCreator creator)
|
||||
: vulkan{vulkan}, shaderReflector{creator.shaderReflector}
|
||||
Pipeline::Pipeline(PipelineCreator creator)
|
||||
: shaderReflector{creator.shaderReflector}
|
||||
{
|
||||
InitializeDescriptorSetLayout(creator);
|
||||
InitializePipeline(creator);
|
||||
@@ -15,11 +15,11 @@ namespace Copium
|
||||
|
||||
Pipeline::~Pipeline()
|
||||
{
|
||||
vkDestroyPipeline(vulkan.GetDevice(), graphicsPipeline, nullptr);
|
||||
vkDestroyPipelineLayout(vulkan.GetDevice(), pipelineLayout, nullptr);
|
||||
vkDestroyPipeline(Vulkan::GetDevice(), graphicsPipeline, nullptr);
|
||||
vkDestroyPipelineLayout(Vulkan::GetDevice(), pipelineLayout, nullptr);
|
||||
for (auto&& descriptorSetLayout : descriptorSetLayouts)
|
||||
{
|
||||
vkDestroyDescriptorSetLayout(vulkan.GetDevice(), descriptorSetLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Copium
|
||||
bindings.emplace(binding);
|
||||
}
|
||||
|
||||
return std::make_unique<DescriptorSet>(vulkan, descriptorPool, descriptorSetLayouts[setIndex], bindings);
|
||||
return std::make_unique<DescriptorSet>(descriptorPool, descriptorSetLayouts[setIndex], bindings);
|
||||
}
|
||||
|
||||
void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator)
|
||||
@@ -76,13 +76,13 @@ namespace Copium
|
||||
createInfo.bindingCount = layoutBindings.size();
|
||||
createInfo.pBindings = layoutBindings.data();
|
||||
|
||||
CP_VK_ASSERT(vkCreateDescriptorSetLayout(vulkan.GetDevice(), &createInfo, nullptr, &descriptorSetLayouts[i++]), "InitializeDescriptorSetLayout : Failed to initialize descriptor set layout");
|
||||
CP_VK_ASSERT(vkCreateDescriptorSetLayout(Vulkan::GetDevice(), &createInfo, nullptr, &descriptorSetLayouts[i++]), "InitializeDescriptorSetLayout : Failed to initialize descriptor set layout");
|
||||
}
|
||||
}
|
||||
|
||||
void Pipeline::InitializePipeline(const PipelineCreator& creator)
|
||||
{
|
||||
Shader shader{vulkan, Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader};
|
||||
Shader shader{Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader};
|
||||
|
||||
VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{};
|
||||
vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||
@@ -192,7 +192,7 @@ namespace Copium
|
||||
pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
||||
pipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
||||
|
||||
CP_VK_ASSERT(vkCreatePipelineLayout(vulkan.GetDevice(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout), "InitializePipeline : Failed to initialize pipeline layout");
|
||||
CP_VK_ASSERT(vkCreatePipelineLayout(Vulkan::GetDevice(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout), "InitializePipeline : Failed to initialize pipeline layout");
|
||||
|
||||
const std::vector<VkPipelineShaderStageCreateInfo>& shaderStages = shader.GetShaderStages();
|
||||
VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfo{};
|
||||
@@ -213,6 +213,6 @@ namespace Copium
|
||||
graphicsPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
|
||||
graphicsPipelineCreateInfo.basePipelineIndex = -1;
|
||||
|
||||
CP_VK_ASSERT(vkCreateGraphicsPipelines(vulkan.GetDevice(), VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline), "InitializePipeline : Failed to initialize graphics pipeline");
|
||||
CP_VK_ASSERT(vkCreateGraphicsPipelines(Vulkan::GetDevice(), VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &graphicsPipeline), "InitializePipeline : Failed to initialize graphics pipeline");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/buffer/CommandBuffer.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/pipeline/DescriptorSet.h"
|
||||
#include "copium/pipeline/PipelineCreator.h"
|
||||
#include "copium/util/Common.h"
|
||||
@@ -15,8 +14,6 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline);
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
ShaderReflector shaderReflector;
|
||||
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{};
|
||||
std::vector<VkDescriptorSet> boundDescriptorSets;
|
||||
@@ -24,7 +21,7 @@ namespace Copium
|
||||
VkPipeline graphicsPipeline;
|
||||
|
||||
public:
|
||||
Pipeline(Vulkan& vulkan, PipelineCreator creator);
|
||||
Pipeline(PipelineCreator creator);
|
||||
~Pipeline();
|
||||
void Bind(const CommandBuffer& commandBuffer);
|
||||
void SetDescriptorSet(const DescriptorSet& descriptorSet);
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/pipeline/VertexDescriptor.h"
|
||||
#include "copium/pipeline/ShaderReflector.h"
|
||||
#include "copium/pipeline/VertexDescriptor.h"
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include "Shader.h"
|
||||
|
||||
#include "copium/util/FileSystem.h"
|
||||
#include "copium/core/Device.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Shader::Shader(Vulkan& vulkan, Type type, const std::string& vertexInput, const std::string& fragmentInput)
|
||||
: vulkan{vulkan}
|
||||
Shader::Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
@@ -46,8 +45,8 @@ namespace Copium
|
||||
|
||||
Shader::~Shader()
|
||||
{
|
||||
vkDestroyShaderModule(vulkan.GetDevice(), vertShaderModule, nullptr);
|
||||
vkDestroyShaderModule(vulkan.GetDevice(), fragShaderModule, nullptr);
|
||||
vkDestroyShaderModule(Vulkan::GetDevice(), vertShaderModule, nullptr);
|
||||
vkDestroyShaderModule(Vulkan::GetDevice(), fragShaderModule, nullptr);
|
||||
}
|
||||
|
||||
const std::vector<VkPipelineShaderStageCreateInfo> Shader::GetShaderStages() const
|
||||
@@ -126,7 +125,7 @@ namespace Copium
|
||||
createInfo.pCode = data;
|
||||
|
||||
VkShaderModule shaderModule;
|
||||
CP_VK_ASSERT(vkCreateShaderModule(vulkan.GetDevice(), &createInfo, nullptr, &shaderModule), "InitializeShaderModule : Failed to initialize shader module");
|
||||
CP_VK_ASSERT(vkCreateShaderModule(Vulkan::GetDevice(), &createInfo, nullptr, &shaderModule), "InitializeShaderModule : Failed to initialize shader module");
|
||||
|
||||
return shaderModule;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <shaderc/shaderc.hpp>
|
||||
@@ -18,13 +17,11 @@ namespace Copium
|
||||
};
|
||||
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
VkShaderModule vertShaderModule;
|
||||
VkShaderModule fragShaderModule;
|
||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
||||
public:
|
||||
Shader(Vulkan& vulkan, Type type, const std::string& vertexInput, const std::string& fragmentInput);
|
||||
Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput);
|
||||
~Shader();
|
||||
|
||||
const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "copium/pipeline/ShaderBinding.h"
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user