In gl_texture_image, replace ImageStride with an ImageOffsets array.
[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 */
530 static GLboolean
531 _savage_texstore_a1114444(TEXSTORE_PARAMS)
532 {
533 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
534 baseInternalFormat,
535 baseInternalFormat,
536 srcWidth, srcHeight, srcDepth,
537 srcFormat, srcType, srcAddr,
538 srcPacking);
539 const GLchan *src = tempImage;
540 GLint img, row, col;
541
542 ASSERT(dstFormat == &_savage_texformat_a1114444);
543 ASSERT(baseInternalFormat == GL_ALPHA);
544
545 if (!tempImage)
546 return GL_FALSE;
547 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
548 for (img = 0; img < srcDepth; img++) {
549 GLubyte *dstRow = (GLubyte *) dstAddr
550 + dstImageOffsets[dstZoffset + img] * dstFormat->TexelBytes
551 + dstYoffset * dstRowStride
552 + dstXoffset * dstFormat->TexelBytes;
553 for (row = 0; row < srcHeight; row++) {
554 GLushort *dstUI = (GLushort *) dstRow;
555 for (col = 0; col < srcWidth; col++) {
556 dstUI[col] = PACK_COLOR_4444( CHAN_TO_UBYTE(src[0]),
557 255, 255, 255 );
558 src += 1;
559 }
560 dstRow += dstRowStride;
561 }
562 }
563 _mesa_free((void *) tempImage);
564
565 return GL_TRUE;
566 }
567
568
569 static GLboolean
570 _savage_texstore_a1118888(TEXSTORE_PARAMS)
571 {
572 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
573 baseInternalFormat,
574 baseInternalFormat,
575 srcWidth, srcHeight, srcDepth,
576 srcFormat, srcType, srcAddr,
577 srcPacking);
578 const GLchan *src = tempImage;
579 GLint img, row, col;
580
581 ASSERT(dstFormat == &_savage_texformat_a1118888);
582 ASSERT(baseInternalFormat == GL_ALPHA);
583
584 if (!tempImage)
585 return GL_FALSE;
586 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
587 for (img = 0; img < srcDepth; img++) {
588 GLubyte *dstRow = (GLubyte *) dstAddr
589 + dstImageOffsets[dstZoffset + img] * dstFormat->TexelBytes
590 + dstYoffset * dstRowStride
591 + dstXoffset * dstFormat->TexelBytes;
592 for (row = 0; row < srcHeight; row++) {
593 GLuint *dstUI = (GLuint *) dstRow;
594 for (col = 0; col < srcWidth; col++) {
595 dstUI[col] = PACK_COLOR_8888( CHAN_TO_UBYTE(src[0]),
596 255, 255, 255 );
597 src += 1;
598 }
599 dstRow += dstRowStride;
600 }
601 }
602 _mesa_free((void *) tempImage);
603
604 return GL_TRUE;
605 }
606
607
608 static struct gl_texture_format _savage_texformat_a1114444 = {
609 MESA_FORMAT_ARGB4444, /* MesaFormat */
610 GL_RGBA, /* BaseFormat */
611 GL_UNSIGNED_NORMALIZED_ARB, /* DataType */
612 4, /* RedBits */
613 4, /* GreenBits */
614 4, /* BlueBits */
615 4, /* AlphaBits */
616 0, /* LuminanceBits */
617 0, /* IntensityBits */
618 0, /* IndexBits */
619 0, /* DepthBits */
620 0, /* StencilBits */
621 2, /* TexelBytes */
622 _savage_texstore_a1114444, /* StoreTexImageFunc */
623 NULL, NULL, NULL, NULL, NULL, NULL /* FetchTexel* filled in by
624 * savageDDInitTextureFuncs */
625 };
626 static struct gl_texture_format _savage_texformat_a1118888 = {
627 MESA_FORMAT_ARGB8888, /* MesaFormat */
628 GL_RGBA, /* BaseFormat */
629 GL_UNSIGNED_NORMALIZED_ARB, /* DataType */
630 8, /* RedBits */
631 8, /* GreenBits */
632 8, /* BlueBits */
633 8, /* AlphaBits */
634 0, /* LuminanceBits */
635 0, /* IntensityBits */
636 0, /* IndexBits */
637 0, /* DepthBits */
638 0, /* StencilBits */
639 4, /* TexelBytes */
640 _savage_texstore_a1118888, /* StoreTexImageFunc */
641 NULL, NULL, NULL, NULL, NULL, NULL /* FetchTexel* filled in by
642 * savageDDInitTextureFuncs */
643 };
644
645
646 /* Called by the _mesa_store_teximage[123]d() functions. */
647 static const struct gl_texture_format *
648 savageChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
649 GLenum format, GLenum type )
650 {
651 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
652 const GLboolean do32bpt =
653 ( imesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_32 );
654 const GLboolean force16bpt =
655 ( imesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FORCE_16 );
656 const GLboolean isSavage4 = (imesa->savageScreen->chipset >= S3_SAVAGE4);
657 (void) format;
658
659 switch ( internalFormat ) {
660 case 4:
661 case GL_RGBA:
662 case GL_COMPRESSED_RGBA:
663 switch ( type ) {
664 case GL_UNSIGNED_INT_10_10_10_2:
665 case GL_UNSIGNED_INT_2_10_10_10_REV:
666 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555;
667 case GL_UNSIGNED_SHORT_4_4_4_4:
668 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
669 return &_mesa_texformat_argb4444;
670 case GL_UNSIGNED_SHORT_5_5_5_1:
671 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
672 return &_mesa_texformat_argb1555;
673 default:
674 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
675 }
676
677 case 3:
678 case GL_RGB:
679 case GL_COMPRESSED_RGB:
680 switch ( type ) {
681 case GL_UNSIGNED_SHORT_4_4_4_4:
682 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
683 return &_mesa_texformat_argb4444;
684 case GL_UNSIGNED_SHORT_5_5_5_1:
685 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
686 return &_mesa_texformat_argb1555;
687 case GL_UNSIGNED_SHORT_5_6_5:
688 case GL_UNSIGNED_SHORT_5_6_5_REV:
689 return &_mesa_texformat_rgb565;
690 default:
691 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
692 }
693
694 case GL_RGBA8:
695 case GL_RGBA12:
696 case GL_RGBA16:
697 return !force16bpt ?
698 &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
699
700 case GL_RGB10_A2:
701 return !force16bpt ?
702 &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555;
703
704 case GL_RGBA4:
705 case GL_RGBA2:
706 return &_mesa_texformat_argb4444;
707
708 case GL_RGB5_A1:
709 return &_mesa_texformat_argb1555;
710
711 case GL_RGB8:
712 case GL_RGB10:
713 case GL_RGB12:
714 case GL_RGB16:
715 return !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
716
717 case GL_RGB5:
718 case GL_RGB4:
719 case GL_R3_G3_B2:
720 return &_mesa_texformat_rgb565;
721
722 case GL_ALPHA:
723 case GL_COMPRESSED_ALPHA:
724 return isSavage4 ? &_mesa_texformat_a8 : (
725 do32bpt ? &_savage_texformat_a1118888 : &_savage_texformat_a1114444);
726 case GL_ALPHA4:
727 return isSavage4 ? &_mesa_texformat_a8 : &_savage_texformat_a1114444;
728 case GL_ALPHA8:
729 case GL_ALPHA12:
730 case GL_ALPHA16:
731 return isSavage4 ? &_mesa_texformat_a8 : (
732 !force16bpt ? &_savage_texformat_a1118888 : &_savage_texformat_a1114444);
733
734 case 1:
735 case GL_LUMINANCE:
736 case GL_COMPRESSED_LUMINANCE:
737 /* no alpha, but use argb1555 in 16bit case to get pure grey values */
738 return isSavage4 ? &_mesa_texformat_l8 : (
739 do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555);
740 case GL_LUMINANCE4:
741 return isSavage4 ? &_mesa_texformat_l8 : &_mesa_texformat_argb1555;
742 case GL_LUMINANCE8:
743 case GL_LUMINANCE12:
744 case GL_LUMINANCE16:
745 return isSavage4 ? &_mesa_texformat_l8 : (
746 !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555);
747
748 case 2:
749 case GL_LUMINANCE_ALPHA:
750 case GL_COMPRESSED_LUMINANCE_ALPHA:
751 /* Savage4 has a al44 texture format. But it's not supported by Mesa. */
752 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
753 case GL_LUMINANCE4_ALPHA4:
754 case GL_LUMINANCE6_ALPHA2:
755 return &_mesa_texformat_argb4444;
756 case GL_LUMINANCE8_ALPHA8:
757 case GL_LUMINANCE12_ALPHA4:
758 case GL_LUMINANCE12_ALPHA12:
759 case GL_LUMINANCE16_ALPHA16:
760 return !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
761 #if 0
762 /* TFT_I8 produces garbage on ProSavageDDR and subsequent texture
763 * disable keeps rendering garbage. Disabled for now. */
764 case GL_INTENSITY:
765 case GL_COMPRESSED_INTENSITY:
766 return isSavage4 ? &_mesa_texformat_i8 : (
767 do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444);
768 case GL_INTENSITY4:
769 return isSavage4 ? &_mesa_texformat_i8 : &_mesa_texformat_argb4444;
770 case GL_INTENSITY8:
771 case GL_INTENSITY12:
772 case GL_INTENSITY16:
773 return isSavage4 ? &_mesa_texformat_i8 : (
774 !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444);
775 #else
776 case GL_INTENSITY:
777 case GL_COMPRESSED_INTENSITY:
778 return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
779 case GL_INTENSITY4:
780 return &_mesa_texformat_argb4444;
781 case GL_INTENSITY8:
782 case GL_INTENSITY12:
783 case GL_INTENSITY16:
784 return !force16bpt ? &_mesa_texformat_argb8888 :
785 &_mesa_texformat_argb4444;
786 #endif
787
788 case GL_RGB_S3TC:
789 case GL_RGB4_S3TC:
790 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
791 return &_mesa_texformat_rgb_dxt1;
792 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
793 return &_mesa_texformat_rgba_dxt1;
794
795 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
796 return &_mesa_texformat_rgba_dxt3;
797
798 case GL_RGBA_S3TC:
799 case GL_RGBA4_S3TC:
800 if (!isSavage4)
801 /* Not the best choice but Savage3D/MX/IX don't support DXT3 or DXT5. */
802 return &_mesa_texformat_rgba_dxt1;
803 /* fall through */
804 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
805 return &_mesa_texformat_rgba_dxt5;
806
807 /*
808 case GL_COLOR_INDEX:
809 case GL_COLOR_INDEX1_EXT:
810 case GL_COLOR_INDEX2_EXT:
811 case GL_COLOR_INDEX4_EXT:
812 case GL_COLOR_INDEX8_EXT:
813 case GL_COLOR_INDEX12_EXT:
814 case GL_COLOR_INDEX16_EXT:
815 return &_mesa_texformat_ci8;
816 */
817 default:
818 _mesa_problem(ctx, "unexpected texture format in %s", __FUNCTION__);
819 return NULL;
820 }
821 }
822
823 static void savageSetTexImages( savageContextPtr imesa,
824 const struct gl_texture_object *tObj )
825 {
826 savageTexObjPtr t = (savageTexObjPtr) tObj->DriverData;
827 struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
828 GLuint offset, i, textureFormat, tileIndex, size;
829 GLint firstLevel, lastLevel;
830
831 assert(t);
832 assert(image);
833
834 switch (image->TexFormat->MesaFormat) {
835 case MESA_FORMAT_ARGB8888:
836 textureFormat = TFT_ARGB8888;
837 t->texelBytes = tileIndex = 4;
838 break;
839 case MESA_FORMAT_ARGB1555:
840 textureFormat = TFT_ARGB1555;
841 t->texelBytes = tileIndex = 2;
842 break;
843 case MESA_FORMAT_ARGB4444:
844 textureFormat = TFT_ARGB4444;
845 t->texelBytes = tileIndex = 2;
846 break;
847 case MESA_FORMAT_RGB565:
848 textureFormat = TFT_RGB565;
849 t->texelBytes = tileIndex = 2;
850 break;
851 case MESA_FORMAT_L8:
852 textureFormat = TFT_L8;
853 t->texelBytes = tileIndex = 1;
854 break;
855 case MESA_FORMAT_I8:
856 textureFormat = TFT_I8;
857 t->texelBytes = tileIndex = 1;
858 break;
859 case MESA_FORMAT_A8:
860 textureFormat = TFT_A8;
861 t->texelBytes = tileIndex = 1;
862 break;
863 case MESA_FORMAT_RGB_DXT1:
864 textureFormat = TFT_S3TC4Bit;
865 tileIndex = TILE_INDEX_DXT1;
866 t->texelBytes = 8;
867 break;
868 case MESA_FORMAT_RGBA_DXT1:
869 textureFormat = TFT_S3TC4Bit;
870 tileIndex = TILE_INDEX_DXT1;
871 t->texelBytes = 8;
872 break;
873 case MESA_FORMAT_RGBA_DXT3:
874 textureFormat = TFT_S3TC4A4Bit;
875 tileIndex = TILE_INDEX_DXTn;
876 t->texelBytes = 16;
877 break;
878 case MESA_FORMAT_RGBA_DXT5:
879 textureFormat = TFT_S3TC4CA4Bit;
880 tileIndex = TILE_INDEX_DXTn;
881 t->texelBytes = 16;
882 break;
883 default:
884 _mesa_problem(imesa->glCtx, "Bad texture format in %s", __FUNCTION__);
885 return;
886 }
887 t->hwFormat = textureFormat;
888
889 /* Select tiling format depending on the chipset and texture format */
890 if (imesa->savageScreen->chipset <= S3_SAVAGE4)
891 t->tileInfo = &tileInfo_s3d_s4[tileIndex];
892 else
893 t->tileInfo = &tileInfo_pro[tileIndex];
894
895 /* Compute which mipmap levels we really want to send to the hardware.
896 */
897 driCalculateTextureFirstLastLevel( &t->base );
898 firstLevel = t->base.firstLevel;
899 lastLevel = t->base.lastLevel;
900
901 /* Figure out the size now (and count the levels). Upload won't be
902 * done until later. If the number of tiles changes, it means that
903 * this function is called for the first time on this tex object or
904 * the image or the destination color format changed. So all tiles
905 * are marked as dirty.
906 */
907 offset = 0;
908 size = 1;
909 for ( i = firstLevel ; i <= lastLevel && tObj->Image[0][i] ; i++ ) {
910 GLuint nTiles;
911 nTiles = savageTexImageTiles (image->Width2, image->Height2, t->tileInfo);
912 if (t->image[i].nTiles != nTiles) {
913 GLuint words = (nTiles + 31) / 32;
914 if (t->image[i].nTiles != 0) {
915 free(t->image[i].dirtyTiles);
916 }
917 t->image[i].dirtyTiles = malloc(words*sizeof(GLuint));
918 memset(t->image[i].dirtyTiles, ~0, words*sizeof(GLuint));
919 }
920 t->image[i].nTiles = nTiles;
921
922 t->image[i].offset = offset;
923
924 image = tObj->Image[0][i];
925 if (t->texelBytes >= 8)
926 size = savageCompressedTexImageSize (image->Width2, image->Height2,
927 t->texelBytes);
928 else
929 size = savageTexImageSize (image->Width2, image->Height2,
930 t->texelBytes);
931 offset += size;
932 }
933
934 t->base.lastLevel = i-1;
935 t->base.totalSize = offset;
936 /* the last three mipmap levels don't add to the offset. They are packed
937 * into 64 pixels. */
938 if (size == 0)
939 t->base.totalSize += (t->texelBytes >= 8 ? 4 : 64) * t->texelBytes;
940 /* 2k-aligned (really needed?) */
941 t->base.totalSize = (t->base.totalSize + 2047UL) & ~2047UL;
942 }
943
944 void savageDestroyTexObj(savageContextPtr imesa, savageTexObjPtr t)
945 {
946 GLuint i;
947
948 /* Free dirty tiles bit vectors */
949 for (i = 0; i < SAVAGE_TEX_MAXLEVELS; ++i) {
950 if (t->image[i].nTiles)
951 free (t->image[i].dirtyTiles);
952 }
953
954 /* See if it was the driver's current object.
955 */
956 if ( imesa != NULL )
957 {
958 for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ )
959 {
960 if ( &t->base == imesa->CurrentTexObj[ i ] ) {
961 assert( t->base.bound & (1 << i) );
962 imesa->CurrentTexObj[ i ] = NULL;
963 }
964 }
965 }
966 }
967
968 /* Upload a texture's images to one of the texture heaps. May have to
969 * eject our own and/or other client's texture objects to make room
970 * for the upload.
971 */
972 static void savageUploadTexImages( savageContextPtr imesa, savageTexObjPtr t )
973 {
974 const GLint numLevels = t->base.lastLevel - t->base.firstLevel + 1;
975 GLuint i;
976
977 assert(t);
978
979 LOCK_HARDWARE(imesa);
980
981 /* Do we need to eject LRU texture objects?
982 */
983 if (!t->base.memBlock) {
984 GLint heap;
985 GLuint ofs;
986
987 heap = driAllocateTexture(imesa->textureHeaps, imesa->lastTexHeap,
988 (driTextureObject *)t);
989 if (heap == -1) {
990 UNLOCK_HARDWARE(imesa);
991 return;
992 }
993
994 ofs = t->base.memBlock->ofs;
995 t->setup.physAddr = imesa->savageScreen->textureOffset[heap] + ofs;
996 t->bufAddr = (GLubyte *)imesa->savageScreen->texVirtual[heap] + ofs;
997 imesa->dirty |= SAVAGE_UPLOAD_GLOBAL; /* FIXME: really needed? */
998 }
999
1000 /* Let the world know we've used this memory recently.
1001 */
1002 driUpdateTextureLRU( &t->base );
1003 UNLOCK_HARDWARE(imesa);
1004
1005 if (t->base.dirty_images[0] || t->dirtySubImages) {
1006 if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX)
1007 fprintf(stderr, "Texture upload: |");
1008
1009 /* Heap timestamps are only reliable with Savage DRM 2.3.x or
1010 * later. Earlier versions had only 16 bit time stamps which
1011 * would wrap too frequently. */
1012 if (imesa->savageScreen->driScrnPriv->drmMinor >= 3) {
1013 unsigned int heap = t->base.heap->heapId;
1014 LOCK_HARDWARE(imesa);
1015 savageWaitEvent (imesa, imesa->textureHeaps[heap]->timestamp);
1016 } else {
1017 savageFlushVertices (imesa);
1018 LOCK_HARDWARE(imesa);
1019 savageFlushCmdBufLocked (imesa, GL_FALSE);
1020 WAIT_IDLE_EMPTY_LOCKED(imesa);
1021 }
1022
1023 for (i = 0 ; i < numLevels ; i++) {
1024 const GLint j = t->base.firstLevel + i; /* the texObj's level */
1025 if (t->base.dirty_images[0] & (1 << j)) {
1026 savageMarkAllTiles(t, j);
1027 if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX)
1028 fprintf (stderr, "*");
1029 } else if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX) {
1030 if (t->dirtySubImages & (1 << j))
1031 fprintf (stderr, ".");
1032 else
1033 fprintf (stderr, " ");
1034 }
1035 if ((t->base.dirty_images[0] | t->dirtySubImages) & (1 << j))
1036 savageUploadTexLevel( t, j );
1037 }
1038
1039 UNLOCK_HARDWARE(imesa);
1040 t->base.dirty_images[0] = 0;
1041 t->dirtySubImages = 0;
1042
1043 if (SAVAGE_DEBUG & DEBUG_VERBOSE_TEX)
1044 fprintf(stderr, "|\n");
1045 }
1046 }
1047
1048
1049 static void
1050 savage4_set_wrap_mode( savageContextPtr imesa, unsigned unit,
1051 GLenum s_mode, GLenum t_mode )
1052 {
1053 switch( s_mode ) {
1054 case GL_REPEAT:
1055 imesa->regs.s4.texCtrl[ unit ].ni.uMode = TAM_Wrap;
1056 break;
1057 case GL_CLAMP:
1058 case GL_CLAMP_TO_EDGE:
1059 imesa->regs.s4.texCtrl[ unit ].ni.uMode = TAM_Clamp;
1060 break;
1061 case GL_MIRRORED_REPEAT:
1062 imesa->regs.s4.texCtrl[ unit ].ni.uMode = TAM_Mirror;
1063 break;
1064 }
1065
1066 switch( t_mode ) {
1067 case GL_REPEAT:
1068 imesa->regs.s4.texCtrl[ unit ].ni.vMode = TAM_Wrap;
1069 break;
1070 case GL_CLAMP:
1071 case GL_CLAMP_TO_EDGE:
1072 imesa->regs.s4.texCtrl[ unit ].ni.vMode = TAM_Clamp;
1073 break;
1074 case GL_MIRRORED_REPEAT:
1075 imesa->regs.s4.texCtrl[ unit ].ni.vMode = TAM_Mirror;
1076 break;
1077 }
1078 }
1079
1080
1081 /**
1082 * Sets the hardware bits for the specified GL texture filter modes.
1083 *
1084 * \todo
1085 * Does the Savage4 have the ability to select the magnification filter?
1086 */
1087 static void
1088 savage4_set_filter_mode( savageContextPtr imesa, unsigned unit,
1089 GLenum minFilter, GLenum magFilter )
1090 {
1091 (void) magFilter;
1092
1093 switch (minFilter) {
1094 case GL_NEAREST:
1095 imesa->regs.s4.texCtrl[ unit ].ni.filterMode = TFM_Point;
1096 imesa->regs.s4.texCtrl[ unit ].ni.mipmapEnable = GL_FALSE;
1097 break;
1098
1099 case GL_LINEAR:
1100 imesa->regs.s4.texCtrl[ unit ].ni.filterMode = TFM_Bilin;
1101 imesa->regs.s4.texCtrl[ unit ].ni.mipmapEnable = GL_FALSE;
1102 break;
1103
1104 case GL_NEAREST_MIPMAP_NEAREST:
1105 imesa->regs.s4.texCtrl[ unit ].ni.filterMode = TFM_Point;
1106 imesa->regs.s4.texCtrl[ unit ].ni.mipmapEnable = GL_TRUE;
1107 break;
1108
1109 case GL_LINEAR_MIPMAP_NEAREST:
1110 imesa->regs.s4.texCtrl[ unit ].ni.filterMode = TFM_Bilin;
1111 imesa->regs.s4.texCtrl[ unit ].ni.mipmapEnable = GL_TRUE;
1112 break;
1113
1114 case GL_NEAREST_MIPMAP_LINEAR:
1115 case GL_LINEAR_MIPMAP_LINEAR:
1116 imesa->regs.s4.texCtrl[ unit ].ni.filterMode = TFM_Trilin;
1117 imesa->regs.s4.texCtrl[ unit ].ni.mipmapEnable = GL_TRUE;
1118 break;
1119 }
1120 }
1121
1122
1123 static void savageUpdateTex0State_s4( GLcontext *ctx )
1124 {
1125 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1126 struct gl_texture_object *tObj;
1127 struct gl_texture_image *image;
1128 savageTexObjPtr t;
1129 GLuint format;
1130
1131 /* disable */
1132 imesa->regs.s4.texDescr.ni.tex0En = GL_FALSE;
1133 imesa->regs.s4.texBlendCtrl[0].ui = TBC_NoTexMap;
1134 imesa->regs.s4.texCtrl[0].ui = 0x20f040;
1135 if (ctx->Texture.Unit[0]._ReallyEnabled == 0)
1136 return;
1137
1138 tObj = ctx->Texture.Unit[0]._Current;
1139 if ((ctx->Texture.Unit[0]._ReallyEnabled & ~(TEXTURE_1D_BIT|TEXTURE_2D_BIT))
1140 || tObj->Image[0][tObj->BaseLevel]->Border > 0) {
1141 /* 3D texturing enabled, or texture border - fallback */
1142 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1143 return;
1144 }
1145
1146 /* Do 2D texture setup */
1147
1148 t = tObj->DriverData;
1149 if (!t) {
1150 t = savageAllocTexObj( tObj );
1151 if (!t)
1152 return;
1153 }
1154
1155 imesa->CurrentTexObj[0] = &t->base;
1156 t->base.bound |= 1;
1157
1158 if (t->base.dirty_images[0] || t->dirtySubImages) {
1159 savageSetTexImages(imesa, tObj);
1160 savageUploadTexImages(imesa, t);
1161 }
1162
1163 driUpdateTextureLRU( &t->base );
1164
1165 format = tObj->Image[0][tObj->BaseLevel]->_BaseFormat;
1166
1167 switch (ctx->Texture.Unit[0].EnvMode) {
1168 case GL_REPLACE:
1169 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1170 switch(format)
1171 {
1172 case GL_LUMINANCE:
1173 case GL_RGB:
1174 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Decal;
1175 break;
1176
1177 case GL_LUMINANCE_ALPHA:
1178 case GL_RGBA:
1179 case GL_INTENSITY:
1180 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Copy;
1181 break;
1182
1183 case GL_ALPHA:
1184 imesa->regs.s4.texBlendCtrl[0].ui = TBC_CopyAlpha;
1185 break;
1186 }
1187 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1188 &imesa->regs.s4.texBlendCtrl[0]);
1189 break;
1190
1191 case GL_DECAL:
1192 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1193 switch (format)
1194 {
1195 case GL_RGB:
1196 case GL_LUMINANCE:
1197 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Decal;
1198 break;
1199
1200 case GL_RGBA:
1201 case GL_INTENSITY:
1202 case GL_LUMINANCE_ALPHA:
1203 imesa->regs.s4.texBlendCtrl[0].ui = TBC_DecalAlpha;
1204 break;
1205
1206 /*
1207 GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_INTENSITY
1208 are undefined with GL_DECAL
1209 */
1210
1211 case GL_ALPHA:
1212 imesa->regs.s4.texBlendCtrl[0].ui = TBC_CopyAlpha;
1213 break;
1214 }
1215 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1216 &imesa->regs.s4.texBlendCtrl[0]);
1217 break;
1218
1219 case GL_MODULATE:
1220 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1221 imesa->regs.s4.texBlendCtrl[0].ui = TBC_ModulAlpha;
1222 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1223 &imesa->regs.s4.texBlendCtrl[0]);
1224 break;
1225
1226 case GL_BLEND:
1227 imesa->regs.s4.texBlendColor.ui = imesa->texEnvColor;
1228
1229 switch (format)
1230 {
1231 case GL_ALPHA:
1232 imesa->regs.s4.texBlendCtrl[0].ui = TBC_ModulAlpha;
1233 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1234 break;
1235
1236 case GL_LUMINANCE:
1237 case GL_RGB:
1238 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Blend0;
1239 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1240 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1241 imesa->regs.s4.texDescr.ni.tex1Width =
1242 imesa->regs.s4.texDescr.ni.tex0Width;
1243 imesa->regs.s4.texDescr.ni.tex1Height =
1244 imesa->regs.s4.texDescr.ni.tex0Height;
1245 imesa->regs.s4.texDescr.ni.tex1Fmt =
1246 imesa->regs.s4.texDescr.ni.tex0Fmt;
1247
1248 imesa->regs.s4.texAddr[1].ui = imesa->regs.s4.texAddr[0].ui;
1249 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Blend1;
1250
1251 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_TRUE;
1252 imesa->bTexEn1 = GL_TRUE;
1253 break;
1254
1255 case GL_LUMINANCE_ALPHA:
1256 case GL_RGBA:
1257 imesa->regs.s4.texBlendCtrl[0].ui = TBC_BlendAlpha0;
1258 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1259 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1260 imesa->regs.s4.texDescr.ni.tex1Width =
1261 imesa->regs.s4.texDescr.ni.tex0Width;
1262 imesa->regs.s4.texDescr.ni.tex1Height =
1263 imesa->regs.s4.texDescr.ni.tex0Height;
1264 imesa->regs.s4.texDescr.ni.tex1Fmt =
1265 imesa->regs.s4.texDescr.ni.tex0Fmt;
1266
1267 imesa->regs.s4.texAddr[1].ui = imesa->regs.s4.texAddr[0].ui;
1268 imesa->regs.s4.texBlendCtrl[1].ui = TBC_BlendAlpha1;
1269
1270 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_TRUE;
1271 imesa->bTexEn1 = GL_TRUE;
1272 break;
1273
1274 case GL_INTENSITY:
1275 imesa->regs.s4.texBlendCtrl[0].ui = TBC_BlendInt0;
1276 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1277 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1278 imesa->regs.s4.texDescr.ni.tex1Width =
1279 imesa->regs.s4.texDescr.ni.tex0Width;
1280 imesa->regs.s4.texDescr.ni.tex1Height =
1281 imesa->regs.s4.texDescr.ni.tex0Height;
1282 imesa->regs.s4.texDescr.ni.tex1Fmt =
1283 imesa->regs.s4.texDescr.ni.tex0Fmt;
1284
1285 imesa->regs.s4.texAddr[1].ui = imesa->regs.s4.texAddr[0].ui;
1286 imesa->regs.s4.texBlendCtrl[1].ui = TBC_BlendInt1;
1287
1288 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_TRUE;
1289 imesa->regs.s4.texCtrl[0].ni.alphaArg1Invert = GL_TRUE;
1290 imesa->bTexEn1 = GL_TRUE;
1291 break;
1292 }
1293 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1294 &imesa->regs.s4.texBlendCtrl[0]);
1295 break;
1296
1297 case GL_ADD:
1298 imesa->regs.s4.texCtrl[0].ni.clrArg1Invert = GL_FALSE;
1299 switch (format)
1300 {
1301 case GL_ALPHA:
1302 imesa->regs.s4.texBlendCtrl[0].ui = TBC_ModulAlpha;
1303 break;
1304
1305 case GL_LUMINANCE:
1306 case GL_RGB:
1307 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Add;
1308 break;
1309
1310 case GL_LUMINANCE_ALPHA:
1311 case GL_RGBA:
1312 imesa->regs.s4.texBlendCtrl[0].ui = TBC_Add;
1313 break;
1314
1315 case GL_INTENSITY:
1316 imesa->regs.s4.texBlendCtrl[0].ui = TBC_AddAlpha;
1317 break;
1318 }
1319 __HWEnvCombineSingleUnitScale(imesa, 0, 0,
1320 &imesa->regs.s4.texBlendCtrl[0]);
1321 break;
1322
1323 #if GL_ARB_texture_env_combine
1324 case GL_COMBINE_ARB:
1325 __HWParseTexEnvCombine(imesa, 0, &imesa->regs.s4.texCtrl[0],
1326 &imesa->regs.s4.texBlendCtrl[0]);
1327 break;
1328 #endif
1329
1330 default:
1331 fprintf(stderr, "unknown tex env mode");
1332 exit(1);
1333 break;
1334 }
1335
1336 savage4_set_wrap_mode( imesa, 0, t->setup.sWrapMode, t->setup.tWrapMode );
1337 savage4_set_filter_mode( imesa, 0, t->setup.minFilter, t->setup.magFilter );
1338
1339 if((ctx->Texture.Unit[0].LodBias !=0.0F) ||
1340 (imesa->regs.s4.texCtrl[0].ni.dBias != 0))
1341 {
1342 int bias = (int)(ctx->Texture.Unit[0].LodBias * 32.0) +
1343 SAVAGE4_LOD_OFFSET;
1344 if (bias < -256)
1345 bias = -256;
1346 else if (bias > 255)
1347 bias = 255;
1348 imesa->regs.s4.texCtrl[0].ni.dBias = bias & 0x1ff;
1349 }
1350
1351 image = tObj->Image[0][tObj->BaseLevel];
1352 imesa->regs.s4.texDescr.ni.tex0En = GL_TRUE;
1353 imesa->regs.s4.texDescr.ni.tex0Width = image->WidthLog2;
1354 imesa->regs.s4.texDescr.ni.tex0Height = image->HeightLog2;
1355 imesa->regs.s4.texDescr.ni.tex0Fmt = t->hwFormat;
1356 imesa->regs.s4.texCtrl[0].ni.dMax = t->base.lastLevel - t->base.firstLevel;
1357
1358 if (imesa->regs.s4.texDescr.ni.tex1En)
1359 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1360
1361 imesa->regs.s4.texAddr[0].ui = (u_int32_t) t->setup.physAddr | 0x2;
1362 if(t->base.heap->heapId == SAVAGE_AGP_HEAP)
1363 imesa->regs.s4.texAddr[0].ui |= 0x1;
1364
1365 return;
1366 }
1367 static void savageUpdateTex1State_s4( GLcontext *ctx )
1368 {
1369 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1370 struct gl_texture_object *tObj;
1371 struct gl_texture_image *image;
1372 savageTexObjPtr t;
1373 GLuint format;
1374
1375 /* disable */
1376 if(imesa->bTexEn1)
1377 {
1378 imesa->bTexEn1 = GL_FALSE;
1379 return;
1380 }
1381
1382 imesa->regs.s4.texDescr.ni.tex1En = GL_FALSE;
1383 imesa->regs.s4.texBlendCtrl[1].ui = TBC_NoTexMap1;
1384 imesa->regs.s4.texCtrl[1].ui = 0x20f040;
1385 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_FALSE;
1386 if (ctx->Texture.Unit[1]._ReallyEnabled == 0)
1387 return;
1388
1389 tObj = ctx->Texture.Unit[1]._Current;
1390
1391 if ((ctx->Texture.Unit[1]._ReallyEnabled & ~(TEXTURE_1D_BIT|TEXTURE_2D_BIT))
1392 || tObj->Image[0][tObj->BaseLevel]->Border > 0) {
1393 /* 3D texturing enabled, or texture border - fallback */
1394 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1395 return;
1396 }
1397
1398 /* Do 2D texture setup */
1399
1400 t = tObj->DriverData;
1401 if (!t) {
1402 t = savageAllocTexObj( tObj );
1403 if (!t)
1404 return;
1405 }
1406
1407 imesa->CurrentTexObj[1] = &t->base;
1408
1409 t->base.bound |= 2;
1410
1411 if (t->base.dirty_images[0] || t->dirtySubImages) {
1412 savageSetTexImages(imesa, tObj);
1413 savageUploadTexImages(imesa, t);
1414 }
1415
1416 driUpdateTextureLRU( &t->base );
1417
1418 format = tObj->Image[0][tObj->BaseLevel]->_BaseFormat;
1419
1420 switch (ctx->Texture.Unit[1].EnvMode) {
1421 case GL_REPLACE:
1422 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1423 switch (format)
1424 {
1425 case GL_LUMINANCE:
1426 case GL_RGB:
1427 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Decal;
1428 break;
1429
1430 case GL_LUMINANCE_ALPHA:
1431 case GL_INTENSITY:
1432 case GL_RGBA:
1433 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Copy;
1434 break;
1435
1436 case GL_ALPHA:
1437 imesa->regs.s4.texBlendCtrl[1].ui = TBC_CopyAlpha1;
1438 break;
1439 }
1440 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1441 break;
1442 case GL_MODULATE:
1443 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1444 imesa->regs.s4.texBlendCtrl[1].ui = TBC_ModulAlpha1;
1445 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1446 break;
1447
1448 case GL_ADD:
1449 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1450 switch (format)
1451 {
1452 case GL_ALPHA:
1453 imesa->regs.s4.texBlendCtrl[1].ui = TBC_ModulAlpha1;
1454 break;
1455
1456 case GL_LUMINANCE:
1457 case GL_RGB:
1458 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Add1;
1459 break;
1460
1461 case GL_LUMINANCE_ALPHA:
1462 case GL_RGBA:
1463 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Add1;
1464 break;
1465
1466 case GL_INTENSITY:
1467 imesa->regs.s4.texBlendCtrl[1].ui = TBC_AddAlpha1;
1468 break;
1469 }
1470 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1471 break;
1472
1473 #if GL_ARB_texture_env_combine
1474 case GL_COMBINE_ARB:
1475 __HWParseTexEnvCombine(imesa, 1, &texCtrl, &imesa->regs.s4.texBlendCtrl);
1476 break;
1477 #endif
1478
1479 case GL_DECAL:
1480 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_FALSE;
1481
1482 switch (format)
1483 {
1484 case GL_LUMINANCE:
1485 case GL_RGB:
1486 imesa->regs.s4.texBlendCtrl[1].ui = TBC_Decal1;
1487 break;
1488 case GL_LUMINANCE_ALPHA:
1489 case GL_INTENSITY:
1490 case GL_RGBA:
1491 imesa->regs.s4.texBlendCtrl[1].ui = TBC_DecalAlpha1;
1492 break;
1493
1494 /*
1495 // GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_INTENSITY
1496 // are undefined with GL_DECAL
1497 */
1498 case GL_ALPHA:
1499 imesa->regs.s4.texBlendCtrl[1].ui = TBC_CopyAlpha1;
1500 break;
1501 }
1502 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1503 break;
1504
1505 case GL_BLEND:
1506 if (format == GL_LUMINANCE)
1507 {
1508 /*
1509 // This is a hack for GLQuake, invert.
1510 */
1511 imesa->regs.s4.texCtrl[1].ni.clrArg1Invert = GL_TRUE;
1512 imesa->regs.s4.texBlendCtrl[1].ui = 0;
1513 }
1514 __HWEnvCombineSingleUnitScale(imesa, 0, 1, &imesa->regs.s4.texBlendCtrl);
1515 break;
1516
1517 default:
1518 fprintf(stderr, "unknown tex 1 env mode\n");
1519 exit(1);
1520 break;
1521 }
1522
1523 savage4_set_wrap_mode( imesa, 1, t->setup.sWrapMode, t->setup.tWrapMode );
1524 savage4_set_filter_mode( imesa, 1, t->setup.minFilter, t->setup.magFilter );
1525
1526 if((ctx->Texture.Unit[1].LodBias !=0.0F) ||
1527 (imesa->regs.s4.texCtrl[1].ni.dBias != 0))
1528 {
1529 int bias = (int)(ctx->Texture.Unit[1].LodBias * 32.0) +
1530 SAVAGE4_LOD_OFFSET;
1531 if (bias < -256)
1532 bias = -256;
1533 else if (bias > 255)
1534 bias = 255;
1535 imesa->regs.s4.texCtrl[1].ni.dBias = bias & 0x1ff;
1536 }
1537
1538 image = tObj->Image[0][tObj->BaseLevel];
1539 imesa->regs.s4.texDescr.ni.tex1En = GL_TRUE;
1540 imesa->regs.s4.texDescr.ni.tex1Width = image->WidthLog2;
1541 imesa->regs.s4.texDescr.ni.tex1Height = image->HeightLog2;
1542 imesa->regs.s4.texDescr.ni.tex1Fmt = t->hwFormat;
1543 imesa->regs.s4.texCtrl[1].ni.dMax = t->base.lastLevel - t->base.firstLevel;
1544 imesa->regs.s4.texDescr.ni.texBLoopEn = GL_TRUE;
1545
1546 imesa->regs.s4.texAddr[1].ui = (u_int32_t) t->setup.physAddr | 2;
1547 if(t->base.heap->heapId == SAVAGE_AGP_HEAP)
1548 imesa->regs.s4.texAddr[1].ui |= 0x1;
1549 }
1550 static void savageUpdateTexState_s3d( GLcontext *ctx )
1551 {
1552 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1553 struct gl_texture_object *tObj;
1554 struct gl_texture_image *image;
1555 savageTexObjPtr t;
1556 GLuint format;
1557
1558 /* disable */
1559 imesa->regs.s3d.texCtrl.ui = 0;
1560 imesa->regs.s3d.texCtrl.ni.texEn = GL_FALSE;
1561 imesa->regs.s3d.texCtrl.ni.dBias = 0x08;
1562 imesa->regs.s3d.texCtrl.ni.texXprEn = GL_TRUE;
1563 if (ctx->Texture.Unit[0]._ReallyEnabled == 0)
1564 return;
1565
1566 tObj = ctx->Texture.Unit[0]._Current;
1567 if ((ctx->Texture.Unit[0]._ReallyEnabled & ~(TEXTURE_1D_BIT|TEXTURE_2D_BIT))
1568 || tObj->Image[0][tObj->BaseLevel]->Border > 0) {
1569 /* 3D texturing enabled, or texture border - fallback */
1570 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1571 return;
1572 }
1573
1574 /* Do 2D texture setup */
1575 t = tObj->DriverData;
1576 if (!t) {
1577 t = savageAllocTexObj( tObj );
1578 if (!t)
1579 return;
1580 }
1581
1582 imesa->CurrentTexObj[0] = &t->base;
1583 t->base.bound |= 1;
1584
1585 if (t->base.dirty_images[0] || t->dirtySubImages) {
1586 savageSetTexImages(imesa, tObj);
1587 savageUploadTexImages(imesa, t);
1588 }
1589
1590 driUpdateTextureLRU( &t->base );
1591
1592 format = tObj->Image[0][tObj->BaseLevel]->_BaseFormat;
1593
1594 /* FIXME: copied from utah-glx, probably needs some tuning */
1595 switch (ctx->Texture.Unit[0].EnvMode) {
1596 case GL_DECAL:
1597 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_DECALALPHA_S3D;
1598 break;
1599 case GL_REPLACE:
1600 switch (format) {
1601 case GL_ALPHA: /* FIXME */
1602 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = 1;
1603 break;
1604 case GL_LUMINANCE_ALPHA:
1605 case GL_RGBA:
1606 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = 4;
1607 break;
1608 case GL_RGB:
1609 case GL_LUMINANCE:
1610 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_DECAL_S3D;
1611 break;
1612 case GL_INTENSITY:
1613 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_COPY_S3D;
1614 }
1615 break;
1616 case GL_BLEND: /* hardware can't do GL_BLEND */
1617 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1618 return;
1619 case GL_MODULATE:
1620 imesa->regs.s3d.drawCtrl.ni.texBlendCtrl = SAVAGETBC_MODULATEALPHA_S3D;
1621 break;
1622 default:
1623 fprintf(stderr, "unknown tex env mode\n");
1624 /*exit(1);*/
1625 break;
1626 }
1627
1628 /* The Savage3D can't handle different wrapping modes in s and t.
1629 * If they are not the same, fall back to software. */
1630 if (t->setup.sWrapMode != t->setup.tWrapMode) {
1631 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_TRUE);
1632 return;
1633 }
1634 imesa->regs.s3d.texCtrl.ni.uWrapEn = 0;
1635 imesa->regs.s3d.texCtrl.ni.vWrapEn = 0;
1636 imesa->regs.s3d.texCtrl.ni.wrapMode =
1637 (t->setup.sWrapMode == GL_REPEAT) ? TAM_Wrap : TAM_Clamp;
1638
1639 switch (t->setup.minFilter) {
1640 case GL_NEAREST:
1641 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Point;
1642 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_TRUE;
1643 break;
1644
1645 case GL_LINEAR:
1646 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Bilin;
1647 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_TRUE;
1648 break;
1649
1650 case GL_NEAREST_MIPMAP_NEAREST:
1651 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Point;
1652 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_FALSE;
1653 break;
1654
1655 case GL_LINEAR_MIPMAP_NEAREST:
1656 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Bilin;
1657 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_FALSE;
1658 break;
1659
1660 case GL_NEAREST_MIPMAP_LINEAR:
1661 case GL_LINEAR_MIPMAP_LINEAR:
1662 imesa->regs.s3d.texCtrl.ni.filterMode = TFM_Trilin;
1663 imesa->regs.s3d.texCtrl.ni.mipmapDisable = GL_FALSE;
1664 break;
1665 }
1666
1667 /* There is no way to specify a maximum mipmap level. We may have to
1668 disable mipmapping completely. */
1669 /*
1670 if (t->max_level < t->image[0].image->WidthLog2 ||
1671 t->max_level < t->image[0].image->HeightLog2) {
1672 texCtrl.ni.mipmapEnable = GL_TRUE;
1673 if (texCtrl.ni.filterMode == TFM_Trilin)
1674 texCtrl.ni.filterMode = TFM_Bilin;
1675 texCtrl.ni.filterMode = TFM_Point;
1676 }
1677 */
1678
1679 if((ctx->Texture.Unit[0].LodBias !=0.0F) ||
1680 (imesa->regs.s3d.texCtrl.ni.dBias != 0))
1681 {
1682 int bias = (int)(ctx->Texture.Unit[0].LodBias * 16.0);
1683 if (bias < -256)
1684 bias = -256;
1685 else if (bias > 255)
1686 bias = 255;
1687 imesa->regs.s3d.texCtrl.ni.dBias = bias & 0x1ff;
1688 }
1689
1690 image = tObj->Image[0][tObj->BaseLevel];
1691 imesa->regs.s3d.texCtrl.ni.texEn = GL_TRUE;
1692 imesa->regs.s3d.texDescr.ni.texWidth = image->WidthLog2;
1693 imesa->regs.s3d.texDescr.ni.texHeight = image->HeightLog2;
1694 assert (t->hwFormat <= 7);
1695 imesa->regs.s3d.texDescr.ni.texFmt = t->hwFormat;
1696
1697 imesa->regs.s3d.texAddr.ui = (u_int32_t) t->setup.physAddr | 2;
1698 if(t->base.heap->heapId == SAVAGE_AGP_HEAP)
1699 imesa->regs.s3d.texAddr.ui |= 0x1;
1700 }
1701
1702
1703 static void savageTimestampTextures( savageContextPtr imesa )
1704 {
1705 /* Timestamp current texture objects for texture heap aging.
1706 * Only useful with long-lived 32-bit event tags available
1707 * with Savage DRM 2.3.x or later. */
1708 if ((imesa->CurrentTexObj[0] || imesa->CurrentTexObj[1]) &&
1709 imesa->savageScreen->driScrnPriv->drmMinor >= 3) {
1710 unsigned int e;
1711 FLUSH_BATCH(imesa);
1712 e = savageEmitEvent(imesa, SAVAGE_WAIT_3D);
1713 if (imesa->CurrentTexObj[0])
1714 imesa->CurrentTexObj[0]->timestamp = e;
1715 if (imesa->CurrentTexObj[1])
1716 imesa->CurrentTexObj[1]->timestamp = e;
1717 }
1718 }
1719
1720
1721 static void savageUpdateTextureState_s4( GLcontext *ctx )
1722 {
1723 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1724
1725 /* When a texture is about to change or be disabled, timestamp the
1726 * old texture(s). We'll have to wait for this time stamp before
1727 * uploading anything to the same texture heap.
1728 */
1729 if ((imesa->CurrentTexObj[0] && ctx->Texture.Unit[0]._ReallyEnabled &&
1730 ctx->Texture.Unit[0]._Current->DriverData != imesa->CurrentTexObj[0]) ||
1731 (imesa->CurrentTexObj[1] && ctx->Texture.Unit[1]._ReallyEnabled &&
1732 ctx->Texture.Unit[1]._Current->DriverData != imesa->CurrentTexObj[1]) ||
1733 (imesa->CurrentTexObj[0] && !ctx->Texture.Unit[0]._ReallyEnabled) ||
1734 (imesa->CurrentTexObj[1] && !ctx->Texture.Unit[1]._ReallyEnabled))
1735 savageTimestampTextures(imesa);
1736
1737 if (imesa->CurrentTexObj[0]) imesa->CurrentTexObj[0]->bound &= ~1;
1738 if (imesa->CurrentTexObj[1]) imesa->CurrentTexObj[1]->bound &= ~2;
1739 imesa->CurrentTexObj[0] = 0;
1740 imesa->CurrentTexObj[1] = 0;
1741 savageUpdateTex0State_s4( ctx );
1742 savageUpdateTex1State_s4( ctx );
1743 imesa->dirty |= (SAVAGE_UPLOAD_TEX0 |
1744 SAVAGE_UPLOAD_TEX1);
1745 }
1746 static void savageUpdateTextureState_s3d( GLcontext *ctx )
1747 {
1748 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
1749
1750 /* When a texture is about to change or be disabled, timestamp the
1751 * old texture(s). We'll have to wait for this time stamp before
1752 * uploading anything to the same texture heap.
1753 */
1754 if ((imesa->CurrentTexObj[0] && ctx->Texture.Unit[0]._ReallyEnabled &&
1755 ctx->Texture.Unit[0]._Current->DriverData != imesa->CurrentTexObj[0]) ||
1756 (imesa->CurrentTexObj[0] && !ctx->Texture.Unit[0]._ReallyEnabled))
1757 savageTimestampTextures(imesa);
1758
1759 if (imesa->CurrentTexObj[0]) imesa->CurrentTexObj[0]->bound &= ~1;
1760 imesa->CurrentTexObj[0] = 0;
1761 savageUpdateTexState_s3d( ctx );
1762 imesa->dirty |= (SAVAGE_UPLOAD_TEX0);
1763 }
1764 void savageUpdateTextureState( GLcontext *ctx)
1765 {
1766 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
1767 FALLBACK (ctx, SAVAGE_FALLBACK_TEXTURE, GL_FALSE);
1768 FALLBACK(ctx, SAVAGE_FALLBACK_PROJ_TEXTURE, GL_FALSE);
1769 if (imesa->savageScreen->chipset >= S3_SAVAGE4)
1770 savageUpdateTextureState_s4 (ctx);
1771 else
1772 savageUpdateTextureState_s3d (ctx);
1773 }
1774
1775
1776
1777 /*****************************************
1778 * DRIVER functions
1779 *****************************************/
1780
1781 static void savageTexEnv( GLcontext *ctx, GLenum target,
1782 GLenum pname, const GLfloat *param )
1783 {
1784 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
1785
1786 if (pname == GL_TEXTURE_ENV_MODE) {
1787
1788 imesa->new_state |= SAVAGE_NEW_TEXTURE;
1789
1790 } else if (pname == GL_TEXTURE_ENV_COLOR) {
1791
1792 struct gl_texture_unit *texUnit =
1793 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1794 const GLfloat *fc = texUnit->EnvColor;
1795 GLuint r, g, b, a;
1796 CLAMPED_FLOAT_TO_UBYTE(r, fc[0]);
1797 CLAMPED_FLOAT_TO_UBYTE(g, fc[1]);
1798 CLAMPED_FLOAT_TO_UBYTE(b, fc[2]);
1799 CLAMPED_FLOAT_TO_UBYTE(a, fc[3]);
1800
1801 imesa->texEnvColor = ((a << 24) | (r << 16) |
1802 (g << 8) | (b << 0));
1803
1804
1805 }
1806 }
1807
1808 /* Update the heap's time stamp, so the new image is not uploaded
1809 * while the old one is still in use. If the texture that is going to
1810 * be changed is currently bound, we need to timestamp the texture
1811 * first. */
1812 static void savageTexImageChanged (savageTexObjPtr t) {
1813 if (t->base.heap) {
1814 if (t->base.bound)
1815 savageTimestampTextures(
1816 (savageContextPtr)t->base.heap->driverContext);
1817 if (t->base.timestamp > t->base.heap->timestamp)
1818 t->base.heap->timestamp = t->base.timestamp;
1819 }
1820 }
1821
1822 static void savageTexImage1D( GLcontext *ctx, GLenum target, GLint level,
1823 GLint internalFormat,
1824 GLint width, GLint border,
1825 GLenum format, GLenum type, const GLvoid *pixels,
1826 const struct gl_pixelstore_attrib *packing,
1827 struct gl_texture_object *texObj,
1828 struct gl_texture_image *texImage )
1829 {
1830 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1831 if (t) {
1832 savageTexImageChanged (t);
1833 } else {
1834 t = savageAllocTexObj(texObj);
1835 if (!t) {
1836 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
1837 return;
1838 }
1839 }
1840 _mesa_store_teximage1d( ctx, target, level, internalFormat,
1841 width, border, format, type,
1842 pixels, packing, texObj, texImage );
1843 t->base.dirty_images[0] |= (1 << level);
1844 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1845 }
1846
1847 static void savageTexSubImage1D( GLcontext *ctx,
1848 GLenum target,
1849 GLint level,
1850 GLint xoffset,
1851 GLsizei width,
1852 GLenum format, GLenum type,
1853 const GLvoid *pixels,
1854 const struct gl_pixelstore_attrib *packing,
1855 struct gl_texture_object *texObj,
1856 struct gl_texture_image *texImage )
1857 {
1858 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1859 assert( t ); /* this _should_ be true */
1860 if (t) {
1861 savageTexImageChanged (t);
1862 savageMarkDirtyTiles(t, level, texImage->Width2, 1,
1863 xoffset, 0, width, 1);
1864 } else {
1865 t = savageAllocTexObj(texObj);
1866 if (!t) {
1867 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage1D");
1868 return;
1869 }
1870 t->base.dirty_images[0] |= (1 << level);
1871 }
1872 _mesa_store_texsubimage1d(ctx, target, level, xoffset, width,
1873 format, type, pixels, packing, texObj,
1874 texImage);
1875 t->dirtySubImages |= (1 << level);
1876 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1877 }
1878
1879 static void savageTexImage2D( GLcontext *ctx, GLenum target, GLint level,
1880 GLint internalFormat,
1881 GLint width, GLint height, GLint border,
1882 GLenum format, GLenum type, const GLvoid *pixels,
1883 const struct gl_pixelstore_attrib *packing,
1884 struct gl_texture_object *texObj,
1885 struct gl_texture_image *texImage )
1886 {
1887 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1888 if (t) {
1889 savageTexImageChanged (t);
1890 } else {
1891 t = savageAllocTexObj(texObj);
1892 if (!t) {
1893 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
1894 return;
1895 }
1896 }
1897 _mesa_store_teximage2d( ctx, target, level, internalFormat,
1898 width, height, border, format, type,
1899 pixels, packing, texObj, texImage );
1900 t->base.dirty_images[0] |= (1 << level);
1901 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1902 }
1903
1904 static void savageTexSubImage2D( GLcontext *ctx,
1905 GLenum target,
1906 GLint level,
1907 GLint xoffset, GLint yoffset,
1908 GLsizei width, GLsizei height,
1909 GLenum format, GLenum type,
1910 const GLvoid *pixels,
1911 const struct gl_pixelstore_attrib *packing,
1912 struct gl_texture_object *texObj,
1913 struct gl_texture_image *texImage )
1914 {
1915 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1916 assert( t ); /* this _should_ be true */
1917 if (t) {
1918 savageTexImageChanged (t);
1919 savageMarkDirtyTiles(t, level, texImage->Width2, texImage->Height2,
1920 xoffset, yoffset, width, height);
1921 } else {
1922 t = savageAllocTexObj(texObj);
1923 if (!t) {
1924 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
1925 return;
1926 }
1927 t->base.dirty_images[0] |= (1 << level);
1928 }
1929 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
1930 height, format, type, pixels, packing, texObj,
1931 texImage);
1932 t->dirtySubImages |= (1 << level);
1933 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1934 }
1935
1936 static void
1937 savageCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
1938 GLint internalFormat,
1939 GLint width, GLint height, GLint border,
1940 GLsizei imageSize, const GLvoid *data,
1941 struct gl_texture_object *texObj,
1942 struct gl_texture_image *texImage )
1943 {
1944 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1945 if (t) {
1946 savageTexImageChanged (t);
1947 } else {
1948 t = savageAllocTexObj(texObj);
1949 if (!t) {
1950 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
1951 return;
1952 }
1953 }
1954 _mesa_store_compressed_teximage2d( ctx, target, level, internalFormat,
1955 width, height, border, imageSize,
1956 data, texObj, texImage );
1957 t->base.dirty_images[0] |= (1 << level);
1958 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1959 }
1960
1961 static void
1962 savageCompressedTexSubImage2D( GLcontext *ctx,
1963 GLenum target,
1964 GLint level,
1965 GLint xoffset, GLint yoffset,
1966 GLsizei width, GLsizei height,
1967 GLenum format, GLsizei imageSize,
1968 const GLvoid *data,
1969 struct gl_texture_object *texObj,
1970 struct gl_texture_image *texImage )
1971 {
1972 savageTexObjPtr t = (savageTexObjPtr) texObj->DriverData;
1973 assert( t ); /* this _should_ be true */
1974 if (t) {
1975 savageTexImageChanged (t);
1976 savageMarkDirtyTiles(t, level, texImage->Width2, texImage->Height2,
1977 xoffset, yoffset, width, height);
1978 } else {
1979 t = savageAllocTexObj(texObj);
1980 if (!t) {
1981 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
1982 return;
1983 }
1984 t->base.dirty_images[0] |= (1 << level);
1985 }
1986 _mesa_store_compressed_texsubimage2d(ctx, target, level, xoffset, yoffset,
1987 width, height, format, imageSize,
1988 data, texObj, texImage);
1989 t->dirtySubImages |= (1 << level);
1990 SAVAGE_CONTEXT(ctx)->new_state |= SAVAGE_NEW_TEXTURE;
1991 }
1992
1993 static void savageTexParameter( GLcontext *ctx, GLenum target,
1994 struct gl_texture_object *tObj,
1995 GLenum pname, const GLfloat *params )
1996 {
1997 savageTexObjPtr t = (savageTexObjPtr) tObj->DriverData;
1998 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
1999
2000 if (!t || (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D))
2001 return;
2002
2003 switch (pname) {
2004 case GL_TEXTURE_MIN_FILTER:
2005 case GL_TEXTURE_MAG_FILTER:
2006 savageSetTexFilter(t,tObj->MinFilter,tObj->MagFilter);
2007 break;
2008
2009 case GL_TEXTURE_WRAP_S:
2010 case GL_TEXTURE_WRAP_T:
2011 savageSetTexWrapping(t,tObj->WrapS,tObj->WrapT);
2012 break;
2013
2014 case GL_TEXTURE_BORDER_COLOR:
2015 savageSetTexBorderColor(t,tObj->_BorderChan);
2016 break;
2017
2018 default:
2019 return;
2020 }
2021
2022 imesa->new_state |= SAVAGE_NEW_TEXTURE;
2023 }
2024
2025 static void savageBindTexture( GLcontext *ctx, GLenum target,
2026 struct gl_texture_object *tObj )
2027 {
2028 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
2029
2030 assert( (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D) ||
2031 (tObj->DriverData != NULL) );
2032
2033 imesa->new_state |= SAVAGE_NEW_TEXTURE;
2034 }
2035
2036 static void savageDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
2037 {
2038 driTextureObject *t = (driTextureObject *)tObj->DriverData;
2039 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
2040
2041 if (t) {
2042 if (t->bound)
2043 savageTimestampTextures(imesa);
2044
2045 driDestroyTextureObject(t);
2046 }
2047 /* Free mipmap images and the texture object itself */
2048 _mesa_delete_texture_object(ctx, tObj);
2049 }
2050
2051
2052 static struct gl_texture_object *
2053 savageNewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
2054 {
2055 struct gl_texture_object *obj;
2056 obj = _mesa_new_texture_object(ctx, name, target);
2057 savageAllocTexObj( obj );
2058
2059 return obj;
2060 }
2061
2062 void savageDDInitTextureFuncs( struct dd_function_table *functions )
2063 {
2064 functions->TexEnv = savageTexEnv;
2065 functions->ChooseTextureFormat = savageChooseTextureFormat;
2066 functions->TexImage1D = savageTexImage1D;
2067 functions->TexSubImage1D = savageTexSubImage1D;
2068 functions->TexImage2D = savageTexImage2D;
2069 functions->TexSubImage2D = savageTexSubImage2D;
2070 functions->CompressedTexImage2D = savageCompressedTexImage2D;
2071 functions->CompressedTexSubImage2D = savageCompressedTexSubImage2D;
2072 functions->BindTexture = savageBindTexture;
2073 functions->NewTextureObject = savageNewTextureObject;
2074 functions->DeleteTexture = savageDeleteTexture;
2075 functions->IsTextureResident = driIsTextureResident;
2076 functions->TexParameter = savageTexParameter;
2077
2078 /* Texel fetching with our custom texture formats works just like
2079 * the standard argb formats. */
2080 _savage_texformat_a1114444.FetchTexel1D = _mesa_texformat_argb4444.FetchTexel1D;
2081 _savage_texformat_a1114444.FetchTexel2D = _mesa_texformat_argb4444.FetchTexel2D;
2082 _savage_texformat_a1114444.FetchTexel3D = _mesa_texformat_argb4444.FetchTexel3D;
2083 _savage_texformat_a1114444.FetchTexel1Df= _mesa_texformat_argb4444.FetchTexel1Df;
2084 _savage_texformat_a1114444.FetchTexel2Df= _mesa_texformat_argb4444.FetchTexel2Df;
2085 _savage_texformat_a1114444.FetchTexel3Df= _mesa_texformat_argb4444.FetchTexel3Df;
2086
2087 _savage_texformat_a1118888.FetchTexel1D = _mesa_texformat_argb8888.FetchTexel1D;
2088 _savage_texformat_a1118888.FetchTexel2D = _mesa_texformat_argb8888.FetchTexel2D;
2089 _savage_texformat_a1118888.FetchTexel3D = _mesa_texformat_argb8888.FetchTexel3D;
2090 _savage_texformat_a1118888.FetchTexel1Df= _mesa_texformat_argb8888.FetchTexel1Df;
2091 _savage_texformat_a1118888.FetchTexel2Df= _mesa_texformat_argb8888.FetchTexel2Df;
2092 _savage_texformat_a1118888.FetchTexel3Df= _mesa_texformat_argb8888.FetchTexel3Df;
2093 }