tess.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright 2019 Daniel Gavin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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.resolution.width = m_width;
  285. init.resolution.height = m_height;
  286. init.resolution.reset = m_reset;
  287. bgfx::init(init);
  288. m_dmap = { "textures/dmap.png", 0.45f };
  289. m_computeThreadCount = 5;
  290. m_shading = PROGRAM_TERRAIN;
  291. m_primitivePixelLengthTarget = 7.0f;
  292. m_fovy = 60.0f;
  293. m_pingPong = 0;
  294. m_restart = true;
  295. // Enable m_debug text.
  296. bgfx::setDebug(m_debug);
  297. // Set view 0 clear state.
  298. bgfx::setViewClear(0
  299. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  300. , 0x303030ff
  301. , 1.0f
  302. , 0
  303. );
  304. bgfx::setViewClear(1
  305. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  306. , 0x303030ff
  307. , 1.0f
  308. , 0
  309. );
  310. // Imgui.
  311. imguiCreate();
  312. m_timeOffset = bx::getHPCounter();
  313. m_oldWidth = 0;
  314. m_oldHeight = 0;
  315. m_oldReset = m_reset;
  316. cameraCreate();
  317. cameraSetPosition({ 0.0f, 0.5f, 0.0f });
  318. cameraSetVerticalAngle(0);
  319. m_wireframe = false;
  320. m_freeze = false;
  321. m_cull = true;
  322. loadPrograms();
  323. loadBuffers();
  324. loadTextures();
  325. createAtomicCounters();
  326. m_dispatchIndirect = bgfx::createIndirectBuffer(2);
  327. }
  328. virtual int shutdown() override
  329. {
  330. // Cleanup.
  331. cameraDestroy();
  332. imguiDestroy();
  333. m_uniforms.destroy();
  334. bgfx::destroy(m_bufferCounter);
  335. bgfx::destroy(m_bufferCulledSubd);
  336. bgfx::destroy(m_bufferSubd[0]);
  337. bgfx::destroy(m_bufferSubd[1]);
  338. bgfx::destroy(m_dispatchIndirect);
  339. bgfx::destroy(m_geometryIndices);
  340. bgfx::destroy(m_geometryVertices);
  341. bgfx::destroy(m_instancedGeometryIndices);
  342. bgfx::destroy(m_instancedGeometryVertices);
  343. for (uint32_t i = 0; i < PROGRAM_COUNT; ++i)
  344. {
  345. bgfx::destroy(m_programsCompute[i]);
  346. }
  347. for (uint32_t i = 0; i < SHADING_COUNT; ++i)
  348. {
  349. bgfx::destroy(m_programsDraw[i]);
  350. }
  351. for (uint32_t i = 0; i < SAMPLER_COUNT; ++i)
  352. {
  353. bgfx::destroy(m_samplers[i]);
  354. }
  355. for (uint32_t i = 0; i < TEXTURE_COUNT; ++i)
  356. {
  357. bgfx::destroy(m_textures[i]);
  358. }
  359. // Shutdown bgfx.
  360. bgfx::shutdown();
  361. return 0;
  362. }
  363. bool update() override
  364. {
  365. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  366. {
  367. int64_t now = bx::getHPCounter();
  368. static int64_t last = now;
  369. const int64_t frameTime = now - last;
  370. last = now;
  371. const double freq = double(bx::getHPFrequency() );
  372. const float deltaTime = float(frameTime / freq);
  373. imguiBeginFrame(
  374. m_mouseState.m_mx
  375. , m_mouseState.m_my
  376. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  377. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  378. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  379. , m_mouseState.m_mz
  380. , uint16_t(m_width)
  381. , uint16_t(m_height)
  382. );
  383. showExampleDialog(this);
  384. ImGui::SetNextWindowPos(
  385. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  386. , ImGuiCond_FirstUseEver
  387. );
  388. ImGui::SetNextWindowSize(
  389. ImVec2(m_width / 5.0f, m_height / 3.0f)
  390. , ImGuiCond_FirstUseEver
  391. );
  392. ImGui::Begin("Settings", NULL, 0);
  393. if (ImGui::Checkbox("Debug wireframe", &m_wireframe) )
  394. {
  395. bgfx::setDebug(m_wireframe
  396. ? BGFX_DEBUG_WIREFRAME
  397. : BGFX_DEBUG_NONE
  398. );
  399. }
  400. ImGui::SameLine();
  401. if (ImGui::Checkbox("Cull", &m_cull) )
  402. {
  403. m_uniforms.cull = m_cull ? 1.0f : 0.0f;
  404. }
  405. ImGui::SameLine();
  406. if (ImGui::Checkbox("Freeze subdividing", &m_freeze) )
  407. {
  408. m_uniforms.freeze = m_freeze ? 1.0f : 0.0f;
  409. }
  410. ImGui::SliderFloat("Pixels per edge", &m_primitivePixelLengthTarget, 1, 20);
  411. int gpuSlider = (int)m_uniforms.gpuSubd;
  412. if (ImGui::SliderInt("Triangle Patch level", &gpuSlider, 0, 3) )
  413. {
  414. m_restart = true;
  415. m_uniforms.gpuSubd = float(gpuSlider);
  416. }
  417. ImGui::Combo("Shading", &m_shading, s_shaderOptions, 2);
  418. ImGui::Text("Some variables require rebuilding the subdivide buffers and causes a stutter.");
  419. ImGui::End();
  420. // Update camera.
  421. cameraUpdate(deltaTime*0.01f, m_mouseState, ImGui::MouseOverArea() );
  422. bgfx::touch(0);
  423. bgfx::touch(1);
  424. configureUniforms();
  425. cameraGetViewMtx(m_viewMtx);
  426. float model[16];
  427. bx::mtxRotateX(model, bx::toRad(90) );
  428. bx::mtxProj(m_projMtx, m_fovy, float(m_width) / float(m_height), 0.0001f, 2000.0f, bgfx::getCaps()->homogeneousDepth);
  429. // Set view 0
  430. bgfx::setViewTransform(0, m_viewMtx, m_projMtx);
  431. // Set view 1
  432. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  433. bgfx::setViewTransform(1, m_viewMtx, m_projMtx);
  434. m_uniforms.submit();
  435. // update the subd buffers
  436. if (m_restart)
  437. {
  438. m_pingPong = 1;
  439. bgfx::destroy(m_instancedGeometryVertices);
  440. bgfx::destroy(m_instancedGeometryIndices);
  441. bgfx::destroy(m_bufferSubd[BUFFER_SUBD]);
  442. bgfx::destroy(m_bufferSubd[BUFFER_SUBD + 1]);
  443. bgfx::destroy(m_bufferCulledSubd);
  444. loadInstancedGeometryBuffers();
  445. loadSubdivisionBuffers();
  446. //init indirect
  447. bgfx::setBuffer(1, m_bufferSubd[m_pingPong], bgfx::Access::ReadWrite);
  448. bgfx::setBuffer(2, m_bufferCulledSubd, bgfx::Access::ReadWrite);
  449. bgfx::setBuffer(3, m_dispatchIndirect, bgfx::Access::ReadWrite);
  450. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  451. bgfx::setBuffer(8, m_bufferSubd[1 - m_pingPong], bgfx::Access::ReadWrite);
  452. bgfx::dispatch(0, m_programsCompute[PROGRAM_INIT_INDIRECT], 1, 1, 1);
  453. m_restart = false;
  454. }
  455. else
  456. {
  457. // update batch
  458. bgfx::setBuffer(3, m_dispatchIndirect, bgfx::Access::ReadWrite);
  459. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  460. bgfx::dispatch(0, m_programsCompute[PROGRAM_UPDATE_INDIRECT], 1, 1, 1);
  461. }
  462. bgfx::setBuffer(1, m_bufferSubd[m_pingPong], bgfx::Access::ReadWrite);
  463. bgfx::setBuffer(2, m_bufferCulledSubd, bgfx::Access::ReadWrite);
  464. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  465. bgfx::setBuffer(6, m_geometryVertices, bgfx::Access::Read);
  466. bgfx::setBuffer(7, m_geometryIndices, bgfx::Access::Read);
  467. bgfx::setBuffer(8, m_bufferSubd[1 - m_pingPong], bgfx::Access::Read);
  468. bgfx::setTransform(model);
  469. bgfx::setTexture(0, m_samplers[TERRAIN_DMAP_SAMPLER], m_textures[TEXTURE_DMAP], BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  470. m_uniforms.submit();
  471. // update the subd buffer
  472. bgfx::dispatch(0, m_programsCompute[PROGRAM_SUBD_CS_LOD], m_dispatchIndirect, 1);
  473. // update draw
  474. bgfx::setBuffer(3, m_dispatchIndirect, bgfx::Access::ReadWrite);
  475. bgfx::setBuffer(4, m_bufferCounter, bgfx::Access::ReadWrite);
  476. m_uniforms.submit();
  477. bgfx::dispatch(1, m_programsCompute[PROGRAM_UPDATE_DRAW], 1, 1, 1);
  478. // render the terrain
  479. bgfx::setTexture(0, m_samplers[TERRAIN_DMAP_SAMPLER], m_textures[TEXTURE_DMAP], BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  480. bgfx::setTexture(1, m_samplers[TERRAIN_SMAP_SAMPLER], m_textures[TEXTURE_SMAP], BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC);
  481. bgfx::setTransform(model);
  482. bgfx::setVertexBuffer(0, m_instancedGeometryVertices);
  483. bgfx::setIndexBuffer(m_instancedGeometryIndices);
  484. bgfx::setBuffer(2, m_bufferCulledSubd, bgfx::Access::Read);
  485. bgfx::setBuffer(3, m_geometryVertices, bgfx::Access::Read);
  486. bgfx::setBuffer(4, m_geometryIndices, bgfx::Access::Read);
  487. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS);
  488. m_uniforms.submit();
  489. bgfx::submit(1, m_programsDraw[m_shading], m_dispatchIndirect);
  490. m_pingPong = 1 - m_pingPong;
  491. imguiEndFrame();
  492. // Advance to next frame. Rendering thread will be kicked to
  493. // process submitted rendering primitives.
  494. bgfx::frame(false);
  495. return true;
  496. }
  497. return false;
  498. }
  499. void createAtomicCounters()
  500. {
  501. m_bufferCounter = bgfx::createDynamicIndexBuffer(3, BGFX_BUFFER_INDEX32 | BGFX_BUFFER_COMPUTE_READ_WRITE);
  502. }
  503. void configureUniforms()
  504. {
  505. float lodFactor = 2.0f * bx::tan(bx::toRad(m_fovy) / 2.0f)
  506. / m_width * (1 << (int)m_uniforms.gpuSubd)
  507. * m_primitivePixelLengthTarget;
  508. m_uniforms.lodFactor = lodFactor;
  509. m_uniforms.dmapFactor = m_dmap.scale;
  510. }
  511. /**
  512. * Load the Terrain Program
  513. *
  514. * This program renders an adaptive terrain using the implicit subdivision
  515. * technique discribed in GPU Zen 2.
  516. **/
  517. void loadPrograms()
  518. {
  519. m_samplers[TERRAIN_DMAP_SAMPLER] = bgfx::createUniform("u_DmapSampler", bgfx::UniformType::Sampler);
  520. m_samplers[TERRAIN_SMAP_SAMPLER] = bgfx::createUniform("u_SmapSampler", bgfx::UniformType::Sampler);
  521. m_uniforms.init();
  522. m_programsDraw[PROGRAM_TERRAIN] = loadProgram("vs_terrain_render", "fs_terrain_render");
  523. m_programsDraw[PROGRAM_TERRAIN_NORMAL] = loadProgram("vs_terrain_render", "fs_terrain_render_normal");
  524. m_programsCompute[PROGRAM_SUBD_CS_LOD] = bgfx::createProgram(loadShader("cs_terrain_lod"), true);
  525. m_programsCompute[PROGRAM_UPDATE_INDIRECT] = bgfx::createProgram(loadShader("cs_terrain_update_indirect"), true);
  526. m_programsCompute[PROGRAM_UPDATE_DRAW] = bgfx::createProgram(loadShader("cs_terrain_update_draw"), true);
  527. m_programsCompute[PROGRAM_INIT_INDIRECT] = bgfx::createProgram(loadShader("cs_terrain_init"), true);
  528. }
  529. void loadSmapTexture()
  530. {
  531. int w = dmap->m_width;
  532. int h = dmap->m_height;
  533. const uint16_t *texels = (const uint16_t *)dmap->m_data;
  534. int mipcnt = dmap->m_numMips;
  535. const bgfx::Memory* mem = bgfx::alloc(w * h * 2 * sizeof(float) );
  536. float* smap = (float*)mem->data;
  537. for (int j = 0; j < h; ++j)
  538. {
  539. for (int i = 0; i < w; ++i)
  540. {
  541. int i1 = bx::max(0, i - 1);
  542. int i2 = bx::min(w - 1, i + 1);
  543. int j1 = bx::max(0, j - 1);
  544. int j2 = bx::min(h - 1, j + 1);
  545. uint16_t px_l = texels[i1 + w * j]; // in [0,2^16-1]
  546. uint16_t px_r = texels[i2 + w * j]; // in [0,2^16-1]
  547. uint16_t px_b = texels[i + w * j1]; // in [0,2^16-1]
  548. uint16_t px_t = texels[i + w * j2]; // in [0,2^16-1]
  549. float z_l = (float)px_l / 65535.0f; // in [0, 1]
  550. float z_r = (float)px_r / 65535.0f; // in [0, 1]
  551. float z_b = (float)px_b / 65535.0f; // in [0, 1]
  552. float z_t = (float)px_t / 65535.0f; // in [0, 1]
  553. float slope_x = (float)w * 0.5f * (z_r - z_l);
  554. float slope_y = (float)h * 0.5f * (z_t - z_b);
  555. smap[2 * (i + w * j)] = slope_x;
  556. smap[1 + 2 * (i + w * j)] = slope_y;
  557. }
  558. }
  559. m_textures[TEXTURE_SMAP] = bgfx::createTexture2D(
  560. (uint16_t)w
  561. , (uint16_t)h
  562. , mipcnt > 1
  563. , 1
  564. , bgfx::TextureFormat::RG32F
  565. , BGFX_TEXTURE_NONE
  566. , mem
  567. );
  568. }
  569. /**
  570. * Load the Displacement Texture
  571. *
  572. * This loads an R16 texture used as a displacement map
  573. */
  574. void loadDmapTexture()
  575. {
  576. dmap = imageLoad(m_dmap.pathToFile.getCPtr(), bgfx::TextureFormat::R16);
  577. m_textures[TEXTURE_DMAP] = bgfx::createTexture2D(
  578. (uint16_t)dmap->m_width
  579. , (uint16_t)dmap->m_height
  580. , false
  581. , 1
  582. , bgfx::TextureFormat::R16
  583. , BGFX_TEXTURE_NONE
  584. , bgfx::makeRef(dmap->m_data, dmap->m_size)
  585. );
  586. }
  587. /**
  588. * Load All Textures
  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. );