1 /**************************************************************************
2  *
3  * Copyright 2012-2021 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  **************************************************************************/
27 
28 /*
29  * Format.h --
30  *    Functions for format manipulation.
31  */
32 
33 
34 #include <stdint.h>
35 
36 #include "Format.h"
37 #include "State.h"
38 
39 #include "Debug.h"
40 
41 
42 /* XXX: for a time llvmpipe didn't support SRGB */
43 #define HAVE_SRGB 1
44 
45 
46 enum pipe_format
FormatTranslate(DXGI_FORMAT Format,BOOL depth)47 FormatTranslate(DXGI_FORMAT Format, BOOL depth)
48 {
49    switch (Format) {
50 
51    case DXGI_FORMAT_UNKNOWN:
52       return PIPE_FORMAT_NONE;
53 
54    case DXGI_FORMAT_R32G32B32A32_FLOAT:
55       return PIPE_FORMAT_R32G32B32A32_FLOAT;
56    case DXGI_FORMAT_R32G32B32A32_UINT:
57       return PIPE_FORMAT_R32G32B32A32_UINT;
58    case DXGI_FORMAT_R32G32B32A32_SINT:
59       return PIPE_FORMAT_R32G32B32A32_SINT;
60    case DXGI_FORMAT_R32G32B32_FLOAT:
61       return PIPE_FORMAT_R32G32B32_FLOAT;
62    case DXGI_FORMAT_R32G32B32_UINT:
63       return PIPE_FORMAT_R32G32B32_UINT;
64    case DXGI_FORMAT_R32G32B32_SINT:
65       return PIPE_FORMAT_R32G32B32_SINT;
66    case DXGI_FORMAT_R16G16B16A16_UNORM:
67       return PIPE_FORMAT_R16G16B16A16_UNORM;
68    case DXGI_FORMAT_R16G16B16A16_UINT:
69       return PIPE_FORMAT_R16G16B16A16_UINT;
70    case DXGI_FORMAT_R16G16B16A16_SNORM:
71       return PIPE_FORMAT_R16G16B16A16_SNORM;
72    case DXGI_FORMAT_R16G16B16A16_SINT:
73       return PIPE_FORMAT_R16G16B16A16_SINT;
74    case DXGI_FORMAT_R32G32_FLOAT:
75       return PIPE_FORMAT_R32G32_FLOAT;
76    case DXGI_FORMAT_R32G32_UINT:
77       return PIPE_FORMAT_R32G32_UINT;
78    case DXGI_FORMAT_R32G32_SINT:
79       return PIPE_FORMAT_R32G32_SINT;
80    case DXGI_FORMAT_R8G8B8A8_UNORM:
81       return PIPE_FORMAT_R8G8B8A8_UNORM;
82    case DXGI_FORMAT_R8G8B8A8_UINT:
83       return PIPE_FORMAT_R8G8B8A8_UINT;
84    case DXGI_FORMAT_R8G8B8A8_SNORM:
85       return PIPE_FORMAT_R8G8B8A8_SNORM;
86    case DXGI_FORMAT_R8G8B8A8_SINT:
87       return PIPE_FORMAT_R8G8B8A8_SINT;
88    case DXGI_FORMAT_R16G16_UNORM:
89       return PIPE_FORMAT_R16G16_UNORM;
90    case DXGI_FORMAT_R16G16_UINT:
91       return PIPE_FORMAT_R16G16_UINT;
92    case DXGI_FORMAT_R16G16_SNORM:
93       return PIPE_FORMAT_R16G16_SNORM;
94    case DXGI_FORMAT_R16G16_SINT:
95       return PIPE_FORMAT_R16G16_SINT;
96    case DXGI_FORMAT_D32_FLOAT:
97       return PIPE_FORMAT_Z32_FLOAT;
98    case DXGI_FORMAT_R32_FLOAT:
99       return PIPE_FORMAT_R32_FLOAT;
100    case DXGI_FORMAT_R32_UINT:
101       return PIPE_FORMAT_R32_UINT;
102    case DXGI_FORMAT_R32_SINT:
103       return PIPE_FORMAT_R32_SINT;
104    case DXGI_FORMAT_D24_UNORM_S8_UINT:
105       return PIPE_FORMAT_Z24_UNORM_S8_UINT;
106    case DXGI_FORMAT_R8G8_UNORM:
107       return PIPE_FORMAT_R8G8_UNORM;
108    case DXGI_FORMAT_R8G8_UINT:
109       return PIPE_FORMAT_R8G8_UINT;
110    case DXGI_FORMAT_R8G8_SNORM:
111       return PIPE_FORMAT_R8G8_SNORM;
112    case DXGI_FORMAT_R8G8_SINT:
113       return PIPE_FORMAT_R8G8_SINT;
114    case DXGI_FORMAT_D16_UNORM:
115       return PIPE_FORMAT_Z16_UNORM;
116    case DXGI_FORMAT_R16_UNORM:
117       return PIPE_FORMAT_R16_UNORM;
118    case DXGI_FORMAT_R16_UINT:
119       return PIPE_FORMAT_R16_UINT;
120    case DXGI_FORMAT_R16_SNORM:
121       return PIPE_FORMAT_R16_SNORM;
122    case DXGI_FORMAT_R16_SINT:
123       return PIPE_FORMAT_R16_SINT;
124    case DXGI_FORMAT_R8_UNORM:
125       return PIPE_FORMAT_R8_UNORM;
126    case DXGI_FORMAT_R8_UINT:
127       return PIPE_FORMAT_R8_UINT;
128    case DXGI_FORMAT_R8_SNORM:
129       return PIPE_FORMAT_R8_SNORM;
130    case DXGI_FORMAT_R8_SINT:
131       return PIPE_FORMAT_R8_SINT;
132    case DXGI_FORMAT_A8_UNORM:
133       return PIPE_FORMAT_A8_UNORM;
134    case DXGI_FORMAT_BC1_UNORM:
135       return PIPE_FORMAT_DXT1_RGBA;
136    case DXGI_FORMAT_BC1_UNORM_SRGB:
137 #if HAVE_SRGB
138       return PIPE_FORMAT_DXT1_SRGBA;
139 #else
140       return PIPE_FORMAT_DXT1_RGBA;
141 #endif
142    case DXGI_FORMAT_BC2_UNORM:
143       return PIPE_FORMAT_DXT3_RGBA;
144    case DXGI_FORMAT_BC2_UNORM_SRGB:
145 #if HAVE_SRGB
146       return PIPE_FORMAT_DXT3_SRGBA;
147 #else
148       return PIPE_FORMAT_DXT3_RGBA;
149 #endif
150    case DXGI_FORMAT_BC3_UNORM:
151       return PIPE_FORMAT_DXT5_RGBA;
152    case DXGI_FORMAT_BC3_UNORM_SRGB:
153 #if HAVE_SRGB
154       return PIPE_FORMAT_DXT5_SRGBA;
155 #else
156       return PIPE_FORMAT_DXT5_RGBA;
157 #endif
158    case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
159 #if HAVE_SRGB
160       return PIPE_FORMAT_R8G8B8A8_SRGB;
161 #else
162       return PIPE_FORMAT_R8G8B8A8_UNORM;
163 #endif
164 
165    /* Gallium has no typeless formats.  We simply use an equivalent
166     * typed format.
167      */
168    case DXGI_FORMAT_R8G8B8A8_TYPELESS:
169       return PIPE_FORMAT_R8G8B8A8_UNORM;
170    case DXGI_FORMAT_BC1_TYPELESS:
171       return PIPE_FORMAT_DXT1_RGBA;
172    case DXGI_FORMAT_BC2_TYPELESS:
173       return PIPE_FORMAT_DXT3_RGBA;
174    case DXGI_FORMAT_BC3_TYPELESS:
175       return PIPE_FORMAT_DXT5_RGBA;
176    case DXGI_FORMAT_R8_TYPELESS:
177       return PIPE_FORMAT_R8_UNORM;
178    case DXGI_FORMAT_R32G32B32A32_TYPELESS:
179       return PIPE_FORMAT_R32G32B32A32_UNORM;
180    case DXGI_FORMAT_R32G32B32_TYPELESS:
181       return PIPE_FORMAT_R32G32B32_UNORM;
182    case DXGI_FORMAT_R16G16B16A16_TYPELESS:
183       return PIPE_FORMAT_R16G16B16A16_UNORM;
184    case DXGI_FORMAT_R32G32_TYPELESS:
185       return PIPE_FORMAT_R32G32_UNORM;
186    case DXGI_FORMAT_R8G8_TYPELESS:
187       return PIPE_FORMAT_R8G8_UNORM;
188    case DXGI_FORMAT_R16_TYPELESS:
189       return depth ? PIPE_FORMAT_Z16_UNORM : PIPE_FORMAT_R16_UNORM;
190    case DXGI_FORMAT_R16G16_TYPELESS:
191       return PIPE_FORMAT_R16G16_UNORM;
192    case DXGI_FORMAT_R32_TYPELESS:
193       return depth ? PIPE_FORMAT_Z32_FLOAT : PIPE_FORMAT_R32_FLOAT;
194    case DXGI_FORMAT_R24G8_TYPELESS:
195       return PIPE_FORMAT_Z24_UNORM_S8_UINT;
196    case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
197       return PIPE_FORMAT_Z24X8_UNORM;
198    case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
199       return PIPE_FORMAT_Z24_UNORM_S8_UINT;
200 
201    case DXGI_FORMAT_R8G8_B8G8_UNORM:
202       return PIPE_FORMAT_R8G8_B8G8_UNORM;
203    case DXGI_FORMAT_G8R8_G8B8_UNORM:
204       return PIPE_FORMAT_G8R8_G8B8_UNORM;
205 
206    case DXGI_FORMAT_R16G16B16A16_FLOAT:
207       return PIPE_FORMAT_R16G16B16A16_FLOAT;
208    case DXGI_FORMAT_R16G16_FLOAT:
209       return PIPE_FORMAT_R16G16_FLOAT;
210    case DXGI_FORMAT_R16_FLOAT:
211       return PIPE_FORMAT_R16_FLOAT;
212    case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
213       return PIPE_FORMAT_R9G9B9E5_FLOAT;
214    case DXGI_FORMAT_R1_UNORM:
215       return PIPE_FORMAT_R1_UNORM;
216 
217    case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
218    case DXGI_FORMAT_R32G8X24_TYPELESS:
219    case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
220    case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
221       return PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
222 
223    case DXGI_FORMAT_R10G10B10A2_TYPELESS:
224    case DXGI_FORMAT_R10G10B10A2_UNORM:
225        return PIPE_FORMAT_R10G10B10A2_UNORM;
226    case DXGI_FORMAT_R10G10B10A2_UINT:
227       return PIPE_FORMAT_R10G10B10A2_UINT;
228    case DXGI_FORMAT_R11G11B10_FLOAT:
229       return PIPE_FORMAT_R11G11B10_FLOAT;
230 
231    case DXGI_FORMAT_BC4_TYPELESS:
232    case DXGI_FORMAT_BC4_UNORM:
233       return PIPE_FORMAT_RGTC1_UNORM;
234    case DXGI_FORMAT_BC4_SNORM:
235       return PIPE_FORMAT_RGTC1_SNORM;
236 
237    case DXGI_FORMAT_BC5_TYPELESS:
238    case DXGI_FORMAT_BC5_UNORM:
239       return PIPE_FORMAT_RGTC2_UNORM;
240    case DXGI_FORMAT_BC5_SNORM:
241       return PIPE_FORMAT_RGTC2_SNORM;
242 
243    case DXGI_FORMAT_B5G6R5_UNORM:
244       return PIPE_FORMAT_B5G6R5_UNORM;
245    case DXGI_FORMAT_B5G5R5A1_UNORM:
246       return PIPE_FORMAT_B5G5R5A1_UNORM;
247    case DXGI_FORMAT_B8G8R8A8_UNORM:
248       return PIPE_FORMAT_B8G8R8A8_UNORM;
249    case DXGI_FORMAT_B8G8R8X8_UNORM:
250       return PIPE_FORMAT_B8G8R8X8_UNORM;
251    case DXGI_FORMAT_B8G8R8A8_TYPELESS:
252       return PIPE_FORMAT_B8G8R8A8_UNORM;
253    case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
254 #if HAVE_SRGB
255       return PIPE_FORMAT_B8G8R8A8_SRGB;
256 #else
257       return PIPE_FORMAT_B8G8R8A8_UNORM;
258 #endif
259    case DXGI_FORMAT_B8G8R8X8_TYPELESS:
260       return PIPE_FORMAT_B8G8R8X8_UNORM;
261    case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
262 #if HAVE_SRGB
263       return PIPE_FORMAT_B8G8R8X8_SRGB;
264 #else
265       return PIPE_FORMAT_B8G8R8X8_UNORM;
266 #endif
267 
268    case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM:
269       return PIPE_FORMAT_R10G10B10A2_UNORM;
270 
271    default:
272       DebugPrintf("%s: unsupported format %s\n",
273                   __FUNCTION__, FormatToName(Format));
274       return PIPE_FORMAT_NONE;
275    }
276 }
277 
278 
279 
280 #define CASE(fmt) case fmt: return #fmt
281 
282 const char *
FormatToName(DXGI_FORMAT Format)283 FormatToName(DXGI_FORMAT Format)
284 {
285    switch (Format) {
286    CASE(DXGI_FORMAT_UNKNOWN);
287    CASE(DXGI_FORMAT_R32G32B32A32_TYPELESS);
288    CASE(DXGI_FORMAT_R32G32B32A32_FLOAT);
289    CASE(DXGI_FORMAT_R32G32B32A32_UINT);
290    CASE(DXGI_FORMAT_R32G32B32A32_SINT);
291    CASE(DXGI_FORMAT_R32G32B32_TYPELESS);
292    CASE(DXGI_FORMAT_R32G32B32_FLOAT);
293    CASE(DXGI_FORMAT_R32G32B32_UINT);
294    CASE(DXGI_FORMAT_R32G32B32_SINT);
295    CASE(DXGI_FORMAT_R16G16B16A16_TYPELESS);
296    CASE(DXGI_FORMAT_R16G16B16A16_FLOAT);
297    CASE(DXGI_FORMAT_R16G16B16A16_UNORM);
298    CASE(DXGI_FORMAT_R16G16B16A16_UINT);
299    CASE(DXGI_FORMAT_R16G16B16A16_SNORM);
300    CASE(DXGI_FORMAT_R16G16B16A16_SINT);
301    CASE(DXGI_FORMAT_R32G32_TYPELESS);
302    CASE(DXGI_FORMAT_R32G32_FLOAT);
303    CASE(DXGI_FORMAT_R32G32_UINT);
304    CASE(DXGI_FORMAT_R32G32_SINT);
305    CASE(DXGI_FORMAT_R32G8X24_TYPELESS);
306    CASE(DXGI_FORMAT_D32_FLOAT_S8X24_UINT);
307    CASE(DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS);
308    CASE(DXGI_FORMAT_X32_TYPELESS_G8X24_UINT);
309    CASE(DXGI_FORMAT_R10G10B10A2_TYPELESS);
310    CASE(DXGI_FORMAT_R10G10B10A2_UNORM);
311    CASE(DXGI_FORMAT_R10G10B10A2_UINT);
312    CASE(DXGI_FORMAT_R11G11B10_FLOAT);
313    CASE(DXGI_FORMAT_R8G8B8A8_TYPELESS);
314    CASE(DXGI_FORMAT_R8G8B8A8_UNORM);
315    CASE(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB);
316    CASE(DXGI_FORMAT_R8G8B8A8_UINT);
317    CASE(DXGI_FORMAT_R8G8B8A8_SNORM);
318    CASE(DXGI_FORMAT_R8G8B8A8_SINT);
319    CASE(DXGI_FORMAT_R16G16_TYPELESS);
320    CASE(DXGI_FORMAT_R16G16_FLOAT);
321    CASE(DXGI_FORMAT_R16G16_UNORM);
322    CASE(DXGI_FORMAT_R16G16_UINT);
323    CASE(DXGI_FORMAT_R16G16_SNORM);
324    CASE(DXGI_FORMAT_R16G16_SINT);
325    CASE(DXGI_FORMAT_R32_TYPELESS);
326    CASE(DXGI_FORMAT_D32_FLOAT);
327    CASE(DXGI_FORMAT_R32_FLOAT);
328    CASE(DXGI_FORMAT_R32_UINT);
329    CASE(DXGI_FORMAT_R32_SINT);
330    CASE(DXGI_FORMAT_R24G8_TYPELESS);
331    CASE(DXGI_FORMAT_D24_UNORM_S8_UINT);
332    CASE(DXGI_FORMAT_R24_UNORM_X8_TYPELESS);
333    CASE(DXGI_FORMAT_X24_TYPELESS_G8_UINT);
334    CASE(DXGI_FORMAT_R8G8_TYPELESS);
335    CASE(DXGI_FORMAT_R8G8_UNORM);
336    CASE(DXGI_FORMAT_R8G8_UINT);
337    CASE(DXGI_FORMAT_R8G8_SNORM);
338    CASE(DXGI_FORMAT_R8G8_SINT);
339    CASE(DXGI_FORMAT_R16_TYPELESS);
340    CASE(DXGI_FORMAT_R16_FLOAT);
341    CASE(DXGI_FORMAT_D16_UNORM);
342    CASE(DXGI_FORMAT_R16_UNORM);
343    CASE(DXGI_FORMAT_R16_UINT);
344    CASE(DXGI_FORMAT_R16_SNORM);
345    CASE(DXGI_FORMAT_R16_SINT);
346    CASE(DXGI_FORMAT_R8_TYPELESS);
347    CASE(DXGI_FORMAT_R8_UNORM);
348    CASE(DXGI_FORMAT_R8_UINT);
349    CASE(DXGI_FORMAT_R8_SNORM);
350    CASE(DXGI_FORMAT_R8_SINT);
351    CASE(DXGI_FORMAT_A8_UNORM);
352    CASE(DXGI_FORMAT_R1_UNORM);
353    CASE(DXGI_FORMAT_R9G9B9E5_SHAREDEXP);
354    CASE(DXGI_FORMAT_R8G8_B8G8_UNORM);
355    CASE(DXGI_FORMAT_G8R8_G8B8_UNORM);
356    CASE(DXGI_FORMAT_BC1_TYPELESS);
357    CASE(DXGI_FORMAT_BC1_UNORM);
358    CASE(DXGI_FORMAT_BC1_UNORM_SRGB);
359    CASE(DXGI_FORMAT_BC2_TYPELESS);
360    CASE(DXGI_FORMAT_BC2_UNORM);
361    CASE(DXGI_FORMAT_BC2_UNORM_SRGB);
362    CASE(DXGI_FORMAT_BC3_TYPELESS);
363    CASE(DXGI_FORMAT_BC3_UNORM);
364    CASE(DXGI_FORMAT_BC3_UNORM_SRGB);
365    CASE(DXGI_FORMAT_BC4_TYPELESS);
366    CASE(DXGI_FORMAT_BC4_UNORM);
367    CASE(DXGI_FORMAT_BC4_SNORM);
368    CASE(DXGI_FORMAT_BC5_TYPELESS);
369    CASE(DXGI_FORMAT_BC5_UNORM);
370    CASE(DXGI_FORMAT_BC5_SNORM);
371    CASE(DXGI_FORMAT_B5G6R5_UNORM);
372    CASE(DXGI_FORMAT_B5G5R5A1_UNORM);
373    CASE(DXGI_FORMAT_B8G8R8A8_UNORM);
374    CASE(DXGI_FORMAT_B8G8R8X8_UNORM);
375    CASE(DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM);
376    CASE(DXGI_FORMAT_B8G8R8A8_TYPELESS);
377    CASE(DXGI_FORMAT_B8G8R8A8_UNORM_SRGB);
378    CASE(DXGI_FORMAT_B8G8R8X8_TYPELESS);
379    CASE(DXGI_FORMAT_B8G8R8X8_UNORM_SRGB);
380    CASE(DXGI_FORMAT_BC6H_TYPELESS);
381    CASE(DXGI_FORMAT_BC6H_UF16);
382    CASE(DXGI_FORMAT_BC6H_SF16);
383    CASE(DXGI_FORMAT_BC7_TYPELESS);
384    CASE(DXGI_FORMAT_BC7_UNORM);
385    CASE(DXGI_FORMAT_BC7_UNORM_SRGB);
386    default:
387       return "???";
388    }
389 }
390