platform: p5 allocator fix; ref_gu: using vram for palettes

This commit is contained in:
Crow-bar
2022-09-21 15:31:26 +03:00
parent b567531cdb
commit b517019750
8 changed files with 49 additions and 25 deletions

View File

@@ -18,6 +18,7 @@ GNU General Public License for more details.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 )
{

View File

@@ -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 );

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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 ));

View File

@@ -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 ));
}

View File

@@ -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