aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Gal/ImageFormatConverter.cs
blob: 7aa39f5fb0027f7fceddfd386ff64c1deed6fbb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;

namespace Ryujinx.Graphics.Gal
{
    public static class ImageFormatConverter
    {
        public static GalImageFormat ConvertTexture(
            GalTextureFormat Format,
            GalTextureType RType,
            GalTextureType GType,
            GalTextureType BType,
            GalTextureType AType)
        {
            if (RType != GType || RType != BType || RType != AType)
            {
                throw new NotImplementedException("Per component types are not implemented");
            }

            GalTextureType Type = RType;

            switch (Type)
            {
                case GalTextureType.Snorm:
                    switch (Format)
                    {
                        case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SNORM;
                        case GalTextureFormat.A8B8G8R8:     return GalImageFormat.A8B8G8R8_SNORM_PACK32;
                        case GalTextureFormat.A2B10G10R10:  return GalImageFormat.A2B10G10R10_SNORM_PACK32;
                        case GalTextureFormat.G8R8:         return GalImageFormat.R8G8_SNORM;
                        case GalTextureFormat.R16:          return GalImageFormat.R16_SNORM;
                        case GalTextureFormat.R8:           return GalImageFormat.R8_SNORM;
                        case GalTextureFormat.BC4:          return GalImageFormat.BC4_SNORM_BLOCK;
                        case GalTextureFormat.BC5:          return GalImageFormat.BC5_SNORM_BLOCK;
                    }
                    break;

                case GalTextureType.Unorm:
                    switch (Format)
                    {
                        case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_UNORM;
                        case GalTextureFormat.A8B8G8R8:     return GalImageFormat.A8B8G8R8_UNORM_PACK32;
                        case GalTextureFormat.A2B10G10R10:  return GalImageFormat.A2B10G10R10_UNORM_PACK32;
                        case GalTextureFormat.A4B4G4R4:     return GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED;
                        case GalTextureFormat.A1B5G5R5:     return GalImageFormat.A1R5G5B5_UNORM_PACK16;
                        case GalTextureFormat.B5G6R5:       return GalImageFormat.B5G6R5_UNORM_PACK16;
                        case GalTextureFormat.BC7U:         return GalImageFormat.BC7_UNORM_BLOCK;
                        case GalTextureFormat.G8R8:         return GalImageFormat.R8G8_UNORM;
                        case GalTextureFormat.R16:          return GalImageFormat.R16_UNORM;
                        case GalTextureFormat.R8:           return GalImageFormat.R8_UNORM;
                        case GalTextureFormat.BC1:          return GalImageFormat.BC1_RGBA_UNORM_BLOCK;
                        case GalTextureFormat.BC2:          return GalImageFormat.BC2_UNORM_BLOCK;
                        case GalTextureFormat.BC3:          return GalImageFormat.BC3_UNORM_BLOCK;
                        case GalTextureFormat.BC4:          return GalImageFormat.BC4_UNORM_BLOCK;
                        case GalTextureFormat.BC5:          return GalImageFormat.BC5_UNORM_BLOCK;
                        case GalTextureFormat.Z24S8:        return GalImageFormat.D24_UNORM_S8_UINT;
                        case GalTextureFormat.ZF32_X24S8:   return GalImageFormat.D32_SFLOAT_S8_UINT;
                        case GalTextureFormat.Astc2D4x4:    return GalImageFormat.ASTC_4x4_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D5x5:    return GalImageFormat.ASTC_5x5_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D6x6:    return GalImageFormat.ASTC_6x6_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D8x8:    return GalImageFormat.ASTC_8x8_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D10x10:  return GalImageFormat.ASTC_10x10_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D12x12:  return GalImageFormat.ASTC_12x12_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D5x4:    return GalImageFormat.ASTC_5x4_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D6x5:    return GalImageFormat.ASTC_6x5_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D8x6:    return GalImageFormat.ASTC_8x6_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D10x8:   return GalImageFormat.ASTC_10x8_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D12x10:  return GalImageFormat.ASTC_12x10_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D8x5:    return GalImageFormat.ASTC_8x5_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D10x5:   return GalImageFormat.ASTC_10x5_UNORM_BLOCK;
                        case GalTextureFormat.Astc2D10x6:   return GalImageFormat.ASTC_10x6_UNORM_BLOCK;
                    }
                    break;

                case GalTextureType.Sint:
                    switch (Format)
                    {
                        case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_SINT;
                        case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SINT;
                        case GalTextureFormat.R32G32:       return GalImageFormat.R32G32_SINT;
                        case GalTextureFormat.A8B8G8R8:     return GalImageFormat.A8B8G8R8_SINT_PACK32;
                        case GalTextureFormat.A2B10G10R10:  return GalImageFormat.A2B10G10R10_SINT_PACK32;
                        case GalTextureFormat.R32:          return GalImageFormat.R32_SINT;
                        case GalTextureFormat.G8R8:         return GalImageFormat.R8G8_SINT;
                        case GalTextureFormat.R16:          return GalImageFormat.R16_SINT;
                        case GalTextureFormat.R8:           return GalImageFormat.R8_SINT;
                    }
                    break;

                case GalTextureType.Uint:
                    switch (Format)
                    {
                        case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_UINT;
                        case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_UINT;
                        case GalTextureFormat.R32G32:       return GalImageFormat.R32G32_UINT;
                        case GalTextureFormat.A8B8G8R8:     return GalImageFormat.A8B8G8R8_UINT_PACK32;
                        case GalTextureFormat.A2B10G10R10:  return GalImageFormat.A2B10G10R10_UINT_PACK32;
                        case GalTextureFormat.R32:          return GalImageFormat.R32_UINT;
                        case GalTextureFormat.G8R8:         return GalImageFormat.R8G8_UINT;
                        case GalTextureFormat.R16:          return GalImageFormat.R16_UINT;
                        case GalTextureFormat.R8:           return GalImageFormat.R8_UINT;
                    }
                    break;

                case GalTextureType.Snorm_Force_Fp16:
                    //TODO
                    break;

                case GalTextureType.Unorm_Force_Fp16:
                    //TODO
                    break;

                case GalTextureType.Float:
                    switch (Format)
                    {
                        case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_SFLOAT;
                        case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SFLOAT;
                        case GalTextureFormat.R32G32:       return GalImageFormat.R32G32_SFLOAT;
                        case GalTextureFormat.R32:          return GalImageFormat.R32_SFLOAT;
                        case GalTextureFormat.BC6H_SF16:    return GalImageFormat.BC6H_SFLOAT_BLOCK;
                        case GalTextureFormat.BC6H_UF16:    return GalImageFormat.BC6H_UFLOAT_BLOCK;
                        case GalTextureFormat.R16:          return GalImageFormat.R16_SFLOAT;
                        case GalTextureFormat.BF10GF11RF11: return GalImageFormat.B10G11R11_UFLOAT_PACK32;
                        case GalTextureFormat.ZF32:         return GalImageFormat.D32_SFLOAT;
                    }
                    break;
            }

            throw new NotImplementedException("0x" + ((int)Format).ToString("x2") + " " + Type.ToString());
        }

        public static GalImageFormat ConvertFrameBuffer(GalFrameBufferFormat Format)
        {
            switch (Format)
            {
                case GalFrameBufferFormat.R32Float:       return GalImageFormat.R32_SFLOAT;
                case GalFrameBufferFormat.RGB10A2Unorm:   return GalImageFormat.A2B10G10R10_UNORM_PACK32;
                case GalFrameBufferFormat.RGBA8Srgb:      return GalImageFormat.A8B8G8R8_SRGB_PACK32;
                case GalFrameBufferFormat.RGBA16Float:    return GalImageFormat.R16G16B16A16_SFLOAT;
                case GalFrameBufferFormat.R16Float:       return GalImageFormat.R16_SFLOAT;
                case GalFrameBufferFormat.R8Unorm:        return GalImageFormat.R8_UNORM;
                case GalFrameBufferFormat.RGBA8Unorm:     return GalImageFormat.A8B8G8R8_UNORM_PACK32;
                case GalFrameBufferFormat.R11G11B10Float: return GalImageFormat.B10G11R11_UFLOAT_PACK32;
                case GalFrameBufferFormat.RGBA32Float:    return GalImageFormat.R32G32B32A32_SFLOAT;
                case GalFrameBufferFormat.RG16Snorm:      return GalImageFormat.R16G16_SNORM;
                case GalFrameBufferFormat.RG16Float:      return GalImageFormat.R16G16_SFLOAT;
                case GalFrameBufferFormat.RG8Snorm:       return GalImageFormat.R8_SNORM;
                case GalFrameBufferFormat.RGBA8Snorm:     return GalImageFormat.A8B8G8R8_SNORM_PACK32;
                case GalFrameBufferFormat.RG8Unorm:       return GalImageFormat.R8G8_UNORM;
                case GalFrameBufferFormat.BGRA8Unorm:     return GalImageFormat.A8B8G8R8_UNORM_PACK32;
                case GalFrameBufferFormat.BGRA8Srgb:      return GalImageFormat.A8B8G8R8_SRGB_PACK32;
                case GalFrameBufferFormat.RG32Float:      return GalImageFormat.R32G32_SFLOAT;
                case GalFrameBufferFormat.RG32Sint:       return GalImageFormat.R32G32_SINT;
                case GalFrameBufferFormat.RG32Uint:       return GalImageFormat.R32G32_UINT;
            }

            throw new NotImplementedException(Format.ToString());
        }

        public static GalImageFormat ConvertZeta(GalZetaFormat Format)
        {
            switch (Format)
            {
                case GalZetaFormat.Z32Float:      return GalImageFormat.D32_SFLOAT;
                case GalZetaFormat.S8Z24Unorm:    return GalImageFormat.D24_UNORM_S8_UINT;
                case GalZetaFormat.Z16Unorm:      return GalImageFormat.D16_UNORM;
                case GalZetaFormat.Z32S8X24Float: return GalImageFormat.D32_SFLOAT_S8_UINT;
            }

            throw new NotImplementedException(Format.ToString());
        }

        public static bool HasColor(GalImageFormat Format)
        {
            switch (Format)
            {
                case GalImageFormat.R32G32B32A32_SFLOAT:
                case GalImageFormat.R32G32B32A32_SINT:
                case GalImageFormat.R32G32B32A32_UINT:
                case GalImageFormat.R16G16B16A16_SFLOAT:
                case GalImageFormat.R16G16B16A16_SINT:
                case GalImageFormat.R16G16B16A16_UINT:
                case GalImageFormat.R32G32_SFLOAT:
                case GalImageFormat.R32G32_SINT:
                case GalImageFormat.R32G32_UINT:
                case GalImageFormat.A8B8G8R8_SNORM_PACK32:
                case GalImageFormat.A8B8G8R8_UNORM_PACK32:
                case GalImageFormat.A8B8G8R8_SINT_PACK32:
                case GalImageFormat.A8B8G8R8_UINT_PACK32:
                case GalImageFormat.A2B10G10R10_SINT_PACK32:
                case GalImageFormat.A2B10G10R10_SNORM_PACK32:
                case GalImageFormat.A2B10G10R10_UINT_PACK32:
                case GalImageFormat.A2B10G10R10_UNORM_PACK32:
                case GalImageFormat.R32_SFLOAT:
                case GalImageFormat.R32_SINT:
                case GalImageFormat.R32_UINT:
                case GalImageFormat.BC6H_SFLOAT_BLOCK:
                case GalImageFormat.BC6H_UFLOAT_BLOCK:
                case GalImageFormat.A1R5G5B5_UNORM_PACK16:
                case GalImageFormat.B5G6R5_UNORM_PACK16:
                case GalImageFormat.BC7_UNORM_BLOCK:
                case GalImageFormat.R16G16_SFLOAT:
                case GalImageFormat.R16G16_SINT:
                case GalImageFormat.R16G16_SNORM:
                case GalImageFormat.R16G16_UNORM:
                case GalImageFormat.R8G8_SINT:
                case GalImageFormat.R8G8_SNORM:
                case GalImageFormat.R8G8_UINT:
                case GalImageFormat.R8G8_UNORM:
                case GalImageFormat.R16_SFLOAT:
                case GalImageFormat.R16_SINT:
                case GalImageFormat.R16_SNORM:
                case GalImageFormat.R16_UINT:
                case GalImageFormat.R16_UNORM:
                case GalImageFormat.R8_SINT:
                case GalImageFormat.R8_SNORM:
                case GalImageFormat.R8_UINT:
                case GalImageFormat.R8_UNORM:
                case GalImageFormat.B10G11R11_UFLOAT_PACK32:
                case GalImageFormat.BC1_RGBA_UNORM_BLOCK:
                case GalImageFormat.BC2_UNORM_BLOCK:
                case GalImageFormat.BC3_UNORM_BLOCK:
                case GalImageFormat.BC4_UNORM_BLOCK:
                case GalImageFormat.BC5_UNORM_BLOCK:
                case GalImageFormat.ASTC_4x4_UNORM_BLOCK:
                case GalImageFormat.ASTC_5x5_UNORM_BLOCK:
                case GalImageFormat.ASTC_6x6_UNORM_BLOCK:
                case GalImageFormat.ASTC_8x8_UNORM_BLOCK:
                case GalImageFormat.ASTC_10x10_UNORM_BLOCK:
                case GalImageFormat.ASTC_12x12_UNORM_BLOCK:
                case GalImageFormat.ASTC_5x4_UNORM_BLOCK:
                case GalImageFormat.ASTC_6x5_UNORM_BLOCK:
                case GalImageFormat.ASTC_8x6_UNORM_BLOCK:
                case GalImageFormat.ASTC_10x8_UNORM_BLOCK:
                case GalImageFormat.ASTC_12x10_UNORM_BLOCK:
                case GalImageFormat.ASTC_8x5_UNORM_BLOCK:
                case GalImageFormat.ASTC_10x5_UNORM_BLOCK:
                case GalImageFormat.ASTC_10x6_UNORM_BLOCK:
                case GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED:
                    return true;

                case GalImageFormat.D24_UNORM_S8_UINT:
                case GalImageFormat.D32_SFLOAT:
                case GalImageFormat.D16_UNORM:
                case GalImageFormat.D32_SFLOAT_S8_UINT:
                    return false;
            }

            throw new NotImplementedException(Format.ToString());
        }

        public static bool HasDepth(GalImageFormat Format)
        {
            switch (Format)
            {
                case GalImageFormat.D24_UNORM_S8_UINT:
                case GalImageFormat.D32_SFLOAT:
                case GalImageFormat.D16_UNORM:
                case GalImageFormat.D32_SFLOAT_S8_UINT:
                    return true;
            }

            //Depth formats are fewer than colors, so it's harder to miss one
            //Instead of checking for individual formats, return false
            return false;
        }

        public static bool HasStencil(GalImageFormat Format)
        {
            switch (Format)
            {
                case GalImageFormat.D24_UNORM_S8_UINT:
                case GalImageFormat.D32_SFLOAT_S8_UINT:
                    return true;
            }

            return false;
        }
    }
}