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