texformat.c revision 1c85aa33321821b44bea83d3dee702ab4e05f406
1/* $Id: texformat.c,v 1.10 2001/04/20 16:46:04 brianp Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.5
6 *
7 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 *
26 * Author:
27 *    Gareth Hughes <gareth@valinux.com>
28 */
29
30#ifdef PC_HEADER
31#include "all.h"
32#else
33#include "glheader.h"
34#include "colormac.h"
35#include "context.h"
36#include "image.h"
37#include "mem.h"
38#include "mmath.h"
39#include "mtypes.h"
40#include "texformat.h"
41#include "teximage.h"
42#include "texstate.h"
43#include "swrast/s_span.h"
44#endif
45
46
47/* Texel fetch routines for all supported formats:
48 */
49#define DIM 1
50#include "texformat_tmp.h"
51
52#define DIM 2
53#include "texformat_tmp.h"
54
55#define DIM 3
56#include "texformat_tmp.h"
57
58/* Have to have this so the FetchTexel function pointer is never NULL.
59 */
60static void fetch_null_texel( const struct gl_texture_image *texImage,
61			      GLint i, GLint j, GLint k, GLvoid *texel )
62{
63   GLchan *rgba = (GLchan *) texel;
64   rgba[RCOMP] = 0;
65   rgba[GCOMP] = 0;
66   rgba[BCOMP] = 0;
67   rgba[ACOMP] = 0;
68}
69
70
71/* =============================================================
72 * Default GLchan-based formats:
73 */
74
75const struct gl_texture_format _mesa_texformat_rgba = {
76   MESA_FORMAT_RGBA,			/* MesaFormat */
77   GL_RGBA,				/* BaseFormat */
78   CHAN_TYPE,				/* Type */
79   CHAN_BITS,				/* RedBits */
80   CHAN_BITS,				/* GreenBits */
81   CHAN_BITS,				/* BlueBits */
82   CHAN_BITS,				/* AlphaBits */
83   0,					/* LuminanceBits */
84   0,					/* IntensityBits */
85   0,					/* IndexBits */
86   0,					/* DepthBits */
87   4 * CHAN_BITS / 8,			/* TexelBytes */
88   fetch_1d_texel_rgba,			/* FetchTexel1D */
89   fetch_2d_texel_rgba,			/* FetchTexel2D */
90   fetch_3d_texel_rgba,			/* FetchTexel3D */
91};
92
93const struct gl_texture_format _mesa_texformat_rgb = {
94   MESA_FORMAT_RGB,			/* MesaFormat */
95   GL_RGB,				/* BaseFormat */
96   CHAN_TYPE,				/* Type */
97   CHAN_BITS,				/* RedBits */
98   CHAN_BITS,				/* GreenBits */
99   CHAN_BITS,				/* BlueBits */
100   0,					/* AlphaBits */
101   0,					/* LuminanceBits */
102   0,					/* IntensityBits */
103   0,					/* IndexBits */
104   0,					/* DepthBits */
105   3 * CHAN_BITS / 8,			/* TexelBytes */
106   fetch_1d_texel_rgb,			/* FetchTexel1D */
107   fetch_2d_texel_rgb,			/* FetchTexel2D */
108   fetch_3d_texel_rgb,			/* FetchTexel3D */
109};
110
111const struct gl_texture_format _mesa_texformat_alpha = {
112   MESA_FORMAT_ALPHA,			/* MesaFormat */
113   GL_ALPHA,				/* BaseFormat */
114   CHAN_TYPE,				/* Type */
115   0,					/* RedBits */
116   0,					/* GreenBits */
117   0,					/* BlueBits */
118   CHAN_BITS,				/* AlphaBits */
119   0,					/* LuminanceBits */
120   0,					/* IntensityBits */
121   0,					/* IndexBits */
122   0,					/* DepthBits */
123   CHAN_BITS / 8,			/* TexelBytes */
124   fetch_1d_texel_alpha,		/* FetchTexel1D */
125   fetch_2d_texel_alpha,		/* FetchTexel2D */
126   fetch_3d_texel_alpha,		/* FetchTexel3D */
127};
128
129const struct gl_texture_format _mesa_texformat_luminance = {
130   MESA_FORMAT_LUMINANCE,		/* MesaFormat */
131   GL_LUMINANCE,			/* BaseFormat */
132   CHAN_TYPE,				/* Type */
133   0,					/* RedBits */
134   0,					/* GreenBits */
135   0,					/* BlueBits */
136   0,					/* AlphaBits */
137   CHAN_BITS,				/* LuminanceBits */
138   0,					/* IntensityBits */
139   0,					/* IndexBits */
140   0,					/* DepthBits */
141   CHAN_BITS / 8,			/* TexelBytes */
142   fetch_1d_texel_luminance,		/* FetchTexel1D */
143   fetch_2d_texel_luminance,		/* FetchTexel2D */
144   fetch_3d_texel_luminance,		/* FetchTexel3D */
145};
146
147const struct gl_texture_format _mesa_texformat_luminance_alpha = {
148   MESA_FORMAT_LUMINANCE_ALPHA,		/* MesaFormat */
149   GL_LUMINANCE_ALPHA,			/* BaseFormat */
150   CHAN_TYPE,				/* Type */
151   0,					/* RedBits */
152   0,					/* GreenBits */
153   0,					/* BlueBits */
154   CHAN_BITS,				/* AlphaBits */
155   CHAN_BITS,				/* LuminanceBits */
156   0,					/* IntensityBits */
157   0,					/* IndexBits */
158   0,					/* DepthBits */
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 */
163};
164
165const struct gl_texture_format _mesa_texformat_intensity = {
166   MESA_FORMAT_INTENSITY,		/* MesaFormat */
167   GL_INTENSITY,			/* BaseFormat */
168   CHAN_TYPE,				/* Type */
169   0,					/* RedBits */
170   0,					/* GreenBits */
171   0,					/* BlueBits */
172   0,					/* AlphaBits */
173   0,					/* LuminanceBits */
174   CHAN_BITS,				/* IntensityBits */
175   0,					/* IndexBits */
176   0,					/* DepthBits */
177   CHAN_BITS / 8,			/* TexelBytes */
178   fetch_1d_texel_intensity,		/* FetchTexel1D */
179   fetch_2d_texel_intensity,		/* FetchTexel2D */
180   fetch_3d_texel_intensity,		/* FetchTexel3D */
181};
182
183const struct gl_texture_format _mesa_texformat_color_index = {
184   MESA_FORMAT_COLOR_INDEX,		/* MesaFormat */
185   GL_COLOR_INDEX,			/* BaseFormat */
186   CHAN_TYPE,				/* Type */
187   0,					/* RedBits */
188   0,					/* GreenBits */
189   0,					/* BlueBits */
190   0,					/* AlphaBits */
191   0,					/* LuminanceBits */
192   0,					/* IntensityBits */
193   CHAN_BITS,				/* IndexBits */
194   0,					/* DepthBits */
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 */
199};
200
201const struct gl_texture_format _mesa_texformat_depth_component = {
202   MESA_FORMAT_DEPTH_COMPONENT,		/* MesaFormat */
203   GL_DEPTH_COMPONENT,			/* BaseFormat */
204   GL_FLOAT,				/* Type */
205   0,					/* RedBits */
206   0,					/* GreenBits */
207   0,					/* BlueBits */
208   0,					/* AlphaBits */
209   0,					/* LuminanceBits */
210   0,					/* IntensityBits */
211   0,					/* IndexBits */
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 */
217};
218
219
220/* =============================================================
221 * Hardware formats:
222 */
223
224const struct gl_texture_format _mesa_texformat_rgba8888 = {
225   MESA_FORMAT_RGBA8888,		/* MesaFormat */
226   GL_RGBA,				/* BaseFormat */
227   GL_UNSIGNED_INT_8_8_8_8,		/* Type */
228   8,					/* RedBits */
229   8,					/* GreenBits */
230   8,					/* BlueBits */
231   8,					/* AlphaBits */
232   0,					/* LuminanceBits */
233   0,					/* IntensityBits */
234   0,					/* IndexBits */
235   0,					/* DepthBits */
236   4,					/* TexelBytes */
237   fetch_1d_texel_rgba8888,		/* FetchTexel1D */
238   fetch_2d_texel_rgba8888,		/* FetchTexel2D */
239   fetch_3d_texel_rgba8888,		/* FetchTexel3D */
240};
241
242const struct gl_texture_format _mesa_texformat_argb8888 = {
243   MESA_FORMAT_ARGB8888,		/* MesaFormat */
244   GL_RGBA,				/* BaseFormat */
245   GL_UNSIGNED_INT_8_8_8_8_REV,		/* Type */
246   8,					/* RedBits */
247   8,					/* GreenBits */
248   8,					/* BlueBits */
249   8,					/* AlphaBits */
250   0,					/* LuminanceBits */
251   0,					/* IntensityBits */
252   0,					/* IndexBits */
253   0,					/* DepthBits */
254   4,					/* TexelBytes */
255   fetch_1d_texel_argb8888,		/* FetchTexel1D */
256   fetch_2d_texel_argb8888,		/* FetchTexel2D */
257   fetch_3d_texel_argb8888,		/* FetchTexel3D */
258};
259
260const struct gl_texture_format _mesa_texformat_rgb888 = {
261   MESA_FORMAT_RGB888,			/* MesaFormat */
262   GL_RGB,				/* BaseFormat */
263   GL_UNSIGNED_BYTE,			/* Type */
264   8,					/* RedBits */
265   8,					/* GreenBits */
266   8,					/* BlueBits */
267   0,					/* AlphaBits */
268   0,					/* LuminanceBits */
269   0,					/* IntensityBits */
270   0,					/* IndexBits */
271   0,					/* DepthBits */
272   3,					/* TexelBytes */
273   fetch_1d_texel_rgb888,		/* FetchTexel1D */
274   fetch_2d_texel_rgb888,		/* FetchTexel2D */
275   fetch_3d_texel_rgb888,		/* FetchTexel3D */
276};
277
278const struct gl_texture_format _mesa_texformat_rgb565 = {
279   MESA_FORMAT_RGB565,			/* MesaFormat */
280   GL_RGB,				/* BaseFormat */
281   GL_UNSIGNED_SHORT_5_6_5,		/* Type */
282   5,					/* RedBits */
283   6,					/* GreenBits */
284   5,					/* BlueBits */
285   0,					/* AlphaBits */
286   0,					/* LuminanceBits */
287   0,					/* IntensityBits */
288   0,					/* IndexBits */
289   0,					/* DepthBits */
290   2,					/* TexelBytes */
291   fetch_1d_texel_rgb565,		/* FetchTexel1D */
292   fetch_2d_texel_rgb565,		/* FetchTexel2D */
293   fetch_3d_texel_rgb565,		/* FetchTexel3D */
294};
295
296const struct gl_texture_format _mesa_texformat_argb4444 = {
297   MESA_FORMAT_ARGB4444,		/* MesaFormat */
298   GL_RGBA,				/* BaseFormat */
299   GL_UNSIGNED_SHORT_4_4_4_4_REV,	/* Type */
300   4,					/* RedBits */
301   4,					/* GreenBits */
302   4,					/* BlueBits */
303   4,					/* AlphaBits */
304   0,					/* LuminanceBits */
305   0,					/* IntensityBits */
306   0,					/* IndexBits */
307   0,					/* DepthBits */
308   2,					/* TexelBytes */
309   fetch_1d_texel_argb4444,		/* FetchTexel1D */
310   fetch_2d_texel_argb4444,		/* FetchTexel2D */
311   fetch_3d_texel_argb4444,		/* FetchTexel3D */
312};
313
314const struct gl_texture_format _mesa_texformat_argb1555 = {
315   MESA_FORMAT_ARGB1555,		/* MesaFormat */
316   GL_RGBA,				/* BaseFormat */
317   GL_UNSIGNED_SHORT_1_5_5_5_REV,	/* Type */
318   5,					/* RedBits */
319   5,					/* GreenBits */
320   5,					/* BlueBits */
321   1,					/* AlphaBits */
322   0,					/* LuminanceBits */
323   0,					/* IntensityBits */
324   0,					/* IndexBits */
325   0,					/* DepthBits */
326   2,					/* TexelBytes */
327   fetch_1d_texel_argb1555,		/* FetchTexel1D */
328   fetch_2d_texel_argb1555,		/* FetchTexel2D */
329   fetch_3d_texel_argb1555,		/* FetchTexel3D */
330};
331
332const struct gl_texture_format _mesa_texformat_al88 = {
333   MESA_FORMAT_AL88,			/* MesaFormat */
334   GL_LUMINANCE_ALPHA,			/* BaseFormat */
335   GL_UNSIGNED_BYTE,			/* Type */
336   0,					/* RedBits */
337   0,					/* GreenBits */
338   0,					/* BlueBits */
339   8,					/* AlphaBits */
340   8,					/* LuminanceBits */
341   0,					/* IntensityBits */
342   0,					/* IndexBits */
343   0,					/* DepthBits */
344   2,					/* TexelBytes */
345   fetch_1d_texel_al88,			/* FetchTexel1D */
346   fetch_2d_texel_al88,			/* FetchTexel2D */
347   fetch_3d_texel_al88,			/* FetchTexel3D */
348};
349
350const struct gl_texture_format _mesa_texformat_rgb332 = {
351   MESA_FORMAT_RGB332,			/* MesaFormat */
352   GL_RGB,				/* BaseFormat */
353   GL_UNSIGNED_BYTE_3_3_2,		/* Type */
354   3,					/* RedBits */
355   3,					/* GreenBits */
356   2,					/* BlueBits */
357   0,					/* AlphaBits */
358   0,					/* LuminanceBits */
359   0,					/* IntensityBits */
360   0,					/* IndexBits */
361   0,					/* DepthBits */
362   1,					/* TexelBytes */
363   fetch_1d_texel_rgb332,		/* FetchTexel1D */
364   fetch_2d_texel_rgb332,		/* FetchTexel2D */
365   fetch_3d_texel_rgb332,		/* FetchTexel3D */
366};
367
368const struct gl_texture_format _mesa_texformat_a8 = {
369   MESA_FORMAT_A8,			/* MesaFormat */
370   GL_ALPHA,				/* BaseFormat */
371   GL_UNSIGNED_BYTE,			/* Type */
372   0,					/* RedBits */
373   0,					/* GreenBits */
374   0,					/* BlueBits */
375   8,					/* AlphaBits */
376   0,					/* LuminanceBits */
377   0,					/* IntensityBits */
378   0,					/* IndexBits */
379   0,					/* DepthBits */
380   1,					/* TexelBytes */
381   fetch_1d_texel_a8,			/* FetchTexel1D */
382   fetch_2d_texel_a8,			/* FetchTexel2D */
383   fetch_3d_texel_a8,			/* FetchTexel3D */
384};
385
386const struct gl_texture_format _mesa_texformat_l8 = {
387   MESA_FORMAT_L8,			/* MesaFormat */
388   GL_LUMINANCE,			/* BaseFormat */
389   GL_UNSIGNED_BYTE,			/* Type */
390   0,					/* RedBits */
391   0,					/* GreenBits */
392   0,					/* BlueBits */
393   0,					/* AlphaBits */
394   8,					/* LuminanceBits */
395   0,					/* IntensityBits */
396   0,					/* IndexBits */
397   0,					/* DepthBits */
398   1,					/* TexelBytes */
399   fetch_1d_texel_l8,			/* FetchTexel1D */
400   fetch_2d_texel_l8,			/* FetchTexel2D */
401   fetch_3d_texel_l8,			/* FetchTexel3D */
402};
403
404const struct gl_texture_format _mesa_texformat_i8 = {
405   MESA_FORMAT_I8,			/* MesaFormat */
406   GL_INTENSITY,			/* BaseFormat */
407   GL_UNSIGNED_BYTE,			/* Type */
408   0,					/* RedBits */
409   0,					/* GreenBits */
410   0,					/* BlueBits */
411   0,					/* AlphaBits */
412   0,					/* LuminanceBits */
413   8,					/* IntensityBits */
414   0,					/* IndexBits */
415   0,					/* DepthBits */
416   1,					/* TexelBytes */
417   fetch_1d_texel_i8,			/* FetchTexel1D */
418   fetch_2d_texel_i8,			/* FetchTexel2D */
419   fetch_3d_texel_i8,			/* FetchTexel3D */
420};
421
422const struct gl_texture_format _mesa_texformat_ci8 = {
423   MESA_FORMAT_CI8,			/* MesaFormat */
424   GL_COLOR_INDEX,			/* BaseFormat */
425   GL_UNSIGNED_BYTE,			/* Type */
426   0,					/* RedBits */
427   0,					/* GreenBits */
428   0,					/* BlueBits */
429   0,					/* AlphaBits */
430   0,					/* LuminanceBits */
431   0,					/* IntensityBits */
432   8,					/* IndexBits */
433   0,					/* DepthBits */
434   1,					/* TexelBytes */
435   fetch_1d_texel_ci8,			/* FetchTexel1D */
436   fetch_2d_texel_ci8,			/* FetchTexel2D */
437   fetch_3d_texel_ci8,			/* FetchTexel3D */
438};
439
440
441/* =============================================================
442 * Null format:
443 */
444
445const struct gl_texture_format _mesa_null_texformat = {
446   -1,					/* MesaFormat */
447   0,					/* BaseFormat */
448   0,					/* Type */
449   0,					/* RedBits */
450   0,					/* GreenBits */
451   0,					/* BlueBits */
452   0,					/* AlphaBits */
453   0,					/* LuminanceBits */
454   0,					/* IntensityBits */
455   0,					/* IndexBits */
456   0,					/* DepthBits */
457   0,					/* TexelBytes */
458   fetch_null_texel,			/* FetchTexel1D */
459   fetch_null_texel,			/* FetchTexel2D */
460   fetch_null_texel,			/* FetchTexel3D */
461};
462
463
464
465GLboolean
466_mesa_is_hardware_tex_format( const struct gl_texture_format *format )
467{
468   return (format->MesaFormat < MESA_FORMAT_RGBA);
469}
470
471
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
475 * storage format.
476 */
477const struct gl_texture_format *
478_mesa_choose_tex_format( GLcontext *ctx, GLint internalFormat,
479                         GLenum format, GLenum type )
480{
481   (void) format;
482   (void) type;
483
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
487       * huge list.
488       */
489   case 4:				/* Quake3 uses this... */
490   case GL_RGBA:
491      return &_mesa_texformat_rgba;
492
493   case 3:				/* ... and this. */
494   case GL_RGB:
495      return &_mesa_texformat_rgb;
496
497      /* GH: Okay, keep checking as normal.  Still test for GL_RGB,
498       * GL_RGBA formats first.
499       */
500   case GL_RGBA2:
501   case GL_RGBA4:
502   case GL_RGB5_A1:
503   case GL_RGBA8:
504   case GL_RGB10_A2:
505   case GL_RGBA12:
506   case GL_RGBA16:
507      return &_mesa_texformat_rgba;
508
509   case GL_R3_G3_B2:
510   case GL_RGB4:
511   case GL_RGB5:
512   case GL_RGB8:
513   case GL_RGB10:
514   case GL_RGB12:
515   case GL_RGB16:
516      return &_mesa_texformat_rgb;
517
518   case GL_ALPHA:
519   case GL_ALPHA4:
520   case GL_ALPHA8:
521   case GL_ALPHA12:
522   case GL_ALPHA16:
523      return &_mesa_texformat_alpha;
524
525   case 1:
526   case GL_LUMINANCE:
527   case GL_LUMINANCE4:
528   case GL_LUMINANCE8:
529   case GL_LUMINANCE12:
530   case GL_LUMINANCE16:
531      return &_mesa_texformat_luminance;
532
533   case 2:
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;
542
543   case GL_INTENSITY:
544   case GL_INTENSITY4:
545   case GL_INTENSITY8:
546   case GL_INTENSITY12:
547   case GL_INTENSITY16:
548      return &_mesa_texformat_intensity;
549
550   case GL_COLOR_INDEX:
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;
558
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;
566
567   default:
568      _mesa_problem( ctx, "unexpected format in _mesa_choose_tex_format()" );
569      printf("intformat = %d %x\n", internalFormat, internalFormat );
570      return NULL;
571   }
572}
573