1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/glformats.h"
38#include "main/texstore.h"
39#include "main/image.h"
40#include "main/macros.h"
41#include "main/mfeatures.h"
42
43#include "pipe/p_context.h"
44#include "pipe/p_defines.h"
45#include "pipe/p_screen.h"
46#include "util/u_format.h"
47#include "st_context.h"
48#include "st_format.h"
49
50
51/**
52 * Translate Mesa format to Gallium format.
53 */
54enum pipe_format
55st_mesa_format_to_pipe_format(gl_format mesaFormat)
56{
57   switch (mesaFormat) {
58   case MESA_FORMAT_RGBA8888:
59      return PIPE_FORMAT_A8B8G8R8_UNORM;
60   case MESA_FORMAT_RGBA8888_REV:
61      return PIPE_FORMAT_R8G8B8A8_UNORM;
62   case MESA_FORMAT_ARGB8888:
63      return PIPE_FORMAT_B8G8R8A8_UNORM;
64   case MESA_FORMAT_ARGB8888_REV:
65      return PIPE_FORMAT_A8R8G8B8_UNORM;
66   case MESA_FORMAT_RGBX8888:
67      return PIPE_FORMAT_X8B8G8R8_UNORM;
68   case MESA_FORMAT_RGBX8888_REV:
69      return PIPE_FORMAT_R8G8B8X8_UNORM;
70   case MESA_FORMAT_XRGB8888:
71      return PIPE_FORMAT_B8G8R8X8_UNORM;
72   case MESA_FORMAT_XRGB8888_REV:
73      return PIPE_FORMAT_X8R8G8B8_UNORM;
74   case MESA_FORMAT_ARGB1555:
75      return PIPE_FORMAT_B5G5R5A1_UNORM;
76   case MESA_FORMAT_ARGB4444:
77      return PIPE_FORMAT_B4G4R4A4_UNORM;
78   case MESA_FORMAT_RGB565:
79      return PIPE_FORMAT_B5G6R5_UNORM;
80   case MESA_FORMAT_RGB332:
81      return PIPE_FORMAT_B2G3R3_UNORM;
82   case MESA_FORMAT_ARGB2101010:
83      return PIPE_FORMAT_B10G10R10A2_UNORM;
84   case MESA_FORMAT_AL44:
85      return PIPE_FORMAT_L4A4_UNORM;
86   case MESA_FORMAT_AL88:
87      return PIPE_FORMAT_L8A8_UNORM;
88   case MESA_FORMAT_AL1616:
89      return PIPE_FORMAT_L16A16_UNORM;
90   case MESA_FORMAT_A8:
91      return PIPE_FORMAT_A8_UNORM;
92   case MESA_FORMAT_A16:
93      return PIPE_FORMAT_A16_UNORM;
94   case MESA_FORMAT_L8:
95      return PIPE_FORMAT_L8_UNORM;
96   case MESA_FORMAT_L16:
97      return PIPE_FORMAT_L16_UNORM;
98   case MESA_FORMAT_I8:
99      return PIPE_FORMAT_I8_UNORM;
100   case MESA_FORMAT_I16:
101      return PIPE_FORMAT_I16_UNORM;
102   case MESA_FORMAT_Z16:
103      return PIPE_FORMAT_Z16_UNORM;
104   case MESA_FORMAT_Z32:
105      return PIPE_FORMAT_Z32_UNORM;
106   case MESA_FORMAT_Z24_S8:
107      return PIPE_FORMAT_S8_UINT_Z24_UNORM;
108   case MESA_FORMAT_S8_Z24:
109      return PIPE_FORMAT_Z24_UNORM_S8_UINT;
110   case MESA_FORMAT_Z24_X8:
111      return PIPE_FORMAT_X8Z24_UNORM;
112   case MESA_FORMAT_X8_Z24:
113      return PIPE_FORMAT_Z24X8_UNORM;
114   case MESA_FORMAT_S8:
115      return PIPE_FORMAT_S8_UINT;
116   case MESA_FORMAT_Z32_FLOAT:
117      return PIPE_FORMAT_Z32_FLOAT;
118   case MESA_FORMAT_Z32_FLOAT_X24S8:
119      return PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
120   case MESA_FORMAT_YCBCR:
121      return PIPE_FORMAT_UYVY;
122#if FEATURE_texture_s3tc
123   case MESA_FORMAT_RGB_DXT1:
124      return PIPE_FORMAT_DXT1_RGB;
125   case MESA_FORMAT_RGBA_DXT1:
126      return PIPE_FORMAT_DXT1_RGBA;
127   case MESA_FORMAT_RGBA_DXT3:
128      return PIPE_FORMAT_DXT3_RGBA;
129   case MESA_FORMAT_RGBA_DXT5:
130      return PIPE_FORMAT_DXT5_RGBA;
131#if FEATURE_EXT_texture_sRGB
132   case MESA_FORMAT_SRGB_DXT1:
133      return PIPE_FORMAT_DXT1_SRGB;
134   case MESA_FORMAT_SRGBA_DXT1:
135      return PIPE_FORMAT_DXT1_SRGBA;
136   case MESA_FORMAT_SRGBA_DXT3:
137      return PIPE_FORMAT_DXT3_SRGBA;
138   case MESA_FORMAT_SRGBA_DXT5:
139      return PIPE_FORMAT_DXT5_SRGBA;
140#endif
141#endif
142#if FEATURE_EXT_texture_sRGB
143   case MESA_FORMAT_SLA8:
144      return PIPE_FORMAT_L8A8_SRGB;
145   case MESA_FORMAT_SL8:
146      return PIPE_FORMAT_L8_SRGB;
147   case MESA_FORMAT_SRGB8:
148      return PIPE_FORMAT_R8G8B8_SRGB;
149   case MESA_FORMAT_SRGBA8:
150      return PIPE_FORMAT_A8B8G8R8_SRGB;
151   case MESA_FORMAT_SARGB8:
152      return PIPE_FORMAT_B8G8R8A8_SRGB;
153#endif
154   case MESA_FORMAT_RGBA_FLOAT32:
155      return PIPE_FORMAT_R32G32B32A32_FLOAT;
156   case MESA_FORMAT_RGBA_FLOAT16:
157      return PIPE_FORMAT_R16G16B16A16_FLOAT;
158   case MESA_FORMAT_RGB_FLOAT32:
159      return PIPE_FORMAT_R32G32B32_FLOAT;
160   case MESA_FORMAT_RGB_FLOAT16:
161      return PIPE_FORMAT_R16G16B16_FLOAT;
162   case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32:
163      return PIPE_FORMAT_L32A32_FLOAT;
164   case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16:
165      return PIPE_FORMAT_L16A16_FLOAT;
166   case MESA_FORMAT_LUMINANCE_FLOAT32:
167      return PIPE_FORMAT_L32_FLOAT;
168   case MESA_FORMAT_LUMINANCE_FLOAT16:
169      return PIPE_FORMAT_L16_FLOAT;
170   case MESA_FORMAT_ALPHA_FLOAT32:
171      return PIPE_FORMAT_A32_FLOAT;
172   case MESA_FORMAT_ALPHA_FLOAT16:
173      return PIPE_FORMAT_A16_FLOAT;
174   case MESA_FORMAT_INTENSITY_FLOAT32:
175      return PIPE_FORMAT_I32_FLOAT;
176   case MESA_FORMAT_INTENSITY_FLOAT16:
177      return PIPE_FORMAT_I16_FLOAT;
178   case MESA_FORMAT_R_FLOAT32:
179      return PIPE_FORMAT_R32_FLOAT;
180   case MESA_FORMAT_R_FLOAT16:
181      return PIPE_FORMAT_R16_FLOAT;
182   case MESA_FORMAT_RG_FLOAT32:
183      return PIPE_FORMAT_R32G32_FLOAT;
184   case MESA_FORMAT_RG_FLOAT16:
185      return PIPE_FORMAT_R16G16_FLOAT;
186
187   case MESA_FORMAT_R8:
188      return PIPE_FORMAT_R8_UNORM;
189   case MESA_FORMAT_R16:
190      return PIPE_FORMAT_R16_UNORM;
191   case MESA_FORMAT_GR88:
192      return PIPE_FORMAT_R8G8_UNORM;
193   case MESA_FORMAT_RG1616:
194      return PIPE_FORMAT_R16G16_UNORM;
195   case MESA_FORMAT_RGBA_16:
196      return PIPE_FORMAT_R16G16B16A16_UNORM;
197
198   /* signed int formats */
199   case MESA_FORMAT_ALPHA_UINT8:
200      return PIPE_FORMAT_A8_UINT;
201   case MESA_FORMAT_ALPHA_UINT16:
202      return PIPE_FORMAT_A16_UINT;
203   case MESA_FORMAT_ALPHA_UINT32:
204      return PIPE_FORMAT_A32_UINT;
205
206   case MESA_FORMAT_ALPHA_INT8:
207      return PIPE_FORMAT_A8_SINT;
208   case MESA_FORMAT_ALPHA_INT16:
209      return PIPE_FORMAT_A16_SINT;
210   case MESA_FORMAT_ALPHA_INT32:
211      return PIPE_FORMAT_A32_SINT;
212
213   case MESA_FORMAT_INTENSITY_UINT8:
214      return PIPE_FORMAT_I8_UINT;
215   case MESA_FORMAT_INTENSITY_UINT16:
216      return PIPE_FORMAT_I16_UINT;
217   case MESA_FORMAT_INTENSITY_UINT32:
218      return PIPE_FORMAT_I32_UINT;
219
220   case MESA_FORMAT_INTENSITY_INT8:
221      return PIPE_FORMAT_I8_SINT;
222   case MESA_FORMAT_INTENSITY_INT16:
223      return PIPE_FORMAT_I16_SINT;
224   case MESA_FORMAT_INTENSITY_INT32:
225      return PIPE_FORMAT_I32_SINT;
226
227   case MESA_FORMAT_LUMINANCE_UINT8:
228      return PIPE_FORMAT_L8_UINT;
229   case MESA_FORMAT_LUMINANCE_UINT16:
230      return PIPE_FORMAT_L16_UINT;
231   case MESA_FORMAT_LUMINANCE_UINT32:
232      return PIPE_FORMAT_L32_UINT;
233
234   case MESA_FORMAT_LUMINANCE_INT8:
235      return PIPE_FORMAT_L8_SINT;
236   case MESA_FORMAT_LUMINANCE_INT16:
237      return PIPE_FORMAT_L16_SINT;
238   case MESA_FORMAT_LUMINANCE_INT32:
239      return PIPE_FORMAT_L32_SINT;
240
241   case MESA_FORMAT_LUMINANCE_ALPHA_UINT8:
242      return PIPE_FORMAT_L8A8_UINT;
243   case MESA_FORMAT_LUMINANCE_ALPHA_UINT16:
244      return PIPE_FORMAT_L16A16_UINT;
245   case MESA_FORMAT_LUMINANCE_ALPHA_UINT32:
246      return PIPE_FORMAT_L32A32_UINT;
247
248   case MESA_FORMAT_LUMINANCE_ALPHA_INT8:
249      return PIPE_FORMAT_L8A8_SINT;
250   case MESA_FORMAT_LUMINANCE_ALPHA_INT16:
251      return PIPE_FORMAT_L16A16_SINT;
252   case MESA_FORMAT_LUMINANCE_ALPHA_INT32:
253      return PIPE_FORMAT_L32A32_SINT;
254
255   case MESA_FORMAT_R_INT8:
256      return PIPE_FORMAT_R8_SINT;
257   case MESA_FORMAT_RG_INT8:
258      return PIPE_FORMAT_R8G8_SINT;
259   case MESA_FORMAT_RGB_INT8:
260      return PIPE_FORMAT_R8G8B8_SINT;
261   case MESA_FORMAT_RGBA_INT8:
262      return PIPE_FORMAT_R8G8B8A8_SINT;
263   case MESA_FORMAT_R_INT16:
264      return PIPE_FORMAT_R16_SINT;
265   case MESA_FORMAT_RG_INT16:
266      return PIPE_FORMAT_R16G16_SINT;
267   case MESA_FORMAT_RGB_INT16:
268      return PIPE_FORMAT_R16G16B16_SINT;
269   case MESA_FORMAT_RGBA_INT16:
270      return PIPE_FORMAT_R16G16B16A16_SINT;
271   case MESA_FORMAT_R_INT32:
272      return PIPE_FORMAT_R32_SINT;
273   case MESA_FORMAT_RG_INT32:
274      return PIPE_FORMAT_R32G32_SINT;
275   case MESA_FORMAT_RGB_INT32:
276      return PIPE_FORMAT_R32G32B32_SINT;
277   case MESA_FORMAT_RGBA_INT32:
278      return PIPE_FORMAT_R32G32B32A32_SINT;
279
280   /* unsigned int formats */
281   case MESA_FORMAT_R_UINT8:
282      return PIPE_FORMAT_R8_UINT;
283   case MESA_FORMAT_RG_UINT8:
284      return PIPE_FORMAT_R8G8_UINT;
285   case MESA_FORMAT_RGB_UINT8:
286      return PIPE_FORMAT_R8G8B8_UINT;
287   case MESA_FORMAT_RGBA_UINT8:
288      return PIPE_FORMAT_R8G8B8A8_UINT;
289   case MESA_FORMAT_R_UINT16:
290      return PIPE_FORMAT_R16_UINT;
291   case MESA_FORMAT_RG_UINT16:
292      return PIPE_FORMAT_R16G16_UINT;
293   case MESA_FORMAT_RGB_UINT16:
294      return PIPE_FORMAT_R16G16B16_UINT;
295   case MESA_FORMAT_RGBA_UINT16:
296      return PIPE_FORMAT_R16G16B16A16_UINT;
297   case MESA_FORMAT_R_UINT32:
298      return PIPE_FORMAT_R32_UINT;
299   case MESA_FORMAT_RG_UINT32:
300      return PIPE_FORMAT_R32G32_UINT;
301   case MESA_FORMAT_RGB_UINT32:
302      return PIPE_FORMAT_R32G32B32_UINT;
303   case MESA_FORMAT_RGBA_UINT32:
304      return PIPE_FORMAT_R32G32B32A32_UINT;
305
306   case MESA_FORMAT_RED_RGTC1:
307      return PIPE_FORMAT_RGTC1_UNORM;
308   case MESA_FORMAT_SIGNED_RED_RGTC1:
309      return PIPE_FORMAT_RGTC1_SNORM;
310   case MESA_FORMAT_RG_RGTC2:
311      return PIPE_FORMAT_RGTC2_UNORM;
312   case MESA_FORMAT_SIGNED_RG_RGTC2:
313      return PIPE_FORMAT_RGTC2_SNORM;
314
315   case MESA_FORMAT_L_LATC1:
316      return PIPE_FORMAT_LATC1_UNORM;
317   case MESA_FORMAT_SIGNED_L_LATC1:
318      return PIPE_FORMAT_LATC1_SNORM;
319   case MESA_FORMAT_LA_LATC2:
320      return PIPE_FORMAT_LATC2_UNORM;
321   case MESA_FORMAT_SIGNED_LA_LATC2:
322      return PIPE_FORMAT_LATC2_SNORM;
323
324   case MESA_FORMAT_ETC1_RGB8:
325      return PIPE_FORMAT_ETC1_RGB8;
326
327   /* signed normalized formats */
328   case MESA_FORMAT_SIGNED_R8:
329      return PIPE_FORMAT_R8_SNORM;
330   case MESA_FORMAT_SIGNED_RG88_REV:
331      return PIPE_FORMAT_R8G8_SNORM;
332   case MESA_FORMAT_SIGNED_RGBA8888_REV:
333      return PIPE_FORMAT_R8G8B8A8_SNORM;
334
335   case MESA_FORMAT_SIGNED_A8:
336      return PIPE_FORMAT_A8_SNORM;
337   case MESA_FORMAT_SIGNED_L8:
338      return PIPE_FORMAT_L8_SNORM;
339   case MESA_FORMAT_SIGNED_AL88:
340      return PIPE_FORMAT_L8A8_SNORM;
341   case MESA_FORMAT_SIGNED_I8:
342      return PIPE_FORMAT_I8_SNORM;
343
344   case MESA_FORMAT_SIGNED_R16:
345      return PIPE_FORMAT_R16_SNORM;
346   case MESA_FORMAT_SIGNED_GR1616:
347      return PIPE_FORMAT_R16G16_SNORM;
348   case MESA_FORMAT_SIGNED_RGBA_16:
349      return PIPE_FORMAT_R16G16B16A16_SNORM;
350
351   case MESA_FORMAT_SIGNED_A16:
352      return PIPE_FORMAT_A16_SNORM;
353   case MESA_FORMAT_SIGNED_L16:
354      return PIPE_FORMAT_L16_SNORM;
355   case MESA_FORMAT_SIGNED_AL1616:
356      return PIPE_FORMAT_L16A16_SNORM;
357   case MESA_FORMAT_SIGNED_I16:
358      return PIPE_FORMAT_I16_SNORM;
359
360   case MESA_FORMAT_RGB9_E5_FLOAT:
361      return PIPE_FORMAT_R9G9B9E5_FLOAT;
362   case MESA_FORMAT_R11_G11_B10_FLOAT:
363      return PIPE_FORMAT_R11G11B10_FLOAT;
364   case MESA_FORMAT_ARGB2101010_UINT:
365      return PIPE_FORMAT_B10G10R10A2_UINT;
366   default:
367      assert(0);
368      return PIPE_FORMAT_NONE;
369   }
370}
371
372
373/**
374 * Translate Gallium format to Mesa format.
375 */
376gl_format
377st_pipe_format_to_mesa_format(enum pipe_format format)
378{
379   switch (format) {
380   case PIPE_FORMAT_A8B8G8R8_UNORM:
381      return MESA_FORMAT_RGBA8888;
382   case PIPE_FORMAT_R8G8B8A8_UNORM:
383      return MESA_FORMAT_RGBA8888_REV;
384   case PIPE_FORMAT_B8G8R8A8_UNORM:
385      return MESA_FORMAT_ARGB8888;
386   case PIPE_FORMAT_A8R8G8B8_UNORM:
387      return MESA_FORMAT_ARGB8888_REV;
388   case PIPE_FORMAT_X8B8G8R8_UNORM:
389      return MESA_FORMAT_RGBX8888;
390   case PIPE_FORMAT_R8G8B8X8_UNORM:
391      return MESA_FORMAT_RGBX8888_REV;
392   case PIPE_FORMAT_B8G8R8X8_UNORM:
393      return MESA_FORMAT_XRGB8888;
394   case PIPE_FORMAT_X8R8G8B8_UNORM:
395      return MESA_FORMAT_XRGB8888_REV;
396   case PIPE_FORMAT_B5G5R5A1_UNORM:
397      return MESA_FORMAT_ARGB1555;
398   case PIPE_FORMAT_B4G4R4A4_UNORM:
399      return MESA_FORMAT_ARGB4444;
400   case PIPE_FORMAT_B5G6R5_UNORM:
401      return MESA_FORMAT_RGB565;
402   case PIPE_FORMAT_B2G3R3_UNORM:
403      return MESA_FORMAT_RGB332;
404   case PIPE_FORMAT_B10G10R10A2_UNORM:
405      return MESA_FORMAT_ARGB2101010;
406   case PIPE_FORMAT_L4A4_UNORM:
407      return MESA_FORMAT_AL44;
408   case PIPE_FORMAT_L8A8_UNORM:
409      return MESA_FORMAT_AL88;
410   case PIPE_FORMAT_L16A16_UNORM:
411      return MESA_FORMAT_AL1616;
412   case PIPE_FORMAT_A8_UNORM:
413      return MESA_FORMAT_A8;
414   case PIPE_FORMAT_A16_UNORM:
415      return MESA_FORMAT_A16;
416   case PIPE_FORMAT_L8_UNORM:
417      return MESA_FORMAT_L8;
418   case PIPE_FORMAT_L16_UNORM:
419      return MESA_FORMAT_L16;
420   case PIPE_FORMAT_I8_UNORM:
421      return MESA_FORMAT_I8;
422   case PIPE_FORMAT_I16_UNORM:
423      return MESA_FORMAT_I16;
424   case PIPE_FORMAT_S8_UINT:
425      return MESA_FORMAT_S8;
426
427   case PIPE_FORMAT_R16G16B16A16_UNORM:
428      return MESA_FORMAT_RGBA_16;
429
430   case PIPE_FORMAT_Z16_UNORM:
431      return MESA_FORMAT_Z16;
432   case PIPE_FORMAT_Z32_UNORM:
433      return MESA_FORMAT_Z32;
434   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
435      return MESA_FORMAT_Z24_S8;
436   case PIPE_FORMAT_X8Z24_UNORM:
437      return MESA_FORMAT_Z24_X8;
438   case PIPE_FORMAT_Z24X8_UNORM:
439      return MESA_FORMAT_X8_Z24;
440   case PIPE_FORMAT_Z24_UNORM_S8_UINT:
441      return MESA_FORMAT_S8_Z24;
442   case PIPE_FORMAT_Z32_FLOAT:
443      return MESA_FORMAT_Z32_FLOAT;
444   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
445      return MESA_FORMAT_Z32_FLOAT_X24S8;
446
447   case PIPE_FORMAT_UYVY:
448      return MESA_FORMAT_YCBCR;
449   case PIPE_FORMAT_YUYV:
450      return MESA_FORMAT_YCBCR_REV;
451
452#if FEATURE_texture_s3tc
453   case PIPE_FORMAT_DXT1_RGB:
454      return MESA_FORMAT_RGB_DXT1;
455   case PIPE_FORMAT_DXT1_RGBA:
456      return MESA_FORMAT_RGBA_DXT1;
457   case PIPE_FORMAT_DXT3_RGBA:
458      return MESA_FORMAT_RGBA_DXT3;
459   case PIPE_FORMAT_DXT5_RGBA:
460      return MESA_FORMAT_RGBA_DXT5;
461#if FEATURE_EXT_texture_sRGB
462   case PIPE_FORMAT_DXT1_SRGB:
463      return MESA_FORMAT_SRGB_DXT1;
464   case PIPE_FORMAT_DXT1_SRGBA:
465      return MESA_FORMAT_SRGBA_DXT1;
466   case PIPE_FORMAT_DXT3_SRGBA:
467      return MESA_FORMAT_SRGBA_DXT3;
468   case PIPE_FORMAT_DXT5_SRGBA:
469      return MESA_FORMAT_SRGBA_DXT5;
470#endif
471#endif
472
473#if FEATURE_EXT_texture_sRGB
474   case PIPE_FORMAT_L8A8_SRGB:
475      return MESA_FORMAT_SLA8;
476   case PIPE_FORMAT_L8_SRGB:
477      return MESA_FORMAT_SL8;
478   case PIPE_FORMAT_R8G8B8_SRGB:
479      return MESA_FORMAT_SRGB8;
480   case PIPE_FORMAT_A8B8G8R8_SRGB:
481      return MESA_FORMAT_SRGBA8;
482   case PIPE_FORMAT_B8G8R8A8_SRGB:
483      return MESA_FORMAT_SARGB8;
484#endif
485   case PIPE_FORMAT_R32G32B32A32_FLOAT:
486      return MESA_FORMAT_RGBA_FLOAT32;
487   case PIPE_FORMAT_R16G16B16A16_FLOAT:
488      return MESA_FORMAT_RGBA_FLOAT16;
489   case PIPE_FORMAT_R32G32B32_FLOAT:
490      return MESA_FORMAT_RGB_FLOAT32;
491   case PIPE_FORMAT_R16G16B16_FLOAT:
492      return MESA_FORMAT_RGB_FLOAT16;
493   case PIPE_FORMAT_L32A32_FLOAT:
494      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
495   case PIPE_FORMAT_L16A16_FLOAT:
496      return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
497   case PIPE_FORMAT_L32_FLOAT:
498      return MESA_FORMAT_LUMINANCE_FLOAT32;
499   case PIPE_FORMAT_L16_FLOAT:
500      return MESA_FORMAT_LUMINANCE_FLOAT16;
501   case PIPE_FORMAT_A32_FLOAT:
502      return MESA_FORMAT_ALPHA_FLOAT32;
503   case PIPE_FORMAT_A16_FLOAT:
504      return MESA_FORMAT_ALPHA_FLOAT16;
505   case PIPE_FORMAT_I32_FLOAT:
506      return MESA_FORMAT_INTENSITY_FLOAT32;
507   case PIPE_FORMAT_I16_FLOAT:
508      return MESA_FORMAT_INTENSITY_FLOAT16;
509   case PIPE_FORMAT_R32_FLOAT:
510      return MESA_FORMAT_R_FLOAT32;
511   case PIPE_FORMAT_R16_FLOAT:
512      return MESA_FORMAT_R_FLOAT16;
513   case PIPE_FORMAT_R32G32_FLOAT:
514      return MESA_FORMAT_RG_FLOAT32;
515   case PIPE_FORMAT_R16G16_FLOAT:
516      return MESA_FORMAT_RG_FLOAT16;
517
518   case PIPE_FORMAT_R8_UNORM:
519      return MESA_FORMAT_R8;
520   case PIPE_FORMAT_R16_UNORM:
521      return MESA_FORMAT_R16;
522   case PIPE_FORMAT_R8G8_UNORM:
523      return MESA_FORMAT_GR88;
524   case PIPE_FORMAT_R16G16_UNORM:
525      return MESA_FORMAT_RG1616;
526
527   case PIPE_FORMAT_A8_UINT:
528      return MESA_FORMAT_ALPHA_UINT8;
529   case PIPE_FORMAT_A16_UINT:
530      return MESA_FORMAT_ALPHA_UINT16;
531   case PIPE_FORMAT_A32_UINT:
532      return MESA_FORMAT_ALPHA_UINT32;
533   case PIPE_FORMAT_A8_SINT:
534      return MESA_FORMAT_ALPHA_INT8;
535   case PIPE_FORMAT_A16_SINT:
536      return MESA_FORMAT_ALPHA_INT16;
537   case PIPE_FORMAT_A32_SINT:
538      return MESA_FORMAT_ALPHA_INT32;
539
540   case PIPE_FORMAT_I8_UINT:
541      return MESA_FORMAT_INTENSITY_UINT8;
542   case PIPE_FORMAT_I16_UINT:
543      return MESA_FORMAT_INTENSITY_UINT16;
544   case PIPE_FORMAT_I32_UINT:
545      return MESA_FORMAT_INTENSITY_UINT32;
546   case PIPE_FORMAT_I8_SINT:
547      return MESA_FORMAT_INTENSITY_INT8;
548   case PIPE_FORMAT_I16_SINT:
549      return MESA_FORMAT_INTENSITY_INT16;
550   case PIPE_FORMAT_I32_SINT:
551      return MESA_FORMAT_INTENSITY_INT32;
552
553  case PIPE_FORMAT_L8_UINT:
554      return MESA_FORMAT_LUMINANCE_UINT8;
555   case PIPE_FORMAT_L16_UINT:
556      return MESA_FORMAT_LUMINANCE_UINT16;
557   case PIPE_FORMAT_L32_UINT:
558      return MESA_FORMAT_LUMINANCE_UINT32;
559   case PIPE_FORMAT_L8_SINT:
560      return MESA_FORMAT_LUMINANCE_INT8;
561   case PIPE_FORMAT_L16_SINT:
562      return MESA_FORMAT_LUMINANCE_INT16;
563   case PIPE_FORMAT_L32_SINT:
564      return MESA_FORMAT_LUMINANCE_INT32;
565
566   case PIPE_FORMAT_L8A8_UINT:
567      return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
568   case PIPE_FORMAT_L16A16_UINT:
569      return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
570   case PIPE_FORMAT_L32A32_UINT:
571      return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
572   case PIPE_FORMAT_L8A8_SINT:
573      return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
574   case PIPE_FORMAT_L16A16_SINT:
575      return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
576   case PIPE_FORMAT_L32A32_SINT:
577      return MESA_FORMAT_LUMINANCE_ALPHA_INT32;
578
579   case PIPE_FORMAT_R8_SINT:
580      return MESA_FORMAT_R_INT8;
581   case PIPE_FORMAT_R8G8_SINT:
582      return MESA_FORMAT_RG_INT8;
583   case PIPE_FORMAT_R8G8B8_SINT:
584      return MESA_FORMAT_RGB_INT8;
585   case PIPE_FORMAT_R8G8B8A8_SINT:
586      return MESA_FORMAT_RGBA_INT8;
587
588   case PIPE_FORMAT_R16_SINT:
589      return MESA_FORMAT_R_INT16;
590   case PIPE_FORMAT_R16G16_SINT:
591      return MESA_FORMAT_RG_INT16;
592   case PIPE_FORMAT_R16G16B16_SINT:
593      return MESA_FORMAT_RGB_INT16;
594   case PIPE_FORMAT_R16G16B16A16_SINT:
595      return MESA_FORMAT_RGBA_INT16;
596
597   case PIPE_FORMAT_R32_SINT:
598      return MESA_FORMAT_R_INT32;
599   case PIPE_FORMAT_R32G32_SINT:
600      return MESA_FORMAT_RG_INT32;
601   case PIPE_FORMAT_R32G32B32_SINT:
602      return MESA_FORMAT_RGB_INT32;
603   case PIPE_FORMAT_R32G32B32A32_SINT:
604      return MESA_FORMAT_RGBA_INT32;
605
606   /* unsigned int formats */
607   case PIPE_FORMAT_R8_UINT:
608      return MESA_FORMAT_R_UINT8;
609   case PIPE_FORMAT_R8G8_UINT:
610      return MESA_FORMAT_RG_UINT8;
611   case PIPE_FORMAT_R8G8B8_UINT:
612      return MESA_FORMAT_RGB_UINT8;
613   case PIPE_FORMAT_R8G8B8A8_UINT:
614      return MESA_FORMAT_RGBA_UINT8;
615
616   case PIPE_FORMAT_R16_UINT:
617      return MESA_FORMAT_R_UINT16;
618   case PIPE_FORMAT_R16G16_UINT:
619      return MESA_FORMAT_RG_UINT16;
620   case PIPE_FORMAT_R16G16B16_UINT:
621      return MESA_FORMAT_RGB_UINT16;
622   case PIPE_FORMAT_R16G16B16A16_UINT:
623      return MESA_FORMAT_RGBA_UINT16;
624
625   case PIPE_FORMAT_R32_UINT:
626      return MESA_FORMAT_R_UINT32;
627   case PIPE_FORMAT_R32G32_UINT:
628      return MESA_FORMAT_RG_UINT32;
629   case PIPE_FORMAT_R32G32B32_UINT:
630      return MESA_FORMAT_RGB_UINT32;
631   case PIPE_FORMAT_R32G32B32A32_UINT:
632      return MESA_FORMAT_RGBA_UINT32;
633
634   case PIPE_FORMAT_RGTC1_UNORM:
635      return MESA_FORMAT_RED_RGTC1;
636   case PIPE_FORMAT_RGTC1_SNORM:
637      return MESA_FORMAT_SIGNED_RED_RGTC1;
638   case PIPE_FORMAT_RGTC2_UNORM:
639      return MESA_FORMAT_RG_RGTC2;
640   case PIPE_FORMAT_RGTC2_SNORM:
641      return MESA_FORMAT_SIGNED_RG_RGTC2;
642
643   case PIPE_FORMAT_LATC1_UNORM:
644      return MESA_FORMAT_L_LATC1;
645   case PIPE_FORMAT_LATC1_SNORM:
646      return MESA_FORMAT_SIGNED_L_LATC1;
647   case PIPE_FORMAT_LATC2_UNORM:
648      return MESA_FORMAT_LA_LATC2;
649   case PIPE_FORMAT_LATC2_SNORM:
650      return MESA_FORMAT_SIGNED_LA_LATC2;
651
652   case PIPE_FORMAT_ETC1_RGB8:
653      return MESA_FORMAT_ETC1_RGB8;
654
655   /* signed normalized formats */
656   case PIPE_FORMAT_R8_SNORM:
657      return MESA_FORMAT_SIGNED_R8;
658   case PIPE_FORMAT_R8G8_SNORM:
659      return MESA_FORMAT_SIGNED_RG88_REV;
660   case PIPE_FORMAT_R8G8B8A8_SNORM:
661      return MESA_FORMAT_SIGNED_RGBA8888_REV;
662
663   case PIPE_FORMAT_A8_SNORM:
664      return MESA_FORMAT_SIGNED_A8;
665   case PIPE_FORMAT_L8_SNORM:
666      return MESA_FORMAT_SIGNED_L8;
667   case PIPE_FORMAT_L8A8_SNORM:
668      return MESA_FORMAT_SIGNED_AL88;
669   case PIPE_FORMAT_I8_SNORM:
670      return MESA_FORMAT_SIGNED_I8;
671
672   case PIPE_FORMAT_R16_SNORM:
673      return MESA_FORMAT_SIGNED_R16;
674   case PIPE_FORMAT_R16G16_SNORM:
675      return MESA_FORMAT_SIGNED_GR1616;
676   case PIPE_FORMAT_R16G16B16A16_SNORM:
677      return MESA_FORMAT_SIGNED_RGBA_16;
678
679   case PIPE_FORMAT_A16_SNORM:
680      return MESA_FORMAT_SIGNED_A16;
681   case PIPE_FORMAT_L16_SNORM:
682      return MESA_FORMAT_SIGNED_L16;
683   case PIPE_FORMAT_L16A16_SNORM:
684      return MESA_FORMAT_SIGNED_AL1616;
685   case PIPE_FORMAT_I16_SNORM:
686      return MESA_FORMAT_SIGNED_I16;
687
688   case PIPE_FORMAT_R9G9B9E5_FLOAT:
689      return MESA_FORMAT_RGB9_E5_FLOAT;
690   case PIPE_FORMAT_R11G11B10_FLOAT:
691      return MESA_FORMAT_R11_G11_B10_FLOAT;
692
693   case PIPE_FORMAT_B10G10R10A2_UINT:
694      return MESA_FORMAT_ARGB2101010_UINT;
695   default:
696      assert(0);
697      return MESA_FORMAT_NONE;
698   }
699}
700
701
702/**
703 * Map GL texture formats to Gallium pipe formats.
704 */
705struct format_mapping
706{
707   GLenum glFormats[18];       /**< list of GLenum formats, 0-terminated */
708   enum pipe_format pipeFormats[10]; /**< list of pipe formats, 0-terminated */
709};
710
711
712#define DEFAULT_RGBA_FORMATS \
713      PIPE_FORMAT_B8G8R8A8_UNORM, \
714      PIPE_FORMAT_A8R8G8B8_UNORM, \
715      PIPE_FORMAT_A8B8G8R8_UNORM, \
716      PIPE_FORMAT_B5G6R5_UNORM, \
717      0
718
719#define DEFAULT_RGB_FORMATS \
720      PIPE_FORMAT_B8G8R8X8_UNORM, \
721      PIPE_FORMAT_X8R8G8B8_UNORM, \
722      PIPE_FORMAT_X8B8G8R8_UNORM, \
723      PIPE_FORMAT_B8G8R8A8_UNORM, \
724      PIPE_FORMAT_A8R8G8B8_UNORM, \
725      PIPE_FORMAT_A8B8G8R8_UNORM, \
726      PIPE_FORMAT_B5G6R5_UNORM, \
727      0
728
729#define DEFAULT_SRGBA_FORMATS \
730      PIPE_FORMAT_B8G8R8A8_SRGB, \
731      PIPE_FORMAT_A8R8G8B8_SRGB, \
732      PIPE_FORMAT_A8B8G8R8_SRGB, \
733      0
734
735#define DEFAULT_DEPTH_FORMATS \
736      PIPE_FORMAT_Z24X8_UNORM, \
737      PIPE_FORMAT_X8Z24_UNORM, \
738      PIPE_FORMAT_Z16_UNORM, \
739      PIPE_FORMAT_Z24_UNORM_S8_UINT, \
740      PIPE_FORMAT_S8_UINT_Z24_UNORM, \
741      0
742
743#define DEFAULT_SNORM8_RGBA_FORMATS \
744      PIPE_FORMAT_R8G8B8A8_SNORM, \
745      0
746
747#define DEFAULT_UNORM16_RGBA_FORMATS \
748      PIPE_FORMAT_R16G16B16A16_UNORM, \
749      DEFAULT_RGBA_FORMATS
750
751
752/**
753 * This table maps OpenGL texture format enums to Gallium pipe_format enums.
754 * Multiple GL enums might map to multiple pipe_formats.
755 * The first pipe format in the list that's supported is the one that's chosen.
756 */
757static const struct format_mapping format_map[] = {
758   /* Basic RGB, RGBA formats */
759   {
760      { GL_RGB10, GL_RGB10_A2, 0 },
761      { PIPE_FORMAT_B10G10R10A2_UNORM, DEFAULT_RGBA_FORMATS }
762   },
763   {
764      { 4, GL_RGBA, GL_RGBA8, 0 },
765      { PIPE_FORMAT_R8G8B8A8_UNORM, DEFAULT_RGBA_FORMATS }
766   },
767   {
768      { GL_BGRA, 0 },
769      { PIPE_FORMAT_B8G8R8A8_UNORM, DEFAULT_RGBA_FORMATS }
770   },
771   {
772      { 3, GL_RGB, GL_RGB8, 0 },
773      { DEFAULT_RGB_FORMATS }
774   },
775   {
776      { GL_RGB12, GL_RGB16, GL_RGBA12, GL_RGBA16, 0 },
777      { PIPE_FORMAT_R16G16B16A16_UNORM, DEFAULT_RGBA_FORMATS }
778   },
779   {
780      { GL_RGBA4, GL_RGBA2, 0 },
781      { PIPE_FORMAT_B4G4R4A4_UNORM, DEFAULT_RGBA_FORMATS }
782   },
783   {
784      { GL_RGB5_A1, 0 },
785      { PIPE_FORMAT_B5G5R5A1_UNORM, DEFAULT_RGBA_FORMATS }
786   },
787   {
788      { GL_R3_G3_B2, 0 },
789      { PIPE_FORMAT_B2G3R3_UNORM, PIPE_FORMAT_B5G6R5_UNORM,
790        PIPE_FORMAT_B5G5R5A1_UNORM, DEFAULT_RGBA_FORMATS }
791   },
792   {
793      { GL_RGB5, GL_RGB4 },
794      { PIPE_FORMAT_B5G6R5_UNORM, PIPE_FORMAT_B5G5R5A1_UNORM,
795        DEFAULT_RGBA_FORMATS }
796   },
797   {
798      { GL_RGB565 },
799      { PIPE_FORMAT_B5G6R5_UNORM, DEFAULT_RGBA_FORMATS }
800   },
801
802   /* basic Alpha formats */
803   {
804      { GL_ALPHA12, GL_ALPHA16, 0 },
805      { PIPE_FORMAT_A16_UNORM, PIPE_FORMAT_A8_UNORM,
806        DEFAULT_RGBA_FORMATS }
807   },
808   {
809      { GL_ALPHA, GL_ALPHA4, GL_ALPHA8, GL_COMPRESSED_ALPHA, 0 },
810      { PIPE_FORMAT_A8_UNORM, DEFAULT_RGBA_FORMATS }
811   },
812
813   /* basic Luminance formats */
814   {
815      { GL_LUMINANCE12, GL_LUMINANCE16, 0 },
816      { PIPE_FORMAT_L16_UNORM, PIPE_FORMAT_L8_UNORM, DEFAULT_RGB_FORMATS }
817   },
818   {
819      { 1, GL_LUMINANCE, GL_LUMINANCE4, GL_LUMINANCE8, 0 },
820      { PIPE_FORMAT_L8_UNORM, DEFAULT_RGB_FORMATS }
821   },
822
823   /* basic Luminance/Alpha formats */
824   {
825      { GL_LUMINANCE12_ALPHA4, GL_LUMINANCE12_ALPHA12,
826        GL_LUMINANCE16_ALPHA16, 0},
827      { PIPE_FORMAT_L16A16_UNORM, PIPE_FORMAT_L8A8_UNORM,
828        DEFAULT_RGBA_FORMATS }
829   },
830   {
831      { 2, GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, GL_LUMINANCE8_ALPHA8, 0 },
832      { PIPE_FORMAT_L8A8_UNORM, DEFAULT_RGBA_FORMATS }
833   },
834   {
835      { GL_LUMINANCE4_ALPHA4, 0 },
836      { PIPE_FORMAT_L4A4_UNORM, PIPE_FORMAT_L8A8_UNORM,
837        DEFAULT_RGBA_FORMATS }
838   },
839
840   /* basic Intensity formats */
841   {
842      { GL_INTENSITY12, GL_INTENSITY16, 0 },
843      { PIPE_FORMAT_I16_UNORM, PIPE_FORMAT_I8_UNORM, DEFAULT_RGBA_FORMATS }
844   },
845   {
846      { GL_INTENSITY, GL_INTENSITY4, GL_INTENSITY8,
847        GL_COMPRESSED_INTENSITY, 0 },
848      { PIPE_FORMAT_I8_UNORM, DEFAULT_RGBA_FORMATS }
849   },
850
851   /* YCbCr */
852   {
853      { GL_YCBCR_MESA, 0 },
854      { PIPE_FORMAT_UYVY, PIPE_FORMAT_YUYV, 0 }
855   },
856
857   /* compressed formats */ /* XXX PIPE_BIND_SAMPLER_VIEW only */
858   {
859      { GL_COMPRESSED_RGB, 0 },
860      { PIPE_FORMAT_DXT1_RGB, DEFAULT_RGB_FORMATS }
861   },
862   {
863      { GL_COMPRESSED_RGBA, 0 },
864      { PIPE_FORMAT_DXT5_RGBA, DEFAULT_RGBA_FORMATS }
865   },
866   {
867      { GL_RGB_S3TC, GL_RGB4_S3TC, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 0 },
868      { PIPE_FORMAT_DXT1_RGB, 0 }
869   },
870   {
871      { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 0 },
872      { PIPE_FORMAT_DXT1_RGBA, 0 }
873   },
874   {
875      { GL_RGBA_S3TC, GL_RGBA4_S3TC, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 0 },
876      { PIPE_FORMAT_DXT3_RGBA, 0 }
877   },
878   {
879      { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 0 },
880      { PIPE_FORMAT_DXT5_RGBA, 0 }
881   },
882
883#if 0
884   {
885      { GL_COMPRESSED_RGB_FXT1_3DFX, 0 },
886      { PIPE_FORMAT_RGB_FXT1, 0 }
887   },
888   {
889      { GL_COMPRESSED_RGBA_FXT1_3DFX, 0 },
890      { PIPE_FORMAT_RGBA_FXT1, 0 }
891   },
892#endif
893
894   /* Depth formats */
895   {
896      { GL_DEPTH_COMPONENT16, 0 },
897      { PIPE_FORMAT_Z16_UNORM, DEFAULT_DEPTH_FORMATS }
898   },
899   {
900      { GL_DEPTH_COMPONENT24, 0 },
901      { PIPE_FORMAT_Z24X8_UNORM, PIPE_FORMAT_X8Z24_UNORM,
902        DEFAULT_DEPTH_FORMATS }
903   },
904   {
905      { GL_DEPTH_COMPONENT32, 0 },
906      { PIPE_FORMAT_Z32_UNORM, DEFAULT_DEPTH_FORMATS }
907   },
908   {
909      { GL_DEPTH_COMPONENT, 0 },
910      { DEFAULT_DEPTH_FORMATS }
911   },
912   {
913      { GL_DEPTH_COMPONENT32F, 0 },
914      { PIPE_FORMAT_Z32_FLOAT, 0 }
915   },
916
917   /* stencil formats */
918   {
919      { GL_STENCIL_INDEX, GL_STENCIL_INDEX1_EXT, GL_STENCIL_INDEX4_EXT,
920        GL_STENCIL_INDEX8_EXT, GL_STENCIL_INDEX16_EXT, 0 },
921      {
922         PIPE_FORMAT_S8_UINT, PIPE_FORMAT_Z24_UNORM_S8_UINT,
923         PIPE_FORMAT_S8_UINT_Z24_UNORM, 0
924      }
925   },
926
927   /* Depth / Stencil formats */
928   {
929      { GL_DEPTH_STENCIL_EXT, GL_DEPTH24_STENCIL8_EXT, 0 },
930      { PIPE_FORMAT_Z24_UNORM_S8_UINT, PIPE_FORMAT_S8_UINT_Z24_UNORM, 0 }
931   },
932   {
933      { GL_DEPTH32F_STENCIL8, 0 },
934      { PIPE_FORMAT_Z32_FLOAT_S8X24_UINT, 0 }
935   },
936
937   /* sRGB formats */
938   {
939      { GL_SRGB_EXT, GL_SRGB8_EXT, GL_SRGB_ALPHA_EXT, GL_SRGB8_ALPHA8_EXT, 0 },
940      { DEFAULT_SRGBA_FORMATS }
941   },
942   {
943      { GL_COMPRESSED_SRGB_EXT, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT, 0 },
944      { PIPE_FORMAT_DXT1_SRGB, DEFAULT_SRGBA_FORMATS }
945   },
946   {
947      { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0 },
948      { PIPE_FORMAT_DXT1_SRGBA, 0 }
949   },
950   {
951      { GL_COMPRESSED_SRGB_ALPHA_EXT,
952        GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0 },
953      { PIPE_FORMAT_DXT3_SRGBA, DEFAULT_SRGBA_FORMATS }
954   },
955   {
956      { GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0 },
957      { PIPE_FORMAT_DXT5_SRGBA, 0 }
958   },
959   {
960      { GL_SLUMINANCE_ALPHA_EXT, GL_SLUMINANCE8_ALPHA8_EXT,
961        GL_COMPRESSED_SLUMINANCE_EXT, GL_COMPRESSED_SLUMINANCE_ALPHA_EXT, 0 },
962      { PIPE_FORMAT_L8A8_SRGB, DEFAULT_SRGBA_FORMATS }
963   },
964   {
965      { GL_SLUMINANCE_EXT, GL_SLUMINANCE8_EXT, 0 },
966      { PIPE_FORMAT_L8_SRGB, DEFAULT_SRGBA_FORMATS }
967   },
968
969   /* 16-bit float formats */
970   {
971      { GL_RGBA16F_ARB, 0 },
972      { PIPE_FORMAT_R16G16B16A16_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
973   },
974   {
975      { GL_RGB16F_ARB, 0 },
976      { PIPE_FORMAT_R16G16B16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
977        PIPE_FORMAT_R32G32B32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
978   },
979   {
980      { GL_LUMINANCE_ALPHA16F_ARB, 0 },
981      { PIPE_FORMAT_L16A16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
982        PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
983   },
984   {
985      { GL_ALPHA16F_ARB, 0 },
986      { PIPE_FORMAT_A16_FLOAT, PIPE_FORMAT_L16A16_FLOAT,
987        PIPE_FORMAT_A32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
988        PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
989   },
990   {
991      { GL_INTENSITY16F_ARB, 0 },
992      { PIPE_FORMAT_I16_FLOAT, PIPE_FORMAT_L16A16_FLOAT,
993        PIPE_FORMAT_I32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
994        PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
995   },
996   {
997      { GL_LUMINANCE16F_ARB, 0 },
998      { PIPE_FORMAT_L16_FLOAT, PIPE_FORMAT_L16A16_FLOAT,
999        PIPE_FORMAT_L32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1000        PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1001   },
1002   {
1003      { GL_R16F, 0 },
1004      { PIPE_FORMAT_R16_FLOAT, PIPE_FORMAT_R16G16_FLOAT,
1005        PIPE_FORMAT_R32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1006        PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1007   },
1008   {
1009      { GL_RG16F, 0 },
1010      { PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT,
1011        PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT, 0 }
1012   },
1013
1014   /* 32-bit float formats */
1015   {
1016      { GL_RGBA32F_ARB, 0 },
1017      { PIPE_FORMAT_R32G32B32A32_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1018   },
1019   {
1020      { GL_RGB32F_ARB, 0 },
1021      { PIPE_FORMAT_R32G32B32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT,
1022        PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1023   },
1024   {
1025      { GL_LUMINANCE_ALPHA32F_ARB, 0 },
1026      { PIPE_FORMAT_L32A32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT,
1027        PIPE_FORMAT_L16A16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1028   },
1029   {
1030      { GL_ALPHA32F_ARB, 0 },
1031      { PIPE_FORMAT_A32_FLOAT, PIPE_FORMAT_L32A32_FLOAT,
1032        PIPE_FORMAT_R32G32B32A32_FLOAT, PIPE_FORMAT_A16_FLOAT,
1033        PIPE_FORMAT_L16A16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1034   },
1035   {
1036      { GL_INTENSITY32F_ARB, 0 },
1037      { PIPE_FORMAT_I32_FLOAT, PIPE_FORMAT_L32A32_FLOAT,
1038        PIPE_FORMAT_R32G32B32A32_FLOAT, PIPE_FORMAT_I16_FLOAT,
1039        PIPE_FORMAT_L16A16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1040   },
1041   {
1042      { GL_LUMINANCE32F_ARB, 0 },
1043      { PIPE_FORMAT_L32_FLOAT, PIPE_FORMAT_L32A32_FLOAT,
1044        PIPE_FORMAT_R32G32B32A32_FLOAT, PIPE_FORMAT_L16_FLOAT,
1045        PIPE_FORMAT_L16A16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1046   },
1047   {
1048      { GL_R32F, 0 },
1049      { PIPE_FORMAT_R32_FLOAT, PIPE_FORMAT_R32G32_FLOAT,
1050        PIPE_FORMAT_R32G32B32A32_FLOAT, PIPE_FORMAT_R16_FLOAT,
1051        PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1052   },
1053   {
1054      { GL_RG32F, 0 },
1055      { PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT,
1056        PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R16G16B16A16_FLOAT, 0 }
1057   },
1058
1059   /* R, RG formats */
1060   {
1061      { GL_RED, GL_R8, 0 },
1062      { PIPE_FORMAT_R8_UNORM, PIPE_FORMAT_R8G8_UNORM, DEFAULT_RGBA_FORMATS }
1063   },
1064   {
1065      { GL_RG, GL_RG8, 0 },
1066      { PIPE_FORMAT_R8G8_UNORM, DEFAULT_RGBA_FORMATS }
1067   },
1068   {
1069      { GL_R16, 0 },
1070      { PIPE_FORMAT_R16_UNORM, PIPE_FORMAT_R16G16_UNORM,
1071        DEFAULT_UNORM16_RGBA_FORMATS }
1072   },
1073   {
1074      { GL_RG16, 0 },
1075      { PIPE_FORMAT_R16G16_UNORM, DEFAULT_UNORM16_RGBA_FORMATS }
1076   },
1077
1078   /* compressed R, RG formats */
1079   {
1080      { GL_COMPRESSED_RED, GL_COMPRESSED_RED_RGTC1, 0 },
1081      { PIPE_FORMAT_RGTC1_UNORM, PIPE_FORMAT_R8_UNORM, DEFAULT_RGBA_FORMATS }
1082   },
1083   {
1084      { GL_COMPRESSED_SIGNED_RED_RGTC1, 0 },
1085      { PIPE_FORMAT_RGTC1_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1086   },
1087   {
1088      { GL_COMPRESSED_RG, GL_COMPRESSED_RG_RGTC2, 0 },
1089      { PIPE_FORMAT_RGTC2_UNORM, PIPE_FORMAT_R8G8_UNORM, DEFAULT_RGBA_FORMATS }
1090   },
1091   {
1092      { GL_COMPRESSED_SIGNED_RG_RGTC2, 0 },
1093      { PIPE_FORMAT_RGTC2_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1094   },
1095   {
1096      { GL_COMPRESSED_LUMINANCE, GL_COMPRESSED_LUMINANCE_LATC1_EXT, 0 },
1097      { PIPE_FORMAT_LATC1_UNORM, PIPE_FORMAT_L8_UNORM, DEFAULT_RGBA_FORMATS }
1098   },
1099   {
1100      { GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT, 0 },
1101      { PIPE_FORMAT_LATC1_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1102   },
1103   {
1104      { GL_COMPRESSED_LUMINANCE_ALPHA, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT,
1105        GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI, 0 },
1106      { PIPE_FORMAT_LATC2_UNORM, PIPE_FORMAT_L8A8_UNORM, DEFAULT_RGBA_FORMATS }
1107   },
1108   {
1109      { GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT, 0 },
1110      { PIPE_FORMAT_LATC2_SNORM, DEFAULT_SNORM8_RGBA_FORMATS }
1111   },
1112
1113   /* ETC1 */
1114   {
1115      { GL_ETC1_RGB8_OES, 0 },
1116      { PIPE_FORMAT_ETC1_RGB8, 0 }
1117   },
1118
1119   /* signed/unsigned integer formats.
1120    */
1121   {
1122      { GL_RED_INTEGER_EXT,
1123        GL_GREEN_INTEGER_EXT,
1124        GL_BLUE_INTEGER_EXT,
1125        GL_RGBA_INTEGER_EXT,
1126        GL_BGRA_INTEGER_EXT,
1127        GL_RGBA8I_EXT, 0 },
1128      { PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1129   },
1130   {
1131      { GL_RGB_INTEGER_EXT,
1132        GL_BGR_INTEGER_EXT,
1133        GL_RGB8I_EXT, 0 },
1134      { PIPE_FORMAT_R8G8B8_SINT,
1135        PIPE_FORMAT_R8G8B8A8_SINT, 0 }
1136   },
1137   {
1138      { GL_ALPHA_INTEGER_EXT,
1139        GL_ALPHA8I_EXT, 0 },
1140      { PIPE_FORMAT_A8_SINT, 0 }
1141   },
1142   {
1143      { GL_ALPHA16I_EXT, 0 },
1144      { PIPE_FORMAT_A16_SINT, 0 }
1145   },
1146   {
1147      { GL_ALPHA32I_EXT, 0 },
1148      { PIPE_FORMAT_A32_SINT, 0 }
1149   },
1150   {
1151      { GL_ALPHA8UI_EXT, 0 },
1152      { PIPE_FORMAT_A8_UINT, 0 }
1153   },
1154   {
1155      { GL_ALPHA16UI_EXT, 0 },
1156      { PIPE_FORMAT_A16_UINT, 0 }
1157   },
1158   {
1159      { GL_ALPHA32UI_EXT, 0 },
1160      { PIPE_FORMAT_A32_UINT, 0 }
1161   },
1162   {
1163      { GL_INTENSITY8I_EXT, 0 },
1164      { PIPE_FORMAT_I8_SINT, 0 }
1165   },
1166   {
1167      { GL_INTENSITY16I_EXT, 0 },
1168      { PIPE_FORMAT_I16_SINT, 0 }
1169   },
1170   {
1171      { GL_INTENSITY32I_EXT, 0 },
1172      { PIPE_FORMAT_I32_SINT, 0 }
1173   },
1174   {
1175      { GL_INTENSITY8UI_EXT, 0 },
1176      { PIPE_FORMAT_I8_UINT, 0 }
1177   },
1178   {
1179      { GL_INTENSITY16UI_EXT, 0 },
1180      { PIPE_FORMAT_I16_UINT, 0 }
1181   },
1182   {
1183      { GL_INTENSITY32UI_EXT, 0 },
1184      { PIPE_FORMAT_I32_UINT, 0 }
1185   },
1186   {
1187      { GL_LUMINANCE8I_EXT, 0 },
1188      { PIPE_FORMAT_L8_SINT, 0 }
1189   },
1190   {
1191      { GL_LUMINANCE16I_EXT, 0 },
1192      { PIPE_FORMAT_L16_SINT, 0 }
1193   },
1194   {
1195      { GL_LUMINANCE32I_EXT, 0 },
1196      { PIPE_FORMAT_L32_SINT, 0 }
1197   },
1198   {
1199      { GL_LUMINANCE_INTEGER_EXT,
1200        GL_LUMINANCE8UI_EXT, 0 },
1201      { PIPE_FORMAT_L8_UINT, 0 }
1202   },
1203   {
1204      { GL_LUMINANCE16UI_EXT, 0 },
1205      { PIPE_FORMAT_L16_UINT, 0 }
1206   },
1207   {
1208      { GL_LUMINANCE32UI_EXT, 0 },
1209      { PIPE_FORMAT_L32_UINT, 0 }
1210   },
1211   {
1212      { GL_LUMINANCE_ALPHA_INTEGER_EXT,
1213        GL_LUMINANCE_ALPHA8I_EXT, 0 },
1214      { PIPE_FORMAT_L8A8_SINT, 0 }
1215   },
1216   {
1217      { GL_LUMINANCE_ALPHA16I_EXT, 0 },
1218      { PIPE_FORMAT_L16A16_SINT, 0 }
1219   },
1220   {
1221      { GL_LUMINANCE_ALPHA32I_EXT, 0 },
1222      { PIPE_FORMAT_L32A32_SINT, 0 }
1223   },
1224   {
1225      { GL_LUMINANCE_ALPHA8UI_EXT, 0 },
1226      { PIPE_FORMAT_L8A8_UINT, 0 }
1227   },
1228   {
1229      { GL_LUMINANCE_ALPHA16UI_EXT, 0 },
1230      { PIPE_FORMAT_L16A16_UINT, 0 }
1231   },
1232   {
1233      { GL_LUMINANCE_ALPHA32UI_EXT, 0 },
1234      { PIPE_FORMAT_L32A32_UINT, 0 }
1235   },
1236   {
1237      { GL_RGB16I_EXT, 0 },
1238      { PIPE_FORMAT_R16G16B16_SINT,
1239        PIPE_FORMAT_R16G16B16A16_SINT, 0 },
1240   },
1241   {
1242      { GL_RGBA16I_EXT, 0 },
1243      { PIPE_FORMAT_R16G16B16A16_SINT, 0 },
1244   },
1245   {
1246      { GL_RGB32I_EXT, 0 },
1247      { PIPE_FORMAT_R32G32B32_SINT,
1248        PIPE_FORMAT_R32G32B32A32_SINT, 0 },
1249   },
1250   {
1251      { GL_RGBA32I_EXT, 0 },
1252      { PIPE_FORMAT_R32G32B32A32_SINT, 0 }
1253   },
1254   {
1255      { GL_RGBA8UI_EXT, 0 },
1256      { PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1257   },
1258   {
1259      { GL_RGB8UI_EXT, 0 },
1260      { PIPE_FORMAT_R8G8B8_UINT,
1261        PIPE_FORMAT_R8G8B8A8_UINT, 0 }
1262   },
1263   {
1264      { GL_RGB16UI_EXT, 0 },
1265      { PIPE_FORMAT_R16G16B16_UINT,
1266        PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1267   },
1268   {
1269      { GL_RGBA16UI_EXT, 0 },
1270      { PIPE_FORMAT_R16G16B16A16_UINT, 0 }
1271   },
1272   {
1273      { GL_RGB32UI_EXT, 0},
1274      { PIPE_FORMAT_R32G32B32_UINT,
1275        PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1276   },
1277   {
1278      { GL_RGBA32UI_EXT, 0},
1279      { PIPE_FORMAT_R32G32B32A32_UINT, 0 }
1280   },
1281   {
1282     { GL_R8I, 0},
1283     { PIPE_FORMAT_R8_SINT, 0},
1284   },
1285   {
1286     { GL_R16I, 0},
1287     { PIPE_FORMAT_R16_SINT, 0},
1288   },
1289   {
1290     { GL_R32I, 0},
1291     { PIPE_FORMAT_R32_SINT, 0},
1292   },
1293  {
1294     { GL_R8UI, 0},
1295     { PIPE_FORMAT_R8_UINT, 0},
1296   },
1297   {
1298     { GL_R16UI, 0},
1299     { PIPE_FORMAT_R16_UINT, 0},
1300   },
1301   {
1302     { GL_R32UI, 0},
1303     { PIPE_FORMAT_R32_UINT, 0},
1304   },
1305   {
1306     { GL_RG8I, 0},
1307     { PIPE_FORMAT_R8G8_SINT, 0},
1308   },
1309   {
1310     { GL_RG16I, 0},
1311     { PIPE_FORMAT_R16G16_SINT, 0},
1312   },
1313   {
1314     { GL_RG32I, 0},
1315     { PIPE_FORMAT_R32G32_SINT, 0},
1316   },
1317  {
1318     { GL_RG8UI, 0},
1319     { PIPE_FORMAT_R8G8_UINT, 0},
1320   },
1321   {
1322     { GL_RG16UI, 0},
1323     { PIPE_FORMAT_R16G16_UINT, 0},
1324   },
1325   {
1326     { GL_RG32UI, 0},
1327     { PIPE_FORMAT_R32G32_UINT, 0},
1328   },
1329   /* signed normalized formats */
1330   {
1331      { GL_RED_SNORM, GL_R8_SNORM, 0 },
1332      { PIPE_FORMAT_R8_SNORM, PIPE_FORMAT_R8G8_SNORM,
1333        PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1334   },
1335   {
1336      { GL_R16_SNORM, 0 },
1337      { PIPE_FORMAT_R16_SNORM,
1338        PIPE_FORMAT_R16G16_SNORM,
1339        PIPE_FORMAT_R16G16B16A16_SNORM,
1340        PIPE_FORMAT_R8_SNORM,
1341        PIPE_FORMAT_R8G8_SNORM,
1342        PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1343   },
1344   {
1345      { GL_RG_SNORM, GL_RG8_SNORM, 0 },
1346      { PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1347   },
1348   {
1349      { GL_RG16_SNORM, 0 },
1350      { PIPE_FORMAT_R16G16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1351        PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1352   },
1353   {
1354      { GL_RGB_SNORM, GL_RGB8_SNORM, GL_RGBA_SNORM, GL_RGBA8_SNORM, 0 },
1355      { PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1356   },
1357   {
1358      { GL_RGB16_SNORM, GL_RGBA16_SNORM, 0 },
1359      { PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1360   },
1361   {
1362      { GL_ALPHA_SNORM, GL_ALPHA8_SNORM, 0 },
1363      { PIPE_FORMAT_A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1364   },
1365   {
1366      { GL_ALPHA16_SNORM, 0 },
1367      { PIPE_FORMAT_A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1368        PIPE_FORMAT_A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1369   },
1370   {
1371      { GL_LUMINANCE_SNORM, GL_LUMINANCE8_SNORM, 0 },
1372      { PIPE_FORMAT_L8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1373   },
1374   {
1375      { GL_LUMINANCE16_SNORM, 0 },
1376      { PIPE_FORMAT_L16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1377        PIPE_FORMAT_L8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1378   },
1379   {
1380      { GL_LUMINANCE_ALPHA_SNORM, GL_LUMINANCE8_ALPHA8_SNORM, 0 },
1381      { PIPE_FORMAT_L8A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1382   },
1383   {
1384      { GL_LUMINANCE16_ALPHA16_SNORM, 0 },
1385      { PIPE_FORMAT_L16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1386        PIPE_FORMAT_L8A8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1387   },
1388   {
1389      { GL_INTENSITY_SNORM, GL_INTENSITY8_SNORM, 0 },
1390      { PIPE_FORMAT_I8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1391   },
1392   {
1393      { GL_INTENSITY16_SNORM, 0 },
1394      { PIPE_FORMAT_I16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM,
1395        PIPE_FORMAT_I8_SNORM, PIPE_FORMAT_R8G8B8A8_SNORM, 0 }
1396   },
1397   {
1398      { GL_RGB9_E5, 0 },
1399      { PIPE_FORMAT_R9G9B9E5_FLOAT, 0 }
1400   },
1401   {
1402      { GL_R11F_G11F_B10F, 0 },
1403      { PIPE_FORMAT_R11G11B10_FLOAT, 0 }
1404   },
1405   {
1406      { GL_RGB10_A2UI, 0 },
1407      { PIPE_FORMAT_B10G10R10A2_UINT, 0 }
1408   },
1409};
1410
1411
1412/**
1413 * Return first supported format from the given list.
1414 */
1415static enum pipe_format
1416find_supported_format(struct pipe_screen *screen,
1417                      const enum pipe_format formats[],
1418                      enum pipe_texture_target target,
1419                      unsigned sample_count,
1420                      unsigned tex_usage)
1421{
1422   uint i;
1423   for (i = 0; formats[i]; i++) {
1424      if (screen->is_format_supported(screen, formats[i], target,
1425                                      sample_count, tex_usage)) {
1426         return formats[i];
1427      }
1428   }
1429   return PIPE_FORMAT_NONE;
1430}
1431
1432struct exact_format_mapping
1433{
1434   GLenum format;
1435   GLenum type;
1436   enum pipe_format pformat;
1437};
1438
1439static const struct exact_format_mapping rgba8888_tbl[] =
1440{
1441   { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_A8B8G8R8_UNORM },
1442   { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_A8B8G8R8_UNORM },
1443   { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_R8G8B8A8_UNORM },
1444   { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_R8G8B8A8_UNORM },
1445   { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_A8R8G8B8_UNORM },
1446   { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_B8G8R8A8_UNORM },
1447   { GL_RGBA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_R8G8B8A8_UNORM },
1448   { GL_ABGR_EXT, GL_UNSIGNED_BYTE,               PIPE_FORMAT_A8B8G8R8_UNORM },
1449   { GL_BGRA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_B8G8R8A8_UNORM },
1450   { 0,           0,                              0                          }
1451};
1452
1453static const struct exact_format_mapping rgbx8888_tbl[] =
1454{
1455   { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_X8R8G8B8_UNORM },
1456   { GL_BGRA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_B8G8R8X8_UNORM },
1457   { GL_BGRA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_B8G8R8X8_UNORM },
1458   /* No Mesa formats for these Gallium formats:
1459   { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_X8B8G8R8_UNORM },
1460   { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_X8B8G8R8_UNORM },
1461   { GL_RGBA,     GL_UNSIGNED_INT_8_8_8_8_REV,    PIPE_FORMAT_R8G8B8X8_UNORM },
1462   { GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8,        PIPE_FORMAT_R8G8B8X8_UNORM },
1463   { GL_RGBA,     GL_UNSIGNED_BYTE,               PIPE_FORMAT_R8G8B8X8_UNORM },
1464   { GL_ABGR_EXT, GL_UNSIGNED_BYTE,               PIPE_FORMAT_X8B8G8R8_UNORM },
1465   */
1466   { 0,           0,                              0                          }
1467};
1468
1469static const struct exact_format_mapping rgba1010102_tbl[] =
1470{
1471   { GL_BGRA,     GL_UNSIGNED_INT_2_10_10_10_REV, PIPE_FORMAT_B10G10R10A2_UNORM },
1472   /* No Mesa formats for these Gallium formats:
1473   { GL_RGBA,     GL_UNSIGNED_INT_2_10_10_10_REV, PIPE_FORMAT_R10G10B10A2_UNORM },
1474   { GL_ABGR_EXT, GL_UNSIGNED_INT_10_10_10_2,     PIPE_FORMAT_R10G10B10A2_UNORM },
1475   { GL_ABGR_EXT, GL_UNSIGNED_INT,                PIPE_FORMAT_R10G10B10A2_UNORM },
1476   */
1477   { 0,           0,                              0                             }
1478};
1479
1480/**
1481 * If there is an exact pipe_format match for {internalFormat, format, type}
1482 * return that, otherwise return PIPE_FORMAT_NONE so we can do fuzzy matching.
1483 */
1484static enum pipe_format
1485find_exact_format(GLint internalFormat, GLenum format, GLenum type)
1486{
1487   uint i;
1488   const struct exact_format_mapping* tbl;
1489
1490   if (format == GL_NONE || type == GL_NONE)
1491      return PIPE_FORMAT_NONE;
1492
1493   switch (internalFormat) {
1494   case 4:
1495   case GL_RGBA:
1496   case GL_RGBA8:
1497      tbl = rgba8888_tbl;
1498      break;
1499   case 3:
1500   case GL_RGB:
1501   case GL_RGB8:
1502      tbl = rgbx8888_tbl;
1503      break;
1504   case GL_RGB10_A2:
1505      tbl = rgba1010102_tbl;
1506      break;
1507   default:
1508      return PIPE_FORMAT_NONE;
1509   }
1510
1511   for (i = 0; tbl[i].format; i++)
1512      if (tbl[i].format == format && tbl[i].type == type)
1513         return tbl[i].pformat;
1514
1515   return PIPE_FORMAT_NONE;
1516}
1517
1518/**
1519 * Given an OpenGL internalFormat value for a texture or surface, return
1520 * the best matching PIPE_FORMAT_x, or PIPE_FORMAT_NONE if there's no match.
1521 * This is called during glTexImage2D, for example.
1522 *
1523 * The bindings parameter typically has PIPE_BIND_SAMPLER_VIEW set, plus
1524 * either PIPE_BINDING_RENDER_TARGET or PIPE_BINDING_DEPTH_STENCIL if
1525 * we want render-to-texture ability.
1526 *
1527 * \param internalFormat  the user value passed to glTexImage2D
1528 * \param target  one of PIPE_TEXTURE_x
1529 * \param bindings  bitmask of PIPE_BIND_x flags.
1530 */
1531enum pipe_format
1532st_choose_format(struct pipe_screen *screen, GLenum internalFormat,
1533                 GLenum format, GLenum type,
1534                 enum pipe_texture_target target, unsigned sample_count,
1535                 unsigned bindings)
1536{
1537   GET_CURRENT_CONTEXT(ctx); /* XXX this should be a function parameter */
1538   int i, j;
1539   enum pipe_format pf;
1540
1541   /* can't render to compressed formats at this time */
1542   if (_mesa_is_compressed_format(ctx, internalFormat)
1543       && (bindings & ~PIPE_BIND_SAMPLER_VIEW)) {
1544      return PIPE_FORMAT_NONE;
1545   }
1546
1547   /* search for exact matches */
1548   pf = find_exact_format(internalFormat, format, type);
1549   if (pf != PIPE_FORMAT_NONE &&
1550       screen->is_format_supported(screen, pf,
1551                                   target, sample_count, bindings))
1552      return pf;
1553
1554   /* search table for internalFormat */
1555   for (i = 0; i < Elements(format_map); i++) {
1556      const struct format_mapping *mapping = &format_map[i];
1557      for (j = 0; mapping->glFormats[j]; j++) {
1558         if (mapping->glFormats[j] == internalFormat) {
1559            /* Found the desired internal format.  Find first pipe format
1560             * which is supported by the driver.
1561             */
1562            return find_supported_format(screen, mapping->pipeFormats,
1563                                         target, sample_count, bindings);
1564         }
1565      }
1566   }
1567
1568   _mesa_problem(NULL, "unhandled format!\n");
1569   return PIPE_FORMAT_NONE;
1570}
1571
1572
1573/**
1574 * Called by FBO code to choose a PIPE_FORMAT_ for drawing surfaces.
1575 */
1576enum pipe_format
1577st_choose_renderbuffer_format(struct pipe_screen *screen,
1578                              GLenum internalFormat, unsigned sample_count)
1579{
1580   uint usage;
1581   if (_mesa_is_depth_or_stencil_format(internalFormat))
1582      usage = PIPE_BIND_DEPTH_STENCIL;
1583   else
1584      usage = PIPE_BIND_RENDER_TARGET;
1585   return st_choose_format(screen, internalFormat, GL_NONE, GL_NONE, PIPE_TEXTURE_2D,
1586                           sample_count, usage);
1587}
1588
1589
1590gl_format
1591st_ChooseTextureFormat_renderable(struct gl_context *ctx, GLint internalFormat,
1592				  GLenum format, GLenum type, GLboolean renderable)
1593{
1594   struct pipe_screen *screen = st_context(ctx)->pipe->screen;
1595   enum pipe_format pFormat;
1596   uint bindings;
1597
1598   (void) format;
1599   (void) type;
1600
1601   /* GL textures may wind up being render targets, but we don't know
1602    * that in advance.  Specify potential render target flags now.
1603    */
1604   bindings = PIPE_BIND_SAMPLER_VIEW;
1605   if (renderable) {
1606      if (_mesa_is_depth_or_stencil_format(internalFormat))
1607	 bindings |= PIPE_BIND_DEPTH_STENCIL;
1608      else
1609	 bindings |= PIPE_BIND_RENDER_TARGET;
1610   }
1611
1612   pFormat = st_choose_format(screen, internalFormat, format, type,
1613                              PIPE_TEXTURE_2D, 0, bindings);
1614
1615   if (pFormat == PIPE_FORMAT_NONE) {
1616      /* try choosing format again, this time without render target bindings */
1617      pFormat = st_choose_format(screen, internalFormat, format, type,
1618                                 PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW);
1619   }
1620
1621   if (pFormat == PIPE_FORMAT_NONE) {
1622      /* no luck at all */
1623      return MESA_FORMAT_NONE;
1624   }
1625
1626   return st_pipe_format_to_mesa_format(pFormat);
1627}
1628
1629
1630/**
1631 * Called via ctx->Driver.ChooseTextureFormat().
1632 */
1633gl_format
1634st_ChooseTextureFormat(struct gl_context *ctx, GLenum target,
1635                       GLint internalFormat,
1636                       GLenum format, GLenum type)
1637{
1638   boolean want_renderable =
1639      internalFormat == 3 || internalFormat == 4 ||
1640      internalFormat == GL_RGB || internalFormat == GL_RGBA ||
1641      internalFormat == GL_RGB8 || internalFormat == GL_RGBA8 ||
1642      internalFormat == GL_BGRA;
1643
1644   if (target == GL_TEXTURE_1D || target == GL_TEXTURE_1D_ARRAY) {
1645      /* We don't do compression for these texture targets because of
1646       * difficulty with sub-texture updates on non-block boundaries, etc.
1647       * So change the internal format request to an uncompressed format.
1648       */
1649      internalFormat =
1650        _mesa_generic_compressed_format_to_uncompressed_format(internalFormat);
1651   }
1652
1653   return st_ChooseTextureFormat_renderable(ctx, internalFormat,
1654					    format, type, want_renderable);
1655}
1656
1657
1658GLboolean
1659st_sampler_compat_formats(enum pipe_format format1, enum pipe_format format2)
1660{
1661   if (format1 == format2)
1662      return GL_TRUE;
1663
1664   if (format1 == PIPE_FORMAT_B8G8R8A8_UNORM &&
1665       format2 == PIPE_FORMAT_B8G8R8X8_UNORM)
1666      return GL_TRUE;
1667
1668   if (format1 == PIPE_FORMAT_B8G8R8X8_UNORM &&
1669       format2 == PIPE_FORMAT_B8G8R8A8_UNORM)
1670      return GL_TRUE;
1671
1672   if (format1 == PIPE_FORMAT_A8B8G8R8_UNORM &&
1673       format2 == PIPE_FORMAT_X8B8G8R8_UNORM)
1674      return GL_TRUE;
1675
1676   if (format1 == PIPE_FORMAT_X8B8G8R8_UNORM &&
1677       format2 == PIPE_FORMAT_A8B8G8R8_UNORM)
1678      return GL_TRUE;
1679
1680   if (format1 == PIPE_FORMAT_A8R8G8B8_UNORM &&
1681       format2 == PIPE_FORMAT_X8R8G8B8_UNORM)
1682      return GL_TRUE;
1683
1684   if (format1 == PIPE_FORMAT_X8R8G8B8_UNORM &&
1685       format2 == PIPE_FORMAT_A8R8G8B8_UNORM)
1686      return GL_TRUE;
1687
1688   return GL_FALSE;
1689}
1690
1691
1692
1693/**
1694 * This is used for translating texture border color and the clear
1695 * color.  For example, the clear color is interpreted according to
1696 * the renderbuffer's base format.  For example, if clearing a
1697 * GL_LUMINANCE buffer, we'll return colorOut[0] = colorOut[1] =
1698 * colorOut[2] = colorIn[0].
1699 * Similarly for texture border colors.
1700 */
1701void
1702st_translate_color(union gl_color_union *colorIn,
1703                   union pipe_color_union *colorOut,
1704                   GLenum baseFormat, GLboolean is_integer)
1705{
1706   if (is_integer) {
1707      int *in = colorIn->i;
1708      int *out = colorOut->i;
1709
1710      switch (baseFormat) {
1711      case GL_RED:
1712         out[0] = in[0];
1713         out[1] = 0;
1714         out[2] = 0;
1715         out[3] = 1;
1716         break;
1717      case GL_RG:
1718         out[0] = in[0];
1719         out[1] = in[1];
1720         out[2] = 0;
1721         out[3] = 1;
1722         break;
1723      case GL_RGB:
1724         out[0] = in[0];
1725         out[1] = in[1];
1726         out[2] = in[2];
1727         out[3] = 1;
1728         break;
1729      case GL_ALPHA:
1730         out[0] = out[1] = out[2] = 0;
1731         out[3] = in[3];
1732         break;
1733      case GL_LUMINANCE:
1734         out[0] = out[1] = out[2] = in[0];
1735         out[3] = 1;
1736         break;
1737      case GL_LUMINANCE_ALPHA:
1738         out[0] = out[1] = out[2] = in[0];
1739         out[3] = in[3];
1740         break;
1741      case GL_INTENSITY:
1742         out[0] = out[1] = out[2] = out[3] = in[0];
1743         break;
1744      default:
1745         COPY_4V(out, in);
1746      }
1747   }
1748   else {
1749      float *in = colorIn->f;
1750      float *out = colorOut->f;
1751
1752      switch (baseFormat) {
1753      case GL_RED:
1754         out[0] = in[0];
1755         out[1] = 0.0F;
1756         out[2] = 0.0F;
1757         out[3] = 1.0F;
1758         break;
1759      case GL_RG:
1760         out[0] = in[0];
1761         out[1] = in[1];
1762         out[2] = 0.0F;
1763         out[3] = 1.0F;
1764         break;
1765      case GL_RGB:
1766         out[0] = in[0];
1767         out[1] = in[1];
1768         out[2] = in[2];
1769         out[3] = 1.0F;
1770         break;
1771      case GL_ALPHA:
1772         out[0] = out[1] = out[2] = 0.0F;
1773         out[3] = in[3];
1774         break;
1775      case GL_LUMINANCE:
1776         out[0] = out[1] = out[2] = in[0];
1777         out[3] = 1.0F;
1778         break;
1779      case GL_LUMINANCE_ALPHA:
1780         out[0] = out[1] = out[2] = in[0];
1781         out[3] = in[3];
1782         break;
1783      case GL_INTENSITY:
1784         out[0] = out[1] = out[2] = out[3] = in[0];
1785         break;
1786      default:
1787         COPY_4V(out, in);
1788      }
1789   }
1790}
1791