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