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