1 /* $Id: texformat.c,v 1.18 2002/10/29 20:28:47 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.
37 #include "texformat.h"
42 /* Texel fetch routines for all supported formats:
45 #include "texformat_tmp.h"
48 #include "texformat_tmp.h"
51 #include "texformat_tmp.h"
53 /* Have to have this so the FetchTexel function pointer is never NULL.
55 static void fetch_null_texel( const struct gl_texture_image
*texImage
,
56 GLint i
, GLint j
, GLint k
, GLvoid
*texel
)
58 GLchan
*rgba
= (GLchan
*) texel
;
66 /* =============================================================
67 * Default GLchan-based formats:
70 const struct gl_texture_format _mesa_texformat_rgba
= {
71 MESA_FORMAT_RGBA
, /* MesaFormat */
72 GL_RGBA
, /* BaseFormat */
73 CHAN_BITS
, /* RedBits */
74 CHAN_BITS
, /* GreenBits */
75 CHAN_BITS
, /* BlueBits */
76 CHAN_BITS
, /* AlphaBits */
77 0, /* LuminanceBits */
78 0, /* IntensityBits */
81 4 * CHAN_BITS
/ 8, /* TexelBytes */
82 fetch_1d_texel_rgba
, /* FetchTexel1D */
83 fetch_2d_texel_rgba
, /* FetchTexel2D */
84 fetch_3d_texel_rgba
, /* FetchTexel3D */
87 const struct gl_texture_format _mesa_texformat_rgb
= {
88 MESA_FORMAT_RGB
, /* MesaFormat */
89 GL_RGB
, /* BaseFormat */
90 CHAN_BITS
, /* RedBits */
91 CHAN_BITS
, /* GreenBits */
92 CHAN_BITS
, /* BlueBits */
94 0, /* LuminanceBits */
95 0, /* IntensityBits */
98 3 * CHAN_BITS
/ 8, /* TexelBytes */
99 fetch_1d_texel_rgb
, /* FetchTexel1D */
100 fetch_2d_texel_rgb
, /* FetchTexel2D */
101 fetch_3d_texel_rgb
, /* FetchTexel3D */
104 const struct gl_texture_format _mesa_texformat_alpha
= {
105 MESA_FORMAT_ALPHA
, /* MesaFormat */
106 GL_ALPHA
, /* BaseFormat */
110 CHAN_BITS
, /* AlphaBits */
111 0, /* LuminanceBits */
112 0, /* IntensityBits */
115 CHAN_BITS
/ 8, /* TexelBytes */
116 fetch_1d_texel_alpha
, /* FetchTexel1D */
117 fetch_2d_texel_alpha
, /* FetchTexel2D */
118 fetch_3d_texel_alpha
, /* FetchTexel3D */
121 const struct gl_texture_format _mesa_texformat_luminance
= {
122 MESA_FORMAT_LUMINANCE
, /* MesaFormat */
123 GL_LUMINANCE
, /* BaseFormat */
128 CHAN_BITS
, /* LuminanceBits */
129 0, /* IntensityBits */
132 CHAN_BITS
/ 8, /* TexelBytes */
133 fetch_1d_texel_luminance
, /* FetchTexel1D */
134 fetch_2d_texel_luminance
, /* FetchTexel2D */
135 fetch_3d_texel_luminance
, /* FetchTexel3D */
138 const struct gl_texture_format _mesa_texformat_luminance_alpha
= {
139 MESA_FORMAT_LUMINANCE_ALPHA
, /* MesaFormat */
140 GL_LUMINANCE_ALPHA
, /* BaseFormat */
144 CHAN_BITS
, /* AlphaBits */
145 CHAN_BITS
, /* LuminanceBits */
146 0, /* IntensityBits */
149 2 * CHAN_BITS
/ 8, /* TexelBytes */
150 fetch_1d_texel_luminance_alpha
, /* FetchTexel1D */
151 fetch_2d_texel_luminance_alpha
, /* FetchTexel2D */
152 fetch_3d_texel_luminance_alpha
, /* FetchTexel3D */
155 const struct gl_texture_format _mesa_texformat_intensity
= {
156 MESA_FORMAT_INTENSITY
, /* MesaFormat */
157 GL_INTENSITY
, /* BaseFormat */
162 0, /* LuminanceBits */
163 CHAN_BITS
, /* IntensityBits */
166 CHAN_BITS
/ 8, /* TexelBytes */
167 fetch_1d_texel_intensity
, /* FetchTexel1D */
168 fetch_2d_texel_intensity
, /* FetchTexel2D */
169 fetch_3d_texel_intensity
, /* FetchTexel3D */
172 const struct gl_texture_format _mesa_texformat_color_index
= {
173 MESA_FORMAT_COLOR_INDEX
, /* MesaFormat */
174 GL_COLOR_INDEX
, /* BaseFormat */
179 0, /* LuminanceBits */
180 0, /* IntensityBits */
181 CHAN_BITS
, /* IndexBits */
183 CHAN_BITS
/ 8, /* TexelBytes */
184 fetch_1d_texel_color_index
, /* FetchTexel1D */
185 fetch_2d_texel_color_index
, /* FetchTexel2D */
186 fetch_3d_texel_color_index
, /* FetchTexel3D */
189 const struct gl_texture_format _mesa_texformat_depth_component
= {
190 MESA_FORMAT_DEPTH_COMPONENT
, /* MesaFormat */
191 GL_DEPTH_COMPONENT
, /* BaseFormat */
196 0, /* LuminanceBits */
197 0, /* IntensityBits */
199 sizeof(GLfloat
) * 8, /* DepthBits */
200 sizeof(GLfloat
), /* TexelBytes */
201 fetch_1d_texel_depth_component
, /* FetchTexel1D */
202 fetch_2d_texel_depth_component
, /* FetchTexel2D */
203 fetch_3d_texel_depth_component
, /* FetchTexel3D */
207 /* =============================================================
211 const struct gl_texture_format _mesa_texformat_rgba8888
= {
212 MESA_FORMAT_RGBA8888
, /* MesaFormat */
213 GL_RGBA
, /* BaseFormat */
218 0, /* LuminanceBits */
219 0, /* IntensityBits */
223 fetch_1d_texel_rgba8888
, /* FetchTexel1D */
224 fetch_2d_texel_rgba8888
, /* FetchTexel2D */
225 fetch_3d_texel_rgba8888
, /* FetchTexel3D */
228 const struct gl_texture_format _mesa_texformat_argb8888
= {
229 MESA_FORMAT_ARGB8888
, /* MesaFormat */
230 GL_RGBA
, /* BaseFormat */
235 0, /* LuminanceBits */
236 0, /* IntensityBits */
240 fetch_1d_texel_argb8888
, /* FetchTexel1D */
241 fetch_2d_texel_argb8888
, /* FetchTexel2D */
242 fetch_3d_texel_argb8888
, /* FetchTexel3D */
245 const struct gl_texture_format _mesa_texformat_rgb888
= {
246 MESA_FORMAT_RGB888
, /* MesaFormat */
247 GL_RGB
, /* BaseFormat */
252 0, /* LuminanceBits */
253 0, /* IntensityBits */
257 fetch_1d_texel_rgb888
, /* FetchTexel1D */
258 fetch_2d_texel_rgb888
, /* FetchTexel2D */
259 fetch_3d_texel_rgb888
, /* FetchTexel3D */
262 const struct gl_texture_format _mesa_texformat_rgb565
= {
263 MESA_FORMAT_RGB565
, /* MesaFormat */
264 GL_RGB
, /* BaseFormat */
269 0, /* LuminanceBits */
270 0, /* IntensityBits */
274 fetch_1d_texel_rgb565
, /* FetchTexel1D */
275 fetch_2d_texel_rgb565
, /* FetchTexel2D */
276 fetch_3d_texel_rgb565
, /* FetchTexel3D */
279 const struct gl_texture_format _mesa_texformat_argb4444
= {
280 MESA_FORMAT_ARGB4444
, /* MesaFormat */
281 GL_RGBA
, /* BaseFormat */
286 0, /* LuminanceBits */
287 0, /* IntensityBits */
291 fetch_1d_texel_argb4444
, /* FetchTexel1D */
292 fetch_2d_texel_argb4444
, /* FetchTexel2D */
293 fetch_3d_texel_argb4444
, /* FetchTexel3D */
296 const struct gl_texture_format _mesa_texformat_argb1555
= {
297 MESA_FORMAT_ARGB1555
, /* MesaFormat */
298 GL_RGBA
, /* BaseFormat */
303 0, /* LuminanceBits */
304 0, /* IntensityBits */
308 fetch_1d_texel_argb1555
, /* FetchTexel1D */
309 fetch_2d_texel_argb1555
, /* FetchTexel2D */
310 fetch_3d_texel_argb1555
, /* FetchTexel3D */
313 const struct gl_texture_format _mesa_texformat_al88
= {
314 MESA_FORMAT_AL88
, /* MesaFormat */
315 GL_LUMINANCE_ALPHA
, /* BaseFormat */
320 8, /* LuminanceBits */
321 0, /* IntensityBits */
325 fetch_1d_texel_al88
, /* FetchTexel1D */
326 fetch_2d_texel_al88
, /* FetchTexel2D */
327 fetch_3d_texel_al88
, /* FetchTexel3D */
330 const struct gl_texture_format _mesa_texformat_rgb332
= {
331 MESA_FORMAT_RGB332
, /* MesaFormat */
332 GL_RGB
, /* BaseFormat */
337 0, /* LuminanceBits */
338 0, /* IntensityBits */
342 fetch_1d_texel_rgb332
, /* FetchTexel1D */
343 fetch_2d_texel_rgb332
, /* FetchTexel2D */
344 fetch_3d_texel_rgb332
, /* FetchTexel3D */
347 const struct gl_texture_format _mesa_texformat_a8
= {
348 MESA_FORMAT_A8
, /* MesaFormat */
349 GL_ALPHA
, /* BaseFormat */
354 0, /* LuminanceBits */
355 0, /* IntensityBits */
359 fetch_1d_texel_a8
, /* FetchTexel1D */
360 fetch_2d_texel_a8
, /* FetchTexel2D */
361 fetch_3d_texel_a8
, /* FetchTexel3D */
364 const struct gl_texture_format _mesa_texformat_l8
= {
365 MESA_FORMAT_L8
, /* MesaFormat */
366 GL_LUMINANCE
, /* BaseFormat */
371 8, /* LuminanceBits */
372 0, /* IntensityBits */
376 fetch_1d_texel_l8
, /* FetchTexel1D */
377 fetch_2d_texel_l8
, /* FetchTexel2D */
378 fetch_3d_texel_l8
, /* FetchTexel3D */
381 const struct gl_texture_format _mesa_texformat_i8
= {
382 MESA_FORMAT_I8
, /* MesaFormat */
383 GL_INTENSITY
, /* BaseFormat */
388 0, /* LuminanceBits */
389 8, /* IntensityBits */
393 fetch_1d_texel_i8
, /* FetchTexel1D */
394 fetch_2d_texel_i8
, /* FetchTexel2D */
395 fetch_3d_texel_i8
, /* FetchTexel3D */
398 const struct gl_texture_format _mesa_texformat_ci8
= {
399 MESA_FORMAT_CI8
, /* MesaFormat */
400 GL_COLOR_INDEX
, /* BaseFormat */
405 0, /* LuminanceBits */
406 0, /* IntensityBits */
410 fetch_1d_texel_ci8
, /* FetchTexel1D */
411 fetch_2d_texel_ci8
, /* FetchTexel2D */
412 fetch_3d_texel_ci8
, /* FetchTexel3D */
415 const struct gl_texture_format _mesa_texformat_ycbcr
= {
416 MESA_FORMAT_YCBCR
, /* MesaFormat */
417 GL_YCBCR_MESA
, /* BaseFormat */
422 0, /* LuminanceBits */
423 0, /* IntensityBits */
427 fetch_1d_texel_ycbcr
, /* FetchTexel1D */
428 fetch_2d_texel_ycbcr
, /* FetchTexel2D */
429 fetch_3d_texel_ycbcr
, /* FetchTexel3D */
433 const struct gl_texture_format _mesa_texformat_ycbcr_rev
= {
434 MESA_FORMAT_YCBCR_REV
, /* MesaFormat */
435 GL_YCBCR_MESA
, /* BaseFormat */
440 0, /* LuminanceBits */
441 0, /* IntensityBits */
445 fetch_1d_texel_ycbcr_rev
, /* FetchTexel1D */
446 fetch_2d_texel_ycbcr_rev
, /* FetchTexel2D */
447 fetch_3d_texel_ycbcr_rev
, /* FetchTexel3D */
453 const struct gl_texture_format _mesa_texformat_abgr8888
= {
454 MESA_FORMAT_ABGR8888
, /* MesaFormat */
455 GL_RGBA
, /* BaseFormat */
456 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
461 0, /* LuminanceBits */
462 0, /* IntensityBits */
466 fetch_1d_texel_abgr8888
, /* FetchTexel1D */
467 fetch_2d_texel_abgr8888
, /* FetchTexel2D */
468 fetch_3d_texel_abgr8888
, /* FetchTexel3D */
471 const struct gl_texture_format _mesa_texformat_bgra8888
= {
472 MESA_FORMAT_BGRA8888
, /* MesaFormat */
473 GL_RGBA
, /* BaseFormat */
474 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
479 0, /* LuminanceBits */
480 0, /* IntensityBits */
484 fetch_1d_texel_bgra8888
, /* FetchTexel1D */
485 fetch_2d_texel_bgra8888
, /* FetchTexel2D */
486 fetch_3d_texel_bgra8888
, /* FetchTexel3D */
489 const struct gl_texture_format _mesa_texformat_bgr888
= {
490 MESA_FORMAT_BGR888
, /* MesaFormat */
491 GL_RGB
, /* BaseFormat */
492 GL_UNSIGNED_BYTE
, /* Type */
497 0, /* LuminanceBits */
498 0, /* IntensityBits */
502 fetch_1d_texel_bgr888
, /* FetchTexel1D */
503 fetch_2d_texel_bgr888
, /* FetchTexel2D */
504 fetch_3d_texel_bgr888
, /* FetchTexel3D */
507 const struct gl_texture_format _mesa_texformat_bgr565
= {
508 MESA_FORMAT_BGR565
, /* MesaFormat */
509 GL_RGB
, /* BaseFormat */
510 GL_UNSIGNED_SHORT_5_6_5
, /* Type */
515 0, /* LuminanceBits */
516 0, /* IntensityBits */
520 fetch_1d_texel_bgr565
, /* FetchTexel1D */
521 fetch_2d_texel_bgr565
, /* FetchTexel2D */
522 fetch_3d_texel_bgr565
, /* FetchTexel3D */
525 const struct gl_texture_format _mesa_texformat_bgra4444
= {
526 MESA_FORMAT_BGRA4444
, /* MesaFormat */
527 GL_RGBA
, /* BaseFormat */
528 GL_UNSIGNED_SHORT_4_4_4_4_REV
, /* Type */
533 0, /* LuminanceBits */
534 0, /* IntensityBits */
538 fetch_1d_texel_bgra4444
, /* FetchTexel1D */
539 fetch_2d_texel_bgra4444
, /* FetchTexel2D */
540 fetch_3d_texel_bgra4444
, /* FetchTexel3D */
543 const struct gl_texture_format _mesa_texformat_bgra5551
= {
544 MESA_FORMAT_BGRA5551
, /* MesaFormat */
545 GL_RGBA
, /* BaseFormat */
546 GL_UNSIGNED_SHORT_1_5_5_5_REV
, /* Type */
551 0, /* LuminanceBits */
552 0, /* IntensityBits */
556 fetch_1d_texel_bgra1555
, /* FetchTexel1D */
557 fetch_2d_texel_bgra1555
, /* FetchTexel2D */
558 fetch_3d_texel_bgra1555
, /* FetchTexel3D */
561 const struct gl_texture_format _mesa_texformat_la88
= {
562 MESA_FORMAT_LA88
, /* MesaFormat */
563 GL_LUMINANCE_ALPHA
, /* BaseFormat */
564 GL_UNSIGNED_BYTE
, /* Type */
569 8, /* LuminanceBits */
570 0, /* IntensityBits */
574 fetch_1d_texel_la88
, /* FetchTexel1D */
575 fetch_2d_texel_la88
, /* FetchTexel2D */
576 fetch_3d_texel_la88
, /* FetchTexel3D */
579 const struct gl_texture_format _mesa_texformat_bgr233
= {
580 MESA_FORMAT_BGR233
, /* MesaFormat */
581 GL_RGB
, /* BaseFormat */
582 GL_UNSIGNED_BYTE_3_3_2
, /* Type */
587 0, /* LuminanceBits */
588 0, /* IntensityBits */
592 fetch_1d_texel_bgr233
, /* FetchTexel1D */
593 fetch_2d_texel_bgr233
, /* FetchTexel2D */
594 fetch_3d_texel_bgr233
, /* FetchTexel3D */
598 /* =============================================================
599 * Null format (useful for proxy textures):
602 const struct gl_texture_format _mesa_null_texformat
= {
609 0, /* LuminanceBits */
610 0, /* IntensityBits */
614 fetch_null_texel
, /* FetchTexel1D */
615 fetch_null_texel
, /* FetchTexel2D */
616 fetch_null_texel
, /* FetchTexel3D */
621 _mesa_is_hardware_tex_format( const struct gl_texture_format
*format
)
623 return (format
->MesaFormat
< MESA_FORMAT_RGBA
);
627 /* Given an internal texture format (or 1, 2, 3, 4) return a pointer
628 * to a gl_texture_format which which to store the texture.
629 * This is called via ctx->Driver.ChooseTextureFormat().
630 * Hardware drivers typically override this function with a specialized
633 const struct gl_texture_format
*
634 _mesa_choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
635 GLenum format
, GLenum type
)
640 switch ( internalFormat
) {
641 /* GH: Bias towards GL_RGB, GL_RGBA texture formats. This has
642 * got to be better than sticking them way down the end of this
645 case 4: /* Quake3 uses this... */
647 return &_mesa_texformat_rgba
;
649 case 3: /* ... and this. */
651 return &_mesa_texformat_rgb
;
653 /* GH: Okay, keep checking as normal. Still test for GL_RGB,
654 * GL_RGBA formats first.
663 return &_mesa_texformat_rgba
;
672 return &_mesa_texformat_rgb
;
679 return &_mesa_texformat_alpha
;
687 return &_mesa_texformat_luminance
;
690 case GL_LUMINANCE_ALPHA
:
691 case GL_LUMINANCE4_ALPHA4
:
692 case GL_LUMINANCE6_ALPHA2
:
693 case GL_LUMINANCE8_ALPHA8
:
694 case GL_LUMINANCE12_ALPHA4
:
695 case GL_LUMINANCE12_ALPHA12
:
696 case GL_LUMINANCE16_ALPHA16
:
697 return &_mesa_texformat_luminance_alpha
;
704 return &_mesa_texformat_intensity
;
707 case GL_COLOR_INDEX1_EXT
:
708 case GL_COLOR_INDEX2_EXT
:
709 case GL_COLOR_INDEX4_EXT
:
710 case GL_COLOR_INDEX8_EXT
:
711 case GL_COLOR_INDEX12_EXT
:
712 case GL_COLOR_INDEX16_EXT
:
713 return &_mesa_texformat_color_index
;
715 case GL_DEPTH_COMPONENT
:
716 case GL_DEPTH_COMPONENT16_SGIX
:
717 case GL_DEPTH_COMPONENT24_SGIX
:
718 case GL_DEPTH_COMPONENT32_SGIX
:
719 if (!ctx
->Extensions
.SGIX_depth_texture
)
720 _mesa_problem(ctx
, "depth format without GL_SGIX_depth_texture");
721 return &_mesa_texformat_depth_component
;
723 case GL_COMPRESSED_ALPHA_ARB
:
724 if (!ctx
->Extensions
.ARB_texture_compression
)
725 _mesa_problem(ctx
, "texture compression extension not enabled");
726 return &_mesa_texformat_alpha
;
727 case GL_COMPRESSED_LUMINANCE_ARB
:
728 if (!ctx
->Extensions
.ARB_texture_compression
)
729 _mesa_problem(ctx
, "texture compression extension not enabled");
730 return &_mesa_texformat_luminance
;
731 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
732 if (!ctx
->Extensions
.ARB_texture_compression
)
733 _mesa_problem(ctx
, "texture compression extension not enabled");
734 return &_mesa_texformat_luminance_alpha
;
735 case GL_COMPRESSED_INTENSITY_ARB
:
736 if (!ctx
->Extensions
.ARB_texture_compression
)
737 _mesa_problem(ctx
, "texture compression extension not enabled");
738 return &_mesa_texformat_intensity
;
739 case GL_COMPRESSED_RGB_ARB
:
740 if (!ctx
->Extensions
.ARB_texture_compression
)
741 _mesa_problem(ctx
, "texture compression extension not enabled");
742 return &_mesa_texformat_rgb
;
743 case GL_COMPRESSED_RGBA_ARB
:
744 if (!ctx
->Extensions
.ARB_texture_compression
)
745 _mesa_problem(ctx
, "texture compression extension not enabled");
746 return &_mesa_texformat_rgba
;
748 /* GL_MESA_ycrcr_texture */
750 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
)
751 return &_mesa_texformat_ycbcr
;
753 return &_mesa_texformat_ycbcr_rev
;
756 _mesa_problem(ctx
, "unexpected format in _mesa_choose_tex_format()");
765 * Return the base texture format for the given compressed format
766 * Called via ctx->Driver.BaseCompressedTexFormat().
767 * This function is used by software rasterizers. Hardware drivers
768 * which support texture compression should not use this function but
769 * a specialized function instead.
772 _mesa_base_compressed_texformat(GLcontext
*ctx
, GLint intFormat
)
775 case GL_COMPRESSED_ALPHA_ARB
:
777 case GL_COMPRESSED_LUMINANCE_ARB
:
779 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
780 return GL_LUMINANCE_ALPHA
;
781 case GL_COMPRESSED_INTENSITY_ARB
:
783 case GL_COMPRESSED_RGB_ARB
:
785 case GL_COMPRESSED_RGBA_ARB
:
788 return -1; /* not a recognized compressed format */