DepthOfFieldCopyFocusDepthToCpuPass.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/FrameScheduler.h>
  9. #include <Atom/RHI/CommandList.h>
  10. #include <Atom/RPI.Public/Buffer/BufferSystemInterface.h>
  11. #include <Atom/RPI.Public/Buffer/Buffer.h>
  12. #include <PostProcessing/DepthOfFieldCopyFocusDepthToCpuPass.h>
  13. namespace AZ
  14. {
  15. namespace Render
  16. {
  17. RPI::Ptr<DepthOfFieldCopyFocusDepthToCpuPass> DepthOfFieldCopyFocusDepthToCpuPass::Create(const RPI::PassDescriptor& descriptor)
  18. {
  19. RPI::Ptr<DepthOfFieldCopyFocusDepthToCpuPass> depthOfFieldCopyFocusDepthToCpuPass = aznew DepthOfFieldCopyFocusDepthToCpuPass(descriptor);
  20. return depthOfFieldCopyFocusDepthToCpuPass;
  21. }
  22. DepthOfFieldCopyFocusDepthToCpuPass::DepthOfFieldCopyFocusDepthToCpuPass(const RPI::PassDescriptor& descriptor)
  23. : Pass(descriptor)
  24. {
  25. m_fence = aznew RHI::Fence;
  26. AZ_Assert(m_fence != nullptr, "DepthOfFieldCopyFocusDepthToCpuPass failed to create a fence");
  27. [[maybe_unused]] RHI::ResultCode result = m_fence->Init(RHI::MultiDevice::AllDevices, RHI::FenceState::Reset);
  28. AZ_Assert(result == RHI::ResultCode::Success, "DepthOfFieldCopyFocusDepthToCpuPass failed to init fence");
  29. }
  30. void DepthOfFieldCopyFocusDepthToCpuPass::SetBufferRef(RPI::Ptr<RPI::Buffer> bufferRef)
  31. {
  32. m_bufferRef = bufferRef;
  33. }
  34. float DepthOfFieldCopyFocusDepthToCpuPass::GetFocusDepth()
  35. {
  36. return m_lastFocusDepth;
  37. }
  38. void DepthOfFieldCopyFocusDepthToCpuPass::BuildInternal()
  39. {
  40. InitScope(RHI::ScopeId(GetPathName()));
  41. }
  42. void DepthOfFieldCopyFocusDepthToCpuPass::FrameBeginInternal(FramePrepareParams params)
  43. {
  44. AZ_Assert(m_bufferRef != nullptr, "%s has a null buffer when calling FrameBeginInternal.", GetPathName().GetCStr());
  45. if (m_needsInitialize)
  46. {
  47. RPI::CommonBufferDescriptor desc;
  48. desc.m_bufferName = GetPathName().GetStringView();
  49. desc.m_poolType = RPI::CommonBufferPoolType::ReadBack;
  50. desc.m_byteCount = sizeof(float);
  51. desc.m_elementSize = aznumeric_cast<uint32_t>(desc.m_byteCount);
  52. desc.m_bufferData = nullptr;
  53. m_readbackBuffer = RPI::BufferSystemInterface::Get()->CreateBufferFromCommonPool(desc);
  54. m_copyDescriptor.m_sourceBuffer = m_bufferRef->GetRHIBuffer();
  55. m_copyDescriptor.m_sourceOffset = 0;
  56. m_copyDescriptor.m_destinationBuffer = m_readbackBuffer->GetRHIBuffer();
  57. m_copyDescriptor.m_destinationOffset = 0;
  58. m_copyDescriptor.m_size = sizeof(float);
  59. m_needsInitialize = false;
  60. }
  61. params.m_frameGraphBuilder->ImportScopeProducer(*this);
  62. }
  63. void DepthOfFieldCopyFocusDepthToCpuPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  64. {
  65. AZ_Assert(m_bufferRef != nullptr, "%s has a null buffer when calling Prepare.", GetPathName().GetCStr());
  66. RHI::BufferScopeAttachmentDescriptor desc;
  67. desc.m_attachmentId = m_bufferRef->GetAttachmentId();
  68. desc.m_bufferViewDescriptor = m_bufferRef->GetBufferViewDescriptor();
  69. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::DontCare;
  70. frameGraph.UseCopyAttachment(desc, AZ::RHI::ScopeAttachmentAccess::Read);
  71. frameGraph.SignalFence(*m_fence);
  72. }
  73. void DepthOfFieldCopyFocusDepthToCpuPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  74. {
  75. AZ_UNUSED(context);
  76. }
  77. void DepthOfFieldCopyFocusDepthToCpuPass::BuildCommandList(const RHI::FrameGraphExecuteContext& context)
  78. {
  79. auto deviceIndex = context.GetDeviceIndex();
  80. m_fence->GetDeviceFence(deviceIndex)->WaitOnCpuAsync([this, deviceIndex]()
  81. {
  82. if (m_readbackBuffer)
  83. {
  84. auto buf = m_readbackBuffer->Map(m_copyDescriptor.m_size, 0);
  85. if (buf[deviceIndex] != nullptr)
  86. {
  87. memcpy(&m_lastFocusDepth, buf[deviceIndex], sizeof(m_lastFocusDepth));
  88. m_readbackBuffer->Unmap();
  89. }
  90. }
  91. m_fence->Reset();
  92. }
  93. );
  94. context.GetCommandList()->Submit(m_copyDescriptor.GetDeviceCopyBufferDescriptor(context.GetDeviceIndex()));
  95. }
  96. } // namespace RPI
  97. } // namespace AZ