CARLA
Loading...
Searching...
No Matches
ImageUtil.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
10
11#include <util/ue-header-guard-begin.h>
12#include <CoreMinimal.h>
13#include <CoreGlobals.h>
14#include <Engine/TextureRenderTarget2D.h>
15#include "Runtime/ImageWriteQueue/Public/ImagePixelData.h"
16#include <util/ue-header-guard-end.h>
17
18#include <functional>
19
21class UTextureRenderTarget2D;
22
23namespace ImageUtil
24{
25 // Callback for the untyped async image reading functions below.
26 using ReadImageDataAsyncCallback = std::function<
27 bool(
28 const void*, // Source image data.
29 size_t, // Number of pixels (NOT BYTES) per image row.
30 size_t, // Number of rows.
31 EPixelFormat, // Image pixel format.
32 FIntPoint // Image extent.
33 )
34 >;
35
36
37
38 // Callback for the FColor async image reading functions below.
39 using ReadImageDataAsyncCallbackFColor = std::function<
40 bool(
41 TArrayView<const FColor>, // Source image data as FColor array.
42 FIntPoint // Image extent.
43 )
44 >;
45
46
47
48 // Callback for the FLinearColor async image reading functions below.
50 bool(
51 TArrayView<const FLinearColor>, // Source image data as FLinearColor array.
52 FIntPoint // Image extent.
53 )
54 >;
55
56
57
58 // Reads pixels in the specified format from a region of memory
59 // into an FColor array.
61 const void* PixelData, // Image data to read from.
62 int32 SourcePitch, // Number of bytes per image row.
63 FIntPoint Extent, // Image extent.
64 EPixelFormat Format, // Image pixel format.
65 FReadSurfaceDataFlags Flags, // Read image flags.
66 TArrayView<FColor> Out // Output array view.
67 );
68
69
70
71 // Reads pixels in the specified format from a region of memory.
72 // into an FLinearColor array.
74 const void* PixelData, // Image data to read from.
75 int32 SourcePitch, // Number of bytes per image row.
76 FIntPoint Extent, // Image extent.
77 EPixelFormat Format, // Image pixel format.
78 FReadSurfaceDataFlags Flags, // Read image flags.
79 TArrayView<FLinearColor> Out // Output array view.
80 );
81
82
83
84 // Reads image data from a UTextureRenderTarget2D using its ReadPixels method.
85 // This function is mainly for testing purposes, as we provide faster alternatives.
86 bool ReadImageData(
87 UTextureRenderTarget2D& RenderTarget, // Render target to read from.
88 TArray<FColor>& Out // Output array.
89 );
90
91
92
93 // Reads image data from a UTextureRenderTarget2D using its ReadPixels method.
94 // This function is mainly for testing purposes, as we provide faster alternatives.
95 bool ReadImageData(
96 UTextureRenderTarget2D& RenderTarget, // Render target to read from.
97 TArray64<FColor>& Out // Output array.
98 );
99
100
101
102 // Reads image data from a UTextureRenderTarget2D using its ReadPixels method.
103 // This function is mainly for testing purposes, as we provide faster alternatives.
104 TUniquePtr<TImagePixelData<FColor>> ReadImageData(
105 UTextureRenderTarget2D& RenderTarget // Render target to read from.
106 );
107
108
109
110 // Reads and outputs the contents of a render target to a PNG file.
111 // Currently uses ReadImageData under the hood.
112 TFuture<bool> SaveImageData(
113 UTextureRenderTarget2D& RenderTarget, // Render target to read from.
114 const FStringView& Path // Output image file path.
115 );
116
117
118
119 // Given a TImagePixelData<FColor>, saves it into a PNG file.
120 TFuture<bool> SaveImageData(
121 TUniquePtr<TImagePixelData<FColor>> Data, // Render target to read from.
122 const FStringView& Path // Output image file path.
123 );
124
125
126
127 // Asynchronously reads the contents of the specified RenderTarget
128 // and calls the provided Callback.
130 UTextureRenderTarget2D& RenderTarget, // Render target to read from.
131 ReadImageDataAsyncCallback&& Callback // Callback to invoke when the image is available.
132 );
133
134
135
136 // Pool-aware variant: when Pool is non-null, the per-call FRHIGPUTextureReadback
137 // alloc is recycled through the per-sensor pool.
138 //
139 // bNonBlocking (default false = original behaviour for every existing caller):
140 // when true the render thread only records the copy and returns; completion is
141 // polled off-thread. Used by the path-traced rt_lens sensor, whose readback
142 // must never wait on the GPU on the render thread -- that wait deadlocks the
143 // server under load (see ImageUtil.cpp).
144 //
145 // Blocking (default) calls do NOT deliver on their own anymore: they record
146 // the copy and join the current tick's batch, which is synchronized and
147 // delivered by FlushBatchedReadbacks() (called once per tick by
148 // FSensorManager after every sensor has enqueued its capture).
150 UTextureRenderTarget2D& RenderTarget,
153 bool bNonBlocking = false
154 );
155
156
157
158 // Synchronize the GPU once and deliver every blocking readback recorded
159 // since the previous flush. One pipeline drain per tick for all cameras
160 // together, instead of the historical drain-per-camera, while keeping the
161 // per-tick "all sensors delivered" simulation guarantee.
163
164
165
166 // Asynchronously reads the contents of the specified AShaderBasedSensor
167 // and calls the provided Callback. Uses ReadImageDataAsync underneath.
169 AShaderBasedSensor& Sensor, // ShaderBasedSensor to read from.
170 ReadImageDataAsyncCallback&& Callback // Callback to invoke when the image is available.
171 );
172
173
174
175 // Asynchronously reads the contents of the specified AShaderBasedSensor
176 // and calls the provided Callback. Uses ReadImageDataAsync underneath.
177 // This variant converts the raw pixel data to an FColor array before invoking Callback.
179 UTextureRenderTarget2D& RenderTarget, // Render target to read from.
180 ReadImageDataAsyncCallbackFColor&& Callback, // Callback to invoke when the image is available.
181 bool bNonBlocking = false, // true = render thread never waits on GPU (rt_lens; see ImageUtil.cpp).
182 FRHIGPUReadbackPoolPtr Pool = nullptr // recycling readback pool; avoids a per-frame staging-buffer alloc.
183 );
184
185
186
187 // Asynchronously reads the contents of the specified AShaderBasedSensor
188 // and calls the provided Callback. Uses ReadImageDataAsync underneath.
189 // This variant converts the raw pixel data to an FLinearColor array before invoking Callback.
191 AShaderBasedSensor& Sensor, // ShaderBasedSensor to read from.
192 ReadImageDataAsyncCallbackFColor&& Callback // Callback to invoke when the image is available.
193 );
194
195
196
197 // Asynchronously reads the contents of the specified AShaderBasedSensor
198 // and calls the provided Callback. Uses ReadImageDataAsync underneath.
199 // This variant converts the raw pixel data to an FLinearColor array before invoking Callback.
201 UTextureRenderTarget2D& RenderTarget, // Render target to read from.
202 ReadImageDataAsyncCallbackFLinearColor&& Callback, // Callback to invoke when the image is available.
203 bool bNonBlocking = false, // true = render thread never waits on GPU (see ImageUtil.cpp).
204 FRHIGPUReadbackPoolPtr Pool = nullptr // recycling readback pool; avoids a per-frame staging-buffer alloc.
205 );
206
207
208
209 // Asynchronously reads the contents of the specified AShaderBasedSensor
210 // and calls the provided Callback. Uses ReadImageDataAsync underneath.
211 // This variant converts the raw pixel data to an FLinearColor array before invoking Callback.
213 AShaderBasedSensor& Sensor, // ShaderBasedSensor to read from.
214 ReadImageDataAsyncCallbackFLinearColor&& Callback // Callback to invoke when the image is available.
215 );
216}
TSharedPtr< FRHIGPUReadbackPool, ESPMode::ThreadSafe > FRHIGPUReadbackPoolPtr
A sensor that produces data by applying post-process materials (shaders) to a scene capture image.
void FlushBatchedReadbacks()
bool DecodePixelsByFormat(const void *PixelData, int32 SourcePitch, FIntPoint Extent, EPixelFormat Format, FReadSurfaceDataFlags Flags, TArrayView< FLinearColor > Out)
Definition ImageUtil.cpp:38
bool ReadImageDataAsync(UTextureRenderTarget2D &RenderTarget, FRHIGPUReadbackPoolPtr Pool, ReadImageDataAsyncCallback &&Callback, bool bNonBlocking)
std::function< bool(const void *, size_t, size_t, EPixelFormat, FIntPoint) > ReadImageDataAsyncCallback
Definition ImageUtil.h:34
std::function< bool(TArrayView< const FLinearColor >, FIntPoint) > ReadImageDataAsyncCallbackFLinearColor
Definition ImageUtil.h:54
std::function< bool(TArrayView< const FColor >, FIntPoint) > ReadImageDataAsyncCallbackFColor
Definition ImageUtil.h:44
bool ReadSensorImageDataAsyncFColor(AShaderBasedSensor &Sensor, ReadImageDataAsyncCallbackFColor &&Callback)
bool ReadImageDataAsyncFColor(UTextureRenderTarget2D &RenderTarget, ReadImageDataAsyncCallbackFColor &&Callback, bool bNonBlocking, FRHIGPUReadbackPoolPtr Pool)
bool ReadSensorImageDataAsync(AShaderBasedSensor &Sensor, ReadImageDataAsyncCallback &&Callback)
TFuture< bool > SaveImageData(UTextureRenderTarget2D &RenderTarget, const FStringView &Path)
bool ReadImageDataAsyncFLinearColor(UTextureRenderTarget2D &RenderTarget, ReadImageDataAsyncCallbackFLinearColor &&Callback, bool bNonBlocking, FRHIGPUReadbackPoolPtr Pool)
bool ReadImageData(UTextureRenderTarget2D &RenderTarget, TArray< FColor > &Out)
bool ReadSensorImageDataAsyncFLinearColor(AShaderBasedSensor &Sensor, ReadImageDataAsyncCallbackFLinearColor &&Callback)