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