tess.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Copyright 2019 Daniel Gavin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. /*
  6. * Reference(s):
  7. *
  8. * - Adaptive GPU Tessellation with Compute Shaders by Jad Khoury, Jonathan Dupuy, and Christophe Riccio
  9. * http://onrendering.com/data/papers/isubd/isubd.pdf
  10. * https://github.com/jdupuy/opengl-framework/tree/master/demo-isubd-terrain#implicit-subdivision-on-the-gpu
  11. */
  12. #include <bx/allocator.h>
  13. #include <bx/debug.h>
  14. #include <bx/file.h>
  15. #include <bx/math.h>
  16. #include "bgfx_utils.h"
  17. #include "camera.h"
  18. #include "common.h"
  19. #include "imgui/imgui.h"
  20. namespace
  21. {
  22. static const char* s_shaderOptions[] =
  23. {
  24. "Normal",
  25. "Diffuse"
  26. };
  27. static const float s_verticesL0[] =
  28. {
  29. 0.0f, 0.0f,
  30. 1.0f, 0.0f,
  31. 0.0f, 1.0f,
  32. };
  33. static const uint32_t s_indexesL0[] = { 0u, 1u, 2u };
  34. static const float s_verticesL1[] =
  35. {
  36. 0.0f, 1.0f,
  37. 0.5f, 0.5f,
  38. 0.0f, 0.5f,
  39. 0.0f, 0.0f,
  40. 0.5f, 0.0f,
  41. 1.0f, 0.0f,
  42. };
  43. static const uint32_t s_indexesL1[] =
  44. {
  45. 1u, 0u, 2u,
  46. 1u, 2u, 3u,
  47. 1u, 3u, 4u,
  48. 1u, 4u, 5u,
  49. };
  50. static const float s_verticesL2[] =
  51. {
  52. 0.25f, 0.75f,
  53. 0.0f, 1.0f,
  54. 0.0f, 0.75f,
  55. 0.0f, 0.5f,
  56. 0.25f, 0.5f,
  57. 0.5f, 0.5f,
  58. 0.25f, 0.25f,
  59. 0.0f, 0.25f,
  60. 0.0f, 0.0f,
  61. 0.25f, 0.0f,
  62. 0.5f, 0.0f,
  63. 0.5f, 0.25f,
  64. 0.75f, 0.25f,
  65. 0.75f, 0.0f,
  66. 1.0f, 0.0f,
  67. };
  68. static const uint32_t s_indexesL2[] =
  69. {
  70. 0u, 1u, 2u,
  71. 0u, 2u, 3u,
  72. 0u, 3u, 4u,
  73. 0u, 4u, 5u,
  74. 6u, 5u, 4u,
  75. 6u, 4u, 3u,
  76. 6u, 3u, 7u,
  77. 6u, 7u, 8u,
  78. 6u, 8u, 9u,
  79. 6u, 9u, 10u,
  80. 6u, 10u, 11u,
  81. 6u, 11u, 5u,
  82. 12u, 5u, 11u,
  83. 12u, 11u, 10u,
  84. 12u, 10u, 13u,
  85. 12u, 13u, 14u,
  86. };
  87. static const float s_verticesL3[] =
  88. {
  89. 0.25f*0.5f, 0.75f*0.5f + 0.5f,
  90. 0.0f*0.5f, 1.0f*0.5f + 0.5f,
  91. 0.0f*0.5f, 0.75f*0.5f + 0.5f,
  92. 0.0f*0.5f , 0.5f*0.5f + 0.5f,
  93. 0.25f*0.5f, 0.5f*0.5f + 0.5f,
  94. 0.5f*0.5f, 0.5f*0.5f + 0.5f,
  95. 0.25f*0.5f, 0.25f*0.5f + 0.5f,
  96. 0.0f*0.5f, 0.25f*0.5f + 0.5f,
  97. 0.0f*0.5f, 0.0f*0.5f + 0.5f,
  98. 0.25f*0.5f, 0.0f*0.5f + 0.5f,
  99. 0.5f*0.5f, 0.0f*0.5f + 0.5f,
  100. 0.5f*0.5f, 0.25f*0.5f + 0.5f,
  101. 0.75f*0.5f, 0.25f*0.5f + 0.5f,
  102. 0.75f*0.5f, 0.0f*0.5f + 0.5f,
  103. 1.0f*0.5f, 0.0f*0.5f + 0.5f, //14
  104. 0.375f, 0.375f,
  105. 0.25f, 0.375f,
  106. 0.25f, 0.25f,
  107. 0.375f, 0.25f,
  108. 0.5f, 0.25f,
  109. 0.5f, 0.375f, //20
  110. 0.125f, 0.375f,
  111. 0.0f, 0.375f,
  112. 0.0f, 0.25f,
  113. 0.125f, 0.25f, //24
  114. 0.125f, 0.125f,
  115. 0.0f, 0.125f,
  116. 0.0f, 0.0f,
  117. 0.125f, 0.0f,
  118. 0.25f, 0.0f,
  119. 0.25f, 0.125f, //30
  120. 0.375f, 0.125f,
  121. 0.375f, 0.0f,
  122. 0.5f, 0.0f,
  123. 0.5f, 0.125f, //34
  124. 0.625f, 0.375f,
  125. 0.625f, 0.25f,
  126. 0.75f, 0.25f, //37
  127. 0.625f, 0.125f,
  128. 0.625f, 0.0f,
  129. 0.75f, 0.0f,
  130. 0.75f, 0.125f, //41
  131. 0.875f, 0.125f,
  132. 0.875f, 0.0f,
  133. 1.0f, 0.0f, //44
  134. };
  135. static const uint32_t s_indexesL3[] =
  136. {
  137. 0u, 1u, 2u,
  138. 0u, 2u, 3u,
  139. 0u, 3u, 4u,
  140. 0u, 4u, 5u,
  141. 6u, 5u, 4u,
  142. 6u, 4u, 3u,
  143. 6u, 3u, 7u,
  144. 6u, 7u, 8u,
  145. 6u, 8u, 9u,
  146. 6u, 9u, 10u,
  147. 6u, 10u, 11u,
  148. 6u, 11u, 5u,
  149. 12u, 5u, 11u,
  150. 12u, 11u, 10u,
  151. 12u, 10u, 13u,
  152. 12u, 13u, 14u, //End fo first big triangle
  153. 15u, 14u, 13u,
  154. 15u, 13u, 10u,
  155. 15u, 10u, 16u,
  156. 15u, 16u, 17u,
  157. 15u, 17u, 18u,
  158. 15u, 18u, 19u,
  159. 15u, 19u, 20u,
  160. 15u, 20u, 14u,
  161. 21u, 10u, 9u,
  162. 21u, 9u, 8u,
  163. 21u, 8u, 22u,
  164. 21u, 22u, 23u,
  165. 21u, 23u, 24u,
  166. 21u, 24u, 17u,
  167. 21u, 17u, 16u,
  168. 21u, 16u, 10u,
  169. 25u, 17u, 24u,
  170. 25u, 24u, 23u,
  171. 25u, 23u, 26u,
  172. 25u, 26u, 27u,
  173. 25u, 27u, 28u,
  174. 25u, 28u, 29u,
  175. 25u, 29u, 30u,
  176. 25u, 30u, 17u,
  177. 31u, 19u, 18u,
  178. 31u, 18u, 17u,
  179. 31u, 17u, 30u,
  180. 31u, 30u, 29u,
  181. 31u, 29u, 32u,
  182. 31u, 32u, 33u,
  183. 31u, 33u, 34u,
  184. 31u, 34u, 19u,
  185. 35u, 14u, 20u,
  186. 35u, 20u, 19u,
  187. 35u, 19u, 36u,
  188. 35u, 36u, 37u,
  189. 38u, 37u, 36u,
  190. 38u, 36u, 19u,
  191. 38u, 19u, 34u,
  192. 38u, 34u, 33u,
  193. 38u, 33u, 39u,
  194. 38u, 39u, 40u,
  195. 38u, 40u, 41u,
  196. 38u, 41u, 37u,
  197. 42u, 37u, 41u,
  198. 42u, 41u, 40u,
  199. 42u, 40u, 43u,
  200. 42u, 43u, 44u,
  201. };
  202. enum
  203. {
  204. PROGRAM_TERRAIN_NORMAL,
  205. PROGRAM_TERRAIN,
  206. SHADING_COUNT
  207. };
  208. enum
  209. {
  210. BUFFER_SUBD
  211. };
  212. enum
  213. {
  214. PROGRAM_SUBD_CS_LOD,
  215. PROGRAM_UPDATE_INDIRECT,
  216. PROGRAM_INIT_INDIRECT,
  217. PROGRAM_UPDATE_DRAW,
  218. PROGRAM_COUNT
  219. };
  220. enum
  221. {
  222. TERRAIN_DMAP_SAMPLER,
  223. TERRAIN_SMAP_SAMPLER,
  224. SAMPLER_COUNT
  225. };
  226. enum
  227. {
  228. TEXTURE_DMAP,
  229. TEXTURE_SMAP,
  230. TEXTURE_COUNT
  231. };
  232. constexpr int32_t kNumVec4 = 2;
  233. struct Uniforms
  234. {
  235. void init()
  236. {
  237. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, kNumVec4);
  238. cull = 1;
  239. freeze = 0;
  240. gpuSubd = 3;
  241. }
  242. void submit()
  243. {
  244. bgfx::setUniform(u_params, params, kNumVec4);
  245. }
  246. void destroy()
  247. {
  248. bgfx::destroy(u_params);
  249. }
  250. union
  251. {
  252. struct
  253. {
  254. float dmapFactor;
  255. float lodFactor;
  256. float cull;
  257. float freeze;
  258. float gpuSubd;
  259. float padding0;
  260. float padding1;
  261. float padding2;
  262. };
  263. float params[kNumVec4 * 4];
  264. };
  265. bgfx::UniformHandle u_params;
  266. };
  267. class ExampleTessellation : public entry::AppI
  268. {
  269. public:
  270. ExampleTessellation(const char* _name, const char* _description, const char* _url)
  271. : entry::AppI(_name, _description, _url)
  272. {
  273. }
  274. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  275. {
  276. Args args(_argc, _argv);
  277. m_width = _width;
  278. m_height = _height;
  279. m_debug = BGFX_DEBUG_NONE;
  280. m_reset = BGFX_RESET_NONE;
  281. bgfx::Init init;
  282. init.type = args.m_type;
  283. init.vendorId = args.m_pciId;
  284. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  285. init.platformData.ndt = entry::getNativeDisplayHandle();
  286. init.resolution.width = m_width;
  287. init.resolution.height = m_height;
  288. init.resolution.reset = m_reset;
  289. bgfx::init(init);
  290. m_dmap = { "textures/dmap.png", 0.45f };
  291. m_computeThreadCount = 5;
  292. m_shading = PROGRAM_TERRAIN;
  293. m_primitivePixelLengthTarget = 7.0f;
  294. m_fovy = 60.0f;
  295. m_pingPong = 0;
  296. m_restart = true;
  297. // Enable m_debug text.
  298. bgfx::setDebug(m_debug);
  299. // Set view 0 clear state.
  300. bgfx::setViewClear(0
  301. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  302. , 0x303030ff
  303. , 1.0f
  304. , 0
  305. );
  306. bgfx::setViewClear(1
  307. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  308. , 0x303030ff
  309. , 1.0f
  310. , 0
  311. );
  312. // Imgui.
  313. imguiCreate();
  314. m_timeOffset = bx::getHPCounter();
  315. m_oldWidth = 0;
  316. m_oldHeight = 0;
  317. m_oldReset = m_reset;
  318. cameraCreate();
  319. cameraSetPosition({ 0.0f, 0.5f, 0.0f });
  320. cameraSetVerticalAngle(0);
  321. m_wireframe = false;
  322. m_freeze = false;
  323. m_cull = true;
  324. loadPrograms();
  325. loadBuffers();
  326. loadTextures();
  327. createAtomicCounters();
  328. m_dispatchIndirect = bgfx::createIndirectBuffer(2);
  329. }
  330. virtual int shutdown() override
  331. {
  332. // Cleanup.
  333. cameraDestroy();
  334. imguiDestroy();
  335. m_uniforms.destroy();
  336. bgfx::destroy(m_bufferCounter);
  337. bgfx::destroy(m_bufferCulledSubd);
  338. bgfx::destroy(m_bufferSubd[0]);
  339. bgfx::destroy(m_bufferSubd[1]);
  340. bgfx::destroy(m_dispatchIndirect);
  341. bgfx::destroy(m_geometryIndices);
  342. bgfx::destroy(m_geometryVertices);
  343. bgfx::destroy(m_instancedGeometryIndices);
  344. bgfx::destroy(m_instancedGeometryVertices);
  345. for (uint32_t i = 0; i < PROGRAM_COUNT; ++i)
  346. {
  347. bgfx::destroy(m_programsCompute[i]);
  348. }
  349. for (uint32_t i = 0; i < SHADING_COUNT; ++i)
  350. {
  351. bgfx::destroy(m_programsDraw[i]);
  352. }
  353. for (uint32_t i = 0; i < SAMPLER_COUNT; ++i)
  354. {
  355. bgfx::destroy(m_samplers[i]);
  356. }
  357. for (uint32_t i = 0; i < TEXTURE_COUNT; ++i)
  358. {
  359. bgfx::destroy(m_textures[i]);
  360. }
  361. // Shutdown bgfx.
  362. bgfx::shutdown();
  363. return 0;
  364. }
  365. bool update() override
  366. {
  367. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  368. {
  369. int64_t now = bx::getHPCounter();
  370. static int64_t last = now;
  371. const int64_t frameTime = now - last;
  372. last = now;
  373. const double freq = double(bx::getHPFrequency() );
  374. const float deltaTime = float(frameTime / freq);
  375. imguiBeginFrame(
  376. m_mouseState.m_mx
  377. , m_mouseState.m_my
  378. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  379. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  380. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  381. , m_mouseState.m_mz
  382. , uint16_t(m_width)
  383. , uint16_t(m_height)
  384. );
  385. showExampleDialog(this);
  386. ImGui::SetNextWindowPos(
  387. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  388. , ImGuiCond_FirstUseEver
  389. );
  390. ImGui::SetNextWindowSize(
  391. ImVec2(m_width / 5.0f, m_height / 3.0f)
  392. , ImGuiCond_FirstUseEver
  393. );
  394. ImGui::Begin("Settings", NULL, 0);
  395. if (ImGui::Checkbox("Debug wireframe", &m_wireframe) )
  396. {
  397. bgfx::setDebug(m_wireframe
  398. ? BGFX_DEBUG_WIREFRAME
  399. : BGFX_DEBUG_NONE
  400. );
  401. }
  402. ImGui::SameLine();
  403. if (ImGui::Checkbox("Cull", &m_cull) )
  404. {
  405. m_uniforms.cull = m_cull ? 1.0f : 0.0f;
  406. }
  407. ImGui::SameLine();
  408. if (ImGui::Checkbox("Freeze subdividing", &m_freeze) )
  409. {
  410. m_uniforms.freeze = m_freeze ? 1.0f : 0.0f;
  411. }
  412. ImGui::SliderFloat("Pixels per edge", &m_primitivePixelLengthTarget, 1, 20);
  413. int gpuSlider = (int)m_uniforms.gpuSubd;
  414. if (ImGui::SliderInt("Triangle Patch level", &gpuSlider, 0, 3) )
  415. {
  416. m_restart = true;
  417. m_uniforms.gpuSubd = float(gpuSlider);
  418. }
  419. ImGui::Combo("Shading", &m_shading, s_shaderOptions, 2);
  420. ImGui::Text("Some variables require rebuilding the subdivide buffers and causes a stutter.");
  421. ImGui::End();
  422. // Update camera.
  423. cameraUpdate(deltaTime*0.01f, m_mouseState, ImGui::MouseOverArea() );
  424. bgfx::touch(0);
  425. bgfx::touch(1);
  426. configureUniforms();
  427. cameraGetViewMtx(m_viewMtx);
  428. float model[16];
  429. bx::mtxRotateX(model, bx::toRad(90) );
  430. bx::mtxProj(m_projMtx, m_fovy, float(m_width) / float(m_height), 0.0001f, 2000.0f, bgfx::getCaps()->homogeneousDepth);
  431. // Set view 0
  432. bgfx::setViewTransform(0, m_viewMtx, m_projMtx);
  433. // Set view 1
  434. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  435. bgfx::setViewTransform(1, m_viewMtx, m_projMtx);
  436. m_uniforms.submit();
  437. // update the subd buffers
  438. if (m_restart)
  439. {
  440. m_pingPong = 1;
  441. bgfx::destroy(m_instancedGeometryVertices);
  442. bgfx::destroy(m_instancedGeometryIndices);
  443. bgfx::destroy(m_bufferSubd[BUFFER_SUBD]);
  444. bgfx::destroy(m_bufferSubd[BUFFER_SUBD + 1]);
  445. bgfx::destroy(m_bufferCulledSubd);
  446. loadInstancedGeometryBuffers();
  447. loadSubdivisionBuffers();
  448. //init indirect
  449. bgfx::setBuffer(1, m_bufferSubd[m_pingPong], bgfx::Access::ReadWrite);
  450. bgfx::setBuffer(2, m_bufferCulledSubd, bgfx::Access::ReadWrite);
  451. bgfx::setBuffer(3, m_dispatchIndirect, bgfx::Access::ReadWrite);
  452. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  453. bgfx::setBuffer(8, m_bufferSubd[1 - m_pingPong], bgfx::Access::ReadWrite);
  454. bgfx::dispatch(0, m_programsCompute[PROGRAM_INIT_INDIRECT], 1, 1, 1);
  455. m_restart = false;
  456. }
  457. else
  458. {
  459. // update batch
  460. bgfx::setBuffer(3, m_dispatchIndirect, bgfx::Access::ReadWrite);
  461. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  462. bgfx::dispatch(0, m_programsCompute[PROGRAM_UPDATE_INDIRECT], 1, 1, 1);
  463. }
  464. bgfx::setBuffer(1, m_bufferSubd[m_pingPong], bgfx::Access::ReadWrite);
  465. bgfx::setBuffer(2, m_bufferCulledSubd, bgfx::Access::ReadWrite);
  466. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  467. bgfx::setBuffer(6, m_geometryVertices, bgfx::Access::Read);
  468. bgfx::setBuffer(7, m_geometryIndices, bgfx::Access::Read);
  469. bgfx::setBuffer(8, m_bufferSubd[1 - m_pingPong], bgfx::Access::Read);
  470. bgfx::setTransform(model);
  471. bgfx::setTexture(0, m_samplers[TERRAIN_DMAP_SAMPLER], m_textures[TEXTURE_DMAP], BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  472. m_uniforms.submit();
  473. // update the subd buffer
  474. bgfx::dispatch(0, m_programsCompute[PROGRAM_SUBD_CS_LOD], m_dispatchIndirect, 1);
  475. // update draw
  476. bgfx::setBuffer(3, m_dispatchIndirect, bgfx::Access::ReadWrite);
  477. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  478. m_uniforms.submit();
  479. bgfx::dispatch(1, m_programsCompute[PROGRAM_UPDATE_DRAW], 1, 1, 1);
  480. // render the terrain
  481. bgfx::setTexture(0, m_samplers[TERRAIN_DMAP_SAMPLER], m_textures[TEXTURE_DMAP], BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  482. bgfx::setTexture(1, m_samplers[TERRAIN_SMAP_SAMPLER], m_textures[TEXTURE_SMAP], BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC);
  483. bgfx::setTransform(model);
  484. bgfx::setVertexBuffer(0, m_instancedGeometryVertices);
  485. bgfx::setIndexBuffer(m_instancedGeometryIndices);
  486. bgfx::setBuffer(2, m_bufferCulledSubd, bgfx::Access::Read);
  487. bgfx::setBuffer(3, m_geometryVertices, bgfx::Access::Read);
  488. bgfx::setBuffer(4, m_geometryIndices, bgfx::Access::Read);
  489. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS);
  490. m_uniforms.submit();
  491. bgfx::submit(1, m_programsDraw[m_shading], m_dispatchIndirect);
  492. m_pingPong = 1 - m_pingPong;
  493. imguiEndFrame();
  494. // Advance to next frame. Rendering thread will be kicked to
  495. // process submitted rendering primitives.
  496. bgfx::frame(false);
  497. return true;
  498. }
  499. return false;
  500. }
  501. void createAtomicCounters()
  502. {
  503. m_bufferCounter = bgfx::createDynamicIndexBuffer(3, BGFX_BUFFER_INDEX32 | BGFX_BUFFER_COMPUTE_READ_WRITE);
  504. }
  505. void configureUniforms()
  506. {
  507. float lodFactor = 2.0f * bx::tan(bx::toRad(m_fovy) / 2.0f)
  508. / m_width * (1 << (int)m_uniforms.gpuSubd)
  509. * m_primitivePixelLengthTarget
  510. ;
  511. m_uniforms.lodFactor = lodFactor;
  512. m_uniforms.dmapFactor = m_dmap.scale;
  513. }
  514. /**
  515. * Load the Terrain Program
  516. *
  517. * This program renders an adaptive terrain using the implicit subdivision
  518. * technique described in GPU Zen 2.
  519. **/
  520. void loadPrograms()
  521. {
  522. m_samplers[TERRAIN_DMAP_SAMPLER] = bgfx::createUniform("u_DmapSampler", bgfx::UniformType::Sampler);
  523. m_samplers[TERRAIN_SMAP_SAMPLER] = bgfx::createUniform("u_SmapSampler", bgfx::UniformType::Sampler);
  524. m_uniforms.init();
  525. m_programsDraw[PROGRAM_TERRAIN] = loadProgram("vs_terrain_render", "fs_terrain_render");
  526. m_programsDraw[PROGRAM_TERRAIN_NORMAL] = loadProgram("vs_terrain_render", "fs_terrain_render_normal");
  527. m_programsCompute[PROGRAM_SUBD_CS_LOD] = bgfx::createProgram(loadShader("cs_terrain_lod"), true);
  528. m_programsCompute[PROGRAM_UPDATE_INDIRECT] = bgfx::createProgram(loadShader("cs_terrain_update_indirect"), true);
  529. m_programsCompute[PROGRAM_UPDATE_DRAW] = bgfx::createProgram(loadShader("cs_terrain_update_draw"), true);
  530. m_programsCompute[PROGRAM_INIT_INDIRECT] = bgfx::createProgram(loadShader("cs_terrain_init"), true);
  531. }
  532. void loadSmapTexture()
  533. {
  534. int w = dmap->m_width;
  535. int h = dmap->m_height;
  536. const uint16_t *texels = (const uint16_t *)dmap->m_data;
  537. int mipcnt = dmap->m_numMips;
  538. const bgfx::Memory* mem = bgfx::alloc(w * h * 2 * sizeof(float) );
  539. float* smap = (float*)mem->data;
  540. for (int j = 0; j < h; ++j)
  541. {
  542. for (int i = 0; i < w; ++i)
  543. {
  544. int i1 = bx::max(0, i - 1);
  545. int i2 = bx::min(w - 1, i + 1);
  546. int j1 = bx::max(0, j - 1);
  547. int j2 = bx::min(h - 1, j + 1);
  548. uint16_t px_l = texels[i1 + w * j]; // in [0,2^16-1]
  549. uint16_t px_r = texels[i2 + w * j]; // in [0,2^16-1]
  550. uint16_t px_b = texels[i + w * j1]; // in [0,2^16-1]
  551. uint16_t px_t = texels[i + w * j2]; // in [0,2^16-1]
  552. float z_l = (float)px_l / 65535.0f; // in [0, 1]
  553. float z_r = (float)px_r / 65535.0f; // in [0, 1]
  554. float z_b = (float)px_b / 65535.0f; // in [0, 1]
  555. float z_t = (float)px_t / 65535.0f; // in [0, 1]
  556. float slope_x = (float)w * 0.5f * (z_r - z_l);
  557. float slope_y = (float)h * 0.5f * (z_t - z_b);
  558. smap[2 * (i + w * j)] = slope_x;
  559. smap[1 + 2 * (i + w * j)] = slope_y;
  560. }
  561. }
  562. m_textures[TEXTURE_SMAP] = bgfx::createTexture2D(
  563. (uint16_t)w
  564. , (uint16_t)h
  565. , mipcnt > 1
  566. , 1
  567. , bgfx::TextureFormat::RG32F
  568. , BGFX_TEXTURE_NONE
  569. , mem
  570. );
  571. }
  572. /**
  573. * Load the Displacement Texture
  574. *
  575. * This loads an R16 texture used as a displacement map
  576. */
  577. void loadDmapTexture()
  578. {
  579. dmap = imageLoad(m_dmap.pathToFile.getCPtr(), bgfx::TextureFormat::R16);
  580. m_textures[TEXTURE_DMAP] = bgfx::createTexture2D(
  581. (uint16_t)dmap->m_width
  582. , (uint16_t)dmap->m_height
  583. , false
  584. , 1
  585. , bgfx::TextureFormat::R16
  586. , BGFX_TEXTURE_NONE
  587. , bgfx::makeRef(dmap->m_data, dmap->m_size)
  588. );
  589. }
  590. void loadTextures()
  591. {
  592. loadDmapTexture();
  593. loadSmapTexture();
  594. }
  595. /**
  596. * Load the Geometry Buffer
  597. *
  598. * This procedure loads the scene geometry into an index and
  599. * vertex buffer. Here, we only load 2 triangles to define the
  600. * terrain.
  601. **/
  602. void loadGeometryBuffers()
  603. {
  604. const float vertices[] =
  605. {
  606. -1.0f, -1.0f, 0.0f, 1.0f,
  607. +1.0f, -1.0f, 0.0f, 1.0f,
  608. +1.0f, +1.0f, 0.0f, 1.0f,
  609. -1.0f, +1.0f, 0.0f, 1.0f,
  610. };
  611. const uint32_t indices[] = { 0, 1, 3, 2, 3, 1 };
  612. m_geometryLayout.begin().add(bgfx::Attrib::Position, 4, bgfx::AttribType::Float).end();
  613. m_geometryVertices = bgfx::createVertexBuffer(
  614. bgfx::copy(vertices, sizeof(vertices) )
  615. , m_geometryLayout
  616. , BGFX_BUFFER_COMPUTE_READ
  617. );
  618. m_geometryIndices = bgfx::createIndexBuffer(
  619. bgfx::copy(indices, sizeof(indices) )
  620. , BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_INDEX32
  621. );
  622. }
  623. void loadSubdivisionBuffers()
  624. {
  625. const uint32_t bufferCapacity = 1 << 27;
  626. m_bufferSubd[BUFFER_SUBD] = bgfx::createDynamicIndexBuffer(
  627. bufferCapacity
  628. , BGFX_BUFFER_COMPUTE_READ_WRITE | BGFX_BUFFER_INDEX32
  629. );
  630. m_bufferSubd[BUFFER_SUBD + 1] = bgfx::createDynamicIndexBuffer(
  631. bufferCapacity
  632. , BGFX_BUFFER_COMPUTE_READ_WRITE | BGFX_BUFFER_INDEX32
  633. );
  634. m_bufferCulledSubd = bgfx::createDynamicIndexBuffer(
  635. bufferCapacity
  636. , BGFX_BUFFER_COMPUTE_READ_WRITE | BGFX_BUFFER_INDEX32
  637. );
  638. }
  639. /**
  640. * Load All Buffers
  641. *
  642. */
  643. void loadBuffers()
  644. {
  645. loadSubdivisionBuffers();
  646. loadGeometryBuffers();
  647. loadInstancedGeometryBuffers();
  648. }
  649. /**
  650. * This will be used to instantiate a triangle grid for each subdivision
  651. * key present in the subd buffer.
  652. */
  653. void loadInstancedGeometryBuffers()
  654. {
  655. const float* vertices;
  656. const uint32_t* indexes;
  657. switch (int32_t(m_uniforms.gpuSubd) )
  658. {
  659. case 0:
  660. m_instancedMeshVertexCount = 3;
  661. m_instancedMeshPrimitiveCount = 1;
  662. vertices = s_verticesL0;
  663. indexes = s_indexesL0;
  664. break;
  665. case 1:
  666. m_instancedMeshVertexCount = 6;
  667. m_instancedMeshPrimitiveCount = 4;
  668. vertices = s_verticesL1;
  669. indexes = s_indexesL1;
  670. break;
  671. case 2:
  672. m_instancedMeshVertexCount = 15;
  673. m_instancedMeshPrimitiveCount = 16;
  674. vertices = s_verticesL2;
  675. indexes = s_indexesL2;
  676. break;
  677. default:
  678. m_instancedMeshVertexCount = 45;
  679. m_instancedMeshPrimitiveCount = 64;
  680. vertices = s_verticesL3;
  681. indexes = s_indexesL3;
  682. break;
  683. }
  684. m_instancedGeometryLayout
  685. .begin()
  686. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  687. .end();
  688. m_instancedGeometryVertices = bgfx::createVertexBuffer(
  689. bgfx::makeRef(vertices, sizeof(float) * 2 * m_instancedMeshVertexCount)
  690. , m_instancedGeometryLayout
  691. );
  692. m_instancedGeometryIndices = bgfx::createIndexBuffer(
  693. bgfx::makeRef(indexes, sizeof(uint32_t) * m_instancedMeshPrimitiveCount * 3)
  694. , BGFX_BUFFER_INDEX32
  695. );
  696. }
  697. Uniforms m_uniforms;
  698. bgfx::ProgramHandle m_programsCompute[PROGRAM_COUNT];
  699. bgfx::ProgramHandle m_programsDraw[SHADING_COUNT];
  700. bgfx::TextureHandle m_textures[TEXTURE_COUNT];
  701. bgfx::UniformHandle m_samplers[SAMPLER_COUNT];
  702. bgfx::DynamicIndexBufferHandle m_bufferSubd[2];
  703. bgfx::DynamicIndexBufferHandle m_bufferCulledSubd;
  704. bgfx::DynamicIndexBufferHandle m_bufferCounter;
  705. bgfx::IndexBufferHandle m_geometryIndices;
  706. bgfx::VertexBufferHandle m_geometryVertices;
  707. bgfx::VertexLayout m_geometryLayout;
  708. bgfx::IndexBufferHandle m_instancedGeometryIndices;
  709. bgfx::VertexBufferHandle m_instancedGeometryVertices;
  710. bgfx::VertexLayout m_instancedGeometryLayout;
  711. bgfx::IndirectBufferHandle m_dispatchIndirect;
  712. bimg::ImageContainer* dmap;
  713. float m_viewMtx[16];
  714. float m_projMtx[16];
  715. uint32_t m_width;
  716. uint32_t m_height;
  717. uint32_t m_debug;
  718. uint32_t m_reset;
  719. uint32_t m_oldWidth;
  720. uint32_t m_oldHeight;
  721. uint32_t m_oldReset;
  722. uint32_t m_instancedMeshVertexCount;
  723. uint32_t m_instancedMeshPrimitiveCount;
  724. entry::MouseState m_mouseState;
  725. int64_t m_timeOffset;
  726. struct DMap
  727. {
  728. bx::FilePath pathToFile;
  729. float scale;
  730. };
  731. DMap m_dmap;
  732. int m_computeThreadCount;
  733. int m_shading;
  734. int m_gpuSubd;
  735. int m_pingPong;
  736. float m_primitivePixelLengthTarget;
  737. float m_fovy;
  738. bool m_restart;
  739. bool m_wireframe;
  740. bool m_cull;
  741. bool m_freeze;
  742. };
  743. } // namespace
  744. ENTRY_IMPLEMENT_MAIN(
  745. ExampleTessellation
  746. , "41-tess"
  747. , "Adaptive GPU Tessellation."
  748. , "https://bkaradzic.github.io/bgfx/examples.html#tess"
  749. );