texturev.cpp 28 KB

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