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
;
113 "PIPE_FORMAT: X(%u), Y(%u), Z(%u), W(%u)\n",
121 if (format
== PIPE_FORMAT_U_A1_R5_G5_B5
|| format
== PIPE_FORMAT_U_R5_G6_B5
) {
122 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
127 size
= format_max_bits( info
);
129 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
130 pinfo
->datatype
= GL_UNSIGNED_BYTE
;
132 pinfo
->datatype
= GL_BYTE
;
134 else if (size
== 16) {
135 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
136 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
138 pinfo
->datatype
= GL_SHORT
;
141 assert( size
<= 32 );
142 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
143 pinfo
->datatype
= GL_UNSIGNED_INT
;
145 pinfo
->datatype
= GL_INT
;
150 pinfo
->red_bits
= format_bits( info
, PIPE_FORMAT_COMP_R
);
151 pinfo
->green_bits
= format_bits( info
, PIPE_FORMAT_COMP_G
);
152 pinfo
->blue_bits
= format_bits( info
, PIPE_FORMAT_COMP_B
);
153 pinfo
->alpha_bits
= format_bits( info
, PIPE_FORMAT_COMP_A
);
154 pinfo
->depth_bits
= format_bits( info
, PIPE_FORMAT_COMP_Z
);
155 pinfo
->stencil_bits
= format_bits( info
, PIPE_FORMAT_COMP_S
);
156 pinfo
->luminance_bits
= 0;
157 pinfo
->intensity_bits
= 0;
160 pinfo
->size
= format_size( info
) / 8;
162 /* Luminance & Intensity bits */
163 if( pf_swizzle_x(info
) == PIPE_FORMAT_COMP_R
&&
164 pf_swizzle_y(info
) == PIPE_FORMAT_COMP_R
&&
165 pf_swizzle_z(info
) == PIPE_FORMAT_COMP_R
) {
166 if( pf_swizzle_w(info
) == PIPE_FORMAT_COMP_R
) {
167 pinfo
->intensity_bits
= pinfo
->red_bits
;
170 pinfo
->luminance_bits
= pinfo
->red_bits
;
176 if (pinfo
->depth_bits
) {
177 if (pinfo
->stencil_bits
) {
178 pinfo
->base_format
= GL_DEPTH_STENCIL_EXT
;
181 pinfo
->base_format
= GL_DEPTH_COMPONENT
;
184 else if (pinfo
->stencil_bits
) {
185 pinfo
->base_format
= GL_STENCIL_INDEX
;
188 pinfo
->base_format
= GL_RGBA
;
192 pipe_format_ycbcr_t info
;
194 assert( pf_layout(format
) == PIPE_FORMAT_LAYOUT_YCBCR
);
204 "ST_FORMAT: R(%u), G(%u), B(%u), A(%u), Z(%u), S(%u)\n",
210 pinfo
->stencil_bits
);
213 pinfo
->format
= format
;
220 * Return bytes per pixel for the given format.
223 st_sizeof_format(GLuint pipeFormat
)
225 struct pipe_format_info info
;
226 if (!st_get_format_info( pipeFormat
, &info
)) {
235 * Return bytes per pixel for the given format.
238 st_format_datatype(GLuint pipeFormat
)
240 struct pipe_format_info info
;
241 if (!st_get_format_info( pipeFormat
, &info
)) {
245 return info
.datatype
;
250 st_mesa_format_to_pipe_format(GLuint mesaFormat
)
252 switch (mesaFormat
) {
254 case MESA_FORMAT_ARGB8888_REV
:
255 case MESA_FORMAT_ARGB8888
:
256 return PIPE_FORMAT_U_A8_R8_G8_B8
;
257 case MESA_FORMAT_AL88
:
258 return PIPE_FORMAT_U_A8_L8
;
260 return PIPE_FORMAT_U_A8
;
262 return PIPE_FORMAT_U_L8
;
264 return PIPE_FORMAT_U_I8
;
265 case MESA_FORMAT_Z16
:
266 return PIPE_FORMAT_U_Z16
;
274 * Search list of formats for first RGBA format.
278 struct pipe_context
*pipe
)
280 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R8_G8_B8_A8
)) {
281 return PIPE_FORMAT_U_R8_G8_B8_A8
;
283 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_R8_G8_B8
)) {
284 return PIPE_FORMAT_U_A8_R8_G8_B8
;
286 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R5_G6_B5
)) {
287 return PIPE_FORMAT_U_R5_G6_B5
;
289 return PIPE_FORMAT_NONE
;
294 * Search list of formats for first RGBA format with >8 bits/channel.
297 default_deep_rgba_format(
298 struct pipe_context
*pipe
)
300 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S_R16_G16_B16_A16
)) {
301 return PIPE_FORMAT_S_R16_G16_B16_A16
;
303 return PIPE_FORMAT_NONE
;
308 * Search list of formats for first depth/Z format.
311 default_depth_format(
312 struct pipe_context
*pipe
)
314 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z16
)) {
315 return PIPE_FORMAT_U_Z16
;
317 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z32
)) {
318 return PIPE_FORMAT_U_Z32
;
320 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
)) {
321 return PIPE_FORMAT_S8_Z24
;
323 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
)) {
324 return PIPE_FORMAT_Z24_S8
;
326 return PIPE_FORMAT_NONE
;
330 * Choose the PIPE_FORMAT_ to use for storing a texture image based
331 * on the user's internalFormat, format and type parameters.
332 * We query the pipe device for a list of formats which it supports
333 * and choose from them.
334 * If we find a device that needs a more intricate selection mechanism,
335 * this function _could_ get pushed down into the pipe device.
337 * Note: also used for glRenderbufferStorageEXT()
339 * Note: format and type may be GL_NONE (see renderbuffers)
341 * \return PIPE_FORMAT_NONE if error/problem.
344 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
345 GLenum format
, GLenum type
)
347 switch (internalFormat
) {
350 case GL_COMPRESSED_RGBA
:
351 if (format
== GL_BGRA
) {
352 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
353 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_R8_G8_B8
))
354 return PIPE_FORMAT_U_A8_R8_G8_B8
;
356 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
357 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A4_R4_G4_B4
))
358 return PIPE_FORMAT_U_A4_R4_G4_B4
;
360 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
361 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
362 return PIPE_FORMAT_U_A1_R5_G5_B5
;
365 return default_rgba_format( pipe
);
369 case GL_COMPRESSED_RGB
:
370 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
371 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R5_G6_B5
))
372 return PIPE_FORMAT_U_R5_G6_B5
;
374 return default_rgba_format( pipe
);
379 return default_rgba_format( pipe
);
381 return default_deep_rgba_format( pipe
);
385 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A4_R4_G4_B4
))
386 return PIPE_FORMAT_U_A4_R4_G4_B4
;
387 return default_rgba_format( pipe
);
390 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
391 return PIPE_FORMAT_U_A1_R5_G5_B5
;
392 return default_rgba_format( pipe
);
398 return default_rgba_format( pipe
);
403 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
404 return PIPE_FORMAT_U_A1_R5_G5_B5
;
405 return default_rgba_format( pipe
);
412 case GL_COMPRESSED_ALPHA
:
413 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
))
414 return PIPE_FORMAT_U_A8
;
415 return default_rgba_format( pipe
);
423 case GL_COMPRESSED_LUMINANCE
:
424 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
))
425 return PIPE_FORMAT_U_A8
;
426 return default_rgba_format( pipe
);
429 case GL_LUMINANCE_ALPHA
:
430 case GL_LUMINANCE4_ALPHA4
:
431 case GL_LUMINANCE6_ALPHA2
:
432 case GL_LUMINANCE8_ALPHA8
:
433 case GL_LUMINANCE12_ALPHA4
:
434 case GL_LUMINANCE12_ALPHA12
:
435 case GL_LUMINANCE16_ALPHA16
:
436 case GL_COMPRESSED_LUMINANCE_ALPHA
:
437 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_L8
))
438 return PIPE_FORMAT_U_A8_L8
;
439 return default_rgba_format( pipe
);
446 case GL_COMPRESSED_INTENSITY
:
447 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
))
448 return PIPE_FORMAT_U_I8
;
449 return default_rgba_format( pipe
);
452 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
453 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR
))
454 return PIPE_FORMAT_YCBCR
;
457 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR_REV
))
458 return PIPE_FORMAT_YCBCR_REV
;
460 return PIPE_FORMAT_NONE
;
463 case GL_COMPRESSED_RGB_FXT1_3DFX
:
464 return &_mesa_texformat_rgb_fxt1
;
465 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
466 return &_mesa_texformat_rgba_fxt1
;
470 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
471 return &_mesa_texformat_rgb_dxt1
;
473 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
474 return &_mesa_texformat_rgba_dxt1
;
478 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
479 return &_mesa_texformat_rgba_dxt3
;
481 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
482 return &_mesa_texformat_rgba_dxt5
;
485 case GL_DEPTH_COMPONENT16
:
486 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z16
))
487 return PIPE_FORMAT_U_Z16
;
489 case GL_DEPTH_COMPONENT24
:
490 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
491 return PIPE_FORMAT_S8_Z24
;
492 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
))
493 return PIPE_FORMAT_Z24_S8
;
495 case GL_DEPTH_COMPONENT32
:
496 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z32
))
497 return PIPE_FORMAT_U_Z32
;
499 case GL_DEPTH_COMPONENT
:
500 return default_depth_format( pipe
);
502 case GL_STENCIL_INDEX
:
503 case GL_STENCIL_INDEX1_EXT
:
504 case GL_STENCIL_INDEX4_EXT
:
505 case GL_STENCIL_INDEX8_EXT
:
506 case GL_STENCIL_INDEX16_EXT
:
507 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_S8
))
508 return PIPE_FORMAT_U_S8
;
509 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
510 return PIPE_FORMAT_S8_Z24
;
511 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
))
512 return PIPE_FORMAT_Z24_S8
;
513 return PIPE_FORMAT_NONE
;
515 case GL_DEPTH_STENCIL_EXT
:
516 case GL_DEPTH24_STENCIL8_EXT
:
517 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
518 return PIPE_FORMAT_S8_Z24
;
519 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24_S8
))
520 return PIPE_FORMAT_Z24_S8
;
521 return PIPE_FORMAT_NONE
;
524 return PIPE_FORMAT_NONE
;
530 /* It works out that this function is fine for all the supported
531 * hardware. However, there is still a need to map the formats onto
532 * hardware descriptors.
534 /* Note that the i915 can actually support many more formats than
535 * these if we take the step of simply swizzling the colors
536 * immediately after sampling...
538 const struct gl_texture_format
*
539 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
540 GLenum format
, GLenum type
)
543 struct intel_context
*intel
= intel_context(ctx
);
544 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
546 const GLboolean do32bpt
= 1;
549 switch (internalFormat
) {
552 case GL_COMPRESSED_RGBA
:
553 if (format
== GL_BGRA
) {
554 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
555 return &_mesa_texformat_argb8888
;
557 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
558 return &_mesa_texformat_argb4444
;
560 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
561 return &_mesa_texformat_argb1555
;
564 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
568 case GL_COMPRESSED_RGB
:
569 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
570 return &_mesa_texformat_rgb565
;
572 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
578 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
582 return &_mesa_texformat_argb4444
;
585 return &_mesa_texformat_argb1555
;
591 return &_mesa_texformat_argb8888
;
596 return &_mesa_texformat_rgb565
;
603 case GL_COMPRESSED_ALPHA
:
604 return &_mesa_texformat_a8
;
612 case GL_COMPRESSED_LUMINANCE
:
613 return &_mesa_texformat_l8
;
616 case GL_LUMINANCE_ALPHA
:
617 case GL_LUMINANCE4_ALPHA4
:
618 case GL_LUMINANCE6_ALPHA2
:
619 case GL_LUMINANCE8_ALPHA8
:
620 case GL_LUMINANCE12_ALPHA4
:
621 case GL_LUMINANCE12_ALPHA12
:
622 case GL_LUMINANCE16_ALPHA16
:
623 case GL_COMPRESSED_LUMINANCE_ALPHA
:
624 return &_mesa_texformat_al88
;
631 case GL_COMPRESSED_INTENSITY
:
632 return &_mesa_texformat_i8
;
635 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
636 return &_mesa_texformat_ycbcr
;
638 return &_mesa_texformat_ycbcr_rev
;
640 case GL_COMPRESSED_RGB_FXT1_3DFX
:
641 return &_mesa_texformat_rgb_fxt1
;
642 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
643 return &_mesa_texformat_rgba_fxt1
;
647 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
648 return &_mesa_texformat_rgb_dxt1
;
650 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
651 return &_mesa_texformat_rgba_dxt1
;
655 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
656 return &_mesa_texformat_rgba_dxt3
;
658 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
659 return &_mesa_texformat_rgba_dxt5
;
661 case GL_DEPTH_COMPONENT
:
662 case GL_DEPTH_COMPONENT16
:
663 case GL_DEPTH_COMPONENT24
:
664 case GL_DEPTH_COMPONENT32
:
665 return &_mesa_texformat_z16
;
667 case GL_DEPTH_STENCIL_EXT
:
668 case GL_DEPTH24_STENCIL8_EXT
:
669 return &_mesa_texformat_z24_s8
;
672 fprintf(stderr
, "unexpected texture format %s in %s\n",
673 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
677 return NULL
; /* never get here */