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