texturev.cpp 26 KB

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