gpudrivenrendering.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * Copyright 2018 Kostas Anagnostou. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. /*
  6. * Reference(s):
  7. * - Experiments in GPU-based occlusion culling
  8. * https://web.archive.org/web/20180920045301/https://interplayoflight.wordpress.com/2017/11/15/experiments-in-gpu-based-occlusion-culling/
  9. * - Experiments in GPU-based occlusion culling part 2: MultiDrawIndirect and mesh lodding
  10. * https://web.archive.org/web/20180920045332/https://interplayoflight.wordpress.com/2018/01/15/experiments-in-gpu-based-occlusion-culling-part-2-multidrawindirect-and-mesh-lodding/
  11. */
  12. #include "common.h"
  13. #include "bgfx_utils.h"
  14. #include "imgui/imgui.h"
  15. namespace
  16. {
  17. #define RENDER_PASS_HIZ_ID 0
  18. #define RENDER_PASS_HIZ_DOWNSCALE_ID 1
  19. #define RENDER_PASS_OCCLUDE_PROPS_ID 2
  20. #define RENDER_PASS_COMPACT_STREAM_ID 3
  21. #define RENDER_PASS_MAIN_ID 4
  22. struct Camera
  23. {
  24. Camera()
  25. {
  26. reset();
  27. }
  28. void reset()
  29. {
  30. m_target.curr = { 0.0f, 0.0f, 0.0f };
  31. m_target.dest = { 0.0f, 0.0f, 0.0f };
  32. m_pos.curr = { 55.0f, 20.0f, 65.0f };
  33. m_pos.dest = { 55.0f, 20.0f, 65.0f };
  34. m_orbit[0] = 0.0f;
  35. m_orbit[1] = 0.0f;
  36. }
  37. void mtxLookAt(float* _outViewMtx)
  38. {
  39. bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr);
  40. }
  41. void orbit(float _dx, float _dy)
  42. {
  43. m_orbit[0] += _dx;
  44. m_orbit[1] += _dy;
  45. }
  46. void dolly(float _dz)
  47. {
  48. const float cnear = 1.0f;
  49. const float cfar = 100.0f;
  50. const bx::Vec3 toTarget = bx::sub(m_target.dest, m_pos.dest);
  51. const float toTargetLen = bx::length(toTarget);
  52. const float invToTargetLen = 1.0f / (toTargetLen + bx::kFloatSmallest);
  53. const bx::Vec3 toTargetNorm = bx::mul(toTarget, invToTargetLen);
  54. float delta = toTargetLen * _dz;
  55. float newLen = toTargetLen + delta;
  56. if ( (cnear < newLen || _dz < 0.0f)
  57. && (newLen < cfar || _dz > 0.0f) )
  58. {
  59. m_pos.dest = bx::mad(toTargetNorm, delta, m_pos.dest);
  60. }
  61. }
  62. void consumeOrbit(float _amount)
  63. {
  64. float consume[2];
  65. consume[0] = m_orbit[0] * _amount;
  66. consume[1] = m_orbit[1] * _amount;
  67. m_orbit[0] -= consume[0];
  68. m_orbit[1] -= consume[1];
  69. const bx::Vec3 toPos = bx::sub(m_pos.curr, m_target.curr);
  70. const float toPosLen = bx::length(toPos);
  71. const float invToPosLen = 1.0f / (toPosLen + bx::kFloatSmallest);
  72. const bx::Vec3 toPosNorm = bx::mul(toPos, invToPosLen);
  73. float ll[2];
  74. bx::toLatLong(&ll[0], &ll[1], toPosNorm);
  75. ll[0] += consume[0];
  76. ll[1] -= consume[1];
  77. ll[1] = bx::clamp(ll[1], 0.02f, 0.98f);
  78. const bx::Vec3 tmp = bx::fromLatLong(ll[0], ll[1]);
  79. const bx::Vec3 diff = bx::mul(bx::sub(tmp, toPosNorm), toPosLen);
  80. m_pos.curr = bx::add(m_pos.curr, diff);
  81. m_pos.dest = bx::add(m_pos.dest, diff);
  82. }
  83. void update(float _dt)
  84. {
  85. const float amount = bx::min(_dt / 0.12f, 1.0f);
  86. consumeOrbit(amount);
  87. m_target.curr = bx::lerp(m_target.curr, m_target.dest, amount);
  88. m_pos.curr = bx::lerp(m_pos.curr, m_pos.dest, amount);
  89. }
  90. void envViewMtx(float* _mtx)
  91. {
  92. const bx::Vec3 toTarget = bx::sub(m_target.curr, m_pos.curr);
  93. const float toTargetLen = bx::length(toTarget);
  94. const float invToTargetLen = 1.0f / (toTargetLen + bx::kFloatSmallest);
  95. const bx::Vec3 toTargetNorm = bx::mul(toTarget, invToTargetLen);
  96. const bx::Vec3 right = bx::normalize(bx::cross({ 0.0f, 1.0f, 0.0f }, toTargetNorm) );
  97. const bx::Vec3 up = bx::normalize(bx::cross(toTargetNorm, right) );
  98. _mtx[ 0] = right.x;
  99. _mtx[ 1] = right.y;
  100. _mtx[ 2] = right.z;
  101. _mtx[ 3] = 0.0f;
  102. _mtx[ 4] = up.x;
  103. _mtx[ 5] = up.y;
  104. _mtx[ 6] = up.z;
  105. _mtx[ 7] = 0.0f;
  106. _mtx[ 8] = toTargetNorm.x;
  107. _mtx[ 9] = toTargetNorm.y;
  108. _mtx[10] = toTargetNorm.z;
  109. _mtx[11] = 0.0f;
  110. _mtx[12] = 0.0f;
  111. _mtx[13] = 0.0f;
  112. _mtx[14] = 0.0f;
  113. _mtx[15] = 1.0f;
  114. }
  115. struct Interp3f
  116. {
  117. bx::Vec3 curr = bx::init::None;
  118. bx::Vec3 dest = bx::init::None;
  119. };
  120. Interp3f m_target;
  121. Interp3f m_pos;
  122. float m_orbit[2];
  123. };
  124. struct Mouse
  125. {
  126. Mouse()
  127. : m_dx(0.0f)
  128. , m_dy(0.0f)
  129. , m_prevMx(0.0f)
  130. , m_prevMy(0.0f)
  131. , m_scroll(0)
  132. , m_scrollPrev(0)
  133. {
  134. }
  135. void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height)
  136. {
  137. const float widthf = float(int32_t(_width));
  138. const float heightf = float(int32_t(_height));
  139. // Delta movement.
  140. m_dx = float(_mx - m_prevMx) / widthf;
  141. m_dy = float(_my - m_prevMy) / heightf;
  142. m_prevMx = _mx;
  143. m_prevMy = _my;
  144. // Scroll.
  145. m_scroll = _mz - m_scrollPrev;
  146. m_scrollPrev = _mz;
  147. }
  148. float m_dx; // Screen space.
  149. float m_dy;
  150. float m_prevMx;
  151. float m_prevMy;
  152. int32_t m_scroll;
  153. int32_t m_scrollPrev;
  154. };
  155. struct PosVertex
  156. {
  157. float m_x;
  158. float m_y;
  159. float m_z;
  160. static void init()
  161. {
  162. ms_layout
  163. .begin()
  164. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  165. .end();
  166. };
  167. static bgfx::VertexLayout ms_layout;
  168. };
  169. bgfx::VertexLayout PosVertex::ms_layout;
  170. static PosVertex s_cubeVertices[8] =
  171. {
  172. {-0.5f, 0.5f, 0.5f},
  173. { 0.5f, 0.5f, 0.5f},
  174. {-0.5f, -0.5f, 0.5f},
  175. { 0.5f, -0.5f, 0.5f},
  176. {-0.5f, 0.5f, -0.5f},
  177. { 0.5f, 0.5f, -0.5f},
  178. {-0.5f, -0.5f, -0.5f},
  179. { 0.5f, -0.5f, -0.5f},
  180. };
  181. static const uint16_t s_cubeIndices[36] =
  182. {
  183. 0, 1, 2, // 0
  184. 1, 3, 2,
  185. 4, 6, 5, // 2
  186. 5, 6, 7,
  187. 0, 2, 4, // 4
  188. 4, 2, 6,
  189. 1, 5, 3, // 6
  190. 5, 7, 3,
  191. 0, 4, 1, // 8
  192. 4, 5, 1,
  193. 2, 3, 6, // 10
  194. 6, 3, 7,
  195. };
  196. struct RenderPass
  197. {
  198. enum Enum
  199. {
  200. Occlusion = 1 << 0,
  201. MainPass = 1 << 1,
  202. All = Occlusion | MainPass
  203. };
  204. };
  205. // All the per-instance data we store
  206. struct InstanceData
  207. {
  208. float m_world[16];
  209. float m_bboxMin[4];
  210. float m_bboxMax[4];
  211. };
  212. //A description of each prop
  213. struct Prop
  214. {
  215. PosVertex* m_vertices;
  216. uint16_t* m_indices;
  217. InstanceData* m_instances;
  218. bgfx::VertexBufferHandle m_vertexbufferHandle;
  219. bgfx::IndexBufferHandle m_indexbufferHandle;
  220. uint16_t m_noofVertices;
  221. uint16_t m_noofIndices;
  222. uint16_t m_noofInstances;
  223. uint16_t m_materialID;
  224. RenderPass::Enum m_renderPass;
  225. };
  226. //A simplistic material, comprised of a color only
  227. struct Material
  228. {
  229. float m_color[4];
  230. };
  231. inline void setVector4(float* dest, float x, float y, float z, float w)
  232. {
  233. dest[0] = x;
  234. dest[1] = y;
  235. dest[2] = z;
  236. dest[3] = w;
  237. }
  238. //Sets up a prop
  239. void createCubeMesh(Prop& prop)
  240. {
  241. prop.m_noofVertices = 8;
  242. prop.m_noofIndices = 36;
  243. prop.m_vertices = new PosVertex[prop.m_noofVertices];
  244. prop.m_indices = new uint16_t[prop.m_noofIndices];
  245. bx::memCopy(prop.m_vertices, s_cubeVertices, prop.m_noofVertices * PosVertex::ms_layout.getStride());
  246. bx::memCopy(prop.m_indices, s_cubeIndices, prop.m_noofIndices * sizeof(uint16_t));
  247. prop.m_vertexbufferHandle = bgfx::createVertexBuffer(
  248. bgfx::makeRef(prop.m_vertices, prop.m_noofVertices * PosVertex::ms_layout.getStride()),
  249. PosVertex::ms_layout);
  250. prop.m_indexbufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(prop.m_indices, prop.m_noofIndices * sizeof(uint16_t)));
  251. }
  252. //returns a random number between 0 and 1
  253. float rand01()
  254. {
  255. return rand() / (float)RAND_MAX;
  256. }
  257. class GPUDrivenRendering : public entry::AppI
  258. {
  259. public:
  260. GPUDrivenRendering(const char* _name, const char* _description, const char* _url)
  261. : entry::AppI(_name, _description, _url)
  262. {
  263. }
  264. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  265. {
  266. Args args(_argc, _argv);
  267. m_width = _width;
  268. m_height = _height;
  269. //find largest pow of two dims less than backbuffer size
  270. m_hiZwidth = (uint32_t)bx::pow(2.0f, bx::floor(bx::log2(float(m_width ) ) ) );
  271. m_hiZheight = (uint32_t)bx::pow(2.0f, bx::floor(bx::log2(float(m_height) ) ) );
  272. m_debug = BGFX_DEBUG_TEXT;
  273. m_reset = BGFX_RESET_VSYNC;
  274. bgfx::Init init;
  275. init.type = args.m_type;
  276. init.vendorId = args.m_pciId;
  277. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  278. init.platformData.ndt = entry::getNativeDisplayHandle();
  279. init.resolution.width = m_width;
  280. init.resolution.height = m_height;
  281. init.resolution.reset = m_reset;
  282. bgfx::init(init);
  283. // Enable debug text.
  284. bgfx::setDebug(m_debug);
  285. // Create uniforms and samplers.
  286. u_inputRTSize = bgfx::createUniform("u_inputRTSize", bgfx::UniformType::Vec4);
  287. u_cullingConfig = bgfx::createUniform("u_cullingConfig", bgfx::UniformType::Vec4);
  288. u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4, 32);
  289. s_texOcclusionDepth = bgfx::createUniform("s_texOcclusionDepth", bgfx::UniformType::Sampler);
  290. //create props
  291. {
  292. m_totalInstancesCount = 0;
  293. // Create vertex stream declaration.
  294. PosVertex::init();
  295. m_noofProps = 0;
  296. m_props = new Prop[s_maxNoofProps];
  297. //first create space for some materials
  298. m_materials = new Material[s_maxNoofProps];
  299. m_noofMaterials = 0;
  300. //add a ground plane
  301. {
  302. Prop& prop = m_props[m_noofProps++];
  303. prop.m_renderPass = RenderPass::MainPass;
  304. createCubeMesh(prop);
  305. prop.m_noofInstances = 1;
  306. prop.m_instances = new InstanceData[prop.m_noofInstances];
  307. bx::mtxSRT(prop.m_instances->m_world
  308. , 100.0f, 0.1f, 100.0f
  309. , 0.0f, 0.0f, 0.0f
  310. , 0.0f, 0.0f, 0.0f
  311. );
  312. float temp[4];
  313. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  314. bx::vec4MulMtx(prop.m_instances->m_bboxMin, temp, prop.m_instances->m_world);
  315. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  316. bx::vec4MulMtx(prop.m_instances->m_bboxMax, temp, prop.m_instances->m_world);
  317. prop.m_materialID = m_noofMaterials;
  318. setVector4(m_materials[prop.m_materialID].m_color, 0.0f, 0.6f, 0.0f, 1.0f);
  319. m_noofMaterials++;
  320. m_totalInstancesCount += prop.m_noofInstances;
  321. }
  322. //add a few instances of the occluding mesh
  323. {
  324. Prop& prop = m_props[m_noofProps++];
  325. prop.m_renderPass = RenderPass::All;
  326. //create prop
  327. createCubeMesh(prop);
  328. //add a few instances of the wall mesh
  329. prop.m_noofInstances = 25;
  330. prop.m_instances = new InstanceData[prop.m_noofInstances];
  331. for (int i = 0; i < prop.m_noofInstances; i++)
  332. {
  333. //calculate world position
  334. bx::mtxSRT(prop.m_instances[i].m_world
  335. , 40.0f, 10.0f, 0.1f
  336. , 0.0f, ( rand01() * 120.0f - 60.0f) * 3.1459f / 180.0f, 0.0f
  337. , rand01() * 100.0f - 50.0f, 5.0f, rand01() * 100.0f - 50.0f
  338. );
  339. //calculate bounding box and transform to world space
  340. float temp[4];
  341. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  342. bx::vec4MulMtx(prop.m_instances[i].m_bboxMin, temp, prop.m_instances[i].m_world );
  343. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  344. bx::vec4MulMtx(prop.m_instances[i].m_bboxMax, temp, prop.m_instances[i].m_world );
  345. }
  346. //set the material ID. Will be used in the shader to select the material
  347. prop.m_materialID = m_noofMaterials;
  348. //add a "material" for this prop
  349. setVector4(m_materials[prop.m_materialID].m_color, 0.0f, 0.0f, 1.0f, 0.0f);
  350. m_noofMaterials++;
  351. m_totalInstancesCount += prop.m_noofInstances;
  352. }
  353. //add a few "regular" props
  354. {
  355. //add cubes
  356. {
  357. Prop& prop = m_props[m_noofProps++];
  358. prop.m_renderPass = RenderPass::MainPass;
  359. createCubeMesh(prop);
  360. prop.m_noofInstances = 200;
  361. prop.m_instances = new InstanceData[prop.m_noofInstances];
  362. for (int i = 0; i < prop.m_noofInstances; i++)
  363. {
  364. bx::mtxSRT(prop.m_instances[i].m_world
  365. , 2.0f, 2.0f, 2.0f
  366. , 0.0f, 0.0f, 0.0f
  367. , rand01() * 100.0f - 50.0f, 1.0f, rand01() * 100.0f - 50.0f
  368. );
  369. float temp[4];
  370. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  371. bx::vec4MulMtx(prop.m_instances[i].m_bboxMin, temp, prop.m_instances[i].m_world);
  372. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  373. bx::vec4MulMtx(prop.m_instances[i].m_bboxMax, temp, prop.m_instances[i].m_world);
  374. }
  375. prop.m_materialID = m_noofMaterials;
  376. setVector4(m_materials[prop.m_materialID].m_color, 1.0f, 1.0f, 0.0f, 1.0f);
  377. m_noofMaterials++;
  378. m_totalInstancesCount += prop.m_noofInstances;
  379. }
  380. //add some more cubes
  381. {
  382. Prop& prop = m_props[m_noofProps++];
  383. prop.m_renderPass = RenderPass::MainPass;
  384. createCubeMesh(prop);
  385. prop.m_noofInstances = 300;
  386. prop.m_instances = new InstanceData[prop.m_noofInstances];
  387. for (int i = 0; i < prop.m_noofInstances; i++)
  388. {
  389. bx::mtxSRT(prop.m_instances[i].m_world
  390. , 2.0f, 4.0f, 2.0f
  391. , 0.0f, 0.0f, 0.0f
  392. , rand01() * 100.0f - 50.0f, 2.0f, rand01() * 100.0f - 50.0f
  393. );
  394. float temp[4];
  395. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  396. bx::vec4MulMtx(prop.m_instances[i].m_bboxMin, temp, prop.m_instances[i].m_world );
  397. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  398. bx::vec4MulMtx(prop.m_instances[i].m_bboxMax, temp, prop.m_instances[i].m_world);
  399. }
  400. prop.m_materialID = m_noofMaterials;
  401. setVector4(m_materials[prop.m_materialID].m_color, 1.0f, 0.0f, 0.0f, 1.0f);
  402. m_noofMaterials++;
  403. m_totalInstancesCount += prop.m_noofInstances;
  404. }
  405. }
  406. }
  407. //Setup Occlusion pass
  408. {
  409. const uint64_t tsFlags = 0
  410. | BGFX_TEXTURE_RT
  411. | BGFX_SAMPLER_MIN_POINT
  412. | BGFX_SAMPLER_MAG_POINT
  413. | BGFX_SAMPLER_MIP_POINT
  414. | BGFX_SAMPLER_U_CLAMP
  415. | BGFX_SAMPLER_V_CLAMP
  416. ;
  417. // Create buffers for the HiZ pass
  418. m_hiZDepthBuffer = bgfx::createFrameBuffer(uint16_t(m_hiZwidth), uint16_t(m_hiZheight), bgfx::TextureFormat::D32F, tsFlags);
  419. bgfx::TextureHandle buffer = bgfx::createTexture2D(uint16_t(m_hiZwidth), uint16_t(m_hiZheight), true, 1, bgfx::TextureFormat::R32F, BGFX_TEXTURE_COMPUTE_WRITE | tsFlags);
  420. m_hiZBuffer = bgfx::createFrameBuffer(1, &buffer, true);
  421. //how many mip will the Hi Z buffer have?
  422. m_noofHiZMips = (uint8_t)(1 + bx::floor(bx::log2(float(bx::max(m_hiZwidth, m_hiZheight) ) ) ) );
  423. // Setup compute shader buffers
  424. //The compute shader will write how many unoccluded instances per drawcall there are here
  425. m_drawcallInstanceCounts = bgfx::createDynamicIndexBuffer(s_maxNoofProps, BGFX_BUFFER_INDEX32 | BGFX_BUFFER_COMPUTE_READ_WRITE);
  426. //the compute shader will write the result of the occlusion test for each instance here
  427. m_instancePredicates = bgfx::createDynamicIndexBuffer(s_maxNoofInstances, BGFX_BUFFER_COMPUTE_READ_WRITE);
  428. //bounding box for each instance, will be fed to the compute shader to calculate occlusion
  429. {
  430. bgfx::VertexLayout computeVertexLayout;
  431. computeVertexLayout.begin()
  432. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  433. .end();
  434. //initialise the buffer with the bounding boxes of all instances
  435. const int sizeOfBuffer = 2 * 4 * m_totalInstancesCount;
  436. float* boundingBoxes = new float[sizeOfBuffer];
  437. float* data = boundingBoxes;
  438. for (uint16_t i = 0; i < m_noofProps; i++)
  439. {
  440. Prop& prop = m_props[i];
  441. const uint32_t numInstances = prop.m_noofInstances;
  442. for (uint32_t j = 0; j < numInstances; j++)
  443. {
  444. bx::memCopy(data, prop.m_instances[j].m_bboxMin, 3 * sizeof(float));
  445. data[3] = (float)i; // store the drawcall ID here to avoid creating a separate buffer
  446. data += 4;
  447. bx::memCopy(data, prop.m_instances[j].m_bboxMax, 3 * sizeof(float));
  448. data += 4;
  449. }
  450. }
  451. const bgfx::Memory* mem = bgfx::makeRef(boundingBoxes, sizeof(float) * sizeOfBuffer);
  452. m_instanceBoundingBoxes = bgfx::createDynamicVertexBuffer(mem, computeVertexLayout, BGFX_BUFFER_COMPUTE_READ);
  453. }
  454. //pre and post occlusion culling instance data buffers
  455. {
  456. bgfx::VertexLayout instanceBufferVertexLayout;
  457. instanceBufferVertexLayout.begin()
  458. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  459. .add(bgfx::Attrib::TexCoord1, 4, bgfx::AttribType::Float)
  460. .add(bgfx::Attrib::TexCoord2, 4, bgfx::AttribType::Float)
  461. .add(bgfx::Attrib::TexCoord3, 4, bgfx::AttribType::Float)
  462. .end();
  463. //initialise the buffer with data for all instances
  464. //Currently we only store a world matrix (16 floats)
  465. const int sizeOfBuffer = 16 * m_totalInstancesCount;
  466. float* instanceData = new float[sizeOfBuffer];
  467. float* data = instanceData;
  468. for (uint16_t ii = 0; ii < m_noofProps; ++ii)
  469. {
  470. Prop& prop = m_props[ii];
  471. const uint32_t numInstances = prop.m_noofInstances;
  472. for (uint32_t jj = 0; jj < numInstances; ++jj)
  473. {
  474. bx::memCopy(data, prop.m_instances[jj].m_world, 16 * sizeof(float) );
  475. data[3] = float(ii); // store the drawcall ID here to avoid creating a separate buffer
  476. data += 16;
  477. }
  478. }
  479. const bgfx::Memory* mem = bgfx::makeRef(instanceData, sizeof(float) * sizeOfBuffer);
  480. //pre occlusion buffer
  481. m_instanceBuffer = bgfx::createVertexBuffer(mem, instanceBufferVertexLayout, BGFX_BUFFER_COMPUTE_READ);
  482. //post occlusion buffer
  483. m_culledInstanceBuffer = bgfx::createDynamicVertexBuffer(4 * m_totalInstancesCount, instanceBufferVertexLayout, BGFX_BUFFER_COMPUTE_WRITE);
  484. }
  485. //we use one "drawcall" per prop to render all its instances
  486. m_indirectBuffer = bgfx::createIndirectBuffer(m_noofProps);
  487. // Create programs from shaders for occlusion pass.
  488. m_programOcclusionPass = loadProgram("vs_gdr_render_occlusion", NULL);
  489. m_programCopyZ = loadProgram("cs_gdr_copy_z", NULL);
  490. m_programDownscaleHiZ = loadProgram("cs_gdr_downscale_hi_z", NULL);
  491. m_programOccludeProps = loadProgram("cs_gdr_occlude_props", NULL);
  492. m_programStreamCompaction = loadProgram("cs_gdr_stream_compaction", NULL);
  493. // Set view RENDER_PASS_HIZ_ID clear state.
  494. bgfx::setViewClear(RENDER_PASS_HIZ_ID
  495. , BGFX_CLEAR_DEPTH
  496. , 0x0
  497. , 1.0f
  498. , 0
  499. );
  500. }
  501. // Setup Main pass
  502. {
  503. // Set view 0 clear state.
  504. bgfx::setViewClear(RENDER_PASS_MAIN_ID
  505. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  506. , 0x303030ff
  507. , 1.0f
  508. , 0
  509. );
  510. // Create program from shaders.
  511. m_programMainPass = loadProgram("vs_gdr_instanced_indirect_rendering", "fs_gdr_instanced_indirect_rendering");
  512. }
  513. // Create static vertex buffer for all props.
  514. // Calculate how many vertices/indices the master buffers will need.
  515. uint16_t totalNoofVertices = 0;
  516. uint16_t totalNoofIndices = 0;
  517. for (uint16_t i = 0; i < m_noofProps; i++)
  518. {
  519. Prop& prop = m_props[i];
  520. totalNoofVertices += prop.m_noofVertices;
  521. totalNoofIndices += prop.m_noofIndices;
  522. }
  523. // CPU data to fill the master buffers
  524. m_allPropVerticesDataCPU = new PosVertex[totalNoofVertices];
  525. m_allPropIndicesDataCPU = new uint16_t[totalNoofIndices];
  526. m_indirectBufferDataCPU = new uint32_t[m_noofProps * 3];
  527. // Copy data over to the master buffers
  528. PosVertex* propVerticesData = m_allPropVerticesDataCPU;
  529. uint16_t* propIndicesData = m_allPropIndicesDataCPU;
  530. uint16_t vertexBufferOffset = 0;
  531. uint16_t indexBufferOffset = 0;
  532. for (uint16_t i = 0; i < m_noofProps; i++)
  533. {
  534. Prop& prop = m_props[i];
  535. bx::memCopy(propVerticesData, prop.m_vertices, prop.m_noofVertices * sizeof(PosVertex));
  536. bx::memCopy(propIndicesData, prop.m_indices, prop.m_noofIndices * sizeof(uint16_t));
  537. propVerticesData += prop.m_noofVertices;
  538. propIndicesData += prop.m_noofIndices;
  539. m_indirectBufferDataCPU[ i * 3 ] = prop.m_noofIndices;
  540. m_indirectBufferDataCPU[ i * 3 + 1] = indexBufferOffset;
  541. m_indirectBufferDataCPU[ i * 3 + 2] = vertexBufferOffset;
  542. indexBufferOffset += prop.m_noofIndices;
  543. vertexBufferOffset += prop.m_noofVertices;
  544. }
  545. // Create master vertex buffer
  546. m_allPropsVertexbufferHandle = bgfx::createVertexBuffer(
  547. bgfx::makeRef(m_allPropVerticesDataCPU, totalNoofVertices * PosVertex::ms_layout.getStride())
  548. , PosVertex::ms_layout
  549. );
  550. // Create master index buffer.
  551. m_allPropsIndexbufferHandle = bgfx::createIndexBuffer(
  552. bgfx::makeRef(m_allPropIndicesDataCPU, totalNoofIndices * sizeof(uint16_t) )
  553. );
  554. // Create buffer with const drawcall data which will be copied to the indirect buffer later.
  555. m_indirectBufferData = bgfx::createIndexBuffer(
  556. bgfx::makeRef(m_indirectBufferDataCPU, m_noofProps * 3 * sizeof(uint32_t)),
  557. BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_INDEX32
  558. );
  559. m_timeOffset = bx::getHPCounter();
  560. m_useIndirect = true;
  561. m_firstFrame = true;
  562. imguiCreate();
  563. }
  564. int shutdown() override
  565. {
  566. imguiDestroy();
  567. // Cleanup.
  568. bgfx::destroy(m_programMainPass);
  569. bgfx::destroy(m_programOcclusionPass);
  570. bgfx::destroy(m_programCopyZ);
  571. bgfx::destroy(m_programDownscaleHiZ);
  572. bgfx::destroy(m_programOccludeProps);
  573. bgfx::destroy(m_programStreamCompaction);
  574. for (uint16_t i = 0; i < m_noofProps; i++)
  575. {
  576. Prop& prop = m_props[i];
  577. bgfx::destroy(prop.m_indexbufferHandle);
  578. bgfx::destroy(prop.m_vertexbufferHandle);
  579. delete[] prop.m_indices;
  580. delete[] prop.m_vertices;
  581. delete[] prop.m_instances;
  582. }
  583. delete[] m_props;
  584. bgfx::destroy(m_hiZDepthBuffer);
  585. bgfx::destroy(m_hiZBuffer);
  586. bgfx::destroy(m_indirectBuffer);
  587. bgfx::destroy(m_indirectBufferData);
  588. bgfx::destroy(m_instanceBoundingBoxes);
  589. bgfx::destroy(m_drawcallInstanceCounts);
  590. bgfx::destroy(m_instancePredicates);
  591. bgfx::destroy(m_instanceBuffer);
  592. bgfx::destroy(m_culledInstanceBuffer);
  593. bgfx::destroy(m_allPropsVertexbufferHandle);
  594. bgfx::destroy(m_allPropsIndexbufferHandle);
  595. bgfx::destroy(s_texOcclusionDepth);
  596. bgfx::destroy(u_inputRTSize);
  597. bgfx::destroy(u_cullingConfig);
  598. bgfx::destroy(u_color);
  599. delete[] m_allPropVerticesDataCPU;
  600. delete[] m_allPropIndicesDataCPU;
  601. delete[] m_indirectBufferDataCPU;
  602. // Shutdown bgfx.
  603. bgfx::shutdown();
  604. return 0;
  605. }
  606. //renders the occluders to a depth buffer
  607. void renderOcclusionBufferPass()
  608. {
  609. // Setup the occlusion pass projection
  610. bx::mtxProj(m_occlusionProj, 60.0f, float(m_hiZwidth) / float(m_hiZheight), 0.1f, 500.0f, bgfx::getCaps()->homogeneousDepth);
  611. bgfx::setViewTransform(RENDER_PASS_HIZ_ID, m_mainView, m_occlusionProj);
  612. bgfx::setViewFrameBuffer(RENDER_PASS_HIZ_ID, m_hiZDepthBuffer);
  613. bgfx::setViewRect(RENDER_PASS_HIZ_ID, 0, 0, uint16_t(m_hiZwidth), uint16_t(m_hiZheight));
  614. const uint16_t instanceStride = sizeof(InstanceData);
  615. // render all instances of the occluder meshes
  616. for (uint16_t i = 0; i < m_noofProps; i++)
  617. {
  618. Prop& prop = m_props[i];
  619. if (prop.m_renderPass & RenderPass::Occlusion)
  620. {
  621. const uint32_t numInstances = prop.m_noofInstances;
  622. // render instances to the occlusion buffer
  623. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride))
  624. {
  625. bgfx::InstanceDataBuffer instanceBuffer;
  626. bgfx::allocInstanceDataBuffer(&instanceBuffer, numInstances, instanceStride);
  627. InstanceData *data = (InstanceData *) instanceBuffer.data;
  628. for (uint32_t j = 0; j < numInstances; j++)
  629. {
  630. //we only need the world matrix for the occlusion pass
  631. bx::memCopy(data->m_world, prop.m_instances[j].m_world, sizeof(data->m_world));
  632. data++;
  633. }
  634. // Set vertex and index buffer.
  635. bgfx::setVertexBuffer(0, prop.m_vertexbufferHandle);
  636. bgfx::setIndexBuffer(prop.m_indexbufferHandle);
  637. // Set instance data buffer.
  638. bgfx::setInstanceDataBuffer(&instanceBuffer);
  639. // Set render states.
  640. bgfx::setState(BGFX_STATE_DEFAULT);
  641. // Submit primitive for rendering to view.
  642. bgfx::submit(RENDER_PASS_HIZ_ID, m_programOcclusionPass);
  643. }
  644. }
  645. }
  646. }
  647. // downscale the occluder depth buffer to create a mipmap chain
  648. void renderDownscalePass()
  649. {
  650. uint32_t width = m_hiZwidth;
  651. uint32_t height = m_hiZheight;
  652. // copy mip zero over to the hi Z buffer.
  653. // We can't currently use blit as it requires same format and CopyResource is not exposed.
  654. {
  655. float inputRendertargetSize[4] = { (float)width, (float)height, 0.0f, 0.0f };
  656. bgfx::setUniform(u_inputRTSize, inputRendertargetSize);
  657. bgfx::setTexture(0, s_texOcclusionDepth, getTexture(m_hiZDepthBuffer, 0));
  658. bgfx::setImage(1, getTexture(m_hiZBuffer, 0), 0, bgfx::Access::Write);
  659. bgfx::dispatch(RENDER_PASS_HIZ_DOWNSCALE_ID, m_programCopyZ, width/16, height/16);
  660. }
  661. for (uint8_t lod = 1; lod < m_noofHiZMips; ++lod)
  662. {
  663. float inputRendertargetSize[4] = { (float)width, (float)height, 2.0f, 2.0f };
  664. bgfx::setUniform(u_inputRTSize, inputRendertargetSize);
  665. // down scale mip 1 onwards
  666. width /= 2;
  667. height /= 2;
  668. bgfx::setImage(0, getTexture(m_hiZBuffer, 0), lod - 1, bgfx::Access::Read);
  669. bgfx::setImage(1, getTexture(m_hiZBuffer, 0), lod, bgfx::Access::Write);
  670. bgfx::dispatch(RENDER_PASS_HIZ_DOWNSCALE_ID, m_programDownscaleHiZ, width/16, height/16);
  671. }
  672. }
  673. // perform the occlusion using the mip chain
  674. void renderOccludePropsPass()
  675. {
  676. // run the computer shader to determine visibility of each instance
  677. bgfx::setTexture(0, s_texOcclusionDepth, bgfx::getTexture(m_hiZBuffer, 0) );
  678. bgfx::setBuffer(1, m_instanceBoundingBoxes, bgfx::Access::Read);
  679. bgfx::setBuffer(2, m_drawcallInstanceCounts, bgfx::Access::ReadWrite);
  680. bgfx::setBuffer(3, m_instancePredicates, bgfx::Access::Write);
  681. float inputRendertargetSize[4] = { (float)m_hiZwidth, (float)m_hiZheight, 1.0f/ m_hiZwidth, 1.0f/ m_hiZheight };
  682. bgfx::setUniform(u_inputRTSize, inputRendertargetSize);
  683. // store a rounded-up, power of two instance count for the stream compaction step
  684. float noofInstancesPowOf2 = bx::pow(2.0f, bx::floor(bx::log(m_totalInstancesCount) / bx::log(2.0f) ) + 1.0f);
  685. float cullingConfig[4] =
  686. {
  687. (float)m_totalInstancesCount,
  688. noofInstancesPowOf2,
  689. (float)m_noofHiZMips,
  690. (float)m_noofProps
  691. };
  692. bgfx::setUniform(u_cullingConfig, cullingConfig);
  693. //set the view/projection transforms so that the compute shader can receive the viewProjection matrix automagically
  694. bgfx::setViewTransform(RENDER_PASS_OCCLUDE_PROPS_ID, m_mainView, m_occlusionProj);
  695. uint16_t groupX = bx::max<uint16_t>(m_totalInstancesCount / 64 + 1, 1);
  696. bgfx::dispatch(RENDER_PASS_OCCLUDE_PROPS_ID, m_programOccludeProps, groupX, 1, 1);
  697. // perform stream compaction to remove occluded instances
  698. // the per drawcall data that is constant (noof indices/vertices and offsets to vertex/index buffers)
  699. bgfx::setBuffer(0, m_indirectBufferData, bgfx::Access::Read);
  700. // instance data for all instances (pre culling)
  701. bgfx::setBuffer(1, m_instanceBuffer, bgfx::Access::Read);
  702. // per instance visibility (output of culling pass)
  703. bgfx::setBuffer(2, m_instancePredicates, bgfx::Access::Read);
  704. // how many instances per drawcall
  705. bgfx::setBuffer(3, m_drawcallInstanceCounts, bgfx::Access::ReadWrite);
  706. // drawcall data that will drive drawIndirect
  707. bgfx::setBuffer(4, m_indirectBuffer, bgfx::Access::ReadWrite);
  708. // culled instance data
  709. bgfx::setBuffer(5, m_culledInstanceBuffer, bgfx::Access::Write);
  710. bgfx::setUniform(u_cullingConfig, cullingConfig);
  711. bgfx::dispatch(RENDER_PASS_COMPACT_STREAM_ID, m_programStreamCompaction, 1, 1, 1);
  712. }
  713. // render the unoccluded props to the screen
  714. void renderMainPass()
  715. {
  716. // Set view and projection matrix for view 0.
  717. {
  718. bgfx::setViewTransform(RENDER_PASS_MAIN_ID, m_mainView, m_mainProj);
  719. // Set view 0 default viewport.
  720. bgfx::setViewRect(RENDER_PASS_MAIN_ID, 0, 0, uint16_t(m_width), uint16_t(m_height));
  721. }
  722. // Set render states.
  723. bgfx::setState(BGFX_STATE_DEFAULT);
  724. const uint16_t instanceStride = sizeof(InstanceData);
  725. // Set "material" data (currently a color only)
  726. bgfx::setUniform(u_color, &m_materials[0].m_color, m_noofMaterials);
  727. // We can't use indirect drawing for the first frame because the content of m_drawcallInstanceCounts
  728. // is initially undefined.
  729. if (m_useIndirect && !m_firstFrame)
  730. {
  731. // Set vertex and index buffer.
  732. bgfx::setVertexBuffer(0, m_allPropsVertexbufferHandle);
  733. bgfx::setIndexBuffer( m_allPropsIndexbufferHandle);
  734. // Set instance data buffer.
  735. bgfx::setInstanceDataBuffer(m_culledInstanceBuffer, 0, m_totalInstancesCount );
  736. bgfx::submit(RENDER_PASS_MAIN_ID, m_programMainPass, m_indirectBuffer, 0, m_noofProps);
  737. }
  738. else
  739. {
  740. // render all props using regular instancing
  741. for (uint16_t ii = 0; ii < m_noofProps; ++ii)
  742. {
  743. Prop& prop = m_props[ii];
  744. if (prop.m_renderPass & RenderPass::MainPass)
  745. {
  746. const uint32_t numInstances = prop.m_noofInstances;
  747. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride))
  748. {
  749. bgfx::InstanceDataBuffer instanceBuffer;
  750. bgfx::allocInstanceDataBuffer(&instanceBuffer, numInstances, instanceStride);
  751. InstanceData *data = (InstanceData *)instanceBuffer.data;
  752. for (uint32_t jj = 0; jj < numInstances; ++jj)
  753. {
  754. //copy world matrix
  755. bx::memCopy(data->m_world, prop.m_instances[jj].m_world, sizeof(data->m_world) );
  756. //pack the material ID into the world transform
  757. data->m_world[3] = float(prop.m_materialID);
  758. data++;
  759. }
  760. // Set vertex and index buffer.
  761. bgfx::setVertexBuffer(0, prop.m_vertexbufferHandle);
  762. bgfx::setIndexBuffer(prop.m_indexbufferHandle);
  763. // Set instance data buffer.
  764. bgfx::setInstanceDataBuffer(&instanceBuffer);
  765. bgfx::submit(RENDER_PASS_MAIN_ID, m_programMainPass);
  766. }
  767. }
  768. }
  769. }
  770. m_firstFrame = false;
  771. }
  772. bool update() override
  773. {
  774. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  775. {
  776. imguiBeginFrame(m_mouseState.m_mx
  777. , m_mouseState.m_my
  778. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  779. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  780. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  781. , m_mouseState.m_mz
  782. , uint16_t(m_width)
  783. , uint16_t(m_height)
  784. );
  785. showExampleDialog(this);
  786. ImGui::SetNextWindowPos(
  787. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  788. , ImGuiCond_FirstUseEver
  789. );
  790. ImGui::SetNextWindowSize(
  791. ImVec2(m_width / 5.0f, m_height / 6.0f)
  792. , ImGuiCond_FirstUseEver
  793. );
  794. ImGui::Begin("Settings"
  795. , NULL
  796. , 0
  797. );
  798. ImGui::Checkbox("Use Draw Indirect", &m_useIndirect);
  799. ImGui::End();
  800. imguiEndFrame();
  801. // This dummy draw call is here to make sure that view 0 is cleared
  802. // if no other draw calls are submitted to view 0.
  803. bgfx::touch(0);
  804. int64_t now = bx::getHPCounter();
  805. static int64_t last = now;
  806. const int64_t frameTime = now - last;
  807. last = now;
  808. const double freq = double(bx::getHPFrequency());
  809. const float deltaTimeSec = float(double(frameTime) / freq);
  810. // Camera.
  811. const bool mouseOverGui = ImGui::MouseOverArea();
  812. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  813. if (!mouseOverGui)
  814. {
  815. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  816. {
  817. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  818. }
  819. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  820. {
  821. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  822. }
  823. else if (0 != m_mouse.m_scroll)
  824. {
  825. m_camera.dolly(float(m_mouse.m_scroll)*0.05f);
  826. }
  827. }
  828. m_camera.update(deltaTimeSec);
  829. // Get renderer capabilities info.
  830. const bgfx::Caps* caps = bgfx::getCaps();
  831. // Check if instancing is supported.
  832. if (0 == (BGFX_CAPS_INSTANCING & caps->supported) )
  833. {
  834. // When instancing is not supported by GPU, implement alternative
  835. // code path that doesn't use instancing.
  836. float time = (float)((bx::getHPCounter() - m_timeOffset) / double(bx::getHPFrequency()));
  837. bool blink = uint32_t(time*3.0f)&1;
  838. bgfx::dbgTextPrintf(0, 0, blink ? 0x1f : 0x01, " Instancing is not supported by GPU. ");
  839. }
  840. else
  841. {
  842. // calculate main view and project matrices as they are typically reused between passes.
  843. m_camera.mtxLookAt(m_mainView);
  844. bx::mtxProj(m_mainProj, 60.0f, float(m_width) / float(m_height), 0.1f, 500.0f, bgfx::getCaps()->homogeneousDepth);
  845. //submit drawcalls for all passes
  846. renderOcclusionBufferPass();
  847. renderDownscalePass();
  848. renderOccludePropsPass();
  849. renderMainPass();
  850. }
  851. // Advance to next frame. Rendering thread will be kicked to
  852. // process submitted rendering primitives.
  853. bgfx::frame();
  854. return true;
  855. }
  856. return false;
  857. }
  858. entry::MouseState m_mouseState;
  859. uint32_t m_width;
  860. uint32_t m_height;
  861. uint32_t m_hiZwidth;
  862. uint32_t m_hiZheight;
  863. uint32_t m_debug;
  864. uint32_t m_reset;
  865. float m_mainView[16];
  866. float m_mainProj[16];
  867. float m_occlusionProj[16];
  868. bgfx::ProgramHandle m_programMainPass;
  869. bgfx::ProgramHandle m_programOcclusionPass;
  870. bgfx::ProgramHandle m_programCopyZ;
  871. bgfx::ProgramHandle m_programDownscaleHiZ;
  872. bgfx::ProgramHandle m_programOccludeProps;
  873. bgfx::ProgramHandle m_programStreamCompaction;
  874. bgfx::FrameBufferHandle m_hiZDepthBuffer;
  875. bgfx::FrameBufferHandle m_hiZBuffer;
  876. bgfx::IndirectBufferHandle m_indirectBuffer;
  877. bgfx::VertexBufferHandle m_allPropsVertexbufferHandle;
  878. bgfx::IndexBufferHandle m_allPropsIndexbufferHandle;
  879. bgfx::IndexBufferHandle m_indirectBufferData;
  880. PosVertex* m_allPropVerticesDataCPU;
  881. uint16_t* m_allPropIndicesDataCPU;
  882. uint32_t* m_indirectBufferDataCPU;
  883. bgfx::DynamicVertexBufferHandle m_instanceBoundingBoxes;
  884. bgfx::DynamicIndexBufferHandle m_drawcallInstanceCounts;
  885. bgfx::DynamicIndexBufferHandle m_instancePredicates;
  886. bgfx::VertexBufferHandle m_instanceBuffer;
  887. bgfx::DynamicVertexBufferHandle m_culledInstanceBuffer;
  888. bgfx::UniformHandle s_texOcclusionDepth;
  889. bgfx::UniformHandle u_inputRTSize;
  890. bgfx::UniformHandle u_cullingConfig;
  891. bgfx::UniformHandle u_color;
  892. Prop* m_props;
  893. Material* m_materials;
  894. uint16_t m_noofProps;
  895. uint16_t m_noofMaterials;
  896. uint16_t m_totalInstancesCount;
  897. static const uint16_t s_maxNoofProps = 10;
  898. static const uint16_t s_maxNoofInstances = 2048;
  899. int64_t m_timeOffset;
  900. uint8_t m_noofHiZMips;
  901. bool m_useIndirect;
  902. bool m_firstFrame;
  903. Camera m_camera;
  904. Mouse m_mouse;
  905. };
  906. } // namespace
  907. ENTRY_IMPLEMENT_MAIN(
  908. GPUDrivenRendering
  909. , "37-gpudrivenrendering"
  910. , "GPU-Driven Rendering."
  911. , "https://bkaradzic.github.io/bgfx/examples.html#gpudrivenrendering"
  912. );