texturev.cpp 45 KB

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