Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 337:cdd757aa8e8c
prev329:4b453d68e9ae
next349:05c9b25c361d
author nkeynes
date Sun Jan 28 11:36:00 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Invalidate palette textures when the palette changes (fixes a few texture
bugs)
Fix odd/even selection in pvr2_display_frame
view annotate diff log raw
     1 /**
     2  * $Id: texcache.c,v 1.24 2007-01-28 11:36:00 nkeynes Exp $
     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 "pvr2/pvr2.h"
    24 /** Specifies the maximum number of OpenGL
    25  * textures we're willing to have open at a time. If more are
    26  * needed, textures will be evicted in LRU order.
    27  */
    28 #define MAX_TEXTURES 128
    30 /**
    31  * Data structure:
    32  *
    33  * Main operations:
    34  *    find entry by texture_addr
    35  *    add new entry
    36  *    move entry to tail of lru list
    37  *    remove entry
    38  */
    40 typedef signed short texcache_entry_index;
    41 #define EMPTY_ENTRY 0xFF
    43 static texcache_entry_index texcache_free_ptr = 0;
    44 static GLuint texcache_free_list[MAX_TEXTURES];
    46 typedef struct texcache_entry {
    47     uint32_t texture_addr;
    48     int width, height, mode;
    49     GLuint texture_id;
    50     texcache_entry_index next;
    51     uint32_t lru_count;
    52 } *texcache_entry_t;
    54 static uint8_t texcache_page_lookup[PVR2_RAM_PAGES];
    55 static uint32_t texcache_ref_counter;
    56 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
    58 /**
    59  * Initialize the texture cache.
    60  */
    61 void texcache_init( )
    62 {
    63     int i;
    64     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    65 	texcache_page_lookup[i] = EMPTY_ENTRY;
    66     }
    67     for( i=0; i<MAX_TEXTURES; i++ ) {
    68 	texcache_free_list[i] = i;
    69 	texcache_active_list[i].texture_addr = -1;
    70     }
    71     texcache_free_ptr = 0;
    72     texcache_ref_counter = 0;
    73 }
    75 /**
    76  * Setup the initial texture ids (must be called after the GL context is
    77  * prepared)
    78  */
    79 void texcache_gl_init( )
    80 {
    81     int i;
    82     GLuint texids[MAX_TEXTURES];
    84     glGenTextures( MAX_TEXTURES, texids );
    85     for( i=0; i<MAX_TEXTURES; i++ ) {
    86 	texcache_active_list[i].texture_id = texids[i];
    87     }
    88 }
    90 /**
    91  * Flush all textures from the cache, returning them to the free list.
    92  */
    93 void texcache_flush( )
    94 {
    95     int i;
    96     /* clear structures */
    97     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    98 	texcache_page_lookup[i] = EMPTY_ENTRY;
    99     }
   100     for( i=0; i<MAX_TEXTURES; i++ ) {
   101 	texcache_free_list[i] = i;
   102     }
   103     texcache_free_ptr = 0;
   104     texcache_ref_counter = 0;
   105 }
   107 /**
   108  * Flush all textures and delete. The cache will be non-functional until
   109  * the next call to texcache_init(). This would typically be done if
   110  * switching GL targets.
   111  */    
   112 void texcache_shutdown( )
   113 {
   114     GLuint texids[MAX_TEXTURES];
   115     int i;
   116     texcache_flush();
   118     for( i=0; i<MAX_TEXTURES; i++ ) {
   119 	texids[i] = texcache_active_list[i].texture_id;
   120     }
   121     glDeleteTextures( MAX_TEXTURES, texids );
   122 }
   124 static void texcache_evict( int slot )
   125 {
   126     /* Remove the selected slot from the lookup table */
   127     uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
   128     texcache_entry_index replace_next = texcache_active_list[slot].next;
   129     texcache_active_list[slot].texture_addr = -1;
   130     texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
   131     if( texcache_page_lookup[evict_page] == slot ) {
   132 	texcache_page_lookup[evict_page] = replace_next;
   133     } else {
   134 	texcache_entry_index idx = texcache_page_lookup[evict_page];
   135 	texcache_entry_index next;
   136 	do {
   137 	    next = texcache_active_list[idx].next;
   138 	    if( next == slot ) {
   139 		texcache_active_list[idx].next = replace_next;
   140 		break;
   141 	    }
   142 	    idx = next;
   143 	} while( next != EMPTY_ENTRY );
   144     }
   145 }
   147 /**
   148  * Evict a single texture from the cache.
   149  * @return the slot of the evicted texture.
   150  */
   151 static texcache_entry_index texcache_evict_lru( void )
   152 {
   153     /* Full table scan - take over the entry with the lowest lru value */
   154     texcache_entry_index slot = 0;
   155     int lru_value = texcache_active_list[0].lru_count;
   156     int i;
   157     for( i=1; i<MAX_TEXTURES; i++ ) {
   158 	/* FIXME: account for rollover */
   159 	if( texcache_active_list[i].lru_count < lru_value ) {
   160 	    slot = i;
   161 	    lru_value = texcache_active_list[i].lru_count;
   162 	}
   163     }
   164     texcache_evict(slot);
   166     return slot;
   167 }
   169 /**
   170  * Evict all textures contained in the page identified by a texture address.
   171  */
   172 void texcache_invalidate_page( uint32_t texture_addr ) {
   173     uint32_t texture_page = texture_addr >> 12;
   174     texcache_entry_index idx = texcache_page_lookup[texture_page];
   175     if( idx == EMPTY_ENTRY )
   176 	return;
   177     assert( texcache_free_ptr >= 0 );
   178     do {
   179 	texcache_entry_t entry = &texcache_active_list[idx];
   180 	entry->texture_addr = -1;
   181 	/* release entry */
   182 	texcache_free_ptr--;
   183 	texcache_free_list[texcache_free_ptr] = idx;
   184 	idx = entry->next;
   185 	entry->next = EMPTY_ENTRY;
   186     } while( idx != EMPTY_ENTRY );
   187     texcache_page_lookup[texture_page] = EMPTY_ENTRY;
   188 }
   190 /**
   191  * Mark all textures that use the palette table as needing a re-read (ie 
   192  * for when the palette is changed. We could track exactly which ones are 
   193  * affected, but it's not clear that the extra maintanence overhead is 
   194  * worthwhile.
   195  */
   196 void texcache_invalidate_palette( )
   197 {
   198     int i;
   199     for( i=0; i<MAX_TEXTURES; i++ ) {
   200 	if( texcache_active_list[i].texture_addr != -1 &&
   201 	    PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
   202 	    texcache_evict( i );
   203 	}
   204     }
   205 }
   207 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   208 {
   209     int i;
   210     for( i=0; i<inbytes; i++ ) {
   211 	*out++ = pal[*in++];
   212     }
   213 }
   215 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   216 {
   217     int i;
   218     for( i=0; i<inbytes; i++ ) {
   219 	*out++ = (uint16_t)pal[*in++];
   220     }
   221 }
   223 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   224 {
   225     int i;
   226     for( i=0; i<inbytes; i++ ) {
   227 	*out++ = pal[*in & 0x0F];
   228 	*out++ = pal[(*in >> 4)];
   229 	in++;
   230     }
   231 }
   234 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   235 {
   236     int i;
   237     for( i=0; i<inbytes; i++ ) {
   238 	*out++ = (uint16_t)pal[*in & 0x0F];
   239 	*out++ = (uint16_t)pal[(*in >> 4)];
   240 	in++;
   241     }
   242 }
   244 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
   246 struct vq_codebook {
   247     uint16_t quad[256][4];
   248 };
   250 static void vq_get_codebook( struct vq_codebook *codebook, 
   251 				uint16_t *input )
   252 {
   253     /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
   254     uint16_t *p = (uint16_t *)input;
   255     int i;
   256     for( i=0; i<256; i++ ) {
   257 	codebook->quad[i][0] = *p++;
   258 	codebook->quad[i][2] = *p++;
   259 	codebook->quad[i][1] = *p++;
   260 	codebook->quad[i][3] = *p++;
   261     }
   262 }    
   264 static void vq_decode( uint16_t *output, char *input, int width, int height, 
   265 		       struct vq_codebook *codebook ) {
   266     int i,j;
   268     uint8_t *c = (uint8_t *)input;
   269     for( j=0; j<height; j+=2 ) {
   270 	for( i=0; i<width; i+=2 ) {
   271 	    uint8_t code = *c++;
   272 	    output[i + j*width] = codebook->quad[code][0];
   273 	    output[i + 1 + j*width] = codebook->quad[code][1];
   274 	    output[i + (j+1)*width] = codebook->quad[code][2];
   275 	    output[i + 1 + (j+1)*width] = codebook->quad[code][3];
   276 	}
   277     }
   278 }
   280 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
   281 {
   282     u -= 128;
   283     v -= 128;
   284     int r = (int)(y + v*1.375);
   285     int g = (int)(y - u*0.34375 - v*0.6875);
   286     int b = (int)(y + u*1.71875);
   287     if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
   288     if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
   289     if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
   290     return 0xFF000000 | (r<<16) | (g<<8) | (b);
   291 }
   294 /**
   295  * Convert raster YUV texture data into RGB32 data - most GL implementations don't
   296  * directly support this format unfortunately. The input data is formatted as
   297  * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
   298  * in floating point.
   299  */
   300 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
   301 {
   302     int x, y;
   303     uint32_t *p = input;
   304     for( y=0; y<height; y++ ) {
   305 	for( x=0; x<width; x+=2 ) {
   306 	    float u = (float)(*p & 0xFF);
   307 	    float y0 = (float)( (*p>>8)&0xFF );
   308 	    float v = (float)( (*p>>16)&0xFF );
   309 	    float y1 = (float)( (*p>>24)&0xFF );
   310 	    *output++ = yuv_to_rgb32( y0, u, v ); 
   311 	    *output++ = yuv_to_rgb32( y1, u, v );
   312 	    p++;
   313 	}
   314     }
   315 }
   317 /**
   318  * Load texture data from the given address and parameters into the currently
   319  * bound OpenGL texture.
   320  */
   321 static texcache_load_texture( uint32_t texture_addr, int width, int height,
   322 			      int mode ) {
   323     int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
   324     GLint intFormat, format, type;
   325     int tex_format = mode & PVR2_TEX_FORMAT_MASK;
   326     struct vq_codebook codebook;
   327     GLint filter = GL_LINEAR;
   329     /* Decode the format parameters */
   330     switch( tex_format ) {
   331     case PVR2_TEX_FORMAT_IDX4:
   332     case PVR2_TEX_FORMAT_IDX8:
   333 	/* For indexed-colour modes, we need to lookup the palette control
   334 	 * word to determine the de-indexed texture format.
   335 	 */
   336 	switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
   337 	case 0: /* ARGB1555 */
   338 	    intFormat = GL_RGB5_A1;
   339 	    format = GL_BGRA;
   340 	    type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   341 	    break;
   342 	case 1:  /* RGB565 */
   343 	    intFormat = GL_RGB5;
   344 	    format = GL_RGB;
   345 	    type = GL_UNSIGNED_SHORT_5_6_5;
   346 	    break;
   347 	case 2: /* ARGB4444 */
   348 	    intFormat = GL_RGBA4;
   349 	    format = GL_BGRA;
   350 	    type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   351 	    break;
   352 	case 3: /* ARGB8888 */
   353 	    intFormat = GL_RGBA8;
   354 	    format = GL_BGRA;
   355 	    type = GL_UNSIGNED_INT_8_8_8_8_REV;
   356 	    bpp_shift = 2;
   357 	    break;
   358 	}
   359 	break;
   361     case PVR2_TEX_FORMAT_ARGB1555:
   362 	intFormat = GL_RGB5_A1;
   363 	format = GL_BGRA;
   364 	type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   365 	break;
   366     case PVR2_TEX_FORMAT_RGB565:
   367 	intFormat = GL_RGB5;
   368 	format = GL_RGB;
   369 	type = GL_UNSIGNED_SHORT_5_6_5;
   370 	break;
   371     case PVR2_TEX_FORMAT_ARGB4444:
   372 	intFormat = GL_RGBA4;
   373 	format = GL_BGRA;
   374 	type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   375 	break;
   376     case PVR2_TEX_FORMAT_YUV422:
   377 	/* YUV422 isn't directly supported by most implementations, so decode
   378 	 * it to a (reasonably) standard ARGB32.
   379 	 */
   380 	bpp_shift = 2;
   381 	intFormat = GL_RGBA8;
   382 	format = GL_BGRA;
   383 	type = GL_UNSIGNED_INT_8_8_8_8_REV;
   384 	break;
   385     case PVR2_TEX_FORMAT_BUMPMAP:
   386 	ERROR( "Bumpmap not supported" );
   387 	break;
   388     }
   390     if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
   391 	tex_format != PVR2_TEX_FORMAT_IDX8 ) {
   392 	/* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
   393 	uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
   394 	char data[(width*height) << bpp_shift];
   395 	if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   396 	    char tmp[(width*height)<<1];
   397 	    pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
   398 	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
   399 	} else {
   400 	    pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
   401 	}
   402 	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
   403 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   404 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   405 	return;
   406     } 
   408     int level=0, last_level = 0, mip_width = width, mip_height = height, mip_bytes;
   409     if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
   410 	int i;
   411 	for( i=0; 1<<i < width; i++ );
   412 	last_level = i;
   413 	mip_width = 2;
   414 	mip_height= 2;
   415 	filter = GL_LINEAR_MIPMAP_LINEAR;
   416     }
   417     mip_bytes = (mip_width * mip_height) << bpp_shift;
   419     if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   420 	uint16_t tmp[VQ_CODEBOOK_SIZE];
   421 	pvr2_vram64_read( (char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
   422 	texture_addr += VQ_CODEBOOK_SIZE;
   423 	vq_get_codebook( &codebook, tmp );
   424     }
   426     for( level=last_level; level>= 0; level-- ) {
   427 	char data[mip_bytes];
   428 	/* load data from image, detwiddling/uncompressing as required */
   429 	if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   430 	    int inputlength = mip_bytes >> bpp_shift;
   431 	    int bank = (mode >> 25) &0x03;
   432 	    uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
   433 	    char tmp[inputlength];
   434 	    pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
   435 	    if( bpp_shift == 2 ) {
   436 		decode_pal8_to_32( (uint32_t *)data, tmp, inputlength, palette );
   437 	    } else {
   438 		decode_pal8_to_16( (uint16_t *)data, tmp, inputlength, palette );
   439 	    }
   440 	} else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   441 	    int inputlength = (mip_width * mip_height) >> 1;
   442 	    int bank = (mode >>21 ) & 0x3F;
   443 	    uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
   444 	    char tmp[inputlength];
   445 	    pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
   446 	    if( bpp_shift == 2 ) {
   447 		decode_pal4_to_32( (uint32_t *)data, tmp, inputlength, palette );
   448 	    } else {
   449 		decode_pal4_to_16( (uint16_t *)data, tmp, inputlength, palette );
   450 	    }
   451 	} else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   452 	    int inputlength = ((mip_width*mip_height)<<1);
   453 	    char tmp[inputlength];
   454 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   455 		pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
   456 	    } else {
   457 		pvr2_vram64_read( tmp, texture_addr, inputlength );
   458 	    }
   459 	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
   460 	} else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   461 	    int inputlength = ((mip_width*mip_height) >> 2);
   462 	    char tmp[inputlength];
   463 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   464 		pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
   465 	    } else {
   466 		pvr2_vram64_read( tmp, texture_addr, inputlength );
   467 	    }
   468 	    vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
   469 	} else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   470 	    pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
   471 	} else {
   472 	    pvr2_vram64_read( data, texture_addr, mip_bytes );
   473 	}
   475 	/* Pass to GL */
   476 	if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
   477 	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
   478 			  data + (3 << bpp_shift) );
   479 	    texture_addr += mip_bytes;
   480 	} else {
   481 	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
   482 			  data );
   483 	    texture_addr += mip_bytes;
   484 	    mip_width <<= 1;
   485 	    mip_height <<= 1;
   486 	    mip_bytes <<= 2;
   487 	}
   488     }
   490     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   491     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   492 }
   494 /**
   495  * Return a texture ID for the texture specified at the supplied address
   496  * and given parameters (the same sequence of bytes could in theory have
   497  * multiple interpretations). We use the texture address as the primary
   498  * index, but allow for multiple instances at each address. The texture
   499  * will be bound to the GL_TEXTURE_2D target before being returned.
   500  * 
   501  * If the texture has already been bound, return the ID to which it was
   502  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   503  */
   504 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
   505 			     int mode )
   506 {
   507     uint32_t texture_page = texture_addr >> 12;
   508     texcache_entry_index idx = texcache_page_lookup[texture_page];
   509     while( idx != EMPTY_ENTRY ) {
   510 	texcache_entry_t entry = &texcache_active_list[idx];
   511 	if( entry->texture_addr == texture_addr &&
   512 	    entry->mode == mode &&
   513 	    entry->width == width &&
   514 	    entry->height == height ) {
   515 	    entry->lru_count = texcache_ref_counter++;
   516 	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
   517 	    return entry->texture_id;
   518 	}
   519         idx = entry->next;
   520     }
   522     /* Not found - check the free list */
   523     int slot = 0;
   525     if( texcache_free_ptr < MAX_TEXTURES ) {
   526 	slot = texcache_free_list[texcache_free_ptr++];
   527     } else {
   528 	slot = texcache_evict_lru();
   529     }
   531     /* Construct new entry */
   532     texcache_active_list[slot].texture_addr = texture_addr;
   533     texcache_active_list[slot].width = width;
   534     texcache_active_list[slot].height = height;
   535     texcache_active_list[slot].mode = mode;
   536     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   538     /* Add entry to the lookup table */
   539     texcache_active_list[slot].next = texcache_page_lookup[texture_page];
   540     texcache_page_lookup[texture_page] = slot;
   542     /* Construct the GL texture */
   543     glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   544     texcache_load_texture( texture_addr, width, height, mode );
   546     return texcache_active_list[slot].texture_id;
   547 }
.