texturev.cpp 50 KB

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