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