texturev.cpp 23 KB

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