mesa: initial EXT_texture_snorm support
[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 "mfeatures.h"
30
31
32 /**
33 * Information about texture formats.
34 */
35 struct gl_format_info
36 {
37 gl_format Name;
38
39 /** text name for debugging */
40 const char *StrName;
41
42 /**
43 * Base format is one of GL_RED, GL_RG, GL_RGB, GL_RGBA, GL_ALPHA,
44 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA,
45 * GL_COLOR_INDEX, GL_DEPTH_COMPONENT, GL_STENCIL_INDEX,
46 * GL_DEPTH_STENCIL, GL_DUDV_ATI.
47 */
48 GLenum BaseFormat;
49
50 /**
51 * Logical data type: one of GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALED,
52 * GL_UNSIGNED_INT, GL_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_AL44, /* Name */
227 "MESA_FORMAT_AL44", /* StrName */
228 GL_LUMINANCE_ALPHA, /* BaseFormat */
229 GL_UNSIGNED_NORMALIZED, /* DataType */
230 0, 0, 0, 4, /* Red/Green/Blue/AlphaBits */
231 4, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
232 1, 1, 1 /* BlockWidth/Height,Bytes */
233 },
234 {
235 MESA_FORMAT_AL88, /* Name */
236 "MESA_FORMAT_AL88", /* 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_AL88_REV, /* Name */
245 "MESA_FORMAT_AL88_REV", /* StrName */
246 GL_LUMINANCE_ALPHA, /* BaseFormat */
247 GL_UNSIGNED_NORMALIZED, /* DataType */
248 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
249 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
250 1, 1, 2 /* BlockWidth/Height,Bytes */
251 },
252 {
253 MESA_FORMAT_AL1616, /* Name */
254 "MESA_FORMAT_AL1616", /* 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_AL1616_REV, /* Name */
263 "MESA_FORMAT_AL1616_REV", /* StrName */
264 GL_LUMINANCE_ALPHA, /* BaseFormat */
265 GL_UNSIGNED_NORMALIZED, /* DataType */
266 0, 0, 0, 16, /* Red/Green/Blue/AlphaBits */
267 16, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
268 1, 1, 4 /* BlockWidth/Height,Bytes */
269 },
270 {
271 MESA_FORMAT_RGB332, /* Name */
272 "MESA_FORMAT_RGB332", /* StrName */
273 GL_RGB, /* BaseFormat */
274 GL_UNSIGNED_NORMALIZED, /* DataType */
275 3, 3, 2, 0, /* 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_A8, /* Name */
281 "MESA_FORMAT_A8", /* StrName */
282 GL_ALPHA, /* BaseFormat */
283 GL_UNSIGNED_NORMALIZED, /* DataType */
284 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
285 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
286 1, 1, 1 /* BlockWidth/Height,Bytes */
287 },
288 {
289 MESA_FORMAT_A16, /* Name */
290 "MESA_FORMAT_A16", /* StrName */
291 GL_ALPHA, /* BaseFormat */
292 GL_UNSIGNED_NORMALIZED, /* DataType */
293 0, 0, 0, 16, /* 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_L8, /* Name */
299 "MESA_FORMAT_L8", /* StrName */
300 GL_LUMINANCE, /* BaseFormat */
301 GL_UNSIGNED_NORMALIZED, /* DataType */
302 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
303 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
304 1, 1, 1 /* BlockWidth/Height,Bytes */
305 },
306 {
307 MESA_FORMAT_L16, /* Name */
308 "MESA_FORMAT_L16", /* StrName */
309 GL_LUMINANCE, /* BaseFormat */
310 GL_UNSIGNED_NORMALIZED, /* DataType */
311 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
312 16, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
313 1, 1, 2 /* BlockWidth/Height,Bytes */
314 },
315 {
316 MESA_FORMAT_I8, /* Name */
317 "MESA_FORMAT_I8", /* StrName */
318 GL_INTENSITY, /* BaseFormat */
319 GL_UNSIGNED_NORMALIZED, /* DataType */
320 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
321 0, 8, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
322 1, 1, 1 /* BlockWidth/Height,Bytes */
323 },
324 {
325 MESA_FORMAT_I16, /* Name */
326 "MESA_FORMAT_I16", /* StrName */
327 GL_INTENSITY, /* BaseFormat */
328 GL_UNSIGNED_NORMALIZED, /* DataType */
329 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
330 0, 16, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
331 1, 1, 2 /* BlockWidth/Height,Bytes */
332 },
333 {
334 MESA_FORMAT_CI8, /* Name */
335 "MESA_FORMAT_CI8", /* StrName */
336 GL_COLOR_INDEX, /* BaseFormat */
337 GL_UNSIGNED_INT, /* DataType */
338 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
339 0, 0, 8, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
340 1, 1, 1 /* BlockWidth/Height,Bytes */
341 },
342 {
343 MESA_FORMAT_YCBCR, /* Name */
344 "MESA_FORMAT_YCBCR", /* StrName */
345 GL_YCBCR_MESA, /* BaseFormat */
346 GL_UNSIGNED_NORMALIZED, /* DataType */
347 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
348 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
349 1, 1, 2 /* BlockWidth/Height,Bytes */
350 },
351 {
352 MESA_FORMAT_YCBCR_REV, /* Name */
353 "MESA_FORMAT_YCBCR_REV", /* StrName */
354 GL_YCBCR_MESA, /* BaseFormat */
355 GL_UNSIGNED_NORMALIZED, /* DataType */
356 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
357 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
358 1, 1, 2 /* BlockWidth/Height,Bytes */
359 },
360 {
361 MESA_FORMAT_R8,
362 "MESA_FORMAT_R8",
363 GL_RED,
364 GL_UNSIGNED_NORMALIZED,
365 8, 0, 0, 0,
366 0, 0, 0, 0, 0,
367 1, 1, 1
368 },
369 {
370 MESA_FORMAT_RG88,
371 "MESA_FORMAT_RG88",
372 GL_RG,
373 GL_UNSIGNED_NORMALIZED,
374 8, 8, 0, 0,
375 0, 0, 0, 0, 0,
376 1, 1, 2
377 },
378 {
379 MESA_FORMAT_RG88_REV,
380 "MESA_FORMAT_RG88_REV",
381 GL_RG,
382 GL_UNSIGNED_NORMALIZED,
383 8, 8, 0, 0,
384 0, 0, 0, 0, 0,
385 1, 1, 2
386 },
387 {
388 MESA_FORMAT_R16,
389 "MESA_FORMAT_R16",
390 GL_RED,
391 GL_UNSIGNED_NORMALIZED,
392 16, 0, 0, 0,
393 0, 0, 0, 0, 0,
394 1, 1, 2
395 },
396 {
397 MESA_FORMAT_RG1616,
398 "MESA_FORMAT_RG1616",
399 GL_RG,
400 GL_UNSIGNED_NORMALIZED,
401 16, 16, 0, 0,
402 0, 0, 0, 0, 0,
403 1, 1, 4
404 },
405 {
406 MESA_FORMAT_RG1616_REV,
407 "MESA_FORMAT_RG1616_REV",
408 GL_RG,
409 GL_UNSIGNED_NORMALIZED,
410 16, 16, 0, 0,
411 0, 0, 0, 0, 0,
412 1, 1, 4
413 },
414 {
415 MESA_FORMAT_ARGB2101010,
416 "MESA_FORMAT_ARGB2101010",
417 GL_RGBA,
418 GL_UNSIGNED_NORMALIZED,
419 10, 10, 10, 2,
420 0, 0, 0, 0, 0,
421 1, 1, 4
422 },
423 {
424 MESA_FORMAT_Z24_S8, /* Name */
425 "MESA_FORMAT_Z24_S8", /* StrName */
426 GL_DEPTH_STENCIL, /* BaseFormat */
427 GL_UNSIGNED_INT, /* DataType */
428 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
429 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
430 1, 1, 4 /* BlockWidth/Height,Bytes */
431 },
432 {
433 MESA_FORMAT_S8_Z24, /* Name */
434 "MESA_FORMAT_S8_Z24", /* StrName */
435 GL_DEPTH_STENCIL, /* BaseFormat */
436 GL_UNSIGNED_INT, /* DataType */
437 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
438 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
439 1, 1, 4 /* BlockWidth/Height,Bytes */
440 },
441 {
442 MESA_FORMAT_Z16, /* Name */
443 "MESA_FORMAT_Z16", /* StrName */
444 GL_DEPTH_COMPONENT, /* BaseFormat */
445 GL_UNSIGNED_INT, /* DataType */
446 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
447 0, 0, 0, 16, 0, /* Lum/Int/Index/Depth/StencilBits */
448 1, 1, 2 /* BlockWidth/Height,Bytes */
449 },
450 {
451 MESA_FORMAT_X8_Z24, /* Name */
452 "MESA_FORMAT_X8_Z24", /* StrName */
453 GL_DEPTH_COMPONENT, /* BaseFormat */
454 GL_UNSIGNED_INT, /* DataType */
455 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
456 0, 0, 0, 24, 0, /* Lum/Int/Index/Depth/StencilBits */
457 1, 1, 4 /* BlockWidth/Height,Bytes */
458 },
459 {
460 MESA_FORMAT_Z24_X8, /* Name */
461 "MESA_FORMAT_Z24_X8", /* StrName */
462 GL_DEPTH_COMPONENT, /* BaseFormat */
463 GL_UNSIGNED_INT, /* DataType */
464 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
465 0, 0, 0, 24, 0, /* Lum/Int/Index/Depth/StencilBits */
466 1, 1, 4 /* BlockWidth/Height,Bytes */
467 },
468 {
469 MESA_FORMAT_Z32, /* Name */
470 "MESA_FORMAT_Z32", /* StrName */
471 GL_DEPTH_COMPONENT, /* BaseFormat */
472 GL_UNSIGNED_INT, /* DataType */
473 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
474 0, 0, 0, 32, 0, /* Lum/Int/Index/Depth/StencilBits */
475 1, 1, 4 /* BlockWidth/Height,Bytes */
476 },
477 {
478 MESA_FORMAT_S8, /* Name */
479 "MESA_FORMAT_S8", /* StrName */
480 GL_STENCIL_INDEX, /* BaseFormat */
481 GL_UNSIGNED_INT, /* DataType */
482 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
483 0, 0, 0, 0, 8, /* Lum/Int/Index/Depth/StencilBits */
484 1, 1, 1 /* BlockWidth/Height,Bytes */
485 },
486 {
487 MESA_FORMAT_SRGB8,
488 "MESA_FORMAT_SRGB8",
489 GL_RGB,
490 GL_UNSIGNED_NORMALIZED,
491 8, 8, 8, 0,
492 0, 0, 0, 0, 0,
493 1, 1, 3
494 },
495 {
496 MESA_FORMAT_SRGBA8,
497 "MESA_FORMAT_SRGBA8",
498 GL_RGBA,
499 GL_UNSIGNED_NORMALIZED,
500 8, 8, 8, 8,
501 0, 0, 0, 0, 0,
502 1, 1, 4
503 },
504 {
505 MESA_FORMAT_SARGB8,
506 "MESA_FORMAT_SARGB8",
507 GL_RGBA,
508 GL_UNSIGNED_NORMALIZED,
509 8, 8, 8, 8,
510 0, 0, 0, 0, 0,
511 1, 1, 4
512 },
513 {
514 MESA_FORMAT_SL8,
515 "MESA_FORMAT_SL8",
516 GL_LUMINANCE,
517 GL_UNSIGNED_NORMALIZED,
518 0, 0, 0, 0,
519 8, 0, 0, 0, 0,
520 1, 1, 1
521 },
522 {
523 MESA_FORMAT_SLA8,
524 "MESA_FORMAT_SLA8",
525 GL_LUMINANCE_ALPHA,
526 GL_UNSIGNED_NORMALIZED,
527 0, 0, 0, 8,
528 8, 0, 0, 0, 0,
529 1, 1, 2
530 },
531 {
532 MESA_FORMAT_SRGB_DXT1, /* Name */
533 "MESA_FORMAT_SRGB_DXT1", /* StrName */
534 GL_RGB, /* BaseFormat */
535 GL_UNSIGNED_NORMALIZED, /* DataType */
536 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
537 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
538 4, 4, 8 /* 8 bytes per 4x4 block */
539 },
540 {
541 MESA_FORMAT_SRGBA_DXT1,
542 "MESA_FORMAT_SRGBA_DXT1",
543 GL_RGBA,
544 GL_UNSIGNED_NORMALIZED,
545 4, 4, 4, 4,
546 0, 0, 0, 0, 0,
547 4, 4, 8 /* 8 bytes per 4x4 block */
548 },
549 {
550 MESA_FORMAT_SRGBA_DXT3,
551 "MESA_FORMAT_SRGBA_DXT3",
552 GL_RGBA,
553 GL_UNSIGNED_NORMALIZED,
554 4, 4, 4, 4,
555 0, 0, 0, 0, 0,
556 4, 4, 16 /* 16 bytes per 4x4 block */
557 },
558 {
559 MESA_FORMAT_SRGBA_DXT5,
560 "MESA_FORMAT_SRGBA_DXT5",
561 GL_RGBA,
562 GL_UNSIGNED_NORMALIZED,
563 4, 4, 4, 4,
564 0, 0, 0, 0, 0,
565 4, 4, 16 /* 16 bytes per 4x4 block */
566 },
567
568 {
569 MESA_FORMAT_RGB_FXT1,
570 "MESA_FORMAT_RGB_FXT1",
571 GL_RGB,
572 GL_UNSIGNED_NORMALIZED,
573 4, 4, 4, 0, /* approx Red/Green/BlueBits */
574 0, 0, 0, 0, 0,
575 8, 4, 16 /* 16 bytes per 8x4 block */
576 },
577 {
578 MESA_FORMAT_RGBA_FXT1,
579 "MESA_FORMAT_RGBA_FXT1",
580 GL_RGBA,
581 GL_UNSIGNED_NORMALIZED,
582 4, 4, 4, 1, /* approx Red/Green/Blue/AlphaBits */
583 0, 0, 0, 0, 0,
584 8, 4, 16 /* 16 bytes per 8x4 block */
585 },
586
587 {
588 MESA_FORMAT_RGB_DXT1, /* Name */
589 "MESA_FORMAT_RGB_DXT1", /* StrName */
590 GL_RGB, /* BaseFormat */
591 GL_UNSIGNED_NORMALIZED, /* DataType */
592 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
593 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
594 4, 4, 8 /* 8 bytes per 4x4 block */
595 },
596 {
597 MESA_FORMAT_RGBA_DXT1,
598 "MESA_FORMAT_RGBA_DXT1",
599 GL_RGBA,
600 GL_UNSIGNED_NORMALIZED,
601 4, 4, 4, 4,
602 0, 0, 0, 0, 0,
603 4, 4, 8 /* 8 bytes per 4x4 block */
604 },
605 {
606 MESA_FORMAT_RGBA_DXT3,
607 "MESA_FORMAT_RGBA_DXT3",
608 GL_RGBA,
609 GL_UNSIGNED_NORMALIZED,
610 4, 4, 4, 4,
611 0, 0, 0, 0, 0,
612 4, 4, 16 /* 16 bytes per 4x4 block */
613 },
614 {
615 MESA_FORMAT_RGBA_DXT5,
616 "MESA_FORMAT_RGBA_DXT5",
617 GL_RGBA,
618 GL_UNSIGNED_NORMALIZED,
619 4, 4, 4, 4,
620 0, 0, 0, 0, 0,
621 4, 4, 16 /* 16 bytes per 4x4 block */
622 },
623 {
624 MESA_FORMAT_RGBA_FLOAT32,
625 "MESA_FORMAT_RGBA_FLOAT32",
626 GL_RGBA,
627 GL_FLOAT,
628 32, 32, 32, 32,
629 0, 0, 0, 0, 0,
630 1, 1, 16
631 },
632 {
633 MESA_FORMAT_RGBA_FLOAT16,
634 "MESA_FORMAT_RGBA_FLOAT16",
635 GL_RGBA,
636 GL_FLOAT,
637 16, 16, 16, 16,
638 0, 0, 0, 0, 0,
639 1, 1, 8
640 },
641 {
642 MESA_FORMAT_RGB_FLOAT32,
643 "MESA_FORMAT_RGB_FLOAT32",
644 GL_RGB,
645 GL_FLOAT,
646 32, 32, 32, 0,
647 0, 0, 0, 0, 0,
648 1, 1, 12
649 },
650 {
651 MESA_FORMAT_RGB_FLOAT16,
652 "MESA_FORMAT_RGB_FLOAT16",
653 GL_RGB,
654 GL_FLOAT,
655 16, 16, 16, 0,
656 0, 0, 0, 0, 0,
657 1, 1, 6
658 },
659 {
660 MESA_FORMAT_ALPHA_FLOAT32,
661 "MESA_FORMAT_ALPHA_FLOAT32",
662 GL_ALPHA,
663 GL_FLOAT,
664 0, 0, 0, 32,
665 0, 0, 0, 0, 0,
666 1, 1, 4
667 },
668 {
669 MESA_FORMAT_ALPHA_FLOAT16,
670 "MESA_FORMAT_ALPHA_FLOAT16",
671 GL_ALPHA,
672 GL_FLOAT,
673 0, 0, 0, 16,
674 0, 0, 0, 0, 0,
675 1, 1, 2
676 },
677 {
678 MESA_FORMAT_LUMINANCE_FLOAT32,
679 "MESA_FORMAT_LUMINANCE_FLOAT32",
680 GL_ALPHA,
681 GL_FLOAT,
682 0, 0, 0, 0,
683 32, 0, 0, 0, 0,
684 1, 1, 4
685 },
686 {
687 MESA_FORMAT_LUMINANCE_FLOAT16,
688 "MESA_FORMAT_LUMINANCE_FLOAT16",
689 GL_ALPHA,
690 GL_FLOAT,
691 0, 0, 0, 0,
692 16, 0, 0, 0, 0,
693 1, 1, 2
694 },
695 {
696 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
697 "MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32",
698 GL_LUMINANCE_ALPHA,
699 GL_FLOAT,
700 0, 0, 0, 32,
701 32, 0, 0, 0, 0,
702 1, 1, 8
703 },
704 {
705 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
706 "MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16",
707 GL_LUMINANCE_ALPHA,
708 GL_FLOAT,
709 0, 0, 0, 16,
710 16, 0, 0, 0, 0,
711 1, 1, 4
712 },
713 {
714 MESA_FORMAT_INTENSITY_FLOAT32,
715 "MESA_FORMAT_INTENSITY_FLOAT32",
716 GL_INTENSITY,
717 GL_FLOAT,
718 0, 0, 0, 0,
719 0, 32, 0, 0, 0,
720 1, 1, 4
721 },
722 {
723 MESA_FORMAT_INTENSITY_FLOAT16,
724 "MESA_FORMAT_INTENSITY_FLOAT16",
725 GL_INTENSITY,
726 GL_FLOAT,
727 0, 0, 0, 0,
728 0, 16, 0, 0, 0,
729 1, 1, 2
730 },
731
732 /* unnormalized signed int formats */
733 {
734 MESA_FORMAT_RGBA_INT8,
735 "MESA_FORMAT_RGBA_INT8",
736 GL_RGBA,
737 GL_INT,
738 8, 8, 8, 8,
739 0, 0, 0, 0, 0,
740 1, 1, 4
741 },
742 {
743 MESA_FORMAT_RGBA_INT16,
744 "MESA_FORMAT_RGBA_INT16",
745 GL_RGBA,
746 GL_INT,
747 16, 16, 16, 16,
748 0, 0, 0, 0, 0,
749 1, 1, 8
750 },
751 {
752 MESA_FORMAT_RGBA_INT32,
753 "MESA_FORMAT_RGBA_INT32",
754 GL_RGBA,
755 GL_INT,
756 32, 32, 32, 32,
757 0, 0, 0, 0, 0,
758 1, 1, 16
759 },
760
761 /* unnormalized unsigned int formats */
762 {
763 MESA_FORMAT_RGBA_UINT8,
764 "MESA_FORMAT_RGBA_UINT8",
765 GL_RGBA,
766 GL_UNSIGNED_INT,
767 8, 8, 8, 8,
768 0, 0, 0, 0, 0,
769 1, 1, 4
770 },
771 {
772 MESA_FORMAT_RGBA_UINT16,
773 "MESA_FORMAT_RGBA_UINT16",
774 GL_RGBA,
775 GL_UNSIGNED_INT,
776 16, 16, 16, 16,
777 0, 0, 0, 0, 0,
778 1, 1, 8
779 },
780 {
781 MESA_FORMAT_RGBA_UINT32,
782 "MESA_FORMAT_RGBA_UINT32",
783 GL_RGBA,
784 GL_UNSIGNED_INT,
785 32, 32, 32, 32,
786 0, 0, 0, 0, 0,
787 1, 1, 16
788 },
789
790
791 {
792 MESA_FORMAT_DUDV8,
793 "MESA_FORMAT_DUDV8",
794 GL_DUDV_ATI,
795 GL_SIGNED_NORMALIZED,
796 0, 0, 0, 0,
797 0, 0, 0, 0, 0,
798 1, 1, 2
799 },
800
801 /* Signed 8 bits / channel */
802 {
803 MESA_FORMAT_SIGNED_R8, /* Name */
804 "MESA_FORMAT_SIGNED_R8", /* StrName */
805 GL_RED, /* BaseFormat */
806 GL_SIGNED_NORMALIZED, /* DataType */
807 8, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
808 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
809 1, 1, 1 /* BlockWidth/Height,Bytes */
810 },
811 {
812 MESA_FORMAT_SIGNED_RG88_REV,
813 "MESA_FORMAT_SIGNED_RG88_REV",
814 GL_RG,
815 GL_SIGNED_NORMALIZED,
816 8, 8, 0, 0,
817 0, 0, 0, 0, 0,
818 1, 1, 2
819 },
820 {
821 MESA_FORMAT_SIGNED_RGBX8888,
822 "MESA_FORMAT_SIGNED_RGBX8888",
823 GL_RGB,
824 GL_SIGNED_NORMALIZED,
825 8, 8, 8, 0,
826 0, 0, 0, 0, 0,
827 1, 1, 4 /* 4 bpp, but no alpha */
828 },
829 {
830 MESA_FORMAT_SIGNED_RGBA8888,
831 "MESA_FORMAT_SIGNED_RGBA8888",
832 GL_RGBA,
833 GL_SIGNED_NORMALIZED,
834 8, 8, 8, 8,
835 0, 0, 0, 0, 0,
836 1, 1, 4
837 },
838 {
839 MESA_FORMAT_SIGNED_RGBA8888_REV,
840 "MESA_FORMAT_SIGNED_RGBA8888_REV",
841 GL_RGBA,
842 GL_SIGNED_NORMALIZED,
843 8, 8, 8, 8,
844 0, 0, 0, 0, 0,
845 1, 1, 4
846 },
847
848 /* Signed 16 bits / channel */
849 {
850 MESA_FORMAT_SIGNED_R16,
851 "MESA_FORMAT_SIGNED_R16",
852 GL_RED,
853 GL_SIGNED_NORMALIZED,
854 16, 0, 0, 0,
855 0, 0, 0, 0, 0,
856 1, 1, 2
857 },
858 {
859 MESA_FORMAT_SIGNED_GR1616,
860 "MESA_FORMAT_SIGNED_GR1616",
861 GL_RG,
862 GL_SIGNED_NORMALIZED,
863 16, 16, 0, 0,
864 0, 0, 0, 0, 0,
865 1, 1, 4
866 },
867 {
868 MESA_FORMAT_SIGNED_RGB_16,
869 "MESA_FORMAT_SIGNED_RGB_16",
870 GL_RGB,
871 GL_SIGNED_NORMALIZED,
872 16, 16, 16, 0,
873 0, 0, 0, 0, 0,
874 1, 1, 6
875 },
876 {
877 MESA_FORMAT_SIGNED_RGBA_16,
878 "MESA_FORMAT_SIGNED_RGBA_16",
879 GL_RGBA,
880 GL_SIGNED_NORMALIZED,
881 16, 16, 16, 16,
882 0, 0, 0, 0, 0,
883 1, 1, 8
884 },
885 {
886 MESA_FORMAT_RGBA_16,
887 "MESA_FORMAT_RGBA_16",
888 GL_RGBA,
889 GL_UNSIGNED_NORMALIZED,
890 16, 16, 16, 16,
891 0, 0, 0, 0, 0,
892 1, 1, 8
893 },
894 {
895 MESA_FORMAT_RED_RGTC1,
896 "MESA_FORMAT_RED_RGTC1",
897 GL_RED,
898 GL_UNSIGNED_NORMALIZED,
899 4, 0, 0, 0,
900 0, 0, 0, 0, 0,
901 4, 4, 8 /* 8 bytes per 4x4 block */
902 },
903 {
904 MESA_FORMAT_SIGNED_RED_RGTC1,
905 "MESA_FORMAT_SIGNED_RED_RGTC1",
906 GL_RED,
907 GL_SIGNED_NORMALIZED,
908 4, 0, 0, 0,
909 0, 0, 0, 0, 0,
910 4, 4, 8 /* 8 bytes per 4x4 block */
911 },
912 {
913 MESA_FORMAT_RG_RGTC2,
914 "MESA_FORMAT_RG_RGTC2",
915 GL_RG,
916 GL_UNSIGNED_NORMALIZED,
917 4, 4, 0, 0,
918 0, 0, 0, 0, 0,
919 4, 4, 16 /* 16 bytes per 4x4 block */
920 },
921 {
922 MESA_FORMAT_SIGNED_RG_RGTC2,
923 "MESA_FORMAT_SIGNED_RG_RGTC2",
924 GL_RG,
925 GL_SIGNED_NORMALIZED,
926 4, 4, 0, 0,
927 0, 0, 0, 0, 0,
928 4, 4, 16 /* 16 bytes per 4x4 block */
929 },
930 {
931 MESA_FORMAT_L_LATC1,
932 "MESA_FORMAT_L_LATC1",
933 GL_LUMINANCE,
934 GL_UNSIGNED_NORMALIZED,
935 0, 0, 0, 0,
936 4, 0, 0, 0, 0,
937 4, 4, 8 /* 8 bytes per 4x4 block */
938 },
939 {
940 MESA_FORMAT_SIGNED_L_LATC1,
941 "MESA_FORMAT_SIGNED_L_LATC1",
942 GL_LUMINANCE,
943 GL_SIGNED_NORMALIZED,
944 0, 0, 0, 0,
945 4, 0, 0, 0, 0,
946 4, 4, 8 /* 8 bytes per 4x4 block */
947 },
948 {
949 MESA_FORMAT_LA_LATC2,
950 "MESA_FORMAT_LA_LATC2",
951 GL_LUMINANCE_ALPHA,
952 GL_UNSIGNED_NORMALIZED,
953 0, 0, 0, 4,
954 4, 0, 0, 0, 0,
955 4, 4, 16 /* 16 bytes per 4x4 block */
956 },
957 {
958 MESA_FORMAT_SIGNED_LA_LATC2,
959 "MESA_FORMAT_SIGNED_LA_LATC2",
960 GL_LUMINANCE_ALPHA,
961 GL_SIGNED_NORMALIZED,
962 0, 0, 0, 4,
963 4, 0, 0, 0, 0,
964 4, 4, 16 /* 16 bytes per 4x4 block */
965 },
966
967 /* Signed formats from EXT_texture_snorm that are not in GL3.1 */
968 {
969 MESA_FORMAT_SIGNED_A8,
970 "MESA_FORMAT_SIGNED_A8",
971 GL_ALPHA,
972 GL_SIGNED_NORMALIZED,
973 0, 0, 0, 8,
974 0, 0, 0, 0, 0,
975 1, 1, 1
976 },
977 {
978 MESA_FORMAT_SIGNED_L8,
979 "MESA_FORMAT_SIGNED_L8",
980 GL_LUMINANCE,
981 GL_SIGNED_NORMALIZED,
982 0, 0, 0, 0,
983 8, 0, 0, 0, 0,
984 1, 1, 1
985 },
986 {
987 MESA_FORMAT_SIGNED_AL88,
988 "MESA_FORMAT_SIGNED_AL88",
989 GL_LUMINANCE_ALPHA,
990 GL_SIGNED_NORMALIZED,
991 0, 0, 0, 8,
992 8, 0, 0, 0, 0,
993 1, 1, 2
994 },
995 {
996 MESA_FORMAT_SIGNED_I8,
997 "MESA_FORMAT_SIGNED_I8",
998 GL_INTENSITY,
999 GL_SIGNED_NORMALIZED,
1000 0, 0, 0, 0,
1001 0, 8, 0, 0, 0,
1002 1, 1, 1
1003 },
1004 {
1005 MESA_FORMAT_SIGNED_A16,
1006 "MESA_FORMAT_SIGNED_A16",
1007 GL_ALPHA,
1008 GL_SIGNED_NORMALIZED,
1009 0, 0, 0, 16,
1010 0, 0, 0, 0, 0,
1011 1, 1, 2
1012 },
1013 {
1014 MESA_FORMAT_SIGNED_L16,
1015 "MESA_FORMAT_SIGNED_L16",
1016 GL_LUMINANCE,
1017 GL_SIGNED_NORMALIZED,
1018 0, 0, 0, 0,
1019 16, 0, 0, 0, 0,
1020 1, 1, 2
1021 },
1022 {
1023 MESA_FORMAT_SIGNED_AL1616,
1024 "MESA_FORMAT_SIGNED_AL1616",
1025 GL_LUMINANCE_ALPHA,
1026 GL_SIGNED_NORMALIZED,
1027 0, 0, 0, 16,
1028 16, 0, 0, 0, 0,
1029 1, 1, 4
1030 },
1031 {
1032 MESA_FORMAT_SIGNED_I16,
1033 "MESA_FORMAT_SIGNED_I16",
1034 GL_INTENSITY,
1035 GL_SIGNED_NORMALIZED,
1036 0, 0, 0, 0,
1037 0, 16, 0, 0, 0,
1038 1, 1, 2
1039 }
1040 };
1041
1042
1043
1044 static const struct gl_format_info *
1045 _mesa_get_format_info(gl_format format)
1046 {
1047 const struct gl_format_info *info = &format_info[format];
1048 assert(info->Name == format);
1049 return info;
1050 }
1051
1052
1053 /** Return string name of format (for debugging) */
1054 const char *
1055 _mesa_get_format_name(gl_format format)
1056 {
1057 const struct gl_format_info *info = _mesa_get_format_info(format);
1058 return info->StrName;
1059 }
1060
1061
1062
1063 /**
1064 * Return bytes needed to store a block of pixels in the given format.
1065 * Normally, a block is 1x1 (a single pixel). But for compressed formats
1066 * a block may be 4x4 or 8x4, etc.
1067 */
1068 GLuint
1069 _mesa_get_format_bytes(gl_format format)
1070 {
1071 const struct gl_format_info *info = _mesa_get_format_info(format);
1072 ASSERT(info->BytesPerBlock);
1073 return info->BytesPerBlock;
1074 }
1075
1076
1077 /**
1078 * Return bits per component for the given format.
1079 * \param format one of MESA_FORMAT_x
1080 * \param pname the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
1081 */
1082 GLint
1083 _mesa_get_format_bits(gl_format format, GLenum pname)
1084 {
1085 const struct gl_format_info *info = _mesa_get_format_info(format);
1086
1087 switch (pname) {
1088 case GL_RED_BITS:
1089 case GL_TEXTURE_RED_SIZE:
1090 case GL_RENDERBUFFER_RED_SIZE_EXT:
1091 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1092 return info->RedBits;
1093 case GL_GREEN_BITS:
1094 case GL_TEXTURE_GREEN_SIZE:
1095 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1096 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1097 return info->GreenBits;
1098 case GL_BLUE_BITS:
1099 case GL_TEXTURE_BLUE_SIZE:
1100 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1101 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1102 return info->BlueBits;
1103 case GL_ALPHA_BITS:
1104 case GL_TEXTURE_ALPHA_SIZE:
1105 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1106 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1107 return info->AlphaBits;
1108 case GL_TEXTURE_INTENSITY_SIZE:
1109 return info->IntensityBits;
1110 case GL_TEXTURE_LUMINANCE_SIZE:
1111 return info->LuminanceBits;
1112 case GL_INDEX_BITS:
1113 case GL_TEXTURE_INDEX_SIZE_EXT:
1114 return info->IndexBits;
1115 case GL_DEPTH_BITS:
1116 case GL_TEXTURE_DEPTH_SIZE_ARB:
1117 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1118 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1119 return info->DepthBits;
1120 case GL_STENCIL_BITS:
1121 case GL_TEXTURE_STENCIL_SIZE_EXT:
1122 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1123 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1124 return info->StencilBits;
1125 default:
1126 _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
1127 return 0;
1128 }
1129 }
1130
1131
1132 /**
1133 * Return the data type (or more specifically, the data representation)
1134 * for the given format.
1135 * The return value will be one of:
1136 * GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
1137 * GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
1138 * GL_UNSIGNED_INT = an ordinary unsigned integer
1139 * GL_INT = an ordinary signed integer
1140 * GL_FLOAT = an ordinary float
1141 */
1142 GLenum
1143 _mesa_get_format_datatype(gl_format format)
1144 {
1145 const struct gl_format_info *info = _mesa_get_format_info(format);
1146 return info->DataType;
1147 }
1148
1149
1150 /**
1151 * Return the basic format for the given type. The result will be
1152 * one of GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA,
1153 * GL_INTENSITY, GL_YCBCR_MESA, GL_COLOR_INDEX, GL_DEPTH_COMPONENT,
1154 * GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
1155 */
1156 GLenum
1157 _mesa_get_format_base_format(gl_format format)
1158 {
1159 const struct gl_format_info *info = _mesa_get_format_info(format);
1160 return info->BaseFormat;
1161 }
1162
1163
1164 /**
1165 * Return the block size (in pixels) for the given format. Normally
1166 * the block size is 1x1. But compressed formats will have block sizes
1167 * of 4x4 or 8x4 pixels, etc.
1168 * \param bw returns block width in pixels
1169 * \param bh returns block height in pixels
1170 */
1171 void
1172 _mesa_get_format_block_size(gl_format format, GLuint *bw, GLuint *bh)
1173 {
1174 const struct gl_format_info *info = _mesa_get_format_info(format);
1175 *bw = info->BlockWidth;
1176 *bh = info->BlockHeight;
1177 }
1178
1179
1180 /** Is the given format a compressed format? */
1181 GLboolean
1182 _mesa_is_format_compressed(gl_format format)
1183 {
1184 const struct gl_format_info *info = _mesa_get_format_info(format);
1185 return info->BlockWidth > 1 || info->BlockHeight > 1;
1186 }
1187
1188
1189 /**
1190 * Determine if the given format represents a packed depth/stencil buffer.
1191 */
1192 GLboolean
1193 _mesa_is_format_packed_depth_stencil(gl_format format)
1194 {
1195 const struct gl_format_info *info = _mesa_get_format_info(format);
1196
1197 return info->BaseFormat == GL_DEPTH_STENCIL;
1198 }
1199
1200
1201 /**
1202 * Is the given format a signed/unsigned integer color format?
1203 */
1204 GLboolean
1205 _mesa_is_format_integer_color(gl_format format)
1206 {
1207 const struct gl_format_info *info = _mesa_get_format_info(format);
1208 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
1209 info->BaseFormat != GL_DEPTH_COMPONENT &&
1210 info->BaseFormat != GL_DEPTH_STENCIL &&
1211 info->BaseFormat != GL_STENCIL_INDEX;
1212 }
1213
1214
1215 /**
1216 * Return color encoding for given format.
1217 * \return GL_LINEAR or GL_SRGB
1218 */
1219 GLenum
1220 _mesa_get_format_color_encoding(gl_format format)
1221 {
1222 /* XXX this info should be encoded in gl_format_info */
1223 switch (format) {
1224 case MESA_FORMAT_SRGB8:
1225 case MESA_FORMAT_SRGBA8:
1226 case MESA_FORMAT_SARGB8:
1227 case MESA_FORMAT_SL8:
1228 case MESA_FORMAT_SLA8:
1229 case MESA_FORMAT_SRGB_DXT1:
1230 case MESA_FORMAT_SRGBA_DXT1:
1231 case MESA_FORMAT_SRGBA_DXT3:
1232 case MESA_FORMAT_SRGBA_DXT5:
1233 return GL_SRGB;
1234 default:
1235 return GL_LINEAR;
1236 }
1237 }
1238
1239
1240 /**
1241 * For an sRGB format, return the corresponding linear color space format.
1242 * For non-sRGB formats, return the format as-is.
1243 */
1244 gl_format
1245 _mesa_get_srgb_format_linear(gl_format format)
1246 {
1247 switch (format) {
1248 case MESA_FORMAT_SRGB8:
1249 format = MESA_FORMAT_RGB888;
1250 break;
1251 case MESA_FORMAT_SRGBA8:
1252 format = MESA_FORMAT_RGBA8888;
1253 break;
1254 case MESA_FORMAT_SARGB8:
1255 format = MESA_FORMAT_ARGB8888;
1256 break;
1257 case MESA_FORMAT_SL8:
1258 format = MESA_FORMAT_L8;
1259 break;
1260 case MESA_FORMAT_SLA8:
1261 format = MESA_FORMAT_AL88;
1262 break;
1263 case MESA_FORMAT_SRGB_DXT1:
1264 format = MESA_FORMAT_RGB_DXT1;
1265 break;
1266 case MESA_FORMAT_SRGBA_DXT1:
1267 format = MESA_FORMAT_RGBA_DXT1;
1268 break;
1269 case MESA_FORMAT_SRGBA_DXT3:
1270 format = MESA_FORMAT_RGBA_DXT3;
1271 break;
1272 case MESA_FORMAT_SRGBA_DXT5:
1273 format = MESA_FORMAT_RGBA_DXT5;
1274 break;
1275 default:
1276 break;
1277 }
1278 return format;
1279 }
1280
1281
1282 /**
1283 * Return number of bytes needed to store an image of the given size
1284 * in the given format.
1285 */
1286 GLuint
1287 _mesa_format_image_size(gl_format format, GLsizei width,
1288 GLsizei height, GLsizei depth)
1289 {
1290 const struct gl_format_info *info = _mesa_get_format_info(format);
1291 /* Strictly speaking, a conditional isn't needed here */
1292 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
1293 /* compressed format (2D only for now) */
1294 const GLuint bw = info->BlockWidth, bh = info->BlockHeight;
1295 const GLuint wblocks = (width + bw - 1) / bw;
1296 const GLuint hblocks = (height + bh - 1) / bh;
1297 const GLuint sz = wblocks * hblocks * info->BytesPerBlock;
1298 assert(depth == 1);
1299 return sz;
1300 }
1301 else {
1302 /* non-compressed */
1303 const GLuint sz = width * height * depth * info->BytesPerBlock;
1304 return sz;
1305 }
1306 }
1307
1308
1309 /**
1310 * Same as _mesa_format_image_size() but returns a 64-bit value to
1311 * accomodate very large textures.
1312 */
1313 uint64_t
1314 _mesa_format_image_size64(gl_format format, GLsizei width,
1315 GLsizei height, GLsizei depth)
1316 {
1317 const struct gl_format_info *info = _mesa_get_format_info(format);
1318 /* Strictly speaking, a conditional isn't needed here */
1319 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
1320 /* compressed format (2D only for now) */
1321 const uint64_t bw = info->BlockWidth, bh = info->BlockHeight;
1322 const uint64_t wblocks = (width + bw - 1) / bw;
1323 const uint64_t hblocks = (height + bh - 1) / bh;
1324 const uint64_t sz = wblocks * hblocks * info->BytesPerBlock;
1325 assert(depth == 1);
1326 return sz;
1327 }
1328 else {
1329 /* non-compressed */
1330 const uint64_t sz = ((uint64_t) width *
1331 (uint64_t) height *
1332 (uint64_t) depth *
1333 info->BytesPerBlock);
1334 return sz;
1335 }
1336 }
1337
1338
1339
1340 GLint
1341 _mesa_format_row_stride(gl_format format, GLsizei width)
1342 {
1343 const struct gl_format_info *info = _mesa_get_format_info(format);
1344 /* Strictly speaking, a conditional isn't needed here */
1345 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
1346 /* compressed format */
1347 const GLuint bw = info->BlockWidth;
1348 const GLuint wblocks = (width + bw - 1) / bw;
1349 const GLint stride = wblocks * info->BytesPerBlock;
1350 return stride;
1351 }
1352 else {
1353 const GLint stride = width * info->BytesPerBlock;
1354 return stride;
1355 }
1356 }
1357
1358
1359 /**
1360 * Debug/test: check that all formats are handled in the
1361 * _mesa_format_to_type_and_comps() function. When new pixel formats
1362 * are added to Mesa, that function needs to be updated.
1363 * This is a no-op after the first call.
1364 */
1365 static void
1366 check_format_to_type_and_comps(void)
1367 {
1368 gl_format f;
1369
1370 for (f = MESA_FORMAT_NONE + 1; f < MESA_FORMAT_COUNT; f++) {
1371 GLenum datatype = 0;
1372 GLuint comps = 0;
1373 /* This function will emit a problem/warning if the format is
1374 * not handled.
1375 */
1376 _mesa_format_to_type_and_comps(f, &datatype, &comps);
1377 }
1378 }
1379
1380
1381 /**
1382 * Do sanity checking of the format info table.
1383 */
1384 void
1385 _mesa_test_formats(void)
1386 {
1387 GLuint i;
1388
1389 assert(Elements(format_info) == MESA_FORMAT_COUNT);
1390
1391 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
1392 const struct gl_format_info *info = _mesa_get_format_info(i);
1393 assert(info);
1394
1395 assert(info->Name == i);
1396
1397 if (info->Name == MESA_FORMAT_NONE)
1398 continue;
1399
1400 if (info->BlockWidth == 1 && info->BlockHeight == 1) {
1401 if (info->RedBits > 0) {
1402 GLuint t = info->RedBits + info->GreenBits
1403 + info->BlueBits + info->AlphaBits;
1404 assert(t / 8 <= info->BytesPerBlock);
1405 (void) t;
1406 }
1407 }
1408
1409 assert(info->DataType == GL_UNSIGNED_NORMALIZED ||
1410 info->DataType == GL_SIGNED_NORMALIZED ||
1411 info->DataType == GL_UNSIGNED_INT ||
1412 info->DataType == GL_INT ||
1413 info->DataType == GL_FLOAT);
1414
1415 if (info->BaseFormat == GL_RGB) {
1416 assert(info->RedBits > 0);
1417 assert(info->GreenBits > 0);
1418 assert(info->BlueBits > 0);
1419 assert(info->AlphaBits == 0);
1420 assert(info->LuminanceBits == 0);
1421 assert(info->IntensityBits == 0);
1422 }
1423 else if (info->BaseFormat == GL_RGBA) {
1424 assert(info->RedBits > 0);
1425 assert(info->GreenBits > 0);
1426 assert(info->BlueBits > 0);
1427 assert(info->AlphaBits > 0);
1428 assert(info->LuminanceBits == 0);
1429 assert(info->IntensityBits == 0);
1430 }
1431 else if (info->BaseFormat == GL_RG) {
1432 assert(info->RedBits > 0);
1433 assert(info->GreenBits > 0);
1434 assert(info->BlueBits == 0);
1435 assert(info->AlphaBits == 0);
1436 assert(info->LuminanceBits == 0);
1437 assert(info->IntensityBits == 0);
1438 }
1439 else if (info->BaseFormat == GL_RED) {
1440 assert(info->RedBits > 0);
1441 assert(info->GreenBits == 0);
1442 assert(info->BlueBits == 0);
1443 assert(info->AlphaBits == 0);
1444 assert(info->LuminanceBits == 0);
1445 assert(info->IntensityBits == 0);
1446 }
1447 else if (info->BaseFormat == GL_LUMINANCE) {
1448 assert(info->RedBits == 0);
1449 assert(info->GreenBits == 0);
1450 assert(info->BlueBits == 0);
1451 assert(info->AlphaBits == 0);
1452 assert(info->LuminanceBits > 0);
1453 assert(info->IntensityBits == 0);
1454 }
1455 else if (info->BaseFormat == GL_INTENSITY) {
1456 assert(info->RedBits == 0);
1457 assert(info->GreenBits == 0);
1458 assert(info->BlueBits == 0);
1459 assert(info->AlphaBits == 0);
1460 assert(info->LuminanceBits == 0);
1461 assert(info->IntensityBits > 0);
1462 }
1463 }
1464
1465 check_format_to_type_and_comps();
1466 }
1467
1468
1469
1470 /**
1471 * Return datatype and number of components per texel for the given gl_format.
1472 * Only used for mipmap generation code.
1473 */
1474 void
1475 _mesa_format_to_type_and_comps(gl_format format,
1476 GLenum *datatype, GLuint *comps)
1477 {
1478 switch (format) {
1479 case MESA_FORMAT_RGBA8888:
1480 case MESA_FORMAT_RGBA8888_REV:
1481 case MESA_FORMAT_ARGB8888:
1482 case MESA_FORMAT_ARGB8888_REV:
1483 case MESA_FORMAT_XRGB8888:
1484 case MESA_FORMAT_XRGB8888_REV:
1485 *datatype = GL_UNSIGNED_BYTE;
1486 *comps = 4;
1487 return;
1488 case MESA_FORMAT_RGB888:
1489 case MESA_FORMAT_BGR888:
1490 *datatype = GL_UNSIGNED_BYTE;
1491 *comps = 3;
1492 return;
1493 case MESA_FORMAT_RGB565:
1494 case MESA_FORMAT_RGB565_REV:
1495 *datatype = GL_UNSIGNED_SHORT_5_6_5;
1496 *comps = 3;
1497 return;
1498
1499 case MESA_FORMAT_ARGB4444:
1500 case MESA_FORMAT_ARGB4444_REV:
1501 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
1502 *comps = 4;
1503 return;
1504
1505 case MESA_FORMAT_ARGB1555:
1506 case MESA_FORMAT_ARGB1555_REV:
1507 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1508 *comps = 4;
1509 return;
1510
1511 case MESA_FORMAT_ARGB2101010:
1512 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1513 *comps = 4;
1514 return;
1515
1516 case MESA_FORMAT_RGBA5551:
1517 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
1518 *comps = 4;
1519 return;
1520
1521 case MESA_FORMAT_AL44:
1522 *datatype = MESA_UNSIGNED_BYTE_4_4;
1523 *comps = 2;
1524 return;
1525
1526 case MESA_FORMAT_AL88:
1527 case MESA_FORMAT_AL88_REV:
1528 case MESA_FORMAT_RG88:
1529 case MESA_FORMAT_RG88_REV:
1530 *datatype = GL_UNSIGNED_BYTE;
1531 *comps = 2;
1532 return;
1533
1534 case MESA_FORMAT_AL1616:
1535 case MESA_FORMAT_AL1616_REV:
1536 case MESA_FORMAT_RG1616:
1537 case MESA_FORMAT_RG1616_REV:
1538 *datatype = GL_UNSIGNED_SHORT;
1539 *comps = 2;
1540 return;
1541
1542 case MESA_FORMAT_R16:
1543 case MESA_FORMAT_A16:
1544 case MESA_FORMAT_L16:
1545 case MESA_FORMAT_I16:
1546 *datatype = GL_UNSIGNED_SHORT;
1547 *comps = 1;
1548 return;
1549
1550 case MESA_FORMAT_RGB332:
1551 *datatype = GL_UNSIGNED_BYTE_3_3_2;
1552 *comps = 3;
1553 return;
1554
1555 case MESA_FORMAT_A8:
1556 case MESA_FORMAT_L8:
1557 case MESA_FORMAT_I8:
1558 case MESA_FORMAT_CI8:
1559 case MESA_FORMAT_R8:
1560 case MESA_FORMAT_S8:
1561 *datatype = GL_UNSIGNED_BYTE;
1562 *comps = 1;
1563 return;
1564
1565 case MESA_FORMAT_YCBCR:
1566 case MESA_FORMAT_YCBCR_REV:
1567 *datatype = GL_UNSIGNED_SHORT;
1568 *comps = 2;
1569 return;
1570
1571 case MESA_FORMAT_Z24_S8:
1572 *datatype = GL_UNSIGNED_INT;
1573 *comps = 1; /* XXX OK? */
1574 return;
1575
1576 case MESA_FORMAT_S8_Z24:
1577 *datatype = GL_UNSIGNED_INT;
1578 *comps = 1; /* XXX OK? */
1579 return;
1580
1581 case MESA_FORMAT_Z16:
1582 *datatype = GL_UNSIGNED_SHORT;
1583 *comps = 1;
1584 return;
1585
1586 case MESA_FORMAT_X8_Z24:
1587 *datatype = GL_UNSIGNED_INT;
1588 *comps = 1;
1589 return;
1590
1591 case MESA_FORMAT_Z24_X8:
1592 *datatype = GL_UNSIGNED_INT;
1593 *comps = 1;
1594 return;
1595
1596 case MESA_FORMAT_Z32:
1597 *datatype = GL_UNSIGNED_INT;
1598 *comps = 1;
1599 return;
1600
1601 case MESA_FORMAT_DUDV8:
1602 *datatype = GL_BYTE;
1603 *comps = 2;
1604 return;
1605
1606 case MESA_FORMAT_SIGNED_R8:
1607 case MESA_FORMAT_SIGNED_A8:
1608 case MESA_FORMAT_SIGNED_L8:
1609 case MESA_FORMAT_SIGNED_I8:
1610 *datatype = GL_BYTE;
1611 *comps = 1;
1612 return;
1613 case MESA_FORMAT_SIGNED_RG88_REV:
1614 case MESA_FORMAT_SIGNED_AL88:
1615 *datatype = GL_BYTE;
1616 *comps = 2;
1617 return;
1618 case MESA_FORMAT_SIGNED_RGBA8888:
1619 case MESA_FORMAT_SIGNED_RGBA8888_REV:
1620 case MESA_FORMAT_SIGNED_RGBX8888:
1621 *datatype = GL_BYTE;
1622 *comps = 4;
1623 return;
1624
1625 case MESA_FORMAT_RGBA_16:
1626 *datatype = GL_UNSIGNED_SHORT;
1627 *comps = 4;
1628 return;
1629
1630 case MESA_FORMAT_SIGNED_R16:
1631 case MESA_FORMAT_SIGNED_A16:
1632 case MESA_FORMAT_SIGNED_L16:
1633 case MESA_FORMAT_SIGNED_I16:
1634 *datatype = GL_SHORT;
1635 *comps = 1;
1636 return;
1637 case MESA_FORMAT_SIGNED_GR1616:
1638 case MESA_FORMAT_SIGNED_AL1616:
1639 *datatype = GL_SHORT;
1640 *comps = 2;
1641 return;
1642 case MESA_FORMAT_SIGNED_RGB_16:
1643 *datatype = GL_SHORT;
1644 *comps = 3;
1645 return;
1646 case MESA_FORMAT_SIGNED_RGBA_16:
1647 *datatype = GL_SHORT;
1648 *comps = 4;
1649 return;
1650
1651 #if FEATURE_EXT_texture_sRGB
1652 case MESA_FORMAT_SRGB8:
1653 *datatype = GL_UNSIGNED_BYTE;
1654 *comps = 3;
1655 return;
1656 case MESA_FORMAT_SRGBA8:
1657 case MESA_FORMAT_SARGB8:
1658 *datatype = GL_UNSIGNED_BYTE;
1659 *comps = 4;
1660 return;
1661 case MESA_FORMAT_SL8:
1662 *datatype = GL_UNSIGNED_BYTE;
1663 *comps = 1;
1664 return;
1665 case MESA_FORMAT_SLA8:
1666 *datatype = GL_UNSIGNED_BYTE;
1667 *comps = 2;
1668 return;
1669 #endif
1670
1671 #if FEATURE_texture_fxt1
1672 case MESA_FORMAT_RGB_FXT1:
1673 case MESA_FORMAT_RGBA_FXT1:
1674 #endif
1675 #if FEATURE_texture_s3tc
1676 case MESA_FORMAT_RGB_DXT1:
1677 case MESA_FORMAT_RGBA_DXT1:
1678 case MESA_FORMAT_RGBA_DXT3:
1679 case MESA_FORMAT_RGBA_DXT5:
1680 #if FEATURE_EXT_texture_sRGB
1681 case MESA_FORMAT_SRGB_DXT1:
1682 case MESA_FORMAT_SRGBA_DXT1:
1683 case MESA_FORMAT_SRGBA_DXT3:
1684 case MESA_FORMAT_SRGBA_DXT5:
1685 #endif
1686 #endif
1687 case MESA_FORMAT_RED_RGTC1:
1688 case MESA_FORMAT_SIGNED_RED_RGTC1:
1689 case MESA_FORMAT_RG_RGTC2:
1690 case MESA_FORMAT_SIGNED_RG_RGTC2:
1691 case MESA_FORMAT_L_LATC1:
1692 case MESA_FORMAT_SIGNED_L_LATC1:
1693 case MESA_FORMAT_LA_LATC2:
1694 case MESA_FORMAT_SIGNED_LA_LATC2:
1695 /* XXX generate error instead? */
1696 *datatype = GL_UNSIGNED_BYTE;
1697 *comps = 0;
1698 return;
1699
1700 case MESA_FORMAT_RGBA_FLOAT32:
1701 *datatype = GL_FLOAT;
1702 *comps = 4;
1703 return;
1704 case MESA_FORMAT_RGBA_FLOAT16:
1705 *datatype = GL_HALF_FLOAT_ARB;
1706 *comps = 4;
1707 return;
1708 case MESA_FORMAT_RGB_FLOAT32:
1709 *datatype = GL_FLOAT;
1710 *comps = 3;
1711 return;
1712 case MESA_FORMAT_RGB_FLOAT16:
1713 *datatype = GL_HALF_FLOAT_ARB;
1714 *comps = 3;
1715 return;
1716 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32:
1717 *datatype = GL_FLOAT;
1718 *comps = 2;
1719 return;
1720 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16:
1721 *datatype = GL_HALF_FLOAT_ARB;
1722 *comps = 2;
1723 return;
1724 case MESA_FORMAT_ALPHA_FLOAT32:
1725 case MESA_FORMAT_LUMINANCE_FLOAT32:
1726 case MESA_FORMAT_INTENSITY_FLOAT32:
1727 *datatype = GL_FLOAT;
1728 *comps = 1;
1729 return;
1730 case MESA_FORMAT_ALPHA_FLOAT16:
1731 case MESA_FORMAT_LUMINANCE_FLOAT16:
1732 case MESA_FORMAT_INTENSITY_FLOAT16:
1733 *datatype = GL_HALF_FLOAT_ARB;
1734 *comps = 1;
1735 return;
1736
1737 case MESA_FORMAT_RGBA_INT8:
1738 *datatype = GL_BYTE;
1739 *comps = 4;
1740 return;
1741 case MESA_FORMAT_RGBA_INT16:
1742 *datatype = GL_SHORT;
1743 *comps = 4;
1744 return;
1745 case MESA_FORMAT_RGBA_INT32:
1746 *datatype = GL_INT;
1747 *comps = 4;
1748 return;
1749
1750 /**
1751 * \name Non-normalized unsigned integer formats.
1752 */
1753 case MESA_FORMAT_RGBA_UINT8:
1754 *datatype = GL_UNSIGNED_BYTE;
1755 *comps = 4;
1756 return;
1757 case MESA_FORMAT_RGBA_UINT16:
1758 *datatype = GL_UNSIGNED_SHORT;
1759 *comps = 4;
1760 return;
1761 case MESA_FORMAT_RGBA_UINT32:
1762 *datatype = GL_UNSIGNED_INT;
1763 *comps = 4;
1764 return;
1765
1766 case MESA_FORMAT_COUNT:
1767 assert(0);
1768 return;
1769
1770 case MESA_FORMAT_NONE:
1771 /* For debug builds, warn if any formats are not handled */
1772 #ifdef DEBUG
1773 default:
1774 #endif
1775 _mesa_problem(NULL, "bad format %s in _mesa_format_to_type_and_comps",
1776 _mesa_get_format_name(format));
1777 *datatype = 0;
1778 *comps = 1;
1779 }
1780 }