Skip to content

Commit

Permalink
Working On Uniform Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jul 5, 2021
1 parent e391282 commit 36573b3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
1 change: 0 additions & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ VulkanVertexBuffer::VulkanVertexBuffer(void* vertices, uint32_t size)
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;


if (vkCreateBuffer(logicalDevice, &bufferInfo, nullptr, &m_VertexBuffer) != VK_SUCCESS) {
printf("Vertex Buffer Not Created\n");
}
Expand Down
21 changes: 21 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ VulkanPipeline::VulkanPipeline(PipelineSpecification& spec)
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE;

VkDescriptorSetLayoutBinding uboLayoutBinding{};
uboLayoutBinding.binding = 0;
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uboLayoutBinding.descriptorCount = 1;
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
uboLayoutBinding.pImmutableSamplers = nullptr;

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

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

// Color Blending
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
Expand All @@ -105,6 +124,8 @@ VulkanPipeline::VulkanPipeline(PipelineSpecification& spec)

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

if (vkCreatePipelineLayout(logicalDevice, &pipelineLayoutInfo, nullptr, &m_PipelineLayout) != VK_SUCCESS) {
printf("Pipeline Layout Not Created");
Expand Down
1 change: 1 addition & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class VulkanPipeline : public Pipeline
private:
VkPipelineLayout m_PipelineLayout;
VkPipeline m_Pipeline;
VkDescriptorSetLayout m_DescriptorLayout;
};
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanSwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VulkanSwapChain

std::vector<VkCommandBuffer> GetCommandBuffers() { return m_CommandBuffers; }
std::vector<VkFramebuffer> GetSwapChainFramebuffers() { return m_SwapChainFramebuffers; }
VkCommandBuffer GetFrameCommandBuffer() { return m_CommandBuffers[m_CurrentFrameIndex]; }
uint32_t GetSwapChainImagesSize() { return m_SwapChainImages.size(); }

void SwapBuffers();
private:
Expand Down
65 changes: 50 additions & 15 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanUniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,63 @@
#include "Core/Application.h"
#include "VulkanContext.h"

static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
{
Application& app = Application::Get();
VkDevice logicalDevice = static_cast<VulkanContext*>(app.GetWindow().GetContext())->GetDevice().GetLogicalDevice();
VkPhysicalDevice physicalDevice = static_cast<VulkanContext*>(app.GetWindow().GetContext())->GetPhysicalDevice().GetPhysicalDevice();

VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);

for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
return i;
}
}

return -1;
}

VulkanUniformBuffer::VulkanUniformBuffer()
{
Application& app = Application::Get();
VulkanContext* context = static_cast<VulkanContext*>(app.GetWindow().GetContext());
VkDevice logicalDevice = context->GetDevice().GetLogicalDevice();

VkDescriptorSetLayoutBinding uboLayoutBinding{};
uboLayoutBinding.binding = 0;
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uboLayoutBinding.descriptorCount = 1;
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
uboLayoutBinding.pImmutableSamplers = nullptr;
VkDeviceSize bufferSize = sizeof(UniformBufferObject);

VkDescriptorSetLayoutCreateInfo layoutInfo{};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = &uboLayoutBinding;
m_UniformBuffers.resize(context->GetSwapChain().GetSwapChainImagesSize());
m_UniformBuffersMemory.resize(context->GetSwapChain().GetSwapChainImagesSize());

if (vkCreateDescriptorSetLayout(logicalDevice, &layoutInfo, nullptr, &m_DescriptorSetLayout) != VK_SUCCESS) {
printf("Descriptor Set Layout Not Created\n");
}
else {
printf("Descriptor Set Created\n");
for (int i = 0; i < context->GetSwapChain().GetSwapChainImagesSize(); i++) {
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = bufferSize;
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

if (vkCreateBuffer(logicalDevice, &bufferInfo, nullptr, &m_UniformBuffers[i]) != VK_SUCCESS) {
printf("Uniform Buffer Not Created\n");
}
else {
printf("Uniform Buffer Created\n");
}

// Get buffer memeory requirements
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(logicalDevice, m_UniformBuffers[i], &memRequirements);

VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

if (vkAllocateMemory(logicalDevice, &allocInfo, nullptr, &m_UniformBuffersMemory[i]) != VK_SUCCESS) {
printf("Failed to allocated uniform buffer memory\n");
}
else {
printf("Allocated uniform buffer memory\n");
}
}
}
13 changes: 12 additions & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanUniformBuffer.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
#pragma once

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

#include "Renderer/UniformBuffer.h"

struct UniformBufferObject
{
glm::mat4 model;
glm::mat4 view;
glm::mat4 proj;
};

class VulkanUniformBuffer : public UniformBuffer
{
public:
VulkanUniformBuffer();


private:
VkDescriptorSetLayout m_DescriptorSetLayout;
std::vector<VkBuffer> m_UniformBuffers;
std::vector<VkDeviceMemory> m_UniformBuffersMemory;
};
2 changes: 2 additions & 0 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ void EditorLayer::OnAttach()

// End a pass
Renderer::EndRenderPass(renderPass);

UniformBuffer* uniformBuffer = UniformBuffer::Create();
}

void EditorLayer::OnDetach()
Expand Down

0 comments on commit 36573b3

Please sign in to comment.