From d838626bdb7ee6eaf8e3280cf00e17dd4844396c Mon Sep 17 00:00:00 2001 From: Metehan Tuncbilek Date: Mon, 21 Oct 2024 02:46:43 +0300 Subject: [PATCH] please khronos, write a fucking better doc codes so we can read easily :( --- .../VulkanRHI/Descriptor/VDescBuffer.cpp | 23 +++-- .../Engine/VulkanRHI/Descriptor/VDescBuffer.h | 13 ++- .../Engine/VulkanRHI/Descriptor/VDescSet.cpp | 93 +++++++++++++++++++ Source/Engine/VulkanRHI/Descriptor/VDescSet.h | 65 +++++++++++++ 4 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 Source/Engine/VulkanRHI/Descriptor/VDescSet.cpp create mode 100644 Source/Engine/VulkanRHI/Descriptor/VDescSet.h diff --git a/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.cpp b/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.cpp index a187c90..1dee9a7 100644 --- a/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.cpp +++ b/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.cpp @@ -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 = {}; @@ -25,12 +25,12 @@ 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; @@ -38,8 +38,8 @@ namespace MAGE 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; @@ -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"); @@ -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; diff --git a/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.h b/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.h index 8264bec..b61563d 100644 --- a/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.h +++ b/Source/Engine/VulkanRHI/Descriptor/VDescBuffer.h @@ -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; } @@ -28,6 +35,7 @@ namespace MAGE inline u64 GetOffset() const { return m_offset; } void MapMemory(RawBuffer buffer, u64 offset); + inline VkDeviceAddress GetAddress(); void Destroy() override final; private: @@ -35,7 +43,8 @@ namespace MAGE VkDeviceMemory m_memory; VkDeviceSize m_totalSize; VkDeviceSize m_offset; + VkDeviceAddress m_address; - VDescLayout* m_layoutRef; + DescBufferProps m_props; }; } \ No newline at end of file diff --git a/Source/Engine/VulkanRHI/Descriptor/VDescSet.cpp b/Source/Engine/VulkanRHI/Descriptor/VDescSet.cpp new file mode 100644 index 0000000..2ecfde8 --- /dev/null +++ b/Source/Engine/VulkanRHI/Descriptor/VDescSet.cpp @@ -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(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; + } + } +} diff --git a/Source/Engine/VulkanRHI/Descriptor/VDescSet.h b/Source/Engine/VulkanRHI/Descriptor/VDescSet.h new file mode 100644 index 0000000..be7d9a3 --- /dev/null +++ b/Source/Engine/VulkanRHI/Descriptor/VDescSet.h @@ -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 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; + }; +}