瀏覽代碼

Add some more stats in the left corner

Panagiotis Christopoulos Charitos 7 年之前
父節點
當前提交
a6f970c8bf

+ 10 - 1
src/anki/core/App.cpp

@@ -78,6 +78,9 @@ public:
 	U64 m_allocCount = 0;
 	U64 m_allocCount = 0;
 	U64 m_freeCount = 0;
 	U64 m_freeCount = 0;
 
 
+	U64 m_vkCpuMem = 0;
+	U64 m_vkGpuMem = 0;
+
 	static const U32 BUFFERED_FRAMES = 16;
 	static const U32 BUFFERED_FRAMES = 16;
 	U32 m_bufferedFrames = 0;
 	U32 m_bufferedFrames = 0;
 
 
@@ -104,7 +107,7 @@ public:
 
 
 		nk_style_push_style_item(ctx, &ctx->style.window.fixed_background, nk_style_item_color(nk_rgba(0, 0, 0, 128)));
 		nk_style_push_style_item(ctx, &ctx->style.window.fixed_background, nk_style_item_color(nk_rgba(0, 0, 0, 128)));
 
 
-		if(nk_begin(ctx, "Stats", nk_rect(5, 5, 200, 250), 0))
+		if(nk_begin(ctx, "Stats", nk_rect(5, 5, 230, 290), 0))
 		{
 		{
 			nk_layout_row_dynamic(ctx, 17, 1);
 			nk_layout_row_dynamic(ctx, 17, 1);
 
 
@@ -120,6 +123,8 @@ public:
 			labelBytes(ctx, m_allocatedCpuMem, "Total CPU");
 			labelBytes(ctx, m_allocatedCpuMem, "Total CPU");
 			labelUint(ctx, m_allocCount, "Total allocations");
 			labelUint(ctx, m_allocCount, "Total allocations");
 			labelUint(ctx, m_freeCount, "Total frees");
 			labelUint(ctx, m_freeCount, "Total frees");
+			labelBytes(ctx, m_vkCpuMem, "Vulkan CPU");
+			labelBytes(ctx, m_vkGpuMem, "Vulkan GPU");
 		}
 		}
 
 
 		nk_style_pop_style_item(ctx);
 		nk_style_pop_style_item(ctx);
@@ -657,6 +662,10 @@ Error App::mainLoop()
 			statsUi.m_allocatedCpuMem = m_memStats.m_allocatedMem.load();
 			statsUi.m_allocatedCpuMem = m_memStats.m_allocatedMem.load();
 			statsUi.m_allocCount = m_memStats.m_allocCount.load();
 			statsUi.m_allocCount = m_memStats.m_allocCount.load();
 			statsUi.m_freeCount = m_memStats.m_freeCount.load();
 			statsUi.m_freeCount = m_memStats.m_freeCount.load();
+
+			GrManagerStats grStats = m_gr->getStats();
+			statsUi.m_vkCpuMem = grStats.m_cpuMemory;
+			statsUi.m_vkGpuMem = grStats.m_gpuMemory;
 		}
 		}
 
 
 		++m_globalTimestamp;
 		++m_globalTimestamp;

+ 1 - 1
src/anki/core/Config.cpp

@@ -70,7 +70,7 @@ Config::Config()
 	newOption("core.storagePerFrameMemorySize", 16_MB);
 	newOption("core.storagePerFrameMemorySize", 16_MB);
 	newOption("core.vertexPerFrameMemorySize", 10_MB);
 	newOption("core.vertexPerFrameMemorySize", 10_MB);
 	newOption("core.textureBufferPerFrameMemorySize", 1_MB);
 	newOption("core.textureBufferPerFrameMemorySize", 1_MB);
-	newOption("core.mainThreadCount", getCpuCoresCount());
+	newOption("core.mainThreadCount", getCpuCoresCount() / 2);
 	newOption("core.displayStats", false);
 	newOption("core.displayStats", false);
 }
 }
 
 

+ 10 - 0
src/anki/gr/GrManager.h

@@ -32,6 +32,14 @@ public:
 	NativeWindow* m_window = nullptr;
 	NativeWindow* m_window = nullptr;
 };
 };
 
 
+/// Graphics statistics.
+class GrManagerStats
+{
+public:
+	PtrSize m_cpuMemory;
+	PtrSize m_gpuMemory;
+};
+
 /// The graphics manager, owner of all graphics objects.
 /// The graphics manager, owner of all graphics objects.
 class GrManager
 class GrManager
 {
 {
@@ -70,6 +78,8 @@ public:
 	ANKI_USE_RESULT RenderGraphPtr newRenderGraph();
 	ANKI_USE_RESULT RenderGraphPtr newRenderGraph();
 	/// @}
 	/// @}
 
 
+	GrManagerStats getStats() const;
+
 anki_internal:
 anki_internal:
 	GrAllocator<U8>& getAllocator()
 	GrAllocator<U8>& getAllocator()
 	{
 	{

+ 7 - 1
src/anki/gr/common/ClassGpuAllocator.cpp

@@ -20,7 +20,7 @@ public:
 	ClassGpuAllocatorMemory* m_mem;
 	ClassGpuAllocatorMemory* m_mem;
 
 
 	/// The in use slots mask.
 	/// The in use slots mask.
-	BitSet<MAX_SLOTS_PER_CHUNK, U8> m_inUseSlots = {false};
+	BitSet<MAX_SLOTS_PER_CHUNK, U64> m_inUseSlots = {false};
 
 
 	/// The number of in-use slots.
 	/// The number of in-use slots.
 	U32 m_inUseSlotCount = 0;
 	U32 m_inUseSlotCount = 0;
@@ -160,6 +160,8 @@ Error ClassGpuAllocator::createChunk(Class& cl, Chunk*& chunk)
 
 
 	cl.m_inUseChunks.pushBack(chunk);
 	cl.m_inUseChunks.pushBack(chunk);
 
 
+	// Update stats
+	m_allocatedMem += cl.m_chunkSize;
 	return Error::NONE;
 	return Error::NONE;
 }
 }
 
 
@@ -168,6 +170,10 @@ void ClassGpuAllocator::destroyChunk(Class& cl, Chunk& chunk)
 	cl.m_inUseChunks.erase(&chunk);
 	cl.m_inUseChunks.erase(&chunk);
 	m_iface->free(chunk.m_mem);
 	m_iface->free(chunk.m_mem);
 	m_alloc.deleteInstance(&chunk);
 	m_alloc.deleteInstance(&chunk);
+
+	// Update stats
+	ANKI_ASSERT(m_allocatedMem >= cl.m_chunkSize);
+	m_allocatedMem -= cl.m_chunkSize;
 }
 }
 
 
 Error ClassGpuAllocator::allocate(PtrSize size, U alignment, ClassGpuAllocatorHandle& handle)
 Error ClassGpuAllocator::allocate(PtrSize size, U alignment, ClassGpuAllocatorHandle& handle)

+ 7 - 0
src/anki/gr/common/ClassGpuAllocator.h

@@ -85,6 +85,11 @@ public:
 	/// Free allocated memory.
 	/// Free allocated memory.
 	void free(ClassGpuAllocatorHandle& handle);
 	void free(ClassGpuAllocatorHandle& handle);
 
 
+	PtrSize getAllocatedMemory() const
+	{
+		return m_allocatedMem;
+	}
+
 private:
 private:
 	using Class = ClassGpuAllocatorClass;
 	using Class = ClassGpuAllocatorClass;
 	using Chunk = ClassGpuAllocatorChunk;
 	using Chunk = ClassGpuAllocatorChunk;
@@ -95,6 +100,8 @@ private:
 	/// The memory classes.
 	/// The memory classes.
 	DynamicArray<Class> m_classes;
 	DynamicArray<Class> m_classes;
 
 
+	PtrSize m_allocatedMem = 0; ///< An estimate.
+
 	Class* findClass(PtrSize size, U alignment);
 	Class* findClass(PtrSize size, U alignment);
 
 
 	Chunk* findChunkWithUnusedSlot(Class& cl);
 	Chunk* findChunkWithUnusedSlot(Class& cl);

+ 2 - 2
src/anki/gr/vulkan/BufferImpl.cpp

@@ -76,7 +76,7 @@ Error BufferImpl::init(const BufferInitInfo& inf)
 		// Fallback: just host
 		// Fallback: just host
 		if(memIdx == MAX_U32)
 		if(memIdx == MAX_U32)
 		{
 		{
-			ANKI_VK_LOGW("Vulkan: Using a fallback mode for write-only buffer");
+			ANKI_VK_LOGW("Using a fallback mode for write-only buffer");
 			memIdx = getGrManagerImpl().getGpuMemoryManager().findMemoryType(
 			memIdx = getGrManagerImpl().getGpuMemoryManager().findMemoryType(
 				req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
 				req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
 		}
 		}
@@ -101,7 +101,7 @@ Error BufferImpl::init(const BufferInitInfo& inf)
 		// Fallback: Just host
 		// Fallback: Just host
 		if(memIdx == MAX_U32)
 		if(memIdx == MAX_U32)
 		{
 		{
-			ANKI_VK_LOGW("Vulkan: Using a fallback mode for read/write buffer");
+			ANKI_VK_LOGW("Using a fallback mode for read/write buffer");
 			memIdx = getGrManagerImpl().getGpuMemoryManager().findMemoryType(
 			memIdx = getGrManagerImpl().getGpuMemoryManager().findMemoryType(
 				req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
 				req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0);
 		}
 		}

+ 29 - 0
src/anki/gr/vulkan/GpuMemoryManager.cpp

@@ -157,6 +157,12 @@ public:
 	}
 	}
 };
 };
 
 
+class GpuMemoryManager::ClassAllocator : public ClassGpuAllocator
+{
+public:
+	Bool8 m_isDeviceMemory;
+};
+
 GpuMemoryManager::~GpuMemoryManager()
 GpuMemoryManager::~GpuMemoryManager()
 {
 {
 }
 }
@@ -206,6 +212,11 @@ void GpuMemoryManager::init(VkPhysicalDevice pdev, VkDevice dev, GrAllocator<U8>
 	for(U i = 0; i < m_callocs.getSize(); ++i)
 	for(U i = 0; i < m_callocs.getSize(); ++i)
 	{
 	{
 		m_callocs[i].init(m_alloc, &m_ifaces[i / 2]);
 		m_callocs[i].init(m_alloc, &m_ifaces[i / 2]);
+
+		const U memTypeIdx = i / 2;
+		const U heapIdx = m_memoryProperties.memoryTypes[memTypeIdx].heapIndex;
+		m_callocs[i].m_isDeviceMemory =
+			!!(m_memoryProperties.memoryHeaps[heapIdx].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT);
 	}
 	}
 }
 }
 
 
@@ -263,4 +274,22 @@ U GpuMemoryManager::findMemoryType(
 	return prefered;
 	return prefered;
 }
 }
 
 
+void GpuMemoryManager::getAllocatedMemory(PtrSize& gpuMemory, PtrSize& cpuMemory) const
+{
+	gpuMemory = 0;
+	cpuMemory = 0;
+
+	for(const ClassAllocator& alloc : m_callocs)
+	{
+		if(alloc.m_isDeviceMemory)
+		{
+			gpuMemory += alloc.getAllocatedMemory();
+		}
+		else
+		{
+			cpuMemory += alloc.getAllocatedMemory();
+		}
+	}
+}
+
 } // end namespace anki
 } // end namespace anki

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

@@ -58,14 +58,18 @@ public:
 	/// Find a suitable memory type.
 	/// Find a suitable memory type.
 	U findMemoryType(U resourceMemTypeBits, VkMemoryPropertyFlags preferFlags, VkMemoryPropertyFlags avoidFlags) const;
 	U findMemoryType(U resourceMemTypeBits, VkMemoryPropertyFlags preferFlags, VkMemoryPropertyFlags avoidFlags) const;
 
 
+	/// Get some statistics.
+	void getAllocatedMemory(PtrSize& gpuMemory, PtrSize& cpuMemory) const;
+
 private:
 private:
 	class Memory;
 	class Memory;
 	class Interface;
 	class Interface;
+	class ClassAllocator;
 
 
 	GrAllocator<U8> m_alloc;
 	GrAllocator<U8> m_alloc;
 	VkDevice m_dev;
 	VkDevice m_dev;
 	DynamicArray<Interface> m_ifaces;
 	DynamicArray<Interface> m_ifaces;
-	DynamicArray<ClassGpuAllocator> m_callocs;
+	DynamicArray<ClassAllocator> m_callocs;
 	VkPhysicalDeviceMemoryProperties m_memoryProperties;
 	VkPhysicalDeviceMemoryProperties m_memoryProperties;
 };
 };
 /// @}
 /// @}

+ 10 - 0
src/anki/gr/vulkan/GrManager.cpp

@@ -80,6 +80,16 @@ void GrManager::finish()
 	self.finish();
 	self.finish();
 }
 }
 
 
+GrManagerStats GrManager::getStats() const
+{
+	ANKI_VK_SELF_CONST(GrManagerImpl);
+	GrManagerStats out;
+
+	self.getGpuMemoryManager().getAllocatedMemory(out.m_gpuMemory, out.m_cpuMemory);
+
+	return out;
+}
+
 BufferPtr GrManager::newBuffer(const BufferInitInfo& init)
 BufferPtr GrManager::newBuffer(const BufferInitInfo& init)
 {
 {
 	return BufferPtr(Buffer::newInstance(this, init));
 	return BufferPtr(Buffer::newInstance(this, init));

+ 5 - 0
src/anki/gr/vulkan/GrManagerImpl.h

@@ -100,6 +100,11 @@ public:
 		return m_gpuMemManager;
 		return m_gpuMemManager;
 	}
 	}
 
 
+	const GpuMemoryManager& getGpuMemoryManager() const
+	{
+		return m_gpuMemManager;
+	}
+
 	const VkPhysicalDeviceMemoryProperties& getMemoryProperties() const
 	const VkPhysicalDeviceMemoryProperties& getMemoryProperties() const
 	{
 	{
 		return m_memoryProperties;
 		return m_memoryProperties;