MCPcopy Create free account
hub / github.com/carbonengine/trinity / CreateAndCopyFromRenderTarget

Method CreateAndCopyFromRenderTarget

trinity/Resources/TriTextureRes.cpp:894–958  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

892}
893
894bool TriTextureRes::CreateAndCopyFromRenderTarget( Tr2RenderTarget* renderTarget )
895{
896 USE_MAIN_THREAD_RENDER_CONTEXT();
897
898 DestroyOwnTexture();
899
900 ON_BLOCK_EXIT( [&] { SetTexture( m_ownTexture ); } );
901
902 if( !renderTarget || !renderTarget->IsValid() )
903 {
904 return false;
905 }
906
907 auto& rt = renderTarget->GetRenderTarget();
908
909 auto width = rt.GetWidth();
910 auto height = rt.GetHeight();
911
912 // With mipmaps there may be staging resources involved, so just defer the problem to Tr2TextureAL::CopySubresourcRegion
913 if( rt.GetMipCount() != 1 )
914 {
915 Tr2BitmapDimensions bd( width, height, 0, rt.GetFormat() ); // use this to compute true mipcount of new texture
916
917 {
918 USE_MAIN_THREAD_RENDER_CONTEXT();
919 CR_RETURN_VAL( m_ownTexture.Create(
920 Tr2BitmapDimensions( width, height, bd.GetTrueMipCount(), rt.GetFormat() ),
921 Tr2GpuUsage::SHADER_RESOURCE | Tr2GpuUsage::COPY_DESTINATION,
922 Tr2CpuUsage::READ | Tr2CpuUsage::WRITE,
923 renderContext ),
924 false );
925 }
926
927 Tr2TextureSubresource dst;
928 dst.SetRect( 0, 0, width, height );
929 if( FAILED( m_ownTexture.CopySubresourceRegion( Tr2TextureSubresource(), *renderTarget, dst, renderContext ) ) )
930 {
931 m_ownTexture = Tr2TextureAL();
932 return false;
933 }
934 }
935 else
936 {
937 // No mipmaps, just locking the RT and initializing a new texture with its contents using initialData should work.
938 Tr2SubresourceData srd;
939 if( FAILED( rt.MapForReading( Tr2TextureSubresource( 0 ), srd.m_sysMem, srd.m_sysMemPitch, renderContext ) ) )
940 {
941 return false;
942 }
943 ON_BLOCK_EXIT( [&] { rt.UnmapForReading( renderContext ); } );
944
945 srd.m_sysMemSlicePitch = rt.GetHeight() * srd.m_sysMemPitch;
946
947 CR_RETURN_VAL( m_ownTexture.Create(
948 Tr2BitmapDimensions( width, height, 1, rt.GetFormat() ),
949 Tr2GpuUsage::SHADER_RESOURCE | Tr2GpuUsage::COPY_DESTINATION,
950 Tr2CpuUsage::READ,
951 &srd,

Callers

nothing calls this directly

Calls 14

GetRenderTargetMethod · 0.80
GetTrueMipCountMethod · 0.80
SetRectMethod · 0.80
Tr2TextureALClass · 0.50
IsValidMethod · 0.45
GetWidthMethod · 0.45
GetHeightMethod · 0.45
GetMipCountMethod · 0.45
GetFormatMethod · 0.45
CreateMethod · 0.45
CopySubresourceRegionMethod · 0.45

Tested by

no test coverage detected