i965: fix bugs in projective texture coordinates
[mesa.git] / src / mesa / drivers / dri / r200 / r200_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 "main/glheader.h"
41 #include "main/imports.h"
42 #include "main/context.h"
43 #include "main/colormac.h"
44 #include "main/macros.h"
45 #include "r200_context.h"
46 #include "r200_ioctl.h"
47 #include "r200_tex.h"
48 #include "radeon_reg.h"
49
50 #include <unistd.h> /* for usleep() */
51
52
53 /**
54 * Destroy any device-dependent state associated with the texture. This may
55 * include NULLing out hardware state that points to the texture.
56 */
57 void
58 r200DestroyTexObj( r200ContextPtr rmesa, r200TexObjPtr t )
59 {
60 if ( R200_DEBUG & DEBUG_TEXTURE ) {
61 fprintf( stderr, "%s( %p, %p )\n", __FUNCTION__,
62 (void *)t, (void *)t->base.tObj );
63 }
64
65 if ( rmesa != NULL ) {
66 unsigned i;
67
68
69 for ( i = 0 ; i < rmesa->glCtx->Const.MaxTextureUnits ; i++ ) {
70 if ( t == rmesa->state.texture.unit[i].texobj ) {
71 rmesa->state.texture.unit[i].texobj = NULL;
72 rmesa->hw.tex[i].dirty = GL_FALSE;
73 rmesa->hw.cube[i].dirty = GL_FALSE;
74 }
75 }
76 }
77 }
78
79
80 /* ------------------------------------------------------------
81 * Texture image conversions
82 */
83
84
85 static void r200UploadGARTClientSubImage( r200ContextPtr rmesa,
86 r200TexObjPtr 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 = R200_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 = R200_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 = R200_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 default:
119 return;
120 }
121
122 t->image[0][hwlevel].data = texImage->Data;
123 srcOffset = r200GartOffsetFromVirtual( rmesa, texImage->Data );
124
125 assert( srcOffset != ~0 );
126
127 /* Don't currently need to cope with small pitches?
128 */
129 width = texImage->Width;
130 height = texImage->Height;
131
132 r200EmitWait( rmesa, RADEON_WAIT_3D );
133
134 r200EmitBlit( rmesa, blit_format,
135 srcPitch,
136 srcOffset,
137 dstPitch,
138 t->bufAddr,
139 x,
140 y,
141 t->image[0][hwlevel].x + x,
142 t->image[0][hwlevel].y + y,
143 width,
144 height );
145
146 r200EmitWait( rmesa, RADEON_WAIT_2D );
147 }
148
149 static void r200UploadRectSubImage( r200ContextPtr rmesa,
150 r200TexObjPtr t,
151 struct gl_texture_image *texImage,
152 GLint x, GLint y,
153 GLint width, GLint height )
154 {
155 const struct gl_texture_format *texFormat = texImage->TexFormat;
156 int blit_format, dstPitch, done;
157
158 switch ( texFormat->TexelBytes ) {
159 case 1:
160 blit_format = R200_CP_COLOR_FORMAT_CI8;
161 break;
162 case 2:
163 blit_format = R200_CP_COLOR_FORMAT_RGB565;
164 break;
165 case 4:
166 blit_format = R200_CP_COLOR_FORMAT_ARGB8888;
167 break;
168 default:
169 return;
170 }
171
172 t->image[0][0].data = texImage->Data;
173
174 /* Currently don't need to cope with small pitches.
175 */
176 width = texImage->Width;
177 height = texImage->Height;
178 dstPitch = t->pp_txpitch + 32;
179
180 if (rmesa->prefer_gart_client_texturing && texImage->IsClientData) {
181 /* In this case, could also use GART texturing. This is
182 * currently disabled, but has been tested & works.
183 */
184 if ( !t->image_override )
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 if (tex.format == R200_TXFORMAT_ABGR8888) {
378 /* drm will refuse abgr8888 textures. */
379 tex.format = R200_TXFORMAT_ARGB8888;
380 }
381 tex.pitch = MAX2((texImage->Width * texImage->TexFormat->TexelBytes) / 64, 1);
382 tex.offset += tmp.x & ~1023;
383 tmp.x = tmp.x % 1024;
384 if (t->tile_bits & R200_TXO_MICRO_TILE) {
385 /* need something like "tiled coordinates" ? */
386 tmp.y = tmp.x / (tex.pitch * 128) * 2;
387 tmp.x = tmp.x % (tex.pitch * 128) / 2 / texImage->TexFormat->TexelBytes;
388 tex.pitch |= RADEON_DST_TILE_MICRO >> 22;
389 }
390 else {
391 tmp.x = tmp.x >> (texImage->TexFormat->TexelBytes >> 1);
392 }
393 if ((t->tile_bits & R200_TXO_MACRO_TILE) &&
394 (texImage->Width * texImage->TexFormat->TexelBytes >= 256) &&
395 ((!(t->tile_bits & R200_TXO_MICRO_TILE) && (texImage->Height >= 8)) ||
396 (texImage->Height >= 16))) {
397 /* weird: R200 disables macro tiling if mip width is smaller than 256 bytes,
398 OR if height is smaller than 8 automatically, but if micro tiling is active
399 the limit is height 16 instead ? */
400 tex.pitch |= RADEON_DST_TILE_MACRO >> 22;
401 }
402 }
403 else {
404 /* In case of for instance 8x8 texture (2x2 dxt blocks), padding after the first two blocks is
405 needed (only with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
406 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block) has 4 real pixels. Needed
407 so the kernel module reads the right amount of data. */
408 tex.format = R200_TXFORMAT_I8; /* any 1-byte texel format */
409 tex.pitch = (BLIT_WIDTH_BYTES / 64);
410 tex.height = (imageHeight + 3) / 4;
411 tex.width = (imageWidth + 3) / 4;
412 switch (t->pp_txformat & R200_TXFORMAT_FORMAT_MASK) {
413 case R200_TXFORMAT_DXT1:
414 tex.width *= 8;
415 break;
416 case R200_TXFORMAT_DXT23:
417 case R200_TXFORMAT_DXT45:
418 tex.width *= 16;
419 break;
420 default:
421 fprintf(stderr, "unknown compressed tex format in uploadSubImage\n");
422 }
423 }
424
425 LOCK_HARDWARE( rmesa );
426 do {
427 ret = drmCommandWriteRead( rmesa->dri.fd, DRM_RADEON_TEXTURE,
428 &tex, sizeof(drm_radeon_texture_t) );
429 if (ret) {
430 if (R200_DEBUG & DEBUG_IOCTL)
431 fprintf(stderr, "DRM_RADEON_TEXTURE: again!\n");
432 usleep(1);
433 }
434 } while ( ret == -EAGAIN );
435
436 UNLOCK_HARDWARE( rmesa );
437
438 if ( ret ) {
439 fprintf( stderr, "DRM_RADEON_TEXTURE: return = %d\n", ret );
440 fprintf( stderr, " offset=0x%08x\n",
441 offset );
442 fprintf( stderr, " image width=%d height=%d\n",
443 imageWidth, imageHeight );
444 fprintf( stderr, " blit width=%d height=%d data=%p\n",
445 t->image[face][hwlevel].width, t->image[face][hwlevel].height,
446 t->image[face][hwlevel].data );
447 exit( 1 );
448 }
449 }
450
451
452 /**
453 * Upload the texture images associated with texture \a t. This might
454 * require the allocation of texture memory.
455 *
456 * \param rmesa Context pointer
457 * \param t Texture to be uploaded
458 * \param face Cube map face to be uploaded. Zero for non-cube maps.
459 */
460
461 int r200UploadTexImages( r200ContextPtr rmesa, r200TexObjPtr t, GLuint face )
462 {
463 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
464
465 if ( R200_DEBUG & (DEBUG_TEXTURE|DEBUG_IOCTL) ) {
466 fprintf( stderr, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__,
467 (void *)rmesa->glCtx, (void *)t->base.tObj, t->base.totalSize,
468 t->base.firstLevel, t->base.lastLevel );
469 }
470
471 if ( !t || t->base.totalSize == 0 || t->image_override )
472 return 0;
473
474 if (R200_DEBUG & DEBUG_SYNC) {
475 fprintf(stderr, "%s: Syncing\n", __FUNCTION__ );
476 r200Finish( rmesa->glCtx );
477 }
478
479 LOCK_HARDWARE( rmesa );
480
481 if ( t->base.memBlock == NULL ) {
482 int heap;
483
484 heap = driAllocateTexture( rmesa->texture_heaps, rmesa->nr_heaps,
485 (driTextureObject *) t );
486 if ( heap == -1 ) {
487 UNLOCK_HARDWARE( rmesa );
488 return -1;
489 }
490
491 /* Set the base offset of the texture image */
492 t->bufAddr = rmesa->r200Screen->texOffset[heap]
493 + t->base.memBlock->ofs;
494 t->pp_txoffset = t->bufAddr;
495
496 if (!(t->base.tObj->Image[0][0]->IsClientData)) {
497 /* hope it's safe to add that here... */
498 t->pp_txoffset |= t->tile_bits;
499 }
500
501 /* Mark this texobj as dirty on all units:
502 */
503 t->dirty_state = TEX_ALL;
504 }
505
506 /* Let the world know we've used this memory recently.
507 */
508 driUpdateTextureLRU( (driTextureObject *) t );
509 UNLOCK_HARDWARE( rmesa );
510
511 /* Upload any images that are new */
512 if (t->base.dirty_images[face]) {
513 int i;
514 for ( i = 0 ; i < numLevels ; i++ ) {
515 if ( (t->base.dirty_images[face] & (1 << (i+t->base.firstLevel))) != 0 ) {
516 uploadSubImage( rmesa, t, i, 0, 0, t->image[face][i].width,
517 t->image[face][i].height, face );
518 }
519 }
520 t->base.dirty_images[face] = 0;
521 }
522
523
524 if (R200_DEBUG & DEBUG_SYNC) {
525 fprintf(stderr, "%s: Syncing\n", __FUNCTION__ );
526 r200Finish( rmesa->glCtx );
527 }
528
529 return 0;
530 }