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 /* In case of for instance 8x8 texture (2x2 dxt blocks), padding after the first two blocks is
376 needed (only with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
377 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block) has 4 real pixels. Needed
378 so the kernel module reads the right amount of data. */
379 tex
.height
= (imageHeight
+ 3) / 4;
380 tex
.width
= (imageWidth
+ 3) / 4;
381 switch (t
->pp_txformat
& R200_TXFORMAT_FORMAT_MASK
) {
382 case R200_TXFORMAT_DXT1
:
385 case R200_TXFORMAT_DXT23
:
386 case R200_TXFORMAT_DXT45
:
390 fprintf(stderr
, "unknown compressed tex format in uploadSubImage\n");
395 /* copy (x,y,width,height,data) */
396 memcpy( &tmp
, &t
->image
[face
][hwlevel
], sizeof(tmp
) );
398 /* Adjust the base offset to account for the Y-offset. This is done,
399 * instead of just letting the Y-offset automatically take care of it,
400 * because it is possible, for very large textures, for the Y-offset
401 * to exceede the [-8192,+8191] range.
403 tex
.offset
+= tmp
.y
* 1024;
406 LOCK_HARDWARE( rmesa
);
408 ret
= drmCommandWriteRead( rmesa
->dri
.fd
, DRM_RADEON_TEXTURE
,
409 &tex
, sizeof(drm_radeon_texture_t
) );
411 if (R200_DEBUG
& DEBUG_IOCTL
)
412 fprintf(stderr
, "DRM_RADEON_TEXTURE: again!\n");
415 } while ( ret
&& errno
== EAGAIN
);
417 UNLOCK_HARDWARE( rmesa
);
420 fprintf( stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
421 fprintf( stderr
, " offset=0x%08x\n",
423 fprintf( stderr
, " image width=%d height=%d\n",
424 imageWidth
, imageHeight
);
425 fprintf( stderr
, " blit width=%d height=%d data=%p\n",
426 t
->image
[face
][hwlevel
].width
, t
->image
[face
][hwlevel
].height
,
427 t
->image
[face
][hwlevel
].data
);
434 * Upload the texture images associated with texture \a t. This might
435 * require the allocation of texture memory.
437 * \param rmesa Context pointer
438 * \param t Texture to be uploaded
439 * \param face Cube map face to be uploaded. Zero for non-cube maps.
442 int r200UploadTexImages( r200ContextPtr rmesa
, r200TexObjPtr t
, GLuint face
)
444 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
446 if ( R200_DEBUG
& (DEBUG_TEXTURE
|DEBUG_IOCTL
) ) {
447 fprintf( stderr
, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__
,
448 (void *)rmesa
->glCtx
, (void *)t
->base
.tObj
, t
->base
.totalSize
,
449 t
->base
.firstLevel
, t
->base
.lastLevel
);
452 if ( !t
|| t
->base
.totalSize
== 0 )
455 if (R200_DEBUG
& DEBUG_SYNC
) {
456 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
457 r200Finish( rmesa
->glCtx
);
460 LOCK_HARDWARE( rmesa
);
462 if ( t
->base
.memBlock
== NULL
) {
465 heap
= driAllocateTexture( rmesa
->texture_heaps
, rmesa
->nr_heaps
,
466 (driTextureObject
*) t
);
468 UNLOCK_HARDWARE( rmesa
);
472 /* Set the base offset of the texture image */
473 t
->bufAddr
= rmesa
->r200Screen
->texOffset
[heap
]
474 + t
->base
.memBlock
->ofs
;
475 t
->pp_txoffset
= t
->bufAddr
;
478 /* Mark this texobj as dirty on all units:
480 t
->dirty_state
= TEX_ALL
;
483 /* Let the world know we've used this memory recently.
485 driUpdateTextureLRU( (driTextureObject
*) t
);
486 UNLOCK_HARDWARE( rmesa
);
488 /* Upload any images that are new */
489 if (t
->base
.dirty_images
[face
]) {
491 for ( i
= 0 ; i
< numLevels
; i
++ ) {
492 if ( (t
->base
.dirty_images
[face
] & (1 << (i
+t
->base
.firstLevel
))) != 0 ) {
493 uploadSubImage( rmesa
, t
, i
, 0, 0, t
->image
[face
][i
].width
,
494 t
->image
[face
][i
].height
, face
);
497 t
->base
.dirty_images
[face
] = 0;
501 if (R200_DEBUG
& DEBUG_SYNC
) {
502 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
503 r200Finish( rmesa
->glCtx
);