2
0

texturev.cpp 44 KB

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