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
);
102 st_get_format_info(enum pipe_format format
, struct pipe_format_info
*pinfo
)
104 if (pf_layout(format
) == PIPE_FORMAT_LAYOUT_RGBAZS
) {
105 pipe_format_rgbazs_t info
;
113 pf_sprint_name( fmtname
, format
);
121 if (format
== PIPE_FORMAT_A1R5G5B5_UNORM
|| format
== PIPE_FORMAT_R5G6B5_UNORM
) {
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(enum pipe_format format
)
225 struct pipe_format_info info
;
226 if (!st_get_format_info( format
, &info
)) {
235 * Return bytes per pixel for the given format.
238 st_format_datatype(enum pipe_format format
)
240 struct pipe_format_info info
;
241 if (!st_get_format_info( format
, &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_A8R8G8B8_UNORM
;
257 case MESA_FORMAT_ARGB4444
:
258 return PIPE_FORMAT_A4R4G4B4_UNORM
;
259 case MESA_FORMAT_AL88
:
260 return PIPE_FORMAT_U_A8_L8
;
262 return PIPE_FORMAT_U_A8
;
264 return PIPE_FORMAT_U_L8
;
266 return PIPE_FORMAT_U_I8
;
267 case MESA_FORMAT_Z16
:
268 return PIPE_FORMAT_Z16_UNORM
;
276 * Find an RGBA format supported by the context/winsys.
279 default_rgba_format(struct pipe_context
*pipe
, uint type
)
281 static const enum pipe_format colorFormats
[] = {
282 PIPE_FORMAT_R8G8B8A8_UNORM
,
283 PIPE_FORMAT_A8R8G8B8_UNORM
,
284 PIPE_FORMAT_B8G8R8A8_UNORM
,
285 PIPE_FORMAT_R5G6B5_UNORM
288 for (i
= 0; i
< Elements(colorFormats
); i
++) {
289 if (pipe
->is_format_supported( pipe
, colorFormats
[i
], type
)) {
290 return colorFormats
[i
];
293 return PIPE_FORMAT_NONE
;
298 * Search list of formats for first RGBA format with >8 bits/channel.
301 default_deep_rgba_format(struct pipe_context
*pipe
, uint type
)
303 if (pipe
->is_format_supported(pipe
, PIPE_FORMAT_R16G16B16A16_SNORM
, type
)) {
304 return PIPE_FORMAT_R16G16B16A16_SNORM
;
306 return PIPE_FORMAT_NONE
;
311 * Find an Z format supported by the context/winsys.
314 default_depth_format(struct pipe_context
*pipe
, uint type
)
316 static const enum pipe_format zFormats
[] = {
317 PIPE_FORMAT_Z16_UNORM
,
318 PIPE_FORMAT_Z32_UNORM
,
319 PIPE_FORMAT_S8Z24_UNORM
,
320 PIPE_FORMAT_Z24S8_UNORM
323 for (i
= 0; i
< Elements(zFormats
); i
++) {
324 if (pipe
->is_format_supported( pipe
, zFormats
[i
], type
)) {
328 return PIPE_FORMAT_NONE
;
333 * Choose the PIPE_FORMAT_ to use for storing a texture image based
334 * on the user's internalFormat, format and type parameters.
335 * We query the pipe device for a list of formats which it supports
336 * and choose from them.
337 * If we find a device that needs a more intricate selection mechanism,
338 * this function _could_ get pushed down into the pipe device.
340 * Note: also used for glRenderbufferStorageEXT()
342 * Note: format and type may be GL_NONE (see renderbuffers)
344 * \return PIPE_FORMAT_NONE if error/problem.
347 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
348 GLenum format
, GLenum type
, uint surfType
)
350 assert(surfType
== PIPE_TEXTURE
||
351 surfType
== PIPE_SURFACE
||
352 surfType
== PIPE_SCREEN_SURFACE
);
354 switch (internalFormat
) {
357 case GL_COMPRESSED_RGBA
:
358 if (format
== GL_BGRA
) {
359 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
360 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, surfType
))
361 return PIPE_FORMAT_A8R8G8B8_UNORM
;
363 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
364 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A4R4G4B4_UNORM
, surfType
))
365 return PIPE_FORMAT_A4R4G4B4_UNORM
;
367 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
368 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
369 return PIPE_FORMAT_A1R5G5B5_UNORM
;
372 return default_rgba_format( pipe
, surfType
);
376 case GL_COMPRESSED_RGB
:
377 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
378 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_R5G6B5_UNORM
, surfType
))
379 return PIPE_FORMAT_R5G6B5_UNORM
;
381 return default_rgba_format( pipe
, surfType
);
386 return default_rgba_format( pipe
, surfType
);
388 return default_deep_rgba_format( pipe
, surfType
);
392 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A4R4G4B4_UNORM
, surfType
))
393 return PIPE_FORMAT_A4R4G4B4_UNORM
;
394 return default_rgba_format( pipe
, surfType
);
397 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
398 return PIPE_FORMAT_A1R5G5B5_UNORM
;
399 return default_rgba_format( pipe
, surfType
);
405 return default_rgba_format( pipe
, surfType
);
410 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
411 return PIPE_FORMAT_A1R5G5B5_UNORM
;
412 return default_rgba_format( pipe
, surfType
);
419 case GL_COMPRESSED_ALPHA
:
420 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
, surfType
))
421 return PIPE_FORMAT_U_A8
;
422 return default_rgba_format( pipe
, surfType
);
430 case GL_COMPRESSED_LUMINANCE
:
431 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
, surfType
))
432 return PIPE_FORMAT_U_A8
;
433 return default_rgba_format( pipe
, surfType
);
436 case GL_LUMINANCE_ALPHA
:
437 case GL_LUMINANCE4_ALPHA4
:
438 case GL_LUMINANCE6_ALPHA2
:
439 case GL_LUMINANCE8_ALPHA8
:
440 case GL_LUMINANCE12_ALPHA4
:
441 case GL_LUMINANCE12_ALPHA12
:
442 case GL_LUMINANCE16_ALPHA16
:
443 case GL_COMPRESSED_LUMINANCE_ALPHA
:
444 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_L8
, surfType
))
445 return PIPE_FORMAT_U_A8_L8
;
446 return default_rgba_format( pipe
, surfType
);
453 case GL_COMPRESSED_INTENSITY
:
454 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
, surfType
))
455 return PIPE_FORMAT_U_I8
;
456 return default_rgba_format( pipe
, surfType
);
459 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
460 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR
, surfType
))
461 return PIPE_FORMAT_YCBCR
;
464 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR_REV
, surfType
))
465 return PIPE_FORMAT_YCBCR_REV
;
467 return PIPE_FORMAT_NONE
;
470 case GL_COMPRESSED_RGB_FXT1_3DFX
:
471 return &_mesa_texformat_rgb_fxt1
;
472 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
473 return &_mesa_texformat_rgba_fxt1
;
477 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
478 return &_mesa_texformat_rgb_dxt1
;
480 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
481 return &_mesa_texformat_rgba_dxt1
;
485 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
486 return &_mesa_texformat_rgba_dxt3
;
488 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
489 return &_mesa_texformat_rgba_dxt5
;
492 case GL_DEPTH_COMPONENT16
:
493 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z16_UNORM
, surfType
))
494 return PIPE_FORMAT_Z16_UNORM
;
496 case GL_DEPTH_COMPONENT24
:
497 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
498 return PIPE_FORMAT_S8Z24_UNORM
;
499 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
500 return PIPE_FORMAT_Z24S8_UNORM
;
502 case GL_DEPTH_COMPONENT32
:
503 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z32_UNORM
, surfType
))
504 return PIPE_FORMAT_Z32_UNORM
;
506 case GL_DEPTH_COMPONENT
:
507 return default_depth_format( pipe
, surfType
);
509 case GL_STENCIL_INDEX
:
510 case GL_STENCIL_INDEX1_EXT
:
511 case GL_STENCIL_INDEX4_EXT
:
512 case GL_STENCIL_INDEX8_EXT
:
513 case GL_STENCIL_INDEX16_EXT
:
514 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_S8
, surfType
))
515 return PIPE_FORMAT_U_S8
;
516 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
517 return PIPE_FORMAT_S8Z24_UNORM
;
518 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
519 return PIPE_FORMAT_Z24S8_UNORM
;
520 return PIPE_FORMAT_NONE
;
522 case GL_DEPTH_STENCIL_EXT
:
523 case GL_DEPTH24_STENCIL8_EXT
:
524 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
525 return PIPE_FORMAT_S8Z24_UNORM
;
526 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
527 return PIPE_FORMAT_Z24S8_UNORM
;
528 return PIPE_FORMAT_NONE
;
531 return PIPE_FORMAT_NONE
;
537 /* It works out that this function is fine for all the supported
538 * hardware. However, there is still a need to map the formats onto
539 * hardware descriptors.
541 /* Note that the i915 can actually support many more formats than
542 * these if we take the step of simply swizzling the colors
543 * immediately after sampling...
545 const struct gl_texture_format
*
546 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
547 GLenum format
, GLenum type
)
550 struct intel_context
*intel
= intel_context(ctx
);
551 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
553 const GLboolean do32bpt
= 1;
558 switch (internalFormat
) {
561 case GL_COMPRESSED_RGBA
:
562 if (format
== GL_BGRA
) {
563 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
564 return &_mesa_texformat_argb8888
;
566 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
567 return &_mesa_texformat_argb4444
;
569 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
570 return &_mesa_texformat_argb1555
;
573 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
577 case GL_COMPRESSED_RGB
:
578 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
579 return &_mesa_texformat_rgb565
;
581 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
587 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
591 return &_mesa_texformat_argb4444
;
594 return &_mesa_texformat_argb1555
;
600 return &_mesa_texformat_argb8888
;
605 return &_mesa_texformat_rgb565
;
612 case GL_COMPRESSED_ALPHA
:
613 return &_mesa_texformat_a8
;
621 case GL_COMPRESSED_LUMINANCE
:
622 return &_mesa_texformat_l8
;
625 case GL_LUMINANCE_ALPHA
:
626 case GL_LUMINANCE4_ALPHA4
:
627 case GL_LUMINANCE6_ALPHA2
:
628 case GL_LUMINANCE8_ALPHA8
:
629 case GL_LUMINANCE12_ALPHA4
:
630 case GL_LUMINANCE12_ALPHA12
:
631 case GL_LUMINANCE16_ALPHA16
:
632 case GL_COMPRESSED_LUMINANCE_ALPHA
:
633 return &_mesa_texformat_al88
;
640 case GL_COMPRESSED_INTENSITY
:
641 return &_mesa_texformat_i8
;
644 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
645 return &_mesa_texformat_ycbcr
;
647 return &_mesa_texformat_ycbcr_rev
;
649 case GL_COMPRESSED_RGB_FXT1_3DFX
:
650 return &_mesa_texformat_rgb_fxt1
;
651 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
652 return &_mesa_texformat_rgba_fxt1
;
656 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
657 return &_mesa_texformat_rgb_dxt1
;
659 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
660 return &_mesa_texformat_rgba_dxt1
;
664 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
665 return &_mesa_texformat_rgba_dxt3
;
667 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
668 return &_mesa_texformat_rgba_dxt5
;
670 case GL_DEPTH_COMPONENT
:
671 case GL_DEPTH_COMPONENT16
:
672 case GL_DEPTH_COMPONENT24
:
673 case GL_DEPTH_COMPONENT32
:
674 return &_mesa_texformat_z16
;
676 case GL_DEPTH_STENCIL_EXT
:
677 case GL_DEPTH24_STENCIL8_EXT
:
678 return &_mesa_texformat_z24_s8
;
681 fprintf(stderr
, "unexpected texture format %s in %s\n",
682 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
686 return NULL
; /* never get here */