Skip to content

Commit

Permalink
Zones can inherit custom colors from parents.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcourteaux committed Sep 13, 2024
1 parent 8724400 commit 01b75d7
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 27 deletions.
1 change: 1 addition & 0 deletions manual/tracy.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3412,6 +3412,7 @@ \subsection{Options menu}
\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<float>::add(const float\&)}).
Expand Down
8 changes: 8 additions & 0 deletions profiler/src/profiler/TracyColor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ static tracy_force_inline uint32_t HighlightColor( uint32_t color )
( std::min<int>( 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 |
Expand Down
1 change: 1 addition & 0 deletions profiler/src/profiler/TracyTimelineDraw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct TimelineDraw
short_ptr<void*> ev;
Int48 rend;
uint32_t num;
uint32_t customColor;
};


Expand Down
30 changes: 22 additions & 8 deletions profiler/src/profiler/TracyTimelineItemThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "TracyTimelineItemThread.hpp"
#include "TracyView.hpp"
#include "TracyWorker.hpp"
#include "TracyColor.hpp"

namespace tracy
{
Expand Down Expand Up @@ -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 );
}
} );

Expand Down Expand Up @@ -399,20 +400,20 @@ int TimelineItemThread::PreprocessGhostLevel( const TimelineContext& ctx, const
}
#endif

int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const Vector<short_ptr<ZoneEvent>>& vec, int depth, bool visible )
int TimelineItemThread::PreprocessZoneLevel( const TimelineContext& ctx, const Vector<short_ptr<ZoneEvent>>& vec, int depth, bool visible, uint32_t parentCustomColor )
{
if( vec.is_magic() )
{
return PreprocessZoneLevel<VectorAdapterDirect<ZoneEvent>>( ctx, *(Vector<ZoneEvent>*)( &vec ), depth, visible );
return PreprocessZoneLevel<VectorAdapterDirect<ZoneEvent>>( ctx, *(Vector<ZoneEvent>*)( &vec ), depth, visible, parentCustomColor );
}
else
{
return PreprocessZoneLevel<VectorAdapterPointer<ZoneEvent>>( ctx, vec, depth, visible );
return PreprocessZoneLevel<VectorAdapterPointer<ZoneEvent>>( ctx, vec, depth, visible, parentCustomColor );
}
}

template<typename Adapter, typename V>
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 parentCustomColor )
{
const auto vStart = ctx.vStart;
const auto vEnd = ctx.vEnd;
Expand Down Expand Up @@ -450,17 +451,30 @@ 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 ), parentCustomColor } );
it = next;
}
else
{
uint32_t customColor = m_view.GetZoneCustomColor( ev, m_thread->id );
uint32_t childCustomColor = 0;
if( m_view.GetViewData().inheritParentColors )
{
if( customColor != 0 )
{
childCustomColor = DarkenColorSlightly( customColor );
} else
{
customColor = parentCustomColor;
childCustomColor = parentCustomColor;
}
}
if( ev.HasChildren() )
{
const auto d = PreprocessZoneLevel( ctx, m_worker.GetZoneChildren( ev.Child() ), depth + 1, visible );
const auto d = PreprocessZoneLevel( ctx, m_worker.GetZoneChildren( ev.Child() ), depth + 1, visible, childCustomColor );
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, customColor } );
++it;
}
}
Expand Down
4 changes: 2 additions & 2 deletions profiler/src/profiler/TracyTimelineItemThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class TimelineItemThread final : public TimelineItem
#ifndef TRACY_NO_STATISTICS
int PreprocessGhostLevel( const TimelineContext& ctx, const Vector<GhostZone>& vec, int depth, bool visible );
#endif
int PreprocessZoneLevel( const TimelineContext& ctx, const Vector<short_ptr<ZoneEvent>>& vec, int depth, bool visible );
int PreprocessZoneLevel( const TimelineContext& ctx, const Vector<short_ptr<ZoneEvent>>& vec, int depth, bool visible, uint32_t parentCustomColor );

template<typename Adapter, typename V>
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 parentCustomColor );

void PreprocessContextSwitches( const TimelineContext& ctx, const ContextSwitch& ctxSwitch, bool visible );
void PreprocessSamples( const TimelineContext& ctx, const Vector<SampleData>& vec, bool visible, int yPos );
Expand Down
2 changes: 2 additions & 0 deletions profiler/src/profiler/TracyUserData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
9 changes: 7 additions & 2 deletions profiler/src/profiler/TracyView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,17 @@ class View

void AddAnnotation( int64_t start, int64_t end );

public:
uint32_t GetThreadColor( uint64_t thread, int depth );
uint32_t GetSrcLocColor( const SourceLocation& srcloc, int depth );
uint32_t GetRawSrcLocColor( const SourceLocation& srcloc, int depth );
uint32_t GetZoneColor( const ZoneEvent& ev, uint64_t thread, int depth );
uint32_t GetZoneColor( const ZoneEvent& ev, uint64_t thread, int depth, uint32_t customColor );
uint32_t GetZoneCustomColor( const ZoneEvent& ev, uint64_t thread );
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 customColor );
ZoneColorData GetZoneColorData( const GpuEvent& ev );

private:
void ZoomToZone( const ZoneEvent& ev );
void ZoomToZone( const GpuEvent& ev );
void ZoomToPrevFrame();
Expand All @@ -331,6 +334,7 @@ class View
void CallstackTooltipContents( uint32_t idx );
void CrashTooltip();

public:
const ZoneEvent* GetZoneParent( const ZoneEvent& zone ) const;
const ZoneEvent* GetZoneParent( const ZoneEvent& zone, uint64_t tid ) const;
const ZoneEvent* GetZoneChild( const ZoneEvent& zone, int64_t time ) const;
Expand All @@ -348,6 +352,7 @@ class View
const char* GetFrameSetName( const FrameData& fd ) const;
static const char* GetFrameSetName( const FrameData& fd, const Worker& worker );

private:
#ifndef TRACY_NO_STATISTICS
void FindZones();
void FindZonesCompare();
Expand Down
1 change: 1 addition & 0 deletions profiler/src/profiler/TracyViewData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct ViewData
uint8_t drawCpuUsageGraph = true;
uint8_t drawSamples = true;
uint8_t dynamicColors = 1;
uint8_t inheritParentColors = 1;
uint8_t forceColors = false;
uint8_t ghostZones = true;
ShortenName shortenName = ShortenName::NoSpaceAndNormalize;
Expand Down
3 changes: 3 additions & 0 deletions profiler/src/profiler/TracyView_Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
35 changes: 22 additions & 13 deletions profiler/src/profiler/TracyView_Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,29 @@ uint32_t View::GetSrcLocColor( const SourceLocation& srcloc, int depth )
return GetRawSrcLocColor( srcloc, depth );
}

uint32_t View::GetZoneColor( const ZoneEvent& ev, uint64_t thread, int depth )
uint32_t View::GetZoneCustomColor( const ZoneEvent& ev, uint64_t thread )
{
if( m_worker.HasZoneExtra( ev ) )
{
const auto custom_color = m_worker.GetZoneExtra( ev ).color.Val();
if( custom_color != 0 ) return custom_color | 0xFF000000;
}
const auto sl = ev.SrcLoc();
const auto& srcloc = m_worker.GetSourceLocation( sl );
const auto color = srcloc.color;
if( color != 0 ) return color | 0xFF000000;
return 0;
}

uint32_t View::GetZoneColor( const ZoneEvent& ev, uint64_t thread, int depth, uint32_t customColor )
{
if( !m_vd.forceColors )
{
if( m_worker.HasZoneExtra( ev ) )
{
const auto custom_color = m_worker.GetZoneExtra( ev ).color.Val();
if( custom_color != 0 ) return custom_color | 0xFF000000;
}
const auto color = srcloc.color;
if( color != 0 ) return color | 0xFF000000;
// First check if the zone itself has a color, before we fetch the chain-of-parents, which is expensive to do.
if( customColor != 0 ) return customColor | 0xFF000000;
}
const auto sl = ev.SrcLoc();
const auto& srcloc = m_worker.GetSourceLocation( sl );
switch( m_vd.dynamicColors )
{
case 0:
Expand All @@ -76,27 +85,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 customColor )
{
ZoneColorData ret;
const auto& srcloc = ev.SrcLoc();
if( m_zoneInfoWindow == &ev )
{
ret.color = GetZoneColor( ev, thread, depth );
ret.color = GetZoneColor( ev, thread, depth, customColor );
ret.accentColor = 0xFF44DD44;
ret.thickness = 3.f;
ret.highlight = true;
}
else if( m_zoneHighlight == &ev )
{
ret.color = GetZoneColor( ev, thread, depth );
ret.color = GetZoneColor( ev, thread, depth, customColor );
ret.accentColor = 0xFF4444FF;
ret.thickness = 3.f;
ret.highlight = true;
}
else if( m_zoneSrcLocHighlight == srcloc )
{
ret.color = GetZoneColor( ev, thread, depth );
ret.color = GetZoneColor( ev, thread, depth, customColor );
ret.accentColor = 0xFFEEEEEE;
ret.thickness = 1.f;
ret.highlight = true;
Expand All @@ -119,7 +128,7 @@ View::ZoneColorData View::GetZoneColorData( const ZoneEvent& ev, uint64_t thread
}
else
{
const auto color = GetZoneColor( ev, thread, depth );
const auto color = GetZoneColor( ev, thread, depth, customColor );
ret.color = color;
ret.accentColor = HighlightColor( color );
ret.thickness = 1.f;
Expand Down
4 changes: 2 additions & 2 deletions profiler/src/profiler/TracyView_ZoneTimeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
case TimelineDrawType::Folded:
{
auto& ev = *(const ZoneEvent*)v.ev.get();
const auto color = m_vd.dynamicColors == 2 ? 0xFF666666 : GetThreadColor( tid, v.depth );
const auto color = m_vd.dynamicColors == 2 ? 0xFF666666 : DarkenColor( GetZoneColor( ev, tid, v.depth, v.customColor ) );
const auto rend = v.rend.Val();
const auto px0 = ( ev.Start() - vStart ) * pxns;
const auto px1 = ( rend - vStart ) * pxns;
Expand Down Expand Up @@ -284,7 +284,7 @@ void View::DrawZoneList( const TimelineContext& ctx, const std::vector<TimelineD
auto& ev = *(const ZoneEvent*)v.ev.get();
const auto end = m_worker.GetZoneEnd( ev );
const auto zsz = std::max( ( end - ev.Start() ) * pxns, pxns * 0.5 );
const auto zoneColor = GetZoneColorData( ev, tid, v.depth );
const auto zoneColor = GetZoneColorData( ev, tid, v.depth, v.customColor );
const char* zoneName = m_worker.GetZoneName( ev );

auto tsz = ImGui::CalcTextSize( zoneName );
Expand Down

0 comments on commit 01b75d7

Please sign in to comment.