PassAttachment.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_unifiedScopeDesc.m_loadStoreAction = slot.m_loadStoreAction;
  237. if (slot.m_imageViewDesc != nullptr)
  238. {
  239. m_unifiedScopeDesc.SetAsImage(*slot.m_imageViewDesc);
  240. }
  241. else if (slot.m_bufferViewDesc != nullptr)
  242. {
  243. m_unifiedScopeDesc.SetAsBuffer(*slot.m_bufferViewDesc);
  244. }
  245. ValidateDeviceFormats(slot.m_formatFallbacks);
  246. }
  247. void PassAttachmentBinding::ValidateDeviceFormats(const AZStd::vector<RHI::Format>& formatFallbacks)
  248. {
  249. RHI::FormatCapabilities capabilities =
  250. RHI::GetCapabilities(m_scopeAttachmentUsage, GetAttachmentAccess(), m_unifiedScopeDesc.GetType());
  251. if (m_unifiedScopeDesc.GetType() == RHI::AttachmentType::Buffer)
  252. {
  253. RHI::BufferViewDescriptor& bufferViewDesc = m_unifiedScopeDesc.GetBufferViewDescriptor();
  254. RHI::Format format = bufferViewDesc.m_elementFormat;
  255. AZStd::string formatLocation =
  256. AZStd::string::format("BufferViewDescriptor on PassAttachmentBinding [%s]", m_name.GetCStr());
  257. bufferViewDesc.m_elementFormat = RHI::ValidateFormat(format, formatLocation.c_str(), formatFallbacks, capabilities);
  258. }
  259. else if (m_unifiedScopeDesc.GetType() == RHI::AttachmentType::Image)
  260. {
  261. RHI::ImageViewDescriptor& imageViewDesc = m_unifiedScopeDesc.GetImageViewDescriptor();
  262. RHI::Format format = imageViewDesc.m_overrideFormat;
  263. AZStd::string formatLocation = AZStd::string::format("ImageViewDescriptor on PassAttachmentBinding [%s]", m_name.GetCStr());
  264. imageViewDesc.m_overrideFormat = RHI::ValidateFormat(format, formatLocation.c_str(), formatFallbacks, capabilities);
  265. }
  266. }
  267. RHI::ScopeAttachmentAccess PassAttachmentBinding::GetAttachmentAccess() const
  268. {
  269. RHI::ScopeAttachmentAccess access = RPI::GetAttachmentAccess(m_slotType);
  270. access = AdjustAccessBasedOnUsage(access, m_scopeAttachmentUsage);
  271. return access;
  272. }
  273. void PassAttachmentBinding::SetOriginalAttachment(Ptr<PassAttachment>& attachment)
  274. {
  275. m_originalAttachment = attachment;
  276. SetAttachment(attachment);
  277. }
  278. void PassAttachmentBinding::SetAttachment(const Ptr<PassAttachment>& attachment)
  279. {
  280. m_attachment = attachment;
  281. m_unifiedScopeDesc.m_attachmentId = attachment->GetAttachmentId();
  282. // setup scope descriptors for transient attachments if they weren't set in slot
  283. if (m_unifiedScopeDesc.GetType() == RHI::AttachmentType::Uninitialized)
  284. {
  285. if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Transient)
  286. {
  287. if (attachment->GetAttachmentType() == RHI::AttachmentType::Buffer)
  288. {
  289. // AZ_Assert(false, "Transient buffer's buffer view need to be set in slot");
  290. m_unifiedScopeDesc.SetAsBuffer(RHI::BufferViewDescriptor());
  291. }
  292. else if (attachment->GetAttachmentType() == RHI::AttachmentType::Image)
  293. {
  294. m_unifiedScopeDesc.SetAsImage(RHI::ImageViewDescriptor());
  295. }
  296. }
  297. else if (attachment->m_lifetime == RHI::AttachmentLifetimeType::Imported)
  298. {
  299. if (attachment->m_importedResource)
  300. {
  301. if (attachment->GetAttachmentType() == RHI::AttachmentType::Buffer)
  302. {
  303. Buffer* buffer = static_cast<Buffer*>(attachment->m_importedResource.get());
  304. m_unifiedScopeDesc.SetAsBuffer(buffer->GetBufferViewDescriptor());
  305. }
  306. else if (attachment->GetAttachmentType() == RHI::AttachmentType::Image)
  307. {
  308. AttachmentImage* image = static_cast<AttachmentImage*>(attachment->m_importedResource.get());
  309. m_unifiedScopeDesc.SetAsImage(image->GetImageView()->GetDescriptor());
  310. }
  311. }
  312. else
  313. {
  314. AZ_Assert(false, "Imported pass attachment should have the m_importedResource set");
  315. }
  316. }
  317. }
  318. RHI::FormatCapabilities capabilities =
  319. RHI::GetCapabilities(m_scopeAttachmentUsage, GetAttachmentAccess(), m_unifiedScopeDesc.GetType());
  320. m_attachment->ValidateDeviceFormats(AZStd::vector<RHI::Format>(), capabilities);
  321. m_attachment->OnAttached(*this);
  322. AZ_Error(
  323. "PassSystem",
  324. m_unifiedScopeDesc.GetType() == attachment->GetAttachmentType(),
  325. "Attachment must have same type as unified scope descriptor");
  326. }
  327. void PassAttachmentBinding::UpdateConnection(bool useFallback)
  328. {
  329. Ptr<PassAttachment> targetAttachment = nullptr;
  330. // Use the fallback binding if:
  331. // - the calling pass specifies to use it
  332. // - fallback binding is setup
  333. // - the slot is an output (input/output slots act as their own fallback and having fallback for an input makes no sense)
  334. if (useFallback && m_fallbackBinding && m_slotType == PassSlotType::Output)
  335. {
  336. targetAttachment = m_fallbackBinding->m_attachment;
  337. }
  338. else if (m_connectedBinding)
  339. {
  340. targetAttachment = m_connectedBinding->m_attachment;
  341. }
  342. else if (m_originalAttachment != nullptr)
  343. {
  344. targetAttachment = m_originalAttachment;
  345. }
  346. if (targetAttachment == nullptr ||
  347. (targetAttachment == m_attachment && m_attachment->GetAttachmentId() == m_unifiedScopeDesc.m_attachmentId))
  348. {
  349. return;
  350. }
  351. SetAttachment(targetAttachment);
  352. }
  353. } // namespace RPI
  354. } // namespace AZ