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