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 **************************************************************************/
34 * \author Gareth Hughes <gareth@valinux.com>
36 * \author Kevin E. Martin <martin@valinux.com>
46 #include "simple_list.h"
47 #include "radeon_reg.h" /* gets definition for usleep */
48 #include "r300_context.h"
49 #include "r300_state.h"
50 #include "r300_cmdbuf.h"
51 #include "radeon_ioctl.h"
53 #include "r300_ioctl.h"
54 #include <unistd.h> /* for usleep() */
61 * Destroy any device-dependent state associated with the texture. This may
62 * include NULLing out hardware state that points to the texture.
64 void r300DestroyTexObj(r300ContextPtr rmesa
, r300TexObjPtr t
)
68 if (RADEON_DEBUG
& DEBUG_TEXTURE
) {
69 fprintf(stderr
, "%s( %p, %p )\n", __FUNCTION__
,
70 (void *)t
, (void *)t
->base
.tObj
);
73 for (i
= 0; i
< rmesa
->radeon
.glCtx
->Const
.MaxTextureUnits
; i
++) {
74 if (rmesa
->state
.texture
.unit
[i
].texobj
== t
) {
75 rmesa
->state
.texture
.unit
[i
].texobj
= NULL
;
80 /* ------------------------------------------------------------
81 * Texture image conversions
84 static void r300UploadGARTClientSubImage(r300ContextPtr rmesa
,
86 struct gl_texture_image
*texImage
,
89 GLint width
, GLint height
)
91 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
92 GLuint srcPitch
, dstPitch
;
97 * XXX it appears that we always upload the full image, not a subimage.
98 * I.e. x==0, y==0, width=texWidth, height=texWidth. If this is ever
99 * changed, the src pitch will have to change.
101 switch (texFormat
->TexelBytes
) {
103 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
104 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
105 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
108 blit_format
= R300_CP_COLOR_FORMAT_RGB565
;
109 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
110 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
113 blit_format
= R300_CP_COLOR_FORMAT_ARGB8888
;
114 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
115 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
119 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
120 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
121 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
127 t
->image
[0][hwlevel
].data
= texImage
->Data
;
128 srcOffset
= r300GartOffsetFromVirtual(rmesa
, texImage
->Data
);
130 assert(srcOffset
!= ~0);
132 /* Don't currently need to cope with small pitches?
134 width
= texImage
->Width
;
135 height
= texImage
->Height
;
137 if (texFormat
->TexelBytes
> 4) {
138 width
*= texFormat
->TexelBytes
;
141 r300EmitWait(rmesa
, R300_WAIT_3D
);
143 r300EmitBlit(rmesa
, blit_format
,
150 t
->image
[0][hwlevel
].x
+ x
,
151 t
->image
[0][hwlevel
].y
+ y
, width
, height
);
153 r300EmitWait(rmesa
, R300_WAIT_2D
);
156 static void r300UploadRectSubImage(r300ContextPtr rmesa
,
158 struct gl_texture_image
*texImage
,
159 GLint x
, GLint y
, GLint width
, GLint height
)
161 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
162 int blit_format
, dstPitch
, done
;
164 switch (texFormat
->TexelBytes
) {
166 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
169 blit_format
= R300_CP_COLOR_FORMAT_RGB565
;
172 blit_format
= R300_CP_COLOR_FORMAT_ARGB8888
;
176 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
182 t
->image
[0][0].data
= texImage
->Data
;
184 /* Currently don't need to cope with small pitches.
186 width
= texImage
->Width
;
187 height
= texImage
->Height
;
190 if (texFormat
->TexelBytes
> 4) {
191 width
*= texFormat
->TexelBytes
;
194 if (rmesa
->prefer_gart_client_texturing
&& texImage
->IsClientData
) {
195 /* In this case, could also use GART texturing. This is
196 * currently disabled, but has been tested & works.
198 t
->offset
= r300GartOffsetFromVirtual(rmesa
, texImage
->Data
);
199 t
->pitch
= texImage
->RowStride
* texFormat
->TexelBytes
- 32;
201 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
203 "Using GART texturing for rectangular client texture\n");
205 /* Release FB memory allocated for this image:
207 /* FIXME This may not be correct as driSwapOutTextureObject sets
208 * FIXME dirty_images. It may be fine, though.
210 if (t
->base
.memBlock
) {
211 driSwapOutTextureObject((driTextureObject
*) t
);
213 } else if (texImage
->IsClientData
) {
214 /* Data already in GART memory, with usable pitch.
217 srcPitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
221 r300GartOffsetFromVirtual(rmesa
, texImage
->Data
),
222 dstPitch
, t
->bufAddr
, 0, 0, 0, 0, width
, height
);
224 /* Data not in GART memory, or bad pitch.
226 for (done
= 0; done
< height
;) {
227 struct r300_dma_region region
;
229 MIN2(height
- done
, RADEON_BUFFER_SIZE
/ dstPitch
);
233 src_pitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
235 tex
= (char *)texImage
->Data
+ done
* src_pitch
;
237 memset(®ion
, 0, sizeof(region
));
238 r300AllocDmaRegion(rmesa
, ®ion
, lines
* dstPitch
,
241 /* Copy texdata to dma:
243 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
245 "%s: src_pitch %d dst_pitch %d\n",
246 __FUNCTION__
, src_pitch
, dstPitch
);
248 if (src_pitch
== dstPitch
) {
249 memcpy(region
.address
+ region
.start
, tex
,
252 char *buf
= region
.address
+ region
.start
;
254 for (i
= 0; i
< lines
; i
++) {
255 memcpy(buf
, tex
, src_pitch
);
261 r300EmitWait(rmesa
, R300_WAIT_3D
);
263 /* Blit to framebuffer
267 dstPitch
, GET_START(®ion
),
268 dstPitch
| (t
->tile_bits
>> 16),
269 t
->bufAddr
, 0, 0, 0, done
, width
, lines
);
271 r300EmitWait(rmesa
, R300_WAIT_2D
);
273 r300_mem_use(rmesa
, region
.buf
->id
);
276 r300ReleaseDmaRegion(rmesa
, ®ion
, __FUNCTION__
);
283 * Upload the texture image associated with texture \a t at the specified
284 * level at the address relative to \a start.
286 static void r300UploadSubImage(r300ContextPtr rmesa
, r300TexObjPtr t
,
288 GLint x
, GLint y
, GLint width
, GLint height
,
291 struct gl_texture_image
*texImage
= NULL
;
293 GLint imageWidth
, imageHeight
;
295 drm_radeon_texture_t tex
;
296 drm_radeon_tex_image_t tmp
;
297 const int level
= hwlevel
+ t
->base
.firstLevel
;
299 if (RADEON_DEBUG
& DEBUG_TEXTURE
) {
301 "%s( %p, %p ) level/width/height/face = %d/%d/%d/%u\n",
302 __FUNCTION__
, (void *)t
, (void *)t
->base
.tObj
, level
,
303 width
, height
, face
);
308 /* Ensure we have a valid texture to upload */
309 if ((hwlevel
< 0) || (hwlevel
>= RADEON_MAX_TEXTURE_LEVELS
)) {
310 _mesa_problem(NULL
, "bad texture level in %s", __FUNCTION__
);
314 texImage
= t
->base
.tObj
->Image
[face
][level
];
317 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
318 fprintf(stderr
, "%s: texImage %d is NULL!\n",
319 __FUNCTION__
, level
);
322 if (!texImage
->Data
) {
323 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
324 fprintf(stderr
, "%s: image data is NULL!\n",
329 if (t
->base
.tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
331 assert(hwlevel
== 0);
332 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
333 fprintf(stderr
, "%s: image data is rectangular\n",
335 r300UploadRectSubImage(rmesa
, t
, texImage
, x
, y
, width
, height
);
337 } else if (texImage
->IsClientData
) {
338 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
340 "%s: image data is in GART client storage\n",
342 r300UploadGARTClientSubImage(rmesa
, t
, texImage
, hwlevel
, x
, y
,
345 } else if (RADEON_DEBUG
& DEBUG_TEXTURE
)
346 fprintf(stderr
, "%s: image data is in normal memory\n",
349 imageWidth
= texImage
->Width
;
350 imageHeight
= texImage
->Height
;
352 offset
= t
->bufAddr
+ t
->base
.totalSize
/ 6 * face
;
354 if (RADEON_DEBUG
& (DEBUG_TEXTURE
| DEBUG_IOCTL
)) {
357 GLint blitX
= t
->image
[face
][hwlevel
].x
;
358 GLint blitY
= t
->image
[face
][hwlevel
].y
;
359 GLint blitWidth
= t
->image
[face
][hwlevel
].width
;
360 GLint blitHeight
= t
->image
[face
][hwlevel
].height
;
361 fprintf(stderr
, " upload image: %d,%d at %d,%d\n",
362 imageWidth
, imageHeight
, imageX
, imageY
);
363 fprintf(stderr
, " upload blit: %d,%d at %d,%d\n",
364 blitWidth
, blitHeight
, blitX
, blitY
);
365 fprintf(stderr
, " blit ofs: 0x%07x level: %d/%d\n",
366 (GLuint
) offset
, hwlevel
, level
);
369 t
->image
[face
][hwlevel
].data
= texImage
->Data
;
371 /* Init the DRM_RADEON_TEXTURE command / drm_radeon_texture_t struct.
372 * NOTE: we're always use a 1KB-wide blit and I8 texture format.
373 * We used to use 1, 2 and 4-byte texels and used to use the texture
374 * width to dictate the blit width - but that won't work for compressed
376 * NOTE: can't do that with texture tiling. (sroland)
380 /* copy (x,y,width,height,data) */
381 memcpy(&tmp
, &t
->image
[face
][hwlevel
], sizeof(tmp
));
383 if (texImage
->TexFormat
->TexelBytes
> 4) {
384 const int log2TexelBytes
=
385 (3 + (texImage
->TexFormat
->TexelBytes
>> 4));
386 tex
.format
= RADEON_TXFORMAT_I8
; /* any 1-byte texel format */
388 MAX2((texImage
->Width
* texImage
->TexFormat
->TexelBytes
) /
390 tex
.height
= imageHeight
;
391 tex
.width
= imageWidth
<< log2TexelBytes
;
392 tex
.offset
+= (tmp
.x
<< log2TexelBytes
) & ~1023;
393 tmp
.x
= tmp
.x
% (1024 >> log2TexelBytes
);
394 tmp
.width
= tmp
.width
<< log2TexelBytes
;
395 } else if (texImage
->TexFormat
->TexelBytes
) {
396 /* use multi-byte upload scheme */
397 tex
.height
= imageHeight
;
398 tex
.width
= imageWidth
;
399 switch (texImage
->TexFormat
->TexelBytes
) {
401 tex
.format
= RADEON_TXFORMAT_I8
;
404 tex
.format
= RADEON_TXFORMAT_AI88
;
407 tex
.format
= RADEON_TXFORMAT_ARGB8888
;
411 MAX2((texImage
->Width
* texImage
->TexFormat
->TexelBytes
) /
413 tex
.offset
+= tmp
.x
& ~1023;
414 tmp
.x
= tmp
.x
% 1024;
416 if (t
->tile_bits
& R300_TXO_MICRO_TILE
) {
417 /* need something like "tiled coordinates" ? */
418 tmp
.y
= tmp
.x
/ (tex
.pitch
* 128) * 2;
420 tmp
.x
% (tex
.pitch
* 128) / 2 /
421 texImage
->TexFormat
->TexelBytes
;
422 tex
.pitch
|= RADEON_DST_TILE_MICRO
>> 22;
424 tmp
.x
= tmp
.x
>> (texImage
->TexFormat
->TexelBytes
>> 1);
427 if ((t
->tile_bits
& R300_TXO_MACRO_TILE
) &&
428 (texImage
->Width
* texImage
->TexFormat
->TexelBytes
>= 256)
429 && ((!(t
->tile_bits
& R300_TXO_MICRO_TILE
)
430 && (texImage
->Height
>= 8))
431 || (texImage
->Height
>= 16))) {
432 /* weird: R200 disables macro tiling if mip width is smaller than 256 bytes,
433 OR if height is smaller than 8 automatically, but if micro tiling is active
434 the limit is height 16 instead ? */
435 tex
.pitch
|= RADEON_DST_TILE_MACRO
>> 22;
439 /* In case of for instance 8x8 texture (2x2 dxt blocks),
440 padding after the first two blocks is needed (only
441 with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
442 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block)
443 has 4 real pixels. Needed so the kernel module reads
444 the right amount of data. */
445 tex
.format
= RADEON_TXFORMAT_I8
; /* any 1-byte texel format */
446 tex
.pitch
= (R300_BLIT_WIDTH_BYTES
/ 64);
447 tex
.height
= (imageHeight
+ 3) / 4;
448 tex
.width
= (imageWidth
+ 3) / 4;
449 if ((t
->format
& R300_TX_FORMAT_DXT1
) == R300_TX_FORMAT_DXT1
) {
456 LOCK_HARDWARE(&rmesa
->radeon
);
459 drmCommandWriteRead(rmesa
->radeon
.dri
.fd
,
460 DRM_RADEON_TEXTURE
, &tex
,
461 sizeof(drm_radeon_texture_t
));
463 if (RADEON_DEBUG
& DEBUG_IOCTL
)
465 "DRM_RADEON_TEXTURE: again!\n");
468 } while (ret
== -EAGAIN
);
470 UNLOCK_HARDWARE(&rmesa
->radeon
);
473 fprintf(stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
474 fprintf(stderr
, " offset=0x%08x\n", offset
);
475 fprintf(stderr
, " image width=%d height=%d\n",
476 imageWidth
, imageHeight
);
477 fprintf(stderr
, " blit width=%d height=%d data=%p\n",
478 t
->image
[face
][hwlevel
].width
,
479 t
->image
[face
][hwlevel
].height
,
480 t
->image
[face
][hwlevel
].data
);
486 * Upload the texture images associated with texture \a t. This might
487 * require the allocation of texture memory.
489 * \param rmesa Context pointer
490 * \param t Texture to be uploaded
491 * \param face Cube map face to be uploaded. Zero for non-cube maps.
494 int r300UploadTexImages(r300ContextPtr rmesa
, r300TexObjPtr t
, GLuint face
)
496 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
498 if (t
->image_override
)
501 if (RADEON_DEBUG
& (DEBUG_TEXTURE
| DEBUG_IOCTL
)) {
502 fprintf(stderr
, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__
,
503 (void *)rmesa
->radeon
.glCtx
, (void *)t
->base
.tObj
,
504 t
->base
.totalSize
, t
->base
.firstLevel
,
508 if (!t
|| t
->base
.totalSize
== 0)
511 if (RADEON_DEBUG
& DEBUG_SYNC
) {
512 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
513 radeonFinish(rmesa
->radeon
.glCtx
);
516 LOCK_HARDWARE(&rmesa
->radeon
);
518 if (t
->base
.memBlock
== NULL
) {
521 heap
= driAllocateTexture(rmesa
->texture_heaps
, rmesa
->nr_heaps
,
522 (driTextureObject
*) t
);
524 UNLOCK_HARDWARE(&rmesa
->radeon
);
528 /* Set the base offset of the texture image */
529 t
->bufAddr
= rmesa
->radeon
.radeonScreen
->texOffset
[heap
]
530 + t
->base
.memBlock
->ofs
;
531 t
->offset
= t
->bufAddr
;
533 if (!(t
->base
.tObj
->Image
[0][0]->IsClientData
)) {
534 /* hope it's safe to add that here... */
535 t
->offset
|= t
->tile_bits
;
538 /* Mark this texobj as dirty on all units:
540 t
->dirty_state
= TEX_ALL
;
543 /* Let the world know we've used this memory recently.
545 driUpdateTextureLRU((driTextureObject
*) t
);
546 UNLOCK_HARDWARE(&rmesa
->radeon
);
548 /* Upload any images that are new */
549 if (t
->base
.dirty_images
[face
]) {
551 for (i
= 0; i
< numLevels
; i
++) {
553 dirty_images
[face
] & (1 <<
554 (i
+ t
->base
.firstLevel
))) !=
556 r300UploadSubImage(rmesa
, t
, i
, 0, 0,
557 t
->image
[face
][i
].width
,
558 t
->image
[face
][i
].height
,
562 t
->base
.dirty_images
[face
] = 0;
565 if (RADEON_DEBUG
& DEBUG_SYNC
) {
566 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
567 radeonFinish(rmesa
->radeon
.glCtx
);