2 * Copyright 2000-2001 VA Linux Systems, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
28 #include "main/glheader.h"
31 #include "mgacontext.h"
35 #include "mga_xmesa.h"
37 #include "main/imports.h"
38 #include "main/simple_list.h"
41 * Destroy any device-dependent state associated with the texture. This may
42 * include NULLing out hardware state that points to the texture.
45 mgaDestroyTexObj( mgaContextPtr mmesa
, mgaTextureObjectPtr t
)
50 /* See if it was the driver's current object.
55 if ( t
->age
> mmesa
->dirtyAge
)
56 mmesa
->dirtyAge
= t
->age
;
58 for ( i
= 0 ; i
< mmesa
->glCtx
->Const
.MaxTextureUnits
; i
++ )
60 if ( t
== mmesa
->CurrentTexObj
[ i
] ) {
61 mmesa
->CurrentTexObj
[ i
] = NULL
;
69 * Upload a texture image from system memory to either on-card or AGP
70 * memory. Uploads to on-card memory are performed using an ILOAD operation.
71 * This is used for both initial loading of the entire image, and texSubImage
74 * Performed with the hardware lock held.
76 * Even though this function is named "upload subimage," the entire image
79 * \param mmesa Driver context.
80 * \param t Texture to be uploaded.
81 * \param hwlevel Mipmap level of the texture to be uploaded.
83 * \bug As mentioned above, this fuction actually copies the entier mipmap
84 * level. There should be a version of this function that performs
85 * sub-rectangle uploads. This will perform quite a bit better if only
86 * a small portion of a larger texture has been updated. Care would
87 * need to be take with such an implementation once glCopyTexImage has
88 * been hardware accelerated.
90 static void mgaUploadSubImage( mgaContextPtr mmesa
,
91 mgaTextureObjectPtr t
, GLint hwlevel
)
93 struct gl_texture_image
* texImage
;
97 const int level
= hwlevel
+ t
->base
.firstLevel
;
101 || (hwlevel
>= (MGA_IS_G200(mmesa
)
102 ? G200_TEX_MAXLEVELS
: G400_TEX_MAXLEVELS
)) ) {
103 fprintf( stderr
, "[%s:%d] level = %d\n", __FILE__
, __LINE__
, level
);
107 texImage
= t
->base
.tObj
->Image
[0][level
];
108 if ( texImage
== NULL
) {
109 fprintf( stderr
, "[%s:%d] Image[%d] = NULL\n", __FILE__
, __LINE__
,
115 if (texImage
->Data
== NULL
) {
116 fprintf(stderr
, "null texture image data tObj %p level %d\n",
117 (void *) t
->base
.tObj
, level
);
122 /* find the proper destination offset for this level */
123 if ( MGA_IS_G200(mmesa
) ) {
124 offset
= (t
->base
.memBlock
->ofs
+ t
->offsets
[hwlevel
]);
129 offset
= t
->base
.memBlock
->ofs
;
130 for ( i
= 0 ; i
< hwlevel
; i
++ ) {
131 offset
+= (t
->offsets
[1] >> (i
* 2));
136 /* Copy the texture from system memory to a memory space that can be
137 * directly used by the hardware for texturing.
140 texelBytes
= texImage
->TexFormat
->TexelBytes
;
141 length
= texImage
->Width
* texImage
->Height
* texelBytes
;
142 if ( t
->base
.heap
->heapId
== MGA_CARD_HEAP
) {
143 unsigned tex_offset
= 0;
147 /* We may not be able to upload the entire texture in one batch due to
148 * register limits or dma buffer limits. Split the copy up into maximum
152 offset
+= mmesa
->mgaScreen
->textureOffset
[ t
->base
.heap
->heapId
];
153 while ( length
!= 0 ) {
154 mgaGetILoadBufferLocked( mmesa
);
156 /* The kernel ILOAD ioctl requires that the lenght be an even multiple
157 * of MGA_ILOAD_ALIGN.
159 length
= ((length
) + MGA_ILOAD_MASK
) & ~MGA_ILOAD_MASK
;
161 to_copy
= MIN2( length
, MGA_BUFFER_SIZE
);
162 (void) memcpy( mmesa
->iload_buffer
->address
,
163 (GLubyte
*) texImage
->Data
+ tex_offset
, to_copy
);
165 if ( MGA_DEBUG
& DEBUG_VERBOSE_TEXTURE
)
166 fprintf(stderr
, "[%s:%d] address/size = 0x%08lx/%d\n",
168 (long) (offset
+ tex_offset
),
171 mgaFireILoadLocked( mmesa
, offset
+ tex_offset
, to_copy
);
172 tex_offset
+= to_copy
;
176 /* FIXME: the sync for direct copy reduces speed.. */
177 /* This works, is slower for uploads to card space and needs
178 * additional synchronization with the dma stream.
181 UPDATE_LOCK(mmesa
, DRM_LOCK_FLUSH
| DRM_LOCK_QUIESCENT
);
183 memcpy( mmesa
->mgaScreen
->texVirtual
[t
->base
.heap
->heapId
] + offset
,
184 texImage
->Data
, length
);
186 if ( MGA_DEBUG
& DEBUG_VERBOSE_TEXTURE
)
187 fprintf(stderr
, "[%s:%d] address/size = 0x%08lx/%d\n",
189 (long) (mmesa
->mgaScreen
->texVirtual
[t
->base
.heap
->heapId
]
197 * Upload the texture images associated with texture \a t. This might
198 * require the allocation of texture memory.
200 * \param mmesa Context pointer
201 * \param t Texture to be uploaded
204 int mgaUploadTexImages( mgaContextPtr mmesa
, mgaTextureObjectPtr t
)
210 if ( (t
== NULL
) || (t
->base
.totalSize
== 0) )
213 LOCK_HARDWARE( mmesa
);
215 if (t
->base
.memBlock
== NULL
) {
218 heap
= driAllocateTexture( mmesa
->texture_heaps
, mmesa
->nr_heaps
,
219 (driTextureObject
*) t
);
221 UNLOCK_HARDWARE( mmesa
);
225 ofs
= mmesa
->mgaScreen
->textureOffset
[ heap
]
226 + t
->base
.memBlock
->ofs
;
228 if ( MGA_IS_G200(mmesa
) ) {
229 t
->setup
.texorg
= ofs
;
230 t
->setup
.texorg1
= ofs
+ t
->offsets
[1];
231 t
->setup
.texorg2
= ofs
+ t
->offsets
[2];
232 t
->setup
.texorg3
= ofs
+ t
->offsets
[3];
233 t
->setup
.texorg4
= ofs
+ t
->offsets
[4];
236 t
->setup
.texorg
= ofs
| TO_texorgoffsetsel
;
237 t
->setup
.texorg1
= t
->offsets
[1];
238 t
->setup
.texorg2
= 0;
239 t
->setup
.texorg3
= 0;
240 t
->setup
.texorg4
= 0;
243 mmesa
->dirty
|= MGA_UPLOAD_CONTEXT
;
246 /* Let the world know we've used this memory recently.
248 driUpdateTextureLRU( (driTextureObject
*) t
);
250 if (MGA_DEBUG
&DEBUG_VERBOSE_TEXTURE
)
251 fprintf(stderr
, "[%s:%d] dispatch age: %d age freed memory: %d\n",
253 GET_DISPATCH_AGE(mmesa
), mmesa
->dirtyAge
);
255 if (mmesa
->dirtyAge
>= GET_DISPATCH_AGE(mmesa
))
256 mgaWaitAgeLocked( mmesa
, mmesa
->dirtyAge
);
258 if (t
->base
.dirty_images
[0]) {
259 const int numLevels
= t
->base
.lastLevel
- t
->base
.firstLevel
+ 1;
261 if (MGA_DEBUG
&DEBUG_VERBOSE_TEXTURE
)
262 fprintf(stderr
, "[%s:%d] dirty_images[0] = 0x%04x\n",
263 __FILE__
, __LINE__
, t
->base
.dirty_images
[0] );
265 for (i
= 0 ; i
< numLevels
; i
++) {
266 if ( (t
->base
.dirty_images
[0] & (1U << i
)) != 0 ) {
267 mgaUploadSubImage( mmesa
, t
, i
);
270 t
->base
.dirty_images
[0] = 0;
274 UNLOCK_HARDWARE( mmesa
);