2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * (c) Copyright IBM Corporation 2002
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:
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
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.
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>
34 * Implements all of the device-independent texture memory management.
36 * Currently, only a simple LRU texture memory management policy is
37 * implemented. In the (hopefully very near) future, better policies will be
38 * implemented. The idea is that the DRI should be able to run in one of two
39 * modes. In the default mode the DRI will dynamically attempt to discover
40 * the best texture management policy for the running application. In the
41 * other mode, the user (via some sort of as yet TBD mechanism) will select
42 * a texture management policy that is known to work well with the
47 #include "simple_list.h"
54 static unsigned dummy_swap_counter
;
58 * Calculate \f$\log_2\f$ of a value. This is a particularly poor
59 * implementation of this function. However, since system performance is in
60 * no way dependent on this function, the slowness of the implementation is
63 * \param n Value whose \f$\log_2\f$ is to be calculated
72 for ( log2
= 1 ; n
> 1 ; log2
++ ) {
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
88 * This function is intended to be used as the \c IsTextureResident function
89 * in the device's \c dd_function_table.
91 * \param ctx GL context pointer (currently unused)
92 * \param texObj Texture object to be tested
96 driIsTextureResident( GLcontext
* ctx
,
97 struct gl_texture_object
* texObj
)
102 t
= (driTextureObject
*) texObj
->DriverData
;
103 return( (t
!= NULL
) && (t
->memBlock
!= NULL
) );
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.
116 * \param heap Texture heap to be reset
119 static void resetGlobalLRU( driTexHeap
* heap
)
121 drmTextureRegionPtr list
= heap
->global_regions
;
122 unsigned sz
= 1U << heap
->logGranularity
;
125 for (i
= 0 ; (i
+1) * sz
<= heap
->size
; i
++) {
132 list
[0].prev
= heap
->nrRegions
;
134 list
[i
].next
= heap
->nrRegions
;
135 list
[heap
->nrRegions
].prev
= i
;
136 list
[heap
->nrRegions
].next
= 0;
137 heap
->global_age
[0] = 0;
141 * Print out debugging information about the local texture LRU.
143 * \param heap Texture heap to be printed
144 * \param callername Name of calling function
146 static void printLocalLRU( driTexHeap
* heap
, const char *callername
)
149 unsigned sz
= 1U << heap
->logGranularity
;
151 fprintf( stderr
, "%s in %s:\nLocal LRU, heap %d:\n",
152 __FUNCTION__
, callername
, heap
->heapId
);
154 foreach ( t
, &heap
->texture_objects
) {
158 fprintf( stderr
, "Placeholder (%p) %d at 0x%x sz 0x%x\n",
160 t
->memBlock
->ofs
/ sz
,
164 fprintf( stderr
, "Texture (%p) at 0x%x sz 0x%x\n",
170 foreach ( t
, heap
->swapped_objects
) {
172 fprintf( stderr
, "Swapped Placeholder (%p)\n", t
);
174 fprintf( stderr
, "Swapped Texture (%p)\n", t
);
178 fprintf( stderr
, "\n" );
182 * Print out debugging information about the global texture LRU.
184 * \param heap Texture heap to be printed
185 * \param callername Name of calling function
187 static void printGlobalLRU( driTexHeap
* heap
, const char *callername
)
189 drmTextureRegionPtr list
= heap
->global_regions
;
192 fprintf( stderr
, "%s in %s:\nGlobal LRU, heap %d list %p:\n",
193 __FUNCTION__
, callername
, heap
->heapId
, list
);
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
);
199 if ( j
== heap
->nrRegions
) break;
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
);
210 fprintf( stderr
, "\n" );
215 * Called by the client whenever it touches a local texture.
217 * \param t Texture object that the client has accessed
220 void driUpdateTextureLRU( driTextureObject
* t
)
223 drmTextureRegionPtr list
;
231 if ( heap
!= NULL
) {
232 shift
= heap
->logGranularity
;
233 start
= t
->memBlock
->ofs
>> shift
;
234 end
= (t
->memBlock
->ofs
+ t
->memBlock
->size
- 1) >> shift
;
237 heap
->local_age
= ++heap
->global_age
[0];
238 list
= heap
->global_regions
;
241 /* Update the context's local LRU
244 move_to_head( & heap
->texture_objects
, t
);
247 for (i
= start
; i
<= end
; i
++) {
249 list
[i
].age
= heap
->local_age
;
251 /* remove_from_list(i)
253 list
[(unsigned)list
[i
].next
].prev
= list
[i
].prev
;
254 list
[(unsigned)list
[i
].prev
].next
= list
[i
].next
;
256 /* insert_at_head(list, i)
258 list
[i
].prev
= heap
->nrRegions
;
259 list
[i
].next
= list
[heap
->nrRegions
].next
;
260 list
[(unsigned)list
[heap
->nrRegions
].next
].prev
= i
;
261 list
[heap
->nrRegions
].next
= i
;
265 printGlobalLRU( heap
, __FUNCTION__
);
266 printLocalLRU( heap
, __FUNCTION__
);
275 * Keep track of swapped out texture objects.
277 * \param t Texture object to be "swapped" out of its texture heap
280 void driSwapOutTextureObject( driTextureObject
* t
)
285 if ( t
->memBlock
!= NULL
) {
286 assert( t
->heap
!= NULL
);
287 mmFreeMem( t
->memBlock
);
290 if (t
->timestamp
> t
->heap
->timestamp
)
291 t
->heap
->timestamp
= t
->timestamp
;
293 t
->heap
->texture_swaps
[0]++;
294 move_to_tail( t
->heap
->swapped_objects
, t
);
298 assert( t
->heap
== NULL
);
302 for ( face
= 0 ; face
< 6 ; face
++ ) {
303 t
->dirty_images
[face
] = ~0;
311 * Destroy hardware state associated with texture \a t. Calls the
312 * \a destroy_texture_object method associated with the heap from which
313 * \a t was allocated.
315 * \param t Texture object to be destroyed
318 void driDestroyTextureObject( driTextureObject
* t
)
324 fprintf( stderr
, "[%s:%d] freeing %p (tObj = %p, DriverData = %p)\n",
327 (t
!= NULL
) ? t
->tObj
: NULL
,
328 (t
!= NULL
&& t
->tObj
!= NULL
) ? t
->tObj
->DriverData
: NULL
);
334 assert( heap
!= NULL
);
336 heap
->texture_swaps
[0]++;
338 mmFreeMem( t
->memBlock
);
341 if (t
->timestamp
> t
->heap
->timestamp
)
342 t
->heap
->timestamp
= t
->timestamp
;
344 heap
->destroy_texture_object( heap
->driverContext
, t
);
348 if ( t
->tObj
!= NULL
) {
349 assert( t
->tObj
->DriverData
== t
);
350 t
->tObj
->DriverData
= NULL
;
353 remove_from_list( t
);
358 fprintf( stderr
, "[%s:%d] done freeing %p\n", __FILE__
, __LINE__
, t
);
366 * Update the local heap's representation of texture memory based on
367 * data in the SAREA. This is done each time it is detected that some other
368 * direct rendering client has held the lock. This pertains to both our local
369 * textures and the textures belonging to other clients. Keep track of other
370 * client's textures by pushing a placeholder texture onto the LRU list --
371 * these are denoted by \a tObj being \a NULL.
373 * \param heap Heap whose state is to be updated
374 * \param offset Byte offset in the heap that has been stolen
375 * \param size Size, in bytes, of the stolen block
376 * \param in_use Non-zero if the block is in-use by another context
379 static void driTexturesGone( driTexHeap
* heap
, int offset
, int size
,
382 driTextureObject
* t
;
383 driTextureObject
* tmp
;
386 foreach_s ( t
, tmp
, & heap
->texture_objects
) {
387 if ( (t
->memBlock
->ofs
< (offset
+ size
))
388 && ((t
->memBlock
->ofs
+ t
->memBlock
->size
) > offset
) ) {
389 /* It overlaps - kick it out. If the texture object is just a
390 * place holder, then destroy it all together. Otherwise, mark
391 * it as being swapped out.
394 if ( t
->tObj
!= NULL
) {
395 driSwapOutTextureObject( t
);
399 offset
== t
->memBlock
->ofs
&& size
== t
->memBlock
->size
) {
400 /* Matching placeholder already exists */
403 driDestroyTextureObject( t
);
411 t
= (driTextureObject
*) CALLOC( heap
->texture_object_size
);
412 if ( t
== NULL
) return;
414 t
->memBlock
= mmAllocMem( heap
->memory_heap
, size
, 0, offset
);
415 if ( t
->memBlock
== NULL
) {
416 fprintf( stderr
, "Couldn't alloc placeholder: heap %u sz %x ofs %x\n", heap
->heapId
,
417 (int)size
, (int)offset
);
418 mmDumpMemInfo( heap
->memory_heap
);
422 insert_at_head( & heap
->texture_objects
, t
);
430 * Called by the client on lock contention to determine whether textures have
431 * been stolen. If another client has modified a region in which we have
432 * textures, then we need to figure out which of our textures have been
433 * removed and update our global LRU.
435 * \param heap Texture heap to be updated
438 void driAgeTextures( driTexHeap
* heap
)
440 drmTextureRegionPtr list
= heap
->global_regions
;
441 unsigned sz
= 1U << (heap
->logGranularity
);
445 /* Have to go right round from the back to ensure stuff ends up
446 * LRU in the local list... Fix with a cursor pointer.
449 for (i
= list
[heap
->nrRegions
].prev
;
450 i
!= heap
->nrRegions
&& nr
< heap
->nrRegions
;
451 i
= list
[i
].prev
, nr
++) {
452 /* If switching texturing schemes, then the SAREA might not have been
453 * properly cleared, so we need to reset the global texture LRU.
456 if ( (i
* sz
) > heap
->size
) {
457 nr
= heap
->nrRegions
;
461 if (list
[i
].age
> heap
->local_age
)
462 driTexturesGone( heap
, i
* sz
, sz
, list
[i
].in_use
);
465 /* Loop or uninitialized heap detected. Reset.
468 if (nr
== heap
->nrRegions
) {
469 driTexturesGone( heap
, 0, heap
->size
, 0);
470 resetGlobalLRU( heap
);
474 printGlobalLRU( heap
, __FUNCTION__
);
475 printLocalLRU( heap
, __FUNCTION__
);
478 heap
->local_age
= heap
->global_age
[0];
485 * Allocate memory from a texture heap to hold a texture object. This
486 * routine will attempt to allocate memory for the texture from the heaps
487 * specified by \c heap_array in order. That is, first it will try to
488 * allocate from \c heap_array[0], then \c heap_array[1], and so on.
490 * \param heap_array Array of pointers to texture heaps to use
491 * \param nr_heaps Number of heap pointer in \a heap_array
492 * \param t Texture object for which space is needed
493 * \return The ID of the heap from which memory was allocated, or -1 if
494 * memory could not be allocated.
496 * \bug The replacement policy implemented by this function is horrible.
501 driAllocateTexture( driTexHeap
* const * heap_array
, unsigned nr_heaps
,
502 driTextureObject
* t
)
505 driTextureObject
* temp
;
506 driTextureObject
* cursor
;
510 /* In case it already has texture space, initialize heap. This also
511 * prevents GCC from issuing a warning that heap might be used
518 /* Run through each of the existing heaps and try to allocate a buffer
519 * to hold the texture.
522 for ( id
= 0 ; (t
->memBlock
== NULL
) && (id
< nr_heaps
) ; id
++ ) {
523 heap
= heap_array
[ id
];
524 if ( heap
!= NULL
) {
525 t
->memBlock
= mmAllocMem( heap
->memory_heap
, t
->totalSize
,
526 heap
->alignmentShift
, 0 );
531 /* Kick textures out until the requested texture fits.
534 if ( t
->memBlock
== NULL
) {
535 for ( id
= 0 ; (t
->memBlock
== NULL
) && (id
< nr_heaps
) ; id
++ ) {
536 heap
= heap_array
[ id
];
537 if ( t
->totalSize
<= heap
->size
) {
539 for ( cursor
= heap
->texture_objects
.prev
, temp
= cursor
->prev
;
540 cursor
!= &heap
->texture_objects
;
541 cursor
= temp
, temp
= cursor
->prev
) {
543 /* The the LRU element. If the texture is bound to one of
544 * the texture units, then we cannot kick it out.
546 if ( cursor
->bound
/* || cursor->reserved */ ) {
550 /* If this is a placeholder, there's no need to keep it */
552 driSwapOutTextureObject( cursor
);
554 driDestroyTextureObject( cursor
);
556 t
->memBlock
= mmAllocMem( heap
->memory_heap
, t
->totalSize
,
557 heap
->alignmentShift
, 0 );
562 } /* if ( t->totalSize <= heap->size ) ... */
567 if ( t
->memBlock
!= NULL
) {
568 /* id and heap->heapId may or may not be the same value here.
571 assert( heap
!= NULL
);
572 assert( (t
->heap
== NULL
) || (t
->heap
== heap
) );
578 assert( t
->heap
== NULL
);
580 fprintf( stderr
, "[%s:%d] unable to allocate texture\n",
581 __FUNCTION__
, __LINE__
);
592 * Set the location where the texture-swap counter is stored.
596 driSetTextureSwapCounterLocation( driTexHeap
* heap
, unsigned * counter
)
598 heap
->texture_swaps
= (counter
== NULL
) ? & dummy_swap_counter
: counter
;
605 * Create a new heap for texture data.
607 * \param heap_id Device-dependent heap identifier. This value
608 * will returned by driAllocateTexture when memory
609 * is allocated from this heap.
610 * \param context Device-dependent driver context. This is
611 * supplied as the first parameter to the
612 * \c destroy_tex_obj function.
613 * \param size Size, in bytes, of the texture region
614 * \param alignmentShift Alignment requirement for textures. If textures
615 * must be allocated on a 4096 byte boundry, this
617 * \param nr_regions Number of regions into which this texture space
618 * should be partitioned
619 * \param global_regions Array of \c drmTextureRegion structures in the SAREA
620 * \param global_age Pointer to the global texture age in the SAREA
621 * \param swapped_objects Pointer to the list of texture objects that are
622 * not in texture memory (i.e., have been swapped
624 * \param texture_object_size Size, in bytes, of a device-dependent texture
626 * \param destroy_tex_obj Function used to destroy a device-dependent
629 * \sa driDestroyTextureHeap
633 driCreateTextureHeap( unsigned heap_id
, void * context
, unsigned size
,
634 unsigned alignmentShift
, unsigned nr_regions
,
635 drmTextureRegionPtr global_regions
, unsigned * global_age
,
636 driTextureObject
* swapped_objects
,
637 unsigned texture_object_size
,
638 destroy_texture_object_t
* destroy_tex_obj
646 fprintf( stderr
, "%s( %u, %p, %u, %u, %u )\n",
648 heap_id
, context
, size
, alignmentShift
, nr_regions
);
650 heap
= (driTexHeap
*) CALLOC( sizeof( driTexHeap
) );
651 if ( heap
!= NULL
) {
652 l
= driLog2( (size
- 1) / nr_regions
);
653 if ( l
< alignmentShift
)
658 heap
->logGranularity
= l
;
659 heap
->size
= size
& ~((1L << l
) - 1);
661 heap
->memory_heap
= mmInit( 0, heap
->size
);
662 if ( heap
->memory_heap
!= NULL
) {
663 heap
->heapId
= heap_id
;
664 heap
->driverContext
= context
;
666 heap
->alignmentShift
= alignmentShift
;
667 heap
->nrRegions
= nr_regions
;
668 heap
->global_regions
= global_regions
;
669 heap
->global_age
= global_age
;
670 heap
->swapped_objects
= swapped_objects
;
671 heap
->texture_object_size
= texture_object_size
;
672 heap
->destroy_texture_object
= destroy_tex_obj
;
674 /* Force global heap init */
675 if (heap
->global_age
== 0)
676 heap
->local_age
= ~0;
680 make_empty_list( & heap
->texture_objects
);
681 driSetTextureSwapCounterLocation( heap
, NULL
);
691 fprintf( stderr
, "%s returning %p\n", __FUNCTION__
, heap
);
699 /** Destroys a texture heap
701 * \param heap Texture heap to be destroyed
705 driDestroyTextureHeap( driTexHeap
* heap
)
707 driTextureObject
* t
;
708 driTextureObject
* temp
;
711 if ( heap
!= NULL
) {
712 foreach_s( t
, temp
, & heap
->texture_objects
) {
713 driDestroyTextureObject( t
);
715 foreach_s( t
, temp
, heap
->swapped_objects
) {
716 driDestroyTextureObject( t
);
719 mmDestroy( heap
->memory_heap
);
727 /****************************************************************************/
729 * Determine how many texels (including all mipmap levels) would be required
730 * for a texture map of size \f$2^^\c base_size_log2\f$ would require.
732 * \param base_size_log2 \f$log_2\f$ of the size of a side of the texture
733 * \param dimensions Number of dimensions of the texture. Either 2 or 3.
734 * \param faces Number of faces of the texture. Either 1 or 6 (for cube maps).
735 * \return Number of texels
739 texels_this_map_size( int base_size_log2
, unsigned dimensions
, unsigned faces
)
744 assert( (faces
== 1) || (faces
== 6) );
745 assert( (dimensions
== 2) || (dimensions
== 3) );
748 if ( base_size_log2
>= 0 ) {
749 texels
= (1U << (dimensions
* base_size_log2
));
751 /* See http://www.mail-archive.com/dri-devel@lists.sourceforge.net/msg03636.html
752 * for the complete explaination of why this formulation is used.
753 * Basically, the smaller mipmap levels sum to 0.333 the size of the
754 * level 0 map. The total size is therefore the size of the map
755 * multipled by 1.333. The +2 is there to round up.
758 texels
= (texels
* 4 * faces
+ 2) / 3;
767 struct maps_per_heap
{
772 fill_in_maximums( driTexHeap
* const * heaps
, unsigned nr_heaps
,
773 unsigned max_bytes_per_texel
, unsigned max_size
,
774 unsigned mipmaps_at_once
, unsigned dimensions
,
775 unsigned faces
, struct maps_per_heap
* max_textures
)
782 /* Determine how many textures of each size can be stored in each
786 for ( heap
= 0 ; heap
< nr_heaps
; heap
++ ) {
787 if ( heaps
[ heap
] == NULL
) {
788 (void) memset( max_textures
[ heap
].c
, 0,
789 sizeof( max_textures
[ heap
].c
) );
793 mask
= (1U << heaps
[ heap
]->logGranularity
) - 1;
796 fprintf( stderr
, "[%s:%d] heap[%u] = %u bytes, mask = 0x%08x\n",
798 heap
, heaps
[ heap
]->size
, mask
);
801 for ( log2_size
= max_size
; log2_size
> 0 ; log2_size
-- ) {
805 /* Determine the total number of bytes required by a texture of
809 total
= texels_this_map_size( log2_size
, dimensions
, faces
)
810 - texels_this_map_size( log2_size
- mipmaps_at_once
,
812 total
*= max_bytes_per_texel
;
813 total
= (total
+ mask
) & ~mask
;
815 /* The number of textures of a given size that will fit in a heap
816 * is equal to the size of the heap divided by the size of the
820 max_textures
[ heap
].c
[ log2_size
] = heaps
[ heap
]->size
/ total
;
823 fprintf( stderr
, "[%s:%d] max_textures[%u].c[%02u] "
828 heaps
[ heap
]->size
, total
,
829 heaps
[ heap
]->size
/ total
,
830 max_textures
[ heap
].c
[ log2_size
] );
838 get_max_size( unsigned nr_heaps
,
839 unsigned texture_units
,
841 int all_textures_one_heap
,
842 struct maps_per_heap
* max_textures
)
848 /* Determine the largest texture size such that a texture of that size
849 * can be bound to each texture unit at the same time. Some hardware
850 * may require that all textures be in the same texture heap for
854 for ( log2_size
= max_size
; log2_size
> 0 ; log2_size
-- ) {
857 for ( heap
= 0 ; heap
< nr_heaps
; heap
++ )
859 total
+= max_textures
[ heap
].c
[ log2_size
];
862 fprintf( stderr
, "[%s:%d] max_textures[%u].c[%02u] = %u, "
863 "total = %u\n", __FILE__
, __LINE__
, heap
, log2_size
,
864 max_textures
[ heap
].c
[ log2_size
], total
);
867 if ( (max_textures
[ heap
].c
[ log2_size
] >= texture_units
)
868 || (!all_textures_one_heap
&& (total
>= texture_units
)) ) {
869 /* The number of mipmap levels is the log-base-2 of the
870 * maximum texture size plus 1. If the maximum texture size
871 * is 1x1, the log-base-2 is 0 and 1 mipmap level (the base
872 * level) is available.
875 return log2_size
+ 1;
880 /* This should NEVER happen. It should always be possible to have at
881 * *least* a 1x1 texture in memory!
883 assert( log2_size
!= 0 );
887 #define SET_MAX(f,v) \
888 do { if ( max_sizes[v] != 0 ) { limits-> f = max_sizes[v]; } } while( 0 )
890 #define SET_MAX_RECT(f,v) \
891 do { if ( max_sizes[v] != 0 ) { limits-> f = 1 << max_sizes[v]; } } while( 0 )
895 * Given the amount of texture memory, the number of texture units, and the
896 * maximum size of a texel, calculate the maximum texture size the driver can
899 * \param heaps Texture heaps for this card
900 * \param nr_heap Number of texture heaps
901 * \param limits OpenGL contants. MaxTextureUnits must be set.
902 * \param max_bytes_per_texel Maximum size of a single texel, in bytes
903 * \param max_2D_size \f$\log_2\f$ of the maximum 2D texture size (i.e.,
904 * 1024x1024 textures, this would be 10)
905 * \param max_3D_size \f$\log_2\f$ of the maximum 3D texture size (i.e.,
906 * 1024x1024x1024 textures, this would be 10)
907 * \param max_cube_size \f$\log_2\f$ of the maximum cube texture size (i.e.,
908 * 1024x1024 textures, this would be 10)
909 * \param max_rect_size \f$\log_2\f$ of the maximum texture rectangle size
910 * (i.e., 1024x1024 textures, this would be 10). This is a power-of-2
911 * even though texture rectangles need not be a power-of-2.
912 * \param mipmaps_at_once Total number of mipmaps that can be used
913 * at one time. For most hardware this will be \f$\c max_size + 1\f$.
914 * For hardware that does not support mipmapping, this will be 1.
915 * \param all_textures_one_heap True if the hardware requires that all
916 * textures be in a single texture heap for multitexturing.
920 driCalculateMaxTextureLevels( driTexHeap
* const * heaps
,
922 struct gl_constants
* limits
,
923 unsigned max_bytes_per_texel
,
924 unsigned max_2D_size
,
925 unsigned max_3D_size
,
926 unsigned max_cube_size
,
927 unsigned max_rect_size
,
928 unsigned mipmaps_at_once
,
929 int all_textures_one_heap
)
931 struct maps_per_heap max_textures
[8];
933 const unsigned dimensions
[4] = { 2, 3, 2, 2 };
934 const unsigned faces
[4] = { 1, 1, 6, 1 };
935 unsigned max_sizes
[4];
939 max_sizes
[0] = max_2D_size
;
940 max_sizes
[1] = max_3D_size
;
941 max_sizes
[2] = max_cube_size
;
942 max_sizes
[3] = max_rect_size
;
944 mipmaps
[0] = mipmaps_at_once
;
945 mipmaps
[1] = mipmaps_at_once
;
947 mipmaps
[3] = mipmaps_at_once
;
950 /* Calculate the maximum number of texture levels in two passes. The
951 * first pass determines how many textures of each power-of-two size
952 * (including all mipmap levels for that size) can fit in each texture
953 * heap. The second pass finds the largest texture size that allows
954 * a texture of that size to be bound to every texture unit.
957 for ( i
= 0 ; i
< 4 ; i
++ ) {
958 if ( max_sizes
[ i
] != 0 ) {
959 fill_in_maximums( heaps
, nr_heaps
, max_bytes_per_texel
,
960 max_sizes
[ i
], mipmaps
[ i
],
961 dimensions
[ i
], faces
[ i
],
964 max_sizes
[ i
] = get_max_size( nr_heaps
,
965 limits
->MaxTextureUnits
,
967 all_textures_one_heap
,
972 SET_MAX( MaxTextureLevels
, 0 );
973 SET_MAX( Max3DTextureLevels
, 1 );
974 SET_MAX( MaxCubeTextureLevels
, 2 );
975 SET_MAX_RECT( MaxTextureRectSize
, 3 );
982 * Perform initial binding of default textures objects on a per unit, per
983 * texture target basis.
985 * \param ctx Current OpenGL context
986 * \param swapped List of swapped-out textures
987 * \param targets Bit-mask of value texture targets
990 void driInitTextureObjects( GLcontext
*ctx
, driTextureObject
* swapped
,
993 struct gl_texture_object
*texObj
;
994 GLuint tmp
= ctx
->Texture
.CurrentUnit
;
998 for ( i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++ ) {
999 ctx
->Texture
.CurrentUnit
= i
;
1001 if ( (targets
& DRI_TEXMGR_DO_TEXTURE_1D
) != 0 ) {
1002 texObj
= ctx
->Texture
.Unit
[i
].Current1D
;
1003 ctx
->Driver
.BindTexture( ctx
, GL_TEXTURE_1D
, texObj
);
1004 move_to_tail( swapped
, (driTextureObject
*) texObj
->DriverData
);
1007 if ( (targets
& DRI_TEXMGR_DO_TEXTURE_2D
) != 0 ) {
1008 texObj
= ctx
->Texture
.Unit
[i
].Current2D
;
1009 ctx
->Driver
.BindTexture( ctx
, GL_TEXTURE_2D
, texObj
);
1010 move_to_tail( swapped
, (driTextureObject
*) texObj
->DriverData
);
1013 if ( (targets
& DRI_TEXMGR_DO_TEXTURE_3D
) != 0 ) {
1014 texObj
= ctx
->Texture
.Unit
[i
].Current3D
;
1015 ctx
->Driver
.BindTexture( ctx
, GL_TEXTURE_3D
, texObj
);
1016 move_to_tail( swapped
, (driTextureObject
*) texObj
->DriverData
);
1019 if ( (targets
& DRI_TEXMGR_DO_TEXTURE_CUBE
) != 0 ) {
1020 texObj
= ctx
->Texture
.Unit
[i
].CurrentCubeMap
;
1021 ctx
->Driver
.BindTexture( ctx
, GL_TEXTURE_CUBE_MAP_ARB
, texObj
);
1022 move_to_tail( swapped
, (driTextureObject
*) texObj
->DriverData
);
1025 if ( (targets
& DRI_TEXMGR_DO_TEXTURE_RECT
) != 0 ) {
1026 texObj
= ctx
->Texture
.Unit
[i
].CurrentRect
;
1027 ctx
->Driver
.BindTexture( ctx
, GL_TEXTURE_RECTANGLE_NV
, texObj
);
1028 move_to_tail( swapped
, (driTextureObject
*) texObj
->DriverData
);
1032 ctx
->Texture
.CurrentUnit
= tmp
;
1039 * Verify that the specified texture is in the specificed heap.
1041 * \param tex Texture to be tested.
1042 * \param heap Texture memory heap to be tested.
1043 * \return True if the texture is in the heap, false otherwise.
1047 check_in_heap( const driTextureObject
* tex
, const driTexHeap
* heap
)
1050 return tex
->heap
== heap
;
1052 driTextureObject
* curr
;
1054 foreach( curr
, & heap
->texture_objects
) {
1055 if ( curr
== tex
) {
1066 /****************************************************************************/
1068 * Validate the consistency of a set of texture heaps.
1069 * Original version by Keith Whitwell in r200/r200_sanity.c.
1073 driValidateTextureHeaps( driTexHeap
* const * texture_heaps
,
1074 unsigned nr_heaps
, const driTextureObject
* swapped
)
1076 driTextureObject
*t
;
1079 for ( i
= 0 ; i
< nr_heaps
; i
++ ) {
1081 unsigned textures_in_heap
= 0;
1082 unsigned blocks_in_mempool
= 0;
1083 const driTexHeap
* heap
= texture_heaps
[i
];
1084 const memHeap_t
* p
= heap
->memory_heap
;
1086 /* Check each texture object has a MemBlock, and is linked into
1089 * Check the texobj base address corresponds to the MemBlock
1090 * range. Check the texobj size (recalculate???) fits within
1093 * Count the number of texobj's using this heap.
1096 foreach ( t
, &heap
->texture_objects
) {
1097 if ( !check_in_heap( t
, heap
) ) {
1098 fprintf( stderr
, "%s memory block for texture object @ %p not "
1099 "found in heap #%d\n",
1100 __FUNCTION__
, t
, i
);
1105 if ( t
->totalSize
> t
->memBlock
->size
) {
1106 fprintf( stderr
, "%s: Memory block for texture object @ %p is "
1107 "only %u bytes, but %u are required\n",
1108 __FUNCTION__
, t
, t
->totalSize
, t
->memBlock
->size
);
1115 /* Validate the contents of the heap:
1121 while ( p
!= NULL
) {
1123 fprintf( stderr
, "%s: Block (%08x,%x), is reserved?!\n",
1124 __FUNCTION__
, p
->ofs
, p
->size
);
1128 if (p
->ofs
!= last_end
) {
1129 fprintf( stderr
, "%s: blocks_in_mempool = %d, last_end = %d, p->ofs = %d\n",
1130 __FUNCTION__
, blocks_in_mempool
, last_end
, p
->ofs
);
1134 if (!p
->reserved
&& !p
->free
) {
1135 blocks_in_mempool
++;
1138 last_end
= p
->ofs
+ p
->size
;
1142 if (textures_in_heap
!= blocks_in_mempool
) {
1143 fprintf( stderr
, "%s: Different number of textures objects (%u) and "
1144 "inuse memory blocks (%u)\n",
1145 __FUNCTION__
, textures_in_heap
, blocks_in_mempool
);
1150 fprintf( stderr
, "%s: textures_in_heap = %u\n",
1151 __FUNCTION__
, textures_in_heap
);
1156 /* Check swapped texobj's have zero memblocks
1159 foreach ( t
, swapped
) {
1160 if ( t
->memBlock
!= NULL
) {
1161 fprintf( stderr
, "%s: Swapped texobj %p has non-NULL memblock %p\n",
1162 __FUNCTION__
, t
, t
->memBlock
);
1169 fprintf( stderr
, "%s: swapped texture count = %u\n", i
);