Skip to content

Commit

Permalink
Can now access material descriptor that is generated by shader, just …
Browse files Browse the repository at this point in the history
…need to add a uniform buffer to test passing data through
  • Loading branch information
tomheeleynz committed Jul 27, 2022
1 parent c0aa7b2 commit b7c093e
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 73 deletions.
4 changes: 4 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ namespace Arcane
VulkanMaterial::VulkanMaterial(Shader* shader)
{
m_Shader = shader;

// Reflect Shader to get descriptor info
VulkanShader* vulkanShader = static_cast<VulkanShader*>(shader);

// Get Material descriptor, then add uniform buffer to it

}

std::vector<ShaderVariable> VulkanMaterial::GetShaderVariables()
Expand Down
75 changes: 75 additions & 0 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,81 @@ namespace Arcane
}
}

VulkanSet::VulkanSet(DescriptorSetSpecs& specs, std::vector<DescriptorLayoutSpecs> layoutSpecs)
{
m_SetNumber = specs.SetNumber;

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;
binding.pImmutableSamplers = nullptr;

switch (spec.Location)
{
case DescriptorLocation::VERTEX:
binding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
break;
case DescriptorLocation::FRAGMENT:
binding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
break;
default:
break;
}

// 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");
}
}
}

void VulkanSet::AddUniformBuffer(UniformBuffer* buffer, uint32_t setNumber, uint32_t bindingNumber)
{
// Get Image Count
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Arcane
{
public:
VulkanSet(DescriptorSetSpecs& specs, std::initializer_list<DescriptorLayoutSpecs> layoutSpecs);
VulkanSet(DescriptorSetSpecs& specs, std::vector<DescriptorLayoutSpecs> layoutSpecs);

// Virtual Functions
void AddUniformBuffer(UniformBuffer* buffer, uint32_t setNumber, uint32_t bindingNumber) override;
void AddImageSampler(Texture* texture, uint32_t setNumber, uint32_t bindingNumber) override;
void AddImageSampler(Framebuffer* framebuffer, uint32_t setNumber, uint32_t bindingNumber) override;


// Get Descriptor Set Layout
VkDescriptorSetLayout GetLayout() { return m_DescriptorSetLayout; }
std::vector<VkDescriptorSet> GetDescriptorSets() { return m_DescriptorSets; }
Expand Down
94 changes: 35 additions & 59 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanShader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Arcane/Core/Application.h"
#include "Arcane/Renderer/DescriptorSet.h"
#include "VulkanShader.h"
#include "VulkanContext.h"

Expand Down Expand Up @@ -80,11 +81,13 @@ namespace Arcane {
Reflect();
}

void VulkanShader::Reflect()
DescriptorSet* VulkanShader::GetMaterialDescriptor()
{
// Reflect Vertex Module
ReflectModule(m_VertexByteCode, m_VertexShaderModule);
return m_MaterialSet;
}

void VulkanShader::Reflect()
{
// Reflect Fragment Module
ReflectModule(m_FragmentByteCode, m_FragShaderModule);
}
Expand Down Expand Up @@ -131,64 +134,37 @@ namespace Arcane {

for (int i = 0; i < sets.size(); i++) {
SpvReflectDescriptorSet* reflectSet = sets[i];
DescriptorSetLayoutData newData;

// Resize binding stuff
newData.Bindings.resize(reflectSet->binding_count);
for (int j = 0; j < reflectSet->binding_count; j++) {
// Get the reflected binding
const SpvReflectDescriptorBinding& reflBinding = *(reflectSet->bindings[j]);

// Get Descriptor Binding
VkDescriptorSetLayoutBinding& binding = newData.Bindings[j];

// Load data into descriptor binding
binding.binding = reflBinding.binding;
binding.descriptorType = static_cast<VkDescriptorType>(reflBinding.descriptor_type);
binding.descriptorCount = 1;
binding.pImmutableSamplers = nullptr;
for (uint32_t i_dim = 0; i_dim < reflBinding.array.dims_count; ++i_dim) {
binding.descriptorCount *= reflBinding.array.dims[i_dim];
}
binding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
}

// Load other stuff
newData.SetNumber = reflectSet->set;
newData.CreateInfo = {};
newData.CreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
newData.CreateInfo.bindingCount = reflectSet->binding_count;
newData.CreateInfo.pBindings = newData.Bindings.data();
newData.CreateInfo.flags = 0;

// Actuall Create the descriptor set layout
if (vkCreateDescriptorSetLayout(logicalDevice, &newData.CreateInfo, nullptr, &newData.Layout) != VK_SUCCESS) {
printf("Reflected Shader Descriptor Set layout not created\n");
}

setLayouts.push_back(newData);
}

VkDescriptorPool descriptorPool = _context->GetPool();
// Create descriptor sets
for (int i = 0; i < setLayouts.size(); i++) {
DescriptorSetLayoutData& setLayout = setLayouts[i];

for (int j = 0; j < imageCount; j++) {
VkDescriptorSetAllocateInfo allocInfo = {};
VkDescriptorSet descriptorSet;

allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.pNext = nullptr;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &setLayout.Layout;

if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, &descriptorSet)) {
printf("Failed to allocate descriptor set\n");
// DescriptorSetLayoutData newData;

if (reflectSet->set == 2)
{
DescriptorSetSpecs newSetSpecs;
newSetSpecs.SetNumber = reflectSet->set;

std::vector<DescriptorLayoutSpecs> bindings;
bindings.resize(reflectSet->binding_count);

for (int j = 0; j < reflectSet->binding_count; j++) {
// Get the reflected binding
const SpvReflectDescriptorBinding& reflBinding = *(reflectSet->bindings[j]);

if (reflBinding.binding == 0)
{
// Create my struct for a binding
DescriptorLayoutSpecs& binding = bindings[j];
binding.Binding = reflBinding.binding;
// location
binding.Location = DescriptorLocation::FRAGMENT;
// descriptor count
binding.DescriptorCount = 1;
// material
binding.Name = "Material";
// Uniform Buffer
binding.Type = DescriptorType::UNIFORM_BUFFER;
}
}

m_DescriptorSets.push_back(descriptorSet);
m_MaterialSet = DescriptorSet::Create(newSetSpecs, bindings);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ namespace Arcane

VkShaderModule GetVertexShaderModule() { return m_VertexShaderModule; }
VkShaderModule GetFragmentShaderModule() { return m_FragShaderModule; }

std::vector<VkDescriptorSet> GetAllDescriptorSets() { return m_DescriptorSets; }
DescriptorSet* GetMaterialDescriptor() override;

void Reflect();
private:
Expand All @@ -28,10 +27,11 @@ namespace Arcane
VkShaderModule m_VertexShaderModule;
VkShaderModule m_FragShaderModule;

DescriptorSet* m_MaterialSet = nullptr;

std::vector<char> m_VertexByteCode;
std::vector<char> m_FragmentByteCode;

std::vector<ShaderVariable> m_ShaderVariables;
std::vector<VkDescriptorSet> m_DescriptorSets;
};
}
9 changes: 9 additions & 0 deletions Arcane/src/Arcane/Renderer/DescriptorSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ namespace Arcane
default: return nullptr;
}
}

DescriptorSet* DescriptorSet::Create(DescriptorSetSpecs& setSpecs, std::vector<DescriptorLayoutSpecs> layoutSpecs)
{
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanSet(setSpecs, layoutSpecs);
default: return nullptr;
}
}
}
3 changes: 3 additions & 0 deletions Arcane/src/Arcane/Renderer/DescriptorSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <iostream>
#include <initializer_list>
#include <vector>

#include "UniformBuffer.h"
#include "Texture.h"
Expand Down Expand Up @@ -41,7 +42,9 @@ namespace Arcane
virtual void AddImageSampler(Texture* texture, uint32_t setNum, uint32_t bindingNum) = 0;
virtual void AddImageSampler(Framebuffer* framebuffer, uint32_t setNum, uint32_t bindingNum) = 0;
virtual void AddUniformBuffer(UniformBuffer* buffer, uint32_t setNum, uint32_t bindingNum) = 0;

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

};
Expand Down
13 changes: 9 additions & 4 deletions Arcane/src/Arcane/Renderer/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace Arcane
FramebufferAttachmentType::COLOR,
FramebufferAttachmentType::DEPTH
};

geometryFramebufferSpecs.ClearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
geometryFramebufferSpecs.Width = 512;
geometryFramebufferSpecs.Height = 512;
Expand All @@ -110,8 +110,8 @@ namespace Arcane
VertexType::float3,
VertexType::float3,
VertexType::float2
});
});

// Create Object Descriptor Set
DescriptorSetSpecs objectDescriptorSetSpecs;
objectDescriptorSetSpecs.SetNumber = 1;
Expand All @@ -129,7 +129,12 @@ namespace Arcane
geometrySpecs.renderPass = s_Data.GeometryRenderPass;
geometrySpecs.shader = s_Data.GeometryShader;
geometrySpecs.descriptor = s_Data.GeometryVertexDescriptor;
geometrySpecs.DescriptorSets = {s_Data.GlobalDescriptorSet, s_Data.ObjectDescriptorSet};

if (s_Data.GeometryShader->GetMaterialDescriptor() != nullptr)
geometrySpecs.DescriptorSets = {s_Data.GlobalDescriptorSet, s_Data.ObjectDescriptorSet, s_Data.GeometryShader->GetMaterialDescriptor()};
else
geometrySpecs.DescriptorSets = { s_Data.GlobalDescriptorSet, s_Data.ObjectDescriptorSet };

s_Data.GeometryPipeline = Pipeline::Create(geometrySpecs);

///////////////////////////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion Arcane/src/Arcane/Renderer/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <map>
#include <vector>
#include "DescriptorSet.h"

namespace Arcane
{
Expand All @@ -24,7 +25,7 @@ namespace Arcane
class Shader
{
public:

virtual DescriptorSet* GetMaterialDescriptor() = 0;
virtual std::vector<ShaderVariable> GetShaderVariables() = 0;
static Shader* Create(std::string vertexShader, std::string fragmentShader);
private:
Expand Down
10 changes: 5 additions & 5 deletions EnchantingTable/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ Collapsed=0
DockId=0x00000005,0

[Window][File Browser]
Pos=408,949
Size=1723,428
Pos=408,983
Size=1723,394
Collapsed=0
DockId=0x00000008,0

[Window][Viewport]
Pos=408,19
Size=1723,928
Size=1723,962
Collapsed=0
DockId=0x00000007,0

Expand All @@ -66,8 +66,8 @@ DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=2560,1358 Sp
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=409,1181 Selected=0x18F2AEBA
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1774,1181 Split=X
DockNode ID=0x00000001 Parent=0x00000006 SizeRef=1262,1181 Split=Y Selected=0x995B0CF8
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=1178,928 CentralNode=1 Selected=0x995B0CF8
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=1178,428 Selected=0x2389D759
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=1178,962 CentralNode=1 Selected=0x995B0CF8
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=1178,394 Selected=0x2389D759
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=336,1181 Selected=0x4A17B156
DockNode ID=0x00000004 Parent=0x0000000A SizeRef=360,1181 Selected=0x2DDDAD1E
DockNode ID=0x0000000C Parent=0x3BC79352 SizeRef=267,1358 Selected=0x0984415E
Expand Down

0 comments on commit b7c093e

Please sign in to comment.