fix for gl_ztrick bug (Ove Kaaven)
[mesa.git] / src / mesa / main / texformat.c
1 /* $Id: texformat.c,v 1.3 2001/03/18 13:31:52 gareth 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 */
60 static 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
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 */
83 0, /* IndexBits */
84 0, /* DepthBits */
85 4 * CHAN_BITS / 8, /* TexelBytes */
86 fetch_1d_texel_rgba, /* FetchTexel1D */
87 fetch_2d_texel_rgba, /* FetchTexel2D */
88 fetch_3d_texel_rgba, /* FetchTexel3D */
89 };
90
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 */
96 0, /* AlphaBits */
97 0, /* LuminanceBits */
98 0, /* IntensityBits */
99 0, /* IndexBits */
100 0, /* DepthBits */
101 3 * CHAN_BITS / 8, /* TexelBytes */
102 fetch_1d_texel_rgb, /* FetchTexel1D */
103 fetch_2d_texel_rgb, /* FetchTexel2D */
104 fetch_3d_texel_rgb, /* FetchTexel3D */
105 };
106
107 const struct gl_texture_format _mesa_texformat_alpha = {
108 MESA_FORMAT_ALPHA, /* IntFormat */
109 0, /* RedBits */
110 0, /* GreenBits */
111 0, /* BlueBits */
112 CHAN_BITS, /* AlphaBits */
113 0, /* LuminanceBits */
114 0, /* IntensityBits */
115 0, /* IndexBits */
116 0, /* DepthBits */
117 CHAN_BITS / 8, /* TexelBytes */
118 fetch_1d_texel_alpha, /* FetchTexel1D */
119 fetch_2d_texel_alpha, /* FetchTexel2D */
120 fetch_3d_texel_alpha, /* FetchTexel3D */
121 };
122
123 const struct gl_texture_format _mesa_texformat_luminance = {
124 MESA_FORMAT_LUMINANCE, /* IntFormat */
125 0, /* RedBits */
126 0, /* GreenBits */
127 0, /* BlueBits */
128 0, /* AlphaBits */
129 CHAN_BITS, /* LuminanceBits */
130 0, /* IntensityBits */
131 0, /* IndexBits */
132 0, /* DepthBits */
133 CHAN_BITS / 8, /* TexelBytes */
134 fetch_1d_texel_luminance, /* FetchTexel1D */
135 fetch_2d_texel_luminance, /* FetchTexel2D */
136 fetch_3d_texel_luminance, /* FetchTexel3D */
137 };
138
139 const struct gl_texture_format _mesa_texformat_luminance_alpha = {
140 MESA_FORMAT_LUMINANCE_ALPHA, /* IntFormat */
141 0, /* RedBits */
142 0, /* GreenBits */
143 0, /* BlueBits */
144 CHAN_BITS, /* AlphaBits */
145 CHAN_BITS, /* LuminanceBits */
146 0, /* IntensityBits */
147 0, /* IndexBits */
148 0, /* DepthBits */
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 */
153 };
154
155 const struct gl_texture_format _mesa_texformat_intensity = {
156 MESA_FORMAT_INTENSITY, /* IntFormat */
157 0, /* RedBits */
158 0, /* GreenBits */
159 0, /* BlueBits */
160 0, /* AlphaBits */
161 0, /* LuminanceBits */
162 CHAN_BITS, /* IntensityBits */
163 0, /* IndexBits */
164 0, /* DepthBits */
165 CHAN_BITS / 8, /* TexelBytes */
166 fetch_1d_texel_intensity, /* FetchTexel1D */
167 fetch_2d_texel_intensity, /* FetchTexel2D */
168 fetch_3d_texel_intensity, /* FetchTexel3D */
169 };
170
171 const struct gl_texture_format _mesa_texformat_color_index = {
172 MESA_FORMAT_COLOR_INDEX, /* IntFormat */
173 0, /* RedBits */
174 0, /* GreenBits */
175 0, /* BlueBits */
176 0, /* AlphaBits */
177 0, /* LuminanceBits */
178 0, /* IntensityBits */
179 CHAN_BITS, /* IndexBits */
180 0, /* DepthBits */
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 */
185 };
186
187 const struct gl_texture_format _mesa_texformat_depth_component = {
188 MESA_FORMAT_DEPTH_COMPONENT, /* IntFormat */
189 0, /* RedBits */
190 0, /* GreenBits */
191 0, /* BlueBits */
192 0, /* AlphaBits */
193 0, /* LuminanceBits */
194 0, /* IntensityBits */
195 0, /* IndexBits */
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 */
201 };
202
203
204 /* ================================================================
205 * Hardware formats:
206 */
207
208 const struct gl_texture_format _mesa_texformat_rgba8888 = {
209 MESA_FORMAT_RGBA8888, /* IntFormat */
210 8, /* RedBits */
211 8, /* GreenBits */
212 8, /* BlueBits */
213 8, /* AlphaBits */
214 0, /* LuminanceBits */
215 0, /* IntensityBits */
216 0, /* IndexBits */
217 0, /* DepthBits */
218 4, /* TexelBytes */
219 fetch_1d_texel_rgba8888, /* FetchTexel1D */
220 fetch_2d_texel_rgba8888, /* FetchTexel2D */
221 fetch_3d_texel_rgba8888, /* FetchTexel3D */
222 };
223
224 const struct gl_texture_format _mesa_texformat_argb8888 = {
225 MESA_FORMAT_ARGB8888, /* IntFormat */
226 8, /* RedBits */
227 8, /* GreenBits */
228 8, /* BlueBits */
229 8, /* AlphaBits */
230 0, /* LuminanceBits */
231 0, /* IntensityBits */
232 0, /* IndexBits */
233 0, /* DepthBits */
234 4, /* TexelBytes */
235 fetch_1d_texel_argb8888, /* FetchTexel1D */
236 fetch_2d_texel_argb8888, /* FetchTexel2D */
237 fetch_3d_texel_argb8888, /* FetchTexel3D */
238 };
239
240 const struct gl_texture_format _mesa_texformat_rgb888 = {
241 MESA_FORMAT_RGB888, /* IntFormat */
242 8, /* RedBits */
243 8, /* GreenBits */
244 8, /* BlueBits */
245 0, /* AlphaBits */
246 0, /* LuminanceBits */
247 0, /* IntensityBits */
248 0, /* IndexBits */
249 0, /* DepthBits */
250 3, /* TexelBytes */
251 fetch_1d_texel_rgb888, /* FetchTexel1D */
252 fetch_2d_texel_rgb888, /* FetchTexel2D */
253 fetch_3d_texel_rgb888, /* FetchTexel3D */
254 };
255
256 const struct gl_texture_format _mesa_texformat_rgb565 = {
257 MESA_FORMAT_RGB565, /* IntFormat */
258 5, /* RedBits */
259 6, /* GreenBits */
260 5, /* BlueBits */
261 0, /* AlphaBits */
262 0, /* LuminanceBits */
263 0, /* IntensityBits */
264 0, /* IndexBits */
265 0, /* DepthBits */
266 2, /* TexelBytes */
267 fetch_1d_texel_rgb565, /* FetchTexel1D */
268 fetch_2d_texel_rgb565, /* FetchTexel2D */
269 fetch_3d_texel_rgb565, /* FetchTexel3D */
270 };
271
272 const struct gl_texture_format _mesa_texformat_argb4444 = {
273 MESA_FORMAT_ARGB4444, /* IntFormat */
274 4, /* RedBits */
275 4, /* GreenBits */
276 4, /* BlueBits */
277 4, /* AlphaBits */
278 0, /* LuminanceBits */
279 0, /* IntensityBits */
280 0, /* IndexBits */
281 0, /* DepthBits */
282 2, /* TexelBytes */
283 fetch_1d_texel_argb4444, /* FetchTexel1D */
284 fetch_2d_texel_argb4444, /* FetchTexel2D */
285 fetch_3d_texel_argb4444, /* FetchTexel3D */
286 };
287
288 const struct gl_texture_format _mesa_texformat_argb1555 = {
289 MESA_FORMAT_ARGB1555, /* IntFormat */
290 5, /* RedBits */
291 5, /* GreenBits */
292 5, /* BlueBits */
293 1, /* AlphaBits */
294 0, /* LuminanceBits */
295 0, /* IntensityBits */
296 0, /* IndexBits */
297 0, /* DepthBits */
298 2, /* TexelBytes */
299 fetch_1d_texel_argb1555, /* FetchTexel1D */
300 fetch_2d_texel_argb1555, /* FetchTexel2D */
301 fetch_3d_texel_argb1555, /* FetchTexel3D */
302 };
303
304 const struct gl_texture_format _mesa_texformat_al88 = {
305 MESA_FORMAT_AL88, /* IntFormat */
306 0, /* RedBits */
307 0, /* GreenBits */
308 0, /* BlueBits */
309 8, /* AlphaBits */
310 8, /* LuminanceBits */
311 0, /* IntensityBits */
312 0, /* IndexBits */
313 0, /* DepthBits */
314 2, /* TexelBytes */
315 fetch_1d_texel_al88, /* FetchTexel1D */
316 fetch_2d_texel_al88, /* FetchTexel2D */
317 fetch_3d_texel_al88, /* FetchTexel3D */
318 };
319
320 const struct gl_texture_format _mesa_texformat_rgb332 = {
321 MESA_FORMAT_RGB332, /* IntFormat */
322 3, /* RedBits */
323 3, /* GreenBits */
324 2, /* BlueBits */
325 0, /* AlphaBits */
326 0, /* LuminanceBits */
327 0, /* IntensityBits */
328 0, /* IndexBits */
329 0, /* DepthBits */
330 1, /* TexelBytes */
331 fetch_1d_texel_rgb332, /* FetchTexel1D */
332 fetch_2d_texel_rgb332, /* FetchTexel2D */
333 fetch_3d_texel_rgb332, /* FetchTexel3D */
334 };
335
336 const struct gl_texture_format _mesa_texformat_a8 = {
337 MESA_FORMAT_A8, /* IntFormat */
338 0, /* RedBits */
339 0, /* GreenBits */
340 0, /* BlueBits */
341 8, /* AlphaBits */
342 0, /* LuminanceBits */
343 0, /* IntensityBits */
344 0, /* IndexBits */
345 0, /* DepthBits */
346 1, /* TexelBytes */
347 fetch_1d_texel_a8, /* FetchTexel1D */
348 fetch_2d_texel_a8, /* FetchTexel2D */
349 fetch_3d_texel_a8, /* FetchTexel3D */
350 };
351
352 const struct gl_texture_format _mesa_texformat_l8 = {
353 MESA_FORMAT_L8, /* IntFormat */
354 0, /* RedBits */
355 0, /* GreenBits */
356 0, /* BlueBits */
357 0, /* AlphaBits */
358 8, /* LuminanceBits */
359 0, /* IntensityBits */
360 0, /* IndexBits */
361 0, /* DepthBits */
362 1, /* TexelBytes */
363 fetch_1d_texel_l8, /* FetchTexel1D */
364 fetch_2d_texel_l8, /* FetchTexel2D */
365 fetch_3d_texel_l8, /* FetchTexel3D */
366 };
367
368 const struct gl_texture_format _mesa_texformat_i8 = {
369 MESA_FORMAT_I8, /* IntFormat */
370 0, /* RedBits */
371 0, /* GreenBits */
372 0, /* BlueBits */
373 0, /* AlphaBits */
374 0, /* LuminanceBits */
375 8, /* IntensityBits */
376 0, /* IndexBits */
377 0, /* DepthBits */
378 1, /* TexelBytes */
379 fetch_1d_texel_i8, /* FetchTexel1D */
380 fetch_2d_texel_i8, /* FetchTexel2D */
381 fetch_3d_texel_i8, /* FetchTexel3D */
382 };
383
384 const struct gl_texture_format _mesa_texformat_ci8 = {
385 MESA_FORMAT_CI8, /* IntFormat */
386 0, /* RedBits */
387 0, /* GreenBits */
388 0, /* BlueBits */
389 0, /* AlphaBits */
390 0, /* LuminanceBits */
391 0, /* IntensityBits */
392 8, /* IndexBits */
393 0, /* DepthBits */
394 1, /* TexelBytes */
395 fetch_1d_texel_ci8, /* FetchTexel1D */
396 fetch_2d_texel_ci8, /* FetchTexel2D */
397 fetch_3d_texel_ci8, /* FetchTexel3D */
398 };
399
400
401 /* ================================================================
402 * Null format:
403 */
404
405 const struct gl_texture_format _mesa_null_texformat = {
406 -1, /* IntFormat */
407 0, /* RedBits */
408 0, /* GreenBits */
409 0, /* BlueBits */
410 0, /* AlphaBits */
411 0, /* LuminanceBits */
412 0, /* IntensityBits */
413 0, /* IndexBits */
414 0, /* DepthBits */
415 0, /* TexelBytes */
416 fetch_null_texel, /* FetchTexel1D */
417 fetch_null_texel, /* FetchTexel2D */
418 fetch_null_texel, /* FetchTexel3D */
419 };
420
421
422
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
426 * storage format.
427 */
428 void _mesa_init_tex_format( GLcontext *ctx, GLenum internalFormat,
429 struct gl_texture_image *texImage )
430 {
431 texImage->IntFormat = internalFormat;
432
433 /* Ask the driver for the base format, if it doesn't know, it will
434 * return -1;
435 */
436 if ( ctx->Driver.BaseCompressedTexFormat ) {
437 GLint format = 0; /* Silence compiler warning */
438 format = (*ctx->Driver.BaseCompressedTexFormat)( ctx, format );
439 if ( format >= 0 ) {
440 internalFormat = format;
441 }
442 }
443
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
447 * huge list.
448 */
449 case 4: /* Quake3 uses this... */
450 case GL_RGBA:
451 texImage->Format = GL_RGBA;
452 texImage->Type = CHAN_TYPE;
453 texImage->TexFormat = &_mesa_texformat_rgba;
454 break;
455
456 case 3: /* ... and this. */
457 case GL_RGB:
458 texImage->Format = GL_RGB;
459 texImage->Type = CHAN_TYPE;
460 texImage->TexFormat = &_mesa_texformat_rgb;
461 break;
462
463 /* GH: Okay, keep checking as normal. Still test for GL_RGB,
464 * GL_RGBA formats first.
465 */
466 case GL_RGBA2:
467 case GL_RGBA4:
468 case GL_RGB5_A1:
469 case GL_RGBA8:
470 case GL_RGB10_A2:
471 case GL_RGBA12:
472 case GL_RGBA16:
473 texImage->Format = GL_RGBA;
474 texImage->Type = CHAN_TYPE;
475 texImage->TexFormat = &_mesa_texformat_rgba;
476 break;
477
478 case GL_R3_G3_B2:
479 case GL_RGB4:
480 case GL_RGB5:
481 case GL_RGB8:
482 case GL_RGB10:
483 case GL_RGB12:
484 case GL_RGB16:
485 texImage->Format = GL_RGB;
486 texImage->Type = CHAN_TYPE;
487 texImage->TexFormat = &_mesa_texformat_rgb;
488 break;
489
490 case GL_ALPHA:
491 case GL_ALPHA4:
492 case GL_ALPHA8:
493 case GL_ALPHA12:
494 case GL_ALPHA16:
495 texImage->Format = GL_ALPHA;
496 texImage->Type = CHAN_TYPE;
497 texImage->TexFormat = &_mesa_texformat_alpha;
498 break;
499
500 case 1:
501 case GL_LUMINANCE:
502 case GL_LUMINANCE4:
503 case GL_LUMINANCE8:
504 case GL_LUMINANCE12:
505 case GL_LUMINANCE16:
506 texImage->Format = GL_LUMINANCE;
507 texImage->Type = CHAN_TYPE;
508 texImage->TexFormat = &_mesa_texformat_luminance;
509 break;
510
511 case 2:
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;
522 break;
523
524 case GL_INTENSITY:
525 case GL_INTENSITY4:
526 case GL_INTENSITY8:
527 case GL_INTENSITY12:
528 case GL_INTENSITY16:
529 texImage->Format = GL_INTENSITY;
530 texImage->Type = CHAN_TYPE;
531 texImage->TexFormat = &_mesa_texformat_intensity;
532 break;
533
534 case GL_COLOR_INDEX:
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;
544 break;
545
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;
554 } else {
555 /* This error should have already been caught and dealt with.
556 */
557 _mesa_problem( ctx, "depth format without GL_SGIX_depth_texture" );
558 }
559 break;
560
561 default:
562 _mesa_problem( ctx, "unexpected format in _mesa_init_tex_format" );
563 return;
564 }
565 }