texturev.cpp 28 KB

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