texturev.cpp 21 KB

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