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