2 using System
.Collections
.Generic
;
3 using UnityEngine
.Assertions
;
5 namespace UnityEngine
.Rendering
.PostProcessing
7 // Temporary code dump until the texture format refactor goes into trunk...
10 /// A set of utilities to deal with texture formats.
12 public static class TextureFormatUtilities
14 static Dictionary
<int, RenderTextureFormat
> s_FormatAliasMap
;
15 static Dictionary
<int, bool> s_SupportedRenderTextureFormats
;
16 static Dictionary
<int, bool> s_SupportedTextureFormats
;
18 static TextureFormatUtilities()
20 s_FormatAliasMap
= new Dictionary
<int, RenderTextureFormat
>
22 { (int)TextureFormat.Alpha8, RenderTextureFormat.ARGB32 }
,
23 { (int)TextureFormat.ARGB4444, RenderTextureFormat.ARGB4444 }
,
24 { (int)TextureFormat.RGB24, RenderTextureFormat.ARGB32 }
,
25 { (int)TextureFormat.RGBA32, RenderTextureFormat.ARGB32 }
,
26 { (int)TextureFormat.ARGB32, RenderTextureFormat.ARGB32 }
,
27 { (int)TextureFormat.RGB565, RenderTextureFormat.RGB565 }
,
28 { (int)TextureFormat.R16, RenderTextureFormat.RHalf }
,
29 { (int)TextureFormat.DXT1, RenderTextureFormat.ARGB32 }
,
30 { (int)TextureFormat.DXT5, RenderTextureFormat.ARGB32 }
,
31 { (int)TextureFormat.RGBA4444, RenderTextureFormat.ARGB4444 }
,
32 { (int)TextureFormat.BGRA32, RenderTextureFormat.ARGB32 }
,
33 { (int)TextureFormat.RHalf, RenderTextureFormat.RHalf }
,
34 { (int)TextureFormat.RGHalf, RenderTextureFormat.RGHalf }
,
35 { (int)TextureFormat.RGBAHalf, RenderTextureFormat.ARGBHalf }
,
36 { (int)TextureFormat.RFloat, RenderTextureFormat.RFloat }
,
37 { (int)TextureFormat.RGFloat, RenderTextureFormat.RGFloat }
,
38 { (int)TextureFormat.RGBAFloat, RenderTextureFormat.ARGBFloat }
,
39 { (int)TextureFormat.RGB9e5Float, RenderTextureFormat.ARGBHalf }
,
40 { (int)TextureFormat.BC4, RenderTextureFormat.R8 }
,
41 { (int)TextureFormat.BC5, RenderTextureFormat.RGHalf }
,
42 { (int)TextureFormat.BC6H, RenderTextureFormat.ARGBHalf }
,
43 { (int)TextureFormat.BC7, RenderTextureFormat.ARGB32 }
,
44 #if !UNITY_IOS && !UNITY_TVOS
45 { (int)TextureFormat.DXT1Crunched, RenderTextureFormat.ARGB32 }
,
46 { (int)TextureFormat.DXT5Crunched, RenderTextureFormat.ARGB32 }
,
48 { (int)TextureFormat.PVRTC_RGB2, RenderTextureFormat.ARGB32 }
,
49 { (int)TextureFormat.PVRTC_RGBA2, RenderTextureFormat.ARGB32 }
,
50 { (int)TextureFormat.PVRTC_RGB4, RenderTextureFormat.ARGB32 }
,
51 { (int)TextureFormat.PVRTC_RGBA4, RenderTextureFormat.ARGB32 }
,
52 #if !UNITY_2018_1_OR_NEWER
53 { (int)TextureFormat.ATC_RGB4, RenderTextureFormat.ARGB32 }
,
54 { (int)TextureFormat.ATC_RGBA8, RenderTextureFormat.ARGB32 }
,
56 { (int)TextureFormat.ETC_RGB4, RenderTextureFormat.ARGB32 }
,
57 { (int)TextureFormat.ETC2_RGB, RenderTextureFormat.ARGB32 }
,
58 { (int)TextureFormat.ETC2_RGBA1, RenderTextureFormat.ARGB32 }
,
59 { (int)TextureFormat.ETC2_RGBA8, RenderTextureFormat.ARGB32 }
,
60 #if UNITY_2019_1_OR_NEWER
61 { (int)TextureFormat.ASTC_4x4, RenderTextureFormat.ARGB32 }
,
62 { (int)TextureFormat.ASTC_5x5, RenderTextureFormat.ARGB32 }
,
63 { (int)TextureFormat.ASTC_6x6, RenderTextureFormat.ARGB32 }
,
64 { (int)TextureFormat.ASTC_8x8, RenderTextureFormat.ARGB32 }
,
65 { (int)TextureFormat.ASTC_10x10, RenderTextureFormat.ARGB32 }
,
66 { (int)TextureFormat.ASTC_12x12, RenderTextureFormat.ARGB32 }
,
68 { (int)TextureFormat.ASTC_RGB_4x4, RenderTextureFormat.ARGB32 }
,
69 { (int)TextureFormat.ASTC_RGB_5x5, RenderTextureFormat.ARGB32 }
,
70 { (int)TextureFormat.ASTC_RGB_6x6, RenderTextureFormat.ARGB32 }
,
71 { (int)TextureFormat.ASTC_RGB_8x8, RenderTextureFormat.ARGB32 }
,
72 { (int)TextureFormat.ASTC_RGB_10x10, RenderTextureFormat.ARGB32 }
,
73 { (int)TextureFormat.ASTC_RGB_12x12, RenderTextureFormat.ARGB32 }
,
74 { (int)TextureFormat.ASTC_RGBA_4x4, RenderTextureFormat.ARGB32 }
,
75 { (int)TextureFormat.ASTC_RGBA_5x5, RenderTextureFormat.ARGB32 }
,
76 { (int)TextureFormat.ASTC_RGBA_6x6, RenderTextureFormat.ARGB32 }
,
77 { (int)TextureFormat.ASTC_RGBA_8x8, RenderTextureFormat.ARGB32 }
,
78 { (int)TextureFormat.ASTC_RGBA_10x10, RenderTextureFormat.ARGB32 }
,
79 { (int)TextureFormat.ASTC_RGBA_12x12, RenderTextureFormat.ARGB32 }
,
81 #if !UNITY_2018_3_OR_NEWER
82 { (int)TextureFormat.ETC_RGB4_3DS, RenderTextureFormat.ARGB32 }
,
83 { (int)TextureFormat.ETC_RGBA8_3DS, RenderTextureFormat.ARGB32 }
87 // TODO: refactor the next two scopes in a generic function once we have support for enum constraints on generics
88 // In 2018.1 SystemInfo.SupportsRenderTextureFormat() generates garbage so we need to
89 // cache its calls to avoid that...
91 s_SupportedRenderTextureFormats
= new Dictionary
<int, bool>();
92 var values
= Enum
.GetValues(typeof(RenderTextureFormat
));
94 foreach (var format
in values
)
96 if ((int)format
< 0) // Safe guard, negative values are deprecated stuff
99 if (IsObsolete(format
))
102 bool supported
= SystemInfo
.SupportsRenderTextureFormat((RenderTextureFormat
)format
);
103 s_SupportedRenderTextureFormats
[(int)format
] = supported
;
107 // Same for TextureFormat
109 s_SupportedTextureFormats
= new Dictionary
<int, bool>();
110 var values
= Enum
.GetValues(typeof(TextureFormat
));
112 foreach (var format
in values
)
114 if ((int)format
< 0) // Crashes the runtime otherwise (!)
117 if (IsObsolete(format
))
120 bool supported
= SystemInfo
.SupportsTextureFormat((TextureFormat
)format
);
121 s_SupportedTextureFormats
[(int)format
] = supported
;
126 static bool IsObsolete(object value)
128 var fieldInfo
= value.GetType().GetField(value.ToString());
129 var attributes
= (ObsoleteAttribute
[])fieldInfo
.GetCustomAttributes(typeof(ObsoleteAttribute
), false);
130 return attributes
!= null && attributes
.Length
> 0;
134 /// Returns a <see cref="RenderTextureFormat"/> compatible with the given texture's format.
136 /// <param name="texture">A texture to get a compatible format from</param>
137 /// <returns>A compatible render texture format</returns>
138 public static RenderTextureFormat
GetUncompressedRenderTextureFormat(Texture texture
)
140 Assert
.IsNotNull(texture
);
142 if (texture
is RenderTexture
)
143 return (texture
as RenderTexture
).format
;
145 if (texture
is Texture2D
)
147 var inFormat
= ((Texture2D
)texture
).format
;
148 RenderTextureFormat outFormat
;
150 if (!s_FormatAliasMap
.TryGetValue((int)inFormat
, out outFormat
))
151 throw new NotSupportedException("Texture format not supported");
156 return RenderTextureFormat
.Default
;
159 internal static bool IsSupported(this RenderTextureFormat format
)
162 s_SupportedRenderTextureFormats
.TryGetValue((int)format
, out supported
);
166 internal static bool IsSupported(this TextureFormat format
)
169 s_SupportedTextureFormats
.TryGetValue((int)format
, out supported
);