RenderToTexturePass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Reflect/Format.h>
  10. #include <Atom/RPI.Public/Pass/AttachmentReadback.h>
  11. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  12. #include <Atom/RPI.Public/Pass/PassUtils.h>
  13. #include <Atom/RPI.Public/Pass/Specific/RenderToTexturePass.h>
  14. namespace AZ
  15. {
  16. namespace RPI
  17. {
  18. Ptr<RenderToTexturePass> RenderToTexturePass::Create(const PassDescriptor& descriptor)
  19. {
  20. Ptr<RenderToTexturePass> pass = aznew RenderToTexturePass(descriptor);
  21. return pass;
  22. }
  23. Ptr<ParentPass> RenderToTexturePass::Recreate() const
  24. {
  25. PassDescriptor desc = GetPassDescriptor();
  26. Ptr<ParentPass> pass = aznew RenderToTexturePass(desc);
  27. return pass;
  28. }
  29. RenderToTexturePass::RenderToTexturePass(const PassDescriptor& descriptor)
  30. : ParentPass(descriptor)
  31. {
  32. // Save the pass data for easier access
  33. const RPI::RenderToTexturePassData* passData = RPI::PassUtils::GetPassData<RPI::RenderToTexturePassData>(descriptor);
  34. if (passData)
  35. {
  36. m_passData = *passData;
  37. OnUpdateOutputSize();
  38. }
  39. }
  40. RenderToTexturePass::~RenderToTexturePass()
  41. {
  42. }
  43. void RenderToTexturePass::BuildInternal()
  44. {
  45. m_outputAttachment = aznew PassAttachment();
  46. m_outputAttachment->m_name = "RenderTarget";
  47. m_outputAttachment->ComputePathName(GetPathName());
  48. RHI::ImageDescriptor outputImageDesc;
  49. outputImageDesc.m_bindFlags = RHI::ImageBindFlags::Color | RHI::ImageBindFlags::ShaderRead | RHI::ImageBindFlags::CopyWrite;
  50. outputImageDesc.m_size.m_width = m_passData.m_width;
  51. outputImageDesc.m_size.m_height = m_passData.m_height;
  52. outputImageDesc.m_format = m_passData.m_format;
  53. m_outputAttachment->m_descriptor = outputImageDesc;
  54. m_ownedAttachments.push_back(m_outputAttachment);
  55. PassAttachmentBinding outputBinding;
  56. outputBinding.m_name = "Output";
  57. outputBinding.m_slotType = PassSlotType::Output;
  58. outputBinding.m_scopeAttachmentUsage = RHI::ScopeAttachmentUsage::RenderTarget;
  59. outputBinding.SetAttachment(m_outputAttachment);
  60. AddAttachmentBinding(outputBinding);
  61. Base::BuildInternal();
  62. }
  63. void RenderToTexturePass::FrameBeginInternal(FramePrepareParams params)
  64. {
  65. params.m_scissorState = m_scissor;
  66. params.m_viewportState = m_viewport;
  67. Base::FrameBeginInternal(params);
  68. }
  69. void RenderToTexturePass::ResizeOutput(uint32_t width, uint32_t height)
  70. {
  71. m_passData.m_width = width;
  72. m_passData.m_height = height;
  73. OnUpdateOutputSize();
  74. QueueForBuildAndInitialization();
  75. }
  76. void RenderToTexturePass::OnUpdateOutputSize()
  77. {
  78. // update scissor and viewport when output size changed
  79. m_scissor.m_minX = 0;
  80. m_scissor.m_maxX = m_passData.m_width;
  81. m_scissor.m_minY = 0;
  82. m_scissor.m_maxY = m_passData.m_height;
  83. m_viewport.m_minX = 0;
  84. m_viewport.m_maxX = aznumeric_cast<float>(m_passData.m_width);
  85. m_viewport.m_minX = 0;
  86. m_viewport.m_maxY = aznumeric_cast<float>(m_passData.m_height);
  87. m_viewport.m_minZ = 0;
  88. m_viewport.m_maxZ = 1;
  89. }
  90. void RenderToTexturePass::ReadbackOutput(AZStd::shared_ptr<AttachmentReadback> readback)
  91. {
  92. if (m_outputAttachment)
  93. {
  94. m_readbackOption = PassAttachmentReadbackOption::Output;
  95. m_attachmentReadback = readback;
  96. AZStd::string readbackName = AZStd::string::format("%s_%s", m_outputAttachment->GetAttachmentId().GetCStr(), GetName().GetCStr());
  97. m_attachmentReadback->ReadPassAttachment(m_outputAttachment.get(), AZ::Name(readbackName));
  98. }
  99. }
  100. } // namespace RPI
  101. } // namespace AZ