From 98ecedfaaefdd81e7afab6c7f4ba0722c93cc175 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= <tseeker@nocternity.net>
Date: Sun, 24 Dec 2017 18:40:12 +0100
Subject: [PATCH] Profiler - Display CPU/GPU times on tooltips

---
 ui-profiling.cc | 27 ++++++++++++++++++++++++---
 ui-profiling.hh |  2 ++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/ui-profiling.cc b/ui-profiling.cc
index 2fd9f26..a29981d 100644
--- a/ui-profiling.cc
+++ b/ui-profiling.cc
@@ -56,6 +56,8 @@ void T_Profiler::endFrame( )
 	}
 
 	while ( secDurations_.size( ) < n ) {
+		secCPUDurations_.add( 0 );
+		secGPUDurations_.add( 0 );
 		secDurations_.add( 0 );
 		secStarts_.add( 0 );
 	}
@@ -63,6 +65,8 @@ void T_Profiler::endFrame( )
 	for ( auto i = 0u ; i < n ; i ++ ) {
 		const float cpuD = computeDuration( samples_[ i ] );
 		const float gpuD = computeDuration( gpuSamples_[ i ] );
+		secCPUDurations_[ i ] = cpuD;
+		secGPUDurations_[ i ] = gpuD;
 		secDurations_[ i ] = std::max( cpuD , gpuD );
 		if ( parents_[ i ] != Invalid ) {
 			assert( parents_[ i ] < i );
@@ -158,11 +162,28 @@ void T_Profiler::makeUI( )
 
 		ebcl::T_StringBuilder sb;
 		char tms[ 12 ];
-		snprintf( tms , 12 , "%.3f" , secDurations_[ i ] );
-		sb << sections_[ i ] << " ("
-			<< tms << "ms)" << '\0';
+		snprintf( tms , 12 , "%.1f" , secDurations_[ i ] );
+		sb << sections_[ i ] << " (" << tms << "ms)" << '\0';
 		ImGui::Checkbox( sb.data( ) , (bool*) &displayed_[ i ] );
 		ImGui::PopStyleColor( );
+		if ( ImGui::IsItemHovered( ) ) {
+			ImGui::BeginTooltip( );
+
+			snprintf( tms , 12 , "%.3f" , secDurations_[ i ] );
+			sb.clear( ) << sections_[ i ] << '\0';
+			ImGui::PushStyleColor( ImGuiCol_Text , color );
+			ImGui::Text( "%s" , sb.data( ) );
+			ImGui::PopStyleColor( );
+
+			sb.clear( ) << "\nTime: " << tms << "ms\n\nCPU: ";
+			snprintf( tms , 12 , "%.3f" , secCPUDurations_[ i ] );
+			sb << tms << "ms\nGPU: ";
+			snprintf( tms , 12 , "%.3f" , secGPUDurations_[ i ] );
+			sb << tms << "ms" << '\0';
+			ImGui::Text( "%s" , sb.data( ) );
+
+			ImGui::EndTooltip( );
+		}
 	}
 	ImGui::EndChild( );
 	ImGui::SameLine( );
diff --git a/ui-profiling.hh b/ui-profiling.hh
index f6c251c..38a345a 100644
--- a/ui-profiling.hh
+++ b/ui-profiling.hh
@@ -63,6 +63,8 @@ struct T_Profiler
 	T_Data_ samples_;
 	T_Array< uint64_t > cpuStarts_;
 
+	T_Array< float > secCPUDurations_;
+	T_Array< float > secGPUDurations_;
 	T_Array< float > secDurations_;
 	T_Array< float > secStarts_;