texturev.cpp 28 KB

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