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