texturev.cpp 53 KB

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