MotionVectors.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2009-2022, 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/MotionVectors.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/RenderQueue.h>
  9. #include <AnKi/Core/ConfigSet.h>
  10. namespace anki {
  11. MotionVectors::~MotionVectors()
  12. {
  13. }
  14. Error MotionVectors::init()
  15. {
  16. const Error err = initInternal();
  17. if(err)
  18. {
  19. ANKI_R_LOGE("Failed to initialize motion vectors");
  20. }
  21. return err;
  22. }
  23. Error MotionVectors::initInternal()
  24. {
  25. ANKI_R_LOGV("Initializing motion vectors");
  26. // Prog
  27. ANKI_CHECK(getResourceManager().loadResource((getConfig().getRPreferCompute())
  28. ? "ShaderBinaries/MotionVectorsCompute.ankiprogbin"
  29. : "ShaderBinaries/MotionVectorsRaster.ankiprogbin",
  30. m_prog));
  31. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  32. variantInitInfo.addConstant("FB_SIZE", UVec2(m_r->getInternalResolution().x(), m_r->getInternalResolution().y()));
  33. const ShaderProgramResourceVariant* variant;
  34. m_prog->getOrCreateVariant(variantInitInfo, variant);
  35. m_grProg = variant->getProgram();
  36. // RTs
  37. m_motionVectorsRtDescr = m_r->create2DRenderTargetDescription(
  38. m_r->getInternalResolution().x(), m_r->getInternalResolution().y(), Format::kR16G16_Sfloat, "MotionVectors");
  39. m_motionVectorsRtDescr.bake();
  40. TextureUsageBit historyLengthUsage = TextureUsageBit::kAllSampled;
  41. if(getConfig().getRPreferCompute())
  42. {
  43. historyLengthUsage |= TextureUsageBit::kImageComputeWrite;
  44. }
  45. else
  46. {
  47. historyLengthUsage |= TextureUsageBit::kFramebufferWrite;
  48. }
  49. TextureInitInfo historyLengthTexInit =
  50. m_r->create2DRenderTargetInitInfo(m_r->getInternalResolution().x(), m_r->getInternalResolution().y(),
  51. Format::kR8_Unorm, historyLengthUsage, "MotionVectorsHistoryLen#1");
  52. m_historyLengthTextures[0] = m_r->createAndClearRenderTarget(historyLengthTexInit, TextureUsageBit::kAllSampled);
  53. historyLengthTexInit.setName("MotionVectorsHistoryLen#2");
  54. m_historyLengthTextures[1] = m_r->createAndClearRenderTarget(historyLengthTexInit, TextureUsageBit::kAllSampled);
  55. m_fbDescr.m_colorAttachmentCount = 2;
  56. m_fbDescr.bake();
  57. return Error::NONE;
  58. }
  59. void MotionVectors::populateRenderGraph(RenderingContext& ctx)
  60. {
  61. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  62. m_runCtx.m_motionVectorsRtHandle = rgraph.newRenderTarget(m_motionVectorsRtDescr);
  63. const U32 writeHistoryLenTexIdx = m_r->getFrameCount() & 1;
  64. const U32 readHistoryLenTexIdx = !writeHistoryLenTexIdx;
  65. if(ANKI_LIKELY(m_historyLengthTexturesImportedOnce))
  66. {
  67. m_runCtx.m_historyLengthWriteRtHandle =
  68. rgraph.importRenderTarget(m_historyLengthTextures[writeHistoryLenTexIdx]);
  69. m_runCtx.m_historyLengthReadRtHandle = rgraph.importRenderTarget(m_historyLengthTextures[readHistoryLenTexIdx]);
  70. }
  71. else
  72. {
  73. m_runCtx.m_historyLengthWriteRtHandle =
  74. rgraph.importRenderTarget(m_historyLengthTextures[writeHistoryLenTexIdx], TextureUsageBit::kAllSampled);
  75. m_runCtx.m_historyLengthReadRtHandle =
  76. rgraph.importRenderTarget(m_historyLengthTextures[readHistoryLenTexIdx], TextureUsageBit::kAllSampled);
  77. m_historyLengthTexturesImportedOnce = true;
  78. }
  79. RenderPassDescriptionBase* ppass;
  80. TextureUsageBit readUsage;
  81. TextureUsageBit writeUsage;
  82. if(getConfig().getRPreferCompute())
  83. {
  84. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("MotionVectors");
  85. readUsage = TextureUsageBit::kSampledCompute;
  86. writeUsage = TextureUsageBit::kImageComputeWrite;
  87. ppass = &pass;
  88. }
  89. else
  90. {
  91. GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("MotionVectors");
  92. pass.setFramebufferInfo(m_fbDescr, {m_runCtx.m_motionVectorsRtHandle, m_runCtx.m_historyLengthWriteRtHandle});
  93. readUsage = TextureUsageBit::kSampledFragment;
  94. writeUsage = TextureUsageBit::kFramebufferWrite;
  95. ppass = &pass;
  96. }
  97. ppass->setWork([this, &ctx](RenderPassWorkContext& rgraphCtx) -> void {
  98. run(ctx, rgraphCtx);
  99. });
  100. ppass->newDependency(RenderPassDependency(m_runCtx.m_motionVectorsRtHandle, writeUsage));
  101. ppass->newDependency(RenderPassDependency(m_runCtx.m_historyLengthWriteRtHandle, writeUsage));
  102. ppass->newDependency(RenderPassDependency(m_runCtx.m_historyLengthReadRtHandle, readUsage));
  103. ppass->newDependency(RenderPassDependency(m_r->getGBuffer().getColorRt(3), readUsage));
  104. ppass->newDependency(RenderPassDependency(m_r->getGBuffer().getDepthRt(), readUsage));
  105. ppass->newDependency(RenderPassDependency(m_r->getGBuffer().getPreviousFrameDepthRt(), readUsage));
  106. }
  107. void MotionVectors::run(const RenderingContext& ctx, RenderPassWorkContext& rgraphCtx)
  108. {
  109. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  110. cmdb->bindShaderProgram(m_grProg);
  111. cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
  112. rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(), TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
  113. rgraphCtx.bindTexture(0, 2, m_r->getGBuffer().getPreviousFrameDepthRt(),
  114. TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
  115. rgraphCtx.bindColorTexture(0, 3, m_r->getGBuffer().getColorRt(3));
  116. rgraphCtx.bindColorTexture(0, 4, m_runCtx.m_historyLengthReadRtHandle);
  117. class Uniforms
  118. {
  119. public:
  120. Mat4 m_reprojectionMat;
  121. Mat4 m_viewProjectionInvMat;
  122. Mat4 m_prevViewProjectionInvMat;
  123. } * pc;
  124. pc = allocateAndBindUniforms<Uniforms*>(sizeof(*pc), cmdb, 0, 5);
  125. pc->m_reprojectionMat = ctx.m_matrices.m_reprojection;
  126. pc->m_viewProjectionInvMat = ctx.m_matrices.m_invertedViewProjectionJitter;
  127. pc->m_prevViewProjectionInvMat = ctx.m_prevMatrices.m_invertedViewProjectionJitter;
  128. if(getConfig().getRPreferCompute())
  129. {
  130. rgraphCtx.bindImage(0, 6, m_runCtx.m_motionVectorsRtHandle, TextureSubresourceInfo());
  131. rgraphCtx.bindImage(0, 7, m_runCtx.m_historyLengthWriteRtHandle, TextureSubresourceInfo());
  132. }
  133. if(getConfig().getRPreferCompute())
  134. {
  135. dispatchPPCompute(cmdb, 8, 8, m_r->getInternalResolution().x(), m_r->getInternalResolution().y());
  136. }
  137. else
  138. {
  139. cmdb->setViewport(0, 0, m_r->getInternalResolution().x(), m_r->getInternalResolution().y());
  140. cmdb->drawArrays(PrimitiveTopology::kTriangles, 3);
  141. }
  142. }
  143. } // end namespace anki