1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_texmem.c,v 1.5 2002/12/17 00:32:56 dawes Exp $ */
2 /**************************************************************************
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.
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:
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.
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
30 **************************************************************************/
34 * Kevin E. Martin <martin@valinux.com>
35 * Gareth Hughes <gareth@valinux.com>
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"
53 #include <unistd.h> /* for usleep() */
57 * Destroy any device-dependent state associated with the texture. This may
58 * include NULLing out hardware state that points to the texture.
61 r200DestroyTexObj( r200ContextPtr rmesa
, r200TexObjPtr t
)
63 if ( R200_DEBUG
& DEBUG_TEXTURE
) {
64 fprintf( stderr
, "%s( %p, %p )\n", __FUNCTION__
,
65 (void *)t
, (void *)t
->base
.tObj
);
68 if ( rmesa
!= NULL
) {
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
;
83 /* ------------------------------------------------------------
84 * Texture image conversions
88 static void r200UploadGARTClientSubImage( r200ContextPtr rmesa
,
90 struct gl_texture_image
*texImage
,
93 GLint width
, GLint height
)
95 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
96 GLuint srcPitch
, dstPitch
;
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.
105 switch ( texFormat
->TexelBytes
) {
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
;
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
;
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
;
125 t
->image
[0][hwlevel
].data
= texImage
->Data
;
126 srcOffset
= r200GartOffsetFromVirtual( rmesa
, texImage
->Data
);
128 assert( srcOffset
!= ~0 );
130 /* Don't currently need to cope with small pitches?
132 width
= texImage
->Width
;
133 height
= texImage
->Height
;
135 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
137 r200EmitBlit( rmesa
, blit_format
,
144 t
->image
[0][hwlevel
].x
+ x
,
145 t
->image
[0][hwlevel
].y
+ y
,
149 r200EmitWait( rmesa
, RADEON_WAIT_2D
);
152 static void r200UploadRectSubImage( r200ContextPtr rmesa
,
154 struct gl_texture_image
*texImage
,
156 GLint width
, GLint height
)
158 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
159 int blit_format
, dstPitch
, done
;
161 switch ( texFormat
->TexelBytes
) {
163 blit_format
= R200_CP_COLOR_FORMAT_CI8
;
166 blit_format
= R200_CP_COLOR_FORMAT_RGB565
;
169 blit_format
= R200_CP_COLOR_FORMAT_ARGB8888
;
175 t
->image
[0][0].data
= texImage
->Data
;
177 /* Currently don't need to cope with small pitches.
179 width
= texImage
->Width
;
180 height
= texImage
->Height
;
181 dstPitch
= t
->pp_txpitch
+ 32;
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.
187 t
->pp_txoffset
= r200GartOffsetFromVirtual( rmesa
, texImage
->Data
);
188 t
->pp_txpitch
= texImage
->RowStride
* texFormat
->TexelBytes
- 32;
190 if (R200_DEBUG
& DEBUG_TEXTURE
)
192 "Using GART texturing for rectangular client texture\n");
194 /* Release FB memory allocated for this image:
196 /* FIXME This may not be correct as driSwapOutTextureObject sets
197 * FIXME dirty_images. It may be fine, though.
199 if ( t
->base
.memBlock
) {
200 driSwapOutTextureObject( (driTextureObject
*) t
);
203 else if (texImage
->IsClientData
) {
204 /* Data already in GART memory, with usable pitch.
207 srcPitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
211 r200GartOffsetFromVirtual( rmesa
, texImage
->Data
),
212 dstPitch
, t
->bufAddr
,
218 /* Data not in GART memory, or bad pitch.
220 for (done
= 0; done
< height
; ) {
221 struct r200_dma_region region
;
222 int lines
= MIN2( height
- done
, RADEON_BUFFER_SIZE
/ dstPitch
);
226 src_pitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
228 tex
= (char *)texImage
->Data
+ done
* src_pitch
;
230 memset(®ion
, 0, sizeof(region
));
231 r200AllocDmaRegion( rmesa
, ®ion
, lines
* dstPitch
, 1024 );
233 /* Copy texdata to dma:
236 fprintf(stderr
, "%s: src_pitch %d dst_pitch %d\n",
237 __FUNCTION__
, src_pitch
, dstPitch
);
239 if (src_pitch
== dstPitch
) {
240 memcpy( region
.address
+ region
.start
, tex
, lines
* src_pitch
);
243 char *buf
= region
.address
+ region
.start
;
245 for (i
= 0 ; i
< lines
; i
++) {
246 memcpy( buf
, tex
, src_pitch
);
252 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
254 /* Blit to framebuffer
258 dstPitch
, GET_START( ®ion
),
259 dstPitch
, t
->bufAddr
,
264 r200EmitWait( rmesa
, RADEON_WAIT_2D
);
266 r200ReleaseDmaRegion( rmesa
, ®ion
, __FUNCTION__
);
274 * Upload the texture image associated with texture \a t at the specified
275 * level at the address relative to \a start.
277 static void uploadSubImage( r200ContextPtr rmesa
, r200TexObjPtr t
,
279 GLint x
, GLint y
, GLint width
, GLint height
,
282 struct gl_texture_image
*texImage
= NULL
;
284 GLint imageWidth
, imageHeight
;
286 drm_radeon_texture_t tex
;
287 drm_radeon_tex_image_t tmp
;
288 const int level
= hwlevel
+ t
->base
.firstLevel
;
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
);
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__
);
304 texImage
= t
->base
.tObj
->Image
[face
][level
];
307 if ( R200_DEBUG
& DEBUG_TEXTURE
)
308 fprintf( stderr
, "%s: texImage %d is NULL!\n", __FUNCTION__
, level
);
311 if ( !texImage
->Data
) {
312 if ( R200_DEBUG
& DEBUG_TEXTURE
)
313 fprintf( stderr
, "%s: image data is NULL!\n", __FUNCTION__
);
318 if (t
->base
.tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
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
);
326 else if (texImage
->IsClientData
) {
327 if ( R200_DEBUG
& DEBUG_TEXTURE
)
328 fprintf( stderr
, "%s: image data is in GART client storage\n",
330 r200UploadGARTClientSubImage( rmesa
, t
, texImage
, hwlevel
,
331 x
, y
, width
, height
);
334 else if ( R200_DEBUG
& DEBUG_TEXTURE
)
335 fprintf( stderr
, "%s: image data is in normal memory\n",
339 imageWidth
= texImage
->Width
;
340 imageHeight
= texImage
->Height
;
344 if ( R200_DEBUG
& (DEBUG_TEXTURE
|DEBUG_IOCTL
) ) {
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
);
359 t
->image
[face
][hwlevel
].data
= texImage
->Data
;
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
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
;
375 tex
.width
= imageWidth
; /* compressed */
376 tex
.height
= imageHeight
;
382 /* copy (x,y,width,height,data) */
383 memcpy( &tmp
, &t
->image
[face
][hwlevel
], sizeof(tmp
) );
385 LOCK_HARDWARE( rmesa
);
387 ret
= drmCommandWriteRead( rmesa
->dri
.fd
, DRM_RADEON_TEXTURE
,
388 &tex
, sizeof(drm_radeon_texture_t
) );
390 if (R200_DEBUG
& DEBUG_IOCTL
)
391 fprintf(stderr
, "DRM_RADEON_TEXTURE: again!\n");
394 } while ( ret
&& errno
== EAGAIN
);
396 UNLOCK_HARDWARE( rmesa
);
399 fprintf( stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
400 fprintf( stderr
, " offset=0x%08x\n",
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
);
413 * Upload the texture images associated with texture \a t. This might
414 * require the allocation of texture memory.
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.
421 int r200UploadTexImages( r200ContextPtr rmesa
, r200TexObjPtr t
, GLuint face
)
423 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
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
);
431 if ( !t
|| t
->base
.totalSize
== 0 )
434 if (R200_DEBUG
& DEBUG_SYNC
) {
435 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
436 r200Finish( rmesa
->glCtx
);
439 LOCK_HARDWARE( rmesa
);
441 if ( t
->base
.memBlock
== NULL
) {
444 heap
= driAllocateTexture( rmesa
->texture_heaps
, rmesa
->nr_heaps
,
445 (driTextureObject
*) t
);
447 UNLOCK_HARDWARE( rmesa
);
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
;
457 /* Mark this texobj as dirty on all units:
459 t
->dirty_state
= TEX_ALL
;
462 /* Let the world know we've used this memory recently.
464 driUpdateTextureLRU( (driTextureObject
*) t
);
465 UNLOCK_HARDWARE( rmesa
);
467 /* Upload any images that are new */
468 if (t
->base
.dirty_images
[face
]) {
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
);
476 t
->base
.dirty_images
[face
] = 0;
480 if (R200_DEBUG
& DEBUG_SYNC
) {
481 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
482 r200Finish( rmesa
->glCtx
);