1 /* $XFree86: xc/lib/GL/mesa/src/drv/r128/r128_texmem.c,v 1.1 2002/02/22 21:44:58 dawes Exp $ */
2 /**************************************************************************
4 Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 on the rights to use, copy, modify, merge, publish, distribute, sub
12 license, and/or sell copies of the Software, and to permit persons to whom
13 the Software is furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice (including the next
16 paragraph) shall be included in all copies or substantial portions of the
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * Gareth Hughes <gareth@valinux.com>
32 * Kevin E. Martin <martin@valinux.com>
33 * Brian Paul <brianp@valinux.com>
36 #include "r128_context.h"
37 #include "r128_state.h"
38 #include "r128_ioctl.h"
40 #include "r128_tris.h"
45 #include "simple_list.h"
46 #include "texformat.h"
53 /* Destroy hardware state associated with texture `t'.
55 void r128DestroyTexObj( r128ContextPtr rmesa
, r128TexObjPtr t
)
60 /* See if it was the driver's current object.
65 for ( i
= 0 ; i
< rmesa
->glCtx
->Const
.MaxTextureUnits
; i
++ )
67 if ( t
== rmesa
->CurrentTexObj
[ i
] ) {
68 assert( t
->base
.bound
& (1 << i
) );
69 rmesa
->CurrentTexObj
[ i
] = NULL
;
77 * Upload the texture image associated with texture \a t at the specified
78 * level at the address relative to \a start.
80 static void uploadSubImage( r128ContextPtr rmesa
, r128TexObjPtr t
,
82 GLint x
, GLint y
, GLint width
, GLint height
)
84 struct gl_texture_image
*image
;
85 int texelsPerDword
= 0;
86 int imageWidth
, imageHeight
;
89 int32_t pitch
, offset
;
92 /* Ensure we have a valid texture to upload */
93 if ( ( level
< 0 ) || ( level
> R128_MAX_TEXTURE_LEVELS
) )
96 image
= t
->base
.tObj
->Image
[0][level
];
100 switch ( image
->TexFormat
->TexelBytes
) {
101 case 1: texelsPerDword
= 4; break;
102 case 2: texelsPerDword
= 2; break;
103 case 4: texelsPerDword
= 1; break;
107 /* FIXME: The subimage index calcs are wrong... */
110 width
= image
->Width
;
111 height
= image
->Height
;
114 imageWidth
= image
->Width
;
115 imageHeight
= image
->Height
;
117 format
= t
->textureFormat
>> 16;
119 /* The texel upload routines have a minimum width, so force the size
122 if ( imageWidth
< texelsPerDword
) {
125 factor
= texelsPerDword
/ imageWidth
;
126 imageWidth
= texelsPerDword
;
127 imageHeight
/= factor
;
128 if ( imageHeight
== 0 ) {
129 /* In this case, the texel converter will actually walk a
130 * texel or two off the end of the image, but normal malloc
131 * alignment should prevent it from ever causing a fault.
137 /* We can't upload to a pitch less than 8 texels so we will need to
138 * linearly upload all modified rows for textures smaller than this.
139 * This makes the x/y/width/height different for the blitter and the
142 if ( imageWidth
>= 8 ) {
143 /* The texture walker and the blitter look identical */
144 pitch
= imageWidth
>> 3;
150 start
= (y
* imageWidth
) & ~7;
151 end
= (y
+ height
) * imageWidth
;
153 if ( end
- start
< 8 ) {
154 /* Handle the case where the total number of texels
162 /* Upload some number of full 8 texel blit rows */
163 factor
= 8 / imageWidth
;
174 /* Fixed pitch of 8 */
178 dwords
= width
* height
/ texelsPerDword
;
179 offset
= t
->bufAddr
+ t
->image
[level
- t
->base
.firstLevel
].offset
;
181 #if ENABLE_PERF_BOXES
182 /* Bump the performace counter */
183 rmesa
->c_textureBytes
+= (dwords
<< 2);
186 if ( R128_DEBUG
& DEBUG_VERBOSE_API
) {
187 fprintf( stderr
, "r128UploadSubImage: %d,%d of %d,%d at %d,%d\n",
188 width
, height
, image
->Width
, image
->Height
, x
, y
);
189 fprintf( stderr
, " blit ofs: 0x%07x pitch: 0x%x dwords: %d "
190 "level: %d format: %x\n",
191 (GLuint
)offset
, (GLuint
)pitch
, dwords
, level
, format
);
194 /* Subdivide the texture if required */
195 if ( dwords
<= R128_BUFFER_MAX_DWORDS
/ 2 ) {
198 rows
= (R128_BUFFER_MAX_DWORDS
* texelsPerDword
) / (2 * width
);
201 for ( i
= 0, remaining
= height
;
203 remaining
-= rows
, y
+= rows
, i
++ )
210 height
= MIN2(remaining
, rows
);
212 /* Grab the indirect buffer for the texture blit */
213 LOCK_HARDWARE( rmesa
);
214 buffer
= r128GetBufferLocked( rmesa
);
216 dst
= (int32_t *)((char *)buffer
->address
+ R128_HOSTDATA_BLIT_OFFSET
);
218 /* Copy the next chunck of the texture image into the blit buffer */
220 const GLubyte
*src
= (const GLubyte
*) image
->Data
+
221 (y
* image
->Width
+ x
) * image
->TexFormat
->TexelBytes
;
222 const GLuint bytes
= width
* height
* image
->TexFormat
->TexelBytes
;
223 memcpy(dst
, src
, bytes
);
226 r128FireBlitLocked( rmesa
, buffer
,
227 offset
, pitch
, format
,
228 x
, y
, width
, height
);
229 UNLOCK_HARDWARE( rmesa
);
232 rmesa
->new_state
|= R128_NEW_CONTEXT
;
233 rmesa
->dirty
|= R128_UPLOAD_CONTEXT
| R128_UPLOAD_MASKS
;
237 /* Upload the texture images associated with texture `t'. This might
238 * require removing our own and/or other client's texture objects to
239 * make room for these images.
241 void r128UploadTexImages( r128ContextPtr rmesa
, r128TexObjPtr t
)
243 const GLint numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
246 if ( R128_DEBUG
& DEBUG_VERBOSE_API
) {
247 fprintf( stderr
, "%s( %p, %p )\n",
248 __FUNCTION__
, (void *) rmesa
->glCtx
, (void *) t
);
253 LOCK_HARDWARE( rmesa
);
255 if ( !t
->base
.memBlock
) {
259 heap
= driAllocateTexture( rmesa
->texture_heaps
, rmesa
->nr_heaps
,
260 (driTextureObject
*) t
);
262 UNLOCK_HARDWARE( rmesa
);
266 /* Set the base offset of the texture image */
267 t
->bufAddr
= rmesa
->r128Screen
->texOffset
[heap
]
268 + t
->base
.memBlock
->ofs
;
270 /* Set texture offsets for each mipmap level */
271 if ( t
->setup
.tex_cntl
& R128_MIP_MAP_DISABLE
) {
272 for ( i
= 0 ; i
< R128_MAX_TEXTURE_LEVELS
; i
++ ) {
273 t
->setup
.tex_offset
[i
] = t
->bufAddr
;
276 for ( i
= 0; i
< numLevels
; i
++ ) {
277 const int j
= numLevels
- i
- 1;
278 t
->setup
.tex_offset
[j
] = t
->bufAddr
+ t
->image
[i
].offset
;
283 /* Let the world know we've used this memory recently.
285 driUpdateTextureLRU( (driTextureObject
*) t
);
286 UNLOCK_HARDWARE( rmesa
);
288 /* Upload any images that are new */
289 if ( t
->base
.dirty_images
[0] ) {
290 for ( i
= 0 ; i
< numLevels
; i
++ ) {
291 const GLint j
= t
->base
.firstLevel
+ i
; /* the texObj's level */
292 if ( t
->base
.dirty_images
[0] & (1 << j
) ) {
293 uploadSubImage( rmesa
, t
, j
, 0, 0,
294 t
->image
[i
].width
, t
->image
[i
].height
);
298 rmesa
->setup
.tex_cntl_c
|= R128_TEX_CACHE_FLUSH
;
299 rmesa
->dirty
|= R128_UPLOAD_CONTEXT
;
300 t
->base
.dirty_images
[0] = 0;