UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LandscapeAsyncTextureReadback.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "CoreMinimal.h"
5#include "RHIGPUReadback.h"
6#include "RenderGraphFwd.h"
7
8#include <atomic>
9
10// Class that performs an async GPU texture readback for landscape purposes.
11// Only supports BGRA8 texture format, and returns the results as an array of FColors.
13{
14private:
15 // game thread / render thread shared state
16 std::atomic_int PendingRenderThreadCommands = 0; // number of render thread commands queued and not yet executed (for sanity checks)
17 std::atomic_bool bCancel = false; // set by game thread to signal cancel readback: we don't need to produce valid results
18
19 // render thread state
20 std::atomic_bool bStartedOnRenderThread = false; // render thread start command has sent the readback command to the GPU
21 std::atomic_bool bFinishedOnRenderThread = false; // render thread finish command has made the data available to game thread
22
23 TUniquePtr<FRHIGPUTextureReadback> AsyncReadback; // render thread managed async readback structure
24
25 // results - readable by game thread when bFinishedOnRenderThread
26 int32 TextureWidth = 0;
27 int32 TextureHeight = 0;
28 TArray<FColor> ReadbackResults;
29
30public:
31 // construct on the game thread
33
34 // destruct on the render thread. From game thread, call QueueDeletion() instead
39
40 // use this to start an async readback operation from the render thread (on a render graph texture)
41 void StartReadback_RenderThread(FRDGBuilder& GraphBuilder, FRDGTextureRef RDGTexture);
42
43 // checks if the operation is ready to finish, and if it is, calls FinishReadback_RenderThread() to complete the readback.
44 // bInForceFinish will force the readback to finish by stalling the render thread until it is ready
46
47 // use this to finish an async readback operation from the render thread
48 // Calling this when !IsComplete() will cause a stall until the GPU has completed the readback
50
51 // Non-blocking call from the game thread to check readback status and start the finish command if needed.
52 // Returns true when the AsyncReadbackResults are available to the game thread, at which point you can call TakeResults() to access them.
53 // bOutRenderCommandQueued is set to true if any render command was queued in this call, othwerwise unchanged.
54 // You must call this function occasionally or the readback will never complete, as the finish command is required.
55 // bInForceFinish will force the finish process to be queued on the render thread (potentially stalling render thread, but forcing it to finish the readback)
56 // bInForceFinish DOES NOT generally make the results immediately available, but it will ensure they are available after the render thread executes the command.
58
59 // Call from game thread to terminate any readback in flight and queue deletion of this (FLandscapeAsyncTextureReadback) on the render thread
61
62 // Returns true when async readback results are available. Call TakeResults() to retrieve them.
64 {
65 return bFinishedOnRenderThread;
66 }
67
68 // Retrieve the async readback results. Requires readback to be complete.
69 // This function returns its internal memory buffer, relinquishing control over it, so this function can only be called once.
71 {
72 check(bFinishedOnRenderThread);
73 if (OutSize)
74 {
75 *OutSize = FIntPoint(TextureWidth, TextureHeight);
76 }
77 return MoveTemp(ReadbackResults);
78 }
79
80 FString ToString()
81 {
82 FString Result;
83 Result.Appendf(TEXT("FLandscapeAsyncTextureReadback { RTStart: %d RTComplete: %d PendingCommands: %d Cancel: %d AsyncReadback: %p }"), bStartedOnRenderThread.load(), bFinishedOnRenderThread.load(), PendingRenderThreadCommands.load(), bCancel.load(), AsyncReadback.Get());
84 return Result;
85 }
86
87 // once complete, call this to queue deletion of the readback object on the render thread
88 // (this must be deleted on the render thread to avoid other render-thread queued commands from accessing a deallocated pointer)
90};
91
#define check(expr)
Definition AssertionMacros.h:314
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FInt32Point FIntPoint
Definition MathFwd.h:124
CORE_API bool IsInRenderingThread()
Definition ThreadingBase.cpp:273
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition LandscapeAsyncTextureReadback.h:13
bool IsComplete()
Definition LandscapeAsyncTextureReadback.h:63
void QueueDeletionFromGameThread()
Definition LandscapeAsyncTextureReadback.cpp:178
void StartReadback_RenderThread(FRDGBuilder &GraphBuilder, FRDGTextureRef RDGTexture)
Definition LandscapeAsyncTextureReadback.cpp:9
void CheckAndUpdate_RenderThread(const bool bInForceFinish)
Definition LandscapeAsyncTextureReadback.cpp:23
TArray< FColor > TakeResults(FIntPoint *OutSize)
Definition LandscapeAsyncTextureReadback.h:70
void FinishReadback_RenderThread()
Definition LandscapeAsyncTextureReadback.cpp:39
FLandscapeAsyncTextureReadback()
Definition LandscapeAsyncTextureReadback.h:32
void CancelAndSelfDestruct()
Definition LandscapeAsyncTextureReadback.cpp:128
FString ToString()
Definition LandscapeAsyncTextureReadback.h:80
bool CheckAndUpdate(bool &bOutRenderCommandQueued, const bool bInForceFinish)
Definition LandscapeAsyncTextureReadback.cpp:105
~FLandscapeAsyncTextureReadback()
Definition LandscapeAsyncTextureReadback.h:35
Definition RenderGraphBuilder.h:49
Definition RenderGraphResources.h:571
Definition Array.h:670
Definition UniquePtr.h:107
UE_FORCEINLINE_HINT T * Get() const
Definition UniquePtr.h:324
UE_STRING_CLASS & Appendf(UE::Core::TCheckedFormatString< FmtCharType, Types... > Fmt, Types... Args)
Definition UnrealString.h.inl:1440
Definition IntPoint.h:25