1 /**************************************************************************
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.
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:
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.
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
29 **************************************************************************/
33 * Kevin E. Martin <martin@valinux.com>
34 * Gareth Hughes <gareth@valinux.com>
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"
48 #include "radeon_reg.h"
50 #include <unistd.h> /* for usleep() */
54 * Destroy any device-dependent state associated with the texture. This may
55 * include NULLing out hardware state that points to the texture.
58 r200DestroyTexObj( r200ContextPtr rmesa
, r200TexObjPtr t
)
60 if ( R200_DEBUG
& DEBUG_TEXTURE
) {
61 fprintf( stderr
, "%s( %p, %p )\n", __FUNCTION__
,
62 (void *)t
, (void *)t
->base
.tObj
);
65 if ( rmesa
!= NULL
) {
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
;
80 /* ------------------------------------------------------------
81 * Texture image conversions
85 static void r200UploadGARTClientSubImage( r200ContextPtr rmesa
,
87 struct gl_texture_image
*texImage
,
90 GLint width
, GLint height
)
92 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
93 GLuint srcPitch
, dstPitch
;
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.
102 switch ( texFormat
->TexelBytes
) {
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
;
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
;
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
;
122 t
->image
[0][hwlevel
].data
= texImage
->Data
;
123 srcOffset
= r200GartOffsetFromVirtual( rmesa
, texImage
->Data
);
125 assert( srcOffset
!= ~0 );
127 /* Don't currently need to cope with small pitches?
129 width
= texImage
->Width
;
130 height
= texImage
->Height
;
132 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
134 r200EmitBlit( rmesa
, blit_format
,
141 t
->image
[0][hwlevel
].x
+ x
,
142 t
->image
[0][hwlevel
].y
+ y
,
146 r200EmitWait( rmesa
, RADEON_WAIT_2D
);
149 static void r200UploadRectSubImage( r200ContextPtr rmesa
,
151 struct gl_texture_image
*texImage
,
153 GLint width
, GLint height
)
155 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
156 int blit_format
, dstPitch
, done
;
158 switch ( texFormat
->TexelBytes
) {
160 blit_format
= R200_CP_COLOR_FORMAT_CI8
;
163 blit_format
= R200_CP_COLOR_FORMAT_RGB565
;
166 blit_format
= R200_CP_COLOR_FORMAT_ARGB8888
;
172 t
->image
[0][0].data
= texImage
->Data
;
174 /* Currently don't need to cope with small pitches.
176 width
= texImage
->Width
;
177 height
= texImage
->Height
;
178 dstPitch
= t
->pp_txpitch
+ 32;
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.
184 if ( !t
->image_override
)
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 if (tex
.format
== R200_TXFORMAT_ABGR8888
) {
378 /* drm will refuse abgr8888 textures. */
379 tex
.format
= R200_TXFORMAT_ARGB8888
;
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;
391 tmp
.x
= tmp
.x
>> (texImage
->TexFormat
->TexelBytes
>> 1);
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;
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
:
416 case R200_TXFORMAT_DXT23
:
417 case R200_TXFORMAT_DXT45
:
421 fprintf(stderr
, "unknown compressed tex format in uploadSubImage\n");
425 LOCK_HARDWARE( rmesa
);
427 ret
= drmCommandWriteRead( rmesa
->dri
.fd
, DRM_RADEON_TEXTURE
,
428 &tex
, sizeof(drm_radeon_texture_t
) );
430 if (R200_DEBUG
& DEBUG_IOCTL
)
431 fprintf(stderr
, "DRM_RADEON_TEXTURE: again!\n");
434 } while ( ret
== -EAGAIN
);
436 UNLOCK_HARDWARE( rmesa
);
439 fprintf( stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
440 fprintf( stderr
, " offset=0x%08x\n",
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
);
453 * Upload the texture images associated with texture \a t. This might
454 * require the allocation of texture memory.
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.
461 int r200UploadTexImages( r200ContextPtr rmesa
, r200TexObjPtr t
, GLuint face
)
463 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
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
);
471 if ( !t
|| t
->base
.totalSize
== 0 || t
->image_override
)
474 if (R200_DEBUG
& DEBUG_SYNC
) {
475 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
476 r200Finish( rmesa
->glCtx
);
479 LOCK_HARDWARE( rmesa
);
481 if ( t
->base
.memBlock
== NULL
) {
484 heap
= driAllocateTexture( rmesa
->texture_heaps
, rmesa
->nr_heaps
,
485 (driTextureObject
*) t
);
487 UNLOCK_HARDWARE( rmesa
);
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
;
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
;
501 /* Mark this texobj as dirty on all units:
503 t
->dirty_state
= TEX_ALL
;
506 /* Let the world know we've used this memory recently.
508 driUpdateTextureLRU( (driTextureObject
*) t
);
509 UNLOCK_HARDWARE( rmesa
);
511 /* Upload any images that are new */
512 if (t
->base
.dirty_images
[face
]) {
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
);
520 t
->base
.dirty_images
[face
] = 0;
524 if (R200_DEBUG
& DEBUG_SYNC
) {
525 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
526 r200Finish( rmesa
->glCtx
);