Skip to content

Commit

Permalink
Zones can inherit custom colors from parents.
Browse files Browse the repository at this point in the history
Co-authored-by: Martijn Courteaux <courteauxmartijn@gmail.com>
  • Loading branch information
wolfpld and mcourteaux committed Sep 13, 2024
1 parent 8724400 commit b359936
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 19 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 inheritedColor;
};


Expand Down
40 changes: 31 additions & 9 deletions profiler/src/profiler/TracyTimelineItemThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <algorithm>
#include <limits>

#include "TracyColor.hpp"
#include "TracyImGui.hpp"
#include "TracyLockHelpers.hpp"
#include "TracyMouse.hpp"
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 inheritedColor )
{
if( vec.is_magic() )
{
return PreprocessZoneLevel<VectorAdapterDirect<ZoneEvent>>( ctx, *(Vector<ZoneEvent>*)( &vec ), depth, visible );
return PreprocessZoneLevel<VectorAdapterDirect<ZoneEvent>>( ctx, *(Vector<ZoneEvent>*)( &vec ), depth, visible, inheritedColor );
}
else
{
return PreprocessZoneLevel<VectorAdapterPointer<ZoneEvent>>( ctx, vec, depth, visible );
return PreprocessZoneLevel<VectorAdapterPointer<ZoneEvent>>( ctx, vec, depth, visible, inheritedColor );
}
}

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 inheritedColor )
{
const auto vStart = ctx.vStart;
const auto vEnd = ctx.vEnd;
Expand Down Expand Up @@ -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;
}
}
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 inheritedColor );

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 inheritedColor );

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
2 changes: 1 addition & 1 deletion profiler/src/profiler/TracyView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class View
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 );
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 = true;
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
10 changes: 5 additions & 5 deletions profiler/src/profiler/TracyView_Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
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 = v.inheritedColor ? v.inheritedColor : ( m_vd.dynamicColors == 2 ? 0xFF666666 : GetThreadColor( tid, v.depth ) );
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.inheritedColor );
const char* zoneName = m_worker.GetZoneName( ev );

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

0 comments on commit b359936

Please sign in to comment.