texturev.cpp 27 KB

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