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
);
158 pinfo
->size
= format_size( info
) / 8;
160 /* Luminance & Intensity bits */
161 if( pf_swizzle_x(info
) == PIPE_FORMAT_COMP_R
&&
162 pf_swizzle_y(info
) == PIPE_FORMAT_COMP_R
&&
163 pf_swizzle_z(info
) == PIPE_FORMAT_COMP_R
) {
164 if( pf_swizzle_w(info
) == PIPE_FORMAT_COMP_R
) {
165 pinfo
->luminance_bits
= 0;
166 pinfo
->intensity_bits
= pinfo
->red_bits
;
169 pinfo
->luminance_bits
= pinfo
->red_bits
;
170 pinfo
->intensity_bits
= 0;
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 return PIPE_FORMAT_NONE
;
327 * Choose the PIPE_FORMAT_ to use for storing a texture image based
328 * on the user's internalFormat, format and type parameters.
329 * We query the pipe device for a list of formats which it supports
330 * and choose from them.
331 * If we find a device that needs a more intricate selection mechanism,
332 * this function _could_ get pushed down into the pipe device.
334 * Note: also used for glRenderbufferStorageEXT()
336 * Note: format and type may be GL_NONE (see renderbuffers)
338 * \return PIPE_FORMAT_NONE if error/problem.
341 st_choose_pipe_format(struct pipe_context
*pipe
, GLint internalFormat
,
342 GLenum format
, GLenum type
)
344 switch (internalFormat
) {
347 case GL_COMPRESSED_RGBA
:
348 if (format
== GL_BGRA
) {
349 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
350 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_R8_G8_B8
))
351 return PIPE_FORMAT_U_A8_R8_G8_B8
;
353 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
354 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A4_R4_G4_B4
))
355 return PIPE_FORMAT_U_A4_R4_G4_B4
;
357 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
358 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
359 return PIPE_FORMAT_U_A1_R5_G5_B5
;
362 return default_rgba_format( pipe
);
366 case GL_COMPRESSED_RGB
:
367 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
368 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_R5_G6_B5
))
369 return PIPE_FORMAT_U_R5_G6_B5
;
371 return default_rgba_format( pipe
);
376 return default_rgba_format( pipe
);
378 return default_deep_rgba_format( pipe
);
382 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A4_R4_G4_B4
))
383 return PIPE_FORMAT_U_A4_R4_G4_B4
;
384 return default_rgba_format( pipe
);
387 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
388 return PIPE_FORMAT_U_A1_R5_G5_B5
;
389 return default_rgba_format( pipe
);
395 return default_rgba_format( pipe
);
400 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A1_R5_G5_B5
))
401 return PIPE_FORMAT_U_A1_R5_G5_B5
;
402 return default_rgba_format( pipe
);
409 case GL_COMPRESSED_ALPHA
:
410 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
))
411 return PIPE_FORMAT_U_A8
;
412 return default_rgba_format( pipe
);
420 case GL_COMPRESSED_LUMINANCE
:
421 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8
))
422 return PIPE_FORMAT_U_A8
;
423 return default_rgba_format( pipe
);
426 case GL_LUMINANCE_ALPHA
:
427 case GL_LUMINANCE4_ALPHA4
:
428 case GL_LUMINANCE6_ALPHA2
:
429 case GL_LUMINANCE8_ALPHA8
:
430 case GL_LUMINANCE12_ALPHA4
:
431 case GL_LUMINANCE12_ALPHA12
:
432 case GL_LUMINANCE16_ALPHA16
:
433 case GL_COMPRESSED_LUMINANCE_ALPHA
:
434 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_L8
))
435 return PIPE_FORMAT_U_A8_L8
;
436 return default_rgba_format( pipe
);
443 case GL_COMPRESSED_INTENSITY
:
444 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
))
445 return PIPE_FORMAT_U_I8
;
446 return default_rgba_format( pipe
);
449 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
) {
450 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR
))
451 return PIPE_FORMAT_YCBCR
;
454 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_YCBCR_REV
))
455 return PIPE_FORMAT_YCBCR_REV
;
457 return PIPE_FORMAT_NONE
;
460 case GL_COMPRESSED_RGB_FXT1_3DFX
:
461 return &_mesa_texformat_rgb_fxt1
;
462 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
463 return &_mesa_texformat_rgba_fxt1
;
467 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
468 return &_mesa_texformat_rgb_dxt1
;
470 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
471 return &_mesa_texformat_rgba_dxt1
;
475 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
476 return &_mesa_texformat_rgba_dxt3
;
478 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
479 return &_mesa_texformat_rgba_dxt5
;
482 case GL_DEPTH_COMPONENT16
:
483 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z16
))
484 return PIPE_FORMAT_U_Z16
;
486 case GL_DEPTH_COMPONENT24
:
487 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
488 return PIPE_FORMAT_S8_Z24
;
490 case GL_DEPTH_COMPONENT32
:
491 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_Z32
))
492 return PIPE_FORMAT_U_Z32
;
494 case GL_DEPTH_COMPONENT
:
495 return default_depth_format( pipe
);
497 case GL_STENCIL_INDEX
:
498 case GL_STENCIL_INDEX1_EXT
:
499 case GL_STENCIL_INDEX4_EXT
:
500 case GL_STENCIL_INDEX8_EXT
:
501 case GL_STENCIL_INDEX16_EXT
:
502 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_S8
))
503 return PIPE_FORMAT_U_S8
;
504 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
505 return PIPE_FORMAT_S8_Z24
;
506 return PIPE_FORMAT_NONE
;
508 case GL_DEPTH_STENCIL_EXT
:
509 case GL_DEPTH24_STENCIL8_EXT
:
510 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_S8_Z24
))
511 return PIPE_FORMAT_S8_Z24
;
512 return PIPE_FORMAT_NONE
;
515 return PIPE_FORMAT_NONE
;
521 /* It works out that this function is fine for all the supported
522 * hardware. However, there is still a need to map the formats onto
523 * hardware descriptors.
525 /* Note that the i915 can actually support many more formats than
526 * these if we take the step of simply swizzling the colors
527 * immediately after sampling...
529 const struct gl_texture_format
*
530 st_ChooseTextureFormat(GLcontext
* ctx
, GLint internalFormat
,
531 GLenum format
, GLenum type
)
534 struct intel_context
*intel
= intel_context(ctx
);
535 const GLboolean do32bpt
= (intel
->intelScreen
->front
.cpp
== 4);
537 const GLboolean do32bpt
= 1;
540 switch (internalFormat
) {
543 case GL_COMPRESSED_RGBA
:
544 if (format
== GL_BGRA
) {
545 if (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
546 return &_mesa_texformat_argb8888
;
548 else if (type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
549 return &_mesa_texformat_argb4444
;
551 else if (type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
552 return &_mesa_texformat_argb1555
;
555 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
559 case GL_COMPRESSED_RGB
:
560 if (format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
561 return &_mesa_texformat_rgb565
;
563 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_rgb565
;
569 return do32bpt
? &_mesa_texformat_argb8888
: &_mesa_texformat_argb4444
;
573 return &_mesa_texformat_argb4444
;
576 return &_mesa_texformat_argb1555
;
582 return &_mesa_texformat_argb8888
;
587 return &_mesa_texformat_rgb565
;
594 case GL_COMPRESSED_ALPHA
:
595 return &_mesa_texformat_a8
;
603 case GL_COMPRESSED_LUMINANCE
:
604 return &_mesa_texformat_l8
;
607 case GL_LUMINANCE_ALPHA
:
608 case GL_LUMINANCE4_ALPHA4
:
609 case GL_LUMINANCE6_ALPHA2
:
610 case GL_LUMINANCE8_ALPHA8
:
611 case GL_LUMINANCE12_ALPHA4
:
612 case GL_LUMINANCE12_ALPHA12
:
613 case GL_LUMINANCE16_ALPHA16
:
614 case GL_COMPRESSED_LUMINANCE_ALPHA
:
615 return &_mesa_texformat_al88
;
622 case GL_COMPRESSED_INTENSITY
:
623 return &_mesa_texformat_i8
;
626 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
)
627 return &_mesa_texformat_ycbcr
;
629 return &_mesa_texformat_ycbcr_rev
;
631 case GL_COMPRESSED_RGB_FXT1_3DFX
:
632 return &_mesa_texformat_rgb_fxt1
;
633 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
634 return &_mesa_texformat_rgba_fxt1
;
638 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
639 return &_mesa_texformat_rgb_dxt1
;
641 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
642 return &_mesa_texformat_rgba_dxt1
;
646 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
647 return &_mesa_texformat_rgba_dxt3
;
649 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
650 return &_mesa_texformat_rgba_dxt5
;
652 case GL_DEPTH_COMPONENT
:
653 case GL_DEPTH_COMPONENT16
:
654 case GL_DEPTH_COMPONENT24
:
655 case GL_DEPTH_COMPONENT32
:
656 return &_mesa_texformat_z16
;
658 case GL_DEPTH_STENCIL_EXT
:
659 case GL_DEPTH24_STENCIL8_EXT
:
660 return &_mesa_texformat_z24_s8
;
663 fprintf(stderr
, "unexpected texture format %s in %s\n",
664 _mesa_lookup_enum_by_nr(internalFormat
), __FUNCTION__
);
668 return NULL
; /* never get here */