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
;
74 if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_RGB565
) {
75 _t
= GL_UNSIGNED_SHORT_5_6_5_REV
;
77 } else if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_ARGB4444
) {
78 _t
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
80 } else if (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_ARGB1555
) {
81 _t
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
86 srcRowStride
= srcWidth
* bytesPerPixel
;
93 _s
= src
= MALLOC(srcRowStride
* srcHeight
);
94 _d
= dst
= MALLOC(dstWidth
* bytesPerPixel
* dstHeight
);
95 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
96 &_mesa_texformat_rgba8888_rev
, src
,
97 0, 0, 0, /* dstX/Y/Zoffset */
98 srcRowStride
, /* dstRowStride */
99 0, /* dstImageStride */
100 srcWidth
, srcHeight
, 1,
101 texImage
->_BaseFormat
, _t
, srcImage
, &ctx
->DefaultPacking
);
104 if (srcHeight
== 1) {
105 for (i
= 0; i
< dstWidth
; i
++) {
106 for (k
= 0; k
< bytesPerPixel
; k
++) {
107 dst
[0] = (src
[0] + src
[bytesPerPixel
] + 1) / 2;
111 src
+= bytesPerPixel
;
113 } else if (srcWidth
== 1) {
114 for (j
= 0; j
< dstHeight
; j
++) {
115 for (k
= 0; k
< bytesPerPixel
; k
++) {
116 dst
[0] = (src
[0] + src
[srcRowStride
] + 1) / 2;
123 for (j
= 0; j
< dstHeight
; j
++) {
124 for (i
= 0; i
< dstWidth
; i
++) {
125 for (k
= 0; k
< bytesPerPixel
; k
++) {
129 src
[srcRowStride
+ bytesPerPixel
] + 2) / 4;
133 src
+= bytesPerPixel
;
142 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
143 texImage
->TexFormat
, dstImage
,
144 0, 0, 0, /* dstX/Y/Zoffset */
146 0, /* dstImageStride */
147 dstWidth
, dstHeight
, 1,
148 GL_BGRA
, CHAN_TYPE
, dst
, &ctx
->DefaultPacking
);
179 * Compute various texture image parameters.
180 * Input: w, h - source texture width and height
181 * Output: lodlevel - Glide lod level token for the larger texture dimension
182 * aspectratio - Glide aspect ratio token
183 * sscale - S scale factor used during triangle setup
184 * tscale - T scale factor used during triangle setup
185 * wscale - OpenGL -> Glide image width scale factor
186 * hscale - OpenGL -> Glide image height scale factor
189 * w h lodlevel aspectRatio
190 * 128 128 GR_LOD_LOG2_128 (=7) GR_ASPECT_LOG2_1x1 (=0)
191 * 64 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x1 (=0)
192 * 64 32 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_2x1 (=1)
193 * 32 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x2 (=-1)
194 * 32 32 GR_LOD_LOG2_32 (=5) GR_ASPECT_LOG2_1x1 (=0)
197 tdfxTexGetInfo(const GLcontext
*ctx
, int w
, int h
,
198 GrLOD_t
*lodlevel
, GrAspectRatio_t
*aspectratio
,
199 float *sscale
, float *tscale
,
200 int *wscale
, int *hscale
)
202 int logw
, logh
, ar
, lod
, ws
, hs
;
210 ar
= logw
- logh
; /* aspect ratio = difference in log dimensions */
214 /* Hardware only allows a maximum aspect ratio of 8x1, so handle
215 |ar| > 3 by scaling the image and using an 8x1 aspect ratio */
219 if (ar
<= GR_ASPECT_LOG2_8x1
) {
223 /* have to stretch image height */
226 ar
= GR_ASPECT_LOG2_8x1
;
232 if (ar
>= GR_ASPECT_LOG2_1x8
) {
236 /* have to stretch image width */
239 ar
= GR_ASPECT_LOG2_1x8
;
244 *lodlevel
= (GrLOD_t
) lod
;
246 *aspectratio
= (GrAspectRatio_t
) ar
;
259 * We need to call this when a texture object's minification filter
260 * or texture image sizes change.
262 static void RevalidateTexture(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
264 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
270 minl
= maxl
= tObj
->BaseLevel
;
272 if (tObj
->Image
[0][minl
]) {
273 maxl
= MIN2(tObj
->MaxLevel
, tObj
->Image
[0][minl
]->MaxLog2
);
275 /* compute largeLodLog2, aspect ratio and texcoord scale factors */
276 tdfxTexGetInfo(ctx
, tObj
->Image
[0][minl
]->Width
, tObj
->Image
[0][minl
]->Height
,
277 &ti
->info
.largeLodLog2
,
278 &ti
->info
.aspectRatioLog2
,
279 &(ti
->sScale
), &(ti
->tScale
), NULL
, NULL
);
282 if (tObj
->Image
[0][maxl
] && (tObj
->MinFilter
!= GL_NEAREST
) && (tObj
->MinFilter
!= GL_LINEAR
)) {
283 /* mipmapping: need to compute smallLodLog2 */
284 tdfxTexGetInfo(ctx
, tObj
->Image
[0][maxl
]->Width
,
285 tObj
->Image
[0][maxl
]->Height
,
286 &ti
->info
.smallLodLog2
, NULL
,
287 NULL
, NULL
, NULL
, NULL
);
290 /* not mipmapping: smallLodLog2 = largeLodLog2 */
291 ti
->info
.smallLodLog2
= ti
->info
.largeLodLog2
;
297 ti
->info
.data
= NULL
;
299 /* this is necessary because of fxDDCompressedTexImage2D */
301 struct gl_texture_image
*texImage
= tObj
->Image
[0][minl
];
302 tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
303 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
304 ti
->sScale
/= mml
->wScale
;
305 ti
->tScale
/= mml
->hScale
;
312 fxAllocTexObjData(tdfxContextPtr fxMesa
)
316 if (!(ti
= CALLOC(sizeof(tdfxTexInfo
)))) {
317 _mesa_problem(NULL
, "tdfx driver: out of memory");
321 ti
->isInTM
= GL_FALSE
;
323 ti
->whichTMU
= TDFX_TMU_NONE
;
325 ti
->tm
[TDFX_TMU0
] = NULL
;
326 ti
->tm
[TDFX_TMU1
] = NULL
;
328 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
329 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
331 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
332 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
334 ti
->mmMode
= GR_MIPMAP_NEAREST
;
335 ti
->LODblend
= FXFALSE
;
342 * Called via glBindTexture.
345 tdfxBindTexture(GLcontext
* ctx
, GLenum target
,
346 struct gl_texture_object
*tObj
)
348 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
351 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
352 fprintf(stderr
, "fxmesa: fxDDTexBind(%d,%p)\n", tObj
->Name
,
356 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
359 if (!tObj
->DriverData
) {
360 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
363 ti
= TDFX_TEXTURE_DATA(tObj
);
364 ti
->lastTimeUsed
= fxMesa
->texBindNumber
++;
366 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
371 * Called via glTexEnv.
374 tdfxTexEnv(GLcontext
* ctx
, GLenum target
, GLenum pname
,
375 const GLfloat
* param
)
377 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
379 if ( TDFX_DEBUG
& DEBUG_VERBOSE_API
) {
381 fprintf(stderr
, "fxmesa: texenv(%x,%x)\n", pname
,
384 fprintf(stderr
, "fxmesa: texenv(%x)\n", pname
);
387 /* XXX this is a bit of a hack to force the Glide texture
388 * state to be updated.
390 fxMesa
->TexState
.EnvMode
[ctx
->Texture
.CurrentUnit
] = 0;
392 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
397 * Called via glTexParameter.
400 tdfxTexParameter(GLcontext
* ctx
, GLenum target
,
401 struct gl_texture_object
*tObj
,
402 GLenum pname
, const GLfloat
* params
)
404 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
405 GLenum param
= (GLenum
) (GLint
) params
[0];
408 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
409 fprintf(stderr
, "fxmesa: fxDDTexParam(%d,%p,%x,%x)\n", tObj
->Name
,
410 tObj
->DriverData
, pname
, param
);
413 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
416 if (!tObj
->DriverData
)
417 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
419 ti
= TDFX_TEXTURE_DATA(tObj
);
422 case GL_TEXTURE_MIN_FILTER
:
425 ti
->mmMode
= GR_MIPMAP_DISABLE
;
426 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
427 ti
->LODblend
= FXFALSE
;
430 ti
->mmMode
= GR_MIPMAP_DISABLE
;
431 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
432 ti
->LODblend
= FXFALSE
;
434 case GL_NEAREST_MIPMAP_LINEAR
:
435 if (!fxMesa
->Glide
.HaveCombineExt
) {
436 if (fxMesa
->haveTwoTMUs
) {
437 ti
->mmMode
= GR_MIPMAP_NEAREST
;
438 ti
->LODblend
= FXTRUE
;
441 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
442 ti
->LODblend
= FXFALSE
;
444 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
447 /* XXX Voodoo3/Banshee mipmap blending seems to produce
448 * incorrectly filtered colors for the smallest mipmap levels.
449 * To work-around we fall-through here and use a different filter.
451 case GL_NEAREST_MIPMAP_NEAREST
:
452 ti
->mmMode
= GR_MIPMAP_NEAREST
;
453 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
454 ti
->LODblend
= FXFALSE
;
456 case GL_LINEAR_MIPMAP_LINEAR
:
457 if (!fxMesa
->Glide
.HaveCombineExt
) {
458 if (fxMesa
->haveTwoTMUs
) {
459 ti
->mmMode
= GR_MIPMAP_NEAREST
;
460 ti
->LODblend
= FXTRUE
;
463 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
464 ti
->LODblend
= FXFALSE
;
466 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
469 /* XXX Voodoo3/Banshee mipmap blending seems to produce
470 * incorrectly filtered colors for the smallest mipmap levels.
471 * To work-around we fall-through here and use a different filter.
473 case GL_LINEAR_MIPMAP_NEAREST
:
474 ti
->mmMode
= GR_MIPMAP_NEAREST
;
475 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
476 ti
->LODblend
= FXFALSE
;
481 ti
->reloadImages
= GL_TRUE
;
482 RevalidateTexture(ctx
, tObj
);
483 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
486 case GL_TEXTURE_MAG_FILTER
:
489 ti
->magFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
492 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
497 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
500 case GL_TEXTURE_WRAP_S
:
502 case GL_CLAMP_TO_BORDER
:
503 case GL_CLAMP_TO_EDGE
:
505 ti
->sClamp
= GR_TEXTURECLAMP_CLAMP
;
508 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
510 case GL_MIRRORED_REPEAT
:
511 ti
->sClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
516 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
519 case GL_TEXTURE_WRAP_T
:
521 case GL_CLAMP_TO_BORDER
:
522 case GL_CLAMP_TO_EDGE
:
524 ti
->tClamp
= GR_TEXTURECLAMP_CLAMP
;
527 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
529 case GL_MIRRORED_REPEAT
:
530 ti
->tClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
535 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
538 case GL_TEXTURE_BORDER_COLOR
:
541 case GL_TEXTURE_MIN_LOD
:
544 case GL_TEXTURE_MAX_LOD
:
547 case GL_TEXTURE_BASE_LEVEL
:
548 RevalidateTexture(ctx
, tObj
);
550 case GL_TEXTURE_MAX_LEVEL
:
551 RevalidateTexture(ctx
, tObj
);
561 * Called via glDeleteTextures to delete a texture object.
562 * Here, we delete the Glide data associated with the texture.
565 tdfxDeleteTexture(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
567 if (ctx
&& ctx
->DriverCtx
) {
568 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
569 tdfxTMFreeTexture(fxMesa
, tObj
);
570 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
571 /* Free mipmap images and the texture object itself */
572 _mesa_delete_texture_object(ctx
, tObj
);
578 * Return true if texture is resident, false otherwise.
581 tdfxIsTextureResident(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
583 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
584 return (GLboolean
) (ti
&& ti
->isInTM
);
590 * Convert a gl_color_table texture palette to Glide's format.
593 convertPalette(FxU32 data
[256], const struct gl_color_table
*table
)
595 const GLubyte
*tableUB
= (const GLubyte
*) table
->Table
;
596 GLint width
= table
->Size
;
600 ASSERT(table
->Type
== GL_UNSIGNED_BYTE
);
602 switch (table
->Format
) {
604 for (i
= 0; i
< width
; i
++) {
609 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
611 return GR_TEXTABLE_PALETTE_6666_EXT
;
613 for (i
= 0; i
< width
; i
++) {
618 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
620 return GR_TEXTABLE_PALETTE
;
622 for (i
= 0; i
< width
; i
++) {
625 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
627 return GR_TEXTABLE_PALETTE_6666_EXT
;
628 case GL_LUMINANCE_ALPHA
:
629 for (i
= 0; i
< width
; i
++) {
630 r
= g
= b
= tableUB
[i
* 2 + 0];
631 a
= tableUB
[i
* 2 + 1];
632 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
634 return GR_TEXTABLE_PALETTE_6666_EXT
;
636 for (i
= 0; i
< width
; i
++) {
637 r
= tableUB
[i
* 3 + 0];
638 g
= tableUB
[i
* 3 + 1];
639 b
= tableUB
[i
* 3 + 2];
641 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
643 return GR_TEXTABLE_PALETTE
;
645 for (i
= 0; i
< width
; i
++) {
646 r
= tableUB
[i
* 4 + 0];
647 g
= tableUB
[i
* 4 + 1];
648 b
= tableUB
[i
* 4 + 2];
649 a
= tableUB
[i
* 4 + 3];
650 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
652 return GR_TEXTABLE_PALETTE_6666_EXT
;
654 /* XXX fixme: how can this happen? */
655 _mesa_error(NULL
, GL_INVALID_ENUM
, "convertPalette: table->Format == %s",
656 _mesa_lookup_enum_by_nr(table
->Format
));
657 return GR_TEXTABLE_PALETTE
;
664 tdfxUpdateTexturePalette(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
666 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
669 /* per-texture palette */
672 /* This might be a proxy texture. */
673 if (!tObj
->Palette
.Table
)
676 if (!tObj
->DriverData
)
677 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
678 ti
= TDFX_TEXTURE_DATA(tObj
);
680 ti
->paltype
= convertPalette(ti
->palette
.data
, &tObj
->Palette
);
681 /*tdfxTexInvalidate(ctx, tObj);*/
684 /* global texture palette */
685 fxMesa
->TexPalette
.Type
= convertPalette(fxMesa
->glbPalette
.data
, &ctx
->Texture
.Palette
);
686 fxMesa
->TexPalette
.Data
= &(fxMesa
->glbPalette
.data
);
687 fxMesa
->dirty
|= TDFX_UPLOAD_TEXTURE_PALETTE
;
689 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX too heavy-handed */
693 /**********************************************************************/
694 /**** NEW TEXTURE IMAGE FUNCTIONS ****/
695 /**********************************************************************/
698 static FxBool TexusFatalError
= FXFALSE
;
699 static FxBool TexusError
= FXFALSE
;
701 #define TX_DITHER_NONE 0x00000000
704 fxTexusError(const char *string
, FxBool fatal
)
706 _mesa_problem(NULL
, string
);
708 * Just propagate the fatal value up.
711 TexusFatalError
= fatal
;
716 static const struct gl_texture_format
*
717 tdfxChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
718 GLenum srcFormat
, GLenum srcType
)
720 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
721 const GLboolean allow32bpt
= TDFX_IS_NAPALM(fxMesa
);
723 switch (internalFormat
) {
729 case GL_COMPRESSED_ALPHA
:
730 return &_mesa_texformat_a8
;
737 case GL_COMPRESSED_LUMINANCE
:
738 return &_mesa_texformat_l8
;
740 case GL_LUMINANCE_ALPHA
:
741 case GL_LUMINANCE4_ALPHA4
:
742 case GL_LUMINANCE6_ALPHA2
:
743 case GL_LUMINANCE8_ALPHA8
:
744 case GL_LUMINANCE12_ALPHA4
:
745 case GL_LUMINANCE12_ALPHA12
:
746 case GL_LUMINANCE16_ALPHA16
:
747 case GL_COMPRESSED_LUMINANCE_ALPHA
:
748 return &_mesa_texformat_al88
;
754 case GL_COMPRESSED_INTENSITY
:
755 return &_mesa_texformat_i8
;
759 return &_mesa_texformat_rgb565
;
760 case GL_COMPRESSED_RGB
:
761 /* intentional fall-through */
764 if ( srcFormat
== GL_RGB
&& srcType
== GL_UNSIGNED_SHORT_5_6_5
) {
765 return &_mesa_texformat_rgb565
;
767 /* intentional fall through */
772 return (allow32bpt
) ? &_mesa_texformat_argb8888
773 : &_mesa_texformat_rgb565
;
776 return &_mesa_texformat_argb4444
;
777 case GL_COMPRESSED_RGBA
:
778 /* intentional fall-through */
781 if ( srcFormat
== GL_BGRA
) {
782 if ( srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
783 return &_mesa_texformat_argb8888
;
785 else if ( srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
786 return &_mesa_texformat_argb4444
;
788 else if ( srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
789 return &_mesa_texformat_argb1555
;
792 /* intentional fall through */
797 return allow32bpt
? &_mesa_texformat_argb8888
798 : &_mesa_texformat_argb4444
;
800 return &_mesa_texformat_argb1555
;
802 case GL_COLOR_INDEX1_EXT
:
803 case GL_COLOR_INDEX2_EXT
:
804 case GL_COLOR_INDEX4_EXT
:
805 case GL_COLOR_INDEX8_EXT
:
806 case GL_COLOR_INDEX12_EXT
:
807 case GL_COLOR_INDEX16_EXT
:
808 return &_mesa_texformat_ci8
;
809 /* GL_EXT_texture_compression_s3tc */
811 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
814 return &_mesa_texformat_rgb_dxt1
;
815 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
816 return &_mesa_texformat_rgba_dxt1
;
817 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
820 return &_mesa_texformat_rgba_dxt3
;
821 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
822 return &_mesa_texformat_rgba_dxt5
;
823 /* GL_3DFX_texture_compression_FXT1 */
824 case GL_COMPRESSED_RGB_FXT1_3DFX
:
825 return &_mesa_texformat_rgb_fxt1
;
826 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
827 return &_mesa_texformat_rgba_fxt1
;
829 _mesa_problem(ctx
, "unexpected format in tdfxChooseTextureFormat");
836 * Return the Glide format for the given mesa texture format.
838 static GrTextureFormat_t
839 fxGlideFormat(GLint mesaFormat
)
841 switch (mesaFormat
) {
843 return GR_TEXFMT_ALPHA_8
;
845 return GR_TEXFMT_ALPHA_8
;
847 return GR_TEXFMT_INTENSITY_8
;
848 case MESA_FORMAT_CI8
:
849 return GR_TEXFMT_P_8
;
850 case MESA_FORMAT_AL88
:
851 return GR_TEXFMT_ALPHA_INTENSITY_88
;
852 case MESA_FORMAT_RGB565
:
853 return GR_TEXFMT_RGB_565
;
854 case MESA_FORMAT_ARGB4444
:
855 return GR_TEXFMT_ARGB_4444
;
856 case MESA_FORMAT_ARGB1555
:
857 return GR_TEXFMT_ARGB_1555
;
858 case MESA_FORMAT_ARGB8888
:
859 return GR_TEXFMT_ARGB_8888
;
860 case MESA_FORMAT_RGB_FXT1
:
861 case MESA_FORMAT_RGBA_FXT1
:
862 return GR_TEXFMT_ARGB_CMP_FXT1
;
863 case MESA_FORMAT_RGB_DXT1
:
864 case MESA_FORMAT_RGBA_DXT1
:
865 return GR_TEXFMT_ARGB_CMP_DXT1
;
866 case MESA_FORMAT_RGBA_DXT3
:
867 return GR_TEXFMT_ARGB_CMP_DXT3
;
868 case MESA_FORMAT_RGBA_DXT5
:
869 return GR_TEXFMT_ARGB_CMP_DXT5
;
871 _mesa_problem(NULL
, "Unexpected format in fxGlideFormat");
877 /* Texel-fetch functions for software texturing and glGetTexImage().
878 * We should have been able to use some "standard" fetch functions (which
879 * may get defined in texutil.c) but we have to account for scaled texture
880 * images on tdfx hardware (the 8:1 aspect ratio limit).
881 * Hence, we need special functions here.
884 fxt1_decode_1 (const void *texture
, int width
,
885 int i
, int j
, unsigned char *rgba
);
888 fetch_intensity8(const struct gl_texture_image
*texImage
,
889 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
891 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
892 const GLubyte
*texel
;
897 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
898 rgba
[RCOMP
] = *texel
;
899 rgba
[GCOMP
] = *texel
;
900 rgba
[BCOMP
] = *texel
;
901 rgba
[ACOMP
] = *texel
;
906 fetch_luminance8(const struct gl_texture_image
*texImage
,
907 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
909 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
910 const GLubyte
*texel
;
915 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
916 rgba
[RCOMP
] = *texel
;
917 rgba
[GCOMP
] = *texel
;
918 rgba
[BCOMP
] = *texel
;
924 fetch_alpha8(const struct gl_texture_image
*texImage
,
925 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
927 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
928 const GLubyte
*texel
;
933 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
937 rgba
[ACOMP
] = *texel
;
942 fetch_index8(const struct gl_texture_image
*texImage
,
943 GLint i
, GLint j
, GLint k
, GLchan
* indexOut
)
945 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
946 const GLubyte
*texel
;
951 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
957 fetch_luminance8_alpha8(const struct gl_texture_image
*texImage
,
958 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
960 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
961 const GLubyte
*texel
;
966 texel
= ((GLubyte
*) texImage
->Data
) + (j
* mml
->width
+ i
) * 2;
967 rgba
[RCOMP
] = texel
[0];
968 rgba
[GCOMP
] = texel
[0];
969 rgba
[BCOMP
] = texel
[0];
970 rgba
[ACOMP
] = texel
[1];
975 fetch_r5g6b5(const struct gl_texture_image
*texImage
,
976 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
978 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
979 const GLushort
*texel
;
984 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
985 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
986 rgba
[GCOMP
] = (((*texel
) >> 5) & 0x3f) * 255 / 63;
987 rgba
[BCOMP
] = (((*texel
) >> 0) & 0x1f) * 255 / 31;
993 fetch_r4g4b4a4(const struct gl_texture_image
*texImage
,
994 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
996 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
997 const GLushort
*texel
;
1000 j
= j
* mml
->hScale
;
1002 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1003 rgba
[RCOMP
] = (((*texel
) >> 12) & 0xf) * 255 / 15;
1004 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xf) * 255 / 15;
1005 rgba
[BCOMP
] = (((*texel
) >> 4) & 0xf) * 255 / 15;
1006 rgba
[ACOMP
] = (((*texel
) >> 0) & 0xf) * 255 / 15;
1011 fetch_r5g5b5a1(const struct gl_texture_image
*texImage
,
1012 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1014 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1015 const GLushort
*texel
;
1017 i
= i
* mml
->wScale
;
1018 j
= j
* mml
->hScale
;
1020 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1021 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
1022 rgba
[GCOMP
] = (((*texel
) >> 6) & 0x1f) * 255 / 31;
1023 rgba
[BCOMP
] = (((*texel
) >> 1) & 0x1f) * 255 / 31;
1024 rgba
[ACOMP
] = (((*texel
) >> 0) & 0x01) * 255;
1029 fetch_a8r8g8b8(const struct gl_texture_image
*texImage
,
1030 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1032 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1033 const GLuint
*texel
;
1035 i
= i
* mml
->wScale
;
1036 j
= j
* mml
->hScale
;
1038 texel
= ((GLuint
*) texImage
->Data
) + j
* mml
->width
+ i
;
1039 rgba
[RCOMP
] = (((*texel
) >> 16) & 0xff);
1040 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xff);
1041 rgba
[BCOMP
] = (((*texel
) ) & 0xff);
1042 rgba
[ACOMP
] = (((*texel
) >> 24) & 0xff);
1047 fetch_rgb_fxt1(const struct gl_texture_image
*texImage
,
1048 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1050 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1052 i
= i
* mml
->wScale
;
1053 j
= j
* mml
->hScale
;
1055 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1061 fetch_rgba_fxt1(const struct gl_texture_image
*texImage
,
1062 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1064 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1066 i
= i
* mml
->wScale
;
1067 j
= j
* mml
->hScale
;
1069 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1074 fetch_rgb_dxt1(const struct gl_texture_image
*texImage
,
1075 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1077 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1079 i
= i
* mml
->wScale
;
1080 j
= j
* mml
->hScale
;
1082 _mesa_texformat_rgb_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1087 fetch_rgba_dxt1(const struct gl_texture_image
*texImage
,
1088 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1090 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1092 i
= i
* mml
->wScale
;
1093 j
= j
* mml
->hScale
;
1095 _mesa_texformat_rgba_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1100 fetch_rgba_dxt3(const struct gl_texture_image
*texImage
,
1101 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1103 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1105 i
= i
* mml
->wScale
;
1106 j
= j
* mml
->hScale
;
1108 _mesa_texformat_rgba_dxt3
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1113 fetch_rgba_dxt5(const struct gl_texture_image
*texImage
,
1114 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1116 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1118 i
= i
* mml
->wScale
;
1119 j
= j
* mml
->hScale
;
1121 _mesa_texformat_rgba_dxt5
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1125 static FetchTexelFuncC
1126 fxFetchFunction(GLint mesaFormat
)
1128 switch (mesaFormat
) {
1129 case MESA_FORMAT_I8
:
1130 return &fetch_intensity8
;
1131 case MESA_FORMAT_A8
:
1132 return &fetch_alpha8
;
1133 case MESA_FORMAT_L8
:
1134 return &fetch_luminance8
;
1135 case MESA_FORMAT_CI8
:
1136 return &fetch_index8
;
1137 case MESA_FORMAT_AL88
:
1138 return &fetch_luminance8_alpha8
;
1139 case MESA_FORMAT_RGB565
:
1140 return &fetch_r5g6b5
;
1141 case MESA_FORMAT_ARGB4444
:
1142 return &fetch_r4g4b4a4
;
1143 case MESA_FORMAT_ARGB1555
:
1144 return &fetch_r5g5b5a1
;
1145 case MESA_FORMAT_ARGB8888
:
1146 return &fetch_a8r8g8b8
;
1147 case MESA_FORMAT_RGB_FXT1
:
1148 return &fetch_rgb_fxt1
;
1149 case MESA_FORMAT_RGBA_FXT1
:
1150 return &fetch_rgba_fxt1
;
1151 case MESA_FORMAT_RGB_DXT1
:
1152 return &fetch_rgb_dxt1
;
1153 case MESA_FORMAT_RGBA_DXT1
:
1154 return &fetch_rgba_dxt1
;
1155 case MESA_FORMAT_RGBA_DXT3
:
1156 return &fetch_rgba_dxt3
;
1157 case MESA_FORMAT_RGBA_DXT5
:
1158 return &fetch_rgba_dxt5
;
1160 _mesa_problem(NULL
, "Unexpected format in fxFetchFunction");
1167 adjust2DRatio (GLcontext
*ctx
,
1168 GLint xoffset
, GLint yoffset
,
1169 GLint width
, GLint height
,
1170 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1171 const struct gl_pixelstore_attrib
*packing
,
1172 tdfxMipMapLevel
*mml
,
1173 struct gl_texture_image
*texImage
,
1177 const GLint newWidth
= width
* mml
->wScale
;
1178 const GLint newHeight
= height
* mml
->hScale
;
1181 if (!texImage
->IsCompressed
) {
1183 tempImage
= MALLOC(width
* height
* texelBytes
);
1188 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1189 texImage
->TexFormat
, tempImage
,
1190 0, 0, 0, /* dstX/Y/Zoffset */
1191 width
* texelBytes
, /* dstRowStride */
1192 0, /* dstImageStride */
1194 format
, type
, pixels
, packing
);
1197 /* compute address of dest subimage within the overal tex image */
1198 destAddr
= (GLubyte
*) texImage
->Data
1199 + (yoffset
* mml
->hScale
* mml
->width
1200 + xoffset
* mml
->wScale
) * texelBytes
;
1202 _mesa_rescale_teximage2d(texelBytes
,
1204 dstRowStride
, /* dst stride */
1206 newWidth
, newHeight
,
1207 tempImage
, destAddr
);
1209 const GLint rawBytes
= 4;
1210 GLvoid
*rawImage
= MALLOC(width
* height
* rawBytes
);
1214 tempImage
= MALLOC(newWidth
* newHeight
* rawBytes
);
1218 /* unpack image, apply transfer ops and store in rawImage */
1219 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
1220 &_mesa_texformat_rgba8888_rev
, rawImage
,
1221 0, 0, 0, /* dstX/Y/Zoffset */
1222 width
* rawBytes
, /* dstRowStride */
1223 0, /* dstImageStride */
1225 format
, type
, pixels
, packing
);
1226 _mesa_rescale_teximage2d(rawBytes
,
1228 newWidth
* rawBytes
, /* dst stride */
1229 width
, height
, /* src */
1230 newWidth
, newHeight
, /* dst */
1231 rawImage
/*src*/, tempImage
/*dst*/ );
1232 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1233 texImage
->TexFormat
, texImage
->Data
,
1234 xoffset
* mml
->wScale
, yoffset
* mml
->hScale
, 0, /* dstX/Y/Zoffset */
1236 0, /* dstImageStride */
1237 newWidth
, newHeight
, 1,
1238 GL_RGBA
, CHAN_TYPE
, tempImage
, &ctx
->DefaultPacking
);
1249 tdfxTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1250 GLint internalFormat
, GLint width
, GLint height
, GLint border
,
1251 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1252 const struct gl_pixelstore_attrib
*packing
,
1253 struct gl_texture_object
*texObj
,
1254 struct gl_texture_image
*texImage
)
1256 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1258 tdfxMipMapLevel
*mml
;
1259 GLint texelBytes
, dstRowStride
;
1262 printf("TexImage id=%d int 0x%x format 0x%x type 0x%x %dx%d\n",
1263 texObj->Name, texImage->IntFormat, format, type,
1264 texImage->Width, texImage->Height);
1267 ti
= TDFX_TEXTURE_DATA(texObj
);
1269 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1270 if (!texObj
->DriverData
) {
1271 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1274 ti
= TDFX_TEXTURE_DATA(texObj
);
1278 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1280 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1281 if (!texImage
->DriverData
) {
1282 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1285 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1288 /* Determine width and height scale factors for texture.
1289 * Remember, Glide is limited to 8:1 aspect ratios.
1292 texImage
->Width
, texImage
->Height
,
1293 NULL
, /* lod level */
1294 NULL
, /* aspect ratio */
1295 NULL
, NULL
, /* sscale, tscale */
1296 &mml
->wScale
, &mml
->hScale
);
1298 /* rescaled size: */
1299 mml
->width
= width
* mml
->wScale
;
1300 mml
->height
= height
* mml
->hScale
;
1302 #if FX_COMPRESS_S3TC_AS_FXT1_HACK
1303 /* [koolsmoky] substitute FXT1 for DXTn and Legacy S3TC */
1304 /* [dBorca] we should update texture's attribute, then,
1305 * because if the application asks us to decompress, we
1306 * have to know the REAL format! Also, DXT3/5 might not
1307 * be correct, since it would mess with "compressedSize".
1308 * Ditto for GL_RGBA[4]_S3TC, which is always mapped to DXT3.
1310 if (texImage
->IsCompressed
) {
1311 switch (internalFormat
) {
1312 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1315 internalFormat
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1317 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1318 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1319 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1322 internalFormat
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1324 texImage
->InternalFormat
= internalFormat
;
1328 if (fxMesa
->type
>= GR_SSTTYPE_Voodoo4
) {
1329 GLenum texNapalm
= 0;
1330 if (internalFormat
== GL_COMPRESSED_RGB
) {
1331 texNapalm
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1332 } else if (internalFormat
== GL_COMPRESSED_RGBA
) {
1333 texNapalm
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1336 texImage
->InternalFormat
= internalFormat
= texNapalm
;
1337 texImage
->IsCompressed
= GL_TRUE
;
1342 /* choose the texture format */
1343 assert(ctx
->Driver
.ChooseTextureFormat
);
1344 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1345 internalFormat
, format
, type
);
1346 assert(texImage
->TexFormat
);
1347 mml
->glideFormat
= fxGlideFormat(texImage
->TexFormat
->MesaFormat
);
1348 ti
->info
.format
= mml
->glideFormat
;
1349 texImage
->FetchTexelc
= fxFetchFunction(texImage
->TexFormat
->MesaFormat
);
1350 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1352 if (texImage
->IsCompressed
) {
1353 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1358 dstRowStride
= _mesa_compressed_row_stride(internalFormat
, mml
->width
);
1359 texImage
->Data
= _mesa_malloc(texImage
->CompressedSize
);
1361 dstRowStride
= mml
->width
* texelBytes
;
1362 texImage
->Data
= _mesa_malloc(mml
->width
* mml
->height
* texelBytes
);
1364 if (!texImage
->Data
) {
1365 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1369 if (pixels
!= NULL
) {
1370 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1371 /* rescale image to overcome 1:8 aspect limitation */
1372 if (!adjust2DRatio(ctx
,
1375 format
, type
, pixels
,
1382 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1387 /* no rescaling needed */
1388 /* unpack image, apply transfer ops and store in texImage->Data */
1389 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1390 texImage
->TexFormat
, texImage
->Data
,
1391 0, 0, 0, /* dstX/Y/Zoffset */
1393 0, /* dstImageStride */
1395 format
, type
, pixels
, packing
);
1398 /* GL_SGIS_generate_mipmap */
1399 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1400 GLint mipWidth
, mipHeight
;
1401 tdfxMipMapLevel
*mip
;
1402 struct gl_texture_image
*mipImage
;
1403 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1404 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1406 assert(!texImage
->IsCompressed
);
1408 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
1409 mipWidth
= width
/ 2;
1413 mipHeight
= height
/ 2;
1417 if ((mipWidth
== width
) && (mipHeight
== height
)) {
1420 _mesa_TexImage2D(target
, ++level
, internalFormat
,
1421 mipWidth
, mipHeight
, border
,
1424 mipImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1425 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
1426 _mesa_halve2x2_teximage2d(ctx
,
1429 mml
->width
, mml
->height
,
1430 texImage
->Data
, mipImage
->Data
);
1431 texImage
= mipImage
;
1439 RevalidateTexture(ctx
, texObj
);
1441 ti
->reloadImages
= GL_TRUE
;
1442 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1447 tdfxTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1448 GLint xoffset
, GLint yoffset
,
1449 GLsizei width
, GLsizei height
,
1450 GLenum format
, GLenum type
,
1451 const GLvoid
*pixels
,
1452 const struct gl_pixelstore_attrib
*packing
,
1453 struct gl_texture_object
*texObj
,
1454 struct gl_texture_image
*texImage
)
1456 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1458 tdfxMipMapLevel
*mml
;
1459 GLint texelBytes
, dstRowStride
;
1461 if (!texObj
->DriverData
) {
1462 _mesa_problem(ctx
, "problem in fxDDTexSubImage2D");
1466 ti
= TDFX_TEXTURE_DATA(texObj
);
1468 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1471 assert(texImage
->Data
); /* must have an existing texture image! */
1472 assert(texImage
->_BaseFormat
);
1474 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1475 if (texImage
->IsCompressed
) {
1476 dstRowStride
= _mesa_compressed_row_stride(texImage
->InternalFormat
, mml
->width
);
1478 dstRowStride
= mml
->width
* texelBytes
;
1481 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1482 /* need to rescale subimage to match mipmap level's rescale factors */
1483 if (!adjust2DRatio(ctx
,
1486 format
, type
, pixels
,
1493 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
1498 /* no rescaling needed */
1499 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1500 texImage
->TexFormat
, texImage
->Data
,
1501 xoffset
, yoffset
, 0,
1503 0, /* dstImageStride */
1505 format
, type
, pixels
, packing
);
1508 /* GL_SGIS_generate_mipmap */
1509 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1510 GLint mipWidth
, mipHeight
;
1511 tdfxMipMapLevel
*mip
;
1512 struct gl_texture_image
*mipImage
;
1513 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1514 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1516 assert(!texImage
->IsCompressed
);
1518 width
= texImage
->Width
;
1519 height
= texImage
->Height
;
1520 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
1521 mipWidth
= width
/ 2;
1525 mipHeight
= height
/ 2;
1529 if ((mipWidth
== width
) && (mipHeight
== height
)) {
1533 mipImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1534 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
1535 _mesa_halve2x2_teximage2d(ctx
,
1538 mml
->width
, mml
->height
,
1539 texImage
->Data
, mipImage
->Data
);
1540 texImage
= mipImage
;
1547 ti
->reloadImages
= GL_TRUE
; /* signal the image needs to be reloaded */
1548 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX this might be a bit much */
1553 tdfxTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1554 GLint internalFormat
, GLint width
, GLint border
,
1555 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1556 const struct gl_pixelstore_attrib
*packing
,
1557 struct gl_texture_object
*texObj
,
1558 struct gl_texture_image
*texImage
)
1560 tdfxTexImage2D(ctx
, target
, level
,
1561 internalFormat
, width
, 1, border
,
1562 format
, type
, pixels
,
1569 tdfxTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1572 GLenum format
, GLenum type
,
1573 const GLvoid
*pixels
,
1574 const struct gl_pixelstore_attrib
*packing
,
1575 struct gl_texture_object
*texObj
,
1576 struct gl_texture_image
*texImage
)
1578 tdfxTexSubImage2D(ctx
, target
, level
,
1588 /**********************************************************************/
1589 /**** COMPRESSED TEXTURE IMAGE FUNCTIONS ****/
1590 /**********************************************************************/
1593 tdfxCompressedTexImage2D (GLcontext
*ctx
, GLenum target
,
1594 GLint level
, GLint internalFormat
,
1595 GLsizei width
, GLsizei height
, GLint border
,
1596 GLsizei imageSize
, const GLvoid
*data
,
1597 struct gl_texture_object
*texObj
,
1598 struct gl_texture_image
*texImage
)
1600 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1602 tdfxMipMapLevel
*mml
;
1604 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1605 fprintf(stderr
, "tdfxCompressedTexImage2D: id=%d int 0x%x %dx%d\n",
1606 texObj
->Name
, internalFormat
,
1610 if ((target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_2D
) || texImage
->Border
> 0) {
1611 _mesa_problem(NULL
, "tdfx: unsupported texture in tdfxCompressedTexImg()\n");
1615 assert(texImage
->IsCompressed
);
1617 ti
= TDFX_TEXTURE_DATA(texObj
);
1619 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1620 if (!texObj
->DriverData
) {
1621 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1624 ti
= TDFX_TEXTURE_DATA(texObj
);
1628 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1630 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1631 if (!texImage
->DriverData
) {
1632 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1635 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1638 tdfxTexGetInfo(ctx
, width
, height
, NULL
, NULL
, NULL
, NULL
,
1639 &mml
->wScale
, &mml
->hScale
);
1641 mml
->width
= width
* mml
->wScale
;
1642 mml
->height
= height
* mml
->hScale
;
1645 /* choose the texture format */
1646 assert(ctx
->Driver
.ChooseTextureFormat
);
1647 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1648 internalFormat
, -1/*format*/, -1/*type*/);
1649 assert(texImage
->TexFormat
);
1651 /* Determine the appropriate Glide texel format,
1652 * given the user's internal texture format hint.
1654 mml
->glideFormat
= fxGlideFormat(texImage
->TexFormat
->MesaFormat
);
1655 ti
->info
.format
= mml
->glideFormat
;
1656 texImage
->FetchTexelc
= fxFetchFunction(texImage
->TexFormat
->MesaFormat
);
1658 /* allocate new storage for texture image, if needed */
1659 if (!texImage
->Data
) {
1660 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1665 texImage
->Data
= _mesa_malloc(texImage
->CompressedSize
);
1666 if (!texImage
->Data
) {
1667 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1672 /* save the texture data */
1673 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1674 /* [dBorca] Hack alert:
1675 * now we're screwed. We can't decompress,
1676 * unless we do it in HW (via textureBuffer).
1677 * We still have some chances:
1678 * 1) we got FXT1 textures - we CAN decompress, rescale for
1679 * aspectratio, then compress back.
1680 * 2) there is a chance that MIN("s", "t") won't be overflowed.
1681 * Thus, we don't care about textureclamp and we could lower
1682 * MIN("uscale", "vscale") below 32. We still have to have
1683 * our data aligned inside a 8:1 rectangle.
1684 * 3) just in case if MIN("s", "t") gets overflowed with GL_REPEAT,
1685 * we replicate the data over the padded area.
1686 * For now, we take 2) + 3) but texelfetchers will be wrong!
1688 GLuint srcRowStride
= _mesa_compressed_row_stride(internalFormat
, width
);
1690 GLuint destRowStride
= _mesa_compressed_row_stride(internalFormat
,
1693 _mesa_upscale_teximage2d(srcRowStride
, (height
+3) / 4,
1694 destRowStride
, (mml
->height
+3) / 4,
1695 1, data
, srcRowStride
,
1697 ti
->padded
= GL_TRUE
;
1699 MEMCPY(texImage
->Data
, data
, texImage
->CompressedSize
);
1702 /* GL_SGIS_generate_mipmap */
1703 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1704 assert(!texImage
->IsCompressed
);
1707 RevalidateTexture(ctx
, texObj
);
1709 ti
->reloadImages
= GL_TRUE
;
1710 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1715 tdfxCompressedTexSubImage2D( GLcontext
*ctx
, GLenum target
,
1716 GLint level
, GLint xoffset
,
1717 GLint yoffset
, GLsizei width
,
1718 GLint height
, GLenum format
,
1719 GLsizei imageSize
, const GLvoid
*data
,
1720 struct gl_texture_object
*texObj
,
1721 struct gl_texture_image
*texImage
)
1723 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1725 tdfxMipMapLevel
*mml
;
1726 GLint destRowStride
, srcRowStride
;
1730 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1731 fprintf(stderr
, "tdfxCompressedTexSubImage2D: id=%d\n", texObj
->Name
);
1734 ti
= TDFX_TEXTURE_DATA(texObj
);
1736 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1739 srcRowStride
= _mesa_compressed_row_stride(texImage
->InternalFormat
, width
);
1741 destRowStride
= _mesa_compressed_row_stride(texImage
->InternalFormat
,
1743 dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
1744 texImage
->InternalFormat
,
1746 (GLubyte
*) texImage
->Data
);
1748 rows
= height
/ 4; /* [dBorca] hardcoded 4, but works for FXT1/DXTC */
1750 for (i
= 0; i
< rows
; i
++) {
1751 MEMCPY(dest
, data
, srcRowStride
);
1752 dest
+= destRowStride
;
1753 data
= (GLvoid
*)((GLuint
)data
+ (GLuint
)srcRowStride
);
1756 /* [dBorca] Hack alert:
1757 * see fxDDCompressedTexImage2D for caveats
1759 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1760 srcRowStride
= _mesa_compressed_row_stride(texImage
->InternalFormat
, texImage
->Width
);
1762 destRowStride
= _mesa_compressed_row_stride(texImage
->InternalFormat
,
1764 _mesa_upscale_teximage2d(srcRowStride
, texImage
->Height
/ 4,
1765 destRowStride
, mml
->height
/ 4,
1766 1, texImage
->Data
, destRowStride
,
1770 /* GL_SGIS_generate_mipmap */
1771 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1772 assert(!texImage
->IsCompressed
);
1775 RevalidateTexture(ctx
, texObj
);
1777 ti
->reloadImages
= GL_TRUE
;
1778 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1784 PrintTexture(int w
, int h
, int c
, const GLubyte
* data
)
1787 for (i
= 0; i
< h
; i
++) {
1788 for (j
= 0; j
< w
; j
++) {
1790 printf("%02x %02x ", data
[0], data
[1]);
1792 printf("%02x %02x %02x ", data
[0], data
[1], data
[2]);
1802 tdfxTestProxyTexImage(GLcontext
*ctx
, GLenum target
,
1803 GLint level
, GLint internalFormat
,
1804 GLenum format
, GLenum type
,
1805 GLint width
, GLint height
,
1806 GLint depth
, GLint border
)
1808 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1809 struct gl_shared_state
*mesaShared
= fxMesa
->glCtx
->Shared
;
1810 struct tdfxSharedState
*shared
= (struct tdfxSharedState
*) mesaShared
->DriverData
;
1813 case GL_PROXY_TEXTURE_1D
:
1815 case GL_PROXY_TEXTURE_2D
:
1817 struct gl_texture_object
*tObj
;
1821 tObj
= ctx
->Texture
.Proxy2D
;
1822 if (!tObj
->DriverData
)
1823 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1824 ti
= TDFX_TEXTURE_DATA(tObj
);
1827 /* assign the parameters to test against */
1828 tObj
->Image
[0][level
]->Width
= width
;
1829 tObj
->Image
[0][level
]->Height
= height
;
1830 tObj
->Image
[0][level
]->Border
= border
;
1832 tObj
->Image
[0][level
]->InternalFormat
= internalFormat
;
1835 /* don't use mipmap levels > 0 */
1836 tObj
->MinFilter
= tObj
->MagFilter
= GL_NEAREST
;
1839 /* test with all mipmap levels */
1840 tObj
->MinFilter
= GL_LINEAR_MIPMAP_LINEAR
;
1841 tObj
->MagFilter
= GL_NEAREST
;
1843 RevalidateTexture(ctx
, tObj
);
1846 printf("small lodlog2 0x%x\n", ti->info.smallLodLog2);
1847 printf("large lodlog2 0x%x\n", ti->info.largeLodLog2);
1848 printf("aspect ratio 0x%x\n", ti->info.aspectRatioLog2);
1849 printf("glide format 0x%x\n", ti->info.format);
1850 printf("data %p\n", ti->info.data);
1851 printf("lodblend %d\n", (int) ti->LODblend);
1854 /* determine where texture will reside */
1855 if (ti
->LODblend
&& !shared
->umaTexMemory
) {
1856 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1857 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1858 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1861 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1862 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1863 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1866 printf("Proxy test %d > %d\n", memNeeded, shared->totalTexMem[0]);
1868 if (memNeeded
> shared
->totalTexMem
[0])
1873 case GL_PROXY_TEXTURE_3D
:
1874 return GL_TRUE
; /* software rendering */
1876 return GL_TRUE
; /* never happens, silence compiler */
1882 * Allocate a new texture object.
1883 * Called via ctx->Driver.NewTextureObject.
1884 * Note: this function will be called during context creation to
1885 * allocate the default texture objects.
1886 * Note: we could use containment here to 'derive' the driver-specific
1887 * texture object from the core mesa gl_texture_object. Not done at this time.
1889 static struct gl_texture_object
*
1890 tdfxNewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
1892 struct gl_texture_object
*obj
;
1893 obj
= _mesa_new_texture_object(ctx
, name
, target
);
1898 void tdfxInitTextureFuncs( struct dd_function_table
*functions
)
1900 functions
->BindTexture
= tdfxBindTexture
;
1901 functions
->NewTextureObject
= tdfxNewTextureObject
;
1902 functions
->DeleteTexture
= tdfxDeleteTexture
;
1903 functions
->TexEnv
= tdfxTexEnv
;
1904 functions
->TexParameter
= tdfxTexParameter
;
1905 functions
->ChooseTextureFormat
= tdfxChooseTextureFormat
;
1906 functions
->TexImage1D
= tdfxTexImage1D
;
1907 functions
->TexSubImage1D
= tdfxTexSubImage1D
;
1908 functions
->TexImage2D
= tdfxTexImage2D
;
1909 functions
->TexSubImage2D
= tdfxTexSubImage2D
;
1910 functions
->IsTextureResident
= tdfxIsTextureResident
;
1911 functions
->CompressedTexImage2D
= tdfxCompressedTexImage2D
;
1912 functions
->CompressedTexSubImage2D
= tdfxCompressedTexSubImage2D
;
1913 functions
->UpdateTexturePalette
= tdfxUpdateTexturePalette
;