1 using UnityEngine
.Rendering
;
3 namespace UnityEngine
.Experimental
.Rendering
5 public class TextureCache2D
: TextureCache
7 private Texture2DArray m_Cache
;
9 public TextureCache2D(string cacheName
= "")
14 bool TextureHasMipmaps(Texture texture
)
17 if (texture
is Texture2D
)
18 return ((Texture2D
)texture
).mipmapCount
> 1;
20 return ((RenderTexture
)texture
).useMipMap
;
23 protected override bool TransferToSlice(CommandBuffer cmd
, int sliceIndex
, Texture
[] textureArray
)
25 // Make sure the array is not null or empty and that the first texture is a render-texture or a texture2D
26 if(textureArray
== null || textureArray
.Length
== 0 && (!(textureArray
[0] is RenderTexture
) && !(textureArray
[0] is Texture2D
)))
31 // First check here is to check if all the sub-texture have the same size
32 for(int texIDx
= 1; texIDx
< textureArray
.Length
; ++texIDx
)
34 // We cannot update if the textures if they don't have the same size or not the right type
35 if (textureArray
[texIDx
].width
!= textureArray
[0].width
|| textureArray
[texIDx
].height
!= textureArray
[0].height
|| (!(textureArray
[0] is RenderTexture
) && !(textureArray
[0] is Texture2D
)))
37 Debug
.LogWarning("All the sub-textures should have the same dimensions to be handled by the texture cache.");
42 // Do we have a mismatch ?
43 var mismatch
= (m_Cache
.width
!= textureArray
[0].width
) || (m_Cache
.height
!= textureArray
[0].height
);
45 if (textureArray
[0] is Texture2D
)
47 mismatch
|= (m_Cache
.format
!= (textureArray
[0] as Texture2D
).format
);
50 for (int texIDx
= 0; texIDx
< textureArray
.Length
; ++texIDx
)
54 cmd
.ConvertTexture(textureArray
[texIDx
], 0, m_Cache
, m_SliceSize
* sliceIndex
+ texIDx
);
58 if (TextureHasMipmaps(textureArray
[texIDx
]))
59 cmd
.CopyTexture(textureArray
[texIDx
], 0, m_Cache
, m_SliceSize
* sliceIndex
+ texIDx
);
61 Debug
.LogWarning("The texture '" + textureArray
[texIDx
] + "' should have mipmaps to be handled by the cookie texture array");
67 public override Texture
GetTexCache()
72 public bool AllocTextureArray(int numTextures
, int width
, int height
, TextureFormat format
, bool isMipMapped
)
74 var res
= AllocTextureArray(numTextures
);
75 m_NumMipLevels
= GetNumMips(width
, height
);
77 m_Cache
= new Texture2DArray(width
, height
, numTextures
, format
, isMipMapped
)
79 hideFlags
= HideFlags
.HideAndDontSave
,
80 wrapMode
= TextureWrapMode
.Clamp
,
81 name
= CoreUtils
.GetTextureAutoName(width
, height
, format
, TextureDimension
.Tex2DArray
, depth
: numTextures
, name
: m_CacheName
)
89 CoreUtils
.Destroy(m_Cache
);
92 internal static long GetApproxCacheSizeInByte(int nbElement
, int resolution
, int sliceSize
)
94 return (long)((long)nbElement
* resolution
* resolution
* k_FP16SizeInByte
* k_NbChannel
* k_MipmapFactorApprox
* sliceSize
);
97 internal static int GetMaxCacheSizeForWeightInByte(int weight
, int resolution
, int sliceSize
)
99 int theoricalResult
= Mathf
.FloorToInt(weight
/ ((long)resolution
* resolution
* k_FP16SizeInByte
* k_NbChannel
* k_MipmapFactorApprox
* sliceSize
));
100 return Mathf
.Clamp(theoricalResult
, 1, k_MaxSupported
);