MotionBlur.cpp 6.6 KB

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