mesa: added _mesa_get_format_bits()
[mesa.git] / src / mesa / main / formats.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.7
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008-2009 VMware, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "imports.h"
28 #include "formats.h"
29 #include "config.h"
30 #include "texstore.h"
31
32
33 /**
34 * Info about each format.
35 * These must be in the same order as the MESA_FORMAT_* enums so that
36 * we can do lookups without searching.
37 */
38 static struct gl_format_info format_info[MESA_FORMAT_COUNT] =
39 {
40 {
41 MESA_FORMAT_NONE, /* Name */
42 GL_NONE, /* BaseFormat */
43 GL_NONE, /* DataType */
44 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
45 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
46 0, 0, 0 /* BlockWidth/Height,Bytes */
47 },
48 {
49 MESA_FORMAT_RGBA8888, /* Name */
50 GL_RGBA, /* BaseFormat */
51 GL_UNSIGNED_NORMALIZED, /* DataType */
52 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
53 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
54 1, 1, 4 /* BlockWidth/Height,Bytes */
55 },
56 {
57 MESA_FORMAT_RGBA8888_REV, /* Name */
58 GL_RGBA, /* BaseFormat */
59 GL_UNSIGNED_NORMALIZED, /* DataType */
60 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
61 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
62 1, 1, 4 /* BlockWidth/Height,Bytes */
63 },
64 {
65 MESA_FORMAT_ARGB8888, /* Name */
66 GL_RGBA, /* BaseFormat */
67 GL_UNSIGNED_NORMALIZED, /* DataType */
68 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
69 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
70 1, 1, 4 /* BlockWidth/Height,Bytes */
71 },
72 {
73 MESA_FORMAT_ARGB8888_REV, /* Name */
74 GL_RGBA, /* BaseFormat */
75 GL_UNSIGNED_NORMALIZED, /* DataType */
76 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
77 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
78 1, 1, 4 /* BlockWidth/Height,Bytes */
79 },
80 {
81 MESA_FORMAT_RGB888, /* Name */
82 GL_RGB, /* BaseFormat */
83 GL_UNSIGNED_NORMALIZED, /* DataType */
84 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
85 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
86 1, 1, 3 /* BlockWidth/Height,Bytes */
87 },
88 {
89 MESA_FORMAT_BGR888, /* Name */
90 GL_RGB, /* BaseFormat */
91 GL_UNSIGNED_NORMALIZED, /* DataType */
92 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
93 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
94 1, 1, 3 /* BlockWidth/Height,Bytes */
95 },
96 {
97 MESA_FORMAT_RGB565, /* Name */
98 GL_RGB, /* BaseFormat */
99 GL_UNSIGNED_NORMALIZED, /* DataType */
100 5, 6, 5, 0, /* Red/Green/Blue/AlphaBits */
101 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
102 1, 1, 2 /* BlockWidth/Height,Bytes */
103 },
104 {
105 MESA_FORMAT_RGB565_REV, /* Name */
106 GL_RGB, /* BaseFormat */
107 GL_UNSIGNED_NORMALIZED, /* DataType */
108 5, 6, 5, 0, /* Red/Green/Blue/AlphaBits */
109 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
110 1, 1, 2 /* BlockWidth/Height,Bytes */
111 },
112 {
113 MESA_FORMAT_RGBA4444, /* Name */
114 GL_RGBA, /* BaseFormat */
115 GL_UNSIGNED_NORMALIZED, /* DataType */
116 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
117 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
118 1, 1, 2 /* BlockWidth/Height,Bytes */
119 },
120 {
121 MESA_FORMAT_ARGB4444, /* Name */
122 GL_RGBA, /* BaseFormat */
123 GL_UNSIGNED_NORMALIZED, /* DataType */
124 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
125 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
126 1, 1, 2 /* BlockWidth/Height,Bytes */
127 },
128 {
129 MESA_FORMAT_ARGB4444_REV, /* Name */
130 GL_RGBA, /* BaseFormat */
131 GL_UNSIGNED_NORMALIZED, /* DataType */
132 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
133 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
134 1, 1, 2 /* BlockWidth/Height,Bytes */
135 },
136 {
137 MESA_FORMAT_RGBA5551, /* Name */
138 GL_RGBA, /* BaseFormat */
139 GL_UNSIGNED_NORMALIZED, /* DataType */
140 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
141 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
142 1, 1, 2 /* BlockWidth/Height,Bytes */
143 },
144 {
145 MESA_FORMAT_ARGB1555, /* Name */
146 GL_RGBA, /* BaseFormat */
147 GL_UNSIGNED_NORMALIZED, /* DataType */
148 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
149 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
150 1, 1, 2 /* BlockWidth/Height,Bytes */
151 },
152 {
153 MESA_FORMAT_ARGB1555_REV, /* Name */
154 GL_RGBA, /* BaseFormat */
155 GL_UNSIGNED_NORMALIZED, /* DataType */
156 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
157 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
158 1, 1, 2 /* BlockWidth/Height,Bytes */
159 },
160 {
161 MESA_FORMAT_AL88, /* Name */
162 GL_LUMINANCE_ALPHA, /* BaseFormat */
163 GL_UNSIGNED_NORMALIZED, /* DataType */
164 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
165 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
166 1, 1, 2 /* BlockWidth/Height,Bytes */
167 },
168 {
169 MESA_FORMAT_AL88_REV, /* Name */
170 GL_LUMINANCE_ALPHA, /* BaseFormat */
171 GL_UNSIGNED_NORMALIZED, /* DataType */
172 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
173 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
174 1, 1, 2 /* BlockWidth/Height,Bytes */
175 },
176 {
177 MESA_FORMAT_RGB332, /* Name */
178 GL_RGB, /* BaseFormat */
179 GL_UNSIGNED_NORMALIZED, /* DataType */
180 3, 3, 2, 0, /* Red/Green/Blue/AlphaBits */
181 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
182 1, 1, 1 /* BlockWidth/Height,Bytes */
183 },
184 {
185 MESA_FORMAT_A8, /* Name */
186 GL_ALPHA, /* BaseFormat */
187 GL_UNSIGNED_NORMALIZED, /* DataType */
188 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
189 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
190 1, 1, 1 /* BlockWidth/Height,Bytes */
191 },
192 {
193 MESA_FORMAT_L8, /* Name */
194 GL_LUMINANCE, /* BaseFormat */
195 GL_UNSIGNED_NORMALIZED, /* DataType */
196 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
197 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
198 1, 1, 1 /* BlockWidth/Height,Bytes */
199 },
200 {
201 MESA_FORMAT_I8, /* Name */
202 GL_INTENSITY, /* BaseFormat */
203 GL_UNSIGNED_NORMALIZED, /* DataType */
204 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
205 0, 8, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
206 1, 1, 1 /* BlockWidth/Height,Bytes */
207 },
208 {
209 MESA_FORMAT_CI8, /* Name */
210 GL_COLOR_INDEX, /* BaseFormat */
211 GL_UNSIGNED_INT, /* DataType */
212 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
213 0, 0, 8, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
214 1, 1, 1 /* BlockWidth/Height,Bytes */
215 },
216 {
217 MESA_FORMAT_YCBCR, /* Name */
218 GL_YCBCR_MESA, /* BaseFormat */
219 GL_UNSIGNED_NORMALIZED, /* DataType */
220 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
221 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
222 1, 1, 2 /* BlockWidth/Height,Bytes */
223 },
224 {
225 MESA_FORMAT_YCBCR_REV, /* Name */
226 GL_YCBCR_MESA, /* BaseFormat */
227 GL_UNSIGNED_NORMALIZED, /* DataType */
228 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
229 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
230 1, 1, 2 /* BlockWidth/Height,Bytes */
231 },
232 {
233 MESA_FORMAT_Z24_S8, /* Name */
234 GL_DEPTH_STENCIL, /* BaseFormat */
235 GL_UNSIGNED_INT, /* DataType */
236 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
237 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
238 1, 1, 4 /* BlockWidth/Height,Bytes */
239 },
240 {
241 MESA_FORMAT_S8_Z24, /* Name */
242 GL_DEPTH_STENCIL, /* BaseFormat */
243 GL_UNSIGNED_INT, /* DataType */
244 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
245 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
246 1, 1, 4 /* BlockWidth/Height,Bytes */
247 },
248 {
249 MESA_FORMAT_Z16, /* Name */
250 GL_DEPTH_COMPONENT, /* BaseFormat */
251 GL_UNSIGNED_INT, /* DataType */
252 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
253 0, 0, 0, 16, 0, /* Lum/Int/Index/Depth/StencilBits */
254 1, 1, 2 /* BlockWidth/Height,Bytes */
255 },
256 {
257 MESA_FORMAT_Z32, /* Name */
258 GL_DEPTH_COMPONENT, /* BaseFormat */
259 GL_UNSIGNED_INT, /* DataType */
260 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
261 0, 0, 0, 32, 0, /* Lum/Int/Index/Depth/StencilBits */
262 1, 1, 4 /* BlockWidth/Height,Bytes */
263 },
264 {
265 MESA_FORMAT_S8, /* Name */
266 GL_STENCIL_INDEX, /* BaseFormat */
267 GL_UNSIGNED_INT, /* DataType */
268 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
269 0, 0, 0, 0, 8, /* Lum/Int/Index/Depth/StencilBits */
270 1, 1, 1 /* BlockWidth/Height,Bytes */
271 },
272
273 #if FEATURE_EXT_texture_sRGB
274 {
275 MESA_FORMAT_SRGB8,
276 GL_RGB,
277 GL_UNSIGNED_NORMALIZED,
278 8, 8, 8, 0,
279 0, 0, 0, 0, 0,
280 1, 1, 3
281 },
282 {
283 MESA_FORMAT_SRGBA8,
284 GL_RGBA,
285 GL_UNSIGNED_NORMALIZED,
286 8, 8, 8, 8,
287 0, 0, 0, 0, 0,
288 1, 1, 4
289 },
290 {
291 MESA_FORMAT_SARGB8,
292 GL_RGBA,
293 GL_UNSIGNED_NORMALIZED,
294 8, 8, 8, 8,
295 0, 0, 0, 0, 0,
296 1, 1, 4
297 },
298 {
299 MESA_FORMAT_SL8,
300 GL_LUMINANCE_ALPHA,
301 GL_UNSIGNED_NORMALIZED,
302 0, 0, 0, 8,
303 8, 0, 0, 0, 0,
304 1, 1, 2
305 },
306 {
307 MESA_FORMAT_SLA8,
308 GL_LUMINANCE_ALPHA,
309 GL_UNSIGNED_NORMALIZED,
310 0, 0, 0, 8,
311 8, 0, 0, 0, 0,
312 1, 1, 2
313 },
314 #if FEATURE_texture_s3tc
315 {
316 MESA_FORMAT_SRGB_DXT1, /* Name */
317 GL_RGB, /* BaseFormat */
318 GL_UNSIGNED_NORMALIZED, /* DataType */
319 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
320 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
321 4, 4, 8 /* 8 bytes per 4x4 block */
322 },
323 {
324 MESA_FORMAT_SRGBA_DXT1,
325 GL_RGBA,
326 GL_UNSIGNED_NORMALIZED,
327 4, 4, 4, 4,
328 0, 0, 0, 0, 0,
329 4, 4, 8 /* 8 bytes per 4x4 block */
330 },
331 {
332 MESA_FORMAT_SRGBA_DXT3,
333 GL_RGBA,
334 GL_UNSIGNED_NORMALIZED,
335 4, 4, 4, 4,
336 0, 0, 0, 0, 0,
337 4, 4, 16 /* 16 bytes per 4x4 block */
338 },
339 {
340 MESA_FORMAT_SRGBA_DXT5,
341 GL_RGBA,
342 GL_UNSIGNED_NORMALIZED,
343 4, 4, 4, 4,
344 0, 0, 0, 0, 0,
345 4, 4, 16 /* 16 bytes per 4x4 block */
346 },
347 #endif
348 #endif
349
350 #if FEATURE_texture_fxt1
351 {
352 MESA_FORMAT_RGB_FXT1,
353 GL_RGB,
354 GL_UNSIGNED_NORMALIZED,
355 8, 8, 8, 0,
356 0, 0, 0, 0, 0,
357 8, 4, 16 /* 16 bytes per 8x4 block */
358 },
359 {
360 MESA_FORMAT_RGBA_FXT1,
361 GL_RGBA,
362 GL_UNSIGNED_NORMALIZED,
363 8, 8, 8, 8,
364 0, 0, 0, 0, 0,
365 8, 4, 16 /* 16 bytes per 8x4 block */
366 },
367 #endif
368
369 #if FEATURE_texture_s3tc
370 {
371 MESA_FORMAT_RGB_DXT1, /* Name */
372 GL_RGB, /* BaseFormat */
373 GL_UNSIGNED_NORMALIZED, /* DataType */
374 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
375 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
376 4, 4, 8 /* 8 bytes per 4x4 block */
377 },
378 {
379 MESA_FORMAT_RGBA_DXT1,
380 GL_RGBA,
381 GL_UNSIGNED_NORMALIZED,
382 4, 4, 4, 4,
383 0, 0, 0, 0, 0,
384 4, 4, 8 /* 8 bytes per 4x4 block */
385 },
386 {
387 MESA_FORMAT_RGBA_DXT3,
388 GL_RGBA,
389 GL_UNSIGNED_NORMALIZED,
390 4, 4, 4, 4,
391 0, 0, 0, 0, 0,
392 4, 4, 16 /* 16 bytes per 4x4 block */
393 },
394 {
395 MESA_FORMAT_RGBA_DXT5,
396 GL_RGBA,
397 GL_UNSIGNED_NORMALIZED,
398 4, 4, 4, 4,
399 0, 0, 0, 0, 0,
400 4, 4, 16 /* 16 bytes per 4x4 block */
401 },
402 #endif
403
404 {
405 MESA_FORMAT_RGBA,
406 GL_RGBA,
407 GL_UNSIGNED_NORMALIZED,
408 CHAN_BITS, CHAN_BITS, CHAN_BITS, CHAN_BITS,
409 0, 0, 0, 0, 0,
410 1, 1, 4 * CHAN_BITS / 8
411 },
412 {
413 MESA_FORMAT_RGB,
414 GL_RGB,
415 GL_UNSIGNED_NORMALIZED,
416 CHAN_BITS, CHAN_BITS, CHAN_BITS, 0,
417 0, 0, 0, 0, 0,
418 1, 1, 3 * CHAN_BITS / 8
419 },
420 {
421 MESA_FORMAT_ALPHA,
422 GL_ALPHA,
423 GL_UNSIGNED_NORMALIZED,
424 0, 0, 0, CHAN_BITS,
425 0, 0, 0, 0, 0,
426 1, 1, 1 * CHAN_BITS / 8
427 },
428 {
429 MESA_FORMAT_LUMINANCE,
430 GL_LUMINANCE,
431 GL_UNSIGNED_NORMALIZED,
432 0, 0, 0, 0,
433 CHAN_BITS, 0, 0, 0, 0,
434 1, 1, 1 * CHAN_BITS / 8
435 },
436 {
437 MESA_FORMAT_LUMINANCE_ALPHA,
438 GL_LUMINANCE_ALPHA,
439 GL_UNSIGNED_NORMALIZED,
440 0, 0, 0, CHAN_BITS,
441 CHAN_BITS, 0, 0, 0, 0,
442 1, 1, 2 * CHAN_BITS / 8
443 },
444 {
445 MESA_FORMAT_INTENSITY,
446 GL_INTENSITY,
447 GL_UNSIGNED_NORMALIZED,
448 0, 0, 0, 0,
449 0, CHAN_BITS, 0, 0, 0,
450 1, 1, 1 * CHAN_BITS / 8
451 },
452 {
453 MESA_FORMAT_RGBA_FLOAT32,
454 GL_RGBA,
455 GL_FLOAT,
456 32, 32, 32, 32,
457 0, 0, 0, 0, 0,
458 1, 1, 16
459 },
460 {
461 MESA_FORMAT_RGBA_FLOAT16,
462 GL_RGBA,
463 GL_FLOAT,
464 16, 16, 16, 16,
465 0, 0, 0, 0, 0,
466 1, 1, 8
467 },
468 {
469 MESA_FORMAT_RGB_FLOAT32,
470 GL_RGB,
471 GL_FLOAT,
472 32, 32, 32, 0,
473 0, 0, 0, 0, 0,
474 1, 1, 12
475 },
476 {
477 MESA_FORMAT_RGB_FLOAT16,
478 GL_RGB,
479 GL_FLOAT,
480 16, 16, 16, 0,
481 0, 0, 0, 0, 0,
482 1, 1, 6
483 },
484 {
485 MESA_FORMAT_ALPHA_FLOAT32,
486 GL_ALPHA,
487 GL_FLOAT,
488 0, 0, 0, 32,
489 0, 0, 0, 0, 0,
490 1, 1, 4
491 },
492 {
493 MESA_FORMAT_ALPHA_FLOAT16,
494 GL_ALPHA,
495 GL_FLOAT,
496 0, 0, 0, 16,
497 0, 0, 0, 0, 0,
498 1, 1, 2
499 },
500 {
501 MESA_FORMAT_LUMINANCE_FLOAT32,
502 GL_ALPHA,
503 GL_FLOAT,
504 0, 0, 0, 0,
505 32, 0, 0, 0, 0,
506 1, 1, 4
507 },
508 {
509 MESA_FORMAT_LUMINANCE_FLOAT16,
510 GL_ALPHA,
511 GL_FLOAT,
512 0, 0, 0, 0,
513 16, 0, 0, 0, 0,
514 1, 1, 2
515 },
516 {
517 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
518 GL_LUMINANCE_ALPHA,
519 GL_FLOAT,
520 0, 0, 0, 32,
521 32, 0, 0, 0, 0,
522 1, 1, 8
523 },
524 {
525 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
526 GL_LUMINANCE_ALPHA,
527 GL_FLOAT,
528 0, 0, 0, 16,
529 16, 0, 0, 0, 0,
530 1, 1, 4
531 },
532 {
533 MESA_FORMAT_INTENSITY_FLOAT32,
534 GL_INTENSITY,
535 GL_FLOAT,
536 0, 0, 0, 0,
537 0, 32, 0, 0, 0,
538 1, 1, 4
539 },
540 {
541 MESA_FORMAT_INTENSITY_FLOAT16,
542 GL_INTENSITY,
543 GL_FLOAT,
544 0, 0, 0, 0,
545 0, 16, 0, 0, 0,
546 1, 1, 2
547 },
548
549 {
550 MESA_FORMAT_DUDV8,
551 GL_DUDV_ATI,
552 GL_SIGNED_NORMALIZED,
553 0, 0, 0, 0,
554 0, 0, 0, 0, 0,
555 1, 1, 2
556 },
557
558 {
559 MESA_FORMAT_SIGNED_RGBA8888,
560 GL_RGBA,
561 GL_SIGNED_NORMALIZED,
562 8, 8, 8, 8,
563 0, 0, 0, 0, 0,
564 1, 1, 4
565 },
566 {
567 MESA_FORMAT_SIGNED_RGBA8888_REV,
568 GL_RGBA,
569 GL_SIGNED_NORMALIZED,
570 8, 8, 8, 8,
571 0, 0, 0, 0, 0,
572 1, 1, 4
573 },
574
575 };
576
577
578
579 static const struct gl_format_info *
580 _mesa_get_format_info(gl_format format)
581 {
582 const struct gl_format_info *info = &format_info[format];
583 assert(info->Name == format);
584 return info;
585 }
586
587
588 GLuint
589 _mesa_get_format_bytes(gl_format format)
590 {
591 const struct gl_format_info *info = _mesa_get_format_info(format);
592 ASSERT(info->BytesPerBlock);
593 return info->BytesPerBlock;
594 }
595
596
597 GLenum
598 _mesa_get_format_base_format(gl_format format)
599 {
600 const struct gl_format_info *info = _mesa_get_format_info(format);
601 return info->BaseFormat;
602 }
603
604
605 GLboolean
606 _mesa_is_format_compressed(gl_format format)
607 {
608 const struct gl_format_info *info = _mesa_get_format_info(format);
609 return info->BlockWidth > 1 || info->BlockHeight > 1;
610 }
611
612
613 /**
614 * Do sanity checking of the format info table.
615 */
616 void
617 _mesa_test_formats(void)
618 {
619 GLuint i;
620
621 assert(Elements(format_info) == MESA_FORMAT_COUNT);
622
623 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
624 const struct gl_format_info *info = _mesa_get_format_info(i);
625 assert(info);
626
627 assert(info->Name == i);
628
629 if (info->Name == MESA_FORMAT_NONE)
630 continue;
631
632 if (info->BlockWidth == 1 && info->BlockHeight == 1) {
633 if (info->RedBits > 0) {
634 GLuint t = info->RedBits + info->GreenBits
635 + info->BlueBits + info->AlphaBits;
636 assert(t / 8 == info->BytesPerBlock);
637 }
638 }
639
640 assert(info->DataType == GL_UNSIGNED_NORMALIZED ||
641 info->DataType == GL_SIGNED_NORMALIZED ||
642 info->DataType == GL_UNSIGNED_INT ||
643 info->DataType == GL_FLOAT);
644
645 if (info->BaseFormat == GL_RGB) {
646 assert(info->RedBits > 0);
647 assert(info->GreenBits > 0);
648 assert(info->BlueBits > 0);
649 assert(info->AlphaBits == 0);
650 assert(info->LuminanceBits == 0);
651 assert(info->IntensityBits == 0);
652 }
653 else if (info->BaseFormat == GL_RGBA) {
654 assert(info->RedBits > 0);
655 assert(info->GreenBits > 0);
656 assert(info->BlueBits > 0);
657 assert(info->AlphaBits > 0);
658 assert(info->LuminanceBits == 0);
659 assert(info->IntensityBits == 0);
660 }
661 else if (info->BaseFormat == GL_LUMINANCE) {
662 assert(info->RedBits == 0);
663 assert(info->GreenBits == 0);
664 assert(info->BlueBits == 0);
665 assert(info->AlphaBits == 0);
666 assert(info->LuminanceBits > 0);
667 assert(info->IntensityBits == 0);
668 }
669 else if (info->BaseFormat == GL_INTENSITY) {
670 assert(info->RedBits == 0);
671 assert(info->GreenBits == 0);
672 assert(info->BlueBits == 0);
673 assert(info->AlphaBits == 0);
674 assert(info->LuminanceBits == 0);
675 assert(info->IntensityBits > 0);
676 }
677
678 }
679 }
680
681
682 /**
683 * XXX possible replacement for _mesa_format_to_type_and_comps()
684 * Used for mipmap generation.
685 */
686 void
687 _mesa_format_to_type_and_comps2(gl_format format,
688 GLenum *datatype, GLuint *comps)
689 {
690 const struct gl_format_info *info = _mesa_get_format_info(format);
691
692 /* We use a bunch of heuristics here. If this gets too ugly we could
693 * just encode the info the in the gl_format_info structures.
694 */
695 if (info->BaseFormat == GL_RGB ||
696 info->BaseFormat == GL_RGBA ||
697 info->BaseFormat == GL_ALPHA) {
698 *comps = ((info->RedBits > 0) +
699 (info->GreenBits > 0) +
700 (info->BlueBits > 0) +
701 (info->AlphaBits > 0));
702
703 if (info->DataType== GL_FLOAT) {
704 if (info->RedBits == 32)
705 *datatype = GL_FLOAT;
706 else
707 *datatype = GL_HALF_FLOAT;
708 }
709 else if (info->GreenBits == 3) {
710 *datatype = GL_UNSIGNED_BYTE_3_3_2;
711 }
712 else if (info->GreenBits == 4) {
713 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
714 }
715 else if (info->GreenBits == 6) {
716 *datatype = GL_UNSIGNED_SHORT_5_6_5;
717 }
718 else if (info->GreenBits == 5) {
719 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
720 }
721 else if (info->RedBits == 8) {
722 *datatype = GL_UNSIGNED_BYTE;
723 }
724 else {
725 ASSERT(info->RedBits == 16);
726 *datatype = GL_UNSIGNED_SHORT;
727 }
728 }
729 else if (info->BaseFormat == GL_LUMINANCE ||
730 info->BaseFormat == GL_LUMINANCE_ALPHA) {
731 *comps = ((info->LuminanceBits > 0) +
732 (info->AlphaBits > 0));
733 if (info->LuminanceBits == 8) {
734 *datatype = GL_UNSIGNED_BYTE;
735 }
736 else if (info->LuminanceBits == 16) {
737 *datatype = GL_UNSIGNED_SHORT;
738 }
739 else {
740 *datatype = GL_FLOAT;
741 }
742 }
743 else if (info->BaseFormat == GL_INTENSITY) {
744 *comps = 1;
745 if (info->IntensityBits == 8) {
746 *datatype = GL_UNSIGNED_BYTE;
747 }
748 else if (info->IntensityBits == 16) {
749 *datatype = GL_UNSIGNED_SHORT;
750 }
751 else {
752 *datatype = GL_FLOAT;
753 }
754 }
755 else if (info->BaseFormat == GL_COLOR_INDEX) {
756 *comps = 1;
757 *datatype = GL_UNSIGNED_BYTE;
758 }
759 else if (info->BaseFormat == GL_DEPTH_COMPONENT) {
760 *comps = 1;
761 if (info->DepthBits == 16) {
762 *datatype = GL_UNSIGNED_SHORT;
763 }
764 else {
765 ASSERT(info->DepthBits == 32);
766 *datatype = GL_UNSIGNED_INT;
767 }
768 }
769 else if (info->BaseFormat == GL_DEPTH_STENCIL) {
770 *comps = 1;
771 *datatype = GL_UNSIGNED_INT;
772 }
773 else if (info->BaseFormat == GL_YCBCR_MESA) {
774 *comps = 2;
775 *datatype = GL_UNSIGNED_SHORT;
776 }
777 else if (info->BaseFormat == GL_DUDV_ATI) {
778 *comps = 2;
779 *datatype = GL_BYTE;
780 }
781 else {
782 /* any other formats? */
783 ASSERT(0);
784 *comps = 1;
785 *datatype = GL_UNSIGNED_BYTE;
786 }
787 }
788
789
790 GLint
791 _mesa_get_format_bits(gl_format format, GLenum pname)
792 {
793 const struct gl_format_info *info = _mesa_get_format_info(format);
794
795 switch (pname) {
796 case GL_TEXTURE_RED_SIZE:
797 return info->RedBits;
798 case GL_TEXTURE_GREEN_SIZE:
799 return info->GreenBits;
800 case GL_TEXTURE_BLUE_SIZE:
801 return info->BlueBits;
802 case GL_TEXTURE_ALPHA_SIZE:
803 return info->AlphaBits;
804 case GL_TEXTURE_INTENSITY_SIZE:
805 return info->IntensityBits;
806 case GL_TEXTURE_LUMINANCE_SIZE:
807 return info->LuminanceBits;
808 case GL_TEXTURE_INDEX_SIZE_EXT:
809 return info->IndexBits;
810 case GL_TEXTURE_DEPTH_SIZE_ARB:
811 return info->DepthBits;
812 case GL_TEXTURE_STENCIL_SIZE_EXT:
813 return info->StencilBits;
814 default:
815 _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
816 return 0;
817 }
818 }