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_A8R8G8B8_UNORM
,
287 PIPE_FORMAT_B8G8R8A8_UNORM
,
288 PIPE_FORMAT_R8G8B8A8_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 user-created renderbuffers.
339 * \return PIPE_FORMAT_NONE if error/problem.
342 st_choose_renderbuffer_format(struct pipe_context
*pipe
, GLint internalFormat
)
344 uint surfType
= PIPE_SURFACE
;
346 switch (internalFormat
) {
349 case GL_COMPRESSED_RGBA
:
352 case GL_COMPRESSED_RGB
:
356 return default_rgba_format( pipe
, surfType
);
358 return default_deep_rgba_format( pipe
, surfType
);
362 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A4R4G4B4_UNORM
, surfType
))
363 return PIPE_FORMAT_A4R4G4B4_UNORM
;
364 return default_rgba_format( pipe
, surfType
);
367 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
368 return PIPE_FORMAT_A1R5G5B5_UNORM
;
369 return default_rgba_format( pipe
, surfType
);
375 return default_rgba_format( pipe
, surfType
);
380 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A1R5G5B5_UNORM
, surfType
))
381 return PIPE_FORMAT_A1R5G5B5_UNORM
;
382 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_R5G6B5_UNORM
, surfType
))
383 return PIPE_FORMAT_R5G6B5_UNORM
;
384 return default_rgba_format( pipe
, surfType
);
391 case GL_COMPRESSED_ALPHA
:
392 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
, surfType
))
393 return PIPE_FORMAT_U_A8
;
394 return default_rgba_format( pipe
, surfType
);
402 case GL_COMPRESSED_LUMINANCE
:
403 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_L8
, surfType
))
404 return PIPE_FORMAT_U_A8
;
405 return default_rgba_format( pipe
, surfType
);
408 case GL_LUMINANCE_ALPHA
:
409 case GL_LUMINANCE4_ALPHA4
:
410 case GL_LUMINANCE6_ALPHA2
:
411 case GL_LUMINANCE8_ALPHA8
:
412 case GL_LUMINANCE12_ALPHA4
:
413 case GL_LUMINANCE12_ALPHA12
:
414 case GL_LUMINANCE16_ALPHA16
:
415 case GL_COMPRESSED_LUMINANCE_ALPHA
:
416 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_L8
, surfType
))
417 return PIPE_FORMAT_U_A8_L8
;
418 return default_rgba_format( pipe
, surfType
);
425 case GL_COMPRESSED_INTENSITY
:
426 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
, surfType
))
427 return PIPE_FORMAT_U_I8
;
428 return default_rgba_format( pipe
, surfType
);
431 /* not supported for renderbuffers */
433 return PIPE_FORMAT_NONE
;
434 case GL_COMPRESSED_RGB_FXT1_3DFX
:
435 return &_mesa_texformat_rgb_fxt1
;
436 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
437 return &_mesa_texformat_rgba_fxt1
;
441 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
442 return &_mesa_texformat_rgb_dxt1
;
444 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
445 return &_mesa_texformat_rgba_dxt1
;
449 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
450 return &_mesa_texformat_rgba_dxt3
;
452 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
453 return &_mesa_texformat_rgba_dxt5
;
456 case GL_DEPTH_COMPONENT16
:
457 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z16_UNORM
, surfType
))
458 return PIPE_FORMAT_Z16_UNORM
;
460 case GL_DEPTH_COMPONENT24
:
461 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
462 return PIPE_FORMAT_S8Z24_UNORM
;
463 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
464 return PIPE_FORMAT_Z24S8_UNORM
;
466 case GL_DEPTH_COMPONENT32
:
467 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z32_UNORM
, surfType
))
468 return PIPE_FORMAT_Z32_UNORM
;
470 case GL_DEPTH_COMPONENT
:
471 return default_depth_format( pipe
, surfType
);
473 case GL_STENCIL_INDEX
:
474 case GL_STENCIL_INDEX1_EXT
:
475 case GL_STENCIL_INDEX4_EXT
:
476 case GL_STENCIL_INDEX8_EXT
:
477 case GL_STENCIL_INDEX16_EXT
:
478 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_S8
, surfType
))
479 return PIPE_FORMAT_U_S8
;
480 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
481 return PIPE_FORMAT_S8Z24_UNORM
;
482 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
483 return PIPE_FORMAT_Z24S8_UNORM
;
484 return PIPE_FORMAT_NONE
;
486 case GL_DEPTH_STENCIL_EXT
:
487 case GL_DEPTH24_STENCIL8_EXT
:
488 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8Z24_UNORM
, surfType
))
489 return PIPE_FORMAT_S8Z24_UNORM
;
490 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_Z24S8_UNORM
, surfType
))
491 return PIPE_FORMAT_Z24S8_UNORM
;
492 return PIPE_FORMAT_NONE
;
495 return PIPE_FORMAT_NONE
;
501 /* It works out that this function is fine for all the supported
502 * hardware. However, there is still a need to map the formats onto
503 * hardware descriptors.
505 /* Note that the i915 can actually support many more formats than
506 * these if we take the step of simply swizzling the colors
507 * immediately after sampling...
509 const struct gl_texture_format
*
510 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
511 GLenum format
, GLenum type
)
514 struct intel_context
*intel
= intel_context(ctx
);
515 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
517 const GLboolean do32bpt
= 1;
522 switch (internalFormat
) {
525 case GL_COMPRESSED_RGBA
:
526 if (format
== GL_BGRA
) {
527 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
528 return &_mesa_texformat_argb8888
;
530 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
531 return &_mesa_texformat_argb4444
;
533 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
534 return &_mesa_texformat_argb1555
;
537 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
541 case GL_COMPRESSED_RGB
:
542 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
543 return &_mesa_texformat_rgb565
;
545 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
551 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
555 return &_mesa_texformat_argb4444
;
558 return &_mesa_texformat_argb1555
;
564 return &_mesa_texformat_argb8888
;
569 return &_mesa_texformat_rgb565
;
576 case GL_COMPRESSED_ALPHA
:
577 return &_mesa_texformat_a8
;
585 case GL_COMPRESSED_LUMINANCE
:
586 return &_mesa_texformat_l8
;
589 case GL_LUMINANCE_ALPHA
:
590 case GL_LUMINANCE4_ALPHA4
:
591 case GL_LUMINANCE6_ALPHA2
:
592 case GL_LUMINANCE8_ALPHA8
:
593 case GL_LUMINANCE12_ALPHA4
:
594 case GL_LUMINANCE12_ALPHA12
:
595 case GL_LUMINANCE16_ALPHA16
:
596 case GL_COMPRESSED_LUMINANCE_ALPHA
:
597 return &_mesa_texformat_al88
;
604 case GL_COMPRESSED_INTENSITY
:
605 return &_mesa_texformat_i8
;
608 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
609 return &_mesa_texformat_ycbcr
;
611 return &_mesa_texformat_ycbcr_rev
;
613 case GL_COMPRESSED_RGB_FXT1_3DFX
:
614 return &_mesa_texformat_rgb_fxt1
;
615 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
616 return &_mesa_texformat_rgba_fxt1
;
620 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
621 return &_mesa_texformat_rgb_dxt1
;
623 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
624 return &_mesa_texformat_rgba_dxt1
;
628 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
629 return &_mesa_texformat_rgba_dxt3
;
631 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
632 return &_mesa_texformat_rgba_dxt5
;
634 case GL_DEPTH_COMPONENT
:
635 case GL_DEPTH_COMPONENT16
:
636 case GL_DEPTH_COMPONENT24
:
637 case GL_DEPTH_COMPONENT32
:
638 return &_mesa_texformat_z16
;
640 case GL_DEPTH_STENCIL_EXT
:
641 case GL_DEPTH24_STENCIL8_EXT
:
642 return &_mesa_texformat_z24_s8
;
645 fprintf(stderr
, "unexpected texture format %s in %s\n",
646 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
650 return NULL
; /* never get here */