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>
44 #include "texcompress.h"
45 #include "texformat.h"
49 #include "tdfx_context.h"
51 #include "tdfx_texman.h"
54 /* no borders! can't halve 1x1! (stride > width * comp) not allowed */
56 _mesa_halve2x2_teximage2d ( GLcontext
*ctx
,
57 struct gl_texture_image
*texImage
,
59 GLint srcWidth
, GLint srcHeight
,
60 const GLvoid
*srcImage
, GLvoid
*dstImage
)
63 GLint dstWidth
= srcWidth
/ 2;
64 GLint dstHeight
= srcHeight
/ 2;
65 GLint srcRowStride
= srcWidth
* bytesPerPixel
;
66 GLubyte
*src
= (GLubyte
*)srcImage
;
67 GLubyte
*dst
= dstImage
;
68 GLuint dstImageOffsets
= 0;
75 if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB565
) {
76 _t
= GL_UNSIGNED_SHORT_5_6_5_REV
;
78 } else if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_ARGB4444
) {
79 _t
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
81 } else if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_ARGB1555
) {
82 _t
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
87 srcRowStride
= srcWidth
* bytesPerPixel
;
94 _s
= src
= MALLOC(srcRowStride
* srcHeight
);
95 _d
= dst
= MALLOC(dstWidth
* bytesPerPixel
* dstHeight
);
96 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
97 &_mesa_texformat_rgba8888_rev
, src
,
98 0, 0, 0, /* dstX/Y/Zoffset */
99 srcRowStride
, /* dstRowStride */
101 srcWidth
, srcHeight
, 1,
102 texImage
->_BaseFormat
, _t
, srcImage
, &ctx
->DefaultPacking
);
105 if (srcHeight
== 1) {
106 for (i
= 0; i
< dstWidth
; i
++) {
107 for (k
= 0; k
< bytesPerPixel
; k
++) {
108 dst
[0] = (src
[0] + src
[bytesPerPixel
] + 1) / 2;
112 src
+= bytesPerPixel
;
114 } else if (srcWidth
== 1) {
115 for (j
= 0; j
< dstHeight
; j
++) {
116 for (k
= 0; k
< bytesPerPixel
; k
++) {
117 dst
[0] = (src
[0] + src
[srcRowStride
] + 1) / 2;
124 for (j
= 0; j
< dstHeight
; j
++) {
125 for (i
= 0; i
< dstWidth
; i
++) {
126 for (k
= 0; k
< bytesPerPixel
; k
++) {
130 src
[srcRowStride
+ bytesPerPixel
] + 2) / 4;
134 src
+= bytesPerPixel
;
143 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
144 texImage
->TexFormat
, dstImage
,
145 0, 0, 0, /* dstX/Y/Zoffset */
148 dstWidth
, dstHeight
, 1,
149 GL_BGRA
, CHAN_TYPE
, dst
, &ctx
->DefaultPacking
);
180 * Compute various texture image parameters.
181 * Input: w, h - source texture width and height
182 * Output: lodlevel - Glide lod level token for the larger texture dimension
183 * aspectratio - Glide aspect ratio token
184 * sscale - S scale factor used during triangle setup
185 * tscale - T scale factor used during triangle setup
186 * wscale - OpenGL -> Glide image width scale factor
187 * hscale - OpenGL -> Glide image height scale factor
190 * w h lodlevel aspectRatio
191 * 128 128 GR_LOD_LOG2_128 (=7) GR_ASPECT_LOG2_1x1 (=0)
192 * 64 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x1 (=0)
193 * 64 32 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_2x1 (=1)
194 * 32 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x2 (=-1)
195 * 32 32 GR_LOD_LOG2_32 (=5) GR_ASPECT_LOG2_1x1 (=0)
198 tdfxTexGetInfo(const GLcontext
*ctx
, int w
, int h
,
199 GrLOD_t
*lodlevel
, GrAspectRatio_t
*aspectratio
,
200 float *sscale
, float *tscale
,
201 int *wscale
, int *hscale
)
203 int logw
, logh
, ar
, lod
, ws
, hs
;
211 ar
= logw
- logh
; /* aspect ratio = difference in log dimensions */
215 /* Hardware only allows a maximum aspect ratio of 8x1, so handle
216 |ar| > 3 by scaling the image and using an 8x1 aspect ratio */
220 if (ar
<= GR_ASPECT_LOG2_8x1
) {
224 /* have to stretch image height */
227 ar
= GR_ASPECT_LOG2_8x1
;
233 if (ar
>= GR_ASPECT_LOG2_1x8
) {
237 /* have to stretch image width */
240 ar
= GR_ASPECT_LOG2_1x8
;
245 *lodlevel
= (GrLOD_t
) lod
;
247 *aspectratio
= (GrAspectRatio_t
) ar
;
260 * We need to call this when a texture object's minification filter
261 * or texture image sizes change.
263 static void RevalidateTexture(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
265 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
271 minl
= maxl
= tObj
->BaseLevel
;
273 if (tObj
->Image
[0][minl
]) {
274 maxl
= MIN2(tObj
->MaxLevel
, tObj
->Image
[0][minl
]->MaxLog2
);
276 /* compute largeLodLog2, aspect ratio and texcoord scale factors */
277 tdfxTexGetInfo(ctx
, tObj
->Image
[0][minl
]->Width
, tObj
->Image
[0][minl
]->Height
,
278 &ti
->info
.largeLodLog2
,
279 &ti
->info
.aspectRatioLog2
,
280 &(ti
->sScale
), &(ti
->tScale
), NULL
, NULL
);
283 if (tObj
->Image
[0][maxl
] && (tObj
->MinFilter
!= GL_NEAREST
) && (tObj
->MinFilter
!= GL_LINEAR
)) {
284 /* mipmapping: need to compute smallLodLog2 */
285 tdfxTexGetInfo(ctx
, tObj
->Image
[0][maxl
]->Width
,
286 tObj
->Image
[0][maxl
]->Height
,
287 &ti
->info
.smallLodLog2
, NULL
,
288 NULL
, NULL
, NULL
, NULL
);
291 /* not mipmapping: smallLodLog2 = largeLodLog2 */
292 ti
->info
.smallLodLog2
= ti
->info
.largeLodLog2
;
298 ti
->info
.data
= NULL
;
300 /* this is necessary because of fxDDCompressedTexImage2D */
302 struct gl_texture_image
*texImage
= tObj
->Image
[0][minl
];
303 tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
304 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
305 ti
->sScale
/= mml
->wScale
;
306 ti
->tScale
/= mml
->hScale
;
313 fxAllocTexObjData(tdfxContextPtr fxMesa
)
317 if (!(ti
= CALLOC(sizeof(tdfxTexInfo
)))) {
318 _mesa_problem(NULL
, "tdfx driver: out of memory");
322 ti
->isInTM
= GL_FALSE
;
324 ti
->whichTMU
= TDFX_TMU_NONE
;
326 ti
->tm
[TDFX_TMU0
] = NULL
;
327 ti
->tm
[TDFX_TMU1
] = NULL
;
329 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
330 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
332 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
333 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
335 ti
->mmMode
= GR_MIPMAP_NEAREST
;
336 ti
->LODblend
= FXFALSE
;
343 * Called via glBindTexture.
346 tdfxBindTexture(GLcontext
* ctx
, GLenum target
,
347 struct gl_texture_object
*tObj
)
349 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
352 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
353 fprintf(stderr
, "fxmesa: fxDDTexBind(%d,%p)\n", tObj
->Name
,
357 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
360 if (!tObj
->DriverData
) {
361 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
364 ti
= TDFX_TEXTURE_DATA(tObj
);
365 ti
->lastTimeUsed
= fxMesa
->texBindNumber
++;
367 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
372 * Called via glTexEnv.
375 tdfxTexEnv(GLcontext
* ctx
, GLenum target
, GLenum pname
,
376 const GLfloat
* param
)
378 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
380 if ( TDFX_DEBUG
& DEBUG_VERBOSE_API
) {
382 fprintf(stderr
, "fxmesa: texenv(%x,%x)\n", pname
,
385 fprintf(stderr
, "fxmesa: texenv(%x)\n", pname
);
388 /* XXX this is a bit of a hack to force the Glide texture
389 * state to be updated.
391 fxMesa
->TexState
.EnvMode
[ctx
->Texture
.CurrentUnit
] = 0;
393 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
398 * Called via glTexParameter.
401 tdfxTexParameter(GLcontext
* ctx
, GLenum target
,
402 struct gl_texture_object
*tObj
,
403 GLenum pname
, const GLfloat
* params
)
405 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
406 GLenum param
= (GLenum
) (GLint
) params
[0];
409 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
410 fprintf(stderr
, "fxmesa: fxDDTexParam(%d,%p,%x,%x)\n", tObj
->Name
,
411 tObj
->DriverData
, pname
, param
);
414 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
417 if (!tObj
->DriverData
)
418 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
420 ti
= TDFX_TEXTURE_DATA(tObj
);
423 case GL_TEXTURE_MIN_FILTER
:
426 ti
->mmMode
= GR_MIPMAP_DISABLE
;
427 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
428 ti
->LODblend
= FXFALSE
;
431 ti
->mmMode
= GR_MIPMAP_DISABLE
;
432 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
433 ti
->LODblend
= FXFALSE
;
435 case GL_NEAREST_MIPMAP_LINEAR
:
436 if (!fxMesa
->Glide
.HaveCombineExt
) {
437 if (fxMesa
->haveTwoTMUs
) {
438 ti
->mmMode
= GR_MIPMAP_NEAREST
;
439 ti
->LODblend
= FXTRUE
;
442 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
443 ti
->LODblend
= FXFALSE
;
445 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
448 /* XXX Voodoo3/Banshee mipmap blending seems to produce
449 * incorrectly filtered colors for the smallest mipmap levels.
450 * To work-around we fall-through here and use a different filter.
452 case GL_NEAREST_MIPMAP_NEAREST
:
453 ti
->mmMode
= GR_MIPMAP_NEAREST
;
454 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
455 ti
->LODblend
= FXFALSE
;
457 case GL_LINEAR_MIPMAP_LINEAR
:
458 if (!fxMesa
->Glide
.HaveCombineExt
) {
459 if (fxMesa
->haveTwoTMUs
) {
460 ti
->mmMode
= GR_MIPMAP_NEAREST
;
461 ti
->LODblend
= FXTRUE
;
464 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
465 ti
->LODblend
= FXFALSE
;
467 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
470 /* XXX Voodoo3/Banshee mipmap blending seems to produce
471 * incorrectly filtered colors for the smallest mipmap levels.
472 * To work-around we fall-through here and use a different filter.
474 case GL_LINEAR_MIPMAP_NEAREST
:
475 ti
->mmMode
= GR_MIPMAP_NEAREST
;
476 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
477 ti
->LODblend
= FXFALSE
;
482 ti
->reloadImages
= GL_TRUE
;
483 RevalidateTexture(ctx
, tObj
);
484 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
487 case GL_TEXTURE_MAG_FILTER
:
490 ti
->magFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
493 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
498 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
501 case GL_TEXTURE_WRAP_S
:
503 case GL_CLAMP_TO_BORDER
:
504 case GL_CLAMP_TO_EDGE
:
506 ti
->sClamp
= GR_TEXTURECLAMP_CLAMP
;
509 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
511 case GL_MIRRORED_REPEAT
:
512 ti
->sClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
517 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
520 case GL_TEXTURE_WRAP_T
:
522 case GL_CLAMP_TO_BORDER
:
523 case GL_CLAMP_TO_EDGE
:
525 ti
->tClamp
= GR_TEXTURECLAMP_CLAMP
;
528 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
530 case GL_MIRRORED_REPEAT
:
531 ti
->tClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
536 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
539 case GL_TEXTURE_BORDER_COLOR
:
542 case GL_TEXTURE_MIN_LOD
:
545 case GL_TEXTURE_MAX_LOD
:
548 case GL_TEXTURE_BASE_LEVEL
:
549 RevalidateTexture(ctx
, tObj
);
551 case GL_TEXTURE_MAX_LEVEL
:
552 RevalidateTexture(ctx
, tObj
);
562 * Called via glDeleteTextures to delete a texture object.
563 * Here, we delete the Glide data associated with the texture.
566 tdfxDeleteTexture(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
568 if (ctx
&& ctx
->DriverCtx
) {
569 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
570 tdfxTMFreeTexture(fxMesa
, tObj
);
571 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
572 /* Free mipmap images and the texture object itself */
573 _mesa_delete_texture_object(ctx
, tObj
);
579 * Return true if texture is resident, false otherwise.
582 tdfxIsTextureResident(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
584 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
585 return (GLboolean
) (ti
&& ti
->isInTM
);
591 * Convert a gl_color_table texture palette to Glide's format.
594 convertPalette(FxU32 data
[256], const struct gl_color_table
*table
)
596 const GLubyte
*tableUB
= (const GLubyte
*) table
->Table
;
597 GLint width
= table
->Size
;
601 ASSERT(table
->Type
== GL_UNSIGNED_BYTE
);
603 switch (table
->_BaseFormat
) {
605 for (i
= 0; i
< width
; i
++) {
610 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
612 return GR_TEXTABLE_PALETTE_6666_EXT
;
614 for (i
= 0; i
< width
; i
++) {
619 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
621 return GR_TEXTABLE_PALETTE
;
623 for (i
= 0; i
< width
; i
++) {
626 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
628 return GR_TEXTABLE_PALETTE_6666_EXT
;
629 case GL_LUMINANCE_ALPHA
:
630 for (i
= 0; i
< width
; i
++) {
631 r
= g
= b
= tableUB
[i
* 2 + 0];
632 a
= tableUB
[i
* 2 + 1];
633 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
635 return GR_TEXTABLE_PALETTE_6666_EXT
;
637 for (i
= 0; i
< width
; i
++) {
638 r
= tableUB
[i
* 3 + 0];
639 g
= tableUB
[i
* 3 + 1];
640 b
= tableUB
[i
* 3 + 2];
642 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
644 return GR_TEXTABLE_PALETTE
;
646 for (i
= 0; i
< width
; i
++) {
647 r
= tableUB
[i
* 4 + 0];
648 g
= tableUB
[i
* 4 + 1];
649 b
= tableUB
[i
* 4 + 2];
650 a
= tableUB
[i
* 4 + 3];
651 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
653 return GR_TEXTABLE_PALETTE_6666_EXT
;
655 /* XXX fixme: how can this happen? */
656 _mesa_error(NULL
, GL_INVALID_ENUM
, "convertPalette: table->Format == %s",
657 _mesa_lookup_enum_by_nr(table
->Format
));
658 return GR_TEXTABLE_PALETTE
;
665 tdfxUpdateTexturePalette(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
667 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
670 /* per-texture palette */
673 /* This might be a proxy texture. */
674 if (!tObj
->Palette
.Table
)
677 if (!tObj
->DriverData
)
678 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
679 ti
= TDFX_TEXTURE_DATA(tObj
);
681 ti
->paltype
= convertPalette(ti
->palette
.data
, &tObj
->Palette
);
682 /*tdfxTexInvalidate(ctx, tObj);*/
685 /* global texture palette */
686 fxMesa
->TexPalette
.Type
= convertPalette(fxMesa
->glbPalette
.data
, &ctx
->Texture
.Palette
);
687 fxMesa
->TexPalette
.Data
= &(fxMesa
->glbPalette
.data
);
688 fxMesa
->dirty
|= TDFX_UPLOAD_TEXTURE_PALETTE
;
690 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX too heavy-handed */
694 /**********************************************************************/
695 /**** NEW TEXTURE IMAGE FUNCTIONS ****/
696 /**********************************************************************/
699 static FxBool TexusFatalError
= FXFALSE
;
700 static FxBool TexusError
= FXFALSE
;
702 #define TX_DITHER_NONE 0x00000000
705 fxTexusError(const char *string
, FxBool fatal
)
707 _mesa_problem(NULL
, string
);
709 * Just propagate the fatal value up.
712 TexusFatalError
= fatal
;
717 static const struct gl_texture_format
*
718 tdfxChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
719 GLenum srcFormat
, GLenum srcType
)
721 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
722 const GLboolean allow32bpt
= TDFX_IS_NAPALM(fxMesa
);
724 switch (internalFormat
) {
730 case GL_COMPRESSED_ALPHA
:
731 return &_mesa_texformat_a8
;
738 case GL_COMPRESSED_LUMINANCE
:
739 return &_mesa_texformat_l8
;
741 case GL_LUMINANCE_ALPHA
:
742 case GL_LUMINANCE4_ALPHA4
:
743 case GL_LUMINANCE6_ALPHA2
:
744 case GL_LUMINANCE8_ALPHA8
:
745 case GL_LUMINANCE12_ALPHA4
:
746 case GL_LUMINANCE12_ALPHA12
:
747 case GL_LUMINANCE16_ALPHA16
:
748 case GL_COMPRESSED_LUMINANCE_ALPHA
:
749 return &_mesa_texformat_al88
;
755 case GL_COMPRESSED_INTENSITY
:
756 return &_mesa_texformat_i8
;
760 return &_mesa_texformat_rgb565
;
761 case GL_COMPRESSED_RGB
:
762 /* intentional fall-through */
765 if ( srcFormat
== GL_RGB
&& srcType
== GL_UNSIGNED_SHORT_5_6_5
) {
766 return &_mesa_texformat_rgb565
;
768 /* intentional fall through */
773 return (allow32bpt
) ? &_mesa_texformat_argb8888
774 : &_mesa_texformat_rgb565
;
777 return &_mesa_texformat_argb4444
;
778 case GL_COMPRESSED_RGBA
:
779 /* intentional fall-through */
782 if ( srcFormat
== GL_BGRA
) {
783 if ( srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
784 return &_mesa_texformat_argb8888
;
786 else if ( srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
787 return &_mesa_texformat_argb4444
;
789 else if ( srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
790 return &_mesa_texformat_argb1555
;
793 /* intentional fall through */
798 return allow32bpt
? &_mesa_texformat_argb8888
799 : &_mesa_texformat_argb4444
;
801 return &_mesa_texformat_argb1555
;
803 case GL_COLOR_INDEX1_EXT
:
804 case GL_COLOR_INDEX2_EXT
:
805 case GL_COLOR_INDEX4_EXT
:
806 case GL_COLOR_INDEX8_EXT
:
807 case GL_COLOR_INDEX12_EXT
:
808 case GL_COLOR_INDEX16_EXT
:
809 return &_mesa_texformat_ci8
;
810 /* GL_EXT_texture_compression_s3tc */
812 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
815 return &_mesa_texformat_rgb_dxt1
;
816 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
817 return &_mesa_texformat_rgba_dxt1
;
818 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
821 return &_mesa_texformat_rgba_dxt3
;
822 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
823 return &_mesa_texformat_rgba_dxt5
;
824 /* GL_3DFX_texture_compression_FXT1 */
825 case GL_COMPRESSED_RGB_FXT1_3DFX
:
826 return &_mesa_texformat_rgb_fxt1
;
827 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
828 return &_mesa_texformat_rgba_fxt1
;
830 _mesa_problem(ctx
, "unexpected format in tdfxChooseTextureFormat");
837 * Return the Glide format for the given mesa texture format.
839 static GrTextureFormat_t
840 fxGlideFormat(GLint mesaFormat
)
842 switch (mesaFormat
) {
844 return GR_TEXFMT_ALPHA_8
;
846 return GR_TEXFMT_ALPHA_8
;
848 return GR_TEXFMT_INTENSITY_8
;
849 case MESA_FORMAT_CI8
:
850 return GR_TEXFMT_P_8
;
851 case MESA_FORMAT_AL88
:
852 return GR_TEXFMT_ALPHA_INTENSITY_88
;
853 case MESA_FORMAT_RGB565
:
854 return GR_TEXFMT_RGB_565
;
855 case MESA_FORMAT_ARGB4444
:
856 return GR_TEXFMT_ARGB_4444
;
857 case MESA_FORMAT_ARGB1555
:
858 return GR_TEXFMT_ARGB_1555
;
859 case MESA_FORMAT_ARGB8888
:
860 return GR_TEXFMT_ARGB_8888
;
861 case MESA_FORMAT_RGB_FXT1
:
862 case MESA_FORMAT_RGBA_FXT1
:
863 return GR_TEXFMT_ARGB_CMP_FXT1
;
864 case MESA_FORMAT_RGB_DXT1
:
865 case MESA_FORMAT_RGBA_DXT1
:
866 return GR_TEXFMT_ARGB_CMP_DXT1
;
867 case MESA_FORMAT_RGBA_DXT3
:
868 return GR_TEXFMT_ARGB_CMP_DXT3
;
869 case MESA_FORMAT_RGBA_DXT5
:
870 return GR_TEXFMT_ARGB_CMP_DXT5
;
872 _mesa_problem(NULL
, "Unexpected format in fxGlideFormat");
878 /* Texel-fetch functions for software texturing and glGetTexImage().
879 * We should have been able to use some "standard" fetch functions (which
880 * may get defined in texutil.c) but we have to account for scaled texture
881 * images on tdfx hardware (the 8:1 aspect ratio limit).
882 * Hence, we need special functions here.
885 fxt1_decode_1 (const void *texture
, int width
,
886 int i
, int j
, unsigned char *rgba
);
889 fetch_intensity8(const struct gl_texture_image
*texImage
,
890 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
892 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
893 const GLubyte
*texel
;
898 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
899 rgba
[RCOMP
] = *texel
;
900 rgba
[GCOMP
] = *texel
;
901 rgba
[BCOMP
] = *texel
;
902 rgba
[ACOMP
] = *texel
;
907 fetch_luminance8(const struct gl_texture_image
*texImage
,
908 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
910 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
911 const GLubyte
*texel
;
916 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
917 rgba
[RCOMP
] = *texel
;
918 rgba
[GCOMP
] = *texel
;
919 rgba
[BCOMP
] = *texel
;
925 fetch_alpha8(const struct gl_texture_image
*texImage
,
926 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
928 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
929 const GLubyte
*texel
;
934 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
938 rgba
[ACOMP
] = *texel
;
943 fetch_index8(const struct gl_texture_image
*texImage
,
944 GLint i
, GLint j
, GLint k
, GLchan
* indexOut
)
946 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
947 const GLubyte
*texel
;
952 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
958 fetch_luminance8_alpha8(const struct gl_texture_image
*texImage
,
959 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
961 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
962 const GLubyte
*texel
;
967 texel
= ((GLubyte
*) texImage
->Data
) + (j
* mml
->width
+ i
) * 2;
968 rgba
[RCOMP
] = texel
[0];
969 rgba
[GCOMP
] = texel
[0];
970 rgba
[BCOMP
] = texel
[0];
971 rgba
[ACOMP
] = texel
[1];
976 fetch_r5g6b5(const struct gl_texture_image
*texImage
,
977 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
979 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
980 const GLushort
*texel
;
985 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
986 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
987 rgba
[GCOMP
] = (((*texel
) >> 5) & 0x3f) * 255 / 63;
988 rgba
[BCOMP
] = (((*texel
) >> 0) & 0x1f) * 255 / 31;
994 fetch_r4g4b4a4(const struct gl_texture_image
*texImage
,
995 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
997 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
998 const GLushort
*texel
;
1000 i
= i
* mml
->wScale
;
1001 j
= j
* mml
->hScale
;
1003 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1004 rgba
[RCOMP
] = (((*texel
) >> 12) & 0xf) * 255 / 15;
1005 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xf) * 255 / 15;
1006 rgba
[BCOMP
] = (((*texel
) >> 4) & 0xf) * 255 / 15;
1007 rgba
[ACOMP
] = (((*texel
) >> 0) & 0xf) * 255 / 15;
1012 fetch_r5g5b5a1(const struct gl_texture_image
*texImage
,
1013 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1015 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1016 const GLushort
*texel
;
1018 i
= i
* mml
->wScale
;
1019 j
= j
* mml
->hScale
;
1021 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1022 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
1023 rgba
[GCOMP
] = (((*texel
) >> 6) & 0x1f) * 255 / 31;
1024 rgba
[BCOMP
] = (((*texel
) >> 1) & 0x1f) * 255 / 31;
1025 rgba
[ACOMP
] = (((*texel
) >> 0) & 0x01) * 255;
1030 fetch_a8r8g8b8(const struct gl_texture_image
*texImage
,
1031 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1033 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1034 const GLuint
*texel
;
1036 i
= i
* mml
->wScale
;
1037 j
= j
* mml
->hScale
;
1039 texel
= ((GLuint
*) texImage
->Data
) + j
* mml
->width
+ i
;
1040 rgba
[RCOMP
] = (((*texel
) >> 16) & 0xff);
1041 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xff);
1042 rgba
[BCOMP
] = (((*texel
) ) & 0xff);
1043 rgba
[ACOMP
] = (((*texel
) >> 24) & 0xff);
1048 fetch_rgb_fxt1(const struct gl_texture_image
*texImage
,
1049 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1051 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1053 i
= i
* mml
->wScale
;
1054 j
= j
* mml
->hScale
;
1056 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1062 fetch_rgba_fxt1(const struct gl_texture_image
*texImage
,
1063 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1065 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1067 i
= i
* mml
->wScale
;
1068 j
= j
* mml
->hScale
;
1070 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1075 fetch_rgb_dxt1(const struct gl_texture_image
*texImage
,
1076 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1078 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1080 i
= i
* mml
->wScale
;
1081 j
= j
* mml
->hScale
;
1083 _mesa_texformat_rgb_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1088 fetch_rgba_dxt1(const struct gl_texture_image
*texImage
,
1089 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1091 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1093 i
= i
* mml
->wScale
;
1094 j
= j
* mml
->hScale
;
1096 _mesa_texformat_rgba_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1101 fetch_rgba_dxt3(const struct gl_texture_image
*texImage
,
1102 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1104 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1106 i
= i
* mml
->wScale
;
1107 j
= j
* mml
->hScale
;
1109 _mesa_texformat_rgba_dxt3
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1114 fetch_rgba_dxt5(const struct gl_texture_image
*texImage
,
1115 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1117 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1119 i
= i
* mml
->wScale
;
1120 j
= j
* mml
->hScale
;
1122 _mesa_texformat_rgba_dxt5
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1126 static FetchTexelFuncC
1127 fxFetchFunction(GLint mesaFormat
)
1129 switch (mesaFormat
) {
1130 case MESA_FORMAT_I8
:
1131 return &fetch_intensity8
;
1132 case MESA_FORMAT_A8
:
1133 return &fetch_alpha8
;
1134 case MESA_FORMAT_L8
:
1135 return &fetch_luminance8
;
1136 case MESA_FORMAT_CI8
:
1137 return &fetch_index8
;
1138 case MESA_FORMAT_AL88
:
1139 return &fetch_luminance8_alpha8
;
1140 case MESA_FORMAT_RGB565
:
1141 return &fetch_r5g6b5
;
1142 case MESA_FORMAT_ARGB4444
:
1143 return &fetch_r4g4b4a4
;
1144 case MESA_FORMAT_ARGB1555
:
1145 return &fetch_r5g5b5a1
;
1146 case MESA_FORMAT_ARGB8888
:
1147 return &fetch_a8r8g8b8
;
1148 case MESA_FORMAT_RGB_FXT1
:
1149 return &fetch_rgb_fxt1
;
1150 case MESA_FORMAT_RGBA_FXT1
:
1151 return &fetch_rgba_fxt1
;
1152 case MESA_FORMAT_RGB_DXT1
:
1153 return &fetch_rgb_dxt1
;
1154 case MESA_FORMAT_RGBA_DXT1
:
1155 return &fetch_rgba_dxt1
;
1156 case MESA_FORMAT_RGBA_DXT3
:
1157 return &fetch_rgba_dxt3
;
1158 case MESA_FORMAT_RGBA_DXT5
:
1159 return &fetch_rgba_dxt5
;
1161 _mesa_problem(NULL
, "Unexpected format in fxFetchFunction");
1168 adjust2DRatio (GLcontext
*ctx
,
1169 GLint xoffset
, GLint yoffset
,
1170 GLint width
, GLint height
,
1171 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1172 const struct gl_pixelstore_attrib
*packing
,
1173 tdfxMipMapLevel
*mml
,
1174 struct gl_texture_image
*texImage
,
1178 const GLint newWidth
= width
* mml
->wScale
;
1179 const GLint newHeight
= height
* mml
->hScale
;
1181 GLuint dstImageOffsets
= 0;
1183 if (!texImage
->IsCompressed
) {
1185 tempImage
= MALLOC(width
* height
* texelBytes
);
1190 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1191 texImage
->TexFormat
, tempImage
,
1192 0, 0, 0, /* dstX/Y/Zoffset */
1193 width
* texelBytes
, /* dstRowStride */
1196 format
, type
, pixels
, packing
);
1199 /* compute address of dest subimage within the overal tex image */
1200 destAddr
= (GLubyte
*) texImage
->Data
1201 + (yoffset
* mml
->hScale
* mml
->width
1202 + xoffset
* mml
->wScale
) * texelBytes
;
1204 _mesa_rescale_teximage2d(texelBytes
,
1206 dstRowStride
, /* dst stride */
1208 newWidth
, newHeight
,
1209 tempImage
, destAddr
);
1211 const GLint rawBytes
= 4;
1212 GLvoid
*rawImage
= MALLOC(width
* height
* rawBytes
);
1216 tempImage
= MALLOC(newWidth
* newHeight
* rawBytes
);
1221 /* unpack image, apply transfer ops and store in rawImage */
1222 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
1223 &_mesa_texformat_rgba8888_rev
, rawImage
,
1224 0, 0, 0, /* dstX/Y/Zoffset */
1225 width
* rawBytes
, /* dstRowStride */
1228 format
, type
, pixels
, packing
);
1229 _mesa_rescale_teximage2d(rawBytes
,
1231 newWidth
* rawBytes
, /* dst stride */
1232 width
, height
, /* src */
1233 newWidth
, newHeight
, /* dst */
1234 rawImage
/*src*/, tempImage
/*dst*/ );
1235 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1236 texImage
->TexFormat
, texImage
->Data
,
1237 xoffset
* mml
->wScale
, yoffset
* mml
->hScale
, 0, /* dstX/Y/Zoffset */
1240 newWidth
, newHeight
, 1,
1241 GL_RGBA
, CHAN_TYPE
, tempImage
, &ctx
->DefaultPacking
);
1252 tdfxTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1253 GLint internalFormat
, GLint width
, GLint height
, GLint border
,
1254 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1255 const struct gl_pixelstore_attrib
*packing
,
1256 struct gl_texture_object
*texObj
,
1257 struct gl_texture_image
*texImage
)
1259 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1261 tdfxMipMapLevel
*mml
;
1262 GLint texelBytes
, dstRowStride
;
1266 printf("TexImage id=%d int 0x%x format 0x%x type 0x%x %dx%d\n",
1267 texObj->Name, texImage->InternalFormat, format, type,
1268 texImage->Width, texImage->Height);
1271 ti
= TDFX_TEXTURE_DATA(texObj
);
1273 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1274 if (!texObj
->DriverData
) {
1275 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1278 ti
= TDFX_TEXTURE_DATA(texObj
);
1282 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1284 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1285 if (!texImage
->DriverData
) {
1286 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1289 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1292 /* Determine width and height scale factors for texture.
1293 * Remember, Glide is limited to 8:1 aspect ratios.
1296 texImage
->Width
, texImage
->Height
,
1297 NULL
, /* lod level */
1298 NULL
, /* aspect ratio */
1299 NULL
, NULL
, /* sscale, tscale */
1300 &mml
->wScale
, &mml
->hScale
);
1302 /* rescaled size: */
1303 mml
->width
= width
* mml
->wScale
;
1304 mml
->height
= height
* mml
->hScale
;
1306 #if FX_COMPRESS_S3TC_AS_FXT1_HACK
1307 /* [koolsmoky] substitute FXT1 for DXTn and Legacy S3TC */
1308 /* [dBorca] we should update texture's attribute, then,
1309 * because if the application asks us to decompress, we
1310 * have to know the REAL format! Also, DXT3/5 might not
1311 * be correct, since it would mess with "compressedSize".
1312 * Ditto for GL_RGBA[4]_S3TC, which is always mapped to DXT3.
1314 if (texImage
->IsCompressed
) {
1315 switch (internalFormat
) {
1316 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1319 internalFormat
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1321 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1322 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1323 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1326 internalFormat
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1328 texImage
->InternalFormat
= internalFormat
;
1332 if (fxMesa
->type
>= GR_SSTTYPE_Voodoo4
) {
1333 GLenum texNapalm
= 0;
1334 if (internalFormat
== GL_COMPRESSED_RGB
) {
1335 texNapalm
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1336 } else if (internalFormat
== GL_COMPRESSED_RGBA
) {
1337 texNapalm
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1340 texImage
->InternalFormat
= internalFormat
= texNapalm
;
1341 texImage
->IsCompressed
= GL_TRUE
;
1346 /* choose the texture format */
1347 assert(ctx
->Driver
.ChooseTextureFormat
);
1348 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1349 internalFormat
, format
, type
);
1350 assert(texImage
->TexFormat
);
1351 mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1352 mml
->glideFormat
= fxGlideFormat(mesaFormat
);
1353 ti
->info
.format
= mml
->glideFormat
;
1354 texImage
->FetchTexelc
= fxFetchFunction(mesaFormat
);
1355 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1357 if (texImage
->IsCompressed
) {
1358 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1363 dstRowStride
= _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, mml
->width
);
1364 texImage
->Data
= _mesa_alloc_texmemory(texImage
->CompressedSize
);
1366 dstRowStride
= mml
->width
* texelBytes
;
1367 texImage
->Data
= _mesa_alloc_texmemory(mml
->width
* mml
->height
* texelBytes
);
1369 if (!texImage
->Data
) {
1370 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1374 if (pixels
!= NULL
) {
1375 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1376 /* rescale image to overcome 1:8 aspect limitation */
1377 if (!adjust2DRatio(ctx
,
1380 format
, type
, pixels
,
1387 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1392 /* no rescaling needed */
1393 /* unpack image, apply transfer ops and store in texImage->Data */
1394 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1395 texImage
->TexFormat
, texImage
->Data
,
1396 0, 0, 0, /* dstX/Y/Zoffset */
1398 texImage
->ImageOffsets
,
1400 format
, type
, pixels
, packing
);
1403 /* GL_SGIS_generate_mipmap */
1404 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1405 GLint mipWidth
, mipHeight
;
1406 tdfxMipMapLevel
*mip
;
1407 struct gl_texture_image
*mipImage
;
1408 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1409 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1411 assert(!texImage
->IsCompressed
);
1413 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
1414 mipWidth
= width
/ 2;
1418 mipHeight
= height
/ 2;
1422 if ((mipWidth
== width
) && (mipHeight
== height
)) {
1425 _mesa_TexImage2D(target
, ++level
, internalFormat
,
1426 mipWidth
, mipHeight
, border
,
1429 mipImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1430 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
1431 _mesa_halve2x2_teximage2d(ctx
,
1434 mml
->width
, mml
->height
,
1435 texImage
->Data
, mipImage
->Data
);
1436 texImage
= mipImage
;
1444 RevalidateTexture(ctx
, texObj
);
1446 ti
->reloadImages
= GL_TRUE
;
1447 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1452 tdfxTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1453 GLint xoffset
, GLint yoffset
,
1454 GLsizei width
, GLsizei height
,
1455 GLenum format
, GLenum type
,
1456 const GLvoid
*pixels
,
1457 const struct gl_pixelstore_attrib
*packing
,
1458 struct gl_texture_object
*texObj
,
1459 struct gl_texture_image
*texImage
)
1461 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1463 tdfxMipMapLevel
*mml
;
1464 GLint texelBytes
, dstRowStride
;
1466 if (!texObj
->DriverData
) {
1467 _mesa_problem(ctx
, "problem in fxDDTexSubImage2D");
1471 ti
= TDFX_TEXTURE_DATA(texObj
);
1473 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1476 assert(texImage
->Data
); /* must have an existing texture image! */
1477 assert(texImage
->_BaseFormat
);
1479 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1480 if (texImage
->IsCompressed
) {
1481 dstRowStride
= _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, mml
->width
);
1483 dstRowStride
= mml
->width
* texelBytes
;
1486 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1487 /* need to rescale subimage to match mipmap level's rescale factors */
1488 if (!adjust2DRatio(ctx
,
1491 format
, type
, pixels
,
1498 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
1503 /* no rescaling needed */
1504 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1505 texImage
->TexFormat
, texImage
->Data
,
1506 xoffset
, yoffset
, 0,
1508 texImage
->ImageOffsets
,
1510 format
, type
, pixels
, packing
);
1513 /* GL_SGIS_generate_mipmap */
1514 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1515 GLint mipWidth
, mipHeight
;
1516 tdfxMipMapLevel
*mip
;
1517 struct gl_texture_image
*mipImage
;
1518 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1519 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1521 assert(!texImage
->IsCompressed
);
1523 width
= texImage
->Width
;
1524 height
= texImage
->Height
;
1525 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
1526 mipWidth
= width
/ 2;
1530 mipHeight
= height
/ 2;
1534 if ((mipWidth
== width
) && (mipHeight
== height
)) {
1538 mipImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1539 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
1540 _mesa_halve2x2_teximage2d(ctx
,
1543 mml
->width
, mml
->height
,
1544 texImage
->Data
, mipImage
->Data
);
1545 texImage
= mipImage
;
1552 ti
->reloadImages
= GL_TRUE
; /* signal the image needs to be reloaded */
1553 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX this might be a bit much */
1558 tdfxTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1559 GLint internalFormat
, GLint width
, GLint border
,
1560 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1561 const struct gl_pixelstore_attrib
*packing
,
1562 struct gl_texture_object
*texObj
,
1563 struct gl_texture_image
*texImage
)
1565 tdfxTexImage2D(ctx
, target
, level
,
1566 internalFormat
, width
, 1, border
,
1567 format
, type
, pixels
,
1574 tdfxTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1577 GLenum format
, GLenum type
,
1578 const GLvoid
*pixels
,
1579 const struct gl_pixelstore_attrib
*packing
,
1580 struct gl_texture_object
*texObj
,
1581 struct gl_texture_image
*texImage
)
1583 tdfxTexSubImage2D(ctx
, target
, level
,
1593 /**********************************************************************/
1594 /**** COMPRESSED TEXTURE IMAGE FUNCTIONS ****/
1595 /**********************************************************************/
1598 tdfxCompressedTexImage2D (GLcontext
*ctx
, GLenum target
,
1599 GLint level
, GLint internalFormat
,
1600 GLsizei width
, GLsizei height
, GLint border
,
1601 GLsizei imageSize
, const GLvoid
*data
,
1602 struct gl_texture_object
*texObj
,
1603 struct gl_texture_image
*texImage
)
1605 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1607 tdfxMipMapLevel
*mml
;
1610 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1611 fprintf(stderr
, "tdfxCompressedTexImage2D: id=%d int 0x%x %dx%d\n",
1612 texObj
->Name
, internalFormat
,
1616 if ((target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_2D
) || texImage
->Border
> 0) {
1617 _mesa_problem(NULL
, "tdfx: unsupported texture in tdfxCompressedTexImg()\n");
1621 assert(texImage
->IsCompressed
);
1623 ti
= TDFX_TEXTURE_DATA(texObj
);
1625 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1626 if (!texObj
->DriverData
) {
1627 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1630 ti
= TDFX_TEXTURE_DATA(texObj
);
1634 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1636 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1637 if (!texImage
->DriverData
) {
1638 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1641 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1644 tdfxTexGetInfo(ctx
, width
, height
, NULL
, NULL
, NULL
, NULL
,
1645 &mml
->wScale
, &mml
->hScale
);
1647 mml
->width
= width
* mml
->wScale
;
1648 mml
->height
= height
* mml
->hScale
;
1651 /* choose the texture format */
1652 assert(ctx
->Driver
.ChooseTextureFormat
);
1653 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1654 internalFormat
, -1/*format*/, -1/*type*/);
1655 assert(texImage
->TexFormat
);
1657 /* Determine the appropriate Glide texel format,
1658 * given the user's internal texture format hint.
1660 mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1661 mml
->glideFormat
= fxGlideFormat(mesaFormat
);
1662 ti
->info
.format
= mml
->glideFormat
;
1663 texImage
->FetchTexelc
= fxFetchFunction(mesaFormat
);
1665 /* allocate new storage for texture image, if needed */
1666 if (!texImage
->Data
) {
1667 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1672 texImage
->Data
= _mesa_alloc_texmemory(texImage
->CompressedSize
);
1673 if (!texImage
->Data
) {
1674 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1679 /* save the texture data */
1680 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1681 /* [dBorca] Hack alert:
1682 * now we're screwed. We can't decompress,
1683 * unless we do it in HW (via textureBuffer).
1684 * We still have some chances:
1685 * 1) we got FXT1 textures - we CAN decompress, rescale for
1686 * aspectratio, then compress back.
1687 * 2) there is a chance that MIN("s", "t") won't be overflowed.
1688 * Thus, we don't care about textureclamp and we could lower
1689 * MIN("uscale", "vscale") below 32. We still have to have
1690 * our data aligned inside a 8:1 rectangle.
1691 * 3) just in case if MIN("s", "t") gets overflowed with GL_REPEAT,
1692 * we replicate the data over the padded area.
1693 * For now, we take 2) + 3) but texelfetchers will be wrong!
1695 const GLuint mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1696 GLuint srcRowStride
= _mesa_compressed_row_stride(mesaFormat
, width
);
1698 GLuint destRowStride
= _mesa_compressed_row_stride(mesaFormat
,
1701 _mesa_upscale_teximage2d(srcRowStride
, (height
+3) / 4,
1702 destRowStride
, (mml
->height
+3) / 4,
1703 1, data
, srcRowStride
,
1705 ti
->padded
= GL_TRUE
;
1707 MEMCPY(texImage
->Data
, data
, texImage
->CompressedSize
);
1710 /* GL_SGIS_generate_mipmap */
1711 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1712 assert(!texImage
->IsCompressed
);
1715 RevalidateTexture(ctx
, texObj
);
1717 ti
->reloadImages
= GL_TRUE
;
1718 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1723 tdfxCompressedTexSubImage2D( GLcontext
*ctx
, GLenum target
,
1724 GLint level
, GLint xoffset
,
1725 GLint yoffset
, GLsizei width
,
1726 GLint height
, GLenum format
,
1727 GLsizei imageSize
, const GLvoid
*data
,
1728 struct gl_texture_object
*texObj
,
1729 struct gl_texture_image
*texImage
)
1731 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1733 tdfxMipMapLevel
*mml
;
1734 GLint destRowStride
, srcRowStride
;
1737 const GLuint mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1739 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1740 fprintf(stderr
, "tdfxCompressedTexSubImage2D: id=%d\n", texObj
->Name
);
1743 ti
= TDFX_TEXTURE_DATA(texObj
);
1745 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1748 srcRowStride
= _mesa_compressed_row_stride(mesaFormat
, width
);
1750 destRowStride
= _mesa_compressed_row_stride(mesaFormat
, mml
->width
);
1751 dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
1754 (GLubyte
*) texImage
->Data
);
1756 rows
= height
/ 4; /* [dBorca] hardcoded 4, but works for FXT1/DXTC */
1758 for (i
= 0; i
< rows
; i
++) {
1759 MEMCPY(dest
, data
, srcRowStride
);
1760 dest
+= destRowStride
;
1761 data
= (GLvoid
*)((GLuint
)data
+ (GLuint
)srcRowStride
);
1764 /* [dBorca] Hack alert:
1765 * see fxDDCompressedTexImage2D for caveats
1767 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1768 srcRowStride
= _mesa_compressed_row_stride(mesaFormat
, texImage
->Width
);
1770 destRowStride
= _mesa_compressed_row_stride(mesaFormat
, mml
->width
);
1771 _mesa_upscale_teximage2d(srcRowStride
, texImage
->Height
/ 4,
1772 destRowStride
, mml
->height
/ 4,
1773 1, texImage
->Data
, destRowStride
,
1777 /* GL_SGIS_generate_mipmap */
1778 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1779 assert(!texImage
->IsCompressed
);
1782 RevalidateTexture(ctx
, texObj
);
1784 ti
->reloadImages
= GL_TRUE
;
1785 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1791 PrintTexture(int w
, int h
, int c
, const GLubyte
* data
)
1794 for (i
= 0; i
< h
; i
++) {
1795 for (j
= 0; j
< w
; j
++) {
1797 printf("%02x %02x ", data
[0], data
[1]);
1799 printf("%02x %02x %02x ", data
[0], data
[1], data
[2]);
1809 tdfxTestProxyTexImage(GLcontext
*ctx
, GLenum target
,
1810 GLint level
, GLint internalFormat
,
1811 GLenum format
, GLenum type
,
1812 GLint width
, GLint height
,
1813 GLint depth
, GLint border
)
1815 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1816 struct gl_shared_state
*mesaShared
= fxMesa
->glCtx
->Shared
;
1817 struct tdfxSharedState
*shared
= (struct tdfxSharedState
*) mesaShared
->DriverData
;
1820 case GL_PROXY_TEXTURE_1D
:
1822 case GL_PROXY_TEXTURE_2D
:
1824 struct gl_texture_object
*tObj
;
1828 tObj
= ctx
->Texture
.Proxy2D
;
1829 if (!tObj
->DriverData
)
1830 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1831 ti
= TDFX_TEXTURE_DATA(tObj
);
1834 /* assign the parameters to test against */
1835 tObj
->Image
[0][level
]->Width
= width
;
1836 tObj
->Image
[0][level
]->Height
= height
;
1837 tObj
->Image
[0][level
]->Border
= border
;
1839 tObj
->Image
[0][level
]->InternalFormat
= internalFormat
;
1842 /* don't use mipmap levels > 0 */
1843 tObj
->MinFilter
= tObj
->MagFilter
= GL_NEAREST
;
1846 /* test with all mipmap levels */
1847 tObj
->MinFilter
= GL_LINEAR_MIPMAP_LINEAR
;
1848 tObj
->MagFilter
= GL_NEAREST
;
1850 RevalidateTexture(ctx
, tObj
);
1853 printf("small lodlog2 0x%x\n", ti->info.smallLodLog2);
1854 printf("large lodlog2 0x%x\n", ti->info.largeLodLog2);
1855 printf("aspect ratio 0x%x\n", ti->info.aspectRatioLog2);
1856 printf("glide format 0x%x\n", ti->info.format);
1857 printf("data %p\n", ti->info.data);
1858 printf("lodblend %d\n", (int) ti->LODblend);
1861 /* determine where texture will reside */
1862 if (ti
->LODblend
&& !shared
->umaTexMemory
) {
1863 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1864 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1865 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1868 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1869 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1870 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1873 printf("Proxy test %d > %d\n", memNeeded, shared->totalTexMem[0]);
1875 if (memNeeded
> shared
->totalTexMem
[0])
1880 case GL_PROXY_TEXTURE_3D
:
1881 return GL_TRUE
; /* software rendering */
1883 return GL_TRUE
; /* never happens, silence compiler */
1889 * Allocate a new texture object.
1890 * Called via ctx->Driver.NewTextureObject.
1891 * Note: this function will be called during context creation to
1892 * allocate the default texture objects.
1893 * Note: we could use containment here to 'derive' the driver-specific
1894 * texture object from the core mesa gl_texture_object. Not done at this time.
1896 static struct gl_texture_object
*
1897 tdfxNewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
1899 struct gl_texture_object
*obj
;
1900 obj
= _mesa_new_texture_object(ctx
, name
, target
);
1905 void tdfxInitTextureFuncs( struct dd_function_table
*functions
)
1907 functions
->BindTexture
= tdfxBindTexture
;
1908 functions
->NewTextureObject
= tdfxNewTextureObject
;
1909 functions
->DeleteTexture
= tdfxDeleteTexture
;
1910 functions
->TexEnv
= tdfxTexEnv
;
1911 functions
->TexParameter
= tdfxTexParameter
;
1912 functions
->ChooseTextureFormat
= tdfxChooseTextureFormat
;
1913 functions
->TexImage1D
= tdfxTexImage1D
;
1914 functions
->TexSubImage1D
= tdfxTexSubImage1D
;
1915 functions
->TexImage2D
= tdfxTexImage2D
;
1916 functions
->TexSubImage2D
= tdfxTexSubImage2D
;
1917 functions
->IsTextureResident
= tdfxIsTextureResident
;
1918 functions
->CompressedTexImage2D
= tdfxCompressedTexImage2D
;
1919 functions
->CompressedTexSubImage2D
= tdfxCompressedTexSubImage2D
;
1920 functions
->UpdateTexturePalette
= tdfxUpdateTexturePalette
;