Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 635:76c63aac3590
prev561:533f6b478071
next645:a7392098299c
author nkeynes
date Thu Feb 14 13:54:11 2008 +0000 (13 years ago)
branchlxdream-render
permissions -rw-r--r--
last change Commit render work in progress. Main changes:
* Preliminary OSMesa support
* Move the generic gl code out to pvr2/
* Implement scene data structure + reader
* Remove the 1/z adjustments
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Texture cache. Responsible for maintaining a working set of OpenGL 
     5  * textures. 
     6  *
     7  *
     8  * Copyright (c) 2005 Nathan Keynes.
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License as published by
    12  * the Free Software Foundation; either version 2 of the License, or
    13  * (at your option) any later version.
    14  *
    15  * This program is distributed in the hope that it will be useful,
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  * GNU General Public License for more details.
    19  */
    21 #include <assert.h>
    22 #include "pvr2/pvr2.h"
    24 /** Specifies the maximum number of OpenGL
    25  * textures we're willing to have open at a time. If more are
    26  * needed, textures will be evicted in LRU order.
    27  */
    28 #define MAX_TEXTURES 256
    30 /**
    31  * Data structure:
    32  *
    33  * Main operations:
    34  *    find entry by texture_addr
    35  *    add new entry
    36  *    move entry to tail of lru list
    37  *    remove entry
    38  */
    40 typedef signed short texcache_entry_index;
    41 #define EMPTY_ENTRY -1
    43 static texcache_entry_index texcache_free_ptr = 0;
    44 static GLuint texcache_free_list[MAX_TEXTURES];
    46 typedef struct texcache_entry {
    47     uint32_t texture_addr;
    48     int width, height, mode;
    49     GLuint texture_id;
    50     texcache_entry_index next;
    51     uint32_t lru_count;
    52 } *texcache_entry_t;
    54 static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
    55 static uint32_t texcache_ref_counter;
    56 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
    58 /**
    59  * Initialize the texture cache.
    60  */
    61 void texcache_init( )
    62 {
    63     int i;
    64     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    65 	texcache_page_lookup[i] = EMPTY_ENTRY;
    66     }
    67     for( i=0; i<MAX_TEXTURES; i++ ) {
    68 	texcache_free_list[i] = i;
    69 	texcache_active_list[i].texture_addr = -1;
    70 	texcache_active_list[i].next = EMPTY_ENTRY;
    71     }
    72     texcache_free_ptr = 0;
    73     texcache_ref_counter = 0;
    74 }
    76 /**
    77  * Setup the initial texture ids (must be called after the GL context is
    78  * prepared)
    79  */
    80 void texcache_gl_init( )
    81 {
    82     int i;
    83     GLuint texids[MAX_TEXTURES];
    85     glGenTextures( MAX_TEXTURES, texids );
    86     for( i=0; i<MAX_TEXTURES; i++ ) {
    87 	texcache_active_list[i].texture_id = texids[i];
    88     }
    89 }
    91 /**
    92  * Flush all textures from the cache, returning them to the free list.
    93  */
    94 void texcache_flush( )
    95 {
    96     int i;
    97     /* clear structures */
    98     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    99 	texcache_page_lookup[i] = EMPTY_ENTRY;
   100     }
   101     for( i=0; i<MAX_TEXTURES; i++ ) {
   102 	texcache_free_list[i] = i;
   103 	texcache_active_list[i].next = EMPTY_ENTRY;
   104     }
   105     texcache_free_ptr = 0;
   106     texcache_ref_counter = 0;
   107 }
   109 /**
   110  * Flush all textures and delete. The cache will be non-functional until
   111  * the next call to texcache_init(). This would typically be done if
   112  * switching GL targets.
   113  */    
   114 void texcache_shutdown( )
   115 {
   116     GLuint texids[MAX_TEXTURES];
   117     int i;
   118     texcache_flush();
   120     for( i=0; i<MAX_TEXTURES; i++ ) {
   121 	texids[i] = texcache_active_list[i].texture_id;
   122     }
   123     glDeleteTextures( MAX_TEXTURES, texids );
   124 }
   126 static void texcache_evict( int slot )
   127 {
   128     /* Remove the selected slot from the lookup table */
   129     assert( texcache_active_list[slot].texture_addr != -1 );
   130     uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
   131     texcache_entry_index replace_next = texcache_active_list[slot].next;
   132     texcache_active_list[slot].texture_addr = -1;
   133     texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
   134     if( texcache_page_lookup[evict_page] == slot ) {
   135 	texcache_page_lookup[evict_page] = replace_next;
   136     } else {
   137 	texcache_entry_index idx = texcache_page_lookup[evict_page];
   138 	texcache_entry_index next;
   139 	do {
   140 	    next = texcache_active_list[idx].next;
   141 	    if( next == slot ) {
   142 		assert( idx != replace_next );
   143 		texcache_active_list[idx].next = replace_next;
   144 		break;
   145 	    }
   146 	    idx = next;
   147 	} while( next != EMPTY_ENTRY );
   148     }
   149 }
   151 /**
   152  * Evict a single texture from the cache.
   153  * @return the slot of the evicted texture.
   154  */
   155 static texcache_entry_index texcache_evict_lru( void )
   156 {
   157     /* Full table scan - take over the entry with the lowest lru value */
   158     texcache_entry_index slot = 0;
   159     int lru_value = texcache_active_list[0].lru_count;
   160     int i;
   161     for( i=1; i<MAX_TEXTURES; i++ ) {
   162 	/* FIXME: account for rollover */
   163 	if( texcache_active_list[i].lru_count < lru_value ) {
   164 	    slot = i;
   165 	    lru_value = texcache_active_list[i].lru_count;
   166 	}
   167     }
   168     texcache_evict(slot);
   170     return slot;
   171 }
   173 /**
   174  * Evict all textures contained in the page identified by a texture address.
   175  */
   176 void texcache_invalidate_page( uint32_t texture_addr ) {
   177     uint32_t texture_page = texture_addr >> 12;
   178     texcache_entry_index idx = texcache_page_lookup[texture_page];
   179     if( idx == EMPTY_ENTRY )
   180 	return;
   181     assert( texcache_free_ptr >= 0 );
   182     do {
   183 	texcache_entry_t entry = &texcache_active_list[idx];
   184 	entry->texture_addr = -1;
   185 	/* release entry */
   186 	texcache_free_ptr--;
   187 	texcache_free_list[texcache_free_ptr] = idx;
   188 	idx = entry->next;
   189 	entry->next = EMPTY_ENTRY;
   190     } while( idx != EMPTY_ENTRY );
   191     texcache_page_lookup[texture_page] = EMPTY_ENTRY;
   192 }
   194 /**
   195  * Mark all textures that use the palette table as needing a re-read (ie 
   196  * for when the palette is changed. We could track exactly which ones are 
   197  * affected, but it's not clear that the extra maintanence overhead is 
   198  * worthwhile.
   199  */
   200 void texcache_invalidate_palette( )
   201 {
   202     int i;
   203     for( i=0; i<MAX_TEXTURES; i++ ) {
   204 	if( texcache_active_list[i].texture_addr != -1 &&
   205 	    PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
   206 	    texcache_evict( i );
   207 	    texcache_free_ptr--;
   208 	    texcache_free_list[texcache_free_ptr] = i;
   209 	}
   210     }
   211 }
   213 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   214 {
   215     int i;
   216     for( i=0; i<inbytes; i++ ) {
   217 	*out++ = pal[*in++];
   218     }
   219 }
   221 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   222 {
   223     int i;
   224     for( i=0; i<inbytes; i++ ) {
   225 	*out++ = (uint16_t)pal[*in++];
   226     }
   227 }
   229 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   230 {
   231     int i;
   232     for( i=0; i<inbytes; i++ ) {
   233 	*out++ = pal[*in & 0x0F];
   234 	*out++ = pal[(*in >> 4)];
   235 	in++;
   236     }
   237 }
   240 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   241 {
   242     int i;
   243     for( i=0; i<inbytes; i++ ) {
   244 	*out++ = (uint16_t)pal[*in & 0x0F];
   245 	*out++ = (uint16_t)pal[(*in >> 4)];
   246 	in++;
   247     }
   248 }
   250 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
   252 struct vq_codebook {
   253     uint16_t quad[256][4];
   254 };
   256 static void vq_get_codebook( struct vq_codebook *codebook, 
   257 				uint16_t *input )
   258 {
   259     /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
   260     uint16_t *p = (uint16_t *)input;
   261     int i;
   262     for( i=0; i<256; i++ ) {
   263 	codebook->quad[i][0] = *p++;
   264 	codebook->quad[i][2] = *p++;
   265 	codebook->quad[i][1] = *p++;
   266 	codebook->quad[i][3] = *p++;
   267     }
   268 }    
   270 static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
   271 		       struct vq_codebook *codebook ) {
   272     int i,j;
   274     uint8_t *c = (uint8_t *)input;
   275     for( j=0; j<height; j+=2 ) {
   276 	for( i=0; i<width; i+=2 ) {
   277 	    uint8_t code = *c++;
   278 	    output[i + j*width] = codebook->quad[code][0];
   279 	    output[i + 1 + j*width] = codebook->quad[code][1];
   280 	    output[i + (j+1)*width] = codebook->quad[code][2];
   281 	    output[i + 1 + (j+1)*width] = codebook->quad[code][3];
   282 	}
   283     }
   284 }
   286 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
   287 {
   288     u -= 128;
   289     v -= 128;
   290     int r = (int)(y + v*1.375);
   291     int g = (int)(y - u*0.34375 - v*0.6875);
   292     int b = (int)(y + u*1.71875);
   293     if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
   294     if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
   295     if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
   296     return 0xFF000000 | (r<<16) | (g<<8) | (b);
   297 }
   300 /**
   301  * Convert raster YUV texture data into RGB32 data - most GL implementations don't
   302  * directly support this format unfortunately. The input data is formatted as
   303  * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
   304  * in floating point.
   305  */
   306 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
   307 {
   308     int x, y;
   309     uint32_t *p = input;
   310     for( y=0; y<height; y++ ) {
   311 	for( x=0; x<width; x+=2 ) {
   312 	    float u = (float)(*p & 0xFF);
   313 	    float y0 = (float)( (*p>>8)&0xFF );
   314 	    float v = (float)( (*p>>16)&0xFF );
   315 	    float y1 = (float)( (*p>>24)&0xFF );
   316 	    *output++ = yuv_to_rgb32( y0, u, v ); 
   317 	    *output++ = yuv_to_rgb32( y1, u, v );
   318 	    p++;
   319 	}
   320     }
   321 }
   323 /**
   324  * Load texture data from the given address and parameters into the currently
   325  * bound OpenGL texture.
   326  */
   327 static void texcache_load_texture( uint32_t texture_addr, int width, int height,
   328 			      int mode ) {
   329     int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
   330     GLint intFormat = GL_RGBA, format, type;
   331     int tex_format = mode & PVR2_TEX_FORMAT_MASK;
   332     struct vq_codebook codebook;
   333     GLint filter = GL_LINEAR;
   335     glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
   336     /* Decode the format parameters */
   337     switch( tex_format ) {
   338     case PVR2_TEX_FORMAT_IDX4:
   339     case PVR2_TEX_FORMAT_IDX8:
   340 	/* For indexed-colour modes, we need to lookup the palette control
   341 	 * word to determine the de-indexed texture format.
   342 	 */
   343 	switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
   344 	case 0: /* ARGB1555 */
   345 	    format = GL_BGRA;
   346 	    type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   347 	    break;
   348 	case 1:  /* RGB565 */
   349 	    intFormat = GL_RGB;
   350 	    format = GL_RGB;
   351 	    type = GL_UNSIGNED_SHORT_5_6_5;
   352 	    break;
   353 	case 2: /* ARGB4444 */
   354 	    format = GL_BGRA;
   355 	    type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   356 	    break;
   357 	case 3: /* ARGB8888 */
   358 	    format = GL_BGRA;
   359 	    type = GL_UNSIGNED_BYTE;
   360 	    bpp_shift = 2;
   361 	    break;
   362 	default:
   363 	    return; /* Can't happen, but it makes gcc stop complaining */
   364 	}
   365 	break;
   367     case PVR2_TEX_FORMAT_ARGB1555:
   368 	format = GL_BGRA;
   369 	type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   370 	break;
   371     case PVR2_TEX_FORMAT_RGB565:
   372 	intFormat = GL_RGB;
   373 	format = GL_RGB;
   374 	type = GL_UNSIGNED_SHORT_5_6_5;
   375 	break;
   376     case PVR2_TEX_FORMAT_ARGB4444:
   377 	format = GL_BGRA;
   378 	type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   379 	break;
   380     case PVR2_TEX_FORMAT_YUV422:
   381 	/* YUV422 isn't directly supported by most implementations, so decode
   382 	 * it to a (reasonably) standard ARGB32.
   383 	 */
   384 	bpp_shift = 2;
   385 	format = GL_BGRA;
   386 	type = GL_UNSIGNED_BYTE;
   387 	break;
   388     case PVR2_TEX_FORMAT_BUMPMAP:
   389 	ERROR( "Bumpmap not supported" );
   390 	return;
   391     default:
   392 	ERROR( "Undefined texture format" );
   393 	return;
   394     }
   396     if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
   397 	tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   398 	/* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
   399 	uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
   400 	unsigned char data[(width*height) << bpp_shift];
   401 	if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   402 	    unsigned char tmp[(width*height)<<1];
   403 	    pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
   404 	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
   405 	} else {
   406 	    pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
   407 	}
   408 	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
   409 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   410 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   411 	return;
   412     } 
   414     int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
   415     if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
   416 	int i;
   417 	for( i=0; 1<<i < width; i++ );
   418 	last_level = i;
   419 	mip_width = 2;
   420 	mip_height= 2;
   421 	filter = GL_LINEAR_MIPMAP_LINEAR;
   422     }
   423     dest_bytes = (mip_width * mip_height) << bpp_shift;
   424     src_bytes = dest_bytes; // Modes will change this (below)
   426     if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   427 	uint16_t tmp[VQ_CODEBOOK_SIZE];
   428 	pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
   429 	texture_addr += VQ_CODEBOOK_SIZE;
   430 	vq_get_codebook( &codebook, tmp );
   431     }
   433     for( level=last_level; level>= 0; level-- ) {
   434 	unsigned char data[dest_bytes];
   435 	/* load data from image, detwiddling/uncompressing as required */
   436 	if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   437 	    src_bytes = (mip_width * mip_height);
   438 	    int bank = (mode >> 25) &0x03;
   439 	    uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
   440 	    unsigned char tmp[src_bytes];
   441 	    pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
   442 	    if( bpp_shift == 2 ) {
   443 		decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   444 	    } else {
   445 		decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   446 	    }
   447 	} else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   448 	    src_bytes = (mip_width * mip_height) >> 1;
   449 	    int bank = (mode >>21 ) & 0x3F;
   450 	    uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
   451 	    unsigned char tmp[src_bytes];
   452 	    pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
   453 	    if( bpp_shift == 2 ) {
   454 		decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   455 	    } else {
   456 		decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   457 	    }
   458 	} else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   459 	    src_bytes = ((mip_width*mip_height)<<1);
   460 	    unsigned char tmp[src_bytes];
   461 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   462 		pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
   463 	    } else {
   464 		pvr2_vram64_read( tmp, texture_addr, src_bytes );
   465 	    }
   466 	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
   467 	} else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   468 	    src_bytes = ((mip_width*mip_height) >> 2);
   469 	    unsigned char tmp[src_bytes];
   470 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   471 		pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
   472 	    } else {
   473 		pvr2_vram64_read( tmp, texture_addr, src_bytes );
   474 	    }
   475 	    vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
   476 	} else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   477 	    pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
   478 	} else {
   479 	    pvr2_vram64_read( data, texture_addr, src_bytes );
   480 	}
   482 	/* Pass to GL */
   483 	if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
   484 	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
   485 			  data + (3 << bpp_shift) );
   486 	    texture_addr += src_bytes;
   487 	} else {
   488 	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
   489 			  data );
   490 	    texture_addr += src_bytes;
   491 	    mip_width <<= 1;
   492 	    mip_height <<= 1;
   493 	    dest_bytes <<= 2;
   494 	    src_bytes <<= 2;
   495 	}
   496     }
   498     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   499     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   500 }
   502 /**
   503  * Return a texture ID for the texture specified at the supplied address
   504  * and given parameters (the same sequence of bytes could in theory have
   505  * multiple interpretations). We use the texture address as the primary
   506  * index, but allow for multiple instances at each address. The texture
   507  * will be bound to the GL_TEXTURE_2D target before being returned.
   508  * 
   509  * If the texture has already been bound, return the ID to which it was
   510  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   511  */
   512 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
   513 			     int mode )
   514 {
   515     uint32_t texture_page = texture_addr >> 12;
   516     texcache_entry_index next;
   517     texcache_entry_index idx = texcache_page_lookup[texture_page];
   518     while( idx != EMPTY_ENTRY ) {
   519 	texcache_entry_t entry = &texcache_active_list[idx];
   520 	if( entry->texture_addr == texture_addr &&
   521 	    entry->mode == mode &&
   522 	    entry->width == width &&
   523 	    entry->height == height ) {
   524 	    entry->lru_count = texcache_ref_counter++;
   525 	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
   526 	    return entry->texture_id;
   527 	}
   528         idx = entry->next;
   529     }
   532     /* Not found - check the free list */
   533     texcache_entry_index slot = 0;
   535     if( texcache_free_ptr < MAX_TEXTURES ) {
   536 	slot = texcache_free_list[texcache_free_ptr++];
   537     } else {
   538 	slot = texcache_evict_lru();
   539     }
   541     /* Construct new entry */
   542     texcache_active_list[slot].texture_addr = texture_addr;
   543     texcache_active_list[slot].width = width;
   544     texcache_active_list[slot].height = height;
   545     texcache_active_list[slot].mode = mode;
   546     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   548     /* Add entry to the lookup table */
   549     next = texcache_page_lookup[texture_page];
   550     if( next == slot ) {
   551 	int i;
   552 	fprintf( stderr, "Active list: " );
   553 	for( i=0; i<MAX_TEXTURES; i++ ) {
   554 	    fprintf( stderr, "%d, ", texcache_active_list[i].next );
   555 	}
   556 	fprintf( stderr, "\n" );
   557 	assert( next != slot );
   559     }
   560     assert( next != slot );
   561     texcache_active_list[slot].next = next;
   562     texcache_page_lookup[texture_page] = slot;
   564     /* Construct the GL texture */
   565     glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   566     texcache_load_texture( texture_addr, width, height, mode );
   568     texcache_integrity_check();
   569     return texcache_active_list[slot].texture_id;
   570 }
   572 /**
   573  * Check the integrity of the texcache. Verifies that every cache slot
   574  * appears exactly once on either the free list or one page list. For 
   575  * active slots, the texture address must also match the page it appears on.
   576  * 
   577  */
   578 void texcache_integrity_check()
   579 {
   580     int i;
   581     int slot_found[MAX_TEXTURES];
   583     memset( slot_found, 0, sizeof(slot_found) );
   585     /* Check entries on the free list */
   586     for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
   587 	int slot = texcache_free_list[i];
   588 	assert( slot_found[slot] == 0 );
   589 	assert( texcache_active_list[slot].next == EMPTY_ENTRY );
   590 	slot_found[slot] = 1;
   591     }
   593     /* Check entries on the active lists */
   594     for( i=0; i< PVR2_RAM_PAGES; i++ ) {
   595 	int slot = texcache_page_lookup[i];
   596 	while( slot != EMPTY_ENTRY ) {
   597 	    assert( slot_found[slot] == 0 );
   598 	    assert( (texcache_active_list[slot].texture_addr >> 12) == i );
   599 	    slot_found[slot] = 2;
   600 	    slot = texcache_active_list[slot].next;
   601 	}
   602     }
   604     /* Make sure we didn't miss any entries */
   605     for( i=0; i<MAX_TEXTURES; i++ ) {
   606 	assert( slot_found[i] != 0 );
   607     }
   608 }
.