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>
27 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgatexmem.c,v 1.7 2002/10/30 12:51:36 alanh Exp $ */
32 #include "mgacontext.h"
36 #include "mga_xmesa.h"
39 #include "simple_list.h"
42 * Destroy any device-dependent state associated with the texture. This may
43 * include NULLing out hardware state that points to the texture.
46 mgaDestroyTexObj( mgaContextPtr mmesa, mgaTextureObjectPtr t )
51 /* See if it was the driver's current object.
56 if ( t->age > mmesa->dirtyAge )
57 mmesa->dirtyAge = t->age;
59 for ( i = 0 ; i < mmesa->glCtx->Const.MaxTextureUnits ; i++ )
61 if ( t == mmesa->CurrentTexObj[ i ] ) {
62 mmesa->CurrentTexObj[ i ] = NULL;
70 * Upload a texture image from system memory to either on-card or AGP
71 * memory. Uploads to on-card memory are performed using an ILOAD operation.
72 * This is used for both initial loading of the entire image, and texSubImage
75 * Performed with the hardware lock held.
77 * Even though this function is named "upload subimage," the entire image
80 * \param mmesa Driver context.
81 * \param t Texture to be uploaded.
82 * \param hwlevel Mipmap level of the texture to be uploaded.
84 * \bug As mentioned above, this fuction actually copies the entier mipmap
85 * level. There should be a version of this function that performs
86 * sub-rectangle uploads. This will perform quite a bit better if only
87 * a small portion of a larger texture has been updated. Care would
88 * need to be take with such an implementation once glCopyTexImage has
89 * been hardware accelerated.
91 static void mgaUploadSubImage( mgaContextPtr mmesa,
92 mgaTextureObjectPtr t, GLint hwlevel )
94 struct gl_texture_image * texImage;
98 const int level = hwlevel + t->base.firstLevel;
102 || (hwlevel >= (MGA_IS_G200(mmesa)
103 ? G200_TEX_MAXLEVELS : G400_TEX_MAXLEVELS)) ) {
104 fprintf( stderr, "[%s:%d] level = %d\n", __FILE__, __LINE__, level );
108 texImage = t->base.tObj->Image[level];
109 if ( texImage == NULL ) {
110 fprintf( stderr, "[%s:%d] Image[%d] = NULL\n", __FILE__, __LINE__,
116 if (texImage->Data == NULL) {
117 fprintf(stderr, "null texture image data tObj %p level %d\n",
118 t->base.tObj, level);
123 /* find the proper destination offset for this level */
124 if ( MGA_IS_G200(mmesa) ) {
125 offset = (t->base.memBlock->ofs + t->offsets[hwlevel]);
130 offset = t->base.memBlock->ofs;
131 for ( i = 0 ; i < hwlevel ; i++ ) {
132 offset += (t->offsets[1] >> (i * 2));
137 /* Copy the texture from system memory to a memory space that can be
138 * directly used by the hardware for texturing.
141 texelBytes = texImage->TexFormat->TexelBytes;
142 length = texImage->Width * texImage->Height * texelBytes;
143 if ( t->base.heap->heapId == MGA_CARD_HEAP ) {
144 unsigned tex_offset = 0;
148 /* We may not be able to upload the entire texture in one batch due to
149 * register limits or dma buffer limits. Split the copy up into maximum
153 offset += mmesa->mgaScreen->textureOffset[ t->base.heap->heapId ];
154 while ( length != 0 ) {
155 mgaGetILoadBufferLocked( mmesa );
157 /* The kernel ILOAD ioctl requires that the lenght be an even multiple
158 * of MGA_ILOAD_ALIGN.
160 length = ((length) + MGA_ILOAD_MASK) & ~MGA_ILOAD_MASK;
162 to_copy = MIN2( length, MGA_BUFFER_SIZE );
163 (void) memcpy( mmesa->iload_buffer->address,
164 (GLubyte *) texImage->Data + tex_offset, to_copy );
166 if ( MGA_DEBUG & DEBUG_VERBOSE_TEXTURE )
167 fprintf(stderr, "[%s:%d] address/size = 0x%08lx/%d\n",
169 (long) (offset + tex_offset),
172 mgaFireILoadLocked( mmesa, offset + tex_offset, to_copy );
173 tex_offset += to_copy;
177 /* FIXME: the sync for direct copy reduces speed.. */
178 /* This works, is slower for uploads to card space and needs
179 * additional synchronization with the dma stream.
182 UPDATE_LOCK(mmesa, DRM_LOCK_FLUSH | DRM_LOCK_QUIESCENT);
184 memcpy( mmesa->mgaScreen->texVirtual[t->base.heap->heapId] + offset,
185 texImage->Data, length );
187 if ( MGA_DEBUG & DEBUG_VERBOSE_TEXTURE )
188 fprintf(stderr, "[%s:%d] address/size = 0x%08lx/%d\n",
190 (long) (mmesa->mgaScreen->texVirtual[t->base.heap->heapId]
198 * Upload the texture images associated with texture \a t. This might
199 * require the allocation of texture memory.
201 * \param mmesa Context pointer
202 * \param t Texture to be uploaded
205 int mgaUploadTexImages( mgaContextPtr mmesa, mgaTextureObjectPtr t )
211 if ( (t == NULL) || (t->base.totalSize == 0) )
214 LOCK_HARDWARE( mmesa );
216 if (t->base.memBlock == NULL ) {
219 heap = driAllocateTexture( mmesa->texture_heaps, mmesa->nr_heaps,
220 (driTextureObject *) t );
222 UNLOCK_HARDWARE( mmesa );
226 ofs = mmesa->mgaScreen->textureOffset[ heap ]
227 + t->base.memBlock->ofs;
229 if ( MGA_IS_G200(mmesa) ) {
230 t->setup.texorg = ofs;
231 t->setup.texorg1 = ofs + t->offsets[1];
232 t->setup.texorg2 = ofs + t->offsets[2];
233 t->setup.texorg3 = ofs + t->offsets[3];
234 t->setup.texorg4 = ofs + t->offsets[4];
237 t->setup.texorg = ofs | TO_texorgoffsetsel;
238 t->setup.texorg1 = t->offsets[1];
239 t->setup.texorg2 = 0;
240 t->setup.texorg3 = 0;
241 t->setup.texorg4 = 0;
244 mmesa->dirty |= MGA_UPLOAD_CONTEXT;
247 /* Let the world know we've used this memory recently.
249 driUpdateTextureLRU( (driTextureObject *) t );
251 if (MGA_DEBUG&DEBUG_VERBOSE_TEXTURE)
252 fprintf(stderr, "[%s:%d] dispatch age: %d age freed memory: %d\n",
254 GET_DISPATCH_AGE(mmesa), mmesa->dirtyAge);
256 if (mmesa->dirtyAge >= GET_DISPATCH_AGE(mmesa))
257 mgaWaitAgeLocked( mmesa, mmesa->dirtyAge );
259 if (t->base.dirty_images[0]) {
260 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
262 if (MGA_DEBUG&DEBUG_VERBOSE_TEXTURE)
263 fprintf(stderr, "[%s:%d] dirty_images[0] = 0x%04x\n",
264 __FILE__, __LINE__, t->base.dirty_images[0] );
266 for (i = 0 ; i < numLevels ; i++) {
267 if ( (t->base.dirty_images[0] & (1U << i)) != 0 ) {
268 mgaUploadSubImage( mmesa, t, i );
271 t->base.dirty_images[0] = 0;
275 UNLOCK_HARDWARE( mmesa );