patch to import Jon Smirl's work from Bitkeeper
[mesa.git] / src / mesa / drivers / dri / mga / mgatexmem.c
1 /*
2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgatexmem.c,v 1.7 2002/10/30 12:51:36 alanh Exp $ */
28
29 #include "glheader.h"
30
31 #include "mm.h"
32 #include "mgacontext.h"
33 #include "mgatex.h"
34 #include "mgaregs.h"
35 #include "mgaioctl.h"
36 #include "mga_xmesa.h"
37
38 #include "imports.h"
39 #include "simple_list.h"
40
41 /**
42 * Destroy any device-dependent state associated with the texture. This may
43 * include NULLing out hardware state that points to the texture.
44 */
45 void
46 mgaDestroyTexObj( mgaContextPtr mmesa, mgaTextureObjectPtr t )
47 {
48 unsigned i;
49
50
51 /* See if it was the driver's current object.
52 */
53
54 if ( mmesa != NULL )
55 {
56 if ( t->age > mmesa->dirtyAge )
57 mmesa->dirtyAge = t->age;
58
59 for ( i = 0 ; i < mmesa->glCtx->Const.MaxTextureUnits ; i++ )
60 {
61 if ( t == mmesa->CurrentTexObj[ i ] ) {
62 mmesa->CurrentTexObj[ i ] = NULL;
63 }
64 }
65 }
66 }
67
68
69 /**
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
73 * updates.
74 *
75 * Performed with the hardware lock held.
76 *
77 * Even though this function is named "upload subimage," the entire image
78 * is uploaded.
79 *
80 * \param mmesa Driver context.
81 * \param t Texture to be uploaded.
82 * \param hwlevel Mipmap level of the texture to be uploaded.
83 *
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.
90 */
91 static void mgaUploadSubImage( mgaContextPtr mmesa,
92 mgaTextureObjectPtr t, GLint hwlevel )
93 {
94 struct gl_texture_image * texImage;
95 unsigned offset;
96 unsigned texelBytes;
97 unsigned length;
98 const int level = hwlevel + t->base.firstLevel;
99
100
101 if ( (hwlevel < 0)
102 || (hwlevel >= (MGA_IS_G200(mmesa)
103 ? G200_TEX_MAXLEVELS : G400_TEX_MAXLEVELS)) ) {
104 fprintf( stderr, "[%s:%d] level = %d\n", __FILE__, __LINE__, level );
105 return;
106 }
107
108 texImage = t->base.tObj->Image[level];
109 if ( texImage == NULL ) {
110 fprintf( stderr, "[%s:%d] Image[%d] = NULL\n", __FILE__, __LINE__,
111 level );
112 return;
113 }
114
115
116 if (texImage->Data == NULL) {
117 fprintf(stderr, "null texture image data tObj %p level %d\n",
118 t->base.tObj, level);
119 return;
120 }
121
122
123 /* find the proper destination offset for this level */
124 if ( MGA_IS_G200(mmesa) ) {
125 offset = (t->base.memBlock->ofs + t->offsets[hwlevel]);
126 }
127 else {
128 unsigned i;
129
130 offset = t->base.memBlock->ofs;
131 for ( i = 0 ; i < hwlevel ; i++ ) {
132 offset += (t->offsets[1] >> (i * 2));
133 }
134
135 /* Each mipmap must be DWORD aligned.
136 */
137 offset &= ~0x03;
138 }
139
140
141 /* Copy the texture from system memory to a memory space that can be
142 * directly used by the hardware for texturing.
143 */
144
145 texelBytes = texImage->TexFormat->TexelBytes;
146 length = texImage->Width * texImage->Height * texelBytes;
147 if ( t->base.heap->heapId == MGA_CARD_HEAP ) {
148 unsigned tex_offset = 0;
149 unsigned to_copy;
150
151
152 /* We may not be able to upload the entire texture in one batch due to
153 * register limits or dma buffer limits. Split the copy up into maximum
154 * sized chunks.
155 */
156
157 offset += mmesa->mgaScreen->textureOffset[ t->base.heap->heapId ];
158 while ( length != 0 ) {
159 mgaGetILoadBufferLocked( mmesa );
160
161 /* The kernel ILOAD ioctl requires that the lenght be an even multiple
162 * of MGA_ILOAD_ALIGN.
163 */
164 length = ((length) + MGA_ILOAD_MASK) & ~MGA_ILOAD_MASK;
165
166 to_copy = MIN2( length, MGA_BUFFER_SIZE );
167 (void) memcpy( mmesa->iload_buffer->address,
168 (GLubyte *) texImage->Data + tex_offset, to_copy );
169
170 if ( MGA_DEBUG & DEBUG_VERBOSE_TEXTURE )
171 fprintf(stderr, "[%s:%d] address/size = 0x%08lx/%d\n",
172 __FILE__, __LINE__,
173 (long) (offset + tex_offset),
174 to_copy );
175
176 mgaFireILoadLocked( mmesa, offset + tex_offset, to_copy );
177 tex_offset += to_copy;
178 length -= to_copy;
179 }
180 } else {
181 /* FIXME: the sync for direct copy reduces speed.. */
182 /* This works, is slower for uploads to card space and needs
183 * additional synchronization with the dma stream.
184 */
185
186 UPDATE_LOCK(mmesa, DRM_LOCK_FLUSH | DRM_LOCK_QUIESCENT);
187
188 memcpy( mmesa->mgaScreen->texVirtual[t->base.heap->heapId] + offset,
189 texImage->Data, length );
190
191 if ( MGA_DEBUG & DEBUG_VERBOSE_TEXTURE )
192 fprintf(stderr, "[%s:%d] address/size = 0x%08lx/%d\n",
193 __FILE__, __LINE__,
194 (long) (mmesa->mgaScreen->texVirtual[t->base.heap->heapId]
195 + offset),
196 length);
197 }
198 }
199
200
201 /**
202 * Upload the texture images associated with texture \a t. This might
203 * require the allocation of texture memory.
204 *
205 * \param mmesa Context pointer
206 * \param t Texture to be uploaded
207 */
208
209 int mgaUploadTexImages( mgaContextPtr mmesa, mgaTextureObjectPtr t )
210 {
211 int i;
212 int ofs;
213
214
215 if ( (t == NULL) || (t->base.totalSize == 0) )
216 return 0;
217
218 LOCK_HARDWARE( mmesa );
219
220 if (t->base.memBlock == NULL ) {
221 int heap;
222
223 heap = driAllocateTexture( mmesa->texture_heaps, mmesa->nr_heaps,
224 (driTextureObject *) t );
225 if ( heap == -1 ) {
226 UNLOCK_HARDWARE( mmesa );
227 return -1;
228 }
229
230 ofs = mmesa->mgaScreen->textureOffset[ heap ]
231 + t->base.memBlock->ofs;
232
233 if ( MGA_IS_G200(mmesa) ) {
234 t->setup.texorg = ofs;
235 t->setup.texorg1 = ofs + t->offsets[1];
236 t->setup.texorg2 = ofs + t->offsets[2];
237 t->setup.texorg3 = ofs + t->offsets[3];
238 t->setup.texorg4 = ofs + t->offsets[4];
239 }
240 else {
241 t->setup.texorg = ofs | TO_texorgoffsetsel;
242 t->setup.texorg1 = t->offsets[1];
243 t->setup.texorg2 = 0;
244 t->setup.texorg3 = 0;
245 t->setup.texorg4 = 0;
246 }
247
248 mmesa->dirty |= MGA_UPLOAD_CONTEXT;
249 }
250
251 /* Let the world know we've used this memory recently.
252 */
253 driUpdateTextureLRU( (driTextureObject *) t );
254
255 if (MGA_DEBUG&DEBUG_VERBOSE_TEXTURE)
256 fprintf(stderr, "[%s:%d] dispatch age: %d age freed memory: %d\n",
257 __FILE__, __LINE__,
258 GET_DISPATCH_AGE(mmesa), mmesa->dirtyAge);
259
260 if (mmesa->dirtyAge >= GET_DISPATCH_AGE(mmesa))
261 mgaWaitAgeLocked( mmesa, mmesa->dirtyAge );
262
263 if (t->base.dirty_images[0]) {
264 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
265
266 if (MGA_DEBUG&DEBUG_VERBOSE_TEXTURE)
267 fprintf(stderr, "[%s:%d] dirty_images[0] = 0x%04x\n",
268 __FILE__, __LINE__, t->base.dirty_images[0] );
269
270 for (i = 0 ; i < numLevels ; i++) {
271 if ( (t->base.dirty_images[0] & (1U << i)) != 0 ) {
272 mgaUploadSubImage( mmesa, t, i );
273 }
274 }
275 t->base.dirty_images[0] = 0;
276 }
277
278
279 UNLOCK_HARDWARE( mmesa );
280
281 return 0;
282 }