texturev.cpp 54 KB

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