From b517019750b91ab6dbf959baa39ffc5be9153ee7 Mon Sep 17 00:00:00 2001 From: Crow-bar Date: Wed, 21 Sep 2022 15:31:26 +0300 Subject: [PATCH] platform: p5 allocator fix; ref_gu: using vram for palettes --- engine/platform/psp/p5ram_psp.c | 12 ++++++---- engine/platform/psp/p5ram_psp.h | 2 +- engine/platform/psp/sys_psp.c | 2 +- engine/ref_api.h | 2 +- engine/server/sv_save.c | 8 +++---- ref_gu/gu_image.c | 42 +++++++++++++++++++++++---------- ref_gu/gu_vram.c | 5 ++++ ref_gu/gu_vram.h | 1 + 8 files changed, 49 insertions(+), 25 deletions(-) diff --git a/engine/platform/psp/p5ram_psp.c b/engine/platform/psp/p5ram_psp.c index f2a8cce7..1d58a0aa 100644 --- a/engine/platform/psp/p5ram_psp.c +++ b/engine/platform/psp/p5ram_psp.c @@ -18,6 +18,7 @@ GNU General Public License for more details. #include #include +#include #include "p5ram_psp.h" #define P5RAM_ALIGN( x, a ) ((( x ) + (( typeof( x ))( a ) - 1 )) & ~( typeof( x )( a ) - 1 )) @@ -27,7 +28,7 @@ GNU General Public License for more details. #define P5RAM_FLAG_INIT 0x01 #define P5RAM_FLAG_SUSPENDED 0x02 -typedef struct p5ram_info_s +typedef struct __attribute__(( aligned( 64 ))) p5ram_info_s { SceUID uid; size_t size; @@ -53,7 +54,7 @@ int P5Ram_Init( void ) return result; } -void *P5Ram_Alloc( size_t size ) +void *P5Ram_Alloc( size_t size, int clear ) { SceUID uid; void *ptr; @@ -61,17 +62,18 @@ void *P5Ram_Alloc( size_t size ) if(!( p5ram_flags & P5RAM_FLAG_INIT )) return NULL; - uid = sceKernelAllocPartitionMemory( 5, "USER P5", PSP_SMEM_Low, size + /*sizeof( p5ram_info_t )*/64, NULL ); + uid = sceKernelAllocPartitionMemory( 5, "USER P5", PSP_SMEM_Low, size + sizeof( p5ram_info_t ), NULL ); if( uid < 0 ) return NULL; ptr = sceKernelGetBlockHeadAddr( uid ); (( p5ram_info_t* )ptr )->uid = uid; (( p5ram_info_t* )ptr )->size = size; (( p5ram_info_t* )ptr )->next = p5ram_poolchain; + if( clear ) memset(( unsigned char* )ptr + sizeof( p5ram_info_t ), 0, size ); p5ram_poolchain = ptr; - return ptr + /*sizeof( p5ram_info_t )*/64; + return ( void* )(( unsigned char* )ptr + sizeof( p5ram_info_t )); } void P5Ram_Free( void *ptr ) @@ -80,7 +82,7 @@ void P5Ram_Free( void *ptr ) if( !( p5ram_flags & P5RAM_FLAG_INIT ) || ptr == NULL ) return; - info = ( p5ram_info_t* )( ptr - /*sizeof( p5ram_info_t )*/64); + info = ( p5ram_info_t* )((unsigned char*)ptr - sizeof( p5ram_info_t )); if( info == p5ram_poolchain ) { diff --git a/engine/platform/psp/p5ram_psp.h b/engine/platform/psp/p5ram_psp.h index dc424c27..9f73f051 100644 --- a/engine/platform/psp/p5ram_psp.h +++ b/engine/platform/psp/p5ram_psp.h @@ -22,7 +22,7 @@ extern "C" { #endif // __cplusplus int P5Ram_Init( void ); -void *P5Ram_Alloc( size_t size ); +void *P5Ram_Alloc( size_t size, int clear ); void P5Ram_Free( void *ptr ); void P5Ram_FreeAll( void ); void P5Ram_Shutdown( void ); diff --git a/engine/platform/psp/sys_psp.c b/engine/platform/psp/sys_psp.c index bdb0b2d5..5e2cc518 100644 --- a/engine/platform/psp/sys_psp.c +++ b/engine/platform/psp/sys_psp.c @@ -267,7 +267,7 @@ int Platform_UnloadModule( SceUID modid, int *sce_code ) *sce_code = sceKernelUnloadModule( modid ); return ( ( ( *sce_code ) < 0 ) ? -2 : 0 ); } -#include "vfs_psp.h" + void Platform_Init( void ) { SceUID kamID; diff --git a/engine/ref_api.h b/engine/ref_api.h index 0875c77b..6abbf799 100644 --- a/engine/ref_api.h +++ b/engine/ref_api.h @@ -432,7 +432,7 @@ typedef struct ref_api_s void (*pfnDrawTransparentTriangles)( void ); render_interface_t *drawFuncs; #if XASH_PSP - void *(*P5Ram_Alloc)( size_t size ); + void *(*P5Ram_Alloc)( size_t size, int clear ); void (*P5Ram_Free)( void *ptr ); #endif } ref_api_t; diff --git a/engine/server/sv_save.c b/engine/server/sv_save.c index eafe5c96..f6dc900c 100644 --- a/engine/server/sv_save.c +++ b/engine/server/sv_save.c @@ -292,7 +292,7 @@ static void InitEntityTable( SAVERESTOREDATA *pSaveData, int entityCount ) int i; #if XASH_PSP - pSaveData->pTable = P5Ram_Alloc( sizeof( ENTITYTABLE ) * entityCount ); + pSaveData->pTable = P5Ram_Alloc( sizeof( ENTITYTABLE ) * entityCount, 1 ); #else pSaveData->pTable = Mem_Calloc( host.mempool, sizeof( ENTITYTABLE ) * entityCount ); #endif @@ -612,8 +612,8 @@ static SAVERESTOREDATA *SaveInit( int size, int tokenCount ) SAVERESTOREDATA *pSaveData; #if XASH_PSP - pSaveData = P5Ram_Alloc( sizeof( SAVERESTOREDATA ) + size ); - pSaveData->pTokens = (char **)P5Ram_Alloc( tokenCount * sizeof( char* )); + pSaveData = P5Ram_Alloc( sizeof( SAVERESTOREDATA ) + size, 1 ); + pSaveData->pTokens = (char **)P5Ram_Alloc( tokenCount * sizeof( char* ), 1 ); #else pSaveData = Mem_Calloc( host.mempool, sizeof( SAVERESTOREDATA ) + size ); pSaveData->pTokens = (char **)Mem_Calloc( host.mempool, tokenCount * sizeof( char* )); @@ -2268,7 +2268,7 @@ int GAME_EXPORT SV_GetSaveComment( const char *savename, char *comment ) if( tokenSize > 0 ) { #if XASH_PSP - pTokenList = P5Ram_Alloc( tokenCount * sizeof( char* )); + pTokenList = P5Ram_Alloc( tokenCount * sizeof( char* ), 1 ); #else pTokenList = Mem_Calloc( host.mempool, tokenCount * sizeof( char* )); #endif diff --git a/ref_gu/gu_image.c b/ref_gu/gu_image.c index 214405ea..561c389f 100644 --- a/ref_gu/gu_image.c +++ b/ref_gu/gu_image.c @@ -902,9 +902,13 @@ static qboolean GL_UploadTexture( gl_texture_t *tex, rgbdata_t *pic ) { if( !tex->dstPalette ) { - tex->dstPalette = ( byte* )memalign( 16, PALETTE_SIZE ); - if ( !tex->dstPalette ) - gEngfuncs.Host_Error( "GL_UploadTexture: %s out of memory for palette ( %lu )\n", tex->name, PALETTE_SIZE ); + tex->dstPalette = ( byte* )valloc( PALETTE_SIZE ); + if( !tex->dstPalette ) + { + tex->dstPalette = ( byte* )memalign( 16, PALETTE_SIZE ); + if ( !tex->dstPalette ) + gEngfuncs.Host_Error( "GL_UploadTexture: %s out of memory for palette ( %lu )\n", tex->name, PALETTE_SIZE ); + } } // Load palette @@ -916,7 +920,7 @@ static qboolean GL_UploadTexture( gl_texture_t *tex, rgbdata_t *pic ) // Volatile memory for temporary buffer if( swizzle ) { - tempBuff = gEngfuncs.P5Ram_Alloc( texSize ); + tempBuff = gEngfuncs.P5Ram_Alloc( texSize, 0 ); if( !tempBuff ) gEngfuncs.Host_Error( "GL_UploadTexture: temporary memory error\n" ); } @@ -944,7 +948,7 @@ static qboolean GL_UploadTexture( gl_texture_t *tex, rgbdata_t *pic ) { if( swizzle ) { - tempBuff = gEngfuncs.P5Ram_Alloc( texSize ); + tempBuff = gEngfuncs.P5Ram_Alloc( texSize, 0 ); if( !tempBuff ) gEngfuncs.Host_Error( "GL_UploadTexture: temporary memory error\n" ); } @@ -970,7 +974,7 @@ static qboolean GL_UploadTexture( gl_texture_t *tex, rgbdata_t *pic ) if(( pic->width != tex->width ) || ( pic->height != tex->height )) offset = tex->width * tex->height * 4; // src size - tempBuff = gEngfuncs.P5Ram_Alloc( swizzle ? ( texSize + offset ) : texSize ); + tempBuff = gEngfuncs.P5Ram_Alloc( swizzle ? ( texSize + offset ) : texSize, 0 ); if( !tempBuff ) gEngfuncs.Host_Error( "GL_UploadTexture: temporary memory error\n" ); if(( pic->width != tex->width ) || ( pic->height != tex->height )) @@ -1040,11 +1044,11 @@ qboolean GL_UpdateTexture( int texnum, int xoff, int yoff, int width, int height return false; } - if( !FBitSet( tex->flags, TF_IMG_UPLOADED ) ) + if( !FBitSet( tex->flags, TF_IMG_UPLOADED )) { tex->size = GL_CalcTextureSize( tex->format, tex->width, tex->height, &tex->bpp ); tex->numMips = 1; - tex->dstPalette = NULL; + tex->dstPalette = ( tex->format == GU_PSM_T8 ) ? gl_palette_332 : NULL; // default tex->dstTexture = ( byte* )valloc( tex->size ); if( !tex->dstTexture ) @@ -1263,7 +1267,11 @@ static void GL_DeleteTexture( gl_texture_t *tex ) gEngfuncs.FS_FreeImage( tex->original ); if( tex->dstPalette && tex->dstPalette != gl_palette_332 ) - free( tex->dstPalette ); + { + if( vchkptr( tex->dstPalette )) + vfree( tex->dstPalette ); + else free( tex->dstPalette ); + } if( tex->dstTexture ) { @@ -1876,9 +1884,13 @@ GL_Create332Palette */ static void GL_Create332Palette( void ) { - gl_palette_332 = ( byte* )memalign( 16, PALETTE_SIZE ); - if ( !gl_palette_332 ) - gEngfuncs.Host_Error( "GL_Create332Palette: out of memory!\n" ); + gl_palette_332 = ( byte* )valloc( PALETTE_SIZE ); + if( !gl_palette_332 ) + { + gl_palette_332 = ( byte* )memalign( 16, PALETTE_SIZE ); + if ( !gl_palette_332 ) + gEngfuncs.Host_Error( "GL_Create332Palette: out of memory!\n" ); + } for(int i = 0; i < 256; i++) { @@ -2038,7 +2050,11 @@ void R_ShutdownImages( void ) GL_DeleteTexture( tex ); if( gl_palette_332 ) - free( gl_palette_332 ); + { + if( vchkptr( gl_palette_332 )) + vfree( gl_palette_332 ); + else free( gl_palette_332 ); + } memset( tr.lightmapTextures, 0, sizeof( tr.lightmapTextures )); memset( gl_texturesHashTable, 0, sizeof( gl_texturesHashTable )); diff --git a/ref_gu/gu_vram.c b/ref_gu/gu_vram.c index d589eef4..2f54de5d 100644 --- a/ref_gu/gu_vram.c +++ b/ref_gu/gu_vram.c @@ -292,3 +292,8 @@ size_t vlargestblock( void ) if( __largest_update ) __find_largest_block(); return __largest_block * __BLOCK_SIZE; } + +int vchkptr( void *ptr ) +{ + return ((( unsigned int )ptr >= __mem_start ) && (( unsigned int )ptr < __mem_start + __mem_size )); +} diff --git a/ref_gu/gu_vram.h b/ref_gu/gu_vram.h index 0602ab56..3c75f001 100644 --- a/ref_gu/gu_vram.h +++ b/ref_gu/gu_vram.h @@ -26,6 +26,7 @@ void* valloc( size_t size ); void vfree( void* ptr ); size_t vmemavail(); size_t vlargestblock(); +int vchkptr( void *ptr ); #ifdef _DEBUG