Remove CVS keywords.
[mesa.git] / src / mesa / drivers / dri / common / texmem.c
1 /*
2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * (C) Copyright IBM Corporation 2002, 2003
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Ian Romanick <idr@us.ibm.com>
27 * Keith Whitwell <keithw@tungstengraphics.com>
28 * Kevin E. Martin <kem@users.sourceforge.net>
29 * Gareth Hughes <gareth@nvidia.com>
30 */
31
32 /** \file texmem.c
33 * Implements all of the device-independent texture memory management.
34 *
35 * Currently, only a simple LRU texture memory management policy is
36 * implemented. In the (hopefully very near) future, better policies will be
37 * implemented. The idea is that the DRI should be able to run in one of two
38 * modes. In the default mode the DRI will dynamically attempt to discover
39 * the best texture management policy for the running application. In the
40 * other mode, the user (via some sort of as yet TBD mechanism) will select
41 * a texture management policy that is known to work well with the
42 * application.
43 */
44
45 #include "texmem.h"
46 #include "simple_list.h"
47 #include "imports.h"
48 #include "macros.h"
49 #include "texformat.h"
50
51 #include <assert.h>
52
53
54
55 static unsigned dummy_swap_counter;
56
57
58 /**
59 * Calculate \f$\log_2\f$ of a value. This is a particularly poor
60 * implementation of this function. However, since system performance is in
61 * no way dependent on this function, the slowness of the implementation is
62 * irrelevent.
63 *
64 * \param n Value whose \f$\log_2\f$ is to be calculated
65 */
66
67 static GLuint
68 driLog2( GLuint n )
69 {
70 GLuint log2;
71
72 for ( log2 = 1 ; n > 1 ; log2++ ) {
73 n >>= 1;
74 }
75
76 return log2;
77 }
78
79
80
81
82 /**
83 * Determine if a texture is resident in textureable memory. Depending on
84 * the driver, this may or may not be on-card memory. It could be AGP memory
85 * or anyother type of memory from which the hardware can directly read
86 * texels.
87 *
88 * This function is intended to be used as the \c IsTextureResident function
89 * in the device's \c dd_function_table.
90 *
91 * \param ctx GL context pointer (currently unused)
92 * \param texObj Texture object to be tested
93 */
94
95 GLboolean
96 driIsTextureResident( GLcontext * ctx,
97 struct gl_texture_object * texObj )
98 {
99 driTextureObject * t;
100
101
102 t = (driTextureObject *) texObj->DriverData;
103 return( (t != NULL) && (t->memBlock != NULL) );
104 }
105
106
107
108
109 /**
110 * (Re)initialize the global circular LRU list. The last element
111 * in the array (\a heap->nrRegions) is the sentinal. Keeping it
112 * at the end of the array allows the other elements of the array
113 * to be addressed rationally when looking up objects at a particular
114 * location in texture memory.
115 *
116 * \param heap Texture heap to be reset
117 */
118
119 static void resetGlobalLRU( driTexHeap * heap )
120 {
121 drmTextureRegionPtr list = heap->global_regions;
122 unsigned sz = 1U << heap->logGranularity;
123 unsigned i;
124
125 for (i = 0 ; (i+1) * sz <= heap->size ; i++) {
126 list[i].prev = i-1;
127 list[i].next = i+1;
128 list[i].age = 0;
129 }
130
131 i--;
132 list[0].prev = heap->nrRegions;
133 list[i].prev = i-1;
134 list[i].next = heap->nrRegions;
135 list[heap->nrRegions].prev = i;
136 list[heap->nrRegions].next = 0;
137 heap->global_age[0] = 0;
138 }
139
140 /**
141 * Print out debugging information about the local texture LRU.
142 *
143 * \param heap Texture heap to be printed
144 * \param callername Name of calling function
145 */
146 static void printLocalLRU( driTexHeap * heap, const char *callername )
147 {
148 driTextureObject *t;
149 unsigned sz = 1U << heap->logGranularity;
150
151 fprintf( stderr, "%s in %s:\nLocal LRU, heap %d:\n",
152 __FUNCTION__, callername, heap->heapId );
153
154 foreach ( t, &heap->texture_objects ) {
155 if (!t->memBlock)
156 continue;
157 if (!t->tObj) {
158 fprintf( stderr, "Placeholder (%p) %d at 0x%x sz 0x%x\n",
159 (void *)t,
160 t->memBlock->ofs / sz,
161 t->memBlock->ofs,
162 t->memBlock->size );
163 } else {
164 fprintf( stderr, "Texture (%p) at 0x%x sz 0x%x\n",
165 (void *)t,
166 t->memBlock->ofs,
167 t->memBlock->size );
168 }
169 }
170 foreach ( t, heap->swapped_objects ) {
171 if (!t->tObj) {
172 fprintf( stderr, "Swapped Placeholder (%p)\n", (void *)t );
173 } else {
174 fprintf( stderr, "Swapped Texture (%p)\n", (void *)t );
175 }
176 }
177
178 fprintf( stderr, "\n" );
179 }
180
181 /**
182 * Print out debugging information about the global texture LRU.
183 *
184 * \param heap Texture heap to be printed
185 * \param callername Name of calling function
186 */
187 static void printGlobalLRU( driTexHeap * heap, const char *callername )
188 {
189 drmTextureRegionPtr list = heap->global_regions;
190 unsigned int i, j;
191
192 fprintf( stderr, "%s in %s:\nGlobal LRU, heap %d list %p:\n",
193 __FUNCTION__, callername, heap->heapId, (void *)list );
194
195 for ( i = 0, j = heap->nrRegions ; i < heap->nrRegions ; i++ ) {
196 fprintf( stderr, "list[%d] age %d next %d prev %d in_use %d\n",
197 j, list[j].age, list[j].next, list[j].prev, list[j].in_use );
198 j = list[j].next;
199 if ( j == heap->nrRegions ) break;
200 }
201
202 if ( j != heap->nrRegions ) {
203 fprintf( stderr, "Loop detected in global LRU\n" );
204 for ( i = 0 ; i < heap->nrRegions ; i++ ) {
205 fprintf( stderr, "list[%d] age %d next %d prev %d in_use %d\n",
206 i, list[i].age, list[i].next, list[i].prev, list[i].in_use );
207 }
208 }
209
210 fprintf( stderr, "\n" );
211 }
212
213
214 /**
215 * Called by the client whenever it touches a local texture.
216 *
217 * \param t Texture object that the client has accessed
218 */
219
220 void driUpdateTextureLRU( driTextureObject * t )
221 {
222 driTexHeap * heap;
223 drmTextureRegionPtr list;
224 unsigned shift;
225 unsigned start;
226 unsigned end;
227 unsigned i;
228
229
230 heap = t->heap;
231 if ( heap != NULL ) {
232 shift = heap->logGranularity;
233 start = t->memBlock->ofs >> shift;
234 end = (t->memBlock->ofs + t->memBlock->size - 1) >> shift;
235
236
237 heap->local_age = ++heap->global_age[0];
238 list = heap->global_regions;
239
240
241 /* Update the context's local LRU
242 */
243
244 move_to_head( & heap->texture_objects, t );
245
246
247 for (i = start ; i <= end ; i++) {
248 list[i].age = heap->local_age;
249
250 /* remove_from_list(i)
251 */
252 list[(unsigned)list[i].next].prev = list[i].prev;
253 list[(unsigned)list[i].prev].next = list[i].next;
254
255 /* insert_at_head(list, i)
256 */
257 list[i].prev = heap->nrRegions;
258 list[i].next = list[heap->nrRegions].next;
259 list[(unsigned)list[heap->nrRegions].next].prev = i;
260 list[heap->nrRegions].next = i;
261 }
262
263 if ( 0 ) {
264 printGlobalLRU( heap, __FUNCTION__ );
265 printLocalLRU( heap, __FUNCTION__ );
266 }
267 }
268 }
269
270
271
272
273 /**
274 * Keep track of swapped out texture objects.
275 *
276 * \param t Texture object to be "swapped" out of its texture heap
277 */
278
279 void driSwapOutTextureObject( driTextureObject * t )
280 {
281 unsigned face;
282
283
284 if ( t->memBlock != NULL ) {
285 assert( t->heap != NULL );
286 mmFreeMem( t->memBlock );
287 t->memBlock = NULL;
288
289 if (t->timestamp > t->heap->timestamp)
290 t->heap->timestamp = t->timestamp;
291
292 t->heap->texture_swaps[0]++;
293 move_to_tail( t->heap->swapped_objects, t );
294 t->heap = NULL;
295 }
296 else {
297 assert( t->heap == NULL );
298 }
299
300
301 for ( face = 0 ; face < 6 ; face++ ) {
302 t->dirty_images[face] = ~0;
303 }
304 }
305
306
307
308
309 /**
310 * Destroy hardware state associated with texture \a t. Calls the
311 * \a destroy_texture_object method associated with the heap from which
312 * \a t was allocated.
313 *
314 * \param t Texture object to be destroyed
315 */
316
317 void driDestroyTextureObject( driTextureObject * t )
318 {
319 driTexHeap * heap;
320
321
322 if ( 0 ) {
323 fprintf( stderr, "[%s:%d] freeing %p (tObj = %p, DriverData = %p)\n",
324 __FILE__, __LINE__,
325 (void *)t,
326 (void *)((t != NULL) ? t->tObj : NULL),
327 (void *)((t != NULL && t->tObj != NULL) ? t->tObj->DriverData : NULL ));
328 }
329
330 if ( t != NULL ) {
331 if ( t->memBlock ) {
332 heap = t->heap;
333 assert( heap != NULL );
334
335 heap->texture_swaps[0]++;
336
337 mmFreeMem( t->memBlock );
338 t->memBlock = NULL;
339
340 if (t->timestamp > t->heap->timestamp)
341 t->heap->timestamp = t->timestamp;
342
343 heap->destroy_texture_object( heap->driverContext, t );
344 t->heap = NULL;
345 }
346
347 if ( t->tObj != NULL ) {
348 assert( t->tObj->DriverData == t );
349 t->tObj->DriverData = NULL;
350 }
351
352 remove_from_list( t );
353 FREE( t );
354 }
355
356 if ( 0 ) {
357 fprintf( stderr, "[%s:%d] done freeing %p\n", __FILE__, __LINE__, (void *)t );
358 }
359 }
360
361
362
363
364 /**
365 * Update the local heap's representation of texture memory based on
366 * data in the SAREA. This is done each time it is detected that some other
367 * direct rendering client has held the lock. This pertains to both our local
368 * textures and the textures belonging to other clients. Keep track of other
369 * client's textures by pushing a placeholder texture onto the LRU list --
370 * these are denoted by \a tObj being \a NULL.
371 *
372 * \param heap Heap whose state is to be updated
373 * \param offset Byte offset in the heap that has been stolen
374 * \param size Size, in bytes, of the stolen block
375 * \param in_use Non-zero if the block is pinned/reserved by the kernel
376 */
377
378 static void driTexturesGone( driTexHeap * heap, int offset, int size,
379 int in_use )
380 {
381 driTextureObject * t;
382 driTextureObject * tmp;
383
384
385 foreach_s ( t, tmp, & heap->texture_objects ) {
386 if ( (t->memBlock->ofs < (offset + size))
387 && ((t->memBlock->ofs + t->memBlock->size) > offset) ) {
388 /* It overlaps - kick it out. If the texture object is just a
389 * place holder, then destroy it all together. Otherwise, mark
390 * it as being swapped out.
391 */
392
393 if ( t->tObj != NULL ) {
394 driSwapOutTextureObject( t );
395 }
396 else {
397 driDestroyTextureObject( t );
398 }
399 }
400 }
401
402
403 {
404 t = (driTextureObject *) CALLOC( heap->texture_object_size );
405 if ( t == NULL ) return;
406
407 t->memBlock = mmAllocMem( heap->memory_heap, size, 0, offset );
408 if ( t->memBlock == NULL ) {
409 fprintf( stderr, "Couldn't alloc placeholder: heap %u sz %x ofs %x\n", heap->heapId,
410 (int)size, (int)offset );
411 mmDumpMemInfo( heap->memory_heap );
412 FREE(t);
413 return;
414 }
415 t->heap = heap;
416 if (in_use)
417 t->reserved = 1;
418 insert_at_head( & heap->texture_objects, t );
419 }
420 }
421
422
423
424
425 /**
426 * Called by the client on lock contention to determine whether textures have
427 * been stolen. If another client has modified a region in which we have
428 * textures, then we need to figure out which of our textures have been
429 * removed and update our global LRU.
430 *
431 * \param heap Texture heap to be updated
432 */
433
434 void driAgeTextures( driTexHeap * heap )
435 {
436 drmTextureRegionPtr list = heap->global_regions;
437 unsigned sz = 1U << (heap->logGranularity);
438 unsigned i, nr = 0;
439
440
441 /* Have to go right round from the back to ensure stuff ends up
442 * LRU in the local list... Fix with a cursor pointer.
443 */
444
445 for (i = list[heap->nrRegions].prev ;
446 i != heap->nrRegions && nr < heap->nrRegions ;
447 i = list[i].prev, nr++) {
448 /* If switching texturing schemes, then the SAREA might not have been
449 * properly cleared, so we need to reset the global texture LRU.
450 */
451
452 if ( (i * sz) > heap->size ) {
453 nr = heap->nrRegions;
454 break;
455 }
456
457 if (list[i].age > heap->local_age)
458 driTexturesGone( heap, i * sz, sz, list[i].in_use);
459 }
460
461 /* Loop or uninitialized heap detected. Reset.
462 */
463
464 if (nr == heap->nrRegions) {
465 driTexturesGone( heap, 0, heap->size, 0);
466 resetGlobalLRU( heap );
467 }
468
469 if ( 0 ) {
470 printGlobalLRU( heap, __FUNCTION__ );
471 printLocalLRU( heap, __FUNCTION__ );
472 }
473
474 heap->local_age = heap->global_age[0];
475 }
476
477
478
479
480 #define INDEX_ARRAY_SIZE 6 /* I'm not aware of driver with more than 2 heaps */
481
482 /**
483 * Allocate memory from a texture heap to hold a texture object. This
484 * routine will attempt to allocate memory for the texture from the heaps
485 * specified by \c heap_array in order. That is, first it will try to
486 * allocate from \c heap_array[0], then \c heap_array[1], and so on.
487 *
488 * \param heap_array Array of pointers to texture heaps to use
489 * \param nr_heaps Number of heap pointer in \a heap_array
490 * \param t Texture object for which space is needed
491 * \return The ID of the heap from which memory was allocated, or -1 if
492 * memory could not be allocated.
493 *
494 * \bug The replacement policy implemented by this function is horrible.
495 */
496
497
498 int
499 driAllocateTexture( driTexHeap * const * heap_array, unsigned nr_heaps,
500 driTextureObject * t )
501 {
502 driTexHeap * heap;
503 driTextureObject * temp;
504 driTextureObject * cursor;
505 unsigned id;
506
507
508 /* In case it already has texture space, initialize heap. This also
509 * prevents GCC from issuing a warning that heap might be used
510 * uninitialized.
511 */
512
513 heap = t->heap;
514
515
516 /* Run through each of the existing heaps and try to allocate a buffer
517 * to hold the texture.
518 */
519
520 for ( id = 0 ; (t->memBlock == NULL) && (id < nr_heaps) ; id++ ) {
521 heap = heap_array[ id ];
522 if ( heap != NULL ) {
523 t->memBlock = mmAllocMem( heap->memory_heap, t->totalSize,
524 heap->alignmentShift, 0 );
525 }
526 }
527
528
529 /* Kick textures out until the requested texture fits.
530 */
531
532 if ( t->memBlock == NULL ) {
533 unsigned index[INDEX_ARRAY_SIZE];
534 unsigned nrGoodHeaps = 0;
535
536 /* Trying to avoid dynamic memory allocation. If you have more
537 * heaps, increase INDEX_ARRAY_SIZE. I'm not aware of any
538 * drivers with more than 2 tex heaps. */
539 assert( nr_heaps < INDEX_ARRAY_SIZE );
540
541 /* Sort large enough heaps by duty. Insertion sort should be
542 * fast enough for such a short array. */
543 for ( id = 0 ; id < nr_heaps ; id++ ) {
544 heap = heap_array[ id ];
545
546 if ( heap != NULL && t->totalSize <= heap->size ) {
547 unsigned j;
548
549 for ( j = 0 ; j < nrGoodHeaps; j++ ) {
550 if ( heap->duty > heap_array[ index[ j ] ]->duty )
551 break;
552 }
553
554 if ( j < nrGoodHeaps ) {
555 memmove( &index[ j+1 ], &index[ j ],
556 sizeof(index[ 0 ]) * (nrGoodHeaps - j) );
557 }
558
559 index[ j ] = id;
560
561 nrGoodHeaps++;
562 }
563 }
564
565 for ( id = 0 ; (t->memBlock == NULL) && (id < nrGoodHeaps) ; id++ ) {
566 heap = heap_array[ index[ id ] ];
567
568 for ( cursor = heap->texture_objects.prev, temp = cursor->prev;
569 cursor != &heap->texture_objects ;
570 cursor = temp, temp = cursor->prev ) {
571
572 /* The the LRU element. If the texture is bound to one of
573 * the texture units, then we cannot kick it out.
574 */
575 if ( cursor->bound || cursor->reserved ) {
576 continue;
577 }
578
579 if ( cursor->memBlock )
580 heap->duty -= cursor->memBlock->size;
581
582 /* If this is a placeholder, there's no need to keep it */
583 if (cursor->tObj)
584 driSwapOutTextureObject( cursor );
585 else
586 driDestroyTextureObject( cursor );
587
588 t->memBlock = mmAllocMem( heap->memory_heap, t->totalSize,
589 heap->alignmentShift, 0 );
590
591 if (t->memBlock)
592 break;
593 }
594 }
595
596 /* Rebalance duties. If a heap kicked more data than its duty,
597 * then all other heaps get that amount multiplied with their
598 * relative weight added to their duty. The negative duty is
599 * reset to 0. In the end all heaps have a duty >= 0.
600 *
601 * CAUTION: we must not change the heap pointer here, because it
602 * is used below to update the texture object.
603 */
604 for ( id = 0 ; id < nr_heaps ; id++ )
605 if ( heap_array[ id ] != NULL && heap_array[ id ]->duty < 0) {
606 int duty = -heap_array[ id ]->duty;
607 double weight = heap_array[ id ]->weight;
608 unsigned j;
609
610 for ( j = 0 ; j < nr_heaps ; j++ )
611 if ( j != id && heap_array[ j ] != NULL ) {
612 heap_array[ j ]->duty += (double) duty *
613 heap_array[ j ]->weight / weight;
614 }
615
616 heap_array[ id ]->duty = 0;
617 }
618 }
619
620
621 if ( t->memBlock != NULL ) {
622 /* id and heap->heapId may or may not be the same value here.
623 */
624
625 assert( heap != NULL );
626 assert( (t->heap == NULL) || (t->heap == heap) );
627
628 t->heap = heap;
629 return heap->heapId;
630 }
631 else {
632 assert( t->heap == NULL );
633
634 fprintf( stderr, "[%s:%d] unable to allocate texture\n",
635 __FUNCTION__, __LINE__ );
636 return -1;
637 }
638 }
639
640
641
642
643
644
645 /**
646 * Set the location where the texture-swap counter is stored.
647 */
648
649 void
650 driSetTextureSwapCounterLocation( driTexHeap * heap, unsigned * counter )
651 {
652 heap->texture_swaps = (counter == NULL) ? & dummy_swap_counter : counter;
653 }
654
655
656
657
658 /**
659 * Create a new heap for texture data.
660 *
661 * \param heap_id Device-dependent heap identifier. This value
662 * will returned by driAllocateTexture when memory
663 * is allocated from this heap.
664 * \param context Device-dependent driver context. This is
665 * supplied as the first parameter to the
666 * \c destroy_tex_obj function.
667 * \param size Size, in bytes, of the texture region
668 * \param alignmentShift Alignment requirement for textures. If textures
669 * must be allocated on a 4096 byte boundry, this
670 * would be 12.
671 * \param nr_regions Number of regions into which this texture space
672 * should be partitioned
673 * \param global_regions Array of \c drmTextureRegion structures in the SAREA
674 * \param global_age Pointer to the global texture age in the SAREA
675 * \param swapped_objects Pointer to the list of texture objects that are
676 * not in texture memory (i.e., have been swapped
677 * out).
678 * \param texture_object_size Size, in bytes, of a device-dependent texture
679 * object
680 * \param destroy_tex_obj Function used to destroy a device-dependent
681 * texture object
682 *
683 * \sa driDestroyTextureHeap
684 */
685
686 driTexHeap *
687 driCreateTextureHeap( unsigned heap_id, void * context, unsigned size,
688 unsigned alignmentShift, unsigned nr_regions,
689 drmTextureRegionPtr global_regions, unsigned * global_age,
690 driTextureObject * swapped_objects,
691 unsigned texture_object_size,
692 destroy_texture_object_t * destroy_tex_obj
693 )
694 {
695 driTexHeap * heap;
696 unsigned l;
697
698
699 if ( 0 )
700 fprintf( stderr, "%s( %u, %p, %u, %u, %u )\n",
701 __FUNCTION__,
702 heap_id, (void *)context, size, alignmentShift, nr_regions );
703
704 heap = (driTexHeap *) CALLOC( sizeof( driTexHeap ) );
705 if ( heap != NULL ) {
706 l = driLog2( (size - 1) / nr_regions );
707 if ( l < alignmentShift )
708 {
709 l = alignmentShift;
710 }
711
712 heap->logGranularity = l;
713 heap->size = size & ~((1L << l) - 1);
714
715 heap->memory_heap = mmInit( 0, heap->size );
716 if ( heap->memory_heap != NULL ) {
717 heap->heapId = heap_id;
718 heap->driverContext = context;
719
720 heap->alignmentShift = alignmentShift;
721 heap->nrRegions = nr_regions;
722 heap->global_regions = global_regions;
723 heap->global_age = global_age;
724 heap->swapped_objects = swapped_objects;
725 heap->texture_object_size = texture_object_size;
726 heap->destroy_texture_object = destroy_tex_obj;
727
728 /* Force global heap init */
729 if (heap->global_age[0] == 0)
730 heap->local_age = ~0;
731 else
732 heap->local_age = 0;
733
734 make_empty_list( & heap->texture_objects );
735 driSetTextureSwapCounterLocation( heap, NULL );
736
737 heap->weight = heap->size;
738 heap->duty = 0;
739 }
740 else {
741 FREE( heap );
742 heap = NULL;
743 }
744 }
745
746
747 if ( 0 )
748 fprintf( stderr, "%s returning %p\n", __FUNCTION__, (void *)heap );
749
750 return heap;
751 }
752
753
754
755
756 /** Destroys a texture heap
757 *
758 * \param heap Texture heap to be destroyed
759 */
760
761 void
762 driDestroyTextureHeap( driTexHeap * heap )
763 {
764 driTextureObject * t;
765 driTextureObject * temp;
766
767
768 if ( heap != NULL ) {
769 foreach_s( t, temp, & heap->texture_objects ) {
770 driDestroyTextureObject( t );
771 }
772 foreach_s( t, temp, heap->swapped_objects ) {
773 driDestroyTextureObject( t );
774 }
775
776 mmDestroy( heap->memory_heap );
777 FREE( heap );
778 }
779 }
780
781
782
783
784 /****************************************************************************/
785 /**
786 * Determine how many texels (including all mipmap levels) would be required
787 * for a texture map of size \f$2^^\c base_size_log2\f$ would require.
788 *
789 * \param base_size_log2 \f$log_2\f$ of the size of a side of the texture
790 * \param dimensions Number of dimensions of the texture. Either 2 or 3.
791 * \param faces Number of faces of the texture. Either 1 or 6 (for cube maps).
792 * \return Number of texels
793 */
794
795 static unsigned
796 texels_this_map_size( int base_size_log2, unsigned dimensions, unsigned faces )
797 {
798 unsigned texels;
799
800
801 assert( (faces == 1) || (faces == 6) );
802 assert( (dimensions == 2) || (dimensions == 3) );
803
804 texels = 0;
805 if ( base_size_log2 >= 0 ) {
806 texels = (1U << (dimensions * base_size_log2));
807
808 /* See http://www.mail-archive.com/dri-devel@lists.sourceforge.net/msg03636.html
809 * for the complete explaination of why this formulation is used.
810 * Basically, the smaller mipmap levels sum to 0.333 the size of the
811 * level 0 map. The total size is therefore the size of the map
812 * multipled by 1.333. The +2 is there to round up.
813 */
814
815 texels = (texels * 4 * faces + 2) / 3;
816 }
817
818 return texels;
819 }
820
821
822
823
824 struct maps_per_heap {
825 unsigned c[32];
826 };
827
828 static void
829 fill_in_maximums( driTexHeap * const * heaps, unsigned nr_heaps,
830 unsigned max_bytes_per_texel, unsigned max_size,
831 unsigned mipmaps_at_once, unsigned dimensions,
832 unsigned faces, struct maps_per_heap * max_textures )
833 {
834 unsigned heap;
835 unsigned log2_size;
836 unsigned mask;
837
838
839 /* Determine how many textures of each size can be stored in each
840 * texture heap.
841 */
842
843 for ( heap = 0 ; heap < nr_heaps ; heap++ ) {
844 if ( heaps[ heap ] == NULL ) {
845 (void) memset( max_textures[ heap ].c, 0,
846 sizeof( max_textures[ heap ].c ) );
847 continue;
848 }
849
850 mask = (1U << heaps[ heap ]->logGranularity) - 1;
851
852 if ( 0 ) {
853 fprintf( stderr, "[%s:%d] heap[%u] = %u bytes, mask = 0x%08x\n",
854 __FILE__, __LINE__,
855 heap, heaps[ heap ]->size, mask );
856 }
857
858 for ( log2_size = max_size ; log2_size > 0 ; log2_size-- ) {
859 unsigned total;
860
861
862 /* Determine the total number of bytes required by a texture of
863 * size log2_size.
864 */
865
866 total = texels_this_map_size( log2_size, dimensions, faces )
867 - texels_this_map_size( log2_size - mipmaps_at_once,
868 dimensions, faces );
869 total *= max_bytes_per_texel;
870 total = (total + mask) & ~mask;
871
872 /* The number of textures of a given size that will fit in a heap
873 * is equal to the size of the heap divided by the size of the
874 * texture.
875 */
876
877 max_textures[ heap ].c[ log2_size ] = heaps[ heap ]->size / total;
878
879 if ( 0 ) {
880 fprintf( stderr, "[%s:%d] max_textures[%u].c[%02u] "
881 "= 0x%08x / 0x%08x "
882 "= %u (%u)\n",
883 __FILE__, __LINE__,
884 heap, log2_size,
885 heaps[ heap ]->size, total,
886 heaps[ heap ]->size / total,
887 max_textures[ heap ].c[ log2_size ] );
888 }
889 }
890 }
891 }
892
893
894 static unsigned
895 get_max_size( unsigned nr_heaps,
896 unsigned texture_units,
897 unsigned max_size,
898 int all_textures_one_heap,
899 struct maps_per_heap * max_textures )
900 {
901 unsigned heap;
902 unsigned log2_size;
903
904
905 /* Determine the largest texture size such that a texture of that size
906 * can be bound to each texture unit at the same time. Some hardware
907 * may require that all textures be in the same texture heap for
908 * multitexturing.
909 */
910
911 for ( log2_size = max_size ; log2_size > 0 ; log2_size-- ) {
912 unsigned total = 0;
913
914 for ( heap = 0 ; heap < nr_heaps ; heap++ )
915 {
916 total += max_textures[ heap ].c[ log2_size ];
917
918 if ( 0 ) {
919 fprintf( stderr, "[%s:%d] max_textures[%u].c[%02u] = %u, "
920 "total = %u\n", __FILE__, __LINE__, heap, log2_size,
921 max_textures[ heap ].c[ log2_size ], total );
922 }
923
924 if ( (max_textures[ heap ].c[ log2_size ] >= texture_units)
925 || (!all_textures_one_heap && (total >= texture_units)) ) {
926 /* The number of mipmap levels is the log-base-2 of the
927 * maximum texture size plus 1. If the maximum texture size
928 * is 1x1, the log-base-2 is 0 and 1 mipmap level (the base
929 * level) is available.
930 */
931
932 return log2_size + 1;
933 }
934 }
935 }
936
937 /* This should NEVER happen. It should always be possible to have at
938 * *least* a 1x1 texture in memory!
939 */
940 assert( log2_size != 0 );
941 return 0;
942 }
943
944 #define SET_MAX(f,v) \
945 do { if ( max_sizes[v] != 0 ) { limits-> f = max_sizes[v]; } } while( 0 )
946
947 #define SET_MAX_RECT(f,v) \
948 do { if ( max_sizes[v] != 0 ) { limits-> f = 1 << (max_sizes[v] - 1); } } while( 0 )
949
950
951 /**
952 * Given the amount of texture memory, the number of texture units, and the
953 * maximum size of a texel, calculate the maximum texture size the driver can
954 * advertise.
955 *
956 * \param heaps Texture heaps for this card
957 * \param nr_heap Number of texture heaps
958 * \param limits OpenGL contants. MaxTextureUnits must be set.
959 * \param max_bytes_per_texel Maximum size of a single texel, in bytes
960 * \param max_2D_size \f$\log_2\f$ of the maximum 2D texture size (i.e.,
961 * 1024x1024 textures, this would be 10)
962 * \param max_3D_size \f$\log_2\f$ of the maximum 3D texture size (i.e.,
963 * 1024x1024x1024 textures, this would be 10)
964 * \param max_cube_size \f$\log_2\f$ of the maximum cube texture size (i.e.,
965 * 1024x1024 textures, this would be 10)
966 * \param max_rect_size \f$\log_2\f$ of the maximum texture rectangle size
967 * (i.e., 1024x1024 textures, this would be 10). This is a power-of-2
968 * even though texture rectangles need not be a power-of-2.
969 * \param mipmaps_at_once Total number of mipmaps that can be used
970 * at one time. For most hardware this will be \f$\c max_size + 1\f$.
971 * For hardware that does not support mipmapping, this will be 1.
972 * \param all_textures_one_heap True if the hardware requires that all
973 * textures be in a single texture heap for multitexturing.
974 * \param allow_larger_textures 0 conservative, 1 calculate limits
975 * so at least one worst-case texture can fit, 2 just use hw limits.
976 */
977
978 void
979 driCalculateMaxTextureLevels( driTexHeap * const * heaps,
980 unsigned nr_heaps,
981 struct gl_constants * limits,
982 unsigned max_bytes_per_texel,
983 unsigned max_2D_size,
984 unsigned max_3D_size,
985 unsigned max_cube_size,
986 unsigned max_rect_size,
987 unsigned mipmaps_at_once,
988 int all_textures_one_heap,
989 int allow_larger_textures )
990 {
991 struct maps_per_heap max_textures[8];
992 unsigned i;
993 const unsigned dimensions[4] = { 2, 3, 2, 2 };
994 const unsigned faces[4] = { 1, 1, 6, 1 };
995 unsigned max_sizes[4];
996 unsigned mipmaps[4];
997
998
999 max_sizes[0] = max_2D_size;
1000 max_sizes[1] = max_3D_size;
1001 max_sizes[2] = max_cube_size;
1002 max_sizes[3] = max_rect_size;
1003
1004 mipmaps[0] = mipmaps_at_once;
1005 mipmaps[1] = mipmaps_at_once;
1006 mipmaps[2] = mipmaps_at_once;
1007 mipmaps[3] = 1;
1008
1009
1010 /* Calculate the maximum number of texture levels in two passes. The
1011 * first pass determines how many textures of each power-of-two size
1012 * (including all mipmap levels for that size) can fit in each texture
1013 * heap. The second pass finds the largest texture size that allows
1014 * a texture of that size to be bound to every texture unit.
1015 */
1016
1017 for ( i = 0 ; i < 4 ; i++ ) {
1018 if ( (allow_larger_textures != 2) && (max_sizes[ i ] != 0) ) {
1019 fill_in_maximums( heaps, nr_heaps, max_bytes_per_texel,
1020 max_sizes[ i ], mipmaps[ i ],
1021 dimensions[ i ], faces[ i ],
1022 max_textures );
1023
1024 max_sizes[ i ] = get_max_size( nr_heaps,
1025 allow_larger_textures == 1 ?
1026 1 : limits->MaxTextureUnits,
1027 max_sizes[ i ],
1028 all_textures_one_heap,
1029 max_textures );
1030 }
1031 else if (max_sizes[ i ] != 0) {
1032 max_sizes[ i ] += 1;
1033 }
1034 }
1035
1036 SET_MAX( MaxTextureLevels, 0 );
1037 SET_MAX( Max3DTextureLevels, 1 );
1038 SET_MAX( MaxCubeTextureLevels, 2 );
1039 SET_MAX_RECT( MaxTextureRectSize, 3 );
1040 }
1041
1042
1043
1044
1045 /**
1046 * Perform initial binding of default textures objects on a per unit, per
1047 * texture target basis.
1048 *
1049 * \param ctx Current OpenGL context
1050 * \param swapped List of swapped-out textures
1051 * \param targets Bit-mask of value texture targets
1052 */
1053
1054 void driInitTextureObjects( GLcontext *ctx, driTextureObject * swapped,
1055 GLuint targets )
1056 {
1057 struct gl_texture_object *texObj;
1058 GLuint tmp = ctx->Texture.CurrentUnit;
1059 unsigned i;
1060
1061
1062 for ( i = 0 ; i < ctx->Const.MaxTextureUnits ; i++ ) {
1063 ctx->Texture.CurrentUnit = i;
1064
1065 if ( (targets & DRI_TEXMGR_DO_TEXTURE_1D) != 0 ) {
1066 texObj = ctx->Texture.Unit[i].Current1D;
1067 ctx->Driver.BindTexture( ctx, GL_TEXTURE_1D, texObj );
1068 move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
1069 }
1070
1071 if ( (targets & DRI_TEXMGR_DO_TEXTURE_2D) != 0 ) {
1072 texObj = ctx->Texture.Unit[i].Current2D;
1073 ctx->Driver.BindTexture( ctx, GL_TEXTURE_2D, texObj );
1074 move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
1075 }
1076
1077 if ( (targets & DRI_TEXMGR_DO_TEXTURE_3D) != 0 ) {
1078 texObj = ctx->Texture.Unit[i].Current3D;
1079 ctx->Driver.BindTexture( ctx, GL_TEXTURE_3D, texObj );
1080 move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
1081 }
1082
1083 if ( (targets & DRI_TEXMGR_DO_TEXTURE_CUBE) != 0 ) {
1084 texObj = ctx->Texture.Unit[i].CurrentCubeMap;
1085 ctx->Driver.BindTexture( ctx, GL_TEXTURE_CUBE_MAP_ARB, texObj );
1086 move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
1087 }
1088
1089 if ( (targets & DRI_TEXMGR_DO_TEXTURE_RECT) != 0 ) {
1090 texObj = ctx->Texture.Unit[i].CurrentRect;
1091 ctx->Driver.BindTexture( ctx, GL_TEXTURE_RECTANGLE_NV, texObj );
1092 move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
1093 }
1094 }
1095
1096 ctx->Texture.CurrentUnit = tmp;
1097 }
1098
1099
1100
1101
1102 /**
1103 * Verify that the specified texture is in the specificed heap.
1104 *
1105 * \param tex Texture to be tested.
1106 * \param heap Texture memory heap to be tested.
1107 * \return True if the texture is in the heap, false otherwise.
1108 */
1109
1110 static GLboolean
1111 check_in_heap( const driTextureObject * tex, const driTexHeap * heap )
1112 {
1113 #if 1
1114 return tex->heap == heap;
1115 #else
1116 driTextureObject * curr;
1117
1118 foreach( curr, & heap->texture_objects ) {
1119 if ( curr == tex ) {
1120 break;
1121 }
1122 }
1123
1124 return curr == tex;
1125 #endif
1126 }
1127
1128
1129
1130 /****************************************************************************/
1131 /**
1132 * Validate the consistency of a set of texture heaps.
1133 * Original version by Keith Whitwell in r200/r200_sanity.c.
1134 */
1135
1136 GLboolean
1137 driValidateTextureHeaps( driTexHeap * const * texture_heaps,
1138 unsigned nr_heaps, const driTextureObject * swapped )
1139 {
1140 driTextureObject *t;
1141 unsigned i;
1142
1143 for ( i = 0 ; i < nr_heaps ; i++ ) {
1144 int last_end = 0;
1145 unsigned textures_in_heap = 0;
1146 unsigned blocks_in_mempool = 0;
1147 const driTexHeap * heap = texture_heaps[i];
1148 const struct mem_block *p = heap->memory_heap;
1149
1150 /* Check each texture object has a MemBlock, and is linked into
1151 * the correct heap.
1152 *
1153 * Check the texobj base address corresponds to the MemBlock
1154 * range. Check the texobj size (recalculate?) fits within
1155 * the MemBlock.
1156 *
1157 * Count the number of texobj's using this heap.
1158 */
1159
1160 foreach ( t, &heap->texture_objects ) {
1161 if ( !check_in_heap( t, heap ) ) {
1162 fprintf( stderr, "%s memory block for texture object @ %p not "
1163 "found in heap #%d\n",
1164 __FUNCTION__, (void *)t, i );
1165 return GL_FALSE;
1166 }
1167
1168
1169 if ( t->totalSize > t->memBlock->size ) {
1170 fprintf( stderr, "%s: Memory block for texture object @ %p is "
1171 "only %u bytes, but %u are required\n",
1172 __FUNCTION__, (void *)t, t->totalSize, t->memBlock->size );
1173 return GL_FALSE;
1174 }
1175
1176 textures_in_heap++;
1177 }
1178
1179 /* Validate the contents of the heap:
1180 * - Ordering
1181 * - Overlaps
1182 * - Bounds
1183 */
1184
1185 while ( p != NULL ) {
1186 if (p->reserved) {
1187 fprintf( stderr, "%s: Block (%08x,%x), is reserved?!\n",
1188 __FUNCTION__, p->ofs, p->size );
1189 return GL_FALSE;
1190 }
1191
1192 if (p->ofs != last_end) {
1193 fprintf( stderr, "%s: blocks_in_mempool = %d, last_end = %d, p->ofs = %d\n",
1194 __FUNCTION__, blocks_in_mempool, last_end, p->ofs );
1195 return GL_FALSE;
1196 }
1197
1198 if (!p->reserved && !p->free) {
1199 blocks_in_mempool++;
1200 }
1201
1202 last_end = p->ofs + p->size;
1203 p = p->next;
1204 }
1205
1206 if (textures_in_heap != blocks_in_mempool) {
1207 fprintf( stderr, "%s: Different number of textures objects (%u) and "
1208 "inuse memory blocks (%u)\n",
1209 __FUNCTION__, textures_in_heap, blocks_in_mempool );
1210 return GL_FALSE;
1211 }
1212
1213 #if 0
1214 fprintf( stderr, "%s: textures_in_heap = %u\n",
1215 __FUNCTION__, textures_in_heap );
1216 #endif
1217 }
1218
1219
1220 /* Check swapped texobj's have zero memblocks
1221 */
1222 i = 0;
1223 foreach ( t, swapped ) {
1224 if ( t->memBlock != NULL ) {
1225 fprintf( stderr, "%s: Swapped texobj %p has non-NULL memblock %p\n",
1226 __FUNCTION__, (void *)t, (void *)t->memBlock );
1227 return GL_FALSE;
1228 }
1229 i++;
1230 }
1231
1232 #if 0
1233 fprintf( stderr, "%s: swapped texture count = %u\n", __FUNCTION__, i );
1234 #endif
1235
1236 return GL_TRUE;
1237 }
1238
1239
1240
1241
1242 /****************************************************************************/
1243 /**
1244 * Compute which mipmap levels that really need to be sent to the hardware.
1245 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1246 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1247 */
1248
1249 void
1250 driCalculateTextureFirstLastLevel( driTextureObject * t )
1251 {
1252 struct gl_texture_object * const tObj = t->tObj;
1253 const struct gl_texture_image * const baseImage =
1254 tObj->Image[0][tObj->BaseLevel];
1255
1256 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1257 * and having firstLevel and lastLevel as signed prevents the need for
1258 * extra sign checks.
1259 */
1260 int firstLevel;
1261 int lastLevel;
1262
1263 /* Yes, this looks overly complicated, but it's all needed.
1264 */
1265
1266 switch (tObj->Target) {
1267 case GL_TEXTURE_1D:
1268 case GL_TEXTURE_2D:
1269 case GL_TEXTURE_3D:
1270 case GL_TEXTURE_CUBE_MAP:
1271 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1272 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1273 */
1274
1275 firstLevel = lastLevel = tObj->BaseLevel;
1276 }
1277 else {
1278 firstLevel = tObj->BaseLevel + (GLint)(tObj->MinLod + 0.5);
1279 firstLevel = MAX2(firstLevel, tObj->BaseLevel);
1280 lastLevel = tObj->BaseLevel + (GLint)(tObj->MaxLod + 0.5);
1281 lastLevel = MAX2(lastLevel, t->tObj->BaseLevel);
1282 lastLevel = MIN2(lastLevel, t->tObj->BaseLevel + baseImage->MaxLog2);
1283 lastLevel = MIN2(lastLevel, t->tObj->MaxLevel);
1284 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */
1285 }
1286 break;
1287 case GL_TEXTURE_RECTANGLE_NV:
1288 case GL_TEXTURE_4D_SGIS:
1289 firstLevel = lastLevel = 0;
1290 break;
1291 default:
1292 return;
1293 }
1294
1295 /* save these values */
1296 t->firstLevel = firstLevel;
1297 t->lastLevel = lastLevel;
1298 }
1299
1300
1301
1302
1303 /**
1304 * \name DRI texture formats. Pointers initialized to either the big- or
1305 * little-endian Mesa formats.
1306 */
1307 /*@{*/
1308 const struct gl_texture_format *_dri_texformat_rgba8888 = NULL;
1309 const struct gl_texture_format *_dri_texformat_argb8888 = NULL;
1310 const struct gl_texture_format *_dri_texformat_rgb565 = NULL;
1311 const struct gl_texture_format *_dri_texformat_argb4444 = NULL;
1312 const struct gl_texture_format *_dri_texformat_argb1555 = NULL;
1313 const struct gl_texture_format *_dri_texformat_al88 = NULL;
1314 const struct gl_texture_format *_dri_texformat_a8 = &_mesa_texformat_a8;
1315 const struct gl_texture_format *_dri_texformat_ci8 = &_mesa_texformat_ci8;
1316 const struct gl_texture_format *_dri_texformat_i8 = &_mesa_texformat_i8;
1317 const struct gl_texture_format *_dri_texformat_l8 = &_mesa_texformat_l8;
1318 /*@}*/
1319
1320
1321 /**
1322 * Initialize little endian target, host byte order independent texture formats
1323 */
1324 void
1325 driInitTextureFormats(void)
1326 {
1327 const GLuint ui = 1;
1328 const GLubyte littleEndian = *((const GLubyte *) &ui);
1329
1330 if (littleEndian) {
1331 _dri_texformat_rgba8888 = &_mesa_texformat_rgba8888;
1332 _dri_texformat_argb8888 = &_mesa_texformat_argb8888;
1333 _dri_texformat_rgb565 = &_mesa_texformat_rgb565;
1334 _dri_texformat_argb4444 = &_mesa_texformat_argb4444;
1335 _dri_texformat_argb1555 = &_mesa_texformat_argb1555;
1336 _dri_texformat_al88 = &_mesa_texformat_al88;
1337 }
1338 else {
1339 _dri_texformat_rgba8888 = &_mesa_texformat_rgba8888_rev;
1340 _dri_texformat_argb8888 = &_mesa_texformat_argb8888_rev;
1341 _dri_texformat_rgb565 = &_mesa_texformat_rgb565_rev;
1342 _dri_texformat_argb4444 = &_mesa_texformat_argb4444_rev;
1343 _dri_texformat_argb1555 = &_mesa_texformat_argb1555_rev;
1344 _dri_texformat_al88 = &_mesa_texformat_al88_rev;
1345 }
1346 }