Refactor UniformBuffers and DescriptorSets
- Refactor UniformBuffers to keep track of the uniforms it contains - Refactor DescriptorSets to initialize UniformsBuffers and keep track of all its bindings - DescriptorSets can now be created from the Pipeline - Add custom DescriptorSets to Renderer
This commit is contained in:
@@ -171,7 +171,8 @@
|
||||
<ClCompile Include="src\copium\core\Vulkan.cpp" />
|
||||
<ClCompile Include="src\copium\core\Window.cpp" />
|
||||
<ClCompile Include="src\copium\mesh\Mesh.cpp" />
|
||||
<ClCompile Include="src\copium\renderer\DrawCall.cpp" />
|
||||
<ClCompile Include="src\copium\pipeline\ShaderBinding.cpp" />
|
||||
<ClCompile Include="src\copium\renderer\Batch.cpp" />
|
||||
<ClCompile Include="src\copium\renderer\Renderer.cpp" />
|
||||
<ClCompile Include="src\copium\renderer\RendererVertex.cpp" />
|
||||
<ClCompile Include="src\copium\sampler\ColorAttachment.cpp" />
|
||||
@@ -194,7 +195,7 @@
|
||||
<ClCompile Include="src\copium\pipeline\Shader.cpp" />
|
||||
<ClCompile Include="src\copium\core\SwapChain.cpp" />
|
||||
<ClCompile Include="src\copium\sampler\Texture2D.cpp" />
|
||||
<ClCompile Include="src\copium\util\ShaderReflector.cpp" />
|
||||
<ClCompile Include="src\copium\pipeline\ShaderReflector.cpp" />
|
||||
<ClCompile Include="src\copium\util\Timer.cpp" />
|
||||
<ClCompile Include="src\copium\buffer\UniformBuffer.cpp" />
|
||||
<ClCompile Include="src\copium\mesh\Vertex.cpp" />
|
||||
@@ -208,7 +209,8 @@
|
||||
<ClInclude Include="src\copium\core\Vulkan.h" />
|
||||
<ClInclude Include="src\copium\core\Window.h" />
|
||||
<ClInclude Include="src\copium\mesh\Mesh.h" />
|
||||
<ClInclude Include="src\copium\renderer\DrawCall.h" />
|
||||
<ClInclude Include="src\copium\pipeline\ShaderBinding.h" />
|
||||
<ClInclude Include="src\copium\renderer\Batch.h" />
|
||||
<ClInclude Include="src\copium\renderer\Renderer.h" />
|
||||
<ClInclude Include="src\copium\renderer\RendererVertex.h" />
|
||||
<ClInclude Include="src\copium\sampler\DepthAttachment.h" />
|
||||
@@ -234,7 +236,7 @@
|
||||
<ClInclude Include="src\copium\core\Instance.h" />
|
||||
<ClInclude Include="src\copium\core\QueueFamilies.h" />
|
||||
<ClInclude Include="src\copium\core\SwapChain.h" />
|
||||
<ClInclude Include="src\copium\util\ShaderReflector.h" />
|
||||
<ClInclude Include="src\copium\pipeline\ShaderReflector.h" />
|
||||
<ClInclude Include="src\copium\util\Timer.h" />
|
||||
<ClInclude Include="src\copium\mesh\Vertex.h" />
|
||||
<ClInclude Include="src\copium\buffer\VertexBuffer.h" />
|
||||
|
||||
@@ -120,10 +120,13 @@
|
||||
<ClCompile Include="src\copium\buffer\RendererVertexBuffer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\renderer\DrawCall.cpp">
|
||||
<ClCompile Include="src\copium\renderer\Batch.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\util\ShaderReflector.cpp">
|
||||
<ClCompile Include="src\copium\pipeline\ShaderReflector.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\pipeline\ShaderBinding.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
@@ -236,10 +239,13 @@
|
||||
<ClInclude Include="src\copium\buffer\RendererVertexBuffer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\renderer\DrawCall.h">
|
||||
<ClInclude Include="src\copium\renderer\Batch.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\util\ShaderReflector.h">
|
||||
<ClInclude Include="src\copium\pipeline\ShaderReflector.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\pipeline\ShaderBinding.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#version 450
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 0) in vec2 inPosition;
|
||||
layout(location = 1) in vec3 inColor;
|
||||
layout(location = 2) in vec2 inTexCoord;
|
||||
layout(location = 3) in int inTexIndex;
|
||||
@@ -9,9 +9,15 @@ layout(location = 0) out vec3 fragColor;
|
||||
layout(location = 1) out vec2 fragTexCoord;
|
||||
layout(location = 2) out int fragTexIndex;
|
||||
|
||||
layout(set = 1, binding = 0) uniform SceneUniformBufferObject
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 view;
|
||||
} ubo;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(inPosition, 1.0);
|
||||
gl_Position = ubo.projection * ubo.view * vec4(inPosition, 0.0, 1.0);
|
||||
fragColor = inColor;
|
||||
fragTexCoord = inTexCoord;
|
||||
fragTexIndex = inTexIndex;
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
UniformBuffer::UniformBuffer(Vulkan& vulkan, VkDeviceSize size)
|
||||
: Buffer{vulkan, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, SwapChain::MAX_FRAMES_IN_FLIGHT}
|
||||
{}
|
||||
UniformBuffer::UniformBuffer(Vulkan& vulkan, ShaderBinding binding)
|
||||
: Buffer{vulkan, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, binding.GetUniformBufferSize(), SwapChain::MAX_FRAMES_IN_FLIGHT}, binding{binding}
|
||||
{
|
||||
buffer.resize(Buffer::GetSize());
|
||||
}
|
||||
|
||||
VkDescriptorBufferInfo UniformBuffer::GetDescriptorBufferInfo(int index) const
|
||||
{
|
||||
@@ -16,4 +18,58 @@ namespace Copium
|
||||
bufferInfo.range = size;
|
||||
return bufferInfo;
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::mat3& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat3, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::mat3));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::mat4& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat4, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::mat4));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::vec2& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec2, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::vec2));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::vec3& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec3, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::vec3));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, const glm::vec4& data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec4, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(glm::vec4));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, float data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Float, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(float));
|
||||
}
|
||||
|
||||
void UniformBuffer::Set(const std::string& str, int data)
|
||||
{
|
||||
CP_ASSERT(binding.GetUniformType(str) == UniformType::Int, "Set : Uniform type missmatch = %s", str.c_str());
|
||||
uint32_t offset = binding.GetUniformOffset(str);
|
||||
memcpy(buffer.data() + offset, &data, sizeof(int));
|
||||
}
|
||||
|
||||
void UniformBuffer::Update()
|
||||
{
|
||||
Buffer::Update(buffer.data(), vulkan.GetSwapChain().GetFlightIndex());
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include "copium/buffer/Buffer.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/pipeline/ShaderBinding.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
namespace Copium
|
||||
@@ -12,20 +14,22 @@ namespace Copium
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
|
||||
|
||||
ShaderBinding binding;
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
public:
|
||||
UniformBuffer(Vulkan& vulkan, VkDeviceSize size);
|
||||
UniformBuffer(Vulkan& vulkan, ShaderBinding binding);
|
||||
|
||||
VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const;
|
||||
|
||||
template <typename T>
|
||||
void Update(const T& t);
|
||||
void Set(const std::string& str, const glm::mat3& data);
|
||||
void Set(const std::string& str, const glm::mat4& data);
|
||||
void Set(const std::string& str, const glm::vec2& data);
|
||||
void Set(const std::string& str, const glm::vec3& data);
|
||||
void Set(const std::string& str, const glm::vec4& data);
|
||||
void Set(const std::string& str, float data);
|
||||
void Set(const std::string& str, int data);
|
||||
|
||||
void Update();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void UniformBuffer::Update(const T& t)
|
||||
{
|
||||
CP_ASSERT(sizeof(T) == Buffer::GetSize(), "Update : Template size is not the same as buffer size %u != %u", sizeof(T), Buffer::GetSize());
|
||||
Buffer::Update((void*)&t, vulkan.GetSwapChain().GetFlightIndex());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,25 +38,16 @@ namespace Copium
|
||||
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;
|
||||
};
|
||||
|
||||
Application::Application()
|
||||
{
|
||||
InitializeVulkan();
|
||||
InitializeFrameBuffer();
|
||||
InitializeRenderer();
|
||||
InitializeGraphicsPipeline();
|
||||
InitializeTextureSampler();
|
||||
InitializeUniformBuffer();
|
||||
InitializeDescriptorSets();
|
||||
InitializeMesh();
|
||||
InitializeCommandBuffer();
|
||||
InitializeRenderer();
|
||||
}
|
||||
|
||||
Application::~Application()
|
||||
@@ -97,27 +88,28 @@ namespace Copium
|
||||
framebuffer = std::make_unique<Framebuffer>(*vulkan, vulkan->GetSwapChain().GetExtent().width, vulkan->GetSwapChain().GetExtent().height);
|
||||
}
|
||||
|
||||
void Application::InitializeRenderer()
|
||||
{
|
||||
renderer = std::make_unique<Renderer>(*vulkan, framebuffer->GetRenderPass(), *descriptorPool);
|
||||
}
|
||||
|
||||
void Application::InitializeTextureSampler()
|
||||
{
|
||||
texture2D = std::make_unique<Texture2D>(*vulkan, "res/textures/texture.png");
|
||||
texture2D2 = std::make_unique<Texture2D>(*vulkan, "res/textures/texture2.png");
|
||||
}
|
||||
|
||||
void Application::InitializeUniformBuffer()
|
||||
{
|
||||
shaderUniformBuffer = std::make_unique<UniformBuffer>(*vulkan, sizeof(ShaderUniform));
|
||||
}
|
||||
|
||||
void Application::InitializeDescriptorSets()
|
||||
{
|
||||
descriptorPool = std::make_unique<DescriptorPool>(*vulkan);
|
||||
|
||||
descriptorSet = std::make_unique<DescriptorSet>(*vulkan, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0));
|
||||
descriptorSet->SetUniformBuffer(*shaderUniformBuffer, 0);
|
||||
descriptorSet = graphicsPipeline->CreateDescriptorSet(*descriptorPool, 0);
|
||||
descriptorSet->SetSampler(*texture2D, 1);
|
||||
|
||||
descriptorSetPassthrough = std::make_unique<DescriptorSet>(*vulkan, *descriptorPool, graphicsPipelinePassthrough->GetDescriptorSetLayout(0));
|
||||
descriptorSetPassthrough = graphicsPipelinePassthrough->CreateDescriptorSet(*descriptorPool, 0);
|
||||
descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
|
||||
|
||||
descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(*descriptorPool, 1);
|
||||
}
|
||||
|
||||
void Application::InitializeGraphicsPipeline()
|
||||
@@ -142,11 +134,6 @@ namespace Copium
|
||||
commandBuffer = std::make_unique<CommandBuffer>(*vulkan, CommandBuffer::Type::Dynamic);
|
||||
}
|
||||
|
||||
void Application::InitializeRenderer()
|
||||
{
|
||||
renderer = std::make_unique<Renderer>(*vulkan, framebuffer->GetRenderPass(), *descriptorPool);
|
||||
}
|
||||
|
||||
void Application::RecordCommandBuffer()
|
||||
{
|
||||
commandBuffer->Begin();
|
||||
@@ -156,22 +143,23 @@ namespace Copium
|
||||
|
||||
UpdateUniformBuffer();
|
||||
|
||||
graphicsPipeline->SetDescriptorSet(0, *descriptorSet);
|
||||
graphicsPipeline->SetDescriptorSet(*descriptorSet);
|
||||
graphicsPipeline->BindDescriptorSets(*commandBuffer);
|
||||
|
||||
mesh->Bind(*commandBuffer);
|
||||
mesh->Render(*commandBuffer);
|
||||
|
||||
renderer->SetDescriptorSet(*descriptorSetRenderer);
|
||||
renderer->Begin(*commandBuffer);
|
||||
for (int y = 0; y < 10; y++)
|
||||
{
|
||||
for (int x = 0; x < 10; x++)
|
||||
{
|
||||
renderer->Quad(glm::vec2{-1 + x * 0.2, -1 + y * 0.2}, glm::vec2{-1 + (x + 0.5) * 0.2, -1 + (y + 0.5) * 0.2}, glm::vec3{x * 0.1, y * 0.1, 1.0});
|
||||
renderer->Quad(glm::vec2{-1 + x * 0.2 + 0.05, -1 + y * 0.2 + 0.05}, glm::vec2{0.1, 0.1}, glm::vec3{x * 0.1, y * 0.1, 1.0});
|
||||
}
|
||||
}
|
||||
renderer->Quad(glm::vec2{-0.5, -0.5}, glm::vec2{-0.1, 0.5}, *texture2D);
|
||||
renderer->Quad(glm::vec2{0.1, -0.5}, glm::vec2{0.5, 0.5}, *texture2D2);
|
||||
renderer->Quad(glm::vec2{-0.9, -0.4}, glm::vec2{0.8, 0.8}, *texture2D);
|
||||
renderer->Quad(glm::vec2{ 0.1, -0.4}, glm::vec2{0.8, 0.8}, *texture2D2);
|
||||
renderer->End();
|
||||
|
||||
framebuffer->Unbind(*commandBuffer);
|
||||
@@ -179,7 +167,7 @@ namespace Copium
|
||||
vulkan->GetSwapChain().BeginFrameBuffer(*commandBuffer);
|
||||
|
||||
graphicsPipelinePassthrough->Bind(*commandBuffer);
|
||||
graphicsPipelinePassthrough->SetDescriptorSet(0, *descriptorSetPassthrough);
|
||||
graphicsPipelinePassthrough->SetDescriptorSet(*descriptorSetPassthrough);
|
||||
graphicsPipelinePassthrough->BindDescriptorSets(*commandBuffer);
|
||||
|
||||
meshPassthrough->Bind(*commandBuffer);
|
||||
@@ -194,13 +182,25 @@ namespace Copium
|
||||
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), framebuffer->GetWidth() / (float)framebuffer->GetHeight(), 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};
|
||||
float aspect = framebuffer->GetWidth() / (float)framebuffer->GetHeight();
|
||||
|
||||
shaderUniformBuffer->Update(shaderUniform);
|
||||
{
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 10.0f);
|
||||
projection[1][1] *= -1;
|
||||
|
||||
UniformBuffer& uniformBuffer = descriptorSet->GetUniformBuffer("ubo");
|
||||
uniformBuffer.Set("projection", projection);
|
||||
uniformBuffer.Set("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)));
|
||||
uniformBuffer.Set("model", glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||
uniformBuffer.Set("lightPos", (glm::vec3)(glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1}));
|
||||
uniformBuffer.Update();
|
||||
}
|
||||
|
||||
{
|
||||
UniformBuffer& uniformBuffer = descriptorSetRenderer->GetUniformBuffer("ubo");
|
||||
uniformBuffer.Set("projection", glm::ortho(-aspect, aspect, -1.0f, 1.0f));
|
||||
uniformBuffer.Set("view", glm::translate(glm::mat4(1), glm::vec3(0.1 * glm::sin(4 * time), 0.1 * glm::cos(4 * time), 0.0)));
|
||||
uniformBuffer.Update();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,19 +22,19 @@ namespace Copium
|
||||
private:
|
||||
std::unique_ptr<Vulkan> vulkan;
|
||||
std::unique_ptr<Instance> instance;
|
||||
std::unique_ptr<Renderer> renderer;
|
||||
std::unique_ptr<Framebuffer> framebuffer;
|
||||
std::unique_ptr<Texture2D> texture2D;
|
||||
std::unique_ptr<Texture2D> texture2D2;
|
||||
std::unique_ptr<UniformBuffer> shaderUniformBuffer;
|
||||
std::unique_ptr<DescriptorPool> descriptorPool;
|
||||
std::unique_ptr<DescriptorSet> descriptorSet;
|
||||
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
|
||||
std::unique_ptr<DescriptorSet> descriptorSetRenderer;
|
||||
std::unique_ptr<Pipeline> graphicsPipeline;
|
||||
std::unique_ptr<Pipeline> graphicsPipelinePassthrough;
|
||||
std::unique_ptr<Mesh> mesh;
|
||||
std::unique_ptr<Mesh> meshPassthrough;
|
||||
std::unique_ptr<CommandBuffer> commandBuffer;
|
||||
std::unique_ptr<Renderer> renderer;
|
||||
|
||||
public:
|
||||
Application();
|
||||
@@ -44,13 +44,12 @@ namespace Copium
|
||||
private:
|
||||
void InitializeVulkan();
|
||||
void InitializeFrameBuffer();
|
||||
void InitializeRenderer();
|
||||
void InitializeTextureSampler();
|
||||
void InitializeUniformBuffer();
|
||||
void InitializeDescriptorSets();
|
||||
void InitializeGraphicsPipeline();
|
||||
void InitializeMesh();
|
||||
void InitializeCommandBuffer();
|
||||
void InitializeRenderer();
|
||||
|
||||
void RecordCommandBuffer();
|
||||
void UpdateUniformBuffer();
|
||||
|
||||
@@ -5,10 +5,22 @@
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
DescriptorSet::DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout)
|
||||
: vulkan{vulkan}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}
|
||||
DescriptorSet::DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set<ShaderBinding>& bindings)
|
||||
: vulkan{vulkan}, descriptorPool{descriptorPool}, descriptorSetLayout{descriptorSetLayout}, bindings{bindings}
|
||||
{
|
||||
CP_ASSERT(!bindings.empty(), "DescriptorSet : cannot initialize DescriptorSet with empty ShaderBindings");
|
||||
|
||||
descriptorSets = descriptorPool.AllocateDescriptorSets(descriptorSetLayout);
|
||||
for (auto& binding : bindings)
|
||||
{
|
||||
if (binding.bindingType == BindingType::UniformBuffer)
|
||||
{
|
||||
std::unique_ptr<UniformBuffer> uniformBuffer = std::make_unique<UniformBuffer>(vulkan, binding);
|
||||
SetUniformBuffer(*uniformBuffer, binding.binding);
|
||||
uniformBuffers.emplace(binding.name, std::move(uniformBuffer));
|
||||
}
|
||||
}
|
||||
// TODO: Set default samplers and uniforms?
|
||||
}
|
||||
|
||||
DescriptorSet::~DescriptorSet()
|
||||
@@ -39,17 +51,27 @@ namespace Copium
|
||||
{
|
||||
for (size_t i = 0; i < descriptorSets.size(); ++i)
|
||||
{
|
||||
SetSampler(sampler, binding, i, arrayIndex);
|
||||
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(i);
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = descriptorSets[i];
|
||||
descriptorWrite.dstBinding = binding;
|
||||
descriptorWrite.dstArrayElement = arrayIndex;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
descriptorWrite.descriptorCount = 1;
|
||||
descriptorWrite.pBufferInfo = nullptr;
|
||||
descriptorWrite.pImageInfo = &imageInfo;
|
||||
descriptorWrite.pTexelBufferView = nullptr;
|
||||
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void DescriptorSet::SetSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex)
|
||||
void DescriptorSet::SetSamplerDynamic(const Sampler& sampler, uint32_t binding, int arrayIndex)
|
||||
{
|
||||
CP_ASSERT(index >= 0 && index < descriptorSets.size(), "AddSampler : index is out of range");
|
||||
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(index);
|
||||
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(vulkan.GetSwapChain().GetFlightIndex());
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = descriptorSets[index];
|
||||
descriptorWrite.dstSet = descriptorSets[vulkan.GetSwapChain().GetFlightIndex()];
|
||||
descriptorWrite.dstBinding = binding;
|
||||
descriptorWrite.dstArrayElement = arrayIndex;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
@@ -81,6 +103,18 @@ namespace Copium
|
||||
}
|
||||
}
|
||||
|
||||
UniformBuffer& DescriptorSet::GetUniformBuffer(const std::string& uniformBuffer)
|
||||
{
|
||||
auto it = uniformBuffers.find(uniformBuffer);
|
||||
CP_ASSERT(it != uniformBuffers.end(), "GetUniformBuffer : UniformBuffer not found = %s", uniformBuffer.c_str());
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
uint32_t DescriptorSet::GetSetIndex() const
|
||||
{
|
||||
return bindings.begin()->set;
|
||||
}
|
||||
|
||||
DescriptorSet::operator VkDescriptorSet() const
|
||||
{
|
||||
return descriptorSets[vulkan.GetSwapChain().GetFlightIndex()];
|
||||
|
||||
@@ -2,11 +2,18 @@
|
||||
|
||||
#include "copium/buffer/UniformBuffer.h"
|
||||
#include "copium/pipeline/DescriptorPool.h"
|
||||
#include "copium/pipeline/ShaderBinding.h"
|
||||
#include "copium/pipeline/ShaderReflector.h"
|
||||
#include "copium/sampler/Sampler.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class DescriptorSet final
|
||||
@@ -17,16 +24,21 @@ namespace Copium
|
||||
DescriptorPool& descriptorPool;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
|
||||
std::set<ShaderBinding> bindings;
|
||||
std::vector<VkDescriptorSet> descriptorSets;
|
||||
std::map<std::string, std::unique_ptr<UniformBuffer>> uniformBuffers;
|
||||
|
||||
public:
|
||||
DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout);
|
||||
DescriptorSet(Vulkan& vulkan, 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 SetSampler(const Sampler& sampler, uint32_t binding, int index, 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);
|
||||
UniformBuffer& GetUniformBuffer(const std::string& uniformBuffer);
|
||||
uint32_t GetSetIndex() const;
|
||||
|
||||
operator VkDescriptorSet() const;
|
||||
};
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace Copium
|
||||
{
|
||||
Pipeline::Pipeline(Vulkan& vulkan, PipelineCreator creator)
|
||||
: vulkan{vulkan}
|
||||
: vulkan{vulkan}, shaderReflector{creator.shaderReflector}
|
||||
{
|
||||
InitializeDescriptorSetLayout(creator);
|
||||
InitializePipeline(creator);
|
||||
@@ -28,10 +28,10 @@ namespace Copium
|
||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
|
||||
}
|
||||
|
||||
void Pipeline::SetDescriptorSet(uint32_t setIndex, const DescriptorSet& descriptorSet)
|
||||
void Pipeline::SetDescriptorSet(const DescriptorSet& descriptorSet)
|
||||
{
|
||||
CP_ASSERT(setIndex < boundDescriptorSets.size(), "SetDescriptorSet : DescriptorSet index is out of bounds");
|
||||
boundDescriptorSets[setIndex] = descriptorSet;
|
||||
CP_ASSERT(descriptorSet.GetSetIndex() < boundDescriptorSets.size(), "SetDescriptorSet : DescriptorSet index is out of bounds");
|
||||
boundDescriptorSets[descriptorSet.GetSetIndex()] = descriptorSet;
|
||||
}
|
||||
|
||||
void Pipeline::BindDescriptorSets(const CommandBuffer& commandBuffer)
|
||||
@@ -39,9 +39,17 @@ namespace Copium
|
||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, boundDescriptorSets.size(), boundDescriptorSets.data(), 0, nullptr);
|
||||
}
|
||||
|
||||
VkDescriptorSetLayout Pipeline::GetDescriptorSetLayout(uint32_t setIndex) const
|
||||
std::unique_ptr<DescriptorSet> Pipeline::CreateDescriptorSet(DescriptorPool& descriptorPool, int setIndex) const
|
||||
{
|
||||
return descriptorSetLayouts[setIndex];
|
||||
std::set<ShaderBinding> bindings;
|
||||
for (auto& binding : shaderReflector.bindings)
|
||||
{
|
||||
if (binding.set != setIndex)
|
||||
continue;
|
||||
bindings.emplace(binding);
|
||||
}
|
||||
|
||||
return std::make_unique<DescriptorSet>(vulkan, descriptorPool, descriptorSetLayouts[setIndex], bindings);
|
||||
}
|
||||
|
||||
void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator)
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace Copium
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
|
||||
ShaderReflector shaderReflector;
|
||||
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{};
|
||||
std::vector<VkDescriptorSet> boundDescriptorSets;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
@@ -26,9 +27,10 @@ namespace Copium
|
||||
Pipeline(Vulkan& vulkan, PipelineCreator creator);
|
||||
~Pipeline();
|
||||
void Bind(const CommandBuffer& commandBuffer);
|
||||
void SetDescriptorSet(uint32_t setIndex, const DescriptorSet& descriptorSet);
|
||||
void SetDescriptorSet(const DescriptorSet& descriptorSet);
|
||||
void BindDescriptorSets(const CommandBuffer& commandBuffer);
|
||||
VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t setIndex) const;
|
||||
|
||||
std::unique_ptr<DescriptorSet> CreateDescriptorSet(DescriptorPool& descriptorPool, int setIndex) const;
|
||||
|
||||
private:
|
||||
void InitializeDescriptorSetLayout(const PipelineCreator& creator);
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Copium
|
||||
PipelineCreator::PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader)
|
||||
: vertexShader{vertexShader},
|
||||
fragmentShader{fragmentShader},
|
||||
shaderReflector{vertexShader, fragmentShader},
|
||||
renderPass{renderPass}
|
||||
{
|
||||
AddShaderDescription();
|
||||
@@ -39,8 +40,7 @@ namespace Copium
|
||||
|
||||
void PipelineCreator::AddShaderDescription()
|
||||
{
|
||||
ShaderReflector reflector{vertexShader, fragmentShader};
|
||||
for (auto& binding : reflector.bindings)
|
||||
for (auto& binding : shaderReflector.bindings)
|
||||
{
|
||||
descriptorSetLayouts[binding.set].emplace_back(DescriptorSetBinding{binding.binding, GetDescriptorType(binding.bindingType), binding.arraySize, GetShaderStageFlags(binding.shaderType)});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/pipeline/VertexDescriptor.h"
|
||||
#include "copium/util/ShaderReflector.h"
|
||||
#include "copium/pipeline/ShaderReflector.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
@@ -24,6 +24,8 @@ namespace Copium
|
||||
|
||||
std::string vertexShader;
|
||||
std::string fragmentShader;
|
||||
|
||||
ShaderReflector shaderReflector;
|
||||
VertexDescriptor vertexDescriptor{};
|
||||
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
VkCullModeFlags cullMode = VK_CULL_MODE_FRONT_BIT;
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#include "copium/pipeline/ShaderBinding.h"
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
bool ShaderBinding::operator<(const ShaderBinding& rhs) const
|
||||
{
|
||||
if (set != rhs.set)
|
||||
return set < rhs.set;
|
||||
return binding < rhs.binding;
|
||||
}
|
||||
|
||||
uint32_t ShaderBinding::GetUniformOffset(const std::string& uniform) const
|
||||
{
|
||||
// TODO: Caching?
|
||||
uint32_t offset = 0;
|
||||
for (auto& uniformElem : uniforms)
|
||||
{
|
||||
if (uniformElem.second == uniform)
|
||||
return offset;
|
||||
offset += GetUniformTypeOffset(uniformElem.first);
|
||||
}
|
||||
CP_ABORT("GetUniformOffset : Uniform not found=%s", uniform);
|
||||
}
|
||||
|
||||
uint32_t ShaderBinding::GetUniformSize(const std::string& uniform) const
|
||||
{
|
||||
for (auto& uniformElem : uniforms)
|
||||
{
|
||||
if (uniformElem.second == uniform)
|
||||
return GetUniformTypeSize(uniformElem.first);
|
||||
}
|
||||
CP_ABORT("GetUniformSize : Uniform not found=%s", uniform);
|
||||
}
|
||||
|
||||
UniformType ShaderBinding::GetUniformType(const std::string& uniform) const
|
||||
{
|
||||
for (auto& uniformElem : uniforms)
|
||||
{
|
||||
if (uniformElem.second == uniform)
|
||||
return uniformElem.first;
|
||||
}
|
||||
CP_ABORT("GetUniformType : Uniform not found=%s", uniform);
|
||||
}
|
||||
|
||||
uint32_t ShaderBinding::GetUniformBufferSize() const
|
||||
{
|
||||
CP_ASSERT(bindingType == BindingType::UniformBuffer, "GetUniformBufferSize : BindingType is not UniformBuffer");
|
||||
|
||||
uint32_t size = 0;
|
||||
for (auto& uniform : uniforms)
|
||||
{
|
||||
size += GetUniformTypeOffset(uniform.first);
|
||||
}
|
||||
|
||||
// alignas(64)
|
||||
if (size % 64 != 0)
|
||||
size += 64 - (size % 64);
|
||||
return size;
|
||||
}
|
||||
|
||||
uint32_t ShaderBinding::GetUniformTypeSize(UniformType type) const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case UniformType::Mat3:
|
||||
return 4 * 9; // glm::mat3
|
||||
case UniformType::Mat4:
|
||||
return 4 * 16; // glm::mat4
|
||||
case UniformType::Vec2:
|
||||
return 4 * 2; // glm::vec2
|
||||
case UniformType::Vec3:
|
||||
return 4 * 3; // glm::vec3
|
||||
case UniformType::Vec4:
|
||||
return 4 * 4; // glm::vec4
|
||||
case UniformType::Int:
|
||||
return 4; // int
|
||||
case UniformType::Float:
|
||||
return 4; // float
|
||||
default:
|
||||
CP_ABORT("GetUniformBufferSize : Unhandled switch case");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ShaderBinding::GetUniformTypeOffset(UniformType type) const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case UniformType::Mat3:
|
||||
return 64; // alignas(16) glm::mat3
|
||||
case UniformType::Mat4:
|
||||
return 64; // alignas(16) glm::mat4
|
||||
case UniformType::Vec2:
|
||||
return 16; // alignas(16) glm::vec2
|
||||
case UniformType::Vec3:
|
||||
return 16; // alignas(16) glm::vec2
|
||||
case UniformType::Vec4:
|
||||
return 16; // alignas(16) glm::vec2
|
||||
case UniformType::Int:
|
||||
return 16; // alignas(16) glm::vec2
|
||||
case UniformType::Float:
|
||||
return 16; // alignas(16) glm::vec2
|
||||
default:
|
||||
CP_ABORT("GetUniformBufferSize : Unhandled switch case");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
enum class BindingType
|
||||
{
|
||||
Sampler2D, UniformBuffer
|
||||
};
|
||||
|
||||
enum class ShaderType
|
||||
{
|
||||
Vertex, Fragment
|
||||
};
|
||||
|
||||
enum class UniformType
|
||||
{
|
||||
Mat3, Mat4, Vec2, Vec3, Vec4, Float, Int
|
||||
};
|
||||
|
||||
struct ShaderBinding
|
||||
{
|
||||
std::string name;
|
||||
uint32_t set;
|
||||
uint32_t binding;
|
||||
uint32_t arraySize;
|
||||
BindingType bindingType;
|
||||
ShaderType shaderType;
|
||||
std::vector<std::pair<UniformType, std::string>> uniforms;
|
||||
|
||||
// TODO: Maybe store a cache mapping the std::string to the offset in the uniform buffer?
|
||||
|
||||
bool operator<(const ShaderBinding& rhs) const;
|
||||
|
||||
uint32_t GetUniformOffset (const std::string& uniform) const;
|
||||
uint32_t GetUniformSize(const std::string& uniform) const;
|
||||
UniformType GetUniformType(const std::string& uniform) const;
|
||||
uint32_t GetUniformBufferSize() const;
|
||||
private:
|
||||
uint32_t GetUniformTypeSize(UniformType type) const;
|
||||
uint32_t GetUniformTypeOffset(UniformType type) const;
|
||||
};
|
||||
}
|
||||
+30
-4
@@ -1,4 +1,4 @@
|
||||
#include "copium/util/ShaderReflector.h"
|
||||
#include "copium/pipeline/ShaderReflector.h"
|
||||
|
||||
#include "copium/util/FileSystem.h"
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace Copium
|
||||
|
||||
std::string_view type = ParseWord(str, index);
|
||||
ParseWhitespace(str, index);
|
||||
if (str[index] == '{') ParseUniformBuffer(str, index);
|
||||
if (str[index] == '{') ParseUniformBuffer(str, index, shaderBinding);
|
||||
ParseWhitespace(str, index);
|
||||
std::string_view name = ParseWord(str, index);
|
||||
shaderBinding.name = name;
|
||||
@@ -117,9 +117,35 @@ namespace Copium
|
||||
return std::string_view(&str[start], index - start);
|
||||
}
|
||||
|
||||
void ShaderReflector::ParseUniformBuffer(const std::string& str, int& index)
|
||||
void ShaderReflector::ParseUniformBuffer(const std::string& str, int& index, ShaderBinding& binding)
|
||||
{
|
||||
while (str[index] != '}' && index < str.size()) index++;
|
||||
index++;
|
||||
ParseWhitespace(str, index);
|
||||
while (str[index] != '}')
|
||||
{
|
||||
std::string_view type = ParseWord(str, index);
|
||||
ParseWhitespace(str, index);
|
||||
std::string_view name = ParseWord(str, index); // uniform name
|
||||
if (type == "mat3")
|
||||
binding.uniforms.emplace_back(UniformType::Mat3, std::string(name));
|
||||
else if (type == "mat4")
|
||||
binding.uniforms.emplace_back(UniformType::Mat4, std::string(name));
|
||||
else if (type == "vec2")
|
||||
binding.uniforms.emplace_back(UniformType::Vec2, std::string(name));
|
||||
else if (type == "vec3")
|
||||
binding.uniforms.emplace_back(UniformType::Vec3, std::string(name));
|
||||
else if (type == "vec4")
|
||||
binding.uniforms.emplace_back(UniformType::Vec4, std::string(name));
|
||||
else if (type == "float")
|
||||
binding.uniforms.emplace_back(UniformType::Float, std::string(name));
|
||||
else if (type == "int")
|
||||
binding.uniforms.emplace_back(UniformType::Int, std::string(name));
|
||||
else
|
||||
CP_ABORT("Unsupported uniform type=%s", std::string(type).c_str());
|
||||
ParseWhitespace(str, index);
|
||||
index++; // ";"
|
||||
ParseWhitespace(str, index);
|
||||
}
|
||||
if (index < str.size()) index++; // go past "}"
|
||||
}
|
||||
}
|
||||
+3
-29
@@ -1,38 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/pipeline/ShaderBinding.h"
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
enum class BindingType
|
||||
{
|
||||
Sampler2D, UniformBuffer
|
||||
};
|
||||
|
||||
enum class ShaderType
|
||||
{
|
||||
Vertex, Fragment
|
||||
};
|
||||
|
||||
struct ShaderBinding
|
||||
{
|
||||
std::string name;
|
||||
uint32_t set;
|
||||
uint32_t binding;
|
||||
uint32_t arraySize;
|
||||
BindingType bindingType;
|
||||
ShaderType shaderType;
|
||||
|
||||
bool operator<(const ShaderBinding& rhs) const
|
||||
{
|
||||
if (set != rhs.set)
|
||||
return set < rhs.set;
|
||||
if (binding != rhs.binding)
|
||||
return binding < rhs.binding;
|
||||
}
|
||||
};
|
||||
|
||||
class ShaderReflector
|
||||
{
|
||||
public:
|
||||
@@ -46,6 +20,6 @@ namespace Copium
|
||||
void ParseWhitespace(const std::string& str, int& index);
|
||||
void ParseLayout(const std::string& str, int& index, ShaderType type);
|
||||
std::string_view ParseWord(const std::string& str, int& index);
|
||||
void ParseUniformBuffer(const std::string& str, int& index);
|
||||
void ParseUniformBuffer(const std::string& str, int& index, ShaderBinding& binding);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "copium/renderer/Batch.h"
|
||||
|
||||
#include "copium/renderer/RendererVertex.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Batch::Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers)
|
||||
: vulkan{vulkan},
|
||||
pipeline{pipeline},
|
||||
vertexBuffer{vulkan, RendererVertex::GetDescriptor(), vertexCount},
|
||||
descriptorSet{pipeline.CreateDescriptorSet(descriptorPool, 0)}
|
||||
{
|
||||
descriptorSet->SetSamplers(samplers, 0);
|
||||
}
|
||||
|
||||
RendererVertexBuffer& Batch::GetVertexBuffer()
|
||||
{
|
||||
return vertexBuffer;
|
||||
}
|
||||
|
||||
DescriptorSet& Batch::GetDescriptorSet()
|
||||
{
|
||||
return *descriptorSet;
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -7,17 +7,17 @@
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class DrawCall
|
||||
class Batch
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(DrawCall);
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(Batch);
|
||||
private:
|
||||
Vulkan& vulkan;
|
||||
Pipeline& pipeline;
|
||||
|
||||
RendererVertexBuffer vertexBuffer;
|
||||
DescriptorSet descriptorSet;
|
||||
std::unique_ptr<DescriptorSet> descriptorSet;
|
||||
public:
|
||||
DrawCall(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
|
||||
Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
|
||||
RendererVertexBuffer& GetVertexBuffer();
|
||||
DescriptorSet& GetDescriptorSet();
|
||||
};
|
||||
@@ -1,25 +0,0 @@
|
||||
#include "copium/renderer/DrawCall.h"
|
||||
|
||||
#include "copium/renderer/RendererVertex.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
DrawCall::DrawCall(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers)
|
||||
: vulkan{vulkan},
|
||||
pipeline{pipeline},
|
||||
vertexBuffer{vulkan, RendererVertex::GetDescriptor(), vertexCount},
|
||||
descriptorSet{vulkan, descriptorPool, pipeline.GetDescriptorSetLayout(0)}
|
||||
{
|
||||
descriptorSet.SetSamplers(samplers, 0);
|
||||
}
|
||||
|
||||
RendererVertexBuffer& DrawCall::GetVertexBuffer()
|
||||
{
|
||||
return vertexBuffer;
|
||||
}
|
||||
|
||||
DescriptorSet& DrawCall::GetDescriptorSet()
|
||||
{
|
||||
return descriptorSet;
|
||||
}
|
||||
}
|
||||
@@ -22,24 +22,24 @@ namespace Copium
|
||||
InitializeGraphicsPipeline(renderPass);
|
||||
}
|
||||
|
||||
void Renderer::Quad(const glm::vec2& from, const glm::vec2& to, const glm::vec3& color)
|
||||
void Renderer::Quad(const glm::vec2& pos, const glm::vec2& size, const glm::vec3& color)
|
||||
{
|
||||
AllocateQuad();
|
||||
AddVertex(glm::vec2{from.x, to.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(to, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(glm::vec2{to.x, from.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(from, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(glm::vec2{pos.x, pos.y + size.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(pos + size, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(glm::vec2{pos.x + size.x, pos.y}, color, -1, glm::vec2{0, 0});
|
||||
AddVertex(pos, color, -1, glm::vec2{0, 0});
|
||||
}
|
||||
|
||||
|
||||
void Renderer::Quad(const glm::vec2& from, const glm::vec2& to, const Sampler& sampler, const glm::vec2& texCoord1, const glm::vec2& texCoord2)
|
||||
void Renderer::Quad(const glm::vec2& pos, const glm::vec2& size, const Sampler& sampler, const glm::vec2& texCoord1, const glm::vec2& texCoord2)
|
||||
{
|
||||
AllocateQuad();
|
||||
int texIndex = AllocateSampler(sampler);
|
||||
AddVertex(glm::vec2{from.x, to.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord1.x, texCoord2.y});
|
||||
AddVertex(to, glm::vec3{1,1,1}, texIndex, texCoord2);
|
||||
AddVertex(glm::vec2{to.x, from.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord2.x, texCoord1.y});
|
||||
AddVertex(from, glm::vec3{1,1,1}, texIndex, texCoord1);
|
||||
AddVertex(glm::vec2{pos.x, pos.y + size.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord1.x, texCoord2.y});
|
||||
AddVertex(pos + size, glm::vec3{1,1,1}, texIndex, texCoord2);
|
||||
AddVertex(glm::vec2{pos.x + size.x, pos.y}, glm::vec3{1, 1, 1}, texIndex, glm::vec2{texCoord2.x, texCoord1.y});
|
||||
AddVertex(pos, glm::vec3{1,1,1}, texIndex, texCoord1);
|
||||
}
|
||||
|
||||
void Renderer::AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord)
|
||||
@@ -56,8 +56,8 @@ namespace Copium
|
||||
{
|
||||
graphicsPipeline->Bind(commandBuffer);
|
||||
ibo.Bind(commandBuffer);
|
||||
drawCallIndex = -1;
|
||||
NextDrawCall();
|
||||
batchIndex = -1;
|
||||
NextBatch();
|
||||
currentCommandBuffer = &commandBuffer;
|
||||
}
|
||||
|
||||
@@ -66,6 +66,16 @@ namespace Copium
|
||||
Flush();
|
||||
}
|
||||
|
||||
Pipeline& Renderer::GetGraphicsPipeline()
|
||||
{
|
||||
return *graphicsPipeline;
|
||||
}
|
||||
|
||||
void Renderer::SetDescriptorSet(const DescriptorSet& descriptorSet)
|
||||
{
|
||||
graphicsPipeline->SetDescriptorSet(descriptorSet);
|
||||
}
|
||||
|
||||
void Renderer::InitializeIndexBuffer()
|
||||
{
|
||||
CP_ASSERT(MAX_NUM_INDICES < std::numeric_limits<uint16_t>::max(), "Renderer : Maximum number of indices too big");
|
||||
@@ -104,9 +114,9 @@ namespace Copium
|
||||
if (textureCount == MAX_NUM_TEXTURES)
|
||||
{
|
||||
Flush();
|
||||
NextDrawCall();
|
||||
NextBatch();
|
||||
}
|
||||
currentDrawCall->GetDescriptorSet().SetSampler(sampler, 0, vulkan.GetSwapChain().GetFlightIndex(), textureCount);
|
||||
batches[batchIndex]->GetDescriptorSet().SetSamplerDynamic(sampler, 0, textureCount);
|
||||
samplers[textureCount] = &sampler;
|
||||
textureCount++;
|
||||
return textureCount - 1;
|
||||
@@ -117,30 +127,30 @@ namespace Copium
|
||||
if (quadCount + 1 > MAX_NUM_QUADS)
|
||||
{
|
||||
Flush();
|
||||
NextDrawCall();
|
||||
NextBatch();
|
||||
}
|
||||
quadCount++;
|
||||
}
|
||||
|
||||
void Renderer::Flush()
|
||||
{
|
||||
currentDrawCall->GetVertexBuffer().Unmap();
|
||||
currentDrawCall->GetVertexBuffer().Bind(*currentCommandBuffer);
|
||||
graphicsPipeline->SetDescriptorSet(0, currentDrawCall->GetDescriptorSet());
|
||||
batches[batchIndex]->GetVertexBuffer().Unmap();
|
||||
batches[batchIndex]->GetVertexBuffer().Bind(*currentCommandBuffer);
|
||||
graphicsPipeline->SetDescriptorSet(batches[batchIndex]->GetDescriptorSet());
|
||||
graphicsPipeline->BindDescriptorSets(*currentCommandBuffer);
|
||||
ibo.Draw(*currentCommandBuffer, quadCount * 6);
|
||||
}
|
||||
|
||||
void Renderer::NextDrawCall()
|
||||
void Renderer::NextBatch()
|
||||
{
|
||||
drawCallIndex++;
|
||||
if (drawCallIndex >= drawCalls.size())
|
||||
batchIndex++;
|
||||
if (batchIndex >= batches.size())
|
||||
{
|
||||
drawCalls.emplace_back(std::make_unique<DrawCall>(vulkan, *graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
|
||||
batches.emplace_back(std::make_unique<Batch>(vulkan, *graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
|
||||
}
|
||||
currentDrawCall = drawCalls[drawCallIndex].get();
|
||||
mappedVertexBuffer = (char*)currentDrawCall->GetVertexBuffer().Map() + currentDrawCall->GetVertexBuffer().GetPosition(vulkan.GetSwapChain().GetFlightIndex());
|
||||
mappedVertexBuffer = (char*)batches[batchIndex]->GetVertexBuffer().Map() + batches[batchIndex]->GetVertexBuffer().GetPosition(vulkan.GetSwapChain().GetFlightIndex());
|
||||
quadCount = 0;
|
||||
textureCount = 0;
|
||||
std::fill(samplers.begin(), samplers.end(), &emptyTexture);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "copium/buffer/RendererVertexBuffer.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/pipeline/Pipeline.h"
|
||||
#include "copium/renderer/DrawCall.h"
|
||||
#include "copium/renderer/Batch.h"
|
||||
#include "copium/sampler/Texture2D.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
@@ -24,13 +24,12 @@ namespace Copium
|
||||
IndexBuffer ibo;
|
||||
Texture2D emptyTexture;
|
||||
std::unique_ptr<Pipeline> graphicsPipeline;
|
||||
std::vector<std::unique_ptr<DrawCall>> drawCalls;
|
||||
std::vector<std::unique_ptr<Batch>> batches;
|
||||
|
||||
// Temporary data during a render
|
||||
CommandBuffer* currentCommandBuffer;
|
||||
DrawCall* currentDrawCall;
|
||||
std::vector<const Sampler*> samplers;
|
||||
int drawCallIndex;
|
||||
int batchIndex;
|
||||
int quadCount;
|
||||
int textureCount;
|
||||
void* mappedVertexBuffer;
|
||||
@@ -42,6 +41,9 @@ namespace Copium
|
||||
|
||||
void Begin(CommandBuffer& commandBuffer);
|
||||
void End();
|
||||
|
||||
Pipeline& GetGraphicsPipeline();
|
||||
void SetDescriptorSet(const DescriptorSet& descriptorSet);
|
||||
private:
|
||||
void InitializeIndexBuffer();
|
||||
void InitializeGraphicsPipeline(VkRenderPass renderPass);
|
||||
@@ -49,7 +51,7 @@ namespace Copium
|
||||
int AllocateSampler(const Sampler& sampler);
|
||||
void AllocateQuad();
|
||||
void Flush();
|
||||
void NextDrawCall();
|
||||
void NextBatch();
|
||||
|
||||
void AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user