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