texturev.cpp 30 KB

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