texturev.cpp 24 KB

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