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 "pipe/p_screen.h"
44 #include "st_context.h"
45 #include "st_format.h"
49 pipe_format_rgbazs_t info
,
52 return pf_get_component_bits( (enum pipe_format
) info
, comp
);
57 pipe_format_rgbazs_t info
)
59 GLuint size
= format_bits( info
, PIPE_FORMAT_COMP_R
);
61 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_G
) );
62 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_B
) );
63 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_A
) );
64 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_Z
) );
65 size
= MAX2( size
, format_bits( info
, PIPE_FORMAT_COMP_S
) );
71 pipe_format_rgbazs_t info
)
74 format_bits( info
, PIPE_FORMAT_COMP_R
) +
75 format_bits( info
, PIPE_FORMAT_COMP_G
) +
76 format_bits( info
, PIPE_FORMAT_COMP_B
) +
77 format_bits( info
, PIPE_FORMAT_COMP_A
) +
78 format_bits( info
, PIPE_FORMAT_COMP_Z
) +
79 format_bits( info
, PIPE_FORMAT_COMP_S
);
86 st_get_format_info(enum pipe_format format
, struct pipe_format_info
*pinfo
)
88 if (pf_layout(format
) == PIPE_FORMAT_LAYOUT_RGBAZS
) {
89 pipe_format_rgbazs_t info
;
94 printf("%s\n", pf_name( format
) );
98 if (format
== PIPE_FORMAT_A1R5G5B5_UNORM
|| format
== PIPE_FORMAT_R5G6B5_UNORM
) {
99 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
104 size
= format_max_bits( info
);
106 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
107 pinfo
->datatype
= GL_UNSIGNED_BYTE
;
109 pinfo
->datatype
= GL_BYTE
;
111 else if (size
== 16) {
112 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
113 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
115 pinfo
->datatype
= GL_SHORT
;
118 assert( size
<= 32 );
119 if (pf_type(info
) == PIPE_FORMAT_TYPE_UNORM
)
120 pinfo
->datatype
= GL_UNSIGNED_INT
;
122 pinfo
->datatype
= GL_INT
;
127 pinfo
->red_bits
= format_bits( info
, PIPE_FORMAT_COMP_R
);
128 pinfo
->green_bits
= format_bits( info
, PIPE_FORMAT_COMP_G
);
129 pinfo
->blue_bits
= format_bits( info
, PIPE_FORMAT_COMP_B
);
130 pinfo
->alpha_bits
= format_bits( info
, PIPE_FORMAT_COMP_A
);
131 pinfo
->depth_bits
= format_bits( info
, PIPE_FORMAT_COMP_Z
);
132 pinfo
->stencil_bits
= format_bits( info
, PIPE_FORMAT_COMP_S
);
133 pinfo
->luminance_bits
= 0;
134 pinfo
->intensity_bits
= 0;
137 pinfo
->size
= format_size( info
) / 8;
139 /* Luminance & Intensity bits */
140 if( pf_swizzle_x(info
) == PIPE_FORMAT_COMP_R
&&
141 pf_swizzle_y(info
) == PIPE_FORMAT_COMP_R
&&
142 pf_swizzle_z(info
) == PIPE_FORMAT_COMP_R
) {
143 if( pf_swizzle_w(info
) == PIPE_FORMAT_COMP_R
) {
144 pinfo
->intensity_bits
= pinfo
->red_bits
;
147 pinfo
->luminance_bits
= pinfo
->red_bits
;
153 if (pinfo
->depth_bits
) {
154 if (pinfo
->stencil_bits
) {
155 pinfo
->base_format
= GL_DEPTH_STENCIL_EXT
;
158 pinfo
->base_format
= GL_DEPTH_COMPONENT
;
161 else if (pinfo
->stencil_bits
) {
162 pinfo
->base_format
= GL_STENCIL_INDEX
;
165 pinfo
->base_format
= GL_RGBA
;
168 else if (pf_layout(format
) == PIPE_FORMAT_LAYOUT_YCBCR
) {
169 pinfo
->base_format
= GL_YCBCR_MESA
;
170 pinfo
->datatype
= GL_UNSIGNED_SHORT
;
171 pinfo
->size
= 2; /* two bytes per "texel" */
174 /* compressed format? */
180 "ST_FORMAT: R(%u), G(%u), B(%u), A(%u), Z(%u), S(%u)\n",
186 pinfo
->stencil_bits
);
189 pinfo
->format
= format
;
196 * Return bytes per pixel for the given format.
199 st_sizeof_format(enum pipe_format format
)
201 struct pipe_format_info info
;
202 if (!st_get_format_info( format
, &info
)) {
211 * Return bytes per pixel for the given format.
214 st_format_datatype(enum pipe_format format
)
216 struct pipe_format_info info
;
217 if (!st_get_format_info( format
, &info
)) {
221 return info
.datatype
;
226 st_mesa_format_to_pipe_format(GLuint mesaFormat
)
228 switch (mesaFormat
) {
230 case MESA_FORMAT_ARGB8888_REV
:
231 case MESA_FORMAT_ARGB8888
:
232 return PIPE_FORMAT_A8R8G8B8_UNORM
;
233 case MESA_FORMAT_ARGB1555
:
234 return PIPE_FORMAT_A1R5G5B5_UNORM
;
235 case MESA_FORMAT_ARGB4444
:
236 return PIPE_FORMAT_A4R4G4B4_UNORM
;
237 case MESA_FORMAT_RGB565
:
238 return PIPE_FORMAT_R5G6B5_UNORM
;
239 case MESA_FORMAT_AL88
:
240 return PIPE_FORMAT_A8L8_UNORM
;
242 return PIPE_FORMAT_A8_UNORM
;
244 return PIPE_FORMAT_L8_UNORM
;
246 return PIPE_FORMAT_I8_UNORM
;
247 case MESA_FORMAT_Z16
:
248 return PIPE_FORMAT_Z16_UNORM
;
249 case MESA_FORMAT_Z32
:
250 return PIPE_FORMAT_Z32_UNORM
;
251 case MESA_FORMAT_Z24_S8
:
252 return PIPE_FORMAT_Z24S8_UNORM
;
253 case MESA_FORMAT_S8_Z24
:
254 return PIPE_FORMAT_S8Z24_UNORM
;
255 case MESA_FORMAT_YCBCR
:
256 return PIPE_FORMAT_YCBCR
;
257 #if FEATURE_texture_s3tc
258 case MESA_FORMAT_RGB_DXT1
:
259 return PIPE_FORMAT_DXT1_RGB
;
260 case MESA_FORMAT_RGBA_DXT1
:
261 return PIPE_FORMAT_DXT1_RGBA
;
262 case MESA_FORMAT_RGBA_DXT3
:
263 return PIPE_FORMAT_DXT3_RGBA
;
264 case MESA_FORMAT_RGBA_DXT5
:
265 return PIPE_FORMAT_DXT5_RGBA
;
274 * Find an RGBA format supported by the context/winsys.
276 static enum pipe_format
277 default_rgba_format(struct pipe_screen
*screen
,
278 enum pipe_texture_target target
,
282 static const enum pipe_format colorFormats
[] = {
283 PIPE_FORMAT_A8R8G8B8_UNORM
,
284 PIPE_FORMAT_B8G8R8A8_UNORM
,
285 PIPE_FORMAT_R8G8B8A8_UNORM
,
286 PIPE_FORMAT_R5G6B5_UNORM
289 for (i
= 0; i
< Elements(colorFormats
); i
++) {
290 if (screen
->is_format_supported( screen
, colorFormats
[i
], target
, tex_usage
, geom_flags
)) {
291 return colorFormats
[i
];
294 return PIPE_FORMAT_NONE
;
299 * Search list of formats for first RGBA format with >8 bits/channel.
301 static enum pipe_format
302 default_deep_rgba_format(struct pipe_screen
*screen
,
303 enum pipe_texture_target target
,
307 if (screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16B16A16_SNORM
, target
, tex_usage
, geom_flags
)) {
308 return PIPE_FORMAT_R16G16B16A16_SNORM
;
310 if (tex_usage
& PIPE_TEXTURE_USAGE_RENDER_TARGET
)
311 return default_rgba_format(screen
, target
, tex_usage
, geom_flags
);
313 return PIPE_FORMAT_NONE
;
318 * Find an Z format supported by the context/winsys.
320 static enum pipe_format
321 default_depth_format(struct pipe_screen
*screen
,
322 enum pipe_texture_target target
,
326 static const enum pipe_format zFormats
[] = {
327 PIPE_FORMAT_Z16_UNORM
,
328 PIPE_FORMAT_Z32_UNORM
,
329 PIPE_FORMAT_S8Z24_UNORM
,
330 PIPE_FORMAT_Z24S8_UNORM
333 for (i
= 0; i
< Elements(zFormats
); i
++) {
334 if (screen
->is_format_supported( screen
, zFormats
[i
], target
, tex_usage
, geom_flags
)) {
338 return PIPE_FORMAT_NONE
;
343 * Given an OpenGL internalFormat value for a texture or surface, return
344 * the best matching PIPE_FORMAT_x, or PIPE_FORMAT_NONE if there's no match.
345 * \param target one of PIPE_TEXTURE_x
346 * \param tex_usage either PIPE_TEXTURE_USAGE_RENDER_TARGET
347 * or PIPE_TEXTURE_USAGE_SAMPLER
350 st_choose_format(struct pipe_context
*pipe
, GLint internalFormat
,
351 enum pipe_texture_target target
, unsigned tex_usage
)
353 struct pipe_screen
*screen
= pipe
->screen
;
354 unsigned geom_flags
= 0;
356 switch (internalFormat
) {
359 case GL_COMPRESSED_RGBA
:
362 case GL_COMPRESSED_RGB
:
366 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
368 if (tex_usage
& PIPE_TEXTURE_USAGE_RENDER_TARGET
)
369 return default_deep_rgba_format( screen
, target
, tex_usage
, geom_flags
);
371 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
375 if (screen
->is_format_supported( screen
, PIPE_FORMAT_A4R4G4B4_UNORM
, target
, tex_usage
, geom_flags
))
376 return PIPE_FORMAT_A4R4G4B4_UNORM
;
377 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
380 if (screen
->is_format_supported( screen
, PIPE_FORMAT_A1R5G5B5_UNORM
, target
, tex_usage
, geom_flags
))
381 return PIPE_FORMAT_A1R5G5B5_UNORM
;
382 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
388 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
393 if (screen
->is_format_supported( screen
, PIPE_FORMAT_A1R5G5B5_UNORM
, target
, tex_usage
, geom_flags
))
394 return PIPE_FORMAT_A1R5G5B5_UNORM
;
395 if (screen
->is_format_supported( screen
, PIPE_FORMAT_R5G6B5_UNORM
, target
, tex_usage
, geom_flags
))
396 return PIPE_FORMAT_R5G6B5_UNORM
;
397 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
404 case GL_COMPRESSED_ALPHA
:
405 if (screen
->is_format_supported( screen
, PIPE_FORMAT_A8_UNORM
, target
, tex_usage
, geom_flags
))
406 return PIPE_FORMAT_A8_UNORM
;
407 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
415 case GL_COMPRESSED_LUMINANCE
:
416 if (screen
->is_format_supported( screen
, PIPE_FORMAT_L8_UNORM
, target
, tex_usage
, geom_flags
))
417 return PIPE_FORMAT_L8_UNORM
;
418 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
421 case GL_LUMINANCE_ALPHA
:
422 case GL_LUMINANCE4_ALPHA4
:
423 case GL_LUMINANCE6_ALPHA2
:
424 case GL_LUMINANCE8_ALPHA8
:
425 case GL_LUMINANCE12_ALPHA4
:
426 case GL_LUMINANCE12_ALPHA12
:
427 case GL_LUMINANCE16_ALPHA16
:
428 case GL_COMPRESSED_LUMINANCE_ALPHA
:
429 if (screen
->is_format_supported( screen
, PIPE_FORMAT_A8L8_UNORM
, target
, tex_usage
, geom_flags
))
430 return PIPE_FORMAT_A8L8_UNORM
;
431 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
438 case GL_COMPRESSED_INTENSITY
:
439 if (screen
->is_format_supported( screen
, PIPE_FORMAT_I8_UNORM
, target
, tex_usage
, geom_flags
))
440 return PIPE_FORMAT_I8_UNORM
;
441 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
444 if (screen
->is_format_supported(screen
, PIPE_FORMAT_YCBCR
,
445 target
, tex_usage
, geom_flags
)) {
446 return PIPE_FORMAT_YCBCR
;
448 if (screen
->is_format_supported(screen
, PIPE_FORMAT_YCBCR_REV
,
449 target
, tex_usage
, geom_flags
)) {
450 return PIPE_FORMAT_YCBCR_REV
;
452 return PIPE_FORMAT_NONE
;
456 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
457 return PIPE_FORMAT_DXT1_RGB
;
459 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
460 return PIPE_FORMAT_DXT1_RGBA
;
464 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
465 return PIPE_FORMAT_DXT3_RGBA
;
467 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
468 return PIPE_FORMAT_DXT5_RGBA
;
471 case GL_COMPRESSED_RGB_FXT1_3DFX
:
472 return PIPE_FORMAT_RGB_FXT1
;
473 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
474 return PIPE_FORMAT_RGB_FXT1
;
477 case GL_DEPTH_COMPONENT16
:
478 if (screen
->is_format_supported( screen
, PIPE_FORMAT_Z16_UNORM
, target
, tex_usage
, geom_flags
))
479 return PIPE_FORMAT_Z16_UNORM
;
481 case GL_DEPTH_COMPONENT24
:
482 if (screen
->is_format_supported( screen
, PIPE_FORMAT_S8Z24_UNORM
, target
, tex_usage
, geom_flags
))
483 return PIPE_FORMAT_S8Z24_UNORM
;
484 if (screen
->is_format_supported( screen
, PIPE_FORMAT_Z24S8_UNORM
, target
, tex_usage
, geom_flags
))
485 return PIPE_FORMAT_Z24S8_UNORM
;
487 case GL_DEPTH_COMPONENT32
:
488 if (screen
->is_format_supported( screen
, PIPE_FORMAT_Z32_UNORM
, target
, tex_usage
, geom_flags
))
489 return PIPE_FORMAT_Z32_UNORM
;
491 case GL_DEPTH_COMPONENT
:
492 return default_depth_format( screen
, target
, tex_usage
, geom_flags
);
494 case GL_STENCIL_INDEX
:
495 case GL_STENCIL_INDEX1_EXT
:
496 case GL_STENCIL_INDEX4_EXT
:
497 case GL_STENCIL_INDEX8_EXT
:
498 case GL_STENCIL_INDEX16_EXT
:
499 if (screen
->is_format_supported( screen
, PIPE_FORMAT_S8_UNORM
, target
, tex_usage
, geom_flags
))
500 return PIPE_FORMAT_S8_UNORM
;
501 if (screen
->is_format_supported( screen
, PIPE_FORMAT_S8Z24_UNORM
, target
, tex_usage
, geom_flags
))
502 return PIPE_FORMAT_S8Z24_UNORM
;
503 if (screen
->is_format_supported( screen
, PIPE_FORMAT_Z24S8_UNORM
, target
, tex_usage
, geom_flags
))
504 return PIPE_FORMAT_Z24S8_UNORM
;
505 return PIPE_FORMAT_NONE
;
507 case GL_DEPTH_STENCIL_EXT
:
508 case GL_DEPTH24_STENCIL8_EXT
:
509 if (screen
->is_format_supported( screen
, PIPE_FORMAT_S8Z24_UNORM
, target
, tex_usage
, geom_flags
))
510 return PIPE_FORMAT_S8Z24_UNORM
;
511 if (screen
->is_format_supported( screen
, PIPE_FORMAT_Z24S8_UNORM
, target
, tex_usage
, geom_flags
))
512 return PIPE_FORMAT_Z24S8_UNORM
;
513 return PIPE_FORMAT_NONE
;
517 case GL_COMPRESSED_SRGB_EXT
:
518 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
519 case GL_SRGB_ALPHA_EXT
:
520 case GL_SRGB8_ALPHA8_EXT
:
521 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
522 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
523 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
524 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
525 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
527 case GL_SLUMINANCE_ALPHA_EXT
:
528 case GL_SLUMINANCE8_ALPHA8_EXT
:
529 case GL_COMPRESSED_SLUMINANCE_EXT
:
530 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
531 if (screen
->is_format_supported( screen
, PIPE_FORMAT_A8L8_UNORM
, target
, tex_usage
, geom_flags
))
532 return PIPE_FORMAT_A8L8_UNORM
;
533 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
535 case GL_SLUMINANCE_EXT
:
536 case GL_SLUMINANCE8_EXT
:
537 if (screen
->is_format_supported( screen
, PIPE_FORMAT_L8_UNORM
, target
, tex_usage
, geom_flags
))
538 return PIPE_FORMAT_L8_UNORM
;
539 return default_rgba_format( screen
, target
, tex_usage
, geom_flags
);
542 return PIPE_FORMAT_NONE
;
548 is_stencil_format(GLenum format
)
551 case GL_STENCIL_INDEX
:
552 case GL_STENCIL_INDEX1_EXT
:
553 case GL_STENCIL_INDEX4_EXT
:
554 case GL_STENCIL_INDEX8_EXT
:
555 case GL_STENCIL_INDEX16_EXT
:
556 case GL_DEPTH_STENCIL_EXT
:
557 case GL_DEPTH24_STENCIL8_EXT
:
565 * Called by FBO code to choose a PIPE_FORMAT_ for drawing surfaces.
568 st_choose_renderbuffer_format(struct pipe_context
*pipe
, GLint internalFormat
)
571 if (is_stencil_format(internalFormat
))
572 usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
574 usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
575 return st_choose_format(pipe
, internalFormat
, PIPE_TEXTURE_2D
, usage
);
579 static const struct gl_texture_format
*
580 translate_gallium_format_to_mesa_format(enum pipe_format format
)
583 case PIPE_FORMAT_A8R8G8B8_UNORM
:
584 return &_mesa_texformat_argb8888
;
585 case PIPE_FORMAT_A1R5G5B5_UNORM
:
586 return &_mesa_texformat_argb1555
;
587 case PIPE_FORMAT_A4R4G4B4_UNORM
:
588 return &_mesa_texformat_argb4444
;
589 case PIPE_FORMAT_R5G6B5_UNORM
:
590 return &_mesa_texformat_rgb565
;
591 case PIPE_FORMAT_A8L8_UNORM
:
592 return &_mesa_texformat_al88
;
593 case PIPE_FORMAT_A8_UNORM
:
594 return &_mesa_texformat_a8
;
595 case PIPE_FORMAT_L8_UNORM
:
596 return &_mesa_texformat_l8
;
597 case PIPE_FORMAT_I8_UNORM
:
598 return &_mesa_texformat_i8
;
599 case PIPE_FORMAT_Z16_UNORM
:
600 return &_mesa_texformat_z16
;
601 case PIPE_FORMAT_Z32_UNORM
:
602 return &_mesa_texformat_z32
;
603 case PIPE_FORMAT_Z24S8_UNORM
:
604 return &_mesa_texformat_z24_s8
;
605 case PIPE_FORMAT_S8Z24_UNORM
:
606 return &_mesa_texformat_s8_z24
;
607 case PIPE_FORMAT_YCBCR
:
608 return &_mesa_texformat_ycbcr
;
609 case PIPE_FORMAT_YCBCR_REV
:
610 return &_mesa_texformat_ycbcr_rev
;
611 #if FEATURE_texture_s3tc
612 case PIPE_FORMAT_DXT1_RGB
:
613 return &_mesa_texformat_rgb_dxt1
;
614 case PIPE_FORMAT_DXT1_RGBA
:
615 return &_mesa_texformat_rgba_dxt1
;
616 case PIPE_FORMAT_DXT3_RGBA
:
617 return &_mesa_texformat_rgba_dxt3
;
618 case PIPE_FORMAT_DXT5_RGBA
:
619 return &_mesa_texformat_rgba_dxt5
;
621 /* XXX add additional cases */
630 * Called via ctx->Driver.chooseTextureFormat().
632 const struct gl_texture_format
*
633 st_ChooseTextureFormat(GLcontext
*ctx
, GLint internalFormat
,
634 GLenum format
, GLenum type
)
636 enum pipe_format pFormat
;
641 pFormat
= st_choose_format(ctx
->st
->pipe
, internalFormat
, PIPE_TEXTURE_2D
,
642 PIPE_TEXTURE_USAGE_SAMPLER
);
643 if (pFormat
== PIPE_FORMAT_NONE
)
646 return translate_gallium_format_to_mesa_format(pFormat
);