texturev.cpp 58 KB

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