MotionBlur.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Renderer/MotionBlur.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/MotionVectors.h>
  8. #include <AnKi/Renderer/Tonemapping.h>
  9. #include <AnKi/Renderer/GBuffer.h>
  10. #include <AnKi/Util/Tracer.h>
  11. namespace anki {
  12. Error MotionBlur::init()
  13. {
  14. ANKI_CHECK(loadShaderProgram(
  15. "ShaderBinaries/MotionBlur.ankiprogbin",
  16. {{"TILE_SIZE", MutatorValue(g_cvarRenderMotionBlurTileSize)}, {"SAMPLE_COUNT", MutatorValue(g_cvarRenderMotionBlurSampleCount)}}, m_prog,
  17. m_maxVelocityGrProg, "MaxTileVelocity"));
  18. ANKI_CHECK(loadShaderProgram(
  19. "ShaderBinaries/MotionBlur.ankiprogbin",
  20. {{"TILE_SIZE", MutatorValue(g_cvarRenderMotionBlurTileSize)}, {"SAMPLE_COUNT", MutatorValue(g_cvarRenderMotionBlurSampleCount)}}, m_prog,
  21. m_maxNeightbourVelocityGrProg, "MaxNeighbourTileVelocity"));
  22. ANKI_CHECK(loadShaderProgram(
  23. "ShaderBinaries/MotionBlur.ankiprogbin",
  24. {{"TILE_SIZE", MutatorValue(g_cvarRenderMotionBlurTileSize)}, {"SAMPLE_COUNT", MutatorValue(g_cvarRenderMotionBlurSampleCount)}}, m_prog,
  25. m_reconstructGrProg, "Reconstruct"));
  26. const UVec2 tiledTexSize = (getRenderer().getPostProcessResolution() + g_cvarRenderMotionBlurTileSize - 1) / g_cvarRenderMotionBlurTileSize;
  27. m_maxVelocityRtDesc = getRenderer().create2DRenderTargetDescription(tiledTexSize.x, tiledTexSize.y, Format::kR16G16_Sfloat, "MaxTileVelocity");
  28. m_maxVelocityRtDesc.bake();
  29. m_maxNeighbourVelocityRtDesc =
  30. getRenderer().create2DRenderTargetDescription(tiledTexSize.x, tiledTexSize.y, Format::kR16G16_Sfloat, "MaxNeighbourTileVelocity");
  31. m_maxNeighbourVelocityRtDesc.bake();
  32. m_finalRtDesc = getRenderer().create2DRenderTargetDescription(
  33. getRenderer().getPostProcessResolution().x, getRenderer().getPostProcessResolution().y,
  34. (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR8G8B8_Unorm : Format::kR8G8B8A8_Unorm,
  35. "MotionBlur");
  36. m_finalRtDesc.bake();
  37. return Error::kNone;
  38. }
  39. void MotionBlur::populateRenderGraph(RenderingContext& ctx)
  40. {
  41. ANKI_TRACE_SCOPED_EVENT(MotionBlur);
  42. if(g_cvarRenderMotionBlurSampleCount == 0)
  43. {
  44. m_runCtx.m_rt = {};
  45. return;
  46. }
  47. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  48. // MaxTileVelocity
  49. const RenderTargetHandle maxVelRt = rgraph.newRenderTarget(m_maxVelocityRtDesc);
  50. {
  51. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("Motion blur min velocity");
  52. pass.newTextureDependency(getRenderer().getMotionVectors().getMotionVectorsRt(), TextureUsageBit::kSrvCompute);
  53. pass.newTextureDependency(maxVelRt, TextureUsageBit::kUavCompute);
  54. pass.setWork([this, maxVelRt](RenderPassWorkContext& rgraphCtx) {
  55. ANKI_TRACE_SCOPED_EVENT(MotionBlurMinTileVelocity);
  56. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  57. cmdb.bindShaderProgram(m_maxVelocityGrProg.get());
  58. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearClamp.get());
  59. rgraphCtx.bindSrv(0, 0, getRenderer().getMotionVectors().getMotionVectorsRt());
  60. rgraphCtx.bindUav(0, 0, maxVelRt);
  61. const Vec4 consts(Vec2(getRenderer().getPostProcessResolution()), 0.0f, 0.0f);
  62. cmdb.setFastConstants(&consts, sizeof(consts));
  63. const UVec2 tiledTexSize =
  64. (getRenderer().getPostProcessResolution() + g_cvarRenderMotionBlurTileSize - 1) / g_cvarRenderMotionBlurTileSize;
  65. cmdb.dispatchCompute(tiledTexSize.x, tiledTexSize.y, 1);
  66. });
  67. }
  68. // MaxNeighbourTileVelocity
  69. const RenderTargetHandle maxNeighbourVelRt = rgraph.newRenderTarget(m_maxNeighbourVelocityRtDesc);
  70. {
  71. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("Motion blur min neighbour vel");
  72. pass.newTextureDependency(maxVelRt, TextureUsageBit::kSrvCompute);
  73. pass.newTextureDependency(maxNeighbourVelRt, TextureUsageBit::kUavCompute);
  74. pass.setWork([this, maxVelRt, maxNeighbourVelRt](RenderPassWorkContext& rgraphCtx) {
  75. ANKI_TRACE_SCOPED_EVENT(MotionBlurMaxNeighbourTileVelocity);
  76. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  77. cmdb.bindShaderProgram(m_maxNeightbourVelocityGrProg.get());
  78. rgraphCtx.bindSrv(0, 0, maxVelRt);
  79. rgraphCtx.bindUav(0, 0, maxNeighbourVelRt);
  80. const UVec2 tiledTexSize =
  81. (getRenderer().getPostProcessResolution() + g_cvarRenderMotionBlurTileSize - 1) / g_cvarRenderMotionBlurTileSize;
  82. cmdb.dispatchCompute(tiledTexSize.x, tiledTexSize.y, 1);
  83. });
  84. }
  85. // Recustruct
  86. {
  87. m_runCtx.m_rt = rgraph.newRenderTarget(m_finalRtDesc);
  88. TextureUsageBit readUsage, writeUsage;
  89. RenderPassBase* ppass;
  90. if(g_cvarRenderPreferCompute)
  91. {
  92. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("Motion blur reconstruct");
  93. ppass = &pass;
  94. readUsage = TextureUsageBit::kSrvCompute;
  95. writeUsage = TextureUsageBit::kUavCompute;
  96. }
  97. else
  98. {
  99. GraphicsRenderPass& pass = rgraph.newGraphicsRenderPass("Motion blur reconstruct");
  100. pass.setRenderpassInfo({m_runCtx.m_rt});
  101. ppass = &pass;
  102. readUsage = TextureUsageBit::kSrvPixel;
  103. writeUsage = TextureUsageBit::kRtvDsvWrite;
  104. }
  105. ppass->newTextureDependency(getRenderer().getTonemapping().getRt(), readUsage);
  106. ppass->newTextureDependency(getGBuffer().getDepthRt(), readUsage);
  107. ppass->newTextureDependency(maxNeighbourVelRt, readUsage);
  108. ppass->newTextureDependency(getRenderer().getMotionVectors().getMotionVectorsRt(), readUsage);
  109. ppass->newTextureDependency(m_runCtx.m_rt, writeUsage);
  110. ppass->setWork([this, maxNeighbourVelRt, &ctx](RenderPassWorkContext& rgraphCtx) {
  111. ANKI_TRACE_SCOPED_EVENT(MotionBlurReconstruct);
  112. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  113. cmdb.bindShaderProgram(m_reconstructGrProg.get());
  114. rgraphCtx.bindSrv(0, 0, getRenderer().getTonemapping().getRt());
  115. rgraphCtx.bindSrv(1, 0, getGBuffer().getDepthRt());
  116. rgraphCtx.bindSrv(2, 0, maxNeighbourVelRt);
  117. rgraphCtx.bindSrv(3, 0, getRenderer().getMotionVectors().getMotionVectorsRt());
  118. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearClamp.get());
  119. class Constants
  120. {
  121. public:
  122. Vec2 m_depthLinearizationParams;
  123. U32 m_frame;
  124. F32 m_far;
  125. } consts;
  126. consts.m_depthLinearizationParams.x = (ctx.m_matrices.m_near - ctx.m_matrices.m_far) / ctx.m_matrices.m_near;
  127. consts.m_depthLinearizationParams.y = ctx.m_matrices.m_far / ctx.m_matrices.m_near;
  128. consts.m_frame = getRenderer().getFrameCount() % 32;
  129. consts.m_far = ctx.m_matrices.m_far;
  130. cmdb.setFastConstants(&consts, sizeof(consts));
  131. if(g_cvarRenderPreferCompute)
  132. {
  133. rgraphCtx.bindUav(0, 0, m_runCtx.m_rt);
  134. dispatchPPCompute(cmdb, 8, 8, getRenderer().getPostProcessResolution().x, getRenderer().getPostProcessResolution().y);
  135. }
  136. else
  137. {
  138. cmdb.setViewport(0, 0, getRenderer().getPostProcessResolution().x, getRenderer().getPostProcessResolution().y);
  139. drawQuad(cmdb);
  140. }
  141. });
  142. }
  143. }
  144. } // end namespace anki