texturev.cpp 55 KB

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