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"
39 #include "main/macros.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "st_context.h"
44 #include "st_format.h"
48 pipe_format_rgbazs_t info
,
53 if (pf_swizzle_x(info
) == comp
) {
54 size
= pf_size_x(info
);
56 else if (pf_swizzle_y(info
) == comp
) {
57 size
= pf_size_y(info
);
59 else if (pf_swizzle_z(info
) == comp
) {
60 size
= pf_size_z(info
);
62 else if (pf_swizzle_w(info
) == comp
) {
63 size
= pf_size_w(info
);
68 return size
<< (pf_exp8(info
) * 3);
73 pipe_format_rgbazs_t info
)
75 GLuint size
= format_bits( info
, PIPE_FORMAT_COMP_R
);
77 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_G
) );
78 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_B
) );
79 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_A
) );
80 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_Z
) );
81 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_S
) );
87 pipe_format_rgbazs_t info
)
90 format_bits( info
, PIPE_FORMAT_COMP_R
) +
91 format_bits( info
, PIPE_FORMAT_COMP_G
) +
92 format_bits( info
, PIPE_FORMAT_COMP_B
) +
93 format_bits( info
, PIPE_FORMAT_COMP_A
) +
94 format_bits( info
, PIPE_FORMAT_COMP_Z
) +
95 format_bits( info
, PIPE_FORMAT_COMP_S
);
104 struct pipe_format_info
*pinfo
)
106 if (pf_layout(format
) == PIPE_FORMAT_LAYOUT_RGBAZS
) {
107 pipe_format_rgbazs_t info
;
115 pf_sprint_name( fmtname
, format
);
123 if (format
== PIPE_FORMAT_U_A1_R5_G5_B5
|| format
== PIPE_FORMAT_U_R5_G6_B5
) {
124 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
129 size
= format_max_bits( info
);
131 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
132 pinfo
->datatype
= GL_UNSIGNED_BYTE
;
134 pinfo
->datatype
= GL_BYTE
;
136 else if (size
== 16) {
137 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
138 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
140 pinfo
->datatype
= GL_SHORT
;
143 assert( size
<= 32 );
144 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
145 pinfo
->datatype
= GL_UNSIGNED_INT
;
147 pinfo
->datatype
= GL_INT
;
152 pinfo
->red_bits
= format_bits( info
, PIPE_FORMAT_COMP_R
);
153 pinfo
->green_bits
= format_bits( info
, PIPE_FORMAT_COMP_G
);
154 pinfo
->blue_bits
= format_bits( info
, PIPE_FORMAT_COMP_B
);
155 pinfo
->alpha_bits
= format_bits( info
, PIPE_FORMAT_COMP_A
);
156 pinfo
->depth_bits
= format_bits( info
, PIPE_FORMAT_COMP_Z
);
157 pinfo
->stencil_bits
= format_bits( info
, PIPE_FORMAT_COMP_S
);
158 pinfo
->luminance_bits
= 0;
159 pinfo
->intensity_bits
= 0;
162 pinfo
->size
= format_size( info
) / 8;
164 /* Luminance & Intensity bits */
165 if( pf_swizzle_x(info
) == PIPE_FORMAT_COMP_R
&&
166 pf_swizzle_y(info
) == PIPE_FORMAT_COMP_R
&&
167 pf_swizzle_z(info
) == PIPE_FORMAT_COMP_R
) {
168 if( pf_swizzle_w(info
) == PIPE_FORMAT_COMP_R
) {
169 pinfo
->intensity_bits
= pinfo
->red_bits
;
172 pinfo
->luminance_bits
= pinfo
->red_bits
;
178 if (pinfo
->depth_bits
) {
179 if (pinfo
->stencil_bits
) {
180 pinfo
->base_format
= GL_DEPTH_STENCIL_EXT
;
183 pinfo
->base_format
= GL_DEPTH_COMPONENT
;
186 else if (pinfo
->stencil_bits
) {
187 pinfo
->base_format
= GL_STENCIL_INDEX
;
190 pinfo
->base_format
= GL_RGBA
;
194 pipe_format_ycbcr_t info
;
196 assert( pf_layout(format
) == PIPE_FORMAT_LAYOUT_YCBCR
);
206 "ST_FORMAT: R(%u), G(%u), B(%u), A(%u), Z(%u), S(%u)\n",
212 pinfo
->stencil_bits
);
215 pinfo
->format
= format
;
222 * Return bytes per pixel for the given format.
225 st_sizeof_format(GLuint pipeFormat
)
227 struct pipe_format_info info
;
228 if (!st_get_format_info( pipeFormat
, &info
)) {
237 * Return bytes per pixel for the given format.
240 st_format_datatype(GLuint pipeFormat
)
242 struct pipe_format_info info
;
243 if (!st_get_format_info( pipeFormat
, &info
)) {
247 return info
.datatype
;
252 st_mesa_format_to_pipe_format(GLuint mesaFormat
)
254 switch (mesaFormat
) {
256 case MESA_FORMAT_ARGB8888_REV
:
257 case MESA_FORMAT_ARGB8888
:
258 return PIPE_FORMAT_U_A8_R8_G8_B8
;
259 case MESA_FORMAT_ARGB4444
:
260 return PIPE_FORMAT_U_A4_R4_G4_B4
;
261 case MESA_FORMAT_AL88
:
262 return PIPE_FORMAT_U_A8_L8
;
264 return PIPE_FORMAT_U_A8
;
266 return PIPE_FORMAT_U_L8
;
268 return PIPE_FORMAT_U_I8
;
269 case MESA_FORMAT_Z16
:
270 return PIPE_FORMAT_U_Z16
;
278 * Search list of formats for first RGBA format.
282 struct pipe_context
*pipe
)
284 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R8_G8_B8_A8
)) {
285 return PIPE_FORMAT_U_R8_G8_B8_A8
;
287 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_R8_G8_B8
)) {
288 return PIPE_FORMAT_U_A8_R8_G8_B8
;
290 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R5_G6_B5
)) {
291 return PIPE_FORMAT_U_R5_G6_B5
;
293 return PIPE_FORMAT_NONE
;
298 * Search list of formats for first RGBA format with >8 bits/channel.
301 default_deep_rgba_format(
302 struct pipe_context
*pipe
)
304 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S_R16_G16_B16_A16
)) {
305 return PIPE_FORMAT_S_R16_G16_B16_A16
;
307 return PIPE_FORMAT_NONE
;
312 * Search list of formats for first depth/Z format.
315 default_depth_format(
316 struct pipe_context
*pipe
)
318 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z16
)) {
319 return PIPE_FORMAT_U_Z16
;
321 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z32
)) {
322 return PIPE_FORMAT_U_Z32
;
324 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
)) {
325 return PIPE_FORMAT_S8_Z24
;
327 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
)) {
328 return PIPE_FORMAT_Z24_S8
;
330 return PIPE_FORMAT_NONE
;
334 * Choose the PIPE_FORMAT_ to use for storing a texture image based
335 * on the user's internalFormat, format and type parameters.
336 * We query the pipe device for a list of formats which it supports
337 * and choose from them.
338 * If we find a device that needs a more intricate selection mechanism,
339 * this function _could_ get pushed down into the pipe device.
341 * Note: also used for glRenderbufferStorageEXT()
343 * Note: format and type may be GL_NONE (see renderbuffers)
345 * \return PIPE_FORMAT_NONE if error/problem.
348 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
349 GLenum format
, GLenum type
)
351 switch (internalFormat
) {
354 case GL_COMPRESSED_RGBA
:
355 if (format
== GL_BGRA
) {
356 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
357 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_R8_G8_B8
))
358 return PIPE_FORMAT_U_A8_R8_G8_B8
;
360 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
361 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A4_R4_G4_B4
))
362 return PIPE_FORMAT_U_A4_R4_G4_B4
;
364 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
365 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
366 return PIPE_FORMAT_U_A1_R5_G5_B5
;
369 return default_rgba_format( pipe
);
373 case GL_COMPRESSED_RGB
:
374 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
375 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R5_G6_B5
))
376 return PIPE_FORMAT_U_R5_G6_B5
;
378 return default_rgba_format( pipe
);
383 return default_rgba_format( pipe
);
385 return default_deep_rgba_format( pipe
);
389 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A4_R4_G4_B4
))
390 return PIPE_FORMAT_U_A4_R4_G4_B4
;
391 return default_rgba_format( pipe
);
394 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
395 return PIPE_FORMAT_U_A1_R5_G5_B5
;
396 return default_rgba_format( pipe
);
402 return default_rgba_format( pipe
);
407 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
408 return PIPE_FORMAT_U_A1_R5_G5_B5
;
409 return default_rgba_format( pipe
);
416 case GL_COMPRESSED_ALPHA
:
417 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
))
418 return PIPE_FORMAT_U_A8
;
419 return default_rgba_format( pipe
);
427 case GL_COMPRESSED_LUMINANCE
:
428 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
))
429 return PIPE_FORMAT_U_A8
;
430 return default_rgba_format( pipe
);
433 case GL_LUMINANCE_ALPHA
:
434 case GL_LUMINANCE4_ALPHA4
:
435 case GL_LUMINANCE6_ALPHA2
:
436 case GL_LUMINANCE8_ALPHA8
:
437 case GL_LUMINANCE12_ALPHA4
:
438 case GL_LUMINANCE12_ALPHA12
:
439 case GL_LUMINANCE16_ALPHA16
:
440 case GL_COMPRESSED_LUMINANCE_ALPHA
:
441 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_L8
))
442 return PIPE_FORMAT_U_A8_L8
;
443 return default_rgba_format( pipe
);
450 case GL_COMPRESSED_INTENSITY
:
451 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
))
452 return PIPE_FORMAT_U_I8
;
453 return default_rgba_format( pipe
);
456 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
457 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR
))
458 return PIPE_FORMAT_YCBCR
;
461 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR_REV
))
462 return PIPE_FORMAT_YCBCR_REV
;
464 return PIPE_FORMAT_NONE
;
467 case GL_COMPRESSED_RGB_FXT1_3DFX
:
468 return &_mesa_texformat_rgb_fxt1
;
469 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
470 return &_mesa_texformat_rgba_fxt1
;
474 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
475 return &_mesa_texformat_rgb_dxt1
;
477 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
478 return &_mesa_texformat_rgba_dxt1
;
482 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
483 return &_mesa_texformat_rgba_dxt3
;
485 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
486 return &_mesa_texformat_rgba_dxt5
;
489 case GL_DEPTH_COMPONENT16
:
490 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z16
))
491 return PIPE_FORMAT_U_Z16
;
493 case GL_DEPTH_COMPONENT24
:
494 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
495 return PIPE_FORMAT_S8_Z24
;
496 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
))
497 return PIPE_FORMAT_Z24_S8
;
499 case GL_DEPTH_COMPONENT32
:
500 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z32
))
501 return PIPE_FORMAT_U_Z32
;
503 case GL_DEPTH_COMPONENT
:
504 return default_depth_format( pipe
);
506 case GL_STENCIL_INDEX
:
507 case GL_STENCIL_INDEX1_EXT
:
508 case GL_STENCIL_INDEX4_EXT
:
509 case GL_STENCIL_INDEX8_EXT
:
510 case GL_STENCIL_INDEX16_EXT
:
511 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_S8
))
512 return PIPE_FORMAT_U_S8
;
513 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
514 return PIPE_FORMAT_S8_Z24
;
515 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
))
516 return PIPE_FORMAT_Z24_S8
;
517 return PIPE_FORMAT_NONE
;
519 case GL_DEPTH_STENCIL_EXT
:
520 case GL_DEPTH24_STENCIL8_EXT
:
521 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
522 return PIPE_FORMAT_S8_Z24
;
523 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
))
524 return PIPE_FORMAT_Z24_S8
;
525 return PIPE_FORMAT_NONE
;
528 return PIPE_FORMAT_NONE
;
534 /* It works out that this function is fine for all the supported
535 * hardware. However, there is still a need to map the formats onto
536 * hardware descriptors.
538 /* Note that the i915 can actually support many more formats than
539 * these if we take the step of simply swizzling the colors
540 * immediately after sampling...
542 const struct gl_texture_format
*
543 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
544 GLenum format
, GLenum type
)
547 struct intel_context
*intel
= intel_context(ctx
);
548 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
550 const GLboolean do32bpt
= 1;
553 switch (internalFormat
) {
556 case GL_COMPRESSED_RGBA
:
557 if (format
== GL_BGRA
) {
558 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
559 return &_mesa_texformat_argb8888
;
561 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
562 return &_mesa_texformat_argb4444
;
564 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
565 return &_mesa_texformat_argb1555
;
568 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
572 case GL_COMPRESSED_RGB
:
573 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
574 return &_mesa_texformat_rgb565
;
576 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
582 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
586 return &_mesa_texformat_argb4444
;
589 return &_mesa_texformat_argb1555
;
595 return &_mesa_texformat_argb8888
;
600 return &_mesa_texformat_rgb565
;
607 case GL_COMPRESSED_ALPHA
:
608 return &_mesa_texformat_a8
;
616 case GL_COMPRESSED_LUMINANCE
:
617 return &_mesa_texformat_l8
;
620 case GL_LUMINANCE_ALPHA
:
621 case GL_LUMINANCE4_ALPHA4
:
622 case GL_LUMINANCE6_ALPHA2
:
623 case GL_LUMINANCE8_ALPHA8
:
624 case GL_LUMINANCE12_ALPHA4
:
625 case GL_LUMINANCE12_ALPHA12
:
626 case GL_LUMINANCE16_ALPHA16
:
627 case GL_COMPRESSED_LUMINANCE_ALPHA
:
628 return &_mesa_texformat_al88
;
635 case GL_COMPRESSED_INTENSITY
:
636 return &_mesa_texformat_i8
;
639 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
640 return &_mesa_texformat_ycbcr
;
642 return &_mesa_texformat_ycbcr_rev
;
644 case GL_COMPRESSED_RGB_FXT1_3DFX
:
645 return &_mesa_texformat_rgb_fxt1
;
646 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
647 return &_mesa_texformat_rgba_fxt1
;
651 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
652 return &_mesa_texformat_rgb_dxt1
;
654 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
655 return &_mesa_texformat_rgba_dxt1
;
659 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
660 return &_mesa_texformat_rgba_dxt3
;
662 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
663 return &_mesa_texformat_rgba_dxt5
;
665 case GL_DEPTH_COMPONENT
:
666 case GL_DEPTH_COMPONENT16
:
667 case GL_DEPTH_COMPONENT24
:
668 case GL_DEPTH_COMPONENT32
:
669 return &_mesa_texformat_z16
;
671 case GL_DEPTH_STENCIL_EXT
:
672 case GL_DEPTH24_STENCIL8_EXT
:
673 return &_mesa_texformat_z24_s8
;
676 fprintf(stderr
, "unexpected texture format %s in %s\n",
677 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
681 return NULL
; /* never get here */