Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_texmem.c
1 /**************************************************************************
2
3 Copyright (C) Tungsten Graphics 2002. All Rights Reserved.
4 The Weather Channel, Inc. funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86
6 license. 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 on the rights to use, copy, modify, merge, publish,
12 distribute, sub license, 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
23 NON-INFRINGEMENT. IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR THEIR
24 SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
26 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28
29 **************************************************************************/
30
31 /**
32 * \file
33 *
34 * \author Gareth Hughes <gareth@valinux.com>
35 *
36 * \author Kevin E. Martin <martin@valinux.com>
37 */
38
39 #include <errno.h>
40
41 #include "main/glheader.h"
42 #include "main/imports.h"
43 #include "main/context.h"
44 #include "main/colormac.h"
45 #include "main/macros.h"
46 #include "main/simple_list.h"
47 #include "main/texobj.h"
48 #include "radeon_reg.h" /* gets definition for usleep */
49 #include "r300_context.h"
50 #include "r300_state.h"
51 #include "r300_cmdbuf.h"
52 #include "radeon_ioctl.h"
53 #include "r300_tex.h"
54 #include "r300_ioctl.h"
55 #include <unistd.h> /* for usleep() */
56
57 #ifdef USER_BUFFERS
58 #include "r300_mem.h"
59 #endif
60
61 /**
62 * Destroy any device-dependent state associated with the texture. This may
63 * include NULLing out hardware state that points to the texture.
64 */
65 void r300DestroyTexObj(r300ContextPtr rmesa, r300TexObjPtr t)
66 {
67 int i;
68
69 if (RADEON_DEBUG & DEBUG_TEXTURE) {
70 fprintf(stderr, "%s( %p, %p )\n", __FUNCTION__,
71 (void *)t, (void *)t->base.tObj);
72 }
73
74 for (i = 0; i < rmesa->radeon.glCtx->Const.MaxTextureUnits; i++) {
75 if (rmesa->state.texture.unit[i].texobj == t->base.tObj) {
76 _mesa_reference_texobj(&rmesa->state.texture.unit[i].texobj, NULL);
77 }
78 }
79 }
80
81 /* ------------------------------------------------------------
82 * Texture image conversions
83 */
84
85 static void r300UploadGARTClientSubImage(r300ContextPtr rmesa,
86 r300TexObjPtr t,
87 struct gl_texture_image *texImage,
88 GLint hwlevel,
89 GLint x, GLint y,
90 GLint width, GLint height)
91 {
92 const struct gl_texture_format *texFormat = texImage->TexFormat;
93 GLuint srcPitch, dstPitch;
94 int blit_format;
95 int srcOffset;
96
97 /*
98 * XXX it appears that we always upload the full image, not a subimage.
99 * I.e. x==0, y==0, width=texWidth, height=texWidth. If this is ever
100 * changed, the src pitch will have to change.
101 */
102 switch (texFormat->TexelBytes) {
103 case 1:
104 blit_format = R300_CP_COLOR_FORMAT_CI8;
105 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
106 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
107 break;
108 case 2:
109 blit_format = R300_CP_COLOR_FORMAT_RGB565;
110 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
111 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
112 break;
113 case 4:
114 blit_format = R300_CP_COLOR_FORMAT_ARGB8888;
115 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
116 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
117 break;
118 case 8:
119 case 16:
120 blit_format = R300_CP_COLOR_FORMAT_CI8;
121 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
122 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
123 break;
124 default:
125 return;
126 }
127
128 t->image[0][hwlevel].data = texImage->Data;
129 srcOffset = r300GartOffsetFromVirtual(rmesa, texImage->Data);
130
131 assert(srcOffset != ~0);
132
133 /* Don't currently need to cope with small pitches?
134 */
135 width = texImage->Width;
136 height = texImage->Height;
137
138 if (texFormat->TexelBytes > 4) {
139 width *= texFormat->TexelBytes;
140 }
141
142 r300EmitWait(rmesa, R300_WAIT_3D);
143
144 r300EmitBlit(rmesa, blit_format,
145 srcPitch,
146 srcOffset,
147 dstPitch,
148 t->bufAddr,
149 x,
150 y,
151 t->image[0][hwlevel].x + x,
152 t->image[0][hwlevel].y + y, width, height);
153
154 r300EmitWait(rmesa, R300_WAIT_2D);
155 }
156
157 static void r300UploadRectSubImage(r300ContextPtr rmesa,
158 r300TexObjPtr t,
159 struct gl_texture_image *texImage,
160 GLint x, GLint y, GLint width, GLint height)
161 {
162 const struct gl_texture_format *texFormat = texImage->TexFormat;
163 int blit_format, dstPitch, done;
164
165 switch (texFormat->TexelBytes) {
166 case 1:
167 blit_format = R300_CP_COLOR_FORMAT_CI8;
168 break;
169 case 2:
170 blit_format = R300_CP_COLOR_FORMAT_RGB565;
171 break;
172 case 4:
173 blit_format = R300_CP_COLOR_FORMAT_ARGB8888;
174 break;
175 case 8:
176 case 16:
177 blit_format = R300_CP_COLOR_FORMAT_CI8;
178 break;
179 default:
180 return;
181 }
182
183 t->image[0][0].data = texImage->Data;
184
185 /* Currently don't need to cope with small pitches.
186 */
187 width = texImage->Width;
188 height = texImage->Height;
189 dstPitch = t->pitch;
190
191 if (texFormat->TexelBytes > 4) {
192 width *= texFormat->TexelBytes;
193 }
194
195 if (rmesa->prefer_gart_client_texturing && texImage->IsClientData) {
196 /* In this case, could also use GART texturing. This is
197 * currently disabled, but has been tested & works.
198 */
199 t->offset = r300GartOffsetFromVirtual(rmesa, texImage->Data);
200 t->pitch = texImage->RowStride * texFormat->TexelBytes - 32;
201
202 if (RADEON_DEBUG & DEBUG_TEXTURE)
203 fprintf(stderr,
204 "Using GART texturing for rectangular client texture\n");
205
206 /* Release FB memory allocated for this image:
207 */
208 /* FIXME This may not be correct as driSwapOutTextureObject sets
209 * FIXME dirty_images. It may be fine, though.
210 */
211 if (t->base.memBlock) {
212 driSwapOutTextureObject((driTextureObject *) t);
213 }
214 } else if (texImage->IsClientData) {
215 /* Data already in GART memory, with usable pitch.
216 */
217 GLuint srcPitch;
218 srcPitch = texImage->RowStride * texFormat->TexelBytes;
219 r300EmitBlit(rmesa,
220 blit_format,
221 srcPitch,
222 r300GartOffsetFromVirtual(rmesa, texImage->Data),
223 dstPitch, t->bufAddr, 0, 0, 0, 0, width, height);
224 } else {
225 /* Data not in GART memory, or bad pitch.
226 */
227 for (done = 0; done < height;) {
228 struct r300_dma_region region;
229 int lines =
230 MIN2(height - done, RADEON_BUFFER_SIZE / dstPitch);
231 int src_pitch;
232 char *tex;
233
234 src_pitch = texImage->RowStride * texFormat->TexelBytes;
235
236 tex = (char *)texImage->Data + done * src_pitch;
237
238 memset(&region, 0, sizeof(region));
239 r300AllocDmaRegion(rmesa, &region, lines * dstPitch,
240 1024);
241
242 /* Copy texdata to dma:
243 */
244 if (RADEON_DEBUG & DEBUG_TEXTURE)
245 fprintf(stderr,
246 "%s: src_pitch %d dst_pitch %d\n",
247 __FUNCTION__, src_pitch, dstPitch);
248
249 if (src_pitch == dstPitch) {
250 memcpy(region.address + region.start, tex,
251 lines * src_pitch);
252 } else {
253 char *buf = region.address + region.start;
254 int i;
255 for (i = 0; i < lines; i++) {
256 memcpy(buf, tex, src_pitch);
257 buf += dstPitch;
258 tex += src_pitch;
259 }
260 }
261
262 r300EmitWait(rmesa, R300_WAIT_3D);
263
264 /* Blit to framebuffer
265 */
266 r300EmitBlit(rmesa,
267 blit_format,
268 dstPitch, GET_START(&region),
269 dstPitch | (t->tile_bits >> 16),
270 t->bufAddr, 0, 0, 0, done, width, lines);
271
272 r300EmitWait(rmesa, R300_WAIT_2D);
273 #ifdef USER_BUFFERS
274 r300_mem_use(rmesa, region.buf->id);
275 #endif
276
277 r300ReleaseDmaRegion(rmesa, &region, __FUNCTION__);
278 done += lines;
279 }
280 }
281 }
282
283 /**
284 * Upload the texture image associated with texture \a t at the specified
285 * level at the address relative to \a start.
286 */
287 static void r300UploadSubImage(r300ContextPtr rmesa, r300TexObjPtr t,
288 GLint hwlevel,
289 GLint x, GLint y, GLint width, GLint height,
290 GLuint face)
291 {
292 struct gl_texture_image *texImage = NULL;
293 GLuint offset;
294 GLint imageWidth, imageHeight;
295 GLint ret;
296 drm_radeon_texture_t tex;
297 drm_radeon_tex_image_t tmp;
298 const int level = hwlevel + t->base.firstLevel;
299
300 if (RADEON_DEBUG & DEBUG_TEXTURE) {
301 fprintf(stderr,
302 "%s( %p, %p ) level/width/height/face = %d/%d/%d/%u\n",
303 __FUNCTION__, (void *)t, (void *)t->base.tObj, level,
304 width, height, face);
305 }
306
307 ASSERT(face < 6);
308
309 /* Ensure we have a valid texture to upload */
310 if ((hwlevel < 0) || (hwlevel >= R300_MAX_TEXTURE_LEVELS)) {
311 _mesa_problem(NULL, "bad texture level in %s", __FUNCTION__);
312 return;
313 }
314
315 texImage = t->base.tObj->Image[face][level];
316
317 if (!texImage) {
318 if (RADEON_DEBUG & DEBUG_TEXTURE)
319 fprintf(stderr, "%s: texImage %d is NULL!\n",
320 __FUNCTION__, level);
321 return;
322 }
323 if (!texImage->Data) {
324 if (RADEON_DEBUG & DEBUG_TEXTURE)
325 fprintf(stderr, "%s: image data is NULL!\n",
326 __FUNCTION__);
327 return;
328 }
329
330 if (t->base.tObj->Target == GL_TEXTURE_RECTANGLE_NV) {
331 assert(level == 0);
332 assert(hwlevel == 0);
333 if (RADEON_DEBUG & DEBUG_TEXTURE)
334 fprintf(stderr, "%s: image data is rectangular\n",
335 __FUNCTION__);
336 r300UploadRectSubImage(rmesa, t, texImage, x, y, width, height);
337 return;
338 } else if (texImage->IsClientData) {
339 if (RADEON_DEBUG & DEBUG_TEXTURE)
340 fprintf(stderr,
341 "%s: image data is in GART client storage\n",
342 __FUNCTION__);
343 r300UploadGARTClientSubImage(rmesa, t, texImage, hwlevel, x, y,
344 width, height);
345 return;
346 } else if (RADEON_DEBUG & DEBUG_TEXTURE)
347 fprintf(stderr, "%s: image data is in normal memory\n",
348 __FUNCTION__);
349
350 imageWidth = texImage->Width;
351 imageHeight = texImage->Height;
352
353 offset = t->bufAddr;
354
355 if (RADEON_DEBUG & (DEBUG_TEXTURE | DEBUG_IOCTL)) {
356 GLint imageX = 0;
357 GLint imageY = 0;
358 GLint blitX = t->image[face][hwlevel].x;
359 GLint blitY = t->image[face][hwlevel].y;
360 GLint blitWidth = t->image[face][hwlevel].width;
361 GLint blitHeight = t->image[face][hwlevel].height;
362 fprintf(stderr, " upload image: %d,%d at %d,%d\n",
363 imageWidth, imageHeight, imageX, imageY);
364 fprintf(stderr, " upload blit: %d,%d at %d,%d\n",
365 blitWidth, blitHeight, blitX, blitY);
366 fprintf(stderr, " blit ofs: 0x%07x level: %d/%d\n",
367 (GLuint) offset, hwlevel, level);
368 }
369
370 t->image[face][hwlevel].data = texImage->Data;
371
372 /* Init the DRM_RADEON_TEXTURE command / drm_radeon_texture_t struct.
373 * NOTE: we're always use a 1KB-wide blit and I8 texture format.
374 * We used to use 1, 2 and 4-byte texels and used to use the texture
375 * width to dictate the blit width - but that won't work for compressed
376 * textures. (Brian)
377 * NOTE: can't do that with texture tiling. (sroland)
378 */
379 tex.offset = offset;
380 tex.image = &tmp;
381 /* copy (x,y,width,height,data) */
382 memcpy(&tmp, &t->image[face][hwlevel], sizeof(tmp));
383
384 if (texImage->TexFormat->TexelBytes > 4) {
385 const int log2TexelBytes =
386 (3 + (texImage->TexFormat->TexelBytes >> 4));
387 tex.format = RADEON_TXFORMAT_I8; /* any 1-byte texel format */
388 tex.pitch =
389 MAX2((texImage->Width * texImage->TexFormat->TexelBytes) /
390 64, 1);
391 tex.height = imageHeight;
392 tex.width = imageWidth << log2TexelBytes;
393 tex.offset += (tmp.x << log2TexelBytes) & ~1023;
394 tmp.x = tmp.x % (1024 >> log2TexelBytes);
395 tmp.width = tmp.width << log2TexelBytes;
396 } else if (texImage->TexFormat->TexelBytes) {
397 /* use multi-byte upload scheme */
398 tex.height = imageHeight;
399 tex.width = imageWidth;
400 switch (texImage->TexFormat->TexelBytes) {
401 case 1:
402 tex.format = RADEON_TXFORMAT_I8;
403 break;
404 case 2:
405 tex.format = RADEON_TXFORMAT_AI88;
406 break;
407 case 4:
408 tex.format = RADEON_TXFORMAT_ARGB8888;
409 break;
410 }
411 tex.pitch =
412 MAX2((texImage->Width * texImage->TexFormat->TexelBytes) /
413 64, 1);
414 tex.offset += tmp.x & ~1023;
415 tmp.x = tmp.x % 1024;
416
417 if (t->tile_bits & R300_TXO_MICRO_TILE) {
418 /* need something like "tiled coordinates" ? */
419 tmp.y = tmp.x / (tex.pitch * 128) * 2;
420 tmp.x =
421 tmp.x % (tex.pitch * 128) / 2 /
422 texImage->TexFormat->TexelBytes;
423 tex.pitch |= RADEON_DST_TILE_MICRO >> 22;
424 } else {
425 tmp.x = tmp.x >> (texImage->TexFormat->TexelBytes >> 1);
426 }
427 #if 1
428 if ((t->tile_bits & R300_TXO_MACRO_TILE) &&
429 (texImage->Width * texImage->TexFormat->TexelBytes >= 256)
430 && ((!(t->tile_bits & R300_TXO_MICRO_TILE)
431 && (texImage->Height >= 8))
432 || (texImage->Height >= 16))) {
433 /* weird: R200 disables macro tiling if mip width is smaller than 256 bytes,
434 OR if height is smaller than 8 automatically, but if micro tiling is active
435 the limit is height 16 instead ? */
436 tex.pitch |= RADEON_DST_TILE_MACRO >> 22;
437 }
438 #endif
439 } else {
440 /* In case of for instance 8x8 texture (2x2 dxt blocks),
441 padding after the first two blocks is needed (only
442 with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
443 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block)
444 has 4 real pixels. Needed so the kernel module reads
445 the right amount of data. */
446 tex.format = RADEON_TXFORMAT_I8; /* any 1-byte texel format */
447 tex.pitch = (R300_BLIT_WIDTH_BYTES / 64);
448 tex.height = (imageHeight + 3) / 4;
449 tex.width = (imageWidth + 3) / 4;
450 if ((t->format & R300_TX_FORMAT_DXT1) == R300_TX_FORMAT_DXT1) {
451 tex.width *= 8;
452 } else {
453 tex.width *= 16;
454 }
455 }
456
457 LOCK_HARDWARE(&rmesa->radeon);
458 do {
459 ret =
460 drmCommandWriteRead(rmesa->radeon.dri.fd,
461 DRM_RADEON_TEXTURE, &tex,
462 sizeof(drm_radeon_texture_t));
463 if (ret) {
464 if (RADEON_DEBUG & DEBUG_IOCTL)
465 fprintf(stderr,
466 "DRM_RADEON_TEXTURE: again!\n");
467 usleep(1);
468 }
469 } while (ret == -EAGAIN);
470
471 UNLOCK_HARDWARE(&rmesa->radeon);
472
473 if (ret) {
474 fprintf(stderr, "DRM_RADEON_TEXTURE: return = %d\n", ret);
475 fprintf(stderr, " offset=0x%08x\n", offset);
476 fprintf(stderr, " image width=%d height=%d\n",
477 imageWidth, imageHeight);
478 fprintf(stderr, " blit width=%d height=%d data=%p\n",
479 t->image[face][hwlevel].width,
480 t->image[face][hwlevel].height,
481 t->image[face][hwlevel].data);
482 _mesa_exit(-1);
483 }
484 }
485
486 /**
487 * Upload the texture images associated with texture \a t. This might
488 * require the allocation of texture memory.
489 *
490 * \param rmesa Context pointer
491 * \param t Texture to be uploaded
492 * \param face Cube map face to be uploaded. Zero for non-cube maps.
493 */
494
495 int r300UploadTexImages(r300ContextPtr rmesa, r300TexObjPtr t, GLuint face)
496 {
497 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
498
499 if (t->image_override)
500 return 0;
501
502 if (RADEON_DEBUG & (DEBUG_TEXTURE | DEBUG_IOCTL)) {
503 fprintf(stderr, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__,
504 (void *)rmesa->radeon.glCtx, (void *)t->base.tObj,
505 t->base.totalSize, t->base.firstLevel,
506 t->base.lastLevel);
507 }
508
509 if (t->base.totalSize == 0)
510 return 0;
511
512 if (RADEON_DEBUG & DEBUG_SYNC) {
513 fprintf(stderr, "%s: Syncing\n", __FUNCTION__);
514 radeonFinish(rmesa->radeon.glCtx);
515 }
516
517 LOCK_HARDWARE(&rmesa->radeon);
518
519 if (t->base.memBlock == NULL) {
520 int heap;
521
522 heap = driAllocateTexture(rmesa->texture_heaps, rmesa->nr_heaps,
523 (driTextureObject *) t);
524 if (heap == -1) {
525 UNLOCK_HARDWARE(&rmesa->radeon);
526 return -1;
527 }
528
529 /* Set the base offset of the texture image */
530 t->bufAddr = rmesa->radeon.radeonScreen->texOffset[heap]
531 + t->base.memBlock->ofs;
532 t->offset = t->bufAddr;
533
534 if (!(t->base.tObj->Image[0][0]->IsClientData)) {
535 /* hope it's safe to add that here... */
536 t->offset |= t->tile_bits;
537 }
538 }
539
540 /* Let the world know we've used this memory recently.
541 */
542 driUpdateTextureLRU((driTextureObject *) t);
543 UNLOCK_HARDWARE(&rmesa->radeon);
544
545 /* Upload any images that are new */
546 if (t->base.dirty_images[face]) {
547 int i;
548 for (i = 0; i < numLevels; i++) {
549 if ((t->base.
550 dirty_images[face] & (1 <<
551 (i + t->base.firstLevel))) !=
552 0) {
553 r300UploadSubImage(rmesa, t, i, 0, 0,
554 t->image[face][i].width,
555 t->image[face][i].height,
556 face);
557 }
558 }
559 t->base.dirty_images[face] = 0;
560 }
561
562 if (RADEON_DEBUG & DEBUG_SYNC) {
563 fprintf(stderr, "%s: Syncing\n", __FUNCTION__);
564 radeonFinish(rmesa->radeon.glCtx);
565 }
566
567 return 0;
568 }