1 /* $Id: texformat.c,v 1.16 2002/09/27 02:45:38 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Gareth Hughes <gareth@valinux.com>
40 #include "texformat.h"
46 /* Texel fetch routines for all supported formats:
49 #include "texformat_tmp.h"
52 #include "texformat_tmp.h"
55 #include "texformat_tmp.h"
57 /* Have to have this so the FetchTexel function pointer is never NULL.
59 static void fetch_null_texel( const struct gl_texture_image
*texImage
,
60 GLint i
, GLint j
, GLint k
, GLvoid
*texel
)
62 GLchan
*rgba
= (GLchan
*) texel
;
70 /* =============================================================
71 * Default GLchan-based formats:
74 const struct gl_texture_format _mesa_texformat_rgba
= {
75 MESA_FORMAT_RGBA
, /* MesaFormat */
76 GL_RGBA
, /* BaseFormat */
77 CHAN_BITS
, /* RedBits */
78 CHAN_BITS
, /* GreenBits */
79 CHAN_BITS
, /* BlueBits */
80 CHAN_BITS
, /* AlphaBits */
81 0, /* LuminanceBits */
82 0, /* IntensityBits */
85 4 * CHAN_BITS
/ 8, /* TexelBytes */
86 fetch_1d_texel_rgba
, /* FetchTexel1D */
87 fetch_2d_texel_rgba
, /* FetchTexel2D */
88 fetch_3d_texel_rgba
, /* FetchTexel3D */
91 const struct gl_texture_format _mesa_texformat_rgb
= {
92 MESA_FORMAT_RGB
, /* MesaFormat */
93 GL_RGB
, /* BaseFormat */
94 CHAN_BITS
, /* RedBits */
95 CHAN_BITS
, /* GreenBits */
96 CHAN_BITS
, /* BlueBits */
98 0, /* LuminanceBits */
99 0, /* IntensityBits */
102 3 * CHAN_BITS
/ 8, /* TexelBytes */
103 fetch_1d_texel_rgb
, /* FetchTexel1D */
104 fetch_2d_texel_rgb
, /* FetchTexel2D */
105 fetch_3d_texel_rgb
, /* FetchTexel3D */
108 const struct gl_texture_format _mesa_texformat_alpha
= {
109 MESA_FORMAT_ALPHA
, /* MesaFormat */
110 GL_ALPHA
, /* BaseFormat */
114 CHAN_BITS
, /* AlphaBits */
115 0, /* LuminanceBits */
116 0, /* IntensityBits */
119 CHAN_BITS
/ 8, /* TexelBytes */
120 fetch_1d_texel_alpha
, /* FetchTexel1D */
121 fetch_2d_texel_alpha
, /* FetchTexel2D */
122 fetch_3d_texel_alpha
, /* FetchTexel3D */
125 const struct gl_texture_format _mesa_texformat_luminance
= {
126 MESA_FORMAT_LUMINANCE
, /* MesaFormat */
127 GL_LUMINANCE
, /* BaseFormat */
132 CHAN_BITS
, /* LuminanceBits */
133 0, /* IntensityBits */
136 CHAN_BITS
/ 8, /* TexelBytes */
137 fetch_1d_texel_luminance
, /* FetchTexel1D */
138 fetch_2d_texel_luminance
, /* FetchTexel2D */
139 fetch_3d_texel_luminance
, /* FetchTexel3D */
142 const struct gl_texture_format _mesa_texformat_luminance_alpha
= {
143 MESA_FORMAT_LUMINANCE_ALPHA
, /* MesaFormat */
144 GL_LUMINANCE_ALPHA
, /* BaseFormat */
148 CHAN_BITS
, /* AlphaBits */
149 CHAN_BITS
, /* LuminanceBits */
150 0, /* IntensityBits */
153 2 * CHAN_BITS
/ 8, /* TexelBytes */
154 fetch_1d_texel_luminance_alpha
, /* FetchTexel1D */
155 fetch_2d_texel_luminance_alpha
, /* FetchTexel2D */
156 fetch_3d_texel_luminance_alpha
, /* FetchTexel3D */
159 const struct gl_texture_format _mesa_texformat_intensity
= {
160 MESA_FORMAT_INTENSITY
, /* MesaFormat */
161 GL_INTENSITY
, /* BaseFormat */
166 0, /* LuminanceBits */
167 CHAN_BITS
, /* IntensityBits */
170 CHAN_BITS
/ 8, /* TexelBytes */
171 fetch_1d_texel_intensity
, /* FetchTexel1D */
172 fetch_2d_texel_intensity
, /* FetchTexel2D */
173 fetch_3d_texel_intensity
, /* FetchTexel3D */
176 const struct gl_texture_format _mesa_texformat_color_index
= {
177 MESA_FORMAT_COLOR_INDEX
, /* MesaFormat */
178 GL_COLOR_INDEX
, /* BaseFormat */
183 0, /* LuminanceBits */
184 0, /* IntensityBits */
185 CHAN_BITS
, /* IndexBits */
187 CHAN_BITS
/ 8, /* TexelBytes */
188 fetch_1d_texel_color_index
, /* FetchTexel1D */
189 fetch_2d_texel_color_index
, /* FetchTexel2D */
190 fetch_3d_texel_color_index
, /* FetchTexel3D */
193 const struct gl_texture_format _mesa_texformat_depth_component
= {
194 MESA_FORMAT_DEPTH_COMPONENT
, /* MesaFormat */
195 GL_DEPTH_COMPONENT
, /* BaseFormat */
200 0, /* LuminanceBits */
201 0, /* IntensityBits */
203 sizeof(GLfloat
) * 8, /* DepthBits */
204 sizeof(GLfloat
), /* TexelBytes */
205 fetch_1d_texel_depth_component
, /* FetchTexel1D */
206 fetch_2d_texel_depth_component
, /* FetchTexel2D */
207 fetch_3d_texel_depth_component
, /* FetchTexel3D */
211 /* =============================================================
215 const struct gl_texture_format _mesa_texformat_rgba8888
= {
216 MESA_FORMAT_RGBA8888
, /* MesaFormat */
217 GL_RGBA
, /* BaseFormat */
222 0, /* LuminanceBits */
223 0, /* IntensityBits */
227 fetch_1d_texel_rgba8888
, /* FetchTexel1D */
228 fetch_2d_texel_rgba8888
, /* FetchTexel2D */
229 fetch_3d_texel_rgba8888
, /* FetchTexel3D */
232 const struct gl_texture_format _mesa_texformat_argb8888
= {
233 MESA_FORMAT_ARGB8888
, /* MesaFormat */
234 GL_RGBA
, /* BaseFormat */
239 0, /* LuminanceBits */
240 0, /* IntensityBits */
244 fetch_1d_texel_argb8888
, /* FetchTexel1D */
245 fetch_2d_texel_argb8888
, /* FetchTexel2D */
246 fetch_3d_texel_argb8888
, /* FetchTexel3D */
249 const struct gl_texture_format _mesa_texformat_rgb888
= {
250 MESA_FORMAT_RGB888
, /* MesaFormat */
251 GL_RGB
, /* BaseFormat */
256 0, /* LuminanceBits */
257 0, /* IntensityBits */
261 fetch_1d_texel_rgb888
, /* FetchTexel1D */
262 fetch_2d_texel_rgb888
, /* FetchTexel2D */
263 fetch_3d_texel_rgb888
, /* FetchTexel3D */
266 const struct gl_texture_format _mesa_texformat_rgb565
= {
267 MESA_FORMAT_RGB565
, /* MesaFormat */
268 GL_RGB
, /* BaseFormat */
273 0, /* LuminanceBits */
274 0, /* IntensityBits */
278 fetch_1d_texel_rgb565
, /* FetchTexel1D */
279 fetch_2d_texel_rgb565
, /* FetchTexel2D */
280 fetch_3d_texel_rgb565
, /* FetchTexel3D */
283 const struct gl_texture_format _mesa_texformat_argb4444
= {
284 MESA_FORMAT_ARGB4444
, /* MesaFormat */
285 GL_RGBA
, /* BaseFormat */
290 0, /* LuminanceBits */
291 0, /* IntensityBits */
295 fetch_1d_texel_argb4444
, /* FetchTexel1D */
296 fetch_2d_texel_argb4444
, /* FetchTexel2D */
297 fetch_3d_texel_argb4444
, /* FetchTexel3D */
300 const struct gl_texture_format _mesa_texformat_argb1555
= {
301 MESA_FORMAT_ARGB1555
, /* MesaFormat */
302 GL_RGBA
, /* BaseFormat */
307 0, /* LuminanceBits */
308 0, /* IntensityBits */
312 fetch_1d_texel_argb1555
, /* FetchTexel1D */
313 fetch_2d_texel_argb1555
, /* FetchTexel2D */
314 fetch_3d_texel_argb1555
, /* FetchTexel3D */
317 const struct gl_texture_format _mesa_texformat_al88
= {
318 MESA_FORMAT_AL88
, /* MesaFormat */
319 GL_LUMINANCE_ALPHA
, /* BaseFormat */
324 8, /* LuminanceBits */
325 0, /* IntensityBits */
329 fetch_1d_texel_al88
, /* FetchTexel1D */
330 fetch_2d_texel_al88
, /* FetchTexel2D */
331 fetch_3d_texel_al88
, /* FetchTexel3D */
334 const struct gl_texture_format _mesa_texformat_rgb332
= {
335 MESA_FORMAT_RGB332
, /* MesaFormat */
336 GL_RGB
, /* BaseFormat */
341 0, /* LuminanceBits */
342 0, /* IntensityBits */
346 fetch_1d_texel_rgb332
, /* FetchTexel1D */
347 fetch_2d_texel_rgb332
, /* FetchTexel2D */
348 fetch_3d_texel_rgb332
, /* FetchTexel3D */
351 const struct gl_texture_format _mesa_texformat_a8
= {
352 MESA_FORMAT_A8
, /* MesaFormat */
353 GL_ALPHA
, /* BaseFormat */
358 0, /* LuminanceBits */
359 0, /* IntensityBits */
363 fetch_1d_texel_a8
, /* FetchTexel1D */
364 fetch_2d_texel_a8
, /* FetchTexel2D */
365 fetch_3d_texel_a8
, /* FetchTexel3D */
368 const struct gl_texture_format _mesa_texformat_l8
= {
369 MESA_FORMAT_L8
, /* MesaFormat */
370 GL_LUMINANCE
, /* BaseFormat */
375 8, /* LuminanceBits */
376 0, /* IntensityBits */
380 fetch_1d_texel_l8
, /* FetchTexel1D */
381 fetch_2d_texel_l8
, /* FetchTexel2D */
382 fetch_3d_texel_l8
, /* FetchTexel3D */
385 const struct gl_texture_format _mesa_texformat_i8
= {
386 MESA_FORMAT_I8
, /* MesaFormat */
387 GL_INTENSITY
, /* BaseFormat */
392 0, /* LuminanceBits */
393 8, /* IntensityBits */
397 fetch_1d_texel_i8
, /* FetchTexel1D */
398 fetch_2d_texel_i8
, /* FetchTexel2D */
399 fetch_3d_texel_i8
, /* FetchTexel3D */
402 const struct gl_texture_format _mesa_texformat_ci8
= {
403 MESA_FORMAT_CI8
, /* MesaFormat */
404 GL_COLOR_INDEX
, /* BaseFormat */
409 0, /* LuminanceBits */
410 0, /* IntensityBits */
414 fetch_1d_texel_ci8
, /* FetchTexel1D */
415 fetch_2d_texel_ci8
, /* FetchTexel2D */
416 fetch_3d_texel_ci8
, /* FetchTexel3D */
419 const struct gl_texture_format _mesa_texformat_ycbcr
= {
420 MESA_FORMAT_YCBCR
, /* MesaFormat */
421 GL_YCBCR_MESA
, /* BaseFormat */
426 0, /* LuminanceBits */
427 0, /* IntensityBits */
431 fetch_1d_texel_ycbcr
, /* FetchTexel1D */
432 fetch_2d_texel_ycbcr
, /* FetchTexel2D */
433 fetch_3d_texel_ycbcr
, /* FetchTexel3D */
437 const struct gl_texture_format _mesa_texformat_ycbcr_rev
= {
438 MESA_FORMAT_YCBCR_REV
, /* MesaFormat */
439 GL_YCBCR_MESA
, /* BaseFormat */
444 0, /* LuminanceBits */
445 0, /* IntensityBits */
449 fetch_1d_texel_ycbcr_rev
, /* FetchTexel1D */
450 fetch_2d_texel_ycbcr_rev
, /* FetchTexel2D */
451 fetch_3d_texel_ycbcr_rev
, /* FetchTexel3D */
457 const struct gl_texture_format _mesa_texformat_abgr8888
= {
458 MESA_FORMAT_ABGR8888
, /* MesaFormat */
459 GL_RGBA
, /* BaseFormat */
460 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
465 0, /* LuminanceBits */
466 0, /* IntensityBits */
470 fetch_1d_texel_abgr8888
, /* FetchTexel1D */
471 fetch_2d_texel_abgr8888
, /* FetchTexel2D */
472 fetch_3d_texel_abgr8888
, /* FetchTexel3D */
475 const struct gl_texture_format _mesa_texformat_bgra8888
= {
476 MESA_FORMAT_BGRA8888
, /* MesaFormat */
477 GL_RGBA
, /* BaseFormat */
478 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
483 0, /* LuminanceBits */
484 0, /* IntensityBits */
488 fetch_1d_texel_bgra8888
, /* FetchTexel1D */
489 fetch_2d_texel_bgra8888
, /* FetchTexel2D */
490 fetch_3d_texel_bgra8888
, /* FetchTexel3D */
493 const struct gl_texture_format _mesa_texformat_bgr888
= {
494 MESA_FORMAT_BGR888
, /* MesaFormat */
495 GL_RGB
, /* BaseFormat */
496 GL_UNSIGNED_BYTE
, /* Type */
501 0, /* LuminanceBits */
502 0, /* IntensityBits */
506 fetch_1d_texel_bgr888
, /* FetchTexel1D */
507 fetch_2d_texel_bgr888
, /* FetchTexel2D */
508 fetch_3d_texel_bgr888
, /* FetchTexel3D */
511 const struct gl_texture_format _mesa_texformat_bgr565
= {
512 MESA_FORMAT_BGR565
, /* MesaFormat */
513 GL_RGB
, /* BaseFormat */
514 GL_UNSIGNED_SHORT_5_6_5
, /* Type */
519 0, /* LuminanceBits */
520 0, /* IntensityBits */
524 fetch_1d_texel_bgr565
, /* FetchTexel1D */
525 fetch_2d_texel_bgr565
, /* FetchTexel2D */
526 fetch_3d_texel_bgr565
, /* FetchTexel3D */
529 const struct gl_texture_format _mesa_texformat_bgra4444
= {
530 MESA_FORMAT_BGRA4444
, /* MesaFormat */
531 GL_RGBA
, /* BaseFormat */
532 GL_UNSIGNED_SHORT_4_4_4_4_REV
, /* Type */
537 0, /* LuminanceBits */
538 0, /* IntensityBits */
542 fetch_1d_texel_bgra4444
, /* FetchTexel1D */
543 fetch_2d_texel_bgra4444
, /* FetchTexel2D */
544 fetch_3d_texel_bgra4444
, /* FetchTexel3D */
547 const struct gl_texture_format _mesa_texformat_bgra5551
= {
548 MESA_FORMAT_BGRA5551
, /* MesaFormat */
549 GL_RGBA
, /* BaseFormat */
550 GL_UNSIGNED_SHORT_1_5_5_5_REV
, /* Type */
555 0, /* LuminanceBits */
556 0, /* IntensityBits */
560 fetch_1d_texel_bgra1555
, /* FetchTexel1D */
561 fetch_2d_texel_bgra1555
, /* FetchTexel2D */
562 fetch_3d_texel_bgra1555
, /* FetchTexel3D */
565 const struct gl_texture_format _mesa_texformat_la88
= {
566 MESA_FORMAT_LA88
, /* MesaFormat */
567 GL_LUMINANCE_ALPHA
, /* BaseFormat */
568 GL_UNSIGNED_BYTE
, /* Type */
573 8, /* LuminanceBits */
574 0, /* IntensityBits */
578 fetch_1d_texel_la88
, /* FetchTexel1D */
579 fetch_2d_texel_la88
, /* FetchTexel2D */
580 fetch_3d_texel_la88
, /* FetchTexel3D */
583 const struct gl_texture_format _mesa_texformat_bgr233
= {
584 MESA_FORMAT_BGR233
, /* MesaFormat */
585 GL_RGB
, /* BaseFormat */
586 GL_UNSIGNED_BYTE_3_3_2
, /* Type */
591 0, /* LuminanceBits */
592 0, /* IntensityBits */
596 fetch_1d_texel_bgr233
, /* FetchTexel1D */
597 fetch_2d_texel_bgr233
, /* FetchTexel2D */
598 fetch_3d_texel_bgr233
, /* FetchTexel3D */
602 /* =============================================================
603 * Null format (useful for proxy textures):
606 const struct gl_texture_format _mesa_null_texformat
= {
613 0, /* LuminanceBits */
614 0, /* IntensityBits */
618 fetch_null_texel
, /* FetchTexel1D */
619 fetch_null_texel
, /* FetchTexel2D */
620 fetch_null_texel
, /* FetchTexel3D */
625 _mesa_is_hardware_tex_format( const struct gl_texture_format
*format
)
627 return (format
->MesaFormat
< MESA_FORMAT_RGBA
);
631 /* Given an internal texture format (or 1, 2, 3, 4) return a pointer
632 * to a gl_texture_format which which to store the texture.
633 * This is called via ctx->Driver.ChooseTextureFormat().
634 * Hardware drivers typically override this function with a specialized
637 const struct gl_texture_format
*
638 _mesa_choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
639 GLenum format
, GLenum type
)
644 switch ( internalFormat
) {
645 /* GH: Bias towards GL_RGB, GL_RGBA texture formats. This has
646 * got to be better than sticking them way down the end of this
649 case 4: /* Quake3 uses this... */
651 return &_mesa_texformat_rgba
;
653 case 3: /* ... and this. */
655 return &_mesa_texformat_rgb
;
657 /* GH: Okay, keep checking as normal. Still test for GL_RGB,
658 * GL_RGBA formats first.
667 return &_mesa_texformat_rgba
;
676 return &_mesa_texformat_rgb
;
683 return &_mesa_texformat_alpha
;
691 return &_mesa_texformat_luminance
;
694 case GL_LUMINANCE_ALPHA
:
695 case GL_LUMINANCE4_ALPHA4
:
696 case GL_LUMINANCE6_ALPHA2
:
697 case GL_LUMINANCE8_ALPHA8
:
698 case GL_LUMINANCE12_ALPHA4
:
699 case GL_LUMINANCE12_ALPHA12
:
700 case GL_LUMINANCE16_ALPHA16
:
701 return &_mesa_texformat_luminance_alpha
;
708 return &_mesa_texformat_intensity
;
711 case GL_COLOR_INDEX1_EXT
:
712 case GL_COLOR_INDEX2_EXT
:
713 case GL_COLOR_INDEX4_EXT
:
714 case GL_COLOR_INDEX8_EXT
:
715 case GL_COLOR_INDEX12_EXT
:
716 case GL_COLOR_INDEX16_EXT
:
717 return &_mesa_texformat_color_index
;
719 case GL_DEPTH_COMPONENT
:
720 case GL_DEPTH_COMPONENT16_SGIX
:
721 case GL_DEPTH_COMPONENT24_SGIX
:
722 case GL_DEPTH_COMPONENT32_SGIX
:
723 if (!ctx
->Extensions
.SGIX_depth_texture
)
724 _mesa_problem(ctx
, "depth format without GL_SGIX_depth_texture");
725 return &_mesa_texformat_depth_component
;
727 case GL_COMPRESSED_ALPHA_ARB
:
728 if (!ctx
->Extensions
.ARB_texture_compression
)
729 _mesa_problem(ctx
, "texture compression extension not enabled");
730 return &_mesa_texformat_alpha
;
731 case GL_COMPRESSED_LUMINANCE_ARB
:
732 if (!ctx
->Extensions
.ARB_texture_compression
)
733 _mesa_problem(ctx
, "texture compression extension not enabled");
734 return &_mesa_texformat_luminance
;
735 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
736 if (!ctx
->Extensions
.ARB_texture_compression
)
737 _mesa_problem(ctx
, "texture compression extension not enabled");
738 return &_mesa_texformat_luminance_alpha
;
739 case GL_COMPRESSED_INTENSITY_ARB
:
740 if (!ctx
->Extensions
.ARB_texture_compression
)
741 _mesa_problem(ctx
, "texture compression extension not enabled");
742 return &_mesa_texformat_intensity
;
743 case GL_COMPRESSED_RGB_ARB
:
744 if (!ctx
->Extensions
.ARB_texture_compression
)
745 _mesa_problem(ctx
, "texture compression extension not enabled");
746 return &_mesa_texformat_rgb
;
747 case GL_COMPRESSED_RGBA_ARB
:
748 if (!ctx
->Extensions
.ARB_texture_compression
)
749 _mesa_problem(ctx
, "texture compression extension not enabled");
750 return &_mesa_texformat_rgba
;
752 /* GL_MESA_ycrcr_texture */
754 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
)
755 return &_mesa_texformat_ycbcr
;
757 return &_mesa_texformat_ycbcr_rev
;
760 _mesa_problem(ctx
, "unexpected format in _mesa_choose_tex_format()");
769 * Return the base texture format for the given compressed format
770 * Called via ctx->Driver.BaseCompressedTexFormat().
771 * This function is used by software rasterizers. Hardware drivers
772 * which support texture compression should not use this function but
773 * a specialized function instead.
776 _mesa_base_compressed_texformat(GLcontext
*ctx
, GLint intFormat
)
779 case GL_COMPRESSED_ALPHA_ARB
:
781 case GL_COMPRESSED_LUMINANCE_ARB
:
783 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
784 return GL_LUMINANCE_ALPHA
;
785 case GL_COMPRESSED_INTENSITY_ARB
:
787 case GL_COMPRESSED_RGB_ARB
:
789 case GL_COMPRESSED_RGBA_ARB
:
792 return -1; /* not a recognized compressed format */