Skip to content

Commit f0ddcf8

Browse files
committed
Improvements for automatic TXD resizing
1 parent bd6233b commit f0ddcf8

18 files changed

+247
-90
lines changed

MTA10/core/CGraphics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,14 +2012,14 @@ bool CGraphics::CopyDataToSurface( IDirect3DSurface9* pSurface, const uchar* pDa
20122012
uint uiLineWidthBytes = SurfDesc.Width * CRenderItemManager::GetBitsPerPixel( SurfDesc.Format ) / 8;
20132013

20142014
// Check pitch is not too small (i.e from a mipmap level)
2015+
uiDataPitch /= CRenderItemManager::GetPitchDivisor( SurfDesc.Format );
20152016
if ( uiDataPitch < uiLineWidthBytes )
20162017
return false;
20172018

20182019
D3DLOCKED_RECT LockedRect;
20192020
if ( FAILED( pSurface->LockRect( &LockedRect, NULL, D3DLOCK_NOSYSLOCK ) ) )
20202021
return false;
20212022

2022-
uiDataPitch /= CRenderItemManager::GetPitchDivisor( SurfDesc.Format );
20232023
uint uiSurfPitch = LockedRect.Pitch / CRenderItemManager::GetPitchDivisor( SurfDesc.Format );
20242024
BYTE* pSurfBits = (BYTE*)LockedRect.pBits;
20252025

@@ -2058,7 +2058,7 @@ bool CGraphics::CopyDataFromSurface( IDirect3DSurface9* pSurface, CBuffer& outBu
20582058
pSurface->GetDesc( &SurfDesc );
20592059

20602060
D3DLOCKED_RECT LockedRect;
2061-
if ( FAILED( pSurface->LockRect( &LockedRect, NULL, D3DLOCK_NOSYSLOCK ) ) )
2061+
if ( FAILED( pSurface->LockRect( &LockedRect, NULL, D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY ) ) )
20622062
return false;
20632063

20642064
uint uiSurfPitch = LockedRect.Pitch / CRenderItemManager::GetPitchDivisor( SurfDesc.Format );

MTA10/game_sa/CRenderWareSA.TextureReplacing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ CModelTexturesInfo* CRenderWareSA::GetModelTexturesInfo ( ushort usModelId )
8080
// Load textures from a TXD file
8181
//
8282
////////////////////////////////////////////////////////////////
83-
bool CRenderWareSA::ModelInfoTXDLoadTextures ( SReplacementTextures* pReplacementTextures, const CBuffer& fileData, bool bFilteringEnabled )
83+
bool CRenderWareSA::ModelInfoTXDLoadTextures ( SReplacementTextures* pReplacementTextures, const SString& strFilename, const CBuffer& fileData, bool bFilteringEnabled )
8484
{
8585
// Are we already loaded?
8686
if ( !pReplacementTextures->textures.empty () )
8787
return false;
8888

8989
// Try to load it
90-
RwTexDictionary* pTxd = ReadTXD ( fileData );
90+
RwTexDictionary* pTxd = ReadTXD ( strFilename, fileData );
9191
if ( pTxd )
9292
{
9393
// Get the list of textures into our own list

MTA10/game_sa/CRenderWareSA.TxdRightSizing.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@
2626
// Returns true if shrunk file was written
2727
//
2828
/////////////////////////////////////////////////////////////////////////////
29-
bool CRenderWareSA::RightSizeTxd( const CBuffer& inTxd, const SString& strOutTxdFilename, uint uiSizeLimit )
29+
bool CRenderWareSA::RightSizeTxd( const SString& strInTxdFilename, const SString& strOutTxdFilename, uint uiSizeLimit )
3030
{
3131
//
3232
// Read txd from memory
3333
//
34-
RwBuffer buffer;
35-
buffer.ptr = (void*)inTxd.GetData();
36-
buffer.size = inTxd.GetSize();
37-
RwStream* pStream = RwStreamOpen( STREAM_TYPE_BUFFER, STREAM_MODE_READ, &buffer );
34+
RwStream* pStream = RwStreamOpen( STREAM_TYPE_FILENAME, STREAM_MODE_READ, *strInTxdFilename );
3835
if ( pStream == NULL )
3936
return false;
4037

@@ -69,6 +66,12 @@ bool CRenderWareSA::RightSizeTxd( const CBuffer& inTxd, const SString& strOutTxd
6966
RwTexDictionaryAddTexture( pTxd, pNewRwTexture );
7067
bChanged = true;
7168
}
69+
else
70+
{
71+
// Keep texture (Reinsert to preserve order for easier debugging)
72+
RwTexDictionaryRemoveTexture( pTxd, pTexture );
73+
RwTexDictionaryAddTexture( pTxd, pTexture );
74+
}
7275
}
7376

7477

@@ -115,15 +118,13 @@ RwTexture* CRenderWareSA::RightSizeTexture( RwTexture* pTexture, uint uiSizeLimi
115118
uint uiWidth = pRaster->width;
116119
uint uiHeight = pRaster->height;
117120
D3DFORMAT d3dFormat = pD3DRaster->format;
118-
void* pBits = pD3DRaster->lockedRect.pBits;
119-
int iPitch = pD3DRaster->lockedRect.Pitch;
120121
bool bHasAlpha = pD3DRaster->alpha != 0;
121122
bool bIsCubeTexture = ( pD3DRaster->cubeTextureFlags & 0x01 ) != 0;
122-
bool bHasMipMaps = pRaster->depth != 1;
123+
bool bHasMipMaps = ( pRaster->numLevels > 1 );
123124
bool bIsCompressed = ( pD3DRaster->textureFlags & 0x10 ) != 0;
124125

125126
// Check we can do this
126-
if ( bHasMipMaps || bIsCubeTexture || !bIsCompressed )
127+
if ( bIsCubeTexture || !bIsCompressed )
127128
return NULL;
128129

129130
// Only process DXT formats
@@ -136,9 +137,21 @@ RwTexture* CRenderWareSA::RightSizeTexture( RwTexture* pTexture, uint uiSizeLimi
136137
if ( uiReqWidth == uiWidth && uiReqHeight == uiHeight )
137138
return NULL;
138139

140+
// Lock mip level 0 if required
141+
D3DLOCKED_RECT lockedRect = pD3DRaster->lockedRect;
142+
bool bNeedOwnLock = ( pD3DRaster->lockedLevel != 0 ) || !pD3DRaster->lockedSurface;
143+
if ( bNeedOwnLock )
144+
if ( FAILED( pD3DRaster->texture->LockRect( 0, &lockedRect, NULL, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY ) ) )
145+
return NULL;
146+
139147
// Try resize
140148
CBuffer newPixelBuffer;
141-
if ( !g_pCore->GetGraphics()->ResizeTextureData( pBits, iPitch, uiWidth, uiHeight, d3dFormat, uiReqWidth, uiReqHeight, newPixelBuffer ) )
149+
bool bDidResize = g_pCore->GetGraphics()->ResizeTextureData( lockedRect.pBits, lockedRect.Pitch, uiWidth, uiHeight, d3dFormat, uiReqWidth, uiReqHeight, newPixelBuffer );
150+
151+
if ( bNeedOwnLock )
152+
pD3DRaster->texture->UnlockRect( 0 );
153+
154+
if ( !bDidResize )
142155
return NULL;
143156

144157
// Make new RwTexture from pixels
@@ -150,7 +163,7 @@ RwTexture* CRenderWareSA::RightSizeTexture( RwTexture* pTexture, uint uiSizeLimi
150163
header.TextureFormat.vAddressing = ( pTexture->flags & 0xf000 ) >> 12;
151164
memcpy( header.TextureFormat.name, pTexture->name, 32 );
152165
memcpy( header.TextureFormat.maskName, pTexture->mask, 32 );
153-
header.RasterFormat.rasterFormat = pRaster->format << 8; // ( dxt1 = 0x00000100 or 0x00000200 / dxt3 = 0x00000300 ) | 0x00008000 mipmaps?
166+
header.RasterFormat.rasterFormat = ( pRaster->format & 0x0f ) << 8; // ( dxt1 = 0x00000100 or 0x00000200 / dxt3 = 0x00000300 ) | 0x00008000 mipmaps?
154167
header.RasterFormat.d3dFormat = pD3DRaster->format;
155168
header.RasterFormat.width = uiReqWidth;
156169
header.RasterFormat.height = uiReqHeight;

MTA10/game_sa/CRenderWareSA.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,19 @@ CRenderWareSA::~CRenderWareSA ( void )
195195

196196

197197
// Reads and parses a TXD file specified by a path (szTXD)
198-
RwTexDictionary * CRenderWareSA::ReadTXD ( const CBuffer& fileData )
198+
RwTexDictionary * CRenderWareSA::ReadTXD ( const SString& strFilename, const CBuffer& fileData )
199199
{
200200
// open the stream
201+
RwStream * streamTexture;
201202
RwBuffer buffer;
202-
buffer.ptr = (void*)fileData.GetData();
203-
buffer.size = fileData.GetSize();
204-
RwStream * streamTexture = RwStreamOpen ( STREAM_TYPE_BUFFER, STREAM_MODE_READ, &buffer );
203+
if ( !fileData.IsEmpty() )
204+
{
205+
buffer.ptr = (void*)fileData.GetData();
206+
buffer.size = fileData.GetSize();
207+
streamTexture = RwStreamOpen ( STREAM_TYPE_BUFFER, STREAM_MODE_READ, &buffer );
208+
}
209+
else
210+
streamTexture = RwStreamOpen ( STREAM_TYPE_FILENAME, STREAM_MODE_READ, *strFilename );
205211

206212
// check for errors
207213
if ( streamTexture == NULL )
@@ -229,7 +235,7 @@ RwTexDictionary * CRenderWareSA::ReadTXD ( const CBuffer& fileData )
229235
// Reads and parses a DFF file specified by a path (szDFF) into a CModelInfo identified by the object id (usModelID)
230236
// bLoadEmbeddedCollisions should be true for vehicles
231237
// Any custom TXD should be imported before this call
232-
RpClump * CRenderWareSA::ReadDFF ( const CBuffer& fileData, unsigned short usModelID, bool bLoadEmbeddedCollisions )
238+
RpClump * CRenderWareSA::ReadDFF ( const SString& strFilename, const CBuffer& fileData, unsigned short usModelID, bool bLoadEmbeddedCollisions )
233239
{
234240
// Set correct TXD as materials are processed at the same time
235241
if ( usModelID != 0 )
@@ -239,10 +245,16 @@ RpClump * CRenderWareSA::ReadDFF ( const CBuffer& fileData, unsigned short usMod
239245
}
240246

241247
// open the stream
248+
RwStream * streamModel;
242249
RwBuffer buffer;
243-
buffer.ptr = (void*)fileData.GetData();
244-
buffer.size = fileData.GetSize();
245-
RwStream * streamModel = RwStreamOpen ( STREAM_TYPE_BUFFER, STREAM_MODE_READ, &buffer );
250+
if ( !fileData.IsEmpty() )
251+
{
252+
buffer.ptr = (void*)fileData.GetData();
253+
buffer.size = fileData.GetSize();
254+
streamModel = RwStreamOpen ( STREAM_TYPE_BUFFER, STREAM_MODE_READ, &buffer );
255+
}
256+
else
257+
streamModel = RwStreamOpen ( STREAM_TYPE_FILENAME, STREAM_MODE_READ, *strFilename );
246258

247259
// get the modelinfo array
248260
DWORD *pPool = ( DWORD* ) ARRAY_ModelInfo;

MTA10/game_sa/CRenderWareSA.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ class CRenderWareSA : public CRenderWare
3333
CRenderWareSA ( enum eGameVersion version );
3434
~CRenderWareSA ( void );
3535
void Initialize ( void );
36-
bool ModelInfoTXDLoadTextures ( SReplacementTextures* pReplacementTextures, const CBuffer& fileData, bool bFilteringEnabled );
36+
bool ModelInfoTXDLoadTextures ( SReplacementTextures* pReplacementTextures, const SString& strFilename, const CBuffer& fileData, bool bFilteringEnabled );
3737
bool ModelInfoTXDAddTextures ( SReplacementTextures* pReplacementTextures, ushort usModelId );
3838
void ModelInfoTXDRemoveTextures ( SReplacementTextures* pReplacementTextures );
3939
void ClothesAddReplacementTxd ( char* pFileData, ushort usFileId );
4040
void ClothesRemoveReplacementTxd ( char* pFileData );
4141
bool HasClothesReplacementChanged( void );
4242

4343
// Reads and parses a TXD file specified by a path (szTXD)
44-
RwTexDictionary * ReadTXD ( const CBuffer& fileData );
44+
RwTexDictionary * ReadTXD ( const SString& strFilename, const CBuffer& fileData );
4545

4646
// Reads and parses a DFF file specified by a path (szDFF) into a CModelInfo identified by the object id (usModelID)
47-
RpClump * ReadDFF ( const CBuffer& fileData, unsigned short usModelID, bool bLoadEmbeddedCollisions );
47+
RpClump * ReadDFF ( const SString& strFilename, const CBuffer& fileData, unsigned short usModelID, bool bLoadEmbeddedCollisions );
4848

4949
// Destroys a DFF instance
5050
void DestroyDFF ( RpClump * pClump );
@@ -105,7 +105,7 @@ class CRenderWareSA : public CRenderWare
105105
void AppendSubtractiveMatch ( CSHADERDUMMY* pShaderData, CClientEntityBase* pClientEntity, const char* strTextureNameMatch );
106106
void RemoveClientEntityRefs ( CClientEntityBase* pClientEntity );
107107
void RemoveShaderRefs ( CSHADERDUMMY* pShaderItem );
108-
bool RightSizeTxd ( const CBuffer& inTxd, const SString& strOutTxdFilename, uint uiSizeLimit );
108+
bool RightSizeTxd ( const SString& strInTxdFilename, const SString& strOutTxdFilename, uint uiSizeLimit );
109109

110110
// CRenderWareSA methods
111111
RwTexture* RightSizeTexture ( RwTexture* pTexture, uint uiSizeLimit );

MTA10/mods/deathmatch/logic/CResourceManager.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,18 @@ void CResourceManager::ValidateResourceFile( const SString& strInFilename, const
246246
}
247247
else
248248
{
249-
CChecksum checksum = CChecksum::GenerateChecksumFromBuffer( fileData.GetData(), fileData.GetSize() );
249+
CChecksum checksum;
250+
if ( !fileData.IsEmpty() )
251+
checksum = CChecksum::GenerateChecksumFromBuffer( fileData.GetData(), fileData.GetSize() );
252+
else
253+
checksum = CChecksum::GenerateChecksumFromFile( strInFilename );
250254
if ( checksum != pResourceFile->GetServerChecksum() )
251255
{
252256
if ( pResourceFile->IsDownloaded() )
253257
{
254258
char szMd5[33];
255259
CMD5Hasher::ConvertToHex( checksum.md5, szMd5 );
256-
SString strMessage( "Resource file checksum failed: %s [Size:%d MD5:%s] %08x ", *ConformResourcePath( strInFilename ), fileData.GetSize(), szMd5, fileData.GetData() );
260+
SString strMessage( "Resource file checksum failed: %s [Size:%d MD5:%s]", *ConformResourcePath( strInFilename ), (int)FileSize( strInFilename ), szMd5 );
257261
g_pClientGame->TellServerSomethingImportant( 1007, strMessage, false );
258262
g_pCore->GetConsole ()->Print( strMessage );
259263
AddReportLog( 7057, strMessage + g_pNet->GetConnectedServer( true ), 10 );
@@ -263,7 +267,7 @@ void CResourceManager::ValidateResourceFile( const SString& strInFilename, const
263267
{
264268
char szMd5[33];
265269
CMD5Hasher::ConvertToHex( checksum.md5, szMd5 );
266-
SString strMessage( "Attempt to load resource file before it is ready: %s [Size:%d MD5:%s] %08x ", *ConformResourcePath( strInFilename ), fileData.GetSize(), szMd5, fileData.GetData() );
270+
SString strMessage( "Attempt to load resource file before it is ready: %s [Size:%d MD5:%s]", *ConformResourcePath( strInFilename ), (int)FileSize( strInFilename ), szMd5 );
267271
g_pClientGame->TellServerSomethingImportant( 1008, strMessage, false );
268272
g_pCore->GetConsole ()->Print( strMessage );
269273
AddReportLog( 7058, strMessage + g_pNet->GetConnectedServer( true ), 10 );

MTA10/mods/shared_logic/CClientDFF.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,15 @@ RpClump* CClientDFF::GetLoadedClump ( ushort usModelId )
5555
// Attempt loading it
5656
if( !m_bIsRawData ) // We have file
5757
{
58-
CBuffer buffer;
59-
buffer.LoadFromFile( m_strDffFilename );
60-
g_pClientGame->GetResourceManager()->ValidateResourceFile( m_strDffFilename, buffer );
61-
if ( g_pCore->GetNetwork ()->CheckFile ( "dff", m_strDffFilename, buffer ) )
58+
if ( g_pCore->GetNetwork ()->CheckFile ( "dff", m_strDffFilename ) )
6259
{
63-
info.pClump = g_pGame->GetRenderWare ()->ReadDFF ( buffer, usModelId, CClientVehicleManager::IsValidModel ( usModelId ) );
60+
g_pClientGame->GetResourceManager()->ValidateResourceFile( m_strDffFilename, CBuffer() );
61+
info.pClump = g_pGame->GetRenderWare ()->ReadDFF ( m_strDffFilename, CBuffer(), usModelId, CClientVehicleManager::IsValidModel ( usModelId ) );
6462
}
6563
}
6664
else //We have raw data
6765
{
68-
info.pClump = g_pGame->GetRenderWare ()->ReadDFF ( m_RawDataBuffer, usModelId, CClientVehicleManager::IsValidModel ( usModelId ) );
66+
info.pClump = g_pGame->GetRenderWare ()->ReadDFF ( NULL, m_RawDataBuffer, usModelId, CClientVehicleManager::IsValidModel ( usModelId ) );
6967

7068
// Remove raw data from memory (can only do one replace when using raw data)
7169
m_RawDataBuffer.Clear ();

0 commit comments

Comments
 (0)