texturev.cpp 54 KB

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