DepthDownscale.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/DepthDownscale.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. namespace anki
  9. {
  10. DepthDownscale::~DepthDownscale()
  11. {
  12. if(m_copyToBuff.m_buffAddr)
  13. {
  14. m_copyToBuff.m_buff->unmap();
  15. }
  16. }
  17. Error DepthDownscale::initInternal(const ConfigSet&)
  18. {
  19. const U32 width = m_r->getWidth() >> 1;
  20. const U32 height = m_r->getHeight() >> 1;
  21. m_mipCount = computeMaxMipmapCount2d(width, height, HIERARCHICAL_Z_MIN_HEIGHT);
  22. const U32 lastMipWidth = width >> (m_mipCount - 1);
  23. const U32 lastMipHeight = height >> (m_mipCount - 1);
  24. ANKI_R_LOGI("Initializing HiZ. Mip count %u, last mip size %ux%u", m_mipCount, lastMipWidth, lastMipHeight);
  25. // Create RT descr
  26. TextureInitInfo texInit = m_r->create2DRenderTargetInitInfo(
  27. width, height, Format::R32_SFLOAT, TextureUsageBit::ALL_SAMPLED | TextureUsageBit::IMAGE_COMPUTE_WRITE, "HiZ");
  28. texInit.m_mipmapCount = U8(m_mipCount);
  29. texInit.m_initialUsage = TextureUsageBit::SAMPLED_FRAGMENT;
  30. m_hizTex = m_r->createAndClearRenderTarget(texInit);
  31. // Progs
  32. ANKI_CHECK(getResourceManager().loadResource("Shaders/DepthDownscale.ankiprog", m_prog));
  33. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  34. variantInitInfo.addMutation("SAMPLE_RESOLVE_TYPE", 2);
  35. const ShaderProgramResourceVariant* variant;
  36. m_prog->getOrCreateVariant(variantInitInfo, variant);
  37. m_grProg = variant->getProgram();
  38. // Copy to buffer
  39. {
  40. m_copyToBuff.m_lastMipWidth = lastMipWidth;
  41. m_copyToBuff.m_lastMipHeight = lastMipHeight;
  42. // Create buffer
  43. BufferInitInfo buffInit("HiZ Client");
  44. buffInit.m_mapAccess = BufferMapAccessBit::READ;
  45. buffInit.m_size = PtrSize(lastMipHeight) * PtrSize(lastMipWidth) * sizeof(F32);
  46. buffInit.m_usage = BufferUsageBit::STORAGE_COMPUTE_WRITE;
  47. m_copyToBuff.m_buff = getGrManager().newBuffer(buffInit);
  48. m_copyToBuff.m_buffAddr = m_copyToBuff.m_buff->map(0, buffInit.m_size, BufferMapAccessBit::READ);
  49. // Fill the buffer with 1.0f
  50. for(U32 i = 0; i < lastMipHeight * lastMipWidth; ++i)
  51. {
  52. static_cast<F32*>(m_copyToBuff.m_buffAddr)[i] = 1.0f;
  53. }
  54. }
  55. return Error::NONE;
  56. }
  57. Error DepthDownscale::init(const ConfigSet& cfg)
  58. {
  59. ANKI_R_LOGI("Initializing depth downscale passes");
  60. Error err = initInternal(cfg);
  61. if(err)
  62. {
  63. ANKI_R_LOGE("Failed to initialize depth downscale passes");
  64. }
  65. return err;
  66. }
  67. void DepthDownscale::importRenderTargets(RenderingContext& ctx)
  68. {
  69. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  70. // Import RT
  71. if(m_hizTexImportedOnce)
  72. {
  73. m_runCtx.m_hizRt = rgraph.importRenderTarget(m_hizTex);
  74. }
  75. else
  76. {
  77. m_runCtx.m_hizRt = rgraph.importRenderTarget(m_hizTex, TextureUsageBit::SAMPLED_FRAGMENT);
  78. m_hizTexImportedOnce = true;
  79. }
  80. }
  81. void DepthDownscale::populateRenderGraph(RenderingContext& ctx)
  82. {
  83. RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
  84. m_runCtx.m_mip = 0;
  85. static const Array<CString, 5> passNames = {"HiZ #0", "HiZ #1", "HiZ #2", "HiZ #3", "HiZ #4"};
  86. // Every pass can do MIPS_WRITTEN_PER_PASS mips
  87. for(U32 i = 0; i < m_mipCount; i += MIPS_WRITTEN_PER_PASS)
  88. {
  89. const U mipsToFill = (i + 1 < m_mipCount) ? MIPS_WRITTEN_PER_PASS : 1;
  90. ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass(passNames[i / MIPS_WRITTEN_PER_PASS]);
  91. if(i == 0)
  92. {
  93. pass.newDependency({m_r->getGBuffer().getDepthRt(), TextureUsageBit::SAMPLED_COMPUTE,
  94. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH)});
  95. }
  96. else
  97. {
  98. TextureSubresourceInfo subresource;
  99. subresource.m_firstMipmap = i - 1;
  100. pass.newDependency({m_runCtx.m_hizRt, TextureUsageBit::SAMPLED_COMPUTE, subresource});
  101. }
  102. TextureSubresourceInfo subresource;
  103. subresource.m_firstMipmap = i;
  104. pass.newDependency({m_runCtx.m_hizRt, TextureUsageBit::IMAGE_COMPUTE_WRITE, subresource});
  105. if(mipsToFill == MIPS_WRITTEN_PER_PASS)
  106. {
  107. subresource.m_firstMipmap = i + 1;
  108. pass.newDependency({m_runCtx.m_hizRt, TextureUsageBit::IMAGE_COMPUTE_WRITE, subresource});
  109. }
  110. auto callback = [](RenderPassWorkContext& rgraphCtx) {
  111. DepthDownscale* const self = static_cast<DepthDownscale*>(rgraphCtx.m_userData);
  112. self->run(rgraphCtx);
  113. };
  114. pass.setWork(callback, this, 0);
  115. }
  116. }
  117. void DepthDownscale::run(RenderPassWorkContext& rgraphCtx)
  118. {
  119. CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
  120. const U32 level = m_runCtx.m_mip;
  121. m_runCtx.m_mip += MIPS_WRITTEN_PER_PASS;
  122. const U32 mipsToFill = (level + 1 < m_mipCount) ? MIPS_WRITTEN_PER_PASS : 1;
  123. const U32 copyToClientLevel = (level + mipsToFill == m_mipCount) ? mipsToFill - 1 : MAX_U32;
  124. const U32 level0Width = m_r->getWidth() >> (level + 1);
  125. const U32 level0Height = m_r->getHeight() >> (level + 1);
  126. const U32 level1Width = level0Width >> 1;
  127. const U32 level1Height = level0Height >> 1;
  128. cmdb->bindShaderProgram(m_grProg);
  129. // Uniforms
  130. struct PushConsts
  131. {
  132. UVec2 m_level0WriteImgSize;
  133. UVec2 m_level1WriteImgSize;
  134. U32 m_copyToClientLevel;
  135. U32 m_writeLevel1;
  136. U32 m_padding0;
  137. U32 m_padding1;
  138. } regs;
  139. regs.m_level0WriteImgSize = UVec2(level0Width, level0Height);
  140. regs.m_level1WriteImgSize = UVec2(level1Width, level1Height);
  141. regs.m_copyToClientLevel = copyToClientLevel;
  142. regs.m_writeLevel1 = mipsToFill == MIPS_WRITTEN_PER_PASS;
  143. cmdb->setPushConstants(&regs, sizeof(regs));
  144. cmdb->bindSampler(0, 0, m_r->getSamplers().m_nearestNearestClamp);
  145. // Bind input texure
  146. if(level == 0)
  147. {
  148. rgraphCtx.bindTexture(0, 1, m_r->getGBuffer().getDepthRt(),
  149. TextureSubresourceInfo(DepthStencilAspectBit::DEPTH));
  150. }
  151. else
  152. {
  153. TextureSubresourceInfo subresource;
  154. subresource.m_firstMipmap = level - 1;
  155. rgraphCtx.bindTexture(0, 1, m_runCtx.m_hizRt, subresource);
  156. }
  157. // 1st level
  158. TextureSubresourceInfo subresource;
  159. subresource.m_firstMipmap = level;
  160. rgraphCtx.bindImage(0, 2, m_runCtx.m_hizRt, subresource);
  161. // 2nd level
  162. subresource.m_firstMipmap = (mipsToFill == MIPS_WRITTEN_PER_PASS) ? level + 1 : level; // Bind the next or the same
  163. rgraphCtx.bindImage(0, 3, m_runCtx.m_hizRt, subresource);
  164. // Client buffer
  165. cmdb->bindStorageBuffer(0, 4, m_copyToBuff.m_buff, 0, m_copyToBuff.m_buff->getSize());
  166. // Done
  167. dispatchPPCompute(cmdb, 8, 8, level0Width, level0Height);
  168. }
  169. } // end namespace anki