texturev.cpp 45 KB

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