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