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