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_ARGB1555
:
258 return PIPE_FORMAT_A1R5G5B5_UNORM
;
259 case MESA_FORMAT_ARGB4444
:
260 return PIPE_FORMAT_A4R4G4B4_UNORM
;
261 case MESA_FORMAT_RGB565
:
262 return PIPE_FORMAT_R5G6B5_UNORM
;
263 case MESA_FORMAT_AL88
:
264 return PIPE_FORMAT_U_A8_L8
;
266 return PIPE_FORMAT_U_A8
;
268 return PIPE_FORMAT_U_L8
;
270 return PIPE_FORMAT_U_I8
;
271 case MESA_FORMAT_Z16
:
272 return PIPE_FORMAT_Z16_UNORM
;
280 * Find an RGBA format supported by the context/winsys.
283 default_rgba_format(struct pipe_context
*pipe
, uint type
)
285 static const enum pipe_format colorFormats
[] = {
286 PIPE_FORMAT_R8G8B8A8_UNORM
,
287 PIPE_FORMAT_A8R8G8B8_UNORM
,
288 PIPE_FORMAT_B8G8R8A8_UNORM
,
289 PIPE_FORMAT_R5G6B5_UNORM
292 for (i
= 0; i
< Elements(colorFormats
); i
++) {
293 if (pipe
->is_format_supported( pipe
, colorFormats
[i
], type
)) {
294 return colorFormats
[i
];
297 return PIPE_FORMAT_NONE
;
302 * Search list of formats for first RGBA format with >8 bits/channel.
305 default_deep_rgba_format(struct pipe_context
*pipe
, uint type
)
307 if (pipe
->is_format_supported(pipe
, PIPE_FORMAT_R16G16B16A16_SNORM
, type
)) {
308 return PIPE_FORMAT_R16G16B16A16_SNORM
;
310 return PIPE_FORMAT_NONE
;
315 * Find an Z format supported by the context/winsys.
318 default_depth_format(struct pipe_context
*pipe
, uint type
)
320 static const enum pipe_format zFormats
[] = {
321 PIPE_FORMAT_Z16_UNORM
,
322 PIPE_FORMAT_Z32_UNORM
,
323 PIPE_FORMAT_S8Z24_UNORM
,
324 PIPE_FORMAT_Z24S8_UNORM
327 for (i
= 0; i
< Elements(zFormats
); i
++) {
328 if (pipe
->is_format_supported( pipe
, zFormats
[i
], type
)) {
332 return PIPE_FORMAT_NONE
;
337 * Choose the PIPE_FORMAT_ to use for storing a texture image based
338 * on the user's internalFormat, format and type parameters.
339 * We query the pipe device for a list of formats which it supports
340 * and choose from them.
341 * If we find a device that needs a more intricate selection mechanism,
342 * this function _could_ get pushed down into the pipe device.
344 * Note: also used for glRenderbufferStorageEXT()
346 * Note: format and type may be GL_NONE (see renderbuffers)
348 * \return PIPE_FORMAT_NONE if error/problem.
351 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
352 GLenum format
, GLenum type
, uint surfType
)
354 assert(surfType
== PIPE_TEXTURE
||
355 surfType
== PIPE_SURFACE
||
356 surfType
== PIPE_SCREEN_SURFACE
);
358 switch (internalFormat
) {
361 case GL_COMPRESSED_RGBA
:
362 if (format
== GL_BGRA
) {
363 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
364 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, surfType
))
365 return PIPE_FORMAT_A8R8G8B8_UNORM
;
367 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
368 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A4R4G4B4_UNORM
, surfType
))
369 return PIPE_FORMAT_A4R4G4B4_UNORM
;
371 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
372 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
373 return PIPE_FORMAT_A1R5G5B5_UNORM
;
376 return default_rgba_format( pipe
, surfType
);
380 case GL_COMPRESSED_RGB
:
381 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
382 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_R5G6B5_UNORM
, surfType
))
383 return PIPE_FORMAT_R5G6B5_UNORM
;
385 return default_rgba_format( pipe
, surfType
);
390 return default_rgba_format( pipe
, surfType
);
392 return default_deep_rgba_format( pipe
, surfType
);
396 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A4R4G4B4_UNORM
, surfType
))
397 return PIPE_FORMAT_A4R4G4B4_UNORM
;
398 return default_rgba_format( pipe
, surfType
);
401 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
402 return PIPE_FORMAT_A1R5G5B5_UNORM
;
403 return default_rgba_format( pipe
, surfType
);
409 return default_rgba_format( pipe
, surfType
);
414 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
415 return PIPE_FORMAT_A1R5G5B5_UNORM
;
416 return default_rgba_format( pipe
, surfType
);
423 case GL_COMPRESSED_ALPHA
:
424 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
, surfType
))
425 return PIPE_FORMAT_U_A8
;
426 return default_rgba_format( pipe
, surfType
);
434 case GL_COMPRESSED_LUMINANCE
:
435 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
, surfType
))
436 return PIPE_FORMAT_U_A8
;
437 return default_rgba_format( pipe
, surfType
);
440 case GL_LUMINANCE_ALPHA
:
441 case GL_LUMINANCE4_ALPHA4
:
442 case GL_LUMINANCE6_ALPHA2
:
443 case GL_LUMINANCE8_ALPHA8
:
444 case GL_LUMINANCE12_ALPHA4
:
445 case GL_LUMINANCE12_ALPHA12
:
446 case GL_LUMINANCE16_ALPHA16
:
447 case GL_COMPRESSED_LUMINANCE_ALPHA
:
448 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_L8
, surfType
))
449 return PIPE_FORMAT_U_A8_L8
;
450 return default_rgba_format( pipe
, surfType
);
457 case GL_COMPRESSED_INTENSITY
:
458 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
, surfType
))
459 return PIPE_FORMAT_U_I8
;
460 return default_rgba_format( pipe
, surfType
);
463 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
464 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR
, surfType
))
465 return PIPE_FORMAT_YCBCR
;
468 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR_REV
, surfType
))
469 return PIPE_FORMAT_YCBCR_REV
;
471 return PIPE_FORMAT_NONE
;
474 case GL_COMPRESSED_RGB_FXT1_3DFX
:
475 return &_mesa_texformat_rgb_fxt1
;
476 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
477 return &_mesa_texformat_rgba_fxt1
;
481 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
482 return &_mesa_texformat_rgb_dxt1
;
484 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
485 return &_mesa_texformat_rgba_dxt1
;
489 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
490 return &_mesa_texformat_rgba_dxt3
;
492 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
493 return &_mesa_texformat_rgba_dxt5
;
496 case GL_DEPTH_COMPONENT16
:
497 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z16_UNORM
, surfType
))
498 return PIPE_FORMAT_Z16_UNORM
;
500 case GL_DEPTH_COMPONENT24
:
501 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
502 return PIPE_FORMAT_S8Z24_UNORM
;
503 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
504 return PIPE_FORMAT_Z24S8_UNORM
;
506 case GL_DEPTH_COMPONENT32
:
507 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z32_UNORM
, surfType
))
508 return PIPE_FORMAT_Z32_UNORM
;
510 case GL_DEPTH_COMPONENT
:
511 return default_depth_format( pipe
, surfType
);
513 case GL_STENCIL_INDEX
:
514 case GL_STENCIL_INDEX1_EXT
:
515 case GL_STENCIL_INDEX4_EXT
:
516 case GL_STENCIL_INDEX8_EXT
:
517 case GL_STENCIL_INDEX16_EXT
:
518 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_S8
, surfType
))
519 return PIPE_FORMAT_U_S8
;
520 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
521 return PIPE_FORMAT_S8Z24_UNORM
;
522 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
523 return PIPE_FORMAT_Z24S8_UNORM
;
524 return PIPE_FORMAT_NONE
;
526 case GL_DEPTH_STENCIL_EXT
:
527 case GL_DEPTH24_STENCIL8_EXT
:
528 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
529 return PIPE_FORMAT_S8Z24_UNORM
;
530 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
531 return PIPE_FORMAT_Z24S8_UNORM
;
532 return PIPE_FORMAT_NONE
;
535 return PIPE_FORMAT_NONE
;
541 /* It works out that this function is fine for all the supported
542 * hardware. However, there is still a need to map the formats onto
543 * hardware descriptors.
545 /* Note that the i915 can actually support many more formats than
546 * these if we take the step of simply swizzling the colors
547 * immediately after sampling...
549 const struct gl_texture_format
*
550 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
551 GLenum format
, GLenum type
)
554 struct intel_context
*intel
= intel_context(ctx
);
555 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
557 const GLboolean do32bpt
= 1;
562 switch (internalFormat
) {
565 case GL_COMPRESSED_RGBA
:
566 if (format
== GL_BGRA
) {
567 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
568 return &_mesa_texformat_argb8888
;
570 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
571 return &_mesa_texformat_argb4444
;
573 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
574 return &_mesa_texformat_argb1555
;
577 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
581 case GL_COMPRESSED_RGB
:
582 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
583 return &_mesa_texformat_rgb565
;
585 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
591 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
595 return &_mesa_texformat_argb4444
;
598 return &_mesa_texformat_argb1555
;
604 return &_mesa_texformat_argb8888
;
609 return &_mesa_texformat_rgb565
;
616 case GL_COMPRESSED_ALPHA
:
617 return &_mesa_texformat_a8
;
625 case GL_COMPRESSED_LUMINANCE
:
626 return &_mesa_texformat_l8
;
629 case GL_LUMINANCE_ALPHA
:
630 case GL_LUMINANCE4_ALPHA4
:
631 case GL_LUMINANCE6_ALPHA2
:
632 case GL_LUMINANCE8_ALPHA8
:
633 case GL_LUMINANCE12_ALPHA4
:
634 case GL_LUMINANCE12_ALPHA12
:
635 case GL_LUMINANCE16_ALPHA16
:
636 case GL_COMPRESSED_LUMINANCE_ALPHA
:
637 return &_mesa_texformat_al88
;
644 case GL_COMPRESSED_INTENSITY
:
645 return &_mesa_texformat_i8
;
648 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
649 return &_mesa_texformat_ycbcr
;
651 return &_mesa_texformat_ycbcr_rev
;
653 case GL_COMPRESSED_RGB_FXT1_3DFX
:
654 return &_mesa_texformat_rgb_fxt1
;
655 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
656 return &_mesa_texformat_rgba_fxt1
;
660 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
661 return &_mesa_texformat_rgb_dxt1
;
663 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
664 return &_mesa_texformat_rgba_dxt1
;
668 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
669 return &_mesa_texformat_rgba_dxt3
;
671 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
672 return &_mesa_texformat_rgba_dxt5
;
674 case GL_DEPTH_COMPONENT
:
675 case GL_DEPTH_COMPONENT16
:
676 case GL_DEPTH_COMPONENT24
:
677 case GL_DEPTH_COMPONENT32
:
678 return &_mesa_texformat_z16
;
680 case GL_DEPTH_STENCIL_EXT
:
681 case GL_DEPTH24_STENCIL8_EXT
:
682 return &_mesa_texformat_z24_s8
;
685 fprintf(stderr
, "unexpected texture format %s in %s\n",
686 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
690 return NULL
; /* never get here */