3
0

CopyPass.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <Atom/RHI/CommandList.h>
  9. #include <Atom/RHI/ShaderResourceGroup.h>
  10. #include <Atom/RHI/DrawListTagRegistry.h>
  11. #include <Atom/RHI/RHISystemInterface.h>
  12. #include <Atom/RPI.Public/RenderPipeline.h>
  13. #include <Atom/RPI.Public/RPISystemInterface.h>
  14. #include <Atom/RPI.Public/Scene.h>
  15. #include <Atom/RPI.Public/View.h>
  16. #include <Atom/RPI.Public/Pass/CopyPass.h>
  17. #include <Atom/RPI.Public/Pass/PassUtils.h>
  18. namespace AZ
  19. {
  20. namespace RPI
  21. {
  22. // --- Creation & Initialization ---
  23. Ptr<CopyPass> CopyPass::Create(const PassDescriptor& descriptor)
  24. {
  25. Ptr<CopyPass> pass = aznew CopyPass(descriptor);
  26. return pass;
  27. }
  28. CopyPass::CopyPass(const PassDescriptor& descriptor)
  29. : RenderPass(descriptor)
  30. {
  31. const CopyPassData* copyData = PassUtils::GetPassData<CopyPassData>(descriptor);
  32. if (copyData)
  33. {
  34. m_data = *copyData;
  35. if (copyData->m_useCopyQueue)
  36. {
  37. m_hardwareQueueClass = RHI::HardwareQueueClass::Copy;
  38. }
  39. }
  40. }
  41. RHI::CopyItemType CopyPass::GetCopyItemType()
  42. {
  43. RHI::AttachmentType inputType = GetInputBinding(0).GetAttachment()->GetAttachmentType();
  44. RHI::AttachmentType outputType = GetOutputBinding(0).GetAttachment()->GetAttachmentType();
  45. RHI::CopyItemType copyType = RHI::CopyItemType::Invalid;
  46. if (inputType == RHI::AttachmentType::Buffer && outputType == RHI::AttachmentType::Buffer)
  47. {
  48. copyType = RHI::CopyItemType::Buffer;
  49. }
  50. else if (inputType == RHI::AttachmentType::Image && outputType == RHI::AttachmentType::Image)
  51. {
  52. copyType = RHI::CopyItemType::Image;
  53. }
  54. else if (inputType == RHI::AttachmentType::Buffer && outputType == RHI::AttachmentType::Image)
  55. {
  56. copyType = RHI::CopyItemType::BufferToImage;
  57. }
  58. else if (inputType == RHI::AttachmentType::Image && outputType == RHI::AttachmentType::Buffer)
  59. {
  60. copyType = RHI::CopyItemType::ImageToBuffer;
  61. }
  62. return copyType;
  63. }
  64. // --- Pass behavior overrides ---
  65. void CopyPass::BuildInternal()
  66. {
  67. AZ_Assert(GetInputCount() == 1 && GetOutputCount() == 1,
  68. "CopyPass has %d inputs and %d outputs. It should have exactly one of each.",
  69. GetInputCount(), GetOutputCount());
  70. AZ_Assert(m_attachmentBindings.size() == 2,
  71. "CopyPass must have exactly 2 bindings: 1 input and 1 output. %s has %d bindings.",
  72. GetPathName().GetCStr(), m_attachmentBindings.size());
  73. // Create transient attachment based on input if required
  74. if (m_data.m_cloneInput)
  75. {
  76. const Ptr<PassAttachment>& source = GetInputBinding(0).GetAttachment();
  77. Ptr<PassAttachment> dest = source->Clone();
  78. // Set bind flags to CopyWrite. Other bind flags will be auto-inferred by pass system
  79. if (dest->m_descriptor.m_type == RHI::AttachmentType::Image)
  80. {
  81. dest->m_descriptor.m_image.m_bindFlags = RHI::ImageBindFlags::CopyWrite;
  82. }
  83. else if (dest->m_descriptor.m_type == RHI::AttachmentType::Buffer)
  84. {
  85. dest->m_descriptor.m_buffer.m_bindFlags = RHI::BufferBindFlags::CopyWrite;
  86. }
  87. // Set path name for the new attachment and add it to our attachment list
  88. dest->ComputePathName(GetPathName());
  89. m_ownedAttachments.push_back(dest);
  90. // Set the output binding to the new attachment
  91. GetOutputBinding(0).SetAttachment(dest);
  92. }
  93. }
  94. // --- Scope producer functions ---
  95. void CopyPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  96. {
  97. RenderPass::SetupFrameGraphDependencies(frameGraph);
  98. }
  99. void CopyPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  100. {
  101. RHI::CopyItemType copyType = GetCopyItemType();
  102. switch (copyType)
  103. {
  104. case AZ::RHI::CopyItemType::Buffer:
  105. CopyBuffer(context);
  106. break;
  107. case AZ::RHI::CopyItemType::Image:
  108. CopyImage(context);
  109. break;
  110. case AZ::RHI::CopyItemType::BufferToImage:
  111. CopyBufferToImage(context);
  112. break;
  113. case AZ::RHI::CopyItemType::ImageToBuffer:
  114. CopyImageToBuffer(context);
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. void CopyPass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)
  121. {
  122. if (m_copyItem.m_type != RHI::CopyItemType::Invalid)
  123. {
  124. context.GetCommandList()->Submit(m_copyItem);
  125. }
  126. }
  127. // --- Copy setup functions ---
  128. void CopyPass::CopyBuffer(const RHI::FrameGraphCompileContext& context)
  129. {
  130. RHI::CopyBufferDescriptor copyDesc;
  131. // Source Buffer
  132. PassAttachmentBinding& copySource = GetInputBinding(0);
  133. const AZ::RHI::Buffer* sourceBuffer = context.GetBuffer(copySource.GetAttachment()->GetAttachmentId());
  134. copyDesc.m_sourceBuffer = sourceBuffer;
  135. copyDesc.m_size = static_cast<uint32_t>(sourceBuffer->GetDescriptor().m_byteCount);
  136. copyDesc.m_sourceOffset = m_data.m_bufferSourceOffset;
  137. // Destination Buffer
  138. PassAttachmentBinding& copyDest = GetOutputBinding(0);
  139. copyDesc.m_destinationBuffer = context.GetBuffer(copyDest.GetAttachment()->GetAttachmentId());
  140. copyDesc.m_destinationOffset = m_data.m_bufferDestinationOffset;
  141. m_copyItem = copyDesc;
  142. }
  143. void CopyPass::CopyImage(const RHI::FrameGraphCompileContext& context)
  144. {
  145. RHI::CopyImageDescriptor copyDesc;
  146. // Source Image
  147. PassAttachmentBinding& copySource = GetInputBinding(0);
  148. const AZ::RHI::Image* sourceImage = context.GetImage(copySource.GetAttachment()->GetAttachmentId());
  149. copyDesc.m_sourceImage = sourceImage;
  150. copyDesc.m_sourceSize = sourceImage->GetDescriptor().m_size;
  151. copyDesc.m_sourceOrigin = m_data.m_imageSourceOrigin;
  152. copyDesc.m_sourceSubresource = m_data.m_imageSourceSubresource;
  153. // Destination Image
  154. PassAttachmentBinding& copyDest = GetOutputBinding(0);
  155. copyDesc.m_destinationImage = context.GetImage(copyDest.GetAttachment()->GetAttachmentId());
  156. copyDesc.m_destinationOrigin = m_data.m_imageDestinationOrigin;
  157. copyDesc.m_destinationSubresource = m_data.m_imageDestinationSubresource;
  158. m_copyItem = copyDesc;
  159. }
  160. void CopyPass::CopyBufferToImage(const RHI::FrameGraphCompileContext& context)
  161. {
  162. RHI::CopyBufferToImageDescriptor copyDesc;
  163. // Source Buffer
  164. PassAttachmentBinding& copySource = GetInputBinding(0);
  165. const AZ::RHI::Buffer* sourceBuffer = context.GetBuffer(copySource.GetAttachment()->GetAttachmentId());
  166. copyDesc.m_sourceBuffer = sourceBuffer;
  167. copyDesc.m_sourceSize = m_data.m_sourceSize;
  168. copyDesc.m_sourceOffset = m_data.m_bufferSourceOffset;
  169. copyDesc.m_sourceBytesPerRow = m_data.m_bufferSourceBytesPerRow;
  170. copyDesc.m_sourceBytesPerImage = m_data.m_bufferSourceBytesPerImage;
  171. // Destination Image
  172. PassAttachmentBinding& copyDest = GetOutputBinding(0);
  173. copyDesc.m_destinationImage = context.GetImage(copyDest.GetAttachment()->GetAttachmentId());
  174. copyDesc.m_destinationOrigin = m_data.m_imageDestinationOrigin;
  175. copyDesc.m_destinationSubresource = m_data.m_imageDestinationSubresource;
  176. m_copyItem = copyDesc;
  177. }
  178. void CopyPass::CopyImageToBuffer(const RHI::FrameGraphCompileContext& context)
  179. {
  180. RHI::CopyImageToBufferDescriptor copyDesc;
  181. // Source Image
  182. PassAttachmentBinding& copySource = GetInputBinding(0);
  183. const AZ::RHI::Image* sourceImage = context.GetImage(copySource.GetAttachment()->GetAttachmentId());
  184. copyDesc.m_sourceImage = sourceImage;
  185. copyDesc.m_sourceSize = sourceImage->GetDescriptor().m_size;
  186. copyDesc.m_sourceOrigin = m_data.m_imageSourceOrigin;
  187. copyDesc.m_sourceSubresource = m_data.m_imageSourceSubresource;
  188. // Destination Buffer
  189. PassAttachmentBinding& copyDest = GetOutputBinding(0);
  190. copyDesc.m_destinationBuffer = context.GetBuffer(copyDest.GetAttachment()->GetAttachmentId());
  191. copyDesc.m_destinationOffset = m_data.m_bufferDestinationOffset;
  192. copyDesc.m_destinationBytesPerRow = m_data.m_bufferDestinationBytesPerRow;
  193. copyDesc.m_destinationBytesPerImage = m_data.m_bufferDestinationBytesPerImage;
  194. m_copyItem = copyDesc;
  195. }
  196. } // namespace RPI
  197. } // namespace AZ