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