don't apply pixelzoom to bitmaps
[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 /* XXX lots more */
83 {
84 PIPE_FORMAT_S8_Z24,
85 GL_DEPTH_STENCIL_EXT, /* base_format */
86 0, 0, 0, 0, 0, 0, /* color bits */
87 24, 8, /* depth, stencil */
88 4 /* size in bytes */
89 }
90 };
91 GLuint i;
92
93 for (i = 0; i < sizeof(info) / sizeof(info[0]); i++) {
94 if (info[i].format == format)
95 return info + i;
96 }
97 return NULL;
98 }
99
100
101 GLuint
102 st_sizeof_format(GLuint pipeFormat)
103 {
104 const struct pipe_format_info *info = st_get_format_info(pipeFormat);
105 assert(info);
106 return info->size;
107 }
108
109
110 GLuint
111 st_mesa_format_to_pipe_format(GLuint mesaFormat)
112 {
113 switch (mesaFormat) {
114 /* fix this */
115 case MESA_FORMAT_ARGB8888_REV:
116 case MESA_FORMAT_ARGB8888:
117 return PIPE_FORMAT_U_A8_R8_G8_B8;
118 case MESA_FORMAT_AL88:
119 return PIPE_FORMAT_U_A8_L8;
120 case MESA_FORMAT_A8:
121 return PIPE_FORMAT_U_A8;
122 case MESA_FORMAT_L8:
123 return PIPE_FORMAT_U_L8;
124 case MESA_FORMAT_I8:
125 return PIPE_FORMAT_U_I8;
126 default:
127 assert(0);
128 return 0;
129 }
130 }
131
132
133 /**
134 * Search list of formats for first RGBA format.
135 */
136 static GLuint
137 default_rgba_format(const GLuint formats[], GLuint num)
138 {
139 GLuint i;
140 for (i = 0; i < num; i++) {
141 if (formats[i] == PIPE_FORMAT_U_R8_G8_B8_A8 ||
142 formats[i] == PIPE_FORMAT_U_A8_R8_G8_B8 ||
143 formats[i] == PIPE_FORMAT_U_R5_G6_B5) {
144 return formats[i];
145 }
146 }
147 return PIPE_FORMAT_NONE;
148 }
149
150
151 /**
152 * Search list of formats for first depth/Z format.
153 */
154 static GLuint
155 default_depth_format(const GLuint formats[], GLuint num)
156 {
157 GLuint i;
158 for (i = 0; i < num; i++) {
159 if (formats[i] == PIPE_FORMAT_U_Z16 ||
160 formats[i] == PIPE_FORMAT_U_Z32 ||
161 formats[i] == PIPE_FORMAT_S8_Z24) {
162 return formats[i];
163 }
164 }
165 return PIPE_FORMAT_NONE;
166 }
167
168
169 /**
170 * Choose the PIPE_FORMAT_ to use for storing a texture image based
171 * on the user's internalFormat, format and type parameters.
172 * We query the pipe device for a list of formats which it supports
173 * and choose from them.
174 * If we find a device that needs a more intricate selection mechanism,
175 * this function _could_ get pushed down into the pipe device.
176 *
177 * Note: also used for glRenderbufferStorageEXT()
178 *
179 * Note: format and type may be GL_NONE (see renderbuffers)
180 *
181 * \return PIPE_FORMAT_NONE if error/problem.
182 */
183 GLuint
184 st_choose_pipe_format(struct pipe_context *pipe, GLint internalFormat,
185 GLenum format, GLenum type)
186 {
187 const GLuint *supported;
188 GLboolean allow[PIPE_FORMAT_COUNT];
189 GLuint i, n;
190
191 /* query supported formats and fill in bool allow[] table */
192 supported = pipe->supported_formats(pipe, &n);
193 assert(n < PIPE_FORMAT_COUNT); /* sanity check */
194 memset(allow, 0, sizeof(allow));
195 for (i = 0; i < n; i++) {
196 allow[supported[i]] = 1;
197 }
198
199 switch (internalFormat) {
200 case 4:
201 case GL_RGBA:
202 case GL_COMPRESSED_RGBA:
203 if (format == GL_BGRA) {
204 if (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) {
205 if (allow[PIPE_FORMAT_U_A8_R8_G8_B8])
206 return PIPE_FORMAT_U_A8_R8_G8_B8;
207 }
208 else if (type == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
209 if (allow[PIPE_FORMAT_U_A4_R4_G4_B4])
210 return PIPE_FORMAT_U_A4_R4_G4_B4;
211 }
212 else if (type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
213 if (allow[PIPE_FORMAT_U_A1_R5_G5_B5])
214 return PIPE_FORMAT_U_A1_R5_G5_B5;
215 }
216 }
217 return default_rgba_format(supported, n);
218
219 case 3:
220 case GL_RGB:
221 case GL_COMPRESSED_RGB:
222 if (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) {
223 if (allow[PIPE_FORMAT_U_R5_G6_B5])
224 return PIPE_FORMAT_U_R5_G6_B5;
225 }
226 return default_rgba_format(supported, n);
227
228 case GL_RGBA8:
229 case GL_RGB10_A2:
230 case GL_RGBA12:
231 case GL_RGBA16:
232 return default_rgba_format(supported, n);
233
234 case GL_RGBA4:
235 case GL_RGBA2:
236 if (allow[PIPE_FORMAT_U_A4_R4_G4_B4])
237 return PIPE_FORMAT_U_A4_R4_G4_B4;
238 return default_rgba_format(supported, n);
239
240 case GL_RGB5_A1:
241 if (allow[PIPE_FORMAT_U_A1_R5_G5_B5])
242 return PIPE_FORMAT_U_A1_R5_G5_B5;
243 return default_rgba_format(supported, n);
244
245 case GL_RGB8:
246 case GL_RGB10:
247 case GL_RGB12:
248 case GL_RGB16:
249 return default_rgba_format(supported, n);
250
251 case GL_RGB5:
252 case GL_RGB4:
253 case GL_R3_G3_B2:
254 if (allow[PIPE_FORMAT_U_A1_R5_G5_B5])
255 return PIPE_FORMAT_U_A1_R5_G5_B5;
256 return default_rgba_format(supported, n);
257
258 case GL_ALPHA:
259 case GL_ALPHA4:
260 case GL_ALPHA8:
261 case GL_ALPHA12:
262 case GL_ALPHA16:
263 case GL_COMPRESSED_ALPHA:
264 if (allow[PIPE_FORMAT_U_A8])
265 return PIPE_FORMAT_U_A8;
266 return default_rgba_format(supported, n);
267
268 case 1:
269 case GL_LUMINANCE:
270 case GL_LUMINANCE4:
271 case GL_LUMINANCE8:
272 case GL_LUMINANCE12:
273 case GL_LUMINANCE16:
274 case GL_COMPRESSED_LUMINANCE:
275 if (allow[PIPE_FORMAT_U_A8])
276 return PIPE_FORMAT_U_A8;
277 return default_rgba_format(supported, n);
278
279 case 2:
280 case GL_LUMINANCE_ALPHA:
281 case GL_LUMINANCE4_ALPHA4:
282 case GL_LUMINANCE6_ALPHA2:
283 case GL_LUMINANCE8_ALPHA8:
284 case GL_LUMINANCE12_ALPHA4:
285 case GL_LUMINANCE12_ALPHA12:
286 case GL_LUMINANCE16_ALPHA16:
287 case GL_COMPRESSED_LUMINANCE_ALPHA:
288 if (allow[PIPE_FORMAT_U_A8_L8])
289 return PIPE_FORMAT_U_A8_L8;
290 return default_rgba_format(supported, n);
291
292 case GL_INTENSITY:
293 case GL_INTENSITY4:
294 case GL_INTENSITY8:
295 case GL_INTENSITY12:
296 case GL_INTENSITY16:
297 case GL_COMPRESSED_INTENSITY:
298 if (allow[PIPE_FORMAT_U_I8])
299 return PIPE_FORMAT_U_I8;
300 return default_rgba_format(supported, n);
301
302 case GL_YCBCR_MESA:
303 if (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE) {
304 if (allow[PIPE_FORMAT_YCBCR])
305 return PIPE_FORMAT_YCBCR;
306 }
307 else {
308 if (allow[PIPE_FORMAT_YCBCR_REV])
309 return PIPE_FORMAT_YCBCR_REV;
310 }
311 return PIPE_FORMAT_NONE;
312
313 #if 0
314 case GL_COMPRESSED_RGB_FXT1_3DFX:
315 return &_mesa_texformat_rgb_fxt1;
316 case GL_COMPRESSED_RGBA_FXT1_3DFX:
317 return &_mesa_texformat_rgba_fxt1;
318
319 case GL_RGB_S3TC:
320 case GL_RGB4_S3TC:
321 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
322 return &_mesa_texformat_rgb_dxt1;
323
324 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
325 return &_mesa_texformat_rgba_dxt1;
326
327 case GL_RGBA_S3TC:
328 case GL_RGBA4_S3TC:
329 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
330 return &_mesa_texformat_rgba_dxt3;
331
332 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
333 return &_mesa_texformat_rgba_dxt5;
334 #endif
335
336 case GL_DEPTH_COMPONENT16:
337 if (allow[PIPE_FORMAT_U_Z16])
338 return PIPE_FORMAT_U_Z16;
339 /* fall-through */
340 case GL_DEPTH_COMPONENT24:
341 if (allow[PIPE_FORMAT_S8_Z24])
342 return PIPE_FORMAT_S8_Z24;
343 /* fall-through */
344 case GL_DEPTH_COMPONENT32:
345 if (allow[PIPE_FORMAT_U_Z32])
346 return PIPE_FORMAT_U_Z32;
347 /* fall-through */
348 case GL_DEPTH_COMPONENT:
349 return default_depth_format(supported, n);
350
351 case GL_STENCIL_INDEX:
352 case GL_STENCIL_INDEX1_EXT:
353 case GL_STENCIL_INDEX4_EXT:
354 case GL_STENCIL_INDEX8_EXT:
355 case GL_STENCIL_INDEX16_EXT:
356 if (allow[PIPE_FORMAT_U_S8])
357 return PIPE_FORMAT_U_S8;
358 if (allow[PIPE_FORMAT_S8_Z24])
359 return PIPE_FORMAT_S8_Z24;
360 return PIPE_FORMAT_NONE;
361
362 case GL_DEPTH_STENCIL_EXT:
363 case GL_DEPTH24_STENCIL8_EXT:
364 if (allow[PIPE_FORMAT_S8_Z24])
365 return PIPE_FORMAT_S8_Z24;
366 return PIPE_FORMAT_NONE;
367
368 default:
369 return PIPE_FORMAT_NONE;
370 }
371 }
372
373
374
375 /* It works out that this function is fine for all the supported
376 * hardware. However, there is still a need to map the formats onto
377 * hardware descriptors.
378 */
379 /* Note that the i915 can actually support many more formats than
380 * these if we take the step of simply swizzling the colors
381 * immediately after sampling...
382 */
383 const struct gl_texture_format *
384 st_ChooseTextureFormat(GLcontext * ctx, GLint internalFormat,
385 GLenum format, GLenum type)
386 {
387 #if 0
388 struct intel_context *intel = intel_context(ctx);
389 const GLboolean do32bpt = (intel->intelScreen->front.cpp == 4);
390 #else
391 const GLboolean do32bpt = 1;
392 #endif
393
394 switch (internalFormat) {
395 case 4:
396 case GL_RGBA:
397 case GL_COMPRESSED_RGBA:
398 if (format == GL_BGRA) {
399 if (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) {
400 return &_mesa_texformat_argb8888;
401 }
402 else if (type == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
403 return &_mesa_texformat_argb4444;
404 }
405 else if (type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
406 return &_mesa_texformat_argb1555;
407 }
408 }
409 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
410
411 case 3:
412 case GL_RGB:
413 case GL_COMPRESSED_RGB:
414 if (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) {
415 return &_mesa_texformat_rgb565;
416 }
417 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
418
419 case GL_RGBA8:
420 case GL_RGB10_A2:
421 case GL_RGBA12:
422 case GL_RGBA16:
423 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
424
425 case GL_RGBA4:
426 case GL_RGBA2:
427 return &_mesa_texformat_argb4444;
428
429 case GL_RGB5_A1:
430 return &_mesa_texformat_argb1555;
431
432 case GL_RGB8:
433 case GL_RGB10:
434 case GL_RGB12:
435 case GL_RGB16:
436 return &_mesa_texformat_argb8888;
437
438 case GL_RGB5:
439 case GL_RGB4:
440 case GL_R3_G3_B2:
441 return &_mesa_texformat_rgb565;
442
443 case GL_ALPHA:
444 case GL_ALPHA4:
445 case GL_ALPHA8:
446 case GL_ALPHA12:
447 case GL_ALPHA16:
448 case GL_COMPRESSED_ALPHA:
449 return &_mesa_texformat_a8;
450
451 case 1:
452 case GL_LUMINANCE:
453 case GL_LUMINANCE4:
454 case GL_LUMINANCE8:
455 case GL_LUMINANCE12:
456 case GL_LUMINANCE16:
457 case GL_COMPRESSED_LUMINANCE:
458 return &_mesa_texformat_l8;
459
460 case 2:
461 case GL_LUMINANCE_ALPHA:
462 case GL_LUMINANCE4_ALPHA4:
463 case GL_LUMINANCE6_ALPHA2:
464 case GL_LUMINANCE8_ALPHA8:
465 case GL_LUMINANCE12_ALPHA4:
466 case GL_LUMINANCE12_ALPHA12:
467 case GL_LUMINANCE16_ALPHA16:
468 case GL_COMPRESSED_LUMINANCE_ALPHA:
469 return &_mesa_texformat_al88;
470
471 case GL_INTENSITY:
472 case GL_INTENSITY4:
473 case GL_INTENSITY8:
474 case GL_INTENSITY12:
475 case GL_INTENSITY16:
476 case GL_COMPRESSED_INTENSITY:
477 return &_mesa_texformat_i8;
478
479 case GL_YCBCR_MESA:
480 if (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE)
481 return &_mesa_texformat_ycbcr;
482 else
483 return &_mesa_texformat_ycbcr_rev;
484
485 case GL_COMPRESSED_RGB_FXT1_3DFX:
486 return &_mesa_texformat_rgb_fxt1;
487 case GL_COMPRESSED_RGBA_FXT1_3DFX:
488 return &_mesa_texformat_rgba_fxt1;
489
490 case GL_RGB_S3TC:
491 case GL_RGB4_S3TC:
492 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
493 return &_mesa_texformat_rgb_dxt1;
494
495 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
496 return &_mesa_texformat_rgba_dxt1;
497
498 case GL_RGBA_S3TC:
499 case GL_RGBA4_S3TC:
500 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
501 return &_mesa_texformat_rgba_dxt3;
502
503 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
504 return &_mesa_texformat_rgba_dxt5;
505
506 case GL_DEPTH_COMPONENT:
507 case GL_DEPTH_COMPONENT16:
508 case GL_DEPTH_COMPONENT24:
509 case GL_DEPTH_COMPONENT32:
510 return &_mesa_texformat_z16;
511
512 case GL_DEPTH_STENCIL_EXT:
513 case GL_DEPTH24_STENCIL8_EXT:
514 return &_mesa_texformat_z24_s8;
515
516 default:
517 fprintf(stderr, "unexpected texture format %s in %s\n",
518 _mesa_lookup_enum_by_nr(internalFormat), __FUNCTION__);
519 return NULL;
520 }
521
522 return NULL; /* never get here */
523 }