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 */
84 GL_DEPTH_COMPONENT
, /* base_format */
85 0, 0, 0, 0, 0, 0, /* color bits */
86 16, 0, /* depth, stencil */
91 GL_DEPTH_COMPONENT
, /* base_format */
92 0, 0, 0, 0, 0, 0, /* color bits */
93 32, 0, /* depth, stencil */
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 */
103 /* XXX lots more cases to add */
107 for (i
= 0; i
< sizeof(info
) / sizeof(info
[0]); i
++) {
108 if (info
[i
].format
== format
)
116 st_sizeof_format(GLuint pipeFormat
)
118 const struct pipe_format_info
*info
= st_get_format_info(pipeFormat
);
125 st_mesa_format_to_pipe_format(GLuint mesaFormat
)
127 switch (mesaFormat
) {
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
;
135 return PIPE_FORMAT_U_A8
;
137 return PIPE_FORMAT_U_L8
;
139 return PIPE_FORMAT_U_I8
;
140 case MESA_FORMAT_Z16
:
141 return PIPE_FORMAT_U_Z16
;
150 * Search list of formats for first RGBA format.
153 default_rgba_format(const GLuint formats
[], GLuint num
)
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
) {
163 return PIPE_FORMAT_NONE
;
168 * Search list of formats for first depth/Z format.
171 default_depth_format(const GLuint formats
[], GLuint num
)
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
) {
181 return PIPE_FORMAT_NONE
;
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.
193 * Note: also used for glRenderbufferStorageEXT()
195 * Note: format and type may be GL_NONE (see renderbuffers)
197 * \return PIPE_FORMAT_NONE if error/problem.
200 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
201 GLenum format
, GLenum type
)
203 const GLuint
*supported
;
204 GLboolean allow
[PIPE_FORMAT_COUNT
];
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;
215 switch (internalFormat
) {
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
;
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
;
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
;
233 return default_rgba_format(supported
, n
);
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
;
242 return default_rgba_format(supported
, n
);
248 return default_rgba_format(supported
, n
);
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
);
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
);
265 return default_rgba_format(supported
, n
);
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
);
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
);
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
);
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
);
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
);
319 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
320 if (allow
[PIPE_FORMAT_YCBCR
])
321 return PIPE_FORMAT_YCBCR
;
324 if (allow
[PIPE_FORMAT_YCBCR_REV
])
325 return PIPE_FORMAT_YCBCR_REV
;
327 return PIPE_FORMAT_NONE
;
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
;
337 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
338 return &_mesa_texformat_rgb_dxt1
;
340 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
341 return &_mesa_texformat_rgba_dxt1
;
345 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
346 return &_mesa_texformat_rgba_dxt3
;
348 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
349 return &_mesa_texformat_rgba_dxt5
;
352 case GL_DEPTH_COMPONENT16
:
353 if (allow
[PIPE_FORMAT_U_Z16
])
354 return PIPE_FORMAT_U_Z16
;
356 case GL_DEPTH_COMPONENT24
:
357 if (allow
[PIPE_FORMAT_S8_Z24
])
358 return PIPE_FORMAT_S8_Z24
;
360 case GL_DEPTH_COMPONENT32
:
361 if (allow
[PIPE_FORMAT_U_Z32
])
362 return PIPE_FORMAT_U_Z32
;
364 case GL_DEPTH_COMPONENT
:
365 return default_depth_format(supported
, n
);
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
;
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
;
385 return PIPE_FORMAT_NONE
;
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.
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...
399 const struct gl_texture_format
*
400 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
401 GLenum format
, GLenum type
)
404 struct intel_context
*intel
= intel_context(ctx
);
405 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
407 const GLboolean do32bpt
= 1;
410 switch (internalFormat
) {
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
;
418 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
419 return &_mesa_texformat_argb4444
;
421 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
422 return &_mesa_texformat_argb1555
;
425 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
429 case GL_COMPRESSED_RGB
:
430 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
431 return &_mesa_texformat_rgb565
;
433 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
439 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
443 return &_mesa_texformat_argb4444
;
446 return &_mesa_texformat_argb1555
;
452 return &_mesa_texformat_argb8888
;
457 return &_mesa_texformat_rgb565
;
464 case GL_COMPRESSED_ALPHA
:
465 return &_mesa_texformat_a8
;
473 case GL_COMPRESSED_LUMINANCE
:
474 return &_mesa_texformat_l8
;
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
;
492 case GL_COMPRESSED_INTENSITY
:
493 return &_mesa_texformat_i8
;
496 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
497 return &_mesa_texformat_ycbcr
;
499 return &_mesa_texformat_ycbcr_rev
;
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
;
508 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
509 return &_mesa_texformat_rgb_dxt1
;
511 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
512 return &_mesa_texformat_rgba_dxt1
;
516 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
517 return &_mesa_texformat_rgba_dxt3
;
519 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
520 return &_mesa_texformat_rgba_dxt5
;
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
;
528 case GL_DEPTH_STENCIL_EXT
:
529 case GL_DEPTH24_STENCIL8_EXT
:
530 return &_mesa_texformat_z24_s8
;
533 fprintf(stderr
, "unexpected texture format %s in %s\n",
534 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
538 return NULL
; /* never get here */