Merge branch 'r500-support'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_texstate.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /**
31 * \file
32 *
33 * \author Keith Whitwell <keith@tungstengraphics.com>
34 *
35 * \todo Enable R300 texture tiling code?
36 */
37
38 #include "glheader.h"
39 #include "imports.h"
40 #include "context.h"
41 #include "macros.h"
42 #include "texformat.h"
43 #include "teximage.h"
44 #include "texobj.h"
45 #include "enums.h"
46
47 #include "r300_context.h"
48 #include "r300_state.h"
49 #include "r300_ioctl.h"
50 #include "radeon_ioctl.h"
51 #include "r300_tex.h"
52 #include "r300_reg.h"
53
54 #define VALID_FORMAT(f) ( ((f) <= MESA_FORMAT_RGBA_DXT5 \
55 || ((f) >= MESA_FORMAT_RGBA_FLOAT32 && \
56 (f) <= MESA_FORMAT_INTENSITY_FLOAT16)) \
57 && tx_table[f].flag )
58
59 #define _ASSIGN(entry, format) \
60 [ MESA_FORMAT_ ## entry ] = { format, 0, 1}
61
62 /*
63 * Note that the _REV formats are the same as the non-REV formats. This is
64 * because the REV and non-REV formats are identical as a byte string, but
65 * differ when accessed as 16-bit or 32-bit words depending on the endianness of
66 * the host. Since the textures are transferred to the R300 as a byte string
67 * (i.e. without any byte-swapping), the R300 sees the REV and non-REV formats
68 * identically. -- paulus
69 */
70
71 static const struct tx_table {
72 GLuint format, filter, flag;
73 } tx_table[] = {
74 /* *INDENT-OFF* */
75 #ifdef MESA_LITTLE_ENDIAN
76 _ASSIGN(RGBA8888, R300_EASY_TX_FORMAT(Y, Z, W, X, W8Z8Y8X8)),
77 _ASSIGN(RGBA8888_REV, R300_EASY_TX_FORMAT(Z, Y, X, W, W8Z8Y8X8)),
78 _ASSIGN(ARGB8888, R300_EASY_TX_FORMAT(X, Y, Z, W, W8Z8Y8X8)),
79 _ASSIGN(ARGB8888_REV, R300_EASY_TX_FORMAT(W, Z, Y, X, W8Z8Y8X8)),
80 #else
81 _ASSIGN(RGBA8888, R300_EASY_TX_FORMAT(Z, Y, X, W, W8Z8Y8X8)),
82 _ASSIGN(RGBA8888_REV, R300_EASY_TX_FORMAT(Y, Z, W, X, W8Z8Y8X8)),
83 _ASSIGN(ARGB8888, R300_EASY_TX_FORMAT(W, Z, Y, X, W8Z8Y8X8)),
84 _ASSIGN(ARGB8888_REV, R300_EASY_TX_FORMAT(X, Y, Z, W, W8Z8Y8X8)),
85 #endif
86 _ASSIGN(RGB888, R300_EASY_TX_FORMAT(X, Y, Z, ONE, W8Z8Y8X8)),
87 _ASSIGN(RGB565, R300_EASY_TX_FORMAT(X, Y, Z, ONE, Z5Y6X5)),
88 _ASSIGN(RGB565_REV, R300_EASY_TX_FORMAT(X, Y, Z, ONE, Z5Y6X5)),
89 _ASSIGN(ARGB4444, R300_EASY_TX_FORMAT(X, Y, Z, W, W4Z4Y4X4)),
90 _ASSIGN(ARGB4444_REV, R300_EASY_TX_FORMAT(X, Y, Z, W, W4Z4Y4X4)),
91 _ASSIGN(ARGB1555, R300_EASY_TX_FORMAT(X, Y, Z, W, W1Z5Y5X5)),
92 _ASSIGN(ARGB1555_REV, R300_EASY_TX_FORMAT(X, Y, Z, W, W1Z5Y5X5)),
93 _ASSIGN(AL88, R300_EASY_TX_FORMAT(X, X, X, Y, Y8X8)),
94 _ASSIGN(AL88_REV, R300_EASY_TX_FORMAT(X, X, X, Y, Y8X8)),
95 _ASSIGN(RGB332, R300_EASY_TX_FORMAT(X, Y, Z, ONE, Z3Y3X2)),
96 _ASSIGN(A8, R300_EASY_TX_FORMAT(ZERO, ZERO, ZERO, X, X8)),
97 _ASSIGN(L8, R300_EASY_TX_FORMAT(X, X, X, ONE, X8)),
98 _ASSIGN(I8, R300_EASY_TX_FORMAT(X, X, X, X, X8)),
99 _ASSIGN(CI8, R300_EASY_TX_FORMAT(X, X, X, X, X8)),
100 _ASSIGN(YCBCR, R300_EASY_TX_FORMAT(X, Y, Z, ONE, G8R8_G8B8) | R300_TX_FORMAT_YUV_MODE),
101 _ASSIGN(YCBCR_REV, R300_EASY_TX_FORMAT(X, Y, Z, ONE, G8R8_G8B8) | R300_TX_FORMAT_YUV_MODE),
102 _ASSIGN(RGB_DXT1, R300_EASY_TX_FORMAT(X, Y, Z, ONE, DXT1)),
103 _ASSIGN(RGBA_DXT1, R300_EASY_TX_FORMAT(X, Y, Z, W, DXT1)),
104 _ASSIGN(RGBA_DXT3, R300_EASY_TX_FORMAT(X, Y, Z, W, DXT3)),
105 _ASSIGN(RGBA_DXT5, R300_EASY_TX_FORMAT(Y, Z, W, X, DXT5)),
106 _ASSIGN(RGBA_FLOAT32, R300_EASY_TX_FORMAT(Z, Y, X, W, FL_R32G32B32A32)),
107 _ASSIGN(RGBA_FLOAT16, R300_EASY_TX_FORMAT(Z, Y, X, W, FL_R16G16B16A16)),
108 _ASSIGN(RGB_FLOAT32, 0xffffffff),
109 _ASSIGN(RGB_FLOAT16, 0xffffffff),
110 _ASSIGN(ALPHA_FLOAT32, R300_EASY_TX_FORMAT(ZERO, ZERO, ZERO, X, FL_I32)),
111 _ASSIGN(ALPHA_FLOAT16, R300_EASY_TX_FORMAT(ZERO, ZERO, ZERO, X, FL_I16)),
112 _ASSIGN(LUMINANCE_FLOAT32, R300_EASY_TX_FORMAT(X, X, X, ONE, FL_I32)),
113 _ASSIGN(LUMINANCE_FLOAT16, R300_EASY_TX_FORMAT(X, X, X, ONE, FL_I16)),
114 _ASSIGN(LUMINANCE_ALPHA_FLOAT32, R300_EASY_TX_FORMAT(X, X, X, Y, FL_I32A32)),
115 _ASSIGN(LUMINANCE_ALPHA_FLOAT16, R300_EASY_TX_FORMAT(X, X, X, Y, FL_I16A16)),
116 _ASSIGN(INTENSITY_FLOAT32, R300_EASY_TX_FORMAT(X, X, X, X, FL_I32)),
117 _ASSIGN(INTENSITY_FLOAT16, R300_EASY_TX_FORMAT(X, X, X, X, FL_I16)),
118 _ASSIGN(Z16, R300_EASY_TX_FORMAT(X, X, X, X, X16)),
119 _ASSIGN(Z24_S8, R300_EASY_TX_FORMAT(X, X, X, X, X24_Y8)),
120 _ASSIGN(Z32, R300_EASY_TX_FORMAT(X, X, X, X, X32)),
121 /* *INDENT-ON* */
122 };
123
124 #undef _ASSIGN
125
126 void r300SetDepthTexMode(struct gl_texture_object *tObj)
127 {
128 static const GLuint formats[3][3] = {
129 {
130 R300_EASY_TX_FORMAT(X, X, X, X, X16),
131 R300_EASY_TX_FORMAT(X, X, X, ONE, X16),
132 R300_EASY_TX_FORMAT(ZERO, ZERO, ZERO, X, X16),
133 },
134 {
135 R300_EASY_TX_FORMAT(X, X, X, X, X24_Y8),
136 R300_EASY_TX_FORMAT(X, X, X, ONE, X24_Y8),
137 R300_EASY_TX_FORMAT(ZERO, ZERO, ZERO, X, X24_Y8),
138 },
139 {
140 R300_EASY_TX_FORMAT(X, X, X, X, X32),
141 R300_EASY_TX_FORMAT(X, X, X, ONE, X32),
142 R300_EASY_TX_FORMAT(ZERO, ZERO, ZERO, X, X32),
143 },
144 };
145 const GLuint *format;
146 r300TexObjPtr t;
147
148 if (!tObj)
149 return;
150
151 t = (r300TexObjPtr) tObj->DriverData;
152
153
154 switch (tObj->Image[0][tObj->BaseLevel]->TexFormat->MesaFormat) {
155 case MESA_FORMAT_Z16:
156 format = formats[0];
157 break;
158 case MESA_FORMAT_Z24_S8:
159 format = formats[1];
160 break;
161 case MESA_FORMAT_Z32:
162 format = formats[2];
163 break;
164 default:
165 /* Error...which should have already been caught by higher
166 * levels of Mesa.
167 */
168 ASSERT(0);
169 return;
170 }
171
172 switch (tObj->DepthMode) {
173 case GL_LUMINANCE:
174 t->format = format[0];
175 break;
176 case GL_INTENSITY:
177 t->format = format[1];
178 break;
179 case GL_ALPHA:
180 t->format = format[2];
181 break;
182 default:
183 /* Error...which should have already been caught by higher
184 * levels of Mesa.
185 */
186 ASSERT(0);
187 return;
188 }
189 }
190
191
192 /**
193 * This function computes the number of bytes of storage needed for
194 * the given texture object (all mipmap levels, all cube faces).
195 * The \c image[face][level].x/y/width/height parameters for upload/blitting
196 * are computed here. \c filter, \c format, etc. will be set here
197 * too.
198 *
199 * \param rmesa Context pointer
200 * \param tObj GL texture object whose images are to be posted to
201 * hardware state.
202 */
203 static void r300SetTexImages(r300ContextPtr rmesa,
204 struct gl_texture_object *tObj)
205 {
206 r300TexObjPtr t = (r300TexObjPtr) tObj->DriverData;
207 const struct gl_texture_image *baseImage =
208 tObj->Image[0][tObj->BaseLevel];
209 GLint curOffset, blitWidth;
210 GLint i, texelBytes;
211 GLint numLevels;
212 GLint log2Width, log2Height, log2Depth;
213
214 /* Set the hardware texture format
215 */
216 if (!t->image_override
217 && VALID_FORMAT(baseImage->TexFormat->MesaFormat)) {
218 if (baseImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
219 r300SetDepthTexMode(tObj);
220 } else {
221 t->format = tx_table[baseImage->TexFormat->MesaFormat].format;
222 }
223
224 t->filter |= tx_table[baseImage->TexFormat->MesaFormat].filter;
225 } else if (!t->image_override) {
226 _mesa_problem(NULL, "unexpected texture format in %s",
227 __FUNCTION__);
228 return;
229 }
230
231 texelBytes = baseImage->TexFormat->TexelBytes;
232
233 /* Compute which mipmap levels we really want to send to the hardware.
234 */
235 driCalculateTextureFirstLastLevel((driTextureObject *) t);
236 log2Width = tObj->Image[0][t->base.firstLevel]->WidthLog2;
237 log2Height = tObj->Image[0][t->base.firstLevel]->HeightLog2;
238 log2Depth = tObj->Image[0][t->base.firstLevel]->DepthLog2;
239
240 numLevels = t->base.lastLevel - t->base.firstLevel + 1;
241
242 assert(numLevels <= RADEON_MAX_TEXTURE_LEVELS);
243
244 /* Calculate mipmap offsets and dimensions for blitting (uploading)
245 * The idea is that we lay out the mipmap levels within a block of
246 * memory organized as a rectangle of width BLIT_WIDTH_BYTES.
247 */
248 curOffset = 0;
249 blitWidth = R300_BLIT_WIDTH_BYTES;
250 t->tile_bits = 0;
251
252 /* figure out if this texture is suitable for tiling. */
253 #if 0 /* Disabled for now */
254 if (texelBytes) {
255 if ((tObj->Target != GL_TEXTURE_RECTANGLE_NV) &&
256 /* texrect might be able to use micro tiling too in theory? */
257 (baseImage->Height > 1)) {
258
259 /* allow 32 (bytes) x 1 mip (which will use two times the space
260 the non-tiled version would use) max if base texture is large enough */
261 if ((numLevels == 1) ||
262 (((baseImage->Width * texelBytes /
263 baseImage->Height) <= 32)
264 && (baseImage->Width * texelBytes > 64))
265 ||
266 ((baseImage->Width * texelBytes /
267 baseImage->Height) <= 16)) {
268 t->tile_bits |= R300_TXO_MICRO_TILE;
269 }
270 }
271
272 if (tObj->Target != GL_TEXTURE_RECTANGLE_NV) {
273 /* we can set macro tiling even for small textures, they will be untiled anyway */
274 t->tile_bits |= R300_TXO_MACRO_TILE;
275 }
276 }
277 #endif
278
279 for (i = 0; i < numLevels; i++) {
280 const struct gl_texture_image *texImage;
281 GLuint size;
282
283 texImage = tObj->Image[0][i + t->base.firstLevel];
284 if (!texImage)
285 break;
286
287 /* find image size in bytes */
288 if (texImage->IsCompressed) {
289 if ((t->format & R300_TX_FORMAT_DXT1) ==
290 R300_TX_FORMAT_DXT1) {
291 // fprintf(stderr,"DXT 1 %d %08X\n", texImage->Width, t->format);
292 if ((texImage->Width + 3) < 8) /* width one block */
293 size = texImage->CompressedSize * 4;
294 else if ((texImage->Width + 3) < 16)
295 size = texImage->CompressedSize * 2;
296 else
297 size = texImage->CompressedSize;
298 } else {
299 /* DXT3/5, 16 bytes per block */
300 WARN_ONCE
301 ("DXT 3/5 suffers from multitexturing problems!\n");
302 // fprintf(stderr,"DXT 3/5 %d\n", texImage->Width);
303 if ((texImage->Width + 3) < 8)
304 size = texImage->CompressedSize * 2;
305 else
306 size = texImage->CompressedSize;
307 }
308 } else if (tObj->Target == GL_TEXTURE_RECTANGLE_NV) {
309 size =
310 ((texImage->Width * texelBytes +
311 63) & ~63) * texImage->Height;
312 blitWidth = 64 / texelBytes;
313 } else if (t->tile_bits & R300_TXO_MICRO_TILE) {
314 /* tile pattern is 16 bytes x2. mipmaps stay 32 byte aligned,
315 though the actual offset may be different (if texture is less than
316 32 bytes width) to the untiled case */
317 int w = (texImage->Width * texelBytes * 2 + 31) & ~31;
318 size =
319 (w * ((texImage->Height + 1) / 2)) *
320 texImage->Depth;
321 blitWidth = MAX2(texImage->Width, 64 / texelBytes);
322 } else {
323 int w = (texImage->Width * texelBytes + 31) & ~31;
324 size = w * texImage->Height * texImage->Depth;
325 blitWidth = MAX2(texImage->Width, 64 / texelBytes);
326 }
327 assert(size > 0);
328
329 if (RADEON_DEBUG & DEBUG_TEXTURE)
330 fprintf(stderr, "w=%d h=%d d=%d tb=%d intFormat=%d\n",
331 texImage->Width, texImage->Height,
332 texImage->Depth,
333 texImage->TexFormat->TexelBytes,
334 texImage->InternalFormat);
335
336 /* Align to 32-byte offset. It is faster to do this unconditionally
337 * (no branch penalty).
338 */
339
340 curOffset = (curOffset + 0x1f) & ~0x1f;
341
342 if (texelBytes) {
343 /* fix x and y coords up later together with offset */
344 t->image[0][i].x = curOffset;
345 t->image[0][i].y = 0;
346 t->image[0][i].width =
347 MIN2(size / texelBytes, blitWidth);
348 t->image[0][i].height =
349 (size / texelBytes) / t->image[0][i].width;
350 } else {
351 t->image[0][i].x = curOffset % R300_BLIT_WIDTH_BYTES;
352 t->image[0][i].y = curOffset / R300_BLIT_WIDTH_BYTES;
353 t->image[0][i].width =
354 MIN2(size, R300_BLIT_WIDTH_BYTES);
355 t->image[0][i].height = size / t->image[0][i].width;
356 }
357
358 if (RADEON_DEBUG & DEBUG_TEXTURE)
359 fprintf(stderr,
360 "level %d: %dx%d x=%d y=%d w=%d h=%d size=%d at %d\n",
361 i, texImage->Width, texImage->Height,
362 t->image[0][i].x, t->image[0][i].y,
363 t->image[0][i].width, t->image[0][i].height,
364 size, curOffset);
365
366 curOffset += size;
367 }
368
369 /* Align the total size of texture memory block.
370 */
371 t->base.totalSize =
372 (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK;
373
374 /* Setup remaining cube face blits, if needed */
375 if (tObj->Target == GL_TEXTURE_CUBE_MAP) {
376 GLuint face;
377 for (face = 1; face < 6; face++) {
378 for (i = 0; i < numLevels; i++) {
379 t->image[face][i].x = t->image[0][i].x;
380 t->image[face][i].y = t->image[0][i].y;
381 t->image[face][i].width = t->image[0][i].width;
382 t->image[face][i].height =
383 t->image[0][i].height;
384 }
385 }
386 t->base.totalSize *= 6; /* total texmem needed */
387 }
388
389 if (tObj->Target == GL_TEXTURE_CUBE_MAP) {
390 ASSERT(log2Width == log2Height);
391 t->format |= R300_TX_FORMAT_CUBIC_MAP;
392 }
393
394 t->size =
395 (((tObj->Image[0][t->base.firstLevel]->Width -
396 1) << R300_TX_WIDTHMASK_SHIFT)
397 | ((tObj->Image[0][t->base.firstLevel]->Height - 1) <<
398 R300_TX_HEIGHTMASK_SHIFT))
399 | ((numLevels - 1) << R300_TX_MAX_MIP_LEVEL_SHIFT);
400
401 t->pitch = 0;
402
403 /* Only need to round to nearest 32 for textures, but the blitter
404 * requires 64-byte aligned pitches, and we may/may not need the
405 * blitter. NPOT only!
406 */
407 if (baseImage->IsCompressed) {
408 t->pitch |=
409 (tObj->Image[0][t->base.firstLevel]->Width + 63) & ~(63);
410 } else if (tObj->Target == GL_TEXTURE_RECTANGLE_NV) {
411 unsigned int align = blitWidth - 1;
412 t->pitch |= ((tObj->Image[0][t->base.firstLevel]->Width *
413 texelBytes) + 63) & ~(63);
414 t->size |= R300_TX_SIZE_TXPITCH_EN;
415 if (!t->image_override)
416 t->pitch_reg =
417 (((tObj->Image[0][t->base.firstLevel]->Width) +
418 align) & ~align) - 1;
419 } else {
420 t->pitch |=
421 ((tObj->Image[0][t->base.firstLevel]->Width *
422 texelBytes) + 63) & ~(63);
423 }
424
425 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515) {
426 if (tObj->Image[0][t->base.firstLevel]->Width > 2048)
427 t->pitch_reg |= R500_TXWIDTH_BIT11;
428 if (tObj->Image[0][t->base.firstLevel]->Height > 2048)
429 t->pitch_reg |= R500_TXHEIGHT_BIT11;
430 }
431
432 t->dirty_state = TEX_ALL;
433
434 /* FYI: r300UploadTexImages( rmesa, t ) used to be called here */
435 }
436
437 /* ================================================================
438 * Texture unit state management
439 */
440
441 static GLboolean r300EnableTexture2D(GLcontext * ctx, int unit)
442 {
443 r300ContextPtr rmesa = R300_CONTEXT(ctx);
444 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
445 struct gl_texture_object *tObj = texUnit->_Current;
446 r300TexObjPtr t = (r300TexObjPtr) tObj->DriverData;
447
448 ASSERT(tObj->Target == GL_TEXTURE_2D || tObj->Target == GL_TEXTURE_1D);
449
450 if (t->base.dirty_images[0]) {
451 R300_FIREVERTICES(rmesa);
452
453 r300SetTexImages(rmesa, tObj);
454 r300UploadTexImages(rmesa, (r300TexObjPtr) tObj->DriverData, 0);
455 if (!t->base.memBlock && !t->image_override)
456 return GL_FALSE;
457 }
458
459 return GL_TRUE;
460 }
461
462 static GLboolean r300EnableTexture3D(GLcontext * ctx, int unit)
463 {
464 r300ContextPtr rmesa = R300_CONTEXT(ctx);
465 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
466 struct gl_texture_object *tObj = texUnit->_Current;
467 r300TexObjPtr t = (r300TexObjPtr) tObj->DriverData;
468
469 ASSERT(tObj->Target == GL_TEXTURE_3D);
470
471 /* r300 does not support mipmaps for 3D textures. */
472 if ((tObj->MinFilter != GL_NEAREST) && (tObj->MinFilter != GL_LINEAR)) {
473 return GL_FALSE;
474 }
475
476 if (t->base.dirty_images[0]) {
477 R300_FIREVERTICES(rmesa);
478 r300SetTexImages(rmesa, tObj);
479 r300UploadTexImages(rmesa, (r300TexObjPtr) tObj->DriverData, 0);
480 if (!t->base.memBlock)
481 return GL_FALSE;
482 }
483
484 return GL_TRUE;
485 }
486
487 static GLboolean r300EnableTextureCube(GLcontext * ctx, int unit)
488 {
489 r300ContextPtr rmesa = R300_CONTEXT(ctx);
490 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
491 struct gl_texture_object *tObj = texUnit->_Current;
492 r300TexObjPtr t = (r300TexObjPtr) tObj->DriverData;
493 GLuint face;
494
495 ASSERT(tObj->Target == GL_TEXTURE_CUBE_MAP);
496
497 if (t->base.dirty_images[0] || t->base.dirty_images[1] ||
498 t->base.dirty_images[2] || t->base.dirty_images[3] ||
499 t->base.dirty_images[4] || t->base.dirty_images[5]) {
500 /* flush */
501 R300_FIREVERTICES(rmesa);
502 /* layout memory space, once for all faces */
503 r300SetTexImages(rmesa, tObj);
504 }
505
506 /* upload (per face) */
507 for (face = 0; face < 6; face++) {
508 if (t->base.dirty_images[face]) {
509 r300UploadTexImages(rmesa,
510 (r300TexObjPtr) tObj->DriverData,
511 face);
512 }
513 }
514
515 if (!t->base.memBlock) {
516 /* texmem alloc failed, use s/w fallback */
517 return GL_FALSE;
518 }
519
520 return GL_TRUE;
521 }
522
523 static GLboolean r300EnableTextureRect(GLcontext * ctx, int unit)
524 {
525 r300ContextPtr rmesa = R300_CONTEXT(ctx);
526 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
527 struct gl_texture_object *tObj = texUnit->_Current;
528 r300TexObjPtr t = (r300TexObjPtr) tObj->DriverData;
529
530 ASSERT(tObj->Target == GL_TEXTURE_RECTANGLE_NV);
531
532 if (t->base.dirty_images[0]) {
533 R300_FIREVERTICES(rmesa);
534
535 r300SetTexImages(rmesa, tObj);
536 r300UploadTexImages(rmesa, (r300TexObjPtr) tObj->DriverData, 0);
537 if (!t->base.memBlock && !t->image_override &&
538 !rmesa->prefer_gart_client_texturing)
539 return GL_FALSE;
540 }
541
542 return GL_TRUE;
543 }
544
545 static GLboolean r300UpdateTexture(GLcontext * ctx, int unit)
546 {
547 r300ContextPtr rmesa = R300_CONTEXT(ctx);
548 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
549 struct gl_texture_object *tObj = texUnit->_Current;
550 r300TexObjPtr t = (r300TexObjPtr) tObj->DriverData;
551
552 /* Fallback if there's a texture border */
553 if (tObj->Image[0][tObj->BaseLevel]->Border > 0)
554 return GL_FALSE;
555
556 /* Update state if this is a different texture object to last
557 * time.
558 */
559 if (rmesa->state.texture.unit[unit].texobj != t) {
560 if (rmesa->state.texture.unit[unit].texobj != NULL) {
561 /* The old texture is no longer bound to this texture unit.
562 * Mark it as such.
563 */
564
565 rmesa->state.texture.unit[unit].texobj->base.bound &=
566 ~(1 << unit);
567 }
568
569 rmesa->state.texture.unit[unit].texobj = t;
570 t->base.bound |= (1 << unit);
571 t->dirty_state |= 1 << unit;
572 driUpdateTextureLRU((driTextureObject *) t); /* XXX: should be locked! */
573 }
574
575 return !t->border_fallback;
576 }
577
578 void r300SetTexOffset(__DRIcontext * pDRICtx, GLint texname,
579 unsigned long long offset, GLint depth, GLuint pitch)
580 {
581 r300ContextPtr rmesa = pDRICtx->driverPrivate;
582 struct gl_texture_object *tObj =
583 _mesa_lookup_texture(rmesa->radeon.glCtx, texname);
584 r300TexObjPtr t;
585 uint32_t pitch_val;
586
587 if (!tObj)
588 return;
589
590 t = (r300TexObjPtr) tObj->DriverData;
591
592 t->image_override = GL_TRUE;
593
594 if (!offset)
595 return;
596
597 t->offset = offset;
598 t->pitch_reg &= (1 << 13) -1;
599 pitch_val = pitch;
600
601 switch (depth) {
602 case 32:
603 t->format = R300_EASY_TX_FORMAT(X, Y, Z, W, W8Z8Y8X8);
604 t->filter |= tx_table[2].filter;
605 pitch_val /= 4;
606 break;
607 case 24:
608 default:
609 t->format = R300_EASY_TX_FORMAT(X, Y, Z, ONE, W8Z8Y8X8);
610 t->filter |= tx_table[4].filter;
611 pitch_val /= 4;
612 break;
613 case 16:
614 t->format = R300_EASY_TX_FORMAT(X, Y, Z, ONE, Z5Y6X5);
615 t->filter |= tx_table[5].filter;
616 pitch_val /= 2;
617 break;
618 }
619 pitch_val--;
620
621 t->pitch_reg |= pitch_val;
622 }
623
624 static GLboolean r300UpdateTextureUnit(GLcontext * ctx, int unit)
625 {
626 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
627
628 if (texUnit->_ReallyEnabled & (TEXTURE_RECT_BIT)) {
629 return (r300EnableTextureRect(ctx, unit) &&
630 r300UpdateTexture(ctx, unit));
631 } else if (texUnit->_ReallyEnabled & (TEXTURE_1D_BIT | TEXTURE_2D_BIT)) {
632 return (r300EnableTexture2D(ctx, unit) &&
633 r300UpdateTexture(ctx, unit));
634 } else if (texUnit->_ReallyEnabled & (TEXTURE_3D_BIT)) {
635 return (r300EnableTexture3D(ctx, unit) &&
636 r300UpdateTexture(ctx, unit));
637 } else if (texUnit->_ReallyEnabled & (TEXTURE_CUBE_BIT)) {
638 return (r300EnableTextureCube(ctx, unit) &&
639 r300UpdateTexture(ctx, unit));
640 } else if (texUnit->_ReallyEnabled) {
641 return GL_FALSE;
642 } else {
643 return GL_TRUE;
644 }
645 }
646
647 void r300UpdateTextureState(GLcontext * ctx)
648 {
649 int i;
650
651 for (i = 0; i < 8; i++) {
652 if (!r300UpdateTextureUnit(ctx, i)) {
653 _mesa_warning(ctx,
654 "failed to update texture state for unit %d.\n",
655 i);
656 }
657 }
658 }