texturev.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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/filepath.h>
  10. #include <bx/uint32_t.h>
  11. #include <bx/math.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/file.h>
  20. #include <bx/process.h>
  21. #include <tinystl/allocator.h>
  22. #include <tinystl/vector.h>
  23. namespace stl = tinystl;
  24. #include <string>
  25. #include <algorithm>
  26. #include <bimg/decode.h>
  27. #include <bgfx/embedded_shader.h>
  28. #include "vs_texture.bin.h"
  29. #include "vs_texture_cube.bin.h"
  30. #include "fs_texture.bin.h"
  31. #include "fs_texture_array.bin.h"
  32. #include "fs_texture_cube.bin.h"
  33. #include "fs_texture_cube2.bin.h"
  34. #include "fs_texture_sdf.bin.h"
  35. #include "fs_texture_3d.bin.h"
  36. #define BACKGROUND_VIEW_ID 0
  37. #define IMAGE_VIEW_ID 1
  38. #define BGFX_TEXTUREV_VERSION_MAJOR 1
  39. #define BGFX_TEXTUREV_VERSION_MINOR 0
  40. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  41. {
  42. BGFX_EMBEDDED_SHADER(vs_texture),
  43. BGFX_EMBEDDED_SHADER(fs_texture),
  44. BGFX_EMBEDDED_SHADER(fs_texture_array),
  45. BGFX_EMBEDDED_SHADER(vs_texture_cube),
  46. BGFX_EMBEDDED_SHADER(fs_texture_cube),
  47. BGFX_EMBEDDED_SHADER(fs_texture_cube2),
  48. BGFX_EMBEDDED_SHADER(fs_texture_sdf),
  49. BGFX_EMBEDDED_SHADER(fs_texture_3d),
  50. BGFX_EMBEDDED_SHADER_END()
  51. };
  52. static const char* s_supportedExt[] =
  53. {
  54. "bmp",
  55. "dds",
  56. "exr",
  57. "gif",
  58. "jpg",
  59. "jpeg",
  60. "hdr",
  61. "ktx",
  62. "png",
  63. "psd",
  64. "pvr",
  65. "tga",
  66. };
  67. struct Binding
  68. {
  69. enum Enum
  70. {
  71. App,
  72. View,
  73. Help,
  74. Count
  75. };
  76. };
  77. struct Geometry
  78. {
  79. enum Enum
  80. {
  81. Quad,
  82. Cross,
  83. Hexagon,
  84. Count
  85. };
  86. };
  87. static const InputBinding s_bindingApp[] =
  88. {
  89. { entry::Key::KeyQ, entry::Modifier::None, 1, NULL, "exit" },
  90. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  91. INPUT_BINDING_END
  92. };
  93. const char* s_resetCmd =
  94. "view zoom 1.0\n"
  95. "view rotate 0\n"
  96. "view cubemap\n"
  97. "view pan\n"
  98. ;
  99. static const InputBinding s_bindingView[] =
  100. {
  101. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  102. { entry::Key::Comma, entry::Modifier::None, 1, NULL, "view mip prev" },
  103. { entry::Key::Period, entry::Modifier::None, 1, NULL, "view mip next" },
  104. { entry::Key::Comma, entry::Modifier::LeftShift, 1, NULL, "view mip" },
  105. { entry::Key::Comma, entry::Modifier::RightShift, 1, NULL, "view mip" },
  106. { entry::Key::Slash, entry::Modifier::None, 1, NULL, "view filter" },
  107. { entry::Key::Key1, entry::Modifier::None, 1, NULL, "view zoom 1.0\n"
  108. "view fit\n" },
  109. { entry::Key::Key0, entry::Modifier::None, 1, NULL, s_resetCmd },
  110. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view zoom +0.1" },
  111. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view zoom -0.1" },
  112. { entry::Key::KeyZ, entry::Modifier::None, 1, NULL, "view rotate -90" },
  113. { entry::Key::KeyZ, entry::Modifier::LeftShift, 1, NULL, "view rotate +90" },
  114. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view pan\n"
  115. "view file-up" },
  116. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view pan\n"
  117. "view file-down" },
  118. { entry::Key::PageUp, entry::Modifier::None, 1, NULL, "view pan\n"
  119. "view file-pgup" },
  120. { entry::Key::PageDown, entry::Modifier::None, 1, NULL, "view pan\n"
  121. "view file-pgdown" },
  122. { entry::Key::Left, entry::Modifier::None, 1, NULL, "view layer prev" },
  123. { entry::Key::Right, entry::Modifier::None, 1, NULL, "view layer next" },
  124. { entry::Key::KeyR, entry::Modifier::None, 1, NULL, "view rgb r" },
  125. { entry::Key::KeyG, entry::Modifier::None, 1, NULL, "view rgb g" },
  126. { entry::Key::KeyB, entry::Modifier::None, 1, NULL, "view rgb b" },
  127. { entry::Key::KeyA, entry::Modifier::None, 1, NULL, "view rgb a" },
  128. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  129. { entry::Key::Return, entry::Modifier::None, 1, NULL, "view files" },
  130. { entry::Key::KeyS, entry::Modifier::None, 1, NULL, "view sdf" },
  131. { entry::Key::Space, entry::Modifier::None, 1, NULL, "view geo\n"
  132. "view pan\n" },
  133. INPUT_BINDING_END
  134. };
  135. static const InputBinding s_bindingHelp[] =
  136. {
  137. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "view help" },
  138. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  139. INPUT_BINDING_END
  140. };
  141. static const char* s_bindingName[] =
  142. {
  143. "App",
  144. "View",
  145. "Help",
  146. };
  147. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  148. static const InputBinding* s_binding[] =
  149. {
  150. s_bindingApp,
  151. s_bindingView,
  152. s_bindingHelp,
  153. };
  154. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  155. struct View
  156. {
  157. View()
  158. : m_cubeMapGeo(Geometry::Quad)
  159. , m_fileIndex(0)
  160. , m_scaleFn(0)
  161. , m_mip(0)
  162. , m_layer(0)
  163. , m_abgr(UINT32_MAX)
  164. , m_posx(0.0f)
  165. , m_posy(0.0f)
  166. , m_angx(0.0f)
  167. , m_angy(0.0f)
  168. , m_zoom(1.0f)
  169. , m_angle(0.0f)
  170. , m_filter(true)
  171. , m_fit(true)
  172. , m_alpha(false)
  173. , m_help(false)
  174. , m_files(false)
  175. , m_sdf(false)
  176. {
  177. }
  178. ~View()
  179. {
  180. }
  181. int32_t cmd(int32_t _argc, char const* const* _argv)
  182. {
  183. if (_argc >= 2)
  184. {
  185. if (0 == bx::strCmp(_argv[1], "mip") )
  186. {
  187. if (_argc >= 3)
  188. {
  189. uint32_t mip = m_mip;
  190. if (0 == bx::strCmp(_argv[2], "next") )
  191. {
  192. ++mip;
  193. }
  194. else if (0 == bx::strCmp(_argv[2], "prev") )
  195. {
  196. --mip;
  197. }
  198. else if (0 == bx::strCmp(_argv[2], "last") )
  199. {
  200. mip = INT32_MAX;
  201. }
  202. else
  203. {
  204. mip = atoi(_argv[2]);
  205. }
  206. m_mip = bx::uint32_iclamp(mip, 0, m_info.numMips-1);
  207. }
  208. else
  209. {
  210. m_mip = 0;
  211. }
  212. }
  213. if (0 == bx::strCmp(_argv[1], "layer") )
  214. {
  215. if (_argc >= 3)
  216. {
  217. uint32_t layer = m_layer;
  218. if (0 == bx::strCmp(_argv[2], "next") )
  219. {
  220. ++layer;
  221. }
  222. else if (0 == bx::strCmp(_argv[2], "prev") )
  223. {
  224. --layer;
  225. }
  226. else if (0 == bx::strCmp(_argv[2], "last") )
  227. {
  228. layer = INT32_MAX;
  229. }
  230. else
  231. {
  232. layer = atoi(_argv[2]);
  233. }
  234. m_layer = bx::uint32_iclamp(layer, 0, m_info.numLayers-1);
  235. }
  236. else
  237. {
  238. m_layer = 0;
  239. }
  240. }
  241. else if (0 == bx::strCmp(_argv[1], "pan") )
  242. {
  243. if (_argc >= 3)
  244. {
  245. if (_argc >= 4)
  246. {
  247. float yy;
  248. bx::fromString(&yy, _argv[3]);
  249. if (_argv[3][0] == '+'
  250. || _argv[3][0] == '-')
  251. {
  252. m_posy += yy;
  253. }
  254. else
  255. {
  256. m_posy = yy;
  257. }
  258. }
  259. float xx;
  260. bx::fromString(&xx, _argv[2]);
  261. if (_argv[2][0] == '+'
  262. || _argv[2][0] == '-')
  263. {
  264. m_posx += xx;
  265. }
  266. else
  267. {
  268. m_posx = xx;
  269. }
  270. }
  271. else
  272. {
  273. m_posx = 0.0f;
  274. m_posy = 0.0f;
  275. }
  276. }
  277. else if (0 == bx::strCmp(_argv[1], "cubemap") )
  278. {
  279. if (_argc >= 3)
  280. {
  281. if (_argc >= 4)
  282. {
  283. float yy;
  284. bx::fromString(&yy, _argv[3]);
  285. if (_argv[3][0] == '+'
  286. || _argv[3][0] == '-')
  287. {
  288. m_angy += bx::toRad(yy);
  289. }
  290. else
  291. {
  292. m_angy = bx::toRad(yy);
  293. }
  294. }
  295. float xx;
  296. bx::fromString(&xx, _argv[2]);
  297. if (_argv[2][0] == '+'
  298. || _argv[2][0] == '-')
  299. {
  300. m_angx += bx::toRad(xx);
  301. }
  302. else
  303. {
  304. m_angx = bx::toRad(xx);
  305. }
  306. }
  307. else
  308. {
  309. m_angx = 0.0f;
  310. m_angy = 0.0f;
  311. }
  312. }
  313. else if (0 == bx::strCmp(_argv[1], "zoom") )
  314. {
  315. if (_argc >= 3)
  316. {
  317. float zoom;
  318. bx::fromString(&zoom, _argv[2]);
  319. if (_argv[2][0] == '+'
  320. || _argv[2][0] == '-')
  321. {
  322. m_zoom += zoom;
  323. }
  324. else
  325. {
  326. m_zoom = zoom;
  327. }
  328. m_zoom = bx::fclamp(m_zoom, 0.01f, 10.0f);
  329. }
  330. else
  331. {
  332. m_zoom = 1.0f;
  333. }
  334. }
  335. else if (0 == bx::strCmp(_argv[1], "rotate") )
  336. {
  337. if (_argc >= 3)
  338. {
  339. float angle;
  340. bx::fromString(&angle, _argv[2]);
  341. if (_argv[2][0] == '+'
  342. || _argv[2][0] == '-')
  343. {
  344. m_angle += bx::toRad(angle);
  345. }
  346. else
  347. {
  348. m_angle = bx::toRad(angle);
  349. }
  350. m_angle = bx::fwrap(m_angle, bx::kPi*2.0f);
  351. }
  352. else
  353. {
  354. m_angle = 0.0f;
  355. }
  356. }
  357. else if (0 == bx::strCmp(_argv[1], "filter") )
  358. {
  359. if (_argc >= 3)
  360. {
  361. m_filter = bx::toBool(_argv[2]);
  362. }
  363. else
  364. {
  365. m_filter ^= true;
  366. }
  367. }
  368. else if (0 == bx::strCmp(_argv[1], "fit") )
  369. {
  370. if (_argc >= 3)
  371. {
  372. m_fit = bx::toBool(_argv[2]);
  373. }
  374. else
  375. {
  376. m_fit ^= true;
  377. }
  378. }
  379. else if (0 == bx::strCmp(_argv[1], "file-up") )
  380. {
  381. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  382. }
  383. else if (0 == bx::strCmp(_argv[1], "file-down") )
  384. {
  385. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  386. ++m_fileIndex;
  387. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  388. }
  389. else if (0 == bx::strCmp(_argv[1], "rgb") )
  390. {
  391. if (_argc >= 3)
  392. {
  393. if (_argv[2][0] == 'r')
  394. {
  395. m_abgr ^= 0x000000ff;
  396. }
  397. else if (_argv[2][0] == 'g')
  398. {
  399. m_abgr ^= 0x0000ff00;
  400. }
  401. else if (_argv[2][0] == 'b')
  402. {
  403. m_abgr ^= 0x00ff0000;
  404. }
  405. else if (_argv[2][0] == 'a')
  406. {
  407. m_alpha ^= true;
  408. }
  409. }
  410. else
  411. {
  412. m_abgr = UINT32_MAX;
  413. m_alpha = false;
  414. }
  415. }
  416. else if (0 == bx::strCmp(_argv[1], "sdf") )
  417. {
  418. m_sdf ^= true;
  419. }
  420. else if (0 == bx::strCmp(_argv[1], "geo") )
  421. {
  422. if (_argc >= 3)
  423. {
  424. if (bx::toLower(_argv[2][0]) == 'c')
  425. {
  426. m_cubeMapGeo = Geometry::Cross;
  427. }
  428. else if (bx::toLower(_argv[2][0]) == 'h')
  429. {
  430. m_cubeMapGeo = Geometry::Hexagon;
  431. }
  432. else
  433. {
  434. m_cubeMapGeo = Geometry::Quad;
  435. }
  436. }
  437. else
  438. {
  439. m_cubeMapGeo = Geometry::Enum( (m_cubeMapGeo + 1) % Geometry::Count);
  440. }
  441. }
  442. else if (0 == bx::strCmp(_argv[1], "help") )
  443. {
  444. m_help ^= true;
  445. }
  446. else if (0 == bx::strCmp(_argv[1], "files") )
  447. {
  448. m_files ^= true;
  449. }
  450. }
  451. return 0;
  452. }
  453. static bool sortNameAscending(const std::string& _lhs, const std::string& _rhs)
  454. {
  455. return 0 > bx::strCmpV(_lhs.c_str(), _rhs.c_str() );
  456. }
  457. void updateFileList(const bx::FilePath& _filePath)
  458. {
  459. DIR* dir = opendir(_filePath.get() );
  460. if (NULL == dir)
  461. {
  462. m_path = _filePath.getPath();
  463. dir = opendir(m_path.get() );
  464. }
  465. else
  466. {
  467. m_path = _filePath;
  468. }
  469. if (NULL != dir)
  470. {
  471. for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
  472. {
  473. if (0 == (item->d_type & DT_DIR) )
  474. {
  475. const char* ext = bx::strRFind(item->d_name, '.');
  476. if (NULL != ext)
  477. {
  478. ext += 1;
  479. bool supported = false;
  480. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  481. {
  482. if (0 == bx::strCmpI(ext, s_supportedExt[ii]) )
  483. {
  484. supported = true;
  485. break;
  486. }
  487. }
  488. if (supported)
  489. {
  490. m_fileList.push_back(item->d_name);
  491. }
  492. }
  493. }
  494. }
  495. std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);
  496. uint32_t idx = 0;
  497. for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
  498. {
  499. if (0 == bx::strCmp(it->c_str(), _filePath.getFileName() ) )
  500. {
  501. break;
  502. }
  503. }
  504. m_fileIndex = idx == m_fileList.size() ? 0 : idx;
  505. closedir(dir);
  506. }
  507. }
  508. bx::FilePath m_path;
  509. typedef stl::vector<std::string> FileList;
  510. FileList m_fileList;
  511. bgfx::TextureInfo m_info;
  512. Geometry::Enum m_cubeMapGeo;
  513. uint32_t m_fileIndex;
  514. uint32_t m_scaleFn;
  515. uint32_t m_mip;
  516. uint32_t m_layer;
  517. uint32_t m_abgr;
  518. float m_posx;
  519. float m_posy;
  520. float m_angx;
  521. float m_angy;
  522. float m_zoom;
  523. float m_angle;
  524. bool m_filter;
  525. bool m_fit;
  526. bool m_alpha;
  527. bool m_help;
  528. bool m_files;
  529. bool m_sdf;
  530. };
  531. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  532. {
  533. View* view = static_cast<View*>(_userData);
  534. return view->cmd(_argc, _argv);
  535. }
  536. struct PosUvwColorVertex
  537. {
  538. float m_x;
  539. float m_y;
  540. float m_u;
  541. float m_v;
  542. float m_w;
  543. uint32_t m_abgr;
  544. static void init()
  545. {
  546. ms_decl
  547. .begin()
  548. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  549. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  550. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  551. .end();
  552. }
  553. void set(float _x, float _y, float _u, float _v, float _w, uint32_t _abgr)
  554. {
  555. m_x = _x;
  556. m_y = _y;
  557. m_u = _u;
  558. m_v = _v;
  559. m_w = _w;
  560. m_abgr = _abgr;
  561. }
  562. static bgfx::VertexDecl ms_decl;
  563. };
  564. bgfx::VertexDecl PosUvwColorVertex::ms_decl;
  565. static uint32_t addQuad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
  566. {
  567. _indices[0] = _idx0;
  568. _indices[1] = _idx3;
  569. _indices[2] = _idx1;
  570. _indices[3] = _idx1;
  571. _indices[4] = _idx3;
  572. _indices[5] = _idx2;
  573. return 6;
  574. }
  575. void setGeometry(
  576. Geometry::Enum _type
  577. , int32_t _x
  578. , int32_t _y
  579. , int32_t _width
  580. , uint32_t _height
  581. , uint32_t _abgr
  582. , float _maxu = 1.0f
  583. , float _maxv = 1.0f
  584. )
  585. {
  586. if (Geometry::Quad == _type)
  587. {
  588. if (6 == bgfx::getAvailTransientVertexBuffer(6, PosUvwColorVertex::ms_decl) )
  589. {
  590. bgfx::TransientVertexBuffer vb;
  591. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvwColorVertex::ms_decl);
  592. PosUvwColorVertex* vertex = (PosUvwColorVertex*)vb.data;
  593. const float widthf = float(_width);
  594. const float heightf = float(_height);
  595. const float minx = float(_x);
  596. const float miny = float(_y);
  597. const float maxx = minx+widthf;
  598. const float maxy = miny+heightf;
  599. const float minu = 0.0f;
  600. const float maxu = _maxu;
  601. const float minv = 0.0f;
  602. const float maxv = _maxv;
  603. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  604. vertex->set(maxx, miny, maxu, minv, 0.0f, _abgr); ++vertex;
  605. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  606. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  607. vertex->set(minx, maxy, minu, maxv, 0.0f, _abgr); ++vertex;
  608. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  609. bgfx::setVertexBuffer(0, &vb);
  610. }
  611. }
  612. else
  613. {
  614. const uint32_t numVertices = 14;
  615. const uint32_t numIndices = 36;
  616. if (checkAvailTransientBuffers(numVertices, PosUvwColorVertex::ms_decl, numIndices) )
  617. {
  618. bgfx::TransientVertexBuffer tvb;
  619. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosUvwColorVertex::ms_decl);
  620. bgfx::TransientIndexBuffer tib;
  621. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  622. PosUvwColorVertex* vertex = (PosUvwColorVertex*)tvb.data;
  623. uint16_t* indices = (uint16_t*)tib.data;
  624. if (Geometry::Cross == _type)
  625. {
  626. const float sx = _width /1.5f;
  627. const float sy = _height/1.5f;
  628. const float px = float(_x)-sx/4.0f;
  629. const float py = float(_y);
  630. vertex->set(0.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  631. vertex->set(0.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  632. vertex->set(0.5f*sx+px, 0.0f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  633. vertex->set(0.5f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  634. vertex->set(0.5f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  635. vertex->set(0.5f*sx+px, 1.5f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  636. vertex->set(1.0f*sx+px, 0.0f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  637. vertex->set(1.0f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  638. vertex->set(1.0f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  639. vertex->set(1.0f*sx+px, 1.5f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  640. vertex->set(1.5f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  641. vertex->set(1.5f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  642. vertex->set(2.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  643. vertex->set(2.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  644. indices += addQuad(indices, 0, 3, 4, 1);
  645. indices += addQuad(indices, 2, 6, 7, 3);
  646. indices += addQuad(indices, 3, 7, 8, 4);
  647. indices += addQuad(indices, 4, 8, 9, 5);
  648. indices += addQuad(indices, 7, 10, 11, 8);
  649. indices += addQuad(indices, 10, 12, 13, 11);
  650. }
  651. else
  652. {
  653. const float sx = float(_width);
  654. const float sy = float(_height);
  655. const float px = float(_x) - sx/2.0f;
  656. const float py = float(_y);
  657. vertex->set(0.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  658. vertex->set(0.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  659. vertex->set(0.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  660. vertex->set(0.5f*sx+px, 0.50f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  661. vertex->set(0.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  662. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  663. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  664. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  665. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  666. vertex->set(1.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  667. vertex->set(1.5f*sx+px, 0.50f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  668. vertex->set(1.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  669. vertex->set(2.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  670. vertex->set(2.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  671. indices += addQuad(indices, 0, 2, 3, 1);
  672. indices += addQuad(indices, 1, 3, 6, 4);
  673. indices += addQuad(indices, 2, 5, 6, 3);
  674. indices += addQuad(indices, 7, 9, 12, 10);
  675. indices += addQuad(indices, 7, 10, 11, 8);
  676. indices += addQuad(indices, 10, 12, 13, 11);
  677. }
  678. bgfx::setVertexBuffer(0, &tvb);
  679. bgfx::setIndexBuffer(&tib);
  680. }
  681. }
  682. }
  683. template<bx::LerpFn lerpT, bx::EaseFn easeT>
  684. struct InterpolatorT
  685. {
  686. float from;
  687. float to;
  688. float duration;
  689. int64_t offset;
  690. InterpolatorT(float _value)
  691. {
  692. reset(_value);
  693. }
  694. void reset(float _value)
  695. {
  696. from = _value;
  697. to = _value;
  698. duration = 0.0;
  699. offset = bx::getHPCounter();
  700. }
  701. void set(float _value, float _duration)
  702. {
  703. if (_value != to)
  704. {
  705. from = getValue();
  706. to = _value;
  707. duration = _duration;
  708. offset = bx::getHPCounter();
  709. }
  710. }
  711. float getValue()
  712. {
  713. if (duration > 0.0)
  714. {
  715. const double freq = double(bx::getHPFrequency() );
  716. int64_t now = bx::getHPCounter();
  717. float time = (float)(double(now - offset) / freq);
  718. float lerp = bx::fclamp(time, 0.0, duration) / duration;
  719. return lerpT(from, to, easeT(lerp) );
  720. }
  721. return to;
  722. }
  723. };
  724. typedef InterpolatorT<bx::flerp, bx::easeInOutQuad> Interpolator;
  725. typedef InterpolatorT<bx::angleLerp, bx::easeInOutCubic> InterpolatorAngle;
  726. void keyBindingHelp(const char* _bindings, const char* _description)
  727. {
  728. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
  729. ImGui::SameLine(100);
  730. ImGui::Text(_description);
  731. }
  732. void associate()
  733. {
  734. #if BX_PLATFORM_WINDOWS
  735. std::string str;
  736. char exec[MAX_PATH];
  737. GetModuleFileNameA(GetModuleHandleA(NULL), exec, MAX_PATH);
  738. std::string strExec = bx::replaceAll<std::string>(exec, "\\", "\\\\");
  739. std::string value;
  740. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  741. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  742. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  743. str += value;
  744. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  745. str += value;
  746. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  747. {
  748. const char* ext = s_supportedExt[ii];
  749. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  750. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  751. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  752. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  753. }
  754. bx::FilePath filePath(bx::TempDir::Tag);
  755. filePath.join("texture.reg");
  756. bx::FileWriter writer;
  757. bx::Error err;
  758. if (bx::open(&writer, filePath, false, &err) )
  759. {
  760. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  761. bx::close(&writer);
  762. if (err.isOk() )
  763. {
  764. std::string cmd;
  765. bx::stringPrintf(cmd, "/s %s", filePath.get() );
  766. bx::ProcessReader reader;
  767. if (bx::open(&reader, "regedit.exe", cmd.c_str(), &err) )
  768. {
  769. bx::close(&reader);
  770. }
  771. }
  772. }
  773. #elif BX_PLATFORM_LINUX
  774. std::string str;
  775. str += "#/bin/bash\n\n";
  776. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  777. {
  778. const char* ext = s_supportedExt[ii];
  779. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  780. }
  781. str += "\n";
  782. bx::FileWriter writer;
  783. bx::Error err;
  784. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  785. {
  786. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  787. bx::close(&writer);
  788. if (err.isOk() )
  789. {
  790. bx::ProcessReader reader;
  791. if (bx::open(&reader, "/bin/bash", "/tmp/texturev.sh", &err) )
  792. {
  793. bx::close(&reader);
  794. }
  795. }
  796. }
  797. #endif // BX_PLATFORM_WINDOWS
  798. }
  799. void help(const char* _error = NULL)
  800. {
  801. if (NULL != _error)
  802. {
  803. fprintf(stderr, "Error:\n%s\n\n", _error);
  804. }
  805. fprintf(stderr
  806. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  807. "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
  808. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  809. , BGFX_TEXTUREV_VERSION_MAJOR
  810. , BGFX_TEXTUREV_VERSION_MINOR
  811. , BGFX_API_VERSION
  812. );
  813. fprintf(stderr
  814. , "Usage: texturev <file path>\n"
  815. "\n"
  816. "Supported input file types:\n"
  817. );
  818. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  819. {
  820. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  821. }
  822. fprintf(stderr
  823. , "\n"
  824. "Options:\n"
  825. " -h, --help Help.\n"
  826. " -v, --version Version information only.\n"
  827. " --associate Associate file extensions with texturev.\n"
  828. "\n"
  829. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  830. );
  831. }
  832. int _main_(int _argc, char** _argv)
  833. {
  834. bx::CommandLine cmdLine(_argc, _argv);
  835. if (cmdLine.hasArg('v', "version") )
  836. {
  837. fprintf(stderr
  838. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  839. , BGFX_TEXTUREV_VERSION_MAJOR
  840. , BGFX_TEXTUREV_VERSION_MINOR
  841. , BGFX_API_VERSION
  842. );
  843. return bx::kExitSuccess;
  844. }
  845. if (cmdLine.hasArg('h', "help") )
  846. {
  847. help();
  848. return bx::kExitFailure;
  849. }
  850. else if (cmdLine.hasArg("associate") )
  851. {
  852. associate();
  853. return bx::kExitFailure;
  854. }
  855. uint32_t width = 1280;
  856. uint32_t height = 720;
  857. uint32_t debug = BGFX_DEBUG_TEXT;
  858. uint32_t reset = BGFX_RESET_VSYNC;
  859. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  860. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  861. View view;
  862. cmdAdd("view", cmdView, &view);
  863. bgfx::init();
  864. bgfx::reset(width, height, reset);
  865. // Set view 0 clear state.
  866. bgfx::setViewClear(BACKGROUND_VIEW_ID
  867. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  868. , 0x000000ff
  869. , 1.0f
  870. , 0
  871. );
  872. imguiCreate();
  873. PosUvwColorVertex::init();
  874. const bgfx::Caps* caps = bgfx::getCaps();
  875. bgfx::RendererType::Enum type = caps->rendererType;
  876. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  877. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  878. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  879. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  880. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  881. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  882. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  883. vsTexture
  884. , fsTexture
  885. , true
  886. );
  887. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  888. vsTexture
  889. , bgfx::isValid(fsTextureArray)
  890. ? fsTextureArray
  891. : fsTexture
  892. , true
  893. );
  894. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  895. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  896. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  897. , true
  898. );
  899. bgfx::ProgramHandle textureCube2Program = bgfx::createProgram(
  900. vsTexture
  901. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube2")
  902. , true
  903. );
  904. bgfx::ProgramHandle textureSdfProgram = bgfx::createProgram(
  905. vsTexture
  906. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_sdf")
  907. , true
  908. );
  909. bgfx::ProgramHandle texture3DProgram = bgfx::createProgram(
  910. vsTexture
  911. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_3d")
  912. , true
  913. );
  914. const uint32_t checkerBoardSize = 64;
  915. bgfx::TextureHandle checkerBoard;
  916. {
  917. const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize*checkerBoardSize*4);
  918. bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xff8e8e8e, 0xff5d5d5d);
  919. checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
  920. , bgfx::TextureFormat::BGRA8
  921. , 0
  922. | BGFX_TEXTURE_MIN_POINT
  923. | BGFX_TEXTURE_MIP_POINT
  924. | BGFX_TEXTURE_MAG_POINT
  925. , mem
  926. );
  927. }
  928. float speed = 0.37f;
  929. float time = 0.0f;
  930. Interpolator mip(0.0f);
  931. Interpolator layer(0.0f);
  932. Interpolator zoom(1.0f);
  933. Interpolator scale(1.0f);
  934. Interpolator posx(0.0f);
  935. Interpolator posy(0.0f);
  936. InterpolatorAngle angle(0.0f);
  937. InterpolatorAngle angx(0.0f);
  938. InterpolatorAngle angy(0.0f);
  939. const char* filePath = _argc < 2 ? "" : _argv[1];
  940. std::string path = filePath;
  941. {
  942. bx::FilePath fp(filePath);
  943. view.updateFileList(fp);
  944. }
  945. int exitcode = bx::kExitSuccess;
  946. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  947. if (view.m_fileList.empty() )
  948. {
  949. exitcode = bx::kExitFailure;
  950. if (2 > _argc)
  951. {
  952. help("File path is not specified.");
  953. }
  954. else
  955. {
  956. fprintf(stderr, "Unable to load '%s' texture.\n", filePath);
  957. }
  958. }
  959. else
  960. {
  961. uint32_t fileIndex = 0;
  962. bool dragging = false;
  963. entry::MouseState mouseStatePrev;
  964. entry::MouseState mouseState;
  965. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  966. {
  967. imguiBeginFrame(mouseState.m_mx
  968. , mouseState.m_my
  969. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  970. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  971. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  972. , mouseState.m_mz
  973. , uint16_t(width)
  974. , uint16_t(height)
  975. );
  976. static bool help = false;
  977. static bool mouseDelta = false;
  978. if (!mouseDelta)
  979. {
  980. mouseStatePrev = mouseState;
  981. mouseDelta = true;
  982. }
  983. int32_t zoomDelta = mouseState.m_mz - mouseStatePrev.m_mz;
  984. if (zoomDelta != 0)
  985. {
  986. char exec[64];
  987. bx::snprintf(exec, BX_COUNTOF(exec), "view zoom %+f", -zoomDelta*0.1f);
  988. cmdExec(exec);
  989. }
  990. const float xDelta = float(mouseStatePrev.m_mx - mouseState.m_mx);
  991. const float yDelta = float(mouseStatePrev.m_my - mouseState.m_my);
  992. if (!ImGui::MouseOverArea()
  993. && !help
  994. && mouseState.m_buttons[entry::MouseButton::Left] != mouseStatePrev.m_buttons[entry::MouseButton::Left])
  995. {
  996. dragging = !!mouseState.m_buttons[entry::MouseButton::Left];
  997. }
  998. if (dragging)
  999. {
  1000. if (view.m_info.cubeMap
  1001. && Geometry::Quad == view.m_cubeMapGeo)
  1002. {
  1003. char exec[64];
  1004. bx::snprintf(exec, BX_COUNTOF(exec), "view cubemap %+f %+f", -yDelta, -xDelta);
  1005. cmdExec(exec);
  1006. }
  1007. else
  1008. {
  1009. char exec[64];
  1010. bx::snprintf(exec, BX_COUNTOF(exec), "view pan %+f %+f", xDelta, yDelta);
  1011. cmdExec(exec);
  1012. }
  1013. }
  1014. mouseStatePrev = mouseState;
  1015. if (ImGui::BeginPopupContextVoid("Menu") )
  1016. {
  1017. if (ImGui::MenuItem("Files", NULL, view.m_files) )
  1018. {
  1019. cmdExec("view files");
  1020. }
  1021. // if (ImGui::MenuItem("Save As") )
  1022. {
  1023. }
  1024. if (ImGui::MenuItem("Reset") )
  1025. {
  1026. cmdExec(s_resetCmd);
  1027. }
  1028. ImGui::Separator();
  1029. if (ImGui::BeginMenu("Options"))
  1030. {
  1031. bool filter = view.m_filter;
  1032. if (ImGui::MenuItem("Filter", NULL, &filter) )
  1033. {
  1034. cmdExec("view filter");
  1035. }
  1036. if (ImGui::BeginMenu("Cubemap", view.m_info.cubeMap) )
  1037. {
  1038. if (ImGui::MenuItem("Quad", NULL, Geometry::Quad == view.m_cubeMapGeo) )
  1039. {
  1040. cmdExec("view geo quad");
  1041. }
  1042. if (ImGui::MenuItem("Cross", NULL, Geometry::Cross == view.m_cubeMapGeo) )
  1043. {
  1044. cmdExec("view geo cross");
  1045. }
  1046. if (ImGui::MenuItem("Hexagon", NULL, Geometry::Hexagon == view.m_cubeMapGeo) )
  1047. {
  1048. cmdExec("view geo hexagon");
  1049. }
  1050. ImGui::EndMenu();
  1051. }
  1052. bool rr = 0 != (view.m_abgr & 0x000000ff);
  1053. if (ImGui::MenuItem("R", NULL, &rr) )
  1054. {
  1055. cmdExec("view rgb r");
  1056. }
  1057. bool gg = 0 != (view.m_abgr & 0x0000ff00);
  1058. if (ImGui::MenuItem("G", NULL, &gg) )
  1059. {
  1060. cmdExec("view rgb g");
  1061. }
  1062. bool bb = 0 != (view.m_abgr & 0x00ff0000);
  1063. if (ImGui::MenuItem("B", NULL, &bb) )
  1064. {
  1065. cmdExec("view rgb b");
  1066. }
  1067. bool alpha = view.m_alpha;
  1068. if (ImGui::MenuItem("Checkerboard", NULL, &alpha) )
  1069. {
  1070. cmdExec("view rgb a");
  1071. }
  1072. ImGui::EndMenu();
  1073. }
  1074. ImGui::Separator();
  1075. if (ImGui::MenuItem("Help") )
  1076. {
  1077. cmdExec("view help");
  1078. }
  1079. ImGui::Separator();
  1080. if (ImGui::MenuItem("Exit") )
  1081. {
  1082. cmdExec("exit");
  1083. }
  1084. ImGui::EndPopup();
  1085. }
  1086. if (help != view.m_help)
  1087. {
  1088. if (!help)
  1089. {
  1090. ImGui::OpenPopup("Help");
  1091. inputRemoveBindings(s_bindingName[Binding::View]);
  1092. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  1093. }
  1094. else
  1095. {
  1096. inputRemoveBindings(s_bindingName[Binding::Help]);
  1097. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1098. }
  1099. help = view.m_help;
  1100. }
  1101. if (view.m_files)
  1102. {
  1103. char temp[bx::kMaxFilePath];
  1104. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.get() );
  1105. if (ImGui::Begin(temp, NULL, ImVec2(400.0f, 400.0f) ) )
  1106. {
  1107. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  1108. {
  1109. ImGui::PushFont(ImGui::Font::Mono);
  1110. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  1111. const float listHeight =
  1112. std::max(1.0f, bx::ffloor(ImGui::GetWindowHeight()/itemHeight) )
  1113. * itemHeight
  1114. ;
  1115. ImGui::PushItemWidth(-1);
  1116. if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
  1117. {
  1118. const int32_t itemCount = int32_t(view.m_fileList.size() );
  1119. int32_t start, end;
  1120. ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);
  1121. const int32_t index = int32_t(view.m_fileIndex);
  1122. if (index <= start)
  1123. {
  1124. ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
  1125. }
  1126. else if (index >= end)
  1127. {
  1128. ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
  1129. }
  1130. ImGuiListClipper clipper(itemCount, itemHeight);
  1131. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  1132. {
  1133. ImGui::PushID(pos);
  1134. bool isSelected = uint32_t(pos) == view.m_fileIndex;
  1135. if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
  1136. {
  1137. view.m_fileIndex = pos;
  1138. }
  1139. ImGui::PopID();
  1140. }
  1141. clipper.End();
  1142. ImGui::ListBoxFooter();
  1143. }
  1144. ImGui::PopFont();
  1145. ImGui::EndChild();
  1146. }
  1147. ImGui::End();
  1148. }
  1149. }
  1150. if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize) )
  1151. {
  1152. ImGui::SetWindowFontScale(1.0f);
  1153. ImGui::Text(
  1154. "texturev, bgfx texture viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
  1155. "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
  1156. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  1157. , BGFX_TEXTUREV_VERSION_MAJOR
  1158. , BGFX_TEXTUREV_VERSION_MINOR
  1159. , BGFX_API_VERSION
  1160. );
  1161. ImGui::Separator();
  1162. ImGui::NextLine();
  1163. ImGui::Text("Key bindings:\n\n");
  1164. ImGui::PushFont(ImGui::Font::Mono);
  1165. keyBindingHelp("ESC", "Exit.");
  1166. keyBindingHelp("h", "Toggle help screen.");
  1167. keyBindingHelp("f", "Toggle full-screen.");
  1168. ImGui::NextLine();
  1169. keyBindingHelp("LMB+drag", "Pan.");
  1170. keyBindingHelp("=/- or MW", "Zoom in/out.");
  1171. keyBindingHelp("z/Z", "Rotate.");
  1172. keyBindingHelp("0", "Reset.");
  1173. keyBindingHelp("1", "Fit to window.");
  1174. ImGui::NextLine();
  1175. keyBindingHelp("<", "Reset MIP level.");
  1176. keyBindingHelp(",/,", "MIP level up/down.");
  1177. keyBindingHelp("/", "Toggle linear/point texture sampling.");
  1178. keyBindingHelp("[space]", "Change cubemap mode.");
  1179. ImGui::NextLine();
  1180. keyBindingHelp("left", "Previous layer in texture array.");
  1181. keyBindingHelp("right", "Next layer in texture array.");
  1182. ImGui::NextLine();
  1183. keyBindingHelp("up", "Previous texture.");
  1184. keyBindingHelp("down", "Next texture.");
  1185. ImGui::NextLine();
  1186. keyBindingHelp("r/g/b", "Toggle R, G, or B color channel.");
  1187. keyBindingHelp("a", "Toggle alpha blending.");
  1188. ImGui::NextLine();
  1189. keyBindingHelp("s", "Toggle Multi-channel SDF rendering");
  1190. ImGui::NextLine();
  1191. ImGui::PopFont();
  1192. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1193. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1194. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1195. || !view.m_help)
  1196. {
  1197. view.m_help = false;
  1198. ImGui::CloseCurrentPopup();
  1199. }
  1200. ImGui::EndPopup();
  1201. }
  1202. imguiEndFrame();
  1203. if (!bgfx::isValid(texture)
  1204. || view.m_fileIndex != fileIndex)
  1205. {
  1206. if (bgfx::isValid(texture) )
  1207. {
  1208. bgfx::destroyTexture(texture);
  1209. }
  1210. fileIndex = view.m_fileIndex;
  1211. bx::FilePath fp = view.m_path;
  1212. fp.join(view.m_fileList[view.m_fileIndex].c_str() );
  1213. texture = loadTexture(fp.get()
  1214. , 0
  1215. | BGFX_TEXTURE_U_CLAMP
  1216. | BGFX_TEXTURE_V_CLAMP
  1217. | BGFX_TEXTURE_W_CLAMP
  1218. , 0
  1219. , &view.m_info
  1220. );
  1221. std::string title;
  1222. if (isValid(texture) )
  1223. {
  1224. const char* name = "";
  1225. if (view.m_info.cubeMap)
  1226. {
  1227. name = " CubeMap";
  1228. }
  1229. else if (1 < view.m_info.depth)
  1230. {
  1231. name = " 3D";
  1232. view.m_info.numLayers = view.m_info.depth;
  1233. }
  1234. else if (1 < view.m_info.numLayers)
  1235. {
  1236. name = " 2D Array";
  1237. }
  1238. bx::stringPrintf(title, "%s (%d x %d%s, mips: %d, layers %d, %s)"
  1239. , fp.get()
  1240. , view.m_info.width
  1241. , view.m_info.height
  1242. , name
  1243. , view.m_info.numMips
  1244. , view.m_info.numLayers
  1245. , bimg::getName(bimg::TextureFormat::Enum(view.m_info.format) )
  1246. );
  1247. }
  1248. else
  1249. {
  1250. bx::stringPrintf(title, "Failed to load %s!", filePath);
  1251. }
  1252. entry::WindowHandle handle = { 0 };
  1253. entry::setWindowTitle(handle, title.c_str() );
  1254. }
  1255. int64_t now = bx::getHPCounter();
  1256. static int64_t last = now;
  1257. const int64_t frameTime = now - last;
  1258. last = now;
  1259. const double freq = double(bx::getHPFrequency() );
  1260. time += (float)(frameTime*speed/freq);
  1261. float transitionTime = dragging ? 0.0f : 0.25f;
  1262. posx.set(view.m_posx, transitionTime);
  1263. posy.set(view.m_posy, transitionTime);
  1264. float ortho[16];
  1265. bx::mtxOrtho(
  1266. ortho
  1267. , 0.0f
  1268. , float(width)
  1269. , float(height)
  1270. , 0.0f
  1271. , 0.0f
  1272. , 1000.0f
  1273. , 0.0f
  1274. , caps->homogeneousDepth
  1275. );
  1276. bgfx::setViewTransform(BACKGROUND_VIEW_ID, NULL, ortho);
  1277. bgfx::setViewRect(BACKGROUND_VIEW_ID, 0, 0, uint16_t(width), uint16_t(height) );
  1278. setGeometry(Geometry::Quad
  1279. , 0
  1280. , 0
  1281. , width
  1282. , height
  1283. , view.m_alpha ? UINT32_MAX : 0
  1284. , float(width )/float(checkerBoardSize)
  1285. , float(height)/float(checkerBoardSize)
  1286. );
  1287. bgfx::setTexture(0
  1288. , s_texColor
  1289. , checkerBoard
  1290. );
  1291. bgfx::setState(0
  1292. | BGFX_STATE_RGB_WRITE
  1293. | BGFX_STATE_ALPHA_WRITE
  1294. );
  1295. bgfx::submit(BACKGROUND_VIEW_ID
  1296. , textureProgram
  1297. );
  1298. float px = posx.getValue();
  1299. float py = posy.getValue();
  1300. bx::mtxOrtho(
  1301. ortho
  1302. , px-width/2.0f
  1303. , px+width/2.0f
  1304. , py+height/2.0f
  1305. , py-height/2.0f
  1306. , 0.0f
  1307. , 1000.0f
  1308. , 0.0f
  1309. , caps->homogeneousDepth
  1310. );
  1311. bgfx::setViewTransform(IMAGE_VIEW_ID, NULL, ortho);
  1312. bgfx::setViewRect(IMAGE_VIEW_ID, 0, 0, uint16_t(width), uint16_t(height) );
  1313. bgfx::dbgTextClear();
  1314. if (view.m_fit)
  1315. {
  1316. scale.set(bx::fmin(float(width) / float(view.m_info.width)
  1317. , float(height) / float(view.m_info.height) )
  1318. , 0.1f
  1319. );
  1320. }
  1321. else
  1322. {
  1323. scale.set(1.0f, 0.1f);
  1324. }
  1325. zoom.set(view.m_zoom, transitionTime);
  1326. angle.set(view.m_angle, transitionTime);
  1327. angx.set(view.m_angx, transitionTime);
  1328. angy.set(view.m_angy, transitionTime);
  1329. float ss = scale.getValue()
  1330. * zoom.getValue()
  1331. ;
  1332. setGeometry(view.m_info.cubeMap ? view.m_cubeMapGeo : Geometry::Quad
  1333. , -int(view.m_info.width * ss)/2
  1334. , -int(view.m_info.height * ss)/2
  1335. , int(view.m_info.width * ss)
  1336. , int(view.m_info.height * ss)
  1337. , view.m_abgr
  1338. );
  1339. float rotz[16];
  1340. bx::mtxRotateZ(rotz, angle.getValue() );
  1341. bgfx::setTransform(rotz);
  1342. float mtx[16];
  1343. bx::mtxRotateXY(mtx, angx.getValue(), angy.getValue() );
  1344. bgfx::setUniform(u_mtx, mtx);
  1345. mip.set(float(view.m_mip), 0.5f);
  1346. layer.set(float(view.m_layer), 0.25f);
  1347. float params[4] = { mip.getValue(), layer.getValue(), 0.0f, 0.0f };
  1348. if (1 < view.m_info.depth)
  1349. {
  1350. params[1] = layer.getValue()/view.m_info.depth;
  1351. }
  1352. bgfx::setUniform(u_params, params);
  1353. const uint32_t textureFlags = 0
  1354. | BGFX_TEXTURE_U_CLAMP
  1355. | BGFX_TEXTURE_V_CLAMP
  1356. | BGFX_TEXTURE_W_CLAMP
  1357. | (view.m_filter ? 0 : 0
  1358. | BGFX_TEXTURE_MIN_POINT
  1359. | BGFX_TEXTURE_MIP_POINT
  1360. | BGFX_TEXTURE_MAG_POINT
  1361. )
  1362. ;
  1363. bgfx::setTexture(0
  1364. , s_texColor
  1365. , texture
  1366. , textureFlags
  1367. );
  1368. bgfx::setState(0
  1369. | BGFX_STATE_RGB_WRITE
  1370. | BGFX_STATE_ALPHA_WRITE
  1371. | (view.m_alpha ? BGFX_STATE_BLEND_ALPHA : BGFX_STATE_NONE)
  1372. );
  1373. bgfx:: ProgramHandle program = textureProgram;
  1374. if (1 < view.m_info.depth)
  1375. {
  1376. program = texture3DProgram;
  1377. }
  1378. else if (view.m_info.cubeMap)
  1379. {
  1380. program = Geometry::Quad == view.m_cubeMapGeo
  1381. ? textureCubeProgram
  1382. : textureCube2Program
  1383. ;
  1384. }
  1385. else if (1 < view.m_info.numLayers)
  1386. {
  1387. program = textureArrayProgram;
  1388. }
  1389. else if (view.m_sdf)
  1390. {
  1391. program = textureSdfProgram;
  1392. }
  1393. bgfx::submit(IMAGE_VIEW_ID, program);
  1394. bgfx::frame();
  1395. }
  1396. }
  1397. if (bgfx::isValid(texture) )
  1398. {
  1399. bgfx::destroyTexture(texture);
  1400. }
  1401. bgfx::destroyTexture(checkerBoard);
  1402. bgfx::destroyUniform(s_texColor);
  1403. bgfx::destroyUniform(u_mtx);
  1404. bgfx::destroyUniform(u_params);
  1405. bgfx::destroyProgram(textureProgram);
  1406. bgfx::destroyProgram(textureArrayProgram);
  1407. bgfx::destroyProgram(textureCubeProgram);
  1408. bgfx::destroyProgram(textureCube2Program);
  1409. bgfx::destroyProgram(textureSdfProgram);
  1410. bgfx::destroyProgram(texture3DProgram);
  1411. imguiDestroy();
  1412. bgfx::shutdown();
  1413. return exitcode;
  1414. }