texturev.cpp 55 KB

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