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>
41 #include "main/glheader.h"
42 #include "main/imports.h"
43 #include "main/context.h"
44 #include "main/colormac.h"
45 #include "main/macros.h"
46 #include "main/simple_list.h"
47 #include "main/texobj.h"
48 #include "radeon_reg.h" /* gets definition for usleep */
49 #include "r300_context.h"
50 #include "r300_state.h"
51 #include "r300_cmdbuf.h"
52 #include "radeon_ioctl.h"
54 #include "r300_ioctl.h"
55 #include <unistd.h> /* for usleep() */
62 * Destroy any device-dependent state associated with the texture. This may
63 * include NULLing out hardware state that points to the texture.
65 void r300DestroyTexObj(r300ContextPtr rmesa
, r300TexObjPtr t
)
69 if (RADEON_DEBUG
& DEBUG_TEXTURE
) {
70 fprintf(stderr
, "%s( %p, %p )\n", __FUNCTION__
,
71 (void *)t
, (void *)t
->base
.tObj
);
74 for (i
= 0; i
< rmesa
->radeon
.glCtx
->Const
.MaxTextureUnits
; i
++) {
75 if (rmesa
->state
.texture
.unit
[i
].texobj
== t
->base
.tObj
) {
76 _mesa_reference_texobj(&rmesa
->state
.texture
.unit
[i
].texobj
, NULL
);
81 /* ------------------------------------------------------------
82 * Texture image conversions
85 static void r300UploadGARTClientSubImage(r300ContextPtr 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
= R300_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
= R300_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
= R300_CP_COLOR_FORMAT_ARGB8888
;
115 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
116 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
120 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
121 srcPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
122 dstPitch
= t
->image
[0][0].width
* texFormat
->TexelBytes
;
128 t
->image
[0][hwlevel
].data
= texImage
->Data
;
129 srcOffset
= r300GartOffsetFromVirtual(rmesa
, texImage
->Data
);
131 assert(srcOffset
!= ~0);
133 /* Don't currently need to cope with small pitches?
135 width
= texImage
->Width
;
136 height
= texImage
->Height
;
138 if (texFormat
->TexelBytes
> 4) {
139 width
*= texFormat
->TexelBytes
;
142 r300EmitWait(rmesa
, R300_WAIT_3D
);
144 r300EmitBlit(rmesa
, blit_format
,
151 t
->image
[0][hwlevel
].x
+ x
,
152 t
->image
[0][hwlevel
].y
+ y
, width
, height
);
154 r300EmitWait(rmesa
, R300_WAIT_2D
);
157 static void r300UploadRectSubImage(r300ContextPtr rmesa
,
159 struct gl_texture_image
*texImage
,
160 GLint x
, GLint y
, GLint width
, GLint height
)
162 const struct gl_texture_format
*texFormat
= texImage
->TexFormat
;
163 int blit_format
, dstPitch
, done
;
165 switch (texFormat
->TexelBytes
) {
167 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
170 blit_format
= R300_CP_COLOR_FORMAT_RGB565
;
173 blit_format
= R300_CP_COLOR_FORMAT_ARGB8888
;
177 blit_format
= R300_CP_COLOR_FORMAT_CI8
;
183 t
->image
[0][0].data
= texImage
->Data
;
185 /* Currently don't need to cope with small pitches.
187 width
= texImage
->Width
;
188 height
= texImage
->Height
;
191 if (texFormat
->TexelBytes
> 4) {
192 width
*= texFormat
->TexelBytes
;
195 if (rmesa
->prefer_gart_client_texturing
&& texImage
->IsClientData
) {
196 /* In this case, could also use GART texturing. This is
197 * currently disabled, but has been tested & works.
199 t
->offset
= r300GartOffsetFromVirtual(rmesa
, texImage
->Data
);
200 t
->pitch
= texImage
->RowStride
* texFormat
->TexelBytes
- 32;
202 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
204 "Using GART texturing for rectangular client texture\n");
206 /* Release FB memory allocated for this image:
208 /* FIXME This may not be correct as driSwapOutTextureObject sets
209 * FIXME dirty_images. It may be fine, though.
211 if (t
->base
.memBlock
) {
212 driSwapOutTextureObject((driTextureObject
*) t
);
214 } else if (texImage
->IsClientData
) {
215 /* Data already in GART memory, with usable pitch.
218 srcPitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
222 r300GartOffsetFromVirtual(rmesa
, texImage
->Data
),
223 dstPitch
, t
->bufAddr
, 0, 0, 0, 0, width
, height
);
225 /* Data not in GART memory, or bad pitch.
227 for (done
= 0; done
< height
;) {
228 struct r300_dma_region region
;
230 MIN2(height
- done
, RADEON_BUFFER_SIZE
/ dstPitch
);
234 src_pitch
= texImage
->RowStride
* texFormat
->TexelBytes
;
236 tex
= (char *)texImage
->Data
+ done
* src_pitch
;
238 memset(®ion
, 0, sizeof(region
));
239 r300AllocDmaRegion(rmesa
, ®ion
, lines
* dstPitch
,
242 /* Copy texdata to dma:
244 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
246 "%s: src_pitch %d dst_pitch %d\n",
247 __FUNCTION__
, src_pitch
, dstPitch
);
249 if (src_pitch
== dstPitch
) {
250 memcpy(region
.address
+ region
.start
, tex
,
253 char *buf
= region
.address
+ region
.start
;
255 for (i
= 0; i
< lines
; i
++) {
256 memcpy(buf
, tex
, src_pitch
);
262 r300EmitWait(rmesa
, R300_WAIT_3D
);
264 /* Blit to framebuffer
268 dstPitch
, GET_START(®ion
),
269 dstPitch
| (t
->tile_bits
>> 16),
270 t
->bufAddr
, 0, 0, 0, done
, width
, lines
);
272 r300EmitWait(rmesa
, R300_WAIT_2D
);
274 r300_mem_use(rmesa
, region
.buf
->id
);
277 r300ReleaseDmaRegion(rmesa
, ®ion
, __FUNCTION__
);
284 * Upload the texture image associated with texture \a t at the specified
285 * level at the address relative to \a start.
287 static void r300UploadSubImage(r300ContextPtr rmesa
, r300TexObjPtr t
,
289 GLint x
, GLint y
, GLint width
, GLint height
,
292 struct gl_texture_image
*texImage
= NULL
;
294 GLint imageWidth
, imageHeight
;
296 drm_radeon_texture_t tex
;
297 drm_radeon_tex_image_t tmp
;
298 const int level
= hwlevel
+ t
->base
.firstLevel
;
300 if (RADEON_DEBUG
& DEBUG_TEXTURE
) {
302 "%s( %p, %p ) level/width/height/face = %d/%d/%d/%u\n",
303 __FUNCTION__
, (void *)t
, (void *)t
->base
.tObj
, level
,
304 width
, height
, face
);
309 /* Ensure we have a valid texture to upload */
310 if ((hwlevel
< 0) || (hwlevel
>= R300_MAX_TEXTURE_LEVELS
)) {
311 _mesa_problem(NULL
, "bad texture level in %s", __FUNCTION__
);
315 texImage
= t
->base
.tObj
->Image
[face
][level
];
318 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
319 fprintf(stderr
, "%s: texImage %d is NULL!\n",
320 __FUNCTION__
, level
);
323 if (!texImage
->Data
) {
324 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
325 fprintf(stderr
, "%s: image data is NULL!\n",
330 if (t
->base
.tObj
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
332 assert(hwlevel
== 0);
333 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
334 fprintf(stderr
, "%s: image data is rectangular\n",
336 r300UploadRectSubImage(rmesa
, t
, texImage
, x
, y
, width
, height
);
338 } else if (texImage
->IsClientData
) {
339 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
341 "%s: image data is in GART client storage\n",
343 r300UploadGARTClientSubImage(rmesa
, t
, texImage
, hwlevel
, x
, y
,
346 } else if (RADEON_DEBUG
& DEBUG_TEXTURE
)
347 fprintf(stderr
, "%s: image data is in normal memory\n",
350 imageWidth
= texImage
->Width
;
351 imageHeight
= texImage
->Height
;
355 if (RADEON_DEBUG
& (DEBUG_TEXTURE
| DEBUG_IOCTL
)) {
358 GLint blitX
= t
->image
[face
][hwlevel
].x
;
359 GLint blitY
= t
->image
[face
][hwlevel
].y
;
360 GLint blitWidth
= t
->image
[face
][hwlevel
].width
;
361 GLint blitHeight
= t
->image
[face
][hwlevel
].height
;
362 fprintf(stderr
, " upload image: %d,%d at %d,%d\n",
363 imageWidth
, imageHeight
, imageX
, imageY
);
364 fprintf(stderr
, " upload blit: %d,%d at %d,%d\n",
365 blitWidth
, blitHeight
, blitX
, blitY
);
366 fprintf(stderr
, " blit ofs: 0x%07x level: %d/%d\n",
367 (GLuint
) offset
, hwlevel
, level
);
370 t
->image
[face
][hwlevel
].data
= texImage
->Data
;
372 /* Init the DRM_RADEON_TEXTURE command / drm_radeon_texture_t struct.
373 * NOTE: we're always use a 1KB-wide blit and I8 texture format.
374 * We used to use 1, 2 and 4-byte texels and used to use the texture
375 * width to dictate the blit width - but that won't work for compressed
377 * NOTE: can't do that with texture tiling. (sroland)
381 /* copy (x,y,width,height,data) */
382 memcpy(&tmp
, &t
->image
[face
][hwlevel
], sizeof(tmp
));
384 if (texImage
->TexFormat
->TexelBytes
> 4) {
385 const int log2TexelBytes
=
386 (3 + (texImage
->TexFormat
->TexelBytes
>> 4));
387 tex
.format
= RADEON_TXFORMAT_I8
; /* any 1-byte texel format */
389 MAX2((texImage
->Width
* texImage
->TexFormat
->TexelBytes
) /
391 tex
.height
= imageHeight
;
392 tex
.width
= imageWidth
<< log2TexelBytes
;
393 tex
.offset
+= (tmp
.x
<< log2TexelBytes
) & ~1023;
394 tmp
.x
= tmp
.x
% (1024 >> log2TexelBytes
);
395 tmp
.width
= tmp
.width
<< log2TexelBytes
;
396 } else if (texImage
->TexFormat
->TexelBytes
) {
397 /* use multi-byte upload scheme */
398 tex
.height
= imageHeight
;
399 tex
.width
= imageWidth
;
400 switch (texImage
->TexFormat
->TexelBytes
) {
402 tex
.format
= RADEON_TXFORMAT_I8
;
405 tex
.format
= RADEON_TXFORMAT_AI88
;
408 tex
.format
= RADEON_TXFORMAT_ARGB8888
;
412 MAX2((texImage
->Width
* texImage
->TexFormat
->TexelBytes
) /
414 tex
.offset
+= tmp
.x
& ~1023;
415 tmp
.x
= tmp
.x
% 1024;
417 if (t
->tile_bits
& R300_TXO_MICRO_TILE
) {
418 /* need something like "tiled coordinates" ? */
419 tmp
.y
= tmp
.x
/ (tex
.pitch
* 128) * 2;
421 tmp
.x
% (tex
.pitch
* 128) / 2 /
422 texImage
->TexFormat
->TexelBytes
;
423 tex
.pitch
|= RADEON_DST_TILE_MICRO
>> 22;
425 tmp
.x
= tmp
.x
>> (texImage
->TexFormat
->TexelBytes
>> 1);
428 if ((t
->tile_bits
& R300_TXO_MACRO_TILE
) &&
429 (texImage
->Width
* texImage
->TexFormat
->TexelBytes
>= 256)
430 && ((!(t
->tile_bits
& R300_TXO_MICRO_TILE
)
431 && (texImage
->Height
>= 8))
432 || (texImage
->Height
>= 16))) {
433 /* weird: R200 disables macro tiling if mip width is smaller than 256 bytes,
434 OR if height is smaller than 8 automatically, but if micro tiling is active
435 the limit is height 16 instead ? */
436 tex
.pitch
|= RADEON_DST_TILE_MACRO
>> 22;
440 /* In case of for instance 8x8 texture (2x2 dxt blocks),
441 padding after the first two blocks is needed (only
442 with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
443 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block)
444 has 4 real pixels. Needed so the kernel module reads
445 the right amount of data. */
446 tex
.format
= RADEON_TXFORMAT_I8
; /* any 1-byte texel format */
447 tex
.pitch
= (R300_BLIT_WIDTH_BYTES
/ 64);
448 tex
.height
= (imageHeight
+ 3) / 4;
449 tex
.width
= (imageWidth
+ 3) / 4;
450 if ((t
->format
& R300_TX_FORMAT_DXT1
) == R300_TX_FORMAT_DXT1
) {
457 LOCK_HARDWARE(&rmesa
->radeon
);
460 drmCommandWriteRead(rmesa
->radeon
.dri
.fd
,
461 DRM_RADEON_TEXTURE
, &tex
,
462 sizeof(drm_radeon_texture_t
));
464 if (RADEON_DEBUG
& DEBUG_IOCTL
)
466 "DRM_RADEON_TEXTURE: again!\n");
469 } while (ret
== -EAGAIN
);
471 UNLOCK_HARDWARE(&rmesa
->radeon
);
474 fprintf(stderr
, "DRM_RADEON_TEXTURE: return = %d\n", ret
);
475 fprintf(stderr
, " offset=0x%08x\n", offset
);
476 fprintf(stderr
, " image width=%d height=%d\n",
477 imageWidth
, imageHeight
);
478 fprintf(stderr
, " blit width=%d height=%d data=%p\n",
479 t
->image
[face
][hwlevel
].width
,
480 t
->image
[face
][hwlevel
].height
,
481 t
->image
[face
][hwlevel
].data
);
487 * Upload the texture images associated with texture \a t. This might
488 * require the allocation of texture memory.
490 * \param rmesa Context pointer
491 * \param t Texture to be uploaded
492 * \param face Cube map face to be uploaded. Zero for non-cube maps.
495 int r300UploadTexImages(r300ContextPtr rmesa
, r300TexObjPtr t
, GLuint face
)
497 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
499 if (t
->image_override
)
502 if (RADEON_DEBUG
& (DEBUG_TEXTURE
| DEBUG_IOCTL
)) {
503 fprintf(stderr
, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__
,
504 (void *)rmesa
->radeon
.glCtx
, (void *)t
->base
.tObj
,
505 t
->base
.totalSize
, t
->base
.firstLevel
,
509 if (t
->base
.totalSize
== 0)
512 if (RADEON_DEBUG
& DEBUG_SYNC
) {
513 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
514 radeonFinish(rmesa
->radeon
.glCtx
);
517 LOCK_HARDWARE(&rmesa
->radeon
);
519 if (t
->base
.memBlock
== NULL
) {
522 heap
= driAllocateTexture(rmesa
->texture_heaps
, rmesa
->nr_heaps
,
523 (driTextureObject
*) t
);
525 UNLOCK_HARDWARE(&rmesa
->radeon
);
529 /* Set the base offset of the texture image */
530 t
->bufAddr
= rmesa
->radeon
.radeonScreen
->texOffset
[heap
]
531 + t
->base
.memBlock
->ofs
;
532 t
->offset
= t
->bufAddr
;
534 if (!(t
->base
.tObj
->Image
[0][0]->IsClientData
)) {
535 /* hope it's safe to add that here... */
536 t
->offset
|= t
->tile_bits
;
540 /* Let the world know we've used this memory recently.
542 driUpdateTextureLRU((driTextureObject
*) t
);
543 UNLOCK_HARDWARE(&rmesa
->radeon
);
545 /* Upload any images that are new */
546 if (t
->base
.dirty_images
[face
]) {
548 for (i
= 0; i
< numLevels
; i
++) {
550 dirty_images
[face
] & (1 <<
551 (i
+ t
->base
.firstLevel
))) !=
553 r300UploadSubImage(rmesa
, t
, i
, 0, 0,
554 t
->image
[face
][i
].width
,
555 t
->image
[face
][i
].height
,
559 t
->base
.dirty_images
[face
] = 0;
562 if (RADEON_DEBUG
& DEBUG_SYNC
) {
563 fprintf(stderr
, "%s: Syncing\n", __FUNCTION__
);
564 radeonFinish(rmesa
->radeon
.glCtx
);