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_mesa_format_to_pipe_format(GLuint mesaFormat
)
104 switch (mesaFormat
) {
106 case MESA_FORMAT_ARGB8888_REV
:
107 case MESA_FORMAT_ARGB8888
:
108 return PIPE_FORMAT_U_A8_R8_G8_B8
;
117 * Search list of formats for first RGBA format.
120 default_rgba_format(const GLuint formats
[], GLuint num
)
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
) {
130 return PIPE_FORMAT_NONE
;
135 * Search list of formats for first depth/Z format.
138 default_depth_format(const GLuint formats
[], GLuint num
)
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
) {
148 return PIPE_FORMAT_NONE
;
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.
160 * Note: also used for glRenderbufferStorageEXT()
162 * Note: format and type may be GL_NONE (see renderbuffers)
164 * \return PIPE_FORMAT_NONE if error/problem.
167 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
168 GLenum format
, GLenum type
)
170 const GLuint
*supported
;
171 GLboolean allow
[PIPE_FORMAT_COUNT
];
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;
182 switch (internalFormat
) {
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
;
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
;
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
;
200 return default_rgba_format(supported
, n
);
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
;
209 return default_rgba_format(supported
, n
);
215 return default_rgba_format(supported
, n
);
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
);
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
);
232 return default_rgba_format(supported
, n
);
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
);
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
);
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
);
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
);
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
);
286 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
287 if (allow
[PIPE_FORMAT_YCBCR
])
288 return PIPE_FORMAT_YCBCR
;
291 if (allow
[PIPE_FORMAT_YCBCR_REV
])
292 return PIPE_FORMAT_YCBCR_REV
;
294 return PIPE_FORMAT_NONE
;
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
;
304 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
305 return &_mesa_texformat_rgb_dxt1
;
307 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
308 return &_mesa_texformat_rgba_dxt1
;
312 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
313 return &_mesa_texformat_rgba_dxt3
;
315 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
316 return &_mesa_texformat_rgba_dxt5
;
319 case GL_DEPTH_COMPONENT16
:
320 if (allow
[PIPE_FORMAT_U_Z16
])
321 return PIPE_FORMAT_U_Z16
;
323 case GL_DEPTH_COMPONENT24
:
324 if (allow
[PIPE_FORMAT_S8_Z24
])
325 return PIPE_FORMAT_S8_Z24
;
327 case GL_DEPTH_COMPONENT32
:
328 if (allow
[PIPE_FORMAT_U_Z32
])
329 return PIPE_FORMAT_U_Z32
;
331 case GL_DEPTH_COMPONENT
:
332 return default_depth_format(supported
, n
);
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
;
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
;
352 return PIPE_FORMAT_NONE
;
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.
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...
366 const struct gl_texture_format
*
367 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
368 GLenum format
, GLenum type
)
371 struct intel_context
*intel
= intel_context(ctx
);
372 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
374 const GLboolean do32bpt
= 1;
377 switch (internalFormat
) {
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
;
385 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
386 return &_mesa_texformat_argb4444
;
388 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
389 return &_mesa_texformat_argb1555
;
392 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
396 case GL_COMPRESSED_RGB
:
397 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
398 return &_mesa_texformat_rgb565
;
400 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
406 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
410 return &_mesa_texformat_argb4444
;
413 return &_mesa_texformat_argb1555
;
419 return &_mesa_texformat_argb8888
;
424 return &_mesa_texformat_rgb565
;
431 case GL_COMPRESSED_ALPHA
:
432 return &_mesa_texformat_a8
;
440 case GL_COMPRESSED_LUMINANCE
:
441 return &_mesa_texformat_l8
;
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
;
459 case GL_COMPRESSED_INTENSITY
:
460 return &_mesa_texformat_i8
;
463 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
464 return &_mesa_texformat_ycbcr
;
466 return &_mesa_texformat_ycbcr_rev
;
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
;
475 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
476 return &_mesa_texformat_rgb_dxt1
;
478 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
479 return &_mesa_texformat_rgba_dxt1
;
483 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
484 return &_mesa_texformat_rgba_dxt3
;
486 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
487 return &_mesa_texformat_rgba_dxt5
;
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
;
495 case GL_DEPTH_STENCIL_EXT
:
496 case GL_DEPTH24_STENCIL8_EXT
:
497 return &_mesa_texformat_z24_s8
;
500 fprintf(stderr
, "unexpected texture format %s in %s\n",
501 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
505 return NULL
; /* never get here */