Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 126:988003554ab5
prev113:ce4eb7959d56
next129:88e5ebc991e3
author nkeynes
date Thu Mar 23 13:18:51 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Add correct support for ARGB4444 direct colour
view annotate diff log raw
     1 /**
     2  * $Id: texcache.c,v 1.5 2006-03-23 13:18:51 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;
   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 detwiddle_pal8_to_24(int x1, int y1, int size, int totsize,
   183 				 char **in, uint32_t *out, uint32_t *pal) {
   184     if (size == 1) {
   185 	out[y1 * totsize + x1] = pal[**in];
   186 	(*in)++;
   187     } else {
   188 	int ns = size>>1;
   189 	detwiddle_pal8_to_24(x1, y1, ns, totsize, in, out, pal);
   190 	detwiddle_pal8_to_24(x1, y1+ns, ns, totsize, in, out, pal);
   191 	detwiddle_pal8_to_24(x1+ns, y1, ns, totsize, in, out, pal);
   192 	detwiddle_pal8_to_24(x1+ns, y1+ns, ns, totsize, in, out, pal);
   193     }
   194 }
   196 static void detwiddle_16_to_16(int x1, int y1, int size, int totsize,
   197 			       uint16_t **in, uint16_t *out ) {
   198     if (size == 1) {
   199 	out[y1 * totsize + x1] = **in;
   200 	(*in)++;
   201     } else {
   202 	int ns = size>>1;
   203 	detwiddle_16_to_16(x1, y1, ns, totsize, in, out);
   204 	detwiddle_16_to_16(x1, y1+ns, ns, totsize, in, out);
   205 	detwiddle_16_to_16(x1+ns, y1, ns, totsize, in, out);
   206 	detwiddle_16_to_16(x1+ns, y1+ns, ns, totsize, in, out);
   207     }
   208 }
   211 /**
   212  * Load texture data from the given address and parameters into the currently
   213  * bound OpenGL texture.
   214  */
   215 static texcache_load_texture( uint32_t texture_addr, int width, int height,
   216 			      int mode ) {
   217     uint32_t bytes = width * height;
   218     int shift = 1;
   219     GLint intFormat, format, type;
   220     int tex_format = mode & PVR2_TEX_FORMAT_MASK;
   222     if( tex_format == PVR2_TEX_FORMAT_IDX8 ||
   223 	tex_format == PVR2_TEX_FORMAT_IDX4 ) {
   224 	switch( MMIO_READ( PVR2, PALETTECFG ) & 0x03 ) {
   225 	case 0: /* ARGB1555 */
   226 	    intFormat = GL_RGB5_A1;
   227 	    format = GL_RGBA;
   228 	    type = GL_UNSIGNED_SHORT_5_5_5_1;
   229 	    break;
   230 	case 1: 
   231 	    intFormat = GL_RGB;
   232 	    format = GL_RGB;
   233 	    type = GL_UNSIGNED_SHORT_5_6_5;
   234 	    break;
   235 	case 2:
   236 	    intFormat = GL_RGBA4;
   237 	    format = GL_RGBA;
   238 	    type = GL_UNSIGNED_SHORT_4_4_4_4;
   239 	    break;
   240 	case 3:
   241 	    intFormat = GL_RGBA8;
   242 	    format = GL_BGRA;
   243 	    type = GL_UNSIGNED_INT_8_8_8_8_REV;
   244 	    shift = 2;
   245 	    break;
   246 	}
   248 	if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
   249 	    unsigned char data[bytes<<shift];
   250 	    int bank = (mode >> 25) &0x03;
   251 	    char *palette = mmio_region_PVR2PAL.mem + (bank * (256 << shift));
   252 	    int i;
   253 	    if( shift == 2 ) {
   254 		char tmp[bytes];
   255 	        char *p = tmp;
   256 		pvr2_vram64_read( tmp, texture_addr, bytes );
   257 		detwiddle_pal8_to_24( 0, 0, width, width, &p, 
   258 				      (uint32_t *)data, (uint32_t *)palette );
   259 	    } else {
   260 		pvr2_vram64_read( &data, texture_addr, bytes );
   261 		for( i=bytes-1; i>=0; i-- ) {
   262 		    char ch = data[i];
   263 		    ((uint16_t *)data)[i] = ((uint16_t *)palette)[ch];
   264 		}
   265 		/* FIXME */
   266 	    }
   267 	    glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
   268 			  data );
   270 	}
   271     } else {
   272 	switch( tex_format ) {
   273 	case PVR2_TEX_FORMAT_ARGB1555:
   274 	    bytes <<= 1;
   275 	    intFormat = GL_RGB5_A1;
   276 	    format = GL_RGBA;
   277 	    type = GL_UNSIGNED_SHORT_5_5_5_1;
   278 	    break;
   279 	case PVR2_TEX_FORMAT_RGB565:
   280 	    bytes <<= 1;
   281 	    intFormat = GL_RGBA;
   282 	    format = GL_RGBA;
   283 	    type = GL_UNSIGNED_SHORT_5_6_5;
   284 	    break;
   285 	case PVR2_TEX_FORMAT_ARGB4444:
   286 	    bytes <<= 1;
   287 	    intFormat = GL_RGBA4;
   288 	    format = GL_BGRA;
   289 	    type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   290 	    break;
   291 	case PVR2_TEX_FORMAT_YUV422:
   292 	    ERROR( "YUV textures not supported" );
   293 	    break;
   294 	case PVR2_TEX_FORMAT_BUMPMAP:
   295 	    ERROR( "Bumpmap not supported" );
   296 	    break;
   297 	case PVR2_TEX_FORMAT_IDX4:
   298 	    /* Supported? */
   299 	    bytes >>= 1;
   300 	    intFormat = GL_INTENSITY4;
   301 	    format = GL_COLOR_INDEX;
   302 	    type = GL_UNSIGNED_BYTE;
   303 	    shift = 0;
   304 	    break;
   305 	case PVR2_TEX_FORMAT_IDX8:
   306 	    intFormat = GL_INTENSITY8;
   307 	    format = GL_COLOR_INDEX;
   308 	    type = GL_UNSIGNED_BYTE;
   309 	    shift = 0;
   310 	    break;
   311 	}
   313 	unsigned char data[bytes];
   314 	/* load data from image, detwiddling/uncompressing as required */
   315 	if( PVR2_TEX_IS_COMPRESSED(mode) ) {
   316 	    ERROR( "VQ Compression not supported" );
   317 	} else {
   318 	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
   319 		char tmp[bytes];
   320 		uint16_t *p = (uint16_t *)tmp;
   321 		pvr2_vram64_read( tmp, texture_addr, bytes );
   322 		/* Untwiddle */
   323 		detwiddle_16_to_16( 0, 0, width, width, &p, (uint16_t *)&data );
   324 	    } else {
   325 		pvr2_vram64_read( data, texture_addr, bytes );
   326 	    }
   327 	}
   329 	/* Pass to GL */
   330 	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
   331 		      data );
   332     }
   333     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   334     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   335 }
   337 /**
   338  * Return a texture ID for the texture specified at the supplied address
   339  * and given parameters (the same sequence of bytes could in theory have
   340  * multiple interpretations). We use the texture address as the primary
   341  * index, but allow for multiple instances at each address. The texture
   342  * will be bound to the GL_TEXTURE_2D target before being returned.
   343  * 
   344  * If the texture has already been bound, return the ID to which it was
   345  * bound. Otherwise obtain an unused texture ID and set it up appropriately.
   346  */
   347 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
   348 			     int mode )
   349 {
   350     uint32_t texture_page = texture_addr >> 12;
   351     texcache_entry_index idx = texcache_page_lookup[texture_page];
   352     while( idx != EMPTY_ENTRY ) {
   353 	texcache_entry_t entry = &texcache_active_list[idx];
   354 	if( entry->texture_addr == texture_addr &&
   355 	    entry->mode == mode &&
   356 	    entry->width == width &&
   357 	    entry->height == height ) {
   358 	    entry->lru_count = texcache_ref_counter++;
   359 	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
   360 	    return entry->texture_id;
   361 	}
   362         idx = entry->next;
   363     }
   365     /* Not found - check the free list */
   366     int slot = 0;
   368     if( texcache_free_ptr < MAX_TEXTURES ) {
   369 	slot = texcache_free_list[texcache_free_ptr++];
   370     } else {
   371 	slot = texcache_evict();
   372     }
   374     /* Construct new entry */
   375     texcache_active_list[slot].texture_addr = texture_addr;
   376     texcache_active_list[slot].width = width;
   377     texcache_active_list[slot].height = height;
   378     texcache_active_list[slot].mode = mode;
   379     texcache_active_list[slot].lru_count = texcache_ref_counter++;
   381     /* Add entry to the lookup table */
   382     texcache_active_list[slot].next = texcache_page_lookup[texture_page];
   383     texcache_page_lookup[texture_page] = slot;
   385     /* Construct the GL texture */
   386     glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
   387     texcache_load_texture( texture_addr, width, height, mode );
   389     return texcache_active_list[slot].texture_id;
   390 }
.