svt.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright 2018 Ales Mlakar. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. /*
  6. * Reference(s):
  7. * - Sparse Virtual Textures by Sean Barrett
  8. * http://web.archive.org/web/20190103162611/http://silverspaceship.com/src/svt/
  9. * - Based on Virtual Texture Demo by Brad Blanchard
  10. * http://web.archive.org/web/20190103162638/http://linedef.com/virtual-texture-demo.html
  11. * - Mars texture
  12. * http://web.archive.org/web/20190103162730/http://www.celestiamotherlode.net/catalog/mars.php
  13. */
  14. #include "common.h"
  15. #include "bgfx_utils.h"
  16. #include "imgui/imgui.h"
  17. #include "camera.h"
  18. #include "bounds.h"
  19. #include "vt.h"
  20. namespace
  21. {
  22. struct PosTexcoordVertex
  23. {
  24. float m_x;
  25. float m_y;
  26. float m_z;
  27. float m_u;
  28. float m_v;
  29. static void init()
  30. {
  31. ms_layout
  32. .begin()
  33. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  34. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  35. .end();
  36. };
  37. static bgfx::VertexLayout ms_layout;
  38. };
  39. bgfx::VertexLayout PosTexcoordVertex::ms_layout;
  40. static const float s_planeScale = 50.0f;
  41. static PosTexcoordVertex s_vplaneVertices[] =
  42. {
  43. { -s_planeScale, 0.0f, s_planeScale, 1.0f, 1.0f },
  44. { s_planeScale, 0.0f, s_planeScale, 1.0f, 0.0f },
  45. { -s_planeScale, 0.0f, -s_planeScale, 0.0f, 1.0f },
  46. { s_planeScale, 0.0f, -s_planeScale, 0.0f, 0.0f },
  47. };
  48. static const uint16_t s_planeIndices[] =
  49. {
  50. 0, 1, 2,
  51. 1, 3, 2,
  52. };
  53. class ExampleSVT : public entry::AppI
  54. {
  55. public:
  56. ExampleSVT(const char* _name, const char* _description)
  57. : entry::AppI(_name, _description)
  58. {
  59. }
  60. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  61. {
  62. Args args(_argc, _argv);
  63. m_width = _width;
  64. m_height = _height;
  65. m_debug = BGFX_DEBUG_TEXT;
  66. m_reset = BGFX_RESET_VSYNC;
  67. bgfx::Init init;
  68. init.type = args.m_type;
  69. init.vendorId = args.m_pciId;
  70. init.resolution.width = m_width;
  71. init.resolution.height = m_height;
  72. init.resolution.reset = m_reset;
  73. bgfx::init(init);
  74. // Enable m_debug text.
  75. bgfx::setDebug(m_debug);
  76. // Set views clear state (first pass to 0, second pass to some background color)
  77. for (uint16_t i = 0; i < 2; ++i)
  78. {
  79. bgfx::setViewClear(i
  80. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  81. , i == 0 ? 0 : 0x101050ff
  82. , 1.0f
  83. , 0
  84. );
  85. }
  86. // Create vertex stream declaration.
  87. PosTexcoordVertex::init();
  88. // Create static vertex buffer.
  89. m_vbh = bgfx::createVertexBuffer(
  90. bgfx::makeRef(s_vplaneVertices, sizeof(s_vplaneVertices))
  91. , PosTexcoordVertex::ms_layout
  92. );
  93. m_ibh = bgfx::createIndexBuffer(
  94. bgfx::makeRef(s_planeIndices, sizeof(s_planeIndices))
  95. );
  96. // Create program from shaders.
  97. m_vt_unlit = loadProgram("vs_vt_generic", "fs_vt_unlit");
  98. m_vt_mip = loadProgram("vs_vt_generic", "fs_vt_mip");
  99. // Imgui.
  100. imguiCreate();
  101. m_timeOffset = bx::getHPCounter();
  102. // Get renderer capabilities info.
  103. m_caps = bgfx::getCaps();
  104. m_scrollArea = 0;
  105. // Create and setup camera
  106. cameraCreate();
  107. cameraSetPosition({ 0.0f, 5.0f, 0.0f });
  108. cameraSetVerticalAngle(0.0f);
  109. // Set VirtualTexture system allocator
  110. vt::VirtualTexture::setAllocator(&m_vtAllocator);
  111. // Create Virtual texture info
  112. m_vti = new vt::VirtualTextureInfo();
  113. m_vti->m_virtualTextureSize = 8192; // The actual size will be read from the tile data file
  114. m_vti->m_tileSize = 128;
  115. m_vti->m_borderSize = 1;
  116. // Generate tile data file (if not yet created)
  117. {
  118. vt::TileGenerator tileGenerator(m_vti);
  119. tileGenerator.generate("textures/8k_mars.jpg");
  120. }
  121. // Load tile data file
  122. auto tileDataFile = new vt::TileDataFile("temp/8k_mars.vt", m_vti);
  123. tileDataFile->readInfo();
  124. // Create virtual texture and feedback buffer
  125. m_vt = new vt::VirtualTexture(tileDataFile, m_vti, 2048, 1);
  126. m_feedbackBuffer = new vt::FeedbackBuffer(m_vti, 64, 64);
  127. }
  128. virtual int shutdown() override
  129. {
  130. // Cleanup.
  131. bgfx::frame();
  132. cameraDestroy();
  133. imguiDestroy();
  134. bgfx::destroy(m_ibh);
  135. bgfx::destroy(m_vbh);
  136. bgfx::destroy(m_vt_unlit);
  137. bgfx::destroy(m_vt_mip);
  138. delete m_vti;
  139. delete m_vt;
  140. delete m_feedbackBuffer;
  141. // Shutdown bgfx.
  142. bgfx::shutdown();
  143. return 0;
  144. }
  145. bool update() override
  146. {
  147. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState))
  148. {
  149. imguiBeginFrame(
  150. m_mouseState.m_mx
  151. , m_mouseState.m_my
  152. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  153. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  154. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  155. , m_mouseState.m_mz
  156. , uint16_t(m_width)
  157. , uint16_t(m_height)
  158. );
  159. showExampleDialog(this);
  160. int64_t now = bx::getHPCounter();
  161. static int64_t last = now;
  162. const int64_t frameTime = now - last;
  163. last = now;
  164. const double freq = double(bx::getHPFrequency());
  165. const float deltaTime = float(frameTime / freq);
  166. float time = (float)((now - m_timeOffset) / freq);
  167. if ((BGFX_CAPS_TEXTURE_BLIT | BGFX_CAPS_TEXTURE_READ_BACK) != (bgfx::getCaps()->supported & (BGFX_CAPS_TEXTURE_BLIT | BGFX_CAPS_TEXTURE_READ_BACK)))
  168. {
  169. // When texture read-back or blit is not supported by GPU blink!
  170. bool blink = uint32_t(time*3.0f) & 1;
  171. bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Texture read-back and/or blit not supported by GPU. ");
  172. // Set view 0 default viewport.
  173. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));
  174. // This dummy draw call is here to make sure that view 0 is cleared
  175. // if no other draw calls are submitted to view 0.
  176. bgfx::touch(0);
  177. }
  178. else
  179. {
  180. ImGui::SetNextWindowPos(
  181. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  182. , ImGuiCond_FirstUseEver
  183. );
  184. ImGui::SetNextWindowSize(
  185. ImVec2(m_width / 5.0f, m_height - 10.0f)
  186. , ImGuiCond_FirstUseEver
  187. );
  188. ImGui::Begin("Settings"
  189. , NULL
  190. , 0
  191. );
  192. //ImGui::SliderFloat("intensity", &m_intensity, 0.0f, 3.0f);
  193. auto showBorders = m_vt->isShowBoardersEnabled();
  194. if (ImGui::Checkbox("Show borders", &showBorders))
  195. {
  196. m_vt->enableShowBoarders(showBorders);
  197. }
  198. auto colorMipLevels = m_vt->isColorMipLevelsEnabled();
  199. if (ImGui::Checkbox("Color mip levels", &colorMipLevels))
  200. {
  201. m_vt->enableColorMipLevels(colorMipLevels);
  202. }
  203. auto uploadsperframe = m_vt->getUploadsPerFrame();
  204. if (ImGui::InputInt("Updates per frame", &uploadsperframe, 1, 2))
  205. {
  206. uploadsperframe = bx::clamp(uploadsperframe, 1, 100);
  207. m_vt->setUploadsPerFrame(uploadsperframe);
  208. }
  209. ImGui::ImageButton(m_vt->getAtlastTexture(), ImVec2(m_width / 5.0f - 16.0f, m_width / 5.0f - 16.0f));
  210. ImGui::ImageButton(bgfx::getTexture(m_feedbackBuffer->getFrameBuffer()), ImVec2(m_width / 5.0f - 16.0f, m_width / 5.0f - 16.0f));
  211. ImGui::End();
  212. // Update camera.
  213. cameraUpdate(deltaTime, m_mouseState);
  214. float view[16];
  215. cameraGetViewMtx(view);
  216. float proj[16];
  217. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 1000.0f, m_caps->homogeneousDepth);
  218. // Setup views
  219. for (uint16_t i = 0; i < 2; ++i)
  220. {
  221. uint16_t viewWidth = 0;
  222. uint16_t viewHeight = 0;
  223. // Setup pass, first pass is into mip-map feedback buffer, second pass is on screen
  224. if (i == 0)
  225. {
  226. bgfx::setViewFrameBuffer(i, m_feedbackBuffer->getFrameBuffer());
  227. viewWidth = uint16_t(m_feedbackBuffer->getWidth());
  228. viewHeight = uint16_t(m_feedbackBuffer->getHeight());
  229. }
  230. else
  231. {
  232. bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
  233. bgfx::setViewFrameBuffer(i, invalid);
  234. viewWidth = uint16_t(m_width);
  235. viewHeight = uint16_t(m_height);
  236. }
  237. bgfx::setViewRect(i, 0, 0, viewWidth, viewHeight);
  238. bgfx::setViewTransform(i, view, proj);
  239. float mtx[16];
  240. bx::mtxIdentity(mtx);
  241. // Set identity transform for draw call.
  242. bgfx::setTransform(mtx);
  243. // Set vertex and index buffer.
  244. bgfx::setVertexBuffer(0, m_vbh);
  245. bgfx::setIndexBuffer(m_ibh);
  246. // Set render states.
  247. bgfx::setState(0
  248. | BGFX_STATE_WRITE_RGB
  249. | BGFX_STATE_WRITE_A
  250. | BGFX_STATE_WRITE_Z
  251. | BGFX_STATE_DEPTH_TEST_LESS
  252. );
  253. // Set virtual texture uniforms
  254. m_vt->setUniforms();
  255. // Submit primitive for rendering to first pass (to feedback buffer, where mip levels and tile x/y will be rendered
  256. if (i == 0)
  257. {
  258. bgfx::submit(i, m_vt_mip);
  259. // Download previous frame feedback info
  260. m_feedbackBuffer->download();
  261. // Update and upload new requests
  262. m_vt->update(m_feedbackBuffer->getRequests(), 4);
  263. // Clear feedback
  264. m_feedbackBuffer->clear();
  265. // Copy new frame feedback buffer
  266. m_feedbackBuffer->copy(3);
  267. }
  268. else
  269. {
  270. // Submit primitive for rendering to second pass (to back buffer, where virtual texture page table and atlas will be used)
  271. bgfx::submit(i, m_vt_unlit);
  272. }
  273. }
  274. }
  275. imguiEndFrame();
  276. // Advance to next frame. Rendering thread will be kicked to
  277. // process submitted rendering primitives.
  278. bgfx::frame();
  279. return true;
  280. }
  281. return false;
  282. }
  283. bgfx::VertexBufferHandle m_vbh;
  284. bgfx::IndexBufferHandle m_ibh;
  285. bgfx::ProgramHandle m_vt_unlit;
  286. bgfx::ProgramHandle m_vt_mip;
  287. uint32_t m_width;
  288. uint32_t m_height;
  289. uint32_t m_debug;
  290. uint32_t m_reset;
  291. int32_t m_scrollArea;
  292. entry::MouseState m_mouseState;
  293. const bgfx::Caps* m_caps;
  294. int64_t m_timeOffset;
  295. bx::DefaultAllocator m_vtAllocator;
  296. vt::VirtualTextureInfo* m_vti;
  297. vt::VirtualTexture* m_vt;
  298. vt::FeedbackBuffer* m_feedbackBuffer;
  299. };
  300. } // namespace
  301. ENTRY_IMPLEMENT_MAIN(ExampleSVT, "40-svt", "Sparse Virtual Textures.");