Przeglądaj źródła

Complete the acceleration structure

Panagiotis Christopoulos Charitos 5 lat temu
rodzic
commit
360a4c74a1

+ 22 - 2
src/anki/gr/AccelerationStructure.h

@@ -42,8 +42,28 @@ public:
 
 	Bool isValid() const
 	{
-		ANKI_ASSERT(!"TODO");
-		return false;
+		if(m_type == AccelerationStructureType::COUNT)
+		{
+			return false;
+		}
+
+		if(m_type == AccelerationStructureType::BOTTOM_LEVEL)
+		{
+			if(m_bottomLevel.m_indexType == IndexType::COUNT || m_bottomLevel.m_positionsFormat == Format::NONE
+				|| m_bottomLevel.m_indexCount == 0 || m_bottomLevel.m_vertexCount == 0)
+			{
+				return false;
+			}
+		}
+		else
+		{
+			if(m_topLevel.m_bottomLevelCount == 0)
+			{
+				return false;
+			}
+		}
+
+		return true;
 	}
 };
 

+ 47 - 2
src/anki/gr/vulkan/AccelerationStructureImpl.cpp

@@ -11,7 +11,15 @@ namespace anki
 
 AccelerationStructureImpl::~AccelerationStructureImpl()
 {
-	ANKI_ASSERT(!"TODO");
+	if(m_handle)
+	{
+		vkDestroyAccelerationStructureKHR(getDevice(), m_handle, nullptr);
+	}
+
+	if(m_memHandle)
+	{
+		getGrManagerImpl().getGpuMemoryManager().freeMemory(m_memHandle);
+	}
 }
 
 Error AccelerationStructureImpl::init(const AccelerationStructureInitInfo& inf)
@@ -56,7 +64,44 @@ Error AccelerationStructureImpl::init(const AccelerationStructureInitInfo& inf)
 
 	// Allocate memory
 	{
-		// TODO
+		// Get mem requirements
+		VkAccelerationStructureMemoryRequirementsInfoKHR reqIn{};
+		reqIn.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_KHR;
+		reqIn.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_OBJECT_KHR;
+		reqIn.buildType = VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR;
+		reqIn.accelerationStructure = m_handle;
+
+		VkMemoryRequirements2 req{};
+		req.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
+
+		vkGetAccelerationStructureMemoryRequirementsKHR(getDevice(), &reqIn, &req);
+
+		// Find mem IDX
+		U32 memIdx = getGrManagerImpl().getGpuMemoryManager().findMemoryType(req.memoryRequirements.memoryTypeBits,
+			VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
+			VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
+
+		// Fallback
+		if(memIdx == MAX_U32)
+		{
+			memIdx = getGrManagerImpl().getGpuMemoryManager().findMemoryType(
+				req.memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0);
+		}
+
+		ANKI_ASSERT(memIdx != MAX_U32);
+
+		// Allocate
+		// TODO is it linear or no-linear?
+		getGrManagerImpl().getGpuMemoryManager().allocateMemory(
+			memIdx, req.memoryRequirements.size, U32(req.memoryRequirements.alignment), true, false, m_memHandle);
+
+		// Bind memory
+		VkBindAccelerationStructureMemoryInfoKHR bindInfo{};
+		bindInfo.sType = VK_STRUCTURE_TYPE_BIND_ACCELERATION_STRUCTURE_MEMORY_INFO_KHR;
+		bindInfo.accelerationStructure = m_handle;
+		bindInfo.memory = m_memHandle.m_memory;
+		bindInfo.memoryOffset = m_memHandle.m_offset;
+		ANKI_VK_CHECK(vkBindAccelerationStructureMemoryKHR(getDevice(), 1, &bindInfo));
 	}
 
 	return Error::NONE;

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

@@ -29,8 +29,15 @@ public:
 
 	ANKI_USE_RESULT Error init(const AccelerationStructureInitInfo& inf);
 
+	VkAccelerationStructureKHR getHandle() const
+	{
+		ANKI_ASSERT(m_handle);
+		return m_handle;
+	}
+
 private:
 	VkAccelerationStructureKHR m_handle = VK_NULL_HANDLE;
+	GpuMemoryHandle m_memHandle;
 };
 /// @}