Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 561:533f6b478071
prev477:9a373f2ff009
next635:76c63aac3590
next653:3202ff01d48e
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@103
     1
/**
nkeynes@561
     2
 * $Id$
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@349
    28
#define MAX_TEXTURES 256
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@462
    41
#define EMPTY_ENTRY -1
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@462
    54
static texcache_entry_index 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@462
   127
    assert( texcache_active_list[slot].texture_addr != -1 );
nkeynes@270
   128
    uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
nkeynes@103
   129
    texcache_entry_index replace_next = texcache_active_list[slot].next;
nkeynes@337
   130
    texcache_active_list[slot].texture_addr = -1;
nkeynes@103
   131
    texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
nkeynes@103
   132
    if( texcache_page_lookup[evict_page] == slot ) {
nkeynes@103
   133
	texcache_page_lookup[evict_page] = replace_next;
nkeynes@103
   134
    } else {
nkeynes@103
   135
	texcache_entry_index idx = texcache_page_lookup[evict_page];
nkeynes@103
   136
	texcache_entry_index next;
nkeynes@103
   137
	do {
nkeynes@103
   138
	    next = texcache_active_list[idx].next;
nkeynes@103
   139
	    if( next == slot ) {
nkeynes@462
   140
		assert( idx != replace_next );
nkeynes@103
   141
		texcache_active_list[idx].next = replace_next;
nkeynes@103
   142
		break;
nkeynes@103
   143
	    }
nkeynes@103
   144
	    idx = next;
nkeynes@103
   145
	} while( next != EMPTY_ENTRY );
nkeynes@103
   146
    }
nkeynes@337
   147
}
nkeynes@337
   148
nkeynes@337
   149
/**
nkeynes@337
   150
 * Evict a single texture from the cache.
nkeynes@337
   151
 * @return the slot of the evicted texture.
nkeynes@337
   152
 */
nkeynes@337
   153
static texcache_entry_index texcache_evict_lru( void )
nkeynes@337
   154
{
nkeynes@337
   155
    /* Full table scan - take over the entry with the lowest lru value */
nkeynes@337
   156
    texcache_entry_index slot = 0;
nkeynes@337
   157
    int lru_value = texcache_active_list[0].lru_count;
nkeynes@337
   158
    int i;
nkeynes@337
   159
    for( i=1; i<MAX_TEXTURES; i++ ) {
nkeynes@337
   160
	/* FIXME: account for rollover */
nkeynes@337
   161
	if( texcache_active_list[i].lru_count < lru_value ) {
nkeynes@337
   162
	    slot = i;
nkeynes@337
   163
	    lru_value = texcache_active_list[i].lru_count;
nkeynes@337
   164
	}
nkeynes@337
   165
    }
nkeynes@337
   166
    texcache_evict(slot);
nkeynes@337
   167
    
nkeynes@103
   168
    return slot;
nkeynes@103
   169
}
nkeynes@103
   170
nkeynes@337
   171
/**
nkeynes@337
   172
 * Evict all textures contained in the page identified by a texture address.
nkeynes@337
   173
 */
nkeynes@337
   174
void texcache_invalidate_page( uint32_t texture_addr ) {
nkeynes@337
   175
    uint32_t texture_page = texture_addr >> 12;
nkeynes@337
   176
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@337
   177
    if( idx == EMPTY_ENTRY )
nkeynes@337
   178
	return;
nkeynes@337
   179
    assert( texcache_free_ptr >= 0 );
nkeynes@337
   180
    do {
nkeynes@337
   181
	texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@337
   182
	entry->texture_addr = -1;
nkeynes@337
   183
	/* release entry */
nkeynes@337
   184
	texcache_free_ptr--;
nkeynes@337
   185
	texcache_free_list[texcache_free_ptr] = idx;
nkeynes@337
   186
	idx = entry->next;
nkeynes@337
   187
	entry->next = EMPTY_ENTRY;
nkeynes@337
   188
    } while( idx != EMPTY_ENTRY );
nkeynes@337
   189
    texcache_page_lookup[texture_page] = EMPTY_ENTRY;
nkeynes@337
   190
}
nkeynes@337
   191
nkeynes@337
   192
/**
nkeynes@337
   193
 * Mark all textures that use the palette table as needing a re-read (ie 
nkeynes@337
   194
 * for when the palette is changed. We could track exactly which ones are 
nkeynes@337
   195
 * affected, but it's not clear that the extra maintanence overhead is 
nkeynes@337
   196
 * worthwhile.
nkeynes@337
   197
 */
nkeynes@337
   198
void texcache_invalidate_palette( )
nkeynes@337
   199
{
nkeynes@337
   200
    int i;
nkeynes@337
   201
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@337
   202
	if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@337
   203
	    PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
nkeynes@337
   204
	    texcache_evict( i );
nkeynes@462
   205
	    texcache_free_ptr--;
nkeynes@462
   206
	    texcache_free_list[texcache_free_ptr] = i;
nkeynes@337
   207
	}
nkeynes@337
   208
    }
nkeynes@337
   209
}
nkeynes@337
   210
nkeynes@311
   211
static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   212
{
nkeynes@311
   213
    int i;
nkeynes@311
   214
    for( i=0; i<inbytes; i++ ) {
nkeynes@311
   215
	*out++ = pal[*in++];
nkeynes@129
   216
    }
nkeynes@129
   217
}
nkeynes@129
   218
nkeynes@321
   219
static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   220
{
nkeynes@311
   221
    int i;
nkeynes@311
   222
    for( i=0; i<inbytes; i++ ) {
nkeynes@321
   223
	*out++ = (uint16_t)pal[*in++];
nkeynes@113
   224
    }
nkeynes@113
   225
}
nkeynes@113
   226
nkeynes@315
   227
static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   228
{
nkeynes@315
   229
    int i;
nkeynes@315
   230
    for( i=0; i<inbytes; i++ ) {
nkeynes@315
   231
	*out++ = pal[*in & 0x0F];
nkeynes@315
   232
	*out++ = pal[(*in >> 4)];
nkeynes@315
   233
	in++;
nkeynes@315
   234
    }
nkeynes@315
   235
}
nkeynes@315
   236
nkeynes@315
   237
nkeynes@321
   238
static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   239
{
nkeynes@315
   240
    int i;
nkeynes@315
   241
    for( i=0; i<inbytes; i++ ) {
nkeynes@321
   242
	*out++ = (uint16_t)pal[*in & 0x0F];
nkeynes@321
   243
	*out++ = (uint16_t)pal[(*in >> 4)];
nkeynes@315
   244
	in++;
nkeynes@315
   245
    }
nkeynes@315
   246
}
nkeynes@315
   247
nkeynes@224
   248
#define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
nkeynes@224
   249
nkeynes@224
   250
struct vq_codebook {
nkeynes@224
   251
    uint16_t quad[256][4];
nkeynes@224
   252
};
nkeynes@224
   253
nkeynes@270
   254
static void vq_get_codebook( struct vq_codebook *codebook, 
nkeynes@270
   255
				uint16_t *input )
nkeynes@270
   256
{
nkeynes@270
   257
    /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
nkeynes@270
   258
    uint16_t *p = (uint16_t *)input;
nkeynes@270
   259
    int i;
nkeynes@270
   260
    for( i=0; i<256; i++ ) {
nkeynes@270
   261
	codebook->quad[i][0] = *p++;
nkeynes@270
   262
	codebook->quad[i][2] = *p++;
nkeynes@270
   263
	codebook->quad[i][1] = *p++;
nkeynes@270
   264
	codebook->quad[i][3] = *p++;
nkeynes@270
   265
    }
nkeynes@270
   266
}    
nkeynes@270
   267
nkeynes@429
   268
static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
nkeynes@311
   269
		       struct vq_codebook *codebook ) {
nkeynes@224
   270
    int i,j;
nkeynes@224
   271
    
nkeynes@270
   272
    uint8_t *c = (uint8_t *)input;
nkeynes@311
   273
    for( j=0; j<height; j+=2 ) {
nkeynes@311
   274
	for( i=0; i<width; i+=2 ) {
nkeynes@313
   275
	    uint8_t code = *c++;
nkeynes@311
   276
	    output[i + j*width] = codebook->quad[code][0];
nkeynes@311
   277
	    output[i + 1 + j*width] = codebook->quad[code][1];
nkeynes@311
   278
	    output[i + (j+1)*width] = codebook->quad[code][2];
nkeynes@311
   279
	    output[i + 1 + (j+1)*width] = codebook->quad[code][3];
nkeynes@224
   280
	}
nkeynes@224
   281
    }
nkeynes@224
   282
}
nkeynes@113
   283
nkeynes@282
   284
static inline uint32_t yuv_to_rgb32( float y, float u, float v )
nkeynes@282
   285
{
nkeynes@282
   286
    u -= 128;
nkeynes@282
   287
    v -= 128;
nkeynes@282
   288
    int r = (int)(y + v*1.375);
nkeynes@282
   289
    int g = (int)(y - u*0.34375 - v*0.6875);
nkeynes@282
   290
    int b = (int)(y + u*1.71875);
nkeynes@282
   291
    if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
nkeynes@282
   292
    if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
nkeynes@282
   293
    if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
nkeynes@289
   294
    return 0xFF000000 | (r<<16) | (g<<8) | (b);
nkeynes@282
   295
}
nkeynes@282
   296
nkeynes@282
   297
nkeynes@282
   298
/**
nkeynes@311
   299
 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
nkeynes@282
   300
 * directly support this format unfortunately. The input data is formatted as
nkeynes@282
   301
 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
nkeynes@282
   302
 * in floating point.
nkeynes@282
   303
 */
nkeynes@311
   304
static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
nkeynes@282
   305
{
nkeynes@282
   306
    int x, y;
nkeynes@282
   307
    uint32_t *p = input;
nkeynes@282
   308
    for( y=0; y<height; y++ ) {
nkeynes@282
   309
	for( x=0; x<width; x+=2 ) {
nkeynes@282
   310
	    float u = (float)(*p & 0xFF);
nkeynes@282
   311
	    float y0 = (float)( (*p>>8)&0xFF );
nkeynes@282
   312
	    float v = (float)( (*p>>16)&0xFF );
nkeynes@282
   313
	    float y1 = (float)( (*p>>24)&0xFF );
nkeynes@282
   314
	    *output++ = yuv_to_rgb32( y0, u, v ); 
nkeynes@282
   315
	    *output++ = yuv_to_rgb32( y1, u, v );
nkeynes@287
   316
	    p++;
nkeynes@282
   317
	}
nkeynes@282
   318
    }
nkeynes@282
   319
}
nkeynes@282
   320
nkeynes@103
   321
/**
nkeynes@103
   322
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   323
 * bound OpenGL texture.
nkeynes@103
   324
 */
nkeynes@429
   325
static void texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@103
   326
			      int mode ) {
nkeynes@284
   327
    int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
nkeynes@349
   328
    GLint intFormat = GL_RGBA, format, type;
nkeynes@108
   329
    int tex_format = mode & PVR2_TEX_FORMAT_MASK;
nkeynes@270
   330
    struct vq_codebook codebook;
nkeynes@270
   331
    GLint filter = GL_LINEAR;
nkeynes@108
   332
nkeynes@352
   333
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
nkeynes@270
   334
    /* Decode the format parameters */
nkeynes@270
   335
    switch( tex_format ) {
nkeynes@270
   336
    case PVR2_TEX_FORMAT_IDX4:
nkeynes@270
   337
    case PVR2_TEX_FORMAT_IDX8:
nkeynes@284
   338
	/* For indexed-colour modes, we need to lookup the palette control
nkeynes@284
   339
	 * word to determine the de-indexed texture format.
nkeynes@284
   340
	 */
nkeynes@191
   341
	switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
nkeynes@108
   342
	case 0: /* ARGB1555 */
nkeynes@329
   343
	    format = GL_BGRA;
nkeynes@129
   344
	    type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@108
   345
	    break;
nkeynes@284
   346
	case 1:  /* RGB565 */
nkeynes@349
   347
	    intFormat = GL_RGB;
nkeynes@108
   348
	    format = GL_RGB;
nkeynes@300
   349
	    type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@108
   350
	    break;
nkeynes@284
   351
	case 2: /* ARGB4444 */
nkeynes@129
   352
	    format = GL_BGRA;
nkeynes@129
   353
	    type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@108
   354
	    break;
nkeynes@284
   355
	case 3: /* ARGB8888 */
nkeynes@113
   356
	    format = GL_BGRA;
nkeynes@477
   357
	    type = GL_UNSIGNED_BYTE;
nkeynes@284
   358
	    bpp_shift = 2;
nkeynes@108
   359
	    break;
nkeynes@429
   360
	default:
nkeynes@429
   361
	    return; /* Can't happen, but it makes gcc stop complaining */
nkeynes@108
   362
	}
nkeynes@270
   363
	break;
nkeynes@270
   364
	    
nkeynes@270
   365
    case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@329
   366
	format = GL_BGRA;
nkeynes@270
   367
	type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@270
   368
	break;
nkeynes@270
   369
    case PVR2_TEX_FORMAT_RGB565:
nkeynes@349
   370
	intFormat = GL_RGB;
nkeynes@270
   371
	format = GL_RGB;
nkeynes@300
   372
	type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@270
   373
	break;
nkeynes@270
   374
    case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@270
   375
	format = GL_BGRA;
nkeynes@270
   376
	type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@270
   377
	break;
nkeynes@270
   378
    case PVR2_TEX_FORMAT_YUV422:
nkeynes@284
   379
	/* YUV422 isn't directly supported by most implementations, so decode
nkeynes@284
   380
	 * it to a (reasonably) standard ARGB32.
nkeynes@284
   381
	 */
nkeynes@284
   382
	bpp_shift = 2;
nkeynes@282
   383
	format = GL_BGRA;
nkeynes@477
   384
	type = GL_UNSIGNED_BYTE;
nkeynes@270
   385
	break;
nkeynes@270
   386
    case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@270
   387
	ERROR( "Bumpmap not supported" );
nkeynes@429
   388
	return;
nkeynes@429
   389
    default:
nkeynes@429
   390
	ERROR( "Undefined texture format" );
nkeynes@429
   391
	return;
nkeynes@270
   392
    }
nkeynes@270
   393
	
nkeynes@321
   394
    if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
nkeynes@321
   395
	tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@284
   396
	/* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
nkeynes@284
   397
	uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
nkeynes@429
   398
	unsigned char data[(width*height) << bpp_shift];
nkeynes@284
   399
	if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@429
   400
	    unsigned char tmp[(width*height)<<1];
nkeynes@291
   401
	    pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
nkeynes@311
   402
	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
nkeynes@284
   403
	} else {
nkeynes@291
   404
	    pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
nkeynes@284
   405
	}
nkeynes@284
   406
	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
nkeynes@284
   407
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
nkeynes@284
   408
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@284
   409
	return;
nkeynes@284
   410
    } 
nkeynes@284
   411
nkeynes@349
   412
    int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
nkeynes@270
   413
    if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
nkeynes@270
   414
	int i;
nkeynes@311
   415
	for( i=0; 1<<i < width; i++ );
nkeynes@270
   416
	last_level = i;
nkeynes@313
   417
	mip_width = 2;
nkeynes@313
   418
	mip_height= 2;
nkeynes@270
   419
	filter = GL_LINEAR_MIPMAP_LINEAR;
nkeynes@270
   420
    }
nkeynes@349
   421
    dest_bytes = (mip_width * mip_height) << bpp_shift;
nkeynes@349
   422
    src_bytes = dest_bytes; // Modes will change this (below)
nkeynes@108
   423
nkeynes@270
   424
    if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@270
   425
	uint16_t tmp[VQ_CODEBOOK_SIZE];
nkeynes@429
   426
	pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
nkeynes@270
   427
	texture_addr += VQ_CODEBOOK_SIZE;
nkeynes@270
   428
	vq_get_codebook( &codebook, tmp );
nkeynes@270
   429
    }
nkeynes@270
   430
nkeynes@270
   431
    for( level=last_level; level>= 0; level-- ) {
nkeynes@429
   432
	unsigned char data[dest_bytes];
nkeynes@270
   433
	/* load data from image, detwiddling/uncompressing as required */
nkeynes@108
   434
	if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@349
   435
	    src_bytes = (mip_width * mip_height);
nkeynes@108
   436
	    int bank = (mode >> 25) &0x03;
nkeynes@324
   437
	    uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
nkeynes@429
   438
	    unsigned char tmp[src_bytes];
nkeynes@311
   439
	    pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
nkeynes@284
   440
	    if( bpp_shift == 2 ) {
nkeynes@349
   441
		decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@113
   442
	    } else {
nkeynes@349
   443
		decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@108
   444
	    }
nkeynes@315
   445
	} else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@349
   446
	    src_bytes = (mip_width * mip_height) >> 1;
nkeynes@315
   447
	    int bank = (mode >>21 ) & 0x3F;
nkeynes@324
   448
	    uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
nkeynes@429
   449
	    unsigned char tmp[src_bytes];
nkeynes@315
   450
	    pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@315
   451
	    if( bpp_shift == 2 ) {
nkeynes@349
   452
		decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@315
   453
	    } else {
nkeynes@349
   454
		decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@315
   455
	    }
nkeynes@282
   456
	} else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@349
   457
	    src_bytes = ((mip_width*mip_height)<<1);
nkeynes@429
   458
	    unsigned char tmp[src_bytes];
nkeynes@314
   459
	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@314
   460
		pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
nkeynes@314
   461
	    } else {
nkeynes@349
   462
		pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@314
   463
	    }
nkeynes@311
   464
	    yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
nkeynes@270
   465
	} else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@349
   466
	    src_bytes = ((mip_width*mip_height) >> 2);
nkeynes@429
   467
	    unsigned char tmp[src_bytes];
nkeynes@311
   468
	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@313
   469
		pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
nkeynes@311
   470
	    } else {
nkeynes@349
   471
		pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@311
   472
	    }
nkeynes@311
   473
	    vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
nkeynes@270
   474
	} else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@311
   475
	    pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
nkeynes@270
   476
	} else {
nkeynes@349
   477
	    pvr2_vram64_read( data, texture_addr, src_bytes );
nkeynes@108
   478
	}
nkeynes@313
   479
	
nkeynes@108
   480
	/* Pass to GL */
nkeynes@313
   481
	if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
nkeynes@313
   482
	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
nkeynes@313
   483
			  data + (3 << bpp_shift) );
nkeynes@349
   484
	    texture_addr += src_bytes;
nkeynes@313
   485
	} else {
nkeynes@313
   486
	    glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
nkeynes@313
   487
			  data );
nkeynes@349
   488
	    texture_addr += src_bytes;
nkeynes@313
   489
	    mip_width <<= 1;
nkeynes@313
   490
	    mip_height <<= 1;
nkeynes@349
   491
	    dest_bytes <<= 2;
nkeynes@349
   492
	    src_bytes <<= 2;
nkeynes@313
   493
	}
nkeynes@103
   494
    }
nkeynes@270
   495
nkeynes@270
   496
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
nkeynes@108
   497
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@103
   498
}
nkeynes@103
   499
nkeynes@103
   500
/**
nkeynes@103
   501
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   502
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   503
 * multiple interpretations). We use the texture address as the primary
nkeynes@103
   504
 * index, but allow for multiple instances at each address. The texture
nkeynes@103
   505
 * will be bound to the GL_TEXTURE_2D target before being returned.
nkeynes@103
   506
 * 
nkeynes@103
   507
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   508
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@103
   509
 */
nkeynes@103
   510
GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
nkeynes@103
   511
			     int mode )
nkeynes@103
   512
{
nkeynes@103
   513
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   514
    texcache_entry_index next;
nkeynes@103
   515
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   516
    while( idx != EMPTY_ENTRY ) {
nkeynes@103
   517
	texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@103
   518
	if( entry->texture_addr == texture_addr &&
nkeynes@103
   519
	    entry->mode == mode &&
nkeynes@103
   520
	    entry->width == width &&
nkeynes@103
   521
	    entry->height == height ) {
nkeynes@103
   522
	    entry->lru_count = texcache_ref_counter++;
nkeynes@103
   523
	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
nkeynes@103
   524
	    return entry->texture_id;
nkeynes@103
   525
	}
nkeynes@103
   526
        idx = entry->next;
nkeynes@103
   527
    }
nkeynes@103
   528
nkeynes@103
   529
    /* Not found - check the free list */
nkeynes@462
   530
    texcache_entry_index slot = 0;
nkeynes@103
   531
nkeynes@103
   532
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@103
   533
	slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   534
    } else {
nkeynes@337
   535
	slot = texcache_evict_lru();
nkeynes@103
   536
    }
nkeynes@103
   537
nkeynes@103
   538
    /* Construct new entry */
nkeynes@103
   539
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@103
   540
    texcache_active_list[slot].width = width;
nkeynes@103
   541
    texcache_active_list[slot].height = height;
nkeynes@103
   542
    texcache_active_list[slot].mode = mode;
nkeynes@103
   543
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   544
nkeynes@103
   545
    /* Add entry to the lookup table */
nkeynes@462
   546
    next = texcache_page_lookup[texture_page];
nkeynes@462
   547
    if( next == slot ) {
nkeynes@462
   548
	int i;
nkeynes@462
   549
	fprintf( stderr, "Active list: " );
nkeynes@462
   550
	for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@462
   551
	    fprintf( stderr, "%d, ", texcache_active_list[i].next );
nkeynes@462
   552
	}
nkeynes@462
   553
	fprintf( stderr, "\n" );
nkeynes@462
   554
	assert( next != slot );
nkeynes@462
   555
	    
nkeynes@462
   556
    }
nkeynes@462
   557
    assert( next != slot );
nkeynes@462
   558
    texcache_active_list[slot].next = next;
nkeynes@103
   559
    texcache_page_lookup[texture_page] = slot;
nkeynes@103
   560
nkeynes@103
   561
    /* Construct the GL texture */
nkeynes@108
   562
    glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
nkeynes@103
   563
    texcache_load_texture( texture_addr, width, height, mode );
nkeynes@103
   564
    
nkeynes@103
   565
    return texcache_active_list[slot].texture_id;
nkeynes@103
   566
}
.