texturev.cpp 28 KB

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