1 /* $Id: texformat.c,v 1.19 2003/03/01 01:50:22 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 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.
36 #include "texformat.h"
41 /* Texel fetch routines for all supported formats:
44 #include "texformat_tmp.h"
47 #include "texformat_tmp.h"
50 #include "texformat_tmp.h"
52 /* Have to have this so the FetchTexel function pointer is never NULL.
54 static void fetch_null_texel( const struct gl_texture_image
*texImage
,
55 GLint i
, GLint j
, GLint k
, GLvoid
*texel
)
57 GLchan
*rgba
= (GLchan
*) texel
;
65 /* =============================================================
66 * Default GLchan-based formats:
69 const struct gl_texture_format _mesa_texformat_rgba
= {
70 MESA_FORMAT_RGBA
, /* MesaFormat */
71 GL_RGBA
, /* BaseFormat */
72 CHAN_BITS
, /* RedBits */
73 CHAN_BITS
, /* GreenBits */
74 CHAN_BITS
, /* BlueBits */
75 CHAN_BITS
, /* AlphaBits */
76 0, /* LuminanceBits */
77 0, /* IntensityBits */
80 4 * CHAN_BITS
/ 8, /* TexelBytes */
81 fetch_1d_texel_rgba
, /* FetchTexel1D */
82 fetch_2d_texel_rgba
, /* FetchTexel2D */
83 fetch_3d_texel_rgba
, /* FetchTexel3D */
86 const struct gl_texture_format _mesa_texformat_rgb
= {
87 MESA_FORMAT_RGB
, /* MesaFormat */
88 GL_RGB
, /* BaseFormat */
89 CHAN_BITS
, /* RedBits */
90 CHAN_BITS
, /* GreenBits */
91 CHAN_BITS
, /* BlueBits */
93 0, /* LuminanceBits */
94 0, /* IntensityBits */
97 3 * CHAN_BITS
/ 8, /* TexelBytes */
98 fetch_1d_texel_rgb
, /* FetchTexel1D */
99 fetch_2d_texel_rgb
, /* FetchTexel2D */
100 fetch_3d_texel_rgb
, /* FetchTexel3D */
103 const struct gl_texture_format _mesa_texformat_alpha
= {
104 MESA_FORMAT_ALPHA
, /* MesaFormat */
105 GL_ALPHA
, /* BaseFormat */
109 CHAN_BITS
, /* AlphaBits */
110 0, /* LuminanceBits */
111 0, /* IntensityBits */
114 CHAN_BITS
/ 8, /* TexelBytes */
115 fetch_1d_texel_alpha
, /* FetchTexel1D */
116 fetch_2d_texel_alpha
, /* FetchTexel2D */
117 fetch_3d_texel_alpha
, /* FetchTexel3D */
120 const struct gl_texture_format _mesa_texformat_luminance
= {
121 MESA_FORMAT_LUMINANCE
, /* MesaFormat */
122 GL_LUMINANCE
, /* BaseFormat */
127 CHAN_BITS
, /* LuminanceBits */
128 0, /* IntensityBits */
131 CHAN_BITS
/ 8, /* TexelBytes */
132 fetch_1d_texel_luminance
, /* FetchTexel1D */
133 fetch_2d_texel_luminance
, /* FetchTexel2D */
134 fetch_3d_texel_luminance
, /* FetchTexel3D */
137 const struct gl_texture_format _mesa_texformat_luminance_alpha
= {
138 MESA_FORMAT_LUMINANCE_ALPHA
, /* MesaFormat */
139 GL_LUMINANCE_ALPHA
, /* BaseFormat */
143 CHAN_BITS
, /* AlphaBits */
144 CHAN_BITS
, /* LuminanceBits */
145 0, /* IntensityBits */
148 2 * CHAN_BITS
/ 8, /* TexelBytes */
149 fetch_1d_texel_luminance_alpha
, /* FetchTexel1D */
150 fetch_2d_texel_luminance_alpha
, /* FetchTexel2D */
151 fetch_3d_texel_luminance_alpha
, /* FetchTexel3D */
154 const struct gl_texture_format _mesa_texformat_intensity
= {
155 MESA_FORMAT_INTENSITY
, /* MesaFormat */
156 GL_INTENSITY
, /* BaseFormat */
161 0, /* LuminanceBits */
162 CHAN_BITS
, /* IntensityBits */
165 CHAN_BITS
/ 8, /* TexelBytes */
166 fetch_1d_texel_intensity
, /* FetchTexel1D */
167 fetch_2d_texel_intensity
, /* FetchTexel2D */
168 fetch_3d_texel_intensity
, /* FetchTexel3D */
171 const struct gl_texture_format _mesa_texformat_color_index
= {
172 MESA_FORMAT_COLOR_INDEX
, /* MesaFormat */
173 GL_COLOR_INDEX
, /* BaseFormat */
178 0, /* LuminanceBits */
179 0, /* IntensityBits */
180 CHAN_BITS
, /* IndexBits */
182 CHAN_BITS
/ 8, /* TexelBytes */
183 fetch_1d_texel_color_index
, /* FetchTexel1D */
184 fetch_2d_texel_color_index
, /* FetchTexel2D */
185 fetch_3d_texel_color_index
, /* FetchTexel3D */
188 const struct gl_texture_format _mesa_texformat_depth_component
= {
189 MESA_FORMAT_DEPTH_COMPONENT
, /* MesaFormat */
190 GL_DEPTH_COMPONENT
, /* BaseFormat */
195 0, /* LuminanceBits */
196 0, /* IntensityBits */
198 sizeof(GLfloat
) * 8, /* DepthBits */
199 sizeof(GLfloat
), /* TexelBytes */
200 fetch_1d_texel_depth_component
, /* FetchTexel1D */
201 fetch_2d_texel_depth_component
, /* FetchTexel2D */
202 fetch_3d_texel_depth_component
, /* FetchTexel3D */
206 /* =============================================================
210 const struct gl_texture_format _mesa_texformat_rgba8888
= {
211 MESA_FORMAT_RGBA8888
, /* MesaFormat */
212 GL_RGBA
, /* BaseFormat */
217 0, /* LuminanceBits */
218 0, /* IntensityBits */
222 fetch_1d_texel_rgba8888
, /* FetchTexel1D */
223 fetch_2d_texel_rgba8888
, /* FetchTexel2D */
224 fetch_3d_texel_rgba8888
, /* FetchTexel3D */
227 const struct gl_texture_format _mesa_texformat_argb8888
= {
228 MESA_FORMAT_ARGB8888
, /* MesaFormat */
229 GL_RGBA
, /* BaseFormat */
234 0, /* LuminanceBits */
235 0, /* IntensityBits */
239 fetch_1d_texel_argb8888
, /* FetchTexel1D */
240 fetch_2d_texel_argb8888
, /* FetchTexel2D */
241 fetch_3d_texel_argb8888
, /* FetchTexel3D */
244 const struct gl_texture_format _mesa_texformat_rgb888
= {
245 MESA_FORMAT_RGB888
, /* MesaFormat */
246 GL_RGB
, /* BaseFormat */
251 0, /* LuminanceBits */
252 0, /* IntensityBits */
256 fetch_1d_texel_rgb888
, /* FetchTexel1D */
257 fetch_2d_texel_rgb888
, /* FetchTexel2D */
258 fetch_3d_texel_rgb888
, /* FetchTexel3D */
261 const struct gl_texture_format _mesa_texformat_rgb565
= {
262 MESA_FORMAT_RGB565
, /* MesaFormat */
263 GL_RGB
, /* BaseFormat */
268 0, /* LuminanceBits */
269 0, /* IntensityBits */
273 fetch_1d_texel_rgb565
, /* FetchTexel1D */
274 fetch_2d_texel_rgb565
, /* FetchTexel2D */
275 fetch_3d_texel_rgb565
, /* FetchTexel3D */
278 const struct gl_texture_format _mesa_texformat_argb4444
= {
279 MESA_FORMAT_ARGB4444
, /* MesaFormat */
280 GL_RGBA
, /* BaseFormat */
285 0, /* LuminanceBits */
286 0, /* IntensityBits */
290 fetch_1d_texel_argb4444
, /* FetchTexel1D */
291 fetch_2d_texel_argb4444
, /* FetchTexel2D */
292 fetch_3d_texel_argb4444
, /* FetchTexel3D */
295 const struct gl_texture_format _mesa_texformat_argb1555
= {
296 MESA_FORMAT_ARGB1555
, /* MesaFormat */
297 GL_RGBA
, /* BaseFormat */
302 0, /* LuminanceBits */
303 0, /* IntensityBits */
307 fetch_1d_texel_argb1555
, /* FetchTexel1D */
308 fetch_2d_texel_argb1555
, /* FetchTexel2D */
309 fetch_3d_texel_argb1555
, /* FetchTexel3D */
312 const struct gl_texture_format _mesa_texformat_al88
= {
313 MESA_FORMAT_AL88
, /* MesaFormat */
314 GL_LUMINANCE_ALPHA
, /* BaseFormat */
319 8, /* LuminanceBits */
320 0, /* IntensityBits */
324 fetch_1d_texel_al88
, /* FetchTexel1D */
325 fetch_2d_texel_al88
, /* FetchTexel2D */
326 fetch_3d_texel_al88
, /* FetchTexel3D */
329 const struct gl_texture_format _mesa_texformat_rgb332
= {
330 MESA_FORMAT_RGB332
, /* MesaFormat */
331 GL_RGB
, /* BaseFormat */
336 0, /* LuminanceBits */
337 0, /* IntensityBits */
341 fetch_1d_texel_rgb332
, /* FetchTexel1D */
342 fetch_2d_texel_rgb332
, /* FetchTexel2D */
343 fetch_3d_texel_rgb332
, /* FetchTexel3D */
346 const struct gl_texture_format _mesa_texformat_a8
= {
347 MESA_FORMAT_A8
, /* MesaFormat */
348 GL_ALPHA
, /* BaseFormat */
353 0, /* LuminanceBits */
354 0, /* IntensityBits */
358 fetch_1d_texel_a8
, /* FetchTexel1D */
359 fetch_2d_texel_a8
, /* FetchTexel2D */
360 fetch_3d_texel_a8
, /* FetchTexel3D */
363 const struct gl_texture_format _mesa_texformat_l8
= {
364 MESA_FORMAT_L8
, /* MesaFormat */
365 GL_LUMINANCE
, /* BaseFormat */
370 8, /* LuminanceBits */
371 0, /* IntensityBits */
375 fetch_1d_texel_l8
, /* FetchTexel1D */
376 fetch_2d_texel_l8
, /* FetchTexel2D */
377 fetch_3d_texel_l8
, /* FetchTexel3D */
380 const struct gl_texture_format _mesa_texformat_i8
= {
381 MESA_FORMAT_I8
, /* MesaFormat */
382 GL_INTENSITY
, /* BaseFormat */
387 0, /* LuminanceBits */
388 8, /* IntensityBits */
392 fetch_1d_texel_i8
, /* FetchTexel1D */
393 fetch_2d_texel_i8
, /* FetchTexel2D */
394 fetch_3d_texel_i8
, /* FetchTexel3D */
397 const struct gl_texture_format _mesa_texformat_ci8
= {
398 MESA_FORMAT_CI8
, /* MesaFormat */
399 GL_COLOR_INDEX
, /* BaseFormat */
404 0, /* LuminanceBits */
405 0, /* IntensityBits */
409 fetch_1d_texel_ci8
, /* FetchTexel1D */
410 fetch_2d_texel_ci8
, /* FetchTexel2D */
411 fetch_3d_texel_ci8
, /* FetchTexel3D */
414 const struct gl_texture_format _mesa_texformat_ycbcr
= {
415 MESA_FORMAT_YCBCR
, /* MesaFormat */
416 GL_YCBCR_MESA
, /* BaseFormat */
421 0, /* LuminanceBits */
422 0, /* IntensityBits */
426 fetch_1d_texel_ycbcr
, /* FetchTexel1D */
427 fetch_2d_texel_ycbcr
, /* FetchTexel2D */
428 fetch_3d_texel_ycbcr
, /* FetchTexel3D */
432 const struct gl_texture_format _mesa_texformat_ycbcr_rev
= {
433 MESA_FORMAT_YCBCR_REV
, /* MesaFormat */
434 GL_YCBCR_MESA
, /* BaseFormat */
439 0, /* LuminanceBits */
440 0, /* IntensityBits */
444 fetch_1d_texel_ycbcr_rev
, /* FetchTexel1D */
445 fetch_2d_texel_ycbcr_rev
, /* FetchTexel2D */
446 fetch_3d_texel_ycbcr_rev
, /* FetchTexel3D */
452 const struct gl_texture_format _mesa_texformat_abgr8888
= {
453 MESA_FORMAT_ABGR8888
, /* MesaFormat */
454 GL_RGBA
, /* BaseFormat */
455 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
460 0, /* LuminanceBits */
461 0, /* IntensityBits */
465 fetch_1d_texel_abgr8888
, /* FetchTexel1D */
466 fetch_2d_texel_abgr8888
, /* FetchTexel2D */
467 fetch_3d_texel_abgr8888
, /* FetchTexel3D */
470 const struct gl_texture_format _mesa_texformat_bgra8888
= {
471 MESA_FORMAT_BGRA8888
, /* MesaFormat */
472 GL_RGBA
, /* BaseFormat */
473 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
478 0, /* LuminanceBits */
479 0, /* IntensityBits */
483 fetch_1d_texel_bgra8888
, /* FetchTexel1D */
484 fetch_2d_texel_bgra8888
, /* FetchTexel2D */
485 fetch_3d_texel_bgra8888
, /* FetchTexel3D */
488 const struct gl_texture_format _mesa_texformat_bgr888
= {
489 MESA_FORMAT_BGR888
, /* MesaFormat */
490 GL_RGB
, /* BaseFormat */
491 GL_UNSIGNED_BYTE
, /* Type */
496 0, /* LuminanceBits */
497 0, /* IntensityBits */
501 fetch_1d_texel_bgr888
, /* FetchTexel1D */
502 fetch_2d_texel_bgr888
, /* FetchTexel2D */
503 fetch_3d_texel_bgr888
, /* FetchTexel3D */
506 const struct gl_texture_format _mesa_texformat_bgr565
= {
507 MESA_FORMAT_BGR565
, /* MesaFormat */
508 GL_RGB
, /* BaseFormat */
509 GL_UNSIGNED_SHORT_5_6_5
, /* Type */
514 0, /* LuminanceBits */
515 0, /* IntensityBits */
519 fetch_1d_texel_bgr565
, /* FetchTexel1D */
520 fetch_2d_texel_bgr565
, /* FetchTexel2D */
521 fetch_3d_texel_bgr565
, /* FetchTexel3D */
524 const struct gl_texture_format _mesa_texformat_bgra4444
= {
525 MESA_FORMAT_BGRA4444
, /* MesaFormat */
526 GL_RGBA
, /* BaseFormat */
527 GL_UNSIGNED_SHORT_4_4_4_4_REV
, /* Type */
532 0, /* LuminanceBits */
533 0, /* IntensityBits */
537 fetch_1d_texel_bgra4444
, /* FetchTexel1D */
538 fetch_2d_texel_bgra4444
, /* FetchTexel2D */
539 fetch_3d_texel_bgra4444
, /* FetchTexel3D */
542 const struct gl_texture_format _mesa_texformat_bgra5551
= {
543 MESA_FORMAT_BGRA5551
, /* MesaFormat */
544 GL_RGBA
, /* BaseFormat */
545 GL_UNSIGNED_SHORT_1_5_5_5_REV
, /* Type */
550 0, /* LuminanceBits */
551 0, /* IntensityBits */
555 fetch_1d_texel_bgra1555
, /* FetchTexel1D */
556 fetch_2d_texel_bgra1555
, /* FetchTexel2D */
557 fetch_3d_texel_bgra1555
, /* FetchTexel3D */
560 const struct gl_texture_format _mesa_texformat_la88
= {
561 MESA_FORMAT_LA88
, /* MesaFormat */
562 GL_LUMINANCE_ALPHA
, /* BaseFormat */
563 GL_UNSIGNED_BYTE
, /* Type */
568 8, /* LuminanceBits */
569 0, /* IntensityBits */
573 fetch_1d_texel_la88
, /* FetchTexel1D */
574 fetch_2d_texel_la88
, /* FetchTexel2D */
575 fetch_3d_texel_la88
, /* FetchTexel3D */
578 const struct gl_texture_format _mesa_texformat_bgr233
= {
579 MESA_FORMAT_BGR233
, /* MesaFormat */
580 GL_RGB
, /* BaseFormat */
581 GL_UNSIGNED_BYTE_3_3_2
, /* Type */
586 0, /* LuminanceBits */
587 0, /* IntensityBits */
591 fetch_1d_texel_bgr233
, /* FetchTexel1D */
592 fetch_2d_texel_bgr233
, /* FetchTexel2D */
593 fetch_3d_texel_bgr233
, /* FetchTexel3D */
597 /* =============================================================
598 * Null format (useful for proxy textures):
601 const struct gl_texture_format _mesa_null_texformat
= {
608 0, /* LuminanceBits */
609 0, /* IntensityBits */
613 fetch_null_texel
, /* FetchTexel1D */
614 fetch_null_texel
, /* FetchTexel2D */
615 fetch_null_texel
, /* FetchTexel3D */
620 _mesa_is_hardware_tex_format( const struct gl_texture_format
*format
)
622 return (format
->MesaFormat
< MESA_FORMAT_RGBA
);
626 /* Given an internal texture format (or 1, 2, 3, 4) return a pointer
627 * to a gl_texture_format which which to store the texture.
628 * This is called via ctx->Driver.ChooseTextureFormat().
629 * Hardware drivers typically override this function with a specialized
632 const struct gl_texture_format
*
633 _mesa_choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
634 GLenum format
, GLenum type
)
639 switch ( internalFormat
) {
640 /* GH: Bias towards GL_RGB, GL_RGBA texture formats. This has
641 * got to be better than sticking them way down the end of this
644 case 4: /* Quake3 uses this... */
646 return &_mesa_texformat_rgba
;
648 case 3: /* ... and this. */
650 return &_mesa_texformat_rgb
;
652 /* GH: Okay, keep checking as normal. Still test for GL_RGB,
653 * GL_RGBA formats first.
662 return &_mesa_texformat_rgba
;
671 return &_mesa_texformat_rgb
;
678 return &_mesa_texformat_alpha
;
686 return &_mesa_texformat_luminance
;
689 case GL_LUMINANCE_ALPHA
:
690 case GL_LUMINANCE4_ALPHA4
:
691 case GL_LUMINANCE6_ALPHA2
:
692 case GL_LUMINANCE8_ALPHA8
:
693 case GL_LUMINANCE12_ALPHA4
:
694 case GL_LUMINANCE12_ALPHA12
:
695 case GL_LUMINANCE16_ALPHA16
:
696 return &_mesa_texformat_luminance_alpha
;
703 return &_mesa_texformat_intensity
;
706 case GL_COLOR_INDEX1_EXT
:
707 case GL_COLOR_INDEX2_EXT
:
708 case GL_COLOR_INDEX4_EXT
:
709 case GL_COLOR_INDEX8_EXT
:
710 case GL_COLOR_INDEX12_EXT
:
711 case GL_COLOR_INDEX16_EXT
:
712 return &_mesa_texformat_color_index
;
714 case GL_DEPTH_COMPONENT
:
715 case GL_DEPTH_COMPONENT16_SGIX
:
716 case GL_DEPTH_COMPONENT24_SGIX
:
717 case GL_DEPTH_COMPONENT32_SGIX
:
718 if (!ctx
->Extensions
.SGIX_depth_texture
)
719 _mesa_problem(ctx
, "depth format without GL_SGIX_depth_texture");
720 return &_mesa_texformat_depth_component
;
722 case GL_COMPRESSED_ALPHA_ARB
:
723 if (!ctx
->Extensions
.ARB_texture_compression
)
724 _mesa_problem(ctx
, "texture compression extension not enabled");
725 return &_mesa_texformat_alpha
;
726 case GL_COMPRESSED_LUMINANCE_ARB
:
727 if (!ctx
->Extensions
.ARB_texture_compression
)
728 _mesa_problem(ctx
, "texture compression extension not enabled");
729 return &_mesa_texformat_luminance
;
730 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
731 if (!ctx
->Extensions
.ARB_texture_compression
)
732 _mesa_problem(ctx
, "texture compression extension not enabled");
733 return &_mesa_texformat_luminance_alpha
;
734 case GL_COMPRESSED_INTENSITY_ARB
:
735 if (!ctx
->Extensions
.ARB_texture_compression
)
736 _mesa_problem(ctx
, "texture compression extension not enabled");
737 return &_mesa_texformat_intensity
;
738 case GL_COMPRESSED_RGB_ARB
:
739 if (!ctx
->Extensions
.ARB_texture_compression
)
740 _mesa_problem(ctx
, "texture compression extension not enabled");
741 return &_mesa_texformat_rgb
;
742 case GL_COMPRESSED_RGBA_ARB
:
743 if (!ctx
->Extensions
.ARB_texture_compression
)
744 _mesa_problem(ctx
, "texture compression extension not enabled");
745 return &_mesa_texformat_rgba
;
747 /* GL_MESA_ycrcr_texture */
749 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
)
750 return &_mesa_texformat_ycbcr
;
752 return &_mesa_texformat_ycbcr_rev
;
755 _mesa_problem(ctx
, "unexpected format in _mesa_choose_tex_format()");
764 * Return the base texture format for the given compressed format
765 * Called via ctx->Driver.BaseCompressedTexFormat().
766 * This function is used by software rasterizers. Hardware drivers
767 * which support texture compression should not use this function but
768 * a specialized function instead.
771 _mesa_base_compressed_texformat(GLcontext
*ctx
, GLint intFormat
)
774 case GL_COMPRESSED_ALPHA_ARB
:
776 case GL_COMPRESSED_LUMINANCE_ARB
:
778 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
779 return GL_LUMINANCE_ALPHA
;
780 case GL_COMPRESSED_INTENSITY_ARB
:
782 case GL_COMPRESSED_RGB_ARB
:
784 case GL_COMPRESSED_RGBA_ARB
:
787 return -1; /* not a recognized compressed format */