texturev.cpp 21 KB

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