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