2
0

texturev.cpp 53 KB

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