Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 856:02ac5f37bfc9
prev736:a02d1475ccfd
next860:b429964761c8
author nkeynes
date Thu Sep 11 22:51:24 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Completely untested render-to-texture work in progress
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         case PVR2_TEX_FORMAT_ARGB1555:
   402             format = GL_BGRA;
   403             type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   404             break;
   405         case PVR2_TEX_FORMAT_RGB565:
   406             intFormat = GL_RGB;
   407             format = GL_RGB;
   408             type = GL_UNSIGNED_SHORT_5_6_5;
   409             break;
   410         case PVR2_TEX_FORMAT_ARGB4444:
   411             format = GL_BGRA;
   412             type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   413             break;
   414         case PVR2_TEX_FORMAT_YUV422:
   415             /* YUV422 isn't directly supported by most implementations, so decode
   416              * it to a (reasonably) standard ARGB32.
   417              */
   418             bpp_shift = 2;
   419             format = GL_BGRA;
   420             type = GL_UNSIGNED_BYTE;
   421             break;
   422         case PVR2_TEX_FORMAT_BUMPMAP:
   423             ERROR( "Bumpmap not supported" );
   424             return;
   425         default:
   426             ERROR( "Undefined texture format" );
   427             return;
   428     }
   430     if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
   431             tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   432         /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
   433         uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
   434         unsigned char data[(width*height) << bpp_shift];
   435         if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   436             unsigned char tmp[(width*height)<<1];
   437             pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
   438             yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
   439         } else {
   440             pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
   441         }
   442         glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
   443         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   444         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   445         return;
   446     } 
   448     if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   449         uint16_t tmp[VQ_CODEBOOK_SIZE];
   450         pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
   451         texture_addr += VQ_CODEBOOK_SIZE;
   452         vq_get_codebook( &codebook, tmp );
   453     }
   455     int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
   456     if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
   457         uint32_t src_offset = 0;
   458         filter = GL_LINEAR_MIPMAP_LINEAR;
   459         mip_height = height = width;
   460         while( (1<<last_level) < width ) {
   461             last_level++;
   462             src_offset += ((width>>last_level)*(width>>last_level));
   463         }
   464         if( width != 1 ) {
   465             src_offset += 3;
   466         }
   467         if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   468             src_offset >>= 2;
   469         } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   470             src_offset >>= 1;
   471         } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   472             src_offset <<= 1;
   473         } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   474             src_offset <<= bpp_shift;
   475         }
   476         texture_addr += src_offset;
   477     }
   480     dest_bytes = (mip_width * mip_height) << bpp_shift;
   481     src_bytes = dest_bytes; // Modes will change this (below)
   483     for( level=0; level<= last_level; level++ ) {
   484         unsigned char data[dest_bytes];
   485         /* load data from image, detwiddling/uncompressing as required */
   486         if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   487             src_bytes = (mip_width * mip_height);
   488             int bank = (mode >> 25) &0x03;
   489             uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
   490             unsigned char tmp[src_bytes];
   491             pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
   492             if( bpp_shift == 2 ) {
   493                 decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   494             } else {
   495                 decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   496             }
   497         } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   498             src_bytes = (mip_width * mip_height) >> 1;
   499             int bank = (mode >>21 ) & 0x3F;
   500             uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
   501             unsigned char tmp[src_bytes];
   502             pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
   503             if( bpp_shift == 2 ) {
   504                 decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   505             } else {
   506                 decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   507             }
   508         } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   509             src_bytes = ((mip_width*mip_height)<<1);
   510             unsigned char tmp[src_bytes];
   511             if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   512                 pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
   513             } else {
   514                 pvr2_vram64_read( tmp, texture_addr, src_bytes );
   515             }
   516             yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
   517         } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   518             src_bytes = ((mip_width*mip_height) >> 2);
   519             unsigned char tmp[src_bytes];
   520             if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   521                 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
   522             } else {
   523                 pvr2_vram64_read( tmp, texture_addr, src_bytes );
   524             }
   525             vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
   526         } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   527             pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
   528         } else {
   529             pvr2_vram64_read( data, texture_addr, src_bytes );
   530         }
   532         /* Pass to GL */
   533         if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
   534             glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
   535                     data + (3 << bpp_shift) );
   536         } else {
   537             glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
   538                     data );
   539             if( mip_width > 2 ) {
   540                 mip_width >>= 1;
   541                 mip_height >>= 1;
   542                 dest_bytes >>= 2;
   543                 src_bytes >>= 2;
   544             }
   545             texture_addr -= src_bytes;
   546         }
   547     }
   549     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   550     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   551 }
   553 static int texcache_find_texture_slot( uint32_t texture_word, int width, int height )
   554 {
   555     uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   556     uint32_t texture_page = texture_addr >> 12;
   557     texcache_entry_index next;
   558     texcache_entry_index idx = texcache_page_lookup[texture_page];
   559     while( idx != EMPTY_ENTRY ) {
   560         texcache_entry_t entry = &texcache_active_list[idx];
   561         if( entry->texture_addr == texture_addr &&
   562                 entry->mode == texture_word &&
   563                 entry->width == width &&
   564                 entry->height == height ) {
   565             entry->lru_count = texcache_ref_counter++;
   566             return idx;
   567         }
   568         idx = entry->next;
   569     }
   570     return -1;
   571 }
   573 static int texcache_alloc_texture_slot( uint32_t texture_word, int width, int height )
   574 {
   575     uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   576     uint32_t texture_page = texture_addr >> 12;
   577     texcache_entry_index slot = 0;
   579     if( texcache_free_ptr < MAX_TEXTURES ) {
   580         slot = texcache_free_list[texcache_free_ptr++];
   581     } else {
   582         slot = texcache_evict_lru();
   583     }
   585     /* Construct new entry */
   586     texcache_active_list[slot].texture_addr = texture_addr;
   587     texcache_active_list[slot].width = width;
   588     texcache_active_list[slot].height = height;
   589     texcache_active_list[slot].mode = texture_word;
   590     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   592     /* Add entry to the lookup table */
   593     int next = texcache_page_lookup[texture_page];
   594     if( next == slot ) {
   595         int i;
   596         fprintf( stderr, "Active list: " );
   597         for( i=0; i<MAX_TEXTURES; i++ ) {
   598             fprintf( stderr, "%d, ", texcache_active_list[i].next );
   599         }
   600         fprintf( stderr, "\n" );
   601         assert( next != slot );
   603     }
   604     assert( next != slot );
   605     texcache_active_list[slot].next = next;
   606     texcache_page_lookup[texture_page] = slot;
   607     return slot;
   608 }
   610 /**
   611  * Return a texture ID for the texture specified at the supplied address
   612  * and given parameters (the same sequence of bytes could in theory have
   613  * multiple interpretations). We use the texture address as the primary
   614  * index, but allow for multiple instances at each address. The texture
   615  * will be bound to the GL_TEXTURE_2D target before being returned.
   616  * 
   617  * If the texture has already been bound, return the ID to which it was
   618  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   619  */
   620 GLuint texcache_get_texture( uint32_t texture_word, int width, int height )
   621 {
   622     int slot = texcache_find_texture_slot( texture_word, width, height );
   624     if( slot == -1 ) {
   625         /* Not found - check the free list */
   626         slot = texcache_alloc_texture_slot( texture_word, width, height );
   628         /* Construct the GL texture */
   629         uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   630         glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   631         texcache_load_texture( texture_addr, width, height, texture_word );
   632     }
   634     return texcache_active_list[slot].texture_id;
   635 }
   637 render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
   638 {
   639     INFO( "Rendering to texture!" );
   640     uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
   641     switch( mode ) {
   642     case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
   643     case COLFMT_RGB565:   texture_word |= PVR2_TEX_FORMAT_RGB565; break;
   644     case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
   645     default:
   646         WARN( "Rendering to non-texture colour format" );
   647     }
   648     if( is_npot_texture(width) )
   649         texture_word |= PVR2_TEX_STRIDE;
   652     int slot = texcache_find_texture_slot( texture_word, width, height );
   653     if( slot == -1 ) {
   654         slot = texcache_alloc_texture_slot( texture_word, width, height );
   655     }
   657     texcache_entry_t entry = &texcache_active_list[slot];
   658     if( entry->width != width || entry->height != height ) {
   659         glBindTexture(GL_TEXTURE_2D, entry->texture_id );
   660         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
   661         if( entry->buffer != NULL ) {
   662             texcache_release_render_buffer(entry->buffer);
   663         }
   664         entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
   665     } else {
   666         if( entry->buffer == NULL )
   667             entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
   668     }
   670     return entry->buffer;
   671 }
   673 /**
   674  * Check the integrity of the texcache. Verifies that every cache slot
   675  * appears exactly once on either the free list or one page list. For 
   676  * active slots, the texture address must also match the page it appears on.
   677  * 
   678  */
   679 void texcache_integrity_check()
   680 {
   681     int i;
   682     int slot_found[MAX_TEXTURES];
   684     memset( slot_found, 0, sizeof(slot_found) );
   686     /* Check entries on the free list */
   687     for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
   688         int slot = texcache_free_list[i];
   689         assert( slot_found[slot] == 0 );
   690         assert( texcache_active_list[slot].next == EMPTY_ENTRY );
   691         slot_found[slot] = 1;
   692     }
   694     /* Check entries on the active lists */
   695     for( i=0; i< PVR2_RAM_PAGES; i++ ) {
   696         int slot = texcache_page_lookup[i];
   697         while( slot != EMPTY_ENTRY ) {
   698             assert( slot_found[slot] == 0 );
   699             assert( (texcache_active_list[slot].texture_addr >> 12) == i );
   700             slot_found[slot] = 2;
   701             slot = texcache_active_list[slot].next;
   702         }
   703     }
   705     /* Make sure we didn't miss any entries */
   706     for( i=0; i<MAX_TEXTURES; i++ ) {
   707         assert( slot_found[i] != 0 );
   708     }
   709 }
.