texturev.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bx/commandline.h>
  8. #include <bx/os.h>
  9. #include <bx/string.h>
  10. #include <bx/uint32_t.h>
  11. #include <entry/entry.h>
  12. #include <entry/input.h>
  13. #include <entry/cmd.h>
  14. #include <imgui/imgui.h>
  15. #include <bgfx_utils.h>
  16. #include <dirent.h>
  17. #include <bx/crtimpl.h>
  18. #include <tinystl/allocator.h>
  19. #include <tinystl/vector.h>
  20. #include <string>
  21. namespace stl = tinystl;
  22. #include "image.h"
  23. #include <bgfx/embedded_shader.h>
  24. #include "vs_texture.bin.h"
  25. #include "fs_texture.bin.h"
  26. #include "fs_texture_array.bin.h"
  27. #include "vs_texture_cube.bin.h"
  28. #include "fs_texture_cube.bin.h"
  29. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  30. {
  31. BGFX_EMBEDDED_SHADER(vs_texture),
  32. BGFX_EMBEDDED_SHADER(fs_texture),
  33. BGFX_EMBEDDED_SHADER(fs_texture_array),
  34. BGFX_EMBEDDED_SHADER(vs_texture_cube),
  35. BGFX_EMBEDDED_SHADER(fs_texture_cube),
  36. BGFX_EMBEDDED_SHADER_END()
  37. };
  38. static const char* s_supportedExt[] =
  39. {
  40. "bmp",
  41. "dds",
  42. "exr",
  43. "gif",
  44. "jpg",
  45. "jpeg",
  46. "hdr",
  47. "ktx",
  48. "png",
  49. "psd",
  50. "pvr",
  51. "tga",
  52. };
  53. struct Binding
  54. {
  55. enum Enum
  56. {
  57. App,
  58. View,
  59. Count
  60. };
  61. };
  62. static const InputBinding s_bindingApp[] =
  63. {
  64. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  65. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  66. INPUT_BINDING_END
  67. };
  68. static const InputBinding s_bindingView[] =
  69. {
  70. { entry::Key::Comma, entry::Modifier::None, 1, NULL, "view mip prev" },
  71. { entry::Key::Period, entry::Modifier::None, 1, NULL, "view mip next" },
  72. { entry::Key::Comma, entry::Modifier::LeftShift, 1, NULL, "view mip" },
  73. { entry::Key::Comma, entry::Modifier::RightShift, 1, NULL, "view mip" },
  74. { entry::Key::Slash, entry::Modifier::None, 1, NULL, "view filter" },
  75. { entry::Key::Key0, entry::Modifier::None, 1, NULL, "view zoom 1.0" },
  76. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view zoom +0.1" },
  77. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view zoom -0.1" },
  78. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view file-up" },
  79. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view file-down" },
  80. { entry::Key::PageUp, entry::Modifier::None, 1, NULL, "view file-pgup" },
  81. { entry::Key::PageDown, entry::Modifier::None, 1, NULL, "view file-pgdown" },
  82. { entry::Key::Left, entry::Modifier::None, 1, NULL, "view layer prev" },
  83. { entry::Key::Right, entry::Modifier::None, 1, NULL, "view layer next" },
  84. { entry::Key::KeyR, entry::Modifier::None, 1, NULL, "view rgb r" },
  85. { entry::Key::KeyG, entry::Modifier::None, 1, NULL, "view rgb g" },
  86. { entry::Key::KeyB, entry::Modifier::None, 1, NULL, "view rgb b" },
  87. { entry::Key::KeyA, entry::Modifier::None, 1, NULL, "view rgb a" },
  88. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  89. INPUT_BINDING_END
  90. };
  91. static const char* s_bindingName[] =
  92. {
  93. "App",
  94. "View",
  95. };
  96. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  97. static const InputBinding* s_binding[] =
  98. {
  99. s_bindingApp,
  100. s_bindingView,
  101. };
  102. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  103. struct View
  104. {
  105. View()
  106. : m_fileIndex(0)
  107. , m_scaleFn(0)
  108. , m_mip(0)
  109. , m_layer(0)
  110. , m_abgr(UINT32_MAX)
  111. , m_zoom(1.0f)
  112. , m_filter(true)
  113. , m_alpha(false)
  114. , m_help(false)
  115. {
  116. }
  117. ~View()
  118. {
  119. }
  120. int32_t cmd(int32_t _argc, char const* const* _argv)
  121. {
  122. if (_argc >= 2)
  123. {
  124. if (0 == strcmp(_argv[1], "mip") )
  125. {
  126. if (_argc >= 3)
  127. {
  128. uint32_t mip = m_mip;
  129. if (0 == strcmp(_argv[2], "next") )
  130. {
  131. ++mip;
  132. }
  133. else if (0 == strcmp(_argv[2], "prev") )
  134. {
  135. --mip;
  136. }
  137. else if (0 == strcmp(_argv[2], "last") )
  138. {
  139. mip = INT32_MAX;
  140. }
  141. else
  142. {
  143. mip = atoi(_argv[2]);
  144. }
  145. m_mip = bx::uint32_iclamp(mip, 0, m_info.numMips-1);
  146. }
  147. else
  148. {
  149. m_mip = 0;
  150. }
  151. }
  152. if (0 == strcmp(_argv[1], "layer") )
  153. {
  154. if (_argc >= 3)
  155. {
  156. uint32_t layer = m_layer;
  157. if (0 == strcmp(_argv[2], "next") )
  158. {
  159. ++layer;
  160. }
  161. else if (0 == strcmp(_argv[2], "prev") )
  162. {
  163. --layer;
  164. }
  165. else if (0 == strcmp(_argv[2], "last") )
  166. {
  167. layer = INT32_MAX;
  168. }
  169. else
  170. {
  171. layer = atoi(_argv[2]);
  172. }
  173. m_layer = bx::uint32_iclamp(layer, 0, m_info.numLayers-1);
  174. }
  175. else
  176. {
  177. m_layer = 0;
  178. }
  179. }
  180. else if (0 == strcmp(_argv[1], "zoom") )
  181. {
  182. if (_argc >= 3)
  183. {
  184. float zoom = (float)atof(_argv[2]);
  185. if (_argv[2][0] == '+'
  186. || _argv[2][0] == '-')
  187. {
  188. m_zoom += zoom;
  189. }
  190. else
  191. {
  192. m_zoom = zoom;
  193. }
  194. m_zoom = bx::fclamp(m_zoom, 0.001f, 10.0f);
  195. }
  196. else
  197. {
  198. m_zoom = 1.0f;
  199. }
  200. }
  201. else if (0 == strcmp(_argv[1], "filter") )
  202. {
  203. if (_argc >= 3)
  204. {
  205. m_filter = bx::toBool(_argv[2]);
  206. }
  207. else
  208. {
  209. m_filter ^= true;
  210. }
  211. }
  212. else if (0 == strcmp(_argv[1], "file-up") )
  213. {
  214. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  215. }
  216. else if (0 == strcmp(_argv[1], "file-down") )
  217. {
  218. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  219. ++m_fileIndex;
  220. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  221. }
  222. else if (0 == strcmp(_argv[1], "rgb") )
  223. {
  224. if (_argc >= 3)
  225. {
  226. if (_argv[2][0] == 'r')
  227. {
  228. m_abgr ^= 0x000000ff;
  229. }
  230. else if (_argv[2][0] == 'g')
  231. {
  232. m_abgr ^= 0x0000ff00;
  233. }
  234. else if (_argv[2][0] == 'b')
  235. {
  236. m_abgr ^= 0x00ff0000;
  237. }
  238. else if (_argv[2][0] == 'a')
  239. {
  240. m_alpha ^= true;
  241. }
  242. }
  243. else
  244. {
  245. m_abgr = UINT32_MAX;
  246. m_alpha = false;
  247. }
  248. }
  249. else if (0 == strcmp(_argv[1], "help") )
  250. {
  251. m_help ^= true;
  252. }
  253. }
  254. return 0;
  255. }
  256. void updateFileList(const char* _path, const char* _fileName = "")
  257. {
  258. std::string path = _path;
  259. DIR* dir = opendir(_path);
  260. if (NULL == dir)
  261. {
  262. path = ".";
  263. }
  264. dir = opendir(path.c_str() );
  265. if (NULL != dir)
  266. {
  267. for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
  268. {
  269. if (0 == (item->d_type & DT_DIR) )
  270. {
  271. const char* ext = strrchr(item->d_name, '.');
  272. if (NULL != ext)
  273. {
  274. ext += 1;
  275. bool supported = false;
  276. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  277. {
  278. if (0 == bx::stricmp(ext, s_supportedExt[ii]) )
  279. {
  280. supported = true;
  281. break;
  282. }
  283. }
  284. if (supported)
  285. {
  286. if (0 == strcmp(_fileName, item->d_name) )
  287. {
  288. m_fileIndex = uint32_t(m_fileList.size() );
  289. }
  290. std::string name = path;
  291. char ch = name[name.size()-1];
  292. name += '/' == ch || '\\' == ch ? "" : "/";
  293. name += item->d_name;
  294. m_fileList.push_back(name);
  295. }
  296. }
  297. }
  298. }
  299. closedir(dir);
  300. }
  301. }
  302. typedef stl::vector<std::string> FileList;
  303. FileList m_fileList;
  304. bgfx::TextureInfo m_info;
  305. uint32_t m_fileIndex;
  306. uint32_t m_scaleFn;
  307. uint32_t m_mip;
  308. uint32_t m_layer;
  309. uint32_t m_abgr;
  310. float m_zoom;
  311. bool m_filter;
  312. bool m_alpha;
  313. bool m_help;
  314. };
  315. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  316. {
  317. View* view = static_cast<View*>(_userData);
  318. return view->cmd(_argc, _argv);
  319. }
  320. struct PosUvColorVertex
  321. {
  322. float m_x;
  323. float m_y;
  324. float m_u;
  325. float m_v;
  326. uint32_t m_abgr;
  327. static void init()
  328. {
  329. ms_decl
  330. .begin()
  331. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  332. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  333. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  334. .end();
  335. }
  336. static bgfx::VertexDecl ms_decl;
  337. };
  338. bgfx::VertexDecl PosUvColorVertex::ms_decl;
  339. bool screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, uint32_t _abgr, bool _originBottomLeft = false)
  340. {
  341. if (6 == bgfx::getAvailTransientVertexBuffer(6, PosUvColorVertex::ms_decl) )
  342. {
  343. bgfx::TransientVertexBuffer vb;
  344. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvColorVertex::ms_decl);
  345. PosUvColorVertex* vertex = (PosUvColorVertex*)vb.data;
  346. const float widthf = float(_width);
  347. const float heightf = float(_height);
  348. const float minx = float(_x);
  349. const float miny = float(_y);
  350. const float maxx = minx+widthf;
  351. const float maxy = miny+heightf;
  352. float m_halfTexel = 0.0f;
  353. const float texelHalfW = m_halfTexel/widthf;
  354. const float texelHalfH = m_halfTexel/heightf;
  355. const float minu = texelHalfW;
  356. const float maxu = 1.0f - texelHalfW;
  357. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  358. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  359. vertex[0].m_x = minx;
  360. vertex[0].m_y = miny;
  361. vertex[0].m_u = minu;
  362. vertex[0].m_v = minv;
  363. vertex[1].m_x = maxx;
  364. vertex[1].m_y = miny;
  365. vertex[1].m_u = maxu;
  366. vertex[1].m_v = minv;
  367. vertex[2].m_x = maxx;
  368. vertex[2].m_y = maxy;
  369. vertex[2].m_u = maxu;
  370. vertex[2].m_v = maxv;
  371. vertex[3].m_x = maxx;
  372. vertex[3].m_y = maxy;
  373. vertex[3].m_u = maxu;
  374. vertex[3].m_v = maxv;
  375. vertex[4].m_x = minx;
  376. vertex[4].m_y = maxy;
  377. vertex[4].m_u = minu;
  378. vertex[4].m_v = maxv;
  379. vertex[5].m_x = minx;
  380. vertex[5].m_y = miny;
  381. vertex[5].m_u = minu;
  382. vertex[5].m_v = minv;
  383. vertex[0].m_abgr = _abgr;
  384. vertex[1].m_abgr = _abgr;
  385. vertex[2].m_abgr = _abgr;
  386. vertex[3].m_abgr = _abgr;
  387. vertex[4].m_abgr = _abgr;
  388. vertex[5].m_abgr = _abgr;
  389. bgfx::setVertexBuffer(&vb);
  390. return true;
  391. }
  392. return false;
  393. }
  394. struct Interpolator
  395. {
  396. float from;
  397. float to;
  398. float duration;
  399. int64_t offset;
  400. Interpolator(float _value)
  401. {
  402. reset(_value);
  403. }
  404. void reset(float _value)
  405. {
  406. from = _value;
  407. to = _value;
  408. duration = 0.0;
  409. offset = bx::getHPCounter();
  410. }
  411. void set(float _value, float _duration)
  412. {
  413. if (_value != to)
  414. {
  415. from = getValue();
  416. to = _value;
  417. duration = _duration;
  418. offset = bx::getHPCounter();
  419. }
  420. }
  421. float getValue()
  422. {
  423. if (duration > 0.0)
  424. {
  425. const double freq = double(bx::getHPFrequency() );
  426. int64_t now = bx::getHPCounter();
  427. float time = (float)(double(now - offset) / freq);
  428. float lerp = bx::fclamp(time, 0.0, duration) / duration;
  429. return bx::flerp(from, to, lerp);
  430. }
  431. return to;
  432. }
  433. };
  434. void help(const char* _error = NULL)
  435. {
  436. if (NULL != _error)
  437. {
  438. fprintf(stderr, "Error:\n%s\n\n", _error);
  439. }
  440. fprintf(stderr
  441. , "texturev, bgfx texture viewer tool\n"
  442. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  443. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  444. );
  445. fprintf(stderr
  446. , "Usage: texturev <file path>\n"
  447. "\n"
  448. "Supported input file types:\n"
  449. );
  450. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  451. {
  452. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  453. }
  454. fprintf(stderr
  455. , "\n"
  456. "Options:\n"
  457. " --associate Associate file extensions with texturev.\n"
  458. "\n"
  459. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  460. );
  461. }
  462. void associate()
  463. {
  464. #if BX_PLATFORM_WINDOWS
  465. std::string str;
  466. char exec[MAX_PATH];
  467. GetModuleFileNameA(GetModuleHandleA(NULL), exec, MAX_PATH);
  468. std::string strExec = bx::replaceAll<std::string>(exec, "\\", "\\\\");
  469. std::string value;
  470. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  471. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  472. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  473. str += value;
  474. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  475. str += value;
  476. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  477. {
  478. const char* ext = s_supportedExt[ii];
  479. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  480. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  481. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  482. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  483. }
  484. char temp[MAX_PATH];
  485. GetTempPathA(MAX_PATH, temp);
  486. strcat(temp, "\\texturev.reg");
  487. bx::CrtFileWriter writer;
  488. bx::Error err;
  489. if (bx::open(&writer, temp, false, &err) )
  490. {
  491. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  492. bx::close(&writer);
  493. if (err.isOk() )
  494. {
  495. std::string cmd;
  496. bx::stringPrintf(cmd, "regedit.exe /s %s", temp);
  497. bx::ProcessReader reader;
  498. if (bx::open(&reader, cmd.c_str(), &err) )
  499. {
  500. bx::close(&reader);
  501. }
  502. }
  503. }
  504. #elif BX_PLATFORM_LINUX
  505. std::string str;
  506. str += "#/bin/bash\n\n";
  507. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  508. {
  509. const char* ext = s_supportedExt[ii];
  510. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  511. }
  512. str += "\n";
  513. bx::CrtFileWriter writer;
  514. bx::Error err;
  515. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  516. {
  517. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  518. bx::close(&writer);
  519. if (err.isOk() )
  520. {
  521. bx::ProcessReader reader;
  522. if (bx::open(&reader, "/bin/bash /tmp/texturev.sh", &err) )
  523. {
  524. bx::close(&reader);
  525. }
  526. }
  527. }
  528. #endif // BX_PLATFORM_WINDOWS
  529. }
  530. int _main_(int _argc, char** _argv)
  531. {
  532. bx::CommandLine cmdLine(_argc, _argv);
  533. if (cmdLine.hasArg('h', "help") )
  534. {
  535. help();
  536. return EXIT_FAILURE;
  537. }
  538. else if (cmdLine.hasArg("associate") )
  539. {
  540. associate();
  541. return EXIT_FAILURE;
  542. }
  543. uint32_t width = 1280;
  544. uint32_t height = 720;
  545. uint32_t debug = BGFX_DEBUG_TEXT;
  546. uint32_t reset = BGFX_RESET_VSYNC;
  547. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  548. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  549. View view;
  550. cmdAdd("view", cmdView, &view);
  551. bgfx::init();
  552. bgfx::reset(width, height, reset);
  553. // Set view 0 clear state.
  554. bgfx::setViewClear(0
  555. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  556. , 0x101010ff
  557. , 1.0f
  558. , 0
  559. );
  560. imguiCreate();
  561. PosUvColorVertex::init();
  562. bgfx::RendererType::Enum type = bgfx::getRendererType();
  563. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  564. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  565. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  566. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  567. vsTexture
  568. , fsTexture
  569. , true
  570. );
  571. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  572. vsTexture
  573. , bgfx::isValid(fsTextureArray)
  574. ? fsTextureArray
  575. : fsTexture
  576. , true
  577. );
  578. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  579. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  580. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  581. , true
  582. );
  583. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  584. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  585. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  586. float speed = 0.37f;
  587. float time = 0.0f;
  588. Interpolator mip(0.0f);
  589. Interpolator layer(0.0f);
  590. Interpolator zoom(1.0f);
  591. Interpolator scale(1.0f);
  592. const char* filePath = _argc < 2 ? "" : _argv[1];
  593. bool directory = false;
  594. bx::FileInfo fi;
  595. bx::stat(filePath, fi);
  596. directory = bx::FileInfo::Directory == fi.m_type;
  597. std::string path = filePath;
  598. if (!directory)
  599. {
  600. const char* fileName = directory ? filePath : bx::baseName(filePath);
  601. path.assign(filePath, fileName);
  602. view.updateFileList(path.c_str(), fileName);
  603. }
  604. else
  605. {
  606. view.updateFileList(path.c_str() );
  607. }
  608. int exitcode = EXIT_SUCCESS;
  609. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  610. if (view.m_fileList.empty() )
  611. {
  612. exitcode = EXIT_FAILURE;
  613. if (2 > _argc)
  614. {
  615. help("File path is not specified.");
  616. }
  617. else
  618. {
  619. fprintf(stderr, "Unable to load '%s' texture.\n", filePath);
  620. }
  621. }
  622. else
  623. {
  624. uint32_t fileIndex = 0;
  625. entry::MouseState mouseState;
  626. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  627. {
  628. imguiBeginFrame(mouseState.m_mx
  629. , mouseState.m_my
  630. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  631. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  632. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  633. , mouseState.m_mz
  634. , width
  635. , height
  636. );
  637. static bool help = false;
  638. if (help == false
  639. && help != view.m_help)
  640. {
  641. ImGui::OpenPopup("Help");
  642. }
  643. if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize) )
  644. {
  645. ImGui::SetWindowFontScale(1.0f);
  646. ImGui::Text(
  647. "texturev, bgfx texture viewer tool " ICON_KI_WRENCH "\n"
  648. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  649. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  650. );
  651. ImGui::Separator();
  652. ImGui::NextLine();
  653. ImGui::Text("Key bindings:\n\n");
  654. ImGui::PushFont(ImGui::Font::Mono);
  655. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "ESC"); ImGui::SameLine(64); ImGui::Text("Exit.");
  656. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "h"); ImGui::SameLine(64); ImGui::Text("Toggle help screen.");
  657. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "f"); ImGui::SameLine(64); ImGui::Text("Toggle full-screen.");
  658. ImGui::NextLine();
  659. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "-"); ImGui::SameLine(64); ImGui::Text("Zoom out.");
  660. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "="); ImGui::SameLine(64); ImGui::Text("Zoom in.");
  661. ImGui::NextLine();
  662. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), ","); ImGui::SameLine(64); ImGui::Text("MIP level up.");
  663. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "."); ImGui::SameLine(64); ImGui::Text("MIP level down.");
  664. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "/"); ImGui::SameLine(64); ImGui::Text("Toggle linear/point texture sampling.");
  665. ImGui::NextLine();
  666. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "left"); ImGui::SameLine(64); ImGui::Text("Previous layer in texture array.");
  667. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "right"); ImGui::SameLine(64); ImGui::Text("Next layer in texture array.");
  668. ImGui::NextLine();
  669. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "up"); ImGui::SameLine(64); ImGui::Text("Previous texture.");
  670. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "down"); ImGui::SameLine(64); ImGui::Text("Next texture.");
  671. ImGui::NextLine();
  672. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "r/g/b"); ImGui::SameLine(64); ImGui::Text("Toggle R, G, or B color channel.");
  673. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "a"); ImGui::SameLine(64); ImGui::Text("Toggle alpha blending.");
  674. ImGui::PopFont();
  675. ImGui::NextLine();
  676. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  677. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  678. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  679. || !view.m_help)
  680. {
  681. view.m_help = false;
  682. ImGui::CloseCurrentPopup();
  683. }
  684. ImGui::EndPopup();
  685. }
  686. help = view.m_help;
  687. // bool b;
  688. // ImGui::ShowTestWindow(&b);
  689. imguiEndFrame();
  690. if (!bgfx::isValid(texture)
  691. || view.m_fileIndex != fileIndex)
  692. {
  693. if (bgfx::isValid(texture) )
  694. {
  695. bgfx::destroyTexture(texture);
  696. }
  697. fileIndex = view.m_fileIndex;
  698. filePath = view.m_fileList[view.m_fileIndex].c_str();
  699. texture = loadTexture(filePath
  700. , 0
  701. | BGFX_TEXTURE_U_CLAMP
  702. | BGFX_TEXTURE_V_CLAMP
  703. | BGFX_TEXTURE_W_CLAMP
  704. , 0
  705. , &view.m_info
  706. );
  707. std::string title;
  708. bx::stringPrintf(title, "%s (%d x %d%s, %s)"
  709. , filePath
  710. , view.m_info.width
  711. , view.m_info.height
  712. , view.m_info.cubeMap ? " CubeMap" : ""
  713. , bgfx::getName(view.m_info.format)
  714. );
  715. entry::WindowHandle handle = { 0 };
  716. entry::setWindowTitle(handle, title.c_str() );
  717. }
  718. int64_t now = bx::getHPCounter();
  719. static int64_t last = now;
  720. const int64_t frameTime = now - last;
  721. last = now;
  722. const double freq = double(bx::getHPFrequency() );
  723. time += (float)(frameTime*speed/freq);
  724. float ortho[16];
  725. bx::mtxOrtho(ortho, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  726. bgfx::setViewTransform(0, NULL, ortho);
  727. bgfx::setViewRect(0, 0, 0, width, height);
  728. bgfx::touch(0);
  729. bgfx::dbgTextClear();
  730. scale.set(
  731. bx::fmin( float(width) / float(view.m_info.width)
  732. , float(height) / float(view.m_info.height)
  733. )
  734. , 0.1f
  735. );
  736. zoom.set(view.m_zoom, 0.25);
  737. float ss = scale.getValue() * zoom.getValue();
  738. screenQuad( int(width - view.m_info.width * ss)/2
  739. , int(height - view.m_info.height * ss)/2
  740. , int(view.m_info.width * ss)
  741. , int(view.m_info.height * ss)
  742. , view.m_abgr
  743. );
  744. float mtx[16];
  745. bx::mtxRotateXY(mtx, 0.0f, time);
  746. bgfx::setUniform(u_mtx, mtx);
  747. mip.set(float(view.m_mip), 0.5f);
  748. layer.set(float(view.m_layer), 0.25f);
  749. float params[4] = { mip.getValue(), layer.getValue(), 0.0f, 0.0f };
  750. bgfx::setUniform(u_params, params);
  751. bgfx::setTexture(0
  752. , s_texColor
  753. , texture
  754. , view.m_filter
  755. ? BGFX_TEXTURE_NONE
  756. : 0
  757. | BGFX_TEXTURE_MIN_POINT
  758. | BGFX_TEXTURE_MIP_POINT
  759. | BGFX_TEXTURE_MAG_POINT
  760. );
  761. bgfx::setState(0
  762. | BGFX_STATE_RGB_WRITE
  763. | BGFX_STATE_ALPHA_WRITE
  764. | (view.m_alpha ? BGFX_STATE_BLEND_ALPHA : BGFX_STATE_NONE)
  765. );
  766. bgfx::submit(0, view.m_info.cubeMap ? textureCubeProgram
  767. : 1 < view.m_info.numLayers ? textureArrayProgram
  768. : textureProgram
  769. );
  770. bgfx::frame();
  771. }
  772. }
  773. if (bgfx::isValid(texture) )
  774. {
  775. bgfx::destroyTexture(texture);
  776. }
  777. bgfx::destroyUniform(s_texColor);
  778. bgfx::destroyUniform(u_mtx);
  779. bgfx::destroyUniform(u_params);
  780. bgfx::destroyProgram(textureProgram);
  781. bgfx::destroyProgram(textureArrayProgram);
  782. bgfx::destroyProgram(textureCubeProgram);
  783. imguiDestroy();
  784. bgfx::shutdown();
  785. return exitcode;
  786. }