summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/mach64/mach64_texmem.c
blob: 843b2310517ea93726cf76352c3d7dc180c36af1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* -*- mode: c; c-basic-offset: 3 -*- */
/*
 * Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
 *                                                Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Authors:
 *   Gareth Hughes <gareth@valinux.com>
 *   Leif Delgass <ldelgass@retinalburn.net>
 *   Jose Fonseca <j_r_fonseca@yahoo.co.uk>
 */

#include "mach64_context.h"
#include "mach64_state.h"
#include "mach64_ioctl.h"
#include "mach64_vb.h"
#include "mach64_tris.h"
#include "mach64_tex.h"

#include "main/context.h"
#include "main/macros.h"
#include "main/simple_list.h"
#include "main/texformat.h"
#include "main/imports.h"


/* Destroy hardware state associated with texture `t'.
 */
void mach64DestroyTexObj( mach64ContextPtr mmesa, mach64TexObjPtr t )
{
   unsigned   i;

   /* See if it was the driver's current object.
    */
   if ( mmesa != NULL )
   {
      for ( i = 0 ; i < mmesa->glCtx->Const.MaxTextureUnits ; i++ )
      {
         if ( t == mmesa->CurrentTexObj[ i ] ) {
            assert( t->base.bound & (1 << i) );
            mmesa->CurrentTexObj[ i ] = NULL;
         }
      }
   }
}

/* Upload the texture image associated with texture `t' at level `level'
 * at the address relative to `start'.
 */
static void mach64UploadAGPSubImage( mach64ContextPtr mmesa,
				     mach64TexObjPtr t, int level,
				     int x, int y, int width, int height )
{
   mach64ScreenRec *mach64Screen = mmesa->mach64Screen;
   struct gl_texture_image *image;
   int texelsPerDword = 0;
   int dwords;
   GLuint texelBytes;

   /* Ensure we have a valid texture to upload */
   if ( ( level < 0 ) || ( level > mmesa->glCtx->Const.MaxTextureLevels ) )
     return;

   image = t->base.tObj->Image[0][level];
   if ( !image )
      return;

   texelBytes = _mesa_get_format_bytes(image->TexFormat->MesaFormat);

   switch ( texelBytes ) {
   case 1: texelsPerDword = 4; break;
   case 2: texelsPerDword = 2; break;
   case 4: texelsPerDword = 1; break;
   }

#if 1
   /* FIXME: The subimage index calcs are wrong... */
   x = 0;
   y = 0;
   width = image->Width;
   height = image->Height;
#endif

   dwords = width * height / texelsPerDword;

#if ENABLE_PERF_BOXES
   /* Bump the performance counter */
   mmesa->c_agpTextureBytes += (dwords << 2);
#endif

   if ( MACH64_DEBUG & DEBUG_VERBOSE_API ) {
      fprintf( stderr, "mach64UploadSubImage: %d,%d of %d,%d at %d,%d\n",
	       width, height, image->Width, image->Height, x, y );
      fprintf( stderr, "            blit ofs: 0x%07x pitch: 0x%x dwords: %d\n",
	       (GLuint)t->bufAddr, (GLint)width, dwords );
   }

   assert(image->Data);

   {
      CARD32 *dst = (CARD32 *)((char *)mach64Screen->agpTextures.map + t->base.memBlock->ofs);
      const GLubyte *src = (const GLubyte *) image->Data +
	 (y * image->Width + x) * texelBytes;
      const GLuint bytes = width * height * texelBytes;
      memcpy(dst, src, bytes);
   }

}

/* Upload the texture image associated with texture `t' at level `level'
 * at the address relative to `start'.
 */
static void mach64UploadLocalSubImage( mach64ContextPtr mmesa,
				  mach64TexObjPtr t, int level,
				  int x, int y, int width, int height )
{
   struct gl_texture_image *image;
   int texelsPerDword = 0;
   int imageWidth, imageHeight;
   int remaining, rows;
   int format, dwords;
   const int maxdwords = (MACH64_BUFFER_MAX_DWORDS - (MACH64_HOSTDATA_BLIT_OFFSET / 4));
   CARD32 pitch, offset;
   int i;
   GLuint texelBytes;

   /* Ensure we have a valid texture to upload */
   if ( ( level < 0 ) || ( level > mmesa->glCtx->Const.MaxTextureLevels ) )
      return;

   image = t->base.tObj->Image[0][level];
   if ( !image )
      return;

   texelBytes = _mesa_get_format_bytes(image->TexFormat->MesaFormat);

   switch ( texelBytes ) {
   case 1: texelsPerDword = 4; break;
   case 2: texelsPerDword = 2; break;
   case 4: texelsPerDword = 1; break;
   }

#if 1
   /* FIXME: The subimage index calcs are wrong... */
   x = 0;
   y = 0;
   width = image->Width;
   height = image->Height;
#endif

   imageWidth  = image->Width;
   imageHeight = image->Height;

   format = t->textureFormat;

   /* The texel upload routines have a minimum width, so force the size
    * if needed.
    */
   if ( imageWidth < texelsPerDword ) {
      int factor;

      factor = texelsPerDword / imageWidth;
      imageWidth = texelsPerDword;
      imageHeight /= factor;
      if ( imageHeight == 0 ) {
	 /* In this case, the texel converter will actually walk a
	  * texel or two off the end of the image, but normal malloc
	  * alignment should prevent it from ever causing a fault.
	  */
	 imageHeight = 1;
      }
   }

   /* We can't upload to a pitch less than 64 texels so we will need to
    * linearly upload all modified rows for textures smaller than this.
    * This makes the x/y/width/height different for the blitter and the
    * texture walker.
    */
   if ( imageWidth >= 64 ) {
      /* The texture walker and the blitter look identical */
      pitch = imageWidth >> 3;
   } else {
      int factor;
      int y2;
      int start, end;

      start = (y * imageWidth) & ~63;
      end = (y + height) * imageWidth;

      if ( end - start < 64 ) {
	 /* Handle the case where the total number of texels
	  * uploaded is < 64.
	  */
	 x = 0;
	 y = start / 64;
	 width = end - start;
	 height = 1;
      } else {
	 /* Upload some number of full 64 texel blit rows */
	 factor = 64 / imageWidth;

	 y2 = y + height - 1;
	 y /= factor;
	 y2 /= factor;

	 x = 0;
	 width = 64;
	 height = y2 - y + 1;
      }

      /* Fixed pitch of 64 */
      pitch = 8;
   }

   dwords = width * height / texelsPerDword;
   offset = t->bufAddr;

#if ENABLE_PERF_BOXES
   /* Bump the performance counter */
   mmesa->c_textureBytes += (dwords << 2);
#endif

   if ( MACH64_DEBUG & DEBUG_VERBOSE_API ) {
      fprintf( stderr, "mach64UploadSubImage: %d,%d of %d,%d at %d,%d\n",
	       width, height, image->Width, image->Height, x, y );
      fprintf( stderr, "            blit ofs: 0x%07x pitch: 0x%x dwords: %d\n",
	       (GLuint)offset, (GLint)width, dwords );
   }

   /* Subdivide the texture if required (account for the registers added by the drm) */
   if ( dwords <= maxdwords ) {
      rows = height;
   } else {
      rows = (maxdwords * texelsPerDword) / (2 * width);
   }

   for ( i = 0, remaining = height ;
	 remaining > 0 ;
	 remaining -= rows, y += rows, i++ )
   {
       height = MIN2(remaining, rows);

       assert(image->Data);

       {
          const GLubyte *src = (const GLubyte *) image->Data +
             (y * image->Width + x) * texelBytes;

          mach64FireBlitLocked( mmesa, (void *)src, offset, pitch, format,
				x, y, width, height );
       }

   }

   mmesa->new_state |= MACH64_NEW_CONTEXT;
   mmesa->dirty |= MACH64_UPLOAD_CONTEXT | MACH64_UPLOAD_MISC;
}


/* Upload the texture images associated with texture `t'.  This might
 * require removing our own and/or other client's texture objects to
 * make room for these images.
 */
void mach64UploadTexImages( mach64ContextPtr mmesa, mach64TexObjPtr t )
{
   if ( MACH64_DEBUG & DEBUG_VERBOSE_API ) {
      fprintf( stderr, "%s( %p, %p )\n",
	       __FUNCTION__, mmesa->glCtx, t );
   }

   assert(t);
   assert(t->base.tObj);

   if ( !t->base.memBlock ) {
      int heap;

      /* NULL heaps are skipped */
      heap = driAllocateTexture( mmesa->texture_heaps, MACH64_NR_TEX_HEAPS,
				 (driTextureObject *) t );

      if ( heap == -1 ) {
	 fprintf( stderr, "%s: upload texture failure, sz=%d\n", __FUNCTION__,
		  t->base.totalSize );
	 exit(-1);
	 return;
      }

      t->heap = heap;

      /* Set the base offset of the texture image */
      t->bufAddr = mmesa->mach64Screen->texOffset[heap] + t->base.memBlock->ofs;

      /* Force loading the new state into the hardware */
      mmesa->dirty |= (MACH64_UPLOAD_SCALE_3D_CNTL |
		       MACH64_UPLOAD_TEXTURE);
   }

   /* Let the world know we've used this memory recently */
   driUpdateTextureLRU( (driTextureObject *) t );

   /* Upload any images that are new */
   if ( t->base.dirty_images[0] ) {
      const GLint j = t->base.tObj->BaseLevel;
      if (t->heap == MACH64_AGP_HEAP) {
	 /* Need to make sure any vertex buffers in the queue complete */
	 mach64WaitForIdleLocked( mmesa );
	 mach64UploadAGPSubImage( mmesa, t, j, 0, 0,
				  t->base.tObj->Image[0][j]->Width,
				  t->base.tObj->Image[0][j]->Height );
      } else {
	 mach64UploadLocalSubImage( mmesa, t, j, 0, 0,
				    t->base.tObj->Image[0][j]->Width,
				    t->base.tObj->Image[0][j]->Height );
      }

      mmesa->setup.tex_cntl |= MACH64_TEX_CACHE_FLUSH;
      t->base.dirty_images[0] = 0;
   }

   mmesa->dirty |= MACH64_UPLOAD_TEXTURE;
}


/* Allocate memory from the same texture heap `heap' for both textures
 * `u0' and `u1'.
 */
static int mach64AllocateMultiTex( mach64ContextPtr mmesa,
				   mach64TexObjPtr u0,
				   mach64TexObjPtr u1,
				   int heap, GLboolean alloc_u0 )
{
   /* Both objects should be bound */
   assert( u0->base.bound && u1->base.bound );

   if ( alloc_u0 ) {
      /* Evict u0 from its current heap */
      if ( u0->base.memBlock ) {
	 assert( u0->heap != heap );
	 driSwapOutTextureObject( (driTextureObject *) u0 );
      }

      /* Try to allocate u0 in the chosen heap */
      u0->heap = driAllocateTexture( &mmesa->texture_heaps[heap], 1,
				     (driTextureObject *) u0 );

      if ( u0->heap == -1 ) {
	 return -1;
      }
   }

   /* Evict u1 from its current heap */
   if ( u1->base.memBlock ) {
      assert( u1->heap != heap );
      driSwapOutTextureObject( (driTextureObject *) u1 );
   }

   /* Try to allocate u1 in the same heap as u0 */
   u1->heap = driAllocateTexture( &mmesa->texture_heaps[heap], 1,
				  (driTextureObject *) u1 );

   if ( u1->heap == -1 ) {
      return -1;
   }

   /* Bound objects are not evicted */
   assert( u0->base.memBlock && u1->base.memBlock );
   assert( u0->heap == u1->heap );

   return heap;
}

/* The mach64 needs to have both primary and secondary textures in either
 * local or AGP memory, so we need a "buddy system" to make sure that allocation
 * succeeds or fails for both textures.
 */
void mach64UploadMultiTexImages( mach64ContextPtr mmesa, 
				 mach64TexObjPtr t0,
				 mach64TexObjPtr t1 )
{
   if ( MACH64_DEBUG & DEBUG_VERBOSE_API ) {
      fprintf( stderr, "%s( %p, %p %p )\n",
	       __FUNCTION__, mmesa->glCtx, t0, t1 );
   }

   assert(t0 && t1);
   assert(t0->base.tObj && t1->base.tObj);

   if ( !t0->base.memBlock || !t1->base.memBlock || t0->heap != t1->heap ) {
      mach64TexObjPtr u0 = NULL;
      mach64TexObjPtr u1 = NULL;
      unsigned totalSize = t0->base.totalSize + t1->base.totalSize;

      int heap, ret;

      /* Check if one of the textures is already swapped in a heap and the
       * other texture fits in that heap.
       */
      if ( t0->base.memBlock && totalSize <= t0->base.heap->size ) {
	 u0 = t0;
	 u1 = t1;
      } else if ( t1->base.memBlock && totalSize <= t1->base.heap->size ) {
	 u0 = t1;
	 u1 = t0;
      }

      if ( u0 ) {
	 heap = u0->heap;

	 ret = mach64AllocateMultiTex( mmesa, u0, u1, heap, GL_FALSE );
      } else {
	 /* Both textures are swapped out or collocation is impossible */
	 u0 = t0;
	 u1 = t1;

	 /* Choose the heap appropriately */
	 heap = MACH64_CARD_HEAP;

	 if ( totalSize > mmesa->texture_heaps[heap]->size ) {
	    heap = MACH64_AGP_HEAP;
	 }

	 ret = mach64AllocateMultiTex( mmesa, u0, u1, heap, GL_TRUE );
      }

      if ( ret == -1 && heap == MACH64_CARD_HEAP ) {
	 /* Try AGP if local memory failed */
	 heap = MACH64_AGP_HEAP;

	 ret = mach64AllocateMultiTex( mmesa, u0, u1, heap, GL_TRUE );
      }

      if ( ret == -1 ) {
	 /* FIXME:
	  * Swap out all textures from the AGP heap and re-run allocation, this
	  * should succeed in all cases.
	  */
	 fprintf( stderr, "%s: upload multi-texture failure, sz0=%d sz1=%d\n",
		  __FUNCTION__, t0->base.totalSize, t1->base.totalSize );
	 exit(-1);
      }

      /* Set the base offset of the texture image */
      t0->bufAddr = mmesa->mach64Screen->texOffset[heap] + t0->base.memBlock->ofs;
      t1->bufAddr = mmesa->mach64Screen->texOffset[heap] + t1->base.memBlock->ofs;

      /* Force loading the new state into the hardware */
      mmesa->dirty |= (MACH64_UPLOAD_SCALE_3D_CNTL |
		       MACH64_UPLOAD_TEXTURE);
   }

   /* Let the world know we've used this memory recently */
   driUpdateTextureLRU( (driTextureObject *) t0 );
   driUpdateTextureLRU( (driTextureObject *) t1 );

   /* Upload any images that are new */
   if ( t0->base.dirty_images[0] ) {
      const GLint j0 = t0->base.tObj->BaseLevel;
      if (t0->heap == MACH64_AGP_HEAP) {
	 /* Need to make sure any vertex buffers in the queue complete */
	 mach64WaitForIdleLocked( mmesa );
	 mach64UploadAGPSubImage( mmesa, t0, j0, 0, 0,
				    t0->base.tObj->Image[0][j0]->Width,
				    t0->base.tObj->Image[0][j0]->Height );
      } else {
	 mach64UploadLocalSubImage( mmesa, t0, j0, 0, 0,
				    t0->base.tObj->Image[0][j0]->Width,
				    t0->base.tObj->Image[0][j0]->Height );
      }
      mmesa->setup.tex_cntl |= MACH64_TEX_CACHE_FLUSH;
      t0->base.dirty_images[0] = 0;
   }
   if ( t1->base.dirty_images[0] ) {
      const GLint j1 = t1->base.tObj->BaseLevel;
      if (t1->heap == MACH64_AGP_HEAP) {
	 /* Need to make sure any vertex buffers in the queue complete */
	 mach64WaitForIdleLocked( mmesa );
	 mach64UploadAGPSubImage( mmesa, t1, j1, 0, 0,
			       t1->base.tObj->Image[0][j1]->Width,
			       t1->base.tObj->Image[0][j1]->Height );
      } else {
	 mach64UploadLocalSubImage( mmesa, t1, j1, 0, 0,
			       t1->base.tObj->Image[0][j1]->Width,
			       t1->base.tObj->Image[0][j1]->Height );
      }
      
      mmesa->setup.tex_cntl |= MACH64_TEX_CACHE_FLUSH;
      t1->base.dirty_images[0] = 0;
   }

   mmesa->dirty |= MACH64_UPLOAD_TEXTURE;
}