git.s-ol.nu ~forks/DiligentTools / 39cdd71
GLTF Loader: enabled caching compressed textures assiduous 11 months ago
2 changed file(s) with 51 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
751751 if (CacheInfo.pResourceMgr != nullptr)
752752 {
753753 TexInfo.pCacheAllocation = CacheInfo.pResourceMgr->AllocateTextureSpace(TEX_FORMAT_RGBA8_UNORM, gltf_image.width, gltf_image.height);
754 if (TexInfo.pCacheAllocation)
755 {
756 const auto& TexDesc = TexInfo.pCacheAllocation->GetTexDesc();
757
758 const auto& Region = TexInfo.pCacheAllocation->GetRegion();
759 TexInitData = PrepareGLTFTextureInitData(gltf_image, AlphaCutoff, Region.x, Region.y, TexDesc.MipLevels);
760
761 TexInfo.UVScaleBias.x = static_cast<float>(gltf_image.width) / static_cast<float>(TexDesc.Width);
762 TexInfo.UVScaleBias.y = static_cast<float>(gltf_image.height) / static_cast<float>(TexDesc.Height);
763 TexInfo.UVScaleBias.z = static_cast<float>(Region.x) / static_cast<float>(TexDesc.Width);
764 TexInfo.UVScaleBias.w = static_cast<float>(Region.y) / static_cast<float>(TexDesc.Height);
765
766 TexInfo.Slice = static_cast<float>(TexInfo.pCacheAllocation->GetSlice());
767 }
768754 }
769755 else
770756 {
792778 // Create the texture from raw bits
793779 RefCntAutoPtr<DataBlobImpl> pRawData(MakeNewRCObj<DataBlobImpl>()(gltf_image.image.size()));
794780 memcpy(pRawData->GetDataPtr(), gltf_image.image.data(), gltf_image.image.size());
781 TextureLoadInfo LoadInfo;
782 if (CacheInfo.pResourceMgr != nullptr)
783 {
784 LoadInfo.Usage = USAGE_STAGING;
785 LoadInfo.BindFlags = BIND_NONE;
786 LoadInfo.CPUAccessFlags = CPU_ACCESS_WRITE;
787 }
795788 switch (gltf_image.pixel_type)
796789 {
797790 case IMAGE_FILE_FORMAT_DDS:
798 CreateTextureFromDDS(pRawData, TextureLoadInfo{}, pDevice, &TexInfo.pTexture);
791 CreateTextureFromDDS(pRawData, LoadInfo, pDevice, CacheInfo.pResourceMgr != nullptr ? &TexInitData.pStagingTex : &TexInfo.pTexture);
799792 break;
800793
801794 case IMAGE_FILE_FORMAT_KTX:
802 CreateTextureFromKTX(pRawData, TextureLoadInfo{}, pDevice, &TexInfo.pTexture);
795 CreateTextureFromKTX(pRawData, LoadInfo, pDevice, &TexInfo.pTexture);
803796 break;
804797
805798 default:
806799 UNEXPECTED("Unknown raw image format");
807800 }
801 if (CacheInfo.pResourceMgr != nullptr && TexInitData.pStagingTex)
802 {
803 const auto& TexDesc = TexInitData.pStagingTex->GetDesc();
804 TexInfo.pCacheAllocation = CacheInfo.pResourceMgr->AllocateTextureSpace(TexDesc.Format, TexDesc.Width, TexDesc.Height);
805 }
806 }
807
808 if (TexInfo.pCacheAllocation)
809 {
810 const auto& AtlasDesc = TexInfo.pCacheAllocation->GetTexDesc();
811
812 const auto& Region = TexInfo.pCacheAllocation->GetRegion();
813 if (!TexInitData.pStagingTex)
814 {
815 TexInitData = PrepareGLTFTextureInitData(gltf_image, AlphaCutoff, Region.x, Region.y, AtlasDesc.MipLevels);
816 }
817
818 TexInfo.UVScaleBias.x = static_cast<float>(gltf_image.width) / static_cast<float>(AtlasDesc.Width);
819 TexInfo.UVScaleBias.y = static_cast<float>(gltf_image.height) / static_cast<float>(AtlasDesc.Height);
820 TexInfo.UVScaleBias.z = static_cast<float>(Region.x) / static_cast<float>(AtlasDesc.Width);
821 TexInfo.UVScaleBias.w = static_cast<float>(Region.y) / static_cast<float>(AtlasDesc.Height);
822
823 TexInfo.Slice = static_cast<float>(TexInfo.pCacheAllocation->GetSlice());
808824 }
809825
810826 if (!InitData)
855871
856872 auto& TexData = InitData->Textures[i];
857873
858 const auto& Levels = TexData.Levels;
874 const auto& Levels = TexData.Levels;
875 const auto DstSlice = static_cast<Uint32>(Textures[i].Slice);
859876 if (!Levels.empty())
860877 {
861878 VERIFY_EXPR(Levels.size() == 1 || Levels.size() == pTexture->GetDesc().MipLevels);
864881 const auto& Level = Levels[mip];
865882
866883 TextureSubResData SubresData{Level.Data.data(), Level.Stride};
867 pCtx->UpdateTexture(pTexture, mip, static_cast<Uint32>(Textures[i].Slice), Level.UpdateBox, SubresData, RESOURCE_STATE_TRANSITION_MODE_NONE, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
884 pCtx->UpdateTexture(pTexture, mip, DstSlice, Level.UpdateBox, SubresData, RESOURCE_STATE_TRANSITION_MODE_NONE, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
868885 }
869886
870887 if (Levels.size() == 1 && pTexture->GetDesc().MipLevels > 1)
880897 }
881898 else if (TexData.pStagingTex)
882899 {
883 UNSUPPORTED("Not yet implemented");
900 CopyTextureAttribs CopyAttribs;
901 CopyAttribs.pSrcTexture = TexData.pStagingTex;
902 CopyAttribs.pDstTexture = pTexture;
903 CopyAttribs.SrcTextureTransitionMode = RESOURCE_STATE_TRANSITION_MODE_TRANSITION;
904 CopyAttribs.DstTextureTransitionMode = RESOURCE_STATE_TRANSITION_MODE_TRANSITION;
905
906 const auto& DstTexDesc = pTexture->GetDesc();
907 for (Uint32 mip = 0; mip < DstTexDesc.MipLevels; ++mip)
908 {
909 CopyAttribs.SrcSlice = 0;
910 CopyAttribs.DstSlice = DstSlice;
911 CopyAttribs.SrcMipLevel = mip;
912 CopyAttribs.DstMipLevel = mip;
913 pCtx->CopyTexture(CopyAttribs);
914 }
884915 }
885916 else
886917 {
290290 cache_it = m_Textures.find(Fmt);
291291 if (cache_it == m_Textures.end())
292292 {
293 DEV_CHECK_ERR(m_DefaultTexDesc.Width > 0 && m_DefaultTexDesc.Height > 0, "Default texture description is not initialized");
293294 TextureCacheAttribs NewCacheAttribs;
294295 NewCacheAttribs.Desc = m_DefaultTexDesc;
295296