AttachmentReadback.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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/RPI.Public/Buffer/BufferSystemInterface.h>
  9. #include <Atom/RPI.Public/Buffer/Buffer.h>
  10. #include <Atom/RPI.Public/Image/AttachmentImage.h>
  11. #include <Atom/RPI.Public/Image/ImageSystemInterface.h>
  12. #include <Atom/RPI.Public/Pass/AttachmentReadback.h>
  13. #include <Atom/RPI.Public/RPIUtils.h>
  14. #include <Atom/RPI.Reflect/Buffer/BufferAssetCreator.h>
  15. #include <Atom/RHI/CommandList.h>
  16. #include <Atom/RHI/Factory.h>
  17. #include <Atom/RHI/Fence.h>
  18. #include <Atom/RHI/FrameGraphExecuteContext.h>
  19. #include <Atom/RHI/FrameScheduler.h>
  20. #include <Atom/RHI/RHISystemInterface.h>
  21. #include <Atom/RHI/RHIUtils.h>
  22. #include <Atom/RHI/ScopeProducerFunction.h>
  23. #include <AzCore/Serialization/Json/JsonUtils.h>
  24. #include <AzCore/std/smart_ptr/make_shared.h>
  25. namespace AZ
  26. {
  27. namespace RPI
  28. {
  29. AttachmentReadback::AttachmentReadback(const RHI::ScopeId& scopeId) : m_dispatchItem(RHI::MultiDevice::AllDevices)
  30. {
  31. for(uint32_t i = 0; i < RHI::Limits::Device::FrameCountMax; i++)
  32. {
  33. m_isReadbackComplete.push_back(false);
  34. }
  35. // Create fence
  36. m_fence = aznew RHI::Fence;
  37. AZ_Assert(m_fence != nullptr, "AttachmentReadback failed to create a fence");
  38. [[maybe_unused]] RHI::ResultCode result = m_fence->Init(RHI::MultiDevice::AllDevices, RHI::FenceState::Reset);
  39. AZ_Assert(result == RHI::ResultCode::Success, "AttachmentReadback failed to init fence");
  40. // Load shader and srg
  41. constexpr const char* ShaderPath = "shaders/decomposemsimage.azshader";
  42. m_decomposeShader = LoadCriticalShader(ShaderPath);
  43. if (m_decomposeShader == nullptr)
  44. {
  45. AZ_Error("PassSystem", false, "[AttachmentReadback]: Failed to load shader '%s'!", ShaderPath);
  46. return;
  47. }
  48. // Load SRG
  49. const auto srgLayout = m_decomposeShader->FindShaderResourceGroupLayout(SrgBindingSlot::Object);
  50. if (srgLayout)
  51. {
  52. m_decomposeSrg = ShaderResourceGroup::Create(m_decomposeShader->GetAsset(), m_decomposeShader->GetSupervariantIndex(), srgLayout->GetName());
  53. if (!m_decomposeSrg)
  54. {
  55. AZ_Error("PassSystem", false, "Failed to create SRG from shader asset '%s'", ShaderPath);
  56. return;
  57. }
  58. }
  59. RHI::PipelineStateDescriptorForDispatch pipelineStateDescriptor;
  60. const auto& shaderVariant = m_decomposeShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  61. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  62. m_dispatchItem.SetPipelineState(m_decomposeShader->AcquirePipelineState(pipelineStateDescriptor));
  63. AZStd::array<const RHI::ShaderResourceGroup*, 1> srgs{m_decomposeSrg->GetRHIShaderResourceGroup()};
  64. m_dispatchItem.SetShaderResourceGroups(srgs);
  65. // find srg input indexes
  66. m_decomposeInputImageIndex = m_decomposeSrg->FindShaderInputImageIndex(Name("m_msImage"));
  67. m_decomposeOutputImageIndex = m_decomposeSrg->FindShaderInputImageIndex(Name("m_outputImage"));
  68. // build scope producer for copying
  69. m_copyScopeProducer = AZStd::make_shared<RHI::ScopeProducerFunctionNoData>(
  70. scopeId,
  71. AZStd::bind(&AttachmentReadback::CopyPrepare, this, AZStd::placeholders::_1),
  72. AZStd::bind(&AttachmentReadback::CopyCompile, this, AZStd::placeholders::_1),
  73. AZStd::bind(&AttachmentReadback::CopyExecute, this, AZStd::placeholders::_1));
  74. m_state = ReadbackState::Idle;
  75. }
  76. AttachmentReadback::~AttachmentReadback()
  77. {
  78. Reset();
  79. m_fence = nullptr;
  80. }
  81. bool AttachmentReadback::ReadPassAttachment(const PassAttachment* attachment, const AZ::Name& readbackName, const RHI::ImageSubresourceRange* mipsRange)
  82. {
  83. if (AZ::RHI::IsNullRHI())
  84. {
  85. return false;
  86. }
  87. if (!IsReady())
  88. {
  89. AZ_Assert(false, "AttachmentsReadbackGroup is not ready to readback attachments.");
  90. return false;
  91. }
  92. Reset();
  93. if (!attachment || (attachment->GetAttachmentType() != RHI::AttachmentType::Buffer && attachment->GetAttachmentType() != RHI::AttachmentType::Image))
  94. {
  95. AZ_Assert(false, "ReadPassAttachment: attachment is not a buffer or an image");
  96. return false;
  97. }
  98. m_state = ReadbackState::AttachmentSet;
  99. m_attachmentId = attachment->GetAttachmentId();
  100. m_attachmentType = attachment->GetAttachmentType();
  101. m_readbackName = readbackName;
  102. if (m_readbackName.IsEmpty())
  103. {
  104. m_readbackName = AZStd::string::format("%s_RB", m_attachmentId.GetCStr());
  105. }
  106. m_copyAttachmentId = m_attachmentId;
  107. // Get some attachment information
  108. if (m_attachmentType == RHI::AttachmentType::Buffer)
  109. {
  110. if (attachment->m_importedResource)
  111. {
  112. Buffer* buffer = static_cast<Buffer*>(attachment->m_importedResource.get());
  113. m_bufferAttachmentByteSize = buffer->GetBufferSize();
  114. }
  115. else
  116. {
  117. m_bufferAttachmentByteSize = attachment->m_descriptor.m_buffer.m_byteCount;
  118. }
  119. m_readbackItems.push_back({});
  120. auto& item = m_readbackItems.back();
  121. item.m_readbackBufferArray.resize(RHI::Limits::Device::FrameCountMax, nullptr);
  122. }
  123. else
  124. {
  125. if (attachment->m_importedResource)
  126. {
  127. AttachmentImage* attImage = static_cast<AttachmentImage*>(attachment->m_importedResource.get());
  128. m_imageDescriptor = attImage->GetRHIImage()->GetDescriptor();
  129. }
  130. else
  131. {
  132. m_imageDescriptor = attachment->m_descriptor.m_image;
  133. }
  134. m_imageMipsRange = (mipsRange != nullptr)
  135. ? *mipsRange
  136. : RHI::ImageSubresourceRange(0, 0, 0, 0);
  137. for (uint32_t mipIndex = m_imageMipsRange.m_mipSliceMin; mipIndex <= m_imageMipsRange.m_mipSliceMax; mipIndex++)
  138. {
  139. m_readbackItems.push_back({});
  140. auto& mipItem = m_readbackItems.back();
  141. mipItem.m_readbackBufferArray.resize(RHI::Limits::Device::FrameCountMax, nullptr);
  142. }
  143. // Add decompose scope to convert multi-sampled images to image array
  144. if (m_imageDescriptor.m_multisampleState.m_samples > 1)
  145. {
  146. m_copyAttachmentId = RHI::AttachmentId(AZStd::string::format("%s_Decomposed", m_attachmentId.GetCStr()));
  147. m_decomposeScopeProducer = AZStd::make_shared<RHI::ScopeProducerFunctionNoData>(
  148. m_copyAttachmentId,
  149. AZStd::bind(&AttachmentReadback::DecomposePrepare, this, AZStd::placeholders::_1),
  150. AZStd::bind(&AttachmentReadback::DecomposeCompile, this, AZStd::placeholders::_1),
  151. AZStd::bind(&AttachmentReadback::DecomposeExecute, this, AZStd::placeholders::_1));
  152. }
  153. }
  154. return true;
  155. }
  156. void AttachmentReadback::DecomposePrepare(RHI::FrameGraphInterface frameGraph)
  157. {
  158. RHI::ImageScopeAttachmentDescriptor inputDesc{ m_attachmentId };
  159. inputDesc.m_imageViewDescriptor.m_aspectFlags = RHI::CheckBitsAny(RHI::GetImageAspectFlags(m_imageDescriptor.m_format), RHI::ImageAspectFlags::Depth)?
  160. RHI::ImageAspectFlags::Depth:RHI::ImageAspectFlags::Color;
  161. frameGraph.UseAttachment(inputDesc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentUsage::Shader, RHI::ScopeAttachmentStage::ComputeShader);
  162. RHI::ImageScopeAttachmentDescriptor outputDesc{ m_copyAttachmentId };
  163. frameGraph.UseAttachment(
  164. outputDesc, RHI::ScopeAttachmentAccess::Write, RHI::ScopeAttachmentUsage::Shader, RHI::ScopeAttachmentStage::ComputeShader);
  165. }
  166. void AttachmentReadback::DecomposeCompile(const RHI::FrameGraphCompileContext& context)
  167. {
  168. // prepare compute shader which to convert multi-sample texture to texture array
  169. RHI::DispatchDirect dispatchArgs;
  170. dispatchArgs.m_totalNumberOfThreadsX = m_imageDescriptor.m_size.m_width;
  171. dispatchArgs.m_totalNumberOfThreadsY = m_imageDescriptor.m_size.m_height;
  172. dispatchArgs.m_totalNumberOfThreadsZ = m_imageDescriptor.m_arraySize;
  173. dispatchArgs.m_threadsPerGroupX = 16; // these numbers are matching numthreads in shader file
  174. dispatchArgs.m_threadsPerGroupY = 16;
  175. dispatchArgs.m_threadsPerGroupZ = 1;
  176. m_dispatchItem.SetArguments(dispatchArgs);
  177. const RHI::ImageView* imageView = context.GetImageView(m_attachmentId);
  178. m_decomposeSrg->SetImageView(m_decomposeInputImageIndex, imageView);
  179. imageView = context.GetImageView(m_copyAttachmentId);
  180. m_decomposeSrg->SetImageView(m_decomposeOutputImageIndex, imageView);
  181. m_decomposeSrg->Compile();
  182. }
  183. void AttachmentReadback::DecomposeExecute(const RHI::FrameGraphExecuteContext& context)
  184. {
  185. context.GetCommandList()->Submit(m_dispatchItem.GetDeviceDispatchItem(context.GetDeviceIndex()));
  186. }
  187. void AttachmentReadback::CopyPrepare(RHI::FrameGraphInterface frameGraph)
  188. {
  189. if (m_attachmentType == RHI::AttachmentType::Buffer)
  190. {
  191. RHI::BufferScopeAttachmentDescriptor descriptor{ m_copyAttachmentId };
  192. descriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateRaw(0, aznumeric_cast<uint32_t>(m_bufferAttachmentByteSize));
  193. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Read);
  194. }
  195. else if (m_attachmentType == RHI::AttachmentType::Image)
  196. {
  197. RHI::ImageScopeAttachmentDescriptor descriptor{ m_copyAttachmentId };
  198. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Read);
  199. }
  200. frameGraph.SetEstimatedItemCount(static_cast<uint32_t>(m_readbackItems.size()));
  201. frameGraph.SignalFence(*m_fence);
  202. // CPU has already consumed the GPU buffer. We can clear it now.
  203. // We don't do this in the Async callback as the callback can get signaled by the GPU at anytime.
  204. // We were seeing an issue where during the buffer cleanup there was a chance to hit the assert
  205. // related to disconnecting a bus during a dispatch on a lockless Bus.
  206. // The fix is to clear the buffer outside of the callback.
  207. for (int32_t i = 0; i < RHI::Limits::Device::FrameCountMax; i++)
  208. {
  209. if (m_isReadbackComplete[i])
  210. {
  211. m_isReadbackComplete[i] = false;
  212. for (auto& readbackItem : m_readbackItems)
  213. {
  214. readbackItem.m_readbackBufferArray[i] = nullptr;
  215. }
  216. }
  217. }
  218. // Loop the triple buffer index and cache the current index to the callback.
  219. m_readbackBufferCurrentIndex = (m_readbackBufferCurrentIndex + 1) % RHI::Limits::Device::FrameCountMax;
  220. }
  221. void AttachmentReadback::CopyCompile(const RHI::FrameGraphCompileContext& context)
  222. {
  223. if (m_attachmentType == RHI::AttachmentType::Buffer)
  224. {
  225. const AZ::RHI::Buffer* buffer = context.GetBuffer(m_copyAttachmentId);
  226. RPI::CommonBufferDescriptor desc;
  227. desc.m_poolType = RPI::CommonBufferPoolType::ReadBack;
  228. desc.m_bufferName = m_readbackName.GetStringView();
  229. desc.m_byteCount = buffer->GetDescriptor().m_byteCount;
  230. m_readbackItems[0].m_readbackBufferArray[m_readbackBufferCurrentIndex] = BufferSystemInterface::Get()->CreateBufferFromCommonPool(desc);
  231. // copy buffer
  232. RHI::CopyBufferDescriptor copyBuffer;
  233. copyBuffer.m_sourceBuffer = buffer;
  234. copyBuffer.m_destinationBuffer = m_readbackItems[0].m_readbackBufferArray[m_readbackBufferCurrentIndex]->GetRHIBuffer();
  235. copyBuffer.m_size = aznumeric_cast<uint32_t>(desc.m_byteCount);
  236. m_readbackItems[0].m_copyItem = copyBuffer;
  237. }
  238. else if (m_attachmentType == RHI::AttachmentType::Image)
  239. {
  240. // copy image to read back buffer since only buffer can be accessed by host
  241. const AZ::RHI::Image* image = context.GetImage(m_copyAttachmentId);
  242. if (!image)
  243. {
  244. AZ_Warning("AttachmentReadback", false, "Failed to find attachment image %s for copy to buffer", m_copyAttachmentId.GetCStr());
  245. return;
  246. }
  247. for (uint16_t itemIdx = 0; itemIdx < static_cast<uint16_t>(m_readbackItems.size()); itemIdx++)
  248. {
  249. auto& readbackItem = m_readbackItems[itemIdx];
  250. // copy descriptor for copying image to buffer
  251. RHI::CopyImageToBufferDescriptor copyImageToBuffer;
  252. copyImageToBuffer.m_sourceImage = image;
  253. readbackItem.m_copyItem = copyImageToBuffer;
  254. }
  255. }
  256. }
  257. void AttachmentReadback::CopyExecute(const RHI::FrameGraphExecuteContext& context)
  258. {
  259. for (uint16_t itemIdx = 0; itemIdx < static_cast<uint16_t>(m_readbackItems.size()); itemIdx++)
  260. {
  261. auto& readbackItem = m_readbackItems[itemIdx];
  262. if (m_attachmentType == RHI::AttachmentType::Image)
  263. {
  264. // copy image to read back buffer since only buffer can be accessed by host
  265. const auto image = readbackItem.m_copyItem.m_image.m_sourceImage;
  266. if (!image)
  267. {
  268. AZ_Warning(
  269. "AttachmentReadback",
  270. false,
  271. "Failed to find attachment image %s for copy to buffer",
  272. m_copyAttachmentId.GetCStr());
  273. return;
  274. }
  275. m_imageDescriptor = image->GetDescriptor();
  276. // [GFX TODO] [ATOM-14140] [Pass Tree] Add the ability to output all the array subresources and planars
  277. // only array 0, and one aspect (planar) at this moment.
  278. // Note: Mip Levels and Texture3D images are supported.
  279. const uint16_t mipSlice = m_imageMipsRange.m_mipSliceMin + itemIdx;
  280. RHI::ImageSubresourceRange range(mipSlice, mipSlice, 0, 0);
  281. range.m_aspectFlags = RHI::ImageAspectFlags::Color;
  282. // setup aspect
  283. RHI::ImageAspect imageAspect = RHI::ImageAspect::Color;
  284. RHI::ImageAspectFlags imageAspectFlags = RHI::GetImageAspectFlags(m_imageDescriptor.m_format);
  285. if (RHI::CheckBitsAll(imageAspectFlags, RHI::ImageAspectFlags::Depth))
  286. {
  287. imageAspect = RHI::ImageAspect::Depth;
  288. range.m_aspectFlags = RHI::ImageAspectFlags::Depth;
  289. }
  290. AZStd::vector<RHI::DeviceImageSubresourceLayout> imageSubresourceLayouts;
  291. imageSubresourceLayouts.resize_no_construct(m_imageDescriptor.m_mipLevels);
  292. size_t totalSizeInBytes = 0;
  293. image->GetDeviceImage(context.GetDeviceIndex())->GetSubresourceLayouts(range, imageSubresourceLayouts.data(), &totalSizeInBytes);
  294. AZ::u64 byteCount = totalSizeInBytes;
  295. RPI::CommonBufferDescriptor desc;
  296. desc.m_poolType = RPI::CommonBufferPoolType::ReadBack;
  297. desc.m_bufferName = m_readbackName.GetStringView();
  298. desc.m_byteCount = byteCount;
  299. readbackItem.m_readbackBufferArray[m_readbackBufferCurrentIndex] =
  300. BufferSystemInterface::Get()->CreateBufferFromCommonPool(desc);
  301. // Use the aspect format as output format, this format is also used as copy destination's format
  302. m_imageDescriptor.m_format = FindFormatForAspect(m_imageDescriptor.m_format, imageAspect);
  303. // copy descriptor for copying image to buffer
  304. RHI::CopyImageToBufferDescriptor copyImageToBuffer;
  305. copyImageToBuffer.m_sourceImage = image;
  306. copyImageToBuffer.m_sourceSize = imageSubresourceLayouts[mipSlice].m_size;
  307. copyImageToBuffer.m_sourceSubresource = RHI::ImageSubresource(mipSlice, 0 /*arraySlice*/, imageAspect);
  308. copyImageToBuffer.m_destinationOffset = 0;
  309. copyImageToBuffer.m_destinationBytesPerRow = imageSubresourceLayouts[mipSlice].m_bytesPerRow;
  310. copyImageToBuffer.m_destinationBytesPerImage = imageSubresourceLayouts[mipSlice].m_bytesPerImage;
  311. copyImageToBuffer.m_destinationBuffer = readbackItem.m_readbackBufferArray[m_readbackBufferCurrentIndex]->GetRHIBuffer();
  312. copyImageToBuffer.m_destinationFormat = m_imageDescriptor.m_format;
  313. readbackItem.m_mipInfo.m_slice = mipSlice;
  314. readbackItem.m_mipInfo.m_size = imageSubresourceLayouts[mipSlice].m_size;
  315. readbackItem.m_copyItem = copyImageToBuffer;
  316. }
  317. if (readbackItem.m_readbackBufferArray[m_readbackBufferCurrentIndex])
  318. {
  319. context.GetCommandList()->Submit(readbackItem.m_copyItem.GetDeviceCopyItem(context.GetDeviceIndex()));
  320. }
  321. }
  322. uint32_t readbackBufferCurrentIndex = m_readbackBufferCurrentIndex;
  323. auto deviceIndex = context.GetDeviceIndex();
  324. m_fence->GetDeviceFence(deviceIndex)
  325. ->WaitOnCpuAsync(
  326. [this, readbackBufferCurrentIndex, deviceIndex]()
  327. {
  328. if (m_state == ReadbackState::Reading)
  329. {
  330. if (CopyBufferData(readbackBufferCurrentIndex, deviceIndex))
  331. {
  332. m_state = ReadbackState::Success;
  333. }
  334. else
  335. {
  336. m_state = ReadbackState::Failed;
  337. }
  338. }
  339. if (m_callback)
  340. {
  341. m_callback(GetReadbackResult());
  342. }
  343. Reset();
  344. });
  345. }
  346. void AttachmentReadback::Reset()
  347. {
  348. m_attachmentId = RHI::AttachmentId{};
  349. m_readbackItems.clear();
  350. m_state = ReadbackState::Idle;
  351. m_readbackName = AZ::Name{};
  352. m_copyAttachmentId = RHI::AttachmentId{};
  353. m_decomposeScopeProducer = nullptr;
  354. if (m_decomposeSrg)
  355. {
  356. m_decomposeSrg->SetImageView(m_decomposeInputImageIndex, nullptr);
  357. m_decomposeSrg->SetImageView(m_decomposeOutputImageIndex, nullptr);
  358. }
  359. if (m_fence)
  360. {
  361. m_fence->Reset();
  362. }
  363. }
  364. AttachmentReadback::ReadbackState AttachmentReadback::GetReadbackState()
  365. {
  366. return m_state;
  367. }
  368. void AttachmentReadback::SetCallback(CallbackFunction callback)
  369. {
  370. m_callback = callback;
  371. }
  372. void AttachmentReadback::SetUserIdentifier(uint32_t userIdentifier)
  373. {
  374. m_userIdentifier = userIdentifier;
  375. }
  376. void AttachmentReadback::FrameBegin(Pass::FramePrepareParams params)
  377. {
  378. if (m_state == AttachmentReadback::ReadbackState::AttachmentSet)
  379. {
  380. // Need decompose
  381. if (m_decomposeScopeProducer)
  382. {
  383. // Create transient image array to save decompose result
  384. RHI::TransientImageDescriptor descriptor;
  385. descriptor.m_attachmentId = m_copyAttachmentId;
  386. auto format = m_imageDescriptor.m_format;
  387. // We can only use one planar for none render target shader output. Set to output Depth aspect only
  388. if (RHI::GetImageAspectFlags(format) == RHI::ImageAspectFlags::DepthStencil)
  389. {
  390. format = FindFormatForAspect(format, RHI::ImageAspect::Depth);
  391. }
  392. descriptor.m_imageDescriptor = RHI::ImageDescriptor::Create2DArray(RHI::ImageBindFlags::ShaderReadWrite,
  393. m_imageDescriptor.m_size.m_width, m_imageDescriptor.m_size.m_height,
  394. m_imageDescriptor.m_multisampleState.m_samples, // Use sample count as array size
  395. format);
  396. params.m_frameGraphBuilder->GetAttachmentDatabase().CreateTransientImage(descriptor);
  397. params.m_frameGraphBuilder->ImportScopeProducer(*m_decomposeScopeProducer.get());
  398. }
  399. // Import copy producer
  400. params.m_frameGraphBuilder->ImportScopeProducer(*m_copyScopeProducer.get());
  401. m_state = AttachmentReadback::ReadbackState::Reading;
  402. }
  403. }
  404. bool AttachmentReadback::IsFinished() const
  405. {
  406. return m_state == ReadbackState::Success || m_state == ReadbackState::Failed;
  407. }
  408. bool AttachmentReadback::IsReady() const
  409. {
  410. return !(m_state == ReadbackState::Reading || m_state == ReadbackState::Uninitialized);
  411. }
  412. AttachmentReadback::ReadbackResult AttachmentReadback::GetReadbackResult() const
  413. {
  414. ReadbackResult result;
  415. if (m_readbackItems.empty())
  416. {
  417. // the AttachmentReadback was reset before the readback was triggered from the GPU. Avoid
  418. // a crash by accessing a non-existend readback item
  419. result.m_state = ReadbackState::Failed;
  420. return result;
  421. }
  422. result.m_state = m_state;
  423. result.m_attachmentType = m_attachmentType;
  424. result.m_dataBuffer = m_readbackItems[0].m_dataBuffer;
  425. result.m_name = m_readbackName;
  426. result.m_userIdentifier = m_userIdentifier;
  427. result.m_imageDescriptor = m_imageDescriptor;
  428. result.m_imageDescriptor.m_arraySize = 1;
  429. if (m_attachmentType == RHI::AttachmentType::Image)
  430. {
  431. result.m_mipDataBuffers.reserve(m_readbackItems.size());
  432. for (const auto& readbackItem : m_readbackItems)
  433. {
  434. result.m_mipDataBuffers.push_back({});
  435. auto& mipDataBuffer = result.m_mipDataBuffers.back();
  436. mipDataBuffer.m_mipBuffer = readbackItem.m_dataBuffer;
  437. mipDataBuffer.m_mipInfo = readbackItem.m_mipInfo;
  438. }
  439. }
  440. return result;
  441. }
  442. bool AttachmentReadback::CopyBufferData(uint32_t readbackBufferIndex, int deviceIndex)
  443. {
  444. for (auto& readbackItem : m_readbackItems)
  445. {
  446. Data::Instance<Buffer> readbackBufferCurrent = readbackItem.m_readbackBufferArray[readbackBufferIndex];
  447. if (!readbackBufferCurrent)
  448. {
  449. return false;
  450. }
  451. auto bufferSize = readbackBufferCurrent->GetBufferSize();
  452. readbackItem.m_dataBuffer = AZStd::make_shared<AZStd::vector<uint8_t>>();
  453. void* buf = readbackBufferCurrent->Map(bufferSize, 0)[deviceIndex];
  454. if (buf)
  455. {
  456. if (m_attachmentType == RHI::AttachmentType::Buffer)
  457. {
  458. readbackItem.m_dataBuffer->resize_no_construct(bufferSize);
  459. memcpy(readbackItem.m_dataBuffer->data(), buf, bufferSize);
  460. }
  461. else if (m_attachmentType == RHI::AttachmentType::Image)
  462. {
  463. RHI::Size mipSize = readbackItem.m_mipInfo.m_size;
  464. RHI::DeviceImageSubresourceLayout imageLayout = RHI::GetImageSubresourceLayout(mipSize,
  465. m_imageDescriptor.m_format);
  466. auto rowCount = imageLayout.m_rowCount;
  467. auto byteCount = imageLayout.m_bytesPerImage;
  468. if (m_imageDescriptor.m_dimension == AZ::RHI::ImageDimension::Image3D)
  469. {
  470. byteCount *= mipSize.m_depth;
  471. rowCount *= mipSize.m_depth;
  472. }
  473. readbackItem.m_dataBuffer->resize_no_construct(byteCount);
  474. const uint8_t* const sourceBegin = static_cast<uint8_t*>(buf);
  475. uint8_t* const destBegin = readbackItem.m_dataBuffer->data();
  476. // The source image WAS the destination when the copy item transferred data from GPU to CPU
  477. // this explains why the name srcBytesPerRow for these memcpy operations.
  478. const auto srcBytesPerRow = readbackItem.m_copyItem.m_imageToBuffer.m_destinationBytesPerRow;
  479. for (uint32_t row = 0; row < rowCount; ++row)
  480. {
  481. void* dest = destBegin + row * imageLayout.m_bytesPerRow;
  482. const void* source = sourceBegin + row * srcBytesPerRow;
  483. memcpy(dest, source, imageLayout.m_bytesPerRow);
  484. }
  485. }
  486. readbackBufferCurrent->Unmap();
  487. m_isReadbackComplete[readbackBufferIndex] = true;
  488. }
  489. else
  490. {
  491. return false;
  492. }
  493. }
  494. return true;
  495. }
  496. } // namespace RPI
  497. } // namespace AZ