Add file structure to code

- Rename project to CopiumEngine
This commit is contained in:
Thraix
2023-02-07 21:43:47 +01:00
parent ef4eb7dd2f
commit 827572eada
494 changed files with 205 additions and 195 deletions
+139
View File
@@ -0,0 +1,139 @@
#include "copium/buffer/Buffer.h"
namespace Copium
{
Buffer::Buffer(Instance& instance, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count)
: instance{instance}, size{size}, count{count}
{
VkBufferCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.size = size * (VkDeviceSize)count;
createInfo.usage = usage;
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
CP_VK_ASSERT(vkCreateBuffer(instance.GetDevice(), &createInfo, nullptr, &handle), "Buffer : Failed to initialize buffer");
VkMemoryRequirements memoryRequirements;
vkGetBufferMemoryRequirements(instance.GetDevice(), handle, &memoryRequirements);
VkMemoryAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocateInfo.allocationSize = memoryRequirements.size;
allocateInfo.memoryTypeIndex = instance.FindMemoryType(memoryRequirements.memoryTypeBits, properties);
CP_VK_ASSERT(vkAllocateMemory(instance.GetDevice(), &allocateInfo, nullptr, &memory), "Buffer : Failed to allocate buffer memory");
vkBindBufferMemory(instance.GetDevice(), handle, memory, 0);
}
Buffer::~Buffer()
{
vkFreeMemory(instance.GetDevice(), memory, nullptr);
vkDestroyBuffer(instance.GetDevice(), handle, nullptr);
}
void Buffer::Update(void* indexData, int index)
{
CP_ASSERT(index >= 0 && index < count, "Update : Index is outside of the buffer");
if (mappedData == nullptr)
{
void* data;
vkMapMemory(instance.GetDevice(), memory, index * size, size, 0, &data);
memcpy(data, indexData, size);
vkUnmapMemory(instance.GetDevice(), memory);
}
else
{
memcpy((char*)mappedData + index * size, indexData, size);
}
}
void Buffer::UpdateStaging(void* data)
{
VkDeviceSize bufferSize = size * count;
Buffer stagingBuffer{instance, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, 1};
stagingBuffer.Update(data, 0);
CopyBuffer(instance, stagingBuffer, *this, 0, bufferSize);
}
void Buffer::UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size)
{
Buffer stagingBuffer{instance, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, 1};
stagingBuffer.Update(data, 0);
CopyBuffer(instance, stagingBuffer, *this, offset, size);
}
void* Buffer::Map()
{
CP_ASSERT(mappedData == nullptr, "Map : Mapping an already mapped buffer");
vkMapMemory(instance.GetDevice(), memory, 0, size * count, 0, &mappedData);
return mappedData;
}
void Buffer::Unmap()
{
CP_ASSERT(mappedData != nullptr, "Unmap : Unmapping an already unmapped buffer");
vkUnmapMemory(instance.GetDevice(), memory);
mappedData = nullptr;
}
Buffer::operator VkBuffer() const
{
return handle;
}
VkDeviceSize Buffer::GetSize() const
{
return size;
}
VkDeviceSize Buffer::GetPosition(int index) const
{
CP_ASSERT(index >= 0 && index < count, "GetPosition : Index is outside of the buffer");
return size * (VkDeviceSize)index;
}
void Buffer::CopyBuffer(Instance& instance, const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size)
{
VkCommandBufferAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = instance.GetCommandPool();
allocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer;
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, &commandBuffer), "CopyBuffer : Failed to initialize command buffer");
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(commandBuffer, &beginInfo);
VkBufferCopy bufferCopy{};
bufferCopy.dstOffset = offset;
bufferCopy.srcOffset = 0;
bufferCopy.size = size;
vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &bufferCopy);
vkEndCommandBuffer(commandBuffer);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(instance.GetGraphicsQueue());
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), 1, &commandBuffer);
}
}
+40
View File
@@ -0,0 +1,40 @@
#pragma once
#include "copium/core/Instance.h"
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
class Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(Buffer);
protected:
Instance& instance;
VkDeviceMemory memory;
VkBuffer handle;
VkDeviceSize size;
int count;
void* mappedData = nullptr;
public:
Buffer(Instance& instance, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkDeviceSize size, int count);
virtual ~Buffer();
void Update(void* indexData, int index);
void UpdateStaging(void* data);
void UpdateStaging(void* data, VkDeviceSize offset, VkDeviceSize size);
void* Map();
void Unmap();
operator VkBuffer() const;
VkDeviceSize GetSize() const;
VkDeviceSize GetPosition(int index) const;
static void CopyBuffer(Instance& instance, const Buffer& srcBuffer, const Buffer& dstBuffer, VkDeviceSize offset, VkDeviceSize size);
};
}
@@ -0,0 +1,76 @@
#include "copium/buffer/CommandBuffer.h"
namespace Copium
{
CommandBuffer::CommandBuffer(Instance& instance, Type type)
: instance{instance}, type{type}
{
if (type == Type::Dynamic)
commandBuffers.resize(instance.GetMaxFramesInFlight());
else
commandBuffers.resize(1);
VkCommandBufferAllocateInfo allocateInfo{};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = instance.GetCommandPool();
allocateInfo.commandBufferCount = commandBuffers.size();
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, commandBuffers.data()), "CommandBuffer : Failed to allocate CommandBuffer");
}
CommandBuffer::~CommandBuffer()
{
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), commandBuffers.size(), commandBuffers.data());
}
// TODO: Test as constexpr function to see if it avoids the switch case
void CommandBuffer::Begin()
{
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = 0;
beginInfo.pInheritanceInfo = nullptr;
switch (type)
{
case Type::SingleUse:
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
currentCommandBuffer = commandBuffers.front();
break;
case Type::Dynamic:
currentCommandBuffer = commandBuffers[instance.GetFlightIndex()];
break;
default:
CP_ABORT("Begin : Unreachable switch case");
}
vkResetCommandBuffer(currentCommandBuffer, 0);
CP_VK_ASSERT(vkBeginCommandBuffer(currentCommandBuffer, &beginInfo), "Begin : Failed to begin command buffer");
}
void CommandBuffer::End()
{
vkEndCommandBuffer(currentCommandBuffer);
}
void CommandBuffer::Submit()
{
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &currentCommandBuffer;
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
// TODO: if singleUse?
vkQueueWaitIdle(instance.GetGraphicsQueue());
}
void CommandBuffer::SubmitAsGraphicsQueue()
{
instance.SubmitGraphicsQueue({currentCommandBuffer});
}
CommandBuffer::operator VkCommandBuffer() const
{
return currentCommandBuffer;
}
}
@@ -0,0 +1,37 @@
#pragma once
#include "copium/core/Instance.h"
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
class CommandBuffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
public:
enum class Type
{
SingleUse, Dynamic
};
private:
Instance& instance;
std::vector<VkCommandBuffer> commandBuffers;
const Type type;
VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE};
public:
CommandBuffer(Instance& instance, Type type);
virtual ~CommandBuffer();
void Begin();
void End();
void Submit();
void SubmitAsGraphicsQueue();
operator VkCommandBuffer() const;
};
}
@@ -0,0 +1,16 @@
#include "copium/buffer/CommandBufferScoped.h"
namespace Copium
{
CommandBufferScoped::CommandBufferScoped(Instance& instance)
: CommandBuffer{instance, Type::SingleUse}
{
CommandBuffer::Begin();
}
CommandBufferScoped::~CommandBufferScoped()
{
CommandBuffer::End();
CommandBuffer::Submit();
}
}
@@ -0,0 +1,17 @@
#pragma once
#include "copium/buffer/CommandBuffer.h"
#include "copium/core/Instance.h"
#include "copium/util/Common.h"
namespace Copium
{
class CommandBufferScoped final : public CommandBuffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped);
public:
CommandBufferScoped(Instance& instance);
~CommandBufferScoped() override;
};
}
@@ -0,0 +1,192 @@
#include "copium/buffer/Framebuffer.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/sampler/Image.h"
namespace Copium
{
Framebuffer::Framebuffer(Instance& instance, uint32_t width, uint32_t height)
: instance{instance}, width{width}, height{height}
{
InitializeImage();
InitializeDepthBuffer();
InitializeRenderPass();
InitializeFramebuffers();
}
Framebuffer::~Framebuffer()
{
for (auto& framebuffer : framebuffers)
vkDestroyFramebuffer(instance.GetDevice(), framebuffer, nullptr);
vkDestroyRenderPass(instance.GetDevice(), renderPass, nullptr);
}
void Framebuffer::Resize(uint32_t width, uint32_t height)
{
vkDeviceWaitIdle(instance.GetDevice());
this->width = width;
this->height = height;
colorAttachment.reset();
depthAttachment.reset();
for (auto&& framebuffer : framebuffers)
vkDestroyFramebuffer(instance.GetDevice(), framebuffer, nullptr);
InitializeImage();
InitializeDepthBuffer();
InitializeFramebuffers();
}
void Framebuffer::Bind(const CommandBuffer& commandBuffer)
{
std::vector<VkClearValue> clearValues{2};
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
clearValues[1].depthStencil = {1.0f, 0};
VkRenderPassBeginInfo renderPassBeginInfo{};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.framebuffer = framebuffers[instance.GetFlightIndex()];
renderPassBeginInfo.renderArea.offset = {0, 0};
renderPassBeginInfo.renderArea.extent = {width, height};
renderPassBeginInfo.clearValueCount = clearValues.size();
renderPassBeginInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = width;
viewport.height = height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
VkRect2D scissor{};
scissor.offset = {0, 0};
scissor.extent = {width, height};
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}
void Framebuffer::Unbind(const CommandBuffer& commandBuffer)
{
vkCmdEndRenderPass(commandBuffer);
}
VkRenderPass Framebuffer::GetRenderPass() const
{
return renderPass;
}
VkFramebuffer Framebuffer::GetFramebuffer() const
{
return framebuffers[instance.GetFlightIndex()];
}
const ColorAttachment& Framebuffer::GetColorAttachment() const
{
return *colorAttachment;
}
uint32_t Framebuffer::GetWidth() const
{
return width;
}
uint32_t Framebuffer::GetHeight() const
{
return height;
}
void Framebuffer::InitializeImage()
{
colorAttachment = std::make_unique<ColorAttachment>(instance, width, height);
}
void Framebuffer::InitializeDepthBuffer()
{
depthAttachment = std::make_unique<DepthAttachment>(instance, width, height);
}
void Framebuffer::InitializeRenderPass()
{
VkAttachmentDescription colorAttachment{};
colorAttachment.format = VK_FORMAT_R8G8B8A8_SRGB;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkAttachmentDescription depthAttachment{};
depthAttachment.format = Image::SelectDepthFormat(instance);
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference depthAttachmentRef{};
depthAttachmentRef.attachment = 1;
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
subpass.pDepthStencilAttachment = &depthAttachmentRef;
std::vector<VkSubpassDependency> dependencies{2};
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
dependencies[0].dstSubpass = 0;
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependencies[1].srcSubpass = 0;
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
std::vector<VkAttachmentDescription> attachments{colorAttachment, depthAttachment};
VkRenderPassCreateInfo renderPassCreateInfo{};
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassCreateInfo.attachmentCount = attachments.size();
renderPassCreateInfo.pAttachments = attachments.data();
renderPassCreateInfo.subpassCount = 1;
renderPassCreateInfo.pSubpasses = &subpass;
renderPassCreateInfo.dependencyCount = dependencies.size();
renderPassCreateInfo.pDependencies = dependencies.data();
CP_VK_ASSERT(vkCreateRenderPass(instance.GetDevice(), &renderPassCreateInfo, nullptr, &renderPass), "InitializeRenderPass : Failed to initialze render pass");
}
void Framebuffer::InitializeFramebuffers()
{
framebuffers.resize(instance.GetMaxFramesInFlight());
for (size_t i = 0; i < instance.GetMaxFramesInFlight(); ++i)
{
std::vector<VkImageView> attachments{colorAttachment->GetImageView(i), depthAttachment->GetImageView()};
VkFramebufferCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
createInfo.renderPass = renderPass;
createInfo.attachmentCount = attachments.size();
createInfo.pAttachments = attachments.data();
createInfo.width = width;
createInfo.height = height;
createInfo.layers = 1;
CP_VK_ASSERT(vkCreateFramebuffer(instance.GetDevice(), &createInfo, nullptr, &framebuffers[i]), "InitializeFramebuffers : Failed to initialize framebuffer");
}
}
}
@@ -0,0 +1,45 @@
#pragma once
#include "copium/core/Instance.h"
#include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/DepthAttachment.h"
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
class Framebuffer final
{
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
private:
Instance& instance;
std::unique_ptr<ColorAttachment> colorAttachment;
std::unique_ptr<DepthAttachment> depthAttachment;
std::vector<VkFramebuffer> framebuffers;
VkRenderPass renderPass;
uint32_t width;
uint32_t height;
public:
Framebuffer(Instance& instance, uint32_t width, uint32_t height);
~Framebuffer();
void Resize(uint32_t width, uint32_t height);
void Bind(const CommandBuffer& commandBuffer);
void Unbind(const CommandBuffer& commandBuffer);
VkRenderPass GetRenderPass() const;
VkFramebuffer GetFramebuffer() const;
const ColorAttachment& GetColorAttachment() const;
uint32_t GetWidth() const;
uint32_t GetHeight() const;
private:
void InitializeImage();
void InitializeDepthBuffer();
void InitializeRenderPass();
void InitializeFramebuffers();
};
}
@@ -0,0 +1,23 @@
#include "copium/buffer/IndexBuffer.h"
#include "copium/buffer/CommandBuffer.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
IndexBuffer::IndexBuffer(Instance& instance, int indexCount)
: Buffer{instance, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexCount * sizeof(uint16_t), 1},
indexCount{indexCount}
{}
void IndexBuffer::Bind(const CommandBuffer& commandBuffer)
{
vkCmdBindIndexBuffer(commandBuffer, handle, 0, VK_INDEX_TYPE_UINT16);
}
void IndexBuffer::Draw(const CommandBuffer& commandBuffer)
{
vkCmdDrawIndexed(commandBuffer, indexCount, 1, 0, 0, 0);
}
}
@@ -0,0 +1,20 @@
#pragma once
#include "copium/buffer/Buffer.h"
#include "copium/core/Instance.h"
#include "copium/util/Common.h"
namespace Copium
{
class IndexBuffer final : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(IndexBuffer);
private:
int indexCount;
public:
IndexBuffer(Instance& instance, int indexCount);
void Bind(const CommandBuffer& commandBuffer);
void Draw(const CommandBuffer& commandBuffer);
};
}
@@ -0,0 +1,17 @@
#include "copium/buffer/UniformBuffer.h"
namespace Copium
{
UniformBuffer::UniformBuffer(Instance& instance, VkDeviceSize size)
: Buffer{instance, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, size, instance.GetMaxFramesInFlight()}
{}
VkDescriptorBufferInfo UniformBuffer::GetDescriptorBufferInfo(int index) const
{
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = handle;
bufferInfo.offset = (VkDeviceSize)index * size;
bufferInfo.range = size;
return bufferInfo;
}
}
@@ -0,0 +1,30 @@
#pragma once
#include "copium/buffer/Buffer.h"
#include "copium/core/Instance.h"
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
class UniformBuffer final : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
public:
UniformBuffer(Instance& instance, VkDeviceSize size);
VkDescriptorBufferInfo GetDescriptorBufferInfo(int index) const;
template <typename T>
void Update(const T& t);
};
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, instance.GetFlightIndex());
}
}
@@ -0,0 +1,27 @@
#include "copium/buffer/VertexBuffer.h"
namespace Copium
{
VertexBuffer::VertexBuffer(Instance& instance, const VertexDescriptor& descriptor, int vertexCount)
: Buffer{instance, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, descriptor.GetVertexSize() * vertexCount, 1}
{
VkDeviceSize offset = 0;
for (auto&& binding : descriptor.GetBindings())
{
bindingOffsets.emplace_back(offset);
bindingSizes.emplace_back(binding.stride * vertexCount);
offset += binding.stride * vertexCount;
}
}
void VertexBuffer::Bind(const CommandBuffer& commandBuffer)
{
std::vector<VkBuffer> buffers{bindingOffsets.size(), handle};
vkCmdBindVertexBuffers(commandBuffer, 0, bindingOffsets.size(), buffers.data(), bindingOffsets.data());
}
void VertexBuffer::Update(uint32_t binding, void* data)
{
UpdateStaging(data, bindingOffsets[binding], bindingSizes[binding]);
}
}
@@ -0,0 +1,26 @@
#pragma once
#include "copium/buffer/Buffer.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/core/Instance.h"
#include "copium/pipeline/VertexDescriptor.h"
#include "copium/util/Common.h"
#include <vector>
#include <vulkan/vulkan.hpp>
namespace Copium
{
class VertexBuffer final : public Buffer
{
CP_DELETE_COPY_AND_MOVE_CTOR(VertexBuffer);
private:
std::vector<VkDeviceSize> bindingOffsets;
std::vector<VkDeviceSize> bindingSizes;
public:
VertexBuffer(Instance& instance, const VertexDescriptor& descriptor, int vertexCount);
void Bind(const CommandBuffer& commandBuffer);
void Update(uint32_t binding, void* data);
};
}