NVIDIA DeepLearning Dataset Synthesizer (NDDS)
 All Classes Namespaces Functions Variables Typedefs Pages
NVTextureReader.h
1 /*
2 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10 
11 #pragma once
12 
13 #include "NVSceneCapturerUtils.h"
14 #include "NVTextureReader.generated.h"
15 
16 DECLARE_LOG_CATEGORY_EXTERN(LogNVTextureReader, Log, All)
17 
18 // This class read the pixels data from a texture target
19 USTRUCT()
20 struct NVSCENECAPTURER_API FNVTextureReader
21 {
22  GENERATED_BODY()
23 
24 public:
25  FNVTextureReader();
26  virtual ~FNVTextureReader();
27  FNVTextureReader& operator=(const FNVTextureReader& OtherReader);
28 
29  /// Callback function get called after finish reading pixels data
30  /// uint8* - pointer to pixel array buffer
31  /// EPixelFormat - format of the read back pixels
32  /// FIntPoint - 2d size of the pixels image
33  typedef TFunction<void(uint8*, EPixelFormat, FIntPoint)> OnFinishedReadingRawPixelsCallback;
34 
35  /// Callback function get called after finish reading pixels data
36  /// FNVTexturePixelData - The struct contain the texture's pixels data
37  typedef TFunction<void(const FNVTexturePixelData&)> OnFinishedReadingPixelsDataCallback;
38 
39  /// Read back the pixels data from the current source texture
40  /// @param Callback The function to call after all the pixels data are read from the source texture
41  /// @param bIgnoreAlpha If true, just set the alpha value of the readback pixels to 1, otherwise read it correctly
42  /// NOTE: This function is async, the reading process will run on the rendering thread in parallel with the game thread
43  virtual bool ReadPixelsData(OnFinishedReadingPixelsDataCallback Callback, bool bIgnoreAlpha = false);
44 
45  /// Read back the pixels data from the current source texture
46  /// NOTE: This function is sync, the pixels data is returned right away but it may cause the game to hitches since it flush the rendering commands
47  virtual bool ReadPixelsData(FNVTexturePixelData& OutPixelsData);
48 
49 protected:
50  /// Change the information of the texture to read from
51  /// @param NewSourceTexture The texture to read from
52  /// @param NewSourceRect The region to read from the source texture. If the region is empty, the whole source texture will be read
53  /// @param NewReadbackPixelFormat The pixel format of the read back pixels. If the pixel format is Unknown, the read back pixels will have the same format as the source texture
54  /// @param NewReadbackSize The 2d size of the read back pixels. If the size is zero, the read back size will be the same as the size of the source texture
55  void SetSourceTexture(FTexture2DRHIRef NewSourceTexture,
56  const FIntRect& NewSourceRect = FIntRect(),
57  EPixelFormat NewReadbackPixelFormat = EPixelFormat::PF_Unknown,
58  const FIntPoint& NewReadbackSize = FIntPoint::ZeroValue);
59 
60  /// Read back the pixels data from the current source texture
61  /// @param Callback The function to call after all the pixels data are read from the source texture
62  /// @param NewSourceTexture The texture to read from
63  /// @param NewSourceRect The region to read from the source texture. If the region is empty, the whole source texture will be read
64  /// @param NewReadbackPixelFormat The pixel format of the read back pixels. If the pixel format is Unknown, the read back pixels will have the same format as the source texture
65  /// @param NewReadbackSize The 2d size of the read back pixels. If the size is zero, the read back size will be the same as the size of the source texture
66  /// @param bIgnoreAlpha If true, just set the alpha value of the readback pixels to 1, otherwise read it correctly
67  /// NOTE: This function is async, the reading process will run on the rendering thread in parallel with the game thread
68  // TODO: Group these property into a settings struct to make the read pixels function more simple
69  bool ReadPixelsData(OnFinishedReadingPixelsDataCallback Callback,
70  const FTexture2DRHIRef& NewSourceTexture,
71  const FIntRect& NewSourceRect = FIntRect(),
72  EPixelFormat NewReadbackPixelFormat = EPixelFormat::PF_Unknown,
73  const FIntPoint& NewReadbackSize = FIntPoint::ZeroValue,
74  bool bIgnoreAlpha = false);
75 
76  /// Read back the pixels data from the current source texture
77  /// NOTE: This function is sync, the pixels data is returned right away but it may cause the game to hitches since it flush the rendering commands
78  bool ReadPixelsData(FNVTexturePixelData& OutPixelsData,
79  const FTexture2DRHIRef& NewSourceTexture,
80  const FIntRect& NewSourceRect = FIntRect(),
81  EPixelFormat NewReadbackPixelFormat = EPixelFormat::PF_Unknown,
82  const FIntPoint& NewReadbackSize = FIntPoint::ZeroValue);
83 
84  /// Read the pixel data from a render target
85  /// @param SourceTexture The texture to read from
86  /// @param SourceRect The area where to read from the SourceRenderTarget
87  /// @param TargetPixelFormat The pixel format of the read back pixels data
88  /// @param TargetSize The size of the read back pixels area
89  /// @param bIgnoreAlpha If true, just set the alpha value of the readback pixels to 1, otherwise read it correctly
90  /// @param Callback Function to call after finished reading pixels data
91  static bool ReadPixelsRaw(const FTexture2DRHIRef& SourceTexture,
92  const FIntRect& SourceRect,
93  EPixelFormat TargetPixelFormat,
94  const FIntPoint& TargetSize,
95  bool bIgnoreAlpha,
96  OnFinishedReadingRawPixelsCallback Callback);
97 
98  static FNVTexturePixelData BuildPixelData(uint8* PixelsData, EPixelFormat PixelFormat, const FIntPoint& ImageSize, const FIntPoint& TargetSize);
99  static void BuildPixelData(FNVTexturePixelData& OutPixelsData, uint8* PixelsData, EPixelFormat PixelFormat, const FIntPoint& ImageSize, const FIntPoint& TargetSize);
100 
101  /// Copy the pixels data from a texture to another one
102  /// NOTE: The function return back right away but the action is running in the GPU
103  /// @param RendererModule Reference to the Renderer module
104  /// @param RHICmdList The RHI command list used to copy texture. This parameter can be used to wait for the action to be done
105  /// @param SourceTexture The original texture
106  /// @param SourceRect The region to copy from the SourceTexture
107  /// @param TargetTexture The target texture to copy pixels to
108  /// @param TargetRect The region in the TargetTexture to copy pixels to
109  /// @param bOverwriteAlpha If true, overwrite the alpha of the target using the source texture's alpha
110  static void CopyTexture2d(class IRendererModule* RendererModule, FRHICommandListImmediate& RHICmdList, const FTexture2DRHIRef& SourceTexture, const FIntRect& SourceRect,
111  FTexture2DRHIRef& TargetTexture, const FIntRect& TargetRect, bool bOverwriteAlpha = true);
112 
113 protected:
114  FTexture2DRHIRef SourceTexture;
115  FIntRect SourceRect;
116  EPixelFormat ReadbackPixelFormat;
117  FIntPoint ReadbackSize;
118 };
119 
120 class UTextureRenderTarget2D;
121 USTRUCT()
122 struct NVSCENECAPTURER_API FNVTextureRenderTargetReader : public FNVTextureReader
123 {
124  GENERATED_BODY()
125 
126 public:
127  FNVTextureRenderTargetReader(UTextureRenderTarget2D* InRenderTarget = nullptr);
128  virtual ~FNVTextureRenderTargetReader();
129 
130  const UTextureRenderTarget2D* GetTextureRenderTarget() const
131  {
132  return SourceRenderTarget;
133  }
134  void SetTextureRenderTarget(UTextureRenderTarget2D* NewRenderTarget);
135 
136  virtual bool ReadPixelsData(FNVTexturePixelData& OutPixelData) final;
137  virtual bool ReadPixelsData(OnFinishedReadingPixelsDataCallback Callback, bool bIgnoreAlpha = false) final;
138 
139 protected:
140  void UpdateTextureFromRenderTarget();
141 
142 protected:
143  UPROPERTY(Transient)
144  UTextureRenderTarget2D* SourceRenderTarget;
145 };
TFunction< void(const FNVTexturePixelData &)> OnFinishedReadingPixelsDataCallback
Callback function get called after finish reading pixels data FNVTexturePixelData - The struct contai...
TFunction< void(uint8 *, EPixelFormat, FIntPoint)> OnFinishedReadingRawPixelsCallback
Callback function get called after finish reading pixels data uint8* - pointer to pixel array buffer ...