DepthOfFieldCopyFocusDepthToCpuPass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  26. void DepthOfFieldCopyFocusDepthToCpuPass::SetBufferRef(RPI::Ptr<RPI::Buffer> bufferRef)
  27. {
  28. m_bufferRef = bufferRef;
  29. }
  30. float DepthOfFieldCopyFocusDepthToCpuPass::GetFocusDepth()
  31. {
  32. float depth = 0.0f;
  33. if (m_readbackBuffer)
  34. {
  35. void* buf = m_readbackBuffer->Map(m_copyDescriptor.m_size, 0);
  36. if (buf)
  37. {
  38. memcpy(&depth, buf, sizeof(depth));
  39. m_readbackBuffer->Unmap();
  40. }
  41. }
  42. return depth;
  43. }
  44. void DepthOfFieldCopyFocusDepthToCpuPass::BuildInternal()
  45. {
  46. InitScope(RHI::ScopeId(GetPathName()));
  47. }
  48. void DepthOfFieldCopyFocusDepthToCpuPass::FrameBeginInternal(FramePrepareParams params)
  49. {
  50. AZ_Assert(m_bufferRef != nullptr, "%s has a null buffer when calling FrameBeginInternal.", GetPathName().GetCStr());
  51. if (m_needsInitialize)
  52. {
  53. RPI::CommonBufferDescriptor desc;
  54. desc.m_bufferName = GetPathName().GetStringView();
  55. desc.m_poolType = RPI::CommonBufferPoolType::ReadBack;
  56. desc.m_byteCount = sizeof(float);
  57. desc.m_elementSize = aznumeric_cast<uint32_t>(desc.m_byteCount);
  58. desc.m_bufferData = nullptr;
  59. m_readbackBuffer = RPI::BufferSystemInterface::Get()->CreateBufferFromCommonPool(desc);
  60. m_copyDescriptor.m_sourceBuffer = m_bufferRef->GetRHIBuffer();
  61. m_copyDescriptor.m_sourceOffset = 0;
  62. m_copyDescriptor.m_destinationBuffer = m_readbackBuffer->GetRHIBuffer();
  63. m_copyDescriptor.m_destinationOffset = 0;
  64. m_copyDescriptor.m_size = sizeof(float);
  65. m_needsInitialize = false;
  66. }
  67. params.m_frameGraphBuilder->ImportScopeProducer(*this);
  68. }
  69. void DepthOfFieldCopyFocusDepthToCpuPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  70. {
  71. AZ_Assert(m_bufferRef != nullptr, "%s has a null buffer when calling Prepare.", GetPathName().GetCStr());
  72. RHI::BufferScopeAttachmentDescriptor desc;
  73. desc.m_attachmentId = m_bufferRef->GetAttachmentId();
  74. desc.m_bufferViewDescriptor = m_bufferRef->GetBufferViewDescriptor();
  75. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::DontCare;
  76. frameGraph.UseCopyAttachment(desc, AZ::RHI::ScopeAttachmentAccess::Read);
  77. }
  78. void DepthOfFieldCopyFocusDepthToCpuPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  79. {
  80. AZ_UNUSED(context);
  81. }
  82. void DepthOfFieldCopyFocusDepthToCpuPass::BuildCommandList(const RHI::FrameGraphExecuteContext& context)
  83. {
  84. context.GetCommandList()->Submit(m_copyDescriptor);
  85. }
  86. } // namespace RPI
  87. } // namespace AZ