texturev.cpp 49 KB

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