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>
45 #include "simple_list.h"
46 #include "radeon_reg.h" /* gets definition for usleep */
47 #include "r300_context.h"
48 #include "r300_state.h"
49 #include "r300_cmdbuf.h"
50 #include "radeon_ioctl.h"
52 #include "r300_swtcl.h"
55 #include "r300_ioctl.h"
56 #include <unistd.h> /* for usleep() */
59 #include "radeon_mm.h"
63 * Destroy any device-dependent state associated with the texture. This may
64 * include NULLing out hardware state that points to the texture.
66 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
);
76 for (i
= 0; i
< rmesa
->radeon
.glCtx
->Const
.MaxTextureUnits
; i
++) {
77 if (t
== rmesa
->state
.texture
.unit
[i
].texobj
) {
78 rmesa
->state
.texture
.unit
[i
].texobj
= NULL
;
79 /* This code below is meant to shorten state
80 pushed to the hardware by not programming
83 This does not appear to be worthwhile on R300 */
85 remove_from_list(&rmesa
->hw
.tex
[i
]);
86 make_empty_list(&rmesa
->hw
.tex
[i
]);
87 remove_from_list(&rmesa
->hw
.cube
[i
]);
88 make_empty_list(&rmesa
->hw
.cube
[i
]);
95 /* ------------------------------------------------------------
96 * Texture image conversions
99 static void r300UploadGARTClientSubImage(r300ContextPtr rmesa
,
101 struct gl_texture_image
*texImage
,
104 GLint width
, GLint height
)
106 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
107 GLuint srcPitch
, dstPitch
;
112 * XXX it appears that we always upload the full image, not a subimage.
113 * I.e. x==0, y==0, width=texWidth, height=texWidth. If this is ever
114 * changed, the src pitch will have to change.
116 switch (texFormat
->TexelBytes
) {
118 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
119 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
120 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
123 blit_format
= R300_CP_COLOR_FORMAT_RGB565
;
124 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
125 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
128 blit_format
= R300_CP_COLOR_FORMAT_ARGB8888
;
129 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
130 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
134 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
135 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
136 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
142 t
->image
[0][hwlevel
].data
= texImage
->Data
;
143 srcOffset
= r300GartOffsetFromVirtual(rmesa
, texImage
->Data
);
145 assert(srcOffset
!= ~0);
147 /* Don't currently need to cope with small pitches?
149 width
= texImage
->Width
;
150 height
= texImage
->Height
;
152 if (texFormat
->TexelBytes
> 4) {
153 width
*= texFormat
->TexelBytes
;
156 r300EmitWait(rmesa
, R300_WAIT_3D
);
158 r300EmitBlit(rmesa
, blit_format
,
165 t
->image
[0][hwlevel
].x
+ x
,
166 t
->image
[0][hwlevel
].y
+ y
, width
, height
);
168 r300EmitWait(rmesa
, R300_WAIT_2D
);
171 static void r300UploadRectSubImage(r300ContextPtr rmesa
,
173 struct gl_texture_image
*texImage
,
174 GLint x
, GLint y
, GLint width
, GLint height
)
176 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
177 int blit_format
, dstPitch
, done
;
179 switch (texFormat
->TexelBytes
) {
181 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
184 blit_format
= R300_CP_COLOR_FORMAT_RGB565
;
187 blit_format
= R300_CP_COLOR_FORMAT_ARGB8888
;
191 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
197 t
->image
[0][0].data
= texImage
->Data
;
199 /* Currently don't need to cope with small pitches.
201 width
= texImage
->Width
;
202 height
= texImage
->Height
;
205 if (texFormat
->TexelBytes
> 4) {
206 width
*= texFormat
->TexelBytes
;
209 if (rmesa
->prefer_gart_client_texturing
&& texImage
->IsClientData
) {
210 /* In this case, could also use GART texturing. This is
211 * currently disabled, but has been tested & works.
213 t
->offset
= r300GartOffsetFromVirtual(rmesa
, texImage
->Data
);
214 t
->pitch
= texImage
->RowStride
* texFormat
->TexelBytes
- 32;
216 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
218 "Using GART texturing for rectangular client texture\n");
220 /* Release FB memory allocated for this image:
222 /* FIXME This may not be correct as driSwapOutTextureObject sets
223 * FIXME dirty_images. It may be fine, though.
225 if (t
->base
.memBlock
) {
226 driSwapOutTextureObject((driTextureObject
*) t
);
228 } else if (texImage
->IsClientData
) {
229 /* Data already in GART memory, with usable pitch.
232 srcPitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
236 r300GartOffsetFromVirtual(rmesa
, texImage
->Data
),
237 dstPitch
, t
->bufAddr
, 0, 0, 0, 0, width
, height
);
239 /* Data not in GART memory, or bad pitch.
241 for (done
= 0; done
< height
;) {
242 struct r300_dma_region region
;
244 MIN2(height
- done
, RADEON_BUFFER_SIZE
/ dstPitch
);
248 src_pitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
250 tex
= (char *)texImage
->Data
+ done
* src_pitch
;
252 memset(®ion
, 0, sizeof(region
));
253 r300AllocDmaRegion(rmesa
, ®ion
, lines
* dstPitch
,
256 /* Copy texdata to dma:
260 "%s: src_pitch %d dst_pitch %d\n",
261 __FUNCTION__
, src_pitch
, dstPitch
);
263 if (src_pitch
== dstPitch
) {
264 memcpy(region
.address
+ region
.start
, tex
,
267 char *buf
= region
.address
+ region
.start
;
269 for (i
= 0; i
< lines
; i
++) {
270 memcpy(buf
, tex
, src_pitch
);
276 r300EmitWait(rmesa
, R300_WAIT_3D
);
278 /* Blit to framebuffer
282 dstPitch
, GET_START(®ion
),
283 dstPitch
| (t
->tile_bits
>> 16),
284 t
->bufAddr
, 0, 0, 0, done
, width
, lines
);
286 r300EmitWait(rmesa
, R300_WAIT_2D
);
288 radeon_mm_use(rmesa
, region
.buf
->id
);
291 r300ReleaseDmaRegion(rmesa
, ®ion
, __FUNCTION__
);
298 * Upload the texture image associated with texture \a t at the specified
299 * level at the address relative to \a start.
301 static void uploadSubImage(r300ContextPtr rmesa
, r300TexObjPtr t
,
303 GLint x
, GLint y
, GLint width
, GLint height
,
306 struct gl_texture_image
*texImage
= NULL
;
308 GLint imageWidth
, imageHeight
;
310 drm_radeon_texture_t tex
;
311 drm_radeon_tex_image_t tmp
;
312 const int level
= hwlevel
+ t
->base
.firstLevel
;
314 if (RADEON_DEBUG
& DEBUG_TEXTURE
) {
316 "%s( %p, %p ) level/width/height/face = %d/%d/%d/%u\n",
317 __FUNCTION__
, (void *)t
, (void *)t
->base
.tObj
, level
,
318 width
, height
, face
);
323 /* Ensure we have a valid texture to upload */
324 if ((hwlevel
< 0) || (hwlevel
>= RADEON_MAX_TEXTURE_LEVELS
)) {
325 _mesa_problem(NULL
, "bad texture level in %s", __FUNCTION__
);
329 texImage
= t
->base
.tObj
->Image
[face
][level
];
332 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
333 fprintf(stderr
, "%s: texImage %d is NULL!\n",
334 __FUNCTION__
, level
);
337 if (!texImage
->Data
) {
338 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
339 fprintf(stderr
, "%s: image data is NULL!\n",
344 if (t
->base
.tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
346 assert(hwlevel
== 0);
347 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
348 fprintf(stderr
, "%s: image data is rectangular\n",
350 r300UploadRectSubImage(rmesa
, t
, texImage
, x
, y
, width
, height
);
352 } else if (texImage
->IsClientData
) {
353 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
355 "%s: image data is in GART client storage\n",
357 r300UploadGARTClientSubImage(rmesa
, t
, texImage
, hwlevel
, x
, y
,
360 } else if (RADEON_DEBUG
& DEBUG_TEXTURE
)
361 fprintf(stderr
, "%s: image data is in normal memory\n",
364 imageWidth
= texImage
->Width
;
365 imageHeight
= texImage
->Height
;
367 offset
= t
->bufAddr
+ t
->base
.totalSize
/ 6 * face
;
369 if (RADEON_DEBUG
& (DEBUG_TEXTURE
| DEBUG_IOCTL
)) {
372 GLint blitX
= t
->image
[face
][hwlevel
].x
;
373 GLint blitY
= t
->image
[face
][hwlevel
].y
;
374 GLint blitWidth
= t
->image
[face
][hwlevel
].width
;
375 GLint blitHeight
= t
->image
[face
][hwlevel
].height
;
376 fprintf(stderr
, " upload image: %d,%d at %d,%d\n",
377 imageWidth
, imageHeight
, imageX
, imageY
);
378 fprintf(stderr
, " upload blit: %d,%d at %d,%d\n",
379 blitWidth
, blitHeight
, blitX
, blitY
);
380 fprintf(stderr
, " blit ofs: 0x%07x level: %d/%d\n",
381 (GLuint
) offset
, hwlevel
, level
);
384 t
->image
[face
][hwlevel
].data
= texImage
->Data
;
386 /* Init the DRM_RADEON_TEXTURE command / drm_radeon_texture_t struct.
387 * NOTE: we're always use a 1KB-wide blit and I8 texture format.
388 * We used to use 1, 2 and 4-byte texels and used to use the texture
389 * width to dictate the blit width - but that won't work for compressed
391 * NOTE: can't do that with texture tiling. (sroland)
395 /* copy (x,y,width,height,data) */
396 memcpy(&tmp
, &t
->image
[face
][hwlevel
], sizeof(tmp
));
398 if (texImage
->TexFormat
->TexelBytes
> 4) {
399 const int log2TexelBytes
=
400 (3 + (texImage
->TexFormat
->TexelBytes
>> 4));
401 tex
.format
= RADEON_TXFORMAT_I8
; /* any 1-byte texel format */
403 MAX2((texImage
->Width
* texImage
->TexFormat
->TexelBytes
) /
405 tex
.height
= imageHeight
;
406 tex
.width
= imageWidth
<< log2TexelBytes
;
407 tex
.offset
+= (tmp
.x
<< log2TexelBytes
) & ~1023;
408 tmp
.x
= tmp
.x
% (1024 >> log2TexelBytes
);
409 tmp
.width
= tmp
.width
<< log2TexelBytes
;
410 } else if (texImage
->TexFormat
->TexelBytes
) {
411 /* use multi-byte upload scheme */
412 tex
.height
= imageHeight
;
413 tex
.width
= imageWidth
;
414 switch (texImage
->TexFormat
->TexelBytes
) {
416 tex
.format
= RADEON_TXFORMAT_I8
;
419 tex
.format
= RADEON_TXFORMAT_AI88
;
422 tex
.format
= RADEON_TXFORMAT_ARGB8888
;
426 MAX2((texImage
->Width
* texImage
->TexFormat
->TexelBytes
) /
428 tex
.offset
+= tmp
.x
& ~1023;
429 tmp
.x
= tmp
.x
% 1024;
431 if (t
->tile_bits
& R300_TXO_MICRO_TILE
) {
432 /* need something like "tiled coordinates" ? */
433 tmp
.y
= tmp
.x
/ (tex
.pitch
* 128) * 2;
435 tmp
.x
% (tex
.pitch
* 128) / 2 /
436 texImage
->TexFormat
->TexelBytes
;
437 tex
.pitch
|= RADEON_DST_TILE_MICRO
>> 22;
439 tmp
.x
= tmp
.x
>> (texImage
->TexFormat
->TexelBytes
>> 1);
442 if ((t
->tile_bits
& R300_TXO_MACRO_TILE
) &&
443 (texImage
->Width
* texImage
->TexFormat
->TexelBytes
>= 256)
444 && ((!(t
->tile_bits
& R300_TXO_MICRO_TILE
)
445 && (texImage
->Height
>= 8))
446 || (texImage
->Height
>= 16))) {
447 /* weird: R200 disables macro tiling if mip width is smaller than 256 bytes,
448 OR if height is smaller than 8 automatically, but if micro tiling is active
449 the limit is height 16 instead ? */
450 tex
.pitch
|= RADEON_DST_TILE_MACRO
>> 22;
454 /* In case of for instance 8x8 texture (2x2 dxt blocks),
455 padding after the first two blocks is needed (only
456 with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
457 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block)
458 has 4 real pixels. Needed so the kernel module reads
459 the right amount of data. */
460 tex
.format
= RADEON_TXFORMAT_I8
; /* any 1-byte texel format */
461 tex
.pitch
= (R300_BLIT_WIDTH_BYTES
/ 64);
462 tex
.height
= (imageHeight
+ 3) / 4;
463 tex
.width
= (imageWidth
+ 3) / 4;
464 if ((t
->format
& R300_TX_FORMAT_DXT1
) == R300_TX_FORMAT_DXT1
) {
471 LOCK_HARDWARE(&rmesa
->radeon
);
474 drmCommandWriteRead(rmesa
->radeon
.dri
.fd
,
475 DRM_RADEON_TEXTURE
, &tex
,
476 sizeof(drm_radeon_texture_t
));
478 if (RADEON_DEBUG
& DEBUG_IOCTL
)
480 "DRM_RADEON_TEXTURE: again!\n");
483 } while (ret
== -EAGAIN
);
485 UNLOCK_HARDWARE(&rmesa
->radeon
);
488 fprintf(stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
489 fprintf(stderr
, " offset=0x%08x\n", offset
);
490 fprintf(stderr
, " image width=%d height=%d\n",
491 imageWidth
, imageHeight
);
492 fprintf(stderr
, " blit width=%d height=%d data=%p\n",
493 t
->image
[face
][hwlevel
].width
,
494 t
->image
[face
][hwlevel
].height
,
495 t
->image
[face
][hwlevel
].data
);
501 * Upload the texture images associated with texture \a t. This might
502 * require the allocation of texture memory.
504 * \param rmesa Context pointer
505 * \param t Texture to be uploaded
506 * \param face Cube map face to be uploaded. Zero for non-cube maps.
509 int r300UploadTexImages(r300ContextPtr rmesa
, r300TexObjPtr t
, GLuint face
)
511 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
513 if (RADEON_DEBUG
& (DEBUG_TEXTURE
| DEBUG_IOCTL
)) {
514 fprintf(stderr
, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__
,
515 (void *)rmesa
->radeon
.glCtx
, (void *)t
->base
.tObj
,
516 t
->base
.totalSize
, t
->base
.firstLevel
,
520 if (!t
|| t
->base
.totalSize
== 0)
523 if (RADEON_DEBUG
& DEBUG_SYNC
) {
524 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
525 radeonFinish(rmesa
->radeon
.glCtx
);
528 LOCK_HARDWARE(&rmesa
->radeon
);
530 if (t
->base
.memBlock
== NULL
) {
533 heap
= driAllocateTexture(rmesa
->texture_heaps
, rmesa
->nr_heaps
,
534 (driTextureObject
*) t
);
536 UNLOCK_HARDWARE(&rmesa
->radeon
);
540 /* Set the base offset of the texture image */
541 t
->bufAddr
= rmesa
->radeon
.radeonScreen
->texOffset
[heap
]
542 + t
->base
.memBlock
->ofs
;
543 t
->offset
= t
->bufAddr
;
545 if (!(t
->base
.tObj
->Image
[0][0]->IsClientData
)) {
546 /* hope it's safe to add that here... */
547 t
->offset
|= t
->tile_bits
;
550 /* Mark this texobj as dirty on all units:
552 t
->dirty_state
= TEX_ALL
;
555 /* Let the world know we've used this memory recently.
557 driUpdateTextureLRU((driTextureObject
*) t
);
558 UNLOCK_HARDWARE(&rmesa
->radeon
);
560 /* Upload any images that are new */
561 if (t
->base
.dirty_images
[face
]) {
563 for (i
= 0; i
< numLevels
; i
++) {
565 dirty_images
[face
] & (1 <<
566 (i
+ t
->base
.firstLevel
))) !=
568 uploadSubImage(rmesa
, t
, i
, 0, 0,
569 t
->image
[face
][i
].width
,
570 t
->image
[face
][i
].height
, face
);
573 t
->base
.dirty_images
[face
] = 0;
576 if (RADEON_DEBUG
& DEBUG_SYNC
) {
577 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
578 radeonFinish(rmesa
->radeon
.glCtx
);