1 /* -*- mode: c; c-basic-offset: 3 -*-
3 * Copyright 2000 VA Linux Systems Inc., Fremont, California.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_tex.c,v 1.7 2002/11/05 17:46:10 tsi Exp $ */
30 * Daniel Borca <dborca@users.sourceforge.net>, 19 Jul 2004
33 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
36 * Gareth Hughes <gareth@valinux.com>
37 * Brian Paul <brianp@valinux.com>
42 #include "texcompress.h"
43 #include "texformat.h"
47 #include "tdfx_context.h"
49 #include "tdfx_texman.h"
52 /* no borders! can't halve 1x1! (stride > width * comp) not allowed */
54 _mesa_halve2x2_teximage2d ( GLcontext
*ctx
,
55 struct gl_texture_image
*texImage
,
57 GLint srcWidth
, GLint srcHeight
,
58 const GLvoid
*srcImage
, GLvoid
*dstImage
)
61 GLint dstWidth
= srcWidth
/ 2;
62 GLint dstHeight
= srcHeight
/ 2;
63 GLint srcRowStride
= srcWidth
* bytesPerPixel
;
64 GLubyte
*src
= (GLubyte
*)srcImage
;
65 GLubyte
*dst
= dstImage
;
72 if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB565
) {
73 _t
= GL_UNSIGNED_SHORT_5_6_5_REV
;
75 } else if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_ARGB4444
) {
76 _t
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
78 } else if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_ARGB1555
) {
79 _t
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
84 srcRowStride
= srcWidth
* bytesPerPixel
;
91 _s
= src
= MALLOC(srcRowStride
* srcHeight
);
92 _d
= dst
= MALLOC(dstWidth
* bytesPerPixel
* dstHeight
);
93 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
94 &_mesa_texformat_rgba8888_rev
, src
,
95 0, 0, 0, /* dstX/Y/Zoffset */
96 srcRowStride
, /* dstRowStride */
97 0, /* dstImageStride */
98 srcWidth
, srcHeight
, 1,
99 texImage
->Format
, _t
, srcImage
, &ctx
->DefaultPacking
);
102 if (srcHeight
== 1) {
103 for (i
= 0; i
< dstWidth
; i
++) {
104 for (k
= 0; k
< bytesPerPixel
; k
++) {
105 dst
[0] = (src
[0] + src
[bytesPerPixel
] + 1) / 2;
109 src
+= bytesPerPixel
;
111 } else if (srcWidth
== 1) {
112 for (j
= 0; j
< dstHeight
; j
++) {
113 for (k
= 0; k
< bytesPerPixel
; k
++) {
114 dst
[0] = (src
[0] + src
[srcRowStride
] + 1) / 2;
121 for (j
= 0; j
< dstHeight
; j
++) {
122 for (i
= 0; i
< dstWidth
; i
++) {
123 for (k
= 0; k
< bytesPerPixel
; k
++) {
127 src
[srcRowStride
+ bytesPerPixel
] + 2) / 4;
131 src
+= bytesPerPixel
;
140 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
141 texImage
->TexFormat
, dstImage
,
142 0, 0, 0, /* dstX/Y/Zoffset */
144 0, /* dstImageStride */
145 dstWidth
, dstHeight
, 1,
146 GL_BGRA
, CHAN_TYPE
, dst
, &ctx
->DefaultPacking
);
177 * Compute various texture image parameters.
178 * Input: w, h - source texture width and height
179 * Output: lodlevel - Glide lod level token for the larger texture dimension
180 * aspectratio - Glide aspect ratio token
181 * sscale - S scale factor used during triangle setup
182 * tscale - T scale factor used during triangle setup
183 * wscale - OpenGL -> Glide image width scale factor
184 * hscale - OpenGL -> Glide image height scale factor
187 * w h lodlevel aspectRatio
188 * 128 128 GR_LOD_LOG2_128 (=7) GR_ASPECT_LOG2_1x1 (=0)
189 * 64 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x1 (=0)
190 * 64 32 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_2x1 (=1)
191 * 32 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x2 (=-1)
192 * 32 32 GR_LOD_LOG2_32 (=5) GR_ASPECT_LOG2_1x1 (=0)
195 tdfxTexGetInfo(const GLcontext
*ctx
, int w
, int h
,
196 GrLOD_t
*lodlevel
, GrAspectRatio_t
*aspectratio
,
197 float *sscale
, float *tscale
,
198 int *wscale
, int *hscale
)
200 int logw
, logh
, ar
, lod
, ws
, hs
;
208 ar
= logw
- logh
; /* aspect ratio = difference in log dimensions */
212 /* Hardware only allows a maximum aspect ratio of 8x1, so handle
213 |ar| > 3 by scaling the image and using an 8x1 aspect ratio */
217 if (ar
<= GR_ASPECT_LOG2_8x1
) {
221 /* have to stretch image height */
224 ar
= GR_ASPECT_LOG2_8x1
;
230 if (ar
>= GR_ASPECT_LOG2_1x8
) {
234 /* have to stretch image width */
237 ar
= GR_ASPECT_LOG2_1x8
;
242 *lodlevel
= (GrLOD_t
) lod
;
244 *aspectratio
= (GrAspectRatio_t
) ar
;
257 * We need to call this when a texture object's minification filter
258 * or texture image sizes change.
260 static void RevalidateTexture(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
262 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
268 minl
= maxl
= tObj
->BaseLevel
;
270 if (tObj
->Image
[0][minl
]) {
271 maxl
= MIN2(tObj
->MaxLevel
, tObj
->Image
[0][minl
]->MaxLog2
);
273 /* compute largeLodLog2, aspect ratio and texcoord scale factors */
274 tdfxTexGetInfo(ctx
, tObj
->Image
[0][minl
]->Width
, tObj
->Image
[0][minl
]->Height
,
275 &ti
->info
.largeLodLog2
,
276 &ti
->info
.aspectRatioLog2
,
277 &(ti
->sScale
), &(ti
->tScale
), NULL
, NULL
);
280 if (tObj
->Image
[0][maxl
] && (tObj
->MinFilter
!= GL_NEAREST
) && (tObj
->MinFilter
!= GL_LINEAR
)) {
281 /* mipmapping: need to compute smallLodLog2 */
282 tdfxTexGetInfo(ctx
, tObj
->Image
[0][maxl
]->Width
,
283 tObj
->Image
[0][maxl
]->Height
,
284 &ti
->info
.smallLodLog2
, NULL
,
285 NULL
, NULL
, NULL
, NULL
);
288 /* not mipmapping: smallLodLog2 = largeLodLog2 */
289 ti
->info
.smallLodLog2
= ti
->info
.largeLodLog2
;
295 ti
->info
.data
= NULL
;
297 /* this is necessary because of fxDDCompressedTexImage2D */
299 struct gl_texture_image
*texImage
= tObj
->Image
[0][minl
];
300 tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
301 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
302 ti
->sScale
/= mml
->wScale
;
303 ti
->tScale
/= mml
->hScale
;
310 fxAllocTexObjData(tdfxContextPtr fxMesa
)
314 if (!(ti
= CALLOC(sizeof(tdfxTexInfo
)))) {
315 _mesa_problem(NULL
, "tdfx driver: out of memory");
319 ti
->isInTM
= GL_FALSE
;
321 ti
->whichTMU
= TDFX_TMU_NONE
;
323 ti
->tm
[TDFX_TMU0
] = NULL
;
324 ti
->tm
[TDFX_TMU1
] = NULL
;
326 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
327 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
329 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
330 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
332 ti
->mmMode
= GR_MIPMAP_NEAREST
;
333 ti
->LODblend
= FXFALSE
;
340 * Called via glBindTexture.
343 tdfxBindTexture(GLcontext
* ctx
, GLenum target
,
344 struct gl_texture_object
*tObj
)
346 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
349 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
350 fprintf(stderr
, "fxmesa: fxDDTexBind(%d,%p)\n", tObj
->Name
,
354 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
357 if (!tObj
->DriverData
) {
358 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
361 ti
= TDFX_TEXTURE_DATA(tObj
);
362 ti
->lastTimeUsed
= fxMesa
->texBindNumber
++;
364 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
369 * Called via glTexEnv.
372 tdfxTexEnv(GLcontext
* ctx
, GLenum target
, GLenum pname
,
373 const GLfloat
* param
)
375 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
377 if ( TDFX_DEBUG
& DEBUG_VERBOSE_API
) {
379 fprintf(stderr
, "fxmesa: texenv(%x,%x)\n", pname
,
382 fprintf(stderr
, "fxmesa: texenv(%x)\n", pname
);
385 /* XXX this is a bit of a hack to force the Glide texture
386 * state to be updated.
388 fxMesa
->TexState
.EnvMode
[ctx
->Texture
.CurrentUnit
] = 0;
390 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
395 * Called via glTexParameter.
398 tdfxTexParameter(GLcontext
* ctx
, GLenum target
,
399 struct gl_texture_object
*tObj
,
400 GLenum pname
, const GLfloat
* params
)
402 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
403 GLenum param
= (GLenum
) (GLint
) params
[0];
406 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
407 fprintf(stderr
, "fxmesa: fxDDTexParam(%d,%p,%x,%x)\n", tObj
->Name
,
408 tObj
->DriverData
, pname
, param
);
411 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
414 if (!tObj
->DriverData
)
415 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
417 ti
= TDFX_TEXTURE_DATA(tObj
);
420 case GL_TEXTURE_MIN_FILTER
:
423 ti
->mmMode
= GR_MIPMAP_DISABLE
;
424 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
425 ti
->LODblend
= FXFALSE
;
428 ti
->mmMode
= GR_MIPMAP_DISABLE
;
429 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
430 ti
->LODblend
= FXFALSE
;
432 case GL_NEAREST_MIPMAP_LINEAR
:
433 if (!fxMesa
->Glide
.HaveCombineExt
) {
434 if (fxMesa
->haveTwoTMUs
) {
435 ti
->mmMode
= GR_MIPMAP_NEAREST
;
436 ti
->LODblend
= FXTRUE
;
439 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
440 ti
->LODblend
= FXFALSE
;
442 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
445 /* XXX Voodoo3/Banshee mipmap blending seems to produce
446 * incorrectly filtered colors for the smallest mipmap levels.
447 * To work-around we fall-through here and use a different filter.
449 case GL_NEAREST_MIPMAP_NEAREST
:
450 ti
->mmMode
= GR_MIPMAP_NEAREST
;
451 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
452 ti
->LODblend
= FXFALSE
;
454 case GL_LINEAR_MIPMAP_LINEAR
:
455 if (!fxMesa
->Glide
.HaveCombineExt
) {
456 if (fxMesa
->haveTwoTMUs
) {
457 ti
->mmMode
= GR_MIPMAP_NEAREST
;
458 ti
->LODblend
= FXTRUE
;
461 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
462 ti
->LODblend
= FXFALSE
;
464 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
467 /* XXX Voodoo3/Banshee mipmap blending seems to produce
468 * incorrectly filtered colors for the smallest mipmap levels.
469 * To work-around we fall-through here and use a different filter.
471 case GL_LINEAR_MIPMAP_NEAREST
:
472 ti
->mmMode
= GR_MIPMAP_NEAREST
;
473 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
474 ti
->LODblend
= FXFALSE
;
479 ti
->reloadImages
= GL_TRUE
;
480 RevalidateTexture(ctx
, tObj
);
481 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
484 case GL_TEXTURE_MAG_FILTER
:
487 ti
->magFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
490 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
495 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
498 case GL_TEXTURE_WRAP_S
:
500 case GL_CLAMP_TO_BORDER
:
501 case GL_CLAMP_TO_EDGE
:
503 ti
->sClamp
= GR_TEXTURECLAMP_CLAMP
;
506 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
508 case GL_MIRRORED_REPEAT
:
509 ti
->sClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
514 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
517 case GL_TEXTURE_WRAP_T
:
519 case GL_CLAMP_TO_BORDER
:
520 case GL_CLAMP_TO_EDGE
:
522 ti
->tClamp
= GR_TEXTURECLAMP_CLAMP
;
525 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
527 case GL_MIRRORED_REPEAT
:
528 ti
->tClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
533 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
536 case GL_TEXTURE_BORDER_COLOR
:
539 case GL_TEXTURE_MIN_LOD
:
542 case GL_TEXTURE_MAX_LOD
:
545 case GL_TEXTURE_BASE_LEVEL
:
546 RevalidateTexture(ctx
, tObj
);
548 case GL_TEXTURE_MAX_LEVEL
:
549 RevalidateTexture(ctx
, tObj
);
559 * Called via glDeleteTextures to delete a texture object.
560 * Here, we delete the Glide data associated with the texture.
563 tdfxDeleteTexture(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
565 if (ctx
&& ctx
->DriverCtx
) {
566 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
567 tdfxTMFreeTexture(fxMesa
, tObj
);
568 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
569 /* Free mipmap images and the texture object itself */
570 _mesa_delete_texture_object(ctx
, tObj
);
576 * Return true if texture is resident, false otherwise.
579 tdfxIsTextureResident(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
581 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
582 return (GLboolean
) (ti
&& ti
->isInTM
);
588 * Convert a gl_color_table texture palette to Glide's format.
591 convertPalette(FxU32 data
[256], const struct gl_color_table
*table
)
593 const GLubyte
*tableUB
= (const GLubyte
*) table
->Table
;
594 GLint width
= table
->Size
;
598 ASSERT(table
->Type
== GL_UNSIGNED_BYTE
);
600 switch (table
->Format
) {
602 for (i
= 0; i
< width
; i
++) {
607 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
609 return GR_TEXTABLE_PALETTE_6666_EXT
;
611 for (i
= 0; i
< width
; i
++) {
616 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
618 return GR_TEXTABLE_PALETTE
;
620 for (i
= 0; i
< width
; i
++) {
623 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
625 return GR_TEXTABLE_PALETTE_6666_EXT
;
626 case GL_LUMINANCE_ALPHA
:
627 for (i
= 0; i
< width
; i
++) {
628 r
= g
= b
= tableUB
[i
* 2 + 0];
629 a
= tableUB
[i
* 2 + 1];
630 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
632 return GR_TEXTABLE_PALETTE_6666_EXT
;
634 for (i
= 0; i
< width
; i
++) {
635 r
= tableUB
[i
* 3 + 0];
636 g
= tableUB
[i
* 3 + 1];
637 b
= tableUB
[i
* 3 + 2];
639 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
641 return GR_TEXTABLE_PALETTE
;
643 for (i
= 0; i
< width
; i
++) {
644 r
= tableUB
[i
* 4 + 0];
645 g
= tableUB
[i
* 4 + 1];
646 b
= tableUB
[i
* 4 + 2];
647 a
= tableUB
[i
* 4 + 3];
648 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
650 return GR_TEXTABLE_PALETTE_6666_EXT
;
658 tdfxUpdateTexturePalette(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
660 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
663 /* per-texture palette */
666 /* This might be a proxy texture. */
667 if (!tObj
->Palette
.Table
)
670 if (!tObj
->DriverData
)
671 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
672 ti
= TDFX_TEXTURE_DATA(tObj
);
674 ti
->paltype
= convertPalette(ti
->palette
.data
, &tObj
->Palette
);
675 /*tdfxTexInvalidate(ctx, tObj);*/
678 /* global texture palette */
679 fxMesa
->TexPalette
.Type
= convertPalette(fxMesa
->glbPalette
.data
, &ctx
->Texture
.Palette
);
680 fxMesa
->TexPalette
.Data
= &(fxMesa
->glbPalette
.data
);
681 fxMesa
->dirty
|= TDFX_UPLOAD_TEXTURE_PALETTE
;
683 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX too heavy-handed */
687 /**********************************************************************/
688 /**** NEW TEXTURE IMAGE FUNCTIONS ****/
689 /**********************************************************************/
692 static FxBool TexusFatalError
= FXFALSE
;
693 static FxBool TexusError
= FXFALSE
;
695 #define TX_DITHER_NONE 0x00000000
698 fxTexusError(const char *string
, FxBool fatal
)
700 _mesa_problem(NULL
, string
);
702 * Just propagate the fatal value up.
705 TexusFatalError
= fatal
;
710 static const struct gl_texture_format
*
711 tdfxChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
712 GLenum srcFormat
, GLenum srcType
)
714 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
715 const GLboolean allow32bpt
= TDFX_IS_NAPALM(fxMesa
);
717 switch (internalFormat
) {
723 case GL_COMPRESSED_ALPHA
:
724 return &_mesa_texformat_a8
;
731 case GL_COMPRESSED_LUMINANCE
:
732 return &_mesa_texformat_l8
;
734 case GL_LUMINANCE_ALPHA
:
735 case GL_LUMINANCE4_ALPHA4
:
736 case GL_LUMINANCE6_ALPHA2
:
737 case GL_LUMINANCE8_ALPHA8
:
738 case GL_LUMINANCE12_ALPHA4
:
739 case GL_LUMINANCE12_ALPHA12
:
740 case GL_LUMINANCE16_ALPHA16
:
741 case GL_COMPRESSED_LUMINANCE_ALPHA
:
742 return &_mesa_texformat_al88
;
748 case GL_COMPRESSED_INTENSITY
:
749 return &_mesa_texformat_i8
;
753 return &_mesa_texformat_rgb565
;
754 case GL_COMPRESSED_RGB
:
755 /* intentional fall-through */
758 if ( srcFormat
== GL_RGB
&& srcType
== GL_UNSIGNED_SHORT_5_6_5
) {
759 return &_mesa_texformat_rgb565
;
761 /* intentional fall through */
766 return (allow32bpt
) ? &_mesa_texformat_argb8888
767 : &_mesa_texformat_rgb565
;
770 return &_mesa_texformat_argb4444
;
771 case GL_COMPRESSED_RGBA
:
772 /* intentional fall-through */
775 if ( srcFormat
== GL_BGRA
) {
776 if ( srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
777 return &_mesa_texformat_argb8888
;
779 else if ( srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
780 return &_mesa_texformat_argb4444
;
782 else if ( srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
783 return &_mesa_texformat_argb1555
;
786 /* intentional fall through */
791 return allow32bpt
? &_mesa_texformat_argb8888
792 : &_mesa_texformat_argb4444
;
794 return &_mesa_texformat_argb1555
;
796 case GL_COLOR_INDEX1_EXT
:
797 case GL_COLOR_INDEX2_EXT
:
798 case GL_COLOR_INDEX4_EXT
:
799 case GL_COLOR_INDEX8_EXT
:
800 case GL_COLOR_INDEX12_EXT
:
801 case GL_COLOR_INDEX16_EXT
:
802 return &_mesa_texformat_ci8
;
803 /* GL_EXT_texture_compression_s3tc */
805 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
808 return &_mesa_texformat_rgb_dxt1
;
809 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
810 return &_mesa_texformat_rgba_dxt1
;
811 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
814 return &_mesa_texformat_rgba_dxt3
;
815 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
816 return &_mesa_texformat_rgba_dxt5
;
817 /* GL_3DFX_texture_compression_FXT1 */
818 case GL_COMPRESSED_RGB_FXT1_3DFX
:
819 return &_mesa_texformat_rgb_fxt1
;
820 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
821 return &_mesa_texformat_rgba_fxt1
;
823 _mesa_problem(ctx
, "unexpected format in tdfxChooseTextureFormat");
830 * Return the Glide format for the given mesa texture format.
832 static GrTextureFormat_t
833 fxGlideFormat(GLint mesaFormat
)
835 switch (mesaFormat
) {
837 return GR_TEXFMT_ALPHA_8
;
839 return GR_TEXFMT_ALPHA_8
;
841 return GR_TEXFMT_INTENSITY_8
;
842 case MESA_FORMAT_CI8
:
843 return GR_TEXFMT_P_8
;
844 case MESA_FORMAT_AL88
:
845 return GR_TEXFMT_ALPHA_INTENSITY_88
;
846 case MESA_FORMAT_RGB565
:
847 return GR_TEXFMT_RGB_565
;
848 case MESA_FORMAT_ARGB4444
:
849 return GR_TEXFMT_ARGB_4444
;
850 case MESA_FORMAT_ARGB1555
:
851 return GR_TEXFMT_ARGB_1555
;
852 case MESA_FORMAT_ARGB8888
:
853 return GR_TEXFMT_ARGB_8888
;
854 case MESA_FORMAT_RGB_FXT1
:
855 case MESA_FORMAT_RGBA_FXT1
:
856 return GR_TEXFMT_ARGB_CMP_FXT1
;
857 case MESA_FORMAT_RGB_DXT1
:
858 case MESA_FORMAT_RGBA_DXT1
:
859 return GR_TEXFMT_ARGB_CMP_DXT1
;
860 case MESA_FORMAT_RGBA_DXT3
:
861 return GR_TEXFMT_ARGB_CMP_DXT3
;
862 case MESA_FORMAT_RGBA_DXT5
:
863 return GR_TEXFMT_ARGB_CMP_DXT5
;
865 _mesa_problem(NULL
, "Unexpected format in fxGlideFormat");
871 /* Texel-fetch functions for software texturing and glGetTexImage().
872 * We should have been able to use some "standard" fetch functions (which
873 * may get defined in texutil.c) but we have to account for scaled texture
874 * images on tdfx hardware (the 8:1 aspect ratio limit).
875 * Hence, we need special functions here.
878 fxt1_decode_1 (const void *texture
, int width
,
879 int i
, int j
, unsigned char *rgba
);
882 fetch_intensity8(const struct gl_texture_image
*texImage
,
883 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
885 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
886 const GLubyte
*texel
;
891 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
892 rgba
[RCOMP
] = *texel
;
893 rgba
[GCOMP
] = *texel
;
894 rgba
[BCOMP
] = *texel
;
895 rgba
[ACOMP
] = *texel
;
900 fetch_luminance8(const struct gl_texture_image
*texImage
,
901 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
903 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
904 const GLubyte
*texel
;
909 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
910 rgba
[RCOMP
] = *texel
;
911 rgba
[GCOMP
] = *texel
;
912 rgba
[BCOMP
] = *texel
;
918 fetch_alpha8(const struct gl_texture_image
*texImage
,
919 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
921 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
922 const GLubyte
*texel
;
927 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
931 rgba
[ACOMP
] = *texel
;
936 fetch_index8(const struct gl_texture_image
*texImage
,
937 GLint i
, GLint j
, GLint k
, GLchan
* indexOut
)
939 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
940 const GLubyte
*texel
;
945 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
951 fetch_luminance8_alpha8(const struct gl_texture_image
*texImage
,
952 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
954 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
955 const GLubyte
*texel
;
960 texel
= ((GLubyte
*) texImage
->Data
) + (j
* mml
->width
+ i
) * 2;
961 rgba
[RCOMP
] = texel
[0];
962 rgba
[GCOMP
] = texel
[0];
963 rgba
[BCOMP
] = texel
[0];
964 rgba
[ACOMP
] = texel
[1];
969 fetch_r5g6b5(const struct gl_texture_image
*texImage
,
970 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
972 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
973 const GLushort
*texel
;
978 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
979 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
980 rgba
[GCOMP
] = (((*texel
) >> 5) & 0x3f) * 255 / 63;
981 rgba
[BCOMP
] = (((*texel
) >> 0) & 0x1f) * 255 / 31;
987 fetch_r4g4b4a4(const struct gl_texture_image
*texImage
,
988 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
990 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
991 const GLushort
*texel
;
996 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
997 rgba
[RCOMP
] = (((*texel
) >> 12) & 0xf) * 255 / 15;
998 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xf) * 255 / 15;
999 rgba
[BCOMP
] = (((*texel
) >> 4) & 0xf) * 255 / 15;
1000 rgba
[ACOMP
] = (((*texel
) >> 0) & 0xf) * 255 / 15;
1005 fetch_r5g5b5a1(const struct gl_texture_image
*texImage
,
1006 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1008 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1009 const GLushort
*texel
;
1011 i
= i
* mml
->wScale
;
1012 j
= j
* mml
->hScale
;
1014 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1015 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
1016 rgba
[GCOMP
] = (((*texel
) >> 6) & 0x1f) * 255 / 31;
1017 rgba
[BCOMP
] = (((*texel
) >> 1) & 0x1f) * 255 / 31;
1018 rgba
[ACOMP
] = (((*texel
) >> 0) & 0x01) * 255;
1023 fetch_a8r8g8b8(const struct gl_texture_image
*texImage
,
1024 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1026 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1027 const GLuint
*texel
;
1029 i
= i
* mml
->wScale
;
1030 j
= j
* mml
->hScale
;
1032 texel
= ((GLuint
*) texImage
->Data
) + j
* mml
->width
+ i
;
1033 rgba
[RCOMP
] = (((*texel
) >> 16) & 0xff);
1034 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xff);
1035 rgba
[BCOMP
] = (((*texel
) ) & 0xff);
1036 rgba
[ACOMP
] = (((*texel
) >> 24) & 0xff);
1041 fetch_rgb_fxt1(const struct gl_texture_image
*texImage
,
1042 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1044 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1046 i
= i
* mml
->wScale
;
1047 j
= j
* mml
->hScale
;
1049 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1055 fetch_rgba_fxt1(const struct gl_texture_image
*texImage
,
1056 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1058 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1060 i
= i
* mml
->wScale
;
1061 j
= j
* mml
->hScale
;
1063 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1068 fetch_rgb_dxt1(const struct gl_texture_image
*texImage
,
1069 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1071 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1073 i
= i
* mml
->wScale
;
1074 j
= j
* mml
->hScale
;
1076 _mesa_texformat_rgb_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1081 fetch_rgba_dxt1(const struct gl_texture_image
*texImage
,
1082 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1084 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1086 i
= i
* mml
->wScale
;
1087 j
= j
* mml
->hScale
;
1089 _mesa_texformat_rgba_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1094 fetch_rgba_dxt3(const struct gl_texture_image
*texImage
,
1095 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1097 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1099 i
= i
* mml
->wScale
;
1100 j
= j
* mml
->hScale
;
1102 _mesa_texformat_rgba_dxt3
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1107 fetch_rgba_dxt5(const struct gl_texture_image
*texImage
,
1108 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1110 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1112 i
= i
* mml
->wScale
;
1113 j
= j
* mml
->hScale
;
1115 _mesa_texformat_rgba_dxt5
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1119 static FetchTexelFuncC
1120 fxFetchFunction(GLint mesaFormat
)
1122 switch (mesaFormat
) {
1123 case MESA_FORMAT_I8
:
1124 return &fetch_intensity8
;
1125 case MESA_FORMAT_A8
:
1126 return &fetch_alpha8
;
1127 case MESA_FORMAT_L8
:
1128 return &fetch_luminance8
;
1129 case MESA_FORMAT_CI8
:
1130 return &fetch_index8
;
1131 case MESA_FORMAT_AL88
:
1132 return &fetch_luminance8_alpha8
;
1133 case MESA_FORMAT_RGB565
:
1134 return &fetch_r5g6b5
;
1135 case MESA_FORMAT_ARGB4444
:
1136 return &fetch_r4g4b4a4
;
1137 case MESA_FORMAT_ARGB1555
:
1138 return &fetch_r5g5b5a1
;
1139 case MESA_FORMAT_ARGB8888
:
1140 return &fetch_a8r8g8b8
;
1141 case MESA_FORMAT_RGB_FXT1
:
1142 return &fetch_rgb_fxt1
;
1143 case MESA_FORMAT_RGBA_FXT1
:
1144 return &fetch_rgba_fxt1
;
1145 case MESA_FORMAT_RGB_DXT1
:
1146 return &fetch_rgb_dxt1
;
1147 case MESA_FORMAT_RGBA_DXT1
:
1148 return &fetch_rgba_dxt1
;
1149 case MESA_FORMAT_RGBA_DXT3
:
1150 return &fetch_rgba_dxt3
;
1151 case MESA_FORMAT_RGBA_DXT5
:
1152 return &fetch_rgba_dxt5
;
1154 _mesa_problem(NULL
, "Unexpected format in fxFetchFunction");
1161 adjust2DRatio (GLcontext
*ctx
,
1162 GLint xoffset
, GLint yoffset
,
1163 GLint width
, GLint height
,
1164 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1165 const struct gl_pixelstore_attrib
*packing
,
1166 tdfxMipMapLevel
*mml
,
1167 struct gl_texture_image
*texImage
,
1171 const GLint newWidth
= width
* mml
->wScale
;
1172 const GLint newHeight
= height
* mml
->hScale
;
1175 if (!texImage
->IsCompressed
) {
1177 tempImage
= MALLOC(width
* height
* texelBytes
);
1182 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
1183 texImage
->TexFormat
, tempImage
,
1184 0, 0, 0, /* dstX/Y/Zoffset */
1185 width
* texelBytes
, /* dstRowStride */
1186 0, /* dstImageStride */
1188 format
, type
, pixels
, packing
);
1191 /* compute address of dest subimage within the overal tex image */
1192 destAddr
= (GLubyte
*) texImage
->Data
1193 + (yoffset
* mml
->hScale
* mml
->width
1194 + xoffset
* mml
->wScale
) * texelBytes
;
1196 _mesa_rescale_teximage2d(texelBytes
,
1198 dstRowStride
, /* dst stride */
1200 newWidth
, newHeight
,
1201 tempImage
, destAddr
);
1203 const GLint rawBytes
= 4;
1204 GLvoid
*rawImage
= MALLOC(width
* height
* rawBytes
);
1208 tempImage
= MALLOC(newWidth
* newHeight
* rawBytes
);
1212 /* unpack image, apply transfer ops and store in rawImage */
1213 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
1214 &_mesa_texformat_rgba8888_rev
, rawImage
,
1215 0, 0, 0, /* dstX/Y/Zoffset */
1216 width
* rawBytes
, /* dstRowStride */
1217 0, /* dstImageStride */
1219 format
, type
, pixels
, packing
);
1220 _mesa_rescale_teximage2d(rawBytes
,
1222 newWidth
* rawBytes
, /* dst stride */
1223 width
, height
, /* src */
1224 newWidth
, newHeight
, /* dst */
1225 rawImage
/*src*/, tempImage
/*dst*/ );
1226 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
1227 texImage
->TexFormat
, texImage
->Data
,
1228 xoffset
* mml
->wScale
, yoffset
* mml
->hScale
, 0, /* dstX/Y/Zoffset */
1230 0, /* dstImageStride */
1231 newWidth
, newHeight
, 1,
1232 GL_RGBA
, CHAN_TYPE
, tempImage
, &ctx
->DefaultPacking
);
1243 tdfxTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1244 GLint internalFormat
, GLint width
, GLint height
, GLint border
,
1245 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1246 const struct gl_pixelstore_attrib
*packing
,
1247 struct gl_texture_object
*texObj
,
1248 struct gl_texture_image
*texImage
)
1250 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1252 tdfxMipMapLevel
*mml
;
1253 GLint texelBytes
, dstRowStride
;
1256 printf("TexImage id=%d int 0x%x format 0x%x type 0x%x %dx%d\n",
1257 texObj->Name, texImage->IntFormat, format, type,
1258 texImage->Width, texImage->Height);
1261 ti
= TDFX_TEXTURE_DATA(texObj
);
1263 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1264 if (!texObj
->DriverData
) {
1265 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1268 ti
= TDFX_TEXTURE_DATA(texObj
);
1272 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1274 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1275 if (!texImage
->DriverData
) {
1276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1279 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1282 /* Determine width and height scale factors for texture.
1283 * Remember, Glide is limited to 8:1 aspect ratios.
1286 texImage
->Width
, texImage
->Height
,
1287 NULL
, /* lod level */
1288 NULL
, /* aspect ratio */
1289 NULL
, NULL
, /* sscale, tscale */
1290 &mml
->wScale
, &mml
->hScale
);
1292 /* rescaled size: */
1293 mml
->width
= width
* mml
->wScale
;
1294 mml
->height
= height
* mml
->hScale
;
1296 #if FX_COMPRESS_S3TC_AS_FXT1_HACK
1297 /* [koolsmoky] substitute FXT1 for DXTn and Legacy S3TC */
1298 /* [dBorca] we should update texture's attribute, then,
1299 * because if the application asks us to decompress, we
1300 * have to know the REAL format! Also, DXT3/5 might not
1301 * be correct, since it would mess with "compressedSize".
1302 * Ditto for GL_RGBA[4]_S3TC, which is always mapped to DXT3.
1304 if (texImage
->IsCompressed
) {
1305 switch (internalFormat
) {
1306 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1309 internalFormat
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1311 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1312 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1313 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1316 internalFormat
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1318 texImage
->IntFormat
= internalFormat
;
1322 if (fxMesa
->type
>= GR_SSTTYPE_Voodoo4
) {
1323 GLenum texNapalm
= 0;
1324 if (internalFormat
== GL_COMPRESSED_RGB
) {
1325 texNapalm
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1326 } else if (internalFormat
== GL_COMPRESSED_RGBA
) {
1327 texNapalm
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1330 texImage
->IntFormat
= internalFormat
= texNapalm
;
1331 texImage
->IsCompressed
= GL_TRUE
;
1336 /* choose the texture format */
1337 assert(ctx
->Driver
.ChooseTextureFormat
);
1338 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1339 internalFormat
, format
, type
);
1340 assert(texImage
->TexFormat
);
1341 mml
->glideFormat
= fxGlideFormat(texImage
->TexFormat
->MesaFormat
);
1342 ti
->info
.format
= mml
->glideFormat
;
1343 texImage
->FetchTexelc
= fxFetchFunction(texImage
->TexFormat
->MesaFormat
);
1344 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1346 if (texImage
->IsCompressed
) {
1347 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1352 dstRowStride
= _mesa_compressed_row_stride(internalFormat
, mml
->width
);
1353 texImage
->Data
= MESA_PBUFFER_ALLOC(texImage
->CompressedSize
);
1355 dstRowStride
= mml
->width
* texelBytes
;
1356 texImage
->Data
= MESA_PBUFFER_ALLOC(mml
->width
* mml
->height
* texelBytes
);
1358 if (!texImage
->Data
) {
1359 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1363 if (pixels
!= NULL
) {
1364 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1365 /* rescale image to overcome 1:8 aspect limitation */
1366 if (!adjust2DRatio(ctx
,
1369 format
, type
, pixels
,
1376 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1381 /* no rescaling needed */
1382 /* unpack image, apply transfer ops and store in texImage->Data */
1383 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
1384 texImage
->TexFormat
, texImage
->Data
,
1385 0, 0, 0, /* dstX/Y/Zoffset */
1387 0, /* dstImageStride */
1389 format
, type
, pixels
, packing
);
1392 /* GL_SGIS_generate_mipmap */
1393 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1394 GLint mipWidth
, mipHeight
;
1395 tdfxMipMapLevel
*mip
;
1396 struct gl_texture_image
*mipImage
;
1397 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1398 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1400 assert(!texImage
->IsCompressed
);
1402 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
1403 mipWidth
= width
/ 2;
1407 mipHeight
= height
/ 2;
1411 if ((mipWidth
== width
) && (mipHeight
== height
)) {
1414 _mesa_TexImage2D(target
, ++level
, internalFormat
,
1415 mipWidth
, mipHeight
, border
,
1418 mipImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1419 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
1420 _mesa_halve2x2_teximage2d(ctx
,
1423 mml
->width
, mml
->height
,
1424 texImage
->Data
, mipImage
->Data
);
1425 texImage
= mipImage
;
1433 RevalidateTexture(ctx
, texObj
);
1435 ti
->reloadImages
= GL_TRUE
;
1436 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1441 tdfxTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1442 GLint xoffset
, GLint yoffset
,
1443 GLsizei width
, GLsizei height
,
1444 GLenum format
, GLenum type
,
1445 const GLvoid
*pixels
,
1446 const struct gl_pixelstore_attrib
*packing
,
1447 struct gl_texture_object
*texObj
,
1448 struct gl_texture_image
*texImage
)
1450 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1452 tdfxMipMapLevel
*mml
;
1453 GLint texelBytes
, dstRowStride
;
1455 if (!texObj
->DriverData
) {
1456 _mesa_problem(ctx
, "problem in fxDDTexSubImage2D");
1460 ti
= TDFX_TEXTURE_DATA(texObj
);
1462 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1465 assert(texImage
->Data
); /* must have an existing texture image! */
1466 assert(texImage
->Format
);
1468 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1469 if (texImage
->IsCompressed
) {
1470 dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
, mml
->width
);
1472 dstRowStride
= mml
->width
* texelBytes
;
1475 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1476 /* need to rescale subimage to match mipmap level's rescale factors */
1477 if (!adjust2DRatio(ctx
,
1480 format
, type
, pixels
,
1487 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
1492 /* no rescaling needed */
1493 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
1494 texImage
->TexFormat
, texImage
->Data
,
1495 xoffset
, yoffset
, 0,
1497 0, /* dstImageStride */
1499 format
, type
, pixels
, packing
);
1502 /* GL_SGIS_generate_mipmap */
1503 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1504 GLint mipWidth
, mipHeight
;
1505 tdfxMipMapLevel
*mip
;
1506 struct gl_texture_image
*mipImage
;
1507 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1508 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1510 assert(!texImage
->IsCompressed
);
1512 width
= texImage
->Width
;
1513 height
= texImage
->Height
;
1514 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
1515 mipWidth
= width
/ 2;
1519 mipHeight
= height
/ 2;
1523 if ((mipWidth
== width
) && (mipHeight
== height
)) {
1527 mipImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1528 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
1529 _mesa_halve2x2_teximage2d(ctx
,
1532 mml
->width
, mml
->height
,
1533 texImage
->Data
, mipImage
->Data
);
1534 texImage
= mipImage
;
1541 ti
->reloadImages
= GL_TRUE
; /* signal the image needs to be reloaded */
1542 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX this might be a bit much */
1547 tdfxTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1548 GLint internalFormat
, GLint width
, GLint border
,
1549 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1550 const struct gl_pixelstore_attrib
*packing
,
1551 struct gl_texture_object
*texObj
,
1552 struct gl_texture_image
*texImage
)
1554 tdfxTexImage2D(ctx
, target
, level
,
1555 internalFormat
, width
, 1, border
,
1556 format
, type
, pixels
,
1563 tdfxTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1566 GLenum format
, GLenum type
,
1567 const GLvoid
*pixels
,
1568 const struct gl_pixelstore_attrib
*packing
,
1569 struct gl_texture_object
*texObj
,
1570 struct gl_texture_image
*texImage
)
1572 tdfxTexSubImage2D(ctx
, target
, level
,
1582 /**********************************************************************/
1583 /**** COMPRESSED TEXTURE IMAGE FUNCTIONS ****/
1584 /**********************************************************************/
1587 tdfxCompressedTexImage2D (GLcontext
*ctx
, GLenum target
,
1588 GLint level
, GLint internalFormat
,
1589 GLsizei width
, GLsizei height
, GLint border
,
1590 GLsizei imageSize
, const GLvoid
*data
,
1591 struct gl_texture_object
*texObj
,
1592 struct gl_texture_image
*texImage
)
1594 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1596 tdfxMipMapLevel
*mml
;
1598 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1599 fprintf(stderr
, "tdfxCompressedTexImage2D: id=%d int 0x%x %dx%d\n",
1600 texObj
->Name
, internalFormat
,
1604 if ((target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_2D
) || texImage
->Border
> 0) {
1605 _mesa_problem(NULL
, "tdfx: unsupported texture in tdfxCompressedTexImg()\n");
1609 assert(texImage
->IsCompressed
);
1611 ti
= TDFX_TEXTURE_DATA(texObj
);
1613 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1614 if (!texObj
->DriverData
) {
1615 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1618 ti
= TDFX_TEXTURE_DATA(texObj
);
1622 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1624 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1625 if (!texImage
->DriverData
) {
1626 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1629 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1632 tdfxTexGetInfo(ctx
, width
, height
, NULL
, NULL
, NULL
, NULL
,
1633 &mml
->wScale
, &mml
->hScale
);
1635 mml
->width
= width
* mml
->wScale
;
1636 mml
->height
= height
* mml
->hScale
;
1639 /* choose the texture format */
1640 assert(ctx
->Driver
.ChooseTextureFormat
);
1641 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1642 internalFormat
, -1/*format*/, -1/*type*/);
1643 assert(texImage
->TexFormat
);
1645 /* Determine the appropriate Glide texel format,
1646 * given the user's internal texture format hint.
1648 mml
->glideFormat
= fxGlideFormat(texImage
->TexFormat
->MesaFormat
);
1649 ti
->info
.format
= mml
->glideFormat
;
1650 texImage
->FetchTexelc
= fxFetchFunction(texImage
->TexFormat
->MesaFormat
);
1652 /* allocate new storage for texture image, if needed */
1653 if (!texImage
->Data
) {
1654 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1659 texImage
->Data
= MESA_PBUFFER_ALLOC(texImage
->CompressedSize
);
1660 if (!texImage
->Data
) {
1661 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1666 /* save the texture data */
1667 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1668 /* [dBorca] Hack alert:
1669 * now we're screwed. We can't decompress,
1670 * unless we do it in HW (via textureBuffer).
1671 * We still have some chances:
1672 * 1) we got FXT1 textures - we CAN decompress, rescale for
1673 * aspectratio, then compress back.
1674 * 2) there is a chance that MIN("s", "t") won't be overflowed.
1675 * Thus, we don't care about textureclamp and we could lower
1676 * MIN("uscale", "vscale") below 32. We still have to have
1677 * our data aligned inside a 8:1 rectangle.
1678 * 3) just in case if MIN("s", "t") gets overflowed with GL_REPEAT,
1679 * we replicate the data over the padded area.
1680 * For now, we take 2) + 3) but texelfetchers will be wrong!
1682 GLuint srcRowStride
= _mesa_compressed_row_stride(internalFormat
, width
);
1684 GLuint destRowStride
= _mesa_compressed_row_stride(internalFormat
,
1687 _mesa_upscale_teximage2d(srcRowStride
, (height
+3) / 4,
1688 destRowStride
, (mml
->height
+3) / 4,
1689 1, data
, srcRowStride
,
1691 ti
->padded
= GL_TRUE
;
1693 MEMCPY(texImage
->Data
, data
, texImage
->CompressedSize
);
1696 /* GL_SGIS_generate_mipmap */
1697 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1698 assert(!texImage
->IsCompressed
);
1701 RevalidateTexture(ctx
, texObj
);
1703 ti
->reloadImages
= GL_TRUE
;
1704 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1709 tdfxCompressedTexSubImage2D( GLcontext
*ctx
, GLenum target
,
1710 GLint level
, GLint xoffset
,
1711 GLint yoffset
, GLsizei width
,
1712 GLint height
, GLenum format
,
1713 GLsizei imageSize
, const GLvoid
*data
,
1714 struct gl_texture_object
*texObj
,
1715 struct gl_texture_image
*texImage
)
1717 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1719 tdfxMipMapLevel
*mml
;
1720 GLint destRowStride
, srcRowStride
;
1724 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1725 fprintf(stderr
, "tdfxCompressedTexSubImage2D: id=%d\n", texObj
->Name
);
1728 ti
= TDFX_TEXTURE_DATA(texObj
);
1730 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1733 srcRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
, width
);
1735 destRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
1737 dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
1738 texImage
->IntFormat
,
1740 (GLubyte
*) texImage
->Data
);
1742 rows
= height
/ 4; /* [dBorca] hardcoded 4, but works for FXT1/DXTC */
1744 for (i
= 0; i
< rows
; i
++) {
1745 MEMCPY(dest
, data
, srcRowStride
);
1746 dest
+= destRowStride
;
1747 data
= (GLvoid
*)((GLuint
)data
+ (GLuint
)srcRowStride
);
1750 /* [dBorca] Hack alert:
1751 * see fxDDCompressedTexImage2D for caveats
1753 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1754 srcRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
, texImage
->Width
);
1756 destRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
1758 _mesa_upscale_teximage2d(srcRowStride
, texImage
->Height
/ 4,
1759 destRowStride
, mml
->height
/ 4,
1760 1, texImage
->Data
, destRowStride
,
1764 /* GL_SGIS_generate_mipmap */
1765 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1766 assert(!texImage
->IsCompressed
);
1769 RevalidateTexture(ctx
, texObj
);
1771 ti
->reloadImages
= GL_TRUE
;
1772 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1778 PrintTexture(int w
, int h
, int c
, const GLubyte
* data
)
1781 for (i
= 0; i
< h
; i
++) {
1782 for (j
= 0; j
< w
; j
++) {
1784 printf("%02x %02x ", data
[0], data
[1]);
1786 printf("%02x %02x %02x ", data
[0], data
[1], data
[2]);
1796 tdfxTestProxyTexImage(GLcontext
*ctx
, GLenum target
,
1797 GLint level
, GLint internalFormat
,
1798 GLenum format
, GLenum type
,
1799 GLint width
, GLint height
,
1800 GLint depth
, GLint border
)
1802 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1803 struct gl_shared_state
*mesaShared
= fxMesa
->glCtx
->Shared
;
1804 struct tdfxSharedState
*shared
= (struct tdfxSharedState
*) mesaShared
->DriverData
;
1807 case GL_PROXY_TEXTURE_1D
:
1809 case GL_PROXY_TEXTURE_2D
:
1811 struct gl_texture_object
*tObj
;
1815 tObj
= ctx
->Texture
.Proxy2D
;
1816 if (!tObj
->DriverData
)
1817 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1818 ti
= TDFX_TEXTURE_DATA(tObj
);
1821 /* assign the parameters to test against */
1822 tObj
->Image
[0][level
]->Width
= width
;
1823 tObj
->Image
[0][level
]->Height
= height
;
1824 tObj
->Image
[0][level
]->Border
= border
;
1826 tObj
->Image
[0][level
]->IntFormat
= internalFormat
;
1829 /* don't use mipmap levels > 0 */
1830 tObj
->MinFilter
= tObj
->MagFilter
= GL_NEAREST
;
1833 /* test with all mipmap levels */
1834 tObj
->MinFilter
= GL_LINEAR_MIPMAP_LINEAR
;
1835 tObj
->MagFilter
= GL_NEAREST
;
1837 RevalidateTexture(ctx
, tObj
);
1840 printf("small lodlog2 0x%x\n", ti->info.smallLodLog2);
1841 printf("large lodlog2 0x%x\n", ti->info.largeLodLog2);
1842 printf("aspect ratio 0x%x\n", ti->info.aspectRatioLog2);
1843 printf("glide format 0x%x\n", ti->info.format);
1844 printf("data %p\n", ti->info.data);
1845 printf("lodblend %d\n", (int) ti->LODblend);
1848 /* determine where texture will reside */
1849 if (ti
->LODblend
&& !shared
->umaTexMemory
) {
1850 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1851 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1852 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1855 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1856 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1857 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1860 printf("Proxy test %d > %d\n", memNeeded, shared->totalTexMem[0]);
1862 if (memNeeded
> shared
->totalTexMem
[0])
1867 case GL_PROXY_TEXTURE_3D
:
1868 return GL_TRUE
; /* software rendering */
1870 return GL_TRUE
; /* never happens, silence compiler */
1876 * Allocate a new texture object.
1877 * Called via ctx->Driver.NewTextureObject.
1878 * Note: this function will be called during context creation to
1879 * allocate the default texture objects.
1880 * Note: we could use containment here to 'derive' the driver-specific
1881 * texture object from the core mesa gl_texture_object. Not done at this time.
1883 static struct gl_texture_object
*
1884 tdfxNewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
1886 struct gl_texture_object
*obj
;
1887 obj
= _mesa_new_texture_object(ctx
, name
, target
);
1892 void tdfxInitTextureFuncs( struct dd_function_table
*functions
)
1894 functions
->BindTexture
= tdfxBindTexture
;
1895 functions
->NewTextureObject
= tdfxNewTextureObject
;
1896 functions
->DeleteTexture
= tdfxDeleteTexture
;
1897 functions
->TexEnv
= tdfxTexEnv
;
1898 functions
->TexParameter
= tdfxTexParameter
;
1899 functions
->ChooseTextureFormat
= tdfxChooseTextureFormat
;
1900 functions
->TexImage1D
= tdfxTexImage1D
;
1901 functions
->TexSubImage1D
= tdfxTexSubImage1D
;
1902 functions
->TexImage2D
= tdfxTexImage2D
;
1903 functions
->TexSubImage2D
= tdfxTexSubImage2D
;
1904 functions
->IsTextureResident
= tdfxIsTextureResident
;
1905 functions
->CompressedTexImage2D
= tdfxCompressedTexImage2D
;
1906 functions
->CompressedTexSubImage2D
= tdfxCompressedTexSubImage2D
;
1907 functions
->UpdateTexturePalette
= tdfxUpdateTexturePalette
;