Merge branch '7.8'
[mesa.git] / src / mesa / state_tracker / st_format.c
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/texstore.h"
38 #include "main/enums.h"
39 #include "main/macros.h"
40
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_screen.h"
44 #include "util/u_format.h"
45 #include "st_context.h"
46 #include "st_format.h"
47
48
49 static GLuint
50 format_max_bits(enum pipe_format format)
51 {
52 GLuint size = util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0);
53
54 size = MAX2(size, util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 1));
55 size = MAX2(size, util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 2));
56 size = MAX2(size, util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 3));
57 size = MAX2(size, util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0));
58 size = MAX2(size, util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1));
59 return size;
60 }
61
62
63 /**
64 * Return basic GL datatype for the given gallium format.
65 */
66 GLenum
67 st_format_datatype(enum pipe_format format)
68 {
69 const struct util_format_description *desc;
70
71 desc = util_format_description(format);
72 assert(desc);
73
74 if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
75 if (format == PIPE_FORMAT_B5G5R5A1_UNORM ||
76 format == PIPE_FORMAT_B5G6R5_UNORM) {
77 return GL_UNSIGNED_SHORT;
78 }
79 else if (format == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
80 format == PIPE_FORMAT_S8_USCALED_Z24_UNORM) {
81 return GL_UNSIGNED_INT_24_8;
82 }
83 else {
84 const GLuint size = format_max_bits(format);
85 if (size == 8) {
86 if (desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED)
87 return GL_UNSIGNED_BYTE;
88 else
89 return GL_BYTE;
90 }
91 else if (size == 16) {
92 if (desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED)
93 return GL_UNSIGNED_SHORT;
94 else
95 return GL_SHORT;
96 }
97 else {
98 assert( size <= 32 );
99 if (desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED)
100 return GL_UNSIGNED_INT;
101 else
102 return GL_INT;
103 }
104 }
105 }
106 else if (format == PIPE_FORMAT_UYVY) {
107 return GL_UNSIGNED_SHORT;
108 }
109 else if (format == PIPE_FORMAT_YUYV) {
110 return GL_UNSIGNED_SHORT;
111 }
112 else {
113 /* compressed format? */
114 assert(0);
115 }
116
117 assert(0);
118 return GL_NONE;
119 }
120
121
122 /**
123 * Translate Mesa format to Gallium format.
124 */
125 enum pipe_format
126 st_mesa_format_to_pipe_format(gl_format mesaFormat)
127 {
128 switch (mesaFormat) {
129 case MESA_FORMAT_RGBA8888:
130 return PIPE_FORMAT_A8B8G8R8_UNORM;
131 case MESA_FORMAT_RGBA8888_REV:
132 return PIPE_FORMAT_R8G8B8A8_UNORM;
133 case MESA_FORMAT_ARGB8888:
134 return PIPE_FORMAT_B8G8R8A8_UNORM;
135 case MESA_FORMAT_ARGB8888_REV:
136 return PIPE_FORMAT_A8R8G8B8_UNORM;
137 case MESA_FORMAT_XRGB8888:
138 return PIPE_FORMAT_B8G8R8X8_UNORM;
139 case MESA_FORMAT_XRGB8888_REV:
140 return PIPE_FORMAT_X8R8G8B8_UNORM;
141 case MESA_FORMAT_ARGB1555:
142 return PIPE_FORMAT_B5G5R5A1_UNORM;
143 case MESA_FORMAT_ARGB4444:
144 return PIPE_FORMAT_B4G4R4A4_UNORM;
145 case MESA_FORMAT_RGB565:
146 return PIPE_FORMAT_B5G6R5_UNORM;
147 case MESA_FORMAT_AL88:
148 return PIPE_FORMAT_L8A8_UNORM;
149 case MESA_FORMAT_A8:
150 return PIPE_FORMAT_A8_UNORM;
151 case MESA_FORMAT_L8:
152 return PIPE_FORMAT_L8_UNORM;
153 case MESA_FORMAT_I8:
154 return PIPE_FORMAT_I8_UNORM;
155 case MESA_FORMAT_Z16:
156 return PIPE_FORMAT_Z16_UNORM;
157 case MESA_FORMAT_Z32:
158 return PIPE_FORMAT_Z32_UNORM;
159 case MESA_FORMAT_Z24_S8:
160 return PIPE_FORMAT_S8_USCALED_Z24_UNORM;
161 case MESA_FORMAT_S8_Z24:
162 return PIPE_FORMAT_Z24_UNORM_S8_USCALED;
163 case MESA_FORMAT_Z24_X8:
164 return PIPE_FORMAT_X8Z24_UNORM;
165 case MESA_FORMAT_X8_Z24:
166 return PIPE_FORMAT_Z24X8_UNORM;
167 case MESA_FORMAT_YCBCR:
168 return PIPE_FORMAT_UYVY;
169 #if FEATURE_texture_s3tc
170 case MESA_FORMAT_RGB_DXT1:
171 return PIPE_FORMAT_DXT1_RGB;
172 case MESA_FORMAT_RGBA_DXT1:
173 return PIPE_FORMAT_DXT1_RGBA;
174 case MESA_FORMAT_RGBA_DXT3:
175 return PIPE_FORMAT_DXT3_RGBA;
176 case MESA_FORMAT_RGBA_DXT5:
177 return PIPE_FORMAT_DXT5_RGBA;
178 #if FEATURE_EXT_texture_sRGB
179 case MESA_FORMAT_SRGB_DXT1:
180 return PIPE_FORMAT_DXT1_SRGB;
181 case MESA_FORMAT_SRGBA_DXT1:
182 return PIPE_FORMAT_DXT1_SRGBA;
183 case MESA_FORMAT_SRGBA_DXT3:
184 return PIPE_FORMAT_DXT3_SRGBA;
185 case MESA_FORMAT_SRGBA_DXT5:
186 return PIPE_FORMAT_DXT5_SRGBA;
187 #endif
188 #endif
189 #if FEATURE_EXT_texture_sRGB
190 case MESA_FORMAT_SLA8:
191 return PIPE_FORMAT_L8A8_SRGB;
192 case MESA_FORMAT_SL8:
193 return PIPE_FORMAT_L8_SRGB;
194 case MESA_FORMAT_SRGB8:
195 return PIPE_FORMAT_R8G8B8_SRGB;
196 case MESA_FORMAT_SRGBA8:
197 return PIPE_FORMAT_A8B8G8R8_SRGB;
198 case MESA_FORMAT_SARGB8:
199 return PIPE_FORMAT_B8G8R8A8_SRGB;
200 #endif
201 default:
202 assert(0);
203 return PIPE_FORMAT_NONE;
204 }
205 }
206
207
208 /**
209 * Translate Gallium format to Mesa format.
210 */
211 gl_format
212 st_pipe_format_to_mesa_format(enum pipe_format format)
213 {
214 switch (format) {
215 case PIPE_FORMAT_A8B8G8R8_UNORM:
216 return MESA_FORMAT_RGBA8888;
217 case PIPE_FORMAT_R8G8B8A8_UNORM:
218 return MESA_FORMAT_RGBA8888_REV;
219 case PIPE_FORMAT_B8G8R8A8_UNORM:
220 return MESA_FORMAT_ARGB8888;
221 case PIPE_FORMAT_A8R8G8B8_UNORM:
222 return MESA_FORMAT_ARGB8888_REV;
223 case PIPE_FORMAT_B8G8R8X8_UNORM:
224 return MESA_FORMAT_XRGB8888;
225 case PIPE_FORMAT_X8R8G8B8_UNORM:
226 return MESA_FORMAT_XRGB8888_REV;
227 case PIPE_FORMAT_B5G5R5A1_UNORM:
228 return MESA_FORMAT_ARGB1555;
229 case PIPE_FORMAT_B4G4R4A4_UNORM:
230 return MESA_FORMAT_ARGB4444;
231 case PIPE_FORMAT_B5G6R5_UNORM:
232 return MESA_FORMAT_RGB565;
233 case PIPE_FORMAT_L8A8_UNORM:
234 return MESA_FORMAT_AL88;
235 case PIPE_FORMAT_A8_UNORM:
236 return MESA_FORMAT_A8;
237 case PIPE_FORMAT_L8_UNORM:
238 return MESA_FORMAT_L8;
239 case PIPE_FORMAT_I8_UNORM:
240 return MESA_FORMAT_I8;
241 case PIPE_FORMAT_S8_USCALED:
242 return MESA_FORMAT_S8;
243
244 case PIPE_FORMAT_R16G16B16A16_SNORM:
245 return MESA_FORMAT_SIGNED_RGBA_16;
246
247 case PIPE_FORMAT_Z16_UNORM:
248 return MESA_FORMAT_Z16;
249 case PIPE_FORMAT_Z32_UNORM:
250 return MESA_FORMAT_Z32;
251 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
252 return MESA_FORMAT_Z24_S8;
253 case PIPE_FORMAT_X8Z24_UNORM:
254 return MESA_FORMAT_Z24_X8;
255 case PIPE_FORMAT_Z24X8_UNORM:
256 return MESA_FORMAT_X8_Z24;
257 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
258 return MESA_FORMAT_S8_Z24;
259
260 case PIPE_FORMAT_UYVY:
261 return MESA_FORMAT_YCBCR;
262 case PIPE_FORMAT_YUYV:
263 return MESA_FORMAT_YCBCR_REV;
264
265 #if FEATURE_texture_s3tc
266 case PIPE_FORMAT_DXT1_RGB:
267 return MESA_FORMAT_RGB_DXT1;
268 case PIPE_FORMAT_DXT1_RGBA:
269 return MESA_FORMAT_RGBA_DXT1;
270 case PIPE_FORMAT_DXT3_RGBA:
271 return MESA_FORMAT_RGBA_DXT3;
272 case PIPE_FORMAT_DXT5_RGBA:
273 return MESA_FORMAT_RGBA_DXT5;
274 #if FEATURE_EXT_texture_sRGB
275 case PIPE_FORMAT_DXT1_SRGB:
276 return MESA_FORMAT_SRGB_DXT1;
277 case PIPE_FORMAT_DXT1_SRGBA:
278 return MESA_FORMAT_SRGBA_DXT1;
279 case PIPE_FORMAT_DXT3_SRGBA:
280 return MESA_FORMAT_SRGBA_DXT3;
281 case PIPE_FORMAT_DXT5_SRGBA:
282 return MESA_FORMAT_SRGBA_DXT5;
283 #endif
284 #endif
285
286 #if FEATURE_EXT_texture_sRGB
287 case PIPE_FORMAT_L8A8_SRGB:
288 return MESA_FORMAT_SLA8;
289 case PIPE_FORMAT_L8_SRGB:
290 return MESA_FORMAT_SL8;
291 case PIPE_FORMAT_R8G8B8_SRGB:
292 return MESA_FORMAT_SRGB8;
293 case PIPE_FORMAT_A8B8G8R8_SRGB:
294 return MESA_FORMAT_SRGBA8;
295 case PIPE_FORMAT_B8G8R8A8_SRGB:
296 return MESA_FORMAT_SARGB8;
297 #endif
298 default:
299 assert(0);
300 return MESA_FORMAT_NONE;
301 }
302 }
303
304
305 /**
306 * Find an RGBA format supported by the context/winsys.
307 */
308 static enum pipe_format
309 default_rgba_format(struct pipe_screen *screen,
310 enum pipe_texture_target target,
311 unsigned tex_usage,
312 unsigned geom_flags)
313 {
314 static const enum pipe_format colorFormats[] = {
315 PIPE_FORMAT_B8G8R8A8_UNORM,
316 PIPE_FORMAT_A8R8G8B8_UNORM,
317 PIPE_FORMAT_A8B8G8R8_UNORM,
318 PIPE_FORMAT_B5G6R5_UNORM
319 };
320 uint i;
321 for (i = 0; i < Elements(colorFormats); i++) {
322 if (screen->is_format_supported( screen, colorFormats[i], target, tex_usage, geom_flags )) {
323 return colorFormats[i];
324 }
325 }
326 return PIPE_FORMAT_NONE;
327 }
328
329 /**
330 * Find an RGB format supported by the context/winsys.
331 */
332 static enum pipe_format
333 default_rgb_format(struct pipe_screen *screen,
334 enum pipe_texture_target target,
335 unsigned tex_usage,
336 unsigned geom_flags)
337 {
338 static const enum pipe_format colorFormats[] = {
339 PIPE_FORMAT_B8G8R8X8_UNORM,
340 PIPE_FORMAT_X8R8G8B8_UNORM,
341 PIPE_FORMAT_X8B8G8R8_UNORM,
342 PIPE_FORMAT_B8G8R8A8_UNORM,
343 PIPE_FORMAT_A8R8G8B8_UNORM,
344 PIPE_FORMAT_A8B8G8R8_UNORM,
345 PIPE_FORMAT_B5G6R5_UNORM
346 };
347 uint i;
348 for (i = 0; i < Elements(colorFormats); i++) {
349 if (screen->is_format_supported( screen, colorFormats[i], target, tex_usage, geom_flags )) {
350 return colorFormats[i];
351 }
352 }
353 return PIPE_FORMAT_NONE;
354 }
355
356 /**
357 * Find an sRGBA format supported by the context/winsys.
358 */
359 static enum pipe_format
360 default_srgba_format(struct pipe_screen *screen,
361 enum pipe_texture_target target,
362 unsigned tex_usage,
363 unsigned geom_flags)
364 {
365 static const enum pipe_format colorFormats[] = {
366 PIPE_FORMAT_B8G8R8A8_SRGB,
367 PIPE_FORMAT_A8R8G8B8_SRGB,
368 PIPE_FORMAT_A8B8G8R8_SRGB,
369 };
370 uint i;
371 for (i = 0; i < Elements(colorFormats); i++) {
372 if (screen->is_format_supported( screen, colorFormats[i], target, tex_usage, geom_flags )) {
373 return colorFormats[i];
374 }
375 }
376 return PIPE_FORMAT_NONE;
377 }
378
379 /**
380 * Search list of formats for first RGBA format with >8 bits/channel.
381 */
382 static enum pipe_format
383 default_deep_rgba_format(struct pipe_screen *screen,
384 enum pipe_texture_target target,
385 unsigned tex_usage,
386 unsigned geom_flags)
387 {
388 if (screen->is_format_supported(screen, PIPE_FORMAT_R16G16B16A16_SNORM, target, tex_usage, geom_flags)) {
389 return PIPE_FORMAT_R16G16B16A16_SNORM;
390 }
391 if (tex_usage & PIPE_BIND_RENDER_TARGET)
392 return default_rgba_format(screen, target, tex_usage, geom_flags);
393 else
394 return PIPE_FORMAT_NONE;
395 }
396
397
398 /**
399 * Find an Z format supported by the context/winsys.
400 */
401 static enum pipe_format
402 default_depth_format(struct pipe_screen *screen,
403 enum pipe_texture_target target,
404 unsigned tex_usage,
405 unsigned geom_flags)
406 {
407 static const enum pipe_format zFormats[] = {
408 PIPE_FORMAT_Z16_UNORM,
409 PIPE_FORMAT_Z32_UNORM,
410 PIPE_FORMAT_Z24_UNORM_S8_USCALED,
411 PIPE_FORMAT_S8_USCALED_Z24_UNORM
412 };
413 uint i;
414 for (i = 0; i < Elements(zFormats); i++) {
415 if (screen->is_format_supported( screen, zFormats[i], target, tex_usage, geom_flags )) {
416 return zFormats[i];
417 }
418 }
419 return PIPE_FORMAT_NONE;
420 }
421
422
423 /**
424 * Given an OpenGL internalFormat value for a texture or surface, return
425 * the best matching PIPE_FORMAT_x, or PIPE_FORMAT_NONE if there's no match.
426 * \param target one of PIPE_TEXTURE_x
427 * \param tex_usage either PIPE_BIND_RENDER_TARGET
428 * or PIPE_BIND_SAMPLER_VIEW
429 */
430 enum pipe_format
431 st_choose_format(struct pipe_screen *screen, GLenum internalFormat,
432 enum pipe_texture_target target, unsigned tex_usage)
433 {
434 unsigned geom_flags = 0;
435
436 switch (internalFormat) {
437 case 4:
438 case GL_RGBA:
439 case GL_COMPRESSED_RGBA:
440 case GL_RGBA8:
441 case GL_RGB10_A2:
442 case GL_RGBA12:
443 return default_rgba_format( screen, target, tex_usage, geom_flags );
444 case 3:
445 case GL_RGB:
446 case GL_COMPRESSED_RGB:
447 return default_rgb_format( screen, target, tex_usage, geom_flags );
448 case GL_RGBA16:
449 if (tex_usage & PIPE_BIND_RENDER_TARGET)
450 return default_deep_rgba_format( screen, target, tex_usage, geom_flags );
451 else
452 return default_rgba_format( screen, target, tex_usage, geom_flags );
453
454 case GL_RGBA4:
455 case GL_RGBA2:
456 if (screen->is_format_supported( screen, PIPE_FORMAT_B4G4R4A4_UNORM, target, tex_usage, geom_flags ))
457 return PIPE_FORMAT_B4G4R4A4_UNORM;
458 return default_rgba_format( screen, target, tex_usage, geom_flags );
459
460 case GL_RGB5_A1:
461 if (screen->is_format_supported( screen, PIPE_FORMAT_B5G5R5A1_UNORM, target, tex_usage, geom_flags ))
462 return PIPE_FORMAT_B5G5R5A1_UNORM;
463 return default_rgba_format( screen, target, tex_usage, geom_flags );
464
465 case GL_RGB8:
466 case GL_RGB10:
467 case GL_RGB12:
468 case GL_RGB16:
469 return default_rgb_format( screen, target, tex_usage, geom_flags );
470
471 case GL_RGB5:
472 case GL_RGB4:
473 case GL_R3_G3_B2:
474 if (screen->is_format_supported( screen, PIPE_FORMAT_B5G6R5_UNORM, target, tex_usage, geom_flags ))
475 return PIPE_FORMAT_B5G6R5_UNORM;
476 if (screen->is_format_supported( screen, PIPE_FORMAT_B5G5R5A1_UNORM, target, tex_usage, geom_flags ))
477 return PIPE_FORMAT_B5G5R5A1_UNORM;
478 return default_rgba_format( screen, target, tex_usage, geom_flags );
479
480 case GL_ALPHA:
481 case GL_ALPHA4:
482 case GL_ALPHA8:
483 case GL_ALPHA12:
484 case GL_ALPHA16:
485 case GL_COMPRESSED_ALPHA:
486 if (screen->is_format_supported( screen, PIPE_FORMAT_A8_UNORM, target, tex_usage, geom_flags ))
487 return PIPE_FORMAT_A8_UNORM;
488 return default_rgba_format( screen, target, tex_usage, geom_flags );
489
490 case 1:
491 case GL_LUMINANCE:
492 case GL_LUMINANCE4:
493 case GL_LUMINANCE8:
494 case GL_LUMINANCE12:
495 case GL_LUMINANCE16:
496 case GL_COMPRESSED_LUMINANCE:
497 if (screen->is_format_supported( screen, PIPE_FORMAT_L8_UNORM, target, tex_usage, geom_flags ))
498 return PIPE_FORMAT_L8_UNORM;
499 return default_rgba_format( screen, target, tex_usage, geom_flags );
500
501 case 2:
502 case GL_LUMINANCE_ALPHA:
503 case GL_LUMINANCE4_ALPHA4:
504 case GL_LUMINANCE6_ALPHA2:
505 case GL_LUMINANCE8_ALPHA8:
506 case GL_LUMINANCE12_ALPHA4:
507 case GL_LUMINANCE12_ALPHA12:
508 case GL_LUMINANCE16_ALPHA16:
509 case GL_COMPRESSED_LUMINANCE_ALPHA:
510 if (screen->is_format_supported( screen, PIPE_FORMAT_L8A8_UNORM, target, tex_usage, geom_flags ))
511 return PIPE_FORMAT_L8A8_UNORM;
512 return default_rgba_format( screen, target, tex_usage, geom_flags );
513
514 case GL_INTENSITY:
515 case GL_INTENSITY4:
516 case GL_INTENSITY8:
517 case GL_INTENSITY12:
518 case GL_INTENSITY16:
519 case GL_COMPRESSED_INTENSITY:
520 if (screen->is_format_supported( screen, PIPE_FORMAT_I8_UNORM, target, tex_usage, geom_flags ))
521 return PIPE_FORMAT_I8_UNORM;
522 return default_rgba_format( screen, target, tex_usage, geom_flags );
523
524 case GL_YCBCR_MESA:
525 if (screen->is_format_supported(screen, PIPE_FORMAT_UYVY,
526 target, tex_usage, geom_flags)) {
527 return PIPE_FORMAT_UYVY;
528 }
529 if (screen->is_format_supported(screen, PIPE_FORMAT_YUYV,
530 target, tex_usage, geom_flags)) {
531 return PIPE_FORMAT_YUYV;
532 }
533 return PIPE_FORMAT_NONE;
534
535 case GL_RGB_S3TC:
536 case GL_RGB4_S3TC:
537 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
538 return PIPE_FORMAT_DXT1_RGB;
539
540 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
541 return PIPE_FORMAT_DXT1_RGBA;
542
543 case GL_RGBA_S3TC:
544 case GL_RGBA4_S3TC:
545 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
546 return PIPE_FORMAT_DXT3_RGBA;
547
548 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
549 return PIPE_FORMAT_DXT5_RGBA;
550
551 #if 0
552 case GL_COMPRESSED_RGB_FXT1_3DFX:
553 return PIPE_FORMAT_RGB_FXT1;
554 case GL_COMPRESSED_RGBA_FXT1_3DFX:
555 return PIPE_FORMAT_RGB_FXT1;
556 #endif
557
558 case GL_DEPTH_COMPONENT16:
559 if (screen->is_format_supported( screen, PIPE_FORMAT_Z16_UNORM, target, tex_usage, geom_flags ))
560 return PIPE_FORMAT_Z16_UNORM;
561 /* fall-through */
562 case GL_DEPTH_COMPONENT24:
563 if (screen->is_format_supported( screen, PIPE_FORMAT_Z24_UNORM_S8_USCALED, target, tex_usage, geom_flags ))
564 return PIPE_FORMAT_Z24_UNORM_S8_USCALED;
565 if (screen->is_format_supported( screen, PIPE_FORMAT_S8_USCALED_Z24_UNORM, target, tex_usage, geom_flags ))
566 return PIPE_FORMAT_S8_USCALED_Z24_UNORM;
567 /* fall-through */
568 case GL_DEPTH_COMPONENT32:
569 if (screen->is_format_supported( screen, PIPE_FORMAT_Z32_UNORM, target, tex_usage, geom_flags ))
570 return PIPE_FORMAT_Z32_UNORM;
571 /* fall-through */
572 case GL_DEPTH_COMPONENT:
573 return default_depth_format( screen, target, tex_usage, geom_flags );
574
575 case GL_STENCIL_INDEX:
576 case GL_STENCIL_INDEX1_EXT:
577 case GL_STENCIL_INDEX4_EXT:
578 case GL_STENCIL_INDEX8_EXT:
579 case GL_STENCIL_INDEX16_EXT:
580 if (screen->is_format_supported( screen, PIPE_FORMAT_S8_USCALED, target, tex_usage, geom_flags ))
581 return PIPE_FORMAT_S8_USCALED;
582 if (screen->is_format_supported( screen, PIPE_FORMAT_Z24_UNORM_S8_USCALED, target, tex_usage, geom_flags ))
583 return PIPE_FORMAT_Z24_UNORM_S8_USCALED;
584 if (screen->is_format_supported( screen, PIPE_FORMAT_S8_USCALED_Z24_UNORM, target, tex_usage, geom_flags ))
585 return PIPE_FORMAT_S8_USCALED_Z24_UNORM;
586 return PIPE_FORMAT_NONE;
587
588 case GL_DEPTH_STENCIL_EXT:
589 case GL_DEPTH24_STENCIL8_EXT:
590 if (screen->is_format_supported( screen, PIPE_FORMAT_Z24_UNORM_S8_USCALED, target, tex_usage, geom_flags ))
591 return PIPE_FORMAT_Z24_UNORM_S8_USCALED;
592 if (screen->is_format_supported( screen, PIPE_FORMAT_S8_USCALED_Z24_UNORM, target, tex_usage, geom_flags ))
593 return PIPE_FORMAT_S8_USCALED_Z24_UNORM;
594 return PIPE_FORMAT_NONE;
595
596 case GL_SRGB_EXT:
597 case GL_SRGB8_EXT:
598 case GL_COMPRESSED_SRGB_EXT:
599 case GL_COMPRESSED_SRGB_ALPHA_EXT:
600 case GL_SRGB_ALPHA_EXT:
601 case GL_SRGB8_ALPHA8_EXT:
602 return default_srgba_format( screen, target, tex_usage, geom_flags );
603 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
604 return PIPE_FORMAT_DXT1_SRGB;
605 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
606 return PIPE_FORMAT_DXT1_SRGBA;
607 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
608 return PIPE_FORMAT_DXT3_SRGBA;
609 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
610 return PIPE_FORMAT_DXT5_SRGBA;
611
612 case GL_SLUMINANCE_ALPHA_EXT:
613 case GL_SLUMINANCE8_ALPHA8_EXT:
614 case GL_COMPRESSED_SLUMINANCE_EXT:
615 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
616 if (screen->is_format_supported( screen, PIPE_FORMAT_L8A8_SRGB, target, tex_usage, geom_flags ))
617 return PIPE_FORMAT_L8A8_SRGB;
618 return default_srgba_format( screen, target, tex_usage, geom_flags );
619
620 case GL_SLUMINANCE_EXT:
621 case GL_SLUMINANCE8_EXT:
622 if (screen->is_format_supported( screen, PIPE_FORMAT_L8_SRGB, target, tex_usage, geom_flags ))
623 return PIPE_FORMAT_L8_SRGB;
624 return default_srgba_format( screen, target, tex_usage, geom_flags );
625
626 default:
627 return PIPE_FORMAT_NONE;
628 }
629 }
630
631
632 static GLboolean
633 is_depth_or_stencil_format(GLenum internalFormat)
634 {
635 switch (internalFormat) {
636 case GL_DEPTH_COMPONENT:
637 case GL_DEPTH_COMPONENT16:
638 case GL_DEPTH_COMPONENT24:
639 case GL_DEPTH_COMPONENT32:
640 case GL_STENCIL_INDEX:
641 case GL_STENCIL_INDEX1_EXT:
642 case GL_STENCIL_INDEX4_EXT:
643 case GL_STENCIL_INDEX8_EXT:
644 case GL_STENCIL_INDEX16_EXT:
645 case GL_DEPTH_STENCIL_EXT:
646 case GL_DEPTH24_STENCIL8_EXT:
647 return GL_TRUE;
648 default:
649 return GL_FALSE;
650 }
651 }
652
653 /**
654 * Called by FBO code to choose a PIPE_FORMAT_ for drawing surfaces.
655 */
656 enum pipe_format
657 st_choose_renderbuffer_format(struct pipe_screen *screen,
658 GLenum internalFormat)
659 {
660 uint usage;
661 if (is_depth_or_stencil_format(internalFormat))
662 usage = PIPE_BIND_DEPTH_STENCIL;
663 else
664 usage = PIPE_BIND_RENDER_TARGET;
665 return st_choose_format(screen, internalFormat, PIPE_TEXTURE_2D, usage);
666 }
667
668
669 /**
670 * Called via ctx->Driver.chooseTextureFormat().
671 */
672 gl_format
673 st_ChooseTextureFormat(GLcontext *ctx, GLint internalFormat,
674 GLenum format, GLenum type)
675 {
676 enum pipe_format pFormat;
677
678 (void) format;
679 (void) type;
680
681 pFormat = st_choose_format(ctx->st->pipe->screen, internalFormat,
682 PIPE_TEXTURE_2D, PIPE_BIND_SAMPLER_VIEW);
683 if (pFormat == PIPE_FORMAT_NONE)
684 return MESA_FORMAT_NONE;
685
686 return st_pipe_format_to_mesa_format(pFormat);
687 }
688
689
690 /**
691 * Test if a gallium format is equivalent to a GL format/type.
692 */
693 GLboolean
694 st_equal_formats(enum pipe_format pFormat, GLenum format, GLenum type)
695 {
696 switch (pFormat) {
697 case PIPE_FORMAT_A8B8G8R8_UNORM:
698 return format == GL_RGBA && type == GL_UNSIGNED_BYTE;
699 case PIPE_FORMAT_A8R8G8B8_UNORM:
700 return format == GL_BGRA && type == GL_UNSIGNED_BYTE;
701 case PIPE_FORMAT_B5G6R5_UNORM:
702 return format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5;
703 /* XXX more combos... */
704 default:
705 return GL_FALSE;
706 }
707 }