Initial multitexturing support. Old behaviour can be re-enabled by changing ifdefs...
[mesa.git] / src / mesa / drivers / dri / r300 / r300_texmem.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r300/r300_texmem.c,v 1.5 2002/12/17 00:32:56 dawes Exp $ */
2 /**************************************************************************
3
4 Copyright (C) Tungsten Graphics 2002. All Rights Reserved.
5 The Weather Channel, Inc. funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86
7 license. This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation on the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 NON-INFRINGEMENT. IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR THEIR
25 SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29
30 **************************************************************************/
31
32 /*
33 * Authors:
34 * Kevin E. Martin <martin@valinux.com>
35 * Gareth Hughes <gareth@valinux.com>
36 *
37 */
38
39 #include <errno.h>
40
41 #include "glheader.h"
42 #include "imports.h"
43 #include "context.h"
44 #include "colormac.h"
45 #include "macros.h"
46 #include "simple_list.h"
47 #include "radeon_reg.h" /* gets definition for usleep */
48 #include "r300_context.h"
49 #include "r300_state.h"
50 #include "r300_cmdbuf.h"
51 #include "radeon_ioctl.h"
52 /*
53 #include "r300_swtcl.h"
54 */
55 #include "r300_tex.h"
56 #include "r300_ioctl.h"
57 #include <unistd.h> /* for usleep() */
58
59 /**
60 * Destroy any device-dependent state associated with the texture. This may
61 * include NULLing out hardware state that points to the texture.
62 */
63 void r300DestroyTexObj(r300ContextPtr rmesa, r300TexObjPtr t)
64 {
65 if (RADEON_DEBUG & DEBUG_TEXTURE) {
66 fprintf(stderr, "%s( %p, %p )\n", __FUNCTION__,
67 (void *)t, (void *)t->base.tObj);
68 }
69
70 if (rmesa != NULL) {
71 unsigned i;
72
73 for (i = 0; i < rmesa->radeon.glCtx->Const.MaxTextureUnits; i++) {
74 if (t == rmesa->state.texture.unit[i].texobj) {
75 rmesa->state.texture.unit[i].texobj = NULL;
76 /* This code below is meant to shorten state
77 pushed to the hardware by not programming
78 unneeded units.
79
80 This does not appear to be worthwhile on R300 */
81 #if 0
82 remove_from_list(&rmesa->hw.tex[i]);
83 make_empty_list(&rmesa->hw.tex[i]);
84 remove_from_list(&rmesa->hw.cube[i]);
85 make_empty_list(&rmesa->hw.cube[i]);
86 #endif
87 }
88 }
89 }
90 }
91
92 /* ------------------------------------------------------------
93 * Texture image conversions
94 */
95
96 static void r300UploadGARTClientSubImage(r300ContextPtr rmesa,
97 r300TexObjPtr t,
98 struct gl_texture_image *texImage,
99 GLint hwlevel,
100 GLint x, GLint y,
101 GLint width, GLint height)
102 {
103 const struct gl_texture_format *texFormat = texImage->TexFormat;
104 GLuint srcPitch, dstPitch;
105 int blit_format;
106 int srcOffset;
107
108 /*
109 * XXX it appears that we always upload the full image, not a subimage.
110 * I.e. x==0, y==0, width=texWidth, height=texWidth. If this is ever
111 * changed, the src pitch will have to change.
112 */
113 switch (texFormat->TexelBytes) {
114 case 1:
115 blit_format = R200_CP_COLOR_FORMAT_CI8;
116 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
117 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
118 break;
119 case 2:
120 blit_format = R200_CP_COLOR_FORMAT_RGB565;
121 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
122 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
123 break;
124 case 4:
125 blit_format = R200_CP_COLOR_FORMAT_ARGB8888;
126 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
127 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
128 break;
129 default:
130 return;
131 }
132
133 t->image[0][hwlevel].data = texImage->Data;
134 srcOffset = r300GartOffsetFromVirtual(rmesa, texImage->Data);
135
136 assert(srcOffset != ~0);
137
138 /* Don't currently need to cope with small pitches?
139 */
140 width = texImage->Width;
141 height = texImage->Height;
142
143 r300EmitWait(rmesa, R300_WAIT_3D);
144
145 r300EmitBlit(rmesa, blit_format,
146 srcPitch,
147 srcOffset,
148 dstPitch,
149 t->bufAddr,
150 x,
151 y,
152 t->image[0][hwlevel].x + x,
153 t->image[0][hwlevel].y + y, width, height);
154
155 r300EmitWait(rmesa, R300_WAIT_2D);
156 }
157
158 static void r300UploadRectSubImage(r300ContextPtr rmesa,
159 r300TexObjPtr t,
160 struct gl_texture_image *texImage,
161 GLint x, GLint y, GLint width, GLint height)
162 {
163 const struct gl_texture_format *texFormat = texImage->TexFormat;
164 int blit_format, dstPitch, done;
165
166 switch (texFormat->TexelBytes) {
167 case 1:
168 blit_format = R200_CP_COLOR_FORMAT_CI8;
169 break;
170 case 2:
171 blit_format = R200_CP_COLOR_FORMAT_RGB565;
172 break;
173 case 4:
174 blit_format = R200_CP_COLOR_FORMAT_ARGB8888;
175 break;
176 default:
177 return;
178 }
179
180 t->image[0][0].data = texImage->Data;
181
182 /* Currently don't need to cope with small pitches.
183 */
184 width = texImage->Width;
185 height = texImage->Height;
186 dstPitch = t->pitch + 32;
187
188 if (rmesa->prefer_gart_client_texturing && texImage->IsClientData) {
189 /* In this case, could also use GART texturing. This is
190 * currently disabled, but has been tested & works.
191 */
192 t->offset =
193 r300GartOffsetFromVirtual(rmesa, texImage->Data);
194 t->pitch =
195 texImage->RowStride * texFormat->TexelBytes - 32;
196
197 if (RADEON_DEBUG & DEBUG_TEXTURE)
198 fprintf(stderr,
199 "Using GART texturing for rectangular client texture\n");
200
201 /* Release FB memory allocated for this image:
202 */
203 /* FIXME This may not be correct as driSwapOutTextureObject sets
204 * FIXME dirty_images. It may be fine, though.
205 */
206 if (t->base.memBlock) {
207 driSwapOutTextureObject((driTextureObject *) t);
208 }
209 } else if (texImage->IsClientData) {
210 /* Data already in GART memory, with usable pitch.
211 */
212 GLuint srcPitch;
213 srcPitch = texImage->RowStride * texFormat->TexelBytes;
214 r300EmitBlit(rmesa,
215 blit_format,
216 srcPitch,
217 r300GartOffsetFromVirtual(rmesa, texImage->Data),
218 dstPitch, t->bufAddr, 0, 0, 0, 0, width, height);
219 } else {
220 /* Data not in GART memory, or bad pitch.
221 */
222 for (done = 0; done < height;) {
223 struct r300_dma_region region;
224 int lines =
225 MIN2(height - done, RADEON_BUFFER_SIZE / dstPitch);
226 int src_pitch;
227 char *tex;
228
229 src_pitch = texImage->RowStride * texFormat->TexelBytes;
230
231 tex = (char *)texImage->Data + done * src_pitch;
232
233 memset(&region, 0, sizeof(region));
234 r300AllocDmaRegion(rmesa, &region, lines * dstPitch,
235 1024);
236
237 /* Copy texdata to dma:
238 */
239 if (0)
240 fprintf(stderr,
241 "%s: src_pitch %d dst_pitch %d\n",
242 __FUNCTION__, src_pitch, dstPitch);
243
244 if (src_pitch == dstPitch) {
245 memcpy(region.address + region.start, tex,
246 lines * src_pitch);
247 } else {
248 char *buf = region.address + region.start;
249 int i;
250 for (i = 0; i < lines; i++) {
251 memcpy(buf, tex, src_pitch);
252 buf += dstPitch;
253 tex += src_pitch;
254 }
255 }
256
257 r300EmitWait(rmesa, R300_WAIT_3D);
258
259 /* Blit to framebuffer
260 */
261 r300EmitBlit(rmesa,
262 blit_format,
263 dstPitch, GET_START(&region),
264 dstPitch, t->bufAddr,
265 0, 0, 0, done, width, lines);
266
267 r300EmitWait(rmesa, R300_WAIT_2D);
268
269 r300ReleaseDmaRegion(rmesa, &region, __FUNCTION__);
270 done += lines;
271 }
272 }
273 }
274
275 /**
276 * Upload the texture image associated with texture \a t at the specified
277 * level at the address relative to \a start.
278 */
279 static void uploadSubImage(r300ContextPtr rmesa, r300TexObjPtr t,
280 GLint hwlevel,
281 GLint x, GLint y, GLint width, GLint height,
282 GLuint face)
283 {
284 struct gl_texture_image *texImage = NULL;
285 GLuint offset;
286 GLint imageWidth, imageHeight;
287 GLint ret;
288 drm_radeon_texture_t tex;
289 drm_radeon_tex_image_t tmp;
290 const int level = hwlevel + t->base.firstLevel;
291
292 if (RADEON_DEBUG & DEBUG_TEXTURE) {
293 fprintf(stderr,
294 "%s( %p, %p ) level/width/height/face = %d/%d/%d/%u\n",
295 __FUNCTION__, (void *)t, (void *)t->base.tObj, level,
296 width, height, face);
297 }
298
299 ASSERT(face < 6);
300
301 /* Ensure we have a valid texture to upload */
302 if ((hwlevel < 0) || (hwlevel >= RADEON_MAX_TEXTURE_LEVELS)) {
303 _mesa_problem(NULL, "bad texture level in %s", __FUNCTION__);
304 return;
305 }
306
307 texImage = t->base.tObj->Image[face][level];
308
309 if (!texImage) {
310 if (RADEON_DEBUG & DEBUG_TEXTURE)
311 fprintf(stderr, "%s: texImage %d is NULL!\n",
312 __FUNCTION__, level);
313 return;
314 }
315 if (!texImage->Data) {
316 if (RADEON_DEBUG & DEBUG_TEXTURE)
317 fprintf(stderr, "%s: image data is NULL!\n",
318 __FUNCTION__);
319 return;
320 }
321
322 if (t->base.tObj->Target == GL_TEXTURE_RECTANGLE_NV) {
323 assert(level == 0);
324 assert(hwlevel == 0);
325 if (RADEON_DEBUG & DEBUG_TEXTURE)
326 fprintf(stderr, "%s: image data is rectangular\n",
327 __FUNCTION__);
328 r300UploadRectSubImage(rmesa, t, texImage, x, y, width, height);
329 return;
330 } else if (texImage->IsClientData) {
331 if (RADEON_DEBUG & DEBUG_TEXTURE)
332 fprintf(stderr,
333 "%s: image data is in GART client storage\n",
334 __FUNCTION__);
335 r300UploadGARTClientSubImage(rmesa, t, texImage, hwlevel, x, y,
336 width, height);
337 return;
338 } else if (RADEON_DEBUG & DEBUG_TEXTURE)
339 fprintf(stderr, "%s: image data is in normal memory\n",
340 __FUNCTION__);
341
342 imageWidth = texImage->Width;
343 imageHeight = texImage->Height;
344
345 offset = t->bufAddr;
346
347 if (RADEON_DEBUG & (DEBUG_TEXTURE | DEBUG_IOCTL)) {
348 GLint imageX = 0;
349 GLint imageY = 0;
350 GLint blitX = t->image[face][hwlevel].x;
351 GLint blitY = t->image[face][hwlevel].y;
352 GLint blitWidth = t->image[face][hwlevel].width;
353 GLint blitHeight = t->image[face][hwlevel].height;
354 fprintf(stderr, " upload image: %d,%d at %d,%d\n",
355 imageWidth, imageHeight, imageX, imageY);
356 fprintf(stderr, " upload blit: %d,%d at %d,%d\n",
357 blitWidth, blitHeight, blitX, blitY);
358 fprintf(stderr, " blit ofs: 0x%07x level: %d/%d\n",
359 (GLuint) offset, hwlevel, level);
360 }
361
362 t->image[face][hwlevel].data = texImage->Data;
363
364 /* Init the DRM_RADEON_TEXTURE command / drm_radeon_texture_t struct.
365 * NOTE: we're always use a 1KB-wide blit and I8 texture format.
366 * We used to use 1, 2 and 4-byte texels and used to use the texture
367 * width to dictate the blit width - but that won't work for compressed
368 * textures. (Brian)
369 */
370
371 tex.offset = offset;
372 tex.pitch = BLIT_WIDTH_BYTES / 64;
373 tex.format = R200_TXFORMAT_I8; /* any 1-byte texel format */
374 #if 0 /* I am not sure HOSTDATA_BLT actually works.. Experiment here - V.D */
375 tex.format = R200_TXFORMAT_RGBA8888; /* any 4-byte texel format */
376 #endif
377 if (texImage->TexFormat->TexelBytes) {
378 tex.width = imageWidth * texImage->TexFormat->TexelBytes; /* in bytes */
379 tex.height = imageHeight;
380 } else {
381 tex.width = imageWidth; /* compressed */
382 tex.height = imageHeight;
383 if (tex.height < 4)
384 tex.height = 4;
385 }
386 tex.image = &tmp;
387 #if 0
388 tex.width /= 4;
389 #endif
390
391 /* copy (x,y,width,height,data) */
392 memcpy(&tmp, &t->image[face][hwlevel], sizeof(tmp));
393 #if 0
394 tex.image->width /=4;
395 #endif
396
397 #if 0
398 sleep(1);
399
400 fprintf(stderr, "*** Uploading texture\n");
401 fprintf(stderr, " offset=0x%08x\n", offset);
402 fprintf(stderr, " image width=%d height=%d\n",
403 imageWidth, imageHeight);
404 fprintf(stderr, " blit width=%d height=%d data=%p\n",
405 t->image[face][hwlevel].width,
406 t->image[face][hwlevel].height,
407 t->image[face][hwlevel].data);
408 #endif
409
410 LOCK_HARDWARE(&rmesa->radeon);
411 do {
412 ret = drmCommandWriteRead(rmesa->radeon.dri.fd, DRM_RADEON_TEXTURE,
413 &tex, sizeof(drm_radeon_texture_t));
414 if (ret) {
415 if (RADEON_DEBUG & DEBUG_IOCTL)
416 fprintf(stderr,
417 "DRM_RADEON_TEXTURE: again!\n");
418 usleep(1);
419 }
420 } while (ret && errno == EAGAIN);
421
422 UNLOCK_HARDWARE(&rmesa->radeon);
423
424 if (ret) {
425 fprintf(stderr, "DRM_RADEON_TEXTURE: return = %d\n", ret);
426 fprintf(stderr, " offset=0x%08x\n", offset);
427 fprintf(stderr, " image width=%d height=%d\n",
428 imageWidth, imageHeight);
429 fprintf(stderr, " blit width=%d height=%d data=%p\n",
430 t->image[face][hwlevel].width,
431 t->image[face][hwlevel].height,
432 t->image[face][hwlevel].data);
433 exit(1);
434 }
435 }
436
437 /**
438 * Upload the texture images associated with texture \a t. This might
439 * require the allocation of texture memory.
440 *
441 * \param rmesa Context pointer
442 * \param t Texture to be uploaded
443 * \param face Cube map face to be uploaded. Zero for non-cube maps.
444 */
445
446 int r300UploadTexImages(r300ContextPtr rmesa, r300TexObjPtr t, GLuint face)
447 {
448 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
449
450 if (RADEON_DEBUG & (DEBUG_TEXTURE | DEBUG_IOCTL)) {
451 fprintf(stderr, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__,
452 (void *)rmesa->radeon.glCtx, (void *)t->base.tObj,
453 t->base.totalSize, t->base.firstLevel,
454 t->base.lastLevel);
455 }
456
457 if (!t || t->base.totalSize == 0)
458 return 0;
459
460 if (RADEON_DEBUG & DEBUG_SYNC) {
461 fprintf(stderr, "%s: Syncing\n", __FUNCTION__);
462 radeonFinish(rmesa->radeon.glCtx);
463 }
464
465 LOCK_HARDWARE(&rmesa->radeon);
466
467 if (t->base.memBlock == NULL) {
468 int heap;
469
470 heap = driAllocateTexture(rmesa->texture_heaps, rmesa->nr_heaps,
471 (driTextureObject *) t);
472 if (heap == -1) {
473 UNLOCK_HARDWARE(&rmesa->radeon);
474 return -1;
475 }
476
477 /* Set the base offset of the texture image */
478 t->bufAddr = rmesa->radeon.radeonScreen->texOffset[heap]
479 + t->base.memBlock->ofs;
480 t->offset = t->bufAddr;
481
482 /* Mark this texobj as dirty on all units:
483 */
484 t->dirty_state = TEX_ALL;
485 }
486
487 /* Let the world know we've used this memory recently.
488 */
489 driUpdateTextureLRU((driTextureObject *) t);
490 UNLOCK_HARDWARE(&rmesa->radeon);
491
492 /* Upload any images that are new */
493 if (t->base.dirty_images[face]) {
494 int i;
495 for (i = 0; i < numLevels; i++) {
496 if ((t->base.
497 dirty_images[face] & (1 <<
498 (i + t->base.firstLevel))) !=
499 0) {
500 uploadSubImage(rmesa, t, i, 0, 0,
501 t->image[face][i].width,
502 t->image[face][i].height, face);
503 }
504 }
505 t->base.dirty_images[face] = 0;
506 }
507
508 if (RADEON_DEBUG & DEBUG_SYNC) {
509 fprintf(stderr, "%s: Syncing\n", __FUNCTION__);
510 radeonFinish(rmesa->radeon.glCtx);
511 }
512
513 return 0;
514 }