texturev.cpp 25 KB

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