From b359936c04e496c2c5a5a75d4aa5b0c4110aea4e Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 13 Sep 2024 22:12:57 +0200 Subject: [PATCH] Zones can inherit custom colors from parents. Co-authored-by: Martijn Courteaux --- manual/tracy.tex | 1 + profiler/src/profiler/TracyColor.hpp | 8 ++++ profiler/src/profiler/TracyTimelineDraw.hpp | 1 + .../src/profiler/TracyTimelineItemThread.cpp | 40 ++++++++++++++----- .../src/profiler/TracyTimelineItemThread.hpp | 4 +- profiler/src/profiler/TracyUserData.cpp | 2 + profiler/src/profiler/TracyView.hpp | 2 +- profiler/src/profiler/TracyViewData.hpp | 1 + profiler/src/profiler/TracyView_Options.cpp | 3 ++ profiler/src/profiler/TracyView_Utility.cpp | 10 ++--- .../src/profiler/TracyView_ZoneTimeline.cpp | 4 +- 11 files changed, 57 insertions(+), 19 deletions(-) diff --git a/manual/tracy.tex b/manual/tracy.tex index b987a133..dcfc060f 100644 --- a/manual/tracy.tex +++ b/manual/tracy.tex @@ -3412,6 +3412,7 @@ In this window, you can set various trace-related options. For example, the time \item \emph{Source location dynamic} -- Zone color is determined by source location (function name) and depth level. \end{itemize} Enabling the \emph{Ignore custom} option will force usage of the selected zone coloring scheme, disregarding any colors set by the user in profiled code. +Enabling the \emph{Inherit parent colors} option will cause zones that have a color set by the user in the profiled code to be propagated down to the child zones, although slightly darker. \item \emph{\faRulerHorizontal{} Zone name shortening} -- controls display behavior of long zone names, which don't fit inside a zone box: \begin{itemize} \item \emph{Disabled} -- Shortening of zone names is not performed and names are always displayed in full (e.g.\ \texttt{bool ns::container::add(const float\&)}). diff --git a/profiler/src/profiler/TracyColor.hpp b/profiler/src/profiler/TracyColor.hpp index 98712679..ec1dfe6e 100644 --- a/profiler/src/profiler/TracyColor.hpp +++ b/profiler/src/profiler/TracyColor.hpp @@ -20,6 +20,14 @@ static tracy_force_inline uint32_t HighlightColor( uint32_t color ) ( std::min( 0xFF, ( ( ( color & 0x000000FF ) ) + V ) ) ); } +static tracy_force_inline uint32_t DarkenColorSlightly( uint32_t color ) +{ + return 0xFF000000 | + ( ( ( ( color & 0x00FF0000 ) >> 16 ) * 4 / 5 ) << 16 ) | + ( ( ( ( color & 0x0000FF00 ) >> 8 ) * 4 / 5 ) << 8 ) | + ( ( ( ( color & 0x000000FF ) ) * 4 / 5 ) ); +} + static tracy_force_inline uint32_t DarkenColor( uint32_t color ) { return 0xFF000000 | diff --git a/profiler/src/profiler/TracyTimelineDraw.hpp b/profiler/src/profiler/TracyTimelineDraw.hpp index c5aa73a3..3c814d3b 100644 --- a/profiler/src/profiler/TracyTimelineDraw.hpp +++ b/profiler/src/profiler/TracyTimelineDraw.hpp @@ -24,6 +24,7 @@ struct TimelineDraw short_ptr ev; Int48 rend; uint32_t num; + uint32_t inheritedColor; }; diff --git a/profiler/src/profiler/TracyTimelineItemThread.cpp b/profiler/src/profiler/TracyTimelineItemThread.cpp index 08a4f447..60b11037 100644 --- a/profiler/src/profiler/TracyTimelineItemThread.cpp +++ b/profiler/src/profiler/TracyTimelineItemThread.cpp @@ -1,6 +1,7 @@ #include #include +#include "TracyColor.hpp" #include "TracyImGui.hpp" #include "TracyLockHelpers.hpp" #include "TracyMouse.hpp" @@ -300,7 +301,7 @@ void TimelineItemThread::Preprocess( const TimelineContext& ctx, TaskDispatch& t else #endif { - m_depth = PreprocessZoneLevel( ctx, m_thread->timeline, 0, visible ); + m_depth = PreprocessZoneLevel( ctx, m_thread->timeline, 0, visible, 0 ); } } ); @@ -399,20 +400,20 @@ int TimelineItemThread::PreprocessGhostLevel( const TimelineContext& ctx, const } #endif -int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const Vector>& vec, int depth, bool visible ) +int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const Vector>& vec, int depth, bool visible, uint32_t inheritedColor ) { if( vec.is_magic() ) { - return PreprocessZoneLevel>( ctx, *(Vector*)( &vec ), depth, visible ); + return PreprocessZoneLevel>( ctx, *(Vector*)( &vec ), depth, visible, inheritedColor ); } else { - return PreprocessZoneLevel>( ctx, vec, depth, visible ); + return PreprocessZoneLevel>( ctx, vec, depth, visible, inheritedColor ); } } template -int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const V& vec, int depth, bool visible ) +int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const V& vec, int depth, bool visible, uint32_t inheritedColor ) { const auto vStart = ctx.vStart; const auto vEnd = ctx.vEnd; @@ -450,17 +451,38 @@ int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const V if( nt - pt >= MinVisNs ) break; nextTime = nt + MinVisNs; } - if( visible ) m_draw.emplace_back( TimelineDraw { TimelineDrawType::Folded, uint16_t( depth ), (void**)&ev, m_worker.GetZoneEnd( a(*(next-1)) ), uint32_t( next - it ) } ); + if( visible ) m_draw.emplace_back( TimelineDraw { TimelineDrawType::Folded, uint16_t( depth ), (void**)&ev, m_worker.GetZoneEnd( a(*(next-1)) ), uint32_t( next - it ), inheritedColor } ); it = next; } else { - if( ev.HasChildren() ) + const auto hasChildren = ev.HasChildren(); + auto currentInherited = inheritedColor; + if( m_view.GetViewData().inheritParentColors ) { - const auto d = PreprocessZoneLevel( ctx, m_worker.GetZoneChildren( ev.Child() ), depth + 1, visible ); + uint32_t color = 0; + if( m_worker.HasZoneExtra( ev ) ) + { + const auto& extra = m_worker.GetZoneExtra( ev ); + color = extra.color.Val(); + } + if( color == 0 ) + { + auto& srcloc = m_worker.GetSourceLocation( ev.SrcLoc() ); + color = srcloc.color; + } + if( color != 0 ) + { + currentInherited = color | 0xFF000000; + if( hasChildren ) inheritedColor = DarkenColorSlightly( color ); + } + } + if( hasChildren ) + { + const auto d = PreprocessZoneLevel( ctx, m_worker.GetZoneChildren( ev.Child() ), depth + 1, visible, inheritedColor ); if( d > maxdepth ) maxdepth = d; } - if( visible ) m_draw.emplace_back( TimelineDraw { TimelineDrawType::Zone, uint16_t( depth ), (void**)&ev } ); + if( visible ) m_draw.emplace_back( TimelineDraw { TimelineDrawType::Zone, uint16_t( depth ), (void**)&ev, 0, 0, currentInherited } ); ++it; } } diff --git a/profiler/src/profiler/TracyTimelineItemThread.hpp b/profiler/src/profiler/TracyTimelineItemThread.hpp index a78080c8..61e41d34 100644 --- a/profiler/src/profiler/TracyTimelineItemThread.hpp +++ b/profiler/src/profiler/TracyTimelineItemThread.hpp @@ -37,10 +37,10 @@ private: #ifndef TRACY_NO_STATISTICS int PreprocessGhostLevel( const TimelineContext& ctx, const Vector& vec, int depth, bool visible ); #endif - int PreprocessZoneLevel( const TimelineContext& ctx, const Vector>& vec, int depth, bool visible ); + int PreprocessZoneLevel( const TimelineContext& ctx, const Vector>& vec, int depth, bool visible, uint32_t inheritedColor ); template - int PreprocessZoneLevel( const TimelineContext& ctx, const V& vec, int depth, bool visible ); + int PreprocessZoneLevel( const TimelineContext& ctx, const V& vec, int depth, bool visible, uint32_t inheritedColor ); void PreprocessContextSwitches( const TimelineContext& ctx, const ContextSwitch& ctxSwitch, bool visible ); void PreprocessSamples( const TimelineContext& ctx, const Vector& vec, bool visible, int yPos ); diff --git a/profiler/src/profiler/TracyUserData.cpp b/profiler/src/profiler/TracyUserData.cpp index 71ba35a8..60184da9 100644 --- a/profiler/src/profiler/TracyUserData.cpp +++ b/profiler/src/profiler/TracyUserData.cpp @@ -145,6 +145,7 @@ void UserData::LoadState( ViewData& data ) if( ini_sget( ini, "options", "drawCpuUsageGraph", "%d", &v ) ) data.drawCpuUsageGraph = v; if( ini_sget( ini, "options", "drawSamples", "%d", &v ) ) data.drawSamples = v; if( ini_sget( ini, "options", "dynamicColors", "%d", &v ) ) data.dynamicColors = v; + if( ini_sget( ini, "options", "inheritParentColors", "%d", &v ) ) data.inheritParentColors = v; if( ini_sget( ini, "options", "forceColors", "%d", &v ) ) data.forceColors = v; if( ini_sget( ini, "options", "ghostZones", "%d", &v ) ) data.ghostZones = v; if( ini_sget( ini, "options", "frameTarget", "%d", &v ) ) data.frameTarget = v; @@ -194,6 +195,7 @@ void UserData::SaveState( const ViewData& data ) fprintf( f, "drawCpuUsageGraph = %d\n", data.drawCpuUsageGraph ); fprintf( f, "drawSamples = %d\n", data.drawSamples ); fprintf( f, "dynamicColors = %d\n", data.dynamicColors ); + fprintf( f, "inheritParentColors = %d\n", data.inheritParentColors ); fprintf( f, "forceColors = %d\n", data.forceColors ); fprintf( f, "ghostZones = %d\n", data.ghostZones ); fprintf( f, "frameTarget = %d\n", data.frameTarget ); diff --git a/profiler/src/profiler/TracyView.hpp b/profiler/src/profiler/TracyView.hpp index 6f7b76de..2323fb51 100644 --- a/profiler/src/profiler/TracyView.hpp +++ b/profiler/src/profiler/TracyView.hpp @@ -313,7 +313,7 @@ private: uint32_t GetRawSrcLocColor( const SourceLocation& srcloc, int depth ); uint32_t GetZoneColor( const ZoneEvent& ev, uint64_t thread, int depth ); uint32_t GetZoneColor( const GpuEvent& ev ); - ZoneColorData GetZoneColorData( const ZoneEvent& ev, uint64_t thread, int depth ); + ZoneColorData GetZoneColorData( const ZoneEvent& ev, uint64_t thread, int depth, uint32_t inheritedColor ); ZoneColorData GetZoneColorData( const GpuEvent& ev ); void ZoomToZone( const ZoneEvent& ev ); diff --git a/profiler/src/profiler/TracyViewData.hpp b/profiler/src/profiler/TracyViewData.hpp index d479079e..0d57ec45 100644 --- a/profiler/src/profiler/TracyViewData.hpp +++ b/profiler/src/profiler/TracyViewData.hpp @@ -53,6 +53,7 @@ struct ViewData uint8_t drawCpuUsageGraph = true; uint8_t drawSamples = true; uint8_t dynamicColors = 1; + uint8_t inheritParentColors = true; uint8_t forceColors = false; uint8_t ghostZones = true; ShortenName shortenName = ShortenName::NoSpaceAndNormalize; diff --git a/profiler/src/profiler/TracyView_Options.cpp b/profiler/src/profiler/TracyView_Options.cpp index 6a7f7ad3..e17d7665 100644 --- a/profiler/src/profiler/TracyView_Options.cpp +++ b/profiler/src/profiler/TracyView_Options.cpp @@ -225,6 +225,9 @@ void View::DrawOptions() ImGui::SameLine(); bool forceColors = m_vd.forceColors; if( SmallCheckbox( "Ignore custom", &forceColors ) ) m_vd.forceColors = forceColors; + ImGui::SameLine(); + bool inheritColors = m_vd.inheritParentColors; + if( SmallCheckbox( "Inherit parent colors", &inheritColors ) ) m_vd.inheritParentColors = inheritColors; ImGui::Indent(); ImGui::PushStyleVar( ImGuiStyleVar_FramePadding, ImVec2( 0, 0 ) ); ImGui::RadioButton( "Static", &ival, 0 ); diff --git a/profiler/src/profiler/TracyView_Utility.cpp b/profiler/src/profiler/TracyView_Utility.cpp index e341de59..f33b721e 100644 --- a/profiler/src/profiler/TracyView_Utility.cpp +++ b/profiler/src/profiler/TracyView_Utility.cpp @@ -76,27 +76,27 @@ uint32_t View::GetZoneColor( const GpuEvent& ev ) return color != 0 ? ( color | 0xFF000000 ) : 0xFF222288; } -View::ZoneColorData View::GetZoneColorData( const ZoneEvent& ev, uint64_t thread, int depth ) +View::ZoneColorData View::GetZoneColorData( const ZoneEvent& ev, uint64_t thread, int depth, uint32_t inheritedColor ) { ZoneColorData ret; const auto& srcloc = ev.SrcLoc(); if( m_zoneInfoWindow == &ev ) { - ret.color = GetZoneColor( ev, thread, depth ); + ret.color = inheritedColor ? inheritedColor : GetZoneColor( ev, thread, depth ); ret.accentColor = 0xFF44DD44; ret.thickness = 3.f; ret.highlight = true; } else if( m_zoneHighlight == &ev ) { - ret.color = GetZoneColor( ev, thread, depth ); + ret.color = inheritedColor ? inheritedColor : GetZoneColor( ev, thread, depth ); ret.accentColor = 0xFF4444FF; ret.thickness = 3.f; ret.highlight = true; } else if( m_zoneSrcLocHighlight == srcloc ) { - ret.color = GetZoneColor( ev, thread, depth ); + ret.color = inheritedColor ? inheritedColor : GetZoneColor( ev, thread, depth ); ret.accentColor = 0xFFEEEEEE; ret.thickness = 1.f; ret.highlight = true; @@ -119,7 +119,7 @@ View::ZoneColorData View::GetZoneColorData( const ZoneEvent& ev, uint64_t thread } else { - const auto color = GetZoneColor( ev, thread, depth ); + const auto color = inheritedColor ? inheritedColor : GetZoneColor( ev, thread, depth ); ret.color = color; ret.accentColor = HighlightColor( color ); ret.thickness = 1.f; diff --git a/profiler/src/profiler/TracyView_ZoneTimeline.cpp b/profiler/src/profiler/TracyView_ZoneTimeline.cpp index e3ebdd98..4612952a 100644 --- a/profiler/src/profiler/TracyView_ZoneTimeline.cpp +++ b/profiler/src/profiler/TracyView_ZoneTimeline.cpp @@ -223,7 +223,7 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector