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. "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 RendererTypeRemap
  203. {
  204. bx::StringView name;
  205. bgfx::RendererType::Enum type;
  206. };
  207. struct View
  208. {
  209. View()
  210. : m_rendererType(bgfx::RendererType::Count)
  211. , m_cubeMapGeo(Geometry::Quad)
  212. , m_outputFormat(Output::sRGB)
  213. , m_fileIndex(0)
  214. , m_scaleFn(0)
  215. , m_mip(0)
  216. , m_layer(0)
  217. , m_abgr(UINT32_MAX)
  218. , m_ev(0.0f)
  219. , m_evMin(kEvMin)
  220. , m_evMax(kEvMax)
  221. , m_posx(0.0f)
  222. , m_posy(0.0f)
  223. , m_angx(0.0f)
  224. , m_angy(0.0f)
  225. , m_zoom(1.0f)
  226. , m_angle(0.0f)
  227. , m_orientation(0.0f)
  228. , m_flipH(0.0f)
  229. , m_flipV(0.0f)
  230. , m_transitionTime(1.0f)
  231. , m_width(1280)
  232. , m_height(720)
  233. , m_filter(true)
  234. , m_fit(true)
  235. , m_alpha(false)
  236. , m_help(false)
  237. , m_about(false)
  238. , m_info(false)
  239. , m_files(false)
  240. , m_sdf(false)
  241. , m_inLinear(false)
  242. {
  243. load();
  244. m_textureInfo.format = bgfx::TextureFormat::Count;
  245. }
  246. ~View()
  247. {
  248. }
  249. int32_t cmd(int32_t _argc, char const* const* _argv)
  250. {
  251. if (_argc >= 2)
  252. {
  253. if (0 == bx::strCmp(_argv[1], "mip") )
  254. {
  255. if (_argc >= 3)
  256. {
  257. uint32_t mip = m_mip;
  258. if (0 == bx::strCmp(_argv[2], "next") )
  259. {
  260. ++mip;
  261. }
  262. else if (0 == bx::strCmp(_argv[2], "prev") )
  263. {
  264. --mip;
  265. }
  266. else if (0 == bx::strCmp(_argv[2], "last") )
  267. {
  268. mip = INT32_MAX;
  269. }
  270. else
  271. {
  272. bx::fromString(&mip, _argv[2]);
  273. }
  274. m_mip = bx::uint32_iclamp(mip, 0, m_textureInfo.numMips-1);
  275. }
  276. else
  277. {
  278. m_mip = 0;
  279. }
  280. }
  281. else if (0 == bx::strCmp(_argv[1], "layer") )
  282. {
  283. if (_argc >= 3)
  284. {
  285. uint32_t layer = m_layer;
  286. if (0 == bx::strCmp(_argv[2], "next") )
  287. {
  288. ++layer;
  289. }
  290. else if (0 == bx::strCmp(_argv[2], "prev") )
  291. {
  292. --layer;
  293. }
  294. else if (0 == bx::strCmp(_argv[2], "last") )
  295. {
  296. layer = INT32_MAX;
  297. }
  298. else
  299. {
  300. bx::fromString(&layer, _argv[2]);
  301. }
  302. m_layer = bx::uint32_iclamp(layer, 0, m_textureInfo.numLayers-1);
  303. }
  304. else
  305. {
  306. m_layer = 0;
  307. }
  308. }
  309. else if (0 == bx::strCmp(_argv[1], "ev") )
  310. {
  311. if (_argc >= 3)
  312. {
  313. float ev = m_ev;
  314. bx::fromString(&ev, _argv[2]);
  315. m_ev = bx::clamp(ev, kEvMin, kEvMax);
  316. }
  317. else
  318. {
  319. m_ev = 0.0f;
  320. }
  321. }
  322. else if (0 == bx::strCmp(_argv[1], "pan") )
  323. {
  324. if (_argc >= 3)
  325. {
  326. if (_argc >= 4)
  327. {
  328. float yy;
  329. bx::fromString(&yy, _argv[3]);
  330. if (_argv[3][0] == '+'
  331. || _argv[3][0] == '-')
  332. {
  333. m_posy += yy;
  334. }
  335. else
  336. {
  337. m_posy = yy;
  338. }
  339. }
  340. float xx;
  341. bx::fromString(&xx, _argv[2]);
  342. if (_argv[2][0] == '+'
  343. || _argv[2][0] == '-')
  344. {
  345. m_posx += xx;
  346. }
  347. else
  348. {
  349. m_posx = xx;
  350. }
  351. }
  352. else
  353. {
  354. m_posx = 0.0f;
  355. m_posy = 0.0f;
  356. }
  357. }
  358. else if (0 == bx::strCmp(_argv[1], "cubemap") )
  359. {
  360. if (_argc >= 3)
  361. {
  362. if (_argc >= 4)
  363. {
  364. float yy;
  365. bx::fromString(&yy, _argv[3]);
  366. if (_argv[3][0] == '+'
  367. || _argv[3][0] == '-')
  368. {
  369. m_angy += bx::toRad(yy);
  370. }
  371. else
  372. {
  373. m_angy = bx::toRad(yy);
  374. }
  375. }
  376. float xx;
  377. bx::fromString(&xx, _argv[2]);
  378. if (_argv[2][0] == '+'
  379. || _argv[2][0] == '-')
  380. {
  381. m_angx += bx::toRad(xx);
  382. }
  383. else
  384. {
  385. m_angx = bx::toRad(xx);
  386. }
  387. }
  388. else
  389. {
  390. m_angx = 0.0f;
  391. m_angy = 0.0f;
  392. }
  393. }
  394. else if (0 == bx::strCmp(_argv[1], "zoom") )
  395. {
  396. if (_argc >= 3)
  397. {
  398. float zoom;
  399. bx::fromString(&zoom, _argv[2]);
  400. if (_argv[2][0] == '+'
  401. || _argv[2][0] == '-')
  402. {
  403. m_zoom += zoom;
  404. }
  405. else
  406. {
  407. m_zoom = zoom;
  408. }
  409. m_zoom = bx::clamp(m_zoom, 0.01f, 10.0f);
  410. }
  411. else
  412. {
  413. m_zoom = 1.0f;
  414. }
  415. }
  416. else if (0 == bx::strCmp(_argv[1], "rotate") )
  417. {
  418. if (_argc >= 3)
  419. {
  420. float angle;
  421. bx::fromString(&angle, _argv[2]);
  422. if (_argv[2][0] == '+'
  423. || _argv[2][0] == '-')
  424. {
  425. m_angle += bx::toRad(angle);
  426. }
  427. else
  428. {
  429. m_angle = bx::toRad(angle);
  430. }
  431. m_angle = bx::wrap(m_angle, bx::kPi*2.0f);
  432. }
  433. else
  434. {
  435. m_angle = 0.0f;
  436. }
  437. }
  438. else if (0 == bx::strCmp(_argv[1], "orientation") )
  439. {
  440. if (_argc >= 3)
  441. {
  442. float* dst = NULL;
  443. char axis = bx::toLower(_argv[2][0]);
  444. switch (axis)
  445. {
  446. case 'x': dst = &m_flipV; break;
  447. case 'y': dst = &m_flipH; break;
  448. case 'z': dst = &m_orientation; break;
  449. default: break;
  450. }
  451. if (NULL != dst)
  452. {
  453. if (_argc >= 4)
  454. {
  455. float angle;
  456. bx::fromString(&angle, _argv[3]);
  457. *dst = bx::toRad(angle);
  458. }
  459. else
  460. {
  461. *dst = 0.0f;
  462. }
  463. }
  464. }
  465. else
  466. {
  467. m_flipH = 0.0f;
  468. m_flipV = 0.0f;
  469. m_orientation = 0.0f;
  470. }
  471. }
  472. else if (0 == bx::strCmp(_argv[1], "transition") )
  473. {
  474. if (_argc >= 3)
  475. {
  476. float time;
  477. bx::fromString(&time, _argv[2]);
  478. m_transitionTime = bx::clamp(time, 0.0f, 5.0f);
  479. }
  480. else
  481. {
  482. m_transitionTime = 1.0f;
  483. }
  484. }
  485. else if (0 == bx::strCmp(_argv[1], "filter") )
  486. {
  487. if (_argc >= 3)
  488. {
  489. bx::fromString(&m_filter, _argv[2]);
  490. }
  491. else
  492. {
  493. m_filter ^= true;
  494. }
  495. }
  496. else if (0 == bx::strCmp(_argv[1], "fit") )
  497. {
  498. if (_argc >= 3)
  499. {
  500. bx::fromString(&m_fit, _argv[2]);
  501. }
  502. else
  503. {
  504. m_fit ^= true;
  505. }
  506. }
  507. else if (0 == bx::strCmp(_argv[1], "file-up") )
  508. {
  509. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  510. }
  511. else if (0 == bx::strCmp(_argv[1], "file-down") )
  512. {
  513. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  514. ++m_fileIndex;
  515. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  516. }
  517. else if (0 == bx::strCmp(_argv[1], "rgb") )
  518. {
  519. if (_argc >= 3)
  520. {
  521. if (_argv[2][0] == 'r')
  522. {
  523. m_abgr ^= 0x000000ff;
  524. }
  525. else if (_argv[2][0] == 'g')
  526. {
  527. m_abgr ^= 0x0000ff00;
  528. }
  529. else if (_argv[2][0] == 'b')
  530. {
  531. m_abgr ^= 0x00ff0000;
  532. }
  533. else if (_argv[2][0] == 'a')
  534. {
  535. m_alpha ^= true;
  536. }
  537. }
  538. else
  539. {
  540. m_abgr = UINT32_MAX;
  541. m_alpha = false;
  542. }
  543. }
  544. else if (0 == bx::strCmp(_argv[1], "sdf") )
  545. {
  546. m_sdf ^= true;
  547. }
  548. else if (0 == bx::strCmp(_argv[1], "geo") )
  549. {
  550. if (_argc >= 3)
  551. {
  552. if (bx::toLower(_argv[2][0]) == 'c')
  553. {
  554. m_cubeMapGeo = Geometry::Cross;
  555. }
  556. else if (bx::toLower(_argv[2][0]) == 'h')
  557. {
  558. m_cubeMapGeo = Geometry::Hexagon;
  559. }
  560. else
  561. {
  562. m_cubeMapGeo = Geometry::Quad;
  563. }
  564. }
  565. else
  566. {
  567. m_cubeMapGeo = Geometry::Enum( (m_cubeMapGeo + 1) % Geometry::Count);
  568. }
  569. }
  570. else if (0 == bx::strCmp(_argv[1], "output") )
  571. {
  572. Output::Enum outputPrev = m_outputFormat;
  573. if (_argc >= 3)
  574. {
  575. if (0 == bx::strCmp(_argv[2], "srgb") )
  576. {
  577. m_outputFormat = Output::sRGB;
  578. }
  579. else if (0 == bx::strCmp(_argv[2], "scrgb") )
  580. {
  581. m_outputFormat = Output::scRGB;
  582. }
  583. else if (0 == bx::strCmp(_argv[2], "hdr10") )
  584. {
  585. m_outputFormat = Output::HDR10;
  586. }
  587. }
  588. else
  589. {
  590. m_outputFormat = Output::Enum( (m_outputFormat + 1) % Output::Count);
  591. }
  592. if (outputPrev != m_outputFormat)
  593. {
  594. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8;
  595. uint32_t formatFlag = 0;
  596. if (Output::scRGB == m_outputFormat)
  597. {
  598. format = bgfx::TextureFormat::RGBA16F;
  599. }
  600. else if (Output::HDR10 == m_outputFormat)
  601. {
  602. format = bgfx::TextureFormat::RGB10A2;
  603. formatFlag = BGFX_RESET_HDR10;
  604. }
  605. bgfx::reset(m_width, m_height, BGFX_RESET_VSYNC | formatFlag, format);
  606. }
  607. }
  608. else if (0 == bx::strCmp(_argv[1], "help") )
  609. {
  610. m_help ^= true;
  611. }
  612. else if (0 == bx::strCmp(_argv[1], "about") )
  613. {
  614. m_about ^= true;
  615. }
  616. else if (0 == bx::strCmp(_argv[1], "save") )
  617. {
  618. save();
  619. }
  620. else if (0 == bx::strCmp(_argv[1], "info") )
  621. {
  622. m_info ^= true;
  623. }
  624. else if (0 == bx::strCmp(_argv[1], "files") )
  625. {
  626. m_files ^= true;
  627. }
  628. }
  629. return 0;
  630. }
  631. static bool sortNameAscending(const std::string& _lhs, const std::string& _rhs)
  632. {
  633. return 0 > bx::strCmpV(_lhs.c_str(), _rhs.c_str() );
  634. }
  635. void updateFileList(const bx::FilePath& _filePath)
  636. {
  637. bx::DirectoryReader dr;
  638. if (bx::open(&dr, _filePath) )
  639. {
  640. m_path = _filePath;
  641. }
  642. else if (bx::open(&dr, _filePath.getPath() ) )
  643. {
  644. m_path = _filePath.getPath();
  645. }
  646. else
  647. {
  648. DBG("File path `%s` not found.", _filePath.getCPtr() );
  649. return;
  650. }
  651. bx::Error err;
  652. m_fileList.clear();
  653. while (err.isOk() )
  654. {
  655. bx::FileInfo fi;
  656. bx::read(&dr, fi, &err);
  657. if (err.isOk()
  658. && bx::FileType::File == fi.type)
  659. {
  660. bx::StringView ext = fi.filePath.getExt();
  661. if (!ext.isEmpty() )
  662. {
  663. ext.set(ext.getPtr()+1, ext.getTerm() );
  664. bool supported = false;
  665. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  666. {
  667. const bx::StringView supportedExt(s_supportedExt[ii]);
  668. if (0 == bx::strCmpI(bx::max(ext.getPtr(), ext.getTerm() - supportedExt.getLength() ), supportedExt) )
  669. {
  670. supported = true;
  671. break;
  672. }
  673. }
  674. if (supported)
  675. {
  676. const bx::StringView fileName = fi.filePath.getFileName();
  677. m_fileList.push_back(std::string(fileName.getPtr(), fileName.getTerm() ) );
  678. }
  679. }
  680. }
  681. }
  682. bx::close(&dr);
  683. std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);
  684. m_fileIndex = 0;
  685. uint32_t idx = 0;
  686. const bx::StringView fileName = _filePath.getFileName();
  687. for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
  688. {
  689. if (0 == bx::strCmpI(it->c_str(), fileName) )
  690. {
  691. // If it is case-insensitive match then might be correct one, but keep
  692. // searching.
  693. m_fileIndex = idx;
  694. if (0 == bx::strCmp(it->c_str(), fileName) )
  695. {
  696. // If it is exact match we're done.
  697. break;
  698. }
  699. }
  700. }
  701. }
  702. void load()
  703. {
  704. bx::FilePath filePath(bx::Dir::Home);
  705. filePath.join(".config/bgfx/texturev.ini");
  706. bx::Settings settings(entry::getAllocator() );
  707. bx::FileReader reader;
  708. if (bx::open(&reader, filePath) )
  709. {
  710. bx::read(&reader, settings, bx::ErrorAssert{});
  711. bx::close(&reader);
  712. if (!bx::fromString(&m_transitionTime, settings.get("view/transition") ) )
  713. {
  714. m_transitionTime = 1.0f;
  715. }
  716. if (!bx::fromString(&m_width, settings.get("view/width") ) )
  717. {
  718. m_width = 1280;
  719. }
  720. if (!bx::fromString(&m_height, settings.get("view/height") ) )
  721. {
  722. m_height = 720;
  723. }
  724. m_rendererType = getType(settings.get("view/renderer") );
  725. }
  726. }
  727. void save()
  728. {
  729. bx::FilePath filePath(bx::Dir::Home);
  730. filePath.join(".config/bgfx/texturev.ini");
  731. if (bx::makeAll(filePath.getPath() ) )
  732. {
  733. bx::Settings settings(entry::getAllocator() );
  734. char tmp[256];
  735. bx::toString(tmp, sizeof(tmp), m_transitionTime);
  736. settings.set("view/transition", tmp);
  737. bx::toString(tmp, sizeof(tmp), m_width);
  738. settings.set("view/width", tmp);
  739. bx::toString(tmp, sizeof(tmp), m_height);
  740. settings.set("view/height", tmp);
  741. if (m_rendererType != bgfx::RendererType::Count)
  742. {
  743. settings.set("view/renderer", getName(m_rendererType) );
  744. }
  745. bx::FileWriter writer;
  746. if (bx::open(&writer, filePath) )
  747. {
  748. bx::write(&writer, settings, bx::ErrorAssert{});
  749. bx::close(&writer);
  750. }
  751. }
  752. }
  753. bx::FilePath m_path;
  754. typedef stl::vector<std::string> FileList;
  755. FileList m_fileList;
  756. bgfx::RendererType::Enum m_rendererType;
  757. bgfx::TextureInfo m_textureInfo;
  758. Geometry::Enum m_cubeMapGeo;
  759. Output::Enum m_outputFormat;
  760. uint32_t m_fileIndex;
  761. uint32_t m_scaleFn;
  762. uint32_t m_mip;
  763. uint32_t m_layer;
  764. uint32_t m_abgr;
  765. float m_ev;
  766. float m_evMin;
  767. float m_evMax;
  768. float m_posx;
  769. float m_posy;
  770. float m_angx;
  771. float m_angy;
  772. float m_zoom;
  773. float m_angle;
  774. float m_orientation;
  775. float m_flipH;
  776. float m_flipV;
  777. float m_transitionTime;
  778. uint32_t m_width;
  779. uint32_t m_height;
  780. bool m_filter;
  781. bool m_fit;
  782. bool m_alpha;
  783. bool m_help;
  784. bool m_about;
  785. bool m_info;
  786. bool m_files;
  787. bool m_sdf;
  788. bool m_inLinear;
  789. };
  790. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  791. {
  792. View* view = static_cast<View*>(_userData);
  793. return view->cmd(_argc, _argv);
  794. }
  795. struct PosUvwColorVertex
  796. {
  797. float m_x;
  798. float m_y;
  799. float m_u;
  800. float m_v;
  801. float m_w;
  802. uint32_t m_abgr;
  803. static void init()
  804. {
  805. ms_layout
  806. .begin()
  807. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  808. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  809. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  810. .end();
  811. }
  812. void set(float _x, float _y, float _u, float _v, float _w, uint32_t _abgr)
  813. {
  814. m_x = _x;
  815. m_y = _y;
  816. m_u = _u;
  817. m_v = _v;
  818. m_w = _w;
  819. m_abgr = _abgr;
  820. }
  821. static bgfx::VertexLayout ms_layout;
  822. };
  823. bgfx::VertexLayout PosUvwColorVertex::ms_layout;
  824. static uint32_t addQuad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
  825. {
  826. _indices[0] = _idx0;
  827. _indices[1] = _idx3;
  828. _indices[2] = _idx1;
  829. _indices[3] = _idx1;
  830. _indices[4] = _idx3;
  831. _indices[5] = _idx2;
  832. return 6;
  833. }
  834. void setGeometry(
  835. Geometry::Enum _type
  836. , int32_t _x
  837. , int32_t _y
  838. , uint32_t _width
  839. , uint32_t _height
  840. , uint32_t _abgr
  841. , float _maxu = 1.0f
  842. , float _maxv = 1.0f
  843. )
  844. {
  845. if (Geometry::Quad == _type)
  846. {
  847. if (6 == bgfx::getAvailTransientVertexBuffer(6, PosUvwColorVertex::ms_layout) )
  848. {
  849. bgfx::TransientVertexBuffer vb;
  850. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvwColorVertex::ms_layout);
  851. PosUvwColorVertex* vertex = (PosUvwColorVertex*)vb.data;
  852. const float widthf = float(_width);
  853. const float heightf = float(_height);
  854. const float minx = float(_x);
  855. const float miny = float(_y);
  856. const float maxx = minx+widthf;
  857. const float maxy = miny+heightf;
  858. const float minu = 0.0f;
  859. const float maxu = _maxu;
  860. const float minv = 0.0f;
  861. const float maxv = _maxv;
  862. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  863. vertex->set(maxx, miny, maxu, minv, 0.0f, _abgr); ++vertex;
  864. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  865. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  866. vertex->set(minx, maxy, minu, maxv, 0.0f, _abgr); ++vertex;
  867. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  868. bgfx::setVertexBuffer(0, &vb);
  869. }
  870. }
  871. else
  872. {
  873. const uint32_t numVertices = 14;
  874. const uint32_t numIndices = 36;
  875. if (checkAvailTransientBuffers(numVertices, PosUvwColorVertex::ms_layout, numIndices) )
  876. {
  877. bgfx::TransientVertexBuffer tvb;
  878. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosUvwColorVertex::ms_layout);
  879. bgfx::TransientIndexBuffer tib;
  880. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  881. PosUvwColorVertex* vertex = (PosUvwColorVertex*)tvb.data;
  882. uint16_t* indices = (uint16_t*)tib.data;
  883. if (Geometry::Cross == _type)
  884. {
  885. const float sx = _width /1.5f;
  886. const float sy = _height/1.5f;
  887. const float px = float(_x)-sx/4.0f;
  888. const float py = float(_y);
  889. vertex->set(0.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  890. vertex->set(0.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  891. vertex->set(0.5f*sx+px, 0.0f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  892. vertex->set(0.5f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  893. vertex->set(0.5f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  894. vertex->set(0.5f*sx+px, 1.5f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  895. vertex->set(1.0f*sx+px, 0.0f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  896. vertex->set(1.0f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  897. vertex->set(1.0f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  898. vertex->set(1.0f*sx+px, 1.5f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  899. vertex->set(1.5f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  900. vertex->set(1.5f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  901. vertex->set(2.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  902. vertex->set(2.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  903. indices += addQuad(indices, 0, 3, 4, 1);
  904. indices += addQuad(indices, 2, 6, 7, 3);
  905. indices += addQuad(indices, 3, 7, 8, 4);
  906. indices += addQuad(indices, 4, 8, 9, 5);
  907. indices += addQuad(indices, 7, 10, 11, 8);
  908. indices += addQuad(indices, 10, 12, 13, 11);
  909. }
  910. else
  911. {
  912. const float sx = float(_width);
  913. const float sy = float(_height);
  914. const float px = float(_x) - sx/2.0f;
  915. const float py = float(_y);
  916. vertex->set(0.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  917. vertex->set(0.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  918. vertex->set(0.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  919. vertex->set(0.5f*sx+px, 0.50f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  920. vertex->set(0.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  921. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  922. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  923. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  924. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  925. vertex->set(1.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  926. vertex->set(1.5f*sx+px, 0.50f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  927. vertex->set(1.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  928. vertex->set(2.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  929. vertex->set(2.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  930. indices += addQuad(indices, 0, 2, 3, 1);
  931. indices += addQuad(indices, 1, 3, 6, 4);
  932. indices += addQuad(indices, 2, 5, 6, 3);
  933. indices += addQuad(indices, 7, 9, 12, 10);
  934. indices += addQuad(indices, 7, 10, 11, 8);
  935. indices += addQuad(indices, 10, 12, 13, 11);
  936. }
  937. bgfx::setVertexBuffer(0, &tvb);
  938. bgfx::setIndexBuffer(&tib);
  939. }
  940. }
  941. }
  942. template<bx::LerpFn lerpT, bx::EaseFn easeT>
  943. struct InterpolatorT
  944. {
  945. float from;
  946. float to;
  947. float duration;
  948. int64_t offset;
  949. InterpolatorT(float _value)
  950. {
  951. reset(_value);
  952. }
  953. void reset(float _value)
  954. {
  955. from = _value;
  956. to = _value;
  957. duration = 0.0f;
  958. offset = bx::getHPCounter();
  959. }
  960. void set(float _value, float _duration)
  961. {
  962. if (_value != to)
  963. {
  964. from = getValue();
  965. to = _value;
  966. duration = _duration;
  967. offset = bx::getHPCounter();
  968. }
  969. }
  970. float getValue()
  971. {
  972. if (isActive() )
  973. {
  974. const double freq = double(bx::getHPFrequency() );
  975. int64_t now = bx::getHPCounter();
  976. float time = (float)(double(now - offset) / freq);
  977. float lerp = duration != 0.0f ? bx::clamp(time, 0.0f, duration) / duration : 0.0f;
  978. return lerpT(from, to, easeT(lerp) );
  979. }
  980. return to;
  981. }
  982. bool isActive() const
  983. {
  984. if (0.0f < duration)
  985. {
  986. const double freq = double(bx::getHPFrequency() );
  987. int64_t now = bx::getHPCounter();
  988. float time = (float)(double(now - offset) / freq);
  989. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  990. return lerp < 1.0f;
  991. }
  992. return false;
  993. }
  994. };
  995. typedef InterpolatorT<bx::lerp, bx::easeInOutQuad> Interpolator;
  996. typedef InterpolatorT<bx::angleLerp, bx::easeInOutCubic> InterpolatorAngle;
  997. typedef InterpolatorT<bx::lerp, bx::easeLinear> InterpolatorLinear;
  998. void keyBindingHelp(const char* _bindings, const char* _description)
  999. {
  1000. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "%s", _bindings);
  1001. ImGui::SameLine(100);
  1002. ImGui::Text("%s", _description);
  1003. }
  1004. inline std::string replaceAll(const char* _str, const char* _from, const char* _to)
  1005. {
  1006. std::string str = _str;
  1007. size_t startPos = 0;
  1008. const size_t fromLen = bx::strLen(_from);
  1009. const size_t toLen = bx::strLen(_to);
  1010. while ( (startPos = str.find(_from, startPos) ) != std::string::npos)
  1011. {
  1012. str.replace(startPos, fromLen, _to);
  1013. startPos += toLen;
  1014. }
  1015. return str;
  1016. }
  1017. void associate()
  1018. {
  1019. #if BX_PLATFORM_WINDOWS
  1020. std::string str;
  1021. char exec[bx::kMaxFilePath];
  1022. GetModuleFileNameA(GetModuleHandleA(NULL), exec, sizeof(exec) );
  1023. std::string strExec = replaceAll(exec, "\\", "\\\\");
  1024. std::string value;
  1025. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  1026. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  1027. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  1028. str += value;
  1029. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  1030. str += value;
  1031. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1032. {
  1033. const char* ext = s_supportedExt[ii];
  1034. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  1035. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  1036. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  1037. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  1038. }
  1039. bx::FilePath filePath(bx::Dir::Temp);
  1040. filePath.join("texture.reg");
  1041. bx::FileWriter writer;
  1042. bx::Error err;
  1043. if (bx::open(&writer, filePath, false, &err) )
  1044. {
  1045. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  1046. bx::close(&writer);
  1047. if (err.isOk() )
  1048. {
  1049. std::string cmd;
  1050. bx::stringPrintf(cmd, "/s %s", filePath.getCPtr() );
  1051. bx::ProcessReader reader;
  1052. if (bx::open(&reader, "regedit.exe", cmd.c_str(), &err) )
  1053. {
  1054. bx::close(&reader);
  1055. }
  1056. }
  1057. }
  1058. #elif BX_PLATFORM_LINUX
  1059. std::string mimeType;
  1060. auto associate = [&mimeType](const char* _ext)
  1061. {
  1062. std::string tmp;
  1063. bx::stringPrintf(tmp, "default texturev.desktop image/%s", _ext);
  1064. bx::ProcessReader reader;
  1065. bx::Error err;
  1066. if (bx::open(&reader, "xdg-mime", tmp.c_str(), &err) )
  1067. {
  1068. bx::close(&reader);
  1069. }
  1070. else
  1071. {
  1072. bx::printf("Failed to associate MIME type image/%s (error: \"%S\")!\n", _ext, &err.getMessage() );
  1073. }
  1074. bx::stringPrintf(mimeType, "image/%s;", _ext);
  1075. };
  1076. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1077. {
  1078. associate(s_supportedExt[ii]);
  1079. }
  1080. associate("x-dds");
  1081. bx::FileWriter writer;
  1082. bx::Error err;
  1083. if (bx::open(&writer, "/usr/share/applications/texturev.desktop", false, &err) )
  1084. {
  1085. bx::write(&writer, &err
  1086. , "[Desktop Entry]\n"
  1087. "Version=%d.%d.%d\n"
  1088. "Type=Application\n"
  1089. "Name=texturev\n"
  1090. "GenericName=bgfx Image/Texture Viewer\n"
  1091. "Exec=texturev %%U\n"
  1092. "Terminal=false\n"
  1093. "Categories=Graphics\n"
  1094. "StartupNotify=true\n"
  1095. "MimeType=%s\n"
  1096. , BGFX_TEXTUREV_VERSION_MAJOR
  1097. , BGFX_TEXTUREV_VERSION_MINOR
  1098. , BGFX_API_VERSION
  1099. , mimeType.c_str()
  1100. );
  1101. bx::close(&writer);
  1102. }
  1103. else
  1104. {
  1105. bx::printf("Failed to create texturev desktop entry (error: \"%S\")! Permissions (try sudo)?!\n", &err.getMessage() );
  1106. }
  1107. #endif // BX_PLATFORM_WINDOWS
  1108. }
  1109. void help(const char* _error = NULL)
  1110. {
  1111. if (NULL != _error)
  1112. {
  1113. bx::printf("Error:\n%s\n\n", _error);
  1114. }
  1115. bx::printf(
  1116. "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1117. "Copyright 2011-2024 Branimir Karadzic. All rights reserved.\n"
  1118. "License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE\n\n"
  1119. , BGFX_TEXTUREV_VERSION_MAJOR
  1120. , BGFX_TEXTUREV_VERSION_MINOR
  1121. , BGFX_API_VERSION
  1122. );
  1123. bx::printf(
  1124. "Usage: texturev <file path>\n"
  1125. "\n"
  1126. "Supported input file types:\n"
  1127. );
  1128. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1129. {
  1130. bx::printf(" *.%s\n", s_supportedExt[ii]);
  1131. }
  1132. bx::printf(
  1133. "\n"
  1134. "Options:\n"
  1135. " -h, --help Help.\n"
  1136. " -v, --version Version information only.\n"
  1137. " --associate Associate file extensions with texturev.\n"
  1138. "\n"
  1139. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  1140. );
  1141. }
  1142. int _main_(int _argc, char** _argv)
  1143. {
  1144. bx::CommandLine cmdLine(_argc, _argv);
  1145. if (cmdLine.hasArg('v', "version") )
  1146. {
  1147. bx::printf(
  1148. "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1149. , BGFX_TEXTUREV_VERSION_MAJOR
  1150. , BGFX_TEXTUREV_VERSION_MINOR
  1151. , BGFX_API_VERSION
  1152. );
  1153. return bx::kExitSuccess;
  1154. }
  1155. if (cmdLine.hasArg('h', "help") )
  1156. {
  1157. help();
  1158. return bx::kExitFailure;
  1159. }
  1160. else if (cmdLine.hasArg("associate") )
  1161. {
  1162. associate();
  1163. return bx::kExitFailure;
  1164. }
  1165. uint32_t debug = BGFX_DEBUG_TEXT;
  1166. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  1167. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1168. View view;
  1169. cmdAdd("view", cmdView, &view);
  1170. entry::setWindowFlags(entry::kDefaultWindowHandle, ENTRY_WINDOW_FLAG_ASPECT_RATIO, false);
  1171. entry::setWindowSize(entry::kDefaultWindowHandle, view.m_width, view.m_height);
  1172. bgfx::Init init;
  1173. init.type = view.m_rendererType;
  1174. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  1175. init.platformData.ndt = entry::getNativeDisplayHandle();
  1176. init.resolution.width = view.m_width;
  1177. init.resolution.height = view.m_height;
  1178. init.resolution.reset = BGFX_RESET_VSYNC;
  1179. bgfx::init(init);
  1180. // Set view 0 clear state.
  1181. bgfx::setViewClear(BACKGROUND_VIEW_ID
  1182. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  1183. , 0x000000ff
  1184. , 1.0f
  1185. , 0
  1186. );
  1187. imguiCreate();
  1188. PosUvwColorVertex::init();
  1189. const bgfx::Caps* caps = bgfx::getCaps();
  1190. bgfx::RendererType::Enum type = caps->rendererType;
  1191. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  1192. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  1193. bgfx::UniformHandle u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Vec4);
  1194. bgfx::UniformHandle u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Vec4);
  1195. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  1196. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  1197. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  1198. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  1199. vsTexture
  1200. , fsTexture
  1201. , true
  1202. );
  1203. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  1204. vsTexture
  1205. , bgfx::isValid(fsTextureArray)
  1206. ? fsTextureArray
  1207. : fsTexture
  1208. , true
  1209. );
  1210. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  1211. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  1212. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  1213. , true
  1214. );
  1215. bgfx::ProgramHandle textureCube2Program = bgfx::createProgram(
  1216. vsTexture
  1217. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube2")
  1218. , true
  1219. );
  1220. bgfx::ProgramHandle textureSdfProgram = bgfx::createProgram(
  1221. vsTexture
  1222. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_sdf")
  1223. , true
  1224. );
  1225. bgfx::ProgramHandle textureMsdfProgram = bgfx::createProgram(
  1226. vsTexture
  1227. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_msdf")
  1228. , true
  1229. );
  1230. bgfx::ProgramHandle texture3DProgram = bgfx::createProgram(
  1231. vsTexture
  1232. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_3d")
  1233. , true
  1234. );
  1235. const uint32_t checkerBoardSize = 64;
  1236. bgfx::TextureHandle checkerBoard;
  1237. {
  1238. const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize*checkerBoardSize*4);
  1239. bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xff8e8e8e, 0xff5d5d5d);
  1240. checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
  1241. , bgfx::TextureFormat::BGRA8
  1242. , 0
  1243. | BGFX_SAMPLER_MIN_POINT
  1244. | BGFX_SAMPLER_MIP_POINT
  1245. | BGFX_SAMPLER_MAG_POINT
  1246. , mem
  1247. );
  1248. }
  1249. float speed = 0.37f;
  1250. float time = 0.0f;
  1251. Interpolator menuFade(5.0f);
  1252. Interpolator mip(0.0f);
  1253. Interpolator layer(0.0f);
  1254. InterpolatorLinear ev(0.0f);
  1255. Interpolator zoom(1.0f);
  1256. Interpolator scale(1.0f);
  1257. Interpolator posx(0.0f);
  1258. Interpolator posy(0.0f);
  1259. InterpolatorAngle angle(0.0f);
  1260. InterpolatorAngle angx(0.0f);
  1261. InterpolatorAngle angy(0.0f);
  1262. auto anyActive = [&]() -> bool
  1263. {
  1264. return false
  1265. || ImGui::MouseOverArea()
  1266. || menuFade.isActive()
  1267. || mip.isActive()
  1268. || layer.isActive()
  1269. || ev.isActive()
  1270. || zoom.isActive()
  1271. || scale.isActive()
  1272. || posx.isActive()
  1273. || posy.isActive()
  1274. || angle.isActive()
  1275. || angx.isActive()
  1276. || angy.isActive()
  1277. ;
  1278. };
  1279. const char* filePath = _argc < 2 ? "" : _argv[1];
  1280. view.updateFileList(filePath);
  1281. int exitcode = bx::kExitSuccess;
  1282. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  1283. {
  1284. uint32_t fileIndex = 0;
  1285. bool dragging = false;
  1286. entry::WindowState windowState;
  1287. entry::MouseState mouseStatePrev;
  1288. while (!entry::processWindowEvents(windowState, debug, init.resolution.reset) )
  1289. {
  1290. const entry::MouseState& mouseState = windowState.m_mouse;
  1291. view.m_width = windowState.m_width;
  1292. view.m_height = windowState.m_height;
  1293. if (!windowState.m_dropFile.isEmpty() )
  1294. {
  1295. view.updateFileList(windowState.m_dropFile);
  1296. windowState.m_dropFile.clear();
  1297. }
  1298. imguiBeginFrame(mouseState.m_mx
  1299. , mouseState.m_my
  1300. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1301. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1302. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1303. , mouseState.m_mz
  1304. , uint16_t(view.m_width)
  1305. , uint16_t(view.m_height)
  1306. );
  1307. bool modalWindow = view.m_help || view.m_about;
  1308. bool overArea = false
  1309. || ImGui::GetMousePos().y <= ImGui::GetTextLineHeightWithSpacing()
  1310. || ImGui::MouseOverArea()
  1311. ;
  1312. overArea &= !modalWindow;
  1313. if (overArea)
  1314. {
  1315. menuFade.set(5.0f, 0.25f);
  1316. }
  1317. else if (modalWindow)
  1318. {
  1319. menuFade.reset(0.0f);
  1320. }
  1321. else
  1322. {
  1323. menuFade.set(0.0f, 2.0f);
  1324. }
  1325. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, bx::clamp(menuFade.getValue(), 0.0f, 1.0f) );
  1326. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  1327. if (ImGui::BeginMainMenuBar() )
  1328. {
  1329. if (ImGui::BeginMenu("File"))
  1330. {
  1331. if (ImGui::MenuItem("Open File") )
  1332. {
  1333. bx::FilePath tmp = view.m_path;
  1334. if (openFileSelectionDialog(
  1335. tmp
  1336. , FileSelectionDialogType::Open
  1337. , "texturev: Open File"
  1338. , s_filter
  1339. ) )
  1340. {
  1341. view.updateFileList(tmp);
  1342. }
  1343. }
  1344. if (ImGui::MenuItem("Show File List", NULL, view.m_files) )
  1345. {
  1346. cmdExec("view files");
  1347. }
  1348. ImGui::Separator();
  1349. if (ImGui::MenuItem("Exit") )
  1350. {
  1351. cmdExec("exit");
  1352. }
  1353. ImGui::EndMenu();
  1354. }
  1355. if (ImGui::BeginMenu("View") )
  1356. {
  1357. if (ImGui::MenuItem("Info", NULL, view.m_info) )
  1358. {
  1359. cmdExec("view info");
  1360. }
  1361. if (ImGui::MenuItem("Reset") )
  1362. {
  1363. cmdExec(s_resetCmd);
  1364. }
  1365. ImGui::Separator();
  1366. bool filter = view.m_filter;
  1367. if (ImGui::MenuItem("Filter", NULL, &filter) )
  1368. {
  1369. cmdExec("view filter");
  1370. }
  1371. bool animate = 0.0f < view.m_transitionTime;
  1372. if (ImGui::MenuItem("Animate", NULL, &animate) )
  1373. {
  1374. cmdExec("view transition %f", animate ? 1.0f : 0.0f);
  1375. }
  1376. if (ImGui::BeginMenu("Cubemap", view.m_textureInfo.cubeMap) )
  1377. {
  1378. if (ImGui::MenuItem("Quad", NULL, Geometry::Quad == view.m_cubeMapGeo) )
  1379. {
  1380. cmdExec("view geo quad");
  1381. }
  1382. if (ImGui::MenuItem("Cross", NULL, Geometry::Cross == view.m_cubeMapGeo) )
  1383. {
  1384. cmdExec("view geo cross");
  1385. }
  1386. if (ImGui::MenuItem("Hexagon", NULL, Geometry::Hexagon == view.m_cubeMapGeo) )
  1387. {
  1388. cmdExec("view geo hexagon");
  1389. }
  1390. ImGui::EndMenu();
  1391. }
  1392. if (ImGui::BeginMenu("Output") )
  1393. {
  1394. const bool hdrCap = (bgfx::getCaps()->supported & BGFX_CAPS_HDR10);
  1395. if (ImGui::MenuItem("sRGB", NULL, Output::sRGB == view.m_outputFormat) )
  1396. {
  1397. cmdExec("view output srgb");
  1398. }
  1399. if (hdrCap)
  1400. {
  1401. if (ImGui::MenuItem("scRGB", NULL, Output::scRGB == view.m_outputFormat) )
  1402. {
  1403. cmdExec("view output scrgb");
  1404. }
  1405. if (ImGui::MenuItem("HDR10", NULL, Output::HDR10 == view.m_outputFormat) )
  1406. {
  1407. cmdExec("view output hdr10");
  1408. }
  1409. }
  1410. ImGui::EndMenu();
  1411. }
  1412. bool sdf = view.m_sdf;
  1413. if (ImGui::MenuItem("SDF", NULL, &sdf) )
  1414. {
  1415. cmdExec("view sdf");
  1416. }
  1417. bool rr = 0 != (view.m_abgr & 0x000000ff);
  1418. if (ImGui::MenuItem("R", NULL, &rr) )
  1419. {
  1420. cmdExec("view rgb r");
  1421. }
  1422. bool gg = 0 != (view.m_abgr & 0x0000ff00);
  1423. if (ImGui::MenuItem("G", NULL, &gg) )
  1424. {
  1425. cmdExec("view rgb g");
  1426. }
  1427. bool bb = 0 != (view.m_abgr & 0x00ff0000);
  1428. if (ImGui::MenuItem("B", NULL, &bb) )
  1429. {
  1430. cmdExec("view rgb b");
  1431. }
  1432. bool alpha = view.m_alpha;
  1433. if (ImGui::MenuItem("Checkerboard", NULL, &alpha) )
  1434. {
  1435. cmdExec("view rgb a");
  1436. }
  1437. ImGui::Separator();
  1438. if (ImGui::MenuItem("Save Options") )
  1439. {
  1440. cmdExec("view save");
  1441. }
  1442. ImGui::EndMenu();
  1443. }
  1444. if (ImGui::BeginMenu("Help") )
  1445. {
  1446. if (ImGui::MenuItem("View Help") )
  1447. {
  1448. cmdExec("view help");
  1449. }
  1450. ImGui::Separator();
  1451. if (ImGui::MenuItem("About") )
  1452. {
  1453. cmdExec("view about");
  1454. }
  1455. ImGui::EndMenu();
  1456. }
  1457. if (0 != view.m_fileList.size() )
  1458. {
  1459. ImGui::Separator();
  1460. ImGui::TextColored(
  1461. ImVec4(0.0f, 1.0f, 1.0f, 1.0f)
  1462. , "%s"
  1463. , view.m_fileList[view.m_fileIndex].c_str()
  1464. );
  1465. ImGui::Separator();
  1466. const char* name = "";
  1467. if (view.m_textureInfo.cubeMap)
  1468. {
  1469. name = " CubeMap";
  1470. }
  1471. else if (1 < view.m_textureInfo.depth)
  1472. {
  1473. name = " 3D";
  1474. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1475. }
  1476. else if (1 < view.m_textureInfo.numLayers)
  1477. {
  1478. name = " 2D Array";
  1479. }
  1480. ImGui::Text("%d x %d%s, mips: %d, layers %d, %s"
  1481. , view.m_textureInfo.width
  1482. , view.m_textureInfo.height
  1483. , name
  1484. , view.m_textureInfo.numMips
  1485. , view.m_textureInfo.numLayers
  1486. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1487. );
  1488. }
  1489. ImGui::EndMainMenuBar();
  1490. }
  1491. ImGui::PopStyleVar(2);
  1492. static bool help = false;
  1493. static bool about = false;
  1494. static bool mouseDelta = false;
  1495. if (!mouseDelta)
  1496. {
  1497. mouseStatePrev = mouseState;
  1498. mouseDelta = true;
  1499. }
  1500. int32_t zoomDelta = overArea ? 0 : mouseState.m_mz - mouseStatePrev.m_mz;
  1501. if (zoomDelta != 0)
  1502. {
  1503. char exec[64];
  1504. bx::snprintf(exec, BX_COUNTOF(exec), "view zoom %+f", -zoomDelta*0.1f);
  1505. cmdExec(exec);
  1506. }
  1507. const float xDelta = float(mouseStatePrev.m_mx - mouseState.m_mx);
  1508. const float yDelta = float(mouseStatePrev.m_my - mouseState.m_my);
  1509. if (!overArea
  1510. && !help
  1511. && mouseState.m_buttons[entry::MouseButton::Left] != mouseStatePrev.m_buttons[entry::MouseButton::Left])
  1512. {
  1513. dragging = !!mouseState.m_buttons[entry::MouseButton::Left];
  1514. }
  1515. if (dragging)
  1516. {
  1517. if (view.m_textureInfo.cubeMap
  1518. && Geometry::Quad == view.m_cubeMapGeo)
  1519. {
  1520. char exec[64];
  1521. bx::snprintf(exec, BX_COUNTOF(exec), "view cubemap %+f %+f", -yDelta, -xDelta);
  1522. cmdExec(exec);
  1523. }
  1524. else
  1525. {
  1526. char exec[64];
  1527. bx::snprintf(exec, BX_COUNTOF(exec), "view pan %+f %+f", xDelta, yDelta);
  1528. cmdExec(exec);
  1529. }
  1530. }
  1531. mouseStatePrev = mouseState;
  1532. if (help != view.m_help)
  1533. {
  1534. if (!help)
  1535. {
  1536. ImGui::OpenPopup("Help");
  1537. inputRemoveBindings(s_bindingName[Binding::View]);
  1538. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  1539. }
  1540. else
  1541. {
  1542. inputRemoveBindings(s_bindingName[Binding::Help]);
  1543. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1544. }
  1545. help = view.m_help;
  1546. }
  1547. if (about != view.m_about)
  1548. {
  1549. if (!about)
  1550. {
  1551. ImGui::OpenPopup("About");
  1552. inputRemoveBindings(s_bindingName[Binding::View]);
  1553. inputAddBindings(s_bindingName[Binding::About], s_binding[Binding::About]);
  1554. }
  1555. else
  1556. {
  1557. inputRemoveBindings(s_bindingName[Binding::About]);
  1558. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1559. }
  1560. about = view.m_about;
  1561. }
  1562. if (view.m_info)
  1563. {
  1564. ImGui::SetNextWindowSize(
  1565. ImVec2(300.0f, 320.0f)
  1566. , ImGuiCond_FirstUseEver
  1567. );
  1568. if (ImGui::Begin("Info", &view.m_info) )
  1569. {
  1570. if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
  1571. {
  1572. if (!bgfx::isValid(texture) )
  1573. {
  1574. ImGui::Text("Texture is not loaded.");
  1575. }
  1576. else
  1577. {
  1578. ImGui::Text("Name: %s", view.m_fileList[view.m_fileIndex].c_str() );
  1579. ImGui::Text("Dimensions: %d x %d"
  1580. , view.m_textureInfo.width
  1581. , view.m_textureInfo.height
  1582. );
  1583. ImGui::Text("Format: %s"
  1584. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1585. );
  1586. uint32_t numLayers = view.m_textureInfo.numLayers;
  1587. if (1 < view.m_textureInfo.depth)
  1588. {
  1589. numLayers = bx::max(1, view.m_textureInfo.depth >> view.m_mip);
  1590. }
  1591. view.m_layer = bx::clamp<int32_t>(view.m_layer, 0, numLayers - 1);
  1592. ImGui::SliderInt("Layer", (int32_t*)&view.m_layer, 0, numLayers - 1);
  1593. ImGui::SliderInt("Mip", (int32_t*)&view.m_mip, 0, view.m_textureInfo.numMips - 1);
  1594. ImGui::Separator();
  1595. ImGui::Checkbox("Input linear", &view.m_inLinear);
  1596. ImGui::RangeSliderFloat("EV range", &view.m_evMin, &view.m_evMax, kEvMin, kEvMax);
  1597. ImGui::SliderFloat("EV", &view.m_ev, view.m_evMin, view.m_evMax);
  1598. ImGui::Separator();
  1599. ImGui::Checkbox("Fit to window", &view.m_fit);
  1600. ImGui::SliderFloat("Scale", &view.m_zoom, 0.01f, 10.0f);
  1601. }
  1602. ImGui::EndChild();
  1603. }
  1604. }
  1605. ImGui::End();
  1606. }
  1607. if (view.m_files)
  1608. {
  1609. ImGui::PushFont(ImGui::Font::Mono);
  1610. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  1611. ImGui::PopFont();
  1612. char temp[bx::kMaxFilePath];
  1613. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.getCPtr() );
  1614. ImGui::SetNextWindowSize(
  1615. ImVec2(400.0f, 20*itemHeight)
  1616. , ImGuiCond_FirstUseEver
  1617. );
  1618. if (ImGui::Begin(temp, &view.m_files) )
  1619. {
  1620. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  1621. {
  1622. ImGui::PushFont(ImGui::Font::Mono);
  1623. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  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. }