From 7fbd55369e2c9514bf793e35b1a3c978a0f7f122 Mon Sep 17 00:00:00 2001 From: DrChat Date: Wed, 28 Feb 2018 18:52:45 -0600 Subject: [PATCH] [UI] Update microprofile to latest --- src/xenia/base/profiling.cc | 8 +- third_party/microprofile/microprofile.h | 5615 +++++++++++-------- third_party/microprofile/microprofiledraw.h | 648 +++ third_party/microprofile/microprofilehtml.h | 4860 ++++++++++++++++ third_party/microprofile/microprofileui.h | 5517 ++++++++++-------- 5 files changed, 12147 insertions(+), 4501 deletions(-) create mode 100644 third_party/microprofile/microprofiledraw.h create mode 100644 third_party/microprofile/microprofilehtml.h diff --git a/src/xenia/base/profiling.cc b/src/xenia/base/profiling.cc index da069b6f5..073adaa82 100644 --- a/src/xenia/base/profiling.cc +++ b/src/xenia/base/profiling.cc @@ -91,8 +91,8 @@ void Profiler::Dump() { #if XE_OPTION_PROFILING_UI MicroProfileDumpTimers(); #endif // XE_OPTION_PROFILING_UI - MicroProfileDumpHtml("profile.html"); - MicroProfileDumpHtmlToFile(); + // MicroProfileDumpHtml("profile.html"); + // MicroProfileDumpHtmlToFile(); } void Profiler::Shutdown() { @@ -276,10 +276,6 @@ void Profiler::Flip() {} uint32_t MicroProfileGpuInsertTimeStamp() { return 0; } -uint64_t MicroProfileGpuGetTimeStamp(uint32_t nKey) { return 0; } - -uint64_t MicroProfileTicksPerSecondGpu() { return 0; } - const char* MicroProfileGetThreadName() { return "TODO: get thread name!"; } #if XE_OPTION_PROFILING diff --git a/third_party/microprofile/microprofile.h b/third_party/microprofile/microprofile.h index a7a1b1aad..29de52dcd 100644 --- a/third_party/microprofile/microprofile.h +++ b/third_party/microprofile/microprofile.h @@ -29,7 +29,7 @@ // Call these functions from your code: // MicroProfileOnThreadCreate // MicroProfileMouseButton -// MicroProfileMousePosition +// MicroProfileMousePosition // MicroProfileModKey // MicroProfileFlip <-- Call this once per frame // MicroProfileDraw <-- Call this once per frame @@ -47,7 +47,8 @@ // MICROPROFILE_SCOPEGPU // MICROPROFILE_SCOPEGPUI // MICROPROFILE_META -// +// MICROPROFILE_LABEL +// MICROPROFILE_LABELF // // Usage: // @@ -76,12 +77,41 @@ // void MicroProfileDrawText(int nX, int nY, uint32_t nColor, const char* pText, uint32_t nNumCharacters); // void MicroProfileDrawBox(int nX, int nY, int nX1, int nY1, uint32_t nColor, MicroProfileBoxType = MicroProfileBoxTypeFlat); // void MicroProfileDrawLine2D(uint32_t nVertices, float* pVertices, uint32_t nColor); -// Gpu time stamps: -// uint32_t MicroProfileGpuInsertTimeStamp(); +// Gpu time stamps: (See below for d3d/opengl helper) +// uint32_t MicroProfileGpuInsertTimer(void* pContext); // uint64_t MicroProfileGpuGetTimeStamp(uint32_t nKey); // uint64_t MicroProfileTicksPerSecondGpu(); // threading: // const char* MicroProfileGetThreadName(); Threadnames in detailed view +// +// Default implementations of Gpu timestamp functions: +// OpenGL: +// in .c file where MICROPROFILE_IMPL is defined: +// #define MICROPROFILE_GPU_TIMERS_GL 1 +// call MicroProfileGpuInitGL() on startup +// D3D11: +// in .c file where MICROPROFILE_IMPL is defined: +// #define MICROPROFILE_GPU_TIMERS_D3D11 1 +// call MicroProfileGpuInitD3D11(Device) on startup +// D3D12: +// in .c file where MICROPROFILE_IMPL is defined: +// #define MICROPROFILE_GPU_TIMERS_D3D12 1 +// call MicroProfileGpuInitD3D12(Device, CommandQueue) on startup +// single-threaded: call MicroProfileGpuSetContext(CommandList) every frame before issuing GPU markers +// multi-threaded: +// #define MICROPROFILE_GPU_TIMERS_MULTITHREADED 1 +// on recording thread before using command list, call MicroProfileGpuBegin(CommandList) +// on recording thread after you're done with command list, call work = MicroProfileGpuEnd() +// when replaying, call MicroProfileGpuSubmit(work) in the same order as ExecuteCommandLists +// Vulkan: +// in .c file where MICROPROFILE_IMPL is defined: +// #define MICROPROFILE_GPU_TIMERS_VK 1 +// call MicroProfileGpuInitVK(Device, PhysicalDevice, Queue) on startup +// the rest is the same as for D3D12 +// +// Limitations: +// GPU timestamps can only be inserted from one thread. + #ifndef MICROPROFILE_ENABLED @@ -97,18 +127,26 @@ typedef uint16_t MicroProfileGroupId; #define MICROPROFILE_DECLARE(var) #define MICROPROFILE_DEFINE(var, group, name, color) #define MICROPROFILE_DECLARE_GPU(var) -#define MICROPROFILE_DEFINE_GPU(var, group, name, color) +#define MICROPROFILE_DEFINE_GPU(var, name, color) #define MICROPROFILE_SCOPE(var) do{}while(0) +#define MICROPROFILE_SCOPE_TOKEN(token) do{} while(0) #define MICROPROFILE_SCOPEI(group, name, color) do{}while(0) #define MICROPROFILE_SCOPEGPU(var) do{}while(0) -#define MICROPROFILE_SCOPEGPUI(group, name, color) do{}while(0) +#define MICROPROFILE_SCOPEGPUI(name, color) do{}while(0) #define MICROPROFILE_META_CPU(name, count) #define MICROPROFILE_META_GPU(name, count) +#define MICROPROFILE_LABEL(group, name) do{}while(0) +#define MICROPROFILE_LABELF(group, name, ...) do{}while(0) +#define MICROPROFILE_COUNTER_ADD(name, count) do{} while(0) +#define MICROPROFILE_COUNTER_SUB(name, count) do{} while(0) +#define MICROPROFILE_COUNTER_SET(name, count) do{} while(0) +#define MICROPROFILE_COUNTER_SET_LIMIT(name, count) do{} while(0) +#define MICROPROFILE_COUNTER_CONFIG(name, type, limit, flags) do{} while(0) + #define MICROPROFILE_FORCEENABLECPUGROUP(s) do{} while(0) #define MICROPROFILE_FORCEDISABLECPUGROUP(s) do{} while(0) #define MICROPROFILE_FORCEENABLEGPUGROUP(s) do{} while(0) #define MICROPROFILE_FORCEDISABLEGPUGROUP(s) do{} while(0) -#define MICROPROFILE_SCOPE_TOKEN(token) #define MicroProfileGetTime(group, name) 0.f #define MicroProfileOnThreadCreate(foo) do{}while(0) @@ -117,25 +155,39 @@ typedef uint16_t MicroProfileGroupId; #define MicroProfileGetAggregateFrames() 0 #define MicroProfileGetCurrentAggregateFrames() 0 #define MicroProfileTogglePause() do{}while(0) -#define MicroProfileToggleAllGroups() do{} while(0) -#define MicroProfileDumpTimers() do{}while(0) #define MicroProfileShutdown() do{}while(0) #define MicroProfileSetForceEnable(a) do{} while(0) #define MicroProfileGetForceEnable() false #define MicroProfileSetEnableAllGroups(a) do{} while(0) +#define MicroProfileEnableCategory(a) do{} while(0) +#define MicroProfileDisableCategory(a) do{} while(0) #define MicroProfileGetEnableAllGroups() false #define MicroProfileSetForceMetaCounters(a) #define MicroProfileGetForceMetaCounters() 0 -#define MicroProfileDumpHtml(c) do{} while(0) -#define MicroProfileWebServerPort() ((uint32_t)-1) +#define MicroProfileEnableMetaCounter(c) do{} while(0) +#define MicroProfileDisableMetaCounter(c) do{} while(0) +#define MicroProfileContextSwitchTraceStart() do{} while(0) +#define MicroProfileContextSwitchTraceStop() do{} while(0) +#define MicroProfileDumpFile(path,type,frames) do{} while(0) +#define MicroProfileWebServerStart() do{} while(0) +#define MicroProfileWebServerStop() do{} while(0) +#define MicroProfileWebServerPort() 0 + +#define MicroProfileGpuSetContext(c) do{} while(0) +#define MicroProfileGpuBegin(c) do{} while(0) +#define MicroProfileGpuEnd() 0 +#define MicroProfileGpuSubmit(w) do{} while(0) #else #include #include + +#ifndef MICROPROFILE_NOCXX11 #include #include #include +#endif #ifndef MICROPROFILE_API #define MICROPROFILE_API @@ -150,29 +202,35 @@ MICROPROFILE_API int64_t MicroProfileTicksPerSecondCpu(); #include #include #include -#if TARGET_OS_IPHONE -#define MICROPROFILE_IOS -#endif #define MP_TICK() mach_absolute_time() inline int64_t MicroProfileTicksPerSecondCpu() { - static int64_t nTicksPerSecond = 0; - if(nTicksPerSecond == 0) + static int64_t nTicksPerSecond = 0; + if(nTicksPerSecond == 0) { - mach_timebase_info_data_t sTimebaseInfo; + mach_timebase_info_data_t sTimebaseInfo; mach_timebase_info(&sTimebaseInfo); nTicksPerSecond = 1000000000ll * sTimebaseInfo.denom / sTimebaseInfo.numer; } return nTicksPerSecond; } +inline uint64_t MicroProfileGetCurrentThreadId() +{ + uint64_t tid; + pthread_threadid_np(nullptr, &tid); + return tid; +} #define MP_BREAK() __builtin_trap() +#if __has_feature(tls) #define MP_THREAD_LOCAL __thread +#endif #define MP_STRCASECMP strcasecmp -#define MP_GETCURRENTTHREADID() (uint64_t)pthread_self() -typedef uint64_t ThreadIdType; - +#define MP_GETCURRENTTHREADID() MicroProfileGetCurrentThreadId() +typedef uint64_t MicroProfileThreadIdType; +#define MP_GETCURRENTPROCESSID() getpid() +typedef uint32_t MicroProfileProcessIdType; #elif defined(_WIN32) int64_t MicroProfileGetTick(); #define MP_TICK() MicroProfileGetTick() @@ -180,7 +238,9 @@ int64_t MicroProfileGetTick(); #define MP_THREAD_LOCAL __declspec(thread) #define MP_STRCASECMP _stricmp #define MP_GETCURRENTTHREADID() GetCurrentThreadId() -typedef uint32_t ThreadIdType; +typedef uint32_t MicroProfileThreadIdType; +#define MP_GETCURRENTPROCESSID() GetCurrentProcessId() +typedef uint32_t MicroProfileProcessIdType; #elif defined(__linux__) #include @@ -198,46 +258,64 @@ inline int64_t MicroProfileGetTick() } #define MP_TICK() MicroProfileGetTick() #define MP_BREAK() __builtin_trap() +#ifndef __ANDROID__ // __thread is incompatible with ffunction-sections/fdata-sections #define MP_THREAD_LOCAL __thread +#endif #define MP_STRCASECMP strcasecmp #define MP_GETCURRENTTHREADID() (uint64_t)pthread_self() -typedef uint64_t ThreadIdType; +typedef uint64_t MicroProfileThreadIdType; +#define MP_GETCURRENTPROCESSID() getpid() +typedef uint32_t MicroProfileProcessIdType; #endif -#ifndef MP_GETCURRENTTHREADID + +#ifndef MP_GETCURRENTTHREADID #define MP_GETCURRENTTHREADID() 0 -typedef uint32_t ThreadIdType; +typedef uint32_t MicroProfileThreadIdType; #endif +#ifndef MP_GETCURRENTPROCESSID +#define MP_GETCURRENTPROCESSID() 0 +typedef uint32_t MicroProfileProcessIdType; +#endif +#ifndef MP_ASSERT #define MP_ASSERT(a) do{if(!(a)){MP_BREAK();} }while(0) +#endif + #define MICROPROFILE_DECLARE(var) extern MicroProfileToken g_mp_##var #define MICROPROFILE_DEFINE(var, group, name, color) MicroProfileToken g_mp_##var = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeCpu) #define MICROPROFILE_DECLARE_GPU(var) extern MicroProfileToken g_mp_##var -#define MICROPROFILE_DEFINE_GPU(var, group, name, color) MicroProfileToken g_mp_##var = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeGpu) +#define MICROPROFILE_DEFINE_GPU(var, name, color) MicroProfileToken g_mp_##var = MicroProfileGetToken("GPU", name, color, MicroProfileTokenTypeGpu) #define MICROPROFILE_TOKEN_PASTE0(a, b) a ## b #define MICROPROFILE_TOKEN_PASTE(a, b) MICROPROFILE_TOKEN_PASTE0(a,b) #define MICROPROFILE_SCOPE(var) MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(g_mp_##var) #define MICROPROFILE_SCOPE_TOKEN(token) MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(token) #define MICROPROFILE_SCOPEI(group, name, color) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__) = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeCpu); MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo,__LINE__)( MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__)) -#define MICROPROFILE_SCOPEGPU(var) MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(g_mp_##var) -#define MICROPROFILE_SCOPEGPUI(group, name, color) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__) = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeGpu); MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(foo,__LINE__)( MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__)) +#define MICROPROFILE_SCOPEGPU(var) MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(g_mp_##var) +#define MICROPROFILE_SCOPEGPUI(name, color) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__) = MicroProfileGetToken("GPU", name, color, MicroProfileTokenTypeGpu); MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo,__LINE__)( MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__)) #define MICROPROFILE_META_CPU(name, count) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_meta,__LINE__) = MicroProfileGetMetaToken(name); MicroProfileMetaUpdate(MICROPROFILE_TOKEN_PASTE(g_mp_meta,__LINE__), count, MicroProfileTokenTypeCpu) #define MICROPROFILE_META_GPU(name, count) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_meta,__LINE__) = MicroProfileGetMetaToken(name); MicroProfileMetaUpdate(MICROPROFILE_TOKEN_PASTE(g_mp_meta,__LINE__), count, MicroProfileTokenTypeGpu) - +#define MICROPROFILE_LABEL(group, name) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__) = MicroProfileGetLabelToken(group); MicroProfileLabel(MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__), name) +#define MICROPROFILE_LABELF(group, name, ...) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__) = MicroProfileGetLabelToken(group); MicroProfileLabelFormat(MICROPROFILE_TOKEN_PASTE(g_mp,__LINE__), name, ## __VA_ARGS__) +#define MICROPROFILE_COUNTER_ADD(name, count) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__) = MicroProfileGetCounterToken(name); MicroProfileCounterAdd(MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__), count) +#define MICROPROFILE_COUNTER_SUB(name, count) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__) = MicroProfileGetCounterToken(name); MicroProfileCounterAdd(MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__), -(int64_t)count) +#define MICROPROFILE_COUNTER_SET(name, count) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__) = MicroProfileGetCounterToken(name); MicroProfileCounterSet(MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__), count) +#define MICROPROFILE_COUNTER_SET_LIMIT(name, count) static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__) = MicroProfileGetCounterToken(name); MicroProfileCounterSetLimit(MICROPROFILE_TOKEN_PASTE(g_mp_counter,__LINE__), count) +#define MICROPROFILE_COUNTER_CONFIG(name, type, limit, flags) MicroProfileCounterConfig(name, type, limit, flags #ifndef MICROPROFILE_USE_THREAD_NAME_CALLBACK #define MICROPROFILE_USE_THREAD_NAME_CALLBACK 0 #endif -#ifndef MICROPROFILE_GPU_FRAME_DELAY -#define MICROPROFILE_GPU_FRAME_DELAY 3 //must be > 0 -#endif - #ifndef MICROPROFILE_PER_THREAD_BUFFER_SIZE #define MICROPROFILE_PER_THREAD_BUFFER_SIZE (2048<<10) #endif +#ifndef MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE +#define MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE (1024<<10) +#endif + #ifndef MICROPROFILE_MAX_FRAME_HISTORY #define MICROPROFILE_MAX_FRAME_HISTORY 512 #endif @@ -258,18 +336,42 @@ typedef uint32_t ThreadIdType; #define MICROPROFILE_WEBSERVER 1 #endif -#ifndef MICROPROFILE_WEBSERVER_MAXFRAMES -#define MICROPROFILE_WEBSERVER_MAXFRAMES 30 +#ifndef MICROPROFILE_WEBSERVER_FRAMES +#define MICROPROFILE_WEBSERVER_FRAMES 30 #endif -#ifndef MICROPROFILE_GPU_TIMERS -#define MICROPROFILE_GPU_TIMERS 1 +#ifndef MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE +#define MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE (16<<10) +#endif + +#ifndef MICROPROFILE_LABEL_BUFFER_SIZE +#define MICROPROFILE_LABEL_BUFFER_SIZE (1024<<10) +#endif + +#ifndef MICROPROFILE_GPU_MAX_QUERIES +#define MICROPROFILE_GPU_MAX_QUERIES (8<<10) +#endif + +#ifndef MICROPROFILE_GPU_FRAME_DELAY +#define MICROPROFILE_GPU_FRAME_DELAY 3 #endif #ifndef MICROPROFILE_NAME_MAX_LEN #define MICROPROFILE_NAME_MAX_LEN 64 #endif +#ifndef MICROPROFILE_LABEL_MAX_LEN +#define MICROPROFILE_LABEL_MAX_LEN 256 +#endif + +#ifndef MICROPROFILE_EMBED_HTML +#define MICROPROFILE_EMBED_HTML 1 +#endif + +#ifndef MICROPROFILE_GPU_TIMERS_MULTITHREADED +#define MICROPROFILE_GPU_TIMERS_MULTITHREADED 0 +#endif + #define MICROPROFILE_FORCEENABLECPUGROUP(s) MicroProfileForceEnableGroup(s, MicroProfileTokenTypeCpu) #define MICROPROFILE_FORCEDISABLECPUGROUP(s) MicroProfileForceDisableGroup(s, MicroProfileTokenTypeCpu) #define MICROPROFILE_FORCEENABLEGPUGROUP(s) MicroProfileForceEnableGroup(s, MicroProfileTokenTypeGpu) @@ -279,7 +381,7 @@ typedef uint32_t ThreadIdType; #define MICROPROFILE_GROUP_MASK_ALL 0xffffffffffff -#define MICROPROFILE_INVALID_TOKEN (uint64_t)-1 +#define MICROPROFILE_INVALID_TOKEN (uint64_t)0 enum MicroProfileTokenType { @@ -293,16 +395,17 @@ enum MicroProfileBoxType MicroProfileBoxTypeFlat, }; -// struct MicroProfileState -// { -// uint32_t nDisplay; -// uint32_t nAllGroupsWanted; -// uint64_t nActiveGroupWanted; -// uint32_t nAllThreadsWanted; -// uint32_t nAggregateFlip; -// uint32_t nBars; -// float fReferenceTime; -// }; +enum MicroProfileDumpType +{ + MicroProfileDumpTypeHtml, + MicroProfileDumpTypeCsv +}; + +#ifdef __GNUC__ +#define MICROPROFILE_FORMAT(a, b) __attribute__((format(printf, a, b))) +#else +#define MICROPROFILE_FORMAT(a, b) +#endif struct MicroProfile; @@ -310,69 +413,94 @@ MICROPROFILE_API void MicroProfileInit(); MICROPROFILE_API void MicroProfileShutdown(); MICROPROFILE_API MicroProfileToken MicroProfileFindToken(const char* sGroup, const char* sName); MICROPROFILE_API MicroProfileToken MicroProfileGetToken(const char* sGroup, const char* sName, uint32_t nColor, MicroProfileTokenType Token = MicroProfileTokenTypeCpu); +MICROPROFILE_API MicroProfileToken MicroProfileGetLabelToken(const char* sGroup, MicroProfileTokenType Token = MicroProfileTokenTypeCpu); MICROPROFILE_API MicroProfileToken MicroProfileGetMetaToken(const char* pName); +MICROPROFILE_API MicroProfileToken MicroProfileGetCounterToken(const char* pName); MICROPROFILE_API void MicroProfileMetaUpdate(MicroProfileToken, int nCount, MicroProfileTokenType eTokenType); +MICROPROFILE_API void MicroProfileCounterAdd(MicroProfileToken nToken, int64_t nCount); +MICROPROFILE_API void MicroProfileCounterSet(MicroProfileToken nToken, int64_t nCount); +MICROPROFILE_API void MicroProfileCounterSetLimit(MicroProfileToken nToken, int64_t nCount); +MICROPROFILE_API void MicroProfileCounterConfig(const char* pCounterName, uint32_t nFormat, int64_t nLimit, uint32_t nFlags); MICROPROFILE_API uint64_t MicroProfileEnter(MicroProfileToken nToken); MICROPROFILE_API void MicroProfileLeave(MicroProfileToken nToken, uint64_t nTick); -MICROPROFILE_API uint64_t MicroProfileGpuEnter(MicroProfileToken nToken); -MICROPROFILE_API void MicroProfileGpuLeave(MicroProfileToken nToken, uint64_t nTick); +MICROPROFILE_API void MicroProfileLabel(MicroProfileToken nToken, const char* pName); +MICROPROFILE_FORMAT(2, 3) MICROPROFILE_API void MicroProfileLabelFormat(MicroProfileToken nToken, const char* pName, ...); +MICROPROFILE_API void MicroProfileLabelFormatV(MicroProfileToken nToken, const char* pName, va_list args); inline uint16_t MicroProfileGetTimerIndex(MicroProfileToken t){ return (t&0xffff); } inline uint64_t MicroProfileGetGroupMask(MicroProfileToken t){ return ((t>>16)&MICROPROFILE_GROUP_MASK_ALL);} inline MicroProfileToken MicroProfileMakeToken(uint64_t nGroupMask, uint16_t nTimer){ return (nGroupMask<<16) | nTimer;} -MICROPROFILE_API void MicroProfileFlip(); //! called once per frame. +MICROPROFILE_API void MicroProfileFlip(); //! call once per frame. MICROPROFILE_API void MicroProfileTogglePause(); -// MICROPROFILE_API void MicroProfileGetState(MicroProfileState* pStateOut); -// MICROPROFILE_API void MicroProfileSetState(MicroProfileState* pStateIn); MICROPROFILE_API void MicroProfileForceEnableGroup(const char* pGroup, MicroProfileTokenType Type); MICROPROFILE_API void MicroProfileForceDisableGroup(const char* pGroup, MicroProfileTokenType Type); MICROPROFILE_API float MicroProfileGetTime(const char* pGroup, const char* pName); + MICROPROFILE_API void MicroProfileOnThreadCreate(const char* pThreadName); //should be called from newly created threads MICROPROFILE_API void MicroProfileOnThreadExit(); //call on exit to reuse log -MICROPROFILE_API void MicroProfileInitThreadLog(); MICROPROFILE_API void MicroProfileSetForceEnable(bool bForceEnable); MICROPROFILE_API bool MicroProfileGetForceEnable(); -MICROPROFILE_API void MicroProfileSetEnableAllGroups(bool bEnable); +MICROPROFILE_API void MicroProfileSetEnableAllGroups(bool bEnable); +MICROPROFILE_API void MicroProfileEnableCategory(const char* pCategory); +MICROPROFILE_API void MicroProfileDisableCategory(const char* pCategory); MICROPROFILE_API bool MicroProfileGetEnableAllGroups(); -MICROPROFILE_API void MicroProfileSetForceMetaCounters(bool bEnable); +MICROPROFILE_API void MicroProfileSetForceMetaCounters(bool bEnable); MICROPROFILE_API bool MicroProfileGetForceMetaCounters(); +MICROPROFILE_API void MicroProfileEnableMetaCounter(const char* pMet); +MICROPROFILE_API void MicroProfileDisableMetaCounter(const char* pMet); MICROPROFILE_API void MicroProfileSetAggregateFrames(int frames); MICROPROFILE_API int MicroProfileGetAggregateFrames(); MICROPROFILE_API int MicroProfileGetCurrentAggregateFrames(); MICROPROFILE_API MicroProfile* MicroProfileGet(); MICROPROFILE_API void MicroProfileGetRange(uint32_t nPut, uint32_t nGet, uint32_t nRange[2][2]); MICROPROFILE_API std::recursive_mutex& MicroProfileGetMutex(); -MICROPROFILE_API void MicroProfileStartContextSwitchTrace(); -MICROPROFILE_API void MicroProfileStopContextSwitchTrace(); -MICROPROFILE_API bool MicroProfileIsLocalThread(uint32_t nThreadId); +MICROPROFILE_API void MicroProfileContextSwitchTraceStart(); +MICROPROFILE_API void MicroProfileContextSwitchTraceStop(); -#if MICROPROFILE_WEBSERVER -MICROPROFILE_API void MicroProfileDumpHtml(const char* pFile); +struct MicroProfileThreadInfo +{ + MicroProfileProcessIdType nProcessId; + MicroProfileThreadIdType nThreadId; +}; + +MICROPROFILE_API void MicroProfileContextSwitchSearch(uint32_t* pContextSwitchStart, uint32_t* pContextSwitchEnd, uint64_t nBaseTicksCpu, uint64_t nBaseTicksEndCpu); +MICROPROFILE_API uint32_t MicroProfileContextSwitchGatherThreads(uint32_t nContextSwitchStart, uint32_t nContextSwitchEnd, MicroProfileThreadInfo* Threads, uint32_t* nNumThreadsBase); + +MICROPROFILE_API const char* MicroProfileGetProcessName(MicroProfileProcessIdType nId, char* Buffer, uint32_t nSize); + +MICROPROFILE_API void MicroProfileDumpFile(const char* pPath, MicroProfileDumpType eType, uint32_t nFrames); +MICROPROFILE_API int MicroProfileFormatCounter(int eFormat, int64_t nCounter, char* pOut, uint32_t nBufferSize); + +MICROPROFILE_API void MicroProfileWebServerStart(); +MICROPROFILE_API void MicroProfileWebServerStop(); MICROPROFILE_API uint32_t MicroProfileWebServerPort(); -#else -#define MicroProfileDumpHtml(c) do{} while(0) -#define MicroProfileWebServerPort() ((uint32_t)-1) -#endif +MICROPROFILE_API void MicroProfileGpuInitGL(); +MICROPROFILE_API void MicroProfileGpuInitD3D11(struct ID3D11Device* pDevice); +MICROPROFILE_API void MicroProfileGpuInitD3D12(struct ID3D12Device* pDevice, struct ID3D12CommandQueue* pCommandQueue); +MICROPROFILE_API void MicroProfileGpuInitVK(struct VkDevice_T* pDevice, struct VkPhysicalDevice_T* pPhysicalDevice, struct VkQueue_T* pQueue); +MICROPROFILE_API void MicroProfileGpuShutdown(); +MICROPROFILE_API void MicroProfileGpuSetContext(void* pContext); +MICROPROFILE_API void MicroProfileGpuBegin(void* pContext); +MICROPROFILE_API uint64_t MicroProfileGpuEnd(); +MICROPROFILE_API void MicroProfileGpuSubmit(uint64_t nWork); - -#if MICROPROFILE_GPU_TIMERS -MICROPROFILE_API uint32_t MicroProfileGpuInsertTimeStamp(); +MICROPROFILE_API uint32_t MicroProfileGpuInsertTimer(void* pContext); MICROPROFILE_API uint64_t MicroProfileGpuGetTimeStamp(uint32_t nKey); MICROPROFILE_API uint64_t MicroProfileTicksPerSecondGpu(); -#else -#define MicroProfileGpuInsertTimeStamp() 1 -#define MicroProfileGpuGetTimeStamp(a) 0 -#define MicroProfileTicksPerSecondGpu() 1 -#endif - +MICROPROFILE_API uint32_t MicroProfileGpuFlip(); +MICROPROFILE_API bool MicroProfileGetGpuTickReference(int64_t* pOutCpu, int64_t* pOutGpu); #if MICROPROFILE_USE_THREAD_NAME_CALLBACK MICROPROFILE_API const char* MicroProfileGetThreadName(); #else -#define MicroProfileGetThreadName() "" +#define MicroProfileGetThreadName() "" +#endif + +#if !defined(MICROPROFILE_THREAD_NAME_FROM_ID) +#define MICROPROFILE_THREAD_NAME_FROM_ID(a) "" #endif struct MicroProfileScopeHandler @@ -389,27 +517,16 @@ struct MicroProfileScopeHandler } }; -struct MicroProfileScopeGpuHandler -{ - MicroProfileToken nToken; - uint64_t nTick; - MicroProfileScopeGpuHandler(MicroProfileToken Token):nToken(Token) - { - nTick = MicroProfileGpuEnter(nToken); - } - ~MicroProfileScopeGpuHandler() - { - MicroProfileGpuLeave(nToken, nTick); - } -}; +#define MICROPROFILE_MAX_COUNTERS 512 +#define MICROPROFILE_MAX_COUNTER_NAME_CHARS (MICROPROFILE_MAX_COUNTERS*16) - - -#define MICROPROFILE_MAX_TIMERS 1024 #define MICROPROFILE_MAX_GROUPS 48 //dont bump! no. of bits used it bitmask +#define MICROPROFILE_MAX_CATEGORIES 16 #define MICROPROFILE_MAX_GRAPHS 5 #define MICROPROFILE_GRAPH_HISTORY 128 #define MICROPROFILE_BUFFER_SIZE ((MICROPROFILE_PER_THREAD_BUFFER_SIZE)/sizeof(MicroProfileLogEntry)) +#define MICROPROFILE_GPU_BUFFER_SIZE ((MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE)/sizeof(MicroProfileLogEntry)) +#define MICROPROFILE_GPU_FRAMES ((MICROPROFILE_GPU_FRAME_DELAY)+1) #define MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS 256 #define MICROPROFILE_STACK_MAX 32 //#define MICROPROFILE_MAX_PRESETS 5 @@ -417,10 +534,14 @@ struct MicroProfileScopeGpuHandler #define MICROPROFILE_GAP_TIME 50 //extra ms to fetch to close timers from earlier frames -#ifndef MICROPROFILE_MAX_THREADS -#define MICROPROFILE_MAX_THREADS 64 +#ifndef MICROPROFILE_MAX_TIMERS +#define MICROPROFILE_MAX_TIMERS 1024 #endif +#ifndef MICROPROFILE_MAX_THREADS +#define MICROPROFILE_MAX_THREADS 32 +#endif + #ifndef MICROPROFILE_UNPACK_RED #define MICROPROFILE_UNPACK_RED(c) ((c)>>16) #endif @@ -438,8 +559,10 @@ struct MicroProfileScopeGpuHandler #endif -#ifndef MICROPROFILE_CONTEXT_SWITCH_TRACE -#ifdef _WIN32 +#ifndef MICROPROFILE_CONTEXT_SWITCH_TRACE +#if defined(_WIN32) +#define MICROPROFILE_CONTEXT_SWITCH_TRACE 1 +#elif defined(__APPLE__) && !TARGET_OS_IPHONE #define MICROPROFILE_CONTEXT_SWITCH_TRACE 1 #else #define MICROPROFILE_CONTEXT_SWITCH_TRACE 0 @@ -452,6 +575,14 @@ struct MicroProfileScopeGpuHandler #define MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE (1) #endif +#ifndef MICROPROFILE_MINIZ +#define MICROPROFILE_MINIZ 0 +#endif + +#ifndef MICROPROFILE_COUNTER_HISTORY +#define MICROPROFILE_COUNTER_HISTORY 1 +#endif + #ifdef _WIN32 #include typedef UINT_PTR MpSocket; @@ -459,30 +590,53 @@ typedef UINT_PTR MpSocket; typedef int MpSocket; #endif - +typedef std::thread* MicroProfileThread; enum MicroProfileDrawMask { MP_DRAW_OFF = 0x0, MP_DRAW_BARS = 0x1, MP_DRAW_DETAILED = 0x2, - MP_DRAW_HIDDEN = 0x3, + MP_DRAW_COUNTERS = 0x3, + MP_DRAW_FRAME = 0x4, + MP_DRAW_HIDDEN = 0x5, + MP_DRAW_SIZE = 0x6, }; -enum MicroProfileDrawBarsMask : uint32_t +enum MicroProfileDrawBarsMask { - MP_DRAW_TIMERS = 0x1, - MP_DRAW_AVERAGE = 0x2, - MP_DRAW_MAX = 0x4, - MP_DRAW_CALL_COUNT = 0x8, - MP_DRAW_TIMERS_EXCLUSIVE = 0x10, - MP_DRAW_AVERAGE_EXCLUSIVE = 0x20, - MP_DRAW_MAX_EXCLUSIVE = 0x40, - MP_DRAW_META_FIRST = 0x80, + MP_DRAW_TIMERS = 0x1, + MP_DRAW_AVERAGE = 0x2, + MP_DRAW_MAX = 0x4, + MP_DRAW_MIN = 0x8, + MP_DRAW_CALL_COUNT = 0x10, + MP_DRAW_TIMERS_EXCLUSIVE = 0x20, + MP_DRAW_AVERAGE_EXCLUSIVE = 0x40, + MP_DRAW_MAX_EXCLUSIVE = 0x80, + MP_DRAW_META_FIRST = 0x100, MP_DRAW_ALL = 0xffffffff, }; +enum MicroProfileCounterFormat +{ + MICROPROFILE_COUNTER_FORMAT_DEFAULT, + MICROPROFILE_COUNTER_FORMAT_BYTES, +}; + +enum MicroProfileCounterFlags +{ + MICROPROFILE_COUNTER_FLAG_NONE = 0, + MICROPROFILE_COUNTER_FLAG_DETAILED = 0x1, + MICROPROFILE_COUNTER_FLAG_DETAILED_GRAPH= 0x2, + //internal: + MICROPROFILE_COUNTER_FLAG_INTERNAL_MASK = ~0x3, + MICROPROFILE_COUNTER_FLAG_HAS_LIMIT = 0x4, + MICROPROFILE_COUNTER_FLAG_CLOSED = 0x8, + MICROPROFILE_COUNTER_FLAG_MANUAL_SWAP = 0x10, + MICROPROFILE_COUNTER_FLAG_LEAF = 0x20, +}; + typedef uint64_t MicroProfileLogEntry; struct MicroProfileTimer @@ -491,6 +645,12 @@ struct MicroProfileTimer uint32_t nCount; }; +struct MicroProfileCategory +{ + char pName[MICROPROFILE_NAME_MAX_LEN]; + uint64_t nGroupMask; +}; + struct MicroProfileGroupInfo { char pName[MICROPROFILE_NAME_MAX_LEN]; @@ -498,6 +658,8 @@ struct MicroProfileGroupInfo uint32_t nGroupIndex; uint32_t nNumTimers; uint32_t nMaxTimerNameLen; + uint32_t nColor; + uint32_t nCategory; MicroProfileTokenType Type; }; @@ -512,6 +674,25 @@ struct MicroProfileTimerInfo bool bGraph; }; +struct MicroProfileCounterInfo +{ + int nParent; + int nSibling; + int nFirstChild; + uint16_t nNameLen; + uint8_t nLevel; + char* pName; + uint32_t nFlags; + int64_t nLimit; + MicroProfileCounterFormat eFormat; +}; + +struct MicroProfileCounterHistory +{ + uint32_t nPut; + uint64_t nHistory[MICROPROFILE_GRAPH_HISTORY]; +}; + struct MicroProfileGraphState { int64_t nHistory[MICROPROFILE_GRAPH_HISTORY]; @@ -521,8 +702,9 @@ struct MicroProfileGraphState struct MicroProfileContextSwitch { - ThreadIdType nThreadOut; - ThreadIdType nThreadIn; + MicroProfileThreadIdType nThreadOut; + MicroProfileThreadIdType nThreadIn; + MicroProfileProcessIdType nProcessIn; int64_t nCpu : 8; int64_t nTicks : 56; }; @@ -532,43 +714,66 @@ struct MicroProfileFrameState { int64_t nFrameStartCpu; int64_t nFrameStartGpu; + uint32_t nFrameStartGpuTimer; uint32_t nLogStart[MICROPROFILE_MAX_THREADS]; }; struct MicroProfileThreadLog { - MicroProfileLogEntry Log[MICROPROFILE_BUFFER_SIZE]; - + MicroProfileLogEntry* Log; std::atomic nPut; std::atomic nGet; + + MicroProfileLogEntry* LogGpu; + std::atomic nPutGpu; + uint32_t nStartGpu; + uint32_t bActiveGpu; + void* pContextGpu; + uint32_t nActive; uint32_t nGpu; - ThreadIdType nThreadId; + MicroProfileThreadIdType nThreadId; + uint32_t nLogIndex; uint32_t nStack[MICROPROFILE_STACK_MAX]; int64_t nChildTickStack[MICROPROFILE_STACK_MAX]; uint32_t nStackPos; + + uint8_t nGroupStackPos[MICROPROFILE_MAX_GROUPS]; + int64_t nGroupTicks[MICROPROFILE_MAX_GROUPS]; + int64_t nAggregateGroupTicks[MICROPROFILE_MAX_GROUPS]; enum { THREAD_MAX_LEN = 64, }; char ThreadName[64]; + int nFreeListNext; }; +struct MicroProfileGpu +{ + void (*Shutdown)(); + uint32_t (*Flip)(); + uint32_t (*InsertTimer)(void* pContext); + uint64_t (*GetTimeStamp)(uint32_t nIndex); + uint64_t (*GetTicksPerSecond)(); + bool (*GetTickReference)(int64_t* pOutCpu, int64_t* pOutGpu); +}; struct MicroProfile { uint32_t nTotalTimers; uint32_t nGroupCount; + uint32_t nCategoryCount; uint32_t nAggregateClear; uint32_t nAggregateFlip; uint32_t nAggregateFlipCount; uint32_t nAggregateFrames; uint64_t nAggregateFlipTick; - + uint32_t nDisplay; uint32_t nBars; uint64_t nActiveGroup; @@ -578,6 +783,7 @@ struct MicroProfile uint32_t nForceEnable; uint32_t nForceMetaCounters; + uint64_t nForceGroupUI; uint64_t nActiveGroupWanted; uint32_t nAllGroupsWanted; uint32_t nAllThreadsWanted; @@ -585,36 +791,66 @@ struct MicroProfile uint32_t nOverflow; uint64_t nGroupMask; + uint64_t nGroupMaskGpu; uint32_t nRunning; uint32_t nToggleRunning; uint32_t nMaxGroupSize; - uint32_t nDumpHtmlNextFrame; - char HtmlDumpPath[512]; + uint32_t nDumpFileNextFrame; + uint32_t nAutoClearFrames; + MicroProfileDumpType eDumpType; + uint32_t nDumpFrames; + char DumpPath[512]; int64_t nPauseTicks; float fReferenceTime; float fRcpReferenceTime; + MicroProfileCategory CategoryInfo[MICROPROFILE_MAX_CATEGORIES]; MicroProfileGroupInfo GroupInfo[MICROPROFILE_MAX_GROUPS]; MicroProfileTimerInfo TimerInfo[MICROPROFILE_MAX_TIMERS]; - - MicroProfileTimer AggregateTimers[MICROPROFILE_MAX_TIMERS]; - uint64_t MaxTimers[MICROPROFILE_MAX_TIMERS]; - uint64_t AggregateTimersExclusive[MICROPROFILE_MAX_TIMERS]; - uint64_t MaxTimersExclusive[MICROPROFILE_MAX_TIMERS]; + uint8_t TimerToGroup[MICROPROFILE_MAX_TIMERS]; + + MicroProfileTimer AccumTimers[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumMaxTimers[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumMinTimers[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumTimersExclusive[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumMaxTimersExclusive[MICROPROFILE_MAX_TIMERS]; MicroProfileTimer Frame[MICROPROFILE_MAX_TIMERS]; uint64_t FrameExclusive[MICROPROFILE_MAX_TIMERS]; MicroProfileTimer Aggregate[MICROPROFILE_MAX_TIMERS]; - uint64_t AggregateMax[MICROPROFILE_MAX_TIMERS]; + uint64_t AggregateMax[MICROPROFILE_MAX_TIMERS]; + uint64_t AggregateMin[MICROPROFILE_MAX_TIMERS]; uint64_t AggregateExclusive[MICROPROFILE_MAX_TIMERS]; uint64_t AggregateMaxExclusive[MICROPROFILE_MAX_TIMERS]; - struct + + uint64_t FrameGroup[MICROPROFILE_MAX_GROUPS]; + uint64_t AccumGroup[MICROPROFILE_MAX_GROUPS]; + uint64_t AccumGroupMax[MICROPROFILE_MAX_GROUPS]; + + uint64_t AggregateGroup[MICROPROFILE_MAX_GROUPS]; + uint64_t AggregateGroupMax[MICROPROFILE_MAX_GROUPS]; + + + struct { uint64_t nCounters[MICROPROFILE_MAX_TIMERS]; + + uint64_t nAccum[MICROPROFILE_MAX_TIMERS]; + uint64_t nAccumMax[MICROPROFILE_MAX_TIMERS]; + + uint64_t nAggregate[MICROPROFILE_MAX_TIMERS]; + uint64_t nAggregateMax[MICROPROFILE_MAX_TIMERS]; + + uint64_t nSum; + uint64_t nSumAccum; + uint64_t nSumAccumMax; + uint64_t nSumAggregate; + uint64_t nSumAggregateMax; + const char* pName; } MetaCounters[MICROPROFILE_META_MAX]; @@ -640,8 +876,9 @@ struct MicroProfile uint64_t nFlipAggregateDisplay; uint64_t nFlipMaxDisplay; - std::thread* pContextSwitchThread; + MicroProfileThread ContextSwitchThread; bool bContextSwitchRunning; + bool bContextSwitchStart; bool bContextSwitchStop; bool bContextSwitchAllThreads; bool bContextSwitchNoBars; @@ -656,49 +893,68 @@ struct MicroProfile uint8_t nContextSwitchHoverCpu; uint8_t nContextSwitchHoverCpuNext; - uint32_t nContextSwitchPut; + uint32_t nContextSwitchPut; MicroProfileContextSwitch ContextSwitch[MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE]; + MicroProfileThread WebServerThread; - MpSocket ListenerSocket; + MpSocket WebServerSocket; uint32_t nWebServerPort; + char WebServerBuffer[MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE]; + uint32_t nWebServerPut; + uint64_t nWebServerDataSent; + + std::atomic LabelBuffer; + std::atomic nLabelPut; + + char CounterNames[MICROPROFILE_MAX_COUNTER_NAME_CHARS]; + MicroProfileCounterInfo CounterInfo[MICROPROFILE_MAX_COUNTERS]; + uint32_t nNumCounters; + uint32_t nCounterNamePos; + std::atomic Counters[MICROPROFILE_MAX_COUNTERS]; + +#if MICROPROFILE_COUNTER_HISTORY // uses 1kb per allocated counter. 512kb for default counter count + uint32_t nCounterHistoryPut; + int64_t nCounterHistory[MICROPROFILE_GRAPH_HISTORY][MICROPROFILE_MAX_COUNTERS]; //flipped to make swapping cheap, drawing more expensive. + int64_t nCounterMax[MICROPROFILE_MAX_COUNTERS]; + int64_t nCounterMin[MICROPROFILE_MAX_COUNTERS]; +#endif + + uint32_t nGpuFrameTimer; + MicroProfileGpu GPU; }; #define MP_LOG_TICK_MASK 0x0000ffffffffffff -#define MP_LOG_INDEX_MASK 0x3fff000000000000 -#define MP_LOG_BEGIN_MASK 0xc000000000000000 +#define MP_LOG_INDEX_MASK 0x1fff000000000000 +#define MP_LOG_BEGIN_MASK 0xe000000000000000 +#define MP_LOG_GPU_EXTRA 0x4 +#define MP_LOG_LABEL 0x3 #define MP_LOG_META 0x2 #define MP_LOG_ENTER 0x1 #define MP_LOG_LEAVE 0x0 -inline int MicroProfileLogType(MicroProfileLogEntry Index) +inline uint64_t MicroProfileLogType(MicroProfileLogEntry Index) { - return ((MP_LOG_BEGIN_MASK & Index)>>62) & 0x3; + return ((MP_LOG_BEGIN_MASK & Index)>>61) & 0x7; } inline uint64_t MicroProfileLogTimerIndex(MicroProfileLogEntry Index) { - return (0x3fff&(Index>>48)); + return (MP_LOG_INDEX_MASK & Index)>>48; } inline MicroProfileLogEntry MicroProfileMakeLogIndex(uint64_t nBegin, MicroProfileToken nToken, int64_t nTick) { - MicroProfileLogEntry Entry = (nBegin<<62) | ((0x3fff&nToken)<<48) | (MP_LOG_TICK_MASK&nTick); - int t = MicroProfileLogType(Entry); - uint64_t nTimerIndex = MicroProfileLogTimerIndex(Entry); - MP_ASSERT(t == nBegin); - MP_ASSERT(nTimerIndex == (nToken&0x3fff)); - return Entry; - -} + return (nBegin<<61) | (MP_LOG_INDEX_MASK&(nToken<<48)) | (MP_LOG_TICK_MASK&nTick); +} inline int64_t MicroProfileLogTickDifference(MicroProfileLogEntry Start, MicroProfileLogEntry End) { uint64_t nStart = Start; uint64_t nEnd = End; - int64_t nDifference = ((nEnd<<16) - (nStart<<16)); + int64_t nDifference = ((nEnd<<16) - (nStart<<16)); return nDifference >> 16; } @@ -719,6 +975,9 @@ T MicroProfileMin(T a, T b) template T MicroProfileMax(T a, T b) { return a > b ? a : b; } +template +T MicroProfileClamp(T a, T min_, T max_) +{ return MicroProfileMin(max_, MicroProfileMax(min_, a)); } inline int64_t MicroProfileMsToTick(float fMs, int64_t nTicksPerSecond) { @@ -732,7 +991,7 @@ inline float MicroProfileTickToMsMultiplier(int64_t nTicksPerSecond) inline uint16_t MicroProfileGetGroupIndex(MicroProfileToken t) { - return (uint16_t)MicroProfileGet()->TimerInfo[MicroProfileGetTimerIndex(t)].nGroupIndex; + return (uint16_t)MicroProfileGet()->TimerToGroup[MicroProfileGetTimerIndex(t)]; } @@ -740,6 +999,7 @@ inline uint16_t MicroProfileGetGroupIndex(MicroProfileToken t) #ifdef MICROPROFILE_IMPL #ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN #include #define snprintf _snprintf @@ -747,8 +1007,8 @@ inline uint16_t MicroProfileGetGroupIndex(MicroProfileToken t) #pragma warning(disable: 4244) int64_t MicroProfileTicksPerSecondCpu() { - static int64_t nTicksPerSecond = 0; - if(nTicksPerSecond == 0) + static int64_t nTicksPerSecond = 0; + if(nTicksPerSecond == 0) { QueryPerformanceFrequency((LARGE_INTEGER*)&nTicksPerSecond); } @@ -763,35 +1023,46 @@ int64_t MicroProfileGetTick() #endif +#if MICROPROFILE_WEBSERVER || MICROPROFILE_CONTEXT_SWITCH_TRACE +typedef void* (*MicroProfileThreadFunc)(void*); + +inline void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func) +{ + *pThread = new std::thread(Func, nullptr); +} +inline void MicroProfileThreadJoin(MicroProfileThread* pThread) +{ + (*pThread)->join(); + delete *pThread; + *pThread = nullptr; +} +#endif + #if MICROPROFILE_WEBSERVER #ifdef _WIN32 +#if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_) +#error WinSock.h has already been included; microprofile requires WinSock2 +#endif +#include +#pragma comment(lib, "ws2_32.lib") #define MP_INVALID_SOCKET(f) (f == INVALID_SOCKET) #endif -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(__linux__) #include #include +#include +#include #include #define MP_INVALID_SOCKET(f) (f < 0) #endif -void MicroProfileWebServerStart(); -void MicroProfileWebServerStop(); -bool MicroProfileWebServerUpdate(); -void MicroProfileDumpHtmlToFile(); - -#else - -#define MicroProfileWebServerStart() do{}while(0) -#define MicroProfileWebServerStop() do{}while(0) -#define MicroProfileWebServerUpdate() false -#define MicroProfileDumpHtmlToFile() do{} while(0) -#endif - +#endif #include #include +#include #include #include @@ -805,8 +1076,8 @@ void MicroProfileDumpHtmlToFile(); MicroProfile g_MicroProfile; MicroProfileThreadLog* g_MicroProfileGpuLog = 0; -#ifdef MICROPROFILE_IOS -// iOS doesn't support __thread + +#ifndef MP_THREAD_LOCAL static pthread_key_t g_MicroProfileThreadLogKey; static pthread_once_t g_MicroProfileThreadLogKeyOnce = PTHREAD_ONCE_INIT; static void MicroProfileCreateThreadLogKey() @@ -816,6 +1087,7 @@ static void MicroProfileCreateThreadLogKey() #else MP_THREAD_LOCAL MicroProfileThreadLog* g_MicroProfileThreadLog = 0; #endif + static bool g_bUseLock = false; /// This is used because windows does not support using mutexes under dll init(which is where global initialization is handled) @@ -823,7 +1095,8 @@ MICROPROFILE_DEFINE(g_MicroProfileFlip, "MicroProfile", "MicroProfileFlip", 0x33 MICROPROFILE_DEFINE(g_MicroProfileThreadLoop, "MicroProfile", "ThreadLoop", 0x3355ee); MICROPROFILE_DEFINE(g_MicroProfileClear, "MicroProfile", "Clear", 0x3355ee); MICROPROFILE_DEFINE(g_MicroProfileAccumulate, "MicroProfile", "Accumulate", 0x3355ee); - +MICROPROFILE_DEFINE(g_MicroProfileContextSwitchSearch,"MicroProfile", "ContextSwitchSearch", 0xDD7300); +MICROPROFILE_DEFINE(g_MicroProfileWebServerUpdate,"MicroProfile", "WebServerUpdate", 0xDD7300); inline std::recursive_mutex& MicroProfileMutex() { @@ -853,26 +1126,34 @@ void MicroProfileInit() static bool bOnce = true; if(bOnce) { - S.nMemUsage += sizeof(S); bOnce = false; memset(&S, 0, sizeof(S)); + S.nMemUsage = sizeof(S); for(int i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) { S.GroupInfo[i].pName[0] = '\0'; } + for(int i = 0; i < MICROPROFILE_MAX_CATEGORIES; ++i) + { + S.CategoryInfo[i].pName[0] = '\0'; + S.CategoryInfo[i].nGroupMask = 0; + } + strcpy(&S.CategoryInfo[0].pName[0], "default"); + S.nCategoryCount = 1; for(int i = 0; i < MICROPROFILE_MAX_TIMERS; ++i) { S.TimerInfo[i].pName[0] = '\0'; } S.nGroupCount = 0; S.nAggregateFlipTick = MP_TICK(); + S.nBars = MP_DRAW_AVERAGE | MP_DRAW_MAX | MP_DRAW_CALL_COUNT; S.nActiveGroup = 0; S.nActiveBars = 0; S.nForceGroup = 0; S.nAllGroupsWanted = 0; S.nActiveGroupWanted = 0; S.nAllThreadsWanted = 1; - S.nAggregateFlip = 0; + S.nAggregateFlip = 60; S.nTotalTimers = 0; for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) { @@ -886,17 +1167,26 @@ void MicroProfileInit() for(int i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY; ++i) { S.Frames[i].nFrameStartCpu = nTick; - S.Frames[i].nFrameStartGpu = -1; + S.Frames[i].nFrameStartGpu = 0; + S.Frames[i].nFrameStartGpuTimer = (uint32_t)-1; } +#if MICROPROFILE_COUNTER_HISTORY + S.nCounterHistoryPut = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_COUNTERS; ++i) + { + S.nCounterMin[i] = 0x7fffffffffffffff; + S.nCounterMax[i] = 0x8000000000000000; + } +#endif + + S.nGpuFrameTimer = (uint32_t)-1; + MicroProfileThreadLog* pGpu = MicroProfileCreateThreadLog("GPU"); g_MicroProfileGpuLog = pGpu; MP_ASSERT(S.Pool[0] == pGpu); pGpu->nGpu = 1; pGpu->nThreadId = 0; - - - MicroProfileWebServerStart(); } if(bUseLock) mutex.unlock(); @@ -906,23 +1196,11 @@ void MicroProfileShutdown() { std::lock_guard Lock(MicroProfileMutex()); MicroProfileWebServerStop(); - -#if MICROPROFILE_CONTEXT_SWITCH_TRACE - if(S.pContextSwitchThread) - { - if(S.pContextSwitchThread->joinable()) - { - S.bContextSwitchStop = true; - S.pContextSwitchThread->join(); - } - delete S.pContextSwitchThread; - } -#endif - - + MicroProfileContextSwitchTraceStop(); + MicroProfileGpuShutdown(); } -#ifdef MICROPROFILE_IOS +#ifndef MP_THREAD_LOCAL inline MicroProfileThreadLog* MicroProfileGetThreadLog() { pthread_once(&g_MicroProfileThreadLogKeyOnce, MicroProfileCreateThreadLogKey); @@ -949,20 +1227,28 @@ inline void MicroProfileSetThreadLog(MicroProfileThreadLog* pLog) MicroProfileThreadLog* MicroProfileCreateThreadLog(const char* pName) { MicroProfileThreadLog* pLog = 0; + uint32_t nLogIndex = 0; if(S.nFreeListHead != -1) { - pLog = S.Pool[S.nFreeListHead]; + nLogIndex = S.nFreeListHead; + pLog = S.Pool[nLogIndex]; MP_ASSERT(pLog->nPut.load() == 0); MP_ASSERT(pLog->nGet.load() == 0); S.nFreeListHead = S.Pool[S.nFreeListHead]->nFreeListNext; } + else if(S.nNumLogs == MICROPROFILE_MAX_THREADS) + { + return nullptr; + } else { + nLogIndex = S.nNumLogs; pLog = new MicroProfileThreadLog; S.nMemUsage += sizeof(MicroProfileThreadLog); - S.Pool[S.nNumLogs++] = pLog; + S.Pool[S.nNumLogs++] = pLog; } memset(pLog, 0, sizeof(*pLog)); + pLog->nLogIndex = nLogIndex; int len = (int)strlen(pName); int maxlen = sizeof(pLog->ThreadName)-1; len = len < maxlen ? len : maxlen; @@ -979,10 +1265,12 @@ void MicroProfileOnThreadCreate(const char* pThreadName) g_bUseLock = true; MicroProfileInit(); std::lock_guard Lock(MicroProfileMutex()); - MP_ASSERT(MicroProfileGetThreadLog() == 0); - MicroProfileThreadLog* pLog = MicroProfileCreateThreadLog(pThreadName ? pThreadName : MicroProfileGetThreadName()); - MP_ASSERT(pLog); - MicroProfileSetThreadLog(pLog); + if(MicroProfileGetThreadLog() == 0) + { + MicroProfileThreadLog* pLog = MicroProfileCreateThreadLog(pThreadName ? pThreadName : MicroProfileGetThreadName()); + MP_ASSERT(pLog); + MicroProfileSetThreadLog(pLog); + } } void MicroProfileOnThreadExit() @@ -1005,19 +1293,45 @@ void MicroProfileOnThreadExit() pLog->nActive = 0; pLog->nPut.store(0); pLog->nGet.store(0); + pLog->nPutGpu.store(0); S.nFreeListHead = nLogIndex; for(int i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY; ++i) { S.Frames[i].nLogStart[nLogIndex] = 0; } + memset(pLog->nGroupStackPos, 0, sizeof(pLog->nGroupStackPos)); + memset(pLog->nGroupTicks, 0, sizeof(pLog->nGroupTicks)); + + if(pLog->Log) + { + delete[] pLog->Log; + pLog->Log = 0; + S.nMemUsage -= sizeof(MicroProfileLogEntry) * MICROPROFILE_BUFFER_SIZE; + } + + if(pLog->LogGpu) + { + delete[] pLog->LogGpu; + pLog->LogGpu = 0; + S.nMemUsage -= sizeof(MicroProfileLogEntry) * MICROPROFILE_GPU_BUFFER_SIZE; + } + + MicroProfileSetThreadLog(0); } } -void MicroProfileInitThreadLog() +MicroProfileThreadLog* MicroProfileGetOrCreateThreadLog() { - MicroProfileOnThreadCreate(nullptr); -} + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + if (!pLog) + { + MicroProfileOnThreadCreate(nullptr); + pLog = MicroProfileGetThreadLog(); + } + + return pLog; +} struct MicroProfileScopeLock { @@ -1041,7 +1355,7 @@ MicroProfileToken MicroProfileFindToken(const char* pGroup, const char* pName) MicroProfileScopeLock L(MicroProfileMutex()); for(uint32_t i = 0; i < S.nTotalTimers; ++i) { - if(!MP_STRCASECMP(pName, S.TimerInfo[i].pName) && !MP_STRCASECMP(pGroup, S.GroupInfo[S.TimerInfo[i].nGroupIndex].pName)) + if(!MP_STRCASECMP(pName, S.TimerInfo[i].pName) && !MP_STRCASECMP(pGroup, S.GroupInfo[S.TimerToGroup[i]].pName)) { return S.TimerInfo[i].nToken; } @@ -1058,24 +1372,34 @@ uint16_t MicroProfileGetGroup(const char* pGroup, MicroProfileTokenType Type) return i; } } - uint16_t nGroupIndex = 0xffff; - uint32_t nLen = (uint32_t)strlen(pGroup); + + uint16_t nGroupIndex = S.nGroupCount++; + MP_ASSERT(nGroupIndex < MICROPROFILE_MAX_GROUPS); + + size_t nLen = strlen(pGroup); if(nLen > MICROPROFILE_NAME_MAX_LEN-1) nLen = MICROPROFILE_NAME_MAX_LEN-1; - memcpy(&S.GroupInfo[S.nGroupCount].pName[0], pGroup, nLen); - S.GroupInfo[S.nGroupCount].pName[nLen] = '\0'; - S.GroupInfo[S.nGroupCount].nNameLen = nLen; - S.GroupInfo[S.nGroupCount].nGroupIndex = S.nGroupCount; - S.GroupInfo[S.nGroupCount].nNumTimers = 0; - S.GroupInfo[S.nGroupCount].Type = Type; - S.GroupInfo[S.nGroupCount].nMaxTimerNameLen = 0; - nGroupIndex = S.nGroupCount++; - S.nGroupMask = (S.nGroupMask<<1)|1; - MP_ASSERT(nGroupIndex < MICROPROFILE_MAX_GROUPS); + memcpy(&S.GroupInfo[nGroupIndex].pName[0], pGroup, nLen); + S.GroupInfo[nGroupIndex].pName[nLen] = '\0'; + S.GroupInfo[nGroupIndex].nNameLen = (uint32_t)nLen; + + S.GroupInfo[nGroupIndex].nNumTimers = 0; + S.GroupInfo[nGroupIndex].nGroupIndex = nGroupIndex; + S.GroupInfo[nGroupIndex].Type = Type; + S.GroupInfo[nGroupIndex].nMaxTimerNameLen = 0; + S.GroupInfo[nGroupIndex].nColor = 0x88888888; + S.GroupInfo[nGroupIndex].nCategory = 0; + + S.CategoryInfo[0].nGroupMask |= 1ll << nGroupIndex; + S.nGroupMask |= 1ll << nGroupIndex; + S.nGroupMaskGpu |= uint64_t(Type == MicroProfileTokenTypeGpu) << nGroupIndex; + + if ((S.nRunning || S.nForceEnable) && S.nAllGroupsWanted) + S.nActiveGroup |= 1ll << nGroupIndex; + return nGroupIndex; } - MicroProfileToken MicroProfileGetToken(const char* pGroup, const char* pName, uint32_t nColor, MicroProfileTokenType Type) { MicroProfileInit(); @@ -1083,6 +1407,8 @@ MicroProfileToken MicroProfileGetToken(const char* pGroup, const char* pName, ui MicroProfileToken ret = MicroProfileFindToken(pGroup, pName); if(ret != MICROPROFILE_INVALID_TOKEN) return ret; + if(S.nTotalTimers == MICROPROFILE_MAX_TIMERS) + return MICROPROFILE_INVALID_TOKEN; uint16_t nGroupIndex = MicroProfileGetGroup(pGroup, Type); uint16_t nTimerIndex = (uint16_t)(S.nTotalTimers++); uint64_t nGroupMask = 1ll << nGroupIndex; @@ -1096,11 +1422,48 @@ MicroProfileToken MicroProfileGetToken(const char* pGroup, const char* pName, ui if(nLen > MICROPROFILE_NAME_MAX_LEN-1) nLen = MICROPROFILE_NAME_MAX_LEN-1; memcpy(&S.TimerInfo[nTimerIndex].pName, pName, nLen); + + if(nColor == 0xffffffff) + { + // http://www.two4u.com/color/small-txt.html with some omissions + static const int kDebugColors[] = + { + 0x70DB93, 0xB5A642, 0x5F9F9F, 0xB87333, 0x4F6F4F, 0x9932CD, + 0x871F78, 0x855E42, 0x545454, 0x8E2323, 0x238E23, 0xCD7F32, + 0xDBDB70, 0x527F76, 0x9F9F5F, 0x8E236B, 0xFF2F4F, 0xCFB53B, + 0xFF7F00, 0xDB70DB, 0x5959AB, 0x8C1717, 0x238E68, 0x6B4226, + 0x8E6B23, 0x007FFF, 0x00FF7F, 0x236B8E, 0x38B0DE, 0xDB9370, + 0xCC3299, 0x99CC32, + }; + + // djb2 + unsigned int result = 5381; + for (const char* i = pGroup; *i; ++i) + result = result * 33 ^ *i; + for (const char* i = pName; *i; ++i) + result = result * 33 ^ *i; + + nColor = kDebugColors[result % (sizeof(kDebugColors) / sizeof(kDebugColors[0]))]; + } + S.TimerInfo[nTimerIndex].pName[nLen] = '\0'; S.TimerInfo[nTimerIndex].nNameLen = nLen; S.TimerInfo[nTimerIndex].nColor = nColor&0xffffff; S.TimerInfo[nTimerIndex].nGroupIndex = nGroupIndex; S.TimerInfo[nTimerIndex].nTimerIndex = nTimerIndex; + S.TimerToGroup[nTimerIndex] = nGroupIndex; + return nToken; +} + +MicroProfileToken MicroProfileGetLabelToken(const char* pGroup, MicroProfileTokenType Type) +{ + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + + uint16_t nGroupIndex = MicroProfileGetGroup(pGroup, Type); + uint64_t nGroupMask = 1ll << nGroupIndex; + MicroProfileToken nToken = MicroProfileMakeToken(nGroupMask, 0); + return nToken; } @@ -1124,6 +1487,126 @@ MicroProfileToken MicroProfileGetMetaToken(const char* pName) return (MicroProfileToken)-1; } +const char* MicroProfileNextName(const char* pName, char* pNameOut, uint32_t* nSubNameLen) +{ + uint32_t nMaxLen = MICROPROFILE_NAME_MAX_LEN-1; + const char* pRet = 0; + bool bDone = false; + uint32_t nChars = 0; + for(uint32_t i = 0; i < nMaxLen && !bDone; ++i) + { + char c = *pName++; + switch(c) + { + case 0: + bDone = true; + break; + case '\\': + case '/': + if(nChars) + { + bDone = true; + pRet = pName; + } + break; + default: + nChars++; + *pNameOut++ = c; + } + } + *nSubNameLen = nChars; + *pNameOut = '\0'; + return pRet; +} + + +const char* MicroProfileCounterFullName(int nCounter) +{ + static char Buffer[1024]; + int nNodes[32]; + int nIndex = 0; + do + { + nNodes[nIndex++] = nCounter; + nCounter = S.CounterInfo[nCounter].nParent; + }while(nCounter >= 0); + int nOffset = 0; + while(nIndex >= 0 && nOffset < (int)sizeof(Buffer)-2) + { + uint32_t nLen = S.CounterInfo[nNodes[nIndex]].nNameLen + nOffset;// < sizeof(Buffer)-1 + nLen = MicroProfileMin((uint32_t)(sizeof(Buffer) - 2 - nOffset), nLen); + memcpy(&Buffer[nOffset], S.CounterInfo[nNodes[nIndex]].pName, nLen); + + nOffset += S.CounterInfo[nNodes[nIndex]].nNameLen+1; + if(nIndex) + { + Buffer[nOffset++] = '/'; + } + nIndex--; + } + return &Buffer[0]; +} + +int MicroProfileGetCounterTokenByParent(int nParent, const char* pName) +{ + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + if(nParent == S.CounterInfo[i].nParent && !MP_STRCASECMP(S.CounterInfo[i].pName, pName)) + { + return i; + } + } + MicroProfileToken nResult = S.nNumCounters++; + S.CounterInfo[nResult].nParent = nParent; + S.CounterInfo[nResult].nSibling = -1; + S.CounterInfo[nResult].nFirstChild = -1; + S.CounterInfo[nResult].nFlags = 0; + S.CounterInfo[nResult].eFormat = MICROPROFILE_COUNTER_FORMAT_DEFAULT; + S.CounterInfo[nResult].nLimit = 0; + int nLen = (int)strlen(pName)+1; + + MP_ASSERT(nLen + S.nCounterNamePos <= MICROPROFILE_MAX_COUNTER_NAME_CHARS); + uint32_t nPos = S.nCounterNamePos; + S.nCounterNamePos += nLen; + memcpy(&S.CounterNames[nPos], pName, nLen); + S.CounterInfo[nResult].nNameLen = nLen-1; + S.CounterInfo[nResult].pName = &S.CounterNames[nPos]; + if(nParent >= 0) + { + S.CounterInfo[nResult].nSibling = S.CounterInfo[nParent].nFirstChild; + S.CounterInfo[nResult].nLevel = S.CounterInfo[nParent].nLevel + 1; + S.CounterInfo[nParent].nFirstChild = nResult; + } + else + { + S.CounterInfo[nResult].nLevel = 0; + } + + return nResult; +} + +MicroProfileToken MicroProfileGetCounterToken(const char* pName) +{ + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + char SubName[MICROPROFILE_NAME_MAX_LEN]; + int nResult = -1; + do + { + uint32_t nLen = 0; + pName = MicroProfileNextName(pName, &SubName[0], &nLen); + if(0 == nLen) + { + break; + } + nResult = MicroProfileGetCounterTokenByParent(nResult, SubName); + + }while(pName != 0); + S.CounterInfo[nResult].nFlags |= MICROPROFILE_COUNTER_FLAG_LEAF; + + MP_ASSERT(nResult >= 0); + return nResult; +} inline void MicroProfileLogPut(MicroProfileToken nToken_, uint64_t nTick, uint64_t nBegin, MicroProfileThreadLog* pLog) { @@ -1137,75 +1620,221 @@ inline void MicroProfileLogPut(MicroProfileToken nToken_, uint64_t nTick, uint64 } else { - int64_t test = MicroProfileMakeLogIndex(nBegin, nToken_, nTick);; - MP_ASSERT(MicroProfileLogType(test) == nBegin); - MP_ASSERT(MicroProfileLogTimerIndex(test) == MicroProfileGetTimerIndex(nToken_)); + if(!pLog->Log) + { + pLog->Log = new MicroProfileLogEntry[MICROPROFILE_BUFFER_SIZE]; + memset(pLog->Log, 0, sizeof(MicroProfileLogEntry) * MICROPROFILE_BUFFER_SIZE); + S.nMemUsage += sizeof(MicroProfileLogEntry) * MICROPROFILE_BUFFER_SIZE; + } pLog->Log[nPos] = MicroProfileMakeLogIndex(nBegin, nToken_, nTick); pLog->nPut.store(nNextPos, std::memory_order_release); } } +inline void MicroProfileLogPutGpu(MicroProfileToken nToken_, uint64_t nTick, uint64_t nBegin, MicroProfileThreadLog* pLog) +{ +#if MICROPROFILE_GPU_TIMERS_MULTITHREADED + MP_ASSERT(pLog != 0); //this assert is hit if MicroProfileOnCreateThread is not called + MP_ASSERT(pLog->nActive); + uint32_t nPos = pLog->nPutGpu.load(std::memory_order_relaxed); + if(nPos >= MICROPROFILE_GPU_BUFFER_SIZE) + { + S.nOverflow = 100; + } + else + { + if(!pLog->LogGpu) + { + pLog->LogGpu = new MicroProfileLogEntry[MICROPROFILE_GPU_BUFFER_SIZE]; + memset(pLog->LogGpu, 0, sizeof(MicroProfileLogEntry) * MICROPROFILE_GPU_BUFFER_SIZE); + S.nMemUsage += sizeof(MicroProfileLogEntry) * MICROPROFILE_GPU_BUFFER_SIZE; + } + pLog->LogGpu[nPos] = MicroProfileMakeLogIndex(nBegin, nToken_, nTick); + pLog->nPutGpu.store(nPos + 1, std::memory_order_release); + } +#else + (void)pLog; + + MicroProfileLogPut(nToken_, nTick, nBegin, g_MicroProfileGpuLog); +#endif +} + uint64_t MicroProfileEnter(MicroProfileToken nToken_) +{ + uint64_t nGroupMask = MicroProfileGetGroupMask(nToken_); + if(nGroupMask & S.nActiveGroup) + { + if (MicroProfileThreadLog* pLog = MicroProfileGetOrCreateThreadLog()) + { + if (nGroupMask & S.nGroupMaskGpu) + { + uint32_t nTimer = MicroProfileGpuInsertTimer(pLog->pContextGpu); + if (nTimer != (uint32_t)-1) + { + MicroProfileLogPutGpu(nToken_, nTimer, MP_LOG_ENTER, pLog); + MicroProfileLogPutGpu(pLog->nLogIndex, MP_TICK(), MP_LOG_GPU_EXTRA, pLog); + return 0; + } + } + else + { + uint64_t nTick = MP_TICK(); + MicroProfileLogPut(nToken_, nTick, MP_LOG_ENTER, pLog); + return nTick; + } + } + } + return MICROPROFILE_INVALID_TICK; +} + +uint64_t MicroProfileAllocateLabel(const char* pName) +{ + char* pLabelBuffer = S.LabelBuffer.load(std::memory_order_consume); + if(!pLabelBuffer) + { + MicroProfileScopeLock L(MicroProfileMutex()); + + pLabelBuffer = S.LabelBuffer.load(std::memory_order_consume); + if(!pLabelBuffer) + { + pLabelBuffer = new char[MICROPROFILE_LABEL_BUFFER_SIZE + MICROPROFILE_LABEL_MAX_LEN]; + memset(pLabelBuffer, 0, MICROPROFILE_LABEL_BUFFER_SIZE + MICROPROFILE_LABEL_MAX_LEN); + S.nMemUsage += MICROPROFILE_LABEL_BUFFER_SIZE + MICROPROFILE_LABEL_MAX_LEN; + + S.LabelBuffer.store(pLabelBuffer, std::memory_order_release); + } + } + + size_t nLen = strlen(pName); + + if(nLen > MICROPROFILE_LABEL_MAX_LEN - 1) + nLen = MICROPROFILE_LABEL_MAX_LEN - 1; + + uint64_t nLabel = S.nLabelPut.fetch_add(nLen + 1, std::memory_order_relaxed); + char* pLabel = &pLabelBuffer[nLabel % MICROPROFILE_LABEL_BUFFER_SIZE]; + + memcpy(pLabel, pName, nLen); + pLabel[nLen] = 0; + + return nLabel; +} + +void MicroProfilePutLabel(MicroProfileToken nToken_, const char* pName) +{ + if (MicroProfileThreadLog* pLog = MicroProfileGetThreadLog()) + { + uint64_t nLabel = MicroProfileAllocateLabel(pName); + uint64_t nGroupMask = MicroProfileGetGroupMask(nToken_); + + if (nGroupMask & S.nGroupMaskGpu) + MicroProfileLogPutGpu(nToken_, nLabel, MP_LOG_LABEL, pLog); + else + MicroProfileLogPut(nToken_, nLabel, MP_LOG_LABEL, pLog); + } +} + +void MicroProfileCounterAdd(MicroProfileToken nToken, int64_t nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + S.Counters[nToken].fetch_add(nCount); +} +void MicroProfileCounterSet(MicroProfileToken nToken, int64_t nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + S.Counters[nToken].store(nCount); +} +void MicroProfileCounterSetLimit(MicroProfileToken nToken, int64_t nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + S.CounterInfo[nToken].nLimit = nCount; +} + +void MicroProfileCounterConfig(const char* pName, uint32_t eFormat, int64_t nLimit, uint32_t nFlags) +{ + MicroProfileToken nToken = MicroProfileGetCounterToken(pName); + S.CounterInfo[nToken].eFormat = (MicroProfileCounterFormat)eFormat; + S.CounterInfo[nToken].nLimit = nLimit; + S.CounterInfo[nToken].nFlags |= (nFlags & ~MICROPROFILE_COUNTER_FLAG_INTERNAL_MASK); +} + +const char* MicroProfileGetLabel(uint64_t nLabel) +{ + char* pLabelBuffer = S.LabelBuffer.load(std::memory_order_relaxed); + uint64_t nLabelPut = S.nLabelPut.load(std::memory_order_relaxed); + + MP_ASSERT(pLabelBuffer && nLabel < nLabelPut); + + if (nLabelPut - nLabel > MICROPROFILE_LABEL_BUFFER_SIZE) + return 0; + else + return &pLabelBuffer[nLabel % MICROPROFILE_LABEL_BUFFER_SIZE]; +} + +void MicroProfileLabel(MicroProfileToken nToken_, const char* pName) { if(MicroProfileGetGroupMask(nToken_) & S.nActiveGroup) { - if(!MicroProfileGetThreadLog()) - { - MicroProfileInitThreadLog(); - } - uint64_t nTick = MP_TICK(); - MicroProfileLogPut(nToken_, nTick, MP_LOG_ENTER, MicroProfileGetThreadLog()); - return nTick; + MicroProfilePutLabel(nToken_, pName); + } +} + +void MicroProfileLabelFormat(MicroProfileToken nToken_, const char* pName, ...) +{ + va_list args; + va_start(args, pName); + MicroProfileLabelFormatV(nToken_, pName, args); + va_end(args); +} + +void MicroProfileLabelFormatV(MicroProfileToken nToken_, const char* pName, va_list args) +{ + if(MicroProfileGetGroupMask(nToken_) & S.nActiveGroup) + { + char buffer[MICROPROFILE_LABEL_MAX_LEN]; + vsnprintf(buffer, sizeof(buffer)-1, pName, args); + + buffer[sizeof(buffer)-1] = 0; + + MicroProfilePutLabel(nToken_, buffer); } - return MICROPROFILE_INVALID_TICK; } void MicroProfileMetaUpdate(MicroProfileToken nToken, int nCount, MicroProfileTokenType eTokenType) { if((MP_DRAW_META_FIRST<pContextGpu); + MicroProfileLogPutGpu(nToken_, nTimer, MP_LOG_LEAVE, pLog); + MicroProfileLogPutGpu(pLog->nLogIndex, MP_TICK(), MP_LOG_GPU_EXTRA, pLog); + } + else + { + uint64_t nTick = MP_TICK(); + MicroProfileLogPut(nToken_, nTick, MP_LOG_LEAVE, pLog); + } } - uint64_t nTick = MP_TICK(); - MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); - MicroProfileLogPut(nToken_, nTick, MP_LOG_LEAVE, pLog); - } -} - - -uint64_t MicroProfileGpuEnter(MicroProfileToken nToken_) -{ - if(MicroProfileGetGroupMask(nToken_) & S.nActiveGroup) - { - uint64_t nTimer = MicroProfileGpuInsertTimeStamp(); - MicroProfileLogPut(nToken_, nTimer, MP_LOG_ENTER, g_MicroProfileGpuLog); - return 1; - } - return 0; -} - -void MicroProfileGpuLeave(MicroProfileToken nToken_, uint64_t nTickStart) -{ - if(nTickStart) - { - uint64_t nTimer = MicroProfileGpuInsertTimeStamp(); - MicroProfileLogPut(nToken_, nTimer, MP_LOG_LEAVE, g_MicroProfileGpuLog); } } @@ -1239,20 +1868,30 @@ void MicroProfileGetRange(uint32_t nPut, uint32_t nGet, uint32_t nRange[2][2]) } } -void MicroProfileFlip() +void MicroProfileDumpToFile(); + +void MicroProfileFlipGpu() { - #if 0 - //verify LogEntry wraps correctly - MicroProfileLogEntry c = MP_LOG_TICK_MASK-5000; - for(int i = 0; i < 10000; ++i, c += 1) - { - MicroProfileLogEntry l2 = (c+2500) & MP_LOG_TICK_MASK; - MP_ASSERT(2500 == MicroProfileLogTickDifference(c, l2)); - } - #endif - MICROPROFILE_SCOPE(g_MicroProfileFlip); std::lock_guard Lock(MicroProfileMutex()); + for (uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if (S.Pool[i]) + { + MP_ASSERT(!S.Pool[i]->bActiveGpu); + + S.Pool[i]->nPutGpu.store(0); + } + } + + S.nGpuFrameTimer = MicroProfileGpuFlip(); +} + +void MicroProfileFlipCpu() +{ + std::lock_guard Lock(MicroProfileMutex()); + + if(S.nToggleRunning) { S.nRunning = !S.nRunning; @@ -1268,18 +1907,22 @@ void MicroProfileFlip() } } } - uint32_t nAggregateClear = S.nAggregateClear, nAggregateFlip = 0; - if(S.nDumpHtmlNextFrame) + uint32_t nAggregateClear = S.nAggregateClear || S.nAutoClearFrames, nAggregateFlip = 0; + if(S.nDumpFileNextFrame) { - S.nDumpHtmlNextFrame = 0; - MicroProfileDumpHtmlToFile(); + MicroProfileDumpToFile(); + S.nDumpFileNextFrame = 0; + S.nAutoClearFrames = MICROPROFILE_GPU_FRAME_DELAY + 3; //hide spike from dumping webpage } - if(MicroProfileWebServerUpdate()) + + if(S.nAutoClearFrames) { nAggregateClear = 1; nAggregateFlip = 1; + S.nAutoClearFrames -= 1; } + if(S.nRunning || S.nForceEnable) { S.nFramePutIndex++; @@ -1303,14 +1946,16 @@ void MicroProfileFlip() MicroProfileFrameState* pFramePut = &S.Frames[S.nFramePut]; MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext]; - + pFramePut->nFrameStartCpu = MP_TICK(); - pFramePut->nFrameStartGpu = (uint32_t)MicroProfileGpuInsertTimeStamp(); - if(pFrameNext->nFrameStartGpu != (uint64_t)-1) - pFrameNext->nFrameStartGpu = MicroProfileGpuGetTimeStamp((uint32_t)pFrameNext->nFrameStartGpu); + pFramePut->nFrameStartGpuTimer = S.nGpuFrameTimer; - if(pFrameCurrent->nFrameStartGpu == (uint64_t)-1) - pFrameCurrent->nFrameStartGpu = pFrameNext->nFrameStartGpu + 1; + if(pFrameCurrent->nFrameStartGpuTimer != (uint32_t)-1) + { + uint64_t nTick = MicroProfileGpuGetTimeStamp(pFrameCurrent->nFrameStartGpuTimer); + + pFrameCurrent->nFrameStartGpu = (nTick == MICROPROFILE_INVALID_TICK) ? 0 : nTick; + } uint64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu; uint64_t nFrameEndCpu = pFrameNext->nFrameStartCpu; @@ -1322,6 +1967,7 @@ void MicroProfileFlip() S.nFlipMax = MicroProfileMax(S.nFlipMax, nTick); } + uint8_t* pTimerToGroup = &S.TimerToGroup[0]; for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) { MicroProfileThreadLog* pLog = S.Pool[i]; @@ -1341,6 +1987,7 @@ void MicroProfileFlip() if(S.nRunning) { + uint64_t* pFrameGroup = &S.FrameGroup[0]; { MICROPROFILE_SCOPE(g_MicroProfileClear); for(uint32_t i = 0; i < S.nTotalTimers; ++i) @@ -1349,25 +1996,35 @@ void MicroProfileFlip() S.Frame[i].nCount = 0; S.FrameExclusive[i] = 0; } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + pFrameGroup[i] = 0; + } for(uint32_t j = 0; j < MICROPROFILE_META_MAX; ++j) { - if(S.MetaCounters[j].pName) + if(S.MetaCounters[j].pName && 0 != (S.nActiveBars & (MP_DRAW_META_FIRST<nGroupStackPos[0]; + int64_t nGroupTicks[MICROPROFILE_MAX_GROUPS] = {0}; + + uint32_t nPut = pFrameNext->nLogStart[i]; uint32_t nGet = pFrameCurrent->nLogStart[i]; uint32_t nRange[2][2] = { {0, 0}, {0, 0}, }; @@ -1377,6 +2034,8 @@ void MicroProfileFlip() //fetch gpu results. if(pLog->nGpu) { + uint64_t nLastTick = pFrameCurrent->nFrameStartGpu; + for(uint32_t j = 0; j < 2; ++j) { uint32_t nStart = nRange[j][0]; @@ -1384,12 +2043,24 @@ void MicroProfileFlip() for(uint32_t k = nStart; k < nEnd; ++k) { MicroProfileLogEntry L = pLog->Log[k]; - pLog->Log[k] = MicroProfileLogSetTick(L, MicroProfileGpuGetTimeStamp((uint32_t)MicroProfileLogGetTick(L))); + + int Type = MicroProfileLogType(L); + + if(Type == MP_LOG_ENTER || Type == MP_LOG_LEAVE) + { + uint32_t nTimer = MicroProfileLogGetTick(L); + uint64_t nTick = MicroProfileGpuGetTimeStamp(nTimer); + + if(nTick != MICROPROFILE_INVALID_TICK) + nLastTick = nTick; + + pLog->Log[k] = MicroProfileLogSetTick(L, nLastTick); + } } } } - - + + uint32_t* pStack = &pLog->nStack[0]; int64_t* pChildTickStack = &pLog->nChildTickStack[0]; uint32_t nStackPos = pLog->nStackPos; @@ -1401,12 +2072,18 @@ void MicroProfileFlip() for(uint32_t k = nStart; k < nEnd; ++k) { MicroProfileLogEntry LE = pLog->Log[k]; - int nType = MicroProfileLogType(LE); + uint64_t nType = MicroProfileLogType(LE); + if(MP_LOG_ENTER == nType) { + int nTimer = MicroProfileLogTimerIndex(LE); + uint8_t nGroup = pTimerToGroup[nTimer]; MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX); + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + pGroupStackPos[nGroup]++; pStack[nStackPos++] = k; pChildTickStack[nStackPos] = 0; + } else if(MP_LOG_META == nType) { @@ -1419,11 +2096,13 @@ void MicroProfileFlip() S.MetaCounters[nMetaIndex].nCounters[nCounter] += nMetaCount; } } - else + else if(MP_LOG_LEAVE == nType) { - MP_ASSERT(nType == MP_LOG_LEAVE); + int nTimer = MicroProfileLogTimerIndex(LE); + uint8_t nGroup = pTimerToGroup[nTimer]; + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); if(nStackPos) - { + { int64_t nTickStart = pLog->Log[pStack[nStackPos-1]]; int64_t nTicks = MicroProfileLogTickDifference(nTickStart, LE); int64_t nChildTicks = pChildTickStack[nStackPos]; @@ -1434,10 +2113,27 @@ void MicroProfileFlip() S.Frame[nTimerIndex].nTicks += nTicks; S.FrameExclusive[nTimerIndex] += (nTicks-nChildTicks); S.Frame[nTimerIndex].nCount += 1; + + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + uint8_t nGroupStackPos = pGroupStackPos[nGroup]; + if(nGroupStackPos) + { + nGroupStackPos--; + if(0 == nGroupStackPos) + { + nGroupTicks[nGroup] += nTicks; + } + pGroupStackPos[nGroup] = nGroupStackPos; + } } } } } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + pLog->nGroupTicks[i] += nGroupTicks[i]; + pFrameGroup[i] += nGroupTicks[i]; + } pLog->nStackPos = nStackPos; } } @@ -1445,12 +2141,37 @@ void MicroProfileFlip() MICROPROFILE_SCOPE(g_MicroProfileAccumulate); for(uint32_t i = 0; i < S.nTotalTimers; ++i) { - S.AggregateTimers[i].nTicks += S.Frame[i].nTicks; - S.AggregateTimers[i].nCount += S.Frame[i].nCount; - S.MaxTimers[i] = MicroProfileMax(S.MaxTimers[i], S.Frame[i].nTicks); - S.AggregateTimersExclusive[i] += S.FrameExclusive[i]; - S.MaxTimersExclusive[i] = MicroProfileMax(S.MaxTimersExclusive[i], S.FrameExclusive[i]); + S.AccumTimers[i].nTicks += S.Frame[i].nTicks; + S.AccumTimers[i].nCount += S.Frame[i].nCount; + S.AccumMaxTimers[i] = MicroProfileMax(S.AccumMaxTimers[i], S.Frame[i].nTicks); + S.AccumMinTimers[i] = MicroProfileMin(S.AccumMinTimers[i], S.Frame[i].nTicks); + S.AccumTimersExclusive[i] += S.FrameExclusive[i]; + S.AccumMaxTimersExclusive[i] = MicroProfileMax(S.AccumMaxTimersExclusive[i], S.FrameExclusive[i]); } + + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + S.AccumGroup[i] += pFrameGroup[i]; + S.AccumGroupMax[i] = MicroProfileMax(S.AccumGroupMax[i], pFrameGroup[i]); + } + + for(uint32_t j = 0; j < MICROPROFILE_META_MAX; ++j) + { + if(S.MetaCounters[j].pName && 0 != (S.nActiveBars & (MP_DRAW_META_FIRST<nAggregateGroupTicks[0], &pLog->nGroupTicks[0], sizeof(pLog->nAggregateGroupTicks)); + + if(nAggregateClear) + { + memset(&pLog->nGroupTicks[0], 0, sizeof(pLog->nGroupTicks)); + } + } + + for(uint32_t j = 0; j < MICROPROFILE_META_MAX; ++j) + { + if(S.MetaCounters[j].pName && 0 != (S.nActiveBars & (MP_DRAW_META_FIRST<pContextGpu = pContext; + } +} + +void MicroProfileGpuBegin(void* pContext) +{ + if(MicroProfileThreadLog* pLog = MicroProfileGetOrCreateThreadLog()) + { + MP_ASSERT(!pLog->bActiveGpu); + + pLog->pContextGpu = pContext; + pLog->nStartGpu = pLog->nPutGpu.load(); + pLog->bActiveGpu = 1; + } +} + +uint64_t MicroProfileGpuEnd() +{ + if(MicroProfileThreadLog* pLog = MicroProfileGetThreadLog()) + { + MP_ASSERT(pLog->bActiveGpu); + + uint32_t nStartGpu = pLog->nStartGpu; + uint32_t nPutGpu = pLog->nPutGpu.load(); + MP_ASSERT(nPutGpu >= nStartGpu); + + pLog->pContextGpu = 0; + pLog->nStartGpu = nPutGpu; + pLog->bActiveGpu = 0; + + MP_ASSERT(MICROPROFILE_GPU_BUFFER_SIZE <= 1 << 24); + + return (uint64_t(pLog->nLogIndex) << 48) | (uint64_t(nStartGpu) << 24) | uint64_t(nPutGpu); + } + return 0; +} + +void MicroProfileGpuSubmit(uint64_t nWork) +{ + if (!nWork) + return; + + uint32_t nLogIndex = (nWork >> 48) & 0xffff; + uint32_t nStart = (nWork >> 24) & 0xffffff; + uint32_t nEnd = nWork & 0xffffff; + + MP_ASSERT(nLogIndex < MICROPROFILE_MAX_THREADS); + MP_ASSERT(nStart <= nEnd); + + MicroProfileThreadLog* pLog = S.Pool[nLogIndex]; + if (!pLog) + return; + + MP_ASSERT(nEnd <= pLog->nStartGpu); + + for (uint32_t i = nStart; i < nEnd; ++i) + { + MicroProfileLogEntry LE = pLog->LogGpu[i]; + + MicroProfileLogPut(MicroProfileLogTimerIndex(LE), MicroProfileLogGetTick(LE), MicroProfileLogType(LE), g_MicroProfileGpuLog); + } +} + void MicroProfileSetForceEnable(bool bEnable) { S.nForceEnable = bEnable ? 1 : 0; @@ -1536,6 +2394,40 @@ void MicroProfileSetEnableAllGroups(bool bEnableAllGroups) S.nAllGroupsWanted = bEnableAllGroups ? 1 : 0; } +void MicroProfileEnableCategory(const char* pCategory, bool bEnabled) +{ + int nCategoryIndex = -1; + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + if(!MP_STRCASECMP(pCategory, S.CategoryInfo[i].pName)) + { + nCategoryIndex = (int)i; + break; + } + } + if(nCategoryIndex >= 0) + { + if(bEnabled) + { + S.nActiveGroupWanted |= S.CategoryInfo[nCategoryIndex].nGroupMask; + } + else + { + S.nActiveGroupWanted &= ~S.CategoryInfo[nCategoryIndex].nGroupMask; + } + } +} + + +void MicroProfileEnableCategory(const char* pCategory) +{ + MicroProfileEnableCategory(pCategory, true); +} +void MicroProfileDisableCategory(const char* pCategory) +{ + MicroProfileEnableCategory(pCategory, false); +} + bool MicroProfileGetEnableAllGroups() { return 0 != S.nAllGroupsWanted; @@ -1551,6 +2443,30 @@ bool MicroProfileGetForceMetaCounters() return 0 != S.nForceMetaCounters; } +void MicroProfileEnableMetaCounter(const char* pMeta) +{ + for(uint32_t i = 0; i < MICROPROFILE_META_MAX; ++i) + { + if(S.MetaCounters[i].pName && 0 == MP_STRCASECMP(S.MetaCounters[i].pName, pMeta)) + { + S.nBars |= (MP_DRAW_META_FIRST< pOut) //reverse string + { + char c = *pTmp; + *pTmp = *pOut; + *pOut = c; + pTmp--; + pOut++; + } + } + break; + case MICROPROFILE_COUNTER_FORMAT_BYTES: + { + const char* pExt[] = { "b","kb","mb","gb","tb","pb", "eb","zb", "yb" }; + size_t nNumExt = sizeof(pExt) / sizeof(pExt[0]); + int64_t nShift = 0; + int64_t nDivisor = 1; + int64_t nCountShifted = nCounter >> 10; + while (nCountShifted) + { + nDivisor <<= 10; + nCountShifted >>= 10; + nShift++; + } + MP_ASSERT(nShift < (int64_t)nNumExt); + if (nShift) + { + nLen = snprintf(pOut, nBufferSize - 1, "%3.2f%s", (double)nCounter / nDivisor, pExt[nShift]); + } + else + { + nLen = snprintf(pOut, nBufferSize - 1, "%lld%s", (long long)nCounter, pExt[nShift]); + } + } + break; + } + pBase[nLen] = '\0'; + + return nLen; +} typedef void MicroProfileWriteCallback(void* Handle, size_t size, const char* pData); -uint32_t MicroProfileWebServerPort() +void MicroProfileDumpFile(const char* pPath, MicroProfileDumpType eType, uint32_t nFrames) { - return S.nWebServerPort; -} - -void MicroProfileDumpHtml(const char* pFile) -{ - uint32_t nLen = uint32_t(strlen(pFile)); - if(nLen > sizeof(S.HtmlDumpPath)-1) + size_t nLen = strlen(pPath); + if(nLen > sizeof(S.DumpPath)-1) { return; } - memcpy(S.HtmlDumpPath, pFile, nLen+1); - S.nDumpHtmlNextFrame = 1; + memcpy(S.DumpPath, pPath, nLen+1); + S.nDumpFileNextFrame = 1; + S.eDumpType = eType; + S.nDumpFrames = nFrames; } -void MicroProfilePrintf(MicroProfileWriteCallback CB, void* Handle, const char* pFmt, ...) +MICROPROFILE_FORMAT(3, 4) void MicroProfilePrintf(MicroProfileWriteCallback CB, void* Handle, const char* pFmt, ...) { - char buffer[32*1024]; + char buffer[4096]; va_list args; va_start (args, pFmt); #ifdef _WIN32 @@ -1689,84 +2686,321 @@ void MicroProfilePrintf(MicroProfileWriteCallback CB, void* Handle, const char* va_end (args); } -void MicroProfileDumpHtml(MicroProfileWriteCallback CB, void* Handle, int nMaxFrames) +void MicroProfilePrintUIntComma(MicroProfileWriteCallback CB, void* Handle, uint64_t nData) { - CB(Handle, g_MicroProfileHtml_begin_size-1, &g_MicroProfileHtml_begin[0]); + char Buffer[32]; + uint32_t nOffset = sizeof(Buffer); + Buffer[--nOffset] = ','; + + if(nData < 10) + { + Buffer[--nOffset] = '0' + nData; + } + else + { + do + { + Buffer[--nOffset] = "0123456789abcdef"[nData & 0xf]; + nData >>= 4; + } + while(nData); + + Buffer[--nOffset] = 'x'; + Buffer[--nOffset] = '0'; + } + + CB(Handle, sizeof(Buffer) - nOffset, &Buffer[nOffset]); +} + +void MicroProfilePrintString(MicroProfileWriteCallback CB, void* Handle, const char* pData) +{ + CB(Handle, strlen(pData), pData); +} + +void MicroProfileDumpCsv(MicroProfileWriteCallback CB, void* Handle, int nMaxFrames) +{ + (void)nMaxFrames; + + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + + MicroProfilePrintf(CB, Handle, "frames,%d\n", nAggregateFrames); + MicroProfilePrintf(CB, Handle, "group,name,average,max,callaverage\n"); + + uint32_t nNumTimers = S.nTotalTimers; + uint32_t nBlockSize = 2 * nNumTimers; + float* pTimers = (float*)alloca(nBlockSize * 9 * sizeof(float)); + float* pAverage = pTimers + nBlockSize; + float* pMax = pTimers + 2 * nBlockSize; + float* pMin = pTimers + 3 * nBlockSize; + float* pCallAverage = pTimers + 4 * nBlockSize; + float* pTimersExclusive = pTimers + 5 * nBlockSize; + float* pAverageExclusive = pTimers + 6 * nBlockSize; + float* pMaxExclusive = pTimers + 7 * nBlockSize; + float* pTotal = pTimers + 8 * nBlockSize; + + MicroProfileCalcAllTimers(pTimers, pAverage, pMax, pMin, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, pTotal, nNumTimers); + + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + uint32_t nIdx = i * 2; + MicroProfilePrintf(CB, Handle, "\"%s\",\"%s\",%f,%f,%f\n", S.TimerInfo[i].pName, S.GroupInfo[S.TimerInfo[i].nGroupIndex].pName, pAverage[nIdx], pMax[nIdx], pCallAverage[nIdx]); + } + + MicroProfilePrintf(CB, Handle, "\n\n"); + + MicroProfilePrintf(CB, Handle, "group,average,max,total\n"); + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + const char* pGroupName = S.GroupInfo[j].pName; + float fToMs = S.GroupInfo[j].Type == MicroProfileTokenTypeGpu ? fToMsGPU : fToMsCPU; + if(pGroupName[0] != '\0') + { + MicroProfilePrintf(CB, Handle, "\"%s\",%.3f,%.3f,%.3f\n", pGroupName, fToMs * S.AggregateGroup[j] / nAggregateFrames, fToMs * S.AggregateGroup[j] / nAggregateFrames, fToMs * S.AggregateGroup[j]); + } + } + + MicroProfilePrintf(CB, Handle, "\n\n"); + MicroProfilePrintf(CB, Handle, "group,thread,average,total\n"); + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + const char* pThreadName = &S.Pool[i]->ThreadName[0]; + // MicroProfilePrintf(CB, Handle, "var ThreadGroupTime%d = [", i); + float fToMs = S.Pool[i]->nGpu ? fToMsGPU : fToMsCPU; + { + uint64_t nTicks = S.Pool[i]->nAggregateGroupTicks[j]; + float fTime = nTicks / nAggregateFrames * fToMs; + float fTimeTotal = nTicks * fToMs; + if(fTimeTotal > 0.01f) + { + const char* pGroupName = S.GroupInfo[j].pName; + MicroProfilePrintf(CB, Handle, "\"%s\",\"%s\",%.3f,%.3f\n", pGroupName, pThreadName, fTime, fTimeTotal); + } + } + } + } + } + + MicroProfilePrintf(CB, Handle, "\n\n"); + MicroProfilePrintf(CB, Handle, "frametimecpu\n"); + + const uint32_t nCount = MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 3; + const uint32_t nStart = S.nFrameCurrent; + for(uint32_t i = nCount; i > 0; i--) + { + uint32_t nFrame = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameNext = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + uint64_t nTicks = S.Frames[nFrameNext].nFrameStartCpu - S.Frames[nFrame].nFrameStartCpu; + MicroProfilePrintf(CB, Handle, "%f,", nTicks * fToMsCPU); + } + MicroProfilePrintf(CB, Handle, "\n"); + + MicroProfilePrintf(CB, Handle, "\n\n"); + MicroProfilePrintf(CB, Handle, "frametimegpu\n"); + + for(uint32_t i = nCount; i > 0; i--) + { + uint32_t nFrame = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameNext = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + uint64_t nTicks = S.Frames[nFrameNext].nFrameStartGpu - S.Frames[nFrame].nFrameStartGpu; + MicroProfilePrintf(CB, Handle, "%f,", nTicks * fToMsGPU); + } + MicroProfilePrintf(CB, Handle, "\n\n"); + MicroProfilePrintf(CB, Handle, "Meta\n");//only single frame snapshot + MicroProfilePrintf(CB, Handle, "name,average,max,total\n"); + for(int j = 0; j < MICROPROFILE_META_MAX; ++j) + { + if(S.MetaCounters[j].pName) + { + MicroProfilePrintf(CB, Handle, "\"%s\",%f,%lld,%lld\n",S.MetaCounters[j].pName, S.MetaCounters[j].nSumAggregate / (float)nAggregateFrames, (long long)S.MetaCounters[j].nSumAggregateMax, (long long)S.MetaCounters[j].nSumAggregate); + } + } +} + +#if MICROPROFILE_EMBED_HTML +extern const char* g_MicroProfileHtml_begin[]; +extern size_t g_MicroProfileHtml_begin_sizes[]; +extern size_t g_MicroProfileHtml_begin_count; +extern const char* g_MicroProfileHtml_end[]; +extern size_t g_MicroProfileHtml_end_sizes[]; +extern size_t g_MicroProfileHtml_end_count; + + +void MicroProfileDumpHtml(MicroProfileWriteCallback CB, void* Handle, int nMaxFrames, const char* pHost) +{ + uint32_t nRunning = S.nRunning; + S.nRunning = 0; + //stall pushing of timers + uint64_t nActiveGroup = S.nActiveGroup; + S.nActiveGroup = 0; + S.nPauseTicks = MP_TICK(); + + + for(size_t i = 0; i < g_MicroProfileHtml_begin_count; ++i) + { + CB(Handle, g_MicroProfileHtml_begin_sizes[i]-1, g_MicroProfileHtml_begin[i]); + } //dump info uint64_t nTicks = MP_TICK(); - float fAggregateMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * (nTicks - S.nAggregateFlipTick); + + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + float fAggregateMs = fToMsCPU * (nTicks - S.nAggregateFlipTick); + MicroProfilePrintf(CB, Handle, "var DumpHost = '%s';\n", pHost ? pHost : ""); + time_t CaptureTime; + time(&CaptureTime); + MicroProfilePrintf(CB, Handle, "var DumpUtcCaptureTime = %ld;\n", CaptureTime); MicroProfilePrintf(CB, Handle, "var AggregateInfo = {'Frames':%d, 'Time':%f};\n", S.nAggregateFrames, fAggregateMs); + //categories + MicroProfilePrintf(CB, Handle, "var CategoryInfo = Array(%d);\n",S.nCategoryCount); + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + MicroProfilePrintf(CB, Handle, "CategoryInfo[%d] = \"%s\";\n", i, S.CategoryInfo[i].pName); + } //groups MicroProfilePrintf(CB, Handle, "var GroupInfo = Array(%d);\n\n",S.nGroupCount); + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + for(uint32_t i = 0; i < S.nGroupCount; ++i) { MP_ASSERT(i == S.GroupInfo[i].nGroupIndex); - MicroProfilePrintf(CB, Handle, "GroupInfo[%d] = MakeGroup(%d, \"%s\", %d, %d);\n", S.GroupInfo[i].nGroupIndex, S.GroupInfo[i].nGroupIndex, S.GroupInfo[i].pName, S.GroupInfo[i].nNumTimers, S.GroupInfo[i].Type == MicroProfileTokenTypeGpu?1:0); + float fToMs = S.GroupInfo[i].Type == MicroProfileTokenTypeCpu ? fToMsCPU : fToMsGPU; + uint32_t nColor = S.TimerInfo[i].nColor; + MicroProfilePrintf(CB, Handle, "GroupInfo[%d] = MakeGroup(%d, \"%s\", %d, %d, %d, %f, %f, %f, '#%06x');\n", + S.GroupInfo[i].nGroupIndex, + S.GroupInfo[i].nGroupIndex, + S.GroupInfo[i].pName, + S.GroupInfo[i].nCategory, + S.GroupInfo[i].nNumTimers, + S.GroupInfo[i].Type == MicroProfileTokenTypeGpu?1:0, + fToMs * S.AggregateGroup[i], + fToMs * S.AggregateGroup[i] / nAggregateFrames, + fToMs * S.AggregateGroupMax[i], + ((MICROPROFILE_UNPACK_RED(nColor) & 0xff) << 16) | ((MICROPROFILE_UNPACK_GREEN(nColor) & 0xff) << 8) | (MICROPROFILE_UNPACK_BLUE(nColor) & 0xff)); } //timers uint32_t nNumTimers = S.nTotalTimers; uint32_t nBlockSize = 2 * nNumTimers; - float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float)); + float* pTimers = (float*)alloca(nBlockSize * 9 * sizeof(float)); float* pAverage = pTimers + nBlockSize; float* pMax = pTimers + 2 * nBlockSize; - float* pCallAverage = pTimers + 3 * nBlockSize; - float* pTimersExclusive = pTimers + 4 * nBlockSize; - float* pAverageExclusive = pTimers + 5 * nBlockSize; - float* pMaxExclusive = pTimers + 6 * nBlockSize; + float* pMin = pTimers + 3 * nBlockSize; + float* pCallAverage = pTimers + 4 * nBlockSize; + float* pTimersExclusive = pTimers + 5 * nBlockSize; + float* pAverageExclusive = pTimers + 6 * nBlockSize; + float* pMaxExclusive = pTimers + 7 * nBlockSize; + float* pTotal = pTimers + 8 * nBlockSize; - MicroProfileCalcAllTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nNumTimers); + MicroProfileCalcAllTimers(pTimers, pAverage, pMax, pMin, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, pTotal, nNumTimers); MicroProfilePrintf(CB, Handle, "\nvar TimerInfo = Array(%d);\n\n", S.nTotalTimers); for(uint32_t i = 0; i < S.nTotalTimers; ++i) { uint32_t nIdx = i * 2; MP_ASSERT(i == S.TimerInfo[i].nTimerIndex); - MicroProfilePrintf(CB, Handle, "var Meta%d = [", i); - bool bOnce = true; + + uint32_t nColor = S.TimerInfo[i].nColor; + uint32_t nColorDark = (nColor >> 1) & ~0x80808080; + MicroProfilePrintf(CB, Handle, "TimerInfo[%d] = MakeTimer(%d, \"%s\", %d, '#%06x','#%06x', %f, %f, %f, %f, %f, %f, %d, %f,\n", + S.TimerInfo[i].nTimerIndex, S.TimerInfo[i].nTimerIndex, S.TimerInfo[i].pName, S.TimerInfo[i].nGroupIndex, + ((MICROPROFILE_UNPACK_RED(nColor) & 0xff) << 16) | ((MICROPROFILE_UNPACK_GREEN(nColor) & 0xff) << 8) | (MICROPROFILE_UNPACK_BLUE(nColor) & 0xff), + ((MICROPROFILE_UNPACK_RED(nColorDark) & 0xff) << 16) | ((MICROPROFILE_UNPACK_GREEN(nColorDark) & 0xff) << 8) | (MICROPROFILE_UNPACK_BLUE(nColorDark) & 0xff), + pAverage[nIdx], + pMax[nIdx], + pMin[nIdx], + pAverageExclusive[nIdx], + pMaxExclusive[nIdx], + pCallAverage[nIdx], + S.Aggregate[i].nCount, + pTotal[nIdx]); + + MicroProfilePrintString(CB, Handle, "\t["); for(int j = 0; j < MICROPROFILE_META_MAX; ++j) { if(S.MetaCounters[j].pName) { - uint32_t lala = S.MetaCounters[j].nCounters[i]; - MicroProfilePrintf(CB, Handle, bOnce ? "%d" : ",%d", lala); - bOnce = false; + MicroProfilePrintUIntComma(CB, Handle, S.MetaCounters[j].nCounters[i]); } } - MicroProfilePrintf(CB, Handle, "];\n"); - MicroProfilePrintf(CB, Handle, "TimerInfo[%d] = MakeTimer(%d, \"%s\", %d, '#%02x%02x%02x', %f, %f, %f, %f, %f, %d, Meta%d);\n", S.TimerInfo[i].nTimerIndex, S.TimerInfo[i].nTimerIndex, S.TimerInfo[i].pName, S.TimerInfo[i].nGroupIndex, - MICROPROFILE_UNPACK_RED(S.TimerInfo[i].nColor) & 0xff, - MICROPROFILE_UNPACK_GREEN(S.TimerInfo[i].nColor) & 0xff, - MICROPROFILE_UNPACK_BLUE(S.TimerInfo[i].nColor) & 0xff, - pAverage[nIdx], - pMax[nIdx], - pAverageExclusive[nIdx], - pMaxExclusive[nIdx], - pCallAverage[nIdx], - S.Aggregate[i].nCount, - i - ); - + MicroProfilePrintString(CB, Handle, "],["); + for(int j = 0; j < MICROPROFILE_META_MAX; ++j) + { + if(S.MetaCounters[j].pName) + { + MicroProfilePrintUIntComma(CB, Handle, S.MetaCounters[j].nAggregate[i]); + } + } + MicroProfilePrintString(CB, Handle, "],["); + for(int j = 0; j < MICROPROFILE_META_MAX; ++j) + { + if(S.MetaCounters[j].pName) + { + MicroProfilePrintUIntComma(CB, Handle, S.MetaCounters[j].nAggregateMax[i]); + } + } + MicroProfilePrintString(CB, Handle, "]);\n"); } - MicroProfilePrintf(CB, Handle, "\nvar ThreadNames = ["); + MicroProfilePrintString(CB, Handle, "\nvar ThreadNames = ["); for(uint32_t i = 0; i < S.nNumLogs; ++i) { if(S.Pool[i]) { MicroProfilePrintf(CB, Handle, "'%s',", S.Pool[i]->ThreadName); - } else { MicroProfilePrintf(CB, Handle, "'Thread %d',", i); } } - MicroProfilePrintf(CB, Handle, "];\n\n"); - MicroProfilePrintf(CB, Handle, "\nvar MetaNames = ["); + MicroProfilePrintString(CB, Handle, "];\n\n"); + + + MicroProfilePrintString(CB, Handle, "\nvar ThreadIds = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + MicroProfileThreadIdType nThreadId = S.Pool[i] ? S.Pool[i]->nThreadId : 0; + MicroProfilePrintUIntComma(CB, Handle, nThreadId); + } + MicroProfilePrintString(CB, Handle, "];\n\n"); + + + MicroProfilePrintString(CB, Handle, "\nvar ThreadGpu = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + uint32_t nGpu = S.Pool[i] ? S.Pool[i]->nGpu : 0; + MicroProfilePrintUIntComma(CB, Handle, nGpu); + } + MicroProfilePrintString(CB, Handle, "];\n\n"); + + + MicroProfilePrintString(CB, Handle, "\nvar ThreadGroupTimeArray = [\n"); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + float fToMs = S.Pool[i]->nGpu ? fToMsGPU : fToMsCPU; + MicroProfilePrintf(CB, Handle, "MakeTimes(%e,[", fToMs); + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + MicroProfilePrintUIntComma(CB, Handle, S.Pool[i]->nAggregateGroupTicks[j]); + } + MicroProfilePrintString(CB, Handle, "]),\n"); + } + } + MicroProfilePrintString(CB, Handle, "];"); + + + MicroProfilePrintString(CB, Handle, "\nvar MetaNames = ["); for(int i = 0; i < MICROPROFILE_META_MAX; ++i) { if(S.MetaCounters[i].pName) @@ -1774,31 +3008,103 @@ void MicroProfileDumpHtml(MicroProfileWriteCallback CB, void* Handle, int nMaxFr MicroProfilePrintf(CB, Handle, "'%s',", S.MetaCounters[i].pName); } } + MicroProfilePrintString(CB, Handle, "];\n\n"); - - MicroProfilePrintf(CB, Handle, "];\n\n"); - - - - uint32_t nNumFrames = (MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 1); - if(S.nFrameCurrentIndex < nNumFrames) - nNumFrames = S.nFrameCurrentIndex; - if((int)nNumFrames > nMaxFrames) + MicroProfilePrintString(CB, Handle, "\nvar CounterInfo = ["); + for(uint32_t i = 0; i < S.nNumCounters; ++i) { - nNumFrames = nMaxFrames; + int64_t nCounter = S.Counters[i].load(); + int64_t nLimit = S.CounterInfo[i].nLimit; + float fCounterPrc = 0.f; + float fBoxPrc = 1.f; + if(nLimit) + { + fCounterPrc = (float)nCounter / nLimit; + if(fCounterPrc>1.f) + { + fBoxPrc = 1.f / fCounterPrc; + fCounterPrc = 1.f; + } + } + + int64_t nCounterMin = 0, nCounterMax = 0; + + #if MICROPROFILE_COUNTER_HISTORY + nCounterMin = S.nCounterMin[i]; + nCounterMax = S.nCounterMax[i]; + #endif + + char Formatted[64]; + char FormattedLimit[64]; + MicroProfileFormatCounter(S.CounterInfo[i].eFormat, nCounter, Formatted, sizeof(Formatted)-1); + MicroProfileFormatCounter(S.CounterInfo[i].eFormat, S.CounterInfo[i].nLimit, FormattedLimit, sizeof(FormattedLimit)-1); + MicroProfilePrintf(CB, Handle, "MakeCounter(%d, %d, %d, %d, %d, '%s', %lld, %lld, %lld, '%s', %lld, '%s', %d, %f, %f, [", + i, + S.CounterInfo[i].nParent, + S.CounterInfo[i].nSibling, + S.CounterInfo[i].nFirstChild, + S.CounterInfo[i].nLevel, + S.CounterInfo[i].pName, + (long long)nCounter, + (long long)nCounterMin, + (long long)nCounterMax, + Formatted, + (long long)nLimit, + FormattedLimit, + S.CounterInfo[i].eFormat == MICROPROFILE_COUNTER_FORMAT_BYTES ? 1 : 0, + fCounterPrc, + fBoxPrc + ); + + #if MICROPROFILE_COUNTER_HISTORY + if(0 != (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED)) + { + uint32_t nBaseIndex = S.nCounterHistoryPut; + for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j) + { + uint32_t nHistoryIndex = (nBaseIndex + j) % MICROPROFILE_GRAPH_HISTORY; + int64_t nValue = MicroProfileClamp(S.nCounterHistory[nHistoryIndex][i], nCounterMin, nCounterMax); + MicroProfilePrintUIntComma(CB, Handle, nValue - nCounterMin); + } + } + #endif + + MicroProfilePrintString(CB, Handle, "]),\n"); } + MicroProfilePrintString(CB, Handle, "];\n\n"); + uint32_t nNumFrames = (MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 3); //leave a few to not overwrite + nNumFrames = MicroProfileMin(nNumFrames, (uint32_t)nMaxFrames); + + + uint32_t nFirstFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nLastFrame = (nFirstFrame + nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; + MP_ASSERT(nLastFrame == (S.nFrameCurrent % MICROPROFILE_MAX_FRAME_HISTORY)); + MP_ASSERT(nFirstFrame < MICROPROFILE_MAX_FRAME_HISTORY); + MP_ASSERT(nLastFrame < MICROPROFILE_MAX_FRAME_HISTORY); + const int64_t nTickStart = S.Frames[nFirstFrame].nFrameStartCpu; + const int64_t nTickEnd = S.Frames[nLastFrame].nFrameStartCpu; + int64_t nTickStartGpu = S.Frames[nFirstFrame].nFrameStartGpu; + + int64_t nTicksPerSecondCpu = MicroProfileTicksPerSecondCpu(); + int64_t nTicksPerSecondGpu = MicroProfileTicksPerSecondGpu(); + + int64_t nTickReferenceCpu = 0, nTickReferenceGpu = 0; + // Can't just call GetGpuTickReference off main thread... + if(0 && MicroProfileGetGpuTickReference(&nTickReferenceCpu, &nTickReferenceGpu)) + { + nTickStartGpu = (nTickStart - nTickReferenceCpu) * (double(nTicksPerSecondGpu) / double(nTicksPerSecondCpu)) + nTickReferenceGpu; + } #if MICROPROFILE_DEBUG printf("dumping %d frames\n", nNumFrames); + printf("dumping frame %d to %d\n", nFirstFrame, nLastFrame); #endif - uint32_t nFirstFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; - uint32_t nFirstFrameIndex = S.nFrameCurrentIndex - nNumFrames; - int64_t nTickStart = S.Frames[nFirstFrame].nFrameStartCpu; - int64_t nTickStartGpu = S.Frames[nFirstFrame].nFrameStartGpu; + uint32_t* nTimerCounter = (uint32_t*)alloca(sizeof(uint32_t)* S.nTotalTimers); + memset(nTimerCounter, 0, sizeof(uint32_t) * S.nTotalTimers); MicroProfilePrintf(CB, Handle, "var Frames = Array(%d);\n", nNumFrames); for(uint32_t i = 0; i < nNumFrames; ++i) @@ -1806,2042 +3112,1811 @@ void MicroProfileDumpHtml(MicroProfileWriteCallback CB, void* Handle, int nMaxFr uint32_t nFrameIndex = (nFirstFrame + i) % MICROPROFILE_MAX_FRAME_HISTORY; uint32_t nFrameIndexNext = (nFrameIndex + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + MicroProfilePrintf(CB, Handle, "var tt%d = [\n", i); for(uint32_t j = 0; j < S.nNumLogs; ++j) { MicroProfileThreadLog* pLog = S.Pool[j]; - int64_t nStartTick = pLog->nGpu ? nTickStartGpu : nTickStart; uint32_t nLogStart = S.Frames[nFrameIndex].nLogStart[j]; uint32_t nLogEnd = S.Frames[nFrameIndexNext].nLogStart[j]; - float fToMs = MicroProfileTickToMsMultiplier(pLog->nGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); - MicroProfilePrintf(CB, Handle, "var ts_%d_%d = [", i, j); - if(nLogStart != nLogEnd) + MicroProfilePrintString(CB, Handle, "["); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) { - uint32_t k = nLogStart; uint32_t nLogType = MicroProfileLogType(pLog->Log[k]); - float fTime = nLogType == MP_LOG_META ? 0.f : MicroProfileLogTickDifference(nStartTick, pLog->Log[k]) * fToMs; - MicroProfilePrintf(CB, Handle, "%f", fTime); - for(k = (k+1) % MICROPROFILE_BUFFER_SIZE; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) + if(nLogType == MP_LOG_META) { - uint32_t nLogType = MicroProfileLogType(pLog->Log[k]); - float fTime = nLogType == MP_LOG_META ? 0.f : MicroProfileLogTickDifference(nStartTick, pLog->Log[k]) * fToMs; - MicroProfilePrintf(CB, Handle, ",%f", fTime); + //for meta, store the count + 8, which is the tick part + nLogType = 8 + MicroProfileLogGetTick(pLog->Log[k]); } + MicroProfilePrintUIntComma(CB, Handle, nLogType); } - MicroProfilePrintf(CB, Handle, "];\n"); - MicroProfilePrintf(CB, Handle, "var tt_%d_%d = [", i, j); - if(nLogStart != nLogEnd) + MicroProfilePrintString(CB, Handle, "],\n"); + } + MicroProfilePrintString(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "var ts%d = [\n", i); + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfileThreadLog* pLog = S.Pool[j]; + uint32_t nLogStart = S.Frames[nFrameIndex].nLogStart[j]; + uint32_t nLogEnd = S.Frames[nFrameIndexNext].nLogStart[j]; + + int64_t nStartTick = pLog->nGpu ? nTickStartGpu : nTickStart; + float fToMs = pLog->nGpu ? fToMsGPU : fToMsCPU; + + if(pLog->nGpu) + MicroProfilePrintf(CB, Handle, "MakeTimesExtra(%e,%e,tt%d[%d],[", fToMs, fToMsCPU, i, j); + else + MicroProfilePrintf(CB, Handle, "MakeTimes(%e,[", fToMs); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) { - uint32_t k = nLogStart; - MicroProfilePrintf(CB, Handle, "%d", MicroProfileLogType(pLog->Log[k])); - for(k = (k+1) % MICROPROFILE_BUFFER_SIZE; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) + uint32_t nLogType = MicroProfileLogType(pLog->Log[k]); + uint64_t nTick = + (nLogType == MP_LOG_ENTER || nLogType == MP_LOG_LEAVE) + ? MicroProfileLogTickDifference(nStartTick, pLog->Log[k]) + : (nLogType == MP_LOG_GPU_EXTRA) + ? MicroProfileLogTickDifference(nTickStart, pLog->Log[k]) + : 0; + MicroProfilePrintUIntComma(CB, Handle, nTick); + } + MicroProfilePrintString(CB, Handle, "]),\n"); + } + MicroProfilePrintString(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "var ti%d = [\n", i); + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfileThreadLog* pLog = S.Pool[j]; + uint32_t nLogStart = S.Frames[nFrameIndex].nLogStart[j]; + uint32_t nLogEnd = S.Frames[nFrameIndexNext].nLogStart[j]; + + uint32_t nLabelIndex = 0; + MicroProfilePrintString(CB, Handle, "["); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) + { + uint32_t nLogType = MicroProfileLogType(pLog->Log[k]); + uint32_t nTimerIndex = (uint32_t)MicroProfileLogTimerIndex(pLog->Log[k]); + uint32_t nIndex = (nLogType == MP_LOG_LABEL) ? nLabelIndex++ : nTimerIndex; + MicroProfilePrintUIntComma(CB, Handle, nIndex); + + if(nLogType == MP_LOG_ENTER) + nTimerCounter[nTimerIndex]++; + } + MicroProfilePrintString(CB, Handle, "],\n"); + } + MicroProfilePrintString(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "var tl%d = [\n", i); + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfileThreadLog* pLog = S.Pool[j]; + uint32_t nLogStart = S.Frames[nFrameIndex].nLogStart[j]; + uint32_t nLogEnd = S.Frames[nFrameIndexNext].nLogStart[j]; + + MicroProfilePrintString(CB, Handle, "["); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) + { + uint32_t nLogType = MicroProfileLogType(pLog->Log[k]); + if(nLogType == MP_LOG_LABEL) { - uint32_t nLogType = MicroProfileLogType(pLog->Log[k]); - if(nLogType == MP_LOG_META) + uint64_t nLabel = MicroProfileLogGetTick(pLog->Log[k]); + const char* pLabelName = MicroProfileGetLabel(nLabel); + + if(pLabelName) { - //for meta, store the count + 2, which is the tick part - nLogType = 2 + MicroProfileLogGetTick(pLog->Log[k]); + MicroProfilePrintString(CB, Handle, "\""); + MicroProfilePrintString(CB, Handle, pLabelName); + MicroProfilePrintString(CB, Handle, "\","); } - MicroProfilePrintf(CB, Handle, ",%d", nLogType); + else + MicroProfilePrintString(CB, Handle, "null,"); } } - MicroProfilePrintf(CB, Handle, "];\n"); - - MicroProfilePrintf(CB, Handle, "var ti_%d_%d = [", i, j); - if(nLogStart != nLogEnd) - { - uint32_t k = nLogStart; - MicroProfilePrintf(CB, Handle, "%d", (uint32_t)MicroProfileLogTimerIndex(pLog->Log[k])); - for(k = (k+1) % MICROPROFILE_BUFFER_SIZE; k != nLogEnd; k = (k+1) % MICROPROFILE_BUFFER_SIZE) - { - MicroProfilePrintf(CB, Handle, ",%d", (uint32_t)MicroProfileLogTimerIndex(pLog->Log[k])); - } - } - MicroProfilePrintf(CB, Handle, "];\n"); - + MicroProfilePrintString(CB, Handle, "],\n"); } - - MicroProfilePrintf(CB, Handle, "var ts%d = [", i); - for(uint32_t j = 0; j < S.nNumLogs; ++j) - { - MicroProfilePrintf(CB, Handle, "ts_%d_%d,", i, j); - } - MicroProfilePrintf(CB, Handle, "];\n"); - MicroProfilePrintf(CB, Handle, "var tt%d = [", i); - for(uint32_t j = 0; j < S.nNumLogs; ++j) - { - MicroProfilePrintf(CB, Handle, "tt_%d_%d,", i, j); - } - MicroProfilePrintf(CB, Handle, "];\n"); - - MicroProfilePrintf(CB, Handle, "var ti%d = [", i); - for(uint32_t j = 0; j < S.nNumLogs; ++j) - { - MicroProfilePrintf(CB, Handle, "ti_%d_%d,", i, j); - } - MicroProfilePrintf(CB, Handle, "];\n"); - + MicroProfilePrintString(CB, Handle, "];\n"); int64_t nFrameStart = S.Frames[nFrameIndex].nFrameStartCpu; int64_t nFrameEnd = S.Frames[nFrameIndexNext].nFrameStartCpu; + int64_t nFrameStartGpu = S.Frames[nFrameIndex].nFrameStartGpu; + int64_t nFrameEndGpu = S.Frames[nFrameIndexNext].nFrameStartGpu; - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMs = MicroProfileTickToMsMultiplier(nTicksPerSecondCpu); float fFrameMs = MicroProfileLogTickDifference(nTickStart, nFrameStart) * fToMs; float fFrameEndMs = MicroProfileLogTickDifference(nTickStart, nFrameEnd) * fToMs; - MicroProfilePrintf(CB, Handle, "Frames[%d] = MakeFrame(%d, %f, %f, ts%d, tt%d, ti%d);\n", i, nFirstFrameIndex, fFrameMs,fFrameEndMs, i, i, i); + float fFrameGpuMs = MicroProfileLogTickDifference(nTickStartGpu, nFrameStartGpu) * fToMsGPU; + float fFrameGpuEndMs = MicroProfileLogTickDifference(nTickStartGpu, nFrameEndGpu) * fToMsGPU; + + MicroProfilePrintf(CB, Handle, "Frames[%d] = MakeFrame(%d, %f, %f, %f, %f, ts%d, tt%d, ti%d, tl%d);\n", i, 0, fFrameMs, fFrameEndMs, fFrameGpuMs, fFrameGpuEndMs, i, i, i, i); } - CB(Handle, g_MicroProfileHtml_end_size-1, &g_MicroProfileHtml_end[0]); + + uint32_t nContextSwitchStart = 0; + uint32_t nContextSwitchEnd = 0; + MicroProfileContextSwitchSearch(&nContextSwitchStart, &nContextSwitchEnd, nTickStart, nTickEnd); + + MicroProfilePrintString(CB, Handle, "var CSwitchThreadInOutCpu = [\n"); + for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) + { + MicroProfileContextSwitch CS = S.ContextSwitch[j]; + int nCpu = CS.nCpu; + MicroProfilePrintUIntComma(CB, Handle, CS.nThreadIn); + MicroProfilePrintUIntComma(CB, Handle, CS.nThreadOut); + MicroProfilePrintUIntComma(CB, Handle, nCpu); + } + MicroProfilePrintString(CB, Handle, "];\n"); + + MicroProfilePrintString(CB, Handle, "var CSwitchTime = [\n"); + float fToMsCpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) + { + MicroProfileContextSwitch CS = S.ContextSwitch[j]; + float fTime = MicroProfileLogTickDifference(nTickStart, CS.nTicks) * fToMsCpu; + MicroProfilePrintf(CB, Handle, "%f,", fTime); + } + MicroProfilePrintString(CB, Handle, "];\n"); + + MicroProfileThreadInfo Threads[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS]; + uint32_t nNumThreadsBase = 0; + uint32_t nNumThreads = MicroProfileContextSwitchGatherThreads(nContextSwitchStart, nContextSwitchEnd, Threads, &nNumThreadsBase); + + MicroProfilePrintString(CB, Handle, "var CSwitchThreads = {"); + + for (uint32_t i = 0; i < nNumThreads; ++i) + { + char Name[256]; + const char* pProcessName = MicroProfileGetProcessName(Threads[i].nProcessId, Name, sizeof(Name)); + + const char* p1 = i < nNumThreadsBase ? S.Pool[i]->ThreadName : "?"; + const char* p2 = pProcessName ? pProcessName : "?"; + + MicroProfilePrintf(CB, Handle, "%lld:{\'tid\':%lld,\'pid\':%lld,\'t\':\'%s\',\'p\':\'%s\'},", + (long long)Threads[i].nThreadId, + (long long)Threads[i].nThreadId, + (long long)Threads[i].nProcessId, + p1, p2 + ); + } + + MicroProfilePrintString(CB, Handle, "};\n"); + + for(size_t i = 0; i < g_MicroProfileHtml_end_count; ++i) + { + CB(Handle, g_MicroProfileHtml_end_sizes[i]-1, g_MicroProfileHtml_end[i]); + } + + uint32_t* nGroupCounter = (uint32_t*)alloca(sizeof(uint32_t)* S.nGroupCount); + + memset(nGroupCounter, 0, sizeof(uint32_t) * S.nGroupCount); + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + uint32_t nGroupIndex = S.TimerInfo[i].nGroupIndex; + nGroupCounter[nGroupIndex] += nTimerCounter[i]; + } + + uint32_t* nGroupCounterSort = (uint32_t*)alloca(sizeof(uint32_t)* S.nGroupCount); + uint32_t* nTimerCounterSort = (uint32_t*)alloca(sizeof(uint32_t)* S.nTotalTimers); + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + nGroupCounterSort[i] = i; + } + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + nTimerCounterSort[i] = i; + } + std::sort(nGroupCounterSort, nGroupCounterSort + S.nGroupCount, + [nGroupCounter](const uint32_t l, const uint32_t r) + { + return nGroupCounter[l] > nGroupCounter[r]; + } + ); + + std::sort(nTimerCounterSort, nTimerCounterSort + S.nTotalTimers, + [nTimerCounter](const uint32_t l, const uint32_t r) + { + return nTimerCounter[l] > nTimerCounter[r]; + } + ); + + MicroProfilePrintf(CB, Handle, "\n\n"); + + S.nActiveGroup = nActiveGroup; + S.nRunning = nRunning; + +#if MICROPROFILE_DEBUG + int64_t nTicksEnd = MP_TICK(); + float fMs = fToMsCpu * (nTicksEnd - S.nPauseTicks); + printf("html dump took %6.2fms\n", fMs); +#endif } +#else +void MicroProfileDumpHtml(MicroProfileWriteCallback CB, void* Handle, int nMaxFrames, const char* pHost) +{ + MicroProfilePrintString(CB, Handle, "HTML output is disabled because MICROPROFILE_EMBED_HTML is 0\n"); +} +#endif void MicroProfileWriteFile(void* Handle, size_t nSize, const char* pData) { fwrite(pData, nSize, 1, (FILE*)Handle); } -void MicroProfileDumpHtmlToFile() +void MicroProfileDumpToFile() { std::lock_guard Lock(MicroProfileMutex()); - FILE* F = fopen(S.HtmlDumpPath, "w"); + + FILE* F = fopen(S.DumpPath, "w"); if(F) { - MicroProfileDumpHtml(MicroProfileWriteFile, F, MICROPROFILE_WEBSERVER_MAXFRAMES); + if(S.eDumpType == MicroProfileDumpTypeHtml) + MicroProfileDumpHtml(MicroProfileWriteFile, F, S.nDumpFrames, 0); + else if(S.eDumpType == MicroProfileDumpTypeCsv) + MicroProfileDumpCsv(MicroProfileWriteFile, F, S.nDumpFrames); + fclose(F); } } -static uint64_t g_nMicroProfileDataSent = 0; -void MicroProfileWriteSocket(void* Handle, size_t nSize, const char* pData) +#if MICROPROFILE_WEBSERVER +uint32_t MicroProfileWebServerPort() { - g_nMicroProfileDataSent += nSize; - send(*(MpSocket*)Handle, pData, int(nSize), 0); + return S.nWebServerPort; } - -#ifndef MicroProfileSetNonBlocking //fcntl doesnt work on a some unix like platforms.. -void MicroProfileSetNonBlocking(MpSocket Socket, int NonBlocking) +void MicroProfileSendSocket(MpSocket Socket, const char* pData, size_t nSize) { -#ifdef _WIN32 - u_long nonBlocking = NonBlocking ? 1 : 0; - ioctlsocket(Socket, FIONBIO, &nonBlocking); +#ifdef MSG_NOSIGNAL + int nFlags = MSG_NOSIGNAL; #else - int Options = fcntl(Socket, F_GETFL); - if(NonBlocking) + int nFlags = 0; +#endif + + send(Socket, pData, (int)nSize, nFlags); +} + +void MicroProfileFlushSocket(MpSocket Socket) +{ + if(S.nWebServerPut) { - fcntl(Socket, F_SETFL, Options|O_NONBLOCK); + MicroProfileSendSocket(Socket, &S.WebServerBuffer[0], S.nWebServerPut); + S.nWebServerPut = 0; + } +} + +void MicroProfileWriteSocket(void* Handle, size_t nSize, const char* pData) +{ + MpSocket Socket = *(MpSocket*)Handle; + if(nSize > MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE / 2) + { + MicroProfileFlushSocket(Socket); + MicroProfileSendSocket(Socket, pData, nSize); } else { - fcntl(Socket, F_SETFL, Options&(~O_NONBLOCK)); + memcpy(&S.WebServerBuffer[S.nWebServerPut], pData, nSize); + S.nWebServerPut += (uint32_t)nSize; + if(S.nWebServerPut > MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE/2) + { + MicroProfileFlushSocket(Socket); + } } + + S.nWebServerDataSent += nSize; +} + +#if MICROPROFILE_MINIZ +#ifndef MICROPROFILE_COMPRESS_BUFFER_SIZE +#define MICROPROFILE_COMPRESS_BUFFER_SIZE (256<<10) #endif + +#define MICROPROFILE_COMPRESS_CHUNK (MICROPROFILE_COMPRESS_BUFFER_SIZE/2) +struct MicroProfileCompressedSocketState +{ + unsigned char DeflateOut[MICROPROFILE_COMPRESS_CHUNK]; + unsigned char DeflateIn[MICROPROFILE_COMPRESS_CHUNK]; + mz_stream Stream; + MpSocket Socket; + uint32_t nSize; + uint32_t nCompressedSize; + uint32_t nFlushes; + uint32_t nMemmoveBytes; +}; + +void MicroProfileCompressedSocketFlush(MicroProfileCompressedSocketState* pState) +{ + mz_stream& Stream = pState->Stream; + unsigned char* pSendStart = &pState->DeflateOut[0]; + unsigned char* pSendEnd = &pState->DeflateOut[MICROPROFILE_COMPRESS_CHUNK - Stream.avail_out]; + if(pSendStart != pSendEnd) + { + MicroProfileSendSocket(pState->Socket, (char*)pSendStart, pSendEnd - pSendStart); + pState->nCompressedSize += pSendEnd - pSendStart; + } + Stream.next_out = &pState->DeflateOut[0]; + Stream.avail_out = MICROPROFILE_COMPRESS_CHUNK; + +} +void MicroProfileCompressedSocketStart(MicroProfileCompressedSocketState* pState, MpSocket Socket) +{ + mz_stream& Stream = pState->Stream; + memset(&Stream, 0, sizeof(Stream)); + Stream.next_out = &pState->DeflateOut[0]; + Stream.avail_out = MICROPROFILE_COMPRESS_CHUNK; + Stream.next_in = &pState->DeflateIn[0]; + Stream.avail_in = 0; + mz_deflateInit(&Stream, MZ_DEFAULT_COMPRESSION); + pState->Socket = Socket; + pState->nSize = 0; + pState->nCompressedSize = 0; + pState->nFlushes = 0; + pState->nMemmoveBytes = 0; + +} +void MicroProfileCompressedSocketFinish(MicroProfileCompressedSocketState* pState) +{ + mz_stream& Stream = pState->Stream; + MicroProfileCompressedSocketFlush(pState); + int r = mz_deflate(&Stream, MZ_FINISH); + MP_ASSERT(r == MZ_STREAM_END); + MicroProfileCompressedSocketFlush(pState); + r = mz_deflateEnd(&Stream); + MP_ASSERT(r == MZ_OK); +} + +void MicroProfileCompressedWriteSocket(void* Handle, size_t nSize, const char* pData) +{ + MicroProfileCompressedSocketState* pState = (MicroProfileCompressedSocketState*)Handle; + mz_stream& Stream = pState->Stream; + const unsigned char* pDeflateInEnd = Stream.next_in + Stream.avail_in; + const unsigned char* pDeflateInStart = &pState->DeflateIn[0]; + const unsigned char* pDeflateInRealEnd = &pState->DeflateIn[MICROPROFILE_COMPRESS_CHUNK]; + pState->nSize += nSize; + if(nSize <= pDeflateInRealEnd - pDeflateInEnd) + { + memcpy((void*)pDeflateInEnd, pData, nSize); + Stream.avail_in += nSize; + MP_ASSERT(Stream.next_in + Stream.avail_in <= pDeflateInRealEnd); + return; + } + int Flush = 0; + while(nSize) + { + pDeflateInEnd = Stream.next_in + Stream.avail_in; + if(Flush) + { + pState->nFlushes++; + MicroProfileCompressedSocketFlush(pState); + pDeflateInRealEnd = &pState->DeflateIn[MICROPROFILE_COMPRESS_CHUNK]; + if(pDeflateInEnd == pDeflateInRealEnd) + { + if(Stream.avail_in) + { + MP_ASSERT(pDeflateInStart != Stream.next_in); + memmove((void*)pDeflateInStart, Stream.next_in, Stream.avail_in); + pState->nMemmoveBytes += Stream.avail_in; + } + Stream.next_in = pDeflateInStart; + pDeflateInEnd = Stream.next_in + Stream.avail_in; + } + } + size_t nSpace = pDeflateInRealEnd - pDeflateInEnd; + size_t nBytes = MicroProfileMin(nSpace, nSize); + MP_ASSERT(nBytes + pDeflateInEnd <= pDeflateInRealEnd); + memcpy((void*)pDeflateInEnd, pData, nBytes); + Stream.avail_in += nBytes; + nSize -= nBytes; + pData += nBytes; + int r = mz_deflate(&Stream, MZ_NO_FLUSH); + Flush = r == MZ_BUF_ERROR || nBytes == 0 || Stream.avail_out == 0 ? 1 : 0; + MP_ASSERT(r == MZ_BUF_ERROR || r == MZ_OK); + if(r == MZ_BUF_ERROR) + { + r = mz_deflate(&Stream, MZ_SYNC_FLUSH); + } + } } #endif -void MicroProfileWebServerStart() +void* MicroProfileWebServerUpdate(void*); +void MicroProfileWebServerUpdateStop(); + +void MicroProfileWebServerHello(int nPort) { -#ifdef _WIN32 - WSADATA wsa; - if(WSAStartup(MAKEWORD(2, 2), &wsa)) + uint32_t nInterfaces = 0; + + // getifaddrs hangs on some versions of Android so disable IP address scanning +#if (defined(__APPLE__) || defined(__linux__)) && !defined(__ANDROID__) + struct ifaddrs* ifal; + if(getifaddrs(&ifal) == 0 && ifal) { - S.ListenerSocket = -1; - return; + for(struct ifaddrs* ifa = ifal; ifa; ifa = ifa->ifa_next) + { + if(ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) + { + void* pAddress = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; + char Ip[INET_ADDRSTRLEN]; + if(inet_ntop(AF_INET, pAddress, Ip, sizeof(Ip))) + { + MICROPROFILE_PRINTF("MicroProfile: Web server started on %s:%d\n", Ip, nPort); + nInterfaces++; + } + } + } + + freeifaddrs(ifal); } #endif - S.ListenerSocket = socket(PF_INET, SOCK_STREAM, 6); - MP_ASSERT(!MP_INVALID_SOCKET(S.ListenerSocket)); - MicroProfileSetNonBlocking(S.ListenerSocket, 1); - - S.nWebServerPort = (uint32_t)-1; - struct sockaddr_in Addr; - Addr.sin_family = AF_INET; - Addr.sin_addr.s_addr = INADDR_ANY; - for(int i = 0; i < 20; ++i) + if(nInterfaces == 0) { - Addr.sin_port = htons(MICROPROFILE_WEBSERVER_PORT+i); - if(0 == bind(S.ListenerSocket, (sockaddr*)&Addr, sizeof(Addr))) - { - S.nWebServerPort = MICROPROFILE_WEBSERVER_PORT+i; - break; - } + MICROPROFILE_PRINTF("MicroProfile: Web server started on port %d\n", nPort); + } +} + +void MicroProfileWebServerStart() +{ + if(!S.WebServerThread) + { + MicroProfileThreadStart(&S.WebServerThread, MicroProfileWebServerUpdate); } - listen(S.ListenerSocket, 8); } void MicroProfileWebServerStop() { -#ifdef _WIN32 - closesocket(S.ListenerSocket); - WSACleanup(); -#else - close(S.ListenerSocket); -#endif -} -bool MicroProfileWebServerUpdate() -{ - MICROPROFILE_SCOPEI("MicroProfile", "Webserver-update", -1); - MpSocket Connection = accept(S.ListenerSocket, 0, 0); - bool bServed = false; - if(!MP_INVALID_SOCKET(Connection)) + if(S.WebServerThread) { - int timeout = 100; - setsockopt(Connection, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); - - std::lock_guard Lock(MicroProfileMutex()); - char Req[8192]; - MicroProfileSetNonBlocking(Connection, 0); - int nReceived = recv(Connection, Req, sizeof(Req)-1, 0); - if(nReceived > 0) - { - Req[nReceived] = '\0'; -#if MICROPROFILE_DEBUG - printf("got request \n%s\n", Req); -#endif -#define MICROPROFILE_HTML_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" - char* pHttp = strstr(Req, "HTTP/"); - char* pGet = strstr(Req, "GET / "); - char* pGetParam = strstr(Req, "GET /?"); - if(pHttp && (pGet || pGetParam)) - { - int nMaxFrames = MICROPROFILE_WEBSERVER_MAXFRAMES; - if(pGetParam) - { - *pHttp = '\0'; - pGetParam += sizeof("GET /?")-1; - while(pGetParam) //split url pairs foo=bar&lala=lele etc - { - char* pSplit = strstr(pGetParam, "&"); - if(pSplit) - { - *pSplit++ = '\0'; - } - char* pKey = pGetParam; - char* pValue = strstr(pGetParam, "="); - if(pValue) - { - *pValue++ = '\0'; - } - if(0 == MP_STRCASECMP(pKey, "frames")) - { - if(pValue) - { - nMaxFrames = atoi(pValue); - } - } - pGetParam = pSplit; - } - } - uint64_t nTickStart = MP_TICK(); - send(Connection, MICROPROFILE_HTML_HEADER, sizeof(MICROPROFILE_HTML_HEADER)-1, 0); - uint64_t nDataStart = g_nMicroProfileDataSent; - MicroProfileDumpHtml(MicroProfileWriteSocket, &Connection, nMaxFrames); - uint64_t nDataEnd = g_nMicroProfileDataSent; - uint64_t nTickEnd = MP_TICK(); - float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * (nTickEnd - nTickStart); - MicroProfilePrintf(MicroProfileWriteSocket, &Connection, "\n\n\n",((nDataEnd-nDataStart)>>10) + 1, fMs); -#if MICROPROFILE_DEBUG - printf("\nSent %lldkb, in %6.3fms\n\n", ((nDataEnd-nDataStart)>>10) + 1, fMs); -#endif - bServed = true; - } - } -#ifdef _WIN32 - closesocket(Connection); -#else - close(Connection); -#endif + MicroProfileWebServerUpdateStop(); + MicroProfileThreadJoin(&S.WebServerThread); } - return bServed; +} + +const char* MicroProfileParseHeader(char* pRequest, const char* pPrefix) +{ + size_t nRequestSize = strlen(pRequest); + size_t nPrefixSize = strlen(pPrefix); + + for(uint32_t i = 0; i < nRequestSize; ++i) + { + if((i == 0 || pRequest[i-1] == '\n') && strncmp(&pRequest[i], pPrefix, nPrefixSize) == 0) + { + char* pResult = &pRequest[i + nPrefixSize]; + size_t nResultSize = strcspn(pResult, " \r\n"); + + pResult[nResultSize] = '\0'; + return pResult; + } + } + + return 0; +} + +int MicroProfileParseGet(const char* pGet) +{ + const char* pStart = pGet; + while(*pGet != '\0') + { + if(*pGet < '0' || *pGet > '9') + return 0; + pGet++; + } + int nFrames = atoi(pStart); + if(nFrames) + { + return nFrames; + } + else + { + return MICROPROFILE_WEBSERVER_FRAMES; + } +} + +void MicroProfileWebServerHandleRequest(MpSocket Connection) +{ + char Request[8192]; + int nReceived = recv(Connection, Request, sizeof(Request)-1, 0); + if(nReceived <= 0) + return; + Request[nReceived] = 0; + + std::lock_guard Lock(MicroProfileMutex()); + + MICROPROFILE_SCOPE(g_MicroProfileWebServerUpdate); + +#if MICROPROFILE_MINIZ +#define MICROPROFILE_HTML_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Encoding: deflate\r\nExpires: Tue, 01 Jan 2199 16:00:00 GMT\r\n\r\n" +#else +#define MICROPROFILE_HTML_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nExpires: Tue, 01 Jan 2199 16:00:00 GMT\r\n\r\n" +#endif + + const char* pUrl = MicroProfileParseHeader(Request, "GET /"); + if(!pUrl) + return; + + int nFrames = MicroProfileParseGet(pUrl); + if(nFrames <= 0) + return; + + const char* pHost = MicroProfileParseHeader(Request, "Host: "); + + uint64_t nTickStart = MP_TICK(); + MicroProfileSendSocket(Connection, MICROPROFILE_HTML_HEADER, sizeof(MICROPROFILE_HTML_HEADER)-1); + uint64_t nDataStart = S.nWebServerDataSent; + S.nWebServerPut = 0; +#if 0 == MICROPROFILE_MINIZ + MicroProfileDumpHtml(MicroProfileWriteSocket, &Connection, nFrames, pHost); + uint64_t nDataEnd = S.nWebServerDataSent; + uint64_t nTickEnd = MP_TICK(); + uint64_t nDiff = (nTickEnd - nTickStart); + float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * nDiff; + int nKb = ((nDataEnd-nDataStart)>>10) + 1; + MicroProfilePrintf(MicroProfileWriteSocket, &Connection, "\n\n\n",nKb, fMs); + MicroProfileFlushSocket(Connection); +#else + MicroProfileCompressedSocketState CompressState; + MicroProfileCompressedSocketStart(&CompressState, Connection); + MicroProfileDumpHtml(MicroProfileCompressedWriteSocket, &CompressState, nFrames, pHost); + S.nWebServerDataSent += CompressState.nSize; + uint64_t nDataEnd = S.nWebServerDataSent; + uint64_t nTickEnd = MP_TICK(); + uint64_t nDiff = (nTickEnd - nTickStart); + float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * nDiff; + int nKb = ((nDataEnd-nDataStart)>>10) + 1; + int nCompressedKb = ((CompressState.nCompressedSize)>>10) + 1; + MicroProfilePrintf(MicroProfileCompressedWriteSocket, &CompressState, "\n\n\n", nKb, nCompressedKb, fMs); + MicroProfileCompressedSocketFinish(&CompressState); + MicroProfileFlushSocket(Connection); +#endif +} + +void MicroProfileWebServerCloseSocket(MpSocket Connection) +{ +#ifdef _WIN32 + closesocket(Connection); +#else + shutdown(Connection,SHUT_RDWR); + close(Connection); +#endif +} + +void* MicroProfileWebServerUpdate(void*) +{ +#ifdef _WIN32 + WSADATA wsa; + if(WSAStartup(MAKEWORD(2, 2), &wsa)) + return 0; +#endif + + S.WebServerSocket = socket(PF_INET, SOCK_STREAM, 6); + MP_ASSERT(!MP_INVALID_SOCKET(S.WebServerSocket)); + + uint32_t nPortBegin = MICROPROFILE_WEBSERVER_PORT; + uint32_t nPortEnd = nPortBegin + 20; + + struct sockaddr_in Addr; + Addr.sin_family = AF_INET; + Addr.sin_addr.s_addr = INADDR_ANY; + for(uint32_t nPort = nPortBegin; nPort < nPortEnd; ++nPort) + { + Addr.sin_port = htons(nPort); + if(0 == ::bind(S.WebServerSocket, (sockaddr*)&Addr, sizeof(Addr))) + { + S.nWebServerPort = nPort; + break; + } + } + + if(S.nWebServerPort) + { + MicroProfileWebServerHello(S.nWebServerPort); + + listen(S.WebServerSocket, 8); + + for (;;) + { + MpSocket Connection = accept(S.WebServerSocket, 0, 0); + if(MP_INVALID_SOCKET(Connection)) break; + + #ifdef SO_NOSIGPIPE + int nConnectionOption = 1; + setsockopt(Connection, SOL_SOCKET, SO_NOSIGPIPE, &nConnectionOption, sizeof(nConnectionOption)); + #endif + + MicroProfileWebServerHandleRequest(Connection); + + MicroProfileWebServerCloseSocket(Connection); + } + + S.nWebServerPort = 0; + } + else + { + MICROPROFILE_PRINTF("MicroProfile: Web server could not start: no free ports in range [%d..%d)\n", nPortBegin, nPortEnd); + } + +#ifdef _WIN32 + WSACleanup(); +#endif + + return 0; +} + +void MicroProfileWebServerUpdateStop() +{ + MicroProfileWebServerCloseSocket(S.WebServerSocket); +} +#else +void MicroProfileWebServerStart() +{ +} + +void MicroProfileWebServerStop() +{ +} + +uint32_t MicroProfileWebServerPort() +{ + return 0; } #endif - - #if MICROPROFILE_CONTEXT_SWITCH_TRACE -#ifdef _WIN32 -#define INITGUID -#include -#include -#include +//functions that need to be implemented per platform. +void* MicroProfileTraceThread(void* unused); - -static GUID g_MicroProfileThreadClassGuid = { 0x3d6fa8d1, 0xfe05, 0x11d0, {0x9d, 0xda, 0x00, 0xc0, 0x4f, 0xd7, 0xba, 0x7c } }; - -struct MicroProfileSCSwitch +void MicroProfileContextSwitchTraceStart() { - uint32_t NewThreadId; - uint32_t OldThreadId; - int8_t NewThreadPriority; - int8_t OldThreadPriority; - uint8_t PreviousCState; - int8_t SpareByte; - int8_t OldThreadWaitReason; - int8_t OldThreadWaitMode; - int8_t OldThreadState; - int8_t OldThreadWaitIdealProcessor; - uint32_t NewThreadWaitTime; - uint32_t Reserved; -}; - - -VOID WINAPI MicroProfileContextSwitchCallback(PEVENT_TRACE pEvent) -{ - if (pEvent->Header.Guid == g_MicroProfileThreadClassGuid) + if(!S.ContextSwitchThread) { - if (pEvent->Header.Class.Type == 36) + MicroProfileThreadStart(&S.ContextSwitchThread, MicroProfileTraceThread); + } +} + +void MicroProfileContextSwitchTraceStop() +{ + if(S.ContextSwitchThread) + { + S.bContextSwitchStop = true; + MicroProfileThreadJoin(&S.ContextSwitchThread); + S.bContextSwitchStop = false; + } +} + +void MicroProfileContextSwitchSearch(uint32_t* pContextSwitchStart, uint32_t* pContextSwitchEnd, uint64_t nBaseTicksCpu, uint64_t nBaseTicksEndCpu) +{ + MICROPROFILE_SCOPE(g_MicroProfileContextSwitchSearch); + uint32_t nContextSwitchPut = S.nContextSwitchPut; + uint64_t nContextSwitchStart, nContextSwitchEnd; + nContextSwitchStart = nContextSwitchEnd = (nContextSwitchPut + MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - 1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; + int64_t nSearchEnd = nBaseTicksEndCpu + MicroProfileMsToTick(30.f, MicroProfileTicksPerSecondCpu()); + int64_t nSearchBegin = nBaseTicksCpu - MicroProfileMsToTick(30.f, MicroProfileTicksPerSecondCpu()); + for(uint32_t i = 0; i < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; ++i) + { + uint32_t nIndex = (nContextSwitchPut + MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - (i+1)) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; + MicroProfileContextSwitch& CS = S.ContextSwitch[nIndex]; + if(CS.nTicks > nSearchEnd) { - MicroProfileSCSwitch* pCSwitch = (MicroProfileSCSwitch*) pEvent->MofData; - if ((pCSwitch->NewThreadId != 0) || (pCSwitch->OldThreadId != 0)) + nContextSwitchEnd = nIndex; + } + if(CS.nTicks > nSearchBegin) + { + nContextSwitchStart = nIndex; + } + } + *pContextSwitchStart = nContextSwitchStart; + *pContextSwitchEnd = nContextSwitchEnd; +} + +uint32_t MicroProfileContextSwitchGatherThreads(uint32_t nContextSwitchStart, uint32_t nContextSwitchEnd, MicroProfileThreadInfo* Threads, uint32_t* nNumThreadsBase) +{ + MicroProfileProcessIdType nCurrentProcessId = MP_GETCURRENTPROCESSID(); + + uint32_t nNumThreads = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS && S.Pool[i]; ++i) + { + Threads[nNumThreads].nProcessId = nCurrentProcessId; + Threads[nNumThreads].nThreadId = S.Pool[i]->nThreadId; + nNumThreads++; + } + + *nNumThreadsBase = nNumThreads; + + for(uint32_t i = nContextSwitchStart; i != nContextSwitchEnd; i = (i+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) + { + MicroProfileContextSwitch CS = S.ContextSwitch[i]; + MicroProfileThreadIdType nThreadId = CS.nThreadIn; + if(nThreadId) + { + MicroProfileProcessIdType nProcessId = CS.nProcessIn; + + bool bSeen = false; + for(uint32_t j = 0; j < nNumThreads; ++j) + { + if(Threads[j].nThreadId == nThreadId && Threads[j].nProcessId == nProcessId) + { + bSeen = true; + break; + } + } + if(!bSeen) + { + Threads[nNumThreads].nProcessId = nProcessId; + Threads[nNumThreads].nThreadId = nThreadId; + nNumThreads++; + } + } + if(nNumThreads == MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS) + { + break; + } + } + + return nNumThreads; +} + +#if defined(_WIN32) +#include +#pragma comment(lib, "psapi.lib") + +const char* MicroProfileGetProcessName(MicroProfileProcessIdType nId, char* Buffer, uint32_t nSize) +{ + if(HANDLE Handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, nId)) + { + DWORD nResult = GetModuleBaseNameA(Handle, nullptr, Buffer, nSize); + CloseHandle(Handle); + + return nResult ? Buffer : nullptr; + } + return nullptr; +} + +void* MicroProfileTraceThread(void* unused) +{ + while(!S.bContextSwitchStop) + { + FILE* pFile = fopen("\\\\.\\pipe\\microprofile-contextswitch", "rb"); + if(!pFile) + { + Sleep(1000); + continue; + } + + S.bContextSwitchRunning = true; + + MicroProfileContextSwitch Buffer[1024]; + + while(!ferror(pFile) && !S.bContextSwitchStop) + { + size_t nCount = fread(Buffer, sizeof(MicroProfileContextSwitch), ARRAYSIZE(Buffer), pFile); + + for(size_t i = 0; i < nCount; ++i) + MicroProfileContextSwitchPut(&Buffer[i]); + } + + fclose(pFile); + + S.bContextSwitchRunning = false; + } + + return 0; +} +#elif defined(__APPLE__) +#include +#include + +const char* MicroProfileGetProcessName(MicroProfileProcessIdType nId, char* Buffer, uint32_t nSize) +{ + char Path[PATH_MAX]; + if(proc_pidpath(nId, Path, sizeof(Path)) == 0) + return nullptr; + + char* pSlash = strrchr(Path, '/'); + char* pName = pSlash ? pSlash + 1 : Path; + + strncpy(Buffer, pName, nSize-1); + Buffer[nSize-1] = 0; + + return Buffer; +} + +void* MicroProfileTraceThread(void*) +{ + while(!S.bContextSwitchStop) + { + FILE* pFile = fopen("/tmp/microprofile-contextswitch", "r"); + if(!pFile) + { + usleep(1000000); + continue; + } + + S.bContextSwitchRunning = true; + + char* pLine = 0; + size_t cap = 0; + size_t len = 0; + + uint32_t nLastThread[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS] = {0}; + + while((len = getline(&pLine, &cap, pFile))>0 && !S.bContextSwitchStop) + { + if(strncmp(pLine, "MPTD ", 5) != 0) + continue; + + char* pos = pLine + 4; + uint32_t cpu = strtol(pos + 1, &pos, 16); + uint32_t pid = strtol(pos + 1, &pos, 16); + uint32_t tid = strtol(pos + 1, &pos, 16); + int64_t timestamp = strtoll(pos + 1, &pos, 16); + + if(cpu < MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS) { MicroProfileContextSwitch Switch; - Switch.nThreadOut = pCSwitch->OldThreadId; - Switch.nThreadIn = pCSwitch->NewThreadId; - Switch.nCpu = pEvent->BufferContext.ProcessorNumber; - Switch.nTicks = pEvent->Header.TimeStamp.QuadPart; + + Switch.nThreadOut = nLastThread[cpu]; + Switch.nThreadIn = tid; + Switch.nProcessIn = pid; + Switch.nCpu = cpu; + Switch.nTicks = timestamp; MicroProfileContextSwitchPut(&Switch); + + nLastThread[cpu] = tid; + } + } + + fclose(pFile); + + S.bContextSwitchRunning = false; + } + + return 0; +} +#endif +#else +void MicroProfileContextSwitchTraceStart() +{ +} + +void MicroProfileContextSwitchTraceStop() +{ +} + +void MicroProfileContextSwitchSearch(uint32_t* pContextSwitchStart, uint32_t* pContextSwitchEnd, uint64_t nBaseTicksCpu, uint64_t nBaseTicksEndCpu) +{ + (void)nBaseTicksCpu; + (void)nBaseTicksEndCpu; + + *pContextSwitchStart = 0; + *pContextSwitchEnd = 0; +} + +uint32_t MicroProfileContextSwitchGatherThreads(uint32_t nContextSwitchStart, uint32_t nContextSwitchEnd, MicroProfileThreadInfo* Threads, uint32_t* nNumThreadsBase) +{ + (void)nContextSwitchStart; + (void)nContextSwitchEnd; + (void)Threads; + + *nNumThreadsBase = 0; + return 0; +} + +const char* MicroProfileGetProcessName(MicroProfileProcessIdType nId, char* Buffer, uint32_t nSize) +{ + (void)nId; + (void)Buffer; + (void)nSize; + + return nullptr; +} +#endif + +void MicroProfileGpuShutdown() +{ + if(!S.GPU.Shutdown) + return; + + S.GPU.Shutdown(); + + memset(&S.GPU, 0, sizeof(S.GPU)); +} + +uint32_t MicroProfileGpuFlip() +{ + if(!S.GPU.Flip) + return (uint32_t)-1; + + return S.GPU.Flip(); +} + +uint32_t MicroProfileGpuInsertTimer(void* pContext) +{ + if(!S.GPU.InsertTimer) + return (uint32_t)-1; + + return S.GPU.InsertTimer(pContext); +} + +uint64_t MicroProfileGpuGetTimeStamp(uint32_t nIndex) +{ + if(!S.GPU.GetTimeStamp) + return MICROPROFILE_INVALID_TICK; + + if(nIndex == (uint32_t)-1) + return MICROPROFILE_INVALID_TICK; + + return S.GPU.GetTimeStamp(nIndex); +} + +uint64_t MicroProfileTicksPerSecondGpu() +{ + if (!S.GPU.GetTicksPerSecond) + return 1000000000ll; + + return S.GPU.GetTicksPerSecond(); +} + +bool MicroProfileGetGpuTickReference(int64_t* pOutCpu, int64_t* pOutGpu) +{ + if(!S.GPU.GetTickReference) + return false; + + return S.GPU.GetTickReference(pOutCpu, pOutGpu); +} + +#define MICROPROFILE_GPU_STATE_DECL(API) \ + void MicroProfileGpuInitState##API(); \ + MicroProfileGpuTimerState##API g_MicroProfileGPU_##API; + +#define MICROPROFILE_GPU_STATE_IMPL(API) \ + void MicroProfileGpuInitState##API() \ + { \ + MP_ASSERT(!S.GPU.Shutdown); \ + memset(&g_MicroProfileGPU_##API, 0, sizeof(g_MicroProfileGPU_##API)); \ + S.GPU.Shutdown = MicroProfileGpuShutdown##API; \ + S.GPU.Flip = MicroProfileGpuFlip##API; \ + S.GPU.InsertTimer = MicroProfileGpuInsertTimer##API; \ + S.GPU.GetTimeStamp = MicroProfileGpuGetTimeStamp##API; \ + S.GPU.GetTicksPerSecond = MicroProfileTicksPerSecondGpu##API; \ + S.GPU.GetTickReference = MicroProfileGetGpuTickReference##API; \ + } + +#if MICROPROFILE_GPU_TIMERS_D3D11 +#ifndef D3D11_SDK_VERSION +#include +#endif + +struct MicroProfileGpuTimerStateD3D11 +{ + ID3D11DeviceContext* pDeviceContext; + ID3D11Query* pQueries[MICROPROFILE_GPU_MAX_QUERIES]; + ID3D11Query* pRateQuery; + ID3D11Query* pSyncQuery; + + uint64_t nFrame; + std::atomic nFramePut; + + uint32_t nSubmitted[MICROPROFILE_GPU_FRAMES]; + uint64_t nResults[MICROPROFILE_GPU_MAX_QUERIES]; + + uint32_t nRateQueryIssue; + uint64_t nQueryFrequency; +}; + +MICROPROFILE_GPU_STATE_DECL(D3D11) + +void MicroProfileGpuInitD3D11(ID3D11Device* pDevice) +{ + MicroProfileGpuInitStateD3D11(); + + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + pDevice->GetImmediateContext(&GPU.pDeviceContext); + + D3D11_QUERY_DESC Desc; + Desc.MiscFlags = 0; + Desc.Query = D3D11_QUERY_TIMESTAMP; + for(uint32_t i = 0; i < MICROPROFILE_GPU_MAX_QUERIES; ++i) + { + HRESULT hr = pDevice->CreateQuery(&Desc, &GPU.pQueries[i]); + MP_ASSERT(hr == S_OK); + } + + HRESULT hr = pDevice->CreateQuery(&Desc, &GPU.pSyncQuery); + MP_ASSERT(hr == S_OK); + + Desc.Query = D3D11_QUERY_TIMESTAMP_DISJOINT; + hr = pDevice->CreateQuery(&Desc, &GPU.pRateQuery); + MP_ASSERT(hr == S_OK); +} + +void MicroProfileGpuShutdownD3D11() +{ + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + for(uint32_t i = 0; i < MICROPROFILE_GPU_MAX_QUERIES; ++i) + { + GPU.pQueries[i]->Release(); + GPU.pQueries[i] = 0; + } + + GPU.pRateQuery->Release(); + GPU.pRateQuery = 0; + + GPU.pSyncQuery->Release(); + GPU.pSyncQuery = 0; + + GPU.pDeviceContext->Release(); + GPU.pDeviceContext = 0; +} + +uint32_t MicroProfileGpuFlipD3D11() +{ + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + if (!GPU.pDeviceContext) return (uint32_t)-1; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + // Submit current frame + uint32_t nFrameIndex = GPU.nFrame % MICROPROFILE_GPU_FRAMES; + uint32_t nFramePut = MicroProfileMin(GPU.nFramePut.load(), nFrameQueries); + + GPU.nSubmitted[nFrameIndex] = nFramePut; + GPU.nFramePut.store(0); + GPU.nFrame++; + + // Fetch frame results + if (GPU.nFrame >= MICROPROFILE_GPU_FRAMES) + { + uint64_t nPendingFrame = GPU.nFrame - MICROPROFILE_GPU_FRAMES; + uint32_t nPendingFrameIndex = nPendingFrame % MICROPROFILE_GPU_FRAMES; + + for(uint32_t i = 0; i < GPU.nSubmitted[nPendingFrameIndex]; ++i) + { + uint32_t nQueryIndex = nPendingFrameIndex * nFrameQueries + i; + MP_ASSERT(nQueryIndex < MICROPROFILE_GPU_MAX_QUERIES); + + uint64_t nResult = 0; + + HRESULT hr; + do hr = GPU.pDeviceContext->GetData(GPU.pQueries[nQueryIndex], &nResult, sizeof(nResult), 0); + while(hr == S_FALSE); + + GPU.nResults[nQueryIndex] = (hr == S_OK) ? nResult : MICROPROFILE_INVALID_TICK; + } + } + + // Update timestamp frequency + if(GPU.nRateQueryIssue == 0) + { + GPU.pDeviceContext->Begin(GPU.pRateQuery); + GPU.nRateQueryIssue = 1; + } + else if(GPU.nRateQueryIssue == 1) + { + GPU.pDeviceContext->End(GPU.pRateQuery); + GPU.nRateQueryIssue = 2; + } + else + { + D3D11_QUERY_DATA_TIMESTAMP_DISJOINT Result; + if(S_OK == GPU.pDeviceContext->GetData(GPU.pRateQuery, &Result, sizeof(Result), D3D11_ASYNC_GETDATA_DONOTFLUSH)) + { + GPU.nQueryFrequency = Result.Frequency; + GPU.nRateQueryIssue = 0; + } + } + + return MicroProfileGpuInsertTimer(0); +} + +uint32_t MicroProfileGpuInsertTimerD3D11(void* pContext) +{ + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + uint32_t nIndex = GPU.nFramePut.fetch_add(1); + if(nIndex >= nFrameQueries) + return (uint32_t)-1; + + uint32_t nQueryIndex = (GPU.nFrame % MICROPROFILE_GPU_FRAMES) * nFrameQueries + nIndex; + + GPU.pDeviceContext->End(GPU.pQueries[nQueryIndex]); + + return nQueryIndex; +} + +uint64_t MicroProfileGpuGetTimeStampD3D11(uint32_t nIndex) +{ + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + return GPU.nResults[nIndex]; +} + +uint64_t MicroProfileTicksPerSecondGpuD3D11() +{ + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + return GPU.nQueryFrequency ? GPU.nQueryFrequency : 1000000000ll; +} + +bool MicroProfileGetGpuTickReferenceD3D11(int64_t* pOutCpu, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateD3D11& GPU = g_MicroProfileGPU_D3D11; + + GPU.pDeviceContext->End(GPU.pSyncQuery); + + uint64_t nResult = 0; + + HRESULT hr; + do hr = GPU.pDeviceContext->GetData(GPU.pSyncQuery, &nResult, sizeof(nResult), 0); + while(hr == S_FALSE); + + if (hr != S_OK) return false; + + *pOutCpu = MP_TICK(); + *pOutGpu = nResult; + + return true; +} + +MICROPROFILE_GPU_STATE_IMPL(D3D11) +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D12 +#ifndef D3D12_MAJOR_VERSION +#include +#endif + +struct MicroProfileGpuTimerStateD3D12 +{ + ID3D12CommandQueue* pCommandQueue; + ID3D12QueryHeap* pHeap; + ID3D12Resource* pBuffer; + ID3D12GraphicsCommandList* pCommandLists[MICROPROFILE_GPU_FRAMES]; + ID3D12CommandAllocator* pCommandAllocators[MICROPROFILE_GPU_FRAMES]; + ID3D12Fence* pFence; + void* pFenceEvent; + + uint64_t nFrame; + std::atomic nFramePut; + + uint32_t nSubmitted[MICROPROFILE_GPU_FRAMES]; + uint64_t nResults[MICROPROFILE_GPU_MAX_QUERIES]; + uint64_t nQueryFrequency; +}; + +MICROPROFILE_GPU_STATE_DECL(D3D12) + +void MicroProfileGpuInitD3D12(ID3D12Device* pDevice, ID3D12CommandQueue* pCommandQueue) +{ + MicroProfileGpuInitStateD3D12(); + + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + GPU.pCommandQueue = pCommandQueue; + + HRESULT hr; + + D3D12_QUERY_HEAP_DESC HeapDesc; + HeapDesc.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP; + HeapDesc.Count = MICROPROFILE_GPU_MAX_QUERIES; + HeapDesc.NodeMask = 0; + + D3D12_HEAP_PROPERTIES HeapProperties; + HeapProperties.Type = D3D12_HEAP_TYPE_READBACK; + HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + HeapProperties.CreationNodeMask = 1; + HeapProperties.VisibleNodeMask = 1; + + D3D12_RESOURCE_DESC ResourceDesc; + ResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + ResourceDesc.Alignment = 0; + ResourceDesc.Width = MICROPROFILE_GPU_MAX_QUERIES * sizeof(uint64_t); + ResourceDesc.Height = 1; + ResourceDesc.DepthOrArraySize = 1; + ResourceDesc.MipLevels = 1; + ResourceDesc.Format = DXGI_FORMAT_UNKNOWN; + ResourceDesc.SampleDesc.Count = 1; + ResourceDesc.SampleDesc.Quality = 0; + ResourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + ResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE; + + hr = pDevice->CreateQueryHeap(&HeapDesc, IID_PPV_ARGS(&GPU.pHeap)); + MP_ASSERT(hr == S_OK); + hr = pDevice->CreateCommittedResource(&HeapProperties, D3D12_HEAP_FLAG_NONE, &ResourceDesc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&GPU.pBuffer)); + MP_ASSERT(hr == S_OK); + hr = pDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&GPU.pFence)); + MP_ASSERT(hr == S_OK); + GPU.pFenceEvent = CreateEvent(nullptr, false, false, nullptr); + MP_ASSERT(GPU.pFenceEvent != INVALID_HANDLE_VALUE); + + for (uint32_t i = 0; i < MICROPROFILE_GPU_FRAMES; ++i) + { + hr = pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&GPU.pCommandAllocators[i])); + MP_ASSERT(hr == S_OK); + hr = pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, GPU.pCommandAllocators[i], nullptr, IID_PPV_ARGS(&GPU.pCommandLists[i])); + MP_ASSERT(hr == S_OK); + hr = GPU.pCommandLists[i]->Close(); + MP_ASSERT(hr == S_OK); + } + + hr = pCommandQueue->GetTimestampFrequency(&GPU.nQueryFrequency); + MP_ASSERT(hr == S_OK); +} + +void MicroProfileGpuShutdownD3D12() +{ + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + if(!GPU.pCommandQueue) + return; + + if (GPU.nFrame > 0) + { + GPU.pFence->SetEventOnCompletion(GPU.nFrame, GPU.pFenceEvent); + WaitForSingleObject(GPU.pFenceEvent, INFINITE); + } + + for (uint32_t i = 0; i < MICROPROFILE_GPU_FRAMES; ++i) + { + GPU.pCommandLists[i]->Release(); + GPU.pCommandLists[i] = 0; + + GPU.pCommandAllocators[i]->Release(); + GPU.pCommandAllocators[i] = 0; + } + + GPU.pHeap->Release(); + GPU.pHeap = 0; + + GPU.pBuffer->Release(); + GPU.pBuffer = 0; + + GPU.pFence->Release(); + GPU.pFence = 0; + + CloseHandle(GPU.pFenceEvent); + GPU.pFenceEvent = 0; + + GPU.pCommandQueue = 0; +} + +uint32_t MicroProfileGpuFlipD3D12() +{ + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + // Submit current frame + uint32_t nFrameIndex = GPU.nFrame % MICROPROFILE_GPU_FRAMES; + uint32_t nFrameStart = nFrameIndex * nFrameQueries; + + ID3D12CommandAllocator* pCommandAllocator = GPU.pCommandAllocators[nFrameIndex]; + ID3D12GraphicsCommandList* pCommandList = GPU.pCommandLists[nFrameIndex]; + + pCommandAllocator->Reset(); + pCommandList->Reset(pCommandAllocator, nullptr); + + uint32_t nFrameTimeStamp = MicroProfileGpuInsertTimer(pCommandList); + + uint32_t nFramePut = MicroProfileMin(GPU.nFramePut.load(), nFrameQueries); + + if (nFramePut) + pCommandList->ResolveQueryData(GPU.pHeap, D3D12_QUERY_TYPE_TIMESTAMP, nFrameStart, nFramePut, GPU.pBuffer, nFrameStart * sizeof(int64_t)); + + pCommandList->Close(); + + ID3D12CommandList* pList = pCommandList; + GPU.pCommandQueue->ExecuteCommandLists(1, &pList); + GPU.pCommandQueue->Signal(GPU.pFence, GPU.nFrame + 1); + + GPU.nSubmitted[nFrameIndex] = nFramePut; + GPU.nFramePut.store(0); + GPU.nFrame++; + + // Fetch frame results + if (GPU.nFrame >= MICROPROFILE_GPU_FRAMES) + { + uint64_t nPendingFrame = GPU.nFrame - MICROPROFILE_GPU_FRAMES; + uint32_t nPendingFrameIndex = nPendingFrame % MICROPROFILE_GPU_FRAMES; + + GPU.pFence->SetEventOnCompletion(nPendingFrame + 1, GPU.pFenceEvent); + WaitForSingleObject(GPU.pFenceEvent, INFINITE); + + uint32_t nPendingFrameStart = nPendingFrameIndex * nFrameQueries; + uint32_t nPendingFrameCount = GPU.nSubmitted[nPendingFrameIndex]; + + if (nPendingFrameCount) + { + void* pData = 0; + D3D12_RANGE Range = { nPendingFrameStart * sizeof(uint64_t), (nPendingFrameStart + nPendingFrameCount) * sizeof(uint64_t) }; + + HRESULT hr = GPU.pBuffer->Map(0, &Range, &pData); + MP_ASSERT(hr == S_OK); + + memcpy(&GPU.nResults[nPendingFrameStart], (uint64_t*)pData + nPendingFrameStart, nPendingFrameCount * sizeof(uint64_t)); + + GPU.pBuffer->Unmap(0, 0); + } + } + + return nFrameTimeStamp; +} + +uint32_t MicroProfileGpuInsertTimerD3D12(void* pContext) +{ + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + if (!pContext) return (uint32_t)-1; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + uint32_t nIndex = GPU.nFramePut.fetch_add(1); + if(nIndex >= nFrameQueries) return (uint32_t)-1; + + uint32_t nQueryIndex = (GPU.nFrame % MICROPROFILE_GPU_FRAMES) * nFrameQueries + nIndex; + + ((ID3D12GraphicsCommandList*)pContext)->EndQuery(GPU.pHeap, D3D12_QUERY_TYPE_TIMESTAMP, nQueryIndex); + + return nQueryIndex; +} + +uint64_t MicroProfileGpuGetTimeStampD3D12(uint32_t nIndex) +{ + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + return GPU.nResults[nIndex]; +} + +uint64_t MicroProfileTicksPerSecondGpuD3D12() +{ + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + return GPU.nQueryFrequency ? GPU.nQueryFrequency : 1000000000ll; +} + +bool MicroProfileGetGpuTickReferenceD3D12(int64_t* pOutCpu, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateD3D12& GPU = g_MicroProfileGPU_D3D12; + + return SUCCEEDED(GPU.pCommandQueue->GetClockCalibration((uint64_t*)pOutGpu, (uint64_t*)pOutCpu)); +} + +MICROPROFILE_GPU_STATE_IMPL(D3D12) +#endif + +#if MICROPROFILE_GPU_TIMERS_GL +#ifndef GL_TIMESTAMP +#ifdef __APPLE__ +#include +#else +#error You must include OpenGL headers for GPU timers to work +#endif +#endif + +struct MicroProfileGpuTimerStateGL +{ + int32_t nTimestampBits; + uint32_t nQueries[MICROPROFILE_GPU_MAX_QUERIES]; + + uint64_t nFrame; + std::atomic nFramePut; + + uint64_t nTimerOffset[MICROPROFILE_GPU_FRAMES]; + uint32_t nSubmitted[MICROPROFILE_GPU_FRAMES]; + uint64_t nResults[MICROPROFILE_GPU_MAX_QUERIES]; +}; + +MICROPROFILE_GPU_STATE_DECL(GL) + +void MicroProfileGpuInitGL() +{ + MicroProfileGpuInitStateGL(); + + MicroProfileGpuTimerStateGL& GPU = g_MicroProfileGPU_GL; + + glGetQueryiv(GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &GPU.nTimestampBits); + +#ifdef __APPLE__ + // OSX GL driver (incorrectly) issues GL_INVALID_ENUM when querying the timestamp bits + glGetError(); +#endif + + glGenQueries(MICROPROFILE_GPU_MAX_QUERIES, &GPU.nQueries[0]); +} + +void MicroProfileGpuShutdownGL() +{ + MicroProfileGpuTimerStateGL& GPU = g_MicroProfileGPU_GL; + + glDeleteQueries(MICROPROFILE_GPU_MAX_QUERIES, &GPU.nQueries[0]); +} + +uint32_t MicroProfileGpuFlipGL() +{ + MicroProfileGpuTimerStateGL& GPU = g_MicroProfileGPU_GL; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + // Submit current frame + uint32_t nFrameIndex = GPU.nFrame % MICROPROFILE_GPU_FRAMES; + uint32_t nFramePut = MicroProfileMin(GPU.nFramePut.load(), nFrameQueries); + + if(!GPU.nTimestampBits && nFramePut > 0) + glEndQuery(GL_TIME_ELAPSED); + + GPU.nTimerOffset[nFrameIndex] = MP_TICK() * (double(MicroProfileTicksPerSecondGpu()) / double(MicroProfileTicksPerSecondCpu())); + GPU.nSubmitted[nFrameIndex] = nFramePut; + GPU.nFramePut.store(0); + GPU.nFrame++; + + // Fetch frame results + if (GPU.nFrame >= MICROPROFILE_GPU_FRAMES) + { + uint64_t nPendingFrame = GPU.nFrame - MICROPROFILE_GPU_FRAMES; + uint32_t nPendingFrameIndex = nPendingFrame % MICROPROFILE_GPU_FRAMES; + uint64_t nTimerOffset = GPU.nTimerOffset[nPendingFrameIndex]; + + for(uint32_t i = 0; i < GPU.nSubmitted[nPendingFrameIndex]; ++i) + { + uint32_t nQueryIndex = nPendingFrameIndex * nFrameQueries + i; + MP_ASSERT(nQueryIndex < MICROPROFILE_GPU_MAX_QUERIES); + + uint64_t nResult = 0; + glGetQueryObjectui64v(GPU.nQueries[nQueryIndex], GL_QUERY_RESULT, &nResult); + + if(GPU.nTimestampBits) + { + GPU.nResults[nQueryIndex] = nResult; + } + else + { + GPU.nResults[nQueryIndex] = nTimerOffset; + nTimerOffset += nResult; } } } + + return MicroProfileGpuInsertTimer(0); } -ULONG WINAPI MicroProfileBufferCallback(PEVENT_TRACE_LOGFILE Buffer) +uint32_t MicroProfileGpuInsertTimerGL(void* pContext) { - return (S.bContextSwitchStop || !S.bContextSwitchRunning) ? FALSE : TRUE; + MicroProfileGpuTimerStateGL& GPU = g_MicroProfileGPU_GL; + + (void)pContext; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + uint32_t nIndex = GPU.nFramePut.fetch_add(1); + if(nIndex >= nFrameQueries) return (uint32_t)-1; + + uint32_t nQueryIndex = (GPU.nFrame % MICROPROFILE_GPU_FRAMES) * nFrameQueries + nIndex; + + if(!GPU.nTimestampBits && nIndex > 0) + glEndQuery(GL_TIME_ELAPSED); + + if(GPU.nTimestampBits) + glQueryCounter(GPU.nQueries[nQueryIndex], GL_TIMESTAMP); + else + glBeginQuery(GL_TIME_ELAPSED, GPU.nQueries[nQueryIndex]); + + return nQueryIndex; } - -struct MicroProfileKernelTraceProperties : public EVENT_TRACE_PROPERTIES +uint64_t MicroProfileGpuGetTimeStampGL(uint32_t nIndex) { - char dummy[sizeof(KERNEL_LOGGER_NAME)]; + MicroProfileGpuTimerStateGL& GPU = g_MicroProfileGPU_GL; + + return GPU.nResults[nIndex]; +} + +uint64_t MicroProfileTicksPerSecondGpuGL() +{ + return 1000000000ll; +} + +bool MicroProfileGetGpuTickReferenceGL(int64_t* pOutCpu, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateGL& GPU = g_MicroProfileGPU_GL; + + if(GPU.nTimestampBits) + { + int64_t nGpuTimeStamp = 0; + glGetInteger64v(GL_TIMESTAMP, &nGpuTimeStamp); + + if(nGpuTimeStamp) + { + *pOutCpu = MP_TICK(); + *pOutGpu = nGpuTimeStamp; + return true; + } + + return false; + } + else + { + *pOutCpu = MP_TICK(); + *pOutGpu = MP_TICK() * (double(MicroProfileTicksPerSecondGpu()) / double(MicroProfileTicksPerSecondCpu())); + return true; + } +} + +MICROPROFILE_GPU_STATE_IMPL(GL) +#endif + +#if MICROPROFILE_GPU_TIMERS_VK +#ifndef VK_HEADER_VERSION +#include +#endif + +struct MicroProfileGpuTimerStateVK +{ + VkDevice pDevice; + VkQueue pQueue; + VkQueryPool pQueryPool; + VkCommandPool pCommandPool; + VkCommandBuffer pCommandBuffers[MICROPROFILE_GPU_FRAMES]; + VkFence pFences[MICROPROFILE_GPU_FRAMES]; + + VkCommandBuffer pReferenceCommandBuffer; + uint32_t nReferenceQuery; + + uint64_t nFrame; + std::atomic nFramePut; + + uint32_t nSubmitted[MICROPROFILE_GPU_FRAMES]; + uint64_t nResults[MICROPROFILE_GPU_MAX_QUERIES]; + uint64_t nQueryFrequency; }; -void MicroProfileContextSwitchStopTrace() +MICROPROFILE_GPU_STATE_DECL(VK) + +void MicroProfileGpuInitVK(VkDevice pDevice, VkPhysicalDevice pPhysicalDevice, VkQueue pQueue) { - TRACEHANDLE SessionHandle = 0; - MicroProfileKernelTraceProperties sessionProperties; + MicroProfileGpuInitStateVK(); - ZeroMemory(&sessionProperties, sizeof(sessionProperties)); - sessionProperties.Wnode.BufferSize = sizeof(sessionProperties); - sessionProperties.Wnode.Flags = WNODE_FLAG_TRACED_GUID; - sessionProperties.Wnode.ClientContext = 1; //QPC clock resolution - sessionProperties.Wnode.Guid = SystemTraceControlGuid; - sessionProperties.BufferSize = 1; - sessionProperties.NumberOfBuffers = 128; - sessionProperties.EnableFlags = EVENT_TRACE_FLAG_CSWITCH; - sessionProperties.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; - sessionProperties.MaximumFileSize = 0; - sessionProperties.LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES); - sessionProperties.LogFileNameOffset = 0; + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; - EVENT_TRACE_LOGFILE log; - ZeroMemory(&log, sizeof(log)); - log.LoggerName = (LPWSTR)KERNEL_LOGGER_NAME; - log.ProcessTraceMode = 0; - TRACEHANDLE hLog = OpenTrace(&log); - if (hLog) + VkPhysicalDeviceProperties Properties; + vkGetPhysicalDeviceProperties(pPhysicalDevice, &Properties); + + GPU.pDevice = pDevice; + GPU.pQueue = pQueue; + + VkQueryPoolCreateInfo queryPoolInfo = {}; + queryPoolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; + queryPoolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP; + queryPoolInfo.queryCount = MICROPROFILE_GPU_MAX_QUERIES + 1; // reference query + + VkResult res = vkCreateQueryPool(pDevice, &queryPoolInfo, nullptr, &GPU.pQueryPool); + MP_ASSERT(res == VK_SUCCESS); + + VkCommandPoolCreateInfo commandPoolInfo = {}; + commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; + commandPoolInfo.queueFamilyIndex = 0; + + res = vkCreateCommandPool(pDevice, &commandPoolInfo, nullptr, &GPU.pCommandPool); + MP_ASSERT(res == VK_SUCCESS); + + VkCommandBuffer pCommandBuffers[MICROPROFILE_GPU_FRAMES + 1] = {}; + + VkCommandBufferAllocateInfo commandBufferInfo = {}; + commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + commandBufferInfo.commandPool = GPU.pCommandPool; + commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + commandBufferInfo.commandBufferCount = sizeof(pCommandBuffers) / sizeof(pCommandBuffers[0]); + + res = vkAllocateCommandBuffers(pDevice, &commandBufferInfo, pCommandBuffers); + MP_ASSERT(res == VK_SUCCESS); + + VkFenceCreateInfo fenceInfo = {}; + fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + + for (uint32_t i = 0; i < MICROPROFILE_GPU_FRAMES; ++i) { - ControlTrace(SessionHandle, KERNEL_LOGGER_NAME, &sessionProperties, EVENT_TRACE_CONTROL_STOP); - } - CloseTrace(hLog); + GPU.pCommandBuffers[i] = pCommandBuffers[i]; - -} - -void MicroProfileTraceThread(int unused) -{ - - MicroProfileContextSwitchStopTrace(); - ULONG status = ERROR_SUCCESS; - TRACEHANDLE SessionHandle = 0; - MicroProfileKernelTraceProperties sessionProperties; - - ZeroMemory(&sessionProperties, sizeof(sessionProperties)); - sessionProperties.Wnode.BufferSize = sizeof(sessionProperties); - sessionProperties.Wnode.Flags = WNODE_FLAG_TRACED_GUID; - sessionProperties.Wnode.ClientContext = 1; //QPC clock resolution - sessionProperties.Wnode.Guid = SystemTraceControlGuid; - sessionProperties.BufferSize = 1; - sessionProperties.NumberOfBuffers = 128; - sessionProperties.EnableFlags = EVENT_TRACE_FLAG_CSWITCH|EVENT_TRACE_FLAG_PROCESS; - sessionProperties.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; - sessionProperties.MaximumFileSize = 0; - sessionProperties.LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES); - sessionProperties.LogFileNameOffset = 0; - - - status = StartTrace((PTRACEHANDLE) &SessionHandle, KERNEL_LOGGER_NAME, &sessionProperties); - - if (ERROR_SUCCESS != status) - { - S.bContextSwitchRunning = false; - return; + res = vkCreateFence(pDevice, &fenceInfo, nullptr, &GPU.pFences[i]); + MP_ASSERT(res == VK_SUCCESS); } - EVENT_TRACE_LOGFILE log; - ZeroMemory(&log, sizeof(log)); + GPU.pReferenceCommandBuffer = pCommandBuffers[MICROPROFILE_GPU_FRAMES]; + GPU.nReferenceQuery = MICROPROFILE_GPU_MAX_QUERIES; // reference query - log.LoggerName = (LPWSTR)KERNEL_LOGGER_NAME; - log.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_RAW_TIMESTAMP; - log.EventCallback = MicroProfileContextSwitchCallback; - log.BufferCallback = MicroProfileBufferCallback; - - TRACEHANDLE hLog = OpenTrace(&log); - ProcessTrace(&hLog, 1, 0, 0); - CloseTrace(hLog); - MicroProfileContextSwitchStopTrace(); - - S.bContextSwitchRunning = false; + GPU.nQueryFrequency = 1e9 / Properties.limits.timestampPeriod; } -void MicroProfileStartContextSwitchTrace() +void MicroProfileGpuShutdownVK() { - if(!S.bContextSwitchRunning) + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; + + if (GPU.nFrame > 0) { - if(!S.pContextSwitchThread) - S.pContextSwitchThread = new std::thread(); - if(S.pContextSwitchThread->joinable()) + uint32_t nFrameIndex = (GPU.nFrame - 1) % MICROPROFILE_GPU_FRAMES; + + VkResult res = vkWaitForFences(GPU.pDevice, 1, &GPU.pFences[nFrameIndex], VK_TRUE, UINT64_MAX); + MP_ASSERT(res == VK_SUCCESS); + } + + for (uint32_t i = 0; i < MICROPROFILE_GPU_FRAMES; ++i) + { + vkDestroyFence(GPU.pDevice, GPU.pFences[i], nullptr); + GPU.pFences[i] = 0; + } + + vkDestroyCommandPool(GPU.pDevice, GPU.pCommandPool, nullptr); + memset(GPU.pCommandBuffers, 0, sizeof(GPU.pCommandBuffers)); + GPU.pCommandPool = 0; + + vkDestroyQueryPool(GPU.pDevice, GPU.pQueryPool, nullptr); + GPU.pQueryPool = 0; + + GPU.pQueue = 0; + GPU.pDevice = 0; +} + +uint32_t MicroProfileGpuFlipVK() +{ + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + // Submit current frame + uint32_t nFrameIndex = GPU.nFrame % MICROPROFILE_GPU_FRAMES; + uint32_t nFrameStart = nFrameIndex * nFrameQueries; + + VkCommandBuffer pCommandBuffer = GPU.pCommandBuffers[nFrameIndex]; + + VkCommandBufferBeginInfo commandBufferBeginInfo = {}; + commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + + VkResult res = vkBeginCommandBuffer(pCommandBuffer, &commandBufferBeginInfo); + MP_ASSERT(res == VK_SUCCESS); + + uint32_t nFrameTimeStamp = MicroProfileGpuInsertTimer(pCommandBuffer); + uint32_t nFramePut = MicroProfileMin(GPU.nFramePut.load(), nFrameQueries); + + res = vkEndCommandBuffer(pCommandBuffer); + MP_ASSERT(res == VK_SUCCESS); + + VkSubmitInfo submitInfo = {}; + submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submitInfo.commandBufferCount = 1; + submitInfo.pCommandBuffers = &pCommandBuffer; + + res = vkQueueSubmit(GPU.pQueue, 1, &submitInfo, GPU.pFences[nFrameIndex]); + MP_ASSERT(res == VK_SUCCESS); + + GPU.nSubmitted[nFrameIndex] = nFramePut; + GPU.nFramePut.store(0); + GPU.nFrame++; + + // Fetch frame results + if (GPU.nFrame >= MICROPROFILE_GPU_FRAMES) + { + uint64_t nPendingFrame = GPU.nFrame - MICROPROFILE_GPU_FRAMES; + uint32_t nPendingFrameIndex = nPendingFrame % MICROPROFILE_GPU_FRAMES; + + res = vkWaitForFences(GPU.pDevice, 1, &GPU.pFences[nPendingFrameIndex], VK_TRUE, UINT64_MAX); + MP_ASSERT(res == VK_SUCCESS); + + res = vkResetFences(GPU.pDevice, 1, &GPU.pFences[nPendingFrameIndex]); + MP_ASSERT(res == VK_SUCCESS); + + uint32_t nPendingFrameStart = nPendingFrameIndex * nFrameQueries; + uint32_t nPendingFrameCount = GPU.nSubmitted[nPendingFrameIndex]; + + if (nPendingFrameCount) { - S.bContextSwitchStop = true; - S.pContextSwitchThread->join(); + res = vkGetQueryPoolResults(GPU.pDevice, GPU.pQueryPool, + nPendingFrameStart, nPendingFrameCount, + nPendingFrameCount * sizeof(uint64_t), &GPU.nResults[nPendingFrameStart], + sizeof(uint64_t), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); + MP_ASSERT(res == VK_SUCCESS); } - S.bContextSwitchRunning = true; - S.bContextSwitchStop = false; - *S.pContextSwitchThread = std::thread(&MicroProfileTraceThread, 0); } + + return nFrameTimeStamp; } -void MicroProfileStopContextSwitchTrace() +uint32_t MicroProfileGpuInsertTimerVK(void* pContext) { - if(S.bContextSwitchRunning && S.pContextSwitchThread) - { - S.bContextSwitchStop = true; - S.pContextSwitchThread->join(); - } + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; + + uint32_t nFrameQueries = MICROPROFILE_GPU_MAX_QUERIES / MICROPROFILE_GPU_FRAMES; + + uint32_t nIndex = GPU.nFramePut.fetch_add(1); + if(nIndex >= nFrameQueries) return (uint32_t)-1; + + uint32_t nQueryIndex = (GPU.nFrame % MICROPROFILE_GPU_FRAMES) * nFrameQueries + nIndex; + + vkCmdWriteTimestamp((VkCommandBuffer)pContext, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, GPU.pQueryPool, nQueryIndex); + + return nQueryIndex; } -bool MicroProfileIsLocalThread(uint32_t nThreadId) +uint64_t MicroProfileGpuGetTimeStampVK(uint32_t nIndex) { - HANDLE h = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, nThreadId); - if(h == NULL) - return false; - DWORD hProcess = GetProcessIdOfThread(h); - CloseHandle(h); - return GetCurrentProcessId() == hProcess; + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; + + return GPU.nResults[nIndex]; } -#else -#error "context switch trace not supported/implemented on platform" +uint64_t MicroProfileTicksPerSecondGpuVK() +{ + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; + + return GPU.nQueryFrequency ? GPU.nQueryFrequency : 1000000000ll; +} + +bool MicroProfileGetGpuTickReferenceVK(int64_t* pOutCpu, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateVK& GPU = g_MicroProfileGPU_VK; + + VkCommandBuffer pCommandBuffer = GPU.pReferenceCommandBuffer; + uint32_t nQueryIndex = GPU.nReferenceQuery; + + VkCommandBufferBeginInfo commandBufferBeginInfo = {}; + commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + + VkResult res = vkBeginCommandBuffer(pCommandBuffer, &commandBufferBeginInfo); + MP_ASSERT(res == VK_SUCCESS); + + vkCmdWriteTimestamp(pCommandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, GPU.pQueryPool, nQueryIndex); + + res = vkEndCommandBuffer(pCommandBuffer); + MP_ASSERT(res == VK_SUCCESS); + + VkSubmitInfo submitInfo = {}; + submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submitInfo.commandBufferCount = 1; + submitInfo.pCommandBuffers = &pCommandBuffer; + + res = vkQueueSubmit(GPU.pQueue, 1, &submitInfo, VK_NULL_HANDLE); + MP_ASSERT(res == VK_SUCCESS); + + res = vkQueueWaitIdle(GPU.pQueue); + MP_ASSERT(res == VK_SUCCESS); + + *pOutCpu = MP_TICK(); + + res = vkGetQueryPoolResults(GPU.pDevice, GPU.pQueryPool, nQueryIndex, 1, sizeof(uint64_t), pOutGpu, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); + MP_ASSERT(res == VK_SUCCESS); + + return true; +} + +MICROPROFILE_GPU_STATE_IMPL(VK) #endif -#else - -bool MicroProfileIsLocalThread(uint32_t nThreadId){return false;} -void MicroProfileStopContextSwitchTrace(){} -void MicroProfileStartContextSwitchTrace(){} - -#endif - #undef S #ifdef _WIN32 #pragma warning(pop) #endif -#endif -#endif - - -///start embedded file from microprofile.html -#ifdef MICROPROFILE_EMBED_HTML -const char g_MicroProfileHtml_begin[] = -"\n" -"\n" -"\n" -"MicroProfile Capture\n" -"\n" -"\n" -"\n" -"\n" -"
\n" -"
    \n" -"
  • ?\n" -"
    \n" -"Use Cursor to Inspect
    \n" -"Shift-Drag to Pan view
    \n" -"Ctrl-Drag to Zoom view
    \n" -"Click to Zoom to selected range
    \n" -"
    \n" -"\n" -"
    \n" -"
    \n" -"\n" -"
  • \n" -"
  • Timers \n" -"
  • Detailed \n" -"
  • \n" -"
  • Reference\n" -" \n" -"
  • \n" -"
  • Threads\n" -" \n" -"
  • \n" -"
  • Groups\n" -" \n" -"
  • \n" -"
  • Width\n" -" \n" -"
  • \n" -"
\n" -"\n" -"
\n" -"\n" -"\n" -"\n" -" "; - -const size_t g_MicroProfileHtml_end_size = sizeof(g_MicroProfileHtml_end); +#if MICROPROFILE_EMBED_HTML +#include "microprofilehtml.h" #endif //MICROPROFILE_EMBED_HTML - -///end embedded file from microprofile.html +#endif +#endif diff --git a/third_party/microprofile/microprofiledraw.h b/third_party/microprofile/microprofiledraw.h new file mode 100644 index 000000000..ac7c0f3a4 --- /dev/null +++ b/third_party/microprofile/microprofiledraw.h @@ -0,0 +1,648 @@ +// This is free and unencumbered software released into the public domain. +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// For more information, please refer to + +#ifndef MICROPROFILE_ENABLED +#error "microprofile.h must be included before including microprofiledraw.h" +#endif + +#ifndef MICROPROFILEDRAW_ENABLED +#define MICROPROFILEDRAW_ENABLED MICROPROFILE_ENABLED +#endif + +#ifndef MICROPROFILEDRAW_API +#define MICROPROFILEDRAW_API +#endif + +#if MICROPROFILEDRAW_ENABLED +MICROPROFILEDRAW_API void MicroProfileDrawInitGL(); + +MICROPROFILEDRAW_API void MicroProfileRender(uint32_t nWidth, uint32_t nHeight, float fScale); + +MICROPROFILEDRAW_API void MicroProfileBeginDraw(uint32_t nWidth, uint32_t nHeight, float* pfProjection); +MICROPROFILEDRAW_API void MicroProfileBeginDraw(uint32_t nWidth, uint32_t nHeight, float fScale); +MICROPROFILEDRAW_API void MicroProfileEndDraw(); + +#ifdef MICROPROFILEDRAW_IMPL +struct MicroProfileDrawVertex +{ + float nX; + float nY; + uint32_t nColor; + float fU; + float fV; +}; + +struct MicroProfileDrawCommand +{ + uint32_t nCommand; + uint32_t nNumVertices; +}; + +struct MicroProfileDrawContext +{ + enum + { + MAX_COMMANDS = 32, + MAX_VERTICES = 16384, + }; + + bool bInitialized; + + GLuint nVAO; + GLuint nVertexBuffer; + GLuint nProgram; + GLuint nTexture; + + int nAttributePosition; + int nAttributeColor; + int nAttributeTexture; + int nUniformProjectionMatrix; + + uint32_t nVertexPos; + uint32_t nCommandPos; + + MicroProfileDrawVertex nVertices[MAX_VERTICES]; + MicroProfileDrawCommand nCommands[MAX_COMMANDS]; +}; + +MicroProfileDrawContext g_MicroProfileDraw; + +#define Q0(d, member, v) d[0].member = v +#define Q1(d, member, v) d[1].member = v; d[3].member = v +#define Q2(d, member, v) d[4].member = v +#define Q3(d, member, v) d[2].member = v; d[5].member = v + +#define FONT_TEX_X 1024 +#define FONT_TEX_Y 9 +#define FONT_SIZE (FONT_TEX_X*FONT_TEX_Y * 4) + +namespace +{ + extern const uint8_t g_MicroProfileFont[]; + extern const uint16_t g_MicroProfileFontDescription[]; + + extern const char g_MicroProfileVertexShader_110[]; + extern const char g_MicroProfileFragmentShader_110[]; + + extern const char g_MicroProfileVertexShader_150[]; + extern const char g_MicroProfileFragmentShader_150[]; +} + +bool MicroProfileCompileShader(GLuint* pnHandle, int nType, const char* pShader) +{ + *pnHandle = glCreateShader(nType); + glShaderSource(*pnHandle, 1, &pShader, 0); + glCompileShader(*pnHandle); + + GLint compiled = 0; + glGetShaderiv(*pnHandle, GL_COMPILE_STATUS, &compiled); + + if(!compiled) + { + char temp[4096]; + glGetShaderInfoLog(*pnHandle, 4096, NULL, temp); + printf("SHADER FAILED TO COMPILE:\n%s\n", temp); + return false; + } + + return true; +} + +bool MicroProfileLinkProgram(GLuint* pnHandle, GLuint nVertexShader, GLuint nFragmentShader) +{ + *pnHandle = glCreateProgram(); + glAttachShader(*pnHandle, nVertexShader); + glAttachShader(*pnHandle, nFragmentShader); + glLinkProgram(*pnHandle); + + GLint linked = 0; + glGetProgramiv(*pnHandle, GL_LINK_STATUS, &linked); + + if(!linked) + { + char temp[4096]; + glGetProgramInfoLog(*pnHandle, 4096, NULL, temp); + printf("PROGRAM FAILED TO LINK:\n%s\n", temp); + return false; + } + + return true; +} + +void MicroProfileDrawInitGL() +{ + MicroProfileDrawContext& S = g_MicroProfileDraw; + + MP_ASSERT(!S.bInitialized); + + const GLubyte* pGLVersion = glGetString(GL_VERSION); + const GLubyte* pGLSLVersion = glGetString(GL_SHADING_LANGUAGE_VERSION); + + int nGLVersion = (pGLVersion[0] - '0') * 10 + (pGLVersion[2] - '0'); + int nGLSLVersion = (pGLSLVersion[0] - '0') * 100 + (pGLSLVersion[2] - '0') * 10 + (pGLSLVersion[3] - '0'); + + glGenBuffers(1, &S.nVertexBuffer); + glBindBuffer(GL_ARRAY_BUFFER, S.nVertexBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(S.nVertices), 0, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + if (nGLVersion >= 3) + glGenVertexArrays(1, &S.nVAO); + else + S.nVAO = 0; + + GLuint nVertexShader; + if(!MicroProfileCompileShader(&nVertexShader, GL_VERTEX_SHADER, nGLSLVersion >= 150 ? g_MicroProfileVertexShader_150 : g_MicroProfileVertexShader_110)) + return; + + GLuint nFragmentShader; + if(!MicroProfileCompileShader(&nFragmentShader, GL_FRAGMENT_SHADER, nGLSLVersion >= 150 ? g_MicroProfileFragmentShader_150 : g_MicroProfileFragmentShader_110)) + return; + + if(!MicroProfileLinkProgram(&S.nProgram, nVertexShader, nFragmentShader)) + return; + + S.nAttributePosition = glGetAttribLocation(S.nProgram, "VertexIn"); + S.nAttributeColor = glGetAttribLocation(S.nProgram, "ColorIn"); + S.nAttributeTexture = glGetAttribLocation(S.nProgram, "TCIn"); + + S.nUniformProjectionMatrix = glGetUniformLocation(S.nProgram, "ProjectionMatrix"); + + glUseProgram(S.nProgram); + glUniform1i(glGetUniformLocation(S.nProgram, "Texture"), 0); + glUniform1f(glGetUniformLocation(S.nProgram, "RcpFontHeight"), 1.f / FONT_TEX_Y); + glUseProgram(0); + + uint32_t* pUnpacked = (uint32_t*)alloca(FONT_SIZE); + int idx = 0; + int end = FONT_TEX_X * FONT_TEX_Y / 8; + for(int i = 0; i < end; i++) + { + unsigned char pValue = g_MicroProfileFont[i]; + for(int j = 0; j < 8; ++j) + { + pUnpacked[idx++] = pValue & 0x80 ? (uint32_t)-1 : 0; + pValue <<= 1; + } + } + + pUnpacked[idx-1] = 0xffffffff; + + uint32_t* p4 = &pUnpacked[0]; + glGenTextures(1, &S.nTexture); + glBindTexture(GL_TEXTURE_2D, S.nTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FONT_TEX_X, FONT_TEX_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, &p4[0]); + glBindTexture(GL_TEXTURE_2D, 0); + + S.bInitialized = true; +} + +void MicroProfileBeginDraw(uint32_t nWidth, uint32_t nHeight, float* pfProjection) +{ + (void)nWidth; + (void)nHeight; + + MicroProfileDrawContext& S = g_MicroProfileDraw; + + if (!S.bInitialized) + return; + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + + if (S.nVAO) + glBindVertexArray(S.nVAO); + + glUseProgram(S.nProgram); + glUniformMatrix4fv(S.nUniformProjectionMatrix, 1, 0, pfProjection); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, S.nTexture); + + glBindBuffer(GL_ARRAY_BUFFER, S.nVertexBuffer); + + int nStride = sizeof(MicroProfileDrawVertex); + + glVertexAttribPointer(S.nAttributePosition, 2, GL_FLOAT, 0, nStride, (void*)(offsetof(MicroProfileDrawVertex, nX))); + glVertexAttribPointer(S.nAttributeColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, nStride, (void*)(offsetof(MicroProfileDrawVertex, nColor))); + glVertexAttribPointer(S.nAttributeTexture, 2, GL_FLOAT, 0, nStride, (void*)(offsetof(MicroProfileDrawVertex, fU))); + + glEnableVertexAttribArray(S.nAttributePosition); + glEnableVertexAttribArray(S.nAttributeColor); + glEnableVertexAttribArray(S.nAttributeTexture); + + S.nVertexPos = 0; + S.nCommandPos = 0; +} + +void MicroProfileBeginDraw(uint32_t nWidth, uint32_t nHeight, float fScale) +{ + MicroProfileDrawContext& S = g_MicroProfileDraw; + + if (!S.bInitialized) + return; + + float left = 0.f; + float right = nWidth / fScale; + float bottom = nHeight / fScale; + float top = 0.f; + float near_plane = -1.f; + float far_plane = 1.f; + + float projection[16] = {}; + + projection[0] = 2.0f / (right - left); + projection[5] = 2.0f / (top - bottom); + projection[10] = -2.0f / (far_plane - near_plane); + projection[12] = - (right + left) / (right - left); + projection[13] = - (top + bottom) / (top - bottom); + projection[14] = - (far_plane + near_plane) / (far_plane - near_plane); + projection[15] = 1.f; + + MicroProfileBeginDraw(nWidth, nHeight, projection); +} + +void MicroProfileFlush() +{ + MicroProfileDrawContext& S = g_MicroProfileDraw; + + if(S.nVertexPos == 0) + return; + + MICROPROFILE_SCOPEI("MicroProfile", "Flush", 0xffff3456); + + glBufferSubData(GL_ARRAY_BUFFER, 0, S.nVertexPos * sizeof(MicroProfileDrawVertex), S.nVertices); + + int nOffset = 0; + + for(int i = 0; i < int(S.nCommandPos); ++i) + { + int nCount = S.nCommands[i].nNumVertices; + glDrawArrays(S.nCommands[i].nCommand, nOffset, nCount); + nOffset += nCount; + } + + S.nVertexPos = 0; + S.nCommandPos = 0; +} + +MicroProfileDrawVertex* MicroProfilePushVertices(uint32_t nCommand, int nCount) +{ + MP_ASSERT(nCount <= MicroProfileDrawContext::MAX_VERTICES); + + MicroProfileDrawContext& S = g_MicroProfileDraw; + + if(S.nVertexPos + nCount > MicroProfileDrawContext::MAX_VERTICES) + MicroProfileFlush(); + + if(S.nCommandPos && S.nCommands[S.nCommandPos-1].nCommand == nCommand) + { + S.nCommands[S.nCommandPos-1].nNumVertices += nCount; + } + else + { + if (S.nCommandPos == MicroProfileDrawContext::MAX_COMMANDS) + MicroProfileFlush(); + + S.nCommands[S.nCommandPos].nCommand = nCommand; + S.nCommands[S.nCommandPos].nNumVertices = nCount; + S.nCommandPos++; + } + + uint32_t nOut = S.nVertexPos; + S.nVertexPos += nCount; + + return &S.nVertices[nOut]; +} + +void MicroProfileEndDraw() +{ + MicroProfileDrawContext& S = g_MicroProfileDraw; + + if (!S.bInitialized) + return; + + MicroProfileFlush(); + + glDisableVertexAttribArray(S.nAttributePosition); + glDisableVertexAttribArray(S.nAttributeColor); + glDisableVertexAttribArray(S.nAttributeTexture); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glUseProgram(0); + + if (S.nVAO) + glBindVertexArray(0); + + glDisable(GL_BLEND); +} + +void MicroProfileRender(uint32_t nWidth, uint32_t nHeight, float fScale) +{ + MicroProfileBeginDraw(nWidth, nHeight, fScale); + MicroProfileDraw(nWidth, nHeight); + MicroProfileEndDraw(); +} + +void MicroProfileDrawText(int nX, int nY, uint32_t nColor, const char* pText, uint32_t nLen) +{ + MICROPROFILE_SCOPEI("MicroProfile", "TextDraw", 0xff88ee); + + const float fOffsetU = 5.f / 1024.f; + MP_ASSERT(nLen <= strlen(pText)); + float fX = (float)nX; + float fY = (float)nY; + float fY2 = fY + (MICROPROFILE_TEXT_HEIGHT+1); + + MicroProfileDrawVertex* pVertex = MicroProfilePushVertices(GL_TRIANGLES, 6 * nLen); + if (!pVertex) return; + + const char* pStr = pText; + nColor = 0xff000000|((nColor&0xff)<<16)|(nColor&0xff00)|((nColor>>16)&0xff); + + for(uint32_t j = 0; j < nLen; ++j) + { + int16_t nOffset = g_MicroProfileFontDescription[uint8_t(*pStr++)]; + float fOffset = nOffset / 1024.f; + Q0(pVertex,nX, fX); + Q0(pVertex,nY, fY); + Q0(pVertex,nColor, nColor); + Q0(pVertex,fU, fOffset); + Q0(pVertex,fV, 0.f); + + Q1(pVertex, nX, fX+MICROPROFILE_TEXT_WIDTH); + Q1(pVertex, nY, fY); + Q1(pVertex, nColor, nColor); + Q1(pVertex, fU, fOffset+fOffsetU); + Q1(pVertex, fV, 0.f); + + Q2(pVertex, nX, fX+MICROPROFILE_TEXT_WIDTH); + Q2(pVertex, nY, fY2); + Q2(pVertex, nColor, nColor); + Q2(pVertex, fU, fOffset+fOffsetU); + Q2(pVertex, fV, 1.f); + + + Q3(pVertex, nX, fX); + Q3(pVertex, nY, fY2); + Q3(pVertex, nColor, nColor); + Q3(pVertex, fU, fOffset); + Q3(pVertex, fV, 1.f); + + fX += MICROPROFILE_TEXT_WIDTH+1; + pVertex += 6; + } +} +void MicroProfileDrawBox(int nX0, int nY0, int nX1, int nY1, uint32_t nColor, MicroProfileBoxType Type) +{ + MicroProfileDrawVertex* pVertex = MicroProfilePushVertices(GL_TRIANGLES, 6); + if (!pVertex) return; + + if(Type == MicroProfileBoxTypeFlat) + { + nColor = ((nColor&0xff)<<16)|((nColor>>16)&0xff)|(0xff00ff00&nColor); + Q0(pVertex, nX, (float)nX0); + Q0(pVertex, nY, (float)nY0); + Q0(pVertex, nColor, nColor); + Q0(pVertex, fU, 2.f); + Q0(pVertex, fV, 2.f); + Q1(pVertex, nX, (float)nX1); + Q1(pVertex, nY, (float)nY0); + Q1(pVertex, nColor, nColor); + Q1(pVertex, fU, 2.f); + Q1(pVertex, fV, 2.f); + Q2(pVertex, nX, (float)nX1); + Q2(pVertex, nY, (float)nY1); + Q2(pVertex, nColor, nColor); + Q2(pVertex, fU, 2.f); + Q2(pVertex, fV, 2.f); + Q3(pVertex, nX, (float)nX0); + Q3(pVertex, nY, (float)nY1); + Q3(pVertex, nColor, nColor); + Q3(pVertex, fU, 2.f); + Q3(pVertex, fV, 2.f); + } + else + { + uint32_t r = 0xff & (nColor>>16); + uint32_t g = 0xff & (nColor>>8); + uint32_t b = 0xff & nColor; + uint32_t nMax = MicroProfileMax(MicroProfileMax(MicroProfileMax(r, g), b), 30u); + uint32_t nMin = MicroProfileMin(MicroProfileMin(MicroProfileMin(r, g), b), 180u); + + uint32_t r0 = 0xff & ((r + nMax)/2); + uint32_t g0 = 0xff & ((g + nMax)/2); + uint32_t b0 = 0xff & ((b + nMax)/2); + + uint32_t r1 = 0xff & ((r+nMin)/2); + uint32_t g1 = 0xff & ((g+nMin)/2); + uint32_t b1 = 0xff & ((b+nMin)/2); + uint32_t nColor0 = (r0<<0)|(g0<<8)|(b0<<16)|(0xff000000&nColor); + uint32_t nColor1 = (r1<<0)|(g1<<8)|(b1<<16)|(0xff000000&nColor); + Q0(pVertex, nX, (float)nX0); + Q0(pVertex, nY, (float)nY0); + Q0(pVertex, nColor, nColor0); + Q0(pVertex, fU, 2.f); + Q0(pVertex, fV, 2.f); + Q1(pVertex, nX, (float)nX1); + Q1(pVertex, nY, (float)nY0); + Q1(pVertex, nColor, nColor0); + Q1(pVertex, fU, 3.f); + Q1(pVertex, fV, 2.f); + Q2(pVertex, nX, (float)nX1); + Q2(pVertex, nY, (float)nY1); + Q2(pVertex, nColor, nColor1); + Q2(pVertex, fU, 3.f); + Q2(pVertex, fV, 3.f); + Q3(pVertex, nX, (float)nX0); + Q3(pVertex, nY, (float)nY1); + Q3(pVertex, nColor, nColor1); + Q3(pVertex, fU, 2.f); + Q3(pVertex, fV, 3.f); + } +} + + +void MicroProfileDrawLine2D(uint32_t nVertices, float* pVertices, uint32_t nColor) +{ + if(!nVertices) return; + + MicroProfileDrawVertex* pVertex = MicroProfilePushVertices(GL_LINES, 2*(nVertices-1)); + nColor = ((nColor&0xff)<<16)|(nColor&0xff00ff00)|((nColor>>16)&0xff); + for(uint32_t i = 0; i < nVertices-1; ++i) + { + pVertex[0].nX = pVertices[i*2]; + pVertex[0].nY = pVertices[i*2+1] ; + pVertex[0].nColor = nColor; + pVertex[0].fU = 2.f; + pVertex[0].fV = 2.f; + pVertex[1].nX = pVertices[(i+1)*2]; + pVertex[1].nY = pVertices[(i+1)*2+1] ; + pVertex[1].nColor = nColor; + pVertex[1].fU = 2.f; + pVertex[1].fV = 2.f; + pVertex += 2; + } +} + +namespace +{ + const char g_MicroProfileVertexShader_110[] = + "#version 110\n\ + uniform mat4 ProjectionMatrix; \ + attribute vec3 VertexIn; attribute vec4 ColorIn; attribute vec2 TCIn; \ + varying vec2 TC; varying vec4 Color; \ + void main() { Color = ColorIn; TC = TCIn; gl_Position = ProjectionMatrix * vec4(VertexIn, 1.0); }"; + + const char g_MicroProfileVertexShader_150[] = + "#version 150\n\ + uniform mat4 ProjectionMatrix; \ + in vec3 VertexIn; in vec4 ColorIn; in vec2 TCIn; \ + out vec2 TC; out vec4 Color; \ + void main() { Color = ColorIn; TC = TCIn; gl_Position = ProjectionMatrix * vec4(VertexIn, 1.0); }"; + + const char g_MicroProfileFragmentShader_110[] = + "#version 110\n\ + uniform sampler2D Texture; uniform float RcpFontHeight; \ + varying vec2 TC; varying vec4 Color; \ + void main() { \ + vec4 c0 = texture2D(Texture, TC.xy); \ + vec4 c1 = texture2D(Texture, TC.xy + vec2(0.0, RcpFontHeight)); \ + gl_FragColor = c0.w < 0.5 ? vec4(0, 0, 0, c1.w) : c0 * Color; \ + } \ + "; + + const char g_MicroProfileFragmentShader_150[] = + "#version 150\n\ + uniform sampler2D Texture; uniform float RcpFontHeight; \ + in vec2 TC; in vec4 Color; \ + out vec4 result; \ + void main() { \ + vec4 c0 = texture(Texture, TC.xy); \ + vec4 c1 = texture(Texture, TC.xy + vec2(0.0, RcpFontHeight)); \ + result = c0.w < 0.5 ? vec4(0, 0, 0, c1.w) : c0 * Color; \ + } \ + "; + + const uint16_t g_MicroProfileFontDescription[] = + { + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x201,0x209,0x211,0x219,0x221,0x229,0x231,0x239,0x241,0x249,0x251,0x259,0x261,0x269,0x271, + 0x1b1,0x1b9,0x1c1,0x1c9,0x1d1,0x1d9,0x1e1,0x1e9,0x1f1,0x1f9,0x279,0x281,0x289,0x291,0x299,0x2a1, + 0x2a9,0x001,0x009,0x011,0x019,0x021,0x029,0x031,0x039,0x041,0x049,0x051,0x059,0x061,0x069,0x071, + 0x079,0x081,0x089,0x091,0x099,0x0a1,0x0a9,0x0b1,0x0b9,0x0c1,0x0c9,0x2b1,0x2b9,0x2c1,0x2c9,0x2d1, + 0x0ce,0x0d9,0x0e1,0x0e9,0x0f1,0x0f9,0x101,0x109,0x111,0x119,0x121,0x129,0x131,0x139,0x141,0x149, + 0x151,0x159,0x161,0x169,0x171,0x179,0x181,0x189,0x191,0x199,0x1a1,0x2d9,0x2e1,0x2e9,0x2f1,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + 0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce,0x0ce, + }; + + const uint8_t g_MicroProfileFont[] = + { + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x10,0x78,0x38,0x78,0x7c,0x7c,0x3c,0x44,0x38,0x04,0x44,0x40,0x44,0x44,0x38,0x78, + 0x38,0x78,0x38,0x7c,0x44,0x44,0x44,0x44,0x44,0x7c,0x00,0x00,0x40,0x00,0x04,0x00, + 0x18,0x00,0x40,0x10,0x08,0x40,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x10,0x38,0x7c,0x08,0x7c,0x1c,0x7c,0x38,0x38, + 0x10,0x28,0x28,0x10,0x00,0x20,0x10,0x08,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x04,0x00,0x20,0x38,0x38,0x70,0x00,0x1c,0x10,0x00,0x1c,0x10,0x70,0x30,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x28,0x44,0x44,0x44,0x40,0x40,0x40,0x44,0x10,0x04,0x48,0x40,0x6c,0x44,0x44,0x44, + 0x44,0x44,0x44,0x10,0x44,0x44,0x44,0x44,0x44,0x04,0x00,0x00,0x40,0x00,0x04,0x00, + 0x24,0x00,0x40,0x00,0x00,0x40,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x30,0x44,0x04,0x18,0x40,0x20,0x04,0x44,0x44, + 0x10,0x28,0x28,0x3c,0x44,0x50,0x10,0x10,0x08,0x54,0x10,0x00,0x00,0x00,0x04,0x00, + 0x00,0x08,0x00,0x10,0x44,0x44,0x40,0x40,0x04,0x28,0x00,0x30,0x10,0x18,0x58,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x44,0x44,0x40,0x44,0x40,0x40,0x40,0x44,0x10,0x04,0x50,0x40,0x54,0x64,0x44,0x44, + 0x44,0x44,0x40,0x10,0x44,0x44,0x44,0x28,0x28,0x08,0x00,0x38,0x78,0x3c,0x3c,0x38, + 0x20,0x38,0x78,0x30,0x18,0x44,0x10,0x6c,0x78,0x38,0x78,0x3c,0x5c,0x3c,0x3c,0x44, + 0x44,0x44,0x44,0x44,0x7c,0x00,0x4c,0x10,0x04,0x08,0x28,0x78,0x40,0x08,0x44,0x44, + 0x10,0x00,0x7c,0x50,0x08,0x50,0x00,0x20,0x04,0x38,0x10,0x00,0x00,0x00,0x08,0x10, + 0x10,0x10,0x7c,0x08,0x08,0x54,0x40,0x20,0x04,0x44,0x00,0x30,0x10,0x18,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x44,0x78,0x40,0x44,0x78,0x78,0x40,0x7c,0x10,0x04,0x60,0x40,0x54,0x54,0x44,0x78, + 0x44,0x78,0x38,0x10,0x44,0x44,0x54,0x10,0x10,0x10,0x00,0x04,0x44,0x40,0x44,0x44, + 0x78,0x44,0x44,0x10,0x08,0x48,0x10,0x54,0x44,0x44,0x44,0x44,0x60,0x40,0x10,0x44, + 0x44,0x44,0x28,0x44,0x08,0x00,0x54,0x10,0x18,0x18,0x48,0x04,0x78,0x10,0x38,0x3c, + 0x10,0x00,0x28,0x38,0x10,0x20,0x00,0x20,0x04,0x10,0x7c,0x00,0x7c,0x00,0x10,0x00, + 0x00,0x20,0x00,0x04,0x10,0x5c,0x40,0x10,0x04,0x00,0x00,0x60,0x10,0x0c,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x7c,0x44,0x40,0x44,0x40,0x40,0x4c,0x44,0x10,0x04,0x50,0x40,0x44,0x4c,0x44,0x40, + 0x54,0x50,0x04,0x10,0x44,0x44,0x54,0x28,0x10,0x20,0x00,0x3c,0x44,0x40,0x44,0x7c, + 0x20,0x44,0x44,0x10,0x08,0x70,0x10,0x54,0x44,0x44,0x44,0x44,0x40,0x38,0x10,0x44, + 0x44,0x54,0x10,0x44,0x10,0x00,0x64,0x10,0x20,0x04,0x7c,0x04,0x44,0x20,0x44,0x04, + 0x10,0x00,0x7c,0x14,0x20,0x54,0x00,0x20,0x04,0x38,0x10,0x10,0x00,0x00,0x20,0x10, + 0x10,0x10,0x7c,0x08,0x10,0x58,0x40,0x08,0x04,0x00,0x00,0x30,0x10,0x18,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x44,0x44,0x44,0x44,0x40,0x40,0x44,0x44,0x10,0x44,0x48,0x40,0x44,0x44,0x44,0x40, + 0x48,0x48,0x44,0x10,0x44,0x28,0x6c,0x44,0x10,0x40,0x00,0x44,0x44,0x40,0x44,0x40, + 0x20,0x3c,0x44,0x10,0x08,0x48,0x10,0x54,0x44,0x44,0x44,0x44,0x40,0x04,0x12,0x4c, + 0x28,0x54,0x28,0x3c,0x20,0x00,0x44,0x10,0x40,0x44,0x08,0x44,0x44,0x20,0x44,0x08, + 0x00,0x00,0x28,0x78,0x44,0x48,0x00,0x10,0x08,0x54,0x10,0x10,0x00,0x00,0x40,0x00, + 0x10,0x08,0x00,0x10,0x00,0x40,0x40,0x04,0x04,0x00,0x00,0x30,0x10,0x18,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x44,0x78,0x38,0x78,0x7c,0x40,0x3c,0x44,0x38,0x38,0x44,0x7c,0x44,0x44,0x38,0x40, + 0x34,0x44,0x38,0x10,0x38,0x10,0x44,0x44,0x10,0x7c,0x00,0x3c,0x78,0x3c,0x3c,0x3c, + 0x20,0x04,0x44,0x38,0x48,0x44,0x38,0x44,0x44,0x38,0x78,0x3c,0x40,0x78,0x0c,0x34, + 0x10,0x6c,0x44,0x04,0x7c,0x00,0x38,0x38,0x7c,0x38,0x08,0x38,0x38,0x20,0x38,0x70, + 0x10,0x00,0x28,0x10,0x00,0x34,0x00,0x08,0x10,0x10,0x00,0x20,0x00,0x10,0x00,0x00, + 0x20,0x04,0x00,0x20,0x10,0x3c,0x70,0x00,0x1c,0x00,0x7c,0x1c,0x10,0x70,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x38,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x40,0x04,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + }; +} +#endif +#endif diff --git a/third_party/microprofile/microprofilehtml.h b/third_party/microprofile/microprofilehtml.h new file mode 100644 index 000000000..31074d2af --- /dev/null +++ b/third_party/microprofile/microprofilehtml.h @@ -0,0 +1,4860 @@ +///start file generated from microprofile.html +#ifdef MICROPROFILE_EMBED_HTML +const char g_MicroProfileHtml_begin_0[] = +"\n" +"\n" +"\n" +"MicroProfile Capture\n" +"\n" +"\n" +"\n" +"
\n" +"
Group
\n" +"
Timer/Thread
\n" +"
\n" +"\n" +"
\n" +"
    \n" +"
  • ?\n" +"
    \n" +"History View:
    \n" +"Click + Drag: Pan View
    \n" +"Right Click + Drag : Zoom on region
    \n" +"Click Frame : Center on frame
    \n" +"
    \n" +"Main View:
    \n" +"Ctrl + Mouse up/down: Zoom
    \n" +"Mousewheel : Zoom
    \n" +"Right Click + Drag: Select region
    \n" +"Ctrl + Shift + Drag: Select region
    \n" +"Space: Zoom to Selection
    \n" +"Ctrl + Drag: Pan
    \n" +"Click + Drag: Pan
    \n" +"alt or z: Toggle ToolTip
    \n" +"
    \n" +"Detailed View:
    \n" +"Tab: Go To Worst Instance
    \n" +"Left/Right Arrow: Next/Prev Instance
    \n" +"
    \n" +"Timer Views:
    \n" +"Tab: go to filtering
    \n" +"Esc: Exit & Clear filter\n" +"
    \n" +"\n" +"\n" +"\n" +"\n" +"\n" +"
    CloseClose, Never Show
    \n" +"
    \n" +"
    \n" +"
    \n" +"
  • \n" +"
  • Mode\n" +" \n" +"
  • \n" +"
  • Reference\n" +" \n" +"
  • \n" +"
  • Threads\n" +" \n" +"
  • \n" +"
  • Groups\n" +" \n" +"
  • \n" +"
  • Options      \n" +" \n" +"
  • \n" +"
\n" +"
\n" +"\n" +"\n" +"\n" +""; + +const size_t g_MicroProfileHtml_end_3_size = sizeof(g_MicroProfileHtml_end_3); +const char* g_MicroProfileHtml_end[] = { +&g_MicroProfileHtml_end_0[0], +&g_MicroProfileHtml_end_1[0], +&g_MicroProfileHtml_end_2[0], +&g_MicroProfileHtml_end_3[0], +}; +size_t g_MicroProfileHtml_end_sizes[] = { +sizeof(g_MicroProfileHtml_end_0), +sizeof(g_MicroProfileHtml_end_1), +sizeof(g_MicroProfileHtml_end_2), +sizeof(g_MicroProfileHtml_end_3), +}; +size_t g_MicroProfileHtml_end_count = 4; +#endif //MICROPROFILE_EMBED_HTML + +///end file generated from microprofile.html diff --git a/third_party/microprofile/microprofileui.h b/third_party/microprofile/microprofileui.h index ff94614e0..d422445dd 100644 --- a/third_party/microprofile/microprofileui.h +++ b/third_party/microprofile/microprofileui.h @@ -39,7 +39,7 @@ #endif -#if 0 == MICROPROFILEUI_ENABLED +#if 0 == MICROPROFILEUI_ENABLED #define MicroProfileMouseButton(foo, bar) do{}while(0) #define MicroProfileMousePosition(foo, bar, z) do{}while(0) #define MicroProfileModKey(key) do{}while(0) @@ -49,8 +49,6 @@ #define MicroProfileSetDisplayMode(f) do{}while(0) #else -#pragma warning(disable: 4244) // implicit conversion from float -> int - #ifndef MICROPROFILE_DRAWCURSOR #define MICROPROFILE_DRAWCURSOR 0 #endif @@ -83,7 +81,7 @@ #define MICROPROFILE_GRAPH_HEIGHT 256 #endif -#ifndef MICROPROFILE_BORDER_SIZE +#ifndef MICROPROFILE_BORDER_SIZE #define MICROPROFILE_BORDER_SIZE 1 #endif @@ -91,8 +89,8 @@ #define MICROPROFILE_HELP_LEFT "Left-Click" #endif -#ifndef MICROPROFILE_HELP_ALT -#define MICROPROFILE_HELP_ALT "Alt-Click" +#ifndef MICROPROFILE_HELP_RIGHT +#define MICROPROFILE_HELP_RIGHT "Right-Click" #endif #ifndef MICROPROFILE_HELP_MOD @@ -103,6 +101,19 @@ #define MICROPROFILE_BAR_WIDTH 100 #endif +#ifndef MICROPROFILE_CUSTOM_MAX +#define MICROPROFILE_CUSTOM_MAX 8 +#endif + +#ifndef MICROPROFILE_CUSTOM_MAX_TIMERS +#define MICROPROFILE_CUSTOM_MAX_TIMERS 64 +#endif + +#ifndef MICROPROFILE_CUSTOM_PADDING +#define MICROPROFILE_CUSTOM_PADDING 12 +#endif + + #define MICROPROFILE_FRAME_HISTORY_HEIGHT 50 #define MICROPROFILE_FRAME_HISTORY_WIDTH 7 #define MICROPROFILE_FRAME_HISTORY_COLOR_CPU 0xffff7f27 //255 127 39 @@ -116,8 +127,22 @@ #define MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE (4*1024) #define MICROPROFILE_TOOLTIP_MAX_LOCKED 3 +#define MICROPROFILE_COUNTER_INDENT 4 +#define MICROPROFILE_COUNTER_WIDTH 100 + + + +enum +{ + MICROPROFILE_CUSTOM_BARS = 0x1, + MICROPROFILE_CUSTOM_BAR_SOURCE_MAX = 0x2, + MICROPROFILE_CUSTOM_BAR_SOURCE_AVG = 0, + MICROPROFILE_CUSTOM_STACK = 0x4, + MICROPROFILE_CUSTOM_STACK_SOURCE_MAX = 0x8, + MICROPROFILE_CUSTOM_STACK_SOURCE_AVG = 0, +}; + -MICROPROFILEUI_API void MicroProfileUIInit(); MICROPROFILEUI_API void MicroProfileDraw(uint32_t nWidth, uint32_t nHeight); //! call if drawing microprofilers MICROPROFILEUI_API bool MicroProfileIsDrawing(); MICROPROFILEUI_API void MicroProfileToggleGraph(MicroProfileToken nToken); @@ -138,82 +163,175 @@ MICROPROFILEUI_API void MicroProfileDrawBox(int nX, int nY, int nX1, int nY1, ui MICROPROFILEUI_API void MicroProfileDrawLine2D(uint32_t nVertices, float* pVertices, uint32_t nColor); MICROPROFILEUI_API void MicroProfileDumpTimers(); +MICROPROFILEUI_API void MicroProfileInitUI(); + +MICROPROFILEUI_API void MicroProfileCustomGroupToggle(const char* pCustomName); +MICROPROFILEUI_API void MicroProfileCustomGroupEnable(const char* pCustomName); +MICROPROFILEUI_API void MicroProfileCustomGroupEnable(uint32_t nIndex); +MICROPROFILEUI_API void MicroProfileCustomGroupDisable(); +MICROPROFILEUI_API void MicroProfileCustomGroup(const char* pCustomName, uint32_t nMaxTimers, uint32_t nAggregateFlip, float fReferenceTime, uint32_t nFlags); +MICROPROFILEUI_API void MicroProfileCustomGroupAddTimer(const char* pCustomName, const char* pGroup, const char* pTimer); + #ifdef MICROPROFILEUI_IMPL #ifdef _WIN32 #define snprintf _snprintf #endif -#include -#include +#include +#include +#include #include MICROPROFILE_DEFINE(g_MicroProfileDetailed, "MicroProfile", "Detailed View", 0x8888000); MICROPROFILE_DEFINE(g_MicroProfileDrawGraph, "MicroProfile", "Draw Graph", 0xff44ee00); -MICROPROFILE_DEFINE(g_MicroProfileContextSwitchSearch,"MicroProfile", "ContextSwitchSearch", 0xDD7300); MICROPROFILE_DEFINE(g_MicroProfileDrawBarView, "MicroProfile", "DrawBarView", 0x00dd77); MICROPROFILE_DEFINE(g_MicroProfileDraw,"MicroProfile", "Draw", 0x737373); struct MicroProfileStringArray { - const char* ppStrings[MICROPROFILE_TOOLTIP_MAX_STRINGS]; - char Buffer[MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE]; - char* pBufferPos; - uint32_t nNumStrings; + const char* ppStrings[MICROPROFILE_TOOLTIP_MAX_STRINGS]; + char Buffer[MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE]; + char* pBufferPos; + uint32_t nNumStrings; +}; + +struct MicroProfileGroupMenuItem +{ + uint32_t nIsCategory; + uint32_t nCategoryIndex; + uint32_t nIndex; + const char* pName; +}; + +struct MicroProfileCustom +{ + char pName[MICROPROFILE_NAME_MAX_LEN]; + uint32_t nFlags; + uint32_t nAggregateFlip; + uint32_t nNumTimers; + uint32_t nMaxTimers; + uint64_t nGroupMask; + float fReference; + uint64_t* pTimers; +}; + +struct SOptionDesc +{ + SOptionDesc(){} + SOptionDesc(uint8_t nSubType, uint8_t nIndex, const char* fmt, ...):nSubType(nSubType), nIndex(nIndex) + { + va_list args; + va_start (args, fmt); + vsprintf(Text, fmt, args); + va_end(args); + } + char Text[32]; + uint8_t nSubType; + uint8_t nIndex; + bool bSelected; +}; +static uint32_t g_MicroProfileAggregatePresets[] = {0, 10, 20, 30, 60, 120}; +static float g_MicroProfileReferenceTimePresets[] = {5.f, 10.f, 15.f,20.f, 33.33f, 66.66f, 100.f, 250.f, 500.f, 1000.f}; +static uint32_t g_MicroProfileOpacityPresets[] = {0x40, 0x80, 0xc0, 0xff}; +static const char* g_MicroProfilePresetNames[] = +{ + MICROPROFILE_DEFAULT_PRESET, + "Render", + "GPU", + "Lighting", + "AI", + "Visibility", + "Sound", +}; + +enum +{ + MICROPROFILE_NUM_REFERENCE_PRESETS = sizeof(g_MicroProfileReferenceTimePresets)/sizeof(g_MicroProfileReferenceTimePresets[0]), + MICROPROFILE_NUM_OPACITY_PRESETS = sizeof(g_MicroProfileOpacityPresets)/sizeof(g_MicroProfileOpacityPresets[0]), +#if MICROPROFILE_CONTEXT_SWITCH_TRACE + MICROPROFILE_OPTION_SIZE = MICROPROFILE_NUM_REFERENCE_PRESETS + MICROPROFILE_NUM_OPACITY_PRESETS * 2 + 2 + 6, +#else + MICROPROFILE_OPTION_SIZE = MICROPROFILE_NUM_REFERENCE_PRESETS + MICROPROFILE_NUM_OPACITY_PRESETS * 2 + 2 + 3, +#endif }; struct MicroProfileUI { - //menu/mouse over stuff - uint64_t nHoverToken; - int64_t nHoverTime; - int nHoverFrame; + //menu/mouse over stuff + uint64_t nHoverToken; + int64_t nHoverTime; + int nHoverFrame; #if MICROPROFILE_DEBUG - uint64_t nHoverAddressEnter; - uint64_t nHoverAddressLeave; + uint64_t nHoverAddressEnter; + uint64_t nHoverAddressLeave; #endif - uint32_t nWidth; - uint32_t nHeight; + uint32_t nWidth; + uint32_t nHeight; + int nOffsetX[MP_DRAW_SIZE]; + int nOffsetY[MP_DRAW_SIZE]; - int nOffsetY; - float fDetailedOffset; //display offset relative to start of latest displayable frame. - float fDetailedRange; //no. of ms to display - float fDetailedOffsetTarget; - float fDetailedRangeTarget; - uint32_t nOpacityBackground; - uint32_t nOpacityForeground; - bool bShowSpikes; + float fDetailedOffset; //display offset relative to start of latest displayable frame. + float fDetailedRange; //no. of ms to display + float fDetailedOffsetTarget; + float fDetailedRangeTarget; + uint32_t nOpacityBackground; + uint32_t nOpacityForeground; + bool bShowSpikes; - uint32_t nMouseX; - uint32_t nMouseY; - int nMouseWheelDelta; - uint32_t nMouseDownLeft; - uint32_t nMouseDownRight; - uint32_t nMouseLeft; - uint32_t nMouseRight; - uint32_t nMouseLeftMod; - uint32_t nMouseRightMod; - uint32_t nModDown; - uint32_t nActiveMenu; + uint32_t nMouseX; + uint32_t nMouseY; + uint32_t nMouseDownX; + uint32_t nMouseDownY; + int nMouseWheelDelta; + uint32_t nMouseDownLeft; + uint32_t nMouseDownRight; + uint32_t nMouseLeft; + uint32_t nMouseRight; + uint32_t nMouseLeftMod; + uint32_t nMouseRightMod; + uint32_t nModDown; + uint32_t nActiveMenu; - MicroProfileLogEntry* pDisplayMouseOver; + MicroProfileLogEntry* pDisplayMouseOver; - int64_t nRangeBegin; - int64_t nRangeEnd; - int64_t nRangeBeginGpu; - int64_t nRangeEndGpu; - uint32_t nRangeBeginIndex; - uint32_t nRangeEndIndex; - MicroProfileThreadLog* pRangeLog; - uint32_t nHoverColor; - uint32_t nHoverColorShared; + int64_t nRangeBegin; + int64_t nRangeEnd; + int64_t nRangeBeginGpu; + int64_t nRangeEndGpu; + uint32_t nRangeBeginIndex; + uint32_t nRangeEndIndex; + MicroProfileThreadLog* pRangeLog; + uint32_t nHoverColor; + uint32_t nHoverColorShared; + + int64_t nTickReferenceCpu; + int64_t nTickReferenceGpu; + + MicroProfileStringArray LockedToolTips[MICROPROFILE_TOOLTIP_MAX_LOCKED]; + uint32_t nLockedToolTipColor[MICROPROFILE_TOOLTIP_MAX_LOCKED]; + int LockedToolTipFront; + + MicroProfileGroupMenuItem GroupMenu[MICROPROFILE_MAX_GROUPS + MICROPROFILE_MAX_CATEGORIES]; + uint32_t GroupMenuCount; + + + uint32_t nCustomActive; + uint32_t nCustomTimerCount; + uint32_t nCustomCount; + MicroProfileCustom Custom[MICROPROFILE_CUSTOM_MAX]; + uint64_t CustomTimer[MICROPROFILE_CUSTOM_MAX_TIMERS]; + + SOptionDesc Options[MICROPROFILE_OPTION_SIZE]; + + uint32_t nCounterWidth; + uint32_t nLimitWidth; + uint32_t nCounterWidthTemp; + uint32_t nLimitWidthTemp; - MicroProfileStringArray LockedToolTips[MICROPROFILE_TOOLTIP_MAX_LOCKED]; - uint32_t nLockedToolTipColor[MICROPROFILE_TOOLTIP_MAX_LOCKED]; - int LockedToolTipFront; }; @@ -223,412 +341,517 @@ static uint32_t g_nMicroProfileBackColors[2] = { 0x474747, 0x313131 }; #define MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS 16 static uint32_t g_nMicroProfileContextSwitchThreadColors[MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS] = //palette generated by http://tools.medialab.sciences-po.fr/iwanthue/index.php { - 0x63607B, - 0x755E2B, - 0x326A55, - 0x523135, - 0x904F42, - 0x87536B, - 0x346875, - 0x5E6046, - 0x35404C, - 0x224038, - 0x413D1E, - 0x5E3A26, - 0x5D6161, - 0x4C6234, - 0x7D564F, - 0x5C4352, + 0x63607B, + 0x755E2B, + 0x326A55, + 0x523135, + 0x904F42, + 0x87536B, + 0x346875, + 0x5E6046, + 0x35404C, + 0x224038, + 0x413D1E, + 0x5E3A26, + 0x5D6161, + 0x4C6234, + 0x7D564F, + 0x5C4352, }; -static uint32_t g_MicroProfileAggregatePresets[] = {0, 10, 20, 30, 60, 120}; -static float g_MicroProfileReferenceTimePresets[] = {5.f, 10.f, 15.f,20.f, 33.33f, 66.66f, 100.f}; -static uint32_t g_MicroProfileOpacityPresets[] = {0x40, 0x80, 0xc0, 0xff}; -static const char* g_MicroProfilePresetNames[] = -{ - MICROPROFILE_DEFAULT_PRESET, - "Render", - "GPU", - "Lighting", - "AI", - "Visibility", - "Sound", -}; void MicroProfileInitUI() { - static bool bInitialized = false; - if(!bInitialized) - { - bInitialized = true; - memset(&g_MicroProfileUI, 0, sizeof(g_MicroProfileUI)); - UI.nActiveMenu = (uint32_t)-1; - UI.fDetailedOffsetTarget = UI.fDetailedOffset = 0.f; - UI.fDetailedRangeTarget = UI.fDetailedRange = 50.f; + static bool bInitialized = false; + if(!bInitialized) + { + bInitialized = true; + memset(&g_MicroProfileUI, 0, sizeof(g_MicroProfileUI)); + UI.nActiveMenu = (uint32_t)-1; + UI.fDetailedOffsetTarget = UI.fDetailedOffset = 0.f; + UI.fDetailedRangeTarget = UI.fDetailedRange = 50.f; - UI.nOpacityBackground = 0xff<<24; - UI.nOpacityForeground = 0xff<<24; + UI.nOpacityBackground = 0xff<<24; + UI.nOpacityForeground = 0xff<<24; - UI.bShowSpikes = false; + UI.bShowSpikes = false; - UI.nWidth = 100; - UI.nHeight = 100; + UI.nWidth = 100; + UI.nHeight = 100; + UI.nCustomActive = (uint32_t)-1; + UI.nCustomTimerCount = 0; + UI.nCustomCount = 0; - } + int nIndex = 0; + UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "Reference"); + for(int i = 0; i < MICROPROFILE_NUM_REFERENCE_PRESETS; ++i) + { + UI.Options[nIndex++] = SOptionDesc(0, i, " %6.2fms", g_MicroProfileReferenceTimePresets[i]); + } + UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "BG Opacity"); + for(int i = 0; i < MICROPROFILE_NUM_OPACITY_PRESETS; ++i) + { + UI.Options[nIndex++] = SOptionDesc(1, i, " %7d%%", (i+1)*25); + } + UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "FG Opacity"); + for(int i = 0; i < MICROPROFILE_NUM_OPACITY_PRESETS; ++i) + { + UI.Options[nIndex++] = SOptionDesc(2, i, " %7d%%", (i+1)*25); + } + UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "Spike Display"); + UI.Options[nIndex++] = SOptionDesc(3, 0, "%s", " Enable"); + +#if MICROPROFILE_CONTEXT_SWITCH_TRACE + UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "CSwitch Trace"); + UI.Options[nIndex++] = SOptionDesc(4, 0, "%s", " All Threads"); + UI.Options[nIndex++] = SOptionDesc(4, 1, "%s", " No Bars"); +#endif + MP_ASSERT(nIndex == MICROPROFILE_OPTION_SIZE); + + UI.nCounterWidth = 100; + UI.nLimitWidth = 100; + UI.nCounterWidthTemp = 100; + UI.nLimitWidthTemp = 100; + + } } void MicroProfileSetDisplayMode(int nValue) { - MicroProfile& S = *MicroProfileGet(); - nValue = nValue >= 0 && nValue < 4 ? nValue : S.nDisplay; - S.nDisplay = nValue; - UI.nOffsetY = 0; + MicroProfile& S = *MicroProfileGet(); + nValue = nValue >= 0 && nValue < MP_DRAW_SIZE ? nValue : S.nDisplay; + S.nDisplay = nValue; + UI.nOffsetY[S.nDisplay] = 0; } void MicroProfileToggleDisplayMode() { - MicroProfile& S = *MicroProfileGet(); - S.nDisplay = (S.nDisplay + 1) % 4; - UI.nOffsetY = 0; + MicroProfile& S = *MicroProfileGet(); + S.nDisplay = (S.nDisplay + 1) % MP_DRAW_SIZE; + UI.nOffsetY[S.nDisplay] = 0; } void MicroProfileStringArrayClear(MicroProfileStringArray* pArray) { - pArray->nNumStrings = 0; - pArray->pBufferPos = &pArray->Buffer[0]; + pArray->nNumStrings = 0; + pArray->pBufferPos = &pArray->Buffer[0]; } void MicroProfileStringArrayAddLiteral(MicroProfileStringArray* pArray, const char* pLiteral) { - MP_ASSERT(pArray->nNumStrings < MICROPROFILE_TOOLTIP_MAX_STRINGS); - pArray->ppStrings[pArray->nNumStrings++] = pLiteral; + MP_ASSERT(pArray->nNumStrings < MICROPROFILE_TOOLTIP_MAX_STRINGS); + pArray->ppStrings[pArray->nNumStrings++] = pLiteral; } -void MicroProfileStringArrayFormat(MicroProfileStringArray* pArray, const char* fmt, ...) +MICROPROFILE_FORMAT(2, 3) void MicroProfileStringArrayFormat(MicroProfileStringArray* pArray, const char* fmt, ...) { - MP_ASSERT(pArray->nNumStrings < MICROPROFILE_TOOLTIP_MAX_STRINGS); - pArray->ppStrings[pArray->nNumStrings++] = pArray->pBufferPos; - va_list args; - va_start (args, fmt); - pArray->pBufferPos += 1 + vsprintf(pArray->pBufferPos, fmt, args); - va_end(args); - MP_ASSERT(pArray->pBufferPos < pArray->Buffer + MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE); + MP_ASSERT(pArray->nNumStrings < MICROPROFILE_TOOLTIP_MAX_STRINGS); + pArray->ppStrings[pArray->nNumStrings++] = pArray->pBufferPos; + va_list args; + va_start (args, fmt); + pArray->pBufferPos += 1 + vsprintf(pArray->pBufferPos, fmt, args); + va_end(args); + MP_ASSERT(pArray->pBufferPos < pArray->Buffer + MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE); } void MicroProfileStringArrayCopy(MicroProfileStringArray* pDest, MicroProfileStringArray* pSrc) { - memcpy(&pDest->ppStrings[0], &pSrc->ppStrings[0], sizeof(pDest->ppStrings)); - memcpy(&pDest->Buffer[0], &pSrc->Buffer[0], sizeof(pDest->Buffer)); - for(uint32_t i = 0; i < MICROPROFILE_TOOLTIP_MAX_STRINGS; ++i) - { - if(i < pSrc->nNumStrings) - { - if(pSrc->ppStrings[i] >= &pSrc->Buffer[0] && pSrc->ppStrings[i] < &pSrc->Buffer[0] + MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE) - { - pDest->ppStrings[i] += &pDest->Buffer[0] - &pSrc->Buffer[0]; - } - } - } - pDest->nNumStrings = pSrc->nNumStrings; + memcpy(&pDest->ppStrings[0], &pSrc->ppStrings[0], sizeof(pDest->ppStrings)); + memcpy(&pDest->Buffer[0], &pSrc->Buffer[0], sizeof(pDest->Buffer)); + for(uint32_t i = 0; i < MICROPROFILE_TOOLTIP_MAX_STRINGS; ++i) + { + if(i < pSrc->nNumStrings) + { + if(pSrc->ppStrings[i] >= &pSrc->Buffer[0] && pSrc->ppStrings[i] < &pSrc->Buffer[0] + MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE) + { + pDest->ppStrings[i] += &pDest->Buffer[0] - &pSrc->Buffer[0]; + } + } + } + pDest->nNumStrings = pSrc->nNumStrings; } void MicroProfileFloatWindowSize(const char** ppStrings, uint32_t nNumStrings, uint32_t* pColors, uint32_t& nWidth, uint32_t& nHeight, uint32_t* pStringLengths = 0) { - uint32_t* nStringLengths = pStringLengths ? pStringLengths : (uint32_t*)alloca(nNumStrings * sizeof(uint32_t)); - uint32_t nTextCount = nNumStrings/2; - for(uint32_t i = 0; i < nTextCount; ++i) - { - uint32_t i0 = i * 2; - uint32_t s0, s1; - nStringLengths[i0] = s0 = (uint32_t)strlen(ppStrings[i0]); - nStringLengths[i0+1] = s1 = (uint32_t)strlen(ppStrings[i0+1]); - nWidth = MicroProfileMax(s0+s1, nWidth); - } - nWidth = (MICROPROFILE_TEXT_WIDTH+1) * (2+nWidth) + 2 * MICROPROFILE_BORDER_SIZE; - if(pColors) - nWidth += MICROPROFILE_TEXT_WIDTH + 1; - nHeight = (MICROPROFILE_TEXT_HEIGHT+1) * nTextCount + 2 * MICROPROFILE_BORDER_SIZE; + uint32_t* nStringLengths = pStringLengths ? pStringLengths : (uint32_t*)alloca(nNumStrings * sizeof(uint32_t)); + uint32_t nTextCount = nNumStrings/2; + for(uint32_t i = 0; i < nTextCount; ++i) + { + uint32_t i0 = i * 2; + uint32_t s0, s1; + nStringLengths[i0] = s0 = (uint32_t)strlen(ppStrings[i0]); + nStringLengths[i0+1] = s1 = (uint32_t)strlen(ppStrings[i0+1]); + nWidth = MicroProfileMax(s0+s1, nWidth); + } + nWidth = (MICROPROFILE_TEXT_WIDTH+1) * (2+nWidth) + 2 * MICROPROFILE_BORDER_SIZE; + if(pColors) + nWidth += MICROPROFILE_TEXT_WIDTH + 1; + nHeight = (MICROPROFILE_TEXT_HEIGHT+1) * nTextCount + 2 * MICROPROFILE_BORDER_SIZE; } void MicroProfileDrawFloatWindow(uint32_t nX, uint32_t nY, const char** ppStrings, uint32_t nNumStrings, uint32_t nColor, uint32_t* pColors = 0) { - uint32_t nWidth = 0, nHeight = 0; - uint32_t* nStringLengths = (uint32_t*)alloca(nNumStrings * sizeof(uint32_t)); - MicroProfileFloatWindowSize(ppStrings, nNumStrings, pColors, nWidth, nHeight, nStringLengths); - uint32_t nTextCount = nNumStrings/2; - if(nX + nWidth > UI.nWidth) - nX = UI.nWidth - nWidth; - if(nY + nHeight > UI.nHeight) - nY = UI.nHeight - nHeight; - MicroProfileDrawBox(nX-1, nY-1, nX + nWidth+1, nY + nHeight+1, 0xff000000|nColor); - MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000); - if(pColors) - { - nX += MICROPROFILE_TEXT_WIDTH+1; - nWidth -= MICROPROFILE_TEXT_WIDTH+1; - } - for(uint32_t i = 0; i < nTextCount; ++i) - { - int i0 = i * 2; - if(pColors) - { - MicroProfileDrawBox(nX-MICROPROFILE_TEXT_WIDTH, nY, nX, nY + MICROPROFILE_TEXT_WIDTH, pColors[i]|0xff000000); - } - MicroProfileDrawText(nX + 1, nY + 1, (uint32_t)-1, ppStrings[i0], (uint32_t)strlen(ppStrings[i0])); - MicroProfileDrawText(nX + nWidth - nStringLengths[i0+1] * (MICROPROFILE_TEXT_WIDTH+1), nY + 1, (uint32_t)-1, ppStrings[i0+1], (uint32_t)strlen(ppStrings[i0+1])); - nY += (MICROPROFILE_TEXT_HEIGHT+1); - } + uint32_t nWidth = 0, nHeight = 0; + uint32_t* nStringLengths = (uint32_t*)alloca(nNumStrings * sizeof(uint32_t)); + MicroProfileFloatWindowSize(ppStrings, nNumStrings, pColors, nWidth, nHeight, nStringLengths); + uint32_t nTextCount = nNumStrings/2; + if(nX + nWidth > UI.nWidth) + nX = UI.nWidth - nWidth; + if(nY + nHeight > UI.nHeight) + nY = UI.nHeight - nHeight; + MicroProfileDrawBox(nX-1, nY-1, nX + nWidth+1, nY + nHeight+1, 0xff000000|nColor); + MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000); + if(pColors) + { + nX += MICROPROFILE_TEXT_WIDTH+1; + nWidth -= MICROPROFILE_TEXT_WIDTH+1; + } + for(uint32_t i = 0; i < nTextCount; ++i) + { + int i0 = i * 2; + if(pColors) + { + MicroProfileDrawBox(nX-MICROPROFILE_TEXT_WIDTH, nY, nX, nY + MICROPROFILE_TEXT_WIDTH, pColors[i]|0xff000000); + } + MicroProfileDrawText(nX + 1, nY + 1, (uint32_t)-1, ppStrings[i0], (uint32_t)strlen(ppStrings[i0])); + MicroProfileDrawText(nX + nWidth - nStringLengths[i0+1] * (MICROPROFILE_TEXT_WIDTH+1), nY + 1, (uint32_t)-1, ppStrings[i0+1], (uint32_t)strlen(ppStrings[i0+1])); + nY += (MICROPROFILE_TEXT_HEIGHT+1); + } } -void MicroProfileDrawTextBox(uint32_t nX, uint32_t nY, const char** ppStrings, uint32_t nNumStrings, uint32_t nColor, uint32_t* pColors = 0) + +void MicroProfileDrawTextBackground(uint32_t nX, uint32_t nY, uint32_t nColor, uint32_t nBgColor, const char* pString, uint32_t nStrLen) { - uint32_t nWidth = 0, nHeight = 0; - uint32_t* nStringLengths = (uint32_t*)alloca(nNumStrings * sizeof(uint32_t)); - for(uint32_t i = 0; i < nNumStrings; ++i) - { - nStringLengths[i] = (uint32_t)strlen(ppStrings[i]); - nWidth = MicroProfileMax(nWidth, nStringLengths[i]); - nHeight++; - } - nWidth = (MICROPROFILE_TEXT_WIDTH+1) * (2+nWidth) + 2 * MICROPROFILE_BORDER_SIZE; - nHeight = (MICROPROFILE_TEXT_HEIGHT+1) * nHeight + 2 * MICROPROFILE_BORDER_SIZE; - if(nX + nWidth > UI.nWidth) - nX = UI.nWidth - nWidth; - if(nY + nHeight > UI.nHeight) - nY = UI.nHeight - nHeight; - MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000); - for(uint32_t i = 0; i < nNumStrings; ++i) - { - MicroProfileDrawText(nX + 1, nY + 1, (uint32_t)-1, ppStrings[i], (uint32_t)strlen(ppStrings[i])); - nY += (MICROPROFILE_TEXT_HEIGHT+1); - } + uint32_t nWidth = (MICROPROFILE_TEXT_WIDTH + 1) * (nStrLen) + 2 * MICROPROFILE_BORDER_SIZE; + uint32_t nHeight = (MICROPROFILE_TEXT_HEIGHT + 1) ; + MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, nBgColor); + MicroProfileDrawText(nX, nY, nColor, pString, nStrLen); } - - void MicroProfileToolTipMeta(MicroProfileStringArray* pToolTip) { - MicroProfile& S = *MicroProfileGet(); - if(UI.nRangeBeginIndex != UI.nRangeEndIndex && UI.pRangeLog) - { - uint64_t nMetaSum[MICROPROFILE_META_MAX] = {0}; - uint64_t nMetaSumInclusive[MICROPROFILE_META_MAX] = {0}; - int nStackDepth = 0; - uint32_t nRange[2][2]; - MicroProfileThreadLog* pLog = UI.pRangeLog; + MicroProfile& S = *MicroProfileGet(); + if(UI.nRangeBeginIndex != UI.nRangeEndIndex && UI.pRangeLog) + { + uint64_t nMetaSum[MICROPROFILE_META_MAX] = {0}; + uint64_t nMetaSumInclusive[MICROPROFILE_META_MAX] = {0}; + int nStackDepth = 0; + uint32_t nRange[2][2]; + MicroProfileThreadLog* pLog = UI.pRangeLog; - MicroProfileGetRange(UI.nRangeEndIndex, UI.nRangeBeginIndex, nRange); - for(uint32_t i = 0; i < 2; ++i) - { - uint32_t nStart = nRange[i][0]; - uint32_t nEnd = nRange[i][1]; - for(uint32_t j = nStart; j < nEnd; ++j) - { - MicroProfileLogEntry LE = pLog->Log[j]; - int nType = MicroProfileLogType(LE); - switch(nType) - { - case MP_LOG_META: - { - int64_t nMetaIndex = MicroProfileLogTimerIndex(LE); - int64_t nMetaCount = MicroProfileLogGetTick(LE); - MP_ASSERT(nMetaIndex < MICROPROFILE_META_MAX); - if(nStackDepth>1) - { - nMetaSumInclusive[nMetaIndex] += nMetaCount; - } - else - { - nMetaSum[nMetaIndex] += nMetaCount; - } - } - break; - case MP_LOG_LEAVE: - if(nStackDepth) - { - nStackDepth--; - } - else - { - for(int i = 0; i < MICROPROFILE_META_MAX; ++i) - { - nMetaSumInclusive[i] += nMetaSum[i]; - nMetaSum[i] = 0; - } - } - break; - case MP_LOG_ENTER: - nStackDepth++; - break; - } + MicroProfileGetRange(UI.nRangeEndIndex, UI.nRangeBeginIndex, nRange); + for(uint32_t i = 0; i < 2; ++i) + { + uint32_t nStart = nRange[i][0]; + uint32_t nEnd = nRange[i][1]; + for(uint32_t j = nStart; j < nEnd; ++j) + { + MicroProfileLogEntry LE = pLog->Log[j]; + uint64_t nType = MicroProfileLogType(LE); + switch(nType) + { + case MP_LOG_META: + { + int64_t nMetaIndex = MicroProfileLogTimerIndex(LE); + int64_t nMetaCount = MicroProfileLogGetTick(LE); + MP_ASSERT(nMetaIndex < MICROPROFILE_META_MAX); + if(nStackDepth>1) + { + nMetaSumInclusive[nMetaIndex] += nMetaCount; + } + else + { + nMetaSum[nMetaIndex] += nMetaCount; + } + } + break; + case MP_LOG_LEAVE: + if(nStackDepth) + { + nStackDepth--; + } + else + { + for(int i = 0; i < MICROPROFILE_META_MAX; ++i) + { + nMetaSumInclusive[i] += nMetaSum[i]; + nMetaSum[i] = 0; + } + } + break; + case MP_LOG_ENTER: + nStackDepth++; + break; + } - } - } - bool bSpaced = false; - for(int i = 0; i < MICROPROFILE_META_MAX; ++i) - { - if(S.MetaCounters[i].pName && (nMetaSum[i]||nMetaSumInclusive[i])) - { - if(!bSpaced) - { - bSpaced = true; - MicroProfileStringArrayAddLiteral(pToolTip, ""); - MicroProfileStringArrayAddLiteral(pToolTip, ""); - } - MicroProfileStringArrayFormat(pToolTip, "%s excl", S.MetaCounters[i].pName); - MicroProfileStringArrayFormat(pToolTip, "%5d", nMetaSum[i]); - MicroProfileStringArrayFormat(pToolTip, "%s incl", S.MetaCounters[i].pName); - MicroProfileStringArrayFormat(pToolTip, "%5d", nMetaSum[i] + nMetaSumInclusive[i]); - } - } - } + } + } + bool bSpaced = false; + for(int i = 0; i < MICROPROFILE_META_MAX; ++i) + { + if(S.MetaCounters[i].pName && (nMetaSum[i]||nMetaSumInclusive[i])) + { + if(!bSpaced) + { + bSpaced = true; + MicroProfileStringArrayAddLiteral(pToolTip, ""); + MicroProfileStringArrayAddLiteral(pToolTip, ""); + } + MicroProfileStringArrayFormat(pToolTip, "%s excl", S.MetaCounters[i].pName); + MicroProfileStringArrayFormat(pToolTip, "%5lld", (long long)nMetaSum[i]); + MicroProfileStringArrayFormat(pToolTip, "%s incl", S.MetaCounters[i].pName); + MicroProfileStringArrayFormat(pToolTip, "%5lld", (long long)(nMetaSum[i] + nMetaSumInclusive[i])); + } + } + } +} + +void MicroProfileToolTipLabel(MicroProfileStringArray* pToolTip) +{ + if(UI.nRangeBeginIndex != UI.nRangeEndIndex && UI.pRangeLog) + { + bool bSpaced = false; + int nStackDepth = 0; + uint32_t nRange[2][2]; + MicroProfileThreadLog* pLog = UI.pRangeLog; + + MicroProfileGetRange(UI.nRangeEndIndex, UI.nRangeBeginIndex, nRange); + for(uint32_t i = 0; i < 2; ++i) + { + uint32_t nStart = nRange[i][0]; + uint32_t nEnd = nRange[i][1]; + for(uint32_t j = nStart; j < nEnd; ++j) + { + MicroProfileLogEntry LE = pLog->Log[j]; + uint64_t nType = MicroProfileLogType(LE); + switch(nType) + { + case MP_LOG_LABEL: + { + if(nStackDepth == 1) + { + uint64_t nLabel = MicroProfileLogGetTick(LE); + const char* pLabelName = MicroProfileGetLabel(nLabel); + + if (!bSpaced) + { + bSpaced = true; + MicroProfileStringArrayAddLiteral(pToolTip, ""); + MicroProfileStringArrayAddLiteral(pToolTip, ""); + } + + if (pToolTip->nNumStrings + 2 <= MICROPROFILE_TOOLTIP_MAX_STRINGS) + { + MicroProfileStringArrayAddLiteral(pToolTip, "Label:"); + MicroProfileStringArrayAddLiteral(pToolTip, pLabelName ? pLabelName : "??"); + } + } + } + break; + case MP_LOG_LEAVE: + if(nStackDepth) + { + nStackDepth--; + } + break; + case MP_LOG_ENTER: + nStackDepth++; + break; + } + + } + } + } } void MicroProfileDrawFloatTooltip(uint32_t nX, uint32_t nY, uint32_t nToken, uint64_t nTime) { - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); - uint32_t nIndex = MicroProfileGetTimerIndex(nToken); - uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; - uint32_t nAggregateCount = S.Aggregate[nIndex].nCount ? S.Aggregate[nIndex].nCount : 1; + uint32_t nIndex = MicroProfileGetTimerIndex(nToken); + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + uint32_t nAggregateCount = S.Aggregate[nIndex].nCount ? S.Aggregate[nIndex].nCount : 1; - uint32_t nGroupId = MicroProfileGetGroupIndex(nToken); - uint32_t nTimerId = MicroProfileGetTimerIndex(nToken); - bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu; + uint32_t nGroupId = MicroProfileGetGroupIndex(nToken); + uint32_t nTimerId = MicroProfileGetTimerIndex(nToken); + bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu; - float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); - float fMs = fToMs * (nTime); - float fFrameMs = fToMs * (S.Frame[nIndex].nTicks); - float fAverage = fToMs * (S.Aggregate[nIndex].nTicks/nAggregateFrames); - float fCallAverage = fToMs * (S.Aggregate[nIndex].nTicks / nAggregateCount); - float fMax = fToMs * (S.AggregateMax[nIndex]); + float fMs = fToMs * (nTime); + float fFrameMs = fToMs * (S.Frame[nIndex].nTicks); + float fAverage = fToMs * (S.Aggregate[nIndex].nTicks/nAggregateFrames); + float fCallAverage = fToMs * (S.Aggregate[nIndex].nTicks / nAggregateCount); + float fMax = fToMs * (S.AggregateMax[nIndex]); + float fMin = fToMs * (S.AggregateMin[nIndex]); - float fFrameMsExclusive = fToMs * (S.FrameExclusive[nIndex]); - float fAverageExclusive = fToMs * (S.AggregateExclusive[nIndex]/nAggregateFrames); - float fMaxExclusive = fToMs * (S.AggregateMaxExclusive[nIndex]); + float fFrameMsExclusive = fToMs * (S.FrameExclusive[nIndex]); + float fAverageExclusive = fToMs * (S.AggregateExclusive[nIndex]/nAggregateFrames); + float fMaxExclusive = fToMs * (S.AggregateMaxExclusive[nIndex]); + + float fGroupAverage = fToMs * (S.AggregateGroup[nGroupId] / nAggregateFrames); + float fGroupMax = fToMs * (S.AggregateGroupMax[nGroupId]); + float fGroup = fToMs * (S.FrameGroup[nGroupId]); - MicroProfileStringArray ToolTip; - MicroProfileStringArrayClear(&ToolTip); - const char* pGroupName = S.GroupInfo[nGroupId].pName; - const char* pTimerName = S.TimerInfo[nTimerId].pName; - MicroProfileStringArrayFormat(&ToolTip, "%s", pGroupName); - MicroProfileStringArrayFormat(&ToolTip,"%s", pTimerName); + MicroProfileStringArray ToolTip; + MicroProfileStringArrayClear(&ToolTip); + const char* pGroupName = S.GroupInfo[nGroupId].pName; + const char* pTimerName = S.TimerInfo[nTimerId].pName; + MicroProfileStringArrayAddLiteral(&ToolTip, "Timer:"); + MicroProfileStringArrayFormat(&ToolTip, "%s", pTimerName); #if MICROPROFILE_DEBUG - MicroProfileStringArrayFormat(&ToolTip,"0x%p", UI.nHoverAddressEnter); - MicroProfileStringArrayFormat(&ToolTip,"0x%p", UI.nHoverAddressLeave); + MicroProfileStringArrayFormat(&ToolTip,"0x%p", UI.nHoverAddressEnter); + MicroProfileStringArrayFormat(&ToolTip,"0x%p", UI.nHoverAddressLeave); #endif + + if(nTime != (uint64_t)0) + { + MicroProfileStringArrayAddLiteral(&ToolTip, "Time:"); + MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fMs); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + } - if(nTime != (uint64_t)0) - { - MicroProfileStringArrayAddLiteral(&ToolTip, "Time:"); - MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fMs); - MicroProfileStringArrayAddLiteral(&ToolTip, ""); - MicroProfileStringArrayAddLiteral(&ToolTip, ""); - } + MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Time:"); + MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fFrameMs); - MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Time:"); - MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fFrameMs); + MicroProfileStringArrayAddLiteral(&ToolTip, "Average:"); + MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fAverage); - MicroProfileStringArrayAddLiteral(&ToolTip, "Average:"); - MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fAverage); + MicroProfileStringArrayAddLiteral(&ToolTip, "Max:"); + MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fMax); + + MicroProfileStringArrayAddLiteral(&ToolTip, "Min:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fMin); - MicroProfileStringArrayAddLiteral(&ToolTip, "Max:"); - MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fMax); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); - MicroProfileStringArrayAddLiteral(&ToolTip, ""); - MicroProfileStringArrayAddLiteral(&ToolTip, ""); + MicroProfileStringArrayAddLiteral(&ToolTip, "Call Average:"); + MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fCallAverage); - MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Call Average:"); - MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fCallAverage); + MicroProfileStringArrayAddLiteral(&ToolTip, "Call Count:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.2f", double(nAggregateCount) / nAggregateFrames); - MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Call Count:"); - MicroProfileStringArrayFormat(&ToolTip, "%6d", nAggregateCount / nAggregateFrames); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); - MicroProfileStringArrayAddLiteral(&ToolTip, ""); - MicroProfileStringArrayAddLiteral(&ToolTip, ""); + MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Frame Time:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fFrameMsExclusive); - MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Frame Time:"); - MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fFrameMsExclusive); + MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Average:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fAverageExclusive); - MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Average:"); - MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fAverageExclusive); + MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Max:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fMaxExclusive); - MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Max:"); - MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fMaxExclusive); - - MicroProfileToolTipMeta(&ToolTip); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + + MicroProfileStringArrayAddLiteral(&ToolTip, "Group:"); + MicroProfileStringArrayFormat(&ToolTip, "%s", pGroupName); + MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Time:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3f", fGroup); + MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Average:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3f", fGroupAverage); + MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Max:"); + MicroProfileStringArrayFormat(&ToolTip, "%6.3f", fGroupMax); - MicroProfileDrawFloatWindow(nX, nY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, S.TimerInfo[nTimerId].nColor); - if(UI.nMouseLeftMod) - { - int nIndex = (g_MicroProfileUI.LockedToolTipFront + MICROPROFILE_TOOLTIP_MAX_LOCKED - 1) % MICROPROFILE_TOOLTIP_MAX_LOCKED; - g_MicroProfileUI.nLockedToolTipColor[nIndex] = S.TimerInfo[nTimerId].nColor; - MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex], &ToolTip); - g_MicroProfileUI.LockedToolTipFront = nIndex; - } + MicroProfileToolTipMeta(&ToolTip); + MicroProfileToolTipLabel(&ToolTip); + + + MicroProfileDrawFloatWindow(nX, nY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, S.TimerInfo[nTimerId].nColor); + + if(UI.nMouseLeftMod) + { + int nIndex = (g_MicroProfileUI.LockedToolTipFront + MICROPROFILE_TOOLTIP_MAX_LOCKED - 1) % MICROPROFILE_TOOLTIP_MAX_LOCKED; + g_MicroProfileUI.nLockedToolTipColor[nIndex] = S.TimerInfo[nTimerId].nColor; + MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex], &ToolTip); + g_MicroProfileUI.LockedToolTipFront = nIndex; + + } } - -void MicroProfileZoomTo(int64_t nTickStart, int64_t nTickEnd) +int64_t MicroProfileGetGpuTickSync(int64_t nTickCpu, int64_t nTickGpu) { - MicroProfile& S = *MicroProfileGet(); + if(UI.nTickReferenceCpu && UI.nTickReferenceGpu) + { + int64_t nTicksPerSecondCpu = MicroProfileTicksPerSecondCpu(); + int64_t nTicksPerSecondGpu = MicroProfileTicksPerSecondGpu(); - int64_t nStart = S.Frames[S.nFrameCurrent].nFrameStartCpu; - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - UI.fDetailedOffsetTarget = MicroProfileLogTickDifference(nStart, nTickStart) * fToMs; - UI.fDetailedRangeTarget = MicroProfileLogTickDifference(nTickStart, nTickEnd) * fToMs; + return (nTickCpu - UI.nTickReferenceCpu) * int64_t(double(nTicksPerSecondGpu) / double(nTicksPerSecondCpu)) + UI.nTickReferenceGpu; + } + else + { + return nTickGpu; + } +} + +void MicroProfileZoomTo(int64_t nTickStart, int64_t nTickEnd, MicroProfileTokenType eToken) +{ + MicroProfile& S = *MicroProfileGet(); + + bool bGpu = eToken == MicroProfileTokenTypeGpu; + int64_t nStartCpu = S.Frames[S.nFrameCurrent].nFrameStartCpu; + int64_t nStart = bGpu ? MicroProfileGetGpuTickSync(nStartCpu, S.Frames[S.nFrameCurrent].nFrameStartGpu) : nStartCpu; + uint64_t nFrequency = bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu(); + + float fToMs = MicroProfileTickToMsMultiplier(nFrequency); + UI.fDetailedOffsetTarget = MicroProfileLogTickDifference(nStart, nTickStart) * fToMs; + UI.fDetailedRangeTarget = MicroProfileMax(MicroProfileLogTickDifference(nTickStart, nTickEnd) * fToMs, 0.01f); // clamp to 10us } void MicroProfileCenter(int64_t nTickCenter) { - MicroProfile& S = *MicroProfileGet(); - int64_t nStart = S.Frames[S.nFrameCurrent].nFrameStartCpu; - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - float fCenter = MicroProfileLogTickDifference(nStart, nTickCenter) * fToMs; - UI.fDetailedOffsetTarget = UI.fDetailedOffset = fCenter - 0.5f * UI.fDetailedRange; + MicroProfile& S = *MicroProfileGet(); + int64_t nStart = S.Frames[S.nFrameCurrent].nFrameStartCpu; + float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fCenter = MicroProfileLogTickDifference(nStart, nTickCenter) * fToMs; + UI.fDetailedOffsetTarget = UI.fDetailedOffset = fCenter - 0.5f * UI.fDetailedRange; } + #if MICROPROFILE_DEBUG uint64_t* g_pMicroProfileDumpStart = 0; uint64_t* g_pMicroProfileDumpEnd = 0; void MicroProfileDebugDumpRange() { - MicroProfile& S = *MicroProfileGet(); - if(g_pMicroProfileDumpStart != g_pMicroProfileDumpEnd) - { - uint64_t* pStart = g_pMicroProfileDumpStart; - uint64_t* pEnd = g_pMicroProfileDumpEnd; - while(pStart != pEnd) - { - uint64_t nTick = MicroProfileLogGetTick(*pStart); - uint64_t nToken = MicroProfileLogTimerIndex(*pStart); - uint32_t nTimerId = MicroProfileGetTimerIndex(nToken); + MicroProfile& S = *MicroProfileGet(); + if(g_pMicroProfileDumpStart != g_pMicroProfileDumpEnd) + { + uint64_t* pStart = g_pMicroProfileDumpStart; + uint64_t* pEnd = g_pMicroProfileDumpEnd; + while(pStart != pEnd) + { + uint64_t nTick = MicroProfileLogGetTick(*pStart); + uint64_t nToken = MicroProfileLogTimerIndex(*pStart); + uint32_t nTimerId = MicroProfileGetTimerIndex(nToken); + + const char* pTimerName = S.TimerInfo[nTimerId].pName; + char buffer[256]; + uint64_t type = MicroProfileLogType(*pStart); - const char* pTimerName = S.TimerInfo[nTimerId].pName; - char buffer[256]; - int type = MicroProfileLogType(*pStart); - - const char* pBegin = type == MP_LOG_LEAVE ? "END" : - (type == MP_LOG_ENTER ? "BEGIN" : "META"); - snprintf(buffer, 255, "DUMP 0x%p: %s :: %llx: %s\n", pStart, pBegin, nTick, pTimerName); + const char* pBegin = type == MP_LOG_LEAVE ? "END" : + (type == MP_LOG_ENTER ? "BEGIN" : "META"); + snprintf(buffer, 255, "DUMP 0x%p: %s :: %" PRIx64 ": %s\n", pStart, pBegin, nTick, pTimerName); #ifdef _WIN32 - OutputDebugString(buffer); + OutputDebugString(buffer); #else - printf("%s", buffer); + printf("%s", buffer); #endif - pStart++; - } + pStart++; + } - g_pMicroProfileDumpStart = g_pMicroProfileDumpEnd; - } + g_pMicroProfileDumpStart = g_pMicroProfileDumpEnd; + } } -#define MP_DEBUG_DUMP_RANGE() MicroProfileDebugDumpRange(); +#define MP_DEBUG_DUMP_RANGE() MicroProfileDebugDumpRange() #else #define MP_DEBUG_DUMP_RANGE() do{} while(0) #endif @@ -637,1912 +860,2635 @@ void MicroProfileDebugDumpRange() void MicroProfileDrawDetailedContextSwitchBars(uint32_t nY, uint32_t nThreadId, uint32_t nContextSwitchStart, uint32_t nContextSwitchEnd, int64_t nBaseTicks, uint32_t nBaseY) { - MicroProfile& S = *MicroProfileGet(); - int64_t nTickIn = -1; - uint32_t nThreadBefore = -1; - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - float fMsToScreen = UI.nWidth / UI.fDetailedRange; - float fMouseX = (float)UI.nMouseX; - float fMouseY = (float)UI.nMouseY; + MicroProfile& S = *MicroProfileGet(); + int64_t nTickIn = -1; + uint32_t nThreadBefore = -1; + float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fMsToScreen = UI.nWidth / UI.fDetailedRange; + float fMouseX = (float)UI.nMouseX; + float fMouseY = (float)UI.nMouseY; + int nLineDrawn = -1; - for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) - { - MP_ASSERT(j < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE); - MicroProfileContextSwitch CS = S.ContextSwitch[j]; + for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) + { + MP_ASSERT(j < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE); + MicroProfileContextSwitch CS = S.ContextSwitch[j]; - if(nTickIn == -1) - { - if(CS.nThreadIn == nThreadId) - { - nTickIn = CS.nTicks; - nThreadBefore = CS.nThreadOut; - } - } - else - { - if(CS.nThreadOut == nThreadId) - { - int64_t nTickOut = CS.nTicks; - float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickIn); - float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickOut); - if(fMsStart <= fMsEnd) - { - float fXStart = fMsStart * fMsToScreen; - float fXEnd = fMsEnd * fMsToScreen; - float fYStart = (float)nY; - float fYEnd = fYStart + (MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT); - uint32_t nColor = g_nMicroProfileContextSwitchThreadColors[CS.nCpu%MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS]; - float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd); - bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY; - if(bHover) - { - UI.nRangeBegin = nTickIn; - UI.nRangeEnd = nTickOut; - S.nContextSwitchHoverTickIn = nTickIn; - S.nContextSwitchHoverTickOut = nTickOut; - S.nContextSwitchHoverThread = CS.nThreadOut; - S.nContextSwitchHoverThreadBefore = nThreadBefore; - S.nContextSwitchHoverThreadAfter = CS.nThreadIn; - S.nContextSwitchHoverCpuNext = CS.nCpu; - nColor = UI.nHoverColor; - } - if(CS.nCpu == S.nContextSwitchHoverCpu) - { - nColor = UI.nHoverColorShared; - } - MicroProfileDrawBox(fXStart, fYStart, fXEnd, fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeFlat); - } - nTickIn = -1; - } - } - } + if(nTickIn == -1) + { + if(CS.nThreadIn == nThreadId) + { + nTickIn = CS.nTicks; + nThreadBefore = CS.nThreadOut; + } + } + else + { + if(CS.nThreadOut == nThreadId) + { + int64_t nTickOut = CS.nTicks; + float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickIn); + float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickOut); + if(fMsStart <= fMsEnd) + { + float fXStart = fMsStart * fMsToScreen; + float fXEnd = fMsEnd * fMsToScreen; + float fYStart = (float)nY; + float fYEnd = fYStart + (MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT); + uint32_t nColor = g_nMicroProfileContextSwitchThreadColors[CS.nCpu%MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS]; + float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd); + bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY; + if(bHover) + { + UI.nRangeBegin = nTickIn; + UI.nRangeEnd = nTickOut; + S.nContextSwitchHoverTickIn = nTickIn; + S.nContextSwitchHoverTickOut = nTickOut; + S.nContextSwitchHoverThread = CS.nThreadOut; + S.nContextSwitchHoverThreadBefore = nThreadBefore; + S.nContextSwitchHoverThreadAfter = CS.nThreadIn; + S.nContextSwitchHoverCpuNext = CS.nCpu; + nColor = UI.nHoverColor; + } + if(CS.nCpu == S.nContextSwitchHoverCpu) + { + nColor = UI.nHoverColorShared; + } + + uint32_t nIntegerWidth = (uint32_t)(fXEnd - fXStart); + if(nIntegerWidth) + { + MicroProfileDrawBox((int)fXStart, (int)fYStart, (int)fXEnd, (int)fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeFlat); + } + else + { + float fXAvg = 0.5f * (fXStart + fXEnd); + int nLineX = (int)floor(fXAvg+0.5f); + + if(nLineDrawn != nLineX) + { + nLineDrawn = nLineX; + MicroProfileDrawLineVertical(nLineX, (int)(fYStart + 0.5f), (int)(fYEnd + 0.5f), nColor|UI.nOpacityForeground); + } + } + } + nTickIn = -1; + } + } + } +} + +void MicroProfileWriteThreadHeader(uint32_t nY, MicroProfileThreadIdType ThreadId, const char* pNamedThread, const char* pThreadModule) +{ + char Buffer[512]; + int nStrLen = 0; + if(pThreadModule) + { + nStrLen = snprintf(Buffer, sizeof(Buffer) - 1, "%04x: %s [%s]", (uint32_t)ThreadId, pNamedThread ? pNamedThread : "", pThreadModule); + } + else + { + nStrLen = snprintf(Buffer, sizeof(Buffer) - 1, "%04x: %s", (uint32_t)ThreadId, pNamedThread ? pNamedThread : ""); + } + MicroProfileDrawTextBackground(10, nY, 0xffffff, 0x88777777, Buffer, nStrLen); +} + +uint32_t MicroProfileWriteProcessHeader(uint32_t nY, uint32_t nProcessId) +{ + char Name[256]; + const char* pProcessName = MicroProfileGetProcessName(nProcessId, Name, sizeof(Name)); + + char Buffer[512]; + nY += MICROPROFILE_TEXT_HEIGHT + 1; + int nStrLen = 0; + if(pProcessName) + { + nStrLen = snprintf(Buffer, sizeof(Buffer) - 1, "* %04x: %s", nProcessId, pProcessName); + } + else + { + nStrLen = snprintf(Buffer, sizeof(Buffer) - 1, "* %04x", nProcessId); + } + MicroProfileDrawTextBackground(0, nY, 0xffffff, 0x88777777, Buffer, nStrLen); + nY += MICROPROFILE_TEXT_HEIGHT + 1; + return nY; +} + +void MicroProfileGetFrameRange(int64_t nTicks, int64_t nTicksEnd, int32_t nLogIndex, uint32_t* nFrameBegin, uint32_t* nFrameEnd) +{ + MicroProfile& S = *MicroProfileGet(); + + bool bGpu = (nLogIndex >= 0) ? S.Pool[nLogIndex]->nGpu != 0 : false; + uint32_t nPut = (nLogIndex >= 0) ? S.Pool[nLogIndex]->nPut.load(std::memory_order_relaxed) : 0; + + uint32_t nBegin = S.nFrameCurrent; + + for(uint32_t i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY; ++i) + { + uint32_t nFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; + + if(nLogIndex >= 0) + { + uint32_t nCurrStart = S.Frames[nBegin].nLogStart[nLogIndex]; + uint32_t nPrevStart = S.Frames[nFrame].nLogStart[nLogIndex]; + bool bOverflow = (nPrevStart <= nCurrStart) ? (nPut >= nPrevStart && nPut < nCurrStart) : (nPut < nCurrStart || nPut >= nPrevStart); + if(bOverflow) + break; + } + + nBegin = nFrame; + if((bGpu ? S.Frames[nBegin].nFrameStartGpu : S.Frames[nBegin].nFrameStartCpu) <= nTicks) + break; + } + + uint32_t nEnd = nBegin; + + while (nEnd != S.nFrameCurrent) + { + nEnd = (nEnd + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + if((bGpu ? S.Frames[nEnd].nFrameStartGpu : S.Frames[nEnd].nFrameStartCpu) >= nTicksEnd) + break; + } + + *nFrameBegin = nBegin; + *nFrameEnd = nEnd; } void MicroProfileDrawDetailedBars(uint32_t nWidth, uint32_t nHeight, int nBaseY, int nSelectedFrame) { - MicroProfile& S = *MicroProfileGet(); - MP_DEBUG_DUMP_RANGE(); - int nY = nBaseY - UI.nOffsetY; - int64_t nNumBoxes = 0; - int64_t nNumLines = 0; + MicroProfile& S = *MicroProfileGet(); + MP_DEBUG_DUMP_RANGE(); + int nY = nBaseY - UI.nOffsetY[MP_DRAW_DETAILED]; + int64_t nNumBoxes = 0; + int64_t nNumLines = 0; - uint32_t nFrameNext = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY; - MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; - MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext]; + UI.nRangeBegin = 0; + UI.nRangeEnd = 0; + UI.nRangeBeginGpu = 0; + UI.nRangeEndGpu = 0; + UI.nRangeBeginIndex = UI.nRangeEndIndex = 0; + UI.pRangeLog = 0; - UI.nRangeBegin = 0; - UI.nRangeEnd = 0; - UI.nRangeBeginGpu = 0; - UI.nRangeEndGpu = 0; - UI.nRangeBeginIndex = UI.nRangeEndIndex = 0; - UI.pRangeLog = 0; - uint64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu; - uint64_t nFrameStartGpu = pFrameCurrent->nFrameStartGpu; - float fToMsCpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + int64_t nFrameStartCpu = S.Frames[S.nFrameCurrent].nFrameStartCpu; + int64_t nFrameStartGpu = S.Frames[S.nFrameCurrent].nFrameStartGpu; + int64_t nTicksPerSecondCpu = MicroProfileTicksPerSecondCpu(); + int64_t nTicksPerSecondGpu = MicroProfileTicksPerSecondGpu(); + float fToMsCpu = MicroProfileTickToMsMultiplier(nTicksPerSecondCpu); + float fToMsGpu = MicroProfileTickToMsMultiplier(nTicksPerSecondGpu); - float fDetailedOffset = UI.fDetailedOffset; - float fDetailedRange = UI.fDetailedRange; - int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(fDetailedOffset, MicroProfileTicksPerSecondCpu()); - int64_t nDetailedOffsetTicksGpu = MicroProfileMsToTick(fDetailedOffset, MicroProfileTicksPerSecondGpu()); - int64_t nBaseTicksCpu = nDetailedOffsetTicksCpu + nFrameStartCpu; - int64_t nBaseTicksGpu = nDetailedOffsetTicksGpu + nFrameStartGpu; - int64_t nBaseTicksEndCpu = nBaseTicksCpu + MicroProfileMsToTick(fDetailedRange, MicroProfileTicksPerSecondCpu()); + if(!S.nRunning && UI.nTickReferenceCpu < nFrameStartCpu) + { + int64_t nRefCpu = 0, nRefGpu = 0; + if(MicroProfileGetGpuTickReference(&nRefCpu, &nRefGpu)) + { + UI.nTickReferenceCpu = nRefCpu; + UI.nTickReferenceGpu = nRefGpu; + } + } - MicroProfileFrameState* pFrameFirst = pFrameCurrent; - int64_t nGapTime = MicroProfileTicksPerSecondCpu() * MICROPROFILE_GAP_TIME / 1000; - for(uint32_t i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY; ++i) - { - uint32_t nNextIndex = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; - pFrameFirst = &S.Frames[nNextIndex]; - if(pFrameFirst->nFrameStartCpu <= nBaseTicksCpu-nGapTime) - break; - } + float fDetailedOffset = UI.fDetailedOffset; + float fDetailedRange = UI.fDetailedRange; - float fMsBase = fToMsCpu * nDetailedOffsetTicksCpu; - float fMs = fDetailedRange; - float fMsEnd = fMs + fMsBase; - float fWidth = (float)nWidth; - float fMsToScreen = fWidth / fMs; + int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(fDetailedOffset, MicroProfileTicksPerSecondCpu()); + int64_t nDetailedOffsetTicksGpu = MicroProfileMsToTick(fDetailedOffset, MicroProfileTicksPerSecondGpu()); + int64_t nBaseTicksCpu = nDetailedOffsetTicksCpu + nFrameStartCpu; + int64_t nBaseTicksGpu = MicroProfileGetGpuTickSync(nBaseTicksCpu, nDetailedOffsetTicksGpu + nFrameStartGpu); + int64_t nBaseTicksEndCpu = nBaseTicksCpu + MicroProfileMsToTick(fDetailedRange, MicroProfileTicksPerSecondCpu()); + int64_t nBaseTicksEndGpu = nBaseTicksGpu + MicroProfileMsToTick(fDetailedRange, MicroProfileTicksPerSecondGpu()); - { - float fRate = floor(2*(log10(fMs)-1))/2; - float fStep = powf(10.f, fRate); - float fRcpStep = 1.f / fStep; - int nColorIndex = (int)(floor(fMsBase*fRcpStep)); - float fStart = floor(fMsBase*fRcpStep) * fStep; - for(float f = fStart; f < fMsEnd; ) - { - float fStart = f; - float fNext = f + fStep; - MicroProfileDrawBox(((fStart-fMsBase) * fMsToScreen), nBaseY, (fNext-fMsBase) * fMsToScreen+1, nBaseY + nHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[nColorIndex++ & 1]); - f = fNext; - } - } + uint32_t nFrameBegin, nFrameEnd; + MicroProfileGetFrameRange(nBaseTicksCpu, nBaseTicksEndCpu, -1, &nFrameBegin, &nFrameEnd); - nY += MICROPROFILE_TEXT_HEIGHT+1; - MicroProfileLogEntry* pMouseOver = UI.pDisplayMouseOver; - MicroProfileLogEntry* pMouseOverNext = 0; - uint64_t nMouseOverToken = pMouseOver ? MicroProfileLogTimerIndex(*pMouseOver) : MICROPROFILE_INVALID_TOKEN; - float fMouseX = (float)UI.nMouseX; - float fMouseY = (float)UI.nMouseY; - uint64_t nHoverToken = MICROPROFILE_INVALID_TOKEN; - int64_t nHoverTime = 0; + float fMsBase = fToMsCpu * nDetailedOffsetTicksCpu; + float fMs = fDetailedRange; + float fMsEnd = fMs + fMsBase; + float fWidth = (float)nWidth; + float fMsToScreen = fWidth / fMs; - static int nHoverCounter = 155; - static int nHoverCounterDelta = 10; - nHoverCounter += nHoverCounterDelta; - if(nHoverCounter >= 245) - nHoverCounterDelta = -10; - else if(nHoverCounter < 100) - nHoverCounterDelta = 10; - UI.nHoverColor = (nHoverCounter<<24)|(nHoverCounter<<16)|(nHoverCounter<<8)|nHoverCounter; - uint32_t nHoverCounterShared = nHoverCounter>>2; - UI.nHoverColorShared = (nHoverCounterShared<<24)|(nHoverCounterShared<<16)|(nHoverCounterShared<<8)|nHoverCounterShared; + for(uint32_t i = nFrameBegin; i != nFrameEnd; i = (i+1) % MICROPROFILE_MAX_FRAME_HISTORY) + { + uint64_t nTickStart = S.Frames[i].nFrameStartCpu; + float fMsStart = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, nTickStart); + float fXStart = fMsStart * fMsToScreen; - uint32_t nLinesDrawn[MICROPROFILE_STACK_MAX]={0}; + MicroProfileDrawLineVertical((int)fXStart, nBaseY, nBaseY + nHeight, UI.nOpacityForeground | 0xbbbbbb); + } - uint32_t nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadAfter; - uint32_t nContextSwitchHoverThreadBefore = S.nContextSwitchHoverThreadBefore; - S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = -1; + { + float fRate = floor(2*(log10(fMs)-1))/2; + float fStep = powf(10.f, fRate); + float fRcpStep = 1.f / fStep; + int nColorIndex = (int)(floor(fMsBase*fRcpStep)); + float fStart = floor(fMsBase*fRcpStep) * fStep; - uint32_t nContextSwitchStart = -1; - uint32_t nContextSwitchEnd = -1; - S.nContextSwitchHoverCpuNext = 0xff; - S.nContextSwitchHoverTickIn = -1; - S.nContextSwitchHoverTickOut = -1; - if(S.bContextSwitchRunning) - { - MICROPROFILE_SCOPE(g_MicroProfileContextSwitchSearch); - uint32_t nContextSwitchPut = S.nContextSwitchPut; - nContextSwitchStart = nContextSwitchEnd = (nContextSwitchPut + MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - 1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; - int64_t nSearchEnd = nBaseTicksEndCpu + MicroProfileMsToTick(30.f, MicroProfileTicksPerSecondCpu()); - int64_t nSearchBegin = nBaseTicksCpu - MicroProfileMsToTick(30.f, MicroProfileTicksPerSecondCpu()); - for(uint32_t i = 0; i < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; ++i) - { - uint32_t nIndex = (nContextSwitchPut + MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - (i+1)) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; - MicroProfileContextSwitch& CS = S.ContextSwitch[nIndex]; - if(CS.nTicks > nSearchEnd) - { - nContextSwitchEnd = nIndex; - } - if(CS.nTicks > nSearchBegin) - { - nContextSwitchStart = nIndex; - } - } - } + char StepLabel[64] = ""; + if(fStep >= 0.005 && fStep <= 1000) + { + if(fStep >= 1) + sprintf(StepLabel, "%.3gms", fStep); + else + sprintf(StepLabel, "%.2fms", fStep); + } - bool bSkipBarView = S.bContextSwitchRunning && S.bContextSwitchNoBars; + uint32_t nStepLabelLength = (uint32_t)strlen(StepLabel); + float fStepLabelOffset = (fStep*fMsToScreen-nStepLabelLength*(MICROPROFILE_TEXT_WIDTH+1))/2; - if(!bSkipBarView) - { - for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) - { - MicroProfileThreadLog* pLog = S.Pool[i]; - if(!pLog) - continue; + for(float f = fStart; f < fMsEnd; ) + { + float fStart = f; + float fNext = f + fStep; + MicroProfileDrawBox((int)((fStart-fMsBase) * fMsToScreen), nBaseY, (int)((fNext-fMsBase) * fMsToScreen+1), nBaseY + nHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[nColorIndex++ & 1]); - uint32_t nPut = pFrameNext->nLogStart[i]; - ///note: this may display new samples as old data, but this will only happen when - // unpaused, where the detailed view is hardly perceptible - uint32_t nFront = S.Pool[i]->nPut.load(std::memory_order_relaxed); - MicroProfileFrameState* pFrameLogFirst = pFrameCurrent; - MicroProfileFrameState* pFrameLogLast = pFrameNext; - uint32_t nGet = pFrameLogFirst->nLogStart[i]; - do - { - MP_ASSERT(pFrameLogFirst >= &S.Frames[0] && pFrameLogFirst < &S.Frames[MICROPROFILE_MAX_FRAME_HISTORY]); - uint32_t nNewGet = pFrameLogFirst->nLogStart[i]; - bool bIsValid = false; - if(nPut < nFront) - { - bIsValid = nNewGet <= nPut || nNewGet >= nFront; - } - else - { - bIsValid = nNewGet <= nPut && nNewGet >= nFront; - } - if(bIsValid) - { - nGet = nNewGet; - if(pFrameLogFirst->nFrameStartCpu > nBaseTicksEndCpu) - { - pFrameLogLast = pFrameLogFirst;//pick the last frame that ends after - } + if(nStepLabelLength) + MicroProfileDrawText((int)((fStart-fMsBase) * fMsToScreen + fStepLabelOffset), nBaseY, UI.nOpacityForeground | 0x808080, StepLabel, nStepLabelLength); + f = fNext; + } + } - pFrameLogFirst--; - if(pFrameLogFirst < &S.Frames[0]) - pFrameLogFirst = &S.Frames[MICROPROFILE_MAX_FRAME_HISTORY-1]; - } - else - { - break; - } - }while(pFrameLogFirst != pFrameFirst); + nY += MICROPROFILE_TEXT_HEIGHT+1; + MicroProfileLogEntry* pMouseOver = UI.pDisplayMouseOver; + MicroProfileLogEntry* pMouseOverNext = 0; + uint64_t nMouseOverToken = pMouseOver ? MicroProfileLogTimerIndex(*pMouseOver) : MICROPROFILE_INVALID_TOKEN; + float fMouseX = (float)UI.nMouseX; + float fMouseY = (float)UI.nMouseY; + uint64_t nHoverToken = MICROPROFILE_INVALID_TOKEN; + int64_t nHoverTime = 0; + static int nHoverCounter = 155; + static int nHoverCounterDelta = 10; + nHoverCounter += nHoverCounterDelta; + if(nHoverCounter >= 245) + nHoverCounterDelta = -10; + else if(nHoverCounter < 100) + nHoverCounterDelta = 10; + UI.nHoverColor = (nHoverCounter<<24)|(nHoverCounter<<16)|(nHoverCounter<<8)|nHoverCounter; + uint32_t nHoverCounterShared = nHoverCounter>>2; + UI.nHoverColorShared = (nHoverCounterShared<<24)|(nHoverCounterShared<<16)|(nHoverCounterShared<<8)|nHoverCounterShared; - if(nGet == (uint32_t)-1) - continue; - MP_ASSERT(nGet != (uint32_t)-1); + uint32_t nLinesDrawn[MICROPROFILE_STACK_MAX]={0}; - nPut = pFrameLogLast->nLogStart[i]; + S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = -1; - uint32_t nRange[2][2] = { {0, 0}, {0, 0}, }; + uint32_t nContextSwitchStart = -1; + uint32_t nContextSwitchEnd = -1; + S.nContextSwitchHoverCpuNext = 0xff; + S.nContextSwitchHoverTickIn = -1; + S.nContextSwitchHoverTickOut = -1; + if(S.bContextSwitchRunning) + { + MicroProfileContextSwitchSearch(&nContextSwitchStart, &nContextSwitchEnd, nBaseTicksCpu, nBaseTicksEndCpu); + } - MicroProfileGetRange(nPut, nGet, nRange); - if(nPut == nGet) - continue; - uint32_t nMaxStackDepth = 0; + uint64_t nActiveGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted; - bool bGpu = pLog->nGpu != 0; - float fToMs = bGpu ? fToMsGpu : fToMsCpu; - int64_t nBaseTicks = bGpu ? nBaseTicksGpu : nBaseTicksCpu; - char ThreadName[MicroProfileThreadLog::THREAD_MAX_LEN + 16]; - uint64_t nThreadId = pLog->nThreadId; - snprintf(ThreadName, sizeof(ThreadName)-1, "%04llx: %s", nThreadId, &pLog->ThreadName[0] ); - nY += 3; - uint32_t nThreadColor = -1; - if(pLog->nThreadId == nContextSwitchHoverThreadAfter || pLog->nThreadId == nContextSwitchHoverThreadBefore) - nThreadColor = UI.nHoverColorShared|0x906060; - MicroProfileDrawText(0, nY, nThreadColor, &ThreadName[0], (uint32_t)strlen(&ThreadName[0])); - nY += 3; - nY += MICROPROFILE_TEXT_HEIGHT + 1; + bool bSkipBarView = S.bContextSwitchRunning && S.bContextSwitchNoBars; - if(S.bContextSwitchRunning) - { - MicroProfileDrawDetailedContextSwitchBars(nY, pLog->nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicks, nBaseY); - nY -= MICROPROFILE_DETAILED_BAR_HEIGHT; - nY += MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT+1; - } + if(!bSkipBarView) + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(!pLog) + continue; - uint32_t nYDelta = MICROPROFILE_DETAILED_BAR_HEIGHT; - uint32_t nStack[MICROPROFILE_STACK_MAX]; - uint32_t nStackPos = 0; - for(uint32_t j = 0; j < 2; ++j) - { - uint32_t nStart = nRange[j][0]; - uint32_t nEnd = nRange[j][1]; - for(uint32_t k = nStart; k < nEnd; ++k) - { - MicroProfileLogEntry* pEntry = pLog->Log + k; - int nType = MicroProfileLogType(*pEntry); - if(MP_LOG_ENTER == nType) - { - MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX); - nStack[nStackPos++] = k; - } - else if(MP_LOG_META == nType) - { + bool bGpu = pLog->nGpu != 0; + float fToMs = bGpu ? fToMsGpu : fToMsCpu; + int64_t nBaseTicks = bGpu ? nBaseTicksGpu : nBaseTicksCpu; + int64_t nBaseTicksEnd = bGpu ? nBaseTicksEndGpu : nBaseTicksEndCpu; + MicroProfileThreadIdType nThreadId = pLog->nThreadId; - } - else if(MP_LOG_LEAVE == nType) - { - if(0 == nStackPos) - { - continue; - } + int64_t nGapTime = (bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()) * MICROPROFILE_GAP_TIME / 1000; - MicroProfileLogEntry* pEntryEnter = pLog->Log + nStack[nStackPos-1]; - if(MicroProfileLogTimerIndex(*pEntryEnter) != MicroProfileLogTimerIndex(*pEntry)) - { - //uprintf("mismatch %llx %llx\n", pEntryEnter->nToken, pEntry->nToken); - continue; - } - int64_t nTickStart = MicroProfileLogGetTick(*pEntryEnter); - int64_t nTickEnd = MicroProfileLogGetTick(*pEntry); - uint64_t nTimerIndex = MicroProfileLogTimerIndex(*pEntry); - uint32_t nColor = S.TimerInfo[nTimerIndex].nColor; - if(nMouseOverToken == nTimerIndex) - { - if(pEntry == pMouseOver) - { - nColor = UI.nHoverColor; - if(bGpu) - { - UI.nRangeBeginGpu = *pEntryEnter; - UI.nRangeEndGpu = *pEntry; - UI.nRangeBeginIndex = nStack[nStackPos-1]; - UI.nRangeEndIndex = k; - UI.pRangeLog = pLog; - } - else - { - UI.nRangeBegin = *pEntryEnter; - UI.nRangeEnd = *pEntry; - UI.nRangeBeginIndex = nStack[nStackPos-1]; - UI.nRangeEndIndex = k; - UI.pRangeLog = pLog; + uint32_t nLogFrameBegin, nLogFrameEnd; + MicroProfileGetFrameRange(nBaseTicks - nGapTime, nBaseTicksEnd + nGapTime, i, &nLogFrameBegin, &nLogFrameEnd); - } - } - else - { - nColor = UI.nHoverColorShared; - } - } + uint32_t nGet = S.Frames[nLogFrameBegin].nLogStart[i]; + uint32_t nPut = nLogFrameEnd == S.nFrameCurrent ? pLog->nPut.load(std::memory_order_relaxed) : S.Frames[nLogFrameEnd].nLogStart[i]; + if(nPut == nGet) + continue; - nMaxStackDepth = MicroProfileMax(nMaxStackDepth, nStackPos); - float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickStart); - float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickEnd); - float fXStart = fMsStart * fMsToScreen; - float fXEnd = fMsEnd * fMsToScreen; - float fYStart = (float)(nY + nStackPos * nYDelta); - float fYEnd = fYStart + (MICROPROFILE_DETAILED_BAR_HEIGHT); - float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd); - bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY; - uint32_t nIntegerWidth = (uint32_t)(fXEnd - fXStart); - if(nIntegerWidth) - { - if(bHover && UI.nActiveMenu == -1) - { - nHoverToken = MicroProfileLogTimerIndex(*pEntry); - #if MICROPROFILE_DEBUG - UI.nHoverAddressEnter = (uint64_t)pEntryEnter; - UI.nHoverAddressLeave = (uint64_t)pEntry; - #endif - nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd); - pMouseOverNext = pEntry; - } + uint32_t nRange[2][2] = { {0, 0}, {0, 0}, }; + MicroProfileGetRange(nPut, nGet, nRange); - MicroProfileDrawBox(fXStart, fYStart, fXEnd, fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeBar); + uint32_t nMaxStackDepth = 0; + + nY += 3; + MicroProfileWriteThreadHeader(nY, nThreadId, &pLog->ThreadName[0], nullptr); + nY += 3; + nY += MICROPROFILE_TEXT_HEIGHT + 1; + + if(S.bContextSwitchRunning) + { + MicroProfileDrawDetailedContextSwitchBars(nY, pLog->nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicks, nBaseY); + nY -= MICROPROFILE_DETAILED_BAR_HEIGHT; + nY += MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT+1; + } + + uint32_t nYDelta = MICROPROFILE_DETAILED_BAR_HEIGHT; + uint32_t nStack[MICROPROFILE_STACK_MAX]; + uint32_t nStackPos = 0; + for(uint32_t j = 0; j < 2; ++j) + { + uint32_t nStart = nRange[j][0]; + uint32_t nEnd = nRange[j][1]; + for(uint32_t k = nStart; k < nEnd; ++k) + { + MicroProfileLogEntry* pEntry = pLog->Log + k; + uint64_t nType = MicroProfileLogType(*pEntry); + if(MP_LOG_ENTER == nType) + { + MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX); + nStack[nStackPos++] = k; + } + else if(MP_LOG_META == nType) + { + + } + else if(MP_LOG_LEAVE == nType) + { + if(0 == nStackPos) + { + continue; + } + + MicroProfileLogEntry* pEntryEnter = pLog->Log + nStack[nStackPos-1]; + if(MicroProfileLogTimerIndex(*pEntryEnter) != MicroProfileLogTimerIndex(*pEntry)) + { + //uprintf("mismatch %llx %llx\n", pEntryEnter->nToken, pEntry->nToken); + continue; + } + int64_t nTickStart = MicroProfileLogGetTick(*pEntryEnter); + int64_t nTickEnd = MicroProfileLogGetTick(*pEntry); + uint64_t nTimerIndex = MicroProfileLogTimerIndex(*pEntry); + uint32_t nColor = S.TimerInfo[nTimerIndex].nColor; + if(!(nActiveGroup & (1ull << S.TimerInfo[nTimerIndex].nGroupIndex))) + { + nStackPos--; + continue; + } + if(nMouseOverToken == nTimerIndex) + { + if(pEntry == pMouseOver) + { + nColor = UI.nHoverColor; + if(bGpu) + { + UI.nRangeBeginGpu = *pEntryEnter; + UI.nRangeEndGpu = *pEntry; + uint32_t nCpuBegin = (nStack[nStackPos-1] + 1) % MICROPROFILE_BUFFER_SIZE; + uint32_t nCpuEnd = (k + 1) % MICROPROFILE_BUFFER_SIZE; + MicroProfileLogEntry LogCpuBegin = pLog->Log[nCpuBegin]; + MicroProfileLogEntry LogCpuEnd = pLog->Log[nCpuEnd]; + if(MicroProfileLogType(LogCpuBegin) == MP_LOG_GPU_EXTRA && MicroProfileLogType(LogCpuEnd) == MP_LOG_GPU_EXTRA) + { + UI.nRangeBegin = LogCpuBegin; + UI.nRangeEnd = LogCpuEnd; + } + UI.nRangeBeginIndex = nStack[nStackPos-1]; + UI.nRangeEndIndex = k; + UI.pRangeLog = pLog; + } + else + { + UI.nRangeBegin = *pEntryEnter; + UI.nRangeEnd = *pEntry; + UI.nRangeBeginIndex = nStack[nStackPos-1]; + UI.nRangeEndIndex = k; + UI.pRangeLog = pLog; + + } + } + else + { + nColor = UI.nHoverColorShared; + } + } + + const char* pName = S.TimerInfo[nTimerIndex].pName; + uint32_t nNameLen = S.TimerInfo[nTimerIndex].nNameLen; + + if (pName[0] == '$' && pEntryEnter < pEntry && MicroProfileLogType(pEntryEnter[1 + bGpu]) == MP_LOG_LABEL) + { + const char* pLabel = MicroProfileGetLabel(MicroProfileLogGetTick(pEntryEnter[1 + bGpu])); + + if (pLabel) + { + pName = pLabel; + nNameLen = (uint32_t)strlen(pLabel); + } + } + + nMaxStackDepth = MicroProfileMax(nMaxStackDepth, nStackPos); + float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickStart); + float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickEnd); + float fXStart = fMsStart * fMsToScreen; + float fXEnd = fMsEnd * fMsToScreen; + float fYStart = (float)(nY + nStackPos * nYDelta); + float fYEnd = fYStart + (MICROPROFILE_DETAILED_BAR_HEIGHT); + float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd); + bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY; + uint32_t nIntegerWidth = (uint32_t)(fXEnd - fXStart); + if(nIntegerWidth) + { + if(bHover && UI.nActiveMenu == (uint32_t)-1) + { + nHoverToken = MicroProfileLogTimerIndex(*pEntry); + #if MICROPROFILE_DEBUG + UI.nHoverAddressEnter = (uint64_t)pEntryEnter; + UI.nHoverAddressLeave = (uint64_t)pEntry; + #endif + nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd); + pMouseOverNext = pEntry; + } + + MicroProfileDrawBox((int)fXStart, (int)fYStart, (int)fXEnd, (int)fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeBar); #if MICROPROFILE_DETAILED_BAR_NAMES - if(nIntegerWidth>3*MICROPROFILE_TEXT_WIDTH) - { - float fXStartText = MicroProfileMax(fXStart, 0.f); - int nTextWidth = (int)(fXEnd - fXStartText); - int nCharacters = (nTextWidth - 2*MICROPROFILE_TEXT_WIDTH) / MICROPROFILE_TEXT_WIDTH; - if(nCharacters>0) - { - MicroProfileDrawText(fXStartText+1, fYStart+1, -1, S.TimerInfo[nTimerIndex].pName, MicroProfileMin(S.TimerInfo[nTimerIndex].nNameLen, nCharacters)); - } - } + if(nIntegerWidth>3*MICROPROFILE_TEXT_WIDTH) + { + float fXStartText = MicroProfileMax(fXStart, 0.f); + int nTextWidth = (int)(fXEnd - fXStartText); + int nCharacters = (nTextWidth - MICROPROFILE_TEXT_WIDTH) / (MICROPROFILE_TEXT_WIDTH+1); + if(nCharacters>0) + { + MicroProfileDrawText((int)(fXStartText+1), (int)(fYStart+1), -1, pName, MicroProfileMin(nNameLen, nCharacters)); + } + } #endif - ++nNumBoxes; - } - else - { - float fXAvg = 0.5f * (fXStart + fXEnd); - int nLineX = (int)floor(fXAvg+0.5f); - if(nLineX != (int)nLinesDrawn[nStackPos]) - { - if(bHover && UI.nActiveMenu == -1) - { - nHoverToken = (uint32_t)MicroProfileLogTimerIndex(*pEntry); - nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd); - pMouseOverNext = pEntry; - } - nLinesDrawn[nStackPos] = nLineX; - MicroProfileDrawLineVertical(nLineX, fYStart + 0.5f, fYEnd + 0.5f, nColor|UI.nOpacityForeground); - ++nNumLines; - } - } - nStackPos--; - } - } - } - nY += nMaxStackDepth * nYDelta + MICROPROFILE_DETAILED_BAR_HEIGHT+1; - } - } - if(S.bContextSwitchRunning && (S.bContextSwitchAllThreads||S.bContextSwitchNoBars)) - { - uint32_t nNumThreads = 0; - uint32_t nThreads[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS]; - for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS && S.Pool[i]; ++i) - nThreads[nNumThreads++] = S.Pool[i]->nThreadId; - uint32_t nNumThreadsBase = nNumThreads; - if(S.bContextSwitchAllThreads) - { - for(uint32_t i = nContextSwitchStart; i != nContextSwitchEnd; i = (i+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) - { - MicroProfileContextSwitch CS = S.ContextSwitch[i]; - uint32_t nThreadId = CS.nThreadIn; - if(nThreadId) - { - bool bSeen = false; - for(uint32_t j = 0; j < nNumThreads; ++j) - { - if(nThreads[j] == nThreadId) - { - bSeen = true; - break; - } - } - if(!bSeen) - { - nThreads[nNumThreads++] = nThreadId; - } - } - if(nNumThreads == MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS) - { - S.nOverflow = 10; - break; - } - } - std::sort(&nThreads[nNumThreadsBase], &nThreads[nNumThreads]); - } - uint32_t nStart = nNumThreadsBase; - if(S.bContextSwitchNoBars) - nStart = 0; - for(uint32_t i = nStart; i < nNumThreads; ++i) - { - uint32_t nThreadId = nThreads[i]; - if(nThreadId) - { - char ThreadName[MicroProfileThreadLog::THREAD_MAX_LEN + 16]; - const char* cLocal = MicroProfileIsLocalThread(nThreadId) ? "*": " "; - int nStrLen = snprintf(ThreadName, sizeof(ThreadName)-1, "%04x: %s", nThreadId, i < nNumThreadsBase ? &S.Pool[i]->ThreadName[0] : cLocal ); - uint32_t nThreadColor = -1; - if(nThreadId == nContextSwitchHoverThreadAfter || nThreadId == nContextSwitchHoverThreadBefore) - nThreadColor = UI.nHoverColorShared|0x906060; - MicroProfileDrawDetailedContextSwitchBars(nY+2, nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicksCpu, nBaseY); - MicroProfileDrawText(0, nY, nThreadColor, &ThreadName[0], nStrLen); - nY += MICROPROFILE_TEXT_HEIGHT+1; - } - } - } + ++nNumBoxes; + } + else + { + float fXAvg = 0.5f * (fXStart + fXEnd); + int nLineX = (int)floor(fXAvg+0.5f); + if(nLineX != (int)nLinesDrawn[nStackPos]) + { + if(bHover && UI.nActiveMenu == (uint32_t)-1) + { + nHoverToken = (uint32_t)MicroProfileLogTimerIndex(*pEntry); + nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd); + pMouseOverNext = pEntry; + } + nLinesDrawn[nStackPos] = nLineX; + MicroProfileDrawLineVertical(nLineX, (int)(fYStart + 0.5f), (int)(fYEnd + 0.5f), nColor|UI.nOpacityForeground); + ++nNumLines; + } + } + nStackPos--; - S.nContextSwitchHoverCpu = S.nContextSwitchHoverCpuNext; + if(0 == nStackPos && MicroProfileLogTickDifference(nTickEnd, nBaseTicksEnd) < 0) + { + break; + } + } + } + } + nY += nMaxStackDepth * nYDelta + MICROPROFILE_DETAILED_BAR_HEIGHT+1; + } + } + if(S.bContextSwitchRunning && (S.bContextSwitchAllThreads||S.bContextSwitchNoBars)) + { + uint32_t nContextSwitchSearchEnd = S.bContextSwitchAllThreads ? nContextSwitchEnd : nContextSwitchStart; + + MicroProfileThreadInfo Threads[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS]; + uint32_t nNumThreadsBase = 0; + uint32_t nNumThreads = MicroProfileContextSwitchGatherThreads(nContextSwitchStart, nContextSwitchSearchEnd, Threads, &nNumThreadsBase); + + std::sort(&Threads[nNumThreadsBase], &Threads[nNumThreads], + [](const MicroProfileThreadInfo& l, const MicroProfileThreadInfo& r) + { + return l.nProcessId == r.nProcessId ? l.nThreadId < r.nThreadId : l.nProcessId > r.nProcessId; + }); + + uint32_t nStart = nNumThreadsBase; + if(S.bContextSwitchNoBars) + nStart = 0; + MicroProfileProcessIdType nLastProcessId = MP_GETCURRENTPROCESSID(); + for(uint32_t i = nStart; i < nNumThreads; ++i) + { + MicroProfileThreadInfo tt = Threads[i]; + if(tt.nThreadId) + { + if (nLastProcessId != tt.nProcessId) + { + nY = MicroProfileWriteProcessHeader(nY, (uint32_t)tt.nProcessId); + nLastProcessId = tt.nProcessId; + } + + MicroProfileDrawDetailedContextSwitchBars(nY + 2, tt.nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicksCpu, nBaseY); + + MicroProfileWriteThreadHeader(nY, tt.nThreadId, i < nNumThreadsBase ? &S.Pool[i]->ThreadName[0] : nullptr, nullptr); + nY += MICROPROFILE_TEXT_HEIGHT + 1; + } + } + } + + S.nContextSwitchHoverCpu = S.nContextSwitchHoverCpuNext; - UI.pDisplayMouseOver = pMouseOverNext; + UI.pDisplayMouseOver = pMouseOverNext; - if(!S.nRunning) - { - if(nHoverToken != MICROPROFILE_INVALID_TOKEN && nHoverTime) - { - UI.nHoverToken = nHoverToken; - UI.nHoverTime = nHoverTime; - } + if(!S.nRunning) + { + if(nHoverToken != MICROPROFILE_INVALID_TOKEN && nHoverTime) + { + UI.nHoverToken = nHoverToken; + UI.nHoverTime = nHoverTime; + } - if(nSelectedFrame != -1) - { - UI.nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu; - UI.nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; - UI.nRangeBeginGpu = S.Frames[nSelectedFrame].nFrameStartGpu; - UI.nRangeEndGpu = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartGpu; - } - if(UI.nRangeBegin != UI.nRangeEnd) - { - float fMsStart = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeBegin); - float fMsEnd = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeEnd); - float fXStart = fMsStart * fMsToScreen; - float fXEnd = fMsEnd * fMsToScreen; - MicroProfileDrawBox(fXStart, nBaseY, fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat); - MicroProfileDrawLineVertical(fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000); - MicroProfileDrawLineVertical(fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000); + if(nSelectedFrame != -1) + { + UI.nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu; + UI.nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; + UI.nRangeBeginGpu = S.Frames[nSelectedFrame].nFrameStartGpu; + UI.nRangeEndGpu = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartGpu; + } + if(UI.nRangeBegin != UI.nRangeEnd) + { + float fMsStart = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeBegin); + float fMsEnd = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeEnd); + float fXStart = fMsStart * fMsToScreen; + float fXEnd = fMsEnd * fMsToScreen; + MicroProfileDrawBox((int)fXStart, nBaseY, (int)fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat); + MicroProfileDrawLineVertical((int)fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000); + MicroProfileDrawLineVertical((int)fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000); - fMsStart += fDetailedOffset; - fMsEnd += fDetailedOffset; - char sBuffer[32]; - uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart); - float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart); - float fStartTextX = fXStart - fStartTextWidth - 2; - MicroProfileDrawBox(fStartTextX, nBaseY, fStartTextX + fStartTextWidth + 2, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); - MicroProfileDrawText(fStartTextX+1, nBaseY, (uint32_t)-1, sBuffer, nLenStart); - uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd); - MicroProfileDrawBox(fXEnd+1, nBaseY, fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); - MicroProfileDrawText(fXEnd+2, nBaseY+1, (uint32_t)-1, sBuffer, nLenEnd); + fMsStart += fDetailedOffset; + fMsEnd += fDetailedOffset; + char sBuffer[32]; + uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart); + float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart); + float fStartTextX = fXStart - fStartTextWidth - 2; + MicroProfileDrawBox((int)fStartTextX, nBaseY, (int)(fStartTextX + fStartTextWidth + 2), MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); + MicroProfileDrawText((int)fStartTextX+1, nBaseY, (uint32_t)-1, sBuffer, nLenStart); + uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd); + MicroProfileDrawBox((int)(fXEnd+1), nBaseY, (int)(fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3), MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); + MicroProfileDrawText((int)(fXEnd+2), nBaseY+1, (uint32_t)-1, sBuffer, nLenEnd); - if(UI.nMouseRight) - { - MicroProfileZoomTo(UI.nRangeBegin, UI.nRangeEnd); - } - } + if(UI.nMouseRight) + { + MicroProfileZoomTo(UI.nRangeBegin, UI.nRangeEnd, MicroProfileTokenTypeCpu); + } + } - if(UI.nRangeBeginGpu != UI.nRangeEndGpu) - { - float fMsStart = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeBeginGpu); - float fMsEnd = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeEndGpu); - float fXStart = fMsStart * fMsToScreen; - float fXEnd = fMsEnd * fMsToScreen; - MicroProfileDrawBox(fXStart, nBaseY, fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU, MicroProfileBoxTypeFlat); - MicroProfileDrawLineVertical(fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000); - MicroProfileDrawLineVertical(fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000); + if(UI.nRangeBeginGpu != UI.nRangeEndGpu) + { + float fMsStart = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeBeginGpu); + float fMsEnd = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeEndGpu); + float fXStart = fMsStart * fMsToScreen; + float fXEnd = fMsEnd * fMsToScreen; + MicroProfileDrawBox((int)fXStart, nBaseY, (int)fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU, MicroProfileBoxTypeFlat); + MicroProfileDrawLineVertical((int)fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000); + MicroProfileDrawLineVertical((int)fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000); - nBaseY += MICROPROFILE_TEXT_HEIGHT+1; + nBaseY += MICROPROFILE_TEXT_HEIGHT+1; - fMsStart += fDetailedOffset; - fMsEnd += fDetailedOffset; - char sBuffer[32]; - uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart); - float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart); - float fStartTextX = fXStart - fStartTextWidth - 2; - MicroProfileDrawBox(fStartTextX, nBaseY, fStartTextX + fStartTextWidth + 2, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); - MicroProfileDrawText(fStartTextX+1, nBaseY, (uint32_t)-1, sBuffer, nLenStart); - uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd); - MicroProfileDrawBox(fXEnd+1, nBaseY, fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); - MicroProfileDrawText(fXEnd+2, nBaseY+1, (uint32_t)-1, sBuffer, nLenEnd); - } - } + fMsStart += fDetailedOffset; + fMsEnd += fDetailedOffset; + char sBuffer[32]; + uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart); + float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart); + float fStartTextX = fXStart - fStartTextWidth - 2; + MicroProfileDrawBox((int)fStartTextX, nBaseY, (int)(fStartTextX + fStartTextWidth + 2), MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); + MicroProfileDrawText((int)(fStartTextX+1), nBaseY, (uint32_t)-1, sBuffer, nLenStart); + uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd); + MicroProfileDrawBox((int)(fXEnd+1), nBaseY, (int)(fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3), MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat); + MicroProfileDrawText((int)(fXEnd+2), nBaseY+1, (uint32_t)-1, sBuffer, nLenEnd); + + if(UI.nMouseRight) + { + MicroProfileZoomTo(UI.nRangeBeginGpu, UI.nRangeEndGpu, MicroProfileTokenTypeGpu); + } + } + } } void MicroProfileDrawDetailedFrameHistory(uint32_t nWidth, uint32_t nHeight, uint32_t nBaseY, uint32_t nSelectedFrame) { - MicroProfile& S = *MicroProfileGet(); + (void)nHeight; - const uint32_t nBarHeight = MICROPROFILE_FRAME_HISTORY_HEIGHT; - float fBaseX = (float)nWidth; - float fDx = fBaseX / MICROPROFILE_NUM_FRAMES; + MicroProfile& S = *MicroProfileGet(); - uint32_t nLastIndex = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY; - MicroProfileDrawBox(0, nBaseY, nWidth, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, 0xff000000 | g_nMicroProfileBackColors[0], MicroProfileBoxTypeFlat); - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * S.fRcpReferenceTime; - float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()) * S.fRcpReferenceTime; + const uint32_t nBarHeight = MICROPROFILE_FRAME_HISTORY_HEIGHT; + float fBaseX = (float)nWidth; + float fDx = fBaseX / MICROPROFILE_NUM_FRAMES; + + uint32_t nLastIndex = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY; + MicroProfileDrawBox(0, nBaseY, nWidth, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, 0xff000000 | g_nMicroProfileBackColors[0], MicroProfileBoxTypeFlat); + float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * S.fRcpReferenceTime; + float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()) * S.fRcpReferenceTime; + + + MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; + uint64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu; + int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(UI.fDetailedOffset, MicroProfileTicksPerSecondCpu()); + int64_t nCpuStart = nDetailedOffsetTicksCpu + nFrameStartCpu; + int64_t nCpuEnd = nCpuStart + MicroProfileMsToTick(UI.fDetailedRange, MicroProfileTicksPerSecondCpu());; - MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; - uint64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu; - int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(UI.fDetailedOffset, MicroProfileTicksPerSecondCpu()); - int64_t nCpuStart = nDetailedOffsetTicksCpu + nFrameStartCpu; - int64_t nCpuEnd = nCpuStart + MicroProfileMsToTick(UI.fDetailedRange, MicroProfileTicksPerSecondCpu());; - - - float fSelectionStart = (float)nWidth; - float fSelectionEnd = 0.f; - for(uint32_t i = 0; i < MICROPROFILE_NUM_FRAMES; ++i) - { - uint32_t nIndex = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; - MicroProfileFrameState* pCurrent = &S.Frames[nIndex]; - MicroProfileFrameState* pNext = &S.Frames[nLastIndex]; - - int64_t nTicks = pNext->nFrameStartCpu - pCurrent->nFrameStartCpu; - int64_t nTicksGpu = pNext->nFrameStartGpu - pCurrent->nFrameStartGpu; - float fScale = fToMs * nTicks; - float fScaleGpu = fToMsGpu * nTicksGpu; - fScale = fScale > 1.f ? 0.f : 1.f - fScale; - fScaleGpu = fScaleGpu > 1.f ? 0.f : 1.f - fScaleGpu; - float fXEnd = fBaseX; - float fXStart = fBaseX - fDx; - fBaseX = fXStart; - uint32_t nColor = MICROPROFILE_FRAME_HISTORY_COLOR_CPU; - if(nIndex == nSelectedFrame) - nColor = (uint32_t)-1; - MicroProfileDrawBox(fXStart, nBaseY + fScale * nBarHeight, fXEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, nColor, MicroProfileBoxTypeBar); - if(pNext->nFrameStartCpu > nCpuStart) - { - fSelectionStart = fXStart; - } - if(pCurrent->nFrameStartCpu < nCpuEnd && fSelectionEnd == 0.f) - { - fSelectionEnd = fXEnd; - } - nLastIndex = nIndex; - } - MicroProfileDrawBox(fSelectionStart, nBaseY, fSelectionEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, MICROPROFILE_FRAME_HISTORY_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat); + float fSelectionStart = (float)nWidth; + float fSelectionEnd = 0.f; + for(uint32_t i = 0; i < MICROPROFILE_NUM_FRAMES; ++i) + { + uint32_t nIndex = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; + MicroProfileFrameState* pCurrent = &S.Frames[nIndex]; + MicroProfileFrameState* pNext = &S.Frames[nLastIndex]; + + int64_t nTicks = pNext->nFrameStartCpu - pCurrent->nFrameStartCpu; + int64_t nTicksGpu = pNext->nFrameStartGpu - pCurrent->nFrameStartGpu; + float fScale = fToMs * nTicks; + float fScaleGpu = fToMsGpu * nTicksGpu; + fScale = fScale > 1.f ? 0.f : 1.f - fScale; + fScaleGpu = fScaleGpu > 1.f ? 0.f : 1.f - fScaleGpu; + float fXEnd = fBaseX; + float fXStart = fBaseX - fDx; + fBaseX = fXStart; + uint32_t nColor = MICROPROFILE_FRAME_HISTORY_COLOR_CPU; + if(nIndex == nSelectedFrame) + nColor = (uint32_t)-1; + MicroProfileDrawBox((int)fXStart, (int)(nBaseY + fScale * nBarHeight), (int)fXEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, nColor, MicroProfileBoxTypeBar); + if(pNext->nFrameStartCpu > nCpuStart) + { + fSelectionStart = fXStart; + } + if(pCurrent->nFrameStartCpu < nCpuEnd && fSelectionEnd == 0.f) + { + fSelectionEnd = fXEnd; + } + nLastIndex = nIndex; + } + MicroProfileDrawBox((int)fSelectionStart, nBaseY, (int)fSelectionEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, MICROPROFILE_FRAME_HISTORY_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat); } -void MicroProfileDrawDetailedView(uint32_t nWidth, uint32_t nHeight) +void MicroProfileDrawDetailedView(uint32_t nWidth, uint32_t nHeight, bool bDrawBars) { - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); - MICROPROFILE_SCOPE(g_MicroProfileDetailed); - uint32_t nBaseY = MICROPROFILE_TEXT_HEIGHT + 1; + MICROPROFILE_SCOPE(g_MicroProfileDetailed); + uint32_t nBaseY = MICROPROFILE_TEXT_HEIGHT + 1; - int nSelectedFrame = -1; - if(UI.nMouseY > nBaseY && UI.nMouseY <= nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT && UI.nActiveMenu == -1) - { + int nSelectedFrame = -1; + if(UI.nMouseY > nBaseY && UI.nMouseY <= nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT && UI.nActiveMenu == (uint32_t)-1) + { - nSelectedFrame = ((MICROPROFILE_NUM_FRAMES) * (UI.nWidth-UI.nMouseX) / UI.nWidth); - nSelectedFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nSelectedFrame) % MICROPROFILE_MAX_FRAME_HISTORY; - UI.nHoverFrame = nSelectedFrame; - if(UI.nMouseRight) - { - int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu; - int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; - MicroProfileZoomTo(nRangeBegin, nRangeEnd); - } - if(UI.nMouseDownLeft) - { - uint64_t nFrac = (1024 * (MICROPROFILE_NUM_FRAMES) * (UI.nMouseX) / UI.nWidth) % 1024; - int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu; - int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; - MicroProfileCenter(nRangeBegin + (nRangeEnd-nRangeBegin) * nFrac / 1024); - } - } - else - { - UI.nHoverFrame = -1; - } + nSelectedFrame = ((MICROPROFILE_NUM_FRAMES) * (UI.nWidth-UI.nMouseX) / UI.nWidth); + nSelectedFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nSelectedFrame) % MICROPROFILE_MAX_FRAME_HISTORY; + UI.nHoverFrame = nSelectedFrame; + if(UI.nMouseRight) + { + int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu; + int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; + MicroProfileZoomTo(nRangeBegin, nRangeEnd, MicroProfileTokenTypeCpu); + } + if(UI.nMouseDownLeft) + { + uint64_t nFrac = (1024 * (MICROPROFILE_NUM_FRAMES) * (UI.nMouseX) / UI.nWidth) % 1024; + int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu; + int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; + MicroProfileCenter(nRangeBegin + (nRangeEnd-nRangeBegin) * nFrac / 1024); + } + } + else + { + UI.nHoverFrame = -1; + } - MicroProfileDrawDetailedBars(nWidth, nHeight, nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT, nSelectedFrame); - MicroProfileDrawDetailedFrameHistory(nWidth, nHeight, nBaseY, nSelectedFrame); + if (bDrawBars) + { + MicroProfileDrawDetailedBars(nWidth, nHeight, nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT, nSelectedFrame); + } + + MicroProfileDrawDetailedFrameHistory(nWidth, nHeight, nBaseY, nSelectedFrame); } - -template -void MicroProfileLoopActiveGroupsDraw(int32_t nX, int32_t nY, const char* pName, T CB) +void MicroProfileDrawTextRight(uint32_t nX, uint32_t nY, uint32_t nColor, const char* pStr, uint32_t nStrLen) { - MicroProfile& S = *MicroProfileGet(); - - if(pName) - MicroProfileDrawText(nX, nY, (uint32_t)-1, pName, (uint32_t)strlen(pName)); - - nY += MICROPROFILE_TEXT_HEIGHT + 2; - uint64_t nGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted; - uint32_t nCount = 0; - for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) - { - uint64_t nMask = 1ll << j; - if(nMask & nGroup) - { - nY += MICROPROFILE_TEXT_HEIGHT + 1; - for(uint32_t i = 0; i < S.nTotalTimers;++i) - { - uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken); - if(nTokenMask & nMask) - { - if(nY >= 0) - CB(i, nCount, nMask, nX, nY); - - nCount += 2; - nY += MICROPROFILE_TEXT_HEIGHT + 1; - - if(nY > (int)UI.nHeight) - return; - } - } - - } - } + MicroProfileDrawText(nX - nStrLen * (MICROPROFILE_TEXT_WIDTH+1), nY, nColor, pStr, nStrLen); +} +void MicroProfileDrawHeader(int32_t nX, uint32_t nWidth, const char* pName) +{ + if(pName) + { + MicroProfileDrawBox(nX-8, MICROPROFILE_TEXT_HEIGHT + 2, nX + nWidth+5, MICROPROFILE_TEXT_HEIGHT + 2 + (MICROPROFILE_TEXT_HEIGHT+1), 0xff000000|g_nMicroProfileBackColors[1]); + MicroProfileDrawText(nX, MICROPROFILE_TEXT_HEIGHT + 2, (uint32_t)-1, pName, (uint32_t)strlen(pName)); + } } -void MicroProfileCalcTimers(float* pTimers, float* pAverage, float* pMax, float* pCallAverage, float* pExclusive, float* pAverageExclusive, float* pMaxExclusive, uint64_t nGroup, uint32_t nSize) +typedef void (*MicroProfileLoopGroupCallback)(uint32_t nTimer, uint32_t nIdx, uint32_t nX, uint32_t nY, void* pData); + +void MicroProfileLoopActiveGroupsDraw(int32_t nX, int32_t nY, MicroProfileLoopGroupCallback CB, void* pData) { - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); + nY += MICROPROFILE_TEXT_HEIGHT + 2; + uint64_t nGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted; + uint32_t nCount = 0; + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + uint64_t nMask = 1ll << j; + if(nMask & nGroup) + { + nY += MICROPROFILE_TEXT_HEIGHT + 1; + for(uint32_t i = 0; i < S.nTotalTimers;++i) + { + uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken); + if(nTokenMask & nMask) + { + if(nY >= 0) + CB(i, nCount, nX, nY, pData); + + nCount += 2; + nY += MICROPROFILE_TEXT_HEIGHT + 1; - uint32_t nCount = 0; - uint64_t nMask = 1; + if(nY > (int)UI.nHeight) + return; + } + } + + } + } +} - for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) - { - if(nMask & nGroup) - { - const float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[j].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); - for(uint32_t i = 0; i < S.nTotalTimers;++i) - { - uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken); - if(nTokenMask & nMask) - { - { - uint32_t nTimer = i; - uint32_t nIdx = nCount; - uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; - uint32_t nAggregateCount = S.Aggregate[nTimer].nCount ? S.Aggregate[nTimer].nCount : 1; - float fToPrc = S.fRcpReferenceTime; - float fMs = fToMs * (S.Frame[nTimer].nTicks); - float fPrc = MicroProfileMin(fMs * fToPrc, 1.f); - float fAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateFrames); - float fAveragePrc = MicroProfileMin(fAverageMs * fToPrc, 1.f); - float fMaxMs = fToMs * (S.AggregateMax[nTimer]); - float fMaxPrc = MicroProfileMin(fMaxMs * fToPrc, 1.f); - float fCallAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateCount); - float fCallAveragePrc = MicroProfileMin(fCallAverageMs * fToPrc, 1.f); - float fMsExclusive = fToMs * (S.FrameExclusive[nTimer]); - float fPrcExclusive = MicroProfileMin(fMsExclusive * fToPrc, 1.f); - float fAverageMsExclusive = fToMs * (S.AggregateExclusive[nTimer] / nAggregateFrames); - float fAveragePrcExclusive = MicroProfileMin(fAverageMsExclusive * fToPrc, 1.f); - float fMaxMsExclusive = fToMs * (S.AggregateMaxExclusive[nTimer]); - float fMaxPrcExclusive = MicroProfileMin(fMaxMsExclusive * fToPrc, 1.f); - pTimers[nIdx] = fMs; - pTimers[nIdx+1] = fPrc; - pAverage[nIdx] = fAverageMs; - pAverage[nIdx+1] = fAveragePrc; - pMax[nIdx] = fMaxMs; - pMax[nIdx+1] = fMaxPrc; - pCallAverage[nIdx] = fCallAverageMs; - pCallAverage[nIdx+1] = fCallAveragePrc; - pExclusive[nIdx] = fMsExclusive; - pExclusive[nIdx+1] = fPrcExclusive; - pAverageExclusive[nIdx] = fAverageMsExclusive; - pAverageExclusive[nIdx+1] = fAveragePrcExclusive; - pMaxExclusive[nIdx] = fMaxMsExclusive; - pMaxExclusive[nIdx+1] = fMaxPrcExclusive; - } - nCount += 2; - } - } - } - nMask <<= 1ll; - } + +void MicroProfileCalcTimers(float* pTimers, float* pAverage, float* pMax, float* pMin, float* pCallAverage, float* pExclusive, float* pAverageExclusive, float* pMaxExclusive, uint64_t nGroup, uint32_t nSize) +{ + MicroProfile& S = *MicroProfileGet(); + + uint32_t nCount = 0; + uint64_t nMask = 1; + + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + if(nMask & nGroup) + { + const float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[j].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + for(uint32_t i = 0; i < S.nTotalTimers;++i) + { + uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken); + if(nTokenMask & nMask) + { + MP_ASSERT(nCount + 2 <= nSize); + { + uint32_t nTimer = i; + uint32_t nIdx = nCount; + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + uint32_t nAggregateCount = S.Aggregate[nTimer].nCount ? S.Aggregate[nTimer].nCount : 1; + float fToPrc = S.fRcpReferenceTime; + float fMs = fToMs * (S.Frame[nTimer].nTicks); + float fPrc = MicroProfileMin(fMs * fToPrc, 1.f); + float fAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateFrames); + float fAveragePrc = MicroProfileMin(fAverageMs * fToPrc, 1.f); + float fMaxMs = fToMs * (S.AggregateMax[nTimer]); + float fMaxPrc = MicroProfileMin(fMaxMs * fToPrc, 1.f); + float fMinMs = fToMs * (S.AggregateMin[nTimer] != uint64_t(-1) ? S.AggregateMin[nTimer] : 0); + float fMinPrc = MicroProfileMin(fMinMs * fToPrc, 1.f); + float fCallAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateCount); + float fCallAveragePrc = MicroProfileMin(fCallAverageMs * fToPrc, 1.f); + float fMsExclusive = fToMs * (S.FrameExclusive[nTimer]); + float fPrcExclusive = MicroProfileMin(fMsExclusive * fToPrc, 1.f); + float fAverageMsExclusive = fToMs * (S.AggregateExclusive[nTimer] / nAggregateFrames); + float fAveragePrcExclusive = MicroProfileMin(fAverageMsExclusive * fToPrc, 1.f); + float fMaxMsExclusive = fToMs * (S.AggregateMaxExclusive[nTimer]); + float fMaxPrcExclusive = MicroProfileMin(fMaxMsExclusive * fToPrc, 1.f); + pTimers[nIdx] = fMs; + pTimers[nIdx+1] = fPrc; + pAverage[nIdx] = fAverageMs; + pAverage[nIdx+1] = fAveragePrc; + pMax[nIdx] = fMaxMs; + pMax[nIdx+1] = fMaxPrc; + pMin[nIdx] = fMinMs; + pMin[nIdx + 1] = fMinPrc; + pCallAverage[nIdx] = fCallAverageMs; + pCallAverage[nIdx+1] = fCallAveragePrc; + pExclusive[nIdx] = fMsExclusive; + pExclusive[nIdx+1] = fPrcExclusive; + pAverageExclusive[nIdx] = fAverageMsExclusive; + pAverageExclusive[nIdx+1] = fAveragePrcExclusive; + pMaxExclusive[nIdx] = fMaxMsExclusive; + pMaxExclusive[nIdx+1] = fMaxPrcExclusive; + } + nCount += 2; + } + } + } + nMask <<= 1ll; + } } #define SBUF_MAX 32 +void MicroProfileDrawBarArrayCallback(uint32_t nTimer, uint32_t nIdx, uint32_t nX, uint32_t nY, void* pExtra) +{ + const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT; + const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH); + const float fWidth = (float)MICROPROFILE_BAR_WIDTH; + + float* pTimers = ((float**)pExtra)[0]; + float* pTimers2 = ((float**)pExtra)[1]; + MicroProfile& S = *MicroProfileGet(); + char sBuffer[SBUF_MAX]; + if (pTimers2 && pTimers2[nIdx] > 0.1f) + snprintf(sBuffer, SBUF_MAX-1, "%5.2f %3.1fx", pTimers[nIdx], pTimers[nIdx] / pTimers2[nIdx]); + else + snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pTimers[nIdx]); + if (!pTimers2) + MicroProfileDrawBox(nX + nTextWidth, nY, (int)(nX + nTextWidth + fWidth * pTimers[nIdx+1]), nY + nHeight, UI.nOpacityForeground|S.TimerInfo[nTimer].nColor, MicroProfileBoxTypeBar); + MicroProfileDrawText(nX, nY, (uint32_t)-1, sBuffer, (uint32_t)strlen(sBuffer)); +} + + uint32_t MicroProfileDrawBarArray(int32_t nX, int32_t nY, float* pTimers, const char* pName, uint32_t nTotalHeight, float* pTimers2 = NULL) { - MicroProfile* pState = MicroProfileGet(); + const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH); + const uint32_t nWidth = MICROPROFILE_BAR_WIDTH; - const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT; - const uint32_t nWidth = MICROPROFILE_BAR_WIDTH; - const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH); - const float fWidth = (float)MICROPROFILE_BAR_WIDTH; + MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + float* pTimersArray[2] = {pTimers, pTimers2}; + MicroProfileLoopActiveGroupsDraw(nX, nY, MicroProfileDrawBarArrayCallback, pTimersArray); + MicroProfileDrawHeader(nX, nTextWidth + nWidth, pName); + return nWidth + 5 + nTextWidth; - MicroProfileDrawLineVertical(nX-5, nY, nTotalHeight, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); - - MicroProfileLoopActiveGroupsDraw(nX, nY, pName, - [=](uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY){ - MicroProfile& S = *pState; - char sBuffer[SBUF_MAX]; - if (pTimers2 && pTimers2[nIdx] > 0.1f) - snprintf(sBuffer, SBUF_MAX-1, "%5.2f %3.1fx", pTimers[nIdx], pTimers[nIdx] / pTimers2[nIdx]); - else - snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pTimers[nIdx]); - if (!pTimers2) - MicroProfileDrawBox(nX + nTextWidth, nY, nX + nTextWidth + fWidth * pTimers[nIdx+1], nY + nHeight, UI.nOpacityForeground|S.TimerInfo[nTimer].nColor, MicroProfileBoxTypeBar); - MicroProfileDrawText(nX, nY, (uint32_t)-1, sBuffer, (uint32_t)strlen(sBuffer)); - }); - return nWidth + 5 + nTextWidth; +} +void MicroProfileDrawBarCallCountCallback(uint32_t nTimer, uint32_t nIdx, uint32_t nX, uint32_t nY, void* pExtra) +{ + (void)nIdx; + (void)pExtra; + MicroProfile& S = *MicroProfileGet(); + char sBuffer[SBUF_MAX]; + int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5d", S.Frame[nTimer].nCount);//fix + MicroProfileDrawText(nX, nY, (uint32_t)-1, sBuffer, nLen); } uint32_t MicroProfileDrawBarCallCount(int32_t nX, int32_t nY, const char* pName) { + MicroProfileLoopActiveGroupsDraw(nX, nY, MicroProfileDrawBarCallCountCallback, 0); + const uint32_t nTextWidth = 6 * MICROPROFILE_TEXT_WIDTH; + MicroProfileDrawHeader(nX, 5 + nTextWidth, pName); + return 5 + nTextWidth; +} - MicroProfileLoopActiveGroupsDraw(nX, nY, pName, - [](uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY){ - MicroProfile& S = *MicroProfileGet(); - char sBuffer[SBUF_MAX]; - int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5d", S.Frame[nTimer].nCount);//fix - MicroProfileDrawText(nX, nY, (uint32_t)-1, sBuffer, nLen); - }); - uint32_t nTextWidth = 6 * MICROPROFILE_TEXT_WIDTH; - return 5 + nTextWidth; +struct MicroProfileMetaAverageArgs +{ + uint64_t* pCounters; + float fRcpFrames; +}; + +void MicroProfileDrawBarMetaAverageCallback(uint32_t nTimer, uint32_t nIdx, uint32_t nX, uint32_t nY, void* pExtra) +{ + (void)nIdx; + + MicroProfileMetaAverageArgs* pArgs = (MicroProfileMetaAverageArgs*)pExtra; + uint64_t* pCounters = pArgs->pCounters; + float fRcpFrames = pArgs->fRcpFrames; + char sBuffer[SBUF_MAX]; + int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pCounters[nTimer] * fRcpFrames); + MicroProfileDrawText(nX - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, (uint32_t)-1, sBuffer, nLen); +} + +uint32_t MicroProfileDrawBarMetaAverage(int32_t nX, int32_t nY, uint64_t* pCounters, const char* pName, uint32_t nTotalHeight) +{ + if(!pName) + return 0; + MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax(6, (uint32_t)strlen(pName)); + float fRcpFrames = 1.f / (MicroProfileGet()->nAggregateFrames ? MicroProfileGet()->nAggregateFrames : 1); + MicroProfileMetaAverageArgs Args = {pCounters, fRcpFrames}; + MicroProfileLoopActiveGroupsDraw(nX + nTextWidth, nY, MicroProfileDrawBarMetaAverageCallback, &Args); + MicroProfileDrawHeader(nX, 5 + nTextWidth, pName); + return 5 + nTextWidth; +} + + +void MicroProfileDrawBarMetaCountCallback(uint32_t nTimer, uint32_t nIdx, uint32_t nX, uint32_t nY, void* pExtra) +{ + (void)nIdx; + + uint64_t* pCounters = (uint64_t*)pExtra; + char sBuffer[SBUF_MAX]; + int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5lld", (long long)pCounters[nTimer]); + MicroProfileDrawText(nX - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, (uint32_t)-1, sBuffer, nLen); } uint32_t MicroProfileDrawBarMetaCount(int32_t nX, int32_t nY, uint64_t* pCounters, const char* pName, uint32_t nTotalHeight) { - if(!pName) - return 0; + if(!pName) + return 0; - MicroProfileDrawLineVertical(nX-5, nY, nTotalHeight, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); - uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax(6, (uint32_t)strlen(pName)); - - - MicroProfileLoopActiveGroupsDraw(nX, nY, pName, - [=](uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY){ - char sBuffer[SBUF_MAX]; - int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5llu", pCounters[nTimer]); - MicroProfileDrawText(nX + nTextWidth - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, (uint32_t)-1, sBuffer, nLen); - }); - return 5 + nTextWidth; + MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax(6, (uint32_t)strlen(pName)); + MicroProfileLoopActiveGroupsDraw(nX + nTextWidth, nY, MicroProfileDrawBarMetaCountCallback, pCounters); + MicroProfileDrawHeader(nX, 5 + nTextWidth, pName); + return 5 + nTextWidth; } -uint32_t MicroProfileDrawBarLegend(int32_t nX, int32_t nY, uint32_t nTotalHeight) +void MicroProfileDrawBarLegendCallback(uint32_t nTimer, uint32_t nIdx, uint32_t nX, uint32_t nY, void* pExtra) { - MicroProfileDrawLineVertical(nX-5, nY, nTotalHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); - MicroProfileLoopActiveGroupsDraw(nX, nY, 0, - [](uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY){ - MicroProfile& S = *MicroProfileGet(); - if (S.TimerInfo[nTimer].bGraph) - { - MicroProfileDrawText(nX, nY, S.TimerInfo[nTimer].nColor, ">", 1); - } - MicroProfileDrawText(nX + (MICROPROFILE_TEXT_WIDTH+1), nY, S.TimerInfo[nTimer].nColor, S.TimerInfo[nTimer].pName, (uint32_t)strlen(S.TimerInfo[nTimer].pName)); - if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT+1 && UI.nMouseX < nX + 20 * (MICROPROFILE_TEXT_WIDTH+1)) - { - UI.nHoverToken = nTimer; - UI.nHoverTime = 0; - } - }); - return nX; + (void)nIdx; + (void)pExtra; + + MicroProfile& S = *MicroProfileGet(); + if (S.TimerInfo[nTimer].bGraph) + { + MicroProfileDrawText(nX, nY, S.TimerInfo[nTimer].nColor, ">", 1); + } + MicroProfileDrawTextRight(nX, nY, S.TimerInfo[nTimer].nColor, S.TimerInfo[nTimer].pName, (uint32_t)strlen(S.TimerInfo[nTimer].pName)); + if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT+1) + { + UI.nHoverToken = nTimer; + UI.nHoverTime = 0; + } +} + +uint32_t MicroProfileDrawBarLegend(int32_t nX, int32_t nY, uint32_t nTotalHeight, uint32_t nMaxWidth) +{ + MicroProfileDrawLineVertical(nX-5, nY, nTotalHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + MicroProfileLoopActiveGroupsDraw(nMaxWidth, nY, MicroProfileDrawBarLegendCallback, 0); + return nX; } bool MicroProfileDrawGraph(uint32_t nScreenWidth, uint32_t nScreenHeight) { - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); - MICROPROFILE_SCOPE(g_MicroProfileDrawGraph); - bool bEnabled = false; - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) - bEnabled = true; - if(!bEnabled) - return false; + MICROPROFILE_SCOPE(g_MicroProfileDrawGraph); + bool bEnabled = false; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) + bEnabled = true; + if(!bEnabled) + return false; + + uint32_t nX = nScreenWidth - MICROPROFILE_GRAPH_WIDTH; + uint32_t nY = nScreenHeight - MICROPROFILE_GRAPH_HEIGHT; + MicroProfileDrawBox(nX, nY, nX + MICROPROFILE_GRAPH_WIDTH, nY + MICROPROFILE_GRAPH_HEIGHT, 0x88000000 | g_nMicroProfileBackColors[0]); + bool bMouseOver = UI.nMouseX >= nX && UI.nMouseY >= nY; + float fMouseXPrc =(float(UI.nMouseX - nX)) / MICROPROFILE_GRAPH_WIDTH; + if(bMouseOver) + { + float fXAvg = fMouseXPrc * MICROPROFILE_GRAPH_WIDTH + nX; + MicroProfileDrawLineVertical((int)fXAvg, nY, nY + MICROPROFILE_GRAPH_HEIGHT, (uint32_t)-1); + } - uint32_t nX = nScreenWidth - MICROPROFILE_GRAPH_WIDTH; - uint32_t nY = nScreenHeight - MICROPROFILE_GRAPH_HEIGHT; - MicroProfileDrawBox(nX, nY, nX + MICROPROFILE_GRAPH_WIDTH, nY + MICROPROFILE_GRAPH_HEIGHT, UI.nOpacityBackground | g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); - bool bMouseOver = UI.nMouseX >= nX && UI.nMouseY >= nY; - float fMouseXPrc =(float(UI.nMouseX - nX)) / MICROPROFILE_GRAPH_WIDTH; - if(bMouseOver) - { - float fXAvg = fMouseXPrc * MICROPROFILE_GRAPH_WIDTH + nX; - MicroProfileDrawLineVertical(fXAvg, nY, nY + MICROPROFILE_GRAPH_HEIGHT, (uint32_t)-1); - } + + float fY = (float)nScreenHeight; + float fDX = MICROPROFILE_GRAPH_WIDTH * 1.f / MICROPROFILE_GRAPH_HISTORY; + float fDY = MICROPROFILE_GRAPH_HEIGHT; + uint32_t nPut = S.nGraphPut; + float* pGraphData = (float*)alloca(sizeof(float)* MICROPROFILE_GRAPH_HISTORY*2); + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) + { + uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken); + bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu; + float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + float fToPrc = fToMs * S.fRcpReferenceTime * 3 / 4; + float fX = (float)nX; + for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j) + { + float fWeigth = MicroProfileMin(fToPrc * (S.Graph[i].nHistory[(j+nPut)%MICROPROFILE_GRAPH_HISTORY]), 1.f); + pGraphData[(j*2)] = fX; + pGraphData[(j*2)+1] = fY - fDY * fWeigth; + fX += fDX; + } + MicroProfileDrawLine2D(MICROPROFILE_GRAPH_HISTORY, pGraphData, UI.nOpacityForeground|S.TimerInfo[MicroProfileGetTimerIndex(S.Graph[i].nToken)].nColor); + } + } + { + float fY1 = 0.25f * MICROPROFILE_GRAPH_HEIGHT + nY; + float fY2 = 0.50f * MICROPROFILE_GRAPH_HEIGHT + nY; + float fY3 = 0.75f * MICROPROFILE_GRAPH_HEIGHT + nY; + MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, (int)fY1, 0xffdd4444); + MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, (int)fY2, 0xff000000 | g_nMicroProfileBackColors[0]); + MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, (int)fY3, 0xff000000 | g_nMicroProfileBackColors[0]); - float fY = (float)nScreenHeight; - float fDX = MICROPROFILE_GRAPH_WIDTH * 1.f / MICROPROFILE_GRAPH_HISTORY; - float fDY = MICROPROFILE_GRAPH_HEIGHT; - uint32_t nPut = S.nGraphPut; - float* pGraphData = (float*)alloca(sizeof(float)* MICROPROFILE_GRAPH_HISTORY*2); - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) - { - uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken); - bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu; - float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); - float fToPrc = fToMs * S.fRcpReferenceTime * 3 / 4; - - float fX = (float)nX; - for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j) - { - float fWeigth = MicroProfileMin(fToPrc * (S.Graph[i].nHistory[(j+nPut)%MICROPROFILE_GRAPH_HISTORY]), 1.f); - pGraphData[(j*2)] = fX; - pGraphData[(j*2)+1] = fY - fDY * fWeigth; - fX += fDX; - } - MicroProfileDrawLine2D(MICROPROFILE_GRAPH_HISTORY, pGraphData, S.TimerInfo[MicroProfileGetTimerIndex(S.Graph[i].nToken)].nColor); - } - } - { - float fY1 = 0.25f * MICROPROFILE_GRAPH_HEIGHT + nY; - float fY2 = 0.50f * MICROPROFILE_GRAPH_HEIGHT + nY; - float fY3 = 0.75f * MICROPROFILE_GRAPH_HEIGHT + nY; - MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY1, 0xffdd4444); - MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY2, 0xff000000| g_nMicroProfileBackColors[0]); - MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY3, 0xff000000|g_nMicroProfileBackColors[0]); - - char buf[32]; - int nLen = snprintf(buf, sizeof(buf)-1, "%5.2fms", S.fReferenceTime); - MicroProfileDrawText(nX+1, fY1 - (2+MICROPROFILE_TEXT_HEIGHT), (uint32_t)-1, buf, nLen); - } + char buf[32]; + int nLen = snprintf(buf, sizeof(buf)-1, "%5.2fms", S.fReferenceTime); + MicroProfileDrawText(nX+1, (int)(fY1 - (2+MICROPROFILE_TEXT_HEIGHT)), (uint32_t)-1, buf, nLen); + } - if(bMouseOver) - { - uint32_t pColors[MICROPROFILE_MAX_GRAPHS]; - MicroProfileStringArray Strings; - MicroProfileStringArrayClear(&Strings); - uint32_t nTextCount = 0; - uint32_t nGraphIndex = (S.nGraphPut + MICROPROFILE_GRAPH_HISTORY - int(MICROPROFILE_GRAPH_HISTORY*(1.f - fMouseXPrc))) % MICROPROFILE_GRAPH_HISTORY; + if(bMouseOver) + { + uint32_t pColors[MICROPROFILE_MAX_GRAPHS]; + MicroProfileStringArray Strings; + MicroProfileStringArrayClear(&Strings); + uint32_t nTextCount = 0; + uint32_t nGraphIndex = (S.nGraphPut + MICROPROFILE_GRAPH_HISTORY - int(MICROPROFILE_GRAPH_HISTORY*(1.f - fMouseXPrc))) % MICROPROFILE_GRAPH_HISTORY; - uint32_t nX = UI.nMouseX; - uint32_t nY = UI.nMouseY + 20; + uint32_t nX = UI.nMouseX; + uint32_t nY = UI.nMouseY + 20; - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) - { - uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken); - bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu; - float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); - uint32_t nIndex = MicroProfileGetTimerIndex(S.Graph[i].nToken); - uint32_t nColor = S.TimerInfo[nIndex].nColor; - const char* pName = S.TimerInfo[nIndex].pName; - pColors[nTextCount++] = nColor; - MicroProfileStringArrayAddLiteral(&Strings, pName); - MicroProfileStringArrayFormat(&Strings, "%5.2fms", fToMs * (S.Graph[i].nHistory[nGraphIndex])); - } - } - if(nTextCount) - { - MicroProfileDrawFloatWindow(nX, nY, Strings.ppStrings, Strings.nNumStrings, 0, pColors); - } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) + { + uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken); + bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu; + float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + uint32_t nIndex = MicroProfileGetTimerIndex(S.Graph[i].nToken); + uint32_t nColor = S.TimerInfo[nIndex].nColor; + const char* pName = S.TimerInfo[nIndex].pName; + pColors[nTextCount++] = nColor; + MicroProfileStringArrayAddLiteral(&Strings, pName); + MicroProfileStringArrayFormat(&Strings, "%5.2fms", fToMs * (S.Graph[i].nHistory[nGraphIndex])); + } + } + if(nTextCount) + { + MicroProfileDrawFloatWindow(nX, nY, Strings.ppStrings, Strings.nNumStrings, 0, pColors); + } - if(UI.nMouseRight) - { - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; - } - } - } + if(UI.nMouseRight) + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; + } + } + } - return bMouseOver; + return bMouseOver; } void MicroProfileDumpTimers() { - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); - uint64_t nActiveGroup = S.nGroupMask; + uint64_t nActiveGroup = S.nGroupMask; - uint32_t nNumTimers = S.nTotalTimers; - uint32_t nBlockSize = 2 * nNumTimers; - float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float)); - float* pAverage = pTimers + nBlockSize; - float* pMax = pTimers + 2 * nBlockSize; - float* pCallAverage = pTimers + 3 * nBlockSize; - float* pTimersExclusive = pTimers + 4 * nBlockSize; - float* pAverageExclusive = pTimers + 5 * nBlockSize; - float* pMaxExclusive = pTimers + 6 * nBlockSize; - MicroProfileCalcTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nNumTimers); + uint32_t nNumTimers = S.nTotalTimers; + uint32_t nBlockSize = 2 * nNumTimers; + float* pTimers = (float*)alloca(nBlockSize * 8 * sizeof(float)); + float* pAverage = pTimers + nBlockSize; + float* pMax = pTimers + 2 * nBlockSize; + float* pMin = pTimers + 3 * nBlockSize; + float* pCallAverage = pTimers + 4 * nBlockSize; + float* pTimersExclusive = pTimers + 5 * nBlockSize; + float* pAverageExclusive = pTimers + 6 * nBlockSize; + float* pMaxExclusive = pTimers + 7 * nBlockSize; + MicroProfileCalcTimers(pTimers, pAverage, pMax, pMin, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nBlockSize); - MICROPROFILE_PRINTF("%11s, ", "Time"); - MICROPROFILE_PRINTF("%11s, ", "Average"); - MICROPROFILE_PRINTF("%11s, ", "Max"); - MICROPROFILE_PRINTF("%11s, ", "Call Avg"); - MICROPROFILE_PRINTF("%9s, ", "Count"); - MICROPROFILE_PRINTF("%11s, ", "Excl"); - MICROPROFILE_PRINTF("%11s, ", "Avg Excl"); - MICROPROFILE_PRINTF("%11s, \n", "Max Excl"); + MICROPROFILE_PRINTF("%11s, ", "Time"); + MICROPROFILE_PRINTF("%11s, ", "Average"); + MICROPROFILE_PRINTF("%11s, ", "Max"); + MICROPROFILE_PRINTF("%11s, ", "Min"); + MICROPROFILE_PRINTF("%11s, ", "Call Avg"); + MICROPROFILE_PRINTF("%9s, ", "Count"); + MICROPROFILE_PRINTF("%11s, ", "Excl"); + MICROPROFILE_PRINTF("%11s, ", "Avg Excl"); + MICROPROFILE_PRINTF("%11s, \n", "Max Excl"); - for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) - { - uint64_t nMask = 1ll << j; - if(nMask & nActiveGroup) - { - MICROPROFILE_PRINTF("%s\n", S.GroupInfo[j].pName); - for(uint32_t i = 0; i < S.nTotalTimers;++i) - { - uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken); - if(nTokenMask & nMask) - { - uint32_t nIdx = i * 2; - MICROPROFILE_PRINTF("%9.2fms, ", pTimers[nIdx]); - MICROPROFILE_PRINTF("%9.2fms, ", pAverage[nIdx]); - MICROPROFILE_PRINTF("%9.2fms, ", pMax[nIdx]); - MICROPROFILE_PRINTF("%9.2fms, ", pCallAverage[nIdx]); - MICROPROFILE_PRINTF("%9d, ", S.Frame[i].nCount); - MICROPROFILE_PRINTF("%9.2fms, ", pTimersExclusive[nIdx]); - MICROPROFILE_PRINTF("%9.2fms, ", pAverageExclusive[nIdx]); - MICROPROFILE_PRINTF("%9.2fms, ", pMaxExclusive[nIdx]); - MICROPROFILE_PRINTF("%s\n", S.TimerInfo[i].pName); - } - } - } - } + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + uint64_t nMask = 1ll << j; + if(nMask & nActiveGroup) + { + MICROPROFILE_PRINTF("%s\n", S.GroupInfo[j].pName); + for(uint32_t i = 0; i < S.nTotalTimers;++i) + { + uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken); + if(nTokenMask & nMask) + { + uint32_t nIdx = i * 2; + MICROPROFILE_PRINTF("%9.2fms, ", pTimers[nIdx]); + MICROPROFILE_PRINTF("%9.2fms, ", pAverage[nIdx]); + MICROPROFILE_PRINTF("%9.2fms, ", pMax[nIdx]); + MICROPROFILE_PRINTF("%9.2fms, ", pMin[nIdx]); + MICROPROFILE_PRINTF("%9.2fms, ", pCallAverage[nIdx]); + MICROPROFILE_PRINTF("%9d, ", S.Frame[i].nCount); + MICROPROFILE_PRINTF("%9.2fms, ", pTimersExclusive[nIdx]); + MICROPROFILE_PRINTF("%9.2fms, ", pAverageExclusive[nIdx]); + MICROPROFILE_PRINTF("%9.2fms, ", pMaxExclusive[nIdx]); + MICROPROFILE_PRINTF("%s\n", S.TimerInfo[i].pName); + } + } + } + } } + + +uint32_t MicroProfileDrawCounterRecursive(uint32_t nIndex, uint32_t nY, uint32_t nOffset, uint32_t nTimerWidth) +{ + MicroProfile& S = *MicroProfileGet(); + uint8_t bGraphDetailed = 0 != (S.CounterInfo[nIndex].nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED_GRAPH); + uint32_t nRows = bGraphDetailed ? 5 : 1; + const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT; + const uint32_t nCounterWidth = UI.nCounterWidth; + const uint32_t nLimitWidth = UI.nLimitWidth; + + uint32_t nY0 = nY + nOffset * (nHeight+1); + uint32_t nBackHeight = (nHeight+1) * nRows; + + MicroProfileCounterInfo& CI = S.CounterInfo[nIndex]; + bool bInside = (UI.nActiveMenu == (uint32_t)-1) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nBackHeight))); + uint32_t nTotalWidth = nTimerWidth + nCounterWidth * 3 + MICROPROFILE_COUNTER_WIDTH + nLimitWidth + 4 * (MICROPROFILE_TEXT_WIDTH+1) + + 4 + MICROPROFILE_GRAPH_HISTORY; + uint32_t nBackColor = 0xff000000 | (g_nMicroProfileBackColors[nOffset & 1] + ((bInside) ? 0x002c2c2c : 0)); + MicroProfileDrawBox(0, nY0, nTotalWidth, nY0 + nBackHeight +1, nBackColor); + uint32_t nIndent = MICROPROFILE_COUNTER_INDENT*CI.nLevel * (MICROPROFILE_TEXT_WIDTH+1); + if(CI.nFirstChild != -1 && 0 != (CI.nFlags & MICROPROFILE_COUNTER_FLAG_CLOSED)) + { + MicroProfileDrawText(nIndent, nY0, 0xffffffff, "*", 1); + } + + MicroProfileDrawText(nIndent + MICROPROFILE_TEXT_WIDTH+1, nY0, 0xffffffff, CI.pName, CI.nNameLen); + char buffer[64]; + int64_t nCounterValue = S.Counters[nIndex].load(); + uint32_t nX = nTimerWidth + nCounterWidth; + int nLen = MicroProfileFormatCounter(S.CounterInfo[nIndex].eFormat, nCounterValue, buffer, sizeof(buffer)); + UI.nCounterWidthTemp = MicroProfileMax((uint32_t)nLen, UI.nCounterWidthTemp); + if(0 != nCounterValue || 0 != (CI.nFlags & MICROPROFILE_COUNTER_FLAG_LEAF)) + { + MicroProfileDrawTextRight(nX, nY0, 0xffffffff, buffer, nLen); + } + int64_t nLimit = S.CounterInfo[nIndex].nLimit; + if(nLimit) + { + nX += MICROPROFILE_TEXT_WIDTH+1; + MicroProfileDrawText(nX, nY0, 0xffffffff, "/", 1); + nX += 2 * (MICROPROFILE_TEXT_WIDTH+1); + int nLen = MicroProfileFormatCounter(S.CounterInfo[nIndex].eFormat, nLimit, buffer, sizeof(buffer)); + UI.nLimitWidthTemp = MicroProfileMax(UI.nLimitWidthTemp, (uint32_t)nLen); + MicroProfileDrawText(nX, nY0, 0xffffffff, buffer, nLen); + nX += nLimitWidth; + nY0 += 1; + + float fCounterPrc = (float)nCounterValue / nLimit; + fCounterPrc = MicroProfileMax(fCounterPrc, 0.f); + float fBoxPrc = 1.f; + if(fCounterPrc>1.f) + { + fBoxPrc = 1.f / fCounterPrc; + fCounterPrc = 1.f; + } + + MicroProfileDrawBox(nX, nY0, nX + (int)(fBoxPrc * MICROPROFILE_COUNTER_WIDTH), nY0 + nHeight, 0xffffffff, MicroProfileBoxTypeFlat); + MicroProfileDrawBox(nX+1, nY0+1, nX + MICROPROFILE_COUNTER_WIDTH - 1, nY0 + nHeight - 1, nBackColor, MicroProfileBoxTypeFlat); + MicroProfileDrawBox(nX+1, nY0+1, nX + (int)(fCounterPrc * (MICROPROFILE_COUNTER_WIDTH - 1)), nY0 + nHeight - 1, 0xff0088ff, MicroProfileBoxTypeFlat); + nX += MICROPROFILE_COUNTER_WIDTH + 5; + } + else + { + nX += MICROPROFILE_TEXT_WIDTH+1; + nX += 2 * (MICROPROFILE_TEXT_WIDTH+1); + nX += nLimitWidth; + nX += MICROPROFILE_COUNTER_WIDTH + 5; + } + + if(bInside && (UI.nMouseLeft || UI.nMouseRight)) + { + if(UI.nMouseX > nX) + { + if(UI.nMouseRight) + { + CI.nFlags &= ~MICROPROFILE_COUNTER_FLAG_DETAILED; + } + else + { + // toggle through detailed & detailed graph + if(CI.nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED) + { + CI.nFlags ^= MICROPROFILE_COUNTER_FLAG_DETAILED_GRAPH; + } + else + { + CI.nFlags |= MICROPROFILE_COUNTER_FLAG_DETAILED; + } + } + if(0 == (CI.nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED)) + { + CI.nFlags &= ~MICROPROFILE_COUNTER_FLAG_DETAILED_GRAPH; + } + } + else if(UI.nMouseLeft) + { + CI.nFlags ^= MICROPROFILE_COUNTER_FLAG_CLOSED; + } + } + +#if MICROPROFILE_COUNTER_HISTORY + if(0 != (CI.nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED)) + { + static float pGraphData[MICROPROFILE_GRAPH_HISTORY*2]; + static float pGraphFillData[MICROPROFILE_GRAPH_HISTORY*4]; + + int32_t nMouseGraph = UI.nMouseX - nX; + + + int64_t nCounterMax = S.nCounterMax[nIndex]; + int64_t nCounterMin = S.nCounterMin[nIndex]; + uint32_t nBaseIndex = S.nCounterHistoryPut; + float fX = (float)nX; + + int64_t nCounterHeightBase = nCounterMax; + int64_t nCounterOffset = 0; + if(nCounterMin < 0) + { + nCounterHeightBase = nCounterMax - nCounterMin; + nCounterOffset = -nCounterMin; + } + const int32_t nGraphHeight = nRows * nHeight; + double fRcpMax = nGraphHeight * 1.0 / nCounterHeightBase; + const int32_t nYOffset = nY0 + (bGraphDetailed ? 3 : 1); + const int32_t nYBottom = nGraphHeight + nYOffset; + for(uint32_t i = 0; i < MICROPROFILE_GRAPH_HISTORY; ++i) + { + uint32_t nHistoryIndex = (nBaseIndex + i) % MICROPROFILE_GRAPH_HISTORY; + int64_t nValue = MicroProfileClamp(S.nCounterHistory[nHistoryIndex][nIndex], nCounterMin, nCounterMax); + float fPrc = nGraphHeight - (float)((double)(nValue+nCounterOffset) * fRcpMax); + pGraphData[(i*2)] = fX; + pGraphData[(i*2)+1] = nYOffset + fPrc; + + pGraphFillData[(i*4) + 0] = fX; + pGraphFillData[(i*4) + 1] = nYOffset + fPrc; + pGraphFillData[(i*4) + 2] = fX; + pGraphFillData[(i*4) + 3] = (float)nYBottom; + + fX += 1; + } + MicroProfileDrawLine2D(MICROPROFILE_GRAPH_HISTORY*2, pGraphFillData, 0x330088ff); + MicroProfileDrawLine2D(MICROPROFILE_GRAPH_HISTORY, pGraphData, 0xff0088ff); + + if(nMouseGraph < MICROPROFILE_GRAPH_HISTORY && bInside && nCounterMin <= nCounterMax) + { + uint32_t nMouseX = nX + nMouseGraph; + float fMouseX = (float)nMouseX; + uint32_t nHistoryIndex = (nBaseIndex + nMouseGraph) % MICROPROFILE_GRAPH_HISTORY; + int64_t nValue = MicroProfileClamp(S.nCounterHistory[nHistoryIndex][nIndex], nCounterMin, nCounterMax); + float fPrc = nGraphHeight - (float)((double)(nValue+nCounterOffset) * fRcpMax); + float fCursor[4]; + fCursor[0] = fMouseX-2.f; + fCursor[1] = nYOffset + fPrc + 2.f; + fCursor[2] = fMouseX+2.f; + fCursor[3] = nYOffset + fPrc - 2.f; + MicroProfileDrawLine2D(2, fCursor, 0xddff8800); + fCursor[0] = fMouseX+2.f; + fCursor[1] = nYOffset + fPrc + 2.f; + fCursor[2] = fMouseX-2.f; + fCursor[3] = nYOffset + fPrc - 2.f; + MicroProfileDrawLine2D(2, fCursor, 0xddff8800); + int nLen = MicroProfileFormatCounter(S.CounterInfo[nIndex].eFormat, nValue, buffer, sizeof(buffer)); + MicroProfileDrawText(nX, nY0, 0xffffffff, buffer, nLen); + } + + + nX += MICROPROFILE_GRAPH_HISTORY + 5; + if(nCounterMin <= nCounterMax) + { + int nLen = MicroProfileFormatCounter(S.CounterInfo[nIndex].eFormat, nCounterMin, buffer, sizeof(buffer)); + MicroProfileDrawText(nX, nY0, 0xffffffff, buffer, nLen); + nX += nCounterWidth; + nLen = MicroProfileFormatCounter(S.CounterInfo[nIndex].eFormat, nCounterMax, buffer, sizeof(buffer)); + MicroProfileDrawText(nX, nY0, 0xffffffff, buffer, nLen); + } + + } +#endif + + nOffset += nRows; + if(0 == (CI.nFlags & MICROPROFILE_COUNTER_FLAG_CLOSED)) + { + int nChild = CI.nFirstChild; + while(nChild != -1) + { + nOffset = MicroProfileDrawCounterRecursive(nChild, nY, nOffset, nTimerWidth); + nChild = S.CounterInfo[nChild].nSibling; + } + } + + + return nOffset; +} + +void MicroProfileDrawCounterView(uint32_t nScreenWidth, uint32_t nScreenHeight) +{ + (void)nScreenWidth; + (void)nScreenHeight; + + MicroProfile& S = *MicroProfileGet(); + MICROPROFILE_SCOPE(g_MicroProfileDrawBarView); + + UI.nCounterWidthTemp = 7; + UI.nLimitWidthTemp = 7; + const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT; + uint32_t nTimerWidth = 7 * (MICROPROFILE_TEXT_WIDTH+1); + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + uint32_t nWidth = (2+S.CounterInfo[i].nNameLen + MICROPROFILE_COUNTER_INDENT*S.CounterInfo[i].nLevel) * (MICROPROFILE_TEXT_WIDTH+1); + nTimerWidth = MicroProfileMax(nTimerWidth, nWidth); + } + uint32_t nX = nTimerWidth + UI.nOffsetX[MP_DRAW_COUNTERS]; + uint32_t nY = nHeight + 3 - UI.nOffsetY[MP_DRAW_COUNTERS]; + uint32_t nNumCounters = S.nNumCounters; + nX = 0; + nY = (2*nHeight) + 3 - UI.nOffsetY[MP_DRAW_COUNTERS]; + uint32_t nOffset = 0; + for(uint32_t i = 0; i < nNumCounters; ++i) + { + if(S.CounterInfo[i].nParent == -1) + { + nOffset = MicroProfileDrawCounterRecursive(i, nY, nOffset, nTimerWidth); + } + } + nX = 0; + MicroProfileDrawHeader(nX, nTimerWidth, "Name"); + nX += nTimerWidth; + MicroProfileDrawHeader(nX, UI.nCounterWidth + 1 * (MICROPROFILE_TEXT_WIDTH*3), "Value"); + nX += UI.nCounterWidth; + nX += 1 * (MICROPROFILE_TEXT_WIDTH*3); + MicroProfileDrawHeader(nX, UI.nLimitWidth + MICROPROFILE_COUNTER_WIDTH, "Limit"); + nX += UI.nLimitWidth + MICROPROFILE_COUNTER_WIDTH + 4; + MicroProfileDrawHeader(nX, MICROPROFILE_GRAPH_HISTORY, "Graph"); + nX += MICROPROFILE_GRAPH_HISTORY + 4; + MicroProfileDrawHeader(nX, UI.nCounterWidth , "Min"); + nX += UI.nCounterWidth; + MicroProfileDrawHeader(nX, UI.nCounterWidth , "Max"); + nX += UI.nCounterWidth; + uint32_t nTotalWidth = nX;//nTimerWidth + UI.nCounterWidth + MICROPROFILE_COUNTER_WIDTH + UI.nLimitWidth + 3 * (MICROPROFILE_TEXT_WIDTH+1); + + + + MicroProfileDrawLineVertical(nTimerWidth-2, 0, nOffset*(nHeight+1)+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + MicroProfileDrawLineHorizontal(0, nTotalWidth, 2*MICROPROFILE_TEXT_HEIGHT + 3, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + + UI.nCounterWidth = (1+UI.nCounterWidthTemp) * (MICROPROFILE_TEXT_WIDTH+1); + UI.nLimitWidth = (1+UI.nLimitWidthTemp) * (MICROPROFILE_TEXT_WIDTH+1); + +} + + + void MicroProfileDrawBarView(uint32_t nScreenWidth, uint32_t nScreenHeight) { - MicroProfile& S = *MicroProfileGet(); + (void)nScreenWidth; + (void)nScreenHeight; - uint64_t nActiveGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted; - if(!nActiveGroup) - return; - MICROPROFILE_SCOPE(g_MicroProfileDrawBarView); + MicroProfile& S = *MicroProfileGet(); - const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT; - int nColorIndex = 0; - uint32_t nX = 0; - uint32_t nY = nHeight + 1 - UI.nOffsetY; - uint32_t nNumTimers = 0; - uint32_t nNumGroups = 0; - uint32_t nMaxTimerNameLen = 1; - for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) - { - if(nActiveGroup & (1ll << j)) - { - nNumTimers += S.GroupInfo[j].nNumTimers; - nNumGroups += 1; - nMaxTimerNameLen = MicroProfileMax(nMaxTimerNameLen, S.GroupInfo[j].nMaxTimerNameLen); - } - } - uint32_t nBlockSize = 2 * nNumTimers; - float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float)); - float* pAverage = pTimers + nBlockSize; - float* pMax = pTimers + 2 * nBlockSize; - float* pCallAverage = pTimers + 3 * nBlockSize; - float* pTimersExclusive = pTimers + 4 * nBlockSize; - float* pAverageExclusive = pTimers + 5 * nBlockSize; - float* pMaxExclusive = pTimers + 6 * nBlockSize; - MicroProfileCalcTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nNumTimers); - { - uint32_t nWidth = 0; - for(uint32_t i = 1; i ; i <<= 1) - { - if(S.nBars & i) - { - nWidth += MICROPROFILE_BAR_WIDTH + 5 + 6 * (1+MICROPROFILE_TEXT_WIDTH); - if(i & MP_DRAW_CALL_COUNT) - nWidth += 5 + 6 * MICROPROFILE_TEXT_WIDTH; - } - } - nWidth += (1+nMaxTimerNameLen) * (MICROPROFILE_TEXT_WIDTH+1); - for(uint32_t i = 0; i < nNumTimers+nNumGroups+1; ++i) - { - uint32_t nY0 = nY + i * (nHeight + 1); - bool bInside = (UI.nActiveMenu == -1) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nHeight + 1))); - MicroProfileDrawBox(nX, nY0, nWidth, nY0 + (nHeight+1)+1, UI.nOpacityBackground | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0))); - } - } - int nTotalHeight = (nNumTimers+nNumGroups+2) * (nHeight+1); - uint32_t nLegendOffset = 1; - for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) - { - if(nActiveGroup & (1ll << j)) - { - MicroProfileDrawText(nX, nY + (1+nHeight) * nLegendOffset, (uint32_t)-1, S.GroupInfo[j].pName, S.GroupInfo[j].nNameLen); - nLegendOffset += S.GroupInfo[j].nNumTimers+1; - } - } - if(S.nBars & MP_DRAW_TIMERS) - nX += MicroProfileDrawBarArray(nX, nY, pTimers, "Time", nTotalHeight) + 1; - if(S.nBars & MP_DRAW_AVERAGE) - nX += MicroProfileDrawBarArray(nX, nY, pAverage, "Average", nTotalHeight) + 1; - if(S.nBars & MP_DRAW_MAX) - nX += MicroProfileDrawBarArray(nX, nY, pMax, (!UI.bShowSpikes) ? "Max Time" : "Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverage : NULL) + 1; - if(S.nBars & MP_DRAW_CALL_COUNT) - { - nX += MicroProfileDrawBarArray(nX, nY, pCallAverage, "Call Average", nTotalHeight) + 1; - nX += MicroProfileDrawBarCallCount(nX, nY, "Count") + 1; - } - if(S.nBars & MP_DRAW_TIMERS_EXCLUSIVE) - nX += MicroProfileDrawBarArray(nX, nY, pTimersExclusive, "Exclusive Time", nTotalHeight) + 1; - if(S.nBars & MP_DRAW_AVERAGE_EXCLUSIVE) - nX += MicroProfileDrawBarArray(nX, nY, pAverageExclusive, "Exclusive Average", nTotalHeight) + 1; - if(S.nBars & MP_DRAW_MAX_EXCLUSIVE) - nX += MicroProfileDrawBarArray(nX, nY, pMaxExclusive, (!UI.bShowSpikes) ? "Exclusive Max Time" :"Excl Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverageExclusive : NULL) + 1; + uint64_t nActiveGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted; + if(!nActiveGroup) + return; + MICROPROFILE_SCOPE(g_MicroProfileDrawBarView); - for(int i = 0; i < MICROPROFILE_META_MAX; ++i) - { - if(S.nBars & (MP_DRAW_META_FIRST<= MP_DRAW_META_FIRST) + { + if(nMetaIndex < MICROPROFILE_META_MAX && S.MetaCounters[nMetaIndex].pName) + { + uint32_t nStrWidth = (uint32_t)strlen(S.MetaCounters[nMetaIndex].pName); + if(S.nBars & MP_DRAW_TIMERS) + nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth); + if(S.nBars & MP_DRAW_AVERAGE) + nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4); + if(S.nBars & MP_DRAW_MAX) + nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4); + if (S.nBars & MP_DRAW_MIN) + nWidth += 6 + (1 + MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4); + } + } + else + { + nWidth += MICROPROFILE_BAR_WIDTH + 6 + 6 * (1+MICROPROFILE_TEXT_WIDTH); + if(i & MP_DRAW_CALL_COUNT) + nWidth += 6 + 6 * MICROPROFILE_TEXT_WIDTH; + } + } + if(i >= MP_DRAW_META_FIRST) + { + ++nMetaIndex; + } + } + nWidth += (1+nMaxTimerNameLen) * (MICROPROFILE_TEXT_WIDTH+1); + for(uint32_t i = 0; i < nNumTimers+nNumGroups+1; ++i) + { + uint32_t nY0 = nY + i * (nHeight + 1); + bool bInside = (UI.nActiveMenu == (uint32_t)-1) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nHeight + 1))); + MicroProfileDrawBox(nX, nY0, nWidth+nX, nY0 + (nHeight+1)+1, UI.nOpacityBackground | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0))); + } + nX += 10; + } + int nTotalHeight = (nNumTimers+nNumGroups+1) * (nHeight+1); + uint32_t nLegendOffset = 1; + if(S.nBars & MP_DRAW_TIMERS) + nX += MicroProfileDrawBarArray(nX, nY, pTimers, "Time", nTotalHeight) + 1; + if(S.nBars & MP_DRAW_AVERAGE) + nX += MicroProfileDrawBarArray(nX, nY, pAverage, "Average", nTotalHeight) + 1; + if(S.nBars & MP_DRAW_MAX) + nX += MicroProfileDrawBarArray(nX, nY, pMax, (!UI.bShowSpikes) ? "Max Time" : "Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverage : NULL) + 1; + if (S.nBars & MP_DRAW_MIN) + nX += MicroProfileDrawBarArray(nX, nY, pMin, (!UI.bShowSpikes) ? "Min Time" : "Min Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverage : NULL) + 1; + if(S.nBars & MP_DRAW_CALL_COUNT) + { + nX += MicroProfileDrawBarArray(nX, nY, pCallAverage, "Call Average", nTotalHeight) + 1; + nX += MicroProfileDrawBarCallCount(nX, nY, "Count") + 1; + } + if(S.nBars & MP_DRAW_TIMERS_EXCLUSIVE) + nX += MicroProfileDrawBarArray(nX, nY, pTimersExclusive, "Exclusive Time", nTotalHeight) + 1; + if(S.nBars & MP_DRAW_AVERAGE_EXCLUSIVE) + nX += MicroProfileDrawBarArray(nX, nY, pAverageExclusive, "Exclusive Average", nTotalHeight) + 1; + if(S.nBars & MP_DRAW_MAX_EXCLUSIVE) + nX += MicroProfileDrawBarArray(nX, nY, pMaxExclusive, (!UI.bShowSpikes) ? "Exclusive Max Time" :"Excl Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverageExclusive : NULL) + 1; + + for(int i = 0; i < MICROPROFILE_META_MAX; ++i) + { + if(0 != (S.nBars & (MP_DRAW_META_FIRST<= nY0) && (UI.nMouseY < (nY0 + nHeight + 1))); + MicroProfileDrawBox(nX, nY0, nTimerWidth, nY0 + (nHeight+1)+1, 0xff000000 | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0))); + } + nX += MicroProfileDrawBarLegend(nX, nY, nTotalHeight, nTimerWidth-5) + 1; + + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + if(nActiveGroup & (1ll << j)) + { + MicroProfileDrawText(nX, nY + (1+nHeight) * nLegendOffset, (uint32_t)-1, S.GroupInfo[j].pName, S.GroupInfo[j].nNameLen); + nLegendOffset += S.GroupInfo[j].nNumTimers+1; + } + } + MicroProfileDrawHeader(nX, nTimerWidth-5, "Group"); + MicroProfileDrawTextRight(nTimerWidth-3, MICROPROFILE_TEXT_HEIGHT + 2, (uint32_t)-1, "Timer", 5); + MicroProfileDrawLineVertical(nTimerWidth, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); + MicroProfileDrawLineHorizontal(0, nWidth, 2*MICROPROFILE_TEXT_HEIGHT + 3, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]); +} + +typedef const char* (*MicroProfileSubmenuCallback)(int, bool* bSelected); +typedef void (*MicroProfileClickCallback)(int); + +const char* MicroProfileUIMenuMode(int nIndex, bool* bSelected) +{ + MicroProfile& S = *MicroProfileGet(); + switch(nIndex) + { + case 0: + *bSelected = S.nDisplay == MP_DRAW_DETAILED; + return "Detailed"; + case 1: + *bSelected = S.nDisplay == MP_DRAW_BARS; + return "Timers"; + case 2: + *bSelected = S.nDisplay == MP_DRAW_COUNTERS; + return "Counters"; + case 3: + *bSelected = S.nDisplay == MP_DRAW_FRAME; + return "Frame"; + case 4: + *bSelected = S.nDisplay == MP_DRAW_HIDDEN; + return "Hidden"; + case 5: + *bSelected = false; + return "Off"; + case 6: + *bSelected = false; + return "------"; + case 7: + *bSelected = S.nForceEnable != 0; + return "Force Enable"; + + default: return 0; + } +} + +const char* MicroProfileUIMenuGroups(int nIndex, bool* bSelected) +{ + MicroProfile& S = *MicroProfileGet(); + *bSelected = false; + if(nIndex == 0) + { + *bSelected = S.nAllGroupsWanted != 0; + return "[ALL]"; + } + else + { + nIndex = nIndex-1; + if(nIndex < (int)UI.GroupMenuCount) + { + MicroProfileGroupMenuItem& Item = UI.GroupMenu[nIndex]; + static char buffer[MICROPROFILE_NAME_MAX_LEN+32]; + if(Item.nIsCategory) + { + uint64_t nGroupMask = S.CategoryInfo[Item.nIndex].nGroupMask; + *bSelected = nGroupMask == (nGroupMask & S.nActiveGroupWanted); + snprintf(buffer, sizeof(buffer)-1, "[%s]", Item.pName); + } + else + { + *bSelected = 0 != (S.nActiveGroupWanted & (1ll << Item.nIndex)); + snprintf(buffer, sizeof(buffer)-1, " %s", Item.pName); + } + return buffer; + } + return 0; + } +} + +const char* MicroProfileUIMenuAggregate(int nIndex, bool* bSelected) +{ + MicroProfile& S = *MicroProfileGet(); + int nNumPresets = (int)sizeof(g_MicroProfileAggregatePresets) / (int)sizeof(g_MicroProfileAggregatePresets[0]); + if(nIndex < nNumPresets) + { + int val = g_MicroProfileAggregatePresets[nIndex]; + *bSelected = (int)S.nAggregateFlip == val; + if(0 == val) + return "Infinite"; + else + { + static char buf[128]; + snprintf(buf, sizeof(buf)-1, "%7d", val); + return buf; + } + } + return 0; + +} + +const char* MicroProfileUIMenuTimers(int nIndex, bool* bSelected) +{ + MicroProfile& S = *MicroProfileGet(); + + if(nIndex < 8) + { + static const char* kNames[] = { "Time", "Average", "Max", "Min", "Call Count", "Exclusive Timers", "Exclusive Average", "Exclusive Max" }; + + *bSelected = 0 != (S.nBars & (1 << nIndex)); + return kNames[nIndex]; + } + else if(nIndex == 8) + { + *bSelected = false; + return "------"; + } + else + { + int nMetaIndex = nIndex - 9; + if(nMetaIndex < MICROPROFILE_META_MAX) + { + *bSelected = 0 != (S.nBars & (MP_DRAW_META_FIRST << nMetaIndex)); + return S.MetaCounters[nMetaIndex].pName; + } + } + return 0; +} + +const char* MicroProfileUIMenuOptions(int nIndex, bool* bSelected) +{ + MicroProfile& S = *MicroProfileGet(); + if(nIndex >= MICROPROFILE_OPTION_SIZE) return 0; + switch(UI.Options[nIndex].nSubType) + { + case 0: + *bSelected = S.fReferenceTime == g_MicroProfileReferenceTimePresets[UI.Options[nIndex].nIndex]; + break; + case 1: + *bSelected = UI.nOpacityBackground>>24 == g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex]; + break; + case 2: + *bSelected = UI.nOpacityForeground>>24 == g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex]; + break; + case 3: + *bSelected = UI.bShowSpikes; + break; +#if MICROPROFILE_CONTEXT_SWITCH_TRACE + case 4: + { + switch(UI.Options[nIndex].nIndex) + { + case 0: + *bSelected = S.bContextSwitchAllThreads; + break; + case 1: + *bSelected = S.bContextSwitchNoBars; + break; + } + } + break; +#endif + } + return UI.Options[nIndex].Text; +} + +const char* MicroProfileUIMenuPreset(int nIndex, bool* bSelected) +{ + static char buf[128]; + *bSelected = false; + int nNumPresets = sizeof(g_MicroProfilePresetNames) / sizeof(g_MicroProfilePresetNames[0]); + int nIndexSave = nIndex - nNumPresets - 1; + if(nIndex == nNumPresets) + return "--"; + else if(nIndexSave >=0 && nIndexSave = 0 && nIndexSave < nNumPresets) + { + MicroProfileSavePreset(g_MicroProfilePresetNames[nIndexSave]); + } + else if(nIndex >= 0 && nIndex < nNumPresets) + { + MicroProfileLoadPreset(g_MicroProfilePresetNames[nIndex]); + } +} + +void MicroProfileUIClickCustom(int nIndex) +{ + if(nIndex == 0) + { + MicroProfileCustomGroupDisable(); + } + else + { + MicroProfileCustomGroupEnable(nIndex-2); + } +} + +void MicroProfileUIClickDump(int nIndex) +{ + time_t t = time(0); + + char Name[128] = {}; + strftime(Name, sizeof(Name), "microprofile-%Y%m%d-%H%M%S.html", localtime(&t)); + + char Path[512] = {}; + const char* pHome = getenv("HOME"); + const char* pHomeDrive = getenv("HOMEDRIVE"); + const char* pHomePath = getenv("HOMEPATH"); + if(pHome) + { + snprintf(Path, sizeof(Path)-1, "%s/%s", pHome, Name); + } + else if(pHomeDrive && pHomePath) + { + snprintf(Path, sizeof(Path)-1, "%s%s/%s", pHomeDrive, pHomePath, Name); + } + else + { + snprintf(Path, sizeof(Path)-1, "%s", Name); + } + + MicroProfileDumpFile(Path, MicroProfileDumpTypeHtml, 32 << nIndex); } void MicroProfileDrawMenu(uint32_t nWidth, uint32_t nHeight) { - MicroProfile& S = *MicroProfileGet(); + (void)nWidth; + (void)nHeight; - uint32_t nX = 0; - uint32_t nY = 0; - bool bMouseOver = UI.nMouseY < MICROPROFILE_TEXT_HEIGHT + 1; + MicroProfile& S = *MicroProfileGet(); + + uint32_t nX = 0; + uint32_t nY = 0; #define SBUF_SIZE 256 - char buffer[256]; - MicroProfileDrawBox(nX, nY, nX + nWidth, nY + (MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff000000|g_nMicroProfileBackColors[1]); + char buffer[256]; + MicroProfileDrawBox(nX, nY, nX + nWidth, nY + (MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff000000|g_nMicroProfileBackColors[1]); #define MICROPROFILE_MENU_MAX 16 - const char* pMenuText[MICROPROFILE_MENU_MAX] = {0}; - uint32_t nMenuX[MICROPROFILE_MENU_MAX] = {0}; - uint32_t nNumMenuItems = 0; + const char* pMenuText[MICROPROFILE_MENU_MAX] = {0}; + uint32_t nMenuX[MICROPROFILE_MENU_MAX] = {0}; + uint32_t nNumMenuItems = 0; - int nLen = snprintf(buffer, 127, "MicroProfile"); - MicroProfileDrawText(nX, nY, (uint32_t)-1, buffer, nLen); - nX += (sizeof("MicroProfile")+2) * (MICROPROFILE_TEXT_WIDTH+1); - pMenuText[nNumMenuItems++] = "Mode"; - pMenuText[nNumMenuItems++] = "Groups"; - char AggregateText[64]; - snprintf(AggregateText, sizeof(AggregateText)-1, "Aggregate[%d]", S.nAggregateFlip ? S.nAggregateFlip : S.nAggregateFlipCount); - pMenuText[nNumMenuItems++] = &AggregateText[0]; - pMenuText[nNumMenuItems++] = "Timers"; - pMenuText[nNumMenuItems++] = "Options"; - pMenuText[nNumMenuItems++] = "Preset"; - const int nPauseIndex = nNumMenuItems; - pMenuText[nNumMenuItems++] = S.nRunning ? "Pause" : "Unpause"; - pMenuText[nNumMenuItems++] = "Help"; + int nLen = snprintf(buffer, 127, "MicroProfile"); + MicroProfileDrawText(nX, nY, (uint32_t)-1, buffer, nLen); + nX += (sizeof("MicroProfile")+2) * (MICROPROFILE_TEXT_WIDTH+1); + pMenuText[nNumMenuItems++] = "Mode"; + pMenuText[nNumMenuItems++] = "Groups"; + char AggregateText[64]; + snprintf(AggregateText, sizeof(AggregateText)-1, "Aggregate[%d]", S.nAggregateFlip ? S.nAggregateFlip : S.nAggregateFlipCount); + pMenuText[nNumMenuItems++] = &AggregateText[0]; + pMenuText[nNumMenuItems++] = "Timers"; + pMenuText[nNumMenuItems++] = "Options"; + pMenuText[nNumMenuItems++] = "Preset"; + pMenuText[nNumMenuItems++] = "Custom"; + pMenuText[nNumMenuItems++] = "Dump"; + const int nPauseIndex = nNumMenuItems; + pMenuText[nNumMenuItems++] = S.nRunning ? "Pause" : "Unpause"; + pMenuText[nNumMenuItems++] = "Help"; - if(S.nOverflow) - { - pMenuText[nNumMenuItems++] = "!BUFFERSFULL!"; - } + if(S.nOverflow) + { + pMenuText[nNumMenuItems++] = "!BUFFERSFULL!"; + } - struct SOptionDesc - { - SOptionDesc(){} - SOptionDesc(uint8_t nSubType, uint8_t nIndex, const char* fmt, ...):nSubType(nSubType), nIndex(nIndex) - { - va_list args; - va_start (args, fmt); - vsprintf(Text, fmt, args); - va_end(args); - } - char Text[32]; - uint8_t nSubType; - uint8_t nIndex; - bool bSelected; - }; - static const int nNumReferencePresets = sizeof(g_MicroProfileReferenceTimePresets)/sizeof(g_MicroProfileReferenceTimePresets[0]); - static const int nNumOpacityPresets = sizeof(g_MicroProfileOpacityPresets)/sizeof(g_MicroProfileOpacityPresets[0]); + if(UI.GroupMenuCount != S.nGroupCount + S.nCategoryCount) + { + UI.GroupMenuCount = S.nGroupCount + S.nCategoryCount; + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + UI.GroupMenu[i].nIsCategory = 1; + UI.GroupMenu[i].nCategoryIndex = i; + UI.GroupMenu[i].nIndex = i; + UI.GroupMenu[i].pName = S.CategoryInfo[i].pName; + } + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + uint32_t idx = i + S.nCategoryCount; + UI.GroupMenu[idx].nIsCategory = 0; + UI.GroupMenu[idx].nCategoryIndex = S.GroupInfo[i].nCategory; + UI.GroupMenu[idx].nIndex = i; + UI.GroupMenu[idx].pName = S.GroupInfo[i].pName; + } + std::sort(&UI.GroupMenu[0], &UI.GroupMenu[UI.GroupMenuCount], + [] (const MicroProfileGroupMenuItem& l, const MicroProfileGroupMenuItem& r) -> bool + { + if(l.nCategoryIndex < r.nCategoryIndex) + { + return true; + } + else if(r.nCategoryIndex < l.nCategoryIndex) + { + return false; + } + if(r.nIsCategory || l.nIsCategory) + { + return l.nIsCategory > r.nIsCategory; + } + return MP_STRCASECMP(l.pName, r.pName)<0; + } + ); + } -#if MICROPROFILE_CONTEXT_SWITCH_TRACE - static const int nOptionSize = nNumReferencePresets + nNumOpacityPresets * 2 + 2 + 7; -#else - static const int nOptionSize = nNumReferencePresets + nNumOpacityPresets * 2 + 2 + 3; -#endif + MicroProfileSubmenuCallback GroupCallback[MICROPROFILE_MENU_MAX] = + { + MicroProfileUIMenuMode, + MicroProfileUIMenuGroups, + MicroProfileUIMenuAggregate, + MicroProfileUIMenuTimers, + MicroProfileUIMenuOptions, + MicroProfileUIMenuPreset, + MicroProfileUIMenuCustom, + MicroProfileUIMenuDump, + }; - static SOptionDesc Options[nOptionSize]; - static bool bOptionInit = false; - if(!bOptionInit) - { - bOptionInit = true; - int nIndex = 0; - Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "Reference"); - for(int i = 0; i < nNumReferencePresets; ++i) - { - Options[nIndex++] = SOptionDesc(0, i, " %6.2fms", g_MicroProfileReferenceTimePresets[i]); - } - Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "BG Opacity"); - for(int i = 0; i < nNumOpacityPresets; ++i) - { - Options[nIndex++] = SOptionDesc(1, i, " %7d%%", (i+1)*25); - } - Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "FG Opacity"); - for(int i = 0; i < nNumOpacityPresets; ++i) - { - Options[nIndex++] = SOptionDesc(2, i, " %7d%%", (i+1)*25); - } - Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "Spike Display"); - Options[nIndex++] = SOptionDesc(3, 0, "%s", " Enable"); - -#if MICROPROFILE_CONTEXT_SWITCH_TRACE - Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "CSwitch Trace"); - Options[nIndex++] = SOptionDesc(4, 0, "%s", " Enable"); - Options[nIndex++] = SOptionDesc(4, 1, "%s", " All Threads"); - Options[nIndex++] = SOptionDesc(4, 2, "%s", " No Bars"); -#endif + MicroProfileClickCallback CBClick[MICROPROFILE_MENU_MAX] = + { + MicroProfileUIClickMode, + MicroProfileUIClickGroups, + MicroProfileUIClickAggregate, + MicroProfileUIClickTimers, + MicroProfileUIClickOptions, + MicroProfileUIClickPreset, + MicroProfileUIClickCustom, + MicroProfileUIClickDump, + }; - MP_ASSERT(nIndex == nOptionSize); - } + uint32_t nSelectMenu = (uint32_t)-1; + for(uint32_t i = 0; i < nNumMenuItems; ++i) + { + nMenuX[i] = nX; + uint32_t nLen = (uint32_t)strlen(pMenuText[i]); + uint32_t nEnd = nX + nLen * (MICROPROFILE_TEXT_WIDTH+1); + if(UI.nMouseY <= MICROPROFILE_TEXT_HEIGHT && UI.nMouseX <= nEnd && UI.nMouseX >= nX) + { + MicroProfileDrawBox(nX-1, nY, nX + nLen * (MICROPROFILE_TEXT_WIDTH+1), nY +(MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff888888); + nSelectMenu = i; + if((UI.nMouseLeft || UI.nMouseRight) && (int)i == nPauseIndex) + { + S.nToggleRunning = 1; + } + } + MicroProfileDrawText(nX, nY, (uint32_t)-1, pMenuText[i], (uint32_t)strlen(pMenuText[i])); + nX += (nLen+1) * (MICROPROFILE_TEXT_WIDTH+1); + } + uint32_t nMenu = nSelectMenu != (uint32_t)-1 ? nSelectMenu : UI.nActiveMenu; + UI.nActiveMenu = nSelectMenu; + if((uint32_t)-1 != nMenu && GroupCallback[nMenu]) + { + nX = nMenuX[nMenu]; + nY += MICROPROFILE_TEXT_HEIGHT+1; + MicroProfileSubmenuCallback CB = GroupCallback[nMenu]; + int nNumLines = 0; + bool bSelected = false; + const char* pString = CB(nNumLines, &bSelected); + uint32_t nWidth = 0, nHeight = 0; + while(pString) + { + nWidth = MicroProfileMax(nWidth, (int)strlen(pString)); + nNumLines++; + pString = CB(nNumLines, &bSelected); + } + nWidth = (2+nWidth) * (MICROPROFILE_TEXT_WIDTH+1); + nHeight = nNumLines * (MICROPROFILE_TEXT_HEIGHT+1); + if(UI.nMouseY <= nY + nHeight+0 && UI.nMouseY >= nY-0 && UI.nMouseX <= nX + nWidth + 0 && UI.nMouseX >= nX - 0) + { + UI.nActiveMenu = nMenu; + } + MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000|g_nMicroProfileBackColors[1]); + for(int i = 0; i < nNumLines; ++i) + { + bool bSelected = false; + const char* pString = CB(i, &bSelected); + if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT + 1) + { + if((UI.nMouseLeft || UI.nMouseRight) && CBClick[nMenu]) + { + CBClick[nMenu](i); + } + MicroProfileDrawBox(nX, nY, nX + nWidth, nY + MICROPROFILE_TEXT_HEIGHT + 1, 0xff888888); + } + int nLen = snprintf(buffer, SBUF_SIZE-1, "%c %s", bSelected ? '*' : ' ' ,pString); + MicroProfileDrawText(nX, nY, (uint32_t)-1, buffer, nLen); + nY += MICROPROFILE_TEXT_HEIGHT+1; + } + } - - - typedef std::function SubmenuCallback; - typedef std::function ClickCallback; - SubmenuCallback GroupCallback[] = - { [] (int index, bool& bSelected) -> const char*{ - MicroProfile& S = *MicroProfileGet(); - switch(index) - { - case 0: - bSelected = S.nDisplay == MP_DRAW_DETAILED; - return "Detailed"; - case 1: - bSelected = S.nDisplay == MP_DRAW_BARS; - return "Timers"; - case 2: - bSelected = S.nDisplay == MP_DRAW_HIDDEN; - return "Hidden"; - case 3: - bSelected = false; - return "Off"; - case 4: - bSelected = false; - return "------"; - case 5: - bSelected = S.nForceEnable != 0; - return "Force Enable"; - - default: return 0; - } - }, - [] (int index, bool& bSelected) -> const char*{ - MicroProfile& S = *MicroProfileGet(); - - if(index == 0) - { - bSelected = S.nAllGroupsWanted != 0; - return "ALL"; - } - else - { - index = index-1; - bSelected = 0 != (S.nActiveGroupWanted & (1ll << index)); - if(index < MICROPROFILE_MAX_GROUPS && S.GroupInfo[index].pName[0] != '\0') - return S.GroupInfo[index].pName; - else - return 0; - } - }, - [] (int index, bool& bSelected) -> const char*{ - MicroProfile& S = *MicroProfileGet(); - if(index < sizeof(g_MicroProfileAggregatePresets)/sizeof(g_MicroProfileAggregatePresets[0])) - { - int val = g_MicroProfileAggregatePresets[index]; - bSelected = (int)S.nAggregateFlip == val; - if(0 == val) - return "Infinite"; - else - { - static char buf[128]; - snprintf(buf, sizeof(buf)-1, "%7d", val); - return buf; - } - } - return 0; - }, - [] (int index, bool& bSelected) -> const char*{ - MicroProfile& S = *MicroProfileGet(); - bSelected = 0 != (S.nBars & (1 << index)); - switch(index) - { - case 0: return "Time"; - case 1: return "Average"; - case 2: return "Max"; - case 3: return "Call Count"; - case 4: return "Exclusive Timers"; - case 5: return "Exclusive Average"; - case 6: return "Exclusive Max"; - } - int nMetaIndex = index - 7; - if(nMetaIndex < MICROPROFILE_META_MAX) - { - return S.MetaCounters[nMetaIndex].pName; - } - return 0; - }, - [] (int index, bool& bSelected) -> const char*{ - MicroProfile& S = *MicroProfileGet(); - if(index >= nOptionSize) return 0; - switch(Options[index].nSubType) - { - case 0: - bSelected = S.fReferenceTime == g_MicroProfileReferenceTimePresets[Options[index].nIndex]; - break; - case 1: - bSelected = UI.nOpacityBackground>>24 == g_MicroProfileOpacityPresets[Options[index].nIndex]; - break; - case 2: - bSelected = UI.nOpacityForeground>>24 == g_MicroProfileOpacityPresets[Options[index].nIndex]; - break; - case 3: - bSelected = UI.bShowSpikes; - break; -#if MICROPROFILE_CONTEXT_SWITCH_TRACE - case 4: - { - switch(Options[index].nIndex) - { - case 0: - bSelected = S.bContextSwitchRunning; - break; - case 1: - bSelected = S.bContextSwitchAllThreads; - break; - case 2: - bSelected = S.bContextSwitchNoBars; - break; - } - } - break; -#endif - } - return Options[index].Text; - }, - - [] (int index, bool& bSelected) -> const char*{ - static char buf[128]; - bSelected = false; - int nNumPresets = sizeof(g_MicroProfilePresetNames) / sizeof(g_MicroProfilePresetNames[0]); - int nIndexSave = index - nNumPresets - 1; - if(index == nNumPresets) - return "--"; - else if(nIndexSave >=0 && nIndexSave const char*{ - return 0; - }, - [] (int index, bool& bSelected) -> const char*{ - return 0; - }, - [] (int index, bool& bSelected) -> const char*{ - return 0; - }, - - - }; - ClickCallback CBClick[] = - { - [](int nIndex) - { - MicroProfile& S = *MicroProfileGet(); - switch(nIndex) - { - case 0: - S.nDisplay = MP_DRAW_DETAILED; - break; - case 1: - S.nDisplay = MP_DRAW_BARS; - break; - case 2: - S.nDisplay = MP_DRAW_HIDDEN; - break; - case 3: - S.nDisplay = 0; - break; - case 4: - break; - case 5: - S.nForceEnable = !S.nForceEnable; - break; - } - }, - [](int nIndex) - { - MicroProfile& S = *MicroProfileGet(); - if(nIndex == 0) - S.nAllGroupsWanted = 1-S.nAllGroupsWanted; - else - S.nActiveGroupWanted ^= (1ll << (nIndex-1)); - }, - [](int nIndex) - { - MicroProfile& S = *MicroProfileGet(); - S.nAggregateFlip = g_MicroProfileAggregatePresets[nIndex]; - if(0 == S.nAggregateFlip) - { - S.nAggregateClear = 1; - } - }, - [](int nIndex) - { - MicroProfile& S = *MicroProfileGet(); - S.nBars ^= (1 << nIndex); - }, - [](int nIndex) - { - MicroProfile& S = *MicroProfileGet(); - switch(Options[nIndex].nSubType) - { - case 0: - S.fReferenceTime = g_MicroProfileReferenceTimePresets[Options[nIndex].nIndex]; - S.fRcpReferenceTime = 1.f / S.fReferenceTime; - break; - case 1: - UI.nOpacityBackground = g_MicroProfileOpacityPresets[Options[nIndex].nIndex]<<24; - break; - case 2: - UI.nOpacityForeground = g_MicroProfileOpacityPresets[Options[nIndex].nIndex]<<24; - break; - case 3: - UI.bShowSpikes = !UI.bShowSpikes; - break; -#if MICROPROFILE_CONTEXT_SWITCH_TRACE - case 4: - { - switch(Options[nIndex].nIndex) - { - case 0: - if(S.bContextSwitchRunning) - { - MicroProfileStopContextSwitchTrace(); - } - else - { - MicroProfileStartContextSwitchTrace(); - } - break; - case 1: - S.bContextSwitchAllThreads = !S.bContextSwitchAllThreads; - break; - case 2: - S.bContextSwitchNoBars= !S.bContextSwitchNoBars; - break; - - } - } - break; -#endif - } - }, - [](int nIndex) - { - int nNumPresets = sizeof(g_MicroProfilePresetNames) / sizeof(g_MicroProfilePresetNames[0]); - int nIndexSave = nIndex - nNumPresets - 1; - if(nIndexSave >= 0 && nIndexSave < nNumPresets) - { - MicroProfileSavePreset(g_MicroProfilePresetNames[nIndexSave]); - } - else if(nIndex >= 0 && nIndex < nNumPresets) - { - MicroProfileLoadPreset(g_MicroProfilePresetNames[nIndex]); - } - }, - [](int nIndex) - { - }, - [](int nIndex) - { - }, - [](int nIndex) - { - }, - }; - - uint32_t nSelectMenu = (uint32_t)-1; - for(uint32_t i = 0; i < nNumMenuItems; ++i) - { - nMenuX[i] = nX; - uint32_t nLen = (uint32_t)strlen(pMenuText[i]); - uint32_t nEnd = nX + nLen * (MICROPROFILE_TEXT_WIDTH+1); - if(UI.nMouseY <= MICROPROFILE_TEXT_HEIGHT && UI.nMouseX <= nEnd && UI.nMouseX >= nX) - { - MicroProfileDrawBox(nX-1, nY, nX + nLen * (MICROPROFILE_TEXT_WIDTH+1), nY +(MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff888888); - nSelectMenu = i; - if((UI.nMouseLeft || UI.nMouseRight) && i == (int)nPauseIndex) - { - S.nToggleRunning = 1; - } - } - MicroProfileDrawText(nX, nY, (uint32_t)-1, pMenuText[i], (uint32_t)strlen(pMenuText[i])); - nX += (nLen+1) * (MICROPROFILE_TEXT_WIDTH+1); - } - uint32_t nMenu = nSelectMenu != (uint32_t)-1 ? nSelectMenu : UI.nActiveMenu; - UI.nActiveMenu = nMenu; - if((uint32_t)-1 != nMenu) - { - nX = nMenuX[nMenu]; - nY += MICROPROFILE_TEXT_HEIGHT+1; - SubmenuCallback CB = GroupCallback[nMenu]; - int nNumLines = 0; - bool bSelected = false; - const char* pString = CB(nNumLines, bSelected); - uint32_t nWidth = 0, nHeight = 0; - while(pString) - { - nWidth = MicroProfileMax(nWidth, (int)strlen(pString)); - nNumLines++; - pString = CB(nNumLines, bSelected); - } - nWidth = (2+nWidth) * (MICROPROFILE_TEXT_WIDTH+1); - nHeight = nNumLines * (MICROPROFILE_TEXT_HEIGHT+1); - if(UI.nMouseY <= nY + nHeight+0 && UI.nMouseY >= nY-0 && UI.nMouseX <= nX + nWidth + 0 && UI.nMouseX >= nX - 0) - { - UI.nActiveMenu = nMenu; - } - else if(nSelectMenu == (uint32_t)-1) - { - UI.nActiveMenu = (uint32_t)-1; - } - MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000|g_nMicroProfileBackColors[1]); - for(int i = 0; i < nNumLines; ++i) - { - bool bSelected = false; - const char* pString = CB(i, bSelected); - if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT + 1) - { - bMouseOver = true; - if(UI.nMouseLeft || UI.nMouseRight) - { - CBClick[nMenu](i); - } - MicroProfileDrawBox(nX, nY, nX + nWidth, nY + MICROPROFILE_TEXT_HEIGHT + 1, 0xff888888); - } - int nLen = snprintf(buffer, SBUF_SIZE-1, "%c %s", bSelected ? '*' : ' ' ,pString); - MicroProfileDrawText(nX, nY, (uint32_t)-1, buffer, nLen); - nY += MICROPROFILE_TEXT_HEIGHT+1; - } - } - - - { - static char FrameTimeMessage[64]; - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; - float fMs = fToMs * (S.nFlipTicks); - float fAverageMs = fToMs * (S.nFlipAggregateDisplay / nAggregateFrames); - float fMaxMs = fToMs * S.nFlipMaxDisplay; - int nLen = snprintf(FrameTimeMessage, sizeof(FrameTimeMessage)-1, "Time[%6.2f] Avg[%6.2f] Max[%6.2f]", fMs, fAverageMs, fMaxMs); - pMenuText[nNumMenuItems++] = &FrameTimeMessage[0]; - MicroProfileDrawText(nWidth - nLen * (MICROPROFILE_TEXT_WIDTH+1), 0, -1, FrameTimeMessage, nLen); - } + { + static char FrameTimeMessage[64]; + float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + float fMs = fToMs * (S.nFlipTicks); + float fAverageMs = fToMs * (S.nFlipAggregateDisplay / nAggregateFrames); + float fMaxMs = fToMs * S.nFlipMaxDisplay; + int nLen = snprintf(FrameTimeMessage, sizeof(FrameTimeMessage)-1, "Time[%6.2f] Avg[%6.2f] Max[%6.2f]", fMs, fAverageMs, fMaxMs); + pMenuText[nNumMenuItems++] = &FrameTimeMessage[0]; + MicroProfileDrawText(nWidth - nLen * (MICROPROFILE_TEXT_WIDTH+1), 0, -1, FrameTimeMessage, nLen); + } } void MicroProfileMoveGraph() { - int nZoom = UI.nMouseWheelDelta; - int nPanX = 0; - int nPanY = 0; - static int X = 0, Y = 0; - if(UI.nMouseDownLeft && !UI.nModDown) - { - nPanX = UI.nMouseX - X; - nPanY = UI.nMouseY - Y; - } - X = UI.nMouseX; - Y = UI.nMouseY; + int nZoom = UI.nMouseWheelDelta; + int nPanX = 0; + int nPanY = 0; + static int X = 0, Y = 0; + if(UI.nMouseDownLeft && !UI.nModDown) + { + nPanX = UI.nMouseX - X; + nPanY = UI.nMouseY - Y; + } + X = UI.nMouseX; + Y = UI.nMouseY; - if(nZoom) - { - float fOldRange = UI.fDetailedRange; - if(nZoom>0) - { - UI.fDetailedRangeTarget = UI.fDetailedRange *= UI.nModDown ? 1.40f : 1.05f; - } - else - { - float fNewDetailedRange = UI.fDetailedRange / (UI.nModDown ? 1.40f : 1.05f); - if(fNewDetailedRange < 1e-4f) //100ns - fNewDetailedRange = 1e-4f; - UI.fDetailedRangeTarget = UI.fDetailedRange = fNewDetailedRange; - } + if(nZoom) + { + float fOldRange = UI.fDetailedRange; + if(nZoom>0) + { + UI.fDetailedRangeTarget = UI.fDetailedRange *= UI.nModDown ? 1.40f : 1.05f; + } + else + { + float fNewDetailedRange = UI.fDetailedRange / (UI.nModDown ? 1.40f : 1.05f); + if(fNewDetailedRange < 1e-4f) //100ns + fNewDetailedRange = 1e-4f; + UI.fDetailedRangeTarget = UI.fDetailedRange = fNewDetailedRange; + } - float fDiff = fOldRange - UI.fDetailedRange; - float fMousePrc = MicroProfileMax((float)UI.nMouseX / UI.nWidth ,0.f); - UI.fDetailedOffsetTarget = UI.fDetailedOffset += fDiff * fMousePrc; + float fDiff = fOldRange - UI.fDetailedRange; + float fMousePrc = MicroProfileMax((float)UI.nMouseX / UI.nWidth ,0.f); + UI.fDetailedOffsetTarget = UI.fDetailedOffset += fDiff * fMousePrc; - } - if(nPanX) - { - UI.fDetailedOffsetTarget = UI.fDetailedOffset += -nPanX * UI.fDetailedRange / UI.nWidth; - } - UI.nOffsetY -= nPanY; - if(UI.nOffsetY<0) - UI.nOffsetY = 0; + } + if(nPanX) + { + UI.fDetailedOffsetTarget = UI.fDetailedOffset += -nPanX * UI.fDetailedRange / UI.nWidth; + } + int nMode = MicroProfileGet()->nDisplay; + if(nMode < MP_DRAW_SIZE) + { + UI.nOffsetY[nMode] -= nPanY; + UI.nOffsetX[nMode] += nPanX; + if(UI.nOffsetX[nMode] > 0) + UI.nOffsetX[nMode] = 0; + if(UI.nOffsetY[nMode] < 0) + UI.nOffsetY[nMode] = 0; + } } +void MicroProfileDrawCustom(uint32_t nWidth, uint32_t nHeight) +{ + (void)nWidth; + + if((uint32_t)-1 != UI.nCustomActive) + { + MicroProfile& S = *MicroProfileGet(); + MP_ASSERT(UI.nCustomActive < MICROPROFILE_CUSTOM_MAX); + MicroProfileCustom* pCustom = &UI.Custom[UI.nCustomActive]; + uint32_t nCount = pCustom->nNumTimers; + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + uint32_t nExtraOffset = 1 + ((pCustom->nFlags & MICROPROFILE_CUSTOM_STACK) != 0 ? 3 : 0); + uint32_t nOffsetYBase = nHeight - (nExtraOffset+nCount)* (1+MICROPROFILE_TEXT_HEIGHT) - MICROPROFILE_CUSTOM_PADDING; + uint32_t nOffsetY = nOffsetYBase; + float fReference = pCustom->fReference; + float fRcpReference = 1.f / fReference; + uint32_t nReducedWidth = UI.nWidth - 2*MICROPROFILE_CUSTOM_PADDING - MICROPROFILE_GRAPH_WIDTH; + + char Buffer[MICROPROFILE_NAME_MAX_LEN*2+1]; + float* pTime = (float*)alloca(sizeof(float)*nCount); + float* pTimeAvg = (float*)alloca(sizeof(float)*nCount); + float* pTimeMax = (float*)alloca(sizeof(float)*nCount); + uint32_t* pColors = (uint32_t*)alloca(sizeof(uint32_t)*nCount); + uint32_t nMaxOffsetX = 0; + MicroProfileDrawBox(MICROPROFILE_CUSTOM_PADDING-1, nOffsetY-1, MICROPROFILE_CUSTOM_PADDING+nReducedWidth+1, UI.nHeight - MICROPROFILE_CUSTOM_PADDING+1, 0x88000000|g_nMicroProfileBackColors[0]); + + for(uint32_t i = 0; i < nCount; ++i) + { + uint16_t nTimerIndex = MicroProfileGetTimerIndex(pCustom->pTimers[i]); + uint16_t nGroupIndex = MicroProfileGetGroupIndex(pCustom->pTimers[i]); + float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[nGroupIndex].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + pTime[i] = S.Frame[nTimerIndex].nTicks * fToMs; + pTimeAvg[i] = fToMs * (S.Aggregate[nTimerIndex].nTicks / nAggregateFrames); + pTimeMax[i] = fToMs * (S.AggregateMax[nTimerIndex]); + pColors[i] = S.TimerInfo[nTimerIndex].nColor; + } + + MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING + 3*MICROPROFILE_TEXT_WIDTH, nOffsetY, (uint32_t)-1, "Avg", sizeof("Avg")-1); + MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING + 13*MICROPROFILE_TEXT_WIDTH, nOffsetY, (uint32_t)-1, "Max", sizeof("Max")-1); + for(uint32_t i = 0; i < nCount; ++i) + { + nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT); + uint16_t nTimerIndex = MicroProfileGetTimerIndex(pCustom->pTimers[i]); + uint16_t nGroupIndex = MicroProfileGetGroupIndex(pCustom->pTimers[i]); + MicroProfileTimerInfo* pTimerInfo = &S.TimerInfo[nTimerIndex]; + int nSize; + uint32_t nOffsetX = MICROPROFILE_CUSTOM_PADDING; + nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2f", pTimeAvg[i]); + MicroProfileDrawText(nOffsetX, nOffsetY, (uint32_t)-1, Buffer, nSize); + nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1); + nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2f", pTimeMax[i]); + MicroProfileDrawText(nOffsetX, nOffsetY, (uint32_t)-1, Buffer, nSize); + nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1); + nSize = snprintf(Buffer, sizeof(Buffer)-1, "%s:%s", S.GroupInfo[nGroupIndex].pName, pTimerInfo->pName); + MicroProfileDrawText(nOffsetX, nOffsetY, pTimerInfo->nColor, Buffer, nSize); + nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1); + nMaxOffsetX = MicroProfileMax(nMaxOffsetX, nOffsetX); + } + uint32_t nMaxWidth = nReducedWidth- nMaxOffsetX; + + if(pCustom->nFlags & MICROPROFILE_CUSTOM_BARS) + { + nOffsetY = nOffsetYBase; + float* pMs = pCustom->nFlags & MICROPROFILE_CUSTOM_BAR_SOURCE_MAX ? pTimeMax : pTimeAvg; + const char* pString = pCustom->nFlags & MICROPROFILE_CUSTOM_BAR_SOURCE_MAX ? "Max" : "Avg"; + MicroProfileDrawText(nMaxOffsetX, nOffsetY, (uint32_t)-1, pString, (uint32_t)strlen(pString)); + int nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2fms", fReference); + MicroProfileDrawText(nReducedWidth - (1+nSize) * (MICROPROFILE_TEXT_WIDTH+1), nOffsetY, (uint32_t)-1, Buffer, nSize); + for(uint32_t i = 0; i < nCount; ++i) + { + nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT); + uint32_t nWidth = MicroProfileMin(nMaxWidth, (uint32_t)(nMaxWidth * pMs[i] * fRcpReference)); + MicroProfileDrawBox(nMaxOffsetX, nOffsetY, nMaxOffsetX+nWidth, nOffsetY+MICROPROFILE_TEXT_HEIGHT, pColors[i]|0xff000000); + } + } + if(pCustom->nFlags & MICROPROFILE_CUSTOM_STACK) + { + nOffsetY += 2*(1+MICROPROFILE_TEXT_HEIGHT); + const char* pString = pCustom->nFlags & MICROPROFILE_CUSTOM_STACK_SOURCE_MAX ? "Max" : "Avg"; + MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING, nOffsetY, (uint32_t)-1, pString, (uint32_t)strlen(pString)); + int nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2fms", fReference); + MicroProfileDrawText(nReducedWidth - (1+nSize) * (MICROPROFILE_TEXT_WIDTH+1), nOffsetY, (uint32_t)-1, Buffer, nSize); + nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT); + float fPosX = MICROPROFILE_CUSTOM_PADDING; + float* pMs = pCustom->nFlags & MICROPROFILE_CUSTOM_STACK_SOURCE_MAX ? pTimeMax : pTimeAvg; + for(uint32_t i = 0; i < nCount; ++i) + { + float fWidth = pMs[i] * fRcpReference * nReducedWidth; + uint32_t nX = (uint32_t)fPosX; + fPosX += fWidth; + uint32_t nXEnd = (uint32_t)fPosX; + if(nX < nXEnd) + { + MicroProfileDrawBox(nX, nOffsetY, nXEnd, nOffsetY+MICROPROFILE_TEXT_HEIGHT, pColors[i]|0xff000000); + } + } + } + } +} void MicroProfileDraw(uint32_t nWidth, uint32_t nHeight) { - MICROPROFILE_SCOPE(g_MicroProfileDraw); - MicroProfile& S = *MicroProfileGet(); + MICROPROFILE_SCOPE(g_MicroProfileDraw); + MicroProfile& S = *MicroProfileGet(); + + { + static int once = 0; + if(0 == once) + { + std::recursive_mutex& m = MicroProfileGetMutex(); + m.lock(); + MicroProfileInitUI(); + uint32_t nDisplay = S.nDisplay; + MicroProfileLoadPreset(MICROPROFILE_DEFAULT_PRESET); + once++; + S.nDisplay = nDisplay;// dont load display, just state + m.unlock(); + + } + } - { - static int once = 0; - if(0 == once) - { - std::recursive_mutex& m = MicroProfileGetMutex(); - m.lock(); - MicroProfileInitUI(); + if(S.nDisplay) + { + std::recursive_mutex& m = MicroProfileGetMutex(); + m.lock(); + UI.nWidth = nWidth; + UI.nHeight = nHeight; + UI.nHoverToken = MICROPROFILE_INVALID_TOKEN; + UI.nHoverTime = 0; + UI.nHoverFrame = -1; + if(S.nDisplay != MP_DRAW_DETAILED) + S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = -1; + MicroProfileMoveGraph(); + + + if(S.nDisplay == MP_DRAW_DETAILED || S.nDisplay == MP_DRAW_FRAME) + { + MicroProfileDrawDetailedView(nWidth, nHeight, /* bDrawBars= */ S.nDisplay == MP_DRAW_DETAILED); + } + else if(S.nDisplay == MP_DRAW_BARS && S.nBars) + { + MicroProfileDrawBarView(nWidth, nHeight); + } + else if(S.nDisplay == MP_DRAW_COUNTERS) + { + MicroProfileDrawCounterView(nWidth, nHeight); + } + + MicroProfileDrawMenu(nWidth, nHeight); + bool bMouseOverGraph = MicroProfileDrawGraph(nWidth, nHeight); + MicroProfileDrawCustom(nWidth, nHeight); + bool bHidden = S.nDisplay == MP_DRAW_HIDDEN; + if(!bHidden) + { + uint32_t nLockedToolTipX = 3; + bool bDeleted = false; + for(int i = 0; i < MICROPROFILE_TOOLTIP_MAX_LOCKED; ++i) + { + int nIndex = (g_MicroProfileUI.LockedToolTipFront + i) % MICROPROFILE_TOOLTIP_MAX_LOCKED; + if(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0]) + { + uint32_t nToolTipWidth = 0, nToolTipHeight = 0; + MicroProfileFloatWindowSize(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings, g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, 0, nToolTipWidth, nToolTipHeight, 0); + uint32_t nStartY = nHeight - nToolTipHeight - 2; + if(!bDeleted && UI.nMouseY > nStartY && UI.nMouseX > nLockedToolTipX && UI.nMouseX <= nLockedToolTipX + nToolTipWidth && (UI.nMouseLeft || UI.nMouseRight) ) + { + bDeleted = true; + int j = i; + for(; j < MICROPROFILE_TOOLTIP_MAX_LOCKED-1; ++j) + { + int nIndex0 = (g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED; + int nIndex1 = (g_MicroProfileUI.LockedToolTipFront + j+1) % MICROPROFILE_TOOLTIP_MAX_LOCKED; + MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex0], &g_MicroProfileUI.LockedToolTips[nIndex1]); + } + MicroProfileStringArrayClear(&g_MicroProfileUI.LockedToolTips[(g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED]); + } + else + { + MicroProfileDrawFloatWindow(nLockedToolTipX, nHeight-nToolTipHeight-2, &g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0], g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, g_MicroProfileUI.nLockedToolTipColor[nIndex]); + nLockedToolTipX += nToolTipWidth + 4; + } + } + } + + if(UI.nActiveMenu == 9) + { + if(S.nDisplay & MP_DRAW_DETAILED) + { + MicroProfileStringArray DetailedHelp; + MicroProfileStringArrayClear(&DetailedHelp); + MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_LEFT); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Toggle Graph"); + MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_RIGHT); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom"); + MicroProfileStringArrayFormat(&DetailedHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Lock Tooltip"); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Drag"); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Pan View"); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Mouse Wheel"); + MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom"); + MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, DetailedHelp.ppStrings, DetailedHelp.nNumStrings, 0xff777777); + + MicroProfileStringArray DetailedHistoryHelp; + MicroProfileStringArrayClear(&DetailedHistoryHelp); + MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_LEFT); + MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Center View"); + MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_RIGHT); + MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Zoom to frame"); + MicroProfileDrawFloatWindow(nWidth, 20, DetailedHistoryHelp.ppStrings, DetailedHistoryHelp.nNumStrings, 0xff777777); - uint32_t nDisplay = S.nDisplay; - MicroProfileLoadPreset(MICROPROFILE_DEFAULT_PRESET); - once++; - S.nDisplay = nDisplay;// dont load display, just state - m.unlock(); + } + else if(0 != (S.nDisplay & MP_DRAW_BARS) && S.nBars) + { + MicroProfileStringArray BarHelp; + MicroProfileStringArrayClear(&BarHelp); + MicroProfileStringArrayFormat(&BarHelp, "%s", MICROPROFILE_HELP_LEFT); + MicroProfileStringArrayAddLiteral(&BarHelp, "Toggle Graph"); + MicroProfileStringArrayFormat(&BarHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT); + MicroProfileStringArrayAddLiteral(&BarHelp, "Lock Tooltip"); + MicroProfileStringArrayAddLiteral(&BarHelp, "Drag"); + MicroProfileStringArrayAddLiteral(&BarHelp, "Pan View"); + MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, BarHelp.ppStrings, BarHelp.nNumStrings, 0xff777777); - } - } + } + MicroProfileStringArray Debug; + MicroProfileStringArrayClear(&Debug); + MicroProfileStringArrayAddLiteral(&Debug, "Memory Usage"); + MicroProfileStringArrayFormat(&Debug, "%4.2fmb", S.nMemUsage / (1024.f * 1024.f)); +#if MICROPROFILE_WEBSERVER + MicroProfileStringArrayAddLiteral(&Debug, "Web Server Port"); + MicroProfileStringArrayFormat(&Debug, "%d", MicroProfileWebServerPort()); +#endif + uint32_t nFrameNext = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY; + MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; + MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext]; - if(S.nDisplay) - { - std::recursive_mutex& m = MicroProfileGetMutex(); - m.lock(); - UI.nWidth = nWidth; - UI.nHeight = nHeight; - UI.nHoverToken = MICROPROFILE_INVALID_TOKEN; - UI.nHoverTime = 0; - UI.nHoverFrame = -1; - if(S.nDisplay != MP_DRAW_DETAILED) - S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = -1; - MicroProfileMoveGraph(); - - - if(S.nDisplay == MP_DRAW_DETAILED) - { - MicroProfileDrawDetailedView(nWidth, nHeight); - } - else if(S.nDisplay == MP_DRAW_BARS && S.nBars) - { - MicroProfileDrawBarView(nWidth, nHeight); - } - - MicroProfileDrawMenu(nWidth, nHeight); - bool bMouseOverGraph = MicroProfileDrawGraph(nWidth, nHeight); - bool bHidden = S.nDisplay == MP_DRAW_HIDDEN; - if(!bHidden) - { - uint32_t nLockedToolTipX = 3; - bool bDeleted = false; - for(int i = 0; i < MICROPROFILE_TOOLTIP_MAX_LOCKED; ++i) - { - int nIndex = (g_MicroProfileUI.LockedToolTipFront + i) % MICROPROFILE_TOOLTIP_MAX_LOCKED; - if(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0]) - { - uint32_t nToolTipWidth = 0, nToolTipHeight = 0; - MicroProfileFloatWindowSize(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings, g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, 0, nToolTipWidth, nToolTipHeight, 0); - uint32_t nStartY = nHeight - nToolTipHeight - 2; - if(!bDeleted && UI.nMouseY > nStartY && UI.nMouseX > nLockedToolTipX && UI.nMouseX <= nLockedToolTipX + nToolTipWidth && (UI.nMouseLeft || UI.nMouseRight) ) - { - bDeleted = true; - int j = i; - for(; j < MICROPROFILE_TOOLTIP_MAX_LOCKED-1; ++j) - { - int nIndex0 = (g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED; - int nIndex1 = (g_MicroProfileUI.LockedToolTipFront + j+1) % MICROPROFILE_TOOLTIP_MAX_LOCKED; - MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex0], &g_MicroProfileUI.LockedToolTips[nIndex1]); - } - MicroProfileStringArrayClear(&g_MicroProfileUI.LockedToolTips[(g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED]); - } - else - { - MicroProfileDrawFloatWindow(nLockedToolTipX, nHeight-nToolTipHeight-2, &g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0], g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, g_MicroProfileUI.nLockedToolTipColor[nIndex]); - nLockedToolTipX += nToolTipWidth + 4; - } - } - } - - if(UI.nActiveMenu == 7) - { - if(S.nDisplay & MP_DRAW_DETAILED) - { - MicroProfileStringArray DetailedHelp; - MicroProfileStringArrayClear(&DetailedHelp); - MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_LEFT); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Toggle Graph"); - MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_ALT); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom"); - MicroProfileStringArrayFormat(&DetailedHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Lock Tooltip"); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Drag"); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Pan View"); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Mouse Wheel"); - MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom"); - MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, DetailedHelp.ppStrings, DetailedHelp.nNumStrings, 0xff777777); - - MicroProfileStringArray DetailedHistoryHelp; - MicroProfileStringArrayClear(&DetailedHistoryHelp); - MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_LEFT); - MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Center View"); - MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_ALT); - MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Zoom to frame"); - MicroProfileDrawFloatWindow(nWidth, 20, DetailedHistoryHelp.ppStrings, DetailedHistoryHelp.nNumStrings, 0xff777777); - - - - } - else if(0 != (S.nDisplay & MP_DRAW_BARS) && S.nBars) - { - MicroProfileStringArray BarHelp; - MicroProfileStringArrayClear(&BarHelp); - MicroProfileStringArrayFormat(&BarHelp, "%s", MICROPROFILE_HELP_LEFT); - MicroProfileStringArrayAddLiteral(&BarHelp, "Toggle Graph"); - MicroProfileStringArrayFormat(&BarHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT); - MicroProfileStringArrayAddLiteral(&BarHelp, "Lock Tooltip"); - MicroProfileStringArrayAddLiteral(&BarHelp, "Drag"); - MicroProfileStringArrayAddLiteral(&BarHelp, "Pan View"); - MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, BarHelp.ppStrings, BarHelp.nNumStrings, 0xff777777); - - } - MicroProfileStringArray Debug; - MicroProfileStringArrayClear(&Debug); - MicroProfileStringArrayAddLiteral(&Debug, "Memory Usage"); - MicroProfileStringArrayFormat(&Debug, "%4.2fmb", S.nMemUsage / (1024.f * 1024.f)); - uint32_t nFrameNext = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY; - MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; - MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext]; - - - MicroProfileStringArrayAddLiteral(&Debug, ""); - MicroProfileStringArrayAddLiteral(&Debug, ""); - MicroProfileStringArrayAddLiteral(&Debug, "Usage"); - MicroProfileStringArrayAddLiteral(&Debug, "markers [frames] "); + MicroProfileStringArrayAddLiteral(&Debug, ""); + MicroProfileStringArrayAddLiteral(&Debug, ""); + MicroProfileStringArrayAddLiteral(&Debug, "Usage"); + MicroProfileStringArrayAddLiteral(&Debug, "markers [frames] "); #if MICROPROFILE_CONTEXT_SWITCH_TRACE - MicroProfileStringArrayAddLiteral(&Debug, "Context Switch"); - MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", S.nContextSwitchUsage, MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE / S.nContextSwitchUsage ); + MicroProfileStringArrayAddLiteral(&Debug, "Context Switch"); + MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", S.nContextSwitchUsage, MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE / S.nContextSwitchUsage ); #endif - for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i) - { - if(pFrameCurrent->nLogStart[i] && S.Pool[i]) - { - uint32_t nEnd = pFrameNext->nLogStart[i]; - uint32_t nStart = pFrameCurrent->nLogStart[i]; - uint32_t nUsage = nStart < nEnd ? (nEnd - nStart) : (nEnd + MICROPROFILE_BUFFER_SIZE - nStart); - uint32_t nFrameSupport = MICROPROFILE_BUFFER_SIZE / nUsage; - MicroProfileStringArrayFormat(&Debug, "%s", &S.Pool[i]->ThreadName[0]); - MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", nUsage, nFrameSupport); - } - } + for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(pFrameCurrent->nLogStart[i] && S.Pool[i]) + { + uint32_t nEnd = pFrameNext->nLogStart[i]; + uint32_t nStart = pFrameCurrent->nLogStart[i]; + uint32_t nUsage = nStart <= nEnd ? (nEnd - nStart) : (nEnd + MICROPROFILE_BUFFER_SIZE - nStart); + uint32_t nFrameSupport = (nUsage == 0) ? MICROPROFILE_BUFFER_SIZE : MICROPROFILE_BUFFER_SIZE / nUsage; + MicroProfileStringArrayFormat(&Debug, "%s", &S.Pool[i]->ThreadName[0]); + MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", nUsage, nFrameSupport); + } + } - MicroProfileDrawFloatWindow(0, nHeight-10, Debug.ppStrings, Debug.nNumStrings, 0xff777777); - } + MicroProfileDrawFloatWindow(0, nHeight-10, Debug.ppStrings, Debug.nNumStrings, 0xff777777); + } - if(UI.nActiveMenu == -1 && !bMouseOverGraph) - { - if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN) - { - MicroProfileDrawFloatTooltip(UI.nMouseX, UI.nMouseY, UI.nHoverToken, UI.nHoverTime); - } - else if(S.nContextSwitchHoverThreadAfter != -1 && S.nContextSwitchHoverThreadBefore != -1) - { - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - MicroProfileStringArray ToolTip; - MicroProfileStringArrayClear(&ToolTip); - MicroProfileStringArrayAddLiteral(&ToolTip, "Context Switch"); - MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThread); - MicroProfileStringArrayAddLiteral(&ToolTip, "Before"); - MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadBefore); - MicroProfileStringArrayAddLiteral(&ToolTip, "After"); - MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadAfter); - MicroProfileStringArrayAddLiteral(&ToolTip, "Duration"); - int64_t nDifference = MicroProfileLogTickDifference(S.nContextSwitchHoverTickIn, S.nContextSwitchHoverTickOut); - MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fToMs * nDifference ); - MicroProfileStringArrayAddLiteral(&ToolTip, "CPU"); - MicroProfileStringArrayFormat(&ToolTip, "%d", S.nContextSwitchHoverCpu); - MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, -1); + if(UI.nActiveMenu == (uint32_t)-1 && !bMouseOverGraph) + { + if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN) + { + MicroProfileDrawFloatTooltip(UI.nMouseX, UI.nMouseY, (uint32_t)UI.nHoverToken, UI.nHoverTime); + } + else if(S.nContextSwitchHoverThreadAfter != (uint32_t)-1 && S.nContextSwitchHoverThreadBefore != (uint32_t)-1) + { + float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + MicroProfileStringArray ToolTip; + MicroProfileStringArrayClear(&ToolTip); + MicroProfileStringArrayAddLiteral(&ToolTip, "Context Switch"); + MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThread); + MicroProfileStringArrayAddLiteral(&ToolTip, "Before"); + MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadBefore); + MicroProfileStringArrayAddLiteral(&ToolTip, "After"); + MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadAfter); + MicroProfileStringArrayAddLiteral(&ToolTip, "Duration"); + int64_t nDifference = MicroProfileLogTickDifference(S.nContextSwitchHoverTickIn, S.nContextSwitchHoverTickOut); + MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fToMs * nDifference ); + MicroProfileStringArrayAddLiteral(&ToolTip, "CPU"); + MicroProfileStringArrayFormat(&ToolTip, "%d", S.nContextSwitchHoverCpu); + MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, -1); - } - else if(UI.nHoverFrame != -1) - { - uint32_t nNextFrame = (UI.nHoverFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY; - int64_t nTick = S.Frames[UI.nHoverFrame].nFrameStartCpu; - int64_t nTickNext = S.Frames[nNextFrame].nFrameStartCpu; - int64_t nTickGpu = S.Frames[UI.nHoverFrame].nFrameStartGpu; - int64_t nTickNextGpu = S.Frames[nNextFrame].nFrameStartGpu; + } + else if(UI.nHoverFrame != -1) + { + uint32_t nNextFrame = (UI.nHoverFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY; + int64_t nTick = S.Frames[UI.nHoverFrame].nFrameStartCpu; + int64_t nTickNext = S.Frames[nNextFrame].nFrameStartCpu; + int64_t nTickGpu = S.Frames[UI.nHoverFrame].nFrameStartGpu; + int64_t nTickNextGpu = S.Frames[nNextFrame].nFrameStartGpu; + + float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + float fMs = fToMs * (nTickNext - nTick); + float fMsGpu = fToMsGpu * (nTickNextGpu - nTickGpu); + MicroProfileStringArray ToolTip; + MicroProfileStringArrayClear(&ToolTip); + MicroProfileStringArrayFormat(&ToolTip, "Frame %d", UI.nHoverFrame); + #if MICROPROFILE_DEBUG + MicroProfileStringArrayFormat(&ToolTip, "%p", &S.Frames[UI.nHoverFrame]); + #else + MicroProfileStringArrayAddLiteral(&ToolTip, ""); + #endif + MicroProfileStringArrayAddLiteral(&ToolTip, "CPU Time"); + MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMs); + MicroProfileStringArrayAddLiteral(&ToolTip, "GPU Time"); + MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMsGpu); + #if MICROPROFILE_DEBUG + for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(S.Frames[UI.nHoverFrame].nLogStart[i]) + { + MicroProfileStringArrayFormat(&ToolTip, "%d", i); + MicroProfileStringArrayFormat(&ToolTip, "%d", S.Frames[UI.nHoverFrame].nLogStart[i]); + } + } + #endif + MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, -1); + } + if(UI.nMouseLeft) + { + if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN) + MicroProfileToggleGraph(UI.nHoverToken); + } + } + } - float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); - float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); - float fMs = fToMs * (nTickNext - nTick); - float fMsGpu = fToMsGpu * (nTickNextGpu - nTickGpu); - MicroProfileStringArray ToolTip; - MicroProfileStringArrayClear(&ToolTip); - MicroProfileStringArrayFormat(&ToolTip, "Frame %d", UI.nHoverFrame); - #if MICROPROFILE_DEBUG - MicroProfileStringArrayFormat(&ToolTip, "%p", &S.Frames[UI.nHoverFrame]); - #else - MicroProfileStringArrayAddLiteral(&ToolTip, ""); - #endif - MicroProfileStringArrayAddLiteral(&ToolTip, "CPU Time"); - MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMs); - MicroProfileStringArrayAddLiteral(&ToolTip, "GPU Time"); - MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMsGpu); - #if MICROPROFILE_DEBUG - for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i) - { - if(S.Frames[UI.nHoverFrame].nLogStart[i]) - { - MicroProfileStringArrayFormat(&ToolTip, "%d", i); - MicroProfileStringArrayFormat(&ToolTip, "%d", S.Frames[UI.nHoverFrame].nLogStart[i]); - } - } - #endif - MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, -1); - } - if(UI.nMouseLeft) - { - if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN) - MicroProfileToggleGraph(UI.nHoverToken); - } - } - } #if MICROPROFILE_DRAWCURSOR - { - float fCursor[8] = - { - MicroProfileMax(0, (int)UI.nMouseX-3), UI.nMouseY, - MicroProfileMin(nWidth, UI.nMouseX+3), UI.nMouseY, - UI.nMouseX, MicroProfileMax((int)UI.nMouseY-3, 0), - UI.nMouseX, MicroProfileMin(nHeight, UI.nMouseY+3), - }; - MicroProfileDrawLine2D(2, &fCursor[0], 0xff00ff00); - MicroProfileDrawLine2D(2, &fCursor[4], 0xff00ff00); - } + { + float fCursor[8] = + { + float(MicroProfileMax(0, (int)UI.nMouseX-3)), float(UI.nMouseY), + float(MicroProfileMin(nWidth, UI.nMouseX+3)), float(UI.nMouseY), + float(UI.nMouseX), float(MicroProfileMax((int)UI.nMouseY-3, 0)), + float(UI.nMouseX), float(MicroProfileMin(nHeight, UI.nMouseY+3)), + }; + MicroProfileDrawLine2D(2, &fCursor[0], 0xff00ff00); + MicroProfileDrawLine2D(2, &fCursor[4], 0xff00ff00); + } #endif - m.unlock(); - } - UI.nMouseLeft = UI.nMouseRight = 0; - UI.nMouseLeftMod = UI.nMouseRightMod = 0; - UI.nMouseWheelDelta = 0; - if(S.nOverflow) - S.nOverflow--; + m.unlock(); + } + else if(UI.nCustomActive != (uint32_t)-1) + { + std::recursive_mutex& m = MicroProfileGetMutex(); + m.lock(); + MicroProfileDrawGraph(nWidth, nHeight); + MicroProfileDrawCustom(nWidth, nHeight); + m.unlock(); - UI.fDetailedOffset = UI.fDetailedOffset + (UI.fDetailedOffsetTarget - UI.fDetailedOffset) * MICROPROFILE_ANIM_DELAY_PRC; - UI.fDetailedRange = UI.fDetailedRange + (UI.fDetailedRangeTarget - UI.fDetailedRange) * MICROPROFILE_ANIM_DELAY_PRC; + } + UI.nMouseLeft = UI.nMouseRight = 0; + UI.nMouseLeftMod = UI.nMouseRightMod = 0; + UI.nMouseWheelDelta = 0; + if(S.nOverflow) + S.nOverflow--; + + UI.fDetailedOffset = UI.fDetailedOffset + (UI.fDetailedOffsetTarget - UI.fDetailedOffset) * MICROPROFILE_ANIM_DELAY_PRC; + UI.fDetailedRange = UI.fDetailedRange + (UI.fDetailedRangeTarget - UI.fDetailedRange) * MICROPROFILE_ANIM_DELAY_PRC; } bool MicroProfileIsDrawing() { - MicroProfile& S = *MicroProfileGet(); - return S.nDisplay != 0; + MicroProfile& S = *MicroProfileGet(); + return S.nDisplay != 0; } void MicroProfileToggleGraph(MicroProfileToken nToken) { - MicroProfile& S = *MicroProfileGet(); - uint32_t nTimerId = MicroProfileGetTimerIndex(nToken); - nToken &= 0xffff; - int32_t nMinSort = 0x7fffffff; - int32_t nFreeIndex = -1; - int32_t nMinIndex = 0; - int32_t nMaxSort = 0x80000000; - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - if(S.Graph[i].nToken == MICROPROFILE_INVALID_TOKEN) - nFreeIndex = i; - if(S.Graph[i].nToken == nToken) - { - S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; - S.TimerInfo[nTimerId].bGraph = false; - return; - } - if(S.Graph[i].nKey < nMinSort) - { - nMinSort = S.Graph[i].nKey; - nMinIndex = i; - } - if(S.Graph[i].nKey > nMaxSort) - { - nMaxSort = S.Graph[i].nKey; - } - } - int nIndex = nFreeIndex > -1 ? nFreeIndex : nMinIndex; - if (nFreeIndex == -1) - { - uint32_t idx = MicroProfileGetTimerIndex(S.Graph[nIndex].nToken); - S.TimerInfo[idx].bGraph = false; - } - S.Graph[nIndex].nToken = nToken; - S.Graph[nIndex].nKey = nMaxSort+1; - memset(&S.Graph[nIndex].nHistory[0], 0, sizeof(S.Graph[nIndex].nHistory)); - S.TimerInfo[nTimerId].bGraph = true; + MicroProfile& S = *MicroProfileGet(); + uint32_t nTimerId = MicroProfileGetTimerIndex(nToken); + nToken &= 0xffff; + int32_t nMinSort = 0x7fffffff; + int32_t nFreeIndex = -1; + int32_t nMinIndex = 0; + int32_t nMaxSort = 0x80000000; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + if(S.Graph[i].nToken == MICROPROFILE_INVALID_TOKEN) + nFreeIndex = i; + if(S.Graph[i].nToken == nToken) + { + S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; + S.TimerInfo[nTimerId].bGraph = false; + return; + } + if(S.Graph[i].nKey < nMinSort) + { + nMinSort = S.Graph[i].nKey; + nMinIndex = i; + } + if(S.Graph[i].nKey > nMaxSort) + { + nMaxSort = S.Graph[i].nKey; + } + } + int nIndex = nFreeIndex > -1 ? nFreeIndex : nMinIndex; + if (nFreeIndex == -1) + { + uint32_t idx = MicroProfileGetTimerIndex(S.Graph[nIndex].nToken); + S.TimerInfo[idx].bGraph = false; + } + S.Graph[nIndex].nToken = nToken; + S.Graph[nIndex].nKey = nMaxSort+1; + memset(&S.Graph[nIndex].nHistory[0], 0, sizeof(S.Graph[nIndex].nHistory)); + S.TimerInfo[nTimerId].bGraph = true; } void MicroProfileMousePosition(uint32_t nX, uint32_t nY, int nWheelDelta) { - UI.nMouseX = nX; - UI.nMouseY = nY; - UI.nMouseWheelDelta = nWheelDelta; + UI.nMouseX = nX; + UI.nMouseY = nY; + UI.nMouseWheelDelta = nWheelDelta; } void MicroProfileModKey(uint32_t nKeyState) { - UI.nModDown = nKeyState ? 1 : 0; + UI.nModDown = nKeyState ? 1 : 0; } void MicroProfileClearGraph() { - MicroProfile& S = *MicroProfileGet(); - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - if(S.Graph[i].nToken != 0) - { - S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; - } - } + MicroProfile& S = *MicroProfileGet(); + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + if(S.Graph[i].nToken != 0) + { + S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; + } + } } void MicroProfileMouseButton(uint32_t nLeft, uint32_t nRight) { - if(0 == nLeft && UI.nMouseDownLeft) - { - if(UI.nModDown) - UI.nMouseLeftMod = 1; - else - UI.nMouseLeft = 1; - } + bool bCanRelease = abs((int)(UI.nMouseDownX - UI.nMouseX)) + abs((int)(UI.nMouseDownY - UI.nMouseY)) < 3; - if(0 == nRight && UI.nMouseDownRight) - { - if(UI.nModDown) - UI.nMouseRightMod = 1; - else - UI.nMouseRight = 1; - } + if(0 == nLeft && UI.nMouseDownLeft && bCanRelease) + { + if(UI.nModDown) + UI.nMouseLeftMod = 1; + else + UI.nMouseLeft = 1; + } - UI.nMouseDownLeft = nLeft; - UI.nMouseDownRight = nRight; + if(0 == nRight && UI.nMouseDownRight && bCanRelease) + { + if(UI.nModDown) + UI.nMouseRightMod = 1; + else + UI.nMouseRight = 1; + } + if((nLeft || nRight) && !(UI.nMouseDownLeft || UI.nMouseDownRight)) + { + UI.nMouseDownX = UI.nMouseX; + UI.nMouseDownY = UI.nMouseY; + } + UI.nMouseDownLeft = nLeft; + UI.nMouseDownRight = nRight; + } void MicroProfileDrawLineVertical(int nX, int nTop, int nBottom, uint32_t nColor) { - MicroProfileDrawBox(nX, nTop, nX + 1, nBottom, nColor); + MicroProfileDrawBox(nX, nTop, nX + 1, nBottom, nColor); } void MicroProfileDrawLineHorizontal(int nLeft, int nRight, int nY, uint32_t nColor) { - MicroProfileDrawBox(nLeft, nY, nRight, nY + 1, nColor); + MicroProfileDrawBox(nLeft, nY, nRight, nY + 1, nColor); } @@ -2553,107 +3499,107 @@ void MicroProfileDrawLineHorizontal(int nLeft, int nRight, int nY, uint32_t nCol #define MICROPROFILE_PRESET_HEADER_VERSION 0x00000102 struct MicroProfilePresetHeader { - uint32_t nMagic; - uint32_t nVersion; - //groups, threads, aggregate, reference frame, graphs timers - uint32_t nGroups[MICROPROFILE_MAX_GROUPS]; - uint32_t nThreads[MICROPROFILE_MAX_THREADS]; - uint32_t nGraphName[MICROPROFILE_MAX_GRAPHS]; - uint32_t nGraphGroupName[MICROPROFILE_MAX_GRAPHS]; - uint32_t nAllGroupsWanted; - uint32_t nAllThreadsWanted; - uint32_t nAggregateFlip; - float fReferenceTime; - uint32_t nBars; - uint32_t nDisplay; - uint32_t nOpacityBackground; - uint32_t nOpacityForeground; - uint32_t nShowSpikes; + uint32_t nMagic; + uint32_t nVersion; + //groups, threads, aggregate, reference frame, graphs timers + uint32_t nGroups[MICROPROFILE_MAX_GROUPS]; + uint32_t nThreads[MICROPROFILE_MAX_THREADS]; + uint32_t nGraphName[MICROPROFILE_MAX_GRAPHS]; + uint32_t nGraphGroupName[MICROPROFILE_MAX_GRAPHS]; + uint32_t nAllGroupsWanted; + uint32_t nAllThreadsWanted; + uint32_t nAggregateFlip; + float fReferenceTime; + uint32_t nBars; + uint32_t nDisplay; + uint32_t nOpacityBackground; + uint32_t nOpacityForeground; + uint32_t nShowSpikes; }; #ifndef MICROPROFILE_PRESET_FILENAME_FUNC #define MICROPROFILE_PRESET_FILENAME_FUNC MicroProfilePresetFilename static const char* MicroProfilePresetFilename(const char* pSuffix) { - static char filename[512]; - snprintf(filename, sizeof(filename)-1, ".microprofilepreset.%s", pSuffix); - return filename; + static char filename[512]; + snprintf(filename, sizeof(filename)-1, ".microprofilepreset.%s", pSuffix); + return filename; } #endif void MicroProfileSavePreset(const char* pPresetName) { - std::lock_guard Lock(MicroProfileGetMutex()); - FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pPresetName), "wb"); - if(!F) return; + std::lock_guard Lock(MicroProfileGetMutex()); + FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pPresetName), "wb"); + if(!F) return; - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); - MicroProfilePresetHeader Header; - memset(&Header, 0, sizeof(Header)); - Header.nAggregateFlip = S.nAggregateFlip; - Header.nBars = S.nBars; - Header.fReferenceTime = S.fReferenceTime; - Header.nAllGroupsWanted = S.nAllGroupsWanted; - Header.nAllThreadsWanted = S.nAllThreadsWanted; - Header.nMagic = MICROPROFILE_PRESET_HEADER_MAGIC; - Header.nVersion = MICROPROFILE_PRESET_HEADER_VERSION; - Header.nDisplay = S.nDisplay; - Header.nOpacityBackground = UI.nOpacityBackground; - Header.nOpacityForeground = UI.nOpacityForeground; - Header.nShowSpikes = UI.bShowSpikes ? 1 : 0; - fwrite(&Header, sizeof(Header), 1, F); - uint64_t nMask = 1; - for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) - { - if(S.nActiveGroupWanted & nMask) - { - uint32_t offset = ftell(F); - const char* pName = S.GroupInfo[i].pName; - int nLen = (int)strlen(pName)+1; - fwrite(pName, nLen, 1, F); - Header.nGroups[i] = offset; - } - nMask <<= 1; - } - for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) - { - MicroProfileThreadLog* pLog = S.Pool[i]; - if(pLog && S.nThreadActive[i]) - { - uint32_t nOffset = ftell(F); - const char* pName = &pLog->ThreadName[0]; - int nLen = (int)strlen(pName)+1; - fwrite(pName, nLen, 1, F); - Header.nThreads[i] = nOffset; - } - } - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - MicroProfileToken nToken = S.Graph[i].nToken; - if(nToken != MICROPROFILE_INVALID_TOKEN) - { - uint32_t nGroupIndex = MicroProfileGetGroupIndex(nToken); - uint32_t nTimerIndex = MicroProfileGetTimerIndex(nToken); - const char* pGroupName = S.GroupInfo[nGroupIndex].pName; - const char* pTimerName = S.TimerInfo[nTimerIndex].pName; - MP_ASSERT(pGroupName); - MP_ASSERT(pTimerName); - int nGroupLen = (int)strlen(pGroupName)+1; - int nTimerLen = (int)strlen(pTimerName)+1; + MicroProfilePresetHeader Header; + memset(&Header, 0, sizeof(Header)); + Header.nAggregateFlip = S.nAggregateFlip; + Header.nBars = S.nBars; + Header.fReferenceTime = S.fReferenceTime; + Header.nAllGroupsWanted = S.nAllGroupsWanted; + Header.nAllThreadsWanted = S.nAllThreadsWanted; + Header.nMagic = MICROPROFILE_PRESET_HEADER_MAGIC; + Header.nVersion = MICROPROFILE_PRESET_HEADER_VERSION; + Header.nDisplay = S.nDisplay; + Header.nOpacityBackground = UI.nOpacityBackground; + Header.nOpacityForeground = UI.nOpacityForeground; + Header.nShowSpikes = UI.bShowSpikes ? 1 : 0; + fwrite(&Header, sizeof(Header), 1, F); + uint64_t nMask = 1; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + if(S.nActiveGroupWanted & nMask) + { + uint32_t offset = ftell(F); + const char* pName = S.GroupInfo[i].pName; + int nLen = (int)strlen(pName)+1; + fwrite(pName, nLen, 1, F); + Header.nGroups[i] = offset; + } + nMask <<= 1; + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(pLog && S.nThreadActive[i]) + { + uint32_t nOffset = ftell(F); + const char* pName = &pLog->ThreadName[0]; + int nLen = (int)strlen(pName)+1; + fwrite(pName, nLen, 1, F); + Header.nThreads[i] = nOffset; + } + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + MicroProfileToken nToken = S.Graph[i].nToken; + if(nToken != MICROPROFILE_INVALID_TOKEN) + { + uint32_t nGroupIndex = MicroProfileGetGroupIndex(nToken); + uint32_t nTimerIndex = MicroProfileGetTimerIndex(nToken); + const char* pGroupName = S.GroupInfo[nGroupIndex].pName; + const char* pTimerName = S.TimerInfo[nTimerIndex].pName; + MP_ASSERT(pGroupName); + MP_ASSERT(pTimerName); + int nGroupLen = (int)strlen(pGroupName)+1; + int nTimerLen = (int)strlen(pTimerName)+1; - uint32_t nOffsetGroup = ftell(F); - fwrite(pGroupName, nGroupLen, 1, F); - uint32_t nOffsetTimer = ftell(F); - fwrite(pTimerName, nTimerLen, 1, F); - Header.nGraphName[i] = nOffsetTimer; - Header.nGraphGroupName[i] = nOffsetGroup; - } - } - fseek(F, 0, SEEK_SET); - fwrite(&Header, sizeof(Header), 1, F); + uint32_t nOffsetGroup = ftell(F); + fwrite(pGroupName, nGroupLen, 1, F); + uint32_t nOffsetTimer = ftell(F); + fwrite(pTimerName, nTimerLen, 1, F); + Header.nGraphName[i] = nOffsetTimer; + Header.nGraphGroupName[i] = nOffsetGroup; + } + } + fseek(F, 0, SEEK_SET); + fwrite(&Header, sizeof(Header), 1, F); - fclose(F); + fclose(F); } @@ -2661,100 +3607,221 @@ void MicroProfileSavePreset(const char* pPresetName) void MicroProfileLoadPreset(const char* pSuffix) { - std::lock_guard Lock(MicroProfileGetMutex()); - FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pSuffix), "rb"); - if(!F) - { - return; - } - fseek(F, 0, SEEK_END); - int nSize = ftell(F); - char* const pBuffer = (char*)alloca(nSize); - fseek(F, 0, SEEK_SET); - int nRead = (int)fread(pBuffer, nSize, 1, F); - fclose(F); - if(1 != nRead) - return; + std::lock_guard Lock(MicroProfileGetMutex()); + FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pSuffix), "rb"); + if(!F) + { + return; + } + fseek(F, 0, SEEK_END); + int nSize = ftell(F); + char* const pBuffer = (char*)alloca(nSize); + fseek(F, 0, SEEK_SET); + int nRead = (int)fread(pBuffer, nSize, 1, F); + fclose(F); + if(1 != nRead) + return; - MicroProfile& S = *MicroProfileGet(); + MicroProfile& S = *MicroProfileGet(); + + MicroProfilePresetHeader& Header = *(MicroProfilePresetHeader*)pBuffer; - MicroProfilePresetHeader& Header = *(MicroProfilePresetHeader*)pBuffer; + if(Header.nMagic != MICROPROFILE_PRESET_HEADER_MAGIC || Header.nVersion != MICROPROFILE_PRESET_HEADER_VERSION) + { + return; + } - if(Header.nMagic != MICROPROFILE_PRESET_HEADER_MAGIC || Header.nVersion != MICROPROFILE_PRESET_HEADER_VERSION) - { - return; - } + S.nAggregateFlip = Header.nAggregateFlip; + S.nBars = Header.nBars; + S.fReferenceTime = Header.fReferenceTime; + S.fRcpReferenceTime = 1.f / Header.fReferenceTime; + S.nAllGroupsWanted = Header.nAllGroupsWanted; + S.nAllThreadsWanted = Header.nAllThreadsWanted; + S.nDisplay = Header.nDisplay; + S.nActiveGroupWanted = 0; + UI.nOpacityBackground = Header.nOpacityBackground; + UI.nOpacityForeground = Header.nOpacityForeground; + UI.bShowSpikes = Header.nShowSpikes == 1; - S.nAggregateFlip = Header.nAggregateFlip; - S.nBars = Header.nBars; - S.fReferenceTime = Header.fReferenceTime; - S.fRcpReferenceTime = 1.f / Header.fReferenceTime; - S.nAllGroupsWanted = Header.nAllGroupsWanted; - S.nAllThreadsWanted = Header.nAllThreadsWanted; - S.nDisplay = Header.nDisplay; - S.nActiveGroupWanted = 0; - UI.nOpacityBackground = Header.nOpacityBackground; - UI.nOpacityForeground = Header.nOpacityForeground; - UI.bShowSpikes = Header.nShowSpikes == 1; + memset(&S.nThreadActive[0], 0, sizeof(S.nThreadActive)); - memset(&S.nThreadActive[0], 0, sizeof(S.nThreadActive)); - - for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) - { - if(Header.nGroups[i]) - { - const char* pGroupName = pBuffer + Header.nGroups[i]; - for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) - { - if(0 == MP_STRCASECMP(pGroupName, S.GroupInfo[j].pName)) - { - S.nActiveGroupWanted |= (1ll << j); - } - } - } - } - for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) - { - if(Header.nThreads[i]) - { - const char* pThreadName = pBuffer + Header.nThreads[i]; - for(uint32_t j = 0; j < MICROPROFILE_MAX_THREADS; ++j) - { - MicroProfileThreadLog* pLog = S.Pool[j]; - if(pLog && 0 == MP_STRCASECMP(pThreadName, &pLog->ThreadName[0])) - { - S.nThreadActive[j] = 1; - } - } - } - } - for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) - { - MicroProfileToken nPrevToken = S.Graph[i].nToken; - S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; - if(Header.nGraphName[i] && Header.nGraphGroupName[i]) - { - const char* pGraphName = pBuffer + Header.nGraphName[i]; - const char* pGraphGroupName = pBuffer + Header.nGraphGroupName[i]; - for(uint32_t j = 0; j < S.nTotalTimers; ++j) - { - uint64_t nGroupIndex = S.TimerInfo[j].nGroupIndex; - if(0 == MP_STRCASECMP(pGraphName, S.TimerInfo[j].pName) && 0 == MP_STRCASECMP(pGraphGroupName, S.GroupInfo[nGroupIndex].pName)) - { - MicroProfileToken nToken = MicroProfileMakeToken(1ll << nGroupIndex, (uint16_t)j); - S.Graph[i].nToken = nToken; // note: group index is stored here but is checked without in MicroProfileToggleGraph()! - S.TimerInfo[j].bGraph = true; - if(nToken != nPrevToken) - { - memset(&S.Graph[i].nHistory, 0, sizeof(S.Graph[i].nHistory)); - } - break; - } - } - } - } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + if(Header.nGroups[i]) + { + const char* pGroupName = pBuffer + Header.nGroups[i]; + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + if(0 == MP_STRCASECMP(pGroupName, S.GroupInfo[j].pName)) + { + S.nActiveGroupWanted |= (1ll << j); + } + } + } + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(Header.nThreads[i]) + { + const char* pThreadName = pBuffer + Header.nThreads[i]; + for(uint32_t j = 0; j < MICROPROFILE_MAX_THREADS; ++j) + { + MicroProfileThreadLog* pLog = S.Pool[j]; + if(pLog && 0 == MP_STRCASECMP(pThreadName, &pLog->ThreadName[0])) + { + S.nThreadActive[j] = 1; + } + } + } + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + MicroProfileToken nPrevToken = S.Graph[i].nToken; + S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; + if(Header.nGraphName[i] && Header.nGraphGroupName[i]) + { + const char* pGraphName = pBuffer + Header.nGraphName[i]; + const char* pGraphGroupName = pBuffer + Header.nGraphGroupName[i]; + for(uint32_t j = 0; j < S.nTotalTimers; ++j) + { + uint64_t nGroupIndex = S.TimerInfo[j].nGroupIndex; + if(0 == MP_STRCASECMP(pGraphName, S.TimerInfo[j].pName) && 0 == MP_STRCASECMP(pGraphGroupName, S.GroupInfo[nGroupIndex].pName)) + { + MicroProfileToken nToken = MicroProfileMakeToken(1ll << nGroupIndex, (uint16_t)j); + S.Graph[i].nToken = nToken; // note: group index is stored here but is checked without in MicroProfileToggleGraph()! + S.TimerInfo[j].bGraph = true; + if(nToken != nPrevToken) + { + memset(&S.Graph[i].nHistory, 0, sizeof(S.Graph[i].nHistory)); + } + break; + } + } + } + } } +uint32_t MicroProfileCustomGroupFind(const char* pCustomName) +{ + for(uint32_t i = 0; i < UI.nCustomCount; ++i) + { + if(!MP_STRCASECMP(pCustomName, UI.Custom[i].pName)) + { + return i; + } + } + return (uint32_t)-1; +} + +uint32_t MicroProfileCustomGroup(const char* pCustomName) +{ + for(uint32_t i = 0; i < UI.nCustomCount; ++i) + { + if(!MP_STRCASECMP(pCustomName, UI.Custom[i].pName)) + { + return i; + } + } + MP_ASSERT(UI.nCustomCount < MICROPROFILE_CUSTOM_MAX); + uint32_t nIndex = UI.nCustomCount; + UI.nCustomCount++; + memset(&UI.Custom[nIndex], 0, sizeof(UI.Custom[nIndex])); + uint32_t nLen = (uint32_t)strlen(pCustomName); + if(nLen > MICROPROFILE_NAME_MAX_LEN-1) + nLen = MICROPROFILE_NAME_MAX_LEN-1; + memcpy(&UI.Custom[nIndex].pName[0], pCustomName, nLen); + UI.Custom[nIndex].pName[nLen] = '\0'; + return nIndex; +} +void MicroProfileCustomGroup(const char* pCustomName, uint32_t nMaxTimers, uint32_t nAggregateFlip, float fReferenceTime, uint32_t nFlags) +{ + uint32_t nIndex = MicroProfileCustomGroup(pCustomName); + MP_ASSERT(UI.Custom[nIndex].pTimers == 0);//only call once! + UI.Custom[nIndex].pTimers = &UI.CustomTimer[UI.nCustomTimerCount]; + UI.Custom[nIndex].nMaxTimers = nMaxTimers; + UI.Custom[nIndex].fReference = fReferenceTime; + UI.nCustomTimerCount += nMaxTimers; + MP_ASSERT(UI.nCustomTimerCount <= MICROPROFILE_CUSTOM_MAX_TIMERS); //bump MICROPROFILE_CUSTOM_MAX_TIMERS + UI.Custom[nIndex].nFlags = nFlags; + UI.Custom[nIndex].nAggregateFlip = nAggregateFlip; +} + +void MicroProfileCustomGroupEnable(uint32_t nIndex) +{ + if(nIndex < UI.nCustomCount) + { + MicroProfile& S = *MicroProfileGet(); + S.nForceGroupUI = UI.Custom[nIndex].nGroupMask; + MicroProfileSetAggregateFrames(UI.Custom[nIndex].nAggregateFlip); + S.fReferenceTime = UI.Custom[nIndex].fReference; + S.fRcpReferenceTime = 1.f / UI.Custom[nIndex].fReference; + UI.nCustomActive = nIndex; + + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) + { + uint32_t nTimerId = MicroProfileGetTimerIndex(S.Graph[i].nToken); + S.TimerInfo[nTimerId].bGraph = false; + S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; + } + } + + for(uint32_t i = 0; i < UI.Custom[nIndex].nNumTimers; ++i) + { + if(i == MICROPROFILE_MAX_GRAPHS) + { + break; + } + S.Graph[i].nToken = UI.Custom[nIndex].pTimers[i]; + S.Graph[i].nKey = i; + uint32_t nTimerId = MicroProfileGetTimerIndex(S.Graph[i].nToken); + S.TimerInfo[nTimerId].bGraph = true; + } + } +} + +void MicroProfileCustomGroupToggle(const char* pCustomName) +{ + uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName); + if(nIndex == (uint32_t)-1 || nIndex == UI.nCustomActive) + { + MicroProfileCustomGroupDisable(); + } + else + { + MicroProfileCustomGroupEnable(nIndex); + } +} + +void MicroProfileCustomGroupEnable(const char* pCustomName) +{ + uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName); + MicroProfileCustomGroupEnable(nIndex); +} +void MicroProfileCustomGroupDisable() +{ + MicroProfile& S = *MicroProfileGet(); + S.nForceGroupUI = 0; + UI.nCustomActive = (uint32_t)-1; +} + +void MicroProfileCustomGroupAddTimer(const char* pCustomName, const char* pGroup, const char* pTimer) +{ + uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName); + if((uint32_t)-1 == nIndex) + { + return; + } + uint32_t nTimerIndex = UI.Custom[nIndex].nNumTimers; + MP_ASSERT(nTimerIndex < UI.Custom[nIndex].nMaxTimers); + uint64_t nToken = MicroProfileFindToken(pGroup, pTimer); + MP_ASSERT(nToken != MICROPROFILE_INVALID_TOKEN); //Timer must be registered first. + UI.Custom[nIndex].pTimers[nTimerIndex] = nToken; + uint16_t nGroup = MicroProfileGetGroupIndex(nToken); + UI.Custom[nIndex].nGroupMask |= (1ll << nGroup); + UI.Custom[nIndex].nNumTimers++; +} #undef UI