Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit f0e0489

Browse files
committed
TextureFormatUtilities refactor
1 parent 2b48a5b commit f0e0489

File tree

1 file changed

+70
-60
lines changed

1 file changed

+70
-60
lines changed

PostProcessing/Runtime/Utils/TextureFormatUtilities.cs

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,87 @@
11
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine.Assertions;
24

35
namespace UnityEngine.Rendering.PostProcessing
46
{
57
// Temporary code dump until the texture format refactor goes into trunk...
68
public static class TextureFormatUtilities
79
{
10+
static Dictionary<TextureFormat, RenderTextureFormat> m_FormatMap;
11+
12+
static TextureFormatUtilities()
13+
{
14+
m_FormatMap = new Dictionary<TextureFormat, RenderTextureFormat>
15+
{
16+
{ TextureFormat.Alpha8, RenderTextureFormat.ARGB32 },
17+
{ TextureFormat.ARGB4444, RenderTextureFormat.ARGB4444 },
18+
{ TextureFormat.RGB24, RenderTextureFormat.ARGB32 },
19+
{ TextureFormat.RGBA32, RenderTextureFormat.ARGB32 },
20+
{ TextureFormat.ARGB32, RenderTextureFormat.ARGB32 },
21+
{ TextureFormat.RGB565, RenderTextureFormat.RGB565 },
22+
{ TextureFormat.R16, RenderTextureFormat.RHalf },
23+
{ TextureFormat.DXT1, RenderTextureFormat.ARGB32 },
24+
{ TextureFormat.DXT5, RenderTextureFormat.ARGB32 },
25+
{ TextureFormat.RGBA4444, RenderTextureFormat.ARGB4444 },
26+
{ TextureFormat.BGRA32, RenderTextureFormat.ARGB32 },
27+
{ TextureFormat.RHalf, RenderTextureFormat.RHalf },
28+
{ TextureFormat.RGHalf, RenderTextureFormat.RGHalf },
29+
{ TextureFormat.RGBAHalf, RenderTextureFormat.ARGBHalf },
30+
{ TextureFormat.RFloat, RenderTextureFormat.RFloat },
31+
{ TextureFormat.RGFloat, RenderTextureFormat.RGFloat },
32+
{ TextureFormat.RGBAFloat, RenderTextureFormat.ARGBFloat },
33+
{ TextureFormat.RGB9e5Float, RenderTextureFormat.ARGBHalf },
34+
{ TextureFormat.BC4, RenderTextureFormat.R8 },
35+
{ TextureFormat.BC5, RenderTextureFormat.RGHalf },
36+
{ TextureFormat.BC6H, RenderTextureFormat.ARGBHalf },
37+
{ TextureFormat.BC7, RenderTextureFormat.ARGB32 },
38+
#if !UNITY_IOS
39+
{ TextureFormat.DXT1Crunched, RenderTextureFormat.ARGB32 },
40+
{ TextureFormat.DXT5Crunched, RenderTextureFormat.ARGB32 },
41+
#endif
42+
{ TextureFormat.PVRTC_RGB2, RenderTextureFormat.ARGB32 },
43+
{ TextureFormat.PVRTC_RGBA2, RenderTextureFormat.ARGB32 },
44+
{ TextureFormat.PVRTC_RGB4, RenderTextureFormat.ARGB32 },
45+
{ TextureFormat.PVRTC_RGBA4, RenderTextureFormat.ARGB32 },
46+
{ TextureFormat.ETC_RGB4, RenderTextureFormat.ARGB32 },
47+
{ TextureFormat.ATC_RGB4, RenderTextureFormat.ARGB32 },
48+
{ TextureFormat.ATC_RGBA8, RenderTextureFormat.ARGB32 },
49+
{ TextureFormat.ETC2_RGB, RenderTextureFormat.ARGB32 },
50+
{ TextureFormat.ETC2_RGBA1, RenderTextureFormat.ARGB32 },
51+
{ TextureFormat.ETC2_RGBA8, RenderTextureFormat.ARGB32 },
52+
{ TextureFormat.ASTC_RGB_4x4, RenderTextureFormat.ARGB32 },
53+
{ TextureFormat.ASTC_RGB_5x5, RenderTextureFormat.ARGB32 },
54+
{ TextureFormat.ASTC_RGB_6x6, RenderTextureFormat.ARGB32 },
55+
{ TextureFormat.ASTC_RGB_8x8, RenderTextureFormat.ARGB32 },
56+
{ TextureFormat.ASTC_RGB_10x10, RenderTextureFormat.ARGB32 },
57+
{ TextureFormat.ASTC_RGB_12x12, RenderTextureFormat.ARGB32 },
58+
{ TextureFormat.ASTC_RGBA_4x4, RenderTextureFormat.ARGB32 },
59+
{ TextureFormat.ASTC_RGBA_5x5, RenderTextureFormat.ARGB32 },
60+
{ TextureFormat.ASTC_RGBA_6x6, RenderTextureFormat.ARGB32 },
61+
{ TextureFormat.ASTC_RGBA_8x8, RenderTextureFormat.ARGB32 },
62+
{ TextureFormat.ASTC_RGBA_10x10, RenderTextureFormat.ARGB32 },
63+
{ TextureFormat.ASTC_RGBA_12x12, RenderTextureFormat.ARGB32 },
64+
{ TextureFormat.ETC_RGB4_3DS, RenderTextureFormat.ARGB32 },
65+
{ TextureFormat.ETC_RGBA8_3DS, RenderTextureFormat.ARGB32 }
66+
};
67+
}
68+
869
public static RenderTextureFormat GetUncompressedRenderTextureFormat(Texture texture)
970
{
71+
Assert.IsNotNull(texture);
72+
1073
if (texture is RenderTexture)
1174
return (texture as RenderTexture).format;
1275

1376
if (texture is Texture2D)
1477
{
15-
switch ((texture as Texture2D).format)
16-
{
17-
case TextureFormat.Alpha8: return RenderTextureFormat.ARGB32;
18-
case TextureFormat.ARGB4444: return RenderTextureFormat.ARGB4444;
19-
case TextureFormat.RGB24: return RenderTextureFormat.ARGB32;
20-
case TextureFormat.RGBA32: return RenderTextureFormat.ARGB32;
21-
case TextureFormat.ARGB32: return RenderTextureFormat.ARGB32;
22-
case TextureFormat.RGB565: return RenderTextureFormat.RGB565;
23-
case TextureFormat.R16: return RenderTextureFormat.RHalf; // ???
24-
case TextureFormat.DXT1: return RenderTextureFormat.ARGB32;
25-
case TextureFormat.DXT5: return RenderTextureFormat.ARGB32;
26-
case TextureFormat.RGBA4444: return RenderTextureFormat.ARGB4444;
27-
case TextureFormat.BGRA32: return RenderTextureFormat.ARGB32;
28-
case TextureFormat.RHalf: return RenderTextureFormat.RHalf;
29-
case TextureFormat.RGHalf: return RenderTextureFormat.RGHalf;
30-
case TextureFormat.RGBAHalf: return RenderTextureFormat.ARGBHalf;
31-
case TextureFormat.RFloat: return RenderTextureFormat.RFloat;
32-
case TextureFormat.RGFloat: return RenderTextureFormat.RGFloat;
33-
case TextureFormat.RGBAFloat: return RenderTextureFormat.ARGBFloat;
34-
case TextureFormat.RGB9e5Float: return RenderTextureFormat.ARGBHalf;
35-
case TextureFormat.BC4: return RenderTextureFormat.R8;
36-
case TextureFormat.BC5: return RenderTextureFormat.RGHalf;
37-
case TextureFormat.BC6H: return RenderTextureFormat.ARGBHalf;
38-
case TextureFormat.BC7: return RenderTextureFormat.ARGB32;
39-
#if !UNITY_IOS
40-
case TextureFormat.DXT1Crunched: return RenderTextureFormat.ARGB32;
41-
case TextureFormat.DXT5Crunched: return RenderTextureFormat.ARGB32;
42-
#endif
43-
case TextureFormat.PVRTC_RGB2: return RenderTextureFormat.ARGB32;
44-
case TextureFormat.PVRTC_RGBA2: return RenderTextureFormat.ARGB32;
45-
case TextureFormat.PVRTC_RGB4: return RenderTextureFormat.ARGB32;
46-
case TextureFormat.PVRTC_RGBA4: return RenderTextureFormat.ARGB32;
47-
case TextureFormat.ETC_RGB4: return RenderTextureFormat.ARGB32;
48-
case TextureFormat.ATC_RGB4: return RenderTextureFormat.ARGB32;
49-
case TextureFormat.ATC_RGBA8: return RenderTextureFormat.ARGB32;
50-
case TextureFormat.ETC2_RGB: return RenderTextureFormat.ARGB32;
51-
case TextureFormat.ETC2_RGBA1: return RenderTextureFormat.ARGB32;
52-
case TextureFormat.ETC2_RGBA8: return RenderTextureFormat.ARGB32;
53-
case TextureFormat.ASTC_RGB_4x4: return RenderTextureFormat.ARGB32;
54-
case TextureFormat.ASTC_RGB_5x5: return RenderTextureFormat.ARGB32;
55-
case TextureFormat.ASTC_RGB_6x6: return RenderTextureFormat.ARGB32;
56-
case TextureFormat.ASTC_RGB_8x8: return RenderTextureFormat.ARGB32;
57-
case TextureFormat.ASTC_RGB_10x10: return RenderTextureFormat.ARGB32;
58-
case TextureFormat.ASTC_RGB_12x12: return RenderTextureFormat.ARGB32;
59-
case TextureFormat.ASTC_RGBA_4x4: return RenderTextureFormat.ARGB32;
60-
case TextureFormat.ASTC_RGBA_5x5: return RenderTextureFormat.ARGB32;
61-
case TextureFormat.ASTC_RGBA_6x6: return RenderTextureFormat.ARGB32;
62-
case TextureFormat.ASTC_RGBA_8x8: return RenderTextureFormat.ARGB32;
63-
case TextureFormat.ASTC_RGBA_10x10: return RenderTextureFormat.ARGB32;
64-
case TextureFormat.ASTC_RGBA_12x12: return RenderTextureFormat.ARGB32;
65-
case TextureFormat.ETC_RGB4_3DS: return RenderTextureFormat.ARGB32;
66-
case TextureFormat.ETC_RGBA8_3DS: return RenderTextureFormat.ARGB32;
67-
case TextureFormat.EAC_R: goto default;
68-
case TextureFormat.EAC_R_SIGNED: goto default;
69-
case TextureFormat.EAC_RG: goto default;
70-
case TextureFormat.EAC_RG_SIGNED: goto default;
71-
case TextureFormat.YUY2: goto default;
72-
default:
73-
throw new NotSupportedException("Texture format not supported");
74-
}
78+
var inFormat = ((Texture2D)texture).format;
79+
RenderTextureFormat outFormat;
80+
81+
if (!m_FormatMap.TryGetValue(inFormat, out outFormat))
82+
throw new NotSupportedException("Texture format not supported");
83+
84+
return outFormat;
7585
}
7686

7787
return RenderTextureFormat.Default;

0 commit comments

Comments
 (0)