Cleanup Renderer
This commit is contained in:
@@ -164,13 +164,16 @@
|
|||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="src\copium\buffer\RendererVertexBuffer.cpp" />
|
||||||
<ClCompile Include="src\copium\core\Application.cpp" />
|
<ClCompile Include="src\copium\core\Application.cpp" />
|
||||||
<ClCompile Include="src\copium\buffer\Buffer.cpp" />
|
<ClCompile Include="src\copium\buffer\Buffer.cpp" />
|
||||||
<ClCompile Include="src\copium\core\Device.cpp" />
|
<ClCompile Include="src\copium\core\Device.cpp" />
|
||||||
<ClCompile Include="src\copium\core\Vulkan.cpp" />
|
<ClCompile Include="src\copium\core\Vulkan.cpp" />
|
||||||
<ClCompile Include="src\copium\core\Window.cpp" />
|
<ClCompile Include="src\copium\core\Window.cpp" />
|
||||||
<ClCompile Include="src\copium\mesh\Mesh.cpp" />
|
<ClCompile Include="src\copium\mesh\Mesh.cpp" />
|
||||||
|
<ClCompile Include="src\copium\renderer\DrawCall.cpp" />
|
||||||
<ClCompile Include="src\copium\renderer\Renderer.cpp" />
|
<ClCompile Include="src\copium\renderer\Renderer.cpp" />
|
||||||
|
<ClCompile Include="src\copium\renderer\RendererVertex.cpp" />
|
||||||
<ClCompile Include="src\copium\sampler\ColorAttachment.cpp" />
|
<ClCompile Include="src\copium\sampler\ColorAttachment.cpp" />
|
||||||
<ClCompile Include="src\copium\buffer\CommandBuffer.cpp" />
|
<ClCompile Include="src\copium\buffer\CommandBuffer.cpp" />
|
||||||
<ClCompile Include="src\copium\buffer\CommandBufferScoped.cpp" />
|
<ClCompile Include="src\copium\buffer\CommandBufferScoped.cpp" />
|
||||||
@@ -199,11 +202,14 @@
|
|||||||
<ClCompile Include="src\copium\mesh\VertexPassthrough.cpp" />
|
<ClCompile Include="src\copium\mesh\VertexPassthrough.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="src\copium\buffer\RendererVertexBuffer.h" />
|
||||||
<ClInclude Include="src\copium\core\Device.h" />
|
<ClInclude Include="src\copium\core\Device.h" />
|
||||||
<ClInclude Include="src\copium\core\Vulkan.h" />
|
<ClInclude Include="src\copium\core\Vulkan.h" />
|
||||||
<ClInclude Include="src\copium\core\Window.h" />
|
<ClInclude Include="src\copium\core\Window.h" />
|
||||||
<ClInclude Include="src\copium\mesh\Mesh.h" />
|
<ClInclude Include="src\copium\mesh\Mesh.h" />
|
||||||
|
<ClInclude Include="src\copium\renderer\DrawCall.h" />
|
||||||
<ClInclude Include="src\copium\renderer\Renderer.h" />
|
<ClInclude Include="src\copium\renderer\Renderer.h" />
|
||||||
|
<ClInclude Include="src\copium\renderer\RendererVertex.h" />
|
||||||
<ClInclude Include="src\copium\sampler\DepthAttachment.h" />
|
<ClInclude Include="src\copium\sampler\DepthAttachment.h" />
|
||||||
<ClInclude Include="src\copium\core\Application.h" />
|
<ClInclude Include="src\copium\core\Application.h" />
|
||||||
<ClInclude Include="src\copium\sampler\ColorAttachment.h" />
|
<ClInclude Include="src\copium\sampler\ColorAttachment.h" />
|
||||||
|
|||||||
@@ -114,6 +114,15 @@
|
|||||||
<ClCompile Include="src\copium\renderer\Renderer.cpp">
|
<ClCompile Include="src\copium\renderer\Renderer.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\copium\renderer\RendererVertex.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\copium\buffer\RendererVertexBuffer.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\copium\renderer\DrawCall.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
|
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
|
||||||
@@ -218,6 +227,15 @@
|
|||||||
<ClInclude Include="src\copium\renderer\Renderer.h">
|
<ClInclude Include="src\copium\renderer\Renderer.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\copium\renderer\RendererVertex.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\copium\buffer\RendererVertexBuffer.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\copium\renderer\DrawCall.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="res\shaders\shader.frag" />
|
<None Include="res\shaders\shader.frag" />
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Copium
|
|||||||
|
|
||||||
void IndexBuffer::Draw(const CommandBuffer& commandBuffer, int indices)
|
void IndexBuffer::Draw(const CommandBuffer& commandBuffer, int indices)
|
||||||
{
|
{
|
||||||
CP_ASSERT(indices >= 0 && indices < indexCount, "Draw : amount of indices is out of range");
|
CP_ASSERT(indices > 0 && indices <= indexCount, "Draw : amount of indices is out of range");
|
||||||
vkCmdDrawIndexed(commandBuffer, indices, 1, 0, 0, 0);
|
vkCmdDrawIndexed(commandBuffer, indices, 1, 0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include "copium/buffer/RendererVertexBuffer.h"
|
||||||
|
|
||||||
|
#include "copium/core/SwapChain.h"
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
|
{
|
||||||
|
|
||||||
|
RendererVertexBuffer::RendererVertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount)
|
||||||
|
: Buffer{vulkan, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, descriptor.GetVertexSize() * vertexCount, SwapChain::MAX_FRAMES_IN_FLIGHT}
|
||||||
|
{}
|
||||||
|
|
||||||
|
void RendererVertexBuffer::Bind(const CommandBuffer& commandBuffer)
|
||||||
|
{
|
||||||
|
VkDeviceSize offset = GetPosition(vulkan.GetSwapChain().GetFlightIndex());
|
||||||
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &handle, &offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "copium/buffer/Buffer.h"
|
||||||
|
#include "copium/buffer/CommandBuffer.h"
|
||||||
|
#include "copium/core/Vulkan.h"
|
||||||
|
#include "copium/pipeline/VertexDescriptor.h"
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
|
{
|
||||||
|
class RendererVertexBuffer : public Buffer
|
||||||
|
{
|
||||||
|
CP_DELETE_COPY_AND_MOVE_CTOR(RendererVertexBuffer);
|
||||||
|
public:
|
||||||
|
RendererVertexBuffer(Vulkan& vulkan, const VertexDescriptor& descriptor, int vertexCount);
|
||||||
|
|
||||||
|
void Bind(const CommandBuffer& commandBuffer);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -172,11 +172,11 @@ namespace Copium
|
|||||||
{
|
{
|
||||||
for (int x = 0; x < 10; x++)
|
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, -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(-0.5, -0.5), glm::vec2{-0.1, 0.5}, *texture2D);
|
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.1, -0.5}, glm::vec2{0.5, 0.5}, *texture2D2);
|
||||||
renderer->End();
|
renderer->End();
|
||||||
|
|
||||||
framebuffer->Unbind(*commandBuffer);
|
framebuffer->Unbind(*commandBuffer);
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#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.AddSamplers(samplers, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
RendererVertexBuffer& DrawCall::GetVertexBuffer()
|
||||||
|
{
|
||||||
|
return vertexBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
DescriptorSet& DrawCall::GetDescriptorSet()
|
||||||
|
{
|
||||||
|
return descriptorSet;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "copium/buffer/RendererVertexBuffer.h"
|
||||||
|
#include "copium/pipeline/DescriptorSet.h"
|
||||||
|
#include "copium/pipeline/Pipeline.h"
|
||||||
|
#include "copium/sampler/Sampler.h"
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
|
{
|
||||||
|
class DrawCall
|
||||||
|
{
|
||||||
|
CP_DELETE_COPY_AND_MOVE_CTOR(DrawCall);
|
||||||
|
private:
|
||||||
|
Vulkan& vulkan;
|
||||||
|
Pipeline& pipeline;
|
||||||
|
|
||||||
|
RendererVertexBuffer vertexBuffer;
|
||||||
|
DescriptorSet descriptorSet;
|
||||||
|
public:
|
||||||
|
DrawCall(Vulkan& vulkan, Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
|
||||||
|
RendererVertexBuffer& GetVertexBuffer();
|
||||||
|
DescriptorSet& GetDescriptorSet();
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,41 +1,25 @@
|
|||||||
#include "copium/renderer/Renderer.h"
|
#include "copium/renderer/Renderer.h"
|
||||||
|
|
||||||
#include "copium/core/SwapChain.h"
|
#include "copium/core/SwapChain.h"
|
||||||
|
#include "copium/pipeline/PipelineCreator.h"
|
||||||
|
#include "copium/renderer/RendererVertex.h"
|
||||||
|
|
||||||
namespace Copium
|
namespace Copium
|
||||||
{
|
{
|
||||||
static constexpr int QUAD_COUNT = 10000;
|
static constexpr int MAX_NUM_QUADS = 10000;
|
||||||
static constexpr int MAX_NUM_VERTICES = 4 * QUAD_COUNT;
|
static constexpr int MAX_NUM_VERTICES = 4 * MAX_NUM_QUADS;
|
||||||
static constexpr int MAX_NUM_INDICES = 6 * QUAD_COUNT;
|
static constexpr int MAX_NUM_INDICES = 6 * MAX_NUM_QUADS;
|
||||||
static constexpr int MAX_NUM_TEXTURES = 32;
|
static constexpr int MAX_NUM_TEXTURES = 32;
|
||||||
|
|
||||||
Renderer::Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool)
|
Renderer::Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool)
|
||||||
: vulkan{vulkan},
|
: vulkan{vulkan},
|
||||||
descriptorPool{vulkan},
|
descriptorPool{vulkan},
|
||||||
ibo{vulkan, MAX_NUM_INDICES},
|
ibo{vulkan, MAX_NUM_INDICES},
|
||||||
emptyTexture{vulkan, {0, 0, 0, 0}, 1, 1},
|
emptyTexture{vulkan, {1, 0, 0, 0}, 1, 1},
|
||||||
samplers{MAX_NUM_TEXTURES, &emptyTexture}
|
samplers{MAX_NUM_TEXTURES, &emptyTexture}
|
||||||
{
|
{
|
||||||
CP_ASSERT(MAX_NUM_INDICES < std::numeric_limits<uint16_t>::max(), "Renderer : Maximum number of indices too big");
|
InitializeIndexBuffer();
|
||||||
|
InitializeGraphicsPipeline(renderPass);
|
||||||
|
|
||||||
std::vector<uint16_t> indices;
|
|
||||||
indices.resize(MAX_NUM_INDICES);
|
|
||||||
for (int i = 0; i < QUAD_COUNT; i++)
|
|
||||||
{
|
|
||||||
indices[i * 6] = i * 4;
|
|
||||||
indices[i * 6 + 1] = i * 4 + 1;
|
|
||||||
indices[i * 6 + 2] = i * 4 + 2;
|
|
||||||
indices[i * 6 + 3] = i * 4;
|
|
||||||
indices[i * 6 + 4] = i * 4 + 2;
|
|
||||||
indices[i * 6 + 5] = i * 4 + 3;
|
|
||||||
}
|
|
||||||
ibo.UpdateStaging(indices.data());
|
|
||||||
PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"};
|
|
||||||
creator.SetVertexDescriptor(Vertex::GetDescriptor());
|
|
||||||
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_NUM_TEXTURES, VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
||||||
creator.SetDepthTest(false);
|
|
||||||
graphicsPipeline = std::make_unique<Pipeline>(vulkan, creator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Quad(const glm::vec2& from, const glm::vec2& to, const glm::vec3& color)
|
void Renderer::Quad(const glm::vec2& from, const glm::vec2& to, const glm::vec3& color)
|
||||||
@@ -60,20 +44,20 @@ namespace Copium
|
|||||||
|
|
||||||
void Renderer::AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord)
|
void Renderer::AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord)
|
||||||
{
|
{
|
||||||
Vertex* vertex = (Vertex*)mappedVertexBuffer;
|
RendererVertex* vertex = (RendererVertex*)mappedVertexBuffer;
|
||||||
vertex->position = position;
|
vertex->position = position;
|
||||||
vertex->color = color;
|
vertex->color = color;
|
||||||
vertex->texCoord = texCoord;
|
vertex->texCoord = texCoord;
|
||||||
vertex->texIndex = texindex;
|
vertex->texIndex = texindex;
|
||||||
mappedVertexBuffer = (Vertex*)mappedVertexBuffer + 1;
|
mappedVertexBuffer = (RendererVertex*)mappedVertexBuffer + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Begin(CommandBuffer& commandBuffer)
|
void Renderer::Begin(CommandBuffer& commandBuffer)
|
||||||
{
|
{
|
||||||
graphicsPipeline->Bind(commandBuffer);
|
graphicsPipeline->Bind(commandBuffer);
|
||||||
ibo.Bind(commandBuffer);
|
ibo.Bind(commandBuffer);
|
||||||
vboIndex = -1;
|
drawCallIndex = -1;
|
||||||
NextVertexBuffer();
|
NextDrawCall();
|
||||||
currentCommandBuffer = &commandBuffer;
|
currentCommandBuffer = &commandBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,63 +66,83 @@ namespace Copium
|
|||||||
Flush();
|
Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Renderer::InitializeIndexBuffer()
|
||||||
|
{
|
||||||
|
CP_ASSERT(MAX_NUM_INDICES < std::numeric_limits<uint16_t>::max(), "Renderer : Maximum number of indices too big");
|
||||||
|
|
||||||
|
std::vector<uint16_t> indices;
|
||||||
|
indices.resize(MAX_NUM_INDICES);
|
||||||
|
for (int i = 0; i < MAX_NUM_QUADS; i++)
|
||||||
|
{
|
||||||
|
indices[i * 6] = i * 4;
|
||||||
|
indices[i * 6 + 1] = i * 4 + 1;
|
||||||
|
indices[i * 6 + 2] = i * 4 + 2;
|
||||||
|
indices[i * 6 + 3] = i * 4;
|
||||||
|
indices[i * 6 + 4] = i * 4 + 2;
|
||||||
|
indices[i * 6 + 5] = i * 4 + 3;
|
||||||
|
}
|
||||||
|
ibo.UpdateStaging(indices.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::InitializeGraphicsPipeline(VkRenderPass renderPass)
|
||||||
|
{
|
||||||
|
PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"};
|
||||||
|
creator.SetVertexDescriptor(RendererVertex::GetDescriptor());
|
||||||
|
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_NUM_TEXTURES, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
|
creator.SetDepthTest(false);
|
||||||
|
graphicsPipeline = std::make_unique<Pipeline>(vulkan, creator);
|
||||||
|
}
|
||||||
|
|
||||||
int Renderer::AllocateSampler(const Sampler& sampler)
|
int Renderer::AllocateSampler(const Sampler& sampler)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < texturesUsed; i++)
|
for (size_t i = 0; i < textureCount; i++)
|
||||||
{
|
{
|
||||||
if (*samplers[i] == sampler)
|
if (*samplers[i] == sampler)
|
||||||
{
|
{
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (texturesUsed == MAX_NUM_TEXTURES)
|
if (textureCount == MAX_NUM_TEXTURES)
|
||||||
{
|
{
|
||||||
Flush();
|
Flush();
|
||||||
NextVertexBuffer();
|
NextDrawCall();
|
||||||
}
|
}
|
||||||
currentDescriptorSet->AddSampler(sampler, 0, vulkan.GetSwapChain().GetFlightIndex(), texturesUsed);
|
currentDrawCall->GetDescriptorSet().AddSampler(sampler, 0, vulkan.GetSwapChain().GetFlightIndex(), textureCount);
|
||||||
samplers[texturesUsed] = &sampler;
|
samplers[textureCount] = &sampler;
|
||||||
texturesUsed++;
|
textureCount++;
|
||||||
return texturesUsed - 1;
|
return textureCount - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::AllocateQuad()
|
void Renderer::AllocateQuad()
|
||||||
{
|
{
|
||||||
if (vertexCount + 4 > MAX_NUM_VERTICES)
|
if (quadCount + 1 > MAX_NUM_QUADS)
|
||||||
{
|
{
|
||||||
|
CP_INFO("Flush");
|
||||||
Flush();
|
Flush();
|
||||||
NextVertexBuffer();
|
NextDrawCall();
|
||||||
}
|
}
|
||||||
vertexCount += 4;
|
quadCount++;
|
||||||
indexCount += 6;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Flush()
|
void Renderer::Flush()
|
||||||
{
|
{
|
||||||
currentVertexBuffer->Unmap();
|
currentDrawCall->GetVertexBuffer().Unmap();
|
||||||
VkBuffer buffer = *currentVertexBuffer;
|
currentDrawCall->GetVertexBuffer().Bind(*currentCommandBuffer);
|
||||||
VkDeviceSize offset = currentVertexBuffer->GetPosition(vulkan.GetSwapChain().GetFlightIndex());
|
graphicsPipeline->SetDescriptorSet(0, currentDrawCall->GetDescriptorSet());
|
||||||
vkCmdBindVertexBuffers(*currentCommandBuffer, 0, 1, &buffer, &offset);
|
|
||||||
graphicsPipeline->SetDescriptorSet(0, *currentDescriptorSet);
|
|
||||||
graphicsPipeline->BindDescriptorSets(*currentCommandBuffer);
|
graphicsPipeline->BindDescriptorSets(*currentCommandBuffer);
|
||||||
ibo.Draw(*currentCommandBuffer, indexCount);
|
ibo.Draw(*currentCommandBuffer, quadCount * 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::NextVertexBuffer()
|
void Renderer::NextDrawCall()
|
||||||
{
|
{
|
||||||
vboIndex++;
|
drawCallIndex++;
|
||||||
if (vboIndex >= vbos.size())
|
if (drawCallIndex >= drawCalls.size())
|
||||||
{
|
{
|
||||||
// Allocate new buffer since all existing buffers are full
|
drawCalls.emplace_back(std::make_unique<DrawCall>(vulkan, *graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
|
||||||
vbos.emplace_back(std::make_unique<Buffer>(vulkan, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeof(Vertex) * MAX_NUM_VERTICES, SwapChain::MAX_FRAMES_IN_FLIGHT));
|
|
||||||
descriptorSets.emplace_back(std::make_unique<DescriptorSet>(vulkan, descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0)));
|
|
||||||
descriptorSets.back()->AddSamplers(samplers, 0);
|
|
||||||
}
|
}
|
||||||
currentVertexBuffer = vbos[vboIndex].get();
|
currentDrawCall = drawCalls[drawCallIndex].get();
|
||||||
currentDescriptorSet = descriptorSets[vboIndex].get();
|
mappedVertexBuffer = (char*)currentDrawCall->GetVertexBuffer().Map() + currentDrawCall->GetVertexBuffer().GetPosition(vulkan.GetSwapChain().GetFlightIndex());
|
||||||
mappedVertexBuffer = (char*)currentVertexBuffer->Map() + currentVertexBuffer->GetPosition(vulkan.GetSwapChain().GetFlightIndex());
|
quadCount = 0;
|
||||||
vertexCount = 0;
|
textureCount = 0;
|
||||||
indexCount = 0;
|
|
||||||
texturesUsed = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#include "copium/buffer/CommandBuffer.h"
|
#include "copium/buffer/CommandBuffer.h"
|
||||||
#include "copium/buffer/IndexBuffer.h"
|
#include "copium/buffer/IndexBuffer.h"
|
||||||
#include "copium/buffer/VertexBuffer.h"
|
#include "copium/buffer/RendererVertexBuffer.h"
|
||||||
#include "copium/core/Vulkan.h"
|
#include "copium/core/Vulkan.h"
|
||||||
#include "copium/pipeline/Pipeline.h"
|
#include "copium/pipeline/Pipeline.h"
|
||||||
#include "copium/pipeline/PipelineCreator.h"
|
#include "copium/renderer/DrawCall.h"
|
||||||
#include "copium/sampler/Texture2D.h"
|
#include "copium/sampler/Texture2D.h"
|
||||||
#include "copium/util/Common.h"
|
#include "copium/util/Common.h"
|
||||||
|
|
||||||
@@ -17,25 +17,6 @@ namespace Copium
|
|||||||
class Renderer
|
class Renderer
|
||||||
{
|
{
|
||||||
CP_DELETE_COPY_AND_MOVE_CTOR(Renderer);
|
CP_DELETE_COPY_AND_MOVE_CTOR(Renderer);
|
||||||
|
|
||||||
struct Vertex
|
|
||||||
{
|
|
||||||
glm::vec2 position;
|
|
||||||
glm::vec3 color;
|
|
||||||
glm::vec2 texCoord;
|
|
||||||
int8_t texIndex;
|
|
||||||
|
|
||||||
static VertexDescriptor GetDescriptor()
|
|
||||||
{
|
|
||||||
VertexDescriptor descriptor{};
|
|
||||||
descriptor.AddAttribute(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, position), sizeof(Vertex));
|
|
||||||
descriptor.AddAttribute(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, color), sizeof(Vertex));
|
|
||||||
descriptor.AddAttribute(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, texCoord), sizeof(Vertex));
|
|
||||||
descriptor.AddAttribute(0, 3, VK_FORMAT_R8_SINT, offsetof(Vertex, texIndex), sizeof(Vertex));
|
|
||||||
return descriptor;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vulkan& vulkan;
|
Vulkan& vulkan;
|
||||||
|
|
||||||
@@ -43,19 +24,16 @@ namespace Copium
|
|||||||
IndexBuffer ibo;
|
IndexBuffer ibo;
|
||||||
Texture2D emptyTexture;
|
Texture2D emptyTexture;
|
||||||
std::unique_ptr<Pipeline> graphicsPipeline;
|
std::unique_ptr<Pipeline> graphicsPipeline;
|
||||||
std::vector<std::unique_ptr<Buffer>> vbos;
|
std::vector<std::unique_ptr<DrawCall>> drawCalls;
|
||||||
std::vector<std::unique_ptr<DescriptorSet>> descriptorSets;
|
|
||||||
|
|
||||||
// Temporary data during a render
|
// Temporary data during a render
|
||||||
CommandBuffer* currentCommandBuffer;
|
CommandBuffer* currentCommandBuffer;
|
||||||
Buffer* currentVertexBuffer;
|
DrawCall* currentDrawCall;
|
||||||
DescriptorSet* currentDescriptorSet;
|
|
||||||
std::vector<const Sampler*> samplers;
|
std::vector<const Sampler*> samplers;
|
||||||
int vboIndex;
|
int drawCallIndex;
|
||||||
int vertexCount;
|
int quadCount;
|
||||||
int indexCount;
|
int textureCount;
|
||||||
void* mappedVertexBuffer;
|
void* mappedVertexBuffer;
|
||||||
int texturesUsed;
|
|
||||||
public:
|
public:
|
||||||
Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool);
|
Renderer(Vulkan& vulkan, VkRenderPass renderPass, DescriptorPool& descriptorPool);
|
||||||
|
|
||||||
@@ -65,11 +43,13 @@ namespace Copium
|
|||||||
void Begin(CommandBuffer& commandBuffer);
|
void Begin(CommandBuffer& commandBuffer);
|
||||||
void End();
|
void End();
|
||||||
private:
|
private:
|
||||||
|
void InitializeIndexBuffer();
|
||||||
|
void InitializeGraphicsPipeline(VkRenderPass renderPass);
|
||||||
|
|
||||||
int AllocateSampler(const Sampler& sampler);
|
int AllocateSampler(const Sampler& sampler);
|
||||||
void AllocateQuad();
|
void AllocateQuad();
|
||||||
void Flush();
|
void Flush();
|
||||||
void NextVertexBuffer();
|
void NextDrawCall();
|
||||||
|
|
||||||
void AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord);
|
void AddVertex(const glm::vec2& position, const glm::vec3& color, int texindex, const glm::vec2& texCoord);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#include "copium/renderer/RendererVertex.h"
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
|
{
|
||||||
|
VertexDescriptor RendererVertex::GetDescriptor()
|
||||||
|
{
|
||||||
|
VertexDescriptor descriptor{};
|
||||||
|
descriptor.AddAttribute(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(RendererVertex, position), sizeof(RendererVertex));
|
||||||
|
descriptor.AddAttribute(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(RendererVertex, color), sizeof(RendererVertex));
|
||||||
|
descriptor.AddAttribute(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(RendererVertex, texCoord), sizeof(RendererVertex));
|
||||||
|
descriptor.AddAttribute(0, 3, VK_FORMAT_R8_SINT, offsetof(RendererVertex, texIndex), sizeof(RendererVertex));
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "copium/pipeline/VertexDescriptor.h"
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
namespace Copium
|
||||||
|
{
|
||||||
|
struct RendererVertex
|
||||||
|
{
|
||||||
|
glm::vec2 position;
|
||||||
|
glm::vec3 color;
|
||||||
|
glm::vec2 texCoord;
|
||||||
|
int8_t texIndex;
|
||||||
|
|
||||||
|
static VertexDescriptor GetDescriptor();
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user