texturev.cpp 56 KB

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