Skip to content

Commit

Permalink
please khronos, write a fucking better doc codes so we can read easil…
Browse files Browse the repository at this point in the history
…y :(
  • Loading branch information
mtuncbilek95 committed Oct 20, 2024
1 parent 98c27b4 commit d838626
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 9 deletions.
23 changes: 16 additions & 7 deletions Source/Engine/VulkanRHI/Descriptor/VDescBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace MAGE
return (value + alignment - 1) & ~(alignment - 1);
}

VDescBuffer::VDescBuffer(VDescLayout* layout, VDevice* device) : VObject(device),
m_layoutRef(layout), m_memory(VK_NULL_HANDLE), m_buffer(VK_NULL_HANDLE)
VDescBuffer::VDescBuffer(const DescBufferProps& desc, VDevice* device) : VObject(device),
m_props(desc), m_memory(VK_NULL_HANDLE), m_buffer(VK_NULL_HANDLE)
{
// Get descriptor buffer properties
VkPhysicalDeviceDescriptorBufferPropertiesEXT descriptorBufferProperties = {};
Expand All @@ -25,21 +25,21 @@ namespace MAGE

// Get layout size
VkDeviceSize memorySize;
GetDescriptorSetLayoutSizeEXT(m_rootDevice->GetDevice(), m_layoutRef->GetLayout(), &memorySize);
GetDescriptorSetLayoutSizeEXT(m_rootDevice->GetDevice(), desc.layout->GetLayout(), &memorySize);

// Align memory size
VkDeviceSize offsetSize;
memorySize = AlignDescBuffer(memorySize, descriptorBufferProperties.descriptorBufferOffsetAlignment);
GetDescriptorSetLayoutBindingOffsetEXT(m_rootDevice->GetDevice(), m_layoutRef->GetLayout(), 0u, &offsetSize);
GetDescriptorSetLayoutBindingOffsetEXT(m_rootDevice->GetDevice(), desc.layout->GetLayout(), 0u, &offsetSize);

VkBufferUsageFlags2CreateInfoKHR usageFlags = {};
usageFlags.sType = VK_STRUCTURE_TYPE_BUFFER_USAGE_FLAGS_2_CREATE_INFO_KHR;
usageFlags.usage = VK_BUFFER_USAGE_2_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT;

VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = memorySize * 4;
bufferInfo.usage = VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
bufferInfo.size = memorySize * desc.insideBufferCount;
bufferInfo.usage = VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | desc.extraFlag;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
bufferInfo.pNext = &usageFlags;

Expand All @@ -51,7 +51,7 @@ namespace MAGE
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = device->FindMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
allocInfo.memoryTypeIndex = device->FindMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);

ErrorUtils::VkAssert(vkAllocateMemory(m_rootDevice->GetDevice(), &allocInfo, nullptr, &m_memory), "VulkanDescBuffer");
ErrorUtils::VkAssert(vkBindBufferMemory(m_rootDevice->GetDevice(), m_buffer, m_memory, 0), "VulkanDescBuffer");
Expand All @@ -65,6 +65,15 @@ namespace MAGE
Destroy();
}

inline VkDeviceAddress VDescBuffer::GetAddress()
{
VkBufferDeviceAddressInfo addressInfo = {};
addressInfo.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
addressInfo.buffer = m_buffer;
m_address = vkGetBufferDeviceAddress(m_rootDevice->GetDevice(), &addressInfo);
return m_address;
}

void VDescBuffer::MapMemory(RawBuffer buffer, u64 offset)
{
void* data;
Expand Down
13 changes: 11 additions & 2 deletions Source/Engine/VulkanRHI/Descriptor/VDescBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ namespace MAGE
{
class VDescLayout;

struct DescBufferProps final
{
u32 insideBufferCount;
VDescLayout* layout;
VkBufferUsageFlags extraFlag;
};

class VDescBuffer final : VObject
{
public:
VDescBuffer(VDescLayout* layout, VDevice* device);
VDescBuffer(const DescBufferProps& desc, VDevice* device);
~VDescBuffer() override final;

inline VkBuffer GetBuffer() const { return m_buffer; }
Expand All @@ -28,14 +35,16 @@ namespace MAGE
inline u64 GetOffset() const { return m_offset; }

void MapMemory(RawBuffer buffer, u64 offset);
inline VkDeviceAddress GetAddress();
void Destroy() override final;

private:
VkBuffer m_buffer;
VkDeviceMemory m_memory;
VkDeviceSize m_totalSize;
VkDeviceSize m_offset;
VkDeviceAddress m_address;

VDescLayout* m_layoutRef;
DescBufferProps m_props;
};
}
93 changes: 93 additions & 0 deletions Source/Engine/VulkanRHI/Descriptor/VDescSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "VDescSet.h"

#include "../Core/VAssert.h"
#include "../Core/VLoadFuncs.h"
#include "../Device/VDevice.h"
#include "../Descriptor/VDescLayout.h"
#include "../Descriptor/VDescPool.h"
#include "../Image/VImageView.h"
#include "../Buffer/VDstBuffer.h"
#include "../Sampler/VSampler.h"

namespace MAGE
{
VDescSet::VDescSet(const DescSetProps& desc, VDevice* device) : VObject(device), m_props(desc)
{
VkDescriptorSetLayout dLayout = desc.layout->GetLayout();

VkDescriptorSetAllocateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
info.descriptorSetCount = 1;
info.descriptorPool = desc.pool->GetPool();
info.pSetLayouts = &dLayout;

ErrorUtils::VkAssert(vkAllocateDescriptorSets(m_rootDevice->GetDevice(), &info, &m_set), "VDescSet");
}

VDescSet::~VDescSet()
{
Destroy();
}

void VDescSet::UpdateSet(const UpdateProps& desc) const
{
VkWriteDescriptorSet writeInformations[32];
VkDescriptorBufferInfo writeBufferInformations[32];
VkDescriptorImageInfo writeImageInformations[32];
u32 bufferIndex = 0;
u32 imageIndex = 0;

for (u8 i = 0; i < desc.entries.size(); i++)
{
VkWriteDescriptorSet writeInfo = {};
writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeInfo.dstSet = m_set;
writeInfo.dstBinding = desc.entries[i].binding;
writeInfo.dstArrayElement = desc.entries[i].arrayElement;
writeInfo.descriptorCount = desc.entries[i].count;
writeInfo.descriptorType = desc.entries[i].type;
writeInfo.pNext = nullptr;

switch (desc.entries[i].type)
{
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
{
VkDescriptorImageInfo imageInfo = {};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = desc.entries[i].entry.image->GetView();
imageInfo.sampler = desc.entries[i].sampler->GetSampler();
writeImageInformations[imageIndex] = imageInfo;
writeInfo.pImageInfo = &writeImageInformations[imageIndex];
writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
imageIndex++;
}
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
{
VkDescriptorBufferInfo bufferInfo = {};
bufferInfo.buffer = desc.entries[i].entry.uniform->GetBuffer();
bufferInfo.offset = desc.Entries[i].bufferOffset;
bufferInfo.range = desc.entries[i].entry.uniform->GetTotalSize();
writeBufferInformations[bufferIndex] = bufferInfo;
writeInfo.pBufferInfo = &writeBufferInformations[bufferIndex];
writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
bufferIndex++;
}
default:
spdlog::warn("{} is not a valid descriptor type.", magic_enum::enum_name<VkDescriptorType>(desc.entries[i].type));
}

writeInformations[i] = writeInfo;
}

vkUpdateDescriptorSets(m_rootDevice->GetDevice(), desc.entries.size(), writeInformations, 0, nullptr);
}

void VDescSet::Destroy()
{
if (m_set != VK_NULL_HANDLE)
{
vkFreeDescriptorSets(m_rootDevice->GetDevice(), m_props.pool->GetPool(), 1, &m_set);
m_set = VK_NULL_HANDLE;
}
}
}
65 changes: 65 additions & 0 deletions Source/Engine/VulkanRHI/Descriptor/VDescSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "../Abstraction/VObject.h"

namespace MAGE
{
class VDescLayout;
class VDescPool;
class VDstBuffer;
class VImageView;
class VSampler;

struct DescSetProps final
{
VDescLayout* layout;
VDescPool* pool;
};

union BufferEntry final
{
VDstBuffer* uniform;
VImageView* image;
};

struct UpdateEntry final
{
BufferEntry entry;
VSampler* sampler;
VkDescriptorType type;
u32 count;
u32 arrayElement;
u64 bufferOffset;
u32 binding;
};

struct UpdateProps final
{
Vector<UpdateEntry> entries;
};

class VDescSet final : public VObject
{
public:
VDescSet(const DescSetProps& desc, VDevice* device);
~VDescSet() override final;

inline VkDescriptorSet GetSet() const { return m_set; }

void UpdateSet(const UpdateProps& desc) const;
void Destroy() override final;

private:
VkDescriptorSet m_set;

DescSetProps m_props;
};
}

0 comments on commit d838626

Please sign in to comment.