UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RenderTargetPool.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 RenderTargetPool.h: Scene render target pool manager.
5=============================================================================*/
6
7#pragma once
8
9#include "Containers/Array.h"
10#include "CoreMinimal.h"
11#include "CoreTypes.h"
12#include "HAL/PlatformAtomics.h"
13#include "RHI.h"
16#include "RenderResource.h"
17#include "RendererInterface.h"
20
21class FOutputDevice;
22class FRHICommandList;
24
27{
35
37 {
38 return UnusedForNFrames;
39 }
40
41 const FPooledRenderTargetDesc& GetDesc() const override { return Desc; }
42
43 uint32 AddRef() const override
44 {
45 return uint32(FPlatformAtomics::InterlockedIncrement(&NumRefs));
46 }
47
48 uint32 Release() override
49 {
50 const int32 Refs = FPlatformAtomics::InterlockedDecrement(&NumRefs);
51 if (Refs == 0)
52 {
53 delete this;
54 }
55 return uint32(Refs);
56 }
57
58 uint32 GetRefCount() const override
59 {
60 return uint32(NumRefs);
61 }
62
63 RENDERCORE_API bool IsFree() const override;
64 bool IsTracked() const override { return RenderTargetPool != nullptr; }
66
67private:
68 RENDERCORE_API void SetDebugLabelName(FRHICommandListBase& RHICmdList, const TCHAR* Name);
69
71 FRenderTargetPool* RenderTargetPool;
72
75
77 mutable int32 NumRefs = 0;
78
80 uint32 UnusedForNFrames = 0;
81
83 FRDGPooledTexture PooledTexture;
84
86 RENDERCORE_API bool OnFrameStart();
87
88 friend class FRDGTexture;
89 friend class FRDGBuilder;
90 friend class FRenderTargetPool;
91};
92
97{
98public:
99 FRenderTargetPool() = default;
100
102
104
109
114
122 FRHICommandListBase& RHICmdList,
123 const FPooledRenderTargetDesc& Desc,
125 const TCHAR* InDebugName)
126 {
127 return FindFreeElement(RHICmdList, Translate(Desc), Out, InDebugName);
128 }
129
131
141
144
147
148 // for debugging purpose, assumes you call FlushRenderingCommands() be
149 // @return can be 0, that doesn't mean iteration is done
151
152 uint32 GetElementCount() const { return PooledRenderTargets.Num(); }
153
154 // @return -1 if not found
156
157 // Logs out usage information.
158 RENDERCORE_API void DumpMemoryUsage(FOutputDevice& OutputDevice);
159
160private:
161 void FreeElementAtIndex(int32 Index);
162
163 FPooledRenderTarget* CreateRenderTarget(FRHICommandListBase& RHICmdList, const FRHITextureCreateInfo& Desc, uint32 DescHash, const TCHAR* Name);
164
165 template <typename T>
166 FPooledRenderTarget* TryFindRenderTarget(const FRHITextureCreateInfo& Desc, uint32 DescHash, T&& Predicate) const;
167
168 FPooledRenderTarget* TryFindRenderTarget(const FRHITextureCreateInfo& Desc, uint32 DescHash) const
169 {
170 return TryFindRenderTarget(Desc, DescHash, [](FPooledRenderTarget*) { return true; });
171 }
172
174 // Methods for scheduling allocations for RDG
175
176 FPooledRenderTarget* ScheduleAllocation(
177 FRHICommandListBase& RHICmdList,
179 const TCHAR* Name,
180 const FRHITransientAllocationFences& Fences);
181
182 void ScheduleDeallocation(FPooledRenderTarget* RenderTarget, const FRHITransientAllocationFences& Fences);
183
184 void FinishSchedule(FRHICommandListBase& RHICmdList, FPooledRenderTarget* RenderTarget, const TCHAR* Name);
185
187
188 mutable UE::FRecursiveMutex Mutex;
189
191 TArray<uint32> PooledRenderTargetHashes;
192 TArray< TRefCountPtr<FPooledRenderTarget> > PooledRenderTargets;
193 TArray< TRefCountPtr<FPooledRenderTarget> > DeferredDeleteArray;
194
195 // redundant, can always be computed with GetStats(), to debug "out of memory" situations and used for r.RenderTargetPoolMin
196 uint32 AllocationLevelInKB = 0;
197
198 // to avoid log spam
199 bool bCurrentlyOverBudget = false;
200
201 // could be done on the fly but that makes the RenderTargetPoolEvents harder to read
202 void CompactPool();
203
204 friend struct FPooledRenderTarget;
205 friend class FVisualizeTexture;
207 friend class FRDGBuilder;
208};
209
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
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
D3D12_DESCRIPTOR_HEAP_TYPE Translate(ERHIDescriptorHeapType InHeapType)
Definition D3D12Descriptors.h:19
RENDERCORE_API TGlobalResource< FRenderTargetPool > GRenderTargetPool
Definition RenderTargetPool.cpp:13
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition OutputDevice.h:133
Definition RenderGraphBuilder.h:49
Definition RenderGraphResources.h:537
Definition RenderGraphResources.h:571
Definition RHICommandList.h:455
static FRHICommandListImmediate & Get()
Definition RHICommandList.h:5522
Definition RHICommandList.h:3819
Definition RHIResources.h:2153
Definition RHITransientResourceAllocator.h:23
Definition RenderResource.h:37
Definition RenderTargetPool.h:97
RENDERCORE_API TRefCountPtr< IPooledRenderTarget > FindFreeElement(FRHICommandListBase &RHICmdList, FRHITextureCreateInfo Desc, const TCHAR *Name)
Definition RenderTargetPool.cpp:199
RENDERCORE_API void FreeUnusedResources()
Definition RenderTargetPool.cpp:487
RENDERCORE_API void GetStats(uint32 &OutWholeCount, uint32 &OutWholePoolInKB, uint32 &OutUsedInKB) const
Definition RenderTargetPool.cpp:287
RENDERCORE_API void TickPoolElements()
Definition RenderTargetPool.cpp:315
FRenderTargetPool()=default
RENDERCORE_API void CreateUntrackedElement(const FPooledRenderTargetDesc &Desc, TRefCountPtr< IPooledRenderTarget > &Out, const FSceneRenderTargetItem &Item)
Definition RenderTargetPool.cpp:280
RENDERCORE_API FPooledRenderTarget * GetElementById(uint32 Id) const
Definition RenderTargetPool.cpp:613
bool FindFreeElement(FRHICommandListBase &RHICmdList, const FPooledRenderTargetDesc &Desc, TRefCountPtr< IPooledRenderTarget > &Out, const TCHAR *InDebugName)
Definition RenderTargetPool.h:121
TRefCountPtr< IPooledRenderTarget > FindFreeElement(FRHITextureCreateInfo Desc, const TCHAR *Name)
Definition RenderTargetPool.h:105
RENDERCORE_API void ReleaseRHI()
Definition RenderTargetPool.cpp:605
RENDERCORE_API int32 FindIndex(IPooledRenderTarget *In) const
Definition RenderTargetPool.cpp:432
bool FindFreeElement(const FRHITextureCreateInfo &Desc, TRefCountPtr< IPooledRenderTarget > &Out, const TCHAR *Name)
Definition RenderTargetPool.h:110
RENDERCORE_API void FreeUnusedResource(TRefCountPtr< IPooledRenderTarget > &In)
Definition RenderTargetPool.cpp:460
uint32 GetElementCount() const
Definition RenderTargetPool.h:152
RENDERCORE_API void DumpMemoryUsage(FOutputDevice &OutputDevice)
Definition RenderTargetPool.cpp:530
Definition VisualizeTexturePresent.h:8
Definition VisualizeTexture.h:25
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition RenderResource.h:543
Definition RefCounting.h:454
Definition RecursiveMutex.h:19
U16 Index
Definition radfft.cpp:71
Definition RendererInterface.h:81
Definition RenderTargetPool.h:27
RENDERCORE_API bool IsFree() const override
Definition RenderTargetPool.cpp:709
uint32 AddRef() const override
Definition RenderTargetPool.h:43
uint32 GetUnusedForNFrames() const
Definition RenderTargetPool.h:36
uint32 Release() override
Definition RenderTargetPool.h:48
RENDERCORE_API uint32 ComputeMemorySize() const override
Definition RenderTargetPool.cpp:679
FPooledRenderTarget(FRHITexture *Texture, const FPooledRenderTargetDesc &InDesc, FRenderTargetPool *InRenderTargetPool)
Definition RenderTargetPool.h:28
const FPooledRenderTargetDesc & GetDesc() const override
Definition RenderTargetPool.h:41
uint32 GetRefCount() const override
Definition RenderTargetPool.h:58
bool IsTracked() const override
Definition RenderTargetPool.h:64
Definition RHIResources.h:1689
Definition RendererInterface.h:453
FTextureRHIRef ShaderResourceTexture
Definition RendererInterface.h:484
FTextureRHIRef TargetableTexture
Definition RendererInterface.h:481
Definition RendererInterface.h:494
FSceneRenderTargetItem RenderTargetItem
Definition RendererInterface.h:524