2
0

MotionVectors.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2009-2021, 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. namespace anki {
  10. MotionVectors::~MotionVectors()
  11. {
  12. }
  13. Error MotionVectors::init(const ConfigSet& config)
  14. {
  15. ANKI_R_LOGI("Initializing motion vectors");
  16. // Prog
  17. ANKI_CHECK(getResourceManager().loadResource("Shaders/MotionVectors.ankiprog", m_prog));
  18. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  19. variantInitInfo.addConstant("FB_SIZE", UVec2(m_r->getInternalResolution().x(), m_r->getInternalResolution().y()));
  20. const ShaderProgramResourceVariant* variant;
  21. m_prog->getOrCreateVariant(variantInitInfo, variant);
  22. m_grProg = variant->getProgram();
  23. // RTs
  24. m_motionVectorsRtDescr = m_r->create2DRenderTargetDescription(
  25. m_r->getInternalResolution().x(), m_r->getInternalResolution().y(), Format::R16G16_SFLOAT, "Motion vectors");
  26. m_motionVectorsRtDescr.bake();
  27. m_rejectionFactorRtDescr = m_r->create2DRenderTargetDescription(
  28. m_r->getInternalResolution().x(), m_r->getInternalResolution().y(), Format::R8_UNORM, "Motion vectors rej");
  29. m_rejectionFactorRtDescr.bake();
  30. return Error::NONE;
  31. }
  32. void MotionVectors::populateRenderGraph(RenderingContext& ctx)
  33. {
  34. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  35. m_runCtx.m_motionVectorsRtHandle = rgraph.newRenderTarget(m_motionVectorsRtDescr);
  36. m_runCtx.m_rejectionFactorRtHandle = rgraph.newRenderTarget(m_rejectionFactorRtDescr);
  37. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("Motion vectors");
  38. pass.setWork([this, &ctx](RenderPassWorkContext& rgraphCtx) -> void {
  39. run(ctx, rgraphCtx);
  40. });
  41. pass.newDependency({m_runCtx.m_motionVectorsRtHandle, TextureUsageBit::IMAGE_COMPUTE_WRITE});
  42. pass.newDependency({m_runCtx.m_rejectionFactorRtHandle, TextureUsageBit::IMAGE_COMPUTE_WRITE});
  43. pass.newDependency({m_r->getGBuffer().getColorRt(3), TextureUsageBit::SAMPLED_COMPUTE});
  44. pass.newDependency({m_r->getGBuffer().getDepthRt(), TextureUsageBit::SAMPLED_COMPUTE});
  45. pass.newDependency({m_r->getGBuffer().getPreviousFrameDepthRt(), TextureUsageBit::SAMPLED_COMPUTE});
  46. }
  47. void MotionVectors::run(const RenderingContext& ctx, RenderPassWorkContext& rgraphCtx)
  48. {
  49. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  50. cmdb->bindShaderProgram(m_grProg);
  51. cmdb->bindSampler(0, 0, m_r->getSamplers().m_nearestNearestClamp);
  52. rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(), TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  53. rgraphCtx.bindTexture(0, 2, m_r->getGBuffer().getPreviousFrameDepthRt(),
  54. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  55. rgraphCtx.bindColorTexture(0, 3, m_r->getGBuffer().getColorRt(3));
  56. rgraphCtx.bindImage(0, 4, m_runCtx.m_motionVectorsRtHandle, TextureSubresourceInfo());
  57. rgraphCtx.bindImage(0, 5, m_runCtx.m_rejectionFactorRtHandle, TextureSubresourceInfo());
  58. class PC
  59. {
  60. public:
  61. Mat4 m_reprojectionMat;
  62. Mat4 m_prevViewProjectionInvMat;
  63. } pc;
  64. pc.m_reprojectionMat = ctx.m_matrices.m_reprojection;
  65. pc.m_prevViewProjectionInvMat = ctx.m_prevMatrices.m_invertedProjectionJitter;
  66. cmdb->setPushConstants(&pc, sizeof(pc));
  67. dispatchPPCompute(cmdb, 8, 8, m_r->getInternalResolution().x(), m_r->getInternalResolution().y());
  68. }
  69. } // end namespace anki