texturev.cpp 21 KB

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