1 /* $Id: fxddtex.c,v 1.51 2003/10/14 14:56:45 dborca 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.
47 #include "texformat.h"
53 fxPrintTextureData(tfxTexInfo
* ti
)
55 fprintf(stderr
, "Texture Data:\n");
57 fprintf(stderr
, "\tName: %d\n", ti
->tObj
->Name
);
58 fprintf(stderr
, "\tBaseLevel: %d\n", ti
->tObj
->BaseLevel
);
59 fprintf(stderr
, "\tSize: %d x %d\n",
60 ti
->tObj
->Image
[ti
->tObj
->BaseLevel
]->Width
,
61 ti
->tObj
->Image
[ti
->tObj
->BaseLevel
]->Height
);
64 fprintf(stderr
, "\tName: UNNAMED\n");
65 fprintf(stderr
, "\tLast used: %d\n", ti
->lastTimeUsed
);
66 fprintf(stderr
, "\tTMU: %ld\n", ti
->whichTMU
);
67 fprintf(stderr
, "\t%s\n", (ti
->isInTM
) ? "In TMU" : "Not in TMU");
69 fprintf(stderr
, "\tMem0: %x-%x\n", (unsigned) ti
->tm
[0]->startAddr
,
70 (unsigned) ti
->tm
[0]->endAddr
);
72 fprintf(stderr
, "\tMem1: %x-%x\n", (unsigned) ti
->tm
[1]->startAddr
,
73 (unsigned) ti
->tm
[1]->endAddr
);
74 fprintf(stderr
, "\tMipmaps: %d-%d\n", ti
->minLevel
, ti
->maxLevel
);
75 fprintf(stderr
, "\tFilters: min %d min %d\n",
76 (int) ti
->minFilt
, (int) ti
->maxFilt
);
77 fprintf(stderr
, "\tClamps: s %d t %d\n", (int) ti
->sClamp
,
79 fprintf(stderr
, "\tScales: s %f t %f\n", ti
->sScale
, ti
->tScale
);
80 fprintf(stderr
, "\t%s\n",
81 (ti
->fixedPalette
) ? "Fixed palette" : "Non fixed palette");
82 fprintf(stderr
, "\t%s\n", (ti
->validated
) ? "Validated" : "Not validated");
86 /************************************************************************/
87 /*************************** Texture Mapping ****************************/
88 /************************************************************************/
91 fxTexInvalidate(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
93 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
96 ti
= fxTMGetTexInfo(tObj
);
98 fxTMMoveOutTM(fxMesa
, tObj
); /* TO DO: SLOW but easy to write */
100 ti
->validated
= GL_FALSE
;
101 fxMesa
->new_state
|= FX_NEW_TEXTURING
;
105 fxAllocTexObjData(fxMesaContext fxMesa
)
109 if (!(ti
= CALLOC(sizeof(tfxTexInfo
)))) {
110 fprintf(stderr
, "%s: ERROR: out of memory !\n", __FUNCTION__
);
115 ti
->validated
= GL_FALSE
;
116 ti
->isInTM
= GL_FALSE
;
118 ti
->whichTMU
= FX_TMU_NONE
;
120 ti
->tm
[FX_TMU0
] = NULL
;
121 ti
->tm
[FX_TMU1
] = NULL
;
123 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
124 ti
->maxFilt
= GR_TEXTUREFILTER_BILINEAR
;
126 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
127 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
129 ti
->mmMode
= GR_MIPMAP_NEAREST
;
130 ti
->LODblend
= FXFALSE
;
136 fxDDTexBind(GLcontext
* ctx
, GLenum target
, struct gl_texture_object
*tObj
)
138 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
141 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
142 fprintf(stderr
, "%s(%d, %x)\n", __FUNCTION__
, tObj
->Name
,
143 (GLuint
) tObj
->DriverData
);
146 if (target
!= GL_TEXTURE_2D
)
149 if (!tObj
->DriverData
) {
150 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
153 ti
= fxTMGetTexInfo(tObj
);
155 fxMesa
->texBindNumber
++;
156 ti
->lastTimeUsed
= fxMesa
->texBindNumber
;
158 fxMesa
->new_state
|= FX_NEW_TEXTURING
;
162 fxDDTexEnv(GLcontext
* ctx
, GLenum target
, GLenum pname
,
163 const GLfloat
* param
)
165 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
167 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
169 fprintf(stderr
, "%s(%x, %x)\n", __FUNCTION__
, pname
, (GLint
) (*param
));
171 fprintf(stderr
, "%s(%x)\n", __FUNCTION__
, pname
);
174 /* apply any lod biasing right now */
175 if (pname
== GL_TEXTURE_LOD_BIAS_EXT
) {
176 grTexLodBiasValue(GR_TMU0
, *param
);
178 if (fxMesa
->haveTwoTMUs
) {
179 grTexLodBiasValue(GR_TMU1
, *param
);
184 fxMesa
->new_state
|= FX_NEW_TEXTURING
;
188 fxDDTexParam(GLcontext
* ctx
, GLenum target
, struct gl_texture_object
*tObj
,
189 GLenum pname
, const GLfloat
* params
)
191 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
192 GLenum param
= (GLenum
) (GLint
) params
[0];
195 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
196 fprintf(stderr
, "fxDDTexParam(%d, %x, %s, %s)\n",
197 tObj
->Name
, (GLuint
) tObj
->DriverData
,
198 _mesa_lookup_enum_by_nr(pname
),
199 _mesa_lookup_enum_by_nr(param
));
202 if (target
!= GL_TEXTURE_2D
)
205 if (!tObj
->DriverData
)
206 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
208 ti
= fxTMGetTexInfo(tObj
);
212 case GL_TEXTURE_MIN_FILTER
:
215 ti
->mmMode
= GR_MIPMAP_DISABLE
;
216 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
217 ti
->LODblend
= FXFALSE
;
220 ti
->mmMode
= GR_MIPMAP_DISABLE
;
221 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
222 ti
->LODblend
= FXFALSE
;
224 case GL_NEAREST_MIPMAP_LINEAR
:
225 if (fxMesa
->haveTwoTMUs
) {
226 ti
->mmMode
= GR_MIPMAP_NEAREST
;
227 ti
->LODblend
= FXTRUE
;
230 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
231 ti
->LODblend
= FXFALSE
;
233 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
234 break; /* ZZZ: we may have to fall through here for V3 */
235 case GL_NEAREST_MIPMAP_NEAREST
:
236 ti
->mmMode
= GR_MIPMAP_NEAREST
;
237 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
238 ti
->LODblend
= FXFALSE
;
240 case GL_LINEAR_MIPMAP_LINEAR
:
241 /* ZZZ: HACK ALERT! trilinear is bugged! mipmap blending produce
242 incorrect filtered colors for the smallest mipmap levels. */
244 if (fxMesa
->haveTwoTMUs
) {
245 ti
->mmMode
= GR_MIPMAP_NEAREST
;
246 ti
->LODblend
= FXTRUE
;
249 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
250 ti
->LODblend
= FXFALSE
;
252 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
253 break; /* ZZZ: we may have to fall through here for V3 */
255 case GL_LINEAR_MIPMAP_NEAREST
:
256 ti
->mmMode
= GR_MIPMAP_NEAREST
;
257 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
258 ti
->LODblend
= FXFALSE
;
263 fxTexInvalidate(ctx
, tObj
);
266 case GL_TEXTURE_MAG_FILTER
:
269 ti
->maxFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
272 ti
->maxFilt
= GR_TEXTUREFILTER_BILINEAR
;
277 fxTexInvalidate(ctx
, tObj
);
280 case GL_TEXTURE_WRAP_S
:
282 case GL_MIRRORED_REPEAT
:
283 ti
->sClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
285 case GL_CLAMP_TO_EDGE
: /* CLAMP discarding border */
287 ti
->sClamp
= GR_TEXTURECLAMP_CLAMP
;
290 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
295 fxMesa
->new_state
|= FX_NEW_TEXTURING
;
298 case GL_TEXTURE_WRAP_T
:
300 case GL_MIRRORED_REPEAT
:
301 ti
->tClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
303 case GL_CLAMP_TO_EDGE
: /* CLAMP discarding border */
305 ti
->tClamp
= GR_TEXTURECLAMP_CLAMP
;
308 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
313 fxMesa
->new_state
|= FX_NEW_TEXTURING
;
316 case GL_TEXTURE_BORDER_COLOR
:
320 case GL_TEXTURE_MIN_LOD
:
323 case GL_TEXTURE_MAX_LOD
:
326 case GL_TEXTURE_BASE_LEVEL
:
327 fxTexInvalidate(ctx
, tObj
);
329 case GL_TEXTURE_MAX_LEVEL
:
330 fxTexInvalidate(ctx
, tObj
);
339 fxDDTexDel(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
341 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
342 tfxTexInfo
*ti
= fxTMGetTexInfo(tObj
);
344 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
345 fprintf(stderr
, "%s(%d, %p)\n", __FUNCTION__
, tObj
->Name
, (void *) ti
);
351 fxTMFreeTexture(fxMesa
, tObj
);
354 tObj
->DriverData
= NULL
;
358 * Return true if texture is resident, false otherwise.
361 fxDDIsTextureResident(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
363 tfxTexInfo
*ti
= fxTMGetTexInfo(tObj
);
364 return (ti
&& ti
->isInTM
);
370 * Convert a gl_color_table texture palette to Glide's format.
373 convertPalette(FxU32 data
[256], const struct gl_color_table
*table
)
375 const GLubyte
*tableUB
= (const GLubyte
*) table
->Table
;
376 GLint width
= table
->Size
;
380 ASSERT(!table
->FloatTable
);
382 switch (table
->Format
) {
384 for (i
= 0; i
< width
; i
++) {
389 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
393 for (i
= 0; i
< width
; i
++) {
398 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
402 for (i
= 0; i
< width
; i
++) {
405 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
408 case GL_LUMINANCE_ALPHA
:
409 for (i
= 0; i
< width
; i
++) {
410 r
= g
= b
= tableUB
[i
* 2 + 0];
411 a
= tableUB
[i
* 2 + 1];
412 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
416 for (i
= 0; i
< width
; i
++) {
417 r
= tableUB
[i
* 3 + 0];
418 g
= tableUB
[i
* 3 + 1];
419 b
= tableUB
[i
* 3 + 2];
421 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
425 for (i
= 0; i
< width
; i
++) {
426 r
= tableUB
[i
* 4 + 0];
427 g
= tableUB
[i
* 4 + 1];
428 b
= tableUB
[i
* 4 + 2];
429 a
= tableUB
[i
* 4 + 3];
430 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
438 fxDDTexPalette(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
440 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
443 /* per-texture palette */
445 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
446 fprintf(stderr
, "%s(%d, %x)\n", __FUNCTION__
,
447 tObj
->Name
, (GLuint
) tObj
->DriverData
);
449 if (!tObj
->DriverData
)
450 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
451 ti
= fxTMGetTexInfo(tObj
);
452 convertPalette(ti
->palette
.data
, &tObj
->Palette
);
453 fxTexInvalidate(ctx
, tObj
);
456 /* global texture palette */
457 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
458 fprintf(stderr
, "%s(global)\n", __FUNCTION__
);
460 convertPalette(fxMesa
->glbPalette
.data
, &ctx
->Texture
.Palette
);
461 fxMesa
->new_state
|= FX_NEW_TEXTURING
;
467 fxDDTexUseGlbPalette(GLcontext
* ctx
, GLboolean state
)
469 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
471 if (TDFX_DEBUG
& VERBOSE_DRIVER
) {
472 fprintf(stderr
, "%s(%d)\n", __FUNCTION__
, state
);
476 fxMesa
->haveGlobalPaletteTexture
= 1;
478 grTexDownloadTable(GR_TEXTABLE_PALETTE
, &(fxMesa
->glbPalette
));
481 fxMesa
->haveGlobalPaletteTexture
= 0;
483 if ((ctx
->Texture
.Unit
[0]._Current
== ctx
->Texture
.Unit
[0].Current2D
) &&
484 (ctx
->Texture
.Unit
[0]._Current
!= NULL
)) {
485 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[0]._Current
;
487 if (!tObj
->DriverData
)
488 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
490 fxTexInvalidate(ctx
, tObj
);
520 fxTexGetInfo(int w
, int h
, GrLOD_t
* lodlevel
, GrAspectRatio_t
* ar
,
521 float *sscale
, float *tscale
,
522 int *wscale
, int *hscale
)
524 static GrLOD_t lod
[12] = {
539 int logw
, logh
, ws
, hs
;
541 GrAspectRatio_t aspectratio
;
547 switch (logw
- logh
) {
549 aspectratio
= GR_ASPECT_LOG2_1x1
;
555 aspectratio
= GR_ASPECT_LOG2_2x1
;
563 aspectratio
= GR_ASPECT_LOG2_4x1
;
571 aspectratio
= GR_ASPECT_LOG2_8x1
;
579 aspectratio
= GR_ASPECT_LOG2_1x2
;
587 aspectratio
= GR_ASPECT_LOG2_1x4
;
595 aspectratio
= GR_ASPECT_LOG2_1x8
;
603 if ((logw
- logh
) > 3) {
604 aspectratio
= GR_ASPECT_LOG2_8x1
;
609 hs
= 1 << (logw
- logh
- 3);
610 } else /*if ((logw - logh) < -3)*/ {
611 aspectratio
= GR_ASPECT_LOG2_1x8
;
615 ws
= 1 << (logh
- logw
- 3);
643 fxIsTexSupported(GLenum target
, GLint internalFormat
,
644 const struct gl_texture_image
*image
)
646 if (target
!= GL_TEXTURE_2D
)
649 if (!fxTexGetInfo(image
->Width
, image
->Height
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
))
652 if (image
->Border
> 0)
659 /**********************************************************************/
660 /**** NEW TEXTURE IMAGE FUNCTIONS ****/
661 /**********************************************************************/
663 /* Texel-fetch functions for software texturing and glGetTexImage().
664 * We should have been able to use some "standard" fetch functions (which
665 * may get defined in texutil.c) but we have to account for scaled texture
666 * images on tdfx hardware (the 8:1 aspect ratio limit).
667 * Hence, we need special functions here.
671 fetch_intensity8(const struct gl_texture_image
*texImage
,
672 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
674 GLchan
*rgba
= (GLchan
*) texelOut
;
675 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
676 const GLubyte
*texel
;
681 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
682 rgba
[RCOMP
] = *texel
;
683 rgba
[GCOMP
] = *texel
;
684 rgba
[BCOMP
] = *texel
;
685 rgba
[ACOMP
] = *texel
;
690 fetch_luminance8(const struct gl_texture_image
*texImage
,
691 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
693 GLchan
*rgba
= (GLchan
*) texelOut
;
694 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
695 const GLubyte
*texel
;
700 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
701 rgba
[RCOMP
] = *texel
;
702 rgba
[GCOMP
] = *texel
;
703 rgba
[BCOMP
] = *texel
;
709 fetch_alpha8(const struct gl_texture_image
*texImage
,
710 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
712 GLchan
*rgba
= (GLchan
*) texelOut
;
713 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
714 const GLubyte
*texel
;
718 i
= i
* mml
->width
/ texImage
->Width
;
719 j
= j
* mml
->height
/ texImage
->Height
;
721 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
725 rgba
[ACOMP
] = *texel
;
730 fetch_index8(const struct gl_texture_image
*texImage
,
731 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
733 GLchan
*indexOut
= (GLchan
*) texelOut
;
734 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
735 const GLubyte
*texel
;
739 i
= i
* mml
->width
/ texImage
->Width
;
740 j
= j
* mml
->height
/ texImage
->Height
;
742 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
748 fetch_luminance8_alpha8(const struct gl_texture_image
*texImage
,
749 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
751 GLchan
*rgba
= (GLchan
*) texelOut
;
752 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
753 const GLubyte
*texel
;
758 texel
= ((GLubyte
*) texImage
->Data
) + (j
* mml
->width
+ i
) * 2;
759 rgba
[RCOMP
] = texel
[0];
760 rgba
[GCOMP
] = texel
[0];
761 rgba
[BCOMP
] = texel
[0];
762 rgba
[ACOMP
] = texel
[1];
767 fetch_r5g6b5(const struct gl_texture_image
*texImage
,
768 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
770 GLchan
*rgba
= (GLchan
*) texelOut
;
771 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
772 const GLushort
*texel
;
777 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
778 rgba
[RCOMP
] = FX_rgb_scale_5
[(*texel
>> 11) & 0x1F];
779 rgba
[GCOMP
] = FX_rgb_scale_6
[(*texel
>> 5) & 0x3F];
780 rgba
[BCOMP
] = FX_rgb_scale_5
[ *texel
& 0x1F];
786 fetch_r4g4b4a4(const struct gl_texture_image
*texImage
,
787 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
789 GLchan
*rgba
= (GLchan
*) texelOut
;
790 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
791 const GLushort
*texel
;
796 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
797 rgba
[RCOMP
] = FX_rgb_scale_4
[(*texel
>> 12) & 0xF];
798 rgba
[GCOMP
] = FX_rgb_scale_4
[(*texel
>> 8) & 0xF];
799 rgba
[BCOMP
] = FX_rgb_scale_4
[(*texel
>> 4) & 0xF];
800 rgba
[ACOMP
] = FX_rgb_scale_4
[ *texel
& 0xF];
805 fetch_r5g5b5a1(const struct gl_texture_image
*texImage
,
806 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
808 GLchan
*rgba
= (GLchan
*) texelOut
;
809 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
810 const GLushort
*texel
;
815 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
816 rgba
[RCOMP
] = FX_rgb_scale_5
[(*texel
>> 11) & 0x1F];
817 rgba
[GCOMP
] = FX_rgb_scale_5
[(*texel
>> 6) & 0x1F];
818 rgba
[BCOMP
] = FX_rgb_scale_5
[(*texel
>> 1) & 0x1F];
819 rgba
[ACOMP
] = ((*texel
) & 0x01) * 255;
824 fetch_a8r8g8b8(const struct gl_texture_image
*texImage
,
825 GLint i
, GLint j
, GLint k
, GLvoid
* texelOut
)
827 GLchan
*rgba
= (GLchan
*) texelOut
;
828 const tfxMipMapLevel
*mml
= FX_MIPMAP_DATA(texImage
);
834 texel
= ((GLuint
*) texImage
->Data
) + j
* mml
->width
+ i
;
835 rgba
[RCOMP
] = (((*texel
) >> 16) & 0xff);
836 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xff);
837 rgba
[BCOMP
] = (((*texel
) ) & 0xff);
838 rgba
[ACOMP
] = (((*texel
) >> 24) & 0xff);
843 PrintTexture(int w
, int h
, int c
, const GLubyte
* data
)
846 for (i
= 0; i
< h
; i
++) {
847 for (j
= 0; j
< w
; j
++) {
849 fprintf(stderr
, "%02x %02x ", data
[0], data
[1]);
851 fprintf(stderr
, "%02x %02x %02x ", data
[0], data
[1], data
[2]);
854 fprintf(stderr
, "\n");
859 const struct gl_texture_format
*
860 fxDDChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
861 GLenum srcFormat
, GLenum srcType
)
863 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
864 GLboolean allow32bpt
= fxMesa
->HaveTexFmt
;
866 if (TDFX_DEBUG
& VERBOSE_TEXTURE
) {
867 fprintf(stderr
, "fxDDChooseTextureFormat(...)\n");
870 switch (internalFormat
) {
876 case GL_COMPRESSED_INTENSITY
:
877 return &_mesa_texformat_i8
;
884 case GL_COMPRESSED_LUMINANCE
:
885 return &_mesa_texformat_l8
;
891 case GL_COMPRESSED_ALPHA
:
892 return &_mesa_texformat_a8
;
894 case GL_COLOR_INDEX1_EXT
:
895 case GL_COLOR_INDEX2_EXT
:
896 case GL_COLOR_INDEX4_EXT
:
897 case GL_COLOR_INDEX8_EXT
:
898 case GL_COLOR_INDEX12_EXT
:
899 case GL_COLOR_INDEX16_EXT
:
900 return &_mesa_texformat_ci8
;
902 case GL_LUMINANCE_ALPHA
:
903 case GL_LUMINANCE4_ALPHA4
:
904 case GL_LUMINANCE6_ALPHA2
:
905 case GL_LUMINANCE8_ALPHA8
:
906 case GL_LUMINANCE12_ALPHA4
:
907 case GL_LUMINANCE12_ALPHA12
:
908 case GL_LUMINANCE16_ALPHA16
:
909 case GL_COMPRESSED_LUMINANCE_ALPHA
:
910 return &_mesa_texformat_al88
;
914 return &_mesa_texformat_rgb565
;
917 case GL_COMPRESSED_RGB
:
918 if ( srcFormat
== GL_RGB
&& srcType
== GL_UNSIGNED_SHORT_5_6_5
) {
919 return &_mesa_texformat_rgb565
;
921 /* intentional fall through */
926 return (allow32bpt
) ? &_mesa_texformat_argb8888
927 : &_mesa_texformat_rgb565
;
930 return &_mesa_texformat_argb4444
;
933 case GL_COMPRESSED_RGBA
:
934 if ( srcFormat
== GL_BGRA
) {
935 if ( srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
936 return &_mesa_texformat_argb8888
;
938 else if ( srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
939 return &_mesa_texformat_argb4444
;
941 else if ( srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
942 return &_mesa_texformat_argb1555
;
945 /* intentional fall through */
950 return (allow32bpt
) ? &_mesa_texformat_argb8888
951 : &_mesa_texformat_argb4444
;
953 return &_mesa_texformat_argb1555
;
955 /* GL_EXT_texture_compression_s3tc */
956 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
957 return &_mesa_texformat_rgb_dxt1
;
958 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
959 return &_mesa_texformat_rgba_dxt1
;
960 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
961 return &_mesa_texformat_rgba_dxt3
;
962 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
963 return &_mesa_texformat_rgba_dxt5
;
964 /*case GL_COMPRESSED_RGB_FXT1_3DFX:
965 case GL_COMPRESSED_RGBA_FXT1_3DFX:
969 _mesa_problem(NULL
, "unexpected format in fxDDChooseTextureFormat");
975 static GrTextureFormat_t
976 fxGlideFormat(GLint mesaFormat
)
978 switch (mesaFormat
) {
980 return GR_TEXFMT_ALPHA_8
;
982 return GR_TEXFMT_ALPHA_8
;
984 return GR_TEXFMT_INTENSITY_8
;
985 case MESA_FORMAT_CI8
:
986 return GR_TEXFMT_P_8
;
987 case MESA_FORMAT_AL88
:
988 return GR_TEXFMT_ALPHA_INTENSITY_88
;
989 case MESA_FORMAT_RGB565
:
990 return GR_TEXFMT_RGB_565
;
991 case MESA_FORMAT_ARGB4444
:
992 return GR_TEXFMT_ARGB_4444
;
993 case MESA_FORMAT_ARGB1555
:
994 return GR_TEXFMT_ARGB_1555
;
995 case MESA_FORMAT_ARGB8888
:
996 return GR_TEXFMT_ARGB_8888
;
998 case MESA_FORMAT_RGB_DXT1
:
999 case MESA_FORMAT_RGBA_DXT1
:
1000 return GR_TEXFMT_ARGB_CMP_DXT1
;
1001 case MESA_FORMAT_RGBA_DXT3
:
1002 return GR_TEXFMT_ARGB_CMP_DXT3
;
1003 case MESA_FORMAT_RGBA_DXT5
:
1004 return GR_TEXFMT_ARGB_CMP_DXT5
;
1005 /*case MESA_FORMAT_ARGB_CMP_FXT1:
1006 return GR_TEXFMT_ARGB_CMP_FXT1;
1007 case MESA_FORMAT_RGB_CMP_FXT1:
1008 return GL_COMPRESSED_RGBA_FXT1_3DFX;*/
1011 _mesa_problem(NULL
, "Unexpected format in fxGlideFormat");
1017 static FetchTexelFunc
1018 fxFetchFunction(GLint mesaFormat
)
1020 switch (mesaFormat
) {
1021 case MESA_FORMAT_I8
:
1022 return fetch_intensity8
;
1023 case MESA_FORMAT_A8
:
1024 return fetch_alpha8
;
1025 case MESA_FORMAT_L8
:
1026 return fetch_luminance8
;
1027 case MESA_FORMAT_CI8
:
1028 return fetch_index8
;
1029 case MESA_FORMAT_AL88
:
1030 return fetch_luminance8_alpha8
;
1031 case MESA_FORMAT_RGB565
:
1032 return fetch_r5g6b5
;
1033 case MESA_FORMAT_ARGB4444
:
1034 return fetch_r4g4b4a4
;
1035 case MESA_FORMAT_ARGB1555
:
1036 return fetch_r5g5b5a1
;
1037 case MESA_FORMAT_ARGB8888
:
1038 return fetch_a8r8g8b8
;
1040 case MESA_FORMAT_RGB_DXT1
:
1041 case MESA_FORMAT_RGBA_DXT1
:
1042 case MESA_FORMAT_RGBA_DXT3
:
1043 case MESA_FORMAT_RGBA_DXT5
:
1044 return fetch_r4g4b4a4
;
1047 _mesa_problem(NULL
, "Unexpected format in fxFetchFunction");
1053 fxDDTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1054 GLint internalFormat
, GLint width
, GLint height
, GLint border
,
1055 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1056 const struct gl_pixelstore_attrib
*packing
,
1057 struct gl_texture_object
*texObj
,
1058 struct gl_texture_image
*texImage
)
1060 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
1062 tfxMipMapLevel
*mml
;
1065 if (TDFX_DEBUG
& VERBOSE_TEXTURE
) {
1066 fprintf(stderr
, "fxDDTexImage2D: id=%d int 0x%x format 0x%x type 0x%x %dx%d\n",
1067 texObj
->Name
, texImage
->IntFormat
, format
, type
,
1068 texImage
->Width
, texImage
->Height
);
1071 if (!fxIsTexSupported(target
, internalFormat
, texImage
)) {
1072 _mesa_problem(NULL
, "fx Driver: unsupported texture in fxDDTexImg()\n");
1076 if (!texObj
->DriverData
) {
1077 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1078 if (!texObj
->DriverData
) {
1079 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1083 ti
= fxTMGetTexInfo(texObj
);
1085 if (!texImage
->DriverData
) {
1086 texImage
->DriverData
= CALLOC(sizeof(tfxMipMapLevel
));
1087 if (!texImage
->DriverData
) {
1088 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1092 mml
= FX_MIPMAP_DATA(texImage
);
1094 fxTexGetInfo(width
, height
, NULL
, NULL
, NULL
, NULL
,
1095 &mml
->wScale
, &mml
->hScale
);
1097 mml
->width
= width
* mml
->wScale
;
1098 mml
->height
= height
* mml
->hScale
;
1101 /* choose the texture format */
1102 assert(ctx
->Driver
.ChooseTextureFormat
);
1103 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1104 internalFormat
, format
, type
);
1105 assert(texImage
->TexFormat
);
1106 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1107 /*if (!fxMesa->HaveTexFmt) assert(texelBytes == 1 || texelBytes == 2);*/
1109 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1110 /* rescale image to overcome 1:8 aspect limitation */
1112 tempImage
= MALLOC(width
* height
* texelBytes
);
1114 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1117 /* unpack image, apply transfer ops and store in tempImage */
1118 _mesa_transfer_teximage(ctx
, 2, texImage
->Format
,
1119 texImage
->TexFormat
,
1121 width
, height
, 1, 0, 0, 0,
1123 0, /* dstImageStride */
1124 format
, type
, pixels
, packing
);
1125 assert(!texImage
->Data
);
1126 texImage
->Data
= MESA_PBUFFER_ALLOC(mml
->width
* mml
->height
* texelBytes
);
1127 if (!texImage
->Data
) {
1128 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1132 _mesa_rescale_teximage2d(texelBytes
,
1133 mml
->width
* texelBytes
, /* dst stride */
1135 mml
->width
, mml
->height
,
1136 tempImage
/*src*/, texImage
->Data
/*dst*/ );
1140 /* no rescaling needed */
1141 assert(!texImage
->Data
);
1142 texImage
->Data
= MESA_PBUFFER_ALLOC(mml
->width
* mml
->height
* texelBytes
);
1143 if (!texImage
->Data
) {
1144 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1147 /* unpack image, apply transfer ops and store in texImage->Data */
1148 _mesa_transfer_teximage(ctx
, 2, texImage
->Format
,
1149 texImage
->TexFormat
, texImage
->Data
,
1150 width
, height
, 1, 0, 0, 0,
1151 texImage
->Width
* texelBytes
,
1152 0, /* dstImageStride */
1153 format
, type
, pixels
, packing
);
1156 mml
->glideFormat
= fxGlideFormat(texImage
->TexFormat
->MesaFormat
);
1157 ti
->info
.format
= mml
->glideFormat
;
1158 texImage
->FetchTexel
= fxFetchFunction(texImage
->TexFormat
->MesaFormat
);
1161 * Hack alert: unsure...
1163 if (!(fxMesa
->new_state
& FX_NEW_TEXTURING
) && ti
->validated
&& ti
->isInTM
) {
1164 /*fprintf(stderr, "reloadmipmaplevels\n"); */
1165 fxTMReloadMipMapLevel(fxMesa
, texObj
, level
);
1168 /*fprintf(stderr, "invalidate2\n"); */
1169 fxTexInvalidate(ctx
, texObj
);
1175 fxDDTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1176 GLint xoffset
, GLint yoffset
,
1177 GLsizei width
, GLsizei height
,
1178 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1179 const struct gl_pixelstore_attrib
*packing
,
1180 struct gl_texture_object
*texObj
,
1181 struct gl_texture_image
*texImage
)
1183 fxMesaContext fxMesa
= FX_CONTEXT(ctx
);
1185 tfxMipMapLevel
*mml
;
1188 if (TDFX_DEBUG
& VERBOSE_TEXTURE
) {
1189 fprintf(stderr
, "fxDDTexSubImage2D: id=%d\n", texObj
->Name
);
1192 if (!texObj
->DriverData
) {
1193 _mesa_problem(ctx
, "problem in fxDDTexSubImage2D");
1197 ti
= fxTMGetTexInfo(texObj
);
1199 mml
= FX_MIPMAP_DATA(texImage
);
1202 assert(texImage
->Data
); /* must have an existing texture image! */
1203 assert(texImage
->Format
);
1205 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1207 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1208 /* need to rescale subimage to match mipmap level's rescale factors */
1209 const GLint newWidth
= width
* mml
->wScale
;
1210 const GLint newHeight
= height
* mml
->hScale
;
1213 tempImage
= MALLOC(width
* height
* texelBytes
);
1215 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
1219 _mesa_transfer_teximage(ctx
, 2, texImage
->Format
,/* Tex int format */
1220 texImage
->TexFormat
, /* dest format */
1221 (GLubyte
*) tempImage
, /* dest */
1222 width
, height
, 1, /* subimage size */
1223 0, 0, 0, /* subimage pos */
1224 width
* texelBytes
, /* dest row stride */
1225 0, /* dst image stride */
1226 format
, type
, pixels
, packing
);
1229 /* compute address of dest subimage within the overal tex image */
1230 destAddr
= (GLubyte
*) texImage
->Data
1231 + (yoffset
* mml
->hScale
* mml
->width
1232 + xoffset
* mml
->wScale
) * texelBytes
;
1234 _mesa_rescale_teximage2d(texelBytes
,
1235 mml
->width
* texelBytes
, /* dst stride */
1237 newWidth
, newHeight
,
1238 tempImage
, destAddr
);
1243 /* no rescaling needed */
1244 _mesa_transfer_teximage(ctx
, 2, texImage
->Format
, /* Tex int format */
1245 texImage
->TexFormat
, /* dest format */
1246 (GLubyte
*) texImage
->Data
,/* dest */
1247 width
, height
, 1, /* subimage size */
1248 xoffset
, yoffset
, 0, /* subimage pos */
1249 mml
->width
* texelBytes
, /* dest row stride */
1250 0, /* dst image stride */
1251 format
, type
, pixels
, packing
);
1255 * Hack alert: unsure...
1257 if (!(fxMesa
->new_state
& FX_NEW_TEXTURING
) && ti
->validated
&& ti
->isInTM
)
1258 fxTMReloadMipMapLevel(fxMesa
, texObj
, level
);
1260 fxTexInvalidate(ctx
, texObj
);
1267 * Need this to provide at least one external definition.
1270 extern int gl_fx_dummy_function_ddtex(void);
1272 gl_fx_dummy_function_ddtex(void)