Skip to content

Commit

Permalink
New Descriptor Set API seems to be working
Browse files Browse the repository at this point in the history
Rewrote the descriptor set api to allow for a more module way of declaing it. Will create a seperate uniform buffer class that will be passed seperatly to the renderer for rendering (I think)
  • Loading branch information
tomheeleynz committed Jun 14, 2022
1 parent 8481548 commit 4adae37
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions Arcane/src/Arcane.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Arcane/Renderer/Framebuffer.h"
#include "Arcane/Renderer/SceneRenderer.h"
#include "Arcane/Renderer/Camera.h"
#include "Arcane/Renderer/DescriptorSet.h"

/////////////////////////////////////////////////////
////////// ImGui
Expand Down
20 changes: 20 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ namespace Arcane {

// Setup Swapchain
m_SwapChain = new VulkanSwapChain(*m_Device, m_Surface);

// Create Descriptor pool (I think this should be a global option)
VkDescriptorPoolSize pool_sizes[] = {
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 100 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 100 },
};

VkDescriptorPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
pool_info.maxSets = 100;
pool_info.poolSizeCount = std::size(pool_sizes);
pool_info.pPoolSizes = pool_sizes;

if (vkCreateDescriptorPool(m_Device->GetLogicalDevice(), &pool_info, nullptr, &m_DescriptorPool) != VK_SUCCESS) {
printf("Context Descriptor Pool Not Created\n");
}
else {
printf("Context Descriptor Pool Created\n");
}
}

bool VulkanContext::CheckValidationLayerSupport()
Expand Down
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Arcane {
VulkanPhysicalDevice& GetPhysicalDevice() { return *m_PhysicalDevice; }
VulkanSwapChain& GetSwapChain() { return *m_SwapChain; }
VkSurfaceKHR GetSurface() { return m_Surface; }
VkDescriptorPool GetPool() { return m_DescriptorPool; }
private:
bool CheckValidationLayerSupport();
std::vector<const char*> GetRequiredExtensions();
Expand All @@ -32,5 +33,6 @@ namespace Arcane {
VulkanDevice* m_Device;
VulkanSwapChain* m_SwapChain;
VkDebugUtilsMessengerEXT m_DebugMessenger;
VkDescriptorPool m_DescriptorPool;
};
}
67 changes: 67 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "Arcane/Core/Application.h"
#include "VulkanContext.h"
#include "VulkanSet.h"

namespace Arcane
{
VulkanSet::VulkanSet(DescriptorSetSpecs& specs, std::initializer_list<DescriptorLayoutSpecs> layoutSpecs)
{
Application& app = Application::Get();
VulkanContext* context = static_cast<VulkanContext*>(Application::Get().GetWindow().GetContext());
VulkanSwapChain& swapchain = context->GetSwapChain();
VkDevice logicalDevice = static_cast<VulkanContext*>(app.GetWindow().GetContext())->GetDevice().GetLogicalDevice();

// Iterate through Layouts, create bindings for each
// Manage the counts of each set
std::vector<VkDescriptorSetLayoutBinding> bindings;
for (DescriptorLayoutSpecs spec : layoutSpecs) {
VkDescriptorSetLayoutBinding binding{};
binding.binding = spec.Binding;
binding.descriptorCount = spec.DescriptorCount;

// Switch on the type
switch (spec.Type)
{
case DescriptorType::UNIFORM_BUFFER:
binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
break;
case DescriptorType::SAMPLER:
binding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
break;
default:
break;
}

bindings.push_back(binding);
}

// Actually Create Descriptor set
VkDescriptorSetLayoutCreateInfo setInfo = {};
setInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
setInfo.pNext = nullptr;
setInfo.bindingCount = static_cast<uint32_t>(bindings.size());
setInfo.pBindings = bindings.data();

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

// Get Descriptor Pool from context
VkDescriptorPool descriptorPool = context->GetPool();
uint32_t imageCount = swapchain.GetSwapChainImagesSize();
m_DescriptorSets.resize(imageCount);
for (int i = 0; i < imageCount; i++) {
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.pNext = nullptr;
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &m_DescriptorSetLayout;

if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, &m_DescriptorSets[i]) != VK_SUCCESS) {
printf("Failed to allocate descripor set\n");
}
}

}
}
18 changes: 18 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <vector>
#include <vulkan/vulkan.h>
#include "Arcane/Renderer/DescriptorSet.h"

namespace Arcane
{
class VulkanSet : public DescriptorSet
{
public:
VulkanSet(DescriptorSetSpecs& specs, std::initializer_list<DescriptorLayoutSpecs> layoutSpecs);

private:
VkDescriptorSetLayout m_DescriptorSetLayout = VK_NULL_HANDLE;
std::vector<VkDescriptorSet> m_DescriptorSets;
};
}
18 changes: 18 additions & 0 deletions Arcane/src/Arcane/Renderer/DescriptorSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "DescriptorSet.h"
#include "RendererAPI.h"

#if defined(ARCANE_WIN32)
#include "Arcane/Platform/Vulkan/VulkanSet.h"
#endif

namespace Arcane
{
DescriptorSet* DescriptorSet::Create(DescriptorSetSpecs& setSpecs, std::initializer_list<DescriptorLayoutSpecs> layoutSpecs)
{
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanSet(setSpecs, layoutSpecs);
default: return nullptr;
}
}
}
34 changes: 34 additions & 0 deletions Arcane/src/Arcane/Renderer/DescriptorSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <iostream>
#include <initializer_list>

namespace Arcane
{
enum class DescriptorType
{
UNIFORM_BUFFER,
SAMPLER
};

struct DescriptorSetSpecs
{
uint32_t SetNumber;
};

struct DescriptorLayoutSpecs
{
uint32_t Binding;
uint32_t DescriptorCount;
DescriptorType Type;
std::string Name;
};

class DescriptorSet
{
public:
static DescriptorSet* Create(DescriptorSetSpecs& setSpecs, std::initializer_list<DescriptorLayoutSpecs> layoutSpecs);
private:

};
}
9 changes: 9 additions & 0 deletions EnchantingTable/src/Assets/Shaders/Mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

layout(location = 0) out vec4 Color;

struct DirectionalLight
{
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};


void main() {
Color = vec4(1.0, 0.0, 0.0, 1.0);
}
11 changes: 11 additions & 0 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ void EditorLayer::OnAttach()
// Setup Camera Controller
m_EditorCameraController = new PerspectiveController();
m_EditorCameraController->SetCamera(m_EditorCamera);

// Test Descriptor Set Api
Arcane::DescriptorSetSpecs setSpecs;
setSpecs.SetNumber = 0;

Arcane::DescriptorSet* TestSet = Arcane::DescriptorSet::Create(
setSpecs,{
{0, 1, Arcane::DescriptorType::UNIFORM_BUFFER, "Test Buffer"},
{0, 2, Arcane::DescriptorType::SAMPLER, "Test Sampler"}
}
);
}

void EditorLayer::OnDetach()
Expand Down

0 comments on commit 4adae37

Please sign in to comment.