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