Skip to content

Commit

Permalink
Write Data Working
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Aug 18, 2021
1 parent 3b198f3 commit 075d2da
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 11 deletions.
54 changes: 51 additions & 3 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanDescriptorSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Arcane
{
VulkanDescriptorSet::VulkanDescriptorSet()
VulkanDescriptorSet::VulkanDescriptorSet(std::vector<VkBuffer> uniformBuffers, uint32_t size)
{
Application& app = Application::Get();
VulkanContext* context = static_cast<VulkanContext*>(Application::Get().GetWindow().GetContext());
VkDevice logicalDevice = static_cast<VulkanContext*>(app.GetWindow().GetContext())->GetDevice().GetLogicalDevice();

VulkanSwapChain& swapchain = context->GetSwapChain();

VkDescriptorSetLayoutBinding uboBinding{};
uboBinding.binding = 0;
uboBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Expand All @@ -18,11 +20,57 @@ namespace Arcane

VkDescriptorSetLayoutCreateInfo layoutInfo{};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount - 1;
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = &uboBinding;

if (vkCreateDescriptorSetLayout(logicalDevice, &layoutInfo, nullptr, &m_Layout) != VK_SUCCESS) {
printf("Uniform Descriptor Layout Not Created\n");
}

VkDescriptorPoolSize poolSize{};
poolSize.descriptorCount = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSize.descriptorCount = swapchain.GetSwapChainImagesSize();

VkDescriptorPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT;
poolInfo.poolSizeCount = 1;
poolInfo.pPoolSizes = &poolSize;
poolInfo.maxSets = swapchain.GetSwapChainImagesSize();

if (vkCreateDescriptorPool(logicalDevice, &poolInfo, nullptr, &m_DescriptorPool) != VK_SUCCESS) {
printf("Descriptor Pool Not Created\n");
}

std::vector<VkDescriptorSetLayout> layouts(swapchain.GetSwapChainImagesSize(), m_Layout);

VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = m_DescriptorPool;
allocInfo.descriptorSetCount = swapchain.GetSwapChainImagesSize();
allocInfo.pSetLayouts = layouts.data();

m_DescriptorSets.resize(swapchain.GetSwapChainImagesSize());
if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, m_DescriptorSets.data()) != VK_SUCCESS) {
printf("Failed to allocate descriptor sets\n");
}

for (int i = 0; i < swapchain.GetSwapChainImagesSize(); i++) {
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = uniformBuffers[i];
bufferInfo.offset = 0;
bufferInfo.range = size;

VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = m_DescriptorSets[i];
descriptorWrite.dstBinding = 0;
descriptorWrite.dstArrayElement = 0;

descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pBufferInfo = &bufferInfo;

vkUpdateDescriptorSets(logicalDevice, 1, &descriptorWrite, 0, nullptr);
}
}
}
6 changes: 5 additions & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanDescriptorSet.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#pragma once

#include <vulkan/vulkan.h>
#include <vector>

namespace Arcane
{
class VulkanDescriptorSet
{
public:
VulkanDescriptorSet();
VulkanDescriptorSet(std::vector<VkBuffer> uniformBuffers, uint32_t size);

VkDescriptorSetLayout GetLayout() { return m_Layout; }
std::vector<VkDescriptorSet> GetDescriptorSets() { return m_DescriptorSets; }
private:
VkDescriptorSetLayout m_Layout;
VkDescriptorPool m_DescriptorPool;
std::vector<VkDescriptorSet> m_DescriptorSets;
};
}
5 changes: 5 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "VulkanContext.h"
#include "VulkanRenderPass.h"
#include "VulkanVertexDescriptor.h"
#include "VulkanUniformBuffer.h"

namespace Arcane {
VulkanPipeline::VulkanPipeline(PipelineSpecification& spec)
Expand All @@ -17,7 +18,9 @@ namespace Arcane {
VulkanContext* _context = static_cast<VulkanContext*>(window.GetContext());
VkDevice logicalDevice = _context->GetDevice().GetLogicalDevice();
VkVertexInputBindingDescription bindingDescription = static_cast<VulkanVertexDescriptor*>(spec.descriptor)->GetBindingDescription();

auto attributeDescriptions = static_cast<VulkanVertexDescriptor*>(spec.descriptor)->GetAttributeDescriptions();
auto vulkanUniformBufferLayout = static_cast<VulkanUniformBuffer*>(spec.uniformBuffer)->GetLayout();

// Creating Vertex Shader
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
Expand Down Expand Up @@ -106,6 +109,8 @@ namespace Arcane {

VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &vulkanUniformBufferLayout;

if (vkCreatePipelineLayout(logicalDevice, &pipelineLayoutInfo, nullptr, &m_PipelineLayout) != VK_SUCCESS) {
printf("Pipeline Layout Not Created");
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ namespace Arcane {

uint32_t imageIndex;
vkAcquireNextImageKHR(_context->GetDevice().GetLogicalDevice(), m_SwapChain, UINT64_MAX, m_ImageAvailableSemaphores[m_CurrentFrameIndex], VK_NULL_HANDLE, &imageIndex);
m_CurrentImageIndex = imageIndex;

if (m_ImagesInFlight[imageIndex] != VK_NULL_HANDLE) {
vkWaitForFences(_context->GetDevice().GetLogicalDevice(), 1, &m_ImagesInFlight[imageIndex], VK_TRUE, UINT64_MAX);
}

m_CurrentImageIndex = imageIndex;
m_ImagesInFlight[imageIndex] = m_InFlightFences[m_CurrentFrameIndex];

VkSubmitInfo submitInfo{};
Expand Down
11 changes: 10 additions & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanUniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,19 @@ namespace Arcane
printf("Allocated Uniform buffer memory\n");
}
}

m_DescriptorSet = new VulkanDescriptorSet(m_UniformBuffers, size);
}

void VulkanUniformBuffer::WriteData(void* data)
void VulkanUniformBuffer::WriteData(void* data, uint32_t size)
{
VulkanContext* context = static_cast<VulkanContext*>(Application::Get().GetWindow().GetContext());
VulkanSwapChain& swapchain = context->GetSwapChain();
VkDevice logicalDevice = context->GetDevice().GetLogicalDevice();

void* tempData;
vkMapMemory(logicalDevice, m_UniformBuffersMemory[swapchain.GetImageIndex()], 0, size, 0, &tempData);
memcpy(tempData, data, size);
vkUnmapMemory(logicalDevice, m_UniformBuffersMemory[swapchain.GetImageIndex()]);
}
}
7 changes: 6 additions & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanUniformBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <vector>
#include <vulkan/vulkan.h>
#include "Arcane/Renderer/UniformBuffer.h"
#include "VulkanDescriptorSet.h"

namespace Arcane
{
Expand All @@ -11,9 +12,13 @@ namespace Arcane
public:
VulkanUniformBuffer(uint32_t size);

void WriteData(void* data) override;
void WriteData(void* data, uint32_t size) override;

VkDescriptorSetLayout GetLayout() { return m_DescriptorSet->GetLayout(); }
std::vector<VkDescriptorSet> GetDescriptorSets() { return m_DescriptorSet->GetDescriptorSets(); }
private:
std::vector<VkBuffer> m_UniformBuffers;
std::vector<VkDeviceMemory> m_UniformBuffersMemory;
VulkanDescriptorSet* m_DescriptorSet;
};
}
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/Renderer/Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "Shader.h"
#include "RenderPass.h"
#include "VertexDescriptor.h"
#include "UniformBuffer.h"

namespace Arcane {
struct PipelineSpecification
{
Shader* shader;
RenderPass* renderPass;
VertexDescriptor* descriptor;
UniformBuffer* uniformBuffer;
};

class Pipeline
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Renderer/UniformBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Arcane
class UniformBuffer
{
public:
virtual void WriteData(void* data) = 0;
virtual void WriteData(void* data, uint32_t size) = 0;

static UniformBuffer* Create(uint32_t size);
private:
Expand Down
9 changes: 7 additions & 2 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "EditorLayer.h"

Expand Down Expand Up @@ -32,12 +33,14 @@ void EditorLayer::OnAttach()
Arcane::VertexType::float3
});

m_UniformBuffer = Arcane::UniformBuffer::Create(sizeof(UniformBufferObject));

// Test Pipeline
Arcane::PipelineSpecification spec;
spec.descriptor = m_VertexDescriptor;
spec.renderPass = m_RenderPass;
spec.shader = m_Shader;
spec.uniformBuffer = m_UniformBuffer;

m_Pipeline = Arcane::Pipeline::Create(spec);

Expand All @@ -58,8 +61,6 @@ void EditorLayer::OnAttach()
Arcane::IndexBuffer* indexBuffer = Arcane::IndexBuffer::Create(indices.data(), indices.size());
m_VertexBuffer->AddIndexBuffer(indexBuffer);

// Create a uniform buffer
Arcane::UniformBuffer* uniformBuffer = Arcane::UniformBuffer::Create(sizeof(UniformBufferObject));
}

void EditorLayer::OnDetach()
Expand All @@ -69,6 +70,10 @@ void EditorLayer::OnDetach()

void EditorLayer::OnUpdate(float deltaTime)
{
// Test Quad Color
glm::vec3 quadColor = {1.0f, 1.0f, 1.0f};
m_UniformBuffer->WriteData(glm::value_ptr(quadColor), sizeof(quadColor));

// Begin a Render pass
Arcane::Renderer::BeginRenderPass(m_RenderPass);

Expand Down
2 changes: 1 addition & 1 deletion EnchantingTable/src/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ class EditorLayer : public Arcane::Layer
Arcane::VertexDescriptor* m_VertexDescriptor;
Arcane::VertexBuffer* m_VertexBuffer;
Arcane::Pipeline* m_Pipeline;

Arcane::UniformBuffer* m_UniformBuffer;
};

0 comments on commit 075d2da

Please sign in to comment.