texturev.cpp 55 KB

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