Guest User

Forever Loop in Unreal Editor save Level

a guest
Dec 28th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1.         // Loop while there is work left to do based on whether we have finalized all chunks yet.
  2.         while( NumChunksLeftToFinalize )
  3.         {
  4.             // If true we are waiting for async tasks to complete and should wait to complete some
  5.             // if there are no async tasks finishing this iteration.
  6.             bool bNeedToWaitForAsyncTask = false;
  7.  
  8.             // Try to kick off async tasks if there are chunks left to kick off.
  9.             if( NumChunksLeftToKickOff )
  10.             {
  11.                 // Find free index based on looking at uncompressed size. We can't use the thread counter
  12.                 // for this as that might be a chunk ready for finalization.
  13.                 int32 FreeIndex = INDEX_NONE;
  14.                 for( int32 i=0; i<MaxConcurrentAsyncChunks; i++ )
  15.                 {
  16.                     if( !AsyncChunkIndex[i] )
  17.                     {
  18.                         FreeIndex = i;
  19.                         check(AsyncChunks[FreeIndex].IsIdle()); // this is not supposed to be in use
  20.                         break;
  21.                     }
  22.                 }
  23.  
  24.                 // Kick off async compression task if we found a chunk for it.
  25.                 if( FreeIndex != INDEX_NONE )
  26.                 {
  27.                     FAsyncCompressionChunk& NewChunk = AsyncChunks[FreeIndex].GetTask();
  28.                     // 2 times the uncompressed size should be more than enough; the compressed data shouldn't be that much larger
  29.                     NewChunk.CompressedSize = 2 * GSavingCompressionChunkSize;
  30.                     // Allocate compressed buffer placeholder on first use.
  31.                     if( NewChunk.CompressedBuffer == NULL )
  32.                     {
  33.                         NewChunk.CompressedBuffer = FMemory::Malloc( NewChunk.CompressedSize    );
  34.                     }
  35.  
  36.                     // By default everything is chunked up into GSavingCompressionChunkSize chunks.
  37.                     NewChunk.UncompressedSize   = FMath::Min( BytesRemainingToKickOff, (int64)GSavingCompressionChunkSize );
  38.                     check(NewChunk.UncompressedSize>0);
  39.  
  40.                     // Need to serialize source data if passed in pointer is an FArchive.
  41.                     if( bTreatBufferAsFileReader )
  42.                     {
  43.                         // Allocate memory on first use. We allocate the maximum amount to allow reuse.
  44.                         if( !NewChunk.UncompressedBuffer )
  45.                         {
  46.                             NewChunk.UncompressedBuffer = FMemory::Malloc(GSavingCompressionChunkSize);
  47.                         }
  48.                         ((FArchive*)V)->Serialize(NewChunk.UncompressedBuffer, NewChunk.UncompressedSize);
  49.                     }
  50.                     // Advance src pointer by amount to be compressed.
  51.                     else
  52.                     {
  53.                         NewChunk.UncompressedBuffer = SrcBuffer;
  54.                         SrcBuffer += NewChunk.UncompressedSize;
  55.                     }
  56.  
  57.                     // Update status variables for tracking how much work is left, what to do next.
  58.                     BytesRemainingToKickOff -= NewChunk.UncompressedSize;
  59.                     AsyncChunkIndex[FreeIndex] = CurrentChunkIndex++;
  60.                     NewChunk.Flags = Flags;
  61.                     NumChunksLeftToKickOff--;
  62.  
  63.                     AsyncChunks[FreeIndex].StartBackgroundTask();
  64.                 }
  65.                 // No chunks were available to use, complete some
  66.                 else
  67.                 {
  68.                     bNeedToWaitForAsyncTask = true;
  69.                 }
  70.             }
  71.  
  72.             // Index of oldest chunk, needed as we need to serialize in order.
  73.             int32 OldestAsyncChunkIndex = INDEX_NONE;
  74.             for( int32 i=0; i<MaxConcurrentAsyncChunks; i++ )
  75.             {
  76.                 check(AsyncChunkIndex[i] == 0 || AsyncChunkIndex[i] >= RetireChunkIndex);
  77.                 check(AsyncChunkIndex[i] < RetireChunkIndex + MaxConcurrentAsyncChunks);
  78.                 if (AsyncChunkIndex[i] == RetireChunkIndex)
  79.                 {
  80.                     OldestAsyncChunkIndex = i;
  81.                 }
  82.             }
  83.             check(OldestAsyncChunkIndex != INDEX_NONE);  // the retire chunk better be outstanding
  84.  
  85.  
  86.             bool ChunkReady;
  87.             if (bNeedToWaitForAsyncTask)
  88.             {
  89.                 // This guarantees that the async work has finished, doing it on this thread if it hasn't been started
  90.                 AsyncChunks[OldestAsyncChunkIndex].EnsureCompletion();
  91.                 ChunkReady = true;
  92.             }
  93.             else
  94.             {
  95.                 ChunkReady = AsyncChunks[OldestAsyncChunkIndex].IsDone();
  96.             }
  97.             if (ChunkReady)
  98.             {
  99.                 FAsyncCompressionChunk& DoneChunk = AsyncChunks[OldestAsyncChunkIndex].GetTask();
  100.                 // Serialize the data via archive.
  101.                 Serialize( DoneChunk.CompressedBuffer, DoneChunk.CompressedSize );
  102.  
  103.                 // Update associated chunk.
  104.                 int64 CompressionChunkIndex = RetireChunkIndex++;
  105.                 check(CompressionChunkIndex<TotalChunkCount);
  106.                 CompressionChunks[CompressionChunkIndex].CompressedSize     = DoneChunk.CompressedSize;
  107.                 CompressionChunks[CompressionChunkIndex].UncompressedSize   = DoneChunk.UncompressedSize;
  108.  
  109.                 // Keep track of total compressed size, stored in first chunk.
  110.                 CompressionChunks[0].CompressedSize += DoneChunk.CompressedSize;
  111.  
  112.                 // Clean up chunk. Src and dst buffer are not touched as the contain allocations we keep till the end.
  113.                 AsyncChunkIndex[OldestAsyncChunkIndex] = 0;
  114.                 DoneChunk.CompressedSize    = 0;
  115.                 DoneChunk.UncompressedSize = 0;
  116.  
  117.                 // Finalized one :)
  118.                 NumChunksLeftToFinalize--;
  119.                 bNeedToWaitForAsyncTask = false;
  120.             }
  121.         }
Advertisement
Add Comment
Please, Sign In to add comment