1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Texture Image-related functions.
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"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "st_context.h"
43 #include "st_format.h"
50 const struct pipe_format_info
*
51 st_get_format_info(GLuint format
)
53 static const struct pipe_format_info info
[] = {
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 */
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 */
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 */
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 */
85 GL_DEPTH_STENCIL_EXT
, /* base_format */
86 0, 0, 0, 0, 0, 0, /* color bits */
87 24, 8, /* depth, stencil */
93 for (i
= 0; i
< sizeof(info
) / sizeof(info
[0]); i
++) {
94 if (info
[i
].format
== format
)
102 st_sizeof_format(GLuint pipeFormat
)
104 const struct pipe_format_info
*info
= st_get_format_info(pipeFormat
);
111 st_mesa_format_to_pipe_format(GLuint mesaFormat
)
113 switch (mesaFormat
) {
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
;
121 return PIPE_FORMAT_U_A8
;
123 return PIPE_FORMAT_U_L8
;
125 return PIPE_FORMAT_U_I8
;
134 * Search list of formats for first RGBA format.
137 default_rgba_format(const GLuint formats
[], GLuint num
)
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
) {
147 return PIPE_FORMAT_NONE
;
152 * Search list of formats for first depth/Z format.
155 default_depth_format(const GLuint formats
[], GLuint num
)
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
) {
165 return PIPE_FORMAT_NONE
;
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.
177 * Note: also used for glRenderbufferStorageEXT()
179 * Note: format and type may be GL_NONE (see renderbuffers)
181 * \return PIPE_FORMAT_NONE if error/problem.
184 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
185 GLenum format
, GLenum type
)
187 const GLuint
*supported
;
188 GLboolean allow
[PIPE_FORMAT_COUNT
];
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;
199 switch (internalFormat
) {
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
;
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
;
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
;
217 return default_rgba_format(supported
, n
);
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
;
226 return default_rgba_format(supported
, n
);
232 return default_rgba_format(supported
, n
);
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
);
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
);
249 return default_rgba_format(supported
, n
);
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
);
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
);
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
);
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
);
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
);
303 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
304 if (allow
[PIPE_FORMAT_YCBCR
])
305 return PIPE_FORMAT_YCBCR
;
308 if (allow
[PIPE_FORMAT_YCBCR_REV
])
309 return PIPE_FORMAT_YCBCR_REV
;
311 return PIPE_FORMAT_NONE
;
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
;
321 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
322 return &_mesa_texformat_rgb_dxt1
;
324 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
325 return &_mesa_texformat_rgba_dxt1
;
329 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
330 return &_mesa_texformat_rgba_dxt3
;
332 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
333 return &_mesa_texformat_rgba_dxt5
;
336 case GL_DEPTH_COMPONENT16
:
337 if (allow
[PIPE_FORMAT_U_Z16
])
338 return PIPE_FORMAT_U_Z16
;
340 case GL_DEPTH_COMPONENT24
:
341 if (allow
[PIPE_FORMAT_S8_Z24
])
342 return PIPE_FORMAT_S8_Z24
;
344 case GL_DEPTH_COMPONENT32
:
345 if (allow
[PIPE_FORMAT_U_Z32
])
346 return PIPE_FORMAT_U_Z32
;
348 case GL_DEPTH_COMPONENT
:
349 return default_depth_format(supported
, n
);
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
;
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
;
369 return PIPE_FORMAT_NONE
;
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.
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...
383 const struct gl_texture_format
*
384 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
385 GLenum format
, GLenum type
)
388 struct intel_context
*intel
= intel_context(ctx
);
389 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
391 const GLboolean do32bpt
= 1;
394 switch (internalFormat
) {
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
;
402 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
403 return &_mesa_texformat_argb4444
;
405 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
406 return &_mesa_texformat_argb1555
;
409 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
413 case GL_COMPRESSED_RGB
:
414 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
415 return &_mesa_texformat_rgb565
;
417 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
423 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
427 return &_mesa_texformat_argb4444
;
430 return &_mesa_texformat_argb1555
;
436 return &_mesa_texformat_argb8888
;
441 return &_mesa_texformat_rgb565
;
448 case GL_COMPRESSED_ALPHA
:
449 return &_mesa_texformat_a8
;
457 case GL_COMPRESSED_LUMINANCE
:
458 return &_mesa_texformat_l8
;
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
;
476 case GL_COMPRESSED_INTENSITY
:
477 return &_mesa_texformat_i8
;
480 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
481 return &_mesa_texformat_ycbcr
;
483 return &_mesa_texformat_ycbcr_rev
;
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
;
492 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
493 return &_mesa_texformat_rgb_dxt1
;
495 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
496 return &_mesa_texformat_rgba_dxt1
;
500 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
501 return &_mesa_texformat_rgba_dxt3
;
503 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
504 return &_mesa_texformat_rgba_dxt5
;
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
;
512 case GL_DEPTH_STENCIL_EXT
:
513 case GL_DEPTH24_STENCIL8_EXT
:
514 return &_mesa_texformat_z24_s8
;
517 fprintf(stderr
, "unexpected texture format %s in %s\n",
518 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
522 return NULL
; /* never get here */