texturev.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. /*
  2. * Copyright 2011-2017 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/os.h>
  9. #include <bx/filepath.h>
  10. #include <bx/uint32_t.h>
  11. #include <bx/math.h>
  12. #include <bx/easing.h>
  13. #include <entry/entry.h>
  14. #include <entry/input.h>
  15. #include <entry/cmd.h>
  16. #include <imgui/imgui.h>
  17. #include <bgfx_utils.h>
  18. #include <dirent.h>
  19. #include <bx/file.h>
  20. #include <bx/process.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_3d.bin.h"
  36. #define BACKGROUND_VIEW_ID 0
  37. #define IMAGE_VIEW_ID 1
  38. #define BGFX_TEXTUREV_VERSION_MAJOR 1
  39. #define BGFX_TEXTUREV_VERSION_MINOR 0
  40. const float kEvMin = -10.0f;
  41. const float kEvMax = 20.0f;
  42. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  43. {
  44. BGFX_EMBEDDED_SHADER(vs_texture),
  45. BGFX_EMBEDDED_SHADER(fs_texture),
  46. BGFX_EMBEDDED_SHADER(fs_texture_array),
  47. BGFX_EMBEDDED_SHADER(vs_texture_cube),
  48. BGFX_EMBEDDED_SHADER(fs_texture_cube),
  49. BGFX_EMBEDDED_SHADER(fs_texture_cube2),
  50. BGFX_EMBEDDED_SHADER(fs_texture_sdf),
  51. BGFX_EMBEDDED_SHADER(fs_texture_3d),
  52. BGFX_EMBEDDED_SHADER_END()
  53. };
  54. static const char* s_supportedExt[] =
  55. {
  56. "bmp",
  57. "dds",
  58. "exr",
  59. "gif",
  60. "jpg",
  61. "jpeg",
  62. "hdr",
  63. "ktx",
  64. "pgm",
  65. "png",
  66. "ppm",
  67. "psd",
  68. "pvr",
  69. "tga",
  70. };
  71. struct Binding
  72. {
  73. enum Enum
  74. {
  75. App,
  76. View,
  77. Help,
  78. Count
  79. };
  80. };
  81. struct Geometry
  82. {
  83. enum Enum
  84. {
  85. Quad,
  86. Cross,
  87. Hexagon,
  88. Count
  89. };
  90. };
  91. static const InputBinding s_bindingApp[] =
  92. {
  93. { entry::Key::KeyQ, entry::Modifier::None, 1, NULL, "exit" },
  94. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  95. INPUT_BINDING_END
  96. };
  97. const char* s_resetCmd =
  98. "view zoom 1.0\n"
  99. "view rotate 0\n"
  100. "view cubemap\n"
  101. "view pan\n"
  102. "view ev\n"
  103. ;
  104. static const InputBinding s_bindingView[] =
  105. {
  106. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  107. { entry::Key::Comma, entry::Modifier::None, 1, NULL, "view mip prev" },
  108. { entry::Key::Period, entry::Modifier::None, 1, NULL, "view mip next" },
  109. { entry::Key::Comma, entry::Modifier::LeftShift, 1, NULL, "view mip" },
  110. { entry::Key::Comma, entry::Modifier::RightShift, 1, NULL, "view mip" },
  111. { entry::Key::Slash, entry::Modifier::None, 1, NULL, "view filter" },
  112. { entry::Key::Key1, entry::Modifier::None, 1, NULL, "view zoom 1.0\n"
  113. "view fit\n" },
  114. { entry::Key::Key0, entry::Modifier::None, 1, NULL, s_resetCmd },
  115. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view zoom +0.1" },
  116. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view zoom -0.1" },
  117. { entry::Key::KeyZ, entry::Modifier::None, 1, NULL, "view rotate -90" },
  118. { entry::Key::KeyZ, entry::Modifier::LeftShift, 1, NULL, "view rotate +90" },
  119. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view pan\n"
  120. "view file-up" },
  121. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view pan\n"
  122. "view file-down" },
  123. { entry::Key::PageUp, entry::Modifier::None, 1, NULL, "view pan\n"
  124. "view file-pgup" },
  125. { entry::Key::PageDown, entry::Modifier::None, 1, NULL, "view pan\n"
  126. "view file-pgdown" },
  127. { entry::Key::Left, entry::Modifier::None, 1, NULL, "view layer prev" },
  128. { entry::Key::Right, entry::Modifier::None, 1, NULL, "view layer next" },
  129. { entry::Key::KeyR, entry::Modifier::None, 1, NULL, "view rgb r" },
  130. { entry::Key::KeyG, entry::Modifier::None, 1, NULL, "view rgb g" },
  131. { entry::Key::KeyB, entry::Modifier::None, 1, NULL, "view rgb b" },
  132. { entry::Key::KeyA, entry::Modifier::None, 1, NULL, "view rgb a" },
  133. { entry::Key::KeyI, entry::Modifier::None, 1, NULL, "view info" },
  134. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  135. { entry::Key::Return, entry::Modifier::None, 1, NULL, "view files" },
  136. { entry::Key::KeyS, entry::Modifier::None, 1, NULL, "view sdf" },
  137. { entry::Key::Space, entry::Modifier::None, 1, NULL, "view geo\n"
  138. "view pan\n" },
  139. INPUT_BINDING_END
  140. };
  141. static const InputBinding s_bindingHelp[] =
  142. {
  143. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "view help" },
  144. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  145. INPUT_BINDING_END
  146. };
  147. static const char* s_bindingName[] =
  148. {
  149. "App",
  150. "View",
  151. "Help",
  152. };
  153. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  154. static const InputBinding* s_binding[] =
  155. {
  156. s_bindingApp,
  157. s_bindingView,
  158. s_bindingHelp,
  159. };
  160. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  161. struct View
  162. {
  163. View()
  164. : m_cubeMapGeo(Geometry::Quad)
  165. , m_fileIndex(0)
  166. , m_scaleFn(0)
  167. , m_mip(0)
  168. , m_layer(0)
  169. , m_abgr(UINT32_MAX)
  170. , m_ev(0.0f)
  171. , m_evMin(kEvMin)
  172. , m_evMax(kEvMax)
  173. , m_posx(0.0f)
  174. , m_posy(0.0f)
  175. , m_angx(0.0f)
  176. , m_angy(0.0f)
  177. , m_zoom(1.0f)
  178. , m_angle(0.0f)
  179. , m_orientation(0.0f)
  180. , m_flipH(0.0f)
  181. , m_flipV(0.0f)
  182. , m_transitionTime(1.0f)
  183. , m_filter(true)
  184. , m_fit(true)
  185. , m_alpha(false)
  186. , m_help(false)
  187. , m_info(false)
  188. , m_files(false)
  189. , m_sdf(false)
  190. {
  191. }
  192. ~View()
  193. {
  194. }
  195. int32_t cmd(int32_t _argc, char const* const* _argv)
  196. {
  197. if (_argc >= 2)
  198. {
  199. if (0 == bx::strCmp(_argv[1], "mip") )
  200. {
  201. if (_argc >= 3)
  202. {
  203. uint32_t mip = m_mip;
  204. if (0 == bx::strCmp(_argv[2], "next") )
  205. {
  206. ++mip;
  207. }
  208. else if (0 == bx::strCmp(_argv[2], "prev") )
  209. {
  210. --mip;
  211. }
  212. else if (0 == bx::strCmp(_argv[2], "last") )
  213. {
  214. mip = INT32_MAX;
  215. }
  216. else
  217. {
  218. bx::fromString(&mip, _argv[2]);
  219. }
  220. m_mip = bx::uint32_iclamp(mip, 0, m_textureInfo.numMips-1);
  221. }
  222. else
  223. {
  224. m_mip = 0;
  225. }
  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. 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::fclamp(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::fclamp(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::fwrap(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[3]);
  452. m_transitionTime = bx::fclamp(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. m_filter = bx::toBool(_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. m_fit = bx::toBool(_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], "help") )
  545. {
  546. m_help ^= true;
  547. }
  548. else if (0 == bx::strCmp(_argv[1], "info") )
  549. {
  550. m_info ^= true;
  551. }
  552. else if (0 == bx::strCmp(_argv[1], "files") )
  553. {
  554. m_files ^= true;
  555. }
  556. }
  557. return 0;
  558. }
  559. static bool sortNameAscending(const std::string& _lhs, const std::string& _rhs)
  560. {
  561. return 0 > bx::strCmpV(_lhs.c_str(), _rhs.c_str() );
  562. }
  563. void updateFileList(const bx::FilePath& _filePath)
  564. {
  565. DIR* dir = opendir(_filePath.get() );
  566. if (NULL == dir)
  567. {
  568. m_path = _filePath.getPath();
  569. dir = opendir(m_path.get() );
  570. }
  571. else
  572. {
  573. m_path = _filePath;
  574. }
  575. if (NULL != dir)
  576. {
  577. for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
  578. {
  579. if (0 == (item->d_type & DT_DIR) )
  580. {
  581. const char* ext = bx::strRFind(item->d_name, '.');
  582. if (NULL != ext)
  583. {
  584. ext += 1;
  585. bool supported = false;
  586. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  587. {
  588. if (0 == bx::strCmpI(ext, s_supportedExt[ii]) )
  589. {
  590. supported = true;
  591. break;
  592. }
  593. }
  594. if (supported)
  595. {
  596. m_fileList.push_back(item->d_name);
  597. }
  598. }
  599. }
  600. }
  601. std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);
  602. m_fileIndex = 0;
  603. uint32_t idx = 0;
  604. for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
  605. {
  606. if (0 == bx::strCmpI(it->c_str(), _filePath.getFileName() ) )
  607. {
  608. // If it is case-insensitive match then might be correct one, but keep
  609. // searching.
  610. m_fileIndex = idx;
  611. if (0 == bx::strCmp(it->c_str(), _filePath.getFileName() ) )
  612. {
  613. // If it is exact match we're done.
  614. break;
  615. }
  616. }
  617. }
  618. closedir(dir);
  619. }
  620. }
  621. bx::FilePath m_path;
  622. typedef stl::vector<std::string> FileList;
  623. FileList m_fileList;
  624. bgfx::TextureInfo m_textureInfo;
  625. Geometry::Enum m_cubeMapGeo;
  626. uint32_t m_fileIndex;
  627. uint32_t m_scaleFn;
  628. uint32_t m_mip;
  629. uint32_t m_layer;
  630. uint32_t m_abgr;
  631. float m_ev;
  632. float m_evMin;
  633. float m_evMax;
  634. float m_posx;
  635. float m_posy;
  636. float m_angx;
  637. float m_angy;
  638. float m_zoom;
  639. float m_angle;
  640. float m_orientation;
  641. float m_flipH;
  642. float m_flipV;
  643. float m_transitionTime;
  644. bool m_filter;
  645. bool m_fit;
  646. bool m_alpha;
  647. bool m_help;
  648. bool m_info;
  649. bool m_files;
  650. bool m_sdf;
  651. };
  652. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  653. {
  654. View* view = static_cast<View*>(_userData);
  655. return view->cmd(_argc, _argv);
  656. }
  657. struct PosUvwColorVertex
  658. {
  659. float m_x;
  660. float m_y;
  661. float m_u;
  662. float m_v;
  663. float m_w;
  664. uint32_t m_abgr;
  665. static void init()
  666. {
  667. ms_decl
  668. .begin()
  669. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  670. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  671. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  672. .end();
  673. }
  674. void set(float _x, float _y, float _u, float _v, float _w, uint32_t _abgr)
  675. {
  676. m_x = _x;
  677. m_y = _y;
  678. m_u = _u;
  679. m_v = _v;
  680. m_w = _w;
  681. m_abgr = _abgr;
  682. }
  683. static bgfx::VertexDecl ms_decl;
  684. };
  685. bgfx::VertexDecl PosUvwColorVertex::ms_decl;
  686. static uint32_t addQuad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
  687. {
  688. _indices[0] = _idx0;
  689. _indices[1] = _idx3;
  690. _indices[2] = _idx1;
  691. _indices[3] = _idx1;
  692. _indices[4] = _idx3;
  693. _indices[5] = _idx2;
  694. return 6;
  695. }
  696. void setGeometry(
  697. Geometry::Enum _type
  698. , int32_t _x
  699. , int32_t _y
  700. , int32_t _width
  701. , uint32_t _height
  702. , uint32_t _abgr
  703. , float _maxu = 1.0f
  704. , float _maxv = 1.0f
  705. )
  706. {
  707. if (Geometry::Quad == _type)
  708. {
  709. if (6 == bgfx::getAvailTransientVertexBuffer(6, PosUvwColorVertex::ms_decl) )
  710. {
  711. bgfx::TransientVertexBuffer vb;
  712. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvwColorVertex::ms_decl);
  713. PosUvwColorVertex* vertex = (PosUvwColorVertex*)vb.data;
  714. const float widthf = float(_width);
  715. const float heightf = float(_height);
  716. const float minx = float(_x);
  717. const float miny = float(_y);
  718. const float maxx = minx+widthf;
  719. const float maxy = miny+heightf;
  720. const float minu = 0.0f;
  721. const float maxu = _maxu;
  722. const float minv = 0.0f;
  723. const float maxv = _maxv;
  724. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  725. vertex->set(maxx, miny, maxu, minv, 0.0f, _abgr); ++vertex;
  726. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  727. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  728. vertex->set(minx, maxy, minu, maxv, 0.0f, _abgr); ++vertex;
  729. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  730. bgfx::setVertexBuffer(0, &vb);
  731. }
  732. }
  733. else
  734. {
  735. const uint32_t numVertices = 14;
  736. const uint32_t numIndices = 36;
  737. if (checkAvailTransientBuffers(numVertices, PosUvwColorVertex::ms_decl, numIndices) )
  738. {
  739. bgfx::TransientVertexBuffer tvb;
  740. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosUvwColorVertex::ms_decl);
  741. bgfx::TransientIndexBuffer tib;
  742. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  743. PosUvwColorVertex* vertex = (PosUvwColorVertex*)tvb.data;
  744. uint16_t* indices = (uint16_t*)tib.data;
  745. if (Geometry::Cross == _type)
  746. {
  747. const float sx = _width /1.5f;
  748. const float sy = _height/1.5f;
  749. const float px = float(_x)-sx/4.0f;
  750. const float py = float(_y);
  751. vertex->set(0.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  752. vertex->set(0.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  753. vertex->set(0.5f*sx+px, 0.0f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  754. vertex->set(0.5f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  755. vertex->set(0.5f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  756. vertex->set(0.5f*sx+px, 1.5f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  757. vertex->set(1.0f*sx+px, 0.0f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  758. vertex->set(1.0f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  759. vertex->set(1.0f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  760. vertex->set(1.0f*sx+px, 1.5f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  761. vertex->set(1.5f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  762. vertex->set(1.5f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  763. vertex->set(2.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  764. vertex->set(2.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  765. indices += addQuad(indices, 0, 3, 4, 1);
  766. indices += addQuad(indices, 2, 6, 7, 3);
  767. indices += addQuad(indices, 3, 7, 8, 4);
  768. indices += addQuad(indices, 4, 8, 9, 5);
  769. indices += addQuad(indices, 7, 10, 11, 8);
  770. indices += addQuad(indices, 10, 12, 13, 11);
  771. }
  772. else
  773. {
  774. const float sx = float(_width);
  775. const float sy = float(_height);
  776. const float px = float(_x) - sx/2.0f;
  777. const float py = float(_y);
  778. vertex->set(0.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  779. vertex->set(0.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  780. vertex->set(0.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  781. vertex->set(0.5f*sx+px, 0.50f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  782. vertex->set(0.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  783. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  784. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  785. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  786. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  787. vertex->set(1.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  788. vertex->set(1.5f*sx+px, 0.50f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  789. vertex->set(1.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  790. vertex->set(2.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  791. vertex->set(2.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  792. indices += addQuad(indices, 0, 2, 3, 1);
  793. indices += addQuad(indices, 1, 3, 6, 4);
  794. indices += addQuad(indices, 2, 5, 6, 3);
  795. indices += addQuad(indices, 7, 9, 12, 10);
  796. indices += addQuad(indices, 7, 10, 11, 8);
  797. indices += addQuad(indices, 10, 12, 13, 11);
  798. }
  799. bgfx::setVertexBuffer(0, &tvb);
  800. bgfx::setIndexBuffer(&tib);
  801. }
  802. }
  803. }
  804. template<bx::LerpFn lerpT, bx::EaseFn easeT>
  805. struct InterpolatorT
  806. {
  807. float from;
  808. float to;
  809. float duration;
  810. int64_t offset;
  811. InterpolatorT(float _value)
  812. {
  813. reset(_value);
  814. }
  815. void reset(float _value)
  816. {
  817. from = _value;
  818. to = _value;
  819. duration = 0.0;
  820. offset = bx::getHPCounter();
  821. }
  822. void set(float _value, float _duration)
  823. {
  824. if (_value != to)
  825. {
  826. from = getValue();
  827. to = _value;
  828. duration = _duration;
  829. offset = bx::getHPCounter();
  830. }
  831. }
  832. float getValue()
  833. {
  834. if (duration > 0.0)
  835. {
  836. const double freq = double(bx::getHPFrequency() );
  837. int64_t now = bx::getHPCounter();
  838. float time = (float)(double(now - offset) / freq);
  839. float lerp = bx::fclamp(time, 0.0, duration) / duration;
  840. return lerpT(from, to, easeT(lerp) );
  841. }
  842. return to;
  843. }
  844. };
  845. typedef InterpolatorT<bx::flerp, bx::easeInOutQuad> Interpolator;
  846. typedef InterpolatorT<bx::angleLerp, bx::easeInOutCubic> InterpolatorAngle;
  847. typedef InterpolatorT<bx::flerp, bx::easeLinear> InterpolatorLinear;
  848. void keyBindingHelp(const char* _bindings, const char* _description)
  849. {
  850. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
  851. ImGui::SameLine(100);
  852. ImGui::Text(_description);
  853. }
  854. void associate()
  855. {
  856. #if BX_PLATFORM_WINDOWS
  857. std::string str;
  858. char exec[MAX_PATH];
  859. GetModuleFileNameA(GetModuleHandleA(NULL), exec, MAX_PATH);
  860. std::string strExec = bx::replaceAll<std::string>(exec, "\\", "\\\\");
  861. std::string value;
  862. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  863. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  864. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  865. str += value;
  866. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  867. str += value;
  868. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  869. {
  870. const char* ext = s_supportedExt[ii];
  871. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  872. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  873. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  874. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  875. }
  876. bx::FilePath filePath(bx::TempDir::Tag);
  877. filePath.join("texture.reg");
  878. bx::FileWriter writer;
  879. bx::Error err;
  880. if (bx::open(&writer, filePath, false, &err) )
  881. {
  882. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  883. bx::close(&writer);
  884. if (err.isOk() )
  885. {
  886. std::string cmd;
  887. bx::stringPrintf(cmd, "/s %s", filePath.get() );
  888. bx::ProcessReader reader;
  889. if (bx::open(&reader, "regedit.exe", cmd.c_str(), &err) )
  890. {
  891. bx::close(&reader);
  892. }
  893. }
  894. }
  895. #elif BX_PLATFORM_LINUX
  896. std::string str;
  897. str += "#/bin/bash\n\n";
  898. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  899. {
  900. const char* ext = s_supportedExt[ii];
  901. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  902. }
  903. str += "\n";
  904. bx::FileWriter writer;
  905. bx::Error err;
  906. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  907. {
  908. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  909. bx::close(&writer);
  910. if (err.isOk() )
  911. {
  912. bx::ProcessReader reader;
  913. if (bx::open(&reader, "/bin/bash", "/tmp/texturev.sh", &err) )
  914. {
  915. bx::close(&reader);
  916. }
  917. }
  918. }
  919. #endif // BX_PLATFORM_WINDOWS
  920. }
  921. void help(const char* _error = NULL)
  922. {
  923. if (NULL != _error)
  924. {
  925. fprintf(stderr, "Error:\n%s\n\n", _error);
  926. }
  927. fprintf(stderr
  928. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  929. "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
  930. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  931. , BGFX_TEXTUREV_VERSION_MAJOR
  932. , BGFX_TEXTUREV_VERSION_MINOR
  933. , BGFX_API_VERSION
  934. );
  935. fprintf(stderr
  936. , "Usage: texturev <file path>\n"
  937. "\n"
  938. "Supported input file types:\n"
  939. );
  940. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  941. {
  942. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  943. }
  944. fprintf(stderr
  945. , "\n"
  946. "Options:\n"
  947. " -h, --help Help.\n"
  948. " -v, --version Version information only.\n"
  949. " --associate Associate file extensions with texturev.\n"
  950. "\n"
  951. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  952. );
  953. }
  954. int _main_(int _argc, char** _argv)
  955. {
  956. bx::CommandLine cmdLine(_argc, _argv);
  957. if (cmdLine.hasArg('v', "version") )
  958. {
  959. fprintf(stderr
  960. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  961. , BGFX_TEXTUREV_VERSION_MAJOR
  962. , BGFX_TEXTUREV_VERSION_MINOR
  963. , BGFX_API_VERSION
  964. );
  965. return bx::kExitSuccess;
  966. }
  967. if (cmdLine.hasArg('h', "help") )
  968. {
  969. help();
  970. return bx::kExitFailure;
  971. }
  972. else if (cmdLine.hasArg("associate") )
  973. {
  974. associate();
  975. return bx::kExitFailure;
  976. }
  977. uint32_t width = 1280;
  978. uint32_t height = 720;
  979. uint32_t debug = BGFX_DEBUG_TEXT;
  980. uint32_t reset = BGFX_RESET_VSYNC;
  981. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  982. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  983. View view;
  984. cmdAdd("view", cmdView, &view);
  985. bgfx::init();
  986. bgfx::reset(width, height, reset);
  987. // Set view 0 clear state.
  988. bgfx::setViewClear(BACKGROUND_VIEW_ID
  989. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  990. , 0x000000ff
  991. , 1.0f
  992. , 0
  993. );
  994. imguiCreate();
  995. PosUvwColorVertex::init();
  996. const bgfx::Caps* caps = bgfx::getCaps();
  997. bgfx::RendererType::Enum type = caps->rendererType;
  998. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  999. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  1000. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  1001. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  1002. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  1003. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  1004. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  1005. vsTexture
  1006. , fsTexture
  1007. , true
  1008. );
  1009. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  1010. vsTexture
  1011. , bgfx::isValid(fsTextureArray)
  1012. ? fsTextureArray
  1013. : fsTexture
  1014. , true
  1015. );
  1016. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  1017. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  1018. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  1019. , true
  1020. );
  1021. bgfx::ProgramHandle textureCube2Program = bgfx::createProgram(
  1022. vsTexture
  1023. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube2")
  1024. , true
  1025. );
  1026. bgfx::ProgramHandle textureSdfProgram = bgfx::createProgram(
  1027. vsTexture
  1028. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_sdf")
  1029. , true
  1030. );
  1031. bgfx::ProgramHandle texture3DProgram = bgfx::createProgram(
  1032. vsTexture
  1033. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_3d")
  1034. , true
  1035. );
  1036. const uint32_t checkerBoardSize = 64;
  1037. bgfx::TextureHandle checkerBoard;
  1038. {
  1039. const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize*checkerBoardSize*4);
  1040. bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xff8e8e8e, 0xff5d5d5d);
  1041. checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
  1042. , bgfx::TextureFormat::BGRA8
  1043. , 0
  1044. | BGFX_TEXTURE_MIN_POINT
  1045. | BGFX_TEXTURE_MIP_POINT
  1046. | BGFX_TEXTURE_MAG_POINT
  1047. , mem
  1048. );
  1049. }
  1050. float speed = 0.37f;
  1051. float time = 0.0f;
  1052. Interpolator mip(0.0f);
  1053. Interpolator layer(0.0f);
  1054. InterpolatorLinear ev(0.0f);
  1055. Interpolator zoom(1.0f);
  1056. Interpolator scale(1.0f);
  1057. Interpolator posx(0.0f);
  1058. Interpolator posy(0.0f);
  1059. InterpolatorAngle angle(0.0f);
  1060. InterpolatorAngle angx(0.0f);
  1061. InterpolatorAngle angy(0.0f);
  1062. const char* filePath = _argc < 2 ? "" : _argv[1];
  1063. std::string path = filePath;
  1064. {
  1065. bx::FilePath fp(filePath);
  1066. view.updateFileList(fp);
  1067. }
  1068. int exitcode = bx::kExitSuccess;
  1069. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  1070. if (view.m_fileList.empty() )
  1071. {
  1072. exitcode = bx::kExitFailure;
  1073. if (2 > _argc)
  1074. {
  1075. help("File path is not specified.");
  1076. }
  1077. else
  1078. {
  1079. fprintf(stderr, "Unable to load '%s' texture.\n", filePath);
  1080. }
  1081. }
  1082. else
  1083. {
  1084. uint32_t fileIndex = 0;
  1085. bool dragging = false;
  1086. entry::MouseState mouseStatePrev;
  1087. entry::MouseState mouseState;
  1088. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  1089. {
  1090. imguiBeginFrame(mouseState.m_mx
  1091. , mouseState.m_my
  1092. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1093. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1094. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1095. , mouseState.m_mz
  1096. , uint16_t(width)
  1097. , uint16_t(height)
  1098. );
  1099. static bool help = false;
  1100. static bool mouseDelta = false;
  1101. if (!mouseDelta)
  1102. {
  1103. mouseStatePrev = mouseState;
  1104. mouseDelta = true;
  1105. }
  1106. int32_t zoomDelta = mouseState.m_mz - mouseStatePrev.m_mz;
  1107. if (zoomDelta != 0)
  1108. {
  1109. char exec[64];
  1110. bx::snprintf(exec, BX_COUNTOF(exec), "view zoom %+f", -zoomDelta*0.1f);
  1111. cmdExec(exec);
  1112. }
  1113. const float xDelta = float(mouseStatePrev.m_mx - mouseState.m_mx);
  1114. const float yDelta = float(mouseStatePrev.m_my - mouseState.m_my);
  1115. if (!ImGui::MouseOverArea()
  1116. && !help
  1117. && mouseState.m_buttons[entry::MouseButton::Left] != mouseStatePrev.m_buttons[entry::MouseButton::Left])
  1118. {
  1119. dragging = !!mouseState.m_buttons[entry::MouseButton::Left];
  1120. }
  1121. if (dragging)
  1122. {
  1123. if (view.m_textureInfo.cubeMap
  1124. && Geometry::Quad == view.m_cubeMapGeo)
  1125. {
  1126. char exec[64];
  1127. bx::snprintf(exec, BX_COUNTOF(exec), "view cubemap %+f %+f", -yDelta, -xDelta);
  1128. cmdExec(exec);
  1129. }
  1130. else
  1131. {
  1132. char exec[64];
  1133. bx::snprintf(exec, BX_COUNTOF(exec), "view pan %+f %+f", xDelta, yDelta);
  1134. cmdExec(exec);
  1135. }
  1136. }
  1137. mouseStatePrev = mouseState;
  1138. if (ImGui::BeginPopupContextVoid("Menu") )
  1139. {
  1140. if (ImGui::MenuItem("Files", NULL, view.m_files) )
  1141. {
  1142. cmdExec("view files");
  1143. }
  1144. if (ImGui::MenuItem("Info", NULL, view.m_info) )
  1145. {
  1146. cmdExec("view info");
  1147. }
  1148. // if (ImGui::MenuItem("Save As") )
  1149. {
  1150. }
  1151. if (ImGui::MenuItem("Reset") )
  1152. {
  1153. cmdExec(s_resetCmd);
  1154. }
  1155. ImGui::Separator();
  1156. if (ImGui::BeginMenu("Options"))
  1157. {
  1158. bool filter = view.m_filter;
  1159. if (ImGui::MenuItem("Filter", NULL, &filter) )
  1160. {
  1161. cmdExec("view filter");
  1162. }
  1163. bool animate = 0.0f < view.m_transitionTime;
  1164. if (ImGui::MenuItem("Animate", NULL, &animate) )
  1165. {
  1166. cmdExec("view transition %f", animate ? 1.0f : 0.0f);
  1167. }
  1168. if (ImGui::BeginMenu("Cubemap", view.m_textureInfo.cubeMap) )
  1169. {
  1170. if (ImGui::MenuItem("Quad", NULL, Geometry::Quad == view.m_cubeMapGeo) )
  1171. {
  1172. cmdExec("view geo quad");
  1173. }
  1174. if (ImGui::MenuItem("Cross", NULL, Geometry::Cross == view.m_cubeMapGeo) )
  1175. {
  1176. cmdExec("view geo cross");
  1177. }
  1178. if (ImGui::MenuItem("Hexagon", NULL, Geometry::Hexagon == view.m_cubeMapGeo) )
  1179. {
  1180. cmdExec("view geo hexagon");
  1181. }
  1182. ImGui::EndMenu();
  1183. }
  1184. bool rr = 0 != (view.m_abgr & 0x000000ff);
  1185. if (ImGui::MenuItem("R", NULL, &rr) )
  1186. {
  1187. cmdExec("view rgb r");
  1188. }
  1189. bool gg = 0 != (view.m_abgr & 0x0000ff00);
  1190. if (ImGui::MenuItem("G", NULL, &gg) )
  1191. {
  1192. cmdExec("view rgb g");
  1193. }
  1194. bool bb = 0 != (view.m_abgr & 0x00ff0000);
  1195. if (ImGui::MenuItem("B", NULL, &bb) )
  1196. {
  1197. cmdExec("view rgb b");
  1198. }
  1199. bool alpha = view.m_alpha;
  1200. if (ImGui::MenuItem("Checkerboard", NULL, &alpha) )
  1201. {
  1202. cmdExec("view rgb a");
  1203. }
  1204. ImGui::EndMenu();
  1205. }
  1206. ImGui::Separator();
  1207. if (ImGui::MenuItem("Help") )
  1208. {
  1209. cmdExec("view help");
  1210. }
  1211. ImGui::Separator();
  1212. if (ImGui::MenuItem("Exit") )
  1213. {
  1214. cmdExec("exit");
  1215. }
  1216. ImGui::EndPopup();
  1217. }
  1218. if (help != view.m_help)
  1219. {
  1220. if (!help)
  1221. {
  1222. ImGui::OpenPopup("Help");
  1223. inputRemoveBindings(s_bindingName[Binding::View]);
  1224. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  1225. }
  1226. else
  1227. {
  1228. inputRemoveBindings(s_bindingName[Binding::Help]);
  1229. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1230. }
  1231. help = view.m_help;
  1232. }
  1233. if (view.m_info)
  1234. {
  1235. if (ImGui::Begin("Info", NULL, ImVec2(300.0f, 200.0f) ) )
  1236. {
  1237. if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
  1238. {
  1239. ImGui::Text("Dimensions: %d x %d"
  1240. , view.m_textureInfo.width
  1241. , view.m_textureInfo.height
  1242. );
  1243. ImGui::Text("Format: %s"
  1244. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1245. );
  1246. ImGui::Text("Layers: %d / %d"
  1247. , view.m_layer
  1248. , view.m_textureInfo.numLayers - 1
  1249. );
  1250. ImGui::Text("Mips: %d / %d"
  1251. , view.m_mip
  1252. , view.m_textureInfo.numMips - 1
  1253. );
  1254. ImGui::RangeSliderFloat("EV range", &view.m_evMin, &view.m_evMax, kEvMin, kEvMax);
  1255. ImGui::SliderFloat("EV", &view.m_ev, view.m_evMin, view.m_evMax);
  1256. ImGui::EndChild();
  1257. }
  1258. ImGui::End();
  1259. }
  1260. }
  1261. if (view.m_files)
  1262. {
  1263. char temp[bx::kMaxFilePath];
  1264. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.get() );
  1265. if (ImGui::Begin(temp, NULL, ImVec2(400.0f, 400.0f) ) )
  1266. {
  1267. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  1268. {
  1269. ImGui::PushFont(ImGui::Font::Mono);
  1270. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  1271. const float listHeight =
  1272. std::max(1.0f, bx::ffloor(ImGui::GetWindowHeight()/itemHeight) )
  1273. * itemHeight
  1274. ;
  1275. ImGui::PushItemWidth(-1);
  1276. if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
  1277. {
  1278. const int32_t itemCount = int32_t(view.m_fileList.size() );
  1279. int32_t start, end;
  1280. ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);
  1281. const int32_t index = int32_t(view.m_fileIndex);
  1282. if (index <= start)
  1283. {
  1284. ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
  1285. }
  1286. else if (index >= end)
  1287. {
  1288. ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
  1289. }
  1290. ImGuiListClipper clipper(itemCount, itemHeight);
  1291. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  1292. {
  1293. ImGui::PushID(pos);
  1294. bool isSelected = uint32_t(pos) == view.m_fileIndex;
  1295. if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
  1296. {
  1297. view.m_fileIndex = pos;
  1298. }
  1299. ImGui::PopID();
  1300. }
  1301. clipper.End();
  1302. ImGui::ListBoxFooter();
  1303. }
  1304. ImGui::PopFont();
  1305. ImGui::EndChild();
  1306. }
  1307. ImGui::End();
  1308. }
  1309. }
  1310. if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize) )
  1311. {
  1312. ImGui::SetWindowFontScale(1.0f);
  1313. ImGui::Text(
  1314. "texturev, bgfx texture viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
  1315. "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
  1316. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  1317. , BGFX_TEXTUREV_VERSION_MAJOR
  1318. , BGFX_TEXTUREV_VERSION_MINOR
  1319. , BGFX_API_VERSION
  1320. );
  1321. ImGui::Separator();
  1322. ImGui::NextLine();
  1323. ImGui::Text("Key bindings:\n\n");
  1324. ImGui::PushFont(ImGui::Font::Mono);
  1325. keyBindingHelp("ESC", "Exit.");
  1326. keyBindingHelp("h", "Toggle help screen.");
  1327. keyBindingHelp("f", "Toggle full-screen.");
  1328. ImGui::NextLine();
  1329. keyBindingHelp("LMB+drag", "Pan.");
  1330. keyBindingHelp("=/- or MW", "Zoom in/out.");
  1331. keyBindingHelp("z/Z", "Rotate.");
  1332. keyBindingHelp("0", "Reset.");
  1333. keyBindingHelp("1", "Fit to window.");
  1334. ImGui::NextLine();
  1335. keyBindingHelp("<", "Reset MIP level.");
  1336. keyBindingHelp(",/,", "MIP level up/down.");
  1337. keyBindingHelp("/", "Toggle linear/point texture sampling.");
  1338. keyBindingHelp("[space]", "Change cubemap mode.");
  1339. ImGui::NextLine();
  1340. keyBindingHelp("left", "Previous layer in texture array.");
  1341. keyBindingHelp("right", "Next layer in texture array.");
  1342. ImGui::NextLine();
  1343. keyBindingHelp("up", "Previous texture.");
  1344. keyBindingHelp("down", "Next texture.");
  1345. ImGui::NextLine();
  1346. keyBindingHelp("r/g/b", "Toggle R, G, or B color channel.");
  1347. keyBindingHelp("a", "Toggle alpha blending.");
  1348. ImGui::NextLine();
  1349. keyBindingHelp("s", "Toggle Multi-channel SDF rendering");
  1350. ImGui::NextLine();
  1351. ImGui::PopFont();
  1352. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1353. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1354. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1355. || !view.m_help)
  1356. {
  1357. view.m_help = false;
  1358. ImGui::CloseCurrentPopup();
  1359. }
  1360. ImGui::EndPopup();
  1361. }
  1362. imguiEndFrame();
  1363. if (!bgfx::isValid(texture)
  1364. || view.m_fileIndex != fileIndex)
  1365. {
  1366. if (bgfx::isValid(texture) )
  1367. {
  1368. bgfx::destroy(texture);
  1369. }
  1370. fileIndex = view.m_fileIndex;
  1371. bx::FilePath fp = view.m_path;
  1372. fp.join(view.m_fileList[view.m_fileIndex].c_str() );
  1373. bimg::Orientation::Enum orientation;
  1374. texture = loadTexture(fp.get()
  1375. , 0
  1376. | BGFX_TEXTURE_U_CLAMP
  1377. | BGFX_TEXTURE_V_CLAMP
  1378. | BGFX_TEXTURE_W_CLAMP
  1379. , 0
  1380. , &view.m_textureInfo
  1381. , &orientation
  1382. );
  1383. switch (orientation)
  1384. {
  1385. default:
  1386. case bimg::Orientation::R0: cmdExec("view orientation\nview orientation z 0"); break;
  1387. case bimg::Orientation::R90: cmdExec("view orientation\nview orientation z -90"); break;
  1388. case bimg::Orientation::R180: cmdExec("view orientation\nview orientation z -180"); break;
  1389. case bimg::Orientation::R270: cmdExec("view orientation\nview orientation z -270"); break;
  1390. case bimg::Orientation::HFlip: cmdExec("view orientation\nview orientation x -180"); break;
  1391. case bimg::Orientation::HFlipR90: cmdExec("view orientation\nview orientation z -90\nview orientation x -180"); break;
  1392. case bimg::Orientation::HFlipR270: cmdExec("view orientation\nview orientation z -270\nview orientation x -180"); break;
  1393. case bimg::Orientation::VFlip: cmdExec("view orientation\nview orientation y -180"); break;
  1394. }
  1395. std::string title;
  1396. if (isValid(texture) )
  1397. {
  1398. const char* name = "";
  1399. if (view.m_textureInfo.cubeMap)
  1400. {
  1401. name = " CubeMap";
  1402. }
  1403. else if (1 < view.m_textureInfo.depth)
  1404. {
  1405. name = " 3D";
  1406. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1407. }
  1408. else if (1 < view.m_textureInfo.numLayers)
  1409. {
  1410. name = " 2D Array";
  1411. }
  1412. bx::stringPrintf(title, "%s (%d x %d%s, mips: %d, layers %d, %s)"
  1413. , fp.get()
  1414. , view.m_textureInfo.width
  1415. , view.m_textureInfo.height
  1416. , name
  1417. , view.m_textureInfo.numMips
  1418. , view.m_textureInfo.numLayers
  1419. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1420. );
  1421. }
  1422. else
  1423. {
  1424. bx::stringPrintf(title, "Failed to load %s!", filePath);
  1425. }
  1426. entry::WindowHandle handle = { 0 };
  1427. entry::setWindowTitle(handle, title.c_str() );
  1428. }
  1429. int64_t now = bx::getHPCounter();
  1430. static int64_t last = now;
  1431. const int64_t frameTime = now - last;
  1432. last = now;
  1433. const double freq = double(bx::getHPFrequency() );
  1434. time += (float)(frameTime*speed/freq);
  1435. float transitionTime = dragging ? 0.0f : 0.25f*view.m_transitionTime;
  1436. posx.set(view.m_posx, transitionTime);
  1437. posy.set(view.m_posy, transitionTime);
  1438. float ortho[16];
  1439. bx::mtxOrtho(
  1440. ortho
  1441. , 0.0f
  1442. , float(width)
  1443. , float(height)
  1444. , 0.0f
  1445. , 0.0f
  1446. , 1000.0f
  1447. , 0.0f
  1448. , caps->homogeneousDepth
  1449. );
  1450. bgfx::setViewTransform(BACKGROUND_VIEW_ID, NULL, ortho);
  1451. bgfx::setViewRect(BACKGROUND_VIEW_ID, 0, 0, uint16_t(width), uint16_t(height) );
  1452. setGeometry(Geometry::Quad
  1453. , 0
  1454. , 0
  1455. , width
  1456. , height
  1457. , view.m_alpha ? UINT32_MAX : 0
  1458. , float(width )/float(checkerBoardSize)
  1459. , float(height)/float(checkerBoardSize)
  1460. );
  1461. bgfx::setTexture(0
  1462. , s_texColor
  1463. , checkerBoard
  1464. );
  1465. bgfx::setState(0
  1466. | BGFX_STATE_RGB_WRITE
  1467. | BGFX_STATE_ALPHA_WRITE
  1468. );
  1469. bgfx::submit(BACKGROUND_VIEW_ID
  1470. , textureProgram
  1471. );
  1472. float px = posx.getValue();
  1473. float py = posy.getValue();
  1474. bx::mtxOrtho(
  1475. ortho
  1476. , px-width/2.0f
  1477. , px+width/2.0f
  1478. , py+height/2.0f
  1479. , py-height/2.0f
  1480. , -10.0f
  1481. , 10.0f
  1482. , 0.0f
  1483. , caps->homogeneousDepth
  1484. );
  1485. bgfx::setViewTransform(IMAGE_VIEW_ID, NULL, ortho);
  1486. bgfx::setViewRect(IMAGE_VIEW_ID, 0, 0, uint16_t(width), uint16_t(height) );
  1487. bgfx::dbgTextClear();
  1488. float orientation[16];
  1489. bx::mtxRotateXYZ(orientation, view.m_flipH, view.m_flipV, angle.getValue()+view.m_orientation);
  1490. if (view.m_fit)
  1491. {
  1492. float wh[3] = { float(view.m_textureInfo.width), float(view.m_textureInfo.height), 0.0f };
  1493. float result[3];
  1494. bx::vec3MulMtx(result, wh, orientation);
  1495. result[0] = bx::fround(bx::fabs(result[0]) );
  1496. result[1] = bx::fround(bx::fabs(result[1]) );
  1497. scale.set(bx::fmin(float(width) / result[0]
  1498. , float(height) / result[1])
  1499. , 0.1f*view.m_transitionTime
  1500. );
  1501. }
  1502. else
  1503. {
  1504. scale.set(1.0f, 0.1f*view.m_transitionTime);
  1505. }
  1506. zoom.set(view.m_zoom, transitionTime);
  1507. angle.set(view.m_angle, transitionTime);
  1508. angx.set(view.m_angx, transitionTime);
  1509. angy.set(view.m_angy, transitionTime);
  1510. float ss = scale.getValue()
  1511. * zoom.getValue()
  1512. ;
  1513. setGeometry(view.m_textureInfo.cubeMap ? view.m_cubeMapGeo : Geometry::Quad
  1514. , -int(view.m_textureInfo.width * ss)/2
  1515. , -int(view.m_textureInfo.height * ss)/2
  1516. , int(view.m_textureInfo.width * ss)
  1517. , int(view.m_textureInfo.height * ss)
  1518. , view.m_abgr
  1519. );
  1520. bgfx::setTransform(orientation);
  1521. float mtx[16];
  1522. bx::mtxRotateXY(mtx, angx.getValue(), angy.getValue() );
  1523. bgfx::setUniform(u_mtx, mtx);
  1524. mip.set(float(view.m_mip), 0.5f);
  1525. layer.set(float(view.m_layer), 0.25f);
  1526. ev.set(view.m_ev, 0.5f);
  1527. float params[4] = { mip.getValue(), layer.getValue(), 0.0f, ev.getValue() };
  1528. if (1 < view.m_textureInfo.depth)
  1529. {
  1530. params[1] = layer.getValue()/view.m_textureInfo.depth;
  1531. }
  1532. bgfx::setUniform(u_params, params);
  1533. const uint32_t textureFlags = 0
  1534. | BGFX_TEXTURE_U_CLAMP
  1535. | BGFX_TEXTURE_V_CLAMP
  1536. | BGFX_TEXTURE_W_CLAMP
  1537. | (view.m_filter ? 0 : 0
  1538. | BGFX_TEXTURE_MIN_POINT
  1539. | BGFX_TEXTURE_MIP_POINT
  1540. | BGFX_TEXTURE_MAG_POINT
  1541. )
  1542. ;
  1543. bgfx::setTexture(0
  1544. , s_texColor
  1545. , texture
  1546. , textureFlags
  1547. );
  1548. bgfx::setState(0
  1549. | BGFX_STATE_RGB_WRITE
  1550. | BGFX_STATE_ALPHA_WRITE
  1551. | (view.m_alpha ? BGFX_STATE_BLEND_ALPHA : BGFX_STATE_NONE)
  1552. );
  1553. bgfx:: ProgramHandle program = textureProgram;
  1554. if (1 < view.m_textureInfo.depth)
  1555. {
  1556. program = texture3DProgram;
  1557. }
  1558. else if (view.m_textureInfo.cubeMap)
  1559. {
  1560. program = Geometry::Quad == view.m_cubeMapGeo
  1561. ? textureCubeProgram
  1562. : textureCube2Program
  1563. ;
  1564. }
  1565. else if (1 < view.m_textureInfo.numLayers)
  1566. {
  1567. program = textureArrayProgram;
  1568. }
  1569. else if (view.m_sdf)
  1570. {
  1571. program = textureSdfProgram;
  1572. }
  1573. bgfx::submit(IMAGE_VIEW_ID, program);
  1574. bgfx::frame();
  1575. }
  1576. }
  1577. if (bgfx::isValid(texture) )
  1578. {
  1579. bgfx::destroy(texture);
  1580. }
  1581. bgfx::destroy(checkerBoard);
  1582. bgfx::destroy(s_texColor);
  1583. bgfx::destroy(u_mtx);
  1584. bgfx::destroy(u_params);
  1585. bgfx::destroy(textureProgram);
  1586. bgfx::destroy(textureArrayProgram);
  1587. bgfx::destroy(textureCubeProgram);
  1588. bgfx::destroy(textureCube2Program);
  1589. bgfx::destroy(textureSdfProgram);
  1590. bgfx::destroy(texture3DProgram);
  1591. imguiDestroy();
  1592. bgfx::shutdown();
  1593. return exitcode;
  1594. }