1 /* $Id: texformat.c,v 1.8 2001/04/04 21:54:21 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"
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
, /* MesaFormat */
77 GL_RGBA
, /* IntFormat */
79 CHAN_BITS
, /* RedBits */
80 CHAN_BITS
, /* GreenBits */
81 CHAN_BITS
, /* BlueBits */
82 CHAN_BITS
, /* AlphaBits */
83 0, /* LuminanceBits */
84 0, /* IntensityBits */
87 4 * CHAN_BITS
/ 8, /* TexelBytes */
88 fetch_1d_texel_rgba
, /* FetchTexel1D */
89 fetch_2d_texel_rgba
, /* FetchTexel2D */
90 fetch_3d_texel_rgba
, /* FetchTexel3D */
93 const struct gl_texture_format _mesa_texformat_rgb
= {
94 MESA_FORMAT_RGB
, /* MesaFormat */
95 GL_RGB
, /* IntFormat */
97 CHAN_BITS
, /* RedBits */
98 CHAN_BITS
, /* GreenBits */
99 CHAN_BITS
, /* BlueBits */
101 0, /* LuminanceBits */
102 0, /* IntensityBits */
105 3 * CHAN_BITS
/ 8, /* TexelBytes */
106 fetch_1d_texel_rgb
, /* FetchTexel1D */
107 fetch_2d_texel_rgb
, /* FetchTexel2D */
108 fetch_3d_texel_rgb
, /* FetchTexel3D */
111 const struct gl_texture_format _mesa_texformat_alpha
= {
112 MESA_FORMAT_ALPHA
, /* MesaFormat */
113 GL_ALPHA
, /* IntFormat */
114 CHAN_TYPE
, /* Type */
118 CHAN_BITS
, /* AlphaBits */
119 0, /* LuminanceBits */
120 0, /* IntensityBits */
123 CHAN_BITS
/ 8, /* TexelBytes */
124 fetch_1d_texel_alpha
, /* FetchTexel1D */
125 fetch_2d_texel_alpha
, /* FetchTexel2D */
126 fetch_3d_texel_alpha
, /* FetchTexel3D */
129 const struct gl_texture_format _mesa_texformat_luminance
= {
130 MESA_FORMAT_LUMINANCE
, /* MesaFormat */
131 GL_LUMINANCE
, /* IntFormat */
132 CHAN_TYPE
, /* Type */
137 CHAN_BITS
, /* LuminanceBits */
138 0, /* IntensityBits */
141 CHAN_BITS
/ 8, /* TexelBytes */
142 fetch_1d_texel_luminance
, /* FetchTexel1D */
143 fetch_2d_texel_luminance
, /* FetchTexel2D */
144 fetch_3d_texel_luminance
, /* FetchTexel3D */
147 const struct gl_texture_format _mesa_texformat_luminance_alpha
= {
148 MESA_FORMAT_LUMINANCE_ALPHA
, /* MesaFormat */
149 GL_LUMINANCE_ALPHA
, /* IntFormat */
150 CHAN_TYPE
, /* Type */
154 CHAN_BITS
, /* AlphaBits */
155 CHAN_BITS
, /* LuminanceBits */
156 0, /* IntensityBits */
159 2 * CHAN_BITS
/ 8, /* TexelBytes */
160 fetch_1d_texel_luminance_alpha
, /* FetchTexel1D */
161 fetch_2d_texel_luminance_alpha
, /* FetchTexel2D */
162 fetch_3d_texel_luminance_alpha
, /* FetchTexel3D */
165 const struct gl_texture_format _mesa_texformat_intensity
= {
166 MESA_FORMAT_INTENSITY
, /* MesaFormat */
167 GL_INTENSITY
, /* IntFormat */
168 CHAN_TYPE
, /* Type */
173 0, /* LuminanceBits */
174 CHAN_BITS
, /* IntensityBits */
177 CHAN_BITS
/ 8, /* TexelBytes */
178 fetch_1d_texel_intensity
, /* FetchTexel1D */
179 fetch_2d_texel_intensity
, /* FetchTexel2D */
180 fetch_3d_texel_intensity
, /* FetchTexel3D */
183 const struct gl_texture_format _mesa_texformat_color_index
= {
184 MESA_FORMAT_COLOR_INDEX
, /* MesaFormat */
185 GL_COLOR_INDEX
, /* IntFormat */
186 CHAN_TYPE
, /* Type */
191 0, /* LuminanceBits */
192 0, /* IntensityBits */
193 CHAN_BITS
, /* IndexBits */
195 CHAN_BITS
/ 8, /* TexelBytes */
196 fetch_1d_texel_color_index
, /* FetchTexel1D */
197 fetch_2d_texel_color_index
, /* FetchTexel2D */
198 fetch_3d_texel_color_index
, /* FetchTexel3D */
201 const struct gl_texture_format _mesa_texformat_depth_component
= {
202 MESA_FORMAT_DEPTH_COMPONENT
, /* MesaFormat */
203 GL_DEPTH_COMPONENT
, /* IntFormat */
209 0, /* LuminanceBits */
210 0, /* IntensityBits */
212 sizeof(GLfloat
) * 8, /* DepthBits */
213 sizeof(GLfloat
), /* TexelBytes */
214 fetch_1d_texel_depth_component
, /* FetchTexel1D */
215 fetch_2d_texel_depth_component
, /* FetchTexel2D */
216 fetch_3d_texel_depth_component
, /* FetchTexel3D */
220 /* =============================================================
224 const struct gl_texture_format _mesa_texformat_rgba8888
= {
225 MESA_FORMAT_RGBA8888
, /* MesaFormat */
226 GL_RGBA
, /* IntFormat */
227 GL_UNSIGNED_INT_8_8_8_8
, /* Type */
232 0, /* LuminanceBits */
233 0, /* IntensityBits */
237 fetch_1d_texel_rgba8888
, /* FetchTexel1D */
238 fetch_2d_texel_rgba8888
, /* FetchTexel2D */
239 fetch_3d_texel_rgba8888
, /* FetchTexel3D */
242 const struct gl_texture_format _mesa_texformat_argb8888
= {
243 MESA_FORMAT_ARGB8888
, /* MesaFormat */
244 GL_BGRA
, /* IntFormat */
245 GL_UNSIGNED_INT_8_8_8_8_REV
, /* Type */
250 0, /* LuminanceBits */
251 0, /* IntensityBits */
255 fetch_1d_texel_argb8888
, /* FetchTexel1D */
256 fetch_2d_texel_argb8888
, /* FetchTexel2D */
257 fetch_3d_texel_argb8888
, /* FetchTexel3D */
260 const struct gl_texture_format _mesa_texformat_rgb888
= {
261 MESA_FORMAT_RGB888
, /* MesaFormat */
262 GL_BGR
, /* IntFormat */
263 GL_UNSIGNED_BYTE
, /* Type */
268 0, /* LuminanceBits */
269 0, /* IntensityBits */
273 fetch_1d_texel_rgb888
, /* FetchTexel1D */
274 fetch_2d_texel_rgb888
, /* FetchTexel2D */
275 fetch_3d_texel_rgb888
, /* FetchTexel3D */
278 const struct gl_texture_format _mesa_texformat_rgb565
= {
279 MESA_FORMAT_RGB565
, /* MesaFormat */
280 GL_RGB
, /* IntFormat */
281 GL_UNSIGNED_SHORT_5_6_5
, /* Type */
286 0, /* LuminanceBits */
287 0, /* IntensityBits */
291 fetch_1d_texel_rgb565
, /* FetchTexel1D */
292 fetch_2d_texel_rgb565
, /* FetchTexel2D */
293 fetch_3d_texel_rgb565
, /* FetchTexel3D */
296 const struct gl_texture_format _mesa_texformat_argb4444
= {
297 MESA_FORMAT_ARGB4444
, /* MesaFormat */
298 GL_BGRA
, /* IntFormat */
299 GL_UNSIGNED_SHORT_4_4_4_4_REV
, /* Type */
304 0, /* LuminanceBits */
305 0, /* IntensityBits */
309 fetch_1d_texel_argb4444
, /* FetchTexel1D */
310 fetch_2d_texel_argb4444
, /* FetchTexel2D */
311 fetch_3d_texel_argb4444
, /* FetchTexel3D */
314 const struct gl_texture_format _mesa_texformat_argb1555
= {
315 MESA_FORMAT_ARGB1555
, /* MesaFormat */
316 GL_BGRA
, /* IntFormat */
317 GL_UNSIGNED_SHORT_1_5_5_5_REV
, /* Type */
322 0, /* LuminanceBits */
323 0, /* IntensityBits */
327 fetch_1d_texel_argb1555
, /* FetchTexel1D */
328 fetch_2d_texel_argb1555
, /* FetchTexel2D */
329 fetch_3d_texel_argb1555
, /* FetchTexel3D */
332 const struct gl_texture_format _mesa_texformat_al88
= {
333 MESA_FORMAT_AL88
, /* MesaFormat */
334 GL_LUMINANCE_ALPHA
, /* IntFormat */
335 GL_UNSIGNED_BYTE
, /* Type */
340 8, /* LuminanceBits */
341 0, /* IntensityBits */
345 fetch_1d_texel_al88
, /* FetchTexel1D */
346 fetch_2d_texel_al88
, /* FetchTexel2D */
347 fetch_3d_texel_al88
, /* FetchTexel3D */
350 const struct gl_texture_format _mesa_texformat_rgb332
= {
351 MESA_FORMAT_RGB332
, /* MesaFormat */
352 GL_RGB
, /* IntFormat */
353 GL_UNSIGNED_BYTE_3_3_2
, /* Type */
358 0, /* LuminanceBits */
359 0, /* IntensityBits */
363 fetch_1d_texel_rgb332
, /* FetchTexel1D */
364 fetch_2d_texel_rgb332
, /* FetchTexel2D */
365 fetch_3d_texel_rgb332
, /* FetchTexel3D */
368 const struct gl_texture_format _mesa_texformat_a8
= {
369 MESA_FORMAT_A8
, /* MesaFormat */
370 GL_ALPHA
, /* IntFormat */
371 GL_UNSIGNED_BYTE
, /* Type */
376 0, /* LuminanceBits */
377 0, /* IntensityBits */
381 fetch_1d_texel_a8
, /* FetchTexel1D */
382 fetch_2d_texel_a8
, /* FetchTexel2D */
383 fetch_3d_texel_a8
, /* FetchTexel3D */
386 const struct gl_texture_format _mesa_texformat_l8
= {
387 MESA_FORMAT_L8
, /* MesaFormat */
388 GL_LUMINANCE
, /* IntFormat */
389 GL_UNSIGNED_BYTE
, /* Type */
394 8, /* LuminanceBits */
395 0, /* IntensityBits */
399 fetch_1d_texel_l8
, /* FetchTexel1D */
400 fetch_2d_texel_l8
, /* FetchTexel2D */
401 fetch_3d_texel_l8
, /* FetchTexel3D */
404 const struct gl_texture_format _mesa_texformat_i8
= {
405 MESA_FORMAT_I8
, /* MesaFormat */
406 GL_INTENSITY
, /* IntFormat */
407 GL_UNSIGNED_BYTE
, /* Type */
412 0, /* LuminanceBits */
413 8, /* IntensityBits */
417 fetch_1d_texel_i8
, /* FetchTexel1D */
418 fetch_2d_texel_i8
, /* FetchTexel2D */
419 fetch_3d_texel_i8
, /* FetchTexel3D */
422 const struct gl_texture_format _mesa_texformat_ci8
= {
423 MESA_FORMAT_CI8
, /* MesaFormat */
424 GL_COLOR_INDEX
, /* IntFormat */
425 GL_UNSIGNED_BYTE
, /* Type */
430 0, /* LuminanceBits */
431 0, /* IntensityBits */
435 fetch_1d_texel_ci8
, /* FetchTexel1D */
436 fetch_2d_texel_ci8
, /* FetchTexel2D */
437 fetch_3d_texel_ci8
, /* FetchTexel3D */
441 /* =============================================================
445 const struct gl_texture_format _mesa_null_texformat
= {
453 0, /* LuminanceBits */
454 0, /* IntensityBits */
458 fetch_null_texel
, /* FetchTexel1D */
459 fetch_null_texel
, /* FetchTexel2D */
460 fetch_null_texel
, /* FetchTexel3D */
466 _mesa_is_hardware_tex_format( const struct gl_texture_format
*format
)
468 return (format
->MesaFormat
< MESA_FORMAT_RGBA
);
472 /* Given an internal texture format or 1, 2, 3, 4 initialize the texture
473 * image structure's default format and type information. Drivers will
474 * initialize these fields accordingly if they override the default
477 const struct gl_texture_format
*
478 _mesa_choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
479 GLenum format
, GLenum type
)
484 switch ( internalFormat
) {
485 /* GH: Bias towards GL_RGB, GL_RGBA texture formats. This has
486 * got to be better than sticking them way down the end of this
489 case 4: /* Quake3 uses this... */
491 return &_mesa_texformat_rgba
;
493 case 3: /* ... and this. */
495 return &_mesa_texformat_rgb
;
497 /* GH: Okay, keep checking as normal. Still test for GL_RGB,
498 * GL_RGBA formats first.
507 return &_mesa_texformat_rgba
;
516 return &_mesa_texformat_rgb
;
523 return &_mesa_texformat_alpha
;
531 return &_mesa_texformat_luminance
;
534 case GL_LUMINANCE_ALPHA
:
535 case GL_LUMINANCE4_ALPHA4
:
536 case GL_LUMINANCE6_ALPHA2
:
537 case GL_LUMINANCE8_ALPHA8
:
538 case GL_LUMINANCE12_ALPHA4
:
539 case GL_LUMINANCE12_ALPHA12
:
540 case GL_LUMINANCE16_ALPHA16
:
541 return &_mesa_texformat_luminance_alpha
;
548 return &_mesa_texformat_intensity
;
551 case GL_COLOR_INDEX1_EXT
:
552 case GL_COLOR_INDEX2_EXT
:
553 case GL_COLOR_INDEX4_EXT
:
554 case GL_COLOR_INDEX8_EXT
:
555 case GL_COLOR_INDEX12_EXT
:
556 case GL_COLOR_INDEX16_EXT
:
557 return &_mesa_texformat_color_index
;
559 case GL_DEPTH_COMPONENT
:
560 case GL_DEPTH_COMPONENT16_SGIX
:
561 case GL_DEPTH_COMPONENT24_SGIX
:
562 case GL_DEPTH_COMPONENT32_SGIX
:
563 if ( !ctx
->Extensions
.SGIX_depth_texture
)
564 _mesa_problem( ctx
, "depth format without GL_SGIX_depth_texture" );
565 return &_mesa_texformat_depth_component
;
568 _mesa_problem( ctx
, "unexpected format in _mesa_choose_tex_format()" );
569 printf("intformat = %d %x\n", internalFormat
, internalFormat
);