AtomOutputFrameCapture.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  9. #include <Atom/Feature/Utils/FrameCaptureBus.h>
  10. #include <AzFramework/Components/CameraBus.h>
  11. namespace TrackView
  12. {
  13. //! Provides functionality to capture frames from the "MainCamera".
  14. //! A new pipeline is created (and associated with the scene provided), a callback can be
  15. //! provided to handle the attachment readback (what to do with the captured frame) and also
  16. //! what to do after an individual capture fully completes (called in OnCaptureFinished).
  17. class AtomOutputFrameCapture : private AZ::Render::FrameCaptureNotificationBus::Handler
  18. {
  19. public:
  20. AtomOutputFrameCapture() = default;
  21. using CaptureFinishedCallback = AZStd::function<void()>;
  22. //! Create a new pipeline associated with a given scene.
  23. //! @note "MainCamera" is the view that is captured.
  24. void CreatePipeline(AZ::RPI::Scene& scene, const AZStd::string& pipelineName, uint32_t width, uint32_t height);
  25. //! Removes the pipeline from the scene provided and then destroys it.
  26. //! @note scene must be the same scene used to create the pipeline.
  27. void DestroyPipeline(AZ::RPI::Scene& scene);
  28. bool IsCreated() const { return m_pipelineCreated; }
  29. //! Request a capture to start.
  30. //! @param attachmentReadbackCallback Handles the returned attachment (image data returned by the renderer).
  31. //! @param captureFinishedCallback Logic to run once the capture has completed fully.
  32. bool BeginCapture(
  33. const AZ::RPI::AttachmentReadback::CallbackFunction& attachmentReadbackCallback,
  34. CaptureFinishedCallback captureFinishedCallback);
  35. //! Update the internal view that is associated with the created pipeline.
  36. void UpdateView(const AZ::Matrix3x4& cameraTransform, const AZ::Matrix4x4& cameraProjection, const AZ::RPI::ViewPtr targetView = nullptr);
  37. private:
  38. AZ::RPI::RenderPipelinePtr m_renderPipeline; //!< The internal render pipeline.
  39. AZ::RPI::ViewPtr m_view; //!< The view associated with the render pipeline.
  40. AZ::RPI::ViewPtr m_targetView; //!< The view that this render pipeline will mimic.
  41. AZStd::vector<AZStd::string> m_passHierarchy; //!< Pass hierarchy (includes pipelineName and CopyToSwapChain).
  42. CaptureFinishedCallback m_captureFinishedCallback; //!< Stored callback called from OnCaptureFinished.
  43. bool m_pipelineCreated{};
  44. // FrameCaptureNotificationBus overrides ...
  45. void OnFrameCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string& info) override;
  46. };
  47. inline AZ::EntityId ActiveCameraEntityId()
  48. {
  49. AZ::EntityId activeCameraId;
  50. Camera::CameraSystemRequestBus::BroadcastResult(activeCameraId, &Camera::CameraSystemRequests::GetActiveCamera);
  51. return activeCameraId;
  52. }
  53. //! Returns the transform for the given EntityId.
  54. AZ::Matrix3x4 TransformFromEntityId(AZ::EntityId entityId);
  55. //! Returns the projection matrix for the given camera EntityId.
  56. //! @note Must provide a valid camera entity.
  57. AZ::Matrix4x4 ProjectionFromCameraEntityId(AZ::EntityId entityId, float outputWidth, float outputHeight);
  58. //! Helper to return the GameEntityContext scene.
  59. AZ::RPI::Scene* SceneFromGameEntityContext();
  60. } // namespace TrackView