texturev.cpp 23 KB

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