texturev.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bx/os.h>
  8. #include <bx/string.h>
  9. #include <bx/uint32_t.h>
  10. #include <entry/entry.h>
  11. #include <entry/input.h>
  12. #include <entry/cmd.h>
  13. #include <imgui/imgui.h>
  14. #include <bgfx_utils.h>
  15. #include <dirent.h>
  16. #include "vs_texture.bin.h"
  17. #include "fs_texture.bin.h"
  18. #include "vs_texture_cube.bin.h"
  19. #include "fs_texture_cube.bin.h"
  20. #include <bx/crtimpl.h>
  21. #include <tinystl/allocator.h>
  22. #include <tinystl/vector.h>
  23. #include <string>
  24. namespace stl = tinystl;
  25. #include "image.h"
  26. struct Binding
  27. {
  28. enum Enum
  29. {
  30. App,
  31. View,
  32. Count
  33. };
  34. };
  35. static const InputBinding s_bindingApp[] =
  36. {
  37. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  38. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  39. INPUT_BINDING_END
  40. };
  41. static const InputBinding s_bindingView[] =
  42. {
  43. { entry::Key::Comma, entry::Modifier::None, 1, NULL, "view mip prev" },
  44. { entry::Key::Period, entry::Modifier::None, 1, NULL, "view mip next" },
  45. { entry::Key::Comma, entry::Modifier::LeftShift, 1, NULL, "view mip" },
  46. { entry::Key::Comma, entry::Modifier::RightShift, 1, NULL, "view mip" },
  47. { entry::Key::Slash, entry::Modifier::None, 1, NULL, "view filter" },
  48. { entry::Key::Key0, entry::Modifier::None, 1, NULL, "view zoom 1.0" },
  49. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view zoom +0.1" },
  50. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view zoom -0.1" },
  51. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view file-up" },
  52. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view file-down" },
  53. { entry::Key::PageUp, entry::Modifier::None, 1, NULL, "view file-pgup" },
  54. { entry::Key::PageDown, entry::Modifier::None, 1, NULL, "view file-pgdown" },
  55. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  56. INPUT_BINDING_END
  57. };
  58. static const char* s_bindingName[] =
  59. {
  60. "App",
  61. "View",
  62. };
  63. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  64. static const InputBinding* s_binding[] =
  65. {
  66. s_bindingApp,
  67. s_bindingView,
  68. };
  69. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  70. struct View
  71. {
  72. View()
  73. : m_fileIndex(0)
  74. , m_scaleFn(0)
  75. , m_mip(0)
  76. , m_zoom(1.0f)
  77. , m_filter(true)
  78. , m_help(false)
  79. {
  80. }
  81. ~View()
  82. {
  83. }
  84. int32_t cmd(int32_t _argc, char const* const* _argv)
  85. {
  86. if (_argc >= 2)
  87. {
  88. if (0 == strcmp(_argv[1], "mip") )
  89. {
  90. if (_argc >= 3)
  91. {
  92. uint32_t mip = m_mip;
  93. if (0 == strcmp(_argv[2], "next") )
  94. {
  95. ++mip;
  96. }
  97. else if (0 == strcmp(_argv[2], "prev") )
  98. {
  99. --mip;
  100. }
  101. else if (0 == strcmp(_argv[2], "last") )
  102. {
  103. mip = INT32_MAX;
  104. }
  105. else
  106. {
  107. mip = atoi(_argv[2]);
  108. }
  109. m_mip = bx::uint32_iclamp(mip, 0, m_info.numMips-1);
  110. }
  111. else
  112. {
  113. m_mip = 0;
  114. }
  115. }
  116. else if (0 == strcmp(_argv[1], "zoom") )
  117. {
  118. if (_argc >= 3)
  119. {
  120. float zoom = (float)atof(_argv[2]);
  121. if (_argv[2][0] == '+'
  122. || _argv[2][0] == '-')
  123. {
  124. m_zoom += zoom;
  125. }
  126. else
  127. {
  128. m_zoom = zoom;
  129. }
  130. m_zoom = bx::fclamp(m_zoom, 0.001f, 10.0f);
  131. }
  132. else
  133. {
  134. m_zoom = 1.0f;
  135. }
  136. }
  137. else if (0 == strcmp(_argv[1], "filter") )
  138. {
  139. if (_argc >= 3)
  140. {
  141. m_filter = bx::toBool(_argv[2]);
  142. }
  143. else
  144. {
  145. m_filter ^= true;
  146. }
  147. }
  148. else if (0 == strcmp(_argv[1], "file-up") )
  149. {
  150. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  151. }
  152. else if (0 == strcmp(_argv[1], "file-down") )
  153. {
  154. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  155. ++m_fileIndex;
  156. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  157. }
  158. else if (0 == strcmp(_argv[1], "help") )
  159. {
  160. m_help ^= true;
  161. }
  162. }
  163. return 0;
  164. }
  165. void updateFileList(const char* _path, const char* _fileName = "")
  166. {
  167. std::string path = _path;
  168. DIR* dir = opendir(_path);
  169. if (NULL == dir)
  170. {
  171. path = ".";
  172. }
  173. dir = opendir(path.c_str() );
  174. if (NULL != dir)
  175. {
  176. for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
  177. {
  178. if (0 == (item->d_type & DT_DIR) )
  179. {
  180. const char* ext = strrchr(item->d_name, '.');
  181. if (NULL != ext)
  182. {
  183. if (0 == bx::stricmp(ext, ".dds")
  184. || 0 == bx::stricmp(ext, ".jpg")
  185. || 0 == bx::stricmp(ext, ".jpeg")
  186. || 0 == bx::stricmp(ext, ".hdr")
  187. || 0 == bx::stricmp(ext, ".ktx")
  188. || 0 == bx::stricmp(ext, ".png")
  189. || 0 == bx::stricmp(ext, ".pvr")
  190. || 0 == bx::stricmp(ext, ".tga")
  191. )
  192. {
  193. if (0 == strcmp(_fileName, item->d_name) )
  194. {
  195. m_fileIndex = uint32_t(m_fileList.size() );
  196. }
  197. std::string name = path;
  198. char ch = name.back();
  199. name += '/' == ch || '\\' == ch ? "" : "/";
  200. name += item->d_name;
  201. m_fileList.push_back(name);
  202. }
  203. }
  204. }
  205. }
  206. closedir(dir);
  207. }
  208. }
  209. typedef stl::vector<std::string> FileList;
  210. FileList m_fileList;
  211. bgfx::TextureInfo m_info;
  212. uint32_t m_fileIndex;
  213. uint32_t m_scaleFn;
  214. uint32_t m_mip;
  215. float m_zoom;
  216. bool m_filter;
  217. bool m_help;
  218. };
  219. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  220. {
  221. View* view = static_cast<View*>(_userData);
  222. return view->cmd(_argc, _argv);
  223. }
  224. struct PosUvColorVertex
  225. {
  226. float m_x;
  227. float m_y;
  228. float m_u;
  229. float m_v;
  230. uint32_t m_abgr;
  231. static void init()
  232. {
  233. ms_decl
  234. .begin()
  235. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  236. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  237. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  238. .end();
  239. }
  240. static bgfx::VertexDecl ms_decl;
  241. };
  242. bgfx::VertexDecl PosUvColorVertex::ms_decl;
  243. bool screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
  244. {
  245. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvColorVertex::ms_decl) )
  246. {
  247. bgfx::TransientVertexBuffer vb;
  248. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvColorVertex::ms_decl);
  249. PosUvColorVertex* vertex = (PosUvColorVertex*)vb.data;
  250. const float widthf = float(_width);
  251. const float heightf = float(_height);
  252. const float minx = float(_x);
  253. const float miny = float(_y);
  254. const float maxx = minx+widthf;
  255. const float maxy = miny+heightf;
  256. float m_halfTexel = 0.0f;
  257. const float texelHalfW = m_halfTexel/widthf;
  258. const float texelHalfH = m_halfTexel/heightf;
  259. const float minu = texelHalfW;
  260. const float maxu = 1.0f - texelHalfW;
  261. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  262. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  263. vertex[0].m_x = minx;
  264. vertex[0].m_y = miny;
  265. vertex[0].m_u = minu;
  266. vertex[0].m_v = minv;
  267. vertex[1].m_x = maxx;
  268. vertex[1].m_y = miny;
  269. vertex[1].m_u = maxu;
  270. vertex[1].m_v = minv;
  271. vertex[2].m_x = maxx;
  272. vertex[2].m_y = maxy;
  273. vertex[2].m_u = maxu;
  274. vertex[2].m_v = maxv;
  275. vertex[3].m_x = maxx;
  276. vertex[3].m_y = maxy;
  277. vertex[3].m_u = maxu;
  278. vertex[3].m_v = maxv;
  279. vertex[4].m_x = minx;
  280. vertex[4].m_y = maxy;
  281. vertex[4].m_u = minu;
  282. vertex[4].m_v = maxv;
  283. vertex[5].m_x = minx;
  284. vertex[5].m_y = miny;
  285. vertex[5].m_u = minu;
  286. vertex[5].m_v = minv;
  287. vertex[0].m_abgr = UINT32_MAX;
  288. vertex[1].m_abgr = UINT32_MAX;
  289. vertex[2].m_abgr = UINT32_MAX;
  290. vertex[3].m_abgr = UINT32_MAX;
  291. vertex[4].m_abgr = UINT32_MAX;
  292. vertex[5].m_abgr = UINT32_MAX;
  293. bgfx::setVertexBuffer(&vb);
  294. return true;
  295. }
  296. return false;
  297. }
  298. struct Interpolator
  299. {
  300. float from;
  301. float to;
  302. float duration;
  303. int64_t offset;
  304. Interpolator(float _value)
  305. {
  306. reset(_value);
  307. }
  308. void reset(float _value)
  309. {
  310. from = _value;
  311. to = _value;
  312. duration = 0.0;
  313. offset = bx::getHPCounter();
  314. }
  315. void set(float _value, float _duration)
  316. {
  317. if (_value != to)
  318. {
  319. from = getValue();
  320. to = _value;
  321. duration = _duration;
  322. offset = bx::getHPCounter();
  323. }
  324. }
  325. float getValue()
  326. {
  327. if (duration > 0.0)
  328. {
  329. const double freq = double(bx::getHPFrequency() );
  330. int64_t now = bx::getHPCounter();
  331. float time = (float)(double(now - offset) / freq);
  332. float lerp = bx::fclamp(time, 0.0, duration) / duration;
  333. return bx::flerp(from, to, lerp);
  334. }
  335. return to;
  336. }
  337. };
  338. void help(const char* _error = NULL)
  339. {
  340. if (NULL != _error)
  341. {
  342. fprintf(stderr, "Error:\n%s\n\n", _error);
  343. }
  344. fprintf(stderr
  345. , "texturev, bgfx texture viewer tool\n"
  346. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  347. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  348. );
  349. fprintf(stderr
  350. , "Usage: texturev <file path>\n"
  351. "\n"
  352. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  353. );
  354. }
  355. int _main_(int _argc, char** _argv)
  356. {
  357. uint32_t width = 1280;
  358. uint32_t height = 720;
  359. uint32_t debug = BGFX_DEBUG_TEXT;
  360. uint32_t reset = BGFX_RESET_VSYNC;
  361. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  362. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  363. View view;
  364. cmdAdd("view", cmdView, &view);
  365. bgfx::init();
  366. bgfx::reset(width, height, reset);
  367. // Set view 0 clear state.
  368. bgfx::setViewClear(0
  369. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  370. , 0x101010ff
  371. , 1.0f
  372. , 0
  373. );
  374. imguiCreate();
  375. PosUvColorVertex::init();
  376. const bgfx::Memory* vs_texture;
  377. const bgfx::Memory* fs_texture;
  378. const bgfx::Memory* vs_texture_cube;
  379. const bgfx::Memory* fs_texture_cube;
  380. switch (bgfx::getRendererType())
  381. {
  382. case bgfx::RendererType::Direct3D9:
  383. vs_texture = bgfx::makeRef(vs_texture_dx9, sizeof(vs_texture_dx9) );
  384. fs_texture = bgfx::makeRef(fs_texture_dx9, sizeof(fs_texture_dx9) );
  385. vs_texture_cube = bgfx::makeRef(vs_texture_cube_dx9, sizeof(vs_texture_cube_dx9) );
  386. fs_texture_cube = bgfx::makeRef(fs_texture_cube_dx9, sizeof(fs_texture_cube_dx9) );
  387. break;
  388. case bgfx::RendererType::Direct3D11:
  389. case bgfx::RendererType::Direct3D12:
  390. vs_texture = bgfx::makeRef(vs_texture_dx11, sizeof(vs_texture_dx11) );
  391. fs_texture = bgfx::makeRef(fs_texture_dx11, sizeof(fs_texture_dx11) );
  392. vs_texture_cube = bgfx::makeRef(vs_texture_cube_dx11, sizeof(vs_texture_cube_dx11) );
  393. fs_texture_cube = bgfx::makeRef(fs_texture_cube_dx11, sizeof(fs_texture_cube_dx11) );
  394. break;
  395. default:
  396. vs_texture = bgfx::makeRef(vs_texture_glsl, sizeof(vs_texture_glsl) );
  397. fs_texture = bgfx::makeRef(fs_texture_glsl, sizeof(fs_texture_glsl) );
  398. vs_texture_cube = bgfx::makeRef(vs_texture_cube_glsl, sizeof(vs_texture_cube_glsl) );
  399. fs_texture_cube = bgfx::makeRef(fs_texture_cube_glsl, sizeof(fs_texture_cube_glsl) );
  400. break;
  401. }
  402. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  403. bgfx::createShader(vs_texture)
  404. , bgfx::createShader(fs_texture)
  405. , true
  406. );
  407. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  408. bgfx::createShader(vs_texture_cube)
  409. , bgfx::createShader(fs_texture_cube)
  410. , true
  411. );
  412. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  413. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  414. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  415. float speed = 0.37f;
  416. float time = 0.0f;
  417. Interpolator mip(0.0);
  418. Interpolator zoom(1.0);
  419. Interpolator scale(1.0);
  420. const char* filePath = _argc < 2 ? "" : _argv[1];
  421. bool directory = false;
  422. bx::FileInfo fi;
  423. bx::stat(filePath, fi);
  424. directory = bx::FileInfo::Directory == fi.m_type;
  425. std::string path = filePath;
  426. if (!directory)
  427. {
  428. const char* fileName = directory ? filePath : bx::baseName(filePath);
  429. path.assign(filePath, fileName);
  430. view.updateFileList(path.c_str(), fileName);
  431. }
  432. else
  433. {
  434. view.updateFileList(path.c_str() );
  435. }
  436. int exitcode = EXIT_SUCCESS;
  437. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  438. if (view.m_fileList.empty() )
  439. {
  440. exitcode = EXIT_FAILURE;
  441. if (2 > _argc)
  442. {
  443. help("File path is not specified.");
  444. }
  445. else
  446. {
  447. fprintf(stderr, "Unable to load '%s' texture.\n", filePath);
  448. }
  449. }
  450. else
  451. {
  452. uint32_t fileIndex = 0;
  453. entry::MouseState mouseState;
  454. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  455. {
  456. imguiBeginFrame(mouseState.m_mx
  457. , mouseState.m_my
  458. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  459. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  460. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  461. , mouseState.m_mz
  462. , width
  463. , height
  464. );
  465. static bool help = false;
  466. if (help == false
  467. && help != view.m_help)
  468. {
  469. ImGui::OpenPopup("Help");
  470. }
  471. if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize) )
  472. {
  473. ImGui::SetWindowFontScale(1.2f);
  474. ImGui::Text(
  475. "texturev, bgfx texture viewer tool\n"
  476. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  477. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  478. );
  479. ImGui::Separator();
  480. ImGui::NextLine();
  481. ImGui::Text("Key bindings:\n\n");
  482. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "ESC"); ImGui::SameLine(64); ImGui::Text("Exit.");
  483. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "h"); ImGui::SameLine(64); ImGui::Text("Toggle help screen.");
  484. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "f"); ImGui::SameLine(64); ImGui::Text("Toggle full-screen.");
  485. ImGui::NextLine();
  486. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "-"); ImGui::SameLine(64); ImGui::Text("Zoom out.");
  487. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "="); ImGui::SameLine(64); ImGui::Text("Zoom in.");
  488. ImGui::NextLine();
  489. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), ","); ImGui::SameLine(64); ImGui::Text("MIP level up.");
  490. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "."); ImGui::SameLine(64); ImGui::Text("MIP level down.");
  491. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "/"); ImGui::SameLine(64); ImGui::Text("Toggle linear/point texture sampling.");
  492. ImGui::NextLine();
  493. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "up"); ImGui::SameLine(64); ImGui::Text("Previous texture.");
  494. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "down"); ImGui::SameLine(64); ImGui::Text("Next texture.");
  495. ImGui::NextLine();
  496. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  497. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  498. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  499. || !view.m_help)
  500. {
  501. view.m_help = false;
  502. ImGui::CloseCurrentPopup();
  503. }
  504. ImGui::EndPopup();
  505. }
  506. help = view.m_help;
  507. // bool b;
  508. // ImGui::ShowTestWindow(&b);
  509. imguiEndFrame();
  510. if (!bgfx::isValid(texture)
  511. || view.m_fileIndex != fileIndex)
  512. {
  513. if (bgfx::isValid(texture) )
  514. {
  515. bgfx::destroyTexture(texture);
  516. }
  517. fileIndex = view.m_fileIndex;
  518. filePath = view.m_fileList[view.m_fileIndex].c_str();
  519. texture = loadTexture(filePath
  520. , 0
  521. | BGFX_TEXTURE_U_CLAMP
  522. | BGFX_TEXTURE_V_CLAMP
  523. | BGFX_TEXTURE_W_CLAMP
  524. , 0
  525. , &view.m_info
  526. );
  527. std::string title;
  528. bx::stringPrintf(title, "%s (%d x %d%s, %s)"
  529. , filePath
  530. , view.m_info.width
  531. , view.m_info.height
  532. , view.m_info.cubeMap ? " CubeMap" : ""
  533. , bgfx::getName(view.m_info.format)
  534. );
  535. entry::WindowHandle handle = { 0 };
  536. entry::setWindowTitle(handle, title.c_str() );
  537. }
  538. int64_t now = bx::getHPCounter();
  539. static int64_t last = now;
  540. const int64_t frameTime = now - last;
  541. last = now;
  542. const double freq = double(bx::getHPFrequency() );
  543. time += (float)(frameTime*speed/freq);
  544. float ortho[16];
  545. bx::mtxOrtho(ortho, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  546. bgfx::setViewTransform(0, NULL, ortho);
  547. bgfx::setViewRect(0, 0, 0, width, height);
  548. bgfx::touch(0);
  549. bgfx::dbgTextClear();
  550. scale.set(
  551. bx::fmin( float(width) / float(view.m_info.width)
  552. , float(height) / float(view.m_info.height)
  553. )
  554. , 0.1f
  555. );
  556. zoom.set(view.m_zoom, 0.25);
  557. float ss = scale.getValue() * zoom.getValue();
  558. screenQuad( int(width - view.m_info.width * ss)/2
  559. , int(height - view.m_info.height * ss)/2
  560. , int(view.m_info.width * ss)
  561. , int(view.m_info.height * ss)
  562. );
  563. float mtx[16];
  564. bx::mtxRotateXY(mtx, 0.0f, time);
  565. bgfx::setUniform(u_mtx, mtx);
  566. mip.set( float(view.m_mip), 0.5f);
  567. float params[4] = { mip.getValue(), 0.0f, 0.0f, 0.0f };
  568. bgfx::setUniform(u_params, params);
  569. bgfx::setTexture(0
  570. , s_texColor
  571. , texture
  572. , view.m_filter
  573. ? BGFX_TEXTURE_NONE
  574. : 0
  575. | BGFX_TEXTURE_MIN_POINT
  576. | BGFX_TEXTURE_MIP_POINT
  577. | BGFX_TEXTURE_MAG_POINT
  578. );
  579. bgfx::setState(0
  580. | BGFX_STATE_RGB_WRITE
  581. | BGFX_STATE_ALPHA_WRITE
  582. );
  583. bgfx::submit(0, view.m_info.cubeMap ? textureCubeProgram : textureProgram);
  584. bgfx::frame();
  585. }
  586. }
  587. if (bgfx::isValid(texture) )
  588. {
  589. bgfx::destroyTexture(texture);
  590. }
  591. bgfx::destroyUniform(s_texColor);
  592. bgfx::destroyUniform(u_mtx);
  593. bgfx::destroyUniform(u_params);
  594. bgfx::destroyProgram(textureProgram);
  595. bgfx::destroyProgram(textureCubeProgram);
  596. imguiDestroy();
  597. bgfx::shutdown();
  598. return exitcode;
  599. }