texturev.cpp 27 KB

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