microprofileui.h 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. #pragma once
  2. // This is free and unencumbered software released into the public domain.
  3. // Anyone is free to copy, modify, publish, use, compile, sell, or
  4. // distribute this software, either in source code form or as a compiled
  5. // binary, for any purpose, commercial or non-commercial, and by any
  6. // means.
  7. // In jurisdictions that recognize copyright laws, the author or authors
  8. // of this software dedicate any and all copyright interest in the
  9. // software to the public domain. We make this dedication for the benefit
  10. // of the public at large and to the detriment of our heirs and
  11. // successors. We intend this dedication to be an overt act of
  12. // relinquishment in perpetuity of all present and future rights to this
  13. // software under copyright law.
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. // OTHER DEALINGS IN THE SOFTWARE.
  21. // For more information, please refer to <http://unlicense.org/>
  22. //
  23. // ***********************************************************************
  24. //
  25. //
  26. //
  27. #ifndef MICROPROFILE_ENABLED
  28. #error "microprofile.h must be included before including microprofileui.h"
  29. #endif
  30. #ifndef MICROPROFILEUI_ENABLED
  31. #define MICROPROFILEUI_ENABLED MICROPROFILE_ENABLED
  32. #endif
  33. #ifndef MICROPROFILEUI_API
  34. #define MICROPROFILEUI_API
  35. #endif
  36. #if 0 == MICROPROFILEUI_ENABLED
  37. #define MicroProfileMouseButton(foo, bar) do{}while(0)
  38. #define MicroProfileMousePosition(foo, bar, z) do{}while(0)
  39. #define MicroProfileModKey(key) do{}while(0)
  40. #define MicroProfileDraw(foo, bar) do{}while(0)
  41. #define MicroProfileIsDrawing() 0
  42. #define MicroProfileToggleDisplayMode() do{}while(0)
  43. #define MicroProfileSetDisplayMode(f) do{}while(0)
  44. #else
  45. #ifndef MICROPROFILE_DRAWCURSOR
  46. #define MICROPROFILE_DRAWCURSOR 0
  47. #endif
  48. #ifndef MICROPROFILE_DETAILED_BAR_NAMES
  49. #define MICROPROFILE_DETAILED_BAR_NAMES 1
  50. #endif
  51. #ifndef MICROPROFILE_TEXT_WIDTH
  52. #define MICROPROFILE_TEXT_WIDTH 5
  53. #endif
  54. #ifndef MICROPROFILE_TEXT_HEIGHT
  55. #define MICROPROFILE_TEXT_HEIGHT 8
  56. #endif
  57. #ifndef MICROPROFILE_DETAILED_BAR_HEIGHT
  58. #define MICROPROFILE_DETAILED_BAR_HEIGHT 12
  59. #endif
  60. #ifndef MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT
  61. #define MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT 7
  62. #endif
  63. #ifndef MICROPROFILE_GRAPH_WIDTH
  64. #define MICROPROFILE_GRAPH_WIDTH 256
  65. #endif
  66. #ifndef MICROPROFILE_GRAPH_HEIGHT
  67. #define MICROPROFILE_GRAPH_HEIGHT 256
  68. #endif
  69. #ifndef MICROPROFILE_BORDER_SIZE
  70. #define MICROPROFILE_BORDER_SIZE 1
  71. #endif
  72. #ifndef MICROPROFILE_HELP_LEFT
  73. #define MICROPROFILE_HELP_LEFT "Left-Click"
  74. #endif
  75. #ifndef MICROPROFILE_HELP_ALT
  76. #define MICROPROFILE_HELP_ALT "Alt-Click"
  77. #endif
  78. #ifndef MICROPROFILE_HELP_MOD
  79. #define MICROPROFILE_HELP_MOD "Mod"
  80. #endif
  81. #ifndef MICROPROFILE_BAR_WIDTH
  82. #define MICROPROFILE_BAR_WIDTH 100
  83. #endif
  84. #ifndef MICROPROFILE_CUSTOM_MAX
  85. #define MICROPROFILE_CUSTOM_MAX 8
  86. #endif
  87. #ifndef MICROPROFILE_CUSTOM_MAX_TIMERS
  88. #define MICROPROFILE_CUSTOM_MAX_TIMERS 64
  89. #endif
  90. #ifndef MICROPROFILE_CUSTOM_PADDING
  91. #define MICROPROFILE_CUSTOM_PADDING 12
  92. #endif
  93. #define MICROPROFILE_FRAME_HISTORY_HEIGHT 50
  94. #define MICROPROFILE_FRAME_HISTORY_WIDTH 7
  95. #define MICROPROFILE_FRAME_HISTORY_COLOR_CPU 0xffff7f27 //255 127 39
  96. #define MICROPROFILE_FRAME_HISTORY_COLOR_GPU 0xff37a0ee //55 160 238
  97. #define MICROPROFILE_FRAME_HISTORY_COLOR_HIGHTLIGHT 0x7733bb44
  98. #define MICROPROFILE_FRAME_COLOR_HIGHTLIGHT 0x20009900
  99. #define MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU 0x20996600
  100. #define MICROPROFILE_NUM_FRAMES (MICROPROFILE_MAX_FRAME_HISTORY - (MICROPROFILE_GPU_FRAME_DELAY+1))
  101. #define MICROPROFILE_TOOLTIP_MAX_STRINGS (32 + MICROPROFILE_MAX_GROUPS*2)
  102. #define MICROPROFILE_TOOLTIP_STRING_BUFFER_SIZE (4*1024)
  103. #define MICROPROFILE_TOOLTIP_MAX_LOCKED 3
  104. enum
  105. {
  106. MICROPROFILE_CUSTOM_BARS = 0x1,
  107. MICROPROFILE_CUSTOM_BAR_SOURCE_MAX = 0x2,
  108. MICROPROFILE_CUSTOM_BAR_SOURCE_AVG = 0,
  109. MICROPROFILE_CUSTOM_STACK = 0x4,
  110. MICROPROFILE_CUSTOM_STACK_SOURCE_MAX = 0x8,
  111. MICROPROFILE_CUSTOM_STACK_SOURCE_AVG = 0,
  112. };
  113. MICROPROFILEUI_API void MicroProfileDraw(uint32_t nWidth, uint32_t nHeight); //! call if drawing microprofilers
  114. MICROPROFILEUI_API bool MicroProfileIsDrawing();
  115. MICROPROFILEUI_API void MicroProfileToggleGraph(MicroProfileToken nToken);
  116. MICROPROFILEUI_API bool MicroProfileDrawGraph(uint32_t nScreenWidth, uint32_t nScreenHeight);
  117. MICROPROFILEUI_API void MicroProfileToggleDisplayMode(); //switch between off, bars, detailed
  118. MICROPROFILEUI_API void MicroProfileSetDisplayMode(int); //switch between off, bars, detailed
  119. MICROPROFILEUI_API void MicroProfileClearGraph();
  120. MICROPROFILEUI_API void MicroProfileMousePosition(uint32_t nX, uint32_t nY, int nWheelDelta);
  121. MICROPROFILEUI_API void MicroProfileModKey(uint32_t nKeyState);
  122. MICROPROFILEUI_API void MicroProfileMouseButton(uint32_t nLeft, uint32_t nRight);
  123. MICROPROFILEUI_API void MicroProfileDrawLineVertical(int nX, int nTop, int nBottom, uint32_t nColor);
  124. MICROPROFILEUI_API void MicroProfileDrawLineHorizontal(int nLeft, int nRight, int nY, uint32_t nColor);
  125. MICROPROFILEUI_API void MicroProfileLoadPreset(const char* pSuffix);
  126. MICROPROFILEUI_API void MicroProfileSavePreset(const char* pSuffix);
  127. MICROPROFILEUI_API void MicroProfileDrawText(int nX, int nY, uint32_t nColor, const char* pText, uint32_t nNumCharacters);
  128. MICROPROFILEUI_API void MicroProfileDrawBox(int nX, int nY, int nX1, int nY1, uint32_t nColor, MicroProfileBoxType = MicroProfileBoxTypeFlat);
  129. MICROPROFILEUI_API void MicroProfileDrawLine2D(uint32_t nVertices, float* pVertices, uint32_t nColor);
  130. MICROPROFILEUI_API void MicroProfileDumpTimers();
  131. MICROPROFILEUI_API void MicroProfileInitUI();
  132. MICROPROFILEUI_API void MicroProfileCustomGroupToggle(const char* pCustomName);
  133. MICROPROFILEUI_API void MicroProfileCustomGroupEnable(const char* pCustomName);
  134. MICROPROFILEUI_API void MicroProfileCustomGroupEnable(uint32_t nIndex);
  135. MICROPROFILEUI_API void MicroProfileCustomGroupDisable();
  136. MICROPROFILEUI_API void MicroProfileCustomGroup(const char* pCustomName, uint32_t nMaxTimers, uint32_t nAggregateFlip, float fReferenceTime, uint32_t nFlags);
  137. MICROPROFILEUI_API void MicroProfileCustomGroupAddTimer(const char* pCustomName, const char* pGroup, const char* pTimer);
  138. #ifdef MICROPROFILEUI_IMPL
  139. #include <inttypes.h>
  140. #include <stdio.h>
  141. #include <stdlib.h>
  142. #include <stdarg.h>
  143. #include <math.h>
  144. #include <algorithm>
  145. #include <array>
  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 const std::array<uint32_t, 6> g_MicroProfileAggregatePresets{0, 10, 20, 30, 60, 120};
  191. static const std::array<float, 10> g_MicroProfileReferenceTimePresets{5.f, 10.f, 15.f,20.f, 33.33f, 66.66f, 100.f, 250.f, 500.f, 1000.f};
  192. static const std::array<uint32_t, 4> g_MicroProfileOpacityPresets{0x40, 0x80, 0xc0, 0xff};
  193. static const std::array<const char*, 7> 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 = g_MicroProfileReferenceTimePresets.size(),
  206. MICROPROFILE_NUM_OPACITY_PRESETS = g_MicroProfileOpacityPresets.size(),
  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 const std::array<uint32_t, 2> g_nMicroProfileBackColors{ 0x474747, 0x313131 };
  272. #define MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS 16
  273. static const std::array<uint32_t, MICROPROFILE_NUM_CONTEXT_SWITCH_COLORS> g_nMicroProfileContextSwitchThreadColors //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_MAX;
  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_MAX;
  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. inline void MicroProfileStringArrayClear(MicroProfileStringArray* pArray)
  351. {
  352. pArray->nNumStrings = 0;
  353. pArray->pBufferPos = &pArray->Buffer[0];
  354. }
  355. inline 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. inline 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. inline 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. inline 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. inline 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_MAX, ppStrings[i0], (uint32_t)strlen(ppStrings[i0]));
  428. MicroProfileDrawText(nX + nWidth - nStringLengths[i0+1] * (MICROPROFILE_TEXT_WIDTH+1), nY + 1, UINT32_MAX, ppStrings[i0+1], (uint32_t)strlen(ppStrings[i0+1]));
  429. nY += (MICROPROFILE_TEXT_HEIGHT+1);
  430. }
  431. }
  432. inline 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_MAX, ppStrings[i], (uint32_t)strlen(ppStrings[i]));
  452. nY += (MICROPROFILE_TEXT_HEIGHT+1);
  453. }
  454. }
  455. inline 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. inline 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. inline 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. inline 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. inline 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. inline 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 = UINT32_MAX;
  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. inline 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 || std::abs(nRefCpu-nBaseTicksCpu) > std::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 = UINT32_MAX;
  816. uint32_t nContextSwitchStart = UINT32_MAX;
  817. uint32_t nContextSwitchEnd = UINT32_MAX;
  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_MAX) {
  866. continue;
  867. }
  868. MP_ASSERT(nGet != UINT32_MAX);
  869. nPut = pFrameLogLast->nLogStart[i];
  870. uint32_t nRange[2][2] = { {0, 0}, {0, 0}, };
  871. MicroProfileGetRange(nPut, nGet, nRange);
  872. if(nPut == nGet)
  873. continue;
  874. uint32_t nMaxStackDepth = 0;
  875. bool bGpu = pLog->nGpu != 0;
  876. float fToMs = bGpu ? fToMsGpu : fToMsCpu;
  877. int64_t nBaseTicks = bGpu ? nBaseTicksGpu : nBaseTicksCpu;
  878. char ThreadName[MicroProfileThreadLog::THREAD_MAX_LEN + 16];
  879. uint64_t nThreadId = pLog->nThreadId;
  880. snprintf(ThreadName, sizeof(ThreadName)-1, "%04" PRIx64 ": %s", nThreadId, &pLog->ThreadName[0] );
  881. nY += 3;
  882. uint32_t nThreadColor = UINT32_MAX;
  883. if(pLog->nThreadId == nContextSwitchHoverThreadAfter || pLog->nThreadId == nContextSwitchHoverThreadBefore)
  884. nThreadColor = UI.nHoverColorShared|0x906060;
  885. MicroProfileDrawText(0, nY, nThreadColor, &ThreadName[0], (uint32_t)strlen(&ThreadName[0]));
  886. nY += 3;
  887. nY += MICROPROFILE_TEXT_HEIGHT + 1;
  888. if(S.bContextSwitchRunning)
  889. {
  890. MicroProfileDrawDetailedContextSwitchBars(nY, pLog->nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicks, nBaseY);
  891. nY -= MICROPROFILE_DETAILED_BAR_HEIGHT;
  892. nY += MICROPROFILE_DETAILED_CONTEXT_SWITCH_HEIGHT+1;
  893. }
  894. uint32_t nYDelta = MICROPROFILE_DETAILED_BAR_HEIGHT;
  895. uint32_t nStack[MICROPROFILE_STACK_MAX];
  896. uint32_t nStackPos = 0;
  897. for(uint32_t j = 0; j < 2; ++j)
  898. {
  899. uint32_t nStart = nRange[j][0];
  900. uint32_t nEnd = nRange[j][1];
  901. for(uint32_t k = nStart; k < nEnd; ++k)
  902. {
  903. MicroProfileLogEntry* pEntry = &pLog->Log[k];
  904. int nType = MicroProfileLogType(*pEntry);
  905. if(MP_LOG_ENTER == nType)
  906. {
  907. MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX);
  908. nStack[nStackPos++] = k;
  909. }
  910. else if(MP_LOG_META == nType)
  911. {
  912. }
  913. else if(MP_LOG_LEAVE == nType)
  914. {
  915. if(0 == nStackPos)
  916. {
  917. continue;
  918. }
  919. MicroProfileLogEntry* pEntryEnter = &pLog->Log[nStack[nStackPos-1]];
  920. if(MicroProfileLogTimerIndex(*pEntryEnter) != MicroProfileLogTimerIndex(*pEntry))
  921. {
  922. //uprintf("mismatch %llx %llx\n", pEntryEnter->nToken, pEntry->nToken);
  923. continue;
  924. }
  925. int64_t nTickStart = MicroProfileLogGetTick(*pEntryEnter);
  926. int64_t nTickEnd = MicroProfileLogGetTick(*pEntry);
  927. uint64_t nTimerIndex = MicroProfileLogTimerIndex(*pEntry);
  928. uint32_t nColor = S.TimerInfo[nTimerIndex].nColor;
  929. if(nMouseOverToken == nTimerIndex)
  930. {
  931. if(pEntry == pMouseOver)
  932. {
  933. nColor = UI.nHoverColor;
  934. if(bGpu)
  935. {
  936. UI.nRangeBeginGpu = *pEntryEnter;
  937. UI.nRangeEndGpu = *pEntry;
  938. uint32_t nCpuBegin = (nStack[nStackPos-1] + 1) % MICROPROFILE_BUFFER_SIZE;
  939. uint32_t nCpuEnd = (k + 1) % MICROPROFILE_BUFFER_SIZE;
  940. MicroProfileLogEntry LogCpuBegin = pLog->Log[nCpuBegin];
  941. MicroProfileLogEntry LogCpuEnd = pLog->Log[nCpuEnd];
  942. if(MicroProfileLogType(LogCpuBegin)==3 && MicroProfileLogType(LogCpuEnd) == 3)
  943. {
  944. UI.nRangeBegin = LogCpuBegin;
  945. UI.nRangeEnd = LogCpuEnd;
  946. }
  947. UI.nRangeBeginIndex = nStack[nStackPos-1];
  948. UI.nRangeEndIndex = k;
  949. UI.pRangeLog = pLog;
  950. }
  951. else
  952. {
  953. UI.nRangeBegin = *pEntryEnter;
  954. UI.nRangeEnd = *pEntry;
  955. UI.nRangeBeginIndex = nStack[nStackPos-1];
  956. UI.nRangeEndIndex = k;
  957. UI.pRangeLog = pLog;
  958. }
  959. }
  960. else
  961. {
  962. nColor = UI.nHoverColorShared;
  963. }
  964. }
  965. nMaxStackDepth = MicroProfileMax(nMaxStackDepth, nStackPos);
  966. float fMsStart = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickStart);
  967. float fMsEnd = fToMs * MicroProfileLogTickDifference(nBaseTicks, nTickEnd);
  968. float fXStart = fMsStart * fMsToScreen;
  969. float fXEnd = fMsEnd * fMsToScreen;
  970. float fYStart = (float)(nY + nStackPos * nYDelta);
  971. float fYEnd = fYStart + (MICROPROFILE_DETAILED_BAR_HEIGHT);
  972. float fXDist = MicroProfileMax(fXStart - fMouseX, fMouseX - fXEnd);
  973. bool bHover = fXDist < MICROPROFILE_HOVER_DIST && fYStart <= fMouseY && fMouseY <= fYEnd && nBaseY < fMouseY;
  974. uint32_t nIntegerWidth = (uint32_t)(fXEnd - fXStart);
  975. if(nIntegerWidth)
  976. {
  977. if(bHover && UI.nActiveMenu == UINT32_MAX)
  978. {
  979. nHoverToken = MicroProfileLogTimerIndex(*pEntry);
  980. #if MICROPROFILE_DEBUG
  981. UI.nHoverAddressEnter = (uint64_t)pEntryEnter;
  982. UI.nHoverAddressLeave = (uint64_t)pEntry;
  983. #endif
  984. nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd);
  985. pMouseOverNext = pEntry;
  986. }
  987. MicroProfileDrawBox(fXStart, fYStart, fXEnd, fYEnd, nColor|UI.nOpacityForeground, MicroProfileBoxTypeBar);
  988. #if MICROPROFILE_DETAILED_BAR_NAMES
  989. if(nIntegerWidth>3*MICROPROFILE_TEXT_WIDTH)
  990. {
  991. float fXStartText = MicroProfileMax(fXStart, 0.f);
  992. int nTextWidth = (int)(fXEnd - fXStartText);
  993. int nCharacters = (nTextWidth - 2*MICROPROFILE_TEXT_WIDTH) / MICROPROFILE_TEXT_WIDTH;
  994. if(nCharacters>0)
  995. {
  996. MicroProfileDrawText(fXStartText + 1, fYStart + 1, UINT32_MAX, S.TimerInfo[nTimerIndex].pName, MicroProfileMin<uint32_t>(S.TimerInfo[nTimerIndex].nNameLen, nCharacters));
  997. }
  998. }
  999. #endif
  1000. ++nNumBoxes;
  1001. }
  1002. else
  1003. {
  1004. float fXAvg = 0.5f * (fXStart + fXEnd);
  1005. int nLineX = (int)floor(fXAvg+0.5f);
  1006. if(nLineX != (int)nLinesDrawn[nStackPos])
  1007. {
  1008. if(bHover && UI.nActiveMenu == UINT32_MAX)
  1009. {
  1010. nHoverToken = (uint32_t)MicroProfileLogTimerIndex(*pEntry);
  1011. nHoverTime = MicroProfileLogTickDifference(nTickStart, nTickEnd);
  1012. pMouseOverNext = pEntry;
  1013. }
  1014. nLinesDrawn[nStackPos] = nLineX;
  1015. MicroProfileDrawLineVertical(nLineX, fYStart + 0.5f, fYEnd + 0.5f, nColor|UI.nOpacityForeground);
  1016. ++nNumLines;
  1017. }
  1018. }
  1019. nStackPos--;
  1020. if(0 == nStackPos)
  1021. {
  1022. if(bGpu ? (nTickStart > nBaseTicksEndGpu) : (nTickStart > nBaseTicksEndCpu))
  1023. {
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }
  1030. nY += nMaxStackDepth * nYDelta + MICROPROFILE_DETAILED_BAR_HEIGHT+1;
  1031. }
  1032. }
  1033. if(S.bContextSwitchRunning && (S.bContextSwitchAllThreads||S.bContextSwitchNoBars))
  1034. {
  1035. uint32_t nNumThreads = 0;
  1036. uint32_t nThreads[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS];
  1037. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS && S.Pool[i]; ++i)
  1038. nThreads[nNumThreads++] = S.Pool[i]->nThreadId;
  1039. uint32_t nNumThreadsBase = nNumThreads;
  1040. if(S.bContextSwitchAllThreads)
  1041. {
  1042. for(uint32_t i = nContextSwitchStart; i != nContextSwitchEnd; i = (i+1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE)
  1043. {
  1044. MicroProfileContextSwitch CS = S.ContextSwitch[i];
  1045. ThreadIdType nThreadId = CS.nThreadIn;
  1046. if(nThreadId)
  1047. {
  1048. bool bSeen = false;
  1049. for(uint32_t j = 0; j < nNumThreads; ++j)
  1050. {
  1051. if(nThreads[j] == nThreadId)
  1052. {
  1053. bSeen = true;
  1054. break;
  1055. }
  1056. }
  1057. if(!bSeen)
  1058. {
  1059. nThreads[nNumThreads++] = nThreadId;
  1060. }
  1061. }
  1062. if(nNumThreads == MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS)
  1063. {
  1064. S.nOverflow = 10;
  1065. break;
  1066. }
  1067. }
  1068. std::sort(&nThreads[nNumThreadsBase], &nThreads[nNumThreads]);
  1069. }
  1070. uint32_t nStart = nNumThreadsBase;
  1071. if(S.bContextSwitchNoBars)
  1072. nStart = 0;
  1073. for(uint32_t i = nStart; i < nNumThreads; ++i)
  1074. {
  1075. ThreadIdType nThreadId = nThreads[i];
  1076. if(nThreadId)
  1077. {
  1078. char ThreadName[MicroProfileThreadLog::THREAD_MAX_LEN + 16];
  1079. const char* cLocal = MicroProfileIsLocalThread(nThreadId) ? "*": " ";
  1080. #if defined(_WIN32)
  1081. // nThreadId is 32-bit on Windows
  1082. int nStrLen = snprintf(ThreadName, sizeof(ThreadName)-1, "%04x: %s%s", nThreadId, cLocal, i < nNumThreadsBase ? &S.Pool[i]->ThreadName[0] : MICROPROFILE_THREAD_NAME_FROM_ID(nThreadId) );
  1083. #else
  1084. int nStrLen = snprintf(ThreadName, sizeof(ThreadName)-1, "%04" PRIx64 ": %s%s", nThreadId, cLocal, i < nNumThreadsBase ? &S.Pool[i]->ThreadName[0] : MICROPROFILE_THREAD_NAME_FROM_ID(nThreadId) );
  1085. #endif
  1086. uint32_t nThreadColor = UINT32_MAX;
  1087. if(nThreadId == nContextSwitchHoverThreadAfter || nThreadId == nContextSwitchHoverThreadBefore)
  1088. nThreadColor = UI.nHoverColorShared|0x906060;
  1089. MicroProfileDrawDetailedContextSwitchBars(nY+2, nThreadId, nContextSwitchStart, nContextSwitchEnd, nBaseTicksCpu, nBaseY);
  1090. MicroProfileDrawText(0, nY, nThreadColor, &ThreadName[0], nStrLen);
  1091. nY += MICROPROFILE_TEXT_HEIGHT+1;
  1092. }
  1093. }
  1094. }
  1095. S.nContextSwitchHoverCpu = S.nContextSwitchHoverCpuNext;
  1096. UI.pDisplayMouseOver = pMouseOverNext;
  1097. if(!S.nRunning)
  1098. {
  1099. if(nHoverToken != MICROPROFILE_INVALID_TOKEN && nHoverTime)
  1100. {
  1101. UI.nHoverToken = nHoverToken;
  1102. UI.nHoverTime = nHoverTime;
  1103. }
  1104. if(nSelectedFrame != -1)
  1105. {
  1106. UI.nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu;
  1107. UI.nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu;
  1108. UI.nRangeBeginGpu = S.Frames[nSelectedFrame].nFrameStartGpu;
  1109. UI.nRangeEndGpu = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartGpu;
  1110. }
  1111. if(UI.nRangeBegin != UI.nRangeEnd)
  1112. {
  1113. float fMsStart = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeBegin);
  1114. float fMsEnd = fToMsCpu * MicroProfileLogTickDifference(nBaseTicksCpu, UI.nRangeEnd);
  1115. float fXStart = fMsStart * fMsToScreen;
  1116. float fXEnd = fMsEnd * fMsToScreen;
  1117. MicroProfileDrawBox(fXStart, nBaseY, fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat);
  1118. MicroProfileDrawLineVertical(fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000);
  1119. MicroProfileDrawLineVertical(fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT | 0x44000000);
  1120. fMsStart += fDetailedOffset;
  1121. fMsEnd += fDetailedOffset;
  1122. char sBuffer[32];
  1123. uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart);
  1124. float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart);
  1125. float fStartTextX = fXStart - fStartTextWidth - 2;
  1126. MicroProfileDrawBox(fStartTextX, nBaseY, fStartTextX + fStartTextWidth + 2, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1127. MicroProfileDrawText(fStartTextX+1, nBaseY, UINT32_MAX, sBuffer, nLenStart);
  1128. uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd);
  1129. MicroProfileDrawBox(fXEnd+1, nBaseY, fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1130. MicroProfileDrawText(fXEnd+2, nBaseY+1, UINT32_MAX, sBuffer, nLenEnd);
  1131. if(UI.nMouseRight)
  1132. {
  1133. MicroProfileZoomTo(UI.nRangeBegin, UI.nRangeEnd);
  1134. }
  1135. }
  1136. if(UI.nRangeBeginGpu != UI.nRangeEndGpu)
  1137. {
  1138. float fMsStart = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeBeginGpu);
  1139. float fMsEnd = fToMsGpu * MicroProfileLogTickDifference(nBaseTicksGpu, UI.nRangeEndGpu);
  1140. float fXStart = fMsStart * fMsToScreen;
  1141. float fXEnd = fMsEnd * fMsToScreen;
  1142. MicroProfileDrawBox(fXStart, nBaseY, fXEnd, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU, MicroProfileBoxTypeFlat);
  1143. MicroProfileDrawLineVertical(fXStart, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000);
  1144. MicroProfileDrawLineVertical(fXEnd, nBaseY, nHeight, MICROPROFILE_FRAME_COLOR_HIGHTLIGHT_GPU | 0x44000000);
  1145. nBaseY += MICROPROFILE_TEXT_HEIGHT+1;
  1146. fMsStart += fDetailedOffset;
  1147. fMsEnd += fDetailedOffset;
  1148. char sBuffer[32];
  1149. uint32_t nLenStart = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsStart);
  1150. float fStartTextWidth = (float)((1+MICROPROFILE_TEXT_WIDTH) * nLenStart);
  1151. float fStartTextX = fXStart - fStartTextWidth - 2;
  1152. MicroProfileDrawBox(fStartTextX, nBaseY, fStartTextX + fStartTextWidth + 2, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1153. MicroProfileDrawText(fStartTextX+1, nBaseY, UINT32_MAX, sBuffer, nLenStart);
  1154. uint32_t nLenEnd = snprintf(sBuffer, sizeof(sBuffer)-1, "%.2fms", fMsEnd);
  1155. MicroProfileDrawBox(fXEnd+1, nBaseY, fXEnd+1+(1+MICROPROFILE_TEXT_WIDTH) * nLenEnd + 3, MICROPROFILE_TEXT_HEIGHT + 2 + nBaseY, 0x33000000, MicroProfileBoxTypeFlat);
  1156. MicroProfileDrawText(fXEnd+2, nBaseY+1, UINT32_MAX, sBuffer, nLenEnd);
  1157. }
  1158. }
  1159. }
  1160. inline void MicroProfileDrawDetailedFrameHistory(uint32_t nWidth, uint32_t nHeight, uint32_t nBaseY, uint32_t nSelectedFrame)
  1161. {
  1162. MicroProfile& S = *MicroProfileGet();
  1163. const uint32_t nBarHeight = MICROPROFILE_FRAME_HISTORY_HEIGHT;
  1164. float fBaseX = (float)nWidth;
  1165. float fDx = fBaseX / MICROPROFILE_NUM_FRAMES;
  1166. uint32_t nLastIndex = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY;
  1167. MicroProfileDrawBox(0, nBaseY, nWidth, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, 0xff000000 | g_nMicroProfileBackColors[0], MicroProfileBoxTypeFlat);
  1168. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * S.fRcpReferenceTime;
  1169. float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()) * S.fRcpReferenceTime;
  1170. MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent];
  1171. uint64_t nFrameStartCpu = pFrameCurrent->nFrameStartCpu;
  1172. int64_t nDetailedOffsetTicksCpu = MicroProfileMsToTick(UI.fDetailedOffset, MicroProfileTicksPerSecondCpu());
  1173. int64_t nCpuStart = nDetailedOffsetTicksCpu + nFrameStartCpu;
  1174. int64_t nCpuEnd = nCpuStart + MicroProfileMsToTick(UI.fDetailedRange, MicroProfileTicksPerSecondCpu());;
  1175. float fSelectionStart = (float)nWidth;
  1176. float fSelectionEnd = 0.f;
  1177. for(uint32_t i = 0; i < MICROPROFILE_NUM_FRAMES; ++i)
  1178. {
  1179. uint32_t nIndex = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY;
  1180. MicroProfileFrameState* pCurrent = &S.Frames[nIndex];
  1181. MicroProfileFrameState* pNext = &S.Frames[nLastIndex];
  1182. int64_t nTicks = pNext->nFrameStartCpu - pCurrent->nFrameStartCpu;
  1183. int64_t nTicksGpu = pNext->nFrameStartGpu - pCurrent->nFrameStartGpu;
  1184. float fScale = fToMs * nTicks;
  1185. float fScaleGpu = fToMsGpu * nTicksGpu;
  1186. fScale = fScale > 1.f ? 0.f : 1.f - fScale;
  1187. fScaleGpu = fScaleGpu > 1.f ? 0.f : 1.f - fScaleGpu;
  1188. float fXEnd = fBaseX;
  1189. float fXStart = fBaseX - fDx;
  1190. fBaseX = fXStart;
  1191. uint32_t nColor = MICROPROFILE_FRAME_HISTORY_COLOR_CPU;
  1192. if(nIndex == nSelectedFrame)
  1193. nColor = UINT32_MAX;
  1194. MicroProfileDrawBox(fXStart, nBaseY + fScale * nBarHeight, fXEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, nColor, MicroProfileBoxTypeBar);
  1195. if(pNext->nFrameStartCpu > nCpuStart)
  1196. {
  1197. fSelectionStart = fXStart;
  1198. }
  1199. if(pCurrent->nFrameStartCpu < nCpuEnd && fSelectionEnd == 0.f)
  1200. {
  1201. fSelectionEnd = fXEnd;
  1202. }
  1203. nLastIndex = nIndex;
  1204. }
  1205. MicroProfileDrawBox(fSelectionStart, nBaseY, fSelectionEnd, nBaseY+MICROPROFILE_FRAME_HISTORY_HEIGHT, MICROPROFILE_FRAME_HISTORY_COLOR_HIGHTLIGHT, MicroProfileBoxTypeFlat);
  1206. }
  1207. inline void MicroProfileDrawDetailedView(uint32_t nWidth, uint32_t nHeight)
  1208. {
  1209. MicroProfile& S = *MicroProfileGet();
  1210. MICROPROFILE_SCOPE(g_MicroProfileDetailed);
  1211. uint32_t nBaseY = MICROPROFILE_TEXT_HEIGHT + 1;
  1212. int nSelectedFrame = -1;
  1213. if(UI.nMouseY > nBaseY && UI.nMouseY <= nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT && UI.nActiveMenu == UINT32_MAX)
  1214. {
  1215. nSelectedFrame = ((MICROPROFILE_NUM_FRAMES) * (UI.nWidth-UI.nMouseX) / UI.nWidth);
  1216. nSelectedFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nSelectedFrame) % MICROPROFILE_MAX_FRAME_HISTORY;
  1217. UI.nHoverFrame = nSelectedFrame;
  1218. if(UI.nMouseRight)
  1219. {
  1220. int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu;
  1221. int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu;
  1222. MicroProfileZoomTo(nRangeBegin, nRangeEnd);
  1223. }
  1224. if(UI.nMouseDownLeft)
  1225. {
  1226. uint64_t nFrac = (1024 * (MICROPROFILE_NUM_FRAMES) * (UI.nMouseX) / UI.nWidth) % 1024;
  1227. int64_t nRangeBegin = S.Frames[nSelectedFrame].nFrameStartCpu;
  1228. int64_t nRangeEnd = S.Frames[(nSelectedFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu;
  1229. MicroProfileCenter(nRangeBegin + (nRangeEnd-nRangeBegin) * nFrac / 1024);
  1230. }
  1231. }
  1232. else
  1233. {
  1234. UI.nHoverFrame = -1;
  1235. }
  1236. MicroProfileDrawDetailedBars(nWidth, nHeight, nBaseY + MICROPROFILE_FRAME_HISTORY_HEIGHT, nSelectedFrame);
  1237. MicroProfileDrawDetailedFrameHistory(nWidth, nHeight, nBaseY, nSelectedFrame);
  1238. }
  1239. inline void MicroProfileDrawTextRight(uint32_t nX, uint32_t nY, uint32_t nColor, const char* pStr, uint32_t nStrLen)
  1240. {
  1241. MicroProfileDrawText(nX - nStrLen * (MICROPROFILE_TEXT_WIDTH+1), nY, nColor, pStr, nStrLen);
  1242. }
  1243. inline void MicroProfileDrawHeader(int32_t nX, uint32_t nWidth, const char* pName)
  1244. {
  1245. if(pName)
  1246. {
  1247. MicroProfileDrawBox(nX-8, MICROPROFILE_TEXT_HEIGHT + 2, nX + nWidth+5, MICROPROFILE_TEXT_HEIGHT + 2 + (MICROPROFILE_TEXT_HEIGHT+1), 0xff000000|g_nMicroProfileBackColors[1]);
  1248. MicroProfileDrawText(nX, MICROPROFILE_TEXT_HEIGHT + 2, UINT32_MAX, pName, (uint32_t)strlen(pName));
  1249. }
  1250. }
  1251. typedef void (*MicroProfileLoopGroupCallback)(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pData);
  1252. inline void MicroProfileLoopActiveGroupsDraw(int32_t nX, int32_t nY, const char* pName, MicroProfileLoopGroupCallback CB, void* pData)
  1253. {
  1254. MicroProfile& S = *MicroProfileGet();
  1255. nY += MICROPROFILE_TEXT_HEIGHT + 2;
  1256. uint64_t nGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted;
  1257. uint32_t nCount = 0;
  1258. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1259. {
  1260. uint64_t nMask = 1ULL << j;
  1261. if(nMask & nGroup)
  1262. {
  1263. nY += MICROPROFILE_TEXT_HEIGHT + 1;
  1264. for(uint32_t i = 0; i < S.nTotalTimers;++i)
  1265. {
  1266. uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken);
  1267. if(nTokenMask & nMask)
  1268. {
  1269. if(nY >= 0)
  1270. CB(i, nCount, nMask, nX, nY, pData);
  1271. nCount += 2;
  1272. nY += MICROPROFILE_TEXT_HEIGHT + 1;
  1273. if(nY > (int)UI.nHeight)
  1274. return;
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. inline void MicroProfileCalcTimers(float* pTimers, float* pAverage, float* pMax, float* pCallAverage, float* pExclusive, float* pAverageExclusive, float* pMaxExclusive, uint64_t nGroup, uint32_t nSize)
  1281. {
  1282. MicroProfile& S = *MicroProfileGet();
  1283. uint32_t nCount = 0;
  1284. uint64_t nMask = 1;
  1285. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1286. {
  1287. if(nMask & nGroup)
  1288. {
  1289. const float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[j].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  1290. for(uint32_t i = 0; i < S.nTotalTimers;++i)
  1291. {
  1292. uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken);
  1293. if(nTokenMask & nMask)
  1294. {
  1295. {
  1296. uint32_t nTimer = i;
  1297. uint32_t nIdx = nCount;
  1298. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  1299. uint32_t nAggregateCount = S.Aggregate[nTimer].nCount ? S.Aggregate[nTimer].nCount : 1;
  1300. float fToPrc = S.fRcpReferenceTime;
  1301. float fMs = fToMs * (S.Frame[nTimer].nTicks);
  1302. float fPrc = MicroProfileMin(fMs * fToPrc, 1.f);
  1303. float fAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateFrames);
  1304. float fAveragePrc = MicroProfileMin(fAverageMs * fToPrc, 1.f);
  1305. float fMaxMs = fToMs * (S.AggregateMax[nTimer]);
  1306. float fMaxPrc = MicroProfileMin(fMaxMs * fToPrc, 1.f);
  1307. float fCallAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateCount);
  1308. float fCallAveragePrc = MicroProfileMin(fCallAverageMs * fToPrc, 1.f);
  1309. float fMsExclusive = fToMs * (S.FrameExclusive[nTimer]);
  1310. float fPrcExclusive = MicroProfileMin(fMsExclusive * fToPrc, 1.f);
  1311. float fAverageMsExclusive = fToMs * (S.AggregateExclusive[nTimer] / nAggregateFrames);
  1312. float fAveragePrcExclusive = MicroProfileMin(fAverageMsExclusive * fToPrc, 1.f);
  1313. float fMaxMsExclusive = fToMs * (S.AggregateMaxExclusive[nTimer]);
  1314. float fMaxPrcExclusive = MicroProfileMin(fMaxMsExclusive * fToPrc, 1.f);
  1315. pTimers[nIdx] = fMs;
  1316. pTimers[nIdx+1] = fPrc;
  1317. pAverage[nIdx] = fAverageMs;
  1318. pAverage[nIdx+1] = fAveragePrc;
  1319. pMax[nIdx] = fMaxMs;
  1320. pMax[nIdx+1] = fMaxPrc;
  1321. pCallAverage[nIdx] = fCallAverageMs;
  1322. pCallAverage[nIdx+1] = fCallAveragePrc;
  1323. pExclusive[nIdx] = fMsExclusive;
  1324. pExclusive[nIdx+1] = fPrcExclusive;
  1325. pAverageExclusive[nIdx] = fAverageMsExclusive;
  1326. pAverageExclusive[nIdx+1] = fAveragePrcExclusive;
  1327. pMaxExclusive[nIdx] = fMaxMsExclusive;
  1328. pMaxExclusive[nIdx+1] = fMaxPrcExclusive;
  1329. }
  1330. nCount += 2;
  1331. }
  1332. }
  1333. }
  1334. nMask <<= 1;
  1335. }
  1336. }
  1337. #define SBUF_MAX 32
  1338. inline void MicroProfileDrawBarArrayCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1339. {
  1340. const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT;
  1341. const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH);
  1342. const float fWidth = (float)MICROPROFILE_BAR_WIDTH;
  1343. float* pTimers = ((float**)pExtra)[0];
  1344. float* pTimers2 = ((float**)pExtra)[1];
  1345. MicroProfile& S = *MicroProfileGet();
  1346. char sBuffer[SBUF_MAX];
  1347. if (pTimers2 && pTimers2[nIdx] > 0.1f)
  1348. snprintf(sBuffer, SBUF_MAX-1, "%5.2f %3.1fx", pTimers[nIdx], pTimers[nIdx] / pTimers2[nIdx]);
  1349. else
  1350. snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pTimers[nIdx]);
  1351. if (!pTimers2)
  1352. MicroProfileDrawBox(nX + nTextWidth, nY, nX + nTextWidth + fWidth * pTimers[nIdx+1], nY + nHeight, UI.nOpacityForeground|S.TimerInfo[nTimer].nColor, MicroProfileBoxTypeBar);
  1353. MicroProfileDrawText(nX, nY, UINT32_MAX, sBuffer, (uint32_t)strlen(sBuffer));
  1354. }
  1355. inline uint32_t MicroProfileDrawBarArray(int32_t nX, int32_t nY, float* pTimers, const char* pName, uint32_t nTotalHeight, float* pTimers2 = NULL)
  1356. {
  1357. const uint32_t nTextWidth = 6 * (1+MICROPROFILE_TEXT_WIDTH);
  1358. const uint32_t nWidth = MICROPROFILE_BAR_WIDTH;
  1359. MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1360. float* pTimersArray[2] = {pTimers, pTimers2};
  1361. MicroProfileLoopActiveGroupsDraw(nX, nY, pName, MicroProfileDrawBarArrayCallback, pTimersArray);
  1362. MicroProfileDrawHeader(nX, nTextWidth + nWidth, pName);
  1363. return nWidth + 5 + nTextWidth;
  1364. }
  1365. inline void MicroProfileDrawBarCallCountCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1366. {
  1367. MicroProfile& S = *MicroProfileGet();
  1368. char sBuffer[SBUF_MAX];
  1369. int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5d", S.Frame[nTimer].nCount);//fix
  1370. MicroProfileDrawText(nX, nY, UINT32_MAX, sBuffer, nLen);
  1371. }
  1372. inline uint32_t MicroProfileDrawBarCallCount(int32_t nX, int32_t nY, const char* pName)
  1373. {
  1374. MicroProfileLoopActiveGroupsDraw(nX, nY, pName, MicroProfileDrawBarCallCountCallback, 0);
  1375. const uint32_t nTextWidth = 6 * MICROPROFILE_TEXT_WIDTH;
  1376. MicroProfileDrawHeader(nX, 5 + nTextWidth, pName);
  1377. return 5 + nTextWidth;
  1378. }
  1379. struct MicroProfileMetaAverageArgs
  1380. {
  1381. uint64_t* pCounters;
  1382. float fRcpFrames;
  1383. };
  1384. inline void MicroProfileDrawBarMetaAverageCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1385. {
  1386. MicroProfileMetaAverageArgs* pArgs = (MicroProfileMetaAverageArgs*)pExtra;
  1387. uint64_t* pCounters = pArgs->pCounters;
  1388. float fRcpFrames = pArgs->fRcpFrames;
  1389. char sBuffer[SBUF_MAX];
  1390. int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5.2f", pCounters[nTimer] * fRcpFrames);
  1391. MicroProfileDrawText(nX - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, UINT32_MAX, sBuffer, nLen);
  1392. }
  1393. inline uint32_t MicroProfileDrawBarMetaAverage(int32_t nX, int32_t nY, uint64_t* pCounters, const char* pName, uint32_t nTotalHeight)
  1394. {
  1395. if(!pName)
  1396. return 0;
  1397. MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1398. uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax<uint32_t>(6, (uint32_t)strlen(pName));
  1399. float fRcpFrames = 1.f / (MicroProfileGet()->nAggregateFrames ? MicroProfileGet()->nAggregateFrames : 1);
  1400. MicroProfileMetaAverageArgs Args = {pCounters, fRcpFrames};
  1401. MicroProfileLoopActiveGroupsDraw(nX + nTextWidth, nY, pName, MicroProfileDrawBarMetaAverageCallback, &Args);
  1402. MicroProfileDrawHeader(nX, 5 + nTextWidth, pName);
  1403. return 5 + nTextWidth;
  1404. }
  1405. inline void MicroProfileDrawBarMetaCountCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1406. {
  1407. uint64_t* pCounters = (uint64_t*)pExtra;
  1408. char sBuffer[SBUF_MAX];
  1409. int nLen = snprintf(sBuffer, SBUF_MAX-1, "%5" PRIu64, pCounters[nTimer]);
  1410. MicroProfileDrawText(nX - nLen * (MICROPROFILE_TEXT_WIDTH+1), nY, UINT32_MAX, sBuffer, nLen);
  1411. }
  1412. inline uint32_t MicroProfileDrawBarMetaCount(int32_t nX, int32_t nY, uint64_t* pCounters, const char* pName, uint32_t nTotalHeight)
  1413. {
  1414. if(!pName)
  1415. return 0;
  1416. MicroProfileDrawLineVertical(nX-5, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1417. uint32_t nTextWidth = (1+MICROPROFILE_TEXT_WIDTH) * MicroProfileMax<uint32_t>(6, (uint32_t)strlen(pName));
  1418. MicroProfileLoopActiveGroupsDraw(nX + nTextWidth, nY, pName, MicroProfileDrawBarMetaCountCallback, pCounters);
  1419. MicroProfileDrawHeader(nX, 5 + nTextWidth, pName);
  1420. return 5 + nTextWidth;
  1421. }
  1422. inline void MicroProfileDrawBarLegendCallback(uint32_t nTimer, uint32_t nIdx, uint64_t nGroupMask, uint32_t nX, uint32_t nY, void* pExtra)
  1423. {
  1424. MicroProfile& S = *MicroProfileGet();
  1425. if (S.TimerInfo[nTimer].bGraph)
  1426. {
  1427. MicroProfileDrawText(nX, nY, S.TimerInfo[nTimer].nColor, ">", 1);
  1428. }
  1429. MicroProfileDrawTextRight(nX, nY, S.TimerInfo[nTimer].nColor, S.TimerInfo[nTimer].pName, (uint32_t)strlen(S.TimerInfo[nTimer].pName));
  1430. if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT+1)
  1431. {
  1432. UI.nHoverToken = nTimer;
  1433. UI.nHoverTime = 0;
  1434. }
  1435. }
  1436. inline uint32_t MicroProfileDrawBarLegend(int32_t nX, int32_t nY, uint32_t nTotalHeight, uint32_t nMaxWidth)
  1437. {
  1438. MicroProfileDrawLineVertical(nX-5, nY, nTotalHeight, UI.nOpacityBackground | g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1439. MicroProfileLoopActiveGroupsDraw(nMaxWidth, nY, 0, MicroProfileDrawBarLegendCallback, 0);
  1440. return nX;
  1441. }
  1442. bool MicroProfileDrawGraph(uint32_t nScreenWidth, uint32_t nScreenHeight)
  1443. {
  1444. MicroProfile& S = *MicroProfileGet();
  1445. MICROPROFILE_SCOPE(g_MicroProfileDrawGraph);
  1446. bool bEnabled = false;
  1447. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1448. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  1449. bEnabled = true;
  1450. if(!bEnabled)
  1451. return false;
  1452. uint32_t nX = nScreenWidth - MICROPROFILE_GRAPH_WIDTH;
  1453. uint32_t nY = nScreenHeight - MICROPROFILE_GRAPH_HEIGHT;
  1454. MicroProfileDrawBox(nX, nY, nX + MICROPROFILE_GRAPH_WIDTH, nY + MICROPROFILE_GRAPH_HEIGHT, 0x88000000 | g_nMicroProfileBackColors[0]);
  1455. bool bMouseOver = UI.nMouseX >= nX && UI.nMouseY >= nY;
  1456. float fMouseXPrc =(float(UI.nMouseX - nX)) / MICROPROFILE_GRAPH_WIDTH;
  1457. if(bMouseOver)
  1458. {
  1459. float fXAvg = fMouseXPrc * MICROPROFILE_GRAPH_WIDTH + nX;
  1460. MicroProfileDrawLineVertical(fXAvg, nY, nY + MICROPROFILE_GRAPH_HEIGHT, UINT32_MAX);
  1461. }
  1462. float fY = (float)nScreenHeight;
  1463. float fDX = MICROPROFILE_GRAPH_WIDTH * 1.f / MICROPROFILE_GRAPH_HISTORY;
  1464. float fDY = MICROPROFILE_GRAPH_HEIGHT;
  1465. uint32_t nPut = S.nGraphPut;
  1466. float* pGraphData = (float*)alloca(sizeof(float)* MICROPROFILE_GRAPH_HISTORY*2);
  1467. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1468. {
  1469. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  1470. {
  1471. uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken);
  1472. bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu;
  1473. float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  1474. float fToPrc = fToMs * S.fRcpReferenceTime * 3 / 4;
  1475. float fX = (float)nX;
  1476. for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j)
  1477. {
  1478. float fWeigth = MicroProfileMin(fToPrc * (S.Graph[i].nHistory[(j+nPut)%MICROPROFILE_GRAPH_HISTORY]), 1.f);
  1479. pGraphData[(j*2)] = fX;
  1480. pGraphData[(j*2)+1] = fY - fDY * fWeigth;
  1481. fX += fDX;
  1482. }
  1483. MicroProfileDrawLine2D(MICROPROFILE_GRAPH_HISTORY, pGraphData, S.TimerInfo[MicroProfileGetTimerIndex(S.Graph[i].nToken)].nColor);
  1484. }
  1485. }
  1486. {
  1487. float fY1 = 0.25f * MICROPROFILE_GRAPH_HEIGHT + nY;
  1488. float fY2 = 0.50f * MICROPROFILE_GRAPH_HEIGHT + nY;
  1489. float fY3 = 0.75f * MICROPROFILE_GRAPH_HEIGHT + nY;
  1490. MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY1, 0xffdd4444);
  1491. MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY2, 0xff000000| g_nMicroProfileBackColors[0]);
  1492. MicroProfileDrawLineHorizontal(nX, nX + MICROPROFILE_GRAPH_WIDTH, fY3, 0xff000000|g_nMicroProfileBackColors[0]);
  1493. char buf[32];
  1494. int nLen = snprintf(buf, sizeof(buf)-1, "%5.2fms", S.fReferenceTime);
  1495. MicroProfileDrawText(nX+1, fY1 - (2+MICROPROFILE_TEXT_HEIGHT), UINT32_MAX, buf, nLen);
  1496. }
  1497. if(bMouseOver)
  1498. {
  1499. uint32_t pColors[MICROPROFILE_MAX_GRAPHS];
  1500. MicroProfileStringArray Strings;
  1501. MicroProfileStringArrayClear(&Strings);
  1502. uint32_t nTextCount = 0;
  1503. uint32_t nGraphIndex = (S.nGraphPut + MICROPROFILE_GRAPH_HISTORY - int(MICROPROFILE_GRAPH_HISTORY*(1.f - fMouseXPrc))) % MICROPROFILE_GRAPH_HISTORY;
  1504. uint32_t nX = UI.nMouseX;
  1505. uint32_t nY = UI.nMouseY + 20;
  1506. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1507. {
  1508. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  1509. {
  1510. uint32_t nGroupId = MicroProfileGetGroupIndex(S.Graph[i].nToken);
  1511. bool bGpu = S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu;
  1512. float fToMs = MicroProfileTickToMsMultiplier(bGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  1513. uint32_t nIndex = MicroProfileGetTimerIndex(S.Graph[i].nToken);
  1514. uint32_t nColor = S.TimerInfo[nIndex].nColor;
  1515. const char* pName = S.TimerInfo[nIndex].pName;
  1516. pColors[nTextCount++] = nColor;
  1517. MicroProfileStringArrayAddLiteral(&Strings, pName);
  1518. MicroProfileStringArrayFormat(&Strings, "%5.2fms", fToMs * (S.Graph[i].nHistory[nGraphIndex]));
  1519. }
  1520. }
  1521. if(nTextCount)
  1522. {
  1523. MicroProfileDrawFloatWindow(nX, nY, Strings.ppStrings, Strings.nNumStrings, 0, pColors);
  1524. }
  1525. if(UI.nMouseRight)
  1526. {
  1527. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  1528. {
  1529. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  1530. }
  1531. }
  1532. }
  1533. return bMouseOver;
  1534. }
  1535. void MicroProfileDumpTimers()
  1536. {
  1537. MicroProfile& S = *MicroProfileGet();
  1538. uint64_t nActiveGroup = S.nGroupMask;
  1539. uint32_t nNumTimers = S.nTotalTimers;
  1540. uint32_t nBlockSize = 2 * nNumTimers;
  1541. float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float));
  1542. float* pAverage = pTimers + nBlockSize;
  1543. float* pMax = pTimers + 2 * nBlockSize;
  1544. float* pCallAverage = pTimers + 3 * nBlockSize;
  1545. float* pTimersExclusive = pTimers + 4 * nBlockSize;
  1546. float* pAverageExclusive = pTimers + 5 * nBlockSize;
  1547. float* pMaxExclusive = pTimers + 6 * nBlockSize;
  1548. MicroProfileCalcTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nNumTimers);
  1549. MICROPROFILE_PRINTF("%11s, ", "Time");
  1550. MICROPROFILE_PRINTF("%11s, ", "Average");
  1551. MICROPROFILE_PRINTF("%11s, ", "Max");
  1552. MICROPROFILE_PRINTF("%11s, ", "Call Avg");
  1553. MICROPROFILE_PRINTF("%9s, ", "Count");
  1554. MICROPROFILE_PRINTF("%11s, ", "Excl");
  1555. MICROPROFILE_PRINTF("%11s, ", "Avg Excl");
  1556. MICROPROFILE_PRINTF("%11s, \n", "Max Excl");
  1557. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1558. {
  1559. uint64_t nMask = 1ULL << j;
  1560. if(nMask & nActiveGroup)
  1561. {
  1562. MICROPROFILE_PRINTF("%s\n", S.GroupInfo[j].pName);
  1563. for(uint32_t i = 0; i < S.nTotalTimers;++i)
  1564. {
  1565. uint64_t nTokenMask = MicroProfileGetGroupMask(S.TimerInfo[i].nToken);
  1566. if(nTokenMask & nMask)
  1567. {
  1568. uint32_t nIdx = i * 2;
  1569. MICROPROFILE_PRINTF("%9.2fms, ", pTimers[nIdx]);
  1570. MICROPROFILE_PRINTF("%9.2fms, ", pAverage[nIdx]);
  1571. MICROPROFILE_PRINTF("%9.2fms, ", pMax[nIdx]);
  1572. MICROPROFILE_PRINTF("%9.2fms, ", pCallAverage[nIdx]);
  1573. MICROPROFILE_PRINTF("%9d, ", S.Frame[i].nCount);
  1574. MICROPROFILE_PRINTF("%9.2fms, ", pTimersExclusive[nIdx]);
  1575. MICROPROFILE_PRINTF("%9.2fms, ", pAverageExclusive[nIdx]);
  1576. MICROPROFILE_PRINTF("%9.2fms, ", pMaxExclusive[nIdx]);
  1577. MICROPROFILE_PRINTF("%s\n", S.TimerInfo[i].pName);
  1578. }
  1579. }
  1580. }
  1581. }
  1582. }
  1583. inline void MicroProfileDrawBarView(uint32_t nScreenWidth, uint32_t nScreenHeight)
  1584. {
  1585. MicroProfile& S = *MicroProfileGet();
  1586. uint64_t nActiveGroup = S.nAllGroupsWanted ? S.nGroupMask : S.nActiveGroupWanted;
  1587. if(!nActiveGroup)
  1588. return;
  1589. MICROPROFILE_SCOPE(g_MicroProfileDrawBarView);
  1590. const uint32_t nHeight = MICROPROFILE_TEXT_HEIGHT;
  1591. int nColorIndex = 0;
  1592. uint32_t nMaxTimerNameLen = 1;
  1593. uint32_t nNumTimers = 0;
  1594. uint32_t nNumGroups = 0;
  1595. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1596. {
  1597. if(nActiveGroup & (1ULL << j))
  1598. {
  1599. nNumTimers += S.GroupInfo[j].nNumTimers;
  1600. nNumGroups += 1;
  1601. nMaxTimerNameLen = MicroProfileMax(nMaxTimerNameLen, S.GroupInfo[j].nMaxTimerNameLen);
  1602. }
  1603. }
  1604. uint32_t nTimerWidth = 2+(4+nMaxTimerNameLen) * (MICROPROFILE_TEXT_WIDTH+1);
  1605. uint32_t nX = nTimerWidth + UI.nOffsetX;
  1606. uint32_t nY = nHeight + 3 - UI.nOffsetY;
  1607. uint32_t nBlockSize = 2 * nNumTimers;
  1608. float* pTimers = (float*)alloca(nBlockSize * 7 * sizeof(float));
  1609. float* pAverage = pTimers + nBlockSize;
  1610. float* pMax = pTimers + 2 * nBlockSize;
  1611. float* pCallAverage = pTimers + 3 * nBlockSize;
  1612. float* pTimersExclusive = pTimers + 4 * nBlockSize;
  1613. float* pAverageExclusive = pTimers + 5 * nBlockSize;
  1614. float* pMaxExclusive = pTimers + 6 * nBlockSize;
  1615. MicroProfileCalcTimers(pTimers, pAverage, pMax, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, nActiveGroup, nNumTimers);
  1616. uint32_t nWidth = 0;
  1617. {
  1618. uint32_t nMetaIndex = 0;
  1619. for(uint32_t i = 1; i ; i <<= 1)
  1620. {
  1621. if(S.nBars & i)
  1622. {
  1623. if(i >= MP_DRAW_META_FIRST)
  1624. {
  1625. if(nMetaIndex < MICROPROFILE_META_MAX && S.MetaCounters[nMetaIndex].pName)
  1626. {
  1627. uint32_t nStrWidth = static_cast<uint32_t>(strlen(S.MetaCounters[nMetaIndex].pName));
  1628. if(S.nBars & MP_DRAW_TIMERS)
  1629. nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth);
  1630. if(S.nBars & MP_DRAW_AVERAGE)
  1631. nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4);
  1632. if(S.nBars & MP_DRAW_MAX)
  1633. nWidth += 6 + (1+MICROPROFILE_TEXT_WIDTH) * (nStrWidth + 4);
  1634. }
  1635. }
  1636. else
  1637. {
  1638. nWidth += MICROPROFILE_BAR_WIDTH + 6 + 6 * (1+MICROPROFILE_TEXT_WIDTH);
  1639. if(i & MP_DRAW_CALL_COUNT)
  1640. nWidth += 6 + 6 * MICROPROFILE_TEXT_WIDTH;
  1641. }
  1642. }
  1643. if(i >= MP_DRAW_META_FIRST)
  1644. {
  1645. ++nMetaIndex;
  1646. }
  1647. }
  1648. nWidth += (1+nMaxTimerNameLen) * (MICROPROFILE_TEXT_WIDTH+1);
  1649. for(uint32_t i = 0; i < nNumTimers+nNumGroups+1; ++i)
  1650. {
  1651. uint32_t nY0 = nY + i * (nHeight + 1);
  1652. bool bInside = (UI.nActiveMenu == UINT32_MAX) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nHeight + 1)));
  1653. MicroProfileDrawBox(nX, nY0, nWidth+nX, nY0 + (nHeight+1)+1, UI.nOpacityBackground | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0)));
  1654. }
  1655. nX += 10;
  1656. }
  1657. int nTotalHeight = (nNumTimers+nNumGroups+1) * (nHeight+1);
  1658. uint32_t nLegendOffset = 1;
  1659. if(S.nBars & MP_DRAW_TIMERS)
  1660. nX += MicroProfileDrawBarArray(nX, nY, pTimers, "Time", nTotalHeight) + 1;
  1661. if(S.nBars & MP_DRAW_AVERAGE)
  1662. nX += MicroProfileDrawBarArray(nX, nY, pAverage, "Average", nTotalHeight) + 1;
  1663. if(S.nBars & MP_DRAW_MAX)
  1664. nX += MicroProfileDrawBarArray(nX, nY, pMax, (!UI.bShowSpikes) ? "Max Time" : "Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverage : NULL) + 1;
  1665. if(S.nBars & MP_DRAW_CALL_COUNT)
  1666. {
  1667. nX += MicroProfileDrawBarArray(nX, nY, pCallAverage, "Call Average", nTotalHeight) + 1;
  1668. nX += MicroProfileDrawBarCallCount(nX, nY, "Count") + 1;
  1669. }
  1670. if(S.nBars & MP_DRAW_TIMERS_EXCLUSIVE)
  1671. nX += MicroProfileDrawBarArray(nX, nY, pTimersExclusive, "Exclusive Time", nTotalHeight) + 1;
  1672. if(S.nBars & MP_DRAW_AVERAGE_EXCLUSIVE)
  1673. nX += MicroProfileDrawBarArray(nX, nY, pAverageExclusive, "Exclusive Average", nTotalHeight) + 1;
  1674. if(S.nBars & MP_DRAW_MAX_EXCLUSIVE)
  1675. nX += MicroProfileDrawBarArray(nX, nY, pMaxExclusive, (!UI.bShowSpikes) ? "Exclusive Max Time" :"Excl Max Time, Spike", nTotalHeight, UI.bShowSpikes ? pAverageExclusive : NULL) + 1;
  1676. for(int i = 0; i < MICROPROFILE_META_MAX; ++i)
  1677. {
  1678. if(0 != (S.nBars & (MP_DRAW_META_FIRST<<i)) && S.MetaCounters[i].pName)
  1679. {
  1680. uint32_t nBufferSize = static_cast<uint32_t>(strlen(S.MetaCounters[i].pName) + 32);
  1681. char* buffer = (char*)alloca(nBufferSize);
  1682. if(S.nBars & MP_DRAW_TIMERS)
  1683. nX += MicroProfileDrawBarMetaCount(nX, nY, &S.MetaCounters[i].nCounters[0], S.MetaCounters[i].pName, nTotalHeight) + 1;
  1684. if(S.nBars & MP_DRAW_AVERAGE)
  1685. {
  1686. snprintf(buffer, nBufferSize-1, "%s Avg", S.MetaCounters[i].pName);
  1687. nX += MicroProfileDrawBarMetaAverage(nX, nY, &S.MetaCounters[i].nAggregate[0], buffer, nTotalHeight) + 1;
  1688. }
  1689. if(S.nBars & MP_DRAW_MAX)
  1690. {
  1691. snprintf(buffer, nBufferSize-1, "%s Max", S.MetaCounters[i].pName);
  1692. nX += MicroProfileDrawBarMetaCount(nX, nY, &S.MetaCounters[i].nAggregateMax[0], buffer, nTotalHeight) + 1;
  1693. }
  1694. }
  1695. }
  1696. nX = 0;
  1697. nY = nHeight + 3 - UI.nOffsetY;
  1698. for(uint32_t i = 0; i < nNumTimers+nNumGroups+1; ++i)
  1699. {
  1700. const uint32_t nY0 = nY + i * (nHeight + 1);
  1701. const bool bInside = (UI.nActiveMenu == UINT32_MAX) && ((UI.nMouseY >= nY0) && (UI.nMouseY < (nY0 + nHeight + 1)));
  1702. MicroProfileDrawBox(nX, nY0, nTimerWidth, nY0 + (nHeight+1)+1, 0xff0000000 | (g_nMicroProfileBackColors[nColorIndex++ & 1] + ((bInside) ? 0x002c2c2c : 0)));
  1703. }
  1704. nX += MicroProfileDrawBarLegend(nX, nY, nTotalHeight, nTimerWidth-5) + 1;
  1705. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  1706. {
  1707. if(nActiveGroup & (1ULL << j))
  1708. {
  1709. MicroProfileDrawText(nX, nY + (1+nHeight) * nLegendOffset, UINT32_MAX, S.GroupInfo[j].pName, S.GroupInfo[j].nNameLen);
  1710. nLegendOffset += S.GroupInfo[j].nNumTimers+1;
  1711. }
  1712. }
  1713. MicroProfileDrawHeader(nX, nTimerWidth-5, "Group");
  1714. MicroProfileDrawTextRight(nTimerWidth-3, MICROPROFILE_TEXT_HEIGHT + 2, UINT32_MAX, "Timer", 5);
  1715. MicroProfileDrawLineVertical(nTimerWidth, 0, nTotalHeight+nY, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1716. MicroProfileDrawLineHorizontal(0, nWidth, 2*MICROPROFILE_TEXT_HEIGHT + 3, UI.nOpacityBackground|g_nMicroProfileBackColors[0]|g_nMicroProfileBackColors[1]);
  1717. }
  1718. typedef const char* (*MicroProfileSubmenuCallback)(int, bool* bSelected);
  1719. typedef void (*MicroProfileClickCallback)(int);
  1720. inline const char* MicroProfileUIMenuMode(int nIndex, bool* bSelected)
  1721. {
  1722. MicroProfile& S = *MicroProfileGet();
  1723. switch(nIndex)
  1724. {
  1725. case 0:
  1726. *bSelected = S.nDisplay == MP_DRAW_DETAILED;
  1727. return "Detailed";
  1728. case 1:
  1729. *bSelected = S.nDisplay == MP_DRAW_BARS;
  1730. return "Timers";
  1731. case 2:
  1732. *bSelected = S.nDisplay == MP_DRAW_HIDDEN;
  1733. return "Hidden";
  1734. case 3:
  1735. *bSelected = true;
  1736. return "Off";
  1737. case 4:
  1738. *bSelected = true;
  1739. return "------";
  1740. case 5:
  1741. *bSelected = S.nForceEnable != 0;
  1742. return "Force Enable";
  1743. default: return 0;
  1744. }
  1745. }
  1746. inline const char* MicroProfileUIMenuGroups(int nIndex, bool* bSelected)
  1747. {
  1748. MicroProfile& S = *MicroProfileGet();
  1749. *bSelected = false;
  1750. if(nIndex == 0)
  1751. {
  1752. *bSelected = S.nAllGroupsWanted != 0;
  1753. return "[ALL]";
  1754. }
  1755. else
  1756. {
  1757. nIndex = nIndex-1;
  1758. if(static_cast<uint32_t>(nIndex) < UI.GroupMenuCount)
  1759. {
  1760. MicroProfileGroupMenuItem& Item = UI.GroupMenu[nIndex];
  1761. static char buffer[MICROPROFILE_NAME_MAX_LEN+32];
  1762. if(Item.nIsCategory)
  1763. {
  1764. uint64_t nGroupMask = S.CategoryInfo[Item.nIndex].nGroupMask;
  1765. *bSelected = nGroupMask == (nGroupMask & S.nActiveGroupWanted);
  1766. snprintf(buffer, sizeof(buffer)-1, "[%s]", Item.pName);
  1767. }
  1768. else
  1769. {
  1770. *bSelected = 0 != (S.nActiveGroupWanted & (1ULL << Item.nIndex));
  1771. snprintf(buffer, sizeof(buffer)-1, " %s", Item.pName);
  1772. }
  1773. return buffer;
  1774. }
  1775. return 0;
  1776. }
  1777. }
  1778. inline const char* MicroProfileUIMenuAggregate(int nIndex, bool* bSelected)
  1779. {
  1780. MicroProfile& S = *MicroProfileGet();
  1781. if(static_cast<uint32_t>(nIndex) < g_MicroProfileAggregatePresets.size())
  1782. {
  1783. uint32_t val = g_MicroProfileAggregatePresets[nIndex];
  1784. *bSelected = S.nAggregateFlip == val;
  1785. if (0 == val)
  1786. {
  1787. return "Infinite";
  1788. }
  1789. else
  1790. {
  1791. static char buf[128];
  1792. snprintf(buf, sizeof(buf)-1, "%7u", val);
  1793. return buf;
  1794. }
  1795. }
  1796. return 0;
  1797. }
  1798. inline const char* MicroProfileUIMenuTimers(int nIndex, bool* bSelected)
  1799. {
  1800. MicroProfile& S = *MicroProfileGet();
  1801. *bSelected = 0 != (S.nBars & (1 << nIndex));
  1802. switch(nIndex)
  1803. {
  1804. case 0: return "Time";
  1805. case 1: return "Average";
  1806. case 2: return "Max";
  1807. case 3: return "Call Count";
  1808. case 4: return "Exclusive Timers";
  1809. case 5: return "Exclusive Average";
  1810. case 6: return "Exclusive Max";
  1811. }
  1812. int nMetaIndex = nIndex - 7;
  1813. if(nMetaIndex < MICROPROFILE_META_MAX)
  1814. {
  1815. return S.MetaCounters[nMetaIndex].pName;
  1816. }
  1817. return 0;
  1818. }
  1819. inline const char* MicroProfileUIMenuOptions(int nIndex, bool* bSelected)
  1820. {
  1821. MicroProfile& S = *MicroProfileGet();
  1822. if(nIndex >= MICROPROFILE_OPTION_SIZE) return 0;
  1823. switch(UI.Options[nIndex].nSubType)
  1824. {
  1825. case 0:
  1826. *bSelected = S.fReferenceTime == g_MicroProfileReferenceTimePresets[UI.Options[nIndex].nIndex];
  1827. break;
  1828. case 1:
  1829. *bSelected = UI.nOpacityBackground>>24 == g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex];
  1830. break;
  1831. case 2:
  1832. *bSelected = UI.nOpacityForeground>>24 == g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex];
  1833. break;
  1834. case 3:
  1835. *bSelected = UI.bShowSpikes;
  1836. break;
  1837. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  1838. case 4:
  1839. {
  1840. switch(UI.Options[nIndex].nIndex)
  1841. {
  1842. case 0:
  1843. *bSelected = S.bContextSwitchRunning;
  1844. break;
  1845. case 1:
  1846. *bSelected = S.bContextSwitchAllThreads;
  1847. break;
  1848. case 2:
  1849. *bSelected = S.bContextSwitchNoBars;
  1850. break;
  1851. }
  1852. }
  1853. break;
  1854. #endif
  1855. }
  1856. return UI.Options[nIndex].Text;
  1857. }
  1858. inline const char* MicroProfileUIMenuPreset(int nIndex, bool* bSelected)
  1859. {
  1860. static char buf[128];
  1861. *bSelected = false;
  1862. int nNumPresets = static_cast<int>(g_MicroProfilePresetNames.size());
  1863. int nIndexSave = nIndex - nNumPresets - 1;
  1864. if (nIndex == nNumPresets)
  1865. {
  1866. return "--";
  1867. }
  1868. else if(nIndexSave >=0 && nIndexSave < nNumPresets)
  1869. {
  1870. snprintf(buf, sizeof(buf)-1, "Save '%s'", g_MicroProfilePresetNames[nIndexSave]);
  1871. return buf;
  1872. }
  1873. else if(nIndex < nNumPresets)
  1874. {
  1875. snprintf(buf, sizeof(buf)-1, "Load '%s'", g_MicroProfilePresetNames[nIndex]);
  1876. return buf;
  1877. }
  1878. else
  1879. {
  1880. return 0;
  1881. }
  1882. }
  1883. inline const char* MicroProfileUIMenuCustom(int nIndex, bool* bSelected)
  1884. {
  1885. if(UINT32_MAX == UI.nCustomActive)
  1886. {
  1887. *bSelected = nIndex == 0;
  1888. }
  1889. else
  1890. {
  1891. *bSelected = nIndex-2 == static_cast<int>(UI.nCustomActive);
  1892. }
  1893. switch(nIndex)
  1894. {
  1895. case 0: return "Disable";
  1896. case 1: return "--";
  1897. default:
  1898. nIndex -= 2;
  1899. if(static_cast<uint32_t>(nIndex) < UI.nCustomCount)
  1900. {
  1901. return UI.Custom[nIndex].pName;
  1902. }
  1903. else
  1904. {
  1905. return 0;
  1906. }
  1907. }
  1908. }
  1909. inline const char* MicroProfileUIMenuEmpty(int nIndex, bool* bSelected)
  1910. {
  1911. return 0;
  1912. }
  1913. inline void MicroProfileUIClickMode(int nIndex)
  1914. {
  1915. MicroProfile& S = *MicroProfileGet();
  1916. switch(nIndex)
  1917. {
  1918. case 0:
  1919. S.nDisplay = MP_DRAW_DETAILED;
  1920. break;
  1921. case 1:
  1922. S.nDisplay = MP_DRAW_BARS;
  1923. break;
  1924. case 2:
  1925. S.nDisplay = MP_DRAW_HIDDEN;
  1926. break;
  1927. case 3:
  1928. S.nDisplay = 0;
  1929. break;
  1930. case 4:
  1931. break;
  1932. case 5:
  1933. S.nForceEnable = !S.nForceEnable;
  1934. break;
  1935. }
  1936. }
  1937. inline void MicroProfileUIClickGroups(int nIndex)
  1938. {
  1939. MicroProfile& S = *MicroProfileGet();
  1940. if(nIndex == 0)
  1941. S.nAllGroupsWanted = 1-S.nAllGroupsWanted;
  1942. else
  1943. {
  1944. nIndex -= 1;
  1945. if(static_cast<uint32_t>(nIndex) < UI.GroupMenuCount)
  1946. {
  1947. MicroProfileGroupMenuItem& Item = UI.GroupMenu[nIndex];
  1948. if(Item.nIsCategory)
  1949. {
  1950. uint64_t nGroupMask = S.CategoryInfo[Item.nIndex].nGroupMask;
  1951. if(nGroupMask != (nGroupMask & S.nActiveGroupWanted))
  1952. {
  1953. S.nActiveGroupWanted |= nGroupMask;
  1954. }
  1955. else
  1956. {
  1957. S.nActiveGroupWanted &= ~nGroupMask;
  1958. }
  1959. }
  1960. else
  1961. {
  1962. MP_ASSERT(Item.nIndex < S.nGroupCount);
  1963. S.nActiveGroupWanted ^= (1ULL << Item.nIndex);
  1964. }
  1965. }
  1966. }
  1967. }
  1968. inline void MicroProfileUIClickAggregate(int nIndex)
  1969. {
  1970. MicroProfile& S = *MicroProfileGet();
  1971. S.nAggregateFlip = g_MicroProfileAggregatePresets[nIndex];
  1972. if(0 == S.nAggregateFlip)
  1973. {
  1974. S.nAggregateClear = 1;
  1975. }
  1976. }
  1977. inline void MicroProfileUIClickTimers(int nIndex)
  1978. {
  1979. MicroProfile& S = *MicroProfileGet();
  1980. S.nBars ^= (1 << nIndex);
  1981. }
  1982. inline void MicroProfileUIClickOptions(int nIndex)
  1983. {
  1984. MicroProfile& S = *MicroProfileGet();
  1985. switch(UI.Options[nIndex].nSubType)
  1986. {
  1987. case 0:
  1988. S.fReferenceTime = g_MicroProfileReferenceTimePresets[UI.Options[nIndex].nIndex];
  1989. S.fRcpReferenceTime = 1.f / S.fReferenceTime;
  1990. break;
  1991. case 1:
  1992. UI.nOpacityBackground = g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex]<<24;
  1993. break;
  1994. case 2:
  1995. UI.nOpacityForeground = g_MicroProfileOpacityPresets[UI.Options[nIndex].nIndex]<<24;
  1996. break;
  1997. case 3:
  1998. UI.bShowSpikes = !UI.bShowSpikes;
  1999. break;
  2000. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  2001. case 4:
  2002. {
  2003. switch(UI.Options[nIndex].nIndex)
  2004. {
  2005. case 0:
  2006. if(S.bContextSwitchRunning)
  2007. {
  2008. MicroProfileStopContextSwitchTrace();
  2009. }
  2010. else
  2011. {
  2012. MicroProfileStartContextSwitchTrace();
  2013. }
  2014. break;
  2015. case 1:
  2016. S.bContextSwitchAllThreads = !S.bContextSwitchAllThreads;
  2017. break;
  2018. case 2:
  2019. S.bContextSwitchNoBars= !S.bContextSwitchNoBars;
  2020. break;
  2021. }
  2022. }
  2023. break;
  2024. #endif
  2025. }
  2026. }
  2027. inline void MicroProfileUIClickPreset(int nIndex)
  2028. {
  2029. int nNumPresets = static_cast<int>(g_MicroProfilePresetNames.size());
  2030. int nIndexSave = nIndex - nNumPresets - 1;
  2031. if(nIndexSave >= 0 && nIndexSave < nNumPresets)
  2032. {
  2033. MicroProfileSavePreset(g_MicroProfilePresetNames[nIndexSave]);
  2034. }
  2035. else if(nIndex >= 0 && nIndex < nNumPresets)
  2036. {
  2037. MicroProfileLoadPreset(g_MicroProfilePresetNames[nIndex]);
  2038. }
  2039. }
  2040. inline void MicroProfileUIClickCustom(int nIndex)
  2041. {
  2042. if(nIndex == 0)
  2043. {
  2044. MicroProfileCustomGroupDisable();
  2045. }
  2046. else
  2047. {
  2048. MicroProfileCustomGroupEnable(nIndex-2);
  2049. }
  2050. }
  2051. inline void MicroProfileUIClickEmpty(int nIndex)
  2052. {
  2053. }
  2054. inline void MicroProfileDrawMenu(uint32_t nWidth, uint32_t nHeight)
  2055. {
  2056. MicroProfile& S = *MicroProfileGet();
  2057. uint32_t nX = 0;
  2058. uint32_t nY = 0;
  2059. #define SBUF_SIZE 256
  2060. char buffer[256];
  2061. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + (MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff000000|g_nMicroProfileBackColors[1]);
  2062. #define MICROPROFILE_MENU_MAX 16
  2063. const char* pMenuText[MICROPROFILE_MENU_MAX] = {0};
  2064. uint32_t nMenuX[MICROPROFILE_MENU_MAX] = {0};
  2065. uint32_t nNumMenuItems = 0;
  2066. int nLen = snprintf(buffer, 127, "MicroProfile");
  2067. MicroProfileDrawText(nX, nY, UINT32_MAX, buffer, nLen);
  2068. nX += (sizeof("MicroProfile")+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2069. pMenuText[nNumMenuItems++] = "Mode";
  2070. pMenuText[nNumMenuItems++] = "Groups";
  2071. char AggregateText[64];
  2072. snprintf(AggregateText, sizeof(AggregateText)-1, "Aggregate[%d]", S.nAggregateFlip ? S.nAggregateFlip : S.nAggregateFlipCount);
  2073. pMenuText[nNumMenuItems++] = &AggregateText[0];
  2074. pMenuText[nNumMenuItems++] = "Timers";
  2075. pMenuText[nNumMenuItems++] = "Options";
  2076. pMenuText[nNumMenuItems++] = "Preset";
  2077. pMenuText[nNumMenuItems++] = "Custom";
  2078. const int nPauseIndex = nNumMenuItems;
  2079. pMenuText[nNumMenuItems++] = S.nRunning ? "Pause" : "Unpause";
  2080. pMenuText[nNumMenuItems++] = "Help";
  2081. if(S.nOverflow)
  2082. {
  2083. pMenuText[nNumMenuItems++] = "!BUFFERSFULL!";
  2084. }
  2085. if(UI.GroupMenuCount != S.nGroupCount + S.nCategoryCount)
  2086. {
  2087. UI.GroupMenuCount = S.nGroupCount + S.nCategoryCount;
  2088. for(uint32_t i = 0; i < S.nCategoryCount; ++i)
  2089. {
  2090. UI.GroupMenu[i].nIsCategory = 1;
  2091. UI.GroupMenu[i].nCategoryIndex = i;
  2092. UI.GroupMenu[i].nIndex = i;
  2093. UI.GroupMenu[i].pName = S.CategoryInfo[i].pName;
  2094. }
  2095. for(uint32_t i = 0; i < S.nGroupCount; ++i)
  2096. {
  2097. uint32_t idx = i + S.nCategoryCount;
  2098. UI.GroupMenu[idx].nIsCategory = 0;
  2099. UI.GroupMenu[idx].nCategoryIndex = S.GroupInfo[i].nCategory;
  2100. UI.GroupMenu[idx].nIndex = i;
  2101. UI.GroupMenu[idx].pName = S.GroupInfo[i].pName;
  2102. }
  2103. std::sort(&UI.GroupMenu[0], &UI.GroupMenu[UI.GroupMenuCount],
  2104. [] (const MicroProfileGroupMenuItem& l, const MicroProfileGroupMenuItem& r) -> bool
  2105. {
  2106. if(l.nCategoryIndex < r.nCategoryIndex)
  2107. {
  2108. return true;
  2109. }
  2110. else if(r.nCategoryIndex < l.nCategoryIndex)
  2111. {
  2112. return false;
  2113. }
  2114. if(r.nIsCategory || l.nIsCategory)
  2115. {
  2116. return l.nIsCategory > r.nIsCategory;
  2117. }
  2118. return MP_STRCASECMP(l.pName, r.pName)<0;
  2119. }
  2120. );
  2121. }
  2122. MicroProfileSubmenuCallback GroupCallback[MICROPROFILE_MENU_MAX] =
  2123. {
  2124. MicroProfileUIMenuMode,
  2125. MicroProfileUIMenuGroups,
  2126. MicroProfileUIMenuAggregate,
  2127. MicroProfileUIMenuTimers,
  2128. MicroProfileUIMenuOptions,
  2129. MicroProfileUIMenuPreset,
  2130. MicroProfileUIMenuCustom,
  2131. MicroProfileUIMenuEmpty,
  2132. MicroProfileUIMenuEmpty,
  2133. MicroProfileUIMenuEmpty,
  2134. };
  2135. MicroProfileClickCallback CBClick[MICROPROFILE_MENU_MAX] =
  2136. {
  2137. MicroProfileUIClickMode,
  2138. MicroProfileUIClickGroups,
  2139. MicroProfileUIClickAggregate,
  2140. MicroProfileUIClickTimers,
  2141. MicroProfileUIClickOptions,
  2142. MicroProfileUIClickPreset,
  2143. MicroProfileUIClickCustom,
  2144. MicroProfileUIClickEmpty,
  2145. MicroProfileUIClickEmpty,
  2146. MicroProfileUIClickEmpty,
  2147. };
  2148. uint32_t nSelectMenu = UINT32_MAX;
  2149. for(uint32_t i = 0; i < nNumMenuItems; ++i)
  2150. {
  2151. nMenuX[i] = nX;
  2152. uint32_t nLen = (uint32_t)strlen(pMenuText[i]);
  2153. uint32_t nEnd = nX + nLen * (MICROPROFILE_TEXT_WIDTH+1);
  2154. if(UI.nMouseY <= MICROPROFILE_TEXT_HEIGHT && UI.nMouseX <= nEnd && UI.nMouseX >= nX)
  2155. {
  2156. MicroProfileDrawBox(nX-1, nY, nX + nLen * (MICROPROFILE_TEXT_WIDTH+1), nY +(MICROPROFILE_TEXT_HEIGHT+1)+1, 0xff888888);
  2157. nSelectMenu = i;
  2158. if((UI.nMouseLeft || UI.nMouseRight) && i == (uint32_t)nPauseIndex)
  2159. {
  2160. S.nToggleRunning = 1;
  2161. }
  2162. }
  2163. MicroProfileDrawText(nX, nY, UINT32_MAX, pMenuText[i], (uint32_t)strlen(pMenuText[i]));
  2164. nX += (nLen+1) * (MICROPROFILE_TEXT_WIDTH+1);
  2165. }
  2166. uint32_t nMenu = nSelectMenu != UINT32_MAX ? nSelectMenu : UI.nActiveMenu;
  2167. UI.nActiveMenu = nMenu;
  2168. if(UINT32_MAX != nMenu)
  2169. {
  2170. nX = nMenuX[nMenu];
  2171. nY += MICROPROFILE_TEXT_HEIGHT+1;
  2172. MicroProfileSubmenuCallback CB = GroupCallback[nMenu];
  2173. int nNumLines = 0;
  2174. bool bSelected = false;
  2175. const char* pString = CB(nNumLines, &bSelected);
  2176. uint32_t nWidth = 0, nHeight = 0;
  2177. while(pString)
  2178. {
  2179. nWidth = MicroProfileMax<int>(nWidth, (int)strlen(pString));
  2180. nNumLines++;
  2181. pString = CB(nNumLines, &bSelected);
  2182. }
  2183. nWidth = (2+nWidth) * (MICROPROFILE_TEXT_WIDTH+1);
  2184. nHeight = nNumLines * (MICROPROFILE_TEXT_HEIGHT+1);
  2185. if(UI.nMouseY <= nY + nHeight+0 && UI.nMouseY >= nY-0 && UI.nMouseX <= nX + nWidth + 0 && UI.nMouseX >= nX - 0)
  2186. {
  2187. UI.nActiveMenu = nMenu;
  2188. }
  2189. else if(nSelectMenu == UINT32_MAX)
  2190. {
  2191. UI.nActiveMenu = UINT32_MAX;
  2192. }
  2193. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + nHeight, 0xff000000|g_nMicroProfileBackColors[1]);
  2194. for(int i = 0; i < nNumLines; ++i)
  2195. {
  2196. bool bSelected = false;
  2197. const char* pString = CB(i, &bSelected);
  2198. if(UI.nMouseY >= nY && UI.nMouseY < nY + MICROPROFILE_TEXT_HEIGHT + 1)
  2199. {
  2200. if(UI.nMouseLeft || UI.nMouseRight)
  2201. {
  2202. CBClick[nMenu](i);
  2203. }
  2204. MicroProfileDrawBox(nX, nY, nX + nWidth, nY + MICROPROFILE_TEXT_HEIGHT + 1, 0xff888888);
  2205. }
  2206. int nLen = snprintf(buffer, SBUF_SIZE-1, "%c %s", bSelected ? '*' : ' ' ,pString);
  2207. MicroProfileDrawText(nX, nY, UINT32_MAX, buffer, nLen);
  2208. nY += MICROPROFILE_TEXT_HEIGHT+1;
  2209. }
  2210. }
  2211. {
  2212. static char FrameTimeMessage[64];
  2213. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  2214. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  2215. float fMs = fToMs * (S.nFlipTicks);
  2216. float fAverageMs = fToMs * (S.nFlipAggregateDisplay / nAggregateFrames);
  2217. float fMaxMs = fToMs * S.nFlipMaxDisplay;
  2218. int nLen = snprintf(FrameTimeMessage, sizeof(FrameTimeMessage)-1, "Time[%6.2f] Avg[%6.2f] Max[%6.2f]", fMs, fAverageMs, fMaxMs);
  2219. pMenuText[nNumMenuItems++] = &FrameTimeMessage[0];
  2220. MicroProfileDrawText(nWidth - nLen * (MICROPROFILE_TEXT_WIDTH+1), 0, UINT32_MAX, FrameTimeMessage, nLen);
  2221. }
  2222. }
  2223. inline void MicroProfileMoveGraph()
  2224. {
  2225. int nZoom = UI.nMouseWheelDelta;
  2226. int nPanX = 0;
  2227. int nPanY = 0;
  2228. static int X = 0, Y = 0;
  2229. if(UI.nMouseDownLeft && !UI.nModDown)
  2230. {
  2231. nPanX = UI.nMouseX - X;
  2232. nPanY = UI.nMouseY - Y;
  2233. }
  2234. X = UI.nMouseX;
  2235. Y = UI.nMouseY;
  2236. if(nZoom)
  2237. {
  2238. float fOldRange = UI.fDetailedRange;
  2239. if(nZoom>0)
  2240. {
  2241. UI.fDetailedRangeTarget = UI.fDetailedRange *= UI.nModDown ? 1.40f : 1.05f;
  2242. }
  2243. else
  2244. {
  2245. float fNewDetailedRange = UI.fDetailedRange / (UI.nModDown ? 1.40f : 1.05f);
  2246. if(fNewDetailedRange < 1e-4f) //100ns
  2247. fNewDetailedRange = 1e-4f;
  2248. UI.fDetailedRangeTarget = UI.fDetailedRange = fNewDetailedRange;
  2249. }
  2250. float fDiff = fOldRange - UI.fDetailedRange;
  2251. float fMousePrc = MicroProfileMax((float)UI.nMouseX / UI.nWidth ,0.f);
  2252. UI.fDetailedOffsetTarget = UI.fDetailedOffset += fDiff * fMousePrc;
  2253. }
  2254. if(nPanX)
  2255. {
  2256. UI.fDetailedOffsetTarget = UI.fDetailedOffset += -nPanX * UI.fDetailedRange / UI.nWidth;
  2257. }
  2258. UI.nOffsetY -= nPanY;
  2259. UI.nOffsetX += nPanX;
  2260. if(UI.nOffsetX > 0)
  2261. UI.nOffsetX = 0;
  2262. if(UI.nOffsetY<0)
  2263. UI.nOffsetY = 0;
  2264. }
  2265. inline void MicroProfileDrawCustom(uint32_t nWidth, uint32_t nHeight)
  2266. {
  2267. if(UINT32_MAX != UI.nCustomActive)
  2268. {
  2269. MicroProfile& S = *MicroProfileGet();
  2270. MP_ASSERT(UI.nCustomActive < MICROPROFILE_CUSTOM_MAX);
  2271. MicroProfileCustom* pCustom = &UI.Custom[UI.nCustomActive];
  2272. uint32_t nCount = pCustom->nNumTimers;
  2273. uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1;
  2274. uint32_t nExtraOffset = 1 + ((pCustom->nFlags & MICROPROFILE_CUSTOM_STACK) != 0 ? 3 : 0);
  2275. uint32_t nOffsetYBase = nHeight - (nExtraOffset+nCount)* (1+MICROPROFILE_TEXT_HEIGHT) - MICROPROFILE_CUSTOM_PADDING;
  2276. uint32_t nOffsetY = nOffsetYBase;
  2277. float fReference = pCustom->fReference;
  2278. float fRcpReference = 1.f / fReference;
  2279. uint32_t nReducedWidth = UI.nWidth - 2*MICROPROFILE_CUSTOM_PADDING - MICROPROFILE_GRAPH_WIDTH;
  2280. char Buffer[MICROPROFILE_NAME_MAX_LEN*2+1];
  2281. float* pTime = (float*)alloca(sizeof(float)*nCount);
  2282. float* pTimeAvg = (float*)alloca(sizeof(float)*nCount);
  2283. float* pTimeMax = (float*)alloca(sizeof(float)*nCount);
  2284. uint32_t* pColors = (uint32_t*)alloca(sizeof(uint32_t)*nCount);
  2285. uint32_t nMaxOffsetX = 0;
  2286. MicroProfileDrawBox(MICROPROFILE_CUSTOM_PADDING-1, nOffsetY-1, MICROPROFILE_CUSTOM_PADDING+nReducedWidth+1, UI.nHeight - MICROPROFILE_CUSTOM_PADDING+1, 0x88000000|g_nMicroProfileBackColors[0]);
  2287. for(uint32_t i = 0; i < nCount; ++i)
  2288. {
  2289. uint16_t nTimerIndex = MicroProfileGetTimerIndex(pCustom->pTimers[i]);
  2290. uint16_t nGroupIndex = MicroProfileGetGroupIndex(pCustom->pTimers[i]);
  2291. float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[nGroupIndex].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu());
  2292. pTime[i] = S.Frame[nTimerIndex].nTicks * fToMs;
  2293. pTimeAvg[i] = fToMs * (S.Aggregate[nTimerIndex].nTicks / nAggregateFrames);
  2294. pTimeMax[i] = fToMs * (S.AggregateMax[nTimerIndex]);
  2295. pColors[i] = S.TimerInfo[nTimerIndex].nColor;
  2296. }
  2297. MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING + 3*MICROPROFILE_TEXT_WIDTH, nOffsetY, UINT32_MAX, "Avg", sizeof("Avg")-1);
  2298. MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING + 13*MICROPROFILE_TEXT_WIDTH, nOffsetY, UINT32_MAX, "Max", sizeof("Max")-1);
  2299. for(uint32_t i = 0; i < nCount; ++i)
  2300. {
  2301. nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT);
  2302. uint16_t nTimerIndex = MicroProfileGetTimerIndex(pCustom->pTimers[i]);
  2303. uint16_t nGroupIndex = MicroProfileGetGroupIndex(pCustom->pTimers[i]);
  2304. MicroProfileTimerInfo* pTimerInfo = &S.TimerInfo[nTimerIndex];
  2305. int nSize;
  2306. uint32_t nOffsetX = MICROPROFILE_CUSTOM_PADDING;
  2307. nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2f", pTimeAvg[i]);
  2308. MicroProfileDrawText(nOffsetX, nOffsetY, UINT32_MAX, Buffer, nSize);
  2309. nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2310. nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2f", pTimeMax[i]);
  2311. MicroProfileDrawText(nOffsetX, nOffsetY, UINT32_MAX, Buffer, nSize);
  2312. nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2313. nSize = snprintf(Buffer, sizeof(Buffer)-1, "%s:%s", S.GroupInfo[nGroupIndex].pName, pTimerInfo->pName);
  2314. MicroProfileDrawText(nOffsetX, nOffsetY, pTimerInfo->nColor, Buffer, nSize);
  2315. nOffsetX += (nSize+2) * (MICROPROFILE_TEXT_WIDTH+1);
  2316. nMaxOffsetX = MicroProfileMax(nMaxOffsetX, nOffsetX);
  2317. }
  2318. uint32_t nMaxWidth = nReducedWidth- nMaxOffsetX;
  2319. if(pCustom->nFlags & MICROPROFILE_CUSTOM_BARS)
  2320. {
  2321. nOffsetY = nOffsetYBase;
  2322. float* pMs = pCustom->nFlags & MICROPROFILE_CUSTOM_BAR_SOURCE_MAX ? pTimeMax : pTimeAvg;
  2323. const char* pString = pCustom->nFlags & MICROPROFILE_CUSTOM_BAR_SOURCE_MAX ? "Max" : "Avg";
  2324. MicroProfileDrawText(nMaxOffsetX, nOffsetY, UINT32_MAX, pString, static_cast<uint32_t>(strlen(pString)));
  2325. int nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2fms", fReference);
  2326. MicroProfileDrawText(nReducedWidth - (1+nSize) * (MICROPROFILE_TEXT_WIDTH+1), nOffsetY, UINT32_MAX, Buffer, nSize);
  2327. for(uint32_t i = 0; i < nCount; ++i)
  2328. {
  2329. nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT);
  2330. uint32_t nWidth = MicroProfileMin(nMaxWidth, (uint32_t)(nMaxWidth * pMs[i] * fRcpReference));
  2331. MicroProfileDrawBox(nMaxOffsetX, nOffsetY, nMaxOffsetX+nWidth, nOffsetY+MICROPROFILE_TEXT_HEIGHT, pColors[i]|0xff000000);
  2332. }
  2333. }
  2334. if(pCustom->nFlags & MICROPROFILE_CUSTOM_STACK)
  2335. {
  2336. nOffsetY += 2*(1+MICROPROFILE_TEXT_HEIGHT);
  2337. const char* pString = pCustom->nFlags & MICROPROFILE_CUSTOM_STACK_SOURCE_MAX ? "Max" : "Avg";
  2338. MicroProfileDrawText(MICROPROFILE_CUSTOM_PADDING, nOffsetY, UINT32_MAX, pString, static_cast<uint32_t>(strlen(pString)));
  2339. int nSize = snprintf(Buffer, sizeof(Buffer)-1, "%6.2fms", fReference);
  2340. MicroProfileDrawText(nReducedWidth - (1+nSize) * (MICROPROFILE_TEXT_WIDTH+1), nOffsetY, UINT32_MAX, Buffer, nSize);
  2341. nOffsetY += (1+MICROPROFILE_TEXT_HEIGHT);
  2342. float fPosX = MICROPROFILE_CUSTOM_PADDING;
  2343. float* pMs = pCustom->nFlags & MICROPROFILE_CUSTOM_STACK_SOURCE_MAX ? pTimeMax : pTimeAvg;
  2344. for(uint32_t i = 0; i < nCount; ++i)
  2345. {
  2346. float fWidth = pMs[i] * fRcpReference * nReducedWidth;
  2347. uint32_t nX = fPosX;
  2348. fPosX += fWidth;
  2349. uint32_t nXEnd = fPosX;
  2350. if(nX < nXEnd)
  2351. {
  2352. MicroProfileDrawBox(nX, nOffsetY, nXEnd, nOffsetY+MICROPROFILE_TEXT_HEIGHT, pColors[i]|0xff000000);
  2353. }
  2354. }
  2355. }
  2356. }
  2357. }
  2358. inline void MicroProfileDraw(uint32_t nWidth, uint32_t nHeight)
  2359. {
  2360. MICROPROFILE_SCOPE(g_MicroProfileDraw);
  2361. MicroProfile& S = *MicroProfileGet();
  2362. {
  2363. static int once = 0;
  2364. if(0 == once)
  2365. {
  2366. std::recursive_mutex& m = MicroProfileGetMutex();
  2367. m.lock();
  2368. MicroProfileInitUI();
  2369. uint32_t nDisplay = S.nDisplay;
  2370. MicroProfileLoadPreset(MICROPROFILE_DEFAULT_PRESET);
  2371. once++;
  2372. S.nDisplay = nDisplay;// dont load display, just state
  2373. m.unlock();
  2374. }
  2375. }
  2376. if(S.nDisplay)
  2377. {
  2378. std::recursive_mutex& m = MicroProfileGetMutex();
  2379. m.lock();
  2380. UI.nWidth = nWidth;
  2381. UI.nHeight = nHeight;
  2382. UI.nHoverToken = MICROPROFILE_INVALID_TOKEN;
  2383. UI.nHoverTime = 0;
  2384. UI.nHoverFrame = -1;
  2385. if(S.nDisplay != MP_DRAW_DETAILED)
  2386. S.nContextSwitchHoverThread = S.nContextSwitchHoverThreadAfter = S.nContextSwitchHoverThreadBefore = UINT32_MAX;
  2387. MicroProfileMoveGraph();
  2388. if(S.nDisplay == MP_DRAW_DETAILED)
  2389. {
  2390. MicroProfileDrawDetailedView(nWidth, nHeight);
  2391. }
  2392. else if(S.nDisplay == MP_DRAW_BARS && S.nBars)
  2393. {
  2394. MicroProfileDrawBarView(nWidth, nHeight);
  2395. }
  2396. MicroProfileDrawMenu(nWidth, nHeight);
  2397. bool bMouseOverGraph = MicroProfileDrawGraph(nWidth, nHeight);
  2398. MicroProfileDrawCustom(nWidth, nHeight);
  2399. bool bHidden = S.nDisplay == MP_DRAW_HIDDEN;
  2400. if(!bHidden)
  2401. {
  2402. uint32_t nLockedToolTipX = 3;
  2403. bool bDeleted = false;
  2404. for(int i = 0; i < MICROPROFILE_TOOLTIP_MAX_LOCKED; ++i)
  2405. {
  2406. int nIndex = (g_MicroProfileUI.LockedToolTipFront + i) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  2407. if(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0])
  2408. {
  2409. uint32_t nToolTipWidth = 0, nToolTipHeight = 0;
  2410. MicroProfileFloatWindowSize(g_MicroProfileUI.LockedToolTips[nIndex].ppStrings, g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, 0, nToolTipWidth, nToolTipHeight, 0);
  2411. uint32_t nStartY = nHeight - nToolTipHeight - 2;
  2412. if(!bDeleted && UI.nMouseY > nStartY && UI.nMouseX > nLockedToolTipX && UI.nMouseX <= nLockedToolTipX + nToolTipWidth && (UI.nMouseLeft || UI.nMouseRight) )
  2413. {
  2414. bDeleted = true;
  2415. int j = i;
  2416. for(; j < MICROPROFILE_TOOLTIP_MAX_LOCKED-1; ++j)
  2417. {
  2418. int nIndex0 = (g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  2419. int nIndex1 = (g_MicroProfileUI.LockedToolTipFront + j+1) % MICROPROFILE_TOOLTIP_MAX_LOCKED;
  2420. MicroProfileStringArrayCopy(&g_MicroProfileUI.LockedToolTips[nIndex0], &g_MicroProfileUI.LockedToolTips[nIndex1]);
  2421. }
  2422. MicroProfileStringArrayClear(&g_MicroProfileUI.LockedToolTips[(g_MicroProfileUI.LockedToolTipFront + j) % MICROPROFILE_TOOLTIP_MAX_LOCKED]);
  2423. }
  2424. else
  2425. {
  2426. MicroProfileDrawFloatWindow(nLockedToolTipX, nHeight-nToolTipHeight-2, &g_MicroProfileUI.LockedToolTips[nIndex].ppStrings[0], g_MicroProfileUI.LockedToolTips[nIndex].nNumStrings, g_MicroProfileUI.nLockedToolTipColor[nIndex]);
  2427. nLockedToolTipX += nToolTipWidth + 4;
  2428. }
  2429. }
  2430. }
  2431. if(UI.nActiveMenu == 8)
  2432. {
  2433. if(S.nDisplay & MP_DRAW_DETAILED)
  2434. {
  2435. MicroProfileStringArray DetailedHelp;
  2436. MicroProfileStringArrayClear(&DetailedHelp);
  2437. MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_LEFT);
  2438. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Toggle Graph");
  2439. MicroProfileStringArrayFormat(&DetailedHelp, "%s", MICROPROFILE_HELP_ALT);
  2440. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom");
  2441. MicroProfileStringArrayFormat(&DetailedHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT);
  2442. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Lock Tooltip");
  2443. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Drag");
  2444. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Pan View");
  2445. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Mouse Wheel");
  2446. MicroProfileStringArrayAddLiteral(&DetailedHelp, "Zoom");
  2447. MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, DetailedHelp.ppStrings, DetailedHelp.nNumStrings, 0xff777777);
  2448. MicroProfileStringArray DetailedHistoryHelp;
  2449. MicroProfileStringArrayClear(&DetailedHistoryHelp);
  2450. MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_LEFT);
  2451. MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Center View");
  2452. MicroProfileStringArrayFormat(&DetailedHistoryHelp, "%s", MICROPROFILE_HELP_ALT);
  2453. MicroProfileStringArrayAddLiteral(&DetailedHistoryHelp, "Zoom to frame");
  2454. MicroProfileDrawFloatWindow(nWidth, 20, DetailedHistoryHelp.ppStrings, DetailedHistoryHelp.nNumStrings, 0xff777777);
  2455. }
  2456. else if(0 != (S.nDisplay & MP_DRAW_BARS) && S.nBars)
  2457. {
  2458. MicroProfileStringArray BarHelp;
  2459. MicroProfileStringArrayClear(&BarHelp);
  2460. MicroProfileStringArrayFormat(&BarHelp, "%s", MICROPROFILE_HELP_LEFT);
  2461. MicroProfileStringArrayAddLiteral(&BarHelp, "Toggle Graph");
  2462. MicroProfileStringArrayFormat(&BarHelp, "%s + %s", MICROPROFILE_HELP_MOD, MICROPROFILE_HELP_LEFT);
  2463. MicroProfileStringArrayAddLiteral(&BarHelp, "Lock Tooltip");
  2464. MicroProfileStringArrayAddLiteral(&BarHelp, "Drag");
  2465. MicroProfileStringArrayAddLiteral(&BarHelp, "Pan View");
  2466. MicroProfileDrawFloatWindow(nWidth, MICROPROFILE_FRAME_HISTORY_HEIGHT+20, BarHelp.ppStrings, BarHelp.nNumStrings, 0xff777777);
  2467. }
  2468. MicroProfileStringArray Debug;
  2469. MicroProfileStringArrayClear(&Debug);
  2470. MicroProfileStringArrayAddLiteral(&Debug, "Memory Usage");
  2471. MicroProfileStringArrayFormat(&Debug, "%4.2fmb", S.nMemUsage / (1024.f * 1024.f));
  2472. MicroProfileStringArrayAddLiteral(&Debug, "Web Server Port");
  2473. MicroProfileStringArrayFormat(&Debug, "%d", MicroProfileWebServerPort());
  2474. uint32_t nFrameNext = (S.nFrameCurrent+1) % MICROPROFILE_MAX_FRAME_HISTORY;
  2475. MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent];
  2476. MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext];
  2477. MicroProfileStringArrayAddLiteral(&Debug, "");
  2478. MicroProfileStringArrayAddLiteral(&Debug, "");
  2479. MicroProfileStringArrayAddLiteral(&Debug, "Usage");
  2480. MicroProfileStringArrayAddLiteral(&Debug, "markers [frames] ");
  2481. #if MICROPROFILE_CONTEXT_SWITCH_TRACE
  2482. MicroProfileStringArrayAddLiteral(&Debug, "Context Switch");
  2483. MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", S.nContextSwitchUsage, MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE / S.nContextSwitchUsage );
  2484. #endif
  2485. for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2486. {
  2487. if(pFrameCurrent->nLogStart[i] && S.Pool[i])
  2488. {
  2489. uint32_t nEnd = pFrameNext->nLogStart[i];
  2490. uint32_t nStart = pFrameCurrent->nLogStart[i];
  2491. uint32_t nUsage = nStart < nEnd ? (nEnd - nStart) : (nEnd + MICROPROFILE_BUFFER_SIZE - nStart);
  2492. uint32_t nFrameSupport = MICROPROFILE_BUFFER_SIZE / nUsage;
  2493. MicroProfileStringArrayFormat(&Debug, "%s", &S.Pool[i]->ThreadName[0]);
  2494. MicroProfileStringArrayFormat(&Debug, "%9d [%7d]", nUsage, nFrameSupport);
  2495. }
  2496. }
  2497. MicroProfileDrawFloatWindow(0, nHeight-10, Debug.ppStrings, Debug.nNumStrings, 0xff777777);
  2498. }
  2499. if(UI.nActiveMenu == UINT32_MAX && !bMouseOverGraph)
  2500. {
  2501. if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN)
  2502. {
  2503. MicroProfileDrawFloatTooltip(UI.nMouseX, UI.nMouseY, UI.nHoverToken, UI.nHoverTime);
  2504. }
  2505. else if(S.nContextSwitchHoverThreadAfter != UINT32_MAX && S.nContextSwitchHoverThreadBefore != UINT32_MAX)
  2506. {
  2507. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  2508. MicroProfileStringArray ToolTip;
  2509. MicroProfileStringArrayClear(&ToolTip);
  2510. MicroProfileStringArrayAddLiteral(&ToolTip, "Context Switch");
  2511. MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThread);
  2512. MicroProfileStringArrayAddLiteral(&ToolTip, "Before");
  2513. MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadBefore);
  2514. MicroProfileStringArrayAddLiteral(&ToolTip, "After");
  2515. MicroProfileStringArrayFormat(&ToolTip, "%04x", S.nContextSwitchHoverThreadAfter);
  2516. MicroProfileStringArrayAddLiteral(&ToolTip, "Duration");
  2517. int64_t nDifference = MicroProfileLogTickDifference(S.nContextSwitchHoverTickIn, S.nContextSwitchHoverTickOut);
  2518. MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fToMs * nDifference );
  2519. MicroProfileStringArrayAddLiteral(&ToolTip, "CPU");
  2520. MicroProfileStringArrayFormat(&ToolTip, "%d", S.nContextSwitchHoverCpu);
  2521. MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, UINT32_MAX);
  2522. }
  2523. else if(UI.nHoverFrame != -1)
  2524. {
  2525. uint32_t nNextFrame = (UI.nHoverFrame+1)%MICROPROFILE_MAX_FRAME_HISTORY;
  2526. int64_t nTick = S.Frames[UI.nHoverFrame].nFrameStartCpu;
  2527. int64_t nTickNext = S.Frames[nNextFrame].nFrameStartCpu;
  2528. int64_t nTickGpu = S.Frames[UI.nHoverFrame].nFrameStartGpu;
  2529. int64_t nTickNextGpu = S.Frames[nNextFrame].nFrameStartGpu;
  2530. float fToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu());
  2531. float fToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu());
  2532. float fMs = fToMs * (nTickNext - nTick);
  2533. float fMsGpu = fToMsGpu * (nTickNextGpu - nTickGpu);
  2534. MicroProfileStringArray ToolTip;
  2535. MicroProfileStringArrayClear(&ToolTip);
  2536. MicroProfileStringArrayFormat(&ToolTip, "Frame %d", UI.nHoverFrame);
  2537. #if MICROPROFILE_DEBUG
  2538. MicroProfileStringArrayFormat(&ToolTip, "%p", &S.Frames[UI.nHoverFrame]);
  2539. #else
  2540. MicroProfileStringArrayAddLiteral(&ToolTip, "");
  2541. #endif
  2542. MicroProfileStringArrayAddLiteral(&ToolTip, "CPU Time");
  2543. MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMs);
  2544. MicroProfileStringArrayAddLiteral(&ToolTip, "GPU Time");
  2545. MicroProfileStringArrayFormat(&ToolTip, "%6.2fms", fMsGpu);
  2546. #if MICROPROFILE_DEBUG
  2547. for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2548. {
  2549. if(S.Frames[UI.nHoverFrame].nLogStart[i])
  2550. {
  2551. MicroProfileStringArrayFormat(&ToolTip, "%d", i);
  2552. MicroProfileStringArrayFormat(&ToolTip, "%d", S.Frames[UI.nHoverFrame].nLogStart[i]);
  2553. }
  2554. }
  2555. #endif
  2556. MicroProfileDrawFloatWindow(UI.nMouseX, UI.nMouseY+20, &ToolTip.ppStrings[0], ToolTip.nNumStrings, UINT32_MAX);
  2557. }
  2558. if(UI.nMouseLeft)
  2559. {
  2560. if(UI.nHoverToken != MICROPROFILE_INVALID_TOKEN)
  2561. MicroProfileToggleGraph(UI.nHoverToken);
  2562. }
  2563. }
  2564. }
  2565. #if MICROPROFILE_DRAWCURSOR
  2566. {
  2567. float fCursor[8] =
  2568. {
  2569. MicroProfileMax(0, (int)UI.nMouseX-3), UI.nMouseY,
  2570. MicroProfileMin(nWidth, UI.nMouseX+3), UI.nMouseY,
  2571. UI.nMouseX, MicroProfileMax((int)UI.nMouseY-3, 0),
  2572. UI.nMouseX, MicroProfileMin(nHeight, UI.nMouseY+3),
  2573. };
  2574. MicroProfileDrawLine2D(2, &fCursor[0], 0xff00ff00);
  2575. MicroProfileDrawLine2D(2, &fCursor[4], 0xff00ff00);
  2576. }
  2577. #endif
  2578. m.unlock();
  2579. }
  2580. else if(UI.nCustomActive != UINT32_MAX)
  2581. {
  2582. std::recursive_mutex& m = MicroProfileGetMutex();
  2583. m.lock();
  2584. MicroProfileDrawGraph(nWidth, nHeight);
  2585. MicroProfileDrawCustom(nWidth, nHeight);
  2586. m.unlock();
  2587. }
  2588. UI.nMouseLeft = UI.nMouseRight = 0;
  2589. UI.nMouseLeftMod = UI.nMouseRightMod = 0;
  2590. UI.nMouseWheelDelta = 0;
  2591. if(S.nOverflow)
  2592. S.nOverflow--;
  2593. UI.fDetailedOffset = UI.fDetailedOffset + (UI.fDetailedOffsetTarget - UI.fDetailedOffset) * MICROPROFILE_ANIM_DELAY_PRC;
  2594. UI.fDetailedRange = UI.fDetailedRange + (UI.fDetailedRangeTarget - UI.fDetailedRange) * MICROPROFILE_ANIM_DELAY_PRC;
  2595. }
  2596. bool MicroProfileIsDrawing()
  2597. {
  2598. MicroProfile& S = *MicroProfileGet();
  2599. return S.nDisplay != 0;
  2600. }
  2601. void MicroProfileToggleGraph(MicroProfileToken nToken)
  2602. {
  2603. MicroProfile& S = *MicroProfileGet();
  2604. uint32_t nTimerId = MicroProfileGetTimerIndex(nToken);
  2605. nToken &= 0xffff;
  2606. int32_t nMinSort = 0x7fffffff;
  2607. int32_t nFreeIndex = -1;
  2608. int32_t nMinIndex = 0;
  2609. int32_t nMaxSort = 0x80000000;
  2610. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2611. {
  2612. if(S.Graph[i].nToken == MICROPROFILE_INVALID_TOKEN)
  2613. nFreeIndex = i;
  2614. if(S.Graph[i].nToken == nToken)
  2615. {
  2616. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2617. S.TimerInfo[nTimerId].bGraph = false;
  2618. return;
  2619. }
  2620. if(S.Graph[i].nKey < nMinSort)
  2621. {
  2622. nMinSort = S.Graph[i].nKey;
  2623. nMinIndex = i;
  2624. }
  2625. if(S.Graph[i].nKey > nMaxSort)
  2626. {
  2627. nMaxSort = S.Graph[i].nKey;
  2628. }
  2629. }
  2630. int nIndex = nFreeIndex > -1 ? nFreeIndex : nMinIndex;
  2631. if (nFreeIndex == -1)
  2632. {
  2633. uint32_t idx = MicroProfileGetTimerIndex(S.Graph[nIndex].nToken);
  2634. S.TimerInfo[idx].bGraph = false;
  2635. }
  2636. S.Graph[nIndex].nToken = nToken;
  2637. S.Graph[nIndex].nKey = nMaxSort+1;
  2638. memset(&S.Graph[nIndex].nHistory[0], 0, sizeof(S.Graph[nIndex].nHistory));
  2639. S.TimerInfo[nTimerId].bGraph = true;
  2640. }
  2641. void MicroProfileMousePosition(uint32_t nX, uint32_t nY, int nWheelDelta)
  2642. {
  2643. UI.nMouseX = nX;
  2644. UI.nMouseY = nY;
  2645. UI.nMouseWheelDelta = nWheelDelta;
  2646. }
  2647. void MicroProfileModKey(uint32_t nKeyState)
  2648. {
  2649. UI.nModDown = nKeyState ? 1 : 0;
  2650. }
  2651. void MicroProfileClearGraph()
  2652. {
  2653. MicroProfile& S = *MicroProfileGet();
  2654. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2655. {
  2656. if(S.Graph[i].nToken != 0)
  2657. {
  2658. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2659. }
  2660. }
  2661. }
  2662. void MicroProfileMouseButton(uint32_t nLeft, uint32_t nRight)
  2663. {
  2664. bool bCanRelease = abs((int)(UI.nMouseDownX - UI.nMouseX)) + abs((int)(UI.nMouseDownY - UI.nMouseY)) < 3;
  2665. if(0 == nLeft && UI.nMouseDownLeft && bCanRelease)
  2666. {
  2667. if(UI.nModDown)
  2668. UI.nMouseLeftMod = 1;
  2669. else
  2670. UI.nMouseLeft = 1;
  2671. }
  2672. if(0 == nRight && UI.nMouseDownRight && bCanRelease)
  2673. {
  2674. if(UI.nModDown)
  2675. UI.nMouseRightMod = 1;
  2676. else
  2677. UI.nMouseRight = 1;
  2678. }
  2679. if((nLeft || nRight) && !(UI.nMouseDownLeft || UI.nMouseDownRight))
  2680. {
  2681. UI.nMouseDownX = UI.nMouseX;
  2682. UI.nMouseDownY = UI.nMouseY;
  2683. }
  2684. UI.nMouseDownLeft = nLeft;
  2685. UI.nMouseDownRight = nRight;
  2686. }
  2687. void MicroProfileDrawLineVertical(int nX, int nTop, int nBottom, uint32_t nColor)
  2688. {
  2689. MicroProfileDrawBox(nX, nTop, nX + 1, nBottom, nColor);
  2690. }
  2691. void MicroProfileDrawLineHorizontal(int nLeft, int nRight, int nY, uint32_t nColor)
  2692. {
  2693. MicroProfileDrawBox(nLeft, nY, nRight, nY + 1, nColor);
  2694. }
  2695. #include <stdio.h>
  2696. #define MICROPROFILE_PRESET_HEADER_MAGIC 0x28586813
  2697. #define MICROPROFILE_PRESET_HEADER_VERSION 0x00000102
  2698. struct MicroProfilePresetHeader
  2699. {
  2700. uint32_t nMagic;
  2701. uint32_t nVersion;
  2702. //groups, threads, aggregate, reference frame, graphs timers
  2703. uint32_t nGroups[MICROPROFILE_MAX_GROUPS];
  2704. uint32_t nThreads[MICROPROFILE_MAX_THREADS];
  2705. uint32_t nGraphName[MICROPROFILE_MAX_GRAPHS];
  2706. uint32_t nGraphGroupName[MICROPROFILE_MAX_GRAPHS];
  2707. uint32_t nAllGroupsWanted;
  2708. uint32_t nAllThreadsWanted;
  2709. uint32_t nAggregateFlip;
  2710. float fReferenceTime;
  2711. uint32_t nBars;
  2712. uint32_t nDisplay;
  2713. uint32_t nOpacityBackground;
  2714. uint32_t nOpacityForeground;
  2715. uint32_t nShowSpikes;
  2716. };
  2717. #ifndef MICROPROFILE_PRESET_FILENAME_FUNC
  2718. #define MICROPROFILE_PRESET_FILENAME_FUNC MicroProfilePresetFilename
  2719. static const char* MicroProfilePresetFilename(const char* pSuffix)
  2720. {
  2721. static char filename[512];
  2722. snprintf(filename, sizeof(filename)-1, ".microprofilepreset.%s", pSuffix);
  2723. return filename;
  2724. }
  2725. #endif
  2726. void MicroProfileSavePreset(const char* pPresetName)
  2727. {
  2728. std::lock_guard<std::recursive_mutex> Lock(MicroProfileGetMutex());
  2729. FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pPresetName), "wb");
  2730. if(!F) return;
  2731. MicroProfile& S = *MicroProfileGet();
  2732. MicroProfilePresetHeader Header;
  2733. memset(&Header, 0, sizeof(Header));
  2734. Header.nAggregateFlip = S.nAggregateFlip;
  2735. Header.nBars = S.nBars;
  2736. Header.fReferenceTime = S.fReferenceTime;
  2737. Header.nAllGroupsWanted = S.nAllGroupsWanted;
  2738. Header.nAllThreadsWanted = S.nAllThreadsWanted;
  2739. Header.nMagic = MICROPROFILE_PRESET_HEADER_MAGIC;
  2740. Header.nVersion = MICROPROFILE_PRESET_HEADER_VERSION;
  2741. Header.nDisplay = S.nDisplay;
  2742. Header.nOpacityBackground = UI.nOpacityBackground;
  2743. Header.nOpacityForeground = UI.nOpacityForeground;
  2744. Header.nShowSpikes = UI.bShowSpikes ? 1 : 0;
  2745. fwrite(&Header, sizeof(Header), 1, F);
  2746. uint64_t nMask = 1;
  2747. for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i)
  2748. {
  2749. if(S.nActiveGroupWanted & nMask)
  2750. {
  2751. uint32_t offset = ftell(F);
  2752. const char* pName = S.GroupInfo[i].pName;
  2753. int nLen = (int)strlen(pName)+1;
  2754. fwrite(pName, nLen, 1, F);
  2755. Header.nGroups[i] = offset;
  2756. }
  2757. nMask <<= 1;
  2758. }
  2759. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2760. {
  2761. MicroProfileThreadLog* pLog = S.Pool[i];
  2762. if(pLog && S.nThreadActive[i])
  2763. {
  2764. uint32_t nOffset = ftell(F);
  2765. const char* pName = &pLog->ThreadName[0];
  2766. int nLen = (int)strlen(pName)+1;
  2767. fwrite(pName, nLen, 1, F);
  2768. Header.nThreads[i] = nOffset;
  2769. }
  2770. }
  2771. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2772. {
  2773. MicroProfileToken nToken = S.Graph[i].nToken;
  2774. if(nToken != MICROPROFILE_INVALID_TOKEN)
  2775. {
  2776. uint32_t nGroupIndex = MicroProfileGetGroupIndex(nToken);
  2777. uint32_t nTimerIndex = MicroProfileGetTimerIndex(nToken);
  2778. const char* pGroupName = S.GroupInfo[nGroupIndex].pName;
  2779. const char* pTimerName = S.TimerInfo[nTimerIndex].pName;
  2780. MP_ASSERT(pGroupName);
  2781. MP_ASSERT(pTimerName);
  2782. int nGroupLen = (int)strlen(pGroupName)+1;
  2783. int nTimerLen = (int)strlen(pTimerName)+1;
  2784. uint32_t nOffsetGroup = ftell(F);
  2785. fwrite(pGroupName, nGroupLen, 1, F);
  2786. uint32_t nOffsetTimer = ftell(F);
  2787. fwrite(pTimerName, nTimerLen, 1, F);
  2788. Header.nGraphName[i] = nOffsetTimer;
  2789. Header.nGraphGroupName[i] = nOffsetGroup;
  2790. }
  2791. }
  2792. fseek(F, 0, SEEK_SET);
  2793. fwrite(&Header, sizeof(Header), 1, F);
  2794. fclose(F);
  2795. }
  2796. void MicroProfileLoadPreset(const char* pSuffix)
  2797. {
  2798. std::lock_guard<std::recursive_mutex> Lock(MicroProfileGetMutex());
  2799. FILE* F = fopen(MICROPROFILE_PRESET_FILENAME_FUNC(pSuffix), "rb");
  2800. if(!F)
  2801. {
  2802. return;
  2803. }
  2804. fseek(F, 0, SEEK_END);
  2805. int nSize = ftell(F);
  2806. char* const pBuffer = (char*)alloca(nSize);
  2807. fseek(F, 0, SEEK_SET);
  2808. int nRead = (int)fread(pBuffer, nSize, 1, F);
  2809. fclose(F);
  2810. if(1 != nRead)
  2811. return;
  2812. MicroProfile& S = *MicroProfileGet();
  2813. MicroProfilePresetHeader& Header = *(MicroProfilePresetHeader*)pBuffer;
  2814. if(Header.nMagic != MICROPROFILE_PRESET_HEADER_MAGIC || Header.nVersion != MICROPROFILE_PRESET_HEADER_VERSION)
  2815. {
  2816. return;
  2817. }
  2818. S.nAggregateFlip = Header.nAggregateFlip;
  2819. S.nBars = Header.nBars;
  2820. S.fReferenceTime = Header.fReferenceTime;
  2821. S.fRcpReferenceTime = 1.f / Header.fReferenceTime;
  2822. S.nAllGroupsWanted = Header.nAllGroupsWanted;
  2823. S.nAllThreadsWanted = Header.nAllThreadsWanted;
  2824. S.nDisplay = Header.nDisplay;
  2825. S.nActiveGroupWanted = 0;
  2826. UI.nOpacityBackground = Header.nOpacityBackground;
  2827. UI.nOpacityForeground = Header.nOpacityForeground;
  2828. UI.bShowSpikes = Header.nShowSpikes == 1;
  2829. memset(&S.nThreadActive[0], 0, sizeof(S.nThreadActive));
  2830. for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i)
  2831. {
  2832. if(Header.nGroups[i])
  2833. {
  2834. const char* pGroupName = pBuffer + Header.nGroups[i];
  2835. for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
  2836. {
  2837. if(0 == MP_STRCASECMP(pGroupName, S.GroupInfo[j].pName))
  2838. {
  2839. S.nActiveGroupWanted |= (1ULL << j);
  2840. }
  2841. }
  2842. }
  2843. }
  2844. for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i)
  2845. {
  2846. if(Header.nThreads[i])
  2847. {
  2848. const char* pThreadName = pBuffer + Header.nThreads[i];
  2849. for(uint32_t j = 0; j < MICROPROFILE_MAX_THREADS; ++j)
  2850. {
  2851. MicroProfileThreadLog* pLog = S.Pool[j];
  2852. if(pLog && 0 == MP_STRCASECMP(pThreadName, &pLog->ThreadName[0]))
  2853. {
  2854. S.nThreadActive[j] = 1;
  2855. }
  2856. }
  2857. }
  2858. }
  2859. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2860. {
  2861. MicroProfileToken nPrevToken = S.Graph[i].nToken;
  2862. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2863. if(Header.nGraphName[i] && Header.nGraphGroupName[i])
  2864. {
  2865. const char* pGraphName = pBuffer + Header.nGraphName[i];
  2866. const char* pGraphGroupName = pBuffer + Header.nGraphGroupName[i];
  2867. for(uint32_t j = 0; j < S.nTotalTimers; ++j)
  2868. {
  2869. uint64_t nGroupIndex = S.TimerInfo[j].nGroupIndex;
  2870. if(0 == MP_STRCASECMP(pGraphName, S.TimerInfo[j].pName) && 0 == MP_STRCASECMP(pGraphGroupName, S.GroupInfo[nGroupIndex].pName))
  2871. {
  2872. MicroProfileToken nToken = MicroProfileMakeToken(1ULL << nGroupIndex, (uint16_t)j);
  2873. S.Graph[i].nToken = nToken; // note: group index is stored here but is checked without in MicroProfileToggleGraph()!
  2874. S.TimerInfo[j].bGraph = true;
  2875. if(nToken != nPrevToken)
  2876. {
  2877. memset(&S.Graph[i].nHistory, 0, sizeof(S.Graph[i].nHistory));
  2878. }
  2879. break;
  2880. }
  2881. }
  2882. }
  2883. }
  2884. }
  2885. inline uint32_t MicroProfileCustomGroupFind(const char* pCustomName)
  2886. {
  2887. for(uint32_t i = 0; i < UI.nCustomCount; ++i)
  2888. {
  2889. if(!MP_STRCASECMP(pCustomName, UI.Custom[i].pName))
  2890. {
  2891. return i;
  2892. }
  2893. }
  2894. return UINT32_MAX;
  2895. }
  2896. inline uint32_t MicroProfileCustomGroup(const char* pCustomName)
  2897. {
  2898. for(uint32_t i = 0; i < UI.nCustomCount; ++i)
  2899. {
  2900. if(!MP_STRCASECMP(pCustomName, UI.Custom[i].pName))
  2901. {
  2902. return i;
  2903. }
  2904. }
  2905. MP_ASSERT(UI.nCustomCount < MICROPROFILE_CUSTOM_MAX);
  2906. uint32_t nIndex = UI.nCustomCount;
  2907. UI.nCustomCount++;
  2908. memset(&UI.Custom[nIndex], 0, sizeof(UI.Custom[nIndex]));
  2909. size_t nLen = strlen(pCustomName);
  2910. if(nLen > MICROPROFILE_NAME_MAX_LEN-1)
  2911. nLen = MICROPROFILE_NAME_MAX_LEN-1;
  2912. memcpy(&UI.Custom[nIndex].pName[0], pCustomName, nLen);
  2913. UI.Custom[nIndex].pName[nLen] = '\0';
  2914. return nIndex;
  2915. }
  2916. void MicroProfileCustomGroup(const char* pCustomName, uint32_t nMaxTimers, uint32_t nAggregateFlip, float fReferenceTime, uint32_t nFlags)
  2917. {
  2918. uint32_t nIndex = MicroProfileCustomGroup(pCustomName);
  2919. MP_ASSERT(UI.Custom[nIndex].pTimers == 0);//only call once!
  2920. UI.Custom[nIndex].pTimers = &UI.CustomTimer[UI.nCustomTimerCount];
  2921. UI.Custom[nIndex].nMaxTimers = nMaxTimers;
  2922. UI.Custom[nIndex].fReference = fReferenceTime;
  2923. UI.nCustomTimerCount += nMaxTimers;
  2924. MP_ASSERT(UI.nCustomTimerCount <= MICROPROFILE_CUSTOM_MAX_TIMERS); //bump MICROPROFILE_CUSTOM_MAX_TIMERS
  2925. UI.Custom[nIndex].nFlags = nFlags;
  2926. UI.Custom[nIndex].nAggregateFlip = nAggregateFlip;
  2927. }
  2928. inline void MicroProfileCustomGroupEnable(uint32_t nIndex)
  2929. {
  2930. if(nIndex < UI.nCustomCount)
  2931. {
  2932. MicroProfile& S = *MicroProfileGet();
  2933. S.nForceGroupUI = UI.Custom[nIndex].nGroupMask;
  2934. MicroProfileSetAggregateFrames(UI.Custom[nIndex].nAggregateFlip);
  2935. S.fReferenceTime = UI.Custom[nIndex].fReference;
  2936. S.fRcpReferenceTime = 1.f / UI.Custom[nIndex].fReference;
  2937. UI.nCustomActive = nIndex;
  2938. for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i)
  2939. {
  2940. if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN)
  2941. {
  2942. uint32_t nTimerId = MicroProfileGetTimerIndex(S.Graph[i].nToken);
  2943. S.TimerInfo[nTimerId].bGraph = false;
  2944. S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN;
  2945. }
  2946. }
  2947. for(uint32_t i = 0; i < UI.Custom[nIndex].nNumTimers; ++i)
  2948. {
  2949. if(i == MICROPROFILE_MAX_GRAPHS)
  2950. {
  2951. break;
  2952. }
  2953. S.Graph[i].nToken = UI.Custom[nIndex].pTimers[i];
  2954. S.Graph[i].nKey = i;
  2955. uint32_t nTimerId = MicroProfileGetTimerIndex(S.Graph[i].nToken);
  2956. S.TimerInfo[nTimerId].bGraph = true;
  2957. }
  2958. }
  2959. }
  2960. void MicroProfileCustomGroupToggle(const char* pCustomName)
  2961. {
  2962. uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName);
  2963. if(nIndex == UINT32_MAX || nIndex == UI.nCustomActive)
  2964. {
  2965. MicroProfileCustomGroupDisable();
  2966. }
  2967. else
  2968. {
  2969. MicroProfileCustomGroupEnable(nIndex);
  2970. }
  2971. }
  2972. void MicroProfileCustomGroupEnable(const char* pCustomName)
  2973. {
  2974. uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName);
  2975. MicroProfileCustomGroupEnable(nIndex);
  2976. }
  2977. void MicroProfileCustomGroupDisable()
  2978. {
  2979. MicroProfile& S = *MicroProfileGet();
  2980. S.nForceGroupUI = 0;
  2981. UI.nCustomActive = UINT32_MAX;
  2982. }
  2983. void MicroProfileCustomGroupAddTimer(const char* pCustomName, const char* pGroup, const char* pTimer)
  2984. {
  2985. uint32_t nIndex = MicroProfileCustomGroupFind(pCustomName);
  2986. if(UINT32_MAX == nIndex)
  2987. {
  2988. return;
  2989. }
  2990. uint32_t nTimerIndex = UI.Custom[nIndex].nNumTimers;
  2991. MP_ASSERT(nTimerIndex < UI.Custom[nIndex].nMaxTimers);
  2992. uint64_t nToken = MicroProfileFindToken(pGroup, pTimer);
  2993. MP_ASSERT(nToken != MICROPROFILE_INVALID_TOKEN); //Timer must be registered first.
  2994. UI.Custom[nIndex].pTimers[nTimerIndex] = nToken;
  2995. uint16_t nGroup = MicroProfileGetGroupIndex(nToken);
  2996. UI.Custom[nIndex].nGroupMask |= (1ULL << nGroup);
  2997. UI.Custom[nIndex].nNumTimers++;
  2998. }
  2999. #undef UI
  3000. #endif
  3001. #endif