texturev.cpp 57 KB

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