texturev.cpp 27 KB

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