• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * Copyright (c) 2008-2010 VMware, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 
29 
30 /**
31  * Mesa / Gallium format conversion and format selection code.
32  * \author Brian Paul
33  */
34 
35 #include "main/imports.h"
36 #include "main/context.h"
37 #include "main/enums.h"
38 #include "main/formats.h"
39 #include "main/glformats.h"
40 #include "main/texcompress.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texstore.h"
44 #include "main/image.h"
45 #include "main/macros.h"
46 #include "main/formatquery.h"
47 
48 #include "pipe/p_context.h"
49 #include "pipe/p_defines.h"
50 #include "pipe/p_screen.h"
51 #include "util/u_format.h"
52 #include "st_cb_texture.h"
53 #include "st_context.h"
54 #include "st_format.h"
55 
56 
57 /**
58  * Translate Mesa format to Gallium format.
59  */
60 enum pipe_format
st_mesa_format_to_pipe_format(const struct st_context * st,mesa_format mesaFormat)61 st_mesa_format_to_pipe_format(const struct st_context *st,
62                               mesa_format mesaFormat)
63 {
64    switch (mesaFormat) {
65    case MESA_FORMAT_A8B8G8R8_UNORM:
66       return PIPE_FORMAT_ABGR8888_UNORM;
67    case MESA_FORMAT_R8G8B8A8_UNORM:
68       return PIPE_FORMAT_RGBA8888_UNORM;
69    case MESA_FORMAT_B8G8R8A8_UNORM:
70       return PIPE_FORMAT_BGRA8888_UNORM;
71    case MESA_FORMAT_A8R8G8B8_UNORM:
72       return PIPE_FORMAT_ARGB8888_UNORM;
73    case MESA_FORMAT_X8B8G8R8_UNORM:
74       return PIPE_FORMAT_XBGR8888_UNORM;
75    case MESA_FORMAT_R8G8B8X8_UNORM:
76       return PIPE_FORMAT_RGBX8888_UNORM;
77    case MESA_FORMAT_B8G8R8X8_UNORM:
78       return PIPE_FORMAT_BGRX8888_UNORM;
79    case MESA_FORMAT_X8R8G8B8_UNORM:
80       return PIPE_FORMAT_XRGB8888_UNORM;
81    case MESA_FORMAT_B5G5R5A1_UNORM:
82       return PIPE_FORMAT_B5G5R5A1_UNORM;
83    case MESA_FORMAT_A1B5G5R5_UNORM:
84       return PIPE_FORMAT_A1B5G5R5_UNORM;
85    case MESA_FORMAT_B4G4R4A4_UNORM:
86       return PIPE_FORMAT_B4G4R4A4_UNORM;
87    case MESA_FORMAT_B5G6R5_UNORM:
88       return PIPE_FORMAT_B5G6R5_UNORM;
89    case MESA_FORMAT_B2G3R3_UNORM:
90       return PIPE_FORMAT_B2G3R3_UNORM;
91    case MESA_FORMAT_B10G10R10A2_UNORM:
92       return PIPE_FORMAT_B10G10R10A2_UNORM;
93    case MESA_FORMAT_R10G10B10A2_UNORM:
94       return PIPE_FORMAT_R10G10B10A2_UNORM;
95    case MESA_FORMAT_R10G10B10X2_UNORM:
96       return PIPE_FORMAT_R10G10B10X2_UNORM;
97    case MESA_FORMAT_L4A4_UNORM:
98       return PIPE_FORMAT_L4A4_UNORM;
99    case MESA_FORMAT_L8A8_UNORM:
100       return PIPE_FORMAT_LA88_UNORM;
101    case MESA_FORMAT_A8L8_UNORM:
102       return PIPE_FORMAT_AL88_UNORM;
103    case MESA_FORMAT_L16A16_UNORM:
104       return PIPE_FORMAT_LA1616_UNORM;
105    case MESA_FORMAT_A16L16_UNORM:
106       return PIPE_FORMAT_AL1616_UNORM;
107    case MESA_FORMAT_A_UNORM8:
108       return PIPE_FORMAT_A8_UNORM;
109    case MESA_FORMAT_A_UNORM16:
110       return PIPE_FORMAT_A16_UNORM;
111    case MESA_FORMAT_L_UNORM8:
112       return PIPE_FORMAT_L8_UNORM;
113    case MESA_FORMAT_L_UNORM16:
114       return PIPE_FORMAT_L16_UNORM;
115    case MESA_FORMAT_I_UNORM8:
116       return PIPE_FORMAT_I8_UNORM;
117    case MESA_FORMAT_I_UNORM16:
118       return PIPE_FORMAT_I16_UNORM;
119    case MESA_FORMAT_Z_UNORM16:
120       return PIPE_FORMAT_Z16_UNORM;
121    case MESA_FORMAT_Z_UNORM32:
122       return PIPE_FORMAT_Z32_UNORM;
123    case MESA_FORMAT_S8_UINT_Z24_UNORM:
124       return PIPE_FORMAT_S8_UINT_Z24_UNORM;
125    case MESA_FORMAT_Z24_UNORM_S8_UINT:
126       return PIPE_FORMAT_Z24_UNORM_S8_UINT;
127    case MESA_FORMAT_X8_UINT_Z24_UNORM:
128       return PIPE_FORMAT_X8Z24_UNORM;
129    case MESA_FORMAT_Z24_UNORM_X8_UINT:
130       return PIPE_FORMAT_Z24X8_UNORM;
131    case MESA_FORMAT_S_UINT8:
132       return PIPE_FORMAT_S8_UINT;
133    case MESA_FORMAT_Z_FLOAT32:
134       return PIPE_FORMAT_Z32_FLOAT;
135    case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
136       return PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
137    case MESA_FORMAT_YCBCR:
138       return PIPE_FORMAT_UYVY;
139    case MESA_FORMAT_YCBCR_REV:
140       return PIPE_FORMAT_YUYV;
141    case MESA_FORMAT_RGB_DXT1:
142       return PIPE_FORMAT_DXT1_RGB;
143    case MESA_FORMAT_RGBA_DXT1:
144       return PIPE_FORMAT_DXT1_RGBA;
145    case MESA_FORMAT_RGBA_DXT3:
146       return PIPE_FORMAT_DXT3_RGBA;
147    case MESA_FORMAT_RGBA_DXT5:
148       return PIPE_FORMAT_DXT5_RGBA;
149    case MESA_FORMAT_SRGB_DXT1:
150       return PIPE_FORMAT_DXT1_SRGB;
151    case MESA_FORMAT_SRGBA_DXT1:
152       return PIPE_FORMAT_DXT1_SRGBA;
153    case MESA_FORMAT_SRGBA_DXT3:
154       return PIPE_FORMAT_DXT3_SRGBA;
155    case MESA_FORMAT_SRGBA_DXT5:
156       return PIPE_FORMAT_DXT5_SRGBA;
157    case MESA_FORMAT_L8A8_SRGB:
158       return PIPE_FORMAT_LA88_SRGB;
159    case MESA_FORMAT_A8L8_SRGB:
160       return PIPE_FORMAT_AL88_SRGB;
161    case MESA_FORMAT_L_SRGB8:
162       return PIPE_FORMAT_L8_SRGB;
163    case MESA_FORMAT_BGR_SRGB8:
164       return PIPE_FORMAT_R8G8B8_SRGB;
165    case MESA_FORMAT_A8B8G8R8_SRGB:
166       return PIPE_FORMAT_ABGR8888_SRGB;
167    case MESA_FORMAT_R8G8B8A8_SRGB:
168       return PIPE_FORMAT_RGBA8888_SRGB;
169    case MESA_FORMAT_B8G8R8A8_SRGB:
170       return PIPE_FORMAT_BGRA8888_SRGB;
171    case MESA_FORMAT_A8R8G8B8_SRGB:
172       return PIPE_FORMAT_ARGB8888_SRGB;
173    case MESA_FORMAT_RGBA_FLOAT32:
174       return PIPE_FORMAT_R32G32B32A32_FLOAT;
175    case MESA_FORMAT_RGBA_FLOAT16:
176       return PIPE_FORMAT_R16G16B16A16_FLOAT;
177    case MESA_FORMAT_RGB_FLOAT32:
178       return PIPE_FORMAT_R32G32B32_FLOAT;
179    case MESA_FORMAT_RGB_FLOAT16:
180       return PIPE_FORMAT_R16G16B16_FLOAT;
181    case MESA_FORMAT_LA_FLOAT32:
182       return PIPE_FORMAT_L32A32_FLOAT;
183    case MESA_FORMAT_LA_FLOAT16:
184       return PIPE_FORMAT_L16A16_FLOAT;
185    case MESA_FORMAT_L_FLOAT32:
186       return PIPE_FORMAT_L32_FLOAT;
187    case MESA_FORMAT_L_FLOAT16:
188       return PIPE_FORMAT_L16_FLOAT;
189    case MESA_FORMAT_A_FLOAT32:
190       return PIPE_FORMAT_A32_FLOAT;
191    case MESA_FORMAT_A_FLOAT16:
192       return PIPE_FORMAT_A16_FLOAT;
193    case MESA_FORMAT_I_FLOAT32:
194       return PIPE_FORMAT_I32_FLOAT;
195    case MESA_FORMAT_I_FLOAT16:
196       return PIPE_FORMAT_I16_FLOAT;
197    case MESA_FORMAT_R_FLOAT32:
198       return PIPE_FORMAT_R32_FLOAT;
199    case MESA_FORMAT_R_FLOAT16:
200       return PIPE_FORMAT_R16_FLOAT;
201    case MESA_FORMAT_RG_FLOAT32:
202       return PIPE_FORMAT_R32G32_FLOAT;
203    case MESA_FORMAT_RG_FLOAT16:
204       return PIPE_FORMAT_R16G16_FLOAT;
205 
206    case MESA_FORMAT_R_UNORM8:
207       return PIPE_FORMAT_R8_UNORM;
208    case MESA_FORMAT_R_UNORM16:
209       return PIPE_FORMAT_R16_UNORM;
210    case MESA_FORMAT_R8G8_UNORM:
211       return PIPE_FORMAT_RG88_UNORM;
212    case MESA_FORMAT_G8R8_UNORM:
213       return PIPE_FORMAT_GR88_UNORM;
214    case MESA_FORMAT_R16G16_UNORM:
215       return PIPE_FORMAT_RG1616_UNORM;
216    case MESA_FORMAT_G16R16_UNORM:
217       return PIPE_FORMAT_GR1616_UNORM;
218    case MESA_FORMAT_RGBA_UNORM16:
219       return PIPE_FORMAT_R16G16B16A16_UNORM;
220 
221    /* signed int formats */
222    case MESA_FORMAT_A_UINT8:
223       return PIPE_FORMAT_A8_UINT;
224    case MESA_FORMAT_A_UINT16:
225       return PIPE_FORMAT_A16_UINT;
226    case MESA_FORMAT_A_UINT32:
227       return PIPE_FORMAT_A32_UINT;
228 
229    case MESA_FORMAT_A_SINT8:
230       return PIPE_FORMAT_A8_SINT;
231    case MESA_FORMAT_A_SINT16:
232       return PIPE_FORMAT_A16_SINT;
233    case MESA_FORMAT_A_SINT32:
234       return PIPE_FORMAT_A32_SINT;
235 
236    case MESA_FORMAT_I_UINT8:
237       return PIPE_FORMAT_I8_UINT;
238    case MESA_FORMAT_I_UINT16:
239       return PIPE_FORMAT_I16_UINT;
240    case MESA_FORMAT_I_UINT32:
241       return PIPE_FORMAT_I32_UINT;
242 
243    case MESA_FORMAT_I_SINT8:
244       return PIPE_FORMAT_I8_SINT;
245    case MESA_FORMAT_I_SINT16:
246       return PIPE_FORMAT_I16_SINT;
247    case MESA_FORMAT_I_SINT32:
248       return PIPE_FORMAT_I32_SINT;
249 
250    case MESA_FORMAT_L_UINT8:
251       return PIPE_FORMAT_L8_UINT;
252    case MESA_FORMAT_L_UINT16:
253       return PIPE_FORMAT_L16_UINT;
254    case MESA_FORMAT_L_UINT32:
255       return PIPE_FORMAT_L32_UINT;
256 
257    case MESA_FORMAT_L_SINT8:
258       return PIPE_FORMAT_L8_SINT;
259    case MESA_FORMAT_L_SINT16:
260       return PIPE_FORMAT_L16_SINT;
261    case MESA_FORMAT_L_SINT32:
262       return PIPE_FORMAT_L32_SINT;
263 
264    case MESA_FORMAT_LA_UINT8:
265       return PIPE_FORMAT_L8A8_UINT;
266    case MESA_FORMAT_LA_UINT16:
267       return PIPE_FORMAT_L16A16_UINT;
268    case MESA_FORMAT_LA_UINT32:
269       return PIPE_FORMAT_L32A32_UINT;
270 
271    case MESA_FORMAT_LA_SINT8:
272       return PIPE_FORMAT_L8A8_SINT;
273    case MESA_FORMAT_LA_SINT16:
274       return PIPE_FORMAT_L16A16_SINT;
275    case MESA_FORMAT_LA_SINT32:
276       return PIPE_FORMAT_L32A32_SINT;
277 
278    case MESA_FORMAT_R_SINT8:
279       return PIPE_FORMAT_R8_SINT;
280    case MESA_FORMAT_RG_SINT8:
281       return PIPE_FORMAT_R8G8_SINT;
282    case MESA_FORMAT_RGB_SINT8:
283       return PIPE_FORMAT_R8G8B8_SINT;
284    case MESA_FORMAT_RGBA_SINT8:
285       return PIPE_FORMAT_R8G8B8A8_SINT;
286    case MESA_FORMAT_R_SINT16:
287       return PIPE_FORMAT_R16_SINT;
288    case MESA_FORMAT_RG_SINT16:
289       return PIPE_FORMAT_R16G16_SINT;
290    case MESA_FORMAT_RGB_SINT16:
291       return PIPE_FORMAT_R16G16B16_SINT;
292    case MESA_FORMAT_RGBA_SINT16:
293       return PIPE_FORMAT_R16G16B16A16_SINT;
294    case MESA_FORMAT_R_SINT32:
295       return PIPE_FORMAT_R32_SINT;
296    case MESA_FORMAT_RG_SINT32:
297       return PIPE_FORMAT_R32G32_SINT;
298    case MESA_FORMAT_RGB_SINT32:
299       return PIPE_FORMAT_R32G32B32_SINT;
300    case MESA_FORMAT_RGBA_SINT32:
301       return PIPE_FORMAT_R32G32B32A32_SINT;
302 
303    /* unsigned int formats */
304    case MESA_FORMAT_R_UINT8:
305       return PIPE_FORMAT_R8_UINT;
306    case MESA_FORMAT_RG_UINT8:
307       return PIPE_FORMAT_R8G8_UINT;
308    case MESA_FORMAT_RGB_UINT8:
309       return PIPE_FORMAT_R8G8B8_UINT;
310    case MESA_FORMAT_RGBA_UINT8:
311       return PIPE_FORMAT_R8G8B8A8_UINT;
312    case MESA_FORMAT_R_UINT16:
313       return PIPE_FORMAT_R16_UINT;
314    case MESA_FORMAT_RG_UINT16:
315       return PIPE_FORMAT_R16G16_UINT;
316    case MESA_FORMAT_RGB_UINT16:
317       return PIPE_FORMAT_R16G16B16_UINT;
318    case MESA_FORMAT_RGBA_UINT16:
319       return PIPE_FORMAT_R16G16B16A16_UINT;
320    case MESA_FORMAT_R_UINT32:
321       return PIPE_FORMAT_R32_UINT;
322    case MESA_FORMAT_RG_UINT32:
323       return PIPE_FORMAT_R32G32_UINT;
324    case MESA_FORMAT_RGB_UINT32:
325       return PIPE_FORMAT_R32G32B32_UINT;
326    case MESA_FORMAT_RGBA_UINT32:
327       return PIPE_FORMAT_R32G32B32A32_UINT;
328 
329    case MESA_FORMAT_R_RGTC1_UNORM:
330       return PIPE_FORMAT_RGTC1_UNORM;
331    case MESA_FORMAT_R_RGTC1_SNORM:
332       return PIPE_FORMAT_RGTC1_SNORM;
333    case MESA_FORMAT_RG_RGTC2_UNORM:
334       return PIPE_FORMAT_RGTC2_UNORM;
335    case MESA_FORMAT_RG_RGTC2_SNORM:
336       return PIPE_FORMAT_RGTC2_SNORM;
337 
338    case MESA_FORMAT_L_LATC1_UNORM:
339       return PIPE_FORMAT_LATC1_UNORM;
340    case MESA_FORMAT_L_LATC1_SNORM:
341       return PIPE_FORMAT_LATC1_SNORM;
342    case MESA_FORMAT_LA_LATC2_UNORM:
343       return PIPE_FORMAT_LATC2_UNORM;
344    case MESA_FORMAT_LA_LATC2_SNORM:
345       return PIPE_FORMAT_LATC2_SNORM;
346 
347    /* The destination RGBA format mustn't be changed, because it's also
348     * a destination format of the unpack/decompression function. */
349    case MESA_FORMAT_ETC1_RGB8:
350       return st->has_etc1 ? PIPE_FORMAT_ETC1_RGB8 : PIPE_FORMAT_R8G8B8A8_UNORM;
351 
352    case MESA_FORMAT_BPTC_RGBA_UNORM:
353       return PIPE_FORMAT_BPTC_RGBA_UNORM;
354    case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
355       return PIPE_FORMAT_BPTC_SRGBA;
356    case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
357       return PIPE_FORMAT_BPTC_RGB_FLOAT;
358    case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
359       return PIPE_FORMAT_BPTC_RGB_UFLOAT;
360 
361    /* signed normalized formats */
362    case MESA_FORMAT_R_SNORM8:
363       return PIPE_FORMAT_R8_SNORM;
364    case MESA_FORMAT_R8G8_SNORM:
365       return PIPE_FORMAT_RG88_SNORM;
366    case MESA_FORMAT_G8R8_SNORM:
367       return PIPE_FORMAT_GR88_SNORM;
368    case MESA_FORMAT_R8G8B8A8_SNORM:
369       return PIPE_FORMAT_RGBA8888_SNORM;
370    case MESA_FORMAT_A8B8G8R8_SNORM:
371       return PIPE_FORMAT_ABGR8888_SNORM;
372 
373    case MESA_FORMAT_A_SNORM8:
374       return PIPE_FORMAT_A8_SNORM;
375    case MESA_FORMAT_L_SNORM8:
376       return PIPE_FORMAT_L8_SNORM;
377    case MESA_FORMAT_L8A8_SNORM:
378       return PIPE_FORMAT_LA88_SNORM;
379    case MESA_FORMAT_A8L8_SNORM:
380       return PIPE_FORMAT_AL88_SNORM;
381    case MESA_FORMAT_I_SNORM8:
382       return PIPE_FORMAT_I8_SNORM;
383 
384    case MESA_FORMAT_R_SNORM16:
385       return PIPE_FORMAT_R16_SNORM;
386    case MESA_FORMAT_R16G16_SNORM:
387       return PIPE_FORMAT_RG1616_SNORM;
388    case MESA_FORMAT_G16R16_SNORM:
389       return PIPE_FORMAT_GR1616_SNORM;
390    case MESA_FORMAT_RGBA_SNORM16:
391       return PIPE_FORMAT_R16G16B16A16_SNORM;
392 
393    case MESA_FORMAT_A_SNORM16:
394       return PIPE_FORMAT_A16_SNORM;
395    case MESA_FORMAT_L_SNORM16:
396       return PIPE_FORMAT_L16_SNORM;
397    case MESA_FORMAT_LA_SNORM16:
398       return PIPE_FORMAT_L16A16_SNORM;
399    case MESA_FORMAT_I_SNORM16:
400       return PIPE_FORMAT_I16_SNORM;
401 
402    case MESA_FORMAT_R9G9B9E5_FLOAT:
403       return PIPE_FORMAT_R9G9B9E5_FLOAT;
404    case MESA_FORMAT_R11G11B10_FLOAT:
405       return PIPE_FORMAT_R11G11B10_FLOAT;
406    case MESA_FORMAT_B10G10R10A2_UINT:
407       return PIPE_FORMAT_B10G10R10A2_UINT;
408    case MESA_FORMAT_R10G10B10A2_UINT:
409       return PIPE_FORMAT_R10G10B10A2_UINT;
410 
411    case MESA_FORMAT_B4G4R4X4_UNORM:
412       return PIPE_FORMAT_B4G4R4X4_UNORM;
413    case MESA_FORMAT_B5G5R5X1_UNORM:
414       return PIPE_FORMAT_B5G5R5X1_UNORM;
415    case MESA_FORMAT_X1B5G5R5_UNORM:
416       return PIPE_FORMAT_X1B5G5R5_UNORM;
417    case MESA_FORMAT_R8G8B8X8_SNORM:
418       return PIPE_FORMAT_RGBX8888_SNORM;
419    case MESA_FORMAT_X8B8G8R8_SNORM:
420       return PIPE_FORMAT_XBGR8888_SNORM;
421    case MESA_FORMAT_R8G8B8X8_SRGB:
422       return PIPE_FORMAT_RGBX8888_SRGB;
423    case MESA_FORMAT_X8B8G8R8_SRGB:
424       return PIPE_FORMAT_XBGR8888_SRGB;
425    case MESA_FORMAT_RGBX_UINT8:
426       return PIPE_FORMAT_R8G8B8X8_UINT;
427    case MESA_FORMAT_RGBX_SINT8:
428       return PIPE_FORMAT_R8G8B8X8_SINT;
429    case MESA_FORMAT_B10G10R10X2_UNORM:
430       return PIPE_FORMAT_B10G10R10X2_UNORM;
431    case MESA_FORMAT_RGBX_UNORM16:
432       return PIPE_FORMAT_R16G16B16X16_UNORM;
433    case MESA_FORMAT_RGBX_SNORM16:
434       return PIPE_FORMAT_R16G16B16X16_SNORM;
435    case MESA_FORMAT_RGBX_FLOAT16:
436       return PIPE_FORMAT_R16G16B16X16_FLOAT;
437    case MESA_FORMAT_RGBX_UINT16:
438       return PIPE_FORMAT_R16G16B16X16_UINT;
439    case MESA_FORMAT_RGBX_SINT16:
440       return PIPE_FORMAT_R16G16B16X16_SINT;
441    case MESA_FORMAT_RGBX_FLOAT32:
442       return PIPE_FORMAT_R32G32B32X32_FLOAT;
443    case MESA_FORMAT_RGBX_UINT32:
444       return PIPE_FORMAT_R32G32B32X32_UINT;
445    case MESA_FORMAT_RGBX_SINT32:
446       return PIPE_FORMAT_R32G32B32X32_SINT;
447 
448    case MESA_FORMAT_B8G8R8X8_SRGB:
449       return PIPE_FORMAT_BGRX8888_SRGB;
450    case MESA_FORMAT_X8R8G8B8_SRGB:
451       return PIPE_FORMAT_XRGB8888_SRGB;
452 
453    /* ETC2 formats are emulated as uncompressed ones.
454     * The destination formats mustn't be changed, because they are also
455     * destination formats of the unpack/decompression function. */
456    case MESA_FORMAT_ETC2_RGB8:
457       return st->has_etc2 ? PIPE_FORMAT_ETC2_RGB8 : PIPE_FORMAT_R8G8B8A8_UNORM;
458    case MESA_FORMAT_ETC2_SRGB8:
459       return st->has_etc2 ? PIPE_FORMAT_ETC2_SRGB8 : PIPE_FORMAT_B8G8R8A8_SRGB;
460    case MESA_FORMAT_ETC2_RGBA8_EAC:
461       return st->has_etc2 ? PIPE_FORMAT_ETC2_RGBA8 : PIPE_FORMAT_R8G8B8A8_UNORM;
462    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
463       return st->has_etc2 ? PIPE_FORMAT_ETC2_SRGBA8 : PIPE_FORMAT_B8G8R8A8_SRGB;
464    case MESA_FORMAT_ETC2_R11_EAC:
465       return st->has_etc2 ? PIPE_FORMAT_ETC2_R11_UNORM : PIPE_FORMAT_R16_UNORM;
466    case MESA_FORMAT_ETC2_RG11_EAC:
467       return st->has_etc2 ? PIPE_FORMAT_ETC2_RG11_UNORM : PIPE_FORMAT_R16G16_UNORM;
468    case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
469       return st->has_etc2 ? PIPE_FORMAT_ETC2_R11_SNORM : PIPE_FORMAT_R16_SNORM;
470    case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
471       return st->has_etc2 ? PIPE_FORMAT_ETC2_RG11_SNORM : PIPE_FORMAT_R16G16_SNORM;
472    case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
473       return st->has_etc2 ? PIPE_FORMAT_ETC2_RGB8A1 : PIPE_FORMAT_R8G8B8A8_UNORM;
474    case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
475       return st->has_etc2 ? PIPE_FORMAT_ETC2_SRGB8A1 : PIPE_FORMAT_B8G8R8A8_SRGB;
476 
477    case MESA_FORMAT_RGBA_ASTC_4x4:
478       return PIPE_FORMAT_ASTC_4x4;
479    case MESA_FORMAT_RGBA_ASTC_5x4:
480       return PIPE_FORMAT_ASTC_5x4;
481    case MESA_FORMAT_RGBA_ASTC_5x5:
482       return PIPE_FORMAT_ASTC_5x5;
483    case MESA_FORMAT_RGBA_ASTC_6x5:
484       return PIPE_FORMAT_ASTC_6x5;
485    case MESA_FORMAT_RGBA_ASTC_6x6:
486       return PIPE_FORMAT_ASTC_6x6;
487    case MESA_FORMAT_RGBA_ASTC_8x5:
488       return PIPE_FORMAT_ASTC_8x5;
489    case MESA_FORMAT_RGBA_ASTC_8x6:
490       return PIPE_FORMAT_ASTC_8x6;
491    case MESA_FORMAT_RGBA_ASTC_8x8:
492       return PIPE_FORMAT_ASTC_8x8;
493    case MESA_FORMAT_RGBA_ASTC_10x5:
494       return PIPE_FORMAT_ASTC_10x5;
495    case MESA_FORMAT_RGBA_ASTC_10x6:
496       return PIPE_FORMAT_ASTC_10x6;
497    case MESA_FORMAT_RGBA_ASTC_10x8:
498       return PIPE_FORMAT_ASTC_10x8;
499    case MESA_FORMAT_RGBA_ASTC_10x10:
500       return PIPE_FORMAT_ASTC_10x10;
501    case MESA_FORMAT_RGBA_ASTC_12x10:
502       return PIPE_FORMAT_ASTC_12x10;
503    case MESA_FORMAT_RGBA_ASTC_12x12:
504       return PIPE_FORMAT_ASTC_12x12;
505 
506    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
507       return PIPE_FORMAT_ASTC_4x4_SRGB;
508    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
509       return PIPE_FORMAT_ASTC_5x4_SRGB;
510    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
511       return PIPE_FORMAT_ASTC_5x5_SRGB;
512    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
513       return PIPE_FORMAT_ASTC_6x5_SRGB;
514    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
515       return PIPE_FORMAT_ASTC_6x6_SRGB;
516    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
517       return PIPE_FORMAT_ASTC_8x5_SRGB;
518    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
519       return PIPE_FORMAT_ASTC_8x6_SRGB;
520    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
521       return PIPE_FORMAT_ASTC_8x8_SRGB;
522    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
523       return PIPE_FORMAT_ASTC_10x5_SRGB;
524    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
525       return PIPE_FORMAT_ASTC_10x6_SRGB;
526    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
527       return PIPE_FORMAT_ASTC_10x8_SRGB;
528    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
529       return PIPE_FORMAT_ASTC_10x10_SRGB;
530    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
531       return PIPE_FORMAT_ASTC_12x10_SRGB;
532    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
533       return PIPE_FORMAT_ASTC_12x12_SRGB;
534 
535    default:
536       return PIPE_FORMAT_NONE;
537    }
538 }
539 
540 
541 /**
542  * Translate Gallium format to Mesa format.
543  */
544 mesa_format
st_pipe_format_to_mesa_format(enum pipe_format format)545 st_pipe_format_to_mesa_format(enum pipe_format format)
546 {
547    switch (format) {
548    case PIPE_FORMAT_ABGR8888_UNORM:
549       return MESA_FORMAT_A8B8G8R8_UNORM;
550    case PIPE_FORMAT_RGBA8888_UNORM:
551       return MESA_FORMAT_R8G8B8A8_UNORM;
552    case PIPE_FORMAT_BGRA8888_UNORM:
553       return MESA_FORMAT_B8G8R8A8_UNORM;
554    case PIPE_FORMAT_ARGB8888_UNORM:
555       return MESA_FORMAT_A8R8G8B8_UNORM;
556    case PIPE_FORMAT_XBGR8888_UNORM:
557       return MESA_FORMAT_X8B8G8R8_UNORM;
558    case PIPE_FORMAT_RGBX8888_UNORM:
559       return MESA_FORMAT_R8G8B8X8_UNORM;
560    case PIPE_FORMAT_BGRX8888_UNORM:
561       return MESA_FORMAT_B8G8R8X8_UNORM;
562    case PIPE_FORMAT_XRGB8888_UNORM:
563       return MESA_FORMAT_X8R8G8B8_UNORM;
564    case PIPE_FORMAT_B5G5R5A1_UNORM:
565       return MESA_FORMAT_B5G5R5A1_UNORM;
566    case PIPE_FORMAT_A1B5G5R5_UNORM:
567       return MESA_FORMAT_A1B5G5R5_UNORM;
568    case PIPE_FORMAT_B4G4R4A4_UNORM:
569       return MESA_FORMAT_B4G4R4A4_UNORM;
570    case PIPE_FORMAT_B5G6R5_UNORM:
571       return MESA_FORMAT_B5G6R5_UNORM;
572    case PIPE_FORMAT_B2G3R3_UNORM:
573       return MESA_FORMAT_B2G3R3_UNORM;
574    case PIPE_FORMAT_B10G10R10A2_UNORM:
575       return MESA_FORMAT_B10G10R10A2_UNORM;
576    case PIPE_FORMAT_R10G10B10A2_UNORM:
577       return MESA_FORMAT_R10G10B10A2_UNORM;
578    case PIPE_FORMAT_R10G10B10X2_UNORM:
579       return MESA_FORMAT_R10G10B10X2_UNORM;
580    case PIPE_FORMAT_L4A4_UNORM:
581       return MESA_FORMAT_L4A4_UNORM;
582    case PIPE_FORMAT_LA88_UNORM:
583       return MESA_FORMAT_L8A8_UNORM;
584    case PIPE_FORMAT_AL88_UNORM:
585       return MESA_FORMAT_A8L8_UNORM;
586    case PIPE_FORMAT_LA1616_UNORM:
587       return MESA_FORMAT_L16A16_UNORM;
588    case PIPE_FORMAT_AL1616_UNORM:
589       return MESA_FORMAT_A16L16_UNORM;
590    case PIPE_FORMAT_A8_UNORM:
591       return MESA_FORMAT_A_UNORM8;
592    case PIPE_FORMAT_A16_UNORM:
593       return MESA_FORMAT_A_UNORM16;
594    case PIPE_FORMAT_L8_UNORM:
595       return MESA_FORMAT_L_UNORM8;
596    case PIPE_FORMAT_L16_UNORM:
597       return MESA_FORMAT_L_UNORM16;
598    case PIPE_FORMAT_I8_UNORM:
599       return MESA_FORMAT_I_UNORM8;
600    case PIPE_FORMAT_I16_UNORM:
601       return MESA_FORMAT_I_UNORM16;
602    case PIPE_FORMAT_S8_UINT:
603       return MESA_FORMAT_S_UINT8;
604 
605    case PIPE_FORMAT_R16G16B16A16_UNORM:
606       return MESA_FORMAT_RGBA_UNORM16;
607 
608    case PIPE_FORMAT_Z16_UNORM:
609       return MESA_FORMAT_Z_UNORM16;
610    case PIPE_FORMAT_Z32_UNORM:
611       return MESA_FORMAT_Z_UNORM32;
612    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
613       return MESA_FORMAT_S8_UINT_Z24_UNORM;
614    case PIPE_FORMAT_X8Z24_UNORM:
615       return MESA_FORMAT_X8_UINT_Z24_UNORM;
616    case PIPE_FORMAT_Z24X8_UNORM:
617       return MESA_FORMAT_Z24_UNORM_X8_UINT;
618    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
619       return MESA_FORMAT_Z24_UNORM_S8_UINT;
620    case PIPE_FORMAT_Z32_FLOAT:
621       return MESA_FORMAT_Z_FLOAT32;
622    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
623       return MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
624 
625    case PIPE_FORMAT_UYVY:
626       return MESA_FORMAT_YCBCR;
627    case PIPE_FORMAT_YUYV:
628       return MESA_FORMAT_YCBCR_REV;
629 
630    case PIPE_FORMAT_DXT1_RGB:
631       return MESA_FORMAT_RGB_DXT1;
632    case PIPE_FORMAT_DXT1_RGBA:
633       return MESA_FORMAT_RGBA_DXT1;
634    case PIPE_FORMAT_DXT3_RGBA:
635       return MESA_FORMAT_RGBA_DXT3;
636    case PIPE_FORMAT_DXT5_RGBA:
637       return MESA_FORMAT_RGBA_DXT5;
638    case PIPE_FORMAT_DXT1_SRGB:
639       return MESA_FORMAT_SRGB_DXT1;
640    case PIPE_FORMAT_DXT1_SRGBA:
641       return MESA_FORMAT_SRGBA_DXT1;
642    case PIPE_FORMAT_DXT3_SRGBA:
643       return MESA_FORMAT_SRGBA_DXT3;
644    case PIPE_FORMAT_DXT5_SRGBA:
645       return MESA_FORMAT_SRGBA_DXT5;
646    case PIPE_FORMAT_LA88_SRGB:
647       return MESA_FORMAT_L8A8_SRGB;
648    case PIPE_FORMAT_AL88_SRGB:
649       return MESA_FORMAT_A8L8_SRGB;
650    case PIPE_FORMAT_L8_SRGB:
651       return MESA_FORMAT_L_SRGB8;
652    case PIPE_FORMAT_R8G8B8_SRGB:
653       return MESA_FORMAT_BGR_SRGB8;
654    case PIPE_FORMAT_ABGR8888_SRGB:
655       return MESA_FORMAT_A8B8G8R8_SRGB;
656    case PIPE_FORMAT_RGBA8888_SRGB:
657       return MESA_FORMAT_R8G8B8A8_SRGB;
658    case PIPE_FORMAT_BGRA8888_SRGB:
659       return MESA_FORMAT_B8G8R8A8_SRGB;
660    case PIPE_FORMAT_ARGB8888_SRGB:
661       return MESA_FORMAT_A8R8G8B8_SRGB;
662    case PIPE_FORMAT_R32G32B32A32_FLOAT:
663       return MESA_FORMAT_RGBA_FLOAT32;
664    case PIPE_FORMAT_R16G16B16A16_FLOAT:
665       return MESA_FORMAT_RGBA_FLOAT16;
666    case PIPE_FORMAT_R32G32B32_FLOAT:
667       return MESA_FORMAT_RGB_FLOAT32;
668    case PIPE_FORMAT_R16G16B16_FLOAT:
669       return MESA_FORMAT_RGB_FLOAT16;
670    case PIPE_FORMAT_L32A32_FLOAT:
671       return MESA_FORMAT_LA_FLOAT32;
672    case PIPE_FORMAT_L16A16_FLOAT:
673       return MESA_FORMAT_LA_FLOAT16;
674    case PIPE_FORMAT_L32_FLOAT:
675       return MESA_FORMAT_L_FLOAT32;
676    case PIPE_FORMAT_L16_FLOAT:
677       return MESA_FORMAT_L_FLOAT16;
678    case PIPE_FORMAT_A32_FLOAT:
679       return MESA_FORMAT_A_FLOAT32;
680    case PIPE_FORMAT_A16_FLOAT:
681       return MESA_FORMAT_A_FLOAT16;
682    case PIPE_FORMAT_I32_FLOAT:
683       return MESA_FORMAT_I_FLOAT32;
684    case PIPE_FORMAT_I16_FLOAT:
685       return MESA_FORMAT_I_FLOAT16;
686    case PIPE_FORMAT_R32_FLOAT:
687       return MESA_FORMAT_R_FLOAT32;
688    case PIPE_FORMAT_R16_FLOAT:
689       return MESA_FORMAT_R_FLOAT16;
690    case PIPE_FORMAT_R32G32_FLOAT:
691       return MESA_FORMAT_RG_FLOAT32;
692    case PIPE_FORMAT_R16G16_FLOAT:
693       return MESA_FORMAT_RG_FLOAT16;
694 
695    case PIPE_FORMAT_R8_UNORM:
696       return MESA_FORMAT_R_UNORM8;
697    case PIPE_FORMAT_R16_UNORM:
698       return MESA_FORMAT_R_UNORM16;
699    case PIPE_FORMAT_RG88_UNORM:
700       return MESA_FORMAT_R8G8_UNORM;
701    case PIPE_FORMAT_GR88_UNORM:
702       return MESA_FORMAT_G8R8_UNORM;
703    case PIPE_FORMAT_RG1616_UNORM:
704       return MESA_FORMAT_R16G16_UNORM;
705    case PIPE_FORMAT_GR1616_UNORM:
706       return MESA_FORMAT_G16R16_UNORM;
707 
708    case PIPE_FORMAT_A8_UINT:
709       return MESA_FORMAT_A_UINT8;
710    case PIPE_FORMAT_A16_UINT:
711       return MESA_FORMAT_A_UINT16;
712    case PIPE_FORMAT_A32_UINT:
713       return MESA_FORMAT_A_UINT32;
714    case PIPE_FORMAT_A8_SINT:
715       return MESA_FORMAT_A_SINT8;
716    case PIPE_FORMAT_A16_SINT:
717       return MESA_FORMAT_A_SINT16;
718    case PIPE_FORMAT_A32_SINT:
719       return MESA_FORMAT_A_SINT32;
720 
721    case PIPE_FORMAT_I8_UINT:
722       return MESA_FORMAT_I_UINT8;
723    case PIPE_FORMAT_I16_UINT:
724       return MESA_FORMAT_I_UINT16;
725    case PIPE_FORMAT_I32_UINT:
726       return MESA_FORMAT_I_UINT32;
727    case PIPE_FORMAT_I8_SINT:
728       return MESA_FORMAT_I_SINT8;
729    case PIPE_FORMAT_I16_SINT:
730       return MESA_FORMAT_I_SINT16;
731    case PIPE_FORMAT_I32_SINT:
732       return MESA_FORMAT_I_SINT32;
733 
734   case PIPE_FORMAT_L8_UINT:
735       return MESA_FORMAT_L_UINT8;
736    case PIPE_FORMAT_L16_UINT:
737       return MESA_FORMAT_L_UINT16;
738    case PIPE_FORMAT_L32_UINT:
739       return MESA_FORMAT_L_UINT32;
740    case PIPE_FORMAT_L8_SINT:
741       return MESA_FORMAT_L_SINT8;
742    case PIPE_FORMAT_L16_SINT:
743       return MESA_FORMAT_L_SINT16;
744    case PIPE_FORMAT_L32_SINT:
745       return MESA_FORMAT_L_SINT32;
746 
747    case PIPE_FORMAT_L8A8_UINT:
748       return MESA_FORMAT_LA_UINT8;
749    case PIPE_FORMAT_L16A16_UINT:
750       return MESA_FORMAT_LA_UINT16;
751    case PIPE_FORMAT_L32A32_UINT:
752       return MESA_FORMAT_LA_UINT32;
753    case PIPE_FORMAT_L8A8_SINT:
754       return MESA_FORMAT_LA_SINT8;
755    case PIPE_FORMAT_L16A16_SINT:
756       return MESA_FORMAT_LA_SINT16;
757    case PIPE_FORMAT_L32A32_SINT:
758       return MESA_FORMAT_LA_SINT32;
759 
760    case PIPE_FORMAT_R8_SINT:
761       return MESA_FORMAT_R_SINT8;
762    case PIPE_FORMAT_R8G8_SINT:
763       return MESA_FORMAT_RG_SINT8;
764    case PIPE_FORMAT_R8G8B8_SINT:
765       return MESA_FORMAT_RGB_SINT8;
766    case PIPE_FORMAT_R8G8B8A8_SINT:
767       return MESA_FORMAT_RGBA_SINT8;
768 
769    case PIPE_FORMAT_R16_SINT:
770       return MESA_FORMAT_R_SINT16;
771    case PIPE_FORMAT_R16G16_SINT:
772       return MESA_FORMAT_RG_SINT16;
773    case PIPE_FORMAT_R16G16B16_SINT:
774       return MESA_FORMAT_RGB_SINT16;
775    case PIPE_FORMAT_R16G16B16A16_SINT:
776       return MESA_FORMAT_RGBA_SINT16;
777 
778    case PIPE_FORMAT_R32_SINT:
779       return MESA_FORMAT_R_SINT32;
780    case PIPE_FORMAT_R32G32_SINT:
781       return MESA_FORMAT_RG_SINT32;
782    case PIPE_FORMAT_R32G32B32_SINT:
783       return MESA_FORMAT_RGB_SINT32;
784    case PIPE_FORMAT_R32G32B32A32_SINT:
785       return MESA_FORMAT_RGBA_SINT32;
786 
787    /* unsigned int formats */
788    case PIPE_FORMAT_R8_UINT:
789       return MESA_FORMAT_R_UINT8;
790    case PIPE_FORMAT_R8G8_UINT:
791       return MESA_FORMAT_RG_UINT8;
792    case PIPE_FORMAT_R8G8B8_UINT:
793       return MESA_FORMAT_RGB_UINT8;
794    case PIPE_FORMAT_R8G8B8A8_UINT:
795       return MESA_FORMAT_RGBA_UINT8;
796 
797    case PIPE_FORMAT_R16_UINT:
798       return MESA_FORMAT_R_UINT16;
799    case PIPE_FORMAT_R16G16_UINT:
800       return MESA_FORMAT_RG_UINT16;
801    case PIPE_FORMAT_R16G16B16_UINT:
802       return MESA_FORMAT_RGB_UINT16;
803    case PIPE_FORMAT_R16G16B16A16_UINT:
804       return MESA_FORMAT_RGBA_UINT16;
805 
806    case PIPE_FORMAT_R32_UINT:
807       return MESA_FORMAT_R_UINT32;
808    case PIPE_FORMAT_R32G32_UINT:
809       return MESA_FORMAT_RG_UINT32;
810    case PIPE_FORMAT_R32G32B32_UINT:
811       return MESA_FORMAT_RGB_UINT32;
812    case PIPE_FORMAT_R32G32B32A32_UINT:
813       return MESA_FORMAT_RGBA_UINT32;
814 
815    case PIPE_FORMAT_RGTC1_UNORM:
816       return MESA_FORMAT_R_RGTC1_UNORM;
817    case PIPE_FORMAT_RGTC1_SNORM:
818       return MESA_FORMAT_R_RGTC1_SNORM;
819    case PIPE_FORMAT_RGTC2_UNORM:
820       return MESA_FORMAT_RG_RGTC2_UNORM;
821    case PIPE_FORMAT_RGTC2_SNORM:
822       return MESA_FORMAT_RG_RGTC2_SNORM;
823 
824    case PIPE_FORMAT_LATC1_UNORM:
825       return MESA_FORMAT_L_LATC1_UNORM;
826    case PIPE_FORMAT_LATC1_SNORM:
827       return MESA_FORMAT_L_LATC1_SNORM;
828    case PIPE_FORMAT_LATC2_UNORM:
829       return MESA_FORMAT_LA_LATC2_UNORM;
830    case PIPE_FORMAT_LATC2_SNORM:
831       return MESA_FORMAT_LA_LATC2_SNORM;
832 
833    case PIPE_FORMAT_ETC1_RGB8:
834       return MESA_FORMAT_ETC1_RGB8;
835 
836    case PIPE_FORMAT_BPTC_RGBA_UNORM:
837       return MESA_FORMAT_BPTC_RGBA_UNORM;
838    case PIPE_FORMAT_BPTC_SRGBA:
839       return MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM;
840    case PIPE_FORMAT_BPTC_RGB_FLOAT:
841       return MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT;
842    case PIPE_FORMAT_BPTC_RGB_UFLOAT:
843       return MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT;
844 
845    /* signed normalized formats */
846    case PIPE_FORMAT_R8_SNORM:
847       return MESA_FORMAT_R_SNORM8;
848    case PIPE_FORMAT_RG88_SNORM:
849       return MESA_FORMAT_R8G8_SNORM;
850    case PIPE_FORMAT_GR88_SNORM:
851       return MESA_FORMAT_G8R8_SNORM;
852    case PIPE_FORMAT_RGBA8888_SNORM:
853       return MESA_FORMAT_R8G8B8A8_SNORM;
854    case PIPE_FORMAT_ABGR8888_SNORM:
855       return MESA_FORMAT_A8B8G8R8_SNORM;
856 
857    case PIPE_FORMAT_A8_SNORM:
858       return MESA_FORMAT_A_SNORM8;
859    case PIPE_FORMAT_L8_SNORM:
860       return MESA_FORMAT_L_SNORM8;
861    case PIPE_FORMAT_LA88_SNORM:
862       return MESA_FORMAT_L8A8_SNORM;
863    case PIPE_FORMAT_AL88_SNORM:
864       return MESA_FORMAT_A8L8_SNORM;
865    case PIPE_FORMAT_I8_SNORM:
866       return MESA_FORMAT_I_SNORM8;
867 
868    case PIPE_FORMAT_R16_SNORM:
869       return MESA_FORMAT_R_SNORM16;
870    case PIPE_FORMAT_RG1616_SNORM:
871       return MESA_FORMAT_R16G16_SNORM;
872    case PIPE_FORMAT_GR1616_SNORM:
873       return MESA_FORMAT_G16R16_SNORM;
874    case PIPE_FORMAT_R16G16B16A16_SNORM:
875       return MESA_FORMAT_RGBA_SNORM16;
876 
877    case PIPE_FORMAT_A16_SNORM:
878       return MESA_FORMAT_A_SNORM16;
879    case PIPE_FORMAT_L16_SNORM:
880       return MESA_FORMAT_L_SNORM16;
881    case PIPE_FORMAT_L16A16_SNORM:
882       return MESA_FORMAT_LA_SNORM16;
883    case PIPE_FORMAT_I16_SNORM:
884       return MESA_FORMAT_I_SNORM16;
885 
886    case PIPE_FORMAT_R9G9B9E5_FLOAT:
887       return MESA_FORMAT_R9G9B9E5_FLOAT;
888    case PIPE_FORMAT_R11G11B10_FLOAT:
889       return MESA_FORMAT_R11G11B10_FLOAT;
890 
891    case PIPE_FORMAT_B10G10R10A2_UINT:
892       return MESA_FORMAT_B10G10R10A2_UINT;
893    case PIPE_FORMAT_R10G10B10A2_UINT:
894       return MESA_FORMAT_R10G10B10A2_UINT;
895 
896    case PIPE_FORMAT_B4G4R4X4_UNORM:
897       return MESA_FORMAT_B4G4R4X4_UNORM;
898    case PIPE_FORMAT_B5G5R5X1_UNORM:
899       return MESA_FORMAT_B5G5R5X1_UNORM;
900    case PIPE_FORMAT_X1B5G5R5_UNORM:
901       return MESA_FORMAT_X1B5G5R5_UNORM;
902    case PIPE_FORMAT_RGBX8888_SNORM:
903       return MESA_FORMAT_R8G8B8X8_SNORM;
904    case PIPE_FORMAT_XBGR8888_SNORM:
905       return MESA_FORMAT_X8B8G8R8_SNORM;
906    case PIPE_FORMAT_RGBX8888_SRGB:
907       return MESA_FORMAT_R8G8B8X8_SRGB;
908    case PIPE_FORMAT_XBGR8888_SRGB:
909       return MESA_FORMAT_X8B8G8R8_SRGB;
910    case PIPE_FORMAT_R8G8B8X8_UINT:
911       return MESA_FORMAT_RGBX_UINT8;
912    case PIPE_FORMAT_R8G8B8X8_SINT:
913       return MESA_FORMAT_RGBX_SINT8;
914    case PIPE_FORMAT_B10G10R10X2_UNORM:
915       return MESA_FORMAT_B10G10R10X2_UNORM;
916    case PIPE_FORMAT_R16G16B16X16_UNORM:
917       return MESA_FORMAT_RGBX_UNORM16;
918    case PIPE_FORMAT_R16G16B16X16_SNORM:
919       return MESA_FORMAT_RGBX_SNORM16;
920    case PIPE_FORMAT_R16G16B16X16_FLOAT:
921       return MESA_FORMAT_RGBX_FLOAT16;
922    case PIPE_FORMAT_R16G16B16X16_UINT:
923       return MESA_FORMAT_RGBX_UINT16;
924    case PIPE_FORMAT_R16G16B16X16_SINT:
925       return MESA_FORMAT_RGBX_SINT16;
926    case PIPE_FORMAT_R32G32B32X32_FLOAT:
927       return MESA_FORMAT_RGBX_FLOAT32;
928    case PIPE_FORMAT_R32G32B32X32_UINT:
929       return MESA_FORMAT_RGBX_UINT32;
930    case PIPE_FORMAT_R32G32B32X32_SINT:
931       return MESA_FORMAT_RGBX_SINT32;
932 
933    case PIPE_FORMAT_BGRX8888_SRGB:
934       return MESA_FORMAT_B8G8R8X8_SRGB;
935    case PIPE_FORMAT_XRGB8888_SRGB:
936       return MESA_FORMAT_X8R8G8B8_SRGB;
937 
938    case PIPE_FORMAT_ETC2_RGB8:
939       return MESA_FORMAT_ETC2_RGB8;
940    case PIPE_FORMAT_ETC2_SRGB8:
941       return MESA_FORMAT_ETC2_SRGB8;
942    case PIPE_FORMAT_ETC2_RGB8A1:
943       return MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
944    case PIPE_FORMAT_ETC2_SRGB8A1:
945       return MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1;
946    case PIPE_FORMAT_ETC2_RGBA8:
947       return MESA_FORMAT_ETC2_RGBA8_EAC;
948    case PIPE_FORMAT_ETC2_SRGBA8:
949       return MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC;
950    case PIPE_FORMAT_ETC2_R11_UNORM:
951       return MESA_FORMAT_ETC2_R11_EAC;
952    case PIPE_FORMAT_ETC2_R11_SNORM:
953       return MESA_FORMAT_ETC2_SIGNED_R11_EAC;
954    case PIPE_FORMAT_ETC2_RG11_UNORM:
955       return MESA_FORMAT_ETC2_RG11_EAC;
956    case PIPE_FORMAT_ETC2_RG11_SNORM:
957       return MESA_FORMAT_ETC2_SIGNED_RG11_EAC;
958 
959    case PIPE_FORMAT_ASTC_4x4:
960       return MESA_FORMAT_RGBA_ASTC_4x4;
961    case PIPE_FORMAT_ASTC_5x4:
962       return MESA_FORMAT_RGBA_ASTC_5x4;
963    case PIPE_FORMAT_ASTC_5x5:
964       return MESA_FORMAT_RGBA_ASTC_5x5;
965    case PIPE_FORMAT_ASTC_6x5:
966       return MESA_FORMAT_RGBA_ASTC_6x5;
967    case PIPE_FORMAT_ASTC_6x6:
968       return MESA_FORMAT_RGBA_ASTC_6x6;
969    case PIPE_FORMAT_ASTC_8x5:
970       return MESA_FORMAT_RGBA_ASTC_8x5;
971    case PIPE_FORMAT_ASTC_8x6:
972       return MESA_FORMAT_RGBA_ASTC_8x6;
973    case PIPE_FORMAT_ASTC_8x8:
974       return MESA_FORMAT_RGBA_ASTC_8x8;
975    case PIPE_FORMAT_ASTC_10x5:
976       return MESA_FORMAT_RGBA_ASTC_10x5;
977    case PIPE_FORMAT_ASTC_10x6:
978       return MESA_FORMAT_RGBA_ASTC_10x6;
979    case PIPE_FORMAT_ASTC_10x8:
980       return MESA_FORMAT_RGBA_ASTC_10x8;
981    case PIPE_FORMAT_ASTC_10x10:
982       return MESA_FORMAT_RGBA_ASTC_10x10;
983    case PIPE_FORMAT_ASTC_12x10:
984       return MESA_FORMAT_RGBA_ASTC_12x10;
985    case PIPE_FORMAT_ASTC_12x12:
986       return MESA_FORMAT_RGBA_ASTC_12x12;
987 
988    case PIPE_FORMAT_ASTC_4x4_SRGB:
989       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4;
990    case PIPE_FORMAT_ASTC_5x4_SRGB:
991       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4;
992    case PIPE_FORMAT_ASTC_5x5_SRGB:
993       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5;
994    case PIPE_FORMAT_ASTC_6x5_SRGB:
995       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5;
996    case PIPE_FORMAT_ASTC_6x6_SRGB:
997       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6;
998    case PIPE_FORMAT_ASTC_8x5_SRGB:
999       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5;
1000    case PIPE_FORMAT_ASTC_8x6_SRGB:
1001       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6;
1002    case PIPE_FORMAT_ASTC_8x8_SRGB:
1003       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8;
1004    case PIPE_FORMAT_ASTC_10x5_SRGB:
1005       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5;
1006    case PIPE_FORMAT_ASTC_10x6_SRGB:
1007       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6;
1008    case PIPE_FORMAT_ASTC_10x8_SRGB:
1009       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8;
1010    case PIPE_FORMAT_ASTC_10x10_SRGB:
1011       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10;
1012    case PIPE_FORMAT_ASTC_12x10_SRGB:
1013       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10;
1014    case PIPE_FORMAT_ASTC_12x12_SRGB:
1015       return MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12;
1016 
1017    default:
1018       return MESA_FORMAT_NONE;
1019    }
1020 }
1021 
1022 
1023 /**
1024  * Debug only: check that the two functions above correctly map
1025  * Mesa formats to Gallium formats and back again.
1026  */
1027 static void
test_format_conversion(struct st_context * st)1028 test_format_conversion(struct st_context *st)
1029 {
1030    GLuint i;
1031 
1032    /* test all Mesa formats */
1033    for (i = 1; i < MESA_FORMAT_COUNT; i++) {
1034       enum pipe_format pf;
1035 
1036       /* ETC formats are translated differently, skip them. */
1037       if (_mesa_is_format_etc2(i))
1038          continue;
1039       if (i == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)
1040          continue;
1041 
1042       pf = st_mesa_format_to_pipe_format(st, i);
1043       if (pf != PIPE_FORMAT_NONE) {
1044          mesa_format MAYBE_UNUSED mf = st_pipe_format_to_mesa_format(pf);
1045          assert(mf == i);
1046       }
1047    }
1048 
1049    /* Test all Gallium formats */
1050    for (i = 1; i < PIPE_FORMAT_COUNT; i++) {
1051       /* ETC formats are translated differently, skip them. */
1052       if (i == PIPE_FORMAT_ETC1_RGB8 && !st->has_etc1)
1053          continue;
1054 
1055       mesa_format mf = st_pipe_format_to_mesa_format(i);
1056       if (_mesa_is_format_etc2(mf) && !st->has_etc2)
1057          continue;
1058 
1059       if (mf != MESA_FORMAT_NONE) {
1060          enum pipe_format MAYBE_UNUSED pf =
1061             st_mesa_format_to_pipe_format(st, mf);
1062          assert(pf == i);
1063       }
1064    }
1065 }
1066 
1067 
1068 /**
1069  * Map GL texture formats to Gallium pipe formats.
1070  */
1071 struct format_mapping
1072 {
1073    GLenum glFormats[18];       /**< list of GLenum formats, 0-terminated */
1074    enum pipe_format pipeFormats[14]; /**< list of pipe formats, 0-terminated */
1075 };
1076 
1077 
1078 #define DEFAULT_RGBA_FORMATS \
1079       PIPE_FORMAT_R8G8B8A8_UNORM, \
1080       PIPE_FORMAT_B8G8R8A8_UNORM, \
1081       PIPE_FORMAT_A8R8G8B8_UNORM, \
1082       PIPE_FORMAT_A8B8G8R8_UNORM, \
1083       0
1084 
1085 #define DEFAULT_RGB_FORMATS \
1086       PIPE_FORMAT_R8G8B8X8_UNORM, \
1087       PIPE_FORMAT_B8G8R8X8_UNORM, \
1088       PIPE_FORMAT_X8R8G8B8_UNORM, \
1089       PIPE_FORMAT_X8B8G8R8_UNORM, \
1090       PIPE_FORMAT_B5G6R5_UNORM, \
1091       DEFAULT_RGBA_FORMATS
1092 
1093 #define DEFAULT_SRGBA_FORMATS \
1094       PIPE_FORMAT_R8G8B8A8_SRGB, \
1095       PIPE_FORMAT_B8G8R8A8_SRGB, \
1096       PIPE_FORMAT_A8R8G8B8_SRGB, \
1097       PIPE_FORMAT_A8B8G8R8_SRGB, \
1098       0
1099 
1100 #define DEFAULT_DEPTH_FORMATS \
1101       PIPE_FORMAT_Z24X8_UNORM, \
1102       PIPE_FORMAT_X8Z24_UNORM, \
1103       PIPE_FORMAT_Z16_UNORM, \
1104       PIPE_FORMAT_Z24_UNORM_S8_UINT, \
1105       PIPE_FORMAT_S8_UINT_Z24_UNORM, \
1106       0
1107 
1108 #define DEFAULT_SNORM8_RGBA_FORMATS \
1109       PIPE_FORMAT_R8G8B8A8_SNORM, \
1110       0
1111 
1112 #define DEFAULT_UNORM16_RGBA_FORMATS \
1113       PIPE_FORMAT_R16G16B16A16_UNORM, \
1114       DEFAULT_RGBA_FORMATS
1115 
1116 
1117 /**
1118  * This table maps OpenGL texture format enums to Gallium pipe_format enums.
1119  * Multiple GL enums might map to multiple pipe_formats.
1120  * The first pipe format in the list that's supported is the one that's chosen.
1121  */
1122 static const struct format_mapping format_map[] = {
1123    /* Basic RGB, RGBA formats */
1124    {
1125       { GL_RGB10, 0 },
1126       { PIPE_FORMAT_R10G10B10X2_UNORM, PIPE_FORMAT_B10G10R10X2_UNORM,
1127         PIPE_FORMAT_R10G10B10A2_UNORM, PIPE_FORMAT_B10G10R10A2_UNORM,
1128         DEFAULT_RGB_FORMATS }
1129    },
1130    {
1131       { GL_RGB10_A2, 0 },
1132       { PIPE_FORMAT_R10G10B10A2_UNORM, PIPE_FORMAT_B10G10R10A2_UNORM,
1133         DEFAULT_RGBA_FORMATS }
1134    },
1135    {
1136       { 4, GL_RGBA, GL_RGBA8, 0 },
1137       { PIPE_FORMAT_R8G8B8A8_UNORM, DEFAULT_RGBA_FORMATS }
1138    },
1139    {
1140       { GL_BGRA, 0 },
1141       { DEFAULT_RGBA_FORMATS }
1142    },
1143    {
1144       { 3, GL_RGB, GL_RGB8, 0 },
1145       { PIPE_FORMAT_R8G8B8X8_UNORM, DEFAULT_RGB_FORMATS }
1146    },
1147    {
1148       { GL_RGB12, GL_RGB16, 0 },
1149       { PIPE_FORMAT_R16G16B16X16_UNORM, PIPE_FORMAT_R16G16B16A16_UNORM,
1150         DEFAULT_RGB_FORMATS }
1151    },
1152    {
1153       { GL_RGBA12, GL_RGBA16, 0 },
1154       { PIPE_FORMAT_R16G16B16A16_UNORM, DEFAULT_RGBA_FORMATS }
1155    },
1156    {
1157       { GL_RGBA4, GL_RGBA2, 0 },
1158       { PIPE_FORMAT_B4G4R4A4_UNORM, DEFAULT_RGBA_FORMATS }
1159    },
1160    {
1161       { GL_RGB5_A1, 0 },
1162       { PIPE_FORMAT_B5G5R5A1_UNORM, PIPE_FORMAT_A1B5G5R5_UNORM,
1163         DEFAULT_RGBA_FORMATS }
1164    },
1165    {
1166       { GL_R3_G3_B2, 0 },
1167       { PIPE_FORMAT_B2G3R3_UNORM, PIPE_FORMAT_B5G6R5_UNORM,
1168         PIPE_FORMAT_B5G5R5A1_UNORM, DEFAULT_RGB_FORMATS }
1169    },
1170    {
1171       { GL_RGB4 },
1172       { PIPE_FORMAT_B4G4R4X4_UNORM, PIPE_FORMAT_B4G4R4A4_UNORM,
1173         DEFAULT_RGB_FORMATS }
1174    },
1175    {
1176       { GL_RGB5 },
1177       { PIPE_FORMAT_B5G5R5X1_UNORM, PIPE_FORMAT_X1B5G5R5_UNORM,
1178         PIPE_FORMAT_B5G5R5A1_UNORM, PIPE_FORMAT_A1B5G5R5_UNORM,
1179         DEFAULT_RGB_FORMATS }
1180    },
1181    {
1182       { GL_RGB565 },
1183       { PIPE_FORMAT_B5G6R5_UNORM, DEFAULT_RGB_FORMATS }
1184    },
1185 
1186    /* basic Alpha formats */
1187    {
1188       { GL_ALPHA12, GL_ALPHA16, 0 },
1189       { PIPE_FORMAT_A16_UNORM, PIPE_FORMAT_R16G16B16A16_UNORM,
1190         PIPE_FORMAT_A8_UNORM, DEFAULT_RGBA_FORMATS }
1191    },
1192    {
1193       { GL_ALPHA, GL_ALPHA4, GL_ALPHA8, GL_COMPRESSED_ALPHA, 0 },
1194       { PIPE_FORMAT_A8_UNORM, DEFAULT_RGBA_FORMATS }
1195    },
1196 
1197    /* basic Luminance formats */
1198    {
1199       { GL_LUMINANCE12, GL_LUMINANCE16, 0 },
1200       { PIPE_FORMAT_L16_UNORM, PIPE_FORMAT_R16G16B16A16_UNORM,
1201         PIPE_FORMAT_L8_UNORM, DEFAULT_RGB_FORMATS }
1202    },
1203    {
1204       { 1, GL_LUMINANCE, GL_LUMINANCE4, GL_LUMINANCE8, 0 },
1205       { PIPE_FORMAT_L8_UNORM, PIPE_FORMAT_L8A8_UNORM, DEFAULT_RGB_FORMATS }
1206    },
1207 
1208    /* basic Luminance/Alpha formats */
1209    {
1210       { GL_LUMINANCE12_ALPHA4, GL_LUMINANCE12_ALPHA12,
1211         GL_LUMINANCE16_ALPHA16, 0},
1212       { PIPE_FORMAT_L16A16_UNORM, PIPE_FORMAT_R16G16B16A16_UNORM,
1213         PIPE_FORMAT_L8A8_UNORM, DEFAULT_RGBA_FORMATS }
1214    },
1215    {
1216       { 2, GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, GL_LUMINANCE8_ALPHA8, 0 },
1217       { PIPE_FORMAT_L8A8_UNORM, DEFAULT_RGBA_FORMATS }
1218    },
1219    {
1220       { GL_LUMINANCE4_ALPHA4, 0 },
1221       { PIPE_FORMAT_L4A4_UNORM, PIPE_FORMAT_L8A8_UNORM,
1222         DEFAULT_RGBA_FORMATS }
1223    },
1224 
1225    /* basic Intensity formats */
1226    {
1227       { GL_INTENSITY12, GL_INTENSITY16, 0 },
1228       { PIPE_FORMAT_I16_UNORM, PIPE_FORMAT_R16G16B16A16_UNORM,
1229         PIPE_FORMAT_I8_UNORM, DEFAULT_RGBA_FORMATS }
1230    },
1231    {
1232       { GL_INTENSITY, GL_INTENSITY4, GL_INTENSITY8,
1233         GL_COMPRESSED_INTENSITY, 0 },
1234       { PIPE_FORMAT_I8_UNORM, DEFAULT_RGBA_FORMATS }
1235    },
1236 
1237    /* YCbCr */
1238    {
1239       { GL_YCBCR_MESA, 0 },
1240       { PIPE_FORMAT_UYVY, PIPE_FORMAT_YUYV, 0 }
1241    },
1242 
1243    /* compressed formats */ /* XXX PIPE_BIND_SAMPLER_VIEW only */
1244    {
1245       { GL_COMPRESSED_RGB, 0 },
1246       { PIPE_FORMAT_DXT1_RGB, DEFAULT_RGB_FORMATS }
1247    },
1248    {
1249       { GL_COMPRESSED_RGBA, 0 },
1250       { PIPE_FORMAT_DXT5_RGBA, DEFAULT_RGBA_FORMATS }
1251    },
1252    {
1253       { GL_RGB_S3TC, GL_RGB4_S3TC, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 0 },
1254       { PIPE_FORMAT_DXT1_RGB, 0 }
1255    },
1256    {
1257       { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 0 },
1258       { PIPE_FORMAT_DXT1_RGBA, 0 }
1259    },
1260    {
1261       { GL_RGBA_S3TC, GL_RGBA4_S3TC, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 0 },
1262       { PIPE_FORMAT_DXT3_RGBA, 0 }
1263    },
1264    {
1265       { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 0 },
1266       { PIPE_FORMAT_DXT5_RGBA, 0 }
1267    },
1268 
1269 #if 0
1270    {
1271       { GL_COMPRESSED_RGB_FXT1_3DFX, 0 },
1272       { PIPE_FORMAT_RGB_FXT1, 0 }
1273    },
1274    {
1275       { GL_COMPRESSED_RGBA_FXT1_3DFX, 0 },
1276       { PIPE_FORMAT_RGBA_FXT1, 0 }
1277    },
1278 #endif
1279 
1280    /* Depth formats */
1281    {
1282       { GL_DEPTH_COMPONENT16, 0 },
1283       { PIPE_FORMAT_Z16_UNORM, DEFAULT_DEPTH_FORMATS }
1284    },
1285    {
1286       { GL_DEPTH_COMPONENT24, 0 },
1287       { PIPE_FORMAT_Z24X8_UNORM, PIPE_FORMAT_X8Z24_UNORM,
1288         DEFAULT_DEPTH_FORMATS }
1289    },
1290    {
1291       { GL_DEPTH_COMPONENT32, 0 },
1292       { PIPE_FORMAT_Z32_UNORM, DEFAULT_DEPTH_FORMATS }
1293    },
1294    {
1295       { GL_DEPTH_COMPONENT, 0 },
1296       { DEFAULT_DEPTH_FORMATS }
1297    },
1298    {
1299       { GL_DEPTH_COMPONENT32F, 0 },
1300       { PIPE_FORMAT_Z32_FLOAT, 0 }
1301    },
1302 
1303    /* stencil formats */
1304    {
1305       { GL_STENCIL_INDEX, GL_STENCIL_INDEX1_EXT, GL_STENCIL_INDEX4_EXT,
1306         GL_STENCIL_INDEX8_EXT, GL_STENCIL_INDEX16_EXT, 0 },
1307       {
1308          PIPE_FORMAT_S8_UINT, PIPE_FORMAT_Z24_UNORM_S8_UINT,
1309          PIPE_FORMAT_S8_UINT_Z24_UNORM, 0
1310       }
1311    },
1312 
1313    /* Depth / Stencil formats */
1314    {
1315       { GL_DEPTH_STENCIL_EXT, GL_DEPTH24_STENCIL8_EXT, 0 },
1316       { PIPE_FORMAT_Z24_UNORM_S8_UINT, PIPE_FORMAT_S8_UINT_Z24_UNORM, 0 }
1317    },
1318    {
1319       { GL_DEPTH32F_STENCIL8, 0 },
1320       { PIPE_FORMAT_Z32_FLOAT_S8X24_UINT, 0 }
1321    },
1322 
1323    /* sRGB formats */
1324    {
1325       { GL_SRGB_EXT, GL_SRGB8_EXT, 0 },
1326       { PIPE_FORMAT_R8G8B8X8_SRGB, PIPE_FORMAT_B8G8R8X8_SRGB,
1327         DEFAULT_SRGBA_FORMATS }
1328    },
1329    {
1330       { GL_SRGB_ALPHA_EXT, GL_SRGB8_ALPHA8_EXT, 0 },
1331       { PIPE_FORMAT_R8G8B8A8_SRGB, DEFAULT_SRGBA_FORMATS }
1332    },
1333    {
1334       { GL_COMPRESSED_SRGB_EXT, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT, 0 },
1335       { PIPE_FORMAT_DXT1_SRGB, PIPE_FORMAT_R8G8B8X8_SRGB,
1336         PIPE_FORMAT_B8G8R8X8_SRGB, DEFAULT_SRGBA_FORMATS }
1337    },
1338    {
1339       { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0 },
1340       { PIPE_FORMAT_DXT1_SRGBA, 0 }
1341    },
1342    {
1343       { GL_COMPRESSED_SRGB_ALPHA_EXT,
1344         GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0 },
1345       { PIPE_FORMAT_DXT3_SRGBA, DEFAULT_SRGBA_FORMATS }
1346    },
1347    {
1348       { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0 },
1349       { PIPE_FORMAT_DXT5_SRGBA, 0 }
1350    },
1351    {
1352       { GL_SLUMINANCE_ALPHA_EXT, GL_SLUMINANCE8_ALPHA8_EXT,
1353         GL_COMPRESSED_SLUMINANCE_ALPHA_EXT, 0 },
1354       { PIPE_FORMAT_L8A8_SRGB, DEFAULT_SRGBA_FORMATS }
1355    },
1356    {
1357       { GL_SLUMINANCE_EXT, GL_SLUMINANCE8_EXT, GL_COMPRESSED_SLUMINANCE_EXT,
1358         0 },
1359       { PIPE_FORMAT_L8_SRGB, DEFAULT_SRGBA_FORMATS }
1360    },
1361 
1362    /* 16-bit float formats */
1363    {
1364       { GL_RGBA16F_ARB, 0 },
1365       { PIPE_FORMAT_R16G16B16A16_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1366    },
1367    {
1368       { GL_RGB16F_ARB, 0 },
1369       { PIPE_FORMAT_R16G16B16_FLOAT, PIPE_FORMAT_R16G16B16X16_FLOAT,
1370         PIPE_FORMAT_R16G16B16A16_FLOAT,
1371         PIPE_FORMAT_R32G32B32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1372    },
1373    {
1374       { GL_LUMINANCE_ALPHA16F_ARB, 0 },
1375       { PIPE_FORMAT_L16A16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1376         PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1377    },
1378    {
1379       { GL_ALPHA16F_ARB, 0 },
1380       { PIPE_FORMAT_A16_FLOAT, PIPE_FORMAT_L16A16_FLOAT,
1381         PIPE_FORMAT_A32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1382         PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1383    },
1384    {
1385       { GL_INTENSITY16F_ARB, 0 },
1386       { PIPE_FORMAT_I16_FLOAT, PIPE_FORMAT_L16A16_FLOAT,
1387         PIPE_FORMAT_I32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1388         PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1389    },
1390    {
1391       { GL_LUMINANCE16F_ARB, 0 },
1392       { PIPE_FORMAT_L16_FLOAT, PIPE_FORMAT_L16A16_FLOAT,
1393         PIPE_FORMAT_L32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1394         PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1395    },
1396    {
1397       { GL_R16F, 0 },
1398       { PIPE_FORMAT_R16_FLOAT, PIPE_FORMAT_R16G16_FLOAT,
1399         PIPE_FORMAT_R32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1400         PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1401    },
1402    {
1403       { GL_RG16F, 0 },
1404       { PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1405         PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1406    },
1407 
1408    /* 32-bit float formats */
1409    {
1410       { GL_RGBA32F_ARB, 0 },
1411       { PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1412    },
1413    {
1414       { GL_RGB32F_ARB, 0 },
1415       { PIPE_FORMAT_R32G32B32_FLOAT, PIPE_FORMAT_R32G32B32X32_FLOAT,
1416         PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1417    },
1418    {
1419       { GL_LUMINANCE_ALPHA32F_ARB, 0 },
1420       { PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1421    },
1422    {
1423       { GL_ALPHA32F_ARB, 0 },
1424       { PIPE_FORMAT_A32_FLOAT, PIPE_FORMAT_L32A32_FLOAT,
1425         PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1426    },
1427    {
1428       { GL_INTENSITY32F_ARB, 0 },
1429       { PIPE_FORMAT_I32_FLOAT, PIPE_FORMAT_L32A32_FLOAT,
1430         PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1431    },
1432    {
1433       { GL_LUMINANCE32F_ARB, 0 },
1434       { PIPE_FORMAT_L32_FLOAT, PIPE_FORMAT_L32A32_FLOAT,
1435         PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1436    },
1437    {
1438       { GL_R32F, 0 },
1439       { PIPE_FORMAT_R32_FLOAT, PIPE_FORMAT_R32G32_FLOAT,
1440         PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1441    },
1442    {
1443       { GL_RG32F, 0 },
1444       { PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1445    },
1446 
1447    /* R, RG formats */
1448    {
1449       { GL_RED, GL_R8, 0 },
1450       { PIPE_FORMAT_R8_UNORM, PIPE_FORMAT_R8G8_UNORM, DEFAULT_RGBA_FORMATS }
1451    },
1452    {
1453       { GL_RG, GL_RG8, 0 },
1454       { PIPE_FORMAT_R8G8_UNORM, DEFAULT_RGBA_FORMATS }
1455    },
1456    {
1457       { GL_R16, 0 },
1458       { PIPE_FORMAT_R16_UNORM, PIPE_FORMAT_R16G16_UNORM,
1459         DEFAULT_UNORM16_RGBA_FORMATS }
1460    },
1461    {
1462       { GL_RG16, 0 },
1463       { PIPE_FORMAT_R16G16_UNORM, DEFAULT_UNORM16_RGBA_FORMATS }
1464    },
1465 
1466    /* compressed R, RG formats */
1467    {
1468       { GL_COMPRESSED_RED, GL_COMPRESSED_RED_RGTC1, 0 },
1469       { PIPE_FORMAT_RGTC1_UNORM, PIPE_FORMAT_R8_UNORM, DEFAULT_RGBA_FORMATS }
1470    },
1471    {
1472       { GL_COMPRESSED_SIGNED_RED_RGTC1, 0 },
1473       { PIPE_FORMAT_RGTC1_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1474    },
1475    {
1476       { GL_COMPRESSED_RG, GL_COMPRESSED_RG_RGTC2, 0 },
1477       { PIPE_FORMAT_RGTC2_UNORM, PIPE_FORMAT_R8G8_UNORM, DEFAULT_RGBA_FORMATS }
1478    },
1479    {
1480       { GL_COMPRESSED_SIGNED_RG_RGTC2, 0 },
1481       { PIPE_FORMAT_RGTC2_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1482    },
1483    {
1484       { GL_COMPRESSED_LUMINANCE, GL_COMPRESSED_LUMINANCE_LATC1_EXT, 0 },
1485       { PIPE_FORMAT_LATC1_UNORM, PIPE_FORMAT_L8_UNORM, DEFAULT_RGBA_FORMATS }
1486    },
1487    {
1488       { GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT, 0 },
1489       { PIPE_FORMAT_LATC1_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1490    },
1491    {
1492       { GL_COMPRESSED_LUMINANCE_ALPHA, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT,
1493         GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, 0 },
1494       { PIPE_FORMAT_LATC2_UNORM, PIPE_FORMAT_L8A8_UNORM, DEFAULT_RGBA_FORMATS }
1495    },
1496    {
1497       { GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT, 0 },
1498       { PIPE_FORMAT_LATC2_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1499    },
1500 
1501    /* ETC1 */
1502    {
1503       { GL_ETC1_RGB8_OES, 0 },
1504       { PIPE_FORMAT_ETC1_RGB8, 0 }
1505    },
1506 
1507    /* ETC2 */
1508    {
1509       { GL_COMPRESSED_RGB8_ETC2, 0 },
1510       { PIPE_FORMAT_ETC2_RGB8, 0 }
1511    },
1512    {
1513       { GL_COMPRESSED_SRGB8_ETC2, 0 },
1514       { PIPE_FORMAT_ETC2_SRGB8, 0 }
1515    },
1516    {
1517       { GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, 0 },
1518       { PIPE_FORMAT_ETC2_RGB8A1, 0 }
1519    },
1520    {
1521       { GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, 0 },
1522       { PIPE_FORMAT_ETC2_SRGB8A1, 0 }
1523    },
1524    {
1525       { GL_COMPRESSED_RGBA8_ETC2_EAC, 0 },
1526       { PIPE_FORMAT_ETC2_RGBA8, 0 }
1527    },
1528    {
1529       { GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, 0 },
1530       { PIPE_FORMAT_ETC2_SRGBA8, 0 }
1531    },
1532    {
1533       { GL_COMPRESSED_R11_EAC, 0 },
1534       { PIPE_FORMAT_ETC2_R11_UNORM, 0 }
1535    },
1536    {
1537       { GL_COMPRESSED_SIGNED_R11_EAC, 0 },
1538       { PIPE_FORMAT_ETC2_R11_SNORM, 0 }
1539    },
1540    {
1541       { GL_COMPRESSED_RG11_EAC, 0 },
1542       { PIPE_FORMAT_ETC2_RG11_UNORM, 0 }
1543    },
1544    {
1545       { GL_COMPRESSED_SIGNED_RG11_EAC, 0 },
1546       { PIPE_FORMAT_ETC2_RG11_SNORM, 0 }
1547    },
1548 
1549    /* BPTC */
1550    {
1551       { GL_COMPRESSED_RGBA_BPTC_UNORM, 0 },
1552       { PIPE_FORMAT_BPTC_RGBA_UNORM, 0 },
1553    },
1554    {
1555       { GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, 0 },
1556       { PIPE_FORMAT_BPTC_SRGBA, 0 },
1557    },
1558    {
1559       { GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, 0 },
1560       { PIPE_FORMAT_BPTC_RGB_FLOAT, 0 },
1561    },
1562    {
1563       { GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, 0 },
1564       { PIPE_FORMAT_BPTC_RGB_UFLOAT, 0 },
1565    },
1566 
1567    /* ASTC */
1568    {
1569       { GL_COMPRESSED_RGBA_ASTC_4x4_KHR, 0 },
1570       { PIPE_FORMAT_ASTC_4x4, 0},
1571    },
1572    {
1573       { GL_COMPRESSED_RGBA_ASTC_5x4_KHR, 0 },
1574       { PIPE_FORMAT_ASTC_5x4, 0},
1575    },
1576    {
1577       { GL_COMPRESSED_RGBA_ASTC_5x5_KHR, 0 },
1578       { PIPE_FORMAT_ASTC_5x5, 0},
1579    },
1580    {
1581       { GL_COMPRESSED_RGBA_ASTC_6x5_KHR, 0 },
1582       { PIPE_FORMAT_ASTC_6x5, 0},
1583    },
1584    {
1585       { GL_COMPRESSED_RGBA_ASTC_6x6_KHR, 0 },
1586       { PIPE_FORMAT_ASTC_6x6, 0},
1587    },
1588    {
1589       { GL_COMPRESSED_RGBA_ASTC_8x5_KHR, 0 },
1590       { PIPE_FORMAT_ASTC_8x5, 0},
1591    },
1592    {
1593       { GL_COMPRESSED_RGBA_ASTC_8x6_KHR, 0 },
1594       { PIPE_FORMAT_ASTC_8x6, 0},
1595    },
1596    {
1597       { GL_COMPRESSED_RGBA_ASTC_8x8_KHR, 0 },
1598       { PIPE_FORMAT_ASTC_8x8, 0},
1599    },
1600    {
1601       { GL_COMPRESSED_RGBA_ASTC_10x5_KHR, 0 },
1602       { PIPE_FORMAT_ASTC_10x5, 0},
1603    },
1604    {
1605       { GL_COMPRESSED_RGBA_ASTC_10x6_KHR, 0 },
1606       { PIPE_FORMAT_ASTC_10x6, 0},
1607    },
1608    {
1609       { GL_COMPRESSED_RGBA_ASTC_10x8_KHR, 0 },
1610       { PIPE_FORMAT_ASTC_10x8, 0},
1611    },
1612    {
1613       { GL_COMPRESSED_RGBA_ASTC_10x10_KHR, 0 },
1614       { PIPE_FORMAT_ASTC_10x10, 0},
1615    },
1616    {
1617       { GL_COMPRESSED_RGBA_ASTC_12x10_KHR, 0 },
1618       { PIPE_FORMAT_ASTC_12x10, 0},
1619    },
1620    {
1621       { GL_COMPRESSED_RGBA_ASTC_12x12_KHR, 0 },
1622       { PIPE_FORMAT_ASTC_12x12, 0},
1623    },
1624 
1625    {
1626       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, 0 },
1627       { PIPE_FORMAT_ASTC_4x4_SRGB, 0},
1628    },
1629    {
1630       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, 0 },
1631       { PIPE_FORMAT_ASTC_5x4_SRGB, 0},
1632    },
1633    {
1634       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, 0 },
1635       { PIPE_FORMAT_ASTC_5x5_SRGB, 0},
1636    },
1637    {
1638       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, 0 },
1639       { PIPE_FORMAT_ASTC_6x5_SRGB, 0},
1640    },
1641    {
1642       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, 0 },
1643       { PIPE_FORMAT_ASTC_6x6_SRGB, 0},
1644    },
1645    {
1646       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, 0 },
1647       { PIPE_FORMAT_ASTC_8x5_SRGB, 0},
1648    },
1649    {
1650       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, 0 },
1651       { PIPE_FORMAT_ASTC_8x6_SRGB, 0},
1652    },
1653    {
1654       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, 0 },
1655       { PIPE_FORMAT_ASTC_8x8_SRGB, 0},
1656    },
1657    {
1658       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, 0 },
1659       { PIPE_FORMAT_ASTC_10x5_SRGB, 0},
1660    },
1661    {
1662       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, 0 },
1663       { PIPE_FORMAT_ASTC_10x6_SRGB, 0},
1664    },
1665    {
1666       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, 0 },
1667       { PIPE_FORMAT_ASTC_10x8_SRGB, 0},
1668    },
1669    {
1670       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, 0 },
1671       { PIPE_FORMAT_ASTC_10x10_SRGB, 0},
1672    },
1673    {
1674       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, 0 },
1675       { PIPE_FORMAT_ASTC_12x10_SRGB, 0},
1676    },
1677    {
1678       { GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, 0 },
1679       { PIPE_FORMAT_ASTC_12x12_SRGB, 0},
1680    },
1681 
1682    /* signed/unsigned integer formats.
1683     */
1684    {
1685       { GL_RGBA_INTEGER_EXT,
1686         GL_BGRA_INTEGER_EXT,
1687         GL_RGBA8I_EXT, 0 },
1688       { PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1689    },
1690    {
1691       { GL_RGB_INTEGER_EXT,
1692         GL_BGR_INTEGER_EXT,
1693         GL_RGB8I_EXT,
1694         GL_BLUE_INTEGER_EXT, 0 },
1695       { PIPE_FORMAT_R8G8B8_SINT, PIPE_FORMAT_R8G8B8X8_SINT,
1696         PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1697    },
1698    {
1699       { GL_ALPHA_INTEGER_EXT,
1700         GL_ALPHA8I_EXT, 0 },
1701       { PIPE_FORMAT_A8_SINT, PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1702    },
1703    {
1704       { GL_ALPHA16I_EXT, 0 },
1705       { PIPE_FORMAT_A16_SINT, PIPE_FORMAT_R16G16B16A16_SINT, 0 }
1706    },
1707    {
1708       { GL_ALPHA32I_EXT, 0 },
1709       { PIPE_FORMAT_A32_SINT, PIPE_FORMAT_R32G32B32A32_SINT, 0 }
1710    },
1711    {
1712       { GL_ALPHA8UI_EXT, 0 },
1713       { PIPE_FORMAT_A8_UINT, PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1714    },
1715    {
1716       { GL_ALPHA16UI_EXT, 0 },
1717       { PIPE_FORMAT_A16_UINT, PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1718    },
1719    {
1720       { GL_ALPHA32UI_EXT, 0 },
1721       { PIPE_FORMAT_A32_UINT, PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1722    },
1723    {
1724       { GL_INTENSITY8I_EXT, 0 },
1725       { PIPE_FORMAT_I8_SINT, PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1726    },
1727    {
1728       { GL_INTENSITY16I_EXT, 0 },
1729       { PIPE_FORMAT_I16_SINT, PIPE_FORMAT_R16G16B16A16_SINT, 0 }
1730    },
1731    {
1732       { GL_INTENSITY32I_EXT, 0 },
1733       { PIPE_FORMAT_I32_SINT, PIPE_FORMAT_R32G32B32A32_SINT, 0 }
1734    },
1735    {
1736       { GL_INTENSITY8UI_EXT, 0 },
1737       { PIPE_FORMAT_I8_UINT, PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1738    },
1739    {
1740       { GL_INTENSITY16UI_EXT, 0 },
1741       { PIPE_FORMAT_I16_UINT, PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1742    },
1743    {
1744       { GL_INTENSITY32UI_EXT, 0 },
1745       { PIPE_FORMAT_I32_UINT, PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1746    },
1747    {
1748       { GL_LUMINANCE8I_EXT, 0 },
1749       { PIPE_FORMAT_L8_SINT, PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1750    },
1751    {
1752       { GL_LUMINANCE16I_EXT, 0 },
1753       { PIPE_FORMAT_L16_SINT, PIPE_FORMAT_R16G16B16A16_SINT, 0 }
1754    },
1755    {
1756       { GL_LUMINANCE32I_EXT, 0 },
1757       { PIPE_FORMAT_L32_SINT, PIPE_FORMAT_R32G32B32A32_SINT, 0 }
1758    },
1759    {
1760       { GL_LUMINANCE_INTEGER_EXT,
1761         GL_LUMINANCE8UI_EXT, 0 },
1762       { PIPE_FORMAT_L8_UINT, PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1763    },
1764    {
1765       { GL_LUMINANCE16UI_EXT, 0 },
1766       { PIPE_FORMAT_L16_UINT, PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1767    },
1768    {
1769       { GL_LUMINANCE32UI_EXT, 0 },
1770       { PIPE_FORMAT_L32_UINT, PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1771    },
1772    {
1773       { GL_LUMINANCE_ALPHA_INTEGER_EXT,
1774         GL_LUMINANCE_ALPHA8I_EXT, 0 },
1775       { PIPE_FORMAT_L8A8_SINT, PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1776    },
1777    {
1778       { GL_LUMINANCE_ALPHA16I_EXT, 0 },
1779       { PIPE_FORMAT_L16A16_SINT, PIPE_FORMAT_R16G16B16A16_SINT, 0 }
1780    },
1781    {
1782       { GL_LUMINANCE_ALPHA32I_EXT, 0 },
1783       { PIPE_FORMAT_L32A32_SINT, PIPE_FORMAT_R32G32B32A32_SINT, 0 }
1784    },
1785    {
1786       { GL_LUMINANCE_ALPHA8UI_EXT, 0 },
1787       { PIPE_FORMAT_L8A8_UINT, PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1788    },
1789    {
1790       { GL_LUMINANCE_ALPHA16UI_EXT, 0 },
1791       { PIPE_FORMAT_L16A16_UINT, PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1792    },
1793    {
1794       { GL_LUMINANCE_ALPHA32UI_EXT, 0 },
1795       { PIPE_FORMAT_L32A32_UINT, PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1796    },
1797    {
1798       { GL_RGB16I_EXT, 0 },
1799       { PIPE_FORMAT_R16G16B16_SINT, PIPE_FORMAT_R16G16B16X16_SINT,
1800         PIPE_FORMAT_R16G16B16A16_SINT, 0 },
1801    },
1802    {
1803       { GL_RGBA16I_EXT, 0 },
1804       { PIPE_FORMAT_R16G16B16A16_SINT, 0 },
1805    },
1806    {
1807       { GL_RGB32I_EXT, 0 },
1808       { PIPE_FORMAT_R32G32B32_SINT, PIPE_FORMAT_R32G32B32X32_SINT,
1809         PIPE_FORMAT_R32G32B32A32_SINT, 0 },
1810    },
1811    {
1812       { GL_RGBA32I_EXT, 0 },
1813       { PIPE_FORMAT_R32G32B32A32_SINT, 0 }
1814    },
1815    {
1816       { GL_RGBA8UI_EXT, 0 },
1817       { PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1818    },
1819    {
1820       { GL_RGB8UI_EXT, 0 },
1821       { PIPE_FORMAT_R8G8B8_UINT, PIPE_FORMAT_R8G8B8X8_UINT,
1822         PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1823    },
1824    {
1825       { GL_RGB16UI_EXT, 0 },
1826       { PIPE_FORMAT_R16G16B16_UINT, PIPE_FORMAT_R16G16B16X16_UINT,
1827         PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1828    },
1829    {
1830       { GL_RGBA16UI_EXT, 0 },
1831       { PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1832    },
1833    {
1834       { GL_RGB32UI_EXT, 0},
1835       { PIPE_FORMAT_R32G32B32_UINT, PIPE_FORMAT_R32G32B32X32_UINT,
1836         PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1837    },
1838    {
1839       { GL_RGBA32UI_EXT, 0},
1840       { PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1841    },
1842    {
1843      { GL_R8I, GL_RED_INTEGER_EXT, 0},
1844      { PIPE_FORMAT_R8_SINT, 0},
1845    },
1846    {
1847      { GL_R16I, 0},
1848      { PIPE_FORMAT_R16_SINT, 0},
1849    },
1850    {
1851      { GL_R32I, 0},
1852      { PIPE_FORMAT_R32_SINT, 0},
1853    },
1854   {
1855      { GL_R8UI, 0},
1856      { PIPE_FORMAT_R8_UINT, 0},
1857    },
1858    {
1859      { GL_R16UI, 0},
1860      { PIPE_FORMAT_R16_UINT, 0},
1861    },
1862    {
1863      { GL_R32UI, 0},
1864      { PIPE_FORMAT_R32_UINT, 0},
1865    },
1866    {
1867      { GL_RG8I, GL_GREEN_INTEGER_EXT, 0},
1868      { PIPE_FORMAT_R8G8_SINT, 0},
1869    },
1870    {
1871      { GL_RG16I, 0},
1872      { PIPE_FORMAT_R16G16_SINT, 0},
1873    },
1874    {
1875      { GL_RG32I, 0},
1876      { PIPE_FORMAT_R32G32_SINT, 0},
1877    },
1878   {
1879      { GL_RG8UI, 0},
1880      { PIPE_FORMAT_R8G8_UINT, 0},
1881    },
1882    {
1883      { GL_RG16UI, 0},
1884      { PIPE_FORMAT_R16G16_UINT, 0},
1885    },
1886    {
1887      { GL_RG32UI, 0},
1888      { PIPE_FORMAT_R32G32_UINT, 0},
1889    },
1890    /* signed normalized formats */
1891    {
1892       { GL_RED_SNORM, GL_R8_SNORM, 0 },
1893       { PIPE_FORMAT_R8_SNORM, PIPE_FORMAT_R8G8_SNORM,
1894         PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1895    },
1896    {
1897       { GL_R16_SNORM, 0 },
1898       { PIPE_FORMAT_R16_SNORM,
1899         PIPE_FORMAT_R16G16_SNORM,
1900         PIPE_FORMAT_R16G16B16A16_SNORM,
1901         PIPE_FORMAT_R8_SNORM,
1902         PIPE_FORMAT_R8G8_SNORM,
1903         PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1904    },
1905    {
1906       { GL_RG_SNORM, GL_RG8_SNORM, 0 },
1907       { PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1908    },
1909    {
1910       { GL_RG16_SNORM, 0 },
1911       { PIPE_FORMAT_R16G16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1912         PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1913    },
1914    {
1915       { GL_RGB_SNORM, GL_RGB8_SNORM, 0 },
1916       { PIPE_FORMAT_R8G8B8X8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1917    },
1918    {
1919       { GL_RGBA_SNORM, GL_RGBA8_SNORM, 0 },
1920       { PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1921    },
1922    {
1923       { GL_RGB16_SNORM, 0 },
1924       { PIPE_FORMAT_R16G16B16X16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1925         PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1926    },
1927    {
1928       { GL_RGBA16_SNORM, 0 },
1929       { PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1930    },
1931    {
1932       { GL_ALPHA_SNORM, GL_ALPHA8_SNORM, 0 },
1933       { PIPE_FORMAT_A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1934    },
1935    {
1936       { GL_ALPHA16_SNORM, 0 },
1937       { PIPE_FORMAT_A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1938         PIPE_FORMAT_A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1939    },
1940    {
1941       { GL_LUMINANCE_SNORM, GL_LUMINANCE8_SNORM, 0 },
1942       { PIPE_FORMAT_L8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1943    },
1944    {
1945       { GL_LUMINANCE16_SNORM, 0 },
1946       { PIPE_FORMAT_L16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1947         PIPE_FORMAT_L8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1948    },
1949    {
1950       { GL_LUMINANCE_ALPHA_SNORM, GL_LUMINANCE8_ALPHA8_SNORM, 0 },
1951       { PIPE_FORMAT_L8A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1952    },
1953    {
1954       { GL_LUMINANCE16_ALPHA16_SNORM, 0 },
1955       { PIPE_FORMAT_L16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1956         PIPE_FORMAT_L8A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1957    },
1958    {
1959       { GL_INTENSITY_SNORM, GL_INTENSITY8_SNORM, 0 },
1960       { PIPE_FORMAT_I8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1961    },
1962    {
1963       { GL_INTENSITY16_SNORM, 0 },
1964       { PIPE_FORMAT_I16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1965         PIPE_FORMAT_I8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1966    },
1967    {
1968       { GL_RGB9_E5, 0 },
1969       { PIPE_FORMAT_R9G9B9E5_FLOAT, 0 }
1970    },
1971    {
1972       { GL_R11F_G11F_B10F, 0 },
1973       { PIPE_FORMAT_R11G11B10_FLOAT, 0 }
1974    },
1975    {
1976       { GL_RGB10_A2UI, 0 },
1977       { PIPE_FORMAT_R10G10B10A2_UINT, PIPE_FORMAT_B10G10R10A2_UINT, 0 }
1978    },
1979 };
1980 
1981 
1982 /**
1983  * Return first supported format from the given list.
1984  * \param allow_dxt  indicates whether it's OK to return a DXT format.
1985  */
1986 static enum pipe_format
find_supported_format(struct pipe_screen * screen,const enum pipe_format formats[],enum pipe_texture_target target,unsigned sample_count,unsigned bindings,boolean allow_dxt)1987 find_supported_format(struct pipe_screen *screen,
1988                       const enum pipe_format formats[],
1989                       enum pipe_texture_target target,
1990                       unsigned sample_count,
1991                       unsigned bindings,
1992                       boolean allow_dxt)
1993 {
1994    uint i;
1995    for (i = 0; formats[i]; i++) {
1996       if (screen->is_format_supported(screen, formats[i], target,
1997                                       sample_count, bindings)) {
1998          if (!allow_dxt && util_format_is_s3tc(formats[i])) {
1999             /* we can't return a dxt format, continue searching */
2000             continue;
2001          }
2002 
2003          return formats[i];
2004       }
2005    }
2006    return PIPE_FORMAT_NONE;
2007 }
2008 
2009 
2010 struct exact_format_mapping
2011 {
2012    GLenum format;
2013    GLenum type;
2014    enum pipe_format pformat;
2015 };
2016 
2017 static const struct exact_format_mapping rgba8888_tbl[] =
2018 {
2019    { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_ABGR8888_UNORM },
2020    { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_ABGR8888_UNORM },
2021    { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_RGBA8888_UNORM },
2022    { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_RGBA8888_UNORM },
2023    { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_ARGB8888_UNORM },
2024    { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_BGRA8888_UNORM },
2025    { GL_RGBA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_R8G8B8A8_UNORM },
2026    { GL_ABGR_EXT, GL_UNSIGNED_BYTE,               PIPE_FORMAT_A8B8G8R8_UNORM },
2027    { GL_BGRA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_B8G8R8A8_UNORM },
2028    { 0,           0,                              0                          }
2029 };
2030 
2031 static const struct exact_format_mapping rgbx8888_tbl[] =
2032 {
2033    { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_XBGR8888_UNORM },
2034    { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_XBGR8888_UNORM },
2035    { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_RGBX8888_UNORM },
2036    { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_RGBX8888_UNORM },
2037    { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_XRGB8888_UNORM },
2038    { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_BGRX8888_UNORM },
2039    { GL_RGBA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_R8G8B8X8_UNORM },
2040    { GL_ABGR_EXT, GL_UNSIGNED_BYTE,               PIPE_FORMAT_X8B8G8R8_UNORM },
2041    { GL_BGRA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_B8G8R8X8_UNORM },
2042    { 0,           0,                              0                          }
2043 };
2044 
2045 
2046 /**
2047  * For unsized/base internal formats, we may choose a convenient effective
2048  * internal format for {format, type}. If one exists, return that, otherwise
2049  * return PIPE_FORMAT_NONE.
2050  */
2051 static enum pipe_format
find_exact_format(GLint internalFormat,GLenum format,GLenum type)2052 find_exact_format(GLint internalFormat, GLenum format, GLenum type)
2053 {
2054    uint i;
2055    const struct exact_format_mapping* tbl;
2056 
2057    if (format == GL_NONE || type == GL_NONE)
2058       return PIPE_FORMAT_NONE;
2059 
2060    switch (internalFormat) {
2061    case 4:
2062    case GL_RGBA:
2063       tbl = rgba8888_tbl;
2064       break;
2065    case 3:
2066    case GL_RGB:
2067       tbl = rgbx8888_tbl;
2068       break;
2069    default:
2070       return PIPE_FORMAT_NONE;
2071    }
2072 
2073    for (i = 0; tbl[i].format; i++)
2074       if (tbl[i].format == format && tbl[i].type == type)
2075          return tbl[i].pformat;
2076 
2077    return PIPE_FORMAT_NONE;
2078 }
2079 
2080 
2081 /**
2082  * Given an OpenGL internalFormat value for a texture or surface, return
2083  * the best matching PIPE_FORMAT_x, or PIPE_FORMAT_NONE if there's no match.
2084  * This is called during glTexImage2D, for example.
2085  *
2086  * The bindings parameter typically has PIPE_BIND_SAMPLER_VIEW set, plus
2087  * either PIPE_BINDING_RENDER_TARGET or PIPE_BINDING_DEPTH_STENCIL if
2088  * we want render-to-texture ability.
2089  *
2090  * \param internalFormat  the user value passed to glTexImage2D
2091  * \param target  one of PIPE_TEXTURE_x
2092  * \param bindings  bitmask of PIPE_BIND_x flags.
2093  * \param allow_dxt  indicates whether it's OK to return a DXT format.  This
2094  *                   only matters when internalFormat names a generic or
2095  *                   specific compressed format.  And that should only happen
2096  *                   when we're getting called from gl[Copy]TexImage().
2097  */
2098 enum pipe_format
st_choose_format(struct st_context * st,GLenum internalFormat,GLenum format,GLenum type,enum pipe_texture_target target,unsigned sample_count,unsigned bindings,boolean allow_dxt)2099 st_choose_format(struct st_context *st, GLenum internalFormat,
2100                  GLenum format, GLenum type,
2101                  enum pipe_texture_target target, unsigned sample_count,
2102                  unsigned bindings, boolean allow_dxt)
2103 {
2104    struct pipe_screen *screen = st->pipe->screen;
2105    unsigned i;
2106    int j;
2107    enum pipe_format pf;
2108 
2109 #ifdef DEBUG
2110    {
2111       static boolean firstCall = TRUE;
2112       if (firstCall) {
2113          test_format_conversion(st);
2114          firstCall = FALSE;
2115       }
2116    }
2117 #else
2118    (void) test_format_conversion;
2119 #endif
2120 
2121    /* can't render to compressed formats at this time */
2122    if (_mesa_is_compressed_format(st->ctx, internalFormat)
2123        && (bindings & ~PIPE_BIND_SAMPLER_VIEW)) {
2124       return PIPE_FORMAT_NONE;
2125    }
2126 
2127    /* search for exact matches */
2128    pf = find_exact_format(internalFormat, format, type);
2129    if (pf != PIPE_FORMAT_NONE &&
2130        screen->is_format_supported(screen, pf,
2131                                    target, sample_count, bindings))
2132       return pf;
2133 
2134    /* search table for internalFormat */
2135    for (i = 0; i < ARRAY_SIZE(format_map); i++) {
2136       const struct format_mapping *mapping = &format_map[i];
2137       for (j = 0; mapping->glFormats[j]; j++) {
2138          if (mapping->glFormats[j] == internalFormat) {
2139             /* Found the desired internal format.  Find first pipe format
2140              * which is supported by the driver.
2141              */
2142             return find_supported_format(screen, mapping->pipeFormats,
2143                                          target, sample_count, bindings,
2144                                          allow_dxt);
2145          }
2146       }
2147    }
2148 
2149    _mesa_problem(NULL, "unhandled format!\n");
2150    return PIPE_FORMAT_NONE;
2151 }
2152 
2153 
2154 /**
2155  * Called by FBO code to choose a PIPE_FORMAT_ for drawing surfaces.
2156  */
2157 enum pipe_format
st_choose_renderbuffer_format(struct st_context * st,GLenum internalFormat,unsigned sample_count)2158 st_choose_renderbuffer_format(struct st_context *st,
2159                               GLenum internalFormat, unsigned sample_count)
2160 {
2161    unsigned bindings;
2162    if (_mesa_is_depth_or_stencil_format(internalFormat))
2163       bindings = PIPE_BIND_DEPTH_STENCIL;
2164    else
2165       bindings = PIPE_BIND_RENDER_TARGET;
2166    return st_choose_format(st, internalFormat, GL_NONE, GL_NONE,
2167                            PIPE_TEXTURE_2D, sample_count, bindings, FALSE);
2168 }
2169 
2170 
2171 /**
2172  * Given an OpenGL user-requested format and type, and swapBytes state,
2173  * return the format which exactly matches those parameters, so that
2174  * a memcpy-based transfer can be done.
2175  *
2176  * If no format is supported, return PIPE_FORMAT_NONE.
2177  */
2178 enum pipe_format
st_choose_matching_format(struct st_context * st,unsigned bind,GLenum format,GLenum type,GLboolean swapBytes)2179 st_choose_matching_format(struct st_context *st, unsigned bind,
2180                           GLenum format, GLenum type, GLboolean swapBytes)
2181 {
2182    struct pipe_screen *screen = st->pipe->screen;
2183    mesa_format mesa_format;
2184 
2185    for (mesa_format = 1; mesa_format < MESA_FORMAT_COUNT; mesa_format++) {
2186       if (_mesa_get_format_color_encoding(mesa_format) == GL_SRGB) {
2187          continue;
2188       }
2189       if (_mesa_get_format_bits(mesa_format, GL_TEXTURE_INTENSITY_SIZE) > 0) {
2190          /* If `format` is GL_RED/GL_RED_INTEGER, then we might match some
2191           * intensity formats, which we don't want.
2192           */
2193          continue;
2194       }
2195 
2196       if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2197                                                swapBytes, NULL)) {
2198          enum pipe_format format =
2199             st_mesa_format_to_pipe_format(st, mesa_format);
2200 
2201          if (format &&
2202              screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0,
2203                                          bind)) {
2204             return format;
2205          }
2206          /* It's unlikely to find 2 matching Mesa formats. */
2207          break;
2208       }
2209    }
2210    return PIPE_FORMAT_NONE;
2211 }
2212 
2213 
2214 /**
2215  * Called via ctx->Driver.ChooseTextureFormat().
2216  */
2217 mesa_format
st_ChooseTextureFormat(struct gl_context * ctx,GLenum target,GLint internalFormat,GLenum format,GLenum type)2218 st_ChooseTextureFormat(struct gl_context *ctx, GLenum target,
2219                        GLint internalFormat,
2220                        GLenum format, GLenum type)
2221 {
2222    struct st_context *st = st_context(ctx);
2223    enum pipe_format pFormat;
2224    mesa_format mFormat;
2225    unsigned bindings;
2226    bool is_renderbuffer = false;
2227    enum pipe_texture_target pTarget;
2228 
2229    if (target == GL_RENDERBUFFER) {
2230       pTarget = PIPE_TEXTURE_2D;
2231       is_renderbuffer = true;
2232    } else {
2233       pTarget = gl_target_to_pipe(target);
2234    }
2235 
2236    if (target == GL_TEXTURE_1D || target == GL_TEXTURE_1D_ARRAY) {
2237       /* We don't do compression for these texture targets because of
2238        * difficulty with sub-texture updates on non-block boundaries, etc.
2239        * So change the internal format request to an uncompressed format.
2240        */
2241       internalFormat =
2242         _mesa_generic_compressed_format_to_uncompressed_format(internalFormat);
2243    }
2244 
2245    /* GL textures may wind up being render targets, but we don't know
2246     * that in advance.  Specify potential render target flags now for formats
2247     * that we know should always be renderable.
2248     */
2249    bindings = PIPE_BIND_SAMPLER_VIEW;
2250    if (_mesa_is_depth_or_stencil_format(internalFormat))
2251       bindings |= PIPE_BIND_DEPTH_STENCIL;
2252    else if (is_renderbuffer || internalFormat == 3 || internalFormat == 4 ||
2253             internalFormat == GL_RGB || internalFormat == GL_RGBA ||
2254             internalFormat == GL_RGB8 || internalFormat == GL_RGBA8 ||
2255             internalFormat == GL_BGRA ||
2256             internalFormat == GL_RGB16F ||
2257             internalFormat == GL_RGBA16F ||
2258             internalFormat == GL_RGB32F ||
2259             internalFormat == GL_RGBA32F)
2260       bindings |= PIPE_BIND_RENDER_TARGET;
2261 
2262    /* GLES allows the driver to choose any format which matches
2263     * the format+type combo, because GLES only supports unsized internal
2264     * formats and expects the driver to choose whatever suits it.
2265     */
2266    if (_mesa_is_gles(ctx)) {
2267       GLenum baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2268       GLenum basePackFormat = _mesa_base_pack_format(format);
2269       GLenum iformat = internalFormat;
2270 
2271       /* Treat GL_BGRA as GL_RGBA. */
2272       if (iformat == GL_BGRA)
2273          iformat = GL_RGBA;
2274 
2275       /* Check if the internalformat is unsized and compatible
2276        * with the "format".
2277        */
2278       if (iformat == baseFormat && iformat == basePackFormat) {
2279          pFormat = st_choose_matching_format(st, bindings, format, type,
2280                                              ctx->Unpack.SwapBytes);
2281 
2282          if (pFormat != PIPE_FORMAT_NONE)
2283             return st_pipe_format_to_mesa_format(pFormat);
2284 
2285          if (!is_renderbuffer) {
2286             /* try choosing format again, this time without render
2287              * target bindings.
2288              */
2289             pFormat = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
2290                                                 format, type,
2291                                                 ctx->Unpack.SwapBytes);
2292             if (pFormat != PIPE_FORMAT_NONE)
2293                return st_pipe_format_to_mesa_format(pFormat);
2294          }
2295       }
2296    }
2297 
2298    pFormat = st_choose_format(st, internalFormat, format, type,
2299                               pTarget, 0, bindings, GL_TRUE);
2300 
2301    if (pFormat == PIPE_FORMAT_NONE && !is_renderbuffer) {
2302       /* try choosing format again, this time without render target bindings */
2303       pFormat = st_choose_format(st, internalFormat, format, type,
2304                                  pTarget, 0, PIPE_BIND_SAMPLER_VIEW,
2305                                  GL_TRUE);
2306    }
2307 
2308    if (pFormat == PIPE_FORMAT_NONE) {
2309       /* lie about using etc1/etc2 natively if we do decoding tricks */
2310       mFormat = _mesa_glenum_to_compressed_format(internalFormat);
2311       if ((mFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1) ||
2312           (_mesa_is_format_etc2(mFormat) && !st->has_etc2))
2313           return mFormat;
2314 
2315       /* no luck at all */
2316       return MESA_FORMAT_NONE;
2317    }
2318 
2319    mFormat = st_pipe_format_to_mesa_format(pFormat);
2320 
2321    /* Debugging aid */
2322    if (0) {
2323       debug_printf("%s(intFormat=%s, format=%s, type=%s) -> %s, %s\n",
2324                    __func__,
2325                    _mesa_enum_to_string(internalFormat),
2326                    _mesa_enum_to_string(format),
2327                    _mesa_enum_to_string(type),
2328                    util_format_name(pFormat),
2329                    _mesa_get_format_name(mFormat));
2330    }
2331 
2332    return mFormat;
2333 }
2334 
2335 
2336 /**
2337  * Called via ctx->Driver.QueryInternalFormat().
2338  */
2339 static size_t
st_QuerySamplesForFormat(struct gl_context * ctx,GLenum target,GLenum internalFormat,int samples[16])2340 st_QuerySamplesForFormat(struct gl_context *ctx, GLenum target,
2341                          GLenum internalFormat, int samples[16])
2342 {
2343    struct st_context *st = st_context(ctx);
2344    enum pipe_format format;
2345    unsigned i, bind, num_sample_counts = 0;
2346 
2347    (void) target;
2348 
2349    if (_mesa_is_depth_or_stencil_format(internalFormat))
2350       bind = PIPE_BIND_DEPTH_STENCIL;
2351    else
2352       bind = PIPE_BIND_RENDER_TARGET;
2353 
2354    /* If an sRGB framebuffer is unsupported, sRGB formats behave like linear
2355     * formats.
2356     */
2357    if (!ctx->Extensions.EXT_framebuffer_sRGB) {
2358       internalFormat = _mesa_get_linear_internalformat(internalFormat);
2359    }
2360 
2361    /* Set sample counts in descending order. */
2362    for (i = 16; i > 1; i--) {
2363       format = st_choose_format(st, internalFormat, GL_NONE, GL_NONE,
2364                                 PIPE_TEXTURE_2D, i, bind, FALSE);
2365 
2366       if (format != PIPE_FORMAT_NONE) {
2367          samples[num_sample_counts++] = i;
2368       }
2369    }
2370 
2371    if (!num_sample_counts) {
2372       samples[num_sample_counts++] = 1;
2373    }
2374 
2375    return num_sample_counts;
2376 }
2377 
2378 
2379 /**
2380  * ARB_internalformat_query2 driver hook.
2381  */
2382 void
st_QueryInternalFormat(struct gl_context * ctx,GLenum target,GLenum internalFormat,GLenum pname,GLint * params)2383 st_QueryInternalFormat(struct gl_context *ctx, GLenum target,
2384                        GLenum internalFormat, GLenum pname, GLint *params)
2385 {
2386    struct st_context *st = st_context(ctx);
2387    /* The API entry-point gives us a temporary params buffer that is non-NULL
2388     * and guaranteed to have at least 16 elements.
2389     */
2390    assert(params != NULL);
2391 
2392    switch (pname) {
2393    case GL_SAMPLES:
2394       st_QuerySamplesForFormat(ctx, target, internalFormat, params);
2395       break;
2396 
2397    case GL_NUM_SAMPLE_COUNTS: {
2398       int samples[16];
2399       size_t num_samples;
2400       num_samples = st_QuerySamplesForFormat(ctx, target, internalFormat,
2401                                              samples);
2402       params[0] = (GLint) num_samples;
2403       break;
2404    }
2405    case GL_INTERNALFORMAT_PREFERRED: {
2406       params[0] = GL_NONE;
2407 
2408       /* We need to resolve an internal format that is compatible with
2409        * the passed internal format, and optimal to the driver. By now,
2410        * we just validate that the passed internal format is supported by
2411        * the driver, and if so return the same internal format, otherwise
2412        * return GL_NONE.
2413        */
2414       unsigned bindings;
2415       if (_mesa_is_depth_or_stencil_format(internalFormat))
2416          bindings = PIPE_BIND_DEPTH_STENCIL;
2417       else
2418          bindings = PIPE_BIND_RENDER_TARGET;
2419       enum pipe_format pformat = st_choose_format(st,
2420                                                   internalFormat,
2421                                                   GL_NONE,
2422                                                   GL_NONE,
2423                                                   PIPE_TEXTURE_2D, 1,
2424                                                   bindings, FALSE);
2425       if (pformat)
2426          params[0] = internalFormat;
2427       break;
2428    }
2429    default:
2430       /* For the rest of the pnames, we call back the Mesa's default
2431        * function for drivers that don't implement ARB_internalformat_query2.
2432        */
2433       _mesa_query_internal_format_default(ctx, target, internalFormat, pname,
2434                                           params);
2435    }
2436 }
2437 
2438 
2439 /**
2440  * This is used for translating texture border color and the clear
2441  * color.  For example, the clear color is interpreted according to
2442  * the renderbuffer's base format.  For example, if clearing a
2443  * GL_LUMINANCE buffer, we'll return colorOut[0] = colorOut[1] =
2444  * colorOut[2] = colorIn[0].
2445  * Similarly for texture border colors.
2446  */
2447 void
st_translate_color(const union gl_color_union * colorIn,union pipe_color_union * colorOut,GLenum baseFormat,GLboolean is_integer)2448 st_translate_color(const union gl_color_union *colorIn,
2449                    union pipe_color_union *colorOut,
2450                    GLenum baseFormat, GLboolean is_integer)
2451 {
2452    if (is_integer) {
2453       const int *in = colorIn->i;
2454       int *out = colorOut->i;
2455 
2456       switch (baseFormat) {
2457       case GL_RED:
2458          out[0] = in[0];
2459          out[1] = 0;
2460          out[2] = 0;
2461          out[3] = 1;
2462          break;
2463       case GL_RG:
2464          out[0] = in[0];
2465          out[1] = in[1];
2466          out[2] = 0;
2467          out[3] = 1;
2468          break;
2469       case GL_RGB:
2470          out[0] = in[0];
2471          out[1] = in[1];
2472          out[2] = in[2];
2473          out[3] = 1;
2474          break;
2475       case GL_ALPHA:
2476          out[0] = out[1] = out[2] = 0;
2477          out[3] = in[3];
2478          break;
2479       case GL_LUMINANCE:
2480          out[0] = out[1] = out[2] = in[0];
2481          out[3] = 1;
2482          break;
2483       case GL_LUMINANCE_ALPHA:
2484          out[0] = out[1] = out[2] = in[0];
2485          out[3] = in[3];
2486          break;
2487       case GL_INTENSITY:
2488          out[0] = out[1] = out[2] = out[3] = in[0];
2489          break;
2490       default:
2491          COPY_4V(out, in);
2492       }
2493    }
2494    else {
2495       const float *in = colorIn->f;
2496       float *out = colorOut->f;
2497 
2498       switch (baseFormat) {
2499       case GL_RED:
2500          out[0] = in[0];
2501          out[1] = 0.0F;
2502          out[2] = 0.0F;
2503          out[3] = 1.0F;
2504          break;
2505       case GL_RG:
2506          out[0] = in[0];
2507          out[1] = in[1];
2508          out[2] = 0.0F;
2509          out[3] = 1.0F;
2510          break;
2511       case GL_RGB:
2512          out[0] = in[0];
2513          out[1] = in[1];
2514          out[2] = in[2];
2515          out[3] = 1.0F;
2516          break;
2517       case GL_ALPHA:
2518          out[0] = out[1] = out[2] = 0.0F;
2519          out[3] = in[3];
2520          break;
2521       case GL_LUMINANCE:
2522          out[0] = out[1] = out[2] = in[0];
2523          out[3] = 1.0F;
2524          break;
2525       case GL_LUMINANCE_ALPHA:
2526          out[0] = out[1] = out[2] = in[0];
2527          out[3] = in[3];
2528          break;
2529       case GL_INTENSITY:
2530          out[0] = out[1] = out[2] = out[3] = in[0];
2531          break;
2532       default:
2533          COPY_4V(out, in);
2534       }
2535    }
2536 }
2537