TemporalAA.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/TemporalAA.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/LightShading.h>
  9. #include <AnKi/Renderer/Tonemapping.h>
  10. #include <AnKi/Renderer/MotionVectors.h>
  11. namespace anki {
  12. TemporalAA::TemporalAA(Renderer* r)
  13. : RendererObject(r)
  14. {
  15. }
  16. TemporalAA::~TemporalAA()
  17. {
  18. }
  19. Error TemporalAA::init(const ConfigSet& config)
  20. {
  21. ANKI_R_LOGI("Initializing TAA");
  22. Error err = initInternal(config);
  23. if(err)
  24. {
  25. ANKI_R_LOGE("Failed to init TAA");
  26. }
  27. return Error::NONE;
  28. }
  29. Error TemporalAA::initInternal(const ConfigSet& config)
  30. {
  31. ANKI_CHECK(m_r->getResourceManager().loadResource("Shaders/TemporalAAResolve.ankiprog", m_prog));
  32. for(U32 i = 0; i < 2; ++i)
  33. {
  34. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  35. variantInitInfo.addConstant("VARIANCE_CLIPPING_GAMMA", 2.7f);
  36. variantInitInfo.addConstant("BLEND_FACTOR", 1.0f / 16.0f);
  37. variantInitInfo.addConstant("FB_SIZE",
  38. UVec2(m_r->getInternalResolution().x(), m_r->getInternalResolution().y()));
  39. variantInitInfo.addMutation("VARIANCE_CLIPPING", 1);
  40. variantInitInfo.addMutation("YCBCR", 0);
  41. const ShaderProgramResourceVariant* variant;
  42. m_prog->getOrCreateVariant(variantInitInfo, variant);
  43. m_grProgs[i] = variant->getProgram();
  44. }
  45. for(U i = 0; i < 2; ++i)
  46. {
  47. TextureInitInfo texinit = m_r->create2DRenderTargetInitInfo(
  48. m_r->getInternalResolution().x(), m_r->getInternalResolution().y(),
  49. LIGHT_SHADING_COLOR_ATTACHMENT_PIXEL_FORMAT,
  50. TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::SAMPLED_COMPUTE | TextureUsageBit::IMAGE_COMPUTE_WRITE,
  51. "TemporalAA");
  52. texinit.m_initialUsage = TextureUsageBit::SAMPLED_FRAGMENT;
  53. m_rtTextures[i] = m_r->createAndClearRenderTarget(texinit);
  54. }
  55. m_tonemappedRtDescr =
  56. m_r->create2DRenderTargetDescription(m_r->getInternalResolution().x(), m_r->getInternalResolution().y(),
  57. Format::R8G8B8A8_UNORM, "TemporalAA Tonemapped");
  58. m_tonemappedRtDescr.bake();
  59. return Error::NONE;
  60. }
  61. void TemporalAA::populateRenderGraph(RenderingContext& ctx)
  62. {
  63. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  64. const U32 historyRtIdx = (m_r->getFrameCount() + 1) & 1;
  65. const U32 renderRtIdx = !historyRtIdx;
  66. // Import RTs
  67. if(ANKI_LIKELY(m_rtTexturesImportedOnce[historyRtIdx]))
  68. {
  69. m_runCtx.m_historyRt = rgraph.importRenderTarget(m_rtTextures[historyRtIdx]);
  70. }
  71. else
  72. {
  73. m_runCtx.m_historyRt = rgraph.importRenderTarget(m_rtTextures[historyRtIdx], TextureUsageBit::SAMPLED_FRAGMENT);
  74. m_rtTexturesImportedOnce[historyRtIdx] = true;
  75. }
  76. m_runCtx.m_renderRt = rgraph.importRenderTarget(m_rtTextures[renderRtIdx], TextureUsageBit::NONE);
  77. m_runCtx.m_tonemappedRt = rgraph.newRenderTarget(m_tonemappedRtDescr);
  78. // Create pass
  79. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("TemporalAA");
  80. pass.setWork([this](RenderPassWorkContext& rgraphCtx) {
  81. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  82. cmdb->bindShaderProgram(m_grProgs[m_r->getFrameCount() & 1]);
  83. cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
  84. rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(),
  85. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  86. rgraphCtx.bindColorTexture(0, 2, m_r->getLightShading().getRt());
  87. rgraphCtx.bindColorTexture(0, 3, m_runCtx.m_historyRt);
  88. rgraphCtx.bindColorTexture(0, 4, m_r->getMotionVectors().getMotionVectorsRt());
  89. rgraphCtx.bindImage(0, 5, m_runCtx.m_renderRt, TextureSubresourceInfo());
  90. rgraphCtx.bindImage(0, 6, m_runCtx.m_tonemappedRt, TextureSubresourceInfo());
  91. rgraphCtx.bindUniformBuffer(0, 7, m_r->getTonemapping().getAverageLuminanceBuffer());
  92. dispatchPPCompute(cmdb, 8, 8, m_r->getInternalResolution().x(), m_r->getInternalResolution().y());
  93. });
  94. pass.newDependency({m_runCtx.m_renderRt, TextureUsageBit::IMAGE_COMPUTE_WRITE});
  95. pass.newDependency({m_runCtx.m_tonemappedRt, TextureUsageBit::IMAGE_COMPUTE_WRITE});
  96. pass.newDependency({m_r->getGBuffer().getDepthRt(), TextureUsageBit::SAMPLED_COMPUTE,
  97. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH)});
  98. pass.newDependency({m_r->getLightShading().getRt(), TextureUsageBit::SAMPLED_COMPUTE});
  99. pass.newDependency({m_runCtx.m_historyRt, TextureUsageBit::SAMPLED_COMPUTE});
  100. pass.newDependency({m_r->getMotionVectors().getMotionVectorsRt(), TextureUsageBit::SAMPLED_COMPUTE});
  101. }
  102. } // end namespace anki