texturev.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bx/commandline.h>
  8. #include <bx/easing.h>
  9. #include <bx/file.h>
  10. #include <bx/filepath.h>
  11. #include <bx/math.h>
  12. #include <bx/os.h>
  13. #include <bx/process.h>
  14. #include <bx/settings.h>
  15. #include <bx/uint32_t.h>
  16. #include <entry/entry.h>
  17. #include <entry/input.h>
  18. #include <entry/cmd.h>
  19. #include <imgui/imgui.h>
  20. #include <bgfx_utils.h>
  21. #include <tinystl/allocator.h>
  22. #include <tinystl/vector.h>
  23. namespace stl = tinystl;
  24. #include <string>
  25. #include <algorithm>
  26. #include <bimg/decode.h>
  27. #include <bgfx/embedded_shader.h>
  28. #include "vs_texture.bin.h"
  29. #include "vs_texture_cube.bin.h"
  30. #include "fs_texture.bin.h"
  31. #include "fs_texture_array.bin.h"
  32. #include "fs_texture_cube.bin.h"
  33. #include "fs_texture_cube2.bin.h"
  34. #include "fs_texture_sdf.bin.h"
  35. #include "fs_texture_msdf.bin.h"
  36. #include "fs_texture_3d.bin.h"
  37. #define BACKGROUND_VIEW_ID 0
  38. #define IMAGE_VIEW_ID 1
  39. #define BGFX_TEXTUREV_VERSION_MAJOR 1
  40. #define BGFX_TEXTUREV_VERSION_MINOR 1
  41. const float kEvMin = -10.0f;
  42. const float kEvMax = 20.0f;
  43. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  44. {
  45. BGFX_EMBEDDED_SHADER(vs_texture),
  46. BGFX_EMBEDDED_SHADER(fs_texture),
  47. BGFX_EMBEDDED_SHADER(fs_texture_array),
  48. BGFX_EMBEDDED_SHADER(vs_texture_cube),
  49. BGFX_EMBEDDED_SHADER(fs_texture_cube),
  50. BGFX_EMBEDDED_SHADER(fs_texture_cube2),
  51. BGFX_EMBEDDED_SHADER(fs_texture_sdf),
  52. BGFX_EMBEDDED_SHADER(fs_texture_msdf),
  53. BGFX_EMBEDDED_SHADER(fs_texture_3d),
  54. BGFX_EMBEDDED_SHADER_END()
  55. };
  56. static const char* s_supportedExt[] =
  57. {
  58. "bmp",
  59. "dds",
  60. "exr",
  61. "gif",
  62. "gnf",
  63. "jpg",
  64. "jpeg",
  65. "hdr",
  66. "ktx",
  67. "pgm",
  68. "png",
  69. "ppm",
  70. "psd",
  71. "pvr",
  72. "tga",
  73. };
  74. struct Binding
  75. {
  76. enum Enum
  77. {
  78. App,
  79. View,
  80. Help,
  81. About,
  82. Count
  83. };
  84. };
  85. struct Geometry
  86. {
  87. enum Enum
  88. {
  89. Quad,
  90. Cross,
  91. Hexagon,
  92. Count
  93. };
  94. };
  95. struct Output
  96. {
  97. enum Enum
  98. {
  99. sRGB,
  100. scRGB,
  101. HDR10,
  102. Count
  103. };
  104. };
  105. static const InputBinding s_bindingApp[] =
  106. {
  107. { entry::Key::KeyQ, entry::Modifier::None, 1, NULL, "exit" },
  108. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  109. INPUT_BINDING_END
  110. };
  111. const char* s_resetCmd =
  112. "view zoom 1.0\n"
  113. "view rotate 0\n"
  114. "view cubemap\n"
  115. "view pan\n"
  116. "view ev\n"
  117. ;
  118. static const InputBinding s_bindingView[] =
  119. {
  120. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  121. { entry::Key::Comma, entry::Modifier::None, 1, NULL, "view mip prev" },
  122. { entry::Key::Period, entry::Modifier::None, 1, NULL, "view mip next" },
  123. { entry::Key::Comma, entry::Modifier::LeftShift, 1, NULL, "view mip" },
  124. { entry::Key::Comma, entry::Modifier::RightShift, 1, NULL, "view mip" },
  125. { entry::Key::Slash, entry::Modifier::None, 1, NULL, "view filter" },
  126. { entry::Key::Key1, entry::Modifier::None, 1, NULL, "view zoom 1.0\n"
  127. "view fit\n" },
  128. { entry::Key::Key0, entry::Modifier::None, 1, NULL, s_resetCmd },
  129. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view zoom +0.1" },
  130. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view zoom -0.1" },
  131. { entry::Key::KeyZ, entry::Modifier::None, 1, NULL, "view rotate -90" },
  132. { entry::Key::KeyZ, entry::Modifier::LeftShift, 1, NULL, "view rotate +90" },
  133. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view pan\n"
  134. "view file-up" },
  135. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view pan\n"
  136. "view file-down" },
  137. { entry::Key::PageUp, entry::Modifier::None, 1, NULL, "view pan\n"
  138. "view file-pgup" },
  139. { entry::Key::PageDown, entry::Modifier::None, 1, NULL, "view pan\n"
  140. "view file-pgdown" },
  141. { entry::Key::Left, entry::Modifier::None, 1, NULL, "view layer prev" },
  142. { entry::Key::Right, entry::Modifier::None, 1, NULL, "view layer next" },
  143. { entry::Key::KeyR, entry::Modifier::None, 1, NULL, "view rgb r" },
  144. { entry::Key::KeyG, entry::Modifier::None, 1, NULL, "view rgb g" },
  145. { entry::Key::KeyB, entry::Modifier::None, 1, NULL, "view rgb b" },
  146. { entry::Key::KeyA, entry::Modifier::None, 1, NULL, "view rgb a" },
  147. { entry::Key::KeyI, entry::Modifier::None, 1, NULL, "view info" },
  148. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  149. { entry::Key::Return, entry::Modifier::None, 1, NULL, "view files" },
  150. { entry::Key::KeyS, entry::Modifier::None, 1, NULL, "view sdf" },
  151. { entry::Key::Space, entry::Modifier::None, 1, NULL, "view geo\n"
  152. "view pan\n" },
  153. INPUT_BINDING_END
  154. };
  155. static const InputBinding s_bindingHelp[] =
  156. {
  157. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "view help" },
  158. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  159. INPUT_BINDING_END
  160. };
  161. static const InputBinding s_bindingAbout[] =
  162. {
  163. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "view about" },
  164. INPUT_BINDING_END
  165. };
  166. static const char* s_bindingName[] =
  167. {
  168. "App",
  169. "View",
  170. "Help",
  171. "About",
  172. };
  173. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  174. static const InputBinding* s_binding[] =
  175. {
  176. s_bindingApp,
  177. s_bindingView,
  178. s_bindingHelp,
  179. s_bindingAbout,
  180. };
  181. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  182. struct View
  183. {
  184. View()
  185. : m_cubeMapGeo(Geometry::Quad)
  186. , m_outputFormat(Output::sRGB)
  187. , m_fileIndex(0)
  188. , m_scaleFn(0)
  189. , m_mip(0)
  190. , m_layer(0)
  191. , m_abgr(UINT32_MAX)
  192. , m_ev(0.0f)
  193. , m_evMin(kEvMin)
  194. , m_evMax(kEvMax)
  195. , m_posx(0.0f)
  196. , m_posy(0.0f)
  197. , m_angx(0.0f)
  198. , m_angy(0.0f)
  199. , m_zoom(1.0f)
  200. , m_angle(0.0f)
  201. , m_orientation(0.0f)
  202. , m_flipH(0.0f)
  203. , m_flipV(0.0f)
  204. , m_transitionTime(1.0f)
  205. , m_width(1280)
  206. , m_height(720)
  207. , m_filter(true)
  208. , m_fit(true)
  209. , m_alpha(false)
  210. , m_help(false)
  211. , m_about(false)
  212. , m_info(false)
  213. , m_files(false)
  214. , m_sdf(false)
  215. , m_inLinear(false)
  216. {
  217. load();
  218. m_textureInfo.format = bgfx::TextureFormat::Count;
  219. }
  220. ~View()
  221. {
  222. }
  223. int32_t cmd(int32_t _argc, char const* const* _argv)
  224. {
  225. if (_argc >= 2)
  226. {
  227. if (0 == bx::strCmp(_argv[1], "mip") )
  228. {
  229. if (_argc >= 3)
  230. {
  231. uint32_t mip = m_mip;
  232. if (0 == bx::strCmp(_argv[2], "next") )
  233. {
  234. ++mip;
  235. }
  236. else if (0 == bx::strCmp(_argv[2], "prev") )
  237. {
  238. --mip;
  239. }
  240. else if (0 == bx::strCmp(_argv[2], "last") )
  241. {
  242. mip = INT32_MAX;
  243. }
  244. else
  245. {
  246. bx::fromString(&mip, _argv[2]);
  247. }
  248. m_mip = bx::uint32_iclamp(mip, 0, m_textureInfo.numMips-1);
  249. }
  250. else
  251. {
  252. m_mip = 0;
  253. }
  254. }
  255. else if (0 == bx::strCmp(_argv[1], "layer") )
  256. {
  257. if (_argc >= 3)
  258. {
  259. uint32_t layer = m_layer;
  260. if (0 == bx::strCmp(_argv[2], "next") )
  261. {
  262. ++layer;
  263. }
  264. else if (0 == bx::strCmp(_argv[2], "prev") )
  265. {
  266. --layer;
  267. }
  268. else if (0 == bx::strCmp(_argv[2], "last") )
  269. {
  270. layer = INT32_MAX;
  271. }
  272. else
  273. {
  274. bx::fromString(&layer, _argv[2]);
  275. }
  276. m_layer = bx::uint32_iclamp(layer, 0, m_textureInfo.numLayers-1);
  277. }
  278. else
  279. {
  280. m_layer = 0;
  281. }
  282. }
  283. else if (0 == bx::strCmp(_argv[1], "ev") )
  284. {
  285. if (_argc >= 3)
  286. {
  287. float ev = m_ev;
  288. bx::fromString(&ev, _argv[2]);
  289. m_ev = bx::clamp(ev, kEvMin, kEvMax);
  290. }
  291. else
  292. {
  293. m_ev = 0.0f;
  294. }
  295. }
  296. else if (0 == bx::strCmp(_argv[1], "pan") )
  297. {
  298. if (_argc >= 3)
  299. {
  300. if (_argc >= 4)
  301. {
  302. float yy;
  303. bx::fromString(&yy, _argv[3]);
  304. if (_argv[3][0] == '+'
  305. || _argv[3][0] == '-')
  306. {
  307. m_posy += yy;
  308. }
  309. else
  310. {
  311. m_posy = yy;
  312. }
  313. }
  314. float xx;
  315. bx::fromString(&xx, _argv[2]);
  316. if (_argv[2][0] == '+'
  317. || _argv[2][0] == '-')
  318. {
  319. m_posx += xx;
  320. }
  321. else
  322. {
  323. m_posx = xx;
  324. }
  325. }
  326. else
  327. {
  328. m_posx = 0.0f;
  329. m_posy = 0.0f;
  330. }
  331. }
  332. else if (0 == bx::strCmp(_argv[1], "cubemap") )
  333. {
  334. if (_argc >= 3)
  335. {
  336. if (_argc >= 4)
  337. {
  338. float yy;
  339. bx::fromString(&yy, _argv[3]);
  340. if (_argv[3][0] == '+'
  341. || _argv[3][0] == '-')
  342. {
  343. m_angy += bx::toRad(yy);
  344. }
  345. else
  346. {
  347. m_angy = bx::toRad(yy);
  348. }
  349. }
  350. float xx;
  351. bx::fromString(&xx, _argv[2]);
  352. if (_argv[2][0] == '+'
  353. || _argv[2][0] == '-')
  354. {
  355. m_angx += bx::toRad(xx);
  356. }
  357. else
  358. {
  359. m_angx = bx::toRad(xx);
  360. }
  361. }
  362. else
  363. {
  364. m_angx = 0.0f;
  365. m_angy = 0.0f;
  366. }
  367. }
  368. else if (0 == bx::strCmp(_argv[1], "zoom") )
  369. {
  370. if (_argc >= 3)
  371. {
  372. float zoom;
  373. bx::fromString(&zoom, _argv[2]);
  374. if (_argv[2][0] == '+'
  375. || _argv[2][0] == '-')
  376. {
  377. m_zoom += zoom;
  378. }
  379. else
  380. {
  381. m_zoom = zoom;
  382. }
  383. m_zoom = bx::clamp(m_zoom, 0.01f, 10.0f);
  384. }
  385. else
  386. {
  387. m_zoom = 1.0f;
  388. }
  389. }
  390. else if (0 == bx::strCmp(_argv[1], "rotate") )
  391. {
  392. if (_argc >= 3)
  393. {
  394. float angle;
  395. bx::fromString(&angle, _argv[2]);
  396. if (_argv[2][0] == '+'
  397. || _argv[2][0] == '-')
  398. {
  399. m_angle += bx::toRad(angle);
  400. }
  401. else
  402. {
  403. m_angle = bx::toRad(angle);
  404. }
  405. m_angle = bx::wrap(m_angle, bx::kPi*2.0f);
  406. }
  407. else
  408. {
  409. m_angle = 0.0f;
  410. }
  411. }
  412. else if (0 == bx::strCmp(_argv[1], "orientation") )
  413. {
  414. if (_argc >= 3)
  415. {
  416. float* dst = NULL;
  417. char axis = bx::toLower(_argv[2][0]);
  418. switch (axis)
  419. {
  420. case 'x': dst = &m_flipV; break;
  421. case 'y': dst = &m_flipH; break;
  422. case 'z': dst = &m_orientation; break;
  423. default: break;
  424. }
  425. if (NULL != dst)
  426. {
  427. if (_argc >= 4)
  428. {
  429. float angle;
  430. bx::fromString(&angle, _argv[3]);
  431. *dst = bx::toRad(angle);
  432. }
  433. else
  434. {
  435. *dst = 0.0f;
  436. }
  437. }
  438. }
  439. else
  440. {
  441. m_flipH = 0.0f;
  442. m_flipV = 0.0f;
  443. m_orientation = 0.0f;
  444. }
  445. }
  446. else if (0 == bx::strCmp(_argv[1], "transition") )
  447. {
  448. if (_argc >= 3)
  449. {
  450. float time;
  451. bx::fromString(&time, _argv[2]);
  452. m_transitionTime = bx::clamp(time, 0.0f, 5.0f);
  453. }
  454. else
  455. {
  456. m_transitionTime = 1.0f;
  457. }
  458. }
  459. else if (0 == bx::strCmp(_argv[1], "filter") )
  460. {
  461. if (_argc >= 3)
  462. {
  463. bx::fromString(&m_filter, _argv[2]);
  464. }
  465. else
  466. {
  467. m_filter ^= true;
  468. }
  469. }
  470. else if (0 == bx::strCmp(_argv[1], "fit") )
  471. {
  472. if (_argc >= 3)
  473. {
  474. bx::fromString(&m_fit, _argv[2]);
  475. }
  476. else
  477. {
  478. m_fit ^= true;
  479. }
  480. }
  481. else if (0 == bx::strCmp(_argv[1], "file-up") )
  482. {
  483. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  484. }
  485. else if (0 == bx::strCmp(_argv[1], "file-down") )
  486. {
  487. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  488. ++m_fileIndex;
  489. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  490. }
  491. else if (0 == bx::strCmp(_argv[1], "rgb") )
  492. {
  493. if (_argc >= 3)
  494. {
  495. if (_argv[2][0] == 'r')
  496. {
  497. m_abgr ^= 0x000000ff;
  498. }
  499. else if (_argv[2][0] == 'g')
  500. {
  501. m_abgr ^= 0x0000ff00;
  502. }
  503. else if (_argv[2][0] == 'b')
  504. {
  505. m_abgr ^= 0x00ff0000;
  506. }
  507. else if (_argv[2][0] == 'a')
  508. {
  509. m_alpha ^= true;
  510. }
  511. }
  512. else
  513. {
  514. m_abgr = UINT32_MAX;
  515. m_alpha = false;
  516. }
  517. }
  518. else if (0 == bx::strCmp(_argv[1], "sdf") )
  519. {
  520. m_sdf ^= true;
  521. }
  522. else if (0 == bx::strCmp(_argv[1], "geo") )
  523. {
  524. if (_argc >= 3)
  525. {
  526. if (bx::toLower(_argv[2][0]) == 'c')
  527. {
  528. m_cubeMapGeo = Geometry::Cross;
  529. }
  530. else if (bx::toLower(_argv[2][0]) == 'h')
  531. {
  532. m_cubeMapGeo = Geometry::Hexagon;
  533. }
  534. else
  535. {
  536. m_cubeMapGeo = Geometry::Quad;
  537. }
  538. }
  539. else
  540. {
  541. m_cubeMapGeo = Geometry::Enum( (m_cubeMapGeo + 1) % Geometry::Count);
  542. }
  543. }
  544. else if (0 == bx::strCmp(_argv[1], "output") )
  545. {
  546. Output::Enum outputPrev = m_outputFormat;
  547. if (_argc >= 3)
  548. {
  549. if (0 == bx::strCmp(_argv[2], "srgb") )
  550. {
  551. m_outputFormat = Output::sRGB;
  552. }
  553. else if (0 == bx::strCmp(_argv[2], "scrgb") )
  554. {
  555. m_outputFormat = Output::scRGB;
  556. }
  557. else if (0 == bx::strCmp(_argv[2], "hdr10") )
  558. {
  559. m_outputFormat = Output::HDR10;
  560. }
  561. }
  562. else
  563. {
  564. m_outputFormat = Output::Enum( (m_outputFormat + 1) % Output::Count);
  565. }
  566. if (outputPrev != m_outputFormat)
  567. {
  568. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8;
  569. uint32_t formatFlag = 0;
  570. if (Output::scRGB == m_outputFormat)
  571. {
  572. format = bgfx::TextureFormat::RGBA16F;
  573. }
  574. else if (Output::HDR10 == m_outputFormat)
  575. {
  576. format = bgfx::TextureFormat::RGB10A2;
  577. formatFlag = BGFX_RESET_HDR10;
  578. }
  579. bgfx::reset(m_width, m_height, BGFX_RESET_VSYNC | formatFlag, format);
  580. }
  581. }
  582. else if (0 == bx::strCmp(_argv[1], "help") )
  583. {
  584. m_help ^= true;
  585. }
  586. else if (0 == bx::strCmp(_argv[1], "about") )
  587. {
  588. m_about ^= true;
  589. }
  590. else if (0 == bx::strCmp(_argv[1], "save") )
  591. {
  592. save();
  593. }
  594. else if (0 == bx::strCmp(_argv[1], "info") )
  595. {
  596. m_info ^= true;
  597. }
  598. else if (0 == bx::strCmp(_argv[1], "files") )
  599. {
  600. m_files ^= true;
  601. }
  602. }
  603. return 0;
  604. }
  605. static bool sortNameAscending(const std::string& _lhs, const std::string& _rhs)
  606. {
  607. return 0 > bx::strCmpV(_lhs.c_str(), _rhs.c_str() );
  608. }
  609. void updateFileList(const bx::FilePath& _filePath)
  610. {
  611. bx::DirectoryReader dr;
  612. if (bx::open(&dr, _filePath) )
  613. {
  614. m_path = _filePath;
  615. }
  616. else if (bx::open(&dr, _filePath.getPath() ) )
  617. {
  618. m_path = _filePath.getPath();
  619. }
  620. else
  621. {
  622. DBG("File path `%s` not found.", _filePath.get() );
  623. return;
  624. }
  625. bx::Error err;
  626. while (err.isOk() )
  627. {
  628. bx::FileInfo fi;
  629. bx::read(&dr, fi, &err);
  630. if (err.isOk()
  631. && bx::FileType::File == fi.type)
  632. {
  633. bx::StringView ext = fi.filePath.getExt();
  634. if (!ext.isEmpty() )
  635. {
  636. ext.set(ext.getPtr()+1, ext.getTerm() );
  637. bool supported = false;
  638. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  639. {
  640. if (0 == bx::strCmpI(ext, s_supportedExt[ii]) )
  641. {
  642. supported = true;
  643. break;
  644. }
  645. }
  646. if (supported)
  647. {
  648. m_fileList.push_back(fi.filePath.get() );
  649. }
  650. }
  651. }
  652. }
  653. bx::close(&dr);
  654. std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);
  655. m_fileIndex = 0;
  656. uint32_t idx = 0;
  657. for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
  658. {
  659. if (0 == bx::strCmpI(it->c_str(), _filePath.getFileName() ) )
  660. {
  661. // If it is case-insensitive match then might be correct one, but keep
  662. // searching.
  663. m_fileIndex = idx;
  664. if (0 == bx::strCmp(it->c_str(), _filePath.getFileName() ) )
  665. {
  666. // If it is exact match we're done.
  667. break;
  668. }
  669. }
  670. }
  671. }
  672. void load()
  673. {
  674. bx::FilePath filePath(bx::Dir::Home);
  675. filePath.join(".config/bgfx/texturev.ini");
  676. bx::Settings settings(entry::getAllocator() );
  677. bx::FileReader reader;
  678. if (bx::open(&reader, filePath) )
  679. {
  680. bx::read(&reader, settings);
  681. bx::close(&reader);
  682. if (!bx::fromString(&m_transitionTime, settings.get("view/transition") ) )
  683. {
  684. m_transitionTime = 1.0f;
  685. }
  686. if (!bx::fromString(&m_width, settings.get("view/width") ) )
  687. {
  688. m_width = 1280;
  689. }
  690. if (!bx::fromString(&m_height, settings.get("view/height") ) )
  691. {
  692. m_height = 720;
  693. }
  694. }
  695. }
  696. void save()
  697. {
  698. bx::FilePath filePath(bx::Dir::Home);
  699. filePath.join(".config/bgfx/texturev.ini");
  700. if (bx::makeAll(filePath.getPath() ) )
  701. {
  702. bx::Settings settings(entry::getAllocator() );
  703. char tmp[256];
  704. bx::toString(tmp, sizeof(tmp), m_transitionTime);
  705. settings.set("view/transition", tmp);
  706. bx::toString(tmp, sizeof(tmp), m_width);
  707. settings.set("view/width", tmp);
  708. bx::toString(tmp, sizeof(tmp), m_height);
  709. settings.set("view/height", tmp);
  710. bx::FileWriter writer;
  711. if (bx::open(&writer, filePath) )
  712. {
  713. bx::write(&writer, settings);
  714. bx::close(&writer);
  715. }
  716. }
  717. }
  718. bx::FilePath m_path;
  719. typedef stl::vector<std::string> FileList;
  720. FileList m_fileList;
  721. bgfx::TextureInfo m_textureInfo;
  722. Geometry::Enum m_cubeMapGeo;
  723. Output::Enum m_outputFormat;
  724. uint32_t m_fileIndex;
  725. uint32_t m_scaleFn;
  726. uint32_t m_mip;
  727. uint32_t m_layer;
  728. uint32_t m_abgr;
  729. float m_ev;
  730. float m_evMin;
  731. float m_evMax;
  732. float m_posx;
  733. float m_posy;
  734. float m_angx;
  735. float m_angy;
  736. float m_zoom;
  737. float m_angle;
  738. float m_orientation;
  739. float m_flipH;
  740. float m_flipV;
  741. float m_transitionTime;
  742. uint32_t m_width;
  743. uint32_t m_height;
  744. bool m_filter;
  745. bool m_fit;
  746. bool m_alpha;
  747. bool m_help;
  748. bool m_about;
  749. bool m_info;
  750. bool m_files;
  751. bool m_sdf;
  752. bool m_inLinear;
  753. };
  754. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  755. {
  756. View* view = static_cast<View*>(_userData);
  757. return view->cmd(_argc, _argv);
  758. }
  759. struct PosUvwColorVertex
  760. {
  761. float m_x;
  762. float m_y;
  763. float m_u;
  764. float m_v;
  765. float m_w;
  766. uint32_t m_abgr;
  767. static void init()
  768. {
  769. ms_decl
  770. .begin()
  771. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  772. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  773. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  774. .end();
  775. }
  776. void set(float _x, float _y, float _u, float _v, float _w, uint32_t _abgr)
  777. {
  778. m_x = _x;
  779. m_y = _y;
  780. m_u = _u;
  781. m_v = _v;
  782. m_w = _w;
  783. m_abgr = _abgr;
  784. }
  785. static bgfx::VertexDecl ms_decl;
  786. };
  787. bgfx::VertexDecl PosUvwColorVertex::ms_decl;
  788. static uint32_t addQuad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
  789. {
  790. _indices[0] = _idx0;
  791. _indices[1] = _idx3;
  792. _indices[2] = _idx1;
  793. _indices[3] = _idx1;
  794. _indices[4] = _idx3;
  795. _indices[5] = _idx2;
  796. return 6;
  797. }
  798. void setGeometry(
  799. Geometry::Enum _type
  800. , int32_t _x
  801. , int32_t _y
  802. , uint32_t _width
  803. , uint32_t _height
  804. , uint32_t _abgr
  805. , float _maxu = 1.0f
  806. , float _maxv = 1.0f
  807. )
  808. {
  809. if (Geometry::Quad == _type)
  810. {
  811. if (6 == bgfx::getAvailTransientVertexBuffer(6, PosUvwColorVertex::ms_decl) )
  812. {
  813. bgfx::TransientVertexBuffer vb;
  814. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvwColorVertex::ms_decl);
  815. PosUvwColorVertex* vertex = (PosUvwColorVertex*)vb.data;
  816. const float widthf = float(_width);
  817. const float heightf = float(_height);
  818. const float minx = float(_x);
  819. const float miny = float(_y);
  820. const float maxx = minx+widthf;
  821. const float maxy = miny+heightf;
  822. const float minu = 0.0f;
  823. const float maxu = _maxu;
  824. const float minv = 0.0f;
  825. const float maxv = _maxv;
  826. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  827. vertex->set(maxx, miny, maxu, minv, 0.0f, _abgr); ++vertex;
  828. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  829. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  830. vertex->set(minx, maxy, minu, maxv, 0.0f, _abgr); ++vertex;
  831. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  832. bgfx::setVertexBuffer(0, &vb);
  833. }
  834. }
  835. else
  836. {
  837. const uint32_t numVertices = 14;
  838. const uint32_t numIndices = 36;
  839. if (checkAvailTransientBuffers(numVertices, PosUvwColorVertex::ms_decl, numIndices) )
  840. {
  841. bgfx::TransientVertexBuffer tvb;
  842. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosUvwColorVertex::ms_decl);
  843. bgfx::TransientIndexBuffer tib;
  844. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  845. PosUvwColorVertex* vertex = (PosUvwColorVertex*)tvb.data;
  846. uint16_t* indices = (uint16_t*)tib.data;
  847. if (Geometry::Cross == _type)
  848. {
  849. const float sx = _width /1.5f;
  850. const float sy = _height/1.5f;
  851. const float px = float(_x)-sx/4.0f;
  852. const float py = float(_y);
  853. vertex->set(0.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  854. vertex->set(0.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  855. vertex->set(0.5f*sx+px, 0.0f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  856. vertex->set(0.5f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  857. vertex->set(0.5f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  858. vertex->set(0.5f*sx+px, 1.5f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  859. vertex->set(1.0f*sx+px, 0.0f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  860. vertex->set(1.0f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  861. vertex->set(1.0f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  862. vertex->set(1.0f*sx+px, 1.5f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  863. vertex->set(1.5f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  864. vertex->set(1.5f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  865. vertex->set(2.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  866. vertex->set(2.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  867. indices += addQuad(indices, 0, 3, 4, 1);
  868. indices += addQuad(indices, 2, 6, 7, 3);
  869. indices += addQuad(indices, 3, 7, 8, 4);
  870. indices += addQuad(indices, 4, 8, 9, 5);
  871. indices += addQuad(indices, 7, 10, 11, 8);
  872. indices += addQuad(indices, 10, 12, 13, 11);
  873. }
  874. else
  875. {
  876. const float sx = float(_width);
  877. const float sy = float(_height);
  878. const float px = float(_x) - sx/2.0f;
  879. const float py = float(_y);
  880. vertex->set(0.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  881. vertex->set(0.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  882. vertex->set(0.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  883. vertex->set(0.5f*sx+px, 0.50f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  884. vertex->set(0.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  885. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  886. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  887. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  888. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  889. vertex->set(1.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  890. vertex->set(1.5f*sx+px, 0.50f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  891. vertex->set(1.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  892. vertex->set(2.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  893. vertex->set(2.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  894. indices += addQuad(indices, 0, 2, 3, 1);
  895. indices += addQuad(indices, 1, 3, 6, 4);
  896. indices += addQuad(indices, 2, 5, 6, 3);
  897. indices += addQuad(indices, 7, 9, 12, 10);
  898. indices += addQuad(indices, 7, 10, 11, 8);
  899. indices += addQuad(indices, 10, 12, 13, 11);
  900. }
  901. bgfx::setVertexBuffer(0, &tvb);
  902. bgfx::setIndexBuffer(&tib);
  903. }
  904. }
  905. }
  906. template<bx::LerpFn lerpT, bx::EaseFn easeT>
  907. struct InterpolatorT
  908. {
  909. float from;
  910. float to;
  911. float duration;
  912. int64_t offset;
  913. InterpolatorT(float _value)
  914. {
  915. reset(_value);
  916. }
  917. void reset(float _value)
  918. {
  919. from = _value;
  920. to = _value;
  921. duration = 0.0;
  922. offset = bx::getHPCounter();
  923. }
  924. void set(float _value, float _duration)
  925. {
  926. if (_value != to)
  927. {
  928. from = getValue();
  929. to = _value;
  930. duration = _duration;
  931. offset = bx::getHPCounter();
  932. }
  933. }
  934. float getValue()
  935. {
  936. if (isActive() )
  937. {
  938. const double freq = double(bx::getHPFrequency() );
  939. int64_t now = bx::getHPCounter();
  940. float time = (float)(double(now - offset) / freq);
  941. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  942. return lerpT(from, to, easeT(lerp) );
  943. }
  944. return to;
  945. }
  946. bool isActive() const
  947. {
  948. const double freq = double(bx::getHPFrequency() );
  949. int64_t now = bx::getHPCounter();
  950. float time = (float)(double(now - offset) / freq);
  951. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  952. return lerp < 1.0f;
  953. }
  954. };
  955. typedef InterpolatorT<bx::lerp, bx::easeInOutQuad> Interpolator;
  956. typedef InterpolatorT<bx::angleLerp, bx::easeInOutCubic> InterpolatorAngle;
  957. typedef InterpolatorT<bx::lerp, bx::easeLinear> InterpolatorLinear;
  958. void keyBindingHelp(const char* _bindings, const char* _description)
  959. {
  960. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
  961. ImGui::SameLine(100);
  962. ImGui::Text(_description);
  963. }
  964. void associate()
  965. {
  966. #if BX_PLATFORM_WINDOWS
  967. std::string str;
  968. char exec[MAX_PATH];
  969. GetModuleFileNameA(GetModuleHandleA(NULL), exec, MAX_PATH);
  970. std::string strExec = bx::replaceAll<std::string>(exec, "\\", "\\\\");
  971. std::string value;
  972. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  973. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  974. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  975. str += value;
  976. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  977. str += value;
  978. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  979. {
  980. const char* ext = s_supportedExt[ii];
  981. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  982. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  983. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  984. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  985. }
  986. bx::FilePath filePath(bx::Dir::Temp);
  987. filePath.join("texture.reg");
  988. bx::FileWriter writer;
  989. bx::Error err;
  990. if (bx::open(&writer, filePath, false, &err) )
  991. {
  992. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  993. bx::close(&writer);
  994. if (err.isOk() )
  995. {
  996. std::string cmd;
  997. bx::stringPrintf(cmd, "/s %s", filePath.get() );
  998. bx::ProcessReader reader;
  999. if (bx::open(&reader, "regedit.exe", cmd.c_str(), &err) )
  1000. {
  1001. bx::close(&reader);
  1002. }
  1003. }
  1004. }
  1005. #elif BX_PLATFORM_LINUX
  1006. std::string str;
  1007. str += "#/bin/bash\n\n";
  1008. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1009. {
  1010. const char* ext = s_supportedExt[ii];
  1011. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  1012. }
  1013. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/x-dds\n");
  1014. str += "\n";
  1015. bx::FileWriter writer;
  1016. bx::Error err;
  1017. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  1018. {
  1019. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  1020. bx::close(&writer);
  1021. if (err.isOk() )
  1022. {
  1023. bx::ProcessReader reader;
  1024. if (bx::open(&reader, "/bin/bash", "/tmp/texturev.sh", &err) )
  1025. {
  1026. bx::close(&reader);
  1027. }
  1028. }
  1029. }
  1030. #endif // BX_PLATFORM_WINDOWS
  1031. }
  1032. void help(const char* _error = NULL)
  1033. {
  1034. if (NULL != _error)
  1035. {
  1036. fprintf(stderr, "Error:\n%s\n\n", _error);
  1037. }
  1038. fprintf(stderr
  1039. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1040. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  1041. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  1042. , BGFX_TEXTUREV_VERSION_MAJOR
  1043. , BGFX_TEXTUREV_VERSION_MINOR
  1044. , BGFX_API_VERSION
  1045. );
  1046. fprintf(stderr
  1047. , "Usage: texturev <file path>\n"
  1048. "\n"
  1049. "Supported input file types:\n"
  1050. );
  1051. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1052. {
  1053. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  1054. }
  1055. fprintf(stderr
  1056. , "\n"
  1057. "Options:\n"
  1058. " -h, --help Help.\n"
  1059. " -v, --version Version information only.\n"
  1060. " --associate Associate file extensions with texturev.\n"
  1061. "\n"
  1062. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  1063. );
  1064. }
  1065. int _main_(int _argc, char** _argv)
  1066. {
  1067. bx::CommandLine cmdLine(_argc, _argv);
  1068. if (cmdLine.hasArg('v', "version") )
  1069. {
  1070. fprintf(stderr
  1071. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1072. , BGFX_TEXTUREV_VERSION_MAJOR
  1073. , BGFX_TEXTUREV_VERSION_MINOR
  1074. , BGFX_API_VERSION
  1075. );
  1076. return bx::kExitSuccess;
  1077. }
  1078. if (cmdLine.hasArg('h', "help") )
  1079. {
  1080. help();
  1081. return bx::kExitFailure;
  1082. }
  1083. else if (cmdLine.hasArg("associate") )
  1084. {
  1085. associate();
  1086. return bx::kExitFailure;
  1087. }
  1088. uint32_t debug = BGFX_DEBUG_TEXT;
  1089. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  1090. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1091. View view;
  1092. cmdAdd("view", cmdView, &view);
  1093. entry::setWindowFlags(entry::WindowHandle{0}, ENTRY_WINDOW_FLAG_ASPECT_RATIO, false);
  1094. entry::setWindowSize(entry::WindowHandle{0}, view.m_width, view.m_height);
  1095. bgfx::Init init;
  1096. init.resolution.width = view.m_width;
  1097. init.resolution.width = view.m_height;
  1098. init.resolution.reset = BGFX_RESET_VSYNC;
  1099. bgfx::init(init);
  1100. // Set view 0 clear state.
  1101. bgfx::setViewClear(BACKGROUND_VIEW_ID
  1102. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  1103. , 0x000000ff
  1104. , 1.0f
  1105. , 0
  1106. );
  1107. imguiCreate();
  1108. PosUvwColorVertex::init();
  1109. const bgfx::Caps* caps = bgfx::getCaps();
  1110. bgfx::RendererType::Enum type = caps->rendererType;
  1111. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  1112. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  1113. bgfx::UniformHandle u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Vec4);
  1114. bgfx::UniformHandle u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Vec4);
  1115. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  1116. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  1117. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  1118. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  1119. vsTexture
  1120. , fsTexture
  1121. , true
  1122. );
  1123. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  1124. vsTexture
  1125. , bgfx::isValid(fsTextureArray)
  1126. ? fsTextureArray
  1127. : fsTexture
  1128. , true
  1129. );
  1130. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  1131. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  1132. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  1133. , true
  1134. );
  1135. bgfx::ProgramHandle textureCube2Program = bgfx::createProgram(
  1136. vsTexture
  1137. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube2")
  1138. , true
  1139. );
  1140. bgfx::ProgramHandle textureSdfProgram = bgfx::createProgram(
  1141. vsTexture
  1142. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_sdf")
  1143. , true
  1144. );
  1145. bgfx::ProgramHandle textureMsdfProgram = bgfx::createProgram(
  1146. vsTexture
  1147. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_msdf")
  1148. , true
  1149. );
  1150. bgfx::ProgramHandle texture3DProgram = bgfx::createProgram(
  1151. vsTexture
  1152. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_3d")
  1153. , true
  1154. );
  1155. const uint32_t checkerBoardSize = 64;
  1156. bgfx::TextureHandle checkerBoard;
  1157. {
  1158. const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize*checkerBoardSize*4);
  1159. bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xff8e8e8e, 0xff5d5d5d);
  1160. checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
  1161. , bgfx::TextureFormat::BGRA8
  1162. , 0
  1163. | BGFX_SAMPLER_MIN_POINT
  1164. | BGFX_SAMPLER_MIP_POINT
  1165. | BGFX_SAMPLER_MAG_POINT
  1166. , mem
  1167. );
  1168. }
  1169. float speed = 0.37f;
  1170. float time = 0.0f;
  1171. Interpolator menuFade(5.0f);
  1172. Interpolator mip(0.0f);
  1173. Interpolator layer(0.0f);
  1174. InterpolatorLinear ev(0.0f);
  1175. Interpolator zoom(1.0f);
  1176. Interpolator scale(1.0f);
  1177. Interpolator posx(0.0f);
  1178. Interpolator posy(0.0f);
  1179. InterpolatorAngle angle(0.0f);
  1180. InterpolatorAngle angx(0.0f);
  1181. InterpolatorAngle angy(0.0f);
  1182. auto anyActive = [&]() -> bool
  1183. {
  1184. return false
  1185. || ImGui::MouseOverArea()
  1186. || menuFade.isActive()
  1187. || mip.isActive()
  1188. || layer.isActive()
  1189. || ev.isActive()
  1190. || zoom.isActive()
  1191. || scale.isActive()
  1192. || posx.isActive()
  1193. || posy.isActive()
  1194. || angle.isActive()
  1195. || angx.isActive()
  1196. || angy.isActive()
  1197. ;
  1198. };
  1199. const char* filePath = _argc < 2 ? "" : _argv[1];
  1200. std::string path = filePath;
  1201. {
  1202. bx::FilePath fp(filePath);
  1203. view.updateFileList(fp);
  1204. }
  1205. int exitcode = bx::kExitSuccess;
  1206. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  1207. {
  1208. uint32_t fileIndex = 0;
  1209. bool dragging = false;
  1210. entry::WindowState windowState;
  1211. entry::MouseState mouseStatePrev;
  1212. while (!entry::processWindowEvents(windowState, debug, init.resolution.reset) )
  1213. {
  1214. const entry::MouseState& mouseState = windowState.m_mouse;
  1215. view.m_width = windowState.m_width;
  1216. view.m_height = windowState.m_height;
  1217. if (!windowState.m_dropFile.isEmpty() )
  1218. {
  1219. view.updateFileList(windowState.m_dropFile);
  1220. windowState.m_dropFile.clear();
  1221. }
  1222. imguiBeginFrame(mouseState.m_mx
  1223. , mouseState.m_my
  1224. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1225. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1226. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1227. , mouseState.m_mz
  1228. , uint16_t(view.m_width)
  1229. , uint16_t(view.m_height)
  1230. );
  1231. bool modalWindow = view.m_help || view.m_about;
  1232. bool overArea = false
  1233. || ImGui::GetMousePos().y <= ImGui::GetTextLineHeightWithSpacing()
  1234. || ImGui::MouseOverArea()
  1235. ;
  1236. overArea &= !modalWindow;
  1237. if (overArea)
  1238. {
  1239. menuFade.set(5.0f, 0.25f);
  1240. }
  1241. else if (modalWindow)
  1242. {
  1243. menuFade.reset(0.0f);
  1244. }
  1245. else
  1246. {
  1247. menuFade.set(0.0f, 2.0f);
  1248. }
  1249. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, bx::clamp(menuFade.getValue(), 0.0f, 1.0f) );
  1250. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  1251. if (ImGui::BeginMainMenuBar() )
  1252. {
  1253. if (ImGui::BeginMenu("File"))
  1254. {
  1255. if (ImGui::MenuItem("Show File List", NULL, view.m_files) )
  1256. {
  1257. cmdExec("view files");
  1258. }
  1259. ImGui::Separator();
  1260. if (ImGui::MenuItem("Exit") )
  1261. {
  1262. cmdExec("exit");
  1263. }
  1264. ImGui::EndMenu();
  1265. }
  1266. if (ImGui::BeginMenu("View") )
  1267. {
  1268. if (ImGui::MenuItem("Info", NULL, view.m_info) )
  1269. {
  1270. cmdExec("view info");
  1271. }
  1272. if (ImGui::MenuItem("Reset") )
  1273. {
  1274. cmdExec(s_resetCmd);
  1275. }
  1276. ImGui::Separator();
  1277. bool filter = view.m_filter;
  1278. if (ImGui::MenuItem("Filter", NULL, &filter) )
  1279. {
  1280. cmdExec("view filter");
  1281. }
  1282. bool animate = 0.0f < view.m_transitionTime;
  1283. if (ImGui::MenuItem("Animate", NULL, &animate) )
  1284. {
  1285. cmdExec("view transition %f", animate ? 1.0f : 0.0f);
  1286. }
  1287. if (ImGui::BeginMenu("Cubemap", view.m_textureInfo.cubeMap) )
  1288. {
  1289. if (ImGui::MenuItem("Quad", NULL, Geometry::Quad == view.m_cubeMapGeo) )
  1290. {
  1291. cmdExec("view geo quad");
  1292. }
  1293. if (ImGui::MenuItem("Cross", NULL, Geometry::Cross == view.m_cubeMapGeo) )
  1294. {
  1295. cmdExec("view geo cross");
  1296. }
  1297. if (ImGui::MenuItem("Hexagon", NULL, Geometry::Hexagon == view.m_cubeMapGeo) )
  1298. {
  1299. cmdExec("view geo hexagon");
  1300. }
  1301. ImGui::EndMenu();
  1302. }
  1303. if (ImGui::BeginMenu("Output") )
  1304. {
  1305. const bool hdrCap = (bgfx::getCaps()->supported & BGFX_CAPS_HDR10);
  1306. if (ImGui::MenuItem("sRGB", NULL, Output::sRGB == view.m_outputFormat) )
  1307. {
  1308. cmdExec("view output srgb");
  1309. }
  1310. if (hdrCap)
  1311. {
  1312. if (ImGui::MenuItem("scRGB", NULL, Output::scRGB == view.m_outputFormat) )
  1313. {
  1314. cmdExec("view output scrgb");
  1315. }
  1316. if (ImGui::MenuItem("HDR10", NULL, Output::HDR10 == view.m_outputFormat) )
  1317. {
  1318. cmdExec("view output hdr10");
  1319. }
  1320. }
  1321. ImGui::EndMenu();
  1322. }
  1323. bool sdf = view.m_sdf;
  1324. if (ImGui::MenuItem("SDF", NULL, &sdf) )
  1325. {
  1326. cmdExec("view sdf");
  1327. }
  1328. bool rr = 0 != (view.m_abgr & 0x000000ff);
  1329. if (ImGui::MenuItem("R", NULL, &rr) )
  1330. {
  1331. cmdExec("view rgb r");
  1332. }
  1333. bool gg = 0 != (view.m_abgr & 0x0000ff00);
  1334. if (ImGui::MenuItem("G", NULL, &gg) )
  1335. {
  1336. cmdExec("view rgb g");
  1337. }
  1338. bool bb = 0 != (view.m_abgr & 0x00ff0000);
  1339. if (ImGui::MenuItem("B", NULL, &bb) )
  1340. {
  1341. cmdExec("view rgb b");
  1342. }
  1343. bool alpha = view.m_alpha;
  1344. if (ImGui::MenuItem("Checkerboard", NULL, &alpha) )
  1345. {
  1346. cmdExec("view rgb a");
  1347. }
  1348. ImGui::Separator();
  1349. if (ImGui::MenuItem("Save Options") )
  1350. {
  1351. cmdExec("view save");
  1352. }
  1353. ImGui::EndMenu();
  1354. }
  1355. if (ImGui::BeginMenu("Help") )
  1356. {
  1357. if (ImGui::MenuItem("View Help") )
  1358. {
  1359. cmdExec("view help");
  1360. }
  1361. ImGui::Separator();
  1362. if (ImGui::MenuItem("About") )
  1363. {
  1364. cmdExec("view about");
  1365. }
  1366. ImGui::EndMenu();
  1367. }
  1368. if (0 != view.m_fileList.size() )
  1369. {
  1370. ImGui::Separator();
  1371. ImGui::TextColored(
  1372. ImVec4(0.0f, 1.0f, 1.0f, 1.0f)
  1373. , view.m_fileList[view.m_fileIndex].c_str()
  1374. );
  1375. ImGui::Separator();
  1376. const char* name = "";
  1377. if (view.m_textureInfo.cubeMap)
  1378. {
  1379. name = " CubeMap";
  1380. }
  1381. else if (1 < view.m_textureInfo.depth)
  1382. {
  1383. name = " 3D";
  1384. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1385. }
  1386. else if (1 < view.m_textureInfo.numLayers)
  1387. {
  1388. name = " 2D Array";
  1389. }
  1390. ImGui::Text("%d x %d%s, mips: %d, layers %d, %s"
  1391. , view.m_textureInfo.width
  1392. , view.m_textureInfo.height
  1393. , name
  1394. , view.m_textureInfo.numMips
  1395. , view.m_textureInfo.numLayers
  1396. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1397. );
  1398. }
  1399. ImGui::EndMainMenuBar();
  1400. }
  1401. ImGui::PopStyleVar(2);
  1402. static bool help = false;
  1403. static bool about = false;
  1404. static bool mouseDelta = false;
  1405. if (!mouseDelta)
  1406. {
  1407. mouseStatePrev = mouseState;
  1408. mouseDelta = true;
  1409. }
  1410. int32_t zoomDelta = overArea ? 0 : mouseState.m_mz - mouseStatePrev.m_mz;
  1411. if (zoomDelta != 0)
  1412. {
  1413. char exec[64];
  1414. bx::snprintf(exec, BX_COUNTOF(exec), "view zoom %+f", -zoomDelta*0.1f);
  1415. cmdExec(exec);
  1416. }
  1417. const float xDelta = float(mouseStatePrev.m_mx - mouseState.m_mx);
  1418. const float yDelta = float(mouseStatePrev.m_my - mouseState.m_my);
  1419. if (!overArea
  1420. && !help
  1421. && mouseState.m_buttons[entry::MouseButton::Left] != mouseStatePrev.m_buttons[entry::MouseButton::Left])
  1422. {
  1423. dragging = !!mouseState.m_buttons[entry::MouseButton::Left];
  1424. }
  1425. if (dragging)
  1426. {
  1427. if (view.m_textureInfo.cubeMap
  1428. && Geometry::Quad == view.m_cubeMapGeo)
  1429. {
  1430. char exec[64];
  1431. bx::snprintf(exec, BX_COUNTOF(exec), "view cubemap %+f %+f", -yDelta, -xDelta);
  1432. cmdExec(exec);
  1433. }
  1434. else
  1435. {
  1436. char exec[64];
  1437. bx::snprintf(exec, BX_COUNTOF(exec), "view pan %+f %+f", xDelta, yDelta);
  1438. cmdExec(exec);
  1439. }
  1440. }
  1441. mouseStatePrev = mouseState;
  1442. if (help != view.m_help)
  1443. {
  1444. if (!help)
  1445. {
  1446. ImGui::OpenPopup("Help");
  1447. inputRemoveBindings(s_bindingName[Binding::View]);
  1448. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  1449. }
  1450. else
  1451. {
  1452. inputRemoveBindings(s_bindingName[Binding::Help]);
  1453. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1454. }
  1455. help = view.m_help;
  1456. }
  1457. if (about != view.m_about)
  1458. {
  1459. if (!about)
  1460. {
  1461. ImGui::OpenPopup("About");
  1462. inputRemoveBindings(s_bindingName[Binding::View]);
  1463. inputAddBindings(s_bindingName[Binding::About], s_binding[Binding::About]);
  1464. }
  1465. else
  1466. {
  1467. inputRemoveBindings(s_bindingName[Binding::About]);
  1468. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1469. }
  1470. about = view.m_about;
  1471. }
  1472. if (view.m_info)
  1473. {
  1474. ImGui::SetNextWindowSize(
  1475. ImVec2(300.0f, 320.0f)
  1476. , ImGuiCond_FirstUseEver
  1477. );
  1478. if (ImGui::Begin("Info", &view.m_info) )
  1479. {
  1480. if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
  1481. {
  1482. if (!bgfx::isValid(texture) )
  1483. {
  1484. ImGui::Text("Texture is not loaded.");
  1485. }
  1486. else
  1487. {
  1488. ImGui::Text("Name: %s", view.m_fileList[view.m_fileIndex].c_str() );
  1489. ImGui::Text("Dimensions: %d x %d"
  1490. , view.m_textureInfo.width
  1491. , view.m_textureInfo.height
  1492. );
  1493. ImGui::Text("Format: %s"
  1494. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1495. );
  1496. uint32_t numLayers = view.m_textureInfo.numLayers;
  1497. if (1 < view.m_textureInfo.depth)
  1498. {
  1499. numLayers = bx::max(1, view.m_textureInfo.depth >> view.m_mip);
  1500. }
  1501. view.m_layer = bx::clamp<int32_t>(view.m_layer, 0, numLayers - 1);
  1502. ImGui::SliderInt("Layer", (int32_t*)&view.m_layer, 0, numLayers - 1);
  1503. ImGui::SliderInt("Mip", (int32_t*)&view.m_mip, 0, view.m_textureInfo.numMips - 1);
  1504. ImGui::Separator();
  1505. ImGui::Checkbox("Input linear", &view.m_inLinear);
  1506. ImGui::RangeSliderFloat("EV range", &view.m_evMin, &view.m_evMax, kEvMin, kEvMax);
  1507. ImGui::SliderFloat("EV", &view.m_ev, view.m_evMin, view.m_evMax);
  1508. ImGui::Separator();
  1509. ImGui::Checkbox("Fit to window", &view.m_fit);
  1510. ImGui::SliderFloat("Scale", &view.m_zoom, 0.01f, 10.0f);
  1511. }
  1512. ImGui::EndChild();
  1513. }
  1514. }
  1515. ImGui::End();
  1516. }
  1517. if (view.m_files)
  1518. {
  1519. char temp[bx::kMaxFilePath];
  1520. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.get() );
  1521. ImGui::SetNextWindowSize(
  1522. ImVec2(400.0f, 400.0f)
  1523. , ImGuiCond_FirstUseEver
  1524. );
  1525. if (ImGui::Begin(temp, &view.m_files) )
  1526. {
  1527. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  1528. {
  1529. ImGui::PushFont(ImGui::Font::Mono);
  1530. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  1531. const float listHeight =
  1532. bx::max(1.0f, bx::floor(ImGui::GetWindowHeight()/itemHeight) )
  1533. * itemHeight
  1534. ;
  1535. ImGui::PushItemWidth(-1);
  1536. if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
  1537. {
  1538. const int32_t itemCount = int32_t(view.m_fileList.size() );
  1539. int32_t start, end;
  1540. ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);
  1541. const int32_t index = int32_t(view.m_fileIndex);
  1542. if (index <= start)
  1543. {
  1544. ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
  1545. }
  1546. else if (index >= end)
  1547. {
  1548. ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
  1549. }
  1550. ImGuiListClipper clipper(itemCount, itemHeight);
  1551. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  1552. {
  1553. ImGui::PushID(pos);
  1554. bool isSelected = uint32_t(pos) == view.m_fileIndex;
  1555. if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
  1556. {
  1557. view.m_fileIndex = pos;
  1558. }
  1559. ImGui::PopID();
  1560. }
  1561. clipper.End();
  1562. ImGui::ListBoxFooter();
  1563. }
  1564. ImGui::PopFont();
  1565. ImGui::EndChild();
  1566. }
  1567. }
  1568. ImGui::End();
  1569. }
  1570. if (ImGui::BeginPopupModal("About", &view.m_about, ImGuiWindowFlags_AlwaysAutoResize) )
  1571. {
  1572. ImGui::SetWindowFontScale(1.0f);
  1573. ImGui::Text(
  1574. "texturev, bgfx texture viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
  1575. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  1576. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  1577. , BGFX_TEXTUREV_VERSION_MAJOR
  1578. , BGFX_TEXTUREV_VERSION_MINOR
  1579. , BGFX_API_VERSION
  1580. );
  1581. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1582. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1583. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1584. || !view.m_about)
  1585. {
  1586. view.m_about = false;
  1587. ImGui::CloseCurrentPopup();
  1588. }
  1589. ImGui::EndPopup();
  1590. }
  1591. if (ImGui::BeginPopupModal("Help", &view.m_help, ImGuiWindowFlags_AlwaysAutoResize) )
  1592. {
  1593. ImGui::SetWindowFontScale(1.0f);
  1594. ImGui::Text("Key bindings:\n\n");
  1595. ImGui::PushFont(ImGui::Font::Mono);
  1596. keyBindingHelp("ESC", "Exit.");
  1597. keyBindingHelp("h", "Toggle help screen.");
  1598. keyBindingHelp("f", "Toggle full-screen.");
  1599. ImGui::NextLine();
  1600. keyBindingHelp("LMB+drag", "Pan.");
  1601. keyBindingHelp("=/- or MW", "Zoom in/out.");
  1602. keyBindingHelp("z/Z", "Rotate.");
  1603. keyBindingHelp("0", "Reset.");
  1604. keyBindingHelp("1", "Fit to window.");
  1605. ImGui::NextLine();
  1606. keyBindingHelp("<", "Reset MIP level.");
  1607. keyBindingHelp(",/,", "MIP level up/down.");
  1608. keyBindingHelp("/", "Toggle linear/point texture sampling.");
  1609. keyBindingHelp("[space]", "Change cubemap mode.");
  1610. ImGui::NextLine();
  1611. keyBindingHelp("left", "Previous layer in texture array.");
  1612. keyBindingHelp("right", "Next layer in texture array.");
  1613. ImGui::NextLine();
  1614. keyBindingHelp("up", "Previous texture.");
  1615. keyBindingHelp("down", "Next texture.");
  1616. ImGui::NextLine();
  1617. keyBindingHelp("r/g/b", "Toggle R, G, or B color channel.");
  1618. keyBindingHelp("a", "Toggle alpha blending.");
  1619. ImGui::NextLine();
  1620. keyBindingHelp("s", "Toggle Multi-channel SDF rendering");
  1621. ImGui::NextLine();
  1622. ImGui::PopFont();
  1623. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1624. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1625. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1626. || !view.m_help)
  1627. {
  1628. view.m_help = false;
  1629. ImGui::CloseCurrentPopup();
  1630. }
  1631. ImGui::EndPopup();
  1632. }
  1633. imguiEndFrame();
  1634. if ( (!bgfx::isValid(texture) || view.m_fileIndex != fileIndex)
  1635. && 0 != view.m_fileList.size() )
  1636. {
  1637. if (bgfx::isValid(texture) )
  1638. {
  1639. bgfx::destroy(texture);
  1640. }
  1641. fileIndex = view.m_fileIndex;
  1642. bx::FilePath fp = view.m_path;
  1643. fp.join(view.m_fileList[view.m_fileIndex].c_str() );
  1644. bimg::Orientation::Enum orientation;
  1645. texture = loadTexture(fp.get()
  1646. , 0
  1647. | BGFX_SAMPLER_U_CLAMP
  1648. | BGFX_SAMPLER_V_CLAMP
  1649. | BGFX_SAMPLER_W_CLAMP
  1650. , 0
  1651. , &view.m_textureInfo
  1652. , &orientation
  1653. );
  1654. bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(view.m_textureInfo.format);
  1655. if (format < bimg::TextureFormat::Count)
  1656. {
  1657. view.m_inLinear = bimg::isFloat(format);
  1658. switch (orientation)
  1659. {
  1660. default:
  1661. case bimg::Orientation::R0: cmdExec("view orientation\nview orientation z 0"); break;
  1662. case bimg::Orientation::R90: cmdExec("view orientation\nview orientation z -90"); break;
  1663. case bimg::Orientation::R180: cmdExec("view orientation\nview orientation z -180"); break;
  1664. case bimg::Orientation::R270: cmdExec("view orientation\nview orientation z -270"); break;
  1665. case bimg::Orientation::HFlip: cmdExec("view orientation\nview orientation x -180"); break;
  1666. case bimg::Orientation::HFlipR90: cmdExec("view orientation\nview orientation z -90\nview orientation x -180"); break;
  1667. case bimg::Orientation::HFlipR270: cmdExec("view orientation\nview orientation z -270\nview orientation x -180"); break;
  1668. case bimg::Orientation::VFlip: cmdExec("view orientation\nview orientation y -180"); break;
  1669. }
  1670. }
  1671. std::string title;
  1672. if (isValid(texture) )
  1673. {
  1674. const char* name = "";
  1675. if (view.m_textureInfo.cubeMap)
  1676. {
  1677. name = " CubeMap";
  1678. }
  1679. else if (1 < view.m_textureInfo.depth)
  1680. {
  1681. name = " 3D";
  1682. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1683. }
  1684. else if (1 < view.m_textureInfo.numLayers)
  1685. {
  1686. name = " 2D Array";
  1687. }
  1688. bx::stringPrintf(title, "%s (%d x %d%s, mips: %d, layers %d, %s)"
  1689. , fp.get()
  1690. , view.m_textureInfo.width
  1691. , view.m_textureInfo.height
  1692. , name
  1693. , view.m_textureInfo.numMips
  1694. , view.m_textureInfo.numLayers
  1695. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1696. );
  1697. }
  1698. else
  1699. {
  1700. bx::stringPrintf(title, "Failed to load %s!", filePath);
  1701. }
  1702. entry::WindowHandle handle = { 0 };
  1703. entry::setWindowTitle(handle, title.c_str() );
  1704. }
  1705. int64_t now = bx::getHPCounter();
  1706. static int64_t last = now;
  1707. const int64_t frameTime = now - last;
  1708. last = now;
  1709. const double freq = double(bx::getHPFrequency() );
  1710. time += (float)(frameTime*speed/freq);
  1711. float transitionTime = dragging ? 0.0f : 0.25f*view.m_transitionTime;
  1712. posx.set(view.m_posx, transitionTime);
  1713. posy.set(view.m_posy, transitionTime);
  1714. float ortho[16];
  1715. bx::mtxOrtho(
  1716. ortho
  1717. , 0.0f
  1718. , float(view.m_width)
  1719. , float(view.m_height)
  1720. , 0.0f
  1721. , 0.0f
  1722. , 1000.0f
  1723. , 0.0f
  1724. , caps->homogeneousDepth
  1725. );
  1726. bgfx::setViewTransform(BACKGROUND_VIEW_ID, NULL, ortho);
  1727. bgfx::setViewRect(BACKGROUND_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  1728. setGeometry(Geometry::Quad
  1729. , 0
  1730. , 0
  1731. , view.m_width
  1732. , view.m_height
  1733. , view.m_alpha || !bgfx::isValid(texture) ? UINT32_MAX : 0
  1734. , float(view.m_width )/float(checkerBoardSize)
  1735. , float(view.m_height)/float(checkerBoardSize)
  1736. );
  1737. bgfx::setTexture(0
  1738. , s_texColor
  1739. , checkerBoard
  1740. );
  1741. bgfx::setState(0
  1742. | BGFX_STATE_WRITE_RGB
  1743. | BGFX_STATE_WRITE_A
  1744. );
  1745. bgfx::submit(BACKGROUND_VIEW_ID
  1746. , textureProgram
  1747. );
  1748. float px = posx.getValue();
  1749. float py = posy.getValue();
  1750. bx::mtxOrtho(
  1751. ortho
  1752. , px-view.m_width/2.0f
  1753. , px+view.m_width/2.0f
  1754. , py+view.m_height/2.0f
  1755. , py-view.m_height/2.0f
  1756. , -10.0f
  1757. , 10.0f
  1758. , 0.0f
  1759. , caps->homogeneousDepth
  1760. );
  1761. bgfx::setViewTransform(IMAGE_VIEW_ID, NULL, ortho);
  1762. bgfx::setViewRect(IMAGE_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  1763. bgfx::dbgTextClear();
  1764. float orientation[16];
  1765. bx::mtxRotateXYZ(orientation, view.m_flipH, view.m_flipV, angle.getValue()+view.m_orientation);
  1766. if (view.m_fit)
  1767. {
  1768. const bx::Vec3 wh = { float(view.m_textureInfo.width), float(view.m_textureInfo.height), 0.0f };
  1769. const bx::Vec3 result = bx::round(bx::abs(bx::mul(wh, orientation) ) );
  1770. scale.set(bx::min(float(view.m_width) / result.x
  1771. , float(view.m_height) / result.y)
  1772. , 0.1f*view.m_transitionTime
  1773. );
  1774. }
  1775. else
  1776. {
  1777. scale.set(1.0f, 0.1f*view.m_transitionTime);
  1778. }
  1779. zoom.set(view.m_zoom, transitionTime);
  1780. angle.set(view.m_angle, transitionTime);
  1781. angx.set(view.m_angx, transitionTime);
  1782. angy.set(view.m_angy, transitionTime);
  1783. float ss = scale.getValue()
  1784. * zoom.getValue()
  1785. ;
  1786. setGeometry(view.m_textureInfo.cubeMap ? view.m_cubeMapGeo : Geometry::Quad
  1787. , -int(view.m_textureInfo.width * ss)/2
  1788. , -int(view.m_textureInfo.height * ss)/2
  1789. , int(view.m_textureInfo.width * ss)
  1790. , int(view.m_textureInfo.height * ss)
  1791. , view.m_abgr
  1792. );
  1793. bgfx::setTransform(orientation);
  1794. float mtx[16];
  1795. bx::mtxRotateXY(mtx, angx.getValue(), angy.getValue() );
  1796. bgfx::setUniform(u_mtx, mtx);
  1797. mip.set(float(view.m_mip), 0.5f*view.m_transitionTime);
  1798. layer.set(float(view.m_layer), 0.25f*view.m_transitionTime);
  1799. ev.set(view.m_ev, 0.5f*view.m_transitionTime);
  1800. float params[4] = { mip.getValue(), layer.getValue(), view.m_inLinear ? 1.0f : 0.0f, ev.getValue() };
  1801. if (1 < view.m_textureInfo.depth)
  1802. {
  1803. params[1] = layer.getValue()/float(bx::max(1, view.m_textureInfo.depth >> view.m_mip) );
  1804. }
  1805. bgfx::setUniform(u_params0, params);
  1806. float params1[4] = { float(view.m_outputFormat), 80.0f, 0.0, 0.0f };
  1807. bgfx::setUniform(u_params1, params1);
  1808. const uint32_t textureFlags = 0
  1809. | BGFX_SAMPLER_U_CLAMP
  1810. | BGFX_SAMPLER_V_CLAMP
  1811. | BGFX_SAMPLER_W_CLAMP
  1812. | (view.m_filter ? 0 : 0
  1813. | BGFX_SAMPLER_MIN_POINT
  1814. | BGFX_SAMPLER_MIP_POINT
  1815. | BGFX_SAMPLER_MAG_POINT
  1816. )
  1817. ;
  1818. bgfx::setTexture(0
  1819. , s_texColor
  1820. , texture
  1821. , textureFlags
  1822. );
  1823. bgfx::setState(0
  1824. | BGFX_STATE_WRITE_RGB
  1825. | BGFX_STATE_WRITE_A
  1826. | (view.m_alpha ? BGFX_STATE_BLEND_ALPHA : BGFX_STATE_NONE)
  1827. );
  1828. bgfx:: ProgramHandle program = textureProgram;
  1829. if (1 < view.m_textureInfo.depth)
  1830. {
  1831. program = texture3DProgram;
  1832. }
  1833. else if (view.m_textureInfo.cubeMap)
  1834. {
  1835. program = Geometry::Quad == view.m_cubeMapGeo
  1836. ? textureCubeProgram
  1837. : textureCube2Program
  1838. ;
  1839. }
  1840. else if (1 < view.m_textureInfo.numLayers)
  1841. {
  1842. program = textureArrayProgram;
  1843. }
  1844. else if (view.m_sdf)
  1845. {
  1846. if (8 < bimg::getBitsPerPixel(bimg::TextureFormat::Enum(view.m_textureInfo.format) ) )
  1847. {
  1848. program = textureMsdfProgram;
  1849. }
  1850. else
  1851. {
  1852. program = textureSdfProgram;
  1853. }
  1854. }
  1855. if (bgfx::isValid(texture) )
  1856. {
  1857. bgfx::submit(IMAGE_VIEW_ID, program);
  1858. }
  1859. else
  1860. {
  1861. bgfx::discard();
  1862. }
  1863. bgfx::frame();
  1864. // Slow down when nothing is animating...
  1865. if (!dragging
  1866. && !anyActive() )
  1867. {
  1868. bx::sleep(100);
  1869. }
  1870. }
  1871. }
  1872. if (bgfx::isValid(texture) )
  1873. {
  1874. bgfx::destroy(texture);
  1875. }
  1876. bgfx::destroy(checkerBoard);
  1877. bgfx::destroy(s_texColor);
  1878. bgfx::destroy(u_mtx);
  1879. bgfx::destroy(u_params0);
  1880. bgfx::destroy(u_params1);
  1881. bgfx::destroy(textureProgram);
  1882. bgfx::destroy(textureArrayProgram);
  1883. bgfx::destroy(textureCubeProgram);
  1884. bgfx::destroy(textureCube2Program);
  1885. bgfx::destroy(textureSdfProgram);
  1886. bgfx::destroy(textureMsdfProgram);
  1887. bgfx::destroy(texture3DProgram);
  1888. imguiDestroy();
  1889. bgfx::shutdown();
  1890. return exitcode;
  1891. }