From 9faec15fd6cb7ac42f344e6d7bf5cdbb08634434 Mon Sep 17 00:00:00 2001 From: Thraix Date: Fri, 24 Mar 2023 22:27:03 +0100 Subject: [PATCH] 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 --- CopiumEngine/CopiumEngine.vcxproj | 10 +- CopiumEngine/CopiumEngine.vcxproj.filters | 14 ++- CopiumEngine/res/shaders/renderer.vert | 10 +- .../src/copium/buffer/UniformBuffer.cpp | 62 +++++++++- .../src/copium/buffer/UniformBuffer.h | 24 ++-- CopiumEngine/src/copium/core/Application.cpp | 70 ++++++------ CopiumEngine/src/copium/core/Application.h | 7 +- .../src/copium/pipeline/DescriptorSet.cpp | 48 ++++++-- .../src/copium/pipeline/DescriptorSet.h | 16 ++- CopiumEngine/src/copium/pipeline/Pipeline.cpp | 20 +++- CopiumEngine/src/copium/pipeline/Pipeline.h | 6 +- .../src/copium/pipeline/PipelineCreator.cpp | 4 +- .../src/copium/pipeline/PipelineCreator.h | 4 +- .../src/copium/pipeline/ShaderBinding.cpp | 108 ++++++++++++++++++ .../src/copium/pipeline/ShaderBinding.h | 45 ++++++++ .../{util => pipeline}/ShaderReflector.cpp | 34 +++++- .../{util => pipeline}/ShaderReflector.h | 32 +----- CopiumEngine/src/copium/renderer/Batch.cpp | 25 ++++ .../copium/renderer/{DrawCall.h => Batch.h} | 8 +- CopiumEngine/src/copium/renderer/DrawCall.cpp | 25 ---- CopiumEngine/src/copium/renderer/Renderer.cpp | 58 ++++++---- CopiumEngine/src/copium/renderer/Renderer.h | 12 +- 22 files changed, 469 insertions(+), 173 deletions(-) create mode 100644 CopiumEngine/src/copium/pipeline/ShaderBinding.cpp create mode 100644 CopiumEngine/src/copium/pipeline/ShaderBinding.h rename CopiumEngine/src/copium/{util => pipeline}/ShaderReflector.cpp (73%) rename CopiumEngine/src/copium/{util => pipeline}/ShaderReflector.h (52%) create mode 100644 CopiumEngine/src/copium/renderer/Batch.cpp rename CopiumEngine/src/copium/renderer/{DrawCall.h => Batch.h} (61%) delete mode 100644 CopiumEngine/src/copium/renderer/DrawCall.cpp diff --git a/CopiumEngine/CopiumEngine.vcxproj b/CopiumEngine/CopiumEngine.vcxproj index 72a6a62..4490e3e 100644 --- a/CopiumEngine/CopiumEngine.vcxproj +++ b/CopiumEngine/CopiumEngine.vcxproj @@ -171,7 +171,8 @@ - + + @@ -194,7 +195,7 @@ - + @@ -208,7 +209,8 @@ - + + @@ -234,7 +236,7 @@ - + diff --git a/CopiumEngine/CopiumEngine.vcxproj.filters b/CopiumEngine/CopiumEngine.vcxproj.filters index 92cb79d..502c68b 100644 --- a/CopiumEngine/CopiumEngine.vcxproj.filters +++ b/CopiumEngine/CopiumEngine.vcxproj.filters @@ -120,10 +120,13 @@ Source Files - + Source Files - + + Source Files + + Source Files @@ -236,10 +239,13 @@ Header Files - + Header Files - + + Header Files + + Header Files diff --git a/CopiumEngine/res/shaders/renderer.vert b/CopiumEngine/res/shaders/renderer.vert index 8f4a857..c7fdfa9 100644 --- a/CopiumEngine/res/shaders/renderer.vert +++ b/CopiumEngine/res/shaders/renderer.vert @@ -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; diff --git a/CopiumEngine/src/copium/buffer/UniformBuffer.cpp b/CopiumEngine/src/copium/buffer/UniformBuffer.cpp index a9dbf52..ef362c9 100644 --- a/CopiumEngine/src/copium/buffer/UniformBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/UniformBuffer.cpp @@ -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()); + } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/buffer/UniformBuffer.h b/CopiumEngine/src/copium/buffer/UniformBuffer.h index 6ab9498..1e0104c 100644 --- a/CopiumEngine/src/copium/buffer/UniformBuffer.h +++ b/CopiumEngine/src/copium/buffer/UniformBuffer.h @@ -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 #include namespace Copium @@ -12,20 +14,22 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer); + ShaderBinding binding; + std::vector buffer; + public: - UniformBuffer(Vulkan& vulkan, VkDeviceSize size); + UniformBuffer(Vulkan& vulkan, ShaderBinding binding); VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const; - template - 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 - 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()); - } } diff --git a/CopiumEngine/src/copium/core/Application.cpp b/CopiumEngine/src/copium/core/Application.cpp index 6f1388c..e9da293 100644 --- a/CopiumEngine/src/copium/core/Application.cpp +++ b/CopiumEngine/src/copium/core/Application.cpp @@ -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(*vulkan, vulkan->GetSwapChain().GetExtent().width, vulkan->GetSwapChain().GetExtent().height); } + void Application::InitializeRenderer() + { + renderer = std::make_unique(*vulkan, framebuffer->GetRenderPass(), *descriptorPool); + } + void Application::InitializeTextureSampler() { texture2D = std::make_unique(*vulkan, "res/textures/texture.png"); texture2D2 = std::make_unique(*vulkan, "res/textures/texture2.png"); } - void Application::InitializeUniformBuffer() - { - shaderUniformBuffer = std::make_unique(*vulkan, sizeof(ShaderUniform)); - } - void Application::InitializeDescriptorSets() { descriptorPool = std::make_unique(*vulkan); - descriptorSet = std::make_unique(*vulkan, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0)); - descriptorSet->SetUniformBuffer(*shaderUniformBuffer, 0); + descriptorSet = graphicsPipeline->CreateDescriptorSet(*descriptorPool, 0); descriptorSet->SetSampler(*texture2D, 1); - descriptorSetPassthrough = std::make_unique(*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(*vulkan, CommandBuffer::Type::Dynamic); } - void Application::InitializeRenderer() - { - renderer = std::make_unique(*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(); + } } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/core/Application.h b/CopiumEngine/src/copium/core/Application.h index 00deeea..be2f11c 100644 --- a/CopiumEngine/src/copium/core/Application.h +++ b/CopiumEngine/src/copium/core/Application.h @@ -22,19 +22,19 @@ namespace Copium private: std::unique_ptr vulkan; std::unique_ptr instance; + std::unique_ptr renderer; std::unique_ptr framebuffer; std::unique_ptr texture2D; std::unique_ptr texture2D2; - std::unique_ptr shaderUniformBuffer; std::unique_ptr descriptorPool; std::unique_ptr descriptorSet; std::unique_ptr descriptorSetPassthrough; + std::unique_ptr descriptorSetRenderer; std::unique_ptr graphicsPipeline; std::unique_ptr graphicsPipelinePassthrough; std::unique_ptr mesh; std::unique_ptr meshPassthrough; std::unique_ptr commandBuffer; - std::unique_ptr 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(); diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp index 41ce86d..221166d 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp @@ -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& 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 = std::make_unique(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()]; diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.h b/CopiumEngine/src/copium/pipeline/DescriptorSet.h index 4f405ac..ad0158d 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.h +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.h @@ -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 #include +#include +#include +#include + namespace Copium { class DescriptorSet final @@ -17,16 +24,21 @@ namespace Copium DescriptorPool& descriptorPool; VkDescriptorSetLayout descriptorSetLayout; + std::set bindings; std::vector descriptorSets; + std::map> uniformBuffers; public: - DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout); + DescriptorSet(Vulkan& vulkan, DescriptorPool& descriptorPool, VkDescriptorSetLayout descriptorSetLayout, const std::set& 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& sampler, uint32_t binding); + UniformBuffer& GetUniformBuffer(const std::string& uniformBuffer); + uint32_t GetSetIndex() const; + operator VkDescriptorSet() const; }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.cpp b/CopiumEngine/src/copium/pipeline/Pipeline.cpp index bfc0973..5a08d0b 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.cpp +++ b/CopiumEngine/src/copium/pipeline/Pipeline.cpp @@ -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 Pipeline::CreateDescriptorSet(DescriptorPool& descriptorPool, int setIndex) const { - return descriptorSetLayouts[setIndex]; + std::set bindings; + for (auto& binding : shaderReflector.bindings) + { + if (binding.set != setIndex) + continue; + bindings.emplace(binding); + } + + return std::make_unique(vulkan, descriptorPool, descriptorSetLayouts[setIndex], bindings); } void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator) diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.h b/CopiumEngine/src/copium/pipeline/Pipeline.h index 791fd9e..d41dd30 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.h +++ b/CopiumEngine/src/copium/pipeline/Pipeline.h @@ -17,6 +17,7 @@ namespace Copium private: Vulkan& vulkan; + ShaderReflector shaderReflector; std::vector descriptorSetLayouts{}; std::vector 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 CreateDescriptorSet(DescriptorPool& descriptorPool, int setIndex) const; private: void InitializeDescriptorSetLayout(const PipelineCreator& creator); diff --git a/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp b/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp index 129a434..c697166 100644 --- a/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp +++ b/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp @@ -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)}); } diff --git a/CopiumEngine/src/copium/pipeline/PipelineCreator.h b/CopiumEngine/src/copium/pipeline/PipelineCreator.h index 3ac20d7..1b2be0a 100644 --- a/CopiumEngine/src/copium/pipeline/PipelineCreator.h +++ b/CopiumEngine/src/copium/pipeline/PipelineCreator.h @@ -1,7 +1,7 @@ #pragma once #include "copium/pipeline/VertexDescriptor.h" -#include "copium/util/ShaderReflector.h" +#include "copium/pipeline/ShaderReflector.h" #include #include @@ -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; diff --git a/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp b/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp new file mode 100644 index 0000000..31f76bf --- /dev/null +++ b/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp @@ -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"); + } + } +} diff --git a/CopiumEngine/src/copium/pipeline/ShaderBinding.h b/CopiumEngine/src/copium/pipeline/ShaderBinding.h new file mode 100644 index 0000000..f95240c --- /dev/null +++ b/CopiumEngine/src/copium/pipeline/ShaderBinding.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include + +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> 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; + }; +} diff --git a/CopiumEngine/src/copium/util/ShaderReflector.cpp b/CopiumEngine/src/copium/pipeline/ShaderReflector.cpp similarity index 73% rename from CopiumEngine/src/copium/util/ShaderReflector.cpp rename to CopiumEngine/src/copium/pipeline/ShaderReflector.cpp index b6ab41c..0f76df3 100644 --- a/CopiumEngine/src/copium/util/ShaderReflector.cpp +++ b/CopiumEngine/src/copium/pipeline/ShaderReflector.cpp @@ -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 "}" } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/util/ShaderReflector.h b/CopiumEngine/src/copium/pipeline/ShaderReflector.h similarity index 52% rename from CopiumEngine/src/copium/util/ShaderReflector.h rename to CopiumEngine/src/copium/pipeline/ShaderReflector.h index 358e053..fd87727 100644 --- a/CopiumEngine/src/copium/util/ShaderReflector.h +++ b/CopiumEngine/src/copium/pipeline/ShaderReflector.h @@ -1,38 +1,12 @@ #pragma once +#include "copium/pipeline/ShaderBinding.h" + #include #include 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); }; } diff --git a/CopiumEngine/src/copium/renderer/Batch.cpp b/CopiumEngine/src/copium/renderer/Batch.cpp new file mode 100644 index 0000000..b92ef2a --- /dev/null +++ b/CopiumEngine/src/copium/renderer/Batch.cpp @@ -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 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; + } +} diff --git a/CopiumEngine/src/copium/renderer/DrawCall.h b/CopiumEngine/src/copium/renderer/Batch.h similarity index 61% rename from CopiumEngine/src/copium/renderer/DrawCall.h rename to CopiumEngine/src/copium/renderer/Batch.h index 937581c..2891dfc 100644 --- a/CopiumEngine/src/copium/renderer/DrawCall.h +++ b/CopiumEngine/src/copium/renderer/Batch.h @@ -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; public: - DrawCall(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers); + Batch(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers); RendererVertexBuffer& GetVertexBuffer(); DescriptorSet& GetDescriptorSet(); }; diff --git a/CopiumEngine/src/copium/renderer/DrawCall.cpp b/CopiumEngine/src/copium/renderer/DrawCall.cpp deleted file mode 100644 index 30048d8..0000000 --- a/CopiumEngine/src/copium/renderer/DrawCall.cpp +++ /dev/null @@ -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 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; - } -} diff --git a/CopiumEngine/src/copium/renderer/Renderer.cpp b/CopiumEngine/src/copium/renderer/Renderer.cpp index 899ff89..85a387a 100644 --- a/CopiumEngine/src/copium/renderer/Renderer.cpp +++ b/CopiumEngine/src/copium/renderer/Renderer.cpp @@ -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::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(vulkan, *graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers)); + batches.emplace_back(std::make_unique(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); } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/renderer/Renderer.h b/CopiumEngine/src/copium/renderer/Renderer.h index 360e83a..572ae7d 100644 --- a/CopiumEngine/src/copium/renderer/Renderer.h +++ b/CopiumEngine/src/copium/renderer/Renderer.h @@ -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 graphicsPipeline; - std::vector> drawCalls; + std::vector> batches; // Temporary data during a render CommandBuffer* currentCommandBuffer; - DrawCall* currentDrawCall; std::vector 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); };