microprofileui.h 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. #pragma once
  2. // This is free and unencumbered software released into the public domain.
  3. // Anyone is free to copy, modify, publish, use, compile, sell, or
  4. // distribute this software, either in source code form or as a compiled
  5. // binary, for any purpose, commercial or non-commercial, and by any
  6. // means.
  7. // In jurisdictions that recognize copyright laws, the author or authors
  8. // of this software dedicate any and all copyright interest in the
  9. // software to the public domain. We make this dedication for the benefit
  10. // of the public at large and to the detriment of our heirs and
  11. // successors. We intend this dedication to be an overt act of
  12. // relinquishment in perpetuity of all present and future rights to this
  13. // software under copyright law.
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. // OTHER DEALINGS IN THE SOFTWARE.
  21. // For more information, please refer to <http://unlicense.org/>
  22. //
  23. // ***********************************************************************
  24. //
  25. //
  26. //
  27. #ifndef MICROPROFILE_ENABLED
  28. #error "microprofile.h must be included before including microprofileui.h"
  29. #endif
  30. #ifndef MICROPROFILEUI_ENABLED
  31. #define MICROPROFILEUI_ENABLED MICROPROFILE_ENABLED
  32. #endif
  33. #ifndef MICROPROFILEUI_API
  34. #define MICROPROFILEUI_API
  35. #endif
  36. #if 0 == MICROPROFILEUI_ENABLED
  37. #define MicroProfileMouseButton(foo, bar) do{}while(0)
  38. #define MicroProfileMousePosition(foo, bar, z) do{}while(0)
  39. #define MicroProfileModKey(key) do{}while(0)
  40. #define MicroProfileDraw(foo, bar) do{}while(0)
  41. #define MicroProfileIsDrawing() 0
  42. #define MicroProfileToggleDisplayMode() do{}while(0)
  43. #define MicroProfileSetDisplayMode(f) do{}while(0)
  44. #else
  45. #ifndef MICROPROFILE_DRAWCURSOR
  46. #define MICROPROFILE_DRAWCURSOR 0
  47. #endif
  48. #ifndef MICROPROFILE_DETAILED_BAR_NAMES
  49. #define MICROPROFILE_DETAILED_BAR_NAMES 1
  50. #endif
  51. #ifndef MICROPROFILE_TEXT_WIDTH
  52. #define MICROPROFILE_TEXT_WIDTH 5
  53. #endif
  54. #ifndef MICROPROFILE_TEXT_HEIGHT
  55. #define MICROPROFILE_TEXT_HEIGHT 8
  56. #endif
  57. #ifndef MICROPROFILE_DETAILED_BAR_HEIGHT
  58. #define MICROPROFILE_DETAILED_BAR_HEIGHT 12
  59. #endif
  60. #ifndef MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT
  61. #define MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT 7
  62. #endif
  63. #ifndef MICROPROFILE_GRAPH_WIDTH
  64. #define MICROPROFILE_GRAPH_WIDTH 256
  65. #endif
  66. #ifndef MICROPROFILE_GRAPH_HEIGHT
  67. #define MICROPROFILE_GRAPH_HEIGHT 256
  68. #endif
  69. #ifndef MICROPROFILE_BORDER_SIZE
  70. #define MICROPROFILE_BORDER_SIZE 1
  71. #endif
  72. #ifndef MICROPROFILE_HELP_LEFT
  73. #define MICROPROFILE_HELP_LEFT "Left-Click"
  74. #endif
  75. #ifndef MICROPROFILE_HELP_ALT
  76. #define MICROPROFILE_HELP_ALT "Alt-Click"
  77. #endif
  78. #ifndef MICROPROFILE_HELP_MOD
  79. #define MICROPROFILE_HELP_MOD "Mod"
  80. #endif
  81. #ifndef MICROPROFILE_BAR_WIDTH
  82. #define MICROPROFILE_BAR_WIDTH 100
  83. #endif
  84. #ifndef MICROPROFILE_CUSTOM_MAX
  85. #define MICROPROFILE_CUSTOM_MAX 8
  86. #endif
  87. #ifndef MICROPROFILE_CUSTOM_MAX_TIMERS
  88. #define MICROPROFILE_CUSTOM_MAX_TIMERS 64
  89. #endif
  90. #ifndef MICROPROFILE_CUSTOM_PADDING
  91. #define MICROPROFILE_CUSTOM_PADDING 12
  92. #endif
  93. #define MICROPROFILE_FRAME_HISTORY_HEIGHT 50
  94. #define MICROPROFILE_FRAME_HISTORY_WIDTH 7
  95. #define MICROPROFILE_FRAME_HISTORY_COLOR_CPU 0xffff7f27 //255 127 39
  96. #define MICROPROFILE_FRAME_HISTORY_COLOR_GPU 0xff37a0ee //55 160 238
  97. #define MICROPROFILE_FRAME_HISTORY_COLOR_HIGHTLIGHT 0x7733bb44
  98. #define MICROPROFILE_FRAME_COLOR_HIGHTLIGHT 0x20009900
  99. #define MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU 0x20996600
  100. #define MICROPROFILE_NUM_FRAMES (MICROPROFILE_MAX_FRAME_HISTORY - (MICROPROFILE_GPU_FRAME_DELAY+1))
  101. #define MICROPROFILE_TOOLTIP_MAX_STRINGS (32 + MICROPROFILE_MAX_GROUPS*2)
  102. #define MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE (4*1024)
  103. #define MICROPROFILE_TOOLTIP_MAX_LOCKED 3
  104. enum
  105. {
  106. MICROPROFILE_CUSTOM_BARS = 0x1,
  107. MICROPROFILE_CUSTOM_BAR_SOURCE_MAX = 0x2,
  108. MICROPROFILE_CUSTOM_BAR_SOURCE_AVG = 0,
  109. MICROPROFILE_CUSTOM_STACK = 0x4,
  110. MICROPROFILE_CUSTOM_STACK_SOURCE_MAX = 0x8,
  111. MICROPROFILE_CUSTOM_STACK_SOURCE_AVG = 0,
  112. };
  113. MICROPROFILEUI_API void MicroProfileDraw(uint32_t nWidth, uint32_t nHeight); //! call if drawing microprofilers
  114. MICROPROFILEUI_API bool MicroProfileIsDrawing();
  115. MICROPROFILEUI_API void MicroProfileToggleGraph(MicroProfileToken nToken);
  116. MICROPROFILEUI_API bool MicroProfileDrawGraph(uint32_t nScreenWidth, uint32_t nScreenHeight);
  117. MICROPROFILEUI_API void MicroProfileToggleDisplayMode(); //switch between off, bars, detailed
  118. MICROPROFILEUI_API void MicroProfileSetDisplayMode(int); //switch between off, bars, detailed
  119. MICROPROFILEUI_API void MicroProfileClearGraph();
  120. MICROPROFILEUI_API void MicroProfileMousePosition(uint32_t nX, uint32_t nY, int nWheelDelta);
  121. MICROPROFILEUI_API void MicroProfileModKey(uint32_t nKeyState);
  122. MICROPROFILEUI_API void MicroProfileMouseButton(uint32_t nLeft, uint32_t nRight);
  123. MICROPROFILEUI_API void MicroProfileDrawLineVertical(int nX, int nTop, int nBottom, uint32_t nColor);
  124. MICROPROFILEUI_API void MicroProfileDrawLineHorizontal(int nLeft, int nRight, int nY, uint32_t nColor);
  125. MICROPROFILEUI_API void MicroProfileLoadPreset(const char* pSuffix);
  126. MICROPROFILEUI_API void MicroProfileSavePreset(const char* pSuffix);
  127. MICROPROFILEUI_API void MicroProfileDrawText(int nX, int nY, uint32_t nColor, const char* pText, uint32_t nNumCharacters);
  128. MICROPROFILEUI_API void MicroProfileDrawBox(int nX, int nY, int nX1, int nY1, uint32_t nColor, MicroProfileBoxType = MicroProfileBoxTypeFlat);
  129. MICROPROFILEUI_API void MicroProfileDrawLine2D(uint32_t nVertices, float* pVertices, uint32_t nColor);
  130. MICROPROFILEUI_API void MicroProfileDumpTimers();
  131. MICROPROFILEUI_API void MicroProfileInitUI();
  132. MICROPROFILEUI_API void MicroProfileCustomGroupToggle(const char* pCustomName);
  133. MICROPROFILEUI_API void MicroProfileCustomGroupEnable(const char* pCustomName);
  134. MICROPROFILEUI_API void MicroProfileCustomGroupEnable(uint32_t nIndex);
  135. MICROPROFILEUI_API void MicroProfileCustomGroupDisable();
  136. MICROPROFILEUI_API void MicroProfileCustomGroup(const char* pCustomName, uint32_t nMaxTimers, uint32_t nAggregateFlip, float fReferenceTime, uint32_t nFlags);
  137. MICROPROFILEUI_API void MicroProfileCustomGroupAddTimer(const char* pCustomName, const char* pGroup, const char* pTimer);
  138. #ifdef MICROPROFILEUI_IMPL
  139. #ifdef _WIN32
  140. #define snprintf _snprintf
  141. #endif
  142. #include <stdio.h>
  143. #include <stdlib.h>
  144. #include <stdarg.h>
  145. #include <math.h>
  146. #include <algorithm>
  147. MICROPROFILE_DEFINE(g_MicroProfileDetailed, "MicroProfile", "Detailed View", 0x8888000);
  148. MICROPROFILE_DEFINE(g_MicroProfileDrawGraph, "MicroProfile", "Draw Graph", 0xff44ee00);
  149. MICROPROFILE_DEFINE(g_MicroProfileDrawBarView, "MicroProfile", "DrawBarView", 0x00dd77);
  150. MICROPROFILE_DEFINE(g_MicroProfileDraw,"MicroProfile", "Draw", 0x737373);
  151. struct MicroProfileStringArray
  152. {
  153. const char* ppStrings[MICROPROFILE_TOOLTIP_MAX_STRINGS];
  154. char Buffer[MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE];
  155. char* pBufferPos;
  156. uint32_t nNumStrings;
  157. };
  158. struct MicroProfileGroupMenuItem
  159. {
  160. uint32_t nIsCategory;
  161. uint32_t nCategoryIndex;
  162. uint32_t nIndex;
  163. const char* pName;
  164. };
  165. struct MicroProfileCustom
  166. {
  167. char pName[MICROPROFILE_NAME_MAX_LEN];
  168. uint32_t nFlags;
  169. uint32_t nAggregateFlip;
  170. uint32_t nNumTimers;
  171. uint32_t nMaxTimers;
  172. uint64_t nGroupMask;
  173. float fReference;
  174. uint64_t* pTimers;
  175. };
  176. struct SOptionDesc
  177. {
  178. SOptionDesc(){}
  179. SOptionDesc(uint8_t nSubType, uint8_t nIndex, const char* fmt, ...):nSubType(nSubType), nIndex(nIndex)
  180. {
  181. va_list args;
  182. va_start (args, fmt);
  183. vsprintf(Text, fmt, args);
  184. va_end(args);
  185. }
  186. char Text[32];
  187. uint8_t nSubType;
  188. uint8_t nIndex;
  189. bool bSelected;
  190. };
  191. static uint32_t g_MicroProfileAggregatePresets[] = {0, 10, 20, 30, 60, 120};
  192. static float g_MicroProfileReferenceTimePresets[] = {5.f, 10.f, 15.f,20.f, 33.33f, 66.66f, 100.f, 250.f, 500.f, 1000.f};
  193. static uint32_t g_MicroProfileOpacityPresets[] = {0x40, 0x80, 0xc0, 0xff};
  194. static const char* g_MicroProfilePresetNames[] =
  195. {
  196. MICROPROFILE_DEFAULT_PRESET,
  197. "Render",
  198. "GPU",
  199. "Lighting",
  200. "AI",
  201. "Visibility",
  202. "Sound",
  203. };
  204. enum
  205. {
  206. MICROPROFILE_NUM_REFERENCE_PRESETS = sizeof(g_MicroProfileReferenceTimePresets)/sizeof(g_MicroProfileReferenceTimePresets[0]),
  207. MICROPROFILE_NUM_OPACITY_PRESETS = sizeof(g_MicroProfileOpacityPresets)/sizeof(g_MicroProfileOpacityPresets[0]),
  208. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  209. MICROPROFILE_OPTION_SIZE = MICROPROFILE_NUM_REFERENCE_PRESETS + MICROPROFILE_NUM_OPACITY_PRESETS * 2 + 2 + 7,
  210. #else
  211. MICROPROFILE_OPTION_SIZE = MICROPROFILE_NUM_REFERENCE_PRESETS + MICROPROFILE_NUM_OPACITY_PRESETS * 2 + 2 + 3,
  212. #endif
  213. };
  214. struct MicroProfileUI
  215. {
  216. //menu/mouse over stuff
  217. uint64_t nHoverToken;
  218. int64_t nHoverTime;
  219. int nHoverFrame;
  220. #if MICROPROFILE_DEBUG
  221. uint64_t nHoverAddressEnter;
  222. uint64_t nHoverAddressLeave;
  223. #endif
  224. uint32_t nWidth;
  225. uint32_t nHeight;
  226. int nOffsetX;
  227. int nOffsetY;
  228. float fDetailedOffset; //display offset relative to start of latest displayable frame.
  229. float fDetailedRange; //no. of ms to display
  230. float fDetailedOffsetTarget;
  231. float fDetailedRangeTarget;
  232. uint32_t nOpacityBackground;
  233. uint32_t nOpacityForeground;
  234. bool bShowSpikes;
  235. uint32_t nMouseX;
  236. uint32_t nMouseY;
  237. uint32_t nMouseDownX;
  238. uint32_t nMouseDownY;
  239. int nMouseWheelDelta;
  240. uint32_t nMouseDownLeft;
  241. uint32_t nMouseDownRight;
  242. uint32_t nMouseLeft;
  243. uint32_t nMouseRight;
  244. uint32_t nMouseLeftMod;
  245. uint32_t nMouseRightMod;
  246. uint32_t nModDown;
  247. uint32_t nActiveMenu;
  248. MicroProfileLogEntry* pDisplayMouseOver;
  249. int64_t nRangeBegin;
  250. int64_t nRangeEnd;
  251. int64_t nRangeBeginGpu;
  252. int64_t nRangeEndGpu;
  253. uint32_t nRangeBeginIndex;
  254. uint32_t nRangeEndIndex;
  255. MicroProfileThreadLog* pRangeLog;
  256. uint32_t nHoverColor;
  257. uint32_t nHoverColorShared;
  258. MicroProfileStringArray LockedToolTips[MICROPROFILE_TOOLTIP_MAX_LOCKED];
  259. uint32_t nLockedToolTipColor[MICROPROFILE_TOOLTIP_MAX_LOCKED];
  260. int LockedToolTipFront;
  261. MicroProfileGroupMenuItem GroupMenu[MICROPROFILE_MAX_GROUPS + MICROPROFILE_MAX_CATEGORIES];
  262. uint32_t GroupMenuCount;
  263. uint32_t nCustomActive;
  264. uint32_t nCustomTimerCount;
  265. uint32_t nCustomCount;
  266. MicroProfileCustom Custom[MICROPROFILE_CUSTOM_MAX];
  267. uint64_t CustomTimer[MICROPROFILE_CUSTOM_MAX_TIMERS];
  268. SOptionDesc Options[MICROPROFILE_OPTION_SIZE];
  269. };
  270. MicroProfileUI g_MicroProfileUI;
  271. #define UI g_MicroProfileUI
  272. static uint32_t g_nMicroProfileBackColors[2] = { 0x474747, 0x313131 };
  273. #define MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS 16
  274. static uint32_t g_nMicroProfileContextSwitchThreadColors[MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS] = //palette generated by http://tools.medialab.sciences-po.fr/iwanthue/index.php
  275. {
  276. 0x63607B,
  277. 0x755E2B,
  278. 0x326A55,
  279. 0x523135,
  280. 0x904F42,
  281. 0x87536B,
  282. 0x346875,
  283. 0x5E6046,
  284. 0x35404C,
  285. 0x224038,
  286. 0x413D1E,
  287. 0x5E3A26,
  288. 0x5D6161,
  289. 0x4C6234,
  290. 0x7D564F,
  291. 0x5C4352,
  292. };
  293. void MicroProfileInitUI()
  294. {
  295. static bool bInitialized = false;
  296. if(!bInitialized)
  297. {
  298. bInitialized = true;
  299. memset(&g_MicroProfileUI, 0, sizeof(g_MicroProfileUI));
  300. UI.nActiveMenu = (uint32_t)-1;
  301. UI.fDetailedOffsetTarget = UI.fDetailedOffset = 0.f;
  302. UI.fDetailedRangeTarget = UI.fDetailedRange = 50.f;
  303. UI.nOpacityBackground = 0xff<<24;
  304. UI.nOpacityForeground = 0xff<<24;
  305. UI.bShowSpikes = false;
  306. UI.nWidth = 100;
  307. UI.nHeight = 100;
  308. UI.nCustomActive = (uint32_t)-1;
  309. UI.nCustomTimerCount = 0;
  310. UI.nCustomCount = 0;
  311. int nIndex = 0;
  312. UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "Reference");
  313. for(int i = 0; i < MICROPROFILE_NUM_REFERENCE_PRESETS; ++i)
  314. {
  315. UI.Options[nIndex++] = SOptionDesc(0, i, " %6.2fms", g_MicroProfileReferenceTimePresets[i]);
  316. }
  317. UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "BG Opacity");
  318. for(int i = 0; i < MICROPROFILE_NUM_OPACITY_PRESETS; ++i)
  319. {
  320. UI.Options[nIndex++] = SOptionDesc(1, i, " %7d%%", (i+1)*25);
  321. }
  322. UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "FG Opacity");
  323. for(int i = 0; i < MICROPROFILE_NUM_OPACITY_PRESETS; ++i)
  324. {
  325. UI.Options[nIndex++] = SOptionDesc(2, i, " %7d%%", (i+1)*25);
  326. }
  327. UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "Spike Display");
  328. UI.Options[nIndex++] = SOptionDesc(3, 0, "%s", " Enable");
  329. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  330. UI.Options[nIndex++] = SOptionDesc(0xff, 0, "%s", "CSwitch Trace");
  331. UI.Options[nIndex++] = SOptionDesc(4, 0, "%s", " Enable");
  332. UI.Options[nIndex++] = SOptionDesc(4, 1, "%s", " All Threads");
  333. UI.Options[nIndex++] = SOptionDesc(4, 2, "%s", " No Bars");
  334. #endif
  335. MP_ASSERT(nIndex == MICROPROFILE_OPTION_SIZE);
  336. }
  337. }
  338. void MicroProfileSetDisplayMode(int nValue)
  339. {
  340. MicroProfile& S = *MicroProfileGet();
  341. nValue = nValue >= 0 && nValue < 4 ? nValue : S.nDisplay;
  342. S.nDisplay = nValue;
  343. UI.nOffsetY = 0;
  344. }
  345. void MicroProfileToggleDisplayMode()
  346. {
  347. MicroProfile& S = *MicroProfileGet();
  348. S.nDisplay = (S.nDisplay + 1) % 4;
  349. UI.nOffsetY = 0;
  350. }
  351. inline void MicroProfileStringArrayClear(MicroProfileStringArray* pArray)
  352. {
  353. pArray->nNumStrings = 0;
  354. pArray->pBufferPos = &pArray->Buffer[0];
  355. }
  356. inline void MicroProfileStringArrayAddLiteral(MicroProfileStringArray* pArray, const char* pLiteral)
  357. {
  358. MP_ASSERT(pArray->nNumStrings < MICROPROFILE_TOOLTIP_MAX_STRINGS);
  359. pArray->ppStrings[pArray->nNumStrings++] = pLiteral;
  360. }
  361. inline void MicroProfileStringArrayFormat(MicroProfileStringArray* pArray, const char* fmt, ...)
  362. {
  363. MP_ASSERT(pArray->nNumStrings < MICROPROFILE_TOOLTIP_MAX_STRINGS);
  364. pArray->ppStrings[pArray->nNumStrings++] = pArray->pBufferPos;
  365. va_list args;
  366. va_start (args, fmt);
  367. pArray->pBufferPos += 1 + vsprintf(pArray->pBufferPos, fmt, args);
  368. va_end(args);
  369. MP_ASSERT(pArray->pBufferPos < pArray->Buffer + MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE);
  370. }
  371. inline void MicroProfileStringArrayCopy(MicroProfileStringArray* pDest, MicroProfileStringArray* pSrc)
  372. {
  373. memcpy(&pDest->ppStrings[0], &pSrc->ppStrings[0], sizeof(pDest->ppStrings));
  374. memcpy(&pDest->Buffer[0], &pSrc->Buffer[0], sizeof(pDest->Buffer));
  375. for(uint32_t i = 0; i < MICROPROFILE_TOOLTIP_MAX_STRINGS; ++i)
  376. {
  377. if(i < pSrc->nNumStrings)
  378. {
  379. if(pSrc->ppStrings[i] >= &pSrc->Buffer[0] && pSrc->ppStrings[i] < &pSrc->Buffer[0] + MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE)
  380. {
  381. pDest->ppStrings[i] += &pDest->Buffer[0] - &pSrc->Buffer[0];
  382. }
  383. }
  384. }
  385. pDest->nNumStrings = pSrc->nNumStrings;
  386. }
  387. inline void MicroProfileFloatWindowSize(const char** ppStrings, uint32_t nNumStrings, uint32_t* pColors, uint32_t& nWidth, uint32_t& nHeight, uint32_t* pStringLengths = 0)
  388. {
  389. uint32_t* nStringLengths = pStringLengths ? pStringLengths : (uint32_t*)alloca(nNumStrings * sizeof(uint32_t));
  390. uint32_t nTextCount = nNumStrings/2;
  391. for(uint32_t i = 0; i < nTextCount; ++i)
  392. {
  393. uint32_t i0 = i * 2;
  394. uint32_t s0, s1;
  395. nStringLengths[i0] = s0 = (uint32_t)strlen(ppStrings[i0]);
  396. nStringLengths[i0+1] = s1 = (uint32_t)strlen(ppStrings[i0+1]);
  397. nWidth = MicroProfileMax(s0+s1, nWidth);
  398. }
  399. nWidth = (MICROPROFILE_TEXT_WIDTH+1) * (2+nWidth) + 2 * MICROPROFILE_BORDER_SIZE;
  400. if(pColors)
  401. nWidth += MICROPROFILE_TEXT_WIDTH + 1;
  402. nHeight = (MICROPROFILE_TEXT_HEIGHT+1) * nTextCount + 2 * MICROPROFILE_BORDER_SIZE;
  403. }
  404. inline void MicroProfileDrawFloatWindow(uint32_t nX, uint32_t nY, const char** ppStrings, uint32_t nNumStrings, uint32_t nColor, uint32_t* pColors = 0)
  405. {
  406. uint32_t nWidth = 0, nHeight = 0;
  407. uint32_t* nStringLengths = (uint32_t*)alloca(nNumStrings * sizeof(uint32_t));
  408. MicroProfileFloatWindowSize(ppStrings, nNumStrings, pColors, nWidth, nHeight, nStringLengths);
  409. uint32_t nTextCount = nNumStrings/2;
  410. if(nX + nWidth > UI.nWidth)
  411. nX = UI.nWidth - nWidth;
  412. if(nY + nHeight > UI.nHeight)
  413. nY = UI.nHeight - nHeight;
  414. MicroProfileDrawBox(nX-1, nY-1, nX + nWidth+1, nY + nHeight+1, 0xff000000|nColor);
  415. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000);
  416. if(pColors)
  417. {
  418. nX += MICROPROFILE_TEXT_WIDTH+1;
  419. nWidth -= MICROPROFILE_TEXT_WIDTH+1;
  420. }
  421. for(uint32_t i = 0; i < nTextCount; ++i)
  422. {
  423. int i0 = i * 2;
  424. if(pColors)
  425. {
  426. MicroProfileDrawBox(nX-MICROPROFILE_TEXT_WIDTH, nY, nX, nY + MICROPROFILE_TEXT_WIDTH, pColors[i]|0xff000000);
  427. }
  428. MicroProfileDrawText(nX + 1, nY + 1, (uint32_t)-1, ppStrings[i0], (uint32_t)strlen(ppStrings[i0]));
  429. MicroProfileDrawText(nX + nWidth - nStringLengths[i0+1] * (MICROPROFILE_TEXT_WIDTH+1), nY + 1, (uint32_t)-1, ppStrings[i0+1], (uint32_t)strlen(ppStrings[i0+1]));
  430. nY += (MICROPROFILE_TEXT_HEIGHT+1);
  431. }
  432. }
  433. inline void MicroProfileDrawTextBox(uint32_t nX, uint32_t nY, const char** ppStrings, uint32_t nNumStrings, uint32_t nColor, uint32_t* pColors = 0)
  434. {
  435. uint32_t nWidth = 0, nHeight = 0;
  436. uint32_t* nStringLengths = (uint32_t*)alloca(nNumStrings * sizeof(uint32_t));
  437. for(uint32_t i = 0; i < nNumStrings; ++i)
  438. {
  439. nStringLengths[i] = (uint32_t)strlen(ppStrings[i]);
  440. nWidth = MicroProfileMax(nWidth, nStringLengths[i]);
  441. nHeight++;
  442. }
  443. nWidth = (MICROPROFILE_TEXT_WIDTH+1) * (2+nWidth) + 2 * MICROPROFILE_BORDER_SIZE;
  444. nHeight = (MICROPROFILE_TEXT_HEIGHT+1) * nHeight + 2 * MICROPROFILE_BORDER_SIZE;
  445. if(nX + nWidth > UI.nWidth)
  446. nX = UI.nWidth - nWidth;
  447. if(nY + nHeight > UI.nHeight)
  448. nY = UI.nHeight - nHeight;
  449. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000);
  450. for(uint32_t i = 0; i < nNumStrings; ++i)
  451. {
  452. MicroProfileDrawText(nX + 1, nY + 1, (uint32_t)-1, ppStrings[i], (uint32_t)strlen(ppStrings[i]));
  453. nY += (MICROPROFILE_TEXT_HEIGHT+1);
  454. }
  455. }
  456. inline void MicroProfileToolTipMeta(MicroProfileStringArray* pToolTip)
  457. {
  458. MicroProfile& S = *MicroProfileGet();
  459. if(UI.nRangeBeginIndex != UI.nRangeEndIndex && UI.pRangeLog)
  460. {
  461. uint64_t nMetaSum[MICROPROFILE_META_MAX] = {0};
  462. uint64_t nMetaSumInclusive[MICROPROFILE_META_MAX] = {0};
  463. int nStackDepth = 0;
  464. uint32_t nRange[2][2];
  465. MicroProfileThreadLog* pLog = UI.pRangeLog;
  466. MicroProfileGetRange(UI.nRangeEndIndex, UI.nRangeBeginIndex, nRange);
  467. for(uint32_t i = 0; i < 2; ++i)
  468. {
  469. uint32_t nStart = nRange[i][0];
  470. uint32_t nEnd = nRange[i][1];
  471. for(uint32_t j = nStart; j < nEnd; ++j)
  472. {
  473. MicroProfileLogEntry LE = pLog->Log[j];
  474. int nType = MicroProfileLogType(LE);
  475. switch(nType)
  476. {
  477. case MP_LOG_META:
  478. {
  479. int64_t nMetaIndex = MicroProfileLogTimerIndex(LE);
  480. int64_t nMetaCount = MicroProfileLogGetTick(LE);
  481. MP_ASSERT(nMetaIndex < MICROPROFILE_META_MAX);
  482. if(nStackDepth>1)
  483. {
  484. nMetaSumInclusive[nMetaIndex] += nMetaCount;
  485. }
  486. else
  487. {
  488. nMetaSum[nMetaIndex] += nMetaCount;
  489. }
  490. }
  491. break;
  492. case MP_LOG_LEAVE:
  493. if(nStackDepth)
  494. {
  495. nStackDepth--;
  496. }
  497. else
  498. {
  499. for(int i = 0; i < MICROPROFILE_META_MAX; ++i)
  500. {
  501. nMetaSumInclusive[i] += nMetaSum[i];
  502. nMetaSum[i] = 0;
  503. }
  504. }
  505. break;
  506. case MP_LOG_ENTER:
  507. nStackDepth++;
  508. break;
  509. }
  510. }
  511. }
  512. bool bSpaced = false;
  513. for(int i = 0; i < MICROPROFILE_META_MAX; ++i)
  514. {
  515. if(S.MetaCounters[i].pName && (nMetaSum[i]||nMetaSumInclusive[i]))
  516. {
  517. if(!bSpaced)
  518. {
  519. bSpaced = true;
  520. MicroProfileStringArrayAddLiteral(pToolTip, "");
  521. MicroProfileStringArrayAddLiteral(pToolTip, "");
  522. }
  523. MicroProfileStringArrayFormat(pToolTip, "%s excl", S.MetaCounters[i].pName);
  524. MicroProfileStringArrayFormat(pToolTip, "%5d", nMetaSum[i]);
  525. MicroProfileStringArrayFormat(pToolTip, "%s incl", S.MetaCounters[i].pName);
  526. MicroProfileStringArrayFormat(pToolTip, "%5d", nMetaSum[i] + nMetaSumInclusive[i]);
  527. }
  528. }
  529. }
  530. }
  531. inline void MicroProfileDrawFloatTooltip(uint32_t nX, uint32_t nY, uint32_t nToken, uint64_t nTime)
  532. {
  533. MicroProfile& S = *MicroProfileGet();
  534. uint32_t nIndex = MicroProfileGetTimerIndex(nToken);
  535. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  536. uint32_t nAggregateCount = S.Aggregate[nIndex].nCount ? S.Aggregate[nIndex].nCount : 1;
  537. uint32_t nGroupId = MicroProfileGetGroupIndex(nToken);
  538. uint32_t nTimerId = MicroProfileGetTimerIndex(nToken);
  539. bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu;
  540. float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  541. float fMs = fToMs * (nTime);
  542. float fFrameMs = fToMs * (S.Frame[nIndex].nTicks);
  543. float fAverage = fToMs * (S.Aggregate[nIndex].nTicks/nAggregateFrames);
  544. float fCallAverage = fToMs * (S.Aggregate[nIndex].nTicks / nAggregateCount);
  545. float fMax = fToMs * (S.AggregateMax[nIndex]);
  546. float fFrameMsExclusive = fToMs * (S.FrameExclusive[nIndex]);
  547. float fAverageExclusive = fToMs * (S.AggregateExclusive[nIndex]/nAggregateFrames);
  548. float fMaxExclusive = fToMs * (S.AggregateMaxExclusive[nIndex]);
  549. float fGroupAverage = fToMs * (S.AggregateGroup[nGroupId] / nAggregateFrames);
  550. float fGroupMax = fToMs * (S.AggregateGroupMax[nGroupId]);
  551. float fGroup = fToMs * (S.FrameGroup[nGroupId]);
  552. MicroProfileStringArray ToolTip;
  553. MicroProfileStringArrayClear(&ToolTip);
  554. const char* pGroupName = S.GroupInfo[nGroupId].pName;
  555. const char* pTimerName = S.TimerInfo[nTimerId].pName;
  556. MicroProfileStringArrayAddLiteral(&ToolTip, "Timer:");
  557. MicroProfileStringArrayFormat(&ToolTip, "%s", pTimerName);
  558. #if MICROPROFILE_DEBUG
  559. MicroProfileStringArrayFormat(&ToolTip,"0x%p", UI.nHoverAddressEnter);
  560. MicroProfileStringArrayFormat(&ToolTip,"0x%p", UI.nHoverAddressLeave);
  561. #endif
  562. if(nTime != (uint64_t)0)
  563. {
  564. MicroProfileStringArrayAddLiteral(&ToolTip, "Time:");
  565. MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fMs);
  566. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  567. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  568. }
  569. MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Time:");
  570. MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fFrameMs);
  571. MicroProfileStringArrayAddLiteral(&ToolTip, "Average:");
  572. MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fAverage);
  573. MicroProfileStringArrayAddLiteral(&ToolTip, "Max:");
  574. MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fMax);
  575. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  576. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  577. MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Call Average:");
  578. MicroProfileStringArrayFormat(&ToolTip,"%6.3fms", fCallAverage);
  579. MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Call Count:");
  580. MicroProfileStringArrayFormat(&ToolTip, "%6d", nAggregateCount / nAggregateFrames);
  581. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  582. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  583. MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Frame Time:");
  584. MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fFrameMsExclusive);
  585. MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Average:");
  586. MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fAverageExclusive);
  587. MicroProfileStringArrayAddLiteral(&ToolTip, "Exclusive Max:");
  588. MicroProfileStringArrayFormat(&ToolTip, "%6.3fms", fMaxExclusive);
  589. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  590. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  591. MicroProfileStringArrayAddLiteral(&ToolTip, "Group:");
  592. MicroProfileStringArrayFormat(&ToolTip, "%s", pGroupName);
  593. MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Time:");
  594. MicroProfileStringArrayFormat(&ToolTip, "%6.3f", fGroup);
  595. MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Average:");
  596. MicroProfileStringArrayFormat(&ToolTip, "%6.3f", fGroupAverage);
  597. MicroProfileStringArrayAddLiteral(&ToolTip, "Frame Max:");
  598. MicroProfileStringArrayFormat(&ToolTip, "%6.3f", fGroupMax);
  599. MicroProfileToolTipMeta(&ToolTip);
  600. MicroProfileDrawFloatWindow(nX, nY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, S.TimerInfo[nTimerId].nColor);
  601. if(UI.nMouseLeftMod)
  602. {
  603. int nIndex = (g_MicroProfileUI.LockedToolTipFront + MICROPROFILE_TOOLTIP_MAX_LOCKED - 1) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  604. g_MicroProfileUI.nLockedToolTipColor[nIndex] = S.TimerInfo[nTimerId].nColor;
  605. MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex], &ToolTip);
  606. g_MicroProfileUI.LockedToolTipFront = nIndex;
  607. }
  608. }
  609. inline void MicroProfileZoomTo(int64_t nTickStart, int64_t nTickEnd)
  610. {
  611. MicroProfile& S = *MicroProfileGet();
  612. int64_t nStart = S.Frames[S.nFrameCurrent].nFrameStartCpu;
  613. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  614. UI.fDetailedOffsetTarget = MicroProfileLogTickDifference(nStart, nTickStart) * fToMs;
  615. UI.fDetailedRangeTarget = MicroProfileLogTickDifference(nTickStart, nTickEnd) * fToMs;
  616. }
  617. inline void MicroProfileCenter(int64_t nTickCenter)
  618. {
  619. MicroProfile& S = *MicroProfileGet();
  620. int64_t nStart = S.Frames[S.nFrameCurrent].nFrameStartCpu;
  621. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  622. float fCenter = MicroProfileLogTickDifference(nStart, nTickCenter) * fToMs;
  623. UI.fDetailedOffsetTarget = UI.fDetailedOffset = fCenter - 0.5f * UI.fDetailedRange;
  624. }
  625. #ifdef MICROPROFILE_DEBUG
  626. uint64_t* g_pMicroProfileDumpStart = 0;
  627. uint64_t* g_pMicroProfileDumpEnd = 0;
  628. inline void MicroProfileDebugDumpRange()
  629. {
  630. MicroProfile& S = *MicroProfileGet();
  631. if(g_pMicroProfileDumpStart != g_pMicroProfileDumpEnd)
  632. {
  633. uint64_t* pStart = g_pMicroProfileDumpStart;
  634. uint64_t* pEnd = g_pMicroProfileDumpEnd;
  635. while(pStart != pEnd)
  636. {
  637. uint64_t nTick = MicroProfileLogGetTick(*pStart);
  638. uint64_t nToken = MicroProfileLogTimerIndex(*pStart);
  639. uint32_t nTimerId = MicroProfileGetTimerIndex(nToken);
  640. const char* pTimerName = S.TimerInfo[nTimerId].pName;
  641. char buffer[256];
  642. int type = MicroProfileLogType(*pStart);
  643. const char* pBegin = type == MP_LOG_LEAVE ? "END" :
  644. (type == MP_LOG_ENTER ? "BEGIN" : "META");
  645. snprintf(buffer, 255, "DUMP 0x%p: %s :: %llx: %s\n", pStart, pBegin, nTick, pTimerName);
  646. #ifdef _WIN32
  647. OutputDebugString(buffer);
  648. #else
  649. printf("%s", buffer);
  650. #endif
  651. pStart++;
  652. }
  653. g_pMicroProfileDumpStart = g_pMicroProfileDumpEnd;
  654. }
  655. }
  656. #define MP_DEBUG_DUMP_RANGE() MicroProfileDebugDumpRange();
  657. #else
  658. #define MP_DEBUG_DUMP_RANGE() do{} while(0)
  659. #endif
  660. #define MICROPROFILE_HOVER_DIST 0.5f
  661. inline void MicroProfileDrawDetailedContextSwitchBars(uint32_t nY, uint32_t nThreadId, uint32_t nContextSwitchStart, uint32_t nContextSwitchEnd, int64_t nBaseTicks, uint32_t nBaseY)
  662. {
  663. MicroProfile& S = *MicroProfileGet();
  664. int64_t nTickIn = -1;
  665. uint32_t nThreadBefore = -1;
  666. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  667. float fMsToScreen = UI.nWidth / UI.fDetailedRange;
  668. float fMouseX = (float)UI.nMouseX;
  669. float fMouseY = (float)UI.nMouseY;
  670. for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE)
  671. {
  672. MP_ASSERT(j < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE);
  673. MicroProfileContextSwitch CS = S.ContextSwitch[j];
  674. if(nTickIn == -1)
  675. {
  676. if(CS.nThreadIn == nThreadId)
  677. {
  678. nTickIn = CS.nTicks;
  679. nThreadBefore = CS.nThreadOut;
  680. }
  681. }
  682. else
  683. {
  684. if(CS.nThreadOut == nThreadId)
  685. {
  686. int64_t nTickOut = CS.nTicks;
  687. float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickIn);
  688. float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickOut);
  689. if(fMsStart <= fMsEnd)
  690. {
  691. float fXStart = fMsStart * fMsToScreen;
  692. float fXEnd = fMsEnd * fMsToScreen;
  693. float fYStart = (float)nY;
  694. float fYEnd = fYStart + (MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT);
  695. uint32_t nColor = g_nMicroProfileContextSwitchThreadColors[CS.nCpu%MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS];
  696. float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd);
  697. bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY;
  698. if(bHover)
  699. {
  700. UI.nRangeBegin = nTickIn;
  701. UI.nRangeEnd = nTickOut;
  702. S.nContextSwitchHoverTickIn = nTickIn;
  703. S.nContextSwitchHoverTickOut = nTickOut;
  704. S.nContextSwitchHoverThread = CS.nThreadOut;
  705. S.nContextSwitchHoverThreadBefore = nThreadBefore;
  706. S.nContextSwitchHoverThreadAfter = CS.nThreadIn;
  707. S.nContextSwitchHoverCpuNext = CS.nCpu;
  708. nColor = UI.nHoverColor;
  709. }
  710. if(CS.nCpu == S.nContextSwitchHoverCpu)
  711. {
  712. nColor = UI.nHoverColorShared;
  713. }
  714. MicroProfileDrawBox(fXStart, fYStart, fXEnd, fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeFlat);
  715. }
  716. nTickIn = -1;
  717. }
  718. }
  719. }
  720. }
  721. inline void MicroProfileDrawDetailedBars(uint32_t nWidth, uint32_t nHeight, int nBaseY, int nSelectedFrame)
  722. {
  723. MicroProfile& S = *MicroProfileGet();
  724. MP_DEBUG_DUMP_RANGE();
  725. int nY = nBaseY - UI.nOffsetY;
  726. int64_t nNumBoxes = 0;
  727. int64_t nNumLines = 0;
  728. uint32_t nFrameNext = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY;
  729. MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent];
  730. MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext];
  731. UI.nRangeBegin = 0;
  732. UI.nRangeEnd = 0;
  733. UI.nRangeBeginGpu = 0;
  734. UI.nRangeEndGpu = 0;
  735. UI.nRangeBeginIndex = UI.nRangeEndIndex = 0;
  736. UI.pRangeLog = 0;
  737. int64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu;
  738. int64_t nFrameStartGpu = pFrameCurrent->nFrameStartGpu;
  739. int64_t nTicksPerSecondCpu = MicroProfileTicksPerSecondCpu();
  740. int64_t nTicksPerSecondGpu = MicroProfileTicksPerSecondGpu();
  741. float fToMsCpu = MicroProfileTickToMsMultiplier(nTicksPerSecondCpu);
  742. float fToMsGpu = MicroProfileTickToMsMultiplier(nTicksPerSecondGpu);
  743. float fDetailedOffset = UI.fDetailedOffset;
  744. float fDetailedRange = UI.fDetailedRange;
  745. int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(fDetailedOffset, MicroProfileTicksPerSecondCpu());
  746. int64_t nDetailedOffsetTicksGpu = MicroProfileMsToTick(fDetailedOffset, MicroProfileTicksPerSecondGpu());
  747. int64_t nBaseTicksCpu = nDetailedOffsetTicksCpu + nFrameStartCpu;
  748. int64_t nBaseTicksGpu = nDetailedOffsetTicksGpu + nFrameStartGpu;
  749. int64_t nBaseTicksEndCpu = nBaseTicksCpu + MicroProfileMsToTick(fDetailedRange, MicroProfileTicksPerSecondCpu());
  750. int64_t nTickReferenceCpu = 0, nTickReferenceGpu = 0;
  751. static int64_t nRefCpu = 0, nRefGpu = 0;
  752. if(MicroProfileGetGpuTickReference(&nTickReferenceCpu, &nTickReferenceGpu))
  753. {
  754. if(0 == nRefCpu || std::abs(nRefCpu-nBaseTicksCpu) > std::abs(nTickReferenceCpu-nBaseTicksCpu))
  755. {
  756. nRefCpu = nTickReferenceCpu;
  757. nRefGpu = nTickReferenceGpu;
  758. }
  759. else
  760. {
  761. nTickReferenceCpu = nRefCpu;
  762. nTickReferenceGpu = nRefGpu;
  763. }
  764. nBaseTicksGpu = (nBaseTicksCpu - nTickReferenceCpu) * nTicksPerSecondGpu / nTicksPerSecondCpu + nTickReferenceGpu;
  765. }
  766. int64_t nBaseTicksEndGpu = nBaseTicksCpu + MicroProfileMsToTick(fDetailedRange, MicroProfileTicksPerSecondCpu());
  767. MicroProfileFrameState* pFrameFirst = pFrameCurrent;
  768. int64_t nGapTime = MicroProfileTicksPerSecondCpu() * MICROPROFILE_GAP_TIME / 1000;
  769. for(uint32_t i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY; ++i)
  770. {
  771. uint32_t nNextIndex = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY;
  772. pFrameFirst = &S.Frames[nNextIndex];
  773. if(pFrameFirst->nFrameStartCpu <= nBaseTicksCpu-nGapTime)
  774. break;
  775. }
  776. float fMsBase = fToMsCpu * nDetailedOffsetTicksCpu;
  777. float fMs = fDetailedRange;
  778. float fMsEnd = fMs + fMsBase;
  779. float fWidth = (float)nWidth;
  780. float fMsToScreen = fWidth / fMs;
  781. {
  782. float fRate = floor(2*(log10(fMs)-1))/2;
  783. float fStep = powf(10.f, fRate);
  784. float fRcpStep = 1.f / fStep;
  785. int nColorIndex = (int)(floor(fMsBase*fRcpStep));
  786. float fStart = floor(fMsBase*fRcpStep) * fStep;
  787. for(float f = fStart; f < fMsEnd; )
  788. {
  789. float fStart = f;
  790. float fNext = f + fStep;
  791. MicroProfileDrawBox(((fStart-fMsBase) * fMsToScreen), nBaseY, (fNext-fMsBase) * fMsToScreen+1, nBaseY + nHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[nColorIndex++ & 1]);
  792. f = fNext;
  793. }
  794. }
  795. nY += MICROPROFILE_TEXT_HEIGHT+1;
  796. MicroProfileLogEntry* pMouseOver = UI.pDisplayMouseOver;
  797. MicroProfileLogEntry* pMouseOverNext = 0;
  798. uint64_t nMouseOverToken = pMouseOver ? MicroProfileLogTimerIndex(*pMouseOver) : MICROPROFILE_INVALID_TOKEN;
  799. float fMouseX = (float)UI.nMouseX;
  800. float fMouseY = (float)UI.nMouseY;
  801. uint64_t nHoverToken = MICROPROFILE_INVALID_TOKEN;
  802. int64_t nHoverTime = 0;
  803. static int nHoverCounter = 155;
  804. static int nHoverCounterDelta = 10;
  805. nHoverCounter += nHoverCounterDelta;
  806. if(nHoverCounter >= 245)
  807. nHoverCounterDelta = -10;
  808. else if(nHoverCounter < 100)
  809. nHoverCounterDelta = 10;
  810. UI.nHoverColor = (nHoverCounter<<24)|(nHoverCounter<<16)|(nHoverCounter<<8)|nHoverCounter;
  811. uint32_t nHoverCounterShared = nHoverCounter>>2;
  812. UI.nHoverColorShared = (nHoverCounterShared<<24)|(nHoverCounterShared<<16)|(nHoverCounterShared<<8)|nHoverCounterShared;
  813. uint32_t nLinesDrawn[MICROPROFILE_STACK_MAX]={0};
  814. uint32_t nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadAfter;
  815. uint32_t nContextSwitchHoverThreadBefore = S.nContextSwitchHoverThreadBefore;
  816. S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = -1;
  817. uint32_t nContextSwitchStart = -1;
  818. uint32_t nContextSwitchEnd = -1;
  819. S.nContextSwitchHoverCpuNext = 0xff;
  820. S.nContextSwitchHoverTickIn = -1;
  821. S.nContextSwitchHoverTickOut = -1;
  822. if(S.bContextSwitchRunning)
  823. {
  824. MicroProfileContextSwitchSearch(&nContextSwitchStart, &nContextSwitchEnd, nBaseTicksCpu, nBaseTicksEndCpu);
  825. }
  826. bool bSkipBarView = S.bContextSwitchRunning && S.bContextSwitchNoBars;
  827. if(!bSkipBarView)
  828. {
  829. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  830. {
  831. MicroProfileThreadLog* pLog = S.Pool[i];
  832. if(!pLog)
  833. continue;
  834. uint32_t nPut = pFrameNext->nLogStart[i];
  835. ///note: this may display new samples as old data, but this will only happen when
  836. // unpaused, where the detailed view is hardly perceptible
  837. uint32_t nFront = S.Pool[i]->nPut.load(std::memory_order_relaxed);
  838. MicroProfileFrameState* pFrameLogFirst = pFrameCurrent;
  839. MicroProfileFrameState* pFrameLogLast = pFrameNext;
  840. uint32_t nGet = pFrameLogFirst->nLogStart[i];
  841. do
  842. {
  843. MP_ASSERT(pFrameLogFirst >= &S.Frames[0] && pFrameLogFirst < &S.Frames[MICROPROFILE_MAX_FRAME_HISTORY]);
  844. uint32_t nNewGet = pFrameLogFirst->nLogStart[i];
  845. bool bIsValid = false;
  846. if(nPut < nFront)
  847. {
  848. bIsValid = nNewGet <= nPut || nNewGet >= nFront;
  849. }
  850. else
  851. {
  852. bIsValid = nNewGet <= nPut && nNewGet >= nFront;
  853. }
  854. if(bIsValid)
  855. {
  856. nGet = nNewGet;
  857. pFrameLogFirst--;
  858. if(pFrameLogFirst < &S.Frames[0])
  859. pFrameLogFirst = &S.Frames[MICROPROFILE_MAX_FRAME_HISTORY-1];
  860. }
  861. else
  862. {
  863. break;
  864. }
  865. }while(pFrameLogFirst != pFrameFirst);
  866. if(nGet == (uint32_t)-1)
  867. continue;
  868. MP_ASSERT(nGet != (uint32_t)-1);
  869. nPut = pFrameLogLast->nLogStart[i];
  870. uint32_t nRange[2][2] = { {0, 0}, {0, 0}, };
  871. MicroProfileGetRange(nPut, nGet, nRange);
  872. if(nPut == nGet)
  873. continue;
  874. uint32_t nMaxStackDepth = 0;
  875. bool bGpu = pLog->nGpu != 0;
  876. float fToMs = bGpu ? fToMsGpu : fToMsCpu;
  877. int64_t nBaseTicks = bGpu ? nBaseTicksGpu : nBaseTicksCpu;
  878. char ThreadName[MicroProfileThreadLog::THREAD_MAX_LEN + 16];
  879. uint64_t nThreadId = pLog->nThreadId;
  880. snprintf(ThreadName, sizeof(ThreadName)-1, "%04llx: %s", nThreadId, &pLog->ThreadName[0] );
  881. nY += 3;
  882. uint32_t nThreadColor = -1;
  883. if(pLog->nThreadId == nContextSwitchHoverThreadAfter || pLog->nThreadId == nContextSwitchHoverThreadBefore)
  884. nThreadColor = UI.nHoverColorShared|0x906060;
  885. MicroProfileDrawText(0, nY, nThreadColor, &ThreadName[0], (uint32_t)strlen(&ThreadName[0]));
  886. nY += 3;
  887. nY += MICROPROFILE_TEXT_HEIGHT + 1;
  888. if(S.bContextSwitchRunning)
  889. {
  890. MicroProfileDrawDetailedContextSwitchBars(nY, pLog->nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicks, nBaseY);
  891. nY -= MICROPROFILE_DETAILED_BAR_HEIGHT;
  892. nY += MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT+1;
  893. }
  894. uint32_t nYDelta = MICROPROFILE_DETAILED_BAR_HEIGHT;
  895. uint32_t nStack[MICROPROFILE_STACK_MAX];
  896. uint32_t nStackPos = 0;
  897. for(uint32_t j = 0; j < 2; ++j)
  898. {
  899. uint32_t nStart = nRange[j][0];
  900. uint32_t nEnd = nRange[j][1];
  901. for(uint32_t k = nStart; k < nEnd; ++k)
  902. {
  903. MicroProfileLogEntry* pEntry = pLog->Log + k;
  904. int nType = MicroProfileLogType(*pEntry);
  905. if(MP_LOG_ENTER == nType)
  906. {
  907. MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX);
  908. nStack[nStackPos++] = k;
  909. }
  910. else if(MP_LOG_META == nType)
  911. {
  912. }
  913. else if(MP_LOG_LEAVE == nType)
  914. {
  915. if(0 == nStackPos)
  916. {
  917. continue;
  918. }
  919. MicroProfileLogEntry* pEntryEnter = pLog->Log + nStack[nStackPos-1];
  920. if(MicroProfileLogTimerIndex(*pEntryEnter) != MicroProfileLogTimerIndex(*pEntry))
  921. {
  922. //uprintf("mismatch %llx %llx\n", pEntryEnter->nToken, pEntry->nToken);
  923. continue;
  924. }
  925. int64_t nTickStart = MicroProfileLogGetTick(*pEntryEnter);
  926. int64_t nTickEnd = MicroProfileLogGetTick(*pEntry);
  927. uint64_t nTimerIndex = MicroProfileLogTimerIndex(*pEntry);
  928. uint32_t nColor = S.TimerInfo[nTimerIndex].nColor;
  929. if(nMouseOverToken == nTimerIndex)
  930. {
  931. if(pEntry == pMouseOver)
  932. {
  933. nColor = UI.nHoverColor;
  934. if(bGpu)
  935. {
  936. UI.nRangeBeginGpu = *pEntryEnter;
  937. UI.nRangeEndGpu = *pEntry;
  938. uint32_t nCpuBegin = (nStack[nStackPos-1] + 1) % MICROPROFILE_BUFFER_SIZE;
  939. uint32_t nCpuEnd = (k + 1) % MICROPROFILE_BUFFER_SIZE;
  940. MicroProfileLogEntry LogCpuBegin = pLog->Log[nCpuBegin];
  941. MicroProfileLogEntry LogCpuEnd = pLog->Log[nCpuEnd];
  942. if(MicroProfileLogType(LogCpuBegin)==3 && MicroProfileLogType(LogCpuEnd) == 3)
  943. {
  944. UI.nRangeBegin = LogCpuBegin;
  945. UI.nRangeEnd = LogCpuEnd;
  946. }
  947. UI.nRangeBeginIndex = nStack[nStackPos-1];
  948. UI.nRangeEndIndex = k;
  949. UI.pRangeLog = pLog;
  950. }
  951. else
  952. {
  953. UI.nRangeBegin = *pEntryEnter;
  954. UI.nRangeEnd = *pEntry;
  955. UI.nRangeBeginIndex = nStack[nStackPos-1];
  956. UI.nRangeEndIndex = k;
  957. UI.pRangeLog = pLog;
  958. }
  959. }
  960. else
  961. {
  962. nColor = UI.nHoverColorShared;
  963. }
  964. }
  965. nMaxStackDepth = MicroProfileMax(nMaxStackDepth, nStackPos);
  966. float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickStart);
  967. float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickEnd);
  968. float fXStart = fMsStart * fMsToScreen;
  969. float fXEnd = fMsEnd * fMsToScreen;
  970. float fYStart = (float)(nY + nStackPos * nYDelta);
  971. float fYEnd = fYStart + (MICROPROFILE_DETAILED_BAR_HEIGHT);
  972. float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd);
  973. bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY;
  974. uint32_t nIntegerWidth = (uint32_t)(fXEnd - fXStart);
  975. if(nIntegerWidth)
  976. {
  977. if(bHover && UI.nActiveMenu == -1)
  978. {
  979. nHoverToken = MicroProfileLogTimerIndex(*pEntry);
  980. #if MICROPROFILE_DEBUG
  981. UI.nHoverAddressEnter = (uint64_t)pEntryEnter;
  982. UI.nHoverAddressLeave = (uint64_t)pEntry;
  983. #endif
  984. nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd);
  985. pMouseOverNext = pEntry;
  986. }
  987. MicroProfileDrawBox(fXStart, fYStart, fXEnd, fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeBar);
  988. #if MICROPROFILE_DETAILED_BAR_NAMES
  989. if(nIntegerWidth>3*MICROPROFILE_TEXT_WIDTH)
  990. {
  991. float fXStartText = MicroProfileMax(fXStart, 0.f);
  992. int nTextWidth = (int)(fXEnd - fXStartText);
  993. int nCharacters = (nTextWidth - 2*MICROPROFILE_TEXT_WIDTH) / MICROPROFILE_TEXT_WIDTH;
  994. if(nCharacters>0)
  995. {
  996. MicroProfileDrawText(fXStartText+1, fYStart+1, -1, S.TimerInfo[nTimerIndex].pName, MicroProfileMin<uint32_t>(S.TimerInfo[nTimerIndex].nNameLen, nCharacters));
  997. }
  998. }
  999. #endif
  1000. ++nNumBoxes;
  1001. }
  1002. else
  1003. {
  1004. float fXAvg = 0.5f * (fXStart + fXEnd);
  1005. int nLineX = (int)floor(fXAvg+0.5f);
  1006. if(nLineX != (int)nLinesDrawn[nStackPos])
  1007. {
  1008. if(bHover && UI.nActiveMenu == -1)
  1009. {
  1010. nHoverToken = (uint32_t)MicroProfileLogTimerIndex(*pEntry);
  1011. nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd);
  1012. pMouseOverNext = pEntry;
  1013. }
  1014. nLinesDrawn[nStackPos] = nLineX;
  1015. MicroProfileDrawLineVertical(nLineX, fYStart + 0.5f, fYEnd + 0.5f, nColor|UI.nOpacityForeground);
  1016. ++nNumLines;
  1017. }
  1018. }
  1019. nStackPos--;
  1020. if(0 == nStackPos)
  1021. {
  1022. if(bGpu ? (nTickStart > nBaseTicksEndGpu) : (nTickStart > nBaseTicksEndCpu))
  1023. {
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }
  1030. nY += nMaxStackDepth * nYDelta + MICROPROFILE_DETAILED_BAR_HEIGHT+1;
  1031. }
  1032. }
  1033. if(S.bContextSwitchRunning && (S.bContextSwitchAllThreads||S.bContextSwitchNoBars))
  1034. {
  1035. uint32_t nNumThreads = 0;
  1036. uint32_t nThreads[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS];
  1037. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS && S.Pool[i]; ++i)
  1038. nThreads[nNumThreads++] = S.Pool[i]->nThreadId;
  1039. uint32_t nNumThreadsBase = nNumThreads;
  1040. if(S.bContextSwitchAllThreads)
  1041. {
  1042. for(uint32_t i = nContextSwitchStart; i != nContextSwitchEnd; i = (i+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE)
  1043. {
  1044. MicroProfileContextSwitch CS = S.ContextSwitch[i];
  1045. ThreadIdType nThreadId = CS.nThreadIn;
  1046. if(nThreadId)
  1047. {
  1048. bool bSeen = false;
  1049. for(uint32_t j = 0; j < nNumThreads; ++j)
  1050. {
  1051. if(nThreads[j] == nThreadId)
  1052. {
  1053. bSeen = true;
  1054. break;
  1055. }
  1056. }
  1057. if(!bSeen)
  1058. {
  1059. nThreads[nNumThreads++] = nThreadId;
  1060. }
  1061. }
  1062. if(nNumThreads == MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS)
  1063. {
  1064. S.nOverflow = 10;
  1065. break;
  1066. }
  1067. }
  1068. std::sort(&nThreads[nNumThreadsBase], &nThreads[nNumThreads]);
  1069. }
  1070. uint32_t nStart = nNumThreadsBase;
  1071. if(S.bContextSwitchNoBars)
  1072. nStart = 0;
  1073. for(uint32_t i = nStart; i < nNumThreads; ++i)
  1074. {
  1075. ThreadIdType nThreadId = nThreads[i];
  1076. if(nThreadId)
  1077. {
  1078. char ThreadName[MicroProfileThreadLog::THREAD_MAX_LEN + 16];
  1079. const char* cLocal = MicroProfileIsLocalThread(nThreadId) ? "*": " ";
  1080. #if defined(_WIN32)
  1081. // nThreadId is 32-bit on Windows
  1082. int nStrLen = snprintf(ThreadName, sizeof(ThreadName)-1, "%04x: %s%s", nThreadId, cLocal, i < nNumThreadsBase ? &S.Pool[i]->ThreadName[0] : MICROPROFILE_THREAD_NAME_FROM_ID(nThreadId) );
  1083. #else
  1084. int nStrLen = snprintf(ThreadName, sizeof(ThreadName)-1, "%04llx: %s%s", nThreadId, cLocal, i < nNumThreadsBase ? &S.Pool[i]->ThreadName[0] : MICROPROFILE_THREAD_NAME_FROM_ID(nThreadId) );
  1085. #endif
  1086. uint32_t nThreadColor = -1;
  1087. if(nThreadId == nContextSwitchHoverThreadAfter || nThreadId == nContextSwitchHoverThreadBefore)
  1088. nThreadColor = UI.nHoverColorShared|0x906060;
  1089. MicroProfileDrawDetailedContextSwitchBars(nY+2, nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicksCpu, nBaseY);
  1090. MicroProfileDrawText(0, nY, nThreadColor, &ThreadName[0], nStrLen);
  1091. nY += MICROPROFILE_TEXT_HEIGHT+1;
  1092. }
  1093. }
  1094. }
  1095. S.nContextSwitchHoverCpu = S.nContextSwitchHoverCpuNext;
  1096. UI.pDisplayMouseOver = pMouseOverNext;
  1097. if(!S.nRunning)
  1098. {
  1099. if(nHoverToken != MICROPROFILE_INVALID_TOKEN && nHoverTime)
  1100. {
  1101. UI.nHoverToken = nHoverToken;
  1102. UI.nHoverTime = nHoverTime;
  1103. }
  1104. if(nSelectedFrame != -1)
  1105. {
  1106. UI.nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu;
  1107. UI.nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu;
  1108. UI.nRangeBeginGpu = S.Frames[nSelectedFrame].nFrameStartGpu;
  1109. UI.nRangeEndGpu = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartGpu;
  1110. }
  1111. if(UI.nRangeBegin != UI.nRangeEnd)
  1112. {
  1113. float fMsStart = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeBegin);
  1114. float fMsEnd = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeEnd);
  1115. float fXStart = fMsStart * fMsToScreen;
  1116. float fXEnd = fMsEnd * fMsToScreen;
  1117. MicroProfileDrawBox(fXStart, nBaseY, fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat);
  1118. MicroProfileDrawLineVertical(fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000);
  1119. MicroProfileDrawLineVertical(fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000);
  1120. fMsStart += fDetailedOffset;
  1121. fMsEnd += fDetailedOffset;
  1122. char sBuffer[32];
  1123. uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart);
  1124. float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart);
  1125. float fStartTextX = fXStart - fStartTextWidth - 2;
  1126. MicroProfileDrawBox(fStartTextX, nBaseY, fStartTextX + fStartTextWidth + 2, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1127. MicroProfileDrawText(fStartTextX+1, nBaseY, (uint32_t)-1, sBuffer, nLenStart);
  1128. uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd);
  1129. MicroProfileDrawBox(fXEnd+1, nBaseY, fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1130. MicroProfileDrawText(fXEnd+2, nBaseY+1, (uint32_t)-1, sBuffer, nLenEnd);
  1131. if(UI.nMouseRight)
  1132. {
  1133. MicroProfileZoomTo(UI.nRangeBegin, UI.nRangeEnd);
  1134. }
  1135. }
  1136. if(UI.nRangeBeginGpu != UI.nRangeEndGpu)
  1137. {
  1138. float fMsStart = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeBeginGpu);
  1139. float fMsEnd = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeEndGpu);
  1140. float fXStart = fMsStart * fMsToScreen;
  1141. float fXEnd = fMsEnd * fMsToScreen;
  1142. MicroProfileDrawBox(fXStart, nBaseY, fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU, MicroProfileBoxTypeFlat);
  1143. MicroProfileDrawLineVertical(fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000);
  1144. MicroProfileDrawLineVertical(fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000);
  1145. nBaseY += MICROPROFILE_TEXT_HEIGHT+1;
  1146. fMsStart += fDetailedOffset;
  1147. fMsEnd += fDetailedOffset;
  1148. char sBuffer[32];
  1149. uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart);
  1150. float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart);
  1151. float fStartTextX = fXStart - fStartTextWidth - 2;
  1152. MicroProfileDrawBox(fStartTextX, nBaseY, fStartTextX + fStartTextWidth + 2, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1153. MicroProfileDrawText(fStartTextX+1, nBaseY, (uint32_t)-1, sBuffer, nLenStart);
  1154. uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd);
  1155. MicroProfileDrawBox(fXEnd+1, nBaseY, fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1156. MicroProfileDrawText(fXEnd+2, nBaseY+1, (uint32_t)-1, sBuffer, nLenEnd);
  1157. }
  1158. }
  1159. }
  1160. inline void MicroProfileDrawDetailedFrameHistory(uint32_t nWidth, uint32_t nHeight, uint32_t nBaseY, uint32_t nSelectedFrame)
  1161. {
  1162. MicroProfile& S = *MicroProfileGet();
  1163. const uint32_t nBarHeight = MICROPROFILE_FRAME_HISTORY_HEIGHT;
  1164. float fBaseX = (float)nWidth;
  1165. float fDx = fBaseX / MICROPROFILE_NUM_FRAMES;
  1166. uint32_t nLastIndex = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY;
  1167. MicroProfileDrawBox(0, nBaseY, nWidth, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, 0xff000000 | g_nMicroProfileBackColors[0], MicroProfileBoxTypeFlat);
  1168. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * S.fRcpReferenceTime;
  1169. float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()) * S.fRcpReferenceTime;
  1170. MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent];
  1171. uint64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu;
  1172. int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(UI.fDetailedOffset, MicroProfileTicksPerSecondCpu());
  1173. int64_t nCpuStart = nDetailedOffsetTicksCpu + nFrameStartCpu;
  1174. int64_t nCpuEnd = nCpuStart + MicroProfileMsToTick(UI.fDetailedRange, MicroProfileTicksPerSecondCpu());;
  1175. float fSelectionStart = (float)nWidth;
  1176. float fSelectionEnd = 0.f;
  1177. for(uint32_t i = 0; i < MICROPROFILE_NUM_FRAMES; ++i)
  1178. {
  1179. uint32_t nIndex = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY;
  1180. MicroProfileFrameState* pCurrent = &S.Frames[nIndex];
  1181. MicroProfileFrameState* pNext = &S.Frames[nLastIndex];
  1182. int64_t nTicks = pNext->nFrameStartCpu - pCurrent->nFrameStartCpu;
  1183. int64_t nTicksGpu = pNext->nFrameStartGpu - pCurrent->nFrameStartGpu;
  1184. float fScale = fToMs * nTicks;
  1185. float fScaleGpu = fToMsGpu * nTicksGpu;
  1186. fScale = fScale > 1.f ? 0.f : 1.f - fScale;
  1187. fScaleGpu = fScaleGpu > 1.f ? 0.f : 1.f - fScaleGpu;
  1188. float fXEnd = fBaseX;
  1189. float fXStart = fBaseX - fDx;
  1190. fBaseX = fXStart;
  1191. uint32_t nColor = MICROPROFILE_FRAME_HISTORY_COLOR_CPU;
  1192. if(nIndex == nSelectedFrame)
  1193. nColor = (uint32_t)-1;
  1194. MicroProfileDrawBox(fXStart, nBaseY + fScale * nBarHeight, fXEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, nColor, MicroProfileBoxTypeBar);
  1195. if(pNext->nFrameStartCpu > nCpuStart)
  1196. {
  1197. fSelectionStart = fXStart;
  1198. }
  1199. if(pCurrent->nFrameStartCpu < nCpuEnd && fSelectionEnd == 0.f)
  1200. {
  1201. fSelectionEnd = fXEnd;
  1202. }
  1203. nLastIndex = nIndex;
  1204. }
  1205. MicroProfileDrawBox(fSelectionStart, nBaseY, fSelectionEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, MICROPROFILE_FRAME_HISTORY_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat);
  1206. }
  1207. inline void MicroProfileDrawDetailedView(uint32_t nWidth, uint32_t nHeight)
  1208. {
  1209. MicroProfile& S = *MicroProfileGet();
  1210. MICROPROFILE_SCOPE(g_MicroProfileDetailed);
  1211. uint32_t nBaseY = MICROPROFILE_TEXT_HEIGHT + 1;
  1212. int nSelectedFrame = -1;
  1213. if(UI.nMouseY > nBaseY && UI.nMouseY <= nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT && UI.nActiveMenu == -1)
  1214. {
  1215. nSelectedFrame = ((MICROPROFILE_NUM_FRAMES) * (UI.nWidth-UI.nMouseX) / UI.nWidth);
  1216. nSelectedFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nSelectedFrame) % MICROPROFILE_MAX_FRAME_HISTORY;
  1217. UI.nHoverFrame = nSelectedFrame;
  1218. if(UI.nMouseRight)
  1219. {
  1220. int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu;
  1221. int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu;
  1222. MicroProfileZoomTo(nRangeBegin, nRangeEnd);
  1223. }
  1224. if(UI.nMouseDownLeft)
  1225. {
  1226. uint64_t nFrac = (1024 * (MICROPROFILE_NUM_FRAMES) * (UI.nMouseX) / UI.nWidth) % 1024;
  1227. int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu;
  1228. int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu;
  1229. MicroProfileCenter(nRangeBegin + (nRangeEnd-nRangeBegin) * nFrac / 1024);
  1230. }
  1231. }
  1232. else
  1233. {
  1234. UI.nHoverFrame = -1;
  1235. }
  1236. MicroProfileDrawDetailedBars(nWidth, nHeight, nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT, nSelectedFrame);
  1237. MicroProfileDrawDetailedFrameHistory(nWidth, nHeight, nBaseY, nSelectedFrame);
  1238. }
  1239. inline void MicroProfileDrawTextRight(uint32_t nX, uint32_t nY, uint32_t nColor, const char* pStr, uint32_t nStrLen)
  1240. {
  1241. MicroProfileDrawText(nX - nStrLen * (MICROPROFILE_TEXT_WIDTH+1), nY, nColor, pStr, nStrLen);
  1242. }
  1243. inline void MicroProfileDrawHeader(int32_t nX, uint32_t nWidth, const char* pName)
  1244. {
  1245. if(pName)
  1246. {
  1247. MicroProfileDrawBox(nX-8, MICROPROFILE_TEXT_HEIGHT + 2, nX + nWidth+5, MICROPROFILE_TEXT_HEIGHT + 2 + (MICROPROFILE_TEXT_HEIGHT+1), 0xff000000|g_nMicroProfileBackColors[1]);
  1248. MicroProfileDrawText(nX, MICROPROFILE_TEXT_HEIGHT + 2, (uint32_t)-1, pName, (uint32_t)strlen(pName));
  1249. }
  1250. }
  1251. typedef void (*MicroProfileLoopGroupCallback)(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pData);
  1252. inline void MicroProfileLoopActiveGroupsDraw(int32_t nX, int32_t nY, const char* pName, MicroProfileLoopGroupCallback CB, void* pData)
  1253. {
  1254. MicroProfile& S = *MicroProfileGet();
  1255. nY += MICROPROFILE_TEXT_HEIGHT + 2;
  1256. uint64_t nGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted;
  1257. uint32_t nCount = 0;
  1258. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1259. {
  1260. uint64_t nMask = 1ll << j;
  1261. if(nMask & nGroup)
  1262. {
  1263. nY += MICROPROFILE_TEXT_HEIGHT + 1;
  1264. for(uint32_t i = 0; i < S.nTotalTimers;++i)
  1265. {
  1266. uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken);
  1267. if(nTokenMask & nMask)
  1268. {
  1269. if(nY >= 0)
  1270. CB(i, nCount, nMask, nX, nY, pData);
  1271. nCount += 2;
  1272. nY += MICROPROFILE_TEXT_HEIGHT + 1;
  1273. if(nY > (int)UI.nHeight)
  1274. return;
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. inline void MicroProfileCalcTimers(float* pTimers, float* pAverage, float* pMax, float* pCallAverage, float* pExclusive, float* pAverageExclusive, float* pMaxExclusive, uint64_t nGroup, uint32_t nSize)
  1281. {
  1282. MicroProfile& S = *MicroProfileGet();
  1283. uint32_t nCount = 0;
  1284. uint64_t nMask = 1;
  1285. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1286. {
  1287. if(nMask & nGroup)
  1288. {
  1289. const float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[j].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  1290. for(uint32_t i = 0; i < S.nTotalTimers;++i)
  1291. {
  1292. uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken);
  1293. if(nTokenMask & nMask)
  1294. {
  1295. {
  1296. uint32_t nTimer = i;
  1297. uint32_t nIdx = nCount;
  1298. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  1299. uint32_t nAggregateCount = S.Aggregate[nTimer].nCount ? S.Aggregate[nTimer].nCount : 1;
  1300. float fToPrc = S.fRcpReferenceTime;
  1301. float fMs = fToMs * (S.Frame[nTimer].nTicks);
  1302. float fPrc = MicroProfileMin(fMs * fToPrc, 1.f);
  1303. float fAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateFrames);
  1304. float fAveragePrc = MicroProfileMin(fAverageMs * fToPrc, 1.f);
  1305. float fMaxMs = fToMs * (S.AggregateMax[nTimer]);
  1306. float fMaxPrc = MicroProfileMin(fMaxMs * fToPrc, 1.f);
  1307. float fCallAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateCount);
  1308. float fCallAveragePrc = MicroProfileMin(fCallAverageMs * fToPrc, 1.f);
  1309. float fMsExclusive = fToMs * (S.FrameExclusive[nTimer]);
  1310. float fPrcExclusive = MicroProfileMin(fMsExclusive * fToPrc, 1.f);
  1311. float fAverageMsExclusive = fToMs * (S.AggregateExclusive[nTimer] / nAggregateFrames);
  1312. float fAveragePrcExclusive = MicroProfileMin(fAverageMsExclusive * fToPrc, 1.f);
  1313. float fMaxMsExclusive = fToMs * (S.AggregateMaxExclusive[nTimer]);
  1314. float fMaxPrcExclusive = MicroProfileMin(fMaxMsExclusive * fToPrc, 1.f);
  1315. pTimers[nIdx] = fMs;
  1316. pTimers[nIdx+1] = fPrc;
  1317. pAverage[nIdx] = fAverageMs;
  1318. pAverage[nIdx+1] = fAveragePrc;
  1319. pMax[nIdx] = fMaxMs;
  1320. pMax[nIdx+1] = fMaxPrc;
  1321. pCallAverage[nIdx] = fCallAverageMs;
  1322. pCallAverage[nIdx+1] = fCallAveragePrc;
  1323. pExclusive[nIdx] = fMsExclusive;
  1324. pExclusive[nIdx+1] = fPrcExclusive;
  1325. pAverageExclusive[nIdx] = fAverageMsExclusive;
  1326. pAverageExclusive[nIdx+1] = fAveragePrcExclusive;
  1327. pMaxExclusive[nIdx] = fMaxMsExclusive;
  1328. pMaxExclusive[nIdx+1] = fMaxPrcExclusive;
  1329. }
  1330. nCount += 2;
  1331. }
  1332. }
  1333. }
  1334. nMask <<= 1ll;
  1335. }
  1336. }
  1337. #define SBUF_MAX 32
  1338. inline void MicroProfileDrawBarArrayCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1339. {
  1340. const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT;
  1341. const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH);
  1342. const float fWidth = (float)MICROPROFILE_BAR_WIDTH;
  1343. float* pTimers = ((float**)pExtra)[0];
  1344. float* pTimers2 = ((float**)pExtra)[1];
  1345. MicroProfile& S = *MicroProfileGet();
  1346. char sBuffer[SBUF_MAX];
  1347. if (pTimers2 && pTimers2[nIdx] > 0.1f)
  1348. snprintf(sBuffer, SBUF_MAX-1, "%5.2f %3.1fx", pTimers[nIdx], pTimers[nIdx] / pTimers2[nIdx]);
  1349. else
  1350. snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pTimers[nIdx]);
  1351. if (!pTimers2)
  1352. MicroProfileDrawBox(nX + nTextWidth, nY, nX + nTextWidth + fWidth * pTimers[nIdx+1], nY + nHeight, UI.nOpacityForeground|S.TimerInfo[nTimer].nColor, MicroProfileBoxTypeBar);
  1353. MicroProfileDrawText(nX, nY, (uint32_t)-1, sBuffer, (uint32_t)strlen(sBuffer));
  1354. }
  1355. inline uint32_t MicroProfileDrawBarArray(int32_t nX, int32_t nY, float* pTimers, const char* pName, uint32_t nTotalHeight, float* pTimers2 = NULL)
  1356. {
  1357. const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH);
  1358. const uint32_t nWidth = MICROPROFILE_BAR_WIDTH;
  1359. MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1360. float* pTimersArray[2] = {pTimers, pTimers2};
  1361. MicroProfileLoopActiveGroupsDraw(nX, nY, pName, MicroProfileDrawBarArrayCallback, pTimersArray);
  1362. MicroProfileDrawHeader(nX, nTextWidth + nWidth, pName);
  1363. return nWidth + 5 + nTextWidth;
  1364. }
  1365. inline void MicroProfileDrawBarCallCountCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1366. {
  1367. MicroProfile& S = *MicroProfileGet();
  1368. char sBuffer[SBUF_MAX];
  1369. int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5d", S.Frame[nTimer].nCount);//fix
  1370. MicroProfileDrawText(nX, nY, (uint32_t)-1, sBuffer, nLen);
  1371. }
  1372. inline uint32_t MicroProfileDrawBarCallCount(int32_t nX, int32_t nY, const char* pName)
  1373. {
  1374. MicroProfileLoopActiveGroupsDraw(nX, nY, pName, MicroProfileDrawBarCallCountCallback, 0);
  1375. const uint32_t nTextWidth = 6 * MICROPROFILE_TEXT_WIDTH;
  1376. MicroProfileDrawHeader(nX, 5 + nTextWidth, pName);
  1377. return 5 + nTextWidth;
  1378. }
  1379. struct MicroProfileMetaAverageArgs
  1380. {
  1381. uint64_t* pCounters;
  1382. float fRcpFrames;
  1383. };
  1384. inline void MicroProfileDrawBarMetaAverageCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1385. {
  1386. MicroProfileMetaAverageArgs* pArgs = (MicroProfileMetaAverageArgs*)pExtra;
  1387. uint64_t* pCounters = pArgs->pCounters;
  1388. float fRcpFrames = pArgs->fRcpFrames;
  1389. char sBuffer[SBUF_MAX];
  1390. int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pCounters[nTimer] * fRcpFrames);
  1391. MicroProfileDrawText(nX - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, (uint32_t)-1, sBuffer, nLen);
  1392. }
  1393. inline uint32_t MicroProfileDrawBarMetaAverage(int32_t nX, int32_t nY, uint64_t* pCounters, const char* pName, uint32_t nTotalHeight)
  1394. {
  1395. if(!pName)
  1396. return 0;
  1397. MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1398. uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax<uint32_t>(6, (uint32_t)strlen(pName));
  1399. float fRcpFrames = 1.f / (MicroProfileGet()->nAggregateFrames ? MicroProfileGet()->nAggregateFrames : 1);
  1400. MicroProfileMetaAverageArgs Args = {pCounters, fRcpFrames};
  1401. MicroProfileLoopActiveGroupsDraw(nX + nTextWidth, nY, pName, MicroProfileDrawBarMetaAverageCallback, &Args);
  1402. MicroProfileDrawHeader(nX, 5 + nTextWidth, pName);
  1403. return 5 + nTextWidth;
  1404. }
  1405. inline void MicroProfileDrawBarMetaCountCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1406. {
  1407. uint64_t* pCounters = (uint64_t*)pExtra;
  1408. char sBuffer[SBUF_MAX];
  1409. int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5llu", pCounters[nTimer]);
  1410. MicroProfileDrawText(nX - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, (uint32_t)-1, sBuffer, nLen);
  1411. }
  1412. inline uint32_t MicroProfileDrawBarMetaCount(int32_t nX, int32_t nY, uint64_t* pCounters, const char* pName, uint32_t nTotalHeight)
  1413. {
  1414. if(!pName)
  1415. return 0;
  1416. MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1417. uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax<uint32_t>(6, (uint32_t)strlen(pName));
  1418. MicroProfileLoopActiveGroupsDraw(nX + nTextWidth, nY, pName, MicroProfileDrawBarMetaCountCallback, pCounters);
  1419. MicroProfileDrawHeader(nX, 5 + nTextWidth, pName);
  1420. return 5 + nTextWidth;
  1421. }
  1422. inline void MicroProfileDrawBarLegendCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1423. {
  1424. MicroProfile& S = *MicroProfileGet();
  1425. if (S.TimerInfo[nTimer].bGraph)
  1426. {
  1427. MicroProfileDrawText(nX, nY, S.TimerInfo[nTimer].nColor, ">", 1);
  1428. }
  1429. MicroProfileDrawTextRight(nX, nY, S.TimerInfo[nTimer].nColor, S.TimerInfo[nTimer].pName, (uint32_t)strlen(S.TimerInfo[nTimer].pName));
  1430. if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT+1)
  1431. {
  1432. UI.nHoverToken = nTimer;
  1433. UI.nHoverTime = 0;
  1434. }
  1435. }
  1436. inline uint32_t MicroProfileDrawBarLegend(int32_t nX, int32_t nY, uint32_t nTotalHeight, uint32_t nMaxWidth)
  1437. {
  1438. MicroProfileDrawLineVertical(nX-5, nY, nTotalHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1439. MicroProfileLoopActiveGroupsDraw(nMaxWidth, nY, 0, MicroProfileDrawBarLegendCallback, 0);
  1440. return nX;
  1441. }
  1442. bool MicroProfileDrawGraph(uint32_t nScreenWidth, uint32_t nScreenHeight)
  1443. {
  1444. MicroProfile& S = *MicroProfileGet();
  1445. MICROPROFILE_SCOPE(g_MicroProfileDrawGraph);
  1446. bool bEnabled = false;
  1447. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1448. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  1449. bEnabled = true;
  1450. if(!bEnabled)
  1451. return false;
  1452. uint32_t nX = nScreenWidth - MICROPROFILE_GRAPH_WIDTH;
  1453. uint32_t nY = nScreenHeight - MICROPROFILE_GRAPH_HEIGHT;
  1454. MicroProfileDrawBox(nX, nY, nX + MICROPROFILE_GRAPH_WIDTH, nY + MICROPROFILE_GRAPH_HEIGHT, 0x88000000 | g_nMicroProfileBackColors[0]);
  1455. bool bMouseOver = UI.nMouseX >= nX && UI.nMouseY >= nY;
  1456. float fMouseXPrc =(float(UI.nMouseX - nX)) / MICROPROFILE_GRAPH_WIDTH;
  1457. if(bMouseOver)
  1458. {
  1459. float fXAvg = fMouseXPrc * MICROPROFILE_GRAPH_WIDTH + nX;
  1460. MicroProfileDrawLineVertical(fXAvg, nY, nY + MICROPROFILE_GRAPH_HEIGHT, (uint32_t)-1);
  1461. }
  1462. float fY = (float)nScreenHeight;
  1463. float fDX = MICROPROFILE_GRAPH_WIDTH * 1.f / MICROPROFILE_GRAPH_HISTORY;
  1464. float fDY = MICROPROFILE_GRAPH_HEIGHT;
  1465. uint32_t nPut = S.nGraphPut;
  1466. float* pGraphData = (float*)alloca(sizeof(float)* MICROPROFILE_GRAPH_HISTORY*2);
  1467. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1468. {
  1469. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  1470. {
  1471. uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken);
  1472. bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu;
  1473. float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  1474. float fToPrc = fToMs * S.fRcpReferenceTime * 3 / 4;
  1475. float fX = (float)nX;
  1476. for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j)
  1477. {
  1478. float fWeigth = MicroProfileMin(fToPrc * (S.Graph[i].nHistory[(j+nPut)%MICROPROFILE_GRAPH_HISTORY]), 1.f);
  1479. pGraphData[(j*2)] = fX;
  1480. pGraphData[(j*2)+1] = fY - fDY * fWeigth;
  1481. fX += fDX;
  1482. }
  1483. MicroProfileDrawLine2D(MICROPROFILE_GRAPH_HISTORY, pGraphData, S.TimerInfo[MicroProfileGetTimerIndex(S.Graph[i].nToken)].nColor);
  1484. }
  1485. }
  1486. {
  1487. float fY1 = 0.25f * MICROPROFILE_GRAPH_HEIGHT + nY;
  1488. float fY2 = 0.50f * MICROPROFILE_GRAPH_HEIGHT + nY;
  1489. float fY3 = 0.75f * MICROPROFILE_GRAPH_HEIGHT + nY;
  1490. MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY1, 0xffdd4444);
  1491. MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY2, 0xff000000| g_nMicroProfileBackColors[0]);
  1492. MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY3, 0xff000000|g_nMicroProfileBackColors[0]);
  1493. char buf[32];
  1494. int nLen = snprintf(buf, sizeof(buf)-1, "%5.2fms", S.fReferenceTime);
  1495. MicroProfileDrawText(nX+1, fY1 - (2+MICROPROFILE_TEXT_HEIGHT), (uint32_t)-1, buf, nLen);
  1496. }
  1497. if(bMouseOver)
  1498. {
  1499. uint32_t pColors[MICROPROFILE_MAX_GRAPHS];
  1500. MicroProfileStringArray Strings;
  1501. MicroProfileStringArrayClear(&Strings);
  1502. uint32_t nTextCount = 0;
  1503. uint32_t nGraphIndex = (S.nGraphPut + MICROPROFILE_GRAPH_HISTORY - int(MICROPROFILE_GRAPH_HISTORY*(1.f - fMouseXPrc))) % MICROPROFILE_GRAPH_HISTORY;
  1504. uint32_t nX = UI.nMouseX;
  1505. uint32_t nY = UI.nMouseY + 20;
  1506. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1507. {
  1508. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  1509. {
  1510. uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken);
  1511. bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu;
  1512. float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  1513. uint32_t nIndex = MicroProfileGetTimerIndex(S.Graph[i].nToken);
  1514. uint32_t nColor = S.TimerInfo[nIndex].nColor;
  1515. const char* pName = S.TimerInfo[nIndex].pName;
  1516. pColors[nTextCount++] = nColor;
  1517. MicroProfileStringArrayAddLiteral(&Strings, pName);
  1518. MicroProfileStringArrayFormat(&Strings, "%5.2fms", fToMs * (S.Graph[i].nHistory[nGraphIndex]));
  1519. }
  1520. }
  1521. if(nTextCount)
  1522. {
  1523. MicroProfileDrawFloatWindow(nX, nY, Strings.ppStrings, Strings.nNumStrings, 0, pColors);
  1524. }
  1525. if(UI.nMouseRight)
  1526. {
  1527. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1528. {
  1529. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  1530. }
  1531. }
  1532. }
  1533. return bMouseOver;
  1534. }
  1535. void MicroProfileDumpTimers()
  1536. {
  1537. MicroProfile& S = *MicroProfileGet();
  1538. uint64_t nActiveGroup = S.nGroupMask;
  1539. uint32_t nNumTimers = S.nTotalTimers;
  1540. uint32_t nBlockSize = 2 * nNumTimers;
  1541. float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float));
  1542. float* pAverage = pTimers + nBlockSize;
  1543. float* pMax = pTimers + 2 * nBlockSize;
  1544. float* pCallAverage = pTimers + 3 * nBlockSize;
  1545. float* pTimersExclusive = pTimers + 4 * nBlockSize;
  1546. float* pAverageExclusive = pTimers + 5 * nBlockSize;
  1547. float* pMaxExclusive = pTimers + 6 * nBlockSize;
  1548. MicroProfileCalcTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nNumTimers);
  1549. MICROPROFILE_PRINTF("%11s, ", "Time");
  1550. MICROPROFILE_PRINTF("%11s, ", "Average");
  1551. MICROPROFILE_PRINTF("%11s, ", "Max");
  1552. MICROPROFILE_PRINTF("%11s, ", "Call Avg");
  1553. MICROPROFILE_PRINTF("%9s, ", "Count");
  1554. MICROPROFILE_PRINTF("%11s, ", "Excl");
  1555. MICROPROFILE_PRINTF("%11s, ", "Avg Excl");
  1556. MICROPROFILE_PRINTF("%11s, \n", "Max Excl");
  1557. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1558. {
  1559. uint64_t nMask = 1ll << j;
  1560. if(nMask & nActiveGroup)
  1561. {
  1562. MICROPROFILE_PRINTF("%s\n", S.GroupInfo[j].pName);
  1563. for(uint32_t i = 0; i < S.nTotalTimers;++i)
  1564. {
  1565. uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken);
  1566. if(nTokenMask & nMask)
  1567. {
  1568. uint32_t nIdx = i * 2;
  1569. MICROPROFILE_PRINTF("%9.2fms, ", pTimers[nIdx]);
  1570. MICROPROFILE_PRINTF("%9.2fms, ", pAverage[nIdx]);
  1571. MICROPROFILE_PRINTF("%9.2fms, ", pMax[nIdx]);
  1572. MICROPROFILE_PRINTF("%9.2fms, ", pCallAverage[nIdx]);
  1573. MICROPROFILE_PRINTF("%9d, ", S.Frame[i].nCount);
  1574. MICROPROFILE_PRINTF("%9.2fms, ", pTimersExclusive[nIdx]);
  1575. MICROPROFILE_PRINTF("%9.2fms, ", pAverageExclusive[nIdx]);
  1576. MICROPROFILE_PRINTF("%9.2fms, ", pMaxExclusive[nIdx]);
  1577. MICROPROFILE_PRINTF("%s\n", S.TimerInfo[i].pName);
  1578. }
  1579. }
  1580. }
  1581. }
  1582. }
  1583. inline void MicroProfileDrawBarView(uint32_t nScreenWidth, uint32_t nScreenHeight)
  1584. {
  1585. MicroProfile& S = *MicroProfileGet();
  1586. uint64_t nActiveGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted;
  1587. if(!nActiveGroup)
  1588. return;
  1589. MICROPROFILE_SCOPE(g_MicroProfileDrawBarView);
  1590. const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT;
  1591. int nColorIndex = 0;
  1592. uint32_t nMaxTimerNameLen = 1;
  1593. uint32_t nNumTimers = 0;
  1594. uint32_t nNumGroups = 0;
  1595. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1596. {
  1597. if(nActiveGroup & (1ll << j))
  1598. {
  1599. nNumTimers += S.GroupInfo[j].nNumTimers;
  1600. nNumGroups += 1;
  1601. nMaxTimerNameLen = MicroProfileMax(nMaxTimerNameLen, S.GroupInfo[j].nMaxTimerNameLen);
  1602. }
  1603. }
  1604. uint32_t nTimerWidth = 2+(4+nMaxTimerNameLen) * (MICROPROFILE_TEXT_WIDTH+1);
  1605. uint32_t nX = nTimerWidth + UI.nOffsetX;
  1606. uint32_t nY = nHeight + 3 - UI.nOffsetY;
  1607. uint32_t nBlockSize = 2 * nNumTimers;
  1608. float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float));
  1609. float* pAverage = pTimers + nBlockSize;
  1610. float* pMax = pTimers + 2 * nBlockSize;
  1611. float* pCallAverage = pTimers + 3 * nBlockSize;
  1612. float* pTimersExclusive = pTimers + 4 * nBlockSize;
  1613. float* pAverageExclusive = pTimers + 5 * nBlockSize;
  1614. float* pMaxExclusive = pTimers + 6 * nBlockSize;
  1615. MicroProfileCalcTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nNumTimers);
  1616. uint32_t nWidth = 0;
  1617. {
  1618. uint32_t nMetaIndex = 0;
  1619. for(uint32_t i = 1; i ; i <<= 1)
  1620. {
  1621. if(S.nBars & i)
  1622. {
  1623. if(i >= MP_DRAW_META_FIRST)
  1624. {
  1625. if(nMetaIndex < MICROPROFILE_META_MAX && S.MetaCounters[nMetaIndex].pName)
  1626. {
  1627. uint32_t nStrWidth = static_cast<uint32_t>(strlen(S.MetaCounters[nMetaIndex].pName));
  1628. if(S.nBars & MP_DRAW_TIMERS)
  1629. nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth);
  1630. if(S.nBars & MP_DRAW_AVERAGE)
  1631. nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4);
  1632. if(S.nBars & MP_DRAW_MAX)
  1633. nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4);
  1634. }
  1635. }
  1636. else
  1637. {
  1638. nWidth += MICROPROFILE_BAR_WIDTH + 6 + 6 * (1+MICROPROFILE_TEXT_WIDTH);
  1639. if(i & MP_DRAW_CALL_COUNT)
  1640. nWidth += 6 + 6 * MICROPROFILE_TEXT_WIDTH;
  1641. }
  1642. }
  1643. if(i >= MP_DRAW_META_FIRST)
  1644. {
  1645. ++nMetaIndex;
  1646. }
  1647. }
  1648. nWidth += (1+nMaxTimerNameLen) * (MICROPROFILE_TEXT_WIDTH+1);
  1649. for(uint32_t i = 0; i < nNumTimers+nNumGroups+1; ++i)
  1650. {
  1651. uint32_t nY0 = nY + i * (nHeight + 1);
  1652. bool bInside = (UI.nActiveMenu == -1) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nHeight + 1)));
  1653. MicroProfileDrawBox(nX, nY0, nWidth+nX, nY0 + (nHeight+1)+1, UI.nOpacityBackground | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0)));
  1654. }
  1655. nX += 10;
  1656. }
  1657. int nTotalHeight = (nNumTimers+nNumGroups+1) * (nHeight+1);
  1658. uint32_t nLegendOffset = 1;
  1659. if(S.nBars & MP_DRAW_TIMERS)
  1660. nX += MicroProfileDrawBarArray(nX, nY, pTimers, "Time", nTotalHeight) + 1;
  1661. if(S.nBars & MP_DRAW_AVERAGE)
  1662. nX += MicroProfileDrawBarArray(nX, nY, pAverage, "Average", nTotalHeight) + 1;
  1663. if(S.nBars & MP_DRAW_MAX)
  1664. nX += MicroProfileDrawBarArray(nX, nY, pMax, (!UI.bShowSpikes) ? "Max Time" : "Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverage : NULL) + 1;
  1665. if(S.nBars & MP_DRAW_CALL_COUNT)
  1666. {
  1667. nX += MicroProfileDrawBarArray(nX, nY, pCallAverage, "Call Average", nTotalHeight) + 1;
  1668. nX += MicroProfileDrawBarCallCount(nX, nY, "Count") + 1;
  1669. }
  1670. if(S.nBars & MP_DRAW_TIMERS_EXCLUSIVE)
  1671. nX += MicroProfileDrawBarArray(nX, nY, pTimersExclusive, "Exclusive Time", nTotalHeight) + 1;
  1672. if(S.nBars & MP_DRAW_AVERAGE_EXCLUSIVE)
  1673. nX += MicroProfileDrawBarArray(nX, nY, pAverageExclusive, "Exclusive Average", nTotalHeight) + 1;
  1674. if(S.nBars & MP_DRAW_MAX_EXCLUSIVE)
  1675. nX += MicroProfileDrawBarArray(nX, nY, pMaxExclusive, (!UI.bShowSpikes) ? "Exclusive Max Time" :"Excl Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverageExclusive : NULL) + 1;
  1676. for(int i = 0; i < MICROPROFILE_META_MAX; ++i)
  1677. {
  1678. if(0 != (S.nBars & (MP_DRAW_META_FIRST<<i)) && S.MetaCounters[i].pName)
  1679. {
  1680. uint32_t nBufferSize = static_cast<uint32_t>(strlen(S.MetaCounters[i].pName) + 32);
  1681. char* buffer = (char*)alloca(nBufferSize);
  1682. if(S.nBars & MP_DRAW_TIMERS)
  1683. nX += MicroProfileDrawBarMetaCount(nX, nY, &S.MetaCounters[i].nCounters[0], S.MetaCounters[i].pName, nTotalHeight) + 1;
  1684. if(S.nBars & MP_DRAW_AVERAGE)
  1685. {
  1686. snprintf(buffer, nBufferSize-1, "%s Avg", S.MetaCounters[i].pName);
  1687. nX += MicroProfileDrawBarMetaAverage(nX, nY, &S.MetaCounters[i].nAggregate[0], buffer, nTotalHeight) + 1;
  1688. }
  1689. if(S.nBars & MP_DRAW_MAX)
  1690. {
  1691. snprintf(buffer, nBufferSize-1, "%s Max", S.MetaCounters[i].pName);
  1692. nX += MicroProfileDrawBarMetaCount(nX, nY, &S.MetaCounters[i].nAggregateMax[0], buffer, nTotalHeight) + 1;
  1693. }
  1694. }
  1695. }
  1696. nX = 0;
  1697. nY = nHeight + 3 - UI.nOffsetY;
  1698. for(uint32_t i = 0; i < nNumTimers+nNumGroups+1; ++i)
  1699. {
  1700. uint32_t nY0 = nY + i * (nHeight + 1);
  1701. bool bInside = (UI.nActiveMenu == -1) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nHeight + 1)));
  1702. MicroProfileDrawBox(nX, nY0, nTimerWidth, nY0 + (nHeight+1)+1, 0xff0000000 | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0)));
  1703. }
  1704. nX += MicroProfileDrawBarLegend(nX, nY, nTotalHeight, nTimerWidth-5) + 1;
  1705. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1706. {
  1707. if(nActiveGroup & (1ll << j))
  1708. {
  1709. MicroProfileDrawText(nX, nY + (1+nHeight) * nLegendOffset, (uint32_t)-1, S.GroupInfo[j].pName, S.GroupInfo[j].nNameLen);
  1710. nLegendOffset += S.GroupInfo[j].nNumTimers+1;
  1711. }
  1712. }
  1713. MicroProfileDrawHeader(nX, nTimerWidth-5, "Group");
  1714. MicroProfileDrawTextRight(nTimerWidth-3, MICROPROFILE_TEXT_HEIGHT + 2, (uint32_t)-1, "Timer", 5);
  1715. MicroProfileDrawLineVertical(nTimerWidth, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1716. MicroProfileDrawLineHorizontal(0, nWidth, 2*MICROPROFILE_TEXT_HEIGHT + 3, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1717. }
  1718. typedef const char* (*MicroProfileSubmenuCallback)(int, bool* bSelected);
  1719. typedef void (*MicroProfileClickCallback)(int);
  1720. inline const char* MicroProfileUIMenuMode(int nIndex, bool* bSelected)
  1721. {
  1722. MicroProfile& S = *MicroProfileGet();
  1723. switch(nIndex)
  1724. {
  1725. case 0:
  1726. *bSelected = S.nDisplay == MP_DRAW_DETAILED;
  1727. return "Detailed";
  1728. case 1:
  1729. *bSelected = S.nDisplay == MP_DRAW_BARS;
  1730. return "Timers";
  1731. case 2:
  1732. *bSelected = S.nDisplay == MP_DRAW_HIDDEN;
  1733. return "Hidden";
  1734. case 3:
  1735. *bSelected = true;
  1736. return "Off";
  1737. case 4:
  1738. *bSelected = true;
  1739. return "------";
  1740. case 5:
  1741. *bSelected = S.nForceEnable != 0;
  1742. return "Force Enable";
  1743. default: return 0;
  1744. }
  1745. }
  1746. inline const char* MicroProfileUIMenuGroups(int nIndex, bool* bSelected)
  1747. {
  1748. MicroProfile& S = *MicroProfileGet();
  1749. *bSelected = false;
  1750. if(nIndex == 0)
  1751. {
  1752. *bSelected = S.nAllGroupsWanted != 0;
  1753. return "[ALL]";
  1754. }
  1755. else
  1756. {
  1757. nIndex = nIndex-1;
  1758. if(static_cast<uint32_t>(nIndex) < UI.GroupMenuCount)
  1759. {
  1760. MicroProfileGroupMenuItem& Item = UI.GroupMenu[nIndex];
  1761. static char buffer[MICROPROFILE_NAME_MAX_LEN+32];
  1762. if(Item.nIsCategory)
  1763. {
  1764. uint64_t nGroupMask = S.CategoryInfo[Item.nIndex].nGroupMask;
  1765. *bSelected = nGroupMask == (nGroupMask & S.nActiveGroupWanted);
  1766. snprintf(buffer, sizeof(buffer)-1, "[%s]", Item.pName);
  1767. }
  1768. else
  1769. {
  1770. *bSelected = 0 != (S.nActiveGroupWanted & (1ll << Item.nIndex));
  1771. snprintf(buffer, sizeof(buffer)-1, " %s", Item.pName);
  1772. }
  1773. return buffer;
  1774. }
  1775. return 0;
  1776. }
  1777. }
  1778. inline const char* MicroProfileUIMenuAggregate(int nIndex, bool* bSelected)
  1779. {
  1780. MicroProfile& S = *MicroProfileGet();
  1781. if(nIndex < sizeof(g_MicroProfileAggregatePresets)/sizeof(g_MicroProfileAggregatePresets[0]))
  1782. {
  1783. int val = g_MicroProfileAggregatePresets[nIndex];
  1784. *bSelected = (int)S.nAggregateFlip == val;
  1785. if(0 == val)
  1786. return "Infinite";
  1787. else
  1788. {
  1789. static char buf[128];
  1790. snprintf(buf, sizeof(buf)-1, "%7d", val);
  1791. return buf;
  1792. }
  1793. }
  1794. return 0;
  1795. }
  1796. inline const char* MicroProfileUIMenuTimers(int nIndex, bool* bSelected)
  1797. {
  1798. MicroProfile& S = *MicroProfileGet();
  1799. *bSelected = 0 != (S.nBars & (1 << nIndex));
  1800. switch(nIndex)
  1801. {
  1802. case 0: return "Time";
  1803. case 1: return "Average";
  1804. case 2: return "Max";
  1805. case 3: return "Call Count";
  1806. case 4: return "Exclusive Timers";
  1807. case 5: return "Exclusive Average";
  1808. case 6: return "Exclusive Max";
  1809. }
  1810. int nMetaIndex = nIndex - 7;
  1811. if(nMetaIndex < MICROPROFILE_META_MAX)
  1812. {
  1813. return S.MetaCounters[nMetaIndex].pName;
  1814. }
  1815. return 0;
  1816. }
  1817. inline const char* MicroProfileUIMenuOptions(int nIndex, bool* bSelected)
  1818. {
  1819. MicroProfile& S = *MicroProfileGet();
  1820. if(nIndex >= MICROPROFILE_OPTION_SIZE) return 0;
  1821. switch(UI.Options[nIndex].nSubType)
  1822. {
  1823. case 0:
  1824. *bSelected = S.fReferenceTime == g_MicroProfileReferenceTimePresets[UI.Options[nIndex].nIndex];
  1825. break;
  1826. case 1:
  1827. *bSelected = UI.nOpacityBackground>>24 == g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex];
  1828. break;
  1829. case 2:
  1830. *bSelected = UI.nOpacityForeground>>24 == g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex];
  1831. break;
  1832. case 3:
  1833. *bSelected = UI.bShowSpikes;
  1834. break;
  1835. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  1836. case 4:
  1837. {
  1838. switch(UI.Options[nIndex].nIndex)
  1839. {
  1840. case 0:
  1841. *bSelected = S.bContextSwitchRunning;
  1842. break;
  1843. case 1:
  1844. *bSelected = S.bContextSwitchAllThreads;
  1845. break;
  1846. case 2:
  1847. *bSelected = S.bContextSwitchNoBars;
  1848. break;
  1849. }
  1850. }
  1851. break;
  1852. #endif
  1853. }
  1854. return UI.Options[nIndex].Text;
  1855. }
  1856. inline const char* MicroProfileUIMenuPreset(int nIndex, bool* bSelected)
  1857. {
  1858. static char buf[128];
  1859. *bSelected = false;
  1860. int nNumPresets = sizeof(g_MicroProfilePresetNames) / sizeof(g_MicroProfilePresetNames[0]);
  1861. int nIndexSave = nIndex - nNumPresets - 1;
  1862. if(nIndex == nNumPresets)
  1863. return "--";
  1864. else if(nIndexSave >=0 && nIndexSave <nNumPresets)
  1865. {
  1866. snprintf(buf, sizeof(buf)-1, "Save '%s'", g_MicroProfilePresetNames[nIndexSave]);
  1867. return buf;
  1868. }
  1869. else if(nIndex < nNumPresets)
  1870. {
  1871. snprintf(buf, sizeof(buf)-1, "Load '%s'", g_MicroProfilePresetNames[nIndex]);
  1872. return buf;
  1873. }
  1874. else
  1875. {
  1876. return 0;
  1877. }
  1878. }
  1879. inline const char* MicroProfileUIMenuCustom(int nIndex, bool* bSelected)
  1880. {
  1881. if((uint32_t)-1 == UI.nCustomActive)
  1882. {
  1883. *bSelected = nIndex == 0;
  1884. }
  1885. else
  1886. {
  1887. *bSelected = nIndex-2 == UI.nCustomActive;
  1888. }
  1889. switch(nIndex)
  1890. {
  1891. case 0: return "Disable";
  1892. case 1: return "--";
  1893. default:
  1894. nIndex -= 2;
  1895. if(static_cast<uint32_t>(nIndex) < UI.nCustomCount)
  1896. {
  1897. return UI.Custom[nIndex].pName;
  1898. }
  1899. else
  1900. {
  1901. return 0;
  1902. }
  1903. }
  1904. }
  1905. inline const char* MicroProfileUIMenuEmpty(int nIndex, bool* bSelected)
  1906. {
  1907. return 0;
  1908. }
  1909. inline void MicroProfileUIClickMode(int nIndex)
  1910. {
  1911. MicroProfile& S = *MicroProfileGet();
  1912. switch(nIndex)
  1913. {
  1914. case 0:
  1915. S.nDisplay = MP_DRAW_DETAILED;
  1916. break;
  1917. case 1:
  1918. S.nDisplay = MP_DRAW_BARS;
  1919. break;
  1920. case 2:
  1921. S.nDisplay = MP_DRAW_HIDDEN;
  1922. break;
  1923. case 3:
  1924. S.nDisplay = 0;
  1925. break;
  1926. case 4:
  1927. break;
  1928. case 5:
  1929. S.nForceEnable = !S.nForceEnable;
  1930. break;
  1931. }
  1932. }
  1933. inline void MicroProfileUIClickGroups(int nIndex)
  1934. {
  1935. MicroProfile& S = *MicroProfileGet();
  1936. if(nIndex == 0)
  1937. S.nAllGroupsWanted = 1-S.nAllGroupsWanted;
  1938. else
  1939. {
  1940. nIndex -= 1;
  1941. if(static_cast<uint32_t>(nIndex) < UI.GroupMenuCount)
  1942. {
  1943. MicroProfileGroupMenuItem& Item = UI.GroupMenu[nIndex];
  1944. if(Item.nIsCategory)
  1945. {
  1946. uint64_t nGroupMask = S.CategoryInfo[Item.nIndex].nGroupMask;
  1947. if(nGroupMask != (nGroupMask & S.nActiveGroupWanted))
  1948. {
  1949. S.nActiveGroupWanted |= nGroupMask;
  1950. }
  1951. else
  1952. {
  1953. S.nActiveGroupWanted &= ~nGroupMask;
  1954. }
  1955. }
  1956. else
  1957. {
  1958. MP_ASSERT(Item.nIndex < S.nGroupCount);
  1959. S.nActiveGroupWanted ^= (1ll << Item.nIndex);
  1960. }
  1961. }
  1962. }
  1963. }
  1964. inline void MicroProfileUIClickAggregate(int nIndex)
  1965. {
  1966. MicroProfile& S = *MicroProfileGet();
  1967. S.nAggregateFlip = g_MicroProfileAggregatePresets[nIndex];
  1968. if(0 == S.nAggregateFlip)
  1969. {
  1970. S.nAggregateClear = 1;
  1971. }
  1972. }
  1973. inline void MicroProfileUIClickTimers(int nIndex)
  1974. {
  1975. MicroProfile& S = *MicroProfileGet();
  1976. S.nBars ^= (1 << nIndex);
  1977. }
  1978. inline void MicroProfileUIClickOptions(int nIndex)
  1979. {
  1980. MicroProfile& S = *MicroProfileGet();
  1981. switch(UI.Options[nIndex].nSubType)
  1982. {
  1983. case 0:
  1984. S.fReferenceTime = g_MicroProfileReferenceTimePresets[UI.Options[nIndex].nIndex];
  1985. S.fRcpReferenceTime = 1.f / S.fReferenceTime;
  1986. break;
  1987. case 1:
  1988. UI.nOpacityBackground = g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex]<<24;
  1989. break;
  1990. case 2:
  1991. UI.nOpacityForeground = g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex]<<24;
  1992. break;
  1993. case 3:
  1994. UI.bShowSpikes = !UI.bShowSpikes;
  1995. break;
  1996. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  1997. case 4:
  1998. {
  1999. switch(UI.Options[nIndex].nIndex)
  2000. {
  2001. case 0:
  2002. if(S.bContextSwitchRunning)
  2003. {
  2004. MicroProfileStopContextSwitchTrace();
  2005. }
  2006. else
  2007. {
  2008. MicroProfileStartContextSwitchTrace();
  2009. }
  2010. break;
  2011. case 1:
  2012. S.bContextSwitchAllThreads = !S.bContextSwitchAllThreads;
  2013. break;
  2014. case 2:
  2015. S.bContextSwitchNoBars= !S.bContextSwitchNoBars;
  2016. break;
  2017. }
  2018. }
  2019. break;
  2020. #endif
  2021. }
  2022. }
  2023. inline void MicroProfileUIClickPreset(int nIndex)
  2024. {
  2025. int nNumPresets = sizeof(g_MicroProfilePresetNames) / sizeof(g_MicroProfilePresetNames[0]);
  2026. int nIndexSave = nIndex - nNumPresets - 1;
  2027. if(nIndexSave >= 0 && nIndexSave < nNumPresets)
  2028. {
  2029. MicroProfileSavePreset(g_MicroProfilePresetNames[nIndexSave]);
  2030. }
  2031. else if(nIndex >= 0 && nIndex < nNumPresets)
  2032. {
  2033. MicroProfileLoadPreset(g_MicroProfilePresetNames[nIndex]);
  2034. }
  2035. }
  2036. inline void MicroProfileUIClickCustom(int nIndex)
  2037. {
  2038. if(nIndex == 0)
  2039. {
  2040. MicroProfileCustomGroupDisable();
  2041. }
  2042. else
  2043. {
  2044. MicroProfileCustomGroupEnable(nIndex-2);
  2045. }
  2046. }
  2047. inline void MicroProfileUIClickEmpty(int nIndex)
  2048. {
  2049. }
  2050. inline void MicroProfileDrawMenu(uint32_t nWidth, uint32_t nHeight)
  2051. {
  2052. MicroProfile& S = *MicroProfileGet();
  2053. uint32_t nX = 0;
  2054. uint32_t nY = 0;
  2055. bool bMouseOver = UI.nMouseY < MICROPROFILE_TEXT_HEIGHT + 1;
  2056. #define SBUF_SIZE 256
  2057. char buffer[256];
  2058. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + (MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff000000|g_nMicroProfileBackColors[1]);
  2059. #define MICROPROFILE_MENU_MAX 16
  2060. const char* pMenuText[MICROPROFILE_MENU_MAX] = {0};
  2061. uint32_t nMenuX[MICROPROFILE_MENU_MAX] = {0};
  2062. uint32_t nNumMenuItems = 0;
  2063. int nLen = snprintf(buffer, 127, "MicroProfile");
  2064. MicroProfileDrawText(nX, nY, (uint32_t)-1, buffer, nLen);
  2065. nX += (sizeof("MicroProfile")+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2066. pMenuText[nNumMenuItems++] = "Mode";
  2067. pMenuText[nNumMenuItems++] = "Groups";
  2068. char AggregateText[64];
  2069. snprintf(AggregateText, sizeof(AggregateText)-1, "Aggregate[%d]", S.nAggregateFlip ? S.nAggregateFlip : S.nAggregateFlipCount);
  2070. pMenuText[nNumMenuItems++] = &AggregateText[0];
  2071. pMenuText[nNumMenuItems++] = "Timers";
  2072. pMenuText[nNumMenuItems++] = "Options";
  2073. pMenuText[nNumMenuItems++] = "Preset";
  2074. pMenuText[nNumMenuItems++] = "Custom";
  2075. const int nPauseIndex = nNumMenuItems;
  2076. pMenuText[nNumMenuItems++] = S.nRunning ? "Pause" : "Unpause";
  2077. pMenuText[nNumMenuItems++] = "Help";
  2078. if(S.nOverflow)
  2079. {
  2080. pMenuText[nNumMenuItems++] = "!BUFFERSFULL!";
  2081. }
  2082. if(UI.GroupMenuCount != S.nGroupCount + S.nCategoryCount)
  2083. {
  2084. UI.GroupMenuCount = S.nGroupCount + S.nCategoryCount;
  2085. for(uint32_t i = 0; i < S.nCategoryCount; ++i)
  2086. {
  2087. UI.GroupMenu[i].nIsCategory = 1;
  2088. UI.GroupMenu[i].nCategoryIndex = i;
  2089. UI.GroupMenu[i].nIndex = i;
  2090. UI.GroupMenu[i].pName = S.CategoryInfo[i].pName;
  2091. }
  2092. for(uint32_t i = 0; i < S.nGroupCount; ++i)
  2093. {
  2094. uint32_t idx = i + S.nCategoryCount;
  2095. UI.GroupMenu[idx].nIsCategory = 0;
  2096. UI.GroupMenu[idx].nCategoryIndex = S.GroupInfo[i].nCategory;
  2097. UI.GroupMenu[idx].nIndex = i;
  2098. UI.GroupMenu[idx].pName = S.GroupInfo[i].pName;
  2099. }
  2100. std::sort(&UI.GroupMenu[0], &UI.GroupMenu[UI.GroupMenuCount],
  2101. [] (const MicroProfileGroupMenuItem& l, const MicroProfileGroupMenuItem& r) -> bool
  2102. {
  2103. if(l.nCategoryIndex < r.nCategoryIndex)
  2104. {
  2105. return true;
  2106. }
  2107. else if(r.nCategoryIndex < l.nCategoryIndex)
  2108. {
  2109. return false;
  2110. }
  2111. if(r.nIsCategory || l.nIsCategory)
  2112. {
  2113. return l.nIsCategory > r.nIsCategory;
  2114. }
  2115. return MP_STRCASECMP(l.pName, r.pName)<0;
  2116. }
  2117. );
  2118. }
  2119. MicroProfileSubmenuCallback GroupCallback[MICROPROFILE_MENU_MAX] =
  2120. {
  2121. MicroProfileUIMenuMode,
  2122. MicroProfileUIMenuGroups,
  2123. MicroProfileUIMenuAggregate,
  2124. MicroProfileUIMenuTimers,
  2125. MicroProfileUIMenuOptions,
  2126. MicroProfileUIMenuPreset,
  2127. MicroProfileUIMenuCustom,
  2128. MicroProfileUIMenuEmpty,
  2129. MicroProfileUIMenuEmpty,
  2130. MicroProfileUIMenuEmpty,
  2131. };
  2132. MicroProfileClickCallback CBClick[MICROPROFILE_MENU_MAX] =
  2133. {
  2134. MicroProfileUIClickMode,
  2135. MicroProfileUIClickGroups,
  2136. MicroProfileUIClickAggregate,
  2137. MicroProfileUIClickTimers,
  2138. MicroProfileUIClickOptions,
  2139. MicroProfileUIClickPreset,
  2140. MicroProfileUIClickCustom,
  2141. MicroProfileUIClickEmpty,
  2142. MicroProfileUIClickEmpty,
  2143. MicroProfileUIClickEmpty,
  2144. };
  2145. uint32_t nSelectMenu = (uint32_t)-1;
  2146. for(uint32_t i = 0; i < nNumMenuItems; ++i)
  2147. {
  2148. nMenuX[i] = nX;
  2149. uint32_t nLen = (uint32_t)strlen(pMenuText[i]);
  2150. uint32_t nEnd = nX + nLen * (MICROPROFILE_TEXT_WIDTH+1);
  2151. if(UI.nMouseY <= MICROPROFILE_TEXT_HEIGHT && UI.nMouseX <= nEnd && UI.nMouseX >= nX)
  2152. {
  2153. MicroProfileDrawBox(nX-1, nY, nX + nLen * (MICROPROFILE_TEXT_WIDTH+1), nY +(MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff888888);
  2154. nSelectMenu = i;
  2155. if((UI.nMouseLeft || UI.nMouseRight) && i == (int)nPauseIndex)
  2156. {
  2157. S.nToggleRunning = 1;
  2158. }
  2159. }
  2160. MicroProfileDrawText(nX, nY, (uint32_t)-1, pMenuText[i], (uint32_t)strlen(pMenuText[i]));
  2161. nX += (nLen+1) * (MICROPROFILE_TEXT_WIDTH+1);
  2162. }
  2163. uint32_t nMenu = nSelectMenu != (uint32_t)-1 ? nSelectMenu : UI.nActiveMenu;
  2164. UI.nActiveMenu = nMenu;
  2165. if((uint32_t)-1 != nMenu)
  2166. {
  2167. nX = nMenuX[nMenu];
  2168. nY += MICROPROFILE_TEXT_HEIGHT+1;
  2169. MicroProfileSubmenuCallback CB = GroupCallback[nMenu];
  2170. int nNumLines = 0;
  2171. bool bSelected = false;
  2172. const char* pString = CB(nNumLines, &bSelected);
  2173. uint32_t nWidth = 0, nHeight = 0;
  2174. while(pString)
  2175. {
  2176. nWidth = MicroProfileMax<int>(nWidth, (int)strlen(pString));
  2177. nNumLines++;
  2178. pString = CB(nNumLines, &bSelected);
  2179. }
  2180. nWidth = (2+nWidth) * (MICROPROFILE_TEXT_WIDTH+1);
  2181. nHeight = nNumLines * (MICROPROFILE_TEXT_HEIGHT+1);
  2182. if(UI.nMouseY <= nY + nHeight+0 && UI.nMouseY >= nY-0 && UI.nMouseX <= nX + nWidth + 0 && UI.nMouseX >= nX - 0)
  2183. {
  2184. UI.nActiveMenu = nMenu;
  2185. }
  2186. else if(nSelectMenu == (uint32_t)-1)
  2187. {
  2188. UI.nActiveMenu = (uint32_t)-1;
  2189. }
  2190. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000|g_nMicroProfileBackColors[1]);
  2191. for(int i = 0; i < nNumLines; ++i)
  2192. {
  2193. bool bSelected = false;
  2194. const char* pString = CB(i, &bSelected);
  2195. if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT + 1)
  2196. {
  2197. bMouseOver = true;
  2198. if(UI.nMouseLeft || UI.nMouseRight)
  2199. {
  2200. CBClick[nMenu](i);
  2201. }
  2202. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + MICROPROFILE_TEXT_HEIGHT + 1, 0xff888888);
  2203. }
  2204. int nLen = snprintf(buffer, SBUF_SIZE-1, "%c %s", bSelected ? '*' : ' ' ,pString);
  2205. MicroProfileDrawText(nX, nY, (uint32_t)-1, buffer, nLen);
  2206. nY += MICROPROFILE_TEXT_HEIGHT+1;
  2207. }
  2208. }
  2209. {
  2210. static char FrameTimeMessage[64];
  2211. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  2212. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  2213. float fMs = fToMs * (S.nFlipTicks);
  2214. float fAverageMs = fToMs * (S.nFlipAggregateDisplay / nAggregateFrames);
  2215. float fMaxMs = fToMs * S.nFlipMaxDisplay;
  2216. int nLen = snprintf(FrameTimeMessage, sizeof(FrameTimeMessage)-1, "Time[%6.2f] Avg[%6.2f] Max[%6.2f]", fMs, fAverageMs, fMaxMs);
  2217. pMenuText[nNumMenuItems++] = &FrameTimeMessage[0];
  2218. MicroProfileDrawText(nWidth - nLen * (MICROPROFILE_TEXT_WIDTH+1), 0, -1, FrameTimeMessage, nLen);
  2219. }
  2220. }
  2221. inline void MicroProfileMoveGraph()
  2222. {
  2223. int nZoom = UI.nMouseWheelDelta;
  2224. int nPanX = 0;
  2225. int nPanY = 0;
  2226. static int X = 0, Y = 0;
  2227. if(UI.nMouseDownLeft && !UI.nModDown)
  2228. {
  2229. nPanX = UI.nMouseX - X;
  2230. nPanY = UI.nMouseY - Y;
  2231. }
  2232. X = UI.nMouseX;
  2233. Y = UI.nMouseY;
  2234. if(nZoom)
  2235. {
  2236. float fOldRange = UI.fDetailedRange;
  2237. if(nZoom>0)
  2238. {
  2239. UI.fDetailedRangeTarget = UI.fDetailedRange *= UI.nModDown ? 1.40f : 1.05f;
  2240. }
  2241. else
  2242. {
  2243. float fNewDetailedRange = UI.fDetailedRange / (UI.nModDown ? 1.40f : 1.05f);
  2244. if(fNewDetailedRange < 1e-4f) //100ns
  2245. fNewDetailedRange = 1e-4f;
  2246. UI.fDetailedRangeTarget = UI.fDetailedRange = fNewDetailedRange;
  2247. }
  2248. float fDiff = fOldRange - UI.fDetailedRange;
  2249. float fMousePrc = MicroProfileMax((float)UI.nMouseX / UI.nWidth ,0.f);
  2250. UI.fDetailedOffsetTarget = UI.fDetailedOffset += fDiff * fMousePrc;
  2251. }
  2252. if(nPanX)
  2253. {
  2254. UI.fDetailedOffsetTarget = UI.fDetailedOffset += -nPanX * UI.fDetailedRange / UI.nWidth;
  2255. }
  2256. UI.nOffsetY -= nPanY;
  2257. UI.nOffsetX += nPanX;
  2258. if(UI.nOffsetX > 0)
  2259. UI.nOffsetX = 0;
  2260. if(UI.nOffsetY<0)
  2261. UI.nOffsetY = 0;
  2262. }
  2263. inline void MicroProfileDrawCustom(uint32_t nWidth, uint32_t nHeight)
  2264. {
  2265. if((uint32_t)-1 != UI.nCustomActive)
  2266. {
  2267. MicroProfile& S = *MicroProfileGet();
  2268. MP_ASSERT(UI.nCustomActive < MICROPROFILE_CUSTOM_MAX);
  2269. MicroProfileCustom* pCustom = &UI.Custom[UI.nCustomActive];
  2270. uint32_t nCount = pCustom->nNumTimers;
  2271. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  2272. uint32_t nExtraOffset = 1 + ((pCustom->nFlags & MICROPROFILE_CUSTOM_STACK) != 0 ? 3 : 0);
  2273. uint32_t nOffsetYBase = nHeight - (nExtraOffset+nCount)* (1+MICROPROFILE_TEXT_HEIGHT) - MICROPROFILE_CUSTOM_PADDING;
  2274. uint32_t nOffsetY = nOffsetYBase;
  2275. float fReference = pCustom->fReference;
  2276. float fRcpReference = 1.f / fReference;
  2277. uint32_t nReducedWidth = UI.nWidth - 2*MICROPROFILE_CUSTOM_PADDING - MICROPROFILE_GRAPH_WIDTH;
  2278. char Buffer[MICROPROFILE_NAME_MAX_LEN*2+1];
  2279. float* pTime = (float*)alloca(sizeof(float)*nCount);
  2280. float* pTimeAvg = (float*)alloca(sizeof(float)*nCount);
  2281. float* pTimeMax = (float*)alloca(sizeof(float)*nCount);
  2282. uint32_t* pColors = (uint32_t*)alloca(sizeof(uint32_t)*nCount);
  2283. uint32_t nMaxOffsetX = 0;
  2284. MicroProfileDrawBox(MICROPROFILE_CUSTOM_PADDING-1, nOffsetY-1, MICROPROFILE_CUSTOM_PADDING+nReducedWidth+1, UI.nHeight - MICROPROFILE_CUSTOM_PADDING+1, 0x88000000|g_nMicroProfileBackColors[0]);
  2285. for(uint32_t i = 0; i < nCount; ++i)
  2286. {
  2287. uint16_t nTimerIndex = MicroProfileGetTimerIndex(pCustom->pTimers[i]);
  2288. uint16_t nGroupIndex = MicroProfileGetGroupIndex(pCustom->pTimers[i]);
  2289. float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[nGroupIndex].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  2290. pTime[i] = S.Frame[nTimerIndex].nTicks * fToMs;
  2291. pTimeAvg[i] = fToMs * (S.Aggregate[nTimerIndex].nTicks / nAggregateFrames);
  2292. pTimeMax[i] = fToMs * (S.AggregateMax[nTimerIndex]);
  2293. pColors[i] = S.TimerInfo[nTimerIndex].nColor;
  2294. }
  2295. MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING + 3*MICROPROFILE_TEXT_WIDTH, nOffsetY, (uint32_t)-1, "Avg", sizeof("Avg")-1);
  2296. MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING + 13*MICROPROFILE_TEXT_WIDTH, nOffsetY, (uint32_t)-1, "Max", sizeof("Max")-1);
  2297. for(uint32_t i = 0; i < nCount; ++i)
  2298. {
  2299. nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT);
  2300. uint16_t nTimerIndex = MicroProfileGetTimerIndex(pCustom->pTimers[i]);
  2301. uint16_t nGroupIndex = MicroProfileGetGroupIndex(pCustom->pTimers[i]);
  2302. MicroProfileTimerInfo* pTimerInfo = &S.TimerInfo[nTimerIndex];
  2303. int nSize;
  2304. uint32_t nOffsetX = MICROPROFILE_CUSTOM_PADDING;
  2305. nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2f", pTimeAvg[i]);
  2306. MicroProfileDrawText(nOffsetX, nOffsetY, (uint32_t)-1, Buffer, nSize);
  2307. nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2308. nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2f", pTimeMax[i]);
  2309. MicroProfileDrawText(nOffsetX, nOffsetY, (uint32_t)-1, Buffer, nSize);
  2310. nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2311. nSize = snprintf(Buffer, sizeof(Buffer)-1, "%s:%s", S.GroupInfo[nGroupIndex].pName, pTimerInfo->pName);
  2312. MicroProfileDrawText(nOffsetX, nOffsetY, pTimerInfo->nColor, Buffer, nSize);
  2313. nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2314. nMaxOffsetX = MicroProfileMax(nMaxOffsetX, nOffsetX);
  2315. }
  2316. uint32_t nMaxWidth = nReducedWidth- nMaxOffsetX;
  2317. if(pCustom->nFlags & MICROPROFILE_CUSTOM_BARS)
  2318. {
  2319. nOffsetY = nOffsetYBase;
  2320. float* pMs = pCustom->nFlags & MICROPROFILE_CUSTOM_BAR_SOURCE_MAX ? pTimeMax : pTimeAvg;
  2321. const char* pString = pCustom->nFlags & MICROPROFILE_CUSTOM_BAR_SOURCE_MAX ? "Max" : "Avg";
  2322. MicroProfileDrawText(nMaxOffsetX, nOffsetY, (uint32_t)-1, pString, static_cast<uint32_t>(strlen(pString)));
  2323. int nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2fms", fReference);
  2324. MicroProfileDrawText(nReducedWidth - (1+nSize) * (MICROPROFILE_TEXT_WIDTH+1), nOffsetY, (uint32_t)-1, Buffer, nSize);
  2325. for(uint32_t i = 0; i < nCount; ++i)
  2326. {
  2327. nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT);
  2328. uint32_t nWidth = MicroProfileMin(nMaxWidth, (uint32_t)(nMaxWidth * pMs[i] * fRcpReference));
  2329. MicroProfileDrawBox(nMaxOffsetX, nOffsetY, nMaxOffsetX+nWidth, nOffsetY+MICROPROFILE_TEXT_HEIGHT, pColors[i]|0xff000000);
  2330. }
  2331. }
  2332. if(pCustom->nFlags & MICROPROFILE_CUSTOM_STACK)
  2333. {
  2334. nOffsetY += 2*(1+MICROPROFILE_TEXT_HEIGHT);
  2335. const char* pString = pCustom->nFlags & MICROPROFILE_CUSTOM_STACK_SOURCE_MAX ? "Max" : "Avg";
  2336. MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING, nOffsetY, (uint32_t)-1, pString, static_cast<uint32_t>(strlen(pString)));
  2337. int nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2fms", fReference);
  2338. MicroProfileDrawText(nReducedWidth - (1+nSize) * (MICROPROFILE_TEXT_WIDTH+1), nOffsetY, (uint32_t)-1, Buffer, nSize);
  2339. nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT);
  2340. float fPosX = MICROPROFILE_CUSTOM_PADDING;
  2341. float* pMs = pCustom->nFlags & MICROPROFILE_CUSTOM_STACK_SOURCE_MAX ? pTimeMax : pTimeAvg;
  2342. for(uint32_t i = 0; i < nCount; ++i)
  2343. {
  2344. float fWidth = pMs[i] * fRcpReference * nReducedWidth;
  2345. uint32_t nX = fPosX;
  2346. fPosX += fWidth;
  2347. uint32_t nXEnd = fPosX;
  2348. if(nX < nXEnd)
  2349. {
  2350. MicroProfileDrawBox(nX, nOffsetY, nXEnd, nOffsetY+MICROPROFILE_TEXT_HEIGHT, pColors[i]|0xff000000);
  2351. }
  2352. }
  2353. }
  2354. }
  2355. }
  2356. inline void MicroProfileDraw(uint32_t nWidth, uint32_t nHeight)
  2357. {
  2358. MICROPROFILE_SCOPE(g_MicroProfileDraw);
  2359. MicroProfile& S = *MicroProfileGet();
  2360. {
  2361. static int once = 0;
  2362. if(0 == once)
  2363. {
  2364. std::recursive_mutex& m = MicroProfileGetMutex();
  2365. m.lock();
  2366. MicroProfileInitUI();
  2367. uint32_t nDisplay = S.nDisplay;
  2368. MicroProfileLoadPreset(MICROPROFILE_DEFAULT_PRESET);
  2369. once++;
  2370. S.nDisplay = nDisplay;// dont load display, just state
  2371. m.unlock();
  2372. }
  2373. }
  2374. if(S.nDisplay)
  2375. {
  2376. std::recursive_mutex& m = MicroProfileGetMutex();
  2377. m.lock();
  2378. UI.nWidth = nWidth;
  2379. UI.nHeight = nHeight;
  2380. UI.nHoverToken = MICROPROFILE_INVALID_TOKEN;
  2381. UI.nHoverTime = 0;
  2382. UI.nHoverFrame = -1;
  2383. if(S.nDisplay != MP_DRAW_DETAILED)
  2384. S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = -1;
  2385. MicroProfileMoveGraph();
  2386. if(S.nDisplay == MP_DRAW_DETAILED)
  2387. {
  2388. MicroProfileDrawDetailedView(nWidth, nHeight);
  2389. }
  2390. else if(S.nDisplay == MP_DRAW_BARS && S.nBars)
  2391. {
  2392. MicroProfileDrawBarView(nWidth, nHeight);
  2393. }
  2394. MicroProfileDrawMenu(nWidth, nHeight);
  2395. bool bMouseOverGraph = MicroProfileDrawGraph(nWidth, nHeight);
  2396. MicroProfileDrawCustom(nWidth, nHeight);
  2397. bool bHidden = S.nDisplay == MP_DRAW_HIDDEN;
  2398. if(!bHidden)
  2399. {
  2400. uint32_t nLockedToolTipX = 3;
  2401. bool bDeleted = false;
  2402. for(int i = 0; i < MICROPROFILE_TOOLTIP_MAX_LOCKED; ++i)
  2403. {
  2404. int nIndex = (g_MicroProfileUI.LockedToolTipFront + i) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  2405. if(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0])
  2406. {
  2407. uint32_t nToolTipWidth = 0, nToolTipHeight = 0;
  2408. MicroProfileFloatWindowSize(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings, g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, 0, nToolTipWidth, nToolTipHeight, 0);
  2409. uint32_t nStartY = nHeight - nToolTipHeight - 2;
  2410. if(!bDeleted && UI.nMouseY > nStartY && UI.nMouseX > nLockedToolTipX && UI.nMouseX <= nLockedToolTipX + nToolTipWidth && (UI.nMouseLeft || UI.nMouseRight) )
  2411. {
  2412. bDeleted = true;
  2413. int j = i;
  2414. for(; j < MICROPROFILE_TOOLTIP_MAX_LOCKED-1; ++j)
  2415. {
  2416. int nIndex0 = (g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  2417. int nIndex1 = (g_MicroProfileUI.LockedToolTipFront + j+1) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  2418. MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex0], &g_MicroProfileUI.LockedToolTips[nIndex1]);
  2419. }
  2420. MicroProfileStringArrayClear(&g_MicroProfileUI.LockedToolTips[(g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED]);
  2421. }
  2422. else
  2423. {
  2424. MicroProfileDrawFloatWindow(nLockedToolTipX, nHeight-nToolTipHeight-2, &g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0], g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, g_MicroProfileUI.nLockedToolTipColor[nIndex]);
  2425. nLockedToolTipX += nToolTipWidth + 4;
  2426. }
  2427. }
  2428. }
  2429. if(UI.nActiveMenu == 8)
  2430. {
  2431. if(S.nDisplay & MP_DRAW_DETAILED)
  2432. {
  2433. MicroProfileStringArray DetailedHelp;
  2434. MicroProfileStringArrayClear(&DetailedHelp);
  2435. MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_LEFT);
  2436. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Toggle Graph");
  2437. MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_ALT);
  2438. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom");
  2439. MicroProfileStringArrayFormat(&DetailedHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT);
  2440. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Lock Tooltip");
  2441. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Drag");
  2442. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Pan View");
  2443. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Mouse Wheel");
  2444. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom");
  2445. MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, DetailedHelp.ppStrings, DetailedHelp.nNumStrings, 0xff777777);
  2446. MicroProfileStringArray DetailedHistoryHelp;
  2447. MicroProfileStringArrayClear(&DetailedHistoryHelp);
  2448. MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_LEFT);
  2449. MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Center View");
  2450. MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_ALT);
  2451. MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Zoom to frame");
  2452. MicroProfileDrawFloatWindow(nWidth, 20, DetailedHistoryHelp.ppStrings, DetailedHistoryHelp.nNumStrings, 0xff777777);
  2453. }
  2454. else if(0 != (S.nDisplay & MP_DRAW_BARS) && S.nBars)
  2455. {
  2456. MicroProfileStringArray BarHelp;
  2457. MicroProfileStringArrayClear(&BarHelp);
  2458. MicroProfileStringArrayFormat(&BarHelp, "%s", MICROPROFILE_HELP_LEFT);
  2459. MicroProfileStringArrayAddLiteral(&BarHelp, "Toggle Graph");
  2460. MicroProfileStringArrayFormat(&BarHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT);
  2461. MicroProfileStringArrayAddLiteral(&BarHelp, "Lock Tooltip");
  2462. MicroProfileStringArrayAddLiteral(&BarHelp, "Drag");
  2463. MicroProfileStringArrayAddLiteral(&BarHelp, "Pan View");
  2464. MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, BarHelp.ppStrings, BarHelp.nNumStrings, 0xff777777);
  2465. }
  2466. MicroProfileStringArray Debug;
  2467. MicroProfileStringArrayClear(&Debug);
  2468. MicroProfileStringArrayAddLiteral(&Debug, "Memory Usage");
  2469. MicroProfileStringArrayFormat(&Debug, "%4.2fmb", S.nMemUsage / (1024.f * 1024.f));
  2470. MicroProfileStringArrayAddLiteral(&Debug, "Web Server Port");
  2471. MicroProfileStringArrayFormat(&Debug, "%d", MicroProfileWebServerPort());
  2472. uint32_t nFrameNext = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY;
  2473. MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent];
  2474. MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext];
  2475. MicroProfileStringArrayAddLiteral(&Debug, "");
  2476. MicroProfileStringArrayAddLiteral(&Debug, "");
  2477. MicroProfileStringArrayAddLiteral(&Debug, "Usage");
  2478. MicroProfileStringArrayAddLiteral(&Debug, "markers [frames] ");
  2479. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  2480. MicroProfileStringArrayAddLiteral(&Debug, "Context Switch");
  2481. MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", S.nContextSwitchUsage, MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE / S.nContextSwitchUsage );
  2482. #endif
  2483. for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2484. {
  2485. if(pFrameCurrent->nLogStart[i] && S.Pool[i])
  2486. {
  2487. uint32_t nEnd = pFrameNext->nLogStart[i];
  2488. uint32_t nStart = pFrameCurrent->nLogStart[i];
  2489. uint32_t nUsage = nStart < nEnd ? (nEnd - nStart) : (nEnd + MICROPROFILE_BUFFER_SIZE - nStart);
  2490. uint32_t nFrameSupport = MICROPROFILE_BUFFER_SIZE / nUsage;
  2491. MicroProfileStringArrayFormat(&Debug, "%s", &S.Pool[i]->ThreadName[0]);
  2492. MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", nUsage, nFrameSupport);
  2493. }
  2494. }
  2495. MicroProfileDrawFloatWindow(0, nHeight-10, Debug.ppStrings, Debug.nNumStrings, 0xff777777);
  2496. }
  2497. if(UI.nActiveMenu == -1 && !bMouseOverGraph)
  2498. {
  2499. if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN)
  2500. {
  2501. MicroProfileDrawFloatTooltip(UI.nMouseX, UI.nMouseY, UI.nHoverToken, UI.nHoverTime);
  2502. }
  2503. else if(S.nContextSwitchHoverThreadAfter != -1 && S.nContextSwitchHoverThreadBefore != -1)
  2504. {
  2505. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  2506. MicroProfileStringArray ToolTip;
  2507. MicroProfileStringArrayClear(&ToolTip);
  2508. MicroProfileStringArrayAddLiteral(&ToolTip, "Context Switch");
  2509. MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThread);
  2510. MicroProfileStringArrayAddLiteral(&ToolTip, "Before");
  2511. MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadBefore);
  2512. MicroProfileStringArrayAddLiteral(&ToolTip, "After");
  2513. MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadAfter);
  2514. MicroProfileStringArrayAddLiteral(&ToolTip, "Duration");
  2515. int64_t nDifference = MicroProfileLogTickDifference(S.nContextSwitchHoverTickIn, S.nContextSwitchHoverTickOut);
  2516. MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fToMs * nDifference );
  2517. MicroProfileStringArrayAddLiteral(&ToolTip, "CPU");
  2518. MicroProfileStringArrayFormat(&ToolTip, "%d", S.nContextSwitchHoverCpu);
  2519. MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, -1);
  2520. }
  2521. else if(UI.nHoverFrame != -1)
  2522. {
  2523. uint32_t nNextFrame = (UI.nHoverFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY;
  2524. int64_t nTick = S.Frames[UI.nHoverFrame].nFrameStartCpu;
  2525. int64_t nTickNext = S.Frames[nNextFrame].nFrameStartCpu;
  2526. int64_t nTickGpu = S.Frames[UI.nHoverFrame].nFrameStartGpu;
  2527. int64_t nTickNextGpu = S.Frames[nNextFrame].nFrameStartGpu;
  2528. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  2529. float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu());
  2530. float fMs = fToMs * (nTickNext - nTick);
  2531. float fMsGpu = fToMsGpu * (nTickNextGpu - nTickGpu);
  2532. MicroProfileStringArray ToolTip;
  2533. MicroProfileStringArrayClear(&ToolTip);
  2534. MicroProfileStringArrayFormat(&ToolTip, "Frame %d", UI.nHoverFrame);
  2535. #if MICROPROFILE_DEBUG
  2536. MicroProfileStringArrayFormat(&ToolTip, "%p", &S.Frames[UI.nHoverFrame]);
  2537. #else
  2538. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  2539. #endif
  2540. MicroProfileStringArrayAddLiteral(&ToolTip, "CPU Time");
  2541. MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMs);
  2542. MicroProfileStringArrayAddLiteral(&ToolTip, "GPU Time");
  2543. MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMsGpu);
  2544. #if MICROPROFILE_DEBUG
  2545. for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2546. {
  2547. if(S.Frames[UI.nHoverFrame].nLogStart[i])
  2548. {
  2549. MicroProfileStringArrayFormat(&ToolTip, "%d", i);
  2550. MicroProfileStringArrayFormat(&ToolTip, "%d", S.Frames[UI.nHoverFrame].nLogStart[i]);
  2551. }
  2552. }
  2553. #endif
  2554. MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, -1);
  2555. }
  2556. if(UI.nMouseLeft)
  2557. {
  2558. if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN)
  2559. MicroProfileToggleGraph(UI.nHoverToken);
  2560. }
  2561. }
  2562. }
  2563. #if MICROPROFILE_DRAWCURSOR
  2564. {
  2565. float fCursor[8] =
  2566. {
  2567. MicroProfileMax(0, (int)UI.nMouseX-3), UI.nMouseY,
  2568. MicroProfileMin(nWidth, UI.nMouseX+3), UI.nMouseY,
  2569. UI.nMouseX, MicroProfileMax((int)UI.nMouseY-3, 0),
  2570. UI.nMouseX, MicroProfileMin(nHeight, UI.nMouseY+3),
  2571. };
  2572. MicroProfileDrawLine2D(2, &fCursor[0], 0xff00ff00);
  2573. MicroProfileDrawLine2D(2, &fCursor[4], 0xff00ff00);
  2574. }
  2575. #endif
  2576. m.unlock();
  2577. }
  2578. else if(UI.nCustomActive != (uint32_t)-1)
  2579. {
  2580. std::recursive_mutex& m = MicroProfileGetMutex();
  2581. m.lock();
  2582. MicroProfileDrawGraph(nWidth, nHeight);
  2583. MicroProfileDrawCustom(nWidth, nHeight);
  2584. m.unlock();
  2585. }
  2586. UI.nMouseLeft = UI.nMouseRight = 0;
  2587. UI.nMouseLeftMod = UI.nMouseRightMod = 0;
  2588. UI.nMouseWheelDelta = 0;
  2589. if(S.nOverflow)
  2590. S.nOverflow--;
  2591. UI.fDetailedOffset = UI.fDetailedOffset + (UI.fDetailedOffsetTarget - UI.fDetailedOffset) * MICROPROFILE_ANIM_DELAY_PRC;
  2592. UI.fDetailedRange = UI.fDetailedRange + (UI.fDetailedRangeTarget - UI.fDetailedRange) * MICROPROFILE_ANIM_DELAY_PRC;
  2593. }
  2594. bool MicroProfileIsDrawing()
  2595. {
  2596. MicroProfile& S = *MicroProfileGet();
  2597. return S.nDisplay != 0;
  2598. }
  2599. void MicroProfileToggleGraph(MicroProfileToken nToken)
  2600. {
  2601. MicroProfile& S = *MicroProfileGet();
  2602. uint32_t nTimerId = MicroProfileGetTimerIndex(nToken);
  2603. nToken &= 0xffff;
  2604. int32_t nMinSort = 0x7fffffff;
  2605. int32_t nFreeIndex = -1;
  2606. int32_t nMinIndex = 0;
  2607. int32_t nMaxSort = 0x80000000;
  2608. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2609. {
  2610. if(S.Graph[i].nToken == MICROPROFILE_INVALID_TOKEN)
  2611. nFreeIndex = i;
  2612. if(S.Graph[i].nToken == nToken)
  2613. {
  2614. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2615. S.TimerInfo[nTimerId].bGraph = false;
  2616. return;
  2617. }
  2618. if(S.Graph[i].nKey < nMinSort)
  2619. {
  2620. nMinSort = S.Graph[i].nKey;
  2621. nMinIndex = i;
  2622. }
  2623. if(S.Graph[i].nKey > nMaxSort)
  2624. {
  2625. nMaxSort = S.Graph[i].nKey;
  2626. }
  2627. }
  2628. int nIndex = nFreeIndex > -1 ? nFreeIndex : nMinIndex;
  2629. if (nFreeIndex == -1)
  2630. {
  2631. uint32_t idx = MicroProfileGetTimerIndex(S.Graph[nIndex].nToken);
  2632. S.TimerInfo[idx].bGraph = false;
  2633. }
  2634. S.Graph[nIndex].nToken = nToken;
  2635. S.Graph[nIndex].nKey = nMaxSort+1;
  2636. memset(&S.Graph[nIndex].nHistory[0], 0, sizeof(S.Graph[nIndex].nHistory));
  2637. S.TimerInfo[nTimerId].bGraph = true;
  2638. }
  2639. void MicroProfileMousePosition(uint32_t nX, uint32_t nY, int nWheelDelta)
  2640. {
  2641. UI.nMouseX = nX;
  2642. UI.nMouseY = nY;
  2643. UI.nMouseWheelDelta = nWheelDelta;
  2644. }
  2645. void MicroProfileModKey(uint32_t nKeyState)
  2646. {
  2647. UI.nModDown = nKeyState ? 1 : 0;
  2648. }
  2649. void MicroProfileClearGraph()
  2650. {
  2651. MicroProfile& S = *MicroProfileGet();
  2652. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2653. {
  2654. if(S.Graph[i].nToken != 0)
  2655. {
  2656. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2657. }
  2658. }
  2659. }
  2660. void MicroProfileMouseButton(uint32_t nLeft, uint32_t nRight)
  2661. {
  2662. bool bCanRelease = abs((int)(UI.nMouseDownX - UI.nMouseX)) + abs((int)(UI.nMouseDownY - UI.nMouseY)) < 3;
  2663. if(0 == nLeft && UI.nMouseDownLeft && bCanRelease)
  2664. {
  2665. if(UI.nModDown)
  2666. UI.nMouseLeftMod = 1;
  2667. else
  2668. UI.nMouseLeft = 1;
  2669. }
  2670. if(0 == nRight && UI.nMouseDownRight && bCanRelease)
  2671. {
  2672. if(UI.nModDown)
  2673. UI.nMouseRightMod = 1;
  2674. else
  2675. UI.nMouseRight = 1;
  2676. }
  2677. if((nLeft || nRight) && !(UI.nMouseDownLeft || UI.nMouseDownRight))
  2678. {
  2679. UI.nMouseDownX = UI.nMouseX;
  2680. UI.nMouseDownY = UI.nMouseY;
  2681. }
  2682. UI.nMouseDownLeft = nLeft;
  2683. UI.nMouseDownRight = nRight;
  2684. }
  2685. void MicroProfileDrawLineVertical(int nX, int nTop, int nBottom, uint32_t nColor)
  2686. {
  2687. MicroProfileDrawBox(nX, nTop, nX + 1, nBottom, nColor);
  2688. }
  2689. void MicroProfileDrawLineHorizontal(int nLeft, int nRight, int nY, uint32_t nColor)
  2690. {
  2691. MicroProfileDrawBox(nLeft, nY, nRight, nY + 1, nColor);
  2692. }
  2693. #include <stdio.h>
  2694. #define MICROPROFILE_PRESET_HEADER_MAGIC 0x28586813
  2695. #define MICROPROFILE_PRESET_HEADER_VERSION 0x00000102
  2696. struct MicroProfilePresetHeader
  2697. {
  2698. uint32_t nMagic;
  2699. uint32_t nVersion;
  2700. //groups, threads, aggregate, reference frame, graphs timers
  2701. uint32_t nGroups[MICROPROFILE_MAX_GROUPS];
  2702. uint32_t nThreads[MICROPROFILE_MAX_THREADS];
  2703. uint32_t nGraphName[MICROPROFILE_MAX_GRAPHS];
  2704. uint32_t nGraphGroupName[MICROPROFILE_MAX_GRAPHS];
  2705. uint32_t nAllGroupsWanted;
  2706. uint32_t nAllThreadsWanted;
  2707. uint32_t nAggregateFlip;
  2708. float fReferenceTime;
  2709. uint32_t nBars;
  2710. uint32_t nDisplay;
  2711. uint32_t nOpacityBackground;
  2712. uint32_t nOpacityForeground;
  2713. uint32_t nShowSpikes;
  2714. };
  2715. #ifndef MICROPROFILE_PRESET_FILENAME_FUNC
  2716. #define MICROPROFILE_PRESET_FILENAME_FUNC MicroProfilePresetFilename
  2717. static const char* MicroProfilePresetFilename(const char* pSuffix)
  2718. {
  2719. static char filename[512];
  2720. snprintf(filename, sizeof(filename)-1, ".microprofilepreset.%s", pSuffix);
  2721. return filename;
  2722. }
  2723. #endif
  2724. void MicroProfileSavePreset(const char* pPresetName)
  2725. {
  2726. std::lock_guard<std::recursive_mutex> Lock(MicroProfileGetMutex());
  2727. FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pPresetName), "wb");
  2728. if(!F) return;
  2729. MicroProfile& S = *MicroProfileGet();
  2730. MicroProfilePresetHeader Header;
  2731. memset(&Header, 0, sizeof(Header));
  2732. Header.nAggregateFlip = S.nAggregateFlip;
  2733. Header.nBars = S.nBars;
  2734. Header.fReferenceTime = S.fReferenceTime;
  2735. Header.nAllGroupsWanted = S.nAllGroupsWanted;
  2736. Header.nAllThreadsWanted = S.nAllThreadsWanted;
  2737. Header.nMagic = MICROPROFILE_PRESET_HEADER_MAGIC;
  2738. Header.nVersion = MICROPROFILE_PRESET_HEADER_VERSION;
  2739. Header.nDisplay = S.nDisplay;
  2740. Header.nOpacityBackground = UI.nOpacityBackground;
  2741. Header.nOpacityForeground = UI.nOpacityForeground;
  2742. Header.nShowSpikes = UI.bShowSpikes ? 1 : 0;
  2743. fwrite(&Header, sizeof(Header), 1, F);
  2744. uint64_t nMask = 1;
  2745. for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i)
  2746. {
  2747. if(S.nActiveGroupWanted & nMask)
  2748. {
  2749. uint32_t offset = ftell(F);
  2750. const char* pName = S.GroupInfo[i].pName;
  2751. int nLen = (int)strlen(pName)+1;
  2752. fwrite(pName, nLen, 1, F);
  2753. Header.nGroups[i] = offset;
  2754. }
  2755. nMask <<= 1;
  2756. }
  2757. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2758. {
  2759. MicroProfileThreadLog* pLog = S.Pool[i];
  2760. if(pLog && S.nThreadActive[i])
  2761. {
  2762. uint32_t nOffset = ftell(F);
  2763. const char* pName = &pLog->ThreadName[0];
  2764. int nLen = (int)strlen(pName)+1;
  2765. fwrite(pName, nLen, 1, F);
  2766. Header.nThreads[i] = nOffset;
  2767. }
  2768. }
  2769. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2770. {
  2771. MicroProfileToken nToken = S.Graph[i].nToken;
  2772. if(nToken != MICROPROFILE_INVALID_TOKEN)
  2773. {
  2774. uint32_t nGroupIndex = MicroProfileGetGroupIndex(nToken);
  2775. uint32_t nTimerIndex = MicroProfileGetTimerIndex(nToken);
  2776. const char* pGroupName = S.GroupInfo[nGroupIndex].pName;
  2777. const char* pTimerName = S.TimerInfo[nTimerIndex].pName;
  2778. MP_ASSERT(pGroupName);
  2779. MP_ASSERT(pTimerName);
  2780. int nGroupLen = (int)strlen(pGroupName)+1;
  2781. int nTimerLen = (int)strlen(pTimerName)+1;
  2782. uint32_t nOffsetGroup = ftell(F);
  2783. fwrite(pGroupName, nGroupLen, 1, F);
  2784. uint32_t nOffsetTimer = ftell(F);
  2785. fwrite(pTimerName, nTimerLen, 1, F);
  2786. Header.nGraphName[i] = nOffsetTimer;
  2787. Header.nGraphGroupName[i] = nOffsetGroup;
  2788. }
  2789. }
  2790. fseek(F, 0, SEEK_SET);
  2791. fwrite(&Header, sizeof(Header), 1, F);
  2792. fclose(F);
  2793. }
  2794. void MicroProfileLoadPreset(const char* pSuffix)
  2795. {
  2796. std::lock_guard<std::recursive_mutex> Lock(MicroProfileGetMutex());
  2797. FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pSuffix), "rb");
  2798. if(!F)
  2799. {
  2800. return;
  2801. }
  2802. fseek(F, 0, SEEK_END);
  2803. int nSize = ftell(F);
  2804. char* const pBuffer = (char*)alloca(nSize);
  2805. fseek(F, 0, SEEK_SET);
  2806. int nRead = (int)fread(pBuffer, nSize, 1, F);
  2807. fclose(F);
  2808. if(1 != nRead)
  2809. return;
  2810. MicroProfile& S = *MicroProfileGet();
  2811. MicroProfilePresetHeader& Header = *(MicroProfilePresetHeader*)pBuffer;
  2812. if(Header.nMagic != MICROPROFILE_PRESET_HEADER_MAGIC || Header.nVersion != MICROPROFILE_PRESET_HEADER_VERSION)
  2813. {
  2814. return;
  2815. }
  2816. S.nAggregateFlip = Header.nAggregateFlip;
  2817. S.nBars = Header.nBars;
  2818. S.fReferenceTime = Header.fReferenceTime;
  2819. S.fRcpReferenceTime = 1.f / Header.fReferenceTime;
  2820. S.nAllGroupsWanted = Header.nAllGroupsWanted;
  2821. S.nAllThreadsWanted = Header.nAllThreadsWanted;
  2822. S.nDisplay = Header.nDisplay;
  2823. S.nActiveGroupWanted = 0;
  2824. UI.nOpacityBackground = Header.nOpacityBackground;
  2825. UI.nOpacityForeground = Header.nOpacityForeground;
  2826. UI.bShowSpikes = Header.nShowSpikes == 1;
  2827. memset(&S.nThreadActive[0], 0, sizeof(S.nThreadActive));
  2828. for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i)
  2829. {
  2830. if(Header.nGroups[i])
  2831. {
  2832. const char* pGroupName = pBuffer + Header.nGroups[i];
  2833. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  2834. {
  2835. if(0 == MP_STRCASECMP(pGroupName, S.GroupInfo[j].pName))
  2836. {
  2837. S.nActiveGroupWanted |= (1ll << j);
  2838. }
  2839. }
  2840. }
  2841. }
  2842. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2843. {
  2844. if(Header.nThreads[i])
  2845. {
  2846. const char* pThreadName = pBuffer + Header.nThreads[i];
  2847. for(uint32_t j = 0; j < MICROPROFILE_MAX_THREADS; ++j)
  2848. {
  2849. MicroProfileThreadLog* pLog = S.Pool[j];
  2850. if(pLog && 0 == MP_STRCASECMP(pThreadName, &pLog->ThreadName[0]))
  2851. {
  2852. S.nThreadActive[j] = 1;
  2853. }
  2854. }
  2855. }
  2856. }
  2857. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2858. {
  2859. MicroProfileToken nPrevToken = S.Graph[i].nToken;
  2860. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2861. if(Header.nGraphName[i] && Header.nGraphGroupName[i])
  2862. {
  2863. const char* pGraphName = pBuffer + Header.nGraphName[i];
  2864. const char* pGraphGroupName = pBuffer + Header.nGraphGroupName[i];
  2865. for(uint32_t j = 0; j < S.nTotalTimers; ++j)
  2866. {
  2867. uint64_t nGroupIndex = S.TimerInfo[j].nGroupIndex;
  2868. if(0 == MP_STRCASECMP(pGraphName, S.TimerInfo[j].pName) && 0 == MP_STRCASECMP(pGraphGroupName, S.GroupInfo[nGroupIndex].pName))
  2869. {
  2870. MicroProfileToken nToken = MicroProfileMakeToken(1ll << nGroupIndex, (uint16_t)j);
  2871. S.Graph[i].nToken = nToken; // note: group index is stored here but is checked without in MicroProfileToggleGraph()!
  2872. S.TimerInfo[j].bGraph = true;
  2873. if(nToken != nPrevToken)
  2874. {
  2875. memset(&S.Graph[i].nHistory, 0, sizeof(S.Graph[i].nHistory));
  2876. }
  2877. break;
  2878. }
  2879. }
  2880. }
  2881. }
  2882. }
  2883. inline uint32_t MicroProfileCustomGroupFind(const char* pCustomName)
  2884. {
  2885. for(uint32_t i = 0; i < UI.nCustomCount; ++i)
  2886. {
  2887. if(!MP_STRCASECMP(pCustomName, UI.Custom[i].pName))
  2888. {
  2889. return i;
  2890. }
  2891. }
  2892. return (uint32_t)-1;
  2893. }
  2894. inline uint32_t MicroProfileCustomGroup(const char* pCustomName)
  2895. {
  2896. for(uint32_t i = 0; i < UI.nCustomCount; ++i)
  2897. {
  2898. if(!MP_STRCASECMP(pCustomName, UI.Custom[i].pName))
  2899. {
  2900. return i;
  2901. }
  2902. }
  2903. MP_ASSERT(UI.nCustomCount < MICROPROFILE_CUSTOM_MAX);
  2904. uint32_t nIndex = UI.nCustomCount;
  2905. UI.nCustomCount++;
  2906. memset(&UI.Custom[nIndex], 0, sizeof(UI.Custom[nIndex]));
  2907. uint32_t nLen = (uint32_t)strlen(pCustomName);
  2908. if(nLen > MICROPROFILE_NAME_MAX_LEN-1)
  2909. nLen = MICROPROFILE_NAME_MAX_LEN-1;
  2910. memcpy(&UI.Custom[nIndex].pName[0], pCustomName, nLen);
  2911. UI.Custom[nIndex].pName[nLen] = '\0';
  2912. return nIndex;
  2913. }
  2914. void MicroProfileCustomGroup(const char* pCustomName, uint32_t nMaxTimers, uint32_t nAggregateFlip, float fReferenceTime, uint32_t nFlags)
  2915. {
  2916. uint32_t nIndex = MicroProfileCustomGroup(pCustomName);
  2917. MP_ASSERT(UI.Custom[nIndex].pTimers == 0);//only call once!
  2918. UI.Custom[nIndex].pTimers = &UI.CustomTimer[UI.nCustomTimerCount];
  2919. UI.Custom[nIndex].nMaxTimers = nMaxTimers;
  2920. UI.Custom[nIndex].fReference = fReferenceTime;
  2921. UI.nCustomTimerCount += nMaxTimers;
  2922. MP_ASSERT(UI.nCustomTimerCount <= MICROPROFILE_CUSTOM_MAX_TIMERS); //bump MICROPROFILE_CUSTOM_MAX_TIMERS
  2923. UI.Custom[nIndex].nFlags = nFlags;
  2924. UI.Custom[nIndex].nAggregateFlip = nAggregateFlip;
  2925. }
  2926. inline void MicroProfileCustomGroupEnable(uint32_t nIndex)
  2927. {
  2928. if(nIndex < UI.nCustomCount)
  2929. {
  2930. MicroProfile& S = *MicroProfileGet();
  2931. S.nForceGroupUI = UI.Custom[nIndex].nGroupMask;
  2932. MicroProfileSetAggregateFrames(UI.Custom[nIndex].nAggregateFlip);
  2933. S.fReferenceTime = UI.Custom[nIndex].fReference;
  2934. S.fRcpReferenceTime = 1.f / UI.Custom[nIndex].fReference;
  2935. UI.nCustomActive = nIndex;
  2936. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2937. {
  2938. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  2939. {
  2940. uint32_t nTimerId = MicroProfileGetTimerIndex(S.Graph[i].nToken);
  2941. S.TimerInfo[nTimerId].bGraph = false;
  2942. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2943. }
  2944. }
  2945. for(uint32_t i = 0; i < UI.Custom[nIndex].nNumTimers; ++i)
  2946. {
  2947. if(i == MICROPROFILE_MAX_GRAPHS)
  2948. {
  2949. break;
  2950. }
  2951. S.Graph[i].nToken = UI.Custom[nIndex].pTimers[i];
  2952. S.Graph[i].nKey = i;
  2953. uint32_t nTimerId = MicroProfileGetTimerIndex(S.Graph[i].nToken);
  2954. S.TimerInfo[nTimerId].bGraph = true;
  2955. }
  2956. }
  2957. }
  2958. void MicroProfileCustomGroupToggle(const char* pCustomName)
  2959. {
  2960. uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName);
  2961. if(nIndex == (uint32_t)-1 || nIndex == UI.nCustomActive)
  2962. {
  2963. MicroProfileCustomGroupDisable();
  2964. }
  2965. else
  2966. {
  2967. MicroProfileCustomGroupEnable(nIndex);
  2968. }
  2969. }
  2970. void MicroProfileCustomGroupEnable(const char* pCustomName)
  2971. {
  2972. uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName);
  2973. MicroProfileCustomGroupEnable(nIndex);
  2974. }
  2975. void MicroProfileCustomGroupDisable()
  2976. {
  2977. MicroProfile& S = *MicroProfileGet();
  2978. S.nForceGroupUI = 0;
  2979. UI.nCustomActive = (uint32_t)-1;
  2980. }
  2981. void MicroProfileCustomGroupAddTimer(const char* pCustomName, const char* pGroup, const char* pTimer)
  2982. {
  2983. uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName);
  2984. if((uint32_t)-1 == nIndex)
  2985. {
  2986. return;
  2987. }
  2988. uint32_t nTimerIndex = UI.Custom[nIndex].nNumTimers;
  2989. MP_ASSERT(nTimerIndex < UI.Custom[nIndex].nMaxTimers);
  2990. uint64_t nToken = MicroProfileFindToken(pGroup, pTimer);
  2991. MP_ASSERT(nToken != MICROPROFILE_INVALID_TOKEN); //Timer must be registered first.
  2992. UI.Custom[nIndex].pTimers[nTimerIndex] = nToken;
  2993. uint16_t nGroup = MicroProfileGetGroupIndex(nToken);
  2994. UI.Custom[nIndex].nGroupMask |= (1ll << nGroup);
  2995. UI.Custom[nIndex].nNumTimers++;
  2996. }
  2997. #undef UI
  2998. #endif
  2999. #endif