Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 870:8d4deb2bc1ea
prev860:b429964761c8
next886:2bc6d2329cce
author nkeynes
date Sun Oct 19 05:09:31 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Remove unused fields from vertex_struct
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     render_buffer_t buffer;
    53     texcache_entry_index next;
    54     uint32_t lru_count;
    55 } *texcache_entry_t;
    57 static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
    58 static uint32_t texcache_ref_counter;
    59 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
    61 /**
    62  * Initialize the texture cache.
    63  */
    64 void texcache_init( )
    65 {
    66     int i;
    67     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    68         texcache_page_lookup[i] = EMPTY_ENTRY;
    69     }
    70     for( i=0; i<MAX_TEXTURES; i++ ) {
    71         texcache_free_list[i] = i;
    72         texcache_active_list[i].texture_addr = -1;
    73         texcache_active_list[i].buffer = NULL;
    74         texcache_active_list[i].next = EMPTY_ENTRY;
    75     }
    76     texcache_free_ptr = 0;
    77     texcache_ref_counter = 0;
    78 }
    80 /**
    81  * Setup the initial texture ids (must be called after the GL context is
    82  * prepared)
    83  */
    84 void texcache_gl_init( )
    85 {
    86     int i;
    87     GLuint texids[MAX_TEXTURES];
    89     glGenTextures( MAX_TEXTURES, texids );
    90     for( i=0; i<MAX_TEXTURES; i++ ) {
    91         texcache_active_list[i].texture_id = texids[i];
    92     }
    93 }
    95 void texcache_release_render_buffer( render_buffer_t buffer )
    96 {
    97     if( !buffer->flushed ) 
    98         pvr2_render_buffer_copy_to_sh4(buffer);
    99     pvr2_destroy_render_buffer(buffer);
   100 }
   102 /**
   103  * Flush all textures from the cache, returning them to the free list.
   104  */
   105 void texcache_flush( )
   106 {
   107     int i;
   108     /* clear structures */
   109     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
   110         texcache_page_lookup[i] = EMPTY_ENTRY;
   111     }
   112     for( i=0; i<MAX_TEXTURES; i++ ) {
   113         texcache_free_list[i] = i;
   114         texcache_active_list[i].next = EMPTY_ENTRY;
   115         if( texcache_active_list[i].buffer != NULL ) {
   116             texcache_release_render_buffer(texcache_active_list[i].buffer);
   117             texcache_active_list[i].buffer = NULL;
   118         }
   119     }
   120     texcache_free_ptr = 0;
   121     texcache_ref_counter = 0;
   122 }
   124 /**
   125  * Flush all textures and delete. The cache will be non-functional until
   126  * the next call to texcache_init(). This would typically be done if
   127  * switching GL targets.
   128  */    
   129 void texcache_shutdown( )
   130 {
   131     GLuint texids[MAX_TEXTURES];
   132     int i;
   133     texcache_flush();
   135     for( i=0; i<MAX_TEXTURES; i++ ) {
   136         texids[i] = texcache_active_list[i].texture_id;
   137     }
   138     glDeleteTextures( MAX_TEXTURES, texids );
   139 }
   141 static void texcache_evict( int slot )
   142 {
   143     /* Remove the selected slot from the lookup table */
   144     assert( texcache_active_list[slot].texture_addr != -1 );
   145     uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
   146     texcache_entry_index replace_next = texcache_active_list[slot].next;
   147     texcache_active_list[slot].texture_addr = -1;
   148     texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
   149     if( texcache_active_list[slot].buffer != NULL ) {
   150         texcache_release_render_buffer(texcache_active_list[slot].buffer);
   151         texcache_active_list[slot].buffer = NULL;
   152     }
   153     if( texcache_page_lookup[evict_page] == slot ) {
   154         texcache_page_lookup[evict_page] = replace_next;
   155     } else {
   156         texcache_entry_index idx = texcache_page_lookup[evict_page];
   157         texcache_entry_index next;
   158         do {
   159             next = texcache_active_list[idx].next;
   160             if( next == slot ) {
   161                 assert( idx != replace_next );
   162                 texcache_active_list[idx].next = replace_next;
   163                 break;
   164             }
   165             idx = next;
   166         } while( next != EMPTY_ENTRY );
   167     }
   168 }
   170 /**
   171  * Evict a single texture from the cache.
   172  * @return the slot of the evicted texture.
   173  */
   174 static texcache_entry_index texcache_evict_lru( void )
   175 {
   176     /* Full table scan - take over the entry with the lowest lru value */
   177     texcache_entry_index slot = 0;
   178     int lru_value = texcache_active_list[0].lru_count;
   179     int i;
   180     for( i=1; i<MAX_TEXTURES; i++ ) {
   181         /* FIXME: account for rollover */
   182         if( texcache_active_list[i].lru_count < lru_value ) {
   183             slot = i;
   184             lru_value = texcache_active_list[i].lru_count;
   185         }
   186     }
   187     texcache_evict(slot);
   189     return slot;
   190 }
   192 /**
   193  * Evict all textures contained in the page identified by a texture address.
   194  */
   195 void texcache_invalidate_page( uint32_t texture_addr ) {
   196     uint32_t texture_page = texture_addr >> 12;
   197     texcache_entry_index idx = texcache_page_lookup[texture_page];
   198     if( idx == EMPTY_ENTRY )
   199         return;
   200     assert( texcache_free_ptr >= 0 );
   201     do {
   202         texcache_entry_t entry = &texcache_active_list[idx];
   203         entry->texture_addr = -1;
   204         if( entry->buffer != NULL ) {
   205             texcache_release_render_buffer(entry->buffer);
   206             entry->buffer = NULL;
   207         }
   208         /* release entry */
   209         texcache_free_ptr--;
   210         texcache_free_list[texcache_free_ptr] = idx;
   211         idx = entry->next;
   212         entry->next = EMPTY_ENTRY;
   213     } while( idx != EMPTY_ENTRY );
   214     texcache_page_lookup[texture_page] = EMPTY_ENTRY;
   215 }
   217 /**
   218  * Mark all textures that use the palette table as needing a re-read (ie 
   219  * for when the palette is changed. We could track exactly which ones are 
   220  * affected, but it's not clear that the extra maintanence overhead is 
   221  * worthwhile.
   222  */
   223 void texcache_invalidate_palette( )
   224 {
   225     int i;
   226     for( i=0; i<MAX_TEXTURES; i++ ) {
   227         if( texcache_active_list[i].texture_addr != -1 &&
   228                 PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
   229             texcache_evict( i );
   230             texcache_free_ptr--;
   231             texcache_free_list[texcache_free_ptr] = i;
   232         }
   233     }
   234 }
   236 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   237 {
   238     int i;
   239     for( i=0; i<inbytes; i++ ) {
   240         *out++ = pal[*in++];
   241     }
   242 }
   244 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   245 {
   246     int i;
   247     for( i=0; i<inbytes; i++ ) {
   248         *out++ = (uint16_t)pal[*in++];
   249     }
   250 }
   252 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   253 {
   254     int i;
   255     for( i=0; i<inbytes; i++ ) {
   256         *out++ = pal[*in & 0x0F];
   257         *out++ = pal[(*in >> 4)];
   258         in++;
   259     }
   260 }
   263 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   264 {
   265     int i;
   266     for( i=0; i<inbytes; i++ ) {
   267         *out++ = (uint16_t)pal[*in & 0x0F];
   268         *out++ = (uint16_t)pal[(*in >> 4)];
   269         in++;
   270     }
   271 }
   273 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
   275 struct vq_codebook {
   276     uint16_t quad[256][4];
   277 };
   279 static void vq_get_codebook( struct vq_codebook *codebook, 
   280                              uint16_t *input )
   281 {
   282     /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
   283     uint16_t *p = (uint16_t *)input;
   284     int i;
   285     for( i=0; i<256; i++ ) {
   286         codebook->quad[i][0] = *p++;
   287         codebook->quad[i][2] = *p++;
   288         codebook->quad[i][1] = *p++;
   289         codebook->quad[i][3] = *p++;
   290     }
   291 }    
   293 static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
   294                        struct vq_codebook *codebook ) {
   295     int i,j;
   297     uint8_t *c = (uint8_t *)input;
   298     for( j=0; j<height; j+=2 ) {
   299         for( i=0; i<width; i+=2 ) {
   300             uint8_t code = *c++;
   301             output[i + j*width] = codebook->quad[code][0];
   302             output[i + 1 + j*width] = codebook->quad[code][1];
   303             output[i + (j+1)*width] = codebook->quad[code][2];
   304             output[i + 1 + (j+1)*width] = codebook->quad[code][3];
   305         }
   306     }
   307 }
   309 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
   310 {
   311     u -= 128;
   312     v -= 128;
   313     int r = (int)(y + v*1.375);
   314     int g = (int)(y - u*0.34375 - v*0.6875);
   315     int b = (int)(y + u*1.71875);
   316     if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
   317     if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
   318     if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
   319     return 0xFF000000 | (r<<16) | (g<<8) | (b);
   320 }
   323 /**
   324  * Convert raster YUV texture data into RGB32 data - most GL implementations don't
   325  * directly support this format unfortunately. The input data is formatted as
   326  * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
   327  * in floating point.
   328  */
   329 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
   330 {
   331     int x, y;
   332     uint32_t *p = input;
   333     for( y=0; y<height; y++ ) {
   334         for( x=0; x<width; x+=2 ) {
   335             float u = (float)(*p & 0xFF);
   336             float y0 = (float)( (*p>>8)&0xFF );
   337             float v = (float)( (*p>>16)&0xFF );
   338             float y1 = (float)( (*p>>24)&0xFF );
   339             *output++ = yuv_to_rgb32( y0, u, v ); 
   340             *output++ = yuv_to_rgb32( y1, u, v );
   341             p++;
   342         }
   343     }
   344 }
   346 static gboolean is_npot_texture( int width )
   347 {
   348     while( width != 0 ) {
   349         if( width & 1 ) 
   350             return width != 1;
   351         width >>= 1;
   352     }
   353     return TRUE;
   354 }
   356 /**
   357  * Load texture data from the given address and parameters into the currently
   358  * bound OpenGL texture.
   359  */
   360 static void texcache_load_texture( uint32_t texture_addr, int width, int height,
   361                                    int mode ) {
   362     int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
   363     GLint intFormat = GL_RGBA, format, type;
   364     int tex_format = mode & PVR2_TEX_FORMAT_MASK;
   365     struct vq_codebook codebook;
   366     GLint filter = GL_LINEAR;
   368     glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
   370     /* Decode the format parameters */
   371     switch( tex_format ) {
   372     case PVR2_TEX_FORMAT_IDX4:
   373     case PVR2_TEX_FORMAT_IDX8:
   374         /* For indexed-colour modes, we need to lookup the palette control
   375          * word to determine the de-indexed texture format.
   376          */
   377         switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
   378         case 0: /* ARGB1555 */
   379             format = GL_BGRA;
   380             type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   381             break;
   382         case 1:  /* RGB565 */
   383             intFormat = GL_RGB;
   384             format = GL_RGB;
   385             type = GL_UNSIGNED_SHORT_5_6_5;
   386             break;
   387         case 2: /* ARGB4444 */
   388             format = GL_BGRA;
   389             type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   390             break;
   391         case 3: /* ARGB8888 */
   392             format = GL_BGRA;
   393             type = GL_UNSIGNED_BYTE;
   394             bpp_shift = 2;
   395             break;
   396         default:
   397             return; /* Can't happen, but it makes gcc stop complaining */
   398         }
   399         break;
   401         default:
   402         case PVR2_TEX_FORMAT_ARGB1555:
   403             format = GL_BGRA;
   404             type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   405             break;
   406         case PVR2_TEX_FORMAT_RGB565:
   407             intFormat = GL_RGB;
   408             format = GL_RGB;
   409             type = GL_UNSIGNED_SHORT_5_6_5;
   410             break;
   411         case PVR2_TEX_FORMAT_ARGB4444:
   412             format = GL_BGRA;
   413             type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   414             break;
   415         case PVR2_TEX_FORMAT_YUV422:
   416             /* YUV422 isn't directly supported by most implementations, so decode
   417              * it to a (reasonably) standard ARGB32.
   418              */
   419             bpp_shift = 2;
   420             format = GL_BGRA;
   421             type = GL_UNSIGNED_BYTE;
   422             break;
   423         case PVR2_TEX_FORMAT_BUMPMAP:
   424             WARN( "Bumpmap not supported" );
   425             return;
   426     }
   428     if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
   429             tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   430         /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
   431         uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
   432         unsigned char data[(width*height) << bpp_shift];
   433         if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   434             unsigned char tmp[(width*height)<<1];
   435             pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
   436             yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
   437         } else {
   438             pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
   439         }
   440         glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
   441         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   442         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   443         return;
   444     } 
   446     if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   447         uint16_t tmp[VQ_CODEBOOK_SIZE];
   448         pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
   449         texture_addr += VQ_CODEBOOK_SIZE;
   450         vq_get_codebook( &codebook, tmp );
   451     }
   453     int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
   454     if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
   455         uint32_t src_offset = 0;
   456         filter = GL_LINEAR_MIPMAP_LINEAR;
   457         mip_height = height = width;
   458         while( (1<<last_level) < width ) {
   459             last_level++;
   460             src_offset += ((width>>last_level)*(width>>last_level));
   461         }
   462         if( width != 1 ) {
   463             src_offset += 3;
   464         }
   465         if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   466             src_offset >>= 2;
   467         } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   468             src_offset >>= 1;
   469         } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   470             src_offset <<= 1;
   471         } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   472             src_offset <<= bpp_shift;
   473         }
   474         texture_addr += src_offset;
   475     }
   478     dest_bytes = (mip_width * mip_height) << bpp_shift;
   479     src_bytes = dest_bytes; // Modes will change this (below)
   481     for( level=0; level<= last_level; level++ ) {
   482         unsigned char data[dest_bytes];
   483         /* load data from image, detwiddling/uncompressing as required */
   484         if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   485             src_bytes = (mip_width * mip_height);
   486             int bank = (mode >> 25) &0x03;
   487             uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
   488             unsigned char tmp[src_bytes];
   489             pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
   490             if( bpp_shift == 2 ) {
   491                 decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   492             } else {
   493                 decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   494             }
   495         } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   496             src_bytes = (mip_width * mip_height) >> 1;
   497             int bank = (mode >>21 ) & 0x3F;
   498             uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
   499             unsigned char tmp[src_bytes];
   500             pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
   501             if( bpp_shift == 2 ) {
   502                 decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   503             } else {
   504                 decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   505             }
   506         } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   507             src_bytes = ((mip_width*mip_height)<<1);
   508             unsigned char tmp[src_bytes];
   509             if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   510                 pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
   511             } else {
   512                 pvr2_vram64_read( tmp, texture_addr, src_bytes );
   513             }
   514             yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
   515         } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   516             src_bytes = ((mip_width*mip_height) >> 2);
   517             unsigned char tmp[src_bytes];
   518             if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   519                 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
   520             } else {
   521                 pvr2_vram64_read( tmp, texture_addr, src_bytes );
   522             }
   523             vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
   524         } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   525             pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
   526         } else {
   527             pvr2_vram64_read( data, texture_addr, src_bytes );
   528         }
   530         /* Pass to GL */
   531         if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
   532             glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
   533                     data + (3 << bpp_shift) );
   534         } else {
   535             glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
   536                     data );
   537             if( mip_width > 2 ) {
   538                 mip_width >>= 1;
   539                 mip_height >>= 1;
   540                 dest_bytes >>= 2;
   541                 src_bytes >>= 2;
   542             }
   543             texture_addr -= src_bytes;
   544         }
   545     }
   547     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   548     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   549 }
   551 static int texcache_find_texture_slot( uint32_t texture_word, int width, int height )
   552 {
   553     uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   554     uint32_t texture_page = texture_addr >> 12;
   555     texcache_entry_index next;
   556     texcache_entry_index idx = texcache_page_lookup[texture_page];
   557     while( idx != EMPTY_ENTRY ) {
   558         texcache_entry_t entry = &texcache_active_list[idx];
   559         if( entry->texture_addr == texture_addr &&
   560                 entry->mode == texture_word &&
   561                 entry->width == width &&
   562                 entry->height == height ) {
   563             entry->lru_count = texcache_ref_counter++;
   564             return idx;
   565         }
   566         idx = entry->next;
   567     }
   568     return -1;
   569 }
   571 static int texcache_alloc_texture_slot( uint32_t texture_word, int width, int height )
   572 {
   573     uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   574     uint32_t texture_page = texture_addr >> 12;
   575     texcache_entry_index slot = 0;
   577     if( texcache_free_ptr < MAX_TEXTURES ) {
   578         slot = texcache_free_list[texcache_free_ptr++];
   579     } else {
   580         slot = texcache_evict_lru();
   581     }
   583     /* Construct new entry */
   584     texcache_active_list[slot].texture_addr = texture_addr;
   585     texcache_active_list[slot].width = width;
   586     texcache_active_list[slot].height = height;
   587     texcache_active_list[slot].mode = texture_word;
   588     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   590     /* Add entry to the lookup table */
   591     int next = texcache_page_lookup[texture_page];
   592     if( next == slot ) {
   593         int i;
   594         fprintf( stderr, "Active list: " );
   595         for( i=0; i<MAX_TEXTURES; i++ ) {
   596             fprintf( stderr, "%d, ", texcache_active_list[i].next );
   597         }
   598         fprintf( stderr, "\n" );
   599         assert( next != slot );
   601     }
   602     assert( next != slot );
   603     texcache_active_list[slot].next = next;
   604     texcache_page_lookup[texture_page] = slot;
   605     return slot;
   606 }
   608 /**
   609  * Return a texture ID for the texture specified at the supplied address
   610  * and given parameters (the same sequence of bytes could in theory have
   611  * multiple interpretations). We use the texture address as the primary
   612  * index, but allow for multiple instances at each address. The texture
   613  * will be bound to the GL_TEXTURE_2D target before being returned.
   614  * 
   615  * If the texture has already been bound, return the ID to which it was
   616  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   617  */
   618 GLuint texcache_get_texture( uint32_t texture_word, int width, int height )
   619 {
   620     int slot = texcache_find_texture_slot( texture_word, width, height );
   622     if( slot == -1 ) {
   623         /* Not found - check the free list */
   624         slot = texcache_alloc_texture_slot( texture_word, width, height );
   626         /* Construct the GL texture */
   627         uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   628         glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   629         texcache_load_texture( texture_addr, width, height, texture_word );
   630     }
   632     return texcache_active_list[slot].texture_id;
   633 }
   635 render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
   636 {
   637     INFO( "Rendering to texture!" );
   638     uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
   639     switch( mode ) {
   640     case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
   641     case COLFMT_RGB565:   texture_word |= PVR2_TEX_FORMAT_RGB565; break;
   642     case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
   643     default:
   644         WARN( "Rendering to non-texture colour format" );
   645     }
   646     if( is_npot_texture(width) )
   647         texture_word |= PVR2_TEX_STRIDE;
   650     int slot = texcache_find_texture_slot( texture_word, width, height );
   651     if( slot == -1 ) {
   652         slot = texcache_alloc_texture_slot( texture_word, width, height );
   653     }
   655     texcache_entry_t entry = &texcache_active_list[slot];
   657     if( entry->buffer == NULL ) {
   658         entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
   659     } else if( entry->buffer->width != width || entry->buffer->height != height ) {        
   660         texcache_release_render_buffer(entry->buffer);
   661         entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
   662     }
   664     return entry->buffer;
   665 }
   667 /**
   668  * Check the integrity of the texcache. Verifies that every cache slot
   669  * appears exactly once on either the free list or one page list. For 
   670  * active slots, the texture address must also match the page it appears on.
   671  * 
   672  */
   673 void texcache_integrity_check()
   674 {
   675     int i;
   676     int slot_found[MAX_TEXTURES];
   678     memset( slot_found, 0, sizeof(slot_found) );
   680     /* Check entries on the free list */
   681     for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
   682         int slot = texcache_free_list[i];
   683         assert( slot_found[slot] == 0 );
   684         assert( texcache_active_list[slot].next == EMPTY_ENTRY );
   685         slot_found[slot] = 1;
   686     }
   688     /* Check entries on the active lists */
   689     for( i=0; i< PVR2_RAM_PAGES; i++ ) {
   690         int slot = texcache_page_lookup[i];
   691         while( slot != EMPTY_ENTRY ) {
   692             assert( slot_found[slot] == 0 );
   693             assert( (texcache_active_list[slot].texture_addr >> 12) == i );
   694             slot_found[slot] = 2;
   695             slot = texcache_active_list[slot].next;
   696         }
   697     }
   699     /* Make sure we didn't miss any entries */
   700     for( i=0; i<MAX_TEXTURES; i++ ) {
   701         assert( slot_found[i] != 0 );
   702     }
   703 }
.