Jelajahi Sumber

Some work on AS building

Panagiotis Christopoulos Charitos 5 tahun lalu
induk
melakukan
844caf1f06

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

@@ -385,6 +385,20 @@ public:
 	/// @param dstOffset Offset in the destination buffer.
 	/// @param range Size to copy.
 	void copyBufferToBuffer(BufferPtr src, PtrSize srcOffset, BufferPtr dst, PtrSize dstOffset, PtrSize range);
+
+	/// Build the bottom level acceleration structure.
+	/// @param[in,out] as The AS to build.
+	/// @param[in] positions The position buffer.
+	/// @param positionsOffset Offset to the positions buffer.
+	/// @param positionsStride Stride of the positions.
+	/// @param[in] indices The indices buffer.
+	/// @param indicesOffset The offset of the 1st index.
+	void buildBottomLevelAccelerationStructure(AccelerationStructurePtr as,
+		BufferPtr positions,
+		PtrSize positionsOffset,
+		PtrSize positionsStride,
+		BufferPtr indices,
+		PtrSize indicesOffset);
 	/// @}
 
 	/// @name Sync

+ 11 - 0
src/anki/gr/Common.cpp

@@ -228,4 +228,15 @@ PtrSize computeVolumeSize(U width, U height, U depth, Format fmt)
 	}
 }
 
+PtrSize getFormatBytes(Format fmt)
+{
+	ANKI_ASSERT(fmt != Format::NONE);
+	U texelComponents;
+	U texelBytes;
+	U blockSize;
+	U blockBytes;
+	getFormatInfo(fmt, texelComponents, texelBytes, blockSize, blockBytes);
+	return texelBytes;
+}
+
 } // end namespace anki

+ 3 - 0
src/anki/gr/Common.h

@@ -403,6 +403,9 @@ PtrSize computeSurfaceSize(U width, U height, Format fmt);
 
 /// Compute the size in bytes of the texture volume.
 PtrSize computeVolumeSize(U width, U height, U depth, Format fmt);
+
+/// Get the size of a single component of that format.
+PtrSize getFormatBytes(Format fmt);
 /// @}
 
 } // end namespace anki

+ 12 - 0
src/anki/gr/vulkan/AccelerationStructureImpl.cpp

@@ -27,6 +27,18 @@ Error AccelerationStructureImpl::init(const AccelerationStructureInitInfo& inf)
 	ANKI_ASSERT(inf.isValid());
 	m_type = inf.m_type;
 
+	if(m_type == AccelerationStructureType::BOTTOM_LEVEL)
+	{
+		m_bottomLevelInfo.m_indexType = inf.m_bottomLevel.m_indexType;
+		m_bottomLevelInfo.m_positionsFormat = inf.m_bottomLevel.m_positionsFormat;
+		m_bottomLevelInfo.m_indexCount = inf.m_bottomLevel.m_indexCount;
+		m_bottomLevelInfo.m_vertexCount = inf.m_bottomLevel.m_vertexCount;
+	}
+	else
+	{
+		m_topLevelInfo.m_bottomLevelCount = inf.m_topLevel.m_bottomLevelCount;
+	}
+
 	// Create the handle
 	{
 		VkAccelerationStructureCreateGeometryTypeInfoKHR geom{};

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

@@ -15,6 +15,23 @@ namespace anki
 /// @addtogroup vulkan
 /// @{
 
+/// @memberof AccelerationStructureImpl
+class ASBottomLevelInfo
+{
+public:
+	IndexType m_indexType = IndexType::COUNT;
+	Format m_positionsFormat = Format::NONE;
+	U32 m_indexCount = 0;
+	U32 m_vertexCount = 0;
+};
+
+/// @memberof AccelerationStructureImpl
+class ASTopLevelInfo
+{
+public:
+	U32 m_bottomLevelCount = 0;
+};
+
 /// AccelerationStructure implementation.
 class AccelerationStructureImpl final : public AccelerationStructure,
 										public VulkanObject<AccelerationStructure, AccelerationStructureImpl>
@@ -35,9 +52,27 @@ public:
 		return m_handle;
 	}
 
+	const ASBottomLevelInfo& getBottomLevelInfo() const
+	{
+		ANKI_ASSERT(m_type == AccelerationStructureType::BOTTOM_LEVEL);
+		return m_bottomLevelInfo;
+	}
+
+	const ASTopLevelInfo& getTopLevelInfo() const
+	{
+		ANKI_ASSERT(m_type == AccelerationStructureType::TOP_LEVEL);
+		return m_topLevelInfo;
+	}
+
 private:
 	VkAccelerationStructureKHR m_handle = VK_NULL_HANDLE;
 	GpuMemoryHandle m_memHandle;
+
+	union
+	{
+		ASBottomLevelInfo m_bottomLevelInfo;
+		ASTopLevelInfo m_topLevelInfo;
+	};
 };
 /// @}
 

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

@@ -325,6 +325,18 @@ void CommandBuffer::copyBufferToBuffer(
 	self.copyBufferToBuffer(src, srcOffset, dst, dstOffset, range);
 }
 
+void CommandBuffer::buildBottomLevelAccelerationStructure(AccelerationStructurePtr as,
+	BufferPtr positions,
+	PtrSize positionsOffset,
+	PtrSize positionsStride,
+	BufferPtr indices,
+	PtrSize indicesOffset)
+{
+	ANKI_VK_SELF(CommandBufferImpl);
+	self.buildBottomLevelAccelerationStructureInternal(
+		as, positions, positionsOffset, positionsStride, indices, indicesOffset);
+}
+
 void CommandBuffer::setTextureBarrier(
 	TexturePtr tex, TextureUsageBit prevUsage, TextureUsageBit nextUsage, const TextureSubresourceInfo& subresource)
 {

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

@@ -9,6 +9,7 @@
 
 #include <anki/gr/Framebuffer.h>
 #include <anki/gr/vulkan/FramebufferImpl.h>
+#include <anki/gr/vulkan/AccelerationStructureImpl.h>
 
 #include <algorithm>
 
@@ -812,4 +813,26 @@ void CommandBufferImpl::rebindDynamicState()
 	}
 }
 
+void CommandBufferImpl::buildBottomLevelAccelerationStructureInternal(AccelerationStructurePtr& as,
+	BufferPtr& positions,
+	PtrSize positionsOffset,
+	PtrSize positionsStride,
+	BufferPtr& indices,
+	PtrSize indicesOffset)
+{
+	// Get objects
+	AccelerationStructureImpl& asImpl = static_cast<AccelerationStructureImpl&>(*as);
+	const BufferImpl& positionsImpl = static_cast<const BufferImpl&>(*positions);
+	const BufferImpl& indicesImpl = static_cast<const BufferImpl&>(*indices);
+
+	// Do checks
+#if ANKI_ENABLE_ASSERTS
+	{
+		const ASBottomLevelInfo& bottomInfo = asImpl.getBottomLevelInfo();
+	}
+#endif
+
+	ANKI_ASSERT(!"TODO");
+}
+
 } // end namespace anki

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

@@ -372,6 +372,13 @@ public:
 
 	void copyBufferToBuffer(BufferPtr& src, PtrSize srcOffset, BufferPtr& dst, PtrSize dstOffset, PtrSize range);
 
+	void buildBottomLevelAccelerationStructureInternal(AccelerationStructurePtr& as,
+		BufferPtr& positions,
+		PtrSize positionsOffset,
+		PtrSize positionsStride,
+		BufferPtr& indices,
+		PtrSize indicesOffset);
+
 	void setPushConstants(const void* data, U32 dataSize);
 
 	void setRasterizationOrder(RasterizationOrder order);