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