A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VRRenderTargetManager.h
Go to the documentation of this file.
1#pragma once
2
3#include "Serialization/ArchiveSaveCompressedProxy.h"
4#include "Serialization/ArchiveLoadCompressedProxy.h"
5
6//#include "RenderUtils.h"
7#include "GeomTools.h"
8//#include "DrawDebugHelpers.h"
9#include "Engine/TextureRenderTarget2D.h"
10#include "Kismet/KismetMathLibrary.h"
11#include "Kismet/KismetRenderingLibrary.h"
12#include "Engine/CanvasRenderTarget2D.h"
13//#include "Net/Core/PushModel/PushModel.h"
14#include "TimerManager.h"
15#include "GameFramework/PlayerController.h"
16#include "Engine/Canvas.h"
17#include "Materials/Material.h"
18//#include "ImageWrapper/Public/IImageWrapper.h"
19//#include "ImageWrapper/Public/IImageWrapperModule.h"
20
21#include "VRRenderTargetManager.generated.h"
22
24
25// #TODO: Dirty rects so don't have to send entire texture?
26
27
28USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
29struct VREXPANSIONPLUGIN_API FBPVRReplicatedTextureStore
30{
32public:
33
34 // Not automatically replicated, we are skipping it so that the array isn't checked
35 // We manually copy the data into the serialization buffer during netserialize and keep
36 // a flip flop dirty flag
37
38 UPROPERTY(Transient)
39 TArray<uint8> PackedData;
40
41 UPROPERTY(Transient)
42 TArray<uint16> UnpackedData;
43
44 UPROPERTY(Transient)
45 uint32 Width;
46
47 UPROPERTY()
48 uint32 Height;
49
50 UPROPERTY(Transient)
51 bool bIsZipped;
52
53 //UPROPERTY()
54 // bool bJPG;
55 //UPROPERTY(Transient)
56 EPixelFormat PixelFormat;
59 {
60 Width = 0;
61 Height = 0;
62 bIsZipped = false;
63 }
64
65 void Reset()
66 {
67 PackedData.Reset();
68 UnpackedData.Reset();
69 Width = 0;
70 Height = 0;
71 PixelFormat = (EPixelFormat)0;
72 bIsZipped = false;
73 //bJPG = false;
74 }
76 void PackData();
77 void UnPackData();
78
79
81 // Doing a custom NetSerialize here because this is sent via RPCs and should change on every update
82 bool NetSerialize(FArchive& Ar, class UPackageMap* Map, bool& bOutSuccess);
83
84};
85
86template<>
87struct TStructOpsTypeTraits< FBPVRReplicatedTextureStore > : public TStructOpsTypeTraitsBase2<FBPVRReplicatedTextureStore>
88{
89 enum
90 {
91 WithNetSerializer = true,
92 WithNetSharedSerialization = true,
93 };
94};
95
96
97USTRUCT()
98struct FRenderDataStore {
99 GENERATED_BODY()
100
101 TArray<FColor> ColorData;
102 FRenderCommandFence RenderFence;
103 FIntPoint Size2D;
104 EPixelFormat PixelFormat;
105
107 }
110UENUM(BlueprintType)
111enum class ERenderManagerOperationType : uint8
112{
113 Op_LineDraw = 0x00,
114 Op_TriDraw = 0x01,
115 Op_TexDraw = 0x02
116};
119USTRUCT()
121 GENERATED_BODY()
122public:
123 FVector2D P1;
124 FVector2D P2;
125 FVector2D P3;
126};
127
128USTRUCT()
130 GENERATED_BODY()
131public:
132
133 UPROPERTY()
134 uint32 OwnerID;
135
136 UPROPERTY()
137 ERenderManagerOperationType OperationType;
138
139 UPROPERTY()
140 FColor Color;
141
142 UPROPERTY()
143 FVector2D P1;
145 UPROPERTY()
146 FVector2D P2;
148 UPROPERTY()
149 uint32 Thickness;
150
151 UPROPERTY()
152 TArray<FRenderManagerTri> Tris;
153
154 UPROPERTY()
155 TSoftObjectPtr<UTexture2D> Texture;
157 UPROPERTY()
158 TSoftObjectPtr<UMaterial> Material;
159
161 {
162 OwnerID = 0;
164 Color = FColor::White;
165 P1 = FVector2D::ZeroVector;
166 P2 = FVector2D::ZeroVector;
167 Thickness = 0;
168 }
169
170 bool NetSerialize(FArchive& Ar, class UPackageMap* Map, bool& bOutSuccess);
171};
172template<>
173struct TStructOpsTypeTraits< FRenderManagerOperation > : public TStructOpsTypeTraitsBase2<FRenderManagerOperation>
174{
175 enum
176 {
177 WithNetSerializer = true,
178 WithNetSharedSerialization = true,
179 };
180};
181
185UCLASS(ClassGroup = (VRExpansionPlugin))
186class VREXPANSIONPLUGIN_API ARenderTargetReplicationProxy : public AActor
188 GENERATED_BODY()
189
190public:
191 ARenderTargetReplicationProxy(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
193 UPROPERTY(Replicated, ReplicatedUsing = OnRep_Manager)
194 TWeakObjectPtr<UVRRenderTargetManager> OwningManager;
195
196 UPROPERTY(Replicated)
197 uint32 OwnersID;
198
199 UFUNCTION()
200 void OnRep_Manager();
201
202 UPROPERTY(Transient)
203 FBPVRReplicatedTextureStore TextureStore;
205 UPROPERTY(Transient)
206 int32 BlobNum;
207
208 bool bWaitingForManager;
209
210 void SendInitMessage();
211
212 UFUNCTION()
213 void SendNextDataBlob();
214
215 FTimerHandle SendTimer_Handle;
216 FTimerHandle CheckManager_Handle;
218 // Maximum size of texture blobs to use for sending (size of chunks that it gets broken down into)
219 UPROPERTY()
220 int32 TextureBlobSize;
222 // Maximum bytes per second to send, you will want to play around with this and the
223 // MaxClientRate settings in config in order to balance the bandwidth and avoid saturation
224 // If you raise this above the max replication size of a 65k byte size then you will need
225 // To adjust the max size in engine network settings.
226 UPROPERTY()
227 int32 MaxBytesPerSecondRate;
228
229 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
230 {
231 if(SendTimer_Handle.IsValid())
232 GetWorld()->GetTimerManager().ClearTimer(SendTimer_Handle);
233
234 Super::EndPlay(EndPlayReason);
235 }
236
237
238 UFUNCTION(Reliable, Server, WithValidation)
239 void SendLocalDrawOperations(const TArray<FRenderManagerOperation>& LocalRenderOperationStoreList);
240
241 UFUNCTION(Reliable, Client)
242 void InitTextureSend(int32 Width, int32 Height, int32 TotalDataCount, int32 BlobCount, EPixelFormat PixelFormat, bool bIsZipped/*, bool bIsJPG*/);
244 UFUNCTION(Reliable, Server, WithValidation)
245 void Ack_InitTextureSend(int32 TotalDataCount);
246
247 UFUNCTION(Reliable, Client)
248 void ReceiveTextureBlob(const TArray<uint8>& TextureBlob, int32 LocationInData, int32 BlobCount);
250 UFUNCTION(Reliable, Server, WithValidation)
251 void Ack_ReceiveTextureBlob(int32 BlobCount);
252
253 UFUNCTION(Reliable, Client)
254 void ReceiveTexture(const FBPVRReplicatedTextureStore&TextureData);
255
256};
257
258
259
260USTRUCT()
261struct FClientRepData {
262 GENERATED_BODY()
263
264 UPROPERTY()
265 TWeakObjectPtr<APlayerController> PC;
267 UPROPERTY()
268 TWeakObjectPtr<ARenderTargetReplicationProxy> ReplicationProxy;
269
270 UPROPERTY()
271 bool bIsRelevant;
272
273 UPROPERTY()
274 bool bIsDirty;
275
278 bIsRelevant = false;
279 bIsDirty = false;
280 }
281};
282
283
289UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = (VRExpansionPlugin))
290class VREXPANSIONPLUGIN_API UVRRenderTargetManager : public UActorComponent
291{
292 GENERATED_BODY()
294public:
296 UVRRenderTargetManager(const FObjectInitializer& ObjectInitializer);
297
298 uint32 OwnerIDCounter;
299
300 UPROPERTY(Transient)
301 TWeakObjectPtr<ARenderTargetReplicationProxy> LocalProxy;
302
303 TArray<FRenderManagerOperation> RenderOperationStore;
304 TArray<FRenderManagerOperation> LocalRenderOperationStore;
305
306 // Rate to poll for drawing new operations
307 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
308 float DrawRate;
309
310 UFUNCTION(Reliable, NetMultiCast, WithValidation)
311 void SendDrawOperations(const TArray<FRenderManagerOperation>& RenderOperationStoreList);
312
313 UFUNCTION(BlueprintCallable, Category = "VRRenderTargetManager|DrawingFunctions")
314 void AddLineDrawOperation(FVector2D Point1, FVector2D Point2, FColor Color, int32 Thickness);
315
316 UFUNCTION(BlueprintCallable, Category = "VRRenderTargetManager|DrawingFunctions")
317 void AddTextureDrawOperation(FVector2D Position, UTexture2D* TextureToDisplay);
318
319 // Adds a draw operation for a triangle list, only takes the first vertex's color
320 UFUNCTION(BlueprintCallable, Category = "VRRenderTargetManager|DrawingFunctions")
321 void AddMaterialTrianglesDrawOperation(TArray<FCanvasUVTri> Tris, UMaterial* Material);
322
323 void DrawOperation(UCanvas* Canvas, const FRenderManagerOperation& Operation);
324
325 UFUNCTION()
326 void DrawPoll();
327
328 void DrawOperations();
329
330 UPROPERTY()
331 FTimerHandle DrawHandle;
333 UPROPERTY(Transient)
334 bool bIsStoringImage;
335
336 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
337 bool bInitiallyReplicateTexture;
338
339 UPROPERTY(Transient)
340 bool bIsLoadingTextureBuffer;
341
342 // Maximum size of texture blobs to use for sending (size of chunks that it gets broken down into)
343 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
344 int32 TextureBlobSize;
345
346 // Maximum bytes per second to send, you will want to play around with this and the
347 // MaxClientRate settings in config in order to balance the bandwidth and avoid saturation
348 // If you raise this above the max replication size of a 65k byte size then you will need
349 // To adjust the max size in engine network settings.
350 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
351 int32 MaxBytesPerSecondRate;
352
353 UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "RenderTargetManager")
354 UCanvasRenderTarget2D* RenderTarget;
355
356 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
357 int32 RenderTargetWidth;
358
359 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
360 int32 RenderTargetHeight;
361
362 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
363 FColor ClearColor;
364
365 UPROPERTY(Transient)
366 TArray<FClientRepData> NetRelevancyLog;
368 // Rate to poll for actor relevancy
369 UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
370 float PollRelevancyTime;
371
372 FTimerHandle NetRelevancyTimer_Handle;
373
374 UPROPERTY(Transient)
375 FBPVRReplicatedTextureStore RenderTargetStore;
376
377 UFUNCTION(BlueprintCallable, Category = "VRRenderTargetManager|UtilityFunctions")
378 bool GenerateTrisFromBoxPlaneIntersection(UPrimitiveComponent* PrimToBoxCheck, FTransform WorldTransformOfPlane, const FPlane& LocalProjectionPlane, FVector2D PlaneSize, FColor UVColor, TArray<FCanvasUVTri>& OutTris);
380 // Create the render target that we are managing
381 void InitRenderTarget();
382
383 // Update the list of players that we are checking for relevancy
384 void UpdateRelevancyMap();
385
386 // Decompress the render target data to a texture and copy it to our managed render target
387 bool DeCompressRenderTarget2D();
388
389 // Queues storing the render target image to our buffer
390 void QueueImageStore();
391
392 virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
393 virtual void BeginPlay() override;
394 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
395
396protected:
397 TQueue<FRenderDataStore*> RenderDataQueue;
399};
ERenderManagerOperationType
UENUM(BlueprintType)
UCLASS(ClassGroup = (VRExpansionPlugin))
void InitTextureSend(int32 Width, int32 Height, int32 TotalDataCount, int32 BlobCount, EPixelFormat PixelFormat, bool bIsZipped)
UFUNCTION(Reliable, Client)
int32 BlobNum
UPROPERTY(Transient)
FBPVRReplicatedTextureStore TextureStore
UPROPERTY(Transient)
void ReceiveTexture(const FBPVRReplicatedTextureStore &TextureData)
UFUNCTION(Reliable, Client)
TWeakObjectPtr< UVRRenderTargetManager > OwningManager
UPROPERTY(Replicated, ReplicatedUsing = OnRep_Manager)
void ReceiveTextureBlob(const TArray< uint8 > &TextureBlob, int32 LocationInData, int32 BlobCount)
UFUNCTION(Reliable, Client)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
void Ack_InitTextureSend(int32 TotalDataCount)
UFUNCTION(Reliable, Server, WithValidation)
void SendLocalDrawOperations(const TArray< FRenderManagerOperation > &LocalRenderOperationStoreList)
UFUNCTION(Reliable, Server, WithValidation)
uint32 OwnersID
UPROPERTY(Replicated)
void Ack_ReceiveTextureBlob(int32 BlobCount)
UFUNCTION(Reliable, Server, WithValidation)
UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = (VRExpansionPlugin))
float DrawRate
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
bool bIsLoadingTextureBuffer
UPROPERTY(Transient)
UCanvasRenderTarget2D * RenderTarget
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "RenderTargetManager")
TArray< FRenderManagerOperation > LocalRenderOperationStore
int32 TextureBlobSize
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
bool bIsStoringImage
UPROPERTY(Transient)
int32 MaxBytesPerSecondRate
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
FBPVRReplicatedTextureStore RenderTargetStore
UPROPERTY(Transient)
FTimerHandle DrawHandle
UPROPERTY()
TArray< FClientRepData > NetRelevancyLog
UPROPERTY(Transient)
TWeakObjectPtr< ARenderTargetReplicationProxy > LocalProxy
UPROPERTY(Transient)
FColor ClearColor
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
int32 RenderTargetHeight
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
TArray< FRenderManagerOperation > RenderOperationStore
bool bInitiallyReplicateTexture
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
float PollRelevancyTime
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
void SendDrawOperations(const TArray< FRenderManagerOperation > &RenderOperationStoreList)
UFUNCTION(Reliable, NetMultiCast, WithValidation)
TQueue< FRenderDataStore * > RenderDataQueue
int32 RenderTargetWidth
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RenderTargetManager")
USTRUCT(BlueprintType, Category = "VRExpansionLibrary")
TArray< uint16 > UnpackedData
UPROPERTY(Transient)
TArray< uint8 > PackedData
UPROPERTY(Transient)
bool bIsZipped
UPROPERTY(Transient)
uint32 Width
UPROPERTY(Transient)
bool bIsDirty
UPROPERTY()
bool bIsRelevant
UPROPERTY()
TWeakObjectPtr< APlayerController > PC
UPROPERTY()
TWeakObjectPtr< ARenderTargetReplicationProxy > ReplicationProxy
UPROPERTY()
FRenderCommandFence RenderFence
TArray< FColor > ColorData
ERenderManagerOperationType OperationType
UPROPERTY()
TSoftObjectPtr< UTexture2D > Texture
UPROPERTY()
TSoftObjectPtr< UMaterial > Material
UPROPERTY()
bool NetSerialize(FArchive &Ar, class UPackageMap *Map, bool &bOutSuccess)
TArray< FRenderManagerTri > Tris
UPROPERTY()