3
0

PassAttachment.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 <AzFramework/StringFunc/StringFunc.h>
  10. #include <Atom/RHI.Reflect/Bits.h>
  11. #include <Atom/RHI/RHIUtils.h>
  12. #include <Atom/RPI.Public/Pass/PassAttachment.h>
  13. #include <Atom/RPI.Public/RenderPipeline.h>
  14. #include <Atom/RPI.Reflect/Pass/PassName.h>
  15. namespace AZ
  16. {
  17. namespace RPI
  18. {
  19. // --- PassAttachment ---
  20. PassAttachment::PassAttachment(const PassImageAttachmentDesc& attachmentDesc)
  21. {
  22. m_name = attachmentDesc.m_name;
  23. m_lifetime = attachmentDesc.m_lifetime;
  24. m_generateFullMipChain = attachmentDesc.m_generateFullMipChain;
  25. m_descriptor = RHI::UnifiedAttachmentDescriptor(attachmentDesc.m_imageDescriptor);
  26. ValidateDeviceFormats(attachmentDesc.m_formatFallbacks);
  27. }
  28. PassAttachment::PassAttachment(const PassBufferAttachmentDesc& attachmentDesc)
  29. {
  30. m_descriptor = RHI::UnifiedAttachmentDescriptor(attachmentDesc.m_bufferDescriptor);
  31. m_name = attachmentDesc.m_name;
  32. m_lifetime = attachmentDesc.m_lifetime;
  33. }
  34. Ptr<PassAttachment> PassAttachment::Clone() const
  35. {
  36. Ptr<PassAttachment> clone = aznew PassAttachment();
  37. clone->m_name = this->m_name;
  38. clone->m_descriptor = this->m_descriptor;
  39. clone->m_lifetime = this->m_lifetime;
  40. clone->m_formatSource = this->m_formatSource;
  41. clone->m_multisampleSource = this->m_multisampleSource;
  42. clone->m_sizeSource = this->m_sizeSource;
  43. clone->m_sizeMultipliers = this->m_sizeMultipliers;
  44. clone->m_arraySizeSource = this->m_arraySizeSource;
  45. clone->m_generateFullMipChain = this->m_generateFullMipChain;
  46. return clone;
  47. }
  48. void PassAttachment::ValidateDeviceFormats(const AZStd::vector<RHI::Format>& formatFallbacks, RHI::FormatCapabilities capabilities)
  49. {
  50. if (m_descriptor.m_type == RHI::AttachmentType::Image)
  51. {
  52. RHI::Format format = m_descriptor.m_image.m_format;
  53. capabilities |= RHI::FormatCapabilities::Sample;
  54. AZStd::string formatLocation = AZStd::string::format("PassAttachment [%s]", m_name.GetCStr());
  55. m_descriptor.m_image.m_format = RHI::ValidateFormat(format, formatLocation.c_str(), formatFallbacks);
  56. }
  57. }
  58. RHI::AttachmentId PassAttachment::GetAttachmentId() const
  59. {
  60. AZ_Warning(
  61. "PassSystem",
  62. !m_path.IsEmpty(),
  63. "PassAttachment::GetAttachmentId(): Trying to get AttachmentId without a valid path. Make sure you call ComputePathName.");
  64. return m_path;
  65. }
  66. RHI::AttachmentType PassAttachment::GetAttachmentType() const
  67. {
  68. return m_descriptor.m_type;
  69. }
  70. void PassAttachment::ComputePathName(const Name& passPath)
  71. {
  72. m_path = RHI::AttachmentId(ConcatPassString(passPath, m_name));
  73. }
  74. const RHI::TransientImageDescriptor PassAttachment::GetTransientImageDescriptor() const
  75. {
  76. AZ_Assert(
  77. m_lifetime == RHI::AttachmentLifetimeType::Transient,
  78. "Error, building a transient image descriptor from non-transient pass attachment with path: %s",
  79. m_path.GetCStr());
  80. AZ_Assert(
  81. m_descriptor.m_type == RHI::AttachmentType::Image,
  82. "Error, building a transient image descriptor for an attachment that is not an image: %s",
  83. m_path.GetCStr());
  84. return RHI::TransientImageDescriptor(GetAttachmentId(), m_descriptor.m_image);
  85. }
  86. const RHI::TransientBufferDescriptor PassAttachment::GetTransientBufferDescriptor() const
  87. {
  88. AZ_Assert(
  89. m_lifetime == RHI::AttachmentLifetimeType::Transient,
  90. "Error, building a transient image descriptor from non-transient pass attachment with path: %s",
  91. m_path.GetCStr());
  92. AZ_Assert(
  93. m_descriptor.m_type == RHI::AttachmentType::Buffer,
  94. "Error, building a transient buffer descriptor for an attachment that is not a buffer: %s",
  95. m_path.GetCStr());
  96. return RHI::TransientBufferDescriptor(GetAttachmentId(), m_descriptor.m_buffer);
  97. }
  98. void PassAttachment::Update(bool updateImportedAttachments)
  99. {
  100. if (m_descriptor.m_type == RHI::AttachmentType::Image &&
  101. (m_lifetime == RHI::AttachmentLifetimeType::Transient || updateImportedAttachments == true))
  102. {
  103. UpdateImageFormat();
  104. UpdateImageMultisampleState();
  105. UpdateImageSize();
  106. UpdateImageArraySize();
  107. if (m_generateFullMipChain)
  108. {
  109. uint32_t width = m_descriptor.m_image.m_size.m_width;
  110. uint32_t height = m_descriptor.m_image.m_size.m_height;
  111. double maxDimension = static_cast<double>(AZStd::max(width, height));
  112. double mipMapLevels = floor(log2(maxDimension)) + 1;
  113. m_descriptor.m_image.m_mipLevels = static_cast<uint16_t>(mipMapLevels);
  114. }
  115. }
  116. }
  117. void PassAttachment::OnAttached(const PassAttachmentBinding& binding)
  118. {
  119. // Auto-infer image and buffer bind flags...
  120. if (GetAttachmentType() == RHI::AttachmentType::Image)
  121. {
  122. m_descriptor.m_image.m_bindFlags |= RHI::GetImageBindFlags(binding.m_scopeAttachmentUsage, binding.GetAttachmentAccess());
  123. }
  124. else if (GetAttachmentType() == RHI::AttachmentType::Buffer)
  125. {
  126. bool isInputAssembly = RHI::CheckBitsAny(
  127. m_descriptor.m_buffer.m_bindFlags, RHI::BufferBindFlags::InputAssembly | RHI::BufferBindFlags::DynamicInputAssembly);
  128. bool isConstant = RHI::CheckBitsAny(m_descriptor.m_buffer.m_bindFlags, RHI::BufferBindFlags::Constant);
  129. // Since InputAssembly and Constant cannot be inferred they are set manually. If those flags are set we don't want to add
  130. // inferred flags on top as it may have a performance penalty
  131. if (!isInputAssembly && !isConstant)
  132. {
  133. m_descriptor.m_buffer.m_bindFlags |=
  134. RHI::GetBufferBindFlags(binding.m_scopeAttachmentUsage, binding.GetAttachmentAccess());
  135. }
  136. }
  137. }
  138. void PassAttachment::UpdateImageFormat()
  139. {
  140. if (m_updatingImageFormat)
  141. {
  142. AZ_Assert(false, "PassAttachment::UpdateImageFormat: Error: Circular reference detected");
  143. return;
  144. }
  145. m_updatingImageFormat = true;
  146. if (m_getFormatFromPipeline && m_renderPipelineSource)
  147. {
  148. m_descriptor.m_image.m_format = m_renderPipelineSource->GetRenderSettings().m_format;
  149. }
  150. else if (m_formatSource)
  151. {
  152. auto& refAttachment = m_formatSource->GetAttachment();
  153. if (refAttachment && refAttachment->m_descriptor.m_type == RHI::AttachmentType::Image)
  154. {
  155. refAttachment->UpdateImageFormat();
  156. m_descriptor.m_image.m_format = refAttachment->m_descriptor.m_image.m_format;
  157. }
  158. }
  159. m_updatingImageFormat = false;
  160. }
  161. void PassAttachment::UpdateImageMultisampleState()
  162. {
  163. if (m_updatingMultisampleState)
  164. {
  165. AZ_Assert(false, "PassAttachment::UpdateMultisampleState: Error: Circular reference detected");
  166. return;
  167. }
  168. m_updatingMultisampleState = true;
  169. if (m_getMultisampleStateFromPipeline && m_renderPipelineSource)
  170. {
  171. m_descriptor.m_image.m_multisampleState = m_renderPipelineSource->GetRenderSettings().m_multisampleState;
  172. }
  173. else if (m_multisampleSource)
  174. {
  175. auto& refAttachment = m_multisampleSource->GetAttachment();
  176. if (refAttachment && refAttachment->m_descriptor.m_type == RHI::AttachmentType::Image)
  177. {
  178. refAttachment->UpdateImageMultisampleState();
  179. m_descriptor.m_image.m_multisampleState = refAttachment->m_descriptor.m_image.m_multisampleState;
  180. }
  181. }
  182. m_updatingMultisampleState = false;
  183. }
  184. void PassAttachment::UpdateImageSize()
  185. {
  186. if (m_updatingSize)
  187. {
  188. AZ_Assert(false, "PassAttachment::UpdateImageSize: Error: Circular reference detected");
  189. return;
  190. }
  191. m_updatingSize = true;
  192. if (m_getSizeFromPipeline && m_renderPipelineSource)
  193. {
  194. m_descriptor.m_image.m_size = m_renderPipelineSource->GetRenderSettings().m_size;
  195. }
  196. else if (m_sizeSource)
  197. {
  198. auto& refAttachment = m_sizeSource->GetAttachment();
  199. if (refAttachment && refAttachment->m_descriptor.m_type == RHI::AttachmentType::Image)
  200. {
  201. refAttachment->UpdateImageSize();
  202. RHI::Size sourceSize = refAttachment->m_descriptor.m_image.m_size;
  203. m_descriptor.m_image.m_size = m_sizeMultipliers.ApplyModifiers(sourceSize);
  204. }
  205. }
  206. m_updatingSize = false;
  207. }
  208. void PassAttachment::UpdateImageArraySize()
  209. {
  210. if (m_updatingArraySize)
  211. {
  212. AZ_Assert(false, "PassAttachment::UpdateImageArraySize: Error: Circular reference detected");
  213. return;
  214. }
  215. m_updatingArraySize = true;
  216. if (m_arraySizeSource)
  217. {
  218. auto& refAttachment = m_arraySizeSource->GetAttachment();
  219. if (refAttachment && refAttachment->m_descriptor.m_type == RHI::AttachmentType::Image)
  220. {
  221. refAttachment->UpdateImageArraySize();
  222. m_descriptor.m_image.m_arraySize = refAttachment->m_descriptor.m_image.m_arraySize;
  223. }
  224. }
  225. m_updatingArraySize = false;
  226. }
  227. // --- PassBinding ---
  228. PassAttachmentBinding::PassAttachmentBinding(const PassSlot& slot)
  229. {
  230. m_name = slot.m_name;
  231. m_shaderInputName = slot.m_shaderInputName;
  232. m_shaderImageDimensionsNameIndex = slot.m_shaderImageDimensionsName;
  233. m_shaderInputArrayIndex = slot.m_shaderInputArrayIndex;
  234. m_slotType = slot.m_slotType;
  235. m_scopeAttachmentUsage = slot.m_scopeAttachmentUsage;
  236. m_scopeAttachmentStage = slot.m_scopeAttachmentStage;
  237. m_unifiedScopeDesc.m_loadStoreAction = slot.m_loadStoreAction;
  238. if (slot.m_imageViewDesc != nullptr)
  239. {
  240. m_unifiedScopeDesc.SetAsImage(*slot.m_imageViewDesc);
  241. }
  242. else if (slot.m_bufferViewDesc != nullptr)
  243. {
  244. m_unifiedScopeDesc.SetAsBuffer(*slot.m_bufferViewDesc);
  245. }
  246. ValidateDeviceFormats(slot.m_formatFallbacks);
  247. }
  248. void PassAttachmentBinding::ValidateDeviceFormats(const AZStd::vector<RHI::Format>& formatFallbacks)
  249. {
  250. RHI::FormatCapabilities capabilities =
  251. RHI::GetCapabilities(m_scopeAttachmentUsage, GetAttachmentAccess(), m_unifiedScopeDesc.GetType());
  252. if (m_unifiedScopeDesc.GetType() == RHI::AttachmentType::Buffer)
  253. {
  254. RHI::BufferViewDescriptor& bufferViewDesc = m_unifiedScopeDesc.GetBufferViewDescriptor();
  255. RHI::Format format = bufferViewDesc.m_elementFormat;
  256. AZStd::string formatLocation =
  257. AZStd::string::format("BufferViewDescriptor on PassAttachmentBinding [%s]", m_name.GetCStr());
  258. bufferViewDesc.m_elementFormat = RHI::ValidateFormat(format, formatLocation.c_str(), formatFallbacks, capabilities);
  259. }
  260. else if (m_unifiedScopeDesc.GetType() == RHI::AttachmentType::Image)
  261. {
  262. RHI::ImageViewDescriptor& imageViewDesc = m_unifiedScopeDesc.GetImageViewDescriptor();
  263. RHI::Format format = imageViewDesc.m_overrideFormat;
  264. AZStd::string formatLocation = AZStd::string::format("ImageViewDescriptor on PassAttachmentBinding [%s]", m_name.GetCStr());
  265. imageViewDesc.m_overrideFormat = RHI::ValidateFormat(format, formatLocation.c_str(), formatFallbacks, capabilities);
  266. }
  267. }
  268. RHI::ScopeAttachmentAccess PassAttachmentBinding::GetAttachmentAccess() const
  269. {
  270. RHI::ScopeAttachmentAccess access = RPI::GetAttachmentAccess(m_slotType);
  271. access = AdjustAccessBasedOnUsage(access, m_scopeAttachmentUsage);
  272. return access;
  273. }
  274. void PassAttachmentBinding::SetOriginalAttachment(Ptr<PassAttachment>& attachment)
  275. {
  276. m_originalAttachment = attachment;
  277. SetAttachment(attachment);
  278. }
  279. void PassAttachmentBinding::SetAttachment(const Ptr<PassAttachment>& attachment)
  280. {
  281. m_attachment = attachment;
  282. m_unifiedScopeDesc.m_attachmentId = attachment->GetAttachmentId();
  283. // setup scope descriptors for transient attachments if they weren't set in slot
  284. if (m_unifiedScopeDesc.GetType() == RHI::AttachmentType::Uninitialized)
  285. {
  286. if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Transient)
  287. {
  288. if (attachment->GetAttachmentType() == RHI::AttachmentType::Buffer)
  289. {
  290. // AZ_Assert(false, "Transient buffer's buffer view need to be set in slot");
  291. m_unifiedScopeDesc.SetAsBuffer(RHI::BufferViewDescriptor());
  292. }
  293. else if (attachment->GetAttachmentType() == RHI::AttachmentType::Image)
  294. {
  295. m_unifiedScopeDesc.SetAsImage(RHI::ImageViewDescriptor());
  296. }
  297. }
  298. else if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Imported)
  299. {
  300. if (attachment->m_importedResource)
  301. {
  302. if (attachment->GetAttachmentType() == RHI::AttachmentType::Buffer)
  303. {
  304. Buffer* buffer = static_cast<Buffer*>(attachment->m_importedResource.get());
  305. m_unifiedScopeDesc.SetAsBuffer(buffer->GetBufferViewDescriptor());
  306. }
  307. else if (attachment->GetAttachmentType() == RHI::AttachmentType::Image)
  308. {
  309. AttachmentImage* image = static_cast<AttachmentImage*>(attachment->m_importedResource.get());
  310. m_unifiedScopeDesc.SetAsImage(image->GetImageView()->GetDescriptor());
  311. }
  312. }
  313. else
  314. {
  315. AZ_Assert(false, "Imported pass attachment should have the m_importedResource set");
  316. }
  317. }
  318. }
  319. RHI::FormatCapabilities capabilities =
  320. RHI::GetCapabilities(m_scopeAttachmentUsage, GetAttachmentAccess(), m_unifiedScopeDesc.GetType());
  321. m_attachment->ValidateDeviceFormats(AZStd::vector<RHI::Format>(), capabilities);
  322. m_attachment->OnAttached(*this);
  323. AZ_Error(
  324. "PassSystem",
  325. m_unifiedScopeDesc.GetType() == attachment->GetAttachmentType(),
  326. "Attachment must have same type as unified scope descriptor");
  327. }
  328. void PassAttachmentBinding::UpdateConnection(bool useFallback)
  329. {
  330. Ptr<PassAttachment> targetAttachment = nullptr;
  331. // Use the fallback binding if:
  332. // - the calling pass specifies to use it
  333. // - fallback binding is setup
  334. // - the slot is an output (input/output slots act as their own fallback and having fallback for an input makes no sense)
  335. if (useFallback && m_fallbackBinding && m_slotType == PassSlotType::Output)
  336. {
  337. targetAttachment = m_fallbackBinding->m_attachment;
  338. }
  339. else if (m_connectedBinding)
  340. {
  341. targetAttachment = m_connectedBinding->m_attachment;
  342. }
  343. else if (m_originalAttachment != nullptr)
  344. {
  345. targetAttachment = m_originalAttachment;
  346. }
  347. if (targetAttachment == nullptr ||
  348. (targetAttachment == m_attachment && m_attachment->GetAttachmentId() == m_unifiedScopeDesc.m_attachmentId))
  349. {
  350. return;
  351. }
  352. SetAttachment(targetAttachment);
  353. }
  354. } // namespace RPI
  355. } // namespace AZ