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