Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 314:2a1f3b3d8708
prev313:7e4bd1629268
next315:2d8ba198d62c
author nkeynes
date Tue Jan 23 09:30:59 2007 +0000 (17 years ago)
permissions -rw-r--r--
last change Enable twiddled YUV - no idea if it will actually work though
view annotate diff log raw
     1 /**
     2  * $Id: texcache.c,v 1.18 2007-01-23 09:30:59 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 64
    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     }
    70     texcache_free_ptr = 0;
    71     texcache_ref_counter = 0;
    72 }
    74 /**
    75  * Setup the initial texture ids (must be called after the GL context is
    76  * prepared)
    77  */
    78 void texcache_gl_init( )
    79 {
    80     int i;
    81     GLuint texids[MAX_TEXTURES];
    83     glGenTextures( MAX_TEXTURES, texids );
    84     for( i=0; i<MAX_TEXTURES; i++ ) {
    85 	texcache_active_list[i].texture_id = texids[i];
    86     }
    87 }
    89 /**
    90  * Flush all textures from the cache, returning them to the free list.
    91  */
    92 void texcache_flush( )
    93 {
    94     int i;
    95     /* clear structures */
    96     for( i=0; i<PVR2_RAM_PAGES; i++ ) {
    97 	texcache_page_lookup[i] = EMPTY_ENTRY;
    98     }
    99     for( i=0; i<MAX_TEXTURES; i++ ) {
   100 	texcache_free_list[i] = i;
   101     }
   102     texcache_free_ptr = 0;
   103     texcache_ref_counter = 0;
   104 }
   106 /**
   107  * Flush all textures and delete. The cache will be non-functional until
   108  * the next call to texcache_init(). This would typically be done if
   109  * switching GL targets.
   110  */    
   111 void texcache_shutdown( )
   112 {
   113     GLuint texids[MAX_TEXTURES];
   114     int i;
   115     texcache_flush();
   117     for( i=0; i<MAX_TEXTURES; i++ ) {
   118 	texids[i] = texcache_active_list[i].texture_id;
   119     }
   120     glDeleteTextures( MAX_TEXTURES, texids );
   121 }
   123 /**
   124  * Evict all textures contained in the page identified by a texture address.
   125  */
   126 void texcache_invalidate_page( uint32_t texture_addr ) {
   127     uint32_t texture_page = texture_addr >> 12;
   128     texcache_entry_index idx = texcache_page_lookup[texture_page];
   129     if( idx == EMPTY_ENTRY )
   130 	return;
   131     assert( texcache_free_ptr >= 0 );
   132     do {
   133 	texcache_entry_t entry = &texcache_active_list[idx];	
   134 	/* release entry */
   135 	texcache_free_ptr--;
   136 	texcache_free_list[texcache_free_ptr] = idx;
   137 	idx = entry->next;
   138 	entry->next = EMPTY_ENTRY;
   139     } while( idx != EMPTY_ENTRY );
   140     texcache_page_lookup[texture_page] = EMPTY_ENTRY;
   141 }
   143 /**
   144  * Evict a single texture from the cache.
   145  * @return the slot of the evicted texture.
   146  */
   147 static texcache_entry_index texcache_evict( void )
   148 {
   149     /* Full table scan - take over the entry with the lowest lru value */
   150     texcache_entry_index slot = 0;
   151     int lru_value = texcache_active_list[0].lru_count;
   152     int i;
   153     for( i=1; i<MAX_TEXTURES; i++ ) {
   154 	/* FIXME: account for rollover */
   155 	if( texcache_active_list[i].lru_count < lru_value ) {
   156 	    slot = i;
   157 	    lru_value = texcache_active_list[i].lru_count;
   158 	}
   159     }
   161     /* Remove the selected slot from the lookup table */
   162     uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
   163     texcache_entry_index replace_next = texcache_active_list[slot].next;
   164     texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
   165     if( texcache_page_lookup[evict_page] == slot ) {
   166 	texcache_page_lookup[evict_page] = replace_next;
   167     } else {
   168 	texcache_entry_index idx = texcache_page_lookup[evict_page];
   169 	texcache_entry_index next;
   170 	do {
   171 	    next = texcache_active_list[idx].next;
   172 	    if( next == slot ) {
   173 		texcache_active_list[idx].next = replace_next;
   174 		break;
   175 	    }
   176 	    idx = next;
   177 	} while( next != EMPTY_ENTRY );
   178     }
   179     return slot;
   180 }
   182 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
   183 {
   184     int i;
   185     for( i=0; i<inbytes; i++ ) {
   186 	*out++ = pal[*in++];
   187     }
   188 }
   190 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint16_t *pal )
   191 {
   192     int i;
   193     for( i=0; i<inbytes; i++ ) {
   194 	*out++ = pal[*in++];
   195     }
   196 }
   198 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
   200 struct vq_codebook {
   201     uint16_t quad[256][4];
   202 };
   204 static void vq_get_codebook( struct vq_codebook *codebook, 
   205 				uint16_t *input )
   206 {
   207     /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
   208     uint16_t *p = (uint16_t *)input;
   209     int i;
   210     for( i=0; i<256; i++ ) {
   211 	codebook->quad[i][0] = *p++;
   212 	codebook->quad[i][2] = *p++;
   213 	codebook->quad[i][1] = *p++;
   214 	codebook->quad[i][3] = *p++;
   215     }
   216 }    
   218 static void vq_decode( uint16_t *output, char *input, int width, int height, 
   219 		       struct vq_codebook *codebook ) {
   220     int i,j;
   222     uint8_t *c = (uint8_t *)input;
   223     for( j=0; j<height; j+=2 ) {
   224 	for( i=0; i<width; i+=2 ) {
   225 	    uint8_t code = *c++;
   226 	    output[i + j*width] = codebook->quad[code][0];
   227 	    output[i + 1 + j*width] = codebook->quad[code][1];
   228 	    output[i + (j+1)*width] = codebook->quad[code][2];
   229 	    output[i + 1 + (j+1)*width] = codebook->quad[code][3];
   230 	}
   231     }
   232 }
   234 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
   235 {
   236     u -= 128;
   237     v -= 128;
   238     int r = (int)(y + v*1.375);
   239     int g = (int)(y - u*0.34375 - v*0.6875);
   240     int b = (int)(y + u*1.71875);
   241     if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
   242     if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
   243     if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
   244     return 0xFF000000 | (r<<16) | (g<<8) | (b);
   245 }
   248 /**
   249  * Convert raster YUV texture data into RGB32 data - most GL implementations don't
   250  * directly support this format unfortunately. The input data is formatted as
   251  * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
   252  * in floating point.
   253  */
   254 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
   255 {
   256     int x, y;
   257     uint32_t *p = input;
   258     for( y=0; y<height; y++ ) {
   259 	for( x=0; x<width; x+=2 ) {
   260 	    float u = (float)(*p & 0xFF);
   261 	    float y0 = (float)( (*p>>8)&0xFF );
   262 	    float v = (float)( (*p>>16)&0xFF );
   263 	    float y1 = (float)( (*p>>24)&0xFF );
   264 	    *output++ = yuv_to_rgb32( y0, u, v ); 
   265 	    *output++ = yuv_to_rgb32( y1, u, v );
   266 	    p++;
   267 	}
   268     }
   269 }
   271 /**
   272  * Load texture data from the given address and parameters into the currently
   273  * bound OpenGL texture.
   274  */
   275 static texcache_load_texture( uint32_t texture_addr, int width, int height,
   276 			      int mode ) {
   277     int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
   278     GLint intFormat, format, type;
   279     int tex_format = mode & PVR2_TEX_FORMAT_MASK;
   280     struct vq_codebook codebook;
   281     GLint filter = GL_LINEAR;
   283     /* Decode the format parameters */
   284     switch( tex_format ) {
   285     case PVR2_TEX_FORMAT_IDX4:
   286 	ERROR( "4-bit indexed textures not supported" );
   287     case PVR2_TEX_FORMAT_IDX8:
   288 	/* For indexed-colour modes, we need to lookup the palette control
   289 	 * word to determine the de-indexed texture format.
   290 	 */
   291 	switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
   292 	case 0: /* ARGB1555 */
   293 	    intFormat = GL_RGB5_A1;
   294 	    format = GL_RGBA;
   295 	    type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   296 	    break;
   297 	case 1:  /* RGB565 */
   298 	    intFormat = GL_RGB5;
   299 	    format = GL_RGB;
   300 	    type = GL_UNSIGNED_SHORT_5_6_5;
   301 	    break;
   302 	case 2: /* ARGB4444 */
   303 	    intFormat = GL_RGBA4;
   304 	    format = GL_BGRA;
   305 	    type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   306 	    break;
   307 	case 3: /* ARGB8888 */
   308 	    intFormat = GL_RGBA8;
   309 	    format = GL_BGRA;
   310 	    type = GL_UNSIGNED_INT_8_8_8_8_REV;
   311 	    bpp_shift = 2;
   312 	    break;
   313 	}
   314 	break;
   316     case PVR2_TEX_FORMAT_ARGB1555:
   317 	intFormat = GL_RGB5_A1;
   318 	format = GL_RGBA;
   319 	type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   320 	break;
   321     case PVR2_TEX_FORMAT_RGB565:
   322 	intFormat = GL_RGB5;
   323 	format = GL_RGB;
   324 	type = GL_UNSIGNED_SHORT_5_6_5;
   325 	break;
   326     case PVR2_TEX_FORMAT_ARGB4444:
   327 	intFormat = GL_RGBA4;
   328 	format = GL_BGRA;
   329 	type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   330 	break;
   331     case PVR2_TEX_FORMAT_YUV422:
   332 	/* YUV422 isn't directly supported by most implementations, so decode
   333 	 * it to a (reasonably) standard ARGB32.
   334 	 */
   335 	bpp_shift = 2;
   336 	intFormat = GL_RGBA8;
   337 	format = GL_BGRA;
   338 	type = GL_UNSIGNED_INT_8_8_8_8_REV;
   339 	break;
   340     case PVR2_TEX_FORMAT_BUMPMAP:
   341 	ERROR( "Bumpmap not supported" );
   342 	break;
   343     }
   345     if( PVR2_TEX_IS_STRIDE(mode) ) {
   346 	/* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
   347 	uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
   348 	char data[(width*height) << bpp_shift];
   349 	if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   350 	    char tmp[(width*height)<<1];
   351 	    pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
   352 	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
   353 	} else {
   354 	    pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
   355 	}
   356 	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
   357 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   358 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   359 	return;
   360     } 
   362     int level=0, last_level = 0, mip_width = width, mip_height = height, mip_bytes;
   363     if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
   364 	int i;
   365 	for( i=0; 1<<i < width; i++ );
   366 	last_level = i;
   367 	mip_width = 2;
   368 	mip_height= 2;
   369 	filter = GL_LINEAR_MIPMAP_LINEAR;
   370     }
   371     mip_bytes = (mip_width * mip_height) << bpp_shift;
   373     if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   374 	uint16_t tmp[VQ_CODEBOOK_SIZE];
   375 	pvr2_vram64_read( (char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
   376 	texture_addr += VQ_CODEBOOK_SIZE;
   377 	vq_get_codebook( &codebook, tmp );
   378     }
   380     for( level=last_level; level>= 0; level-- ) {
   381 	char data[mip_bytes];
   382 	/* load data from image, detwiddling/uncompressing as required */
   383 	if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   384 	    int inputlength = mip_bytes >> bpp_shift;
   385 	    int bank = (mode >> 25) &0x03;
   386 	    char *palette = mmio_region_PVR2PAL.mem + (bank * (256 << bpp_shift));
   387 	    char tmp[inputlength];
   388 	    char *p = tmp;
   389 	    pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
   390 	    if( bpp_shift == 2 ) {
   391 		decode_pal8_to_32( (uint32_t *)data, tmp, inputlength, (uint32_t*)palette );
   392 	    } else {
   393 		decode_pal8_to_16( (uint16_t *)data, tmp, inputlength, (uint16_t*)palette );
   394 	    }
   395 	} else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
   396 	    int inputlength = ((mip_width*mip_height)<<1);
   397 	    char tmp[inputlength];
   398 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   399 		pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
   400 	    } else {
   401 		pvr2_vram64_read( tmp, texture_addr, inputlength );
   402 	    }
   403 	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
   404 	} else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   405 	    int inputlength = ((mip_width*mip_height) >> 2);
   406 	    char tmp[inputlength];
   407 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   408 		pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
   409 	    } else {
   410 		pvr2_vram64_read( tmp, texture_addr, inputlength );
   411 	    }
   412 	    vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
   413 	} else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   414 	    pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
   415 	} else {
   416 	    pvr2_vram64_read( data, texture_addr, mip_bytes );
   417 	}
   419 	/* Pass to GL */
   420 	if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
   421 	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
   422 			  data + (3 << bpp_shift) );
   423 	} else {
   424 	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
   425 			  data );
   426 	    texture_addr += mip_bytes;
   427 	    mip_width <<= 1;
   428 	    mip_height <<= 1;
   429 	    mip_bytes <<= 2;
   430 	}
   431     }
   433     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   434     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   435 }
   437 /**
   438  * Return a texture ID for the texture specified at the supplied address
   439  * and given parameters (the same sequence of bytes could in theory have
   440  * multiple interpretations). We use the texture address as the primary
   441  * index, but allow for multiple instances at each address. The texture
   442  * will be bound to the GL_TEXTURE_2D target before being returned.
   443  * 
   444  * If the texture has already been bound, return the ID to which it was
   445  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   446  */
   447 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
   448 			     int mode )
   449 {
   450     uint32_t texture_page = texture_addr >> 12;
   451     texcache_entry_index idx = texcache_page_lookup[texture_page];
   452     while( idx != EMPTY_ENTRY ) {
   453 	texcache_entry_t entry = &texcache_active_list[idx];
   454 	if( entry->texture_addr == texture_addr &&
   455 	    entry->mode == mode &&
   456 	    entry->width == width &&
   457 	    entry->height == height ) {
   458 	    entry->lru_count = texcache_ref_counter++;
   459 	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
   460 	    return entry->texture_id;
   461 	}
   462         idx = entry->next;
   463     }
   465     /* Not found - check the free list */
   466     int slot = 0;
   468     if( texcache_free_ptr < MAX_TEXTURES ) {
   469 	slot = texcache_free_list[texcache_free_ptr++];
   470     } else {
   471 	slot = texcache_evict();
   472     }
   474     /* Construct new entry */
   475     texcache_active_list[slot].texture_addr = texture_addr;
   476     texcache_active_list[slot].width = width;
   477     texcache_active_list[slot].height = height;
   478     texcache_active_list[slot].mode = mode;
   479     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   481     /* Add entry to the lookup table */
   482     texcache_active_list[slot].next = texcache_page_lookup[texture_page];
   483     texcache_page_lookup[texture_page] = slot;
   485     /* Construct the GL texture */
   486     glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   487     texcache_load_texture( texture_addr, width, height, mode );
   489     return texcache_active_list[slot].texture_id;
   490 }
.