tracy/server/TracyView.cpp
2022-07-02 15:27:08 +02:00

3821 lines
157 KiB
C++

#ifdef _MSC_VER
# pragma warning( disable: 4267 ) // conversion from don't care to whatever, possible loss of data
#endif
#ifdef _WIN32
# include <malloc.h>
#else
# include <alloca.h>
#endif
#ifdef __MINGW32__
# define __STDC_FORMAT_MACROS
#endif
#include <algorithm>
#include <assert.h>
#include <chrono>
#include <inttypes.h>
#include <limits>
#include <math.h>
#include <mutex>
#include <numeric>
#include <sstream>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include "tracy_pdqsort.h"
#include "TracyColor.hpp"
#include "TracyFileRead.hpp"
#include "TracyFilesystem.hpp"
#include "TracyMouse.hpp"
#include "TracyPopcnt.hpp"
#include "TracyPrint.hpp"
#include "TracySort.hpp"
#include "TracySourceView.hpp"
#include "TracyView.hpp"
#include "../common/TracyStackFrames.hpp"
#include "imgui_internal.h"
#ifdef _WIN32
# include <windows.h>
#elif defined __linux__
# include <sys/sysinfo.h>
#elif defined __APPLE__ || defined BSD
# include <sys/types.h>
# include <sys/sysctl.h>
#endif
#include "IconsFontAwesome5.h"
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
namespace tracy
{
double s_time = 0;
enum { MinVisSize = 3 };
enum { MinFrameSize = 5 };
static View* s_instance = nullptr;
View::View( void(*cbMainThread)(std::function<void()>, bool), const char* addr, uint16_t port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb, SetScaleCallback sscb )
: m_worker( addr, port )
, m_staticView( false )
, m_viewMode( ViewMode::LastFrames )
, m_viewModeHeuristicTry( true )
, m_forceConnectionPopup( true, true )
, m_frames( nullptr )
, m_messagesScrollBottom( true )
, m_reactToCrash( true )
, m_reactToLostConnection( true )
, m_smallFont( smallFont )
, m_bigFont( bigFont )
, m_fixedFont( fixedWidth )
, m_stcb( stcb )
, m_gwcb( gwcb )
, m_sscb( sscb )
, m_userData()
, m_cbMainThread( cbMainThread )
{
assert( s_instance == nullptr );
s_instance = this;
InitMemory();
InitTextEditor( fixedWidth );
}
View::View( void(*cbMainThread)(std::function<void()>, bool), FileRead& f, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb, SetScaleCallback sscb )
: m_worker( f )
, m_filename( f.GetFilename() )
, m_staticView( true )
, m_viewMode( ViewMode::Paused )
, m_frames( m_worker.GetFramesBase() )
, m_messagesScrollBottom( false )
, m_smallFont( smallFont )
, m_bigFont( bigFont )
, m_fixedFont( fixedWidth )
, m_stcb( stcb )
, m_gwcb( gwcb )
, m_sscb( sscb )
, m_userData( m_worker.GetCaptureProgram().c_str(), m_worker.GetCaptureTime() )
, m_cbMainThread( cbMainThread )
{
assert( s_instance == nullptr );
s_instance = this;
m_notificationTime = 4;
m_notificationText = std::string( "Trace loaded in " ) + TimeToString( m_worker.GetLoadTime() );
InitMemory();
InitTextEditor( fixedWidth );
SetViewToLastFrames();
m_userData.StateShouldBePreserved();
m_userData.LoadState( m_vd );
m_userData.LoadAnnotations( m_annotations );
m_sourceRegexValid = m_userData.LoadSourceSubstitutions( m_sourceSubstitutions );
if( m_worker.GetCallstackFrameCount() == 0 ) m_showUnknownFrames = false;
if( m_worker.GetCallstackSampleCount() == 0 ) m_showAllSymbols = true;
}
View::~View()
{
m_worker.Shutdown();
m_userData.SaveState( m_vd );
m_userData.SaveAnnotations( m_annotations );
m_userData.SaveSourceSubstitutions( m_sourceSubstitutions );
if( m_compare.loadThread.joinable() ) m_compare.loadThread.join();
if( m_saveThread.joinable() ) m_saveThread.join();
if( m_frameTexture ) FreeTexture( m_frameTexture, m_cbMainThread );
if( m_playback.texture ) FreeTexture( m_playback.texture, m_cbMainThread );
assert( s_instance != nullptr );
s_instance = nullptr;
}
void View::InitMemory()
{
#ifdef _WIN32
MEMORYSTATUSEX statex;
statex.dwLength = sizeof( statex );
GlobalMemoryStatusEx( &statex );
m_totalMemory = statex.ullTotalPhys;
#elif defined __linux__
struct sysinfo sysInfo;
sysinfo( &sysInfo );
m_totalMemory = sysInfo.totalram;
#elif defined __APPLE__
size_t memSize;
size_t sz = sizeof( memSize );
sysctlbyname( "hw.memsize", &memSize, &sz, nullptr, 0 );
m_totalMemory = memSize;
#elif defined BSD
size_t memSize;
size_t sz = sizeof( memSize );
sysctlbyname( "hw.physmem", &memSize, &sz, nullptr, 0 );
m_totalMemory = memSize;
#else
m_totalMemory = 0;
#endif
}
void View::InitTextEditor( ImFont* font )
{
m_sourceView = std::make_unique<SourceView>( m_gwcb );
m_sourceViewFile = nullptr;
}
void View::ViewSource( const char* fileName, int line )
{
assert( fileName );
m_sourceViewFile = fileName;
m_sourceView->OpenSource( fileName, line, *this, m_worker );
}
void View::ViewSymbol( const char* fileName, int line, uint64_t baseAddr, uint64_t symAddr )
{
assert( fileName || symAddr );
m_sourceViewFile = fileName ? fileName : (const char*)~uint64_t( 0 );
m_sourceView->OpenSymbol( fileName, line, baseAddr, symAddr, m_worker, *this );
}
bool View::ViewDispatch( const char* fileName, int line, uint64_t symAddr )
{
if( line == 0 )
{
fileName = nullptr;
}
else
{
if( !SourceFileValid( fileName, m_worker.GetCaptureTime(), *this, m_worker ) )
{
fileName = nullptr;
line = 0;
}
}
if( symAddr == 0 )
{
if( line != 0 )
{
ViewSource( fileName, line );
return true;
}
return false;
}
else
{
uint64_t baseAddr = 0;
if( m_worker.HasSymbolCode( symAddr ) )
{
baseAddr = symAddr;
}
else
{
const auto parentAddr = m_worker.GetSymbolForAddress( symAddr );
if( parentAddr != 0 && m_worker.HasSymbolCode( parentAddr ) )
{
baseAddr = parentAddr;
}
}
if( baseAddr != 0 || line != 0 )
{
ViewSymbol( fileName, line, baseAddr, symAddr );
return true;
}
return false;
}
}
static const char* CompressionName[] = {
"LZ4",
"LZ4 HC",
"LZ4 HC extreme",
"Zstd",
nullptr
};
static const char* CompressionDesc[] = {
"Fastest save, fast load time, big file size",
"Slow save, fastest load time, reasonable file size",
"Very slow save, fastest load time, file smaller than LZ4 HC",
"Configurable save time (fast-slowest), reasonable load time, smallest file size",
nullptr
};
static_assert( sizeof( CompressionName ) == sizeof( CompressionDesc ), "Unmatched compression names and descriptions" );
bool View::Draw()
{
HandshakeStatus status = (HandshakeStatus)s_instance->m_worker.GetHandshakeStatus();
switch( status )
{
case HandshakeProtocolMismatch:
ImGui::OpenPopup( "Protocol mismatch" );
break;
case HandshakeNotAvailable:
ImGui::OpenPopup( "Client not ready" );
break;
case HandshakeDropped:
ImGui::OpenPopup( "Client disconnected" );
break;
default:
break;
}
const auto& failure = s_instance->m_worker.GetFailureType();
if( failure != Worker::Failure::None )
{
ImGui::OpenPopup( "Instrumentation failure" );
}
if( ImGui::BeginPopupModal( "Protocol mismatch", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
ImGui::PushFont( s_instance->m_bigFont );
TextCentered( ICON_FA_EXCLAMATION_TRIANGLE );
ImGui::PopFont();
ImGui::TextUnformatted( "The client you are trying to connect to uses incompatible protocol version.\nMake sure you are using the same Tracy version on both client and server." );
ImGui::Separator();
if( ImGui::Button( "My bad" ) )
{
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
return false;
}
ImGui::SameLine();
if( ImGui::Button( "Reconnect" ) )
{
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
s_instance->m_reconnectRequested = true;
return false;
}
ImGui::EndPopup();
}
if( ImGui::BeginPopupModal( "Client not ready", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
ImGui::PushFont( s_instance->m_bigFont );
TextCentered( ICON_FA_LIGHTBULB );
ImGui::PopFont();
ImGui::TextUnformatted( "The client you are trying to connect to is no longer able to sent profiling data,\nbecause another server was already connected to it.\nYou can do the following:\n\n 1. Restart the client application.\n 2. Rebuild the client application with on-demand mode enabled." );
ImGui::Separator();
if( ImGui::Button( "I understand" ) )
{
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
return false;
}
ImGui::SameLine();
if( ImGui::Button( "Reconnect" ) )
{
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
s_instance->m_reconnectRequested = true;
return false;
}
ImGui::EndPopup();
}
if( ImGui::BeginPopupModal( "Client disconnected", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
ImGui::PushFont( s_instance->m_bigFont );
TextCentered( ICON_FA_HANDSHAKE );
ImGui::PopFont();
ImGui::TextUnformatted( "The client you are trying to connect to has disconnected during the initial\nconnection handshake. Please check your network configuration." );
ImGui::Separator();
if( ImGui::Button( "Will do" ) )
{
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
return false;
}
ImGui::SameLine();
if( ImGui::Button( "Reconnect" ) )
{
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
s_instance->m_reconnectRequested = true;
return false;
}
ImGui::EndPopup();
}
if( ImGui::BeginPopupModal( "Instrumentation failure", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
const auto& data = s_instance->m_worker.GetFailureData();
ImGui::PushFont( s_instance->m_bigFont );
TextCentered( ICON_FA_SKULL );
ImGui::PopFont();
ImGui::TextUnformatted( "Profiling session terminated due to improper instrumentation.\nPlease correct your program and try again." );
ImGui::TextUnformatted( "Reason:" );
ImGui::SameLine();
ImGui::TextUnformatted( Worker::GetFailureString( failure ) );
ImGui::Separator();
if( data.srcloc != 0 )
{
const auto& srcloc = s_instance->m_worker.GetSourceLocation( data.srcloc );
if( srcloc.name.active )
{
TextFocused( "Zone name:", s_instance->m_worker.GetString( srcloc.name ) );
}
TextFocused( "Function:", s_instance->m_worker.GetString( srcloc.function ) );
TextDisabledUnformatted( "Location:" );
ImGui::SameLine();
ImGui::TextUnformatted( LocationToString( s_instance->m_worker.GetString( srcloc.file ), srcloc.line ) );
}
if( data.thread != 0 )
{
TextFocused( "Thread:", s_instance->m_worker.GetThreadName( data.thread ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( data.thread ) );
if( s_instance->m_worker.IsThreadFiber( data.thread ) )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 0.2f, 0.6f, 0.2f, 1.f ), "Fiber" );
}
}
if( !data.message.empty() )
{
TextFocused( "Context:", data.message.c_str() );
}
if( data.callstack != 0 )
{
if( ImGui::TreeNode( "Call stack" ) )
{
ImGui::BeginChild( "##callstackFailure", ImVec2( 1200, 500 ) );
if( ImGui::BeginTable( "##callstack", 4, ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders ) )
{
ImGui::TableSetupColumn( "Frame", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize );
ImGui::TableSetupColumn( "Function" );
ImGui::TableSetupColumn( "Location" );
ImGui::TableSetupColumn( "Image" );
ImGui::TableHeadersRow();
auto& cs = s_instance->m_worker.GetCallstack( data.callstack );
int fidx = 0;
int bidx = 0;
for( auto& entry : cs )
{
auto frameData = s_instance->m_worker.GetCallstackFrame( entry );
if( !frameData )
{
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Text( "%i", fidx++ );
ImGui::TableNextColumn();
char buf[32];
sprintf( buf, "%p", (void*)s_instance->m_worker.GetCanonicalPointer( entry ) );
ImGui::TextUnformatted( buf );
if( ImGui::IsItemHovered() )
{
ImGui::BeginTooltip();
ImGui::TextUnformatted( "Click on entry to copy it to clipboard." );
ImGui::EndTooltip();
if( ImGui::IsItemClicked() )
{
ImGui::SetClipboardText( buf );
}
}
}
else
{
const auto fsz = frameData->size;
for( uint8_t f=0; f<fsz; f++ )
{
const auto& frame = frameData->data[f];
auto txt = s_instance->m_worker.GetString( frame.name );
if( fidx == 0 && f != fsz-1 )
{
auto test = s_tracyStackFrames;
bool match = false;
do
{
if( strcmp( txt, *test ) == 0 )
{
match = true;
break;
}
}
while( *++test );
if( match ) continue;
}
bidx++;
ImGui::TableNextRow();
ImGui::TableNextColumn();
if( f == fsz-1 )
{
ImGui::Text( "%i", fidx++ );
}
else
{
TextDisabledUnformatted( "inline" );
}
ImGui::TableNextColumn();
{
ImGui::PushTextWrapPos( 0.0f );
if( txt[0] == '[' )
{
TextDisabledUnformatted( txt );
}
else
{
ImGui::TextUnformatted( txt );
}
ImGui::PopTextWrapPos();
}
if( ImGui::IsItemHovered() )
{
ImGui::BeginTooltip();
ImGui::TextUnformatted( "Click on entry to copy it to clipboard." );
ImGui::EndTooltip();
if( ImGui::IsItemClicked() )
{
ImGui::SetClipboardText( txt );
}
}
ImGui::TableNextColumn();
ImGui::PushTextWrapPos( 0.0f );
txt = s_instance->m_worker.GetString( frame.file );
TextDisabledUnformatted( LocationToString( txt, frame.line ) );
if( ImGui::IsItemHovered() )
{
ImGui::BeginTooltip();
ImGui::TextUnformatted( "Click on entry to copy it to clipboard." );
ImGui::EndTooltip();
if( ImGui::IsItemClicked() )
{
ImGui::SetClipboardText( txt );
}
}
ImGui::PopTextWrapPos();
ImGui::TableNextColumn();
if( frameData->imageName.Active() )
{
TextDisabledUnformatted( s_instance->m_worker.GetString( frameData->imageName ) );
}
}
}
}
ImGui::EndTable();
}
ImGui::EndChild();
ImGui::TreePop();
}
}
ImGui::Separator();
if( ImGui::Button( "I understand" ) )
{
ImGui::CloseCurrentPopup();
s_instance->m_worker.ClearFailure();
}
ImGui::EndPopup();
}
bool saveFailed = false;
if( !s_instance->m_filenameStaging.empty() )
{
ImGui::OpenPopup( "Save trace" );
}
if( ImGui::BeginPopupModal( "Save trace", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
assert( !s_instance->m_filenameStaging.empty() );
auto fn = s_instance->m_filenameStaging.c_str();
ImGui::PushFont( s_instance->m_bigFont );
TextFocused( "Path:", fn );
ImGui::PopFont();
ImGui::Separator();
static FileWrite::Compression comp = FileWrite::Compression::Fast;
static int zlvl = 6;
ImGui::TextUnformatted( ICON_FA_FILE_ARCHIVE " Trace compression" );
ImGui::SameLine();
TextDisabledUnformatted( "Can be changed later with the upgrade utility" );
ImGui::Indent();
int idx = 0;
while( CompressionName[idx] )
{
if( ImGui::RadioButton( CompressionName[idx], (int)comp == idx ) ) comp = (FileWrite::Compression)idx;
ImGui::SameLine();
TextDisabledUnformatted( CompressionDesc[idx] );
idx++;
}
ImGui::Unindent();
ImGui::TextUnformatted( "Zstd level" );
ImGui::SameLine();
TextDisabledUnformatted( "Increasing level decreases file size, but increases save and load times" );
ImGui::Indent();
if( ImGui::SliderInt( "##zstd", &zlvl, 1, 22, "%d", ImGuiSliderFlags_AlwaysClamp ) )
{
comp = FileWrite::Compression::Zstd;
}
ImGui::Unindent();
static bool buildDict = false;
if( s_instance->m_worker.GetFrameImageCount() != 0 )
{
ImGui::Separator();
ImGui::Checkbox( "Build frame images dictionary", &buildDict );
ImGui::SameLine();
TextDisabledUnformatted( "Decreases run-time memory requirements" );
}
ImGui::Separator();
if( ImGui::Button( ICON_FA_SAVE " Save trace" ) )
{
saveFailed = !s_instance->Save( fn, comp, zlvl, buildDict );
s_instance->m_filenameStaging.clear();
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if( ImGui::Button( "Cancel" ) )
{
s_instance->m_filenameStaging.clear();
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
if( saveFailed ) ImGui::OpenPopup( "Save failed" );
if( ImGui::BeginPopupModal( "Save failed", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
ImGui::PushFont( s_instance->m_bigFont );
TextCentered( ICON_FA_EXCLAMATION_TRIANGLE );
ImGui::PopFont();
ImGui::TextUnformatted( "Could not save trace at the specified location. Try again somewhere else." );
ImGui::Separator();
if( ImGui::Button( "Oh well" ) ) ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
s_time += ImGui::GetIO().DeltaTime;
return s_instance->DrawImpl();
}
static const char* MainWindowButtons[] = {
ICON_FA_PLAY " Resume",
ICON_FA_PAUSE " Pause",
ICON_FA_SQUARE " Stopped"
};
enum { MainWindowButtonsCount = sizeof( MainWindowButtons ) / sizeof( *MainWindowButtons ) };
bool View::DrawImpl()
{
if( !m_worker.HasData() )
{
bool keepOpen = true;
char tmp[2048];
sprintf( tmp, "%s###Connection", m_worker.GetAddr().c_str() );
ImGui::Begin( tmp, &keepOpen, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse );
ImGui::PushFont( m_bigFont );
TextCentered( ICON_FA_WIFI );
ImGui::PopFont();
ImGui::TextUnformatted( "Waiting for connection..." );
DrawWaitingDots( s_time );
ImGui::End();
return keepOpen;
}
if( !m_uarchSet )
{
m_uarchSet = true;
m_sourceView->SetCpuId( m_worker.GetCpuId() );
}
if( !m_userData.Valid() ) m_userData.Init( m_worker.GetCaptureProgram().c_str(), m_worker.GetCaptureTime() );
if( m_saveThreadState.load( std::memory_order_acquire ) == SaveThreadState::NeedsJoin )
{
m_saveThread.join();
m_saveThreadState.store( SaveThreadState::Inert, std::memory_order_release );
const auto src = m_srcFileBytes.load( std::memory_order_relaxed );
const auto dst = m_dstFileBytes.load( std::memory_order_relaxed );
m_notificationTime = 4;
char buf[1024];
sprintf( buf, "Trace size %s (%.2f%% ratio)", MemSizeToString( dst ), 100.f * dst / src );
m_notificationText = buf;
}
const auto& io = ImGui::GetIO();
assert( m_shortcut == ShortcutAction::None );
if( io.KeyCtrl )
{
if( ImGui::IsKeyPressed( ImGuiKey_F ) )
{
m_findZone.show = true;
m_shortcut = ShortcutAction::OpenFind;
}
}
if( !m_frames ) m_frames = m_worker.GetFramesBase();
const auto th = ImGui::GetTextLineHeight();
float bw = 0;
for( int i=0; i<MainWindowButtonsCount; i++ )
{
bw = std::max( bw, ImGui::CalcTextSize( MainWindowButtons[i] ).x );
}
bw += th;
bool keepOpen = true;
bool* keepOpenPtr = nullptr;
(void)keepOpenPtr;
if( m_staticView )
{
keepOpenPtr = &keepOpen;
}
#ifndef TRACY_NO_ROOT_WINDOW
if( !m_titleSet && m_stcb )
{
m_titleSet = true;
m_stcb( m_worker.GetCaptureName().c_str() );
}
ImGuiViewport* viewport = ImGui::GetMainViewport();
{
auto& style = ImGui::GetStyle();
const auto wrPrev = style.WindowRounding;
const auto wbsPrev = style.WindowBorderSize;
const auto wpPrev = style.WindowPadding;
style.WindowRounding = 0.f;
style.WindowBorderSize = 0.f;
style.WindowPadding = ImVec2( 4.f, 4.f );
style.Colors[ImGuiCol_WindowBg] = ImVec4( 0.129f, 0.137f, 0.11f, 1.f );
ImGui::SetNextWindowPos( viewport->Pos );
ImGui::SetNextWindowSize( ImVec2( m_rootWidth, m_rootHeight ) );
ImGui::SetNextWindowViewport( viewport->ID );
ImGui::Begin( "Timeline view###Profiler", nullptr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoNavFocus );
style.WindowRounding = wrPrev;
style.WindowBorderSize = wbsPrev;
style.WindowPadding = wpPrev;
style.Colors[ImGuiCol_WindowBg] = ImVec4( 0.11f, 0.11f, 0.08f, 1.f );
}
#else
char tmp[2048];
sprintf( tmp, "%s###Profiler", m_worker.GetCaptureName().c_str() );
ImGui::SetNextWindowSize( ImVec2( 1550, 800 ), ImGuiCond_FirstUseEver );
ImGui::Begin( tmp, keepOpenPtr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoBringToFrontOnFocus );
#endif
if( !m_staticView )
{
if( ImGui::Button( ICON_FA_WIFI ) || m_forceConnectionPopup )
{
if( m_forceConnectionPopup )
{
m_forceConnectionPopup.Decay( false );
ImGui::SetNextWindowPos( viewport->Pos + ImGui::GetCursorPos() );
}
ImGui::OpenPopup( "TracyConnectionPopup" );
}
ImGui::SameLine();
if( ImGui::BeginPopup( "TracyConnectionPopup" ) )
{
const bool wasDisconnectIssued = m_disconnectIssued;
const bool discardData = !DrawConnection();
const bool disconnectIssuedJustNow = m_disconnectIssued != wasDisconnectIssued;
if( discardData ) keepOpen = false;
if( disconnectIssuedJustNow || discardData ) ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
}
std::lock_guard<std::mutex> lock( m_worker.GetDataLock() );
m_worker.DoPostponedWork();
if( !m_worker.IsDataStatic() )
{
if( m_worker.IsConnected() )
{
if( ImGui::Button( m_viewMode == ViewMode::Paused ? MainWindowButtons[0] : MainWindowButtons[1], ImVec2( bw, 0 ) ) )
{
if( m_viewMode != ViewMode::Paused )
{
m_viewMode = ViewMode::Paused;
m_viewModeHeuristicTry = false;
}
else
{
ImGui::OpenPopup( "viewMode" );
}
}
}
else
{
ImGui::BeginDisabled();
ImGui::ButtonEx( MainWindowButtons[2], ImVec2( bw, 0 ) );
ImGui::EndDisabled();
}
if( ImGui::BeginPopup( "viewMode" ) )
{
if( ImGui::Selectable( ICON_FA_SEARCH_PLUS " Newest three frames" ) )
{
m_viewMode = ViewMode::LastFrames;
}
if( ImGui::Selectable( ICON_FA_RULER_HORIZONTAL " Use current zoom level" ) )
{
m_viewMode = ViewMode::LastRange;
}
ImGui::EndPopup();
}
else if( m_viewModeHeuristicTry )
{
const auto lastTime = m_worker.GetLastTime();
if( lastTime > 5*1000*1000*1000ll )
{
if( m_viewMode == ViewMode::LastFrames && m_worker.GetFrameCount( *m_worker.GetFramesBase() ) <= 2 )
{
m_viewMode = ViewMode::LastRange;
ZoomToRange( lastTime - 5*1000*1000*1000ll, lastTime, false );
}
else
{
m_viewModeHeuristicTry = false;
}
}
}
}
else
{
ImGui::PushStyleColor( ImGuiCol_Button, (ImVec4)ImColor::HSV( 0.f, 0.6f, 0.6f) );
ImGui::PushStyleColor( ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV( 0.f, 0.7f, 0.7f) );
ImGui::PushStyleColor( ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV( 0.f, 0.8f, 0.8f) );
if( ImGui::Button( ICON_FA_POWER_OFF ) ) keepOpen = false;
ImGui::PopStyleColor( 3 );
}
ImGui::SameLine();
ToggleButton( ICON_FA_COG " Options", m_showOptions );
ImGui::SameLine();
ToggleButton( ICON_FA_TAGS " Messages", m_showMessages );
ImGui::SameLine();
ToggleButton( ICON_FA_SEARCH " Find zone", m_findZone.show );
ImGui::SameLine();
ToggleButton( ICON_FA_SORT_AMOUNT_UP " Statistics", m_showStatistics );
ImGui::SameLine();
ToggleButton( ICON_FA_MEMORY " Memory", m_memInfo.show );
ImGui::SameLine();
ToggleButton( ICON_FA_BALANCE_SCALE " Compare", m_compare.show );
ImGui::SameLine();
ToggleButton( ICON_FA_FINGERPRINT " Info", m_showInfo );
ImGui::SameLine();
if( ImGui::Button( ICON_FA_TOOLS ) ) ImGui::OpenPopup( "ToolsPopup" );
if( ImGui::BeginPopup( "ToolsPopup" ) )
{
const auto ficnt = m_worker.GetFrameImageCount();
if( ButtonDisablable( ICON_FA_PLAY " Playback", ficnt == 0 ) )
{
m_showPlayback = true;
}
const auto& ctd = m_worker.GetCpuThreadData();
if( ButtonDisablable( ICON_FA_SLIDERS_H " CPU data", ctd.empty() ) )
{
m_showCpuDataWindow = true;
}
ToggleButton( ICON_FA_STICKY_NOTE " Annotations", m_showAnnotationList );
ToggleButton( ICON_FA_RULER " Limits", m_showRanges );
const auto cscnt = m_worker.GetContextSwitchSampleCount();
if( ButtonDisablable( ICON_FA_HOURGLASS_HALF " Wait stacks", cscnt == 0 ) )
{
m_showWaitStacks = true;
}
ImGui::EndPopup();
}
if( m_sscb )
{
ImGui::SameLine();
if( ImGui::Button( ICON_FA_SEARCH_PLUS ) ) ImGui::OpenPopup( "ZoomPopup" );
if( ImGui::BeginPopup( "ZoomPopup" ) )
{
if( ImGui::Button( "50%" ) ) m_sscb( 1.f/2, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "57%" ) ) m_sscb( 1.f/1.75f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "66%" ) ) m_sscb( 1.f/1.5f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "80%" ) ) m_sscb( 1.f/1.25f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "100%" ) ) m_sscb( 1.f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "125%" ) ) m_sscb( 1.25f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "150%" ) ) m_sscb( 1.5f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "175%" ) ) m_sscb( 1.75f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "200%" ) ) m_sscb( 2.f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "225%" ) ) m_sscb( 2.25f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "250%" ) ) m_sscb( 2.5f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "275%" ) ) m_sscb( 2.75f, m_fixedFont, m_bigFont, m_smallFont );
if( ImGui::Button( "300%" ) ) m_sscb( 3.f, m_fixedFont, m_bigFont, m_smallFont );
ImGui::EndPopup();
}
}
ImGui::SameLine();
if( ImGui::SmallButton( " " ICON_FA_CARET_LEFT " " ) ) ZoomToPrevFrame();
ImGui::SameLine();
{
const auto vis = Vis( m_frames ).visible;
if( !vis )
{
ImGui::PushStyleColor( ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled] );
}
ImGui::Text( "%s: %s", m_frames->name == 0 ? "Frames" : m_worker.GetString( m_frames->name ), RealToString( m_worker.GetFrameCount( *m_frames ) ) );
if( !vis )
{
ImGui::PopStyleColor();
}
if( ImGui::IsItemClicked() ) ImGui::OpenPopup( "GoToFramePopup" );
}
ImGui::SameLine();
if( ImGui::SmallButton( " " ICON_FA_CARET_RIGHT " " ) ) ZoomToNextFrame();
ImGui::SameLine();
if( ImGui::BeginCombo( "##frameCombo", nullptr, ImGuiComboFlags_NoPreview ) )
{
auto& frames = m_worker.GetFrames();
for( auto& fd : frames )
{
bool isSelected = m_frames == fd;
if( ImGui::Selectable( fd->name == 0 ? "Frames" : m_worker.GetString( fd->name ), isSelected ) )
{
m_frames = fd;
}
if( isSelected )
{
ImGui::SetItemDefaultFocus();
}
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( fd->frames.size() ) );
}
ImGui::EndCombo();
}
if( ImGui::BeginPopup( "GoToFramePopup" ) )
{
static int frameNum = 1;
const bool mainFrameSet = m_frames->name == 0;
const auto numFrames = mainFrameSet ? m_frames->frames.size() - 1 : m_frames->frames.size();
const auto frameOffset = mainFrameSet ? 0 : 1;
ImGui::SetNextItemWidth( 120 * GetScale() );
const bool clicked = ImGui::InputInt( "##goToFrame", &frameNum, 1, 100, ImGuiInputTextFlags_EnterReturnsTrue );
frameNum = std::min( std::max( frameNum, 1 ), int( numFrames ) );
if( clicked ) ZoomToRange( m_worker.GetFrameBegin( *m_frames, frameNum - frameOffset ), m_worker.GetFrameEnd( *m_frames, frameNum - frameOffset ) );
ImGui::EndPopup();
}
{
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
const auto targetLabelSize = ImGui::CalcTextSize( "WWWWWWW" ).x;
auto cx = ImGui::GetCursorPosX();
ImGui::Text( ICON_FA_EYE " %s", TimeToString( m_vd.zvEnd - m_vd.zvStart ) );
TooltipIfHovered( "View span" );
ImGui::SameLine();
auto dx = ImGui::GetCursorPosX() - cx;
if( dx < targetLabelSize ) ImGui::SameLine( cx + targetLabelSize );
cx = ImGui::GetCursorPosX();
ImGui::Text( ICON_FA_DATABASE " %s", TimeToString( m_worker.GetLastTime() ) );
if( ImGui::IsItemHovered() )
{
ImGui::BeginTooltip();
ImGui::Text( "Time span" );
ImGui::EndTooltip();
if( ImGui::IsItemClicked( 2 ) )
{
ZoomToRange( 0, m_worker.GetLastTime() );
}
}
ImGui::SameLine();
dx = ImGui::GetCursorPosX() - cx;
if( dx < targetLabelSize ) ImGui::SameLine( cx + targetLabelSize );
cx = ImGui::GetCursorPosX();
ImGui::Text( ICON_FA_MEMORY " %s", MemSizeToString( memUsage ) );
TooltipIfHovered( "Profiler memory usage" );
if( m_totalMemory != 0 )
{
ImGui::SameLine();
const auto memUse = float( memUsage ) / m_totalMemory * 100;
if( memUse < 80 )
{
ImGui::TextDisabled( "(%.2f%%)", memUse );
}
else
{
ImGui::TextColored( ImVec4( 1.f, 0.25f, 0.25f, 1.f ), "(%.2f%%)", memUse );
}
}
ImGui::SameLine();
dx = ImGui::GetCursorPosX() - cx;
if( dx < targetLabelSize ) ImGui::SameLine( cx + targetLabelSize );
ImGui::Spacing();
}
DrawNotificationArea();
m_frameHover = -1;
DrawFrames();
const auto dockspaceId = ImGui::GetID( "tracyDockspace" );
ImGui::DockSpace( dockspaceId, ImVec2( 0, 0 ), ImGuiDockNodeFlags_NoDockingInCentralNode );
if( ImGuiDockNode* node = ImGui::DockBuilderGetCentralNode( dockspaceId ) )
{
node->LocalFlags |= ImGuiDockNodeFlags_NoTabBar;
}
ImGui::SetNextWindowDockID( dockspaceId );
{
auto& style = ImGui::GetStyle();
const auto wpPrev = style.WindowPadding;
style.WindowPadding = ImVec2( 1, 0 );
#ifndef TRACY_NO_ROOT_WINDOW
style.Colors[ImGuiCol_WindowBg] = ImVec4( 0.129f, 0.137f, 0.11f, 1.f );
#endif
ImGui::Begin( "Work area", nullptr, ImGuiWindowFlags_NoNavFocus );
style.WindowPadding = wpPrev;
#ifndef TRACY_NO_ROOT_WINDOW
style.Colors[ImGuiCol_WindowBg] = ImVec4( 0.11f, 0.11f, 0.08f, 1.f );
#endif
}
DrawZones();
ImGui::End();
ImGui::End();
m_zoneHighlight = nullptr;
m_gpuHighlight = nullptr;
DrawInfoWindow();
if( m_showOptions ) DrawOptions();
if( m_showMessages ) DrawMessages();
if( m_findZone.show ) DrawFindZone();
if( m_showStatistics ) DrawStatistics();
if( m_memInfo.show ) DrawMemory();
if( m_memInfo.showAllocList ) DrawAllocList();
if( m_compare.show ) DrawCompare();
if( m_callstackInfoWindow != 0 ) DrawCallstackWindow();
if( m_memoryAllocInfoWindow >= 0 ) DrawMemoryAllocWindow();
if( m_showInfo ) DrawInfo();
if( m_sourceViewFile ) DrawTextEditor();
if( m_lockInfoWindow != InvalidId ) DrawLockInfoWindow();
if( m_showPlayback ) DrawPlayback();
if( m_showCpuDataWindow ) DrawCpuDataWindow();
if( m_selectedAnnotation ) DrawSelectedAnnotation();
if( m_showAnnotationList ) DrawAnnotationList();
if( m_sampleParents.symAddr != 0 ) DrawSampleParents();
if( m_showRanges ) DrawRanges();
if( m_showWaitStacks ) DrawWaitStacks();
if( m_setRangePopup.active )
{
m_setRangePopup.active = false;
ImGui::OpenPopup( "SetZoneRange" );
}
if( ImGui::BeginPopup( "SetZoneRange" ) )
{
const auto s = std::min( m_setRangePopup.min, m_setRangePopup.max );
const auto e = std::max( m_setRangePopup.min, m_setRangePopup.max );
if( ImGui::Selectable( ICON_FA_SEARCH " Limit find zone time range" ) )
{
m_findZone.range.active = true;
m_findZone.range.min = s;
m_findZone.range.max = e;
}
if( ImGui::Selectable( ICON_FA_SORT_AMOUNT_UP " Limit statistics time range" ) )
{
m_statRange.active = true;
m_statRange.min = s;
m_statRange.max = e;
}
if( ImGui::Selectable( ICON_FA_HOURGLASS_HALF " Limit wait stacks range" ) )
{
m_waitStackRange.active = true;
m_waitStackRange.min = s;
m_waitStackRange.max = e;
}
if( ImGui::Selectable( ICON_FA_MEMORY " Limit memory range" ) )
{
m_memInfo.range.active = true;
m_memInfo.range.min = s;
m_memInfo.range.max = e;
}
ImGui::Separator();
if( ImGui::Selectable( ICON_FA_STICKY_NOTE " Add annotation" ) )
{
AddAnnotation( s, e );
}
ImGui::EndPopup();
}
m_setRangePopupOpen = ImGui::IsPopupOpen( "SetZoneRange" );
if( m_zoomAnim.active )
{
if( m_viewMode == ViewMode::LastRange )
{
const auto delta = m_worker.GetLastTime() - m_vd.zvEnd;
if( delta != 0 )
{
m_zoomAnim.start0 += delta;
m_zoomAnim.start1 += delta;
m_zoomAnim.end0 += delta;
m_zoomAnim.end1 += delta;
}
}
m_zoomAnim.progress += io.DeltaTime * 3.33f;
if( m_zoomAnim.progress >= 1.f )
{
m_zoomAnim.active = false;
m_vd.zvStart = m_zoomAnim.start1;
m_vd.zvEnd = m_zoomAnim.end1;
}
else
{
const auto v = sqrt( sin( M_PI_2 * m_zoomAnim.progress ) );
m_vd.zvStart = int64_t( m_zoomAnim.start0 + ( m_zoomAnim.start1 - m_zoomAnim.start0 ) * v );
m_vd.zvEnd = int64_t( m_zoomAnim.end0 + ( m_zoomAnim.end1 - m_zoomAnim.end0 ) * v );
}
}
m_callstackBuzzAnim.Update( io.DeltaTime );
m_sampleParentBuzzAnim.Update( io.DeltaTime );
m_callstackTreeBuzzAnim.Update( io.DeltaTime );
m_zoneinfoBuzzAnim.Update( io.DeltaTime );
m_findZoneBuzzAnim.Update( io.DeltaTime );
m_optionsLockBuzzAnim.Update( io.DeltaTime );
m_lockInfoAnim.Update( io.DeltaTime );
m_statBuzzAnim.Update( io.DeltaTime );
if( m_firstFrame )
{
const auto now = std::chrono::high_resolution_clock::now();
if( m_firstFrameTime.time_since_epoch().count() == 0 )
{
m_firstFrameTime = now;
}
else
{
if( std::chrono::duration_cast<std::chrono::milliseconds>( now - m_firstFrameTime ).count() > 500 )
{
m_firstFrame = false;
}
}
}
if( m_reactToCrash )
{
auto& crash = m_worker.GetCrashEvent();
if( crash.thread != 0 )
{
m_reactToCrash = false;
ImGui::OpenPopup( "Application crashed!" );
}
}
if( ImGui::BeginPopupModal( "Application crashed!", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
auto& crash = m_worker.GetCrashEvent();
assert( crash.thread != 0 );
ImGui::TextUnformatted( ICON_FA_SKULL );
ImGui::SameLine();
TextColoredUnformatted( 0xFF4444FF, "Application has crashed" );
ImGui::SameLine();
ImGui::TextUnformatted( ICON_FA_SKULL );
ImGui::Separator();
TextFocused( "Time:", TimeToString( crash.time ) );
TextFocused( "Thread:", m_worker.GetThreadName( crash.thread ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( crash.thread ) );
if( m_worker.IsThreadFiber( crash.thread ) )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 0.2f, 0.6f, 0.2f, 1.f ), "Fiber" );
}
TextFocused( "Reason:", m_worker.GetString( crash.message ) );
if( crash.callstack != 0 )
{
bool hilite = m_callstackInfoWindow == crash.callstack;
if( hilite )
{
SetButtonHighlightColor();
}
if( ImGui::Button( ICON_FA_ALIGN_JUSTIFY " Call stack" ) )
{
m_callstackInfoWindow = crash.callstack;
}
if( hilite )
{
ImGui::PopStyleColor( 3 );
}
if( ImGui::IsItemHovered() )
{
CallstackTooltip( crash.callstack );
}
}
ImGui::Separator();
if( ImGui::Button( ICON_FA_MICROSCOPE " Focus" ) ) CenterAtTime( crash.time );
ImGui::SameLine();
if( ImGui::Button( "Dismiss" ) ) ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
if( m_reactToLostConnection && !m_worker.IsConnected() )
{
m_reactToLostConnection = false;
const auto inFlight = m_worker.GetSendInFlight();
if( inFlight > 1 || ( inFlight == 1 && !m_worker.WasDisconnectIssued() ) )
{
ImGui::OpenPopup( "Connection lost!" );
}
}
if( ImGui::BeginPopupModal( "Connection lost!", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{
ImGui::PushFont( m_bigFont );
TextCentered( ICON_FA_PLUG );
ImGui::PopFont();
ImGui::TextUnformatted(
"Connection to the profiled application was lost\n"
"before all required profiling data could be retrieved.\n"
"This will result in missing source locations,\n"
"unresolved stack frames, etc." );
ImGui::Separator();
if( ImGui::Button( "Dismiss" ) ) ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
return keepOpen;
}
void View::HandleRange( Range& range, int64_t timespan, const ImVec2& wpos, float w )
{
if( !IsMouseDown( 0 ) ) range.modMin = range.modMax = false;
if( !range.active ) return;
auto& io = ImGui::GetIO();
if( range.modMin )
{
const auto nspx = double( timespan ) / w;
range.min = m_vd.zvStart + ( io.MousePos.x - wpos.x ) * nspx;
range.hiMin = true;
ConsumeMouseEvents( 0 );
ImGui::SetMouseCursor( ImGuiMouseCursor_ResizeEW );
if( range.min > range.max )
{
std::swap( range.min, range.max );
std::swap( range.hiMin, range.hiMax );
std::swap( range.modMin, range.modMax );
}
}
else if( range.modMax )
{
const auto nspx = double( timespan ) / w;
range.max = m_vd.zvStart + ( io.MousePos.x - wpos.x ) * nspx;
range.hiMax = true;
ConsumeMouseEvents( 0 );
ImGui::SetMouseCursor( ImGuiMouseCursor_ResizeEW );
if( range.min > range.max )
{
std::swap( range.min, range.max );
std::swap( range.hiMin, range.hiMax );
std::swap( range.modMin, range.modMax );
}
}
else
{
const auto pxns = w / double( timespan );
const auto px0 = ( range.min - m_vd.zvStart ) * pxns;
if( abs( px0 - ( io.MousePos.x - wpos.x ) ) < 3 )
{
range.hiMin = true;
ImGui::SetMouseCursor( ImGuiMouseCursor_ResizeEW );
if( IsMouseClicked( 0 ) )
{
range.modMin = true;
range.min = m_vd.zvStart + ( io.MousePos.x - wpos.x ) / pxns;
ConsumeMouseEvents( 0 );
if( range.min > range.max )
{
std::swap( range.min, range.max );
std::swap( range.hiMin, range.hiMax );
std::swap( range.modMin, range.modMax );
}
}
}
else
{
const auto px1 = ( range.max - m_vd.zvStart ) * pxns;
if( abs( px1 - ( io.MousePos.x - wpos.x ) ) < 3 )
{
range.hiMax = true;
ImGui::SetMouseCursor( ImGuiMouseCursor_ResizeEW );
if( IsMouseClicked( 0 ) )
{
range.modMax = true;
range.max = m_vd.zvStart + ( io.MousePos.x - wpos.x ) / pxns;
ConsumeMouseEvents( 0 );
if( range.min > range.max )
{
std::swap( range.min, range.max );
std::swap( range.hiMin, range.hiMax );
std::swap( range.modMin, range.modMax );
}
}
}
}
}
}
void View::HandleZoneViewMouse( int64_t timespan, const ImVec2& wpos, float w, double& pxns )
{
assert( timespan > 0 );
auto& io = ImGui::GetIO();
const auto nspx = double( timespan ) / w;
if( IsMouseClicked( 0 ) )
{
m_highlight.active = true;
m_highlight.start = m_highlight.end = m_vd.zvStart + ( io.MousePos.x - wpos.x ) * nspx;
}
else if( IsMouseDragging( 0 ) )
{
m_highlight.end = m_vd.zvStart + ( io.MousePos.x - wpos.x ) * nspx;
}
else if( m_highlight.active )
{
if( ImGui::GetIO().KeyCtrl && m_highlight.start != m_highlight.end )
{
m_setRangePopup = RangeSlim { m_highlight.start, m_highlight.end, true };
}
m_highlight.active = false;
}
if( IsMouseClicked( 2 ) )
{
m_highlightZoom.active = true;
m_highlightZoom.start = m_highlightZoom.end = m_vd.zvStart + ( io.MousePos.x - wpos.x ) * nspx;
}
else if( IsMouseDragging( 2 ) )
{
m_highlightZoom.end = m_vd.zvStart + ( io.MousePos.x - wpos.x ) * nspx;
}
else if( m_highlightZoom.active )
{
if( m_highlightZoom.start != m_highlightZoom.end )
{
const auto s = std::min( m_highlightZoom.start, m_highlightZoom.end );
const auto e = std::max( m_highlightZoom.start, m_highlightZoom.end );
// ZoomToRange disables m_highlightZoom.active
if( io.KeyCtrl )
{
const auto tsOld = m_vd.zvEnd - m_vd.zvStart;
const auto tsNew = e - s;
const auto mul = double( tsOld ) / tsNew;
const auto left = s - m_vd.zvStart;
const auto right = m_vd.zvEnd - e;
auto start = m_vd.zvStart - left * mul;
auto end = m_vd.zvEnd + right * mul;
if( end - start > 1000ll * 1000 * 1000 * 60 * 60 * 24 * 10 )
{
start = -1000ll * 1000 * 1000 * 60 * 60 * 24 * 5;
end = 1000ll * 1000 * 1000 * 60 * 60 * 24 * 5;
}
ZoomToRange( start, end );
}
else
{
ZoomToRange( s, e );
}
}
else
{
m_highlightZoom.active = false;
}
}
const auto hwheel_delta = io.MouseWheelH * 100.f;
if( IsMouseDragging( 1 ) || hwheel_delta != 0 )
{
m_viewMode = ViewMode::Paused;
m_viewModeHeuristicTry = false;
m_zoomAnim.active = false;
if( !m_playback.pause && m_playback.sync ) m_playback.pause = true;
const auto delta = GetMouseDragDelta( 1 );
m_yDelta = delta.y;
const auto dpx = int64_t( (delta.x * nspx) + (hwheel_delta * nspx));
if( dpx != 0 )
{
m_vd.zvStart -= dpx;
m_vd.zvEnd -= dpx;
io.MouseClickedPos[1].x = io.MousePos.x;
if( m_vd.zvStart < -1000ll * 1000 * 1000 * 60 * 60 * 24 * 5 )
{
const auto range = m_vd.zvEnd - m_vd.zvStart;
m_vd.zvStart = -1000ll * 1000 * 1000 * 60 * 60 * 24 * 5;
m_vd.zvEnd = m_vd.zvStart + range;
}
else if( m_vd.zvEnd > 1000ll * 1000 * 1000 * 60 * 60 * 24 * 5 )
{
const auto range = m_vd.zvEnd - m_vd.zvStart;
m_vd.zvEnd = 1000ll * 1000 * 1000 * 60 * 60 * 24 * 5;
m_vd.zvStart = m_vd.zvEnd - range;
}
}
}
const auto wheel = io.MouseWheel;
if( wheel != 0 )
{
if( m_viewMode == ViewMode::LastFrames ) m_viewMode = ViewMode::LastRange;
const double mouse = io.MousePos.x - wpos.x;
const auto p = mouse / w;
int64_t t0, t1;
if( m_zoomAnim.active )
{
t0 = m_zoomAnim.start1;
t1 = m_zoomAnim.end1;
}
else
{
t0 = m_vd.zvStart;
t1 = m_vd.zvEnd;
}
const auto zoomSpan = t1 - t0;
const auto p1 = zoomSpan * p;
const auto p2 = zoomSpan - p1;
double mod = 0.25;
if( io.KeyCtrl ) mod = 0.05;
else if( io.KeyShift ) mod = 0.5;
if( wheel > 0 )
{
t0 += int64_t( p1 * mod );
t1 -= int64_t( p2 * mod );
}
else if( zoomSpan < 1000ll * 1000 * 1000 * 60 * 60 )
{
t0 -= std::max( int64_t( 1 ), int64_t( p1 * mod ) );
t1 += std::max( int64_t( 1 ), int64_t( p2 * mod ) );
}
ZoomToRange( t0, t1, !m_worker.IsConnected() || m_viewMode == ViewMode::Paused );
}
}
void View::DrawZoneFramesHeader()
{
const auto wpos = ImGui::GetCursorScreenPos();
const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
const auto w = ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ScrollbarSize;
auto draw = ImGui::GetWindowDrawList();
const auto ty = ImGui::GetTextLineHeight();
const auto ty025 = round( ty * 0.25f );
const auto ty0375 = round( ty * 0.375f );
const auto ty05 = round( ty * 0.5f );
const auto timespan = m_vd.zvEnd - m_vd.zvStart;
const auto pxns = w / double( timespan );
const auto nspx = 1.0 / pxns;
const auto scale = std::max( 0.0, round( log10( nspx ) + 2 ) );
const auto step = pow( 10, scale );
ImGui::InvisibleButton( "##zoneFrames", ImVec2( w, ty * 1.5f ) );
TooltipIfHovered( TimeToStringExact( m_vd.zvStart + ( ImGui::GetIO().MousePos.x - wpos.x ) * nspx ) );
const auto dx = step * pxns;
double x = 0;
int tw = 0;
int tx = 0;
int64_t tt = 0;
while( x < w )
{
DrawLine( draw, dpos + ImVec2( x, 0 ), dpos + ImVec2( x, ty05 ), 0x66FFFFFF );
if( tw == 0 )
{
char buf[128];
auto txt = TimeToStringExact( m_vd.zvStart );
if( m_vd.zvStart >= 0 )
{
sprintf( buf, "+%s", txt );
txt = buf;
}
draw->AddText( wpos + ImVec2( x, ty05 ), 0x66FFFFFF, txt );
tw = ImGui::CalcTextSize( txt ).x;
}
else if( x > tx + tw + ty * 2 )
{
tx = x;
auto txt = TimeToString( tt );
draw->AddText( wpos + ImVec2( x, ty05 ), 0x66FFFFFF, txt );
tw = ImGui::CalcTextSize( txt ).x;
}
if( scale != 0 )
{
for( int i=1; i<5; i++ )
{
DrawLine( draw, dpos + ImVec2( x + i * dx / 10, 0 ), dpos + ImVec2( x + i * dx / 10, ty025 ), 0x33FFFFFF );
}
DrawLine( draw, dpos + ImVec2( x + 5 * dx / 10, 0 ), dpos + ImVec2( x + 5 * dx / 10, ty0375 ), 0x33FFFFFF );
for( int i=6; i<10; i++ )
{
DrawLine( draw, dpos + ImVec2( x + i * dx / 10, 0 ), dpos + ImVec2( x + i * dx / 10, ty025 ), 0x33FFFFFF );
}
}
x += dx;
tt += step;
}
}
static uint32_t MixGhostColor( uint32_t c0, uint32_t c1 )
{
return 0xFF000000 |
( ( ( ( ( c0 & 0x00FF0000 ) >> 16 ) + 3 * ( ( c1 & 0x00FF0000 ) >> 16 ) ) >> 2 ) << 16 ) |
( ( ( ( ( c0 & 0x0000FF00 ) >> 8 ) + 3 * ( ( c1 & 0x0000FF00 ) >> 8 ) ) >> 2 ) << 8 ) |
( ( ( ( ( c0 & 0x000000FF ) ) + 3 * ( ( c1 & 0x000000FF ) ) ) >> 2 ) );
}
static uint32_t GetColorMuted( uint32_t color, bool active )
{
if( active )
{
return 0xFF000000 | color;
}
else
{
return 0x66000000 | color;
}
}
void View::DrawZoneFrames( const FrameData& frames )
{
const auto wpos = ImGui::GetCursorScreenPos();
const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
const auto w = ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ScrollbarSize;
const auto wh = ImGui::GetContentRegionAvail().y;
auto draw = ImGui::GetWindowDrawList();
const auto ty = ImGui::GetTextLineHeight();
const auto ty025 = ty * 0.25f;
const auto ty05 = round( ty * 0.5f );
ImGui::InvisibleButton( "##zoneFrames", ImVec2( w, ty ) );
bool hover = ImGui::IsItemHovered();
auto timespan = m_vd.zvEnd - m_vd.zvStart;
auto pxns = w / double( timespan );
const auto nspx = 1.0 / pxns;
const std::pair <int, int> zrange = m_worker.GetFrameRange( frames, m_vd.zvStart, m_vd.zvEnd );
if( zrange.first < 0 ) return;
int64_t prev = -1;
int64_t prevEnd = -1;
int64_t endPos = -1;
bool tooltipDisplayed = false;
const auto activeFrameSet = m_frames == &frames;
const int64_t frameTarget = ( activeFrameSet && m_vd.drawFrameTargets ) ? 1000000000ll / m_vd.frameTarget : std::numeric_limits<int64_t>::max();
const auto inactiveColor = GetColorMuted( 0x888888, activeFrameSet );
const auto activeColor = GetColorMuted( 0xFFFFFF, activeFrameSet );
const auto redColor = GetColorMuted( 0x4444FF, activeFrameSet );
int i = zrange.first;
auto x1 = ( m_worker.GetFrameBegin( frames, i ) - m_vd.zvStart ) * pxns;
while( i < zrange.second )
{
const auto ftime = m_worker.GetFrameTime( frames, i );
const auto fbegin = m_worker.GetFrameBegin( frames, i );
const auto fend = m_worker.GetFrameEnd( frames, i );
const auto fsz = pxns * ftime;
if( hover )
{
const auto x0 = frames.continuous ? x1 : ( fbegin - m_vd.zvStart ) * pxns;
x1 = ( fend - m_vd.zvStart ) * pxns;
if( ImGui::IsMouseHoveringRect( wpos + ImVec2( x0, 0 ), wpos + ImVec2( x1, ty ) ) )
{
tooltipDisplayed = true;
if( IsMouseClickReleased( 1 ) ) m_setRangePopup = RangeSlim { fbegin, fend, true };
ImGui::BeginTooltip();
ImGui::TextUnformatted( GetFrameText( frames, i, ftime, m_worker.GetFrameOffset() ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%.1f FPS)", 1000000000.0 / ftime );
TextFocused( "Time from start of program:", TimeToStringExact( m_worker.GetFrameBegin( frames, i ) ) );
auto fi = m_worker.GetFrameImage( frames, i );
if( fi )
{
const auto scale = GetScale();
if( fi != m_frameTexturePtr )
{
if( !m_frameTexture ) m_frameTexture = MakeTexture();
UpdateTexture( m_frameTexture, m_worker.UnpackFrameImage( *fi ), fi->w, fi->h );
m_frameTexturePtr = fi;
}
ImGui::Separator();
if( fi->flip )
{
ImGui::Image( m_frameTexture, ImVec2( fi->w * scale, fi->h * scale ), ImVec2( 0, 1 ), ImVec2( 1, 0 ) );
}
else
{
ImGui::Image( m_frameTexture, ImVec2( fi->w * scale, fi->h * scale ) );
}
if( ImGui::GetIO().KeyCtrl && IsMouseClicked( 0 ) )
{
m_showPlayback = true;
m_playback.pause = true;
SetPlaybackFrame( frames.frames[i].frameImage );
}
}
ImGui::EndTooltip();
if( IsMouseClicked( 2 ) )
{
ZoomToRange( fbegin, fend );
}
if( activeFrameSet ) m_frameHover = i;
}
}
if( fsz < MinFrameSize )
{
if( !frames.continuous && prev != -1 )
{
if( ( fbegin - prevEnd ) * pxns >= MinFrameSize )
{
DrawZigZag( draw, wpos + ImVec2( 0, ty05 ), ( prev - m_vd.zvStart ) * pxns, ( prevEnd - m_vd.zvStart ) * pxns, ty025, inactiveColor );
prev = -1;
}
else
{
prevEnd = std::max<int64_t>( fend, fbegin + MinFrameSize * nspx );
}
}
if( prev == -1 )
{
prev = fbegin;
prevEnd = std::max<int64_t>( fend, fbegin + MinFrameSize * nspx );
}
const auto begin = frames.frames.begin() + i;
const auto end = frames.frames.begin() + zrange.second;
auto it = std::lower_bound( begin, end, int64_t( fbegin + MinVisSize * nspx ), [this, &frames] ( const auto& l, const auto& r ) { return m_worker.GetFrameEnd( frames, std::distance( frames.frames.begin(), &l ) ) < r; } );
if( it == begin ) ++it;
i += std::distance( begin, it );
continue;
}
if( prev != -1 )
{
if( frames.continuous )
{
DrawZigZag( draw, wpos + ImVec2( 0, ty05 ), ( prev - m_vd.zvStart ) * pxns, ( fbegin - m_vd.zvStart ) * pxns, ty025, inactiveColor );
}
else
{
DrawZigZag( draw, wpos + ImVec2( 0, ty05 ), ( prev - m_vd.zvStart ) * pxns, ( prevEnd - m_vd.zvStart ) * pxns, ty025, inactiveColor );
}
prev = -1;
}
if( activeFrameSet )
{
if( fend - fbegin > frameTarget )
{
draw->AddRectFilled( wpos + ImVec2( ( fbegin + frameTarget - m_vd.zvStart ) * pxns, 0 ), wpos + ImVec2( ( fend - m_vd.zvStart ) * pxns, wh ), 0x224444FF );
}
if( fbegin >= m_vd.zvStart && endPos != fbegin )
{
DrawLine( draw, dpos + ImVec2( ( fbegin - m_vd.zvStart ) * pxns, 0 ), dpos + ImVec2( ( fbegin - m_vd.zvStart ) * pxns, wh ), 0x22FFFFFF );
}
if( fend <= m_vd.zvEnd )
{
DrawLine( draw, dpos + ImVec2( ( fend - m_vd.zvStart ) * pxns, 0 ), dpos + ImVec2( ( fend - m_vd.zvStart ) * pxns, wh ), 0x22FFFFFF );
}
endPos = fend;
}
auto buf = GetFrameText( frames, i, ftime, m_worker.GetFrameOffset() );
auto tx = ImGui::CalcTextSize( buf ).x;
uint32_t color = ( frames.name == 0 && i == 0 ) ? redColor : activeColor;
if( fsz - 7 <= tx )
{
static char tmp[256];
sprintf( tmp, "%s (%s)", RealToString( i ), TimeToString( ftime ) );
buf = tmp;
tx = ImGui::CalcTextSize( buf ).x;
}
if( fsz - 7 <= tx )
{
buf = TimeToString( ftime );
tx = ImGui::CalcTextSize( buf ).x;
}
if( fbegin >= m_vd.zvStart )
{
DrawLine( draw, dpos + ImVec2( ( fbegin - m_vd.zvStart ) * pxns + 2, 1 ), dpos + ImVec2( ( fbegin - m_vd.zvStart ) * pxns + 2, ty - 1 ), color );
}
if( fend <= m_vd.zvEnd )
{
DrawLine( draw, dpos + ImVec2( ( fend - m_vd.zvStart ) * pxns - 2, 1 ), dpos + ImVec2( ( fend - m_vd.zvStart ) * pxns - 2, ty - 1 ), color );
}
if( fsz - 7 > tx )
{
const auto f0 = ( fbegin - m_vd.zvStart ) * pxns + 2;
const auto f1 = ( fend - m_vd.zvStart ) * pxns - 2;
const auto x0 = f0 + 1;
const auto x1 = f1 - 1;
const auto te = x1 - tx;
auto tpos = ( x0 + te ) / 2;
if( tpos < 0 )
{
tpos = std::min( std::min( 0., te - tpos ), te );
}
else if( tpos > w - tx )
{
tpos = std::max( double( w - tx ), x0 );
}
tpos = round( tpos );
DrawLine( draw, dpos + ImVec2( std::max( -10.0, f0 ), ty05 ), dpos + ImVec2( tpos, ty05 ), color );
DrawLine( draw, dpos + ImVec2( std::max( -10.0, tpos + tx + 1 ), ty05 ), dpos + ImVec2( std::min( w + 20.0, f1 ), ty05 ), color );
draw->AddText( wpos + ImVec2( tpos, 0 ), color, buf );
}
else
{
DrawLine( draw, dpos + ImVec2( std::max( -10.0, ( fbegin - m_vd.zvStart ) * pxns + 2 ), ty05 ), dpos + ImVec2( std::min( w + 20.0, ( fend - m_vd.zvStart ) * pxns - 2 ), ty05 ), color );
}
i++;
}
if( prev != -1 )
{
if( frames.continuous )
{
DrawZigZag( draw, wpos + ImVec2( 0, ty05 ), ( prev - m_vd.zvStart ) * pxns, ( m_worker.GetFrameBegin( frames, zrange.second-1 ) - m_vd.zvStart ) * pxns, ty025, inactiveColor );
}
else
{
const auto begin = ( prev - m_vd.zvStart ) * pxns;
const auto end = ( m_worker.GetFrameBegin( frames, zrange.second-1 ) - m_vd.zvStart ) * pxns;
DrawZigZag( draw, wpos + ImVec2( 0, ty05 ), begin, std::max( begin + MinFrameSize, end ), ty025, inactiveColor );
}
}
if( hover )
{
if( !tooltipDisplayed )
{
ImGui::BeginTooltip();
TextDisabledUnformatted( "Frame set:" );
ImGui::SameLine();
ImGui::TextUnformatted( frames.name == 0 ? "Frames" : m_worker.GetString( frames.name ) );
ImGui::EndTooltip();
}
if( IsMouseClicked( 0 ) )
{
m_frames = &frames;
}
}
}
float View::AdjustThreadPosition( View::VisData& vis, float wy, int& offset )
{
if( vis.offset < offset )
{
vis.offset = offset;
}
else if( vis.offset > offset )
{
const auto diff = vis.offset - offset;
const auto move = std::max( 2.0, diff * 10.0 * ImGui::GetIO().DeltaTime );
offset = vis.offset = int( std::max<double>( vis.offset - move, offset ) );
}
return offset + wy;
}
void View::AdjustThreadHeight( View::VisData& vis, int oldOffset, int& offset )
{
const auto h = offset - oldOffset;
if( vis.height > h )
{
vis.height = h;
offset = oldOffset + vis.height;
}
else if( vis.height < h )
{
if( m_firstFrame )
{
vis.height = h;
offset = oldOffset + h;
}
else
{
const auto diff = h - vis.height;
const auto move = std::max( 2.0, diff * 10.0 * ImGui::GetIO().DeltaTime );
vis.height = int( std::min<double>( vis.height + move, h ) );
offset = oldOffset + vis.height;
}
}
}
void View::DrawZones()
{
m_msgHighlight.Decay( nullptr );
m_zoneSrcLocHighlight.Decay( 0 );
m_lockHoverHighlight.Decay( InvalidId );
m_drawThreadMigrations.Decay( 0 );
m_drawThreadHighlight.Decay( 0 );
m_cpuDataThread.Decay( 0 );
m_zoneHover = nullptr;
m_zoneHover2.Decay( nullptr );
m_findZone.range.StartFrame();
m_statRange.StartFrame();
m_waitStackRange.StartFrame();
m_memInfo.range.StartFrame();
m_yDelta = 0;
if( m_vd.zvStart == m_vd.zvEnd ) return;
assert( m_vd.zvStart < m_vd.zvEnd );
if( ImGui::GetCurrentWindowRead()->SkipItems ) return;
m_gpuThread = 0;
m_gpuStart = 0;
m_gpuEnd = 0;
const auto linepos = ImGui::GetCursorScreenPos();
const auto lineh = ImGui::GetContentRegionAvail().y;
auto draw = ImGui::GetWindowDrawList();
const auto w = ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ScrollbarSize;
const auto timespan = m_vd.zvEnd - m_vd.zvStart;
auto pxns = w / double( timespan );
const auto winpos = ImGui::GetWindowPos();
const auto winsize = ImGui::GetWindowSize();
const bool drawMouseLine = ImGui::IsWindowHovered( ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem ) && ImGui::IsMouseHoveringRect( winpos, winpos + winsize, false );
if( drawMouseLine )
{
HandleRange( m_findZone.range, timespan, ImGui::GetCursorScreenPos(), w );
HandleRange( m_statRange, timespan, ImGui::GetCursorScreenPos(), w );
HandleRange( m_waitStackRange, timespan, ImGui::GetCursorScreenPos(), w );
HandleRange( m_memInfo.range, timespan, ImGui::GetCursorScreenPos(), w );
for( auto& v : m_annotations )
{
v->range.StartFrame();
HandleRange( v->range, timespan, ImGui::GetCursorScreenPos(), w );
}
HandleZoneViewMouse( timespan, ImGui::GetCursorScreenPos(), w, pxns );
}
{
const auto tbegin = 0;
const auto tend = m_worker.GetLastTime();
if( tbegin > m_vd.zvStart )
{
draw->AddRectFilled( linepos, linepos + ImVec2( ( tbegin - m_vd.zvStart ) * pxns, lineh ), 0x44000000 );
}
if( tend < m_vd.zvEnd )
{
draw->AddRectFilled( linepos + ImVec2( ( tend - m_vd.zvStart ) * pxns, 0 ), linepos + ImVec2( w, lineh ), 0x44000000 );
}
}
DrawZoneFramesHeader();
auto& frames = m_worker.GetFrames();
for( auto fd : frames )
{
if( Vis( fd ).visible )
{
DrawZoneFrames( *fd );
}
}
const auto yMin = ImGui::GetCursorScreenPos().y;
const auto yMax = linepos.y + lineh;
ImGui::BeginChild( "##zoneWin", ImVec2( ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y ), false, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_NoScrollWithMouse );
if( m_yDelta != 0 )
{
auto& io = ImGui::GetIO();
auto y = ImGui::GetScrollY();
ImGui::SetScrollY( y - m_yDelta );
io.MouseClickedPos[1].y = io.MousePos.y;
}
const auto wpos = ImGui::GetCursorScreenPos();
const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
const auto h = std::max<float>( m_vd.zvHeight, ImGui::GetContentRegionAvail().y - 4 ); // magic border value
ImGui::InvisibleButton( "##zones", ImVec2( w, h ) );
bool hover = ImGui::IsItemHovered();
draw = ImGui::GetWindowDrawList();
const auto nspx = 1.0 / pxns;
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
int offset = 0;
const auto to = 9.f;
const auto th = ( ty - to ) * sqrt( 3 ) * 0.5;
// gpu zones
if( m_vd.drawGpuZones )
{
for( size_t i=0; i<m_worker.GetGpuData().size(); i++ )
{
const auto& v = m_worker.GetGpuData()[i];
auto& vis = Vis( v );
if( !vis.visible )
{
vis.height = 0;
vis.offset = 0;
continue;
}
bool& showFull = vis.showFull;
const auto yPos = AdjustThreadPosition( vis, wpos.y, offset );
const auto oldOffset = offset;
ImGui::PushClipRect( wpos, wpos + ImVec2( w, oldOffset + vis.height ), true );
ImGui::PushFont( m_smallFont );
const auto sty = ImGui::GetTextLineHeight();
const auto sstep = sty + 1;
ImGui::PopFont();
const auto singleThread = v->threadData.size() == 1;
int depth = 0;
offset += ostep;
if( showFull && !v->threadData.empty() )
{
for( auto& td : v->threadData )
{
auto& tl = td.second.timeline;
assert( !tl.empty() );
if( tl.is_magic() )
{
auto& tlm = *(Vector<GpuEvent>*)&tl;
if( tlm.front().GpuStart() >= 0 )
{
const auto begin = tlm.front().GpuStart();
const auto drift = GpuDrift( v );
if( !singleThread ) offset += sstep;
const auto partDepth = DispatchGpuZoneLevel( tl, hover, pxns, int64_t( nspx ), wpos, offset, 0, v->thread, yMin, yMax, begin, drift );
if( partDepth != 0 )
{
if( !singleThread )
{
ImGui::PushFont( m_smallFont );
DrawTextContrast( draw, wpos + ImVec2( ty, offset-1-sstep ), 0xFFFFAAAA, m_worker.GetThreadName( td.first ) );
DrawLine( draw, dpos + ImVec2( 0, offset+sty-sstep ), dpos + ImVec2( w, offset+sty-sstep ), 0x22FFAAAA );
ImGui::PopFont();
}
offset += ostep * partDepth;
depth += partDepth;
}
else if( !singleThread )
{
offset -= sstep;
}
}
}
else
{
if( tl.front()->GpuStart() >= 0 )
{
const auto begin = tl.front()->GpuStart();
const auto drift = GpuDrift( v );
if( !singleThread ) offset += sstep;
const auto partDepth = DispatchGpuZoneLevel( tl, hover, pxns, int64_t( nspx ), wpos, offset, 0, v->thread, yMin, yMax, begin, drift );
if( partDepth != 0 )
{
if( !singleThread )
{
ImGui::PushFont( m_smallFont );
DrawTextContrast( draw, wpos + ImVec2( ty, offset-1-sstep ), 0xFFFFAAAA, m_worker.GetThreadName( td.first ) );
DrawLine( draw, dpos + ImVec2( 0, offset+sty-sstep ), dpos + ImVec2( w, offset+sty-sstep ), 0x22FFAAAA );
ImGui::PopFont();
}
offset += ostep * partDepth;
depth += partDepth;
}
else if( !singleThread )
{
offset -= sstep;
}
}
}
}
}
offset += ostep * 0.2f;
if( !m_vd.drawEmptyLabels && showFull && depth == 0 )
{
vis.height = 0;
vis.offset = 0;
offset = oldOffset;
}
else if( yPos + ostep >= yMin && yPos <= yMax )
{
DrawLine( draw, dpos + ImVec2( 0, oldOffset + ostep - 1 ), dpos + ImVec2( w, oldOffset + ostep - 1 ), 0x33FFFFFF );
if( showFull )
{
draw->AddTriangleFilled( wpos + ImVec2( to/2, oldOffset + to/2 ), wpos + ImVec2( ty - to/2, oldOffset + to/2 ), wpos + ImVec2( ty * 0.5, oldOffset + to/2 + th ), 0xFFFFAAAA );
}
else
{
draw->AddTriangle( wpos + ImVec2( to/2, oldOffset + to/2 ), wpos + ImVec2( to/2, oldOffset + ty - to/2 ), wpos + ImVec2( to/2 + th, oldOffset + ty * 0.5 ), 0xFF886666, 2.0f );
}
const bool isMultithreaded = (v->type == GpuContextType::Vulkan) || (v->type == GpuContextType::OpenCL) || (v->type == GpuContextType::Direct3D12);
float boxwidth;
char buf[64];
sprintf( buf, "%s context %zu", GpuContextNames[(int)v->type], i );
if( v->name.Active() )
{
char tmp[4096];
sprintf( tmp, "%s: %s", buf, m_worker.GetString( v->name ) );
DrawTextContrast( draw, wpos + ImVec2( ty, oldOffset ), showFull ? 0xFFFFAAAA : 0xFF886666, tmp );
boxwidth = ImGui::CalcTextSize( tmp ).x;
}
else
{
DrawTextContrast( draw, wpos + ImVec2( ty, oldOffset ), showFull ? 0xFFFFAAAA : 0xFF886666, buf );
boxwidth = ImGui::CalcTextSize( buf ).x;
}
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( ty + boxwidth, oldOffset + ty ) ) )
{
if( IsMouseClicked( 0 ) )
{
showFull = !showFull;
}
if( IsMouseClicked( 2 ) )
{
int64_t t0 = std::numeric_limits<int64_t>::max();
int64_t t1 = std::numeric_limits<int64_t>::min();
for( auto& td : v->threadData )
{
int64_t _t0;
if( td.second.timeline.is_magic() )
{
_t0 = ((Vector<GpuEvent>*)&td.second.timeline)->front().GpuStart();
}
else
{
_t0 = td.second.timeline.front()->GpuStart();
}
if( _t0 >= 0 )
{
// FIXME
t0 = std::min( t0, _t0 );
if( td.second.timeline.is_magic() )
{
t1 = std::max( t1, std::min( m_worker.GetLastTime(), m_worker.GetZoneEnd( ((Vector<GpuEvent>*)&td.second.timeline)->back() ) ) );
}
else
{
t1 = std::max( t1, std::min( m_worker.GetLastTime(), m_worker.GetZoneEnd( *td.second.timeline.back() ) ) );
}
}
}
if( t0 < t1 )
{
ZoomToRange( t0, t1 );
}
}
ImGui::BeginTooltip();
ImGui::TextUnformatted( buf );
if( v->name.Active() ) TextFocused( "Name:", m_worker.GetString( v->name ) );
ImGui::Separator();
if( !isMultithreaded )
{
SmallColorBox( GetThreadColor( v->thread, 0 ) );
ImGui::SameLine();
TextFocused( "Thread:", m_worker.GetThreadName( v->thread ) );
}
else
{
if( !v->threadData.empty() )
{
if( v->threadData.size() == 1 )
{
auto it = v->threadData.begin();
auto tid = it->first;
if( tid == 0 )
{
if( !it->second.timeline.empty() )
{
if( it->second.timeline.is_magic() )
{
auto& tl = *(Vector<GpuEvent>*)&it->second.timeline;
tid = m_worker.DecompressThread( tl.begin()->Thread() );
}
else
{
tid = m_worker.DecompressThread( (*it->second.timeline.begin())->Thread() );
}
}
}
SmallColorBox( GetThreadColor( tid, 0 ) );
ImGui::SameLine();
TextFocused( "Thread:", m_worker.GetThreadName( tid ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( tid ) );
if( m_worker.IsThreadFiber( tid ) )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 0.2f, 0.6f, 0.2f, 1.f ), "Fiber" );
}
}
else
{
ImGui::TextDisabled( "Threads:" );
ImGui::Indent();
for( auto& td : v->threadData )
{
SmallColorBox( GetThreadColor( td.first, 0 ) );
ImGui::SameLine();
ImGui::TextUnformatted( m_worker.GetThreadName( td.first ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( td.first ) );
}
ImGui::Unindent();
}
}
}
if( !v->threadData.empty() )
{
int64_t t0 = std::numeric_limits<int64_t>::max();
for( auto& td : v->threadData )
{
int64_t _t0;
if( td.second.timeline.is_magic() )
{
_t0 = ((Vector<GpuEvent>*)&td.second.timeline)->front().GpuStart();
}
else
{
_t0 = td.second.timeline.front()->GpuStart();
}
if( _t0 >= 0 )
{
t0 = std::min( t0, _t0 );
}
}
if( t0 != std::numeric_limits<int64_t>::max() )
{
TextFocused( "Appeared at", TimeToString( t0 ) );
}
}
TextFocused( "Zone count:", RealToString( v->count ) );
if( v->period != 1.f )
{
TextFocused( "Timestamp accuracy:", TimeToString( v->period ) );
}
if( v->overflow != 0 )
{
ImGui::Separator();
ImGui::TextUnformatted( "GPU timer overflow has been detected." );
TextFocused( "Timer resolution:", RealToString( 63 - TracyLzcnt( v->overflow ) ) );
ImGui::SameLine();
TextDisabledUnformatted( "bits" );
}
ImGui::EndTooltip();
}
}
AdjustThreadHeight( vis, oldOffset, offset );
ImGui::PopClipRect();
}
}
// zones
if( m_vd.drawCpuData && m_worker.HasContextSwitches() )
{
offset = DrawCpuData( offset, pxns, wpos, hover, yMin, yMax );
}
const auto& threadData = m_worker.GetThreadData();
if( threadData.size() != m_threadOrder.size() )
{
m_threadOrder.reserve( threadData.size() );
for( size_t i=m_threadOrder.size(); i<threadData.size(); i++ )
{
m_threadOrder.push_back( threadData[i] );
}
}
auto& crash = m_worker.GetCrashEvent();
LockHighlight nextLockHighlight { -1 };
for( const auto& v : m_threadOrder )
{
auto& vis = Vis( v );
if( !vis.visible )
{
vis.height = 0;
vis.offset = 0;
continue;
}
bool showFull = vis.showFull;
const auto yPos = AdjustThreadPosition( vis, wpos.y, offset );
const auto oldOffset = offset;
ImGui::PushClipRect( wpos, wpos + ImVec2( w, offset + vis.height ), true );
int depth = 0;
offset += ostep;
if( showFull )
{
const auto sampleOffset = offset;
const auto hasSamples = m_vd.drawSamples && !v->samples.empty();
const auto hasCtxSwitch = m_vd.drawContextSwitches && m_worker.GetContextSwitchData( v->id );
if( hasSamples )
{
if( hasCtxSwitch )
{
offset += round( ostep * 0.5f );
}
else
{
offset += round( ostep * 0.75f );
}
}
const auto ctxOffset = offset;
if( hasCtxSwitch ) offset += round( ostep * 0.75f );
if( m_vd.drawZones )
{
#ifndef TRACY_NO_STATISTICS
if( m_worker.AreGhostZonesReady() && ( vis.ghost || ( m_vd.ghostZones && v->timeline.empty() ) ) )
{
depth = DispatchGhostLevel( v->ghostZones, hover, pxns, int64_t( nspx ), wpos, offset, 0, yMin, yMax, v->id );
}
else
#endif
{
depth = DispatchZoneLevel( v->timeline, hover, pxns, int64_t( nspx ), wpos, offset, 0, yMin, yMax, v->id );
}
offset += ostep * depth;
}
if( hasCtxSwitch )
{
auto ctxSwitch = m_worker.GetContextSwitchData( v->id );
if( ctxSwitch )
{
DrawContextSwitches( ctxSwitch, v->samples, hover, pxns, int64_t( nspx ), wpos, ctxOffset, offset, v->isFiber );
}
}
if( hasSamples )
{
DrawSamples( v->samples, hover, pxns, int64_t( nspx ), wpos, sampleOffset );
}
if( m_vd.drawLocks )
{
const auto lockDepth = DrawLocks( v->id, hover, pxns, wpos, offset, nextLockHighlight, yMin, yMax );
offset += ostep * lockDepth;
depth += lockDepth;
}
}
offset += ostep * 0.2f;
auto msgit = std::lower_bound( v->messages.begin(), v->messages.end(), m_vd.zvStart, [] ( const auto& lhs, const auto& rhs ) { return lhs->time < rhs; } );
auto msgend = std::lower_bound( msgit, v->messages.end(), m_vd.zvEnd+1, [] ( const auto& lhs, const auto& rhs ) { return lhs->time < rhs; } );
if( !m_vd.drawEmptyLabels && showFull && depth == 0 && msgit == msgend && crash.thread != v->id )
{
auto& vis = Vis( v );
vis.height = 0;
vis.offset = 0;
offset = oldOffset;
}
else if( yPos + ostep >= yMin && yPos <= yMax )
{
DrawLine( draw, dpos + ImVec2( 0, oldOffset + ostep - 1 ), dpos + ImVec2( w, oldOffset + ostep - 1 ), 0x33FFFFFF );
uint32_t labelColor;
if( crash.thread == v->id ) labelColor = showFull ? 0xFF2222FF : 0xFF111188;
else if( v->isFiber ) labelColor = showFull ? 0xFF88FF88 : 0xFF448844;
else labelColor = showFull ? 0xFFFFFFFF : 0xFF888888;
if( showFull )
{
draw->AddTriangleFilled( wpos + ImVec2( to/2, oldOffset + to/2 ), wpos + ImVec2( ty - to/2, oldOffset + to/2 ), wpos + ImVec2( ty * 0.5, oldOffset + to/2 + th ), labelColor );
while( msgit < msgend )
{
const auto next = std::upper_bound( msgit, v->messages.end(), (*msgit)->time + MinVisSize * nspx, [] ( const auto& lhs, const auto& rhs ) { return lhs < rhs->time; } );
const auto dist = std::distance( msgit, next );
const auto px = ( (*msgit)->time - m_vd.zvStart ) * pxns;
const bool isMsgHovered = hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px - (ty - to) * 0.5 - 1, oldOffset ), wpos + ImVec2( px + (ty - to) * 0.5 + 1, oldOffset + ty ) );
unsigned int color = 0xFFDDDDDD;
float animOff = 0;
if( dist > 1 )
{
if( m_msgHighlight && m_worker.DecompressThread( m_msgHighlight->thread ) == v->id )
{
const auto hTime = m_msgHighlight->time;
if( (*msgit)->time <= hTime && ( next == v->messages.end() || (*next)->time > hTime ) )
{
color = 0xFF4444FF;
if( !isMsgHovered )
{
animOff = -fabs( sin( s_time * 8 ) ) * th;
}
}
}
draw->AddTriangleFilled( wpos + ImVec2( px - (ty - to) * 0.5, animOff + oldOffset + to ), wpos + ImVec2( px + (ty - to) * 0.5, animOff + oldOffset + to ), wpos + ImVec2( px, animOff + oldOffset + to + th ), color );
draw->AddTriangle( wpos + ImVec2( px - (ty - to) * 0.5, animOff + oldOffset + to ), wpos + ImVec2( px + (ty - to) * 0.5, animOff + oldOffset + to ), wpos + ImVec2( px, animOff + oldOffset + to + th ), color, 2.0f );
}
else
{
if( m_msgHighlight == *msgit )
{
color = 0xFF4444FF;
if( !isMsgHovered )
{
animOff = -fabs( sin( s_time * 8 ) ) * th;
}
}
draw->AddTriangle( wpos + ImVec2( px - (ty - to) * 0.5, animOff + oldOffset + to ), wpos + ImVec2( px + (ty - to) * 0.5, animOff + oldOffset + to ), wpos + ImVec2( px, animOff + oldOffset + to + th ), color, 2.0f );
}
if( isMsgHovered )
{
ImGui::BeginTooltip();
if( dist > 1 )
{
ImGui::Text( "%i messages", (int)dist );
}
else
{
TextFocused( "Message at", TimeToStringExact( (*msgit)->time ) );
ImGui::PushStyleColor( ImGuiCol_Text, (*msgit)->color );
ImGui::TextUnformatted( m_worker.GetString( (*msgit)->ref ) );
ImGui::PopStyleColor();
}
ImGui::EndTooltip();
m_msgHighlight = *msgit;
if( IsMouseClicked( 0 ) )
{
m_showMessages = true;
m_msgToFocus = *msgit;
}
if( IsMouseClicked( 2 ) )
{
CenterAtTime( (*msgit)->time );
}
}
msgit = next;
}
if( crash.thread == v->id && crash.time >= m_vd.zvStart && crash.time <= m_vd.zvEnd )
{
const auto px = ( crash.time - m_vd.zvStart ) * pxns;
draw->AddTriangleFilled( wpos + ImVec2( px - (ty - to) * 0.25f, oldOffset + to + th * 0.5f ), wpos + ImVec2( px + (ty - to) * 0.25f, oldOffset + to + th * 0.5f ), wpos + ImVec2( px, oldOffset + to + th ), 0xFF2222FF );
draw->AddTriangle( wpos + ImVec2( px - (ty - to) * 0.25f, oldOffset + to + th * 0.5f ), wpos + ImVec2( px + (ty - to) * 0.25f, oldOffset + to + th * 0.5f ), wpos + ImVec2( px, oldOffset + to + th ), 0xFF2222FF, 2.0f );
const auto crashText = ICON_FA_SKULL " crash " ICON_FA_SKULL;
auto ctw = ImGui::CalcTextSize( crashText ).x;
DrawTextContrast( draw, wpos + ImVec2( px - ctw * 0.5f, oldOffset + to + th * 0.5f - ty ), 0xFF2222FF, crashText );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px - (ty - to) * 0.5 - 1, oldOffset ), wpos + ImVec2( px + (ty - to) * 0.5 + 1, oldOffset + ty ) ) )
{
CrashTooltip();
if( IsMouseClicked( 0 ) )
{
m_showInfo = true;
}
if( IsMouseClicked( 2 ) )
{
CenterAtTime( crash.time );
}
}
}
}
else
{
draw->AddTriangle( wpos + ImVec2( to/2, oldOffset + to/2 ), wpos + ImVec2( to/2, oldOffset + ty - to/2 ), wpos + ImVec2( to/2 + th, oldOffset + ty * 0.5 ), labelColor, 2.0f );
}
const auto txt = m_worker.GetThreadName( v->id );
const auto txtsz = ImGui::CalcTextSize( txt );
if( m_gpuThread == v->id )
{
draw->AddRectFilled( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ), 0x228888DD );
draw->AddRect( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ), 0x448888DD );
}
if( m_gpuInfoWindow && m_gpuInfoWindowThread == v->id )
{
draw->AddRectFilled( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ), 0x2288DD88 );
draw->AddRect( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ), 0x4488DD88 );
}
if( m_cpuDataThread == v->id )
{
draw->AddRectFilled( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ), 0x2DFF8888 );
draw->AddRect( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( w, offset ), 0x4DFF8888 );
}
DrawTextContrast( draw, wpos + ImVec2( ty, oldOffset ), labelColor, txt );
#ifndef TRACY_NO_STATISTICS
const bool hasGhostZones = showFull && m_worker.AreGhostZonesReady() && !v->ghostZones.empty();
float ghostSz;
if( hasGhostZones && !v->timeline.empty() )
{
auto& vis = Vis( v );
const auto color = vis.ghost ? 0xFFAA9999 : 0x88AA7777;
draw->AddText( wpos + ImVec2( 1.5f * ty + txtsz.x, oldOffset ), color, ICON_FA_GHOST );
ghostSz = ImGui::CalcTextSize( ICON_FA_GHOST ).x;
}
#endif
if( hover )
{
#ifndef TRACY_NO_STATISTICS
if( hasGhostZones && !v->timeline.empty() && ImGui::IsMouseHoveringRect( wpos + ImVec2( 1.5f * ty + txtsz.x, oldOffset ), wpos + ImVec2( 1.5f * ty + txtsz.x + ghostSz, oldOffset + ty ) ) )
{
if( IsMouseClicked( 0 ) )
{
auto& vis = Vis( v );
vis.ghost = !vis.ghost;
}
}
else
#endif
if( ImGui::IsMouseHoveringRect( wpos + ImVec2( 0, oldOffset ), wpos + ImVec2( ty + txtsz.x, oldOffset + ty ) ) )
{
m_drawThreadMigrations = v->id;
m_drawThreadHighlight = v->id;
ImGui::BeginTooltip();
SmallColorBox( GetThreadColor( v->id, 0 ) );
ImGui::SameLine();
ImGui::TextUnformatted( m_worker.GetThreadName( v->id ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( v->id ) );
if( crash.thread == v->id )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 1.f, 0.2f, 0.2f, 1.f ), ICON_FA_SKULL " Crashed" );
}
if( v->isFiber )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 0.2f, 0.6f, 0.2f, 1.f ), "Fiber" );
}
const auto ctx = m_worker.GetContextSwitchData( v->id );
ImGui::Separator();
int64_t first = std::numeric_limits<int64_t>::max();
int64_t last = -1;
if( ctx && !ctx->v.empty() )
{
const auto& back = ctx->v.back();
first = ctx->v.begin()->Start();
last = back.IsEndValid() ? back.End() : back.Start();
}
if( !v->timeline.empty() )
{
if( v->timeline.is_magic() )
{
auto& tl = *((Vector<ZoneEvent>*)&v->timeline);
first = std::min( first, tl.front().Start() );
last = std::max( last, m_worker.GetZoneEnd( tl.back() ) );
}
else
{
first = std::min( first, v->timeline.front()->Start() );
last = std::max( last, m_worker.GetZoneEnd( *v->timeline.back() ) );
}
}
if( !v->messages.empty() )
{
first = std::min( first, v->messages.front()->time );
last = std::max( last, v->messages.back()->time );
}
size_t lockCnt = 0;
for( const auto& lock : m_worker.GetLockMap() )
{
const auto& lockmap = *lock.second;
if( !lockmap.valid ) continue;
auto it = lockmap.threadMap.find( v->id );
if( it == lockmap.threadMap.end() ) continue;
lockCnt++;
const auto thread = it->second;
auto lptr = lockmap.timeline.data();
auto eptr = lptr + lockmap.timeline.size() - 1;
while( lptr->ptr->thread != thread ) lptr++;
if( lptr->ptr->Time() < first ) first = lptr->ptr->Time();
while( eptr->ptr->thread != thread ) eptr--;
if( eptr->ptr->Time() > last ) last = eptr->ptr->Time();
}
if( last >= 0 )
{
const auto lifetime = last - first;
const auto traceLen = m_worker.GetLastTime();
TextFocused( "Appeared at", TimeToString( first ) );
TextFocused( "Last event at", TimeToString( last ) );
TextFocused( "Lifetime:", TimeToString( lifetime ) );
ImGui::SameLine();
char buf[64];
PrintStringPercent( buf, lifetime / double( traceLen ) * 100 );
TextDisabledUnformatted( buf );
if( ctx )
{
TextFocused( "Time in running state:", TimeToString( ctx->runningTime ) );
ImGui::SameLine();
PrintStringPercent( buf, ctx->runningTime / double( lifetime ) * 100 );
TextDisabledUnformatted( buf );
}
}
ImGui::Separator();
if( !v->timeline.empty() )
{
TextFocused( "Zone count:", RealToString( v->count ) );
TextFocused( "Top-level zones:", RealToString( v->timeline.size() ) );
}
if( !v->messages.empty() )
{
TextFocused( "Messages:", RealToString( v->messages.size() ) );
}
if( lockCnt != 0 )
{
TextFocused( "Locks:", RealToString( lockCnt ) );
}
if( ctx )
{
TextFocused( "Running state regions:", RealToString( ctx->v.size() ) );
}
if( !v->samples.empty() )
{
TextFocused( "Call stack samples:", RealToString( v->samples.size() ) );
if( v->kernelSampleCnt != 0 )
{
TextFocused( "Kernel samples:", RealToString( v->kernelSampleCnt ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%.2f%%)", 100.f * v->kernelSampleCnt / v->samples.size() );
}
}
ImGui::EndTooltip();
if( IsMouseClicked( 0 ) )
{
Vis( v ).showFull = !showFull;
}
if( last >= 0 && IsMouseClicked( 2 ) )
{
ZoomToRange( first, last );
}
}
}
}
AdjustThreadHeight( Vis( v ), oldOffset, offset );
ImGui::PopClipRect();
}
m_lockHighlight = nextLockHighlight;
if( m_vd.drawPlots )
{
offset = DrawPlots( offset, pxns, wpos, hover, yMin, yMax );
}
const auto scrollPos = ImGui::GetScrollY();
if( scrollPos == 0 && m_vd.zvScroll != 0 )
{
m_vd.zvHeight = 0;
}
else
{
if( offset > m_vd.zvHeight ) m_vd.zvHeight = offset;
}
m_vd.zvScroll = scrollPos;
ImGui::EndChild();
for( auto& ann : m_annotations )
{
if( ann->range.min < m_vd.zvEnd && ann->range.max > m_vd.zvStart )
{
uint32_t c0 = ( ann->color & 0xFFFFFF ) | ( m_selectedAnnotation == ann.get() ? 0x44000000 : 0x22000000 );
uint32_t c1 = ( ann->color & 0xFFFFFF ) | ( m_selectedAnnotation == ann.get() ? 0x66000000 : 0x44000000 );
uint32_t c2 = ( ann->color & 0xFFFFFF ) | ( m_selectedAnnotation == ann.get() ? 0xCC000000 : 0xAA000000 );
draw->AddRectFilled( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns, 0 ), linepos + ImVec2( ( ann->range.max - m_vd.zvStart ) * pxns, lineh ), c0 );
DrawLine( draw, linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + 0.5f, 0.5f ), linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + 0.5f, lineh + 0.5f ), ann->range.hiMin ? c2 : c1, ann->range.hiMin ? 2 : 1 );
DrawLine( draw, linepos + ImVec2( ( ann->range.max - m_vd.zvStart ) * pxns + 0.5f, 0.5f ), linepos + ImVec2( ( ann->range.max - m_vd.zvStart ) * pxns + 0.5f, lineh + 0.5f ), ann->range.hiMax ? c2 : c1, ann->range.hiMax ? 2 : 1 );
if( drawMouseLine && ImGui::IsMouseHoveringRect( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns, 0 ), linepos + ImVec2( ( ann->range.max - m_vd.zvStart ) * pxns, lineh ) ) )
{
ImGui::BeginTooltip();
if( ann->text.empty() )
{
TextDisabledUnformatted( "Empty annotation" );
}
else
{
ImGui::TextUnformatted( ann->text.c_str() );
}
ImGui::Separator();
TextFocused( "Annotation begin:", TimeToStringExact( ann->range.min ) );
TextFocused( "Annotation end:", TimeToStringExact( ann->range.max ) );
TextFocused( "Annotation length:", TimeToString( ann->range.max - ann->range.min ) );
ImGui::EndTooltip();
}
const auto aw = ( ann->range.max - ann->range.min ) * pxns;
if( aw > th * 4 )
{
draw->AddCircleFilled( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + th * 2, th * 2 ), th, 0x88AABB22 );
draw->AddCircle( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + th * 2, th * 2 ), th, 0xAAAABB22 );
if( drawMouseLine && IsMouseClicked( 0 ) && ImGui::IsMouseHoveringRect( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + th, th ), linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + th * 3, th * 3 ) ) )
{
m_selectedAnnotation = ann.get();
}
if( !ann->text.empty() )
{
const auto tw = ImGui::CalcTextSize( ann->text.c_str() ).x;
if( aw - th*4 > tw )
{
draw->AddText( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + th * 4, th * 0.5 ), 0xFFFFFFFF, ann->text.c_str() );
}
else
{
draw->PushClipRect( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns, 0 ), linepos + ImVec2( ( ann->range.max - m_vd.zvStart ) * pxns, lineh ), true );
draw->AddText( linepos + ImVec2( ( ann->range.min - m_vd.zvStart ) * pxns + th * 4, th * 0.5 ), 0xFFFFFFFF, ann->text.c_str() );
draw->PopClipRect();
}
}
}
}
}
if( m_gpuStart != 0 && m_gpuEnd != 0 )
{
const auto px0 = ( m_gpuStart - m_vd.zvStart ) * pxns;
const auto px1 = std::max( px0 + std::max( 1.0, pxns * 0.5 ), ( m_gpuEnd - m_vd.zvStart ) * pxns );
draw->AddRectFilled( ImVec2( wpos.x + px0, linepos.y ), ImVec2( wpos.x + px1, linepos.y + lineh ), 0x228888DD );
draw->AddRect( ImVec2( wpos.x + px0, linepos.y ), ImVec2( wpos.x + px1, linepos.y + lineh ), 0x448888DD );
}
if( m_gpuInfoWindow )
{
const auto px0 = ( m_gpuInfoWindow->CpuStart() - m_vd.zvStart ) * pxns;
const auto px1 = std::max( px0 + std::max( 1.0, pxns * 0.5 ), ( m_gpuInfoWindow->CpuEnd() - m_vd.zvStart ) * pxns );
draw->AddRectFilled( ImVec2( wpos.x + px0, linepos.y ), ImVec2( wpos.x + px1, linepos.y + lineh ), 0x2288DD88 );
draw->AddRect( ImVec2( wpos.x + px0, linepos.y ), ImVec2( wpos.x + px1, linepos.y + lineh ), 0x4488DD88 );
}
const auto scale = GetScale();
if( m_findZone.range.active && ( m_findZone.show || m_showRanges ) )
{
const auto px0 = ( m_findZone.range.min - m_vd.zvStart ) * pxns;
const auto px1 = std::max( px0 + std::max( 1.0, pxns * 0.5 ), ( m_findZone.range.max - m_vd.zvStart ) * pxns );
DrawStripedRect( draw, wpos, px0, linepos.y, px1, linepos.y + lineh, 10 * scale, 0x2288DD88, true, true );
DrawLine( draw, ImVec2( dpos.x + px0, linepos.y + 0.5f ), ImVec2( dpos.x + px0, linepos.y + lineh + 0.5f ), m_findZone.range.hiMin ? 0x9988DD88 : 0x3388DD88, m_findZone.range.hiMin ? 2 : 1 );
DrawLine( draw, ImVec2( dpos.x + px1, linepos.y + 0.5f ), ImVec2( dpos.x + px1, linepos.y + lineh + 0.5f ), m_findZone.range.hiMax ? 0x9988DD88 : 0x3388DD88, m_findZone.range.hiMax ? 2 : 1 );
}
if( m_statRange.active && ( m_showStatistics || m_showRanges || ( m_sourceViewFile && m_sourceView->IsSymbolView() ) ) )
{
const auto px0 = ( m_statRange.min - m_vd.zvStart ) * pxns;
const auto px1 = std::max( px0 + std::max( 1.0, pxns * 0.5 ), ( m_statRange.max - m_vd.zvStart ) * pxns );
DrawStripedRect( draw, wpos, px0, linepos.y, px1, linepos.y + lineh, 10 * scale, 0x228888EE, true, false );
DrawLine( draw, ImVec2( dpos.x + px0, linepos.y + 0.5f ), ImVec2( dpos.x + px0, linepos.y + lineh + 0.5f ), m_statRange.hiMin ? 0x998888EE : 0x338888EE, m_statRange.hiMin ? 2 : 1 );
DrawLine( draw, ImVec2( dpos.x + px1, linepos.y + 0.5f ), ImVec2( dpos.x + px1, linepos.y + lineh + 0.5f ), m_statRange.hiMax ? 0x998888EE : 0x338888EE, m_statRange.hiMax ? 2 : 1 );
}
if( m_waitStackRange.active && ( m_showWaitStacks || m_showRanges ) )
{
const auto px0 = ( m_waitStackRange.min - m_vd.zvStart ) * pxns;
const auto px1 = std::max( px0 + std::max( 1.0, pxns * 0.5 ), ( m_waitStackRange.max - m_vd.zvStart ) * pxns );
DrawStripedRect( draw, wpos, px0, linepos.y, px1, linepos.y + lineh, 10 * scale, 0x22EEB588, true, true );
DrawLine( draw, ImVec2( dpos.x + px0, linepos.y + 0.5f ), ImVec2( dpos.x + px0, linepos.y + lineh + 0.5f ), m_waitStackRange.hiMin ? 0x99EEB588 : 0x33EEB588, m_waitStackRange.hiMin ? 2 : 1 );
DrawLine( draw, ImVec2( dpos.x + px1, linepos.y + 0.5f ), ImVec2( dpos.x + px1, linepos.y + lineh + 0.5f ), m_waitStackRange.hiMax ? 0x99EEB588 : 0x33EEB588, m_waitStackRange.hiMax ? 2 : 1 );
}
if( m_memInfo.range.active && ( m_memInfo.show || m_showRanges ) )
{
const auto px0 = ( m_memInfo.range.min - m_vd.zvStart ) * pxns;
const auto px1 = std::max( px0 + std::max( 1.0, pxns * 0.5 ), ( m_memInfo.range.max - m_vd.zvStart ) * pxns );
DrawStripedRect( draw, wpos, px0, linepos.y, px1, linepos.y + lineh, 10 * scale, 0x2288EEE3, true, false );
DrawLine( draw, ImVec2( dpos.x + px0, linepos.y + 0.5f ), ImVec2( dpos.x + px0, linepos.y + lineh + 0.5f ), m_memInfo.range.hiMin ? 0x9988EEE3 : 0x3388EEE3, m_memInfo.range.hiMin ? 2 : 1 );
DrawLine( draw, ImVec2( dpos.x + px1, linepos.y + 0.5f ), ImVec2( dpos.x + px1, linepos.y + lineh + 0.5f ), m_memInfo.range.hiMax ? 0x9988EEE3 : 0x3388EEE3, m_memInfo.range.hiMax ? 2 : 1 );
}
if( m_setRangePopup.active || m_setRangePopupOpen )
{
const auto s = std::min( m_setRangePopup.min, m_setRangePopup.max );
const auto e = std::max( m_setRangePopup.min, m_setRangePopup.max );
DrawStripedRect( draw, wpos, ( s - m_vd.zvStart ) * pxns, linepos.y, ( e - m_vd.zvStart ) * pxns, linepos.y + lineh, 5 * scale, 0x55DD8888, true, false );
draw->AddRect( ImVec2( wpos.x + ( s - m_vd.zvStart ) * pxns, linepos.y ), ImVec2( wpos.x + ( e - m_vd.zvStart ) * pxns, linepos.y + lineh ), 0x77DD8888 );
}
if( m_highlight.active && m_highlight.start != m_highlight.end )
{
const auto s = std::min( m_highlight.start, m_highlight.end );
const auto e = std::max( m_highlight.start, m_highlight.end );
draw->AddRectFilled( ImVec2( wpos.x + ( s - m_vd.zvStart ) * pxns, linepos.y ), ImVec2( wpos.x + ( e - m_vd.zvStart ) * pxns, linepos.y + lineh ), 0x22DD8888 );
draw->AddRect( ImVec2( wpos.x + ( s - m_vd.zvStart ) * pxns, linepos.y ), ImVec2( wpos.x + ( e - m_vd.zvStart ) * pxns, linepos.y + lineh ), 0x44DD8888 );
ImGui::BeginTooltip();
ImGui::TextUnformatted( TimeToString( e - s ) );
ImGui::EndTooltip();
}
else if( drawMouseLine )
{
auto& io = ImGui::GetIO();
DrawLine( draw, ImVec2( io.MousePos.x + 0.5f, linepos.y + 0.5f ), ImVec2( io.MousePos.x + 0.5f, linepos.y + lineh + 0.5f ), 0x33FFFFFF );
}
if( m_highlightZoom.active && m_highlightZoom.start != m_highlightZoom.end )
{
const auto s = std::min( m_highlightZoom.start, m_highlightZoom.end );
const auto e = std::max( m_highlightZoom.start, m_highlightZoom.end );
draw->AddRectFilled( ImVec2( wpos.x + ( s - m_vd.zvStart ) * pxns, linepos.y ), ImVec2( wpos.x + ( e - m_vd.zvStart ) * pxns, linepos.y + lineh ), 0x1688DD88 );
draw->AddRect( ImVec2( wpos.x + ( s - m_vd.zvStart ) * pxns, linepos.y ), ImVec2( wpos.x + ( e - m_vd.zvStart ) * pxns, linepos.y + lineh ), 0x2C88DD88 );
}
}
#ifndef TRACY_NO_STATISTICS
int View::DispatchGhostLevel( const Vector<GhostZone>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, float yMin, float yMax, uint64_t tid )
{
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * depth;
const auto yPos = wpos.y + offset;
// Inline frames have to be taken into account, hence the multiply by 16 (arbitrary limit for inline frames in client)
if( yPos + 16 * ostep >= yMin && yPos <= yMax )
{
return DrawGhostLevel( vec, hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
}
else
{
return SkipGhostLevel( vec, hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
}
}
int View::DrawGhostLevel( const Vector<GhostZone>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, float yMin, float yMax, uint64_t tid )
{
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart ), [] ( const auto& l, const auto& r ) { return l.end.Val() < r; } );
if( it == vec.end() ) return depth;
const auto zitend = std::lower_bound( it, vec.end(), m_vd.zvEnd, [] ( const auto& l, const auto& r ) { return l.start.Val() < r; } );
if( it == zitend ) return depth;
const auto w = ImGui::GetContentRegionAvail().x - 1;
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * depth;
auto draw = ImGui::GetWindowDrawList();
const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
depth++;
int maxdepth = depth;
while( it < zitend )
{
auto& ev = *it;
const auto end = ev.end.Val();
const auto zsz = std::max( ( end - ev.start.Val() ) * pxns, pxns * 0.5 );
if( zsz < MinVisSize )
{
const auto MinVisNs = MinVisSize * nspx;
const auto color = MixGhostColor( GetThreadColor( tid, depth ), 0x665555 );
const auto px0 = ( ev.start.Val() - m_vd.zvStart ) * pxns;
auto px1ns = ev.end.Val() - m_vd.zvStart;
auto rend = end;
auto nextTime = end + MinVisNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, zitend, nextTime, [] ( const auto& l, const auto& r ) { return l.end.Val() < r; } );
if( it == prevIt ) ++it;
if( it == zitend ) break;
const auto nend = it->end.Val();
const auto nsnext = nend - m_vd.zvStart;
if( nsnext - px1ns >= MinVisNs * 2 ) break;
px1ns = nsnext;
rend = nend;
nextTime = nend + nspx;
}
const auto px1 = px1ns * pxns;
draw->AddRectFilled( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), offset + ty ), color );
DrawZigZag( draw, wpos + ImVec2( 0, offset + ty/2 ), std::max( px0, -10.0 ), std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), ty/4, DarkenColor( color ) );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), offset + ty + 1 ) ) )
{
if( IsMouseClickReleased( 1 ) ) m_setRangePopup = RangeSlim { ev.start.Val(), rend , true };
ImGui::BeginTooltip();
ImGui::TextUnformatted( "Multiple ghost zones" );
ImGui::Separator();
TextFocused( "Execution time:", TimeToString( rend - ev.start.Val() ) );
ImGui::EndTooltip();
if( IsMouseClicked( 2 ) && rend - ev.start.Val() > 0 )
{
ZoomToRange( ev.start.Val(), rend );
}
}
}
else
{
const auto& ghostKey = m_worker.GetGhostFrame( ev.frame );
const auto frame = m_worker.GetCallstackFrame( ghostKey.frame );
uint32_t color;
if( m_vd.dynamicColors == 2 )
{
if( frame )
{
const auto& sym = frame->data[ghostKey.inlineFrame];
color = GetHsvColor( sym.name.Idx(), depth );
}
else
{
color = GetHsvColor( ghostKey.frame.data, depth );
}
}
else
{
color = MixGhostColor( GetThreadColor( tid, depth ), 0x665555 );
}
const auto pr0 = ( ev.start.Val() - m_vd.zvStart ) * pxns;
const auto pr1 = ( ev.end.Val() - m_vd.zvStart ) * pxns;
const auto px0 = std::max( pr0, -10.0 );
const auto px1 = std::max( { std::min( pr1, double( w + 10 ) ), px0 + pxns * 0.5, px0 + MinVisSize } );
if( !frame )
{
char symName[64];
sprintf( symName, "0x%" PRIx64, m_worker.GetCanonicalPointer( ghostKey.frame ) );
const auto tsz = ImGui::CalcTextSize( symName );
const auto accentColor = HighlightColor( color );
const auto darkColor = DarkenColor( color );
const auto txtColor = 0xFF888888;
draw->AddRectFilled( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), DarkenColor( color ) );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px0, offset ), dpos + ImVec2( px1-1, offset ), accentColor, 1.f );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px1-1, offset + tsz.y ), dpos + ImVec2( px1-1, offset ), darkColor, 1.f );
if( tsz.x < zsz )
{
const auto x = ( ev.start.Val() - m_vd.zvStart ) * pxns + ( ( end - ev.start.Val() ) * pxns - tsz.x ) / 2;
if( x < 0 || x > w - tsz.x )
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( std::max( std::max( 0., px0 ), std::min( double( w - tsz.x ), x ) ), offset ), txtColor, symName );
ImGui::PopClipRect();
}
else if( ev.start.Val() == ev.end.Val() )
{
DrawTextContrast( draw, wpos + ImVec2( px0 + ( px1 - px0 - tsz.x ) * 0.5, offset ), txtColor, symName );
}
else
{
DrawTextContrast( draw, wpos + ImVec2( x, offset ), txtColor, symName );
}
}
else
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( ( ev.start.Val() - m_vd.zvStart ) * pxns, offset ), txtColor, symName );
ImGui::PopClipRect();
}
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y + 1 ) ) )
{
if( IsMouseClickReleased( 1 ) ) m_setRangePopup = RangeSlim { ev.start.Val(), ev.end.Val() , true };
ImGui::BeginTooltip();
TextDisabledUnformatted( ICON_FA_GHOST " Ghost zone" );
ImGui::Separator();
TextFocused( "Unknown frame:", symName );
TextFocused( "Thread:", m_worker.GetThreadName( tid ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( tid ) );
if( m_worker.IsThreadFiber( tid ) )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 0.2f, 0.6f, 0.2f, 1.f ), "Fiber" );
}
ImGui::Separator();
TextFocused( "Execution time:", TimeToString( ev.end.Val() - ev.start.Val() ) );
ImGui::EndTooltip();
if( !m_zoomAnim.active && IsMouseClicked( 2 ) )
{
ZoomToRange( ev.start.Val(), ev.end.Val() );
}
}
}
else
{
const auto& sym = frame->data[ghostKey.inlineFrame];
const auto isInline = ghostKey.inlineFrame != frame->size-1;
const auto col = isInline ? DarkenColor( color ) : color;
auto symName = m_worker.GetString( sym.name );
uint32_t txtColor;
if( symName[0] == '[' )
{
txtColor = 0xFF999999;
}
else if( !isInline && ( m_worker.GetCanonicalPointer( ghostKey.frame ) >> 63 != 0 ) )
{
txtColor = 0xFF8888FF;
}
else
{
txtColor = 0xFFFFFFFF;
}
auto tsz = ImGui::CalcTextSize( symName );
const auto accentColor = HighlightColor( col );
const auto darkColor = DarkenColor( col );
draw->AddRectFilled( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), col );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px0, offset ), dpos + ImVec2( px1-1, offset ), accentColor, 1.f );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px1-1, offset + tsz.y ), dpos + ImVec2( px1-1, offset ), darkColor, 1.f );
auto origSymName = symName;
if( tsz.x > zsz )
{
symName = ShortenNamespace( symName );
tsz = ImGui::CalcTextSize( symName );
}
if( tsz.x < zsz )
{
const auto x = ( ev.start.Val() - m_vd.zvStart ) * pxns + ( ( end - ev.start.Val() ) * pxns - tsz.x ) / 2;
if( x < 0 || x > w - tsz.x )
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( std::max( std::max( 0., px0 ), std::min( double( w - tsz.x ), x ) ), offset ), txtColor, symName );
ImGui::PopClipRect();
}
else if( ev.start.Val() == ev.end.Val() )
{
DrawTextContrast( draw, wpos + ImVec2( px0 + ( px1 - px0 - tsz.x ) * 0.5, offset ), txtColor, symName );
}
else
{
DrawTextContrast( draw, wpos + ImVec2( x, offset ), txtColor, symName );
}
}
else
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( ( ev.start.Val() - m_vd.zvStart ) * pxns, offset ), txtColor, symName );
ImGui::PopClipRect();
}
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y + 1 ) ) )
{
if( IsMouseClickReleased( 1 ) ) m_setRangePopup = RangeSlim { ev.start.Val(), ev.end.Val(), true };
ImGui::BeginTooltip();
TextDisabledUnformatted( ICON_FA_GHOST " Ghost zone" );
if( sym.symAddr >> 63 != 0 )
{
ImGui::SameLine();
TextDisabledUnformatted( ICON_FA_HAT_WIZARD " kernel" );
}
ImGui::Separator();
ImGui::TextUnformatted( origSymName );
if( isInline )
{
ImGui::SameLine();
TextDisabledUnformatted( "[inline]" );
}
const auto symbol = m_worker.GetSymbolData( sym.symAddr );
if( symbol ) TextFocused( "Image:", m_worker.GetString( symbol->imageName ) );
TextDisabledUnformatted( "Location:" );
ImGui::SameLine();
const char* file = m_worker.GetString( sym.file );
uint32_t line = sym.line;
ImGui::TextUnformatted( LocationToString( file, line ) );
ImGui::SameLine();
ImGui::TextDisabled( "(0x%" PRIx64 ")", sym.symAddr );
TextFocused( "Thread:", m_worker.GetThreadName( tid ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", RealToString( tid ) );
if( m_worker.IsThreadFiber( tid ) )
{
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 0.2f, 0.6f, 0.2f, 1.f ), "Fiber" );
}
ImGui::Separator();
TextFocused( "Execution time:", TimeToString( ev.end.Val() - ev.start.Val() ) );
ImGui::EndTooltip();
if( IsMouseClicked( 0 ) )
{
ViewDispatch( file, line, sym.symAddr );
}
else if( !m_zoomAnim.active && IsMouseClicked( 2 ) )
{
ZoomToRange( ev.start.Val(), ev.end.Val() );
}
}
}
if( ev.child >= 0 )
{
const auto d = DispatchGhostLevel( m_worker.GetGhostChildren( ev.child ), hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
if( d > maxdepth ) maxdepth = d;
}
++it;
}
}
return maxdepth;
}
int View::SkipGhostLevel( const Vector<GhostZone>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, float yMin, float yMax, uint64_t tid )
{
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart ), [] ( const auto& l, const auto& r ) { return l.end.Val() < r; } );
if( it == vec.end() ) return depth;
const auto zitend = std::lower_bound( it, vec.end(), m_vd.zvEnd, [] ( const auto& l, const auto& r ) { return l.start.Val() < r; } );
if( it == zitend ) return depth;
depth++;
int maxdepth = depth;
while( it < zitend )
{
auto& ev = *it;
const auto end = ev.end.Val();
const auto zsz = std::max( ( end - ev.start.Val() ) * pxns, pxns * 0.5 );
if( zsz < MinVisSize )
{
const auto MinVisNs = MinVisSize * nspx;
auto px1ns = ev.end.Val() - m_vd.zvStart;
auto nextTime = end + MinVisNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, zitend, nextTime, [] ( const auto& l, const auto& r ) { return l.end.Val() < r; } );
if( it == prevIt ) ++it;
if( it == zitend ) break;
const auto nend = it->end.Val();
const auto nsnext = nend - m_vd.zvStart;
if( nsnext - px1ns >= MinVisNs * 2 ) break;
px1ns = nsnext;
nextTime = nend + nspx;
}
}
else
{
if( ev.child >= 0 )
{
const auto d = DispatchGhostLevel( m_worker.GetGhostChildren( ev.child ), hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
if( d > maxdepth ) maxdepth = d;
}
++it;
}
}
return maxdepth;
}
#endif
int View::DispatchZoneLevel( const Vector<short_ptr<ZoneEvent>>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, float yMin, float yMax, uint64_t tid )
{
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * depth;
const auto yPos = wpos.y + offset;
if( yPos + ostep >= yMin && yPos <= yMax )
{
if( vec.is_magic() )
{
return DrawZoneLevel<VectorAdapterDirect<ZoneEvent>>( *(Vector<ZoneEvent>*)( &vec ), hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
}
else
{
return DrawZoneLevel<VectorAdapterPointer<ZoneEvent>>( vec, hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
}
}
else
{
if( vec.is_magic() )
{
return SkipZoneLevel<VectorAdapterDirect<ZoneEvent>>( *(Vector<ZoneEvent>*)( &vec ), hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
}
else
{
return SkipZoneLevel<VectorAdapterPointer<ZoneEvent>>( vec, hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
}
}
}
template<typename Adapter, typename V>
int View::DrawZoneLevel( const V& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, float yMin, float yMax, uint64_t tid )
{
const auto delay = m_worker.GetDelay();
const auto resolution = m_worker.GetResolution();
// cast to uint64_t, so that unended zones (end = -1) are still drawn
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart - delay ), [] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)a(l).End() < (uint64_t)r; } );
if( it == vec.end() ) return depth;
const auto zitend = std::lower_bound( it, vec.end(), m_vd.zvEnd + resolution, [] ( const auto& l, const auto& r ) { Adapter a; return a(l).Start() < r; } );
if( it == zitend ) return depth;
Adapter a;
if( !a(*it).IsEndValid() && m_worker.GetZoneEnd( a(*it) ) < m_vd.zvStart ) return depth;
const auto w = ImGui::GetContentRegionAvail().x - 1;
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * depth;
auto draw = ImGui::GetWindowDrawList();
const auto dsz = delay * pxns;
const auto rsz = resolution * pxns;
const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
const auto ty025 = round( ty * 0.25f );
const auto ty05 = round( ty * 0.5f );
const auto ty075 = round( ty * 0.75f );
depth++;
int maxdepth = depth;
while( it < zitend )
{
auto& ev = a(*it);
const auto end = m_worker.GetZoneEnd( ev );
const auto zsz = std::max( ( end - ev.Start() ) * pxns, pxns * 0.5 );
if( zsz < MinVisSize )
{
const auto MinVisNs = MinVisSize * nspx;
const auto color = GetThreadColor( tid, depth );
int num = 0;
const auto px0 = ( ev.Start() - m_vd.zvStart ) * pxns;
auto px1ns = end - m_vd.zvStart;
auto rend = end;
auto nextTime = end + MinVisNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, zitend, nextTime, [] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)a(l).End() < (uint64_t)r; } );
if( it == prevIt ) ++it;
num += std::distance( prevIt, it );
if( it == zitend ) break;
const auto nend = m_worker.GetZoneEnd( a(*it) );
const auto nsnext = nend - m_vd.zvStart;
if( nsnext - px1ns >= MinVisNs * 2 ) break;
px1ns = nsnext;
rend = nend;
nextTime = nend + nspx;
}
const auto px1 = px1ns * pxns;
draw->AddRectFilled( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), offset + ty ), color );
DrawZigZag( draw, wpos + ImVec2( 0, offset + ty/2 ), std::max( px0, -10.0 ), std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), ty/4, DarkenColor( color ) );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), offset + ty + 1 ) ) )
{
if( IsMouseClickReleased( 1 ) ) m_setRangePopup = RangeSlim { ev.Start(), rend, true };
if( num > 1 )
{
ImGui::BeginTooltip();
TextFocused( "Zones too small to display:", RealToString( num ) );
ImGui::Separator();
TextFocused( "Execution time:", TimeToString( rend - ev.Start() ) );
ImGui::EndTooltip();
if( IsMouseClicked( 2 ) && rend - ev.Start() > 0 )
{
ZoomToRange( ev.Start(), rend );
}
}
else
{
ZoneTooltip( ev );
if( IsMouseClicked( 2 ) && rend - ev.Start() > 0 )
{
ZoomToZone( ev );
}
if( IsMouseClicked( 0 ) )
{
if( ImGui::GetIO().KeyCtrl )
{
auto& srcloc = m_worker.GetSourceLocation( ev.SrcLoc() );
m_findZone.ShowZone( ev.SrcLoc(), m_worker.GetString( srcloc.name.active ? srcloc.name : srcloc.function ) );
}
else
{
ShowZoneInfo( ev );
}
}
m_zoneSrcLocHighlight = ev.SrcLoc();
m_zoneHover = &ev;
}
}
const auto tmp = RealToString( num );
const auto tsz = ImGui::CalcTextSize( tmp );
if( tsz.x < px1 - px0 )
{
const auto x = px0 + ( px1 - px0 - tsz.x ) / 2;
DrawTextContrast( draw, wpos + ImVec2( x, offset ), 0xFF4488DD, tmp );
}
}
else
{
const auto zoneColor = GetZoneColorData( ev, tid, depth );
const char* zoneName = m_worker.GetZoneName( ev );
if( ev.HasChildren() )
{
const auto d = DispatchZoneLevel( m_worker.GetZoneChildren( ev.Child() ), hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
if( d > maxdepth ) maxdepth = d;
}
auto tsz = ImGui::CalcTextSize( zoneName );
if( tsz.x > zsz )
{
zoneName = ShortenNamespace( zoneName );
tsz = ImGui::CalcTextSize( zoneName );
}
const auto pr0 = ( ev.Start() - m_vd.zvStart ) * pxns;
const auto pr1 = ( end - m_vd.zvStart ) * pxns;
const auto px0 = std::max( pr0, -10.0 );
const auto px1 = std::max( { std::min( pr1, double( w + 10 ) ), px0 + pxns * 0.5, px0 + MinVisSize } );
draw->AddRectFilled( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), zoneColor.color );
if( zoneColor.highlight )
{
draw->AddRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), zoneColor.accentColor, 0.f, -1, zoneColor.thickness );
}
else
{
const auto darkColor = DarkenColor( zoneColor.color );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px0, offset ), dpos + ImVec2( px1-1, offset ), zoneColor.accentColor, zoneColor.thickness );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px1-1, offset + tsz.y ), dpos + ImVec2( px1-1, offset ), darkColor, zoneColor.thickness );
}
if( dsz > MinVisSize )
{
const auto diff = dsz - MinVisSize;
uint32_t color;
if( diff < 1 )
{
color = ( uint32_t( diff * 0x88 ) << 24 ) | 0x2222DD;
}
else
{
color = 0x882222DD;
}
draw->AddRectFilled( wpos + ImVec2( pr0, offset ), wpos + ImVec2( std::min( pr0+dsz, pr1 ), offset + tsz.y ), color );
draw->AddRectFilled( wpos + ImVec2( pr1, offset ), wpos + ImVec2( pr1+dsz, offset + tsz.y ), color );
}
if( rsz > MinVisSize )
{
const auto diff = rsz - MinVisSize;
uint32_t color;
if( diff < 1 )
{
color = ( uint32_t( diff * 0xAA ) << 24 ) | 0xFFFFFF;
}
else
{
color = 0xAAFFFFFF;
}
DrawLine( draw, dpos + ImVec2( pr0 + rsz, offset + ty05 ), dpos + ImVec2( pr0 - rsz, offset + ty05 ), color );
DrawLine( draw, dpos + ImVec2( pr0 + rsz, offset + ty025 ), dpos + ImVec2( pr0 + rsz, offset + ty075 ), color );
DrawLine( draw, dpos + ImVec2( pr0 - rsz, offset + ty025 ), dpos + ImVec2( pr0 - rsz, offset + ty075 ), color );
DrawLine( draw, dpos + ImVec2( pr1 + rsz, offset + ty05 ), dpos + ImVec2( pr1 - rsz, offset + ty05 ), color );
DrawLine( draw, dpos + ImVec2( pr1 + rsz, offset + ty025 ), dpos + ImVec2( pr1 + rsz, offset + ty075 ), color );
DrawLine( draw, dpos + ImVec2( pr1 - rsz, offset + ty025 ), dpos + ImVec2( pr1 - rsz, offset + ty075 ), color );
}
if( tsz.x < zsz )
{
const auto x = ( ev.Start() - m_vd.zvStart ) * pxns + ( ( end - ev.Start() ) * pxns - tsz.x ) / 2;
if( x < 0 || x > w - tsz.x )
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( std::max( std::max( 0., px0 ), std::min( double( w - tsz.x ), x ) ), offset ), 0xFFFFFFFF, zoneName );
ImGui::PopClipRect();
}
else if( ev.Start() == ev.End() )
{
DrawTextContrast( draw, wpos + ImVec2( px0 + ( px1 - px0 - tsz.x ) * 0.5, offset ), 0xFFFFFFFF, zoneName );
}
else
{
DrawTextContrast( draw, wpos + ImVec2( x, offset ), 0xFFFFFFFF, zoneName );
}
}
else
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( ( ev.Start() - m_vd.zvStart ) * pxns, offset ), 0xFFFFFFFF, zoneName );
ImGui::PopClipRect();
}
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y + 1 ) ) )
{
ZoneTooltip( ev );
if( IsMouseClickReleased( 1 ) ) m_setRangePopup = RangeSlim { ev.Start(), m_worker.GetZoneEnd( ev ), true };
if( !m_zoomAnim.active && IsMouseClicked( 2 ) )
{
ZoomToZone( ev );
}
if( IsMouseClicked( 0 ) )
{
if( ImGui::GetIO().KeyCtrl )
{
auto& srcloc = m_worker.GetSourceLocation( ev.SrcLoc() );
m_findZone.ShowZone( ev.SrcLoc(), m_worker.GetString( srcloc.name.active ? srcloc.name : srcloc.function ) );
}
else
{
ShowZoneInfo( ev );
}
}
m_zoneSrcLocHighlight = ev.SrcLoc();
m_zoneHover = &ev;
}
++it;
}
}
return maxdepth;
}
template<typename Adapter, typename V>
int View::SkipZoneLevel( const V& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, float yMin, float yMax, uint64_t tid )
{
const auto delay = m_worker.GetDelay();
const auto resolution = m_worker.GetResolution();
// cast to uint64_t, so that unended zones (end = -1) are still drawn
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart - delay ), [] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)a(l).End() < (uint64_t)r; } );
if( it == vec.end() ) return depth;
const auto zitend = std::lower_bound( it, vec.end(), m_vd.zvEnd + resolution, [] ( const auto& l, const auto& r ) { Adapter a; return a(l).Start() < r; } );
if( it == zitend ) return depth;
depth++;
int maxdepth = depth;
Adapter a;
while( it < zitend )
{
auto& ev = a(*it);
const auto end = m_worker.GetZoneEnd( ev );
const auto zsz = std::max( ( end - ev.Start() ) * pxns, pxns * 0.5 );
if( zsz < MinVisSize )
{
const auto MinVisNs = MinVisSize * nspx;
auto px1ns = end - m_vd.zvStart;
auto nextTime = end + MinVisNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, zitend, nextTime, [] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)a(l).End() < (uint64_t)r; } );
if( it == prevIt ) ++it;
if( it == zitend ) break;
const auto nend = m_worker.GetZoneEnd( a(*it) );
const auto nsnext = nend - m_vd.zvStart;
if( nsnext - px1ns >= MinVisNs * 2 ) break;
px1ns = nsnext;
nextTime = nend + nspx;
}
}
else
{
if( ev.HasChildren() )
{
const auto d = DispatchZoneLevel( m_worker.GetZoneChildren( ev.Child() ), hover, pxns, nspx, wpos, _offset, depth, yMin, yMax, tid );
if( d > maxdepth ) maxdepth = d;
}
++it;
}
}
return maxdepth;
}
int View::DispatchGpuZoneLevel( const Vector<short_ptr<GpuEvent>>& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift )
{
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * depth;
const auto yPos = wpos.y + offset;
if( yPos + ostep >= yMin && yPos <= yMax )
{
if( vec.is_magic() )
{
return DrawGpuZoneLevel<VectorAdapterDirect<GpuEvent>>( *(Vector<GpuEvent>*)&vec, hover, pxns, nspx, wpos, _offset, depth, thread, yMin, yMax, begin, drift );
}
else
{
return DrawGpuZoneLevel<VectorAdapterPointer<GpuEvent>>( vec, hover, pxns, nspx, wpos, _offset, depth, thread, yMin, yMax, begin, drift );
}
}
else
{
if( vec.is_magic() )
{
return SkipGpuZoneLevel<VectorAdapterDirect<GpuEvent>>( *(Vector<GpuEvent>*)&vec, hover, pxns, nspx, wpos, _offset, depth, thread, yMin, yMax, begin, drift );
}
else
{
return SkipGpuZoneLevel<VectorAdapterPointer<GpuEvent>>( vec, hover, pxns, nspx, wpos, _offset, depth, thread, yMin, yMax, begin, drift );
}
}
}
template<typename Adapter, typename V>
int View::DrawGpuZoneLevel( const V& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift )
{
const auto delay = m_worker.GetDelay();
const auto resolution = m_worker.GetResolution();
// cast to uint64_t, so that unended zones (end = -1) are still drawn
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart - delay ), [begin, drift] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)AdjustGpuTime( a(l).GpuEnd(), begin, drift ) < (uint64_t)r; } );
if( it == vec.end() ) return depth;
const auto zitend = std::lower_bound( it, vec.end(), std::max<int64_t>( 0, m_vd.zvEnd + resolution ), [begin, drift] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)AdjustGpuTime( a(l).GpuStart(), begin, drift ) < (uint64_t)r; } );
if( it == zitend ) return depth;
const auto w = ImGui::GetContentRegionAvail().x - 1;
const auto ty = ImGui::GetTextLineHeight();
const auto ostep = ty + 1;
const auto offset = _offset + ostep * depth;
auto draw = ImGui::GetWindowDrawList();
const auto dpos = wpos + ImVec2( 0.5f, 0.5f );
depth++;
int maxdepth = depth;
Adapter a;
while( it < zitend )
{
auto& ev = a(*it);
auto end = m_worker.GetZoneEnd( ev );
if( end == std::numeric_limits<int64_t>::max() ) break;
const auto start = AdjustGpuTime( ev.GpuStart(), begin, drift );
end = AdjustGpuTime( end, begin, drift );
const auto zsz = std::max( ( end - start ) * pxns, pxns * 0.5 );
if( zsz < MinVisSize )
{
const auto color = GetZoneColor( ev );
const auto MinVisNs = MinVisSize * nspx;
int num = 0;
const auto px0 = ( start - m_vd.zvStart ) * pxns;
auto px1ns = end - m_vd.zvStart;
auto rend = end;
auto nextTime = end + MinVisNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, zitend, std::max<int64_t>( 0, nextTime ), [begin, drift] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)AdjustGpuTime( a(l).GpuEnd(), begin, drift ) < (uint64_t)r; } );
if( it == prevIt ) ++it;
num += std::distance( prevIt, it );
if( it == zitend ) break;
const auto nend = AdjustGpuTime( m_worker.GetZoneEnd( a(*it) ), begin, drift );
const auto nsnext = nend - m_vd.zvStart;
if( nsnext < 0 || nsnext - px1ns >= MinVisNs * 2 ) break;
px1ns = nsnext;
rend = nend;
nextTime = nend + nspx;
}
const auto px1 = px1ns * pxns;
draw->AddRectFilled( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), offset + ty ), color );
DrawZigZag( draw, wpos + ImVec2( 0, offset + ty/2 ), std::max( px0, -10.0 ), std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), ty/4, DarkenColor( color ) );
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( std::max( px0, -10.0 ), offset ), wpos + ImVec2( std::min( std::max( px1, px0+MinVisSize ), double( w + 10 ) ), offset + ty + 1 ) ) )
{
if( num > 1 )
{
ImGui::BeginTooltip();
TextFocused( "Zones too small to display:", RealToString( num ) );
ImGui::Separator();
TextFocused( "Execution time:", TimeToString( rend - start ) );
ImGui::EndTooltip();
if( IsMouseClicked( 2 ) && rend - start > 0 )
{
ZoomToRange( start, rend );
}
}
else
{
const auto zoneThread = thread != 0 ? thread : m_worker.DecompressThread( ev.Thread() );
ZoneTooltip( ev );
if( IsMouseClicked( 2 ) && rend - start > 0 )
{
ZoomToZone( ev );
}
if( IsMouseClicked( 0 ) )
{
ShowZoneInfo( ev, zoneThread );
}
m_gpuThread = zoneThread;
m_gpuStart = ev.CpuStart();
m_gpuEnd = ev.CpuEnd();
}
}
const auto tmp = RealToString( num );
const auto tsz = ImGui::CalcTextSize( tmp );
if( tsz.x < px1 - px0 )
{
const auto x = px0 + ( px1 - px0 - tsz.x ) / 2;
DrawTextContrast( draw, wpos + ImVec2( x, offset ), 0xFF4488DD, tmp );
}
}
else
{
if( ev.Child() >= 0 )
{
const auto d = DispatchGpuZoneLevel( m_worker.GetGpuChildren( ev.Child() ), hover, pxns, nspx, wpos, _offset, depth, thread, yMin, yMax, begin, drift );
if( d > maxdepth ) maxdepth = d;
}
const char* zoneName = m_worker.GetZoneName( ev );
auto tsz = ImGui::CalcTextSize( zoneName );
const auto pr0 = ( start - m_vd.zvStart ) * pxns;
const auto pr1 = ( end - m_vd.zvStart ) * pxns;
const auto px0 = std::max( pr0, -10.0 );
const auto px1 = std::max( { std::min( pr1, double( w + 10 ) ), px0 + pxns * 0.5, px0 + MinVisSize } );
const auto zoneColor = GetZoneColorData( ev );
draw->AddRectFilled( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), zoneColor.color );
if( zoneColor.highlight )
{
draw->AddRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y ), zoneColor.accentColor, 0.f, -1, zoneColor.thickness );
}
else
{
const auto darkColor = DarkenColor( zoneColor.color );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px0, offset ), dpos + ImVec2( px1-1, offset ), zoneColor.accentColor, zoneColor.thickness );
DrawLine( draw, dpos + ImVec2( px0, offset + tsz.y ), dpos + ImVec2( px1-1, offset + tsz.y ), dpos + ImVec2( px1-1, offset ), darkColor, zoneColor.thickness );
}
if( tsz.x < zsz )
{
const auto x = ( start - m_vd.zvStart ) * pxns + ( ( end - start ) * pxns - tsz.x ) / 2;
if( x < 0 || x > w - tsz.x )
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( std::max( std::max( 0., px0 ), std::min( double( w - tsz.x ), x ) ), offset ), 0xFFFFFFFF, zoneName );
ImGui::PopClipRect();
}
else if( ev.GpuStart() == ev.GpuEnd() )
{
DrawTextContrast( draw, wpos + ImVec2( px0 + ( px1 - px0 - tsz.x ) * 0.5, offset ), 0xFFFFFFFF, zoneName );
}
else
{
DrawTextContrast( draw, wpos + ImVec2( x, offset ), 0xFFFFFFFF, zoneName );
}
}
else
{
ImGui::PushClipRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y * 2 ), true );
DrawTextContrast( draw, wpos + ImVec2( ( start - m_vd.zvStart ) * pxns, offset ), 0xFFFFFFFF, zoneName );
ImGui::PopClipRect();
}
if( hover && ImGui::IsMouseHoveringRect( wpos + ImVec2( px0, offset ), wpos + ImVec2( px1, offset + tsz.y + 1 ) ) )
{
const auto zoneThread = thread != 0 ? thread : m_worker.DecompressThread( ev.Thread() );
ZoneTooltip( ev );
if( !m_zoomAnim.active && IsMouseClicked( 2 ) )
{
ZoomToZone( ev );
}
if( IsMouseClicked( 0 ) )
{
ShowZoneInfo( ev, zoneThread );
}
m_gpuThread = zoneThread;
m_gpuStart = ev.CpuStart();
m_gpuEnd = ev.CpuEnd();
}
++it;
}
}
return maxdepth;
}
template<typename Adapter, typename V>
int View::SkipGpuZoneLevel( const V& vec, bool hover, double pxns, int64_t nspx, const ImVec2& wpos, int _offset, int depth, uint64_t thread, float yMin, float yMax, int64_t begin, int drift )
{
const auto delay = m_worker.GetDelay();
const auto resolution = m_worker.GetResolution();
// cast to uint64_t, so that unended zones (end = -1) are still drawn
auto it = std::lower_bound( vec.begin(), vec.end(), std::max<int64_t>( 0, m_vd.zvStart - delay ), [begin, drift] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)AdjustGpuTime( a(l).GpuEnd(), begin, drift ) < (uint64_t)r; } );
if( it == vec.end() ) return depth;
const auto zitend = std::lower_bound( it, vec.end(), std::max<int64_t>( 0, m_vd.zvEnd + resolution ), [begin, drift] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)AdjustGpuTime( a(l).GpuStart(), begin, drift ) < (uint64_t)r; } );
if( it == zitend ) return depth;
depth++;
int maxdepth = depth;
Adapter a;
while( it < zitend )
{
auto& ev = a(*it);
auto end = m_worker.GetZoneEnd( ev );
if( end == std::numeric_limits<int64_t>::max() ) break;
const auto start = AdjustGpuTime( ev.GpuStart(), begin, drift );
end = AdjustGpuTime( end, begin, drift );
const auto zsz = std::max( ( end - start ) * pxns, pxns * 0.5 );
if( zsz < MinVisSize )
{
const auto MinVisNs = MinVisSize * nspx;
auto px1ns = end - m_vd.zvStart;
auto nextTime = end + MinVisNs;
for(;;)
{
const auto prevIt = it;
it = std::lower_bound( it, zitend, nextTime, [begin, drift] ( const auto& l, const auto& r ) { Adapter a; return (uint64_t)AdjustGpuTime( a(l).GpuEnd(), begin, drift ) < (uint64_t)r; } );
if( it == prevIt ) ++it;
if( it == zitend ) break;
const auto nend = AdjustGpuTime( m_worker.GetZoneEnd( a(*it) ), begin, drift );
const auto nsnext = nend - m_vd.zvStart;
if( nsnext - px1ns >= MinVisNs * 2 ) break;
px1ns = nsnext;
nextTime = nend + nspx;
}
}
else
{
if( ev.Child() >= 0 )
{
const auto d = DispatchGpuZoneLevel( m_worker.GetGpuChildren( ev.Child() ), hover, pxns, nspx, wpos, _offset, depth, thread, yMin, yMax, begin, drift );
if( d > maxdepth ) maxdepth = d;
}
++it;
}
}
return maxdepth;
}
void View::DrawTextEditor()
{
const auto scale = GetScale();
ImGui::SetNextWindowSize( ImVec2( 1800 * scale, 800 * scale ), ImGuiCond_FirstUseEver );
bool show = true;
ImGui::Begin( "Source view", &show, ImGuiWindowFlags_NoScrollbar );
if( !ImGui::GetCurrentWindowRead()->SkipItems )
{
m_sourceView->UpdateFont( m_fixedFont, m_smallFont );
m_sourceView->Render( m_worker, *this );
}
ImGui::End();
if( !show ) m_sourceViewFile = nullptr;
}
void View::DrawRanges()
{
ImGui::Begin( "Time range limits", &m_showRanges, ImGuiWindowFlags_AlwaysAutoResize );
if( ImGui::GetCurrentWindowRead()->SkipItems ) { ImGui::End(); return; }
DrawRangeEntry( m_findZone.range, ICON_FA_SEARCH " Find zone", 0x4488DD88, "RangeFindZoneCopyFrom", 0 );
ImGui::Separator();
DrawRangeEntry( m_statRange, ICON_FA_SORT_AMOUNT_UP " Statistics", 0x448888EE, "RangeStatisticsCopyFrom", 1 );
ImGui::Separator();
DrawRangeEntry( m_waitStackRange, ICON_FA_HOURGLASS_HALF " Wait stacks", 0x44EEB588, "RangeWaitStackCopyFrom", 2 );
ImGui::Separator();
DrawRangeEntry( m_memInfo.range, ICON_FA_MEMORY " Memory", 0x4488EEE3, "RangeMemoryCopyFrom", 3 );
ImGui::End();
}
void View::DrawRangeEntry( Range& range, const char* label, uint32_t color, const char* popupLabel, int id )
{
SmallColorBox( color );
ImGui::SameLine();
if( SmallCheckbox( label, &range.active ) )
{
if( range.active && range.min == 0 && range.max == 0 )
{
range.min = m_vd.zvStart;
range.max = m_vd.zvEnd;
}
}
if( range.active )
{
ImGui::SameLine();
if( ImGui::SmallButton( "Limit to view" ) )
{
range.min = m_vd.zvStart;
range.max = m_vd.zvEnd;
}
TextFocused( "Time range:", TimeToStringExact( range.min ) );
ImGui::SameLine();
TextFocused( "-", TimeToStringExact( range.max ) );
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", TimeToString( range.max - range.min ) );
if( ImGui::SmallButton( ICON_FA_MICROSCOPE " Focus" ) ) ZoomToRange( range.min, range.max );
ImGui::SameLine();
if( SmallButtonDisablable( ICON_FA_STICKY_NOTE " Set from annotation", m_annotations.empty() ) ) ImGui::OpenPopup( popupLabel );
if( ImGui::BeginPopup( popupLabel ) )
{
for( auto& v : m_annotations )
{
SmallColorBox( v->color );
ImGui::SameLine();
if( ImGui::Selectable( v->text.c_str() ) )
{
range.min = v->range.min;
range.max = v->range.max;
}
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
ImGui::TextDisabled( "%s - %s (%s)", TimeToStringExact( v->range.min ), TimeToStringExact( v->range.max ), TimeToString( v->range.max - v->range.min ) );
}
ImGui::EndPopup();
}
if( id != 0 )
{
ImGui::SameLine();
if( SmallButtonDisablable( ICON_FA_SEARCH " Copy from find zone", m_findZone.range.min == 0 && m_findZone.range.max == 0 ) ) range = m_findZone.range;
}
if( id != 1 )
{
ImGui::SameLine();
if( SmallButtonDisablable( ICON_FA_SORT_AMOUNT_UP " Copy from statistics", m_statRange.min == 0 && m_statRange.max == 0 ) ) range = m_statRange;
}
if( id != 2 )
{
ImGui::SameLine();
if( SmallButtonDisablable( ICON_FA_HOURGLASS_HALF " Copy from wait stacks", m_waitStackRange.min == 0 && m_waitStackRange.max == 0 ) ) range = m_waitStackRange;
}
if( id != 3 )
{
ImGui::SameLine();
if( SmallButtonDisablable( ICON_FA_MEMORY " Copy from memory", m_memInfo.range.min == 0 && m_memInfo.range.max == 0 ) ) range = m_memInfo.range;
}
}
}
void View::CrashTooltip()
{
auto& crash = m_worker.GetCrashEvent();
ImGui::BeginTooltip();
TextFocused( "Time:", TimeToString( crash.time ) );
TextFocused( "Reason:", m_worker.GetString( crash.message ) );
ImGui::EndTooltip();
}
void View::DrawSourceTooltip( const char* filename, uint32_t srcline, int before, int after, bool separateTooltip )
{
if( !filename ) return;
if( !SourceFileValid( filename, m_worker.GetCaptureTime(), *this, m_worker ) ) return;
m_srcHintCache.Parse( filename, m_worker, *this );
if( m_srcHintCache.empty() ) return;
ImGui::PushStyleVar( ImGuiStyleVar_ItemSpacing, ImVec2( 0, 0 ) );
if( separateTooltip ) ImGui::BeginTooltip();
ImGui::PushFont( m_fixedFont );
auto& lines = m_srcHintCache.get();
const int start = std::max( 0, (int)srcline - ( before+1 ) );
const int end = std::min<int>( m_srcHintCache.get().size(), srcline + after );
bool first = true;
int bottomEmpty = 0;
for( int i=start; i<end; i++ )
{
auto& line = lines[i];
if( line.begin == line.end )
{
if( !first ) bottomEmpty++;
}
else
{
first = false;
while( bottomEmpty > 0 )
{
ImGui::TextUnformatted( "" );
bottomEmpty--;
}
auto ptr = line.begin;
auto it = line.tokens.begin();
while( ptr < line.end )
{
if( it == line.tokens.end() )
{
ImGui::TextUnformatted( ptr, line.end );
ImGui::SameLine( 0, 0 );
break;
}
if( ptr < it->begin )
{
ImGui::TextUnformatted( ptr, it->begin );
ImGui::SameLine( 0, 0 );
}
TextColoredUnformatted( i == srcline-1 ? SyntaxColors[(int)it->color] : SyntaxColorsDimmed[(int)it->color], it->begin, it->end );
ImGui::SameLine( 0, 0 );
ptr = it->end;
++it;
}
ImGui::ItemSize( ImVec2( 0, 0 ), 0 );
}
}
ImGui::PopFont();
if( separateTooltip ) ImGui::EndTooltip();
ImGui::PopStyleVar();
}
bool View::Save( const char* fn, FileWrite::Compression comp, int zlevel, bool buildDict )
{
std::unique_ptr<FileWrite> f( FileWrite::Open( fn, comp, zlevel ) );
if( !f ) return false;
m_userData.StateShouldBePreserved();
m_saveThreadState.store( SaveThreadState::Saving, std::memory_order_relaxed );
m_saveThread = std::thread( [this, f{std::move( f )}, buildDict] {
std::lock_guard<std::mutex> lock( m_worker.GetDataLock() );
m_worker.Write( *f, buildDict );
f->Finish();
const auto stats = f->GetCompressionStatistics();
m_srcFileBytes.store( stats.first, std::memory_order_relaxed );
m_dstFileBytes.store( stats.second, std::memory_order_relaxed );
m_saveThreadState.store( SaveThreadState::NeedsJoin, std::memory_order_release );
} );
return true;
}
}