doom3-bfg/neo/sys/Snapshot_Jobs.cpp

476 lines
14 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../idlib/precompiled.h"
#include "Snapshot_Jobs.h"
uint32 SnapObjChecksum( const uint8* data, int length )
{
extern unsigned long CRC32_BlockChecksum( const void * data, int length );
2012-11-26 18:58:24 +00:00
return CRC32_BlockChecksum( data, length );
}
/*
========================
ObjectsSame
========================
*/
ID_INLINE bool ObjectsSame( objJobState_t& newState, objJobState_t& oldState )
{
2012-11-26 18:58:24 +00:00
assert( newState.valid && oldState.valid );
assert( newState.objectNum == oldState.objectNum );
if( newState.size != oldState.size )
{
2012-11-26 18:58:24 +00:00
//assert( newState.data != oldState.data) );
return false; // Can't match if sizes different
}
/*
if ( newState.data == oldState.data ) {
return true; // Definite match
}
*/
if( memcmp( newState.data, oldState.data, newState.size ) == 0 )
{
2012-11-26 18:58:24 +00:00
return true; // Byte match, same
}
return false; // Not the same
}
/*
========================
SnapshotObjectJob
This job processes objects by delta comparing them, and then zrle encoding them to the dest stream
The dest stream is then eventually read by the lzw job, and then lzw compressed into the final delta packet
ready to be sent to peers.
========================
*/
void SnapshotObjectJob( objParms_t* parms )
{
2012-11-26 18:58:24 +00:00
int visIndex = parms->visIndex;
objJobState_t& newState = parms->newState;
objJobState_t& oldState = parms->oldState;
objHeader_t* header = parms->destHeader;
uint8* dataStart = parms->dest;
2012-11-26 18:58:24 +00:00
assert( newState.valid || oldState.valid );
2012-11-26 18:58:24 +00:00
// Setup header
header->flags = 0;
header->size = newState.valid ? newState.size : 0;
header->csize = 0;
header->objID = -1; // Default to ack
header->data = dataStart;
2012-11-26 18:58:24 +00:00
assert( header->size <= MAX_UNSIGNED_TYPE( objectSize_t ) );
2012-11-26 18:58:24 +00:00
// Setup checksum and tag
#ifdef SNAPSHOT_CHECKSUMS
header->checksum = 0;
#endif
2012-11-26 18:58:24 +00:00
idZeroRunLengthCompressor rleCompressor;
2012-11-26 18:58:24 +00:00
bool visChange = false; // visibility changes will be signified with a 0xffff state size
bool visSendState = false; // the state is sent when an entity is no longer stale
// Compute visibility changes
2012-11-26 18:58:24 +00:00
// (we need to do this before writing out object id, because we may not need to write out the id if we early out)
// (when we don't write out the id, we assume this is an "ack" when we deserialize the objects)
if( newState.valid && oldState.valid )
{
2012-11-26 18:58:24 +00:00
// Check visibility
assert( newState.objectNum == oldState.objectNum );
if( visIndex > 0 )
{
2012-11-26 18:58:24 +00:00
bool oldVisible = ( oldState.visMask & ( 1 << visIndex ) ) != 0;
bool newVisible = ( newState.visMask & ( 1 << visIndex ) ) != 0;
// Force visible if we need to either create or destroy this object
newVisible |= ( newState.size == 0 ) != ( oldState.size == 0 );
2012-11-26 18:58:24 +00:00
if( !oldVisible && !newVisible )
{
2012-11-26 18:58:24 +00:00
// object is stale and ack'ed for this client, write nothing (see 'same object' below)
header->flags |= OBJ_SAME;
return;
}
else if( oldVisible && !newVisible )
{
2012-11-26 18:58:24 +00:00
//SNAP_VERBOSE_PRINT( "object %d to client %d goes stale\n", newState->objectNum, visIndex );
visChange = true;
visSendState = false;
}
else if( !oldVisible && newVisible )
{
2012-11-26 18:58:24 +00:00
//SNAP_VERBOSE_PRINT( "object %d to client %d no longer stale\n", newState->objectNum, visIndex );
visChange = true;
visSendState = true;
}
2012-11-26 18:58:24 +00:00
}
// Same object, write a delta (never early out during vis changes)
if( !visChange && ObjectsSame( newState, oldState ) )
{
2012-11-26 18:58:24 +00:00
// same state, write nothing
header->flags |= OBJ_SAME;
return;
}
}
2012-11-26 18:58:24 +00:00
// Get the id of the object we are writing out
int32 objectNum = ( newState.valid ) ? newState.objectNum : oldState.objectNum;
2012-11-26 18:58:24 +00:00
// Write out object id
header->objID = objectNum;
if( !newState.valid )
{
2012-11-26 18:58:24 +00:00
// Deleted, write 0 size
assert( oldState.valid );
header->flags |= OBJ_DELETED;
}
else if( !oldState.valid )
{
2012-11-26 18:58:24 +00:00
// New object, write out full state
assert( newState.valid );
// delta against an empty snap
rleCompressor.Start( dataStart, NULL, OBJ_DEST_SIZE_ALIGN16( newState.size ) );
rleCompressor.WriteBytes( newState.data, newState.size );
header->csize = rleCompressor.End();
header->flags |= OBJ_NEW;
if( header->csize == -1 )
{
2012-11-26 18:58:24 +00:00
// Not enough space, don't compress, have lzw job do zrle compression instead
memcpy( dataStart, newState.data, newState.size );
}
}
else
{
2012-11-26 18:58:24 +00:00
// Compare to same obj id in different snapshot
assert( newState.objectNum == oldState.objectNum );
2012-11-26 18:58:24 +00:00
header->flags |= OBJ_DIFFERENT;
if( visChange )
{
2012-11-26 18:58:24 +00:00
header->flags |= visSendState ? OBJ_VIS_NOT_STALE : OBJ_VIS_STALE;
}
if( !visChange || visSendState )
{
2012-11-26 18:58:24 +00:00
int compareSize = Min( newState.size, oldState.size );
rleCompressor.Start( dataStart, NULL, OBJ_DEST_SIZE_ALIGN16( newState.size ) );
for( int b = 0; b < compareSize; b++ )
{
2012-11-26 18:58:24 +00:00
byte delta = newState.data[b] - oldState.data[b];
rleCompressor.WriteByte( ( 0xFF + 1 + delta ) & 0xFF );
}
// Get leftover
int leftOver = newState.size - compareSize;
if( leftOver > 0 )
{
2012-11-26 18:58:24 +00:00
rleCompressor.WriteBytes( newState.data + compareSize, leftOver );
}
header->csize = rleCompressor.End();
if( header->csize == -1 )
{
2012-11-26 18:58:24 +00:00
// Not enough space, don't compress, have lzw job do zrle compression instead
for( int b = 0; b < compareSize; b++ )
{
2012-11-26 18:58:24 +00:00
*dataStart++ = ( ( 0xFF + 1 + ( newState.data[b] - oldState.data[b] ) ) & 0xFF );
}
// Get leftover
int leftOver = newState.size - compareSize;
if( leftOver > 0 )
{
2012-11-26 18:58:24 +00:00
memcpy( dataStart, newState.data + compareSize, leftOver );
}
}
}
}
2012-11-26 18:58:24 +00:00
assert( header->csize <= OBJ_DEST_SIZE_ALIGN16( header->size ) );
2012-11-26 18:58:24 +00:00
#ifdef SNAPSHOT_CHECKSUMS
if( newState.valid )
{
2012-11-26 18:58:24 +00:00
assert( newState.size );
header->checksum = SnapObjChecksum( newState.data, newState.size );
}
#endif
}
/*
========================
FinishLZWStream
========================
*/
static void FinishLZWStream( lzwParm_t* parm, idLZWCompressor* lzwCompressor )
{
if( lzwCompressor->IsOverflowed() )
{
2012-11-26 18:58:24 +00:00
lzwCompressor->Restore();
}
lzwDelta_t& pendingDelta = parm->ioData->lzwDeltas[parm->ioData->numlzwDeltas];
if( lzwCompressor->End() == -1 )
{
2012-11-26 18:58:24 +00:00
// If we couldn't end the stream, notify the main thread
pendingDelta.offset = -1;
pendingDelta.size = -1;
pendingDelta.snapSequence = -1;
2012-11-26 18:58:24 +00:00
parm->ioData->numlzwDeltas++;
return;
}
2012-11-26 18:58:24 +00:00
int size = lzwCompressor->Length();
pendingDelta.offset = parm->ioData->lzwBytes; // Remember offset into buffer
pendingDelta.size = size; // Remember size
pendingDelta.snapSequence = parm->ioData->snapSequence; // Remember which snap sequence this delta belongs to
parm->ioData->lzwBytes += size;
parm->ioData->numlzwDeltas++;
}
/*
========================
NewLZWStream
========================
*/
static void NewLZWStream( lzwParm_t* parm, idLZWCompressor* lzwCompressor )
{
2012-11-26 18:58:24 +00:00
// Reset compressor
int maxSize = parm->ioData->maxlzwMem - parm->ioData->lzwBytes;
lzwCompressor->Start( &parm->ioData->lzwMem[parm->ioData->lzwBytes], maxSize );
parm->ioData->lastObjId = 0;
2012-11-26 18:58:24 +00:00
parm->ioData->snapSequence++;
2012-11-26 18:58:24 +00:00
lzwCompressor->WriteAgnostic( parm->ioData->snapSequence );
lzwCompressor->WriteAgnostic( parm->baseSequence );
lzwCompressor->WriteAgnostic( parm->curTime );
}
/*
========================
ContinueLZWStream
========================
*/
static void ContinueLZWStream( lzwParm_t* parm, idLZWCompressor* lzwCompressor )
{
2012-11-26 18:58:24 +00:00
// Continue compressor where we left off
int maxSize = parm->ioData->maxlzwMem - parm->ioData->lzwBytes;
lzwCompressor->Start( &parm->ioData->lzwMem[parm->ioData->lzwBytes], maxSize, true );
}
/*
========================
LZWJobInternal
This job takes a stream of objects, which should already be zrle compressed, and then lzw compresses them
and builds a final delta packet ready to be sent to peers.
========================
*/
void LZWJobInternal( lzwParm_t* parm, unsigned int dmaTag )
{
2012-11-26 18:58:24 +00:00
assert( parm->numObjects > 0 );
2012-11-26 18:58:24 +00:00
#ifndef ALLOW_MULTIPLE_DELTAS
if( parm->ioData->numlzwDeltas > 0 )
{
2012-11-26 18:58:24 +00:00
// Currently, we don't use fragmented deltas.
// We only send the first one and rely on a full snap being sent to get the whole snap across
assert( parm->ioData->numlzwDeltas == 1 );
assert( !parm->ioData->fullSnap );
return;
2012-11-26 18:58:24 +00:00
}
#endif
2012-11-26 18:58:24 +00:00
assert( parm->ioData->lzwBytes < parm->ioData->maxlzwMem );
2012-11-26 18:58:24 +00:00
dmaTag = dmaTag;
2012-11-26 18:58:24 +00:00
ALIGN16( idLZWCompressor lzwCompressor( parm->ioData->lzwData ) );
if( parm->fragmented )
{
2012-11-26 18:58:24 +00:00
// This packet was partially written out, we need to continue writing, using previous lzw dictionary values
ContinueLZWStream( parm, &lzwCompressor );
}
else
{
2012-11-26 18:58:24 +00:00
// We can start a new lzw dictionary
NewLZWStream( parm, &lzwCompressor );
}
2012-11-26 18:58:24 +00:00
int numChangedObjProcessed = 0;
for( int i = 0; i < parm->numObjects; i++ )
{
// This will eventually be gracefully caught in SnapshotProcessor.cpp.
2012-11-26 18:58:24 +00:00
// It's nice to know right when it happens though, so you can inspect the situation.
assert( !lzwCompressor.IsOverflowed() || numChangedObjProcessed > 1 );
2012-11-26 18:58:24 +00:00
// First, see if we need to finish the current lzw stream
if( lzwCompressor.IsOverflowed() || lzwCompressor.Length() >= parm->ioData->optimalLength )
{
2012-11-26 18:58:24 +00:00
FinishLZWStream( parm, &lzwCompressor );
// indicate how much needs to be DMA'ed back out
parm->ioData->lzwDmaOut = parm->ioData->lzwBytes;
#ifdef ALLOW_MULTIPLE_DELTAS
NewLZWStream( parm, &lzwCompressor );
#else
// Currently, we don't use fragmented deltas.
// We only send the first one and rely on a full snap being sent to get the whole snap across
assert( !parm->ioData->fullSnap );
assert( parm->ioData->numlzwDeltas == 1 );
return;
#endif
}
if( numChangedObjProcessed > 0 )
{
2012-11-26 18:58:24 +00:00
// We should be at a good spot in the stream if we've written at least one obj without overflowing, so save it
lzwCompressor.Save();
}
2012-11-26 18:58:24 +00:00
// Get header
objHeader_t* header = &parm->headers[i];
if( header->objID == -1 )
{
2012-11-26 18:58:24 +00:00
assert( header->flags & OBJ_SAME );
continue; // Don't send object (which means ack)
}
2012-11-26 18:58:24 +00:00
numChangedObjProcessed++;
2012-11-26 18:58:24 +00:00
// Write obj id as delta into stream
lzwCompressor.WriteAgnostic<uint16>( ( uint16 )( header->objID - parm->ioData->lastObjId ) );
parm->ioData->lastObjId = ( uint16 )header->objID;
2012-11-26 18:58:24 +00:00
// Check special stale/notstale flags
if( header->flags & ( OBJ_VIS_STALE | OBJ_VIS_NOT_STALE ) )
{
2012-11-26 18:58:24 +00:00
// Write stale/notstale flag
objectSize_t value = ( header->flags & OBJ_VIS_STALE ) ? SIZE_STALE : SIZE_NOT_STALE;
lzwCompressor.WriteAgnostic<objectSize_t>( value );
}
if( header->flags & OBJ_VIS_STALE )
{
2012-11-26 18:58:24 +00:00
continue; // Don't write out data for stale objects
}
if( header->flags & OBJ_DELETED )
{
2012-11-26 18:58:24 +00:00
// Object was deleted
lzwCompressor.WriteAgnostic<objectSize_t>( 0 );
continue;
}
2012-11-26 18:58:24 +00:00
// Write size
lzwCompressor.WriteAgnostic<objectSize_t>( ( objectSize_t )header->size );
2012-11-26 18:58:24 +00:00
// Get compressed data area
uint8* compressedData = header->data;
if( header->csize == -1 )
{
2012-11-26 18:58:24 +00:00
// Wasn't zrle compressed, zrle now while lzw'ing
idZeroRunLengthCompressor rleCompressor;
rleCompressor.Start( NULL, &lzwCompressor, 0xFFFF );
rleCompressor.WriteBytes( compressedData, header->size );
rleCompressor.End();
}
else
{
2012-11-26 18:58:24 +00:00
// Write out zero-rle compressed data
lzwCompressor.Write( compressedData, header->csize );
}
2012-11-26 18:58:24 +00:00
#ifdef SNAPSHOT_CHECKSUMS
// Write checksum
lzwCompressor.WriteAgnostic( header->checksum );
#endif
// This will eventually be gracefully caught in SnapshotProcessor.cpp.
2012-11-26 18:58:24 +00:00
// It's nice to know right when it happens though, so you can inspect the situation.
assert( !lzwCompressor.IsOverflowed() || numChangedObjProcessed > 1 );
2012-11-26 18:58:24 +00:00
}
if( !parm->saveDictionary )
{
2012-11-26 18:58:24 +00:00
// Write out terminator
uint16 objectDelta = 0xFFFF - parm->ioData->lastObjId;
lzwCompressor.WriteAgnostic( objectDelta );
// Last stream
FinishLZWStream( parm, &lzwCompressor );
2012-11-26 18:58:24 +00:00
// indicate how much needs to be DMA'ed back out
parm->ioData->lzwDmaOut = parm->ioData->lzwBytes;
2012-11-26 18:58:24 +00:00
parm->ioData->fullSnap = true; // We sent a full snap
}
else
{
2012-11-26 18:58:24 +00:00
// the compressor did some work, wrote data to lzwMem, but since we didn't call FinishLZWStream to end the compression,
// we need to figure how much needs to be DMA'ed back out
assert( parm->ioData->lzwBytes == 0 ); // I don't think we ever hit this with lzwBytes != 0, but adding it just in case
parm->ioData->lzwDmaOut = parm->ioData->lzwBytes + lzwCompressor.Length();
}
2012-11-26 18:58:24 +00:00
assert( parm->ioData->lzwBytes < parm->ioData->maxlzwMem );
}
/*
========================
LZWJob
========================
*/
void LZWJob( lzwParm_t* parm )
{
2012-11-26 18:58:24 +00:00
LZWJobInternal( parm, 0 );
}