MotionBlur.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 =
  28. getRenderer().create2DRenderTargetDescription(tiledTexSize.x(), tiledTexSize.y(), Format::kR16G16_Sfloat, "MaxTileVelocity");
  29. m_maxVelocityRtDesc.bake();
  30. m_maxNeighbourVelocityRtDesc =
  31. getRenderer().create2DRenderTargetDescription(tiledTexSize.x(), tiledTexSize.y(), Format::kR16G16_Sfloat, "MaxNeighbourTileVelocity");
  32. m_maxNeighbourVelocityRtDesc.bake();
  33. m_finalRtDesc = getRenderer().create2DRenderTargetDescription(
  34. getRenderer().getPostProcessResolution().x(), getRenderer().getPostProcessResolution().y(),
  35. (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR8G8B8_Unorm : Format::kR8G8B8A8_Unorm,
  36. "MotionBlur");
  37. m_finalRtDesc.bake();
  38. return Error::kNone;
  39. }
  40. void MotionBlur::populateRenderGraph(RenderingContext& ctx)
  41. {
  42. ANKI_TRACE_SCOPED_EVENT(MotionBlur);
  43. if(g_cvarRenderMotionBlurSampleCount == 0)
  44. {
  45. m_runCtx.m_rt = {};
  46. return;
  47. }
  48. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  49. // MaxTileVelocity
  50. const RenderTargetHandle maxVelRt = rgraph.newRenderTarget(m_maxVelocityRtDesc);
  51. {
  52. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("Motion blur min velocity");
  53. pass.newTextureDependency(getRenderer().getMotionVectors().getMotionVectorsRt(), TextureUsageBit::kSrvCompute);
  54. pass.newTextureDependency(maxVelRt, TextureUsageBit::kUavCompute);
  55. pass.setWork([this, maxVelRt](RenderPassWorkContext& rgraphCtx) {
  56. ANKI_TRACE_SCOPED_EVENT(MotionBlurMinTileVelocity);
  57. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  58. cmdb.bindShaderProgram(m_maxVelocityGrProg.get());
  59. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearClamp.get());
  60. rgraphCtx.bindSrv(0, 0, getRenderer().getMotionVectors().getMotionVectorsRt());
  61. rgraphCtx.bindUav(0, 0, maxVelRt);
  62. const Vec4 consts(Vec2(getRenderer().getPostProcessResolution()), 0.0f, 0.0f);
  63. cmdb.setFastConstants(&consts, sizeof(consts));
  64. const UVec2 tiledTexSize =
  65. (getRenderer().getPostProcessResolution() + g_cvarRenderMotionBlurTileSize - 1) / g_cvarRenderMotionBlurTileSize;
  66. cmdb.dispatchCompute(tiledTexSize.x(), tiledTexSize.y(), 1);
  67. });
  68. }
  69. // MaxNeighbourTileVelocity
  70. const RenderTargetHandle maxNeighbourVelRt = rgraph.newRenderTarget(m_maxNeighbourVelocityRtDesc);
  71. {
  72. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("Motion blur min neighbour vel");
  73. pass.newTextureDependency(maxVelRt, TextureUsageBit::kSrvCompute);
  74. pass.newTextureDependency(maxNeighbourVelRt, TextureUsageBit::kUavCompute);
  75. pass.setWork([this, maxVelRt, maxNeighbourVelRt](RenderPassWorkContext& rgraphCtx) {
  76. ANKI_TRACE_SCOPED_EVENT(MotionBlurMaxNeighbourTileVelocity);
  77. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  78. cmdb.bindShaderProgram(m_maxNeightbourVelocityGrProg.get());
  79. rgraphCtx.bindSrv(0, 0, maxVelRt);
  80. rgraphCtx.bindUav(0, 0, maxNeighbourVelRt);
  81. const UVec2 tiledTexSize =
  82. (getRenderer().getPostProcessResolution() + g_cvarRenderMotionBlurTileSize - 1) / g_cvarRenderMotionBlurTileSize;
  83. cmdb.dispatchCompute(tiledTexSize.x(), tiledTexSize.y(), 1);
  84. });
  85. }
  86. // Recustruct
  87. {
  88. m_runCtx.m_rt = rgraph.newRenderTarget(m_finalRtDesc);
  89. TextureUsageBit readUsage, writeUsage;
  90. RenderPassBase* ppass;
  91. if(g_cvarRenderPreferCompute)
  92. {
  93. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("Motion blur reconstruct");
  94. ppass = &pass;
  95. readUsage = TextureUsageBit::kSrvCompute;
  96. writeUsage = TextureUsageBit::kUavCompute;
  97. }
  98. else
  99. {
  100. GraphicsRenderPass& pass = rgraph.newGraphicsRenderPass("Motion blur reconstruct");
  101. pass.setRenderpassInfo({m_runCtx.m_rt});
  102. ppass = &pass;
  103. readUsage = TextureUsageBit::kSrvPixel;
  104. writeUsage = TextureUsageBit::kRtvDsvWrite;
  105. }
  106. ppass->newTextureDependency(getRenderer().getTonemapping().getRt(), readUsage);
  107. ppass->newTextureDependency(getGBuffer().getDepthRt(), readUsage);
  108. ppass->newTextureDependency(maxNeighbourVelRt, readUsage);
  109. ppass->newTextureDependency(getRenderer().getMotionVectors().getMotionVectorsRt(), readUsage);
  110. ppass->newTextureDependency(m_runCtx.m_rt, writeUsage);
  111. ppass->setWork([this, maxNeighbourVelRt, &ctx](RenderPassWorkContext& rgraphCtx) {
  112. ANKI_TRACE_SCOPED_EVENT(MotionBlurReconstruct);
  113. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  114. cmdb.bindShaderProgram(m_reconstructGrProg.get());
  115. rgraphCtx.bindSrv(0, 0, getRenderer().getTonemapping().getRt());
  116. rgraphCtx.bindSrv(1, 0, getGBuffer().getDepthRt());
  117. rgraphCtx.bindSrv(2, 0, maxNeighbourVelRt);
  118. rgraphCtx.bindSrv(3, 0, getRenderer().getMotionVectors().getMotionVectorsRt());
  119. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearClamp.get());
  120. class Constants
  121. {
  122. public:
  123. Vec2 m_depthLinearizationParams;
  124. U32 m_frame;
  125. F32 m_far;
  126. } consts;
  127. consts.m_depthLinearizationParams.x() = (ctx.m_matrices.m_near - ctx.m_matrices.m_far) / ctx.m_matrices.m_near;
  128. consts.m_depthLinearizationParams.y() = ctx.m_matrices.m_far / ctx.m_matrices.m_near;
  129. consts.m_frame = getRenderer().getFrameCount() % 32;
  130. consts.m_far = ctx.m_matrices.m_far;
  131. cmdb.setFastConstants(&consts, sizeof(consts));
  132. if(g_cvarRenderPreferCompute)
  133. {
  134. rgraphCtx.bindUav(0, 0, m_runCtx.m_rt);
  135. dispatchPPCompute(cmdb, 8, 8, getRenderer().getPostProcessResolution().x(), getRenderer().getPostProcessResolution().y());
  136. }
  137. else
  138. {
  139. cmdb.setViewport(0, 0, getRenderer().getPostProcessResolution().x(), getRenderer().getPostProcessResolution().y());
  140. drawQuad(cmdb);
  141. }
  142. });
  143. }
  144. }
  145. } // end namespace anki