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