denoise.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright 2021 elven cache. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. /*
  6. * Implement SVGF style denoising as bgfx example. Goal is to explore various
  7. * options and parameters, not produce an optimized, efficient denoiser.
  8. *
  9. * Starts with deferred rendering scene with very basic lighting. Lighting is
  10. * masked out with a noise pattern to provide something to denoise. There are
  11. * two options for the noise pattern. One is a fixed 2x2 dither pattern to
  12. * stand-in for lighting at quarter resolution. The other is the common
  13. * shadertoy random pattern as a stand-in for some fancier lighting without
  14. * enough samples per pixel, like ray tracing.
  15. *
  16. * First a temporal denoising filter is applied. The temporal filter is only
  17. * using normals to reject previous samples. The SVGF paper also describes using
  18. * depth comparison to reject samples but that is not implemented here.
  19. *
  20. * Followed by some number of spatial filters. These are implemented like in the
  21. * SVGF paper. As an alternative to the 5x5 Edge-Avoiding A-Trous filter, can
  22. * select a 3x3 filter instead. The 3x3 filter takes fewer samples and covers a
  23. * smaller area, but takes less time to compute. From a loosely eyeballed
  24. * comparison, N 5x5 passes looks similar to N+1 3x3 passes. The wider spatial
  25. * filters take a fair chunk of time to compute. I wonder if it would be a good
  26. * idea to interleave the input texture before computing, after the first pass
  27. * which skips zero pixels.
  28. *
  29. * I have not implemetened the variance guided part.
  30. *
  31. * There's also an optional TXAA pass to be applied after. I am not happy with
  32. * its implementation yet, so it defaults to off here.
  33. */
  34. /*
  35. * Reference(s):
  36. *
  37. * - Spatiotemporal Variance-Guided Filtering: Real-Time Reconstruction for Path-Traced Global Illumination.
  38. * https://web.archive.org/web/20170720213354/https://research.nvidia.com/sites/default/files/pubs/2017-07_Spatiotemporal-Variance-Guided-Filtering%3A/svgf_preprint.pdf
  39. *
  40. * - Streaming G-Buffer Compression for Multi-Sample Anti-Aliasing.
  41. * https://web.archive.org/web/20200807211002/https://software.intel.com/content/www/us/en/develop/articles/streaming-g-buffer-compression-for-multi-sample-anti-aliasing.html
  42. *
  43. * - Edge-Avoiding A-Trous Wavelet Transform for fast Global Illumination Filtering
  44. * https://web.archive.org/web/20130412085423/https://www.uni-ulm.de/fileadmin/website_uni_ulm/iui.inst.100/institut/Papers/atrousGIfilter.pdf
  45. *
  46. */
  47. #include <common.h>
  48. #include <camera.h>
  49. #include <bgfx_utils.h>
  50. #include <imgui/imgui.h>
  51. #include <bx/rng.h>
  52. #include <bx/os.h>
  53. namespace {
  54. #define DENOISE_MAX_PASSES 6
  55. // Gbuffer has multiple render targets
  56. #define GBUFFER_RT_COLOR 0
  57. #define GBUFFER_RT_NORMAL 1
  58. #define GBUFFER_RT_VELOCITY 2
  59. #define GBUFFER_RT_DEPTH 3
  60. #define GBUFFER_RENDER_TARGETS 4
  61. #define MODEL_COUNT 100
  62. static const char * s_meshPaths[] =
  63. {
  64. "meshes/column.bin",
  65. "meshes/tree.bin",
  66. "meshes/hollowcube.bin",
  67. "meshes/bunny.bin"
  68. };
  69. static const float s_meshScale[] =
  70. {
  71. 0.05f,
  72. 0.15f,
  73. 0.25f,
  74. 0.25f
  75. };
  76. // Vertex decl for our screen space quad (used in deferred rendering)
  77. struct PosTexCoord0Vertex
  78. {
  79. float m_x;
  80. float m_y;
  81. float m_z;
  82. float m_u;
  83. float m_v;
  84. static void init()
  85. {
  86. ms_layout
  87. .begin()
  88. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  89. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  90. .end();
  91. }
  92. static bgfx::VertexLayout ms_layout;
  93. };
  94. bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
  95. struct Uniforms
  96. {
  97. enum { NumVec4 = 13 };
  98. void init() {
  99. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4);
  100. };
  101. void submit() const {
  102. bgfx::setUniform(u_params, m_params, NumVec4);
  103. }
  104. void destroy() {
  105. bgfx::destroy(u_params);
  106. }
  107. union
  108. {
  109. struct
  110. {
  111. /* 0 */ struct { float m_cameraJitterCurr[2]; float m_cameraJitterPrev[2]; };
  112. /* 1 */ struct { float m_feedbackMin; float m_feedbackMax; float m_unused1[2]; };
  113. /* 2 */ struct { float m_unused2; float m_applyMitchellFilter; float m_options[2]; };
  114. /* 3-6 */ struct { float m_worldToViewPrev[16]; };
  115. /* 7-10 */ struct { float m_viewToProjPrev[16]; };
  116. /* 11 */ struct { float m_frameOffsetForNoise; float m_noiseType; float m_unused11[2]; };
  117. /* 12 */ struct { float m_denoiseStep; float m_sigmaDepth; float m_sigmaNormal; float m_unused12; };
  118. };
  119. float m_params[NumVec4 * 4];
  120. };
  121. bgfx::UniformHandle u_params;
  122. };
  123. struct RenderTarget
  124. {
  125. void init(uint32_t _width, uint32_t _height, bgfx::TextureFormat::Enum _format, uint64_t _flags)
  126. {
  127. m_texture = bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), false, 1, _format, _flags);
  128. const bool destroyTextures = true;
  129. m_buffer = bgfx::createFrameBuffer(1, &m_texture, destroyTextures);
  130. }
  131. void destroy()
  132. {
  133. // also responsible for destroying texture
  134. bgfx::destroy(m_buffer);
  135. }
  136. bgfx::TextureHandle m_texture;
  137. bgfx::FrameBufferHandle m_buffer;
  138. };
  139. void screenSpaceQuad(bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
  140. {
  141. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
  142. {
  143. bgfx::TransientVertexBuffer vb;
  144. bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
  145. PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
  146. const float minx = -_width;
  147. const float maxx = _width;
  148. const float miny = 0.0f;
  149. const float maxy = _height * 2.0f;
  150. const float minu = -1.0f;
  151. const float maxu = 1.0f;
  152. const float zz = 0.0f;
  153. float minv = 0.0f;
  154. float maxv = 2.0f;
  155. if (_originBottomLeft)
  156. {
  157. float temp = minv;
  158. minv = maxv;
  159. maxv = temp;
  160. minv -= 1.0f;
  161. maxv -= 1.0f;
  162. }
  163. vertex[0].m_x = minx;
  164. vertex[0].m_y = miny;
  165. vertex[0].m_z = zz;
  166. vertex[0].m_u = minu;
  167. vertex[0].m_v = minv;
  168. vertex[1].m_x = maxx;
  169. vertex[1].m_y = miny;
  170. vertex[1].m_z = zz;
  171. vertex[1].m_u = maxu;
  172. vertex[1].m_v = minv;
  173. vertex[2].m_x = maxx;
  174. vertex[2].m_y = maxy;
  175. vertex[2].m_z = zz;
  176. vertex[2].m_u = maxu;
  177. vertex[2].m_v = maxv;
  178. bgfx::setVertexBuffer(0, &vb);
  179. }
  180. }
  181. void vec2Set(float* _v, float _x, float _y)
  182. {
  183. _v[0] = _x;
  184. _v[1] = _y;
  185. }
  186. void mat4Set(float * _m, const float * _src)
  187. {
  188. const uint32_t MAT4_FLOATS = 16;
  189. for (uint32_t ii = 0; ii < MAT4_FLOATS; ++ii) {
  190. _m[ii] = _src[ii];
  191. }
  192. }
  193. class ExampleDenoise : public entry::AppI
  194. {
  195. public:
  196. ExampleDenoise(const char* _name, const char* _description)
  197. : entry::AppI(_name, _description)
  198. , m_currFrame(UINT32_MAX)
  199. {
  200. }
  201. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  202. {
  203. Args args(_argc, _argv);
  204. m_width = _width;
  205. m_height = _height;
  206. m_debug = BGFX_DEBUG_NONE;
  207. m_reset = BGFX_RESET_VSYNC;
  208. bgfx::Init init;
  209. init.type = args.m_type;
  210. init.vendorId = args.m_pciId;
  211. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  212. init.platformData.ndt = entry::getNativeDisplayHandle();
  213. init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle);
  214. init.resolution.width = m_width;
  215. init.resolution.height = m_height;
  216. init.resolution.reset = m_reset;
  217. bgfx::init(init);
  218. // Enable debug text.
  219. bgfx::setDebug(m_debug);
  220. // Create uniforms
  221. m_uniforms.init();
  222. // Create texture sampler uniforms (used when we bind textures)
  223. s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler); // Model's source albedo
  224. s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler); // Color (albedo) gbuffer, default color input
  225. s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler); // Normal gbuffer, Model's source normal
  226. s_velocity = bgfx::createUniform("s_velocity", bgfx::UniformType::Sampler); // Velocity gbuffer
  227. s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler); // Depth gbuffer
  228. s_previousColor = bgfx::createUniform("s_previousColor", bgfx::UniformType::Sampler); // Previous frame's result
  229. s_previousNormal = bgfx::createUniform("s_previousNormal", bgfx::UniformType::Sampler); // Previous frame's gbuffer normal
  230. // Create program from shaders.
  231. m_gbufferProgram = loadProgram("vs_denoise_gbuffer", "fs_denoise_gbuffer"); // Fill gbuffer
  232. m_combineProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_deferred_combine"); // Compute lighting from gbuffer
  233. m_copyProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_copy");
  234. m_denoiseTemporalProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_temporal");
  235. m_denoiseSpatialProgram3x3 = loadProgram("vs_denoise_screenquad", "fs_denoise_spatial_3x3");
  236. m_denoiseSpatialProgram5x5 = loadProgram("vs_denoise_screenquad", "fs_denoise_spatial_5x5");
  237. m_denoiseApplyLighting = loadProgram("vs_denoise_screenquad", "fs_denoise_apply_lighting");
  238. m_txaaProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_txaa");
  239. // Load some meshes
  240. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  241. {
  242. m_meshes[ii] = meshLoad(s_meshPaths[ii]);
  243. }
  244. // Randomly create some models
  245. bx::RngMwc mwc;
  246. for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
  247. {
  248. Model& model = m_models[ii];
  249. model.mesh = mwc.gen() % BX_COUNTOF(s_meshPaths);
  250. model.position[0] = ( ( (mwc.gen() % 256) ) - 128.0f) / 20.0f;
  251. model.position[1] = 0;
  252. model.position[2] = ( ( (mwc.gen() % 256) ) - 128.0f) / 20.0f;
  253. }
  254. // Load ground, just use the cube
  255. m_ground = meshLoad("meshes/cube.bin");
  256. m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
  257. m_normalTexture = loadTexture("textures/fieldstone-n.dds");
  258. m_recreateFrameBuffers = false;
  259. createFramebuffers();
  260. // Vertex decl
  261. PosTexCoord0Vertex::init();
  262. // Init camera
  263. cameraCreate();
  264. cameraSetPosition({ 0.0f, 1.5f, 0.0f });
  265. cameraSetVerticalAngle(-0.3f);
  266. m_fovY = 60.0f;
  267. // Init "prev" matrices, will be same for first frame
  268. cameraGetViewMtx(m_view);
  269. bx::mtxProj(m_proj, m_fovY, float(m_size[0]) / float(m_size[1]), 0.01f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  270. mat4Set(m_worldToViewPrev, m_view);
  271. mat4Set(m_viewToProjPrev, m_proj);
  272. // Track whether previous results are valid
  273. m_havePrevious = false;
  274. imguiCreate();
  275. }
  276. int32_t shutdown() override
  277. {
  278. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  279. {
  280. meshUnload(m_meshes[ii]);
  281. }
  282. meshUnload(m_ground);
  283. bgfx::destroy(m_normalTexture);
  284. bgfx::destroy(m_groundTexture);
  285. bgfx::destroy(m_gbufferProgram);
  286. bgfx::destroy(m_combineProgram);
  287. bgfx::destroy(m_copyProgram);
  288. bgfx::destroy(m_denoiseTemporalProgram);
  289. bgfx::destroy(m_denoiseSpatialProgram3x3);
  290. bgfx::destroy(m_denoiseSpatialProgram5x5);
  291. bgfx::destroy(m_denoiseApplyLighting);
  292. bgfx::destroy(m_txaaProgram);
  293. m_uniforms.destroy();
  294. bgfx::destroy(s_albedo);
  295. bgfx::destroy(s_color);
  296. bgfx::destroy(s_normal);
  297. bgfx::destroy(s_velocity);
  298. bgfx::destroy(s_depth);
  299. bgfx::destroy(s_previousColor);
  300. bgfx::destroy(s_previousNormal);
  301. destroyFramebuffers();
  302. cameraDestroy();
  303. imguiDestroy();
  304. bgfx::shutdown();
  305. return 0;
  306. }
  307. bool update() override
  308. {
  309. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  310. {
  311. // skip processing when minimized, otherwise crashing
  312. if (0 == m_width
  313. || 0 == m_height)
  314. {
  315. return true;
  316. }
  317. // Update frame timer
  318. int64_t now = bx::getHPCounter();
  319. static int64_t last = now;
  320. const int64_t frameTime = now - last;
  321. last = now;
  322. const double freq = double(bx::getHPFrequency() );
  323. const float deltaTime = float(frameTime / freq);
  324. const bgfx::Caps* caps = bgfx::getCaps();
  325. if (m_size[0] != (int32_t)m_width
  326. || m_size[1] != (int32_t)m_height
  327. || m_recreateFrameBuffers)
  328. {
  329. destroyFramebuffers();
  330. createFramebuffers();
  331. m_recreateFrameBuffers = false;
  332. }
  333. // Update camera
  334. cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
  335. // Set up matrices for gbuffer
  336. cameraGetViewMtx(m_view);
  337. updateUniforms();
  338. bx::mtxProj(m_proj, m_fovY, float(m_size[0]) / float(m_size[1]), 0.01f, 100.0f, caps->homogeneousDepth);
  339. bx::mtxProj(m_proj2, m_fovY, float(m_size[0]) / float(m_size[1]), 0.01f, 100.0f, false);
  340. if (m_enableTxaa)
  341. {
  342. m_proj[2*4+0] += m_jitter[0] * (2.0f / m_size[0]);
  343. m_proj[2*4+1] -= m_jitter[1] * (2.0f / m_size[1]);
  344. }
  345. bgfx::ViewId view = 0;
  346. // Draw everything into gbuffer
  347. {
  348. bgfx::setViewName(view, "GBuffer");
  349. bgfx::setViewClear(view
  350. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  351. , 0
  352. , 1.0f
  353. , 0
  354. );
  355. bgfx::setViewRect(view, 0, 0, uint16_t(m_size[0]), uint16_t(m_size[1]) );
  356. bgfx::setViewTransform(view, m_view, m_proj);
  357. // Make sure when we draw it goes into gbuffer and not backbuffer
  358. bgfx::setViewFrameBuffer(view, m_gbuffer);
  359. bgfx::setState(0
  360. | BGFX_STATE_WRITE_RGB
  361. | BGFX_STATE_WRITE_A
  362. | BGFX_STATE_WRITE_Z
  363. | BGFX_STATE_DEPTH_TEST_LESS
  364. );
  365. drawAllModels(view, m_gbufferProgram, m_uniforms);
  366. ++view;
  367. }
  368. float orthoProj[16];
  369. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  370. // Shade gbuffer
  371. {
  372. bgfx::setViewName(view, "Combine");
  373. // for some reason, previous draws texture lingering in transform stack
  374. // need to clear out, otherwise this copy is garbled. this used to work
  375. // and broke after updating, but i last updated like 2 years ago.
  376. float identity[16];
  377. bx::mtxIdentity(identity);
  378. bgfx::setTransform(identity);
  379. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  380. bgfx::setViewTransform(view, NULL, orthoProj);
  381. bgfx::setViewFrameBuffer(view, m_currentColor.m_buffer);
  382. bgfx::setState(0
  383. | BGFX_STATE_WRITE_RGB
  384. | BGFX_STATE_WRITE_A
  385. | BGFX_STATE_DEPTH_TEST_ALWAYS
  386. );
  387. bgfx::setTexture(0, s_color, m_gbufferTex[GBUFFER_RT_COLOR]);
  388. bgfx::setTexture(1, s_normal, m_gbufferTex[GBUFFER_RT_NORMAL]);
  389. m_uniforms.submit();
  390. screenSpaceQuad(caps->originBottomLeft);
  391. bgfx::submit(view, m_combineProgram);
  392. ++view;
  393. }
  394. // update last texture written, to chain passes together
  395. bgfx::TextureHandle lastTex = m_currentColor.m_texture;
  396. // denoise temporal pass
  397. if (m_useTemporalPass && m_havePrevious)
  398. {
  399. bgfx::setViewName(view, "Denoise Temporal");
  400. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  401. bgfx::setViewTransform(view, NULL, orthoProj);
  402. bgfx::setViewFrameBuffer(view, m_temporaryColor.m_buffer);
  403. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
  404. // want color, prevColor
  405. // normal, prevNormal
  406. // depth, prevDepth to reject previous samples from accumulating - skipping depth for now
  407. bgfx::setTexture(0, s_color, lastTex);
  408. bgfx::setTexture(1, s_normal, m_gbufferTex[GBUFFER_RT_NORMAL]);
  409. bgfx::setTexture(2, s_velocity, m_gbufferTex[GBUFFER_RT_VELOCITY]);
  410. bgfx::setTexture(3, s_previousColor, m_previousDenoise.m_texture);
  411. bgfx::setTexture(4, s_previousNormal, m_previousNormal.m_texture);
  412. m_uniforms.submit();
  413. screenSpaceQuad(caps->originBottomLeft);
  414. bgfx::submit(view, m_denoiseTemporalProgram);
  415. ++view;
  416. lastTex = m_temporaryColor.m_texture;
  417. }
  418. // denoise spatial passes
  419. if (0 < m_denoisePasses)
  420. {
  421. // variable number of passes for denoise, alternate between two textures/buffers
  422. bgfx::FrameBufferHandle destBuffer[] =
  423. {
  424. m_previousDenoise.m_buffer,
  425. m_currentColor.m_buffer,
  426. m_temporaryColor.m_buffer,
  427. m_currentColor.m_buffer,
  428. m_temporaryColor.m_buffer,
  429. m_currentColor.m_buffer,
  430. };
  431. BX_STATIC_ASSERT(BX_COUNTOF(destBuffer) == DENOISE_MAX_PASSES);
  432. const uint32_t denoisePasses = bx::min(DENOISE_MAX_PASSES, m_denoisePasses);
  433. for (uint32_t ii = 0; ii < denoisePasses; ++ii)
  434. {
  435. char name[64];
  436. bx::snprintf(name, BX_COUNTOF(name), "Denoise %d/%d", ii, denoisePasses-1);
  437. bgfx::setViewName(view, name);
  438. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  439. bgfx::setViewTransform(view, NULL, orthoProj);
  440. bgfx::setViewFrameBuffer(view, destBuffer[ii]);
  441. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
  442. bgfx::setTexture(0, s_color, lastTex);
  443. bgfx::setTexture(1, s_normal, m_gbufferTex[GBUFFER_RT_NORMAL]);
  444. bgfx::setTexture(2, s_depth, m_gbufferTex[GBUFFER_RT_DEPTH]);
  445. // need to update some denoise uniforms per draw
  446. const float denoiseStepScale = bx::pow(2.0f, float(ii) );
  447. m_uniforms.m_denoiseStep = denoiseStepScale;
  448. m_uniforms.submit();
  449. screenSpaceQuad(caps->originBottomLeft);
  450. const bgfx::ProgramHandle spatialProgram = (0 == m_spatialSampleType)
  451. ? m_denoiseSpatialProgram3x3
  452. : m_denoiseSpatialProgram5x5
  453. ;
  454. bgfx::submit(view, spatialProgram);
  455. ++view;
  456. if (m_previousDenoise.m_buffer.idx == destBuffer[ii].idx)
  457. {
  458. lastTex = m_previousDenoise.m_texture;
  459. }
  460. else if (m_temporaryColor.m_buffer.idx == destBuffer[ii].idx)
  461. {
  462. lastTex = m_temporaryColor.m_texture;
  463. }
  464. else
  465. {
  466. lastTex = m_currentColor.m_texture;
  467. }
  468. }
  469. }
  470. else
  471. {
  472. // need color result for temporal denoise if not supplied by spatial pass
  473. // (per SVGF paper, reuse previous frame's first spatial pass output as previous color
  474. bgfx::setViewName(view, "Copy Color for Temporal Denoise");
  475. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  476. bgfx::setViewTransform(view, NULL, orthoProj);
  477. bgfx::setViewFrameBuffer(view, m_previousDenoise.m_buffer);
  478. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
  479. bgfx::setTexture(0, s_color, lastTex);
  480. screenSpaceQuad(caps->originBottomLeft);
  481. bgfx::submit(view, m_copyProgram);
  482. ++view;
  483. }
  484. // apply lighting
  485. {
  486. bgfx::setViewName(view, "Apply Lighting");
  487. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  488. bgfx::setViewTransform(view, NULL, orthoProj);
  489. bgfx::FrameBufferHandle destBuffer = (lastTex.idx == m_currentColor.m_texture.idx)
  490. ? m_temporaryColor.m_buffer
  491. : m_currentColor.m_buffer
  492. ;
  493. bgfx::setViewFrameBuffer(view, destBuffer);
  494. bgfx::setState(0
  495. | BGFX_STATE_WRITE_RGB
  496. | BGFX_STATE_WRITE_A
  497. | BGFX_STATE_DEPTH_TEST_ALWAYS
  498. );
  499. bgfx::setTexture(0, s_color, lastTex);
  500. bgfx::setTexture(1, s_albedo, m_gbufferTex[GBUFFER_RT_COLOR]);
  501. m_uniforms.submit();
  502. screenSpaceQuad(caps->originBottomLeft);
  503. bgfx::submit(view, m_denoiseApplyLighting);
  504. ++view;
  505. lastTex = (m_temporaryColor.m_buffer.idx == destBuffer.idx)
  506. ? m_temporaryColor.m_texture
  507. : m_currentColor.m_texture
  508. ;
  509. }
  510. if (m_enableTxaa)
  511. {
  512. // Draw txaa to txaa buffer
  513. {
  514. bgfx::setViewName(view, "Temporal AA");
  515. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  516. bgfx::setViewTransform(view, NULL, orthoProj);
  517. bgfx::setViewFrameBuffer(view, m_txaaColor.m_buffer);
  518. bgfx::setState(0
  519. | BGFX_STATE_WRITE_RGB
  520. | BGFX_STATE_WRITE_A
  521. | BGFX_STATE_DEPTH_TEST_ALWAYS
  522. );
  523. bgfx::setTexture(0, s_color, lastTex);
  524. bgfx::setTexture(1, s_previousColor, m_previousColor.m_texture);
  525. bgfx::setTexture(2, s_velocity, m_gbufferTex[GBUFFER_RT_VELOCITY]);
  526. bgfx::setTexture(3, s_depth, m_gbufferTex[GBUFFER_RT_DEPTH]);
  527. m_uniforms.submit();
  528. screenSpaceQuad(caps->originBottomLeft);
  529. bgfx::submit(view, m_txaaProgram);
  530. ++view;
  531. }
  532. // Copy txaa result to previous
  533. {
  534. bgfx::setViewName(view, "Copy to Previous");
  535. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  536. bgfx::setViewTransform(view, NULL, orthoProj);
  537. bgfx::setViewFrameBuffer(view, m_previousColor.m_buffer);
  538. bgfx::setState(0
  539. | BGFX_STATE_WRITE_RGB
  540. | BGFX_STATE_WRITE_A
  541. | BGFX_STATE_DEPTH_TEST_ALWAYS
  542. );
  543. bgfx::setTexture(0, s_color, m_txaaColor.m_texture);
  544. screenSpaceQuad(caps->originBottomLeft);
  545. bgfx::submit(view, m_copyProgram);
  546. ++view;
  547. }
  548. // Copy txaa result to swap chain
  549. {
  550. bgfx::setViewName(view, "Display");
  551. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  552. bgfx::setViewTransform(view, NULL, orthoProj);
  553. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  554. bgfx::setState(0
  555. | BGFX_STATE_WRITE_RGB
  556. | BGFX_STATE_WRITE_A
  557. | BGFX_STATE_DEPTH_TEST_ALWAYS
  558. );
  559. bgfx::setTexture(0, s_color, m_txaaColor.m_texture);
  560. screenSpaceQuad(caps->originBottomLeft);
  561. bgfx::submit(view, m_copyProgram);
  562. ++view;
  563. }
  564. }
  565. else
  566. {
  567. // Copy color result to swap chain
  568. {
  569. bgfx::setViewName(view, "Display");
  570. bgfx::setViewClear(view
  571. , BGFX_CLEAR_NONE
  572. , 0
  573. , 1.0f
  574. , 0
  575. );
  576. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  577. bgfx::setViewTransform(view, NULL, orthoProj);
  578. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  579. bgfx::setState(0
  580. | BGFX_STATE_WRITE_RGB
  581. | BGFX_STATE_WRITE_A
  582. );
  583. bgfx::setTexture(0, s_color, lastTex);
  584. screenSpaceQuad(caps->originBottomLeft);
  585. bgfx::submit(view, m_copyProgram);
  586. ++view;
  587. }
  588. }
  589. // copy the normal buffer for next time
  590. {
  591. bgfx::setViewName(view, "Copy Normals");
  592. bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  593. bgfx::setViewTransform(view, NULL, orthoProj);
  594. bgfx::setViewFrameBuffer(view, m_previousNormal.m_buffer);
  595. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
  596. bgfx::setTexture(0, s_color, m_gbufferTex[GBUFFER_RT_NORMAL]);
  597. screenSpaceQuad(caps->originBottomLeft);
  598. bgfx::submit(view, m_copyProgram);
  599. ++view;
  600. // update previous status
  601. m_havePrevious = true;
  602. }
  603. // Copy matrices for next time
  604. mat4Set(m_worldToViewPrev, m_view);
  605. mat4Set(m_viewToProjPrev, m_proj);
  606. // Draw UI
  607. imguiBeginFrame(m_mouseState.m_mx
  608. , m_mouseState.m_my
  609. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  610. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  611. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  612. , m_mouseState.m_mz
  613. , uint16_t(m_width)
  614. , uint16_t(m_height)
  615. );
  616. showExampleDialog(this);
  617. ImGui::SetNextWindowPos(
  618. ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
  619. , ImGuiCond_FirstUseEver
  620. );
  621. ImGui::SetNextWindowSize(
  622. ImVec2(m_width / 4.0f, m_height / 1.24f)
  623. , ImGuiCond_FirstUseEver
  624. );
  625. ImGui::Begin("Settings"
  626. , NULL
  627. , 0
  628. );
  629. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  630. {
  631. ImGui::TextWrapped(
  632. "In this demo, noise is added to results of deferred lighting. Then denoise is applied "
  633. "before multiplying the lit result with gbuffer albedo. Optionally, temporal antialiasing "
  634. "can be applied after that. (off by default, implementation blurry)");
  635. ImGui::Separator();
  636. ImGui::Text("noise controls:");
  637. ImGui::Combo("pattern", &m_noiseType, "none\0dither\0random\0\0");
  638. if (ImGui::IsItemHovered() )
  639. {
  640. ImGui::BeginTooltip();
  641. ImGui::Text("none");
  642. ImGui::BulletText("compare denoised results to this");
  643. ImGui::BulletText("brighter than noisy images, not losing any pixel's energy");
  644. ImGui::Text("dither");
  645. ImGui::BulletText("reject 3 out of 4 pixels in 2x2 pattern");
  646. ImGui::BulletText("could represent lower resolution signal");
  647. ImGui::Text("random");
  648. ImGui::BulletText("reject about half pixels, using common shader random");
  649. ImGui::BulletText("could represent monte carlo something or other");
  650. ImGui::EndTooltip();
  651. }
  652. ImGui::Checkbox("dynamic noise", &m_dynamicNoise);
  653. if (ImGui::IsItemHovered() )
  654. {
  655. ImGui::SetTooltip("update noise pattern each frame");
  656. }
  657. ImGui::Separator();
  658. }
  659. {
  660. ImGui::Text("temporal denoise pass controls:");
  661. ImGui::Checkbox("use temporal pass", &m_useTemporalPass);
  662. ImGui::Separator();
  663. }
  664. {
  665. ImGui::Text("spatial denoise pass controls:");
  666. ImGui::SliderInt("spatial passes", &m_denoisePasses, 0, DENOISE_MAX_PASSES);
  667. if (ImGui::IsItemHovered() )
  668. {
  669. ImGui::SetTooltip("set passes to 0 to turn off spatial denoise");
  670. }
  671. ImGui::Combo("spatial sample extent", &m_spatialSampleType, "three\0five\0\0");
  672. if (ImGui::IsItemHovered() )
  673. {
  674. ImGui::SetTooltip("select 3x3 or 5x5 filter kernel");
  675. }
  676. ImGui::SliderFloat("sigma z", &m_sigmaDepth, 0.0f, 0.1f, "%.5f");
  677. if (ImGui::IsItemHovered() )
  678. {
  679. ImGui::SetTooltip("lower sigma z, pickier blending across depth edges");
  680. }
  681. ImGui::SliderFloat("sigma n", &m_sigmaNormal, 1.0f, 256.0f);
  682. if (ImGui::IsItemHovered() )
  683. {
  684. ImGui::SetTooltip("higher sigma n, pickier blending across normal edges");
  685. }
  686. ImGui::Separator();
  687. }
  688. if (ImGui::CollapsingHeader("TXAA options") )
  689. {
  690. ImGui::Checkbox("use TXAA", &m_enableTxaa);
  691. ImGui::Checkbox("apply extra blur to current color", &m_applyMitchellFilter);
  692. if (ImGui::IsItemHovered() )
  693. {
  694. ImGui::SetTooltip("reduces flicker/crawl on thin features, maybe too much!");
  695. }
  696. ImGui::SliderFloat("feedback min", &m_feedbackMin, 0.0f, 1.0f);
  697. if (ImGui::IsItemHovered() )
  698. {
  699. ImGui::SetTooltip("minimum amount of previous frame to blend in");
  700. }
  701. ImGui::SliderFloat("feedback max", &m_feedbackMax, 0.0f, 1.0f);
  702. if (ImGui::IsItemHovered() )
  703. {
  704. ImGui::SetTooltip("maximum amount of previous frame to blend in");
  705. }
  706. ImGui::Checkbox("debug TXAA with slow frame rate", &m_useTxaaSlow);
  707. if (ImGui::IsItemHovered() )
  708. {
  709. ImGui::BeginTooltip();
  710. ImGui::Text("sleep 100ms per frame to highlight temporal artifacts");
  711. ImGui::Text("high framerate compensates for flickering, masking issues");
  712. ImGui::EndTooltip();
  713. }
  714. ImGui::Separator();
  715. }
  716. ImGui::End();
  717. imguiEndFrame();
  718. // Advance to next frame. Rendering thread will be kicked to
  719. // process submitted rendering primitives.
  720. m_currFrame = bgfx::frame();
  721. // add artificial wait to emphasize txaa behavior
  722. if (m_useTxaaSlow)
  723. {
  724. bx::sleep(100);
  725. }
  726. return true;
  727. }
  728. return false;
  729. }
  730. void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, const Uniforms & _uniforms)
  731. {
  732. for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
  733. {
  734. const Model& model = m_models[ii];
  735. // Set up transform matrix for each model
  736. const float scale = s_meshScale[model.mesh];
  737. float mtx[16];
  738. bx::mtxSRT(mtx
  739. , scale
  740. , scale
  741. , scale
  742. , 0.0f
  743. , 0.0f
  744. , 0.0f
  745. , model.position[0]
  746. , model.position[1]
  747. , model.position[2]
  748. );
  749. // Submit mesh to gbuffer
  750. bgfx::setTexture(0, s_albedo, m_groundTexture);
  751. bgfx::setTexture(1, s_normal, m_normalTexture);
  752. _uniforms.submit();
  753. meshSubmit(m_meshes[model.mesh], _pass, _program, mtx);
  754. }
  755. // Draw ground
  756. float mtxScale[16];
  757. const float scale = 10.0f;
  758. bx::mtxScale(mtxScale, scale, scale, scale);
  759. float mtxTranslate[16];
  760. bx::mtxTranslate(mtxTranslate
  761. , 0.0f
  762. , -10.0f
  763. , 0.0f
  764. );
  765. float mtx[16];
  766. bx::mtxMul(mtx, mtxScale, mtxTranslate);
  767. bgfx::setTexture(0, s_albedo, m_groundTexture);
  768. bgfx::setTexture(1, s_normal, m_normalTexture);
  769. _uniforms.submit();
  770. meshSubmit(m_ground, _pass, _program, mtx);
  771. }
  772. void createFramebuffers()
  773. {
  774. m_size[0] = m_width;
  775. m_size[1] = m_height;
  776. const uint64_t bilinearFlags = 0
  777. | BGFX_TEXTURE_RT
  778. | BGFX_SAMPLER_U_CLAMP
  779. | BGFX_SAMPLER_V_CLAMP
  780. ;
  781. const uint64_t pointSampleFlags = bilinearFlags
  782. | BGFX_SAMPLER_MIN_POINT
  783. | BGFX_SAMPLER_MAG_POINT
  784. | BGFX_SAMPLER_MIP_POINT
  785. ;
  786. m_gbufferTex[GBUFFER_RT_COLOR] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::BGRA8, pointSampleFlags);
  787. m_gbufferTex[GBUFFER_RT_NORMAL] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::BGRA8, pointSampleFlags);
  788. m_gbufferTex[GBUFFER_RT_VELOCITY] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::RG16F, pointSampleFlags);
  789. m_gbufferTex[GBUFFER_RT_DEPTH] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::D32F , pointSampleFlags);
  790. m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
  791. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RG11B10F;
  792. if (!bgfx::isTextureValid(1, false, 1, format, bilinearFlags))
  793. {
  794. format = bgfx::TextureFormat::RGBA16F;
  795. }
  796. m_currentColor .init(m_size[0], m_size[1], format, bilinearFlags);
  797. m_previousColor .init(m_size[0], m_size[1], format, bilinearFlags);
  798. m_txaaColor .init(m_size[0], m_size[1], format, bilinearFlags);
  799. m_temporaryColor .init(m_size[0], m_size[1], format, bilinearFlags);
  800. m_previousNormal .init(m_size[0], m_size[1], format, pointSampleFlags);
  801. m_previousDenoise.init(m_size[0], m_size[1], format, bilinearFlags);
  802. }
  803. // all buffers set to destroy their textures
  804. void destroyFramebuffers()
  805. {
  806. bgfx::destroy(m_gbuffer);
  807. m_currentColor.destroy();
  808. m_previousColor.destroy();
  809. m_txaaColor.destroy();
  810. m_temporaryColor.destroy();
  811. m_previousNormal.destroy();
  812. m_previousDenoise.destroy();
  813. }
  814. void updateUniforms()
  815. {
  816. {
  817. uint32_t idx = m_currFrame % 8;
  818. const float offsets[] =
  819. {
  820. (1.0f/ 2.0f), (1.0f/3.0f),
  821. (1.0f/ 4.0f), (2.0f/3.0f),
  822. (3.0f/ 4.0f), (1.0f/9.0f),
  823. (1.0f/ 8.0f), (4.0f/9.0f),
  824. (5.0f/ 8.0f), (7.0f/9.0f),
  825. (3.0f/ 8.0f), (2.0f/9.0f),
  826. (7.0f/ 8.0f), (5.0f/9.0f),
  827. (1.0f/16.0f), (8.0f/9.0f),
  828. };
  829. // Strange constant for jitterX is because 8 values from halton2
  830. // sequence above do not average out to 0.5, 1/16 skews it to the
  831. // left. Subtracting a smaller value to center the range of jitter
  832. // around 0. Not necessary for jitterY. Not confident this makes sense...
  833. const float jitterX = 1.0f * (offsets[2*idx] - (7.125f/16.0f) );
  834. const float jitterY = 1.0f * (offsets[2*idx+1] - 0.5f);
  835. vec2Set(m_uniforms.m_cameraJitterCurr, jitterX, jitterY);
  836. vec2Set(m_uniforms.m_cameraJitterPrev, m_jitter[0], m_jitter[1]);
  837. m_jitter[0] = jitterX;
  838. m_jitter[1] = jitterY;
  839. }
  840. m_uniforms.m_feedbackMin = m_feedbackMin;
  841. m_uniforms.m_feedbackMax = m_feedbackMax;
  842. m_uniforms.m_applyMitchellFilter = m_applyMitchellFilter ? 1.0f : 0.0f;
  843. mat4Set(m_uniforms.m_worldToViewPrev, m_worldToViewPrev);
  844. mat4Set(m_uniforms.m_viewToProjPrev, m_viewToProjPrev);
  845. m_uniforms.m_frameOffsetForNoise = m_dynamicNoise
  846. ? float(m_currFrame % 8)
  847. : 0.0f
  848. ;
  849. m_uniforms.m_noiseType = float(m_noiseType);
  850. m_uniforms.m_sigmaDepth = m_sigmaDepth;
  851. m_uniforms.m_sigmaNormal = m_sigmaNormal;
  852. }
  853. uint32_t m_width;
  854. uint32_t m_height;
  855. uint32_t m_debug;
  856. uint32_t m_reset;
  857. entry::MouseState m_mouseState;
  858. // Resource handles
  859. bgfx::ProgramHandle m_gbufferProgram;
  860. bgfx::ProgramHandle m_combineProgram;
  861. bgfx::ProgramHandle m_copyProgram;
  862. bgfx::ProgramHandle m_denoiseTemporalProgram;
  863. bgfx::ProgramHandle m_denoiseSpatialProgram3x3;
  864. bgfx::ProgramHandle m_denoiseSpatialProgram5x5;
  865. bgfx::ProgramHandle m_denoiseApplyLighting;
  866. bgfx::ProgramHandle m_txaaProgram;
  867. // Shader uniforms
  868. Uniforms m_uniforms;
  869. // Uniforms to identify texture samplers
  870. bgfx::UniformHandle s_albedo;
  871. bgfx::UniformHandle s_color;
  872. bgfx::UniformHandle s_normal;
  873. bgfx::UniformHandle s_velocity;
  874. bgfx::UniformHandle s_depth;
  875. bgfx::UniformHandle s_previousColor;
  876. bgfx::UniformHandle s_previousNormal;
  877. bgfx::FrameBufferHandle m_gbuffer;
  878. bgfx::TextureHandle m_gbufferTex[GBUFFER_RENDER_TARGETS];
  879. RenderTarget m_currentColor;
  880. RenderTarget m_previousColor;
  881. RenderTarget m_txaaColor;
  882. RenderTarget m_temporaryColor; // need another buffer to ping-pong results
  883. RenderTarget m_previousNormal;
  884. RenderTarget m_previousDenoise; // color output by first spatial denoise pass, input to next frame as previous color
  885. struct Model
  886. {
  887. uint32_t mesh; // Index of mesh in m_meshes
  888. float position[3];
  889. };
  890. Model m_models[MODEL_COUNT];
  891. Mesh* m_meshes[BX_COUNTOF(s_meshPaths)];
  892. Mesh* m_ground;
  893. bgfx::TextureHandle m_groundTexture;
  894. bgfx::TextureHandle m_normalTexture;
  895. uint32_t m_currFrame;
  896. float m_fovY = 60.0f;
  897. bool m_recreateFrameBuffers = false;
  898. bool m_havePrevious = false;
  899. float m_view[16];
  900. float m_proj[16];
  901. float m_proj2[16];
  902. float m_viewToProjPrev[16];
  903. float m_worldToViewPrev[16];
  904. float m_jitter[2];
  905. int32_t m_size[2];
  906. // UI parameters
  907. int32_t m_noiseType = 2;
  908. int32_t m_spatialSampleType = 1;
  909. int32_t m_denoisePasses = 5;
  910. float m_sigmaDepth = 0.05f;
  911. float m_sigmaNormal = 128.0f;
  912. float m_feedbackMin = 0.8f;
  913. float m_feedbackMax = 0.95f;
  914. bool m_dynamicNoise = true;
  915. bool m_useTemporalPass = true;
  916. bool m_enableTxaa = false;
  917. bool m_applyMitchellFilter = true;
  918. bool m_useTxaaSlow = false;
  919. };
  920. } // namespace
  921. ENTRY_IMPLEMENT_MAIN(ExampleDenoise, "43-denoise", "Denoise.");