texturev.cpp 43 KB

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