texturev.cpp 51 KB

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