texturev.cpp 56 KB

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