2
0

gpudrivenrendering.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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::InitNone;
  118. bx::Vec3 dest = bx::InitNone;
  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.platformData.type = entry::getNativeWindowHandleType();
  280. init.resolution.width = m_width;
  281. init.resolution.height = m_height;
  282. init.resolution.reset = m_reset;
  283. bgfx::init(init);
  284. // Enable debug text.
  285. bgfx::setDebug(m_debug);
  286. // Create uniforms and samplers.
  287. u_inputRTSize = bgfx::createUniform("u_inputRTSize", bgfx::UniformType::Vec4);
  288. u_cullingConfig = bgfx::createUniform("u_cullingConfig", bgfx::UniformType::Vec4);
  289. u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4, 32);
  290. s_texOcclusionDepth = bgfx::createUniform("s_texOcclusionDepth", bgfx::UniformType::Sampler);
  291. //create props
  292. {
  293. m_totalInstancesCount = 0;
  294. // Create vertex stream declaration.
  295. PosVertex::init();
  296. m_noofProps = 0;
  297. m_props = new Prop[s_maxNoofProps];
  298. //first create space for some materials
  299. m_materials = new Material[s_maxNoofProps];
  300. m_noofMaterials = 0;
  301. //add a ground plane
  302. {
  303. Prop& prop = m_props[m_noofProps++];
  304. prop.m_renderPass = RenderPass::MainPass;
  305. createCubeMesh(prop);
  306. prop.m_noofInstances = 1;
  307. prop.m_instances = new InstanceData[prop.m_noofInstances];
  308. bx::mtxSRT(prop.m_instances->m_world
  309. , 100.0f, 0.1f, 100.0f
  310. , 0.0f, 0.0f, 0.0f
  311. , 0.0f, 0.0f, 0.0f
  312. );
  313. float temp[4];
  314. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  315. bx::vec4MulMtx(prop.m_instances->m_bboxMin, temp, prop.m_instances->m_world);
  316. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  317. bx::vec4MulMtx(prop.m_instances->m_bboxMax, temp, prop.m_instances->m_world);
  318. prop.m_materialID = m_noofMaterials;
  319. setVector4(m_materials[prop.m_materialID].m_color, 0.0f, 0.6f, 0.0f, 1.0f);
  320. m_noofMaterials++;
  321. m_totalInstancesCount += prop.m_noofInstances;
  322. }
  323. //add a few instances of the occluding mesh
  324. {
  325. Prop& prop = m_props[m_noofProps++];
  326. prop.m_renderPass = RenderPass::All;
  327. //create prop
  328. createCubeMesh(prop);
  329. //add a few instances of the wall mesh
  330. prop.m_noofInstances = 25;
  331. prop.m_instances = new InstanceData[prop.m_noofInstances];
  332. for (int i = 0; i < prop.m_noofInstances; i++)
  333. {
  334. //calculate world position
  335. bx::mtxSRT(prop.m_instances[i].m_world
  336. , 40.0f, 10.0f, 0.1f
  337. , 0.0f, ( rand01() * 120.0f - 60.0f) * 3.1459f / 180.0f, 0.0f
  338. , rand01() * 100.0f - 50.0f, 5.0f, rand01() * 100.0f - 50.0f
  339. );
  340. //calculate bounding box and transform to world space
  341. float temp[4];
  342. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  343. bx::vec4MulMtx(prop.m_instances[i].m_bboxMin, temp, prop.m_instances[i].m_world );
  344. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  345. bx::vec4MulMtx(prop.m_instances[i].m_bboxMax, temp, prop.m_instances[i].m_world );
  346. }
  347. //set the material ID. Will be used in the shader to select the material
  348. prop.m_materialID = m_noofMaterials;
  349. //add a "material" for this prop
  350. setVector4(m_materials[prop.m_materialID].m_color, 0.0f, 0.0f, 1.0f, 0.0f);
  351. m_noofMaterials++;
  352. m_totalInstancesCount += prop.m_noofInstances;
  353. }
  354. //add a few "regular" props
  355. {
  356. //add cubes
  357. {
  358. Prop& prop = m_props[m_noofProps++];
  359. prop.m_renderPass = RenderPass::MainPass;
  360. createCubeMesh(prop);
  361. prop.m_noofInstances = 200;
  362. prop.m_instances = new InstanceData[prop.m_noofInstances];
  363. for (int i = 0; i < prop.m_noofInstances; i++)
  364. {
  365. bx::mtxSRT(prop.m_instances[i].m_world
  366. , 2.0f, 2.0f, 2.0f
  367. , 0.0f, 0.0f, 0.0f
  368. , rand01() * 100.0f - 50.0f, 1.0f, rand01() * 100.0f - 50.0f
  369. );
  370. float temp[4];
  371. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  372. bx::vec4MulMtx(prop.m_instances[i].m_bboxMin, temp, prop.m_instances[i].m_world);
  373. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  374. bx::vec4MulMtx(prop.m_instances[i].m_bboxMax, temp, prop.m_instances[i].m_world);
  375. }
  376. prop.m_materialID = m_noofMaterials;
  377. setVector4(m_materials[prop.m_materialID].m_color, 1.0f, 1.0f, 0.0f, 1.0f);
  378. m_noofMaterials++;
  379. m_totalInstancesCount += prop.m_noofInstances;
  380. }
  381. //add some more cubes
  382. {
  383. Prop& prop = m_props[m_noofProps++];
  384. prop.m_renderPass = RenderPass::MainPass;
  385. createCubeMesh(prop);
  386. prop.m_noofInstances = 300;
  387. prop.m_instances = new InstanceData[prop.m_noofInstances];
  388. for (int i = 0; i < prop.m_noofInstances; i++)
  389. {
  390. bx::mtxSRT(prop.m_instances[i].m_world
  391. , 2.0f, 4.0f, 2.0f
  392. , 0.0f, 0.0f, 0.0f
  393. , rand01() * 100.0f - 50.0f, 2.0f, rand01() * 100.0f - 50.0f
  394. );
  395. float temp[4];
  396. setVector4(temp, -0.5f, -0.5f, -0.5f, 1.0f);
  397. bx::vec4MulMtx(prop.m_instances[i].m_bboxMin, temp, prop.m_instances[i].m_world );
  398. setVector4(temp, 0.5f, 0.5f, 0.5f, 1.0f);
  399. bx::vec4MulMtx(prop.m_instances[i].m_bboxMax, temp, prop.m_instances[i].m_world);
  400. }
  401. prop.m_materialID = m_noofMaterials;
  402. setVector4(m_materials[prop.m_materialID].m_color, 1.0f, 0.0f, 0.0f, 1.0f);
  403. m_noofMaterials++;
  404. m_totalInstancesCount += prop.m_noofInstances;
  405. }
  406. }
  407. }
  408. //Setup Occlusion pass
  409. {
  410. const uint64_t tsFlags = 0
  411. | BGFX_TEXTURE_RT
  412. | BGFX_SAMPLER_MIN_POINT
  413. | BGFX_SAMPLER_MAG_POINT
  414. | BGFX_SAMPLER_MIP_POINT
  415. | BGFX_SAMPLER_U_CLAMP
  416. | BGFX_SAMPLER_V_CLAMP
  417. ;
  418. // Create buffers for the HiZ pass
  419. m_hiZDepthBuffer = bgfx::createFrameBuffer(uint16_t(m_hiZwidth), uint16_t(m_hiZheight), bgfx::TextureFormat::D32F, tsFlags);
  420. bgfx::TextureHandle buffer = bgfx::createTexture2D(uint16_t(m_hiZwidth), uint16_t(m_hiZheight), true, 1, bgfx::TextureFormat::R32F, BGFX_TEXTURE_COMPUTE_WRITE | tsFlags);
  421. m_hiZBuffer = bgfx::createFrameBuffer(1, &buffer, true);
  422. //how many mip will the Hi Z buffer have?
  423. m_noofHiZMips = (uint8_t)(1 + bx::floor(bx::log2(float(bx::max(m_hiZwidth, m_hiZheight) ) ) ) );
  424. // Setup compute shader buffers
  425. //The compute shader will write how many unoccluded instances per drawcall there are here
  426. m_drawcallInstanceCounts = bgfx::createDynamicIndexBuffer(s_maxNoofProps, BGFX_BUFFER_INDEX32 | BGFX_BUFFER_COMPUTE_READ_WRITE);
  427. //the compute shader will write the result of the occlusion test for each instance here
  428. m_instancePredicates = bgfx::createDynamicIndexBuffer(s_maxNoofInstances, BGFX_BUFFER_COMPUTE_READ_WRITE);
  429. //bounding box for each instance, will be fed to the compute shader to calculate occlusion
  430. {
  431. bgfx::VertexLayout computeVertexLayout;
  432. computeVertexLayout.begin()
  433. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  434. .end();
  435. //initialise the buffer with the bounding boxes of all instances
  436. const int sizeOfBuffer = 2 * 4 * m_totalInstancesCount;
  437. float* boundingBoxes = new float[sizeOfBuffer];
  438. float* data = boundingBoxes;
  439. for (uint16_t i = 0; i < m_noofProps; i++)
  440. {
  441. Prop& prop = m_props[i];
  442. const uint32_t numInstances = prop.m_noofInstances;
  443. for (uint32_t j = 0; j < numInstances; j++)
  444. {
  445. bx::memCopy(data, prop.m_instances[j].m_bboxMin, 3 * sizeof(float));
  446. data[3] = (float)i; // store the drawcall ID here to avoid creating a separate buffer
  447. data += 4;
  448. bx::memCopy(data, prop.m_instances[j].m_bboxMax, 3 * sizeof(float));
  449. data += 4;
  450. }
  451. }
  452. const bgfx::Memory* mem = bgfx::makeRef(boundingBoxes, sizeof(float) * sizeOfBuffer);
  453. m_instanceBoundingBoxes = bgfx::createDynamicVertexBuffer(mem, computeVertexLayout, BGFX_BUFFER_COMPUTE_READ);
  454. }
  455. //pre and post occlusion culling instance data buffers
  456. {
  457. bgfx::VertexLayout instanceBufferVertexLayout;
  458. instanceBufferVertexLayout.begin()
  459. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  460. .add(bgfx::Attrib::TexCoord1, 4, bgfx::AttribType::Float)
  461. .add(bgfx::Attrib::TexCoord2, 4, bgfx::AttribType::Float)
  462. .add(bgfx::Attrib::TexCoord3, 4, bgfx::AttribType::Float)
  463. .end();
  464. //initialise the buffer with data for all instances
  465. //Currently we only store a world matrix (16 floats)
  466. const int sizeOfBuffer = 16 * m_totalInstancesCount;
  467. float* instanceData = new float[sizeOfBuffer];
  468. float* data = instanceData;
  469. for (uint16_t ii = 0; ii < m_noofProps; ++ii)
  470. {
  471. Prop& prop = m_props[ii];
  472. const uint32_t numInstances = prop.m_noofInstances;
  473. for (uint32_t jj = 0; jj < numInstances; ++jj)
  474. {
  475. bx::memCopy(data, prop.m_instances[jj].m_world, 16 * sizeof(float) );
  476. data[3] = float(ii); // store the drawcall ID here to avoid creating a separate buffer
  477. data += 16;
  478. }
  479. }
  480. const bgfx::Memory* mem = bgfx::makeRef(instanceData, sizeof(float) * sizeOfBuffer);
  481. //pre occlusion buffer
  482. m_instanceBuffer = bgfx::createVertexBuffer(mem, instanceBufferVertexLayout, BGFX_BUFFER_COMPUTE_READ);
  483. //post occlusion buffer
  484. m_culledInstanceBuffer = bgfx::createDynamicVertexBuffer(4 * m_totalInstancesCount, instanceBufferVertexLayout, BGFX_BUFFER_COMPUTE_WRITE);
  485. }
  486. //we use one "drawcall" per prop to render all its instances
  487. m_indirectBuffer = bgfx::createIndirectBuffer(m_noofProps);
  488. // Create programs from shaders for occlusion pass.
  489. m_programOcclusionPass = loadProgram("vs_gdr_render_occlusion", NULL);
  490. m_programCopyZ = loadProgram("cs_gdr_copy_z", NULL);
  491. m_programDownscaleHiZ = loadProgram("cs_gdr_downscale_hi_z", NULL);
  492. m_programOccludeProps = loadProgram("cs_gdr_occlude_props", NULL);
  493. m_programStreamCompaction = loadProgram("cs_gdr_stream_compaction", NULL);
  494. // Set view RENDER_PASS_HIZ_ID clear state.
  495. bgfx::setViewClear(RENDER_PASS_HIZ_ID
  496. , BGFX_CLEAR_DEPTH
  497. , 0x0
  498. , 1.0f
  499. , 0
  500. );
  501. }
  502. // Setup Main pass
  503. {
  504. // Set view 0 clear state.
  505. bgfx::setViewClear(RENDER_PASS_MAIN_ID
  506. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  507. , 0x303030ff
  508. , 1.0f
  509. , 0
  510. );
  511. // Create program from shaders.
  512. m_programMainPass = loadProgram("vs_gdr_instanced_indirect_rendering", "fs_gdr_instanced_indirect_rendering");
  513. }
  514. // Create static vertex buffer for all props.
  515. // Calculate how many vertices/indices the master buffers will need.
  516. uint16_t totalNoofVertices = 0;
  517. uint16_t totalNoofIndices = 0;
  518. for (uint16_t i = 0; i < m_noofProps; i++)
  519. {
  520. Prop& prop = m_props[i];
  521. totalNoofVertices += prop.m_noofVertices;
  522. totalNoofIndices += prop.m_noofIndices;
  523. }
  524. // CPU data to fill the master buffers
  525. m_allPropVerticesDataCPU = new PosVertex[totalNoofVertices];
  526. m_allPropIndicesDataCPU = new uint16_t[totalNoofIndices];
  527. m_indirectBufferDataCPU = new uint32_t[m_noofProps * 3];
  528. // Copy data over to the master buffers
  529. PosVertex* propVerticesData = m_allPropVerticesDataCPU;
  530. uint16_t* propIndicesData = m_allPropIndicesDataCPU;
  531. uint16_t vertexBufferOffset = 0;
  532. uint16_t indexBufferOffset = 0;
  533. for (uint16_t i = 0; i < m_noofProps; i++)
  534. {
  535. Prop& prop = m_props[i];
  536. bx::memCopy(propVerticesData, prop.m_vertices, prop.m_noofVertices * sizeof(PosVertex));
  537. bx::memCopy(propIndicesData, prop.m_indices, prop.m_noofIndices * sizeof(uint16_t));
  538. propVerticesData += prop.m_noofVertices;
  539. propIndicesData += prop.m_noofIndices;
  540. m_indirectBufferDataCPU[ i * 3 ] = prop.m_noofIndices;
  541. m_indirectBufferDataCPU[ i * 3 + 1] = indexBufferOffset;
  542. m_indirectBufferDataCPU[ i * 3 + 2] = vertexBufferOffset;
  543. indexBufferOffset += prop.m_noofIndices;
  544. vertexBufferOffset += prop.m_noofVertices;
  545. }
  546. // Create master vertex buffer
  547. m_allPropsVertexbufferHandle = bgfx::createVertexBuffer(
  548. bgfx::makeRef(m_allPropVerticesDataCPU, totalNoofVertices * PosVertex::ms_layout.getStride())
  549. , PosVertex::ms_layout
  550. );
  551. // Create master index buffer.
  552. m_allPropsIndexbufferHandle = bgfx::createIndexBuffer(
  553. bgfx::makeRef(m_allPropIndicesDataCPU, totalNoofIndices * sizeof(uint16_t) )
  554. );
  555. // Create buffer with const drawcall data which will be copied to the indirect buffer later.
  556. m_indirectBufferData = bgfx::createIndexBuffer(
  557. bgfx::makeRef(m_indirectBufferDataCPU, m_noofProps * 3 * sizeof(uint32_t)),
  558. BGFX_BUFFER_COMPUTE_READ | BGFX_BUFFER_INDEX32
  559. );
  560. m_timeOffset = bx::getHPCounter();
  561. m_useIndirect = true;
  562. m_firstFrame = true;
  563. imguiCreate();
  564. }
  565. int shutdown() override
  566. {
  567. imguiDestroy();
  568. // Cleanup.
  569. bgfx::destroy(m_programMainPass);
  570. bgfx::destroy(m_programOcclusionPass);
  571. bgfx::destroy(m_programCopyZ);
  572. bgfx::destroy(m_programDownscaleHiZ);
  573. bgfx::destroy(m_programOccludeProps);
  574. bgfx::destroy(m_programStreamCompaction);
  575. for (uint16_t i = 0; i < m_noofProps; i++)
  576. {
  577. Prop& prop = m_props[i];
  578. bgfx::destroy(prop.m_indexbufferHandle);
  579. bgfx::destroy(prop.m_vertexbufferHandle);
  580. delete[] prop.m_indices;
  581. delete[] prop.m_vertices;
  582. delete[] prop.m_instances;
  583. }
  584. delete[] m_props;
  585. bgfx::destroy(m_hiZDepthBuffer);
  586. bgfx::destroy(m_hiZBuffer);
  587. bgfx::destroy(m_indirectBuffer);
  588. bgfx::destroy(m_indirectBufferData);
  589. bgfx::destroy(m_instanceBoundingBoxes);
  590. bgfx::destroy(m_drawcallInstanceCounts);
  591. bgfx::destroy(m_instancePredicates);
  592. bgfx::destroy(m_instanceBuffer);
  593. bgfx::destroy(m_culledInstanceBuffer);
  594. bgfx::destroy(m_allPropsVertexbufferHandle);
  595. bgfx::destroy(m_allPropsIndexbufferHandle);
  596. bgfx::destroy(s_texOcclusionDepth);
  597. bgfx::destroy(u_inputRTSize);
  598. bgfx::destroy(u_cullingConfig);
  599. bgfx::destroy(u_color);
  600. delete[] m_allPropVerticesDataCPU;
  601. delete[] m_allPropIndicesDataCPU;
  602. delete[] m_indirectBufferDataCPU;
  603. // Shutdown bgfx.
  604. bgfx::shutdown();
  605. return 0;
  606. }
  607. //renders the occluders to a depth buffer
  608. void renderOcclusionBufferPass()
  609. {
  610. // Setup the occlusion pass projection
  611. bx::mtxProj(m_occlusionProj, 60.0f, float(m_hiZwidth) / float(m_hiZheight), 0.1f, 500.0f, bgfx::getCaps()->homogeneousDepth);
  612. bgfx::setViewTransform(RENDER_PASS_HIZ_ID, m_mainView, m_occlusionProj);
  613. bgfx::setViewFrameBuffer(RENDER_PASS_HIZ_ID, m_hiZDepthBuffer);
  614. bgfx::setViewRect(RENDER_PASS_HIZ_ID, 0, 0, uint16_t(m_hiZwidth), uint16_t(m_hiZheight));
  615. const uint16_t instanceStride = sizeof(InstanceData);
  616. // render all instances of the occluder meshes
  617. for (uint16_t i = 0; i < m_noofProps; i++)
  618. {
  619. Prop& prop = m_props[i];
  620. if (prop.m_renderPass & RenderPass::Occlusion)
  621. {
  622. const uint32_t numInstances = prop.m_noofInstances;
  623. // render instances to the occlusion buffer
  624. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride))
  625. {
  626. bgfx::InstanceDataBuffer instanceBuffer;
  627. bgfx::allocInstanceDataBuffer(&instanceBuffer, numInstances, instanceStride);
  628. InstanceData *data = (InstanceData *) instanceBuffer.data;
  629. for (uint32_t j = 0; j < numInstances; j++)
  630. {
  631. //we only need the world matrix for the occlusion pass
  632. bx::memCopy(data->m_world, prop.m_instances[j].m_world, sizeof(data->m_world));
  633. data++;
  634. }
  635. // Set vertex and index buffer.
  636. bgfx::setVertexBuffer(0, prop.m_vertexbufferHandle);
  637. bgfx::setIndexBuffer(prop.m_indexbufferHandle);
  638. // Set instance data buffer.
  639. bgfx::setInstanceDataBuffer(&instanceBuffer);
  640. // Set render states.
  641. bgfx::setState(BGFX_STATE_DEFAULT);
  642. // Submit primitive for rendering to view.
  643. bgfx::submit(RENDER_PASS_HIZ_ID, m_programOcclusionPass);
  644. }
  645. }
  646. }
  647. }
  648. // downscale the occluder depth buffer to create a mipmap chain
  649. void renderDownscalePass()
  650. {
  651. uint32_t width = m_hiZwidth;
  652. uint32_t height = m_hiZheight;
  653. // copy mip zero over to the hi Z buffer.
  654. // We can't currently use blit as it requires same format and CopyResource is not exposed.
  655. {
  656. float inputRendertargetSize[4] = { (float)width, (float)height, 0.0f, 0.0f };
  657. bgfx::setUniform(u_inputRTSize, inputRendertargetSize);
  658. bgfx::setTexture(0, s_texOcclusionDepth, getTexture(m_hiZDepthBuffer, 0));
  659. bgfx::setImage(1, getTexture(m_hiZBuffer, 0), 0, bgfx::Access::Write);
  660. bgfx::dispatch(RENDER_PASS_HIZ_DOWNSCALE_ID, m_programCopyZ, width/16, height/16);
  661. }
  662. for (uint8_t lod = 1; lod < m_noofHiZMips; ++lod)
  663. {
  664. float inputRendertargetSize[4] = { (float)width, (float)height, 2.0f, 2.0f };
  665. bgfx::setUniform(u_inputRTSize, inputRendertargetSize);
  666. // down scale mip 1 onwards
  667. width /= 2;
  668. height /= 2;
  669. bgfx::setImage(0, getTexture(m_hiZBuffer, 0), lod - 1, bgfx::Access::Read);
  670. bgfx::setImage(1, getTexture(m_hiZBuffer, 0), lod, bgfx::Access::Write);
  671. bgfx::dispatch(RENDER_PASS_HIZ_DOWNSCALE_ID, m_programDownscaleHiZ, width/16, height/16);
  672. }
  673. }
  674. // perform the occlusion using the mip chain
  675. void renderOccludePropsPass()
  676. {
  677. // run the computer shader to determine visibility of each instance
  678. bgfx::setTexture(0, s_texOcclusionDepth, bgfx::getTexture(m_hiZBuffer, 0) );
  679. bgfx::setBuffer(1, m_instanceBoundingBoxes, bgfx::Access::Read);
  680. bgfx::setBuffer(2, m_drawcallInstanceCounts, bgfx::Access::ReadWrite);
  681. bgfx::setBuffer(3, m_instancePredicates, bgfx::Access::Write);
  682. float inputRendertargetSize[4] = { (float)m_hiZwidth, (float)m_hiZheight, 1.0f/ m_hiZwidth, 1.0f/ m_hiZheight };
  683. bgfx::setUniform(u_inputRTSize, inputRendertargetSize);
  684. // store a rounded-up, power of two instance count for the stream compaction step
  685. float noofInstancesPowOf2 = bx::pow(2.0f, bx::floor(bx::log(m_totalInstancesCount) / bx::log(2.0f) ) + 1.0f);
  686. float cullingConfig[4] =
  687. {
  688. (float)m_totalInstancesCount,
  689. noofInstancesPowOf2,
  690. (float)m_noofHiZMips,
  691. (float)m_noofProps
  692. };
  693. bgfx::setUniform(u_cullingConfig, cullingConfig);
  694. //set the view/projection transforms so that the compute shader can receive the viewProjection matrix automagically
  695. bgfx::setViewTransform(RENDER_PASS_OCCLUDE_PROPS_ID, m_mainView, m_occlusionProj);
  696. uint16_t groupX = bx::max<uint16_t>(m_totalInstancesCount / 64 + 1, 1);
  697. bgfx::dispatch(RENDER_PASS_OCCLUDE_PROPS_ID, m_programOccludeProps, groupX, 1, 1);
  698. // perform stream compaction to remove occluded instances
  699. // the per drawcall data that is constant (noof indices/vertices and offsets to vertex/index buffers)
  700. bgfx::setBuffer(0, m_indirectBufferData, bgfx::Access::Read);
  701. // instance data for all instances (pre culling)
  702. bgfx::setBuffer(1, m_instanceBuffer, bgfx::Access::Read);
  703. // per instance visibility (output of culling pass)
  704. bgfx::setBuffer(2, m_instancePredicates, bgfx::Access::Read);
  705. // how many instances per drawcall
  706. bgfx::setBuffer(3, m_drawcallInstanceCounts, bgfx::Access::ReadWrite);
  707. // drawcall data that will drive drawIndirect
  708. bgfx::setBuffer(4, m_indirectBuffer, bgfx::Access::ReadWrite);
  709. // culled instance data
  710. bgfx::setBuffer(5, m_culledInstanceBuffer, bgfx::Access::Write);
  711. bgfx::setUniform(u_cullingConfig, cullingConfig);
  712. bgfx::dispatch(RENDER_PASS_COMPACT_STREAM_ID, m_programStreamCompaction, 1, 1, 1);
  713. }
  714. // render the unoccluded props to the screen
  715. void renderMainPass()
  716. {
  717. // Set view and projection matrix for view 0.
  718. {
  719. bgfx::setViewTransform(RENDER_PASS_MAIN_ID, m_mainView, m_mainProj);
  720. // Set view 0 default viewport.
  721. bgfx::setViewRect(RENDER_PASS_MAIN_ID, 0, 0, uint16_t(m_width), uint16_t(m_height));
  722. }
  723. // Set render states.
  724. bgfx::setState(BGFX_STATE_DEFAULT);
  725. const uint16_t instanceStride = sizeof(InstanceData);
  726. // Set "material" data (currently a color only)
  727. bgfx::setUniform(u_color, &m_materials[0].m_color, m_noofMaterials);
  728. // We can't use indirect drawing for the first frame because the content of m_drawcallInstanceCounts
  729. // is initially undefined.
  730. if (m_useIndirect && !m_firstFrame)
  731. {
  732. // Set vertex and index buffer.
  733. bgfx::setVertexBuffer(0, m_allPropsVertexbufferHandle);
  734. bgfx::setIndexBuffer( m_allPropsIndexbufferHandle);
  735. // Set instance data buffer.
  736. bgfx::setInstanceDataBuffer(m_culledInstanceBuffer, 0, m_totalInstancesCount );
  737. bgfx::submit(RENDER_PASS_MAIN_ID, m_programMainPass, m_indirectBuffer, 0, m_noofProps);
  738. }
  739. else
  740. {
  741. // render all props using regular instancing
  742. for (uint16_t ii = 0; ii < m_noofProps; ++ii)
  743. {
  744. Prop& prop = m_props[ii];
  745. if (prop.m_renderPass & RenderPass::MainPass)
  746. {
  747. const uint32_t numInstances = prop.m_noofInstances;
  748. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride))
  749. {
  750. bgfx::InstanceDataBuffer instanceBuffer;
  751. bgfx::allocInstanceDataBuffer(&instanceBuffer, numInstances, instanceStride);
  752. InstanceData *data = (InstanceData *)instanceBuffer.data;
  753. for (uint32_t jj = 0; jj < numInstances; ++jj)
  754. {
  755. //copy world matrix
  756. bx::memCopy(data->m_world, prop.m_instances[jj].m_world, sizeof(data->m_world) );
  757. //pack the material ID into the world transform
  758. data->m_world[3] = float(prop.m_materialID);
  759. data++;
  760. }
  761. // Set vertex and index buffer.
  762. bgfx::setVertexBuffer(0, prop.m_vertexbufferHandle);
  763. bgfx::setIndexBuffer(prop.m_indexbufferHandle);
  764. // Set instance data buffer.
  765. bgfx::setInstanceDataBuffer(&instanceBuffer);
  766. bgfx::submit(RENDER_PASS_MAIN_ID, m_programMainPass);
  767. }
  768. }
  769. }
  770. }
  771. m_firstFrame = false;
  772. }
  773. bool update() override
  774. {
  775. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  776. {
  777. imguiBeginFrame(m_mouseState.m_mx
  778. , m_mouseState.m_my
  779. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  780. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  781. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  782. , m_mouseState.m_mz
  783. , uint16_t(m_width)
  784. , uint16_t(m_height)
  785. );
  786. showExampleDialog(this);
  787. ImGui::SetNextWindowPos(
  788. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  789. , ImGuiCond_FirstUseEver
  790. );
  791. ImGui::SetNextWindowSize(
  792. ImVec2(m_width / 5.0f, m_height / 6.0f)
  793. , ImGuiCond_FirstUseEver
  794. );
  795. ImGui::Begin("Settings"
  796. , NULL
  797. , 0
  798. );
  799. ImGui::Checkbox("Use Draw Indirect", &m_useIndirect);
  800. ImGui::End();
  801. imguiEndFrame();
  802. // This dummy draw call is here to make sure that view 0 is cleared
  803. // if no other draw calls are submitted to view 0.
  804. bgfx::touch(0);
  805. int64_t now = bx::getHPCounter();
  806. static int64_t last = now;
  807. const int64_t frameTime = now - last;
  808. last = now;
  809. const double freq = double(bx::getHPFrequency());
  810. const float deltaTimeSec = float(double(frameTime) / freq);
  811. // Camera.
  812. const bool mouseOverGui = ImGui::MouseOverArea();
  813. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  814. if (!mouseOverGui)
  815. {
  816. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  817. {
  818. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  819. }
  820. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  821. {
  822. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  823. }
  824. else if (0 != m_mouse.m_scroll)
  825. {
  826. m_camera.dolly(float(m_mouse.m_scroll)*0.05f);
  827. }
  828. }
  829. m_camera.update(deltaTimeSec);
  830. // Get renderer capabilities info.
  831. const bgfx::Caps* caps = bgfx::getCaps();
  832. // Check if instancing is supported.
  833. if (0 == (BGFX_CAPS_INSTANCING & caps->supported) )
  834. {
  835. // When instancing is not supported by GPU, implement alternative
  836. // code path that doesn't use instancing.
  837. float time = (float)((bx::getHPCounter() - m_timeOffset) / double(bx::getHPFrequency()));
  838. bool blink = uint32_t(time*3.0f)&1;
  839. bgfx::dbgTextPrintf(0, 0, blink ? 0x1f : 0x01, " Instancing is not supported by GPU. ");
  840. }
  841. else
  842. {
  843. // calculate main view and project matrices as they are typically reused between passes.
  844. m_camera.mtxLookAt(m_mainView);
  845. bx::mtxProj(m_mainProj, 60.0f, float(m_width) / float(m_height), 0.1f, 500.0f, bgfx::getCaps()->homogeneousDepth);
  846. //submit drawcalls for all passes
  847. renderOcclusionBufferPass();
  848. renderDownscalePass();
  849. renderOccludePropsPass();
  850. renderMainPass();
  851. }
  852. // Advance to next frame. Rendering thread will be kicked to
  853. // process submitted rendering primitives.
  854. bgfx::frame();
  855. return true;
  856. }
  857. return false;
  858. }
  859. entry::MouseState m_mouseState;
  860. uint32_t m_width;
  861. uint32_t m_height;
  862. uint32_t m_hiZwidth;
  863. uint32_t m_hiZheight;
  864. uint32_t m_debug;
  865. uint32_t m_reset;
  866. float m_mainView[16];
  867. float m_mainProj[16];
  868. float m_occlusionProj[16];
  869. bgfx::ProgramHandle m_programMainPass;
  870. bgfx::ProgramHandle m_programOcclusionPass;
  871. bgfx::ProgramHandle m_programCopyZ;
  872. bgfx::ProgramHandle m_programDownscaleHiZ;
  873. bgfx::ProgramHandle m_programOccludeProps;
  874. bgfx::ProgramHandle m_programStreamCompaction;
  875. bgfx::FrameBufferHandle m_hiZDepthBuffer;
  876. bgfx::FrameBufferHandle m_hiZBuffer;
  877. bgfx::IndirectBufferHandle m_indirectBuffer;
  878. bgfx::VertexBufferHandle m_allPropsVertexbufferHandle;
  879. bgfx::IndexBufferHandle m_allPropsIndexbufferHandle;
  880. bgfx::IndexBufferHandle m_indirectBufferData;
  881. PosVertex* m_allPropVerticesDataCPU;
  882. uint16_t* m_allPropIndicesDataCPU;
  883. uint32_t* m_indirectBufferDataCPU;
  884. bgfx::DynamicVertexBufferHandle m_instanceBoundingBoxes;
  885. bgfx::DynamicIndexBufferHandle m_drawcallInstanceCounts;
  886. bgfx::DynamicIndexBufferHandle m_instancePredicates;
  887. bgfx::VertexBufferHandle m_instanceBuffer;
  888. bgfx::DynamicVertexBufferHandle m_culledInstanceBuffer;
  889. bgfx::UniformHandle s_texOcclusionDepth;
  890. bgfx::UniformHandle u_inputRTSize;
  891. bgfx::UniformHandle u_cullingConfig;
  892. bgfx::UniformHandle u_color;
  893. Prop* m_props;
  894. Material* m_materials;
  895. uint16_t m_noofProps;
  896. uint16_t m_noofMaterials;
  897. uint16_t m_totalInstancesCount;
  898. static const uint16_t s_maxNoofProps = 10;
  899. static const uint16_t s_maxNoofInstances = 2048;
  900. int64_t m_timeOffset;
  901. uint8_t m_noofHiZMips;
  902. bool m_useIndirect;
  903. bool m_firstFrame;
  904. Camera m_camera;
  905. Mouse m_mouse;
  906. };
  907. } // namespace
  908. ENTRY_IMPLEMENT_MAIN(
  909. GPUDrivenRendering
  910. , "37-gpudrivenrendering"
  911. , "GPU-Driven Rendering."
  912. , "https://bkaradzic.github.io/bgfx/examples.html#gpudrivenrendering"
  913. );