Browse Source

Enable device buffer address on most buffers when RT is enabled

Panagiotis Christopoulos Charitos 5 years ago
parent
commit
4fc7e65c84

+ 0 - 1
src/anki/gr/Buffer.h

@@ -21,7 +21,6 @@ public:
 	PtrSize m_size = 0;
 	BufferUsageBit m_usage = BufferUsageBit::NONE;
 	BufferMapAccessBit m_access = BufferMapAccessBit::NONE;
-	Bool m_exposeGpuAddress = false; ///< Expose the buffer's GPU address.
 
 	BufferInitInfo(CString name = {})
 		: GrBaseInitInfo(name)

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

@@ -106,7 +106,7 @@ Error AccelerationStructureImpl::init(const AccelerationStructureInitInfo& inf)
 		// Allocate
 		// TODO is it linear or no-linear?
 		getGrManagerImpl().getGpuMemoryManager().allocateMemory(
-			memIdx, req.memoryRequirements.size, U32(req.memoryRequirements.alignment), true, false, m_memHandle);
+			memIdx, req.memoryRequirements.size, U32(req.memoryRequirements.alignment), true, m_memHandle);
 
 		// Bind memory
 		VkBindAccelerationStructureMemoryInfoKHR bindInfo{};
@@ -173,7 +173,6 @@ void AccelerationStructureImpl::initBuildInfo()
 			BufferInitInfo buffInit("RT_instances");
 			buffInit.m_size = sizeof(VkAccelerationStructureInstanceKHR) * instanceCount;
 			buffInit.m_usage = BufferImpl::ACCELERATION_STRUCTURE_BUILD_SCRATCH_USAGE;
-			buffInit.m_exposeGpuAddress = true;
 			buffInit.m_access = BufferMapAccessBit::WRITE;
 			m_topLevelInfo.m_instancesBuff = getManager().newBuffer(buffInit);
 		}

+ 5 - 4
src/anki/gr/vulkan/BufferImpl.cpp

@@ -27,6 +27,8 @@ BufferImpl::~BufferImpl()
 Error BufferImpl::init(const BufferInitInfo& inf)
 {
 	ANKI_ASSERT(!isCreated());
+	const Bool exposeGpuAddress = !!(getGrManagerImpl().getExtensions() & VulkanExtensions::KHR_RAY_TRACING)
+								  && !!(inf.m_usage & ~BufferUsageBit::ALL_TRANSFER);
 
 	PtrSize size = inf.m_size;
 	BufferMapAccessBit access = inf.m_access;
@@ -43,7 +45,7 @@ Error BufferImpl::init(const BufferInitInfo& inf)
 	ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
 	ci.size = size;
 	ci.usage = convertBufferUsageBit(usage);
-	if(inf.m_exposeGpuAddress)
+	if(exposeGpuAddress)
 	{
 		ci.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
 	}
@@ -150,8 +152,7 @@ Error BufferImpl::init(const BufferInitInfo& inf)
 	m_memoryFlags = props.memoryTypes[memIdx].propertyFlags;
 
 	// Allocate
-	getGrManagerImpl().getGpuMemoryManager().allocateMemory(memIdx, req.size, U32(req.alignment), true,
-															inf.m_exposeGpuAddress, m_memHandle);
+	getGrManagerImpl().getGpuMemoryManager().allocateMemory(memIdx, req.size, U32(req.alignment), true, m_memHandle);
 
 	// Bind mem to buffer
 	{
@@ -160,7 +161,7 @@ Error BufferImpl::init(const BufferInitInfo& inf)
 	}
 
 	// Get GPU buffer address
-	if(inf.m_exposeGpuAddress)
+	if(exposeGpuAddress)
 	{
 		VkBufferDeviceAddressInfo info = {};
 		info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;

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

@@ -789,7 +789,6 @@ void CommandBufferImpl::buildAccelerationStructureInternal(AccelerationStructure
 
 	// Create the scrach buffer
 	BufferInitInfo bufferInit;
-	bufferInit.m_exposeGpuAddress = true;
 	bufferInit.m_usage = BufferImpl::ACCELERATION_STRUCTURE_BUILD_SCRATCH_USAGE;
 	bufferInit.m_size = asImpl.getBuildScratchBufferSize();
 	BufferPtr scratchBuff = getManager().newBuffer(bufferInit);

+ 18 - 53
src/anki/gr/vulkan/GpuMemoryManager.cpp

@@ -192,7 +192,7 @@ void GpuMemoryManager::destroy()
 	m_callocs.destroy(m_alloc);
 }
 
-void GpuMemoryManager::init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc)
+void GpuMemoryManager::init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc, Bool exposeBufferGpuAddress)
 {
 	ANKI_ASSERT(pdev);
 	ANKI_ASSERT(dev);
@@ -212,52 +212,34 @@ void GpuMemoryManager::init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8>
 	m_ifaces.create(alloc, m_memoryProperties.memoryTypeCount);
 	for(U32 memTypeIdx = 0; memTypeIdx < m_ifaces.getSize(); ++memTypeIdx)
 	{
-		for(U32 type = 0; type < 2; ++type)
+		for(U32 linear = 0; linear < 2; ++linear)
 		{
-			m_ifaces[memTypeIdx][type].m_alloc = alloc;
-			m_ifaces[memTypeIdx][type].m_dev = dev;
-			m_ifaces[memTypeIdx][type].m_memTypeIdx = U8(memTypeIdx);
-			m_ifaces[memTypeIdx][type].m_exposesBufferGpuAddress = (type == 1);
+			m_ifaces[memTypeIdx][linear].m_alloc = alloc;
+			m_ifaces[memTypeIdx][linear].m_dev = dev;
+			m_ifaces[memTypeIdx][linear].m_memTypeIdx = U8(memTypeIdx);
+			m_ifaces[memTypeIdx][linear].m_exposesBufferGpuAddress = (linear == 1) && exposeBufferGpuAddress;
 		}
 	}
 
-	// One allocator per type per linear/non-linear resources
+	// One allocator per linear/non-linear resources
 	m_callocs.create(alloc, m_memoryProperties.memoryTypeCount);
 	for(U32 memTypeIdx = 0; memTypeIdx < m_callocs.getSize(); ++memTypeIdx)
 	{
-		for(U32 type = 0; type < 3; ++type)
+		for(U32 linear = 0; linear < 2; ++linear)
 		{
-			const Bool exposesBufferGpuAddress = (type == 2);
-			ANKI_ASSERT(m_ifaces[memTypeIdx][exposesBufferGpuAddress].m_exposesBufferGpuAddress
-						== exposesBufferGpuAddress);
-			m_callocs[memTypeIdx][type].init(m_alloc, &m_ifaces[memTypeIdx][exposesBufferGpuAddress]);
+			m_callocs[memTypeIdx][linear].init(m_alloc, &m_ifaces[memTypeIdx][linear]);
 
 			const U32 heapIdx = m_memoryProperties.memoryTypes[memTypeIdx].heapIndex;
-			m_callocs[memTypeIdx][type].m_isDeviceMemory =
+			m_callocs[memTypeIdx][linear].m_isDeviceMemory =
 				!!(m_memoryProperties.memoryHeaps[heapIdx].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT);
 		}
 	}
 }
 
 void GpuMemoryManager::allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, Bool linearResource,
-									  Bool exposesBufferGpuAddress, GpuMemoryHandle& handle)
+									  GpuMemoryHandle& handle)
 {
-	U32 type;
-	if(!linearResource)
-	{
-		type = 0;
-	}
-	else if(!exposesBufferGpuAddress)
-	{
-		type = 1;
-	}
-	else
-	{
-		ANKI_ASSERT(linearResource);
-		type = 2;
-	}
-
-	ClassGpuAllocator& calloc = m_callocs[memTypeIdx][type];
+	ClassGpuAllocator& calloc = m_callocs[memTypeIdx][linearResource];
 	const Error err = calloc.allocate(size, alignment, handle.m_classHandle);
 	(void)err;
 
@@ -265,29 +247,12 @@ void GpuMemoryManager::allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignmen
 	handle.m_offset = handle.m_classHandle.m_offset;
 	handle.m_linear = linearResource;
 	handle.m_memTypeIdx = U8(memTypeIdx);
-	handle.m_exposesBufferGpuAddress = exposesBufferGpuAddress;
 }
 
 void GpuMemoryManager::freeMemory(GpuMemoryHandle& handle)
 {
 	ANKI_ASSERT(handle);
-
-	U32 type;
-	if(handle.m_exposesBufferGpuAddress)
-	{
-		type = 2;
-	}
-	else if(handle.m_linear)
-	{
-		type = 1;
-	}
-	else
-	{
-		ANKI_ASSERT(!handle.m_exposesBufferGpuAddress);
-		type = 0;
-	}
-
-	ClassGpuAllocator& calloc = m_callocs[handle.m_memTypeIdx][type];
+	ClassGpuAllocator& calloc = m_callocs[handle.m_memTypeIdx][handle.m_linear];
 	calloc.free(handle.m_classHandle);
 
 	handle = {};
@@ -297,7 +262,7 @@ void* GpuMemoryManager::getMappedAddress(GpuMemoryHandle& handle)
 {
 	ANKI_ASSERT(handle);
 
-	Interface& iface = m_ifaces[handle.m_memTypeIdx][handle.m_exposesBufferGpuAddress];
+	Interface& iface = m_ifaces[handle.m_memTypeIdx][handle.m_linear];
 	U8* out = static_cast<U8*>(iface.mapMemory(handle.m_classHandle.m_memory));
 	return static_cast<void*>(out + handle.m_offset);
 }
@@ -351,15 +316,15 @@ void GpuMemoryManager::getAllocatedMemory(PtrSize& gpuMemory, PtrSize& cpuMemory
 
 	for(U32 memTypeIdx = 0; memTypeIdx < m_callocs.getSize(); ++memTypeIdx)
 	{
-		for(U32 type = 0; type < 3; ++type)
+		for(U32 linear = 0; linear < 2; ++linear)
 		{
-			if(m_callocs[memTypeIdx][type].m_isDeviceMemory)
+			if(m_callocs[memTypeIdx][linear].m_isDeviceMemory)
 			{
-				gpuMemory += m_callocs[memTypeIdx][type].getAllocatedMemory();
+				gpuMemory += m_callocs[memTypeIdx][linear].getAllocatedMemory();
 			}
 			else
 			{
-				cpuMemory += m_callocs[memTypeIdx][type].getAllocatedMemory();
+				cpuMemory += m_callocs[memTypeIdx][linear].getAllocatedMemory();
 			}
 		}
 	}

+ 3 - 5
src/anki/gr/vulkan/GpuMemoryManager.h

@@ -32,7 +32,6 @@ private:
 	ClassGpuAllocatorHandle m_classHandle;
 	U8 m_memTypeIdx = MAX_U8;
 	Bool m_linear = false;
-	Bool m_exposesBufferGpuAddress = false;
 };
 
 /// Dynamic GPU memory allocator for all types.
@@ -43,13 +42,12 @@ public:
 
 	~GpuMemoryManager();
 
-	void init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc);
+	void init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8> alloc, Bool exposeBufferGpuAddress);
 
 	void destroy();
 
 	/// Allocate memory.
-	void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, Bool linearResource, Bool exposesBufferGpuAddress,
-						GpuMemoryHandle& handle);
+	void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, Bool linearResource, GpuMemoryHandle& handle);
 
 	/// Free memory.
 	void freeMemory(GpuMemoryHandle& handle);
@@ -71,7 +69,7 @@ private:
 
 	GrAllocator<U8> m_alloc;
 	DynamicArray<Array<Interface, 2>> m_ifaces;
-	DynamicArray<Array<ClassAllocator, 3>> m_callocs;
+	DynamicArray<Array<ClassAllocator, 2>> m_callocs;
 	VkPhysicalDeviceMemoryProperties m_memoryProperties;
 };
 /// @}

+ 2 - 1
src/anki/gr/vulkan/GrManagerImpl.cpp

@@ -678,7 +678,8 @@ Error GrManagerImpl::initMemory(const ConfigSet& cfg)
 					 ANKI_FORMAT_U32(m_memoryProperties.memoryTypes[i].propertyFlags));
 	}
 
-	m_gpuMemManager.init(m_physicalDevice, m_device, getAllocator());
+	m_gpuMemManager.init(m_physicalDevice, m_device, getAllocator(),
+						 !!(m_extensions & VulkanExtensions::KHR_RAY_TRACING));
 
 	return Error::NONE;
 }

+ 1 - 1
src/anki/gr/vulkan/TextureImpl.cpp

@@ -349,7 +349,7 @@ Error TextureImpl::initImage(const TextureInitInfo& init_)
 		// Allocate
 		getGrManagerImpl().getGpuMemoryManager().allocateMemory(memIdx, requirements.memoryRequirements.size,
 																U32(requirements.memoryRequirements.alignment), false,
-																false, m_memHandle);
+																m_memHandle);
 
 		// Bind mem to image
 		ANKI_TRACE_SCOPED_EVENT(VK_BIND_OBJECT);

+ 0 - 3
tests/gr/Gr.cpp

@@ -2306,7 +2306,6 @@ void main()
 	info.m_size = sizeof(Vec4) * 2;
 	info.m_usage = BufferUsageBit::ALL_COMPUTE;
 	info.m_access = BufferMapAccessBit::WRITE;
-	info.m_exposeGpuAddress = true;
 	BufferPtr ptrBuff = gr->newBuffer(info);
 
 	Vec4* mapped = static_cast<Vec4*>(ptrBuff->map(0, MAX_PTR_SIZE, BufferMapAccessBit::WRITE));
@@ -2367,7 +2366,6 @@ ANKI_TEST(Gr, RayQueries)
 		BufferInitInfo init;
 		init.m_access = BufferMapAccessBit::WRITE;
 		init.m_usage = BufferUsageBit::INDEX;
-		init.m_exposeGpuAddress = true;
 		init.m_size = sizeof(indices);
 		idxBuffer = gr->newBuffer(init);
 
@@ -2385,7 +2383,6 @@ ANKI_TEST(Gr, RayQueries)
 		BufferInitInfo init;
 		init.m_access = BufferMapAccessBit::WRITE;
 		init.m_usage = BufferUsageBit::VERTEX;
-		init.m_exposeGpuAddress = true;
 		init.m_size = sizeof(verts);
 		vertBuffer = gr->newBuffer(init);