reflectiveshadowmap.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright 2016 Joseph Cherlin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <common.h>
  6. #include <camera.h>
  7. #include <bgfx_utils.h>
  8. #include <imgui/imgui.h>
  9. #include <bx/rng.h>
  10. namespace
  11. {
  12. /*
  13. * Intro
  14. * =====
  15. *
  16. * RSM (reflective shadow map) is a technique for global illumination.
  17. * It is similar to shadow map. It piggybacks on the shadow map, in fact.
  18. *
  19. * RSM is compatible with any type of lighting which can handle handle
  20. * a lot of point lights. This sample happens to use a deferred renderer,
  21. * but other types would work.
  22. *
  23. * Overview:
  24. *
  25. * - Draw into G-Buffer
  26. * - Draw Shadow Map (with RSM piggybacked on)
  27. * - Populate light buffer
  28. * - Deferred "combine" pass.
  29. *
  30. * Details
  31. * =======
  32. *
  33. * ## G-Buffer
  34. *
  35. * Typical G-Buffer with normals, color, depth.
  36. *
  37. * ## RSM
  38. *
  39. * A typical shadow map, except it also outputs to a "RSM" buffer.
  40. * The RSM contains the color of the item drawn, as well as a scalar value which represents
  41. * how much light would bounce off of the surface if it were hit with light from the origin
  42. * of the shadow map.
  43. *
  44. * ## Light Buffer
  45. *
  46. * We draw a lot of spheres into the light buffer. These spheres are called VPL (virtual
  47. * point lights). VPLs represent bounced light, and let us eliminate the classic "ambient"
  48. * term. Instead of us supplying their world space position in a transform matrix,
  49. * VPLs gain their position from the shadow map from step 2, using an unprojection. They gain
  50. * their color from the RSM. You could also store their position in a buffer while drawing shadows,
  51. * I'm just using depth to keep the sample smaller.
  52. *
  53. * ## Deferred combine
  54. *
  55. * Typical combine used in almost any sort of deferred renderer.
  56. *
  57. * References
  58. * ==========
  59. *
  60. * http: *www.bpeers.com/blog/?itemid=517
  61. *
  62. */
  63. // Render passes
  64. #define RENDER_PASS_GBUFFER 0 // GBuffer for normals and albedo
  65. #define RENDER_PASS_SHADOW_MAP 1 // Draw into the shadow map (RSM and regular shadow map at same time)
  66. #define RENDER_PASS_LIGHT_BUFFER 2 // Light buffer for point lights
  67. #define RENDER_PASS_COMBINE 3 // Directional light and final result
  68. // Gbuffer has multiple render targets
  69. #define GBUFFER_RT_NORMAL 0
  70. #define GBUFFER_RT_COLOR 1
  71. #define GBUFFER_RT_DEPTH 2
  72. // Shadow map has multiple render targets
  73. #define SHADOW_RT_RSM 0 // In this algorithm, shadows write lighting info as well.
  74. #define SHADOW_RT_DEPTH 1 // Shadow maps always write a depth
  75. // Random meshes we draw
  76. #define MODEL_COUNT 222 // In this demo, a model is a mesh plus a transform and a color
  77. #define SHADOW_MAP_DIM 512
  78. #define LIGHT_DIST 10.0f
  79. static const char * s_meshPaths[] =
  80. {
  81. "meshes/cube.bin",
  82. "meshes/orb.bin",
  83. "meshes/column.bin",
  84. "meshes/bunny.bin",
  85. "meshes/tree.bin",
  86. "meshes/hollowcube.bin"
  87. };
  88. static const float s_meshScale[] =
  89. {
  90. 0.25f,
  91. 0.5f,
  92. 0.05f,
  93. 0.5f,
  94. 0.05f,
  95. 0.05f
  96. };
  97. // Vertex layout for our screen space quad (used in deferred rendering)
  98. struct PosTexCoord0Vertex
  99. {
  100. float m_x;
  101. float m_y;
  102. float m_z;
  103. float m_u;
  104. float m_v;
  105. static void init()
  106. {
  107. ms_layout
  108. .begin()
  109. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  110. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  111. .end();
  112. }
  113. static bgfx::VertexLayout ms_layout;
  114. };
  115. bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
  116. // Utility function to draw a screen space quad for deferred rendering
  117. void screenSpaceQuad(bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
  118. {
  119. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
  120. {
  121. bgfx::TransientVertexBuffer vb;
  122. bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
  123. PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
  124. const float minx = -_width;
  125. const float maxx = _width;
  126. const float miny = 0.0f;
  127. const float maxy = _height*2.0f;
  128. const float minu = -1.0f;
  129. const float maxu = 1.0f;
  130. const float zz = 0.0f;
  131. float minv = 0.0f;
  132. float maxv = 2.0f;
  133. if (_originBottomLeft)
  134. {
  135. float temp = minv;
  136. minv = maxv;
  137. maxv = temp;
  138. minv -= 1.0f;
  139. maxv -= 1.0f;
  140. }
  141. vertex[0].m_x = minx;
  142. vertex[0].m_y = miny;
  143. vertex[0].m_z = zz;
  144. vertex[0].m_u = minu;
  145. vertex[0].m_v = minv;
  146. vertex[1].m_x = maxx;
  147. vertex[1].m_y = miny;
  148. vertex[1].m_z = zz;
  149. vertex[1].m_u = maxu;
  150. vertex[1].m_v = minv;
  151. vertex[2].m_x = maxx;
  152. vertex[2].m_y = maxy;
  153. vertex[2].m_z = zz;
  154. vertex[2].m_u = maxu;
  155. vertex[2].m_v = maxv;
  156. bgfx::setVertexBuffer(0, &vb);
  157. }
  158. }
  159. class ExampleRSM : public entry::AppI
  160. {
  161. public:
  162. ExampleRSM(const char* _name, const char* _description, const char* _url)
  163. : entry::AppI(_name, _description, _url)
  164. , m_reading(0)
  165. , m_currFrame(UINT32_MAX)
  166. , m_cameraSpin(false)
  167. , m_lightElevation(35.0f)
  168. , m_lightAzimuth(215.0f)
  169. , m_rsmAmount(0.25f)
  170. , m_vplRadius(3.0f)
  171. {
  172. }
  173. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  174. {
  175. Args args(_argc, _argv);
  176. m_width = _width;
  177. m_height = _height;
  178. m_debug = BGFX_DEBUG_NONE;
  179. m_reset = BGFX_RESET_VSYNC;
  180. bgfx::Init init;
  181. init.type = args.m_type;
  182. init.vendorId = args.m_pciId;
  183. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  184. init.platformData.ndt = entry::getNativeDisplayHandle();
  185. init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle);
  186. init.resolution.width = m_width;
  187. init.resolution.height = m_height;
  188. init.resolution.reset = m_reset;
  189. bgfx::init(init);
  190. // Enable debug text.
  191. bgfx::setDebug(m_debug);
  192. // Labeling for renderdoc captures, etc
  193. bgfx::setViewName(RENDER_PASS_GBUFFER, "gbuffer" );
  194. bgfx::setViewName(RENDER_PASS_SHADOW_MAP, "shadow map" );
  195. bgfx::setViewName(RENDER_PASS_LIGHT_BUFFER, "light buffer");
  196. bgfx::setViewName(RENDER_PASS_COMBINE, "post combine");
  197. // Set up screen clears
  198. bgfx::setViewClear(RENDER_PASS_GBUFFER
  199. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  200. , 0
  201. , 1.0f
  202. , 0
  203. );
  204. bgfx::setViewClear(RENDER_PASS_LIGHT_BUFFER
  205. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  206. , 0
  207. , 1.0f
  208. , 0
  209. );
  210. bgfx::setViewClear(RENDER_PASS_SHADOW_MAP
  211. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  212. , 0
  213. , 1.0f
  214. , 0
  215. );
  216. // Create uniforms
  217. u_tint = bgfx::createUniform("u_tint", bgfx::UniformType::Vec4); // Tint for when you click on items
  218. u_lightDir = bgfx::createUniform("u_lightDir", bgfx::UniformType::Vec4); // Single directional light for entire scene
  219. u_sphereInfo = bgfx::createUniform("u_sphereInfo", bgfx::UniformType::Vec4); // Info for RSM
  220. u_invMvp = bgfx::createUniform("u_invMvp", bgfx::UniformType::Mat4); // Matrix needed in light buffer
  221. u_invMvpShadow = bgfx::createUniform("u_invMvpShadow", bgfx::UniformType::Mat4); // Matrix needed in light buffer
  222. u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4); // Matrix needed to use shadow map (world to shadow space)
  223. u_shadowDimsInv = bgfx::createUniform("u_shadowDimsInv", bgfx::UniformType::Vec4); // Used in PCF
  224. u_rsmAmount = bgfx::createUniform("u_rsmAmount", bgfx::UniformType::Vec4); // How much RSM to use vs directional light
  225. // Create texture sampler uniforms (used when we bind textures)
  226. s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler); // Normal gbuffer
  227. s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler); // Normal gbuffer
  228. s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler); // Color (albedo) gbuffer
  229. s_light = bgfx::createUniform("s_light", bgfx::UniformType::Sampler); // Light buffer
  230. s_shadowMap = bgfx::createUniform("s_shadowMap", bgfx::UniformType::Sampler); // Shadow map
  231. s_rsm = bgfx::createUniform("s_rsm", bgfx::UniformType::Sampler); // Reflective shadow map
  232. // Create program from shaders.
  233. m_gbufferProgram = loadProgram("vs_rsm_gbuffer", "fs_rsm_gbuffer"); // Gbuffer
  234. m_shadowProgram = loadProgram("vs_rsm_shadow", "fs_rsm_shadow" ); // Drawing shadow map
  235. m_lightProgram = loadProgram("vs_rsm_lbuffer", "fs_rsm_lbuffer"); // Light buffer
  236. m_combineProgram = loadProgram("vs_rsm_combine", "fs_rsm_combine"); // Combiner
  237. // Load some meshes
  238. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  239. {
  240. m_meshes[ii] = meshLoad(s_meshPaths[ii]);
  241. }
  242. // Randomly create some models
  243. bx::RngMwc mwc; // Random number generator
  244. for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
  245. {
  246. Model& model = m_models[ii];
  247. uint32_t rr = mwc.gen() % 256;
  248. uint32_t gg = mwc.gen() % 256;
  249. uint32_t bb = mwc.gen() % 256;
  250. model.mesh = 1+mwc.gen()%(BX_COUNTOF(s_meshPaths)-1);
  251. model.color[0] = rr/255.0f;
  252. model.color[1] = gg/255.0f;
  253. model.color[2] = bb/255.0f;
  254. model.color[3] = 1.0f;
  255. model.position[0] = (((mwc.gen() % 256)) - 128.0f)/20.0f;
  256. model.position[1] = 0;
  257. model.position[2] = (((mwc.gen() % 256)) - 128.0f)/20.0f;
  258. }
  259. // Load ground. We'll just use the cube since I don't have a ground model right now
  260. m_ground = meshLoad("meshes/cube.bin");
  261. // Light sphere
  262. m_lightSphere = meshLoad("meshes/unit_sphere.bin");
  263. const uint64_t tsFlags = 0
  264. | BGFX_TEXTURE_RT
  265. | BGFX_SAMPLER_MIN_POINT
  266. | BGFX_SAMPLER_MAG_POINT
  267. | BGFX_SAMPLER_MIP_POINT
  268. | BGFX_SAMPLER_U_CLAMP
  269. | BGFX_SAMPLER_V_CLAMP
  270. ;
  271. m_gbufferTex[GBUFFER_RT_NORMAL] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
  272. m_gbufferTex[GBUFFER_RT_COLOR] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
  273. m_gbufferTex[GBUFFER_RT_DEPTH] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::D32F, tsFlags);
  274. m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
  275. // Make light buffer
  276. m_lightBufferTex = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
  277. bgfx::TextureHandle lightBufferRTs[] = {
  278. m_lightBufferTex
  279. };
  280. m_lightBuffer = bgfx::createFrameBuffer(BX_COUNTOF(lightBufferRTs), lightBufferRTs, true);
  281. // Make shadow buffer
  282. const uint64_t rsmFlags = 0
  283. | BGFX_TEXTURE_RT
  284. | BGFX_SAMPLER_MIN_POINT
  285. | BGFX_SAMPLER_MAG_POINT
  286. | BGFX_SAMPLER_MIP_POINT
  287. | BGFX_SAMPLER_U_CLAMP
  288. | BGFX_SAMPLER_V_CLAMP
  289. ;
  290. // Reflective shadow map
  291. m_shadowBufferTex[SHADOW_RT_RSM] = bgfx::createTexture2D(
  292. SHADOW_MAP_DIM
  293. , SHADOW_MAP_DIM
  294. , false
  295. , 1
  296. , bgfx::TextureFormat::BGRA8
  297. , rsmFlags
  298. );
  299. // Typical shadow map
  300. m_shadowBufferTex[SHADOW_RT_DEPTH] = bgfx::createTexture2D(
  301. SHADOW_MAP_DIM
  302. , SHADOW_MAP_DIM
  303. , false
  304. , 1
  305. , bgfx::TextureFormat::D16
  306. , BGFX_TEXTURE_RT /* | BGFX_SAMPLER_COMPARE_LEQUAL*/
  307. ); // Note I'm not setting BGFX_SAMPLER_COMPARE_LEQUAL. Why?
  308. // Normally a PCF shadow map such as this requires a compare. However, this sample also
  309. // reads from this texture in the lighting pass, and only uses the PCF capabilities in
  310. // the combine pass, so the flag is disabled by default.
  311. m_shadowBuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_shadowBufferTex), m_shadowBufferTex, true);
  312. // Vertex layout
  313. PosTexCoord0Vertex::init();
  314. // Init camera
  315. cameraCreate();
  316. cameraSetPosition({0.0f, 1.5f, 0.0f});
  317. cameraSetVerticalAngle(-0.3f);
  318. // Init directional light
  319. updateLightDir();
  320. // Get renderer capabilities info.
  321. m_caps = bgfx::getCaps();
  322. imguiCreate();
  323. }
  324. int shutdown() override
  325. {
  326. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  327. {
  328. meshUnload(m_meshes[ii]);
  329. }
  330. meshUnload(m_ground);
  331. meshUnload(m_lightSphere);
  332. // Cleanup.
  333. bgfx::destroy(m_gbufferProgram);
  334. bgfx::destroy(m_lightProgram);
  335. bgfx::destroy(m_combineProgram);
  336. bgfx::destroy(m_shadowProgram);
  337. bgfx::destroy(u_tint);
  338. bgfx::destroy(u_lightDir);
  339. bgfx::destroy(u_sphereInfo);
  340. bgfx::destroy(u_invMvp);
  341. bgfx::destroy(u_invMvpShadow);
  342. bgfx::destroy(u_lightMtx);
  343. bgfx::destroy(u_shadowDimsInv);
  344. bgfx::destroy(u_rsmAmount);
  345. bgfx::destroy(s_normal);
  346. bgfx::destroy(s_depth);
  347. bgfx::destroy(s_light);
  348. bgfx::destroy(s_color);
  349. bgfx::destroy(s_shadowMap);
  350. bgfx::destroy(s_rsm);
  351. bgfx::destroy(m_gbuffer);
  352. bgfx::destroy(m_lightBuffer);
  353. bgfx::destroy(m_shadowBuffer);
  354. for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
  355. {
  356. bgfx::destroy(m_gbufferTex[ii]);
  357. }
  358. bgfx::destroy(m_lightBufferTex);
  359. for (uint32_t ii = 0; ii < BX_COUNTOF(m_shadowBufferTex); ++ii)
  360. {
  361. bgfx::destroy(m_shadowBufferTex[ii]);
  362. }
  363. cameraDestroy();
  364. imguiDestroy();
  365. // Shutdown bgfx.
  366. bgfx::shutdown();
  367. return 0;
  368. }
  369. bool update() override
  370. {
  371. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  372. {
  373. // Update frame timer
  374. int64_t now = bx::getHPCounter();
  375. static int64_t last = now;
  376. const int64_t frameTime = now - last;
  377. last = now;
  378. const double freq = double(bx::getHPFrequency());
  379. const float deltaTime = float(frameTime/freq);
  380. // Update camera
  381. cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
  382. // Set up matrices for gbuffer
  383. float view[16];
  384. cameraGetViewMtx(view);
  385. float proj[16];
  386. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  387. bgfx::setViewRect(RENDER_PASS_GBUFFER, 0, 0, uint16_t(m_width), uint16_t(m_height));
  388. bgfx::setViewTransform(RENDER_PASS_GBUFFER, view, proj);
  389. // Make sure when we draw it goes into gbuffer and not backbuffer
  390. bgfx::setViewFrameBuffer(RENDER_PASS_GBUFFER, m_gbuffer);
  391. // Draw everything into g-buffer
  392. drawAllModels(RENDER_PASS_GBUFFER, m_gbufferProgram);
  393. // Draw shadow map
  394. // Set up transforms for shadow map
  395. float smView[16], smProj[16], lightEye[3], lightAt[3];
  396. lightEye[0] = m_lightDir[0]*LIGHT_DIST;
  397. lightEye[1] = m_lightDir[1]*LIGHT_DIST;
  398. lightEye[2] = m_lightDir[2]*LIGHT_DIST;
  399. lightAt[0] = 0.0f;
  400. lightAt[1] = 0.0f;
  401. lightAt[2] = 0.0f;
  402. bx::mtxLookAt(smView, bx::load<bx::Vec3>(lightEye), bx::load<bx::Vec3>(lightAt) );
  403. const float area = 10.0f;
  404. const bgfx::Caps* caps = bgfx::getCaps();
  405. bx::mtxOrtho(smProj, -area, area, -area, area, -100.0f, 100.0f, 0.0f, caps->homogeneousDepth);
  406. bgfx::setViewTransform(RENDER_PASS_SHADOW_MAP, smView, smProj);
  407. bgfx::setViewFrameBuffer(RENDER_PASS_SHADOW_MAP, m_shadowBuffer);
  408. bgfx::setViewRect(RENDER_PASS_SHADOW_MAP, 0, 0, SHADOW_MAP_DIM, SHADOW_MAP_DIM);
  409. drawAllModels(RENDER_PASS_SHADOW_MAP, m_shadowProgram);
  410. // Next draw light buffer
  411. // Set up matrices for light buffer
  412. bgfx::setViewRect(RENDER_PASS_LIGHT_BUFFER, 0, 0, uint16_t(m_width), uint16_t(m_height));
  413. bgfx::setViewTransform(RENDER_PASS_LIGHT_BUFFER, view, proj); // Notice, same view and proj as gbuffer
  414. // Set drawing into light buffer
  415. bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_BUFFER, m_lightBuffer);
  416. // Inverse view projection is needed in shader so set that up
  417. float vp[16], invMvp[16];
  418. bx::mtxMul(vp, view, proj);
  419. bx::mtxInverse(invMvp, vp);
  420. // Light matrix used in combine pass and inverse used in light pass
  421. float lightMtx[16]; // World space to light space (shadow map space)
  422. bx::mtxMul(lightMtx, smView, smProj);
  423. float invMvpShadow[16];
  424. bx::mtxInverse(invMvpShadow, lightMtx);
  425. // Draw some lights (these should really be instanced but for this example they aren't...)
  426. const uint32_t kMaxSpheres = 32;
  427. for (uint32_t i = 0; i < kMaxSpheres; i++)
  428. {
  429. for (uint32_t j = 0; j < kMaxSpheres; j++)
  430. {
  431. // These are used in the fragment shader
  432. bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL) ); // Normal for lighting calculations
  433. bgfx::setTexture(1, s_depth, bgfx::getTexture(m_gbuffer, GBUFFER_RT_DEPTH) ); // Depth to reconstruct world position
  434. // Thse are used in the vert shader
  435. bgfx::setTexture(2, s_shadowMap, bgfx::getTexture(m_shadowBuffer, SHADOW_RT_DEPTH) ); // Used to place sphere
  436. bgfx::setTexture(3, s_rsm, bgfx::getTexture(m_shadowBuffer, SHADOW_RT_RSM) ); // Used to scale/color sphere
  437. bgfx::setUniform(u_invMvp, invMvp);
  438. bgfx::setUniform(u_invMvpShadow, invMvpShadow);
  439. float sphereInfo[4];
  440. sphereInfo[0] = ((float)i/(kMaxSpheres-1));
  441. sphereInfo[1] = ((float)j/(kMaxSpheres-1));
  442. sphereInfo[2] = m_vplRadius;
  443. sphereInfo[3] = 0.0; // Unused
  444. bgfx::setUniform(u_sphereInfo, sphereInfo);
  445. const uint64_t lightDrawState = 0
  446. | BGFX_STATE_WRITE_RGB
  447. | BGFX_STATE_BLEND_ADD // <=== Overlapping lights contribute more
  448. | BGFX_STATE_WRITE_A
  449. | BGFX_STATE_CULL_CW // <=== If we go into the lights, there will be problems, so we draw the far back face.
  450. ;
  451. meshSubmit(
  452. m_lightSphere
  453. , RENDER_PASS_LIGHT_BUFFER
  454. , m_lightProgram
  455. , NULL
  456. , lightDrawState
  457. );
  458. }
  459. }
  460. // Draw combine pass
  461. // Texture inputs for combine pass
  462. bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL) );
  463. bgfx::setTexture(1, s_color, bgfx::getTexture(m_gbuffer, GBUFFER_RT_COLOR) );
  464. bgfx::setTexture(2, s_light, bgfx::getTexture(m_lightBuffer, 0) );
  465. bgfx::setTexture(3, s_depth, bgfx::getTexture(m_gbuffer, GBUFFER_RT_DEPTH) );
  466. bgfx::setTexture(4, s_shadowMap, bgfx::getTexture(m_shadowBuffer, SHADOW_RT_DEPTH)
  467. , BGFX_SAMPLER_COMPARE_LEQUAL
  468. );
  469. // Uniforms for combine pass
  470. bgfx::setUniform(u_lightDir, m_lightDir);
  471. bgfx::setUniform(u_invMvp, invMvp);
  472. bgfx::setUniform(u_lightMtx, lightMtx);
  473. const float invDim[4] = {1.0f/SHADOW_MAP_DIM, 0.0f, 0.0f, 0.0f};
  474. bgfx::setUniform(u_shadowDimsInv, invDim);
  475. float rsmAmount[4] = {m_rsmAmount,m_rsmAmount,m_rsmAmount,m_rsmAmount};
  476. bgfx::setUniform(u_rsmAmount, rsmAmount);
  477. // Set up state for combine pass
  478. // point of this is to avoid doing depth test, which is in the default state
  479. bgfx::setState(0
  480. | BGFX_STATE_WRITE_RGB
  481. | BGFX_STATE_WRITE_A
  482. );
  483. // Set up transform matrix for fullscreen quad
  484. float orthoProj[16];
  485. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
  486. bgfx::setViewTransform(RENDER_PASS_COMBINE, NULL, orthoProj);
  487. bgfx::setViewRect(RENDER_PASS_COMBINE, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  488. // Bind vertex buffer and draw quad
  489. screenSpaceQuad(m_caps->originBottomLeft);
  490. bgfx::submit(RENDER_PASS_COMBINE, m_combineProgram);
  491. // Draw UI
  492. imguiBeginFrame(m_mouseState.m_mx
  493. , m_mouseState.m_my
  494. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  495. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  496. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  497. , m_mouseState.m_mz
  498. , uint16_t(m_width)
  499. , uint16_t(m_height)
  500. );
  501. showExampleDialog(this);
  502. ImGui::SetNextWindowPos(
  503. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  504. , ImGuiCond_FirstUseEver
  505. );
  506. ImGui::SetNextWindowSize(
  507. ImVec2(m_width / 5.0f, m_height / 3.0f)
  508. , ImGuiCond_FirstUseEver
  509. );
  510. ImGui::Begin("Settings"
  511. , NULL
  512. , 0
  513. );
  514. ImGui::SliderFloat("RSM Amount", &m_rsmAmount, 0.0f, 0.7f);
  515. ImGui::SliderFloat("VPL Radius", &m_vplRadius, 0.25f, 20.0f);
  516. ImGui::SliderFloat("Light Azimuth", &m_lightAzimuth, 0.0f, 360.0f);
  517. ImGui::SliderFloat("Light Elevation", &m_lightElevation, 35.0f, 90.0f);
  518. ImGui::End();
  519. imguiEndFrame();
  520. updateLightDir();
  521. // Advance to next frame. Rendering thread will be kicked to
  522. // process submitted rendering primitives.
  523. m_currFrame = bgfx::frame();
  524. return true;
  525. }
  526. return false;
  527. }
  528. void drawAllModels(uint8_t _pass, bgfx::ProgramHandle _program)
  529. {
  530. for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
  531. {
  532. const Model& model = m_models[ii];
  533. // Set up transform matrix for each model
  534. float scale = s_meshScale[model.mesh];
  535. float mtx[16];
  536. bx::mtxSRT(mtx
  537. , scale
  538. , scale
  539. , scale
  540. , 0.0f
  541. , 0.0f
  542. , 0.0f
  543. , model.position[0]
  544. , model.position[1]
  545. , model.position[2]
  546. );
  547. // Submit mesh to gbuffer
  548. bgfx::setUniform(u_tint, model.color);
  549. meshSubmit(m_meshes[model.mesh], _pass, _program, mtx);
  550. }
  551. // Draw ground
  552. const float white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  553. bgfx::setUniform(u_tint, white);
  554. float mtxScale[16];
  555. float scale = 10.0;
  556. bx::mtxScale(mtxScale
  557. , scale
  558. , scale
  559. , scale
  560. );
  561. float mtxTrans[16];
  562. bx::mtxTranslate(mtxTrans
  563. , 0.0f
  564. , -10.0f
  565. , 0.0f
  566. );
  567. float mtx[16];
  568. bx::mtxMul(mtx, mtxScale, mtxTrans);
  569. meshSubmit(m_ground, _pass, _program, mtx);
  570. }
  571. void updateLightDir()
  572. {
  573. float el = m_lightElevation * (bx::kPi/180.0f);
  574. float az = m_lightAzimuth * (bx::kPi/180.0f);
  575. m_lightDir[0] = bx::cos(el)*bx::cos(az);
  576. m_lightDir[2] = bx::cos(el)*bx::sin(az);
  577. m_lightDir[1] = bx::sin(el);
  578. m_lightDir[3] = 0.0f;
  579. }
  580. uint32_t m_width;
  581. uint32_t m_height;
  582. uint32_t m_debug;
  583. uint32_t m_reset;
  584. entry::MouseState m_mouseState;
  585. Mesh* m_ground;
  586. Mesh* m_lightSphere; // Unit sphere
  587. // Resource handles
  588. bgfx::ProgramHandle m_gbufferProgram;
  589. bgfx::ProgramHandle m_shadowProgram;
  590. bgfx::ProgramHandle m_lightProgram;
  591. bgfx::ProgramHandle m_combineProgram;
  592. bgfx::FrameBufferHandle m_gbuffer;
  593. bgfx::FrameBufferHandle m_lightBuffer;
  594. bgfx::FrameBufferHandle m_shadowBuffer;
  595. // Shader uniforms
  596. bgfx::UniformHandle u_tint;
  597. bgfx::UniformHandle u_invMvp;
  598. bgfx::UniformHandle u_invMvpShadow;
  599. bgfx::UniformHandle u_lightMtx;
  600. bgfx::UniformHandle u_lightDir;
  601. bgfx::UniformHandle u_sphereInfo;
  602. bgfx::UniformHandle u_shadowDimsInv;
  603. bgfx::UniformHandle u_rsmAmount;
  604. // Uniforms to identify texture samples
  605. bgfx::UniformHandle s_normal;
  606. bgfx::UniformHandle s_depth;
  607. bgfx::UniformHandle s_color;
  608. bgfx::UniformHandle s_light;
  609. bgfx::UniformHandle s_shadowMap;
  610. bgfx::UniformHandle s_rsm;
  611. // Various render targets
  612. bgfx::TextureHandle m_gbufferTex[3];
  613. bgfx::TextureHandle m_lightBufferTex;
  614. bgfx::TextureHandle m_shadowBufferTex[2];
  615. const bgfx::Caps* m_caps;
  616. struct Model
  617. {
  618. uint32_t mesh; // Index of mesh in m_meshes
  619. float color[4];
  620. float position[3];
  621. };
  622. Model m_models[MODEL_COUNT];
  623. Mesh * m_meshes[BX_COUNTOF(s_meshPaths)];
  624. uint32_t m_reading;
  625. uint32_t m_currFrame;
  626. // UI
  627. bool m_cameraSpin;
  628. // Light position;
  629. float m_lightDir[4];
  630. float m_lightElevation;
  631. float m_lightAzimuth;
  632. float m_rsmAmount; // Amount of rsm
  633. float m_vplRadius; // Radius of virtual point light
  634. };
  635. } // namespace
  636. ENTRY_IMPLEMENT_MAIN(
  637. ExampleRSM
  638. , "31-rsm"
  639. , "Global Illumination with Reflective Shadow Map."
  640. , "https://bkaradzic.github.io/bgfx/examples.html#rsm"
  641. );