1 /* $Id: texformat.c,v 1.3 2001/03/18 13:31:52 gareth 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"
43 #include "swrast/s_span.h"
47 /* Texel fetch routines for all supported formats:
50 #include "texformat_tmp.h"
53 #include "texformat_tmp.h"
56 #include "texformat_tmp.h"
58 /* Have to have this so the FetchTexel function pointer is never NULL.
60 static void fetch_null_texel( const struct gl_texture_image
*texImage
,
61 GLint i
, GLint j
, GLint k
, GLvoid
*texel
)
63 GLchan
*rgba
= (GLchan
*) texel
;
71 /* ================================================================
72 * Default GLchan-based formats:
75 const struct gl_texture_format _mesa_texformat_rgba
= {
76 MESA_FORMAT_RGBA
, /* IntFormat */
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_RGBA
, /* IntFormat */
93 CHAN_BITS
, /* RedBits */
94 CHAN_BITS
, /* GreenBits */
95 CHAN_BITS
, /* BlueBits */
97 0, /* LuminanceBits */
98 0, /* IntensityBits */
101 3 * CHAN_BITS
/ 8, /* TexelBytes */
102 fetch_1d_texel_rgb
, /* FetchTexel1D */
103 fetch_2d_texel_rgb
, /* FetchTexel2D */
104 fetch_3d_texel_rgb
, /* FetchTexel3D */
107 const struct gl_texture_format _mesa_texformat_alpha
= {
108 MESA_FORMAT_ALPHA
, /* IntFormat */
112 CHAN_BITS
, /* AlphaBits */
113 0, /* LuminanceBits */
114 0, /* IntensityBits */
117 CHAN_BITS
/ 8, /* TexelBytes */
118 fetch_1d_texel_alpha
, /* FetchTexel1D */
119 fetch_2d_texel_alpha
, /* FetchTexel2D */
120 fetch_3d_texel_alpha
, /* FetchTexel3D */
123 const struct gl_texture_format _mesa_texformat_luminance
= {
124 MESA_FORMAT_LUMINANCE
, /* IntFormat */
129 CHAN_BITS
, /* LuminanceBits */
130 0, /* IntensityBits */
133 CHAN_BITS
/ 8, /* TexelBytes */
134 fetch_1d_texel_luminance
, /* FetchTexel1D */
135 fetch_2d_texel_luminance
, /* FetchTexel2D */
136 fetch_3d_texel_luminance
, /* FetchTexel3D */
139 const struct gl_texture_format _mesa_texformat_luminance_alpha
= {
140 MESA_FORMAT_LUMINANCE_ALPHA
, /* IntFormat */
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
, /* IntFormat */
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
, /* IntFormat */
177 0, /* LuminanceBits */
178 0, /* IntensityBits */
179 CHAN_BITS
, /* IndexBits */
181 CHAN_BITS
/ 8, /* TexelBytes */
182 fetch_1d_texel_color_index
, /* FetchTexel1D */
183 fetch_2d_texel_color_index
, /* FetchTexel2D */
184 fetch_3d_texel_color_index
, /* FetchTexel3D */
187 const struct gl_texture_format _mesa_texformat_depth_component
= {
188 MESA_FORMAT_DEPTH_COMPONENT
, /* IntFormat */
193 0, /* LuminanceBits */
194 0, /* IntensityBits */
196 sizeof(GLfloat
) * 8, /* DepthBits */
197 sizeof(GLfloat
), /* TexelBytes */
198 fetch_1d_texel_depth_component
, /* FetchTexel1D */
199 fetch_2d_texel_depth_component
, /* FetchTexel2D */
200 fetch_3d_texel_depth_component
, /* FetchTexel3D */
204 /* ================================================================
208 const struct gl_texture_format _mesa_texformat_rgba8888
= {
209 MESA_FORMAT_RGBA8888
, /* IntFormat */
214 0, /* LuminanceBits */
215 0, /* IntensityBits */
219 fetch_1d_texel_rgba8888
, /* FetchTexel1D */
220 fetch_2d_texel_rgba8888
, /* FetchTexel2D */
221 fetch_3d_texel_rgba8888
, /* FetchTexel3D */
224 const struct gl_texture_format _mesa_texformat_argb8888
= {
225 MESA_FORMAT_ARGB8888
, /* IntFormat */
230 0, /* LuminanceBits */
231 0, /* IntensityBits */
235 fetch_1d_texel_argb8888
, /* FetchTexel1D */
236 fetch_2d_texel_argb8888
, /* FetchTexel2D */
237 fetch_3d_texel_argb8888
, /* FetchTexel3D */
240 const struct gl_texture_format _mesa_texformat_rgb888
= {
241 MESA_FORMAT_RGB888
, /* IntFormat */
246 0, /* LuminanceBits */
247 0, /* IntensityBits */
251 fetch_1d_texel_rgb888
, /* FetchTexel1D */
252 fetch_2d_texel_rgb888
, /* FetchTexel2D */
253 fetch_3d_texel_rgb888
, /* FetchTexel3D */
256 const struct gl_texture_format _mesa_texformat_rgb565
= {
257 MESA_FORMAT_RGB565
, /* IntFormat */
262 0, /* LuminanceBits */
263 0, /* IntensityBits */
267 fetch_1d_texel_rgb565
, /* FetchTexel1D */
268 fetch_2d_texel_rgb565
, /* FetchTexel2D */
269 fetch_3d_texel_rgb565
, /* FetchTexel3D */
272 const struct gl_texture_format _mesa_texformat_argb4444
= {
273 MESA_FORMAT_ARGB4444
, /* IntFormat */
278 0, /* LuminanceBits */
279 0, /* IntensityBits */
283 fetch_1d_texel_argb4444
, /* FetchTexel1D */
284 fetch_2d_texel_argb4444
, /* FetchTexel2D */
285 fetch_3d_texel_argb4444
, /* FetchTexel3D */
288 const struct gl_texture_format _mesa_texformat_argb1555
= {
289 MESA_FORMAT_ARGB1555
, /* IntFormat */
294 0, /* LuminanceBits */
295 0, /* IntensityBits */
299 fetch_1d_texel_argb1555
, /* FetchTexel1D */
300 fetch_2d_texel_argb1555
, /* FetchTexel2D */
301 fetch_3d_texel_argb1555
, /* FetchTexel3D */
304 const struct gl_texture_format _mesa_texformat_al88
= {
305 MESA_FORMAT_AL88
, /* IntFormat */
310 8, /* LuminanceBits */
311 0, /* IntensityBits */
315 fetch_1d_texel_al88
, /* FetchTexel1D */
316 fetch_2d_texel_al88
, /* FetchTexel2D */
317 fetch_3d_texel_al88
, /* FetchTexel3D */
320 const struct gl_texture_format _mesa_texformat_rgb332
= {
321 MESA_FORMAT_RGB332
, /* IntFormat */
326 0, /* LuminanceBits */
327 0, /* IntensityBits */
331 fetch_1d_texel_rgb332
, /* FetchTexel1D */
332 fetch_2d_texel_rgb332
, /* FetchTexel2D */
333 fetch_3d_texel_rgb332
, /* FetchTexel3D */
336 const struct gl_texture_format _mesa_texformat_a8
= {
337 MESA_FORMAT_A8
, /* IntFormat */
342 0, /* LuminanceBits */
343 0, /* IntensityBits */
347 fetch_1d_texel_a8
, /* FetchTexel1D */
348 fetch_2d_texel_a8
, /* FetchTexel2D */
349 fetch_3d_texel_a8
, /* FetchTexel3D */
352 const struct gl_texture_format _mesa_texformat_l8
= {
353 MESA_FORMAT_L8
, /* IntFormat */
358 8, /* LuminanceBits */
359 0, /* IntensityBits */
363 fetch_1d_texel_l8
, /* FetchTexel1D */
364 fetch_2d_texel_l8
, /* FetchTexel2D */
365 fetch_3d_texel_l8
, /* FetchTexel3D */
368 const struct gl_texture_format _mesa_texformat_i8
= {
369 MESA_FORMAT_I8
, /* IntFormat */
374 0, /* LuminanceBits */
375 8, /* IntensityBits */
379 fetch_1d_texel_i8
, /* FetchTexel1D */
380 fetch_2d_texel_i8
, /* FetchTexel2D */
381 fetch_3d_texel_i8
, /* FetchTexel3D */
384 const struct gl_texture_format _mesa_texformat_ci8
= {
385 MESA_FORMAT_CI8
, /* IntFormat */
390 0, /* LuminanceBits */
391 0, /* IntensityBits */
395 fetch_1d_texel_ci8
, /* FetchTexel1D */
396 fetch_2d_texel_ci8
, /* FetchTexel2D */
397 fetch_3d_texel_ci8
, /* FetchTexel3D */
401 /* ================================================================
405 const struct gl_texture_format _mesa_null_texformat
= {
411 0, /* LuminanceBits */
412 0, /* IntensityBits */
416 fetch_null_texel
, /* FetchTexel1D */
417 fetch_null_texel
, /* FetchTexel2D */
418 fetch_null_texel
, /* FetchTexel3D */
423 /* Given an internal texture format or 1, 2, 3, 4 initialize the texture
424 * image structure's default format and type information. Drivers will
425 * initialize these fields accordingly if they override the default
428 void _mesa_init_tex_format( GLcontext
*ctx
, GLenum internalFormat
,
429 struct gl_texture_image
*texImage
)
431 texImage
->IntFormat
= internalFormat
;
433 /* Ask the driver for the base format, if it doesn't know, it will
436 if ( ctx
->Driver
.BaseCompressedTexFormat
) {
437 GLint format
= 0; /* Silence compiler warning */
438 format
= (*ctx
->Driver
.BaseCompressedTexFormat
)( ctx
, format
);
440 internalFormat
= format
;
444 switch ( internalFormat
) {
445 /* GH: Bias towards GL_RGB, GL_RGBA texture formats. This has
446 * got to be better than sticking them way down the end of this
449 case 4: /* Quake3 uses this... */
451 texImage
->Format
= GL_RGBA
;
452 texImage
->Type
= CHAN_TYPE
;
453 texImage
->TexFormat
= &_mesa_texformat_rgba
;
456 case 3: /* ... and this. */
458 texImage
->Format
= GL_RGB
;
459 texImage
->Type
= CHAN_TYPE
;
460 texImage
->TexFormat
= &_mesa_texformat_rgb
;
463 /* GH: Okay, keep checking as normal. Still test for GL_RGB,
464 * GL_RGBA formats first.
473 texImage
->Format
= GL_RGBA
;
474 texImage
->Type
= CHAN_TYPE
;
475 texImage
->TexFormat
= &_mesa_texformat_rgba
;
485 texImage
->Format
= GL_RGB
;
486 texImage
->Type
= CHAN_TYPE
;
487 texImage
->TexFormat
= &_mesa_texformat_rgb
;
495 texImage
->Format
= GL_ALPHA
;
496 texImage
->Type
= CHAN_TYPE
;
497 texImage
->TexFormat
= &_mesa_texformat_alpha
;
506 texImage
->Format
= GL_LUMINANCE
;
507 texImage
->Type
= CHAN_TYPE
;
508 texImage
->TexFormat
= &_mesa_texformat_luminance
;
512 case GL_LUMINANCE_ALPHA
:
513 case GL_LUMINANCE4_ALPHA4
:
514 case GL_LUMINANCE6_ALPHA2
:
515 case GL_LUMINANCE8_ALPHA8
:
516 case GL_LUMINANCE12_ALPHA4
:
517 case GL_LUMINANCE12_ALPHA12
:
518 case GL_LUMINANCE16_ALPHA16
:
519 texImage
->Format
= GL_LUMINANCE_ALPHA
;
520 texImage
->Type
= CHAN_TYPE
;
521 texImage
->TexFormat
= &_mesa_texformat_luminance_alpha
;
529 texImage
->Format
= GL_INTENSITY
;
530 texImage
->Type
= CHAN_TYPE
;
531 texImage
->TexFormat
= &_mesa_texformat_intensity
;
535 case GL_COLOR_INDEX1_EXT
:
536 case GL_COLOR_INDEX2_EXT
:
537 case GL_COLOR_INDEX4_EXT
:
538 case GL_COLOR_INDEX8_EXT
:
539 case GL_COLOR_INDEX12_EXT
:
540 case GL_COLOR_INDEX16_EXT
:
541 texImage
->Format
= GL_COLOR_INDEX
;
542 texImage
->Type
= CHAN_TYPE
;
543 texImage
->TexFormat
= &_mesa_texformat_color_index
;
546 case GL_DEPTH_COMPONENT
:
547 case GL_DEPTH_COMPONENT16_SGIX
:
548 case GL_DEPTH_COMPONENT24_SGIX
:
549 case GL_DEPTH_COMPONENT32_SGIX
:
550 if ( ctx
->Extensions
.SGIX_depth_texture
) {
551 texImage
->Format
= GL_DEPTH_COMPONENT
;
552 texImage
->Type
= GL_FLOAT
; /* XXX or GL_UNSIGNED_INT? */
553 texImage
->TexFormat
= &_mesa_texformat_depth_component
;
555 /* This error should have already been caught and dealt with.
557 _mesa_problem( ctx
, "depth format without GL_SGIX_depth_texture" );
562 _mesa_problem( ctx
, "unexpected format in _mesa_init_tex_format" );