texturev.cpp 22 KB

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