DownscaleBlur.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/DownscaleBlur.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/TemporalAA.h>
  8. #include <AnKi/Core/ConfigSet.h>
  9. namespace anki {
  10. DownscaleBlur::~DownscaleBlur()
  11. {
  12. m_fbDescrs.destroy(getAllocator());
  13. }
  14. Error DownscaleBlur::init()
  15. {
  16. const Error err = initInternal();
  17. if(err)
  18. {
  19. ANKI_R_LOGE("Failed to initialize downscale blur");
  20. }
  21. return err;
  22. }
  23. Error DownscaleBlur::initInternal()
  24. {
  25. m_passCount = computeMaxMipmapCount2d(m_r->getPostProcessResolution().x(), m_r->getPostProcessResolution().y(),
  26. DOWNSCALE_BLUR_DOWN_TO)
  27. - 1;
  28. const UVec2 rez = m_r->getPostProcessResolution() / 2;
  29. ANKI_R_LOGV("Initializing downscale pyramid. Resolution %ux%u, mip count %u", rez.x(), rez.y(), m_passCount);
  30. const Bool preferCompute = getConfig().getRPreferCompute();
  31. // Create the miped texture
  32. TextureInitInfo texinit =
  33. m_r->create2DRenderTargetDescription(rez.x(), rez.y(), m_r->getHdrFormat(), "DownscaleBlur");
  34. texinit.m_usage = TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::SAMPLED_COMPUTE;
  35. if(preferCompute)
  36. {
  37. texinit.m_usage |= TextureUsageBit::SAMPLED_COMPUTE | TextureUsageBit::IMAGE_COMPUTE_WRITE;
  38. }
  39. else
  40. {
  41. texinit.m_usage |= TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE;
  42. }
  43. texinit.m_mipmapCount = U8(m_passCount);
  44. m_rtTex = m_r->createAndClearRenderTarget(texinit, TextureUsageBit::SAMPLED_COMPUTE);
  45. // FB descr
  46. if(!preferCompute)
  47. {
  48. m_fbDescrs.create(getAllocator(), m_passCount);
  49. for(U32 pass = 0; pass < m_passCount; ++pass)
  50. {
  51. m_fbDescrs[pass].m_colorAttachmentCount = 1;
  52. m_fbDescrs[pass].m_colorAttachments[0].m_surface.m_level = pass;
  53. m_fbDescrs[pass].bake();
  54. }
  55. }
  56. // Shader programs
  57. ANKI_CHECK(getResourceManager().loadResource((preferCompute) ? "ShaderBinaries/DownscaleBlurCompute.ankiprogbin"
  58. : "ShaderBinaries/DownscaleBlurRaster.ankiprogbin",
  59. m_prog));
  60. const ShaderProgramResourceVariant* variant = nullptr;
  61. m_prog->getOrCreateVariant(variant);
  62. m_grProg = variant->getProgram();
  63. return Error::NONE;
  64. }
  65. void DownscaleBlur::importRenderTargets(RenderingContext& ctx)
  66. {
  67. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  68. m_runCtx.m_rt = rgraph.importRenderTarget(m_rtTex, TextureUsageBit::SAMPLED_COMPUTE);
  69. }
  70. void DownscaleBlur::populateRenderGraph(RenderingContext& ctx)
  71. {
  72. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  73. // Create passes
  74. static const Array<CString, 8> passNames = {"DownBlur #0", "Down/Blur #1", "Down/Blur #2", "Down/Blur #3",
  75. "Down/Blur #4", "Down/Blur #5", "Down/Blur #6", "Down/Blur #7"};
  76. if(getConfig().getRPreferCompute())
  77. {
  78. for(U32 i = 0; i < m_passCount; ++i)
  79. {
  80. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass(passNames[i]);
  81. pass.setWork([this, i](RenderPassWorkContext& rgraphCtx) {
  82. run(i, rgraphCtx);
  83. });
  84. if(i > 0)
  85. {
  86. TextureSubresourceInfo sampleSubresource;
  87. TextureSubresourceInfo renderSubresource;
  88. sampleSubresource.m_firstMipmap = i - 1;
  89. renderSubresource.m_firstMipmap = i;
  90. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::IMAGE_COMPUTE_WRITE, renderSubresource});
  91. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::SAMPLED_COMPUTE, sampleSubresource});
  92. }
  93. else
  94. {
  95. TextureSubresourceInfo renderSubresource;
  96. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::IMAGE_COMPUTE_WRITE, renderSubresource});
  97. pass.newDependency({m_r->getTemporalAA().getHdrRt(), TextureUsageBit::SAMPLED_COMPUTE});
  98. }
  99. }
  100. }
  101. else
  102. {
  103. for(U32 i = 0; i < m_passCount; ++i)
  104. {
  105. GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass(passNames[i]);
  106. pass.setWork([this, i](RenderPassWorkContext& rgraphCtx) {
  107. run(i, rgraphCtx);
  108. });
  109. pass.setFramebufferInfo(m_fbDescrs[i], {m_runCtx.m_rt});
  110. if(i > 0)
  111. {
  112. TextureSubresourceInfo sampleSubresource;
  113. TextureSubresourceInfo renderSubresource;
  114. sampleSubresource.m_firstMipmap = i - 1;
  115. renderSubresource.m_firstMipmap = i;
  116. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, renderSubresource});
  117. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::SAMPLED_FRAGMENT, sampleSubresource});
  118. }
  119. else
  120. {
  121. TextureSubresourceInfo renderSubresource;
  122. pass.newDependency({m_runCtx.m_rt, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, renderSubresource});
  123. pass.newDependency({m_r->getTemporalAA().getHdrRt(), TextureUsageBit::SAMPLED_FRAGMENT});
  124. }
  125. }
  126. }
  127. }
  128. void DownscaleBlur::run(U32 passIdx, RenderPassWorkContext& rgraphCtx)
  129. {
  130. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  131. cmdb->bindShaderProgram(m_grProg);
  132. const U32 vpWidth = m_rtTex->getWidth() >> passIdx;
  133. const U32 vpHeight = m_rtTex->getHeight() >> passIdx;
  134. cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
  135. if(passIdx > 0)
  136. {
  137. TextureSubresourceInfo sampleSubresource;
  138. sampleSubresource.m_firstMipmap = passIdx - 1;
  139. rgraphCtx.bindTexture(0, 1, m_runCtx.m_rt, sampleSubresource);
  140. }
  141. else
  142. {
  143. rgraphCtx.bindColorTexture(0, 1, m_r->getTemporalAA().getHdrRt());
  144. }
  145. if(getConfig().getRPreferCompute())
  146. {
  147. TextureSubresourceInfo sampleSubresource;
  148. sampleSubresource.m_firstMipmap = passIdx;
  149. rgraphCtx.bindImage(0, 2, m_runCtx.m_rt, sampleSubresource);
  150. UVec4 fbSize(vpWidth, vpHeight, 0, 0);
  151. cmdb->setPushConstants(&fbSize, sizeof(fbSize));
  152. dispatchPPCompute(cmdb, m_workgroupSize[0], m_workgroupSize[1], vpWidth, vpHeight);
  153. }
  154. else
  155. {
  156. cmdb->setViewport(0, 0, vpWidth, vpHeight);
  157. cmdb->drawArrays(PrimitiveTopology::TRIANGLES, 3);
  158. }
  159. }
  160. } // end namespace anki