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