microprofileui.h 133 KB

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