CARLA
Loading...
Searching...
No Matches
RHIGPUReadbackPool.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
9#include <util/ue-header-guard-begin.h>
10#include "CoreMinimal.h"
11#include "HAL/CriticalSection.h"
12#include "RHIGPUReadback.h"
13#include "Templates/SharedPointer.h"
14#include "Templates/UniquePtr.h"
15#include <util/ue-header-guard-end.h>
16
17// Pool of FRHIGPUTextureReadback objects shared by one ASceneCaptureSensor.
18// The pool is owned via TSharedPtr<...,ESPMode::ThreadSafe>: the sensor holds
19// one strong ref; every in-flight AsyncTask holds another strong ref so the
20// pool outlives the sensor if a frame is still being consumed when the
21// sensor ends play. Acquire/Release run from different threads (render
22// thread for Acquire, AsyncTask for Release) and are guarded by Lock.
23//
24// Slots are lazy-allocated up to MaxSize; once allocated, an FRHIGPUTextureReadback
25// instance and its underlying Vulkan staging buffer live for the rest of the
26// pool's lifetime, recycled across frames. Eliminates the per-frame
27// alloc/free of large staging buffers that was driving Vulkan suballocator
28// fragmentation.
30{
31public:
32
33 explicit FRHIGPUReadbackPool(const TCHAR *InDebugName, int32 InMaxSize = 4)
34 : DebugName(InDebugName), MaxSize(InMaxSize) {}
35
36 // Returns a raw pointer (pool retains ownership) and writes the slot index
37 // into OutSlotIndex. Returns nullptr only if every slot is in use AND we
38 // already grew to MaxSize; the caller falls back to a per-call alloc.
39 FRHIGPUTextureReadback *Acquire(int32 &OutSlotIndex);
40
41 // Marks the slot free. The FRHIGPUTextureReadback object stays alive and
42 // its Vulkan staging buffer is reused on the next Acquire.
43 void Release(int32 SlotIndex);
44
45private:
46
47 FCriticalSection Lock;
48 TArray<TUniquePtr<FRHIGPUTextureReadback>> Slots;
49 TArray<bool> InUse;
50 FString DebugName;
51 int32 MaxSize;
52};
53
54using FRHIGPUReadbackPoolPtr = TSharedPtr<FRHIGPUReadbackPool, ESPMode::ThreadSafe>;
TSharedPtr< FRHIGPUReadbackPool, ESPMode::ThreadSafe > FRHIGPUReadbackPoolPtr
FRHIGPUTextureReadback * Acquire(int32 &OutSlotIndex)
FCriticalSection Lock
void Release(int32 SlotIndex)
TArray< TUniquePtr< FRHIGPUTextureReadback > > Slots
FRHIGPUReadbackPool(const TCHAR *InDebugName, int32 InMaxSize=4)