texturev.cpp 35 KB

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