2
0
Эх сурвалжийг харах

Add the descriptor set code for AS

Panagiotis Christopoulos Charitos 5 жил өмнө
parent
commit
e10256d0e0

+ 7 - 0
src/anki/gr/CommandBuffer.h

@@ -286,6 +286,13 @@ public:
 	void bindTextureBuffer(
 		U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, Format fmt, U32 arrayIdx = 0);
 
+	/// Bind an acceleration structure.
+	/// @param set The set to bind to.
+	/// @param binding The binding to bind to.
+	/// @param[in,out] as The AS to bind.
+	/// @param arrayIdx The array index if the binding is an array.
+	void bindAccelerationStructure(U32 set, U32 binding, AccelerationStructurePtr as, U32 arrayIdx = 0);
+
 	/// Bind the bindless descriptor set into a slot.
 	void bindAllBindless(U32 set);
 

+ 6 - 0
src/anki/gr/vulkan/CommandBuffer.cpp

@@ -200,6 +200,12 @@ void CommandBuffer::bindImage(U32 set, U32 binding, TextureViewPtr img, U32 arra
 	self.bindImageInternal(set, binding, img, arrayIdx);
 }
 
+void CommandBuffer::bindAccelerationStructure(U32 set, U32 binding, AccelerationStructurePtr as, U32 arrayIdx)
+{
+	ANKI_VK_SELF(CommandBufferImpl);
+	self.bindAccelerationStructureInternal(set, binding, as, arrayIdx);
+}
+
 void CommandBuffer::bindTextureBuffer(
 	U32 set, U32 binding, BufferPtr buff, PtrSize offset, PtrSize range, Format fmt, U32 arrayIdx)
 {

+ 2 - 0
src/anki/gr/vulkan/CommandBufferImpl.cpp

@@ -815,6 +815,8 @@ void CommandBufferImpl::rebindDynamicState()
 
 void CommandBufferImpl::buildAccelerationStructureInternal(AccelerationStructurePtr& as)
 {
+	commandCommon();
+
 	// Get objects
 	const AccelerationStructureImpl& asImpl = static_cast<AccelerationStructureImpl&>(*as);
 

+ 7 - 0
src/anki/gr/vulkan/CommandBufferImpl.h

@@ -262,6 +262,13 @@ public:
 		m_microCmdb->pushObjectRef(img);
 	}
 
+	void bindAccelerationStructureInternal(U32 set, U32 binding, AccelerationStructurePtr& as, U32 arrayIdx)
+	{
+		commandCommon();
+		m_dsetState[set].bindAccelerationStructure(binding, arrayIdx, as.get());
+		m_microCmdb->pushObjectRef(as);
+	}
+
 	void bindAllBindlessInternal(U32 set)
 	{
 		commandCommon();

+ 4 - 0
src/anki/gr/vulkan/Common.h

@@ -51,6 +51,7 @@ enum class DescriptorType : U8
 	STORAGE_BUFFER,
 	IMAGE,
 	TEXTURE_BUFFER,
+	ACCELERATION_STRUCTURE,
 
 	COUNT
 };
@@ -236,6 +237,9 @@ ANKI_USE_RESULT inline VkDescriptorType convertDescriptorType(DescriptorType ak)
 	case DescriptorType::IMAGE:
 		out = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
 		break;
+	case DescriptorType::ACCELERATION_STRUCTURE:
+		out = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
+		break;
 	default:
 		out = VK_DESCRIPTOR_TYPE_MAX_ENUM;
 		ANKI_ASSERT(0);

+ 19 - 1
src/anki/gr/vulkan/DescriptorSet.cpp

@@ -531,6 +531,7 @@ void DSThreadAllocator::writeSet(const Array<AnyBindingExtended, MAX_BINDINGS_PE
 	DynamicArrayAuto<VkWriteDescriptorSet> writeInfos(tmpAlloc);
 	DynamicArrayAuto<VkDescriptorImageInfo> texInfos(tmpAlloc);
 	DynamicArrayAuto<VkDescriptorBufferInfo> buffInfos(tmpAlloc);
+	DynamicArrayAuto<VkWriteDescriptorSetAccelerationStructureKHR> asInfos(tmpAlloc);
 
 	// First pass: Populate the VkDescriptorImageInfo and VkDescriptorBufferInfo
 	for(U bindingIdx = m_layoutEntry->m_minBinding; bindingIdx <= m_layoutEntry->m_maxBinding; ++bindingIdx)
@@ -586,6 +587,15 @@ void DSThreadAllocator::writeSet(const Array<AnyBindingExtended, MAX_BINDINGS_PE
 					info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
 					break;
 				}
+				case DescriptorType::ACCELERATION_STRUCTURE:
+				{
+					VkWriteDescriptorSetAccelerationStructureKHR& info = *asInfos.emplaceBack();
+					info.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR;
+					info.pNext = nullptr;
+					info.accelerationStructureCount = 1;
+					info.pAccelerationStructures = &b.m_accelerationStructure.m_accelerationStructureHandle;
+					break;
+				}
 				default:
 					ANKI_ASSERT(0);
 				}
@@ -596,8 +606,9 @@ void DSThreadAllocator::writeSet(const Array<AnyBindingExtended, MAX_BINDINGS_PE
 	// Second pass: Populate the VkWriteDescriptorSet with VkDescriptorImageInfo and VkDescriptorBufferInfo
 	U32 texCounter = 0;
 	U32 buffCounter = 0;
+	U32 asCounter = 0;
 
-	VkWriteDescriptorSet writeTemplate = {};
+	VkWriteDescriptorSet writeTemplate{};
 	writeTemplate.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
 	writeTemplate.pNext = nullptr;
 	writeTemplate.dstSet = set.m_handle;
@@ -629,6 +640,9 @@ void DSThreadAllocator::writeSet(const Array<AnyBindingExtended, MAX_BINDINGS_PE
 				case DescriptorType::STORAGE_BUFFER:
 					writeInfo.pBufferInfo = &buffInfos[buffCounter++];
 					break;
+				case DescriptorType::ACCELERATION_STRUCTURE:
+					writeInfo.pNext = &asInfos[asCounter++];
+					break;
 				default:
 					ANKI_ASSERT(0);
 				}
@@ -878,6 +892,10 @@ void DescriptorSetState::flush(U64& hash,
 					case DescriptorType::IMAGE:
 						ANKI_ASSERT(anyBinding.m_type == DescriptorType::IMAGE && "Have bound the wrong type");
 						break;
+					case DescriptorType::ACCELERATION_STRUCTURE:
+						ANKI_ASSERT(
+							anyBinding.m_type == DescriptorType::ACCELERATION_STRUCTURE && "Have bound the wrong type");
+						break;
 					default:
 						ANKI_ASSERT(0);
 					}

+ 21 - 0
src/anki/gr/vulkan/DescriptorSet.h

@@ -10,6 +10,7 @@
 #include <anki/gr/vulkan/TextureImpl.h>
 #include <anki/gr/vulkan/TextureViewImpl.h>
 #include <anki/gr/vulkan/SamplerImpl.h>
+#include <anki/gr/vulkan/AccelerationStructureImpl.h>
 #include <anki/util/WeakArray.h>
 #include <anki/util/BitSet.h>
 
@@ -106,6 +107,12 @@ public:
 	VkImageView m_imgViewHandle;
 };
 
+class AsBinding
+{
+public:
+	VkAccelerationStructureKHR m_accelerationStructureHandle;
+};
+
 class AnyBinding
 {
 public:
@@ -118,6 +125,7 @@ public:
 		SamplerBinding m_sampler;
 		BufferBinding m_buff;
 		ImageBinding m_image;
+		AsBinding m_accelerationStructure;
 	};
 
 	DescriptorType m_type;
@@ -276,6 +284,19 @@ public:
 		unbindBindlessDSet();
 	}
 
+	void bindAccelerationStructure(U32 binding, U32 arrayIdx, const AccelerationStructure* as)
+	{
+		AnyBinding& b = getBindingToPopulate(binding, arrayIdx);
+		b = {};
+		b.m_type = DescriptorType::ACCELERATION_STRUCTURE;
+		b.m_uuids[0] = b.m_uuids[1] = as->getUuid();
+		b.m_accelerationStructure.m_accelerationStructureHandle =
+			static_cast<const AccelerationStructureImpl*>(as)->getHandle();
+
+		m_dirtyBindings.set(binding);
+		unbindBindlessDSet();
+	}
+
 	/// Forget all the rest of the bindings and bind the whole bindless descriptor set.
 	void bindBindlessDescriptorSet()
 	{

+ 1 - 0
src/anki/gr/vulkan/ShaderImpl.cpp

@@ -189,6 +189,7 @@ void ShaderImpl::doReflection(ConstWeakArray<U8> spirv, SpecConstsVector& specCo
 	func(rsrc.separate_samplers, DescriptorType::SAMPLER);
 	func(rsrc.storage_buffers, DescriptorType::STORAGE_BUFFER);
 	func(rsrc.storage_images, DescriptorType::IMAGE);
+	func(rsrc.acceleration_structures, DescriptorType::ACCELERATION_STRUCTURE);
 
 	for(U32 set = 0; set < MAX_DESCRIPTOR_SETS; ++set)
 	{