texturev.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  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. if (0 == bx::strCmpI(ext, s_supportedExt[ii]) )
  827. {
  828. supported = true;
  829. break;
  830. }
  831. }
  832. if (supported)
  833. {
  834. const bx::StringView fileName = fi.filePath.getFileName();
  835. m_fileList.push_back(std::string(fileName.getPtr(), fileName.getTerm() ) );
  836. }
  837. }
  838. }
  839. }
  840. bx::close(&dr);
  841. std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);
  842. m_fileIndex = 0;
  843. uint32_t idx = 0;
  844. const bx::StringView fileName = _filePath.getFileName();
  845. for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
  846. {
  847. if (0 == bx::strCmpI(it->c_str(), fileName) )
  848. {
  849. // If it is case-insensitive match then might be correct one, but keep
  850. // searching.
  851. m_fileIndex = idx;
  852. if (0 == bx::strCmp(it->c_str(), fileName) )
  853. {
  854. // If it is exact match we're done.
  855. break;
  856. }
  857. }
  858. }
  859. }
  860. void load()
  861. {
  862. bx::FilePath filePath(bx::Dir::Home);
  863. filePath.join(".config/bgfx/texturev.ini");
  864. bx::Settings settings(entry::getAllocator() );
  865. bx::FileReader reader;
  866. if (bx::open(&reader, filePath) )
  867. {
  868. bx::read(&reader, settings);
  869. bx::close(&reader);
  870. if (!bx::fromString(&m_transitionTime, settings.get("view/transition") ) )
  871. {
  872. m_transitionTime = 1.0f;
  873. }
  874. if (!bx::fromString(&m_width, settings.get("view/width") ) )
  875. {
  876. m_width = 1280;
  877. }
  878. if (!bx::fromString(&m_height, settings.get("view/height") ) )
  879. {
  880. m_height = 720;
  881. }
  882. }
  883. }
  884. void save()
  885. {
  886. bx::FilePath filePath(bx::Dir::Home);
  887. filePath.join(".config/bgfx/texturev.ini");
  888. if (bx::makeAll(filePath.getPath() ) )
  889. {
  890. bx::Settings settings(entry::getAllocator() );
  891. char tmp[256];
  892. bx::toString(tmp, sizeof(tmp), m_transitionTime);
  893. settings.set("view/transition", tmp);
  894. bx::toString(tmp, sizeof(tmp), m_width);
  895. settings.set("view/width", tmp);
  896. bx::toString(tmp, sizeof(tmp), m_height);
  897. settings.set("view/height", tmp);
  898. bx::FileWriter writer;
  899. if (bx::open(&writer, filePath) )
  900. {
  901. bx::write(&writer, settings);
  902. bx::close(&writer);
  903. }
  904. }
  905. }
  906. bx::FilePath m_path;
  907. typedef stl::vector<std::string> FileList;
  908. FileList m_fileList;
  909. bgfx::TextureInfo m_textureInfo;
  910. Geometry::Enum m_cubeMapGeo;
  911. Output::Enum m_outputFormat;
  912. uint32_t m_fileIndex;
  913. uint32_t m_scaleFn;
  914. uint32_t m_mip;
  915. uint32_t m_layer;
  916. uint32_t m_abgr;
  917. float m_ev;
  918. float m_evMin;
  919. float m_evMax;
  920. float m_posx;
  921. float m_posy;
  922. float m_angx;
  923. float m_angy;
  924. float m_zoom;
  925. float m_angle;
  926. float m_orientation;
  927. float m_flipH;
  928. float m_flipV;
  929. float m_transitionTime;
  930. uint32_t m_width;
  931. uint32_t m_height;
  932. bool m_filter;
  933. bool m_fit;
  934. bool m_alpha;
  935. bool m_help;
  936. bool m_about;
  937. bool m_info;
  938. bool m_files;
  939. bool m_sdf;
  940. bool m_inLinear;
  941. };
  942. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  943. {
  944. View* view = static_cast<View*>(_userData);
  945. return view->cmd(_argc, _argv);
  946. }
  947. struct PosUvwColorVertex
  948. {
  949. float m_x;
  950. float m_y;
  951. float m_u;
  952. float m_v;
  953. float m_w;
  954. uint32_t m_abgr;
  955. static void init()
  956. {
  957. ms_decl
  958. .begin()
  959. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  960. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  961. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  962. .end();
  963. }
  964. void set(float _x, float _y, float _u, float _v, float _w, uint32_t _abgr)
  965. {
  966. m_x = _x;
  967. m_y = _y;
  968. m_u = _u;
  969. m_v = _v;
  970. m_w = _w;
  971. m_abgr = _abgr;
  972. }
  973. static bgfx::VertexDecl ms_decl;
  974. };
  975. bgfx::VertexDecl PosUvwColorVertex::ms_decl;
  976. static uint32_t addQuad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
  977. {
  978. _indices[0] = _idx0;
  979. _indices[1] = _idx3;
  980. _indices[2] = _idx1;
  981. _indices[3] = _idx1;
  982. _indices[4] = _idx3;
  983. _indices[5] = _idx2;
  984. return 6;
  985. }
  986. void setGeometry(
  987. Geometry::Enum _type
  988. , int32_t _x
  989. , int32_t _y
  990. , uint32_t _width
  991. , uint32_t _height
  992. , uint32_t _abgr
  993. , float _maxu = 1.0f
  994. , float _maxv = 1.0f
  995. )
  996. {
  997. if (Geometry::Quad == _type)
  998. {
  999. if (6 == bgfx::getAvailTransientVertexBuffer(6, PosUvwColorVertex::ms_decl) )
  1000. {
  1001. bgfx::TransientVertexBuffer vb;
  1002. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvwColorVertex::ms_decl);
  1003. PosUvwColorVertex* vertex = (PosUvwColorVertex*)vb.data;
  1004. const float widthf = float(_width);
  1005. const float heightf = float(_height);
  1006. const float minx = float(_x);
  1007. const float miny = float(_y);
  1008. const float maxx = minx+widthf;
  1009. const float maxy = miny+heightf;
  1010. const float minu = 0.0f;
  1011. const float maxu = _maxu;
  1012. const float minv = 0.0f;
  1013. const float maxv = _maxv;
  1014. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  1015. vertex->set(maxx, miny, maxu, minv, 0.0f, _abgr); ++vertex;
  1016. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  1017. vertex->set(maxx, maxy, maxu, maxv, 0.0f, _abgr); ++vertex;
  1018. vertex->set(minx, maxy, minu, maxv, 0.0f, _abgr); ++vertex;
  1019. vertex->set(minx, miny, minu, minv, 0.0f, _abgr); ++vertex;
  1020. bgfx::setVertexBuffer(0, &vb);
  1021. }
  1022. }
  1023. else
  1024. {
  1025. const uint32_t numVertices = 14;
  1026. const uint32_t numIndices = 36;
  1027. if (checkAvailTransientBuffers(numVertices, PosUvwColorVertex::ms_decl, numIndices) )
  1028. {
  1029. bgfx::TransientVertexBuffer tvb;
  1030. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosUvwColorVertex::ms_decl);
  1031. bgfx::TransientIndexBuffer tib;
  1032. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1033. PosUvwColorVertex* vertex = (PosUvwColorVertex*)tvb.data;
  1034. uint16_t* indices = (uint16_t*)tib.data;
  1035. if (Geometry::Cross == _type)
  1036. {
  1037. const float sx = _width /1.5f;
  1038. const float sy = _height/1.5f;
  1039. const float px = float(_x)-sx/4.0f;
  1040. const float py = float(_y);
  1041. vertex->set(0.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1042. vertex->set(0.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1043. vertex->set(0.5f*sx+px, 0.0f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1044. vertex->set(0.5f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  1045. vertex->set(0.5f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  1046. vertex->set(0.5f*sx+px, 1.5f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1047. vertex->set(1.0f*sx+px, 0.0f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1048. vertex->set(1.0f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  1049. vertex->set(1.0f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  1050. vertex->set(1.0f*sx+px, 1.5f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1051. vertex->set(1.5f*sx+px, 0.5f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1052. vertex->set(1.5f*sx+px, 1.0f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1053. vertex->set(2.0f*sx+px, 0.5f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1054. vertex->set(2.0f*sx+px, 1.0f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1055. indices += addQuad(indices, 0, 3, 4, 1);
  1056. indices += addQuad(indices, 2, 6, 7, 3);
  1057. indices += addQuad(indices, 3, 7, 8, 4);
  1058. indices += addQuad(indices, 4, 8, 9, 5);
  1059. indices += addQuad(indices, 7, 10, 11, 8);
  1060. indices += addQuad(indices, 10, 12, 13, 11);
  1061. }
  1062. else
  1063. {
  1064. const float sx = float(_width);
  1065. const float sy = float(_height);
  1066. const float px = float(_x) - sx/2.0f;
  1067. const float py = float(_y);
  1068. vertex->set(0.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1069. vertex->set(0.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1070. vertex->set(0.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  1071. vertex->set(0.5f*sx+px, 0.50f*sy+py, -1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  1072. vertex->set(0.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1073. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  1074. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  1075. vertex->set(1.0f*sx+px, 0.25f*sy+py, 1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  1076. vertex->set(1.0f*sx+px, 0.75f*sy+py, 1.0f, -1.0f, 1.0f, _abgr); ++vertex;
  1077. vertex->set(1.5f*sx+px, 0.00f*sy+py, -1.0f, 1.0f, 1.0f, _abgr); ++vertex;
  1078. vertex->set(1.5f*sx+px, 0.50f*sy+py, 1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1079. vertex->set(1.5f*sx+px, 1.00f*sy+py, 1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1080. vertex->set(2.0f*sx+px, 0.25f*sy+py, -1.0f, 1.0f, -1.0f, _abgr); ++vertex;
  1081. vertex->set(2.0f*sx+px, 0.75f*sy+py, -1.0f, -1.0f, -1.0f, _abgr); ++vertex;
  1082. indices += addQuad(indices, 0, 2, 3, 1);
  1083. indices += addQuad(indices, 1, 3, 6, 4);
  1084. indices += addQuad(indices, 2, 5, 6, 3);
  1085. indices += addQuad(indices, 7, 9, 12, 10);
  1086. indices += addQuad(indices, 7, 10, 11, 8);
  1087. indices += addQuad(indices, 10, 12, 13, 11);
  1088. }
  1089. bgfx::setVertexBuffer(0, &tvb);
  1090. bgfx::setIndexBuffer(&tib);
  1091. }
  1092. }
  1093. }
  1094. template<bx::LerpFn lerpT, bx::EaseFn easeT>
  1095. struct InterpolatorT
  1096. {
  1097. float from;
  1098. float to;
  1099. float duration;
  1100. int64_t offset;
  1101. InterpolatorT(float _value)
  1102. {
  1103. reset(_value);
  1104. }
  1105. void reset(float _value)
  1106. {
  1107. from = _value;
  1108. to = _value;
  1109. duration = 0.0;
  1110. offset = bx::getHPCounter();
  1111. }
  1112. void set(float _value, float _duration)
  1113. {
  1114. if (_value != to)
  1115. {
  1116. from = getValue();
  1117. to = _value;
  1118. duration = _duration;
  1119. offset = bx::getHPCounter();
  1120. }
  1121. }
  1122. float getValue()
  1123. {
  1124. if (isActive() )
  1125. {
  1126. const double freq = double(bx::getHPFrequency() );
  1127. int64_t now = bx::getHPCounter();
  1128. float time = (float)(double(now - offset) / freq);
  1129. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  1130. return lerpT(from, to, easeT(lerp) );
  1131. }
  1132. return to;
  1133. }
  1134. bool isActive() const
  1135. {
  1136. const double freq = double(bx::getHPFrequency() );
  1137. int64_t now = bx::getHPCounter();
  1138. float time = (float)(double(now - offset) / freq);
  1139. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  1140. return lerp < 1.0f;
  1141. }
  1142. };
  1143. typedef InterpolatorT<bx::lerp, bx::easeInOutQuad> Interpolator;
  1144. typedef InterpolatorT<bx::angleLerp, bx::easeInOutCubic> InterpolatorAngle;
  1145. typedef InterpolatorT<bx::lerp, bx::easeLinear> InterpolatorLinear;
  1146. void keyBindingHelp(const char* _bindings, const char* _description)
  1147. {
  1148. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
  1149. ImGui::SameLine(100);
  1150. ImGui::Text(_description);
  1151. }
  1152. void associate()
  1153. {
  1154. #if BX_PLATFORM_WINDOWS
  1155. std::string str;
  1156. char exec[bx::kMaxFilePath];
  1157. GetModuleFileNameA(GetModuleHandleA(NULL), exec, sizeof(exec) );
  1158. std::string strExec = bx::replaceAll<std::string>(exec, "\\", "\\\\");
  1159. std::string value;
  1160. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  1161. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  1162. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  1163. str += value;
  1164. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  1165. str += value;
  1166. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1167. {
  1168. const char* ext = s_supportedExt[ii];
  1169. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  1170. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  1171. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  1172. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  1173. }
  1174. bx::FilePath filePath(bx::Dir::Temp);
  1175. filePath.join("texture.reg");
  1176. bx::FileWriter writer;
  1177. bx::Error err;
  1178. if (bx::open(&writer, filePath, false, &err) )
  1179. {
  1180. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  1181. bx::close(&writer);
  1182. if (err.isOk() )
  1183. {
  1184. std::string cmd;
  1185. bx::stringPrintf(cmd, "/s %s", filePath.getCPtr() );
  1186. bx::ProcessReader reader;
  1187. if (bx::open(&reader, "regedit.exe", cmd.c_str(), &err) )
  1188. {
  1189. bx::close(&reader);
  1190. }
  1191. }
  1192. }
  1193. #elif BX_PLATFORM_LINUX
  1194. std::string str;
  1195. str += "#/bin/bash\n\n";
  1196. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1197. {
  1198. const char* ext = s_supportedExt[ii];
  1199. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  1200. }
  1201. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/x-dds\n");
  1202. str += "\n";
  1203. bx::FileWriter writer;
  1204. bx::Error err;
  1205. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  1206. {
  1207. bx::write(&writer, str.c_str(), uint32_t(str.length()), &err);
  1208. bx::close(&writer);
  1209. if (err.isOk() )
  1210. {
  1211. bx::ProcessReader reader;
  1212. if (bx::open(&reader, "/bin/bash", "/tmp/texturev.sh", &err) )
  1213. {
  1214. bx::close(&reader);
  1215. }
  1216. }
  1217. }
  1218. #endif // BX_PLATFORM_WINDOWS
  1219. }
  1220. void help(const char* _error = NULL)
  1221. {
  1222. if (NULL != _error)
  1223. {
  1224. fprintf(stderr, "Error:\n%s\n\n", _error);
  1225. }
  1226. fprintf(stderr
  1227. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1228. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  1229. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  1230. , BGFX_TEXTUREV_VERSION_MAJOR
  1231. , BGFX_TEXTUREV_VERSION_MINOR
  1232. , BGFX_API_VERSION
  1233. );
  1234. fprintf(stderr
  1235. , "Usage: texturev <file path>\n"
  1236. "\n"
  1237. "Supported input file types:\n"
  1238. );
  1239. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  1240. {
  1241. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  1242. }
  1243. fprintf(stderr
  1244. , "\n"
  1245. "Options:\n"
  1246. " -h, --help Help.\n"
  1247. " -v, --version Version information only.\n"
  1248. " --associate Associate file extensions with texturev.\n"
  1249. "\n"
  1250. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  1251. );
  1252. }
  1253. int _main_(int _argc, char** _argv)
  1254. {
  1255. bx::CommandLine cmdLine(_argc, _argv);
  1256. if (cmdLine.hasArg('v', "version") )
  1257. {
  1258. fprintf(stderr
  1259. , "texturev, bgfx texture viewer tool, version %d.%d.%d.\n"
  1260. , BGFX_TEXTUREV_VERSION_MAJOR
  1261. , BGFX_TEXTUREV_VERSION_MINOR
  1262. , BGFX_API_VERSION
  1263. );
  1264. return bx::kExitSuccess;
  1265. }
  1266. if (cmdLine.hasArg('h', "help") )
  1267. {
  1268. help();
  1269. return bx::kExitFailure;
  1270. }
  1271. else if (cmdLine.hasArg("associate") )
  1272. {
  1273. associate();
  1274. return bx::kExitFailure;
  1275. }
  1276. uint32_t debug = BGFX_DEBUG_TEXT;
  1277. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  1278. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1279. View view;
  1280. cmdAdd("view", cmdView, &view);
  1281. entry::setWindowFlags(entry::WindowHandle{0}, ENTRY_WINDOW_FLAG_ASPECT_RATIO, false);
  1282. entry::setWindowSize(entry::WindowHandle{0}, view.m_width, view.m_height);
  1283. bgfx::Init init;
  1284. init.resolution.width = view.m_width;
  1285. init.resolution.width = view.m_height;
  1286. init.resolution.reset = BGFX_RESET_VSYNC;
  1287. bgfx::init(init);
  1288. // Set view 0 clear state.
  1289. bgfx::setViewClear(BACKGROUND_VIEW_ID
  1290. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  1291. , 0x000000ff
  1292. , 1.0f
  1293. , 0
  1294. );
  1295. imguiCreate();
  1296. PosUvwColorVertex::init();
  1297. const bgfx::Caps* caps = bgfx::getCaps();
  1298. bgfx::RendererType::Enum type = caps->rendererType;
  1299. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  1300. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  1301. bgfx::UniformHandle u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Vec4);
  1302. bgfx::UniformHandle u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Vec4);
  1303. bgfx::ShaderHandle vsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture");
  1304. bgfx::ShaderHandle fsTexture = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture");
  1305. bgfx::ShaderHandle fsTextureArray = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_array");
  1306. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  1307. vsTexture
  1308. , fsTexture
  1309. , true
  1310. );
  1311. bgfx::ProgramHandle textureArrayProgram = bgfx::createProgram(
  1312. vsTexture
  1313. , bgfx::isValid(fsTextureArray)
  1314. ? fsTextureArray
  1315. : fsTexture
  1316. , true
  1317. );
  1318. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  1319. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_texture_cube")
  1320. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube")
  1321. , true
  1322. );
  1323. bgfx::ProgramHandle textureCube2Program = bgfx::createProgram(
  1324. vsTexture
  1325. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_cube2")
  1326. , true
  1327. );
  1328. bgfx::ProgramHandle textureSdfProgram = bgfx::createProgram(
  1329. vsTexture
  1330. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_sdf")
  1331. , true
  1332. );
  1333. bgfx::ProgramHandle textureMsdfProgram = bgfx::createProgram(
  1334. vsTexture
  1335. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_msdf")
  1336. , true
  1337. );
  1338. bgfx::ProgramHandle texture3DProgram = bgfx::createProgram(
  1339. vsTexture
  1340. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_texture_3d")
  1341. , true
  1342. );
  1343. const uint32_t checkerBoardSize = 64;
  1344. bgfx::TextureHandle checkerBoard;
  1345. {
  1346. const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize*checkerBoardSize*4);
  1347. bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xff8e8e8e, 0xff5d5d5d);
  1348. checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
  1349. , bgfx::TextureFormat::BGRA8
  1350. , 0
  1351. | BGFX_SAMPLER_MIN_POINT
  1352. | BGFX_SAMPLER_MIP_POINT
  1353. | BGFX_SAMPLER_MAG_POINT
  1354. , mem
  1355. );
  1356. }
  1357. float speed = 0.37f;
  1358. float time = 0.0f;
  1359. Interpolator menuFade(5.0f);
  1360. Interpolator mip(0.0f);
  1361. Interpolator layer(0.0f);
  1362. InterpolatorLinear ev(0.0f);
  1363. Interpolator zoom(1.0f);
  1364. Interpolator scale(1.0f);
  1365. Interpolator posx(0.0f);
  1366. Interpolator posy(0.0f);
  1367. InterpolatorAngle angle(0.0f);
  1368. InterpolatorAngle angx(0.0f);
  1369. InterpolatorAngle angy(0.0f);
  1370. auto anyActive = [&]() -> bool
  1371. {
  1372. return false
  1373. || ImGui::MouseOverArea()
  1374. || menuFade.isActive()
  1375. || mip.isActive()
  1376. || layer.isActive()
  1377. || ev.isActive()
  1378. || zoom.isActive()
  1379. || scale.isActive()
  1380. || posx.isActive()
  1381. || posy.isActive()
  1382. || angle.isActive()
  1383. || angx.isActive()
  1384. || angy.isActive()
  1385. ;
  1386. };
  1387. const char* filePath = _argc < 2 ? "" : _argv[1];
  1388. std::string path = filePath;
  1389. {
  1390. bx::FilePath fp(filePath);
  1391. view.updateFileList(fp);
  1392. }
  1393. int exitcode = bx::kExitSuccess;
  1394. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  1395. {
  1396. uint32_t fileIndex = 0;
  1397. bool dragging = false;
  1398. entry::WindowState windowState;
  1399. entry::MouseState mouseStatePrev;
  1400. while (!entry::processWindowEvents(windowState, debug, init.resolution.reset) )
  1401. {
  1402. const entry::MouseState& mouseState = windowState.m_mouse;
  1403. view.m_width = windowState.m_width;
  1404. view.m_height = windowState.m_height;
  1405. if (!windowState.m_dropFile.isEmpty() )
  1406. {
  1407. view.updateFileList(windowState.m_dropFile);
  1408. windowState.m_dropFile.clear();
  1409. }
  1410. imguiBeginFrame(mouseState.m_mx
  1411. , mouseState.m_my
  1412. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1413. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1414. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1415. , mouseState.m_mz
  1416. , uint16_t(view.m_width)
  1417. , uint16_t(view.m_height)
  1418. );
  1419. bool modalWindow = view.m_help || view.m_about;
  1420. bool overArea = false
  1421. || ImGui::GetMousePos().y <= ImGui::GetTextLineHeightWithSpacing()
  1422. || ImGui::MouseOverArea()
  1423. ;
  1424. overArea &= !modalWindow;
  1425. if (overArea)
  1426. {
  1427. menuFade.set(5.0f, 0.25f);
  1428. }
  1429. else if (modalWindow)
  1430. {
  1431. menuFade.reset(0.0f);
  1432. }
  1433. else
  1434. {
  1435. menuFade.set(0.0f, 2.0f);
  1436. }
  1437. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, bx::clamp(menuFade.getValue(), 0.0f, 1.0f) );
  1438. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  1439. if (ImGui::BeginMainMenuBar() )
  1440. {
  1441. if (ImGui::BeginMenu("File"))
  1442. {
  1443. if (ImGui::MenuItem("Open File") )
  1444. {
  1445. bx::FilePath tmp = view.m_path;
  1446. if (openFileSelectionDialog(
  1447. tmp
  1448. , FileSelectionDialogType::Open
  1449. , "texturev: Open File"
  1450. , s_filter
  1451. ) )
  1452. {
  1453. view.updateFileList(tmp);
  1454. }
  1455. }
  1456. if (ImGui::MenuItem("Show File List", NULL, view.m_files) )
  1457. {
  1458. cmdExec("view files");
  1459. }
  1460. ImGui::Separator();
  1461. if (ImGui::MenuItem("Exit") )
  1462. {
  1463. cmdExec("exit");
  1464. }
  1465. ImGui::EndMenu();
  1466. }
  1467. if (ImGui::BeginMenu("View") )
  1468. {
  1469. if (ImGui::MenuItem("Info", NULL, view.m_info) )
  1470. {
  1471. cmdExec("view info");
  1472. }
  1473. if (ImGui::MenuItem("Reset") )
  1474. {
  1475. cmdExec(s_resetCmd);
  1476. }
  1477. ImGui::Separator();
  1478. bool filter = view.m_filter;
  1479. if (ImGui::MenuItem("Filter", NULL, &filter) )
  1480. {
  1481. cmdExec("view filter");
  1482. }
  1483. bool animate = 0.0f < view.m_transitionTime;
  1484. if (ImGui::MenuItem("Animate", NULL, &animate) )
  1485. {
  1486. cmdExec("view transition %f", animate ? 1.0f : 0.0f);
  1487. }
  1488. if (ImGui::BeginMenu("Cubemap", view.m_textureInfo.cubeMap) )
  1489. {
  1490. if (ImGui::MenuItem("Quad", NULL, Geometry::Quad == view.m_cubeMapGeo) )
  1491. {
  1492. cmdExec("view geo quad");
  1493. }
  1494. if (ImGui::MenuItem("Cross", NULL, Geometry::Cross == view.m_cubeMapGeo) )
  1495. {
  1496. cmdExec("view geo cross");
  1497. }
  1498. if (ImGui::MenuItem("Hexagon", NULL, Geometry::Hexagon == view.m_cubeMapGeo) )
  1499. {
  1500. cmdExec("view geo hexagon");
  1501. }
  1502. ImGui::EndMenu();
  1503. }
  1504. if (ImGui::BeginMenu("Output") )
  1505. {
  1506. const bool hdrCap = (bgfx::getCaps()->supported & BGFX_CAPS_HDR10);
  1507. if (ImGui::MenuItem("sRGB", NULL, Output::sRGB == view.m_outputFormat) )
  1508. {
  1509. cmdExec("view output srgb");
  1510. }
  1511. if (hdrCap)
  1512. {
  1513. if (ImGui::MenuItem("scRGB", NULL, Output::scRGB == view.m_outputFormat) )
  1514. {
  1515. cmdExec("view output scrgb");
  1516. }
  1517. if (ImGui::MenuItem("HDR10", NULL, Output::HDR10 == view.m_outputFormat) )
  1518. {
  1519. cmdExec("view output hdr10");
  1520. }
  1521. }
  1522. ImGui::EndMenu();
  1523. }
  1524. bool sdf = view.m_sdf;
  1525. if (ImGui::MenuItem("SDF", NULL, &sdf) )
  1526. {
  1527. cmdExec("view sdf");
  1528. }
  1529. bool rr = 0 != (view.m_abgr & 0x000000ff);
  1530. if (ImGui::MenuItem("R", NULL, &rr) )
  1531. {
  1532. cmdExec("view rgb r");
  1533. }
  1534. bool gg = 0 != (view.m_abgr & 0x0000ff00);
  1535. if (ImGui::MenuItem("G", NULL, &gg) )
  1536. {
  1537. cmdExec("view rgb g");
  1538. }
  1539. bool bb = 0 != (view.m_abgr & 0x00ff0000);
  1540. if (ImGui::MenuItem("B", NULL, &bb) )
  1541. {
  1542. cmdExec("view rgb b");
  1543. }
  1544. bool alpha = view.m_alpha;
  1545. if (ImGui::MenuItem("Checkerboard", NULL, &alpha) )
  1546. {
  1547. cmdExec("view rgb a");
  1548. }
  1549. ImGui::Separator();
  1550. if (ImGui::MenuItem("Save Options") )
  1551. {
  1552. cmdExec("view save");
  1553. }
  1554. ImGui::EndMenu();
  1555. }
  1556. if (ImGui::BeginMenu("Help") )
  1557. {
  1558. if (ImGui::MenuItem("View Help") )
  1559. {
  1560. cmdExec("view help");
  1561. }
  1562. ImGui::Separator();
  1563. if (ImGui::MenuItem("About") )
  1564. {
  1565. cmdExec("view about");
  1566. }
  1567. ImGui::EndMenu();
  1568. }
  1569. if (0 != view.m_fileList.size() )
  1570. {
  1571. ImGui::Separator();
  1572. ImGui::TextColored(
  1573. ImVec4(0.0f, 1.0f, 1.0f, 1.0f)
  1574. , view.m_fileList[view.m_fileIndex].c_str()
  1575. );
  1576. ImGui::Separator();
  1577. const char* name = "";
  1578. if (view.m_textureInfo.cubeMap)
  1579. {
  1580. name = " CubeMap";
  1581. }
  1582. else if (1 < view.m_textureInfo.depth)
  1583. {
  1584. name = " 3D";
  1585. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1586. }
  1587. else if (1 < view.m_textureInfo.numLayers)
  1588. {
  1589. name = " 2D Array";
  1590. }
  1591. ImGui::Text("%d x %d%s, mips: %d, layers %d, %s"
  1592. , view.m_textureInfo.width
  1593. , view.m_textureInfo.height
  1594. , name
  1595. , view.m_textureInfo.numMips
  1596. , view.m_textureInfo.numLayers
  1597. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1598. );
  1599. }
  1600. ImGui::EndMainMenuBar();
  1601. }
  1602. ImGui::PopStyleVar(2);
  1603. static bool help = false;
  1604. static bool about = false;
  1605. static bool mouseDelta = false;
  1606. if (!mouseDelta)
  1607. {
  1608. mouseStatePrev = mouseState;
  1609. mouseDelta = true;
  1610. }
  1611. int32_t zoomDelta = overArea ? 0 : mouseState.m_mz - mouseStatePrev.m_mz;
  1612. if (zoomDelta != 0)
  1613. {
  1614. char exec[64];
  1615. bx::snprintf(exec, BX_COUNTOF(exec), "view zoom %+f", -zoomDelta*0.1f);
  1616. cmdExec(exec);
  1617. }
  1618. const float xDelta = float(mouseStatePrev.m_mx - mouseState.m_mx);
  1619. const float yDelta = float(mouseStatePrev.m_my - mouseState.m_my);
  1620. if (!overArea
  1621. && !help
  1622. && mouseState.m_buttons[entry::MouseButton::Left] != mouseStatePrev.m_buttons[entry::MouseButton::Left])
  1623. {
  1624. dragging = !!mouseState.m_buttons[entry::MouseButton::Left];
  1625. }
  1626. if (dragging)
  1627. {
  1628. if (view.m_textureInfo.cubeMap
  1629. && Geometry::Quad == view.m_cubeMapGeo)
  1630. {
  1631. char exec[64];
  1632. bx::snprintf(exec, BX_COUNTOF(exec), "view cubemap %+f %+f", -yDelta, -xDelta);
  1633. cmdExec(exec);
  1634. }
  1635. else
  1636. {
  1637. char exec[64];
  1638. bx::snprintf(exec, BX_COUNTOF(exec), "view pan %+f %+f", xDelta, yDelta);
  1639. cmdExec(exec);
  1640. }
  1641. }
  1642. mouseStatePrev = mouseState;
  1643. if (help != view.m_help)
  1644. {
  1645. if (!help)
  1646. {
  1647. ImGui::OpenPopup("Help");
  1648. inputRemoveBindings(s_bindingName[Binding::View]);
  1649. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  1650. }
  1651. else
  1652. {
  1653. inputRemoveBindings(s_bindingName[Binding::Help]);
  1654. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1655. }
  1656. help = view.m_help;
  1657. }
  1658. if (about != view.m_about)
  1659. {
  1660. if (!about)
  1661. {
  1662. ImGui::OpenPopup("About");
  1663. inputRemoveBindings(s_bindingName[Binding::View]);
  1664. inputAddBindings(s_bindingName[Binding::About], s_binding[Binding::About]);
  1665. }
  1666. else
  1667. {
  1668. inputRemoveBindings(s_bindingName[Binding::About]);
  1669. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  1670. }
  1671. about = view.m_about;
  1672. }
  1673. if (view.m_info)
  1674. {
  1675. ImGui::SetNextWindowSize(
  1676. ImVec2(300.0f, 320.0f)
  1677. , ImGuiCond_FirstUseEver
  1678. );
  1679. if (ImGui::Begin("Info", &view.m_info) )
  1680. {
  1681. if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
  1682. {
  1683. if (!bgfx::isValid(texture) )
  1684. {
  1685. ImGui::Text("Texture is not loaded.");
  1686. }
  1687. else
  1688. {
  1689. ImGui::Text("Name: %s", view.m_fileList[view.m_fileIndex].c_str() );
  1690. ImGui::Text("Dimensions: %d x %d"
  1691. , view.m_textureInfo.width
  1692. , view.m_textureInfo.height
  1693. );
  1694. ImGui::Text("Format: %s"
  1695. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1696. );
  1697. uint32_t numLayers = view.m_textureInfo.numLayers;
  1698. if (1 < view.m_textureInfo.depth)
  1699. {
  1700. numLayers = bx::max(1, view.m_textureInfo.depth >> view.m_mip);
  1701. }
  1702. view.m_layer = bx::clamp<int32_t>(view.m_layer, 0, numLayers - 1);
  1703. ImGui::SliderInt("Layer", (int32_t*)&view.m_layer, 0, numLayers - 1);
  1704. ImGui::SliderInt("Mip", (int32_t*)&view.m_mip, 0, view.m_textureInfo.numMips - 1);
  1705. ImGui::Separator();
  1706. ImGui::Checkbox("Input linear", &view.m_inLinear);
  1707. ImGui::RangeSliderFloat("EV range", &view.m_evMin, &view.m_evMax, kEvMin, kEvMax);
  1708. ImGui::SliderFloat("EV", &view.m_ev, view.m_evMin, view.m_evMax);
  1709. ImGui::Separator();
  1710. ImGui::Checkbox("Fit to window", &view.m_fit);
  1711. ImGui::SliderFloat("Scale", &view.m_zoom, 0.01f, 10.0f);
  1712. }
  1713. ImGui::EndChild();
  1714. }
  1715. }
  1716. ImGui::End();
  1717. }
  1718. if (view.m_files)
  1719. {
  1720. char temp[bx::kMaxFilePath];
  1721. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.getCPtr() );
  1722. ImGui::SetNextWindowSize(
  1723. ImVec2(400.0f, 400.0f)
  1724. , ImGuiCond_FirstUseEver
  1725. );
  1726. if (ImGui::Begin(temp, &view.m_files) )
  1727. {
  1728. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  1729. {
  1730. ImGui::PushFont(ImGui::Font::Mono);
  1731. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  1732. const float listHeight =
  1733. bx::max(1.0f, bx::floor(ImGui::GetWindowHeight()/itemHeight) )
  1734. * itemHeight
  1735. ;
  1736. ImGui::PushItemWidth(-1);
  1737. if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
  1738. {
  1739. const int32_t itemCount = int32_t(view.m_fileList.size() );
  1740. int32_t start, end;
  1741. ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);
  1742. const int32_t index = int32_t(view.m_fileIndex);
  1743. if (index <= start)
  1744. {
  1745. ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
  1746. }
  1747. else if (index >= end)
  1748. {
  1749. ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
  1750. }
  1751. ImGuiListClipper clipper(itemCount, itemHeight);
  1752. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  1753. {
  1754. ImGui::PushID(pos);
  1755. bool isSelected = uint32_t(pos) == view.m_fileIndex;
  1756. if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
  1757. {
  1758. view.m_fileIndex = pos;
  1759. }
  1760. ImGui::PopID();
  1761. }
  1762. clipper.End();
  1763. ImGui::ListBoxFooter();
  1764. }
  1765. ImGui::PopFont();
  1766. ImGui::EndChild();
  1767. }
  1768. }
  1769. ImGui::End();
  1770. }
  1771. if (ImGui::BeginPopupModal("About", &view.m_about, ImGuiWindowFlags_AlwaysAutoResize) )
  1772. {
  1773. ImGui::SetWindowFontScale(1.0f);
  1774. ImGui::Text(
  1775. "texturev, bgfx texture viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
  1776. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  1777. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  1778. , BGFX_TEXTUREV_VERSION_MAJOR
  1779. , BGFX_TEXTUREV_VERSION_MINOR
  1780. , BGFX_API_VERSION
  1781. );
  1782. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1783. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1784. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1785. || !view.m_about)
  1786. {
  1787. view.m_about = false;
  1788. ImGui::CloseCurrentPopup();
  1789. }
  1790. ImGui::EndPopup();
  1791. }
  1792. if (ImGui::BeginPopupModal("Help", &view.m_help, ImGuiWindowFlags_AlwaysAutoResize) )
  1793. {
  1794. ImGui::SetWindowFontScale(1.0f);
  1795. ImGui::Text("Key bindings:\n\n");
  1796. ImGui::PushFont(ImGui::Font::Mono);
  1797. keyBindingHelp("ESC", "Exit.");
  1798. keyBindingHelp("h", "Toggle help screen.");
  1799. keyBindingHelp("f", "Toggle full-screen.");
  1800. ImGui::NextLine();
  1801. keyBindingHelp("LMB+drag", "Pan.");
  1802. keyBindingHelp("=/- or MW", "Zoom in/out.");
  1803. keyBindingHelp("z/Z", "Rotate.");
  1804. keyBindingHelp("0", "Reset.");
  1805. keyBindingHelp("1", "Fit to window.");
  1806. ImGui::NextLine();
  1807. keyBindingHelp("<", "Reset MIP level.");
  1808. keyBindingHelp(",/,", "MIP level up/down.");
  1809. keyBindingHelp("/", "Toggle linear/point texture sampling.");
  1810. keyBindingHelp("[space]", "Change cubemap mode.");
  1811. ImGui::NextLine();
  1812. keyBindingHelp("left", "Previous layer in texture array.");
  1813. keyBindingHelp("right", "Next layer in texture array.");
  1814. ImGui::NextLine();
  1815. keyBindingHelp("up", "Previous texture.");
  1816. keyBindingHelp("down", "Next texture.");
  1817. ImGui::NextLine();
  1818. keyBindingHelp("r/g/b", "Toggle R, G, or B color channel.");
  1819. keyBindingHelp("a", "Toggle alpha blending.");
  1820. ImGui::NextLine();
  1821. keyBindingHelp("s", "Toggle Multi-channel SDF rendering");
  1822. ImGui::NextLine();
  1823. ImGui::PopFont();
  1824. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  1825. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  1826. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  1827. || !view.m_help)
  1828. {
  1829. view.m_help = false;
  1830. ImGui::CloseCurrentPopup();
  1831. }
  1832. ImGui::EndPopup();
  1833. }
  1834. imguiEndFrame();
  1835. if ( (!bgfx::isValid(texture) || view.m_fileIndex != fileIndex)
  1836. && 0 != view.m_fileList.size() )
  1837. {
  1838. if (bgfx::isValid(texture) )
  1839. {
  1840. bgfx::destroy(texture);
  1841. }
  1842. fileIndex = view.m_fileIndex;
  1843. bx::FilePath fp = view.m_path;
  1844. fp.join(view.m_fileList[view.m_fileIndex].c_str() );
  1845. bimg::Orientation::Enum orientation;
  1846. texture = loadTexture(fp.getCPtr()
  1847. , 0
  1848. | BGFX_SAMPLER_U_CLAMP
  1849. | BGFX_SAMPLER_V_CLAMP
  1850. | BGFX_SAMPLER_W_CLAMP
  1851. , 0
  1852. , &view.m_textureInfo
  1853. , &orientation
  1854. );
  1855. bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(view.m_textureInfo.format);
  1856. if (format < bimg::TextureFormat::Count)
  1857. {
  1858. view.m_inLinear = bimg::isFloat(format);
  1859. switch (orientation)
  1860. {
  1861. default:
  1862. case bimg::Orientation::R0: cmdExec("view orientation\nview orientation z 0"); break;
  1863. case bimg::Orientation::R90: cmdExec("view orientation\nview orientation z -90"); break;
  1864. case bimg::Orientation::R180: cmdExec("view orientation\nview orientation z -180"); break;
  1865. case bimg::Orientation::R270: cmdExec("view orientation\nview orientation z -270"); break;
  1866. case bimg::Orientation::HFlip: cmdExec("view orientation\nview orientation x -180"); break;
  1867. case bimg::Orientation::HFlipR90: cmdExec("view orientation\nview orientation z -90\nview orientation x -180"); break;
  1868. case bimg::Orientation::HFlipR270: cmdExec("view orientation\nview orientation z -270\nview orientation x -180"); break;
  1869. case bimg::Orientation::VFlip: cmdExec("view orientation\nview orientation y -180"); break;
  1870. }
  1871. }
  1872. std::string title;
  1873. if (isValid(texture) )
  1874. {
  1875. const char* name = "";
  1876. if (view.m_textureInfo.cubeMap)
  1877. {
  1878. name = " CubeMap";
  1879. }
  1880. else if (1 < view.m_textureInfo.depth)
  1881. {
  1882. name = " 3D";
  1883. view.m_textureInfo.numLayers = view.m_textureInfo.depth;
  1884. }
  1885. else if (1 < view.m_textureInfo.numLayers)
  1886. {
  1887. name = " 2D Array";
  1888. }
  1889. bx::stringPrintf(title, "%s (%d x %d%s, mips: %d, layers %d, %s)"
  1890. , fp.getCPtr()
  1891. , view.m_textureInfo.width
  1892. , view.m_textureInfo.height
  1893. , name
  1894. , view.m_textureInfo.numMips
  1895. , view.m_textureInfo.numLayers
  1896. , bimg::getName(bimg::TextureFormat::Enum(view.m_textureInfo.format) )
  1897. );
  1898. }
  1899. else
  1900. {
  1901. bx::stringPrintf(title, "Failed to load %s!", filePath);
  1902. }
  1903. entry::WindowHandle handle = { 0 };
  1904. entry::setWindowTitle(handle, title.c_str() );
  1905. }
  1906. int64_t now = bx::getHPCounter();
  1907. static int64_t last = now;
  1908. const int64_t frameTime = now - last;
  1909. last = now;
  1910. const double freq = double(bx::getHPFrequency() );
  1911. time += (float)(frameTime*speed/freq);
  1912. float transitionTime = dragging ? 0.0f : 0.25f*view.m_transitionTime;
  1913. posx.set(view.m_posx, transitionTime);
  1914. posy.set(view.m_posy, transitionTime);
  1915. float ortho[16];
  1916. bx::mtxOrtho(
  1917. ortho
  1918. , 0.0f
  1919. , float(view.m_width)
  1920. , float(view.m_height)
  1921. , 0.0f
  1922. , 0.0f
  1923. , 1000.0f
  1924. , 0.0f
  1925. , caps->homogeneousDepth
  1926. );
  1927. bgfx::setViewTransform(BACKGROUND_VIEW_ID, NULL, ortho);
  1928. bgfx::setViewRect(BACKGROUND_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  1929. setGeometry(Geometry::Quad
  1930. , 0
  1931. , 0
  1932. , view.m_width
  1933. , view.m_height
  1934. , view.m_alpha || !bgfx::isValid(texture) ? UINT32_MAX : 0
  1935. , float(view.m_width )/float(checkerBoardSize)
  1936. , float(view.m_height)/float(checkerBoardSize)
  1937. );
  1938. bgfx::setTexture(0
  1939. , s_texColor
  1940. , checkerBoard
  1941. );
  1942. bgfx::setState(0
  1943. | BGFX_STATE_WRITE_RGB
  1944. | BGFX_STATE_WRITE_A
  1945. );
  1946. bgfx::submit(BACKGROUND_VIEW_ID
  1947. , textureProgram
  1948. );
  1949. float px = posx.getValue();
  1950. float py = posy.getValue();
  1951. bx::mtxOrtho(
  1952. ortho
  1953. , px-view.m_width/2.0f
  1954. , px+view.m_width/2.0f
  1955. , py+view.m_height/2.0f
  1956. , py-view.m_height/2.0f
  1957. , -10.0f
  1958. , 10.0f
  1959. , 0.0f
  1960. , caps->homogeneousDepth
  1961. );
  1962. bgfx::setViewTransform(IMAGE_VIEW_ID, NULL, ortho);
  1963. bgfx::setViewRect(IMAGE_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  1964. bgfx::dbgTextClear();
  1965. float orientation[16];
  1966. bx::mtxRotateXYZ(orientation, view.m_flipH, view.m_flipV, angle.getValue()+view.m_orientation);
  1967. if (view.m_fit)
  1968. {
  1969. const bx::Vec3 wh = { float(view.m_textureInfo.width), float(view.m_textureInfo.height), 0.0f };
  1970. const bx::Vec3 result = bx::round(bx::abs(bx::mul(wh, orientation) ) );
  1971. scale.set(bx::min(float(view.m_width) / result.x
  1972. , float(view.m_height) / result.y)
  1973. , 0.1f*view.m_transitionTime
  1974. );
  1975. }
  1976. else
  1977. {
  1978. scale.set(1.0f, 0.1f*view.m_transitionTime);
  1979. }
  1980. zoom.set(view.m_zoom, transitionTime);
  1981. angle.set(view.m_angle, transitionTime);
  1982. angx.set(view.m_angx, transitionTime);
  1983. angy.set(view.m_angy, transitionTime);
  1984. float ss = scale.getValue()
  1985. * zoom.getValue()
  1986. ;
  1987. setGeometry(view.m_textureInfo.cubeMap ? view.m_cubeMapGeo : Geometry::Quad
  1988. , -int(view.m_textureInfo.width * ss)/2
  1989. , -int(view.m_textureInfo.height * ss)/2
  1990. , int(view.m_textureInfo.width * ss)
  1991. , int(view.m_textureInfo.height * ss)
  1992. , view.m_abgr
  1993. );
  1994. bgfx::setTransform(orientation);
  1995. float mtx[16];
  1996. bx::mtxRotateXY(mtx, angx.getValue(), angy.getValue() );
  1997. bgfx::setUniform(u_mtx, mtx);
  1998. mip.set(float(view.m_mip), 0.5f*view.m_transitionTime);
  1999. layer.set(float(view.m_layer), 0.25f*view.m_transitionTime);
  2000. ev.set(view.m_ev, 0.5f*view.m_transitionTime);
  2001. float params[4] = { mip.getValue(), layer.getValue(), view.m_inLinear ? 1.0f : 0.0f, ev.getValue() };
  2002. if (1 < view.m_textureInfo.depth)
  2003. {
  2004. params[1] = layer.getValue()/float(bx::max(1, view.m_textureInfo.depth >> view.m_mip) );
  2005. }
  2006. bgfx::setUniform(u_params0, params);
  2007. float params1[4] = { float(view.m_outputFormat), 80.0f, 0.0, 0.0f };
  2008. bgfx::setUniform(u_params1, params1);
  2009. const uint32_t textureFlags = 0
  2010. | BGFX_SAMPLER_U_CLAMP
  2011. | BGFX_SAMPLER_V_CLAMP
  2012. | BGFX_SAMPLER_W_CLAMP
  2013. | (view.m_filter ? 0 : 0
  2014. | BGFX_SAMPLER_MIN_POINT
  2015. | BGFX_SAMPLER_MIP_POINT
  2016. | BGFX_SAMPLER_MAG_POINT
  2017. )
  2018. ;
  2019. bgfx::setTexture(0
  2020. , s_texColor
  2021. , texture
  2022. , textureFlags
  2023. );
  2024. bgfx::setState(0
  2025. | BGFX_STATE_WRITE_RGB
  2026. | BGFX_STATE_WRITE_A
  2027. | (view.m_alpha ? BGFX_STATE_BLEND_ALPHA : BGFX_STATE_NONE)
  2028. );
  2029. bgfx:: ProgramHandle program = textureProgram;
  2030. if (1 < view.m_textureInfo.depth)
  2031. {
  2032. program = texture3DProgram;
  2033. }
  2034. else if (view.m_textureInfo.cubeMap)
  2035. {
  2036. program = Geometry::Quad == view.m_cubeMapGeo
  2037. ? textureCubeProgram
  2038. : textureCube2Program
  2039. ;
  2040. }
  2041. else if (1 < view.m_textureInfo.numLayers)
  2042. {
  2043. program = textureArrayProgram;
  2044. }
  2045. else if (view.m_sdf)
  2046. {
  2047. if (8 < bimg::getBitsPerPixel(bimg::TextureFormat::Enum(view.m_textureInfo.format) ) )
  2048. {
  2049. program = textureMsdfProgram;
  2050. }
  2051. else
  2052. {
  2053. program = textureSdfProgram;
  2054. }
  2055. }
  2056. if (bgfx::isValid(texture) )
  2057. {
  2058. bgfx::submit(IMAGE_VIEW_ID, program);
  2059. }
  2060. else
  2061. {
  2062. bgfx::discard();
  2063. }
  2064. bgfx::frame();
  2065. // Slow down when nothing is animating...
  2066. if (!dragging
  2067. && !anyActive() )
  2068. {
  2069. bx::sleep(100);
  2070. }
  2071. }
  2072. }
  2073. if (bgfx::isValid(texture) )
  2074. {
  2075. bgfx::destroy(texture);
  2076. }
  2077. bgfx::destroy(checkerBoard);
  2078. bgfx::destroy(s_texColor);
  2079. bgfx::destroy(u_mtx);
  2080. bgfx::destroy(u_params0);
  2081. bgfx::destroy(u_params1);
  2082. bgfx::destroy(textureProgram);
  2083. bgfx::destroy(textureArrayProgram);
  2084. bgfx::destroy(textureCubeProgram);
  2085. bgfx::destroy(textureCube2Program);
  2086. bgfx::destroy(textureSdfProgram);
  2087. bgfx::destroy(textureMsdfProgram);
  2088. bgfx::destroy(texture3DProgram);
  2089. imguiDestroy();
  2090. bgfx::shutdown();
  2091. return exitcode;
  2092. }