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