microprofileui.h 132 KB

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