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