1 /**************************************************************************
3 Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Gareth Hughes <gareth@valinux.com>
31 * Kevin E. Martin <martin@valinux.com>
32 * Brian Paul <brianp@valinux.com>
35 #include "r128_context.h"
36 #include "r128_state.h"
37 #include "r128_ioctl.h"
38 #include "r128_tris.h"
43 #include "simple_list.h"
44 #include "texformat.h"
51 /* Destroy hardware state associated with texture `t'.
53 void r128DestroyTexObj( r128ContextPtr rmesa
, r128TexObjPtr t
)
58 /* See if it was the driver's current object.
63 for ( i
= 0 ; i
< rmesa
->glCtx
->Const
.MaxTextureUnits
; i
++ )
65 if ( t
== rmesa
->CurrentTexObj
[ i
] ) {
66 assert( t
->base
.bound
& (1 << i
) );
67 rmesa
->CurrentTexObj
[ i
] = NULL
;
75 * Upload the texture image associated with texture \a t at the specified
76 * level at the address relative to \a start.
78 static void uploadSubImage( r128ContextPtr rmesa
, r128TexObjPtr t
,
80 GLint x
, GLint y
, GLint width
, GLint height
)
82 struct gl_texture_image
*image
;
83 int texelsPerDword
= 0;
84 int imageWidth
, imageHeight
;
87 uint32_t pitch
, offset
;
90 /* Ensure we have a valid texture to upload */
91 if ( ( level
< 0 ) || ( level
> R128_MAX_TEXTURE_LEVELS
) )
94 image
= t
->base
.tObj
->Image
[0][level
];
98 switch ( image
->TexFormat
->TexelBytes
) {
99 case 1: texelsPerDword
= 4; break;
100 case 2: texelsPerDword
= 2; break;
101 case 4: texelsPerDword
= 1; break;
105 /* FIXME: The subimage index calcs are wrong... */
108 width
= image
->Width
;
109 height
= image
->Height
;
112 imageWidth
= image
->Width
;
113 imageHeight
= image
->Height
;
115 format
= t
->textureFormat
>> 16;
117 /* The texel upload routines have a minimum width, so force the size
120 if ( imageWidth
< texelsPerDword
) {
123 factor
= texelsPerDword
/ imageWidth
;
124 imageWidth
= texelsPerDword
;
125 imageHeight
/= factor
;
126 if ( imageHeight
== 0 ) {
127 /* In this case, the texel converter will actually walk a
128 * texel or two off the end of the image, but normal malloc
129 * alignment should prevent it from ever causing a fault.
135 /* We can't upload to a pitch less than 8 texels so we will need to
136 * linearly upload all modified rows for textures smaller than this.
137 * This makes the x/y/width/height different for the blitter and the
140 if ( imageWidth
>= 8 ) {
141 /* The texture walker and the blitter look identical */
142 pitch
= imageWidth
>> 3;
148 start
= (y
* imageWidth
) & ~7;
149 end
= (y
+ height
) * imageWidth
;
151 if ( end
- start
< 8 ) {
152 /* Handle the case where the total number of texels
160 /* Upload some number of full 8 texel blit rows */
161 factor
= 8 / imageWidth
;
172 /* Fixed pitch of 8 */
176 dwords
= width
* height
/ texelsPerDword
;
177 offset
= t
->bufAddr
+ t
->image
[level
- t
->base
.firstLevel
].offset
;
179 #if ENABLE_PERF_BOXES
180 /* Bump the performace counter */
181 rmesa
->c_textureBytes
+= (dwords
<< 2);
184 if ( R128_DEBUG
& DEBUG_VERBOSE_API
) {
185 fprintf( stderr
, "r128UploadSubImage: %d,%d of %d,%d at %d,%d\n",
186 width
, height
, image
->Width
, image
->Height
, x
, y
);
187 fprintf( stderr
, " blit ofs: 0x%07x pitch: 0x%x dwords: %d "
188 "level: %d format: %x\n",
189 (GLuint
)offset
, (GLuint
)pitch
, dwords
, level
, format
);
192 /* Subdivide the texture if required */
193 if ( dwords
<= R128_BUFFER_MAX_DWORDS
/ 2 ) {
196 rows
= (R128_BUFFER_MAX_DWORDS
* texelsPerDword
) / (2 * width
);
199 for ( i
= 0, remaining
= height
;
201 remaining
-= rows
, y
+= rows
, i
++ )
208 height
= MIN2(remaining
, rows
);
210 /* Grab the indirect buffer for the texture blit */
211 LOCK_HARDWARE( rmesa
);
212 buffer
= r128GetBufferLocked( rmesa
);
214 dst
= (uint32_t *)((char *)buffer
->address
+ R128_HOSTDATA_BLIT_OFFSET
);
216 /* Copy the next chunck of the texture image into the blit buffer */
218 const GLubyte
*src
= (const GLubyte
*) image
->Data
+
219 (y
* image
->Width
+ x
) * image
->TexFormat
->TexelBytes
;
220 const GLuint bytes
= width
* height
* image
->TexFormat
->TexelBytes
;
221 memcpy(dst
, src
, bytes
);
224 r128FireBlitLocked( rmesa
, buffer
,
225 offset
, pitch
, format
,
226 x
, y
, width
, height
);
227 UNLOCK_HARDWARE( rmesa
);
230 rmesa
->new_state
|= R128_NEW_CONTEXT
;
231 rmesa
->dirty
|= R128_UPLOAD_CONTEXT
| R128_UPLOAD_MASKS
;
235 /* Upload the texture images associated with texture `t'. This might
236 * require removing our own and/or other client's texture objects to
237 * make room for these images.
239 void r128UploadTexImages( r128ContextPtr rmesa
, r128TexObjPtr t
)
241 const GLint numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
244 if ( R128_DEBUG
& DEBUG_VERBOSE_API
) {
245 fprintf( stderr
, "%s( %p, %p )\n",
246 __FUNCTION__
, (void *) rmesa
->glCtx
, (void *) t
);
251 LOCK_HARDWARE( rmesa
);
253 if ( !t
->base
.memBlock
) {
257 heap
= driAllocateTexture( rmesa
->texture_heaps
, rmesa
->nr_heaps
,
258 (driTextureObject
*) t
);
260 UNLOCK_HARDWARE( rmesa
);
264 /* Set the base offset of the texture image */
265 t
->bufAddr
= rmesa
->r128Screen
->texOffset
[heap
]
266 + t
->base
.memBlock
->ofs
;
268 /* Set texture offsets for each mipmap level */
269 if ( t
->setup
.tex_cntl
& R128_MIP_MAP_DISABLE
) {
270 for ( i
= 0 ; i
< R128_MAX_TEXTURE_LEVELS
; i
++ ) {
271 t
->setup
.tex_offset
[i
] = t
->bufAddr
;
274 for ( i
= 0; i
< numLevels
; i
++ ) {
275 const int j
= numLevels
- i
- 1;
276 t
->setup
.tex_offset
[j
] = t
->bufAddr
+ t
->image
[i
].offset
;
281 /* Let the world know we've used this memory recently.
283 driUpdateTextureLRU( (driTextureObject
*) t
);
284 UNLOCK_HARDWARE( rmesa
);
286 /* Upload any images that are new */
287 if ( t
->base
.dirty_images
[0] ) {
288 for ( i
= 0 ; i
< numLevels
; i
++ ) {
289 const GLint j
= t
->base
.firstLevel
+ i
; /* the texObj's level */
290 if ( t
->base
.dirty_images
[0] & (1 << j
) ) {
291 uploadSubImage( rmesa
, t
, j
, 0, 0,
292 t
->image
[i
].width
, t
->image
[i
].height
);
296 rmesa
->setup
.tex_cntl_c
|= R128_TEX_CACHE_FLUSH
;
297 rmesa
->dirty
|= R128_UPLOAD_CONTEXT
;
298 t
->base
.dirty_images
[0] = 0;