@@ -376,6 +376,12 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
376376 {
377377 case PL_RESOURCE_DATA_TYPE_IMAGE :
378378 {
379+
380+ char * sbtNameConcat = NULL ;
381+ char acFileNameOnly [256 ] = {0 };
382+ pl_str_get_file_name_only (pcName , acFileNameOnly , 256 );
383+ pl_sb_sprintf (sbtNameConcat , "/cache/%s.dds" , acFileNameOnly );
384+
379385 plDevice * ptDevice = gptResourceManager -> tDesc .ptDevice ;
380386
381387 plTexture * ptTexture = NULL ;
@@ -518,11 +524,6 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
518524 // bind memory
519525 gptGfx -> bind_texture_to_memory (ptDevice , tResource .tTexture , & tAllocation );
520526
521- char * sbtNameConcat = NULL ;
522- char acFileNameOnly [256 ] = {0 };
523- pl_str_get_file_name_only (pcName , acFileNameOnly , 256 );
524- pl_sb_sprintf (sbtNameConcat , "/cache/%s.dds" , acFileNameOnly );
525-
526527 if (gptVfs -> does_file_exist (sbtNameConcat ))
527528 {
528529 size_t szShaderSize = gptVfs -> get_file_size_str (sbtNameConcat );
@@ -561,9 +562,12 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
561562 {
562563 memcpy ((uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szStagingOffset ], puVertexShaderCode , tDDSReadInfo .atMipInfo [i ].uSize );
563564
565+ int iCurrentWidth = (int )tImageInfo .iWidth / ((1 << (int )i ));
566+ int iCurrentHeight = (int )tImageInfo .iHeight / ((1 << (int )i ));
567+
564568 const plBufferImageCopy tBufferImageCopy0 = {
565- .uImageWidth = (uint32_t )tImageInfo . iWidth ,
566- .uImageHeight = (uint32_t )tImageInfo . iHeight ,
569+ .uImageWidth = (uint32_t )iCurrentWidth ,
570+ .uImageHeight = (uint32_t )iCurrentHeight ,
567571 .uImageDepth = 1 ,
568572 .uLayerCount = 1 ,
569573 .szBufferOffset = szStagingOffset ,
@@ -599,6 +603,9 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
599603 PL_ASSERT (puRawBytes );
600604 gptImage -> free (puOldRawBytes );
601605 }
606+
607+ // calculate total staging size required
608+ size_t szRequiredStagingSize = 0 ;
602609
603610 plDxtInfo tDxtInfoOriginal = {
604611 .tFlags = PL_DXT_FLAGS_HIGH_QUALITY ,
@@ -607,9 +614,28 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
607614 .uChannels = 4 ,
608615 .puData = puRawBytes
609616 };
610- size_t szRequiredStagingSize = 0 ;
617+
611618 gptDxt -> compress (& tDxtInfoOriginal , NULL , & szRequiredStagingSize );
612619
620+ for (uint32_t uMipLevel = 1 ; uMipLevel < ptTexture -> tDesc .uMips ; uMipLevel ++ )
621+ {
622+
623+ int iCurrentWidth = (int )tImageInfo .iWidth / ((1 << (int )uMipLevel ));
624+ int iCurrentHeight = (int )tImageInfo .iHeight / ((1 << (int )uMipLevel ));
625+
626+ plDxtInfo tDxtInfo = {
627+ .tFlags = PL_DXT_FLAGS_HIGH_QUALITY ,
628+ .uWidth = (uint32_t )iCurrentWidth ,
629+ .uHeight = (uint32_t )iCurrentHeight ,
630+ .uChannels = 4
631+ };
632+
633+ size_t szMipCompressedSize = 0 ;
634+ gptDxt -> compress (& tDxtInfo , NULL , & szMipCompressedSize );
635+ szRequiredStagingSize += szMipCompressedSize ;
636+ }
637+
638+ // size staging buffer
613639 if (!gptGfx -> is_buffer_valid (ptDevice , gptResourceManager -> tStagingBuffer .tStagingBufferHandle ))
614640 pl__resource_create_staging_buffer (szRequiredStagingSize );
615641 else if (gptResourceManager -> tStagingBuffer .szOffset + szRequiredStagingSize >= gptResourceManager -> tStagingBuffer .szSize )
@@ -621,27 +647,10 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
621647
622648 plBuffer * ptStagingBuffer = gptGfx -> get_buffer (ptDevice , gptResourceManager -> tStagingBuffer .tStagingBufferHandle );
623649
624- gptDxt -> compress (& tDxtInfoOriginal , (uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szStagingOffset ], & szRequiredStagingSize );
625-
626-
627- plDdsWriteInfo tDDSWriteInfo = {
628- .uWidth = (uint32_t )tImageInfo .iWidth ,
629- .uHeight = (uint32_t )tImageInfo .iHeight ,
630- .uDepth = 0 ,
631- .uMips = ptTexture -> tDesc .uMips ,
632- .uLayers = 1 ,
633- .tFormat = PL_FORMAT_BC3_UNORM ,
634- .tType = PL_TEXTURE_TYPE_2D
635- };
636-
637- uint8_t * pcBlah = PL_ALLOC (gptDds -> get_header_size () + szRequiredStagingSize );
638- gptDds -> write_info (pcBlah , & tDDSWriteInfo );
639- memcpy (& pcBlah [gptDds -> get_header_size ()], (uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szStagingOffset ], szRequiredStagingSize );
640- plVfsFileHandle tHandle = gptVfs -> open_file (sbtNameConcat , PL_VFS_FILE_MODE_WRITE );
641- gptVfs -> write_file (tHandle , (uint8_t * )pcBlah , gptDds -> get_header_size () + szRequiredStagingSize );
642- gptVfs -> close_file (tHandle );
643-
644- PL_FREE (pcBlah );
650+ size_t szMipCompressedSize = 0 ;
651+ size_t szOriginalStagingOffset = szStagingOffset ;
652+ gptDxt -> compress (& tDxtInfoOriginal , (uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szOriginalStagingOffset ], & szMipCompressedSize );
653+ szStagingOffset += szMipCompressedSize ;
645654
646655 size_t szCurrentSize = szRequiredStagingSize ;
647656 size_t szMaxBufferSize = tImageInfo .iWidth * tImageInfo .iHeight * 4 ;
@@ -655,7 +664,7 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
655664 .uImageHeight = (uint32_t )tImageInfo .iHeight ,
656665 .uImageDepth = 1 ,
657666 .uLayerCount = 1 ,
658- .szBufferOffset = szStagingOffset ,
667+ .szBufferOffset = szOriginalStagingOffset ,
659668 .uMipLevel = 0
660669 };
661670
@@ -667,7 +676,7 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
667676
668677 gptGfx -> copy_buffer_to_texture (ptBlitEncoder , gptResourceManager -> tStagingBuffer .tStagingBufferHandle , tResource .tTexture , 1 , & tBufferImageCopy0 );
669678
670- szStagingOffset += szCurrentSize ;
679+ // szStagingOffset += szCurrentSize;
671680
672681 for (uint32_t uMipLevel = 1 ; uMipLevel < ptTexture -> tDesc .uMips ; uMipLevel ++ )
673682 {
@@ -709,20 +718,18 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
709718 }
710719
711720 // compression
712- {
713- plDxtInfo tDxtInfo = {
714- .tFlags = PL_DXT_FLAGS_HIGH_QUALITY ,
715- .uWidth = (uint32_t )iCurrentWidth ,
716- .uHeight = (uint32_t )iCurrentHeight ,
717- .uChannels = 4 ,
718- .puData = puDstBuffer
719- };
720- szRequiredStagingSize = 0 ;
721- gptDxt -> compress (& tDxtInfo , NULL , & szRequiredStagingSize );
722- gptDxt -> compress (& tDxtInfo , (uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szStagingOffset ], & szRequiredStagingSize );
723- szCurrentSize = szRequiredStagingSize ;
724- }
725721
722+ plDxtInfo tDxtInfo = {
723+ .tFlags = PL_DXT_FLAGS_HIGH_QUALITY ,
724+ .uWidth = (uint32_t )iCurrentWidth ,
725+ .uHeight = (uint32_t )iCurrentHeight ,
726+ .uChannels = 4 ,
727+ .puData = puDstBuffer
728+ };
729+ szMipCompressedSize = 0 ;
730+ gptDxt -> compress (& tDxtInfo , NULL , & szMipCompressedSize );
731+ gptDxt -> compress (& tDxtInfo , (uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szStagingOffset ], & szMipCompressedSize );
732+
726733 const plBufferImageCopy tBufferImageCopy = {
727734 .uImageWidth = (uint32_t )iCurrentWidth ,
728735 .uImageHeight = (uint32_t )iCurrentHeight ,
@@ -731,13 +738,13 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
731738 .szBufferOffset = szStagingOffset ,
732739 .uMipLevel = uMipLevel
733740 };
741+ szStagingOffset += szMipCompressedSize ;
734742
735743 gptGfx -> copy_buffer_to_texture (ptBlitEncoder ,
736744 gptResourceManager -> tStagingBuffer .tStagingBufferHandle ,
737745 tResource .tTexture ,
738746 1 ,
739747 & tBufferImageCopy );
740- szStagingOffset += szCurrentSize ;
741748 }
742749
743750 gptGfx -> end_blit_pass (ptBlitEncoder );
@@ -748,9 +755,26 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
748755
749756 gptImage -> free (puRawBytes );
750757 PL_FREE (auWorkingBuffer [0 ]);
751- }
752758
753- pl_sb_free (sbtNameConcat );
759+ plDdsWriteInfo tDDSWriteInfo = {
760+ .uWidth = (uint32_t )tImageInfo .iWidth ,
761+ .uHeight = (uint32_t )tImageInfo .iHeight ,
762+ .uDepth = 0 ,
763+ .uMips = ptTexture -> tDesc .uMips ,
764+ .uLayers = 1 ,
765+ .tFormat = PL_FORMAT_BC3_UNORM ,
766+ .tType = PL_TEXTURE_TYPE_2D
767+ };
768+
769+ uint8_t * pcBlah = PL_ALLOC (gptDds -> get_header_size () + szRequiredStagingSize );
770+ gptDds -> write_info (pcBlah , & tDDSWriteInfo );
771+
772+ memcpy (& pcBlah [gptDds -> get_header_size ()], (uint8_t * )& ptStagingBuffer -> tMemoryAllocation .pHostMapped [szOriginalStagingOffset ], szRequiredStagingSize );
773+ plVfsFileHandle tHandle = gptVfs -> open_file (sbtNameConcat , PL_VFS_FILE_MODE_WRITE );
774+ gptVfs -> write_file (tHandle , (uint8_t * )pcBlah , gptDds -> get_header_size () + szRequiredStagingSize );
775+ gptVfs -> close_file (tHandle );
776+ PL_FREE (pcBlah );
777+ }
754778 }
755779 else
756780 {
@@ -827,6 +851,7 @@ pl_resource_load_ex(const char* pcName, plResourceLoadFlags tFlags, uint8_t* puO
827851 pl_sb_push (gptResourceManager -> sbtTextureUploadJobs , tUpload );
828852 }
829853 }
854+ pl_sb_free (sbtNameConcat );
830855 break ;
831856 }
832857 }
0 commit comments