@@ -20,7 +20,7 @@ bool ConvertRAWSurfaceDataToFLinearColor(EPixelFormat Format, uint32 Width, uint
2020#endif
2121
2222UBH_GameRecorder::UBH_GameRecorder (const FObjectInitializer& ObjectInitializer)
23- : Super(ObjectInitializer), bIsRecording(false ), bCopyTextureStarted(false ), StagingTexture(nullptr ), RawDataWidth (0 ), RawDataHeight (0 )
23+ : Super(ObjectInitializer), bIsRecording(false ), bCopyTextureStarted(false ), StagingTexture(nullptr ), BackTextureWidth (0 ), BackTextureHeight (0 )
2424{
2525 FrameBuffer = ObjectInitializer.CreateDefaultSubobject <UBH_FrameBuffer>(this , TEXT (" FrameBuffer" ));
2626}
@@ -162,6 +162,17 @@ void UBH_GameRecorder::Tick(float DeltaTime)
162162 {
163163 AsyncTask (ENamedThreads::AnyBackgroundThreadNormalTask, [this ]()
164164 {
165+ FScopeLock Lock (&AsyncTextureProcessingLock);
166+
167+ BackTextureLock.Lock ();
168+ TextureBuffer.SwapBuffers (); // swap back with current
169+
170+ const uint8 *RawData = reinterpret_cast <const uint8*>(TextureBuffer.GetCurrentBuffer ());
171+ int32 RawDataWidth = BackTextureWidth;
172+ int32 RawDataHeight = BackTextureHeight;
173+
174+ BackTextureLock.Unlock ();
175+
165176 // Make sure that PendingPixels is of the correct size
166177 int32 NumPixels = RawDataWidth * RawDataHeight;
167178
@@ -175,7 +186,8 @@ void UBH_GameRecorder::Tick(float DeltaTime)
175186
176187
177188 // Convert raw surface data to linear color
178- ConvertRAWSurfaceDataToFLinearColor (StagingTextureFormat, RawDataWidth, RawDataHeight, reinterpret_cast <uint8*>(RawData), Pitch, PendingLinearPixels.GetData (), FReadSurfaceDataFlags ({}));
189+ ConvertRAWSurfaceDataToFLinearColor (StagingTextureFormat, RawDataWidth, RawDataHeight, const_cast <uint8*>(RawData),
190+ Pitch, PendingLinearPixels.GetData (), FReadSurfaceDataFlags ({}));
179191
180192 // Convert linear color to FColor
181193 for (int32 i = 0 ; i < NumPixels; ++i)
@@ -323,7 +335,22 @@ void UBH_GameRecorder::ReadPixels()
323335 FRHICopyTextureInfo CopyInfo;
324336 RHICmdList.CopyTexture (GEngine->GameViewport ->Viewport ->GetRenderTargetTexture (), StagingTexture, CopyInfo);
325337
326- RHICmdList.MapStagingSurface (StagingTexture, this ->RawData , this ->RawDataWidth , this ->RawDataHeight );
338+ void * RawDataTemp = nullptr ;
339+ int32 RawDataWidthTemp = 0 ;
340+ int32 RawDataHeightTemp = 0 ;
341+
342+ RHICmdList.MapStagingSurface (StagingTexture, RawDataTemp, RawDataWidthTemp, RawDataHeightTemp);
343+
344+ // copy the memory, because RawDataTemp is temporary
345+ BackTextureLock.Lock ();
346+
347+ TextureBuffer.MemCopyFrom (
348+ reinterpret_cast <uint8*>(RawDataTemp),
349+ RawDataWidthTemp * RawDataHeightTemp * GPixelFormats[StagingTextureFormat].BlockBytes );
350+ BackTextureWidth = RawDataWidthTemp;
351+ BackTextureHeight = RawDataHeightTemp;
352+
353+ BackTextureLock.Unlock ();
327354
328355 RHICmdList.UnmapStagingSurface (StagingTexture);
329356 }
0 commit comments