Skip to content

Commit

Permalink
Finishing Work on Uniform buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jul 23, 2021
1 parent c3d91c8 commit 2f439df
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 16 deletions.
7 changes: 6 additions & 1 deletion Arcane/src/Arcane/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Arcane {

m_ImGuiLayer = ImGuiLayer::Create();
m_ImGuiLayer->OnAttach();

m_Clock = new CClock();
}

void Application::RenderImGui()
Expand Down Expand Up @@ -59,8 +61,11 @@ namespace Arcane {
{
while (m_bIsRunning)
{
m_Clock->Process();
float deltaTime = m_Clock->GetDeltaTick() / 1000.0f;

for (Layer* layer : m_LayerStack) {
layer->OnUpdate(1.0f);
layer->OnUpdate(deltaTime);
}

RenderImGui();
Expand Down
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <vector>

#include "Clock.h"
#include "Window.h"
#include "Arcane/ImGui/ImGuiLayer.h"

Expand Down Expand Up @@ -43,5 +44,6 @@ namespace Arcane {
std::vector<Layer*> m_LayerStack;
bool m_bIsRunning = true;
ImGuiLayer* m_ImGuiLayer;
CClock* m_Clock;
};
}
90 changes: 90 additions & 0 deletions Arcane/src/Arcane/Core/Clock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "Clock.h"

namespace Arcane
{
// Library Includes
#include <chrono>

// Local Includes

// This Includes
#include "clock.h"


// Implementation
//****************************************************
// CClock: CClock Class Constructor
// @author:
// @parameter: No parameters
//
// @return: none
//*****************************************************
CClock::CClock()
: m_fTimeElapsed(0.0f)
, m_fDeltaTime(0.0f)
{

}

//****************************************************
// ~CClock: CClock Class Destructor
// @author:
// @parameter: No parameters
//
// @return: none
//*****************************************************
CClock::~CClock()
{

}

//****************************************************
// Initialise: CClock Class Initialiser - sets the first time values
// @author:
// @parameter: No parameters
//
// @return: true if initialisation is successful, false if not
//*****************************************************
bool CClock::Initialise()
{
m_fCurrentTime = std::chrono::high_resolution_clock::now();
return (true);
}

//****************************************************
// Process: processes the change in time since it was last called
// @author:
// @parameter: No parameters
//
// @return: void
//*****************************************************
void
CClock::Process()
{
m_fLastTime = m_fCurrentTime;

m_fCurrentTime = std::chrono::high_resolution_clock::now();


m_fDeltaTime = static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(m_fCurrentTime - m_fLastTime).count());

m_fTimeElapsed += m_fDeltaTime;
}

//****************************************************
// GetDeltaTick: gets the current delta tick value
// @author:
// @parameter: No parameters
//
// @return: the current delta tick value
//*****************************************************
float CClock::GetDeltaTick()
{
return (static_cast<float>(m_fDeltaTime));
}

void CClock::Reset()
{
m_fCurrentTime = std::chrono::high_resolution_clock::now();
}
}
46 changes: 46 additions & 0 deletions Arcane/src/Arcane/Core/Clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef _CLOCK_H_
#define _CLOCK_H_

// Library Includes
#include <chrono>
#include <vector>

namespace Arcane
{
// Prototypes
class CClock
{
// Member Functions
public:
CClock();
~CClock();
bool Initialise();
void Process();
float GetDeltaTick();
void Reset();

protected:

private:
CClock(const CClock& _kr);
CClock& operator= (const CClock& _kr);

// Member Variables
public:

protected:
double m_fTimeElapsed;
double m_fDeltaTime;
std::chrono::high_resolution_clock::time_point m_fLastTime;
std::chrono::high_resolution_clock::time_point m_fCurrentTime;

std::vector<double> m_vecTimeHistory;

long long m_llNumCounts;

private:

};
}

#endif //
45 changes: 43 additions & 2 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanUniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Arcane {
return -1;
}

VulkanUniformBuffer::VulkanUniformBuffer()
VulkanUniformBuffer::VulkanUniformBuffer(uint32_t size)
{
Application& app = Application::Get();
VulkanContext* context = static_cast<VulkanContext*>(app.GetWindow().GetContext());
Expand All @@ -48,7 +48,7 @@ namespace Arcane {
}

// Create Uniform Buffers
VkDeviceSize bufferSize = sizeof(UniformBufferObject);
VkDeviceSize bufferSize = size;

m_UniformBuffers.resize(context->GetSwapChain().GetSwapChainImagesSize());
m_UniformBuffersMemory.resize(context->GetSwapChain().GetSwapChainImagesSize());
Expand Down Expand Up @@ -101,5 +101,46 @@ namespace Arcane {
else {
printf("Created descriptor pool\n");
}

std::vector<VkDescriptorSetLayout> layouts(context->GetSwapChain().GetSwapChainImagesSize(), m_DescriptorLayout);
VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = m_DescriptorPool;
allocInfo.descriptorSetCount = context->GetSwapChain().GetSwapChainImagesSize();
allocInfo.pSetLayouts = layouts.data();

m_DescriptorSets.resize(context->GetSwapChain().GetSwapChainImagesSize());

if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, m_DescriptorSets.data()) != VK_SUCCESS) {
printf("Descriptor Sets Not Allocated\n");
}

// Now we populate every descriptor
for (size_t i = 0; i < context->GetSwapChain().GetSwapChainImagesSize(); i++) {
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = m_UniformBuffers[i];
bufferInfo.offset = 0;
bufferInfo.range = sizeof(UniformBufferObject);

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;
descriptorWrite.pImageInfo = nullptr; // Optional
descriptorWrite.pTexelBufferView = nullptr; // Optional

vkUpdateDescriptorSets(logicalDevice, 1, &descriptorWrite, 0, nullptr);
}
}

void VulkanUniformBuffer::WriteData(void* data)
{

}
}
5 changes: 4 additions & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanUniformBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ namespace Arcane {
class VulkanUniformBuffer : public UniformBuffer
{
public:
VulkanUniformBuffer();
VulkanUniformBuffer(uint32_t size);

// Get Descriptor Layout to give to pipeline
VkDescriptorSetLayout GetLayout() { return m_DescriptorLayout; }

// Write Data
void WriteData(void* data) override;
private:
// Descriptor Set Layout
VkDescriptorSetLayout m_DescriptorLayout;
Expand Down
4 changes: 2 additions & 2 deletions Arcane/src/Arcane/Renderer/UniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include "Arcane/Platform/Vulkan/VulkanUniformBuffer.h"

namespace Arcane {
UniformBuffer* UniformBuffer::Create()
UniformBuffer* UniformBuffer::Create(uint32_t size)
{
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanUniformBuffer();
case RendererAPIType::Vulkan: return new VulkanUniformBuffer(size);
case RendererAPIType::None: return nullptr;
default: return nullptr;
}
Expand Down
6 changes: 4 additions & 2 deletions Arcane/src/Arcane/Renderer/UniformBuffer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include <iostream>

namespace Arcane {
class UniformBuffer
{
public:

static UniformBuffer* Create();
virtual void WriteData(void* data) = 0;
static UniformBuffer* Create(uint32_t size);
private:

};
Expand Down
29 changes: 21 additions & 8 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <vector>
#include <filesystem>
#include <glm/gtc/matrix_transform.hpp>

#include "EditorLayer.h"

Expand All @@ -10,6 +10,13 @@ struct TestVertex
glm::vec3 color;
};

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


EditorLayer::EditorLayer()
{
Expand All @@ -33,17 +40,16 @@ void EditorLayer::OnAttach()
spec.descriptor = m_VertexDescriptor;
spec.renderPass = m_RenderPass;
spec.shader = m_Shader;
spec.uniformBuffer = Arcane::UniformBuffer::Create();

spec.uniformBuffer = Arcane::UniformBuffer::Create(sizeof(UniformBufferObject));

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

// Test Vertices
std::vector<TestVertex> vertices = {
{{-0.5f, -0.5f,0.0f}, {1.0f, 0.5f, 0.2f}},
{{0.5f, -0.5f, 0.0f}, {1.0f, 0.5f, 0.2f}},
{{0.5f, 0.5f, 0.0f}, {1.0f, 0.5f, 0.2f}},
{{-0.5f, 0.5f, 0.0f}, {1.0f, 0.5f, 0.2f}}
{{-0.5f, -0.5f,0.0f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}}
};

std::vector<uint32_t> indices = {
Expand All @@ -63,6 +69,13 @@ void EditorLayer::OnDetach()

void EditorLayer::OnUpdate(float deltaTime)
{
printf("%.2f\n", deltaTime);

UniformBufferObject ubo{};
ubo.model = glm::rotate(glm::mat4(1.0f), deltaTime * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.proj = glm::perspective(glm::radians(45.0f), 1600.0f / 1200.0f, 0.1f, 10.0f);

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

Expand Down
1 change: 1 addition & 0 deletions EnchantingTable/src/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ 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 2f439df

Please sign in to comment.