texturev.cpp 41 KB

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