mesa: additional comments in format code
[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 * Information about texture formats.
35 */
36 struct gl_format_info
37 {
38 gl_format Name;
39
40 /**
41 * Base format is one of GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE,
42 * GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA, GL_COLOR_INDEX,
43 * GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
44 */
45 GLenum BaseFormat;
46
47 /**
48 * Logical data type: one of GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALED,
49 * GL_UNSIGNED_INT, GL_SIGNED_INT, GL_FLOAT.
50 */
51 GLenum DataType;
52
53 GLubyte RedBits;
54 GLubyte GreenBits;
55 GLubyte BlueBits;
56 GLubyte AlphaBits;
57 GLubyte LuminanceBits;
58 GLubyte IntensityBits;
59 GLubyte IndexBits;
60 GLubyte DepthBits;
61 GLubyte StencilBits;
62
63 /**
64 * To describe compressed formats. If not compressed, Width=Height=1.
65 */
66 GLubyte BlockWidth, BlockHeight;
67 GLubyte BytesPerBlock;
68 };
69
70
71 /**
72 * Info about each format.
73 * These must be in the same order as the MESA_FORMAT_* enums so that
74 * we can do lookups without searching.
75 */
76 static struct gl_format_info format_info[MESA_FORMAT_COUNT] =
77 {
78 {
79 MESA_FORMAT_NONE, /* Name */
80 GL_NONE, /* BaseFormat */
81 GL_NONE, /* DataType */
82 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
83 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
84 0, 0, 0 /* BlockWidth/Height,Bytes */
85 },
86 {
87 MESA_FORMAT_RGBA8888, /* Name */
88 GL_RGBA, /* BaseFormat */
89 GL_UNSIGNED_NORMALIZED, /* DataType */
90 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
91 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
92 1, 1, 4 /* BlockWidth/Height,Bytes */
93 },
94 {
95 MESA_FORMAT_RGBA8888_REV, /* Name */
96 GL_RGBA, /* BaseFormat */
97 GL_UNSIGNED_NORMALIZED, /* DataType */
98 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
99 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
100 1, 1, 4 /* BlockWidth/Height,Bytes */
101 },
102 {
103 MESA_FORMAT_ARGB8888, /* Name */
104 GL_RGBA, /* BaseFormat */
105 GL_UNSIGNED_NORMALIZED, /* DataType */
106 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
107 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
108 1, 1, 4 /* BlockWidth/Height,Bytes */
109 },
110 {
111 MESA_FORMAT_ARGB8888_REV, /* Name */
112 GL_RGBA, /* BaseFormat */
113 GL_UNSIGNED_NORMALIZED, /* DataType */
114 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
115 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
116 1, 1, 4 /* BlockWidth/Height,Bytes */
117 },
118 {
119 MESA_FORMAT_XRGB8888, /* Name */
120 GL_RGB, /* BaseFormat */
121 GL_UNSIGNED_NORMALIZED, /* DataType */
122 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
123 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
124 1, 1, 4 /* BlockWidth/Height,Bytes */
125 },
126 {
127 MESA_FORMAT_RGB888, /* Name */
128 GL_RGB, /* BaseFormat */
129 GL_UNSIGNED_NORMALIZED, /* DataType */
130 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
131 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
132 1, 1, 3 /* BlockWidth/Height,Bytes */
133 },
134 {
135 MESA_FORMAT_BGR888, /* Name */
136 GL_RGB, /* BaseFormat */
137 GL_UNSIGNED_NORMALIZED, /* DataType */
138 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
139 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
140 1, 1, 3 /* BlockWidth/Height,Bytes */
141 },
142 {
143 MESA_FORMAT_RGB565, /* Name */
144 GL_RGB, /* BaseFormat */
145 GL_UNSIGNED_NORMALIZED, /* DataType */
146 5, 6, 5, 0, /* Red/Green/Blue/AlphaBits */
147 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
148 1, 1, 2 /* BlockWidth/Height,Bytes */
149 },
150 {
151 MESA_FORMAT_RGB565_REV, /* Name */
152 GL_RGB, /* BaseFormat */
153 GL_UNSIGNED_NORMALIZED, /* DataType */
154 5, 6, 5, 0, /* Red/Green/Blue/AlphaBits */
155 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
156 1, 1, 2 /* BlockWidth/Height,Bytes */
157 },
158 {
159 MESA_FORMAT_ARGB4444, /* Name */
160 GL_RGBA, /* BaseFormat */
161 GL_UNSIGNED_NORMALIZED, /* DataType */
162 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
163 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
164 1, 1, 2 /* BlockWidth/Height,Bytes */
165 },
166 {
167 MESA_FORMAT_ARGB4444_REV, /* Name */
168 GL_RGBA, /* BaseFormat */
169 GL_UNSIGNED_NORMALIZED, /* DataType */
170 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
171 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
172 1, 1, 2 /* BlockWidth/Height,Bytes */
173 },
174 {
175 MESA_FORMAT_RGBA5551, /* Name */
176 GL_RGBA, /* BaseFormat */
177 GL_UNSIGNED_NORMALIZED, /* DataType */
178 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
179 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
180 1, 1, 2 /* BlockWidth/Height,Bytes */
181 },
182 {
183 MESA_FORMAT_ARGB1555, /* Name */
184 GL_RGBA, /* BaseFormat */
185 GL_UNSIGNED_NORMALIZED, /* DataType */
186 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
187 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
188 1, 1, 2 /* BlockWidth/Height,Bytes */
189 },
190 {
191 MESA_FORMAT_ARGB1555_REV, /* Name */
192 GL_RGBA, /* BaseFormat */
193 GL_UNSIGNED_NORMALIZED, /* DataType */
194 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
195 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
196 1, 1, 2 /* BlockWidth/Height,Bytes */
197 },
198 {
199 MESA_FORMAT_AL88, /* Name */
200 GL_LUMINANCE_ALPHA, /* BaseFormat */
201 GL_UNSIGNED_NORMALIZED, /* DataType */
202 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
203 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
204 1, 1, 2 /* BlockWidth/Height,Bytes */
205 },
206 {
207 MESA_FORMAT_AL88_REV, /* Name */
208 GL_LUMINANCE_ALPHA, /* BaseFormat */
209 GL_UNSIGNED_NORMALIZED, /* DataType */
210 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
211 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
212 1, 1, 2 /* BlockWidth/Height,Bytes */
213 },
214 {
215 MESA_FORMAT_RGB332, /* Name */
216 GL_RGB, /* BaseFormat */
217 GL_UNSIGNED_NORMALIZED, /* DataType */
218 3, 3, 2, 0, /* Red/Green/Blue/AlphaBits */
219 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
220 1, 1, 1 /* BlockWidth/Height,Bytes */
221 },
222 {
223 MESA_FORMAT_A8, /* Name */
224 GL_ALPHA, /* BaseFormat */
225 GL_UNSIGNED_NORMALIZED, /* DataType */
226 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
227 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
228 1, 1, 1 /* BlockWidth/Height,Bytes */
229 },
230 {
231 MESA_FORMAT_L8, /* Name */
232 GL_LUMINANCE, /* BaseFormat */
233 GL_UNSIGNED_NORMALIZED, /* DataType */
234 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
235 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
236 1, 1, 1 /* BlockWidth/Height,Bytes */
237 },
238 {
239 MESA_FORMAT_I8, /* Name */
240 GL_INTENSITY, /* BaseFormat */
241 GL_UNSIGNED_NORMALIZED, /* DataType */
242 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
243 0, 8, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
244 1, 1, 1 /* BlockWidth/Height,Bytes */
245 },
246 {
247 MESA_FORMAT_CI8, /* Name */
248 GL_COLOR_INDEX, /* BaseFormat */
249 GL_UNSIGNED_INT, /* DataType */
250 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
251 0, 0, 8, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
252 1, 1, 1 /* BlockWidth/Height,Bytes */
253 },
254 {
255 MESA_FORMAT_YCBCR, /* Name */
256 GL_YCBCR_MESA, /* BaseFormat */
257 GL_UNSIGNED_NORMALIZED, /* DataType */
258 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
259 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
260 1, 1, 2 /* BlockWidth/Height,Bytes */
261 },
262 {
263 MESA_FORMAT_YCBCR_REV, /* Name */
264 GL_YCBCR_MESA, /* BaseFormat */
265 GL_UNSIGNED_NORMALIZED, /* DataType */
266 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
267 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
268 1, 1, 2 /* BlockWidth/Height,Bytes */
269 },
270 {
271 MESA_FORMAT_Z24_S8, /* Name */
272 GL_DEPTH_STENCIL, /* BaseFormat */
273 GL_UNSIGNED_INT, /* DataType */
274 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
275 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
276 1, 1, 4 /* BlockWidth/Height,Bytes */
277 },
278 {
279 MESA_FORMAT_S8_Z24, /* Name */
280 GL_DEPTH_STENCIL, /* BaseFormat */
281 GL_UNSIGNED_INT, /* DataType */
282 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
283 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
284 1, 1, 4 /* BlockWidth/Height,Bytes */
285 },
286 {
287 MESA_FORMAT_Z16, /* Name */
288 GL_DEPTH_COMPONENT, /* BaseFormat */
289 GL_UNSIGNED_INT, /* DataType */
290 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
291 0, 0, 0, 16, 0, /* Lum/Int/Index/Depth/StencilBits */
292 1, 1, 2 /* BlockWidth/Height,Bytes */
293 },
294 {
295 MESA_FORMAT_X8_Z24, /* Name */
296 GL_DEPTH_COMPONENT, /* BaseFormat */
297 GL_UNSIGNED_INT, /* DataType */
298 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
299 0, 0, 0, 24, 0, /* Lum/Int/Index/Depth/StencilBits */
300 1, 1, 4 /* BlockWidth/Height,Bytes */
301 },
302 {
303 MESA_FORMAT_Z32, /* Name */
304 GL_DEPTH_COMPONENT, /* BaseFormat */
305 GL_UNSIGNED_INT, /* DataType */
306 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
307 0, 0, 0, 32, 0, /* Lum/Int/Index/Depth/StencilBits */
308 1, 1, 4 /* BlockWidth/Height,Bytes */
309 },
310 {
311 MESA_FORMAT_S8, /* Name */
312 GL_STENCIL_INDEX, /* BaseFormat */
313 GL_UNSIGNED_INT, /* DataType */
314 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
315 0, 0, 0, 0, 8, /* Lum/Int/Index/Depth/StencilBits */
316 1, 1, 1 /* BlockWidth/Height,Bytes */
317 },
318 {
319 MESA_FORMAT_SRGB8,
320 GL_RGB,
321 GL_UNSIGNED_NORMALIZED,
322 8, 8, 8, 0,
323 0, 0, 0, 0, 0,
324 1, 1, 3
325 },
326 {
327 MESA_FORMAT_SRGBA8,
328 GL_RGBA,
329 GL_UNSIGNED_NORMALIZED,
330 8, 8, 8, 8,
331 0, 0, 0, 0, 0,
332 1, 1, 4
333 },
334 {
335 MESA_FORMAT_SARGB8,
336 GL_RGBA,
337 GL_UNSIGNED_NORMALIZED,
338 8, 8, 8, 8,
339 0, 0, 0, 0, 0,
340 1, 1, 4
341 },
342 {
343 MESA_FORMAT_SL8,
344 GL_LUMINANCE_ALPHA,
345 GL_UNSIGNED_NORMALIZED,
346 0, 0, 0, 8,
347 8, 0, 0, 0, 0,
348 1, 1, 2
349 },
350 {
351 MESA_FORMAT_SLA8,
352 GL_LUMINANCE_ALPHA,
353 GL_UNSIGNED_NORMALIZED,
354 0, 0, 0, 8,
355 8, 0, 0, 0, 0,
356 1, 1, 2
357 },
358 {
359 MESA_FORMAT_SRGB_DXT1, /* Name */
360 GL_RGB, /* BaseFormat */
361 GL_UNSIGNED_NORMALIZED, /* DataType */
362 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
363 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
364 4, 4, 8 /* 8 bytes per 4x4 block */
365 },
366 {
367 MESA_FORMAT_SRGBA_DXT1,
368 GL_RGBA,
369 GL_UNSIGNED_NORMALIZED,
370 4, 4, 4, 4,
371 0, 0, 0, 0, 0,
372 4, 4, 8 /* 8 bytes per 4x4 block */
373 },
374 {
375 MESA_FORMAT_SRGBA_DXT3,
376 GL_RGBA,
377 GL_UNSIGNED_NORMALIZED,
378 4, 4, 4, 4,
379 0, 0, 0, 0, 0,
380 4, 4, 16 /* 16 bytes per 4x4 block */
381 },
382 {
383 MESA_FORMAT_SRGBA_DXT5,
384 GL_RGBA,
385 GL_UNSIGNED_NORMALIZED,
386 4, 4, 4, 4,
387 0, 0, 0, 0, 0,
388 4, 4, 16 /* 16 bytes per 4x4 block */
389 },
390
391 {
392 MESA_FORMAT_RGB_FXT1,
393 GL_RGB,
394 GL_UNSIGNED_NORMALIZED,
395 8, 8, 8, 0,
396 0, 0, 0, 0, 0,
397 8, 4, 16 /* 16 bytes per 8x4 block */
398 },
399 {
400 MESA_FORMAT_RGBA_FXT1,
401 GL_RGBA,
402 GL_UNSIGNED_NORMALIZED,
403 8, 8, 8, 8,
404 0, 0, 0, 0, 0,
405 8, 4, 16 /* 16 bytes per 8x4 block */
406 },
407
408 {
409 MESA_FORMAT_RGB_DXT1, /* Name */
410 GL_RGB, /* BaseFormat */
411 GL_UNSIGNED_NORMALIZED, /* DataType */
412 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
413 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
414 4, 4, 8 /* 8 bytes per 4x4 block */
415 },
416 {
417 MESA_FORMAT_RGBA_DXT1,
418 GL_RGBA,
419 GL_UNSIGNED_NORMALIZED,
420 4, 4, 4, 4,
421 0, 0, 0, 0, 0,
422 4, 4, 8 /* 8 bytes per 4x4 block */
423 },
424 {
425 MESA_FORMAT_RGBA_DXT3,
426 GL_RGBA,
427 GL_UNSIGNED_NORMALIZED,
428 4, 4, 4, 4,
429 0, 0, 0, 0, 0,
430 4, 4, 16 /* 16 bytes per 4x4 block */
431 },
432 {
433 MESA_FORMAT_RGBA_DXT5,
434 GL_RGBA,
435 GL_UNSIGNED_NORMALIZED,
436 4, 4, 4, 4,
437 0, 0, 0, 0, 0,
438 4, 4, 16 /* 16 bytes per 4x4 block */
439 },
440 {
441 MESA_FORMAT_RGBA_FLOAT32,
442 GL_RGBA,
443 GL_FLOAT,
444 32, 32, 32, 32,
445 0, 0, 0, 0, 0,
446 1, 1, 16
447 },
448 {
449 MESA_FORMAT_RGBA_FLOAT16,
450 GL_RGBA,
451 GL_FLOAT,
452 16, 16, 16, 16,
453 0, 0, 0, 0, 0,
454 1, 1, 8
455 },
456 {
457 MESA_FORMAT_RGB_FLOAT32,
458 GL_RGB,
459 GL_FLOAT,
460 32, 32, 32, 0,
461 0, 0, 0, 0, 0,
462 1, 1, 12
463 },
464 {
465 MESA_FORMAT_RGB_FLOAT16,
466 GL_RGB,
467 GL_FLOAT,
468 16, 16, 16, 0,
469 0, 0, 0, 0, 0,
470 1, 1, 6
471 },
472 {
473 MESA_FORMAT_ALPHA_FLOAT32,
474 GL_ALPHA,
475 GL_FLOAT,
476 0, 0, 0, 32,
477 0, 0, 0, 0, 0,
478 1, 1, 4
479 },
480 {
481 MESA_FORMAT_ALPHA_FLOAT16,
482 GL_ALPHA,
483 GL_FLOAT,
484 0, 0, 0, 16,
485 0, 0, 0, 0, 0,
486 1, 1, 2
487 },
488 {
489 MESA_FORMAT_LUMINANCE_FLOAT32,
490 GL_ALPHA,
491 GL_FLOAT,
492 0, 0, 0, 0,
493 32, 0, 0, 0, 0,
494 1, 1, 4
495 },
496 {
497 MESA_FORMAT_LUMINANCE_FLOAT16,
498 GL_ALPHA,
499 GL_FLOAT,
500 0, 0, 0, 0,
501 16, 0, 0, 0, 0,
502 1, 1, 2
503 },
504 {
505 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
506 GL_LUMINANCE_ALPHA,
507 GL_FLOAT,
508 0, 0, 0, 32,
509 32, 0, 0, 0, 0,
510 1, 1, 8
511 },
512 {
513 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
514 GL_LUMINANCE_ALPHA,
515 GL_FLOAT,
516 0, 0, 0, 16,
517 16, 0, 0, 0, 0,
518 1, 1, 4
519 },
520 {
521 MESA_FORMAT_INTENSITY_FLOAT32,
522 GL_INTENSITY,
523 GL_FLOAT,
524 0, 0, 0, 0,
525 0, 32, 0, 0, 0,
526 1, 1, 4
527 },
528 {
529 MESA_FORMAT_INTENSITY_FLOAT16,
530 GL_INTENSITY,
531 GL_FLOAT,
532 0, 0, 0, 0,
533 0, 16, 0, 0, 0,
534 1, 1, 2
535 },
536 {
537 MESA_FORMAT_DUDV8,
538 GL_DUDV_ATI,
539 GL_SIGNED_NORMALIZED,
540 0, 0, 0, 0,
541 0, 0, 0, 0, 0,
542 1, 1, 2
543 },
544 {
545 MESA_FORMAT_SIGNED_RGBA8888,
546 GL_RGBA,
547 GL_SIGNED_NORMALIZED,
548 8, 8, 8, 8,
549 0, 0, 0, 0, 0,
550 1, 1, 4
551 },
552 {
553 MESA_FORMAT_SIGNED_RGBA8888_REV,
554 GL_RGBA,
555 GL_SIGNED_NORMALIZED,
556 8, 8, 8, 8,
557 0, 0, 0, 0, 0,
558 1, 1, 4
559 },
560 {
561 MESA_FORMAT_SIGNED_RGBA_16,
562 GL_RGBA,
563 GL_SIGNED_NORMALIZED,
564 16, 16, 16, 16,
565 0, 0, 0, 0, 0,
566 1, 1, 8
567 }
568 };
569
570
571
572 static const struct gl_format_info *
573 _mesa_get_format_info(gl_format format)
574 {
575 const struct gl_format_info *info = &format_info[format];
576 assert(info->Name == format);
577 return info;
578 }
579
580
581 /**
582 * Return bytes needed to store a block of pixels in the given format.
583 * Normally, a block is 1x1 (a single pixel). But for compressed formats
584 * a block may be 4x4 or 8x4, etc.
585 */
586 GLuint
587 _mesa_get_format_bytes(gl_format format)
588 {
589 const struct gl_format_info *info = _mesa_get_format_info(format);
590 ASSERT(info->BytesPerBlock);
591 return info->BytesPerBlock;
592 }
593
594
595 /**
596 * Return bits per component for the given format.
597 * \param format one of MESA_FORMAT_x
598 * \param pname the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
599 */
600 GLint
601 _mesa_get_format_bits(gl_format format, GLenum pname)
602 {
603 const struct gl_format_info *info = _mesa_get_format_info(format);
604
605 switch (pname) {
606 case GL_RED_BITS:
607 case GL_TEXTURE_RED_SIZE:
608 case GL_RENDERBUFFER_RED_SIZE_EXT:
609 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
610 return info->RedBits;
611 case GL_GREEN_BITS:
612 case GL_TEXTURE_GREEN_SIZE:
613 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
614 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
615 return info->GreenBits;
616 case GL_BLUE_BITS:
617 case GL_TEXTURE_BLUE_SIZE:
618 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
619 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
620 return info->BlueBits;
621 case GL_ALPHA_BITS:
622 case GL_TEXTURE_ALPHA_SIZE:
623 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
624 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
625 return info->AlphaBits;
626 case GL_TEXTURE_INTENSITY_SIZE:
627 return info->IntensityBits;
628 case GL_TEXTURE_LUMINANCE_SIZE:
629 return info->LuminanceBits;
630 case GL_INDEX_BITS:
631 case GL_TEXTURE_INDEX_SIZE_EXT:
632 return info->IndexBits;
633 case GL_DEPTH_BITS:
634 case GL_TEXTURE_DEPTH_SIZE_ARB:
635 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
636 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
637 return info->DepthBits;
638 case GL_STENCIL_BITS:
639 case GL_TEXTURE_STENCIL_SIZE_EXT:
640 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
641 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
642 return info->StencilBits;
643 default:
644 _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
645 return 0;
646 }
647 }
648
649
650 /**
651 * Return the data type (or more specifically, the data representation)
652 * for the given format.
653 * The return value will be one of:
654 * GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
655 * GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
656 * GL_UNSIGNED_INT = an ordinary unsigned integer
657 * GL_FLOAT = an ordinary float
658 */
659 GLenum
660 _mesa_get_format_datatype(gl_format format)
661 {
662 const struct gl_format_info *info = _mesa_get_format_info(format);
663 return info->DataType;
664 }
665
666
667 /**
668 * Return the basic format for the given type. The result will be
669 * one of GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA,
670 * GL_INTENSITY, GL_YCBCR_MESA, GL_COLOR_INDEX, GL_DEPTH_COMPONENT,
671 * GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
672 */
673 GLenum
674 _mesa_get_format_base_format(gl_format format)
675 {
676 const struct gl_format_info *info = _mesa_get_format_info(format);
677 return info->BaseFormat;
678 }
679
680
681 /** Is the given format a compressed format? */
682 GLboolean
683 _mesa_is_format_compressed(gl_format format)
684 {
685 const struct gl_format_info *info = _mesa_get_format_info(format);
686 return info->BlockWidth > 1 || info->BlockHeight > 1;
687 }
688
689
690 /**
691 * Return color encoding for given format.
692 * \return GL_LINEAR or GL_SRGB
693 */
694 GLenum
695 _mesa_get_format_color_encoding(gl_format format)
696 {
697 /* XXX this info should be encoded in gl_format_info */
698 switch (format) {
699 case MESA_FORMAT_SRGB8:
700 case MESA_FORMAT_SRGBA8:
701 case MESA_FORMAT_SARGB8:
702 case MESA_FORMAT_SL8:
703 case MESA_FORMAT_SLA8:
704 case MESA_FORMAT_SRGB_DXT1:
705 case MESA_FORMAT_SRGBA_DXT1:
706 case MESA_FORMAT_SRGBA_DXT3:
707 case MESA_FORMAT_SRGBA_DXT5:
708 return GL_SRGB;
709 default:
710 return GL_LINEAR;
711 }
712 }
713
714
715 /**
716 * Return number of bytes needed to store an image of the given size
717 * in the given format.
718 */
719 GLuint
720 _mesa_format_image_size(gl_format format, GLsizei width,
721 GLsizei height, GLsizei depth)
722 {
723 const struct gl_format_info *info = _mesa_get_format_info(format);
724 /* Strictly speaking, a conditional isn't needed here */
725 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
726 /* compressed format */
727 const GLuint bw = info->BlockWidth, bh = info->BlockHeight;
728 const GLuint wblocks = (width + bw - 1) / bw;
729 const GLuint hblocks = (height + bh - 1) / bh;
730 const GLuint sz = wblocks * hblocks * info->BytesPerBlock;
731 return sz;
732 }
733 else {
734 /* non-compressed */
735 const GLuint sz = width * height * depth * info->BytesPerBlock;
736 return sz;
737 }
738 }
739
740
741
742 GLint
743 _mesa_format_row_stride(gl_format format, GLsizei width)
744 {
745 const struct gl_format_info *info = _mesa_get_format_info(format);
746 /* Strictly speaking, a conditional isn't needed here */
747 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
748 /* compressed format */
749 const GLuint bw = info->BlockWidth;
750 const GLuint wblocks = (width + bw - 1) / bw;
751 const GLint stride = wblocks * info->BytesPerBlock;
752 return stride;
753 }
754 else {
755 const GLint stride = width * info->BytesPerBlock;
756 return stride;
757 }
758 }
759
760
761
762 /**
763 * Do sanity checking of the format info table.
764 */
765 void
766 _mesa_test_formats(void)
767 {
768 GLuint i;
769
770 assert(Elements(format_info) == MESA_FORMAT_COUNT);
771
772 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
773 const struct gl_format_info *info = _mesa_get_format_info(i);
774 assert(info);
775
776 assert(info->Name == i);
777
778 if (info->Name == MESA_FORMAT_NONE)
779 continue;
780
781 if (info->BlockWidth == 1 && info->BlockHeight == 1) {
782 if (info->RedBits > 0) {
783 GLuint t = info->RedBits + info->GreenBits
784 + info->BlueBits + info->AlphaBits;
785 assert(t / 8 == info->BytesPerBlock);
786 }
787 }
788
789 assert(info->DataType == GL_UNSIGNED_NORMALIZED ||
790 info->DataType == GL_SIGNED_NORMALIZED ||
791 info->DataType == GL_UNSIGNED_INT ||
792 info->DataType == GL_FLOAT);
793
794 if (info->BaseFormat == GL_RGB) {
795 assert(info->RedBits > 0);
796 assert(info->GreenBits > 0);
797 assert(info->BlueBits > 0);
798 assert(info->AlphaBits == 0);
799 assert(info->LuminanceBits == 0);
800 assert(info->IntensityBits == 0);
801 }
802 else if (info->BaseFormat == GL_RGBA) {
803 assert(info->RedBits > 0);
804 assert(info->GreenBits > 0);
805 assert(info->BlueBits > 0);
806 assert(info->AlphaBits > 0);
807 assert(info->LuminanceBits == 0);
808 assert(info->IntensityBits == 0);
809 }
810 else if (info->BaseFormat == GL_LUMINANCE) {
811 assert(info->RedBits == 0);
812 assert(info->GreenBits == 0);
813 assert(info->BlueBits == 0);
814 assert(info->AlphaBits == 0);
815 assert(info->LuminanceBits > 0);
816 assert(info->IntensityBits == 0);
817 }
818 else if (info->BaseFormat == GL_INTENSITY) {
819 assert(info->RedBits == 0);
820 assert(info->GreenBits == 0);
821 assert(info->BlueBits == 0);
822 assert(info->AlphaBits == 0);
823 assert(info->LuminanceBits == 0);
824 assert(info->IntensityBits > 0);
825 }
826
827 }
828 }
829
830
831
832 /**
833 * Return datatype and number of components per texel for the given gl_format.
834 * Only used for mipmap generation code.
835 */
836 void
837 _mesa_format_to_type_and_comps(gl_format format,
838 GLenum *datatype, GLuint *comps)
839 {
840 switch (format) {
841 case MESA_FORMAT_RGBA8888:
842 case MESA_FORMAT_RGBA8888_REV:
843 case MESA_FORMAT_ARGB8888:
844 case MESA_FORMAT_ARGB8888_REV:
845 case MESA_FORMAT_XRGB8888:
846 *datatype = GL_UNSIGNED_BYTE;
847 *comps = 4;
848 return;
849 case MESA_FORMAT_RGB888:
850 case MESA_FORMAT_BGR888:
851 *datatype = GL_UNSIGNED_BYTE;
852 *comps = 3;
853 return;
854 case MESA_FORMAT_RGB565:
855 case MESA_FORMAT_RGB565_REV:
856 *datatype = GL_UNSIGNED_SHORT_5_6_5;
857 *comps = 3;
858 return;
859
860 case MESA_FORMAT_ARGB4444:
861 case MESA_FORMAT_ARGB4444_REV:
862 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
863 *comps = 4;
864 return;
865
866 case MESA_FORMAT_ARGB1555:
867 case MESA_FORMAT_ARGB1555_REV:
868 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
869 *comps = 4;
870 return;
871
872 case MESA_FORMAT_AL88:
873 case MESA_FORMAT_AL88_REV:
874 *datatype = GL_UNSIGNED_BYTE;
875 *comps = 2;
876 return;
877 case MESA_FORMAT_RGB332:
878 *datatype = GL_UNSIGNED_BYTE_3_3_2;
879 *comps = 3;
880 return;
881
882 case MESA_FORMAT_A8:
883 case MESA_FORMAT_L8:
884 case MESA_FORMAT_I8:
885 case MESA_FORMAT_CI8:
886 *datatype = GL_UNSIGNED_BYTE;
887 *comps = 1;
888 return;
889
890 case MESA_FORMAT_YCBCR:
891 case MESA_FORMAT_YCBCR_REV:
892 *datatype = GL_UNSIGNED_SHORT;
893 *comps = 2;
894 return;
895
896 case MESA_FORMAT_Z24_S8:
897 *datatype = GL_UNSIGNED_INT;
898 *comps = 1; /* XXX OK? */
899 return;
900
901 case MESA_FORMAT_S8_Z24:
902 *datatype = GL_UNSIGNED_INT;
903 *comps = 1; /* XXX OK? */
904 return;
905
906 case MESA_FORMAT_Z16:
907 *datatype = GL_UNSIGNED_SHORT;
908 *comps = 1;
909 return;
910
911 case MESA_FORMAT_X8_Z24:
912 *datatype = GL_UNSIGNED_INT;
913 *comps = 1;
914 return;
915
916 case MESA_FORMAT_Z32:
917 *datatype = GL_UNSIGNED_INT;
918 *comps = 1;
919 return;
920
921 case MESA_FORMAT_DUDV8:
922 *datatype = GL_BYTE;
923 *comps = 2;
924 return;
925
926 case MESA_FORMAT_SIGNED_RGBA8888:
927 case MESA_FORMAT_SIGNED_RGBA8888_REV:
928 *datatype = GL_BYTE;
929 *comps = 4;
930 return;
931 case MESA_FORMAT_SIGNED_RGBA_16:
932 *datatype = GL_SHORT;
933 *comps = 4;
934 return;
935
936 #if FEATURE_EXT_texture_sRGB
937 case MESA_FORMAT_SRGB8:
938 *datatype = GL_UNSIGNED_BYTE;
939 *comps = 3;
940 return;
941 case MESA_FORMAT_SRGBA8:
942 case MESA_FORMAT_SARGB8:
943 *datatype = GL_UNSIGNED_BYTE;
944 *comps = 4;
945 return;
946 case MESA_FORMAT_SL8:
947 *datatype = GL_UNSIGNED_BYTE;
948 *comps = 1;
949 return;
950 case MESA_FORMAT_SLA8:
951 *datatype = GL_UNSIGNED_BYTE;
952 *comps = 2;
953 return;
954 #endif
955
956 #if FEATURE_texture_fxt1
957 case MESA_FORMAT_RGB_FXT1:
958 case MESA_FORMAT_RGBA_FXT1:
959 #endif
960 #if FEATURE_texture_s3tc
961 case MESA_FORMAT_RGB_DXT1:
962 case MESA_FORMAT_RGBA_DXT1:
963 case MESA_FORMAT_RGBA_DXT3:
964 case MESA_FORMAT_RGBA_DXT5:
965 #if FEATURE_EXT_texture_sRGB
966 case MESA_FORMAT_SRGB_DXT1:
967 case MESA_FORMAT_SRGBA_DXT1:
968 case MESA_FORMAT_SRGBA_DXT3:
969 case MESA_FORMAT_SRGBA_DXT5:
970 #endif
971 /* XXX generate error instead? */
972 *datatype = GL_UNSIGNED_BYTE;
973 *comps = 0;
974 return;
975 #endif
976
977 case MESA_FORMAT_RGBA_FLOAT32:
978 *datatype = GL_FLOAT;
979 *comps = 4;
980 return;
981 case MESA_FORMAT_RGBA_FLOAT16:
982 *datatype = GL_HALF_FLOAT_ARB;
983 *comps = 4;
984 return;
985 case MESA_FORMAT_RGB_FLOAT32:
986 *datatype = GL_FLOAT;
987 *comps = 3;
988 return;
989 case MESA_FORMAT_RGB_FLOAT16:
990 *datatype = GL_HALF_FLOAT_ARB;
991 *comps = 3;
992 return;
993 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32:
994 *datatype = GL_FLOAT;
995 *comps = 2;
996 return;
997 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16:
998 *datatype = GL_HALF_FLOAT_ARB;
999 *comps = 2;
1000 return;
1001 case MESA_FORMAT_ALPHA_FLOAT32:
1002 case MESA_FORMAT_LUMINANCE_FLOAT32:
1003 case MESA_FORMAT_INTENSITY_FLOAT32:
1004 *datatype = GL_FLOAT;
1005 *comps = 1;
1006 return;
1007 case MESA_FORMAT_ALPHA_FLOAT16:
1008 case MESA_FORMAT_LUMINANCE_FLOAT16:
1009 case MESA_FORMAT_INTENSITY_FLOAT16:
1010 *datatype = GL_HALF_FLOAT_ARB;
1011 *comps = 1;
1012 return;
1013
1014 default:
1015 _mesa_problem(NULL, "bad format in _mesa_format_to_type_and_comps");
1016 *datatype = 0;
1017 *comps = 1;
1018 }
1019 }