Fixed and cleaned up programming of watermark registers. There may be
[mesa.git] / src / mesa / drivers / dri / savage / savagetex.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include <GL/gl.h>
30
31 #include "mm.h"
32 #include "savagecontext.h"
33 #include "savagetex.h"
34 #include "savagetris.h"
35 #include "savageioctl.h"
36 #include "simple_list.h"
37 #include "enums.h"
38 #include "savage_bci.h"
39
40 #include "macros.h"
41 #include "texformat.h"
42 #include "texstore.h"
43 #include "texobj.h"
44
45 #include "convolve.h"
46 #include "colormac.h"
47
48 #include "swrast/swrast.h"
49
50 #include "xmlpool.h"
51
52 #define TILE_INDEX_DXT1 0
53 #define TILE_INDEX_8 1
54 #define TILE_INDEX_16 2
55 #define TILE_INDEX_DXTn 3
56 #define TILE_INDEX_32 4
57
58 /* On Savage4 the texure LOD-bias needs an offset of ~ 0.3 to get
59 * somewhere close to software rendering.
60 */
61 #define SAVAGE4_LOD_OFFSET 10
62
63 /* Tile info for S3TC formats counts in 4x4 blocks instead of texels.
64 * In DXT1 each block is encoded in 64 bits. In DXT3 and 5 each block is
65 * encoded in 128 bits. */
66
67 /* Size 1, 2 and 4 images are packed into the last subtile. Each image
68 * is repeated to fill a 4x4 pixel area. The figure below shows the
69 * layout of those 4x4 pixel areas in the 8x8 subtile.
70 *
71 * 4 2
72 * x 1
73 *
74 * Yuck! 8-bit texture formats use 4x8 subtiles. See below.
75 */
76 static const savageTileInfo tileInfo_pro[5] = {
77 {16, 16, 16, 8, 1, 2, {0x18, 0x10}}, /* DXT1 */
78 {64, 32, 16, 4, 4, 8, {0x30, 0x20}}, /* 8-bit */
79 {64, 16, 8, 2, 8, 8, {0x48, 0x08}}, /* 16-bit */
80 {16, 8, 16, 4, 1, 2, {0x30, 0x20}}, /* DXT3, DXT5 */
81 {32, 16, 4, 2, 8, 8, {0x90, 0x10}}, /* 32-bit */
82 };
83
84 /* Size 1, 2 and 4 images are packed into the last two subtiles. Each
85 * image is repeated to fill a 4x4 pixel area. The figures below show
86 * the layout of those 4x4 pixel areas in the two 4x8 subtiles.
87 *
88 * second last subtile: 4 last subtile: 2
89 * x 1
90 */
91 static const savageTileInfo tileInfo_s3d_s4[5] = {
92 {16, 16, 16, 8, 1, 2, {0x18, 0x10}}, /* DXT1 */
93 {64, 32, 16, 4, 4, 8, {0x30, 0x20}}, /* 8-bit */
94 {64, 16, 16, 2, 4, 8, {0x60, 0x40}}, /* 16-bit */
95 {16, 8, 16, 4, 1, 2, {0x30, 0x20}}, /* DXT3, DXT5 */
96 {32, 16, 8, 2, 4, 8, {0xc0, 0x80}}, /* 32-bit */
97 };
98
99 /** \brief Template for subtile uploads.
100 * \param h height in pixels
101 * \param w width in bytes
102 */
103 #define SUBTILE_FUNC(w,h) \
104 static __inline GLubyte *savageUploadSubtile_##w##x##h \
105 (GLubyte *dest, GLubyte *src, GLuint srcStride) \
106 { \
107 GLuint y; \
108 for (y = 0; y < h; ++y) { \
109 memcpy (dest, src, w); \
110 src += srcStride; \
111 dest += w; \
112 } \
113 return dest; \
114 }
115
116 SUBTILE_FUNC(2, 8) /* 4 bits per pixel, 4 pixels wide */
117 SUBTILE_FUNC(4, 8)
118 SUBTILE_FUNC(8, 8)
119 SUBTILE_FUNC(16, 8)
120 SUBTILE_FUNC(32, 8) /* 4 bytes per pixel, 8 pixels wide */
121
122 SUBTILE_FUNC(8, 2) /* DXT1 */
123 SUBTILE_FUNC(16, 2) /* DXT3 and DXT5 */
124
125 /** \brief Upload a complete tile from src (srcStride) to dest
126 *
127 * \param tileInfo Pointer to tiling information
128 * \param wInSub Width of source/dest image in subtiles
129 * \param hInSub Height of source/dest image in subtiles
130 * \param bpp Bytes per pixel
131 * \param src Pointer to source data
132 * \param srcStride Byte stride of rows in the source data
133 * \param dest Pointer to destination
134 *
135 * Writes linearly to the destination memory in order to exploit write
136 * combining.
137 *
138 * For a complete tile wInSub and hInSub are set to the same values as
139 * in tileInfo. If the source image is smaller than a whole tile in
140 * one or both dimensions then they are set to the values of the
141 * source image. This only works as long as the source image is bigger
142 * than 8x8 pixels.
143 */
144 static void savageUploadTile (const savageTileInfo *tileInfo,
145 GLuint wInSub, GLuint hInSub, GLuint bpp,
146 GLubyte *src, GLuint srcStride, GLubyte *dest) {
147 GLuint subStride = tileInfo->subWidth * bpp;
148 GLubyte *srcSRow = src, *srcSTile = src;
149 GLubyte *(*subtileFunc) (GLubyte *, GLubyte *, GLuint);
150 GLuint sx, sy;
151 switch (subStride) {
152 case 2: subtileFunc = savageUploadSubtile_2x8; break;
153 case 4: subtileFunc = savageUploadSubtile_4x8; break;
154 case 8: subtileFunc = tileInfo->subHeight == 8 ?
155 savageUploadSubtile_8x8 : savageUploadSubtile_8x2; break;
156 case 16: subtileFunc = tileInfo->subHeight == 8 ?
157 savageUploadSubtile_16x8 : savageUploadSubtile_16x2; break;
158 case 32: subtileFunc = savageUploadSubtile_32x8; break;
159 default: assert(0);
160 }
161 for (sy = 0; sy < hInSub; ++sy) {
162 srcSTile = srcSRow;
163 for (sx = 0; sx < wInSub; ++sx) {
164 src = srcSTile;
165 dest = subtileFunc (dest, src, srcStride);
166 srcSTile += subStride;
167 }
168 srcSRow += srcStride * tileInfo->subHeight;
169 }
170 }
171
172 /** \brief Upload a image that is smaller than 8 pixels in either dimension.
173 *
174 * \param tileInfo Pointer to tiling information
175 * \param width Width of the image
176 * \param height Height of the image
177 * \param bpp Bytes per pixel
178 * \param src Pointer to source data
179 * \param dest Pointer to destination
180 *
181 * This function handles all the special cases that need to be taken
182 * care off. The caller may need to call this function multiple times
183 * with the destination offset in different ways since small texture
184 * images must be repeated in order to fill a whole tile (or 4x4 for
185 * the last 3 levels).
186 *
187 * FIXME: Repeating inside this function would be more efficient.
188 */
189 static void savageUploadTiny (const savageTileInfo *tileInfo,
190 GLuint pixWidth, GLuint pixHeight,
191 GLuint width, GLuint height, GLuint bpp,
192 GLubyte *src, GLubyte *dest) {
193 GLuint size = MAX2(pixWidth, pixHeight);
194
195 if (width > tileInfo->subWidth) { /* assert: height <= subtile height */
196 GLuint wInSub = width / tileInfo->subWidth;
197 GLuint srcStride = width * bpp;
198 GLuint subStride = tileInfo->subWidth * bpp;
199 GLuint subSkip = (tileInfo->subHeight - height) * subStride;
200 GLubyte *srcSTile = src;
201 GLuint sx, y;
202 for (sx = 0; sx < wInSub; ++sx) {
203 src = srcSTile;
204 for (y = 0; y < height; ++y) {
205 memcpy (dest, src, subStride);
206 src += srcStride;
207 dest += subStride;
208 }
209 dest += subSkip;
210 srcSTile += subStride;
211 }
212 } else if (size > 4) { /* a tile or less wide, except the last 3 levels */
213 GLuint srcStride = width * bpp;
214 GLuint subStride = tileInfo->subWidth * bpp;
215 /* if the subtile width is 4 we have to skip every other subtile */
216 GLuint subSkip = tileInfo->subWidth <= 4 ?
217 subStride * tileInfo->subHeight : 0;
218 GLuint skipRemainder = tileInfo->subHeight - 1;
219 GLuint y;
220 for (y = 0; y < height; ++y) {
221 memcpy (dest, src, srcStride);
222 src += srcStride;
223 dest += subStride;
224 if ((y & skipRemainder) == skipRemainder)
225 dest += subSkip;
226 }
227 } else { /* the last 3 mipmap levels */
228 GLuint offset = (size <= 2 ? tileInfo->tinyOffset[size-1] : 0);
229 GLuint subStride = tileInfo->subWidth * bpp;
230 GLuint y;
231 dest += offset;
232 for (y = 0; y < height; ++y) {
233 memcpy (dest, src, bpp*width);
234 src += width * bpp;
235 dest += subStride;
236 }
237 }
238 }
239
240 /** \brief Upload an image from mesa's internal copy.
241 */
242 static void savageUploadTexLevel( savageTexObjPtr t, int level )
243 {
244 const struct gl_texture_image *image = t->base.tObj->Image[0][level];
245 const savageTileInfo *tileInfo = t->tileInfo;
246 GLuint pixWidth = image->Width2, pixHeight = image->Height2;
247 GLuint bpp = t->texelBytes;
248 GLuint width, height;
249
250 /* FIXME: Need triangle (rather than pixel) fallbacks to simulate
251 * this using normal textured triangles.
252 *
253 * DO THIS IN DRIVER STATE MANAGMENT, not hardware state.
254 */
255 if(image->Border != 0)
256 fprintf (stderr, "Not supported texture border %d.\n",
257 (int) image->Border);
258
259 if (t->hwFormat == TFT_S3TC4A4Bit || t->hwFormat == TFT_S3TC4CA4Bit ||
260 t->hwFormat == TFT_S3TC4Bit) {
261 width = (pixWidth+3) / 4;
262 height = (pixHeight+3) / 4;
263 } else {
264 width = pixWidth;
265 height = pixHeight;
266 }
267
268 if (pixWidth >= 8 && pixHeight >= 8) {
269 GLuint *dirtyPtr = t->image[level].dirtyTiles;
270 GLuint dirtyMask = 1;
271
272 if (width >= tileInfo->width && height >= tileInfo->height) {
273 GLuint wInTiles = width / tileInfo->width;
274 GLuint hInTiles = height / tileInfo->height;
275 GLubyte *srcTRow = image->Data, *src;
276 GLubyte *dest = (GLubyte *)(t->bufAddr + t->image[level].offset);
277 GLuint x, y;
278 for (y = 0; y < hInTiles; ++y) {
279 src = srcTRow;
280 for (x = 0; x < wInTiles; ++x) {
281 if (*dirtyPtr & dirtyMask) {
282 savageUploadTile (tileInfo,
283 tileInfo->wInSub, tileInfo->hInSub,
284 bpp, src, width * bpp, dest);
285 }
286 src += tileInfo->width * bpp;
287 dest += 2048; /* tile size is always 2k */
288 if (dirtyMask == 1<<31) {
289 dirtyMask = 1;
290 dirtyPtr++;
291 } else
292 dirtyMask <<= 1;
293 }
294 srcTRow += width * tileInfo->height * bpp;
295 }
296 } else if (width >= tileInfo->width) {
297 GLuint wInTiles = width / tileInfo->width;
298 GLubyte *src = image->Data;
299 GLubyte *dest = (GLubyte *)(t->bufAddr + t->image[level].offset);
300 GLuint tileStride = tileInfo->width * bpp * height;
301 savageContextPtr imesa = (savageContextPtr)t->base.heap->driverContext;
302 GLuint x;
303 /* Savage3D-based chips seem so use a constant tile stride
304 * of 2048 for vertically incomplete tiles, but only if
305 * the color depth is 32bpp. Nobody said this was supposed
306 * to be logical!
307 */
308 if (bpp == 4 && imesa->savageScreen->chipset < S3_SAVAGE4)
309 tileStride = 2048;
310 for (x = 0; x < wInTiles; ++x) {
311 if (*dirtyPtr & dirtyMask) {
312 savageUploadTile (tileInfo,
313 tileInfo->wInSub,
314 height / tileInfo->subHeight,
315 bpp, src, width * bpp, dest);
316 }
317 src += tileInfo->width * bpp;
318 dest += tileStride;
319 if (dirtyMask == 1<<31) {
320 dirtyMask = 1;
321 dirtyPtr++;
322 } else
323 dirtyMask <<= 1;
324 }
325 } else {
326 savageUploadTile (tileInfo, width / tileInfo->subWidth,
327 height / tileInfo->subHeight, bpp,
328 image->Data, width * bpp,
329 (GLubyte *)(t->bufAddr+t->image[level].offset));
330 }
331 } else {
332 GLuint minHeight, minWidth, hRepeat, vRepeat, x, y;
333 if (t->hwFormat == TFT_S3TC4A4Bit || t->hwFormat == TFT_S3TC4CA4Bit ||
334 t->hwFormat == TFT_S3TC4Bit)
335 minWidth = minHeight = 1;
336 else
337 minWidth = minHeight = 4;
338 if (width > minWidth || height > minHeight) {
339 minWidth = tileInfo->subWidth;
340 minHeight = tileInfo->subHeight;
341 }
342 hRepeat = width >= minWidth ? 1 : minWidth / width;
343 vRepeat = height >= minHeight ? 1 : minHeight / height;
344 for (y = 0; y < vRepeat; ++y) {
345 GLuint offset = y * tileInfo->subWidth*height * bpp;
346 for (x = 0; x < hRepeat; ++x) {
347 savageUploadTiny (tileInfo, pixWidth, pixHeight,
348 width, height, bpp, image->Data,
349 (GLubyte *)(t->bufAddr +
350 t->image[level].offset+offset));
351 offset += width * bpp;
352 }
353 }
354 }
355 }
356
357 /** \brief Compute the destination size of a texture image
358 */
359 static GLuint savageTexImageSize (GLuint width, GLuint height, GLuint bpp) {
360 /* full subtiles */
361 if (width >= 8 && height >= 8)
362 return width * height * bpp;
363 /* special case for the last three mipmap levels: the hardware computes
364 * the offset internally */
365 else if (width <= 4 && height <= 4)
366 return 0;
367 /* partially filled sub tiles waste memory
368 * on Savage3D and Savage4 with subtile width 4 every other subtile is
369 * skipped if width < 8 so we can assume a uniform subtile width of 8 */
370 else if (width >= 8)
371 return width * 8 * bpp;
372 else if (height >= 8)
373 return 8 * height * bpp;
374 else
375 return 64 * bpp;
376 }
377
378 /** \brief Compute the destination size of a compressed texture image
379 */
380 static GLuint savageCompressedTexImageSize (GLuint width, GLuint height,
381 GLuint bpp) {
382 width = (width+3) / 4;
383 height = (height+3) / 4;
384 /* full subtiles */
385 if (width >= 2 && height >= 2)
386 return width * height * bpp;
387 /* special case for the last three mipmap levels: the hardware computes
388 * the offset internally */
389 else if (width <= 1 && height <= 1)
390 return 0;
391 /* partially filled sub tiles waste memory
392 * on Savage3D and Savage4 with subtile width 4 every other subtile is
393 * skipped if width < 8 so we can assume a uniform subtile width of 8 */
394 else if (width >= 2)
395 return width * 2 * bpp;
396 else if (height >= 2)
397 return 2 * height * bpp;
398 else
399 return 4 * bpp;
400 }
401
402 /** \brief Compute the number of (partial) tiles of a texture image
403 */
404 static GLuint savageTexImageTiles (GLuint width, GLuint height,
405 const savageTileInfo *tileInfo)
406 {
407 return (width + tileInfo->width - 1) / tileInfo->width *
408 (height + tileInfo->height - 1) / tileInfo->height;
409 }
410
411 /** \brief Mark dirty tiles
412 *
413 * Some care must be taken because tileInfo may not be set or not
414 * up-to-date. So we check if tileInfo is initialized and if the number
415 * of tiles in the bit vector matches the number of tiles computed from
416 * the current tileInfo.
417 */
418 static void savageMarkDirtyTiles (savageTexObjPtr t, GLuint level,
419 GLuint totalWidth, GLuint totalHeight,
420 GLint xoffset, GLint yoffset,
421 GLsizei width, GLsizei height)
422 {
423 GLuint wInTiles, hInTiles;
424 GLuint x0, y0, x1, y1;
425 GLuint x, y;
426 if (!t->tileInfo)
427 return;
428 wInTiles = (totalWidth + t->tileInfo->width - 1) / t->tileInfo->width;
429 hInTiles = (totalHeight + t->tileInfo->height - 1) / t->tileInfo->height;
430 if (wInTiles * hInTiles != t->image[level].nTiles)
431 return;
432
433 x0 = xoffset / t->tileInfo->width;
434 y0 = yoffset / t->tileInfo->height;
435 x1 = (xoffset + width - 1) / t->tileInfo->width;
436 y1 = (yoffset + height - 1) / t->tileInfo->height;
437
438 for (y = y0; y <= y1; ++y) {
439 GLuint *ptr = t->image[level].dirtyTiles + (y * wInTiles + x0) / 32;
440 GLuint mask = 1 << (y * wInTiles + x0) % 32;
441 for (x = x0; x <= x1; ++x) {
442 *ptr |= mask;
443 if (mask == (1<<31)) {
444 ptr++;
445 mask = 1;
446 } else {
447 mask <<= 1;
448 }
449 }
450 }
451 }
452
453 /** \brief Mark all tiles as dirty
454 */
455 static void savageMarkAllTiles (savageTexObjPtr t, GLuint level)
456 {
457 GLuint words = (t->image[level].nTiles + 31) / 32;
458 if (words)
459 memset(t->image[level].dirtyTiles, ~0, words*sizeof(GLuint));
460 }
461
462
463 static void savageSetTexWrapping(savageTexObjPtr tex, GLenum s, GLenum t)
464 {
465 tex->setup.sWrapMode = s;
466 tex->setup.tWrapMode = t;
467 }
468
469 static void savageSetTexFilter(savageTexObjPtr t, GLenum minf, GLenum magf)
470 {
471 t->setup.minFilter = minf;
472 t->setup.magFilter = magf;
473 }
474
475
476 /* Need a fallback ?
477 */
478 static void savageSetTexBorderColor(savageTexObjPtr t, GLubyte color[4])
479 {
480 /* t->Setup[SAVAGE_TEXREG_TEXBORDERCOL] = */
481 /*t->setup.borderColor = SAVAGEPACKCOLOR8888(color[0],color[1],color[2],color[3]); */
482 }
483
484
485
486 static savageTexObjPtr
487 savageAllocTexObj( struct gl_texture_object *texObj )
488 {
489 savageTexObjPtr t;
490
491 t = (savageTexObjPtr) calloc(1,sizeof(*t));
492 texObj->DriverData = t;
493 if ( t != NULL ) {
494 GLuint i;
495
496 /* Initialize non-image-dependent parts of the state:
497 */
498 t->base.tObj = texObj;
499 t->base.dirty_images[0] = 0;
500 t->dirtySubImages = 0;
501 t->tileInfo = NULL;
502
503 /* Initialize dirty tiles bit vectors
504 */
505 for (i = 0; i < SAVAGE_TEX_MAXLEVELS; ++i)
506 t->image[i].nTiles = 0;
507
508 /* FIXME Something here to set initial values for other parts of
509 * FIXME t->setup?
510 */
511
512 make_empty_list( &t->base );
513
514 savageSetTexWrapping(t,texObj->WrapS,texObj->WrapT);
515 savageSetTexFilter(t,texObj->MinFilter,texObj->MagFilter);
516 savageSetTexBorderColor(t,texObj->_BorderChan);
517 }
518
519 return t;
520 }
521
522 /* Mesa texture formats for alpha-images on Savage3D/IX/MX
523 *
524 * Promoting texture images to ARGB888 or ARGB4444 doesn't work
525 * because we can't tell the hardware to ignore the color components
526 * and only use the alpha component. So we define our own texture
527 * formats that promote to ARGB8888 or ARGB4444 and set the color
528 * components to white. This way we get the correct result. */
529 static GLboolean
530 _savage_texstore_a1114444 (GLcontext *ctx, GLuint dims,
531 GLenum baseInternalFormat,
532 const struct gl_texture_format *dstFormat,
533 GLvoid *dstAddr,
534 GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
535 GLint dstRowStride, GLint dstImageStride,
536 GLint srcWidth, GLint srcHeight, GLint srcDepth,
537 GLenum srcFormat, GLenum srcType,
538 const GLvoid *srcAddr,
539 const struct gl_pixelstore_attrib *srcPacking);
540 static GLboolean
541 _savage_texstore_a1118888 (GLcontext *ctx, GLuint dims,
542 GLenum baseInternalFormat,
543 const struct gl_texture_format *dstFormat,
544 GLvoid *dstAddr,
545 GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
546 GLint dstRowStride, GLint dstImageStride,
547 GLint srcWidth, GLint srcHeight, GLint srcDepth,
548 GLenum srcFormat, GLenum srcType,
549 const GLvoid *srcAddr,
550 const struct gl_pixelstore_attrib *srcPacking);
551
552 static struct gl_texture_format _savage_texformat_a1114444 = {
553 MESA_FORMAT_ARGB4444, /* MesaFormat */
554 GL_RGBA, /* BaseFormat */
555 GL_UNSIGNED_NORMALIZED_ARB, /* DataType */
556 4, /* RedBits */
557 4, /* GreenBits */
558 4, /* BlueBits */
559 4, /* AlphaBits */
560 0, /* LuminanceBits */
561 0, /* IntensityBits */
562 0, /* IndexBits */
563 0, /* DepthBits */
564 2, /* TexelBytes */
565 _savage_texstore_a1114444, /* StoreTexImageFunc */
566 NULL, NULL, NULL, NULL, NULL, NULL /* FetchTexel* filled in by
567 * savageDDInitTextureFuncs */
568 };
569 static struct gl_texture_format _savage_texformat_a1118888 = {
570 MESA_FORMAT_ARGB8888, /* MesaFormat */
571 GL_RGBA, /* BaseFormat */
572 GL_UNSIGNED_NORMALIZED_ARB, /* DataType */
573 8, /* RedBits */
574 8, /* GreenBits */
575 8, /* BlueBits */
576 8, /* AlphaBits */
577 0, /* LuminanceBits */
578 0, /* IntensityBits */
579 0, /* IndexBits */
580 0, /* DepthBits */
581 4, /* TexelBytes */
582 _savage_texstore_a1118888, /* StoreTexImageFunc */
583 NULL, NULL, NULL, NULL, NULL, NULL /* FetchTexel* filled in by
584 * savageDDInitTextureFuncs */
585 };
586
587 static GLboolean
588 _savage_texstore_a1114444 (GLcontext *ctx, GLuint dims,
589 GLenum baseInternalFormat,
590 const struct gl_texture_format *dstFormat,
591 GLvoid *dstAddr,
592 GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
593 GLint dstRowStride, GLint dstImageStride,
594 GLint srcWidth, GLint srcHeight, GLint srcDepth,
595 GLenum srcFormat, GLenum srcType,
596 const GLvoid *srcAddr,
597 const struct gl_pixelstore_attrib *srcPacking)
598 {
599 /* general path */
600 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
601 baseInternalFormat,
602 baseInternalFormat,
603 srcWidth, srcHeight, srcDepth,
604 srcFormat, srcType, srcAddr,
605 srcPacking);
606 const GLchan *src = tempImage;
607 GLubyte *dstImage = (GLubyte *) dstAddr
608 + dstZoffset * dstImageStride
609 + dstYoffset * dstRowStride
610 + dstXoffset * dstFormat->TexelBytes;
611 GLint img, row, col;
612
613 ASSERT(dstFormat == &_savage_texformat_a1114444);
614 ASSERT(baseInternalFormat == GL_ALPHA);
615
616 if (!tempImage)
617 return GL_FALSE;
618 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
619 for (img = 0; img < srcDepth; img++) {
620 GLubyte *dstRow = dstImage;
621 for (row = 0; row < srcHeight; row++) {
622 GLushort *dstUI = (GLushort *) dstRow;
623 for (col = 0; col < srcWidth; col++) {
624 dstUI[col] = PACK_COLOR_4444( CHAN_TO_UBYTE(src[0]),
625 255, 255, 255 );
626 src += 1;
627 }
628 dstRow += dstRowStride;
629 }
630 dstImage += dstImageStride;
631 }
632 _mesa_free((void *) tempImage);
633
634 return GL_TRUE;
635 }
636 static GLboolean
637 _savage_texstore_a1118888 (GLcontext *ctx, GLuint dims,
638 GLenum baseInternalFormat,
639 const struct gl_texture_format *dstFormat,
640 GLvoid *dstAddr,
641 GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
642 GLint dstRowStride, GLint dstImageStride,
643 GLint srcWidth, GLint srcHeight, GLint srcDepth,
644 GLenum srcFormat, GLenum srcType,
645 const GLvoid *srcAddr,
646 const struct gl_pixelstore_attrib *srcPacking)
647 {
648 /* general path */
649 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
650 baseInternalFormat,
651 baseInternalFormat,
652 srcWidth, srcHeight, srcDepth,
653 srcFormat, srcType, srcAddr,
654 srcPacking);
655 const GLchan *src = tempImage;
656 GLubyte *dstImage = (GLubyte *) dstAddr
657 + dstZoffset * dstImageStride
658 + dstYoffset * dstRowStride
659 + dstXoffset * dstFormat->TexelBytes;
660 GLint img, row, col;
661
662 ASSERT(dstFormat == &_savage_texformat_a1118888);
663 ASSERT(baseInternalFormat == GL_ALPHA);
664
665 if (!tempImage)
666 return GL_FALSE;
667 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
668 for (img = 0; img < srcDepth; img++) {
669 GLubyte *dstRow = dstImage;
670 for (row = 0; row < srcHeight; row++) {
671 GLuint *dstUI = (GLuint *) dstRow;
672 for (col = 0; col < srcWidth; col++) {
673 dstUI[col] = PACK_COLOR_8888( CHAN_TO_UBYTE(src[0]),
674 255, 255, 255 );
675 src += 1;
676 }
677 dstRow += dstRowStride;
678 }
679 dstImage += dstImageStride;
680 }
681 _mesa_free((void *) tempImage);
682
683 return GL_TRUE;
684 }
685
686 /* Called by the _mesa_store_teximage[123]d() functions. */
687 static const struct gl_texture_format *
688 savageChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
689 GLenum format, GLenum type )
690 {
691 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
692 const GLboolean do32bpt =
693 ( imesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_32 );
694 const GLboolean force16bpt =
695 ( imesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FORCE_16 );
696 const GLboolean isSavage4 = (imesa->savageScreen->chipset >= S3_SAVAGE4);
697 (void) format;
698
699 switch ( internalFormat ) {
700 case 4:
701 case GL_RGBA:
702 case GL_COMPRESSED_RGBA:
703 switch ( type ) {
704 case GL_UNSIGNED_INT_10_10_10_2:
705 case GL_UNSIGNED_INT_2_10_10_10_REV:
706 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555;
707 case GL_UNSIGNED_SHORT_4_4_4_4:
708 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
709 return &_mesa_texformat_argb4444;
710 case GL_UNSIGNED_SHORT_5_5_5_1:
711 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
712 return &_mesa_texformat_argb1555;
713 default:
714 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
715 }
716
717 case 3:
718 case GL_RGB:
719 case GL_COMPRESSED_RGB:
720 switch ( type ) {
721 case GL_UNSIGNED_SHORT_4_4_4_4:
722 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
723 return &_mesa_texformat_argb4444;
724 case GL_UNSIGNED_SHORT_5_5_5_1:
725 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
726 return &_mesa_texformat_argb1555;
727 case GL_UNSIGNED_SHORT_5_6_5:
728 case GL_UNSIGNED_SHORT_5_6_5_REV:
729 return &_mesa_texformat_rgb565;
730 default:
731 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
732 }
733
734 case GL_RGBA8:
735 case GL_RGBA12:
736 case GL_RGBA16:
737 return !force16bpt ?
738 &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
739
740 case GL_RGB10_A2:
741 return !force16bpt ?
742 &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555;
743
744 case GL_RGBA4:
745 case GL_RGBA2:
746 return &_mesa_texformat_argb4444;
747
748 case GL_RGB5_A1:
749 return &_mesa_texformat_argb1555;
750
751 case GL_RGB8:
752 case GL_RGB10:
753 case GL_RGB12:
754 case GL_RGB16:
755 return !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
756
757 case GL_RGB5:
758 case GL_RGB4:
759 case GL_R3_G3_B2:
760 return &_mesa_texformat_rgb565;
761
762 case GL_ALPHA:
763 case GL_COMPRESSED_ALPHA:
764 return isSavage4 ? &_mesa_texformat_a8 : (
765 do32bpt ? &_savage_texformat_a1118888 : &_savage_texformat_a1114444);
766 case GL_ALPHA4:
767 return isSavage4 ? &_mesa_texformat_a8 : &_savage_texformat_a1114444;
768 case GL_ALPHA8:
769 case GL_ALPHA12:
770 case GL_ALPHA16:
771 return isSavage4 ? &_mesa_texformat_a8 : (
772 !force16bpt ? &_savage_texformat_a1118888 : &_savage_texformat_a1114444);
773
774 case 1:
775 case GL_LUMINANCE:
776 case GL_COMPRESSED_LUMINANCE:
777 /* no alpha, but use argb1555 in 16bit case to get pure grey values */
778 return isSavage4 ? &_mesa_texformat_l8 : (
779 do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555);
780 case GL_LUMINANCE4:
781 return isSavage4 ? &_mesa_texformat_l8 : &_mesa_texformat_argb1555;
782 case GL_LUMINANCE8:
783 case GL_LUMINANCE12:
784 case GL_LUMINANCE16:
785 return isSavage4 ? &_mesa_texformat_l8 : (
786 !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555);
787
788 case 2:
789 case GL_LUMINANCE_ALPHA:
790 case GL_COMPRESSED_LUMINANCE_ALPHA:
791 /* Savage4 has a al44 texture format. But it's not supported by Mesa. */
792 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
793 case GL_LUMINANCE4_ALPHA4:
794 case GL_LUMINANCE6_ALPHA2:
795 return &_mesa_texformat_argb4444;
796 case GL_LUMINANCE8_ALPHA8:
797 case GL_LUMINANCE12_ALPHA4:
798 case GL_LUMINANCE12_ALPHA12:
799 case GL_LUMINANCE16_ALPHA16:
800 return !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
801 #if 0
802 /* TFT_I8 produces garbage on ProSavageDDR and subsequent texture
803 * disable keeps rendering garbage. Disabled for now. */
804 case GL_INTENSITY:
805 case GL_COMPRESSED_INTENSITY:
806 return isSavage4 ? &_mesa_texformat_i8 : (
807 do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444);
808 case GL_INTENSITY4:
809 return isSavage4 ? &_mesa_texformat_i8 : &_mesa_texformat_argb4444;
810 case GL_INTENSITY8:
811 case GL_INTENSITY12:
812 case GL_INTENSITY16:
813 return isSavage4 ? &_mesa_texformat_i8 : (
814 !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444);
815 #else
816 case GL_INTENSITY:
817 case GL_COMPRESSED_INTENSITY:
818 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
819 case GL_INTENSITY4:
820 return &_mesa_texformat_argb4444;
821 case GL_INTENSITY8:
822 case GL_INTENSITY12:
823 case GL_INTENSITY16:
824 return !force16bpt ? &_mesa_texformat_argb8888 :
825 &_mesa_texformat_argb4444;
826 #endif
827
828 case GL_RGB_S3TC:
829 case GL_RGB4_S3TC:
830 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
831 return &_mesa_texformat_rgb_dxt1;
832 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
833 return &_mesa_texformat_rgba_dxt1;
834
835 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
836 return &_mesa_texformat_rgba_dxt3;
837
838 case GL_RGBA_S3TC:
839 case GL_RGBA4_S3TC:
840 if (!isSavage4)
841 /* Not the best choice but Savage3D/MX/IX don't support DXT3 or DXT5. */
842 return &_mesa_texformat_rgba_dxt1;
843 /* fall through */
844 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
845 return &_mesa_texformat_rgba_dxt5;
846
847 /*
848 case GL_COLOR_INDEX:
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 */
857 default:
858 _mesa_problem(ctx, "unexpected texture format in %s", __FUNCTION__);
859 return NULL;
860 }
861 }
862
863 static void savageSetTexImages( savageContextPtr imesa,
864 const struct gl_texture_object *tObj )
865 {
866 savageTexObjPtr t = (savageTexObjPtr) tObj->DriverData;
867 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
868 GLuint offset, i, textureFormat, tileIndex, size;
869 GLint firstLevel, lastLevel;
870
871 assert(t);
872 assert(image);
873
874 switch (image->TexFormat->MesaFormat) {
875 case MESA_FORMAT_ARGB8888:
876 textureFormat = TFT_ARGB8888;
877 t->texelBytes = tileIndex = 4;
878 break;
879 case MESA_FORMAT_ARGB1555:
880 textureFormat = TFT_ARGB1555;
881 t->texelBytes = tileIndex = 2;
882 break;
883 case MESA_FORMAT_ARGB4444:
884 textureFormat = TFT_ARGB4444;
885 t->texelBytes = tileIndex = 2;
886 break;
887 case MESA_FORMAT_RGB565:
888 textureFormat = TFT_RGB565;
889 t->texelBytes = tileIndex = 2;
890 break;
891 case MESA_FORMAT_L8:
892 textureFormat = TFT_L8;
893 t->texelBytes = tileIndex = 1;
894 break;
895 case MESA_FORMAT_I8:
896 textureFormat = TFT_I8;
897 t->texelBytes = tileIndex = 1;
898 break;
899 case MESA_FORMAT_A8:
900 textureFormat = TFT_A8;
901 t->texelBytes = tileIndex = 1;
902 break;
903 case MESA_FORMAT_RGB_DXT1:
904 textureFormat = TFT_S3TC4Bit;
905 tileIndex = TILE_INDEX_DXT1;
906 t->texelBytes = 8;
907 break;
908 case MESA_FORMAT_RGBA_DXT1:
909 textureFormat = TFT_S3TC4Bit;
910 tileIndex = TILE_INDEX_DXT1;
911 t->texelBytes = 8;
912 break;
913 case MESA_FORMAT_RGBA_DXT3:
914 textureFormat = TFT_S3TC4A4Bit;
915 tileIndex = TILE_INDEX_DXTn;
916 t->texelBytes = 16;
917 break;
918 case MESA_FORMAT_RGBA_DXT5:
919 textureFormat = TFT_S3TC4CA4Bit;
920 tileIndex = TILE_INDEX_DXTn;
921 t->texelBytes = 16;
922 break;
923 default:
924 _mesa_problem(imesa->glCtx, "Bad texture format in %s", __FUNCTION__);
925 return;
926 }
927 t->hwFormat = textureFormat;
928
929 /* Select tiling format depending on the chipset and texture format */
930 if (imesa->savageScreen->chipset <= S3_SAVAGE4)
931 t->tileInfo = &tileInfo_s3d_s4[tileIndex];
932 else
933 t->tileInfo = &tileInfo_pro[tileIndex];
934
935 /* Compute which mipmap levels we really want to send to the hardware.
936 */
937 driCalculateTextureFirstLastLevel( &t->base );
938 firstLevel = t->base.firstLevel;
939 lastLevel = t->base.lastLevel;
940
941 /* Figure out the size now (and count the levels). Upload won't be
942 * done until later. If the number of tiles changes, it means that
943 * this function is called for the first time on this tex object or
944 * the image or the destination color format changed. So all tiles
945 * are marked as dirty.
946 */
947 offset = 0;
948 size = 1;
949 for ( i = firstLevel ; i <= lastLevel && tObj->Image[0][i] ; i++ ) {
950 GLuint nTiles;
951 nTiles = savageTexImageTiles (image->Width2, image->Height2, t->tileInfo);
952 if (t->image[i].nTiles != nTiles) {
953 GLuint words = (nTiles + 31) / 32;
954 if (t->image[i].nTiles != 0) {
955 free(t->image[i].dirtyTiles);
956 }
957 t->image[i].dirtyTiles = malloc(words*sizeof(GLuint));
958 memset(t->image[i].dirtyTiles, ~0, words*sizeof(GLuint));
959 }
960 t->image[i].nTiles = nTiles;
961
962 t->image[i].offset = offset;
963
964 image = tObj->Image[0][i];
965 if (t->texelBytes >= 8)
966 size = savageCompressedTexImageSize (image->Width2, image->Height2,
967 t->texelBytes);
968 else
969 size = savageTexImageSize (image->Width2, image->Height2,
970 t->texelBytes);
971 offset += size;
972 }
973
974 t->base.lastLevel = i-1;
975 t->base.totalSize = offset;
976 /* the last three mipmap levels don't add to the offset. They are packed
977 * into 64 pixels. */
978 if (size == 0)
979 t->base.totalSize += (t->texelBytes >= 8 ? 4 : 64) * t->texelBytes;
980 /* 2k-aligned (really needed?) */
981 t->base.totalSize = (t->base.totalSize + 2047UL) & ~2047UL;
982 }
983
984 void savageDestroyTexObj(savageContextPtr imesa, savageTexObjPtr t)
985 {
986 GLuint i;
987
988 /* Free dirty tiles bit vectors */
989 for (i = 0; i < SAVAGE_TEX_MAXLEVELS; ++i) {
990 if (t->image[i].nTiles)
991 free (t->image[i].dirtyTiles);
992 }
993
994 /* See if it was the driver's current object.
995 */
996 if ( imesa != NULL )
997 {
998 for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ )
999 {
1000 if ( &t->base == imesa->CurrentTexObj[ i ] ) {
1001 assert( t->base.bound & (1 << i) );
1002 imesa->CurrentTexObj[ i ] = NULL;
1003 }
1004 }
1005 }
1006 }
1007
1008 /* Upload a texture's images to one of the texture heaps. May have to
1009 * eject our own and/or other client's texture objects to make room
1010 * for the upload.
1011 */
1012 static void savageUploadTexImages( savageContextPtr imesa, savageTexObjPtr t )
1013 {
1014 const GLint numLevels = t->base.lastLevel - t->base.firstLevel + 1;
1015 GLuint i;
1016
1017 assert(t);
1018
1019 LOCK_HARDWARE(imesa);
1020
1021 /* Do we need to eject LRU texture objects?
1022 */
1023 if (!t->base.memBlock) {
1024 GLint heap;
1025 GLuint ofs;
1026
1027 heap = driAllocateTexture(imesa->textureHeaps, imesa->lastTexHeap,
1028 (driTextureObject *)t);
1029 if (heap == -1) {
1030 UNLOCK_HARDWARE(imesa);
1031 return;
1032 }
1033
1034 ofs = t->base.memBlock->ofs;
1035 t->setup.physAddr = imesa->savageScreen->textureOffset[heap] + ofs;
1036 t->bufAddr = (GLubyte *)imesa->savageScreen->texVirtual[heap] + ofs;
1037 imesa->dirty |= SAVAGE_UPLOAD_GLOBAL; /* FIXME: really needed? */
1038 }
1039
1040 /* Let the world know we've used this memory recently.
1041 */
1042 driUpdateTextureLRU( &t->base );
1043 UNLOCK_HARDWARE(imesa);
1044
1045 if (t->base.dirty_images[0] || t->dirtySubImages) {
1046 if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX)
1047 fprintf(stderr, "Texture upload: |");
1048
1049 /* Heap timestamps are only reliable with Savage DRM 2.3.x or
1050 * later. Earlier versions had only 16 bit time stamps which
1051 * would wrap too frequently. */
1052 if (imesa->savageScreen->driScrnPriv->drmMinor >= 3) {
1053 unsigned int heap = t->base.heap->heapId;
1054 LOCK_HARDWARE(imesa);
1055 savageWaitEvent (imesa, imesa->textureHeaps[heap]->timestamp);
1056 } else {
1057 savageFlushVertices (imesa);
1058 LOCK_HARDWARE(imesa);
1059 savageFlushCmdBufLocked (imesa, GL_FALSE);
1060 WAIT_IDLE_EMPTY_LOCKED(imesa);
1061 }
1062
1063 for (i = 0 ; i < numLevels ; i++) {
1064 const GLint j = t->base.firstLevel + i; /* the texObj's level */
1065 if (t->base.dirty_images[0] & (1 << j)) {
1066 savageMarkAllTiles(t, j);
1067 if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX)
1068 fprintf (stderr, "*");
1069 } else if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX) {
1070 if (t->dirtySubImages & (1 << j))
1071 fprintf (stderr, ".");
1072 else
1073 fprintf (stderr, " ");
1074 }
1075 if ((t->base.dirty_images[0] | t->dirtySubImages) & (1 << j))
1076 savageUploadTexLevel( t, j );
1077 }
1078
1079 UNLOCK_HARDWARE(imesa);
1080 t->base.dirty_images[0] = 0;
1081 t->dirtySubImages = 0;
1082
1083 if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX)
1084 fprintf(stderr, "|\n");
1085 }
1086 }
1087
1088
1089
1090
1091 static void savageUpdateTex0State_s4( GLcontext *ctx )
1092 {
1093 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1094 struct gl_texture_object *tObj;
1095 struct gl_texture_image *image;
1096 savageTexObjPtr t;
1097 GLuint format;
1098
1099 /* disable */
1100 imesa->regs.s4.texDescr.ni.tex0En = GL_FALSE;
1101 imesa->regs.s4.texBlendCtrl[0].ui = TBC_NoTexMap;
1102 imesa->regs.s4.texCtrl[0].ui = 0x20f040;
1103 if (ctx->Texture.Unit[0]._ReallyEnabled == 0)
1104 return;
1105
1106 tObj = ctx->Texture.Unit[0]._Current;
1107 if ((ctx->Texture.Unit[0]._ReallyEnabled & ~(TEXTURE_1D_BIT|TEXTURE_2D_BIT))
1108 || tObj->Image[0][tObj->BaseLevel]->Border > 0) {
1109 /* 3D texturing enabled, or texture border - fallback */
1110 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1111 return;
1112 }
1113
1114 /* Do 2D texture setup */
1115
1116 t = tObj->DriverData;
1117 if (!t) {
1118 t = savageAllocTexObj( tObj );
1119 if (!t)
1120 return;
1121 }
1122
1123 imesa->CurrentTexObj[0] = &t->base;
1124 t->base.bound |= 1;
1125
1126 if (t->base.dirty_images[0] || t->dirtySubImages) {
1127 savageSetTexImages(imesa, tObj);
1128 savageUploadTexImages(imesa, t);
1129 }
1130
1131 driUpdateTextureLRU( &t->base );
1132
1133 format = tObj->Image[0][tObj->BaseLevel]->Format;
1134
1135 switch (ctx->Texture.Unit[0].EnvMode) {
1136 case GL_REPLACE:
1137 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1138 switch(format)
1139 {
1140 case GL_LUMINANCE:
1141 case GL_RGB:
1142 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Decal;
1143 break;
1144
1145 case GL_LUMINANCE_ALPHA:
1146 case GL_RGBA:
1147 case GL_INTENSITY:
1148 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Copy;
1149 break;
1150
1151 case GL_ALPHA:
1152 imesa->regs.s4.texBlendCtrl[0].ui = TBC_CopyAlpha;
1153 break;
1154 }
1155 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1156 &imesa->regs.s4.texBlendCtrl[0]);
1157 break;
1158
1159 case GL_DECAL:
1160 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1161 switch (format)
1162 {
1163 case GL_RGB:
1164 case GL_LUMINANCE:
1165 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Decal;
1166 break;
1167
1168 case GL_RGBA:
1169 case GL_INTENSITY:
1170 case GL_LUMINANCE_ALPHA:
1171 imesa->regs.s4.texBlendCtrl[0].ui = TBC_DecalAlpha;
1172 break;
1173
1174 /*
1175 GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_INTENSITY
1176 are undefined with GL_DECAL
1177 */
1178
1179 case GL_ALPHA:
1180 imesa->regs.s4.texBlendCtrl[0].ui = TBC_CopyAlpha;
1181 break;
1182 }
1183 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1184 &imesa->regs.s4.texBlendCtrl[0]);
1185 break;
1186
1187 case GL_MODULATE:
1188 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1189 imesa->regs.s4.texBlendCtrl[0].ui = TBC_ModulAlpha;
1190 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1191 &imesa->regs.s4.texBlendCtrl[0]);
1192 break;
1193
1194 case GL_BLEND:
1195 imesa->regs.s4.texBlendColor.ui = imesa->texEnvColor;
1196
1197 switch (format)
1198 {
1199 case GL_ALPHA:
1200 imesa->regs.s4.texBlendCtrl[0].ui = TBC_ModulAlpha;
1201 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1202 break;
1203
1204 case GL_LUMINANCE:
1205 case GL_RGB:
1206 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Blend0;
1207 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1208 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1209 imesa->regs.s4.texDescr.ni.tex1Width =
1210 imesa->regs.s4.texDescr.ni.tex0Width;
1211 imesa->regs.s4.texDescr.ni.tex1Height =
1212 imesa->regs.s4.texDescr.ni.tex0Height;
1213 imesa->regs.s4.texDescr.ni.tex1Fmt =
1214 imesa->regs.s4.texDescr.ni.tex0Fmt;
1215
1216 imesa->regs.s4.texAddr[1].ui = imesa->regs.s4.texAddr[0].ui;
1217 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Blend1;
1218
1219 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_TRUE;
1220 imesa->bTexEn1 = GL_TRUE;
1221 break;
1222
1223 case GL_LUMINANCE_ALPHA:
1224 case GL_RGBA:
1225 imesa->regs.s4.texBlendCtrl[0].ui = TBC_BlendAlpha0;
1226 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1227 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1228 imesa->regs.s4.texDescr.ni.tex1Width =
1229 imesa->regs.s4.texDescr.ni.tex0Width;
1230 imesa->regs.s4.texDescr.ni.tex1Height =
1231 imesa->regs.s4.texDescr.ni.tex0Height;
1232 imesa->regs.s4.texDescr.ni.tex1Fmt =
1233 imesa->regs.s4.texDescr.ni.tex0Fmt;
1234
1235 imesa->regs.s4.texAddr[1].ui = imesa->regs.s4.texAddr[0].ui;
1236 imesa->regs.s4.texBlendCtrl[1].ui = TBC_BlendAlpha1;
1237
1238 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_TRUE;
1239 imesa->bTexEn1 = GL_TRUE;
1240 break;
1241
1242 case GL_INTENSITY:
1243 imesa->regs.s4.texBlendCtrl[0].ui = TBC_BlendInt0;
1244 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1245 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1246 imesa->regs.s4.texDescr.ni.tex1Width =
1247 imesa->regs.s4.texDescr.ni.tex0Width;
1248 imesa->regs.s4.texDescr.ni.tex1Height =
1249 imesa->regs.s4.texDescr.ni.tex0Height;
1250 imesa->regs.s4.texDescr.ni.tex1Fmt =
1251 imesa->regs.s4.texDescr.ni.tex0Fmt;
1252
1253 imesa->regs.s4.texAddr[1].ui = imesa->regs.s4.texAddr[0].ui;
1254 imesa->regs.s4.texBlendCtrl[1].ui = TBC_BlendInt1;
1255
1256 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_TRUE;
1257 imesa->regs.s4.texCtrl[0].ni.alphaArg1Invert = GL_TRUE;
1258 imesa->bTexEn1 = GL_TRUE;
1259 break;
1260 }
1261 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1262 &imesa->regs.s4.texBlendCtrl[0]);
1263 break;
1264
1265 case GL_ADD:
1266 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1267 switch (format)
1268 {
1269 case GL_ALPHA:
1270 imesa->regs.s4.texBlendCtrl[0].ui = TBC_ModulAlpha;
1271 break;
1272
1273 case GL_LUMINANCE:
1274 case GL_RGB:
1275 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Add;
1276 break;
1277
1278 case GL_LUMINANCE_ALPHA:
1279 case GL_RGBA:
1280 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Add;
1281 break;
1282
1283 case GL_INTENSITY:
1284 imesa->regs.s4.texBlendCtrl[0].ui = TBC_AddAlpha;
1285 break;
1286 }
1287 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1288 &imesa->regs.s4.texBlendCtrl[0]);
1289 break;
1290
1291 #if GL_ARB_texture_env_combine
1292 case GL_COMBINE_ARB:
1293 __HWParseTexEnvCombine(imesa, 0, &imesa->regs.s4.texCtrl[0],
1294 &imesa->regs.s4.texBlendCtrl[0]);
1295 break;
1296 #endif
1297
1298 default:
1299 fprintf(stderr, "unknown tex env mode");
1300 exit(1);
1301 break;
1302 }
1303
1304 imesa->regs.s4.texCtrl[0].ni.uMode =
1305 t->setup.sWrapMode == GL_REPEAT ? 0 : 1;
1306 imesa->regs.s4.texCtrl[0].ni.vMode =
1307 t->setup.tWrapMode == GL_REPEAT ? 0 : 1;
1308
1309 switch (t->setup.minFilter)
1310 {
1311 case GL_NEAREST:
1312 imesa->regs.s4.texCtrl[0].ni.filterMode = TFM_Point;
1313 imesa->regs.s4.texCtrl[0].ni.mipmapEnable = GL_FALSE;
1314 break;
1315
1316 case GL_LINEAR:
1317 imesa->regs.s4.texCtrl[0].ni.filterMode = TFM_Bilin;
1318 imesa->regs.s4.texCtrl[0].ni.mipmapEnable = GL_FALSE;
1319 break;
1320
1321 case GL_NEAREST_MIPMAP_NEAREST:
1322 imesa->regs.s4.texCtrl[0].ni.filterMode = TFM_Point;
1323 imesa->regs.s4.texCtrl[0].ni.mipmapEnable = GL_TRUE;
1324 break;
1325
1326 case GL_LINEAR_MIPMAP_NEAREST:
1327 imesa->regs.s4.texCtrl[0].ni.filterMode = TFM_Bilin;
1328 imesa->regs.s4.texCtrl[0].ni.mipmapEnable = GL_TRUE;
1329 break;
1330
1331 case GL_NEAREST_MIPMAP_LINEAR:
1332 case GL_LINEAR_MIPMAP_LINEAR:
1333 imesa->regs.s4.texCtrl[0].ni.filterMode = TFM_Trilin;
1334 imesa->regs.s4.texCtrl[0].ni.mipmapEnable = GL_TRUE;
1335 break;
1336 }
1337
1338 if((ctx->Texture.Unit[0].LodBias !=0.0F) ||
1339 (imesa->regs.s4.texCtrl[0].ni.dBias != 0))
1340 {
1341 int bias = (int)(ctx->Texture.Unit[0].LodBias * 32.0) +
1342 SAVAGE4_LOD_OFFSET;
1343 if (bias < -256)
1344 bias = -256;
1345 else if (bias > 255)
1346 bias = 255;
1347 imesa->regs.s4.texCtrl[0].ni.dBias = bias & 0x1ff;
1348 }
1349
1350 image = tObj->Image[0][tObj->BaseLevel];
1351 imesa->regs.s4.texDescr.ni.tex0En = GL_TRUE;
1352 imesa->regs.s4.texDescr.ni.tex0Width = image->WidthLog2;
1353 imesa->regs.s4.texDescr.ni.tex0Height = image->HeightLog2;
1354 imesa->regs.s4.texDescr.ni.tex0Fmt = t->hwFormat;
1355 imesa->regs.s4.texCtrl[0].ni.dMax = t->base.lastLevel - t->base.firstLevel;
1356
1357 if (imesa->regs.s4.texDescr.ni.tex1En)
1358 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1359
1360 imesa->regs.s4.texAddr[0].ui = (u_int32_t) t->setup.physAddr | 0x2;
1361 if(t->base.heap->heapId == SAVAGE_AGP_HEAP)
1362 imesa->regs.s4.texAddr[0].ui |= 0x1;
1363
1364 return;
1365 }
1366 static void savageUpdateTex1State_s4( GLcontext *ctx )
1367 {
1368 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1369 struct gl_texture_object *tObj;
1370 struct gl_texture_image *image;
1371 savageTexObjPtr t;
1372 GLuint format;
1373
1374 /* disable */
1375 if(imesa->bTexEn1)
1376 {
1377 imesa->bTexEn1 = GL_FALSE;
1378 return;
1379 }
1380
1381 imesa->regs.s4.texDescr.ni.tex1En = GL_FALSE;
1382 imesa->regs.s4.texBlendCtrl[1].ui = TBC_NoTexMap1;
1383 imesa->regs.s4.texCtrl[1].ui = 0x20f040;
1384 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_FALSE;
1385 if (ctx->Texture.Unit[1]._ReallyEnabled == 0)
1386 return;
1387
1388 tObj = ctx->Texture.Unit[1]._Current;
1389
1390 if ((ctx->Texture.Unit[1]._ReallyEnabled & ~(TEXTURE_1D_BIT|TEXTURE_2D_BIT))
1391 || tObj->Image[0][tObj->BaseLevel]->Border > 0) {
1392 /* 3D texturing enabled, or texture border - fallback */
1393 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1394 return;
1395 }
1396
1397 /* Do 2D texture setup */
1398
1399 t = tObj->DriverData;
1400 if (!t) {
1401 t = savageAllocTexObj( tObj );
1402 if (!t)
1403 return;
1404 }
1405
1406 imesa->CurrentTexObj[1] = &t->base;
1407
1408 t->base.bound |= 2;
1409
1410 if (t->base.dirty_images[0] || t->dirtySubImages) {
1411 savageSetTexImages(imesa, tObj);
1412 savageUploadTexImages(imesa, t);
1413 }
1414
1415 driUpdateTextureLRU( &t->base );
1416
1417 format = tObj->Image[0][tObj->BaseLevel]->Format;
1418
1419 switch (ctx->Texture.Unit[1].EnvMode) {
1420 case GL_REPLACE:
1421 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1422 switch (format)
1423 {
1424 case GL_LUMINANCE:
1425 case GL_RGB:
1426 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Decal;
1427 break;
1428
1429 case GL_LUMINANCE_ALPHA:
1430 case GL_INTENSITY:
1431 case GL_RGBA:
1432 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Copy;
1433 break;
1434
1435 case GL_ALPHA:
1436 imesa->regs.s4.texBlendCtrl[1].ui = TBC_CopyAlpha1;
1437 break;
1438 }
1439 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1440 break;
1441 case GL_MODULATE:
1442 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1443 imesa->regs.s4.texBlendCtrl[1].ui = TBC_ModulAlpha1;
1444 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1445 break;
1446
1447 case GL_ADD:
1448 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1449 switch (format)
1450 {
1451 case GL_ALPHA:
1452 imesa->regs.s4.texBlendCtrl[1].ui = TBC_ModulAlpha1;
1453 break;
1454
1455 case GL_LUMINANCE:
1456 case GL_RGB:
1457 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Add1;
1458 break;
1459
1460 case GL_LUMINANCE_ALPHA:
1461 case GL_RGBA:
1462 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Add1;
1463 break;
1464
1465 case GL_INTENSITY:
1466 imesa->regs.s4.texBlendCtrl[1].ui = TBC_AddAlpha1;
1467 break;
1468 }
1469 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1470 break;
1471
1472 #if GL_ARB_texture_env_combine
1473 case GL_COMBINE_ARB:
1474 __HWParseTexEnvCombine(imesa, 1, &texCtrl, &imesa->regs.s4.texBlendCtrl);
1475 break;
1476 #endif
1477
1478 case GL_DECAL:
1479 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1480
1481 switch (format)
1482 {
1483 case GL_LUMINANCE:
1484 case GL_RGB:
1485 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Decal1;
1486 break;
1487 case GL_LUMINANCE_ALPHA:
1488 case GL_INTENSITY:
1489 case GL_RGBA:
1490 imesa->regs.s4.texBlendCtrl[1].ui = TBC_DecalAlpha1;
1491 break;
1492
1493 /*
1494 // GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_INTENSITY
1495 // are undefined with GL_DECAL
1496 */
1497 case GL_ALPHA:
1498 imesa->regs.s4.texBlendCtrl[1].ui = TBC_CopyAlpha1;
1499 break;
1500 }
1501 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1502 break;
1503
1504 case GL_BLEND:
1505 if (format == GL_LUMINANCE)
1506 {
1507 /*
1508 // This is a hack for GLQuake, invert.
1509 */
1510 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_TRUE;
1511 imesa->regs.s4.texBlendCtrl[1].ui = 0;
1512 }
1513 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1514 break;
1515
1516 default:
1517 fprintf(stderr, "unkown tex 1 env mode\n");
1518 exit(1);
1519 break;
1520 }
1521
1522 imesa->regs.s4.texCtrl[1].ni.uMode =
1523 t->setup.sWrapMode == GL_REPEAT ? 0 : 1;
1524 imesa->regs.s4.texCtrl[1].ni.vMode =
1525 t->setup.tWrapMode == GL_REPEAT ? 0 : 1;
1526
1527 switch (t->setup.minFilter)
1528 {
1529 case GL_NEAREST:
1530 imesa->regs.s4.texCtrl[1].ni.filterMode = TFM_Point;
1531 imesa->regs.s4.texCtrl[1].ni.mipmapEnable = GL_FALSE;
1532 break;
1533
1534 case GL_LINEAR:
1535 imesa->regs.s4.texCtrl[1].ni.filterMode = TFM_Bilin;
1536 imesa->regs.s4.texCtrl[1].ni.mipmapEnable = GL_FALSE;
1537 break;
1538
1539 case GL_NEAREST_MIPMAP_NEAREST:
1540 imesa->regs.s4.texCtrl[1].ni.filterMode = TFM_Point;
1541 imesa->regs.s4.texCtrl[1].ni.mipmapEnable = GL_TRUE;
1542 break;
1543
1544 case GL_LINEAR_MIPMAP_NEAREST:
1545 imesa->regs.s4.texCtrl[1].ni.filterMode = TFM_Bilin;
1546 imesa->regs.s4.texCtrl[1].ni.mipmapEnable = GL_TRUE;
1547 break;
1548
1549 case GL_NEAREST_MIPMAP_LINEAR:
1550 case GL_LINEAR_MIPMAP_LINEAR:
1551 imesa->regs.s4.texCtrl[1].ni.filterMode = TFM_Trilin;
1552 imesa->regs.s4.texCtrl[1].ni.mipmapEnable = GL_TRUE;
1553 break;
1554 }
1555
1556 if((ctx->Texture.Unit[1].LodBias !=0.0F) ||
1557 (imesa->regs.s4.texCtrl[1].ni.dBias != 0))
1558 {
1559 int bias = (int)(ctx->Texture.Unit[1].LodBias * 32.0) +
1560 SAVAGE4_LOD_OFFSET;
1561 if (bias < -256)
1562 bias = -256;
1563 else if (bias > 255)
1564 bias = 255;
1565 imesa->regs.s4.texCtrl[1].ni.dBias = bias & 0x1ff;
1566 }
1567
1568 image = tObj->Image[0][tObj->BaseLevel];
1569 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1570 imesa->regs.s4.texDescr.ni.tex1Width = image->WidthLog2;
1571 imesa->regs.s4.texDescr.ni.tex1Height = image->HeightLog2;
1572 imesa->regs.s4.texDescr.ni.tex1Fmt = t->hwFormat;
1573 imesa->regs.s4.texCtrl[1].ni.dMax = t->base.lastLevel - t->base.firstLevel;
1574 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1575
1576 imesa->regs.s4.texAddr[1].ui = (u_int32_t) t->setup.physAddr | 2;
1577 if(t->base.heap->heapId == SAVAGE_AGP_HEAP)
1578 imesa->regs.s4.texAddr[1].ui |= 0x1;
1579 }
1580 static void savageUpdateTexState_s3d( GLcontext *ctx )
1581 {
1582 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1583 struct gl_texture_object *tObj;
1584 struct gl_texture_image *image;
1585 savageTexObjPtr t;
1586 GLuint format;
1587
1588 /* disable */
1589 imesa->regs.s3d.texCtrl.ui = 0;
1590 imesa->regs.s3d.texCtrl.ni.texEn = GL_FALSE;
1591 imesa->regs.s3d.texCtrl.ni.dBias = 0x08;
1592 imesa->regs.s3d.texCtrl.ni.texXprEn = GL_TRUE;
1593 if (ctx->Texture.Unit[0]._ReallyEnabled == 0)
1594 return;
1595
1596 tObj = ctx->Texture.Unit[0]._Current;
1597 if ((ctx->Texture.Unit[0]._ReallyEnabled & ~(TEXTURE_1D_BIT|TEXTURE_2D_BIT))
1598 || tObj->Image[0][tObj->BaseLevel]->Border > 0) {
1599 /* 3D texturing enabled, or texture border - fallback */
1600 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1601 return;
1602 }
1603
1604 /* Do 2D texture setup */
1605 t = tObj->DriverData;
1606 if (!t) {
1607 t = savageAllocTexObj( tObj );
1608 if (!t)
1609 return;
1610 }
1611
1612 imesa->CurrentTexObj[0] = &t->base;
1613 t->base.bound |= 1;
1614
1615 if (t->base.dirty_images[0] || t->dirtySubImages) {
1616 savageSetTexImages(imesa, tObj);
1617 savageUploadTexImages(imesa, t);
1618 }
1619
1620 driUpdateTextureLRU( &t->base );
1621
1622 format = tObj->Image[0][tObj->BaseLevel]->Format;
1623
1624 /* FIXME: copied from utah-glx, probably needs some tuning */
1625 switch (ctx->Texture.Unit[0].EnvMode) {
1626 case GL_DECAL:
1627 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_DECALALPHA_S3D;
1628 break;
1629 case GL_REPLACE:
1630 switch (format) {
1631 case GL_ALPHA: /* FIXME */
1632 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = 1;
1633 break;
1634 case GL_LUMINANCE_ALPHA:
1635 case GL_RGBA:
1636 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = 4;
1637 break;
1638 case GL_RGB:
1639 case GL_LUMINANCE:
1640 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_DECAL_S3D;
1641 break;
1642 case GL_INTENSITY:
1643 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_COPY_S3D;
1644 }
1645 break;
1646 case GL_BLEND: /* hardware can't do GL_BLEND */
1647 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1648 return;
1649 case GL_MODULATE:
1650 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_MODULATEALPHA_S3D;
1651 break;
1652 default:
1653 fprintf(stderr, "unkown tex env mode\n");
1654 /*exit(1);*/
1655 break;
1656 }
1657
1658 /* The Savage3D can't handle different wrapping modes in s and t.
1659 * If they are not the same, fall back to software. */
1660 if (t->setup.sWrapMode != t->setup.tWrapMode) {
1661 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1662 return;
1663 }
1664 imesa->regs.s3d.texCtrl.ni.uWrapEn = 0;
1665 imesa->regs.s3d.texCtrl.ni.vWrapEn = 0;
1666 imesa->regs.s3d.texCtrl.ni.wrapMode =
1667 (t->setup.sWrapMode == GL_REPEAT) ? TAM_Wrap : TAM_Clamp;
1668
1669 switch (t->setup.minFilter) {
1670 case GL_NEAREST:
1671 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Point;
1672 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_TRUE;
1673 break;
1674
1675 case GL_LINEAR:
1676 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Bilin;
1677 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_TRUE;
1678 break;
1679
1680 case GL_NEAREST_MIPMAP_NEAREST:
1681 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Point;
1682 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_FALSE;
1683 break;
1684
1685 case GL_LINEAR_MIPMAP_NEAREST:
1686 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Bilin;
1687 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_FALSE;
1688 break;
1689
1690 case GL_NEAREST_MIPMAP_LINEAR:
1691 case GL_LINEAR_MIPMAP_LINEAR:
1692 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Trilin;
1693 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_FALSE;
1694 break;
1695 }
1696
1697 /* There is no way to specify a maximum mipmap level. We may have to
1698 disable mipmapping completely. */
1699 /*
1700 if (t->max_level < t->image[0].image->WidthLog2 ||
1701 t->max_level < t->image[0].image->HeightLog2) {
1702 texCtrl.ni.mipmapEnable = GL_TRUE;
1703 if (texCtrl.ni.filterMode == TFM_Trilin)
1704 texCtrl.ni.filterMode = TFM_Bilin;
1705 texCtrl.ni.filterMode = TFM_Point;
1706 }
1707 */
1708
1709 if((ctx->Texture.Unit[0].LodBias !=0.0F) ||
1710 (imesa->regs.s3d.texCtrl.ni.dBias != 0))
1711 {
1712 int bias = (int)(ctx->Texture.Unit[0].LodBias * 16.0);
1713 if (bias < -256)
1714 bias = -256;
1715 else if (bias > 255)
1716 bias = 255;
1717 imesa->regs.s3d.texCtrl.ni.dBias = bias & 0x1ff;
1718 }
1719
1720 image = tObj->Image[0][tObj->BaseLevel];
1721 imesa->regs.s3d.texCtrl.ni.texEn = GL_TRUE;
1722 imesa->regs.s3d.texDescr.ni.texWidth = image->WidthLog2;
1723 imesa->regs.s3d.texDescr.ni.texHeight = image->HeightLog2;
1724 assert (t->hwFormat <= 7);
1725 imesa->regs.s3d.texDescr.ni.texFmt = t->hwFormat;
1726
1727 imesa->regs.s3d.texAddr.ui = (u_int32_t) t->setup.physAddr | 2;
1728 if(t->base.heap->heapId == SAVAGE_AGP_HEAP)
1729 imesa->regs.s3d.texAddr.ui |= 0x1;
1730 }
1731
1732
1733 static void savageTimestampTextures( savageContextPtr imesa )
1734 {
1735 /* Timestamp current texture objects for texture heap aging.
1736 * Only useful with long-lived 32-bit event tags available
1737 * with Savage DRM 2.3.x or later. */
1738 if ((imesa->CurrentTexObj[0] || imesa->CurrentTexObj[1]) &&
1739 imesa->savageScreen->driScrnPriv->drmMinor >= 3) {
1740 unsigned int e;
1741 FLUSH_BATCH(imesa);
1742 e = savageEmitEvent(imesa, SAVAGE_WAIT_3D);
1743 if (imesa->CurrentTexObj[0])
1744 imesa->CurrentTexObj[0]->timestamp = e;
1745 if (imesa->CurrentTexObj[1])
1746 imesa->CurrentTexObj[1]->timestamp = e;
1747 }
1748 }
1749
1750
1751 static void savageUpdateTextureState_s4( GLcontext *ctx )
1752 {
1753 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1754
1755 /* When a texture is about to change or be disabled, timestamp the
1756 * old texture(s). We'll have to wait for this time stamp before
1757 * uploading anything to the same texture heap.
1758 */
1759 if ((imesa->CurrentTexObj[0] && ctx->Texture.Unit[0]._ReallyEnabled &&
1760 ctx->Texture.Unit[0]._Current->DriverData != imesa->CurrentTexObj[0]) ||
1761 (imesa->CurrentTexObj[1] && ctx->Texture.Unit[1]._ReallyEnabled &&
1762 ctx->Texture.Unit[1]._Current->DriverData != imesa->CurrentTexObj[1]) ||
1763 (imesa->CurrentTexObj[0] && !ctx->Texture.Unit[0]._ReallyEnabled) ||
1764 (imesa->CurrentTexObj[1] && !ctx->Texture.Unit[1]._ReallyEnabled))
1765 savageTimestampTextures(imesa);
1766
1767 if (imesa->CurrentTexObj[0]) imesa->CurrentTexObj[0]->bound &= ~1;
1768 if (imesa->CurrentTexObj[1]) imesa->CurrentTexObj[1]->bound &= ~2;
1769 imesa->CurrentTexObj[0] = 0;
1770 imesa->CurrentTexObj[1] = 0;
1771 savageUpdateTex0State_s4( ctx );
1772 savageUpdateTex1State_s4( ctx );
1773 imesa->dirty |= (SAVAGE_UPLOAD_TEX0 |
1774 SAVAGE_UPLOAD_TEX1);
1775 }
1776 static void savageUpdateTextureState_s3d( GLcontext *ctx )
1777 {
1778 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1779
1780 /* When a texture is about to change or be disabled, timestamp the
1781 * old texture(s). We'll have to wait for this time stamp before
1782 * uploading anything to the same texture heap.
1783 */
1784 if ((imesa->CurrentTexObj[0] && ctx->Texture.Unit[0]._ReallyEnabled &&
1785 ctx->Texture.Unit[0]._Current->DriverData != imesa->CurrentTexObj[0]) ||
1786 (imesa->CurrentTexObj[0] && !ctx->Texture.Unit[0]._ReallyEnabled))
1787 savageTimestampTextures(imesa);
1788
1789 if (imesa->CurrentTexObj[0]) imesa->CurrentTexObj[0]->bound &= ~1;
1790 imesa->CurrentTexObj[0] = 0;
1791 savageUpdateTexState_s3d( ctx );
1792 imesa->dirty |= (SAVAGE_UPLOAD_TEX0);
1793 }
1794 void savageUpdateTextureState( GLcontext *ctx)
1795 {
1796 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
1797 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_FALSE);
1798 FALLBACK(ctx, SAVAGE_FALLBACK_PROJ_TEXTURE, GL_FALSE);
1799 if (imesa->savageScreen->chipset >= S3_SAVAGE4)
1800 savageUpdateTextureState_s4 (ctx);
1801 else
1802 savageUpdateTextureState_s3d (ctx);
1803 }
1804
1805
1806
1807 /*****************************************
1808 * DRIVER functions
1809 *****************************************/
1810
1811 static void savageTexEnv( GLcontext *ctx, GLenum target,
1812 GLenum pname, const GLfloat *param )
1813 {
1814 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
1815
1816 if (pname == GL_TEXTURE_ENV_MODE) {
1817
1818 imesa->new_state |= SAVAGE_NEW_TEXTURE;
1819
1820 } else if (pname == GL_TEXTURE_ENV_COLOR) {
1821
1822 struct gl_texture_unit *texUnit =
1823 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1824 const GLfloat *fc = texUnit->EnvColor;
1825 GLuint r, g, b, a;
1826 CLAMPED_FLOAT_TO_UBYTE(r, fc[0]);
1827 CLAMPED_FLOAT_TO_UBYTE(g, fc[1]);
1828 CLAMPED_FLOAT_TO_UBYTE(b, fc[2]);
1829 CLAMPED_FLOAT_TO_UBYTE(a, fc[3]);
1830
1831 imesa->texEnvColor = ((a << 24) | (r << 16) |
1832 (g << 8) | (b << 0));
1833
1834
1835 }
1836 }
1837
1838 /* Update the heap's time stamp, so the new image is not uploaded
1839 * while the old one is still in use. If the texture that is going to
1840 * be changed is currently bound, we need to timestamp the texture
1841 * first. */
1842 static void savageTexImageChanged (savageTexObjPtr t) {
1843 if (t->base.heap) {
1844 if (t->base.bound)
1845 savageTimestampTextures(
1846 (savageContextPtr)t->base.heap->driverContext);
1847 if (t->base.timestamp > t->base.heap->timestamp)
1848 t->base.heap->timestamp = t->base.timestamp;
1849 }
1850 }
1851
1852 static void savageTexImage1D( GLcontext *ctx, GLenum target, GLint level,
1853 GLint internalFormat,
1854 GLint width, GLint border,
1855 GLenum format, GLenum type, const GLvoid *pixels,
1856 const struct gl_pixelstore_attrib *packing,
1857 struct gl_texture_object *texObj,
1858 struct gl_texture_image *texImage )
1859 {
1860 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1861 if (t) {
1862 savageTexImageChanged (t);
1863 } else {
1864 t = savageAllocTexObj(texObj);
1865 if (!t) {
1866 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
1867 return;
1868 }
1869 }
1870 _mesa_store_teximage1d( ctx, target, level, internalFormat,
1871 width, border, format, type,
1872 pixels, packing, texObj, texImage );
1873 t->base.dirty_images[0] |= (1 << level);
1874 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1875 }
1876
1877 static void savageTexSubImage1D( GLcontext *ctx,
1878 GLenum target,
1879 GLint level,
1880 GLint xoffset,
1881 GLsizei width,
1882 GLenum format, GLenum type,
1883 const GLvoid *pixels,
1884 const struct gl_pixelstore_attrib *packing,
1885 struct gl_texture_object *texObj,
1886 struct gl_texture_image *texImage )
1887 {
1888 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1889 assert( t ); /* this _should_ be true */
1890 if (t) {
1891 savageTexImageChanged (t);
1892 savageMarkDirtyTiles(t, level, texImage->Width2, 1,
1893 xoffset, 0, width, 1);
1894 } else {
1895 t = savageAllocTexObj(texObj);
1896 if (!t) {
1897 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage1D");
1898 return;
1899 }
1900 t->base.dirty_images[0] |= (1 << level);
1901 }
1902 _mesa_store_texsubimage1d(ctx, target, level, xoffset, width,
1903 format, type, pixels, packing, texObj,
1904 texImage);
1905 t->dirtySubImages |= (1 << level);
1906 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1907 }
1908
1909 static void savageTexImage2D( GLcontext *ctx, GLenum target, GLint level,
1910 GLint internalFormat,
1911 GLint width, GLint height, GLint border,
1912 GLenum format, GLenum type, const GLvoid *pixels,
1913 const struct gl_pixelstore_attrib *packing,
1914 struct gl_texture_object *texObj,
1915 struct gl_texture_image *texImage )
1916 {
1917 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1918 if (t) {
1919 savageTexImageChanged (t);
1920 } else {
1921 t = savageAllocTexObj(texObj);
1922 if (!t) {
1923 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
1924 return;
1925 }
1926 }
1927 _mesa_store_teximage2d( ctx, target, level, internalFormat,
1928 width, height, border, format, type,
1929 pixels, packing, texObj, texImage );
1930 t->base.dirty_images[0] |= (1 << level);
1931 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1932 }
1933
1934 static void savageTexSubImage2D( GLcontext *ctx,
1935 GLenum target,
1936 GLint level,
1937 GLint xoffset, GLint yoffset,
1938 GLsizei width, GLsizei height,
1939 GLenum format, GLenum type,
1940 const GLvoid *pixels,
1941 const struct gl_pixelstore_attrib *packing,
1942 struct gl_texture_object *texObj,
1943 struct gl_texture_image *texImage )
1944 {
1945 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1946 assert( t ); /* this _should_ be true */
1947 if (t) {
1948 savageTexImageChanged (t);
1949 savageMarkDirtyTiles(t, level, texImage->Width2, texImage->Height2,
1950 xoffset, yoffset, width, height);
1951 } else {
1952 t = savageAllocTexObj(texObj);
1953 if (!t) {
1954 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
1955 return;
1956 }
1957 t->base.dirty_images[0] |= (1 << level);
1958 }
1959 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
1960 height, format, type, pixels, packing, texObj,
1961 texImage);
1962 t->dirtySubImages |= (1 << level);
1963 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1964 }
1965
1966 static void
1967 savageCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
1968 GLint internalFormat,
1969 GLint width, GLint height, GLint border,
1970 GLsizei imageSize, const GLvoid *data,
1971 struct gl_texture_object *texObj,
1972 struct gl_texture_image *texImage )
1973 {
1974 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1975 if (t) {
1976 savageTexImageChanged (t);
1977 } else {
1978 t = savageAllocTexObj(texObj);
1979 if (!t) {
1980 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
1981 return;
1982 }
1983 }
1984 _mesa_store_compressed_teximage2d( ctx, target, level, internalFormat,
1985 width, height, border, imageSize,
1986 data, texObj, texImage );
1987 t->base.dirty_images[0] |= (1 << level);
1988 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1989 }
1990
1991 static void
1992 savageCompressedTexSubImage2D( GLcontext *ctx,
1993 GLenum target,
1994 GLint level,
1995 GLint xoffset, GLint yoffset,
1996 GLsizei width, GLsizei height,
1997 GLenum format, GLsizei imageSize,
1998 const GLvoid *data,
1999 struct gl_texture_object *texObj,
2000 struct gl_texture_image *texImage )
2001 {
2002 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
2003 assert( t ); /* this _should_ be true */
2004 if (t) {
2005 savageTexImageChanged (t);
2006 savageMarkDirtyTiles(t, level, texImage->Width2, texImage->Height2,
2007 xoffset, yoffset, width, height);
2008 } else {
2009 t = savageAllocTexObj(texObj);
2010 if (!t) {
2011 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
2012 return;
2013 }
2014 t->base.dirty_images[0] |= (1 << level);
2015 }
2016 _mesa_store_compressed_texsubimage2d(ctx, target, level, xoffset, yoffset,
2017 width, height, format, imageSize,
2018 data, texObj, texImage);
2019 t->dirtySubImages |= (1 << level);
2020 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
2021 }
2022
2023 static void savageTexParameter( GLcontext *ctx, GLenum target,
2024 struct gl_texture_object *tObj,
2025 GLenum pname, const GLfloat *params )
2026 {
2027 savageTexObjPtr t = (savageTexObjPtr) tObj->DriverData;
2028 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
2029
2030 if (!t || (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D))
2031 return;
2032
2033 switch (pname) {
2034 case GL_TEXTURE_MIN_FILTER:
2035 case GL_TEXTURE_MAG_FILTER:
2036 savageSetTexFilter(t,tObj->MinFilter,tObj->MagFilter);
2037 break;
2038
2039 case GL_TEXTURE_WRAP_S:
2040 case GL_TEXTURE_WRAP_T:
2041 savageSetTexWrapping(t,tObj->WrapS,tObj->WrapT);
2042 break;
2043
2044 case GL_TEXTURE_BORDER_COLOR:
2045 savageSetTexBorderColor(t,tObj->_BorderChan);
2046 break;
2047
2048 default:
2049 return;
2050 }
2051
2052 imesa->new_state |= SAVAGE_NEW_TEXTURE;
2053 }
2054
2055 static void savageBindTexture( GLcontext *ctx, GLenum target,
2056 struct gl_texture_object *tObj )
2057 {
2058 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
2059
2060 assert( (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D) ||
2061 (tObj->DriverData != NULL) );
2062
2063 imesa->new_state |= SAVAGE_NEW_TEXTURE;
2064 }
2065
2066 static void savageDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
2067 {
2068 driTextureObject *t = (driTextureObject *)tObj->DriverData;
2069 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
2070
2071 if (t) {
2072 if (t->bound)
2073 savageTimestampTextures(imesa);
2074
2075 driDestroyTextureObject(t);
2076 }
2077 /* Free mipmap images and the texture object itself */
2078 _mesa_delete_texture_object(ctx, tObj);
2079 }
2080
2081
2082 static struct gl_texture_object *
2083 savageNewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
2084 {
2085 struct gl_texture_object *obj;
2086 obj = _mesa_new_texture_object(ctx, name, target);
2087 savageAllocTexObj( obj );
2088
2089 return obj;
2090 }
2091
2092 void savageDDInitTextureFuncs( struct dd_function_table *functions )
2093 {
2094 functions->TexEnv = savageTexEnv;
2095 functions->ChooseTextureFormat = savageChooseTextureFormat;
2096 functions->TexImage1D = savageTexImage1D;
2097 functions->TexSubImage1D = savageTexSubImage1D;
2098 functions->TexImage2D = savageTexImage2D;
2099 functions->TexSubImage2D = savageTexSubImage2D;
2100 functions->CompressedTexImage2D = savageCompressedTexImage2D;
2101 functions->CompressedTexSubImage2D = savageCompressedTexSubImage2D;
2102 functions->BindTexture = savageBindTexture;
2103 functions->NewTextureObject = savageNewTextureObject;
2104 functions->DeleteTexture = savageDeleteTexture;
2105 functions->IsTextureResident = driIsTextureResident;
2106 functions->TexParameter = savageTexParameter;
2107
2108 /* Texel fetching with our custom texture formats works just like
2109 * the standard argb formats. */
2110 _savage_texformat_a1114444.FetchTexel1D = _mesa_texformat_argb4444.FetchTexel1D;
2111 _savage_texformat_a1114444.FetchTexel2D = _mesa_texformat_argb4444.FetchTexel2D;
2112 _savage_texformat_a1114444.FetchTexel3D = _mesa_texformat_argb4444.FetchTexel3D;
2113 _savage_texformat_a1114444.FetchTexel1Df= _mesa_texformat_argb4444.FetchTexel1Df;
2114 _savage_texformat_a1114444.FetchTexel2Df= _mesa_texformat_argb4444.FetchTexel2Df;
2115 _savage_texformat_a1114444.FetchTexel3Df= _mesa_texformat_argb4444.FetchTexel3Df;
2116
2117 _savage_texformat_a1118888.FetchTexel1D = _mesa_texformat_argb8888.FetchTexel1D;
2118 _savage_texformat_a1118888.FetchTexel2D = _mesa_texformat_argb8888.FetchTexel2D;
2119 _savage_texformat_a1118888.FetchTexel3D = _mesa_texformat_argb8888.FetchTexel3D;
2120 _savage_texformat_a1118888.FetchTexel1Df= _mesa_texformat_argb8888.FetchTexel1Df;
2121 _savage_texformat_a1118888.FetchTexel2Df= _mesa_texformat_argb8888.FetchTexel2Df;
2122 _savage_texformat_a1118888.FetchTexel3Df= _mesa_texformat_argb8888.FetchTexel3Df;
2123 }