texturev.cpp 43 KB

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