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