Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 1236:d93175c36387
prev1233:06923d1020de
next1256:a9d29fe74bf3
author nkeynes
date Fri Feb 24 21:17:47 2012 +1000 (9 years ago)
permissions -rw-r--r--
last change Factor video_width/video_height out into video_gl.c
Convert immediate-mode bits in video_gl.c into a structure for glDrawArray
Move setOrtho into defineOrthoMatrix in glutil.c
Rearrange various GL settings to keep a consistent state
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"
    25 #include "pvr2/glutil.h"
    27 /** Specifies the maximum number of OpenGL
    28  * textures we're willing to have open at a time. If more are
    29  * needed, textures will be evicted in LRU order.
    30  */
    31 #define MAX_TEXTURES 256
    33 /**
    34  * Data structure:
    35  *
    36  * Main operations:
    37  *    find entry by texture_addr
    38  *    add new entry
    39  *    move entry to tail of lru list
    40  *    remove entry
    41  */
    43 typedef signed short texcache_entry_index;
    44 #define EMPTY_ENTRY -1
    46 static texcache_entry_index texcache_free_ptr = 0;
    47 static GLuint texcache_free_list[MAX_TEXTURES];
    49 typedef struct texcache_entry {
    50     uint32_t texture_addr;
    51     uint32_t poly2_mode, tex_mode;
    52     GLuint texture_id;
    53     render_buffer_t buffer;
    54     texcache_entry_index next;
    55     uint32_t lru_count;
    56 } *texcache_entry_t;
    58 static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
    59 static uint32_t texcache_ref_counter;
    60 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
    61 static uint32_t texcache_palette_mode;
    62 static uint32_t texcache_stride_width;
    63 static gboolean texcache_have_palette_shader;
    64 static gboolean texcache_palette_valid;
    65 static GLuint texcache_palette_texid;
    67 /**
    68  * Initialize the texture cache.
    69  */
    70 void texcache_init( )
    71 {
    72     int i;
    73     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    74         texcache_page_lookup[i] = EMPTY_ENTRY;
    75     }
    76     for( i=0; i<MAX_TEXTURES; i++ ) {
    77         texcache_free_list[i] = i;
    78         texcache_active_list[i].texture_addr = -1;
    79         texcache_active_list[i].buffer = NULL;
    80         texcache_active_list[i].next = EMPTY_ENTRY;
    81     }
    82     texcache_free_ptr = 0;
    83     texcache_ref_counter = 0;
    84     texcache_palette_mode = -1;
    85     texcache_stride_width = 0;
    86 }
    88 /**
    89  * Setup the initial texture ids (must be called after the GL context is
    90  * prepared)
    91  */
    92 void texcache_gl_init( gboolean withShaders )
    93 {
    94     int i;
    95     GLuint texids[MAX_TEXTURES];
    97     if( withShaders ) {
    98         texcache_have_palette_shader = TRUE;
    99         texcache_palette_valid = FALSE;
   100         glGenTextures(1, &texcache_palette_texid );
   102         /* Bind the texture and set the params */
   103         glActiveTexture(GL_TEXTURE1);
   104         glBindTexture(GL_TEXTURE_2D, texcache_palette_texid);
   105         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   106         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   107         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
   108         glActiveTexture(GL_TEXTURE0);
   110     } else {
   111         texcache_have_palette_shader = FALSE;
   112     }
   114     glGenTextures( MAX_TEXTURES, texids );
   115     for( i=0; i<MAX_TEXTURES; i++ ) {
   116         texcache_active_list[i].texture_id = texids[i];
   117     }
   118 }
   120 void texcache_release_render_buffer( render_buffer_t buffer )
   121 {
   122     if( !buffer->flushed ) 
   123         pvr2_render_buffer_copy_to_sh4(buffer);
   124     pvr2_destroy_render_buffer(buffer);
   125 }
   127 /**
   128  * Flush all textures from the cache, returning them to the free list.
   129  */
   130 void texcache_flush( )
   131 {
   132     int i;
   133     /* clear structures */
   134     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
   135         texcache_page_lookup[i] = EMPTY_ENTRY;
   136     }
   137     for( i=0; i<MAX_TEXTURES; i++ ) {
   138         texcache_free_list[i] = i;
   139         texcache_active_list[i].next = EMPTY_ENTRY;
   140         texcache_active_list[i].texture_addr = -1;
   141         if( texcache_active_list[i].buffer != NULL ) {
   142             texcache_release_render_buffer(texcache_active_list[i].buffer);
   143             texcache_active_list[i].buffer = NULL;
   144         }
   145     }
   146     texcache_free_ptr = 0;
   147     texcache_ref_counter = 0;
   148 }
   150 /**
   151  * Flush all textures and delete. The cache will be non-functional until
   152  * the next call to texcache_init(). This would typically be done if
   153  * switching GL targets.
   154  */    
   155 void texcache_shutdown( )
   156 {
   157     GLuint texids[MAX_TEXTURES];
   158     int i;
   159     texcache_flush();
   161     if( texcache_have_palette_shader )
   162         glDeleteTextures( 1, &texcache_palette_texid );
   164     for( i=0; i<MAX_TEXTURES; i++ ) {
   165         texids[i] = texcache_active_list[i].texture_id;
   166     }
   167     glDeleteTextures( MAX_TEXTURES, texids );
   168 }
   170 static void texcache_evict( int slot )
   171 {
   172     /* Remove the selected slot from the lookup table */
   173     assert( texcache_active_list[slot].texture_addr != -1 );
   174     uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
   175     texcache_entry_index replace_next = texcache_active_list[slot].next;
   176     texcache_active_list[slot].texture_addr = -1;
   177     texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
   178     if( texcache_active_list[slot].buffer != NULL ) {
   179         texcache_release_render_buffer(texcache_active_list[slot].buffer);
   180         texcache_active_list[slot].buffer = NULL;
   181     }
   182     if( texcache_page_lookup[evict_page] == slot ) {
   183         texcache_page_lookup[evict_page] = replace_next;
   184     } else {
   185         texcache_entry_index idx = texcache_page_lookup[evict_page];
   186         texcache_entry_index next;
   187         do {
   188             next = texcache_active_list[idx].next;
   189             if( next == slot ) {
   190                 assert( idx != replace_next );
   191                 texcache_active_list[idx].next = replace_next;
   192                 break;
   193             }
   194             idx = next;
   195         } while( next != EMPTY_ENTRY );
   196     }
   197 }
   199 /**
   200  * Evict a single texture from the cache.
   201  * @return the slot of the evicted texture.
   202  */
   203 static texcache_entry_index texcache_evict_lru( void )
   204 {
   205     /* Full table scan - take over the entry with the lowest lru value */
   206     texcache_entry_index slot = 0;
   207     int lru_value = texcache_active_list[0].lru_count;
   208     int i;
   209     for( i=1; i<MAX_TEXTURES; i++ ) {
   210         /* FIXME: account for rollover */
   211         if( texcache_active_list[i].lru_count < lru_value ) {
   212             slot = i;
   213             lru_value = texcache_active_list[i].lru_count;
   214         }
   215     }
   216     texcache_evict(slot);
   218     return slot;
   219 }
   221 /**
   222  * Evict all textures contained in the page identified by a texture address.
   223  */
   224 void texcache_invalidate_page( uint32_t texture_addr ) {
   225     uint32_t texture_page = texture_addr >> 12;
   226     texcache_entry_index idx = texcache_page_lookup[texture_page];
   227     if( idx == EMPTY_ENTRY )
   228         return;
   229     assert( texcache_free_ptr >= 0 );
   230     do {
   231         texcache_entry_t entry = &texcache_active_list[idx];
   232         entry->texture_addr = -1;
   233         if( entry->buffer != NULL ) {
   234             texcache_release_render_buffer(entry->buffer);
   235             entry->buffer = NULL;
   236         }
   237         /* release entry */
   238         texcache_free_ptr--;
   239         texcache_free_list[texcache_free_ptr] = idx;
   240         idx = entry->next;
   241         entry->next = EMPTY_ENTRY;
   242     } while( idx != EMPTY_ENTRY );
   243     texcache_page_lookup[texture_page] = EMPTY_ENTRY;
   244 }
   246 /**
   247  * Load the palette into 4 textures of 256 entries each. This mirrors the
   248  * banking done by the PVR2 for 8-bit textures, and also ensures that we
   249  * can use 8-bit paletted textures ourselves.
   250  */
   251 static void texcache_load_palette_texture( gboolean format_changed )
   252 {
   253     GLint format, type, intFormat = GL_RGBA;
   254     unsigned i;
   255     int bpp = 2;
   256     uint32_t *palette = (uint32_t *)mmio_region_PVR2PAL.mem;
   257     uint16_t packed_palette[1024];
   258     char *data = (char *)palette;
   260     switch( texcache_palette_mode ) {
   261     case 0: /* ARGB1555 */
   262         format = GL_BGRA;
   263         type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   264         break;
   265     case 1:  /* RGB565 */
   266         intFormat = GL_RGB;
   267         format = GL_RGB;
   268         type = GL_UNSIGNED_SHORT_5_6_5;
   269         break;
   270     case 2: /* ARGB4444 */
   271         format = GL_BGRA;
   272         type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   273         break;
   274     case 3: /* ARGB8888 */
   275         format = GL_BGRA;
   276         type = GL_UNSIGNED_BYTE;
   277         bpp = 4;
   278         break;
   279     default:
   280         break; /* Can't happen */
   281     }
   284     if( bpp == 2 ) {
   285         for( i=0; i<1024; i++ ) {
   286             packed_palette[i] = (uint16_t)palette[i];
   287         }
   288         data = (char *)packed_palette;
   290     }
   292     glActiveTexture(GL_TEXTURE1);
   293     if( format_changed )
   294         glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 1024, 1, 0, format, type, data );
   295     else
   296         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1024, 1, format, type, data);
   297     glActiveTexture(GL_TEXTURE0);
   298     texcache_palette_valid = TRUE;
   299 }
   302 /**
   303  * Mark the palette as having changed. If we have palette support (via shaders)
   304  * we just flag the palette, otherwise we have to invalidate all palette
   305  * textures.
   306  */
   307 void texcache_invalidate_palette( )
   308 {
   309     if( texcache_have_palette_shader ) {
   310         texcache_palette_valid = FALSE;
   311     } else {
   312         int i;
   313         for( i=0; i<MAX_TEXTURES; i++ ) {
   314             if( texcache_active_list[i].texture_addr != -1 &&
   315                     PVR2_TEX_IS_PALETTE(texcache_active_list[i].tex_mode) ) {
   316                 texcache_evict( i );
   317                 texcache_free_ptr--;
   318                 texcache_free_list[texcache_free_ptr] = i;
   319             }
   320         }
   321     }
   322 }
   323 /**
   324  * Mark all stride textures as needing a re-read (ie when the stride width
   325  * is changed).
   326  */
   327 void texcache_invalidate_stride( )
   328 {
   329     int i;
   330     for( i=0; i<MAX_TEXTURES; i++ ) {
   331         if( texcache_active_list[i].texture_addr != -1 &&
   332                 PVR2_TEX_IS_STRIDE(texcache_active_list[i].tex_mode) ) {
   333             texcache_evict( i );
   334             texcache_free_ptr--;
   335             texcache_free_list[texcache_free_ptr] = i;
   336         }
   337     }
   338 }
   340 void texcache_begin_scene( uint32_t palette_mode, uint32_t stride )
   341 {
   342     gboolean format_changed = FALSE;
   343     if( palette_mode != texcache_palette_mode ) {
   344         texcache_invalidate_palette();
   345         format_changed = TRUE;
   346     }
   347     if( stride != texcache_stride_width )
   348         texcache_invalidate_stride();
   350     texcache_palette_mode = palette_mode;
   351     texcache_stride_width = stride;
   353     if( !texcache_palette_valid && texcache_have_palette_shader )
   354         texcache_load_palette_texture(format_changed);
   355 }
   357 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   358 {
   359     int i;
   360     for( i=0; i<inbytes; i++ ) {
   361         *out++ = pal[*in++];
   362     }
   363 }
   365 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   366 {
   367     int i;
   368     for( i=0; i<inbytes; i++ ) {
   369         *out++ = (uint16_t)pal[*in++];
   370     }
   371 }
   373 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   374 {
   375     int i;
   376     for( i=0; i<inbytes; i++ ) {
   377         *out++ = pal[*in & 0x0F];
   378         *out++ = pal[(*in >> 4)];
   379         in++;
   380     }
   381 }
   383 static void decode_pal4_to_pal8( uint8_t *out, uint8_t *in, int inbytes )
   384 {
   385     int i;
   386     for( i=0; i<inbytes; i++ ) {
   387         *out++ = (uint8_t)(*in & 0x0F);
   388         *out++ = (uint8_t)(*in >> 4);
   389         in++;
   390     }
   391 }
   395 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   396 {
   397     int i;
   398     for( i=0; i<inbytes; i++ ) {
   399         *out++ = (uint16_t)pal[*in & 0x0F];
   400         *out++ = (uint16_t)pal[(*in >> 4)];
   401         in++;
   402     }
   403 }
   405 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
   407 struct vq_codebook {
   408     uint16_t quad[256][4];
   409 };
   411 static void vq_get_codebook( struct vq_codebook *codebook, 
   412                              uint16_t *input )
   413 {
   414     /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
   415     uint16_t *p = (uint16_t *)input;
   416     int i;
   417     for( i=0; i<256; i++ ) {
   418         codebook->quad[i][0] = *p++;
   419         codebook->quad[i][2] = *p++;
   420         codebook->quad[i][1] = *p++;
   421         codebook->quad[i][3] = *p++;
   422     }
   423 }    
   425 static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
   426                        struct vq_codebook *codebook ) {
   427     int i,j;
   429     uint8_t *c = (uint8_t *)input;
   430     for( j=0; j<height; j+=2 ) {
   431         for( i=0; i<width; i+=2 ) {
   432             uint8_t code = *c++;
   433             output[i + j*width] = codebook->quad[code][0];
   434             output[i + 1 + j*width] = codebook->quad[code][1];
   435             output[i + (j+1)*width] = codebook->quad[code][2];
   436             output[i + 1 + (j+1)*width] = codebook->quad[code][3];
   437         }
   438     }
   439 }
   441 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
   442 {
   443     u -= 128;
   444     v -= 128;
   445     int r = (int)(y + v*1.375);
   446     int g = (int)(y - u*0.34375 - v*0.6875);
   447     int b = (int)(y + u*1.71875);
   448     if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
   449     if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
   450     if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
   451     return 0xFF000000 | (r<<16) | (g<<8) | (b);
   452 }
   455 /**
   456  * Convert raster YUV texture data into RGB32 data - most GL implementations don't
   457  * directly support this format unfortunately. The input data is formatted as
   458  * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
   459  * in floating point.
   460  */
   461 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
   462 {
   463     int x, y;
   464     uint32_t *p = input;
   465     for( y=0; y<height; y++ ) {
   466         for( x=0; x<width; x+=2 ) {
   467             float u = (float)(*p & 0xFF);
   468             float y0 = (float)( (*p>>8)&0xFF );
   469             float v = (float)( (*p>>16)&0xFF );
   470             float y1 = (float)( (*p>>24)&0xFF );
   471             *output++ = yuv_to_rgb32( y0, u, v ); 
   472             *output++ = yuv_to_rgb32( y1, u, v );
   473             p++;
   474         }
   475     }
   476 }
   478 static gboolean is_npot_texture( int width )
   479 {
   480     while( width != 0 ) {
   481         if( width & 1 ) 
   482             return width != 1;
   483         width >>= 1;
   484     }
   485     return TRUE;
   486 }
   488 /**
   489  * Load texture data from the given address and parameters into the currently
   490  * bound OpenGL texture.
   491  */
   492 static void texcache_load_texture( uint32_t texture_addr, int width, int height,
   493                                    int mode ) {
   494     int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
   495     GLint intFormat = GL_RGBA, format, type;
   496     int tex_format = mode & PVR2_TEX_FORMAT_MASK;
   497     struct vq_codebook codebook;
   498     GLint min_filter = GL_LINEAR;
   499     GLint max_filter = GL_LINEAR;
   500     GLint mipmapfilter = GL_LINEAR_MIPMAP_LINEAR;
   502     /* Decode the format parameters */
   503     switch( tex_format ) {
   504     case PVR2_TEX_FORMAT_IDX4:
   505     case PVR2_TEX_FORMAT_IDX8:
   506         if( texcache_have_palette_shader ) {
   507             intFormat = GL_ALPHA;
   508             format = GL_ALPHA;
   509             type = GL_UNSIGNED_BYTE;
   510             bpp_shift = 0;
   511             min_filter = max_filter = GL_NEAREST;
   512             mipmapfilter = GL_NEAREST_MIPMAP_NEAREST;
   513         } else {
   514             /* For indexed-colour modes, we need to lookup the palette control
   515              * word to determine the de-indexed texture format.
   516              */
   517             switch( texcache_palette_mode ) {
   518             case 0: /* ARGB1555 */
   519                 format = GL_BGRA;
   520                 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   521                 break;
   522             case 1:  /* RGB565 */
   523                 intFormat = GL_RGB;
   524                 format = GL_RGB;
   525                 type = GL_UNSIGNED_SHORT_5_6_5;
   526                 break;
   527             case 2: /* ARGB4444 */
   528                 format = GL_BGRA;
   529                 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   530                 break;
   531             case 3: /* ARGB8888 */
   532                 format = GL_BGRA;
   533                 type = GL_UNSIGNED_BYTE;
   534                 bpp_shift = 2;
   535                 break;
   536             default:
   537                 return; /* Can't happen, but it makes gcc stop complaining */
   538             }
   539         }
   540         break;
   542         default:
   543         case PVR2_TEX_FORMAT_ARGB1555:
   544             format = GL_BGRA;
   545             type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   546             break;
   547         case PVR2_TEX_FORMAT_RGB565:
   548             intFormat = GL_RGB;
   549             format = GL_RGB;
   550             type = GL_UNSIGNED_SHORT_5_6_5;
   551             break;
   552         case PVR2_TEX_FORMAT_ARGB4444:
   553             format = GL_BGRA;
   554             type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   555             break;
   556         case PVR2_TEX_FORMAT_YUV422:
   557             /* YUV422 isn't directly supported by most implementations, so decode
   558              * it to a (reasonably) standard ARGB32.
   559              */
   560             bpp_shift = 2;
   561             format = GL_BGRA;
   562             type = GL_UNSIGNED_BYTE;
   563             break;
   564         case PVR2_TEX_FORMAT_BUMPMAP:
   565             WARN( "Bumpmap not supported" );
   566             return;
   567     }
   569     if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
   570             tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   571         /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
   572         unsigned char data[(width*height) << bpp_shift];
   573         if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   574             unsigned char tmp[(width*height)<<1];
   575             pvr2_vram64_read_stride( tmp, width<<1, texture_addr, texcache_stride_width<<1, height );
   576             yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
   577         } else {
   578             pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, texcache_stride_width<<bpp_shift, height );
   579         }
   580         glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
   581         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
   582         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
   583         return;
   584     } 
   586     if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   587         uint16_t tmp[VQ_CODEBOOK_SIZE];
   588         pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
   589         texture_addr += VQ_CODEBOOK_SIZE;
   590         vq_get_codebook( &codebook, tmp );
   591     }
   593     int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
   594     if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
   595         uint32_t src_offset = 0;
   596         min_filter = mipmapfilter;
   597         mip_height = height = width;
   598         while( (1<<last_level) < width ) {
   599             last_level++;
   600             src_offset += ((width>>last_level)*(width>>last_level));
   601         }
   602         if( width != 1 ) {
   603             src_offset += 3;
   604         }
   605         if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   606             src_offset >>= 2;
   607         } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   608             src_offset >>= 1;
   609         } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   610             src_offset <<= 1;
   611         } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   612             src_offset <<= bpp_shift;
   613         }
   614         texture_addr += src_offset;
   615     }
   618     dest_bytes = (mip_width * mip_height) << bpp_shift;
   619     src_bytes = dest_bytes; // Modes will change this (below)
   621     for( level=0; level<= last_level; level++ ) {
   622         unsigned char data[dest_bytes];
   623         /* load data from image, detwiddling/uncompressing as required */
   624         if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   625             if( texcache_have_palette_shader ) {
   626                 pvr2_vram64_read_twiddled_8( data, texture_addr, mip_width, mip_height );
   627             } else {
   628                 src_bytes = (mip_width * mip_height);
   629                 int bank = (mode >> 25) &0x03;
   630                 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
   631                 unsigned char tmp[src_bytes];
   632                 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
   633                 if( bpp_shift == 2 ) {
   634                     decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   635                 } else {
   636                     decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   637                 }
   638             }
   639         } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   640             src_bytes = (mip_width * mip_height) >> 1;
   641             unsigned char tmp[src_bytes];
   642             if( texcache_have_palette_shader ) {
   643                 pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
   644                 decode_pal4_to_pal8( data, tmp, src_bytes );
   645             } else {
   646                 int bank = (mode >>21 ) & 0x3F;
   647                 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
   648                 pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
   649                 if( bpp_shift == 2 ) {
   650                     decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
   651                 } else {
   652                     decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
   653                 }
   654             }
   655         } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   656             src_bytes = ((mip_width*mip_height)<<1);
   657             unsigned char tmp[src_bytes];
   658             if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   659                 pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
   660             } else {
   661                 pvr2_vram64_read( tmp, texture_addr, src_bytes );
   662             }
   663             yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
   664         } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   665             src_bytes = ((mip_width*mip_height) >> 2);
   666             unsigned char tmp[src_bytes];
   667             if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   668                 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
   669             } else {
   670                 pvr2_vram64_read( tmp, texture_addr, src_bytes );
   671             }
   672             vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
   673         } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   674             pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
   675         } else {
   676             pvr2_vram64_read( data, texture_addr, src_bytes );
   677         }
   679         /* Pass to GL */
   680         if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
   681             glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
   682                     data + (3 << bpp_shift) );
   683         } else {
   684             glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
   685                     data );
   686             if( mip_width > 2 ) {
   687                 mip_width >>= 1;
   688                 mip_height >>= 1;
   689                 dest_bytes >>= 2;
   690                 src_bytes >>= 2;
   691             }
   692             texture_addr -= src_bytes;
   693         }
   694     }
   696     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
   697     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
   698 }
   700 static int texcache_find_texture_slot( uint32_t poly2_masked_word, uint32_t texture_word )
   701 {
   702     uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   703     uint32_t texture_page = texture_addr >> 12;
   704     texcache_entry_index next;
   705     texcache_entry_index idx = texcache_page_lookup[texture_page];
   706     while( idx != EMPTY_ENTRY ) {
   707         texcache_entry_t entry = &texcache_active_list[idx];
   708         if( entry->tex_mode == texture_word &&
   709                 entry->poly2_mode == poly2_masked_word ) {
   710             entry->lru_count = texcache_ref_counter++;
   711             return idx;
   712         }
   713         idx = entry->next;
   714     }
   715     return -1;
   716 }
   718 static int texcache_alloc_texture_slot( uint32_t poly2_word, uint32_t texture_word )
   719 {
   720     uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   721     uint32_t texture_page = texture_addr >> 12;
   722     texcache_entry_index slot = 0;
   724     if( texcache_free_ptr < MAX_TEXTURES ) {
   725         slot = texcache_free_list[texcache_free_ptr++];
   726     } else {
   727         slot = texcache_evict_lru();
   728     }
   730     /* Construct new entry */
   731     assert( texcache_active_list[slot].texture_addr == -1 );
   732     texcache_active_list[slot].texture_addr = texture_addr;
   733     texcache_active_list[slot].tex_mode = texture_word;
   734     texcache_active_list[slot].poly2_mode = poly2_word;
   735     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   737     /* Add entry to the lookup table */
   738     int next = texcache_page_lookup[texture_page];
   739     if( next == slot ) {
   740         int i;
   741         fprintf( stderr, "Active list: " );
   742         for( i=0; i<MAX_TEXTURES; i++ ) {
   743             fprintf( stderr, "%d, ", texcache_active_list[i].next );
   744         }
   745         fprintf( stderr, "\n" );
   746         assert( next != slot );
   748     }
   749     texcache_active_list[slot].next = next;
   750     texcache_page_lookup[texture_page] = slot;
   751     return slot;
   752 }
   754 /**
   755  * Return a texture ID for the texture specified at the supplied address
   756  * and given parameters (the same sequence of bytes could in theory have
   757  * multiple interpretations). We use the texture address as the primary
   758  * index, but allow for multiple instances at each address.
   759  * 
   760  * If the texture has already been bound, return the ID to which it was
   761  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   762  * The current GL_TEXTURE_2D binding will be changed in this case.
   763  */
   764 GLuint texcache_get_texture( uint32_t poly2_word, uint32_t texture_word )
   765 {
   766     poly2_word &= 0x000F803F; /* Get just the texture-relevant bits */
   767     uint32_t texture_lookup = texture_word;
   768     if( PVR2_TEX_IS_PALETTE(texture_lookup) ) {
   769         texture_lookup &= 0xF81FFFFF; /* Mask out the bank bits */
   770     }
   771     int slot = texcache_find_texture_slot( poly2_word, texture_lookup );
   773     if( slot == -1 ) {
   774         /* Not found - check the free list */
   775         slot = texcache_alloc_texture_slot( poly2_word, texture_lookup );
   777         /* Construct the GL texture */
   778         uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
   779         unsigned width = POLY2_TEX_WIDTH(poly2_word);
   780         unsigned height = POLY2_TEX_HEIGHT(poly2_word);
   782         glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   783         texcache_load_texture( texture_addr, width, height, texture_word );
   785         /* Set texture parameters from the poly2 word */
   786         if( POLY2_TEX_CLAMP_U(poly2_word) ) {
   787             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
   788         } else if( POLY2_TEX_MIRROR_U(poly2_word) ) {
   789             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
   790         } else {
   791             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   792         }
   793         if( POLY2_TEX_CLAMP_V(poly2_word) ) {
   794             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
   795         } else if( POLY2_TEX_MIRROR_V(poly2_word) ) {
   796             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
   797         } else {
   798             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   799         }
   800     }
   802     return texcache_active_list[slot].texture_id;
   803 }
   805 #if 0
   806 render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
   807 {
   808     uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
   809     switch( mode ) {
   810     case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
   811     case COLFMT_RGB565:   texture_word |= PVR2_TEX_FORMAT_RGB565; break;
   812     case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
   813     default:
   814         WARN( "Rendering to non-texture colour format" );
   815     }
   816     if( is_npot_texture(width) )
   817         texture_word |= PVR2_TEX_STRIDE;
   820     int slot = texcache_find_texture_slot( texture_word, width, height );
   821     if( slot == -1 ) {
   822         slot = texcache_alloc_texture_slot( texture_word, width, height );
   823     }
   825     texcache_entry_t entry = &texcache_active_list[slot];
   827     if( entry->buffer == NULL ) {
   828         entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
   829     } else if( entry->buffer->width != width || entry->buffer->height != height ) {        
   830         texcache_release_render_buffer(entry->buffer);
   831         entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
   832     }
   834     return entry->buffer;
   835 }
   836 #endif
   838 /**
   839  * Check the integrity of the texcache. Verifies that every cache slot
   840  * appears exactly once on either the free list or one page list. For 
   841  * active slots, the texture address must also match the page it appears on.
   842  * 
   843  */
   844 void texcache_integrity_check()
   845 {
   846     int i;
   847     int slot_found[MAX_TEXTURES];
   849     memset( slot_found, 0, sizeof(slot_found) );
   851     /* Check entries on the free list */
   852     for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
   853         int slot = texcache_free_list[i];
   854         assert( slot_found[slot] == 0 );
   855         assert( texcache_active_list[slot].next == EMPTY_ENTRY );
   856         slot_found[slot] = 1;
   857     }
   859     /* Check entries on the active lists */
   860     for( i=0; i< PVR2_RAM_PAGES; i++ ) {
   861         int slot = texcache_page_lookup[i];
   862         while( slot != EMPTY_ENTRY ) {
   863             assert( slot_found[slot] == 0 );
   864             assert( (texcache_active_list[slot].texture_addr >> 12) == i );
   865             slot_found[slot] = 2;
   866             slot = texcache_active_list[slot].next;
   867         }
   868     }
   870     /* Make sure we didn't miss any entries */
   871     for( i=0; i<MAX_TEXTURES; i++ ) {
   872         assert( slot_found[i] != 0 );
   873     }
   874 }
   876 /**
   877  * Dump the contents of the texture cache
   878  */
   879 void texcache_dump()
   880 {
   881     unsigned i;
   882     GLboolean boolresult;
   883     for( i=0; i< PVR2_RAM_PAGES; i++ ) {
   884         int slot = texcache_page_lookup[i];
   885         while( slot != EMPTY_ENTRY ) {
   886             fprintf( stderr, "%-3d: %08X %dx%d (%08X %08X) %s\n", slot,
   887                     texcache_active_list[slot].texture_addr,
   888                     POLY2_TEX_WIDTH(texcache_active_list[slot].poly2_mode),
   889                     POLY2_TEX_HEIGHT(texcache_active_list[slot].poly2_mode),
   890                     texcache_active_list[slot].poly2_mode,
   891                     texcache_active_list[slot].tex_mode,
   892 #ifdef HAVE_OPENGL_TEX_RESIDENT
   893                     (glAreTexturesResident(1, &texcache_active_list[slot].texture_id, &boolresult) ? "[RESIDENT]" : "[NOT RESIDENT]")
   894 #else
   895                     ""
   896 #endif
   897                     );
   898             slot = texcache_active_list[slot].next;
   899         }
   900     }
   901 }
   903 void texcache_print_idx4( uint32_t texture_addr, int width )
   904 {
   905     unsigned x,y;
   906     int src_bytes = (width*width>>1);
   907     char tmp[src_bytes];
   908     char data[width*width];
   909     pvr2_vram64_read_twiddled_4( tmp, texture_addr, width, width );
   910     decode_pal4_to_pal8( data, tmp, src_bytes );
   911     for( y=0; y<width; y++ ) {
   912         for( x=0; x<width; x++ ) {
   913             printf( "%1x", data[y*width+x] );
   914         }
   915         printf( "\n" );
   916     }
   917 }
.