texturev.cpp 55 KB

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