texturev.cpp 14 KB

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