mesa: Fix #ifdef typo in _mesa_format_to_type_and_comps.
[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_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE,
44 * GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA, GL_COLOR_INDEX,
45 * GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
46 */
47 GLenum BaseFormat;
48
49 /**
50 * Logical data type: one of GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALED,
51 * GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
52 */
53 GLenum DataType;
54
55 GLubyte RedBits;
56 GLubyte GreenBits;
57 GLubyte BlueBits;
58 GLubyte AlphaBits;
59 GLubyte LuminanceBits;
60 GLubyte IntensityBits;
61 GLubyte IndexBits;
62 GLubyte DepthBits;
63 GLubyte StencilBits;
64
65 /**
66 * To describe compressed formats. If not compressed, Width=Height=1.
67 */
68 GLubyte BlockWidth, BlockHeight;
69 GLubyte BytesPerBlock;
70 };
71
72
73 /**
74 * Info about each format.
75 * These must be in the same order as the MESA_FORMAT_* enums so that
76 * we can do lookups without searching.
77 */
78 static struct gl_format_info format_info[MESA_FORMAT_COUNT] =
79 {
80 {
81 MESA_FORMAT_NONE, /* Name */
82 "MESA_FORMAT_NONE", /* StrName */
83 GL_NONE, /* BaseFormat */
84 GL_NONE, /* DataType */
85 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
86 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
87 0, 0, 0 /* BlockWidth/Height,Bytes */
88 },
89 {
90 MESA_FORMAT_RGBA8888, /* Name */
91 "MESA_FORMAT_RGBA8888", /* StrName */
92 GL_RGBA, /* BaseFormat */
93 GL_UNSIGNED_NORMALIZED, /* DataType */
94 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
95 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
96 1, 1, 4 /* BlockWidth/Height,Bytes */
97 },
98 {
99 MESA_FORMAT_RGBA8888_REV, /* Name */
100 "MESA_FORMAT_RGBA8888_REV", /* StrName */
101 GL_RGBA, /* BaseFormat */
102 GL_UNSIGNED_NORMALIZED, /* DataType */
103 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
104 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
105 1, 1, 4 /* BlockWidth/Height,Bytes */
106 },
107 {
108 MESA_FORMAT_ARGB8888, /* Name */
109 "MESA_FORMAT_ARGB8888", /* StrName */
110 GL_RGBA, /* BaseFormat */
111 GL_UNSIGNED_NORMALIZED, /* DataType */
112 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
113 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
114 1, 1, 4 /* BlockWidth/Height,Bytes */
115 },
116 {
117 MESA_FORMAT_ARGB8888_REV, /* Name */
118 "MESA_FORMAT_ARGB8888_REV", /* StrName */
119 GL_RGBA, /* BaseFormat */
120 GL_UNSIGNED_NORMALIZED, /* DataType */
121 8, 8, 8, 8, /* Red/Green/Blue/AlphaBits */
122 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
123 1, 1, 4 /* BlockWidth/Height,Bytes */
124 },
125 {
126 MESA_FORMAT_XRGB8888, /* Name */
127 "MESA_FORMAT_XRGB8888", /* StrName */
128 GL_RGB, /* BaseFormat */
129 GL_UNSIGNED_NORMALIZED, /* DataType */
130 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
131 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
132 1, 1, 4 /* BlockWidth/Height,Bytes */
133 },
134 {
135 MESA_FORMAT_XRGB8888_REV, /* Name */
136 "MESA_FORMAT_XRGB8888_REV", /* StrName */
137 GL_RGB, /* BaseFormat */
138 GL_UNSIGNED_NORMALIZED, /* DataType */
139 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
140 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
141 1, 1, 4 /* BlockWidth/Height,Bytes */
142 },
143 {
144 MESA_FORMAT_RGB888, /* Name */
145 "MESA_FORMAT_RGB888", /* StrName */
146 GL_RGB, /* BaseFormat */
147 GL_UNSIGNED_NORMALIZED, /* DataType */
148 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
149 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
150 1, 1, 3 /* BlockWidth/Height,Bytes */
151 },
152 {
153 MESA_FORMAT_BGR888, /* Name */
154 "MESA_FORMAT_BGR888", /* StrName */
155 GL_RGB, /* BaseFormat */
156 GL_UNSIGNED_NORMALIZED, /* DataType */
157 8, 8, 8, 0, /* Red/Green/Blue/AlphaBits */
158 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
159 1, 1, 3 /* BlockWidth/Height,Bytes */
160 },
161 {
162 MESA_FORMAT_RGB565, /* Name */
163 "MESA_FORMAT_RGB565", /* StrName */
164 GL_RGB, /* BaseFormat */
165 GL_UNSIGNED_NORMALIZED, /* DataType */
166 5, 6, 5, 0, /* Red/Green/Blue/AlphaBits */
167 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
168 1, 1, 2 /* BlockWidth/Height,Bytes */
169 },
170 {
171 MESA_FORMAT_RGB565_REV, /* Name */
172 "MESA_FORMAT_RGB565_REV", /* StrName */
173 GL_RGB, /* BaseFormat */
174 GL_UNSIGNED_NORMALIZED, /* DataType */
175 5, 6, 5, 0, /* Red/Green/Blue/AlphaBits */
176 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
177 1, 1, 2 /* BlockWidth/Height,Bytes */
178 },
179 {
180 MESA_FORMAT_ARGB4444, /* Name */
181 "MESA_FORMAT_ARGB4444", /* StrName */
182 GL_RGBA, /* BaseFormat */
183 GL_UNSIGNED_NORMALIZED, /* DataType */
184 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
185 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
186 1, 1, 2 /* BlockWidth/Height,Bytes */
187 },
188 {
189 MESA_FORMAT_ARGB4444_REV, /* Name */
190 "MESA_FORMAT_ARGB4444_REV", /* StrName */
191 GL_RGBA, /* BaseFormat */
192 GL_UNSIGNED_NORMALIZED, /* DataType */
193 4, 4, 4, 4, /* Red/Green/Blue/AlphaBits */
194 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
195 1, 1, 2 /* BlockWidth/Height,Bytes */
196 },
197 {
198 MESA_FORMAT_RGBA5551, /* Name */
199 "MESA_FORMAT_RGBA5551", /* StrName */
200 GL_RGBA, /* BaseFormat */
201 GL_UNSIGNED_NORMALIZED, /* DataType */
202 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
203 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
204 1, 1, 2 /* BlockWidth/Height,Bytes */
205 },
206 {
207 MESA_FORMAT_ARGB1555, /* Name */
208 "MESA_FORMAT_ARGB1555", /* StrName */
209 GL_RGBA, /* BaseFormat */
210 GL_UNSIGNED_NORMALIZED, /* DataType */
211 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
212 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
213 1, 1, 2 /* BlockWidth/Height,Bytes */
214 },
215 {
216 MESA_FORMAT_ARGB1555_REV, /* Name */
217 "MESA_FORMAT_ARGB1555_REV", /* StrName */
218 GL_RGBA, /* BaseFormat */
219 GL_UNSIGNED_NORMALIZED, /* DataType */
220 5, 5, 5, 1, /* Red/Green/Blue/AlphaBits */
221 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
222 1, 1, 2 /* BlockWidth/Height,Bytes */
223 },
224 {
225 MESA_FORMAT_AL44, /* Name */
226 "MESA_FORMAT_AL44", /* StrName */
227 GL_LUMINANCE_ALPHA, /* BaseFormat */
228 GL_UNSIGNED_NORMALIZED, /* DataType */
229 0, 0, 0, 4, /* Red/Green/Blue/AlphaBits */
230 4, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
231 1, 1, 1 /* BlockWidth/Height,Bytes */
232 },
233 {
234 MESA_FORMAT_AL88, /* Name */
235 "MESA_FORMAT_AL88", /* StrName */
236 GL_LUMINANCE_ALPHA, /* BaseFormat */
237 GL_UNSIGNED_NORMALIZED, /* DataType */
238 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
239 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
240 1, 1, 2 /* BlockWidth/Height,Bytes */
241 },
242 {
243 MESA_FORMAT_AL88_REV, /* Name */
244 "MESA_FORMAT_AL88_REV", /* StrName */
245 GL_LUMINANCE_ALPHA, /* BaseFormat */
246 GL_UNSIGNED_NORMALIZED, /* DataType */
247 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
248 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
249 1, 1, 2 /* BlockWidth/Height,Bytes */
250 },
251 {
252 MESA_FORMAT_AL1616, /* Name */
253 "MESA_FORMAT_AL1616", /* StrName */
254 GL_LUMINANCE_ALPHA, /* BaseFormat */
255 GL_UNSIGNED_NORMALIZED, /* DataType */
256 0, 0, 0, 16, /* Red/Green/Blue/AlphaBits */
257 16, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
258 1, 1, 4 /* BlockWidth/Height,Bytes */
259 },
260 {
261 MESA_FORMAT_AL1616_REV, /* Name */
262 "MESA_FORMAT_AL1616_REV", /* StrName */
263 GL_LUMINANCE_ALPHA, /* BaseFormat */
264 GL_UNSIGNED_NORMALIZED, /* DataType */
265 0, 0, 0, 16, /* Red/Green/Blue/AlphaBits */
266 16, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
267 1, 1, 4 /* BlockWidth/Height,Bytes */
268 },
269 {
270 MESA_FORMAT_RGB332, /* Name */
271 "MESA_FORMAT_RGB332", /* StrName */
272 GL_RGB, /* BaseFormat */
273 GL_UNSIGNED_NORMALIZED, /* DataType */
274 3, 3, 2, 0, /* Red/Green/Blue/AlphaBits */
275 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
276 1, 1, 1 /* BlockWidth/Height,Bytes */
277 },
278 {
279 MESA_FORMAT_A8, /* Name */
280 "MESA_FORMAT_A8", /* StrName */
281 GL_ALPHA, /* BaseFormat */
282 GL_UNSIGNED_NORMALIZED, /* DataType */
283 0, 0, 0, 8, /* Red/Green/Blue/AlphaBits */
284 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
285 1, 1, 1 /* BlockWidth/Height,Bytes */
286 },
287 {
288 MESA_FORMAT_A16, /* Name */
289 "MESA_FORMAT_A16", /* StrName */
290 GL_ALPHA, /* BaseFormat */
291 GL_UNSIGNED_NORMALIZED, /* DataType */
292 0, 0, 0, 16, /* Red/Green/Blue/AlphaBits */
293 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
294 1, 1, 2 /* BlockWidth/Height,Bytes */
295 },
296 {
297 MESA_FORMAT_L8, /* Name */
298 "MESA_FORMAT_L8", /* StrName */
299 GL_LUMINANCE, /* BaseFormat */
300 GL_UNSIGNED_NORMALIZED, /* DataType */
301 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
302 8, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
303 1, 1, 1 /* BlockWidth/Height,Bytes */
304 },
305 {
306 MESA_FORMAT_L16, /* Name */
307 "MESA_FORMAT_L16", /* StrName */
308 GL_LUMINANCE, /* BaseFormat */
309 GL_UNSIGNED_NORMALIZED, /* DataType */
310 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
311 16, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
312 1, 1, 2 /* BlockWidth/Height,Bytes */
313 },
314 {
315 MESA_FORMAT_I8, /* Name */
316 "MESA_FORMAT_I8", /* StrName */
317 GL_INTENSITY, /* BaseFormat */
318 GL_UNSIGNED_NORMALIZED, /* DataType */
319 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
320 0, 8, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
321 1, 1, 1 /* BlockWidth/Height,Bytes */
322 },
323 {
324 MESA_FORMAT_I16, /* Name */
325 "MESA_FORMAT_I16", /* StrName */
326 GL_INTENSITY, /* BaseFormat */
327 GL_UNSIGNED_NORMALIZED, /* DataType */
328 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
329 0, 16, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
330 1, 1, 2 /* BlockWidth/Height,Bytes */
331 },
332 {
333 MESA_FORMAT_CI8, /* Name */
334 "MESA_FORMAT_CI8", /* StrName */
335 GL_COLOR_INDEX, /* BaseFormat */
336 GL_UNSIGNED_INT, /* DataType */
337 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
338 0, 0, 8, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
339 1, 1, 1 /* BlockWidth/Height,Bytes */
340 },
341 {
342 MESA_FORMAT_YCBCR, /* Name */
343 "MESA_FORMAT_YCBCR", /* StrName */
344 GL_YCBCR_MESA, /* BaseFormat */
345 GL_UNSIGNED_NORMALIZED, /* DataType */
346 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
347 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
348 1, 1, 2 /* BlockWidth/Height,Bytes */
349 },
350 {
351 MESA_FORMAT_YCBCR_REV, /* Name */
352 "MESA_FORMAT_YCBCR_REV", /* StrName */
353 GL_YCBCR_MESA, /* BaseFormat */
354 GL_UNSIGNED_NORMALIZED, /* DataType */
355 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
356 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
357 1, 1, 2 /* BlockWidth/Height,Bytes */
358 },
359 {
360 MESA_FORMAT_R8,
361 "MESA_FORMAT_R8",
362 GL_RED,
363 GL_UNSIGNED_NORMALIZED,
364 8, 0, 0, 0,
365 0, 0, 0, 0, 0,
366 1, 1, 1
367 },
368 {
369 MESA_FORMAT_RG88,
370 "MESA_FORMAT_RG88",
371 GL_RG,
372 GL_UNSIGNED_NORMALIZED,
373 8, 8, 0, 0,
374 0, 0, 0, 0, 0,
375 1, 1, 2
376 },
377 {
378 MESA_FORMAT_RG88_REV,
379 "MESA_FORMAT_RG88_REV",
380 GL_RG,
381 GL_UNSIGNED_NORMALIZED,
382 8, 8, 0, 0,
383 0, 0, 0, 0, 0,
384 1, 1, 2
385 },
386 {
387 MESA_FORMAT_R16,
388 "MESA_FORMAT_R16",
389 GL_RED,
390 GL_UNSIGNED_NORMALIZED,
391 16, 0, 0, 0,
392 0, 0, 0, 0, 0,
393 1, 1, 2
394 },
395 {
396 MESA_FORMAT_RG1616,
397 "MESA_FORMAT_RG1616",
398 GL_RG,
399 GL_UNSIGNED_NORMALIZED,
400 16, 16, 0, 0,
401 0, 0, 0, 0, 0,
402 1, 1, 4
403 },
404 {
405 MESA_FORMAT_RG1616_REV,
406 "MESA_FORMAT_RG1616_REV",
407 GL_RG,
408 GL_UNSIGNED_NORMALIZED,
409 16, 16, 0, 0,
410 0, 0, 0, 0, 0,
411 1, 1, 4
412 },
413 {
414 MESA_FORMAT_ARGB2101010,
415 "MESA_FORMAT_ARGB2101010",
416 GL_RGBA,
417 GL_UNSIGNED_NORMALIZED,
418 10, 10, 10, 2,
419 0, 0, 0, 0, 0,
420 1, 1, 4
421 },
422 {
423 MESA_FORMAT_Z24_S8, /* Name */
424 "MESA_FORMAT_Z24_S8", /* StrName */
425 GL_DEPTH_STENCIL, /* BaseFormat */
426 GL_UNSIGNED_INT, /* DataType */
427 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
428 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
429 1, 1, 4 /* BlockWidth/Height,Bytes */
430 },
431 {
432 MESA_FORMAT_S8_Z24, /* Name */
433 "MESA_FORMAT_S8_Z24", /* StrName */
434 GL_DEPTH_STENCIL, /* BaseFormat */
435 GL_UNSIGNED_INT, /* DataType */
436 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
437 0, 0, 0, 24, 8, /* Lum/Int/Index/Depth/StencilBits */
438 1, 1, 4 /* BlockWidth/Height,Bytes */
439 },
440 {
441 MESA_FORMAT_Z16, /* Name */
442 "MESA_FORMAT_Z16", /* StrName */
443 GL_DEPTH_COMPONENT, /* BaseFormat */
444 GL_UNSIGNED_INT, /* DataType */
445 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
446 0, 0, 0, 16, 0, /* Lum/Int/Index/Depth/StencilBits */
447 1, 1, 2 /* BlockWidth/Height,Bytes */
448 },
449 {
450 MESA_FORMAT_X8_Z24, /* Name */
451 "MESA_FORMAT_X8_Z24", /* StrName */
452 GL_DEPTH_COMPONENT, /* BaseFormat */
453 GL_UNSIGNED_INT, /* DataType */
454 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
455 0, 0, 0, 24, 0, /* Lum/Int/Index/Depth/StencilBits */
456 1, 1, 4 /* BlockWidth/Height,Bytes */
457 },
458 {
459 MESA_FORMAT_Z24_X8, /* Name */
460 "MESA_FORMAT_Z24_X8", /* StrName */
461 GL_DEPTH_COMPONENT, /* BaseFormat */
462 GL_UNSIGNED_INT, /* DataType */
463 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
464 0, 0, 0, 24, 0, /* Lum/Int/Index/Depth/StencilBits */
465 1, 1, 4 /* BlockWidth/Height,Bytes */
466 },
467 {
468 MESA_FORMAT_Z32, /* Name */
469 "MESA_FORMAT_Z32", /* StrName */
470 GL_DEPTH_COMPONENT, /* BaseFormat */
471 GL_UNSIGNED_INT, /* DataType */
472 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
473 0, 0, 0, 32, 0, /* Lum/Int/Index/Depth/StencilBits */
474 1, 1, 4 /* BlockWidth/Height,Bytes */
475 },
476 {
477 MESA_FORMAT_S8, /* Name */
478 "MESA_FORMAT_S8", /* StrName */
479 GL_STENCIL_INDEX, /* BaseFormat */
480 GL_UNSIGNED_INT, /* DataType */
481 0, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
482 0, 0, 0, 0, 8, /* Lum/Int/Index/Depth/StencilBits */
483 1, 1, 1 /* BlockWidth/Height,Bytes */
484 },
485 {
486 MESA_FORMAT_SRGB8,
487 "MESA_FORMAT_SRGB8",
488 GL_RGB,
489 GL_UNSIGNED_NORMALIZED,
490 8, 8, 8, 0,
491 0, 0, 0, 0, 0,
492 1, 1, 3
493 },
494 {
495 MESA_FORMAT_SRGBA8,
496 "MESA_FORMAT_SRGBA8",
497 GL_RGBA,
498 GL_UNSIGNED_NORMALIZED,
499 8, 8, 8, 8,
500 0, 0, 0, 0, 0,
501 1, 1, 4
502 },
503 {
504 MESA_FORMAT_SARGB8,
505 "MESA_FORMAT_SARGB8",
506 GL_RGBA,
507 GL_UNSIGNED_NORMALIZED,
508 8, 8, 8, 8,
509 0, 0, 0, 0, 0,
510 1, 1, 4
511 },
512 {
513 MESA_FORMAT_SL8,
514 "MESA_FORMAT_SL8",
515 GL_LUMINANCE,
516 GL_UNSIGNED_NORMALIZED,
517 0, 0, 0, 0,
518 8, 0, 0, 0, 0,
519 1, 1, 1
520 },
521 {
522 MESA_FORMAT_SLA8,
523 "MESA_FORMAT_SLA8",
524 GL_LUMINANCE_ALPHA,
525 GL_UNSIGNED_NORMALIZED,
526 0, 0, 0, 8,
527 8, 0, 0, 0, 0,
528 1, 1, 2
529 },
530 {
531 MESA_FORMAT_SRGB_DXT1, /* Name */
532 "MESA_FORMAT_SRGB_DXT1", /* StrName */
533 GL_RGB, /* BaseFormat */
534 GL_UNSIGNED_NORMALIZED, /* DataType */
535 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
536 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
537 4, 4, 8 /* 8 bytes per 4x4 block */
538 },
539 {
540 MESA_FORMAT_SRGBA_DXT1,
541 "MESA_FORMAT_SRGBA_DXT1",
542 GL_RGBA,
543 GL_UNSIGNED_NORMALIZED,
544 4, 4, 4, 4,
545 0, 0, 0, 0, 0,
546 4, 4, 8 /* 8 bytes per 4x4 block */
547 },
548 {
549 MESA_FORMAT_SRGBA_DXT3,
550 "MESA_FORMAT_SRGBA_DXT3",
551 GL_RGBA,
552 GL_UNSIGNED_NORMALIZED,
553 4, 4, 4, 4,
554 0, 0, 0, 0, 0,
555 4, 4, 16 /* 16 bytes per 4x4 block */
556 },
557 {
558 MESA_FORMAT_SRGBA_DXT5,
559 "MESA_FORMAT_SRGBA_DXT5",
560 GL_RGBA,
561 GL_UNSIGNED_NORMALIZED,
562 4, 4, 4, 4,
563 0, 0, 0, 0, 0,
564 4, 4, 16 /* 16 bytes per 4x4 block */
565 },
566
567 {
568 MESA_FORMAT_RGB_FXT1,
569 "MESA_FORMAT_RGB_FXT1",
570 GL_RGB,
571 GL_UNSIGNED_NORMALIZED,
572 4, 4, 4, 0, /* approx Red/Green/BlueBits */
573 0, 0, 0, 0, 0,
574 8, 4, 16 /* 16 bytes per 8x4 block */
575 },
576 {
577 MESA_FORMAT_RGBA_FXT1,
578 "MESA_FORMAT_RGBA_FXT1",
579 GL_RGBA,
580 GL_UNSIGNED_NORMALIZED,
581 4, 4, 4, 1, /* approx Red/Green/Blue/AlphaBits */
582 0, 0, 0, 0, 0,
583 8, 4, 16 /* 16 bytes per 8x4 block */
584 },
585
586 {
587 MESA_FORMAT_RGB_DXT1, /* Name */
588 "MESA_FORMAT_RGB_DXT1", /* StrName */
589 GL_RGB, /* BaseFormat */
590 GL_UNSIGNED_NORMALIZED, /* DataType */
591 4, 4, 4, 0, /* approx Red/Green/Blue/AlphaBits */
592 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
593 4, 4, 8 /* 8 bytes per 4x4 block */
594 },
595 {
596 MESA_FORMAT_RGBA_DXT1,
597 "MESA_FORMAT_RGBA_DXT1",
598 GL_RGBA,
599 GL_UNSIGNED_NORMALIZED,
600 4, 4, 4, 4,
601 0, 0, 0, 0, 0,
602 4, 4, 8 /* 8 bytes per 4x4 block */
603 },
604 {
605 MESA_FORMAT_RGBA_DXT3,
606 "MESA_FORMAT_RGBA_DXT3",
607 GL_RGBA,
608 GL_UNSIGNED_NORMALIZED,
609 4, 4, 4, 4,
610 0, 0, 0, 0, 0,
611 4, 4, 16 /* 16 bytes per 4x4 block */
612 },
613 {
614 MESA_FORMAT_RGBA_DXT5,
615 "MESA_FORMAT_RGBA_DXT5",
616 GL_RGBA,
617 GL_UNSIGNED_NORMALIZED,
618 4, 4, 4, 4,
619 0, 0, 0, 0, 0,
620 4, 4, 16 /* 16 bytes per 4x4 block */
621 },
622 {
623 MESA_FORMAT_RGBA_FLOAT32,
624 "MESA_FORMAT_RGBA_FLOAT32",
625 GL_RGBA,
626 GL_FLOAT,
627 32, 32, 32, 32,
628 0, 0, 0, 0, 0,
629 1, 1, 16
630 },
631 {
632 MESA_FORMAT_RGBA_FLOAT16,
633 "MESA_FORMAT_RGBA_FLOAT16",
634 GL_RGBA,
635 GL_FLOAT,
636 16, 16, 16, 16,
637 0, 0, 0, 0, 0,
638 1, 1, 8
639 },
640 {
641 MESA_FORMAT_RGB_FLOAT32,
642 "MESA_FORMAT_RGB_FLOAT32",
643 GL_RGB,
644 GL_FLOAT,
645 32, 32, 32, 0,
646 0, 0, 0, 0, 0,
647 1, 1, 12
648 },
649 {
650 MESA_FORMAT_RGB_FLOAT16,
651 "MESA_FORMAT_RGB_FLOAT16",
652 GL_RGB,
653 GL_FLOAT,
654 16, 16, 16, 0,
655 0, 0, 0, 0, 0,
656 1, 1, 6
657 },
658 {
659 MESA_FORMAT_ALPHA_FLOAT32,
660 "MESA_FORMAT_ALPHA_FLOAT32",
661 GL_ALPHA,
662 GL_FLOAT,
663 0, 0, 0, 32,
664 0, 0, 0, 0, 0,
665 1, 1, 4
666 },
667 {
668 MESA_FORMAT_ALPHA_FLOAT16,
669 "MESA_FORMAT_ALPHA_FLOAT16",
670 GL_ALPHA,
671 GL_FLOAT,
672 0, 0, 0, 16,
673 0, 0, 0, 0, 0,
674 1, 1, 2
675 },
676 {
677 MESA_FORMAT_LUMINANCE_FLOAT32,
678 "MESA_FORMAT_LUMINANCE_FLOAT32",
679 GL_ALPHA,
680 GL_FLOAT,
681 0, 0, 0, 0,
682 32, 0, 0, 0, 0,
683 1, 1, 4
684 },
685 {
686 MESA_FORMAT_LUMINANCE_FLOAT16,
687 "MESA_FORMAT_LUMINANCE_FLOAT16",
688 GL_ALPHA,
689 GL_FLOAT,
690 0, 0, 0, 0,
691 16, 0, 0, 0, 0,
692 1, 1, 2
693 },
694 {
695 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32,
696 "MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32",
697 GL_LUMINANCE_ALPHA,
698 GL_FLOAT,
699 0, 0, 0, 32,
700 32, 0, 0, 0, 0,
701 1, 1, 8
702 },
703 {
704 MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16,
705 "MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16",
706 GL_LUMINANCE_ALPHA,
707 GL_FLOAT,
708 0, 0, 0, 16,
709 16, 0, 0, 0, 0,
710 1, 1, 4
711 },
712 {
713 MESA_FORMAT_INTENSITY_FLOAT32,
714 "MESA_FORMAT_INTENSITY_FLOAT32",
715 GL_INTENSITY,
716 GL_FLOAT,
717 0, 0, 0, 0,
718 0, 32, 0, 0, 0,
719 1, 1, 4
720 },
721 {
722 MESA_FORMAT_INTENSITY_FLOAT16,
723 "MESA_FORMAT_INTENSITY_FLOAT16",
724 GL_INTENSITY,
725 GL_FLOAT,
726 0, 0, 0, 0,
727 0, 16, 0, 0, 0,
728 1, 1, 2
729 },
730
731 /* unnormalized signed int formats */
732 {
733 MESA_FORMAT_RGBA_INT8,
734 "MESA_FORMAT_RGBA_INT8",
735 GL_RGBA,
736 GL_INT,
737 8, 8, 8, 8,
738 0, 0, 0, 0, 0,
739 1, 1, 4
740 },
741 {
742 MESA_FORMAT_RGBA_INT16,
743 "MESA_FORMAT_RGBA_INT16",
744 GL_RGBA,
745 GL_INT,
746 16, 16, 16, 16,
747 0, 0, 0, 0, 0,
748 1, 1, 8
749 },
750 {
751 MESA_FORMAT_RGBA_INT32,
752 "MESA_FORMAT_RGBA_INT32",
753 GL_RGBA,
754 GL_INT,
755 32, 32, 32, 32,
756 0, 0, 0, 0, 0,
757 1, 1, 16
758 },
759
760 /* unnormalized unsigned int formats */
761 {
762 MESA_FORMAT_RGBA_UINT8,
763 "MESA_FORMAT_RGBA_UINT8",
764 GL_RGBA,
765 GL_UNSIGNED_INT,
766 8, 8, 8, 8,
767 0, 0, 0, 0, 0,
768 1, 1, 4
769 },
770 {
771 MESA_FORMAT_RGBA_UINT16,
772 "MESA_FORMAT_RGBA_UINT16",
773 GL_RGBA,
774 GL_UNSIGNED_INT,
775 16, 16, 16, 16,
776 0, 0, 0, 0, 0,
777 1, 1, 8
778 },
779 {
780 MESA_FORMAT_RGBA_UINT32,
781 "MESA_FORMAT_RGBA_UINT32",
782 GL_RGBA,
783 GL_UNSIGNED_INT,
784 32, 32, 32, 32,
785 0, 0, 0, 0, 0,
786 1, 1, 16
787 },
788
789
790 {
791 MESA_FORMAT_DUDV8,
792 "MESA_FORMAT_DUDV8",
793 GL_DUDV_ATI,
794 GL_SIGNED_NORMALIZED,
795 0, 0, 0, 0,
796 0, 0, 0, 0, 0,
797 1, 1, 2
798 },
799
800 /* Signed 8 bits / channel */
801 {
802 MESA_FORMAT_SIGNED_R8, /* Name */
803 "MESA_FORMAT_SIGNED_R8", /* StrName */
804 GL_RGBA, /* BaseFormat */
805 GL_SIGNED_NORMALIZED, /* DataType */
806 8, 0, 0, 0, /* Red/Green/Blue/AlphaBits */
807 0, 0, 0, 0, 0, /* Lum/Int/Index/Depth/StencilBits */
808 1, 1, 1 /* BlockWidth/Height,Bytes */
809 },
810 {
811 MESA_FORMAT_SIGNED_RG88,
812 "MESA_FORMAT_SIGNED_RG88",
813 GL_RGBA,
814 GL_SIGNED_NORMALIZED,
815 8, 8, 0, 0,
816 0, 0, 0, 0, 0,
817 1, 1, 2
818 },
819 {
820 MESA_FORMAT_SIGNED_RGBX8888,
821 "MESA_FORMAT_SIGNED_RGBX8888",
822 GL_RGBA,
823 GL_SIGNED_NORMALIZED,
824 8, 8, 8, 0,
825 0, 0, 0, 0, 0,
826 1, 1, 4 /* 4 bpp, but no alpha */
827 },
828 {
829 MESA_FORMAT_SIGNED_RGBA8888,
830 "MESA_FORMAT_SIGNED_RGBA8888",
831 GL_RGBA,
832 GL_SIGNED_NORMALIZED,
833 8, 8, 8, 8,
834 0, 0, 0, 0, 0,
835 1, 1, 4
836 },
837 {
838 MESA_FORMAT_SIGNED_RGBA8888_REV,
839 "MESA_FORMAT_SIGNED_RGBA8888_REV",
840 GL_RGBA,
841 GL_SIGNED_NORMALIZED,
842 8, 8, 8, 8,
843 0, 0, 0, 0, 0,
844 1, 1, 4
845 },
846
847 /* Signed 16 bits / channel */
848 {
849 MESA_FORMAT_SIGNED_R_16,
850 "MESA_FORMAT_SIGNED_R_16",
851 GL_RGBA,
852 GL_SIGNED_NORMALIZED,
853 16, 0, 0, 0,
854 0, 0, 0, 0, 0,
855 1, 1, 2
856 },
857 {
858 MESA_FORMAT_SIGNED_RG_16,
859 "MESA_FORMAT_SIGNED_RG_16",
860 GL_RGBA,
861 GL_SIGNED_NORMALIZED,
862 16, 16, 0, 0,
863 0, 0, 0, 0, 0,
864 1, 1, 4
865 },
866 {
867 MESA_FORMAT_SIGNED_RGB_16,
868 "MESA_FORMAT_SIGNED_RGB_16",
869 GL_RGBA,
870 GL_SIGNED_NORMALIZED,
871 16, 16, 16, 0,
872 0, 0, 0, 0, 0,
873 1, 1, 6
874 },
875 {
876 MESA_FORMAT_SIGNED_RGBA_16,
877 "MESA_FORMAT_SIGNED_RGBA_16",
878 GL_RGBA,
879 GL_SIGNED_NORMALIZED,
880 16, 16, 16, 16,
881 0, 0, 0, 0, 0,
882 1, 1, 8
883 },
884 {
885 MESA_FORMAT_RGBA_16,
886 "MESA_FORMAT_RGBA_16",
887 GL_RGBA,
888 GL_UNSIGNED_NORMALIZED,
889 16, 16, 16, 16,
890 0, 0, 0, 0, 0,
891 1, 1, 8
892 }
893 };
894
895
896
897 static const struct gl_format_info *
898 _mesa_get_format_info(gl_format format)
899 {
900 const struct gl_format_info *info = &format_info[format];
901 assert(info->Name == format);
902 return info;
903 }
904
905
906 /** Return string name of format (for debugging) */
907 const char *
908 _mesa_get_format_name(gl_format format)
909 {
910 const struct gl_format_info *info = _mesa_get_format_info(format);
911 return info->StrName;
912 }
913
914
915
916 /**
917 * Return bytes needed to store a block of pixels in the given format.
918 * Normally, a block is 1x1 (a single pixel). But for compressed formats
919 * a block may be 4x4 or 8x4, etc.
920 */
921 GLuint
922 _mesa_get_format_bytes(gl_format format)
923 {
924 const struct gl_format_info *info = _mesa_get_format_info(format);
925 ASSERT(info->BytesPerBlock);
926 return info->BytesPerBlock;
927 }
928
929
930 /**
931 * Return bits per component for the given format.
932 * \param format one of MESA_FORMAT_x
933 * \param pname the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
934 */
935 GLint
936 _mesa_get_format_bits(gl_format format, GLenum pname)
937 {
938 const struct gl_format_info *info = _mesa_get_format_info(format);
939
940 switch (pname) {
941 case GL_RED_BITS:
942 case GL_TEXTURE_RED_SIZE:
943 case GL_RENDERBUFFER_RED_SIZE_EXT:
944 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
945 return info->RedBits;
946 case GL_GREEN_BITS:
947 case GL_TEXTURE_GREEN_SIZE:
948 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
949 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
950 return info->GreenBits;
951 case GL_BLUE_BITS:
952 case GL_TEXTURE_BLUE_SIZE:
953 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
954 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
955 return info->BlueBits;
956 case GL_ALPHA_BITS:
957 case GL_TEXTURE_ALPHA_SIZE:
958 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
959 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
960 return info->AlphaBits;
961 case GL_TEXTURE_INTENSITY_SIZE:
962 return info->IntensityBits;
963 case GL_TEXTURE_LUMINANCE_SIZE:
964 return info->LuminanceBits;
965 case GL_INDEX_BITS:
966 case GL_TEXTURE_INDEX_SIZE_EXT:
967 return info->IndexBits;
968 case GL_DEPTH_BITS:
969 case GL_TEXTURE_DEPTH_SIZE_ARB:
970 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
971 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
972 return info->DepthBits;
973 case GL_STENCIL_BITS:
974 case GL_TEXTURE_STENCIL_SIZE_EXT:
975 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
976 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
977 return info->StencilBits;
978 default:
979 _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
980 return 0;
981 }
982 }
983
984
985 /**
986 * Return the data type (or more specifically, the data representation)
987 * for the given format.
988 * The return value will be one of:
989 * GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
990 * GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
991 * GL_UNSIGNED_INT = an ordinary unsigned integer
992 * GL_INT = an ordinary signed integer
993 * GL_FLOAT = an ordinary float
994 */
995 GLenum
996 _mesa_get_format_datatype(gl_format format)
997 {
998 const struct gl_format_info *info = _mesa_get_format_info(format);
999 return info->DataType;
1000 }
1001
1002
1003 /**
1004 * Return the basic format for the given type. The result will be
1005 * one of GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA,
1006 * GL_INTENSITY, GL_YCBCR_MESA, GL_COLOR_INDEX, GL_DEPTH_COMPONENT,
1007 * GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
1008 */
1009 GLenum
1010 _mesa_get_format_base_format(gl_format format)
1011 {
1012 const struct gl_format_info *info = _mesa_get_format_info(format);
1013 return info->BaseFormat;
1014 }
1015
1016
1017 /**
1018 * Return the block size (in pixels) for the given format. Normally
1019 * the block size is 1x1. But compressed formats will have block sizes
1020 * of 4x4 or 8x4 pixels, etc.
1021 * \param bw returns block width in pixels
1022 * \param bh returns block height in pixels
1023 */
1024 void
1025 _mesa_get_format_block_size(gl_format format, GLuint *bw, GLuint *bh)
1026 {
1027 const struct gl_format_info *info = _mesa_get_format_info(format);
1028 *bw = info->BlockWidth;
1029 *bh = info->BlockHeight;
1030 }
1031
1032
1033 /** Is the given format a compressed format? */
1034 GLboolean
1035 _mesa_is_format_compressed(gl_format format)
1036 {
1037 const struct gl_format_info *info = _mesa_get_format_info(format);
1038 return info->BlockWidth > 1 || info->BlockHeight > 1;
1039 }
1040
1041
1042 /**
1043 * Determine if the given format represents a packed depth/stencil buffer.
1044 */
1045 GLboolean
1046 _mesa_is_format_packed_depth_stencil(gl_format format)
1047 {
1048 const struct gl_format_info *info = _mesa_get_format_info(format);
1049
1050 return info->BaseFormat == GL_DEPTH_STENCIL;
1051 }
1052
1053
1054 /**
1055 * Is the given format a signed/unsigned integer color format?
1056 */
1057 GLboolean
1058 _mesa_is_format_integer_color(gl_format format)
1059 {
1060 const struct gl_format_info *info = _mesa_get_format_info(format);
1061 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
1062 info->BaseFormat != GL_DEPTH_COMPONENT &&
1063 info->BaseFormat != GL_DEPTH_STENCIL &&
1064 info->BaseFormat != GL_STENCIL_INDEX;
1065 }
1066
1067
1068 /**
1069 * Return color encoding for given format.
1070 * \return GL_LINEAR or GL_SRGB
1071 */
1072 GLenum
1073 _mesa_get_format_color_encoding(gl_format format)
1074 {
1075 /* XXX this info should be encoded in gl_format_info */
1076 switch (format) {
1077 case MESA_FORMAT_SRGB8:
1078 case MESA_FORMAT_SRGBA8:
1079 case MESA_FORMAT_SARGB8:
1080 case MESA_FORMAT_SL8:
1081 case MESA_FORMAT_SLA8:
1082 case MESA_FORMAT_SRGB_DXT1:
1083 case MESA_FORMAT_SRGBA_DXT1:
1084 case MESA_FORMAT_SRGBA_DXT3:
1085 case MESA_FORMAT_SRGBA_DXT5:
1086 return GL_SRGB;
1087 default:
1088 return GL_LINEAR;
1089 }
1090 }
1091
1092
1093 /**
1094 * Return number of bytes needed to store an image of the given size
1095 * in the given format.
1096 */
1097 GLuint
1098 _mesa_format_image_size(gl_format format, GLsizei width,
1099 GLsizei height, GLsizei depth)
1100 {
1101 const struct gl_format_info *info = _mesa_get_format_info(format);
1102 /* Strictly speaking, a conditional isn't needed here */
1103 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
1104 /* compressed format (2D only for now) */
1105 const GLuint bw = info->BlockWidth, bh = info->BlockHeight;
1106 const GLuint wblocks = (width + bw - 1) / bw;
1107 const GLuint hblocks = (height + bh - 1) / bh;
1108 const GLuint sz = wblocks * hblocks * info->BytesPerBlock;
1109 assert(depth == 1);
1110 return sz;
1111 }
1112 else {
1113 /* non-compressed */
1114 const GLuint sz = width * height * depth * info->BytesPerBlock;
1115 return sz;
1116 }
1117 }
1118
1119
1120 /**
1121 * Same as _mesa_format_image_size() but returns a 64-bit value to
1122 * accomodate very large textures.
1123 */
1124 uint64_t
1125 _mesa_format_image_size64(gl_format format, GLsizei width,
1126 GLsizei height, GLsizei depth)
1127 {
1128 const struct gl_format_info *info = _mesa_get_format_info(format);
1129 /* Strictly speaking, a conditional isn't needed here */
1130 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
1131 /* compressed format (2D only for now) */
1132 const uint64_t bw = info->BlockWidth, bh = info->BlockHeight;
1133 const uint64_t wblocks = (width + bw - 1) / bw;
1134 const uint64_t hblocks = (height + bh - 1) / bh;
1135 const uint64_t sz = wblocks * hblocks * info->BytesPerBlock;
1136 assert(depth == 1);
1137 return sz;
1138 }
1139 else {
1140 /* non-compressed */
1141 const uint64_t sz = ((uint64_t) width *
1142 (uint64_t) height *
1143 (uint64_t) depth *
1144 info->BytesPerBlock);
1145 return sz;
1146 }
1147 }
1148
1149
1150
1151 GLint
1152 _mesa_format_row_stride(gl_format format, GLsizei width)
1153 {
1154 const struct gl_format_info *info = _mesa_get_format_info(format);
1155 /* Strictly speaking, a conditional isn't needed here */
1156 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
1157 /* compressed format */
1158 const GLuint bw = info->BlockWidth;
1159 const GLuint wblocks = (width + bw - 1) / bw;
1160 const GLint stride = wblocks * info->BytesPerBlock;
1161 return stride;
1162 }
1163 else {
1164 const GLint stride = width * info->BytesPerBlock;
1165 return stride;
1166 }
1167 }
1168
1169
1170
1171 /**
1172 * Do sanity checking of the format info table.
1173 */
1174 void
1175 _mesa_test_formats(void)
1176 {
1177 GLuint i;
1178
1179 assert(Elements(format_info) == MESA_FORMAT_COUNT);
1180
1181 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
1182 const struct gl_format_info *info = _mesa_get_format_info(i);
1183 assert(info);
1184
1185 assert(info->Name == i);
1186
1187 if (info->Name == MESA_FORMAT_NONE)
1188 continue;
1189
1190 if (info->BlockWidth == 1 && info->BlockHeight == 1) {
1191 if (info->RedBits > 0) {
1192 GLuint t = info->RedBits + info->GreenBits
1193 + info->BlueBits + info->AlphaBits;
1194 assert(t / 8 == info->BytesPerBlock);
1195 (void) t;
1196 }
1197 }
1198
1199 assert(info->DataType == GL_UNSIGNED_NORMALIZED ||
1200 info->DataType == GL_SIGNED_NORMALIZED ||
1201 info->DataType == GL_UNSIGNED_INT ||
1202 info->DataType == GL_FLOAT);
1203
1204 if (info->BaseFormat == GL_RGB) {
1205 assert(info->RedBits > 0);
1206 assert(info->GreenBits > 0);
1207 assert(info->BlueBits > 0);
1208 assert(info->AlphaBits == 0);
1209 assert(info->LuminanceBits == 0);
1210 assert(info->IntensityBits == 0);
1211 }
1212 else if (info->BaseFormat == GL_RGBA) {
1213 assert(info->RedBits > 0);
1214 assert(info->GreenBits > 0);
1215 assert(info->BlueBits > 0);
1216 assert(info->AlphaBits > 0);
1217 assert(info->LuminanceBits == 0);
1218 assert(info->IntensityBits == 0);
1219 }
1220 else if (info->BaseFormat == GL_RG) {
1221 assert(info->RedBits > 0);
1222 assert(info->GreenBits > 0);
1223 assert(info->BlueBits == 0);
1224 assert(info->AlphaBits == 0);
1225 assert(info->LuminanceBits == 0);
1226 assert(info->IntensityBits == 0);
1227 }
1228 else if (info->BaseFormat == GL_RED) {
1229 assert(info->RedBits > 0);
1230 assert(info->GreenBits == 0);
1231 assert(info->BlueBits == 0);
1232 assert(info->AlphaBits == 0);
1233 assert(info->LuminanceBits == 0);
1234 assert(info->IntensityBits == 0);
1235 }
1236 else if (info->BaseFormat == GL_LUMINANCE) {
1237 assert(info->RedBits == 0);
1238 assert(info->GreenBits == 0);
1239 assert(info->BlueBits == 0);
1240 assert(info->AlphaBits == 0);
1241 assert(info->LuminanceBits > 0);
1242 assert(info->IntensityBits == 0);
1243 }
1244 else if (info->BaseFormat == GL_INTENSITY) {
1245 assert(info->RedBits == 0);
1246 assert(info->GreenBits == 0);
1247 assert(info->BlueBits == 0);
1248 assert(info->AlphaBits == 0);
1249 assert(info->LuminanceBits == 0);
1250 assert(info->IntensityBits > 0);
1251 }
1252
1253 }
1254 }
1255
1256
1257
1258 /**
1259 * Return datatype and number of components per texel for the given gl_format.
1260 * Only used for mipmap generation code.
1261 */
1262 void
1263 _mesa_format_to_type_and_comps(gl_format format,
1264 GLenum *datatype, GLuint *comps)
1265 {
1266 switch (format) {
1267 case MESA_FORMAT_RGBA8888:
1268 case MESA_FORMAT_RGBA8888_REV:
1269 case MESA_FORMAT_ARGB8888:
1270 case MESA_FORMAT_ARGB8888_REV:
1271 case MESA_FORMAT_XRGB8888:
1272 case MESA_FORMAT_XRGB8888_REV:
1273 *datatype = GL_UNSIGNED_BYTE;
1274 *comps = 4;
1275 return;
1276 case MESA_FORMAT_RGB888:
1277 case MESA_FORMAT_BGR888:
1278 *datatype = GL_UNSIGNED_BYTE;
1279 *comps = 3;
1280 return;
1281 case MESA_FORMAT_RGB565:
1282 case MESA_FORMAT_RGB565_REV:
1283 *datatype = GL_UNSIGNED_SHORT_5_6_5;
1284 *comps = 3;
1285 return;
1286
1287 case MESA_FORMAT_ARGB4444:
1288 case MESA_FORMAT_ARGB4444_REV:
1289 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
1290 *comps = 4;
1291 return;
1292
1293 case MESA_FORMAT_ARGB1555:
1294 case MESA_FORMAT_ARGB1555_REV:
1295 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1296 *comps = 4;
1297 return;
1298
1299 case MESA_FORMAT_ARGB2101010:
1300 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1301 *comps = 4;
1302 return;
1303
1304 case MESA_FORMAT_RGBA5551:
1305 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
1306 *comps = 4;
1307 return;
1308
1309 case MESA_FORMAT_AL44: /* XXX this isn't plain GL_UNSIGNED_BYTE */
1310 case MESA_FORMAT_AL88:
1311 case MESA_FORMAT_AL88_REV:
1312 case MESA_FORMAT_RG88:
1313 case MESA_FORMAT_RG88_REV:
1314 *datatype = GL_UNSIGNED_BYTE;
1315 *comps = 2;
1316 return;
1317
1318 case MESA_FORMAT_AL1616:
1319 case MESA_FORMAT_AL1616_REV:
1320 case MESA_FORMAT_RG1616:
1321 case MESA_FORMAT_RG1616_REV:
1322 *datatype = GL_UNSIGNED_SHORT;
1323 *comps = 2;
1324 return;
1325
1326 case MESA_FORMAT_R16:
1327 case MESA_FORMAT_A16:
1328 case MESA_FORMAT_L16:
1329 case MESA_FORMAT_I16:
1330 *datatype = GL_UNSIGNED_SHORT;
1331 *comps = 1;
1332 return;
1333
1334 case MESA_FORMAT_RGB332:
1335 *datatype = GL_UNSIGNED_BYTE_3_3_2;
1336 *comps = 3;
1337 return;
1338
1339 case MESA_FORMAT_A8:
1340 case MESA_FORMAT_L8:
1341 case MESA_FORMAT_I8:
1342 case MESA_FORMAT_CI8:
1343 case MESA_FORMAT_R8:
1344 case MESA_FORMAT_S8:
1345 *datatype = GL_UNSIGNED_BYTE;
1346 *comps = 1;
1347 return;
1348
1349 case MESA_FORMAT_YCBCR:
1350 case MESA_FORMAT_YCBCR_REV:
1351 *datatype = GL_UNSIGNED_SHORT;
1352 *comps = 2;
1353 return;
1354
1355 case MESA_FORMAT_Z24_S8:
1356 *datatype = GL_UNSIGNED_INT;
1357 *comps = 1; /* XXX OK? */
1358 return;
1359
1360 case MESA_FORMAT_S8_Z24:
1361 *datatype = GL_UNSIGNED_INT;
1362 *comps = 1; /* XXX OK? */
1363 return;
1364
1365 case MESA_FORMAT_Z16:
1366 *datatype = GL_UNSIGNED_SHORT;
1367 *comps = 1;
1368 return;
1369
1370 case MESA_FORMAT_X8_Z24:
1371 *datatype = GL_UNSIGNED_INT;
1372 *comps = 1;
1373 return;
1374
1375 case MESA_FORMAT_Z24_X8:
1376 *datatype = GL_UNSIGNED_INT;
1377 *comps = 1;
1378 return;
1379
1380 case MESA_FORMAT_Z32:
1381 *datatype = GL_UNSIGNED_INT;
1382 *comps = 1;
1383 return;
1384
1385 case MESA_FORMAT_DUDV8:
1386 *datatype = GL_BYTE;
1387 *comps = 2;
1388 return;
1389
1390 case MESA_FORMAT_SIGNED_R8:
1391 *datatype = GL_BYTE;
1392 *comps = 1;
1393 return;
1394 case MESA_FORMAT_SIGNED_RG88:
1395 *datatype = GL_BYTE;
1396 *comps = 2;
1397 return;
1398 case MESA_FORMAT_SIGNED_RGBA8888:
1399 case MESA_FORMAT_SIGNED_RGBA8888_REV:
1400 case MESA_FORMAT_SIGNED_RGBX8888:
1401 *datatype = GL_BYTE;
1402 *comps = 4;
1403 return;
1404
1405 case MESA_FORMAT_RGBA_16:
1406 *datatype = GL_UNSIGNED_SHORT;
1407 *comps = 4;
1408 return;
1409
1410 case MESA_FORMAT_SIGNED_R_16:
1411 *datatype = GL_SHORT;
1412 *comps = 1;
1413 return;
1414 case MESA_FORMAT_SIGNED_RG_16:
1415 *datatype = GL_SHORT;
1416 *comps = 2;
1417 return;
1418 case MESA_FORMAT_SIGNED_RGB_16:
1419 *datatype = GL_SHORT;
1420 *comps = 3;
1421 return;
1422 case MESA_FORMAT_SIGNED_RGBA_16:
1423 *datatype = GL_SHORT;
1424 *comps = 4;
1425 return;
1426
1427 #if FEATURE_EXT_texture_sRGB
1428 case MESA_FORMAT_SRGB8:
1429 *datatype = GL_UNSIGNED_BYTE;
1430 *comps = 3;
1431 return;
1432 case MESA_FORMAT_SRGBA8:
1433 case MESA_FORMAT_SARGB8:
1434 *datatype = GL_UNSIGNED_BYTE;
1435 *comps = 4;
1436 return;
1437 case MESA_FORMAT_SL8:
1438 *datatype = GL_UNSIGNED_BYTE;
1439 *comps = 1;
1440 return;
1441 case MESA_FORMAT_SLA8:
1442 *datatype = GL_UNSIGNED_BYTE;
1443 *comps = 2;
1444 return;
1445 #endif
1446
1447 #if FEATURE_texture_fxt1
1448 case MESA_FORMAT_RGB_FXT1:
1449 case MESA_FORMAT_RGBA_FXT1:
1450 #endif
1451 #if FEATURE_texture_s3tc
1452 case MESA_FORMAT_RGB_DXT1:
1453 case MESA_FORMAT_RGBA_DXT1:
1454 case MESA_FORMAT_RGBA_DXT3:
1455 case MESA_FORMAT_RGBA_DXT5:
1456 #if FEATURE_EXT_texture_sRGB
1457 case MESA_FORMAT_SRGB_DXT1:
1458 case MESA_FORMAT_SRGBA_DXT1:
1459 case MESA_FORMAT_SRGBA_DXT3:
1460 case MESA_FORMAT_SRGBA_DXT5:
1461 #endif
1462 #endif
1463 /* XXX generate error instead? */
1464 *datatype = GL_UNSIGNED_BYTE;
1465 *comps = 0;
1466 return;
1467
1468 case MESA_FORMAT_RGBA_FLOAT32:
1469 *datatype = GL_FLOAT;
1470 *comps = 4;
1471 return;
1472 case MESA_FORMAT_RGBA_FLOAT16:
1473 *datatype = GL_HALF_FLOAT_ARB;
1474 *comps = 4;
1475 return;
1476 case MESA_FORMAT_RGB_FLOAT32:
1477 *datatype = GL_FLOAT;
1478 *comps = 3;
1479 return;
1480 case MESA_FORMAT_RGB_FLOAT16:
1481 *datatype = GL_HALF_FLOAT_ARB;
1482 *comps = 3;
1483 return;
1484 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32:
1485 *datatype = GL_FLOAT;
1486 *comps = 2;
1487 return;
1488 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16:
1489 *datatype = GL_HALF_FLOAT_ARB;
1490 *comps = 2;
1491 return;
1492 case MESA_FORMAT_ALPHA_FLOAT32:
1493 case MESA_FORMAT_LUMINANCE_FLOAT32:
1494 case MESA_FORMAT_INTENSITY_FLOAT32:
1495 *datatype = GL_FLOAT;
1496 *comps = 1;
1497 return;
1498 case MESA_FORMAT_ALPHA_FLOAT16:
1499 case MESA_FORMAT_LUMINANCE_FLOAT16:
1500 case MESA_FORMAT_INTENSITY_FLOAT16:
1501 *datatype = GL_HALF_FLOAT_ARB;
1502 *comps = 1;
1503 return;
1504
1505 case MESA_FORMAT_RGBA_INT8:
1506 *datatype = GL_BYTE;
1507 *comps = 4;
1508 return;
1509 case MESA_FORMAT_RGBA_INT16:
1510 *datatype = GL_SHORT;
1511 *comps = 4;
1512 return;
1513 case MESA_FORMAT_RGBA_INT32:
1514 *datatype = GL_INT;
1515 *comps = 4;
1516 return;
1517
1518 /**
1519 * \name Non-normalized unsigned integer formats.
1520 */
1521 case MESA_FORMAT_RGBA_UINT8:
1522 *datatype = GL_UNSIGNED_BYTE;
1523 *comps = 4;
1524 return;
1525 case MESA_FORMAT_RGBA_UINT16:
1526 *datatype = GL_UNSIGNED_SHORT;
1527 *comps = 4;
1528 return;
1529 case MESA_FORMAT_RGBA_UINT32:
1530 *datatype = GL_UNSIGNED_INT;
1531 *comps = 4;
1532 return;
1533
1534 case MESA_FORMAT_NONE:
1535 case MESA_FORMAT_COUNT:
1536 /* For debug builds, warn if any formats are not handled */
1537 #ifdef DEBUG
1538 default:
1539 #endif
1540 _mesa_problem(NULL, "bad format %s in _mesa_format_to_type_and_comps",
1541 _mesa_get_format_name(format));
1542 *datatype = 0;
1543 *comps = 1;
1544 }
1545 }