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
29 * Daniel Borca <dborca@users.sourceforge.net>, 19 Jul 2004
32 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
35 * Gareth Hughes <gareth@valinux.com>
36 * Brian Paul <brianp@valinux.com>
41 #include "main/enums.h"
42 #include "main/image.h"
43 #include "main/mipmap.h"
44 #include "main/texcompress.h"
45 #include "main/texformat.h"
46 #include "main/teximage.h"
47 #include "main/texstore.h"
48 #include "main/texobj.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 tdfxGenerateMipmap(GLcontext
*ctx
, GLenum target
,
181 struct gl_texture_object
*texObj
)
183 GLint mipWidth
, mipHeight
;
184 tdfxMipMapLevel
*mip
;
185 struct gl_texture_image
*mipImage
; /* the new/next image */
186 struct gl_texture_image
*texImage
;
187 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
188 GLint level
= texObj
->BaseLevel
;
189 GLsizei width
, height
, texelBytes
;
190 const tdfxMipMapLevel
*mml
;
192 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
193 assert(!texImage
->IsCompressed
);
195 mml
= TDFX_TEXIMAGE_DATA(texImage
);
197 width
= texImage
->Width
;
198 height
= texImage
->Height
;
199 while (level
< texObj
->MaxLevel
&& level
< maxLevels
- 1) {
200 mipWidth
= width
/ 2;
204 mipHeight
= height
/ 2;
208 if ((mipWidth
== width
) && (mipHeight
== height
)) {
212 mipImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
213 mip
= TDFX_TEXIMAGE_DATA(mipImage
);
214 _mesa_halve2x2_teximage2d(ctx
,
217 mml
->width
, mml
->height
,
218 texImage
->Data
, mipImage
->Data
);
228 * Compute various texture image parameters.
229 * Input: w, h - source texture width and height
230 * Output: lodlevel - Glide lod level token for the larger texture dimension
231 * aspectratio - Glide aspect ratio token
232 * sscale - S scale factor used during triangle setup
233 * tscale - T scale factor used during triangle setup
234 * wscale - OpenGL -> Glide image width scale factor
235 * hscale - OpenGL -> Glide image height scale factor
238 * w h lodlevel aspectRatio
239 * 128 128 GR_LOD_LOG2_128 (=7) GR_ASPECT_LOG2_1x1 (=0)
240 * 64 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x1 (=0)
241 * 64 32 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_2x1 (=1)
242 * 32 64 GR_LOD_LOG2_64 (=6) GR_ASPECT_LOG2_1x2 (=-1)
243 * 32 32 GR_LOD_LOG2_32 (=5) GR_ASPECT_LOG2_1x1 (=0)
246 tdfxTexGetInfo(const GLcontext
*ctx
, int w
, int h
,
247 GrLOD_t
*lodlevel
, GrAspectRatio_t
*aspectratio
,
248 float *sscale
, float *tscale
,
249 int *wscale
, int *hscale
)
251 int logw
, logh
, ar
, lod
, ws
, hs
;
259 ar
= logw
- logh
; /* aspect ratio = difference in log dimensions */
263 /* Hardware only allows a maximum aspect ratio of 8x1, so handle
264 |ar| > 3 by scaling the image and using an 8x1 aspect ratio */
268 if (ar
<= GR_ASPECT_LOG2_8x1
) {
272 /* have to stretch image height */
275 ar
= GR_ASPECT_LOG2_8x1
;
281 if (ar
>= GR_ASPECT_LOG2_1x8
) {
285 /* have to stretch image width */
288 ar
= GR_ASPECT_LOG2_1x8
;
293 *lodlevel
= (GrLOD_t
) lod
;
295 *aspectratio
= (GrAspectRatio_t
) ar
;
308 * We need to call this when a texture object's minification filter
309 * or texture image sizes change.
311 static void RevalidateTexture(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
313 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
319 minl
= maxl
= tObj
->BaseLevel
;
321 if (tObj
->Image
[0][minl
]) {
322 maxl
= MIN2(tObj
->MaxLevel
, tObj
->Image
[0][minl
]->MaxLog2
);
324 /* compute largeLodLog2, aspect ratio and texcoord scale factors */
325 tdfxTexGetInfo(ctx
, tObj
->Image
[0][minl
]->Width
, tObj
->Image
[0][minl
]->Height
,
326 &ti
->info
.largeLodLog2
,
327 &ti
->info
.aspectRatioLog2
,
328 &(ti
->sScale
), &(ti
->tScale
), NULL
, NULL
);
331 if (tObj
->Image
[0][maxl
] && (tObj
->MinFilter
!= GL_NEAREST
) && (tObj
->MinFilter
!= GL_LINEAR
)) {
332 /* mipmapping: need to compute smallLodLog2 */
333 tdfxTexGetInfo(ctx
, tObj
->Image
[0][maxl
]->Width
,
334 tObj
->Image
[0][maxl
]->Height
,
335 &ti
->info
.smallLodLog2
, NULL
,
336 NULL
, NULL
, NULL
, NULL
);
339 /* not mipmapping: smallLodLog2 = largeLodLog2 */
340 ti
->info
.smallLodLog2
= ti
->info
.largeLodLog2
;
346 ti
->info
.data
= NULL
;
348 /* this is necessary because of fxDDCompressedTexImage2D */
350 struct gl_texture_image
*texImage
= tObj
->Image
[0][minl
];
351 tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
352 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
353 ti
->sScale
/= mml
->wScale
;
354 ti
->tScale
/= mml
->hScale
;
361 fxAllocTexObjData(tdfxContextPtr fxMesa
)
365 if (!(ti
= CALLOC(sizeof(tdfxTexInfo
)))) {
366 _mesa_problem(NULL
, "tdfx driver: out of memory");
370 ti
->isInTM
= GL_FALSE
;
372 ti
->whichTMU
= TDFX_TMU_NONE
;
374 ti
->tm
[TDFX_TMU0
] = NULL
;
375 ti
->tm
[TDFX_TMU1
] = NULL
;
377 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
378 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
380 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
381 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
383 ti
->mmMode
= GR_MIPMAP_NEAREST
;
384 ti
->LODblend
= FXFALSE
;
391 * Called via glBindTexture.
394 tdfxBindTexture(GLcontext
* ctx
, GLenum target
,
395 struct gl_texture_object
*tObj
)
397 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
400 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
401 fprintf(stderr
, "fxmesa: fxDDTexBind(%d,%p)\n", tObj
->Name
,
405 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
408 if (!tObj
->DriverData
) {
409 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
412 ti
= TDFX_TEXTURE_DATA(tObj
);
413 ti
->lastTimeUsed
= fxMesa
->texBindNumber
++;
415 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
420 * Called via glTexEnv.
423 tdfxTexEnv(GLcontext
* ctx
, GLenum target
, GLenum pname
,
424 const GLfloat
* param
)
426 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
428 if ( TDFX_DEBUG
& DEBUG_VERBOSE_API
) {
430 fprintf(stderr
, "fxmesa: texenv(%x,%x)\n", pname
,
433 fprintf(stderr
, "fxmesa: texenv(%x)\n", pname
);
436 /* XXX this is a bit of a hack to force the Glide texture
437 * state to be updated.
439 fxMesa
->TexState
.EnvMode
[ctx
->Texture
.CurrentUnit
] = 0;
441 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
446 * Called via glTexParameter.
449 tdfxTexParameter(GLcontext
* ctx
, GLenum target
,
450 struct gl_texture_object
*tObj
,
451 GLenum pname
, const GLfloat
* params
)
453 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
454 GLenum param
= (GLenum
) (GLint
) params
[0];
457 if (MESA_VERBOSE
& VERBOSE_DRIVER
) {
458 fprintf(stderr
, "fxmesa: fxDDTexParam(%d,%p,%x,%x)\n", tObj
->Name
,
459 tObj
->DriverData
, pname
, param
);
462 if ((target
!= GL_TEXTURE_1D
) && (target
!= GL_TEXTURE_2D
))
465 if (!tObj
->DriverData
)
466 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
468 ti
= TDFX_TEXTURE_DATA(tObj
);
471 case GL_TEXTURE_MIN_FILTER
:
474 ti
->mmMode
= GR_MIPMAP_DISABLE
;
475 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
476 ti
->LODblend
= FXFALSE
;
479 ti
->mmMode
= GR_MIPMAP_DISABLE
;
480 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
481 ti
->LODblend
= FXFALSE
;
483 case GL_NEAREST_MIPMAP_LINEAR
:
484 if (!fxMesa
->Glide
.HaveCombineExt
) {
485 if (fxMesa
->haveTwoTMUs
) {
486 ti
->mmMode
= GR_MIPMAP_NEAREST
;
487 ti
->LODblend
= FXTRUE
;
490 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
491 ti
->LODblend
= FXFALSE
;
493 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
496 /* XXX Voodoo3/Banshee mipmap blending seems to produce
497 * incorrectly filtered colors for the smallest mipmap levels.
498 * To work-around we fall-through here and use a different filter.
500 case GL_NEAREST_MIPMAP_NEAREST
:
501 ti
->mmMode
= GR_MIPMAP_NEAREST
;
502 ti
->minFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
503 ti
->LODblend
= FXFALSE
;
505 case GL_LINEAR_MIPMAP_LINEAR
:
506 if (!fxMesa
->Glide
.HaveCombineExt
) {
507 if (fxMesa
->haveTwoTMUs
) {
508 ti
->mmMode
= GR_MIPMAP_NEAREST
;
509 ti
->LODblend
= FXTRUE
;
512 ti
->mmMode
= GR_MIPMAP_NEAREST_DITHER
;
513 ti
->LODblend
= FXFALSE
;
515 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
518 /* XXX Voodoo3/Banshee mipmap blending seems to produce
519 * incorrectly filtered colors for the smallest mipmap levels.
520 * To work-around we fall-through here and use a different filter.
522 case GL_LINEAR_MIPMAP_NEAREST
:
523 ti
->mmMode
= GR_MIPMAP_NEAREST
;
524 ti
->minFilt
= GR_TEXTUREFILTER_BILINEAR
;
525 ti
->LODblend
= FXFALSE
;
530 ti
->reloadImages
= GL_TRUE
;
531 RevalidateTexture(ctx
, tObj
);
532 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
535 case GL_TEXTURE_MAG_FILTER
:
538 ti
->magFilt
= GR_TEXTUREFILTER_POINT_SAMPLED
;
541 ti
->magFilt
= GR_TEXTUREFILTER_BILINEAR
;
546 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
549 case GL_TEXTURE_WRAP_S
:
551 case GL_CLAMP_TO_BORDER
:
552 case GL_CLAMP_TO_EDGE
:
554 ti
->sClamp
= GR_TEXTURECLAMP_CLAMP
;
557 ti
->sClamp
= GR_TEXTURECLAMP_WRAP
;
559 case GL_MIRRORED_REPEAT
:
560 ti
->sClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
565 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
568 case GL_TEXTURE_WRAP_T
:
570 case GL_CLAMP_TO_BORDER
:
571 case GL_CLAMP_TO_EDGE
:
573 ti
->tClamp
= GR_TEXTURECLAMP_CLAMP
;
576 ti
->tClamp
= GR_TEXTURECLAMP_WRAP
;
578 case GL_MIRRORED_REPEAT
:
579 ti
->tClamp
= GR_TEXTURECLAMP_MIRROR_EXT
;
584 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
587 case GL_TEXTURE_BORDER_COLOR
:
590 case GL_TEXTURE_MIN_LOD
:
593 case GL_TEXTURE_MAX_LOD
:
596 case GL_TEXTURE_BASE_LEVEL
:
597 RevalidateTexture(ctx
, tObj
);
599 case GL_TEXTURE_MAX_LEVEL
:
600 RevalidateTexture(ctx
, tObj
);
610 * Called via glDeleteTextures to delete a texture object.
611 * Here, we delete the Glide data associated with the texture.
614 tdfxDeleteTexture(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
616 if (ctx
&& ctx
->DriverCtx
) {
617 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
618 tdfxTMFreeTexture(fxMesa
, tObj
);
619 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
620 /* Free mipmap images and the texture object itself */
621 _mesa_delete_texture_object(ctx
, tObj
);
627 * Return true if texture is resident, false otherwise.
630 tdfxIsTextureResident(GLcontext
*ctx
, struct gl_texture_object
*tObj
)
632 tdfxTexInfo
*ti
= TDFX_TEXTURE_DATA(tObj
);
633 return (GLboolean
) (ti
&& ti
->isInTM
);
639 * Convert a gl_color_table texture palette to Glide's format.
642 convertPalette(FxU32 data
[256], const struct gl_color_table
*table
)
644 const GLubyte
*tableUB
= table
->TableUB
;
645 GLint width
= table
->Size
;
649 switch (table
->_BaseFormat
) {
651 for (i
= 0; i
< width
; i
++) {
656 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
658 return GR_TEXTABLE_PALETTE_6666_EXT
;
660 for (i
= 0; i
< width
; i
++) {
665 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
667 return GR_TEXTABLE_PALETTE
;
669 for (i
= 0; i
< width
; i
++) {
672 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
674 return GR_TEXTABLE_PALETTE_6666_EXT
;
675 case GL_LUMINANCE_ALPHA
:
676 for (i
= 0; i
< width
; i
++) {
677 r
= g
= b
= tableUB
[i
* 2 + 0];
678 a
= tableUB
[i
* 2 + 1];
679 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
681 return GR_TEXTABLE_PALETTE_6666_EXT
;
683 for (i
= 0; i
< width
; i
++) {
684 r
= tableUB
[i
* 3 + 0];
685 g
= tableUB
[i
* 3 + 1];
686 b
= tableUB
[i
* 3 + 2];
688 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
690 return GR_TEXTABLE_PALETTE
;
692 for (i
= 0; i
< width
; i
++) {
693 r
= tableUB
[i
* 4 + 0];
694 g
= tableUB
[i
* 4 + 1];
695 b
= tableUB
[i
* 4 + 2];
696 a
= tableUB
[i
* 4 + 3];
697 data
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
699 return GR_TEXTABLE_PALETTE_6666_EXT
;
701 /* XXX fixme: how can this happen? */
702 _mesa_error(NULL
, GL_INVALID_ENUM
, "convertPalette: table->Format == %s",
703 _mesa_lookup_enum_by_nr(table
->Format
));
704 return GR_TEXTABLE_PALETTE
;
711 tdfxUpdateTexturePalette(GLcontext
* ctx
, struct gl_texture_object
*tObj
)
713 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
716 /* per-texture palette */
719 /* This might be a proxy texture. */
720 if (!tObj
->Palette
.TableUB
)
723 if (!tObj
->DriverData
)
724 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
725 ti
= TDFX_TEXTURE_DATA(tObj
);
727 ti
->paltype
= convertPalette(ti
->palette
.data
, &tObj
->Palette
);
728 /*tdfxTexInvalidate(ctx, tObj);*/
731 /* global texture palette */
732 fxMesa
->TexPalette
.Type
= convertPalette(fxMesa
->glbPalette
.data
, &ctx
->Texture
.Palette
);
733 fxMesa
->TexPalette
.Data
= &(fxMesa
->glbPalette
.data
);
734 fxMesa
->dirty
|= TDFX_UPLOAD_TEXTURE_PALETTE
;
736 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX too heavy-handed */
740 /**********************************************************************/
741 /**** NEW TEXTURE IMAGE FUNCTIONS ****/
742 /**********************************************************************/
745 static FxBool TexusFatalError
= FXFALSE
;
746 static FxBool TexusError
= FXFALSE
;
748 #define TX_DITHER_NONE 0x00000000
751 fxTexusError(const char *string
, FxBool fatal
)
753 _mesa_problem(NULL
, string
);
755 * Just propagate the fatal value up.
758 TexusFatalError
= fatal
;
763 static const struct gl_texture_format
*
764 tdfxChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
765 GLenum srcFormat
, GLenum srcType
)
767 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
768 const GLboolean allow32bpt
= TDFX_IS_NAPALM(fxMesa
);
770 switch (internalFormat
) {
776 case GL_COMPRESSED_ALPHA
:
777 return &_mesa_texformat_a8
;
784 case GL_COMPRESSED_LUMINANCE
:
785 return &_mesa_texformat_l8
;
787 case GL_LUMINANCE_ALPHA
:
788 case GL_LUMINANCE4_ALPHA4
:
789 case GL_LUMINANCE6_ALPHA2
:
790 case GL_LUMINANCE8_ALPHA8
:
791 case GL_LUMINANCE12_ALPHA4
:
792 case GL_LUMINANCE12_ALPHA12
:
793 case GL_LUMINANCE16_ALPHA16
:
794 case GL_COMPRESSED_LUMINANCE_ALPHA
:
795 return &_mesa_texformat_al88
;
801 case GL_COMPRESSED_INTENSITY
:
802 return &_mesa_texformat_i8
;
806 return &_mesa_texformat_rgb565
;
807 case GL_COMPRESSED_RGB
:
808 /* intentional fall-through */
811 if ( srcFormat
== GL_RGB
&& srcType
== GL_UNSIGNED_SHORT_5_6_5
) {
812 return &_mesa_texformat_rgb565
;
814 /* intentional fall through */
819 return (allow32bpt
) ? &_mesa_texformat_argb8888
820 : &_mesa_texformat_rgb565
;
823 return &_mesa_texformat_argb4444
;
824 case GL_COMPRESSED_RGBA
:
825 /* intentional fall-through */
828 if ( srcFormat
== GL_BGRA
) {
829 if ( srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
830 return &_mesa_texformat_argb8888
;
832 else if ( srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
833 return &_mesa_texformat_argb4444
;
835 else if ( srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
836 return &_mesa_texformat_argb1555
;
839 /* intentional fall through */
844 return allow32bpt
? &_mesa_texformat_argb8888
845 : &_mesa_texformat_argb4444
;
847 return &_mesa_texformat_argb1555
;
849 case GL_COLOR_INDEX1_EXT
:
850 case GL_COLOR_INDEX2_EXT
:
851 case GL_COLOR_INDEX4_EXT
:
852 case GL_COLOR_INDEX8_EXT
:
853 case GL_COLOR_INDEX12_EXT
:
854 case GL_COLOR_INDEX16_EXT
:
855 return &_mesa_texformat_ci8
;
856 /* GL_EXT_texture_compression_s3tc */
858 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
861 return &_mesa_texformat_rgb_dxt1
;
862 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
863 return &_mesa_texformat_rgba_dxt1
;
864 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
867 return &_mesa_texformat_rgba_dxt3
;
868 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
869 return &_mesa_texformat_rgba_dxt5
;
870 /* GL_3DFX_texture_compression_FXT1 */
871 case GL_COMPRESSED_RGB_FXT1_3DFX
:
872 return &_mesa_texformat_rgb_fxt1
;
873 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
874 return &_mesa_texformat_rgba_fxt1
;
876 _mesa_problem(ctx
, "unexpected format in tdfxChooseTextureFormat");
883 * Return the Glide format for the given mesa texture format.
885 static GrTextureFormat_t
886 fxGlideFormat(GLint mesaFormat
)
888 switch (mesaFormat
) {
890 return GR_TEXFMT_ALPHA_8
;
892 return GR_TEXFMT_ALPHA_8
;
894 return GR_TEXFMT_INTENSITY_8
;
895 case MESA_FORMAT_CI8
:
896 return GR_TEXFMT_P_8
;
897 case MESA_FORMAT_AL88
:
898 return GR_TEXFMT_ALPHA_INTENSITY_88
;
899 case MESA_FORMAT_RGB565
:
900 return GR_TEXFMT_RGB_565
;
901 case MESA_FORMAT_ARGB4444
:
902 return GR_TEXFMT_ARGB_4444
;
903 case MESA_FORMAT_ARGB1555
:
904 return GR_TEXFMT_ARGB_1555
;
905 case MESA_FORMAT_ARGB8888
:
906 return GR_TEXFMT_ARGB_8888
;
907 case MESA_FORMAT_RGB_FXT1
:
908 case MESA_FORMAT_RGBA_FXT1
:
909 return GR_TEXFMT_ARGB_CMP_FXT1
;
910 case MESA_FORMAT_RGB_DXT1
:
911 case MESA_FORMAT_RGBA_DXT1
:
912 return GR_TEXFMT_ARGB_CMP_DXT1
;
913 case MESA_FORMAT_RGBA_DXT3
:
914 return GR_TEXFMT_ARGB_CMP_DXT3
;
915 case MESA_FORMAT_RGBA_DXT5
:
916 return GR_TEXFMT_ARGB_CMP_DXT5
;
918 _mesa_problem(NULL
, "Unexpected format in fxGlideFormat");
924 /* Texel-fetch functions for software texturing and glGetTexImage().
925 * We should have been able to use some "standard" fetch functions (which
926 * may get defined in texutil.c) but we have to account for scaled texture
927 * images on tdfx hardware (the 8:1 aspect ratio limit).
928 * Hence, we need special functions here.
931 fxt1_decode_1 (const void *texture
, int width
,
932 int i
, int j
, unsigned char *rgba
);
935 fetch_intensity8(const struct gl_texture_image
*texImage
,
936 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
938 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
939 const GLubyte
*texel
;
944 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
945 rgba
[RCOMP
] = *texel
;
946 rgba
[GCOMP
] = *texel
;
947 rgba
[BCOMP
] = *texel
;
948 rgba
[ACOMP
] = *texel
;
953 fetch_luminance8(const struct gl_texture_image
*texImage
,
954 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
956 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
957 const GLubyte
*texel
;
962 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
963 rgba
[RCOMP
] = *texel
;
964 rgba
[GCOMP
] = *texel
;
965 rgba
[BCOMP
] = *texel
;
971 fetch_alpha8(const struct gl_texture_image
*texImage
,
972 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
974 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
975 const GLubyte
*texel
;
980 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
984 rgba
[ACOMP
] = *texel
;
989 fetch_index8(const struct gl_texture_image
*texImage
,
990 GLint i
, GLint j
, GLint k
, GLchan
* indexOut
)
992 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
993 const GLubyte
*texel
;
998 texel
= ((GLubyte
*) texImage
->Data
) + j
* mml
->width
+ i
;
1004 fetch_luminance8_alpha8(const struct gl_texture_image
*texImage
,
1005 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1007 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1008 const GLubyte
*texel
;
1010 i
= i
* mml
->wScale
;
1011 j
= j
* mml
->hScale
;
1013 texel
= ((GLubyte
*) texImage
->Data
) + (j
* mml
->width
+ i
) * 2;
1014 rgba
[RCOMP
] = texel
[0];
1015 rgba
[GCOMP
] = texel
[0];
1016 rgba
[BCOMP
] = texel
[0];
1017 rgba
[ACOMP
] = texel
[1];
1022 fetch_r5g6b5(const struct gl_texture_image
*texImage
,
1023 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1025 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1026 const GLushort
*texel
;
1028 i
= i
* mml
->wScale
;
1029 j
= j
* mml
->hScale
;
1031 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1032 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
1033 rgba
[GCOMP
] = (((*texel
) >> 5) & 0x3f) * 255 / 63;
1034 rgba
[BCOMP
] = (((*texel
) >> 0) & 0x1f) * 255 / 31;
1040 fetch_r4g4b4a4(const struct gl_texture_image
*texImage
,
1041 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1043 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1044 const GLushort
*texel
;
1046 i
= i
* mml
->wScale
;
1047 j
= j
* mml
->hScale
;
1049 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1050 rgba
[RCOMP
] = (((*texel
) >> 12) & 0xf) * 255 / 15;
1051 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xf) * 255 / 15;
1052 rgba
[BCOMP
] = (((*texel
) >> 4) & 0xf) * 255 / 15;
1053 rgba
[ACOMP
] = (((*texel
) >> 0) & 0xf) * 255 / 15;
1058 fetch_r5g5b5a1(const struct gl_texture_image
*texImage
,
1059 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1061 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1062 const GLushort
*texel
;
1064 i
= i
* mml
->wScale
;
1065 j
= j
* mml
->hScale
;
1067 texel
= ((GLushort
*) texImage
->Data
) + j
* mml
->width
+ i
;
1068 rgba
[RCOMP
] = (((*texel
) >> 11) & 0x1f) * 255 / 31;
1069 rgba
[GCOMP
] = (((*texel
) >> 6) & 0x1f) * 255 / 31;
1070 rgba
[BCOMP
] = (((*texel
) >> 1) & 0x1f) * 255 / 31;
1071 rgba
[ACOMP
] = (((*texel
) >> 0) & 0x01) * 255;
1076 fetch_a8r8g8b8(const struct gl_texture_image
*texImage
,
1077 GLint i
, GLint j
, GLint k
, GLchan
* rgba
)
1079 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1080 const GLuint
*texel
;
1082 i
= i
* mml
->wScale
;
1083 j
= j
* mml
->hScale
;
1085 texel
= ((GLuint
*) texImage
->Data
) + j
* mml
->width
+ i
;
1086 rgba
[RCOMP
] = (((*texel
) >> 16) & 0xff);
1087 rgba
[GCOMP
] = (((*texel
) >> 8) & 0xff);
1088 rgba
[BCOMP
] = (((*texel
) ) & 0xff);
1089 rgba
[ACOMP
] = (((*texel
) >> 24) & 0xff);
1094 fetch_rgb_fxt1(const struct gl_texture_image
*texImage
,
1095 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1097 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1099 i
= i
* mml
->wScale
;
1100 j
= j
* mml
->hScale
;
1102 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1108 fetch_rgba_fxt1(const struct gl_texture_image
*texImage
,
1109 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1111 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1113 i
= i
* mml
->wScale
;
1114 j
= j
* mml
->hScale
;
1116 fxt1_decode_1(texImage
->Data
, mml
->width
, i
, j
, rgba
);
1121 fetch_rgb_dxt1(const struct gl_texture_image
*texImage
,
1122 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1124 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1126 i
= i
* mml
->wScale
;
1127 j
= j
* mml
->hScale
;
1129 _mesa_texformat_rgb_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1134 fetch_rgba_dxt1(const struct gl_texture_image
*texImage
,
1135 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1137 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1139 i
= i
* mml
->wScale
;
1140 j
= j
* mml
->hScale
;
1142 _mesa_texformat_rgba_dxt1
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1147 fetch_rgba_dxt3(const struct gl_texture_image
*texImage
,
1148 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1150 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1152 i
= i
* mml
->wScale
;
1153 j
= j
* mml
->hScale
;
1155 _mesa_texformat_rgba_dxt3
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1160 fetch_rgba_dxt5(const struct gl_texture_image
*texImage
,
1161 GLint i
, GLint j
, GLint k
, GLchan
*rgba
)
1163 const tdfxMipMapLevel
*mml
= TDFX_TEXIMAGE_DATA(texImage
);
1165 i
= i
* mml
->wScale
;
1166 j
= j
* mml
->hScale
;
1168 _mesa_texformat_rgba_dxt5
.FetchTexel2D(texImage
, i
, j
, k
, rgba
);
1172 static FetchTexelFuncC
1173 fxFetchFunction(GLint mesaFormat
)
1175 switch (mesaFormat
) {
1176 case MESA_FORMAT_I8
:
1177 return &fetch_intensity8
;
1178 case MESA_FORMAT_A8
:
1179 return &fetch_alpha8
;
1180 case MESA_FORMAT_L8
:
1181 return &fetch_luminance8
;
1182 case MESA_FORMAT_CI8
:
1183 return &fetch_index8
;
1184 case MESA_FORMAT_AL88
:
1185 return &fetch_luminance8_alpha8
;
1186 case MESA_FORMAT_RGB565
:
1187 return &fetch_r5g6b5
;
1188 case MESA_FORMAT_ARGB4444
:
1189 return &fetch_r4g4b4a4
;
1190 case MESA_FORMAT_ARGB1555
:
1191 return &fetch_r5g5b5a1
;
1192 case MESA_FORMAT_ARGB8888
:
1193 return &fetch_a8r8g8b8
;
1194 case MESA_FORMAT_RGB_FXT1
:
1195 return &fetch_rgb_fxt1
;
1196 case MESA_FORMAT_RGBA_FXT1
:
1197 return &fetch_rgba_fxt1
;
1198 case MESA_FORMAT_RGB_DXT1
:
1199 return &fetch_rgb_dxt1
;
1200 case MESA_FORMAT_RGBA_DXT1
:
1201 return &fetch_rgba_dxt1
;
1202 case MESA_FORMAT_RGBA_DXT3
:
1203 return &fetch_rgba_dxt3
;
1204 case MESA_FORMAT_RGBA_DXT5
:
1205 return &fetch_rgba_dxt5
;
1207 _mesa_problem(NULL
, "Unexpected format in fxFetchFunction");
1214 adjust2DRatio (GLcontext
*ctx
,
1215 GLint xoffset
, GLint yoffset
,
1216 GLint width
, GLint height
,
1217 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1218 const struct gl_pixelstore_attrib
*packing
,
1219 tdfxMipMapLevel
*mml
,
1220 struct gl_texture_image
*texImage
,
1224 const GLint newWidth
= width
* mml
->wScale
;
1225 const GLint newHeight
= height
* mml
->hScale
;
1227 GLuint dstImageOffsets
= 0;
1229 if (!texImage
->IsCompressed
) {
1231 tempImage
= MALLOC(width
* height
* texelBytes
);
1236 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1237 texImage
->TexFormat
, tempImage
,
1238 0, 0, 0, /* dstX/Y/Zoffset */
1239 width
* texelBytes
, /* dstRowStride */
1242 format
, type
, pixels
, packing
);
1245 /* compute address of dest subimage within the overal tex image */
1246 destAddr
= (GLubyte
*) texImage
->Data
1247 + (yoffset
* mml
->hScale
* mml
->width
1248 + xoffset
* mml
->wScale
) * texelBytes
;
1250 _mesa_rescale_teximage2d(texelBytes
,
1252 dstRowStride
, /* dst stride */
1254 newWidth
, newHeight
,
1255 tempImage
, destAddr
);
1257 const GLint rawBytes
= 4;
1258 GLvoid
*rawImage
= MALLOC(width
* height
* rawBytes
);
1262 tempImage
= MALLOC(newWidth
* newHeight
* rawBytes
);
1267 /* unpack image, apply transfer ops and store in rawImage */
1268 _mesa_texstore_rgba8888(ctx
, 2, GL_RGBA
,
1269 &_mesa_texformat_rgba8888_rev
, rawImage
,
1270 0, 0, 0, /* dstX/Y/Zoffset */
1271 width
* rawBytes
, /* dstRowStride */
1274 format
, type
, pixels
, packing
);
1275 _mesa_rescale_teximage2d(rawBytes
,
1277 newWidth
* rawBytes
, /* dst stride */
1278 width
, height
, /* src */
1279 newWidth
, newHeight
, /* dst */
1280 rawImage
/*src*/, tempImage
/*dst*/ );
1281 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1282 texImage
->TexFormat
, texImage
->Data
,
1283 xoffset
* mml
->wScale
, yoffset
* mml
->hScale
, 0, /* dstX/Y/Zoffset */
1286 newWidth
, newHeight
, 1,
1287 GL_RGBA
, CHAN_TYPE
, tempImage
, &ctx
->DefaultPacking
);
1298 tdfxTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1299 GLint internalFormat
, GLint width
, GLint height
, GLint border
,
1300 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1301 const struct gl_pixelstore_attrib
*packing
,
1302 struct gl_texture_object
*texObj
,
1303 struct gl_texture_image
*texImage
)
1305 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1307 tdfxMipMapLevel
*mml
;
1308 GLint texelBytes
, dstRowStride
;
1312 printf("TexImage id=%d int 0x%x format 0x%x type 0x%x %dx%d\n",
1313 texObj->Name, texImage->InternalFormat, format, type,
1314 texImage->Width, texImage->Height);
1317 ti
= TDFX_TEXTURE_DATA(texObj
);
1319 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1320 if (!texObj
->DriverData
) {
1321 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1324 ti
= TDFX_TEXTURE_DATA(texObj
);
1328 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1330 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1331 if (!texImage
->DriverData
) {
1332 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1335 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1338 /* Determine width and height scale factors for texture.
1339 * Remember, Glide is limited to 8:1 aspect ratios.
1342 texImage
->Width
, texImage
->Height
,
1343 NULL
, /* lod level */
1344 NULL
, /* aspect ratio */
1345 NULL
, NULL
, /* sscale, tscale */
1346 &mml
->wScale
, &mml
->hScale
);
1348 /* rescaled size: */
1349 mml
->width
= width
* mml
->wScale
;
1350 mml
->height
= height
* mml
->hScale
;
1352 #if FX_COMPRESS_S3TC_AS_FXT1_HACK
1353 /* [koolsmoky] substitute FXT1 for DXTn and Legacy S3TC */
1354 /* [dBorca] we should update texture's attribute, then,
1355 * because if the application asks us to decompress, we
1356 * have to know the REAL format! Also, DXT3/5 might not
1357 * be correct, since it would mess with "compressedSize".
1358 * Ditto for GL_RGBA[4]_S3TC, which is always mapped to DXT3.
1360 if (texImage
->IsCompressed
) {
1361 switch (internalFormat
) {
1362 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1365 internalFormat
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1367 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1368 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1369 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1372 internalFormat
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1374 texImage
->InternalFormat
= internalFormat
;
1378 if (fxMesa
->type
>= GR_SSTTYPE_Voodoo4
) {
1379 GLenum texNapalm
= 0;
1380 if (internalFormat
== GL_COMPRESSED_RGB
) {
1381 texNapalm
= GL_COMPRESSED_RGB_FXT1_3DFX
;
1382 } else if (internalFormat
== GL_COMPRESSED_RGBA
) {
1383 texNapalm
= GL_COMPRESSED_RGBA_FXT1_3DFX
;
1386 texImage
->InternalFormat
= internalFormat
= texNapalm
;
1387 texImage
->IsCompressed
= GL_TRUE
;
1392 /* choose the texture format */
1393 assert(ctx
->Driver
.ChooseTextureFormat
);
1394 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1395 internalFormat
, format
, type
);
1396 assert(texImage
->TexFormat
);
1397 mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1398 mml
->glideFormat
= fxGlideFormat(mesaFormat
);
1399 ti
->info
.format
= mml
->glideFormat
;
1400 texImage
->FetchTexelc
= fxFetchFunction(mesaFormat
);
1401 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1403 if (texImage
->IsCompressed
) {
1404 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1409 dstRowStride
= _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, mml
->width
);
1410 texImage
->Data
= _mesa_alloc_texmemory(texImage
->CompressedSize
);
1412 dstRowStride
= mml
->width
* texelBytes
;
1413 texImage
->Data
= _mesa_alloc_texmemory(mml
->width
* mml
->height
* texelBytes
);
1415 if (!texImage
->Data
) {
1416 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1420 if (pixels
!= NULL
) {
1421 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1422 /* rescale image to overcome 1:8 aspect limitation */
1423 if (!adjust2DRatio(ctx
,
1426 format
, type
, pixels
,
1433 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1438 /* no rescaling needed */
1439 /* unpack image, apply transfer ops and store in texImage->Data */
1440 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1441 texImage
->TexFormat
, texImage
->Data
,
1442 0, 0, 0, /* dstX/Y/Zoffset */
1444 texImage
->ImageOffsets
,
1446 format
, type
, pixels
, packing
);
1450 RevalidateTexture(ctx
, texObj
);
1452 ti
->reloadImages
= GL_TRUE
;
1453 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1458 tdfxTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1459 GLint xoffset
, GLint yoffset
,
1460 GLsizei width
, GLsizei height
,
1461 GLenum format
, GLenum type
,
1462 const GLvoid
*pixels
,
1463 const struct gl_pixelstore_attrib
*packing
,
1464 struct gl_texture_object
*texObj
,
1465 struct gl_texture_image
*texImage
)
1467 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1469 tdfxMipMapLevel
*mml
;
1470 GLint texelBytes
, dstRowStride
;
1472 if (!texObj
->DriverData
) {
1473 _mesa_problem(ctx
, "problem in fxDDTexSubImage2D");
1477 ti
= TDFX_TEXTURE_DATA(texObj
);
1479 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1482 assert(texImage
->Data
); /* must have an existing texture image! */
1483 assert(texImage
->_BaseFormat
);
1485 texelBytes
= texImage
->TexFormat
->TexelBytes
;
1486 if (texImage
->IsCompressed
) {
1487 dstRowStride
= _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, mml
->width
);
1489 dstRowStride
= mml
->width
* texelBytes
;
1492 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1493 /* need to rescale subimage to match mipmap level's rescale factors */
1494 if (!adjust2DRatio(ctx
,
1497 format
, type
, pixels
,
1504 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
1509 /* no rescaling needed */
1510 texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->_BaseFormat
,
1511 texImage
->TexFormat
, texImage
->Data
,
1512 xoffset
, yoffset
, 0,
1514 texImage
->ImageOffsets
,
1516 format
, type
, pixels
, packing
);
1519 ti
->reloadImages
= GL_TRUE
; /* signal the image needs to be reloaded */
1520 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
; /* XXX this might be a bit much */
1525 tdfxTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1526 GLint internalFormat
, GLint width
, GLint border
,
1527 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1528 const struct gl_pixelstore_attrib
*packing
,
1529 struct gl_texture_object
*texObj
,
1530 struct gl_texture_image
*texImage
)
1532 tdfxTexImage2D(ctx
, target
, level
,
1533 internalFormat
, width
, 1, border
,
1534 format
, type
, pixels
,
1541 tdfxTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1544 GLenum format
, GLenum type
,
1545 const GLvoid
*pixels
,
1546 const struct gl_pixelstore_attrib
*packing
,
1547 struct gl_texture_object
*texObj
,
1548 struct gl_texture_image
*texImage
)
1550 tdfxTexSubImage2D(ctx
, target
, level
,
1560 /**********************************************************************/
1561 /**** COMPRESSED TEXTURE IMAGE FUNCTIONS ****/
1562 /**********************************************************************/
1565 tdfxCompressedTexImage2D (GLcontext
*ctx
, GLenum target
,
1566 GLint level
, GLint internalFormat
,
1567 GLsizei width
, GLsizei height
, GLint border
,
1568 GLsizei imageSize
, const GLvoid
*data
,
1569 struct gl_texture_object
*texObj
,
1570 struct gl_texture_image
*texImage
)
1572 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1574 tdfxMipMapLevel
*mml
;
1577 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1578 fprintf(stderr
, "tdfxCompressedTexImage2D: id=%d int 0x%x %dx%d\n",
1579 texObj
->Name
, internalFormat
,
1583 if ((target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_2D
) || texImage
->Border
> 0) {
1584 _mesa_problem(NULL
, "tdfx: unsupported texture in tdfxCompressedTexImg()\n");
1588 assert(texImage
->IsCompressed
);
1590 ti
= TDFX_TEXTURE_DATA(texObj
);
1592 texObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1593 if (!texObj
->DriverData
) {
1594 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1597 ti
= TDFX_TEXTURE_DATA(texObj
);
1601 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1603 texImage
->DriverData
= CALLOC(sizeof(tdfxMipMapLevel
));
1604 if (!texImage
->DriverData
) {
1605 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1608 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1611 tdfxTexGetInfo(ctx
, width
, height
, NULL
, NULL
, NULL
, NULL
,
1612 &mml
->wScale
, &mml
->hScale
);
1614 mml
->width
= width
* mml
->wScale
;
1615 mml
->height
= height
* mml
->hScale
;
1618 /* choose the texture format */
1619 assert(ctx
->Driver
.ChooseTextureFormat
);
1620 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1621 internalFormat
, -1/*format*/, -1/*type*/);
1622 assert(texImage
->TexFormat
);
1624 /* Determine the appropriate Glide texel format,
1625 * given the user's internal texture format hint.
1627 mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1628 mml
->glideFormat
= fxGlideFormat(mesaFormat
);
1629 ti
->info
.format
= mml
->glideFormat
;
1630 texImage
->FetchTexelc
= fxFetchFunction(mesaFormat
);
1632 /* allocate new storage for texture image, if needed */
1633 if (!texImage
->Data
) {
1634 texImage
->CompressedSize
= _mesa_compressed_texture_size(ctx
,
1639 texImage
->Data
= _mesa_alloc_texmemory(texImage
->CompressedSize
);
1640 if (!texImage
->Data
) {
1641 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
1646 /* save the texture data */
1647 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1648 /* [dBorca] Hack alert:
1649 * now we're screwed. We can't decompress,
1650 * unless we do it in HW (via textureBuffer).
1651 * We still have some chances:
1652 * 1) we got FXT1 textures - we CAN decompress, rescale for
1653 * aspectratio, then compress back.
1654 * 2) there is a chance that MIN("s", "t") won't be overflowed.
1655 * Thus, we don't care about textureclamp and we could lower
1656 * MIN("uscale", "vscale") below 32. We still have to have
1657 * our data aligned inside a 8:1 rectangle.
1658 * 3) just in case if MIN("s", "t") gets overflowed with GL_REPEAT,
1659 * we replicate the data over the padded area.
1660 * For now, we take 2) + 3) but texelfetchers will be wrong!
1662 const GLuint mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1663 GLuint srcRowStride
= _mesa_compressed_row_stride(mesaFormat
, width
);
1665 GLuint destRowStride
= _mesa_compressed_row_stride(mesaFormat
,
1668 _mesa_upscale_teximage2d(srcRowStride
, (height
+3) / 4,
1669 destRowStride
, (mml
->height
+3) / 4,
1670 1, data
, srcRowStride
,
1672 ti
->padded
= GL_TRUE
;
1674 MEMCPY(texImage
->Data
, data
, texImage
->CompressedSize
);
1677 RevalidateTexture(ctx
, texObj
);
1679 ti
->reloadImages
= GL_TRUE
;
1680 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1685 tdfxCompressedTexSubImage2D( GLcontext
*ctx
, GLenum target
,
1686 GLint level
, GLint xoffset
,
1687 GLint yoffset
, GLsizei width
,
1688 GLint height
, GLenum format
,
1689 GLsizei imageSize
, const GLvoid
*data
,
1690 struct gl_texture_object
*texObj
,
1691 struct gl_texture_image
*texImage
)
1693 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1695 tdfxMipMapLevel
*mml
;
1696 GLint destRowStride
, srcRowStride
;
1699 const GLuint mesaFormat
= texImage
->TexFormat
->MesaFormat
;
1701 if (TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
1702 fprintf(stderr
, "tdfxCompressedTexSubImage2D: id=%d\n", texObj
->Name
);
1705 ti
= TDFX_TEXTURE_DATA(texObj
);
1707 mml
= TDFX_TEXIMAGE_DATA(texImage
);
1710 srcRowStride
= _mesa_compressed_row_stride(mesaFormat
, width
);
1712 destRowStride
= _mesa_compressed_row_stride(mesaFormat
, mml
->width
);
1713 dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
1716 (GLubyte
*) texImage
->Data
);
1718 rows
= height
/ 4; /* [dBorca] hardcoded 4, but works for FXT1/DXTC */
1720 for (i
= 0; i
< rows
; i
++) {
1721 MEMCPY(dest
, data
, srcRowStride
);
1722 dest
+= destRowStride
;
1723 data
= (GLvoid
*)((intptr_t)data
+ (intptr_t)srcRowStride
);
1726 /* [dBorca] Hack alert:
1727 * see fxDDCompressedTexImage2D for caveats
1729 if (mml
->wScale
!= 1 || mml
->hScale
!= 1) {
1730 srcRowStride
= _mesa_compressed_row_stride(mesaFormat
, texImage
->Width
);
1732 destRowStride
= _mesa_compressed_row_stride(mesaFormat
, mml
->width
);
1733 _mesa_upscale_teximage2d(srcRowStride
, texImage
->Height
/ 4,
1734 destRowStride
, mml
->height
/ 4,
1735 1, texImage
->Data
, destRowStride
,
1739 RevalidateTexture(ctx
, texObj
);
1741 ti
->reloadImages
= GL_TRUE
;
1742 fxMesa
->new_state
|= TDFX_NEW_TEXTURE
;
1748 PrintTexture(int w
, int h
, int c
, const GLubyte
* data
)
1751 for (i
= 0; i
< h
; i
++) {
1752 for (j
= 0; j
< w
; j
++) {
1754 printf("%02x %02x ", data
[0], data
[1]);
1756 printf("%02x %02x %02x ", data
[0], data
[1], data
[2]);
1766 tdfxTestProxyTexImage(GLcontext
*ctx
, GLenum target
,
1767 GLint level
, GLint internalFormat
,
1768 GLenum format
, GLenum type
,
1769 GLint width
, GLint height
,
1770 GLint depth
, GLint border
)
1772 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1773 struct gl_shared_state
*mesaShared
= fxMesa
->glCtx
->Shared
;
1774 struct tdfxSharedState
*shared
= (struct tdfxSharedState
*) mesaShared
->DriverData
;
1777 case GL_PROXY_TEXTURE_1D
:
1779 case GL_PROXY_TEXTURE_2D
:
1781 struct gl_texture_object
*tObj
;
1785 tObj
= ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
1786 if (!tObj
->DriverData
)
1787 tObj
->DriverData
= fxAllocTexObjData(fxMesa
);
1788 ti
= TDFX_TEXTURE_DATA(tObj
);
1791 /* assign the parameters to test against */
1792 tObj
->Image
[0][level
]->Width
= width
;
1793 tObj
->Image
[0][level
]->Height
= height
;
1794 tObj
->Image
[0][level
]->Border
= border
;
1796 tObj
->Image
[0][level
]->InternalFormat
= internalFormat
;
1799 /* don't use mipmap levels > 0 */
1800 tObj
->MinFilter
= tObj
->MagFilter
= GL_NEAREST
;
1803 /* test with all mipmap levels */
1804 tObj
->MinFilter
= GL_LINEAR_MIPMAP_LINEAR
;
1805 tObj
->MagFilter
= GL_NEAREST
;
1807 RevalidateTexture(ctx
, tObj
);
1810 printf("small lodlog2 0x%x\n", ti->info.smallLodLog2);
1811 printf("large lodlog2 0x%x\n", ti->info.largeLodLog2);
1812 printf("aspect ratio 0x%x\n", ti->info.aspectRatioLog2);
1813 printf("glide format 0x%x\n", ti->info.format);
1814 printf("data %p\n", ti->info.data);
1815 printf("lodblend %d\n", (int) ti->LODblend);
1818 /* determine where texture will reside */
1819 if (ti
->LODblend
&& !shared
->umaTexMemory
) {
1820 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1821 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1822 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1825 /* XXX GR_MIPMAPLEVELMASK_BOTH might not be right, but works */
1826 memNeeded
= fxMesa
->Glide
.grTexTextureMemRequired(
1827 GR_MIPMAPLEVELMASK_BOTH
, &(ti
->info
));
1830 printf("Proxy test %d > %d\n", memNeeded, shared->totalTexMem[0]);
1832 if (memNeeded
> shared
->totalTexMem
[0])
1837 case GL_PROXY_TEXTURE_3D
:
1838 return GL_TRUE
; /* software rendering */
1840 return GL_TRUE
; /* never happens, silence compiler */
1846 * Allocate a new texture object.
1847 * Called via ctx->Driver.NewTextureObject.
1848 * Note: this function will be called during context creation to
1849 * allocate the default texture objects.
1850 * Note: we could use containment here to 'derive' the driver-specific
1851 * texture object from the core mesa gl_texture_object. Not done at this time.
1853 static struct gl_texture_object
*
1854 tdfxNewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
1856 struct gl_texture_object
*obj
;
1857 obj
= _mesa_new_texture_object(ctx
, name
, target
);
1862 void tdfxInitTextureFuncs( struct dd_function_table
*functions
)
1864 functions
->BindTexture
= tdfxBindTexture
;
1865 functions
->NewTextureObject
= tdfxNewTextureObject
;
1866 functions
->DeleteTexture
= tdfxDeleteTexture
;
1867 functions
->TexEnv
= tdfxTexEnv
;
1868 functions
->TexParameter
= tdfxTexParameter
;
1869 functions
->ChooseTextureFormat
= tdfxChooseTextureFormat
;
1870 functions
->TexImage1D
= tdfxTexImage1D
;
1871 functions
->TexSubImage1D
= tdfxTexSubImage1D
;
1872 functions
->TexImage2D
= tdfxTexImage2D
;
1873 functions
->TexSubImage2D
= tdfxTexSubImage2D
;
1874 functions
->IsTextureResident
= tdfxIsTextureResident
;
1875 functions
->CompressedTexImage2D
= tdfxCompressedTexImage2D
;
1876 functions
->CompressedTexSubImage2D
= tdfxCompressedTexSubImage2D
;
1877 functions
->UpdateTexturePalette
= tdfxUpdateTexturePalette
;
1878 functions
->GenerateMipmap
= tdfxGenerateMipmap
;