@@ -483,36 +483,118 @@ pl_resource_is_loaded(const char* pcName)
483483 return pl_hm_has_key_str (& gptResourceManager -> tNameHashmap , pcName );
484484}
485485
486+ void
487+ pl_resource_unload (plResourceHandle tHandle )
488+ {
489+ if (!pl_resource_is_valid (tHandle ))
490+ return ;
491+
492+ plResource * ptResource = & gptResourceManager -> sbtResources [tHandle .uIndex ];
493+
494+ if (gptGfx -> is_texture_valid (gptResourceManager -> tDesc .ptDevice , ptResource -> tTexture ))
495+ {
496+ gptGfx -> queue_texture_for_deletion (gptResourceManager -> tDesc .ptDevice , ptResource -> tTexture );
497+ }
498+
499+ // free file data if we retained it
500+ if (ptResource -> tFlags & PL_RESOURCE_LOAD_FLAG_RETAIN_FILE_DATA )
501+ {
502+ if (ptResource -> puFileData )
503+ {
504+ PL_FREE (ptResource -> puFileData );
505+ }
506+ ptResource -> puFileData = NULL ;
507+ ptResource -> szFileDataSize = 0 ;
508+ }
509+
510+ pl_hm_remove_str (& gptResourceManager -> tNameHashmap , ptResource -> acName );
511+ }
512+
486513bool
487- pl_resource_is_resident (plResourceHandle tHandle )
514+ pl_resource_is_resident (plResourceHandle tHandle , plResourceEvictFlags tFlags )
488515{
489516 if (!pl_resource_is_valid (tHandle ))
490517 return false;
491518
492- plTextureHandle tTexture = pl_resource_get_texture_handle (tHandle );
493- return gptGfx -> is_texture_valid (gptResourceManager -> tDesc .ptDevice , tTexture );
519+ if (tFlags & PL_RESOURCE_EVICT_FLAG_DROP_GPU )
520+ {
521+ plTextureHandle tTexture = pl_resource_get_texture_handle (tHandle );
522+ if (!gptGfx -> is_texture_valid (gptResourceManager -> tDesc .ptDevice , tTexture ))
523+ return false;
524+ }
525+
526+ if (tFlags & PL_RESOURCE_EVICT_FLAG_DROP_CACHE )
527+ {
528+ char acFinalFile [512 ] = {0 };
529+ char acFileNameOnly [256 ] = {0 };
530+ pl_str_get_file_name_only (gptResourceManager -> sbtResources [tHandle .uIndex ].acName , acFileNameOnly , 256 );
531+ pl_sprintf (acFinalFile , "/cache/%s.dds" , acFileNameOnly );
532+
533+ // prep texture for GPU if not done already
534+ if (!gptVfs -> does_file_exist (acFinalFile ))
535+ return false;
536+ }
537+
538+ if (tFlags & PL_RESOURCE_EVICT_FLAG_DROP_FILE_DATA )
539+ {
540+ if (gptResourceManager -> sbtResources [tHandle .uIndex ].tFlags & PL_RESOURCE_LOAD_FLAG_RETAIN_FILE_DATA )
541+ {
542+ if (gptResourceManager -> sbtResources [tHandle .uIndex ].szFileDataSize == 0 )
543+ return false;
544+
545+ }
546+ }
547+ return true;
494548}
495549
496550void
497551pl_resource_evict_ex (plResourceHandle tHandle , plResourceEvictFlags tFlags )
498552{
499- if (!pl_resource_is_resident (tHandle ))
553+ if (!pl_resource_is_resident (tHandle , tFlags ))
500554 return ;
501555
502- plTextureHandle tTexture = pl_resource_get_texture_handle (tHandle );
503- gptGfx -> queue_texture_for_deletion (gptResourceManager -> tDesc .ptDevice , tTexture );
556+ if (tFlags & PL_RESOURCE_EVICT_FLAG_DROP_GPU )
557+ {
558+ plTextureHandle tTexture = pl_resource_get_texture_handle (tHandle );
559+ gptGfx -> queue_texture_for_deletion (gptResourceManager -> tDesc .ptDevice , tTexture );
560+ }
561+
562+ if (tFlags & PL_RESOURCE_EVICT_FLAG_DROP_CACHE )
563+ {
564+ char acFinalFile [512 ] = {0 };
565+ char acFileNameOnly [256 ] = {0 };
566+ pl_str_get_file_name_only (gptResourceManager -> sbtResources [tHandle .uIndex ].acName , acFileNameOnly , 256 );
567+ pl_sprintf (acFinalFile , "/cache/%s.dds" , acFileNameOnly );
568+
569+ // prep texture for GPU if not done already
570+ if (gptVfs -> does_file_exist (acFinalFile ))
571+ {
572+ plVfsFileHandle tFileHandle = gptVfs -> register_file (acFinalFile , true);
573+ gptVfs -> delete_file (tFileHandle );
574+ }
575+ }
576+
577+ if (tFlags & PL_RESOURCE_EVICT_FLAG_DROP_FILE_DATA )
578+ {
579+ if (gptResourceManager -> sbtResources [tHandle .uIndex ].tFlags & PL_RESOURCE_LOAD_FLAG_RETAIN_FILE_DATA )
580+ {
581+ // PL_FREE(gptResourceManager->sbtResources[tHandle.uIndex].puFileData);
582+ // gptResourceManager->sbtResources[tHandle.uIndex].puFileData = NULL;
583+ // gptResourceManager->sbtResources[tHandle.uIndex].szFileDataSize = 0;
584+ }
585+ }
504586}
505587
506588void
507589pl_resource_evict (plResourceHandle tHandle )
508590{
509- pl_resource_evict_ex (tHandle , PL_RESOURCE_EVICT_FLAG_DROP_GPU );
591+ pl_resource_evict_ex (tHandle , PL_RESOURCE_EVICT_FLAG_DROP_ALL );
510592}
511593
512594void
513595pl_resource_make_resident (plResourceHandle tHandle )
514596{
515- if (pl_resource_is_resident (tHandle ))
597+ if (pl_resource_is_resident (tHandle , PL_RESOURCE_EVICT_FLAG_DROP_ALL ))
516598 return ;
517599
518600 plResource * ptResource = & gptResourceManager -> sbtResources [tHandle .uIndex ];
@@ -1079,6 +1161,7 @@ pl_load_resource_ext(plApiRegistryI* ptApiRegistry, bool bReload)
10791161 .get_texture = pl_resource_get_texture_handle ,
10801162 .is_valid = pl_resource_is_valid ,
10811163 .is_loaded = pl_resource_is_loaded ,
1164+ .unload = pl_resource_unload ,
10821165 .clear = pl_resource_clear_all ,
10831166 .is_resident = pl_resource_is_resident ,
10841167 .evict = pl_resource_evict ,
0 commit comments