texturev.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  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 <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. const double freq = double(bx::getHPFrequency() );
  973. int64_t now = bx::getHPCounter();
  974. float time = (float)(double(now - offset) / freq);
  975. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  976. return lerp < 1.0f;
  977. }
  978. };
  979. typedef InterpolatorT<bx::lerp, bx::easeInOutQuad> Interpolator;
  980. typedef InterpolatorT<bx::angleLerp, bx::easeInOutCubic> InterpolatorAngle;
  981. typedef InterpolatorT<bx::lerp, bx::easeLinear> InterpolatorLinear;
  982. void keyBindingHelp(const char* _bindings, const char* _description)
  983. {
  984. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
  985. ImGui::SameLine(100);
  986. ImGui::Text(_description);
  987. }
  988. void associate()
  989. {
  990. #if BX_PLATFORM_WINDOWS
  991. std::string str;
  992. char exec[bx::kMaxFilePath];
  993. GetModuleFileNameA(GetModuleHandleA(NULL), exec, sizeof(exec) );
  994. std::string strExec = bx::replaceAll<std::string>(exec, "\\", "\\\\");
  995. std::string value;
  996. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  997. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  998. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  999. str += value;
  1000. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  1001. str += value;
  1002. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1003. {
  1004. const char* ext = s_supportedExt[ii];
  1005. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  1006. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  1007. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  1008. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  1009. }
  1010. bx::FilePath filePath(bx::Dir::Temp);
  1011. filePath.join("texture.reg");
  1012. bx::FileWriter writer;
  1013. bx::Error err;
  1014. if (bx::open(&writer, filePath, false, &err) )
  1015. {
  1016. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  1017. bx::close(&writer);
  1018. if (err.isOk() )
  1019. {
  1020. std::string cmd;
  1021. bx::stringPrintf(cmd, "/s %s", filePath.getCPtr() );
  1022. bx::ProcessReader reader;
  1023. if (bx::open(&reader, "regedit.exe", cmd.c_str(), &err) )
  1024. {
  1025. bx::close(&reader);
  1026. }
  1027. }
  1028. }
  1029. #elif BX_PLATFORM_LINUX
  1030. std::string str;
  1031. str += "#/bin/bash\n\n";
  1032. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1033. {
  1034. const char* ext = s_supportedExt[ii];
  1035. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  1036. }
  1037. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/x-dds\n");
  1038. str += "\n";
  1039. bx::FileWriter writer;
  1040. bx::Error err;
  1041. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  1042. {
  1043. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  1044. bx::close(&writer);
  1045. if (err.isOk() )
  1046. {
  1047. bx::ProcessReader reader;
  1048. if (bx::open(&reader, "/bin/bash", "/tmp/texturev.sh", &err) )
  1049. {
  1050. bx::close(&reader);
  1051. }
  1052. }
  1053. }
  1054. #endif // BX_PLATFORM_WINDOWS
  1055. }
  1056. void help(const char* _error = NULL)
  1057. {
  1058. if (NULL != _error)
  1059. {
  1060. bx::printf("Error:\n%s\n\n", _error);
  1061. }
  1062. bx::printf(
  1063. "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1064. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  1065. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  1066. , BGFX_TEXTUREV_VERSION_MAJOR
  1067. , BGFX_TEXTUREV_VERSION_MINOR
  1068. , BGFX_API_VERSION
  1069. );
  1070. bx::printf(
  1071. "Usage: texturev <file path>\n"
  1072. "\n"
  1073. "Supported input file types:\n"
  1074. );
  1075. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1076. {
  1077. bx::printf(" *.%s\n", s_supportedExt[ii]);
  1078. }
  1079. bx::printf(
  1080. "\n"
  1081. "Options:\n"
  1082. " -h, --help Help.\n"
  1083. " -v, --version Version information only.\n"
  1084. " --associate Associate file extensions with texturev.\n"
  1085. "\n"
  1086. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  1087. );
  1088. }
  1089. int _main_(int _argc, char** _argv)
  1090. {
  1091. bx::CommandLine cmdLine(_argc, _argv);
  1092. if (cmdLine.hasArg('v', "version") )
  1093. {
  1094. bx::printf(
  1095. "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1096. , BGFX_TEXTUREV_VERSION_MAJOR
  1097. , BGFX_TEXTUREV_VERSION_MINOR
  1098. , BGFX_API_VERSION
  1099. );
  1100. return bx::kExitSuccess;
  1101. }
  1102. if (cmdLine.hasArg('h', "help") )
  1103. {
  1104. help();
  1105. return bx::kExitFailure;
  1106. }
  1107. else if (cmdLine.hasArg("associate") )
  1108. {
  1109. associate();
  1110. return bx::kExitFailure;
  1111. }
  1112. uint32_t debug = BGFX_DEBUG_TEXT;
  1113. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  1114. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1115. View view;
  1116. cmdAdd("view", cmdView, &view);
  1117. entry::setWindowFlags(entry::WindowHandle{0}, ENTRY_WINDOW_FLAG_ASPECT_RATIO, false);
  1118. entry::setWindowSize(entry::WindowHandle{0}, view.m_width, view.m_height);
  1119. bgfx::Init init;
  1120. init.resolution.width = view.m_width;
  1121. init.resolution.width = view.m_height;
  1122. init.resolution.reset = BGFX_RESET_VSYNC;
  1123. bgfx::init(init);
  1124. // Set view 0 clear state.
  1125. bgfx::setViewClear(BACKGROUND_VIEW_ID
  1126. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  1127. , 0x000000ff
  1128. , 1.0f
  1129. , 0
  1130. );
  1131. imguiCreate();
  1132. PosUvwColorVertex::init();
  1133. const bgfx::Caps* caps = bgfx::getCaps();
  1134. bgfx::RendererType::Enum type = caps->rendererType;
  1135. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  1136. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  1137. bgfx::UniformHandle u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Vec4);
  1138. bgfx::UniformHandle u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Vec4);
  1139. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  1140. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  1141. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  1142. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  1143. vsTexture
  1144. , fsTexture
  1145. , true
  1146. );
  1147. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  1148. vsTexture
  1149. , bgfx::isValid(fsTextureArray)
  1150. ? fsTextureArray
  1151. : fsTexture
  1152. , true
  1153. );
  1154. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  1155. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  1156. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  1157. , true
  1158. );
  1159. bgfx::ProgramHandle textureCube2Program = bgfx::createProgram(
  1160. vsTexture
  1161. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube2")
  1162. , true
  1163. );
  1164. bgfx::ProgramHandle textureSdfProgram = bgfx::createProgram(
  1165. vsTexture
  1166. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_sdf")
  1167. , true
  1168. );
  1169. bgfx::ProgramHandle textureMsdfProgram = bgfx::createProgram(
  1170. vsTexture
  1171. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_msdf")
  1172. , true
  1173. );
  1174. bgfx::ProgramHandle texture3DProgram = bgfx::createProgram(
  1175. vsTexture
  1176. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_3d")
  1177. , true
  1178. );
  1179. const uint32_t checkerBoardSize = 64;
  1180. bgfx::TextureHandle checkerBoard;
  1181. {
  1182. const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize*checkerBoardSize*4);
  1183. bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xff8e8e8e, 0xff5d5d5d);
  1184. checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
  1185. , bgfx::TextureFormat::BGRA8
  1186. , 0
  1187. | BGFX_SAMPLER_MIN_POINT
  1188. | BGFX_SAMPLER_MIP_POINT
  1189. | BGFX_SAMPLER_MAG_POINT
  1190. , mem
  1191. );
  1192. }
  1193. float speed = 0.37f;
  1194. float time = 0.0f;
  1195. Interpolator menuFade(5.0f);
  1196. Interpolator mip(0.0f);
  1197. Interpolator layer(0.0f);
  1198. InterpolatorLinear ev(0.0f);
  1199. Interpolator zoom(1.0f);
  1200. Interpolator scale(1.0f);
  1201. Interpolator posx(0.0f);
  1202. Interpolator posy(0.0f);
  1203. InterpolatorAngle angle(0.0f);
  1204. InterpolatorAngle angx(0.0f);
  1205. InterpolatorAngle angy(0.0f);
  1206. auto anyActive = [&]() -> bool
  1207. {
  1208. return false
  1209. || ImGui::MouseOverArea()
  1210. || menuFade.isActive()
  1211. || mip.isActive()
  1212. || layer.isActive()
  1213. || ev.isActive()
  1214. || zoom.isActive()
  1215. || scale.isActive()
  1216. || posx.isActive()
  1217. || posy.isActive()
  1218. || angle.isActive()
  1219. || angx.isActive()
  1220. || angy.isActive()
  1221. ;
  1222. };
  1223. const char* filePath = _argc < 2 ? "" : _argv[1];
  1224. std::string path = filePath;
  1225. {
  1226. bx::FilePath fp(filePath);
  1227. view.updateFileList(fp);
  1228. }
  1229. int exitcode = bx::kExitSuccess;
  1230. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  1231. {
  1232. uint32_t fileIndex = 0;
  1233. bool dragging = false;
  1234. entry::WindowState windowState;
  1235. entry::MouseState mouseStatePrev;
  1236. while (!entry::processWindowEvents(windowState, debug, init.resolution.reset) )
  1237. {
  1238. const entry::MouseState& mouseState = windowState.m_mouse;
  1239. view.m_width = windowState.m_width;
  1240. view.m_height = windowState.m_height;
  1241. if (!windowState.m_dropFile.isEmpty() )
  1242. {
  1243. view.updateFileList(windowState.m_dropFile);
  1244. windowState.m_dropFile.clear();
  1245. }
  1246. imguiBeginFrame(mouseState.m_mx
  1247. , mouseState.m_my
  1248. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1249. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1250. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1251. , mouseState.m_mz
  1252. , uint16_t(view.m_width)
  1253. , uint16_t(view.m_height)
  1254. );
  1255. bool modalWindow = view.m_help || view.m_about;
  1256. bool overArea = false
  1257. || ImGui::GetMousePos().y <= ImGui::GetTextLineHeightWithSpacing()
  1258. || ImGui::MouseOverArea()
  1259. ;
  1260. overArea &= !modalWindow;
  1261. if (overArea)
  1262. {
  1263. menuFade.set(5.0f, 0.25f);
  1264. }
  1265. else if (modalWindow)
  1266. {
  1267. menuFade.reset(0.0f);
  1268. }
  1269. else
  1270. {
  1271. menuFade.set(0.0f, 2.0f);
  1272. }
  1273. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, bx::clamp(menuFade.getValue(), 0.0f, 1.0f) );
  1274. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  1275. if (ImGui::BeginMainMenuBar() )
  1276. {
  1277. if (ImGui::BeginMenu("File"))
  1278. {
  1279. if (ImGui::MenuItem("Open File") )
  1280. {
  1281. bx::FilePath tmp = view.m_path;
  1282. if (openFileSelectionDialog(
  1283. tmp
  1284. , FileSelectionDialogType::Open
  1285. , "texturev: Open File"
  1286. , s_filter
  1287. ) )
  1288. {
  1289. view.updateFileList(tmp);
  1290. }
  1291. }
  1292. if (ImGui::MenuItem("Show File List", NULL, view.m_files) )
  1293. {
  1294. cmdExec("view files");
  1295. }
  1296. ImGui::Separator();
  1297. if (ImGui::MenuItem("Exit") )
  1298. {
  1299. cmdExec("exit");
  1300. }
  1301. ImGui::EndMenu();
  1302. }
  1303. if (ImGui::BeginMenu("View") )
  1304. {
  1305. if (ImGui::MenuItem("Info", NULL, view.m_info) )
  1306. {
  1307. cmdExec("view info");
  1308. }
  1309. if (ImGui::MenuItem("Reset") )
  1310. {
  1311. cmdExec(s_resetCmd);
  1312. }
  1313. ImGui::Separator();
  1314. bool filter = view.m_filter;
  1315. if (ImGui::MenuItem("Filter", NULL, &filter) )
  1316. {
  1317. cmdExec("view filter");
  1318. }
  1319. bool animate = 0.0f < view.m_transitionTime;
  1320. if (ImGui::MenuItem("Animate", NULL, &animate) )
  1321. {
  1322. cmdExec("view transition %f", animate ? 1.0f : 0.0f);
  1323. }
  1324. if (ImGui::BeginMenu("Cubemap", view.m_textureInfo.cubeMap) )
  1325. {
  1326. if (ImGui::MenuItem("Quad", NULL, Geometry::Quad == view.m_cubeMapGeo) )
  1327. {
  1328. cmdExec("view geo quad");
  1329. }
  1330. if (ImGui::MenuItem("Cross", NULL, Geometry::Cross == view.m_cubeMapGeo) )
  1331. {
  1332. cmdExec("view geo cross");
  1333. }
  1334. if (ImGui::MenuItem("Hexagon", NULL, Geometry::Hexagon == view.m_cubeMapGeo) )
  1335. {
  1336. cmdExec("view geo hexagon");
  1337. }
  1338. ImGui::EndMenu();
  1339. }
  1340. if (ImGui::BeginMenu("Output") )
  1341. {
  1342. const bool hdrCap = (bgfx::getCaps()->supported & BGFX_CAPS_HDR10);
  1343. if (ImGui::MenuItem("sRGB", NULL, Output::sRGB == view.m_outputFormat) )
  1344. {
  1345. cmdExec("view output srgb");
  1346. }
  1347. if (hdrCap)
  1348. {
  1349. if (ImGui::MenuItem("scRGB", NULL, Output::scRGB == view.m_outputFormat) )
  1350. {
  1351. cmdExec("view output scrgb");
  1352. }
  1353. if (ImGui::MenuItem("HDR10", NULL, Output::HDR10 == view.m_outputFormat) )
  1354. {
  1355. cmdExec("view output hdr10");
  1356. }
  1357. }
  1358. ImGui::EndMenu();
  1359. }
  1360. bool sdf = view.m_sdf;
  1361. if (ImGui::MenuItem("SDF", NULL, &sdf) )
  1362. {
  1363. cmdExec("view sdf");
  1364. }
  1365. bool rr = 0 != (view.m_abgr & 0x000000ff);
  1366. if (ImGui::MenuItem("R", NULL, &rr) )
  1367. {
  1368. cmdExec("view rgb r");
  1369. }
  1370. bool gg = 0 != (view.m_abgr & 0x0000ff00);
  1371. if (ImGui::MenuItem("G", NULL, &gg) )
  1372. {
  1373. cmdExec("view rgb g");
  1374. }
  1375. bool bb = 0 != (view.m_abgr & 0x00ff0000);
  1376. if (ImGui::MenuItem("B", NULL, &bb) )
  1377. {
  1378. cmdExec("view rgb b");
  1379. }
  1380. bool alpha = view.m_alpha;
  1381. if (ImGui::MenuItem("Checkerboard", NULL, &alpha) )
  1382. {
  1383. cmdExec("view rgb a");
  1384. }
  1385. ImGui::Separator();
  1386. if (ImGui::MenuItem("Save Options") )
  1387. {
  1388. cmdExec("view save");
  1389. }
  1390. ImGui::EndMenu();
  1391. }
  1392. if (ImGui::BeginMenu("Help") )
  1393. {
  1394. if (ImGui::MenuItem("View Help") )
  1395. {
  1396. cmdExec("view help");
  1397. }
  1398. ImGui::Separator();
  1399. if (ImGui::MenuItem("About") )
  1400. {
  1401. cmdExec("view about");
  1402. }
  1403. ImGui::EndMenu();
  1404. }
  1405. if (0 != view.m_fileList.size() )
  1406. {
  1407. ImGui::Separator();
  1408. ImGui::TextColored(
  1409. ImVec4(0.0f, 1.0f, 1.0f, 1.0f)
  1410. , view.m_fileList[view.m_fileIndex].c_str()
  1411. );
  1412. ImGui::Separator();
  1413. const char* name = "";
  1414. if (view.m_textureInfo.cubeMap)
  1415. {
  1416. name = " CubeMap";
  1417. }
  1418. else if (1 < view.m_textureInfo.depth)
  1419. {
  1420. name = " 3D";
  1421. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1422. }
  1423. else if (1 < view.m_textureInfo.numLayers)
  1424. {
  1425. name = " 2D Array";
  1426. }
  1427. ImGui::Text("%d x %d%s, mips: %d, layers %d, %s"
  1428. , view.m_textureInfo.width
  1429. , view.m_textureInfo.height
  1430. , name
  1431. , view.m_textureInfo.numMips
  1432. , view.m_textureInfo.numLayers
  1433. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1434. );
  1435. }
  1436. ImGui::EndMainMenuBar();
  1437. }
  1438. ImGui::PopStyleVar(2);
  1439. static bool help = false;
  1440. static bool about = false;
  1441. static bool mouseDelta = false;
  1442. if (!mouseDelta)
  1443. {
  1444. mouseStatePrev = mouseState;
  1445. mouseDelta = true;
  1446. }
  1447. int32_t zoomDelta = overArea ? 0 : mouseState.m_mz - mouseStatePrev.m_mz;
  1448. if (zoomDelta != 0)
  1449. {
  1450. char exec[64];
  1451. bx::snprintf(exec, BX_COUNTOF(exec), "view zoom %+f", -zoomDelta*0.1f);
  1452. cmdExec(exec);
  1453. }
  1454. const float xDelta = float(mouseStatePrev.m_mx - mouseState.m_mx);
  1455. const float yDelta = float(mouseStatePrev.m_my - mouseState.m_my);
  1456. if (!overArea
  1457. && !help
  1458. && mouseState.m_buttons[entry::MouseButton::Left] != mouseStatePrev.m_buttons[entry::MouseButton::Left])
  1459. {
  1460. dragging = !!mouseState.m_buttons[entry::MouseButton::Left];
  1461. }
  1462. if (dragging)
  1463. {
  1464. if (view.m_textureInfo.cubeMap
  1465. && Geometry::Quad == view.m_cubeMapGeo)
  1466. {
  1467. char exec[64];
  1468. bx::snprintf(exec, BX_COUNTOF(exec), "view cubemap %+f %+f", -yDelta, -xDelta);
  1469. cmdExec(exec);
  1470. }
  1471. else
  1472. {
  1473. char exec[64];
  1474. bx::snprintf(exec, BX_COUNTOF(exec), "view pan %+f %+f", xDelta, yDelta);
  1475. cmdExec(exec);
  1476. }
  1477. }
  1478. mouseStatePrev = mouseState;
  1479. if (help != view.m_help)
  1480. {
  1481. if (!help)
  1482. {
  1483. ImGui::OpenPopup("Help");
  1484. inputRemoveBindings(s_bindingName[Binding::View]);
  1485. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  1486. }
  1487. else
  1488. {
  1489. inputRemoveBindings(s_bindingName[Binding::Help]);
  1490. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1491. }
  1492. help = view.m_help;
  1493. }
  1494. if (about != view.m_about)
  1495. {
  1496. if (!about)
  1497. {
  1498. ImGui::OpenPopup("About");
  1499. inputRemoveBindings(s_bindingName[Binding::View]);
  1500. inputAddBindings(s_bindingName[Binding::About], s_binding[Binding::About]);
  1501. }
  1502. else
  1503. {
  1504. inputRemoveBindings(s_bindingName[Binding::About]);
  1505. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1506. }
  1507. about = view.m_about;
  1508. }
  1509. if (view.m_info)
  1510. {
  1511. ImGui::SetNextWindowSize(
  1512. ImVec2(300.0f, 320.0f)
  1513. , ImGuiCond_FirstUseEver
  1514. );
  1515. if (ImGui::Begin("Info", &view.m_info) )
  1516. {
  1517. if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
  1518. {
  1519. if (!bgfx::isValid(texture) )
  1520. {
  1521. ImGui::Text("Texture is not loaded.");
  1522. }
  1523. else
  1524. {
  1525. ImGui::Text("Name: %s", view.m_fileList[view.m_fileIndex].c_str() );
  1526. ImGui::Text("Dimensions: %d x %d"
  1527. , view.m_textureInfo.width
  1528. , view.m_textureInfo.height
  1529. );
  1530. ImGui::Text("Format: %s"
  1531. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1532. );
  1533. uint32_t numLayers = view.m_textureInfo.numLayers;
  1534. if (1 < view.m_textureInfo.depth)
  1535. {
  1536. numLayers = bx::max(1, view.m_textureInfo.depth >> view.m_mip);
  1537. }
  1538. view.m_layer = bx::clamp<int32_t>(view.m_layer, 0, numLayers - 1);
  1539. ImGui::SliderInt("Layer", (int32_t*)&view.m_layer, 0, numLayers - 1);
  1540. ImGui::SliderInt("Mip", (int32_t*)&view.m_mip, 0, view.m_textureInfo.numMips - 1);
  1541. ImGui::Separator();
  1542. ImGui::Checkbox("Input linear", &view.m_inLinear);
  1543. ImGui::RangeSliderFloat("EV range", &view.m_evMin, &view.m_evMax, kEvMin, kEvMax);
  1544. ImGui::SliderFloat("EV", &view.m_ev, view.m_evMin, view.m_evMax);
  1545. ImGui::Separator();
  1546. ImGui::Checkbox("Fit to window", &view.m_fit);
  1547. ImGui::SliderFloat("Scale", &view.m_zoom, 0.01f, 10.0f);
  1548. }
  1549. ImGui::EndChild();
  1550. }
  1551. }
  1552. ImGui::End();
  1553. }
  1554. if (view.m_files)
  1555. {
  1556. char temp[bx::kMaxFilePath];
  1557. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.getCPtr() );
  1558. ImGui::SetNextWindowSize(
  1559. ImVec2(400.0f, 400.0f)
  1560. , ImGuiCond_FirstUseEver
  1561. );
  1562. if (ImGui::Begin(temp, &view.m_files) )
  1563. {
  1564. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  1565. {
  1566. ImGui::PushFont(ImGui::Font::Mono);
  1567. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  1568. const float listHeight =
  1569. bx::max(1.0f, bx::floor(ImGui::GetWindowHeight()/itemHeight) )
  1570. * itemHeight
  1571. ;
  1572. ImGui::PushItemWidth(-1);
  1573. if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
  1574. {
  1575. const int32_t itemCount = int32_t(view.m_fileList.size() );
  1576. int32_t start, end;
  1577. ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);
  1578. const int32_t index = int32_t(view.m_fileIndex);
  1579. if (index <= start)
  1580. {
  1581. ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
  1582. }
  1583. else if (index >= end)
  1584. {
  1585. ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
  1586. }
  1587. ImGuiListClipper clipper(itemCount, itemHeight);
  1588. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  1589. {
  1590. ImGui::PushID(pos);
  1591. bool isSelected = uint32_t(pos) == view.m_fileIndex;
  1592. if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
  1593. {
  1594. view.m_fileIndex = pos;
  1595. }
  1596. ImGui::PopID();
  1597. }
  1598. clipper.End();
  1599. ImGui::ListBoxFooter();
  1600. }
  1601. ImGui::PopFont();
  1602. ImGui::EndChild();
  1603. }
  1604. }
  1605. ImGui::End();
  1606. }
  1607. if (ImGui::BeginPopupModal("About", &view.m_about, ImGuiWindowFlags_AlwaysAutoResize) )
  1608. {
  1609. ImGui::SetWindowFontScale(1.0f);
  1610. ImGui::Text(
  1611. "texturev, bgfx texture viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
  1612. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  1613. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  1614. , BGFX_TEXTUREV_VERSION_MAJOR
  1615. , BGFX_TEXTUREV_VERSION_MINOR
  1616. , BGFX_API_VERSION
  1617. );
  1618. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1619. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1620. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1621. || !view.m_about)
  1622. {
  1623. view.m_about = false;
  1624. ImGui::CloseCurrentPopup();
  1625. }
  1626. ImGui::EndPopup();
  1627. }
  1628. if (ImGui::BeginPopupModal("Help", &view.m_help, ImGuiWindowFlags_AlwaysAutoResize) )
  1629. {
  1630. ImGui::SetWindowFontScale(1.0f);
  1631. ImGui::Text("Key bindings:\n\n");
  1632. ImGui::PushFont(ImGui::Font::Mono);
  1633. keyBindingHelp("ESC", "Exit.");
  1634. keyBindingHelp("h", "Toggle help screen.");
  1635. keyBindingHelp("f", "Toggle full-screen.");
  1636. ImGui::NextLine();
  1637. keyBindingHelp("LMB+drag", "Pan.");
  1638. keyBindingHelp("=/- or MW", "Zoom in/out.");
  1639. keyBindingHelp("z/Z", "Rotate.");
  1640. keyBindingHelp("0", "Reset.");
  1641. keyBindingHelp("1", "Fit to window.");
  1642. ImGui::NextLine();
  1643. keyBindingHelp("<", "Reset MIP level.");
  1644. keyBindingHelp(",/,", "MIP level up/down.");
  1645. keyBindingHelp("/", "Toggle linear/point texture sampling.");
  1646. keyBindingHelp("[space]", "Change cubemap mode.");
  1647. ImGui::NextLine();
  1648. keyBindingHelp("left", "Previous layer in texture array.");
  1649. keyBindingHelp("right", "Next layer in texture array.");
  1650. ImGui::NextLine();
  1651. keyBindingHelp("up", "Previous texture.");
  1652. keyBindingHelp("down", "Next texture.");
  1653. ImGui::NextLine();
  1654. keyBindingHelp("r/g/b", "Toggle R, G, or B color channel.");
  1655. keyBindingHelp("a", "Toggle alpha blending.");
  1656. ImGui::NextLine();
  1657. keyBindingHelp("s", "Toggle Multi-channel SDF rendering");
  1658. ImGui::NextLine();
  1659. ImGui::PopFont();
  1660. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1661. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1662. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1663. || !view.m_help)
  1664. {
  1665. view.m_help = false;
  1666. ImGui::CloseCurrentPopup();
  1667. }
  1668. ImGui::EndPopup();
  1669. }
  1670. imguiEndFrame();
  1671. if ( (!bgfx::isValid(texture) || view.m_fileIndex != fileIndex)
  1672. && 0 != view.m_fileList.size() )
  1673. {
  1674. if (bgfx::isValid(texture) )
  1675. {
  1676. bgfx::destroy(texture);
  1677. }
  1678. fileIndex = view.m_fileIndex;
  1679. bx::FilePath fp = view.m_path;
  1680. fp.join(view.m_fileList[view.m_fileIndex].c_str() );
  1681. bimg::Orientation::Enum orientation;
  1682. texture = loadTexture(fp.getCPtr()
  1683. , 0
  1684. | BGFX_SAMPLER_U_CLAMP
  1685. | BGFX_SAMPLER_V_CLAMP
  1686. | BGFX_SAMPLER_W_CLAMP
  1687. , 0
  1688. , &view.m_textureInfo
  1689. , &orientation
  1690. );
  1691. bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(view.m_textureInfo.format);
  1692. if (format < bimg::TextureFormat::Count)
  1693. {
  1694. view.m_inLinear = bimg::isFloat(format);
  1695. switch (orientation)
  1696. {
  1697. default:
  1698. case bimg::Orientation::R0: cmdExec("view orientation\nview orientation z 0"); break;
  1699. case bimg::Orientation::R90: cmdExec("view orientation\nview orientation z -90"); break;
  1700. case bimg::Orientation::R180: cmdExec("view orientation\nview orientation z -180"); break;
  1701. case bimg::Orientation::R270: cmdExec("view orientation\nview orientation z -270"); break;
  1702. case bimg::Orientation::HFlip: cmdExec("view orientation\nview orientation x -180"); break;
  1703. case bimg::Orientation::HFlipR90: cmdExec("view orientation\nview orientation z -90\nview orientation x -180"); break;
  1704. case bimg::Orientation::HFlipR270: cmdExec("view orientation\nview orientation z -270\nview orientation x -180"); break;
  1705. case bimg::Orientation::VFlip: cmdExec("view orientation\nview orientation y -180"); break;
  1706. }
  1707. }
  1708. std::string title;
  1709. if (isValid(texture) )
  1710. {
  1711. const char* name = "";
  1712. if (view.m_textureInfo.cubeMap)
  1713. {
  1714. name = " CubeMap";
  1715. }
  1716. else if (1 < view.m_textureInfo.depth)
  1717. {
  1718. name = " 3D";
  1719. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1720. }
  1721. else if (1 < view.m_textureInfo.numLayers)
  1722. {
  1723. name = " 2D Array";
  1724. }
  1725. bx::stringPrintf(title, "%s (%d x %d%s, mips: %d, layers %d, %s)"
  1726. , fp.getCPtr()
  1727. , view.m_textureInfo.width
  1728. , view.m_textureInfo.height
  1729. , name
  1730. , view.m_textureInfo.numMips
  1731. , view.m_textureInfo.numLayers
  1732. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1733. );
  1734. }
  1735. else
  1736. {
  1737. bx::stringPrintf(title, "Failed to load %s!", filePath);
  1738. }
  1739. entry::WindowHandle handle = { 0 };
  1740. entry::setWindowTitle(handle, title.c_str() );
  1741. }
  1742. int64_t now = bx::getHPCounter();
  1743. static int64_t last = now;
  1744. const int64_t frameTime = now - last;
  1745. last = now;
  1746. const double freq = double(bx::getHPFrequency() );
  1747. time += (float)(frameTime*speed/freq);
  1748. float transitionTime = dragging ? 0.0f : 0.25f*view.m_transitionTime;
  1749. posx.set(view.m_posx, transitionTime);
  1750. posy.set(view.m_posy, transitionTime);
  1751. float ortho[16];
  1752. bx::mtxOrtho(
  1753. ortho
  1754. , 0.0f
  1755. , float(view.m_width)
  1756. , float(view.m_height)
  1757. , 0.0f
  1758. , 0.0f
  1759. , 1000.0f
  1760. , 0.0f
  1761. , caps->homogeneousDepth
  1762. );
  1763. bgfx::setViewTransform(BACKGROUND_VIEW_ID, NULL, ortho);
  1764. bgfx::setViewRect(BACKGROUND_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  1765. setGeometry(Geometry::Quad
  1766. , 0
  1767. , 0
  1768. , view.m_width
  1769. , view.m_height
  1770. , view.m_alpha || !bgfx::isValid(texture) ? UINT32_MAX : 0
  1771. , float(view.m_width )/float(checkerBoardSize)
  1772. , float(view.m_height)/float(checkerBoardSize)
  1773. );
  1774. bgfx::setTexture(0
  1775. , s_texColor
  1776. , checkerBoard
  1777. );
  1778. bgfx::setState(0
  1779. | BGFX_STATE_WRITE_RGB
  1780. | BGFX_STATE_WRITE_A
  1781. );
  1782. bgfx::submit(BACKGROUND_VIEW_ID
  1783. , textureProgram
  1784. );
  1785. float px = posx.getValue();
  1786. float py = posy.getValue();
  1787. bx::mtxOrtho(
  1788. ortho
  1789. , px-view.m_width/2.0f
  1790. , px+view.m_width/2.0f
  1791. , py+view.m_height/2.0f
  1792. , py-view.m_height/2.0f
  1793. , -10.0f
  1794. , 10.0f
  1795. , 0.0f
  1796. , caps->homogeneousDepth
  1797. );
  1798. bgfx::setViewTransform(IMAGE_VIEW_ID, NULL, ortho);
  1799. bgfx::setViewRect(IMAGE_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  1800. bgfx::dbgTextClear();
  1801. float orientation[16];
  1802. bx::mtxRotateXYZ(orientation, view.m_flipH, view.m_flipV, angle.getValue()+view.m_orientation);
  1803. if (view.m_fit)
  1804. {
  1805. const bx::Vec3 wh = { float(view.m_textureInfo.width), float(view.m_textureInfo.height), 0.0f };
  1806. const bx::Vec3 result = bx::round(bx::abs(bx::mul(wh, orientation) ) );
  1807. scale.set(bx::min(float(view.m_width) / result.x
  1808. , float(view.m_height) / result.y)
  1809. , 0.1f*view.m_transitionTime
  1810. );
  1811. }
  1812. else
  1813. {
  1814. scale.set(1.0f, 0.1f*view.m_transitionTime);
  1815. }
  1816. zoom.set(view.m_zoom, transitionTime);
  1817. angle.set(view.m_angle, transitionTime);
  1818. angx.set(view.m_angx, transitionTime);
  1819. angy.set(view.m_angy, transitionTime);
  1820. float ss = scale.getValue()
  1821. * zoom.getValue()
  1822. ;
  1823. setGeometry(view.m_textureInfo.cubeMap ? view.m_cubeMapGeo : Geometry::Quad
  1824. , -int(view.m_textureInfo.width * ss)/2
  1825. , -int(view.m_textureInfo.height * ss)/2
  1826. , int(view.m_textureInfo.width * ss)
  1827. , int(view.m_textureInfo.height * ss)
  1828. , view.m_abgr
  1829. );
  1830. bgfx::setTransform(orientation);
  1831. float mtx[16];
  1832. bx::mtxRotateXY(mtx, angx.getValue(), angy.getValue() );
  1833. bgfx::setUniform(u_mtx, mtx);
  1834. mip.set(float(view.m_mip), 0.5f*view.m_transitionTime);
  1835. layer.set(float(view.m_layer), 0.25f*view.m_transitionTime);
  1836. ev.set(view.m_ev, 0.5f*view.m_transitionTime);
  1837. float params[4] = { mip.getValue(), layer.getValue(), view.m_inLinear ? 1.0f : 0.0f, ev.getValue() };
  1838. if (1 < view.m_textureInfo.depth)
  1839. {
  1840. params[1] = layer.getValue()/float(bx::max(1, view.m_textureInfo.depth >> view.m_mip) );
  1841. }
  1842. bgfx::setUniform(u_params0, params);
  1843. float params1[4] = { float(view.m_outputFormat), 80.0f, 0.0, 0.0f };
  1844. bgfx::setUniform(u_params1, params1);
  1845. const uint32_t textureFlags = 0
  1846. | BGFX_SAMPLER_U_CLAMP
  1847. | BGFX_SAMPLER_V_CLAMP
  1848. | BGFX_SAMPLER_W_CLAMP
  1849. | (view.m_filter ? 0 : 0
  1850. | BGFX_SAMPLER_MIN_POINT
  1851. | BGFX_SAMPLER_MIP_POINT
  1852. | BGFX_SAMPLER_MAG_POINT
  1853. )
  1854. ;
  1855. bgfx::setTexture(0
  1856. , s_texColor
  1857. , texture
  1858. , textureFlags
  1859. );
  1860. bgfx::setState(0
  1861. | BGFX_STATE_WRITE_RGB
  1862. | BGFX_STATE_WRITE_A
  1863. | (view.m_alpha ? BGFX_STATE_BLEND_ALPHA : BGFX_STATE_NONE)
  1864. );
  1865. bgfx:: ProgramHandle program = textureProgram;
  1866. if (1 < view.m_textureInfo.depth)
  1867. {
  1868. program = texture3DProgram;
  1869. }
  1870. else if (view.m_textureInfo.cubeMap)
  1871. {
  1872. program = Geometry::Quad == view.m_cubeMapGeo
  1873. ? textureCubeProgram
  1874. : textureCube2Program
  1875. ;
  1876. }
  1877. else if (1 < view.m_textureInfo.numLayers)
  1878. {
  1879. program = textureArrayProgram;
  1880. }
  1881. else if (view.m_sdf)
  1882. {
  1883. if (8 < bimg::getBitsPerPixel(bimg::TextureFormat::Enum(view.m_textureInfo.format) ) )
  1884. {
  1885. program = textureMsdfProgram;
  1886. }
  1887. else
  1888. {
  1889. program = textureSdfProgram;
  1890. }
  1891. }
  1892. if (bgfx::isValid(texture) )
  1893. {
  1894. bgfx::submit(IMAGE_VIEW_ID, program);
  1895. }
  1896. else
  1897. {
  1898. bgfx::discard();
  1899. }
  1900. bgfx::frame();
  1901. // Slow down when nothing is animating...
  1902. if (!dragging
  1903. && !anyActive() )
  1904. {
  1905. bx::sleep(100);
  1906. }
  1907. }
  1908. }
  1909. if (bgfx::isValid(texture) )
  1910. {
  1911. bgfx::destroy(texture);
  1912. }
  1913. bgfx::destroy(checkerBoard);
  1914. bgfx::destroy(s_texColor);
  1915. bgfx::destroy(u_mtx);
  1916. bgfx::destroy(u_params0);
  1917. bgfx::destroy(u_params1);
  1918. bgfx::destroy(textureProgram);
  1919. bgfx::destroy(textureArrayProgram);
  1920. bgfx::destroy(textureCubeProgram);
  1921. bgfx::destroy(textureCube2Program);
  1922. bgfx::destroy(textureSdfProgram);
  1923. bgfx::destroy(textureMsdfProgram);
  1924. bgfx::destroy(texture3DProgram);
  1925. imguiDestroy();
  1926. bgfx::shutdown();
  1927. return exitcode;
  1928. }