3
0

DownsampleMipChainPass.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <math.h>
  9. #include <AtomCore/Instance/InstanceDatabase.h>
  10. #include <AtomCore/std/containers/vector_set.h>
  11. #include <Atom/RHI/FrameGraphBuilder.h>
  12. #include <Atom/RHI/FrameGraphAttachmentInterface.h>
  13. #include <Atom/RHI.Reflect/ImageViewDescriptor.h>
  14. #include <Atom/RPI.Public/Pass/ComputePass.h>
  15. #include <Atom/RPI.Public/Pass/PassDefines.h>
  16. #include <Atom/RPI.Public/Pass/PassFactory.h>
  17. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  18. #include <Atom/RPI.Public/Pass/PassUtils.h>
  19. #include <Atom/RPI.Public/Pass/Specific/DownsampleMipChainPass.h>
  20. #include <Atom/RPI.Public/RenderPipeline.h>
  21. #include <Atom/RPI.Reflect/Pass/ComputePassData.h>
  22. #include <Atom/RPI.Reflect/Pass/PassRequest.h>
  23. namespace AZ
  24. {
  25. namespace RPI
  26. {
  27. Ptr<DownsampleMipChainPass> DownsampleMipChainPass::Create(const PassDescriptor& descriptor)
  28. {
  29. Ptr<DownsampleMipChainPass> pass = aznew DownsampleMipChainPass(descriptor);
  30. return pass;
  31. }
  32. DownsampleMipChainPass::DownsampleMipChainPass(const PassDescriptor& descriptor)
  33. : ParentPass(descriptor)
  34. {
  35. // Load DownsampleMipChainPassData
  36. const DownsampleMipChainPassData* passData = PassUtils::GetPassData<DownsampleMipChainPassData>(descriptor);
  37. if (passData == nullptr)
  38. {
  39. AZ_Error("PassSystem", false, "[DownsampleMipChainPass '%s']: Trying to construct without valid DownsampleMipChainPassData!",
  40. GetPathName().GetCStr());
  41. return;
  42. }
  43. m_passData = *passData;
  44. ShaderReloadNotificationBus::Handler::BusConnect(passData->m_shaderReference.m_assetId);
  45. }
  46. DownsampleMipChainPass::~DownsampleMipChainPass()
  47. {
  48. ShaderReloadNotificationBus::Handler::BusDisconnect();
  49. }
  50. void DownsampleMipChainPass::ResetInternal()
  51. {
  52. RemoveChildren();
  53. }
  54. void DownsampleMipChainPass::GetInputInfo()
  55. {
  56. // Get the input/output mip chain attachment for this pass (at binding 0)
  57. AZ_Assert(GetInputOutputCount() > 0, "[DownsampleMipChainPass '%s']: must have an input/output", GetPathName().GetCStr());
  58. PassAttachment* attachment = GetInputOutputBinding(0).GetAttachment().get();
  59. if (attachment != nullptr)
  60. {
  61. // Check if we need to rebuild children because the number of mips has changed
  62. m_needToRebuildChildren = m_needToRebuildChildren || (m_mipLevels != attachment->m_descriptor.m_image.m_mipLevels);
  63. // Check if we need to update children because the image dimensions have changed
  64. m_needToUpdateChildren |= (m_inputWidth != attachment->m_descriptor.m_image.m_size.m_width);
  65. m_needToUpdateChildren |= (m_inputHeight != attachment->m_descriptor.m_image.m_size.m_height);
  66. m_needToUpdateChildren |= m_needToRebuildChildren;
  67. // Get parameters from the image descriptor
  68. m_mipLevels = attachment->m_descriptor.m_image.m_mipLevels;
  69. m_inputWidth = attachment->m_descriptor.m_image.m_size.m_width;
  70. m_inputHeight = attachment->m_descriptor.m_image.m_size.m_height;
  71. }
  72. }
  73. void DownsampleMipChainPass::BuildChildPasses()
  74. {
  75. RemoveChildren();
  76. PassSystemInterface* passSystem = PassSystemInterface::Get();
  77. PassDescriptor childDesc;
  78. childDesc.m_passData = AZStd::make_shared<ComputePassData>();
  79. ComputePassData* passData = static_cast<ComputePassData*>(childDesc.m_passData.get());
  80. passData->m_shaderReference = m_passData.m_shaderReference;
  81. PassAttachmentBinding& inOutBinding = GetInputOutputBinding(0);
  82. const Ptr<PassAttachment>& inOutAttachment = inOutBinding.GetAttachment();
  83. // We are downsampling a mip chain where the first mip is already written to
  84. // Create passes to write to the other [mipLevels - 1] mips
  85. for(uint16_t mip = 0; mip < m_mipLevels - 1; ++mip)
  86. {
  87. // Generate name based on mip
  88. AZStd::string childName = AZStd::string::format("DownSample%d", mip);
  89. childDesc.m_passName = Name(childName);
  90. // Create Pass
  91. Ptr<ComputePass> childPass = passSystem->CreatePass<ComputePass>(childDesc);
  92. // Create input binding for child
  93. {
  94. PassAttachmentBinding inBinding;
  95. inBinding.m_name = "Input";
  96. inBinding.m_slotType = PassSlotType::Input;
  97. inBinding.m_scopeAttachmentUsage = RHI::ScopeAttachmentUsage::Shader;
  98. inBinding.m_connectedBinding = &inOutBinding;
  99. RHI::ImageViewDescriptor inViewDesc;
  100. inViewDesc.m_mipSliceMin = mip;
  101. inViewDesc.m_mipSliceMax = mip;
  102. inBinding.m_unifiedScopeDesc.SetAsImage(inViewDesc);
  103. inBinding.SetAttachment(inOutAttachment);
  104. childPass->AddAttachmentBinding(inBinding);
  105. }
  106. // Create output binding for child
  107. {
  108. PassAttachmentBinding outBinding;
  109. outBinding.m_name = "Output";
  110. outBinding.m_slotType = PassSlotType::InputOutput;
  111. outBinding.m_scopeAttachmentUsage = RHI::ScopeAttachmentUsage::Shader;
  112. outBinding.m_connectedBinding = &inOutBinding;
  113. RHI::ImageViewDescriptor outViewDesc;
  114. outViewDesc.m_mipSliceMin = mip + 1;
  115. outViewDesc.m_mipSliceMax = mip + 1;
  116. outBinding.m_unifiedScopeDesc.SetAsImage(outViewDesc);
  117. outBinding.SetAttachment(inOutAttachment);
  118. childPass->AddAttachmentBinding(outBinding);
  119. }
  120. AddChild(childPass);
  121. }
  122. m_needToRebuildChildren = false;
  123. }
  124. void DownsampleMipChainPass::UpdateChildren()
  125. {
  126. AZ_Assert(m_children.size() == m_mipLevels - 1,
  127. "[DownsampleMipChainPass '%s']: number of child passes (%d) does not match number of mips (%d)",
  128. GetPathName().GetCStr(),
  129. m_children.size(),
  130. m_mipLevels - 1);
  131. uint32_t sourceWidth = m_inputWidth;
  132. uint32_t sourceHeight = m_inputHeight;
  133. uint32_t targetWidth = (sourceWidth + 1) / 2;
  134. uint32_t targetHeight = (sourceHeight + 1) / 2;
  135. for (const Ptr<Pass>& child : m_children)
  136. {
  137. ComputePass* computeChild = static_cast<ComputePass*>(child.get());
  138. computeChild->SetTargetThreadCounts(targetWidth, targetHeight, 1);
  139. sourceWidth = targetWidth;
  140. sourceHeight = targetHeight;
  141. targetWidth = (sourceWidth + 1) / 2;
  142. targetHeight = (sourceHeight + 1) / 2;
  143. }
  144. m_needToUpdateChildren = false;
  145. }
  146. // Pass behavior functions...
  147. void DownsampleMipChainPass::BuildInternal()
  148. {
  149. GetInputInfo();
  150. BuildChildPasses();
  151. UpdateChildren();
  152. ParentPass::BuildInternal();
  153. }
  154. void DownsampleMipChainPass::FrameBeginInternal(FramePrepareParams params)
  155. {
  156. GetInputInfo();
  157. if (m_needToRebuildChildren)
  158. {
  159. BuildChildPasses();
  160. }
  161. if (m_needToUpdateChildren)
  162. {
  163. UpdateChildren();
  164. }
  165. ParentPass::FrameBeginInternal(params);
  166. }
  167. void DownsampleMipChainPass::OnShaderReinitialized([[maybe_unused]] const Shader& shader)
  168. {
  169. m_needToUpdateChildren = true;
  170. }
  171. void DownsampleMipChainPass::OnShaderAssetReinitialized([[maybe_unused]] const Data::Asset<ShaderAsset>& shaderAsset)
  172. {
  173. m_needToUpdateChildren = true;
  174. }
  175. void DownsampleMipChainPass::OnShaderVariantReinitialized([[maybe_unused]] const ShaderVariant& shaderVariant)
  176. {
  177. m_needToUpdateChildren = true;
  178. }
  179. } // namespace RPI
  180. } // namespace AZ