DepthDownscale.cpp 6.2 KB

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