format info for Z16/Z32
[mesa.git] / src / mesa / state_tracker / st_format.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Texture Image-related functions.
31 * \author Brian Paul
32 */
33
34 #include "main/imports.h"
35 #include "main/context.h"
36 #include "main/texstore.h"
37 #include "main/texformat.h"
38 #include "main/enums.h"
39
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "st_context.h"
43 #include "st_format.h"
44
45
46
47 /*
48 * XXX temporary here
49 */
50 const struct pipe_format_info *
51 st_get_format_info(GLuint format)
52 {
53 static const struct pipe_format_info info[] = {
54 {
55 PIPE_FORMAT_U_R8_G8_B8_A8, /* format */
56 GL_RGBA, /* base_format */
57 8, 8, 8, 8, 0, 0, /* color bits */
58 0, 0, /* depth, stencil */
59 4 /* size in bytes */
60 },
61 {
62 PIPE_FORMAT_U_A8_R8_G8_B8,
63 GL_RGBA, /* base_format */
64 8, 8, 8, 8, 0, 0, /* color bits */
65 0, 0, /* depth, stencil */
66 4 /* size in bytes */
67 },
68 {
69 PIPE_FORMAT_U_A1_R5_G5_B5,
70 GL_RGBA, /* base_format */
71 5, 5, 5, 1, 0, 0, /* color bits */
72 0, 0, /* depth, stencil */
73 2 /* size in bytes */
74 },
75 {
76 PIPE_FORMAT_U_R5_G6_B5,
77 GL_RGBA, /* base_format */
78 5, 6, 5, 0, 0, 0, /* color bits */
79 0, 0, /* depth, stencil */
80 2 /* size in bytes */
81 },
82 {
83 PIPE_FORMAT_U_Z16,
84 GL_DEPTH_COMPONENT, /* base_format */
85 0, 0, 0, 0, 0, 0, /* color bits */
86 16, 0, /* depth, stencil */
87 2 /* size in bytes */
88 },
89 {
90 PIPE_FORMAT_U_Z32,
91 GL_DEPTH_COMPONENT, /* base_format */
92 0, 0, 0, 0, 0, 0, /* color bits */
93 32, 0, /* depth, stencil */
94 4 /* size in bytes */
95 },
96 {
97 PIPE_FORMAT_S8_Z24,
98 GL_DEPTH_STENCIL_EXT, /* base_format */
99 0, 0, 0, 0, 0, 0, /* color bits */
100 24, 8, /* depth, stencil */
101 4 /* size in bytes */
102 }
103 /* XXX lots more cases to add */
104 };
105 GLuint i;
106
107 for (i = 0; i < sizeof(info) / sizeof(info[0]); i++) {
108 if (info[i].format == format)
109 return info + i;
110 }
111 return NULL;
112 }
113
114
115 GLuint
116 st_sizeof_format(GLuint pipeFormat)
117 {
118 const struct pipe_format_info *info = st_get_format_info(pipeFormat);
119 assert(info);
120 return info->size;
121 }
122
123
124 GLuint
125 st_mesa_format_to_pipe_format(GLuint mesaFormat)
126 {
127 switch (mesaFormat) {
128 /* fix this */
129 case MESA_FORMAT_ARGB8888_REV:
130 case MESA_FORMAT_ARGB8888:
131 return PIPE_FORMAT_U_A8_R8_G8_B8;
132 case MESA_FORMAT_AL88:
133 return PIPE_FORMAT_U_A8_L8;
134 case MESA_FORMAT_A8:
135 return PIPE_FORMAT_U_A8;
136 case MESA_FORMAT_L8:
137 return PIPE_FORMAT_U_L8;
138 case MESA_FORMAT_I8:
139 return PIPE_FORMAT_U_I8;
140 case MESA_FORMAT_Z16:
141 return PIPE_FORMAT_U_Z16;
142 default:
143 assert(0);
144 return 0;
145 }
146 }
147
148
149 /**
150 * Search list of formats for first RGBA format.
151 */
152 static GLuint
153 default_rgba_format(const GLuint formats[], GLuint num)
154 {
155 GLuint i;
156 for (i = 0; i < num; i++) {
157 if (formats[i] == PIPE_FORMAT_U_R8_G8_B8_A8 ||
158 formats[i] == PIPE_FORMAT_U_A8_R8_G8_B8 ||
159 formats[i] == PIPE_FORMAT_U_R5_G6_B5) {
160 return formats[i];
161 }
162 }
163 return PIPE_FORMAT_NONE;
164 }
165
166
167 /**
168 * Search list of formats for first depth/Z format.
169 */
170 static GLuint
171 default_depth_format(const GLuint formats[], GLuint num)
172 {
173 GLuint i;
174 for (i = 0; i < num; i++) {
175 if (formats[i] == PIPE_FORMAT_U_Z16 ||
176 formats[i] == PIPE_FORMAT_U_Z32 ||
177 formats[i] == PIPE_FORMAT_S8_Z24) {
178 return formats[i];
179 }
180 }
181 return PIPE_FORMAT_NONE;
182 }
183
184
185 /**
186 * Choose the PIPE_FORMAT_ to use for storing a texture image based
187 * on the user's internalFormat, format and type parameters.
188 * We query the pipe device for a list of formats which it supports
189 * and choose from them.
190 * If we find a device that needs a more intricate selection mechanism,
191 * this function _could_ get pushed down into the pipe device.
192 *
193 * Note: also used for glRenderbufferStorageEXT()
194 *
195 * Note: format and type may be GL_NONE (see renderbuffers)
196 *
197 * \return PIPE_FORMAT_NONE if error/problem.
198 */
199 GLuint
200 st_choose_pipe_format(struct pipe_context *pipe, GLint internalFormat,
201 GLenum format, GLenum type)
202 {
203 const GLuint *supported;
204 GLboolean allow[PIPE_FORMAT_COUNT];
205 GLuint i, n;
206
207 /* query supported formats and fill in bool allow[] table */
208 supported = pipe->supported_formats(pipe, &n);
209 assert(n < PIPE_FORMAT_COUNT); /* sanity check */
210 memset(allow, 0, sizeof(allow));
211 for (i = 0; i < n; i++) {
212 allow[supported[i]] = 1;
213 }
214
215 switch (internalFormat) {
216 case 4:
217 case GL_RGBA:
218 case GL_COMPRESSED_RGBA:
219 if (format == GL_BGRA) {
220 if (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) {
221 if (allow[PIPE_FORMAT_U_A8_R8_G8_B8])
222 return PIPE_FORMAT_U_A8_R8_G8_B8;
223 }
224 else if (type == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
225 if (allow[PIPE_FORMAT_U_A4_R4_G4_B4])
226 return PIPE_FORMAT_U_A4_R4_G4_B4;
227 }
228 else if (type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
229 if (allow[PIPE_FORMAT_U_A1_R5_G5_B5])
230 return PIPE_FORMAT_U_A1_R5_G5_B5;
231 }
232 }
233 return default_rgba_format(supported, n);
234
235 case 3:
236 case GL_RGB:
237 case GL_COMPRESSED_RGB:
238 if (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) {
239 if (allow[PIPE_FORMAT_U_R5_G6_B5])
240 return PIPE_FORMAT_U_R5_G6_B5;
241 }
242 return default_rgba_format(supported, n);
243
244 case GL_RGBA8:
245 case GL_RGB10_A2:
246 case GL_RGBA12:
247 case GL_RGBA16:
248 return default_rgba_format(supported, n);
249
250 case GL_RGBA4:
251 case GL_RGBA2:
252 if (allow[PIPE_FORMAT_U_A4_R4_G4_B4])
253 return PIPE_FORMAT_U_A4_R4_G4_B4;
254 return default_rgba_format(supported, n);
255
256 case GL_RGB5_A1:
257 if (allow[PIPE_FORMAT_U_A1_R5_G5_B5])
258 return PIPE_FORMAT_U_A1_R5_G5_B5;
259 return default_rgba_format(supported, n);
260
261 case GL_RGB8:
262 case GL_RGB10:
263 case GL_RGB12:
264 case GL_RGB16:
265 return default_rgba_format(supported, n);
266
267 case GL_RGB5:
268 case GL_RGB4:
269 case GL_R3_G3_B2:
270 if (allow[PIPE_FORMAT_U_A1_R5_G5_B5])
271 return PIPE_FORMAT_U_A1_R5_G5_B5;
272 return default_rgba_format(supported, n);
273
274 case GL_ALPHA:
275 case GL_ALPHA4:
276 case GL_ALPHA8:
277 case GL_ALPHA12:
278 case GL_ALPHA16:
279 case GL_COMPRESSED_ALPHA:
280 if (allow[PIPE_FORMAT_U_A8])
281 return PIPE_FORMAT_U_A8;
282 return default_rgba_format(supported, n);
283
284 case 1:
285 case GL_LUMINANCE:
286 case GL_LUMINANCE4:
287 case GL_LUMINANCE8:
288 case GL_LUMINANCE12:
289 case GL_LUMINANCE16:
290 case GL_COMPRESSED_LUMINANCE:
291 if (allow[PIPE_FORMAT_U_A8])
292 return PIPE_FORMAT_U_A8;
293 return default_rgba_format(supported, n);
294
295 case 2:
296 case GL_LUMINANCE_ALPHA:
297 case GL_LUMINANCE4_ALPHA4:
298 case GL_LUMINANCE6_ALPHA2:
299 case GL_LUMINANCE8_ALPHA8:
300 case GL_LUMINANCE12_ALPHA4:
301 case GL_LUMINANCE12_ALPHA12:
302 case GL_LUMINANCE16_ALPHA16:
303 case GL_COMPRESSED_LUMINANCE_ALPHA:
304 if (allow[PIPE_FORMAT_U_A8_L8])
305 return PIPE_FORMAT_U_A8_L8;
306 return default_rgba_format(supported, n);
307
308 case GL_INTENSITY:
309 case GL_INTENSITY4:
310 case GL_INTENSITY8:
311 case GL_INTENSITY12:
312 case GL_INTENSITY16:
313 case GL_COMPRESSED_INTENSITY:
314 if (allow[PIPE_FORMAT_U_I8])
315 return PIPE_FORMAT_U_I8;
316 return default_rgba_format(supported, n);
317
318 case GL_YCBCR_MESA:
319 if (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE) {
320 if (allow[PIPE_FORMAT_YCBCR])
321 return PIPE_FORMAT_YCBCR;
322 }
323 else {
324 if (allow[PIPE_FORMAT_YCBCR_REV])
325 return PIPE_FORMAT_YCBCR_REV;
326 }
327 return PIPE_FORMAT_NONE;
328
329 #if 0
330 case GL_COMPRESSED_RGB_FXT1_3DFX:
331 return &_mesa_texformat_rgb_fxt1;
332 case GL_COMPRESSED_RGBA_FXT1_3DFX:
333 return &_mesa_texformat_rgba_fxt1;
334
335 case GL_RGB_S3TC:
336 case GL_RGB4_S3TC:
337 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
338 return &_mesa_texformat_rgb_dxt1;
339
340 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
341 return &_mesa_texformat_rgba_dxt1;
342
343 case GL_RGBA_S3TC:
344 case GL_RGBA4_S3TC:
345 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
346 return &_mesa_texformat_rgba_dxt3;
347
348 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
349 return &_mesa_texformat_rgba_dxt5;
350 #endif
351
352 case GL_DEPTH_COMPONENT16:
353 if (allow[PIPE_FORMAT_U_Z16])
354 return PIPE_FORMAT_U_Z16;
355 /* fall-through */
356 case GL_DEPTH_COMPONENT24:
357 if (allow[PIPE_FORMAT_S8_Z24])
358 return PIPE_FORMAT_S8_Z24;
359 /* fall-through */
360 case GL_DEPTH_COMPONENT32:
361 if (allow[PIPE_FORMAT_U_Z32])
362 return PIPE_FORMAT_U_Z32;
363 /* fall-through */
364 case GL_DEPTH_COMPONENT:
365 return default_depth_format(supported, n);
366
367 case GL_STENCIL_INDEX:
368 case GL_STENCIL_INDEX1_EXT:
369 case GL_STENCIL_INDEX4_EXT:
370 case GL_STENCIL_INDEX8_EXT:
371 case GL_STENCIL_INDEX16_EXT:
372 if (allow[PIPE_FORMAT_U_S8])
373 return PIPE_FORMAT_U_S8;
374 if (allow[PIPE_FORMAT_S8_Z24])
375 return PIPE_FORMAT_S8_Z24;
376 return PIPE_FORMAT_NONE;
377
378 case GL_DEPTH_STENCIL_EXT:
379 case GL_DEPTH24_STENCIL8_EXT:
380 if (allow[PIPE_FORMAT_S8_Z24])
381 return PIPE_FORMAT_S8_Z24;
382 return PIPE_FORMAT_NONE;
383
384 default:
385 return PIPE_FORMAT_NONE;
386 }
387 }
388
389
390
391 /* It works out that this function is fine for all the supported
392 * hardware. However, there is still a need to map the formats onto
393 * hardware descriptors.
394 */
395 /* Note that the i915 can actually support many more formats than
396 * these if we take the step of simply swizzling the colors
397 * immediately after sampling...
398 */
399 const struct gl_texture_format *
400 st_ChooseTextureFormat(GLcontext * ctx, GLint internalFormat,
401 GLenum format, GLenum type)
402 {
403 #if 0
404 struct intel_context *intel = intel_context(ctx);
405 const GLboolean do32bpt = (intel->intelScreen->front.cpp == 4);
406 #else
407 const GLboolean do32bpt = 1;
408 #endif
409
410 switch (internalFormat) {
411 case 4:
412 case GL_RGBA:
413 case GL_COMPRESSED_RGBA:
414 if (format == GL_BGRA) {
415 if (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) {
416 return &_mesa_texformat_argb8888;
417 }
418 else if (type == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
419 return &_mesa_texformat_argb4444;
420 }
421 else if (type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
422 return &_mesa_texformat_argb1555;
423 }
424 }
425 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
426
427 case 3:
428 case GL_RGB:
429 case GL_COMPRESSED_RGB:
430 if (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) {
431 return &_mesa_texformat_rgb565;
432 }
433 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
434
435 case GL_RGBA8:
436 case GL_RGB10_A2:
437 case GL_RGBA12:
438 case GL_RGBA16:
439 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
440
441 case GL_RGBA4:
442 case GL_RGBA2:
443 return &_mesa_texformat_argb4444;
444
445 case GL_RGB5_A1:
446 return &_mesa_texformat_argb1555;
447
448 case GL_RGB8:
449 case GL_RGB10:
450 case GL_RGB12:
451 case GL_RGB16:
452 return &_mesa_texformat_argb8888;
453
454 case GL_RGB5:
455 case GL_RGB4:
456 case GL_R3_G3_B2:
457 return &_mesa_texformat_rgb565;
458
459 case GL_ALPHA:
460 case GL_ALPHA4:
461 case GL_ALPHA8:
462 case GL_ALPHA12:
463 case GL_ALPHA16:
464 case GL_COMPRESSED_ALPHA:
465 return &_mesa_texformat_a8;
466
467 case 1:
468 case GL_LUMINANCE:
469 case GL_LUMINANCE4:
470 case GL_LUMINANCE8:
471 case GL_LUMINANCE12:
472 case GL_LUMINANCE16:
473 case GL_COMPRESSED_LUMINANCE:
474 return &_mesa_texformat_l8;
475
476 case 2:
477 case GL_LUMINANCE_ALPHA:
478 case GL_LUMINANCE4_ALPHA4:
479 case GL_LUMINANCE6_ALPHA2:
480 case GL_LUMINANCE8_ALPHA8:
481 case GL_LUMINANCE12_ALPHA4:
482 case GL_LUMINANCE12_ALPHA12:
483 case GL_LUMINANCE16_ALPHA16:
484 case GL_COMPRESSED_LUMINANCE_ALPHA:
485 return &_mesa_texformat_al88;
486
487 case GL_INTENSITY:
488 case GL_INTENSITY4:
489 case GL_INTENSITY8:
490 case GL_INTENSITY12:
491 case GL_INTENSITY16:
492 case GL_COMPRESSED_INTENSITY:
493 return &_mesa_texformat_i8;
494
495 case GL_YCBCR_MESA:
496 if (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE)
497 return &_mesa_texformat_ycbcr;
498 else
499 return &_mesa_texformat_ycbcr_rev;
500
501 case GL_COMPRESSED_RGB_FXT1_3DFX:
502 return &_mesa_texformat_rgb_fxt1;
503 case GL_COMPRESSED_RGBA_FXT1_3DFX:
504 return &_mesa_texformat_rgba_fxt1;
505
506 case GL_RGB_S3TC:
507 case GL_RGB4_S3TC:
508 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
509 return &_mesa_texformat_rgb_dxt1;
510
511 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
512 return &_mesa_texformat_rgba_dxt1;
513
514 case GL_RGBA_S3TC:
515 case GL_RGBA4_S3TC:
516 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
517 return &_mesa_texformat_rgba_dxt3;
518
519 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
520 return &_mesa_texformat_rgba_dxt5;
521
522 case GL_DEPTH_COMPONENT:
523 case GL_DEPTH_COMPONENT16:
524 case GL_DEPTH_COMPONENT24:
525 case GL_DEPTH_COMPONENT32:
526 return &_mesa_texformat_z16;
527
528 case GL_DEPTH_STENCIL_EXT:
529 case GL_DEPTH24_STENCIL8_EXT:
530 return &_mesa_texformat_z24_s8;
531
532 default:
533 fprintf(stderr, "unexpected texture format %s in %s\n",
534 _mesa_lookup_enum_by_nr(internalFormat), __FUNCTION__);
535 return NULL;
536 }
537
538 return NULL; /* never get here */
539 }