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 "r200_context.h"
47 #include "r200_ioctl.h"
49 #include "radeon_reg.h"
51 #include <unistd.h> /* for usleep() */
55 * Destroy any device-dependent state associated with the texture. This may
56 * include NULLing out hardware state that points to the texture.
59 r200DestroyTexObj( r200ContextPtr rmesa
, r200TexObjPtr t
)
61 if ( R200_DEBUG
& DEBUG_TEXTURE
) {
62 fprintf( stderr
, "%s( %p, %p )\n", __FUNCTION__
,
63 (void *)t
, (void *)t
->base
.tObj
);
66 if ( rmesa
!= NULL
) {
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
;
81 /* ------------------------------------------------------------
82 * Texture image conversions
86 static void r200UploadGARTClientSubImage( r200ContextPtr rmesa
,
88 struct gl_texture_image
*texImage
,
91 GLint width
, GLint height
)
93 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
94 GLuint srcPitch
, dstPitch
;
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.
103 switch ( texFormat
->TexelBytes
) {
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
;
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
;
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
;
123 t
->image
[0][hwlevel
].data
= texImage
->Data
;
124 srcOffset
= r200GartOffsetFromVirtual( rmesa
, texImage
->Data
);
126 assert( srcOffset
!= ~0 );
128 /* Don't currently need to cope with small pitches?
130 width
= texImage
->Width
;
131 height
= texImage
->Height
;
133 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
135 r200EmitBlit( rmesa
, blit_format
,
142 t
->image
[0][hwlevel
].x
+ x
,
143 t
->image
[0][hwlevel
].y
+ y
,
147 r200EmitWait( rmesa
, RADEON_WAIT_2D
);
150 static void r200UploadRectSubImage( r200ContextPtr rmesa
,
152 struct gl_texture_image
*texImage
,
154 GLint width
, GLint height
)
156 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
157 int blit_format
, dstPitch
, done
;
159 switch ( texFormat
->TexelBytes
) {
161 blit_format
= R200_CP_COLOR_FORMAT_CI8
;
164 blit_format
= R200_CP_COLOR_FORMAT_RGB565
;
167 blit_format
= R200_CP_COLOR_FORMAT_ARGB8888
;
173 t
->image
[0][0].data
= texImage
->Data
;
175 /* Currently don't need to cope with small pitches.
177 width
= texImage
->Width
;
178 height
= texImage
->Height
;
179 dstPitch
= t
->pp_txpitch
+ 32;
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.
185 t
->pp_txoffset
= r200GartOffsetFromVirtual( rmesa
, texImage
->Data
);
186 t
->pp_txpitch
= texImage
->RowStride
* texFormat
->TexelBytes
- 32;
188 if (R200_DEBUG
& DEBUG_TEXTURE
)
190 "Using GART texturing for rectangular client texture\n");
192 /* Release FB memory allocated for this image:
194 /* FIXME This may not be correct as driSwapOutTextureObject sets
195 * FIXME dirty_images. It may be fine, though.
197 if ( t
->base
.memBlock
) {
198 driSwapOutTextureObject( (driTextureObject
*) t
);
201 else if (texImage
->IsClientData
) {
202 /* Data already in GART memory, with usable pitch.
205 srcPitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
209 r200GartOffsetFromVirtual( rmesa
, texImage
->Data
),
210 dstPitch
, t
->bufAddr
,
216 /* Data not in GART memory, or bad pitch.
218 for (done
= 0; done
< height
; ) {
219 struct r200_dma_region region
;
220 int lines
= MIN2( height
- done
, RADEON_BUFFER_SIZE
/ dstPitch
);
224 src_pitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
226 tex
= (char *)texImage
->Data
+ done
* src_pitch
;
228 memset(®ion
, 0, sizeof(region
));
229 r200AllocDmaRegion( rmesa
, ®ion
, lines
* dstPitch
, 1024 );
231 /* Copy texdata to dma:
234 fprintf(stderr
, "%s: src_pitch %d dst_pitch %d\n",
235 __FUNCTION__
, src_pitch
, dstPitch
);
237 if (src_pitch
== dstPitch
) {
238 memcpy( region
.address
+ region
.start
, tex
, lines
* src_pitch
);
241 char *buf
= region
.address
+ region
.start
;
243 for (i
= 0 ; i
< lines
; i
++) {
244 memcpy( buf
, tex
, src_pitch
);
250 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
252 /* Blit to framebuffer
256 dstPitch
, GET_START( ®ion
),
257 dstPitch
| (t
->tile_bits
>> 16),
263 r200EmitWait( rmesa
, RADEON_WAIT_2D
);
265 r200ReleaseDmaRegion( rmesa
, ®ion
, __FUNCTION__
);
273 * Upload the texture image associated with texture \a t at the specified
274 * level at the address relative to \a start.
276 static void uploadSubImage( r200ContextPtr rmesa
, r200TexObjPtr t
,
278 GLint x
, GLint y
, GLint width
, GLint height
,
281 struct gl_texture_image
*texImage
= NULL
;
283 GLint imageWidth
, imageHeight
;
285 drm_radeon_texture_t tex
;
286 drm_radeon_tex_image_t tmp
;
287 const int level
= hwlevel
+ t
->base
.firstLevel
;
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
);
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__
);
303 texImage
= t
->base
.tObj
->Image
[face
][level
];
306 if ( R200_DEBUG
& DEBUG_TEXTURE
)
307 fprintf( stderr
, "%s: texImage %d is NULL!\n", __FUNCTION__
, level
);
310 if ( !texImage
->Data
) {
311 if ( R200_DEBUG
& DEBUG_TEXTURE
)
312 fprintf( stderr
, "%s: image data is NULL!\n", __FUNCTION__
);
317 if (t
->base
.tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
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
);
325 else if (texImage
->IsClientData
) {
326 if ( R200_DEBUG
& DEBUG_TEXTURE
)
327 fprintf( stderr
, "%s: image data is in GART client storage\n",
329 r200UploadGARTClientSubImage( rmesa
, t
, texImage
, hwlevel
,
330 x
, y
, width
, height
);
333 else if ( R200_DEBUG
& DEBUG_TEXTURE
)
334 fprintf( stderr
, "%s: image data is in normal memory\n",
338 imageWidth
= texImage
->Width
;
339 imageHeight
= texImage
->Height
;
341 offset
= t
->bufAddr
+ t
->base
.totalSize
/ 6 * face
;
343 if ( R200_DEBUG
& (DEBUG_TEXTURE
|DEBUG_IOCTL
) ) {
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
);
358 t
->image
[face
][hwlevel
].data
= texImage
->Data
;
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
365 * NOTE: can't do that with texture tiling. (sroland)
369 /* copy (x,y,width,height,data) */
370 memcpy( &tmp
, &t
->image
[face
][hwlevel
], sizeof(tmp
) );
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;
387 tmp
.x
= tmp
.x
>> (texImage
->TexFormat
->TexelBytes
>> 1);
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;
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
:
412 case R200_TXFORMAT_DXT23
:
413 case R200_TXFORMAT_DXT45
:
417 fprintf(stderr
, "unknown compressed tex format in uploadSubImage\n");
421 LOCK_HARDWARE( rmesa
);
423 ret
= drmCommandWriteRead( rmesa
->dri
.fd
, DRM_RADEON_TEXTURE
,
424 &tex
, sizeof(drm_radeon_texture_t
) );
426 if (R200_DEBUG
& DEBUG_IOCTL
)
427 fprintf(stderr
, "DRM_RADEON_TEXTURE: again!\n");
430 } while ( ret
&& errno
== EAGAIN
);
432 UNLOCK_HARDWARE( rmesa
);
435 fprintf( stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
436 fprintf( stderr
, " offset=0x%08x\n",
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
);
449 * Upload the texture images associated with texture \a t. This might
450 * require the allocation of texture memory.
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.
457 int r200UploadTexImages( r200ContextPtr rmesa
, r200TexObjPtr t
, GLuint face
)
459 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
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
);
467 if ( !t
|| t
->base
.totalSize
== 0 )
470 if (R200_DEBUG
& DEBUG_SYNC
) {
471 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
472 r200Finish( rmesa
->glCtx
);
475 LOCK_HARDWARE( rmesa
);
477 if ( t
->base
.memBlock
== NULL
) {
480 heap
= driAllocateTexture( rmesa
->texture_heaps
, rmesa
->nr_heaps
,
481 (driTextureObject
*) t
);
483 UNLOCK_HARDWARE( rmesa
);
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
;
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
;
497 /* Mark this texobj as dirty on all units:
499 t
->dirty_state
= TEX_ALL
;
502 /* Let the world know we've used this memory recently.
504 driUpdateTextureLRU( (driTextureObject
*) t
);
505 UNLOCK_HARDWARE( rmesa
);
507 /* Upload any images that are new */
508 if (t
->base
.dirty_images
[face
]) {
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
);
516 t
->base
.dirty_images
[face
] = 0;
520 if (R200_DEBUG
& DEBUG_SYNC
) {
521 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
522 r200Finish( rmesa
->glCtx
);