#include "texmem.h"
#include "simple_list.h"
#include "imports.h"
#include "macros.h"
#include <assert.h>
static unsigned dummy_swap_counter;
static unsigned
driLog2( unsigned n )
{
unsigned log2;
for ( log2 = 1 ; n > 1 ; log2++ ) {
n >>= 1;
}
return log2;
}
GLboolean
driIsTextureResident( GLcontext * ctx,
struct gl_texture_object * texObj )
{
driTextureObject * t;
t = (driTextureObject *) texObj->DriverData;
return( (t != NULL) && (t->memBlock != NULL) );
}
static void resetGlobalLRU( driTexHeap * heap )
{
drmTextureRegionPtr list = heap->global_regions;
unsigned sz = 1U << heap->logGranularity;
unsigned i;
for (i = 0 ; (i+1) * sz <= heap->size ; i++) {
list[i].prev = i-1;
list[i].next = i+1;
list[i].age = 0;
}
i--;
list[0].prev = heap->nrRegions;
list[i].prev = i-1;
list[i].next = heap->nrRegions;
list[heap->nrRegions].prev = i;
list[heap->nrRegions].next = 0;
heap->global_age[0] = 0;
}
static void printLocalLRU( driTexHeap * heap, const char *callername )
{
driTextureObject *t;
unsigned sz = 1U << heap->logGranularity;
fprintf( stderr, "%s in %s:\nLocal LRU, heap %d:\n",
__FUNCTION__, callername, heap->heapId );
foreach ( t, &heap->texture_objects ) {
if (!t->memBlock)
continue;
if (!t->tObj) {
fprintf( stderr, "Placeholder (%p) %d at 0x%x sz 0x%x\n",
(void *)t,
t->memBlock->ofs / sz,
t->memBlock->ofs,
t->memBlock->size );
} else {
fprintf( stderr, "Texture (%p) at 0x%x sz 0x%x\n",
(void *)t,
t->memBlock->ofs,
t->memBlock->size );
}
}
foreach ( t, heap->swapped_objects ) {
if (!t->tObj) {
fprintf( stderr, "Swapped Placeholder (%p)\n", (void *)t );
} else {
fprintf( stderr, "Swapped Texture (%p)\n", (void *)t );
}
}
fprintf( stderr, "\n" );
}
static void printGlobalLRU( driTexHeap * heap, const char *callername )
{
drmTextureRegionPtr list = heap->global_regions;
int i, j;
fprintf( stderr, "%s in %s:\nGlobal LRU, heap %d list %p:\n",
__FUNCTION__, callername, heap->heapId, (void *)list );
for ( i = 0, j = heap->nrRegions ; i < heap->nrRegions ; i++ ) {
fprintf( stderr, "list[%d] age %d next %d prev %d in_use %d\n",
j, list[j].age, list[j].next, list[j].prev, list[j].in_use );
j = list[j].next;
if ( j == heap->nrRegions ) break;
}
if ( j != heap->nrRegions ) {
fprintf( stderr, "Loop detected in global LRU\n" );
for ( i = 0 ; i < heap->nrRegions ; i++ ) {
fprintf( stderr, "list[%d] age %d next %d prev %d in_use %d\n",
i, list[i].age, list[i].next, list[i].prev, list[i].in_use );
}
}
fprintf( stderr, "\n" );
}
void driUpdateTextureLRU( driTextureObject * t )
{
driTexHeap * heap;
drmTextureRegionPtr list;
unsigned shift;
unsigned start;
unsigned end;
unsigned i;
heap = t->heap;
if ( heap != NULL ) {
shift = heap->logGranularity;
start = t->memBlock->ofs >> shift;
end = (t->memBlock->ofs + t->memBlock->size - 1) >> shift;
heap->local_age = ++heap->global_age[0];
list = heap->global_regions;
move_to_head( & heap->texture_objects, t );
for (i = start ; i <= end ; i++) {
list[i].in_use = 1;
list[i].age = heap->local_age;
list[(unsigned)list[i].next].prev = list[i].prev;
list[(unsigned)list[i].prev].next = list[i].next;
list[i].prev = heap->nrRegions;
list[i].next = list[heap->nrRegions].next;
list[(unsigned)list[heap->nrRegions].next].prev = i;
list[heap->nrRegions].next = i;
}
if ( 0 ) {
printGlobalLRU( heap, __FUNCTION__ );
printLocalLRU( heap, __FUNCTION__ );
}
}
}
void driSwapOutTextureObject( driTextureObject * t )
{
unsigned face;
if ( t->memBlock != NULL ) {
assert( t->heap != NULL );
mmFreeMem( t->memBlock );
t->memBlock = NULL;
if (t->timestamp > t->heap->timestamp)
t->heap->timestamp = t->timestamp;
t->heap->texture_swaps[0]++;
move_to_tail( t->heap->swapped_objects, t );
t->heap = NULL;
}
else {
assert( t->heap == NULL );
}
for ( face = 0 ; face < 6 ; face++ ) {
t->dirty_images[face] = ~0;
}
}
void driDestroyTextureObject( driTextureObject * t )
{
driTexHeap * heap;
if ( 0 ) {
fprintf( stderr, "[%s:%d] freeing %p (tObj = %p, DriverData = %p)\n",
__FILE__, __LINE__,
(void *)t,
(void *)((t != NULL) ? t->tObj : NULL),
(void *)((t != NULL && t->tObj != NULL) ? t->tObj->DriverData : NULL ));
}
if ( t != NULL ) {
if ( t->memBlock ) {
heap = t->heap;
assert( heap != NULL );
heap->texture_swaps[0]++;
mmFreeMem( t->memBlock );
t->memBlock = NULL;
if (t->timestamp > t->heap->timestamp)
t->heap->timestamp = t->timestamp;
heap->destroy_texture_object( heap->driverContext, t );
t->heap = NULL;
}
if ( t->tObj != NULL ) {
assert( t->tObj->DriverData == t );
t->tObj->DriverData = NULL;
}
remove_from_list( t );
FREE( t );
}
if ( 0 ) {
fprintf( stderr, "[%s:%d] done freeing %p\n", __FILE__, __LINE__, (void *)t );
}
}
static void driTexturesGone( driTexHeap * heap, int offset, int size,
int in_use )
{
driTextureObject * t;
driTextureObject * tmp;
foreach_s ( t, tmp, & heap->texture_objects ) {
if ( (t->memBlock->ofs < (offset + size))
&& ((t->memBlock->ofs + t->memBlock->size) > offset) ) {
if ( t->tObj != NULL ) {
driSwapOutTextureObject( t );
}
else {
if ( in_use &&
offset == t->memBlock->ofs && size == t->memBlock->size ) {
return;
} else {
driDestroyTextureObject( t );
}
}
}
}
if ( in_use ) {
t = (driTextureObject *) CALLOC( heap->texture_object_size );
if ( t == NULL ) return;
t->memBlock = mmAllocMem( heap->memory_heap, size, 0, offset );
if ( t->memBlock == NULL ) {
fprintf( stderr, "Couldn't alloc placeholder: heap %u sz %x ofs %x\n", heap->heapId,
(int)size, (int)offset );
mmDumpMemInfo( heap->memory_heap );
return;
}
t->heap = heap;
insert_at_head( & heap->texture_objects, t );
}
}
void driAgeTextures( driTexHeap * heap )
{
drmTextureRegionPtr list = heap->global_regions;
unsigned sz = 1U << (heap->logGranularity);
unsigned i, nr = 0;
for (i = list[heap->nrRegions].prev ;
i != heap->nrRegions && nr < heap->nrRegions ;
i = list[i].prev, nr++) {
if ( (i * sz) > heap->size ) {
nr = heap->nrRegions;
break;
}
if (list[i].age > heap->local_age)
driTexturesGone( heap, i * sz, sz, list[i].in_use);
}
if (nr == heap->nrRegions) {
driTexturesGone( heap, 0, heap->size, 0);
resetGlobalLRU( heap );
}
if ( 0 ) {
printGlobalLRU( heap, __FUNCTION__ );
printLocalLRU( heap, __FUNCTION__ );
}
heap->local_age = heap->global_age[0];
}
int
driAllocateTexture( driTexHeap * const * heap_array, unsigned nr_heaps,
driTextureObject * t )
{
driTexHeap * heap;
driTextureObject * temp;
driTextureObject * cursor;
unsigned id;
heap = t->heap;
for ( id = 0 ; (t->memBlock == NULL) && (id < nr_heaps) ; id++ ) {
heap = heap_array[ id ];
if ( heap != NULL ) {
t->memBlock = mmAllocMem( heap->memory_heap, t->totalSize,
heap->alignmentShift, 0 );
}
}
if ( t->memBlock == NULL ) {
for ( id = 0 ; (t->memBlock == NULL) && (id < nr_heaps) ; id++ ) {
heap = heap_array[ id ];
if ( t->totalSize <= heap->size ) {
for ( cursor = heap->texture_objects.prev, temp = cursor->prev;
cursor != &heap->texture_objects ;
cursor = temp, temp = cursor->prev ) {
if ( cursor->bound ) {
continue;
}
if (cursor->tObj)
driSwapOutTextureObject( cursor );
else
driDestroyTextureObject( cursor );
t->memBlock = mmAllocMem( heap->memory_heap, t->totalSize,
heap->alignmentShift, 0 );
if (t->memBlock)
break;
}
}
}
}
if ( t->memBlock != NULL ) {
assert( heap != NULL );
assert( (t->heap == NULL) || (t->heap == heap) );
t->heap = heap;
return heap->heapId;
}
else {
assert( t->heap == NULL );
fprintf( stderr, "[%s:%d] unable to allocate texture\n",
__FUNCTION__, __LINE__ );
return -1;
}
}
void
driSetTextureSwapCounterLocation( driTexHeap * heap, unsigned * counter )
{
heap->texture_swaps = (counter == NULL) ? & dummy_swap_counter : counter;
}
driTexHeap *
driCreateTextureHeap( unsigned heap_id, void * context, unsigned size,
unsigned alignmentShift, unsigned nr_regions,
drmTextureRegionPtr global_regions, unsigned * global_age,
driTextureObject * swapped_objects,
unsigned texture_object_size,
destroy_texture_object_t * destroy_tex_obj
)
{
driTexHeap * heap;
unsigned l;
if ( 0 )
fprintf( stderr, "%s( %u, %p, %u, %u, %u )\n",
__FUNCTION__,
heap_id, (void *)context, size, alignmentShift, nr_regions );
heap = (driTexHeap *) CALLOC( sizeof( driTexHeap ) );
if ( heap != NULL ) {
l = driLog2( (size - 1) / nr_regions );
if ( l < alignmentShift )
{
l = alignmentShift;
}
heap->logGranularity = l;
heap->size = size & ~((1L << l) - 1);
heap->memory_heap = mmInit( 0, heap->size );
if ( heap->memory_heap != NULL ) {
heap->heapId = heap_id;
heap->driverContext = context;
heap->alignmentShift = alignmentShift;
heap->nrRegions = nr_regions;
heap->global_regions = global_regions;
heap->global_age = global_age;
heap->swapped_objects = swapped_objects;
heap->texture_object_size = texture_object_size;
heap->destroy_texture_object = destroy_tex_obj;
if (heap->global_age == 0)
heap->local_age = ~0;
else
heap->local_age = 0;
make_empty_list( & heap->texture_objects );
driSetTextureSwapCounterLocation( heap, NULL );
}
else {
FREE( heap );
heap = NULL;
}
}
if ( 0 )
fprintf( stderr, "%s returning %p\n", __FUNCTION__, (void *)heap );
return heap;
}
void
driDestroyTextureHeap( driTexHeap * heap )
{
driTextureObject * t;
driTextureObject * temp;
if ( heap != NULL ) {
foreach_s( t, temp, & heap->texture_objects ) {
driDestroyTextureObject( t );
}
foreach_s( t, temp, heap->swapped_objects ) {
driDestroyTextureObject( t );
}
mmDestroy( heap->memory_heap );
FREE( heap );
}
}
static unsigned
texels_this_map_size( int base_size_log2, unsigned dimensions, unsigned faces )
{
unsigned texels;
assert( (faces == 1) || (faces == 6) );
assert( (dimensions == 2) || (dimensions == 3) );
texels = 0;
if ( base_size_log2 >= 0 ) {
texels = (1U << (dimensions * base_size_log2));
texels = (texels * 4 * faces + 2) / 3;
}
return texels;
}
struct maps_per_heap {
unsigned c[32];
};
static void
fill_in_maximums( driTexHeap * const * heaps, unsigned nr_heaps,
unsigned max_bytes_per_texel, unsigned max_size,
unsigned mipmaps_at_once, unsigned dimensions,
unsigned faces, struct maps_per_heap * max_textures )
{
unsigned heap;
unsigned log2_size;
unsigned mask;
for ( heap = 0 ; heap < nr_heaps ; heap++ ) {
if ( heaps[ heap ] == NULL ) {
(void) memset( max_textures[ heap ].c, 0,
sizeof( max_textures[ heap ].c ) );
continue;
}
mask = (1U << heaps[ heap ]->logGranularity) - 1;
if ( 0 ) {
fprintf( stderr, "[%s:%d] heap[%u] = %u bytes, mask = 0x%08x\n",
__FILE__, __LINE__,
heap, heaps[ heap ]->size, mask );
}
for ( log2_size = max_size ; log2_size > 0 ; log2_size-- ) {
unsigned total;
total = texels_this_map_size( log2_size, dimensions, faces )
- texels_this_map_size( log2_size - mipmaps_at_once,
dimensions, faces );
total *= max_bytes_per_texel;
total = (total + mask) & ~mask;
max_textures[ heap ].c[ log2_size ] = heaps[ heap ]->size / total;
if ( 0 ) {
fprintf( stderr, "[%s:%d] max_textures[%u].c[%02u] "
"= 0x%08x / 0x%08x "
"= %u (%u)\n",
__FILE__, __LINE__,
heap, log2_size,
heaps[ heap ]->size, total,
heaps[ heap ]->size / total,
max_textures[ heap ].c[ log2_size ] );
}
}
}
}
static unsigned
get_max_size( unsigned nr_heaps,
unsigned texture_units,
unsigned max_size,
int all_textures_one_heap,
struct maps_per_heap * max_textures )
{
unsigned heap;
unsigned log2_size;
for ( log2_size = max_size ; log2_size > 0 ; log2_size-- ) {
unsigned total = 0;
for ( heap = 0 ; heap < nr_heaps ; heap++ )
{
total += max_textures[ heap ].c[ log2_size ];
if ( 0 ) {
fprintf( stderr, "[%s:%d] max_textures[%u].c[%02u] = %u, "
"total = %u\n", __FILE__, __LINE__, heap, log2_size,
max_textures[ heap ].c[ log2_size ], total );
}
if ( (max_textures[ heap ].c[ log2_size ] >= texture_units)
|| (!all_textures_one_heap && (total >= texture_units)) ) {
return log2_size + 1;
}
}
}
assert( log2_size != 0 );
return 0;
}
#define SET_MAX(f,v) \
do { if ( max_sizes[v] != 0 ) { limits-> f = max_sizes[v]; } } while( 0 )
#define SET_MAX_RECT(f,v) \
do { if ( max_sizes[v] != 0 ) { limits-> f = 1 << max_sizes[v]; } } while( 0 )
void
driCalculateMaxTextureLevels( driTexHeap * const * heaps,
unsigned nr_heaps,
struct gl_constants * limits,
unsigned max_bytes_per_texel,
unsigned max_2D_size,
unsigned max_3D_size,
unsigned max_cube_size,
unsigned max_rect_size,
unsigned mipmaps_at_once,
int all_textures_one_heap )
{
struct maps_per_heap max_textures[8];
unsigned i;
const unsigned dimensions[4] = { 2, 3, 2, 2 };
const unsigned faces[4] = { 1, 1, 6, 1 };
unsigned max_sizes[4];
unsigned mipmaps[4];
max_sizes[0] = max_2D_size;
max_sizes[1] = max_3D_size;
max_sizes[2] = max_cube_size;
max_sizes[3] = max_rect_size;
mipmaps[0] = mipmaps_at_once;
mipmaps[1] = mipmaps_at_once;
mipmaps[2] = 1;
mipmaps[3] = mipmaps_at_once;
for ( i = 0 ; i < 4 ; i++ ) {
if ( max_sizes[ i ] != 0 ) {
fill_in_maximums( heaps, nr_heaps, max_bytes_per_texel,
max_sizes[ i ], mipmaps[ i ],
dimensions[ i ], faces[ i ],
max_textures );
max_sizes[ i ] = get_max_size( nr_heaps,
limits->MaxTextureUnits,
max_sizes[ i ],
all_textures_one_heap,
max_textures );
}
}
SET_MAX( MaxTextureLevels, 0 );
SET_MAX( Max3DTextureLevels, 1 );
SET_MAX( MaxCubeTextureLevels, 2 );
SET_MAX_RECT( MaxTextureRectSize, 3 );
}
void driInitTextureObjects( GLcontext *ctx, driTextureObject * swapped,
GLuint targets )
{
struct gl_texture_object *texObj;
GLuint tmp = ctx->Texture.CurrentUnit;
unsigned i;
for ( i = 0 ; i < ctx->Const.MaxTextureUnits ; i++ ) {
ctx->Texture.CurrentUnit = i;
if ( (targets & DRI_TEXMGR_DO_TEXTURE_1D) != 0 ) {
texObj = ctx->Texture.Unit[i].Current1D;
ctx->Driver.BindTexture( ctx, GL_TEXTURE_1D, texObj );
move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
}
if ( (targets & DRI_TEXMGR_DO_TEXTURE_2D) != 0 ) {
texObj = ctx->Texture.Unit[i].Current2D;
ctx->Driver.BindTexture( ctx, GL_TEXTURE_2D, texObj );
move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
}
if ( (targets & DRI_TEXMGR_DO_TEXTURE_3D) != 0 ) {
texObj = ctx->Texture.Unit[i].Current3D;
ctx->Driver.BindTexture( ctx, GL_TEXTURE_3D, texObj );
move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
}
if ( (targets & DRI_TEXMGR_DO_TEXTURE_CUBE) != 0 ) {
texObj = ctx->Texture.Unit[i].CurrentCubeMap;
ctx->Driver.BindTexture( ctx, GL_TEXTURE_CUBE_MAP_ARB, texObj );
move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
}
if ( (targets & DRI_TEXMGR_DO_TEXTURE_RECT) != 0 ) {
texObj = ctx->Texture.Unit[i].CurrentRect;
ctx->Driver.BindTexture( ctx, GL_TEXTURE_RECTANGLE_NV, texObj );
move_to_tail( swapped, (driTextureObject *) texObj->DriverData );
}
}
ctx->Texture.CurrentUnit = tmp;
}
static GLboolean
check_in_heap( const driTextureObject * tex, const driTexHeap * heap )
{
#if 1
return tex->heap == heap;
#else
driTextureObject * curr;
foreach( curr, & heap->texture_objects ) {
if ( curr == tex ) {
break;
}
}
return curr == tex;
#endif
}
GLboolean
driValidateTextureHeaps( driTexHeap * const * texture_heaps,
unsigned nr_heaps, const driTextureObject * swapped )
{
driTextureObject *t;
unsigned i;
for ( i = 0 ; i < nr_heaps ; i++ ) {
int last_end = 0;
unsigned textures_in_heap = 0;
unsigned blocks_in_mempool = 0;
const driTexHeap * heap = texture_heaps[i];
const memHeap_t * p = heap->memory_heap;
foreach ( t, &heap->texture_objects ) {
if ( !check_in_heap( t, heap ) ) {
fprintf( stderr, "%s memory block for texture object @ %p not "
"found in heap #%d\n",
__FUNCTION__, (void *)t, i );
return GL_FALSE;
}
if ( t->totalSize > t->memBlock->size ) {
fprintf( stderr, "%s: Memory block for texture object @ %p is "
"only %u bytes, but %u are required\n",
__FUNCTION__, (void *)t, t->totalSize, t->memBlock->size );
return GL_FALSE;
}
textures_in_heap++;
}
while ( p != NULL ) {
if (p->reserved) {
fprintf( stderr, "%s: Block (%08x,%x), is reserved?!\n",
__FUNCTION__, p->ofs, p->size );
return GL_FALSE;
}
if (p->ofs != last_end) {
fprintf( stderr, "%s: blocks_in_mempool = %d, last_end = %d, p->ofs = %d\n",
__FUNCTION__, blocks_in_mempool, last_end, p->ofs );
return GL_FALSE;
}
if (!p->reserved && !p->free) {
blocks_in_mempool++;
}
last_end = p->ofs + p->size;
p = p->next;
}
if (textures_in_heap != blocks_in_mempool) {
fprintf( stderr, "%s: Different number of textures objects (%u) and "
"inuse memory blocks (%u)\n",
__FUNCTION__, textures_in_heap, blocks_in_mempool );
return GL_FALSE;
}
#if 0
fprintf( stderr, "%s: textures_in_heap = %u\n",
__FUNCTION__, textures_in_heap );
#endif
}
i = 0;
foreach ( t, swapped ) {
if ( t->memBlock != NULL ) {
fprintf( stderr, "%s: Swapped texobj %p has non-NULL memblock %p\n",
__FUNCTION__, (void *)t, (void *)t->memBlock );
return GL_FALSE;
}
i++;
}
#if 0
fprintf( stderr, "%s: swapped texture count = %u\n", i );
#endif
return GL_TRUE;
}
void
driCalculateTextureFirstLastLevel( driTextureObject * t )
{
struct gl_texture_object * const tObj = t->tObj;
const struct gl_texture_image * const baseImage =
tObj->Image[tObj->BaseLevel];
int firstLevel;
int lastLevel;
switch (tObj->Target) {
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
case GL_TEXTURE_CUBE_MAP:
if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
firstLevel = lastLevel = tObj->BaseLevel;
}
else {
firstLevel = tObj->BaseLevel + (GLint)(tObj->MinLod + 0.5);
firstLevel = MAX2(firstLevel, tObj->BaseLevel);
lastLevel = tObj->BaseLevel + (GLint)(tObj->MaxLod + 0.5);
lastLevel = MAX2(lastLevel, t->tObj->BaseLevel);
lastLevel = MIN2(lastLevel, t->tObj->BaseLevel + baseImage->MaxLog2);
lastLevel = MIN2(lastLevel, t->tObj->MaxLevel);
lastLevel = MAX2(firstLevel, lastLevel);
}
break;
case GL_TEXTURE_RECTANGLE_NV:
case GL_TEXTURE_4D_SGIS:
firstLevel = lastLevel = 0;
break;
default:
return;
}
t->firstLevel = firstLevel;
t->lastLevel = lastLevel;
}