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